Coverage Report

Created: 2017-04-29 12:21

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/lib/External/isl/isl_val.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2013      Ecole Normale Superieure
3
 *
4
 * Use of this software is governed by the MIT license
5
 *
6
 * Written by Sven Verdoolaege,
7
 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
8
 */
9
10
#include <isl_int.h>
11
#include <isl_ctx_private.h>
12
#include <isl_val_private.h>
13
14
#undef BASE
15
#define BASE val
16
17
#include <isl_list_templ.c>
18
19
/* Allocate an isl_val object with indeterminate value.
20
 */
21
__isl_give isl_val *isl_val_alloc(isl_ctx *ctx)
22
100k
{
23
100k
  isl_val *v;
24
100k
25
100k
  v = isl_alloc_type(ctx, struct isl_val);
26
100k
  if (!v)
27
0
    return NULL;
28
100k
29
100k
  v->ctx = ctx;
30
100k
  isl_ctx_ref(ctx);
31
100k
  v->ref = 1;
32
100k
  isl_int_init(v->n);
33
100k
  isl_int_init(v->d);
34
100k
35
100k
  return v;
36
100k
}
37
38
/* Return a reference to an isl_val representing zero.
39
 */
40
__isl_give isl_val *isl_val_zero(isl_ctx *ctx)
41
4.92k
{
42
4.92k
  return isl_val_int_from_si(ctx, 0);
43
4.92k
}
44
45
/* Return a reference to an isl_val representing one.
46
 */
47
__isl_give isl_val *isl_val_one(isl_ctx *ctx)
48
27
{
49
27
  return isl_val_int_from_si(ctx, 1);
50
27
}
51
52
/* Return a reference to an isl_val representing negative one.
53
 */
54
__isl_give isl_val *isl_val_negone(isl_ctx *ctx)
55
0
{
56
0
  return isl_val_int_from_si(ctx, -1);
57
0
}
58
59
/* Return a reference to an isl_val representing NaN.
60
 */
61
__isl_give isl_val *isl_val_nan(isl_ctx *ctx)
62
821
{
63
821
  isl_val *v;
64
821
65
821
  v = isl_val_alloc(ctx);
66
821
  if (!v)
67
0
    return NULL;
68
821
69
821
  
isl_int_set_si821
(v->n, 0);821
70
821
  isl_int_set_si(v->d, 0);
71
821
72
821
  return v;
73
821
}
74
75
/* Change "v" into a NaN.
76
 */
77
__isl_give isl_val *isl_val_set_nan(__isl_take isl_val *v)
78
13
{
79
13
  if (!v)
80
0
    return NULL;
81
13
  
if (13
isl_val_is_nan(v)13
)
82
0
    return v;
83
13
  v = isl_val_cow(v);
84
13
  if (!v)
85
0
    return NULL;
86
13
87
13
  
isl_int_set_si13
(v->n, 0);13
88
13
  isl_int_set_si(v->d, 0);
89
13
90
13
  return v;
91
13
}
92
93
/* Return a reference to an isl_val representing +infinity.
94
 */
95
__isl_give isl_val *isl_val_infty(isl_ctx *ctx)
96
245
{
97
245
  isl_val *v;
98
245
99
245
  v = isl_val_alloc(ctx);
100
245
  if (!v)
101
0
    return NULL;
102
245
103
245
  
isl_int_set_si245
(v->n, 1);245
104
245
  isl_int_set_si(v->d, 0);
105
245
106
245
  return v;
107
245
}
108
109
/* Return a reference to an isl_val representing -infinity.
110
 */
111
__isl_give isl_val *isl_val_neginfty(isl_ctx *ctx)
112
32
{
113
32
  isl_val *v;
114
32
115
32
  v = isl_val_alloc(ctx);
116
32
  if (!v)
117
0
    return NULL;
118
32
119
32
  
isl_int_set_si32
(v->n, -1);32
120
32
  isl_int_set_si(v->d, 0);
121
32
122
32
  return v;
123
32
}
124
125
/* Return a reference to an isl_val representing the integer "i".
126
 */
127
__isl_give isl_val *isl_val_int_from_si(isl_ctx *ctx, long i)
128
19.5k
{
129
19.5k
  isl_val *v;
130
19.5k
131
19.5k
  v = isl_val_alloc(ctx);
132
19.5k
  if (!v)
133
0
    return NULL;
134
19.5k
135
19.5k
  
isl_int_set_si19.5k
(v->n, i);19.5k
136
19.5k
  isl_int_set_si(v->d, 1);
137
19.5k
138
19.5k
  return v;
139
19.5k
}
140
141
/* Change the value of "v" to be equal to the integer "i".
142
 */
143
__isl_give isl_val *isl_val_set_si(__isl_take isl_val *v, long i)
144
6
{
145
6
  if (!v)
146
0
    return NULL;
147
6
  
if (6
isl_val_is_int(v) && 6
isl_int_cmp_si6
(v->n, i) == 06
)
148
0
    return v;
149
6
  v = isl_val_cow(v);
150
6
  if (!v)
151
0
    return NULL;
152
6
153
6
  
isl_int_set_si6
(v->n, i);6
154
6
  isl_int_set_si(v->d, 1);
155
6
156
6
  return v;
157
6
}
158
159
/* Change the value of "v" to be equal to zero.
160
 */
161
__isl_give isl_val *isl_val_set_zero(__isl_take isl_val *v)
162
4
{
163
4
  return isl_val_set_si(v, 0);
164
4
}
165
166
/* Return a reference to an isl_val representing the unsigned integer "u".
167
 */
168
__isl_give isl_val *isl_val_int_from_ui(isl_ctx *ctx, unsigned long u)
169
5.42k
{
170
5.42k
  isl_val *v;
171
5.42k
172
5.42k
  v = isl_val_alloc(ctx);
173
5.42k
  if (!v)
174
0
    return NULL;
175
5.42k
176
5.42k
  
isl_int_set_ui5.42k
(v->n, u);5.42k
177
5.42k
  isl_int_set_si(v->d, 1);
178
5.42k
179
5.42k
  return v;
180
5.42k
}
181
182
/* Return a reference to an isl_val representing the integer "n".
183
 */
184
__isl_give isl_val *isl_val_int_from_isl_int(isl_ctx *ctx, isl_int n)
185
16.9k
{
186
16.9k
  isl_val *v;
187
16.9k
188
16.9k
  v = isl_val_alloc(ctx);
189
16.9k
  if (!v)
190
0
    return NULL;
191
16.9k
192
16.9k
  
isl_int_set16.9k
(v->n, n);16.9k
193
16.9k
  isl_int_set_si(v->d, 1);
194
16.9k
195
16.9k
  return v;
196
16.9k
}
197
198
/* Return a reference to an isl_val representing the rational value "n"/"d".
199
 * Normalizing the isl_val (if needed) is left to the caller.
200
 */
201
__isl_give isl_val *isl_val_rat_from_isl_int(isl_ctx *ctx,
202
  isl_int n, isl_int d)
203
33.0k
{
204
33.0k
  isl_val *v;
205
33.0k
206
33.0k
  v = isl_val_alloc(ctx);
207
33.0k
  if (!v)
208
0
    return NULL;
209
33.0k
210
33.0k
  
isl_int_set33.0k
(v->n, n);33.0k
211
33.0k
  isl_int_set(v->d, d);
212
33.0k
213
33.0k
  return v;
214
33.0k
}
215
216
/* Return a new reference to "v".
217
 */
218
__isl_give isl_val *isl_val_copy(__isl_keep isl_val *v)
219
23.5k
{
220
23.5k
  if (!v)
221
0
    return NULL;
222
23.5k
223
23.5k
  v->ref++;
224
23.5k
  return v;
225
23.5k
}
226
227
/* Return a fresh copy of "val".
228
 */
229
__isl_give isl_val *isl_val_dup(__isl_keep isl_val *val)
230
5.53k
{
231
5.53k
  isl_val *dup;
232
5.53k
233
5.53k
  if (!val)
234
0
    return NULL;
235
5.53k
236
5.53k
  dup = isl_val_alloc(isl_val_get_ctx(val));
237
5.53k
  if (!dup)
238
0
    return NULL;
239
5.53k
240
5.53k
  
isl_int_set5.53k
(dup->n, val->n);5.53k
241
5.53k
  isl_int_set(dup->d, val->d);
242
5.53k
243
5.53k
  return dup;
244
5.53k
}
245
246
/* Return an isl_val that is equal to "val" and that has only
247
 * a single reference.
248
 */
