Bug Summary

File:polly/lib/External/isl/isl_int_sioimath.h
Warning:line 333, column 28
Dereference of null pointer (loaded from variable 'ptr')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name isl_polynomial.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/build-llvm/tools/polly/lib/External -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/build-llvm/tools/polly/lib/External -I /build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External -I /build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl -I /build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/include -I /build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/imath -I /build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/build-llvm/tools/polly/lib/External/isl -I /build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/build-llvm/tools/polly/include -I /build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/pet/include -I /build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/build-llvm/tools/polly/lib/External/isl/include -I /build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/include -I /build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/build-llvm/include -I /build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=gnu99 -fconst-strings -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/build-llvm/tools/polly/lib/External -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0=. -ferror-limit 19 -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-08-28-193554-24367-1 -x c /build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c

/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c

1/*
2 * Copyright 2010 INRIA Saclay
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
10
11#include <stdlib.h>
12#include <isl_ctx_private.h>
13#include <isl_map_private.h>
14#include <isl_factorization.h>
15#include <isl_lp_private.h>
16#include <isl_seq.h>
17#include <isl_union_map_private.h>
18#include <isl_constraint_private.h>
19#include <isl_polynomial_private.h>
20#include <isl_point_private.h>
21#include <isl_space_private.h>
22#include <isl_mat_private.h>
23#include <isl_vec_private.h>
24#include <isl_range.h>
25#include <isl_local.h>
26#include <isl_local_space_private.h>
27#include <isl_aff_private.h>
28#include <isl_val_private.h>
29#include <isl_config.h>
30
31#undef EL_BASEpw_qpolynomial
32#define EL_BASEpw_qpolynomial qpolynomial
33
34#include <isl_list_templ.c>
35
36#undef EL_BASEpw_qpolynomial
37#define EL_BASEpw_qpolynomial pw_qpolynomial
38
39#include <isl_list_templ.c>
40
41static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
42{
43 switch (type) {
44 case isl_dim_param: return 0;
45 case isl_dim_in: return space->nparam;
46 case isl_dim_out: return space->nparam + space->n_in;
47 default: return 0;
48 }
49}
50
51isl_bool isl_poly_is_cst(__isl_keep isl_poly *poly)
52{
53 if (!poly)
54 return isl_bool_error;
55
56 return isl_bool_ok(poly->var < 0);
57}
58
59__isl_keep isl_poly_cst *isl_poly_as_cst(__isl_keep isl_poly *poly)
60{
61 if (!poly)
62 return NULL((void*)0);
63
64 isl_assert(poly->ctx, poly->var < 0, return NULL)do { if (poly->var < 0) break; do { isl_handle_error(poly
->ctx, isl_error_unknown, "Assertion \"" "poly->var < 0"
"\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 64); return ((void*)0); } while (0); } while (0)
;
65
66 return (isl_poly_cst *) poly;
67}
68
69__isl_keep isl_poly_rec *isl_poly_as_rec(__isl_keep isl_poly *poly)
70{
71 if (!poly)
72 return NULL((void*)0);
73
74 isl_assert(poly->ctx, poly->var >= 0, return NULL)do { if (poly->var >= 0) break; do { isl_handle_error(poly
->ctx, isl_error_unknown, "Assertion \"" "poly->var >= 0"
"\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 74); return ((void*)0); } while (0); } while (0)
;
75
76 return (isl_poly_rec *) poly;
77}
78
79/* Compare two polynomials.
80 *
81 * Return -1 if "poly1" is "smaller" than "poly2", 1 if "poly1" is "greater"
82 * than "poly2" and 0 if they are equal.
83 */
84static int isl_poly_plain_cmp(__isl_keep isl_poly *poly1,
85 __isl_keep isl_poly *poly2)
86{
87 int i;
88 isl_bool is_cst1;
89 isl_poly_rec *rec1, *rec2;
90
91 if (poly1 == poly2)
92 return 0;
93 is_cst1 = isl_poly_is_cst(poly1);
94 if (is_cst1 < 0)
95 return -1;
96 if (!poly2)
97 return 1;
98 if (poly1->var != poly2->var)
99 return poly1->var - poly2->var;
100
101 if (is_cst1) {
102 isl_poly_cst *cst1, *cst2;
103 int cmp;
104
105 cst1 = isl_poly_as_cst(poly1);
106 cst2 = isl_poly_as_cst(poly2);
107 if (!cst1 || !cst2)
108 return 0;
109 cmp = isl_int_cmp(cst1->n, cst2->n)isl_sioimath_cmp(*(cst1->n), *(cst2->n));
110 if (cmp != 0)
111 return cmp;
112 return isl_int_cmp(cst1->d, cst2->d)isl_sioimath_cmp(*(cst1->d), *(cst2->d));
113 }
114
115 rec1 = isl_poly_as_rec(poly1);
116 rec2 = isl_poly_as_rec(poly2);
117 if (!rec1 || !rec2)
118 return 0;
119
120 if (rec1->n != rec2->n)
121 return rec1->n - rec2->n;
122
123 for (i = 0; i < rec1->n; ++i) {
124 int cmp = isl_poly_plain_cmp(rec1->p[i], rec2->p[i]);
125 if (cmp != 0)
126 return cmp;
127 }
128
129 return 0;
130}
131
132isl_bool isl_poly_is_equal(__isl_keep isl_poly *poly1,
133 __isl_keep isl_poly *poly2)
134{
135 int i;
136 isl_bool is_cst1;
137 isl_poly_rec *rec1, *rec2;
138
139 is_cst1 = isl_poly_is_cst(poly1);
140 if (is_cst1 < 0 || !poly2)
141 return isl_bool_error;
142 if (poly1 == poly2)
143 return isl_bool_true;
144 if (poly1->var != poly2->var)
145 return isl_bool_false;
146 if (is_cst1) {
147 isl_poly_cst *cst1, *cst2;
148 int r;
149 cst1 = isl_poly_as_cst(poly1);
150 cst2 = isl_poly_as_cst(poly2);
151 if (!cst1 || !cst2)
152 return isl_bool_error;
153 r = isl_int_eq(cst1->n, cst2->n)(isl_sioimath_cmp(*(cst1->n), *(cst2->n)) == 0) &&
154 isl_int_eq(cst1->d, cst2->d)(isl_sioimath_cmp(*(cst1->d), *(cst2->d)) == 0);
155 return isl_bool_ok(r);
156 }
157
158 rec1 = isl_poly_as_rec(poly1);
159 rec2 = isl_poly_as_rec(poly2);
160 if (!rec1 || !rec2)
161 return isl_bool_error;
162
163 if (rec1->n != rec2->n)
164 return isl_bool_false;
165
166 for (i = 0; i < rec1->n; ++i) {
167 isl_bool eq = isl_poly_is_equal(rec1->p[i], rec2->p[i]);
168 if (eq < 0 || !eq)
169 return eq;
170 }
171
172 return isl_bool_true;
173}
174
175isl_bool isl_poly_is_zero(__isl_keep isl_poly *poly)
176{
177 isl_bool is_cst;
178 isl_poly_cst *cst;
179
180 is_cst = isl_poly_is_cst(poly);
181 if (is_cst < 0 || !is_cst)
182 return is_cst;
183
184 cst = isl_poly_as_cst(poly);
185 if (!cst)
186 return isl_bool_error;
187
188 return isl_bool_ok(isl_int_is_zero(cst->n)(isl_sioimath_sgn(*(cst->n)) == 0) && isl_int_is_pos(cst->d)(isl_sioimath_sgn(*(cst->d)) > 0));
189}
190
191int isl_poly_sgn(__isl_keep isl_poly *poly)
192{
193 isl_bool is_cst;
194 isl_poly_cst *cst;
195
196 is_cst = isl_poly_is_cst(poly);
197 if (is_cst < 0 || !is_cst)
198 return 0;
199
200 cst = isl_poly_as_cst(poly);
201 if (!cst)
202 return 0;
203
204 return isl_int_sgn(cst->n)isl_sioimath_sgn(*(cst->n));
205}
206
207isl_bool isl_poly_is_nan(__isl_keep isl_poly *poly)
208{
209 isl_bool is_cst;
210 isl_poly_cst *cst;
211
212 is_cst = isl_poly_is_cst(poly);
213 if (is_cst < 0 || !is_cst)
214 return is_cst;
215
216 cst = isl_poly_as_cst(poly);
217 if (!cst)
218 return isl_bool_error;
219
220 return isl_bool_ok(isl_int_is_zero(cst->n)(isl_sioimath_sgn(*(cst->n)) == 0) && isl_int_is_zero(cst->d)(isl_sioimath_sgn(*(cst->d)) == 0));
221}
222
223isl_bool isl_poly_is_infty(__isl_keep isl_poly *poly)
224{
225 isl_bool is_cst;
226 isl_poly_cst *cst;
227
228 is_cst = isl_poly_is_cst(poly);
229 if (is_cst < 0 || !is_cst)
230 return is_cst;
231
232 cst = isl_poly_as_cst(poly);
233 if (!cst)
234 return isl_bool_error;
235
236 return isl_bool_ok(isl_int_is_pos(cst->n)(isl_sioimath_sgn(*(cst->n)) > 0) && isl_int_is_zero(cst->d)(isl_sioimath_sgn(*(cst->d)) == 0));
237}
238
239isl_bool isl_poly_is_neginfty(__isl_keep isl_poly *poly)
240{
241 isl_bool is_cst;
242 isl_poly_cst *cst;
243
244 is_cst = isl_poly_is_cst(poly);
245 if (is_cst < 0 || !is_cst)
246 return is_cst;
247
248 cst = isl_poly_as_cst(poly);
249 if (!cst)
250 return isl_bool_error;
251
252 return isl_bool_ok(isl_int_is_neg(cst->n)(isl_sioimath_sgn(*(cst->n)) < 0) && isl_int_is_zero(cst->d)(isl_sioimath_sgn(*(cst->d)) == 0));
253}
254
255isl_bool isl_poly_is_one(__isl_keep isl_poly *poly)
256{
257 isl_bool is_cst;
258 isl_poly_cst *cst;
259 int r;
260
261 is_cst = isl_poly_is_cst(poly);
262 if (is_cst < 0 || !is_cst)
263 return is_cst;
264
265 cst = isl_poly_as_cst(poly);
266 if (!cst)
267 return isl_bool_error;
268
269 r = isl_int_eq(cst->n, cst->d)(isl_sioimath_cmp(*(cst->n), *(cst->d)) == 0) && isl_int_is_pos(cst->d)(isl_sioimath_sgn(*(cst->d)) > 0);
270 return isl_bool_ok(r);
271}
272
273isl_bool isl_poly_is_negone(__isl_keep isl_poly *poly)
274{
275 isl_bool is_cst;
276 isl_poly_cst *cst;
277
278 is_cst = isl_poly_is_cst(poly);
279 if (is_cst < 0 || !is_cst)
280 return is_cst;
281
282 cst = isl_poly_as_cst(poly);
283 if (!cst)
284 return isl_bool_error;
285
286 return isl_bool_ok(isl_int_is_negone(cst->n)(isl_sioimath_cmp_si(*(cst->n), -1) == 0) && isl_int_is_one(cst->d)(isl_sioimath_cmp_si(*(cst->d), 1) == 0));
287}
288
289__isl_give isl_poly_cst *isl_poly_cst_alloc(isl_ctx *ctx)
290{
291 isl_poly_cst *cst;
292
293 cst = isl_alloc_type(ctx, struct isl_poly_cst)((struct isl_poly_cst *)isl_malloc_or_die(ctx, sizeof(struct isl_poly_cst
)))
;
294 if (!cst)
295 return NULL((void*)0);
296
297 cst->poly.ref = 1;
298 cst->poly.ctx = ctx;
299 isl_ctx_ref(ctx);
300 cst->poly.var = -1;
301
302 isl_int_init(cst->n)isl_sioimath_init((cst->n));
303 isl_int_init(cst->d)isl_sioimath_init((cst->d));
304
305 return cst;
306}
307
308__isl_give isl_poly *isl_poly_zero(isl_ctx *ctx)
309{
310 isl_poly_cst *cst;
311
312 cst = isl_poly_cst_alloc(ctx);
313 if (!cst)
314 return NULL((void*)0);
315
316 isl_int_set_si(cst->n, 0)isl_sioimath_set_si((cst->n), 0);
317 isl_int_set_si(cst->d, 1)isl_sioimath_set_si((cst->d), 1);
318
319 return &cst->poly;
320}
321
322__isl_give isl_poly *isl_poly_one(isl_ctx *ctx)
323{
324 isl_poly_cst *cst;
325
326 cst = isl_poly_cst_alloc(ctx);
327 if (!cst)
328 return NULL((void*)0);
329
330 isl_int_set_si(cst->n, 1)isl_sioimath_set_si((cst->n), 1);
331 isl_int_set_si(cst->d, 1)isl_sioimath_set_si((cst->d), 1);
332
333 return &cst->poly;
334}
335
336__isl_give isl_poly *isl_poly_infty(isl_ctx *ctx)
337{
338 isl_poly_cst *cst;
339
340 cst = isl_poly_cst_alloc(ctx);
341 if (!cst)
342 return NULL((void*)0);
343
344 isl_int_set_si(cst->n, 1)isl_sioimath_set_si((cst->n), 1);
345 isl_int_set_si(cst->d, 0)isl_sioimath_set_si((cst->d), 0);
346
347 return &cst->poly;
348}
349
350__isl_give isl_poly *isl_poly_neginfty(isl_ctx *ctx)
351{
352 isl_poly_cst *cst;
353
354 cst = isl_poly_cst_alloc(ctx);
355 if (!cst)
356 return NULL((void*)0);
357
358 isl_int_set_si(cst->n, -1)isl_sioimath_set_si((cst->n), -1);
359 isl_int_set_si(cst->d, 0)isl_sioimath_set_si((cst->d), 0);
360
361 return &cst->poly;
362}
363
364__isl_give isl_poly *isl_poly_nan(isl_ctx *ctx)
365{
366 isl_poly_cst *cst;
367
368 cst = isl_poly_cst_alloc(ctx);
369 if (!cst)
370 return NULL((void*)0);
371
372 isl_int_set_si(cst->n, 0)isl_sioimath_set_si((cst->n), 0);
373 isl_int_set_si(cst->d, 0)isl_sioimath_set_si((cst->d), 0);
374
375 return &cst->poly;
376}
377
378__isl_give isl_poly *isl_poly_rat_cst(isl_ctx *ctx, isl_int n, isl_int d)
379{
380 isl_poly_cst *cst;
381
382 cst = isl_poly_cst_alloc(ctx);
383 if (!cst)
384 return NULL((void*)0);
385
386 isl_int_set(cst->n, n)isl_sioimath_set((cst->n), *(n));
387 isl_int_set(cst->d, d)isl_sioimath_set((cst->d), *(d));
388
389 return &cst->poly;
390}
391
392__isl_give isl_poly_rec *isl_poly_alloc_rec(isl_ctx *ctx, int var, int size)
393{
394 isl_poly_rec *rec;
395
396 isl_assert(ctx, var >= 0, return NULL)do { if (var >= 0) break; do { isl_handle_error(ctx, isl_error_unknown
, "Assertion \"" "var >= 0" "\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 396); return ((void*)0); } while (0); } while (0)
;
397 isl_assert(ctx, size >= 0, return NULL)do { if (size >= 0) break; do { isl_handle_error(ctx, isl_error_unknown
, "Assertion \"" "size >= 0" "\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 397); return ((void*)0); } while (0); } while (0)
;
398 rec = isl_calloc(ctx, struct isl_poly_rec,((struct isl_poly_rec *)isl_calloc_or_die(ctx, 1, sizeof(struct
isl_poly_rec) + size * sizeof(struct isl_poly *)))
399 sizeof(struct isl_poly_rec) +((struct isl_poly_rec *)isl_calloc_or_die(ctx, 1, sizeof(struct
isl_poly_rec) + size * sizeof(struct isl_poly *)))
400 size * sizeof(struct isl_poly *))((struct isl_poly_rec *)isl_calloc_or_die(ctx, 1, sizeof(struct
isl_poly_rec) + size * sizeof(struct isl_poly *)))
;
401 if (!rec)
402 return NULL((void*)0);
403
404 rec->poly.ref = 1;
405 rec->poly.ctx = ctx;
406 isl_ctx_ref(ctx);
407 rec->poly.var = var;
408
409 rec->n = 0;
410 rec->size = size;
411
412 return rec;
413}
414
415__isl_give isl_qpolynomial *isl_qpolynomial_reset_domain_space(
416 __isl_take isl_qpolynomial *qp, __isl_take isl_space *space)
417{
418 qp = isl_qpolynomial_cow(qp);
419 if (!qp || !space)
420 goto error;
421
422 isl_space_free(qp->dim);
423 qp->dim = space;
424
425 return qp;
426error:
427 isl_qpolynomial_free(qp);
428 isl_space_free(space);
429 return NULL((void*)0);
430}
431
432/* Reset the space of "qp". This function is called from isl_pw_templ.c
433 * and doesn't know if the space of an element object is represented
434 * directly or through its domain. It therefore passes along both.
435 */
436__isl_give isl_qpolynomial *isl_qpolynomial_reset_space_and_domain(
437 __isl_take isl_qpolynomial *qp, __isl_take isl_space *space,
438 __isl_take isl_space *domain)
439{
440 isl_space_free(space);
441 return isl_qpolynomial_reset_domain_space(qp, domain);
442}
443
444isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
445{
446 return qp ? qp->dim->ctx : NULL((void*)0);
447}
448
449/* Return the domain space of "qp".
450 */
451static __isl_keep isl_space *isl_qpolynomial_peek_domain_space(
452 __isl_keep isl_qpolynomial *qp)
453{
454 return qp ? qp->dim : NULL((void*)0);
455}
456
457/* Return a copy of the domain space of "qp".
458 */
459__isl_give isl_space *isl_qpolynomial_get_domain_space(
460 __isl_keep isl_qpolynomial *qp)
461{
462 return isl_space_copy(isl_qpolynomial_peek_domain_space(qp));
463}
464
465#undef TYPEisl_term
466#define TYPEisl_term isl_qpolynomial
467#undef PEEK_SPACEpeek_space
468#define PEEK_SPACEpeek_space peek_domain_space
469
470static
471#include "isl_type_has_equal_space_bin_templ.c"
472static
473#include "isl_type_check_equal_space_templ.c"
474
475#undef PEEK_SPACEpeek_space
476
477/* Return a copy of the local space on which "qp" is defined.
478 */
479static __isl_give isl_local_space *isl_qpolynomial_get_domain_local_space(
480 __isl_keep isl_qpolynomial *qp)
481{
482 isl_space *space;
483
484 if (!qp)
485 return NULL((void*)0);
486
487 space = isl_qpolynomial_get_domain_space(qp);
488 return isl_local_space_alloc_div(space, isl_mat_copy(qp->div));
489}
490
491__isl_give isl_space *isl_qpolynomial_get_space(__isl_keep isl_qpolynomial *qp)
492{
493 isl_space *space;
494 if (!qp)
495 return NULL((void*)0);
496 space = isl_space_copy(qp->dim);
497 space = isl_space_from_domain(space);
498 space = isl_space_add_dims(space, isl_dim_out, 1);
499 return space;
500}
501
502/* Return the number of variables of the given type in the domain of "qp".
503 */
504isl_size isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial *qp,
505 enum isl_dim_type type)
506{
507 isl_space *space;
508 isl_size dim;
509
510 space = isl_qpolynomial_peek_domain_space(qp);
511
512 if (!space)
513 return isl_size_error((int) -1);
514 if (type == isl_dim_div)
515 return qp->div->n_row;
516 dim = isl_space_dim(space, type);
517 if (dim < 0)
518 return isl_size_error((int) -1);
519 if (type == isl_dim_all) {
520 isl_size n_div;
521
522 n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
523 if (n_div < 0)
524 return isl_size_error((int) -1);
525 dim += n_div;
526 }
527 return dim;
528}
529
530/* Given the type of a dimension of an isl_qpolynomial,
531 * return the type of the corresponding dimension in its domain.
532 * This function is only called for "type" equal to isl_dim_in or
533 * isl_dim_param.
534 */
535static enum isl_dim_type domain_type(enum isl_dim_type type)
536{
537 return type == isl_dim_in ? isl_dim_set : type;
538}
539
540/* Externally, an isl_qpolynomial has a map space, but internally, the
541 * ls field corresponds to the domain of that space.
542 */
543isl_size isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
544 enum isl_dim_type type)
545{
546 if (!qp)
547 return isl_size_error((int) -1);
548 if (type == isl_dim_out)
549 return 1;
550 type = domain_type(type);
551 return isl_qpolynomial_domain_dim(qp, type);
552}
553
554/* Return the offset of the first variable of type "type" within
555 * the variables of the domain of "qp".
556 */
557static isl_size isl_qpolynomial_domain_var_offset(
558 __isl_keep isl_qpolynomial *qp, enum isl_dim_type type)
559{
560 isl_space *space;
561
562 space = isl_qpolynomial_peek_domain_space(qp);
563 if (!space)
564 return isl_size_error((int) -1);
565
566 switch (type) {
567 case isl_dim_param:
568 case isl_dim_set: return isl_space_offset(space, type);
569 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
570 case isl_dim_cst:
571 default:
572 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,do { isl_handle_error(isl_qpolynomial_get_ctx(qp), isl_error_invalid
, "invalid dimension type", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 573); return ((int) -1); } while (0)
573 "invalid dimension type", return isl_size_error)do { isl_handle_error(isl_qpolynomial_get_ctx(qp), isl_error_invalid
, "invalid dimension type", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 573); return ((int) -1); } while (0)
;
574 }
575}
576
577/* Return the offset of the first coefficient of type "type" in
578 * the domain of "qp".
579 */
580unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial *qp,
581 enum isl_dim_type type)
582{
583 switch (type) {
584 case isl_dim_cst:
585 return 0;
586 case isl_dim_param:
587 case isl_dim_set:
588 case isl_dim_div:
589 return 1 + isl_qpolynomial_domain_var_offset(qp, type);
590 default:
591 return 0;
592 }
593}
594
595isl_bool isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
596{
597 return qp ? isl_poly_is_zero(qp->poly) : isl_bool_error;
598}
599
600isl_bool isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
601{
602 return qp ? isl_poly_is_one(qp->poly) : isl_bool_error;
603}
604
605isl_bool isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
606{
607 return qp ? isl_poly_is_nan(qp->poly) : isl_bool_error;
608}
609
610isl_bool isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
611{
612 return qp ? isl_poly_is_infty(qp->poly) : isl_bool_error;
613}
614
615isl_bool isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
616{
617 return qp ? isl_poly_is_neginfty(qp->poly) : isl_bool_error;
618}
619
620int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
621{
622 return qp ? isl_poly_sgn(qp->poly) : 0;
623}
624
625static void poly_free_cst(__isl_take isl_poly_cst *cst)
626{
627 isl_int_clear(cst->n)isl_sioimath_clear((cst->n));
628 isl_int_clear(cst->d)isl_sioimath_clear((cst->d));
629}
630
631static void poly_free_rec(__isl_take isl_poly_rec *rec)
632{
633 int i;
634
635 for (i = 0; i < rec->n; ++i)
636 isl_poly_free(rec->p[i]);
637}
638
639__isl_give isl_poly *isl_poly_copy(__isl_keep isl_poly *poly)
640{
641 if (!poly)
642 return NULL((void*)0);
643
644 poly->ref++;
645 return poly;
646}
647
648__isl_give isl_poly *isl_poly_dup_cst(__isl_keep isl_poly *poly)
649{
650 isl_poly_cst *cst;
651 isl_poly_cst *dup;
652
653 cst = isl_poly_as_cst(poly);
654 if (!cst)
655 return NULL((void*)0);
656
657 dup = isl_poly_as_cst(isl_poly_zero(poly->ctx));
658 if (!dup)
659 return NULL((void*)0);
660 isl_int_set(dup->n, cst->n)isl_sioimath_set((dup->n), *(cst->n));
661 isl_int_set(dup->d, cst->d)isl_sioimath_set((dup->d), *(cst->d));
662
663 return &dup->poly;
664}
665
666__isl_give isl_poly *isl_poly_dup_rec(__isl_keep isl_poly *poly)
667{
668 int i;
669 isl_poly_rec *rec;
670 isl_poly_rec *dup;
671
672 rec = isl_poly_as_rec(poly);
673 if (!rec)
674 return NULL((void*)0);
675
676 dup = isl_poly_alloc_rec(poly->ctx, poly->var, rec->n);
677 if (!dup)
678 return NULL((void*)0);
679
680 for (i = 0; i < rec->n; ++i) {
681 dup->p[i] = isl_poly_copy(rec->p[i]);
682 if (!dup->p[i])
683 goto error;
684 dup->n++;
685 }
686
687 return &dup->poly;
688error:
689 isl_poly_free(&dup->poly);
690 return NULL((void*)0);
691}
692
693__isl_give isl_poly *isl_poly_dup(__isl_keep isl_poly *poly)
694{
695 isl_bool is_cst;
696
697 is_cst = isl_poly_is_cst(poly);
698 if (is_cst < 0)
699 return NULL((void*)0);
700 if (is_cst)
701 return isl_poly_dup_cst(poly);
702 else
703 return isl_poly_dup_rec(poly);
704}
705
706__isl_give isl_poly *isl_poly_cow(__isl_take isl_poly *poly)
707{
708 if (!poly)
709 return NULL((void*)0);
710
711 if (poly->ref == 1)
712 return poly;
713 poly->ref--;
714 return isl_poly_dup(poly);
715}
716
717__isl_null isl_poly *isl_poly_free(__isl_take isl_poly *poly)
718{
719 if (!poly)
720 return NULL((void*)0);
721
722 if (--poly->ref > 0)
723 return NULL((void*)0);
724
725 if (poly->var < 0)
726 poly_free_cst((isl_poly_cst *) poly);
727 else
728 poly_free_rec((isl_poly_rec *) poly);
729
730 isl_ctx_deref(poly->ctx);
731 free(poly);
732 return NULL((void*)0);
733}
734
735static void isl_poly_cst_reduce(__isl_keep isl_poly_cst *cst)
736{
737 isl_int gcd;
738
739 isl_int_init(gcd)isl_sioimath_init((gcd));
740 isl_int_gcd(gcd, cst->n, cst->d)isl_sioimath_gcd((gcd), *(cst->n), *(cst->d));
741 if (!isl_int_is_zero(gcd)(isl_sioimath_sgn(*(gcd)) == 0) && !isl_int_is_one(gcd)(isl_sioimath_cmp_si(*(gcd), 1) == 0)) {
742 isl_int_divexact(cst->n, cst->n, gcd)isl_sioimath_tdiv_q((cst->n), *(cst->n), *(gcd));
743 isl_int_divexact(cst->d, cst->d, gcd)isl_sioimath_tdiv_q((cst->d), *(cst->d), *(gcd));
744 }
745 isl_int_clear(gcd)isl_sioimath_clear((gcd));
746}
747
748__isl_give isl_poly *isl_poly_sum_cst(__isl_take isl_poly *poly1,
749 __isl_take isl_poly *poly2)
750{
751 isl_poly_cst *cst1;
752 isl_poly_cst *cst2;
753
754 poly1 = isl_poly_cow(poly1);
755 if (!poly1 || !poly2)
756 goto error;
757
758 cst1 = isl_poly_as_cst(poly1);
759 cst2 = isl_poly_as_cst(poly2);
760
761 if (isl_int_eq(cst1->d, cst2->d)(isl_sioimath_cmp(*(cst1->d), *(cst2->d)) == 0))
762 isl_int_add(cst1->n, cst1->n, cst2->n)isl_sioimath_add((cst1->n), *(cst1->n), *(cst2->n));
763 else {
764 isl_int_mul(cst1->n, cst1->n, cst2->d)isl_sioimath_mul((cst1->n), *(cst1->n), *(cst2->d));
765 isl_int_addmul(cst1->n, cst2->n, cst1->d)isl_sioimath_addmul((cst1->n), *(cst2->n), *(cst1->d
))
;
766 isl_int_mul(cst1->d, cst1->d, cst2->d)isl_sioimath_mul((cst1->d), *(cst1->d), *(cst2->d));
767 }
768
769 isl_poly_cst_reduce(cst1);
770
771 isl_poly_free(poly2);
772 return poly1;
773error:
774 isl_poly_free(poly1);
775 isl_poly_free(poly2);
776 return NULL((void*)0);
777}
778
779static __isl_give isl_poly *replace_by_zero(__isl_take isl_poly *poly)
780{
781 struct isl_ctx *ctx;
782
783 if (!poly)
784 return NULL((void*)0);
785 ctx = poly->ctx;
786 isl_poly_free(poly);
787 return isl_poly_zero(ctx);
788}
789
790static __isl_give isl_poly *replace_by_constant_term(__isl_take isl_poly *poly)
791{
792 isl_poly_rec *rec;
793 isl_poly *cst;
794
795 if (!poly)
796 return NULL((void*)0);
797
798 rec = isl_poly_as_rec(poly);
799 if (!rec)
800 goto error;
801 cst = isl_poly_copy(rec->p[0]);
802 isl_poly_free(poly);
803 return cst;
804error:
805 isl_poly_free(poly);
806 return NULL((void*)0);
807}
808
809__isl_give isl_poly *isl_poly_sum(__isl_take isl_poly *poly1,
810 __isl_take isl_poly *poly2)
811{
812 int i;
813 isl_bool is_zero, is_nan, is_cst;
814 isl_poly_rec *rec1, *rec2;
815
816 if (!poly1 || !poly2)
817 goto error;
818
819 is_nan = isl_poly_is_nan(poly1);
820 if (is_nan < 0)
821 goto error;
822 if (is_nan) {
823 isl_poly_free(poly2);
824 return poly1;
825 }
826
827 is_nan = isl_poly_is_nan(poly2);
828 if (is_nan < 0)
829 goto error;
830 if (is_nan) {
831 isl_poly_free(poly1);
832 return poly2;
833 }
834
835 is_zero = isl_poly_is_zero(poly1);
836 if (is_zero < 0)
837 goto error;
838 if (is_zero) {
839 isl_poly_free(poly1);
840 return poly2;
841 }
842
843 is_zero = isl_poly_is_zero(poly2);
844 if (is_zero < 0)
845 goto error;
846 if (is_zero) {
847 isl_poly_free(poly2);
848 return poly1;
849 }
850
851 if (poly1->var < poly2->var)
852 return isl_poly_sum(poly2, poly1);
853
854 if (poly2->var < poly1->var) {
855 isl_poly_rec *rec;
856 isl_bool is_infty;
857
858 is_infty = isl_poly_is_infty(poly2);
859 if (is_infty >= 0 && !is_infty)
860 is_infty = isl_poly_is_neginfty(poly2);
861 if (is_infty < 0)
862 goto error;
863 if (is_infty) {
864 isl_poly_free(poly1);
865 return poly2;
866 }
867 poly1 = isl_poly_cow(poly1);
868 rec = isl_poly_as_rec(poly1);
869 if (!rec)
870 goto error;
871 rec->p[0] = isl_poly_sum(rec->p[0], poly2);
872 if (rec->n == 1)
873 poly1 = replace_by_constant_term(poly1);
874 return poly1;
875 }
876
877 is_cst = isl_poly_is_cst(poly1);
878 if (is_cst < 0)
879 goto error;
880 if (is_cst)
881 return isl_poly_sum_cst(poly1, poly2);
882
883 rec1 = isl_poly_as_rec(poly1);
884 rec2 = isl_poly_as_rec(poly2);
885 if (!rec1 || !rec2)
886 goto error;
887
888 if (rec1->n < rec2->n)
889 return isl_poly_sum(poly2, poly1);
890
891 poly1 = isl_poly_cow(poly1);
892 rec1 = isl_poly_as_rec(poly1);
893 if (!rec1)
894 goto error;
895
896 for (i = rec2->n - 1; i >= 0; --i) {
897 isl_bool is_zero;
898
899 rec1->p[i] = isl_poly_sum(rec1->p[i],
900 isl_poly_copy(rec2->p[i]));
901 if (!rec1->p[i])
902 goto error;
903 if (i != rec1->n - 1)
904 continue;
905 is_zero = isl_poly_is_zero(rec1->p[i]);
906 if (is_zero < 0)
907 goto error;
908 if (is_zero) {
909 isl_poly_free(rec1->p[i]);
910 rec1->n--;
911 }
912 }
913
914 if (rec1->n == 0)
915 poly1 = replace_by_zero(poly1);
916 else if (rec1->n == 1)
917 poly1 = replace_by_constant_term(poly1);
918
919 isl_poly_free(poly2);
920
921 return poly1;
922error:
923 isl_poly_free(poly1);
924 isl_poly_free(poly2);
925 return NULL((void*)0);
926}
927
928__isl_give isl_poly *isl_poly_cst_add_isl_int(__isl_take isl_poly *poly,
929 isl_int v)
930{
931 isl_poly_cst *cst;
932
933 poly = isl_poly_cow(poly);
934 if (!poly)
935 return NULL((void*)0);
936
937 cst = isl_poly_as_cst(poly);
938
939 isl_int_addmul(cst->n, cst->d, v)isl_sioimath_addmul((cst->n), *(cst->d), *(v));
940
941 return poly;
942}
943
944__isl_give isl_poly *isl_poly_add_isl_int(__isl_take isl_poly *poly, isl_int v)
945{
946 isl_bool is_cst;
947 isl_poly_rec *rec;
948
949 is_cst = isl_poly_is_cst(poly);
950 if (is_cst < 0)
951 return isl_poly_free(poly);
952 if (is_cst)
953 return isl_poly_cst_add_isl_int(poly, v);
954
955 poly = isl_poly_cow(poly);
956 rec = isl_poly_as_rec(poly);
957 if (!rec)
958 goto error;
959
960 rec->p[0] = isl_poly_add_isl_int(rec->p[0], v);
961 if (!rec->p[0])
962 goto error;
963
964 return poly;
965error:
966 isl_poly_free(poly);
967 return NULL((void*)0);
968}
969
970__isl_give isl_poly *isl_poly_cst_mul_isl_int(__isl_take isl_poly *poly,
971 isl_int v)
972{
973 isl_bool is_zero;
974 isl_poly_cst *cst;
975
976 is_zero = isl_poly_is_zero(poly);
977 if (is_zero < 0)
978 return isl_poly_free(poly);
979 if (is_zero)
980 return poly;
981
982 poly = isl_poly_cow(poly);
983 if (!poly)
984 return NULL((void*)0);
985
986 cst = isl_poly_as_cst(poly);
987
988 isl_int_mul(cst->n, cst->n, v)isl_sioimath_mul((cst->n), *(cst->n), *(v));
989
990 return poly;
991}
992
993__isl_give isl_poly *isl_poly_mul_isl_int(__isl_take isl_poly *poly, isl_int v)
994{
995 int i;
996 isl_bool is_cst;
997 isl_poly_rec *rec;
998
999 is_cst = isl_poly_is_cst(poly);
1000 if (is_cst < 0)
1001 return isl_poly_free(poly);
1002 if (is_cst)
1003 return isl_poly_cst_mul_isl_int(poly, v);
1004
1005 poly = isl_poly_cow(poly);
1006 rec = isl_poly_as_rec(poly);
1007 if (!rec)
1008 goto error;
1009
1010 for (i = 0; i < rec->n; ++i) {
1011 rec->p[i] = isl_poly_mul_isl_int(rec->p[i], v);
1012 if (!rec->p[i])
1013 goto error;
1014 }
1015
1016 return poly;
1017error:
1018 isl_poly_free(poly);
1019 return NULL((void*)0);
1020}
1021
1022/* Multiply the constant polynomial "poly" by "v".
1023 */
1024static __isl_give isl_poly *isl_poly_cst_scale_val(__isl_take isl_poly *poly,
1025 __isl_keep isl_val *v)
1026{
1027 isl_bool is_zero;
1028 isl_poly_cst *cst;
1029
1030 is_zero = isl_poly_is_zero(poly);
1031 if (is_zero < 0)
1032 return isl_poly_free(poly);
1033 if (is_zero)
1034 return poly;
1035
1036 poly = isl_poly_cow(poly);
1037 if (!poly)
1038 return NULL((void*)0);
1039
1040 cst = isl_poly_as_cst(poly);
1041
1042 isl_int_mul(cst->n, cst->n, v->n)isl_sioimath_mul((cst->n), *(cst->n), *(v->n));
1043 isl_int_mul(cst->d, cst->d, v->d)isl_sioimath_mul((cst->d), *(cst->d), *(v->d));
1044 isl_poly_cst_reduce(cst);
1045
1046 return poly;
1047}
1048
1049/* Multiply the polynomial "poly" by "v".
1050 */
1051static __isl_give isl_poly *isl_poly_scale_val(__isl_take isl_poly *poly,
1052 __isl_keep isl_val *v)
1053{
1054 int i;
1055 isl_bool is_cst;
1056 isl_poly_rec *rec;
1057
1058 is_cst = isl_poly_is_cst(poly);
1059 if (is_cst < 0)
1060 return isl_poly_free(poly);
1061 if (is_cst)
1062 return isl_poly_cst_scale_val(poly, v);
1063
1064 poly = isl_poly_cow(poly);
1065 rec = isl_poly_as_rec(poly);
1066 if (!rec)
1067 goto error;
1068
1069 for (i = 0; i < rec->n; ++i) {
1070 rec->p[i] = isl_poly_scale_val(rec->p[i], v);
1071 if (!rec->p[i])
1072 goto error;
1073 }
1074
1075 return poly;
1076error:
1077 isl_poly_free(poly);
1078 return NULL((void*)0);
1079}
1080
1081__isl_give isl_poly *isl_poly_mul_cst(__isl_take isl_poly *poly1,
1082 __isl_take isl_poly *poly2)
1083{
1084 isl_poly_cst *cst1;
1085 isl_poly_cst *cst2;
1086
1087 poly1 = isl_poly_cow(poly1);
1088 if (!poly1 || !poly2)
1089 goto error;
1090
1091 cst1 = isl_poly_as_cst(poly1);
1092 cst2 = isl_poly_as_cst(poly2);
1093
1094 isl_int_mul(cst1->n, cst1->n, cst2->n)isl_sioimath_mul((cst1->n), *(cst1->n), *(cst2->n));
1095 isl_int_mul(cst1->d, cst1->d, cst2->d)isl_sioimath_mul((cst1->d), *(cst1->d), *(cst2->d));
1096
1097 isl_poly_cst_reduce(cst1);
1098
1099 isl_poly_free(poly2);
1100 return poly1;
1101error:
1102 isl_poly_free(poly1);
1103 isl_poly_free(poly2);
1104 return NULL((void*)0);
1105}
1106
1107__isl_give isl_poly *isl_poly_mul_rec(__isl_take isl_poly *poly1,
1108 __isl_take isl_poly *poly2)
1109{
1110 isl_poly_rec *rec1;
1111 isl_poly_rec *rec2;
1112 isl_poly_rec *res = NULL((void*)0);
1113 int i, j;
1114 int size;
1115
1116 rec1 = isl_poly_as_rec(poly1);
1117 rec2 = isl_poly_as_rec(poly2);
1118 if (!rec1 || !rec2)
1119 goto error;
1120 size = rec1->n + rec2->n - 1;
1121 res = isl_poly_alloc_rec(poly1->ctx, poly1->var, size);
1122 if (!res)
1123 goto error;
1124
1125 for (i = 0; i < rec1->n; ++i) {
1126 res->p[i] = isl_poly_mul(isl_poly_copy(rec2->p[0]),
1127 isl_poly_copy(rec1->p[i]));
1128 if (!res->p[i])
1129 goto error;
1130 res->n++;
1131 }
1132 for (; i < size; ++i) {
1133 res->p[i] = isl_poly_zero(poly1->ctx);
1134 if (!res->p[i])
1135 goto error;
1136 res->n++;
1137 }
1138 for (i = 0; i < rec1->n; ++i) {
1139 for (j = 1; j < rec2->n; ++j) {
1140 isl_poly *poly;
1141 poly = isl_poly_mul(isl_poly_copy(rec2->p[j]),
1142 isl_poly_copy(rec1->p[i]));
1143 res->p[i + j] = isl_poly_sum(res->p[i + j], poly);
1144 if (!res->p[i + j])
1145 goto error;
1146 }
1147 }
1148
1149 isl_poly_free(poly1);
1150 isl_poly_free(poly2);
1151
1152 return &res->poly;
1153error:
1154 isl_poly_free(poly1);
1155 isl_poly_free(poly2);
1156 isl_poly_free(&res->poly);
1157 return NULL((void*)0);
1158}
1159
1160__isl_give isl_poly *isl_poly_mul(__isl_take isl_poly *poly1,
1161 __isl_take isl_poly *poly2)
1162{
1163 isl_bool is_zero, is_nan, is_one, is_cst;
1164
1165 if (!poly1 || !poly2)
1166 goto error;
1167
1168 is_nan = isl_poly_is_nan(poly1);
1169 if (is_nan < 0)
1170 goto error;
1171 if (is_nan) {
1172 isl_poly_free(poly2);
1173 return poly1;
1174 }
1175
1176 is_nan = isl_poly_is_nan(poly2);
1177 if (is_nan < 0)
1178 goto error;
1179 if (is_nan) {
1180 isl_poly_free(poly1);
1181 return poly2;
1182 }
1183
1184 is_zero = isl_poly_is_zero(poly1);
1185 if (is_zero < 0)
1186 goto error;
1187 if (is_zero) {
1188 isl_poly_free(poly2);
1189 return poly1;
1190 }
1191
1192 is_zero = isl_poly_is_zero(poly2);
1193 if (is_zero < 0)
1194 goto error;
1195 if (is_zero) {
1196 isl_poly_free(poly1);
1197 return poly2;
1198 }
1199
1200 is_one = isl_poly_is_one(poly1);
1201 if (is_one < 0)
1202 goto error;
1203 if (is_one) {
1204 isl_poly_free(poly1);
1205 return poly2;
1206 }
1207
1208 is_one = isl_poly_is_one(poly2);
1209 if (is_one < 0)
1210 goto error;
1211 if (is_one) {
1212 isl_poly_free(poly2);
1213 return poly1;
1214 }
1215
1216 if (poly1->var < poly2->var)
1217 return isl_poly_mul(poly2, poly1);
1218
1219 if (poly2->var < poly1->var) {
1220 int i;
1221 isl_poly_rec *rec;
1222 isl_bool is_infty;
1223
1224 is_infty = isl_poly_is_infty(poly2);
1225 if (is_infty >= 0 && !is_infty)
1226 is_infty = isl_poly_is_neginfty(poly2);
1227 if (is_infty < 0)
1228 goto error;
1229 if (is_infty) {
1230 isl_ctx *ctx = poly1->ctx;
1231 isl_poly_free(poly1);
1232 isl_poly_free(poly2);
1233 return isl_poly_nan(ctx);
1234 }
1235 poly1 = isl_poly_cow(poly1);
1236 rec = isl_poly_as_rec(poly1);
1237 if (!rec)
1238 goto error;
1239
1240 for (i = 0; i < rec->n; ++i) {
1241 rec->p[i] = isl_poly_mul(rec->p[i],
1242 isl_poly_copy(poly2));
1243 if (!rec->p[i])
1244 goto error;
1245 }
1246 isl_poly_free(poly2);
1247 return poly1;
1248 }
1249
1250 is_cst = isl_poly_is_cst(poly1);
1251 if (is_cst < 0)
1252 goto error;
1253 if (is_cst)
1254 return isl_poly_mul_cst(poly1, poly2);
1255
1256 return isl_poly_mul_rec(poly1, poly2);
1257error:
1258 isl_poly_free(poly1);
1259 isl_poly_free(poly2);
1260 return NULL((void*)0);
1261}
1262
1263__isl_give isl_poly *isl_poly_pow(__isl_take isl_poly *poly, unsigned power)
1264{
1265 isl_poly *res;
1266
1267 if (!poly)
1268 return NULL((void*)0);
1269 if (power == 1)
1270 return poly;
1271
1272 if (power % 2)
1273 res = isl_poly_copy(poly);
1274 else
1275 res = isl_poly_one(poly->ctx);
1276
1277 while (power >>= 1) {
1278 poly = isl_poly_mul(poly, isl_poly_copy(poly));
1279 if (power % 2)
1280 res = isl_poly_mul(res, isl_poly_copy(poly));
1281 }
1282
1283 isl_poly_free(poly);
1284 return res;
1285}
1286
1287__isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_space *space,
1288 unsigned n_div, __isl_take isl_poly *poly)
1289{
1290 struct isl_qpolynomial *qp = NULL((void*)0);
1291 isl_size total;
1292
1293 total = isl_space_dim(space, isl_dim_all);
1294 if (total < 0 || !poly)
1295 goto error;
1296
1297 if (!isl_space_is_set(space))
1298 isl_die(isl_space_get_ctx(space), isl_error_invalid,do { isl_handle_error(isl_space_get_ctx(space), isl_error_invalid
, "domain of polynomial should be a set", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 1299); goto error; } while (0)
1299 "domain of polynomial should be a set", goto error)do { isl_handle_error(isl_space_get_ctx(space), isl_error_invalid
, "domain of polynomial should be a set", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 1299); goto error; } while (0)
;
1300
1301 qp = isl_calloc_type(space->ctx, struct isl_qpolynomial)((struct isl_qpolynomial *)isl_calloc_or_die(space->ctx, 1
, sizeof(struct isl_qpolynomial)))
;
1302 if (!qp)
1303 goto error;
1304
1305 qp->ref = 1;
1306 qp->div = isl_mat_alloc(space->ctx, n_div, 1 + 1 + total + n_div);
1307 if (!qp->div)
1308 goto error;
1309
1310 qp->dim = space;
1311 qp->poly = poly;
1312
1313 return qp;
1314error:
1315 isl_space_free(space);
1316 isl_poly_free(poly);
1317 isl_qpolynomial_free(qp);
1318 return NULL((void*)0);
1319}
1320
1321__isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
1322{
1323 if (!qp)
1324 return NULL((void*)0);
1325
1326 qp->ref++;
1327 return qp;
1328}
1329
1330__isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
1331{
1332 struct isl_qpolynomial *dup;
1333
1334 if (!qp)
1335 return NULL((void*)0);
1336
1337 dup = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row,
1338 isl_poly_copy(qp->poly));
1339 if (!dup)
1340 return NULL((void*)0);
1341 isl_mat_free(dup->div);
1342 dup->div = isl_mat_copy(qp->div);
1343 if (!dup->div)
1344 goto error;
1345
1346 return dup;
1347error:
1348 isl_qpolynomial_free(dup);
1349 return NULL((void*)0);
1350}
1351
1352__isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1353{
1354 if (!qp)
1355 return NULL((void*)0);
1356
1357 if (qp->ref == 1)
1358 return qp;
1359 qp->ref--;
1360 return isl_qpolynomial_dup(qp);
1361}
1362
1363__isl_null isl_qpolynomial *isl_qpolynomial_free(
1364 __isl_take isl_qpolynomial *qp)
1365{
1366 if (!qp)
1367 return NULL((void*)0);
1368
1369 if (--qp->ref > 0)
1370 return NULL((void*)0);
1371
1372 isl_space_free(qp->dim);
1373 isl_mat_free(qp->div);
1374 isl_poly_free(qp->poly);
1375
1376 free(qp);
1377 return NULL((void*)0);
1378}
1379
1380__isl_give isl_poly *isl_poly_var_pow(isl_ctx *ctx, int pos, int power)
1381{
1382 int i;
1383 isl_poly_rec *rec;
1384 isl_poly_cst *cst;
1385
1386 rec = isl_poly_alloc_rec(ctx, pos, 1 + power);
1387 if (!rec)
1388 return NULL((void*)0);
1389 for (i = 0; i < 1 + power; ++i) {
1390 rec->p[i] = isl_poly_zero(ctx);
1391 if (!rec->p[i])
1392 goto error;
1393 rec->n++;
1394 }
1395 cst = isl_poly_as_cst(rec->p[power]);
1396 isl_int_set_si(cst->n, 1)isl_sioimath_set_si((cst->n), 1);
1397
1398 return &rec->poly;
1399error:
1400 isl_poly_free(&rec->poly);
1401 return NULL((void*)0);
1402}
1403
1404/* r array maps original positions to new positions.
1405 */
1406static __isl_give isl_poly *reorder(__isl_take isl_poly *poly, int *r)
1407{
1408 int i;
1409 isl_bool is_cst;
1410 isl_poly_rec *rec;
1411 isl_poly *base;
1412 isl_poly *res;
1413
1414 is_cst = isl_poly_is_cst(poly);
1415 if (is_cst < 0)
1416 return isl_poly_free(poly);
1417 if (is_cst)
1418 return poly;
1419
1420 rec = isl_poly_as_rec(poly);
1421 if (!rec)
1422 goto error;
1423
1424 isl_assert(poly->ctx, rec->n >= 1, goto error)do { if (rec->n >= 1) break; do { isl_handle_error(poly
->ctx, isl_error_unknown, "Assertion \"" "rec->n >= 1"
"\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 1424); goto error; } while (0); } while (0)
;
1425
1426 base = isl_poly_var_pow(poly->ctx, r[poly->var], 1);
1427 res = reorder(isl_poly_copy(rec->p[rec->n - 1]), r);
1428
1429 for (i = rec->n - 2; i >= 0; --i) {
1430 res = isl_poly_mul(res, isl_poly_copy(base));
1431 res = isl_poly_sum(res, reorder(isl_poly_copy(rec->p[i]), r));
1432 }
1433
1434 isl_poly_free(base);
1435 isl_poly_free(poly);
1436
1437 return res;
1438error:
1439 isl_poly_free(poly);
1440 return NULL((void*)0);
1441}
1442
1443static isl_bool compatible_divs(__isl_keep isl_mat *div1,
1444 __isl_keep isl_mat *div2)
1445{
1446 int n_row, n_col;
1447 isl_bool equal;
1448
1449 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&do { if (div1->n_row >= div2->n_row && div1->
n_col >= div2->n_col) break; do { isl_handle_error(div1
->ctx, isl_error_unknown, "Assertion \"" "div1->n_row >= div2->n_row && div1->n_col >= div2->n_col"
"\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 1451); return isl_bool_error; } while (0); } while (0)
1450 div1->n_col >= div2->n_col,do { if (div1->n_row >= div2->n_row && div1->
n_col >= div2->n_col) break; do { isl_handle_error(div1
->ctx, isl_error_unknown, "Assertion \"" "div1->n_row >= div2->n_row && div1->n_col >= div2->n_col"
"\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 1451); return isl_bool_error; } while (0); } while (0)
1451 return isl_bool_error)do { if (div1->n_row >= div2->n_row && div1->
n_col >= div2->n_col) break; do { isl_handle_error(div1
->ctx, isl_error_unknown, "Assertion \"" "div1->n_row >= div2->n_row && div1->n_col >= div2->n_col"
"\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 1451); return isl_bool_error; } while (0); } while (0)
;
1452
1453 if (div1->n_row == div2->n_row)
1454 return isl_mat_is_equal(div1, div2);
1455
1456 n_row = div1->n_row;
1457 n_col = div1->n_col;
1458 div1->n_row = div2->n_row;
1459 div1->n_col = div2->n_col;
1460
1461 equal = isl_mat_is_equal(div1, div2);
1462
1463 div1->n_row = n_row;
1464 div1->n_col = n_col;
1465
1466 return equal;
1467}
1468
1469static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1470{
1471 int li, lj;
1472
1473 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1474 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1475
1476 if (li != lj)
1477 return li - lj;
1478
1479 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1480}
1481
1482struct isl_div_sort_info {
1483 isl_mat *div;
1484 int row;
1485};
1486
1487static int div_sort_cmp(const void *p1, const void *p2)
1488{
1489 const struct isl_div_sort_info *i1, *i2;
1490 i1 = (const struct isl_div_sort_info *) p1;
1491 i2 = (const struct isl_div_sort_info *) p2;
1492
1493 return cmp_row(i1->div, i1->row, i2->row);
1494}
1495
1496/* Sort divs and remove duplicates.
1497 */
1498static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1499{
1500 int i;
1501 int skip;
1502 int len;
1503 struct isl_div_sort_info *array = NULL((void*)0);
1504 int *pos = NULL((void*)0), *at = NULL((void*)0);
1505 int *reordering = NULL((void*)0);
1506 isl_size div_pos;
1507
1508 if (!qp)
1509 return NULL((void*)0);
1510 if (qp->div->n_row <= 1)
1511 return qp;
1512
1513 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
1514 if (div_pos < 0)
1515 return isl_qpolynomial_free(qp);
1516
1517 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,((struct isl_div_sort_info *)isl_malloc_or_die(qp->div->
ctx, (qp->div->n_row)*sizeof(struct isl_div_sort_info))
)
1518 qp->div->n_row)((struct isl_div_sort_info *)isl_malloc_or_die(qp->div->
ctx, (qp->div->n_row)*sizeof(struct isl_div_sort_info))
)
;
1519 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row)((int *)isl_malloc_or_die(qp->div->ctx, (qp->div->
n_row)*sizeof(int)))
;
1520 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row)((int *)isl_malloc_or_die(qp->div->ctx, (qp->div->
n_row)*sizeof(int)))
;
1521 len = qp->div->n_col - 2;
1522 reordering = isl_alloc_array(qp->div->ctx, int, len)((int *)isl_malloc_or_die(qp->div->ctx, (len)*sizeof(int
)))
;
1523 if (!array || !pos || !at || !reordering)
1524 goto error;
1525
1526 for (i = 0; i < qp->div->n_row; ++i) {
1527 array[i].div = qp->div;
1528 array[i].row = i;
1529 pos[i] = i;
1530 at[i] = i;
1531 }
1532
1533 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1534 div_sort_cmp);
1535
1536 for (i = 0; i < div_pos; ++i)
1537 reordering[i] = i;
1538
1539 for (i = 0; i < qp->div->n_row; ++i) {
1540 if (pos[array[i].row] == i)
1541 continue;
1542 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1543 pos[at[i]] = pos[array[i].row];
1544 at[pos[array[i].row]] = at[i];
1545 at[i] = array[i].row;
1546 pos[array[i].row] = i;
1547 }
1548
1549 skip = 0;
1550 for (i = 0; i < len - div_pos; ++i) {
1551 if (i > 0 &&
1552 isl_seq_eq(qp->div->row[i - skip - 1],
1553 qp->div->row[i - skip], qp->div->n_col)) {
1554 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1555 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1556 2 + div_pos + i - skip);
1557 qp->div = isl_mat_drop_cols(qp->div,
1558 2 + div_pos + i - skip, 1);
1559 skip++;
1560 }
1561 reordering[div_pos + array[i].row] = div_pos + i - skip;
1562 }
1563
1564 qp->poly = reorder(qp->poly, reordering);
1565
1566 if (!qp->poly || !qp->div)
1567 goto error;
1568
1569 free(at);
1570 free(pos);
1571 free(array);
1572 free(reordering);
1573
1574 return qp;
1575error:
1576 free(at);
1577 free(pos);
1578 free(array);
1579 free(reordering);
1580 isl_qpolynomial_free(qp);
1581 return NULL((void*)0);
1582}
1583
1584static __isl_give isl_poly *expand(__isl_take isl_poly *poly, int *exp,
1585 int first)
1586{
1587 int i;
1588 isl_bool is_cst;
1589 isl_poly_rec *rec;
1590
1591 is_cst = isl_poly_is_cst(poly);
1592 if (is_cst < 0)
1593 return isl_poly_free(poly);
1594 if (is_cst)
1595 return poly;
1596
1597 if (poly->var < first)
1598 return poly;
1599
1600 if (exp[poly->var - first] == poly->var - first)
1601 return poly;
1602
1603 poly = isl_poly_cow(poly);
1604 if (!poly)
1605 goto error;
1606
1607 poly->var = exp[poly->var - first] + first;
1608
1609 rec = isl_poly_as_rec(poly);
1610 if (!rec)
1611 goto error;
1612
1613 for (i = 0; i < rec->n; ++i) {
1614 rec->p[i] = expand(rec->p[i], exp, first);
1615 if (!rec->p[i])
1616 goto error;
1617 }
1618
1619 return poly;
1620error:
1621 isl_poly_free(poly);
1622 return NULL((void*)0);
1623}
1624
1625static __isl_give isl_qpolynomial *with_merged_divs(
1626 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1627 __isl_take isl_qpolynomial *qp2),
1628 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1629{
1630 int *exp1 = NULL((void*)0);
1631 int *exp2 = NULL((void*)0);
1632 isl_mat *div = NULL((void*)0);
1633 int n_div1, n_div2;
1634
1635 qp1 = isl_qpolynomial_cow(qp1);
1636 qp2 = isl_qpolynomial_cow(qp2);
1637
1638 if (!qp1 || !qp2)
1639 goto error;
1640
1641 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&do { if (qp1->div->n_row >= qp2->div->n_row &&
qp1->div->n_col >= qp2->div->n_col) break; do
{ isl_handle_error(qp1->div->ctx, isl_error_unknown, "Assertion \""
"qp1->div->n_row >= qp2->div->n_row && qp1->div->n_col >= qp2->div->n_col"
"\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 1642); goto error; } while (0); } while (0)
1642 qp1->div->n_col >= qp2->div->n_col, goto error)do { if (qp1->div->n_row >= qp2->div->n_row &&
qp1->div->n_col >= qp2->div->n_col) break; do
{ isl_handle_error(qp1->div->ctx, isl_error_unknown, "Assertion \""
"qp1->div->n_row >= qp2->div->n_row && qp1->div->n_col >= qp2->div->n_col"
"\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 1642); goto error; } while (0); } while (0)
;
1643
1644 n_div1 = qp1->div->n_row;
1645 n_div2 = qp2->div->n_row;
1646 exp1 = isl_alloc_array(qp1->div->ctx, int, n_div1)((int *)isl_malloc_or_die(qp1->div->ctx, (n_div1)*sizeof
(int)))
;
1647 exp2 = isl_alloc_array(qp2->div->ctx, int, n_div2)((int *)isl_malloc_or_die(qp2->div->ctx, (n_div2)*sizeof
(int)))
;
1648 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1649 goto error;
1650
1651 div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1652 if (!div)
1653 goto error;
1654
1655 isl_mat_free(qp1->div);
1656 qp1->div = isl_mat_copy(div);
1657 isl_mat_free(qp2->div);
1658 qp2->div = isl_mat_copy(div);
1659
1660 qp1->poly = expand(qp1->poly, exp1, div->n_col - div->n_row - 2);
1661 qp2->poly = expand(qp2->poly, exp2, div->n_col - div->n_row - 2);
1662
1663 if (!qp1->poly || !qp2->poly)
1664 goto error;
1665
1666 isl_mat_free(div);
1667 free(exp1);
1668 free(exp2);
1669
1670 return fn(qp1, qp2);
1671error:
1672 isl_mat_free(div);
1673 free(exp1);
1674 free(exp2);
1675 isl_qpolynomial_free(qp1);
1676 isl_qpolynomial_free(qp2);
1677 return NULL((void*)0);
1678}
1679
1680__isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1681 __isl_take isl_qpolynomial *qp2)
1682{
1683 isl_bool compatible;
1684
1685 qp1 = isl_qpolynomial_cow(qp1);
1686
1687 if (isl_qpolynomial_check_equal_space(qp1, qp2) < 0)
1688 goto error;
1689
1690 if (qp1->div->n_row < qp2->div->n_row)
1691 return isl_qpolynomial_add(qp2, qp1);
1692
1693 compatible = compatible_divs(qp1->div, qp2->div);
1694 if (compatible < 0)
1695 goto error;
1696 if (!compatible)
1697 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1698
1699 qp1->poly = isl_poly_sum(qp1->poly, isl_poly_copy(qp2->poly));
1700 if (!qp1->poly)
1701 goto error;
1702
1703 isl_qpolynomial_free(qp2);
1704
1705 return qp1;
1706error:
1707 isl_qpolynomial_free(qp1);
1708 isl_qpolynomial_free(qp2);
1709 return NULL((void*)0);
1710}
1711
1712__isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1713 __isl_keep isl_setisl_map *dom,
1714 __isl_take isl_qpolynomial *qp1,
1715 __isl_take isl_qpolynomial *qp2)
1716{
1717 qp1 = isl_qpolynomial_add(qp1, qp2);
1718 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1719 return qp1;
1720}
1721
1722__isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1723 __isl_take isl_qpolynomial *qp2)
1724{
1725 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1726}
1727
1728__isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1729 __isl_take isl_qpolynomial *qp, isl_int v)
1730{
1731 if (isl_int_is_zero(v)(isl_sioimath_sgn(*(v)) == 0))
1732 return qp;
1733
1734 qp = isl_qpolynomial_cow(qp);
1735 if (!qp)
1736 return NULL((void*)0);
1737
1738 qp->poly = isl_poly_add_isl_int(qp->poly, v);
1739 if (!qp->poly)
1740 goto error;
1741
1742 return qp;
1743error:
1744 isl_qpolynomial_free(qp);
1745 return NULL((void*)0);
1746
1747}
1748
1749__isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1750{
1751 if (!qp)
1752 return NULL((void*)0);
1753
1754 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1755}
1756
1757__isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1758 __isl_take isl_qpolynomial *qp, isl_int v)
1759{
1760 if (isl_int_is_one(v)(isl_sioimath_cmp_si(*(v), 1) == 0))
1761 return qp;
1762
1763 if (qp && isl_int_is_zero(v)(isl_sioimath_sgn(*(v)) == 0)) {
1764 isl_qpolynomial *zero;
1765 zero = isl_qpolynomial_zero_on_domain(isl_space_copy(qp->dim));
1766 isl_qpolynomial_free(qp);
1767 return zero;
1768 }
1769
1770 qp = isl_qpolynomial_cow(qp);
1771 if (!qp)
1772 return NULL((void*)0);
1773
1774 qp->poly = isl_poly_mul_isl_int(qp->poly, v);
1775 if (!qp->poly)
1776 goto error;
1777
1778 return qp;
1779error:
1780 isl_qpolynomial_free(qp);
1781 return NULL((void*)0);
1782}
1783
1784__isl_give isl_qpolynomial *isl_qpolynomial_scale(
1785 __isl_take isl_qpolynomial *qp, isl_int v)
1786{
1787 return isl_qpolynomial_mul_isl_int(qp, v);
1788}
1789
1790/* Multiply "qp" by "v".
1791 */
1792__isl_give isl_qpolynomial *isl_qpolynomial_scale_val(
1793 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1794{
1795 if (!qp || !v)
1796 goto error;
1797
1798 if (!isl_val_is_rat(v))
1799 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,do { isl_handle_error(isl_qpolynomial_get_ctx(qp), isl_error_invalid
, "expecting rational factor", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 1800); goto error; } while (0)
1800 "expecting rational factor", goto error)do { isl_handle_error(isl_qpolynomial_get_ctx(qp), isl_error_invalid
, "expecting rational factor", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 1800); goto error; } while (0)
;
1801
1802 if (isl_val_is_one(v)) {
1803 isl_val_free(v);
1804 return qp;
1805 }
1806
1807 if (isl_val_is_zero(v)) {
1808 isl_space *space;
1809
1810 space = isl_qpolynomial_get_domain_space(qp);
1811 isl_qpolynomial_free(qp);
1812 isl_val_free(v);
1813 return isl_qpolynomial_zero_on_domain(space);
1814 }
1815
1816 qp = isl_qpolynomial_cow(qp);
1817 if (!qp)
1818 goto error;
1819
1820 qp->poly = isl_poly_scale_val(qp->poly, v);
1821 if (!qp->poly)
1822 qp = isl_qpolynomial_free(qp);
1823
1824 isl_val_free(v);
1825 return qp;
1826error:
1827 isl_val_free(v);
1828 isl_qpolynomial_free(qp);
1829 return NULL((void*)0);
1830}
1831
1832/* Divide "qp" by "v".
1833 */
1834__isl_give isl_qpolynomial *isl_qpolynomial_scale_down_val(
1835 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1836{
1837 if (!qp || !v)
1838 goto error;
1839
1840 if (!isl_val_is_rat(v))
1841 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,do { isl_handle_error(isl_qpolynomial_get_ctx(qp), isl_error_invalid
, "expecting rational factor", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 1842); goto error; } while (0)
1842 "expecting rational factor", goto error)do { isl_handle_error(isl_qpolynomial_get_ctx(qp), isl_error_invalid
, "expecting rational factor", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 1842); goto error; } while (0)
;
1843 if (isl_val_is_zero(v))
1844 isl_die(isl_val_get_ctx(v), isl_error_invalid,do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "cannot scale down by zero"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 1845); goto error; } while (0)
1845 "cannot scale down by zero", goto error)do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "cannot scale down by zero"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 1845); goto error; } while (0)
;
1846
1847 return isl_qpolynomial_scale_val(qp, isl_val_inv(v));
1848error:
1849 isl_val_free(v);
1850 isl_qpolynomial_free(qp);
1851 return NULL((void*)0);
1852}
1853
1854__isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1855 __isl_take isl_qpolynomial *qp2)
1856{
1857 isl_bool compatible;
1858
1859 qp1 = isl_qpolynomial_cow(qp1);
1860
1861 if (isl_qpolynomial_check_equal_space(qp1, qp2) < 0)
1862 goto error;
1863
1864 if (qp1->div->n_row < qp2->div->n_row)
1865 return isl_qpolynomial_mul(qp2, qp1);
1866
1867 compatible = compatible_divs(qp1->div, qp2->div);
1868 if (compatible < 0)
1869 goto error;
1870 if (!compatible)
1871 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1872
1873 qp1->poly = isl_poly_mul(qp1->poly, isl_poly_copy(qp2->poly));
1874 if (!qp1->poly)
1875 goto error;
1876
1877 isl_qpolynomial_free(qp2);
1878
1879 return qp1;
1880error:
1881 isl_qpolynomial_free(qp1);
1882 isl_qpolynomial_free(qp2);
1883 return NULL((void*)0);
1884}
1885
1886__isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1887 unsigned power)
1888{
1889 qp = isl_qpolynomial_cow(qp);
1890
1891 if (!qp)
1892 return NULL((void*)0);
1893
1894 qp->poly = isl_poly_pow(qp->poly, power);
1895 if (!qp->poly)
1896 goto error;
1897
1898 return qp;
1899error:
1900 isl_qpolynomial_free(qp);
1901 return NULL((void*)0);
1902}
1903
1904__isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_pow(
1905 __isl_take isl_pw_qpolynomial *pwqp, unsigned power)
1906{
1907 int i;
1908
1909 if (power == 1)
1910 return pwqp;
1911
1912 pwqp = isl_pw_qpolynomial_cow(pwqp);
1913 if (!pwqp)
1914 return NULL((void*)0);
1915
1916 for (i = 0; i < pwqp->n; ++i) {
1917 pwqp->p[i].qp = isl_qpolynomial_pow(pwqp->p[i].qp, power);
1918 if (!pwqp->p[i].qp)
1919 return isl_pw_qpolynomial_free(pwqp);
1920 }
1921
1922 return pwqp;
1923}
1924
1925__isl_give isl_qpolynomial *isl_qpolynomial_zero_on_domain(
1926 __isl_take isl_space *domain)
1927{
1928 if (!domain)
2
Assuming 'domain' is non-null
3
Taking false branch
1929 return NULL((void*)0);
1930 return isl_qpolynomial_alloc(domain, 0, isl_poly_zero(domain->ctx));
4
Returning pointer, which participates in a condition later
1931}
1932
1933__isl_give isl_qpolynomial *isl_qpolynomial_one_on_domain(
1934 __isl_take isl_space *domain)
1935{
1936 if (!domain)
1937 return NULL((void*)0);
1938 return isl_qpolynomial_alloc(domain, 0, isl_poly_one(domain->ctx));
1939}
1940
1941__isl_give isl_qpolynomial *isl_qpolynomial_infty_on_domain(
1942 __isl_take isl_space *domain)
1943{
1944 if (!domain)
1945 return NULL((void*)0);
1946 return isl_qpolynomial_alloc(domain, 0, isl_poly_infty(domain->ctx));
1947}
1948
1949__isl_give isl_qpolynomial *isl_qpolynomial_neginfty_on_domain(
1950 __isl_take isl_space *domain)
1951{
1952 if (!domain)
1953 return NULL((void*)0);
1954 return isl_qpolynomial_alloc(domain, 0, isl_poly_neginfty(domain->ctx));
1955}
1956
1957__isl_give isl_qpolynomial *isl_qpolynomial_nan_on_domain(
1958 __isl_take isl_space *domain)
1959{
1960 if (!domain)
1961 return NULL((void*)0);
1962 return isl_qpolynomial_alloc(domain, 0, isl_poly_nan(domain->ctx));
1963}
1964
1965__isl_give isl_qpolynomial *isl_qpolynomial_cst_on_domain(
1966 __isl_take isl_space *domain,
1967 isl_int v)
1968{
1969 struct isl_qpolynomial *qp;
1970 isl_poly_cst *cst;
1971
1972 qp = isl_qpolynomial_zero_on_domain(domain);
1
Calling 'isl_qpolynomial_zero_on_domain'
5
Returning from 'isl_qpolynomial_zero_on_domain'
1973 if (!qp)
6
Assuming 'qp' is non-null
7
Taking false branch
1974 return NULL((void*)0);
1975
1976 cst = isl_poly_as_cst(qp->poly);
1977 isl_int_set(cst->n, v)isl_sioimath_set((cst->n), *(v));
8
Passing null pointer value via 1st parameter 'dst'
9
Calling 'isl_sioimath_set'
1978
1979 return qp;
1980}
1981
1982isl_bool isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1983 isl_int *n, isl_int *d)
1984{
1985 isl_bool is_cst;
1986 isl_poly_cst *cst;
1987
1988 if (!qp)
1989 return isl_bool_error;
1990
1991 is_cst = isl_poly_is_cst(qp->poly);
1992 if (is_cst < 0 || !is_cst)
1993 return is_cst;
1994
1995 cst = isl_poly_as_cst(qp->poly);
1996 if (!cst)
1997 return isl_bool_error;
1998
1999 if (n)
2000 isl_int_set(*n, cst->n)isl_sioimath_set((*n), *(cst->n));
2001 if (d)
2002 isl_int_set(*d, cst->d)isl_sioimath_set((*d), *(cst->d));
2003
2004 return isl_bool_true;
2005}
2006
2007/* Return the constant term of "poly".
2008 */
2009static __isl_give isl_val *isl_poly_get_constant_val(__isl_keep isl_poly *poly)
2010{
2011 isl_bool is_cst;
2012 isl_poly_cst *cst;
2013
2014 if (!poly)
2015 return NULL((void*)0);
2016
2017 while ((is_cst = isl_poly_is_cst(poly)) == isl_bool_false) {
2018 isl_poly_rec *rec;
2019
2020 rec = isl_poly_as_rec(poly);
2021 if (!rec)
2022 return NULL((void*)0);
2023 poly = rec->p[0];
2024 }
2025 if (is_cst < 0)
2026 return NULL((void*)0);
2027
2028 cst = isl_poly_as_cst(poly);
2029 if (!cst)
2030 return NULL((void*)0);
2031 return isl_val_rat_from_isl_int(cst->poly.ctx, cst->n, cst->d);
2032}
2033
2034/* Return the constant term of "qp".
2035 */
2036__isl_give isl_val *isl_qpolynomial_get_constant_val(
2037 __isl_keep isl_qpolynomial *qp)
2038{
2039 if (!qp)
2040 return NULL((void*)0);
2041
2042 return isl_poly_get_constant_val(qp->poly);
2043}
2044
2045isl_bool isl_poly_is_affine(__isl_keep isl_poly *poly)
2046{
2047 isl_bool is_cst;
2048 isl_poly_rec *rec;
2049
2050 if (!poly)
2051 return isl_bool_error;
2052
2053 if (poly->var < 0)
2054 return isl_bool_true;
2055
2056 rec = isl_poly_as_rec(poly);
2057 if (!rec)
2058 return isl_bool_error;
2059
2060 if (rec->n > 2)
2061 return isl_bool_false;
2062
2063 isl_assert(poly->ctx, rec->n > 1, return isl_bool_error)do { if (rec->n > 1) break; do { isl_handle_error(poly->
ctx, isl_error_unknown, "Assertion \"" "rec->n > 1" "\" failed"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 2063); return isl_bool_error; } while (0); } while (0)
;
2064
2065 is_cst = isl_poly_is_cst(rec->p[1]);
2066 if (is_cst < 0 || !is_cst)
2067 return is_cst;
2068
2069 return isl_poly_is_affine(rec->p[0]);
2070}
2071
2072isl_bool isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
2073{
2074 if (!qp)
2075 return isl_bool_error;
2076
2077 if (qp->div->n_row > 0)
2078 return isl_bool_false;
2079
2080 return isl_poly_is_affine(qp->poly);
2081}
2082
2083static void update_coeff(__isl_keep isl_vec *aff,
2084 __isl_keep isl_poly_cst *cst, int pos)
2085{
2086 isl_int gcd;
2087 isl_int f;
2088
2089 if (isl_int_is_zero(cst->n)(isl_sioimath_sgn(*(cst->n)) == 0))
2090 return;
2091
2092 isl_int_init(gcd)isl_sioimath_init((gcd));
2093 isl_int_init(f)isl_sioimath_init((f));
2094 isl_int_gcd(gcd, cst->d, aff->el[0])isl_sioimath_gcd((gcd), *(cst->d), *(aff->el[0]));
2095 isl_int_divexact(f, cst->d, gcd)isl_sioimath_tdiv_q((f), *(cst->d), *(gcd));
2096 isl_int_divexact(gcd, aff->el[0], gcd)isl_sioimath_tdiv_q((gcd), *(aff->el[0]), *(gcd));
2097 isl_seq_scale(aff->el, aff->el, f, aff->size);
2098 isl_int_mul(aff->el[1 + pos], gcd, cst->n)isl_sioimath_mul((aff->el[1 + pos]), *(gcd), *(cst->n));
2099 isl_int_clear(gcd)isl_sioimath_clear((gcd));
2100 isl_int_clear(f)isl_sioimath_clear((f));
2101}
2102
2103int isl_poly_update_affine(__isl_keep isl_poly *poly, __isl_keep isl_vec *aff)
2104{
2105 isl_poly_cst *cst;
2106 isl_poly_rec *rec;
2107
2108 if (!poly || !aff)
2109 return -1;
2110
2111 if (poly->var < 0) {
2112 isl_poly_cst *cst;
2113
2114 cst = isl_poly_as_cst(poly);
2115 if (!cst)
2116 return -1;
2117 update_coeff(aff, cst, 0);
2118 return 0;
2119 }
2120
2121 rec = isl_poly_as_rec(poly);
2122 if (!rec)
2123 return -1;
2124 isl_assert(poly->ctx, rec->n == 2, return -1)do { if (rec->n == 2) break; do { isl_handle_error(poly->
ctx, isl_error_unknown, "Assertion \"" "rec->n == 2" "\" failed"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 2124); return -1; } while (0); } while (0)
;
2125
2126 cst = isl_poly_as_cst(rec->p[1]);
2127 if (!cst)
2128 return -1;
2129 update_coeff(aff, cst, 1 + poly->var);
2130
2131 return isl_poly_update_affine(rec->p[0], aff);
2132}
2133
2134__isl_give isl_vec *isl_qpolynomial_extract_affine(
2135 __isl_keep isl_qpolynomial *qp)
2136{
2137 isl_vec *aff;
2138 isl_size d;
2139
2140 d = isl_qpolynomial_domain_dim(qp, isl_dim_all);
2141 if (d < 0)
2142 return NULL((void*)0);
2143
2144 aff = isl_vec_alloc(qp->div->ctx, 2 + d);
2145 if (!aff)
2146 return NULL((void*)0);
2147
2148 isl_seq_clr(aff->el + 1, 1 + d);
2149 isl_int_set_si(aff->el[0], 1)isl_sioimath_set_si((aff->el[0]), 1);
2150
2151 if (isl_poly_update_affine(qp->poly, aff) < 0)
2152 goto error;
2153
2154 return aff;
2155error:
2156 isl_vec_free(aff);
2157 return NULL((void*)0);
2158}
2159
2160/* Compare two quasi-polynomials.
2161 *
2162 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2163 * than "qp2" and 0 if they are equal.
2164 */
2165int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial *qp1,
2166 __isl_keep isl_qpolynomial *qp2)
2167{
2168 int cmp;
2169
2170 if (qp1 == qp2)
2171 return 0;
2172 if (!qp1)
2173 return -1;
2174 if (!qp2)
2175 return 1;
2176
2177 cmp = isl_space_cmp(qp1->dim, qp2->dim);
2178 if (cmp != 0)
2179 return cmp;
2180
2181 cmp = isl_local_cmp(qp1->div, qp2->div);
2182 if (cmp != 0)
2183 return cmp;
2184
2185 return isl_poly_plain_cmp(qp1->poly, qp2->poly);
2186}
2187
2188/* Is "qp1" obviously equal to "qp2"?
2189 *
2190 * NaN is not equal to anything, not even to another NaN.
2191 */
2192isl_bool isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
2193 __isl_keep isl_qpolynomial *qp2)
2194{
2195 isl_bool equal;
2196
2197 if (!qp1 || !qp2)
2198 return isl_bool_error;
2199
2200 if (isl_qpolynomial_is_nan(qp1) || isl_qpolynomial_is_nan(qp2))
2201 return isl_bool_false;
2202
2203 equal = isl_space_is_equal(qp1->dim, qp2->dim);
2204 if (equal < 0 || !equal)
2205 return equal;
2206
2207 equal = isl_mat_is_equal(qp1->div, qp2->div);
2208 if (equal < 0 || !equal)
2209 return equal;
2210
2211 return isl_poly_is_equal(qp1->poly, qp2->poly);
2212}
2213
2214static isl_stat poly_update_den(__isl_keep isl_poly *poly, isl_int *d)
2215{
2216 int i;
2217 isl_bool is_cst;
2218 isl_poly_rec *rec;
2219
2220 is_cst = isl_poly_is_cst(poly);
2221 if (is_cst < 0)
2222 return isl_stat_error;
2223 if (is_cst) {
2224 isl_poly_cst *cst;
2225 cst = isl_poly_as_cst(poly);
2226 if (!cst)
2227 return isl_stat_error;
2228 isl_int_lcm(*d, *d, cst->d)isl_sioimath_lcm((*d), *(*d), *(cst->d));
2229 return isl_stat_ok;
2230 }
2231
2232 rec = isl_poly_as_rec(poly);
2233 if (!rec)
2234 return isl_stat_error;
2235
2236 for (i = 0; i < rec->n; ++i)
2237 poly_update_den(rec->p[i], d);
2238
2239 return isl_stat_ok;
2240}
2241
2242__isl_give isl_val *isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp)
2243{
2244 isl_val *d;
2245
2246 if (!qp)
2247 return NULL((void*)0);
2248 d = isl_val_one(isl_qpolynomial_get_ctx(qp));
2249 if (!d)
2250 return NULL((void*)0);
2251 if (poly_update_den(qp->poly, &d->n) < 0)
2252 return isl_val_free(d);
2253 return d;
2254}
2255
2256__isl_give isl_qpolynomial *isl_qpolynomial_var_pow_on_domain(
2257 __isl_take isl_space *domain, int pos, int power)
2258{
2259 struct isl_ctx *ctx;
2260
2261 if (!domain)
2262 return NULL((void*)0);
2263
2264 ctx = domain->ctx;
2265
2266 return isl_qpolynomial_alloc(domain, 0,
2267 isl_poly_var_pow(ctx, pos, power));
2268}
2269
2270__isl_give isl_qpolynomial *isl_qpolynomial_var_on_domain(
2271 __isl_take isl_space *domain, enum isl_dim_type type, unsigned pos)
2272{
2273 if (isl_space_check_is_set(domain ) < 0)
2274 goto error;
2275 if (isl_space_check_range(domain, type, pos, 1) < 0)
2276 goto error;
2277
2278 pos += isl_space_offset(domain, type);
2279
2280 return isl_qpolynomial_var_pow_on_domain(domain, pos, 1);
2281error:
2282 isl_space_free(domain);
2283 return NULL((void*)0);
2284}
2285
2286__isl_give isl_poly *isl_poly_subs(__isl_take isl_poly *poly,
2287 unsigned first, unsigned n, __isl_keep isl_poly **subs)
2288{
2289 int i;
2290 isl_bool is_cst;
2291 isl_poly_rec *rec;
2292 isl_poly *base, *res;
2293
2294 is_cst = isl_poly_is_cst(poly);
2295 if (is_cst < 0)
2296 return isl_poly_free(poly);
2297 if (is_cst)
2298 return poly;
2299
2300 if (poly->var < first)
2301 return poly;
2302
2303 rec = isl_poly_as_rec(poly);
2304 if (!rec)
2305 goto error;
2306
2307 isl_assert(poly->ctx, rec->n >= 1, goto error)do { if (rec->n >= 1) break; do { isl_handle_error(poly
->ctx, isl_error_unknown, "Assertion \"" "rec->n >= 1"
"\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 2307); goto error; } while (0); } while (0)
;
2308
2309 if (poly->var >= first + n)
2310 base = isl_poly_var_pow(poly->ctx, poly->var, 1);
2311 else
2312 base = isl_poly_copy(subs[poly->var - first]);
2313
2314 res = isl_poly_subs(isl_poly_copy(rec->p[rec->n - 1]), first, n, subs);
2315 for (i = rec->n - 2; i >= 0; --i) {
2316 isl_poly *t;
2317 t = isl_poly_subs(isl_poly_copy(rec->p[i]), first, n, subs);
2318 res = isl_poly_mul(res, isl_poly_copy(base));
2319 res = isl_poly_sum(res, t);
2320 }
2321
2322 isl_poly_free(base);
2323 isl_poly_free(poly);
2324
2325 return res;
2326error:
2327 isl_poly_free(poly);
2328 return NULL((void*)0);
2329}
2330
2331__isl_give isl_poly *isl_poly_from_affine(isl_ctx *ctx, isl_int *f,
2332 isl_int denom, unsigned len)
2333{
2334 int i;
2335 isl_poly *poly;
2336
2337 isl_assert(ctx, len >= 1, return NULL)do { if (len >= 1) break; do { isl_handle_error(ctx, isl_error_unknown
, "Assertion \"" "len >= 1" "\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 2337); return ((void*)0); } while (0); } while (0)
;
2338
2339 poly = isl_poly_rat_cst(ctx, f[0], denom);
2340 for (i = 0; i < len - 1; ++i) {
2341 isl_poly *t;
2342 isl_poly *c;
2343
2344 if (isl_int_is_zero(f[1 + i])(isl_sioimath_sgn(*(f[1 + i])) == 0))
2345 continue;
2346
2347 c = isl_poly_rat_cst(ctx, f[1 + i], denom);
2348 t = isl_poly_var_pow(ctx, i, 1);
2349 t = isl_poly_mul(c, t);
2350 poly = isl_poly_sum(poly, t);
2351 }
2352
2353 return poly;
2354}
2355
2356/* Remove common factor of non-constant terms and denominator.
2357 */
2358static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
2359{
2360 isl_ctx *ctx = qp->div->ctx;
2361 unsigned total = qp->div->n_col - 2;
2362
2363 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
2364 isl_int_gcd(ctx->normalize_gcd,isl_sioimath_gcd((ctx->normalize_gcd), *(ctx->normalize_gcd
), *(qp->div->row[div][0]))
2365 ctx->normalize_gcd, qp->div->row[div][0])isl_sioimath_gcd((ctx->normalize_gcd), *(ctx->normalize_gcd
), *(qp->div->row[div][0]))
;
2366 if (isl_int_is_one(ctx->normalize_gcd)(isl_sioimath_cmp_si(*(ctx->normalize_gcd), 1) == 0))
2367 return;
2368
2369 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
2370 ctx->normalize_gcd, total);
2371 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],isl_sioimath_tdiv_q((qp->div->row[div][0]), *(qp->div
->row[div][0]), *(ctx->normalize_gcd))
2372 ctx->normalize_gcd)isl_sioimath_tdiv_q((qp->div->row[div][0]), *(qp->div
->row[div][0]), *(ctx->normalize_gcd))
;
2373 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],isl_sioimath_fdiv_q((qp->div->row[div][1]), *(qp->div
->row[div][1]), *(ctx->normalize_gcd))
2374 ctx->normalize_gcd)isl_sioimath_fdiv_q((qp->div->row[div][1]), *(qp->div
->row[div][1]), *(ctx->normalize_gcd))
;
2375}
2376
2377/* Replace the integer division identified by "div" by the polynomial "s".
2378 * The integer division is assumed not to appear in the definition
2379 * of any other integer divisions.
2380 */
2381static __isl_give isl_qpolynomial *substitute_div(
2382 __isl_take isl_qpolynomial *qp, int div, __isl_take isl_poly *s)
2383{
2384 int i;
2385 isl_size div_pos;
2386 int *reordering;
2387 isl_ctx *ctx;
2388
2389 if (!qp || !s)
2390 goto error;
2391
2392 qp = isl_qpolynomial_cow(qp);
2393 if (!qp)
2394 goto error;
2395
2396 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
2397 if (div_pos < 0)
2398 goto error;
2399 qp->poly = isl_poly_subs(qp->poly, div_pos + div, 1, &s);
2400 if (!qp->poly)
2401 goto error;
2402
2403 ctx = isl_qpolynomial_get_ctx(qp);
2404 reordering = isl_alloc_array(ctx, int, div_pos + qp->div->n_row)((int *)isl_malloc_or_die(ctx, (div_pos + qp->div->n_row
)*sizeof(int)))
;
2405 if (!reordering)
2406 goto error;
2407 for (i = 0; i < div_pos + div; ++i)
2408 reordering[i] = i;
2409 for (i = div_pos + div + 1; i < div_pos + qp->div->n_row; ++i)
2410 reordering[i] = i - 1;
2411 qp->div = isl_mat_drop_rows(qp->div, div, 1);
2412 qp->div = isl_mat_drop_cols(qp->div, 2 + div_pos + div, 1);
2413 qp->poly = reorder(qp->poly, reordering);
2414 free(reordering);
2415
2416 if (!qp->poly || !qp->div)
2417 goto error;
2418
2419 isl_poly_free(s);
2420 return qp;
2421error:
2422 isl_qpolynomial_free(qp);
2423 isl_poly_free(s);
2424 return NULL((void*)0);
2425}
2426
2427/* Replace all integer divisions [e/d] that turn out to not actually be integer
2428 * divisions because d is equal to 1 by their definition, i.e., e.
2429 */
2430static __isl_give isl_qpolynomial *substitute_non_divs(
2431 __isl_take isl_qpolynomial *qp)
2432{
2433 int i, j;
2434 isl_size div_pos;
2435 isl_poly *s;
2436
2437 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
2438 if (div_pos < 0)
2439 return isl_qpolynomial_free(qp);
2440
2441 for (i = 0; qp && i < qp->div->n_row; ++i) {
2442 if (!isl_int_is_one(qp->div->row[i][0])(isl_sioimath_cmp_si(*(qp->div->row[i][0]), 1) == 0))
2443 continue;
2444 for (j = i + 1; j < qp->div->n_row; ++j) {
2445 if (isl_int_is_zero(qp->div->row[j][2 + div_pos + i])(isl_sioimath_sgn(*(qp->div->row[j][2 + div_pos + i])) ==
0)
)
2446 continue;
2447 isl_seq_combine(qp->div->row[j] + 1,
2448 qp->div->ctx->one, qp->div->row[j] + 1,
2449 qp->div->row[j][2 + div_pos + i],
2450 qp->div->row[i] + 1, 1 + div_pos + i);
2451 isl_int_set_si(qp->div->row[j][2 + div_pos + i], 0)isl_sioimath_set_si((qp->div->row[j][2 + div_pos + i]),
0)
;
2452 normalize_div(qp, j);
2453 }
2454 s = isl_poly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
2455 qp->div->row[i][0], qp->div->n_col - 1);
2456 qp = substitute_div(qp, i, s);
2457 --i;
2458 }
2459
2460 return qp;
2461}
2462
2463/* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2464 * with d the denominator. When replacing the coefficient e of x by
2465 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2466 * inside the division, so we need to add floor(e/d) * x outside.
2467 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2468 * to adjust the coefficient of x in each later div that depends on the
2469 * current div "div" and also in the affine expressions in the rows of "mat"
2470 * (if they too depend on "div").
2471 */
2472static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
2473 __isl_keep isl_mat **mat)
2474{
2475 int i, j;
2476 isl_int v;
2477 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2478
2479 isl_int_init(v)isl_sioimath_init((v));
2480 for (i = 0; i < 1 + total + div; ++i) {
2481 if (isl_int_is_nonneg(qp->div->row[div][1 + i])(isl_sioimath_sgn(*(qp->div->row[div][1 + i])) >= 0) &&
2482 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0])(isl_sioimath_cmp(*(qp->div->row[div][1 + i]), *(qp->
div->row[div][0])) < 0)
)
2483 continue;
2484 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0])isl_sioimath_fdiv_q((v), *(qp->div->row[div][1 + i]), *
(qp->div->row[div][0]))
;
2485 isl_int_fdiv_r(qp->div->row[div][1 + i],isl_sioimath_fdiv_r((qp->div->row[div][1 + i]), *(qp->
div->row[div][1 + i]), *(qp->div->row[div][0]))
2486 qp->div->row[div][1 + i], qp->div->row[div][0])isl_sioimath_fdiv_r((qp->div->row[div][1 + i]), *(qp->
div->row[div][1 + i]), *(qp->div->row[div][0]))
;
2487 *mat = isl_mat_col_addmul(*mat, i, v, 1 + total + div);
2488 for (j = div + 1; j < qp->div->n_row; ++j) {
2489 if (isl_int_is_zero(qp->div->row[j][2 + total + div])(isl_sioimath_sgn(*(qp->div->row[j][2 + total + div])) ==
0)
)
2490 continue;
2491 isl_int_addmul(qp->div->row[j][1 + i],isl_sioimath_addmul((qp->div->row[j][1 + i]), *(v), *(qp
->div->row[j][2 + total + div]))
2492 v, qp->div->row[j][2 + total + div])isl_sioimath_addmul((qp->div->row[j][1 + i]), *(v), *(qp
->div->row[j][2 + total + div]))
;
2493 }
2494 }
2495 isl_int_clear(v)isl_sioimath_clear((v));
2496}
2497
2498/* Check if the last non-zero coefficient is bigger that half of the
2499 * denominator. If so, we will invert the div to further reduce the number
2500 * of distinct divs that may appear.
2501 * If the last non-zero coefficient is exactly half the denominator,
2502 * then we continue looking for earlier coefficients that are bigger
2503 * than half the denominator.
2504 */
2505static int needs_invert(__isl_keep isl_mat *div, int row)
2506{
2507 int i;
2508 int cmp;
2509
2510 for (i = div->n_col - 1; i >= 1; --i) {
2511 if (isl_int_is_zero(div->row[row][i])(isl_sioimath_sgn(*(div->row[row][i])) == 0))
2512 continue;
2513 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2)isl_sioimath_mul_ui((div->row[row][i]), *(div->row[row]
[i]), 2)
;
2514 cmp = isl_int_cmp(div->row[row][i], div->row[row][0])isl_sioimath_cmp(*(div->row[row][i]), *(div->row[row][0
]))
;
2515 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2)isl_sioimath_tdiv_q_ui((div->row[row][i]), *(div->row[row
][i]), 2)
;
2516 if (cmp)
2517 return cmp > 0;
2518 if (i == 1)
2519 return 1;
2520 }
2521
2522 return 0;
2523}
2524
2525/* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2526 * We only invert the coefficients of e (and the coefficient of q in
2527 * later divs and in the rows of "mat"). After calling this function, the
2528 * coefficients of e should be reduced again.
2529 */
2530static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2531 __isl_keep isl_mat **mat)
2532{
2533 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2534
2535 isl_seq_neg(qp->div->row[div] + 1,
2536 qp->div->row[div] + 1, qp->div->n_col - 1);
2537 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1)isl_sioimath_sub_ui((qp->div->row[div][1]), *(qp->div
->row[div][1]), 1)
;
2538 isl_int_add(qp->div->row[div][1],isl_sioimath_add((qp->div->row[div][1]), *(qp->div->
row[div][1]), *(qp->div->row[div][0]))
2539 qp->div->row[div][1], qp->div->row[div][0])isl_sioimath_add((qp->div->row[div][1]), *(qp->div->
row[div][1]), *(qp->div->row[div][0]))
;
2540 *mat = isl_mat_col_neg(*mat, 1 + total + div);
2541 isl_mat_col_mul(qp->div, 2 + total + div,
2542 qp->div->ctx->negone, 2 + total + div);
2543}
2544
2545/* Reduce all divs of "qp" to have coefficients
2546 * in the interval [0, d-1], with d the denominator and such that the
2547 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2548 * The modifications to the integer divisions need to be reflected
2549 * in the factors of the polynomial that refer to the original
2550 * integer divisions. To this end, the modifications are collected
2551 * as a set of affine expressions and then plugged into the polynomial.
2552 *
2553 * After the reduction, some divs may have become redundant or identical,
2554 * so we call substitute_non_divs and sort_divs. If these functions
2555 * eliminate divs or merge two or more divs into one, the coefficients
2556 * of the enclosing divs may have to be reduced again, so we call
2557 * ourselves recursively if the number of divs decreases.
2558 */
2559static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2560{
2561 int i;
2562 isl_ctx *ctx;
2563 isl_mat *mat;
2564 isl_poly **s;
2565 unsigned o_div;
2566 isl_size n_div, total, new_n_div;
2567
2568 total = isl_qpolynomial_domain_dim(qp, isl_dim_all);
2569 n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
2570 o_div = isl_qpolynomial_domain_offset(qp, isl_dim_div);
2571 if (total < 0 || n_div < 0)
2572 return isl_qpolynomial_free(qp);
2573 ctx = isl_qpolynomial_get_ctx(qp);
2574 mat = isl_mat_zero(ctx, n_div, 1 + total);
2575
2576 for (i = 0; i < n_div; ++i)
2577 mat = isl_mat_set_element_si(mat, i, o_div + i, 1);
2578
2579 for (i = 0; i < qp->div->n_row; ++i) {
2580 normalize_div(qp, i);
2581 reduce_div(qp, i, &mat);
2582 if (needs_invert(qp->div, i)) {
2583 invert_div(qp, i, &mat);
2584 reduce_div(qp, i, &mat);
2585 }
2586 }
2587 if (!mat)
2588 goto error;
2589
2590 s = isl_alloc_array(ctx, struct isl_poly *, n_div)((struct isl_poly * *)isl_malloc_or_die(ctx, (n_div)*sizeof(struct
isl_poly *)))
;
2591 if (n_div && !s)
2592 goto error;
2593 for (i = 0; i < n_div; ++i)
2594 s[i] = isl_poly_from_affine(ctx, mat->row[i], ctx->one,
2595 1 + total);
2596 qp->poly = isl_poly_subs(qp->poly, o_div - 1, n_div, s);
2597 for (i = 0; i < n_div; ++i)
2598 isl_poly_free(s[i]);
2599 free(s);
2600 if (!qp->poly)
2601 goto error;
2602
2603 isl_mat_free(mat);
2604
2605 qp = substitute_non_divs(qp);
2606 qp = sort_divs(qp);
2607 new_n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
2608 if (new_n_div < 0)
2609 return isl_qpolynomial_free(qp);
2610 if (new_n_div < n_div)
2611 return reduce_divs(qp);
2612
2613 return qp;
2614error:
2615 isl_qpolynomial_free(qp);
2616 isl_mat_free(mat);
2617 return NULL((void*)0);
2618}
2619
2620__isl_give isl_qpolynomial *isl_qpolynomial_rat_cst_on_domain(
2621 __isl_take isl_space *domain, const isl_int n, const isl_int d)
2622{
2623 struct isl_qpolynomial *qp;
2624 isl_poly_cst *cst;
2625
2626 qp = isl_qpolynomial_zero_on_domain(domain);
2627 if (!qp)
2628 return NULL((void*)0);
2629
2630 cst = isl_poly_as_cst(qp->poly);
2631 isl_int_set(cst->n, n)isl_sioimath_set((cst->n), *(n));
2632 isl_int_set(cst->d, d)isl_sioimath_set((cst->d), *(d));
2633
2634 return qp;
2635}
2636
2637/* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2638 */
2639__isl_give isl_qpolynomial *isl_qpolynomial_val_on_domain(
2640 __isl_take isl_space *domain, __isl_take isl_val *val)
2641{
2642 isl_qpolynomial *qp;
2643 isl_poly_cst *cst;
2644
2645 qp = isl_qpolynomial_zero_on_domain(domain);
2646 if (!qp || !val)
2647 goto error;
2648
2649 cst = isl_poly_as_cst(qp->poly);
2650 isl_int_set(cst->n, val->n)isl_sioimath_set((cst->n), *(val->n));
2651 isl_int_set(cst->d, val->d)isl_sioimath_set((cst->d), *(val->d));
2652
2653 isl_val_free(val);
2654 return qp;
2655error:
2656 isl_val_free(val);
2657 isl_qpolynomial_free(qp);
2658 return NULL((void*)0);
2659}
2660
2661static isl_stat poly_set_active(__isl_keep isl_poly *poly, int *active, int d)
2662{
2663 isl_bool is_cst;
2664 isl_poly_rec *rec;
2665 int i;
2666
2667 is_cst = isl_poly_is_cst(poly);
2668 if (is_cst < 0)
2669 return isl_stat_error;
2670 if (is_cst)
2671 return isl_stat_ok;
2672
2673 if (poly->var < d)
2674 active[poly->var] = 1;
2675
2676 rec = isl_poly_as_rec(poly);
2677 for (i = 0; i < rec->n; ++i)
2678 if (poly_set_active(rec->p[i], active, d) < 0)
2679 return isl_stat_error;
2680
2681 return isl_stat_ok;
2682}
2683
2684static isl_stat set_active(__isl_keep isl_qpolynomial *qp, int *active)
2685{
2686 int i, j;
2687 isl_size d;
2688 isl_space *space;
2689
2690 space = isl_qpolynomial_peek_domain_space(qp);
2691 d = isl_space_dim(space, isl_dim_all);
2692 if (d < 0 || !active)
2693 return isl_stat_error;
2694
2695 for (i = 0; i < d; ++i)
2696 for (j = 0; j < qp->div->n_row; ++j) {
2697 if (isl_int_is_zero(qp->div->row[j][2 + i])(isl_sioimath_sgn(*(qp->div->row[j][2 + i])) == 0))
2698 continue;
2699 active[i] = 1;
2700 break;
2701 }
2702
2703 return poly_set_active(qp->poly, active, d);
2704}
2705
2706#undef TYPEisl_term
2707#define TYPEisl_term isl_qpolynomial
2708static
2709#include "check_type_range_templ.c"
2710
2711isl_bool isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2712 enum isl_dim_type type, unsigned first, unsigned n)
2713{
2714 int i;
2715 int *active = NULL((void*)0);
2716 isl_bool involves = isl_bool_false;
2717 isl_size offset;
2718 isl_size d;
2719 isl_space *space;
2720
2721 if (!qp)
2722 return isl_bool_error;
2723 if (n == 0)
2724 return isl_bool_false;
2725
2726 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
2727 return isl_bool_error;
2728 isl_assert(qp->dim->ctx, type == isl_dim_param ||do { if (type == isl_dim_param || type == isl_dim_in) break; do
{ isl_handle_error(qp->dim->ctx, isl_error_unknown, "Assertion \""
"type == isl_dim_param || type == isl_dim_in" "\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 2729); return isl_bool_error; } while (0); } while (0)
2729 type == isl_dim_in, return isl_bool_error)do { if (type == isl_dim_param || type == isl_dim_in) break; do
{ isl_handle_error(qp->dim->ctx, isl_error_unknown, "Assertion \""
"type == isl_dim_param || type == isl_dim_in" "\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 2729); return isl_bool_error; } while (0); } while (0)
;
2730
2731 space = isl_qpolynomial_peek_domain_space(qp);
2732 d = isl_space_dim(space, isl_dim_all);
2733 if (d < 0)
2734 return isl_bool_error;
2735 active = isl_calloc_array(qp->dim->ctx, int, d)((int *)isl_calloc_or_die(qp->dim->ctx, d, sizeof(int))
)
;
2736 if (set_active(qp, active) < 0)
2737 goto error;
2738
2739 offset = isl_qpolynomial_domain_var_offset(qp, domain_type(type));
2740 if (offset < 0)
2741 goto error;
2742 first += offset;
2743 for (i = 0; i < n; ++i)
2744 if (active[first + i]) {
2745 involves = isl_bool_true;
2746 break;
2747 }
2748
2749 free(active);
2750
2751 return involves;
2752error:
2753 free(active);
2754 return isl_bool_error;
2755}
2756
2757/* Remove divs that do not appear in the quasi-polynomial, nor in any
2758 * of the divs that do appear in the quasi-polynomial.
2759 */
2760static __isl_give isl_qpolynomial *remove_redundant_divs(
2761 __isl_take isl_qpolynomial *qp)
2762{
2763 int i, j;
2764 isl_size div_pos;
2765 int len;
2766 int skip;
2767 int *active = NULL((void*)0);
2768 int *reordering = NULL((void*)0);
2769 int redundant = 0;
2770 int n_div;
2771 isl_ctx *ctx;
2772
2773 if (!qp)
2774 return NULL((void*)0);
2775 if (qp->div->n_row == 0)
2776 return qp;
2777
2778 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
2779 if (div_pos < 0)
2780 return isl_qpolynomial_free(qp);
2781 len = qp->div->n_col - 2;
2782 ctx = isl_qpolynomial_get_ctx(qp);
2783 active = isl_calloc_array(ctx, int, len)((int *)isl_calloc_or_die(ctx, len, sizeof(int)));
2784 if (!active)
2785 goto error;
2786
2787 if (poly_set_active(qp->poly, active, len) < 0)
2788 goto error;
2789
2790 for (i = qp->div->n_row - 1; i >= 0; --i) {
2791 if (!active[div_pos + i]) {
2792 redundant = 1;
2793 continue;
2794 }
2795 for (j = 0; j < i; ++j) {
2796 if (isl_int_is_zero(qp->div->row[i][2 + div_pos + j])(isl_sioimath_sgn(*(qp->div->row[i][2 + div_pos + j])) ==
0)
)
2797 continue;
2798 active[div_pos + j] = 1;
2799 break;
2800 }
2801 }
2802
2803 if (!redundant) {
2804 free(active);
2805 return qp;
2806 }
2807
2808 reordering = isl_alloc_array(qp->div->ctx, int, len)((int *)isl_malloc_or_die(qp->div->ctx, (len)*sizeof(int
)))
;
2809 if (!reordering)
2810 goto error;
2811
2812 for (i = 0; i < div_pos; ++i)
2813 reordering[i] = i;
2814
2815 skip = 0;
2816 n_div = qp->div->n_row;
2817 for (i = 0; i < n_div; ++i) {
2818 if (!active[div_pos + i]) {
2819 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2820 qp->div = isl_mat_drop_cols(qp->div,
2821 2 + div_pos + i - skip, 1);
2822 skip++;
2823 }
2824 reordering[div_pos + i] = div_pos + i - skip;
2825 }
2826
2827 qp->poly = reorder(qp->poly, reordering);
2828
2829 if (!qp->poly || !qp->div)
2830 goto error;
2831
2832 free(active);
2833 free(reordering);
2834
2835 return qp;
2836error:
2837 free(active);
2838 free(reordering);
2839 isl_qpolynomial_free(qp);
2840 return NULL((void*)0);
2841}
2842
2843__isl_give isl_poly *isl_poly_drop(__isl_take isl_poly *poly,
2844 unsigned first, unsigned n)
2845{
2846 int i;
2847 isl_poly_rec *rec;
2848
2849 if (!poly)
2850 return NULL((void*)0);
2851 if (n == 0 || poly->var < 0 || poly->var < first)
2852 return poly;
2853 if (poly->var < first + n) {
2854 poly = replace_by_constant_term(poly);
2855 return isl_poly_drop(poly, first, n);
2856 }
2857 poly = isl_poly_cow(poly);
2858 if (!poly)
2859 return NULL((void*)0);
2860 poly->var -= n;
2861 rec = isl_poly_as_rec(poly);
2862 if (!rec)
2863 goto error;
2864
2865 for (i = 0; i < rec->n; ++i) {
2866 rec->p[i] = isl_poly_drop(rec->p[i], first, n);
2867 if (!rec->p[i])
2868 goto error;
2869 }
2870
2871 return poly;
2872error:
2873 isl_poly_free(poly);
2874 return NULL((void*)0);
2875}
2876
2877__isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2878 __isl_take isl_qpolynomial *qp,
2879 enum isl_dim_type type, unsigned pos, const char *s)
2880{
2881 qp = isl_qpolynomial_cow(qp);
2882 if (!qp)
2883 return NULL((void*)0);
2884 if (type == isl_dim_out)
2885 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,do { isl_handle_error(isl_qpolynomial_get_ctx(qp), isl_error_invalid
, "cannot set name of output/set dimension", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 2887); return isl_qpolynomial_free(qp); } while (0)
2886 "cannot set name of output/set dimension",do { isl_handle_error(isl_qpolynomial_get_ctx(qp), isl_error_invalid
, "cannot set name of output/set dimension", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 2887); return isl_qpolynomial_free(qp); } while (0)
2887 return isl_qpolynomial_free(qp))do { isl_handle_error(isl_qpolynomial_get_ctx(qp), isl_error_invalid
, "cannot set name of output/set dimension", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 2887); return isl_qpolynomial_free(qp); } while (0)
;
2888 type = domain_type(type);
2889 qp->dim = isl_space_set_dim_name(qp->dim, type, pos, s);
2890 if (!qp->dim)
2891 goto error;
2892 return qp;
2893error:
2894 isl_qpolynomial_free(qp);
2895 return NULL((void*)0);
2896}
2897
2898__isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2899 __isl_take isl_qpolynomial *qp,
2900 enum isl_dim_type type, unsigned first, unsigned n)
2901{
2902 isl_size offset;
2903
2904 if (!qp)
2905 return NULL((void*)0);
2906 if (type == isl_dim_out)
2907 isl_die(qp->dim->ctx, isl_error_invalid,do { isl_handle_error(qp->dim->ctx, isl_error_invalid, "cannot drop output/set dimension"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 2909); goto error; } while (0)
2908 "cannot drop output/set dimension",do { isl_handle_error(qp->dim->ctx, isl_error_invalid, "cannot drop output/set dimension"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 2909); goto error; } while (0)
2909 goto error)do { isl_handle_error(qp->dim->ctx, isl_error_invalid, "cannot drop output/set dimension"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 2909); goto error; } while (0)
;
2910 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
2911 return isl_qpolynomial_free(qp);
2912 type = domain_type(type);
2913 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
2914 return qp;
2915
2916 qp = isl_qpolynomial_cow(qp);
2917 if (!qp)
2918 return NULL((void*)0);
2919
2920 isl_assert(qp->dim->ctx, type == isl_dim_param ||do { if (type == isl_dim_param || type == isl_dim_set) break;
do { isl_handle_error(qp->dim->ctx, isl_error_unknown,
"Assertion \"" "type == isl_dim_param || type == isl_dim_set"
"\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 2921); goto error; } while (0); } while (0)
2921 type == isl_dim_set, goto error)do { if (type == isl_dim_param || type == isl_dim_set) break;
do { isl_handle_error(qp->dim->ctx, isl_error_unknown,
"Assertion \"" "type == isl_dim_param || type == isl_dim_set"
"\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 2921); goto error; } while (0); } while (0)
;
2922
2923 qp->dim = isl_space_drop_dims(qp->dim, type, first, n);
2924 if (!qp->dim)
2925 goto error;
2926
2927 offset = isl_qpolynomial_domain_var_offset(qp, type);
2928 if (offset < 0)
2929 goto error;
2930 first += offset;
2931
2932 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2933 if (!qp->div)
2934 goto error;
2935
2936 qp->poly = isl_poly_drop(qp->poly, first, n);
2937 if (!qp->poly)
2938 goto error;
2939
2940 return qp;
2941error:
2942 isl_qpolynomial_free(qp);
2943 return NULL((void*)0);
2944}
2945
2946/* Project the domain of the quasi-polynomial onto its parameter space.
2947 * The quasi-polynomial may not involve any of the domain dimensions.
2948 */
2949__isl_give isl_qpolynomial *isl_qpolynomial_project_domain_on_params(
2950 __isl_take isl_qpolynomial *qp)
2951{
2952 isl_space *space;
2953 isl_size n;
2954 isl_bool involves;
2955
2956 n = isl_qpolynomial_dim(qp, isl_dim_in);
2957 if (n < 0)
2958 return isl_qpolynomial_free(qp);
2959 involves = isl_qpolynomial_involves_dims(qp, isl_dim_in, 0, n);
2960 if (involves < 0)
2961 return isl_qpolynomial_free(qp);
2962 if (involves)
2963 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,do { isl_handle_error(isl_qpolynomial_get_ctx(qp), isl_error_invalid
, "polynomial involves some of the domain dimensions", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 2965); return isl_qpolynomial_free(qp); } while (0)
2964 "polynomial involves some of the domain dimensions",do { isl_handle_error(isl_qpolynomial_get_ctx(qp), isl_error_invalid
, "polynomial involves some of the domain dimensions", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 2965); return isl_qpolynomial_free(qp); } while (0)
2965 return isl_qpolynomial_free(qp))do { isl_handle_error(isl_qpolynomial_get_ctx(qp), isl_error_invalid
, "polynomial involves some of the domain dimensions", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 2965); return isl_qpolynomial_free(qp); } while (0)
;
2966 qp = isl_qpolynomial_drop_dims(qp, isl_dim_in, 0, n);
2967 space = isl_qpolynomial_get_domain_space(qp);
2968 space = isl_space_params(space);
2969 qp = isl_qpolynomial_reset_domain_space(qp, space);
2970 return qp;
2971}
2972
2973static __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities_lifted(
2974 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_setisl_basic_map *eq)
2975{
2976 int i, j, k;
2977 isl_int denom;
2978 unsigned total;
2979 unsigned n_div;
2980 isl_poly *poly;
2981
2982 if (!eq)
2983 goto error;
2984 if (eq->n_eq == 0) {
2985 isl_basic_set_free(eq);
2986 return qp;
2987 }
2988
2989 qp = isl_qpolynomial_cow(qp);
2990 if (!qp)
2991 goto error;
2992 qp->div = isl_mat_cow(qp->div);
2993 if (!qp->div)
2994 goto error;
2995
2996 total = isl_basic_set_offset(eq, isl_dim_div);
2997 n_div = eq->n_div;
2998 isl_int_init(denom)isl_sioimath_init((denom));
2999 for (i = 0; i < eq->n_eq; ++i) {
3000 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
3001 if (j < 0 || j == 0 || j >= total)
3002 continue;
3003
3004 for (k = 0; k < qp->div->n_row; ++k) {
3005 if (isl_int_is_zero(qp->div->row[k][1 + j])(isl_sioimath_sgn(*(qp->div->row[k][1 + j])) == 0))
3006 continue;
3007 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
3008 &qp->div->row[k][0]);
3009 normalize_div(qp, k);
3010 }
3011
3012 if (isl_int_is_pos(eq->eq[i][j])(isl_sioimath_sgn(*(eq->eq[i][j])) > 0))
3013 isl_seq_neg(eq->eq[i], eq->eq[i], total);
3014 isl_int_abs(denom, eq->eq[i][j])isl_sioimath_abs((denom), *(eq->eq[i][j]));
3015 isl_int_set_si(eq->eq[i][j], 0)isl_sioimath_set_si((eq->eq[i][j]), 0);
3016
3017 poly = isl_poly_from_affine(qp->dim->ctx,
3018 eq->eq[i], denom, total);
3019 qp->poly = isl_poly_subs(qp->poly, j - 1, 1, &poly);
3020 isl_poly_free(poly);
3021 }
3022 isl_int_clear(denom)isl_sioimath_clear((denom));
3023
3024 if (!qp->poly)
3025 goto error;
3026
3027 isl_basic_set_free(eq);
3028
3029 qp = substitute_non_divs(qp);
3030 qp = sort_divs(qp);
3031
3032 return qp;
3033error:
3034 isl_basic_set_free(eq);
3035 isl_qpolynomial_free(qp);
3036 return NULL((void*)0);
3037}
3038
3039/* Exploit the equalities in "eq" to simplify the quasi-polynomial.
3040 */
3041__isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
3042 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_setisl_basic_map *eq)
3043{
3044 if (!qp || !eq)
3045 goto error;
3046 if (qp->div->n_row > 0)
3047 eq = isl_basic_set_add_dims(eq, isl_dim_set, qp->div->n_row);
3048 return isl_qpolynomial_substitute_equalities_lifted(qp, eq);
3049error:
3050 isl_basic_set_free(eq);
3051 isl_qpolynomial_free(qp);
3052 return NULL((void*)0);
3053}
3054
3055/* Look for equalities among the variables shared by context and qp
3056 * and the integer divisions of qp, if any.
3057 * The equalities are then used to eliminate variables and/or integer
3058 * divisions from qp.
3059 */
3060__isl_give isl_qpolynomial *isl_qpolynomial_gist(
3061 __isl_take isl_qpolynomial *qp, __isl_take isl_setisl_map *context)
3062{
3063 isl_local_space *ls;
3064 isl_basic_setisl_basic_map *aff;
3065
3066 ls = isl_qpolynomial_get_domain_local_space(qp);
3067 context = isl_local_space_lift_set(ls, context);
3068
3069 aff = isl_set_affine_hull(context);
3070 return isl_qpolynomial_substitute_equalities_lifted(qp, aff);
3071}
3072
3073__isl_give isl_qpolynomial *isl_qpolynomial_gist_params(
3074 __isl_take isl_qpolynomial *qp, __isl_take isl_setisl_map *context)
3075{
3076 isl_space *space = isl_qpolynomial_get_domain_space(qp);
3077 isl_setisl_map *dom_context = isl_set_universe(space);
3078 dom_context = isl_set_intersect_params(dom_context, context);
3079 return isl_qpolynomial_gist(qp, dom_context);
3080}
3081
3082/* Return a zero isl_qpolynomial in the given space.
3083 *
3084 * This is a helper function for isl_pw_*_as_* that ensures a uniform
3085 * interface over all piecewise types.
3086 */
3087static __isl_give isl_qpolynomial *isl_qpolynomial_zero_in_space(
3088 __isl_take isl_space *space)
3089{
3090 return isl_qpolynomial_zero_on_domain(isl_space_domain(space));
3091}
3092
3093#define isl_qpolynomial_involves_nanisl_qpolynomial_is_nan isl_qpolynomial_is_nan
3094
3095#undef PWisl_pw_qpolynomial
3096#define PWisl_pw_qpolynomial isl_pw_qpolynomial
3097#undef BASEpw_qpolynomial
3098#define BASEpw_qpolynomial qpolynomial
3099#undef EL_IS_ZEROis_zero
3100#define EL_IS_ZEROis_zero is_zero
3101#undef ZEROzero
3102#define ZEROzero zero
3103#undef IS_ZEROis_zero
3104#define IS_ZEROis_zero is_zero
3105#undef FIELDqp
3106#define FIELDqp qp
3107#undef DEFAULT_IS_ZERO1
3108#define DEFAULT_IS_ZERO1 1
3109
3110#include <isl_pw_templ.c>
3111#include <isl_pw_eval.c>
3112#include <isl_pw_insert_dims_templ.c>
3113#include <isl_pw_lift_templ.c>
3114#include <isl_pw_morph_templ.c>
3115#include <isl_pw_move_dims_templ.c>
3116#include <isl_pw_neg_templ.c>
3117#include <isl_pw_opt_templ.c>
3118#include <isl_pw_sub_templ.c>
3119
3120#undef BASEpw_qpolynomial
3121#define BASEpw_qpolynomial pw_qpolynomial
3122
3123#include <isl_union_single.c>
3124#include <isl_union_eval.c>
3125#include <isl_union_neg.c>
3126
3127int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
3128{
3129 if (!pwqp)
3130 return -1;
3131
3132 if (pwqp->n != -1)
3133 return 0;
3134
3135 if (!isl_set_plain_is_universe(pwqp->p[0].set))
3136 return 0;
3137
3138 return isl_qpolynomial_is_one(pwqp->p[0].qp);
3139}
3140
3141__isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add(
3142 __isl_take isl_pw_qpolynomial *pwqp1,
3143 __isl_take isl_pw_qpolynomial *pwqp2)
3144{
3145 return isl_pw_qpolynomial_union_add_(pwqp1, pwqp2);
3146}
3147
3148__isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
3149 __isl_take isl_pw_qpolynomial *pwqp1,
3150 __isl_take isl_pw_qpolynomial *pwqp2)
3151{
3152 int i, j, n;
3153 struct isl_pw_qpolynomial *res;
3154
3155 if (!pwqp1 || !pwqp2)
3156 goto error;
3157
3158 isl_assert(pwqp1->dim->ctx, isl_space_is_equal(pwqp1->dim, pwqp2->dim),do { if (isl_space_is_equal(pwqp1->dim, pwqp2->dim)) break
; do { isl_handle_error(pwqp1->dim->ctx, isl_error_unknown
, "Assertion \"" "isl_space_is_equal(pwqp1->dim, pwqp2->dim)"
"\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3159); goto error; } while (0); } while (0)
3159 goto error)do { if (isl_space_is_equal(pwqp1->dim, pwqp2->dim)) break
; do { isl_handle_error(pwqp1->dim->ctx, isl_error_unknown
, "Assertion \"" "isl_space_is_equal(pwqp1->dim, pwqp2->dim)"
"\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3159); goto error; } while (0); } while (0)
;
3160
3161 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
3162 isl_pw_qpolynomial_free(pwqp2);
3163 return pwqp1;
3164 }
3165
3166 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
3167 isl_pw_qpolynomial_free(pwqp1);
3168 return pwqp2;
3169 }
3170
3171 if (isl_pw_qpolynomial_is_one(pwqp1)) {
3172 isl_pw_qpolynomial_free(pwqp1);
3173 return pwqp2;
3174 }
3175
3176 if (isl_pw_qpolynomial_is_one(pwqp2)) {
3177 isl_pw_qpolynomial_free(pwqp2);
3178 return pwqp1;
3179 }
3180
3181 n = pwqp1->n * pwqp2->n;
3182 res = isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1->dim), n);
3183
3184 for (i = 0; i < pwqp1->n; ++i) {
3185 for (j = 0; j < pwqp2->n; ++j) {
3186 struct isl_setisl_map *common;
3187 struct isl_qpolynomial *prod;
3188 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
3189 isl_set_copy(pwqp2->p[j].set));
3190 if (isl_set_plain_is_empty(common)) {
3191 isl_set_free(common);
3192 continue;
3193 }
3194
3195 prod = isl_qpolynomial_mul(
3196 isl_qpolynomial_copy(pwqp1->p[i].qp),
3197 isl_qpolynomial_copy(pwqp2->p[j].qp));
3198
3199 res = isl_pw_qpolynomial_add_piece(res, common, prod);
3200 }
3201 }
3202
3203 isl_pw_qpolynomial_free(pwqp1);
3204 isl_pw_qpolynomial_free(pwqp2);
3205
3206 return res;
3207error:
3208 isl_pw_qpolynomial_free(pwqp1);
3209 isl_pw_qpolynomial_free(pwqp2);
3210 return NULL((void*)0);
3211}
3212
3213__isl_give isl_val *isl_poly_eval(__isl_take isl_poly *poly,
3214 __isl_take isl_vec *vec)
3215{
3216 int i;
3217 isl_bool is_cst;
3218 isl_poly_rec *rec;
3219 isl_val *res;
3220 isl_val *base;
3221
3222 is_cst = isl_poly_is_cst(poly);
3223 if (is_cst < 0)
3224 goto error;
3225 if (is_cst) {
3226 isl_vec_free(vec);
3227 res = isl_poly_get_constant_val(poly);
3228 isl_poly_free(poly);
3229 return res;
3230 }
3231
3232 rec = isl_poly_as_rec(poly);
3233 if (!rec || !vec)
3234 goto error;
3235
3236 isl_assert(poly->ctx, rec->n >= 1, goto error)do { if (rec->n >= 1) break; do { isl_handle_error(poly
->ctx, isl_error_unknown, "Assertion \"" "rec->n >= 1"
"\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3236); goto error; } while (0); } while (0)
;
3237
3238 base = isl_val_rat_from_isl_int(poly->ctx,
3239 vec->el[1 + poly->var], vec->el[0]);
3240
3241 res = isl_poly_eval(isl_poly_copy(rec->p[rec->n - 1]),
3242 isl_vec_copy(vec));
3243
3244 for (i = rec->n - 2; i >= 0; --i) {
3245 res = isl_val_mul(res, isl_val_copy(base));
3246 res = isl_val_add(res, isl_poly_eval(isl_poly_copy(rec->p[i]),
3247 isl_vec_copy(vec)));
3248 }
3249
3250 isl_val_free(base);
3251 isl_poly_free(poly);
3252 isl_vec_free(vec);
3253 return res;
3254error:
3255 isl_poly_free(poly);
3256 isl_vec_free(vec);
3257 return NULL((void*)0);
3258}
3259
3260/* Evaluate "qp" in the void point "pnt".
3261 * In particular, return the value NaN.
3262 */
3263static __isl_give isl_val *eval_void(__isl_take isl_qpolynomial *qp,
3264 __isl_take isl_point *pnt)
3265{
3266 isl_ctx *ctx;
3267
3268 ctx = isl_point_get_ctx(pnt);
3269 isl_qpolynomial_free(qp);
3270 isl_point_free(pnt);
3271 return isl_val_nan(ctx);
3272}
3273
3274__isl_give isl_val *isl_qpolynomial_eval(__isl_take isl_qpolynomial *qp,
3275 __isl_take isl_point *pnt)
3276{
3277 isl_bool is_void;
3278 isl_vec *ext;
3279 isl_val *v;
3280
3281 if (!qp || !pnt)
3282 goto error;
3283 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, qp->dim), goto error)do { if (isl_space_is_equal(pnt->dim, qp->dim)) break; do
{ isl_handle_error(pnt->dim->ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(pnt->dim, qp->dim)" "\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3283); goto error; } while (0); } while (0)
;
3284 is_void = isl_point_is_void(pnt);
3285 if (is_void < 0)
3286 goto error;
3287 if (is_void)
3288 return eval_void(qp, pnt);
3289
3290 ext = isl_local_extend_point_vec(qp->div, isl_vec_copy(pnt->vec));
3291
3292 v = isl_poly_eval(isl_poly_copy(qp->poly), ext);
3293
3294 isl_qpolynomial_free(qp);
3295 isl_point_free(pnt);
3296
3297 return v;
3298error:
3299 isl_qpolynomial_free(qp);
3300 isl_point_free(pnt);
3301 return NULL((void*)0);
3302}
3303
3304int isl_poly_cmp(__isl_keep isl_poly_cst *cst1, __isl_keep isl_poly_cst *cst2)
3305{
3306 int cmp;
3307 isl_int t;
3308 isl_int_init(t)isl_sioimath_init((t));
3309 isl_int_mul(t, cst1->n, cst2->d)isl_sioimath_mul((t), *(cst1->n), *(cst2->d));
3310 isl_int_submul(t, cst2->n, cst1->d)isl_sioimath_submul((t), *(cst2->n), *(cst1->d));
3311 cmp = isl_int_sgn(t)isl_sioimath_sgn(*(t));
3312 isl_int_clear(t)isl_sioimath_clear((t));
3313 return cmp;
3314}
3315
3316__isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
3317 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
3318 unsigned first, unsigned n)
3319{
3320 unsigned total;
3321 unsigned g_pos;
3322 int *exp;
3323
3324 if (!qp)
3325 return NULL((void*)0);
3326 if (type == isl_dim_out)
3327 isl_die(qp->div->ctx, isl_error_invalid,do { isl_handle_error(qp->div->ctx, isl_error_invalid, "cannot insert output/set dimensions"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3329); goto error; } while (0)
3328 "cannot insert output/set dimensions",do { isl_handle_error(qp->div->ctx, isl_error_invalid, "cannot insert output/set dimensions"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3329); goto error; } while (0)
3329 goto error)do { isl_handle_error(qp->div->ctx, isl_error_invalid, "cannot insert output/set dimensions"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3329); goto error; } while (0)
;
3330 if (isl_qpolynomial_check_range(qp, type, first, 0) < 0)
3331 return isl_qpolynomial_free(qp);
3332 type = domain_type(type);
3333 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
3334 return qp;
3335
3336 qp = isl_qpolynomial_cow(qp);
3337 if (!qp)
3338 return NULL((void*)0);
3339
3340 g_pos = pos(qp->dim, type) + first;
3341
3342 qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
3343 if (!qp->div)
3344 goto error;
3345
3346 total = qp->div->n_col - 2;
3347 if (total > g_pos) {
3348 int i;
3349 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos)((int *)isl_malloc_or_die(qp->div->ctx, (total - g_pos)
*sizeof(int)))
;
3350 if (!exp)
3351 goto error;
3352 for (i = 0; i < total - g_pos; ++i)
3353 exp[i] = i + n;
3354 qp->poly = expand(qp->poly, exp, g_pos);
3355 free(exp);
3356 if (!qp->poly)
3357 goto error;
3358 }
3359
3360 qp->dim = isl_space_insert_dims(qp->dim, type, first, n);
3361 if (!qp->dim)
3362 goto error;
3363
3364 return qp;
3365error:
3366 isl_qpolynomial_free(qp);
3367 return NULL((void*)0);
3368}
3369
3370__isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
3371 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
3372{
3373 isl_size pos;
3374
3375 pos = isl_qpolynomial_dim(qp, type);
3376 if (pos < 0)
3377 return isl_qpolynomial_free(qp);
3378
3379 return isl_qpolynomial_insert_dims(qp, type, pos, n);
3380}
3381
3382static int *reordering_move(isl_ctx *ctx,
3383 unsigned len, unsigned dst, unsigned src, unsigned n)
3384{
3385 int i;
3386 int *reordering;
3387
3388 reordering = isl_alloc_array(ctx, int, len)((int *)isl_malloc_or_die(ctx, (len)*sizeof(int)));
3389 if (!reordering)
3390 return NULL((void*)0);
3391
3392 if (dst <= src) {
3393 for (i = 0; i < dst; ++i)
3394 reordering[i] = i;
3395 for (i = 0; i < n; ++i)
3396 reordering[src + i] = dst + i;
3397 for (i = 0; i < src - dst; ++i)
3398 reordering[dst + i] = dst + n + i;
3399 for (i = 0; i < len - src - n; ++i)
3400 reordering[src + n + i] = src + n + i;
3401 } else {
3402 for (i = 0; i < src; ++i)
3403 reordering[i] = i;
3404 for (i = 0; i < n; ++i)
3405 reordering[src + i] = dst + i;
3406 for (i = 0; i < dst - src; ++i)
3407 reordering[src + n + i] = src + i;
3408 for (i = 0; i < len - dst - n; ++i)
3409 reordering[dst + n + i] = dst + n + i;
3410 }
3411
3412 return reordering;
3413}
3414
3415__isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
3416 __isl_take isl_qpolynomial *qp,
3417 enum isl_dim_type dst_type, unsigned dst_pos,
3418 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3419{
3420 unsigned g_dst_pos;
3421 unsigned g_src_pos;
3422 int *reordering;
3423
3424 if (!qp)
3425 return NULL((void*)0);
3426
3427 if (dst_type == isl_dim_out || src_type == isl_dim_out)
3428 isl_die(qp->dim->ctx, isl_error_invalid,do { isl_handle_error(qp->dim->ctx, isl_error_invalid, "cannot move output/set dimension"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3430); goto error; } while (0)
3429 "cannot move output/set dimension",do { isl_handle_error(qp->dim->ctx, isl_error_invalid, "cannot move output/set dimension"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3430); goto error; } while (0)
3430 goto error)do { isl_handle_error(qp->dim->ctx, isl_error_invalid, "cannot move output/set dimension"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3430); goto error; } while (0)
;
3431 if (isl_qpolynomial_check_range(qp, src_type, src_pos, n) < 0)
3432 return isl_qpolynomial_free(qp);
3433 if (dst_type == isl_dim_in)
3434 dst_type = isl_dim_set;
3435 if (src_type == isl_dim_in)
3436 src_type = isl_dim_set;
3437
3438 if (n == 0 &&
3439 !isl_space_is_named_or_nested(qp->dim, src_type) &&
3440 !isl_space_is_named_or_nested(qp->dim, dst_type))
3441 return qp;
3442
3443 qp = isl_qpolynomial_cow(qp);
3444 if (!qp)
3445 return NULL((void*)0);
3446
3447 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
3448 g_src_pos = pos(qp->dim, src_type) + src_pos;
3449 if (dst_type > src_type)
3450 g_dst_pos -= n;
3451
3452 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
3453 if (!qp->div)
3454 goto error;
3455 qp = sort_divs(qp);
3456 if (!qp)
3457 goto error;
3458
3459 reordering = reordering_move(qp->dim->ctx,
3460 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
3461 if (!reordering)
3462 goto error;
3463
3464 qp->poly = reorder(qp->poly, reordering);
3465 free(reordering);
3466 if (!qp->poly)
3467 goto error;
3468
3469 qp->dim = isl_space_move_dims(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
3470 if (!qp->dim)
3471 goto error;
3472
3473 return qp;
3474error:
3475 isl_qpolynomial_free(qp);
3476 return NULL((void*)0);
3477}
3478
3479__isl_give isl_qpolynomial *isl_qpolynomial_from_affine(
3480 __isl_take isl_space *space, isl_int *f, isl_int denom)
3481{
3482 isl_size d;
3483 isl_poly *poly;
3484
3485 space = isl_space_domain(space);
3486 if (!space)
3487 return NULL((void*)0);
3488
3489 d = isl_space_dim(space, isl_dim_all);
3490 poly = d < 0 ? NULL((void*)0) : isl_poly_from_affine(space->ctx, f, denom, 1 + d);
3491
3492 return isl_qpolynomial_alloc(space, 0, poly);
3493}
3494
3495__isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
3496{
3497 isl_ctx *ctx;
3498 isl_poly *poly;
3499 isl_qpolynomial *qp;
3500
3501 if (!aff)
3502 return NULL((void*)0);
3503
3504 ctx = isl_aff_get_ctx(aff);
3505 poly = isl_poly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
3506 aff->v->size - 1);
3507
3508 qp = isl_qpolynomial_alloc(isl_aff_get_domain_space(aff),
3509 aff->ls->div->n_row, poly);
3510 if (!qp)
3511 goto error;
3512
3513 isl_mat_free(qp->div);
3514 qp->div = isl_mat_copy(aff->ls->div);
3515 qp->div = isl_mat_cow(qp->div);
3516 if (!qp->div)
3517 goto error;
3518
3519 isl_aff_free(aff);
3520 qp = reduce_divs(qp);
3521 qp = remove_redundant_divs(qp);
3522 return qp;
3523error:
3524 isl_aff_free(aff);
3525 return isl_qpolynomial_free(qp);
3526}
3527
3528__isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_pw_aff(
3529 __isl_take isl_pw_aff *pwaff)
3530{
3531 int i;
3532 isl_pw_qpolynomial *pwqp;
3533
3534 if (!pwaff)
3535 return NULL((void*)0);
3536
3537 pwqp = isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff),
3538 pwaff->n);
3539
3540 for (i = 0; i < pwaff->n; ++i) {
3541 isl_setisl_map *dom;
3542 isl_qpolynomial *qp;
3543
3544 dom = isl_set_copy(pwaff->p[i].set);
3545 qp = isl_qpolynomial_from_aff(isl_aff_copy(pwaff->p[i].aff));
3546 pwqp = isl_pw_qpolynomial_add_piece(pwqp, dom, qp);
3547 }
3548
3549 isl_pw_aff_free(pwaff);
3550 return pwqp;
3551}
3552
3553__isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
3554 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
3555{
3556 isl_aff *aff;
3557
3558 aff = isl_constraint_get_bound(c, type, pos);
3559 isl_constraint_free(c);
3560 return isl_qpolynomial_from_aff(aff);
3561}
3562
3563/* For each 0 <= i < "n", replace variable "first" + i of type "type"
3564 * in "qp" by subs[i].
3565 */
3566__isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3567 __isl_take isl_qpolynomial *qp,
3568 enum isl_dim_type type, unsigned first, unsigned n,
3569 __isl_keep isl_qpolynomial **subs)
3570{
3571 int i;
3572 isl_poly **polys;
3573
3574 if (n == 0)
3575 return qp;
3576
3577 qp = isl_qpolynomial_cow(qp);
3578 if (!qp)
3579 return NULL((void*)0);
3580
3581 if (type == isl_dim_out)
3582 isl_die(qp->dim->ctx, isl_error_invalid,do { isl_handle_error(qp->dim->ctx, isl_error_invalid, "cannot substitute output/set dimension"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3584); goto error; } while (0)
3583 "cannot substitute output/set dimension",do { isl_handle_error(qp->dim->ctx, isl_error_invalid, "cannot substitute output/set dimension"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3584); goto error; } while (0)
3584 goto error)do { isl_handle_error(qp->dim->ctx, isl_error_invalid, "cannot substitute output/set dimension"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3584); goto error; } while (0)
;
3585 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
3586 return isl_qpolynomial_free(qp);
3587 type = domain_type(type);
3588
3589 for (i = 0; i < n; ++i)
3590 if (!subs[i])
3591 goto error;
3592
3593 for (i = 0; i < n; ++i)
3594 if (isl_qpolynomial_check_equal_space(qp, subs[i]) < 0)
3595 goto error;
3596
3597 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error)do { if (qp->div->n_row == 0) break; do { isl_handle_error
(qp->dim->ctx, isl_error_unknown, "Assertion \"" "qp->div->n_row == 0"
"\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3597); goto error; } while (0); } while (0)
;
3598 for (i = 0; i < n; ++i)
3599 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error)do { if (subs[i]->div->n_row == 0) break; do { isl_handle_error
(qp->dim->ctx, isl_error_unknown, "Assertion \"" "subs[i]->div->n_row == 0"
"\" failed", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3599); goto error; } while (0); } while (0)
;
3600
3601 first += pos(qp->dim, type);
3602
3603 polys = isl_alloc_array(qp->dim->ctx, struct isl_poly *, n)((struct isl_poly * *)isl_malloc_or_die(qp->dim->ctx, (
n)*sizeof(struct isl_poly *)))
;
3604 if (!polys)
3605 goto error;
3606 for (i = 0; i < n; ++i)
3607 polys[i] = subs[i]->poly;
3608
3609 qp->poly = isl_poly_subs(qp->poly, first, n, polys);
3610
3611 free(polys);
3612
3613 if (!qp->poly)
3614 goto error;
3615
3616 return qp;
3617error:
3618 isl_qpolynomial_free(qp);
3619 return NULL((void*)0);
3620}
3621
3622/* Extend "bset" with extra set dimensions for each integer division
3623 * in "qp" and then call "fn" with the extended bset and the polynomial
3624 * that results from replacing each of the integer divisions by the
3625 * corresponding extra set dimension.
3626 */
3627isl_stat isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3628 __isl_keep isl_basic_setisl_basic_map *bset,
3629 isl_stat (*fn)(__isl_take isl_basic_setisl_basic_map *bset,
3630 __isl_take isl_qpolynomial *poly, void *user), void *user)
3631{
3632 isl_space *space;
3633 isl_local_space *ls;
3634 isl_qpolynomial *poly;
3635
3636 if (!qp || !bset)
3637 return isl_stat_error;
3638 if (qp->div->n_row == 0)
3639 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3640 user);
3641
3642 space = isl_space_copy(qp->dim);
3643 space = isl_space_add_dims(space, isl_dim_set, qp->div->n_row);
3644 poly = isl_qpolynomial_alloc(space, 0, isl_poly_copy(qp->poly));
3645 bset = isl_basic_set_copy(bset);
3646 ls = isl_qpolynomial_get_domain_local_space(qp);
3647 bset = isl_local_space_lift_basic_set(ls, bset);
3648
3649 return fn(bset, poly, user);
3650}
3651
3652/* Return total degree in variables first (inclusive) up to last (exclusive).
3653 */
3654int isl_poly_degree(__isl_keep isl_poly *poly, int first, int last)
3655{
3656 int deg = -1;
3657 int i;
3658 isl_bool is_zero, is_cst;
3659 isl_poly_rec *rec;
3660
3661 is_zero = isl_poly_is_zero(poly);
3662 if (is_zero < 0)
3663 return -2;
3664 if (is_zero)
3665 return -1;
3666 is_cst = isl_poly_is_cst(poly);
3667 if (is_cst < 0)
3668 return -2;
3669 if (is_cst || poly->var < first)
3670 return 0;
3671
3672 rec = isl_poly_as_rec(poly);
3673 if (!rec)
3674 return -2;
3675
3676 for (i = 0; i < rec->n; ++i) {
3677 int d;
3678
3679 is_zero = isl_poly_is_zero(rec->p[i]);
3680 if (is_zero < 0)
3681 return -2;
3682 if (is_zero)
3683 continue;
3684 d = isl_poly_degree(rec->p[i], first, last);
3685 if (poly->var < last)
3686 d += i;
3687 if (d > deg)
3688 deg = d;
3689 }
3690
3691 return deg;
3692}
3693
3694/* Return total degree in set variables.
3695 */
3696int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3697{
3698 unsigned ovar;
3699 isl_size nvar;
3700
3701 if (!poly)
3702 return -2;
3703
3704 ovar = isl_space_offset(poly->dim, isl_dim_set);
3705 nvar = isl_space_dim(poly->dim, isl_dim_set);
3706 if (nvar < 0)
3707 return -2;
3708 return isl_poly_degree(poly->poly, ovar, ovar + nvar);
3709}
3710
3711__isl_give isl_poly *isl_poly_coeff(__isl_keep isl_poly *poly,
3712 unsigned pos, int deg)
3713{
3714 int i;
3715 isl_bool is_cst;
3716 isl_poly_rec *rec;
3717
3718 is_cst = isl_poly_is_cst(poly);
3719 if (is_cst < 0)
3720 return NULL((void*)0);
3721 if (is_cst || poly->var < pos) {
3722 if (deg == 0)
3723 return isl_poly_copy(poly);
3724 else
3725 return isl_poly_zero(poly->ctx);
3726 }
3727
3728 rec = isl_poly_as_rec(poly);
3729 if (!rec)
3730 return NULL((void*)0);
3731
3732 if (poly->var == pos) {
3733 if (deg < rec->n)
3734 return isl_poly_copy(rec->p[deg]);
3735 else
3736 return isl_poly_zero(poly->ctx);
3737 }
3738
3739 poly = isl_poly_copy(poly);
3740 poly = isl_poly_cow(poly);
3741 rec = isl_poly_as_rec(poly);
3742 if (!rec)
3743 goto error;
3744
3745 for (i = 0; i < rec->n; ++i) {
3746 isl_poly *t;
3747 t = isl_poly_coeff(rec->p[i], pos, deg);
3748 if (!t)
3749 goto error;
3750 isl_poly_free(rec->p[i]);
3751 rec->p[i] = t;
3752 }
3753
3754 return poly;
3755error:
3756 isl_poly_free(poly);
3757 return NULL((void*)0);
3758}
3759
3760/* Return coefficient of power "deg" of variable "t_pos" of type "type".
3761 */
3762__isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3763 __isl_keep isl_qpolynomial *qp,
3764 enum isl_dim_type type, unsigned t_pos, int deg)
3765{
3766 unsigned g_pos;
3767 isl_poly *poly;
3768 isl_qpolynomial *c;
3769
3770 if (!qp)
3771 return NULL((void*)0);
3772
3773 if (type == isl_dim_out)
3774 isl_die(qp->div->ctx, isl_error_invalid,do { isl_handle_error(qp->div->ctx, isl_error_invalid, "output/set dimension does not have a coefficient"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3776); return ((void*)0); } while (0)
3775 "output/set dimension does not have a coefficient",do { isl_handle_error(qp->div->ctx, isl_error_invalid, "output/set dimension does not have a coefficient"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3776); return ((void*)0); } while (0)
3776 return NULL)do { isl_handle_error(qp->div->ctx, isl_error_invalid, "output/set dimension does not have a coefficient"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 3776); return ((void*)0); } while (0)
;
3777 if (isl_qpolynomial_check_range(qp, type, t_pos, 1) < 0)
3778 return NULL((void*)0);
3779 type = domain_type(type);
3780
3781 g_pos = pos(qp->dim, type) + t_pos;
3782 poly = isl_poly_coeff(qp->poly, g_pos, deg);
3783
3784 c = isl_qpolynomial_alloc(isl_space_copy(qp->dim),
3785 qp->div->n_row, poly);
3786 if (!c)
3787 return NULL((void*)0);
3788 isl_mat_free(c->div);
3789 c->div = isl_mat_copy(qp->div);
3790 if (!c->div)
3791 goto error;
3792 return c;
3793error:
3794 isl_qpolynomial_free(c);
3795 return NULL((void*)0);
3796}
3797
3798/* Homogenize the polynomial in the variables first (inclusive) up to
3799 * last (exclusive) by inserting powers of variable first.
3800 * Variable first is assumed not to appear in the input.
3801 */
3802__isl_give isl_poly *isl_poly_homogenize(__isl_take isl_poly *poly, int deg,
3803 int target, int first, int last)
3804{
3805 int i;
3806 isl_bool is_zero, is_cst;
3807 isl_poly_rec *rec;
3808
3809 is_zero = isl_poly_is_zero(poly);
3810 if (is_zero < 0)
3811 return isl_poly_free(poly);
3812 if (is_zero)
3813 return poly;
3814 if (deg == target)
3815 return poly;
3816 is_cst = isl_poly_is_cst(poly);
3817 if (is_cst < 0)
3818 return isl_poly_free(poly);
3819 if (is_cst || poly->var < first) {
3820 isl_poly *hom;
3821
3822 hom = isl_poly_var_pow(poly->ctx, first, target - deg);
3823 if (!hom)
3824 goto error;
3825 rec = isl_poly_as_rec(hom);
3826 rec->p[target - deg] = isl_poly_mul(rec->p[target - deg], poly);
3827
3828 return hom;
3829 }
3830
3831 poly = isl_poly_cow(poly);
3832 rec = isl_poly_as_rec(poly);
3833 if (!rec)
3834 goto error;
3835
3836 for (i = 0; i < rec->n; ++i) {
3837 is_zero = isl_poly_is_zero(rec->p[i]);
3838 if (is_zero < 0)
3839 return isl_poly_free(poly);
3840 if (is_zero)
3841 continue;
3842 rec->p[i] = isl_poly_homogenize(rec->p[i],
3843 poly->var < last ? deg + i : i, target,
3844 first, last);
3845 if (!rec->p[i])
3846 goto error;
3847 }
3848
3849 return poly;
3850error:
3851 isl_poly_free(poly);
3852 return NULL((void*)0);
3853}
3854
3855/* Homogenize the polynomial in the set variables by introducing
3856 * powers of an extra set variable at position 0.
3857 */
3858__isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3859 __isl_take isl_qpolynomial *poly)
3860{
3861 unsigned ovar;
3862 isl_size nvar;
3863 int deg = isl_qpolynomial_degree(poly);
3864
3865 if (deg < -1)
3866 goto error;
3867
3868 poly = isl_qpolynomial_insert_dims(poly, isl_dim_in, 0, 1);
3869 poly = isl_qpolynomial_cow(poly);
3870 if (!poly)
3871 goto error;
3872
3873 ovar = isl_space_offset(poly->dim, isl_dim_set);
3874 nvar = isl_space_dim(poly->dim, isl_dim_set);
3875 if (nvar < 0)
3876 return isl_qpolynomial_free(poly);
3877 poly->poly = isl_poly_homogenize(poly->poly, 0, deg, ovar, ovar + nvar);
3878 if (!poly->poly)
3879 goto error;
3880
3881 return poly;
3882error:
3883 isl_qpolynomial_free(poly);
3884 return NULL((void*)0);
3885}
3886
3887__isl_give isl_term *isl_term_alloc(__isl_take isl_space *space,
3888 __isl_take isl_mat *div)
3889{
3890 isl_term *term;
3891 isl_size d;
3892 int n;
3893
3894 d = isl_space_dim(space, isl_dim_all);
3895 if (d < 0 || !div)
3896 goto error;
3897
3898 n = d + div->n_row;
3899
3900 term = isl_calloc(space->ctx, struct isl_term,((struct isl_term *)isl_calloc_or_die(space->ctx, 1, sizeof
(struct isl_term) + (n - 1) * sizeof(int)))
3901 sizeof(struct isl_term) + (n - 1) * sizeof(int))((struct isl_term *)isl_calloc_or_die(space->ctx, 1, sizeof
(struct isl_term) + (n - 1) * sizeof(int)))
;
3902 if (!term)
3903 goto error;
3904
3905 term->ref = 1;
3906 term->dim = space;
3907 term->div = div;
3908 isl_int_init(term->n)isl_sioimath_init((term->n));
3909 isl_int_init(term->d)isl_sioimath_init((term->d));
3910
3911 return term;
3912error:
3913 isl_space_free(space);
3914 isl_mat_free(div);
3915 return NULL((void*)0);
3916}
3917
3918__isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3919{
3920 if (!term)
3921 return NULL((void*)0);
3922
3923 term->ref++;
3924 return term;
3925}
3926
3927__isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3928{
3929 int i;
3930 isl_term *dup;
3931 isl_size total;
3932
3933 total = isl_term_dim(term, isl_dim_all);
3934 if (total < 0)
3935 return NULL((void*)0);
3936
3937 dup = isl_term_alloc(isl_space_copy(term->dim), isl_mat_copy(term->div));
3938 if (!dup)
3939 return NULL((void*)0);
3940
3941 isl_int_set(dup->n, term->n)isl_sioimath_set((dup->n), *(term->n));
3942 isl_int_set(dup->d, term->d)isl_sioimath_set((dup->d), *(term->d));
3943
3944 for (i = 0; i < total; ++i)
3945 dup->pow[i] = term->pow[i];
3946
3947 return dup;
3948}
3949
3950__isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3951{
3952 if (!term)
3953 return NULL((void*)0);
3954
3955 if (term->ref == 1)
3956 return term;
3957 term->ref--;
3958 return isl_term_dup(term);
3959}
3960
3961__isl_null isl_term *isl_term_free(__isl_take isl_term *term)
3962{
3963 if (!term)
3964 return NULL((void*)0);
3965
3966 if (--term->ref > 0)
3967 return NULL((void*)0);
3968
3969 isl_space_free(term->dim);
3970 isl_mat_free(term->div);
3971 isl_int_clear(term->n)isl_sioimath_clear((term->n));
3972 isl_int_clear(term->d)isl_sioimath_clear((term->d));
3973 free(term);
3974
3975 return NULL((void*)0);
3976}
3977
3978isl_size isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3979{
3980 isl_size dim;
3981
3982 if (!term)
3983 return isl_size_error((int) -1);
3984
3985 switch (type) {
3986 case isl_dim_param:
3987 case isl_dim_in:
3988 case isl_dim_out: return isl_space_dim(term->dim, type);
3989 case isl_dim_div: return term->div->n_row;
3990 case isl_dim_all: dim = isl_space_dim(term->dim, isl_dim_all);
3991 if (dim < 0)
3992 return isl_size_error((int) -1);
3993 return dim + term->div->n_row;
3994 default: return isl_size_error((int) -1);
3995 }
3996}
3997
3998/* Return the space of "term".
3999 */
4000static __isl_keep isl_space *isl_term_peek_space(__isl_keep isl_term *term)
4001{
4002 return term ? term->dim : NULL((void*)0);
4003}
4004
4005/* Return the offset of the first variable of type "type" within
4006 * the variables of "term".
4007 */
4008static isl_size isl_term_offset(__isl_keep isl_term *term,
4009 enum isl_dim_type type)
4010{
4011 isl_space *space;
4012
4013 space = isl_term_peek_space(term);
4014 if (!space)
4015 return isl_size_error((int) -1);
4016
4017 switch (type) {
4018 case isl_dim_param:
4019 case isl_dim_set: return isl_space_offset(space, type);
4020 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
4021 default:
4022 isl_die(isl_term_get_ctx(term), isl_error_invalid,do { isl_handle_error(isl_term_get_ctx(term), isl_error_invalid
, "invalid dimension type", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 4023); return ((int) -1); } while (0)
4023 "invalid dimension type", return isl_size_error)do { isl_handle_error(isl_term_get_ctx(term), isl_error_invalid
, "invalid dimension type", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 4023); return ((int) -1); } while (0)
;
4024 }
4025}
4026
4027isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
4028{
4029 return term ? term->dim->ctx : NULL((void*)0);
4030}
4031
4032void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
4033{
4034 if (!term)
4035 return;
4036 isl_int_set(*n, term->n)isl_sioimath_set((*n), *(term->n));
4037}
4038
4039/* Return the coefficient of the term "term".
4040 */
4041__isl_give isl_val *isl_term_get_coefficient_val(__isl_keep isl_term *term)
4042{
4043 if (!term)
4044 return NULL((void*)0);
4045
4046 return isl_val_rat_from_isl_int(isl_term_get_ctx(term),
4047 term->n, term->d);
4048}
4049
4050#undef TYPEisl_term
4051#define TYPEisl_term isl_term
4052static
4053#include "check_type_range_templ.c"
4054
4055isl_size isl_term_get_exp(__isl_keep isl_term *term,
4056 enum isl_dim_type type, unsigned pos)
4057{
4058 isl_size offset;
4059
4060 if (isl_term_check_range(term, type, pos, 1) < 0)
4061 return isl_size_error((int) -1);
4062 offset = isl_term_offset(term, type);
4063 if (offset < 0)
4064 return isl_size_error((int) -1);
4065
4066 return term->pow[offset + pos];
4067}
4068
4069__isl_give isl_aff *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
4070{
4071 isl_local_space *ls;
4072 isl_aff *aff;
4073
4074 if (isl_term_check_range(term, isl_dim_div, pos, 1) < 0)
4075 return NULL((void*)0);
4076
4077 ls = isl_local_space_alloc_div(isl_space_copy(term->dim),
4078 isl_mat_copy(term->div));
4079 aff = isl_aff_alloc(ls);
4080 if (!aff)
4081 return NULL((void*)0);
4082
4083 isl_seq_cpy(aff->v->el, term->div->row[pos], aff->v->size);
4084
4085 aff = isl_aff_normalize(aff);
4086
4087 return aff;
4088}
4089
4090__isl_give isl_term *isl_poly_foreach_term(__isl_keep isl_poly *poly,
4091 isl_stat (*fn)(__isl_take isl_term *term, void *user),
4092 __isl_take isl_term *term, void *user)
4093{
4094 int i;
4095 isl_bool is_zero, is_bad, is_cst;
4096 isl_poly_rec *rec;
4097
4098 is_zero = isl_poly_is_zero(poly);
4099 if (is_zero < 0 || !term)
4100 goto error;
4101
4102 if (is_zero)
4103 return term;
4104
4105 is_cst = isl_poly_is_cst(poly);
4106 is_bad = isl_poly_is_nan(poly);
4107 if (is_bad >= 0 && !is_bad)
4108 is_bad = isl_poly_is_infty(poly);
4109 if (is_bad >= 0 && !is_bad)
4110 is_bad = isl_poly_is_neginfty(poly);
4111 if (is_cst < 0 || is_bad < 0)
4112 return isl_term_free(term);
4113 if (is_bad)
4114 isl_die(isl_term_get_ctx(term), isl_error_invalid,do { isl_handle_error(isl_term_get_ctx(term), isl_error_invalid
, "cannot handle NaN/infty polynomial", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 4116); return isl_term_free(term); } while (0)
4115 "cannot handle NaN/infty polynomial",do { isl_handle_error(isl_term_get_ctx(term), isl_error_invalid
, "cannot handle NaN/infty polynomial", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 4116); return isl_term_free(term); } while (0)
4116 return isl_term_free(term))do { isl_handle_error(isl_term_get_ctx(term), isl_error_invalid
, "cannot handle NaN/infty polynomial", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 4116); return isl_term_free(term); } while (0)
;
4117
4118 if (is_cst) {
4119 isl_poly_cst *cst;
4120 cst = isl_poly_as_cst(poly);
4121 if (!cst)
4122 goto error;
4123 term = isl_term_cow(term);
4124 if (!term)
4125 goto error;
4126 isl_int_set(term->n, cst->n)isl_sioimath_set((term->n), *(cst->n));
4127 isl_int_set(term->d, cst->d)isl_sioimath_set((term->d), *(cst->d));
4128 if (fn(isl_term_copy(term), user) < 0)
4129 goto error;
4130 return term;
4131 }
4132
4133 rec = isl_poly_as_rec(poly);
4134 if (!rec)
4135 goto error;
4136
4137 for (i = 0; i < rec->n; ++i) {
4138 term = isl_term_cow(term);
4139 if (!term)
4140 goto error;
4141 term->pow[poly->var] = i;
4142 term = isl_poly_foreach_term(rec->p[i], fn, term, user);
4143 if (!term)
4144 goto error;
4145 }
4146 term = isl_term_cow(term);
4147 if (!term)
4148 return NULL((void*)0);
4149 term->pow[poly->var] = 0;
4150
4151 return term;
4152error:
4153 isl_term_free(term);
4154 return NULL((void*)0);
4155}
4156
4157isl_stat isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
4158 isl_stat (*fn)(__isl_take isl_term *term, void *user), void *user)
4159{
4160 isl_term *term;
4161
4162 if (!qp)
4163 return isl_stat_error;
4164
4165 term = isl_term_alloc(isl_space_copy(qp->dim), isl_mat_copy(qp->div));
4166 if (!term)
4167 return isl_stat_error;
4168
4169 term = isl_poly_foreach_term(qp->poly, fn, term, user);
4170
4171 isl_term_free(term);
4172
4173 return term ? isl_stat_ok : isl_stat_error;
4174}
4175
4176__isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
4177{
4178 isl_poly *poly;
4179 isl_qpolynomial *qp;
4180 int i;
4181 isl_size n;
4182
4183 n = isl_term_dim(term, isl_dim_all);
4184 if (n < 0)
4185 term = isl_term_free(term);
4186 if (!term)
4187 return NULL((void*)0);
4188
4189 poly = isl_poly_rat_cst(term->dim->ctx, term->n, term->d);
4190 for (i = 0; i < n; ++i) {
4191 if (!term->pow[i])
4192 continue;
4193 poly = isl_poly_mul(poly,
4194 isl_poly_var_pow(term->dim->ctx, i, term->pow[i]));
4195 }
4196
4197 qp = isl_qpolynomial_alloc(isl_space_copy(term->dim),
4198 term->div->n_row, poly);
4199 if (!qp)
4200 goto error;
4201 isl_mat_free(qp->div);
4202 qp->div = isl_mat_copy(term->div);
4203 if (!qp->div)
4204 goto error;
4205
4206 isl_term_free(term);
4207 return qp;
4208error:
4209 isl_qpolynomial_free(qp);
4210 isl_term_free(term);
4211 return NULL((void*)0);
4212}
4213
4214__isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
4215 __isl_take isl_space *space)
4216{
4217 int i;
4218 int extra;
4219 isl_size total, d_set, d_qp;
4220
4221 if (!qp || !space)
4222 goto error;
4223
4224 if (isl_space_is_equal(qp->dim, space)) {
4225 isl_space_free(space);
4226 return qp;
4227 }
4228
4229 qp = isl_qpolynomial_cow(qp);
4230 if (!qp)
4231 goto error;
4232
4233 d_set = isl_space_dim(space, isl_dim_set);
4234 d_qp = isl_qpolynomial_domain_dim(qp, isl_dim_set);
4235 extra = d_set - d_qp;
4236 total = isl_space_dim(qp->dim, isl_dim_all);
4237 if (d_set < 0 || d_qp < 0 || total < 0)
4238 goto error;
4239 if (qp->div->n_row) {
4240 int *exp;
4241
4242 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row)((int *)isl_malloc_or_die(qp->div->ctx, (qp->div->
n_row)*sizeof(int)))
;
4243 if (!exp)
4244 goto error;
4245 for (i = 0; i < qp->div->n_row; ++i)
4246 exp[i] = extra + i;
4247 qp->poly = expand(qp->poly, exp, total);
4248 free(exp);
4249 if (!qp->poly)
4250 goto error;
4251 }
4252 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
4253 if (!qp->div)
4254 goto error;
4255 for (i = 0; i < qp->div->n_row; ++i)
4256 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
4257
4258 isl_space_free(qp->dim);
4259 qp->dim = space;
4260
4261 return qp;
4262error:
4263 isl_space_free(space);
4264 isl_qpolynomial_free(qp);
4265 return NULL((void*)0);
4266}
4267
4268/* For each parameter or variable that does not appear in qp,
4269 * first eliminate the variable from all constraints and then set it to zero.
4270 */
4271static __isl_give isl_setisl_map *fix_inactive(__isl_take isl_setisl_map *set,
4272 __isl_keep isl_qpolynomial *qp)
4273{
4274 int *active = NULL((void*)0);
4275 int i;
4276 isl_size d;
4277 isl_size nparam;
4278 isl_size nvar;
4279
4280 d = isl_set_dim(set, isl_dim_all);
4281 if (d < 0 || !qp)
4282 goto error;
4283
4284 active = isl_calloc_array(set->ctx, int, d)((int *)isl_calloc_or_die(set->ctx, d, sizeof(int)));
4285 if (set_active(qp, active) < 0)
4286 goto error;
4287
4288 for (i = 0; i < d; ++i)
4289 if (!active[i])
4290 break;
4291
4292 if (i == d) {
4293 free(active);
4294 return set;
4295 }
4296
4297 nparam = isl_set_dim(set, isl_dim_param);
4298 nvar = isl_set_dim(set, isl_dim_set);
4299 if (nparam < 0 || nvar < 0)
4300 goto error;
4301 for (i = 0; i < nparam; ++i) {
4302 if (active[i])
4303 continue;
4304 set = isl_set_eliminate(set, isl_dim_param, i, 1);
4305 set = isl_set_fix_si(set, isl_dim_param, i, 0);
4306 }
4307 for (i = 0; i < nvar; ++i) {
4308 if (active[nparam + i])
4309 continue;
4310 set = isl_set_eliminate(set, isl_dim_set, i, 1);
4311 set = isl_set_fix_si(set, isl_dim_set, i, 0);
4312 }
4313
4314 free(active);
4315
4316 return set;
4317error:
4318 free(active);
4319 isl_set_free(set);
4320 return NULL((void*)0);
4321}
4322
4323struct isl_opt_data {
4324 isl_qpolynomial *qp;
4325 int first;
4326 isl_val *opt;
4327 int max;
4328};
4329
4330static isl_stat opt_fn(__isl_take isl_point *pnt, void *user)
4331{
4332 struct isl_opt_data *data = (struct isl_opt_data *)user;
4333 isl_val *val;
4334
4335 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
4336 if (data->first) {
4337 data->first = 0;
4338 data->opt = val;
4339 } else if (data->max) {
4340 data->opt = isl_val_max(data->opt, val);
4341 } else {
4342 data->opt = isl_val_min(data->opt, val);
4343 }
4344
4345 return isl_stat_ok;
4346}
4347
4348__isl_give isl_val *isl_qpolynomial_opt_on_domain(
4349 __isl_take isl_qpolynomial *qp, __isl_take isl_setisl_map *set, int max)
4350{
4351 struct isl_opt_data data = { NULL((void*)0), 1, NULL((void*)0), max };
4352 isl_bool is_cst;
4353
4354 if (!set || !qp)
4355 goto error;
4356
4357 is_cst = isl_poly_is_cst(qp->poly);
4358 if (is_cst < 0)
4359 goto error;
4360 if (is_cst) {
4361 isl_set_free(set);
4362 data.opt = isl_qpolynomial_get_constant_val(qp);
4363 isl_qpolynomial_free(qp);
4364 return data.opt;
4365 }
4366
4367 set = fix_inactive(set, qp);
4368
4369 data.qp = qp;
4370 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
4371 goto error;
4372
4373 if (data.first)
4374 data.opt = isl_val_zero(isl_set_get_ctx(set));
4375
4376 isl_set_free(set);
4377 isl_qpolynomial_free(qp);
4378 return data.opt;
4379error:
4380 isl_set_free(set);
4381 isl_qpolynomial_free(qp);
4382 isl_val_free(data.opt);
4383 return NULL((void*)0);
4384}
4385
4386__isl_give isl_qpolynomial *isl_qpolynomial_morph_domain(
4387 __isl_take isl_qpolynomial *qp, __isl_take isl_morph *morph)
4388{
4389 int i;
4390 int n_sub;
4391 isl_ctx *ctx;
4392 isl_space *space;
4393 isl_poly **subs;
4394 isl_mat *mat, *diag;
4395
4396 qp = isl_qpolynomial_cow(qp);
4397
4398 space = isl_qpolynomial_peek_domain_space(qp);
4399 if (isl_morph_check_applies(morph, space) < 0)
4400 goto error;
4401
4402 ctx = isl_qpolynomial_get_ctx(qp);
4403 n_sub = morph->inv->n_row - 1;
4404 if (morph->inv->n_row != morph->inv->n_col)
4405 n_sub += qp->div->n_row;
4406 subs = isl_calloc_array(ctx, struct isl_poly *, n_sub)((struct isl_poly * *)isl_calloc_or_die(ctx, n_sub, sizeof(struct
isl_poly *)))
;
4407 if (n_sub && !subs)
4408 goto error;
4409
4410 for (i = 0; 1 + i < morph->inv->n_row; ++i)
4411 subs[i] = isl_poly_from_affine(ctx, morph->inv->row[1 + i],
4412 morph->inv->row[0][0], morph->inv->n_col);
4413 if (morph->inv->n_row != morph->inv->n_col)
4414 for (i = 0; i < qp->div->n_row; ++i)
4415 subs[morph->inv->n_row - 1 + i] =
4416 isl_poly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
4417
4418 qp->poly = isl_poly_subs(qp->poly, 0, n_sub, subs);
4419
4420 for (i = 0; i < n_sub; ++i)
4421 isl_poly_free(subs[i]);
4422 free(subs);
4423
4424 diag = isl_mat_diag(ctx, 1, morph->inv->row[0][0]);
4425 mat = isl_mat_diagonal(diag, isl_mat_copy(morph->inv));
4426 diag = isl_mat_diag(ctx, qp->div->n_row, morph->inv->row[0][0]);
4427 mat = isl_mat_diagonal(mat, diag);
4428 qp->div = isl_mat_product(qp->div, mat);
4429 isl_space_free(qp->dim);
4430 qp->dim = isl_space_copy(morph->ran->dim);
4431
4432 if (!qp->poly || !qp->div || !qp->dim)
4433 goto error;
4434
4435 isl_morph_free(morph);
4436
4437 return qp;
4438error:
4439 isl_qpolynomial_free(qp);
4440 isl_morph_free(morph);
4441 return NULL((void*)0);
4442}
4443
4444__isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
4445 __isl_take isl_union_pw_qpolynomial *upwqp1,
4446 __isl_take isl_union_pw_qpolynomial *upwqp2)
4447{
4448 return isl_union_pw_qpolynomial_match_bin_op(upwqp1, upwqp2,
4449 &isl_pw_qpolynomial_mul);
4450}
4451
4452/* Reorder the dimension of "qp" according to the given reordering.
4453 */
4454__isl_give isl_qpolynomial *isl_qpolynomial_realign_domain(
4455 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
4456{
4457 isl_space *space;
4458
4459 qp = isl_qpolynomial_cow(qp);
4460 if (!qp)
4461 goto error;
4462
4463 r = isl_reordering_extend(r, qp->div->n_row);
4464 if (!r)
4465 goto error;
4466
4467 qp->div = isl_local_reorder(qp->div, isl_reordering_copy(r));
4468 if (!qp->div)
4469 goto error;
4470
4471 qp->poly = reorder(qp->poly, r->pos);
4472 if (!qp->poly)
4473 goto error;
4474
4475 space = isl_reordering_get_space(r);
4476 qp = isl_qpolynomial_reset_domain_space(qp, space);
4477
4478 isl_reordering_free(r);
4479 return qp;
4480error:
4481 isl_qpolynomial_free(qp);
4482 isl_reordering_free(r);
4483 return NULL((void*)0);
4484}
4485
4486__isl_give isl_qpolynomial *isl_qpolynomial_align_params(
4487 __isl_take isl_qpolynomial *qp, __isl_take isl_space *model)
4488{
4489 isl_bool equal_params;
4490
4491 if (!qp || !model)
4492 goto error;
4493
4494 equal_params = isl_space_has_equal_params(qp->dim, model);
4495 if (equal_params < 0)
4496 goto error;
4497 if (!equal_params) {
4498 isl_reordering *exp;
4499
4500 exp = isl_parameter_alignment_reordering(qp->dim, model);
4501 exp = isl_reordering_extend_space(exp,
4502 isl_qpolynomial_get_domain_space(qp));
4503 qp = isl_qpolynomial_realign_domain(qp, exp);
4504 }
4505
4506 isl_space_free(model);
4507 return qp;
4508error:
4509 isl_space_free(model);
4510 isl_qpolynomial_free(qp);
4511 return NULL((void*)0);
4512}
4513
4514struct isl_split_periods_data {
4515 int max_periods;
4516 isl_pw_qpolynomial *res;
4517};
4518
4519/* Create a slice where the integer division "div" has the fixed value "v".
4520 * In particular, if "div" refers to floor(f/m), then create a slice
4521 *
4522 * m v <= f <= m v + (m - 1)
4523 *
4524 * or
4525 *
4526 * f - m v >= 0
4527 * -f + m v + (m - 1) >= 0
4528 */
4529static __isl_give isl_setisl_map *set_div_slice(__isl_take isl_space *space,
4530 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
4531{
4532 isl_size total;
4533 isl_basic_setisl_basic_map *bset = NULL((void*)0);
4534 int k;
4535
4536 total = isl_space_dim(space, isl_dim_all);
4537 if (total < 0 || !qp)
4538 goto error;
4539
4540 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0, 0, 2);
4541
4542 k = isl_basic_set_alloc_inequality(bset);
4543 if (k < 0)
4544 goto error;
4545 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4546 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0])isl_sioimath_submul((bset->ineq[k][0]), *(v), *(qp->div
->row[div][0]))
;
4547
4548 k = isl_basic_set_alloc_inequality(bset);
4549 if (k < 0)
4550 goto error;
4551 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4552 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0])isl_sioimath_addmul((bset->ineq[k][0]), *(v), *(qp->div
->row[div][0]))
;
4553 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0])isl_sioimath_add((bset->ineq[k][0]), *(bset->ineq[k][0]
), *(qp->div->row[div][0]))
;
4554 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1)isl_sioimath_sub_ui((bset->ineq[k][0]), *(bset->ineq[k]
[0]), 1)
;
4555
4556 isl_space_free(space);
4557 return isl_set_from_basic_set(bset);
4558error:
4559 isl_basic_set_free(bset);
4560 isl_space_free(space);
4561 return NULL((void*)0);
4562}
4563
4564static isl_stat split_periods(__isl_take isl_setisl_map *set,
4565 __isl_take isl_qpolynomial *qp, void *user);
4566
4567/* Create a slice of the domain "set" such that integer division "div"
4568 * has the fixed value "v" and add the results to data->res,
4569 * replacing the integer division by "v" in "qp".
4570 */
4571static isl_stat set_div(__isl_take isl_setisl_map *set,
4572 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4573 struct isl_split_periods_data *data)
4574{
4575 int i;
4576 isl_size div_pos;
4577 isl_setisl_map *slice;
4578 isl_poly *cst;
4579
4580 slice = set_div_slice(isl_set_get_space(set), qp, div, v);
4581 set = isl_set_intersect(set, slice);
4582
4583 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
4584 if (div_pos < 0)
4585 goto error;
4586
4587 for (i = div + 1; i < qp->div->n_row; ++i) {
4588 if (isl_int_is_zero(qp->div->row[i][2 + div_pos + div])(isl_sioimath_sgn(*(qp->div->row[i][2 + div_pos + div])
) == 0)
)
4589 continue;
4590 isl_int_addmul(qp->div->row[i][1],isl_sioimath_addmul((qp->div->row[i][1]), *(qp->div->
row[i][2 + div_pos + div]), *(v))
4591 qp->div->row[i][2 + div_pos + div], v)isl_sioimath_addmul((qp->div->row[i][1]), *(qp->div->
row[i][2 + div_pos + div]), *(v))
;
4592 isl_int_set_si(qp->div->row[i][2 + div_pos + div], 0)isl_sioimath_set_si((qp->div->row[i][2 + div_pos + div]
), 0)
;
4593 }
4594
4595 cst = isl_poly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4596 qp = substitute_div(qp, div, cst);
4597
4598 return split_periods(set, qp, data);
4599error:
4600 isl_set_free(set);
4601 isl_qpolynomial_free(qp);
4602 return isl_stat_error;
4603}
4604
4605/* Split the domain "set" such that integer division "div"
4606 * has a fixed value (ranging from "min" to "max") on each slice
4607 * and add the results to data->res.
4608 */
4609static isl_stat split_div(__isl_take isl_setisl_map *set,
4610 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4611 struct isl_split_periods_data *data)
4612{
4613 for (; isl_int_le(min, max)(isl_sioimath_cmp(*(min), *(max)) <= 0); isl_int_add_ui(min, min, 1)isl_sioimath_add_ui((min), *(min), 1)) {
4614 isl_setisl_map *set_i = isl_set_copy(set);
4615 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4616
4617 if (set_div(set_i, qp_i, div, min, data) < 0)
4618 goto error;
4619 }
4620 isl_set_free(set);
4621 isl_qpolynomial_free(qp);
4622 return isl_stat_ok;
4623error:
4624 isl_set_free(set);
4625 isl_qpolynomial_free(qp);
4626 return isl_stat_error;
4627}
4628
4629/* If "qp" refers to any integer division
4630 * that can only attain "max_periods" distinct values on "set"
4631 * then split the domain along those distinct values.
4632 * Add the results (or the original if no splitting occurs)
4633 * to data->res.
4634 */
4635static isl_stat split_periods(__isl_take isl_setisl_map *set,
4636 __isl_take isl_qpolynomial *qp, void *user)
4637{
4638 int i;
4639 isl_pw_qpolynomial *pwqp;
4640 struct isl_split_periods_data *data;
4641 isl_int min, max;
4642 isl_size div_pos;
4643 isl_stat r = isl_stat_ok;
4644
4645 data = (struct isl_split_periods_data *)user;
4646
4647 if (!set || !qp)
4648 goto error;
4649
4650 if (qp->div->n_row == 0) {
4651 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4652 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4653 return isl_stat_ok;
4654 }
4655
4656 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
4657 if (div_pos < 0)
4658 goto error;
4659
4660 isl_int_init(min)isl_sioimath_init((min));
4661 isl_int_init(max)isl_sioimath_init((max));
4662 for (i = 0; i < qp->div->n_row; ++i) {
4663 enum isl_lp_result lp_res;
4664
4665 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + div_pos,
4666 qp->div->n_row) != -1)
4667 continue;
4668
4669 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4670 set->ctx->one, &min, NULL((void*)0), NULL((void*)0));
4671 if (lp_res == isl_lp_error)
4672 goto error2;
4673 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4674 continue;
4675 isl_int_fdiv_q(min, min, qp->div->row[i][0])isl_sioimath_fdiv_q((min), *(min), *(qp->div->row[i][0]
))
;
4676
4677 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4678 set->ctx->one, &max, NULL((void*)0), NULL((void*)0));
4679 if (lp_res == isl_lp_error)
4680 goto error2;
4681 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4682 continue;
4683 isl_int_fdiv_q(max, max, qp->div->row[i][0])isl_sioimath_fdiv_q((max), *(max), *(qp->div->row[i][0]
))
;
4684
4685 isl_int_sub(max, max, min)isl_sioimath_sub((max), *(max), *(min));
4686 if (isl_int_cmp_si(max, data->max_periods)isl_sioimath_cmp_si(*(max), data->max_periods) < 0) {
4687 isl_int_add(max, max, min)isl_sioimath_add((max), *(max), *(min));
4688 break;
4689 }
4690 }
4691
4692 if (i < qp->div->n_row) {
4693 r = split_div(set, qp, i, min, max, data);
4694 } else {
4695 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4696 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4697 }
4698
4699 isl_int_clear(max)isl_sioimath_clear((max));
4700 isl_int_clear(min)isl_sioimath_clear((min));
4701
4702 return r;
4703error2:
4704 isl_int_clear(max)isl_sioimath_clear((max));
4705 isl_int_clear(min)isl_sioimath_clear((min));
4706error:
4707 isl_set_free(set);
4708 isl_qpolynomial_free(qp);
4709 return isl_stat_error;
4710}
4711
4712/* If any quasi-polynomial in pwqp refers to any integer division
4713 * that can only attain "max_periods" distinct values on its domain
4714 * then split the domain along those distinct values.
4715 */
4716__isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4717 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4718{
4719 struct isl_split_periods_data data;
4720
4721 data.max_periods = max_periods;
4722 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4723
4724 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4725 goto error;
4726
4727 isl_pw_qpolynomial_free(pwqp);
4728
4729 return data.res;
4730error:
4731 isl_pw_qpolynomial_free(data.res);
4732 isl_pw_qpolynomial_free(pwqp);
4733 return NULL((void*)0);
4734}
4735
4736/* Construct a piecewise quasipolynomial that is constant on the given
4737 * domain. In particular, it is
4738 * 0 if cst == 0
4739 * 1 if cst == 1
4740 * infinity if cst == -1
4741 *
4742 * If cst == -1, then explicitly check whether the domain is empty and,
4743 * if so, return 0 instead.
4744 */
4745static __isl_give isl_pw_qpolynomial *constant_on_domain(
4746 __isl_take isl_basic_setisl_basic_map *bset, int cst)
4747{
4748 isl_space *space;
4749 isl_qpolynomial *qp;
4750
4751 if (cst < 0 && isl_basic_set_is_empty(bset) == isl_bool_true)
4752 cst = 0;
4753 if (!bset)
4754 return NULL((void*)0);
4755
4756 bset = isl_basic_set_params(bset);
4757 space = isl_basic_set_get_space(bset);
4758 if (cst < 0)
4759 qp = isl_qpolynomial_infty_on_domain(space);
4760 else if (cst == 0)
4761 qp = isl_qpolynomial_zero_on_domain(space);
4762 else
4763 qp = isl_qpolynomial_one_on_domain(space);
4764 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4765}
4766
4767/* Internal data structure for multiplicative_call_factor_pw_qpolynomial.
4768 * "fn" is the function that is called on each factor.
4769 * "pwpq" collects the results.
4770 */
4771struct isl_multiplicative_call_data_pw_qpolynomial {
4772 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_setisl_basic_map *bset);
4773 isl_pw_qpolynomial *pwqp;
4774};
4775
4776/* Call "fn" on "bset" and return the result,
4777 * but first check if "bset" has any redundant constraints or
4778 * implicit equality constraints.
4779 * If so, there may be further opportunities for detecting factors or
4780 * removing equality constraints, so recursively call
4781 * the top-level isl_basic_set_multiplicative_call.
4782 */
4783static __isl_give isl_pw_qpolynomial *multiplicative_call_base(
4784 __isl_take isl_basic_setisl_basic_map *bset,
4785 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_setisl_basic_map *bset))
4786{
4787 isl_size n1, n2, n_eq;
4788
4789 n1 = isl_basic_set_n_constraint(bset);
4790 if (n1 < 0)
4791 bset = isl_basic_set_free(bset);
4792 bset = isl_basic_set_remove_redundancies(bset);
4793 bset = isl_basic_set_detect_equalities(bset);
4794 n2 = isl_basic_set_n_constraint(bset);
4795 n_eq = isl_basic_set_n_equality(bset);
4796 if (n2 < 0 || n_eq < 0)
4797 bset = isl_basic_set_free(bset);
4798 else if (n2 < n1 || n_eq > 0)
4799 return isl_basic_set_multiplicative_call(bset, fn);
4800 return fn(bset);
4801}
4802
4803/* isl_factorizer_every_factor_basic_set callback that applies
4804 * data->fn to the factor "bset" and multiplies in the result
4805 * in data->pwqp.
4806 */
4807static isl_bool multiplicative_call_factor_pw_qpolynomial(
4808 __isl_keep isl_basic_setisl_basic_map *bset, void *user)
4809{
4810 struct isl_multiplicative_call_data_pw_qpolynomial *data = user;
4811 isl_pw_qpolynomial *res;
4812
4813 bset = isl_basic_set_copy(bset);
4814 res = multiplicative_call_base(bset, data->fn);
4815 data->pwqp = isl_pw_qpolynomial_mul(data->pwqp, res);
4816 if (!data->pwqp)
4817 return isl_bool_error;
4818
4819 return isl_bool_true;
4820}
4821
4822/* Factor bset, call fn on each of the factors and return the product.
4823 *
4824 * If no factors can be found, simply call fn on the input.
4825 * Otherwise, construct the factors based on the factorizer,
4826 * call fn on each factor and compute the product.
4827 */
4828static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4829 __isl_take isl_basic_setisl_basic_map *bset,
4830 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_setisl_basic_map *bset))
4831{
4832 struct isl_multiplicative_call_data_pw_qpolynomial data = { fn };
4833 isl_space *space;
4834 isl_setisl_map *set;
4835 isl_factorizer *f;
4836 isl_qpolynomial *qp;
4837 isl_bool every;
4838
4839 f = isl_basic_set_factorizer(bset);
4840 if (!f)
4841 goto error;
4842 if (f->n_group == 0) {
4843 isl_factorizer_free(f);
4844 return multiplicative_call_base(bset, fn);
4845 }
4846
4847 space = isl_basic_set_get_space(bset);
4848 space = isl_space_params(space);
4849 set = isl_set_universe(isl_space_copy(space));
4850 qp = isl_qpolynomial_one_on_domain(space);
4851 data.pwqp = isl_pw_qpolynomial_alloc(set, qp);
4852
4853 every = isl_factorizer_every_factor_basic_set(f,
4854 &multiplicative_call_factor_pw_qpolynomial, &data);
4855 if (every < 0)
4856 data.pwqp = isl_pw_qpolynomial_free(data.pwqp);
4857
4858 isl_basic_set_free(bset);
4859 isl_factorizer_free(f);
4860
4861 return data.pwqp;
4862error:
4863 isl_basic_set_free(bset);
4864 return NULL((void*)0);
4865}
4866
4867/* Factor bset, call fn on each of the factors and return the product.
4868 * The function is assumed to evaluate to zero on empty domains,
4869 * to one on zero-dimensional domains and to infinity on unbounded domains
4870 * and will not be called explicitly on zero-dimensional or unbounded domains.
4871 *
4872 * We first check for some special cases and remove all equalities.
4873 * Then we hand over control to compressed_multiplicative_call.
4874 */
4875__isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4876 __isl_take isl_basic_setisl_basic_map *bset,
4877 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_setisl_basic_map *bset))
4878{
4879 isl_bool bounded;
4880 isl_size dim;
4881 isl_morph *morph;
4882 isl_pw_qpolynomial *pwqp;
4883
4884 if (!bset)
4885 return NULL((void*)0);
4886
4887 if (isl_basic_set_plain_is_empty(bset))
4888 return constant_on_domain(bset, 0);
4889
4890 dim = isl_basic_set_dim(bset, isl_dim_set);
4891 if (dim < 0)
4892 goto error;
4893 if (dim == 0)
4894 return constant_on_domain(bset, 1);
4895
4896 bounded = isl_basic_set_is_bounded(bset);
4897 if (bounded < 0)
4898 goto error;
4899 if (!bounded)
4900 return constant_on_domain(bset, -1);
4901
4902 if (bset->n_eq == 0)
4903 return compressed_multiplicative_call(bset, fn);
4904
4905 morph = isl_basic_set_full_compression(bset);
4906 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4907
4908 pwqp = compressed_multiplicative_call(bset, fn);
4909
4910 morph = isl_morph_dom_params(morph);
4911 morph = isl_morph_ran_params(morph);
4912 morph = isl_morph_inverse(morph);
4913
4914 pwqp = isl_pw_qpolynomial_morph_domain(pwqp, morph);
4915
4916 return pwqp;
4917error:
4918 isl_basic_set_free(bset);
4919 return NULL((void*)0);
4920}
4921
4922/* Drop all floors in "qp", turning each integer division [a/m] into
4923 * a rational division a/m. If "down" is set, then the integer division
4924 * is replaced by (a-(m-1))/m instead.
4925 */
4926static __isl_give isl_qpolynomial *qp_drop_floors(
4927 __isl_take isl_qpolynomial *qp, int down)
4928{
4929 int i;
4930 isl_poly *s;
4931
4932 if (!qp)
4933 return NULL((void*)0);
4934 if (qp->div->n_row == 0)
4935 return qp;
4936
4937 qp = isl_qpolynomial_cow(qp);
4938 if (!qp)
4939 return NULL((void*)0);
4940
4941 for (i = qp->div->n_row - 1; i >= 0; --i) {
4942 if (down) {
4943 isl_int_sub(qp->div->row[i][1],isl_sioimath_sub((qp->div->row[i][1]), *(qp->div->
row[i][1]), *(qp->div->row[i][0]))
4944 qp->div->row[i][1], qp->div->row[i][0])isl_sioimath_sub((qp->div->row[i][1]), *(qp->div->
row[i][1]), *(qp->div->row[i][0]))
;
4945 isl_int_add_ui(qp->div->row[i][1],isl_sioimath_add_ui((qp->div->row[i][1]), *(qp->div->
row[i][1]), 1)
4946 qp->div->row[i][1], 1)isl_sioimath_add_ui((qp->div->row[i][1]), *(qp->div->
row[i][1]), 1)
;
4947 }
4948 s = isl_poly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4949 qp->div->row[i][0], qp->div->n_col - 1);
4950 qp = substitute_div(qp, i, s);
4951 if (!qp)
4952 return NULL((void*)0);
4953 }
4954
4955 return qp;
4956}
4957
4958/* Drop all floors in "pwqp", turning each integer division [a/m] into
4959 * a rational division a/m.
4960 */
4961static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4962 __isl_take isl_pw_qpolynomial *pwqp)
4963{
4964 int i;
4965
4966 if (!pwqp)
4967 return NULL((void*)0);
4968
4969 if (isl_pw_qpolynomial_is_zero(pwqp))
4970 return pwqp;
4971
4972 pwqp = isl_pw_qpolynomial_cow(pwqp);
4973 if (!pwqp)
4974 return NULL((void*)0);
4975
4976 for (i = 0; i < pwqp->n; ++i) {
4977 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4978 if (!pwqp->p[i].qp)
4979 goto error;
4980 }
4981
4982 return pwqp;
4983error:
4984 isl_pw_qpolynomial_free(pwqp);
4985 return NULL((void*)0);
4986}
4987
4988/* Adjust all the integer divisions in "qp" such that they are at least
4989 * one over the given orthant (identified by "signs"). This ensures
4990 * that they will still be non-negative even after subtracting (m-1)/m.
4991 *
4992 * In particular, f is replaced by f' + v, changing f = [a/m]
4993 * to f' = [(a - m v)/m].
4994 * If the constant term k in a is smaller than m,
4995 * the constant term of v is set to floor(k/m) - 1.
4996 * For any other term, if the coefficient c and the variable x have
4997 * the same sign, then no changes are needed.
4998 * Otherwise, if the variable is positive (and c is negative),
4999 * then the coefficient of x in v is set to floor(c/m).
5000 * If the variable is negative (and c is positive),
5001 * then the coefficient of x in v is set to ceil(c/m).
5002 */
5003static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
5004 int *signs)
5005{
5006 int i, j;
5007 isl_size div_pos;
5008 isl_vec *v = NULL((void*)0);
5009 isl_poly *s;
5010
5011 qp = isl_qpolynomial_cow(qp);
5012 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
5013 if (div_pos < 0)
5014 return isl_qpolynomial_free(qp);
5015 qp->div = isl_mat_cow(qp->div);
5016 if (!qp->div)
5017 goto error;
5018
5019 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
5020
5021 for (i = 0; i < qp->div->n_row; ++i) {
5022 isl_int *row = qp->div->row[i];
5023 v = isl_vec_clr(v);
5024 if (!v)
5025 goto error;
5026 if (isl_int_lt(row[1], row[0])(isl_sioimath_cmp(*(row[1]), *(row[0])) < 0)) {
5027 isl_int_fdiv_q(v->el[0], row[1], row[0])isl_sioimath_fdiv_q((v->el[0]), *(row[1]), *(row[0]));
5028 isl_int_sub_ui(v->el[0], v->el[0], 1)isl_sioimath_sub_ui((v->el[0]), *(v->el[0]), 1);
5029 isl_int_submul(row[1], row[0], v->el[0])isl_sioimath_submul((row[1]), *(row[0]), *(v->el[0]));
5030 }
5031 for (j = 0; j < div_pos; ++j) {
5032 if (isl_int_sgn(row[2 + j])isl_sioimath_sgn(*(row[2 + j])) * signs[j] >= 0)
5033 continue;
5034 if (signs[j] < 0)
5035 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0])isl_sioimath_cdiv_q((v->el[1 + j]), *(row[2 + j]), *(row[0
]))
;
5036 else
5037 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0])isl_sioimath_fdiv_q((v->el[1 + j]), *(row[2 + j]), *(row[0
]))
;
5038 isl_int_submul(row[2 + j], row[0], v->el[1 + j])isl_sioimath_submul((row[2 + j]), *(row[0]), *(v->el[1 + j
]))
;
5039 }
5040 for (j = 0; j < i; ++j) {
5041 if (isl_int_sgn(row[2 + div_pos + j])isl_sioimath_sgn(*(row[2 + div_pos + j])) >= 0)
5042 continue;
5043 isl_int_fdiv_q(v->el[1 + div_pos + j],isl_sioimath_fdiv_q((v->el[1 + div_pos + j]), *(row[2 + div_pos
+ j]), *(row[0]))
5044 row[2 + div_pos + j], row[0])isl_sioimath_fdiv_q((v->el[1 + div_pos + j]), *(row[2 + div_pos
+ j]), *(row[0]))
;
5045 isl_int_submul(row[2 + div_pos + j],isl_sioimath_submul((row[2 + div_pos + j]), *(row[0]), *(v->
el[1 + div_pos + j]))
5046 row[0], v->el[1 + div_pos + j])isl_sioimath_submul((row[2 + div_pos + j]), *(row[0]), *(v->
el[1 + div_pos + j]))
;
5047 }
5048 for (j = i + 1; j < qp->div->n_row; ++j) {
5049 if (isl_int_is_zero(qp->div->row[j][2 + div_pos + i])(isl_sioimath_sgn(*(qp->div->row[j][2 + div_pos + i])) ==
0)
)
5050 continue;
5051 isl_seq_combine(qp->div->row[j] + 1,
5052 qp->div->ctx->one, qp->div->row[j] + 1,
5053 qp->div->row[j][2 + div_pos + i], v->el,
5054 v->size);
5055 }
5056 isl_int_set_si(v->el[1 + div_pos + i], 1)isl_sioimath_set_si((v->el[1 + div_pos + i]), 1);
5057 s = isl_poly_from_affine(qp->dim->ctx, v->el,
5058 qp->div->ctx->one, v->size);
5059 qp->poly = isl_poly_subs(qp->poly, div_pos + i, 1, &s);
5060 isl_poly_free(s);
5061 if (!qp->poly)
5062 goto error;
5063 }
5064
5065 isl_vec_free(v);
5066 return qp;
5067error:
5068 isl_vec_free(v);
5069 isl_qpolynomial_free(qp);
5070 return NULL((void*)0);
5071}
5072
5073struct isl_to_poly_data {
5074 int sign;
5075 isl_pw_qpolynomial *res;
5076 isl_qpolynomial *qp;
5077};
5078
5079/* Appoximate data->qp by a polynomial on the orthant identified by "signs".
5080 * We first make all integer divisions positive and then split the
5081 * quasipolynomials into terms with sign data->sign (the direction
5082 * of the requested approximation) and terms with the opposite sign.
5083 * In the first set of terms, each integer division [a/m] is
5084 * overapproximated by a/m, while in the second it is underapproximated
5085 * by (a-(m-1))/m.
5086 */
5087static isl_stat to_polynomial_on_orthant(__isl_take isl_setisl_map *orthant,
5088 int *signs, void *user)
5089{
5090 struct isl_to_poly_data *data = user;
5091 isl_pw_qpolynomial *t;
5092 isl_qpolynomial *qp, *up, *down;
5093
5094 qp = isl_qpolynomial_copy(data->qp);
5095 qp = make_divs_pos(qp, signs);
5096
5097 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
5098 up = qp_drop_floors(up, 0);
5099 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
5100 down = qp_drop_floors(down, 1);
5101
5102 isl_qpolynomial_free(qp);
5103 qp = isl_qpolynomial_add(up, down);
5104
5105 t = isl_pw_qpolynomial_alloc(orthant, qp);
5106 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
5107
5108 return isl_stat_ok;
5109}
5110
5111/* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
5112 * the polynomial will be an overapproximation. If "sign" is negative,
5113 * it will be an underapproximation. If "sign" is zero, the approximation
5114 * will lie somewhere in between.
5115 *
5116 * In particular, is sign == 0, we simply drop the floors, turning
5117 * the integer divisions into rational divisions.
5118 * Otherwise, we split the domains into orthants, make all integer divisions
5119 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
5120 * depending on the requested sign and the sign of the term in which
5121 * the integer division appears.
5122 */
5123__isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
5124 __isl_take isl_pw_qpolynomial *pwqp, int sign)
5125{
5126 int i;
5127 struct isl_to_poly_data data;
5128
5129 if (sign == 0)
5130 return pwqp_drop_floors(pwqp);
5131
5132 if (!pwqp)
5133 return NULL((void*)0);
5134
5135 data.sign = sign;
5136 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
5137
5138 for (i = 0; i < pwqp->n; ++i) {
5139 if (pwqp->p[i].qp->div->n_row == 0) {
5140 isl_pw_qpolynomial *t;
5141 t = isl_pw_qpolynomial_alloc(
5142 isl_set_copy(pwqp->p[i].set),
5143 isl_qpolynomial_copy(pwqp->p[i].qp));
5144 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
5145 continue;
5146 }
5147 data.qp = pwqp->p[i].qp;
5148 if (isl_set_foreach_orthant(pwqp->p[i].set,
5149 &to_polynomial_on_orthant, &data) < 0)
5150 goto error;
5151 }
5152
5153 isl_pw_qpolynomial_free(pwqp);
5154
5155 return data.res;
5156error:
5157 isl_pw_qpolynomial_free(pwqp);
5158 isl_pw_qpolynomial_free(data.res);
5159 return NULL((void*)0);
5160}
5161
5162static __isl_give isl_pw_qpolynomial *poly_entry(
5163 __isl_take isl_pw_qpolynomial *pwqp, void *user)
5164{
5165 int *sign = user;
5166
5167 return isl_pw_qpolynomial_to_polynomial(pwqp, *sign);
5168}
5169
5170__isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
5171 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
5172{
5173 return isl_union_pw_qpolynomial_transform_inplace(upwqp,
5174 &poly_entry, &sign);
5175}
5176
5177__isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
5178 __isl_take isl_qpolynomial *qp)
5179{
5180 int i, k;
5181 isl_space *space;
5182 isl_vec *aff = NULL((void*)0);
5183 isl_basic_map *bmap = NULL((void*)0);
5184 isl_bool is_affine;
5185 unsigned pos;
5186 unsigned n_div;
5187
5188 if (!qp)
5189 return NULL((void*)0);
5190 is_affine = isl_poly_is_affine(qp->poly);
5191 if (is_affine < 0)
5192 goto error;
5193 if (!is_affine)
5194 isl_die(qp->dim->ctx, isl_error_invalid,do { isl_handle_error(qp->dim->ctx, isl_error_invalid, "input quasi-polynomial not affine"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 5195); goto error; } while (0)
5195 "input quasi-polynomial not affine", goto error)do { isl_handle_error(qp->dim->ctx, isl_error_invalid, "input quasi-polynomial not affine"
, "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_polynomial.c"
, 5195); goto error; } while (0)
;
5196 aff = isl_qpolynomial_extract_affine(qp);
5197 if (!aff)
5198 goto error;
5199 space = isl_qpolynomial_get_space(qp);
5200 pos = 1 + isl_space_offset(space, isl_dim_out);
5201 n_div = qp->div->n_row;
5202 bmap = isl_basic_map_alloc_space(space, n_div, 1, 2 * n_div);
5203
5204 for (i = 0; i < n_div; ++i) {
5205 k = isl_basic_map_alloc_div(bmap);
5206 if (k < 0)
5207 goto error;
5208 isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
5209 isl_int_set_si(bmap->div[k][qp->div->n_col], 0)isl_sioimath_set_si((bmap->div[k][qp->div->n_col]), 0
)
;
5210 bmap = isl_basic_map_add_div_constraints(bmap, k);
5211 }
5212 k = isl_basic_map_alloc_equality(bmap);
5213 if (k < 0)
5214 goto error;
5215 isl_int_neg(bmap->eq[k][pos], aff->el[0])isl_sioimath_neg((bmap->eq[k][pos]), *(aff->el[0]));
5216 isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
5217 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
5218
5219 isl_vec_free(aff);
5220 isl_qpolynomial_free(qp);
5221 bmap = isl_basic_map_finalize(bmap);
5222 return bmap;
5223error:
5224 isl_vec_free(aff);
5225 isl_qpolynomial_free(qp);
5226 isl_basic_map_free(bmap);
5227 return NULL((void*)0);
5228}

