Bug Summary

File:tools/polly/lib/External/isl/isl_val.c
Warning:line 1632, column 27
Use of memory after it is freed

Annotated Source Code

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