Coverage Report

Created: 2018-12-13 20:48

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/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
180k
{
23
180k
  isl_val *v;
24
180k
25
180k
  v = isl_alloc_type(ctx, struct isl_val);
26
180k
  if (!v)
27
0
    return NULL;
28
180k
29
180k
  v->ctx = ctx;
30
180k
  isl_ctx_ref(ctx);
31
180k
  v->ref = 1;
32
180k
  isl_int_init(v->n);
33
180k
  isl_int_init(v->d);
34
180k
35
180k
  return v;
36
180k
}
37
38
/* Return a reference to an isl_val representing zero.
39
 */
40
__isl_give isl_val *isl_val_zero(isl_ctx *ctx)
41
10.0k
{
42
10.0k
  return isl_val_int_from_si(ctx, 0);
43
10.0k
}
44
45
/* Return a reference to an isl_val representing one.
46
 */
47
__isl_give isl_val *isl_val_one(isl_ctx *ctx)
48
6.33k
{
49
6.33k
  return isl_val_int_from_si(ctx, 1);
50
6.33k
}
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
1.08k
{
63
1.08k
  isl_val *v;
64
1.08k
65
1.08k
  v = isl_val_alloc(ctx);
66
1.08k
  if (!v)
67
0
    return NULL;
68
1.08k
69
1.08k
  isl_int_set_si(v->n, 0);
70
1.08k
  isl_int_set_si(v->d, 0);
71
1.08k
72
1.08k
  return v;
73
1.08k
}
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 (isl_val_is_nan(v))
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_si(v->n, 0);
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
257
{
97
257
  isl_val *v;
98
257
99
257
  v = isl_val_alloc(ctx);
100
257
  if (!v)
101
0
    return NULL;
102
257
103
257
  isl_int_set_si(v->n, 1);
104
257
  isl_int_set_si(v->d, 0);
105
257
106
257
  return v;
107
257
}
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_si(v->n, -1);
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
38.6k
{
129
38.6k
  isl_val *v;
130
38.6k
131
38.6k
  v = isl_val_alloc(ctx);
132
38.6k
  if (!v)
133
0
    return NULL;
134
38.6k
135
38.6k
  isl_int_set_si(v->n, i);
136
38.6k
  isl_int_set_si(v->d, 1);
137
38.6k
138
38.6k
  return v;
139
38.6k
}
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 (isl_val_is_int(v) && isl_int_cmp_si(v->n, i) == 0)
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_si(v->n, i);
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
6.19k
{
170
6.19k
  isl_val *v;
171
6.19k
172
6.19k
  v = isl_val_alloc(ctx);
173
6.19k
  if (!v)
174
0
    return NULL;
175
6.19k
176
6.19k
  isl_int_set_ui(v->n, u);
177
6.19k
  isl_int_set_si(v->d, 1);
178
6.19k
179
6.19k
  return v;
180
6.19k
}
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
28.8k
{
186
28.8k
  isl_val *v;
187
28.8k
188
28.8k
  v = isl_val_alloc(ctx);
189
28.8k
  if (!v)
190
0
    return NULL;
191
28.8k
192
28.8k
  isl_int_set(v->n, n);
193
28.8k
  isl_int_set_si(v->d, 1);
194
28.8k
195
28.8k
  return v;
196
28.8k
}
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
66.9k
{
204
66.9k
  isl_val *v;
205
66.9k
206
66.9k
  v = isl_val_alloc(ctx);
207
66.9k
  if (!v)
208
0
    return NULL;
209
66.9k
210
66.9k
  isl_int_set(v->n, n);
211
66.9k
  isl_int_set(v->d, d);
212
66.9k
213
66.9k
  return v;
214
66.9k
}
215
216
/* Return a new reference to "v".
217
 */
218
__isl_give isl_val *isl_val_copy(__isl_keep isl_val *v)
219
69.1k
{
220
69.1k
  if (!v)
221
0
    return NULL;
222
69.1k
223
69.1k
  v->ref++;
224
69.1k
  return v;
225
69.1k
}
226
227
/* Return a fresh copy of "val".
228
 */
229
__isl_give isl_val *isl_val_dup(__isl_keep isl_val *val)
230
13.6k
{
231
13.6k
  isl_val *dup;
232
13.6k
233
13.6k
  if (!val)
234
0
    return NULL;
235
13.6k
236
13.6k
  dup = isl_val_alloc(isl_val_get_ctx(val));
237
13.6k
  if (!dup)
238
0
    return NULL;
239
13.6k
240
13.6k
  isl_int_set(dup->n, val->n);
241
13.6k
  isl_int_set(dup->d, val->d);
242
13.6k
243
13.6k
  return dup;
244
13.6k
}
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
20.6k
{
251
20.6k
  if (!val)
252
0
    return NULL;
253
20.6k
254
20.6k
  if (val->ref == 1)
255
6.95k
    return val;
256
13.6k
  val->ref--;
257
13.6k
  return isl_val_dup(val);
258
13.6k
}
259
260
/* Free "v" and return NULL.
261
 */
262
__isl_null isl_val *isl_val_free(__isl_take isl_val *v)
263
244k
{
264
244k
  if (!v)
265
8.48k
    return NULL;
266
236k
267
236k
  if (--v->ref > 0)
268
55.4k
    return NULL;
269
180k
270
180k
  isl_ctx_deref(v->ctx);
271
180k
  isl_int_clear(v->n);
272
180k
  isl_int_clear(v->d);
273
180k
  free(v);
274
180k
  return NULL;
275
180k
}
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
2.41k
{
283
2.41k
  if (!v)
284
0
    return 0;
285
2.41k
  if (!isl_val_is_rat(v))
286
2.41k
    
isl_die0
(isl_val_get_ctx(v), isl_error_invalid,
287
2.41k
      "expecting rational value", return 0);
288
2.41k
  if (!isl_int_fits_slong(v->n))
289
2.41k
    
isl_die0
(isl_val_get_ctx(v), isl_error_invalid,
290
2.41k
      "numerator too large", return 0);
291
2.41k
  return isl_int_get_si(v->n);
292
2.41k
}
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
isl_stat isl_val_get_num_isl_int(__isl_keep isl_val *v, isl_int *n)
299
4
{
300
4
  if (!v)
301
0
    return isl_stat_error;
302
4
  if (!isl_val_is_rat(v))
303
4
    
isl_die0
(isl_val_get_ctx(v), isl_error_invalid,
304
4
      "expecting rational value", return isl_stat_error);
305
4
  isl_int_set(*n, v->n);
306
4
  return isl_stat_ok;
307
4
}
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 (!isl_val_is_rat(v))
318
0
    isl_die(isl_val_get_ctx(v), isl_error_invalid,
319
0
      "expecting rational value", return 0);
320
0
  if (!isl_int_fits_slong(v->d))
321
0
    isl_die(isl_val_get_ctx(v), isl_error_invalid,
322
0
      "denominator too large", return 0);
