isl_pw_*_eval: align parameters of arguments
[isl.git] / isl_test.c
blobe4f871f219b429c315fda7bb24c741c0f20b76dd
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <assert.h>
19 #include <stdio.h>
20 #include <limits.h>
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl/id.h>
26 #include <isl/set.h>
27 #include <isl/flow.h>
28 #include <isl_constraint_private.h>
29 #include <isl/polynomial.h>
30 #include <isl/union_set.h>
31 #include <isl/union_map.h>
32 #include <isl_factorization.h>
33 #include <isl/schedule.h>
34 #include <isl/schedule_node.h>
35 #include <isl_options_private.h>
36 #include <isl_vertices_private.h>
37 #include <isl/ast_build.h>
38 #include <isl/val.h>
39 #include <isl/ilp.h>
40 #include <isl_ast_build_expr.h>
41 #include <isl/options.h>
43 #include "isl_srcdir.c"
45 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
47 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
48 char *filename;
49 int length;
50 char *pattern = "%s/test_inputs/%s.%s";
52 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
53 + strlen(suffix) + 1;
54 filename = isl_alloc_array(ctx, char, length);
56 if (!filename)
57 return NULL;
59 sprintf(filename, pattern, srcdir, name, suffix);
61 return filename;
64 void test_parse_map(isl_ctx *ctx, const char *str)
66 isl_map *map;
68 map = isl_map_read_from_str(ctx, str);
69 assert(map);
70 isl_map_free(map);
73 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
75 isl_map *map, *map2;
76 int equal;
78 map = isl_map_read_from_str(ctx, str);
79 map2 = isl_map_read_from_str(ctx, str2);
80 equal = isl_map_is_equal(map, map2);
81 isl_map_free(map);
82 isl_map_free(map2);
84 if (equal < 0)
85 return -1;
86 if (!equal)
87 isl_die(ctx, isl_error_unknown, "maps not equal",
88 return -1);
90 return 0;
93 void test_parse_pwqp(isl_ctx *ctx, const char *str)
95 isl_pw_qpolynomial *pwqp;
97 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
98 assert(pwqp);
99 isl_pw_qpolynomial_free(pwqp);
102 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
104 isl_pw_aff *pwaff;
106 pwaff = isl_pw_aff_read_from_str(ctx, str);
107 assert(pwaff);
108 isl_pw_aff_free(pwaff);
111 /* Check that we can read an isl_multi_val from "str" without errors.
113 static int test_parse_multi_val(isl_ctx *ctx, const char *str)
115 isl_multi_val *mv;
117 mv = isl_multi_val_read_from_str(ctx, str);
118 isl_multi_val_free(mv);
120 return mv ? 0 : -1;
123 /* String descriptions of multi piecewise affine expressions
124 * that are used for testing printing and parsing.
126 static const char *reparse_multi_pw_aff_tests[] = {
127 "{ A[x, y] -> [] : x + y >= 0 }",
128 "{ A[x, y] -> B[] : x + y >= 0 }",
129 "{ A[x, y] -> [x] : x + y >= 0 }",
130 "[N] -> { A[x, y] -> [x] : x + y <= N }",
131 "{ A[x, y] -> [x, y] : x + y >= 0 }",
132 "{ A[x, y] -> [(x : x >= 0), (y : y >= 0)] : x + y >= 0 }",
133 "[N] -> { [] : N >= 0 }",
134 "[N] -> { [] : N >= 0 }",
135 "[N] -> { [N] : N >= 0 }",
136 "[N] -> { [N, N + 1] : N >= 0 }",
137 "[N, M] -> { [(N : N >= 0), (M : M >= 0)] : N + M >= 0 }",
138 "{ [a] -> [b = a] }",
139 "{ [a] -> [b = a] : a >= 0 }",
142 #undef BASE
143 #define BASE multi_pw_aff
145 #include "check_reparse_templ.c"
146 #include "check_reparse_test_templ.c"
148 /* String descriptions that cannot be parsed
149 * as multi piecewise affine expressions.
151 static const char *parse_multi_pw_aff_fail_tests[] = {
152 "{ [a] -> [b] : b = a }",
153 "{ [a] -> [b = a] : b >= 0 }",
156 #include "check_parse_fail_test_templ.c"
158 /* String descriptions of piecewise multi affine expressions
159 * that are used for testing printing and parsing.
161 static const char *reparse_pw_multi_aff_tests[] = {
162 "{ [x] -> [x] }",
163 "{ [x] -> [x % 4] }",
164 "{ [x] -> [x % 4] : x mod 3 = 1 }",
165 "{ [x, x] -> [x % 4] }",
166 "{ [x, x + 1] -> [x % 4] : x mod 3 = 1 }",
167 "{ [x, x mod 2] -> [x % 4] }",
168 "{ [a] -> [a//2] : exists (e0: 8*floor((-a + e0)/8) <= -8 - a + 8e0) }",
171 #undef BASE
172 #define BASE pw_multi_aff
174 #include "check_reparse_templ.c"
175 #include "check_reparse_test_templ.c"
177 /* Test parsing of piecewise multi affine expressions by printing
178 * the expressions and checking that parsing the output results
179 * in the same expression.
180 * Do this for an expression converted from a map with an output
181 * dimension name that is equal to an automatically generated name, and
182 * a set of expressions parsed from strings.
184 static isl_stat test_parse_pma(isl_ctx *ctx)
186 isl_map *map;
187 isl_pw_multi_aff *pma;
189 map = isl_map_read_from_str(ctx, "{ [a, a] -> [i1 = a + 1] }");
190 pma = isl_pw_multi_aff_from_map(map);
191 if (check_reparse_pw_multi_aff(ctx, pma) < 0)
192 return isl_stat_error;
194 if (check_reparse_pw_multi_aff_tests(ctx) < 0)
195 return isl_stat_error;
197 return isl_stat_ok;
200 /* String descriptions that cannot be parsed
201 * as union piecewise multi affine expressions.
203 static const char *parse_union_pw_multi_aff_fail_tests[] = {
204 "{ [a] -> [b] : b = a }",
205 "{ [a] -> [b = a] : b >= 0 }",
208 #undef BASE
209 #define BASE union_pw_multi_aff
211 #include "check_parse_fail_test_templ.c"
213 /* Test parsing of union piecewise multi affine expressions.
215 * In particular, check some cases where parsing is supposed to fail.
217 static isl_stat test_parse_upma(isl_ctx *ctx)
219 if (check_parse_union_pw_multi_aff_fail_tests(ctx) < 0)
220 return isl_stat_error;
222 return isl_stat_ok;
225 /* Test parsing of multi piecewise affine expressions by printing
226 * the expressions and checking that parsing the output results
227 * in the same expression.
228 * Do this for a couple of manually constructed expressions,
229 * an expression converted from a map with an output dimension name
230 * that is equal to an automatically generated name, and
231 * a set of expressions parsed from strings.
233 * Additionally, check some cases where parsing is supposed to fail.
235 static int test_parse_mpa(isl_ctx *ctx)
237 isl_space *space;
238 isl_set *dom;
239 isl_map *map;
240 isl_pw_multi_aff *pma;
241 isl_multi_pw_aff *mpa;
242 isl_stat r;
244 space = isl_space_set_alloc(ctx, 0, 0);
245 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
246 mpa = isl_multi_pw_aff_zero(space);
247 r = check_reparse_multi_pw_aff(ctx, mpa);
248 if (r < 0)
249 return -1;
251 space = isl_space_set_alloc(ctx, 1, 0);
252 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
253 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
254 dom = isl_set_universe(isl_space_params(isl_space_copy(space)));
255 dom = isl_set_lower_bound_si(dom, isl_dim_param, 0, 5);
256 mpa = isl_multi_pw_aff_zero(space);
257 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
258 r = check_reparse_multi_pw_aff(ctx, mpa);
259 if (r < 0)
260 return -1;
262 map = isl_map_read_from_str(ctx, "{ [a, a] -> [i1 = a + 1] }");
263 pma = isl_pw_multi_aff_from_map(map);
264 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
265 if (check_reparse_multi_pw_aff(ctx, mpa) < 0)
266 return -1;
268 if (check_reparse_multi_pw_aff_tests(ctx) < 0)
269 return -1;
270 if (check_parse_multi_pw_aff_fail_tests(ctx) < 0)
271 return -1;
273 return 0;
276 /* String descriptions of multi union piecewise affine expressions
277 * that are used for testing printing and parsing.
279 static const char *reparse_multi_union_pw_aff_tests[] = {
280 "[]",
281 "A[]",
282 "A[B[] -> C[]]",
283 "(A[] : { S[x] : x > 0; T[y] : y >= 0 })",
284 "(A[] : { })",
285 "[N] -> (A[] : { })",
286 "[N] -> (A[] : { : N >= 0 })",
287 "[N] -> (A[] : { S[x] : x > N; T[y] : y >= 0 })",
288 "(A[] : [N] -> { S[x] : x > N; T[y] : y >= 0 })",
289 "A[{ S[x] -> [x + 1]; T[x] -> [x] }]",
290 "(A[{ S[x] -> [x + 1]; T[x] -> [x] }] : "
291 "{ S[x] : x > 0; T[y] : y >= 0 })",
294 #undef BASE
295 #define BASE multi_union_pw_aff
297 #include "check_reparse_templ.c"
298 #include "check_reparse_test_templ.c"
300 /* Test parsing of multi union piecewise affine expressions by printing
301 * the expressions and checking that parsing the output results
302 * in the same expression.
303 * Do this for a couple of manually constructed expressions and
304 * a set of expressions parsed from strings.
306 static int test_parse_mupa(isl_ctx *ctx)
308 isl_space *space;
309 isl_multi_union_pw_aff *mupa;
310 isl_set *dom;
311 isl_union_set *uset;
312 isl_stat r;
314 space = isl_space_set_alloc(ctx, 0, 0);
315 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
316 mupa = isl_multi_union_pw_aff_zero(space);
317 r = check_reparse_multi_union_pw_aff(ctx, mupa);
318 if (r < 0)
319 return -1;
321 space = isl_space_set_alloc(ctx, 1, 0);
322 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
323 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
324 dom = isl_set_universe(space);
325 dom = isl_set_lower_bound_si(dom, isl_dim_param, 0, 5);
326 uset = isl_union_set_from_set(dom);
327 space = isl_space_set_alloc(ctx, 1, 0);
328 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
329 space = isl_space_set_tuple_name(space, isl_dim_set, "B");
330 mupa = isl_multi_union_pw_aff_zero(space);
331 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, uset);
332 r = check_reparse_multi_union_pw_aff(ctx, mupa);
333 if (r < 0)
334 return -1;
336 if (check_reparse_multi_union_pw_aff_tests(ctx) < 0)
337 return -1;
339 return 0;
342 /* Test parsing of multi expressions.
344 static int test_parse_multi(isl_ctx *ctx)
346 if (test_parse_mpa(ctx) < 0)
347 return -1;
348 if (test_parse_mupa(ctx) < 0)
349 return -1;
351 return 0;
354 /* Pairs of binary relation representations that should represent
355 * the same binary relations.
357 struct {
358 const char *map1;
359 const char *map2;
360 } parse_map_equal_tests[] = {
361 { "{ [x,y] : [([x/2]+y)/3] >= 1 }",
362 "{ [x, y] : 2y >= 6 - x }" },
363 { "{ [x,y] : x <= min(y, 2*y+3) }",
364 "{ [x,y] : x <= y, 2*y + 3 }" },
365 { "{ [x,y] : x >= min(y, 2*y+3) }",
366 "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }" },
367 { "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }",
368 "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }" },
369 { "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }",
370 "{ [i,j] -> [min(i,j)] }" },
371 { "{ [i,j] : i != j }",
372 "{ [i,j] : i < j or i > j }" },
373 { "{ [i,j] : (i+1)*2 >= j }",
374 "{ [i, j] : j <= 2 + 2i }" },
375 { "{ [i] -> [i > 0 ? 4 : 5] }",
376 "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }" },
377 { "[N=2,M] -> { [i=[(M+N)/4]] }",
378 "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }" },
379 { "{ [x] : x >= 0 }",
380 "{ [x] : x-0 >= 0 }" },
381 { "{ [i] : ((i > 10)) }",
382 "{ [i] : i >= 11 }" },
383 { "{ [i] -> [0] }",
384 "{ [i] -> [0 * i] }" },
385 { "{ [a] -> [b] : (not false) }",
386 "{ [a] -> [b] : true }" },
387 { "{ [i] : i/2 <= 5 }",
388 "{ [i] : i <= 10 }" },
389 { "{Sym=[n] [i] : i <= n }",
390 "[n] -> { [i] : i <= n }" },
391 { "{ [*] }",
392 "{ [a] }" },
393 { "{ [i] : 2*floor(i/2) = i }",
394 "{ [i] : exists a : i = 2 a }" },
395 { "{ [a] -> [b] : a = 5 implies b = 5 }",
396 "{ [a] -> [b] : a != 5 or b = 5 }" },
397 { "{ [a] -> [a - 1 : a > 0] }",
398 "{ [a] -> [a - 1] : a > 0 }" },
399 { "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
400 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }" },
401 { "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
402 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
403 { "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
404 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
405 { "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
406 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
407 { "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
408 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
409 { "{ [a,b] -> [i,j] : a,b << i,j }",
410 "{ [a,b] -> [i,j] : a < i or (a = i and b < j) }" },
411 { "{ [a,b] -> [i,j] : a,b <<= i,j }",
412 "{ [a,b] -> [i,j] : a < i or (a = i and b <= j) }" },
413 { "{ [a,b] -> [i,j] : a,b >> i,j }",
414 "{ [a,b] -> [i,j] : a > i or (a = i and b > j) }" },
415 { "{ [a,b] -> [i,j] : a,b >>= i,j }",
416 "{ [a,b] -> [i,j] : a > i or (a = i and b >= j) }" },
417 { "{ [n] -> [i] : exists (a, b, c: 8b <= i - 32a and "
418 "8b >= -7 + i - 32 a and b >= 0 and b <= 3 and "
419 "8c < n - 32a and i < n and c >= 0 and "
420 "c <= 3 and c >= -4a) }",
421 "{ [n] -> [i] : 0 <= i < n }" },
422 { "{ [x] -> [] : exists (a, b: 0 <= a <= 1 and 0 <= b <= 3 and "
423 "2b <= x - 8a and 2b >= -1 + x - 8a) }",
424 "{ [x] -> [] : 0 <= x <= 15 }" },
425 { "{ [x] -> [x] : }",
426 "{ [x] -> [x] }" },
427 { "{ [x=4:5] -> [x + 1] }",
428 "{ [x] -> [x + 1] : 4 <= x <= 5 }" },
429 { "{ [x=4:5] -> [x + 1 : x + 1] }",
430 "{ [x=4:5] -> [x + 1] }" },
431 { "{ [x] -> [x - 1 : x + 1] }",
432 "{ [x] -> [y] : x - 1 <= y <= x + 1 }" },
433 { "{ [x=4:] -> [x + 1] }",
434 "{ [x] -> [x + 1] : 4 <= x }" },
435 { "{ [x=:5] -> [x + 1] }",
436 "{ [x] -> [x + 1] : x <= 5 }" },
437 { "{ [x=:] -> [x + 1] }",
438 "{ [x] -> [x + 1] }" },
439 { "{ [:] -> [:] }",
440 "{ [x] -> [y] }" },
441 { "{ [x, x//4] }",
442 "{ [x, floor(x/4)] }" },
443 { "{ [10//4] }",
444 "{ [2] }" },
445 { "[a, b, c, d] -> { [max(a,b,c,d)] }",
446 "[a, b, c, d] -> { [a] : b < a and c < a and d < a; "
447 "[b] : b >= a and c < b and d < b; "
448 "[c] : c >= a and c >= b and d < c; "
449 "[d] : d >= a and d >= b and d >= c }" },
450 { "[a, b, c, d] -> { [min(a,b,c,d)] }",
451 "[a, b, c, d] -> { [a] : b >= a and c >= a and d >= a; "
452 "[b] : b < a and c >= b and d >= b; "
453 "[c] : c < b and c < a and d >= c; "
454 "[d] : d < c and d < b and d < a }" },
457 int test_parse(struct isl_ctx *ctx)
459 int i;
460 isl_map *map, *map2;
461 const char *str, *str2;
463 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
464 return -1;
465 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
466 return -1;
467 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
468 return -1;
469 if (test_parse_multi(ctx) < 0)
470 return -1;
471 if (test_parse_pma(ctx) < 0)
472 return -1;
473 if (test_parse_upma(ctx) < 0)
474 return -1;
476 str = "{ [i] -> [-i] }";
477 map = isl_map_read_from_str(ctx, str);
478 assert(map);
479 isl_map_free(map);
481 str = "{ A[i] -> L[([i/3])] }";
482 map = isl_map_read_from_str(ctx, str);
483 assert(map);
484 isl_map_free(map);
486 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
487 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
488 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
490 for (i = 0; i < ARRAY_SIZE(parse_map_equal_tests); ++i) {
491 str = parse_map_equal_tests[i].map1;
492 str2 = parse_map_equal_tests[i].map2;
493 if (test_parse_map_equal(ctx, str, str2) < 0)
494 return -1;
497 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
498 map = isl_map_read_from_str(ctx, str);
499 str = "{ [new, old] -> [o0, o1] : "
500 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
501 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
502 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
503 map2 = isl_map_read_from_str(ctx, str);
504 assert(isl_map_is_equal(map, map2));
505 isl_map_free(map);
506 isl_map_free(map2);
508 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
509 map = isl_map_read_from_str(ctx, str);
510 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
511 map2 = isl_map_read_from_str(ctx, str);
512 assert(isl_map_is_equal(map, map2));
513 isl_map_free(map);
514 isl_map_free(map2);
516 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
517 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
518 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
519 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
520 test_parse_pwaff(ctx, "{ [] -> [(100)] }");
522 return 0;
525 static int test_read(isl_ctx *ctx)
527 char *filename;
528 FILE *input;
529 isl_basic_set *bset1, *bset2;
530 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
531 int equal;
533 filename = get_filename(ctx, "set", "omega");
534 assert(filename);
535 input = fopen(filename, "r");
536 assert(input);
538 bset1 = isl_basic_set_read_from_file(ctx, input);
539 bset2 = isl_basic_set_read_from_str(ctx, str);
541 equal = isl_basic_set_is_equal(bset1, bset2);
543 isl_basic_set_free(bset1);
544 isl_basic_set_free(bset2);
545 free(filename);
547 fclose(input);
549 if (equal < 0)
550 return -1;
551 if (!equal)
552 isl_die(ctx, isl_error_unknown,
553 "read sets not equal", return -1);
555 return 0;
558 static int test_bounded(isl_ctx *ctx)
560 isl_set *set;
561 isl_bool bounded;
563 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
564 bounded = isl_set_is_bounded(set);
565 isl_set_free(set);
567 if (bounded < 0)
568 return -1;
569 if (!bounded)
570 isl_die(ctx, isl_error_unknown,
571 "set not considered bounded", return -1);
573 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
574 bounded = isl_set_is_bounded(set);
575 assert(!bounded);
576 isl_set_free(set);
578 if (bounded < 0)
579 return -1;
580 if (bounded)
581 isl_die(ctx, isl_error_unknown,
582 "set considered bounded", return -1);
584 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
585 bounded = isl_set_is_bounded(set);
586 isl_set_free(set);
588 if (bounded < 0)
589 return -1;
590 if (bounded)
591 isl_die(ctx, isl_error_unknown,
592 "set considered bounded", return -1);
594 return 0;
597 /* Construct the basic set { [i] : 5 <= i <= N } */
598 static int test_construction_1(isl_ctx *ctx)
600 isl_space *space;
601 isl_local_space *ls;
602 isl_basic_set *bset;
603 isl_constraint *c;
605 space = isl_space_set_alloc(ctx, 1, 1);
606 bset = isl_basic_set_universe(isl_space_copy(space));
607 ls = isl_local_space_from_space(space);
609 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
610 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
611 c = isl_constraint_set_coefficient_si(c, isl_dim_param, 0, 1);
612 bset = isl_basic_set_add_constraint(bset, c);
614 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
615 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
616 c = isl_constraint_set_constant_si(c, -5);
617 bset = isl_basic_set_add_constraint(bset, c);
619 isl_local_space_free(ls);
620 isl_basic_set_free(bset);
622 return 0;
625 /* Construct the basic set { [x] : -100 <= x <= 100 }
626 * using isl_basic_set_{lower,upper}_bound_val and
627 * check that it is equal the same basic set parsed from a string.
629 static int test_construction_2(isl_ctx *ctx)
631 isl_bool equal;
632 isl_val *v;
633 isl_space *space;
634 isl_basic_set *bset1, *bset2;
636 v = isl_val_int_from_si(ctx, 100);
637 space = isl_space_set_alloc(ctx, 0, 1);
638 bset1 = isl_basic_set_universe(space);
639 bset1 = isl_basic_set_upper_bound_val(bset1, isl_dim_set, 0,
640 isl_val_copy(v));
641 bset1 = isl_basic_set_lower_bound_val(bset1, isl_dim_set, 0,
642 isl_val_neg(v));
643 bset2 = isl_basic_set_read_from_str(ctx, "{ [x] : -100 <= x <= 100 }");
644 equal = isl_basic_set_is_equal(bset1, bset2);
645 isl_basic_set_free(bset1);
646 isl_basic_set_free(bset2);
648 if (equal < 0)
649 return -1;
650 if (!equal)
651 isl_die(ctx, isl_error_unknown,
652 "failed construction", return -1);
654 return 0;
657 /* Basic tests for constructing basic sets.
659 static int test_construction(isl_ctx *ctx)
661 if (test_construction_1(ctx) < 0)
662 return -1;
663 if (test_construction_2(ctx) < 0)
664 return -1;
665 return 0;
668 static int test_dim(isl_ctx *ctx)
670 const char *str;
671 isl_map *map1, *map2;
672 int equal;
674 map1 = isl_map_read_from_str(ctx,
675 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
676 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
677 map2 = isl_map_read_from_str(ctx,
678 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
679 equal = isl_map_is_equal(map1, map2);
680 isl_map_free(map2);
682 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
683 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
684 if (equal >= 0 && equal)
685 equal = isl_map_is_equal(map1, map2);
687 isl_map_free(map1);
688 isl_map_free(map2);
690 if (equal < 0)
691 return -1;
692 if (!equal)
693 isl_die(ctx, isl_error_unknown,
694 "unexpected result", return -1);
696 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
697 map1 = isl_map_read_from_str(ctx, str);
698 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
699 map2 = isl_map_read_from_str(ctx, str);
700 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
701 equal = isl_map_is_equal(map1, map2);
702 isl_map_free(map1);
703 isl_map_free(map2);
705 if (equal < 0)
706 return -1;
707 if (!equal)
708 isl_die(ctx, isl_error_unknown,
709 "unexpected result", return -1);
711 return 0;
714 #undef BASE
715 #define BASE multi_val
716 #include "isl_test_plain_equal_templ.c"
718 #undef BASE
719 #define BASE multi_aff
720 #include "isl_test_plain_equal_templ.c"
722 /* Check that "val" is equal to the value described by "str".
723 * If "str" is "NaN", then check for a NaN value explicitly.
725 static isl_stat val_check_equal(__isl_keep isl_val *val, const char *str)
727 isl_bool ok, is_nan;
728 isl_ctx *ctx;
729 isl_val *res;
731 if (!val)
732 return isl_stat_error;
734 ctx = isl_val_get_ctx(val);
735 res = isl_val_read_from_str(ctx, str);
736 is_nan = isl_val_is_nan(res);
737 if (is_nan < 0)
738 ok = isl_bool_error;
739 else if (is_nan)
740 ok = isl_val_is_nan(val);
741 else
742 ok = isl_val_eq(val, res);
743 isl_val_free(res);
744 if (ok < 0)
745 return isl_stat_error;
746 if (!ok)
747 isl_die(ctx, isl_error_unknown,
748 "unexpected result", return isl_stat_error);
749 return isl_stat_ok;
752 struct {
753 __isl_give isl_val *(*op)(__isl_take isl_val *v);
754 const char *arg;
755 const char *res;
756 } val_un_tests[] = {
757 { &isl_val_neg, "0", "0" },
758 { &isl_val_abs, "0", "0" },
759 { &isl_val_pow2, "0", "1" },
760 { &isl_val_floor, "0", "0" },
761 { &isl_val_ceil, "0", "0" },
762 { &isl_val_neg, "1", "-1" },
763 { &isl_val_neg, "-1", "1" },
764 { &isl_val_neg, "1/2", "-1/2" },
765 { &isl_val_neg, "-1/2", "1/2" },
766 { &isl_val_neg, "infty", "-infty" },
767 { &isl_val_neg, "-infty", "infty" },
768 { &isl_val_neg, "NaN", "NaN" },
769 { &isl_val_abs, "1", "1" },
770 { &isl_val_abs, "-1", "1" },
771 { &isl_val_abs, "1/2", "1/2" },
772 { &isl_val_abs, "-1/2", "1/2" },
773 { &isl_val_abs, "infty", "infty" },
774 { &isl_val_abs, "-infty", "infty" },
775 { &isl_val_abs, "NaN", "NaN" },
776 { &isl_val_floor, "1", "1" },
777 { &isl_val_floor, "-1", "-1" },
778 { &isl_val_floor, "1/2", "0" },
779 { &isl_val_floor, "-1/2", "-1" },
780 { &isl_val_floor, "infty", "infty" },
781 { &isl_val_floor, "-infty", "-infty" },
782 { &isl_val_floor, "NaN", "NaN" },
783 { &isl_val_ceil, "1", "1" },
784 { &isl_val_ceil, "-1", "-1" },
785 { &isl_val_ceil, "1/2", "1" },
786 { &isl_val_ceil, "-1/2", "0" },
787 { &isl_val_ceil, "infty", "infty" },
788 { &isl_val_ceil, "-infty", "-infty" },
789 { &isl_val_ceil, "NaN", "NaN" },
790 { &isl_val_pow2, "-3", "1/8" },
791 { &isl_val_pow2, "-1", "1/2" },
792 { &isl_val_pow2, "1", "2" },
793 { &isl_val_pow2, "2", "4" },
794 { &isl_val_pow2, "3", "8" },
795 { &isl_val_inv, "1", "1" },
796 { &isl_val_inv, "2", "1/2" },
797 { &isl_val_inv, "1/2", "2" },
798 { &isl_val_inv, "-2", "-1/2" },
799 { &isl_val_inv, "-1/2", "-2" },
800 { &isl_val_inv, "0", "NaN" },
801 { &isl_val_inv, "NaN", "NaN" },
802 { &isl_val_inv, "infty", "0" },
803 { &isl_val_inv, "-infty", "0" },
806 /* Perform some basic tests of unary operations on isl_val objects.
808 static int test_un_val(isl_ctx *ctx)
810 int i;
811 isl_val *v;
812 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
814 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
815 isl_stat r;
817 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
818 fn = val_un_tests[i].op;
819 v = fn(v);
820 r = val_check_equal(v, val_un_tests[i].res);
821 isl_val_free(v);
822 if (r < 0)
823 return -1;
826 return 0;
829 struct {
830 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
831 __isl_take isl_val *v2);
832 } val_bin_op[] = {
833 ['+'] = { &isl_val_add },
834 ['-'] = { &isl_val_sub },
835 ['*'] = { &isl_val_mul },
836 ['/'] = { &isl_val_div },
837 ['g'] = { &isl_val_gcd },
838 ['m'] = { &isl_val_min },
839 ['M'] = { &isl_val_max },
842 struct {
843 const char *arg1;
844 unsigned char op;
845 const char *arg2;
846 const char *res;
847 } val_bin_tests[] = {
848 { "0", '+', "0", "0" },
849 { "1", '+', "0", "1" },
850 { "1", '+', "1", "2" },
851 { "1", '-', "1", "0" },
852 { "1", '*', "1", "1" },
853 { "1", '/', "1", "1" },
854 { "2", '*', "3", "6" },
855 { "2", '*', "1/2", "1" },
856 { "2", '*', "1/3", "2/3" },
857 { "2/3", '*', "3/5", "2/5" },
858 { "2/3", '*', "7/5", "14/15" },
859 { "2", '/', "1/2", "4" },
860 { "-2", '/', "-1/2", "4" },
861 { "-2", '/', "1/2", "-4" },
862 { "2", '/', "-1/2", "-4" },
863 { "2", '/', "2", "1" },
864 { "2", '/', "3", "2/3" },
865 { "2/3", '/', "5/3", "2/5" },
866 { "2/3", '/', "5/7", "14/15" },
867 { "0", '/', "0", "NaN" },
868 { "42", '/', "0", "NaN" },
869 { "-42", '/', "0", "NaN" },
870 { "infty", '/', "0", "NaN" },
871 { "-infty", '/', "0", "NaN" },
872 { "NaN", '/', "0", "NaN" },
873 { "0", '/', "NaN", "NaN" },
874 { "42", '/', "NaN", "NaN" },
875 { "-42", '/', "NaN", "NaN" },
876 { "infty", '/', "NaN", "NaN" },
877 { "-infty", '/', "NaN", "NaN" },
878 { "NaN", '/', "NaN", "NaN" },
879 { "0", '/', "infty", "0" },
880 { "42", '/', "infty", "0" },
881 { "-42", '/', "infty", "0" },
882 { "infty", '/', "infty", "NaN" },
883 { "-infty", '/', "infty", "NaN" },
884 { "NaN", '/', "infty", "NaN" },
885 { "0", '/', "-infty", "0" },
886 { "42", '/', "-infty", "0" },
887 { "-42", '/', "-infty", "0" },
888 { "infty", '/', "-infty", "NaN" },
889 { "-infty", '/', "-infty", "NaN" },
890 { "NaN", '/', "-infty", "NaN" },
891 { "1", '-', "1/3", "2/3" },
892 { "1/3", '+', "1/2", "5/6" },
893 { "1/2", '+', "1/2", "1" },
894 { "3/4", '-', "1/4", "1/2" },
895 { "1/2", '-', "1/3", "1/6" },
896 { "infty", '+', "42", "infty" },
897 { "infty", '+', "infty", "infty" },
898 { "42", '+', "infty", "infty" },
899 { "infty", '-', "infty", "NaN" },
900 { "infty", '*', "infty", "infty" },
901 { "infty", '*', "-infty", "-infty" },
902 { "-infty", '*', "infty", "-infty" },
903 { "-infty", '*', "-infty", "infty" },
904 { "0", '*', "infty", "NaN" },
905 { "1", '*', "infty", "infty" },
906 { "infty", '*', "0", "NaN" },
907 { "infty", '*', "42", "infty" },
908 { "42", '-', "infty", "-infty" },
909 { "infty", '+', "-infty", "NaN" },
910 { "4", 'g', "6", "2" },
911 { "5", 'g', "6", "1" },
912 { "42", 'm', "3", "3" },
913 { "42", 'M', "3", "42" },
914 { "3", 'm', "42", "3" },
915 { "3", 'M', "42", "42" },
916 { "42", 'm', "infty", "42" },
917 { "42", 'M', "infty", "infty" },
918 { "42", 'm', "-infty", "-infty" },
919 { "42", 'M', "-infty", "42" },
920 { "42", 'm', "NaN", "NaN" },
921 { "42", 'M', "NaN", "NaN" },
922 { "infty", 'm', "-infty", "-infty" },
923 { "infty", 'M', "-infty", "infty" },
926 /* Perform some basic tests of binary operations on isl_val objects.
928 static int test_bin_val(isl_ctx *ctx)
930 int i;
931 isl_val *v1, *v2, *res;
932 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
933 __isl_take isl_val *v2);
934 int ok;
936 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
937 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
938 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
939 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
940 fn = val_bin_op[val_bin_tests[i].op].fn;
941 v1 = fn(v1, v2);
942 if (isl_val_is_nan(res))
943 ok = isl_val_is_nan(v1);
944 else
945 ok = isl_val_eq(v1, res);
946 isl_val_free(v1);
947 isl_val_free(res);
948 if (ok < 0)
949 return -1;
950 if (!ok)
951 isl_die(ctx, isl_error_unknown,
952 "unexpected result", return -1);
955 return 0;
958 /* Perform some basic tests on isl_val objects.
960 static int test_val(isl_ctx *ctx)
962 if (test_un_val(ctx) < 0)
963 return -1;
964 if (test_bin_val(ctx) < 0)
965 return -1;
966 return 0;
969 /* Sets described using existentially quantified variables that
970 * can also be described without.
972 static const char *elimination_tests[] = {
973 "{ [i,j] : 2 * [i/2] + 3 * [j/4] <= 10 and 2 i = j }",
974 "{ [m, w] : exists a : w - 2m - 5 <= 3a <= m - 2w }",
975 "{ [m, w] : exists a : w >= 0 and a < m and -1 + w <= a <= 2m - w }",
978 /* Check that redundant existentially quantified variables are
979 * getting removed.
981 static int test_elimination(isl_ctx *ctx)
983 int i;
984 isl_size n;
985 isl_basic_set *bset;
987 for (i = 0; i < ARRAY_SIZE(elimination_tests); ++i) {
988 bset = isl_basic_set_read_from_str(ctx, elimination_tests[i]);
989 n = isl_basic_set_dim(bset, isl_dim_div);
990 isl_basic_set_free(bset);
991 if (n < 0)
992 return -1;
993 if (n != 0)
994 isl_die(ctx, isl_error_unknown,
995 "expecting no existentials", return -1);
998 return 0;
1001 static int test_div(isl_ctx *ctx)
1003 const char *str;
1004 int empty;
1005 isl_space *space;
1006 isl_set *set;
1007 isl_local_space *ls;
1008 struct isl_basic_set *bset;
1009 struct isl_constraint *c;
1011 /* test 1 */
1012 space = isl_space_set_alloc(ctx, 0, 3);
1013 bset = isl_basic_set_universe(isl_space_copy(space));
1014 ls = isl_local_space_from_space(space);
1016 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1017 c = isl_constraint_set_constant_si(c, -1);
1018 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1019 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1020 bset = isl_basic_set_add_constraint(bset, c);
1022 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1023 c = isl_constraint_set_constant_si(c, 1);
1024 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1025 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
1026 bset = isl_basic_set_add_constraint(bset, c);
1028 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1030 assert(bset && bset->n_div == 1);
1031 isl_local_space_free(ls);
1032 isl_basic_set_free(bset);
1034 /* test 2 */
1035 space = isl_space_set_alloc(ctx, 0, 3);
1036 bset = isl_basic_set_universe(isl_space_copy(space));
1037 ls = isl_local_space_from_space(space);
1039 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1040 c = isl_constraint_set_constant_si(c, 1);
1041 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1042 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1043 bset = isl_basic_set_add_constraint(bset, c);
1045 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1046 c = isl_constraint_set_constant_si(c, -1);
1047 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1048 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
1049 bset = isl_basic_set_add_constraint(bset, c);
1051 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1053 assert(bset && bset->n_div == 1);
1054 isl_local_space_free(ls);
1055 isl_basic_set_free(bset);
1057 /* test 3 */
1058 space = isl_space_set_alloc(ctx, 0, 3);
1059 bset = isl_basic_set_universe(isl_space_copy(space));
1060 ls = isl_local_space_from_space(space);
1062 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1063 c = isl_constraint_set_constant_si(c, 1);
1064 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1065 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1066 bset = isl_basic_set_add_constraint(bset, c);
1068 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1069 c = isl_constraint_set_constant_si(c, -3);
1070 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1071 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 4);
1072 bset = isl_basic_set_add_constraint(bset, c);
1074 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1076 assert(bset && bset->n_div == 1);
1077 isl_local_space_free(ls);
1078 isl_basic_set_free(bset);
1080 /* test 4 */
1081 space = isl_space_set_alloc(ctx, 0, 3);
1082 bset = isl_basic_set_universe(isl_space_copy(space));
1083 ls = isl_local_space_from_space(space);
1085 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1086 c = isl_constraint_set_constant_si(c, 2);
1087 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1088 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1089 bset = isl_basic_set_add_constraint(bset, c);
1091 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1092 c = isl_constraint_set_constant_si(c, -1);
1093 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1094 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
1095 bset = isl_basic_set_add_constraint(bset, c);
1097 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1099 assert(isl_basic_set_is_empty(bset));
1100 isl_local_space_free(ls);
1101 isl_basic_set_free(bset);
1103 /* test 5 */
1104 space = isl_space_set_alloc(ctx, 0, 3);
1105 bset = isl_basic_set_universe(isl_space_copy(space));
1106 ls = isl_local_space_from_space(space);
1108 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1109 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1110 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
1111 bset = isl_basic_set_add_constraint(bset, c);
1113 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1114 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1115 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1116 bset = isl_basic_set_add_constraint(bset, c);
1118 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1120 assert(bset && bset->n_div == 0);
1121 isl_basic_set_free(bset);
1122 isl_local_space_free(ls);
1124 /* test 6 */
1125 space = isl_space_set_alloc(ctx, 0, 3);
1126 bset = isl_basic_set_universe(isl_space_copy(space));
1127 ls = isl_local_space_from_space(space);
1129 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1130 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1131 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
1132 bset = isl_basic_set_add_constraint(bset, c);
1134 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1135 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1136 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1137 bset = isl_basic_set_add_constraint(bset, c);
1139 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1141 assert(bset && bset->n_div == 1);
1142 isl_basic_set_free(bset);
1143 isl_local_space_free(ls);
1145 /* test 7 */
1146 /* This test is a bit tricky. We set up an equality
1147 * a + 3b + 3c = 6 e0
1148 * Normalization of divs creates _two_ divs
1149 * a = 3 e0
1150 * c - b - e0 = 2 e1
1151 * Afterwards e0 is removed again because it has coefficient -1
1152 * and we end up with the original equality and div again.
1153 * Perhaps we can avoid the introduction of this temporary div.
1155 space = isl_space_set_alloc(ctx, 0, 4);
1156 bset = isl_basic_set_universe(isl_space_copy(space));
1157 ls = isl_local_space_from_space(space);
1159 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1160 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1161 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1162 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -3);
1163 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 6);
1164 bset = isl_basic_set_add_constraint(bset, c);
1166 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
1168 /* Test disabled for now */
1170 assert(bset && bset->n_div == 1);
1172 isl_local_space_free(ls);
1173 isl_basic_set_free(bset);
1175 /* test 8 */
1176 space = isl_space_set_alloc(ctx, 0, 5);
1177 bset = isl_basic_set_universe(isl_space_copy(space));
1178 ls = isl_local_space_from_space(space);
1180 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1181 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1182 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1183 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, -3);
1184 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 4, 6);
1185 bset = isl_basic_set_add_constraint(bset, c);
1187 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1188 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1189 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 1);
1190 c = isl_constraint_set_constant_si(c, 1);
1191 bset = isl_basic_set_add_constraint(bset, c);
1193 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
1195 /* Test disabled for now */
1197 assert(bset && bset->n_div == 1);
1199 isl_local_space_free(ls);
1200 isl_basic_set_free(bset);
1202 /* test 9 */
1203 space = isl_space_set_alloc(ctx, 0, 4);
1204 bset = isl_basic_set_universe(isl_space_copy(space));
1205 ls = isl_local_space_from_space(space);
1207 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1208 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1209 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -1);
1210 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1211 bset = isl_basic_set_add_constraint(bset, c);
1213 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1214 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1215 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 3);
1216 c = isl_constraint_set_constant_si(c, 2);
1217 bset = isl_basic_set_add_constraint(bset, c);
1219 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
1221 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1223 assert(!isl_basic_set_is_empty(bset));
1225 isl_local_space_free(ls);
1226 isl_basic_set_free(bset);
1228 /* test 10 */
1229 space = isl_space_set_alloc(ctx, 0, 3);
1230 bset = isl_basic_set_universe(isl_space_copy(space));
1231 ls = isl_local_space_from_space(space);
1233 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1234 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1235 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1236 bset = isl_basic_set_add_constraint(bset, c);
1238 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1240 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1242 isl_local_space_free(ls);
1243 isl_basic_set_free(bset);
1245 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1246 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
1247 set = isl_set_read_from_str(ctx, str);
1248 set = isl_set_compute_divs(set);
1249 isl_set_free(set);
1250 if (!set)
1251 return -1;
1253 if (test_elimination(ctx) < 0)
1254 return -1;
1256 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
1257 set = isl_set_read_from_str(ctx, str);
1258 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
1259 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
1260 empty = isl_set_is_empty(set);
1261 isl_set_free(set);
1262 if (empty < 0)
1263 return -1;
1264 if (!empty)
1265 isl_die(ctx, isl_error_unknown,
1266 "result not as accurate as expected", return -1);
1268 return 0;
1271 void test_application_case(struct isl_ctx *ctx, const char *name)
1273 char *filename;
1274 FILE *input;
1275 struct isl_basic_set *bset1, *bset2;
1276 struct isl_basic_map *bmap;
1278 filename = get_filename(ctx, name, "omega");
1279 assert(filename);
1280 input = fopen(filename, "r");
1281 assert(input);
1283 bset1 = isl_basic_set_read_from_file(ctx, input);
1284 bmap = isl_basic_map_read_from_file(ctx, input);
1286 bset1 = isl_basic_set_apply(bset1, bmap);
1288 bset2 = isl_basic_set_read_from_file(ctx, input);
1290 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1292 isl_basic_set_free(bset1);
1293 isl_basic_set_free(bset2);
1294 free(filename);
1296 fclose(input);
1299 static int test_application(isl_ctx *ctx)
1301 test_application_case(ctx, "application");
1302 test_application_case(ctx, "application2");
1304 return 0;
1307 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1309 char *filename;
1310 FILE *input;
1311 struct isl_basic_set *bset1, *bset2;
1313 filename = get_filename(ctx, name, "polylib");
1314 assert(filename);
1315 input = fopen(filename, "r");
1316 assert(input);
1318 bset1 = isl_basic_set_read_from_file(ctx, input);
1319 bset2 = isl_basic_set_read_from_file(ctx, input);
1321 bset1 = isl_basic_set_affine_hull(bset1);
1323 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1325 isl_basic_set_free(bset1);
1326 isl_basic_set_free(bset2);
1327 free(filename);
1329 fclose(input);
1332 /* Pairs of sets and the corresponding expected results of
1333 * isl_basic_set_recession_cone.
1335 struct {
1336 const char *set;
1337 const char *cone;
1338 } recession_cone_tests[] = {
1339 { "{ [i] : 0 <= i <= 10 }", "{ [0] }" },
1340 { "{ [i] : 0 <= i }", "{ [i] : 0 <= i }" },
1341 { "{ [i] : i <= 10 }", "{ [i] : i <= 0 }" },
1342 { "{ [i] : false }", "{ [i] : false }" },
1345 /* Perform some basic isl_basic_set_recession_cone tests.
1347 static int test_recession_cone(struct isl_ctx *ctx)
1349 int i;
1351 for (i = 0; i < ARRAY_SIZE(recession_cone_tests); ++i) {
1352 const char *str;
1353 isl_basic_set *bset;
1354 isl_basic_set *cone, *expected;
1355 isl_bool equal;
1357 str = recession_cone_tests[i].set;
1358 bset = isl_basic_set_read_from_str(ctx, str);
1359 str = recession_cone_tests[i].cone;
1360 expected = isl_basic_set_read_from_str(ctx, str);
1361 cone = isl_basic_set_recession_cone(bset);
1362 equal = isl_basic_set_is_equal(cone, expected);
1363 isl_basic_set_free(cone);
1364 isl_basic_set_free(expected);
1365 if (equal < 0)
1366 return -1;
1367 if (!equal)
1368 isl_die(ctx, isl_error_unknown, "unexpected cone",
1369 return -1);
1372 return 0;
1375 int test_affine_hull(struct isl_ctx *ctx)
1377 const char *str;
1378 isl_set *set;
1379 isl_basic_set *bset, *bset2;
1380 isl_size n;
1381 isl_bool subset;
1383 test_affine_hull_case(ctx, "affine2");
1384 test_affine_hull_case(ctx, "affine");
1385 test_affine_hull_case(ctx, "affine3");
1387 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1388 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1389 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1390 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1391 set = isl_set_read_from_str(ctx, str);
1392 bset = isl_set_affine_hull(set);
1393 n = isl_basic_set_dim(bset, isl_dim_div);
1394 isl_basic_set_free(bset);
1395 if (n < 0)
1396 return -1;
1397 if (n != 0)
1398 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1399 return -1);
1401 /* Check that isl_map_affine_hull is not confused by
1402 * the reordering of divs in isl_map_align_divs.
1404 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1405 "32e0 = b and 32e1 = c); "
1406 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1407 set = isl_set_read_from_str(ctx, str);
1408 bset = isl_set_affine_hull(set);
1409 isl_basic_set_free(bset);
1410 if (!bset)
1411 return -1;
1413 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1414 "32e2 = 31 + 31e0 }";
1415 set = isl_set_read_from_str(ctx, str);
1416 bset = isl_set_affine_hull(set);
1417 str = "{ [a] : exists e : a = 32 e }";
1418 bset2 = isl_basic_set_read_from_str(ctx, str);
1419 subset = isl_basic_set_is_subset(bset, bset2);
1420 isl_basic_set_free(bset);
1421 isl_basic_set_free(bset2);
1422 if (subset < 0)
1423 return -1;
1424 if (!subset)
1425 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1426 return -1);
1428 return 0;
1431 /* Test a special case of isl_set_plain_unshifted_simple_hull
1432 * where older versions of isl would include a redundant constraint
1433 * in the result.
1434 * Check that the result does not have any constraints.
1436 static isl_stat test_plain_unshifted_simple_hull_special(isl_ctx *ctx)
1438 const char *str;
1439 isl_bool is_universe;
1440 isl_set *set;
1441 isl_basic_set *bset;
1443 str = "{[x, y] : x = 0 or 2*((x+y)//2) <= y + 2 }";
1444 set = isl_set_read_from_str(ctx, str);
1445 bset = isl_set_plain_unshifted_simple_hull(set);
1446 is_universe = isl_basic_set_plain_is_universe(bset);
1447 isl_basic_set_free(bset);
1449 if (is_universe < 0)
1450 return isl_stat_error;
1451 if (!is_universe)
1452 isl_die(ctx, isl_error_unknown,
1453 "hull should not have any constraints",
1454 return isl_stat_error);
1456 return isl_stat_ok;
1459 /* Inputs for simple hull tests, consisting of
1460 * the specific simple hull function, the input set and the expected result.
1462 struct {
1463 __isl_give isl_basic_set *(*fn)(__isl_take isl_set *set);
1464 const char *set;
1465 const char *hull;
1466 } simple_hull_tests[] = {
1467 { &isl_set_plain_unshifted_simple_hull,
1468 "{ [i,j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1469 "{ [i,j] : i >= 1 }" },
1470 { &isl_set_plain_unshifted_simple_hull,
1471 "{ [n,i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1472 "(j mod 4 = 2 and k mod 6 = n) }",
1473 "{ [n,i,j,k] : j mod 4 = 2 }" },
1474 { &isl_set_unshifted_simple_hull,
1475 "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1476 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1477 { &isl_set_simple_hull,
1478 "{ [a, b] : b <= 0 and "
1479 "2*floor((-2*floor((b)/2))/5) >= a - floor((b)/2); "
1480 "[a, b] : a mod 2 = 0 }",
1481 "{ [a, b] }" },
1484 /* Basic tests for various simple hull functions.
1486 static int test_various_simple_hull(isl_ctx *ctx)
1488 int i;
1489 isl_set *set;
1490 isl_basic_set *hull, *expected;
1491 isl_bool equal;
1493 for (i = 0; i < ARRAY_SIZE(simple_hull_tests); ++i) {
1494 const char *str;
1495 str = simple_hull_tests[i].set;
1496 set = isl_set_read_from_str(ctx, str);
1497 str = simple_hull_tests[i].hull;
1498 expected = isl_basic_set_read_from_str(ctx, str);
1499 hull = simple_hull_tests[i].fn(set);
1500 equal = isl_basic_set_is_equal(hull, expected);
1501 isl_basic_set_free(hull);
1502 isl_basic_set_free(expected);
1503 if (equal < 0)
1504 return -1;
1505 if (!equal)
1506 isl_die(ctx, isl_error_unknown, "unexpected hull",
1507 return -1);
1510 return 0;
1513 static int test_simple_hull(struct isl_ctx *ctx)
1515 const char *str;
1516 isl_set *set;
1517 isl_basic_set *bset;
1518 isl_bool is_empty;
1520 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1521 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1522 set = isl_set_read_from_str(ctx, str);
1523 bset = isl_set_simple_hull(set);
1524 is_empty = isl_basic_set_is_empty(bset);
1525 isl_basic_set_free(bset);
1527 if (is_empty == isl_bool_error)
1528 return -1;
1530 if (is_empty == isl_bool_false)
1531 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1532 return -1);
1534 if (test_plain_unshifted_simple_hull_special(ctx) < 0)
1535 return -1;
1536 if (test_various_simple_hull(ctx) < 0)
1537 return -1;
1539 return 0;
1542 /* Inputs for isl_set_get_simple_fixed_box_hull tests.
1543 * "set" is the input set.
1544 * "offset" is the expected box offset.
1545 * "size" is the expected box size.
1547 static struct {
1548 const char *set;
1549 const char *offset;
1550 const char *size;
1551 } box_hull_tests[] = {
1552 { "{ S[x, y] : 0 <= x, y < 10 }", "{ S[0, 0] }", "{ S[10, 10] }" },
1553 { "[N] -> { S[x, y] : N <= x, y < N + 10 }",
1554 "[N] -> { S[N, N] }", "{ S[10, 10] }" },
1555 { "{ S[x, y] : 0 <= x + y, x - y < 10 }",
1556 "{ S[0, -4] }", "{ S[10, 9] }" },
1557 { "{ [i=0:10] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1558 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }",
1559 "{ [3] }", "{ [8] }" },
1560 { "[N] -> { [w = 0:17] : exists (e0: w < 2N and "
1561 "-1 + w <= e0 <= w and 2e0 >= N + w and w <= 2e0 <= 15 + w) }",
1562 "[N] -> { [N] }", "{ [9] }" },
1565 /* Perform basic isl_set_get_simple_fixed_box_hull tests.
1567 static int test_box_hull(struct isl_ctx *ctx)
1569 int i;
1571 for (i = 0; i < ARRAY_SIZE(box_hull_tests); ++i) {
1572 const char *str;
1573 isl_stat r;
1574 isl_set *set;
1575 isl_multi_aff *offset;
1576 isl_multi_val *size;
1577 isl_fixed_box *box;
1579 set = isl_set_read_from_str(ctx, box_hull_tests[i].set);
1580 box = isl_set_get_simple_fixed_box_hull(set);
1581 offset = isl_fixed_box_get_offset(box);
1582 size = isl_fixed_box_get_size(box);
1583 str = box_hull_tests[i].offset;
1584 r = multi_aff_check_plain_equal(offset, str);
1585 str = box_hull_tests[i].size;
1586 if (r >= 0)
1587 r = multi_val_check_plain_equal(size, str);
1588 isl_multi_aff_free(offset);
1589 isl_multi_val_free(size);
1590 isl_fixed_box_free(box);
1591 isl_set_free(set);
1592 if (r < 0)
1593 return -1;
1596 return 0;
1599 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1601 char *filename;
1602 FILE *input;
1603 struct isl_basic_set *bset1, *bset2;
1604 struct isl_set *set;
1606 filename = get_filename(ctx, name, "polylib");
1607 assert(filename);
1608 input = fopen(filename, "r");
1609 assert(input);
1611 bset1 = isl_basic_set_read_from_file(ctx, input);
1612 bset2 = isl_basic_set_read_from_file(ctx, input);
1614 set = isl_basic_set_union(bset1, bset2);
1615 bset1 = isl_set_convex_hull(set);
1617 bset2 = isl_basic_set_read_from_file(ctx, input);
1619 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1621 isl_basic_set_free(bset1);
1622 isl_basic_set_free(bset2);
1623 free(filename);
1625 fclose(input);
1628 struct {
1629 const char *set;
1630 const char *hull;
1631 } convex_hull_tests[] = {
1632 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1633 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1634 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1635 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1636 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1637 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1638 "i2 <= 5 and i2 >= 4; "
1639 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1640 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1641 "i2 <= 5 + i0 and i2 >= i0 }" },
1642 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1643 "{ [x, y] : 1 = 0 }" },
1644 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1645 "[x, y, 0] : x >= 0 and y < 0 }",
1646 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1647 { "{ [a, b, c] : a <= 1 and -a < b <= 1 and 0 <= c <= 2 - a - b and "
1648 "c <= a; "
1649 "[0, 2, 0]; [3, 1, 0] }",
1650 "{ [a, b, c] : b > -a and 2b >= -1 + a and 0 <= c <= a and "
1651 "5c <= 6 - a - 3b }" },
1654 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1656 int i;
1657 int orig_convex = ctx->opt->convex;
1658 ctx->opt->convex = convex;
1660 test_convex_hull_case(ctx, "convex0");
1661 test_convex_hull_case(ctx, "convex1");
1662 test_convex_hull_case(ctx, "convex2");
1663 test_convex_hull_case(ctx, "convex3");
1664 test_convex_hull_case(ctx, "convex4");
1665 test_convex_hull_case(ctx, "convex5");
1666 test_convex_hull_case(ctx, "convex6");
1667 test_convex_hull_case(ctx, "convex7");
1668 test_convex_hull_case(ctx, "convex8");
1669 test_convex_hull_case(ctx, "convex9");
1670 test_convex_hull_case(ctx, "convex10");
1671 test_convex_hull_case(ctx, "convex11");
1672 test_convex_hull_case(ctx, "convex12");
1673 test_convex_hull_case(ctx, "convex13");
1674 test_convex_hull_case(ctx, "convex14");
1675 test_convex_hull_case(ctx, "convex15");
1677 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1678 isl_set *set1, *set2;
1679 int equal;
1681 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1682 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1683 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1684 equal = isl_set_is_equal(set1, set2);
1685 isl_set_free(set1);
1686 isl_set_free(set2);
1688 if (equal < 0)
1689 return -1;
1690 if (!equal)
1691 isl_die(ctx, isl_error_unknown,
1692 "unexpected convex hull", return -1);
1695 ctx->opt->convex = orig_convex;
1697 return 0;
1700 static int test_convex_hull(isl_ctx *ctx)
1702 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1703 return -1;
1704 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1705 return -1;
1706 return 0;
1709 /* Check that computing the gist of "map" with respect to "context"
1710 * does not make any copy of "map" get marked empty.
1711 * Earlier versions of isl would end up doing that.
1713 static isl_stat test_gist_empty_pair(isl_ctx *ctx, const char *map,
1714 const char *context)
1716 isl_map *m1, *m2, *m3;
1717 isl_bool empty_before, empty_after;
1719 m1 = isl_map_read_from_str(ctx, map);
1720 m2 = isl_map_read_from_str(ctx, context);
1721 m3 = isl_map_copy(m1);
1722 empty_before = isl_map_is_empty(m3);
1723 m1 = isl_map_gist(m1, m2);
1724 empty_after = isl_map_is_empty(m3);
1725 isl_map_free(m1);
1726 isl_map_free(m3);
1728 if (empty_before < 0 || empty_after < 0)
1729 return isl_stat_error;
1730 if (empty_before)
1731 isl_die(ctx, isl_error_unknown, "map should not be empty",
1732 return isl_stat_error);
1733 if (empty_after)
1734 isl_die(ctx, isl_error_unknown, "map should still not be empty",
1735 return isl_stat_error);
1737 return isl_stat_ok;
1740 /* Check that computing a gist does not make any copy of the input
1741 * get marked empty.
1742 * Earlier versions of isl would end up doing that on some pairs of inputs.
1744 static isl_stat test_gist_empty(isl_ctx *ctx)
1746 const char *map, *context;
1748 map = "{ [] -> [a, b, c] : 2b = 1 + a }";
1749 context = "{ [] -> [a, b, c] : 2c = 2 + a }";
1750 if (test_gist_empty_pair(ctx, map, context) < 0)
1751 return isl_stat_error;
1752 map = "{ [] -> [0, 0] }";
1753 context = "{ [] -> [a, b] : a > b }";
1754 if (test_gist_empty_pair(ctx, map, context) < 0)
1755 return isl_stat_error;
1757 return isl_stat_ok;
1760 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1762 struct {
1763 const char *map;
1764 const char *context;
1765 const char *gist;
1766 } plain_gist_tests[] = {
1767 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1768 "{ [i] -> [j] : i >= 1 }",
1769 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1770 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1771 "(j mod 4 = 2 and k mod 6 = n) }",
1772 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1773 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1774 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1775 "{ [i] -> [j] : i > j }",
1776 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1779 /* Basic tests for isl_map_plain_gist_basic_map.
1781 static int test_plain_gist(isl_ctx *ctx)
1783 int i;
1785 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1786 const char *str;
1787 int equal;
1788 isl_map *map, *gist;
1789 isl_basic_map *context;
1791 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1792 str = plain_gist_tests[i].context;
1793 context = isl_basic_map_read_from_str(ctx, str);
1794 map = isl_map_plain_gist_basic_map(map, context);
1795 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1796 equal = isl_map_is_equal(map, gist);
1797 isl_map_free(map);
1798 isl_map_free(gist);
1799 if (equal < 0)
1800 return -1;
1801 if (!equal)
1802 isl_die(ctx, isl_error_unknown,
1803 "incorrect gist result", return -1);
1806 return 0;
1809 /* Inputs for isl_basic_set_gist tests that are expected to fail.
1811 struct {
1812 const char *set;
1813 const char *context;
1814 } gist_fail_tests[] = {
1815 { "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1816 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }",
1817 "{ [i] : i >= 0 }" },
1820 /* Check that isl_basic_set_gist fails (gracefully) when expected.
1821 * In particular, the user should be able to recover from the failure.
1823 static isl_stat test_gist_fail(struct isl_ctx *ctx)
1825 int i, n;
1826 int on_error;
1828 on_error = isl_options_get_on_error(ctx);
1829 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
1830 n = ARRAY_SIZE(gist_fail_tests);
1831 for (i = 0; i < n; ++i) {
1832 const char *str;
1833 isl_basic_set *bset, *context;
1835 bset = isl_basic_set_read_from_str(ctx, gist_fail_tests[i].set);
1836 str = gist_fail_tests[i].context;
1837 context = isl_basic_set_read_from_str(ctx, str);
1838 bset = isl_basic_set_gist(bset, context);
1839 isl_basic_set_free(bset);
1840 if (bset)
1841 break;
1843 isl_options_set_on_error(ctx, on_error);
1844 if (i < n)
1845 isl_die(ctx, isl_error_unknown,
1846 "operation not expected to succeed",
1847 return isl_stat_error);
1849 return isl_stat_ok;
1852 struct {
1853 const char *set;
1854 const char *context;
1855 const char *gist;
1856 } gist_tests[] = {
1857 { "{ [1, -1, 3] }",
1858 "{ [1, b, 2 - b] : -1 <= b <= 2 }",
1859 "{ [a, -1, c] }" },
1860 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1861 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1862 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1863 "{ [a, b, c] : a <= 15 }" },
1864 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1865 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1866 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1867 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1868 { "{ [m, n, a, b] : a <= 2147 + n }",
1869 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1870 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1871 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1872 "b >= 0) }",
1873 "{ [m, n, ku, kl] }" },
1874 { "{ [a, a, b] : a >= 10 }",
1875 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1876 "{ [a, a, b] : a >= 10 }" },
1877 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1878 "{ [0, j] : j >= 0 }" },
1879 /* Check that no constraints on i6 are introduced in the gist */
1880 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1881 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1882 "5e0 <= 381 - t1 and i4 <= 1) }",
1883 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1884 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1885 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1886 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1887 "20e0 >= 1511 - 4t1 - 5i4) }" },
1888 /* Check that no constraints on i6 are introduced in the gist */
1889 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1890 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1891 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1892 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1893 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1894 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1895 "20e1 <= 1530 - 4t1 - 5i4 and "
1896 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1897 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1898 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1899 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1900 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1901 "10e0 <= -91 + 5i4 + 4i6 and "
1902 "10e0 >= -105 + 5i4 + 4i6) }",
1903 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1904 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1905 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1906 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1907 "{ [a, b, q, p] : b >= 1 + a }",
1908 "{ [a, b, q, p] : false }" },
1909 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1910 "[n] -> { [x] : x mod 32 = 0 }",
1911 "[n] -> { [x = n] }" },
1912 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1913 "{ [x] : x mod 2 = 0 }" },
1914 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1915 "{ [x] : x mod 128 = 0 }" },
1916 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1917 "{ [x] : x mod 3200 = 0 }" },
1918 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1919 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1920 "{ [a, b, c = a] }" },
1921 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1922 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1923 "{ [a, b, c = a] : a mod 3 = 0 }" },
1924 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1925 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1926 "{ [x] }" },
1927 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1928 "{ [x,y] : 1 <= y <= 3 }",
1929 "{ [x,y] }" },
1932 /* Check that isl_set_gist behaves as expected.
1934 * For the test cases in gist_tests, besides checking that the result
1935 * is as expected, also check that applying the gist operation does
1936 * not modify the input set (an earlier version of isl would do that) and
1937 * that the test case is consistent, i.e., that the gist has the same
1938 * intersection with the context as the input set.
1940 static int test_gist(struct isl_ctx *ctx)
1942 int i;
1943 const char *str;
1944 isl_basic_set *bset1, *bset2;
1945 isl_map *map1, *map2;
1946 isl_bool equal;
1947 isl_size n_div;
1949 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1950 isl_bool equal_input, equal_intersection;
1951 isl_set *set1, *set2, *copy, *context;
1953 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1954 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1955 copy = isl_set_copy(set1);
1956 set1 = isl_set_gist(set1, isl_set_copy(context));
1957 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1958 equal = isl_set_is_equal(set1, set2);
1959 isl_set_free(set1);
1960 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1961 equal_input = isl_set_is_equal(set1, copy);
1962 isl_set_free(copy);
1963 set1 = isl_set_intersect(set1, isl_set_copy(context));
1964 set2 = isl_set_intersect(set2, context);
1965 equal_intersection = isl_set_is_equal(set1, set2);
1966 isl_set_free(set2);
1967 isl_set_free(set1);
1968 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1969 return -1;
1970 if (!equal)
1971 isl_die(ctx, isl_error_unknown,
1972 "incorrect gist result", return -1);
1973 if (!equal_input)
1974 isl_die(ctx, isl_error_unknown,
1975 "gist modified input", return -1);
1976 if (!equal_input)
1977 isl_die(ctx, isl_error_unknown,
1978 "inconsistent gist test case", return -1);
1981 if (test_gist_fail(ctx) < 0)
1982 return -1;
1984 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1985 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1986 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1987 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1988 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1989 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1990 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1991 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1992 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1993 bset1 = isl_basic_set_read_from_str(ctx, str);
1994 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1995 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1996 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1997 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1998 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1999 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
2000 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
2001 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
2002 bset2 = isl_basic_set_read_from_str(ctx, str);
2003 bset1 = isl_basic_set_gist(bset1, bset2);
2004 assert(bset1 && bset1->n_div == 0);
2005 isl_basic_set_free(bset1);
2007 /* Check that the integer divisions of the second disjunct
2008 * do not spread to the first disjunct.
2010 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
2011 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
2012 "(exists (e0 = [(-1 + t1)/16], "
2013 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
2014 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
2015 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
2016 "o0 <= 4294967295 and t1 <= -1)) }";
2017 map1 = isl_map_read_from_str(ctx, str);
2018 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
2019 map2 = isl_map_read_from_str(ctx, str);
2020 map1 = isl_map_gist(map1, map2);
2021 if (!map1)
2022 return -1;
2023 if (map1->n != 1)
2024 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
2025 isl_map_free(map1); return -1);
2026 n_div = isl_basic_map_dim(map1->p[0], isl_dim_div);
2027 isl_map_free(map1);
2028 if (n_div < 0)
2029 return -1;
2030 if (n_div != 1)
2031 isl_die(ctx, isl_error_unknown, "expecting single div",
2032 return -1);
2034 if (test_gist_empty(ctx) < 0)
2035 return -1;
2036 if (test_plain_gist(ctx) < 0)
2037 return -1;
2039 return 0;
2042 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
2044 isl_set *set, *set2;
2045 int equal;
2046 int one;
2048 set = isl_set_read_from_str(ctx, str);
2049 set = isl_set_coalesce(set);
2050 set2 = isl_set_read_from_str(ctx, str);
2051 equal = isl_set_is_equal(set, set2);
2052 one = set && set->n == 1;
2053 isl_set_free(set);
2054 isl_set_free(set2);
2056 if (equal < 0)
2057 return -1;
2058 if (!equal)
2059 isl_die(ctx, isl_error_unknown,
2060 "coalesced set not equal to input", return -1);
2061 if (check_one && !one)
2062 isl_die(ctx, isl_error_unknown,
2063 "coalesced set should not be a union", return -1);
2065 return 0;
2068 /* Inputs for coalescing tests with unbounded wrapping.
2069 * "str" is a string representation of the input set.
2070 * "single_disjunct" is set if we expect the result to consist of
2071 * a single disjunct.
2073 struct {
2074 int single_disjunct;
2075 const char *str;
2076 } coalesce_unbounded_tests[] = {
2077 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
2078 "-x - y + 1 >= 0 and -3 <= z <= 3;"
2079 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
2080 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
2081 "-10 <= y <= 0}" },
2082 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
2083 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
2084 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
2085 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
2086 { 0, "{ [x, y, z] : 0 <= x,y,z <= 100 and 0 < z <= 2 + 2x + 2y; "
2087 "[x, y, 0] : x,y <= 100 and y <= 9 + 11x and x <= 9 + 11y }" },
2088 { 1, "{ [0:1, 0:1]; [0, 2:3] }" },
2089 { 1, "{ [0:1, 0:1]; [0, 2:3]; [1, -2:-1] }" },
2090 { 1, "{ [0:3, 0:1]; [1:2, 2:5] }" },
2091 { 1, "{ [0:3, 0:1]; [0:2, 2:5] }" },
2092 { 1, "{ [0:3, 0:1]; [1:3, 2:5] }" },
2093 { 0, "{ [0:3, 0:1]; [1:4, 2:5] }" },
2094 { 0, "{ [0:3, 0:1]; [1:5, 2:5] }" },
2097 /* Test the functionality of isl_set_coalesce with the bounded wrapping
2098 * option turned off.
2100 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
2102 int i;
2103 int r = 0;
2104 int bounded;
2106 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
2107 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
2109 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
2110 const char *str = coalesce_unbounded_tests[i].str;
2111 int check_one = coalesce_unbounded_tests[i].single_disjunct;
2112 if (test_coalesce_set(ctx, str, check_one) >= 0)
2113 continue;
2114 r = -1;
2115 break;
2118 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
2120 return r;
2123 /* Inputs for coalescing tests.
2124 * "str" is a string representation of the input set.
2125 * "single_disjunct" is set if we expect the result to consist of
2126 * a single disjunct.
2128 struct {
2129 int single_disjunct;
2130 const char *str;
2131 } coalesce_tests[] = {
2132 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
2133 "y >= x & x >= 2 & 5 >= y }" },
2134 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
2135 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
2136 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
2137 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
2138 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
2139 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
2140 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
2141 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
2142 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
2143 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
2144 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
2145 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
2146 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
2147 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
2148 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
2149 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
2150 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
2151 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
2152 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
2153 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
2154 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
2155 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
2156 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
2157 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
2158 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
2159 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
2160 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
2161 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
2162 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
2163 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
2164 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
2165 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
2166 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
2167 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
2168 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
2169 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
2170 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
2171 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
2172 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
2173 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
2174 "[o0, o1, o2, o3, o4, o5, o6]] : "
2175 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
2176 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
2177 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
2178 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
2179 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
2180 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
2181 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
2182 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
2183 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
2184 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
2185 "o6 >= i3 + i6 - o3 and M >= 0 and "
2186 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
2187 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
2188 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
2189 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
2190 "(o0 = 0 and M >= 2 and N >= 3) or "
2191 "(M = 0 and o0 = 0 and N >= 3) }" },
2192 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
2193 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
2194 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
2195 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
2196 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
2197 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
2198 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
2199 "(y = 3 and x = 1) }" },
2200 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
2201 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
2202 "i1 <= M and i3 <= M and i4 <= M) or "
2203 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
2204 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
2205 "i4 <= -1 + M) }" },
2206 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
2207 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
2208 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
2209 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
2210 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
2211 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
2212 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
2213 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
2214 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
2215 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
2216 { 0, "{ [a, b] : exists e : 2e = a and "
2217 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
2218 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
2219 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
2220 "j >= 1 and j' <= i + j - i' and i >= 1; "
2221 "[1, 1, 1, 1] }" },
2222 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
2223 "[i,j] : exists a : j = 3a }" },
2224 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
2225 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
2226 "a >= 3) or "
2227 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
2228 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
2229 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
2230 "c <= 6 + 8a and a >= 3; "
2231 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
2232 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
2233 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2234 "[x,0] : 3 <= x <= 4 }" },
2235 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
2236 "[x,0] : 4 <= x <= 5 }" },
2237 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2238 "[x,0] : 3 <= x <= 5 }" },
2239 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
2240 "[x,0] : 3 <= x <= 4 }" },
2241 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
2242 "i1 <= 0; "
2243 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
2244 { 1, "{ [0,0]; [1,1] }" },
2245 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
2246 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
2247 "ii <= k;"
2248 "[k, 0, k] : k <= 6 and k >= 1 }" },
2249 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
2250 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
2251 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
2252 { 1, "[n] -> { [1] : n >= 0;"
2253 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
2254 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
2255 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
2256 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
2257 "2e0 <= x and 2e0 <= n);"
2258 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
2259 "n >= 0) }" },
2260 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
2261 "128e0 >= -134 + 127t1 and t1 >= 2 and "
2262 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
2263 "t1 = 1 }" },
2264 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
2265 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
2266 "[0, 0] }" },
2267 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
2268 "t1 >= 13 and t1 <= 16);"
2269 "[t1] : t1 <= 15 and t1 >= 12 }" },
2270 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
2271 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
2272 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
2273 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
2274 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
2275 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
2276 "i <= 4j + 2 }" },
2277 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
2278 "(exists (e0 : 3e0 = -2 + c0)) }" },
2279 { 0, "[n, b0, t0] -> "
2280 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2281 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2282 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2283 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2284 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
2285 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
2286 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
2287 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
2288 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2289 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2290 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2291 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
2292 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
2293 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
2294 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
2295 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
2296 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
2297 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
2298 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
2299 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
2300 { 0, "{ [i0, i1, i2] : "
2301 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
2302 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
2303 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
2304 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
2305 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
2306 "32e0 <= 31 + i0)) or "
2307 "i0 >= 0 }" },
2308 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
2309 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
2310 "2*floor((c)/2) = c and 0 <= a <= 192;"
2311 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
2313 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
2314 "(0 <= a <= b <= n) }" },
2315 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
2316 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
2317 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
2318 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2319 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2320 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2321 "b = 3 and 9e0 <= -19 + 2c)) }" },
2322 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2323 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2324 "(a = 4 and b = 3 and "
2325 "9*floor((-16 + 2c)/9) <= -19 + 2c) }" },
2326 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
2327 "(b = -1 + a and 0 < a <= 3 and "
2328 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2329 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2330 "b = 3 and 9e0 <= -19 + 2c)) }" },
2331 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2332 "[1, 0] }" },
2333 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2334 "[0, 1] }" },
2335 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2336 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2337 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
2338 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
2339 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
2340 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
2341 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
2342 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
2343 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
2344 { 1, "{ [a] : a <= 8 and "
2345 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
2346 { 1, "{ [x, y] : 2y = -x and x <= 0 or "
2347 "x <= -1 and 2y <= -x - 1 and 2y >= x - 1 }" },
2348 { 0, "{ [x, y] : 2y = -x and x <= 0 or "
2349 "x <= -2 and 2y <= -x - 1 and 2y >= x - 1 }" },
2350 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2351 "(a < 0 and 3*floor((a)/3) < a) }" },
2352 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2353 "(a < -1 and 3*floor((a)/3) < a) }" },
2354 { 1, "{ [a, b] : a <= 1024 and b >= 0 and "
2355 "((-31 - a + b <= 32*floor((-1 - a)/32) <= -33 + b and "
2356 "32*floor((-1 - a)/32) <= -16 + b + 16*floor((-1 - a)/16))"
2357 "or (2 <= a <= 15 and b < a)) }" },
2358 { 1, "{ [a] : a > 0 and ((16*floor((a)/16) < a and "
2359 "32*floor((a)/32) < a) or a <= 15) }" },
2360 { 1, "{ [a, b, c, d] : (-a + d) mod 64 = 0 and a <= 8 and b <= 1 and "
2361 "10 - a <= c <= 3 and d >= 5 and 9 - 64b <= d <= 70;"
2362 "[a, b = 1, c, d] : (-a + d) mod 64 = 0 and a <= 8 and c >= 4 and "
2363 "10 - a <= c <= 5 and 5 <= d <= 73 - c }" },
2364 { 1, "[n, m] -> { S_0[i] : (-n + i) mod 3 = 0 and m >= 3 + n and "
2365 "i >= n and 3*floor((2 + n + 2m)/3) <= n + 3m - i; "
2366 "S_0[n] : n <= m <= 2 + n }" },
2367 { 1, "{ [a, b] : exists (e0: 0 <= a <= 1 and b >= 0 and "
2368 "2e0 >= -5 + a + 2b and 2e0 >= -1 + a + b and "
2369 "2e0 <= a + b); "
2370 "[a, b] : exists (e0: 0 <= a <= 1 and 2e0 >= -5 + a + 2b and "
2371 "2e0 >= -1 - a + b and 2e0 <= -a + b and "
2372 "2e0 < -a + 2b) }" },
2373 { 1, "{ [i, j, i - 8j] : 8 <= i <= 63 and -7 + i <= 8j <= i; "
2374 "[i, 0, i] : 0 <= i <= 7 }" },
2375 { 1, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [1, 1] }" },
2376 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [0, 2] }" },
2377 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [-1, 3] }" },
2378 { 1, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [1, 1] }" },
2379 { 0, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [2, 1] }" },
2380 { 0, "{ [a, c] : (2 + a) mod 4 = 0 or "
2381 "(c = 4 + a and 4 * floor((a)/4) = a and a >= 0 and a <= 4) or "
2382 "(c = 3 + a and 4 * floor((-1 + a)/4) = -1 + a and "
2383 "a > 0 and a <= 5) }" },
2384 { 1, "{ [1, 0, 0]; [a, b, c] : -1 <= -a < b <= 0 and 2c > b }" },
2385 { 0, "{ [j, a, l] : a mod 2 = 0 and j <= 29 and a >= 2 and "
2386 "2a <= -5 + j and 32j + 2a + 2 <= 4l < 33j; "
2387 "[j, 0, l] : 4 <= j <= 29 and -3 + 33j <= 4l <= 33j }" },
2388 { 0, "{ [0:1, 0:1]; [0, 2:3] }" },
2389 { 1, "{ [a] : (a = 0 or ((1 + a) mod 2 = 0 and 0 < a <= 15) or "
2390 "((a) mod 2 = 0 and 0 < a <= 15)) }" },
2391 { 1, "{ rat: [0:2]; rat: [1:3] }" },
2394 /* A specialized coalescing test case that would result
2395 * in a segmentation fault or a failed assertion in earlier versions of isl.
2397 static int test_coalesce_special(struct isl_ctx *ctx)
2399 const char *str;
2400 isl_map *map1, *map2;
2402 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2403 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
2404 "(y = 201 and o1 <= 239 and o1 >= 212) or "
2405 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
2406 "o1 <= 239 and o1 >= 212)) or "
2407 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
2408 "o1 <= 241 and o1 >= 240));"
2409 "[S_L220_OUT[] -> T7[]] -> "
2410 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
2411 "(y = 2 and o1 <= 241 and o1 >= 212) or "
2412 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
2413 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
2414 map1 = isl_map_read_from_str(ctx, str);
2415 map1 = isl_map_align_divs_internal(map1);
2416 map1 = isl_map_coalesce(map1);
2417 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2418 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
2419 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
2420 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
2421 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
2422 map2 = isl_map_read_from_str(ctx, str);
2423 map2 = isl_map_union(map2, map1);
2424 map2 = isl_map_align_divs_internal(map2);
2425 map2 = isl_map_coalesce(map2);
2426 isl_map_free(map2);
2427 if (!map2)
2428 return -1;
2430 return 0;
2433 /* Check that the union of the basic sets described by "str1" and "str2"
2434 * can be coalesced and that the result is equal to the union.
2435 * The explicit call to isl_basic_set_union prevents the implicit
2436 * equality constraints in the basic maps from being detected prior
2437 * to the call to isl_set_coalesce, at least at the point
2438 * where this function was introduced.
2440 static isl_stat test_coalesce_union(isl_ctx *ctx, const char *str1,
2441 const char *str2)
2443 isl_basic_set *bset1, *bset2;
2444 isl_set *set, *set2;
2445 isl_bool equal;
2447 bset1 = isl_basic_set_read_from_str(ctx, str1);
2448 bset2 = isl_basic_set_read_from_str(ctx, str2);
2449 set = isl_basic_set_union(bset1, bset2);
2450 set = isl_set_coalesce(set);
2452 bset1 = isl_basic_set_read_from_str(ctx, str1);
2453 bset2 = isl_basic_set_read_from_str(ctx, str2);
2454 set2 = isl_basic_set_union(bset1, bset2);
2456 equal = isl_set_is_equal(set, set2);
2457 isl_set_free(set);
2458 isl_set_free(set2);
2460 if (equal < 0)
2461 return isl_stat_error;
2462 if (!equal)
2463 isl_die(ctx, isl_error_unknown,
2464 "coalesced set not equal to input",
2465 return isl_stat_error);
2467 return isl_stat_non_null(set);
2470 /* A specialized coalescing test case that would result in an assertion
2471 * in an earlier version of isl. Use test_coalesce_union with
2472 * an explicit call to isl_basic_set_union to prevent the implicit
2473 * equality constraints in the first basic map from being detected prior
2474 * to the call to isl_set_coalesce, at least at the point
2475 * where this test case was introduced.
2477 static isl_stat test_coalesce_special2(struct isl_ctx *ctx)
2479 const char *str1;
2480 const char *str2;
2482 str1 = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
2483 str2 = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }";
2484 return test_coalesce_union(ctx, str1, str2);
2487 /* Check that calling isl_set_coalesce does not leave other sets
2488 * that may share some information with the input to isl_set_coalesce
2489 * in an inconsistent state.
2490 * In particular, older versions of isl would modify all copies
2491 * of the basic sets in the isl_set_coalesce input in a way
2492 * that could leave them in an inconsistent state.
2493 * The result of printing any other set containing one of these
2494 * basic sets would then result in an invalid set description.
2496 static int test_coalesce_special3(isl_ctx *ctx)
2498 const char *str;
2499 char *s;
2500 isl_set *set1, *set2;
2501 isl_printer *p;
2503 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
2504 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
2505 set2 = isl_set_read_from_str(ctx, str);
2506 set1 = isl_set_union(set1, isl_set_copy(set2));
2507 set1 = isl_set_coalesce(set1);
2508 isl_set_free(set1);
2510 p = isl_printer_to_str(ctx);
2511 p = isl_printer_print_set(p, set2);
2512 isl_set_free(set2);
2513 s = isl_printer_get_str(p);
2514 isl_printer_free(p);
2515 set1 = isl_set_read_from_str(ctx, s);
2516 free(s);
2517 isl_set_free(set1);
2519 if (!set1)
2520 return -1;
2522 return 0;
2525 /* Check that calling isl_set_coalesce on the intersection of
2526 * the sets described by "s1" and "s2" does not leave other sets
2527 * that may share some information with the input to isl_set_coalesce
2528 * in an inconsistent state.
2529 * In particular, when isl_set_coalesce detects equality constraints,
2530 * it does not immediately perform Gaussian elimination on them,
2531 * but then it needs to ensure that it is performed at some point.
2532 * The input set has implicit equality constraints in the first disjunct.
2533 * It is constructed as an intersection, because otherwise
2534 * those equality constraints would already be detected during parsing.
2536 static isl_stat test_coalesce_intersection(isl_ctx *ctx,
2537 const char *s1, const char *s2)
2539 isl_set *set1, *set2;
2541 set1 = isl_set_read_from_str(ctx, s1);
2542 set2 = isl_set_read_from_str(ctx, s2);
2543 set1 = isl_set_intersect(set1, set2);
2544 isl_set_free(isl_set_coalesce(isl_set_copy(set1)));
2545 set1 = isl_set_coalesce(set1);
2546 isl_set_free(set1);
2548 if (!set1)
2549 return isl_stat_error;
2551 return isl_stat_ok;
2554 /* Check that calling isl_set_coalesce does not leave other sets
2555 * that may share some information with the input to isl_set_coalesce
2556 * in an inconsistent state, for the case where one disjunct
2557 * is a subset of the other.
2559 static isl_stat test_coalesce_special4(isl_ctx *ctx)
2561 const char *s1, *s2;
2563 s1 = "{ [a, b] : b <= 0 or a <= 1 }";
2564 s2 = "{ [a, b] : -1 <= -a < b }";
2565 return test_coalesce_intersection(ctx, s1, s2);
2568 /* Check that calling isl_set_coalesce does not leave other sets
2569 * that may share some information with the input to isl_set_coalesce
2570 * in an inconsistent state, for the case where two disjuncts
2571 * can be fused.
2573 static isl_stat test_coalesce_special5(isl_ctx *ctx)
2575 const char *s1, *s2;
2577 s1 = "{ [a, b, c] : b <= 0 }";
2578 s2 = "{ [a, b, c] : -1 <= -a < b and (c >= 0 or c < 0) }";
2579 return test_coalesce_intersection(ctx, s1, s2);
2582 /* Check that calling isl_set_coalesce does not leave other sets
2583 * that may share some information with the input to isl_set_coalesce
2584 * in an inconsistent state, for the case where two disjuncts
2585 * can be fused and where both disjuncts have implicit equality constraints.
2587 static isl_stat test_coalesce_special6(isl_ctx *ctx)
2589 const char *s1, *s2;
2591 s1 = "{ [a, b, c] : c <= 0 }";
2592 s2 = "{ [a, b, c] : 0 <= a <= b <= c or (0 <= b <= c and a > 0) }";
2593 return test_coalesce_intersection(ctx, s1, s2);
2596 /* A specialized coalescing test case that would result in an assertion failure
2597 * in an earlier version of isl. Use test_coalesce_union with
2598 * an explicit call to isl_basic_set_union to prevent the implicit
2599 * equality constraints in the basic maps from being detected prior
2600 * to the call to isl_set_coalesce, at least at the point
2601 * where this test case was introduced.
2603 static isl_stat test_coalesce_special7(isl_ctx *ctx)
2605 const char *str1;
2606 const char *str2;
2608 str1 = "{ [a, b, c=0:17] : a <= 7 and 2b <= 11 - a and "
2609 "c <= -7 + 2a and 2c >= - 3 + 3a - 2b }";
2610 str2 = "{ [a, b, c] : c > -15a and c >= -7 + 2a and c < 0 and "
2611 "3c <= -5 + 5a - 3b and 2b >= 11 - a }";
2612 return test_coalesce_union(ctx, str1, str2);
2615 /* A specialized coalescing test case that would result in a disjunct
2616 * getting dropped in an earlier version of isl. Use test_coalesce_union with
2617 * an explicit call to isl_basic_set_union to prevent the implicit
2618 * equality constraints in the basic maps from being detected prior
2619 * to the call to isl_set_coalesce, at least at the point
2620 * where this test case was introduced.
2622 static isl_stat test_coalesce_special8(isl_ctx *ctx)
2624 const char *str1;
2625 const char *str2;
2627 str1 = "{ [a, b, c] : 2c <= -a and b >= -a and b <= 5 and "
2628 "6c > -7a and 11c >= -5a - b and a <= 3 }";
2629 str2 = "{ [a, b, c] : 6c > -7a and b >= -a and b <= 5 and "
2630 "11c >= -5a - b and a >= 4 and 2b <= a and 2c <= -a }";
2631 return test_coalesce_union(ctx, str1, str2);
2634 /* Test the functionality of isl_set_coalesce.
2635 * That is, check that the output is always equal to the input
2636 * and in some cases that the result consists of a single disjunct.
2638 static int test_coalesce(struct isl_ctx *ctx)
2640 int i;
2642 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2643 const char *str = coalesce_tests[i].str;
2644 int check_one = coalesce_tests[i].single_disjunct;
2645 if (test_coalesce_set(ctx, str, check_one) < 0)
2646 return -1;
2649 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2650 return -1;
2651 if (test_coalesce_special(ctx) < 0)
2652 return -1;
2653 if (test_coalesce_special2(ctx) < 0)
2654 return -1;
2655 if (test_coalesce_special3(ctx) < 0)
2656 return -1;
2657 if (test_coalesce_special4(ctx) < 0)
2658 return -1;
2659 if (test_coalesce_special5(ctx) < 0)
2660 return -1;
2661 if (test_coalesce_special6(ctx) < 0)
2662 return -1;
2663 if (test_coalesce_special7(ctx) < 0)
2664 return -1;
2665 if (test_coalesce_special8(ctx) < 0)
2666 return -1;
2668 return 0;
2671 /* Construct a representation of the graph on the right of Figure 1
2672 * in "Computing the Transitive Closure of a Union of
2673 * Affine Integer Tuple Relations".
2675 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2677 isl_set *dom;
2678 isl_map *up, *right;
2680 dom = isl_set_read_from_str(ctx,
2681 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2682 "2 x - 3 y + 3 >= 0 }");
2683 right = isl_map_read_from_str(ctx,
2684 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2685 up = isl_map_read_from_str(ctx,
2686 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2687 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2688 right = isl_map_intersect_range(right, isl_set_copy(dom));
2689 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2690 up = isl_map_intersect_range(up, dom);
2691 return isl_map_union(up, right);
2694 /* Construct a representation of the power of the graph
2695 * on the right of Figure 1 in "Computing the Transitive Closure of
2696 * a Union of Affine Integer Tuple Relations".
2698 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2700 return isl_map_read_from_str(ctx,
2701 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2702 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2703 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2706 /* Construct a representation of the transitive closure of the graph
2707 * on the right of Figure 1 in "Computing the Transitive Closure of
2708 * a Union of Affine Integer Tuple Relations".
2710 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2712 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2715 static int test_closure(isl_ctx *ctx)
2717 const char *str;
2718 isl_map *map, *map2;
2719 isl_bool exact, equal;
2721 /* COCOA example 1 */
2722 map = isl_map_read_from_str(ctx,
2723 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2724 "1 <= i and i < n and 1 <= j and j < n or "
2725 "i2 = i + 1 and j2 = j - 1 and "
2726 "1 <= i and i < n and 2 <= j and j <= n }");
2727 map = isl_map_power(map, &exact);
2728 assert(exact);
2729 isl_map_free(map);
2731 /* COCOA example 1 */
2732 map = isl_map_read_from_str(ctx,
2733 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2734 "1 <= i and i < n and 1 <= j and j < n or "
2735 "i2 = i + 1 and j2 = j - 1 and "
2736 "1 <= i and i < n and 2 <= j and j <= n }");
2737 map = isl_map_transitive_closure(map, &exact);
2738 assert(exact);
2739 map2 = isl_map_read_from_str(ctx,
2740 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2741 "1 <= i and i < n and 1 <= j and j <= n and "
2742 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2743 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2744 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2745 assert(isl_map_is_equal(map, map2));
2746 isl_map_free(map2);
2747 isl_map_free(map);
2749 map = isl_map_read_from_str(ctx,
2750 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2751 " 0 <= y and y <= n }");
2752 map = isl_map_transitive_closure(map, &exact);
2753 map2 = isl_map_read_from_str(ctx,
2754 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2755 " 0 <= y and y <= n }");
2756 assert(isl_map_is_equal(map, map2));
2757 isl_map_free(map2);
2758 isl_map_free(map);
2760 /* COCOA example 2 */
2761 map = isl_map_read_from_str(ctx,
2762 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2763 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2764 "i2 = i + 2 and j2 = j - 2 and "
2765 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2766 map = isl_map_transitive_closure(map, &exact);
2767 assert(exact);
2768 map2 = isl_map_read_from_str(ctx,
2769 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2770 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2771 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2772 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2773 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2774 assert(isl_map_is_equal(map, map2));
2775 isl_map_free(map);
2776 isl_map_free(map2);
2778 /* COCOA Fig.2 left */
2779 map = isl_map_read_from_str(ctx,
2780 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2781 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2782 "j <= n or "
2783 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2784 "j <= 2 i - 3 and j <= n - 2 or "
2785 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2786 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2787 map = isl_map_transitive_closure(map, &exact);
2788 assert(exact);
2789 isl_map_free(map);
2791 /* COCOA Fig.2 right */
2792 map = isl_map_read_from_str(ctx,
2793 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2794 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2795 "j <= n or "
2796 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2797 "j <= 2 i - 4 and j <= n - 3 or "
2798 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2799 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2800 map = isl_map_power(map, &exact);
2801 assert(exact);
2802 isl_map_free(map);
2804 /* COCOA Fig.2 right */
2805 map = isl_map_read_from_str(ctx,
2806 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2807 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2808 "j <= n or "
2809 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2810 "j <= 2 i - 4 and j <= n - 3 or "
2811 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2812 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2813 map = isl_map_transitive_closure(map, &exact);
2814 assert(exact);
2815 map2 = isl_map_read_from_str(ctx,
2816 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2817 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2818 "j <= n and 3 + i + 2 j <= 3 n and "
2819 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2820 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2821 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2822 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2823 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2824 assert(isl_map_is_equal(map, map2));
2825 isl_map_free(map2);
2826 isl_map_free(map);
2828 map = cocoa_fig_1_right_graph(ctx);
2829 map = isl_map_transitive_closure(map, &exact);
2830 assert(exact);
2831 map2 = cocoa_fig_1_right_tc(ctx);
2832 assert(isl_map_is_equal(map, map2));
2833 isl_map_free(map2);
2834 isl_map_free(map);
2836 map = cocoa_fig_1_right_graph(ctx);
2837 map = isl_map_power(map, &exact);
2838 map2 = cocoa_fig_1_right_power(ctx);
2839 equal = isl_map_is_equal(map, map2);
2840 isl_map_free(map2);
2841 isl_map_free(map);
2842 if (equal < 0)
2843 return -1;
2844 if (!exact)
2845 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2846 if (!equal)
2847 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2849 /* COCOA Theorem 1 counter example */
2850 map = isl_map_read_from_str(ctx,
2851 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2852 "i2 = 1 and j2 = j or "
2853 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2854 map = isl_map_transitive_closure(map, &exact);
2855 assert(exact);
2856 isl_map_free(map);
2858 map = isl_map_read_from_str(ctx,
2859 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2860 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2861 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2862 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2863 map = isl_map_transitive_closure(map, &exact);
2864 assert(exact);
2865 isl_map_free(map);
2867 /* Kelly et al 1996, fig 12 */
2868 map = isl_map_read_from_str(ctx,
2869 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2870 "1 <= i,j,j+1 <= n or "
2871 "j = n and j2 = 1 and i2 = i + 1 and "
2872 "1 <= i,i+1 <= n }");
2873 map = isl_map_transitive_closure(map, &exact);
2874 assert(exact);
2875 map2 = isl_map_read_from_str(ctx,
2876 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2877 "1 <= i <= n and i = i2 or "
2878 "1 <= i < i2 <= n and 1 <= j <= n and "
2879 "1 <= j2 <= n }");
2880 assert(isl_map_is_equal(map, map2));
2881 isl_map_free(map2);
2882 isl_map_free(map);
2884 /* Omega's closure4 */
2885 map = isl_map_read_from_str(ctx,
2886 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2887 "1 <= x,y <= 10 or "
2888 "x2 = x + 1 and y2 = y and "
2889 "1 <= x <= 20 && 5 <= y <= 15 }");
2890 map = isl_map_transitive_closure(map, &exact);
2891 assert(exact);
2892 isl_map_free(map);
2894 map = isl_map_read_from_str(ctx,
2895 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2896 map = isl_map_transitive_closure(map, &exact);
2897 assert(!exact);
2898 map2 = isl_map_read_from_str(ctx,
2899 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2900 assert(isl_map_is_equal(map, map2));
2901 isl_map_free(map);
2902 isl_map_free(map2);
2904 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2905 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2906 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2907 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2908 map = isl_map_read_from_str(ctx, str);
2909 map = isl_map_transitive_closure(map, &exact);
2910 assert(exact);
2911 map2 = isl_map_read_from_str(ctx, str);
2912 assert(isl_map_is_equal(map, map2));
2913 isl_map_free(map);
2914 isl_map_free(map2);
2916 str = "{[0] -> [1]; [2] -> [3]}";
2917 map = isl_map_read_from_str(ctx, str);
2918 map = isl_map_transitive_closure(map, &exact);
2919 assert(exact);
2920 map2 = isl_map_read_from_str(ctx, str);
2921 assert(isl_map_is_equal(map, map2));
2922 isl_map_free(map);
2923 isl_map_free(map2);
2925 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2926 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2927 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2928 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2929 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2930 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2931 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2932 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2933 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2934 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2935 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2936 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2937 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2938 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2939 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2940 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2941 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2942 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2943 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2944 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2945 map = isl_map_read_from_str(ctx, str);
2946 map = isl_map_transitive_closure(map, NULL);
2947 assert(map);
2948 isl_map_free(map);
2950 return 0;
2953 /* Check that the actual result of a boolean operation is equal
2954 * to the expected result.
2956 static isl_stat check_bool(isl_ctx *ctx, isl_bool actual, isl_bool expected)
2958 if (actual != expected)
2959 isl_die(ctx, isl_error_unknown,
2960 "incorrect boolean operation", return isl_stat_error);
2961 return isl_stat_ok;
2964 /* Test operations on isl_bool values.
2966 * This tests:
2968 * isl_bool_not
2969 * isl_bool_ok
2971 static int test_isl_bool(isl_ctx *ctx)
2973 if (check_bool(ctx, isl_bool_not(isl_bool_true), isl_bool_false) < 0)
2974 return -1;
2975 if (check_bool(ctx, isl_bool_not(isl_bool_false), isl_bool_true) < 0)
2976 return -1;
2977 if (check_bool(ctx, isl_bool_not(isl_bool_error), isl_bool_error) < 0)
2978 return -1;
2979 if (check_bool(ctx, isl_bool_ok(0), isl_bool_false) < 0)
2980 return -1;
2981 if (check_bool(ctx, isl_bool_ok(1), isl_bool_true) < 0)
2982 return -1;
2983 if (check_bool(ctx, isl_bool_ok(-1), isl_bool_true) < 0)
2984 return -1;
2985 if (check_bool(ctx, isl_bool_ok(2), isl_bool_true) < 0)
2986 return -1;
2987 if (check_bool(ctx, isl_bool_ok(-2), isl_bool_true) < 0)
2988 return -1;
2990 return 0;
2993 static int test_lex(struct isl_ctx *ctx)
2995 isl_space *space;
2996 isl_map *map;
2997 int empty;
2999 space = isl_space_set_alloc(ctx, 0, 0);
3000 map = isl_map_lex_le(space);
3001 empty = isl_map_is_empty(map);
3002 isl_map_free(map);
3004 if (empty < 0)
3005 return -1;
3006 if (empty)
3007 isl_die(ctx, isl_error_unknown,
3008 "expecting non-empty result", return -1);
3010 return 0;
3013 /* Inputs for isl_map_lexmin tests.
3014 * "map" is the input and "lexmin" is the expected result.
3016 struct {
3017 const char *map;
3018 const char *lexmin;
3019 } lexmin_tests [] = {
3020 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
3021 "{ [x] -> [5] : 6 <= x <= 8; "
3022 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
3023 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
3024 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
3025 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
3026 "{ [x] -> [y] : (4y = x and x >= 0) or "
3027 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
3028 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
3029 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
3030 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
3031 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
3032 /* Check that empty pieces are properly combined. */
3033 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
3034 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
3035 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
3036 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
3037 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
3038 "x >= 4 }" },
3039 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
3040 "a <= 255 and c <= 255 and d <= 255 - j and "
3041 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
3042 "247d <= 247 + k - j and 247d <= 247 + k - b and "
3043 "247d <= 247 + i and 248 - b <= 248d <= c and "
3044 "254d >= i - a + b and 254d >= -a + b and "
3045 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
3046 "{ [i, k, j] -> "
3047 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
3048 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
3049 "247j >= 62738 - i and 509j <= 129795 + i and "
3050 "742j >= 188724 - i; "
3051 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
3052 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
3053 "16*floor((8 + b)/16) <= 7 + b; "
3054 "[a] -> [1] }",
3055 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
3056 "[a] -> [b = 0] : 0 < a <= 509 }" },
3057 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
3058 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
3059 { "{ rat: [i] : 21 <= 2i <= 29 or i = 5 }", "{ rat: [5] }" },
3062 static int test_lexmin(struct isl_ctx *ctx)
3064 int i;
3065 int equal;
3066 const char *str;
3067 isl_basic_map *bmap;
3068 isl_map *map, *map2;
3069 isl_set *set;
3070 isl_set *set2;
3071 isl_pw_multi_aff *pma;
3073 str = "[p0, p1] -> { [] -> [] : "
3074 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
3075 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
3076 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
3077 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
3078 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
3079 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
3080 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
3081 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
3082 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
3083 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
3084 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
3085 map = isl_map_read_from_str(ctx, str);
3086 map = isl_map_lexmin(map);
3087 isl_map_free(map);
3088 if (!map)
3089 return -1;
3091 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
3092 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
3093 set = isl_set_read_from_str(ctx, str);
3094 set = isl_set_lexmax(set);
3095 str = "[C] -> { [obj,a,b,c] : C = 8 }";
3096 set2 = isl_set_read_from_str(ctx, str);
3097 set = isl_set_intersect(set, set2);
3098 assert(!isl_set_is_empty(set));
3099 isl_set_free(set);
3101 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
3102 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
3103 map = isl_map_lexmin(map);
3104 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
3105 equal = isl_map_is_equal(map, map2);
3106 isl_map_free(map);
3107 isl_map_free(map2);
3109 if (equal < 0)
3110 return -1;
3111 if (!equal)
3112 isl_die(ctx, isl_error_unknown,
3113 "unexpected result", return -1);
3116 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
3117 " 8i' <= i and 8i' >= -7 + i }";
3118 bmap = isl_basic_map_read_from_str(ctx, str);
3119 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
3120 map2 = isl_map_from_pw_multi_aff(pma);
3121 map = isl_map_from_basic_map(bmap);
3122 assert(isl_map_is_equal(map, map2));
3123 isl_map_free(map);
3124 isl_map_free(map2);
3126 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
3127 " 8i' <= i and 8i' >= -7 + i }";
3128 set = isl_set_read_from_str(ctx, str);
3129 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
3130 set2 = isl_set_from_pw_multi_aff(pma);
3131 equal = isl_set_is_equal(set, set2);
3132 isl_set_free(set);
3133 isl_set_free(set2);
3134 if (equal < 0)
3135 return -1;
3136 if (!equal)
3137 isl_die(ctx, isl_error_unknown,
3138 "unexpected difference between set and "
3139 "piecewise affine expression", return -1);
3141 return 0;
3144 /* Inputs for isl_pw_multi_aff_max_multi_val tests.
3145 * "pma" is the input.
3146 * "res" is the expected result.
3148 static struct {
3149 const char *pma;
3150 const char *res;
3151 } opt_pw_tests[] = {
3152 { "{ [-1] -> [-1]; [1] -> [1] }", "{ [1] }" },
3153 { "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] : "
3154 "0 <= a, b <= 100 and b mod 2 = 0}", "{ [30] }" },
3155 { "[N] -> { [i,j] -> A[i, -i, i + j] : 0 <= i,j <= N <= 10 }",
3156 "{ A[10, 0, 20] }" },
3157 { "[N] -> {A[N, -N, 2N] : 0 <= N }", "{ A[infty, 0, infty] }" },
3160 /* Perform basic isl_pw_multi_aff_max_multi_val tests.
3162 static isl_stat test_pw_max(struct isl_ctx *ctx)
3164 int i;
3165 isl_pw_multi_aff *pma;
3166 isl_multi_val *mv;
3167 isl_stat r;
3169 for (i = 0; i < ARRAY_SIZE(opt_pw_tests); ++i) {
3170 pma = isl_pw_multi_aff_read_from_str(ctx, opt_pw_tests[i].pma);
3171 mv = isl_pw_multi_aff_max_multi_val(pma);
3172 r = multi_val_check_plain_equal(mv, opt_pw_tests[i].res);
3173 isl_multi_val_free(mv);
3175 if (r < 0)
3176 return isl_stat_error;
3179 return isl_stat_ok;
3182 /* A specialized isl_set_min_val test case that would return the wrong result
3183 * in earlier versions of isl.
3184 * The explicit call to isl_basic_set_union prevents the second basic set
3185 * from being determined to be empty prior to the call to isl_set_min_val,
3186 * at least at the point where this test case was introduced.
3188 static int test_min_special(isl_ctx *ctx)
3190 const char *str;
3191 isl_basic_set *bset1, *bset2;
3192 isl_set *set;
3193 isl_aff *obj;
3194 isl_val *res;
3195 int ok;
3197 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
3198 bset1 = isl_basic_set_read_from_str(ctx, str);
3199 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
3200 bset2 = isl_basic_set_read_from_str(ctx, str);
3201 set = isl_basic_set_union(bset1, bset2);
3202 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
3204 res = isl_set_min_val(set, obj);
3205 ok = isl_val_cmp_si(res, 5) == 0;
3207 isl_aff_free(obj);
3208 isl_set_free(set);
3209 isl_val_free(res);
3211 if (!res)
3212 return -1;
3213 if (!ok)
3214 isl_die(ctx, isl_error_unknown, "unexpected minimum",
3215 return -1);
3217 return 0;
3220 /* A specialized isl_set_min_val test case that would return an error
3221 * in earlier versions of isl.
3223 static int test_min_special2(isl_ctx *ctx)
3225 const char *str;
3226 isl_basic_set *bset;
3227 isl_aff *obj;
3228 isl_val *res;
3230 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
3231 bset = isl_basic_set_read_from_str(ctx, str);
3233 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
3235 res = isl_basic_set_max_val(bset, obj);
3237 isl_basic_set_free(bset);
3238 isl_aff_free(obj);
3239 isl_val_free(res);
3241 if (!res)
3242 return -1;
3244 return 0;
3247 /* Check that the result of isl_set_min_multi_pw_aff
3248 * on the union of the sets with string descriptions "s1" and "s2"
3249 * consists of a single expression (on a single cell).
3251 static isl_stat check_single_expr_min(isl_ctx *ctx, const char *s1,
3252 const char *s2)
3254 isl_size n;
3255 isl_set *set1, *set2;
3256 isl_multi_pw_aff *mpa;
3257 isl_pw_multi_aff *pma;
3259 set1 = isl_set_read_from_str(ctx, s1);
3260 set2 = isl_set_read_from_str(ctx, s2);
3261 set1 = isl_set_union(set1, set2);
3262 mpa = isl_set_min_multi_pw_aff(set1);
3263 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
3264 n = isl_pw_multi_aff_n_piece(pma);
3265 isl_pw_multi_aff_free(pma);
3267 if (n < 0)
3268 return isl_stat_error;
3269 if (n != 1)
3270 isl_die(ctx, isl_error_unknown, "expecting single expression",
3271 return isl_stat_error);
3272 return isl_stat_ok;
3275 /* A specialized isl_set_min_multi_pw_aff test that checks
3276 * that the minimum of 2N and 3N for N >= 0 is represented
3277 * by a single expression, without splitting off the special case N = 0.
3278 * Do this for both orderings.
3280 static int test_min_mpa(isl_ctx *ctx)
3282 const char *s1, *s2;
3284 s1 = "[N=0:] -> { [1, 3N:] }";
3285 s2 = "[N=0:] -> { [10, 2N:] }";
3286 if (check_single_expr_min(ctx, s1, s2) < 0)
3287 return -1;
3288 if (check_single_expr_min(ctx, s2, s1) < 0)
3289 return -1;
3291 return 0;
3294 struct {
3295 const char *set;
3296 const char *obj;
3297 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
3298 __isl_keep isl_aff *obj);
3299 const char *res;
3300 } opt_tests[] = {
3301 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
3302 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
3303 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
3304 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
3305 &isl_set_max_val, "30" },
3309 /* Perform basic isl_set_min_val and isl_set_max_val tests.
3310 * In particular, check the results on non-convex inputs.
3312 static int test_min(struct isl_ctx *ctx)
3314 int i;
3315 isl_set *set;
3316 isl_aff *obj;
3317 isl_val *val, *res;
3318 isl_bool ok;
3320 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
3321 set = isl_set_read_from_str(ctx, opt_tests[i].set);
3322 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
3323 res = isl_val_read_from_str(ctx, opt_tests[i].res);
3324 val = opt_tests[i].fn(set, obj);
3325 ok = isl_val_eq(res, val);
3326 isl_val_free(res);
3327 isl_val_free(val);
3328 isl_aff_free(obj);
3329 isl_set_free(set);
3331 if (ok < 0)
3332 return -1;
3333 if (!ok)
3334 isl_die(ctx, isl_error_unknown,
3335 "unexpected optimum", return -1);
3338 if (test_pw_max(ctx) < 0)
3339 return -1;
3340 if (test_min_special(ctx) < 0)
3341 return -1;
3342 if (test_min_special2(ctx) < 0)
3343 return -1;
3345 return 0;
3348 struct must_may {
3349 isl_map *must;
3350 isl_map *may;
3353 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
3354 void *dep_user, void *user)
3356 struct must_may *mm = (struct must_may *)user;
3358 if (must)
3359 mm->must = isl_map_union(mm->must, dep);
3360 else
3361 mm->may = isl_map_union(mm->may, dep);
3363 return isl_stat_ok;
3366 static int common_space(void *first, void *second)
3368 int depth = *(int *)first;
3369 return 2 * depth;
3372 static int map_is_equal(__isl_keep isl_map *map, const char *str)
3374 isl_map *map2;
3375 int equal;
3377 if (!map)
3378 return -1;
3380 map2 = isl_map_read_from_str(map->ctx, str);
3381 equal = isl_map_is_equal(map, map2);
3382 isl_map_free(map2);
3384 return equal;
3387 static int map_check_equal(__isl_keep isl_map *map, const char *str)
3389 int equal;
3391 equal = map_is_equal(map, str);
3392 if (equal < 0)
3393 return -1;
3394 if (!equal)
3395 isl_die(isl_map_get_ctx(map), isl_error_unknown,
3396 "result not as expected", return -1);
3397 return 0;
3400 /* Is "set" equal to the set described by "str"?
3402 static isl_bool set_is_equal(__isl_keep isl_set *set, const char *str)
3404 isl_set *set2;
3405 isl_bool equal;
3407 if (!set)
3408 return isl_bool_error;
3410 set2 = isl_set_read_from_str(isl_set_get_ctx(set), str);
3411 equal = isl_set_is_equal(set, set2);
3412 isl_set_free(set2);
3414 return equal;
3417 /* Check that "set" is equal to the set described by "str".
3419 static isl_stat set_check_equal(__isl_keep isl_set *set, const char *str)
3421 isl_bool equal;
3423 equal = set_is_equal(set, str);
3424 if (equal < 0)
3425 return isl_stat_error;
3426 if (!equal)
3427 isl_die(isl_set_get_ctx(set), isl_error_unknown,
3428 "result not as expected", return isl_stat_error);
3429 return isl_stat_ok;
3432 /* Is "uset" equal to the union set described by "str"?
3434 static isl_bool uset_is_equal(__isl_keep isl_union_set *uset, const char *str)
3436 isl_union_set *uset2;
3437 isl_bool equal;
3439 if (!uset)
3440 return isl_bool_error;
3442 uset2 = isl_union_set_read_from_str(isl_union_set_get_ctx(uset), str);
3443 equal = isl_union_set_is_equal(uset, uset2);
3444 isl_union_set_free(uset2);
3446 return equal;
3449 /* Check that "uset" is equal to the union set described by "str".
3451 static isl_stat uset_check_equal(__isl_keep isl_union_set *uset,
3452 const char *str)
3454 isl_bool equal;
3456 equal = uset_is_equal(uset, str);
3457 if (equal < 0)
3458 return isl_stat_error;
3459 if (!equal)
3460 isl_die(isl_union_set_get_ctx(uset), isl_error_unknown,
3461 "result not as expected", return isl_stat_error);
3462 return isl_stat_ok;
3465 static int test_dep(struct isl_ctx *ctx)
3467 const char *str;
3468 isl_space *space;
3469 isl_map *map;
3470 isl_access_info *ai;
3471 isl_flow *flow;
3472 int depth;
3473 struct must_may mm;
3475 depth = 3;
3477 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3478 map = isl_map_read_from_str(ctx, str);
3479 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3481 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3482 map = isl_map_read_from_str(ctx, str);
3483 ai = isl_access_info_add_source(ai, map, 1, &depth);
3485 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3486 map = isl_map_read_from_str(ctx, str);
3487 ai = isl_access_info_add_source(ai, map, 1, &depth);
3489 flow = isl_access_info_compute_flow(ai);
3490 space = isl_space_alloc(ctx, 0, 3, 3);
3491 mm.must = isl_map_empty(isl_space_copy(space));
3492 mm.may = isl_map_empty(space);
3494 isl_flow_foreach(flow, collect_must_may, &mm);
3496 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
3497 " [1,10,0] -> [2,5,0] }";
3498 assert(map_is_equal(mm.must, str));
3499 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3500 assert(map_is_equal(mm.may, str));
3502 isl_map_free(mm.must);
3503 isl_map_free(mm.may);
3504 isl_flow_free(flow);
3507 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3508 map = isl_map_read_from_str(ctx, str);
3509 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3511 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3512 map = isl_map_read_from_str(ctx, str);
3513 ai = isl_access_info_add_source(ai, map, 1, &depth);
3515 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3516 map = isl_map_read_from_str(ctx, str);
3517 ai = isl_access_info_add_source(ai, map, 0, &depth);
3519 flow = isl_access_info_compute_flow(ai);
3520 space = isl_space_alloc(ctx, 0, 3, 3);
3521 mm.must = isl_map_empty(isl_space_copy(space));
3522 mm.may = isl_map_empty(space);
3524 isl_flow_foreach(flow, collect_must_may, &mm);
3526 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
3527 assert(map_is_equal(mm.must, str));
3528 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3529 assert(map_is_equal(mm.may, str));
3531 isl_map_free(mm.must);
3532 isl_map_free(mm.may);
3533 isl_flow_free(flow);
3536 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3537 map = isl_map_read_from_str(ctx, str);
3538 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3540 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3541 map = isl_map_read_from_str(ctx, str);
3542 ai = isl_access_info_add_source(ai, map, 0, &depth);
3544 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3545 map = isl_map_read_from_str(ctx, str);
3546 ai = isl_access_info_add_source(ai, map, 0, &depth);
3548 flow = isl_access_info_compute_flow(ai);
3549 space = isl_space_alloc(ctx, 0, 3, 3);
3550 mm.must = isl_map_empty(isl_space_copy(space));
3551 mm.may = isl_map_empty(space);
3553 isl_flow_foreach(flow, collect_must_may, &mm);
3555 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
3556 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3557 assert(map_is_equal(mm.may, str));
3558 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3559 assert(map_is_equal(mm.must, str));
3561 isl_map_free(mm.must);
3562 isl_map_free(mm.may);
3563 isl_flow_free(flow);
3566 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
3567 map = isl_map_read_from_str(ctx, str);
3568 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3570 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3571 map = isl_map_read_from_str(ctx, str);
3572 ai = isl_access_info_add_source(ai, map, 0, &depth);
3574 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
3575 map = isl_map_read_from_str(ctx, str);
3576 ai = isl_access_info_add_source(ai, map, 0, &depth);
3578 flow = isl_access_info_compute_flow(ai);
3579 space = isl_space_alloc(ctx, 0, 3, 3);
3580 mm.must = isl_map_empty(isl_space_copy(space));
3581 mm.may = isl_map_empty(space);
3583 isl_flow_foreach(flow, collect_must_may, &mm);
3585 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
3586 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
3587 assert(map_is_equal(mm.may, str));
3588 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3589 assert(map_is_equal(mm.must, str));
3591 isl_map_free(mm.must);
3592 isl_map_free(mm.may);
3593 isl_flow_free(flow);
3596 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
3597 map = isl_map_read_from_str(ctx, str);
3598 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3600 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3601 map = isl_map_read_from_str(ctx, str);
3602 ai = isl_access_info_add_source(ai, map, 0, &depth);
3604 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
3605 map = isl_map_read_from_str(ctx, str);
3606 ai = isl_access_info_add_source(ai, map, 0, &depth);
3608 flow = isl_access_info_compute_flow(ai);
3609 space = isl_space_alloc(ctx, 0, 3, 3);
3610 mm.must = isl_map_empty(isl_space_copy(space));
3611 mm.may = isl_map_empty(space);
3613 isl_flow_foreach(flow, collect_must_may, &mm);
3615 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
3616 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
3617 assert(map_is_equal(mm.may, str));
3618 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3619 assert(map_is_equal(mm.must, str));
3621 isl_map_free(mm.must);
3622 isl_map_free(mm.may);
3623 isl_flow_free(flow);
3626 depth = 5;
3628 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3629 map = isl_map_read_from_str(ctx, str);
3630 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
3632 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3633 map = isl_map_read_from_str(ctx, str);
3634 ai = isl_access_info_add_source(ai, map, 1, &depth);
3636 flow = isl_access_info_compute_flow(ai);
3637 space = isl_space_alloc(ctx, 0, 5, 5);
3638 mm.must = isl_map_empty(isl_space_copy(space));
3639 mm.may = isl_map_empty(space);
3641 isl_flow_foreach(flow, collect_must_may, &mm);
3643 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
3644 assert(map_is_equal(mm.must, str));
3645 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
3646 assert(map_is_equal(mm.may, str));
3648 isl_map_free(mm.must);
3649 isl_map_free(mm.may);
3650 isl_flow_free(flow);
3652 return 0;
3655 /* Check that the dependence analysis proceeds without errors.
3656 * Earlier versions of isl would break down during the analysis
3657 * due to the use of the wrong spaces.
3659 static int test_flow(isl_ctx *ctx)
3661 const char *str;
3662 isl_union_map *access, *schedule;
3663 isl_union_map *must_dep, *may_dep;
3664 int r;
3666 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
3667 access = isl_union_map_read_from_str(ctx, str);
3668 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
3669 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
3670 "S2[] -> [1,0,0,0]; "
3671 "S3[] -> [-1,0,0,0] }";
3672 schedule = isl_union_map_read_from_str(ctx, str);
3673 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
3674 isl_union_map_copy(access), schedule,
3675 &must_dep, &may_dep, NULL, NULL);
3676 isl_union_map_free(may_dep);
3677 isl_union_map_free(must_dep);
3679 return r;
3682 struct {
3683 const char *map;
3684 int sv;
3685 } sv_tests[] = {
3686 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
3687 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
3688 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
3689 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
3690 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
3691 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
3692 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3693 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3694 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
3697 int test_sv(isl_ctx *ctx)
3699 isl_union_map *umap;
3700 int i;
3701 int sv;
3703 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
3704 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
3705 sv = isl_union_map_is_single_valued(umap);
3706 isl_union_map_free(umap);
3707 if (sv < 0)
3708 return -1;
3709 if (sv_tests[i].sv && !sv)
3710 isl_die(ctx, isl_error_internal,
3711 "map not detected as single valued", return -1);
3712 if (!sv_tests[i].sv && sv)
3713 isl_die(ctx, isl_error_internal,
3714 "map detected as single valued", return -1);
3717 return 0;
3720 struct {
3721 const char *str;
3722 int bijective;
3723 } bijective_tests[] = {
3724 { "[N,M]->{[i,j] -> [i]}", 0 },
3725 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
3726 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
3727 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
3728 { "[N,M]->{[i,j] -> [j,i]}", 1 },
3729 { "[N,M]->{[i,j] -> [i+j]}", 0 },
3730 { "[N,M]->{[i,j] -> []}", 0 },
3731 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
3732 { "[N,M]->{[i,j] -> [2i]}", 0 },
3733 { "[N,M]->{[i,j] -> [i,i]}", 0 },
3734 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
3735 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
3736 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
3739 static int test_bijective(struct isl_ctx *ctx)
3741 isl_map *map;
3742 int i;
3743 int bijective;
3745 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
3746 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
3747 bijective = isl_map_is_bijective(map);
3748 isl_map_free(map);
3749 if (bijective < 0)
3750 return -1;
3751 if (bijective_tests[i].bijective && !bijective)
3752 isl_die(ctx, isl_error_internal,
3753 "map not detected as bijective", return -1);
3754 if (!bijective_tests[i].bijective && bijective)
3755 isl_die(ctx, isl_error_internal,
3756 "map detected as bijective", return -1);
3759 return 0;
3762 /* Inputs for isl_pw_qpolynomial_gist tests.
3763 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
3765 struct {
3766 const char *pwqp;
3767 const char *set;
3768 const char *gist;
3769 } pwqp_gist_tests[] = {
3770 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
3771 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
3772 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
3773 "{ [i] -> -1/2 + 1/2 * i }" },
3774 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
3775 { "{ [i] -> i^2 : i > 0; [i] -> i^2 : i < 0 }", "{ [i] : i != 0 }",
3776 "{ [i] -> i^2 }" },
3779 /* Perform some basic isl_pw_qpolynomial_gist tests.
3781 static isl_stat test_pwqp_gist(isl_ctx *ctx)
3783 int i;
3784 const char *str;
3785 isl_set *set;
3786 isl_pw_qpolynomial *pwqp1, *pwqp2;
3787 isl_bool equal;
3789 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
3790 str = pwqp_gist_tests[i].pwqp;
3791 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3792 str = pwqp_gist_tests[i].set;
3793 set = isl_set_read_from_str(ctx, str);
3794 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
3795 str = pwqp_gist_tests[i].gist;
3796 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3797 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3798 equal = isl_pw_qpolynomial_is_zero(pwqp1);
3799 isl_pw_qpolynomial_free(pwqp1);
3801 if (equal < 0)
3802 return isl_stat_error;
3803 if (!equal)
3804 isl_die(ctx, isl_error_unknown,
3805 "unexpected result", return isl_stat_error);
3808 return isl_stat_ok;
3811 /* Perform a basic isl_pw_qpolynomial_max test.
3813 static isl_stat test_pwqp_max(isl_ctx *ctx)
3815 const char *str;
3816 isl_pw_qpolynomial *pwqp;
3817 isl_val *v;
3818 int ok;
3820 str = "{ [x=2:9, y] -> floor((x + 1)/4)^3 - floor((2x)/3)^2 }";
3821 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3822 v = isl_pw_qpolynomial_max(pwqp);
3823 ok = isl_val_cmp_si(v, -1) == 0;
3824 isl_val_free(v);
3826 if (!v)
3827 return isl_stat_error;
3828 if (!ok)
3829 isl_die(ctx, isl_error_unknown, "unexpected maximum",
3830 return isl_stat_error);
3832 return isl_stat_ok;
3835 static int test_pwqp(struct isl_ctx *ctx)
3837 const char *str;
3838 isl_set *set;
3839 isl_pw_qpolynomial *pwqp1, *pwqp2;
3840 int equal;
3842 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3843 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3845 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
3846 isl_dim_in, 1, 1);
3848 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3849 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3851 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3853 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3855 isl_pw_qpolynomial_free(pwqp1);
3857 if (test_pwqp_gist(ctx) < 0)
3858 return -1;
3860 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
3861 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3862 str = "{ [i] -> ([(2 * [i/2])/5]) }";
3863 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3865 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3867 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3869 isl_pw_qpolynomial_free(pwqp1);
3871 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3872 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3873 str = "{ [x] -> x }";
3874 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3876 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3878 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3880 isl_pw_qpolynomial_free(pwqp1);
3882 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3883 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3884 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3885 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3886 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3887 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3888 isl_pw_qpolynomial_free(pwqp1);
3890 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3891 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3892 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3893 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3894 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3895 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3896 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3897 isl_pw_qpolynomial_free(pwqp1);
3898 isl_pw_qpolynomial_free(pwqp2);
3899 if (equal < 0)
3900 return -1;
3901 if (!equal)
3902 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3904 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3905 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3906 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3907 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3908 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3909 isl_val_one(ctx));
3910 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3911 isl_pw_qpolynomial_free(pwqp1);
3912 isl_pw_qpolynomial_free(pwqp2);
3913 if (equal < 0)
3914 return -1;
3915 if (!equal)
3916 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3918 if (test_pwqp_max(ctx) < 0)
3919 return -1;
3921 return 0;
3924 static int test_split_periods(isl_ctx *ctx)
3926 const char *str;
3927 isl_pw_qpolynomial *pwqp;
3929 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3930 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3931 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3932 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3934 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3936 isl_pw_qpolynomial_free(pwqp);
3938 if (!pwqp)
3939 return -1;
3941 return 0;
3944 static int test_union(isl_ctx *ctx)
3946 const char *str;
3947 isl_union_set *uset1, *uset2;
3948 isl_union_map *umap1, *umap2;
3949 int equal;
3951 str = "{ [i] : 0 <= i <= 1 }";
3952 uset1 = isl_union_set_read_from_str(ctx, str);
3953 str = "{ [1] -> [0] }";
3954 umap1 = isl_union_map_read_from_str(ctx, str);
3956 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3957 equal = isl_union_map_is_equal(umap1, umap2);
3959 isl_union_map_free(umap1);
3960 isl_union_map_free(umap2);
3962 if (equal < 0)
3963 return -1;
3964 if (!equal)
3965 isl_die(ctx, isl_error_unknown, "union maps not equal",
3966 return -1);
3968 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3969 umap1 = isl_union_map_read_from_str(ctx, str);
3970 str = "{ A[i]; B[i] }";
3971 uset1 = isl_union_set_read_from_str(ctx, str);
3973 uset2 = isl_union_map_domain(umap1);
3975 equal = isl_union_set_is_equal(uset1, uset2);
3977 isl_union_set_free(uset1);
3978 isl_union_set_free(uset2);
3980 if (equal < 0)
3981 return -1;
3982 if (!equal)
3983 isl_die(ctx, isl_error_unknown, "union sets not equal",
3984 return -1);
3986 return 0;
3989 /* Inputs for basic isl_pw_qpolynomial_bound tests.
3990 * "type" is the type of bound that should be computed.
3991 * "poly" is a string representation of the input.
3992 * "bound" is a string representation of the expected result.
3993 * "tight" is set if the result is expected to be tight.
3995 static struct {
3996 int tight;
3997 enum isl_fold type;
3998 const char *poly;
3999 const char *bound;
4000 } bound_tests[] = {
4001 /* Check that computing a bound of a non-zero polynomial
4002 * over an unbounded domain does not produce a rational value.
4003 * In particular, check that the upper bound is infinity.
4005 { 0, isl_fold_max, "{ [m, n] -> -m * n }", "{ max(infty) }" },
4006 { 1, isl_fold_max, "{ [[a, b, c, d] -> [e]] -> 0 }",
4007 "{ [a, b, c, d] -> max(0) }" },
4008 { 1, isl_fold_max, "{ [[x] -> [x]] -> 1 : exists a : x = 2 a }",
4009 "{ [x] -> max(1) : x mod 2 = 0 }" },
4010 { 1, isl_fold_min, "{ [x=5:10] -> (x + 2)^2 }", "{ min(49) }" },
4011 { 1, isl_fold_max, "{ [0:10] -> 1 }", "{ max(1) }" },
4012 { 1, isl_fold_max, "{ [[m] -> [0:m]] -> m^2 }",
4013 "{ [m] -> max(m^2) : m >= 0 }" },
4016 /* Check that the bound computation can handle differences
4017 * in domain dimension names of the input polynomial and its domain.
4019 static isl_stat test_bound_space(isl_ctx *ctx)
4021 const char *str;
4022 isl_set *set;
4023 isl_pw_qpolynomial *pwqp;
4024 isl_pw_qpolynomial_fold *pwf;
4026 str = "{ [[c] -> [c]] }";
4027 set = isl_set_read_from_str(ctx, str);
4028 str = "{ [[a] -> [b]] -> 1 }";
4029 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
4030 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
4031 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
4032 isl_pw_qpolynomial_fold_free(pwf);
4034 return isl_stat_non_null(pwf);
4037 /* Perform basic isl_pw_qpolynomial_bound tests.
4039 static int test_bound(isl_ctx *ctx)
4041 int i;
4043 if (test_bound_space(ctx) < 0)
4044 return -1;
4046 for (i = 0; i < ARRAY_SIZE(bound_tests); ++i) {
4047 const char *str;
4048 enum isl_fold type;
4049 isl_bool equal, tight;
4050 isl_pw_qpolynomial *pwqp;
4051 isl_pw_qpolynomial_fold *pwf1, *pwf2;
4053 str = bound_tests[i].poly;
4054 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
4055 type = bound_tests[i].type;
4056 pwf1 = isl_pw_qpolynomial_bound(pwqp, type, &tight);
4057 str = bound_tests[i].bound;
4058 pwf2 = isl_pw_qpolynomial_fold_read_from_str(ctx, str);
4059 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf1, pwf2);
4060 isl_pw_qpolynomial_fold_free(pwf2);
4061 isl_pw_qpolynomial_fold_free(pwf1);
4062 if (equal < 0)
4063 return -1;
4064 if (!equal)
4065 isl_die(ctx, isl_error_unknown,
4066 "incorrect bound result", return -1);
4067 if (bound_tests[i].tight && !tight)
4068 isl_die(ctx, isl_error_unknown,
4069 "bound unexpectedly not tight", return -1);
4072 return 0;
4075 /* isl_set is defined to isl_map internally, so the corresponding elements
4076 * are isl_basic_map objects.
4078 #undef EL_BASE
4079 #undef SET_BASE
4080 #define EL_BASE basic_map
4081 #define SET_BASE set
4082 #include "isl_test_list_templ.c"
4084 #undef EL_BASE
4085 #undef SET_BASE
4086 #define EL_BASE basic_set
4087 #define SET_BASE union_set
4088 #include "isl_test_list_templ.c"
4090 #undef EL_BASE
4091 #undef SET_BASE
4092 #define EL_BASE set
4093 #define SET_BASE union_set
4094 #include "isl_test_list_templ.c"
4096 #undef EL_BASE
4097 #undef SET_BASE
4098 #define EL_BASE basic_map
4099 #define SET_BASE map
4100 #include "isl_test_list_templ.c"
4102 #undef EL_BASE
4103 #undef SET_BASE
4104 #define EL_BASE map
4105 #define SET_BASE union_map
4106 #include "isl_test_list_templ.c"
4108 /* Check that the conversion from isl objects to lists works as expected.
4110 static int test_get_list(isl_ctx *ctx)
4112 if (test_get_list_basic_map_from_set(ctx, "{ [0]; [2]; [3] }"))
4113 return -1;
4114 if (test_get_list_basic_set_from_union_set(ctx, "{ A[0]; B[2]; B[3] }"))
4115 return -1;
4116 if (test_get_list_set_from_union_set(ctx, "{ A[0]; A[2]; B[3] }"))
4117 return -1;
4118 if (test_get_list_basic_map_from_map(ctx,
4119 "{ [0] -> [0]; [2] -> [0]; [3] -> [0] }"))
4120 return -1;
4121 if (test_get_list_map_from_union_map(ctx,
4122 "{ A[0] -> [0]; A[2] -> [0]; B[3] -> [0] }"))
4123 return -1;
4125 return 0;
4128 static int test_lift(isl_ctx *ctx)
4130 const char *str;
4131 isl_basic_map *bmap;
4132 isl_basic_set *bset;
4134 str = "{ [i0] : exists e0 : i0 = 4e0 }";
4135 bset = isl_basic_set_read_from_str(ctx, str);
4136 bset = isl_basic_set_lift(bset);
4137 bmap = isl_basic_map_from_range(bset);
4138 bset = isl_basic_map_domain(bmap);
4139 isl_basic_set_free(bset);
4141 return 0;
4144 /* Check that isl_set_is_subset is not confused by identical
4145 * integer divisions.
4146 * The call to isl_set_normalize ensures that the equality constraints
4147 * a = b = 0 are discovered, turning e0 and e1 into identical
4148 * integer divisions. Any further simplification would remove
4149 * the duplicate integer divisions.
4151 static isl_stat test_subset_duplicate_integer_divisions(isl_ctx *ctx)
4153 const char *str;
4154 isl_bool is_subset;
4155 isl_set *set1, *set2;
4157 str = "{ [a, b, c, d] : "
4158 "exists (e0 = floor((a + d)/4), e1 = floor((d)/4), "
4159 "e2 = floor((-a - d + 4 *floor((a + d)/4))/10), "
4160 "e3 = floor((-d + 4*floor((d)/4))/10): "
4161 "10e2 = -a - 2c - d + 4e0 and 10e3 = -2c - d + 4e1 and "
4162 "b >= 0 and a <= 0 and b <= a) }";
4163 set1 = isl_set_read_from_str(ctx, str);
4164 set2 = isl_set_read_from_str(ctx, str);
4165 set2 = isl_set_normalize(set2);
4167 is_subset = isl_set_is_subset(set1, set2);
4169 isl_set_free(set1);
4170 isl_set_free(set2);
4172 if (is_subset < 0)
4173 return isl_stat_error;
4174 if (!is_subset)
4175 isl_die(ctx, isl_error_unknown,
4176 "set is not considered to be a subset of itself",
4177 return isl_stat_error);
4179 return isl_stat_ok;
4182 struct {
4183 const char *set1;
4184 const char *set2;
4185 int subset;
4186 } subset_tests[] = {
4187 { "{ [112, 0] }",
4188 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
4189 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
4190 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
4191 { "{ [65] }",
4192 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
4193 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
4194 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
4195 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
4196 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
4197 "256e0 <= 255i and 256e0 >= -255 + 255i and "
4198 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
4199 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
4200 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
4201 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
4202 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
4203 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
4204 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
4205 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
4206 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
4207 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
4208 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
4209 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
4210 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
4211 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
4212 "4e0 <= -57 + i0 + i1)) or "
4213 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
4214 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
4215 "4e0 >= -61 + i0 + i1)) or "
4216 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
4217 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
4220 static int test_subset(isl_ctx *ctx)
4222 int i;
4223 isl_set *set1, *set2;
4224 int subset;
4226 if (test_subset_duplicate_integer_divisions(ctx) < 0)
4227 return -1;
4229 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
4230 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
4231 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
4232 subset = isl_set_is_subset(set1, set2);
4233 isl_set_free(set1);
4234 isl_set_free(set2);
4235 if (subset < 0)
4236 return -1;
4237 if (subset != subset_tests[i].subset)
4238 isl_die(ctx, isl_error_unknown,
4239 "incorrect subset result", return -1);
4242 return 0;
4245 /* Perform a set subtraction with a set that has a non-obviously empty disjunct.
4246 * Older versions of isl would fail on such cases.
4248 static isl_stat test_subtract_empty(isl_ctx *ctx)
4250 const char *str;
4251 isl_set *s1, *s2;
4253 s1 = isl_set_read_from_str(ctx, "{ [0] }");
4254 str = "{ [a] : (exists (e0, e1, e2: 1056e1 <= 32 + a - 33e0 and "
4255 "1089e1 >= a - 33e0 and 1089e1 <= 1 + a - 33e0 and "
4256 "33e2 >= -a + 33e0 + 1056e1 and "
4257 "33e2 < -2a + 66e0 + 2112e1)) or a = 0 }";
4258 s2 = isl_set_read_from_str(ctx, str);
4259 s1 = isl_set_subtract(s1, s2);
4260 isl_set_free(s1);
4262 return isl_stat_non_null(s1);
4265 struct {
4266 const char *minuend;
4267 const char *subtrahend;
4268 const char *difference;
4269 } subtract_domain_tests[] = {
4270 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
4271 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
4272 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
4275 static int test_subtract(isl_ctx *ctx)
4277 int i;
4278 isl_union_map *umap1, *umap2;
4279 isl_union_pw_multi_aff *upma1, *upma2;
4280 isl_union_set *uset;
4281 int equal;
4283 if (test_subtract_empty(ctx) < 0)
4284 return -1;
4286 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
4287 umap1 = isl_union_map_read_from_str(ctx,
4288 subtract_domain_tests[i].minuend);
4289 uset = isl_union_set_read_from_str(ctx,
4290 subtract_domain_tests[i].subtrahend);
4291 umap2 = isl_union_map_read_from_str(ctx,
4292 subtract_domain_tests[i].difference);
4293 umap1 = isl_union_map_subtract_domain(umap1, uset);
4294 equal = isl_union_map_is_equal(umap1, umap2);
4295 isl_union_map_free(umap1);
4296 isl_union_map_free(umap2);
4297 if (equal < 0)
4298 return -1;
4299 if (!equal)
4300 isl_die(ctx, isl_error_unknown,
4301 "incorrect subtract domain result", return -1);
4304 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
4305 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4306 subtract_domain_tests[i].minuend);
4307 uset = isl_union_set_read_from_str(ctx,
4308 subtract_domain_tests[i].subtrahend);
4309 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4310 subtract_domain_tests[i].difference);
4311 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
4312 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
4313 isl_union_pw_multi_aff_free(upma1);
4314 isl_union_pw_multi_aff_free(upma2);
4315 if (equal < 0)
4316 return -1;
4317 if (!equal)
4318 isl_die(ctx, isl_error_unknown,
4319 "incorrect subtract domain result", return -1);
4322 return 0;
4325 /* Check that intersecting the empty basic set with another basic set
4326 * does not increase the number of constraints. In particular,
4327 * the empty basic set should maintain its canonical representation.
4329 static int test_intersect_1(isl_ctx *ctx)
4331 isl_size n1, n2;
4332 isl_basic_set *bset1, *bset2;
4334 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
4335 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
4336 n1 = isl_basic_set_n_constraint(bset1);
4337 bset1 = isl_basic_set_intersect(bset1, bset2);
4338 n2 = isl_basic_set_n_constraint(bset1);
4339 isl_basic_set_free(bset1);
4340 if (n1 < 0 || n2 < 0)
4341 return -1;
4342 if (n1 != n2)
4343 isl_die(ctx, isl_error_unknown,
4344 "number of constraints of empty set changed",
4345 return -1);
4347 return 0;
4350 /* Check that intersecting a set with itself does not cause
4351 * an explosion in the number of disjuncts.
4353 static isl_stat test_intersect_2(isl_ctx *ctx)
4355 int i;
4356 isl_set *set;
4358 set = isl_set_read_from_str(ctx, "{ [x,y] : x >= 0 or y >= 0 }");
4359 for (i = 0; i < 100; ++i)
4360 set = isl_set_intersect(set, isl_set_copy(set));
4361 isl_set_free(set);
4362 if (!set)
4363 return isl_stat_error;
4364 return isl_stat_ok;
4367 /* Perform some intersection tests.
4369 static int test_intersect(isl_ctx *ctx)
4371 if (test_intersect_1(ctx) < 0)
4372 return -1;
4373 if (test_intersect_2(ctx) < 0)
4374 return -1;
4376 return 0;
4379 int test_factorize(isl_ctx *ctx)
4381 const char *str;
4382 isl_basic_set *bset;
4383 isl_factorizer *f;
4385 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
4386 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
4387 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
4388 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
4389 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
4390 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
4391 "3i5 >= -2i0 - i2 + 3i4 }";
4392 bset = isl_basic_set_read_from_str(ctx, str);
4393 f = isl_basic_set_factorizer(bset);
4394 isl_basic_set_free(bset);
4395 isl_factorizer_free(f);
4396 if (!f)
4397 isl_die(ctx, isl_error_unknown,
4398 "failed to construct factorizer", return -1);
4400 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
4401 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
4402 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
4403 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
4404 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
4405 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
4406 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
4407 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
4408 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
4409 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
4410 bset = isl_basic_set_read_from_str(ctx, str);
4411 f = isl_basic_set_factorizer(bset);
4412 isl_basic_set_free(bset);
4413 isl_factorizer_free(f);
4414 if (!f)
4415 isl_die(ctx, isl_error_unknown,
4416 "failed to construct factorizer", return -1);
4418 return 0;
4421 static isl_stat check_injective(__isl_take isl_map *map, void *user)
4423 int *injective = user;
4425 *injective = isl_map_is_injective(map);
4426 isl_map_free(map);
4428 if (*injective < 0 || !*injective)
4429 return isl_stat_error;
4431 return isl_stat_ok;
4434 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
4435 const char *r, const char *s, int tilable, int parallel)
4437 int i;
4438 isl_union_set *D;
4439 isl_union_map *W, *R, *S;
4440 isl_union_map *empty;
4441 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
4442 isl_union_map *validity, *proximity, *coincidence;
4443 isl_union_map *schedule;
4444 isl_union_map *test;
4445 isl_union_set *delta;
4446 isl_union_set *domain;
4447 isl_set *delta_set;
4448 isl_set *slice;
4449 isl_set *origin;
4450 isl_schedule_constraints *sc;
4451 isl_schedule *sched;
4452 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
4453 isl_size n;
4455 D = isl_union_set_read_from_str(ctx, d);
4456 W = isl_union_map_read_from_str(ctx, w);
4457 R = isl_union_map_read_from_str(ctx, r);
4458 S = isl_union_map_read_from_str(ctx, s);
4460 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
4461 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
4463 empty = isl_union_map_empty(isl_union_map_get_space(S));
4464 isl_union_map_compute_flow(isl_union_map_copy(R),
4465 isl_union_map_copy(W), empty,
4466 isl_union_map_copy(S),
4467 &dep_raw, NULL, NULL, NULL);
4468 isl_union_map_compute_flow(isl_union_map_copy(W),
4469 isl_union_map_copy(W),
4470 isl_union_map_copy(R),
4471 isl_union_map_copy(S),
4472 &dep_waw, &dep_war, NULL, NULL);
4474 dep = isl_union_map_union(dep_waw, dep_war);
4475 dep = isl_union_map_union(dep, dep_raw);
4476 validity = isl_union_map_copy(dep);
4477 coincidence = isl_union_map_copy(dep);
4478 proximity = isl_union_map_copy(dep);
4480 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
4481 sc = isl_schedule_constraints_set_validity(sc, validity);
4482 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
4483 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4484 sched = isl_schedule_constraints_compute_schedule(sc);
4485 schedule = isl_schedule_get_map(sched);
4486 isl_schedule_free(sched);
4487 isl_union_map_free(W);
4488 isl_union_map_free(R);
4489 isl_union_map_free(S);
4491 is_injection = 1;
4492 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
4494 domain = isl_union_map_domain(isl_union_map_copy(schedule));
4495 is_complete = isl_union_set_is_subset(D, domain);
4496 isl_union_set_free(D);
4497 isl_union_set_free(domain);
4499 test = isl_union_map_reverse(isl_union_map_copy(schedule));
4500 test = isl_union_map_apply_range(test, dep);
4501 test = isl_union_map_apply_range(test, schedule);
4503 delta = isl_union_map_deltas(test);
4504 n = isl_union_set_n_set(delta);
4505 if (n < 0) {
4506 isl_union_set_free(delta);
4507 return -1;
4509 if (n == 0) {
4510 is_tilable = 1;
4511 is_parallel = 1;
4512 is_nonneg = 1;
4513 isl_union_set_free(delta);
4514 } else {
4515 isl_size dim;
4517 delta_set = isl_set_from_union_set(delta);
4519 slice = isl_set_universe(isl_set_get_space(delta_set));
4520 for (i = 0; i < tilable; ++i)
4521 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
4522 is_tilable = isl_set_is_subset(delta_set, slice);
4523 isl_set_free(slice);
4525 slice = isl_set_universe(isl_set_get_space(delta_set));
4526 for (i = 0; i < parallel; ++i)
4527 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
4528 is_parallel = isl_set_is_subset(delta_set, slice);
4529 isl_set_free(slice);
4531 origin = isl_set_universe(isl_set_get_space(delta_set));
4532 dim = isl_set_dim(origin, isl_dim_set);
4533 if (dim < 0)
4534 origin = isl_set_free(origin);
4535 for (i = 0; i < dim; ++i)
4536 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
4538 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
4539 delta_set = isl_set_lexmin(delta_set);
4541 is_nonneg = isl_set_is_equal(delta_set, origin);
4543 isl_set_free(origin);
4544 isl_set_free(delta_set);
4547 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
4548 is_injection < 0 || is_complete < 0)
4549 return -1;
4550 if (!is_complete)
4551 isl_die(ctx, isl_error_unknown,
4552 "generated schedule incomplete", return -1);
4553 if (!is_injection)
4554 isl_die(ctx, isl_error_unknown,
4555 "generated schedule not injective on each statement",
4556 return -1);
4557 if (!is_nonneg)
4558 isl_die(ctx, isl_error_unknown,
4559 "negative dependences in generated schedule",
4560 return -1);
4561 if (!is_tilable)
4562 isl_die(ctx, isl_error_unknown,
4563 "generated schedule not as tilable as expected",
4564 return -1);
4565 if (!is_parallel)
4566 isl_die(ctx, isl_error_unknown,
4567 "generated schedule not as parallel as expected",
4568 return -1);
4570 return 0;
4573 /* Compute a schedule for the given instance set, validity constraints,
4574 * proximity constraints and context and return a corresponding union map
4575 * representation.
4577 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
4578 const char *domain, const char *validity, const char *proximity,
4579 const char *context)
4581 isl_set *con;
4582 isl_union_set *dom;
4583 isl_union_map *dep;
4584 isl_union_map *prox;
4585 isl_schedule_constraints *sc;
4586 isl_schedule *schedule;
4587 isl_union_map *sched;
4589 con = isl_set_read_from_str(ctx, context);
4590 dom = isl_union_set_read_from_str(ctx, domain);
4591 dep = isl_union_map_read_from_str(ctx, validity);
4592 prox = isl_union_map_read_from_str(ctx, proximity);
4593 sc = isl_schedule_constraints_on_domain(dom);
4594 sc = isl_schedule_constraints_set_context(sc, con);
4595 sc = isl_schedule_constraints_set_validity(sc, dep);
4596 sc = isl_schedule_constraints_set_proximity(sc, prox);
4597 schedule = isl_schedule_constraints_compute_schedule(sc);
4598 sched = isl_schedule_get_map(schedule);
4599 isl_schedule_free(schedule);
4601 return sched;
4604 /* Compute a schedule for the given instance set, validity constraints and
4605 * proximity constraints and return a corresponding union map representation.
4607 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
4608 const char *domain, const char *validity, const char *proximity)
4610 return compute_schedule_with_context(ctx, domain, validity, proximity,
4611 "{ : }");
4614 /* Check that a schedule can be constructed on the given domain
4615 * with the given validity and proximity constraints.
4617 static int test_has_schedule(isl_ctx *ctx, const char *domain,
4618 const char *validity, const char *proximity)
4620 isl_union_map *sched;
4622 sched = compute_schedule(ctx, domain, validity, proximity);
4623 if (!sched)
4624 return -1;
4626 isl_union_map_free(sched);
4627 return 0;
4630 int test_special_schedule(isl_ctx *ctx, const char *domain,
4631 const char *validity, const char *proximity, const char *expected_sched)
4633 isl_union_map *sched1, *sched2;
4634 int equal;
4636 sched1 = compute_schedule(ctx, domain, validity, proximity);
4637 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
4639 equal = isl_union_map_is_equal(sched1, sched2);
4640 isl_union_map_free(sched1);
4641 isl_union_map_free(sched2);
4643 if (equal < 0)
4644 return -1;
4645 if (!equal)
4646 isl_die(ctx, isl_error_unknown, "unexpected schedule",
4647 return -1);
4649 return 0;
4652 /* Check that the schedule map is properly padded, i.e., that the range
4653 * lives in a single space.
4655 static int test_padded_schedule(isl_ctx *ctx)
4657 const char *str;
4658 isl_union_set *D;
4659 isl_union_map *validity, *proximity;
4660 isl_schedule_constraints *sc;
4661 isl_schedule *sched;
4662 isl_union_map *umap;
4663 isl_union_set *range;
4664 isl_set *set;
4666 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
4667 D = isl_union_set_read_from_str(ctx, str);
4668 validity = isl_union_map_empty(isl_union_set_get_space(D));
4669 proximity = isl_union_map_copy(validity);
4670 sc = isl_schedule_constraints_on_domain(D);
4671 sc = isl_schedule_constraints_set_validity(sc, validity);
4672 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4673 sched = isl_schedule_constraints_compute_schedule(sc);
4674 umap = isl_schedule_get_map(sched);
4675 isl_schedule_free(sched);
4676 range = isl_union_map_range(umap);
4677 set = isl_set_from_union_set(range);
4678 isl_set_free(set);
4680 if (!set)
4681 return -1;
4683 return 0;
4686 /* Check that conditional validity constraints are also taken into
4687 * account across bands.
4688 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
4689 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
4690 * and then check that the adjacent order constraint C[2,1]->D[2,0]
4691 * is enforced by the rest of the schedule.
4693 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
4695 const char *str;
4696 isl_union_set *domain;
4697 isl_union_map *validity, *proximity, *condition;
4698 isl_union_map *sink, *source, *dep;
4699 isl_schedule_constraints *sc;
4700 isl_schedule *schedule;
4701 isl_union_access_info *access;
4702 isl_union_flow *flow;
4703 int empty;
4705 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4706 "A[k] : k >= 1 and k <= -1 + n; "
4707 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4708 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
4709 domain = isl_union_set_read_from_str(ctx, str);
4710 sc = isl_schedule_constraints_on_domain(domain);
4711 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
4712 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4713 "D[k, i] -> C[1 + k, i] : "
4714 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4715 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
4716 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
4717 validity = isl_union_map_read_from_str(ctx, str);
4718 sc = isl_schedule_constraints_set_validity(sc, validity);
4719 str = "[n] -> { C[k, i] -> D[k, i] : "
4720 "0 <= i <= -1 + k and k <= -1 + n }";
4721 proximity = isl_union_map_read_from_str(ctx, str);
4722 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4723 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
4724 "i <= -1 + k and i >= 1 and k <= -2 + n; "
4725 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
4726 "k <= -1 + n and i >= 0 and i <= -2 + k }";
4727 condition = isl_union_map_read_from_str(ctx, str);
4728 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
4729 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4730 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
4731 "i >= 0 and i <= -1 + k and k <= -1 + n and "
4732 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
4733 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
4734 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4735 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
4736 "k <= -1 + n and i >= 0 and i <= -1 + k and "
4737 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
4738 validity = isl_union_map_read_from_str(ctx, str);
4739 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4740 validity);
4741 schedule = isl_schedule_constraints_compute_schedule(sc);
4742 str = "{ D[2,0] -> [] }";
4743 sink = isl_union_map_read_from_str(ctx, str);
4744 access = isl_union_access_info_from_sink(sink);
4745 str = "{ C[2,1] -> [] }";
4746 source = isl_union_map_read_from_str(ctx, str);
4747 access = isl_union_access_info_set_must_source(access, source);
4748 access = isl_union_access_info_set_schedule(access, schedule);
4749 flow = isl_union_access_info_compute_flow(access);
4750 dep = isl_union_flow_get_must_dependence(flow);
4751 isl_union_flow_free(flow);
4752 empty = isl_union_map_is_empty(dep);
4753 isl_union_map_free(dep);
4755 if (empty < 0)
4756 return -1;
4757 if (empty)
4758 isl_die(ctx, isl_error_unknown,
4759 "conditional validity not respected", return -1);
4761 return 0;
4764 /* Check that the test for violated conditional validity constraints
4765 * is not confused by domain compression.
4766 * In particular, earlier versions of isl would apply
4767 * a schedule on the compressed domains to the original domains,
4768 * resulting in a failure to detect that the default schedule
4769 * violates the conditional validity constraints.
4771 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
4773 const char *str;
4774 isl_bool empty;
4775 isl_union_set *domain;
4776 isl_union_map *validity, *condition;
4777 isl_schedule_constraints *sc;
4778 isl_schedule *schedule;
4779 isl_union_map *umap;
4780 isl_map *map, *ge;
4782 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
4783 domain = isl_union_set_read_from_str(ctx, str);
4784 sc = isl_schedule_constraints_on_domain(domain);
4785 str = "{ B[1, i] -> A[0, i + 1] }";
4786 condition = isl_union_map_read_from_str(ctx, str);
4787 str = "{ A[0, i] -> B[1, i - 1] }";
4788 validity = isl_union_map_read_from_str(ctx, str);
4789 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4790 isl_union_map_copy(validity));
4791 schedule = isl_schedule_constraints_compute_schedule(sc);
4792 umap = isl_schedule_get_map(schedule);
4793 isl_schedule_free(schedule);
4794 validity = isl_union_map_apply_domain(validity,
4795 isl_union_map_copy(umap));
4796 validity = isl_union_map_apply_range(validity, umap);
4797 map = isl_map_from_union_map(validity);
4798 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4799 map = isl_map_intersect(map, ge);
4800 empty = isl_map_is_empty(map);
4801 isl_map_free(map);
4803 if (empty < 0)
4804 return -1;
4805 if (!empty)
4806 isl_die(ctx, isl_error_unknown,
4807 "conditional validity constraints not satisfied",
4808 return -1);
4810 return 0;
4813 /* Input for testing of schedule construction based on
4814 * conditional constraints.
4816 * domain is the iteration domain
4817 * flow are the flow dependences, which determine the validity and
4818 * proximity constraints
4819 * condition are the conditions on the conditional validity constraints
4820 * conditional_validity are the conditional validity constraints
4821 * outer_band_n is the expected number of members in the outer band
4823 struct {
4824 const char *domain;
4825 const char *flow;
4826 const char *condition;
4827 const char *conditional_validity;
4828 int outer_band_n;
4829 } live_range_tests[] = {
4830 /* Contrived example that illustrates that we need to keep
4831 * track of tagged condition dependences and
4832 * tagged conditional validity dependences
4833 * in isl_sched_edge separately.
4834 * In particular, the conditional validity constraints on A
4835 * cannot be satisfied,
4836 * but they can be ignored because there are no corresponding
4837 * condition constraints. However, we do have an additional
4838 * conditional validity constraint that maps to the same
4839 * dependence relation
4840 * as the condition constraint on B. If we did not make a distinction
4841 * between tagged condition and tagged conditional validity
4842 * dependences, then we
4843 * could end up treating this shared dependence as an condition
4844 * constraint on A, forcing a localization of the conditions,
4845 * which is impossible.
4847 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
4848 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
4849 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
4850 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4851 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4852 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
4855 /* TACO 2013 Fig. 7 */
4856 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4857 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4858 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4859 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
4860 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
4861 "0 <= i < n and 0 <= j < n - 1 }",
4862 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
4863 "0 <= i < n and 0 <= j < j' < n;"
4864 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
4865 "0 <= i < i' < n and 0 <= j,j' < n;"
4866 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
4867 "0 <= i,j,j' < n and j < j' }",
4870 /* TACO 2013 Fig. 7, without tags */
4871 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4872 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4873 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4874 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4875 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4876 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
4877 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
4878 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
4881 /* TACO 2013 Fig. 12 */
4882 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
4883 "S3[i,3] : 0 <= i <= 1 }",
4884 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
4885 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
4886 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
4887 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
4888 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4889 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
4890 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4891 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
4892 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
4893 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
4894 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
4899 /* Test schedule construction based on conditional constraints.
4900 * In particular, check the number of members in the outer band node
4901 * as an indication of whether tiling is possible or not.
4903 static int test_conditional_schedule_constraints(isl_ctx *ctx)
4905 int i;
4906 isl_union_set *domain;
4907 isl_union_map *condition;
4908 isl_union_map *flow;
4909 isl_union_map *validity;
4910 isl_schedule_constraints *sc;
4911 isl_schedule *schedule;
4912 isl_schedule_node *node;
4913 isl_size n_member;
4915 if (test_special_conditional_schedule_constraints(ctx) < 0)
4916 return -1;
4917 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
4918 return -1;
4920 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
4921 domain = isl_union_set_read_from_str(ctx,
4922 live_range_tests[i].domain);
4923 flow = isl_union_map_read_from_str(ctx,
4924 live_range_tests[i].flow);
4925 condition = isl_union_map_read_from_str(ctx,
4926 live_range_tests[i].condition);
4927 validity = isl_union_map_read_from_str(ctx,
4928 live_range_tests[i].conditional_validity);
4929 sc = isl_schedule_constraints_on_domain(domain);
4930 sc = isl_schedule_constraints_set_validity(sc,
4931 isl_union_map_copy(flow));
4932 sc = isl_schedule_constraints_set_proximity(sc, flow);
4933 sc = isl_schedule_constraints_set_conditional_validity(sc,
4934 condition, validity);
4935 schedule = isl_schedule_constraints_compute_schedule(sc);
4936 node = isl_schedule_get_root(schedule);
4937 while (node &&
4938 isl_schedule_node_get_type(node) != isl_schedule_node_band)
4939 node = isl_schedule_node_first_child(node);
4940 n_member = isl_schedule_node_band_n_member(node);
4941 isl_schedule_node_free(node);
4942 isl_schedule_free(schedule);
4944 if (!schedule || n_member < 0)
4945 return -1;
4946 if (n_member != live_range_tests[i].outer_band_n)
4947 isl_die(ctx, isl_error_unknown,
4948 "unexpected number of members in outer band",
4949 return -1);
4951 return 0;
4954 /* Check that the schedule computed for the given instance set and
4955 * dependence relation strongly satisfies the dependences.
4956 * In particular, check that no instance is scheduled before
4957 * or together with an instance on which it depends.
4958 * Earlier versions of isl would produce a schedule that
4959 * only weakly satisfies the dependences.
4961 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
4963 const char *domain, *dep;
4964 isl_union_map *D, *schedule;
4965 isl_map *map, *ge;
4966 int empty;
4968 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
4969 "A[i0] : 0 <= i0 <= 1 }";
4970 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
4971 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
4972 schedule = compute_schedule(ctx, domain, dep, dep);
4973 D = isl_union_map_read_from_str(ctx, dep);
4974 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
4975 D = isl_union_map_apply_range(D, schedule);
4976 map = isl_map_from_union_map(D);
4977 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4978 map = isl_map_intersect(map, ge);
4979 empty = isl_map_is_empty(map);
4980 isl_map_free(map);
4982 if (empty < 0)
4983 return -1;
4984 if (!empty)
4985 isl_die(ctx, isl_error_unknown,
4986 "dependences not strongly satisfied", return -1);
4988 return 0;
4991 /* Compute a schedule for input where the instance set constraints
4992 * conflict with the context constraints.
4993 * Earlier versions of isl did not properly handle this situation.
4995 static int test_conflicting_context_schedule(isl_ctx *ctx)
4997 isl_union_map *schedule;
4998 const char *domain, *context;
5000 domain = "[n] -> { A[] : n >= 0 }";
5001 context = "[n] -> { : n < 0 }";
5002 schedule = compute_schedule_with_context(ctx,
5003 domain, "{}", "{}", context);
5004 isl_union_map_free(schedule);
5006 if (!schedule)
5007 return -1;
5009 return 0;
5012 /* Check that a set of schedule constraints that only allow for
5013 * a coalescing schedule still produces a schedule even if the user
5014 * request a non-coalescing schedule. Earlier versions of isl
5015 * would not handle this case correctly.
5017 static int test_coalescing_schedule(isl_ctx *ctx)
5019 const char *domain, *dep;
5020 isl_union_set *I;
5021 isl_union_map *D;
5022 isl_schedule_constraints *sc;
5023 isl_schedule *schedule;
5024 int treat_coalescing;
5026 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
5027 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
5028 I = isl_union_set_read_from_str(ctx, domain);
5029 D = isl_union_map_read_from_str(ctx, dep);
5030 sc = isl_schedule_constraints_on_domain(I);
5031 sc = isl_schedule_constraints_set_validity(sc, D);
5032 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
5033 isl_options_set_schedule_treat_coalescing(ctx, 1);
5034 schedule = isl_schedule_constraints_compute_schedule(sc);
5035 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
5036 isl_schedule_free(schedule);
5037 if (!schedule)
5038 return -1;
5039 return 0;
5042 /* Check that the scheduler does not perform any needless
5043 * compound skewing. Earlier versions of isl would compute
5044 * schedules in terms of transformed schedule coefficients and
5045 * would not accurately keep track of the sum of the original
5046 * schedule coefficients. It could then produce the schedule
5047 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
5048 * for the input below instead of the schedule below.
5050 static int test_skewing_schedule(isl_ctx *ctx)
5052 const char *D, *V, *P, *S;
5054 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
5055 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
5056 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
5057 "-1 <= a-i + b-j + c-k <= 1 }";
5058 P = "{ }";
5059 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
5061 return test_special_schedule(ctx, D, V, P, S);
5064 int test_schedule(isl_ctx *ctx)
5066 const char *D, *W, *R, *V, *P, *S;
5067 int max_coincidence;
5068 int treat_coalescing;
5070 /* Handle resulting schedule with zero bands. */
5071 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
5072 return -1;
5074 /* Jacobi */
5075 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
5076 W = "{ S1[t,i] -> a[t,i] }";
5077 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
5078 "S1[t,i] -> a[t-1,i+1] }";
5079 S = "{ S1[t,i] -> [t,i] }";
5080 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
5081 return -1;
5083 /* Fig. 5 of CC2008 */
5084 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
5085 "j <= -1 + N }";
5086 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
5087 "j >= 2 and j <= -1 + N }";
5088 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
5089 "j >= 2 and j <= -1 + N; "
5090 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
5091 "j >= 2 and j <= -1 + N }";
5092 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
5093 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
5094 return -1;
5096 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
5097 W = "{ S1[i] -> a[i] }";
5098 R = "{ S2[i] -> a[i+1] }";
5099 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
5100 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
5101 return -1;
5103 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
5104 W = "{ S1[i] -> a[i] }";
5105 R = "{ S2[i] -> a[9-i] }";
5106 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
5107 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
5108 return -1;
5110 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
5111 W = "{ S1[i] -> a[i] }";
5112 R = "[N] -> { S2[i] -> a[N-1-i] }";
5113 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
5114 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
5115 return -1;
5117 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
5118 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
5119 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
5120 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
5121 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5122 return -1;
5124 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
5125 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
5126 R = "{ S2[i,j] -> a[i-1,j] }";
5127 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
5128 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
5129 return -1;
5131 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
5132 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
5133 R = "{ S2[i,j] -> a[i,j-1] }";
5134 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
5135 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
5136 return -1;
5138 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
5139 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
5140 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
5141 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
5142 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
5143 "S_0[] -> [0, 0, 0] }";
5144 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
5145 return -1;
5146 ctx->opt->schedule_parametric = 0;
5147 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5148 return -1;
5149 ctx->opt->schedule_parametric = 1;
5151 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
5152 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
5153 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
5154 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
5155 "S4[i] -> a[i,N] }";
5156 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
5157 "S4[i] -> [4,i,0] }";
5158 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
5159 isl_options_set_schedule_maximize_coincidence(ctx, 0);
5160 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
5161 return -1;
5162 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
5164 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
5165 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
5166 "j <= N }";
5167 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
5168 "j <= N; "
5169 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
5170 "j <= N }";
5171 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
5172 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5173 return -1;
5175 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
5176 " S_2[t] : t >= 0 and t <= -1 + N; "
5177 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
5178 "i <= -1 + N }";
5179 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
5180 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
5181 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
5182 "i >= 0 and i <= -1 + N }";
5183 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
5184 "i >= 0 and i <= -1 + N; "
5185 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
5186 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
5187 " S_0[t] -> [0, t, 0] }";
5189 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
5190 return -1;
5191 ctx->opt->schedule_parametric = 0;
5192 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5193 return -1;
5194 ctx->opt->schedule_parametric = 1;
5196 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
5197 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
5198 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
5199 return -1;
5201 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
5202 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
5203 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
5204 "j >= 0 and j <= -1 + N; "
5205 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
5206 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
5207 "j >= 0 and j <= -1 + N; "
5208 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
5209 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
5210 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5211 return -1;
5213 D = "{ S_0[i] : i >= 0 }";
5214 W = "{ S_0[i] -> a[i] : i >= 0 }";
5215 R = "{ S_0[i] -> a[0] : i >= 0 }";
5216 S = "{ S_0[i] -> [0, i, 0] }";
5217 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5218 return -1;
5220 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
5221 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
5222 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
5223 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
5224 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5225 return -1;
5227 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
5228 "k <= -1 + n and k >= 0 }";
5229 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
5230 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
5231 "k <= -1 + n and k >= 0; "
5232 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
5233 "k <= -1 + n and k >= 0; "
5234 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
5235 "k <= -1 + n and k >= 0 }";
5236 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
5237 ctx->opt->schedule_outer_coincidence = 1;
5238 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5239 return -1;
5240 ctx->opt->schedule_outer_coincidence = 0;
5242 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
5243 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
5244 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
5245 "Stmt_for_body24[i0, i1, 1, 0]:"
5246 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
5247 "Stmt_for_body7[i0, i1, i2]:"
5248 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
5249 "i2 <= 7 }";
5251 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
5252 "Stmt_for_body24[1, i1, i2, i3]:"
5253 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
5254 "i2 >= 1;"
5255 "Stmt_for_body24[0, i1, i2, i3] -> "
5256 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
5257 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
5258 "i3 >= 0;"
5259 "Stmt_for_body24[0, i1, i2, i3] ->"
5260 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
5261 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
5262 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
5263 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
5264 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
5265 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
5266 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
5267 "i1 <= 6 and i1 >= 0;"
5268 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
5269 "Stmt_for_body7[i0, i1, i2] -> "
5270 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
5271 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
5272 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
5273 "Stmt_for_body7[i0, i1, i2] -> "
5274 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
5275 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
5276 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
5277 P = V;
5279 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
5280 isl_options_set_schedule_treat_coalescing(ctx, 0);
5281 if (test_has_schedule(ctx, D, V, P) < 0)
5282 return -1;
5283 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
5285 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
5286 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
5287 "j >= 1 and j <= 7;"
5288 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
5289 "j >= 1 and j <= 8 }";
5290 P = "{ }";
5291 S = "{ S_0[i, j] -> [i + j, i] }";
5292 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5293 if (test_special_schedule(ctx, D, V, P, S) < 0)
5294 return -1;
5295 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5297 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
5298 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
5299 "j >= 0 and j <= -1 + i }";
5300 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
5301 "i <= -1 + N and j >= 0;"
5302 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
5303 "i <= -2 + N }";
5304 P = "{ }";
5305 S = "{ S_0[i, j] -> [i, j] }";
5306 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5307 if (test_special_schedule(ctx, D, V, P, S) < 0)
5308 return -1;
5309 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5311 /* Test both algorithms on a case with only proximity dependences. */
5312 D = "{ S[i,j] : 0 <= i <= 10 }";
5313 V = "{ }";
5314 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
5315 S = "{ S[i, j] -> [j, i] }";
5316 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5317 if (test_special_schedule(ctx, D, V, P, S) < 0)
5318 return -1;
5319 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5320 if (test_special_schedule(ctx, D, V, P, S) < 0)
5321 return -1;
5323 D = "{ A[a]; B[] }";
5324 V = "{}";
5325 P = "{ A[a] -> B[] }";
5326 if (test_has_schedule(ctx, D, V, P) < 0)
5327 return -1;
5329 if (test_padded_schedule(ctx) < 0)
5330 return -1;
5332 /* Check that check for progress is not confused by rational
5333 * solution.
5335 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
5336 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
5337 "i0 <= -2 + N; "
5338 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
5339 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
5340 P = "{}";
5341 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5342 if (test_has_schedule(ctx, D, V, P) < 0)
5343 return -1;
5344 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5346 /* Check that we allow schedule rows that are only non-trivial
5347 * on some full-dimensional domains.
5349 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
5350 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
5351 "S1[j] -> S2[1] : 0 <= j <= 1 }";
5352 P = "{}";
5353 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5354 if (test_has_schedule(ctx, D, V, P) < 0)
5355 return -1;
5356 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5358 if (test_conditional_schedule_constraints(ctx) < 0)
5359 return -1;
5361 if (test_strongly_satisfying_schedule(ctx) < 0)
5362 return -1;
5364 if (test_conflicting_context_schedule(ctx) < 0)
5365 return -1;
5367 if (test_coalescing_schedule(ctx) < 0)
5368 return -1;
5369 if (test_skewing_schedule(ctx) < 0)
5370 return -1;
5372 return 0;
5375 /* Perform scheduling tests using the whole component scheduler.
5377 static int test_schedule_whole(isl_ctx *ctx)
5379 int whole;
5380 int r;
5382 whole = isl_options_get_schedule_whole_component(ctx);
5383 isl_options_set_schedule_whole_component(ctx, 1);
5384 r = test_schedule(ctx);
5385 isl_options_set_schedule_whole_component(ctx, whole);
5387 return r;
5390 /* Perform scheduling tests using the incremental scheduler.
5392 static int test_schedule_incremental(isl_ctx *ctx)
5394 int whole;
5395 int r;
5397 whole = isl_options_get_schedule_whole_component(ctx);
5398 isl_options_set_schedule_whole_component(ctx, 0);
5399 r = test_schedule(ctx);
5400 isl_options_set_schedule_whole_component(ctx, whole);
5402 return r;
5405 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
5407 isl_union_map *umap;
5408 int test;
5410 umap = isl_union_map_read_from_str(ctx, str);
5411 test = isl_union_map_plain_is_injective(umap);
5412 isl_union_map_free(umap);
5413 if (test < 0)
5414 return -1;
5415 if (test == injective)
5416 return 0;
5417 if (injective)
5418 isl_die(ctx, isl_error_unknown,
5419 "map not detected as injective", return -1);
5420 else
5421 isl_die(ctx, isl_error_unknown,
5422 "map detected as injective", return -1);
5425 int test_injective(isl_ctx *ctx)
5427 const char *str;
5429 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
5430 return -1;
5431 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
5432 return -1;
5433 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
5434 return -1;
5435 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
5436 return -1;
5437 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
5438 return -1;
5439 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
5440 return -1;
5441 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
5442 return -1;
5443 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
5444 return -1;
5446 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
5447 if (test_plain_injective(ctx, str, 1))
5448 return -1;
5449 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
5450 if (test_plain_injective(ctx, str, 0))
5451 return -1;
5453 return 0;
5456 #undef BASE
5457 #define BASE aff
5458 #include "isl_test_plain_equal_templ.c"
5460 #undef BASE
5461 #define BASE pw_multi_aff
5462 #include "isl_test_plain_equal_templ.c"
5464 #undef BASE
5465 #define BASE union_pw_aff
5466 #include "isl_test_plain_equal_templ.c"
5468 /* Basic tests on isl_union_pw_aff.
5470 * In particular, check that isl_union_pw_aff_aff_on_domain
5471 * aligns the parameters of the input objects and
5472 * that isl_union_pw_aff_param_on_domain_id properly
5473 * introduces the parameter.
5475 static int test_upa(isl_ctx *ctx)
5477 const char *str;
5478 isl_id *id;
5479 isl_aff *aff;
5480 isl_union_set *domain;
5481 isl_union_pw_aff *upa;
5482 isl_stat ok;
5484 aff = isl_aff_read_from_str(ctx, "[N] -> { [N] }");
5485 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5486 domain = isl_union_set_read_from_str(ctx, str);
5487 upa = isl_union_pw_aff_aff_on_domain(domain, aff);
5488 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5489 ok = union_pw_aff_check_plain_equal(upa, str);
5490 isl_union_pw_aff_free(upa);
5491 if (ok < 0)
5492 return -1;
5494 id = isl_id_alloc(ctx, "N", NULL);
5495 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5496 domain = isl_union_set_read_from_str(ctx, str);
5497 upa = isl_union_pw_aff_param_on_domain_id(domain, id);
5498 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5499 ok = union_pw_aff_check_plain_equal(upa, str);
5500 isl_union_pw_aff_free(upa);
5501 if (ok < 0)
5502 return -1;
5504 return 0;
5507 struct {
5508 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5509 __isl_take isl_aff *aff2);
5510 } aff_bin_op[] = {
5511 ['+'] = { &isl_aff_add },
5512 ['-'] = { &isl_aff_sub },
5513 ['*'] = { &isl_aff_mul },
5514 ['/'] = { &isl_aff_div },
5517 struct {
5518 const char *arg1;
5519 unsigned char op;
5520 const char *arg2;
5521 const char *res;
5522 } aff_bin_tests[] = {
5523 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
5524 "{ [i] -> [2i] }" },
5525 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
5526 "{ [i] -> [0] }" },
5527 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
5528 "{ [i] -> [2i] }" },
5529 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
5530 "{ [i] -> [2i] }" },
5531 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
5532 "{ [i] -> [i/2] }" },
5533 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
5534 "{ [i] -> [i] }" },
5535 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
5536 "{ [i] -> [NaN] }" },
5537 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
5538 "{ [i] -> [NaN] }" },
5539 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
5540 "{ [i] -> [NaN] }" },
5541 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
5542 "{ [i] -> [NaN] }" },
5543 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
5544 "{ [i] -> [NaN] }" },
5545 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
5546 "{ [i] -> [NaN] }" },
5547 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
5548 "{ [i] -> [NaN] }" },
5549 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
5550 "{ [i] -> [NaN] }" },
5551 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
5552 "{ [i] -> [NaN] }" },
5553 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
5554 "{ [i] -> [NaN] }" },
5555 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
5556 "{ [i] -> [NaN] }" },
5557 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
5558 "{ [i] -> [NaN] }" },
5559 { "{ [i] -> [i] }", '/', "{ [i] -> [0] }",
5560 "{ [i] -> [NaN] }" },
5563 /* Perform some basic tests of binary operations on isl_aff objects.
5565 static int test_bin_aff(isl_ctx *ctx)
5567 int i;
5568 isl_aff *aff1, *aff2, *res;
5569 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5570 __isl_take isl_aff *aff2);
5571 int ok;
5573 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
5574 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
5575 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
5576 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
5577 fn = aff_bin_op[aff_bin_tests[i].op].fn;
5578 aff1 = fn(aff1, aff2);
5579 if (isl_aff_is_nan(res))
5580 ok = isl_aff_is_nan(aff1);
5581 else
5582 ok = isl_aff_plain_is_equal(aff1, res);
5583 isl_aff_free(aff1);
5584 isl_aff_free(res);
5585 if (ok < 0)
5586 return -1;
5587 if (!ok)
5588 isl_die(ctx, isl_error_unknown,
5589 "unexpected result", return -1);
5592 return 0;
5595 struct {
5596 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
5597 __isl_take isl_pw_aff *pa2);
5598 } pw_aff_bin_op[] = {
5599 ['m'] = { &isl_pw_aff_min },
5600 ['M'] = { &isl_pw_aff_max },
5603 /* Inputs for binary isl_pw_aff operation tests.
5604 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
5605 * defined by pw_aff_bin_op, and "res" is the expected result.
5607 struct {
5608 const char *arg1;
5609 unsigned char op;
5610 const char *arg2;
5611 const char *res;
5612 } pw_aff_bin_tests[] = {
5613 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
5614 "{ [i] -> [i] }" },
5615 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
5616 "{ [i] -> [i] }" },
5617 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
5618 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
5619 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
5620 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
5621 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
5622 "{ [i] -> [NaN] }" },
5623 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
5624 "{ [i] -> [NaN] }" },
5627 /* Perform some basic tests of binary operations on isl_pw_aff objects.
5629 static int test_bin_pw_aff(isl_ctx *ctx)
5631 int i;
5632 isl_bool ok;
5633 isl_pw_aff *pa1, *pa2, *res;
5635 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
5636 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
5637 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
5638 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
5639 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
5640 if (isl_pw_aff_involves_nan(res))
5641 ok = isl_pw_aff_involves_nan(pa1);
5642 else
5643 ok = isl_pw_aff_plain_is_equal(pa1, res);
5644 isl_pw_aff_free(pa1);
5645 isl_pw_aff_free(res);
5646 if (ok < 0)
5647 return -1;
5648 if (!ok)
5649 isl_die(ctx, isl_error_unknown,
5650 "unexpected result", return -1);
5653 return 0;
5656 /* Inputs for basic tests of test operations on
5657 * isl_union_pw_multi_aff objects.
5658 * "fn" is the function that is being tested.
5659 * "arg" is a string description of the input.
5660 * "res" is the expected result.
5662 static struct {
5663 isl_bool (*fn)(__isl_keep isl_union_pw_multi_aff *upma1);
5664 const char *arg;
5665 isl_bool res;
5666 } upma_test_tests[] = {
5667 { &isl_union_pw_multi_aff_involves_nan, "{ A[] -> [0]; B[0] -> [1] }",
5668 isl_bool_false },
5669 { &isl_union_pw_multi_aff_involves_nan, "{ A[] -> [NaN]; B[0] -> [1] }",
5670 isl_bool_true },
5671 { &isl_union_pw_multi_aff_involves_nan, "{ A[] -> [0]; B[0] -> [NaN] }",
5672 isl_bool_true },
5673 { &isl_union_pw_multi_aff_involves_nan,
5674 "{ A[] -> [0]; B[0] -> [1, NaN, 5] }",
5675 isl_bool_true },
5676 { &isl_union_pw_multi_aff_involves_locals,
5677 "{ A[] -> [0]; B[0] -> [1] }",
5678 isl_bool_false },
5679 { &isl_union_pw_multi_aff_involves_locals,
5680 "{ A[] -> [0]; B[x] -> [1] : x mod 2 = 0 }",
5681 isl_bool_true },
5682 { &isl_union_pw_multi_aff_involves_locals,
5683 "{ A[] -> [0]; B[x] -> [x // 2] }",
5684 isl_bool_true },
5685 { &isl_union_pw_multi_aff_involves_locals,
5686 "{ A[i] -> [i // 2]; B[0] -> [1] }",
5687 isl_bool_true },
5690 /* Perform some basic tests of test operations on
5691 * isl_union_pw_multi_aff objects.
5693 static isl_stat test_upma_test(isl_ctx *ctx)
5695 int i;
5696 isl_union_pw_multi_aff *upma;
5697 isl_bool res;
5699 for (i = 0; i < ARRAY_SIZE(upma_test_tests); ++i) {
5700 const char *str;
5702 str = upma_test_tests[i].arg;
5703 upma = isl_union_pw_multi_aff_read_from_str(ctx, str);
5704 res = upma_test_tests[i].fn(upma);
5705 isl_union_pw_multi_aff_free(upma);
5706 if (res < 0)
5707 return isl_stat_error;
5708 if (res != upma_test_tests[i].res)
5709 isl_die(ctx, isl_error_unknown,
5710 "unexpected result", return isl_stat_error);
5713 return isl_stat_ok;
5716 struct {
5717 __isl_give isl_union_pw_multi_aff *(*fn)(
5718 __isl_take isl_union_pw_multi_aff *upma1,
5719 __isl_take isl_union_pw_multi_aff *upma2);
5720 const char *arg1;
5721 const char *arg2;
5722 const char *res;
5723 } upma_bin_tests[] = {
5724 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
5725 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
5726 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
5727 "{ B[x] -> [2] : x >= 0 }",
5728 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
5729 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
5730 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
5731 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
5732 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
5733 "D[i] -> B[2] : i >= 5 }" },
5734 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5735 "{ B[x] -> C[2] : x > 0 }",
5736 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
5737 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5738 "{ B[x] -> A[2] : x >= 0 }",
5739 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
5741 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5742 "{ B[x] -> C[x + 2] }",
5743 "{ D[y] -> B[2y] }",
5744 "{ }" },
5746 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5747 "{ [A[x] -> B[x + 1]] -> C[x + 2] }",
5748 "{ D[y] -> B[2y] }",
5749 "{ }" },
5751 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5752 "{ [A[x] -> B[x + 1]] -> C[x + 2]; B[x] -> C[x + 2] }",
5753 "{ D[y] -> A[2y] }",
5754 "{ [D[y] -> B[2y + 1]] -> C[2y + 2] }" },
5756 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5757 "{ T[A[x] -> B[x + 1]] -> C[x + 2]; B[x] -> C[x + 2] }",
5758 "{ D[y] -> A[2y] }",
5759 "{ T[D[y] -> B[2y + 1]] -> C[2y + 2] }" },
5762 /* Perform some basic tests of binary operations on
5763 * isl_union_pw_multi_aff objects.
5765 static int test_bin_upma(isl_ctx *ctx)
5767 int i;
5768 isl_union_pw_multi_aff *upma1, *upma2, *res;
5769 int ok;
5771 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
5772 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5773 upma_bin_tests[i].arg1);
5774 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5775 upma_bin_tests[i].arg2);
5776 res = isl_union_pw_multi_aff_read_from_str(ctx,
5777 upma_bin_tests[i].res);
5778 upma1 = upma_bin_tests[i].fn(upma1, upma2);
5779 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
5780 isl_union_pw_multi_aff_free(upma1);
5781 isl_union_pw_multi_aff_free(res);
5782 if (ok < 0)
5783 return -1;
5784 if (!ok)
5785 isl_die(ctx, isl_error_unknown,
5786 "unexpected result", return -1);
5789 return 0;
5792 struct {
5793 __isl_give isl_union_pw_multi_aff *(*fn)(
5794 __isl_take isl_union_pw_multi_aff *upma1,
5795 __isl_take isl_union_pw_multi_aff *upma2);
5796 const char *arg1;
5797 const char *arg2;
5798 } upma_bin_fail_tests[] = {
5799 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5800 "{ B[x] -> C[2] : x >= 0 }" },
5803 /* Perform some basic tests of binary operations on
5804 * isl_union_pw_multi_aff objects that are expected to fail.
5806 static int test_bin_upma_fail(isl_ctx *ctx)
5808 int i, n;
5809 isl_union_pw_multi_aff *upma1, *upma2;
5810 int on_error;
5812 on_error = isl_options_get_on_error(ctx);
5813 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
5814 n = ARRAY_SIZE(upma_bin_fail_tests);
5815 for (i = 0; i < n; ++i) {
5816 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5817 upma_bin_fail_tests[i].arg1);
5818 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5819 upma_bin_fail_tests[i].arg2);
5820 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
5821 isl_union_pw_multi_aff_free(upma1);
5822 if (upma1)
5823 break;
5825 isl_options_set_on_error(ctx, on_error);
5826 if (i < n)
5827 isl_die(ctx, isl_error_unknown,
5828 "operation not expected to succeed", return -1);
5830 return 0;
5833 /* Inputs for basic tests of binary operations on
5834 * pairs of isl_union_pw_multi_aff and isl_union_set objects.
5835 * "fn" is the function that is being tested.
5836 * "arg1" and "arg2" are string descriptions of the inputs.
5837 * "res" is a string description of the expected result.
5839 struct {
5840 __isl_give isl_union_pw_multi_aff *(*fn)(
5841 __isl_take isl_union_pw_multi_aff *upma,
5842 __isl_take isl_union_set *uset);
5843 const char *arg1;
5844 const char *arg2;
5845 const char *res;
5846 } upma_uset_tests[] = {
5847 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5848 "{ A[i] -> B[i] }", "{ B[0] }",
5849 "{ }" },
5850 { &isl_union_pw_multi_aff_intersect_domain_wrapped_domain,
5851 "{ [A[i] -> B[i]] -> C[i + 1] }", "{ A[1]; B[0] }",
5852 "{ [A[1] -> B[1]] -> C[2] }" },
5853 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5854 "{ [A[i] -> B[i]] -> C[i + 1] }", "{ A[1]; B[0] }",
5855 "{ [A[0] -> B[0]] -> C[1] }" },
5856 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5857 "{ [A[i] -> B[i]] -> C[i + 1] }", "[N] -> { B[N] }",
5858 "[N] -> { [A[N] -> B[N]] -> C[N + 1] }" },
5859 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5860 "[M] -> { [A[M] -> B[M]] -> C[M + 1] }", "[N] -> { B[N] }",
5861 "[N, M] -> { [A[N] -> B[N]] -> C[N + 1] : N = M }" },
5862 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5863 "{ [A[] -> B[]] -> C[]; N[A[] -> B[]] -> D[]; [B[] -> A[]] -> E[] }",
5864 "{ B[] }",
5865 "{ [A[] -> B[]] -> C[]; N[A[] -> B[]] -> D[] }" },
5868 /* Perform some basic tests of binary operations on
5869 * pairs of isl_union_pw_multi_aff and isl_union_set objects.
5871 static isl_stat test_upma_uset(isl_ctx *ctx)
5873 int i;
5874 isl_bool ok;
5875 isl_union_pw_multi_aff *upma, *res;
5876 isl_union_set *uset;
5878 for (i = 0; i < ARRAY_SIZE(upma_uset_tests); ++i) {
5879 upma = isl_union_pw_multi_aff_read_from_str(ctx,
5880 upma_uset_tests[i].arg1);
5881 uset = isl_union_set_read_from_str(ctx,
5882 upma_uset_tests[i].arg2);
5883 res = isl_union_pw_multi_aff_read_from_str(ctx,
5884 upma_uset_tests[i].res);
5885 upma = upma_uset_tests[i].fn(upma, uset);
5886 ok = isl_union_pw_multi_aff_plain_is_equal(upma, res);
5887 isl_union_pw_multi_aff_free(upma);
5888 isl_union_pw_multi_aff_free(res);
5889 if (ok < 0)
5890 return isl_stat_error;
5891 if (!ok)
5892 isl_die(ctx, isl_error_unknown,
5893 "unexpected result", return isl_stat_error);
5896 return isl_stat_ok;
5899 /* Inputs for basic tests of unary operations on isl_multi_pw_aff objects.
5900 * "fn" is the function that is tested.
5901 * "arg" is a string description of the input.
5902 * "res" is a string description of the expected result.
5904 struct {
5905 __isl_give isl_multi_pw_aff *(*fn)(__isl_take isl_multi_pw_aff *mpa);
5906 const char *arg;
5907 const char *res;
5908 } mpa_un_tests[] = {
5909 { &isl_multi_pw_aff_range_factor_domain,
5910 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5911 "{ A[x] -> B[(1 : x >= 5)] }" },
5912 { &isl_multi_pw_aff_range_factor_range,
5913 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5914 "{ A[y] -> C[(2 : y <= 10)] }" },
5915 { &isl_multi_pw_aff_range_factor_domain,
5916 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5917 "{ A[x] -> B[(1 : x >= 5)] }" },
5918 { &isl_multi_pw_aff_range_factor_range,
5919 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5920 "{ A[y] -> C[] }" },
5921 { &isl_multi_pw_aff_range_factor_domain,
5922 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5923 "{ A[x] -> B[] }" },
5924 { &isl_multi_pw_aff_range_factor_range,
5925 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5926 "{ A[y] -> C[(2 : y <= 10)] }" },
5927 { &isl_multi_pw_aff_range_factor_domain,
5928 "{ A[x] -> [B[] -> C[]] }",
5929 "{ A[x] -> B[] }" },
5930 { &isl_multi_pw_aff_range_factor_range,
5931 "{ A[x] -> [B[] -> C[]] }",
5932 "{ A[y] -> C[] }" },
5933 { &isl_multi_pw_aff_factor_range,
5934 "{ [B[] -> C[]] }",
5935 "{ C[] }" },
5936 { &isl_multi_pw_aff_range_factor_domain,
5937 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5938 "{ A[x] -> B[] : x >= 0 }" },
5939 { &isl_multi_pw_aff_range_factor_range,
5940 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5941 "{ A[y] -> C[] : y >= 0 }" },
5942 { &isl_multi_pw_aff_factor_range,
5943 "[N] -> { [B[] -> C[]] : N >= 0 }",
5944 "[N] -> { C[] : N >= 0 }" },
5947 /* Perform some basic tests of unary operations on isl_multi_pw_aff objects.
5949 static int test_un_mpa(isl_ctx *ctx)
5951 int i;
5952 isl_bool ok;
5953 isl_multi_pw_aff *mpa, *res;
5955 for (i = 0; i < ARRAY_SIZE(mpa_un_tests); ++i) {
5956 mpa = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].arg);
5957 res = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].res);
5958 mpa = mpa_un_tests[i].fn(mpa);
5959 ok = isl_multi_pw_aff_plain_is_equal(mpa, res);
5960 isl_multi_pw_aff_free(mpa);
5961 isl_multi_pw_aff_free(res);
5962 if (ok < 0)
5963 return -1;
5964 if (!ok)
5965 isl_die(ctx, isl_error_unknown,
5966 "unexpected result", return -1);
5969 return 0;
5972 /* Inputs for basic tests of binary operations on isl_multi_pw_aff objects.
5973 * "fn" is the function that is tested.
5974 * "arg1" and "arg2" are string descriptions of the inputs.
5975 * "res" is a string description of the expected result.
5977 struct {
5978 __isl_give isl_multi_pw_aff *(*fn)(
5979 __isl_take isl_multi_pw_aff *mpa1,
5980 __isl_take isl_multi_pw_aff *mpa2);
5981 const char *arg1;
5982 const char *arg2;
5983 const char *res;
5984 } mpa_bin_tests[] = {
5985 { &isl_multi_pw_aff_add, "{ A[] -> [1] }", "{ A[] -> [2] }",
5986 "{ A[] -> [3] }" },
5987 { &isl_multi_pw_aff_add, "{ A[x] -> [(1 : x >= 5)] }",
5988 "{ A[x] -> [(x : x <= 10)] }",
5989 "{ A[x] -> [(1 + x : 5 <= x <= 10)] }" },
5990 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5991 "{ A[x] -> [] : x <= 10 }",
5992 "{ A[x] -> [] : 5 <= x <= 10 }" },
5993 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5994 "[N] -> { A[x] -> [] : x <= N }",
5995 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5996 { &isl_multi_pw_aff_add,
5997 "[N] -> { A[x] -> [] : x <= N }",
5998 "{ A[x] -> [] : x >= 5 }",
5999 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
6000 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
6001 "{ A[y] -> C[(2 : y <= 10)] }",
6002 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
6003 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
6004 "{ A[y] -> C[2] : y <= 10 }",
6005 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
6006 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
6007 "[N] -> { A[y] -> C[2] : y <= N }",
6008 "[N] -> { A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= N)]] }" },
6009 { &isl_multi_pw_aff_range_product, "[N] -> { A[x] -> B[1] : x >= N }",
6010 "{ A[y] -> C[2] : y <= 10 }",
6011 "[N] -> { A[x] -> [B[(1 : x >= N)] -> C[(2 : x <= 10)]] }" },
6012 { &isl_multi_pw_aff_range_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
6013 "{ A[] -> [B[1] -> C[2]] }" },
6014 { &isl_multi_pw_aff_range_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
6015 "{ A[] -> [B[] -> C[]] }" },
6016 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
6017 "{ A[y] -> C[] : y <= 10 }",
6018 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] : x <= 10 }" },
6019 { &isl_multi_pw_aff_range_product, "{ A[y] -> C[] : y <= 10 }",
6020 "{ A[x] -> B[(1 : x >= 5)] }",
6021 "{ A[x] -> [C[] -> B[(1 : x >= 5)]] : x <= 10 }" },
6022 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
6023 "{ A[y] -> C[(2 : y <= 10)] }",
6024 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[(2 : y <= 10)]] }" },
6025 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
6026 "{ A[y] -> C[] : y <= 10 }",
6027 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= 10 }" },
6028 { &isl_multi_pw_aff_product, "{ A[y] -> C[] : y <= 10 }",
6029 "{ A[x] -> B[(1 : x >= 5)] }",
6030 "{ [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= 10 }" },
6031 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
6032 "[N] -> { A[y] -> C[] : y <= N }",
6033 "[N] -> { [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= N }" },
6034 { &isl_multi_pw_aff_product, "[N] -> { A[y] -> C[] : y <= N }",
6035 "{ A[x] -> B[(1 : x >= 5)] }",
6036 "[N] -> { [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= N }" },
6037 { &isl_multi_pw_aff_product, "{ A[x] -> B[] : x >= 5 }",
6038 "{ A[y] -> C[] : y <= 10 }",
6039 "{ [A[x] -> A[y]] -> [B[] -> C[]] : x >= 5 and y <= 10 }" },
6040 { &isl_multi_pw_aff_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
6041 "{ [A[] -> A[]] -> [B[1] -> C[2]] }" },
6042 { &isl_multi_pw_aff_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
6043 "{ [A[] -> A[]] -> [B[] -> C[]] }" },
6044 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6045 "{ B[i,j] -> C[i + 2j] }", "{ A[a,b] -> B[b,a] }",
6046 "{ A[a,b] -> C[b + 2a] }" },
6047 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6048 "{ B[i,j] -> C[i + 2j] }",
6049 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6050 "{ A[a,b] -> C[(b + 2a : b > a)] }" },
6051 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6052 "{ B[i,j] -> C[(i + 2j : j > 4)] }",
6053 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6054 "{ A[a,b] -> C[(b + 2a : b > a > 4)] }" },
6055 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6056 "{ B[i,j] -> C[] }",
6057 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6058 "{ A[a,b] -> C[] }" },
6059 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6060 "{ B[i,j] -> C[] : i > j }",
6061 "{ A[a,b] -> B[b,a] }",
6062 "{ A[a,b] -> C[] : b > a }" },
6063 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6064 "{ B[i,j] -> C[] : j > 5 }",
6065 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6066 "{ A[a,b] -> C[] : b > a > 5 }" },
6067 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6068 "[N] -> { B[i,j] -> C[] : j > N }",
6069 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6070 "[N] -> { A[a,b] -> C[] : b > a > N }" },
6071 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6072 "[M,N] -> { B[] -> C[] : N > 5 }",
6073 "[M,N] -> { A[] -> B[] : M > N }",
6074 "[M,N] -> { A[] -> C[] : M > N > 5 }" },
6077 /* Perform some basic tests of binary operations on isl_multi_pw_aff objects.
6079 static int test_bin_mpa(isl_ctx *ctx)
6081 int i;
6082 isl_bool ok;
6083 isl_multi_pw_aff *mpa1, *mpa2, *res;
6085 for (i = 0; i < ARRAY_SIZE(mpa_bin_tests); ++i) {
6086 mpa1 = isl_multi_pw_aff_read_from_str(ctx,
6087 mpa_bin_tests[i].arg1);
6088 mpa2 = isl_multi_pw_aff_read_from_str(ctx,
6089 mpa_bin_tests[i].arg2);
6090 res = isl_multi_pw_aff_read_from_str(ctx,
6091 mpa_bin_tests[i].res);
6092 mpa1 = mpa_bin_tests[i].fn(mpa1, mpa2);
6093 ok = isl_multi_pw_aff_plain_is_equal(mpa1, res);
6094 isl_multi_pw_aff_free(mpa1);
6095 isl_multi_pw_aff_free(res);
6096 if (ok < 0)
6097 return -1;
6098 if (!ok)
6099 isl_die(ctx, isl_error_unknown,
6100 "unexpected result", return -1);
6103 return 0;
6106 /* Inputs for basic tests of unary operations on
6107 * isl_multi_union_pw_aff objects.
6108 * "fn" is the function that is tested.
6109 * "arg" is a string description of the input.
6110 * "res" is a string description of the expected result.
6112 struct {
6113 __isl_give isl_multi_union_pw_aff *(*fn)(
6114 __isl_take isl_multi_union_pw_aff *mupa);
6115 const char *arg;
6116 const char *res;
6117 } mupa_un_tests[] = {
6118 { &isl_multi_union_pw_aff_factor_range,
6119 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]",
6120 "C[{ A[] -> [2] }]" },
6121 { &isl_multi_union_pw_aff_factor_range,
6122 "[B[] -> C[{ A[] -> [2] }]]",
6123 "C[{ A[] -> [2] }]" },
6124 { &isl_multi_union_pw_aff_factor_range,
6125 "[B[{ A[] -> [1] }] -> C[]]",
6126 "C[]" },
6127 { &isl_multi_union_pw_aff_factor_range,
6128 "[B[] -> C[]]",
6129 "C[]" },
6130 { &isl_multi_union_pw_aff_factor_range,
6131 "([B[] -> C[]] : { A[x] : x >= 0 })",
6132 "(C[] : { A[x] : x >= 0 })" },
6133 { &isl_multi_union_pw_aff_factor_range,
6134 "[N] -> ([B[] -> C[]] : { A[x] : x <= N })",
6135 "[N] -> (C[] : { A[x] : x <= N })" },
6136 { &isl_multi_union_pw_aff_factor_range,
6137 "[N] -> ([B[] -> C[]] : { : N >= 0 })",
6138 "[N] -> (C[] : { : N >= 0 })" },
6141 /* Perform some basic tests of unary operations on
6142 * isl_multi_union_pw_aff objects.
6144 static int test_un_mupa(isl_ctx *ctx)
6146 int i;
6147 isl_bool ok;
6148 isl_multi_union_pw_aff *mupa, *res;
6150 for (i = 0; i < ARRAY_SIZE(mupa_un_tests); ++i) {
6151 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6152 mupa_un_tests[i].arg);
6153 res = isl_multi_union_pw_aff_read_from_str(ctx,
6154 mupa_un_tests[i].res);
6155 mupa = mupa_un_tests[i].fn(mupa);
6156 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6157 isl_multi_union_pw_aff_free(mupa);
6158 isl_multi_union_pw_aff_free(res);
6159 if (ok < 0)
6160 return -1;
6161 if (!ok)
6162 isl_die(ctx, isl_error_unknown,
6163 "unexpected result", return -1);
6166 return 0;
6169 /* Inputs for basic tests of binary operations on
6170 * isl_multi_union_pw_aff objects.
6171 * "fn" is the function that is tested.
6172 * "arg1" and "arg2" are string descriptions of the inputs.
6173 * "res" is a string description of the expected result.
6175 struct {
6176 __isl_give isl_multi_union_pw_aff *(*fn)(
6177 __isl_take isl_multi_union_pw_aff *mupa1,
6178 __isl_take isl_multi_union_pw_aff *mupa2);
6179 const char *arg1;
6180 const char *arg2;
6181 const char *res;
6182 } mupa_bin_tests[] = {
6183 { &isl_multi_union_pw_aff_add, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
6184 "[{ A[] -> [3] }]" },
6185 { &isl_multi_union_pw_aff_sub, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
6186 "[{ A[] -> [-1] }]" },
6187 { &isl_multi_union_pw_aff_add,
6188 "[{ A[] -> [1]; B[] -> [4] }]",
6189 "[{ A[] -> [2]; C[] -> [5] }]",
6190 "[{ A[] -> [3] }]" },
6191 { &isl_multi_union_pw_aff_union_add,
6192 "[{ A[] -> [1]; B[] -> [4] }]",
6193 "[{ A[] -> [2]; C[] -> [5] }]",
6194 "[{ A[] -> [3]; B[] -> [4]; C[] -> [5] }]" },
6195 { &isl_multi_union_pw_aff_add, "[{ A[x] -> [(1)] : x >= 5 }]",
6196 "[{ A[x] -> [(x)] : x <= 10 }]",
6197 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10 }]" },
6198 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
6199 "([] : { A[x] : x <= 10 })",
6200 "([] : { A[x] : 5 <= x <= 10 })" },
6201 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
6202 "[N] -> ([] : { A[x] : x <= N })",
6203 "[N] -> ([] : { A[x] : 5 <= x <= N })" },
6204 { &isl_multi_union_pw_aff_add, "[N] -> ([] : { A[x] : x >= N })",
6205 "([] : { A[x] : x <= 10 })",
6206 "[N] -> ([] : { A[x] : N <= x <= 10 })" },
6207 { &isl_multi_union_pw_aff_union_add, "[{ A[x] -> [(1)] : x >= 5 }]",
6208 "[{ A[x] -> [(x)] : x <= 10 }]",
6209 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10; "
6210 "A[x] -> [(1)] : x > 10; A[x] -> [(x)] : x < 5 }]" },
6211 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 5 })",
6212 "([] : { A[x] : x <= 10 })",
6213 "([] : { A[x] })" },
6214 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 0 })",
6215 "[N] -> ([] : { A[x] : x >= N })",
6216 "[N] -> ([] : { A[x] : x >= 0 or x >= N })" },
6217 { &isl_multi_union_pw_aff_union_add,
6218 "[N] -> ([] : { A[] : N >= 0})",
6219 "[N] -> ([] : { A[] : N <= 0})",
6220 "[N] -> ([] : { A[] })" },
6221 { &isl_multi_union_pw_aff_union_add,
6222 "[N] -> ([] : { A[] })",
6223 "[N] -> ([] : { : })",
6224 "[N] -> ([] : { : })" },
6225 { &isl_multi_union_pw_aff_union_add,
6226 "[N] -> ([] : { : })",
6227 "[N] -> ([] : { A[] })",
6228 "[N] -> ([] : { : })" },
6229 { &isl_multi_union_pw_aff_union_add,
6230 "[N] -> ([] : { : N >= 0})",
6231 "[N] -> ([] : { : N <= 0})",
6232 "[N] -> ([] : { : })" },
6233 { &isl_multi_union_pw_aff_range_product,
6234 "B[{ A[] -> [1] }]",
6235 "C[{ A[] -> [2] }]",
6236 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]" },
6237 { &isl_multi_union_pw_aff_range_product,
6238 "(B[] : { A[x] : x >= 5 })",
6239 "(C[] : { A[x] : x <= 10 })",
6240 "([B[] -> C[]] : { A[x] : 5 <= x <= 10 })" },
6241 { &isl_multi_union_pw_aff_range_product,
6242 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6243 "(C[] : { A[x] : x <= 10 })",
6244 "[B[{ A[x] -> [x + 1] : 5 <= x <= 10 }] -> C[]]" },
6245 { &isl_multi_union_pw_aff_range_product,
6246 "(C[] : { A[x] : x <= 10 })",
6247 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6248 "[C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= 10 }]]" },
6249 { &isl_multi_union_pw_aff_range_product,
6250 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6251 "[N] -> (C[] : { A[x] : x <= N })",
6252 "[N] -> [B[{ A[x] -> [x + 1] : 5 <= x <= N }] -> C[]]" },
6253 { &isl_multi_union_pw_aff_range_product,
6254 "[N] -> (C[] : { A[x] : x <= N })",
6255 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6256 "[N] -> [C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= N }]]" },
6257 { &isl_multi_union_pw_aff_range_product,
6258 "B[{ A[] -> [1]; D[] -> [3] }]",
6259 "C[{ A[] -> [2] }]",
6260 "[B[{ A[] -> [1]; D[] -> [3] }] -> C[{ A[] -> [2] }]]" },
6261 { &isl_multi_union_pw_aff_range_product,
6262 "B[] }]",
6263 "(C[] : { A[x] })",
6264 "([B[] -> C[]] : { A[x] })" },
6265 { &isl_multi_union_pw_aff_range_product,
6266 "(B[] : { A[x] })",
6267 "C[] }]",
6268 "([B[] -> C[]] : { A[x] })" },
6271 /* Perform some basic tests of binary operations on
6272 * isl_multi_union_pw_aff objects.
6274 static int test_bin_mupa(isl_ctx *ctx)
6276 int i;
6277 isl_bool ok;
6278 isl_multi_union_pw_aff *mupa1, *mupa2, *res;
6280 for (i = 0; i < ARRAY_SIZE(mupa_bin_tests); ++i) {
6281 mupa1 = isl_multi_union_pw_aff_read_from_str(ctx,
6282 mupa_bin_tests[i].arg1);
6283 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx,
6284 mupa_bin_tests[i].arg2);
6285 res = isl_multi_union_pw_aff_read_from_str(ctx,
6286 mupa_bin_tests[i].res);
6287 mupa1 = mupa_bin_tests[i].fn(mupa1, mupa2);
6288 ok = isl_multi_union_pw_aff_plain_is_equal(mupa1, res);
6289 isl_multi_union_pw_aff_free(mupa1);
6290 isl_multi_union_pw_aff_free(res);
6291 if (ok < 0)
6292 return -1;
6293 if (!ok)
6294 isl_die(ctx, isl_error_unknown,
6295 "unexpected result", return -1);
6298 return 0;
6301 /* Inputs for basic tests of binary operations on
6302 * pairs of isl_multi_union_pw_aff and isl_set objects.
6303 * "fn" is the function that is tested.
6304 * "arg1" and "arg2" are string descriptions of the inputs.
6305 * "res" is a string description of the expected result.
6307 struct {
6308 __isl_give isl_multi_union_pw_aff *(*fn)(
6309 __isl_take isl_multi_union_pw_aff *mupa,
6310 __isl_take isl_set *set);
6311 const char *arg1;
6312 const char *arg2;
6313 const char *res;
6314 } mupa_set_tests[] = {
6315 { &isl_multi_union_pw_aff_intersect_range,
6316 "C[{ B[i,j] -> [i + 2j] }]", "{ C[1] }",
6317 "C[{ B[i,j] -> [i + 2j] : i + 2j = 1 }]" },
6318 { &isl_multi_union_pw_aff_intersect_range,
6319 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[N] }",
6320 "[N] -> C[{ B[i,j] -> [i + 2j] : i + 2j = N }]" },
6321 { &isl_multi_union_pw_aff_intersect_range,
6322 "[N] -> C[{ B[i,j] -> [i + 2j + N] }]", "{ C[1] }",
6323 "[N] -> C[{ B[i,j] -> [i + 2j + N] : i + 2j + N = 1 }]" },
6324 { &isl_multi_union_pw_aff_intersect_range,
6325 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[x] : N >= 0 }",
6326 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0 }]" },
6327 { &isl_multi_union_pw_aff_intersect_range,
6328 "C[]", "{ C[] }", "C[]" },
6329 { &isl_multi_union_pw_aff_intersect_range,
6330 "[N] -> (C[] : { : N >= 0 })",
6331 "{ C[] }",
6332 "[N] -> (C[] : { : N >= 0 })" },
6333 { &isl_multi_union_pw_aff_intersect_range,
6334 "(C[] : { A[a,b] })",
6335 "{ C[] }",
6336 "(C[] : { A[a,b] })" },
6337 { &isl_multi_union_pw_aff_intersect_range,
6338 "[N] -> (C[] : { A[a,b] : a,b <= N })",
6339 "{ C[] }",
6340 "[N] -> (C[] : { A[a,b] : a,b <= N })" },
6341 { &isl_multi_union_pw_aff_intersect_range,
6342 "C[]",
6343 "[N] -> { C[] : N >= 0 }",
6344 "[N] -> (C[] : { : N >= 0 })" },
6345 { &isl_multi_union_pw_aff_intersect_range,
6346 "(C[] : { A[a,b] })",
6347 "[N] -> { C[] : N >= 0 }",
6348 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
6349 { &isl_multi_union_pw_aff_intersect_range,
6350 "[N] -> (C[] : { : N >= 0 })",
6351 "[N] -> { C[] : N < 1024 }",
6352 "[N] -> (C[] : { : 0 <= N < 1024 })" },
6353 { &isl_multi_union_pw_aff_intersect_params,
6354 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { : N >= 0 }",
6355 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0}]" },
6356 { &isl_multi_union_pw_aff_intersect_params,
6357 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "[N] -> { : N >= 0 }",
6358 "[N] -> C[{ B[i,j] -> [i + 2j] : 0 <= N <= 256 }]" },
6359 { &isl_multi_union_pw_aff_intersect_params,
6360 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "{ : }",
6361 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]" },
6362 { &isl_multi_union_pw_aff_intersect_params,
6363 "C[]", "[N] -> { : N >= 0 }",
6364 "[N] -> (C[] : { : N >= 0 })" },
6365 { &isl_multi_union_pw_aff_intersect_params,
6366 "(C[] : { A[a,b] })", "[N] -> { : N >= 0 }",
6367 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
6368 { &isl_multi_union_pw_aff_intersect_params,
6369 "[N] -> (C[] : { A[a,N] })", "{ : }",
6370 "[N] -> (C[] : { A[a,N] })" },
6371 { &isl_multi_union_pw_aff_intersect_params,
6372 "[N] -> (C[] : { A[a,b] : N <= 256 })", "[N] -> { : N >= 0 }",
6373 "[N] -> (C[] : { A[a,b] : 0 <= N <= 256 })" },
6376 /* Perform some basic tests of binary operations on
6377 * pairs of isl_multi_union_pw_aff and isl_set objects.
6379 static int test_mupa_set(isl_ctx *ctx)
6381 int i;
6382 isl_bool ok;
6383 isl_multi_union_pw_aff *mupa, *res;
6384 isl_set *set;
6386 for (i = 0; i < ARRAY_SIZE(mupa_set_tests); ++i) {
6387 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6388 mupa_set_tests[i].arg1);
6389 set = isl_set_read_from_str(ctx, mupa_set_tests[i].arg2);
6390 res = isl_multi_union_pw_aff_read_from_str(ctx,
6391 mupa_set_tests[i].res);
6392 mupa = mupa_set_tests[i].fn(mupa, set);
6393 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6394 isl_multi_union_pw_aff_free(mupa);
6395 isl_multi_union_pw_aff_free(res);
6396 if (ok < 0)
6397 return -1;
6398 if (!ok)
6399 isl_die(ctx, isl_error_unknown,
6400 "unexpected result", return -1);
6403 return 0;
6406 /* Inputs for basic tests of binary operations on
6407 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
6408 * "fn" is the function that is tested.
6409 * "arg1" and "arg2" are string descriptions of the inputs.
6410 * "res" is a string description of the expected result.
6412 struct {
6413 __isl_give isl_multi_union_pw_aff *(*fn)(
6414 __isl_take isl_multi_union_pw_aff *mupa,
6415 __isl_take isl_union_set *uset);
6416 const char *arg1;
6417 const char *arg2;
6418 const char *res;
6419 } mupa_uset_tests[] = {
6420 { &isl_multi_union_pw_aff_intersect_domain,
6421 "C[{ B[i,j] -> [i + 2j] }]", "{ B[i,i] }",
6422 "C[{ B[i,i] -> [3i] }]" },
6423 { &isl_multi_union_pw_aff_intersect_domain,
6424 "(C[] : { B[i,j] })", "{ B[i,i] }",
6425 "(C[] : { B[i,i] })" },
6426 { &isl_multi_union_pw_aff_intersect_domain,
6427 "(C[] : { B[i,j] })", "[N] -> { B[N,N] }",
6428 "[N] -> (C[] : { B[N,N] })" },
6429 { &isl_multi_union_pw_aff_intersect_domain,
6430 "C[]", "{ B[i,i] }",
6431 "(C[] : { B[i,i] })" },
6432 { &isl_multi_union_pw_aff_intersect_domain,
6433 "[N] -> (C[] : { : N >= 0 })", "{ B[i,i] }",
6434 "[N] -> (C[] : { B[i,i] : N >= 0 })" },
6437 /* Perform some basic tests of binary operations on
6438 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
6440 static int test_mupa_uset(isl_ctx *ctx)
6442 int i;
6443 isl_bool ok;
6444 isl_multi_union_pw_aff *mupa, *res;
6445 isl_union_set *uset;
6447 for (i = 0; i < ARRAY_SIZE(mupa_uset_tests); ++i) {
6448 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6449 mupa_uset_tests[i].arg1);
6450 uset = isl_union_set_read_from_str(ctx,
6451 mupa_uset_tests[i].arg2);
6452 res = isl_multi_union_pw_aff_read_from_str(ctx,
6453 mupa_uset_tests[i].res);
6454 mupa = mupa_uset_tests[i].fn(mupa, uset);
6455 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6456 isl_multi_union_pw_aff_free(mupa);
6457 isl_multi_union_pw_aff_free(res);
6458 if (ok < 0)
6459 return -1;
6460 if (!ok)
6461 isl_die(ctx, isl_error_unknown,
6462 "unexpected result", return -1);
6465 return 0;
6468 /* Inputs for basic tests of binary operations on
6469 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
6470 * "fn" is the function that is tested.
6471 * "arg1" and "arg2" are string descriptions of the inputs.
6472 * "res" is a string description of the expected result.
6474 struct {
6475 __isl_give isl_multi_union_pw_aff *(*fn)(
6476 __isl_take isl_multi_union_pw_aff *mupa,
6477 __isl_take isl_multi_aff *ma);
6478 const char *arg1;
6479 const char *arg2;
6480 const char *res;
6481 } mupa_ma_tests[] = {
6482 { &isl_multi_union_pw_aff_apply_multi_aff,
6483 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6484 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6485 "{ C[a,b] -> D[b,a] }",
6486 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
6487 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
6488 { &isl_multi_union_pw_aff_apply_multi_aff,
6489 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
6490 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6491 "{ C[a,b] -> D[b,a] }",
6492 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
6493 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
6494 { &isl_multi_union_pw_aff_apply_multi_aff,
6495 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6496 "[N] -> { C[a] -> D[a + N] }",
6497 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }] " },
6498 { &isl_multi_union_pw_aff_apply_multi_aff,
6499 "C[]",
6500 "{ C[] -> D[] }",
6501 "D[]" },
6502 { &isl_multi_union_pw_aff_apply_multi_aff,
6503 "[N] -> (C[] : { : N >= 0 })",
6504 "{ C[] -> D[] }",
6505 "[N] -> (D[] : { : N >= 0 })" },
6506 { &isl_multi_union_pw_aff_apply_multi_aff,
6507 "C[]",
6508 "[N] -> { C[] -> D[N] }",
6509 "[N] -> D[{ [N] }]" },
6510 { &isl_multi_union_pw_aff_apply_multi_aff,
6511 "(C[] : { A[i,j] : i >= j })",
6512 "{ C[] -> D[] }",
6513 "(D[] : { A[i,j] : i >= j })" },
6514 { &isl_multi_union_pw_aff_apply_multi_aff,
6515 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6516 "{ C[] -> D[] }",
6517 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6518 { &isl_multi_union_pw_aff_apply_multi_aff,
6519 "(C[] : { A[i,j] : i >= j })",
6520 "[N] -> { C[] -> D[N] }",
6521 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6524 /* Perform some basic tests of binary operations on
6525 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
6527 static int test_mupa_ma(isl_ctx *ctx)
6529 int i;
6530 isl_bool ok;
6531 isl_multi_union_pw_aff *mupa, *res;
6532 isl_multi_aff *ma;
6534 for (i = 0; i < ARRAY_SIZE(mupa_ma_tests); ++i) {
6535 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6536 mupa_ma_tests[i].arg1);
6537 ma = isl_multi_aff_read_from_str(ctx, mupa_ma_tests[i].arg2);
6538 res = isl_multi_union_pw_aff_read_from_str(ctx,
6539 mupa_ma_tests[i].res);
6540 mupa = mupa_ma_tests[i].fn(mupa, ma);
6541 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6542 isl_multi_union_pw_aff_free(mupa);
6543 isl_multi_union_pw_aff_free(res);
6544 if (ok < 0)
6545 return -1;
6546 if (!ok)
6547 isl_die(ctx, isl_error_unknown,
6548 "unexpected result", return -1);
6551 return 0;
6554 /* Inputs for basic tests of binary operations on
6555 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
6556 * "fn" is the function that is tested.
6557 * "arg1" and "arg2" are string descriptions of the inputs.
6558 * "res" is a string description of the expected result.
6560 struct {
6561 __isl_give isl_union_pw_aff *(*fn)(
6562 __isl_take isl_multi_union_pw_aff *mupa,
6563 __isl_take isl_pw_aff *pa);
6564 const char *arg1;
6565 const char *arg2;
6566 const char *res;
6567 } mupa_pa_tests[] = {
6568 { &isl_multi_union_pw_aff_apply_pw_aff,
6569 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6570 "[N] -> { C[a] -> [a + N] }",
6571 "[N] -> { A[i,j] -> [i + N]; B[i,j] -> [j + N] }" },
6572 { &isl_multi_union_pw_aff_apply_pw_aff,
6573 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6574 "{ C[a] -> [a] : a >= 0; C[a] -> [-a] : a < 0 }",
6575 "{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
6576 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }" },
6577 { &isl_multi_union_pw_aff_apply_pw_aff,
6578 "C[]",
6579 "[N] -> { C[] -> [N] }",
6580 "[N] -> { [N] }" },
6581 { &isl_multi_union_pw_aff_apply_pw_aff,
6582 "C[]",
6583 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
6584 "[N] -> { [N] : N >= 0; [-N] : N < 0 }" },
6585 { &isl_multi_union_pw_aff_apply_pw_aff,
6586 "[N] -> (C[] : { : N >= 0 })",
6587 "[N] -> { C[] -> [N] }",
6588 "[N] -> { [N] : N >= 0 }" },
6589 { &isl_multi_union_pw_aff_apply_pw_aff,
6590 "[N] -> (C[] : { : N >= 0 })",
6591 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
6592 "[N] -> { [N] : N >= 0 }" },
6593 { &isl_multi_union_pw_aff_apply_pw_aff,
6594 "[N] -> (C[] : { : N >= 0 })",
6595 "{ C[] -> [0] }",
6596 "[N] -> { [0] : N >= 0 }" },
6597 { &isl_multi_union_pw_aff_apply_pw_aff,
6598 "(C[] : { A[i,j] : i >= j })",
6599 "[N] -> { C[] -> [N] }",
6600 "[N] -> { A[i,j] -> [N] : i >= j }" },
6601 { &isl_multi_union_pw_aff_apply_pw_aff,
6602 "(C[] : { A[i,j] : i >= j })",
6603 "[N] -> { C[] -> [N] : N >= 0 }",
6604 "[N] -> { A[i,j] -> [N] : i >= j and N >= 0 }" },
6607 /* Perform some basic tests of binary operations on
6608 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
6610 static int test_mupa_pa(isl_ctx *ctx)
6612 int i;
6613 isl_bool ok;
6614 isl_multi_union_pw_aff *mupa;
6615 isl_union_pw_aff *upa, *res;
6616 isl_pw_aff *pa;
6618 for (i = 0; i < ARRAY_SIZE(mupa_pa_tests); ++i) {
6619 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6620 mupa_pa_tests[i].arg1);
6621 pa = isl_pw_aff_read_from_str(ctx, mupa_pa_tests[i].arg2);
6622 res = isl_union_pw_aff_read_from_str(ctx,
6623 mupa_pa_tests[i].res);
6624 upa = mupa_pa_tests[i].fn(mupa, pa);
6625 ok = isl_union_pw_aff_plain_is_equal(upa, res);
6626 isl_union_pw_aff_free(upa);
6627 isl_union_pw_aff_free(res);
6628 if (ok < 0)
6629 return -1;
6630 if (!ok)
6631 isl_die(ctx, isl_error_unknown,
6632 "unexpected result", return -1);
6635 return 0;
6638 /* Inputs for basic tests of binary operations on
6639 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6640 * "fn" is the function that is tested.
6641 * "arg1" and "arg2" are string descriptions of the inputs.
6642 * "res" is a string description of the expected result.
6644 struct {
6645 __isl_give isl_multi_union_pw_aff *(*fn)(
6646 __isl_take isl_multi_union_pw_aff *mupa,
6647 __isl_take isl_pw_multi_aff *pma);
6648 const char *arg1;
6649 const char *arg2;
6650 const char *res;
6651 } mupa_pma_tests[] = {
6652 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6653 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6654 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6655 "{ C[a,b] -> D[b,a] }",
6656 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
6657 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
6658 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6659 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
6660 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6661 "{ C[a,b] -> D[b,a] }",
6662 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
6663 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
6664 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6665 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6666 "[N] -> { C[a] -> D[a + N] }",
6667 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }]" },
6668 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6669 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6670 "{ C[a] -> D[a] : a >= 0; C[a] -> D[-a] : a < 0 }",
6671 "D[{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
6672 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }]" },
6673 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6674 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6675 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6676 "{ C[a,b] -> D[a,b] : a >= b; C[a,b] -> D[b,a] : a < b }",
6677 "D[{ A[i,j] -> [i] : i >= j; A[i,j] -> [j] : i < j; "
6678 "B[i,j] -> [j] : i <= j; B[i,j] -> [i] : i > j }, "
6679 "{ A[i,j] -> [j] : i >= j; A[i,j] -> [i] : i < j; "
6680 "B[i,j] -> [i] : i <= j; B[i,j] -> [j] : i > j }]" },
6681 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6682 "C[]",
6683 "{ C[] -> D[] }",
6684 "D[]" },
6685 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6686 "[N] -> (C[] : { : N >= 0 })",
6687 "{ C[] -> D[] }",
6688 "[N] -> (D[] : { : N >= 0 })" },
6689 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6690 "C[]",
6691 "[N] -> { C[] -> D[N] }",
6692 "[N] -> D[{ [N] }]" },
6693 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6694 "(C[] : { A[i,j] : i >= j })",
6695 "{ C[] -> D[] }",
6696 "(D[] : { A[i,j] : i >= j })" },
6697 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6698 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6699 "{ C[] -> D[] }",
6700 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6701 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6702 "(C[] : { A[i,j] : i >= j })",
6703 "[N] -> { C[] -> D[N] }",
6704 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6705 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6706 "C[]",
6707 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6708 "[N] -> D[{ [N] : N >= 0; [-N] : N < 0 }]" },
6709 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6710 "[N] -> (C[] : { : N >= 0 })",
6711 "[N] -> { C[] -> D[N] }",
6712 "[N] -> D[{ [N] : N >= 0 }]" },
6713 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6714 "[N] -> (C[] : { : N >= 0 })",
6715 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6716 "[N] -> D[{ [N] : N >= 0 }]" },
6717 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6718 "[N] -> (C[] : { : N >= 0 })",
6719 "{ C[] -> D[0] }",
6720 "[N] -> D[{ [0] : N >= 0 }]" },
6721 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6722 "(C[] : { A[i,j] : i >= j })",
6723 "[N] -> { C[] -> D[N] : N >= 0 }",
6724 "[N] -> D[{ A[i,j] -> [N] : i >= j and N >= 0 }]" },
6727 /* Perform some basic tests of binary operations on
6728 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6730 static int test_mupa_pma(isl_ctx *ctx)
6732 int i;
6733 isl_bool ok;
6734 isl_multi_union_pw_aff *mupa, *res;
6735 isl_pw_multi_aff *pma;
6737 for (i = 0; i < ARRAY_SIZE(mupa_pma_tests); ++i) {
6738 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6739 mupa_pma_tests[i].arg1);
6740 pma = isl_pw_multi_aff_read_from_str(ctx,
6741 mupa_pma_tests[i].arg2);
6742 res = isl_multi_union_pw_aff_read_from_str(ctx,
6743 mupa_pma_tests[i].res);
6744 mupa = mupa_pma_tests[i].fn(mupa, pma);
6745 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6746 isl_multi_union_pw_aff_free(mupa);
6747 isl_multi_union_pw_aff_free(res);
6748 if (ok < 0)
6749 return -1;
6750 if (!ok)
6751 isl_die(ctx, isl_error_unknown,
6752 "unexpected result", return -1);
6755 return 0;
6758 /* Inputs for basic tests of binary operations on
6759 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6760 * "fn" is the function that is tested.
6761 * "arg1" and "arg2" are string descriptions of the inputs.
6762 * "res" is a string description of the expected result.
6764 struct {
6765 __isl_give isl_multi_union_pw_aff *(*fn)(
6766 __isl_take isl_multi_union_pw_aff *mupa,
6767 __isl_take isl_union_pw_multi_aff *upma);
6768 const char *arg1;
6769 const char *arg2;
6770 const char *res;
6771 } mupa_upma_tests[] = {
6772 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6773 "C[{ B[i,j] -> [i + 2j] }]", "{ A[a,b] -> B[b,a] }",
6774 "C[{ A[a,b] -> [b + 2a] }]" },
6775 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6776 "C[{ B[i,j] -> [i + 2j] }]",
6777 "{ A[a,b] -> B[b,a] : b > a }",
6778 "C[{ A[a,b] -> [b + 2a] : b > a }]" },
6779 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6780 "C[{ B[i,j] -> [i + 2j] : j > 4 }]",
6781 "{ A[a,b] -> B[b,a] : b > a }",
6782 "C[{ A[a,b] -> [b + 2a] : b > a > 4 }]" },
6783 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6784 "C[{ B[i,j] -> [i + 2j] }]",
6785 "{ A[a,b] -> B[b,a] : a > b; A[a,b] -> B[a,b] : a <= b }",
6786 "C[{ A[a,b] -> [b + 2a] : a > b; A[a,b] -> [a + 2b] : a <= b }]" },
6787 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6788 "(C[] : { B[a,b] })",
6789 "{ A[a,b] -> B[b,a] }",
6790 "(C[] : { A[a,b] })" },
6791 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6792 "(C[] : { B[a,b] })",
6793 "{ B[a,b] -> A[b,a] }",
6794 "(C[] : { })" },
6795 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6796 "(C[] : { B[a,b] })",
6797 "{ A[a,b] -> B[b,a] : a > b }",
6798 "(C[] : { A[a,b] : a > b })" },
6799 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6800 "(C[] : { B[a,b] : a > b })",
6801 "{ A[a,b] -> B[b,a] }",
6802 "(C[] : { A[a,b] : b > a })" },
6803 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6804 "[N] -> (C[] : { B[a,b] : a > N })",
6805 "{ A[a,b] -> B[b,a] : a > b }",
6806 "[N] -> (C[] : { A[a,b] : a > b > N })" },
6807 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6808 "(C[] : { B[a,b] : a > b })",
6809 "[N] -> { A[a,b] -> B[b,a] : a > N }",
6810 "[N] -> (C[] : { A[a,b] : b > a > N })" },
6811 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6812 "C[]",
6813 "{ A[a,b] -> B[b,a] }",
6814 "C[]" },
6815 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6816 "[N] -> (C[] : { : N >= 0 })",
6817 "{ A[a,b] -> B[b,a] }",
6818 "[N] -> (C[] : { : N >= 0 })" },
6819 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6820 "C[]",
6821 "[N] -> { A[a,b] -> B[b,a] : N >= 0 }",
6822 "[N] -> (C[] : { : N >= 0 })" },
6825 /* Perform some basic tests of binary operations on
6826 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6828 static int test_mupa_upma(isl_ctx *ctx)
6830 int i;
6831 isl_bool ok;
6832 isl_multi_union_pw_aff *mupa, *res;
6833 isl_union_pw_multi_aff *upma;
6835 for (i = 0; i < ARRAY_SIZE(mupa_upma_tests); ++i) {
6836 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6837 mupa_upma_tests[i].arg1);
6838 upma = isl_union_pw_multi_aff_read_from_str(ctx,
6839 mupa_upma_tests[i].arg2);
6840 res = isl_multi_union_pw_aff_read_from_str(ctx,
6841 mupa_upma_tests[i].res);
6842 mupa = mupa_upma_tests[i].fn(mupa, upma);
6843 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6844 isl_multi_union_pw_aff_free(mupa);
6845 isl_multi_union_pw_aff_free(res);
6846 if (ok < 0)
6847 return -1;
6848 if (!ok)
6849 isl_die(ctx, isl_error_unknown,
6850 "unexpected result", return -1);
6853 return 0;
6856 /* Check that the input tuple of an isl_aff can be set properly.
6858 static isl_stat test_aff_set_tuple_id(isl_ctx *ctx)
6860 isl_id *id;
6861 isl_aff *aff;
6862 isl_stat equal;
6864 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x + 1] }");
6865 id = isl_id_alloc(ctx, "A", NULL);
6866 aff = isl_aff_set_tuple_id(aff, isl_dim_in, id);
6867 equal = aff_check_plain_equal(aff, "{ A[x] -> [x + 1] }");
6868 isl_aff_free(aff);
6869 if (equal < 0)
6870 return isl_stat_error;
6872 return isl_stat_ok;
6875 /* Check that affine expressions get normalized on addition/subtraction.
6876 * In particular, check that (final) unused integer divisions get removed
6877 * such that an expression derived from expressions with integer divisions
6878 * is found to be obviously equal to one that is created directly.
6880 static isl_stat test_aff_normalize(isl_ctx *ctx)
6882 isl_aff *aff, *aff2;
6883 isl_stat ok;
6885 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x//2] }");
6886 aff2 = isl_aff_read_from_str(ctx, "{ [x] -> [1 + x//2] }");
6887 aff = isl_aff_sub(aff2, aff);
6888 ok = aff_check_plain_equal(aff, "{ [x] -> [1] }");
6889 isl_aff_free(aff);
6891 return ok;
6894 int test_aff(isl_ctx *ctx)
6896 const char *str;
6897 isl_set *set;
6898 isl_space *space;
6899 isl_local_space *ls;
6900 isl_aff *aff;
6901 int zero;
6902 isl_stat equal;
6904 if (test_upa(ctx) < 0)
6905 return -1;
6906 if (test_bin_aff(ctx) < 0)
6907 return -1;
6908 if (test_bin_pw_aff(ctx) < 0)
6909 return -1;
6910 if (test_upma_test(ctx) < 0)
6911 return -1;
6912 if (test_bin_upma(ctx) < 0)
6913 return -1;
6914 if (test_bin_upma_fail(ctx) < 0)
6915 return -1;
6916 if (test_upma_uset(ctx) < 0)
6917 return -1;
6918 if (test_un_mpa(ctx) < 0)
6919 return -1;
6920 if (test_bin_mpa(ctx) < 0)
6921 return -1;
6922 if (test_un_mupa(ctx) < 0)
6923 return -1;
6924 if (test_bin_mupa(ctx) < 0)
6925 return -1;
6926 if (test_mupa_set(ctx) < 0)
6927 return -1;
6928 if (test_mupa_uset(ctx) < 0)
6929 return -1;
6930 if (test_mupa_ma(ctx) < 0)
6931 return -1;
6932 if (test_mupa_pa(ctx) < 0)
6933 return -1;
6934 if (test_mupa_pma(ctx) < 0)
6935 return -1;
6936 if (test_mupa_upma(ctx) < 0)
6937 return -1;
6939 space = isl_space_set_alloc(ctx, 0, 1);
6940 ls = isl_local_space_from_space(space);
6941 aff = isl_aff_zero_on_domain(ls);
6943 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6944 aff = isl_aff_scale_down_ui(aff, 3);
6945 aff = isl_aff_floor(aff);
6946 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6947 aff = isl_aff_scale_down_ui(aff, 2);
6948 aff = isl_aff_floor(aff);
6949 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6951 str = "{ [10] }";
6952 set = isl_set_read_from_str(ctx, str);
6953 aff = isl_aff_gist(aff, set);
6955 aff = isl_aff_add_constant_si(aff, -16);
6956 zero = isl_aff_plain_is_zero(aff);
6957 isl_aff_free(aff);
6959 if (zero < 0)
6960 return -1;
6961 if (!zero)
6962 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6964 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
6965 aff = isl_aff_scale_down_ui(aff, 64);
6966 aff = isl_aff_floor(aff);
6967 equal = aff_check_plain_equal(aff, "{ [-1] }");
6968 isl_aff_free(aff);
6969 if (equal < 0)
6970 return -1;
6972 if (test_aff_set_tuple_id(ctx) < 0)
6973 return -1;
6974 if (test_aff_normalize(ctx) < 0)
6975 return -1;
6977 return 0;
6980 /* Inputs for isl_set_bind tests.
6981 * "set" is the input set.
6982 * "tuple" is the binding tuple.
6983 * "res" is the expected result.
6985 static
6986 struct {
6987 const char *set;
6988 const char *tuple;
6989 const char *res;
6990 } bind_set_tests[] = {
6991 { "{ A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
6992 "{ A[M, N] }",
6993 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
6994 { "{ B[N, M] : M mod 2 = 0 and N mod 8 = 3 }",
6995 "{ B[N, M] }",
6996 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
6997 { "[M] -> { C[N] : M mod 2 = 0 and N mod 8 = 3 }",
6998 "{ C[N] }",
6999 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
7000 { "[M] -> { D[x, N] : x mod 2 = 0 and N mod 8 = 3 and M >= 0 }",
7001 "{ D[M, N] }",
7002 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 and M >= 0 }" },
7005 /* Perform basic isl_set_bind tests.
7007 static isl_stat test_bind_set(isl_ctx *ctx)
7009 int i;
7011 for (i = 0; i < ARRAY_SIZE(bind_set_tests); ++i) {
7012 const char *str;
7013 isl_set *set;
7014 isl_multi_id *tuple;
7015 isl_stat r;
7017 set = isl_set_read_from_str(ctx, bind_set_tests[i].set);
7018 str = bind_set_tests[i].tuple;
7019 tuple = isl_multi_id_read_from_str(ctx, str);
7020 set = isl_set_bind(set, tuple);
7021 r = set_check_equal(set, bind_set_tests[i].res);
7022 isl_set_free(set);
7023 if (r < 0)
7024 return isl_stat_error;
7027 return isl_stat_ok;
7030 /* Inputs for isl_map_bind_domain tests.
7031 * "map" is the input map.
7032 * "tuple" is the binding tuple.
7033 * "res" is the expected result.
7035 struct {
7036 const char *map;
7037 const char *tuple;
7038 const char *res;
7039 } bind_map_domain_tests[] = {
7040 { "{ A[M, N] -> [M + floor(N/2)] }",
7041 "{ A[M, N] }",
7042 "[M, N] -> { [M + floor(N/2)] }" },
7043 { "{ B[N, M] -> [M + floor(N/2)] }",
7044 "{ B[N, M] }",
7045 "[N, M] -> { [M + floor(N/2)] }" },
7046 { "[M] -> { C[N] -> [M + floor(N/2)] }",
7047 "{ C[N] }",
7048 "[M, N] -> { [M + floor(N/2)] }" },
7049 { "[M] -> { C[x, N] -> [x + floor(N/2)] }",
7050 "{ C[M, N] }",
7051 "[M, N] -> { [M + floor(N/2)] }" },
7052 { "[M] -> { C[x, N] -> [M + floor(N/2)] }",
7053 "{ C[M, N] }",
7054 "[M, N] -> { [M + floor(N/2)] }" },
7055 { "[A, M] -> { C[N, x] -> [x + floor(N/2)] }",
7056 "{ C[N, M] }",
7057 "[A, N, M] -> { [M + floor(N/2)] }" },
7060 /* Perform basic isl_map_bind_domain tests.
7062 static isl_stat test_bind_map_domain(isl_ctx *ctx)
7064 int i;
7066 for (i = 0; i < ARRAY_SIZE(bind_map_domain_tests); ++i) {
7067 const char *str;
7068 isl_map *map;
7069 isl_set *set;
7070 isl_multi_id *tuple;
7071 isl_stat r;
7073 str = bind_map_domain_tests[i].map;
7074 map = isl_map_read_from_str(ctx, str);
7075 str = bind_map_domain_tests[i].tuple;
7076 tuple = isl_multi_id_read_from_str(ctx, str);
7077 set = isl_map_bind_domain(map, tuple);
7078 str = bind_map_domain_tests[i].res;
7079 r = set_check_equal(set, str);
7080 isl_set_free(set);
7081 if (r < 0)
7082 return isl_stat_error;
7085 return isl_stat_ok;
7088 /* Inputs for isl_union_map_bind_range tests.
7089 * "map" is the input union map.
7090 * "tuple" is the binding tuple.
7091 * "res" is the expected result.
7093 struct {
7094 const char *map;
7095 const char *tuple;
7096 const char *res;
7097 } bind_umap_range_tests[] = {
7098 { "{ B[N, M] -> A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
7099 "{ A[M, N] }",
7100 "[M, N] -> { B[N, M] : M mod 2 = 0 and N mod 8 = 3 }" },
7101 { "{ B[N, M] -> A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
7102 "{ B[M, N] }",
7103 "{ }" },
7104 { "{ A[] -> B[]; C[] -> D[]; E[] -> B[] }",
7105 "{ B[] }",
7106 "{ A[]; E[] }" },
7109 /* Perform basic isl_union_map_bind_range tests.
7111 static isl_stat test_bind_umap_range(isl_ctx *ctx)
7113 int i;
7115 for (i = 0; i < ARRAY_SIZE(bind_umap_range_tests); ++i) {
7116 const char *str;
7117 isl_union_map *umap;
7118 isl_union_set *uset;
7119 isl_multi_id *tuple;
7120 isl_stat r;
7122 str = bind_umap_range_tests[i].map;
7123 umap = isl_union_map_read_from_str(ctx, str);
7124 str = bind_umap_range_tests[i].tuple;
7125 tuple = isl_multi_id_read_from_str(ctx, str);
7126 uset = isl_union_map_bind_range(umap, tuple);
7127 str = bind_umap_range_tests[i].res;
7128 r = uset_check_equal(uset, str);
7129 isl_union_set_free(uset);
7130 if (r < 0)
7131 return isl_stat_error;
7134 return isl_stat_ok;
7137 /* Inputs for isl_pw_multi_aff_bind_domain tests.
7138 * "pma" is the input expression.
7139 * "tuple" is the binding tuple.
7140 * "res" is the expected result.
7142 struct {
7143 const char *pma;
7144 const char *tuple;
7145 const char *res;
7146 } bind_pma_domain_tests[] = {
7147 { "{ A[M, N] -> [M + floor(N/2)] }",
7148 "{ A[M, N] }",
7149 "[M, N] -> { [M + floor(N/2)] }" },
7150 { "{ B[N, M] -> [M + floor(N/2)] }",
7151 "{ B[N, M] }",
7152 "[N, M] -> { [M + floor(N/2)] }" },
7153 { "[M] -> { C[N] -> [M + floor(N/2)] }",
7154 "{ C[N] }",
7155 "[M, N] -> { [M + floor(N/2)] }" },
7156 { "[M] -> { C[x, N] -> [x + floor(N/2)] }",
7157 "{ C[M, N] }",
7158 "[M, N] -> { [M + floor(N/2)] }" },
7159 { "[M] -> { C[x, N] -> [M + floor(N/2)] }",
7160 "{ C[M, N] }",
7161 "[M, N] -> { [M + floor(N/2)] }" },
7162 { "[A, M] -> { C[N, x] -> [x + floor(N/2)] }",
7163 "{ C[N, M] }",
7164 "[A, N, M] -> { [M + floor(N/2)] }" },
7167 /* Perform basic isl_pw_multi_aff_bind_domain tests.
7169 static isl_stat test_bind_pma_domain(isl_ctx *ctx)
7171 int i;
7173 for (i = 0; i < ARRAY_SIZE(bind_pma_domain_tests); ++i) {
7174 const char *str;
7175 isl_pw_multi_aff *pma;
7176 isl_multi_id *tuple;
7177 isl_stat r;
7179 str = bind_pma_domain_tests[i].pma;
7180 pma = isl_pw_multi_aff_read_from_str(ctx, str);
7181 str = bind_pma_domain_tests[i].tuple;
7182 tuple = isl_multi_id_read_from_str(ctx, str);
7183 pma = isl_pw_multi_aff_bind_domain(pma, tuple);
7184 str = bind_pma_domain_tests[i].res;
7185 r = pw_multi_aff_check_plain_equal(pma, str);
7186 isl_pw_multi_aff_free(pma);
7187 if (r < 0)
7188 return isl_stat_error;
7191 return isl_stat_ok;
7194 /* Inputs for isl_pw_multi_aff_bind_domain_wrapped_domain tests.
7195 * "pma" is the input expression.
7196 * "tuple" is the binding tuple.
7197 * "res" is the expected result.
7199 struct {
7200 const char *pma;
7201 const char *tuple;
7202 const char *res;
7203 } bind_pma_domain_wrapped_tests[] = {
7204 { "{ [A[M, N] -> B[]] -> [M + floor(N/2)] }",
7205 "{ A[M, N] }",
7206 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
7207 { "{ [B[N, M] -> D[]] -> [M + floor(N/2)] }",
7208 "{ B[N, M] }",
7209 "[N, M] -> { D[] -> [M + floor(N/2)] }" },
7210 { "[M] -> { [C[N] -> B[x]] -> [x + M + floor(N/2)] }",
7211 "{ C[N] }",
7212 "[M, N] -> { B[x] -> [x + M + floor(N/2)] }" },
7213 { "[M] -> { [C[x, N] -> B[]] -> [x + floor(N/2)] }",
7214 "{ C[M, N] }",
7215 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
7216 { "[M] -> { [C[x, N] -> B[]] -> [M + floor(N/2)] }",
7217 "{ C[M, N] }",
7218 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
7219 { "[A, M] -> { [C[N, x] -> B[]] -> [x + floor(N/2)] }",
7220 "{ C[N, M] }",
7221 "[A, N, M] -> { B[] -> [M + floor(N/2)] }" },
7224 /* Perform basic isl_pw_multi_aff_bind_domain_wrapped_domain tests.
7226 static isl_stat test_bind_pma_domain_wrapped(isl_ctx *ctx)
7228 int i;
7230 for (i = 0; i < ARRAY_SIZE(bind_pma_domain_wrapped_tests); ++i) {
7231 const char *str;
7232 isl_pw_multi_aff *pma;
7233 isl_multi_id *tuple;
7234 isl_stat r;
7236 str = bind_pma_domain_wrapped_tests[i].pma;
7237 pma = isl_pw_multi_aff_read_from_str(ctx, str);
7238 str = bind_pma_domain_wrapped_tests[i].tuple;
7239 tuple = isl_multi_id_read_from_str(ctx, str);
7240 pma = isl_pw_multi_aff_bind_domain_wrapped_domain(pma, tuple);
7241 str = bind_pma_domain_wrapped_tests[i].res;
7242 r = pw_multi_aff_check_plain_equal(pma, str);
7243 isl_pw_multi_aff_free(pma);
7244 if (r < 0)
7245 return isl_stat_error;
7248 return isl_stat_ok;
7251 /* Inputs for isl_aff_bind_id tests.
7252 * "aff" is the input expression.
7253 * "id" is the binding id.
7254 * "res" is the expected result.
7256 static
7257 struct {
7258 const char *aff;
7259 const char *id;
7260 const char *res;
7261 } bind_aff_tests[] = {
7262 { "{ [4] }", "M", "[M = 4] -> { : }" },
7263 { "{ B[x] -> [floor(x/2)] }", "M", "[M] -> { B[x] : M = floor(x/2) }" },
7264 { "[M] -> { [4] }", "M", "[M = 4] -> { : }" },
7265 { "[M] -> { [floor(M/2)] }", "M", "[M] -> { : floor(M/2) = M }" },
7266 { "{ [NaN] }", "M", "{ : false }" },
7267 { "{ A[x] -> [NaN] }", "M", "{ A[x] : false }" },
7270 /* Perform basic isl_aff_bind_id tests.
7272 static isl_stat test_bind_aff(isl_ctx *ctx)
7274 int i;
7276 for (i = 0; i < ARRAY_SIZE(bind_aff_tests); ++i) {
7277 isl_aff *aff;
7278 isl_set *res;
7279 isl_id *id;
7280 isl_stat r;
7282 aff = isl_aff_read_from_str(ctx, bind_aff_tests[i].aff);
7283 id = isl_id_read_from_str(ctx, bind_aff_tests[i].id);
7284 res = isl_set_from_basic_set(isl_aff_bind_id(aff, id));
7285 r = set_check_equal(res, bind_aff_tests[i].res);
7286 isl_set_free(res);
7287 if (r < 0)
7288 return isl_stat_error;
7291 return isl_stat_ok;
7294 /* Inputs for isl_pw_aff_bind_id tests.
7295 * "pa" is the input expression.
7296 * "id" is the binding id.
7297 * "res" is the expected result.
7299 static
7300 struct {
7301 const char *pa;
7302 const char *id;
7303 const char *res;
7304 } bind_pa_tests[] = {
7305 { "{ [4] }", "M", "[M = 4] -> { : }" },
7306 { "{ B[x] -> [floor(x/2)] }", "M", "[M] -> { B[x] : M = floor(x/2) }" },
7307 { "[M] -> { [4] }", "M", "[M = 4] -> { : }" },
7308 { "[M] -> { [floor(M/2)] }", "M", "[M] -> { : floor(M/2) = M }" },
7309 { "[M] -> { [M] : M >= 0; [-M] : M < 0 }", "M", "[M] -> { : M >= 0 }" },
7310 { "{ [NaN] }", "M", "{ : false }" },
7311 { "{ A[x] -> [NaN] }", "M", "{ A[x] : false }" },
7314 /* Perform basic isl_pw_aff_bind_id tests.
7316 static isl_stat test_bind_pa(isl_ctx *ctx)
7318 int i;
7320 for (i = 0; i < ARRAY_SIZE(bind_pa_tests); ++i) {
7321 isl_pw_aff *pa;
7322 isl_set *res;
7323 isl_id *id;
7324 isl_stat r;
7326 pa = isl_pw_aff_read_from_str(ctx, bind_pa_tests[i].pa);
7327 id = isl_id_read_from_str(ctx, bind_pa_tests[i].id);
7328 res = isl_pw_aff_bind_id(pa, id);
7329 r = set_check_equal(res, bind_pa_tests[i].res);
7330 isl_set_free(res);
7331 if (r < 0)
7332 return isl_stat_error;
7335 return isl_stat_ok;
7338 /* Inputs for isl_multi_union_pw_aff_bind tests.
7339 * "mupa" is the input expression.
7340 * "tuple" is the binding tuple.
7341 * "res" is the expected result.
7343 static
7344 struct {
7345 const char *mupa;
7346 const char *tuple;
7347 const char *res;
7348 } bind_mupa_tests[] = {
7349 { "A[{ [4] }, { [5] }]",
7350 "{ A[M, N] }",
7351 "[M = 4, N = 5] -> { : }" },
7352 { "A[{ B[x] -> [floor(x/2)] }, { B[y] -> [y + 5] }]",
7353 "{ A[M, N] }",
7354 "[M, N] -> { B[x] : M = floor(x/2) and N = x + 5 }" },
7355 { "[M] -> A[{ [4] }, { [M + 1] }]",
7356 "{ A[M, N] }",
7357 "[M = 4, N = 5] -> { : }" },
7360 /* Perform basic isl_multi_union_pw_aff_bind tests.
7362 static isl_stat test_bind_mupa(isl_ctx *ctx)
7364 int i;
7366 for (i = 0; i < ARRAY_SIZE(bind_mupa_tests); ++i) {
7367 const char *str;
7368 isl_multi_union_pw_aff *mupa;
7369 isl_union_set *res;
7370 isl_multi_id *tuple;
7371 isl_stat r;
7373 str = bind_mupa_tests[i].mupa;
7374 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7375 str = bind_mupa_tests[i].tuple;
7376 tuple = isl_multi_id_read_from_str(ctx, str);
7377 res = isl_multi_union_pw_aff_bind(mupa, tuple);
7378 r = uset_check_equal(res, bind_mupa_tests[i].res);
7379 isl_union_set_free(res);
7380 if (r < 0)
7381 return isl_stat_error;
7384 return isl_stat_ok;
7387 /* Perform tests that reinterpret dimensions as parameters.
7389 static int test_bind(isl_ctx *ctx)
7391 if (test_bind_set(ctx) < 0)
7392 return -1;
7393 if (test_bind_map_domain(ctx) < 0)
7394 return -1;
7395 if (test_bind_umap_range(ctx) < 0)
7396 return -1;
7397 if (test_bind_pma_domain(ctx) < 0)
7398 return -1;
7399 if (test_bind_pma_domain_wrapped(ctx) < 0)
7400 return -1;
7401 if (test_bind_aff(ctx) < 0)
7402 return -1;
7403 if (test_bind_pa(ctx) < 0)
7404 return -1;
7405 if (test_bind_mupa(ctx) < 0)
7406 return -1;
7408 return 0;
7411 /* Inputs for isl_set_unbind_params tests.
7412 * "set" is the input parameter domain.
7413 * "tuple" is the tuple of the constructed set.
7414 * "res" is the expected result.
7416 struct {
7417 const char *set;
7418 const char *tuple;
7419 const char *res;
7420 } unbind_set_tests[] = {
7421 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7422 "{ A[M, N] }",
7423 "{ A[M, N] : M mod 2 = 0 and N mod 8 = 3 }" },
7424 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7425 "{ B[N, M] }",
7426 "{ B[N, M] : M mod 2 = 0 and N mod 8 = 3 }" },
7427 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7428 "{ C[N] }",
7429 "[M] -> { C[N] : M mod 2 = 0 and N mod 8 = 3 }" },
7430 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7431 "{ D[T, N] }",
7432 "[M] -> { D[x, N] : M mod 2 = 0 and N mod 8 = 3 }" },
7435 /* Perform basic isl_set_unbind_params tests.
7437 static isl_stat test_unbind_set(isl_ctx *ctx)
7439 int i;
7441 for (i = 0; i < ARRAY_SIZE(unbind_set_tests); ++i) {
7442 const char *str;
7443 isl_set *set;
7444 isl_multi_id *tuple;
7445 isl_stat r;
7447 set = isl_set_read_from_str(ctx, unbind_set_tests[i].set);
7448 str = unbind_set_tests[i].tuple;
7449 tuple = isl_multi_id_read_from_str(ctx, str);
7450 set = isl_set_unbind_params(set, tuple);
7451 r = set_check_equal(set, unbind_set_tests[i].res);
7452 isl_set_free(set);
7453 if (r < 0)
7454 return isl_stat_error;
7457 return isl_stat_ok;
7460 /* Inputs for isl_aff_unbind_params_insert_domain tests.
7461 * "aff" is the input affine expression defined over a parameter domain.
7462 * "tuple" is the tuple of the domain that gets introduced.
7463 * "res" is the expected result.
7465 struct {
7466 const char *aff;
7467 const char *tuple;
7468 const char *res;
7469 } unbind_aff_tests[] = {
7470 { "[M, N] -> { [M + floor(N/2)] }",
7471 "{ A[M, N] }",
7472 "{ A[M, N] -> [M + floor(N/2)] }" },
7473 { "[M, N] -> { [M + floor(N/2)] }",
7474 "{ B[N, M] }",
7475 "{ B[N, M] -> [M + floor(N/2)] }" },
7476 { "[M, N] -> { [M + floor(N/2)] }",
7477 "{ C[N] }",
7478 "[M] -> { C[N] -> [M + floor(N/2)] }" },
7479 { "[M, N] -> { [M + floor(N/2)] }",
7480 "{ D[A, B, C, N, Z] }",
7481 "[M] -> { D[A, B, C, N, Z] -> [M + floor(N/2)] }" },
7484 /* Perform basic isl_aff_unbind_params_insert_domain tests.
7486 static isl_stat test_unbind_aff(isl_ctx *ctx)
7488 int i;
7490 for (i = 0; i < ARRAY_SIZE(unbind_aff_tests); ++i) {
7491 const char *str;
7492 isl_aff *aff;
7493 isl_multi_id *tuple;
7494 isl_stat r;
7496 aff = isl_aff_read_from_str(ctx, unbind_aff_tests[i].aff);
7497 str = unbind_aff_tests[i].tuple;
7498 tuple = isl_multi_id_read_from_str(ctx, str);
7499 aff = isl_aff_unbind_params_insert_domain(aff, tuple);
7500 r = aff_check_plain_equal(aff, unbind_aff_tests[i].res);
7501 isl_aff_free(aff);
7502 if (r < 0)
7503 return isl_stat_error;
7506 return isl_stat_ok;
7509 /* Inputs for isl_multi_aff_unbind_params_insert_domain tests.
7510 * "ma" is the input multi affine expression defined over a parameter domain.
7511 * "tuple" is the tuple of the domain that gets introduced.
7512 * "res" is the expected result.
7514 static struct {
7515 const char *ma;
7516 const char *tuple;
7517 const char *res;
7518 } unbind_multi_aff_tests[] = {
7519 { "[M, N] -> { T[M + floor(N/2)] }",
7520 "{ A[M, N] }",
7521 "{ A[M, N] -> T[M + floor(N/2)] }" },
7522 { "[M, N] -> { [M + floor(N/2)] }",
7523 "{ B[N, M] }",
7524 "{ B[N, M] -> [M + floor(N/2)] }" },
7525 { "[M, N] -> { [M + floor(N/2)] }",
7526 "{ C[N] }",
7527 "[M] -> { C[N] -> [M + floor(N/2)] }" },
7528 { "[M, N] -> { [M + floor(N/2)] }",
7529 "{ D[A, B, C, N, Z] }",
7530 "[M] -> { D[A, B, C, N, Z] -> [M + floor(N/2)] }" },
7531 { "[M, N] -> { T[M + floor(N/2), N + floor(M/3)] }",
7532 "{ A[M, N] }",
7533 "{ A[M, N] -> T[M + floor(N/2), N + floor(M/3)] }" },
7536 /* Perform basic isl_multi_aff_unbind_params_insert_domain tests.
7538 static isl_stat test_unbind_multi_aff(isl_ctx *ctx)
7540 int i;
7542 for (i = 0; i < ARRAY_SIZE(unbind_multi_aff_tests); ++i) {
7543 const char *str;
7544 isl_multi_aff *ma;
7545 isl_multi_id *tuple;
7546 isl_stat r;
7548 str = unbind_multi_aff_tests[i].ma;
7549 ma = isl_multi_aff_read_from_str(ctx, str);
7550 str = unbind_multi_aff_tests[i].tuple;
7551 tuple = isl_multi_id_read_from_str(ctx, str);
7552 ma = isl_multi_aff_unbind_params_insert_domain(ma, tuple);
7553 str = unbind_multi_aff_tests[i].res;
7554 r = multi_aff_check_plain_equal(ma, str);
7555 isl_multi_aff_free(ma);
7556 if (r < 0)
7557 return isl_stat_error;
7560 return isl_stat_ok;
7563 /* Perform tests that reinterpret parameters.
7565 static int test_unbind(isl_ctx *ctx)
7567 if (test_unbind_set(ctx) < 0)
7568 return -1;
7569 if (test_unbind_aff(ctx) < 0)
7570 return -1;
7571 if (test_unbind_multi_aff(ctx) < 0)
7572 return -1;
7574 return 0;
7577 /* Check that "pa" consists of a single expression.
7579 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
7581 isl_size n;
7583 n = isl_pw_aff_n_piece(pa);
7584 isl_pw_aff_free(pa);
7586 if (n < 0)
7587 return -1;
7588 if (n != 1)
7589 isl_die(ctx, isl_error_unknown, "expecting single expression",
7590 return -1);
7592 return 0;
7595 /* Check that the computation below results in a single expression.
7596 * One or two expressions may result depending on which constraint
7597 * ends up being considered as redundant with respect to the other
7598 * constraints after the projection that is performed internally
7599 * by isl_set_dim_min.
7601 static int test_dim_max_1(isl_ctx *ctx)
7603 const char *str;
7604 isl_set *set;
7605 isl_pw_aff *pa;
7607 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
7608 "-4a <= b <= 3 and b < n - 4a }";
7609 set = isl_set_read_from_str(ctx, str);
7610 pa = isl_set_dim_min(set, 0);
7611 return check_single_piece(ctx, pa);
7614 /* Check that the computation below results in a single expression.
7615 * The PIP problem corresponding to these constraints has a row
7616 * that causes a split of the solution domain. The solver should
7617 * first pick rows that split off empty parts such that the actual
7618 * solution domain does not get split.
7619 * Note that the description contains some redundant constraints.
7620 * If these constraints get removed first, then the row mentioned
7621 * above does not appear in the PIP problem.
7623 static int test_dim_max_2(isl_ctx *ctx)
7625 const char *str;
7626 isl_set *set;
7627 isl_pw_aff *pa;
7629 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
7630 "N > 0 and P >= 0 }";
7631 set = isl_set_read_from_str(ctx, str);
7632 pa = isl_set_dim_max(set, 0);
7633 return check_single_piece(ctx, pa);
7636 int test_dim_max(isl_ctx *ctx)
7638 int equal;
7639 const char *str;
7640 isl_set *set1, *set2;
7641 isl_set *set;
7642 isl_map *map;
7643 isl_pw_aff *pwaff;
7645 if (test_dim_max_1(ctx) < 0)
7646 return -1;
7647 if (test_dim_max_2(ctx) < 0)
7648 return -1;
7650 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
7651 set = isl_set_read_from_str(ctx, str);
7652 pwaff = isl_set_dim_max(set, 0);
7653 set1 = isl_set_from_pw_aff(pwaff);
7654 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
7655 set2 = isl_set_read_from_str(ctx, str);
7656 equal = isl_set_is_equal(set1, set2);
7657 isl_set_free(set1);
7658 isl_set_free(set2);
7659 if (equal < 0)
7660 return -1;
7661 if (!equal)
7662 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7664 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
7665 set = isl_set_read_from_str(ctx, str);
7666 pwaff = isl_set_dim_max(set, 0);
7667 set1 = isl_set_from_pw_aff(pwaff);
7668 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
7669 set2 = isl_set_read_from_str(ctx, str);
7670 equal = isl_set_is_equal(set1, set2);
7671 isl_set_free(set1);
7672 isl_set_free(set2);
7673 if (equal < 0)
7674 return -1;
7675 if (!equal)
7676 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7678 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
7679 set = isl_set_read_from_str(ctx, str);
7680 pwaff = isl_set_dim_max(set, 0);
7681 set1 = isl_set_from_pw_aff(pwaff);
7682 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
7683 set2 = isl_set_read_from_str(ctx, str);
7684 equal = isl_set_is_equal(set1, set2);
7685 isl_set_free(set1);
7686 isl_set_free(set2);
7687 if (equal < 0)
7688 return -1;
7689 if (!equal)
7690 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7692 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
7693 "0 <= i < N and 0 <= j < M }";
7694 map = isl_map_read_from_str(ctx, str);
7695 set = isl_map_range(map);
7697 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
7698 set1 = isl_set_from_pw_aff(pwaff);
7699 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
7700 set2 = isl_set_read_from_str(ctx, str);
7701 equal = isl_set_is_equal(set1, set2);
7702 isl_set_free(set1);
7703 isl_set_free(set2);
7705 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
7706 set1 = isl_set_from_pw_aff(pwaff);
7707 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
7708 set2 = isl_set_read_from_str(ctx, str);
7709 if (equal >= 0 && equal)
7710 equal = isl_set_is_equal(set1, set2);
7711 isl_set_free(set1);
7712 isl_set_free(set2);
7714 isl_set_free(set);
7716 if (equal < 0)
7717 return -1;
7718 if (!equal)
7719 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7721 /* Check that solutions are properly merged. */
7722 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
7723 "c <= -1 + n - 4a - 2b and c >= -2b and "
7724 "4a >= -4 + n and c >= 0 }";
7725 set = isl_set_read_from_str(ctx, str);
7726 pwaff = isl_set_dim_min(set, 2);
7727 set1 = isl_set_from_pw_aff(pwaff);
7728 str = "[n] -> { [(0)] : n >= 1 }";
7729 set2 = isl_set_read_from_str(ctx, str);
7730 equal = isl_set_is_equal(set1, set2);
7731 isl_set_free(set1);
7732 isl_set_free(set2);
7734 if (equal < 0)
7735 return -1;
7736 if (!equal)
7737 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7739 /* Check that empty solution lie in the right space. */
7740 str = "[n] -> { [t,a] : 1 = 0 }";
7741 set = isl_set_read_from_str(ctx, str);
7742 pwaff = isl_set_dim_max(set, 0);
7743 set1 = isl_set_from_pw_aff(pwaff);
7744 str = "[n] -> { [t] : 1 = 0 }";
7745 set2 = isl_set_read_from_str(ctx, str);
7746 equal = isl_set_is_equal(set1, set2);
7747 isl_set_free(set1);
7748 isl_set_free(set2);
7750 if (equal < 0)
7751 return -1;
7752 if (!equal)
7753 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7755 return 0;
7758 /* Basic test for isl_pw_multi_aff_product.
7760 * Check that multiple pieces are properly handled.
7762 static int test_product_pma(isl_ctx *ctx)
7764 isl_stat equal;
7765 const char *str;
7766 isl_pw_multi_aff *pma1, *pma2;
7768 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
7769 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
7770 str = "{ C[] -> D[] }";
7771 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
7772 pma1 = isl_pw_multi_aff_product(pma1, pma2);
7773 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
7774 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
7775 equal = pw_multi_aff_check_plain_equal(pma1, str);
7776 isl_pw_multi_aff_free(pma1);
7777 if (equal < 0)
7778 return -1;
7780 return 0;
7783 int test_product(isl_ctx *ctx)
7785 const char *str;
7786 isl_set *set;
7787 isl_union_set *uset1, *uset2;
7788 int ok;
7790 str = "{ A[i] }";
7791 set = isl_set_read_from_str(ctx, str);
7792 set = isl_set_product(set, isl_set_copy(set));
7793 ok = isl_set_is_wrapping(set);
7794 isl_set_free(set);
7795 if (ok < 0)
7796 return -1;
7797 if (!ok)
7798 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7800 str = "{ [] }";
7801 uset1 = isl_union_set_read_from_str(ctx, str);
7802 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
7803 str = "{ [[] -> []] }";
7804 uset2 = isl_union_set_read_from_str(ctx, str);
7805 ok = isl_union_set_is_equal(uset1, uset2);
7806 isl_union_set_free(uset1);
7807 isl_union_set_free(uset2);
7808 if (ok < 0)
7809 return -1;
7810 if (!ok)
7811 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7813 if (test_product_pma(ctx) < 0)
7814 return -1;
7816 return 0;
7819 /* Check that two sets are not considered disjoint just because
7820 * they have a different set of (named) parameters.
7822 static int test_disjoint(isl_ctx *ctx)
7824 const char *str;
7825 isl_set *set, *set2;
7826 int disjoint;
7828 str = "[n] -> { [[]->[]] }";
7829 set = isl_set_read_from_str(ctx, str);
7830 str = "{ [[]->[]] }";
7831 set2 = isl_set_read_from_str(ctx, str);
7832 disjoint = isl_set_is_disjoint(set, set2);
7833 isl_set_free(set);
7834 isl_set_free(set2);
7835 if (disjoint < 0)
7836 return -1;
7837 if (disjoint)
7838 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7840 return 0;
7843 /* Inputs for isl_pw_multi_aff_is_equal tests.
7844 * "f1" and "f2" are the two function that need to be compared.
7845 * "equal" is the expected result.
7847 struct {
7848 int equal;
7849 const char *f1;
7850 const char *f2;
7851 } pma_equal_tests[] = {
7852 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
7853 "[N] -> { [0] : 0 <= N <= 1 }" },
7854 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
7855 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
7856 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
7857 "[N] -> { [0] : 0 <= N <= 1 }" },
7858 { 0, "{ [NaN] }", "{ [NaN] }" },
7861 int test_equal(isl_ctx *ctx)
7863 int i;
7864 const char *str;
7865 isl_set *set, *set2;
7866 int equal;
7868 str = "{ S_6[i] }";
7869 set = isl_set_read_from_str(ctx, str);
7870 str = "{ S_7[i] }";
7871 set2 = isl_set_read_from_str(ctx, str);
7872 equal = isl_set_is_equal(set, set2);
7873 isl_set_free(set);
7874 isl_set_free(set2);
7875 if (equal < 0)
7876 return -1;
7877 if (equal)
7878 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7880 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
7881 int expected = pma_equal_tests[i].equal;
7882 isl_pw_multi_aff *f1, *f2;
7884 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
7885 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
7886 equal = isl_pw_multi_aff_is_equal(f1, f2);
7887 isl_pw_multi_aff_free(f1);
7888 isl_pw_multi_aff_free(f2);
7889 if (equal < 0)
7890 return -1;
7891 if (equal != expected)
7892 isl_die(ctx, isl_error_unknown,
7893 "unexpected equality result", return -1);
7896 return 0;
7899 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
7900 enum isl_dim_type type, unsigned pos, int fixed)
7902 isl_bool test;
7904 test = isl_map_plain_is_fixed(map, type, pos, NULL);
7905 isl_map_free(map);
7906 if (test < 0)
7907 return -1;
7908 if (test == fixed)
7909 return 0;
7910 if (fixed)
7911 isl_die(ctx, isl_error_unknown,
7912 "map not detected as fixed", return -1);
7913 else
7914 isl_die(ctx, isl_error_unknown,
7915 "map detected as fixed", return -1);
7918 int test_fixed(isl_ctx *ctx)
7920 const char *str;
7921 isl_map *map;
7923 str = "{ [i] -> [i] }";
7924 map = isl_map_read_from_str(ctx, str);
7925 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
7926 return -1;
7927 str = "{ [i] -> [1] }";
7928 map = isl_map_read_from_str(ctx, str);
7929 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7930 return -1;
7931 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
7932 map = isl_map_read_from_str(ctx, str);
7933 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7934 return -1;
7935 map = isl_map_read_from_str(ctx, str);
7936 map = isl_map_neg(map);
7937 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7938 return -1;
7940 return 0;
7943 struct isl_vertices_test_data {
7944 const char *set;
7945 int n;
7946 const char *vertex[6];
7947 } vertices_tests[] = {
7948 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
7949 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
7950 { "{ A[t, i] : t = 14 and i = 1 }",
7951 1, { "{ A[14, 1] }" } },
7952 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
7953 "c <= m and m <= n and m > 0 }",
7954 6, {
7955 "[n, m] -> { [n, m, m] : 0 < m <= n }",
7956 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
7957 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
7958 "[n, m] -> { [m, m, m] : 0 < m <= n }",
7959 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
7960 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
7961 } },
7962 /* An input with implicit equality constraints among the parameters. */
7963 { "[N, M] -> { [a, b] : M >= 3 and 9 + 3M <= a <= 29 + 2N + 11M and "
7964 "2b >= M + a and 5 - 2N - M + a <= 2b <= 3 + a and "
7965 "3b >= 15 + a }",
7966 2, {
7967 "[N, M] -> { [(21), (12)] : M = 3 and N >= 0 }",
7968 "[N, M] -> { [(61 + 2N), (32 + N)] : M = 3 and N >= 0 }",
7973 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
7975 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
7977 struct isl_vertices_test_data *data = user;
7978 isl_ctx *ctx;
7979 isl_multi_aff *ma;
7980 isl_basic_set *bset;
7981 isl_pw_multi_aff *pma;
7982 int i;
7983 isl_bool equal;
7985 ctx = isl_vertex_get_ctx(vertex);
7986 bset = isl_vertex_get_domain(vertex);
7987 ma = isl_vertex_get_expr(vertex);
7988 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
7990 for (i = 0; i < data->n; ++i) {
7991 isl_pw_multi_aff *pma_i;
7993 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
7994 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
7995 isl_pw_multi_aff_free(pma_i);
7997 if (equal < 0 || equal)
7998 break;
8001 isl_pw_multi_aff_free(pma);
8002 isl_vertex_free(vertex);
8004 if (equal < 0)
8005 return isl_stat_error;
8007 return equal ? isl_stat_ok : isl_stat_error;
8010 int test_vertices(isl_ctx *ctx)
8012 int i;
8014 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
8015 isl_basic_set *bset;
8016 isl_vertices *vertices;
8017 int ok = 1;
8018 isl_size n;
8020 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
8021 vertices = isl_basic_set_compute_vertices(bset);
8022 n = isl_vertices_get_n_vertices(vertices);
8023 if (vertices_tests[i].n != n)
8024 ok = 0;
8025 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
8026 &vertices_tests[i]) < 0)
8027 ok = 0;
8028 isl_vertices_free(vertices);
8029 isl_basic_set_free(bset);
8031 if (n < 0)
8032 return -1;
8033 if (!ok)
8034 isl_die(ctx, isl_error_unknown, "unexpected vertices",
8035 return -1);
8038 return 0;
8041 /* Inputs for basic tests of unary operations on isl_union_map.
8042 * "fn" is the function that is being tested.
8043 * "arg" is a string description of the input.
8044 * "res" is a string description of the expected result.
8046 static struct {
8047 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap);
8048 const char *arg;
8049 const char *res;
8050 } umap_un_tests[] = {
8051 { &isl_union_map_range_reverse,
8052 "{ A[] -> [B[] -> C[]]; A[] -> B[]; A[0] -> N[B[1] -> B[2]] }",
8053 "{ A[] -> [C[] -> B[]]; A[0] -> N[B[2] -> B[1]] }" },
8054 { &isl_union_map_range_reverse,
8055 "{ A[] -> N[B[] -> C[]] }",
8056 "{ A[] -> [C[] -> B[]] }" },
8057 { &isl_union_map_range_reverse,
8058 "{ A[] -> N[B[x] -> B[y]] }",
8059 "{ A[] -> N[B[*] -> B[*]] }" },
8062 /* Perform basic tests of unary operations on isl_union_map.
8064 static isl_stat test_un_union_map(isl_ctx *ctx)
8066 int i;
8068 for (i = 0; i < ARRAY_SIZE(umap_un_tests); ++i) {
8069 const char *str;
8070 isl_union_map *umap, *res;
8071 isl_bool equal;
8073 str = umap_un_tests[i].arg;
8074 umap = isl_union_map_read_from_str(ctx, str);
8075 str = umap_un_tests[i].res;
8076 res = isl_union_map_read_from_str(ctx, str);
8077 umap = umap_un_tests[i].fn(umap);
8078 equal = isl_union_map_is_equal(umap, res);
8079 isl_union_map_free(umap);
8080 isl_union_map_free(res);
8081 if (equal < 0)
8082 return isl_stat_error;
8083 if (!equal)
8084 isl_die(ctx, isl_error_unknown,
8085 "unexpected result", return isl_stat_error);
8088 return isl_stat_ok;
8091 /* Inputs for basic tests of binary operations on isl_union_map.
8092 * "fn" is the function that is being tested.
8093 * "arg1" and "arg2" are string descriptions of the inputs.
8094 * "res" is a string description of the expected result.
8096 static struct {
8097 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap1,
8098 __isl_take isl_union_map *umap2);
8099 const char *arg1;
8100 const char *arg2;
8101 const char *res;
8102 } umap_bin_tests[] = {
8103 { &isl_union_map_intersect,
8104 "[n] -> { A[i] -> [] : 0 <= i <= n; B[] -> [] }",
8105 "[m] -> { A[i] -> [] : 0 <= i <= m; C[] -> [] }",
8106 "[m, n] -> { A[i] -> [] : 0 <= i <= n and i <= m }" },
8107 { &isl_union_map_intersect_domain_factor_domain,
8108 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8109 "[N] -> { B[i] -> C[N] }",
8110 "{ }" },
8111 { &isl_union_map_intersect_domain_factor_domain,
8112 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8113 "[N] -> { A[i] -> C[N] }",
8114 "[N] -> { [A[N - 2] -> B[N - 1]] -> C[N] }" },
8115 { &isl_union_map_intersect_domain_factor_domain,
8116 "{ T[A[i] -> B[i + 1]] -> C[i + 2] }",
8117 "[N] -> { A[i] -> C[N] }",
8118 "[N] -> { T[A[N - 2] -> B[N - 1]] -> C[N] }" },
8119 { &isl_union_map_intersect_domain_factor_range,
8120 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8121 "[N] -> { B[i] -> C[N] }",
8122 "[N] -> { [A[N - 2] -> B[N - 1]] -> C[N] }" },
8123 { &isl_union_map_intersect_domain_factor_range,
8124 "{ T[A[i] -> B[i + 1]] -> C[i + 2] }",
8125 "[N] -> { B[i] -> C[N] }",
8126 "[N] -> { T[A[N - 2] -> B[N - 1]] -> C[N] }" },
8127 { &isl_union_map_intersect_domain_factor_range,
8128 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8129 "[N] -> { A[i] -> C[N] }",
8130 "{ }" },
8131 { &isl_union_map_intersect_range_factor_domain,
8132 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8133 "[N] -> { A[i] -> B[N] }",
8134 "[N] -> { A[N - 1] -> [B[N] -> C[N + 1]] }" },
8135 { &isl_union_map_intersect_range_factor_domain,
8136 "{ A[i] -> T[B[i + 1] -> C[i + 2]] }",
8137 "[N] -> { A[i] -> B[N] }",
8138 "[N] -> { A[N - 1] -> T[B[N] -> C[N + 1]] }" },
8139 { &isl_union_map_intersect_range_factor_domain,
8140 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8141 "[N] -> { A[i] -> C[N] }",
8142 "{ }" },
8143 { &isl_union_map_intersect_range_factor_range,
8144 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8145 "[N] -> { A[i] -> C[N] }",
8146 "[N] -> { A[N - 2] -> [B[N - 1] -> C[N]] }" },
8147 { &isl_union_map_intersect_range_factor_range,
8148 "{ A[i] -> T[B[i + 1] -> C[i + 2]] }",
8149 "[N] -> { A[i] -> C[N] }",
8150 "[N] -> { A[N - 2] -> T[B[N - 1] -> C[N]] }" },
8151 { &isl_union_map_intersect_range_factor_range,
8152 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8153 "[N] -> { A[i] -> B[N] }",
8154 "{ }" },
8157 /* Perform basic tests of binary operations on isl_union_map.
8159 static isl_stat test_bin_union_map(isl_ctx *ctx)
8161 int i;
8163 for (i = 0; i < ARRAY_SIZE(umap_bin_tests); ++i) {
8164 const char *str;
8165 isl_union_map *umap1, *umap2, *res;
8166 isl_bool equal;
8168 str = umap_bin_tests[i].arg1;
8169 umap1 = isl_union_map_read_from_str(ctx, str);
8170 str = umap_bin_tests[i].arg2;
8171 umap2 = isl_union_map_read_from_str(ctx, str);
8172 str = umap_bin_tests[i].res;
8173 res = isl_union_map_read_from_str(ctx, str);
8174 umap1 = umap_bin_tests[i].fn(umap1, umap2);
8175 equal = isl_union_map_is_equal(umap1, res);
8176 isl_union_map_free(umap1);
8177 isl_union_map_free(res);
8178 if (equal < 0)
8179 return isl_stat_error;
8180 if (!equal)
8181 isl_die(ctx, isl_error_unknown,
8182 "unexpected result", return isl_stat_error);
8185 return isl_stat_ok;
8188 /* Check that isl_union_set_contains finds space independently
8189 * of the parameters.
8191 static isl_stat test_union_set_contains(isl_ctx *ctx)
8193 const char *str;
8194 isl_bool ok;
8195 isl_space *space;
8196 isl_id *id;
8197 isl_union_set *uset;
8199 str = "[N] -> { A[0:N]; B[*,*] }";
8200 uset = isl_union_set_read_from_str(ctx, str);
8201 space = isl_space_unit(ctx);
8202 id = isl_id_alloc(ctx, "A", NULL);
8203 space = isl_space_add_named_tuple_id_ui(space, id, 1);
8204 ok = isl_union_set_contains(uset, space);
8205 isl_space_free(space);
8206 isl_union_set_free(uset);
8208 if (ok < 0)
8209 return isl_stat_error;
8210 if (!ok)
8211 isl_die(ctx, isl_error_unknown,
8212 "unexpected result", return isl_stat_error);
8214 return isl_stat_ok;
8217 /* Perform basic tests of operations on isl_union_map or isl_union_set.
8219 static int test_union_map(isl_ctx *ctx)
8221 if (test_un_union_map(ctx) < 0)
8222 return -1;
8223 if (test_bin_union_map(ctx) < 0)
8224 return -1;
8225 if (test_union_set_contains(ctx) < 0)
8226 return -1;
8227 return 0;
8230 int test_union_pw(isl_ctx *ctx)
8232 int equal;
8233 const char *str;
8234 isl_union_set *uset;
8235 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
8237 str = "{ [x] -> x^2 }";
8238 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
8239 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
8240 uset = isl_union_pw_qpolynomial_domain(upwqp1);
8241 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
8242 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
8243 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
8244 isl_union_pw_qpolynomial_free(upwqp1);
8245 isl_union_pw_qpolynomial_free(upwqp2);
8246 if (equal < 0)
8247 return -1;
8248 if (!equal)
8249 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
8251 return 0;
8254 /* Inputs for basic tests of functions that select
8255 * subparts of the domain of an isl_multi_union_pw_aff.
8256 * "fn" is the function that is tested.
8257 * "arg" is a string description of the input.
8258 * "res" is a string description of the expected result.
8260 struct {
8261 __isl_give isl_union_set *(*fn)(
8262 __isl_take isl_multi_union_pw_aff *mupa);
8263 const char *arg;
8264 const char *res;
8265 } un_locus_tests[] = {
8266 { &isl_multi_union_pw_aff_zero_union_set,
8267 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
8268 "{ A[0,j]; B[0,j] }" },
8269 { &isl_multi_union_pw_aff_zero_union_set,
8270 "F[{ A[i,j] -> [i-j]; B[i,j] -> [i-j] : i >= 0 }]",
8271 "{ A[i,i]; B[i,i] : i >= 0 }" },
8272 { &isl_multi_union_pw_aff_zero_union_set,
8273 "(F[] : { A[i,j]; B[i,i] : i >= 0 })",
8274 "{ A[i,j]; B[i,i] : i >= 0 }" },
8277 /* Perform some basic tests of functions that select
8278 * subparts of the domain of an isl_multi_union_pw_aff.
8280 static int test_un_locus(isl_ctx *ctx)
8282 int i;
8283 isl_bool ok;
8284 isl_union_set *uset, *res;
8285 isl_multi_union_pw_aff *mupa;
8287 for (i = 0; i < ARRAY_SIZE(un_locus_tests); ++i) {
8288 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
8289 un_locus_tests[i].arg);
8290 res = isl_union_set_read_from_str(ctx, un_locus_tests[i].res);
8291 uset = un_locus_tests[i].fn(mupa);
8292 ok = isl_union_set_is_equal(uset, res);
8293 isl_union_set_free(uset);
8294 isl_union_set_free(res);
8295 if (ok < 0)
8296 return -1;
8297 if (!ok)
8298 isl_die(ctx, isl_error_unknown,
8299 "unexpected result", return -1);
8302 return 0;
8305 /* Inputs for basic tests of functions that select
8306 * subparts of an isl_union_map based on a relation
8307 * specified by an isl_multi_union_pw_aff.
8308 * "fn" is the function that is tested.
8309 * "arg1" and "arg2" are string descriptions of the inputs.
8310 * "res" is a string description of the expected result.
8312 struct {
8313 __isl_give isl_union_map *(*fn)(
8314 __isl_take isl_union_map *umap,
8315 __isl_take isl_multi_union_pw_aff *mupa);
8316 const char *arg1;
8317 const char *arg2;
8318 const char *res;
8319 } bin_locus_tests[] = {
8320 { &isl_union_map_eq_at_multi_union_pw_aff,
8321 "{ A[i,j] -> B[i',j'] }",
8322 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
8323 "{ A[i,j] -> B[i,j'] }" },
8324 { &isl_union_map_eq_at_multi_union_pw_aff,
8325 "{ A[i,j] -> B[i',j'] }",
8326 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8327 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8328 "{ A[i,j] -> B[i,j] }" },
8329 { &isl_union_map_eq_at_multi_union_pw_aff,
8330 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
8331 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
8332 "{ A[i,j] -> B[i,j'] }" },
8333 { &isl_union_map_eq_at_multi_union_pw_aff,
8334 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
8335 "F[{ A[i,j] -> [i]; B[i,j] -> [i]; C[i,j] -> [0] }]",
8336 "{ A[i,j] -> B[i,j']; A[0,j] -> C[i',j'] }" },
8337 { &isl_union_map_eq_at_multi_union_pw_aff,
8338 "{ A[i,j] -> B[i',j'] }",
8339 "F[{ A[i,j] -> [i] : i > j; B[i,j] -> [i] }]",
8340 "{ A[i,j] -> B[i,j'] : i > j }" },
8341 { &isl_union_map_lex_le_at_multi_union_pw_aff,
8342 "{ A[i,j] -> B[i',j'] }",
8343 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8344 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8345 "{ A[i,j] -> B[i',j'] : i,j <<= i',j' }" },
8346 { &isl_union_map_lex_lt_at_multi_union_pw_aff,
8347 "{ A[i,j] -> B[i',j'] }",
8348 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8349 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8350 "{ A[i,j] -> B[i',j'] : i,j << i',j' }" },
8351 { &isl_union_map_lex_ge_at_multi_union_pw_aff,
8352 "{ A[i,j] -> B[i',j'] }",
8353 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8354 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8355 "{ A[i,j] -> B[i',j'] : i,j >>= i',j' }" },
8356 { &isl_union_map_lex_gt_at_multi_union_pw_aff,
8357 "{ A[i,j] -> B[i',j'] }",
8358 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8359 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8360 "{ A[i,j] -> B[i',j'] : i,j >> i',j' }" },
8361 { &isl_union_map_eq_at_multi_union_pw_aff,
8362 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
8363 "(F[] : { A[i,j]; B[i,j] })",
8364 "{ A[i,j] -> B[i',j'] }" },
8365 { &isl_union_map_eq_at_multi_union_pw_aff,
8366 "{ A[i,j] -> B[i',j'] }",
8367 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
8368 "{ A[i,j] -> B[i',j'] : i > j and i' < j' }" },
8369 { &isl_union_map_eq_at_multi_union_pw_aff,
8370 "[N] -> { A[i,j] -> B[i',j'] : i,i' <= N }",
8371 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
8372 "[N] -> { A[i,j] -> B[i',j'] : i > j and i' < j' and i,i' <= N }" },
8373 { &isl_union_map_eq_at_multi_union_pw_aff,
8374 "{ A[i,j] -> B[i',j'] }",
8375 "[N] -> (F[] : { A[i,j] : i < N; B[i,j] : i < N })",
8376 "[N] -> { A[i,j] -> B[i',j'] : i,i' < N }" },
8377 { &isl_union_map_eq_at_multi_union_pw_aff,
8378 "{ A[i,j] -> B[i',j'] }",
8379 "[N] -> (F[] : { : N >= 0 })",
8380 "[N] -> { A[i,j] -> B[i',j'] : N >= 0 }" },
8383 /* Perform some basic tests of functions that select
8384 * subparts of an isl_union_map based on a relation
8385 * specified by an isl_multi_union_pw_aff.
8387 static int test_bin_locus(isl_ctx *ctx)
8389 int i;
8390 isl_bool ok;
8391 isl_union_map *umap, *res;
8392 isl_multi_union_pw_aff *mupa;
8394 for (i = 0; i < ARRAY_SIZE(bin_locus_tests); ++i) {
8395 umap = isl_union_map_read_from_str(ctx,
8396 bin_locus_tests[i].arg1);
8397 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
8398 bin_locus_tests[i].arg2);
8399 res = isl_union_map_read_from_str(ctx, bin_locus_tests[i].res);
8400 umap = bin_locus_tests[i].fn(umap, mupa);
8401 ok = isl_union_map_is_equal(umap, res);
8402 isl_union_map_free(umap);
8403 isl_union_map_free(res);
8404 if (ok < 0)
8405 return -1;
8406 if (!ok)
8407 isl_die(ctx, isl_error_unknown,
8408 "unexpected result", return -1);
8411 return 0;
8414 /* Inputs for basic tests of functions that determine
8415 * the part of the domain where two isl_multi_aff objects
8416 * related to each other in a specific way.
8417 * "fn" is the function that is being tested.
8418 * "arg1" and "arg2" are string descriptions of the inputs.
8419 * "res" is a string description of the expected result.
8421 static struct {
8422 __isl_give isl_set *(*fn)(__isl_take isl_multi_aff *ma1,
8423 __isl_take isl_multi_aff *ma2);
8424 const char *arg1;
8425 const char *arg2;
8426 const char *res;
8427 } bin_locus_ma_tests[] = {
8428 { &isl_multi_aff_lex_le_set, "{ [] }", "{ [] }", "{ : }" },
8429 { &isl_multi_aff_lex_lt_set, "{ [] }", "{ [] }", "{ : false }" },
8430 { &isl_multi_aff_lex_le_set,
8431 "{ A[i] -> [i] }", "{ A[i] -> [0] }",
8432 "{ A[i] : i <= 0 }" },
8433 { &isl_multi_aff_lex_lt_set,
8434 "{ A[i] -> [i] }", "{ A[i] -> [0] }",
8435 "{ A[i] : i < 0 }" },
8436 { &isl_multi_aff_lex_le_set,
8437 "{ A[i] -> [i, i] }", "{ A[i] -> [0, 0] }",
8438 "{ A[i] : i <= 0 }" },
8439 { &isl_multi_aff_lex_le_set,
8440 "{ A[i] -> [i, 0] }", "{ A[i] -> [0, 0] }",
8441 "{ A[i] : i <= 0 }" },
8442 { &isl_multi_aff_lex_le_set,
8443 "{ A[i] -> [i, 1] }", "{ A[i] -> [0, 0] }",
8444 "{ A[i] : i < 0 }" },
8447 /* Perform some basic tests of functions that determine
8448 * the part of the domain where two isl_multi_aff objects
8449 * related to each other in a specific way.
8451 static isl_stat test_bin_locus_ma(isl_ctx *ctx)
8453 int i;
8455 for (i = 0; i < ARRAY_SIZE(bin_locus_ma_tests); ++i) {
8456 const char *str;
8457 isl_bool ok;
8458 isl_multi_aff *ma1, *ma2;
8459 isl_set *set, *res;
8461 str = bin_locus_ma_tests[i].arg1;
8462 ma1 = isl_multi_aff_read_from_str(ctx, str);
8463 str = bin_locus_ma_tests[i].arg2;
8464 ma2 = isl_multi_aff_read_from_str(ctx, str);
8465 res = isl_set_read_from_str(ctx, bin_locus_ma_tests[i].res);
8466 set = bin_locus_ma_tests[i].fn(ma1, ma2);
8467 ok = isl_set_is_equal(set, res);
8468 isl_set_free(set);
8469 isl_set_free(res);
8470 if (ok < 0)
8471 return isl_stat_error;
8472 if (!ok)
8473 isl_die(ctx, isl_error_unknown,
8474 "unexpected result", return isl_stat_error);
8477 return isl_stat_ok;
8480 /* Perform basic locus tests.
8482 static int test_locus(isl_ctx *ctx)
8484 if (test_un_locus(ctx) < 0)
8485 return -1;
8486 if (test_bin_locus(ctx) < 0)
8487 return -1;
8488 if (test_bin_locus_ma(ctx) < 0)
8489 return -1;
8490 return 0;
8493 /* Test that isl_union_pw_qpolynomial_eval picks up the function
8494 * defined over the correct domain space.
8496 static int test_eval_1(isl_ctx *ctx)
8498 const char *str;
8499 isl_point *pnt;
8500 isl_set *set;
8501 isl_union_pw_qpolynomial *upwqp;
8502 isl_val *v;
8503 int cmp;
8505 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
8506 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
8507 str = "{ A[6] }";
8508 set = isl_set_read_from_str(ctx, str);
8509 pnt = isl_set_sample_point(set);
8510 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
8511 cmp = isl_val_cmp_si(v, 36);
8512 isl_val_free(v);
8514 if (!v)
8515 return -1;
8516 if (cmp != 0)
8517 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
8519 return 0;
8522 /* Check that isl_qpolynomial_eval handles getting called on a void point.
8524 static int test_eval_2(isl_ctx *ctx)
8526 const char *str;
8527 isl_point *pnt;
8528 isl_set *set;
8529 isl_qpolynomial *qp;
8530 isl_val *v;
8531 isl_bool ok;
8533 str = "{ A[x] -> [x] }";
8534 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
8535 str = "{ A[x] : false }";
8536 set = isl_set_read_from_str(ctx, str);
8537 pnt = isl_set_sample_point(set);
8538 v = isl_qpolynomial_eval(qp, pnt);
8539 ok = isl_val_is_nan(v);
8540 isl_val_free(v);
8542 if (ok < 0)
8543 return -1;
8544 if (!ok)
8545 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
8547 return 0;
8550 /* Check that a polynomial (without local variables) can be evaluated
8551 * in a rational point.
8553 static isl_stat test_eval_3(isl_ctx *ctx)
8555 isl_pw_qpolynomial *pwqp;
8556 isl_point *pnt;
8557 isl_val *v;
8558 isl_stat r;
8560 pwqp = isl_pw_qpolynomial_read_from_str(ctx, "{ [x] -> x^2 }");
8561 pnt = isl_point_zero(isl_pw_qpolynomial_get_domain_space(pwqp));
8562 v = isl_val_read_from_str(ctx, "1/2");
8563 pnt = isl_point_set_coordinate_val(pnt, isl_dim_set, 0, v);
8564 v = isl_pw_qpolynomial_eval(pwqp, pnt);
8565 r = val_check_equal(v, "1/4");
8566 isl_val_free(v);
8568 return r;
8571 /* Inputs for isl_pw_aff_eval test.
8572 * "f" is the affine function.
8573 * "p" is the point where the function should be evaluated.
8574 * "res" is the expected result.
8576 struct {
8577 const char *f;
8578 const char *p;
8579 const char *res;
8580 } aff_eval_tests[] = {
8581 { "{ [i] -> [2 * i] }", "{ [4] }", "8" },
8582 { "{ [i] -> [2 * i] }", "{ [x] : false }", "NaN" },
8583 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [0] }", "0" },
8584 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [1] }", "1" },
8585 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [2] }", "3" },
8586 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [3] }", "5" },
8587 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [4] }", "7" },
8588 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [0] }", "0" },
8589 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [1] }", "0" },
8590 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [2] }", "0" },
8591 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [3] }", "0" },
8592 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [4] }", "1" },
8593 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [6] }", "1" },
8594 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [8] }", "2" },
8595 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [4] }", "4" },
8596 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [-2] }", "2" },
8597 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [0] }", "NaN" },
8598 { "[N] -> { [2 * N] }", "[N] -> { : N = 4 }", "8" },
8599 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 1] }", "1" },
8600 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 2] }", "3/2" },
8601 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [4] }", "4" },
8602 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [3] }", "NaN" },
8603 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [x] : false }", "NaN" },
8604 { "[m, n] -> { [2m + 3n] }", "[n=1, m=10] -> { : }", "23" },
8607 /* Perform basic isl_pw_aff_eval tests.
8609 static int test_eval_aff(isl_ctx *ctx)
8611 int i;
8613 for (i = 0; i < ARRAY_SIZE(aff_eval_tests); ++i) {
8614 isl_stat r;
8615 isl_pw_aff *pa;
8616 isl_set *set;
8617 isl_point *pnt;
8618 isl_val *v;
8620 pa = isl_pw_aff_read_from_str(ctx, aff_eval_tests[i].f);
8621 set = isl_set_read_from_str(ctx, aff_eval_tests[i].p);
8622 pnt = isl_set_sample_point(set);
8623 v = isl_pw_aff_eval(pa, pnt);
8624 r = val_check_equal(v, aff_eval_tests[i].res);
8625 isl_val_free(v);
8626 if (r < 0)
8627 return -1;
8629 return 0;
8632 /* Perform basic evaluation tests.
8634 static int test_eval(isl_ctx *ctx)
8636 if (test_eval_1(ctx) < 0)
8637 return -1;
8638 if (test_eval_2(ctx) < 0)
8639 return -1;
8640 if (test_eval_3(ctx) < 0)
8641 return -1;
8642 if (test_eval_aff(ctx) < 0)
8643 return -1;
8644 return 0;
8647 /* Descriptions of sets that are tested for reparsing after printing.
8649 const char *output_tests[] = {
8650 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
8651 "{ [x] : 1 = 0 }",
8652 "{ [x] : false }",
8653 "{ [x] : x mod 2 = 0 }",
8654 "{ [x] : x mod 2 = 1 }",
8655 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
8656 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
8657 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
8658 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
8659 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
8660 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
8663 /* Check that printing a set and reparsing a set from the printed output
8664 * results in the same set.
8666 static int test_output_set(isl_ctx *ctx)
8668 int i;
8669 char *str;
8670 isl_set *set1, *set2;
8671 isl_bool equal;
8673 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
8674 set1 = isl_set_read_from_str(ctx, output_tests[i]);
8675 str = isl_set_to_str(set1);
8676 set2 = isl_set_read_from_str(ctx, str);
8677 free(str);
8678 equal = isl_set_is_equal(set1, set2);
8679 isl_set_free(set1);
8680 isl_set_free(set2);
8681 if (equal < 0)
8682 return -1;
8683 if (!equal)
8684 isl_die(ctx, isl_error_unknown,
8685 "parsed output not the same", return -1);
8688 return 0;
8691 /* Check that an isl_multi_aff is printed using a consistent space.
8693 static isl_stat test_output_ma(isl_ctx *ctx)
8695 char *str;
8696 isl_bool equal;
8697 isl_aff *aff;
8698 isl_multi_aff *ma, *ma2;
8700 ma = isl_multi_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
8701 aff = isl_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
8702 ma = isl_multi_aff_set_aff(ma, 0, aff);
8703 str = isl_multi_aff_to_str(ma);
8704 ma2 = isl_multi_aff_read_from_str(ctx, str);
8705 free(str);
8706 equal = isl_multi_aff_plain_is_equal(ma, ma2);
8707 isl_multi_aff_free(ma2);
8708 isl_multi_aff_free(ma);
8710 if (equal < 0)
8711 return isl_stat_error;
8712 if (!equal)
8713 isl_die(ctx, isl_error_unknown, "bad conversion",
8714 return isl_stat_error);
8716 return isl_stat_ok;
8719 /* Check that an isl_multi_pw_aff is printed using a consistent space.
8721 static isl_stat test_output_mpa(isl_ctx *ctx)
8723 char *str;
8724 isl_bool equal;
8725 isl_pw_aff *pa;
8726 isl_multi_pw_aff *mpa, *mpa2;
8728 mpa = isl_multi_pw_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
8729 pa = isl_pw_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
8730 mpa = isl_multi_pw_aff_set_pw_aff(mpa, 0, pa);
8731 str = isl_multi_pw_aff_to_str(mpa);
8732 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
8733 free(str);
8734 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
8735 isl_multi_pw_aff_free(mpa2);
8736 isl_multi_pw_aff_free(mpa);
8738 if (equal < 0)
8739 return isl_stat_error;
8740 if (!equal)
8741 isl_die(ctx, isl_error_unknown, "bad conversion",
8742 return isl_stat_error);
8744 return isl_stat_ok;
8747 int test_output(isl_ctx *ctx)
8749 char *s;
8750 const char *str;
8751 isl_pw_aff *pa;
8752 isl_printer *p;
8753 int equal;
8755 if (test_output_set(ctx) < 0)
8756 return -1;
8757 if (test_output_ma(ctx) < 0)
8758 return -1;
8759 if (test_output_mpa(ctx) < 0)
8760 return -1;
8762 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
8763 pa = isl_pw_aff_read_from_str(ctx, str);
8765 p = isl_printer_to_str(ctx);
8766 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
8767 p = isl_printer_print_pw_aff(p, pa);
8768 s = isl_printer_get_str(p);
8769 isl_printer_free(p);
8770 isl_pw_aff_free(pa);
8771 if (!s)
8772 equal = -1;
8773 else
8774 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
8775 free(s);
8776 if (equal < 0)
8777 return -1;
8778 if (!equal)
8779 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
8781 return 0;
8784 int test_sample(isl_ctx *ctx)
8786 const char *str;
8787 isl_basic_set *bset1, *bset2;
8788 int empty, subset;
8790 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
8791 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
8792 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
8793 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
8794 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
8795 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
8796 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
8797 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
8798 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
8799 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
8800 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
8801 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
8802 "d - 1073741821e and "
8803 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
8804 "3j >= 1 - a + b + 2e and "
8805 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
8806 "3i <= 4 - a + 4b - e and "
8807 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
8808 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
8809 "c <= -1 + a and 3i >= -2 - a + 3e and "
8810 "1073741822e <= 1073741823 - a + 1073741822b + c and "
8811 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
8812 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
8813 "1073741823e >= 1 + 1073741823b - d and "
8814 "3i >= 1073741823b + c - 1073741823e - f and "
8815 "3i >= 1 + 2b + e + 3g }";
8816 bset1 = isl_basic_set_read_from_str(ctx, str);
8817 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
8818 empty = isl_basic_set_is_empty(bset2);
8819 subset = isl_basic_set_is_subset(bset2, bset1);
8820 isl_basic_set_free(bset1);
8821 isl_basic_set_free(bset2);
8822 if (empty < 0 || subset < 0)
8823 return -1;
8824 if (empty)
8825 isl_die(ctx, isl_error_unknown, "point not found", return -1);
8826 if (!subset)
8827 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
8829 return 0;
8832 /* Perform a projection on a basic set that is known to be empty
8833 * but that has not been assigned a canonical representation.
8834 * Earlier versions of isl would run into a stack overflow
8835 * on this example.
8837 static int test_empty_projection(isl_ctx *ctx)
8839 const char *str;
8840 isl_bool empty;
8841 isl_basic_set *bset;
8843 str = "{ [a, b, c, d, e, f, g, h] : 5f = 1 + 4a - b + 5c - d - 2e and "
8844 "3h = 2 + b + c and 14c >= 9 - 3a + 25b and "
8845 "4c <= 50 - 3a + 23b and 6b <= -39 + a and "
8846 "9g >= -6 + 3a + b + c and e < a + b - 2d and "
8847 "7d >= -5 + 2a + 2b and 5g >= -14 + a - 4b + d + 2e and "
8848 "9g <= -28 - 5b - 2c + 3d + 6e }";
8849 bset = isl_basic_set_read_from_str(ctx, str);
8850 empty = isl_basic_set_is_empty(bset);
8851 bset = isl_basic_set_params(bset);
8852 isl_basic_set_free(bset);
8854 if (empty < 0)
8855 return -1;
8857 return 0;
8860 int test_fixed_power(isl_ctx *ctx)
8862 const char *str;
8863 isl_map *map;
8864 isl_val *exp;
8865 int equal;
8867 str = "{ [i] -> [i + 1] }";
8868 map = isl_map_read_from_str(ctx, str);
8869 exp = isl_val_int_from_si(ctx, 23);
8870 map = isl_map_fixed_power_val(map, exp);
8871 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
8872 isl_map_free(map);
8873 if (equal < 0)
8874 return -1;
8876 return 0;
8879 int test_slice(isl_ctx *ctx)
8881 const char *str;
8882 isl_map *map;
8883 int equal;
8885 str = "{ [i] -> [j] }";
8886 map = isl_map_read_from_str(ctx, str);
8887 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
8888 equal = map_check_equal(map, "{ [i] -> [i] }");
8889 isl_map_free(map);
8890 if (equal < 0)
8891 return -1;
8893 str = "{ [i] -> [j] }";
8894 map = isl_map_read_from_str(ctx, str);
8895 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
8896 equal = map_check_equal(map, "{ [i] -> [j] }");
8897 isl_map_free(map);
8898 if (equal < 0)
8899 return -1;
8901 str = "{ [i] -> [j] }";
8902 map = isl_map_read_from_str(ctx, str);
8903 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
8904 equal = map_check_equal(map, "{ [i] -> [-i] }");
8905 isl_map_free(map);
8906 if (equal < 0)
8907 return -1;
8909 str = "{ [i] -> [j] }";
8910 map = isl_map_read_from_str(ctx, str);
8911 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
8912 equal = map_check_equal(map, "{ [0] -> [j] }");
8913 isl_map_free(map);
8914 if (equal < 0)
8915 return -1;
8917 str = "{ [i] -> [j] }";
8918 map = isl_map_read_from_str(ctx, str);
8919 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
8920 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
8921 isl_map_free(map);
8922 if (equal < 0)
8923 return -1;
8925 str = "{ [i] -> [j] }";
8926 map = isl_map_read_from_str(ctx, str);
8927 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
8928 equal = map_check_equal(map, "{ [i] -> [j] : false }");
8929 isl_map_free(map);
8930 if (equal < 0)
8931 return -1;
8933 return 0;
8936 int test_eliminate(isl_ctx *ctx)
8938 const char *str;
8939 isl_map *map;
8940 int equal;
8942 str = "{ [i] -> [j] : i = 2j }";
8943 map = isl_map_read_from_str(ctx, str);
8944 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
8945 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
8946 isl_map_free(map);
8947 if (equal < 0)
8948 return -1;
8950 return 0;
8953 /* Check basic functionality of isl_map_deltas_map.
8955 static int test_deltas_map(isl_ctx *ctx)
8957 const char *str;
8958 isl_map *map;
8959 int equal;
8961 str = "{ A[i] -> A[i + 1] }";
8962 map = isl_map_read_from_str(ctx, str);
8963 map = isl_map_deltas_map(map);
8964 equal = map_check_equal(map, "{ [A[i] -> A[i + 1]] -> A[1] }");
8965 isl_map_free(map);
8966 if (equal < 0)
8967 return -1;
8969 return 0;
8972 /* Check that isl_set_dim_residue_class detects that the values of j
8973 * in the set below are all odd and that it does not detect any spurious
8974 * strides.
8976 static int test_residue_class(isl_ctx *ctx)
8978 const char *str;
8979 isl_set *set;
8980 isl_int m, r;
8981 isl_stat res;
8983 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
8984 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
8985 set = isl_set_read_from_str(ctx, str);
8986 isl_int_init(m);
8987 isl_int_init(r);
8988 res = isl_set_dim_residue_class(set, 1, &m, &r);
8989 if (res >= 0 &&
8990 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
8991 isl_die(ctx, isl_error_unknown, "incorrect residue class",
8992 res = isl_stat_error);
8993 isl_int_clear(r);
8994 isl_int_clear(m);
8995 isl_set_free(set);
8997 return res;
9000 static int test_align_parameters_1(isl_ctx *ctx)
9002 const char *str;
9003 isl_space *space;
9004 isl_multi_aff *ma1, *ma2;
9005 int equal;
9007 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
9008 ma1 = isl_multi_aff_read_from_str(ctx, str);
9010 space = isl_space_params_alloc(ctx, 1);
9011 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
9012 ma1 = isl_multi_aff_align_params(ma1, space);
9014 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
9015 ma2 = isl_multi_aff_read_from_str(ctx, str);
9017 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
9019 isl_multi_aff_free(ma1);
9020 isl_multi_aff_free(ma2);
9022 if (equal < 0)
9023 return -1;
9024 if (!equal)
9025 isl_die(ctx, isl_error_unknown,
9026 "result not as expected", return -1);
9028 return 0;
9031 /* Check the isl_multi_*_from_*_list operation in case inputs
9032 * have unaligned parameters.
9033 * In particular, older versions of isl would simply fail
9034 * (without printing any error message).
9036 static isl_stat test_align_parameters_2(isl_ctx *ctx)
9038 isl_space *space;
9039 isl_map *map;
9040 isl_aff *aff;
9041 isl_multi_aff *ma;
9043 map = isl_map_read_from_str(ctx, "{ A[] -> M[x] }");
9044 space = isl_map_get_space(map);
9045 isl_map_free(map);
9047 aff = isl_aff_read_from_str(ctx, "[N] -> { A[] -> [N] }");
9048 ma = isl_multi_aff_from_aff_list(space, isl_aff_list_from_aff(aff));
9049 isl_multi_aff_free(ma);
9051 if (!ma)
9052 return isl_stat_error;
9053 return isl_stat_ok;
9056 /* Perform basic parameter alignment tests.
9058 static int test_align_parameters(isl_ctx *ctx)
9060 if (test_align_parameters_1(ctx) < 0)
9061 return -1;
9062 if (test_align_parameters_2(ctx) < 0)
9063 return -1;
9065 return 0;
9068 /* Check that isl_*_drop_unused_params actually drops the unused parameters
9069 * by comparing the result using isl_*_plain_is_equal.
9070 * Note that this assumes that isl_*_plain_is_equal does not consider
9071 * objects that only differ by unused parameters to be equal.
9073 int test_drop_unused_parameters(isl_ctx *ctx)
9075 const char *str_with, *str_without;
9076 isl_basic_set *bset1, *bset2;
9077 isl_set *set1, *set2;
9078 isl_pw_aff *pwa1, *pwa2;
9079 int equal;
9081 str_with = "[n, m, o] -> { [m] }";
9082 str_without = "[m] -> { [m] }";
9084 bset1 = isl_basic_set_read_from_str(ctx, str_with);
9085 bset2 = isl_basic_set_read_from_str(ctx, str_without);
9086 bset1 = isl_basic_set_drop_unused_params(bset1);
9087 equal = isl_basic_set_plain_is_equal(bset1, bset2);
9088 isl_basic_set_free(bset1);
9089 isl_basic_set_free(bset2);
9091 if (equal < 0)
9092 return -1;
9093 if (!equal)
9094 isl_die(ctx, isl_error_unknown,
9095 "result not as expected", return -1);
9097 set1 = isl_set_read_from_str(ctx, str_with);
9098 set2 = isl_set_read_from_str(ctx, str_without);
9099 set1 = isl_set_drop_unused_params(set1);
9100 equal = isl_set_plain_is_equal(set1, set2);
9101 isl_set_free(set1);
9102 isl_set_free(set2);
9104 if (equal < 0)
9105 return -1;
9106 if (!equal)
9107 isl_die(ctx, isl_error_unknown,
9108 "result not as expected", return -1);
9110 pwa1 = isl_pw_aff_read_from_str(ctx, str_with);
9111 pwa2 = isl_pw_aff_read_from_str(ctx, str_without);
9112 pwa1 = isl_pw_aff_drop_unused_params(pwa1);
9113 equal = isl_pw_aff_plain_is_equal(pwa1, pwa2);
9114 isl_pw_aff_free(pwa1);
9115 isl_pw_aff_free(pwa2);
9117 if (equal < 0)
9118 return -1;
9119 if (!equal)
9120 isl_die(ctx, isl_error_unknown,
9121 "result not as expected", return -1);
9123 return 0;
9126 static int test_list(isl_ctx *ctx)
9128 isl_id *a, *b, *c, *d, *id;
9129 isl_id_list *list;
9130 isl_size n;
9131 int ok;
9133 a = isl_id_alloc(ctx, "a", NULL);
9134 b = isl_id_alloc(ctx, "b", NULL);
9135 c = isl_id_alloc(ctx, "c", NULL);
9136 d = isl_id_alloc(ctx, "d", NULL);
9138 list = isl_id_list_alloc(ctx, 4);
9139 list = isl_id_list_add(list, b);
9140 list = isl_id_list_insert(list, 0, a);
9141 list = isl_id_list_add(list, c);
9142 list = isl_id_list_add(list, d);
9143 list = isl_id_list_drop(list, 1, 1);
9145 n = isl_id_list_n_id(list);
9146 if (n < 0)
9147 return -1;
9148 if (n != 3) {
9149 isl_id_list_free(list);
9150 isl_die(ctx, isl_error_unknown,
9151 "unexpected number of elements in list", return -1);
9154 id = isl_id_list_get_id(list, 0);
9155 ok = id == a;
9156 isl_id_free(id);
9157 id = isl_id_list_get_id(list, 1);
9158 ok = ok && id == c;
9159 isl_id_free(id);
9160 id = isl_id_list_get_id(list, 2);
9161 ok = ok && id == d;
9162 isl_id_free(id);
9164 isl_id_list_free(list);
9166 if (!ok)
9167 isl_die(ctx, isl_error_unknown,
9168 "unexpected elements in list", return -1);
9170 return 0;
9173 /* Check the conversion from an isl_multi_aff to an isl_basic_set.
9175 static isl_stat test_ma_conversion(isl_ctx *ctx)
9177 const char *str;
9178 isl_bool equal;
9179 isl_multi_aff *ma;
9180 isl_basic_set *bset1, *bset2;
9182 str = "[N] -> { A[0, N + 1] }";
9183 ma = isl_multi_aff_read_from_str(ctx, str);
9184 bset1 = isl_basic_set_read_from_str(ctx, str);
9185 bset2 = isl_basic_set_from_multi_aff(ma);
9186 equal = isl_basic_set_is_equal(bset1, bset2);
9187 isl_basic_set_free(bset1);
9188 isl_basic_set_free(bset2);
9189 if (equal < 0)
9190 return isl_stat_error;
9191 if (!equal)
9192 isl_die(ctx, isl_error_unknown, "bad conversion",
9193 return isl_stat_error);
9194 return isl_stat_ok;
9197 const char *set_conversion_tests[] = {
9198 "[N] -> { [i] : N - 1 <= 2 i <= N }",
9199 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
9200 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
9201 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
9202 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
9203 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
9204 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
9205 "-3 + c <= d <= 28 + c) }",
9208 /* Check that converting from isl_set to isl_pw_multi_aff and back
9209 * to isl_set produces the original isl_set.
9211 static int test_set_conversion(isl_ctx *ctx)
9213 int i;
9214 const char *str;
9215 isl_set *set1, *set2;
9216 isl_pw_multi_aff *pma;
9217 int equal;
9219 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
9220 str = set_conversion_tests[i];
9221 set1 = isl_set_read_from_str(ctx, str);
9222 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
9223 set2 = isl_set_from_pw_multi_aff(pma);
9224 equal = isl_set_is_equal(set1, set2);
9225 isl_set_free(set1);
9226 isl_set_free(set2);
9228 if (equal < 0)
9229 return -1;
9230 if (!equal)
9231 isl_die(ctx, isl_error_unknown, "bad conversion",
9232 return -1);
9235 return 0;
9238 const char *conversion_tests[] = {
9239 "{ [a, b, c, d] -> s0[a, b, e, f] : "
9240 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
9241 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
9242 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
9243 "9e <= -2 - 2a) }",
9244 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
9245 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
9246 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
9249 /* Check that converting from isl_map to isl_pw_multi_aff and back
9250 * to isl_map produces the original isl_map.
9252 static int test_map_conversion(isl_ctx *ctx)
9254 int i;
9255 isl_map *map1, *map2;
9256 isl_pw_multi_aff *pma;
9257 int equal;
9259 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
9260 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
9261 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
9262 map2 = isl_map_from_pw_multi_aff(pma);
9263 equal = isl_map_is_equal(map1, map2);
9264 isl_map_free(map1);
9265 isl_map_free(map2);
9267 if (equal < 0)
9268 return -1;
9269 if (!equal)
9270 isl_die(ctx, isl_error_unknown, "bad conversion",
9271 return -1);
9274 return 0;
9277 /* Descriptions of isl_pw_multi_aff objects for testing conversion
9278 * to isl_multi_pw_aff and back.
9280 const char *mpa_conversion_tests[] = {
9281 "{ [x] -> A[x] }",
9282 "{ [x] -> A[x] : x >= 0 }",
9283 "{ [x] -> A[x] : x >= 0; [x] -> A[-x] : x < 0 }",
9284 "{ [x] -> A[x, x + 1] }",
9285 "{ [x] -> A[] }",
9286 "{ [x] -> A[] : x >= 0 }",
9289 /* Check that conversion from isl_pw_multi_aff to isl_multi_pw_aff and
9290 * back to isl_pw_multi_aff preserves the original meaning.
9292 static int test_mpa_conversion(isl_ctx *ctx)
9294 int i;
9295 isl_pw_multi_aff *pma1, *pma2;
9296 isl_multi_pw_aff *mpa;
9297 int equal;
9299 for (i = 0; i < ARRAY_SIZE(mpa_conversion_tests); ++i) {
9300 const char *str;
9301 str = mpa_conversion_tests[i];
9302 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
9303 pma2 = isl_pw_multi_aff_copy(pma1);
9304 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma1);
9305 pma1 = isl_pw_multi_aff_from_multi_pw_aff(mpa);
9306 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
9307 isl_pw_multi_aff_free(pma1);
9308 isl_pw_multi_aff_free(pma2);
9310 if (equal < 0)
9311 return -1;
9312 if (!equal)
9313 isl_die(ctx, isl_error_unknown, "bad conversion",
9314 return -1);
9317 return 0;
9320 /* Descriptions of union maps that should be convertible
9321 * to an isl_multi_union_pw_aff.
9323 const char *umap_mupa_conversion_tests[] = {
9324 "{ [a, b, c, d] -> s0[a, b, e, f] : "
9325 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
9326 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
9327 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
9328 "9e <= -2 - 2a) }",
9329 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
9330 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
9331 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
9332 "{ A[] -> B[0]; C[] -> B[1] }",
9333 "{ A[] -> B[]; C[] -> B[] }",
9336 /* Check that converting from isl_union_map to isl_multi_union_pw_aff and back
9337 * to isl_union_map produces the original isl_union_map.
9339 static int test_union_map_mupa_conversion(isl_ctx *ctx)
9341 int i;
9342 isl_union_map *umap1, *umap2;
9343 isl_multi_union_pw_aff *mupa;
9344 int equal;
9346 for (i = 0; i < ARRAY_SIZE(umap_mupa_conversion_tests); ++i) {
9347 const char *str;
9348 str = umap_mupa_conversion_tests[i];
9349 umap1 = isl_union_map_read_from_str(ctx, str);
9350 umap2 = isl_union_map_copy(umap1);
9351 mupa = isl_multi_union_pw_aff_from_union_map(umap2);
9352 umap2 = isl_union_map_from_multi_union_pw_aff(mupa);
9353 equal = isl_union_map_is_equal(umap1, umap2);
9354 isl_union_map_free(umap1);
9355 isl_union_map_free(umap2);
9357 if (equal < 0)
9358 return -1;
9359 if (!equal)
9360 isl_die(ctx, isl_error_unknown, "bad conversion",
9361 return -1);
9364 return 0;
9367 static int test_conversion(isl_ctx *ctx)
9369 if (test_ma_conversion(ctx) < 0)
9370 return -1;
9371 if (test_set_conversion(ctx) < 0)
9372 return -1;
9373 if (test_map_conversion(ctx) < 0)
9374 return -1;
9375 if (test_mpa_conversion(ctx) < 0)
9376 return -1;
9377 if (test_union_map_mupa_conversion(ctx) < 0)
9378 return -1;
9379 return 0;
9382 /* Check that isl_basic_map_curry does not modify input.
9384 static int test_curry(isl_ctx *ctx)
9386 const char *str;
9387 isl_basic_map *bmap1, *bmap2;
9388 int equal;
9390 str = "{ [A[] -> B[]] -> C[] }";
9391 bmap1 = isl_basic_map_read_from_str(ctx, str);
9392 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
9393 equal = isl_basic_map_is_equal(bmap1, bmap2);
9394 isl_basic_map_free(bmap1);
9395 isl_basic_map_free(bmap2);
9397 if (equal < 0)
9398 return -1;
9399 if (equal)
9400 isl_die(ctx, isl_error_unknown,
9401 "curried map should not be equal to original",
9402 return -1);
9404 return 0;
9407 struct {
9408 const char *ma1;
9409 const char *ma;
9410 const char *res;
9411 } pullback_tests[] = {
9412 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
9413 "{ A[a,b] -> C[b + 2a] }" },
9414 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
9415 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
9416 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
9417 "{ A[a] -> C[(a)/6] }" },
9418 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
9419 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
9420 "{ A[a] -> C[(2a)/3] }" },
9421 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
9422 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
9423 "{ A[i,j] -> C[i + j, i + j] }"},
9424 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
9425 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
9426 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
9427 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
9428 "{ [i, j] -> [floor((i)/3), j] }",
9429 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
9432 static int test_pullback(isl_ctx *ctx)
9434 int i;
9435 isl_multi_aff *ma1, *ma2;
9436 isl_multi_aff *ma;
9437 int equal;
9439 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
9440 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
9441 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
9442 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
9443 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
9444 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
9445 isl_multi_aff_free(ma1);
9446 isl_multi_aff_free(ma2);
9447 if (equal < 0)
9448 return -1;
9449 if (!equal)
9450 isl_die(ctx, isl_error_unknown, "bad pullback",
9451 return -1);
9454 return 0;
9457 /* Check that negation is printed correctly and that equal expressions
9458 * are correctly identified.
9460 static int test_ast(isl_ctx *ctx)
9462 isl_ast_expr *expr, *expr1, *expr2, *expr3;
9463 char *str;
9464 int ok, equal;
9466 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
9467 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
9468 expr = isl_ast_expr_add(expr1, expr2);
9469 expr2 = isl_ast_expr_copy(expr);
9470 expr = isl_ast_expr_neg(expr);
9471 expr2 = isl_ast_expr_neg(expr2);
9472 equal = isl_ast_expr_is_equal(expr, expr2);
9473 str = isl_ast_expr_to_C_str(expr);
9474 ok = str ? !strcmp(str, "-(A + B)") : -1;
9475 free(str);
9476 isl_ast_expr_free(expr);
9477 isl_ast_expr_free(expr2);
9479 if (ok < 0 || equal < 0)
9480 return -1;
9481 if (!equal)
9482 isl_die(ctx, isl_error_unknown,
9483 "equal expressions not considered equal", return -1);
9484 if (!ok)
9485 isl_die(ctx, isl_error_unknown,
9486 "isl_ast_expr printed incorrectly", return -1);
9488 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
9489 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
9490 expr = isl_ast_expr_add(expr1, expr2);
9491 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
9492 expr = isl_ast_expr_sub(expr3, expr);
9493 str = isl_ast_expr_to_C_str(expr);
9494 ok = str ? !strcmp(str, "C - (A + B)") : -1;
9495 free(str);
9496 isl_ast_expr_free(expr);
9498 if (ok < 0)
9499 return -1;
9500 if (!ok)
9501 isl_die(ctx, isl_error_unknown,
9502 "isl_ast_expr printed incorrectly", return -1);
9504 return 0;
9507 /* Check that isl_ast_build_expr_from_set returns a valid expression
9508 * for an empty set. Note that isl_ast_build_expr_from_set getting
9509 * called on an empty set probably indicates a bug in the caller.
9511 static int test_ast_build(isl_ctx *ctx)
9513 isl_set *set;
9514 isl_ast_build *build;
9515 isl_ast_expr *expr;
9517 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9518 build = isl_ast_build_from_context(set);
9520 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
9521 expr = isl_ast_build_expr_from_set(build, set);
9523 isl_ast_expr_free(expr);
9524 isl_ast_build_free(build);
9526 if (!expr)
9527 return -1;
9529 return 0;
9532 /* Internal data structure for before_for and after_for callbacks.
9534 * depth is the current depth
9535 * before is the number of times before_for has been called
9536 * after is the number of times after_for has been called
9538 struct isl_test_codegen_data {
9539 int depth;
9540 int before;
9541 int after;
9544 /* This function is called before each for loop in the AST generated
9545 * from test_ast_gen1.
9547 * Increment the number of calls and the depth.
9548 * Check that the space returned by isl_ast_build_get_schedule_space
9549 * matches the target space of the schedule returned by
9550 * isl_ast_build_get_schedule.
9551 * Return an isl_id that is checked by the corresponding call
9552 * to after_for.
9554 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
9555 void *user)
9557 struct isl_test_codegen_data *data = user;
9558 isl_ctx *ctx;
9559 isl_space *space;
9560 isl_union_map *schedule;
9561 isl_union_set *uset;
9562 isl_set *set;
9563 isl_bool empty;
9564 isl_size n;
9565 char name[] = "d0";
9567 ctx = isl_ast_build_get_ctx(build);
9569 if (data->before >= 3)
9570 isl_die(ctx, isl_error_unknown,
9571 "unexpected number of for nodes", return NULL);
9572 if (data->depth < 0 || data->depth >= 2)
9573 isl_die(ctx, isl_error_unknown,
9574 "unexpected depth", return NULL);
9576 snprintf(name, sizeof(name), "d%d", data->depth);
9577 data->before++;
9578 data->depth++;
9580 schedule = isl_ast_build_get_schedule(build);
9581 uset = isl_union_map_range(schedule);
9582 n = isl_union_set_n_set(uset);
9583 if (n != 1) {
9584 isl_union_set_free(uset);
9585 if (n < 0)
9586 return NULL;
9587 isl_die(ctx, isl_error_unknown,
9588 "expecting single range space", return NULL);
9591 space = isl_ast_build_get_schedule_space(build);
9592 set = isl_union_set_extract_set(uset, space);
9593 isl_union_set_free(uset);
9594 empty = isl_set_is_empty(set);
9595 isl_set_free(set);
9597 if (empty < 0)
9598 return NULL;
9599 if (empty)
9600 isl_die(ctx, isl_error_unknown,
9601 "spaces don't match", return NULL);
9603 return isl_id_alloc(ctx, name, NULL);
9606 /* This function is called after each for loop in the AST generated
9607 * from test_ast_gen1.
9609 * Increment the number of calls and decrement the depth.
9610 * Check that the annotation attached to the node matches
9611 * the isl_id returned by the corresponding call to before_for.
9613 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
9614 __isl_keep isl_ast_build *build, void *user)
9616 struct isl_test_codegen_data *data = user;
9617 isl_id *id;
9618 const char *name;
9619 int valid;
9621 data->after++;
9622 data->depth--;
9624 if (data->after > data->before)
9625 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
9626 "mismatch in number of for nodes",
9627 return isl_ast_node_free(node));
9629 id = isl_ast_node_get_annotation(node);
9630 if (!id)
9631 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
9632 "missing annotation", return isl_ast_node_free(node));
9634 name = isl_id_get_name(id);
9635 valid = name && atoi(name + 1) == data->depth;
9636 isl_id_free(id);
9638 if (!valid)
9639 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
9640 "wrong annotation", return isl_ast_node_free(node));
9642 return node;
9645 /* Check that the before_each_for and after_each_for callbacks
9646 * are called for each for loop in the generated code,
9647 * that they are called in the right order and that the isl_id
9648 * returned from the before_each_for callback is attached to
9649 * the isl_ast_node passed to the corresponding after_each_for call.
9651 static int test_ast_gen1(isl_ctx *ctx)
9653 const char *str;
9654 isl_set *set;
9655 isl_union_map *schedule;
9656 isl_ast_build *build;
9657 isl_ast_node *tree;
9658 struct isl_test_codegen_data data;
9660 str = "[N] -> { : N >= 10 }";
9661 set = isl_set_read_from_str(ctx, str);
9662 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
9663 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
9664 schedule = isl_union_map_read_from_str(ctx, str);
9666 data.before = 0;
9667 data.after = 0;
9668 data.depth = 0;
9669 build = isl_ast_build_from_context(set);
9670 build = isl_ast_build_set_before_each_for(build,
9671 &before_for, &data);
9672 build = isl_ast_build_set_after_each_for(build,
9673 &after_for, &data);
9674 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9675 isl_ast_build_free(build);
9676 if (!tree)
9677 return -1;
9679 isl_ast_node_free(tree);
9681 if (data.before != 3 || data.after != 3)
9682 isl_die(ctx, isl_error_unknown,
9683 "unexpected number of for nodes", return -1);
9685 return 0;
9688 /* Check that the AST generator handles domains that are integrally disjoint
9689 * but not rationally disjoint.
9691 static int test_ast_gen2(isl_ctx *ctx)
9693 const char *str;
9694 isl_set *set;
9695 isl_union_map *schedule;
9696 isl_union_map *options;
9697 isl_ast_build *build;
9698 isl_ast_node *tree;
9700 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
9701 schedule = isl_union_map_read_from_str(ctx, str);
9702 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9703 build = isl_ast_build_from_context(set);
9705 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
9706 options = isl_union_map_read_from_str(ctx, str);
9707 build = isl_ast_build_set_options(build, options);
9708 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9709 isl_ast_build_free(build);
9710 if (!tree)
9711 return -1;
9712 isl_ast_node_free(tree);
9714 return 0;
9717 /* Increment *user on each call.
9719 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
9720 __isl_keep isl_ast_build *build, void *user)
9722 int *n = user;
9724 (*n)++;
9726 return node;
9729 /* Test that unrolling tries to minimize the number of instances.
9730 * In particular, for the schedule given below, make sure it generates
9731 * 3 nodes (rather than 101).
9733 static int test_ast_gen3(isl_ctx *ctx)
9735 const char *str;
9736 isl_set *set;
9737 isl_union_map *schedule;
9738 isl_union_map *options;
9739 isl_ast_build *build;
9740 isl_ast_node *tree;
9741 int n_domain = 0;
9743 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
9744 schedule = isl_union_map_read_from_str(ctx, str);
9745 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9747 str = "{ [i] -> unroll[0] }";
9748 options = isl_union_map_read_from_str(ctx, str);
9750 build = isl_ast_build_from_context(set);
9751 build = isl_ast_build_set_options(build, options);
9752 build = isl_ast_build_set_at_each_domain(build,
9753 &count_domains, &n_domain);
9754 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9755 isl_ast_build_free(build);
9756 if (!tree)
9757 return -1;
9759 isl_ast_node_free(tree);
9761 if (n_domain != 3)
9762 isl_die(ctx, isl_error_unknown,
9763 "unexpected number of for nodes", return -1);
9765 return 0;
9768 /* Check that if the ast_build_exploit_nested_bounds options is set,
9769 * we do not get an outer if node in the generated AST,
9770 * while we do get such an outer if node if the options is not set.
9772 static int test_ast_gen4(isl_ctx *ctx)
9774 const char *str;
9775 isl_set *set;
9776 isl_union_map *schedule;
9777 isl_ast_build *build;
9778 isl_ast_node *tree;
9779 enum isl_ast_node_type type;
9780 int enb;
9782 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
9783 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
9785 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
9787 schedule = isl_union_map_read_from_str(ctx, str);
9788 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9789 build = isl_ast_build_from_context(set);
9790 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9791 isl_ast_build_free(build);
9792 if (!tree)
9793 return -1;
9795 type = isl_ast_node_get_type(tree);
9796 isl_ast_node_free(tree);
9798 if (type == isl_ast_node_if)
9799 isl_die(ctx, isl_error_unknown,
9800 "not expecting if node", return -1);
9802 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
9804 schedule = isl_union_map_read_from_str(ctx, str);
9805 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9806 build = isl_ast_build_from_context(set);
9807 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9808 isl_ast_build_free(build);
9809 if (!tree)
9810 return -1;
9812 type = isl_ast_node_get_type(tree);
9813 isl_ast_node_free(tree);
9815 if (type != isl_ast_node_if)
9816 isl_die(ctx, isl_error_unknown,
9817 "expecting if node", return -1);
9819 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
9821 return 0;
9824 /* This function is called for each leaf in the AST generated
9825 * from test_ast_gen5.
9827 * We finalize the AST generation by extending the outer schedule
9828 * with a zero-dimensional schedule. If this results in any for loops,
9829 * then this means that we did not pass along enough information
9830 * about the outer schedule to the inner AST generation.
9832 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
9833 void *user)
9835 isl_union_map *schedule, *extra;
9836 isl_ast_node *tree;
9838 schedule = isl_ast_build_get_schedule(build);
9839 extra = isl_union_map_copy(schedule);
9840 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
9841 schedule = isl_union_map_range_product(schedule, extra);
9842 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9843 isl_ast_build_free(build);
9845 if (!tree)
9846 return NULL;
9848 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
9849 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
9850 "code should not contain any for loop",
9851 return isl_ast_node_free(tree));
9853 return tree;
9856 /* Check that we do not lose any information when going back and
9857 * forth between internal and external schedule.
9859 * In particular, we create an AST where we unroll the only
9860 * non-constant dimension in the schedule. We therefore do
9861 * not expect any for loops in the AST. However, older versions
9862 * of isl would not pass along enough information about the outer
9863 * schedule when performing an inner code generation from a create_leaf
9864 * callback, resulting in the inner code generation producing a for loop.
9866 static int test_ast_gen5(isl_ctx *ctx)
9868 const char *str;
9869 isl_set *set;
9870 isl_union_map *schedule, *options;
9871 isl_ast_build *build;
9872 isl_ast_node *tree;
9874 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
9875 schedule = isl_union_map_read_from_str(ctx, str);
9877 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
9878 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
9879 options = isl_union_map_read_from_str(ctx, str);
9881 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9882 build = isl_ast_build_from_context(set);
9883 build = isl_ast_build_set_options(build, options);
9884 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
9885 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9886 isl_ast_build_free(build);
9887 isl_ast_node_free(tree);
9888 if (!tree)
9889 return -1;
9891 return 0;
9894 /* Check that the expression
9896 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
9898 * is not combined into
9900 * min(n/2, 0)
9902 * as this would result in n/2 being evaluated in parts of
9903 * the definition domain where n is not a multiple of 2.
9905 static int test_ast_expr(isl_ctx *ctx)
9907 const char *str;
9908 isl_pw_aff *pa;
9909 isl_ast_build *build;
9910 isl_ast_expr *expr;
9911 int min_max;
9912 int is_min;
9914 min_max = isl_options_get_ast_build_detect_min_max(ctx);
9915 isl_options_set_ast_build_detect_min_max(ctx, 1);
9917 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
9918 pa = isl_pw_aff_read_from_str(ctx, str);
9919 build = isl_ast_build_alloc(ctx);
9920 expr = isl_ast_build_expr_from_pw_aff(build, pa);
9921 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
9922 isl_ast_expr_get_op_type(expr) == isl_ast_expr_op_min;
9923 isl_ast_build_free(build);
9924 isl_ast_expr_free(expr);
9926 isl_options_set_ast_build_detect_min_max(ctx, min_max);
9928 if (!expr)
9929 return -1;
9930 if (is_min)
9931 isl_die(ctx, isl_error_unknown,
9932 "expressions should not be combined", return -1);
9934 return 0;
9937 static int test_ast_gen(isl_ctx *ctx)
9939 if (test_ast_gen1(ctx) < 0)
9940 return -1;
9941 if (test_ast_gen2(ctx) < 0)
9942 return -1;
9943 if (test_ast_gen3(ctx) < 0)
9944 return -1;
9945 if (test_ast_gen4(ctx) < 0)
9946 return -1;
9947 if (test_ast_gen5(ctx) < 0)
9948 return -1;
9949 if (test_ast_expr(ctx) < 0)
9950 return -1;
9951 return 0;
9954 /* Check if dropping output dimensions from an isl_pw_multi_aff
9955 * works properly.
9957 static int test_pw_multi_aff(isl_ctx *ctx)
9959 const char *str;
9960 isl_pw_multi_aff *pma1, *pma2;
9961 int equal;
9963 str = "{ [i,j] -> [i+j, 4i-j] }";
9964 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
9965 str = "{ [i,j] -> [4i-j] }";
9966 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
9968 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
9970 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
9972 isl_pw_multi_aff_free(pma1);
9973 isl_pw_multi_aff_free(pma2);
9974 if (equal < 0)
9975 return -1;
9976 if (!equal)
9977 isl_die(ctx, isl_error_unknown,
9978 "expressions not equal", return -1);
9980 return 0;
9983 /* Check that we can properly parse multi piecewise affine expressions
9984 * where the piecewise affine expressions have different domains.
9986 static int test_multi_pw_aff_1(isl_ctx *ctx)
9988 const char *str;
9989 isl_set *dom, *dom2;
9990 isl_multi_pw_aff *mpa1, *mpa2;
9991 isl_pw_aff *pa;
9992 int equal;
9993 int equal_domain;
9995 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
9996 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
9997 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
9998 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
9999 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
10000 str = "{ [i] -> [(i : i > 0), 2i] }";
10001 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
10003 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
10005 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
10006 dom = isl_pw_aff_domain(pa);
10007 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
10008 dom2 = isl_pw_aff_domain(pa);
10009 equal_domain = isl_set_is_equal(dom, dom2);
10011 isl_set_free(dom);
10012 isl_set_free(dom2);
10013 isl_multi_pw_aff_free(mpa1);
10014 isl_multi_pw_aff_free(mpa2);
10016 if (equal < 0)
10017 return -1;
10018 if (!equal)
10019 isl_die(ctx, isl_error_unknown,
10020 "expressions not equal", return -1);
10022 if (equal_domain < 0)
10023 return -1;
10024 if (equal_domain)
10025 isl_die(ctx, isl_error_unknown,
10026 "domains unexpectedly equal", return -1);
10028 return 0;
10031 /* Check that the dimensions in the explicit domain
10032 * of a multi piecewise affine expression are properly
10033 * taken into account.
10035 static int test_multi_pw_aff_2(isl_ctx *ctx)
10037 const char *str;
10038 isl_bool involves1, involves2, involves3, equal;
10039 isl_multi_pw_aff *mpa, *mpa1, *mpa2;
10041 str = "{ A[x,y] -> B[] : x >= y }";
10042 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
10043 involves1 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 2);
10044 mpa1 = isl_multi_pw_aff_copy(mpa);
10046 mpa = isl_multi_pw_aff_insert_dims(mpa, isl_dim_in, 0, 1);
10047 involves2 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 1);
10048 involves3 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 1, 2);
10049 str = "{ [a,x,y] -> B[] : x >= y }";
10050 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
10051 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
10052 isl_multi_pw_aff_free(mpa2);
10054 mpa = isl_multi_pw_aff_drop_dims(mpa, isl_dim_in, 0, 1);
10055 mpa = isl_multi_pw_aff_set_tuple_name(mpa, isl_dim_in, "A");
10056 if (equal >= 0 && equal)
10057 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa1);
10058 isl_multi_pw_aff_free(mpa1);
10059 isl_multi_pw_aff_free(mpa);
10061 if (involves1 < 0 || involves2 < 0 || involves3 < 0 || equal < 0)
10062 return -1;
10063 if (!equal)
10064 isl_die(ctx, isl_error_unknown,
10065 "incorrect result of dimension insertion/removal",
10066 return isl_stat_error);
10067 if (!involves1 || involves2 || !involves3)
10068 isl_die(ctx, isl_error_unknown,
10069 "incorrect characterization of involved dimensions",
10070 return isl_stat_error);
10072 return 0;
10075 /* Check that isl_multi_union_pw_aff_multi_val_on_domain
10076 * sets the explicit domain of a zero-dimensional result,
10077 * such that it can be converted to an isl_union_map.
10079 static isl_stat test_multi_pw_aff_3(isl_ctx *ctx)
10081 isl_space *space;
10082 isl_union_set *dom;
10083 isl_multi_val *mv;
10084 isl_multi_union_pw_aff *mupa;
10085 isl_union_map *umap;
10087 dom = isl_union_set_read_from_str(ctx, "{ A[]; B[] }");
10088 space = isl_union_set_get_space(dom);
10089 mv = isl_multi_val_zero(isl_space_set_from_params(space));
10090 mupa = isl_multi_union_pw_aff_multi_val_on_domain(dom, mv);
10091 umap = isl_union_map_from_multi_union_pw_aff(mupa);
10092 isl_union_map_free(umap);
10093 if (!umap)
10094 return isl_stat_error;
10096 return isl_stat_ok;
10099 /* String descriptions of boxes that
10100 * are used for reconstructing box maps from their lower and upper bounds.
10102 static const char *multi_pw_aff_box_tests[] = {
10103 "{ A[x, y] -> [] : x + y >= 0 }",
10104 "[N] -> { A[x, y] -> [x] : x + y <= N }",
10105 "[N] -> { A[x, y] -> [x : y] : x + y <= N }",
10108 /* Check that map representations of boxes can be reconstructed
10109 * from their lower and upper bounds.
10111 static isl_stat test_multi_pw_aff_box(isl_ctx *ctx)
10113 int i;
10115 for (i = 0; i < ARRAY_SIZE(multi_pw_aff_box_tests); ++i) {
10116 const char *str;
10117 isl_bool equal;
10118 isl_map *map, *box;
10119 isl_multi_pw_aff *min, *max;
10121 str = multi_pw_aff_box_tests[i];
10122 map = isl_map_read_from_str(ctx, str);
10123 min = isl_map_min_multi_pw_aff(isl_map_copy(map));
10124 max = isl_map_max_multi_pw_aff(isl_map_copy(map));
10125 box = isl_map_universe(isl_map_get_space(map));
10126 box = isl_map_lower_bound_multi_pw_aff(box, min);
10127 box = isl_map_upper_bound_multi_pw_aff(box, max);
10128 equal = isl_map_is_equal(map, box);
10129 isl_map_free(map);
10130 isl_map_free(box);
10131 if (equal < 0)
10132 return isl_stat_error;
10133 if (!equal)
10134 isl_die(ctx, isl_error_unknown,
10135 "unexpected result", return isl_stat_error);
10138 return isl_stat_ok;
10141 /* Perform some tests on multi piecewise affine expressions.
10143 static int test_multi_pw_aff(isl_ctx *ctx)
10145 if (test_multi_pw_aff_1(ctx) < 0)
10146 return -1;
10147 if (test_multi_pw_aff_2(ctx) < 0)
10148 return -1;
10149 if (test_multi_pw_aff_3(ctx) < 0)
10150 return -1;
10151 if (test_multi_pw_aff_box(ctx) < 0)
10152 return -1;
10153 return 0;
10156 /* This is a regression test for a bug where isl_basic_map_simplify
10157 * would end up in an infinite loop. In particular, we construct
10158 * an empty basic set that is not obviously empty.
10159 * isl_basic_set_is_empty marks the basic set as empty.
10160 * After projecting out i3, the variable can be dropped completely,
10161 * but isl_basic_map_simplify refrains from doing so if the basic set
10162 * is empty and would end up in an infinite loop if it didn't test
10163 * explicitly for empty basic maps in the outer loop.
10165 static int test_simplify_1(isl_ctx *ctx)
10167 const char *str;
10168 isl_basic_set *bset;
10169 int empty;
10171 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
10172 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
10173 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
10174 "i3 >= i2 }";
10175 bset = isl_basic_set_read_from_str(ctx, str);
10176 empty = isl_basic_set_is_empty(bset);
10177 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
10178 isl_basic_set_free(bset);
10179 if (!bset)
10180 return -1;
10181 if (!empty)
10182 isl_die(ctx, isl_error_unknown,
10183 "basic set should be empty", return -1);
10185 return 0;
10188 /* Check that the equality in the set description below
10189 * is simplified away.
10191 static int test_simplify_2(isl_ctx *ctx)
10193 const char *str;
10194 isl_basic_set *bset;
10195 isl_bool universe;
10197 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
10198 bset = isl_basic_set_read_from_str(ctx, str);
10199 universe = isl_basic_set_plain_is_universe(bset);
10200 isl_basic_set_free(bset);
10202 if (universe < 0)
10203 return -1;
10204 if (!universe)
10205 isl_die(ctx, isl_error_unknown,
10206 "equality not simplified away", return -1);
10207 return 0;
10210 /* Some simplification tests.
10212 static int test_simplify(isl_ctx *ctx)
10214 if (test_simplify_1(ctx) < 0)
10215 return -1;
10216 if (test_simplify_2(ctx) < 0)
10217 return -1;
10218 return 0;
10221 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
10222 * with gbr context would fail to disable the use of the shifted tableau
10223 * when transferring equalities for the input to the context, resulting
10224 * in invalid sample values.
10226 static int test_partial_lexmin(isl_ctx *ctx)
10228 const char *str;
10229 isl_basic_set *bset;
10230 isl_basic_map *bmap;
10231 isl_map *map;
10233 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
10234 bmap = isl_basic_map_read_from_str(ctx, str);
10235 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
10236 bset = isl_basic_set_read_from_str(ctx, str);
10237 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
10238 isl_map_free(map);
10240 if (!map)
10241 return -1;
10243 return 0;
10246 /* Check that the variable compression performed on the existentially
10247 * quantified variables inside isl_basic_set_compute_divs is not confused
10248 * by the implicit equalities among the parameters.
10250 static int test_compute_divs(isl_ctx *ctx)
10252 const char *str;
10253 isl_basic_set *bset;
10254 isl_set *set;
10256 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
10257 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
10258 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
10259 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
10260 bset = isl_basic_set_read_from_str(ctx, str);
10261 set = isl_basic_set_compute_divs(bset);
10262 isl_set_free(set);
10263 if (!set)
10264 return -1;
10266 return 0;
10269 /* Check that isl_schedule_get_map is not confused by a schedule tree
10270 * with divergent filter node parameters, as can result from a call
10271 * to isl_schedule_intersect_domain.
10273 static int test_schedule_tree(isl_ctx *ctx)
10275 const char *str;
10276 isl_union_set *uset;
10277 isl_schedule *sched1, *sched2;
10278 isl_union_map *umap;
10280 uset = isl_union_set_read_from_str(ctx, "{ A[i] }");
10281 sched1 = isl_schedule_from_domain(uset);
10282 uset = isl_union_set_read_from_str(ctx, "{ B[] }");
10283 sched2 = isl_schedule_from_domain(uset);
10285 sched1 = isl_schedule_sequence(sched1, sched2);
10286 str = "[n] -> { A[i] : 0 <= i < n; B[] }";
10287 uset = isl_union_set_read_from_str(ctx, str);
10288 sched1 = isl_schedule_intersect_domain(sched1, uset);
10289 umap = isl_schedule_get_map(sched1);
10290 isl_schedule_free(sched1);
10291 isl_union_map_free(umap);
10292 if (!umap)
10293 return -1;
10295 return 0;
10298 /* Check that a zero-dimensional prefix schedule keeps track
10299 * of the domain and outer filters.
10301 static int test_schedule_tree_prefix(isl_ctx *ctx)
10303 const char *str;
10304 isl_bool equal;
10305 isl_union_set *uset;
10306 isl_union_set_list *filters;
10307 isl_multi_union_pw_aff *mupa, *mupa2;
10308 isl_schedule_node *node;
10310 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
10311 uset = isl_union_set_read_from_str(ctx, str);
10312 node = isl_schedule_node_from_domain(uset);
10313 node = isl_schedule_node_child(node, 0);
10315 str = "{ S1[i,j] : i > j }";
10316 uset = isl_union_set_read_from_str(ctx, str);
10317 filters = isl_union_set_list_from_union_set(uset);
10318 str = "{ S1[i,j] : i <= j; S2[i,j] }";
10319 uset = isl_union_set_read_from_str(ctx, str);
10320 filters = isl_union_set_list_add(filters, uset);
10321 node = isl_schedule_node_insert_sequence(node, filters);
10323 node = isl_schedule_node_grandchild(node, 0, 0);
10324 mupa = isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
10325 str = "([] : { S1[i,j] : i > j })";
10326 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx, str);
10327 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
10328 isl_multi_union_pw_aff_free(mupa2);
10329 isl_multi_union_pw_aff_free(mupa);
10330 isl_schedule_node_free(node);
10332 if (equal < 0)
10333 return -1;
10334 if (!equal)
10335 isl_die(ctx, isl_error_unknown, "unexpected prefix schedule",
10336 return -1);
10338 return 0;
10341 /* Check that the reaching domain elements and the prefix schedule
10342 * at a leaf node are the same before and after grouping.
10344 static int test_schedule_tree_group_1(isl_ctx *ctx)
10346 int equal;
10347 const char *str;
10348 isl_id *id;
10349 isl_union_set *uset;
10350 isl_multi_union_pw_aff *mupa;
10351 isl_union_pw_multi_aff *upma1, *upma2;
10352 isl_union_set *domain1, *domain2;
10353 isl_union_map *umap1, *umap2;
10354 isl_schedule_node *node;
10356 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
10357 uset = isl_union_set_read_from_str(ctx, str);
10358 node = isl_schedule_node_from_domain(uset);
10359 node = isl_schedule_node_child(node, 0);
10360 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
10361 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10362 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10363 node = isl_schedule_node_child(node, 0);
10364 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
10365 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10366 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10367 node = isl_schedule_node_child(node, 0);
10368 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
10369 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
10370 domain1 = isl_schedule_node_get_domain(node);
10371 id = isl_id_alloc(ctx, "group", NULL);
10372 node = isl_schedule_node_parent(node);
10373 node = isl_schedule_node_group(node, id);
10374 node = isl_schedule_node_child(node, 0);
10375 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
10376 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
10377 domain2 = isl_schedule_node_get_domain(node);
10378 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
10379 if (equal >= 0 && equal)
10380 equal = isl_union_set_is_equal(domain1, domain2);
10381 if (equal >= 0 && equal)
10382 equal = isl_union_map_is_equal(umap1, umap2);
10383 isl_union_map_free(umap1);
10384 isl_union_map_free(umap2);
10385 isl_union_set_free(domain1);
10386 isl_union_set_free(domain2);
10387 isl_union_pw_multi_aff_free(upma1);
10388 isl_union_pw_multi_aff_free(upma2);
10389 isl_schedule_node_free(node);
10391 if (equal < 0)
10392 return -1;
10393 if (!equal)
10394 isl_die(ctx, isl_error_unknown,
10395 "expressions not equal", return -1);
10397 return 0;
10400 /* Check that we can have nested groupings and that the union map
10401 * schedule representation is the same before and after the grouping.
10402 * Note that after the grouping, the union map representation contains
10403 * the domain constraints from the ranges of the expansion nodes,
10404 * while they are missing from the union map representation of
10405 * the tree without expansion nodes.
10407 * Also check that the global expansion is as expected.
10409 static int test_schedule_tree_group_2(isl_ctx *ctx)
10411 int equal, equal_expansion;
10412 const char *str;
10413 isl_id *id;
10414 isl_union_set *uset;
10415 isl_union_map *umap1, *umap2;
10416 isl_union_map *expansion1, *expansion2;
10417 isl_union_set_list *filters;
10418 isl_multi_union_pw_aff *mupa;
10419 isl_schedule *schedule;
10420 isl_schedule_node *node;
10422 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
10423 "S3[i,j] : 0 <= i,j < 10 }";
10424 uset = isl_union_set_read_from_str(ctx, str);
10425 node = isl_schedule_node_from_domain(uset);
10426 node = isl_schedule_node_child(node, 0);
10427 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
10428 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10429 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10430 node = isl_schedule_node_child(node, 0);
10431 str = "{ S1[i,j] }";
10432 uset = isl_union_set_read_from_str(ctx, str);
10433 filters = isl_union_set_list_from_union_set(uset);
10434 str = "{ S2[i,j]; S3[i,j] }";
10435 uset = isl_union_set_read_from_str(ctx, str);
10436 filters = isl_union_set_list_add(filters, uset);
10437 node = isl_schedule_node_insert_sequence(node, filters);
10438 node = isl_schedule_node_grandchild(node, 1, 0);
10439 str = "{ S2[i,j] }";
10440 uset = isl_union_set_read_from_str(ctx, str);
10441 filters = isl_union_set_list_from_union_set(uset);
10442 str = "{ S3[i,j] }";
10443 uset = isl_union_set_read_from_str(ctx, str);
10444 filters = isl_union_set_list_add(filters, uset);
10445 node = isl_schedule_node_insert_sequence(node, filters);
10447 schedule = isl_schedule_node_get_schedule(node);
10448 umap1 = isl_schedule_get_map(schedule);
10449 uset = isl_schedule_get_domain(schedule);
10450 umap1 = isl_union_map_intersect_domain(umap1, uset);
10451 isl_schedule_free(schedule);
10453 node = isl_schedule_node_grandparent(node);
10454 id = isl_id_alloc(ctx, "group1", NULL);
10455 node = isl_schedule_node_group(node, id);
10456 node = isl_schedule_node_grandchild(node, 1, 0);
10457 id = isl_id_alloc(ctx, "group2", NULL);
10458 node = isl_schedule_node_group(node, id);
10460 schedule = isl_schedule_node_get_schedule(node);
10461 umap2 = isl_schedule_get_map(schedule);
10462 isl_schedule_free(schedule);
10464 node = isl_schedule_node_root(node);
10465 node = isl_schedule_node_child(node, 0);
10466 expansion1 = isl_schedule_node_get_subtree_expansion(node);
10467 isl_schedule_node_free(node);
10469 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
10470 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
10471 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
10473 expansion2 = isl_union_map_read_from_str(ctx, str);
10475 equal = isl_union_map_is_equal(umap1, umap2);
10476 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
10478 isl_union_map_free(umap1);
10479 isl_union_map_free(umap2);
10480 isl_union_map_free(expansion1);
10481 isl_union_map_free(expansion2);
10483 if (equal < 0 || equal_expansion < 0)
10484 return -1;
10485 if (!equal)
10486 isl_die(ctx, isl_error_unknown,
10487 "expressions not equal", return -1);
10488 if (!equal_expansion)
10489 isl_die(ctx, isl_error_unknown,
10490 "unexpected expansion", return -1);
10492 return 0;
10495 /* Some tests for the isl_schedule_node_group function.
10497 static int test_schedule_tree_group(isl_ctx *ctx)
10499 if (test_schedule_tree_group_1(ctx) < 0)
10500 return -1;
10501 if (test_schedule_tree_group_2(ctx) < 0)
10502 return -1;
10503 return 0;
10506 struct {
10507 const char *set;
10508 const char *dual;
10509 } coef_tests[] = {
10510 { "{ rat: [i] : 0 <= i <= 10 }",
10511 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
10512 { "{ rat: [i] : FALSE }",
10513 "{ rat: coefficients[[cst] -> [a]] }" },
10514 { "{ rat: [i] : }",
10515 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
10516 { "{ [0:,1,2:3] }",
10517 "{ rat: coefficients[[c_cst] -> [a, b, c]] : "
10518 "a >= 0 and 2c >= -c_cst - b and 3c >= -c_cst - b }" },
10519 { "[M, N] -> { [x = (1 - N):-1, -4x:(M - 4x)] }",
10520 "{ rat: coefficients[[c_cst, c_M = 0:, c_N = 0:] -> [a, b = -c_M:]] :"
10521 "4b >= -c_N + a and 4b >= -c_cst - 2c_N + a }" },
10522 { "{ rat : [x, y] : 1 <= 2x <= 9 and 2 <= 3y <= 16 }",
10523 "{ rat: coefficients[[c_cst] -> [c_x, c_y]] : "
10524 "4c_y >= -6c_cst - 3c_x and 4c_y >= -6c_cst - 27c_x and "
10525 "32c_y >= -6c_cst - 3c_x and 32c_y >= -6c_cst - 27c_x }" },
10526 { "{ [x, y, z] : 3y <= 2x - 2 and y >= -2 + 2x and 2y >= 2 - x }",
10527 "{ rat: coefficients[[cst] -> [a, b, c]] }" },
10530 struct {
10531 const char *set;
10532 const char *dual;
10533 } sol_tests[] = {
10534 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
10535 "{ rat: [i] : 0 <= i <= 10 }" },
10536 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
10537 "{ rat: [i] }" },
10538 { "{ rat: coefficients[[cst] -> [a]] }",
10539 "{ rat: [i] : FALSE }" },
10542 /* Test the basic functionality of isl_basic_set_coefficients and
10543 * isl_basic_set_solutions.
10545 static int test_dual(isl_ctx *ctx)
10547 int i;
10549 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
10550 int equal;
10551 isl_basic_set *bset1, *bset2;
10553 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
10554 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
10555 bset1 = isl_basic_set_coefficients(bset1);
10556 equal = isl_basic_set_is_equal(bset1, bset2);
10557 isl_basic_set_free(bset1);
10558 isl_basic_set_free(bset2);
10559 if (equal < 0)
10560 return -1;
10561 if (!equal)
10562 isl_die(ctx, isl_error_unknown,
10563 "incorrect dual", return -1);
10566 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
10567 int equal;
10568 isl_basic_set *bset1, *bset2;
10570 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
10571 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
10572 bset1 = isl_basic_set_solutions(bset1);
10573 equal = isl_basic_set_is_equal(bset1, bset2);
10574 isl_basic_set_free(bset1);
10575 isl_basic_set_free(bset2);
10576 if (equal < 0)
10577 return -1;
10578 if (!equal)
10579 isl_die(ctx, isl_error_unknown,
10580 "incorrect dual", return -1);
10583 return 0;
10586 struct {
10587 int scale_tile;
10588 int shift_point;
10589 const char *domain;
10590 const char *schedule;
10591 const char *sizes;
10592 const char *tile;
10593 const char *point;
10594 } tile_tests[] = {
10595 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
10596 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10597 "{ [32,32] }",
10598 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
10599 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10601 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
10602 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10603 "{ [32,32] }",
10604 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
10605 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10607 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
10608 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10609 "{ [32,32] }",
10610 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
10611 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
10613 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
10614 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10615 "{ [32,32] }",
10616 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
10617 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
10621 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
10622 * tile the band and then check if the tile and point bands have the
10623 * expected partial schedule.
10625 static int test_tile(isl_ctx *ctx)
10627 int i;
10628 int scale;
10629 int shift;
10631 scale = isl_options_get_tile_scale_tile_loops(ctx);
10632 shift = isl_options_get_tile_shift_point_loops(ctx);
10634 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
10635 int opt;
10636 int equal;
10637 const char *str;
10638 isl_union_set *domain;
10639 isl_multi_union_pw_aff *mupa, *mupa2;
10640 isl_schedule_node *node;
10641 isl_multi_val *sizes;
10643 opt = tile_tests[i].scale_tile;
10644 isl_options_set_tile_scale_tile_loops(ctx, opt);
10645 opt = tile_tests[i].shift_point;
10646 isl_options_set_tile_shift_point_loops(ctx, opt);
10648 str = tile_tests[i].domain;
10649 domain = isl_union_set_read_from_str(ctx, str);
10650 node = isl_schedule_node_from_domain(domain);
10651 node = isl_schedule_node_child(node, 0);
10652 str = tile_tests[i].schedule;
10653 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10654 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10655 str = tile_tests[i].sizes;
10656 sizes = isl_multi_val_read_from_str(ctx, str);
10657 node = isl_schedule_node_band_tile(node, sizes);
10659 str = tile_tests[i].tile;
10660 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10661 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
10662 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
10663 isl_multi_union_pw_aff_free(mupa);
10664 isl_multi_union_pw_aff_free(mupa2);
10666 node = isl_schedule_node_child(node, 0);
10668 str = tile_tests[i].point;
10669 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10670 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
10671 if (equal >= 0 && equal)
10672 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
10673 mupa2);
10674 isl_multi_union_pw_aff_free(mupa);
10675 isl_multi_union_pw_aff_free(mupa2);
10677 isl_schedule_node_free(node);
10679 if (equal < 0)
10680 return -1;
10681 if (!equal)
10682 isl_die(ctx, isl_error_unknown,
10683 "unexpected result", return -1);
10686 isl_options_set_tile_scale_tile_loops(ctx, scale);
10687 isl_options_set_tile_shift_point_loops(ctx, shift);
10689 return 0;
10692 /* Check that the domain hash of a space is equal to the hash
10693 * of the domain of the space, both ignoring parameters.
10695 static int test_domain_hash(isl_ctx *ctx)
10697 isl_map *map;
10698 isl_space *space;
10699 uint32_t hash1, hash2;
10701 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
10702 space = isl_map_get_space(map);
10703 isl_map_free(map);
10704 hash1 = isl_space_get_tuple_domain_hash(space);
10705 space = isl_space_domain(space);
10706 hash2 = isl_space_get_tuple_hash(space);
10707 isl_space_free(space);
10709 if (!space)
10710 return -1;
10711 if (hash1 != hash2)
10712 isl_die(ctx, isl_error_unknown,
10713 "domain hash not equal to hash of domain", return -1);
10715 return 0;
10718 /* Check that a universe basic set that is not obviously equal to the universe
10719 * is still recognized as being equal to the universe.
10721 static int test_universe(isl_ctx *ctx)
10723 const char *s;
10724 isl_basic_set *bset;
10725 isl_bool is_univ;
10727 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
10728 bset = isl_basic_set_read_from_str(ctx, s);
10729 is_univ = isl_basic_set_is_universe(bset);
10730 isl_basic_set_free(bset);
10732 if (is_univ < 0)
10733 return -1;
10734 if (!is_univ)
10735 isl_die(ctx, isl_error_unknown,
10736 "not recognized as universe set", return -1);
10738 return 0;
10741 /* Sets for which chambers are computed and checked.
10743 const char *chambers_tests[] = {
10744 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
10745 "z >= 0 and z <= C - y and z <= B - x - y }",
10748 /* Add the domain of "cell" to "cells".
10750 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
10752 isl_basic_set_list **cells = user;
10753 isl_basic_set *dom;
10755 dom = isl_cell_get_domain(cell);
10756 isl_cell_free(cell);
10757 *cells = isl_basic_set_list_add(*cells, dom);
10759 return *cells ? isl_stat_ok : isl_stat_error;
10762 /* Check that the elements of "list" are pairwise disjoint.
10764 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
10766 int i, j;
10767 isl_size n;
10769 n = isl_basic_set_list_n_basic_set(list);
10770 if (n < 0)
10771 return isl_stat_error;
10773 for (i = 0; i < n; ++i) {
10774 isl_basic_set *bset_i;
10776 bset_i = isl_basic_set_list_get_basic_set(list, i);
10777 for (j = i + 1; j < n; ++j) {
10778 isl_basic_set *bset_j;
10779 isl_bool disjoint;
10781 bset_j = isl_basic_set_list_get_basic_set(list, j);
10782 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
10783 isl_basic_set_free(bset_j);
10784 if (!disjoint)
10785 isl_die(isl_basic_set_list_get_ctx(list),
10786 isl_error_unknown, "not disjoint",
10787 break);
10788 if (disjoint < 0 || !disjoint)
10789 break;
10791 isl_basic_set_free(bset_i);
10792 if (j < n)
10793 return isl_stat_error;
10796 return isl_stat_ok;
10799 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
10800 * are pairwise disjoint.
10802 static int test_chambers(isl_ctx *ctx)
10804 int i;
10806 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
10807 isl_basic_set *bset;
10808 isl_vertices *vertices;
10809 isl_basic_set_list *cells;
10810 isl_stat ok;
10812 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
10813 vertices = isl_basic_set_compute_vertices(bset);
10814 cells = isl_basic_set_list_alloc(ctx, 0);
10815 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
10816 &cells) < 0)
10817 cells = isl_basic_set_list_free(cells);
10818 ok = check_pairwise_disjoint(cells);
10819 isl_basic_set_list_free(cells);
10820 isl_vertices_free(vertices);
10821 isl_basic_set_free(bset);
10823 if (ok < 0)
10824 return -1;
10827 return 0;
10830 struct {
10831 const char *name;
10832 int (*fn)(isl_ctx *ctx);
10833 } tests [] = {
10834 { "universe", &test_universe },
10835 { "domain hash", &test_domain_hash },
10836 { "dual", &test_dual },
10837 { "dependence analysis", &test_flow },
10838 { "val", &test_val },
10839 { "compute divs", &test_compute_divs },
10840 { "partial lexmin", &test_partial_lexmin },
10841 { "simplify", &test_simplify },
10842 { "curry", &test_curry },
10843 { "piecewise multi affine expressions", &test_pw_multi_aff },
10844 { "multi piecewise affine expressions", &test_multi_pw_aff },
10845 { "conversion", &test_conversion },
10846 { "list", &test_list },
10847 { "align parameters", &test_align_parameters },
10848 { "drop unused parameters", &test_drop_unused_parameters },
10849 { "pullback", &test_pullback },
10850 { "AST", &test_ast },
10851 { "AST build", &test_ast_build },
10852 { "AST generation", &test_ast_gen },
10853 { "eliminate", &test_eliminate },
10854 { "deltas_map", &test_deltas_map },
10855 { "residue class", &test_residue_class },
10856 { "div", &test_div },
10857 { "slice", &test_slice },
10858 { "fixed power", &test_fixed_power },
10859 { "sample", &test_sample },
10860 { "empty projection", &test_empty_projection },
10861 { "output", &test_output },
10862 { "vertices", &test_vertices },
10863 { "chambers", &test_chambers },
10864 { "fixed", &test_fixed },
10865 { "equal", &test_equal },
10866 { "disjoint", &test_disjoint },
10867 { "product", &test_product },
10868 { "dim_max", &test_dim_max },
10869 { "affine", &test_aff },
10870 { "injective", &test_injective },
10871 { "schedule (whole component)", &test_schedule_whole },
10872 { "schedule (incremental)", &test_schedule_incremental },
10873 { "schedule tree", &test_schedule_tree },
10874 { "schedule tree prefix", &test_schedule_tree_prefix },
10875 { "schedule tree grouping", &test_schedule_tree_group },
10876 { "tile", &test_tile },
10877 { "union map", &test_union_map },
10878 { "union_pw", &test_union_pw },
10879 { "locus", &test_locus },
10880 { "eval", &test_eval },
10881 { "parse", &test_parse },
10882 { "single-valued", &test_sv },
10883 { "recession cone", &test_recession_cone },
10884 { "affine hull", &test_affine_hull },
10885 { "simple_hull", &test_simple_hull },
10886 { "box hull", &test_box_hull },
10887 { "coalesce", &test_coalesce },
10888 { "factorize", &test_factorize },
10889 { "subset", &test_subset },
10890 { "subtract", &test_subtract },
10891 { "intersect", &test_intersect },
10892 { "lexmin", &test_lexmin },
10893 { "min", &test_min },
10894 { "set lower bounds", &test_min_mpa },
10895 { "gist", &test_gist },
10896 { "piecewise quasi-polynomials", &test_pwqp },
10897 { "lift", &test_lift },
10898 { "bind parameters", &test_bind },
10899 { "unbind parameters", &test_unbind },
10900 { "bound", &test_bound },
10901 { "get lists", &test_get_list },
10902 { "union", &test_union },
10903 { "split periods", &test_split_periods },
10904 { "lexicographic order", &test_lex },
10905 { "bijectivity", &test_bijective },
10906 { "dataflow analysis", &test_dep },
10907 { "reading", &test_read },
10908 { "bounded", &test_bounded },
10909 { "construction", &test_construction },
10910 { "dimension manipulation", &test_dim },
10911 { "map application", &test_application },
10912 { "convex hull", &test_convex_hull },
10913 { "transitive closure", &test_closure },
10914 { "isl_bool", &test_isl_bool},
10917 int main(int argc, char **argv)
10919 int i;
10920 struct isl_ctx *ctx;
10921 struct isl_options *options;
10923 options = isl_options_new_with_defaults();
10924 assert(options);
10925 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
10927 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
10928 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
10929 printf("%s\n", tests[i].name);
10930 if (tests[i].fn(ctx) < 0)
10931 goto error;
10933 isl_ctx_free(ctx);
10934 return 0;
10935 error:
10936 isl_ctx_free(ctx);
10937 return -1;