249
__isl_give isl_val *isl_val_cow(__isl_take isl_val *val)
250
13.7k
{
251
13.7k
  if (!val)
252
0
    return NULL;
253
13.7k
254
13.7k
  
if (13.7k
val->ref == 113.7k
)
255
8.24k
    return val;
256
5.53k
  val->ref--;
257
5.53k
  return isl_val_dup(val);
258
13.7k
}
259
260
/* Free "v" and return NULL.
261
 */
262
__isl_null isl_val *isl_val_free(__isl_take isl_val *v)
263
121k
{
264
121k
  if (!v)
265
2.98k
    return NULL;
266
121k
267
118k
  
if (118k
--v->ref > 0118k
)
268
18.0k
    return NULL;
269
118k
270
100k
  isl_ctx_deref(v->ctx);
271
100k
  isl_int_clear(v->n);
272
100k
  isl_int_clear(v->d);
273
100k
  free(v);
274
100k
  return NULL;
275
118k
}
276
277
/* Extract the numerator of a rational value "v" as an integer.
278
 *
279
 * If "v" is not a rational value, then the result is undefined.
280
 */
281
long isl_val_get_num_si(__isl_keep isl_val *v)
282
779
{
283
779
  if (!v)
284
0
    return 0;
285
779
  
if (779
!isl_val_is_rat(v)779
)
286
0
    isl_die(isl_val_get_ctx(v), isl_error_invalid,
287
779
      "expecting rational value", return 0);
288
779
  
if (779
!779
isl_int_fits_slong779
(v->n))
289
0
    isl_die(isl_val_get_ctx(v), isl_error_invalid,
290
779
      "numerator too large", return 0);
291
779
  
return 779
isl_int_get_si779
(v->n);
292
779
}
293
294
/* Extract the numerator of a rational value "v" as an isl_int.
295
 *
296
 * If "v" is not a rational value, then the result is undefined.
297
 */
298
int isl_val_get_num_isl_int(__isl_keep isl_val *v, isl_int *n)
299
3
{
300
3
  if (!v)
301
0
    return -1;
302
3
  
if (3
!isl_val_is_rat(v)3
)
303
0
    isl_die(isl_val_get_ctx(v), isl_error_invalid,
304
3
      "expecting rational value", return -1);
305
3
  
isl_int_set3
(*n, v->n);3
306
3
  return 0;
307
3
}
308
309
/* Extract the denominator of a rational value "v" as an integer.
310
 *
311
 * If "v" is not a rational value, then the result is undefined.
312
 */
313
long isl_val_get_den_si(__isl_keep isl_val *v)
314
0
{
315
0
  if (!v)
316
0
    return 0;
317
0
  
if (0
!isl_val_is_rat(v)0
)
318
0
    isl_die(isl_val_get_ctx(v), isl_error_invalid,
319
0
      "expecting rational value", return 0);
320
0
  
if (0
!0
isl_int_fits_slong0
(v->d))
321
0
    isl_die(isl_val_get_ctx(v), isl_error_invalid,
322
0
      "denominator too large", return 0);
323
0
  
return 0
isl_int_get_si0
(v->d);
324
0
}
325
326
/* Extract the denominator of a rational value "v" as an isl_val.
327
 *
328
 * If "v" is not a rational value, then the result is undefined.
329
 */
330
__isl_give isl_val *isl_val_get_den_val(__isl_keep isl_val *v)
331
0
{
332
0
  if (!v)
333
0
    return NULL;
334
0
  
if (0
!isl_val_is_rat(v)0
)
335
0
    isl_die(isl_val_get_ctx(v), isl_error_invalid,
336
0
      "expecting rational value", return NULL);
337
0
  return isl_val_int_from_isl_int(isl_val_get_ctx(v), v->d);
338
0
}
339
340
/* Return an approximation of "v" as a double.
341
 */
342
double isl_val_get_d(__isl_keep isl_val *v)
343
0
{
344
0
  if (!v)
345
0
    return 0;
346
0
  
if (0
!isl_val_is_rat(v)0
)
347
0
    isl_die(isl_val_get_ctx(v), isl_error_invalid,
348
0
      "expecting rational value", return 0);
349
0
  
return 0
isl_int_get_d0
(v->n) /
isl_int_get_d0
(v->d);
350
0
}
351
352
/* Return the isl_ctx to which "val" belongs.
353
 */
354
isl_ctx *isl_val_get_ctx(__isl_keep isl_val *val)
355
10.8k
{
356
10.8k
  return val ? val->ctx : NULL;
357
10.8k
}
358
359
/* Return a hash value that digests "val".
360
 */
361
uint32_t isl_val_get_hash(__isl_keep isl_val *val)
362
0
{
363
0
  uint32_t hash;
364
0
365
0
  if (!val)
366
0
    return 0;
367
0
368
0
  
hash = 0
isl_hash_init0
();
369
0
  hash = isl_int_hash(val->n, hash);
370
0
  hash = isl_int_hash(val->d, hash);
371
0
372
0
  return hash;
373
0
}
374
375
/* Normalize "v".
376
 *
377
 * In particular, make sure that the denominator of a rational value
378
 * is positive and the numerator and denominator do not have any
379
 * common divisors.
380
 *
381
 * This function should not be called by an external user
382
 * since it will only be given normalized values.
383
 */
384
__isl_give isl_val *isl_val_normalize(__isl_take isl_val *v)
385
36.1k
{
386
36.1k
  isl_ctx *ctx;
387
36.1k
388
36.1k
  if (!v)
389
0
    return NULL;
390
36.1k
  
if (36.1k
isl_val_is_int(v)36.1k
)
391
36.0k
    return v;
392
105
  
if (105
!isl_val_is_rat(v)105
)
393
0
    return v;
394
105
  
if (105
isl_int_is_neg105
(v->d))
{4
395
4
    isl_int_neg(v->d, v->d);
396
4
    isl_int_neg(v->n, v->n);
397
4
  }
398
105
  ctx = isl_val_get_ctx(v);
399
105
  isl_int_gcd(ctx->normalize_gcd, v->n, v->d);
400
105
  if (isl_int_is_one(ctx->normalize_gcd))
401
71
    return v;
402
34
  
isl_int_divexact34
(v->n, v->n, ctx->normalize_gcd);34
403
34
  isl_int_divexact(v->d, v->d, ctx->normalize_gcd);
404
34
  return v;
405
105
}
406
407
/* Return the opposite of "v".
408
 */
409
__isl_give isl_val *isl_val_neg(__isl_take isl_val *v)
410
2.70k
{
411
2.70k
  if (!v)
412
0
    return NULL;
413
2.70k
  
if (2.70k
isl_val_is_nan(v)2.70k
)
414
1
    return v;
415
2.70k
  
if (2.70k
isl_val_is_zero(v)2.70k
)
416
177
    return v;
417
2.70k
418
2.52k
  v = isl_val_cow(v);
419
2.52k
  if (!v)
420
0
    return NULL;
421
2.52k
  
isl_int_neg2.52k
(v->n, v->n);2.52k
422
2.52k
423
2.52k
  return v;
424
2.52k
}
425
426
/* Return the inverse of "v".
427
 */
428
__isl_give isl_val *isl_val_inv(__isl_take isl_val *v)
429
9
{
430
9
  if (!v)
431
0
    return NULL;
432
9
  
if (9
isl_val_is_nan(v)9
)
433
1
    return v;
434
8
  
if (8
isl_val_is_zero(v)8
)
{1
435
1
    isl_ctx *ctx = isl_val_get_ctx(v);
436
1
    isl_val_free(v);
437
1
    return isl_val_nan(ctx);
438
1
  }
439
7
  
if (7
isl_val_is_infty(v) || 7
isl_val_is_neginfty(v)6
)
{2
440
2
    isl_ctx *ctx = isl_val_get_ctx(v);
441
2
    isl_val_free(v);
442
2
    return isl_val_zero(ctx);
443
2
  }
444
7
445
5
  v = isl_val_cow(v);
446
5
  if (!v)
447
0
    return NULL;
448
5
  
isl_int_swap5
(v->n, v->d);5
449
5
450
5
  return isl_val_normalize(v);
451
5
}
452
453
/* Return the absolute value of "v".
454
 */
455
__isl_give isl_val *isl_val_abs(__isl_take isl_val *v)
456
647
{
457
647
  if (!v)
458
0
    return NULL;
459
647
  
if (647
isl_val_is_nan(v)647
)
460
1
    return v;
461
646
  
if (646
isl_val_is_nonneg(v)646
)
462
399
    return v;
463
247
  return isl_val_neg(v);
464
646
}
465
466
/* Return the "floor" (greatest integer part) of "v".
467
 * That is, return the result of rounding towards -infinity.
468
 */