323
0
  return isl_int_get_si(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 (!isl_val_is_rat(v))
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 (!isl_val_is_rat(v))
347
0
    isl_die(isl_val_get_ctx(v), isl_error_invalid,
348
0
      "expecting rational value", return 0);
349
0
  return isl_int_get_d(v->n) / isl_int_get_d(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
30.5k
{
356
30.5k
  return val ? val->ctx : NULL;
357
30.5k
}
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 = isl_hash_init();
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
74.9k
{
386
74.9k
  isl_ctx *ctx;
387
74.9k
388
74.9k
  if (!v)
389
0
    return NULL;
390
74.9k
  if (isl_val_is_int(v))
391
74.6k
    return v;
392
220
  if (!isl_val_is_rat(v))
393
0
    return v;
394
220
  if (isl_int_is_neg(v->d)) {
395
4
    isl_int_neg(v->d, v->d);
396
4
    isl_int_neg(v->n, v->n);
397
4
  }
398
220
  ctx = isl_val_get_ctx(v);
399
220
  isl_int_gcd(ctx->normalize_gcd, v->n, v->d);
400
220
  if (isl_int_is_one(ctx->normalize_gcd))
401
220
    
return v98
;
402
122
  isl_int_divexact(v->n, v->n, ctx->normalize_gcd);
403
122
  isl_int_divexact(v->d, v->d, ctx->normalize_gcd);
404
122
  return v;
405
122
}
406
407
/* Return the opposite of "v".
408
 */
409
__isl_give isl_val *isl_val_neg(__isl_take isl_val *v)
410
3.71k
{
411
3.71k
  if (!v)
412
0
    return NULL;
413
3.71k
  if (isl_val_is_nan(v))
414
1
    return v;
415
3.71k
  if (isl_val_is_zero(v))
416
263
    return v;
417
3.45k
418
3.45k
  v = isl_val_cow(v);
419
3.45k
  if (!v)
420
0
    return NULL;
421
3.45k
  isl_int_neg(v->n, v->n);
422
3.45k
423
3.45k
  return v;
424
3.45k
}
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 (isl_val_is_nan(v))
433
1
    return v;
434
8
  if (isl_val_is_zero(v)) {
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 (isl_val_is_infty(v) || 
isl_val_is_neginfty(v)6
) {
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
5
445
5
  v = isl_val_cow(v);
446
5
  if (!v)
447
0
    return NULL;
448
5
  isl_int_swap(v->n, v->d);
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
1.04k
{
457
1.04k
  if (!v)
458
0
    return NULL;
459
1.04k
  if (isl_val_is_nan(v))
460
1
    return v;
461
1.04k
  if (isl_val_is_nonneg(v))
462
677
    return v;
463
364
  return isl_val_neg(v);
464
364
}
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 (isl_val_is_int(v))
474
3
    return v;
475
9
  if (!isl_val_is_rat(v))
476
3
    return v;
477
6
478
6
  v = isl_val_cow(v);
479
6
  if (!v)
480
0
    return NULL;
481
6
  isl_int_fdiv_q(v->n, v->n, v->d);
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
56
{
492
56
  if (!v)
493
0
    return NULL;
494
56
  if (isl_val_is_int(v))
495
36
    return v;
496
20
  if (!isl_val_is_rat(v))
497
3
    return v;
498
17
499
17
  v = isl_val_cow(v);
500
17
  if (!v)
501
0
    return NULL;
502
17
  isl_int_cdiv_q(v->n, v->n, v->d);
503
17
  isl_int_set_si(v->d, 1);
504
17
505
17
  return v;
506
17
}
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 (isl_val_is_int(v))
516
0
    return v;
517
0
  if (!isl_val_is_rat(v))
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_q(v->n, v->n, v->d);
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_pow2(__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 (!isl_val_is_int(v))
540
5.86k
    
isl_die0
(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
5.86k
    
isl_int_neg2
(v->n, v->n);
546
5.86k
  if (!isl_int_fits_ulong(v->n))
547
5.86k
    
isl_die0
(isl_val_get_ctx(v), isl_error_invalid,
548
5.86k
      "exponent too large", return isl_val_free(v));
549
5.86k
  exp = isl_int_get_ui(v->n);
550
5.86k
  if (neg) {
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
/* This is an alternative name for the function above.
561
 */
562
__isl_give isl_val *isl_val_2exp(__isl_take isl_val *v)
563
2.95k
{
564
2.95k
  return isl_val_pow2(v);
565
2.95k
}
566
567
/* Return the minimum of "v1" and "v2".
568
 */
569
__isl_give isl_val *isl_val_min(__isl_take isl_val *v1, __isl_take isl_val *v2)
570
6
{
571
6
  if (!v1 || !v2)
572
0
    goto error;
573
6
574
6
  if (isl_val_is_nan(v1)) {
575
0
    isl_val_free(v2);
576
0
    return v1;
577
0
  }
578
6
  if (isl_val_is_nan(v2)) {
579
1
    isl_val_free(v1);
580
1
    return v2;
581
1
  }
582
5
  if (isl_val_le(v1, v2)) {
583
2
    isl_val_free(v2);
584
2
    return v1;
585
3
  } else {
586
3
    isl_val_free(v1);
587
3
    return v2;
588
3
  }
589
0
error:
590
0
  isl_val_free(v1);
591
0
  isl_val_free(v2);
592
0
  return NULL;
593
5
}
594
595
/* Return the maximum of "v1" and "v2".
596
 */
597
__isl_give isl_val *isl_val_max(__isl_take isl_val *v1, __isl_take isl_val *v2)
598
6
{
599
6
  if (!v1 || !v2)
600
0
    goto error;
601
6
602
6
  if (isl_val_is_nan(v1)) {
603
0
    isl_val_free(v2);
604
0
    return v1;
605
0
  }
606
6
  if (isl_val_is_nan(v2)) {
607
1
    isl_val_free(v1);
608
1
    return v2;
609
1
  }
610
5
  if (isl_val_ge(v1, v2)) {
611
3
    isl_val_free(v2);
612
3
    return v1;
613
3
  } else {
614
2
    isl_val_free(v1);
615
2
    return v2;
616
2
  }
617
0
error:
618
0
  isl_val_free(v1);
619
0
  isl_val_free(v2);
620
0
  return NULL;
621
5
}
622
623
/* Return the sum of "v1" and "v2".
624
 */
625
__isl_give isl_val *isl_val_add(__isl_take isl_val *v1, __isl_take isl_val *v2)
626
495
{
627
495
  if (!v1 || !v2)
628
0
    goto error;
629
495
  if (isl_val_is_nan(v1)) {
630
0
    isl_val_free(v2);
631
0
    return v1;
632
0
  }
633
495
  if (isl_val_is_nan(v2)) {
634
0
    isl_val_free(v1);
635
0
    return v2;
636
0
  }
637
495
  if ((isl_val_is_infty(v1) && 
isl_val_is_neginfty(v2)3
) ||
638
495
      
(494
isl_val_is_neginfty(v1)494
&&
isl_val_is_infty(v2)0
)) {
639
1
    isl_val_free(v2);
640
1
    return isl_val_set_nan(v1);
641
1
  }
642
494
  if (isl_val_is_infty(v1) || 
isl_val_is_neginfty(v1)492
) {
643
2
    isl_val_free(v2);
644
2
    return v1;
645
2
  }
646
492
  if (isl_val_is_infty(v2) || 
isl_val_is_neginfty(v2)491
) {
647
1
    isl_val_free(v1);
648
1
    return v2;
649
1
  }
650
491
  if (isl_val_is_zero(v1)) {
651
384
    isl_val_free(v1);
652
384
    return v2;
653
384
  }
654
107
  if (isl_val_is_zero(v2)) {
655
3
    isl_val_free(v2);
656
3
    return v1;
657
3
  }
658
104
659
104
  v1 = isl_val_cow(v1);
660
104
  if (!v1)
661
0
    goto error;
662
104
  if (isl_val_is_int(v1) && 
isl_val_is_int(v2)102
)
663
104
    
isl_int_add102
(v1->n, v1->n, v2->n);
664
104
  else {
665
2
    if (isl_int_eq(v1->d, v2->d))
666
2
      
isl_int_add1
(v1->n, v1->n, v2->n);
667
2
    else {
668
1
      isl_int_mul(v1->n, v1->n, v2->d);
669
1
      isl_int_addmul(v1->n, v2->n, v1->d);
670
1
      isl_int_mul(v1->d, v1->d, v2->d);
671
1
    }
672
2
    v1 = isl_val_normalize(v1);
673
2
  }
674
104
  isl_val_free(v2);
675
104
  return v1;
676
0
error:
677
0
  isl_val_free(v1);
678
0
  isl_val_free(v2);
679
0
  return NULL;
680
104
}
681
682
/* Return the sum of "v1" and "v2".
683
 */
684
__isl_give isl_val *isl_val_add_ui(__isl_take isl_val *v1, unsigned long v2)
685
5.30k
{
686
5.30k
  if (!v1)
687
0
    return NULL;
688
5.30k
  if (!isl_val_is_rat(v1))
689
0
    return v1;
690
5.30k
  if (v2 == 0)
691
0
    return v1;
692
5.30k
  v1 = isl_val_cow(v1);
693
5.30k
  if (!v1)
694
0
    return NULL;
695
5.30k
696
5.30k
  isl_int_addmul_ui(v1->n, v1->d, v2);
697
5.30k
698
5.30k
  return v1;
699
5.30k
}
700
701
/* Subtract "v2" from "v1".
702
 */
703
__isl_give isl_val *isl_val_sub(__isl_take isl_val *v1, __isl_take isl_val *v2)
704
165
{
705
165
  if (!v1 || !v2)
706
0
    goto error;
707
165
  if (isl_val_is_nan(v1)) {
708
0
    isl_val_free(v2);
709
0
    return v1;
710
0
  }
711
165
  if (isl_val_is_nan(v2)) {
712
0
    isl_val_free(v1);
713
0
    return v2;
714
0
  }
715
165
  if ((isl_val_is_infty(v1) && 
isl_val_is_infty(v2)1
) ||
716
165
      
(164
isl_val_is_neginfty(v1)164
&&
isl_val_is_neginfty(v2)0
)) {
717
1
    isl_val_free(v2);
718
1
    return isl_val_set_nan(v1);
719
1
  }
720
164
  if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
721
0
    isl_val_free(v2);
722
0
    return v1;
723
0
  }
724
164
  if (isl_val_is_infty(v2) || 
isl_val_is_neginfty(v2)163
) {
725
1
    isl_val_free(v1);
726
1
    return isl_val_neg(v2);
727
1
  }
728
163
  if (isl_val_is_zero(v2)) {
729
113
    isl_val_free(v2);
730
113
    return v1;
731
113
  }
732
50
  if (isl_val_is_zero(v1)) {
733
0
    isl_val_free(v1);
734
0
    return isl_val_neg(v2);
735
0
  }
736
50
737
50
  v1 = isl_val_cow(v1);
738
50
  if (!v1)
739
0
    goto error;
740
50
  if (isl_val_is_int(v1) && 
isl_val_is_int(v2)48
)
741
50
    
isl_int_sub47
(v1->n, v1->n, v2->n);
742
50
  else {
743
3
    if (isl_int_eq(v1->d, v2->d))
744
3
      
isl_int_sub1
(v1->n, v1->n, v2->n);
745
3
    else {
746
2
      isl_int_mul(v1->n, v1->n, v2->d);
747
2
      isl_int_submul(v1->n, v2->n, v1->d);
748
2
      isl_int_mul(v1->d, v1->d, v2->d);
749
2
    }
750
3
    v1 = isl_val_normalize(v1);
751
3
  }
752
50
  isl_val_free(v2);
753
50
  return v1;
754
0
error:
755
0
  isl_val_free(v1);
756
0
  isl_val_free(v2);
757
0
  return NULL;
758
50
}
759
760
/* Subtract "v2" from "v1".
761
 */
762
__isl_give isl_val *isl_val_sub_ui(__isl_take isl_val *v1, unsigned long v2)
763
219
{
764
219
  if (!v1)
765
0
    return NULL;
766
219
  if (!isl_val_is_rat(v1))
767
0
    return v1;
768
219
  if (v2 == 0)
769
0
    return v1;
770
219
  v1 = isl_val_cow(v1);
771
219
  if (!v1)
772
0
    return NULL;
773
219
774
219
  isl_int_submul_ui(v1->n, v1->d, v2);
775
219
776
219
  return v1;
777
219
}
778
779
/* Return the product of "v1" and "v2".
780
 */
781
__isl_give isl_val *isl_val_mul(__isl_take isl_val *v1, __isl_take isl_val *v2)
782
17
{
783
17
  if (!v1 || !v2)
784
0
    goto error;
785
17
  if (isl_val_is_nan(v1)) {
786
0
    isl_val_free(v2);
787
0
    return v1;
788
0
  }
789
17
  if (isl_val_is_nan(v2)) {
790
0
    isl_val_free(v1);
791
0
    return v2;
792
0
  }
793
17
  if ((!isl_val_is_rat(v1) && 
isl_val_is_zero(v2)6
) ||
794
17
      
(16
isl_val_is_zero(v1)16
&&
!isl_val_is_rat(v2)1
)) {
795
2
    isl_val_free(v2);
796
2
    return isl_val_set_nan(v1);
797
2
  }
798
15
  if (isl_val_is_zero(v1)) {
799
0
    isl_val_free(v2);
800
0
    return v1;
801
0
  }
802
15
  if (isl_val_is_zero(v2)) {
803
0
    isl_val_free(v1);
804
0
    return v2;
805
0
  }
806
15
  if (isl_val_is_infty(v1) || 
isl_val_is_neginfty(v1)12
) {
807
5
    if (isl_val_is_neg(v2))
808
2
      v1 = isl_val_neg(v1);
809
5
    isl_val_free(v2);
810
5
    return v1;
811
5
  }
812
10
  if (isl_val_is_infty(v2) || 
isl_val_is_neginfty(v2)9
) {
813
1
    if (isl_val_is_neg(v1))
814
0
      v2 = isl_val_neg(v2);
815
1
    isl_val_free(v1);
816
1
    return v2;
817
1
  }
818
9
819
9
  v1 = isl_val_cow(v1);
820
9
  if (!v1)
821
0
    goto error;
822
9
  if (isl_val_is_int(v1) && 
isl_val_is_int(v2)7
)
823
9
    
isl_int_mul5
(v1->n, v1->n, v2->n);
824
9
  else {
825
4
    isl_int_mul(v1->n, v1->n, v2->n);
826
4
    isl_int_mul(v1->d, v1->d, v2->d);
827
4
    v1 = isl_val_normalize(v1);
828
4
  }
829
9
  isl_val_free(v2);
830
9
  return v1;
831
0
error:
832
0
  isl_val_free(v1);
833
0
  isl_val_free(v2);
834
0
  return NULL;
835
9
}
836
837
/* Return the product of "v1" and "v2".
838
 *
839
 * This is a private copy of isl_val_mul for use in the generic
840
 * isl_multi_*_scale_val instantiated for isl_val.
841
 */
842
__isl_give isl_val *isl_val_scale_val(__isl_take isl_val *v1,
843
  __isl_take isl_val *v2)
844
0
{
845
0
  return isl_val_mul(v1, v2);
846
0
}
847
848
/* Return the product of "v1" and "v2".
849
 */
850
__isl_give isl_val *isl_val_mul_ui(__isl_take isl_val *v1, unsigned long v2)
851
0
{
852
0
  if (!v1)
853
0
    return NULL;
854
0
  if (isl_val_is_nan(v1))
855
0
    return v1;
856
0
  if (!isl_val_is_rat(v1)) {
857
0
    if (v2 == 0)
858
0
      v1 = isl_val_set_nan(v1);
859
0
    return v1;
860
0
  }
861
0
  if (v2 == 1)
862
0
    return v1;
863
0
  v1 = isl_val_cow(v1);
864
0
  if (!v1)
865
0
    return NULL;
866
0
867
0
  isl_int_mul_ui(v1->n, v1->n, v2);
868
0
869
0
  return isl_val_normalize(v1);
870
0
}
871
872
/* Divide "v1" by "v2".
873
 */
874
__isl_give isl_val *isl_val_div(__isl_take isl_val *v1, __isl_take isl_val *v2)
875
10.8k
{
876
10.8k
  if (!v1 || !v2)
877
0
    goto error;
878
10.8k
  if (isl_val_is_nan(v1)) {
879
4
    isl_val_free(v2);
880
4
    return v1;
881
4
  }
882
10.8k
  if (isl_val_is_nan(v2)) {
883
5
    isl_val_free(v1);
884
5
    return v2;
885
5
  }
886
10.8k
  if (isl_val_is_zero(v2) ||
887
10.8k
      
(10.8k
!isl_val_is_rat(v1)10.8k
&&
!isl_val_is_rat(v2)4
)) {
888
9
    isl_val_free(v2);
889
9
    return isl_val_set_nan(v1);
890
9
  }
891
10.8k
  if (isl_val_is_zero(v1)) {
892
5.38k
    isl_val_free(v2);
893
5.38k
    return v1;
894
5.38k
  }
895
5.49k
  if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
896
0
    if (isl_val_is_neg(v2))
897
0
      v1 = isl_val_neg(v1);
898
0
    isl_val_free(v2);
899
0
    return v1;
900
0
  }
901
5.49k
  if (isl_val_is_infty(v2) || 
isl_val_is_neginfty(v2)5.49k
) {
902
4
    isl_val_free(v2);
903
4
    return isl_val_set_zero(v1);
904
4
  }
905
5.49k
906
5.49k
  v1 = isl_val_cow(v1);
907
5.49k
  if (!v1)
908
0
    goto error;
909
5.49k
  if (isl_val_is_int(v2)) {
910
5.48k
    isl_int_mul(v1->d, v1->d, v2->n);
911
5.48k
    v1 = isl_val_normalize(v1);
912
5.48k
  } else {
913
6
    isl_int_mul(v1->d, v1->d, v2->n);
914
6
    isl_int_mul(v1->n, v1->n, v2->d);
915
6
    v1 = isl_val_normalize(v1);
916
6
  }
917
5.49k
  isl_val_free(v2);
918
5.49k
  return v1;
919
0
error:
920
0
  isl_val_free(v1);
921
0
  isl_val_free(v2);
922
0
  return NULL;
923
5.49k
}
924
925
/* Divide "v1" by "v2".
926
 */
927
__isl_give isl_val *isl_val_div_ui(__isl_take isl_val *v1, unsigned long v2)
928
12
{
929
12
  if (!v1)
930
0
    return NULL;
931
12
  if (isl_val_is_nan(v1))
932
0
    return v1;
933
12
  if (v2 == 0)
934
0
    return isl_val_set_nan(v1);
935
12
  if (v2 == 1)
936
0
    return v1;
937
12
  if (isl_val_is_zero(v1))
938
0
    return v1;
939
12
  if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1))
940
0
    return v1;
941
12
  v1 = isl_val_cow(v1);
942
12
  if (!v1)
943
0
    return NULL;
944
12
945
12
  isl_int_mul_ui(v1->d, v1->d, v2);
946
12
947
12
  return isl_val_normalize(v1);
948
12
}
949
950
/* Divide "v1" by "v2".
951
 *
952
 * This is a private copy of isl_val_div for use in the generic
953
 * isl_multi_*_scale_down_val instantiated for isl_val.
954
 */
955
__isl_give isl_val *isl_val_scale_down_val(__isl_take isl_val *v1,
956
  __isl_take isl_val *v2)
957
0
{
958
0
  return isl_val_div(v1, v2);
959
0
}
960
961
/* Given two integer values "v1" and "v2", check if "v1" is divisible by "v2".
962
 */
963
isl_bool isl_val_is_divisible_by(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
964
556
{
965
556
  if (!v1 || !v2)
966
0
    return isl_bool_error;
967
556
968
556
  if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
969
556
    
isl_die0
(isl_val_get_ctx(v1), isl_error_invalid,
970
556
      "expecting two integers", return isl_bool_error);
971
556
972
556
  return isl_int_is_divisible_by(v1->n, v2->n);
973
556
}
974
975
/* Given two integer values "v1" and "v2", return the residue of "v1"
976
 * modulo "v2".
977
 */
978
__isl_give isl_val *isl_val_mod(__isl_take isl_val *v1, __isl_take isl_val *v2)
979
45
{
980
45
  if (!v1 || !v2)
981
0
    goto error;
982
45
  if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
983
45
    
isl_die0
(isl_val_get_ctx(v1), isl_error_invalid,
984
45
      "expecting two integers", goto error);
985
45
  if (isl_val_is_nonneg(v1) && 
isl_val_lt(v1, v2)41
) {
986
15
    isl_val_free(v2);
987
15
    return v1;
988
15
  }
989
30
  v1 = isl_val_cow(v1);
990
30
  if (!v1)
991
0
    goto error;
992
30
  isl_int_fdiv_r(v1->n, v1->n, v2->n);
993
30
  isl_val_free(v2);
994
30
  return v1;
995
0
error:
996
0
  isl_val_free(v1);
997
0
  isl_val_free(v2);
998
0
  return NULL;
999
30
}
1000
1001
/* Given two integer values "v1" and "v2", return the residue of "v1"
1002
 * modulo "v2".
1003
 *
1004
 * This is a private copy of isl_val_mod for use in the generic
1005
 * isl_multi_*_mod_multi_val instantiated for isl_val.
1006
 */
1007
__isl_give isl_val *isl_val_mod_val(__isl_take isl_val *v1,
1008
  __isl_take isl_val *v2)
1009
0
{
1010
0
  return isl_val_mod(v1, v2);
1011
0
}
1012
1013
/* Given two integer values, return their greatest common divisor.
1014
 */
1015
__isl_give isl_val *isl_val_gcd(__isl_take isl_val *v1, __isl_take isl_val *v2)
1016
5.45k
{
1017
5.45k
  if (!v1 || !v2)
1018
0
    goto error;
1019
5.45k
  if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
1020
5.45k
    
isl_die0
(isl_val_get_ctx(v1), isl_error_invalid,
1021
5.45k
      "expecting two integers", goto error);
1022
5.45k
  if (isl_val_eq(v1, v2)) {
1023
75
    isl_val_free(v2);
1024
75
    return v1;
1025
75
  }
1026
5.38k
  if (isl_val_is_one(v1)) {
1027
0
    isl_val_free(v2);
1028
0
    return v1;
1029
0
  }
1030
5.38k
  if (isl_val_is_one(v2)) {
1031
5.35k
    isl_val_free(v1);
1032
5.35k
    return v2;
1033
5.35k
  }
1034
30
  v1 = isl_val_cow(v1);
1035
30
  if (!v1)
1036
0
    goto error;
1037
30
  isl_int_gcd(v1->n, v1->n, v2->n);
1038
30
  isl_val_free(v2);
1039
30
  return v1;
1040
0
error:
1041
0
  isl_val_free(v1);
1042
0
  isl_val_free(v2);
1043
0
  return NULL;
1044
30
}
1045
1046
/* Compute x, y and g such that g = gcd(a,b) and a*x+b*y = g.
1047
 */
1048
static void isl_int_gcdext(isl_int *g, isl_int *x, isl_int *y,
1049
  isl_int a, isl_int b)
1050
14
{
1051
14
  isl_int d, tmp;
1052
14
  isl_int a_copy, b_copy;
1053
14
1054
14
  isl_int_init(a_copy);
1055
14
  isl_int_init(b_copy);
1056
14
  isl_int_init(d);
1057
14
  isl_int_init(tmp);
1058
14
  isl_int_set(a_copy, a);
1059
14
  isl_int_set(b_copy, b);
1060
14
  isl_int_abs(*g, a_copy);
1061
14
  isl_int_abs(d, b_copy);
1062
14
  isl_int_set_si(*x, 1);
1063
14
  isl_int_set_si(*y, 0);
1064
42
  while (isl_int_is_pos(d)) {
1065
28
    isl_int_fdiv_q(tmp, *g, d);
1066
28
    isl_int_submul(*x, tmp, *y);
1067
28
    isl_int_submul(*g, tmp, d);
1068
28
    isl_int_swap(*g, d);
1069
28
    isl_int_swap(*x, *y);
1070
28
  }
1071
14
  if (isl_int_is_zero(a_copy))
1072
14
    
isl_int_set_si0
(*x, 0);
1073
14
  else if (isl_int_is_neg(a_copy))
1074
14
    isl_int_neg(*x, *x);
1075
14
  if (isl_int_is_zero(b_copy))
1076
14
    
isl_int_set_si0
(*y, 0);
1077
14
  else {
1078
14
    isl_int_mul(tmp, a_copy, *x);
1079
14
    isl_int_sub(tmp, *g, tmp);
1080
14
    isl_int_divexact(*y, tmp, b_copy);
1081
14
  }
1082
14
  isl_int_clear(d);
1083
14
  isl_int_clear(tmp);
1084
14
  isl_int_clear(a_copy);
1085
14
  isl_int_clear(b_copy);
1086
14
}
1087
1088
/* Given two integer values v1 and v2, return their greatest common divisor g,
1089
 * as well as two integers x and y such that x * v1 + y * v2 = g.
1090
 */
1091
__isl_give isl_val *isl_val_gcdext(__isl_take isl_val *v1,
1092
  __isl_take isl_val *v2, __isl_give isl_val **x, __isl_give isl_val **y)
1093
14
{
1094
14
  isl_ctx *ctx;
1095
14
  isl_val *a = NULL, *b = NULL;
1096
14
1097
14
  if (!x && 
!y0
)
1098
0
    return isl_val_gcd(v1, v2);
1099
14
1100
14
  if (!v1 || !v2)
1101
0
    goto error;
1102
14
1103
14
  ctx = isl_val_get_ctx(v1);
1104
14
  if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
1105
14
    
isl_die0
(ctx, isl_error_invalid,
1106
14
      "expecting two integers", goto error);
1107
14
1108
14
  v1 = isl_val_cow(v1);
1109
14
  a = isl_val_alloc(ctx);
1110
14
  b = isl_val_alloc(ctx);
1111
14
  if (!v1 || !a || !b)
1112
0
    goto error;
1113
14
  isl_int_gcdext(&v1->n, &a->n, &b->n, v1->n, v2->n);
1114
14
  if (x) {
1115
14
    isl_int_set_si(a->d, 1);
1116
14
    *x = a;
1117
14
  } else
1118
0
    isl_val_free(a);
1119
14
  if (y) {
1120
14
    isl_int_set_si(b->d, 1);
1121
14
    *y = b;
1122
14
  } else
1123
0
    isl_val_free(b);
1124
14
  isl_val_free(v2);
1125
14
  return v1;
1126
0
error:
1127
0
  isl_val_free(v1);
1128
0
  isl_val_free(v2);
1129
0
  isl_val_free(a);
1130
0
  isl_val_free(b);
1131
0
  if (x)
1132
0
    *x = NULL;
1133
0
  if (y)
1134
0
    *y = NULL;
1135
0
  return NULL;
1136
14
}
1137
1138
/* Does "v" represent an integer value?
1139
 */
1140
isl_bool isl_val_is_int(__isl_keep isl_val *v)
1141
127k
{
1142
127k
  if (!v)
1143
0
    return isl_bool_error;
1144
127k
1145
127k
  return isl_int_is_one(v->d);
1146
127k
}
1147
1148
/* Does "v" represent a rational value?
1149
 */
1150
isl_bool isl_val_is_rat(__isl_keep isl_val *v)
1151
56.7k
{
1152
56.7k
  if (!v)
1153
0
    return isl_bool_error;
1154
56.7k
1155
56.7k
  return !isl_int_is_zero(v->d);
1156
56.7k
}
1157
1158
/* Does "v" represent NaN?
1159
 */
1160
isl_bool isl_val_is_nan(__isl_keep isl_val *v)
1161
92.8k
{
1162
92.8k
  if (!v)
1163
0
    return isl_bool_error;
1164
92.8k
1165
92.8k
  return isl_int_is_zero(v->n) && 
isl_int_is_zero19.3k
(v->d);
1166
92.8k
}
1167
1168
/* Does "v" represent +infinity?
1169
 */
1170
isl_bool isl_val_is_infty(__isl_keep isl_val *v)
1171
15.3k
{
1172
15.3k
  if (!v)
1173
0
    return isl_bool_error;
1174
15.3k
1175
15.3k
  return isl_int_is_pos(v->n) && 
isl_int_is_zero13.7k
(v->d);
1176
15.3k
}
1177
1178
/* Does "v" represent -infinity?
1179
 */
1180
isl_bool isl_val_is_neginfty(__isl_keep isl_val *v)
1181
13.0k
{
1182
13.0k
  if (!v)
1183
0
    return isl_bool_error;
1184
13.0k
1185
13.0k
  return isl_int_is_neg(v->n) && 
isl_int_is_zero206
(v->d);
1186
13.0k
}
1187
1188
/* Does "v" represent the integer zero?
1189
 */
1190
isl_bool isl_val_is_zero(__isl_keep isl_val *v)
1191
115k
{
1192
115k
  if (!v)
1193
0
    return isl_bool_error;
1194
115k
1195
115k
  return isl_int_is_zero(v->n) && 
!75.7k
isl_int_is_zero75.7k
(v->d);
1196
115k
}
1197
1198
/* Does "v" represent the integer one?
1199
 */
1200
isl_bool isl_val_is_one(__isl_keep isl_val *v)
1201
50.3k
{
1202
50.3k
  if (!v)
1203
0
    return isl_bool_error;
1204
50.3k
1205
50.3k
  if (isl_val_is_nan(v))
1206
0
    return isl_bool_false;
1207
50.3k
1208
50.3k
  return isl_int_eq(v->n, v->d);
1209
50.3k
}
1210
1211
/* Does "v" represent the integer negative one?
1212
 */
1213
isl_bool isl_val_is_negone(__isl_keep isl_val *v)
1214
5.33k
{
1215
5.33k
  if (!v)
1216
0
    return isl_bool_error;
1217
5.33k
1218
5.33k
  return isl_int_is_neg(v->n) && 
isl_int_abs_eq175
(v->n, v->d);
1219
5.33k
}
1220
1221
/* Is "v" (strictly) positive?
1222
 */
1223
isl_bool isl_val_is_pos(__isl_keep isl_val *v)
1224
1.99k
{
1225
1.99k
  if (!v)
1226
0
    return isl_bool_error;
1227
1.99k
1228
1.99k
  return isl_int_is_pos(v->n);
1229
1.99k
}
1230
1231
/* Is "v" (strictly) negative?
1232
 */
1233
isl_bool isl_val_is_neg(__isl_keep isl_val *v)
1234
24.0k
{
1235
24.0k
  if (!v)
1236
0
    return isl_bool_error;
1237
24.0k
1238
24.0k
  return isl_int_is_neg(v->n);
1239
24.0k
}
1240
1241
/* Is "v" non-negative?
1242
 */
1243
isl_bool isl_val_is_nonneg(__isl_keep isl_val *v)
1244
1.08k
{
1245
1.08k
  if (!v)
1246
0
    return isl_bool_error;
1247
1.08k
1248
1.08k
  if (isl_val_is_nan(v))
1249
0
    return isl_bool_false;
1250
1.08k
1251
1.08k
  return isl_int_is_nonneg(v->n);
1252
1.08k
}
1253
1254
/* Is "v" non-positive?
1255
 */
1256
isl_bool isl_val_is_nonpos(__isl_keep isl_val *v)
1257
0
{
1258
0
  if (!v)
1259
0
    return isl_bool_error;
1260
0
1261
0
  if (isl_val_is_nan(v))
1262
0
    return isl_bool_false;
1263
0
1264
0
  return isl_int_is_nonpos(v->n);
1265
0
}
1266
1267
/* Return the sign of "v".
1268
 *
1269
 * The sign of NaN is undefined.
1270
 */
1271
int isl_val_sgn(__isl_keep isl_val *v)
1272
4.19k
{
1273
4.19k
  if (!v)
1274
0
    return 0;
1275
4.19k
  if (isl_val_is_zero(v))
1276
2.62k
    return 0;
1277
1.56k
  if (isl_val_is_pos(v))
1278
889
    return 1;
1279
675
  return -1;
1280
675
}
1281
1282
/* Is "v1" (strictly) less than "v2"?
1283
 */
1284
isl_bool isl_val_lt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1285
46
{
1286
46
  isl_int t;
1287
46
  isl_bool lt;
1288
46
1289
46
  if (!v1 || !v2)
1290
0
    return isl_bool_error;
1291
46
  if (isl_val_is_int(v1) && isl_val_is_int(v2))
1292
46
    return isl_int_lt(v1->n, v2->n);
1293
0
  if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1294
0
    return isl_bool_false;
1295
0
  if (isl_val_eq(v1, v2))
1296
0
    return isl_bool_false;
1297
0
  if (isl_val_is_infty(v2))
1298
0
    return isl_bool_true;
1299
0
  if (isl_val_is_infty(v1))
1300
0
    return isl_bool_false;
1301
0
  if (isl_val_is_neginfty(v1))
1302
0
    return isl_bool_true;
1303
0
  if (isl_val_is_neginfty(v2))
1304
0
    return isl_bool_false;
1305
0
1306
0
  isl_int_init(t);
1307
0
  isl_int_mul(t, v1->n, v2->d);
1308
0
  isl_int_submul(t, v2->n, v1->d);
1309
0
  lt = isl_int_is_neg(t);
1310
0
  isl_int_clear(t);
1311
0
1312
0
  return lt;
1313
0
}
1314
1315
/* Is "v1" (strictly) greater than "v2"?
1316
 */
1317
isl_bool isl_val_gt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1318
0
{
1319
0
  return isl_val_lt(v2, v1);
1320
0
}
1321
1322
/* Is "v" (strictly) greater than "i"?
1323
 */
1324
isl_bool isl_val_gt_si(__isl_keep isl_val *v, long i)
1325
5.36k
{
1326
5.36k
  isl_val *vi;
1327
5.36k
  isl_bool res;
1328
5.36k
1329
5.36k
  if (!v)
1330
0
    return isl_bool_error;
1331
5.36k
  if (isl_val_is_int(v))
1332
5.36k
    return isl_int_cmp_si(v->n, i) > 0;
1333
0
  if (isl_val_is_nan(v))
1334
0
    return isl_bool_false;
1335
0
  if (isl_val_is_infty(v))
1336
0
    return isl_bool_true;
1337
0
  if (isl_val_is_neginfty(v))
1338
0
    return isl_bool_false;
1339
0
1340
0
  vi = isl_val_int_from_si(isl_val_get_ctx(v), i);
1341
0
  res = isl_val_gt(v, vi);
1342
0
  isl_val_free(vi);
1343
0
1344
0
  return res;
1345
0
}
1346
1347
/* Is "v1" less than or equal to "v2"?
1348
 */
1349
isl_bool isl_val_le(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1350
10
{
1351
10
  isl_int t;
1352
10
  isl_bool le;
1353
10
1354
10
  if (!v1 || !v2)
1355
0
    return isl_bool_error;
1356
10
  if (isl_val_is_int(v1) && 
isl_val_is_int(v2)6
)
1357
4
    return isl_int_le(v1->n, v2->n);
1358
6
  if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1359
0
    return isl_bool_false;
1360
6
  if (isl_val_eq(v1, v2))
1361
0
    return isl_bool_true;
1362
6
  if (isl_val_is_infty(v2))
1363
2
    return isl_bool_true;
1364
4
  if (isl_val_is_infty(v1))
1365
2
    return isl_bool_false;
1366
2
  if (isl_val_is_neginfty(v1))
1367
1
    return isl_bool_true;
1368
1
  if (isl_val_is_neginfty(v2))
1369
1
    return isl_bool_false;
1370
0
1371
0
  isl_int_init(t);
1372
0
  isl_int_mul(t, v1->n, v2->d);
1373
0
  isl_int_submul(t, v2->n, v1->d);
1374
0
  le = isl_int_is_nonpos(t);
1375
0
  isl_int_clear(t);
1376
0
1377
0
  return le;
1378
0
}
1379
1380
/* Is "v1" greater than or equal to "v2"?
1381
 */
1382
isl_bool isl_val_ge(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1383
5
{
1384
5
  return isl_val_le(v2, v1);
1385
5
}
1386
1387
/* How does "v" compare to "i"?
1388
 *
1389
 * Return 1 if v is greater, -1 if v is smaller and 0 if v is equal to i.
1390
 *
1391
 * If v is NaN (or NULL), then the result is undefined.
1392
 */
1393
int isl_val_cmp_si(__isl_keep isl_val *v, long i)
1394
2.45k
{
1395
2.45k
  isl_int t;
1396
2.45k
  int cmp;
1397
2.45k
1398
2.45k
  if (!v)
1399
0
    return 0;
1400
2.45k
  if (isl_val_is_int(v))
1401
2.45k
    return isl_int_cmp_si(v->n, i);
1402
0
  if (isl_val_is_nan(v))
1403
0
    return 0;
1404
0
  if (isl_val_is_infty(v))
1405
0
    return 1;
1406
0
  if (isl_val_is_neginfty(v))
1407
0
    return -1;
1408
0
1409
0
  isl_int_init(t);
1410
0
  isl_int_mul_si(t, v->d, i);
1411
0
  isl_int_sub(t, v->n, t);
1412
0
  cmp = isl_int_sgn(t);
1413
0
  isl_int_clear(t);
1414
0
1415
0
  return cmp;
1416
0
}
1417
1418
/* Is "v1" equal to "v2"?
1419
 */
1420
isl_bool isl_val_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1421
5.58k
{
1422
5.58k
  if (!v1 || !v2)
1423
0
    return isl_bool_error;
1424
5.58k
  if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1425
0
    return isl_bool_false;
1426
5.58k
1427
5.58k
  return isl_int_eq(v1->n, v2->n) && 
isl_int_eq193
(v1->d, v2->d);
1428
5.58k
}
1429
1430
/* Is "v1" equal to "v2" in absolute value?
1431
 */
1432
isl_bool isl_val_abs_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1433
17
{
1434
17
  if (!v1 || !v2)
1435
0
    return isl_bool_error;
1436
17
  if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1437
0
    return isl_bool_false;
1438
17
1439
17
  return isl_int_abs_eq(v1->n, v2->n) && isl_int_eq(v1->d, v2->d);
1440
17
}
1441
1442
/* Is "v1" different from "v2"?
1443
 */
1444
isl_bool isl_val_ne(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1445
5
{
1446
5
  if (!v1 || !v2)
1447
0
    return isl_bool_error;
1448
5
  if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1449
0
    return isl_bool_false;
1450
5
1451
5
  return isl_int_ne(v1->n, v2->n) || 
isl_int_ne1
(v1->d, v2->d);
1452
5
}
1453
1454
/* Print a textual representation of "v" onto "p".
1455
 */
1456
__isl_give isl_printer *isl_printer_print_val(__isl_take isl_printer *p,
1457
  __isl_keep isl_val *v)
1458
9.57k
{
1459
9.57k
  int neg;
1460
9.57k
1461
9.57k
  if (!p || !v)
1462
0
    return isl_printer_free(p);
1463
9.57k
1464
9.57k
  neg = isl_int_is_neg(v->n);
1465
9.57k
  if (neg) {
1466
68
    p = isl_printer_print_str(p, "-");
1467
68
    isl_int_neg(v->n, v->n);
1468
68
  }
1469
9.57k
  if (isl_int_is_zero(v->d)) {
1470
0
    int sgn = isl_int_sgn(v->n);
1471
0
    p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1472
0
              sgn == 0 ? "NaN" : "infty");
1473
0
  } else
1474
9.57k
    p = isl_printer_print_isl_int(p, v->n);
1475
9.57k
  if (neg)
1476
9.57k
    
isl_int_neg68
(v->n, v->n);
1477
9.57k
  if (!isl_int_is_zero(v->d) && !isl_int_is_one(v->d)) {
1478
0
    p = isl_printer_print_str(p, "/");
1479
0
    p = isl_printer_print_isl_int(p, v->d);
1480
0
  }
1481
9.57k
1482
9.57k
  return p;
1483
9.57k
}
1484
1485
/* Is "val1" (obviously) equal to "val2"?
1486
 *
1487
 * This is a private copy of isl_val_eq for use in the generic
1488
 * isl_multi_*_plain_is_equal instantiated for isl_val.
1489
 */
1490
int isl_val_plain_is_equal(__isl_keep isl_val *val1, __isl_keep isl_val *val2)
1491
0
{
1492
0
  return isl_val_eq(val1, val2);
1493
0
}
1494
1495
/* Does "v" have any non-zero coefficients
1496
 * for any dimension in the given range?
1497
 *
1498
 * This function is only meant to be used in the generic isl_multi_*
1499
 * functions which have to deal with base objects that have an associated
1500
 * space.  Since an isl_val does not have any coefficients, this function
1501
 * always returns isl_bool_false.
1502
 */
1503
isl_bool isl_val_involves_dims(__isl_keep isl_val *v, enum isl_dim_type type,
1504
  unsigned first, unsigned n)
1505
0
{
1506
0
  if (!v)
1507
0
    return isl_bool_error;
1508
0
1509
0
  return isl_bool_false;
1510
0
}
1511
1512
/* Insert "n" dimensions of type "type" at position "first".
1513
 *
1514
 * This function is only meant to be used in the generic isl_multi_*
1515
 * functions which have to deal with base objects that have an associated
1516
 * space.  Since an isl_val does not have an associated space, this function
1517
 * does not do anything.
1518
 */
1519
__isl_give isl_val *isl_val_insert_dims(__isl_take isl_val *v,
1520
  enum isl_dim_type type, unsigned first, unsigned n)
1521
0
{
1522
0
  return v;
1523
0
}
1524
1525
/* Drop the "n" first dimensions of type "type" at position "first".
1526
 *
1527
 * This function is only meant to be used in the generic isl_multi_*
1528
 * functions which have to deal with base objects that have an associated
1529
 * space.  Since an isl_val does not have an associated space, this function
1530
 * does not do anything.
1531
 */
1532
__isl_give isl_val *isl_val_drop_dims(__isl_take isl_val *v,
1533
  enum isl_dim_type type, unsigned first, unsigned n)
1534
0
{
1535
0
  return v;
1536
0
}
1537
1538
/* Change the name of the dimension of type "type" at position "pos" to "s".
1539
 *
1540
 * This function is only meant to be used in the generic isl_multi_*
1541
 * functions which have to deal with base objects that have an associated
1542
 * space.  Since an isl_val does not have an associated space, this function
1543
 * does not do anything.
1544
 */
1545
__isl_give isl_val *isl_val_set_dim_name(__isl_take isl_val *v,
1546
  enum isl_dim_type type, unsigned pos, const char *s)
1547
0
{
1548
0
  return v;
1549
0
}
1550
1551
/* Return the space of "v".
1552
 *
1553
 * This function is only meant to be used in the generic isl_multi_*
1554
 * functions which have to deal with base objects that have an associated
1555
 * space.  The conditions surrounding the call to this function make sure
1556
 * that this function will never actually get called.  We return a valid
1557
 * space anyway, just in case.
1558
 */
1559
__isl_give isl_space *isl_val_get_space(__isl_keep isl_val *v)
1560
17
{
1561
17
  if (!v)
1562
0
    return NULL;
1563
17
1564
17
  return isl_space_params_alloc(isl_val_get_ctx(v), 0);
1565
17
}
1566
1567
/* Reset the domain space of "v" to "space".
1568
 *
1569
 * This function is only meant to be used in the generic isl_multi_*
1570
 * functions which have to deal with base objects that have an associated
1571
 * space.  Since an isl_val does not have an associated space, this function
1572
 * does not do anything, apart from error handling and cleaning up memory.
1573
 */
1574
__isl_give isl_val *isl_val_reset_domain_space(__isl_take isl_val *v,
1575
  __isl_take isl_space *space)
1576
3.04k
{
1577
3.04k
  if (!space)
1578
0
    return isl_val_free(v);
1579
3.04k
  isl_space_free(space);
1580
3.04k
  return v;
1581
3.04k
}
1582
1583
/* Align the parameters of "v" to those of "space".
1584
 *
1585
 * This function is only meant to be used in the generic isl_multi_*
1586
 * functions which have to deal with base objects that have an associated
1587
 * space.  Since an isl_val does not have an associated space, this function
1588
 * does not do anything, apart from error handling and cleaning up memory.
1589
 * Note that the conditions surrounding the call to this function make sure
1590
 * that this function will never actually get called.
1591
 */
1592
__isl_give isl_val *isl_val_align_params(__isl_take isl_val *v,
1593
  __isl_take isl_space *space)
1594
17
{
1595
17
  if (!space)
1596
0
    return isl_val_free(v);
1597
17
  isl_space_free(space);
1598
17
  return v;
1599
17
}
1600
1601
/* Reorder the dimensions of the domain of "v" according
1602
 * to the given reordering.
1603
 *
1604
 * This function is only meant to be used in the generic isl_multi_*
1605
 * functions which have to deal with base objects that have an associated
1606
 * space.  Since an isl_val does not have an associated space, this function
1607
 * does not do anything, apart from error handling and cleaning up memory.
1608
 */
1609
__isl_give isl_val *isl_val_realign_domain(__isl_take isl_val *v,
1610
  __isl_take isl_reordering *r)
1611
3.04k
{
1612
3.04k
  if (!r)
1613
0
    return isl_val_free(v);
1614
3.04k
  isl_reordering_free(r);
1615
3.04k
  return v;
1616
3.04k
}
1617
1618
/* Return an isl_val that is zero on "ls".
1619
 *
1620
 * This function is only meant to be used in the generic isl_multi_*
1621
 * functions which have to deal with base objects that have an associated
1622
 * space.  Since an isl_val does not have an associated space, this function
1623
 * simply returns a zero isl_val in the same context as "ls".
1624
 */
1625
__isl_give isl_val *isl_val_zero_on_domain(__isl_take isl_local_space *ls)
1626
3.47k
{
1627
3.47k
  isl_ctx *ctx;
1628
3.47k
1629
3.47k
  if (!ls)
1630
0
    return NULL;
1631
3.47k
  ctx = isl_local_space_get_ctx(ls);
1632
3.47k
  isl_local_space_free(ls);
1633
3.47k
  return isl_val_zero(ctx);
1634
3.47k
}
1635
1636
/* Do the parameters of "v" match those of "space"?
1637
 *
1638
 * This function is only meant to be used in the generic isl_multi_*
1639
 * functions which have to deal with base objects that have an associated
1640
 * space.  Since an isl_val does not have an associated space, this function
1641
 * simply returns true, except if "v" or "space" are NULL.
1642
 */
1643
isl_bool isl_val_matching_params(__isl_keep isl_val *v,
1644
  __isl_keep isl_space *space)
1645
14.2k
{
1646
14.2k
  if (!v || !space)
1647
0
    return isl_bool_error;
1648
14.2k
  return isl_bool_true;
1649
14.2k
}
1650
1651
/* Check that the domain space of "v" matches "space".
1652
 *
1653
 * This function is only meant to be used in the generic isl_multi_*
1654
 * functions which have to deal with base objects that have an associated
1655
 * space.  Since an isl_val does not have an associated space, this function
1656
 * simply returns 0, except if "v" or "space" are NULL.
1657
 */
1658
isl_stat isl_val_check_match_domain_space(__isl_keep isl_val *v,
1659
  __isl_keep isl_space *space)
1660
14.2k
{
1661
14.2k
  if (!v || !space)
1662
0
    return isl_stat_error;
1663
14.2k
  return isl_stat_ok;
1664
14.2k
}
1665
1666
0
#define isl_val_involves_nan isl_val_is_nan
1667
1668
#undef BASE
1669
#define BASE val
1670
1671
#define NO_DOMAIN
1672
#define NO_IDENTITY
1673
#define NO_FROM_BASE
1674
#define NO_MOVE_DIMS
1675
#include <isl_multi_no_explicit_domain.c>
1676
#include <isl_multi_templ.c>
1677
#include <isl_multi_dims.c>
1678
1679
/* Apply "fn" to each of the elements of "mv" with as second argument "v".
1680
 */
1681
static __isl_give isl_multi_val *isl_multi_val_fn_val(
1682
  __isl_take isl_multi_val *mv,
1683
  __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
1684
          __isl_take isl_val *v2),
1685
  __isl_take isl_val *v)
1686
4
{
1687
4
  int i;
1688
4
1689
4
  mv = isl_multi_val_cow(mv);
1690
4
  if (!mv || !v)
1691
0
    goto error;
1692
4
1693
16
  
for (i = 0; 4
i < mv->n;
++i12
) {
1694
12
    mv->u.p[i] = fn(mv->u.p[i], isl_val_copy(v));
1695
12
    if (!mv->u.p[i])
1696
0
      goto error;
1697
12
  }
1698
4
1699
4
  isl_val_free(v);
1700
4
  return mv;
1701
0
error:
1702
0
  isl_val_free(v);
1703
0
  isl_multi_val_free(mv);
1704
0
  return NULL;
1705
4
}
1706
1707
/* Add "v" to each of the elements of "mv".
1708
 */
1709
__isl_give isl_multi_val *isl_multi_val_add_val(__isl_take isl_multi_val *mv,
1710
  __isl_take isl_val *v)
1711
3
{
1712
3
  if (!v)
1713
0
    return isl_multi_val_free(mv);
1714
3
  if (isl_val_is_zero(v)) {
1715
2
    isl_val_free(v);
1716
2
    return mv;
1717
2
  }
1718
1
  return isl_multi_val_fn_val(mv, &isl_val_add, v);
1719
1
}
1720
1721
/* Reduce the elements of "mv" modulo "v".
1722
 */
1723
__isl_give isl_multi_val *isl_multi_val_mod_val(__isl_take isl_multi_val *mv,
1724
  __isl_take isl_val *v)
1725
3
{
1726
3
  return isl_multi_val_fn_val(mv, &isl_val_mod, v);
1727
3
}