Coverage Report

Created: 2017-11-21 03:47

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