469
__isl_give isl_val *isl_val_floor(__isl_take isl_val *v)
470
12
{
471
12
  if (!v)
472
0
    return NULL;
473
12
  
if (12
isl_val_is_int(v)12
)
474
3
    return v;
475
9
  
if (9
!isl_val_is_rat(v)9
)
476
3
    return v;
477
9
478
6
  v = isl_val_cow(v);
479
6
  if (!v)
480
0
    return NULL;
481
6
  
isl_int_fdiv_q6
(v->n, v->n, v->d);6
482
6
  isl_int_set_si(v->d, 1);
483
6
484
6
  return v;
485
6
}
486
487
/* Return the "ceiling" of "v".
488
 * That is, return the result of rounding towards +infinity.
489
 */
490
__isl_give isl_val *isl_val_ceil(__isl_take isl_val *v)
491
18
{
492
18
  if (!v)
493
0
    return NULL;
494
18
  
if (18
isl_val_is_int(v)18
)
495
11
    return v;
496
7
  
if (7
!isl_val_is_rat(v)7
)
497
3
    return v;
498
7
499
4
  v = isl_val_cow(v);
500
4
  if (!v)
501
0
    return NULL;
502
4
  
isl_int_cdiv_q4
(v->n, v->n, v->d);4
503
4
  isl_int_set_si(v->d, 1);
504
4
505
4
  return v;
506
4
}
507
508
/* Truncate "v".
509
 * That is, return the result of rounding towards zero.
510
 */
511
__isl_give isl_val *isl_val_trunc(__isl_take isl_val *v)
512
0
{
513
0
  if (!v)
514
0
    return NULL;
515
0
  
if (0
isl_val_is_int(v)0
)
516
0
    return v;
517
0
  
if (0
!isl_val_is_rat(v)0
)
518
0
    return v;
519
0
520
0
  v = isl_val_cow(v);
521
0
  if (!v)
522
0
    return NULL;
523
0
  
isl_int_tdiv_q0
(v->n, v->n, v->d);0
524
0
  isl_int_set_si(v->d, 1);
525
0
526
0
  return v;
527
0
}
528
529
/* Return 2^v, where v is an integer (that is not too large).
530
 */
531
__isl_give isl_val *isl_val_2exp(__isl_take isl_val *v)
532
5.44k
{
533
5.44k
  unsigned long exp;
534
5.44k
  int neg;
535
5.44k
536
5.44k
  v = isl_val_cow(v);
537
5.44k
  if (!v)
538
0
    return NULL;
539
5.44k
  
if (5.44k
!isl_val_is_int(v)5.44k
)
540
0
    isl_die(isl_val_get_ctx(v), isl_error_invalid,
541
5.44k
      "can only compute integer powers",
542
5.44k
      return isl_val_free(v));
543
5.44k
  neg = isl_val_is_neg(v);
544
5.44k
  if (neg)
545
2
    isl_int_neg(v->n, v->n);
546
5.44k
  if (
!5.44k
isl_int_fits_ulong5.44k
(v->n))
547
0
    isl_die(isl_val_get_ctx(v), isl_error_invalid,
548
5.44k
      "exponent too large", return isl_val_free(v));
549
5.44k
  
exp = 5.44k
isl_int_get_ui5.44k
(v->n);
550
5.44k
  if (
neg5.44k
)
{2
551
2
    isl_int_mul_2exp(v->d, v->d, exp);
552
2
    isl_int_set_si(v->n, 1);
553
5.44k
  } else {
554
5.44k
    isl_int_mul_2exp(v->n, v->d, exp);
555
5.44k
  }
556
5.44k
557
5.44k
  return v;
558
5.44k
}
559
560
/* Return the minimum of "v1" and "v2".
561
 */
562
__isl_give isl_val *isl_val_min(__isl_take isl_val *v1, __isl_take isl_val *v2)
563
6
{
564
6
  if (
!v1 || 6
!v26
)
565
0
    goto error;
566
6
567
6
  
if (6
isl_val_is_nan(v1)6
)
{0
568
0
    isl_val_free(v2);
569
0
    return v1;
570
0
  }
571
6
  
if (6
isl_val_is_nan(v2)6
)
{1
572
1
    isl_val_free(v1);
573
1
    return v2;
574
1
  }
575
5
  
if (5
isl_val_le(v1, v2)5
)
{2
576
2
    isl_val_free(v2);
577
2
    return v1;
578
3
  } else {
579
3
    isl_val_free(v1);
580
3
    return v2;
581
3
  }
582
0
error:
583
0
  isl_val_free(v1);
584
0
  isl_val_free(v2);
585
0
  return NULL;
586
5
}
587
588
/* Return the maximum of "v1" and "v2".
589
 */
590
__isl_give isl_val *isl_val_max(__isl_take isl_val *v1, __isl_take isl_val *v2)
591
6
{
592
6
  if (
!v1 || 6
!v26
)
593
0
    goto error;
594
6
595
6
  
if (6
isl_val_is_nan(v1)6
)
{0
596
0
    isl_val_free(v2);
597
0
    return v1;
598
0
  }
599
6
  
if (6
isl_val_is_nan(v2)6
)
{1
600
1
    isl_val_free(v1);
601
1
    return v2;
602
1
  }
603
5
  
if (5
isl_val_ge(v1, v2)5
)
{3
604
3
    isl_val_free(v2);
605
3
    return v1;
606
2
  } else {
607
2
    isl_val_free(v1);
608
2
    return v2;
609
2
  }
610
0
error:
611
0
  isl_val_free(v1);
612
0
  isl_val_free(v2);
613
0
  return NULL;
614
5
}
615
616
/* Return the sum of "v1" and "v2".
617
 */
618
__isl_give isl_val *isl_val_add(__isl_take isl_val *v1, __isl_take isl_val *v2)
619
86
{
620
86
  if (
!v1 || 86
!v286
)
621
0
    goto error;
622
86
  
if (86
isl_val_is_nan(v1)86
)
{0
623
0
    isl_val_free(v2);
624
0
    return v1;
625
0
  }
626
86
  
if (86
isl_val_is_nan(v2)86
)
{0
627
0
    isl_val_free(v1);
628
0
    return v2;
629
0
  }
630
86
  
if (86
(isl_val_is_infty(v1) && 86
isl_val_is_neginfty(v2)3
) ||
631
85
      
(isl_val_is_neginfty(v1) && 85
isl_val_is_infty(v2)0
))
{1
632
1
    isl_val_free(v2);
633
1
    return isl_val_set_nan(v1);
634
1
  }
635
85
  
if (85
isl_val_is_infty(v1) || 85
isl_val_is_neginfty(v1)83
)
{2
636
2
    isl_val_free(v2);
637
2
    return v1;
638
2
  }
639
83
  
if (83
isl_val_is_infty(v2) || 83
isl_val_is_neginfty(v2)82
)
{1
640
1
    isl_val_free(v1);
641
1
    return v2;
642
1
  }
643
82
  
if (82
isl_val_is_zero(v1)82
)
{48
644
48
    isl_val_free(v1);
645
48
    return v2;
646
48
  }
647
34
  
if (34
isl_val_is_zero(v2)34
)
{3
648
3
    isl_val_free(v2);
649
3
    return v1;
650
3
  }
651
34
652
31
  v1 = isl_val_cow(v1);
653
31
  if (!v1)
654
0
    goto error;
655
31
  
if (31
isl_val_is_int(v1) && 31
isl_val_is_int(v2)29
)
656
29
    isl_int_add(v1->n, v1->n, v2->n);
657
2
  else {
658
2
    if (isl_int_eq(v1->d, v2->d))
659
1
      isl_int_add(v1->n, v1->n, v2->n);
660
1
    else {
661
1
      isl_int_mul(v1->n, v1->n, v2->d);
662
1
      isl_int_addmul(v1->n, v2->n, v1->d);
663
1
      isl_int_mul(v1->d, v1->d, v2->d);
664
1
    }
665
2
    v1 = isl_val_normalize(v1);
666
2
  }
667
31
  isl_val_free(v2);
668
31
  return v1;
669
0
error:
670
0
  isl_val_free(v1);
671
0
  isl_val_free(v2);
672
0
  return NULL;
673
31
}
674
675
/* Return the sum of "v1" and "v2".
676
 */
