Coverage Report

Created: 2017-06-23 12:40

/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
106k
{
23
106k
  isl_val *v;
24
106k
25
106k
  v = isl_alloc_type(ctx, struct isl_val);
26
106k
  if (!v)
27
0
    return NULL;
28
106k
29
106k
  v->ctx = ctx;
30
106k
  isl_ctx_ref(ctx);
31
106k
  v->ref = 1;
32
106k
  isl_int_init(v->n);
33
106k
  isl_int_init(v->d);
34
106k
35
106k
  return v;
36
106k
}
37
38
/* Return a reference to an isl_val representing zero.
39
 */
40
__isl_give isl_val *isl_val_zero(isl_ctx *ctx)
41
5.39k
{
42
5.39k
  return isl_val_int_from_si(ctx, 0);
43
5.39k
}
44
45
/* Return a reference to an isl_val representing one.
46
 */
47
__isl_give isl_val *isl_val_one(isl_ctx *ctx)
48
87
{
49
87
  return isl_val_int_from_si(ctx, 1);
50
87
}
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
939
{
63
939
  isl_val *v;
64
939
65
939
  v = isl_val_alloc(ctx);
66
939
  if (!v)
67
0
    return NULL;
68
939
69
939
  
isl_int_set_si939
(v->n, 0);939
70
939
  isl_int_set_si(v->d, 0);
71
939
72
939
  return v;
73
939
}
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
243
{
97
243
  isl_val *v;
98
243
99
243
  v = isl_val_alloc(ctx);
100
243
  if (!v)
101
0
    return NULL;
102
243
103
243
  
isl_int_set_si243
(v->n, 1);243
104
243
  isl_int_set_si(v->d, 0);
105
243
106
243
  return v;
107
243
}
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
20.7k
{
129
20.7k
  isl_val *v;
130
20.7k
131
20.7k
  v = isl_val_alloc(ctx);
132
20.7k
  if (!v)
133
0
    return NULL;
134
20.7k
135
20.7k
  
isl_int_set_si20.7k
(v->n, i);20.7k
136
20.7k
  isl_int_set_si(v->d, 1);
137
20.7k
138
20.7k
  return v;
139
20.7k
}
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.85k
{
170
5.85k
  isl_val *v;
171
5.85k
172
5.85k
  v = isl_val_alloc(ctx);
173
5.85k
  if (!v)
174
0
    return NULL;
175
5.85k
176
5.85k
  
isl_int_set_ui5.85k
(v->n, u);5.85k
177
5.85k
  isl_int_set_si(v->d, 1);
178
5.85k
179
5.85k
  return v;
180
5.85k
}
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
17.6k
{
186
17.6k
  isl_val *v;
187
17.6k
188
17.6k
  v = isl_val_alloc(ctx);
189
17.6k
  if (!v)
190
0
    return NULL;
191
17.6k
192
17.6k
  
isl_int_set17.6k
(v->n, n);17.6k
193
17.6k
  isl_int_set_si(v->d, 1);
194
17.6k
195
17.6k
  return v;
196
17.6k
}
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.8k
{
204
33.8k
  isl_val *v;
205
33.8k
206
33.8k
  v = isl_val_alloc(ctx);
207
33.8k
  if (!v)
208
0
    return NULL;
209
33.8k
210
33.8k
  
isl_int_set33.8k
(v->n, n);33.8k
211
33.8k
  isl_int_set(v->d, d);
212
33.8k
213
33.8k
  return v;
214
33.8k
}
215
216
/* Return a new reference to "v".
217
 */
218
__isl_give isl_val *isl_val_copy(__isl_keep isl_val *v)
219
28.9k
{
220
28.9k
  if (!v)
221
0
    return NULL;
222
28.9k
223
28.9k
  v->ref++;
224
28.9k
  return v;
225
28.9k
}
226
227
/* Return a fresh copy of "val".
228
 */
229
__isl_give isl_val *isl_val_dup(__isl_keep isl_val *val)
230
6.76k
{
231
6.76k
  isl_val *dup;
232
6.76k
233
6.76k
  if (!val)
234
0
    return NULL;
235
6.76k
236
6.76k
  dup = isl_val_alloc(isl_val_get_ctx(val));
237
6.76k
  if (!dup)
238
0
    return NULL;
239
6.76k
240
6.76k
  
isl_int_set6.76k
(dup->n, val->n);6.76k
241
6.76k
  isl_int_set(dup->d, val->d);
242
6.76k
243
6.76k
  return dup;
244
6.76k
}
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
15.7k
{
251
15.7k
  if (!val)
252
0
    return NULL;
253
15.7k
254
15.7k
  
if (15.7k
val->ref == 115.7k
)
255
8.94k
    return val;
256
6.76k
  val->ref--;
257
6.76k
  return isl_val_dup(val);
258
15.7k
}
259
260
/* Free "v" and return NULL.
261
 */