/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/polly/lib/External/isl/isl_int_sioimath.h

1/*
2 * Copyright 2015 INRIA Paris-Rocquencourt
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Michael Kruse, INRIA Paris-Rocquencourt,
7 * Domaine de Voluceau, Rocquenqourt, B.P. 105,
8 * 78153 Le Chesnay Cedex France
9 */
10#ifndef ISL_INT_SIOIMATH_H
11#define ISL_INT_SIOIMATH_H
12
13#include <inttypes.h>
14#include <limits.h>
15#include <stdint.h>
16#include <stdlib.h>
17
18#include <isl_imath.h>
19#include <isl/hash.h>
20
21#define ARRAY_SIZE(array)(sizeof(array)/sizeof(*array)) (sizeof(array)/sizeof(*array))
22
23/* Visual Studio before VS2015 does not support the inline keyword when
24 * compiling in C mode because it was introduced in C99 which it does not
25 * officially support. Instead, it has a proprietary extension using __inline.
26 */
27#if defined(_MSC_VER) && (_MSC_VER < 1900)
28#define inline __inline
29#endif
30
31/* The type to represent integers optimized for small values. It is either a
32 * pointer to an mp_int ( = mpz_t*; big representation) or an int32_t (small
33 * represenation) with a discriminator at the least significant bit. In big
34 * representation it will be always zero because of heap alignment. It is set
35 * to 1 for small representation and use the 32 most significant bits for the
36 * int32_t.
37 *
38 * Structure on 64 bit machines, with 8-byte aligment (3 bits):
39 *
40 * Big representation:
41 * MSB LSB
42 * |------------------------------------------------------------000
43 * | mpz_t* |
44 * | != NULL |
45 *
46 * Small representation:
47 * MSB 32 LSB
48 * |------------------------------|00000000000000000000000000000001
49 * | int32_t |
50 * | 2147483647 ... -2147483647 |
51 * ^
52 * |
53 * discriminator bit
54 *
55 * On 32 bit machines isl_sioimath type is blown up to 8 bytes, i.e.
56 * isl_sioimath is guaranteed to be at least 8 bytes. This is to ensure the
57 * int32_t can be hidden in that type without data loss. In the future we might
58 * optimize this to use 31 hidden bits in a 32 bit pointer. We may also use 63
59 * bits on 64 bit machines, but this comes with the cost of additional overflow
60 * checks because there is no standardized 128 bit integer we could expand to.
61 *
62 * We use native integer types and avoid union structures to avoid assumptions
63 * on the machine's endianness.
64 *
65 * This implementation makes the following assumptions:
66 * - long can represent any int32_t
67 * - mp_small is signed long
68 * - mp_usmall is unsigned long
69 * - adresses returned by malloc are aligned to 2-byte boundaries (leastmost
70 * bit is zero)
71 */
72#if UINT64_MAX(18446744073709551615UL) > UINTPTR_MAX(18446744073709551615UL)
73typedef uint64_t isl_sioimath;
74#else
75typedef uintptr_t isl_sioimath;
76#endif
77
78/* The negation of the smallest possible number in int32_t, INT32_MIN
79 * (0x80000000u, -2147483648), cannot be represented in an int32_t, therefore
80 * every operation that may produce this value needs to special-case it.
81 * The operations are:
82 * abs(INT32_MIN)
83 * -INT32_MIN (negation)
84 * -1 * INT32_MIN (multiplication)
85 * INT32_MIN/-1 (any division: divexact, fdiv, cdiv, tdiv)
86 * To avoid checking these cases, we exclude INT32_MIN from small
87 * representation.
88 */
89#define ISL_SIOIMATH_SMALL_MIN(-(2147483647)) (-INT32_MAX(2147483647))
90
91/* Largest possible number in small representation */
92#define ISL_SIOIMATH_SMALL_MAX(2147483647) INT32_MAX(2147483647)
93
94/* Used for function parameters the function modifies. */
95typedef isl_sioimath *isl_sioimath_ptr;
96
97/* Used for function parameters that are read-only. */
98typedef isl_sioimath isl_sioimath_src;
99
100/* Return whether the argument is stored in small representation.
101 */
102inline int isl_sioimath_is_small(isl_sioimath val)
103{
104 return val & 0x00000001;
105}
106
107/* Return whether the argument is stored in big representation.
108 */
109inline int isl_sioimath_is_big(isl_sioimath val)
110{
111 return !isl_sioimath_is_small(val);
112}
113
114/* Get the number of an isl_int in small representation. Result is undefined if
115 * val is not stored in that format.
116 */
117inline int32_t isl_sioimath_get_small(isl_sioimath val)
118{
119 return val >> 32;
120}
121
122/* Get the number of an in isl_int in big representation. Result is undefined if
123 * val is not stored in that format.
124 */
125inline mp_int isl_sioimath_get_big(isl_sioimath val)
126{
127 return (mp_int)(uintptr_t) val;
128}
129
130/* Return 1 if val is stored in small representation and store its value to
131 * small. We rely on the compiler to optimize the isl_sioimath_get_small such
132 * that the shift is moved into the branch that executes in case of small
133 * representation. If there is no such branch, then a single shift is still
134 * cheaper than introducing branching code.
135 */
136inline int isl_sioimath_decode_small(isl_sioimath val, int32_t *small)
137{
138 *small = isl_sioimath_get_small(val);
139 return isl_sioimath_is_small(val);
140}
141
142/* Return 1 if val is stored in big representation and store its value to big.
143 */
144inline int isl_sioimath_decode_big(isl_sioimath val, mp_int *big)
145{
146 *big = isl_sioimath_get_big(val);
147 return isl_sioimath_is_big(val);
148}
149
150/* Encode a small representation into an isl_int.
151 */
152inline isl_sioimath isl_sioimath_encode_small(int32_t val)
153{
154 return ((isl_sioimath) val) << 32 | 0x00000001;
155}
156
157/* Encode a big representation.
158 */
159inline isl_sioimath isl_sioimath_encode_big(mp_int val)
160{
161 return (isl_sioimath)(uintptr_t) val;
162}
163
164/* A common situation is to call an IMath function with at least one argument
165 * that is currently in small representation or an integer parameter, i.e. a big
166 * representation of the same number is required. Promoting the original
167 * argument comes with multiple problems, such as modifying a read-only
168 * argument, the responsibility of deallocation and the execution cost. Instead,
169 * we make a copy by 'faking' the IMath internal structure.
170 *
171 * We reserve the maximum number of required digits on the stack to avoid heap
172 * allocations.
173 *
174 * mp_digit can be uint32_t or uint16_t. This code must work for little and big
175 * endian digits. The structure for an uint64_t argument and 32-bit mp_digits is
176 * sketched below.
177 *
178 * |----------------------------|
179 * uint64_t
180 *
181 * |-------------||-------------|
182 * mp_digit mp_digit
183 * digits[1] digits[0]
184 * Most sig digit Least sig digit
185 */
186typedef struct {
187 mpz_t big;
188 mp_digit digits[(sizeof(uintmax_t) + sizeof(mp_digit) - 1) /
189 sizeof(mp_digit)];
190} isl_sioimath_scratchspace_t;
191
192/* Convert a native integer to IMath's digit representation. A native integer
193 * might be big- or little endian, but IMath always stores the least significant
194 * digit in the lowest array indices. memcpy therefore is not possible.
195 *
196 * We also have to consider that long and mp_digit can be of different sizes,
197 * depending on the compiler (LP64, LLP64) and IMath's USE_64BIT_WORDS. This
198 * macro should work for all of them.
199 *
200 * "used" is set to the number of written digits. It must be minimal (IMath
201 * checks zeroness using the used field), but always at least one. Also note
202 * that the result of num>>(sizeof(num)*CHAR_BIT) is undefined.
203 */
204#define ISL_SIOIMATH_TO_DIGITS(num, digits, used)do { int i = 0; do { (digits)[i] = ((num) >> (sizeof(mp_digit
) * 8 * i)); i += 1; if (i >= (sizeof(num) + sizeof(mp_digit
) - 1) / sizeof(mp_digit)) break; if (((num) >> (sizeof
(mp_digit) * 8 * i)) == 0) break; } while (1); (used) = i; } while
(0)
\
205 do { \
206 int i = 0; \
207 do { \
208 (digits)[i] = \
209 ((num) >> (sizeof(mp_digit) * CHAR_BIT8 * i)); \
210 i += 1; \
211 if (i >= (sizeof(num) + sizeof(mp_digit) - 1) / \
212 sizeof(mp_digit)) \
213 break; \
214 if (((num) >> (sizeof(mp_digit) * CHAR_BIT8 * i)) == 0) \
215 break; \
216 } while (1); \
217 (used) = i; \
218 } while (0)
219
220inline void isl_siomath_uint32_to_digits(uint32_t num, mp_digit *digits,
221 mp_size *used)
222{
223 ISL_SIOIMATH_TO_DIGITS(num, digits, *used)do { int i = 0; do { (digits)[i] = ((num) >> (sizeof(mp_digit
) * 8 * i)); i += 1; if (i >= (sizeof(num) + sizeof(mp_digit
) - 1) / sizeof(mp_digit)) break; if (((num) >> (sizeof
(mp_digit) * 8 * i)) == 0) break; } while (1); (*used) = i; }
while (0)
;
224}
225
226inline void isl_siomath_ulong_to_digits(unsigned long num, mp_digit *digits,
227 mp_size *used)
228{
229 ISL_SIOIMATH_TO_DIGITS(num, digits, *used)do { int i = 0; do { (digits)[i] = ((num) >> (sizeof(mp_digit
) * 8 * i)); i += 1; if (i >= (sizeof(num) + sizeof(mp_digit
) - 1) / sizeof(mp_digit)) break; if (((num) >> (sizeof
(mp_digit) * 8 * i)) == 0) break; } while (1); (*used) = i; }
while (0)
;
230}
231
232inline void isl_siomath_uint64_to_digits(uint64_t num, mp_digit *digits,
233 mp_size *used)
234{
235 ISL_SIOIMATH_TO_DIGITS(num, digits, *used)do { int i = 0; do { (digits)[i] = ((num) >> (sizeof(mp_digit
) * 8 * i)); i += 1; if (i >= (sizeof(num) + sizeof(mp_digit
) - 1) / sizeof(mp_digit)) break; if (((num) >> (sizeof
(mp_digit) * 8 * i)) == 0) break; } while (1); (*used) = i; }
while (0)
;
236}
237
238/* Get the IMath representation of an isl_int without modifying it.
239 * For the case it is not in big representation yet, pass some scratch space we
240 * can use to store the big representation in.
241 * In order to avoid requiring init and free on the scratch space, we directly
242 * modify the internal representation.
243 *
244 * The name derives from its indented use: getting the big representation of an
245 * input (src) argument.
246 */
247inline mp_int isl_sioimath_bigarg_src(isl_sioimath arg,
248 isl_sioimath_scratchspace_t *scratch)
249{
250 mp_int big;
251 int32_t small;
252 uint32_t num;
253
254 if (isl_sioimath_decode_big(arg, &big))
255 return big;
256
257 small = isl_sioimath_get_small(arg);
258 scratch->big.digits = scratch->digits;
259 scratch->big.alloc = ARRAY_SIZE(scratch->digits)(sizeof(scratch->digits)/sizeof(*scratch->digits));
260 if (small >= 0) {
261 scratch->big.sign = MP_ZPOS;
262 num = small;
263 } else {
264 scratch->big.sign = MP_NEG;
265 num = -small;
266 }
267
268 isl_siomath_uint32_to_digits(num, scratch->digits, &scratch->big.used);
269 return &scratch->big;
270}
271
272/* Create a temporary IMath mp_int for a signed long.
273 */
274inline mp_int isl_sioimath_siarg_src(signed long arg,
275 isl_sioimath_scratchspace_t *scratch)
276{
277 unsigned long num;
278
279 scratch->big.digits = scratch->digits;
280 scratch->big.alloc = ARRAY_SIZE(scratch->digits)(sizeof(scratch->digits)/sizeof(*scratch->digits));
281 if (arg >= 0) {
282 scratch->big.sign = MP_ZPOS;
283 num = arg;
284 } else {
285 scratch->big.sign = MP_NEG;
286 num = (arg == LONG_MIN(-9223372036854775807L -1L)) ? ((unsigned long) LONG_MAX9223372036854775807L) + 1 : -arg;
287 }
288
289 isl_siomath_ulong_to_digits(num, scratch->digits, &scratch->big.used);
290 return &scratch->big;
291}
292
293/* Create a temporary IMath mp_int for an int64_t.
294 */
295inline mp_int isl_sioimath_si64arg_src(int64_t arg,
296 isl_sioimath_scratchspace_t *scratch)
297{
298 uint64_t num;
299
300 scratch->big.digits = scratch->digits;
301 scratch->big.alloc = ARRAY_SIZE(scratch->digits)(sizeof(scratch->digits)/sizeof(*scratch->digits));
302 if (arg >= 0) {
303 scratch->big.sign = MP_ZPOS;
304 num = arg;
305 } else {
306 scratch->big.sign = MP_NEG;
307 num = (arg == INT64_MIN(-9223372036854775807L -1)) ? ((uint64_t) INT64_MAX(9223372036854775807L)) + 1 : -arg;
308 }
309
310 isl_siomath_uint64_to_digits(num, scratch->digits, &scratch->big.used);
311 return &scratch->big;
312}
313
314/* Create a temporary IMath mp_int for an unsigned long.
315 */
316inline mp_int isl_sioimath_uiarg_src(unsigned long arg,
317 isl_sioimath_scratchspace_t *scratch)
318{
319 scratch->big.digits = scratch->digits;
320 scratch->big.alloc = ARRAY_SIZE(scratch->digits)(sizeof(scratch->digits)/sizeof(*scratch->digits));
321 scratch->big.sign = MP_ZPOS;
322
323 isl_siomath_ulong_to_digits(arg, scratch->digits, &scratch->big.used);
324 return &scratch->big;
325}
326
327/* Ensure big representation. Does not preserve the current number.
328 * Callers may use the fact that the value _is_ preserved if the presentation
329 * was big before.
330 */
331inline mp_int isl_sioimath_reinit_big(isl_sioimath_ptr ptr)
332{
333 if (isl_sioimath_is_small(*ptr))
14
Dereference of null pointer (loaded from variable 'ptr')
334 *ptr = isl_sioimath_encode_big(mp_int_alloc());
335 return isl_sioimath_get_big(*ptr);
336}
337
338/* Set ptr to a number in small representation.
339 */
340inline void isl_sioimath_set_small(isl_sioimath_ptr ptr, int32_t val)
341{
342 if (isl_sioimath_is_big(*ptr))
343 mp_int_free(isl_sioimath_get_big(*ptr));
344 *ptr = isl_sioimath_encode_small(val);
345}
346
347/* Set ptr to val, choosing small representation if possible.
348 */
349inline void isl_sioimath_set_int32(isl_sioimath_ptr ptr, int32_t val)
350{
351 if (ISL_SIOIMATH_SMALL_MIN(-(2147483647)) <= val && val <= ISL_SIOIMATH_SMALL_MAX(2147483647)) {
352 isl_sioimath_set_small(ptr, val);
353 return;
354 }
355
356 mp_int_init_value(isl_sioimath_reinit_big(ptr), val);
357}
358
359/* Assign an int64_t number using small representation if possible.
360 */
361inline void isl_sioimath_set_int64(isl_sioimath_ptr ptr, int64_t val)
362{
363 if (ISL_SIOIMATH_SMALL_MIN(-(2147483647)) <= val && val <= ISL_SIOIMATH_SMALL_MAX(2147483647)) {
364 isl_sioimath_set_small(ptr, val);
365 return;
366 }
367
368 isl_sioimath_scratchspace_t scratch;
369 mp_int_copy(isl_sioimath_si64arg_src(val, &scratch),
370 isl_sioimath_reinit_big(ptr));
371}
372
373/* Convert to big representation while preserving the current number.
374 */
375inline void isl_sioimath_promote(isl_sioimath_ptr dst)
376{
377 int32_t small;
378
379 if (isl_sioimath_is_big(*dst))
380 return;
381
382 small = isl_sioimath_get_small(*dst);
383 mp_int_set_value(isl_sioimath_reinit_big(dst), small);
384}
385
386/* Convert to small representation while preserving the current number. Does
387 * nothing if dst doesn't fit small representation.
388 */
389inline void isl_sioimath_try_demote(isl_sioimath_ptr dst)
390{
391 mp_small small;
392
393 if (isl_sioimath_is_small(*dst))
394 return;
395
396 if (mp_int_to_int(isl_sioimath_get_big(*dst), &small) != MP_OK)
397 return;
398
399 if (ISL_SIOIMATH_SMALL_MIN(-(2147483647)) <= small && small <= ISL_SIOIMATH_SMALL_MAX(2147483647))
400 isl_sioimath_set_small(dst, small);
401}
402
403/* Initialize an isl_int. The implicit value is 0 in small representation.
404 */
405inline void isl_sioimath_init(isl_sioimath_ptr dst)
406{
407 *dst = isl_sioimath_encode_small(0);
408}
409
410/* Free the resources taken by an isl_int.
411 */
412inline void isl_sioimath_clear(isl_sioimath_ptr dst)
413{
414 if (isl_sioimath_is_small(*dst))
415 return;
416
417 mp_int_free(isl_sioimath_get_big(*dst));
418}
419
420/* Copy the value of one isl_int to another.
421 */
422inline void isl_sioimath_set(isl_sioimath_ptr dst, isl_sioimath_src val)
423{
424 if (isl_sioimath_is_small(val)) {
10
Assuming the condition is false
11
Taking false branch
425 isl_sioimath_set_small(dst, isl_sioimath_get_small(val));
426 return;
427 }
428
429 mp_int_copy(isl_sioimath_get_big(val), isl_sioimath_reinit_big(dst));
12
Passing null pointer value via 1st parameter 'ptr'
13
Calling 'isl_sioimath_reinit_big'
430}
431
432/* Store a signed long into an isl_int.
433 */
434inline void isl_sioimath_set_si(isl_sioimath_ptr dst, long val)
435{
436 if (ISL_SIOIMATH_SMALL_MIN(-(2147483647)) <= val && val <= ISL_SIOIMATH_SMALL_MAX(2147483647)) {
437 isl_sioimath_set_small(dst, val);
438 return;
439 }
440
441 mp_int_set_value(isl_sioimath_reinit_big(dst), val);
442}
443
444/* Store an unsigned long into an isl_int.
445 */
446inline void isl_sioimath_set_ui(isl_sioimath_ptr dst, unsigned long val)
447{
448 if (val <= ISL_SIOIMATH_SMALL_MAX(2147483647)) {
449 isl_sioimath_set_small(dst, val);
450 return;
451 }
452
453 mp_int_set_uvalue(isl_sioimath_reinit_big(dst), val);
454}
455
456/* Return whether a number can be represented by a signed long.
457 */
458inline int isl_sioimath_fits_slong(isl_sioimath_src val)
459{
460 mp_small dummy;
461
462 if (isl_sioimath_is_small(val))
463 return 1;
464
465 return mp_int_to_int(isl_sioimath_get_big(val), &dummy) == MP_OK;
466}
467
468/* Return a number as signed long. Result is undefined if the number cannot be
469 * represented as long.
470 */
471inline long isl_sioimath_get_si(isl_sioimath_src val)
472{
473 mp_small result;
474
475 if (isl_sioimath_is_small(val))
476 return isl_sioimath_get_small(val);
477
478 mp_int_to_int(isl_sioimath_get_big(val), &result);
479 return result;
480}
481
482/* Return whether a number can be represented as unsigned long.
483 */
484inline int isl_sioimath_fits_ulong(isl_sioimath_src val)
485{
486 mp_usmall dummy;
487
488 if (isl_sioimath_is_small(val))
489 return isl_sioimath_get_small(val) >= 0;
490
491 return mp_int_to_uint(isl_sioimath_get_big(val), &dummy) == MP_OK;
492}
493
494/* Return a number as unsigned long. Result is undefined if the number cannot be
495 * represented as unsigned long.
496 */
497inline unsigned long isl_sioimath_get_ui(isl_sioimath_src val)
498{
499 mp_usmall result;
500
501 if (isl_sioimath_is_small(val))
502 return isl_sioimath_get_small(val);
503
504 mp_int_to_uint(isl_sioimath_get_big(val), &result);
505 return result;
506}
507
508/* Return a number as floating point value.
509 */
510inline double isl_sioimath_get_d(isl_sioimath_src val)
511{
512 mp_int big;
513 double result = 0;
514 int i;
515
516 if (isl_sioimath_is_small(val))
517 return isl_sioimath_get_small(val);
518
519 big = isl_sioimath_get_big(val);
520 for (i = 0; i < big->used; ++i)
521 result = result * (double) ((uintmax_t) MP_DIGIT_MAX((4294967295U) * 1UL) + 1) +
522 (double) big->digits[i];
523
524 if (big->sign == MP_NEG)
525 result = -result;
526
527 return result;
528}
529
530/* Format a number as decimal string.
531 *
532 * The largest possible string from small representation is 12 characters
533 * ("-2147483647").
534 */
535inline char *isl_sioimath_get_str(isl_sioimath_src val)
536{
537 char *result;
538
539 if (isl_sioimath_is_small(val)) {
540 result = malloc(12);
541 snprintf(result, 12, "%" PRIi32"i", isl_sioimath_get_small(val));
542 return result;
543 }
544
545 return impz_get_str(NULL((void*)0), 10, isl_sioimath_get_big(val));
546}
547
548/* Return the absolute value.
549 */
550inline void isl_sioimath_abs(isl_sioimath_ptr dst, isl_sioimath_src arg)
551{
552 if (isl_sioimath_is_small(arg)) {
553 isl_sioimath_set_small(dst, labs(isl_sioimath_get_small(arg)));
554 return;
555 }
556
557 mp_int_abs(isl_sioimath_get_big(arg), isl_sioimath_reinit_big(dst));
558}
559
560/* Return the negation of a number.
561 */
562inline void isl_sioimath_neg(isl_sioimath_ptr dst, isl_sioimath_src arg)
563{
564 if (isl_sioimath_is_small(arg)) {
565 isl_sioimath_set_small(dst, -isl_sioimath_get_small(arg));
566 return;
567 }
568
569 mp_int_neg(isl_sioimath_get_big(arg), isl_sioimath_reinit_big(dst));
570}
571
572/* Swap two isl_ints.
573 *
574 * isl_sioimath can be copied bytewise; nothing depends on its address. It can
575 * also be stored in a CPU register.
576 */
577inline void isl_sioimath_swap(isl_sioimath_ptr lhs, isl_sioimath_ptr rhs)
578{
579 isl_sioimath tmp = *lhs;
580 *lhs = *rhs;
581 *rhs = tmp;
582}
583
584/* Add an unsigned long to the number.
585 *
586 * On LP64 unsigned long exceeds the range of an int64_t, therefore we check in
587 * advance whether small representation possibly overflows.
588 */
589inline void isl_sioimath_add_ui(isl_sioimath_ptr dst, isl_sioimath lhs,
590 unsigned long rhs)
591{
592 int32_t smalllhs;
593 isl_sioimath_scratchspace_t lhsscratch;
594
595 if (isl_sioimath_decode_small(lhs, &smalllhs) &&
596 (rhs <= (uint64_t) INT64_MAX(9223372036854775807L) - (uint64_t) ISL_SIOIMATH_SMALL_MAX(2147483647))) {
597 isl_sioimath_set_int64(dst, (int64_t) smalllhs + rhs);
598 return;
599 }
600
601 impz_add_ui(isl_sioimath_reinit_big(dst),
602 isl_sioimath_bigarg_src(lhs, &lhsscratch), rhs);
603 isl_sioimath_try_demote(dst);
604}
605
606/* Subtract an unsigned long.
607 *
608 * On LP64 unsigned long exceeds the range of an int64_t. If
609 * ISL_SIOIMATH_SMALL_MIN-rhs>=INT64_MIN we can do the calculation using int64_t
610 * without risking an overflow.
611 */
612inline void isl_sioimath_sub_ui(isl_sioimath_ptr dst, isl_sioimath lhs,
613 unsigned long rhs)
614{
615 int32_t smalllhs;
616 isl_sioimath_scratchspace_t lhsscratch;
617
618 if (isl_sioimath_decode_small(lhs, &smalllhs) &&
619 (rhs < (uint64_t) INT64_MIN(-9223372036854775807L -1) - (uint64_t) ISL_SIOIMATH_SMALL_MIN(-(2147483647)))) {
620 isl_sioimath_set_int64(dst, (int64_t) smalllhs - rhs);
621 return;
622 }
623
624 impz_sub_ui(isl_sioimath_reinit_big(dst),
625 isl_sioimath_bigarg_src(lhs, &lhsscratch), rhs);
626 isl_sioimath_try_demote(dst);
627}
628
629/* Sum of two isl_ints.
630 */
631inline void isl_sioimath_add(isl_sioimath_ptr dst, isl_sioimath_src lhs,
632 isl_sioimath_src rhs)
633{
634 isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
635 int32_t smalllhs, smallrhs;
636
637 if (isl_sioimath_decode_small(lhs, &smalllhs) &&
638 isl_sioimath_decode_small(rhs, &smallrhs)) {
639 isl_sioimath_set_int64(
640 dst, (int64_t) smalllhs + (int64_t) smallrhs);
641 return;
642 }
643
644 mp_int_add(isl_sioimath_bigarg_src(lhs, &scratchlhs),
645 isl_sioimath_bigarg_src(rhs, &scratchrhs),
646 isl_sioimath_reinit_big(dst));
647 isl_sioimath_try_demote(dst);
648}
649
650/* Subtract two isl_ints.
651 */
652inline void isl_sioimath_sub(isl_sioimath_ptr dst, isl_sioimath_src lhs,
653 isl_sioimath_src rhs)
654{
655 isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
656 int32_t smalllhs, smallrhs;
657
658 if (isl_sioimath_decode_small(lhs, &smalllhs) &&
659 isl_sioimath_decode_small(rhs, &smallrhs)) {
660 isl_sioimath_set_int64(
661 dst, (int64_t) smalllhs - (int64_t) smallrhs);
662 return;
663 }
664
665 mp_int_sub(isl_sioimath_bigarg_src(lhs, &scratchlhs),
666 isl_sioimath_bigarg_src(rhs, &scratchrhs),
667 isl_sioimath_reinit_big(dst));
668 isl_sioimath_try_demote(dst);
669}
670
671/* Multiply two isl_ints.
672 */
673inline void isl_sioimath_mul(isl_sioimath_ptr dst, isl_sioimath_src lhs,
674 isl_sioimath_src rhs)
675{
676 isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
677 int32_t smalllhs, smallrhs;
678
679 if (isl_sioimath_decode_small(lhs, &smalllhs) &&
680 isl_sioimath_decode_small(rhs, &smallrhs)) {
681 isl_sioimath_set_int64(
682 dst, (int64_t) smalllhs * (int64_t) smallrhs);
683 return;
684 }
685
686 mp_int_mul(isl_sioimath_bigarg_src(lhs, &scratchlhs),
687 isl_sioimath_bigarg_src(rhs, &scratchrhs),
688 isl_sioimath_reinit_big(dst));
689 isl_sioimath_try_demote(dst);
690}
691
692/* Shift lhs by rhs bits to the left and store the result in dst. Effectively,
693 * this operation computes 'lhs * 2^rhs'.
694 */
695inline void isl_sioimath_mul_2exp(isl_sioimath_ptr dst, isl_sioimath lhs,
696 unsigned long rhs)
697{
698 isl_sioimath_scratchspace_t scratchlhs;
699 int32_t smalllhs;
700
701 if (isl_sioimath_decode_small(lhs, &smalllhs) && (rhs <= 32ul)) {
702 isl_sioimath_set_int64(dst, ((int64_t) smalllhs) << rhs);
703 return;
704 }
705
706 mp_int_mul_pow2(isl_sioimath_bigarg_src(lhs, &scratchlhs), rhs,
707 isl_sioimath_reinit_big(dst));
708}
709
710/* Multiply an isl_int and a signed long.
711 */
712inline void isl_sioimath_mul_si(isl_sioimath_ptr dst, isl_sioimath lhs,
713 signed long rhs)
714{
715 isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
716 int32_t smalllhs;
717
718 if (isl_sioimath_decode_small(lhs, &smalllhs) && (rhs > LONG_MIN(-9223372036854775807L -1L)) &&
719 (labs(rhs) <= UINT32_MAX(4294967295U))) {
720 isl_sioimath_set_int64(dst, (int64_t) smalllhs * (int64_t) rhs);
721 return;
722 }
723
724 mp_int_mul(isl_sioimath_bigarg_src(lhs, &scratchlhs),
725 isl_sioimath_siarg_src(rhs, &scratchrhs),
726 isl_sioimath_reinit_big(dst));
727 isl_sioimath_try_demote(dst);
728}
729
730/* Multiply an isl_int and an unsigned long.
731 */
732inline void isl_sioimath_mul_ui(isl_sioimath_ptr dst, isl_sioimath lhs,
733 unsigned long rhs)
734{
735 isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
736 int32_t smalllhs;
737
738 if (isl_sioimath_decode_small(lhs, &smalllhs) && (rhs <= UINT32_MAX(4294967295U))) {
739 isl_sioimath_set_int64(dst, (int64_t) smalllhs * (int64_t) rhs);
740 return;
741 }
742
743 mp_int_mul(isl_sioimath_bigarg_src(lhs, &scratchlhs),
744 isl_sioimath_uiarg_src(rhs, &scratchrhs),
745 isl_sioimath_reinit_big(dst));
746 isl_sioimath_try_demote(dst);
747}
748
749/* Compute the power of an isl_int to an unsigned long.
750 * Always let IMath do it; the result is unlikely to be small except in some
751 * special cases.
752 * Note: 0^0 == 1
753 */
754inline void isl_sioimath_pow_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
755 unsigned long rhs)
756{
757 isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
758 int32_t smalllhs;
759
760 switch (rhs) {
761 case 0:
762 isl_sioimath_set_small(dst, 1);
763 return;
764 case 1:
765 isl_sioimath_set(dst, lhs);
766 return;
767 case 2:
768 isl_sioimath_mul(dst, lhs, lhs);
769 return;
770 }
771
772 if (isl_sioimath_decode_small(lhs, &smalllhs)) {
773 switch (smalllhs) {
774 case 0:
775 isl_sioimath_set_small(dst, 0);
776 return;
777 case 1:
778 isl_sioimath_set_small(dst, 1);
779 return;
780 case 2:
781 isl_sioimath_set_small(dst, 1);
782 isl_sioimath_mul_2exp(dst, *dst, rhs);
783 return;
784 default:
785 if ((MP_SMALL_MIN(-9223372036854775807L -1L) <= rhs) && (rhs <= MP_SMALL_MAX9223372036854775807L)) {
786 mp_int_expt_value(smalllhs, rhs,
787 isl_sioimath_reinit_big(dst));
788 isl_sioimath_try_demote(dst);
789 return;
790 }
791 }
792 }
793
794 mp_int_expt_full(isl_sioimath_bigarg_src(lhs, &scratchlhs),
795 isl_sioimath_uiarg_src(rhs, &scratchrhs),
796 isl_sioimath_reinit_big(dst));
797 isl_sioimath_try_demote(dst);
798}
799
800/* Fused multiply-add.
801 */
802inline void isl_sioimath_addmul(isl_sioimath_ptr dst, isl_sioimath_src lhs,
803 isl_sioimath_src rhs)
804{
805 isl_sioimath tmp;
806 isl_sioimath_init(&tmp);
807 isl_sioimath_mul(&tmp, lhs, rhs);
808 isl_sioimath_add(dst, *dst, tmp);
809 isl_sioimath_clear(&tmp);
810}
811
812/* Fused multiply-add with an unsigned long.
813 */
814inline void isl_sioimath_addmul_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
815 unsigned long rhs)
816{
817 isl_sioimath tmp;
818 isl_sioimath_init(&tmp);
819 isl_sioimath_mul_ui(&tmp, lhs, rhs);
820 isl_sioimath_add(dst, *dst, tmp);
821 isl_sioimath_clear(&tmp);
822}
823
824/* Fused multiply-subtract.
825 */
826inline void isl_sioimath_submul(isl_sioimath_ptr dst, isl_sioimath_src lhs,
827 isl_sioimath_src rhs)
828{
829 isl_sioimath tmp;
830 isl_sioimath_init(&tmp);
831 isl_sioimath_mul(&tmp, lhs, rhs);
832 isl_sioimath_sub(dst, *dst, tmp);
833 isl_sioimath_clear(&tmp);
834}
835
836/* Fused multiply-add with an unsigned long.
837 */
838inline void isl_sioimath_submul_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
839 unsigned long rhs)
840{
841 isl_sioimath tmp;
842 isl_sioimath_init(&tmp);
843 isl_sioimath_mul_ui(&tmp, lhs, rhs);
844 isl_sioimath_sub(dst, *dst, tmp);
845 isl_sioimath_clear(&tmp);
846}
847
848void isl_sioimath_gcd(isl_sioimath_ptr dst, isl_sioimath_src lhs,
849 isl_sioimath_src rhs);
850void isl_sioimath_lcm(isl_sioimath_ptr dst, isl_sioimath_src lhs,
851 isl_sioimath_src rhs);
852
853/* Divide lhs by rhs, rounding to zero (Truncate).
854 */
855inline void isl_sioimath_tdiv_q(isl_sioimath_ptr dst, isl_sioimath_src lhs,
856 isl_sioimath_src rhs)
857{
858 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
859 int32_t lhssmall, rhssmall;
860
861 if (isl_sioimath_decode_small(lhs, &lhssmall) &&
862 isl_sioimath_decode_small(rhs, &rhssmall)) {
863 isl_sioimath_set_small(dst, lhssmall / rhssmall);
864 return;
865 }
866
867 mp_int_div(isl_sioimath_bigarg_src(lhs, &lhsscratch),
868 isl_sioimath_bigarg_src(rhs, &rhsscratch),
869 isl_sioimath_reinit_big(dst), NULL((void*)0));
870 isl_sioimath_try_demote(dst);
871 return;
872}
873
874/* Divide lhs by an unsigned long rhs, rounding to zero (Truncate).
875 */
876inline void isl_sioimath_tdiv_q_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
877 unsigned long rhs)
878{
879 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
880 int32_t lhssmall;
881
882 if (isl_sioimath_is_small(lhs) && (rhs <= (unsigned long) INT32_MAX(2147483647))) {
883 lhssmall = isl_sioimath_get_small(lhs);
884 isl_sioimath_set_small(dst, lhssmall / (int32_t) rhs);
885 return;
886 }
887
888 if (rhs <= MP_SMALL_MAX9223372036854775807L) {
889 mp_int_div_value(isl_sioimath_bigarg_src(lhs, &lhsscratch), rhs,
890 isl_sioimath_reinit_big(dst), NULL((void*)0));
891 isl_sioimath_try_demote(dst);
892 return;
893 }
894
895 mp_int_div(isl_sioimath_bigarg_src(lhs, &lhsscratch),
896 isl_sioimath_uiarg_src(rhs, &rhsscratch),
897 isl_sioimath_reinit_big(dst), NULL((void*)0));
898 isl_sioimath_try_demote(dst);
899}
900
901/* Divide lhs by rhs, rounding to positive infinity (Ceil).
902 */
903inline void isl_sioimath_cdiv_q(isl_sioimath_ptr dst, isl_sioimath_src lhs,
904 isl_sioimath_src rhs)
905{
906 int32_t lhssmall, rhssmall;
907 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
908 int32_t q;
909
910 if (isl_sioimath_decode_small(lhs, &lhssmall) &&
911 isl_sioimath_decode_small(rhs, &rhssmall)) {
912 if ((lhssmall >= 0) && (rhssmall >= 0))
913 q = ((int64_t) lhssmall + (int64_t) rhssmall - 1) /
914 rhssmall;
915 else if ((lhssmall < 0) && (rhssmall < 0))
916 q = ((int64_t) lhssmall + (int64_t) rhssmall + 1) /
917 rhssmall;
918 else
919 q = lhssmall / rhssmall;
920 isl_sioimath_set_small(dst, q);
921 return;
922 }
923
924 impz_cdiv_q(isl_sioimath_reinit_big(dst),
925 isl_sioimath_bigarg_src(lhs, &lhsscratch),
926 isl_sioimath_bigarg_src(rhs, &rhsscratch));
927 isl_sioimath_try_demote(dst);
928}
929
930/* Compute the division of lhs by a rhs of type unsigned long, rounding towards
931 * positive infinity (Ceil).
932 */
933inline void isl_sioimath_cdiv_q_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
934 unsigned long rhs)
935{
936 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
937 int32_t lhssmall, q;
938
939 if (isl_sioimath_decode_small(lhs, &lhssmall) && (rhs <= INT32_MAX(2147483647))) {
940 if (lhssmall >= 0)
941 q = ((int64_t) lhssmall + ((int64_t) rhs - 1)) /
942 (int64_t) rhs;
943 else
944 q = lhssmall / (int32_t) rhs;
945 isl_sioimath_set_small(dst, q);
946 return;
947 }
948
949 impz_cdiv_q(isl_sioimath_reinit_big(dst),
950 isl_sioimath_bigarg_src(lhs, &lhsscratch),
951 isl_sioimath_uiarg_src(rhs, &rhsscratch));
952 isl_sioimath_try_demote(dst);
953}
954
955/* Divide lhs by rhs, rounding to negative infinity (Floor).
956 */
957inline void isl_sioimath_fdiv_q(isl_sioimath_ptr dst, isl_sioimath_src lhs,
958 isl_sioimath_src rhs)
959{
960 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
961 int32_t lhssmall, rhssmall;
962 int32_t q;
963
964 if (isl_sioimath_decode_small(lhs, &lhssmall) &&
965 isl_sioimath_decode_small(rhs, &rhssmall)) {
966 if ((lhssmall < 0) && (rhssmall >= 0))
967 q = ((int64_t) lhssmall - ((int64_t) rhssmall - 1)) /
968 rhssmall;
969 else if ((lhssmall >= 0) && (rhssmall < 0))
970 q = ((int64_t) lhssmall - ((int64_t) rhssmall + 1)) /
971 rhssmall;
972 else
973 q = lhssmall / rhssmall;
974 isl_sioimath_set_small(dst, q);
975 return;
976 }
977
978 impz_fdiv_q(isl_sioimath_reinit_big(dst),
979 isl_sioimath_bigarg_src(lhs, &lhsscratch),
980 isl_sioimath_bigarg_src(rhs, &rhsscratch));
981 isl_sioimath_try_demote(dst);
982}
983
984/* Compute the division of lhs by a rhs of type unsigned long, rounding towards
985 * negative infinity (Floor).
986 */
987inline void isl_sioimath_fdiv_q_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
988 unsigned long rhs)
989{
990 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
991 int32_t lhssmall, q;
992
993 if (isl_sioimath_decode_small(lhs, &lhssmall) && (rhs <= INT32_MAX(2147483647))) {
994 if (lhssmall >= 0)
995 q = (uint32_t) lhssmall / rhs;
996 else
997 q = ((int64_t) lhssmall - ((int64_t) rhs - 1)) /
998 (int64_t) rhs;
999 isl_sioimath_set_small(dst, q);
1000 return;
1001 }
1002
1003 impz_fdiv_q(isl_sioimath_reinit_big(dst),
1004 isl_sioimath_bigarg_src(lhs, &lhsscratch),
1005 isl_sioimath_uiarg_src(rhs, &rhsscratch));
1006 isl_sioimath_try_demote(dst);
1007}
1008
1009/* Get the remainder of: lhs divided by rhs rounded towards negative infinite
1010 * (Floor).
1011 */
1012inline void isl_sioimath_fdiv_r(isl_sioimath_ptr dst, isl_sioimath_src lhs,
1013 isl_sioimath_src rhs)
1014{
1015 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
1016 int64_t lhssmall, rhssmall;
1017 int32_t r;
1018
1019 if (isl_sioimath_is_small(lhs) && isl_sioimath_is_small(rhs)) {
1020 lhssmall = isl_sioimath_get_small(lhs);
1021 rhssmall = isl_sioimath_get_small(rhs);
1022 r = (rhssmall + lhssmall % rhssmall) % rhssmall;
1023 isl_sioimath_set_small(dst, r);
1024 return;
1025 }
1026
1027 impz_fdiv_r(isl_sioimath_reinit_big(dst),
1028 isl_sioimath_bigarg_src(lhs, &lhsscratch),
1029 isl_sioimath_bigarg_src(rhs, &rhsscratch));
1030 isl_sioimath_try_demote(dst);
1031}
1032
1033void isl_sioimath_read(isl_sioimath_ptr dst, const char *str);
1034
1035/* Return:
1036 * +1 for a positive number
1037 * -1 for a negative number
1038 * 0 if the number is zero
1039 */
1040inline int isl_sioimath_sgn(isl_sioimath_src arg)
1041{
1042 int32_t small;
1043
1044 if (isl_sioimath_decode_small(arg, &small))
1045 return (small > 0) - (small < 0);
1046
1047 return mp_int_compare_zero(isl_sioimath_get_big(arg));
1048}
1049
1050/* Return:
1051 * +1 if lhs > rhs
1052 * -1 if lhs < rhs
1053 * 0 if lhs = rhs
1054 */
1055inline int isl_sioimath_cmp(isl_sioimath_src lhs, isl_sioimath_src rhs)
1056{
1057 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
1058 int32_t lhssmall, rhssmall;
1059
1060 if (isl_sioimath_decode_small(lhs, &lhssmall) &&
1061 isl_sioimath_decode_small(rhs, &rhssmall))
1062 return (lhssmall > rhssmall) - (lhssmall < rhssmall);
1063
1064 if (isl_sioimath_decode_small(rhs, &rhssmall))
1065 return mp_int_compare_value(
1066 isl_sioimath_bigarg_src(lhs, &lhsscratch), rhssmall);
1067
1068 if (isl_sioimath_decode_small(lhs, &lhssmall))
1069 return -mp_int_compare_value(
1070 isl_sioimath_bigarg_src(rhs, &rhsscratch), lhssmall);
1071
1072 return mp_int_compare(
1073 isl_sioimath_get_big(lhs), isl_sioimath_get_big(rhs));
1074}
1075
1076/* As isl_sioimath_cmp, but with signed long rhs.
1077 */
1078inline int isl_sioimath_cmp_si(isl_sioimath_src lhs, signed long rhs)
1079{
1080 int32_t lhssmall;
1081
1082 if (isl_sioimath_decode_small(lhs, &lhssmall))
1083 return (lhssmall > rhs) - (lhssmall < rhs);
1084
1085 return mp_int_compare_value(isl_sioimath_get_big(lhs), rhs);
1086}
1087
1088/* Return:
1089 * +1 if |lhs| > |rhs|
1090 * -1 if |lhs| < |rhs|
1091 * 0 if |lhs| = |rhs|
1092 */
1093inline int isl_sioimath_abs_cmp(isl_sioimath_src lhs, isl_sioimath_src rhs)
1094{
1095 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
1096 int32_t lhssmall, rhssmall;
1097
1098 if (isl_sioimath_decode_small(lhs, &lhssmall) &&
1099 isl_sioimath_decode_small(rhs, &rhssmall)) {
1100 lhssmall = labs(lhssmall);
1101 rhssmall = labs(rhssmall);
1102 return (lhssmall > rhssmall) - (lhssmall < rhssmall);
1103 }
1104
1105 return mp_int_compare_unsigned(
1106 isl_sioimath_bigarg_src(lhs, &lhsscratch),
1107 isl_sioimath_bigarg_src(rhs, &rhsscratch));
1108}
1109
1110/* Return whether lhs is divisible by rhs.
1111 * In particular, can rhs be multiplied by some integer to result in lhs?
1112 * If rhs is zero, then this means lhs has to be zero too.
1113 */
1114inline int isl_sioimath_is_divisible_by(isl_sioimath_src lhs,
1115 isl_sioimath_src rhs)
1116{
1117 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
1118 int32_t lhssmall, rhssmall;
1119 mpz_t rem;
1120 int cmp;
1121
1122 if (isl_sioimath_sgn(rhs) == 0)
1123 return isl_sioimath_sgn(lhs) == 0;
1124
1125 if (isl_sioimath_decode_small(lhs, &lhssmall) &&
1126 isl_sioimath_decode_small(rhs, &rhssmall))
1127 return lhssmall % rhssmall == 0;
1128
1129 if (isl_sioimath_decode_small(rhs, &rhssmall))
1130 return mp_int_divisible_value(
1131 isl_sioimath_bigarg_src(lhs, &lhsscratch), rhssmall);
1132
1133 mp_int_init(&rem);
1134 mp_int_div(isl_sioimath_bigarg_src(lhs, &lhsscratch),
1135 isl_sioimath_bigarg_src(rhs, &rhsscratch), NULL((void*)0), &rem);
1136 cmp = mp_int_compare_zero(&rem);
1137 mp_int_clear(&rem);
1138 return cmp == 0;
1139}
1140
1141/* Return a hash code of an isl_sioimath.
1142 * The hash code for a number in small and big representation must be identical
1143 * on the same machine because small representation if not obligatory if fits.
1144 */
1145inline uint32_t isl_sioimath_hash(isl_sioimath_src arg, uint32_t hash)
1146{
1147 int32_t small;
1148 int i;
1149 uint32_t num;
1150 mp_digit digits[(sizeof(uint32_t) + sizeof(mp_digit) - 1) /
1151 sizeof(mp_digit)];
1152 mp_size used;
1153 const unsigned char *digitdata = (const unsigned char *) &digits;
1154
1155 if (isl_sioimath_decode_small(arg, &small)) {
1156 if (small < 0)
1157 isl_hash_byte(hash, 0xFF)do { hash *= 16777619; hash ^= 0xFF; } while(0);
1158 num = labs(small);
1159
1160 isl_siomath_uint32_to_digits(num, digits, &used);
1161 for (i = 0; i < used * sizeof(mp_digit); i += 1)
1162 isl_hash_byte(hash, digitdata[i])do { hash *= 16777619; hash ^= digitdata[i]; } while(0);
1163 return hash;
1164 }
1165
1166 return isl_imath_hash(isl_sioimath_get_big(arg), hash);
1167}
1168
1169/* Return the number of digits in a number of the given base or more, i.e. the
1170 * string length without sign and null terminator.
1171 *
1172 * Current implementation for small representation returns the maximal number
1173 * of binary digits in that representation, which can be much larger than the
1174 * smallest possible solution.
1175 */
1176inline size_t isl_sioimath_sizeinbase(isl_sioimath_src arg, int base)
1177{
1178 int32_t small;
1179
1180 if (isl_sioimath_decode_small(arg, &small))
1181 return sizeof(int32_t) * CHAR_BIT8 - 1;
1182
1183 return impz_sizeinbase(isl_sioimath_get_big(arg), base);
1184}
1185
1186void isl_sioimath_print(FILE *out, isl_sioimath_src i, int width);
1187void isl_sioimath_dump(isl_sioimath_src arg);
1188
1189typedef isl_sioimath isl_int[1];
1190#define isl_int_init(i)isl_sioimath_init((i)) isl_sioimath_init((i))
1191#define isl_int_clear(i)isl_sioimath_clear((i)) isl_sioimath_clear((i))
1192
1193#define isl_int_set(r, i)isl_sioimath_set((r), *(i)) isl_sioimath_set((r), *(i))
1194#define isl_int_set_si(r, i)isl_sioimath_set_si((r), i) isl_sioimath_set_si((r), i)
1195#define isl_int_set_ui(r, i)isl_sioimath_set_ui((r), i) isl_sioimath_set_ui((r), i)
1196#define isl_int_fits_slong(r)isl_sioimath_fits_slong(*(r)) isl_sioimath_fits_slong(*(r))
1197#define isl_int_get_si(r)isl_sioimath_get_si(*(r)) isl_sioimath_get_si(*(r))
1198#define isl_int_fits_ulong(r)isl_sioimath_fits_ulong(*(r)) isl_sioimath_fits_ulong(*(r))
1199#define isl_int_get_ui(r)isl_sioimath_get_ui(*(r)) isl_sioimath_get_ui(*(r))
1200#define isl_int_get_d(r)isl_sioimath_get_d(*(r)) isl_sioimath_get_d(*(r))
1201#define isl_int_get_str(r)isl_sioimath_get_str(*(r)) isl_sioimath_get_str(*(r))
1202#define isl_int_abs(r, i)isl_sioimath_abs((r), *(i)) isl_sioimath_abs((r), *(i))
1203#define isl_int_neg(r, i)isl_sioimath_neg((r), *(i)) isl_sioimath_neg((r), *(i))
1204#define isl_int_swap(i, j)isl_sioimath_swap((i), (j)) isl_sioimath_swap((i), (j))
1205#define isl_int_swap_or_set(i, j)isl_sioimath_swap((i), (j)) isl_sioimath_swap((i), (j))
1206#define isl_int_add_ui(r, i, j)isl_sioimath_add_ui((r), *(i), j) isl_sioimath_add_ui((r), *(i), j)
1207#define isl_int_sub_ui(r, i, j)isl_sioimath_sub_ui((r), *(i), j) isl_sioimath_sub_ui((r), *(i), j)
1208
1209#define isl_int_add(r, i, j)isl_sioimath_add((r), *(i), *(j)) isl_sioimath_add((r), *(i), *(j))
1210#define isl_int_sub(r, i, j)isl_sioimath_sub((r), *(i), *(j)) isl_sioimath_sub((r), *(i), *(j))
1211#define isl_int_mul(r, i, j)isl_sioimath_mul((r), *(i), *(j)) isl_sioimath_mul((r), *(i), *(j))
1212#define isl_int_mul_2exp(r, i, j)isl_sioimath_mul_2exp((r), *(i), j) isl_sioimath_mul_2exp((r), *(i), j)
1213#define isl_int_mul_si(r, i, j)isl_sioimath_mul_si((r), *(i), j) isl_sioimath_mul_si((r), *(i), j)
1214#define isl_int_mul_ui(r, i, j)isl_sioimath_mul_ui((r), *(i), j) isl_sioimath_mul_ui((r), *(i), j)
1215#define isl_int_pow_ui(r, i, j)isl_sioimath_pow_ui((r), *(i), j) isl_sioimath_pow_ui((r), *(i), j)
1216#define isl_int_addmul(r, i, j)isl_sioimath_addmul((r), *(i), *(j)) isl_sioimath_addmul((r), *(i), *(j))
1217#define isl_int_addmul_ui(r, i, j)isl_sioimath_addmul_ui((r), *(i), j) isl_sioimath_addmul_ui((r), *(i), j)
1218#define isl_int_submul(r, i, j)isl_sioimath_submul((r), *(i), *(j)) isl_sioimath_submul((r), *(i), *(j))
1219#define isl_int_submul_ui(r, i, j)isl_sioimath_submul_ui((r), *(i), j) isl_sioimath_submul_ui((r), *(i), j)
1220
1221#define isl_int_gcd(r, i, j)isl_sioimath_gcd((r), *(i), *(j)) isl_sioimath_gcd((r), *(i), *(j))
1222#define isl_int_lcm(r, i, j)isl_sioimath_lcm((r), *(i), *(j)) isl_sioimath_lcm((r), *(i), *(j))
1223#define isl_int_divexact(r, i, j)isl_sioimath_tdiv_q((r), *(i), *(j)) isl_sioimath_tdiv_q((r), *(i), *(j))
1224#define isl_int_divexact_ui(r, i, j)isl_sioimath_tdiv_q_ui((r), *(i), j) isl_sioimath_tdiv_q_ui((r), *(i), j)
1225#define isl_int_tdiv_q(r, i, j)isl_sioimath_tdiv_q((r), *(i), *(j)) isl_sioimath_tdiv_q((r), *(i), *(j))
1226#define isl_int_cdiv_q(r, i, j)isl_sioimath_cdiv_q((r), *(i), *(j)) isl_sioimath_cdiv_q((r), *(i), *(j))
1227#define isl_int_cdiv_q_ui(r, i, j)isl_sioimath_cdiv_q_ui((r), *(i), j) isl_sioimath_cdiv_q_ui((r), *(i), j)
1228#define isl_int_fdiv_q(r, i, j)isl_sioimath_fdiv_q((r), *(i), *(j)) isl_sioimath_fdiv_q((r), *(i), *(j))
1229#define isl_int_fdiv_r(r, i, j)isl_sioimath_fdiv_r((r), *(i), *(j)) isl_sioimath_fdiv_r((r), *(i), *(j))
1230#define isl_int_fdiv_q_ui(r, i, j)isl_sioimath_fdiv_q_ui((r), *(i), j) isl_sioimath_fdiv_q_ui((r), *(i), j)
1231
1232#define isl_int_read(r, s)isl_sioimath_read((r), s) isl_sioimath_read((r), s)
1233#define isl_int_sgn(i)isl_sioimath_sgn(*(i)) isl_sioimath_sgn(*(i))
1234#define isl_int_cmp(i, j)isl_sioimath_cmp(*(i), *(j)) isl_sioimath_cmp(*(i), *(j))
1235#define isl_int_cmp_si(i, si)isl_sioimath_cmp_si(*(i), si) isl_sioimath_cmp_si(*(i), si)
1236#define isl_int_eq(i, j)(isl_sioimath_cmp(*(i), *(j)) == 0) (isl_sioimath_cmp(*(i), *(j)) == 0)
1237#define isl_int_ne(i, j)(isl_sioimath_cmp(*(i), *(j)) != 0) (isl_sioimath_cmp(*(i), *(j)) != 0)
1238#define isl_int_lt(i, j)(isl_sioimath_cmp(*(i), *(j)) < 0) (isl_sioimath_cmp(*(i), *(j)) < 0)
1239#define isl_int_le(i, j)(isl_sioimath_cmp(*(i), *(j)) <= 0) (isl_sioimath_cmp(*(i), *(j)) <= 0)
1240#define isl_int_gt(i, j)(isl_sioimath_cmp(*(i), *(j)) > 0) (isl_sioimath_cmp(*(i), *(j)) > 0)
1241#define isl_int_ge(i, j)(isl_sioimath_cmp(*(i), *(j)) >= 0) (isl_sioimath_cmp(*(i), *(j)) >= 0)
1242#define isl_int_abs_cmp(i, j)isl_sioimath_abs_cmp(*(i), *(j)) isl_sioimath_abs_cmp(*(i), *(j))
1243#define isl_int_abs_eq(i, j)(isl_sioimath_abs_cmp(*(i), *(j)) == 0) (isl_sioimath_abs_cmp(*(i), *(j)) == 0)
1244#define isl_int_abs_ne(i, j)(isl_sioimath_abs_cmp(*(i), *(j)) != 0) (isl_sioimath_abs_cmp(*(i), *(j)) != 0)
1245#define isl_int_abs_lt(i, j)(isl_sioimath_abs_cmp(*(i), *(j)) < 0) (isl_sioimath_abs_cmp(*(i), *(j)) < 0)
1246#define isl_int_abs_gt(i, j)(isl_sioimath_abs_cmp(*(i), *(j)) > 0) (isl_sioimath_abs_cmp(*(i), *(j)) > 0)
1247#define isl_int_abs_ge(i, j)(isl_sioimath_abs_cmp(*(i), *(j)) >= 0) (isl_sioimath_abs_cmp(*(i), *(j)) >= 0)
1248#define isl_int_is_divisible_by(i, j)isl_sioimath_is_divisible_by(*(i), *(j)) isl_sioimath_is_divisible_by(*(i), *(j))
1249
1250#define isl_int_hash(v, h)isl_sioimath_hash(*(v), h) isl_sioimath_hash(*(v), h)
1251#define isl_int_free_str(s)free(s) free(s)
1252#define isl_int_print(out, i, width)isl_sioimath_print(out, *(i), width) isl_sioimath_print(out, *(i), width)
1253
1254#endif /* ISL_INT_SIOIMATH_H */