677
__isl_give isl_val *isl_val_add_ui(__isl_take isl_val *v1, unsigned long v2)
678
3.30k
{
679
3.30k
  if (!v1)
680
0
    return NULL;
681
3.30k
  
if (3.30k
!isl_val_is_rat(v1)3.30k
)
682
0
    return v1;
683
3.30k
  
if (3.30k
v2 == 03.30k
)
684
0
    return v1;
685
3.30k
  v1 = isl_val_cow(v1);
686
3.30k
  if (!v1)
687
0
    return NULL;
688
3.30k
689
3.30k
  
isl_int_addmul_ui3.30k
(v1->n, v1->d, v2);3.30k
690
3.30k
691
3.30k
  return v1;
692
3.30k
}
693
694
/* Subtract "v2" from "v1".
695
 */
696
__isl_give isl_val *isl_val_sub(__isl_take isl_val *v1, __isl_take isl_val *v2)
697
50
{
698
50
  if (
!v1 || 50
!v250
)
699
0
    goto error;
700
50
  
if (50
isl_val_is_nan(v1)50
)
{0
701
0
    isl_val_free(v2);
702
0
    return v1;
703
0
  }
704
50
  
if (50
isl_val_is_nan(v2)50
)
{0
705
0
    isl_val_free(v1);
706
0
    return v2;
707
0
  }
708
50
  
if (50
(isl_val_is_infty(v1) && 50
isl_val_is_infty(v2)1
) ||
709
49
      
(isl_val_is_neginfty(v1) && 49
isl_val_is_neginfty(v2)0
))
{1
710
1
    isl_val_free(v2);
711
1
    return isl_val_set_nan(v1);
712
1
  }
713
49
  
if (49
isl_val_is_infty(v1) || 49
isl_val_is_neginfty(v1)49
)
{0
714
0
    isl_val_free(v2);
715
0
    return v1;
716
0
  }
717
49
  
if (49
isl_val_is_infty(v2) || 49
isl_val_is_neginfty(v2)48
)
{1
718
1
    isl_val_free(v1);
719
1
    return isl_val_neg(v2);
720
1
  }
721
48
  
if (48
isl_val_is_zero(v2)48
)
{26
722
26
    isl_val_free(v2);
723
26
    return v1;
724
26
  }
725
22
  
if (22
isl_val_is_zero(v1)22
)
{0
726
0
    isl_val_free(v1);
727
0
    return isl_val_neg(v2);
728
0
  }
729
22
730
22
  v1 = isl_val_cow(v1);
731
22
  if (!v1)
732
0
    goto error;
733
22
  
if (22
isl_val_is_int(v1) && 22
isl_val_is_int(v2)20
)
734
19
    isl_int_sub(v1->n, v1->n, v2->n);
735
3
  else {
736
3
    if (isl_int_eq(v1->d, v2->d))
737
1
      isl_int_sub(v1->n, v1->n, v2->n);
738
2
    else {
739
2
      isl_int_mul(v1->n, v1->n, v2->d);
740
2
      isl_int_submul(v1->n, v2->n, v1->d);
741
2
      isl_int_mul(v1->d, v1->d, v2->d);
742
2
    }
743
3
    v1 = isl_val_normalize(v1);
744
3
  }
745
22
  isl_val_free(v2);
746
22
  return v1;
747
0
error:
748
0
  isl_val_free(v1);
749
0
  isl_val_free(v2);
750
0
  return NULL;
751
22
}
752
753
/* Subtract "v2" from "v1".
754
 */
755
__isl_give isl_val *isl_val_sub_ui(__isl_take isl_val *v1, unsigned long v2)
756
112
{
757
112
  if (!v1)
758
0
    return NULL;
759
112
  
if (112
!isl_val_is_rat(v1)112
)
760
0
    return v1;
761
112
  
if (112
v2 == 0112
)
762
0
    return v1;
763
112
  v1 = isl_val_cow(v1);
764
112
  if (!v1)
765
0
    return NULL;
766
112
767
112
  
isl_int_submul_ui112
(v1->n, v1->d, v2);112
768
112
769
112
  return v1;
770
112
}
771
772
/* Return the product of "v1" and "v2".
773
 */
774
__isl_give isl_val *isl_val_mul(__isl_take isl_val *v1, __isl_take isl_val *v2)
775
17
{
776
17
  if (
!v1 || 17
!v217
)
777
0
    goto error;
778
17
  
if (17
isl_val_is_nan(v1)17
)
{0
779
0
    isl_val_free(v2);
780
0
    return v1;
781
0
  }
782
17
  
if (17
isl_val_is_nan(v2)17
)
{0
783
0
    isl_val_free(v1);
784
0
    return v2;
785
0
  }
786
17
  
if (17
(!isl_val_is_rat(v1) && 17
isl_val_is_zero(v2)6
) ||
787
16
      
(isl_val_is_zero(v1) && 16
!isl_val_is_rat(v2)1
))
{2
788
2
    isl_val_free(v2);
789
2
    return isl_val_set_nan(v1);
790
2
  }
791
15
  
if (15
isl_val_is_zero(v1)15
)
{0
792
0
    isl_val_free(v2);
793
0
    return v1;
794
0
  }
795
15
  
if (15
isl_val_is_zero(v2)15
)
{0
796
0
    isl_val_free(v1);
797
0
    return v2;
798
0
  }
799
15
  
if (15
isl_val_is_infty(v1) || 15
isl_val_is_neginfty(v1)12
)
{5
800
5
    if (isl_val_is_neg(v2))
801
2
      v1 = isl_val_neg(v1);
802
5
    isl_val_free(v2);
803
5
    return v1;
804
5
  }
805
10
  
if (10
isl_val_is_infty(v2) || 10
isl_val_is_neginfty(v2)9
)
{1
806
1
    if (isl_val_is_neg(v1))
807
0
      v2 = isl_val_neg(v2);
808
1
    isl_val_free(v1);
809
1
    return v2;
810
1
  }
811
10
812
9
  v1 = isl_val_cow(v1);
813
9
  if (!v1)
814
0
    goto error;
815
9
  
if (9
isl_val_is_int(v1) && 9
isl_val_is_int(v2)7
)
816
5
    isl_int_mul(v1->n, v1->n, v2->n);
817
4
  else {
818
4
    isl_int_mul(v1->n, v1->n, v2->n);
819
4
    isl_int_mul(v1->d, v1->d, v2->d);
820
4
    v1 = isl_val_normalize(v1);
821
4
  }
822
9
  isl_val_free(v2);
823
9
  return v1;
824
0
error:
825
0
  isl_val_free(v1);
826
0
  isl_val_free(v2);
827
0
  return NULL;
828
9
}
829
830
/* Return the product of "v1" and "v2".
831
 *
832
 * This is a private copy of isl_val_mul for use in the generic
833
 * isl_multi_*_scale_val instantiated for isl_val.
834
 */
835
__isl_give isl_val *isl_val_scale_val(__isl_take isl_val *v1,
836
  __isl_take isl_val *v2)
837
0
{
838
0
  return isl_val_mul(v1, v2);
839
0
}
840
841
/* Return the product of "v1" and "v2".
842
 */
843
__isl_give isl_val *isl_val_mul_ui(__isl_take isl_val *v1, unsigned long v2)
844
0
{
845
0
  if (!v1)
846
0
    return NULL;
847
0
  
if (0
isl_val_is_nan(v1)0
)
848
0
    return v1;
849
0
  
if (0
!isl_val_is_rat(v1)0
)
{0
850
0
    if (v2 == 0)
851
0
      v1 = isl_val_set_nan(v1);
852
0
    return v1;
853
0
  }
854
0
  
if (0
v2 == 10
)
855
0
    return v1;
856
0
  v1 = isl_val_cow(v1);
857
0
  if (!v1)
858
0
    return NULL;
859
0
860
0
  
isl_int_mul_ui0
(v1->n, v1->n, v2);0
861
0
862
0
  return isl_val_normalize(v1);
863
0
}
864
865
/* Divide "v1" by "v2".
866
 */