262
__isl_null isl_val *isl_val_free(__isl_take isl_val *v)
263
131k
{
264
131k
  if (!v)
265
3.33k
    return NULL;
266
131k
267
128k
  
if (128k
--v->ref > 0128k
)
268
22.1k
    return NULL;
269
128k
270
106k
  isl_ctx_deref(v->ctx);
271
106k
  isl_int_clear(v->n);
272
106k
  isl_int_clear(v->d);
273
106k
  free(v);
274
106k
  return NULL;
275
128k
}
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
12.2k
{
356
12.2k
  return val ? val->ctx : NULL;
357
12.2k
}
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
37.0k
{
386
37.0k
  isl_ctx *ctx;
387
37.0k
388
37.0k
  if (!v)
389
0
    return NULL;
390
37.0k
  
if (37.0k
isl_val_is_int(v)37.0k
)
391
36.9k
    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.94k
{
411
2.94k
  if (!v)
412
0
    return NULL;
413
2.94k
  
if (2.94k
isl_val_is_nan(v)2.94k
)
414
1
    return v;
415
2.94k
  
if (2.94k
isl_val_is_zero(v)2.94k
)
416
179
    return v;
417
2.94k
418
2.76k
  v = isl_val_cow(v);
419
2.76k
  if (!v)
420
0
    return NULL;
421
2.76k
  
isl_int_neg2.76k
(v->n, v->n);2.76k
422
2.76k
423
2.76k
  return v;
424
2.76k
}
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
698
{
457
698
  if (!v)
458
0
    return NULL;
459
698
  
if (698
isl_val_is_nan(v)698
)
460
1
    return v;
461
697
  
if (697
isl_val_is_nonneg(v)697
)
462
427
    return v;
463
270
  return isl_val_neg(v);
464
697
}
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.86k
{
533
5.86k
  unsigned long exp;
534
5.86k
  int neg;
535
5.86k
536
5.86k
  v = isl_val_cow(v);
537
5.86k
  if (!v)
538
0
    return NULL;
539
5.86k
  
if (5.86k
!isl_val_is_int(v)5.86k
)
540
0
    isl_die(isl_val_get_ctx(v), isl_error_invalid,
541
5.86k
      "can only compute integer powers",
542
5.86k
      return isl_val_free(v));
543
5.86k
  neg = isl_val_is_neg(v);
544
5.86k
  if (neg)
545
2
    isl_int_neg(v->n, v->n);
546
5.86k
  if (
!5.86k
isl_int_fits_ulong5.86k
(v->n))
547
0
    isl_die(isl_val_get_ctx(v), isl_error_invalid,
548
5.86k
      "exponent too large", return isl_val_free(v));
549
5.86k
  
exp = 5.86k
isl_int_get_ui5.86k
(v->n);
550
5.86k
  if (
neg5.86k
)
{2
551
2
    isl_int_mul_2exp(v->d, v->d, exp);
552
2
    isl_int_set_si(v->n, 1);
553
5.86k
  } else {
554
5.86k
    isl_int_mul_2exp(v->n, v->d, exp);
555
5.86k
  }
556
5.86k
557
5.86k
  return v;
558
5.86k
}
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
80
{
620
80
  if (
!v1 || 80
!v280
)
621
0
    goto error;
622
80
  
if (80
isl_val_is_nan(v1)80
)
{0
623
0
    isl_val_free(v2);
624
0
    return v1;
625
0
  }
626
80
  
if (80
isl_val_is_nan(v2)80
)
{0
627
0
    isl_val_free(v1);
628
0
    return v2;
629
0
  }
630
80
  
if (80
(isl_val_is_infty(v1) && 80
isl_val_is_neginfty(v2)3
) ||
631
79
      
(isl_val_is_neginfty(v1) && 79
isl_val_is_infty(v2)0
))
{1
632
1
    isl_val_free(v2);
633
1
    return isl_val_set_nan(v1);
634
1
  }
635
79
  
if (79
isl_val_is_infty(v1) || 79
isl_val_is_neginfty(v1)77
)
{2
636
2
    isl_val_free(v2);
637
2
    return v1;
638
2
  }
639
77
  
if (77
isl_val_is_infty(v2) || 77
isl_val_is_neginfty(v2)76
)
{1
640
1
    isl_val_free(v1);
641
1
    return v2;
642
1
  }
643
76
  
if (76
isl_val_is_zero(v1)76
)
{46
644
46
    isl_val_free(v1);
645
46
    return v2;
646
46
  }
647
30
  
if (30
isl_val_is_zero(v2)30
)
{3
648
3
    isl_val_free(v2);
649
3
    return v1;
650
3
  }
651
30
652
27
  v1 = isl_val_cow(v1);
653
27
  if (!v1)
654
0
    goto error;
655
27
  
if (27
isl_val_is_int(v1) && 27
isl_val_is_int(v2)25
)
656
25
    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
27
  isl_val_free(v2);
668
27
  return v1;
669
0
error:
670
0
  isl_val_free(v1);
671
0
  isl_val_free(v2);
672
0
  return NULL;
673
27
}
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
4.50k
{
679
4.50k
  if (!v1)
680
0
    return NULL;
681
4.50k
  
if (4.50k
!isl_val_is_rat(v1)4.50k
)
682
0
    return v1;
683
4.50k
  
if (4.50k
v2 == 04.50k
)
684
0
    return v1;
685
4.50k
  v1 = isl_val_cow(v1);
686
4.50k
  if (!v1)
687
0
    return NULL;
688
4.50k
689
4.50k
  
isl_int_addmul_ui4.50k
(v1->n, v1->d, v2);4.50k
690
4.50k
691
4.50k
  return v1;
692
4.50k
}
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
72
{
698
72
  if (
!v1 || 72
!v272
)
699
0
    goto error;
700
72
  
if (72
isl_val_is_nan(v1)72
)
{0
701
0
    isl_val_free(v2);
702
0
    return v1;
703
0
  }
704
72
  
if (72
isl_val_is_nan(v2)72
)
{0
705
0
    isl_val_free(v1);
706
0
    return v2;
707
0
  }
708
72
  
if (72
(isl_val_is_infty(v1) && 72
isl_val_is_infty(v2)1
) ||
709
71
      
(isl_val_is_neginfty(v1) && 71
isl_val_is_neginfty(v2)0
))
{1
710
1
    isl_val_free(v2);
711
1
    return isl_val_set_nan(v1);
712
1
  }
713
71
  
if (71
isl_val_is_infty(v1) || 71
isl_val_is_neginfty(v1)71
)
{0
714
0
    isl_val_free(v2);
715
0
    return v1;
716
0
  }
717
71
  
if (71
isl_val_is_infty(v2) || 71
isl_val_is_neginfty(v2)70
)
{1
718
1
    isl_val_free(v1);
719
1
    return isl_val_neg(v2);
720
1
  }
721
70
  
if (70
isl_val_is_zero(v2)70
)
{48
722
48
    isl_val_free(v2);
723
48
    return v1;
724
48
  }
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
162
{
757
162
  if (!v1)
758
0
    return NULL;
759
162
  
if (162
!isl_val_is_rat(v1)162
)
760
0
    return v1;
761
162
  
if (162
v2 == 0162
)
762
0
    return v1;
763
162
  v1 = isl_val_cow(v1);
764
162
  if (!v1)
765
0
    return NULL;
766
162
767
162
  
isl_int_submul_ui162
(v1->n, v1->d, v2);162
768
162
769
162
  return v1;
770
162
}
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.51k
{
869
4.51k
  if (
!v1 || 4.51k
!v24.51k
)
870
0
    goto error;
871
4.51k
  
if (4.51k
isl_val_is_nan(v1)4.51k
)
{4
872
4
    isl_val_free(v2);
873
4
    return v1;
874
4
  }
875
4.50k
  
if (4.50k
isl_val_is_nan(v2)4.50k
)
{5
876
5
    isl_val_free(v1);
877
5
    return v2;
878
5
  }
879
4.50k
  
if (4.50k
isl_val_is_zero(v2) ||4.50k
880
4.49k
      
(!isl_val_is_rat(v1) && 4.49k
!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.49k
  
if (4.49k
isl_val_is_zero(v1)4.49k
)
{2.21k
885
2.21k
    isl_val_free(v2);
886
2.21k
    return v1;
887
2.21k
  }
888
2.27k
  
if (2.27k
isl_val_is_infty(v1) || 2.27k
isl_val_is_neginfty(v1)2.27k
)
{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.27k
  
if (2.27k
isl_val_is_infty(v2) || 2.27k
isl_val_is_neginfty(v2)2.27k
)
{4
895
4
    isl_val_free(v2);
896
4
    return isl_val_set_zero(v1);
897
4
  }
898
2.27k
899
2.27k
  v1 = isl_val_cow(v1);
900
2.27k
  if (!v1)
901
0
    goto error;
902
2.27k
  
if (2.27k
isl_val_is_int(v2)2.27k
)
{2.26k
903
2.26k
    isl_int_mul(v1->d, v1->d, v2->n);
904
2.26k
    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.27k
  isl_val_free(v2);
911
2.27k
  return v1;
912
0
error:
913
0
  isl_val_free(v1);
914
0
  isl_val_free(v2);
915
0
  return NULL;
916
2.27k
}
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)16
)
{14
954
14
    isl_val_free(v2);
955
14
    return v1;
956
14
  }
957
5
  v1 = isl_val_cow(v1);
958
5
  if (!v1)
959
0
    goto error;
960
5
  
isl_int_fdiv_r5
(v1->n, v1->n, v2->n);5
961
5
  isl_val_free(v2);
962
5
  return v1;
963
0
error:
964
0
  isl_val_free(v1);
965
0
  isl_val_free(v2);
966
0
  return NULL;
967
5
}
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.28k
{
985
2.28k
  if (
!v1 || 2.28k
!v22.28k
)
986
0
    goto error;
987
2.28k
  
if (2.28k
!isl_val_is_int(v1) || 2.28k
!isl_val_is_int(v2)2.28k
)
988
0
    isl_die(isl_val_get_ctx(v1), isl_error_invalid,
989
2.28k
      "expecting two integers", goto error);
990
2.28k
  
if (2.28k
isl_val_eq(v1, v2)2.28k
)
{38
991
38
    isl_val_free(v2);
992
38
    return v1;
993
38
  }
994
2.24k
  
if (2.24k
isl_val_is_one(v1)2.24k
)
{0
995
0
    isl_val_free(v2);
996
0
    return v1;
997
0
  }
998
2.24k
  
if (2.24k
isl_val_is_one(v2)2.24k
)
{2.21k
999
2.21k
    isl_val_free(v1);
1000
2.21k
    return v2;
1001
2.21k
  }
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
65.5k
{
1110
65.5k
  if (!v)
1111
0
    return isl_bool_error;
1112
65.5k
1113
65.5k
  
return 65.5k
isl_int_is_one65.5k
(v->d);
1114
65.5k
}
1115
1116
/* Does "v" represent a rational value?
1117
 */
1118
isl_bool isl_val_is_rat(__isl_keep isl_val *v)
1119
39.4k
{
1120
39.4k
  if (!v)
1121
0
    return isl_bool_error;
1122
39.4k
1123
39.4k
  
return !39.4k
isl_int_is_zero39.4k
(v->d);
1124
39.4k
}
1125
1126
/* Does "v" represent NaN?
1127
 */
1128
isl_bool isl_val_is_nan(__isl_keep isl_val *v)
1129
45.6k
{
1130
45.6k
  if (!v)
1131
0
    return isl_bool_error;
1132
45.6k
1133
45.6k
  
return 45.6k
isl_int_is_zero45.6k
(v->n) &&
isl_int_is_zero8.75k
(v->d);
1134
45.6k
}
1135
1136
/* Does "v" represent +infinity?
1137
 */
1138
isl_bool isl_val_is_infty(__isl_keep isl_val *v)
1139
5.81k
{
1140
5.81k
  if (!v)
1141
0
    return isl_bool_error;
1142
5.81k
1143
5.81k
  
return 5.81k
isl_int_is_pos5.81k
(v->n) &&
isl_int_is_zero5.51k
(v->d);
1144
5.81k
}
1145
1146
/* Does "v" represent -infinity?
1147
 */
1148
isl_bool isl_val_is_neginfty(__isl_keep isl_val *v)
1149
5.03k
{
1150
5.03k
  if (!v)
1151
0
    return isl_bool_error;
1152
5.03k
1153
5.03k
  
return 5.03k
isl_int_is_neg5.03k
(v->n) &&
isl_int_is_zero88
(v->d);
1154
5.03k
}
1155
1156
/* Does "v" represent the integer zero?
1157
 */
1158
isl_bool isl_val_is_zero(__isl_keep isl_val *v)
1159
62.8k
{
1160
62.8k
  if (!v)
1161
0
    return isl_bool_error;
1162
62.8k
1163
62.8k
  
return 62.8k
isl_int_is_zero62.8k
(v->n) &&
!41.6k
isl_int_is_zero41.6k
(v->d);
1164
62.8k
}
1165
1166
/* Does "v" represent the integer one?
1167
 */
1168
isl_bool isl_val_is_one(__isl_keep isl_val *v)
1169
25.0k
{
1170
25.0k
  if (!v)
1171
0
    return isl_bool_error;
1172
25.0k
1173
25.0k
  
if (25.0k
isl_val_is_nan(v)25.0k
)
1174
0
    return isl_bool_false;
1175
25.0k
1176
25.0k
  
return 25.0k
isl_int_eq25.0k
(v->n, v->d);
1177
25.0k
}
1178
1179
/* Does "v" represent the integer negative one?
1180
 */
1181
isl_bool isl_val_is_negone(__isl_keep isl_val *v)
1182
2.63k
{
1183
2.63k
  if (!v)
1184
0
    return isl_bool_error;
1185
2.63k
1186
2.63k
  
return 2.63k
isl_int_is_neg2.63k
(v->n) &&
isl_int_abs_eq225
(v->n, v->d);
1187
2.63k
}
1188
1189
/* Is "v" (strictly) positive?
1190
 */
1191
isl_bool isl_val_is_pos(__isl_keep isl_val *v)
1192
1.57k
{
1193
1.57k
  if (!v)
1194
0
    return isl_bool_error;
1195
1.57k
1196
1.57k
  
return 1.57k
isl_int_is_pos1.57k
(v->n);
1197
1.57k
}
1198
1199
/* Is "v" (strictly) negative?
1200
 */
1201
isl_bool isl_val_is_neg(__isl_keep isl_val *v)
1202
16.4k
{
1203
16.4k
  if (!v)
1204
0
    return isl_bool_error;
1205
16.4k
1206
16.4k
  
return 16.4k
isl_int_is_neg16.4k
(v->n);
1207
16.4k
}
1208
1209
/* Is "v" non-negative?
1210
 */
1211
isl_bool isl_val_is_nonneg(__isl_keep isl_val *v)
1212
716
{
1213
716
  if (!v)
1214
0
    return isl_bool_error;
1215
716
1216
716
  
if (716
isl_val_is_nan(v)716
)
1217
0
    return isl_bool_false;
1218
716
1219
716
  
return 716
isl_int_is_nonneg716
(v->n);
1220
716
}
1221
1222
/* Is "v" non-positive?
1223
 */
1224
isl_bool isl_val_is_nonpos(__isl_keep isl_val *v)
1225
0
{
1226
0
  if (!v)
1227
0
    return isl_bool_error;
1228
0
1229
0
  
if (0
isl_val_is_nan(v)0
)
1230
0
    return isl_bool_false;
1231
0
1232
0
  
return 0
isl_int_is_nonpos0
(v->n);
1233
0
}
1234
1235
/* Return the sign of "v".
1236
 *
1237
 * The sign of NaN is undefined.
1238
 */
1239
int isl_val_sgn(__isl_keep isl_val *v)
1240
2.23k
{
1241
2.23k
  if (!v)
1242
0
    return 0;
1243
2.23k
  
if (2.23k
isl_val_is_zero(v)2.23k
)
1244
980
    return 0;
1245
1.25k
  
if (1.25k
isl_val_is_pos(v)1.25k
)
1246
736
    return 1;
1247
521
  return -1;
1248
1.25k
}
1249
1250
/* Is "v1" (strictly) less than "v2"?
1251
 */
1252
isl_bool isl_val_lt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1253
20
{
1254
20
  isl_int t;
1255
20
  isl_bool lt;
1256
20
1257
20
  if (
!v1 || 20
!v220
)
1258
0
    return isl_bool_error;
1259
20
  
if (20
isl_val_is_int(v1) && 20
isl_val_is_int(v2)20
)
1260
20
    
return 20
isl_int_lt20
(v1->n, v2->n);
1261
0
  
if (0
isl_val_is_nan(v1) || 0
isl_val_is_nan(v2)0
)
1262
0
    return isl_bool_false;
1263
0
  
if (0
isl_val_eq(v1, v2)0
)
1264
0
    return isl_bool_false;
1265
0
  
if (0
isl_val_is_infty(v2)0
)
1266
0
    return isl_bool_true;
1267
0
  
if (0
isl_val_is_infty(v1)0
)
1268
0
    return isl_bool_false;
1269
0
  
if (0
isl_val_is_neginfty(v1)0
)
1270
0
    return isl_bool_true;
1271
0
  
if (0
isl_val_is_neginfty(v2)0
)
1272
0
    return isl_bool_false;
1273
0
1274
0
  
isl_int_init0
(t);0
1275
0
  isl_int_mul(t, v1->n, v2->d);
1276
0
  isl_int_submul(t, v2->n, v1->d);
1277
0
  lt = isl_int_is_neg(t);
1278
0
  isl_int_clear(t);
1279
0
1280
0
  return lt;
1281
0
}
1282
1283
/* Is "v1" (strictly) greater than "v2"?
1284
 */
1285
isl_bool isl_val_gt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1286
0
{
1287
0
  return isl_val_lt(v2, v1);
1288
0
}
1289
1290
/* Is "v1" less than or equal to "v2"?
1291
 */
1292
isl_bool isl_val_le(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1293
10
{
1294
10
  isl_int t;
1295
10
  isl_bool le;
1296
10
1297
10
  if (
!v1 || 10
!v210
)
1298
0
    return isl_bool_error;
1299
10
  
if (10
isl_val_is_int(v1) && 10
isl_val_is_int(v2)6
)
1300
4
    
return 4
isl_int_le4
(v1->n, v2->n);
1301
6
  
if (6
isl_val_is_nan(v1) || 6
isl_val_is_nan(v2)6
)
1302
0
    return isl_bool_false;
1303
6
  
if (6
isl_val_eq(v1, v2)6
)
1304
0
    return isl_bool_true;
1305
6
  
if (6
isl_val_is_infty(v2)6
)
1306
2
    return isl_bool_true;
1307
4
  
if (4
isl_val_is_infty(v1)4
)
1308
2
    return isl_bool_false;
1309
2
  
if (2
isl_val_is_neginfty(v1)2
)
1310
1
    return isl_bool_true;
1311
1
  
if (1
isl_val_is_neginfty(v2)1
)
1312
1
    return isl_bool_false;
1313
1
1314
0
  
isl_int_init0
(t);0
1315
0
  isl_int_mul(t, v1->n, v2->d);
1316
0
  isl_int_submul(t, v2->n, v1->d);
1317
0
  le = isl_int_is_nonpos(t);
1318
0
  isl_int_clear(t);
1319
0
1320
0
  return le;
1321
1
}
1322
1323
/* Is "v1" greater than or equal to "v2"?
1324
 */
1325
isl_bool isl_val_ge(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1326
5
{
1327
5
  return isl_val_le(v2, v1);
1328
5
}
1329
1330
/* How does "v" compare to "i"?
1331
 *
1332
 * Return 1 if v is greater, -1 if v is smaller and 0 if v is equal to i.
1333
 *
1334
 * If v is NaN (or NULL), then the result is undefined.
1335
 */
1336
int isl_val_cmp_si(__isl_keep isl_val *v, long i)
1337
838
{
1338
838
  isl_int t;
1339
838
  int cmp;
1340
838
1341
838
  if (!v)
1342
0
    return 0;
1343
838
  
if (838
isl_val_is_int(v)838
)
1344
838
    
return 838
isl_int_cmp_si838
(v->n, i);
1345
0
  
if (0
isl_val_is_nan(v)0
)
1346
0
    return 0;
1347
0
  
if (0
isl_val_is_infty(v)0
)
1348
0
    return 1;
1349
0
  
if (0
isl_val_is_neginfty(v)0
)
1350
0
    return -1;
1351
0
1352
0
  
isl_int_init0
(t);0
1353
0
  isl_int_mul_si(t, v->d, i);
1354
0
  isl_int_sub(t, v->n, t);
1355
0
  cmp = isl_int_sgn(t);
1356
0
  isl_int_clear(t);
1357
0
1358
0
  return cmp;
1359
0
}
1360
1361
/* Is "v1" equal to "v2"?
1362
 */
1363
isl_bool isl_val_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1364
2.38k
{
1365
2.38k
  if (
!v1 || 2.38k
!v22.38k
)
1366
0
    return isl_bool_error;
1367
2.38k
  
if (2.38k
isl_val_is_nan(v1) || 2.38k
isl_val_is_nan(v2)2.38k
)
1368
0
    return isl_bool_false;
1369
2.38k
1370
2.38k
  
return 2.38k
isl_int_eq2.38k
(v1->n, v2->n) &&
isl_int_eq137
(v1->d, v2->d);
1371
2.38k
}
1372
1373
/* Is "v1" equal to "v2" in absolute value?
1374
 */
1375
isl_bool isl_val_abs_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1376
12
{
1377
12
  if (
!v1 || 12
!v212
)
1378
0
    return isl_bool_error;
1379
12
  
if (12
isl_val_is_nan(v1) || 12
isl_val_is_nan(v2)12
)
1380
0
    return isl_bool_false;
1381
12
1382
12
  
return 12
isl_int_abs_eq12
(v1->n, v2->n) &&
isl_int_eq12
(v1->d, v2->d);
1383
12
}
1384
1385
/* Is "v1" different from "v2"?
1386
 */
1387
isl_bool isl_val_ne(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1388
5
{
1389
5
  if (
!v1 || 5
!v25
)
1390
0
    return isl_bool_error;
1391
5
  
if (5
isl_val_is_nan(v1) || 5
isl_val_is_nan(v2)5
)
1392
0
    return isl_bool_false;
1393
5
1394
5
  
return 5
isl_int_ne5
(v1->n, v2->n) ||
isl_int_ne1
(v1->d, v2->d);
1395
5
}
1396
1397
/* Print a textual representation of "v" onto "p".
1398
 */
1399
__isl_give isl_printer *isl_printer_print_val(__isl_take isl_printer *p,
1400
  __isl_keep isl_val *v)
1401
5.14k
{
1402
5.14k
  int neg;
1403
5.14k
1404
5.14k
  if (
!p || 5.14k
!v5.14k
)
1405
0
    return isl_printer_free(p);
1406
5.14k
1407
5.14k
  
neg = 5.14k
isl_int_is_neg5.14k
(v->n);
1408
5.14k
  if (
neg5.14k
)
{48
1409
48
    p = isl_printer_print_str(p, "-");
1410
48
    isl_int_neg(v->n, v->n);
1411
48
  }
1412
5.14k
  if (
isl_int_is_zero5.14k
(v->d))
{0
1413
0
    int sgn = isl_int_sgn(v->n);
1414
0
    p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1415
0
              
sgn == 0 ? 0
"NaN"0
:
"infty"0
);
1416
0
  } else
1417
5.14k
    p = isl_printer_print_isl_int(p, v->n);
1418
5.14k
  if (neg)
1419
48
    isl_int_neg(v->n, v->n);
1420
5.14k
  if (
!5.14k
isl_int_is_zero5.14k
(v->d) &&
!5.14k
isl_int_is_one5.14k
(v->d))
{0
1421
0
    p = isl_printer_print_str(p, "/");
1422
0
    p = isl_printer_print_isl_int(p, v->d);
1423
0
  }
1424
5.14k
1425
5.14k
  return p;
1426
5.14k
}
1427
1428
/* Is "val1" (obviously) equal to "val2"?
1429
 *
1430
 * This is a private copy of isl_val_eq for use in the generic
1431
 * isl_multi_*_plain_is_equal instantiated for isl_val.
1432
 */
1433
int isl_val_plain_is_equal(__isl_keep isl_val *val1, __isl_keep isl_val *val2)
1434
0
{
1435
0
  return isl_val_eq(val1, val2);
1436
0
}
1437
1438
/* Does "v" have any non-zero coefficients
1439
 * for any dimension in the given range?
1440
 *
1441
 * This function is only meant to be used in the generic isl_multi_*
1442
 * functions which have to deal with base objects that have an associated
1443
 * space.  Since an isl_val does not have any coefficients, this function
1444
 * always return 0.
1445
 */
1446
int isl_val_involves_dims(__isl_keep isl_val *v, enum isl_dim_type type,
1447
  unsigned first, unsigned n)
1448
0
{
1449
0
  if (!v)
1450
0
    return -1;
1451
0
1452
0
  return 0;
1453
0
}
1454
1455
/* Insert "n" dimensions of type "type" at position "first".
1456
 *
1457
 * This function is only meant to be used in the generic isl_multi_*
1458
 * functions which have to deal with base objects that have an associated
1459
 * space.  Since an isl_val does not have an associated space, this function
1460
 * does not do anything.
1461
 */
1462
__isl_give isl_val *isl_val_insert_dims(__isl_take isl_val *v,
1463
  enum isl_dim_type type, unsigned first, unsigned n)
1464
0
{
1465
0
  return v;
1466
0
}
1467
1468
/* Drop the the "n" first dimensions of type "type" at position "first".
1469
 *
1470
 * This function is only meant to be used in the generic isl_multi_*
1471
 * functions which have to deal with base objects that have an associated
1472
 * space.  Since an isl_val does not have an associated space, this function
1473
 * does not do anything.
1474
 */
1475
__isl_give isl_val *isl_val_drop_dims(__isl_take isl_val *v,
1476
  enum isl_dim_type type, unsigned first, unsigned n)
1477
0
{
1478
0
  return v;
1479
0
}
1480
1481
/* Change the name of the dimension of type "type" at position "pos" to "s".
1482
 *
1483
 * This function is only meant to be used in the generic isl_multi_*
1484
 * functions which have to deal with base objects that have an associated
1485
 * space.  Since an isl_val does not have an associated space, this function
1486
 * does not do anything.
1487
 */
1488
__isl_give isl_val *isl_val_set_dim_name(__isl_take isl_val *v,
1489
  enum isl_dim_type type, unsigned pos, const char *s)
1490
0
{
1491
0
  return v;
1492
0
}
1493
1494
/* Return the space of "v".
1495
 *
1496
 * This function is only meant to be used in the generic isl_multi_*
1497
 * functions which have to deal with base objects that have an associated
1498
 * space.  The conditions surrounding the call to this function make sure
1499
 * that this function will never actually get called.  We return a valid
1500
 * space anyway, just in case.
1501
 */
1502
__isl_give isl_space *isl_val_get_space(__isl_keep isl_val *v)
1503
0
{
1504
0
  if (!v)
1505
0
    return NULL;
1506
0
1507
0
  return isl_space_params_alloc(isl_val_get_ctx(v), 0);
1508
0
}
1509
1510
/* Reset the domain space of "v" to "space".
1511
 *
1512
 * This function is only meant to be used in the generic isl_multi_*
1513
 * functions which have to deal with base objects that have an associated
1514
 * space.  Since an isl_val does not have an associated space, this function
1515
 * does not do anything, apart from error handling and cleaning up memory.
1516
 */
1517
__isl_give isl_val *isl_val_reset_domain_space(__isl_take isl_val *v,
1518
  __isl_take isl_space *space)
1519
0
{
1520
0
  if (!space)
1521
0
    return isl_val_free(v);
1522
0
  isl_space_free(space);
1523
0
  return v;
1524
0
}
1525
1526
/* Align the parameters of "v" to those of "space".
1527
 *
1528
 * This function is only meant to be used in the generic isl_multi_*
1529
 * functions which have to deal with base objects that have an associated
1530
 * space.  Since an isl_val does not have an associated space, this function
1531
 * does not do anything, apart from error handling and cleaning up memory.
1532
 * Note that the conditions surrounding the call to this function make sure
1533
 * that this function will never actually get called.
1534
 */
1535
__isl_give isl_val *isl_val_align_params(__isl_take isl_val *v,
1536
  __isl_take isl_space *space)
1537
0
{
1538
0
  if (!space)
1539
0
    return isl_val_free(v);
1540
0
  isl_space_free(space);
1541
0
  return v;
1542
0
}
1543
1544
/* Reorder the dimensions of the domain of "v" according
1545
 * to the given reordering.
1546
 *
1547
 * This function is only meant to be used in the generic isl_multi_*
1548
 * functions which have to deal with base objects that have an associated
1549
 * space.  Since an isl_val does not have an associated space, this function
1550
 * does not do anything, apart from error handling and cleaning up memory.
1551
 */
1552
__isl_give isl_val *isl_val_realign_domain(__isl_take isl_val *v,
1553
  __isl_take isl_reordering *r)
1554
0
{
1555
0
  if (!r)
1556
0
    return isl_val_free(v);
1557
0
  isl_reordering_free(r);
1558
0
  return v;
1559
0
}
1560
1561
/* Return an isl_val that is zero on "ls".
1562
 *
1563
 * This function is only meant to be used in the generic isl_multi_*
1564
 * functions which have to deal with base objects that have an associated
1565
 * space.  Since an isl_val does not have an associated space, this function
1566
 * simply returns a zero isl_val in the same context as "ls".
1567
 */
1568
__isl_give isl_val *isl_val_zero_on_domain(__isl_take isl_local_space *ls)
1569
2.24k
{
1570
2.24k
  isl_ctx *ctx;
1571
2.24k
1572
2.24k
  if (!ls)
1573
0
    return NULL;
1574
2.24k
  ctx = isl_local_space_get_ctx(ls);
1575
2.24k
  isl_local_space_free(ls);
1576
2.24k
  return isl_val_zero(ctx);
1577
2.24k
}
1578
1579
/* Do the parameters of "v" match those of "space"?
1580
 *
1581
 * This function is only meant to be used in the generic isl_multi_*
1582
 * functions which have to deal with base objects that have an associated
1583
 * space.  Since an isl_val does not have an associated space, this function
1584
 * simply returns true, except if "v" or "space" are NULL.
1585
 */
1586
isl_bool isl_val_matching_params(__isl_keep isl_val *v,
1587
  __isl_keep isl_space *space)
1588
8.30k
{
1589
8.30k
  if (
!v || 8.30k
!space8.30k
)
1590
0
    return isl_bool_error;
1591
8.30k
  return isl_bool_true;
1592
8.30k
}
1593
1594
/* Check that the domain space of "v" matches "space".
1595
 *
1596
 * This function is only meant to be used in the generic isl_multi_*
1597
 * functions which have to deal with base objects that have an associated
1598
 * space.  Since an isl_val does not have an associated space, this function
1599
 * simply returns 0, except if "v" or "space" are NULL.
1600
 */
1601
isl_stat isl_val_check_match_domain_space(__isl_keep isl_val *v,
1602
  __isl_keep isl_space *space)
1603
8.30k
{
1604
8.30k
  if (
!v || 8.30k
!space8.30k
)
1605
0
    return isl_stat_error;
1606
8.30k
  return isl_stat_ok;
1607
8.30k
}
1608
1609
0
#define isl_val_involves_nan isl_val_is_nan
1610
1611
#undef BASE
1612
#define BASE val
1613
1614
#define NO_DOMAIN
1615
#define NO_IDENTITY
1616
#define NO_FROM_BASE
1617
#define NO_MOVE_DIMS
1618
#include <isl_multi_templ.c>
1619
1620
/* Apply "fn" to each of the elements of "mv" with as second argument "v".
1621
 */
1622
static __isl_give isl_multi_val *isl_multi_val_fn_val(
1623
  __isl_take isl_multi_val *mv,
1624
  __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
1625
          __isl_take isl_val *v2),
1626
  __isl_take isl_val *v)
1627
4
{
1628
4
  int i;
1629
4
1630
4
  mv = isl_multi_val_cow(mv);
1631
4
  if (
!mv || 4
!v4
)
1632
0
    goto error;
1633
4
1634
16
  
for (i = 0; 4
i < mv->n16
;
++i12
)
{12
1635
12
    mv->p[i] = fn(mv->p[i], isl_val_copy(v));
1636
12
    if (!mv->p[i])
1637
0
      goto error;
1638
12
  }
1639
4
1640
4
  isl_val_free(v);
1641
4
  return mv;
1642
0
error:
1643
0
  isl_val_free(v);
1644
0
  isl_multi_val_free(mv);
1645
0
  return NULL;
1646
4
}
1647
1648
/* Add "v" to each of the elements of "mv".
1649
 */
1650
__isl_give isl_multi_val *isl_multi_val_add_val(__isl_take isl_multi_val *mv,
1651
  __isl_take isl_val *v)
1652
3
{
1653
3
  if (!v)
1654
0
    return isl_multi_val_free(mv);
1655
3
  
if (3
isl_val_is_zero(v)3
)
{2
1656
2
    isl_val_free(v);
1657
2
    return mv;
1658
2
  }
1659
1
  return isl_multi_val_fn_val(mv, &isl_val_add, v);
1660
3
}
1661
1662
/* Reduce the elements of "mv" modulo "v".
1663
 */
1664
__isl_give isl_multi_val *isl_multi_val_mod_val(__isl_take isl_multi_val *mv,
1665
  __isl_take isl_val *v)
1666
3
{
1667
3
  return isl_multi_val_fn_val(mv, &isl_val_mod, v);
1668
3
}