867
__isl_give isl_val *isl_val_div(__isl_take isl_val *v1, __isl_take isl_val *v2)
868
4.44k
{
869
4.44k
  if (
!v1 || 4.44k
!v24.44k
)
870
0
    goto error;
871
4.44k
  
if (4.44k
isl_val_is_nan(v1)4.44k
)
{4
872
4
    isl_val_free(v2);
873
4
    return v1;
874
4
  }
875
4.44k
  
if (4.44k
isl_val_is_nan(v2)4.44k
)
{5
876
5
    isl_val_free(v1);
877
5
    return v2;
878
5
  }
879
4.43k
  
if (4.43k
isl_val_is_zero(v2) ||4.43k
880
4.43k
      
(!isl_val_is_rat(v1) && 4.43k
!isl_val_is_rat(v2)4
))
{9
881
9
    isl_val_free(v2);
882
9
    return isl_val_set_nan(v1);
883
9
  }
884
4.42k
  
if (4.42k
isl_val_is_zero(v1)4.42k
)
{2.18k
885
2.18k
    isl_val_free(v2);
886
2.18k
    return v1;
887
2.18k
  }
888
2.24k
  
if (2.24k
isl_val_is_infty(v1) || 2.24k
isl_val_is_neginfty(v1)2.24k
)
{0
889
0
    if (isl_val_is_neg(v2))
890
0
      v1 = isl_val_neg(v1);
891
0
    isl_val_free(v2);
892
0
    return v1;
893
0
  }
894
2.24k
  
if (2.24k
isl_val_is_infty(v2) || 2.24k
isl_val_is_neginfty(v2)2.24k
)
{4
895
4
    isl_val_free(v2);
896
4
    return isl_val_set_zero(v1);
897
4
  }
898
2.24k
899
2.24k
  v1 = isl_val_cow(v1);
900
2.24k
  if (!v1)
901
0
    goto error;
902
2.24k
  
if (2.24k
isl_val_is_int(v2)2.24k
)
{2.23k
903
2.23k
    isl_int_mul(v1->d, v1->d, v2->n);
904
2.23k
    v1 = isl_val_normalize(v1);
905
6
  } else {
906
6
    isl_int_mul(v1->d, v1->d, v2->n);
907
6
    isl_int_mul(v1->n, v1->n, v2->d);
908
6
    v1 = isl_val_normalize(v1);
909
6
  }
910
2.24k
  isl_val_free(v2);
911
2.24k
  return v1;
912
0
error:
913
0
  isl_val_free(v1);
914
0
  isl_val_free(v2);
915
0
  return NULL;
916
2.24k
}
917
918
/* Divide "v1" by "v2".
919
 *
920
 * This is a private copy of isl_val_div for use in the generic
921
 * isl_multi_*_scale_down_val instantiated for isl_val.
922
 */
923
__isl_give isl_val *isl_val_scale_down_val(__isl_take isl_val *v1,
924
  __isl_take isl_val *v2)
925
0
{
926
0
  return isl_val_div(v1, v2);
927
0
}
928
929
/* Given two integer values "v1" and "v2", check if "v1" is divisible by "v2".
930
 */
931
isl_bool isl_val_is_divisible_by(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
932
114
{
933
114
  if (
!v1 || 114
!v2114
)
934
0
    return isl_bool_error;
935
114
936
114
  
if (114
!isl_val_is_int(v1) || 114
!isl_val_is_int(v2)114
)
937
0
    isl_die(isl_val_get_ctx(v1), isl_error_invalid,
938
114
      "expecting two integers", return isl_bool_error);
939
114
940
114
  
return 114
isl_int_is_divisible_by114
(v1->n, v2->n);
941
114
}
942
943
/* Given two integer values "v1" and "v2", return the residue of "v1"
944
 * modulo "v2".
945
 */
946
__isl_give isl_val *isl_val_mod(__isl_take isl_val *v1, __isl_take isl_val *v2)
947
19
{
948
19
  if (
!v1 || 19
!v219
)
949
0
    goto error;
950
19
  
if (19
!isl_val_is_int(v1) || 19
!isl_val_is_int(v2)19
)
951
0
    isl_die(isl_val_get_ctx(v1), isl_error_invalid,
952
19
      "expecting two integers", goto error);
953
19
  
if (19
isl_val_is_nonneg(v1) && 19
isl_val_lt(v1, v2)12
)
{10
954
10
    isl_val_free(v2);
955
10
    return v1;
956
10
  }
957
9
  v1 = isl_val_cow(v1);
958
9
  if (!v1)
959
0
    goto error;
960
9
  
isl_int_fdiv_r9
(v1->n, v1->n, v2->n);9
961
9
  isl_val_free(v2);
962
9
  return v1;
963
0
error:
964
0
  isl_val_free(v1);
965
0
  isl_val_free(v2);
966
0
  return NULL;
967
9
}
968
969
/* Given two integer values "v1" and "v2", return the residue of "v1"
970
 * modulo "v2".
971
 *
972
 * This is a private copy of isl_val_mod for use in the generic
973
 * isl_multi_*_mod_multi_val instantiated for isl_val.
974
 */
975
__isl_give isl_val *isl_val_mod_val(__isl_take isl_val *v1,
976
  __isl_take isl_val *v2)
977
0
{
978
0
  return isl_val_mod(v1, v2);
979
0
}
980
981
/* Given two integer values, return their greatest common divisor.
982
 */
983
__isl_give isl_val *isl_val_gcd(__isl_take isl_val *v1, __isl_take isl_val *v2)
984
2.24k
{
985
2.24k
  if (
!v1 || 2.24k
!v22.24k
)
986
0
    goto error;
987
2.24k
  
if (2.24k
!isl_val_is_int(v1) || 2.24k
!isl_val_is_int(v2)2.24k
)
988
0
    isl_die(isl_val_get_ctx(v1), isl_error_invalid,
989
2.24k
      "expecting two integers", goto error);
990
2.24k
  
if (2.24k
isl_val_eq(v1, v2)2.24k
)
{36
991
36
    isl_val_free(v2);
992
36
    return v1;
993
36
  }
994
2.20k
  
if (2.20k
isl_val_is_one(v1)2.20k
)
{0
995
0
    isl_val_free(v2);
996
0
    return v1;
997
0
  }
998
2.20k
  
if (2.20k
isl_val_is_one(v2)2.20k
)
{2.17k
999
2.17k
    isl_val_free(v1);
1000
2.17k
    return v2;
1001
2.17k
  }
1002
30
  v1 = isl_val_cow(v1);
1003
30
  if (!v1)
1004
0
    goto error;
1005
30
  
isl_int_gcd30
(v1->n, v1->n, v2->n);30
1006
30
  isl_val_free(v2);
1007
30
  return v1;
1008
0
error:
1009
0
  isl_val_free(v1);
1010
0
  isl_val_free(v2);
1011
0
  return NULL;
1012
30
}
1013
1014
/* Compute x, y and g such that g = gcd(a,b) and a*x+b*y = g.
1015
 */
1016
static void isl_int_gcdext(isl_int *g, isl_int *x, isl_int *y,
1017
  isl_int a, isl_int b)
1018
14
{
1019
14
  isl_int d, tmp;
1020
14
  isl_int a_copy, b_copy;
1021
14
1022
14
  isl_int_init(a_copy);
1023
14
  isl_int_init(b_copy);
1024
14
  isl_int_init(d);
1025
14
  isl_int_init(tmp);
1026
14
  isl_int_set(a_copy, a);
1027
14
  isl_int_set(b_copy, b);
1028
14
  isl_int_abs(*g, a_copy);
1029
14
  isl_int_abs(d, b_copy);
1030
14
  isl_int_set_si(*x, 1);
1031
14
  isl_int_set_si(*y, 0);
1032
42
  while (
isl_int_is_pos42
(d))
{28
1033
28
    isl_int_fdiv_q(tmp, *g, d);
1034
28
    isl_int_submul(*x, tmp, *y);
1035
28
    isl_int_submul(*g, tmp, d);
1036
28
    isl_int_swap(*g, d);
1037
28
    isl_int_swap(*x, *y);
1038
28
  }
1039
14
  if (isl_int_is_zero(a_copy))
1040
0
    isl_int_set_si(*x, 0);
1041
14
  else 
if (14
isl_int_is_neg14
(a_copy))
1042
14
    isl_int_neg(*x, *x);
1043
14
  if (isl_int_is_zero(b_copy))
1044
0
    isl_int_set_si(*y, 0);
1045
14
  else {
1046
14
    isl_int_mul(tmp, a_copy, *x);
1047
14
    isl_int_sub(tmp, *g, tmp);
1048
14
    isl_int_divexact(*y, tmp, b_copy);
1049
14
  }
1050
14
  isl_int_clear(d);
1051
14
  isl_int_clear(tmp);
1052
14
  isl_int_clear(a_copy);
1053
14
  isl_int_clear(b_copy);
1054
14
}
1055
1056
/* Given two integer values v1 and v2, return their greatest common divisor g,
1057
 * as well as two integers x and y such that x * v1 + y * v2 = g.
1058
 */
1059
__isl_give isl_val *isl_val_gcdext(__isl_take isl_val *v1,
1060
  __isl_take isl_val *v2, __isl_give isl_val **x, __isl_give isl_val **y)
1061
14
{
1062
14
  isl_ctx *ctx;
1063
14
  isl_val *a = NULL, *b = NULL;
1064
14
1065
14
  if (
!x && 14
!y0
)
1066
0
    return isl_val_gcd(v1, v2);
1067
14
1068
14
  
if (14
!v1 || 14
!v214
)
1069
0
    goto error;
1070
14
1071
14
  ctx = isl_val_get_ctx(v1);
1072
14
  if (
!isl_val_is_int(v1) || 14
!isl_val_is_int(v2)14
)
1073
0
    isl_die(ctx, isl_error_invalid,
1074
14
      "expecting two integers", goto error);
1075
14
1076
14
  v1 = isl_val_cow(v1);
1077
14
  a = isl_val_alloc(ctx);
1078
14
  b = isl_val_alloc(ctx);
1079
14
  if (
!v1 || 14
!a14
||
!b14
)
1080
0
    goto error;
1081
14
  isl_int_gcdext(&v1->n, &a->n, &b->n, v1->n, v2->n);
1082
14
  if (
x14
)
{14
1083
14
    isl_int_set_si(a->d, 1);
1084
14
    *x = a;
1085
14
  } else
1086
0
    isl_val_free(a);
1087
14
  if (
y14
)
{14
1088
14
    isl_int_set_si(b->d, 1);
1089
14
    *y = b;
1090
14
  } else
1091
0
    isl_val_free(b);
1092
14
  isl_val_free(v2);
1093
14
  return v1;
1094
0
error:
1095
0
  isl_val_free(v1);
1096
0
  isl_val_free(v2);
1097
0
  isl_val_free(a);
1098
0
  isl_val_free(b);
1099
0
  if (x)
1100
0
    *x = NULL;
1101
0
  if (y)
1102
0
    *y = NULL;
1103
0
  return NULL;
1104
14
}
1105
1106
/* Does "v" represent an integer value?
1107
 */
1108
isl_bool isl_val_is_int(__isl_keep isl_val *v)
1109
63.3k
{
1110
63.3k
  if (!v)
1111
0
    return isl_bool_error;
1112
63.3k
1113
63.3k
  
return 63.3k
isl_int_is_one63.3k
(v->d);
1114
63.3k
}
1115
1116
/* Does "v" represent a rational value?
1117
 */
1118
isl_bool isl_val_is_rat(__isl_keep isl_val *v)
1119
34.8k
{
1120
34.8k
  if (!v)
1121
0
    return isl_bool_error;
1122
34.8k
1123
34.8k
  
return !34.8k
isl_int_is_zero34.8k
(v->d);
1124
34.8k
}
1125
1126
/* Does "v" represent NaN?
1127
 */
1128
isl_bool isl_val_is_nan(__isl_keep isl_val *v)
1129
19.8k
{
1130
19.8k
  if (!v)
1131
0
    return isl_bool_error;
1132
19.8k
1133
19.8k
  
return 19.8k
isl_int_is_zero19.8k
(v->n) &&
isl_int_is_zero6.25k
(v->d);
1134
19.8k
}
1135
1136
/* Does "v" represent +infinity?
1137
 */
1138
isl_bool isl_val_is_infty(__isl_keep isl_val *v)
1139
5.70k
{
1140
5.70k
  if (!v)
1141
0
    return isl_bool_error;
1142
5.70k
1143
5.70k
  
return 5.70k
isl_int_is_pos5.70k
(v->n) &&
isl_int_is_zero5.41k
(v->d);
1144
5.70k
}
1145
1146
/* Does "v" represent -infinity?
1147
 */
1148
isl_bool isl_val_is_neginfty(__isl_keep isl_val *v)
1149
4.92k
{
1150
4.92k
  if (!v)
1151
0
    return isl_bool_error;
1152
4.92k
1153
4.92k
  
return 4.92k
isl_int_is_neg4.92k
(v->n) &&
isl_int_is_zero94
(v->d);
1154
4.92k
}
1155
1156
/* Does "v" represent the integer zero?
1157
 */
1158
isl_bool isl_val_is_zero(__isl_keep isl_val *v)
1159
60.9k
{
1160
60.9k
  if (!v)
1161
0
    return isl_bool_error;
1162
60.9k
1163
60.9k
  
return 60.9k
isl_int_is_zero60.9k
(v->n) &&
!40.5k
isl_int_is_zero40.5k
(v->d);
1164
60.9k
}
1165
1166
/* Does "v" represent the integer one?
1167
 */
1168
isl_bool isl_val_is_one(__isl_keep isl_val *v)
1169
24.2k
{
1170
24.2k
  if (!v)
1171
0
    return isl_bool_error;
1172
24.2k
1173
24.2k
  
return 24.2k
isl_int_eq24.2k
(v->n, v->d);
1174
24.2k
}
1175
1176
/* Does "v" represent the integer negative one?
1177
 */
1178
isl_bool isl_val_is_negone(__isl_keep isl_val *v)
1179
2.59k
{
1180
2.59k
  if (!v)
1181
0
    return isl_bool_error;
1182
2.59k
1183
2.59k
  
return 2.59k
isl_int_is_neg2.59k
(v->n) &&
isl_int_abs_eq213
(v->n, v->d);
1184
2.59k
}
1185
1186
/* Is "v" (strictly) positive?
1187
 */
1188
isl_bool isl_val_is_pos(__isl_keep isl_val *v)
1189
1.47k
{
1190
1.47k
  if (!v)
1191
0
    return isl_bool_error;
1192
1.47k
1193
1.47k
  
return 1.47k
isl_int_is_pos1.47k
(v->n);
1194
1.47k
}
1195
1196
/* Is "v" (strictly) negative?
1197
 */
1198
isl_bool isl_val_is_neg(__isl_keep isl_val *v)
1199
15.5k
{
1200
15.5k
  if (!v)
1201
0
    return isl_bool_error;
1202
15.5k
1203
15.5k
  
return 15.5k
isl_int_is_neg15.5k
(v->n);
1204
15.5k
}
1205
1206
/* Is "v" non-negative?
1207
 */
1208
isl_bool isl_val_is_nonneg(__isl_keep isl_val *v)
1209
665
{
1210
665
  if (!v)
1211
0
    return isl_bool_error;
1212
665
1213
665
  
if (665
isl_val_is_nan(v)665
)
1214
0
    return isl_bool_false;
1215
665
1216
665
  
return 665
isl_int_is_nonneg665
(v->n);
1217
665
}
1218
1219
/* Is "v" non-positive?
1220
 */
1221
isl_bool isl_val_is_nonpos(__isl_keep isl_val *v)
1222
0
{
1223
0
  if (!v)
1224
0
    return isl_bool_error;
1225
0
1226
0
  
if (0
isl_val_is_nan(v)0
)
1227
0
    return isl_bool_false;
1228
0
1229
0
  
return 0
isl_int_is_nonpos0
(v->n);
1230
0
}
1231
1232
/* Return the sign of "v".
1233
 *
1234
 * The sign of NaN is undefined.
1235
 */
1236
int isl_val_sgn(__isl_keep isl_val *v)
1237
2.10k
{
1238
2.10k
  if (!v)
1239
0
    return 0;
1240
2.10k
  
if (2.10k
isl_val_is_zero(v)2.10k
)
1241
952
    return 0;
1242
1.15k
  
if (1.15k
isl_val_is_pos(v)1.15k
)
1243
680
    return 1;
1244
475
  return -1;
1245
1.15k
}
1246
1247
/* Is "v1" (strictly) less than "v2"?
1248
 */
1249
isl_bool isl_val_lt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1250
16
{
1251
16
  isl_int t;
1252
16
  isl_bool lt;
1253
16
1254
16
  if (
!v1 || 16
!v216
)
1255
0
    return isl_bool_error;
1256
16
  
if (16
isl_val_is_int(v1) && 16
isl_val_is_int(v2)16
)
1257
16
    
return 16
isl_int_lt16
(v1->n, v2->n);
1258
0
  
if (0
isl_val_is_nan(v1) || 0
isl_val_is_nan(v2)0
)
1259
0
    return isl_bool_false;
1260
0
  
if (0
isl_val_eq(v1, v2)0
)
1261
0
    return isl_bool_false;
1262
0
  
if (0
isl_val_is_infty(v2)0
)
1263
0
    return isl_bool_true;
1264
0
  
if (0
isl_val_is_infty(v1)0
)
1265
0
    return isl_bool_false;
1266
0
  
if (0
isl_val_is_neginfty(v1)0
)
1267
0
    return isl_bool_true;
1268
0
  
if (0
isl_val_is_neginfty(v2)0
)
1269
0
    return isl_bool_false;
1270
0
1271
0
  
isl_int_init0
(t);0
1272
0
  isl_int_mul(t, v1->n, v2->d);
1273
0
  isl_int_submul(t, v2->n, v1->d);
1274
0
  lt = isl_int_is_neg(t);
1275
0
  isl_int_clear(t);
1276
0
1277
0
  return lt;
1278
0
}
1279
1280
/* Is "v1" (strictly) greater than "v2"?
1281
 */
1282
isl_bool isl_val_gt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1283
0
{
1284
0
  return isl_val_lt(v2, v1);
1285
0
}
1286
1287
/* Is "v1" less than or equal to "v2"?
1288
 */
1289
isl_bool isl_val_le(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1290
10
{
1291
10
  isl_int t;
1292
10
  isl_bool le;
1293
10
1294
10
  if (
!v1 || 10
!v210
)
1295
0
    return isl_bool_error;
1296
10
  
if (10
isl_val_is_int(v1) && 10
isl_val_is_int(v2)6
)
1297
4
    
return 4
isl_int_le4
(v1->n, v2->n);
1298
6
  
if (6
isl_val_is_nan(v1) || 6
isl_val_is_nan(v2)6
)
1299
0
    return isl_bool_false;
1300
6
  
if (6
isl_val_eq(v1, v2)6
)
1301
0
    return isl_bool_true;
1302
6
  
if (6
isl_val_is_infty(v2)6
)
1303
2
    return isl_bool_true;
1304
4
  
if (4
isl_val_is_infty(v1)4
)
1305
2
    return isl_bool_false;
1306
2
  
if (2
isl_val_is_neginfty(v1)2
)
1307
1
    return isl_bool_true;
1308
1
  
if (1
isl_val_is_neginfty(v2)1
)
1309
1
    return isl_bool_false;
1310
1
1311
0
  
isl_int_init0
(t);0
1312
0
  isl_int_mul(t, v1->n, v2->d);
1313
0
  isl_int_submul(t, v2->n, v1->d);
1314
0
  le = isl_int_is_nonpos(t);
1315
0
  isl_int_clear(t);
1316
0
1317
0
  return le;
1318
1
}
1319
1320
/* Is "v1" greater than or equal to "v2"?
1321
 */
1322
isl_bool isl_val_ge(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1323
5
{
1324
5
  return isl_val_le(v2, v1);
1325
5
}
1326
1327
/* How does "v" compare to "i"?
1328
 *
1329
 * Return 1 if v is greater, -1 if v is smaller and 0 if v is equal to i.
1330
 *
1331
 * If v is NaN (or NULL), then the result is undefined.
1332
 */
1333
int isl_val_cmp_si(__isl_keep isl_val *v, long i)
1334
838
{
1335
838
  isl_int t;
1336
838
  int cmp;
1337
838
1338
838
  if (!v)
1339
0
    return 0;
1340
838
  
if (838
isl_val_is_int(v)838
)
1341
838
    
return 838
isl_int_cmp_si838
(v->n, i);
1342
0
  
if (0
isl_val_is_nan(v)0
)
1343
0
    return 0;
1344
0
  
if (0
isl_val_is_infty(v)0
)
1345
0
    return 1;
1346
0
  
if (0
isl_val_is_neginfty(v)0
)
1347
0
    return -1;
1348
0
1349
0
  
isl_int_init0
(t);0
1350
0
  isl_int_mul_si(t, v->d, i);
1351
0
  isl_int_sub(t, v->n, t);
1352
0
  cmp = isl_int_sgn(t);
1353
0
  isl_int_clear(t);
1354
0
1355
0
  return cmp;
1356
0
}
1357
1358
/* Is "v1" equal to "v2"?
1359
 */
1360
isl_bool isl_val_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1361
2.35k
{
1362
2.35k
  if (
!v1 || 2.35k
!v22.35k
)
1363
0
    return isl_bool_error;
1364
2.35k
  
if (2.35k
isl_val_is_nan(v1) || 2.35k
isl_val_is_nan(v2)2.35k
)
1365
0
    return isl_bool_false;
1366
2.35k
1367
2.35k
  
return 2.35k
isl_int_eq2.35k
(v1->n, v2->n) &&
isl_int_eq135
(v1->d, v2->d);
1368
2.35k
}
1369
1370
/* Is "v1" equal to "v2" in absolute value?
1371
 */
1372
isl_bool isl_val_abs_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1373
12
{
1374
12
  if (
!v1 || 12
!v212
)
1375
0
    return isl_bool_error;
1376
12
  
if (12
isl_val_is_nan(v1) || 12
isl_val_is_nan(v2)12
)
1377
0
    return isl_bool_false;
1378
12
1379
12
  
return 12
isl_int_abs_eq12
(v1->n, v2->n) &&
isl_int_eq12
(v1->d, v2->d);
1380
12
}
1381
1382
/* Is "v1" different from "v2"?
1383
 */
1384
isl_bool isl_val_ne(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1385
3
{
1386
3
  if (
!v1 || 3
!v23
)
1387
0
    return isl_bool_error;
1388
3
  
if (3
isl_val_is_nan(v1) || 3
isl_val_is_nan(v2)3
)
1389
0
    return isl_bool_false;
1390
3
1391
3
  
return 3
isl_int_ne3
(v1->n, v2->n) ||
isl_int_ne0
(v1->d, v2->d);
1392
3
}
1393
1394
/* Print a textual representation of "v" onto "p".
1395
 */
1396
__isl_give isl_printer *isl_printer_print_val(__isl_take isl_printer *p,
1397
  __isl_keep isl_val *v)
1398
5.13k
{
1399
5.13k
  int neg;
1400
5.13k
1401
5.13k
  if (
!p || 5.13k
!v5.13k
)
1402
0
    return isl_printer_free(p);
1403
5.13k
1404
5.13k
  
neg = 5.13k
isl_int_is_neg5.13k
(v->n);
1405
5.13k
  if (
neg5.13k
)
{47
1406
47
    p = isl_printer_print_str(p, "-");
1407
47
    isl_int_neg(v->n, v->n);
1408
47
  }
1409
5.13k
  if (
isl_int_is_zero5.13k
(v->d))
{0
1410
0
    int sgn = isl_int_sgn(v->n);
1411
0
    p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1412
0
              
sgn == 0 ? 0
"NaN"0
:
"infty"0
);
1413
0
  } else
1414
5.13k
    p = isl_printer_print_isl_int(p, v->n);
1415
5.13k
  if (neg)
1416
47
    isl_int_neg(v->n, v->n);
1417
5.13k
  if (
!5.13k
isl_int_is_zero5.13k
(v->d) &&
!5.13k
isl_int_is_one5.13k
(v->d))
{0
1418
0
    p = isl_printer_print_str(p, "/");
1419
0
    p = isl_printer_print_isl_int(p, v->d);
1420
0
  }
1421
5.13k
1422
5.13k
  return p;
1423
5.13k
}
1424
1425
/* Is "val1" (obviously) equal to "val2"?
1426
 *
1427
 * This is a private copy of isl_val_eq for use in the generic
1428
 * isl_multi_*_plain_is_equal instantiated for isl_val.
1429
 */
1430
int isl_val_plain_is_equal(__isl_keep isl_val *val1, __isl_keep isl_val *val2)
1431
0
{
1432
0
  return isl_val_eq(val1, val2);
1433
0
}
1434
1435
/* Does "v" have any non-zero coefficients
1436
 * for any dimension in the given range?
1437
 *
1438
 * This function is only meant to be used in the generic isl_multi_*
1439
 * functions which have to deal with base objects that have an associated
1440
 * space.  Since an isl_val does not have any coefficients, this function
1441
 * always return 0.
1442
 */
1443
int isl_val_involves_dims(__isl_keep isl_val *v, enum isl_dim_type type,
1444
  unsigned first, unsigned n)
1445
0
{
1446
0
  if (!v)
1447
0
    return -1;
1448
0
1449
0
  return 0;
1450
0
}
1451
1452
/* Insert "n" dimensions of type "type" at position "first".
1453
 *
1454
 * This function is only meant to be used in the generic isl_multi_*
1455
 * functions which have to deal with base objects that have an associated
1456
 * space.  Since an isl_val does not have an associated space, this function
1457
 * does not do anything.
1458
 */
1459
__isl_give isl_val *isl_val_insert_dims(__isl_take isl_val *v,
1460
  enum isl_dim_type type, unsigned first, unsigned n)
1461
0
{
1462
0
  return v;
1463
0
}
1464
1465
/* Drop the the "n" first dimensions of type "type" at position "first".
1466
 *
1467
 * This function is only meant to be used in the generic isl_multi_*
1468
 * functions which have to deal with base objects that have an associated
1469
 * space.  Since an isl_val does not have an associated space, this function
1470
 * does not do anything.
1471
 */
1472
__isl_give isl_val *isl_val_drop_dims(__isl_take isl_val *v,
1473
  enum isl_dim_type type, unsigned first, unsigned n)
1474
0
{
1475
0
  return v;
1476
0
}
1477
1478
/* Change the name of the dimension of type "type" at position "pos" to "s".
1479
 *
1480
 * This function is only meant to be used in the generic isl_multi_*
1481
 * functions which have to deal with base objects that have an associated
1482
 * space.  Since an isl_val does not have an associated space, this function
1483
 * does not do anything.
1484
 */
1485
__isl_give isl_val *isl_val_set_dim_name(__isl_take isl_val *v,
1486
  enum isl_dim_type type, unsigned pos, const char *s)
1487
0
{
1488
0
  return v;
1489
0
}
1490
1491
/* Return the space of "v".
1492
 *
1493
 * This function is only meant to be used in the generic isl_multi_*
1494
 * functions which have to deal with base objects that have an associated
1495
 * space.  The conditions surrounding the call to this function make sure
1496
 * that this function will never actually get called.  We return a valid
1497
 * space anyway, just in case.
1498
 */
1499
__isl_give isl_space *isl_val_get_space(__isl_keep isl_val *v)
1500
0
{
1501
0
  if (!v)
1502
0
    return NULL;
1503
0
1504
0
  return isl_space_params_alloc(isl_val_get_ctx(v), 0);
1505
0
}
1506
1507
/* Reset the domain space of "v" to "space".
1508
 *
1509
 * This function is only meant to be used in the generic isl_multi_*
1510
 * functions which have to deal with base objects that have an associated
1511
 * space.  Since an isl_val does not have an associated space, this function
1512
 * does not do anything, apart from error handling and cleaning up memory.
1513
 */
1514
__isl_give isl_val *isl_val_reset_domain_space(__isl_take isl_val *v,
1515
  __isl_take isl_space *space)
1516
0
{
1517
0
  if (!space)
1518
0
    return isl_val_free(v);
1519
0
  isl_space_free(space);
1520
0
  return v;
1521
0
}
1522
1523
/* Align the parameters of "v" to those of "space".
1524
 *
1525
 * This function is only meant to be used in the generic isl_multi_*
1526
 * functions which have to deal with base objects that have an associated
1527
 * space.  Since an isl_val does not have an associated space, this function
1528
 * does not do anything, apart from error handling and cleaning up memory.
1529
 * Note that the conditions surrounding the call to this function make sure
1530
 * that this function will never actually get called.
1531
 */
1532
__isl_give isl_val *isl_val_align_params(__isl_take isl_val *v,
1533
  __isl_take isl_space *space)
1534
0
{
1535
0
  if (!space)
1536
0
    return isl_val_free(v);
1537
0
  isl_space_free(space);
1538
0
  return v;
1539
0
}
1540
1541
/* Reorder the dimensions of the domain of "v" according
1542
 * to the given reordering.
1543
 *
1544
 * This function is only meant to be used in the generic isl_multi_*
1545
 * functions which have to deal with base objects that have an associated
1546
 * space.  Since an isl_val does not have an associated space, this function
1547
 * does not do anything, apart from error handling and cleaning up memory.
1548
 */
1549
__isl_give isl_val *isl_val_realign_domain(__isl_take isl_val *v,
1550
  __isl_take isl_reordering *r)
1551
0
{
1552
0
  if (!r)
1553
0
    return isl_val_free(v);
1554
0
  isl_reordering_free(r);
1555
0
  return v;
1556
0
}
1557
1558
/* Return an isl_val that is zero on "ls".
1559
 *
1560
 * This function is only meant to be used in the generic isl_multi_*
1561
 * functions which have to deal with base objects that have an associated
1562
 * space.  Since an isl_val does not have an associated space, this function
1563
 * simply returns a zero isl_val in the same context as "ls".
1564
 */
1565
__isl_give isl_val *isl_val_zero_on_domain(__isl_take isl_local_space *ls)
1566
1.96k
{
1567
1.96k
  isl_ctx *ctx;
1568
1.96k
1569
1.96k
  if (!ls)
1570
0
    return NULL;
1571
1.96k
  ctx = isl_local_space_get_ctx(ls);
1572
1.96k
  isl_local_space_free(ls);
1573
1.96k
  return isl_val_zero(ctx);
1574
1.96k
}
1575
1576
/* Do the parameters of "v" match those of "space"?
1577
 *
1578
 * This function is only meant to be used in the generic isl_multi_*
1579
 * functions which have to deal with base objects that have an associated
1580
 * space.  Since an isl_val does not have an associated space, this function
1581
 * simply returns true, except if "v" or "space" are NULL.
1582
 */
1583
isl_bool isl_val_matching_params(__isl_keep isl_val *v,
1584
  __isl_keep isl_space *space)
1585
6.77k
{
1586
6.77k
  if (
!v || 6.77k
!space6.77k
)
1587
0
    return isl_bool_error;
1588
6.77k
  return isl_bool_true;
1589
6.77k
}
1590
1591
/* Check that the domain space of "v" matches "space".
1592
 *
1593
 * This function is only meant to be used in the generic isl_multi_*
1594
 * functions which have to deal with base objects that have an associated
1595
 * space.  Since an isl_val does not have an associated space, this function
1596
 * simply returns 0, except if "v" or "space" are NULL.
1597
 */
1598
isl_stat isl_val_check_match_domain_space(__isl_keep isl_val *v,
1599
  __isl_keep isl_space *space)
1600
6.77k
{
1601
6.77k
  if (
!v || 6.77k
!space6.77k
)
1602
0
    return isl_stat_error;
1603
6.77k
  return isl_stat_ok;
1604
6.77k
}
1605
1606
0
#define isl_val_involves_nan isl_val_is_nan
1607
1608
#undef BASE
1609
#define BASE val
1610
1611
#define NO_DOMAIN
1612
#define NO_IDENTITY
1613
#define NO_FROM_BASE
1614
#define NO_MOVE_DIMS
1615
#include <isl_multi_templ.c>
1616
1617
/* Apply "fn" to each of the elements of "mv" with as second argument "v".
1618
 */
1619
static __isl_give isl_multi_val *isl_multi_val_fn_val(
1620
  __isl_take isl_multi_val *mv,
1621
  __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
1622
          __isl_take isl_val *v2),
1623
  __isl_take isl_val *v)
1624
6
{
1625
6
  int i;
1626
6
1627
6
  mv = isl_multi_val_cow(mv);
1628
6
  if (
!mv || 6
!v6
)
1629
0
    goto error;
1630
6
1631
24
  
for (i = 0; 6
i < mv->n24
;
++i18
)
{18
1632
18
    mv->p[i] = fn(mv->p[i], isl_val_copy(v));
1633
18
    if (!mv->p[i])
1634
0
      goto error;
1635
18
  }
1636
6
1637
6
  isl_val_free(v);
1638
6
  return mv;
1639
0
error:
1640
0
  isl_val_free(v);
1641
0
  isl_multi_val_free(mv);
1642
0
  return NULL;
1643
6
}
1644
1645
/* Add "v" to each of the elements of "mv".
1646
 */
1647
__isl_give isl_multi_val *isl_multi_val_add_val(__isl_take isl_multi_val *mv,
1648
  __isl_take isl_val *v)
1649
3
{
1650
3
  if (!v)
1651
0
    return isl_multi_val_free(mv);
1652
3
  
if (3
isl_val_is_zero(v)3
)
{0
1653
0
    isl_val_free(v);
1654
0
    return mv;
1655
0
  }
1656
3
  return isl_multi_val_fn_val(mv, &isl_val_add, v);
1657
3
}
1658
1659
/* Reduce the elements of "mv" modulo "v".
1660
 */
1661
__isl_give isl_multi_val *isl_multi_val_mod_val(__isl_take isl_multi_val *mv,
1662
  __isl_take isl_val *v)
1663
3
{
1664
3
  return isl_multi_val_fn_val(mv, &isl_val_mod, v);
1665
3
}