isl_test.c: test_coalesce_special2: return isl_stat
[isl.git] / isl_test.c
blob24d0b18b2bd591f3cb7341e54dbaaf6e864e1bc8
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] }",
170 #undef BASE
171 #define BASE pw_multi_aff
173 #include "check_reparse_templ.c"
174 #include "check_reparse_test_templ.c"
176 /* Test parsing of piecewise multi affine expressions by printing
177 * the expressions and checking that parsing the output results
178 * in the same expression.
179 * Do this for an expression converted from a map with an output
180 * dimension name that is equal to an automatically generated name, and
181 * a set of expressions parsed from strings.
183 static isl_stat test_parse_pma(isl_ctx *ctx)
185 isl_map *map;
186 isl_pw_multi_aff *pma;
188 map = isl_map_read_from_str(ctx, "{ [a, a] -> [i1 = a + 1] }");
189 pma = isl_pw_multi_aff_from_map(map);
190 if (check_reparse_pw_multi_aff(ctx, pma) < 0)
191 return isl_stat_error;
193 if (check_reparse_pw_multi_aff_tests(ctx) < 0)
194 return isl_stat_error;
196 return isl_stat_ok;
199 /* String descriptions that cannot be parsed
200 * as union piecewise multi affine expressions.
202 static const char *parse_union_pw_multi_aff_fail_tests[] = {
203 "{ [a] -> [b] : b = a }",
204 "{ [a] -> [b = a] : b >= 0 }",
207 #undef BASE
208 #define BASE union_pw_multi_aff
210 #include "check_parse_fail_test_templ.c"
212 /* Test parsing of union piecewise multi affine expressions.
214 * In particular, check some cases where parsing is supposed to fail.
216 static isl_stat test_parse_upma(isl_ctx *ctx)
218 if (check_parse_union_pw_multi_aff_fail_tests(ctx) < 0)
219 return isl_stat_error;
221 return isl_stat_ok;
224 /* Test parsing of multi piecewise affine expressions by printing
225 * the expressions and checking that parsing the output results
226 * in the same expression.
227 * Do this for a couple of manually constructed expressions,
228 * an expression converted from a map with an output dimension name
229 * that is equal to an automatically generated name, and
230 * a set of expressions parsed from strings.
232 * Additionally, check some cases where parsing is supposed to fail.
234 static int test_parse_mpa(isl_ctx *ctx)
236 isl_space *space;
237 isl_set *dom;
238 isl_map *map;
239 isl_pw_multi_aff *pma;
240 isl_multi_pw_aff *mpa;
241 isl_stat r;
243 space = isl_space_set_alloc(ctx, 0, 0);
244 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
245 mpa = isl_multi_pw_aff_zero(space);
246 r = check_reparse_multi_pw_aff(ctx, mpa);
247 if (r < 0)
248 return -1;
250 space = isl_space_set_alloc(ctx, 1, 0);
251 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
252 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
253 dom = isl_set_universe(isl_space_params(isl_space_copy(space)));
254 dom = isl_set_lower_bound_si(dom, isl_dim_param, 0, 5);
255 mpa = isl_multi_pw_aff_zero(space);
256 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
257 r = check_reparse_multi_pw_aff(ctx, mpa);
258 if (r < 0)
259 return -1;
261 map = isl_map_read_from_str(ctx, "{ [a, a] -> [i1 = a + 1] }");
262 pma = isl_pw_multi_aff_from_map(map);
263 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
264 if (check_reparse_multi_pw_aff(ctx, mpa) < 0)
265 return -1;
267 if (check_reparse_multi_pw_aff_tests(ctx) < 0)
268 return -1;
269 if (check_parse_multi_pw_aff_fail_tests(ctx) < 0)
270 return -1;
272 return 0;
275 /* String descriptions of multi union piecewise affine expressions
276 * that are used for testing printing and parsing.
278 static const char *reparse_multi_union_pw_aff_tests[] = {
279 "[]",
280 "A[]",
281 "A[B[] -> C[]]",
282 "(A[] : { S[x] : x > 0; T[y] : y >= 0 })",
283 "(A[] : { })",
284 "[N] -> (A[] : { })",
285 "[N] -> (A[] : { : N >= 0 })",
286 "[N] -> (A[] : { S[x] : x > N; T[y] : y >= 0 })",
287 "(A[] : [N] -> { S[x] : x > N; T[y] : y >= 0 })",
288 "A[{ S[x] -> [x + 1]; T[x] -> [x] }]",
289 "(A[{ S[x] -> [x + 1]; T[x] -> [x] }] : "
290 "{ S[x] : x > 0; T[y] : y >= 0 })",
293 #undef BASE
294 #define BASE multi_union_pw_aff
296 #include "check_reparse_templ.c"
297 #include "check_reparse_test_templ.c"
299 /* Test parsing of multi union piecewise affine expressions by printing
300 * the expressions and checking that parsing the output results
301 * in the same expression.
302 * Do this for a couple of manually constructed expressions and
303 * a set of expressions parsed from strings.
305 static int test_parse_mupa(isl_ctx *ctx)
307 isl_space *space;
308 isl_multi_union_pw_aff *mupa;
309 isl_set *dom;
310 isl_union_set *uset;
311 isl_stat r;
313 space = isl_space_set_alloc(ctx, 0, 0);
314 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
315 mupa = isl_multi_union_pw_aff_zero(space);
316 r = check_reparse_multi_union_pw_aff(ctx, mupa);
317 if (r < 0)
318 return -1;
320 space = isl_space_set_alloc(ctx, 1, 0);
321 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
322 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
323 dom = isl_set_universe(space);
324 dom = isl_set_lower_bound_si(dom, isl_dim_param, 0, 5);
325 uset = isl_union_set_from_set(dom);
326 space = isl_space_set_alloc(ctx, 1, 0);
327 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
328 space = isl_space_set_tuple_name(space, isl_dim_set, "B");
329 mupa = isl_multi_union_pw_aff_zero(space);
330 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, uset);
331 r = check_reparse_multi_union_pw_aff(ctx, mupa);
332 if (r < 0)
333 return -1;
335 if (check_reparse_multi_union_pw_aff_tests(ctx) < 0)
336 return -1;
338 return 0;
341 /* Test parsing of multi expressions.
343 static int test_parse_multi(isl_ctx *ctx)
345 if (test_parse_mpa(ctx) < 0)
346 return -1;
347 if (test_parse_mupa(ctx) < 0)
348 return -1;
350 return 0;
353 /* Pairs of binary relation representations that should represent
354 * the same binary relations.
356 struct {
357 const char *map1;
358 const char *map2;
359 } parse_map_equal_tests[] = {
360 { "{ [x,y] : [([x/2]+y)/3] >= 1 }",
361 "{ [x, y] : 2y >= 6 - x }" },
362 { "{ [x,y] : x <= min(y, 2*y+3) }",
363 "{ [x,y] : x <= y, 2*y + 3 }" },
364 { "{ [x,y] : x >= min(y, 2*y+3) }",
365 "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }" },
366 { "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }",
367 "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }" },
368 { "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }",
369 "{ [i,j] -> [min(i,j)] }" },
370 { "{ [i,j] : i != j }",
371 "{ [i,j] : i < j or i > j }" },
372 { "{ [i,j] : (i+1)*2 >= j }",
373 "{ [i, j] : j <= 2 + 2i }" },
374 { "{ [i] -> [i > 0 ? 4 : 5] }",
375 "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }" },
376 { "[N=2,M] -> { [i=[(M+N)/4]] }",
377 "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }" },
378 { "{ [x] : x >= 0 }",
379 "{ [x] : x-0 >= 0 }" },
380 { "{ [i] : ((i > 10)) }",
381 "{ [i] : i >= 11 }" },
382 { "{ [i] -> [0] }",
383 "{ [i] -> [0 * i] }" },
384 { "{ [a] -> [b] : (not false) }",
385 "{ [a] -> [b] : true }" },
386 { "{ [i] : i/2 <= 5 }",
387 "{ [i] : i <= 10 }" },
388 { "{Sym=[n] [i] : i <= n }",
389 "[n] -> { [i] : i <= n }" },
390 { "{ [*] }",
391 "{ [a] }" },
392 { "{ [i] : 2*floor(i/2) = i }",
393 "{ [i] : exists a : i = 2 a }" },
394 { "{ [a] -> [b] : a = 5 implies b = 5 }",
395 "{ [a] -> [b] : a != 5 or b = 5 }" },
396 { "{ [a] -> [a - 1 : a > 0] }",
397 "{ [a] -> [a - 1] : a > 0 }" },
398 { "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
399 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }" },
400 { "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
401 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
402 { "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
403 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
404 { "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
405 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
406 { "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
407 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
408 { "{ [a,b] -> [i,j] : a,b << i,j }",
409 "{ [a,b] -> [i,j] : a < i or (a = i and b < j) }" },
410 { "{ [a,b] -> [i,j] : a,b <<= i,j }",
411 "{ [a,b] -> [i,j] : a < i or (a = i and b <= j) }" },
412 { "{ [a,b] -> [i,j] : a,b >> i,j }",
413 "{ [a,b] -> [i,j] : a > i or (a = i and b > j) }" },
414 { "{ [a,b] -> [i,j] : a,b >>= i,j }",
415 "{ [a,b] -> [i,j] : a > i or (a = i and b >= j) }" },
416 { "{ [n] -> [i] : exists (a, b, c: 8b <= i - 32a and "
417 "8b >= -7 + i - 32 a and b >= 0 and b <= 3 and "
418 "8c < n - 32a and i < n and c >= 0 and "
419 "c <= 3 and c >= -4a) }",
420 "{ [n] -> [i] : 0 <= i < n }" },
421 { "{ [x] -> [] : exists (a, b: 0 <= a <= 1 and 0 <= b <= 3 and "
422 "2b <= x - 8a and 2b >= -1 + x - 8a) }",
423 "{ [x] -> [] : 0 <= x <= 15 }" },
424 { "{ [x] -> [x] : }",
425 "{ [x] -> [x] }" },
426 { "{ [x=4:5] -> [x + 1] }",
427 "{ [x] -> [x + 1] : 4 <= x <= 5 }" },
428 { "{ [x=4:5] -> [x + 1 : x + 1] }",
429 "{ [x=4:5] -> [x + 1] }" },
430 { "{ [x] -> [x - 1 : x + 1] }",
431 "{ [x] -> [y] : x - 1 <= y <= x + 1 }" },
432 { "{ [x=4:] -> [x + 1] }",
433 "{ [x] -> [x + 1] : 4 <= x }" },
434 { "{ [x=:5] -> [x + 1] }",
435 "{ [x] -> [x + 1] : x <= 5 }" },
436 { "{ [x=:] -> [x + 1] }",
437 "{ [x] -> [x + 1] }" },
438 { "{ [:] -> [:] }",
439 "{ [x] -> [y] }" },
440 { "{ [x, x//4] }",
441 "{ [x, floor(x/4)] }" },
442 { "{ [10//4] }",
443 "{ [2] }" },
446 int test_parse(struct isl_ctx *ctx)
448 int i;
449 isl_map *map, *map2;
450 const char *str, *str2;
452 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
453 return -1;
454 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
455 return -1;
456 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
457 return -1;
458 if (test_parse_multi(ctx) < 0)
459 return -1;
460 if (test_parse_pma(ctx) < 0)
461 return -1;
462 if (test_parse_upma(ctx) < 0)
463 return -1;
465 str = "{ [i] -> [-i] }";
466 map = isl_map_read_from_str(ctx, str);
467 assert(map);
468 isl_map_free(map);
470 str = "{ A[i] -> L[([i/3])] }";
471 map = isl_map_read_from_str(ctx, str);
472 assert(map);
473 isl_map_free(map);
475 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
476 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
477 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
479 for (i = 0; i < ARRAY_SIZE(parse_map_equal_tests); ++i) {
480 str = parse_map_equal_tests[i].map1;
481 str2 = parse_map_equal_tests[i].map2;
482 if (test_parse_map_equal(ctx, str, str2) < 0)
483 return -1;
486 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
487 map = isl_map_read_from_str(ctx, str);
488 str = "{ [new, old] -> [o0, o1] : "
489 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
490 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
491 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
492 map2 = isl_map_read_from_str(ctx, str);
493 assert(isl_map_is_equal(map, map2));
494 isl_map_free(map);
495 isl_map_free(map2);
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] -> [(new+1)%2,(old+1)%2]}";
500 map2 = isl_map_read_from_str(ctx, str);
501 assert(isl_map_is_equal(map, map2));
502 isl_map_free(map);
503 isl_map_free(map2);
505 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
506 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
507 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
508 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
509 test_parse_pwaff(ctx, "{ [] -> [(100)] }");
511 return 0;
514 static int test_read(isl_ctx *ctx)
516 char *filename;
517 FILE *input;
518 isl_basic_set *bset1, *bset2;
519 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
520 int equal;
522 filename = get_filename(ctx, "set", "omega");
523 assert(filename);
524 input = fopen(filename, "r");
525 assert(input);
527 bset1 = isl_basic_set_read_from_file(ctx, input);
528 bset2 = isl_basic_set_read_from_str(ctx, str);
530 equal = isl_basic_set_is_equal(bset1, bset2);
532 isl_basic_set_free(bset1);
533 isl_basic_set_free(bset2);
534 free(filename);
536 fclose(input);
538 if (equal < 0)
539 return -1;
540 if (!equal)
541 isl_die(ctx, isl_error_unknown,
542 "read sets not equal", return -1);
544 return 0;
547 static int test_bounded(isl_ctx *ctx)
549 isl_set *set;
550 isl_bool bounded;
552 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
553 bounded = isl_set_is_bounded(set);
554 isl_set_free(set);
556 if (bounded < 0)
557 return -1;
558 if (!bounded)
559 isl_die(ctx, isl_error_unknown,
560 "set not considered bounded", return -1);
562 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
563 bounded = isl_set_is_bounded(set);
564 assert(!bounded);
565 isl_set_free(set);
567 if (bounded < 0)
568 return -1;
569 if (bounded)
570 isl_die(ctx, isl_error_unknown,
571 "set considered bounded", return -1);
573 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
574 bounded = isl_set_is_bounded(set);
575 isl_set_free(set);
577 if (bounded < 0)
578 return -1;
579 if (bounded)
580 isl_die(ctx, isl_error_unknown,
581 "set considered bounded", return -1);
583 return 0;
586 /* Construct the basic set { [i] : 5 <= i <= N } */
587 static int test_construction_1(isl_ctx *ctx)
589 isl_space *space;
590 isl_local_space *ls;
591 isl_basic_set *bset;
592 isl_constraint *c;
594 space = isl_space_set_alloc(ctx, 1, 1);
595 bset = isl_basic_set_universe(isl_space_copy(space));
596 ls = isl_local_space_from_space(space);
598 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
599 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
600 c = isl_constraint_set_coefficient_si(c, isl_dim_param, 0, 1);
601 bset = isl_basic_set_add_constraint(bset, c);
603 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
604 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
605 c = isl_constraint_set_constant_si(c, -5);
606 bset = isl_basic_set_add_constraint(bset, c);
608 isl_local_space_free(ls);
609 isl_basic_set_free(bset);
611 return 0;
614 /* Construct the basic set { [x] : -100 <= x <= 100 }
615 * using isl_basic_set_{lower,upper}_bound_val and
616 * check that it is equal the same basic set parsed from a string.
618 static int test_construction_2(isl_ctx *ctx)
620 isl_bool equal;
621 isl_val *v;
622 isl_space *space;
623 isl_basic_set *bset1, *bset2;
625 v = isl_val_int_from_si(ctx, 100);
626 space = isl_space_set_alloc(ctx, 0, 1);
627 bset1 = isl_basic_set_universe(space);
628 bset1 = isl_basic_set_upper_bound_val(bset1, isl_dim_set, 0,
629 isl_val_copy(v));
630 bset1 = isl_basic_set_lower_bound_val(bset1, isl_dim_set, 0,
631 isl_val_neg(v));
632 bset2 = isl_basic_set_read_from_str(ctx, "{ [x] : -100 <= x <= 100 }");
633 equal = isl_basic_set_is_equal(bset1, bset2);
634 isl_basic_set_free(bset1);
635 isl_basic_set_free(bset2);
637 if (equal < 0)
638 return -1;
639 if (!equal)
640 isl_die(ctx, isl_error_unknown,
641 "failed construction", return -1);
643 return 0;
646 /* Basic tests for constructing basic sets.
648 static int test_construction(isl_ctx *ctx)
650 if (test_construction_1(ctx) < 0)
651 return -1;
652 if (test_construction_2(ctx) < 0)
653 return -1;
654 return 0;
657 static int test_dim(isl_ctx *ctx)
659 const char *str;
660 isl_map *map1, *map2;
661 int equal;
663 map1 = isl_map_read_from_str(ctx,
664 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
665 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
666 map2 = isl_map_read_from_str(ctx,
667 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
668 equal = isl_map_is_equal(map1, map2);
669 isl_map_free(map2);
671 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
672 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
673 if (equal >= 0 && equal)
674 equal = isl_map_is_equal(map1, map2);
676 isl_map_free(map1);
677 isl_map_free(map2);
679 if (equal < 0)
680 return -1;
681 if (!equal)
682 isl_die(ctx, isl_error_unknown,
683 "unexpected result", return -1);
685 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
686 map1 = isl_map_read_from_str(ctx, str);
687 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
688 map2 = isl_map_read_from_str(ctx, str);
689 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
690 equal = isl_map_is_equal(map1, map2);
691 isl_map_free(map1);
692 isl_map_free(map2);
694 if (equal < 0)
695 return -1;
696 if (!equal)
697 isl_die(ctx, isl_error_unknown,
698 "unexpected result", return -1);
700 return 0;
703 #undef BASE
704 #define BASE multi_val
705 #include "isl_test_plain_equal_templ.c"
707 #undef BASE
708 #define BASE multi_aff
709 #include "isl_test_plain_equal_templ.c"
711 /* Check that "val" is equal to the value described by "str".
712 * If "str" is "NaN", then check for a NaN value explicitly.
714 static isl_stat val_check_equal(__isl_keep isl_val *val, const char *str)
716 isl_bool ok, is_nan;
717 isl_ctx *ctx;
718 isl_val *res;
720 if (!val)
721 return isl_stat_error;
723 ctx = isl_val_get_ctx(val);
724 res = isl_val_read_from_str(ctx, str);
725 is_nan = isl_val_is_nan(res);
726 if (is_nan < 0)
727 ok = isl_bool_error;
728 else if (is_nan)
729 ok = isl_val_is_nan(val);
730 else
731 ok = isl_val_eq(val, res);
732 isl_val_free(res);
733 if (ok < 0)
734 return isl_stat_error;
735 if (!ok)
736 isl_die(ctx, isl_error_unknown,
737 "unexpected result", return isl_stat_error);
738 return isl_stat_ok;
741 struct {
742 __isl_give isl_val *(*op)(__isl_take isl_val *v);
743 const char *arg;
744 const char *res;
745 } val_un_tests[] = {
746 { &isl_val_neg, "0", "0" },
747 { &isl_val_abs, "0", "0" },
748 { &isl_val_pow2, "0", "1" },
749 { &isl_val_floor, "0", "0" },
750 { &isl_val_ceil, "0", "0" },
751 { &isl_val_neg, "1", "-1" },
752 { &isl_val_neg, "-1", "1" },
753 { &isl_val_neg, "1/2", "-1/2" },
754 { &isl_val_neg, "-1/2", "1/2" },
755 { &isl_val_neg, "infty", "-infty" },
756 { &isl_val_neg, "-infty", "infty" },
757 { &isl_val_neg, "NaN", "NaN" },
758 { &isl_val_abs, "1", "1" },
759 { &isl_val_abs, "-1", "1" },
760 { &isl_val_abs, "1/2", "1/2" },
761 { &isl_val_abs, "-1/2", "1/2" },
762 { &isl_val_abs, "infty", "infty" },
763 { &isl_val_abs, "-infty", "infty" },
764 { &isl_val_abs, "NaN", "NaN" },
765 { &isl_val_floor, "1", "1" },
766 { &isl_val_floor, "-1", "-1" },
767 { &isl_val_floor, "1/2", "0" },
768 { &isl_val_floor, "-1/2", "-1" },
769 { &isl_val_floor, "infty", "infty" },
770 { &isl_val_floor, "-infty", "-infty" },
771 { &isl_val_floor, "NaN", "NaN" },
772 { &isl_val_ceil, "1", "1" },
773 { &isl_val_ceil, "-1", "-1" },
774 { &isl_val_ceil, "1/2", "1" },
775 { &isl_val_ceil, "-1/2", "0" },
776 { &isl_val_ceil, "infty", "infty" },
777 { &isl_val_ceil, "-infty", "-infty" },
778 { &isl_val_ceil, "NaN", "NaN" },
779 { &isl_val_pow2, "-3", "1/8" },
780 { &isl_val_pow2, "-1", "1/2" },
781 { &isl_val_pow2, "1", "2" },
782 { &isl_val_pow2, "2", "4" },
783 { &isl_val_pow2, "3", "8" },
784 { &isl_val_inv, "1", "1" },
785 { &isl_val_inv, "2", "1/2" },
786 { &isl_val_inv, "1/2", "2" },
787 { &isl_val_inv, "-2", "-1/2" },
788 { &isl_val_inv, "-1/2", "-2" },
789 { &isl_val_inv, "0", "NaN" },
790 { &isl_val_inv, "NaN", "NaN" },
791 { &isl_val_inv, "infty", "0" },
792 { &isl_val_inv, "-infty", "0" },
795 /* Perform some basic tests of unary operations on isl_val objects.
797 static int test_un_val(isl_ctx *ctx)
799 int i;
800 isl_val *v;
801 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
803 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
804 isl_stat r;
806 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
807 fn = val_un_tests[i].op;
808 v = fn(v);
809 r = val_check_equal(v, val_un_tests[i].res);
810 isl_val_free(v);
811 if (r < 0)
812 return -1;
815 return 0;
818 struct {
819 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
820 __isl_take isl_val *v2);
821 } val_bin_op[] = {
822 ['+'] = { &isl_val_add },
823 ['-'] = { &isl_val_sub },
824 ['*'] = { &isl_val_mul },
825 ['/'] = { &isl_val_div },
826 ['g'] = { &isl_val_gcd },
827 ['m'] = { &isl_val_min },
828 ['M'] = { &isl_val_max },
831 struct {
832 const char *arg1;
833 unsigned char op;
834 const char *arg2;
835 const char *res;
836 } val_bin_tests[] = {
837 { "0", '+', "0", "0" },
838 { "1", '+', "0", "1" },
839 { "1", '+', "1", "2" },
840 { "1", '-', "1", "0" },
841 { "1", '*', "1", "1" },
842 { "1", '/', "1", "1" },
843 { "2", '*', "3", "6" },
844 { "2", '*', "1/2", "1" },
845 { "2", '*', "1/3", "2/3" },
846 { "2/3", '*', "3/5", "2/5" },
847 { "2/3", '*', "7/5", "14/15" },
848 { "2", '/', "1/2", "4" },
849 { "-2", '/', "-1/2", "4" },
850 { "-2", '/', "1/2", "-4" },
851 { "2", '/', "-1/2", "-4" },
852 { "2", '/', "2", "1" },
853 { "2", '/', "3", "2/3" },
854 { "2/3", '/', "5/3", "2/5" },
855 { "2/3", '/', "5/7", "14/15" },
856 { "0", '/', "0", "NaN" },
857 { "42", '/', "0", "NaN" },
858 { "-42", '/', "0", "NaN" },
859 { "infty", '/', "0", "NaN" },
860 { "-infty", '/', "0", "NaN" },
861 { "NaN", '/', "0", "NaN" },
862 { "0", '/', "NaN", "NaN" },
863 { "42", '/', "NaN", "NaN" },
864 { "-42", '/', "NaN", "NaN" },
865 { "infty", '/', "NaN", "NaN" },
866 { "-infty", '/', "NaN", "NaN" },
867 { "NaN", '/', "NaN", "NaN" },
868 { "0", '/', "infty", "0" },
869 { "42", '/', "infty", "0" },
870 { "-42", '/', "infty", "0" },
871 { "infty", '/', "infty", "NaN" },
872 { "-infty", '/', "infty", "NaN" },
873 { "NaN", '/', "infty", "NaN" },
874 { "0", '/', "-infty", "0" },
875 { "42", '/', "-infty", "0" },
876 { "-42", '/', "-infty", "0" },
877 { "infty", '/', "-infty", "NaN" },
878 { "-infty", '/', "-infty", "NaN" },
879 { "NaN", '/', "-infty", "NaN" },
880 { "1", '-', "1/3", "2/3" },
881 { "1/3", '+', "1/2", "5/6" },
882 { "1/2", '+', "1/2", "1" },
883 { "3/4", '-', "1/4", "1/2" },
884 { "1/2", '-', "1/3", "1/6" },
885 { "infty", '+', "42", "infty" },
886 { "infty", '+', "infty", "infty" },
887 { "42", '+', "infty", "infty" },
888 { "infty", '-', "infty", "NaN" },
889 { "infty", '*', "infty", "infty" },
890 { "infty", '*', "-infty", "-infty" },
891 { "-infty", '*', "infty", "-infty" },
892 { "-infty", '*', "-infty", "infty" },
893 { "0", '*', "infty", "NaN" },
894 { "1", '*', "infty", "infty" },
895 { "infty", '*', "0", "NaN" },
896 { "infty", '*', "42", "infty" },
897 { "42", '-', "infty", "-infty" },
898 { "infty", '+', "-infty", "NaN" },
899 { "4", 'g', "6", "2" },
900 { "5", 'g', "6", "1" },
901 { "42", 'm', "3", "3" },
902 { "42", 'M', "3", "42" },
903 { "3", 'm', "42", "3" },
904 { "3", 'M', "42", "42" },
905 { "42", 'm', "infty", "42" },
906 { "42", 'M', "infty", "infty" },
907 { "42", 'm', "-infty", "-infty" },
908 { "42", 'M', "-infty", "42" },
909 { "42", 'm', "NaN", "NaN" },
910 { "42", 'M', "NaN", "NaN" },
911 { "infty", 'm', "-infty", "-infty" },
912 { "infty", 'M', "-infty", "infty" },
915 /* Perform some basic tests of binary operations on isl_val objects.
917 static int test_bin_val(isl_ctx *ctx)
919 int i;
920 isl_val *v1, *v2, *res;
921 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
922 __isl_take isl_val *v2);
923 int ok;
925 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
926 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
927 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
928 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
929 fn = val_bin_op[val_bin_tests[i].op].fn;
930 v1 = fn(v1, v2);
931 if (isl_val_is_nan(res))
932 ok = isl_val_is_nan(v1);
933 else
934 ok = isl_val_eq(v1, res);
935 isl_val_free(v1);
936 isl_val_free(res);
937 if (ok < 0)
938 return -1;
939 if (!ok)
940 isl_die(ctx, isl_error_unknown,
941 "unexpected result", return -1);
944 return 0;
947 /* Perform some basic tests on isl_val objects.
949 static int test_val(isl_ctx *ctx)
951 if (test_un_val(ctx) < 0)
952 return -1;
953 if (test_bin_val(ctx) < 0)
954 return -1;
955 return 0;
958 /* Sets described using existentially quantified variables that
959 * can also be described without.
961 static const char *elimination_tests[] = {
962 "{ [i,j] : 2 * [i/2] + 3 * [j/4] <= 10 and 2 i = j }",
963 "{ [m, w] : exists a : w - 2m - 5 <= 3a <= m - 2w }",
964 "{ [m, w] : exists a : w >= 0 and a < m and -1 + w <= a <= 2m - w }",
967 /* Check that redundant existentially quantified variables are
968 * getting removed.
970 static int test_elimination(isl_ctx *ctx)
972 int i;
973 isl_size n;
974 isl_basic_set *bset;
976 for (i = 0; i < ARRAY_SIZE(elimination_tests); ++i) {
977 bset = isl_basic_set_read_from_str(ctx, elimination_tests[i]);
978 n = isl_basic_set_dim(bset, isl_dim_div);
979 isl_basic_set_free(bset);
980 if (n < 0)
981 return -1;
982 if (n != 0)
983 isl_die(ctx, isl_error_unknown,
984 "expecting no existentials", return -1);
987 return 0;
990 static int test_div(isl_ctx *ctx)
992 const char *str;
993 int empty;
994 isl_space *space;
995 isl_set *set;
996 isl_local_space *ls;
997 struct isl_basic_set *bset;
998 struct isl_constraint *c;
1000 /* test 1 */
1001 space = isl_space_set_alloc(ctx, 0, 3);
1002 bset = isl_basic_set_universe(isl_space_copy(space));
1003 ls = isl_local_space_from_space(space);
1005 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1006 c = isl_constraint_set_constant_si(c, -1);
1007 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1008 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1009 bset = isl_basic_set_add_constraint(bset, c);
1011 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1012 c = isl_constraint_set_constant_si(c, 1);
1013 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1014 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
1015 bset = isl_basic_set_add_constraint(bset, c);
1017 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1019 assert(bset && bset->n_div == 1);
1020 isl_local_space_free(ls);
1021 isl_basic_set_free(bset);
1023 /* test 2 */
1024 space = isl_space_set_alloc(ctx, 0, 3);
1025 bset = isl_basic_set_universe(isl_space_copy(space));
1026 ls = isl_local_space_from_space(space);
1028 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1029 c = isl_constraint_set_constant_si(c, 1);
1030 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1031 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1032 bset = isl_basic_set_add_constraint(bset, c);
1034 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1035 c = isl_constraint_set_constant_si(c, -1);
1036 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1037 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
1038 bset = isl_basic_set_add_constraint(bset, c);
1040 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1042 assert(bset && bset->n_div == 1);
1043 isl_local_space_free(ls);
1044 isl_basic_set_free(bset);
1046 /* test 3 */
1047 space = isl_space_set_alloc(ctx, 0, 3);
1048 bset = isl_basic_set_universe(isl_space_copy(space));
1049 ls = isl_local_space_from_space(space);
1051 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1052 c = isl_constraint_set_constant_si(c, 1);
1053 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1054 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1055 bset = isl_basic_set_add_constraint(bset, c);
1057 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1058 c = isl_constraint_set_constant_si(c, -3);
1059 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1060 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 4);
1061 bset = isl_basic_set_add_constraint(bset, c);
1063 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1065 assert(bset && bset->n_div == 1);
1066 isl_local_space_free(ls);
1067 isl_basic_set_free(bset);
1069 /* test 4 */
1070 space = isl_space_set_alloc(ctx, 0, 3);
1071 bset = isl_basic_set_universe(isl_space_copy(space));
1072 ls = isl_local_space_from_space(space);
1074 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1075 c = isl_constraint_set_constant_si(c, 2);
1076 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1077 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1078 bset = isl_basic_set_add_constraint(bset, c);
1080 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1081 c = isl_constraint_set_constant_si(c, -1);
1082 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1083 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
1084 bset = isl_basic_set_add_constraint(bset, c);
1086 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1088 assert(isl_basic_set_is_empty(bset));
1089 isl_local_space_free(ls);
1090 isl_basic_set_free(bset);
1092 /* test 5 */
1093 space = isl_space_set_alloc(ctx, 0, 3);
1094 bset = isl_basic_set_universe(isl_space_copy(space));
1095 ls = isl_local_space_from_space(space);
1097 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1098 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1099 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
1100 bset = isl_basic_set_add_constraint(bset, c);
1102 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1103 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1104 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1105 bset = isl_basic_set_add_constraint(bset, c);
1107 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1109 assert(bset && bset->n_div == 0);
1110 isl_basic_set_free(bset);
1111 isl_local_space_free(ls);
1113 /* test 6 */
1114 space = isl_space_set_alloc(ctx, 0, 3);
1115 bset = isl_basic_set_universe(isl_space_copy(space));
1116 ls = isl_local_space_from_space(space);
1118 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1119 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1120 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
1121 bset = isl_basic_set_add_constraint(bset, c);
1123 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1124 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1125 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1126 bset = isl_basic_set_add_constraint(bset, c);
1128 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1130 assert(bset && bset->n_div == 1);
1131 isl_basic_set_free(bset);
1132 isl_local_space_free(ls);
1134 /* test 7 */
1135 /* This test is a bit tricky. We set up an equality
1136 * a + 3b + 3c = 6 e0
1137 * Normalization of divs creates _two_ divs
1138 * a = 3 e0
1139 * c - b - e0 = 2 e1
1140 * Afterwards e0 is removed again because it has coefficient -1
1141 * and we end up with the original equality and div again.
1142 * Perhaps we can avoid the introduction of this temporary div.
1144 space = isl_space_set_alloc(ctx, 0, 4);
1145 bset = isl_basic_set_universe(isl_space_copy(space));
1146 ls = isl_local_space_from_space(space);
1148 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1149 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1150 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1151 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -3);
1152 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 6);
1153 bset = isl_basic_set_add_constraint(bset, c);
1155 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
1157 /* Test disabled for now */
1159 assert(bset && bset->n_div == 1);
1161 isl_local_space_free(ls);
1162 isl_basic_set_free(bset);
1164 /* test 8 */
1165 space = isl_space_set_alloc(ctx, 0, 5);
1166 bset = isl_basic_set_universe(isl_space_copy(space));
1167 ls = isl_local_space_from_space(space);
1169 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1170 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1171 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1172 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, -3);
1173 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 4, 6);
1174 bset = isl_basic_set_add_constraint(bset, c);
1176 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1177 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1178 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 1);
1179 c = isl_constraint_set_constant_si(c, 1);
1180 bset = isl_basic_set_add_constraint(bset, c);
1182 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
1184 /* Test disabled for now */
1186 assert(bset && bset->n_div == 1);
1188 isl_local_space_free(ls);
1189 isl_basic_set_free(bset);
1191 /* test 9 */
1192 space = isl_space_set_alloc(ctx, 0, 4);
1193 bset = isl_basic_set_universe(isl_space_copy(space));
1194 ls = isl_local_space_from_space(space);
1196 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1197 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1198 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -1);
1199 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1200 bset = isl_basic_set_add_constraint(bset, c);
1202 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1203 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1204 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 3);
1205 c = isl_constraint_set_constant_si(c, 2);
1206 bset = isl_basic_set_add_constraint(bset, c);
1208 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
1210 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1212 assert(!isl_basic_set_is_empty(bset));
1214 isl_local_space_free(ls);
1215 isl_basic_set_free(bset);
1217 /* test 10 */
1218 space = isl_space_set_alloc(ctx, 0, 3);
1219 bset = isl_basic_set_universe(isl_space_copy(space));
1220 ls = isl_local_space_from_space(space);
1222 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1223 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1224 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1225 bset = isl_basic_set_add_constraint(bset, c);
1227 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1229 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1231 isl_local_space_free(ls);
1232 isl_basic_set_free(bset);
1234 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1235 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
1236 set = isl_set_read_from_str(ctx, str);
1237 set = isl_set_compute_divs(set);
1238 isl_set_free(set);
1239 if (!set)
1240 return -1;
1242 if (test_elimination(ctx) < 0)
1243 return -1;
1245 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
1246 set = isl_set_read_from_str(ctx, str);
1247 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
1248 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
1249 empty = isl_set_is_empty(set);
1250 isl_set_free(set);
1251 if (empty < 0)
1252 return -1;
1253 if (!empty)
1254 isl_die(ctx, isl_error_unknown,
1255 "result not as accurate as expected", return -1);
1257 return 0;
1260 void test_application_case(struct isl_ctx *ctx, const char *name)
1262 char *filename;
1263 FILE *input;
1264 struct isl_basic_set *bset1, *bset2;
1265 struct isl_basic_map *bmap;
1267 filename = get_filename(ctx, name, "omega");
1268 assert(filename);
1269 input = fopen(filename, "r");
1270 assert(input);
1272 bset1 = isl_basic_set_read_from_file(ctx, input);
1273 bmap = isl_basic_map_read_from_file(ctx, input);
1275 bset1 = isl_basic_set_apply(bset1, bmap);
1277 bset2 = isl_basic_set_read_from_file(ctx, input);
1279 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1281 isl_basic_set_free(bset1);
1282 isl_basic_set_free(bset2);
1283 free(filename);
1285 fclose(input);
1288 static int test_application(isl_ctx *ctx)
1290 test_application_case(ctx, "application");
1291 test_application_case(ctx, "application2");
1293 return 0;
1296 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1298 char *filename;
1299 FILE *input;
1300 struct isl_basic_set *bset1, *bset2;
1302 filename = get_filename(ctx, name, "polylib");
1303 assert(filename);
1304 input = fopen(filename, "r");
1305 assert(input);
1307 bset1 = isl_basic_set_read_from_file(ctx, input);
1308 bset2 = isl_basic_set_read_from_file(ctx, input);
1310 bset1 = isl_basic_set_affine_hull(bset1);
1312 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1314 isl_basic_set_free(bset1);
1315 isl_basic_set_free(bset2);
1316 free(filename);
1318 fclose(input);
1321 /* Pairs of sets and the corresponding expected results of
1322 * isl_basic_set_recession_cone.
1324 struct {
1325 const char *set;
1326 const char *cone;
1327 } recession_cone_tests[] = {
1328 { "{ [i] : 0 <= i <= 10 }", "{ [0] }" },
1329 { "{ [i] : 0 <= i }", "{ [i] : 0 <= i }" },
1330 { "{ [i] : i <= 10 }", "{ [i] : i <= 0 }" },
1331 { "{ [i] : false }", "{ [i] : false }" },
1334 /* Perform some basic isl_basic_set_recession_cone tests.
1336 static int test_recession_cone(struct isl_ctx *ctx)
1338 int i;
1340 for (i = 0; i < ARRAY_SIZE(recession_cone_tests); ++i) {
1341 const char *str;
1342 isl_basic_set *bset;
1343 isl_basic_set *cone, *expected;
1344 isl_bool equal;
1346 str = recession_cone_tests[i].set;
1347 bset = isl_basic_set_read_from_str(ctx, str);
1348 str = recession_cone_tests[i].cone;
1349 expected = isl_basic_set_read_from_str(ctx, str);
1350 cone = isl_basic_set_recession_cone(bset);
1351 equal = isl_basic_set_is_equal(cone, expected);
1352 isl_basic_set_free(cone);
1353 isl_basic_set_free(expected);
1354 if (equal < 0)
1355 return -1;
1356 if (!equal)
1357 isl_die(ctx, isl_error_unknown, "unexpected cone",
1358 return -1);
1361 return 0;
1364 int test_affine_hull(struct isl_ctx *ctx)
1366 const char *str;
1367 isl_set *set;
1368 isl_basic_set *bset, *bset2;
1369 isl_size n;
1370 isl_bool subset;
1372 test_affine_hull_case(ctx, "affine2");
1373 test_affine_hull_case(ctx, "affine");
1374 test_affine_hull_case(ctx, "affine3");
1376 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1377 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1378 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1379 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1380 set = isl_set_read_from_str(ctx, str);
1381 bset = isl_set_affine_hull(set);
1382 n = isl_basic_set_dim(bset, isl_dim_div);
1383 isl_basic_set_free(bset);
1384 if (n < 0)
1385 return -1;
1386 if (n != 0)
1387 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1388 return -1);
1390 /* Check that isl_map_affine_hull is not confused by
1391 * the reordering of divs in isl_map_align_divs.
1393 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1394 "32e0 = b and 32e1 = c); "
1395 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1396 set = isl_set_read_from_str(ctx, str);
1397 bset = isl_set_affine_hull(set);
1398 isl_basic_set_free(bset);
1399 if (!bset)
1400 return -1;
1402 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1403 "32e2 = 31 + 31e0 }";
1404 set = isl_set_read_from_str(ctx, str);
1405 bset = isl_set_affine_hull(set);
1406 str = "{ [a] : exists e : a = 32 e }";
1407 bset2 = isl_basic_set_read_from_str(ctx, str);
1408 subset = isl_basic_set_is_subset(bset, bset2);
1409 isl_basic_set_free(bset);
1410 isl_basic_set_free(bset2);
1411 if (subset < 0)
1412 return -1;
1413 if (!subset)
1414 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1415 return -1);
1417 return 0;
1420 /* Test a special case of isl_set_plain_unshifted_simple_hull
1421 * where older versions of isl would include a redundant constraint
1422 * in the result.
1423 * Check that the result does not have any constraints.
1425 static isl_stat test_plain_unshifted_simple_hull_special(isl_ctx *ctx)
1427 const char *str;
1428 isl_bool is_universe;
1429 isl_set *set;
1430 isl_basic_set *bset;
1432 str = "{[x, y] : x = 0 or 2*((x+y)//2) <= y + 2 }";
1433 set = isl_set_read_from_str(ctx, str);
1434 bset = isl_set_plain_unshifted_simple_hull(set);
1435 is_universe = isl_basic_set_plain_is_universe(bset);
1436 isl_basic_set_free(bset);
1438 if (is_universe < 0)
1439 return isl_stat_error;
1440 if (!is_universe)
1441 isl_die(ctx, isl_error_unknown,
1442 "hull should not have any constraints",
1443 return isl_stat_error);
1445 return isl_stat_ok;
1448 /* Inputs for simple hull tests, consisting of
1449 * the specific simple hull function, the input set and the expected result.
1451 struct {
1452 __isl_give isl_basic_set *(*fn)(__isl_take isl_set *set);
1453 const char *set;
1454 const char *hull;
1455 } simple_hull_tests[] = {
1456 { &isl_set_plain_unshifted_simple_hull,
1457 "{ [i,j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1458 "{ [i,j] : i >= 1 }" },
1459 { &isl_set_plain_unshifted_simple_hull,
1460 "{ [n,i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1461 "(j mod 4 = 2 and k mod 6 = n) }",
1462 "{ [n,i,j,k] : j mod 4 = 2 }" },
1463 { &isl_set_unshifted_simple_hull,
1464 "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1465 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1466 { &isl_set_simple_hull,
1467 "{ [a, b] : b <= 0 and "
1468 "2*floor((-2*floor((b)/2))/5) >= a - floor((b)/2); "
1469 "[a, b] : a mod 2 = 0 }",
1470 "{ [a, b] }" },
1473 /* Basic tests for various simple hull functions.
1475 static int test_various_simple_hull(isl_ctx *ctx)
1477 int i;
1478 isl_set *set;
1479 isl_basic_set *hull, *expected;
1480 isl_bool equal;
1482 for (i = 0; i < ARRAY_SIZE(simple_hull_tests); ++i) {
1483 const char *str;
1484 str = simple_hull_tests[i].set;
1485 set = isl_set_read_from_str(ctx, str);
1486 str = simple_hull_tests[i].hull;
1487 expected = isl_basic_set_read_from_str(ctx, str);
1488 hull = simple_hull_tests[i].fn(set);
1489 equal = isl_basic_set_is_equal(hull, expected);
1490 isl_basic_set_free(hull);
1491 isl_basic_set_free(expected);
1492 if (equal < 0)
1493 return -1;
1494 if (!equal)
1495 isl_die(ctx, isl_error_unknown, "unexpected hull",
1496 return -1);
1499 return 0;
1502 static int test_simple_hull(struct isl_ctx *ctx)
1504 const char *str;
1505 isl_set *set;
1506 isl_basic_set *bset;
1507 isl_bool is_empty;
1509 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1510 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1511 set = isl_set_read_from_str(ctx, str);
1512 bset = isl_set_simple_hull(set);
1513 is_empty = isl_basic_set_is_empty(bset);
1514 isl_basic_set_free(bset);
1516 if (is_empty == isl_bool_error)
1517 return -1;
1519 if (is_empty == isl_bool_false)
1520 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1521 return -1);
1523 if (test_plain_unshifted_simple_hull_special(ctx) < 0)
1524 return -1;
1525 if (test_various_simple_hull(ctx) < 0)
1526 return -1;
1528 return 0;
1531 /* Inputs for isl_set_get_simple_fixed_box_hull tests.
1532 * "set" is the input set.
1533 * "offset" is the expected box offset.
1534 * "size" is the expected box size.
1536 static struct {
1537 const char *set;
1538 const char *offset;
1539 const char *size;
1540 } box_hull_tests[] = {
1541 { "{ S[x, y] : 0 <= x, y < 10 }", "{ S[0, 0] }", "{ S[10, 10] }" },
1542 { "[N] -> { S[x, y] : N <= x, y < N + 10 }",
1543 "[N] -> { S[N, N] }", "{ S[10, 10] }" },
1544 { "{ S[x, y] : 0 <= x + y, x - y < 10 }",
1545 "{ S[0, -4] }", "{ S[10, 9] }" },
1546 { "{ [i=0:10] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1547 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }",
1548 "{ [3] }", "{ [8] }" },
1549 { "[N] -> { [w = 0:17] : exists (e0: w < 2N and "
1550 "-1 + w <= e0 <= w and 2e0 >= N + w and w <= 2e0 <= 15 + w) }",
1551 "[N] -> { [N] }", "{ [9] }" },
1554 /* Perform basic isl_set_get_simple_fixed_box_hull tests.
1556 static int test_box_hull(struct isl_ctx *ctx)
1558 int i;
1560 for (i = 0; i < ARRAY_SIZE(box_hull_tests); ++i) {
1561 const char *str;
1562 isl_stat r;
1563 isl_set *set;
1564 isl_multi_aff *offset;
1565 isl_multi_val *size;
1566 isl_fixed_box *box;
1568 set = isl_set_read_from_str(ctx, box_hull_tests[i].set);
1569 box = isl_set_get_simple_fixed_box_hull(set);
1570 offset = isl_fixed_box_get_offset(box);
1571 size = isl_fixed_box_get_size(box);
1572 str = box_hull_tests[i].offset;
1573 r = multi_aff_check_plain_equal(offset, str);
1574 str = box_hull_tests[i].size;
1575 if (r >= 0)
1576 r = multi_val_check_plain_equal(size, str);
1577 isl_multi_aff_free(offset);
1578 isl_multi_val_free(size);
1579 isl_fixed_box_free(box);
1580 isl_set_free(set);
1581 if (r < 0)
1582 return -1;
1585 return 0;
1588 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1590 char *filename;
1591 FILE *input;
1592 struct isl_basic_set *bset1, *bset2;
1593 struct isl_set *set;
1595 filename = get_filename(ctx, name, "polylib");
1596 assert(filename);
1597 input = fopen(filename, "r");
1598 assert(input);
1600 bset1 = isl_basic_set_read_from_file(ctx, input);
1601 bset2 = isl_basic_set_read_from_file(ctx, input);
1603 set = isl_basic_set_union(bset1, bset2);
1604 bset1 = isl_set_convex_hull(set);
1606 bset2 = isl_basic_set_read_from_file(ctx, input);
1608 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1610 isl_basic_set_free(bset1);
1611 isl_basic_set_free(bset2);
1612 free(filename);
1614 fclose(input);
1617 struct {
1618 const char *set;
1619 const char *hull;
1620 } convex_hull_tests[] = {
1621 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1622 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1623 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1624 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1625 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1626 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1627 "i2 <= 5 and i2 >= 4; "
1628 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1629 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1630 "i2 <= 5 + i0 and i2 >= i0 }" },
1631 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1632 "{ [x, y] : 1 = 0 }" },
1633 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1634 "[x, y, 0] : x >= 0 and y < 0 }",
1635 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1636 { "{ [a, b, c] : a <= 1 and -a < b <= 1 and 0 <= c <= 2 - a - b and "
1637 "c <= a; "
1638 "[0, 2, 0]; [3, 1, 0] }",
1639 "{ [a, b, c] : b > -a and 2b >= -1 + a and 0 <= c <= a and "
1640 "5c <= 6 - a - 3b }" },
1643 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1645 int i;
1646 int orig_convex = ctx->opt->convex;
1647 ctx->opt->convex = convex;
1649 test_convex_hull_case(ctx, "convex0");
1650 test_convex_hull_case(ctx, "convex1");
1651 test_convex_hull_case(ctx, "convex2");
1652 test_convex_hull_case(ctx, "convex3");
1653 test_convex_hull_case(ctx, "convex4");
1654 test_convex_hull_case(ctx, "convex5");
1655 test_convex_hull_case(ctx, "convex6");
1656 test_convex_hull_case(ctx, "convex7");
1657 test_convex_hull_case(ctx, "convex8");
1658 test_convex_hull_case(ctx, "convex9");
1659 test_convex_hull_case(ctx, "convex10");
1660 test_convex_hull_case(ctx, "convex11");
1661 test_convex_hull_case(ctx, "convex12");
1662 test_convex_hull_case(ctx, "convex13");
1663 test_convex_hull_case(ctx, "convex14");
1664 test_convex_hull_case(ctx, "convex15");
1666 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1667 isl_set *set1, *set2;
1668 int equal;
1670 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1671 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1672 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1673 equal = isl_set_is_equal(set1, set2);
1674 isl_set_free(set1);
1675 isl_set_free(set2);
1677 if (equal < 0)
1678 return -1;
1679 if (!equal)
1680 isl_die(ctx, isl_error_unknown,
1681 "unexpected convex hull", return -1);
1684 ctx->opt->convex = orig_convex;
1686 return 0;
1689 static int test_convex_hull(isl_ctx *ctx)
1691 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1692 return -1;
1693 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1694 return -1;
1695 return 0;
1698 /* Check that computing the gist of "map" with respect to "context"
1699 * does not make any copy of "map" get marked empty.
1700 * Earlier versions of isl would end up doing that.
1702 static isl_stat test_gist_empty_pair(isl_ctx *ctx, const char *map,
1703 const char *context)
1705 isl_map *m1, *m2, *m3;
1706 isl_bool empty_before, empty_after;
1708 m1 = isl_map_read_from_str(ctx, map);
1709 m2 = isl_map_read_from_str(ctx, context);
1710 m3 = isl_map_copy(m1);
1711 empty_before = isl_map_is_empty(m3);
1712 m1 = isl_map_gist(m1, m2);
1713 empty_after = isl_map_is_empty(m3);
1714 isl_map_free(m1);
1715 isl_map_free(m3);
1717 if (empty_before < 0 || empty_after < 0)
1718 return isl_stat_error;
1719 if (empty_before)
1720 isl_die(ctx, isl_error_unknown, "map should not be empty",
1721 return isl_stat_error);
1722 if (empty_after)
1723 isl_die(ctx, isl_error_unknown, "map should still not be empty",
1724 return isl_stat_error);
1726 return isl_stat_ok;
1729 /* Check that computing a gist does not make any copy of the input
1730 * get marked empty.
1731 * Earlier versions of isl would end up doing that on some pairs of inputs.
1733 static isl_stat test_gist_empty(isl_ctx *ctx)
1735 const char *map, *context;
1737 map = "{ [] -> [a, b, c] : 2b = 1 + a }";
1738 context = "{ [] -> [a, b, c] : 2c = 2 + a }";
1739 if (test_gist_empty_pair(ctx, map, context) < 0)
1740 return isl_stat_error;
1741 map = "{ [] -> [0, 0] }";
1742 context = "{ [] -> [a, b] : a > b }";
1743 if (test_gist_empty_pair(ctx, map, context) < 0)
1744 return isl_stat_error;
1746 return isl_stat_ok;
1749 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1751 struct {
1752 const char *map;
1753 const char *context;
1754 const char *gist;
1755 } plain_gist_tests[] = {
1756 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1757 "{ [i] -> [j] : i >= 1 }",
1758 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1759 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1760 "(j mod 4 = 2 and k mod 6 = n) }",
1761 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1762 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1763 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1764 "{ [i] -> [j] : i > j }",
1765 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1768 /* Basic tests for isl_map_plain_gist_basic_map.
1770 static int test_plain_gist(isl_ctx *ctx)
1772 int i;
1774 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1775 const char *str;
1776 int equal;
1777 isl_map *map, *gist;
1778 isl_basic_map *context;
1780 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1781 str = plain_gist_tests[i].context;
1782 context = isl_basic_map_read_from_str(ctx, str);
1783 map = isl_map_plain_gist_basic_map(map, context);
1784 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1785 equal = isl_map_is_equal(map, gist);
1786 isl_map_free(map);
1787 isl_map_free(gist);
1788 if (equal < 0)
1789 return -1;
1790 if (!equal)
1791 isl_die(ctx, isl_error_unknown,
1792 "incorrect gist result", return -1);
1795 return 0;
1798 /* Inputs for isl_basic_set_gist tests that are expected to fail.
1800 struct {
1801 const char *set;
1802 const char *context;
1803 } gist_fail_tests[] = {
1804 { "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1805 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }",
1806 "{ [i] : i >= 0 }" },
1809 /* Check that isl_basic_set_gist fails (gracefully) when expected.
1810 * In particular, the user should be able to recover from the failure.
1812 static isl_stat test_gist_fail(struct isl_ctx *ctx)
1814 int i, n;
1815 int on_error;
1817 on_error = isl_options_get_on_error(ctx);
1818 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
1819 n = ARRAY_SIZE(gist_fail_tests);
1820 for (i = 0; i < n; ++i) {
1821 const char *str;
1822 isl_basic_set *bset, *context;
1824 bset = isl_basic_set_read_from_str(ctx, gist_fail_tests[i].set);
1825 str = gist_fail_tests[i].context;
1826 context = isl_basic_set_read_from_str(ctx, str);
1827 bset = isl_basic_set_gist(bset, context);
1828 isl_basic_set_free(bset);
1829 if (bset)
1830 break;
1832 isl_options_set_on_error(ctx, on_error);
1833 if (i < n)
1834 isl_die(ctx, isl_error_unknown,
1835 "operation not expected to succeed",
1836 return isl_stat_error);
1838 return isl_stat_ok;
1841 struct {
1842 const char *set;
1843 const char *context;
1844 const char *gist;
1845 } gist_tests[] = {
1846 { "{ [1, -1, 3] }",
1847 "{ [1, b, 2 - b] : -1 <= b <= 2 }",
1848 "{ [a, -1, c] }" },
1849 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1850 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1851 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1852 "{ [a, b, c] : a <= 15 }" },
1853 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1854 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1855 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1856 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1857 { "{ [m, n, a, b] : a <= 2147 + n }",
1858 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1859 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1860 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1861 "b >= 0) }",
1862 "{ [m, n, ku, kl] }" },
1863 { "{ [a, a, b] : a >= 10 }",
1864 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1865 "{ [a, a, b] : a >= 10 }" },
1866 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1867 "{ [0, j] : j >= 0 }" },
1868 /* Check that no constraints on i6 are introduced in the gist */
1869 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1870 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1871 "5e0 <= 381 - t1 and i4 <= 1) }",
1872 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1873 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1874 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1875 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1876 "20e0 >= 1511 - 4t1 - 5i4) }" },
1877 /* Check that no constraints on i6 are introduced in the gist */
1878 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1879 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1880 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1881 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1882 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1883 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1884 "20e1 <= 1530 - 4t1 - 5i4 and "
1885 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1886 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1887 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1888 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1889 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1890 "10e0 <= -91 + 5i4 + 4i6 and "
1891 "10e0 >= -105 + 5i4 + 4i6) }",
1892 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1893 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1894 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1895 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1896 "{ [a, b, q, p] : b >= 1 + a }",
1897 "{ [a, b, q, p] : false }" },
1898 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1899 "[n] -> { [x] : x mod 32 = 0 }",
1900 "[n] -> { [x = n] }" },
1901 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1902 "{ [x] : x mod 2 = 0 }" },
1903 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1904 "{ [x] : x mod 128 = 0 }" },
1905 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1906 "{ [x] : x mod 3200 = 0 }" },
1907 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1908 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1909 "{ [a, b, c = a] }" },
1910 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1911 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1912 "{ [a, b, c = a] : a mod 3 = 0 }" },
1913 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1914 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1915 "{ [x] }" },
1916 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1917 "{ [x,y] : 1 <= y <= 3 }",
1918 "{ [x,y] }" },
1921 /* Check that isl_set_gist behaves as expected.
1923 * For the test cases in gist_tests, besides checking that the result
1924 * is as expected, also check that applying the gist operation does
1925 * not modify the input set (an earlier version of isl would do that) and
1926 * that the test case is consistent, i.e., that the gist has the same
1927 * intersection with the context as the input set.
1929 static int test_gist(struct isl_ctx *ctx)
1931 int i;
1932 const char *str;
1933 isl_basic_set *bset1, *bset2;
1934 isl_map *map1, *map2;
1935 isl_bool equal;
1936 isl_size n_div;
1938 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1939 isl_bool equal_input, equal_intersection;
1940 isl_set *set1, *set2, *copy, *context;
1942 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1943 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1944 copy = isl_set_copy(set1);
1945 set1 = isl_set_gist(set1, isl_set_copy(context));
1946 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1947 equal = isl_set_is_equal(set1, set2);
1948 isl_set_free(set1);
1949 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1950 equal_input = isl_set_is_equal(set1, copy);
1951 isl_set_free(copy);
1952 set1 = isl_set_intersect(set1, isl_set_copy(context));
1953 set2 = isl_set_intersect(set2, context);
1954 equal_intersection = isl_set_is_equal(set1, set2);
1955 isl_set_free(set2);
1956 isl_set_free(set1);
1957 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1958 return -1;
1959 if (!equal)
1960 isl_die(ctx, isl_error_unknown,
1961 "incorrect gist result", return -1);
1962 if (!equal_input)
1963 isl_die(ctx, isl_error_unknown,
1964 "gist modified input", return -1);
1965 if (!equal_input)
1966 isl_die(ctx, isl_error_unknown,
1967 "inconsistent gist test case", return -1);
1970 if (test_gist_fail(ctx) < 0)
1971 return -1;
1973 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1974 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1975 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1976 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1977 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1978 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1979 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1980 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1981 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1982 bset1 = isl_basic_set_read_from_str(ctx, str);
1983 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1984 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1985 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1986 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1987 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1988 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1989 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1990 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1991 bset2 = isl_basic_set_read_from_str(ctx, str);
1992 bset1 = isl_basic_set_gist(bset1, bset2);
1993 assert(bset1 && bset1->n_div == 0);
1994 isl_basic_set_free(bset1);
1996 /* Check that the integer divisions of the second disjunct
1997 * do not spread to the first disjunct.
1999 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
2000 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
2001 "(exists (e0 = [(-1 + t1)/16], "
2002 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
2003 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
2004 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
2005 "o0 <= 4294967295 and t1 <= -1)) }";
2006 map1 = isl_map_read_from_str(ctx, str);
2007 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
2008 map2 = isl_map_read_from_str(ctx, str);
2009 map1 = isl_map_gist(map1, map2);
2010 if (!map1)
2011 return -1;
2012 if (map1->n != 1)
2013 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
2014 isl_map_free(map1); return -1);
2015 n_div = isl_basic_map_dim(map1->p[0], isl_dim_div);
2016 isl_map_free(map1);
2017 if (n_div < 0)
2018 return -1;
2019 if (n_div != 1)
2020 isl_die(ctx, isl_error_unknown, "expecting single div",
2021 return -1);
2023 if (test_gist_empty(ctx) < 0)
2024 return -1;
2025 if (test_plain_gist(ctx) < 0)
2026 return -1;
2028 return 0;
2031 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
2033 isl_set *set, *set2;
2034 int equal;
2035 int one;
2037 set = isl_set_read_from_str(ctx, str);
2038 set = isl_set_coalesce(set);
2039 set2 = isl_set_read_from_str(ctx, str);
2040 equal = isl_set_is_equal(set, set2);
2041 one = set && set->n == 1;
2042 isl_set_free(set);
2043 isl_set_free(set2);
2045 if (equal < 0)
2046 return -1;
2047 if (!equal)
2048 isl_die(ctx, isl_error_unknown,
2049 "coalesced set not equal to input", return -1);
2050 if (check_one && !one)
2051 isl_die(ctx, isl_error_unknown,
2052 "coalesced set should not be a union", return -1);
2054 return 0;
2057 /* Inputs for coalescing tests with unbounded wrapping.
2058 * "str" is a string representation of the input set.
2059 * "single_disjunct" is set if we expect the result to consist of
2060 * a single disjunct.
2062 struct {
2063 int single_disjunct;
2064 const char *str;
2065 } coalesce_unbounded_tests[] = {
2066 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
2067 "-x - y + 1 >= 0 and -3 <= z <= 3;"
2068 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
2069 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
2070 "-10 <= y <= 0}" },
2071 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
2072 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
2073 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
2074 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
2075 { 0, "{ [x, y, z] : 0 <= x,y,z <= 100 and 0 < z <= 2 + 2x + 2y; "
2076 "[x, y, 0] : x,y <= 100 and y <= 9 + 11x and x <= 9 + 11y }" },
2077 { 1, "{ [0:1, 0:1]; [0, 2:3] }" },
2078 { 1, "{ [0:1, 0:1]; [0, 2:3]; [1, -2:-1] }" },
2079 { 1, "{ [0:3, 0:1]; [1:2, 2:5] }" },
2080 { 1, "{ [0:3, 0:1]; [0:2, 2:5] }" },
2081 { 1, "{ [0:3, 0:1]; [1:3, 2:5] }" },
2082 { 0, "{ [0:3, 0:1]; [1:4, 2:5] }" },
2083 { 0, "{ [0:3, 0:1]; [1:5, 2:5] }" },
2086 /* Test the functionality of isl_set_coalesce with the bounded wrapping
2087 * option turned off.
2089 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
2091 int i;
2092 int r = 0;
2093 int bounded;
2095 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
2096 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
2098 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
2099 const char *str = coalesce_unbounded_tests[i].str;
2100 int check_one = coalesce_unbounded_tests[i].single_disjunct;
2101 if (test_coalesce_set(ctx, str, check_one) >= 0)
2102 continue;
2103 r = -1;
2104 break;
2107 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
2109 return r;
2112 /* Inputs for coalescing tests.
2113 * "str" is a string representation of the input set.
2114 * "single_disjunct" is set if we expect the result to consist of
2115 * a single disjunct.
2117 struct {
2118 int single_disjunct;
2119 const char *str;
2120 } coalesce_tests[] = {
2121 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
2122 "y >= x & x >= 2 & 5 >= y }" },
2123 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
2124 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
2125 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
2126 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
2127 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
2128 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
2129 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
2130 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
2131 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
2132 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
2133 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
2134 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
2135 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
2136 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
2137 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
2138 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
2139 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
2140 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
2141 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
2142 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
2143 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
2144 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
2145 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
2146 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
2147 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
2148 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
2149 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
2150 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
2151 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
2152 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
2153 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
2154 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
2155 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
2156 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
2157 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
2158 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
2159 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
2160 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
2161 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
2162 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
2163 "[o0, o1, o2, o3, o4, o5, o6]] : "
2164 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
2165 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
2166 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
2167 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
2168 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
2169 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
2170 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
2171 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
2172 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
2173 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
2174 "o6 >= i3 + i6 - o3 and M >= 0 and "
2175 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
2176 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
2177 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
2178 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
2179 "(o0 = 0 and M >= 2 and N >= 3) or "
2180 "(M = 0 and o0 = 0 and N >= 3) }" },
2181 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
2182 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
2183 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
2184 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
2185 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
2186 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
2187 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
2188 "(y = 3 and x = 1) }" },
2189 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
2190 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
2191 "i1 <= M and i3 <= M and i4 <= M) or "
2192 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
2193 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
2194 "i4 <= -1 + M) }" },
2195 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
2196 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
2197 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
2198 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
2199 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
2200 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
2201 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
2202 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
2203 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
2204 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
2205 { 0, "{ [a, b] : exists e : 2e = a and "
2206 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
2207 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
2208 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
2209 "j >= 1 and j' <= i + j - i' and i >= 1; "
2210 "[1, 1, 1, 1] }" },
2211 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
2212 "[i,j] : exists a : j = 3a }" },
2213 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
2214 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
2215 "a >= 3) or "
2216 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
2217 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
2218 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
2219 "c <= 6 + 8a and a >= 3; "
2220 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
2221 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
2222 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2223 "[x,0] : 3 <= x <= 4 }" },
2224 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
2225 "[x,0] : 4 <= x <= 5 }" },
2226 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2227 "[x,0] : 3 <= x <= 5 }" },
2228 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
2229 "[x,0] : 3 <= x <= 4 }" },
2230 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
2231 "i1 <= 0; "
2232 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
2233 { 1, "{ [0,0]; [1,1] }" },
2234 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
2235 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
2236 "ii <= k;"
2237 "[k, 0, k] : k <= 6 and k >= 1 }" },
2238 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
2239 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
2240 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
2241 { 1, "[n] -> { [1] : n >= 0;"
2242 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
2243 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
2244 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
2245 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
2246 "2e0 <= x and 2e0 <= n);"
2247 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
2248 "n >= 0) }" },
2249 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
2250 "128e0 >= -134 + 127t1 and t1 >= 2 and "
2251 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
2252 "t1 = 1 }" },
2253 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
2254 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
2255 "[0, 0] }" },
2256 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
2257 "t1 >= 13 and t1 <= 16);"
2258 "[t1] : t1 <= 15 and t1 >= 12 }" },
2259 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
2260 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
2261 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
2262 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
2263 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
2264 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
2265 "i <= 4j + 2 }" },
2266 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
2267 "(exists (e0 : 3e0 = -2 + c0)) }" },
2268 { 0, "[n, b0, t0] -> "
2269 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2270 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2271 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2272 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2273 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
2274 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
2275 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
2276 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
2277 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2278 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2279 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2280 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
2281 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
2282 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
2283 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
2284 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
2285 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
2286 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
2287 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
2288 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
2289 { 0, "{ [i0, i1, i2] : "
2290 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
2291 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
2292 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
2293 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
2294 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
2295 "32e0 <= 31 + i0)) or "
2296 "i0 >= 0 }" },
2297 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
2298 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
2299 "2*floor((c)/2) = c and 0 <= a <= 192;"
2300 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
2302 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
2303 "(0 <= a <= b <= n) }" },
2304 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
2305 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
2306 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
2307 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2308 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2309 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2310 "b = 3 and 9e0 <= -19 + 2c)) }" },
2311 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2312 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2313 "(a = 4 and b = 3 and "
2314 "9*floor((-16 + 2c)/9) <= -19 + 2c) }" },
2315 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
2316 "(b = -1 + a and 0 < a <= 3 and "
2317 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2318 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2319 "b = 3 and 9e0 <= -19 + 2c)) }" },
2320 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2321 "[1, 0] }" },
2322 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2323 "[0, 1] }" },
2324 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2325 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2326 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
2327 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
2328 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
2329 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
2330 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
2331 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
2332 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
2333 { 1, "{ [a] : a <= 8 and "
2334 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
2335 { 1, "{ [x, y] : 2y = -x and x <= 0 or "
2336 "x <= -1 and 2y <= -x - 1 and 2y >= x - 1 }" },
2337 { 0, "{ [x, y] : 2y = -x and x <= 0 or "
2338 "x <= -2 and 2y <= -x - 1 and 2y >= x - 1 }" },
2339 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2340 "(a < 0 and 3*floor((a)/3) < a) }" },
2341 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2342 "(a < -1 and 3*floor((a)/3) < a) }" },
2343 { 1, "{ [a, b] : a <= 1024 and b >= 0 and "
2344 "((-31 - a + b <= 32*floor((-1 - a)/32) <= -33 + b and "
2345 "32*floor((-1 - a)/32) <= -16 + b + 16*floor((-1 - a)/16))"
2346 "or (2 <= a <= 15 and b < a)) }" },
2347 { 1, "{ [a] : a > 0 and ((16*floor((a)/16) < a and "
2348 "32*floor((a)/32) < a) or a <= 15) }" },
2349 { 1, "{ [a, b, c, d] : (-a + d) mod 64 = 0 and a <= 8 and b <= 1 and "
2350 "10 - a <= c <= 3 and d >= 5 and 9 - 64b <= d <= 70;"
2351 "[a, b = 1, c, d] : (-a + d) mod 64 = 0 and a <= 8 and c >= 4 and "
2352 "10 - a <= c <= 5 and 5 <= d <= 73 - c }" },
2353 { 1, "[n, m] -> { S_0[i] : (-n + i) mod 3 = 0 and m >= 3 + n and "
2354 "i >= n and 3*floor((2 + n + 2m)/3) <= n + 3m - i; "
2355 "S_0[n] : n <= m <= 2 + n }" },
2356 { 1, "{ [a, b] : exists (e0: 0 <= a <= 1 and b >= 0 and "
2357 "2e0 >= -5 + a + 2b and 2e0 >= -1 + a + b and "
2358 "2e0 <= a + b); "
2359 "[a, b] : exists (e0: 0 <= a <= 1 and 2e0 >= -5 + a + 2b and "
2360 "2e0 >= -1 - a + b and 2e0 <= -a + b and "
2361 "2e0 < -a + 2b) }" },
2362 { 1, "{ [i, j, i - 8j] : 8 <= i <= 63 and -7 + i <= 8j <= i; "
2363 "[i, 0, i] : 0 <= i <= 7 }" },
2364 { 1, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [1, 1] }" },
2365 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [0, 2] }" },
2366 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [-1, 3] }" },
2367 { 1, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [1, 1] }" },
2368 { 0, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [2, 1] }" },
2369 { 0, "{ [a, c] : (2 + a) mod 4 = 0 or "
2370 "(c = 4 + a and 4 * floor((a)/4) = a and a >= 0 and a <= 4) or "
2371 "(c = 3 + a and 4 * floor((-1 + a)/4) = -1 + a and "
2372 "a > 0 and a <= 5) }" },
2373 { 1, "{ [1, 0, 0]; [a, b, c] : -1 <= -a < b <= 0 and 2c > b }" },
2374 { 0, "{ [j, a, l] : a mod 2 = 0 and j <= 29 and a >= 2 and "
2375 "2a <= -5 + j and 32j + 2a + 2 <= 4l < 33j; "
2376 "[j, 0, l] : 4 <= j <= 29 and -3 + 33j <= 4l <= 33j }" },
2377 { 0, "{ [0:1, 0:1]; [0, 2:3] }" },
2378 { 1, "{ [a] : (a = 0 or ((1 + a) mod 2 = 0 and 0 < a <= 15) or "
2379 "((a) mod 2 = 0 and 0 < a <= 15)) }" },
2382 /* A specialized coalescing test case that would result
2383 * in a segmentation fault or a failed assertion in earlier versions of isl.
2385 static int test_coalesce_special(struct isl_ctx *ctx)
2387 const char *str;
2388 isl_map *map1, *map2;
2390 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2391 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
2392 "(y = 201 and o1 <= 239 and o1 >= 212) or "
2393 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
2394 "o1 <= 239 and o1 >= 212)) or "
2395 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
2396 "o1 <= 241 and o1 >= 240));"
2397 "[S_L220_OUT[] -> T7[]] -> "
2398 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
2399 "(y = 2 and o1 <= 241 and o1 >= 212) or "
2400 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
2401 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
2402 map1 = isl_map_read_from_str(ctx, str);
2403 map1 = isl_map_align_divs_internal(map1);
2404 map1 = isl_map_coalesce(map1);
2405 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2406 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
2407 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
2408 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
2409 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
2410 map2 = isl_map_read_from_str(ctx, str);
2411 map2 = isl_map_union(map2, map1);
2412 map2 = isl_map_align_divs_internal(map2);
2413 map2 = isl_map_coalesce(map2);
2414 isl_map_free(map2);
2415 if (!map2)
2416 return -1;
2418 return 0;
2421 /* A specialized coalescing test case that would result in an assertion
2422 * in an earlier version of isl.
2423 * The explicit call to isl_basic_set_union prevents the implicit
2424 * equality constraints in the first basic map from being detected prior
2425 * to the call to isl_set_coalesce, at least at the point
2426 * where this test case was introduced.
2428 static isl_stat test_coalesce_special2(struct isl_ctx *ctx)
2430 const char *str;
2431 isl_basic_set *bset1, *bset2;
2432 isl_set *set;
2434 str = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
2435 bset1 = isl_basic_set_read_from_str(ctx, str);
2436 str = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }" ;
2437 bset2 = isl_basic_set_read_from_str(ctx, str);
2438 set = isl_basic_set_union(bset1, bset2);
2439 set = isl_set_coalesce(set);
2440 isl_set_free(set);
2442 return isl_stat_non_null(set);
2445 /* Check that calling isl_set_coalesce does not leave other sets
2446 * that may share some information with the input to isl_set_coalesce
2447 * in an inconsistent state.
2448 * In particular, older versions of isl would modify all copies
2449 * of the basic sets in the isl_set_coalesce input in a way
2450 * that could leave them in an inconsistent state.
2451 * The result of printing any other set containing one of these
2452 * basic sets would then result in an invalid set description.
2454 static int test_coalesce_special3(isl_ctx *ctx)
2456 const char *str;
2457 char *s;
2458 isl_set *set1, *set2;
2459 isl_printer *p;
2461 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
2462 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
2463 set2 = isl_set_read_from_str(ctx, str);
2464 set1 = isl_set_union(set1, isl_set_copy(set2));
2465 set1 = isl_set_coalesce(set1);
2466 isl_set_free(set1);
2468 p = isl_printer_to_str(ctx);
2469 p = isl_printer_print_set(p, set2);
2470 isl_set_free(set2);
2471 s = isl_printer_get_str(p);
2472 isl_printer_free(p);
2473 set1 = isl_set_read_from_str(ctx, s);
2474 free(s);
2475 isl_set_free(set1);
2477 if (!set1)
2478 return -1;
2480 return 0;
2483 /* Check that calling isl_set_coalesce on the intersection of
2484 * the sets described by "s1" and "s2" does not leave other sets
2485 * that may share some information with the input to isl_set_coalesce
2486 * in an inconsistent state.
2487 * In particular, when isl_set_coalesce detects equality constraints,
2488 * it does not immediately perform Gaussian elimination on them,
2489 * but then it needs to ensure that it is performed at some point.
2490 * The input set has implicit equality constraints in the first disjunct.
2491 * It is constructed as an intersection, because otherwise
2492 * those equality constraints would already be detected during parsing.
2494 static isl_stat test_coalesce_intersection(isl_ctx *ctx,
2495 const char *s1, const char *s2)
2497 isl_set *set1, *set2;
2499 set1 = isl_set_read_from_str(ctx, s1);
2500 set2 = isl_set_read_from_str(ctx, s2);
2501 set1 = isl_set_intersect(set1, set2);
2502 isl_set_free(isl_set_coalesce(isl_set_copy(set1)));
2503 set1 = isl_set_coalesce(set1);
2504 isl_set_free(set1);
2506 if (!set1)
2507 return isl_stat_error;
2509 return isl_stat_ok;
2512 /* Check that calling isl_set_coalesce does not leave other sets
2513 * that may share some information with the input to isl_set_coalesce
2514 * in an inconsistent state, for the case where one disjunct
2515 * is a subset of the other.
2517 static isl_stat test_coalesce_special4(isl_ctx *ctx)
2519 const char *s1, *s2;
2521 s1 = "{ [a, b] : b <= 0 or a <= 1 }";
2522 s2 = "{ [a, b] : -1 <= -a < b }";
2523 return test_coalesce_intersection(ctx, s1, s2);
2526 /* Check that calling isl_set_coalesce does not leave other sets
2527 * that may share some information with the input to isl_set_coalesce
2528 * in an inconsistent state, for the case where two disjuncts
2529 * can be fused.
2531 static isl_stat test_coalesce_special5(isl_ctx *ctx)
2533 const char *s1, *s2;
2535 s1 = "{ [a, b, c] : b <= 0 }";
2536 s2 = "{ [a, b, c] : -1 <= -a < b and (c >= 0 or c < 0) }";
2537 return test_coalesce_intersection(ctx, s1, s2);
2540 /* Check that calling isl_set_coalesce does not leave other sets
2541 * that may share some information with the input to isl_set_coalesce
2542 * in an inconsistent state, for the case where two disjuncts
2543 * can be fused and where both disjuncts have implicit equality constraints.
2545 static isl_stat test_coalesce_special6(isl_ctx *ctx)
2547 const char *s1, *s2;
2549 s1 = "{ [a, b, c] : c <= 0 }";
2550 s2 = "{ [a, b, c] : 0 <= a <= b <= c or (0 <= b <= c and a > 0) }";
2551 return test_coalesce_intersection(ctx, s1, s2);
2554 /* Test the functionality of isl_set_coalesce.
2555 * That is, check that the output is always equal to the input
2556 * and in some cases that the result consists of a single disjunct.
2558 static int test_coalesce(struct isl_ctx *ctx)
2560 int i;
2562 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2563 const char *str = coalesce_tests[i].str;
2564 int check_one = coalesce_tests[i].single_disjunct;
2565 if (test_coalesce_set(ctx, str, check_one) < 0)
2566 return -1;
2569 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2570 return -1;
2571 if (test_coalesce_special(ctx) < 0)
2572 return -1;
2573 if (test_coalesce_special2(ctx) < 0)
2574 return -1;
2575 if (test_coalesce_special3(ctx) < 0)
2576 return -1;
2577 if (test_coalesce_special4(ctx) < 0)
2578 return -1;
2579 if (test_coalesce_special5(ctx) < 0)
2580 return -1;
2581 if (test_coalesce_special6(ctx) < 0)
2582 return -1;
2585 return 0;
2588 /* Construct a representation of the graph on the right of Figure 1
2589 * in "Computing the Transitive Closure of a Union of
2590 * Affine Integer Tuple Relations".
2592 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2594 isl_set *dom;
2595 isl_map *up, *right;
2597 dom = isl_set_read_from_str(ctx,
2598 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2599 "2 x - 3 y + 3 >= 0 }");
2600 right = isl_map_read_from_str(ctx,
2601 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2602 up = isl_map_read_from_str(ctx,
2603 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2604 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2605 right = isl_map_intersect_range(right, isl_set_copy(dom));
2606 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2607 up = isl_map_intersect_range(up, dom);
2608 return isl_map_union(up, right);
2611 /* Construct a representation of the power of the graph
2612 * on the right of Figure 1 in "Computing the Transitive Closure of
2613 * a Union of Affine Integer Tuple Relations".
2615 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2617 return isl_map_read_from_str(ctx,
2618 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2619 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2620 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2623 /* Construct a representation of the transitive closure of the graph
2624 * on the right of Figure 1 in "Computing the Transitive Closure of
2625 * a Union of Affine Integer Tuple Relations".
2627 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2629 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2632 static int test_closure(isl_ctx *ctx)
2634 const char *str;
2635 isl_map *map, *map2;
2636 isl_bool exact, equal;
2638 /* COCOA example 1 */
2639 map = isl_map_read_from_str(ctx,
2640 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2641 "1 <= i and i < n and 1 <= j and j < n or "
2642 "i2 = i + 1 and j2 = j - 1 and "
2643 "1 <= i and i < n and 2 <= j and j <= n }");
2644 map = isl_map_power(map, &exact);
2645 assert(exact);
2646 isl_map_free(map);
2648 /* COCOA example 1 */
2649 map = isl_map_read_from_str(ctx,
2650 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2651 "1 <= i and i < n and 1 <= j and j < n or "
2652 "i2 = i + 1 and j2 = j - 1 and "
2653 "1 <= i and i < n and 2 <= j and j <= n }");
2654 map = isl_map_transitive_closure(map, &exact);
2655 assert(exact);
2656 map2 = isl_map_read_from_str(ctx,
2657 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2658 "1 <= i and i < n and 1 <= j and j <= n and "
2659 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2660 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2661 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2662 assert(isl_map_is_equal(map, map2));
2663 isl_map_free(map2);
2664 isl_map_free(map);
2666 map = isl_map_read_from_str(ctx,
2667 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2668 " 0 <= y and y <= n }");
2669 map = isl_map_transitive_closure(map, &exact);
2670 map2 = isl_map_read_from_str(ctx,
2671 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2672 " 0 <= y and y <= n }");
2673 assert(isl_map_is_equal(map, map2));
2674 isl_map_free(map2);
2675 isl_map_free(map);
2677 /* COCOA example 2 */
2678 map = isl_map_read_from_str(ctx,
2679 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2680 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2681 "i2 = i + 2 and j2 = j - 2 and "
2682 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2683 map = isl_map_transitive_closure(map, &exact);
2684 assert(exact);
2685 map2 = isl_map_read_from_str(ctx,
2686 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2687 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2688 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2689 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2690 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2691 assert(isl_map_is_equal(map, map2));
2692 isl_map_free(map);
2693 isl_map_free(map2);
2695 /* COCOA Fig.2 left */
2696 map = isl_map_read_from_str(ctx,
2697 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2698 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2699 "j <= n or "
2700 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2701 "j <= 2 i - 3 and j <= n - 2 or "
2702 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2703 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2704 map = isl_map_transitive_closure(map, &exact);
2705 assert(exact);
2706 isl_map_free(map);
2708 /* COCOA Fig.2 right */
2709 map = isl_map_read_from_str(ctx,
2710 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2711 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2712 "j <= n or "
2713 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2714 "j <= 2 i - 4 and j <= n - 3 or "
2715 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2716 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2717 map = isl_map_power(map, &exact);
2718 assert(exact);
2719 isl_map_free(map);
2721 /* COCOA Fig.2 right */
2722 map = isl_map_read_from_str(ctx,
2723 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2724 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2725 "j <= n or "
2726 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2727 "j <= 2 i - 4 and j <= n - 3 or "
2728 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2729 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2730 map = isl_map_transitive_closure(map, &exact);
2731 assert(exact);
2732 map2 = isl_map_read_from_str(ctx,
2733 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2734 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2735 "j <= n and 3 + i + 2 j <= 3 n and "
2736 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2737 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2738 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2739 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2740 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2741 assert(isl_map_is_equal(map, map2));
2742 isl_map_free(map2);
2743 isl_map_free(map);
2745 map = cocoa_fig_1_right_graph(ctx);
2746 map = isl_map_transitive_closure(map, &exact);
2747 assert(exact);
2748 map2 = cocoa_fig_1_right_tc(ctx);
2749 assert(isl_map_is_equal(map, map2));
2750 isl_map_free(map2);
2751 isl_map_free(map);
2753 map = cocoa_fig_1_right_graph(ctx);
2754 map = isl_map_power(map, &exact);
2755 map2 = cocoa_fig_1_right_power(ctx);
2756 equal = isl_map_is_equal(map, map2);
2757 isl_map_free(map2);
2758 isl_map_free(map);
2759 if (equal < 0)
2760 return -1;
2761 if (!exact)
2762 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2763 if (!equal)
2764 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2766 /* COCOA Theorem 1 counter example */
2767 map = isl_map_read_from_str(ctx,
2768 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2769 "i2 = 1 and j2 = j or "
2770 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2771 map = isl_map_transitive_closure(map, &exact);
2772 assert(exact);
2773 isl_map_free(map);
2775 map = isl_map_read_from_str(ctx,
2776 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2777 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2778 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2779 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2780 map = isl_map_transitive_closure(map, &exact);
2781 assert(exact);
2782 isl_map_free(map);
2784 /* Kelly et al 1996, fig 12 */
2785 map = isl_map_read_from_str(ctx,
2786 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2787 "1 <= i,j,j+1 <= n or "
2788 "j = n and j2 = 1 and i2 = i + 1 and "
2789 "1 <= i,i+1 <= n }");
2790 map = isl_map_transitive_closure(map, &exact);
2791 assert(exact);
2792 map2 = isl_map_read_from_str(ctx,
2793 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2794 "1 <= i <= n and i = i2 or "
2795 "1 <= i < i2 <= n and 1 <= j <= n and "
2796 "1 <= j2 <= n }");
2797 assert(isl_map_is_equal(map, map2));
2798 isl_map_free(map2);
2799 isl_map_free(map);
2801 /* Omega's closure4 */
2802 map = isl_map_read_from_str(ctx,
2803 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2804 "1 <= x,y <= 10 or "
2805 "x2 = x + 1 and y2 = y and "
2806 "1 <= x <= 20 && 5 <= y <= 15 }");
2807 map = isl_map_transitive_closure(map, &exact);
2808 assert(exact);
2809 isl_map_free(map);
2811 map = isl_map_read_from_str(ctx,
2812 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2813 map = isl_map_transitive_closure(map, &exact);
2814 assert(!exact);
2815 map2 = isl_map_read_from_str(ctx,
2816 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2817 assert(isl_map_is_equal(map, map2));
2818 isl_map_free(map);
2819 isl_map_free(map2);
2821 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2822 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2823 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2824 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2825 map = isl_map_read_from_str(ctx, str);
2826 map = isl_map_transitive_closure(map, &exact);
2827 assert(exact);
2828 map2 = isl_map_read_from_str(ctx, str);
2829 assert(isl_map_is_equal(map, map2));
2830 isl_map_free(map);
2831 isl_map_free(map2);
2833 str = "{[0] -> [1]; [2] -> [3]}";
2834 map = isl_map_read_from_str(ctx, str);
2835 map = isl_map_transitive_closure(map, &exact);
2836 assert(exact);
2837 map2 = isl_map_read_from_str(ctx, str);
2838 assert(isl_map_is_equal(map, map2));
2839 isl_map_free(map);
2840 isl_map_free(map2);
2842 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2843 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2844 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2845 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2846 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2847 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2848 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2849 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2850 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2851 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2852 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2853 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2854 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2855 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2856 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2857 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2858 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2859 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2860 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2861 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2862 map = isl_map_read_from_str(ctx, str);
2863 map = isl_map_transitive_closure(map, NULL);
2864 assert(map);
2865 isl_map_free(map);
2867 return 0;
2870 /* Check that the actual result of a boolean operation is equal
2871 * to the expected result.
2873 static isl_stat check_bool(isl_ctx *ctx, isl_bool actual, isl_bool expected)
2875 if (actual != expected)
2876 isl_die(ctx, isl_error_unknown,
2877 "incorrect boolean operation", return isl_stat_error);
2878 return isl_stat_ok;
2881 /* Test operations on isl_bool values.
2883 * This tests:
2885 * isl_bool_not
2886 * isl_bool_ok
2888 static int test_isl_bool(isl_ctx *ctx)
2890 if (check_bool(ctx, isl_bool_not(isl_bool_true), isl_bool_false) < 0)
2891 return -1;
2892 if (check_bool(ctx, isl_bool_not(isl_bool_false), isl_bool_true) < 0)
2893 return -1;
2894 if (check_bool(ctx, isl_bool_not(isl_bool_error), isl_bool_error) < 0)
2895 return -1;
2896 if (check_bool(ctx, isl_bool_ok(0), isl_bool_false) < 0)
2897 return -1;
2898 if (check_bool(ctx, isl_bool_ok(1), isl_bool_true) < 0)
2899 return -1;
2900 if (check_bool(ctx, isl_bool_ok(-1), isl_bool_true) < 0)
2901 return -1;
2902 if (check_bool(ctx, isl_bool_ok(2), isl_bool_true) < 0)
2903 return -1;
2904 if (check_bool(ctx, isl_bool_ok(-2), isl_bool_true) < 0)
2905 return -1;
2907 return 0;
2910 static int test_lex(struct isl_ctx *ctx)
2912 isl_space *space;
2913 isl_map *map;
2914 int empty;
2916 space = isl_space_set_alloc(ctx, 0, 0);
2917 map = isl_map_lex_le(space);
2918 empty = isl_map_is_empty(map);
2919 isl_map_free(map);
2921 if (empty < 0)
2922 return -1;
2923 if (empty)
2924 isl_die(ctx, isl_error_unknown,
2925 "expecting non-empty result", return -1);
2927 return 0;
2930 /* Inputs for isl_map_lexmin tests.
2931 * "map" is the input and "lexmin" is the expected result.
2933 struct {
2934 const char *map;
2935 const char *lexmin;
2936 } lexmin_tests [] = {
2937 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2938 "{ [x] -> [5] : 6 <= x <= 8; "
2939 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2940 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2941 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2942 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2943 "{ [x] -> [y] : (4y = x and x >= 0) or "
2944 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2945 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2946 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2947 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2948 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2949 /* Check that empty pieces are properly combined. */
2950 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2951 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2952 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2953 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2954 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2955 "x >= 4 }" },
2956 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2957 "a <= 255 and c <= 255 and d <= 255 - j and "
2958 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2959 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2960 "247d <= 247 + i and 248 - b <= 248d <= c and "
2961 "254d >= i - a + b and 254d >= -a + b and "
2962 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2963 "{ [i, k, j] -> "
2964 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2965 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2966 "247j >= 62738 - i and 509j <= 129795 + i and "
2967 "742j >= 188724 - i; "
2968 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2969 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2970 "16*floor((8 + b)/16) <= 7 + b; "
2971 "[a] -> [1] }",
2972 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2973 "[a] -> [b = 0] : 0 < a <= 509 }" },
2974 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2975 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2976 { "{ rat: [i] : 21 <= 2i <= 29 or i = 5 }", "{ rat: [5] }" },
2979 static int test_lexmin(struct isl_ctx *ctx)
2981 int i;
2982 int equal;
2983 const char *str;
2984 isl_basic_map *bmap;
2985 isl_map *map, *map2;
2986 isl_set *set;
2987 isl_set *set2;
2988 isl_pw_multi_aff *pma;
2990 str = "[p0, p1] -> { [] -> [] : "
2991 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2992 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2993 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2994 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2995 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2996 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2997 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2998 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2999 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
3000 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
3001 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
3002 map = isl_map_read_from_str(ctx, str);
3003 map = isl_map_lexmin(map);
3004 isl_map_free(map);
3005 if (!map)
3006 return -1;
3008 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
3009 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
3010 set = isl_set_read_from_str(ctx, str);
3011 set = isl_set_lexmax(set);
3012 str = "[C] -> { [obj,a,b,c] : C = 8 }";
3013 set2 = isl_set_read_from_str(ctx, str);
3014 set = isl_set_intersect(set, set2);
3015 assert(!isl_set_is_empty(set));
3016 isl_set_free(set);
3018 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
3019 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
3020 map = isl_map_lexmin(map);
3021 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
3022 equal = isl_map_is_equal(map, map2);
3023 isl_map_free(map);
3024 isl_map_free(map2);
3026 if (equal < 0)
3027 return -1;
3028 if (!equal)
3029 isl_die(ctx, isl_error_unknown,
3030 "unexpected result", return -1);
3033 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
3034 " 8i' <= i and 8i' >= -7 + i }";
3035 bmap = isl_basic_map_read_from_str(ctx, str);
3036 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
3037 map2 = isl_map_from_pw_multi_aff(pma);
3038 map = isl_map_from_basic_map(bmap);
3039 assert(isl_map_is_equal(map, map2));
3040 isl_map_free(map);
3041 isl_map_free(map2);
3043 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
3044 " 8i' <= i and 8i' >= -7 + i }";
3045 set = isl_set_read_from_str(ctx, str);
3046 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
3047 set2 = isl_set_from_pw_multi_aff(pma);
3048 equal = isl_set_is_equal(set, set2);
3049 isl_set_free(set);
3050 isl_set_free(set2);
3051 if (equal < 0)
3052 return -1;
3053 if (!equal)
3054 isl_die(ctx, isl_error_unknown,
3055 "unexpected difference between set and "
3056 "piecewise affine expression", return -1);
3058 return 0;
3061 /* Inputs for isl_pw_multi_aff_max_multi_val tests.
3062 * "pma" is the input.
3063 * "res" is the expected result.
3065 static struct {
3066 const char *pma;
3067 const char *res;
3068 } opt_pw_tests[] = {
3069 { "{ [-1] -> [-1]; [1] -> [1] }", "{ [1] }" },
3070 { "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] : "
3071 "0 <= a, b <= 100 and b mod 2 = 0}", "{ [30] }" },
3072 { "[N] -> { [i,j] -> A[i, -i, i + j] : 0 <= i,j <= N <= 10 }",
3073 "{ A[10, 0, 20] }" },
3074 { "[N] -> {A[N, -N, 2N] : 0 <= N }", "{ A[infty, 0, infty] }" },
3077 /* Perform basic isl_pw_multi_aff_max_multi_val tests.
3079 static isl_stat test_pw_max(struct isl_ctx *ctx)
3081 int i;
3082 isl_pw_multi_aff *pma;
3083 isl_multi_val *mv;
3084 isl_stat r;
3086 for (i = 0; i < ARRAY_SIZE(opt_pw_tests); ++i) {
3087 pma = isl_pw_multi_aff_read_from_str(ctx, opt_pw_tests[i].pma);
3088 mv = isl_pw_multi_aff_max_multi_val(pma);
3089 r = multi_val_check_plain_equal(mv, opt_pw_tests[i].res);
3090 isl_multi_val_free(mv);
3092 if (r < 0)
3093 return isl_stat_error;
3096 return isl_stat_ok;
3099 /* A specialized isl_set_min_val test case that would return the wrong result
3100 * in earlier versions of isl.
3101 * The explicit call to isl_basic_set_union prevents the second basic set
3102 * from being determined to be empty prior to the call to isl_set_min_val,
3103 * at least at the point where this test case was introduced.
3105 static int test_min_special(isl_ctx *ctx)
3107 const char *str;
3108 isl_basic_set *bset1, *bset2;
3109 isl_set *set;
3110 isl_aff *obj;
3111 isl_val *res;
3112 int ok;
3114 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
3115 bset1 = isl_basic_set_read_from_str(ctx, str);
3116 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
3117 bset2 = isl_basic_set_read_from_str(ctx, str);
3118 set = isl_basic_set_union(bset1, bset2);
3119 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
3121 res = isl_set_min_val(set, obj);
3122 ok = isl_val_cmp_si(res, 5) == 0;
3124 isl_aff_free(obj);
3125 isl_set_free(set);
3126 isl_val_free(res);
3128 if (!res)
3129 return -1;
3130 if (!ok)
3131 isl_die(ctx, isl_error_unknown, "unexpected minimum",
3132 return -1);
3134 return 0;
3137 /* A specialized isl_set_min_val test case that would return an error
3138 * in earlier versions of isl.
3140 static int test_min_special2(isl_ctx *ctx)
3142 const char *str;
3143 isl_basic_set *bset;
3144 isl_aff *obj;
3145 isl_val *res;
3147 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
3148 bset = isl_basic_set_read_from_str(ctx, str);
3150 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
3152 res = isl_basic_set_max_val(bset, obj);
3154 isl_basic_set_free(bset);
3155 isl_aff_free(obj);
3156 isl_val_free(res);
3158 if (!res)
3159 return -1;
3161 return 0;
3164 /* Check that the result of isl_set_min_multi_pw_aff
3165 * on the union of the sets with string descriptions "s1" and "s2"
3166 * consists of a single expression (on a single cell).
3168 static isl_stat check_single_expr_min(isl_ctx *ctx, const char *s1,
3169 const char *s2)
3171 isl_size n;
3172 isl_set *set1, *set2;
3173 isl_multi_pw_aff *mpa;
3174 isl_pw_multi_aff *pma;
3176 set1 = isl_set_read_from_str(ctx, s1);
3177 set2 = isl_set_read_from_str(ctx, s2);
3178 set1 = isl_set_union(set1, set2);
3179 mpa = isl_set_min_multi_pw_aff(set1);
3180 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
3181 n = isl_pw_multi_aff_n_piece(pma);
3182 isl_pw_multi_aff_free(pma);
3184 if (n < 0)
3185 return isl_stat_error;
3186 if (n != 1)
3187 isl_die(ctx, isl_error_unknown, "expecting single expression",
3188 return isl_stat_error);
3189 return isl_stat_ok;
3192 /* A specialized isl_set_min_multi_pw_aff test that checks
3193 * that the minimum of 2N and 3N for N >= 0 is represented
3194 * by a single expression, without splitting off the special case N = 0.
3195 * Do this for both orderings.
3197 static int test_min_mpa(isl_ctx *ctx)
3199 const char *s1, *s2;
3201 s1 = "[N=0:] -> { [1, 3N:] }";
3202 s2 = "[N=0:] -> { [10, 2N:] }";
3203 if (check_single_expr_min(ctx, s1, s2) < 0)
3204 return -1;
3205 if (check_single_expr_min(ctx, s2, s1) < 0)
3206 return -1;
3208 return 0;
3211 struct {
3212 const char *set;
3213 const char *obj;
3214 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
3215 __isl_keep isl_aff *obj);
3216 const char *res;
3217 } opt_tests[] = {
3218 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
3219 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
3220 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
3221 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
3222 &isl_set_max_val, "30" },
3226 /* Perform basic isl_set_min_val and isl_set_max_val tests.
3227 * In particular, check the results on non-convex inputs.
3229 static int test_min(struct isl_ctx *ctx)
3231 int i;
3232 isl_set *set;
3233 isl_aff *obj;
3234 isl_val *val, *res;
3235 isl_bool ok;
3237 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
3238 set = isl_set_read_from_str(ctx, opt_tests[i].set);
3239 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
3240 res = isl_val_read_from_str(ctx, opt_tests[i].res);
3241 val = opt_tests[i].fn(set, obj);
3242 ok = isl_val_eq(res, val);
3243 isl_val_free(res);
3244 isl_val_free(val);
3245 isl_aff_free(obj);
3246 isl_set_free(set);
3248 if (ok < 0)
3249 return -1;
3250 if (!ok)
3251 isl_die(ctx, isl_error_unknown,
3252 "unexpected optimum", return -1);
3255 if (test_pw_max(ctx) < 0)
3256 return -1;
3257 if (test_min_special(ctx) < 0)
3258 return -1;
3259 if (test_min_special2(ctx) < 0)
3260 return -1;
3262 return 0;
3265 struct must_may {
3266 isl_map *must;
3267 isl_map *may;
3270 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
3271 void *dep_user, void *user)
3273 struct must_may *mm = (struct must_may *)user;
3275 if (must)
3276 mm->must = isl_map_union(mm->must, dep);
3277 else
3278 mm->may = isl_map_union(mm->may, dep);
3280 return isl_stat_ok;
3283 static int common_space(void *first, void *second)
3285 int depth = *(int *)first;
3286 return 2 * depth;
3289 static int map_is_equal(__isl_keep isl_map *map, const char *str)
3291 isl_map *map2;
3292 int equal;
3294 if (!map)
3295 return -1;
3297 map2 = isl_map_read_from_str(map->ctx, str);
3298 equal = isl_map_is_equal(map, map2);
3299 isl_map_free(map2);
3301 return equal;
3304 static int map_check_equal(__isl_keep isl_map *map, const char *str)
3306 int equal;
3308 equal = map_is_equal(map, str);
3309 if (equal < 0)
3310 return -1;
3311 if (!equal)
3312 isl_die(isl_map_get_ctx(map), isl_error_unknown,
3313 "result not as expected", return -1);
3314 return 0;
3317 /* Is "set" equal to the set described by "str"?
3319 static isl_bool set_is_equal(__isl_keep isl_set *set, const char *str)
3321 isl_set *set2;
3322 isl_bool equal;
3324 if (!set)
3325 return isl_bool_error;
3327 set2 = isl_set_read_from_str(isl_set_get_ctx(set), str);
3328 equal = isl_set_is_equal(set, set2);
3329 isl_set_free(set2);
3331 return equal;
3334 /* Check that "set" is equal to the set described by "str".
3336 static isl_stat set_check_equal(__isl_keep isl_set *set, const char *str)
3338 isl_bool equal;
3340 equal = set_is_equal(set, str);
3341 if (equal < 0)
3342 return isl_stat_error;
3343 if (!equal)
3344 isl_die(isl_set_get_ctx(set), isl_error_unknown,
3345 "result not as expected", return isl_stat_error);
3346 return isl_stat_ok;
3349 /* Is "uset" equal to the union set described by "str"?
3351 static isl_bool uset_is_equal(__isl_keep isl_union_set *uset, const char *str)
3353 isl_union_set *uset2;
3354 isl_bool equal;
3356 if (!uset)
3357 return isl_bool_error;
3359 uset2 = isl_union_set_read_from_str(isl_union_set_get_ctx(uset), str);
3360 equal = isl_union_set_is_equal(uset, uset2);
3361 isl_union_set_free(uset2);
3363 return equal;
3366 /* Check that "uset" is equal to the union set described by "str".
3368 static isl_stat uset_check_equal(__isl_keep isl_union_set *uset,
3369 const char *str)
3371 isl_bool equal;
3373 equal = uset_is_equal(uset, str);
3374 if (equal < 0)
3375 return isl_stat_error;
3376 if (!equal)
3377 isl_die(isl_union_set_get_ctx(uset), isl_error_unknown,
3378 "result not as expected", return isl_stat_error);
3379 return isl_stat_ok;
3382 static int test_dep(struct isl_ctx *ctx)
3384 const char *str;
3385 isl_space *space;
3386 isl_map *map;
3387 isl_access_info *ai;
3388 isl_flow *flow;
3389 int depth;
3390 struct must_may mm;
3392 depth = 3;
3394 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3395 map = isl_map_read_from_str(ctx, str);
3396 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3398 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3399 map = isl_map_read_from_str(ctx, str);
3400 ai = isl_access_info_add_source(ai, map, 1, &depth);
3402 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3403 map = isl_map_read_from_str(ctx, str);
3404 ai = isl_access_info_add_source(ai, map, 1, &depth);
3406 flow = isl_access_info_compute_flow(ai);
3407 space = isl_space_alloc(ctx, 0, 3, 3);
3408 mm.must = isl_map_empty(isl_space_copy(space));
3409 mm.may = isl_map_empty(space);
3411 isl_flow_foreach(flow, collect_must_may, &mm);
3413 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
3414 " [1,10,0] -> [2,5,0] }";
3415 assert(map_is_equal(mm.must, str));
3416 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3417 assert(map_is_equal(mm.may, str));
3419 isl_map_free(mm.must);
3420 isl_map_free(mm.may);
3421 isl_flow_free(flow);
3424 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3425 map = isl_map_read_from_str(ctx, str);
3426 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3428 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3429 map = isl_map_read_from_str(ctx, str);
3430 ai = isl_access_info_add_source(ai, map, 1, &depth);
3432 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3433 map = isl_map_read_from_str(ctx, str);
3434 ai = isl_access_info_add_source(ai, map, 0, &depth);
3436 flow = isl_access_info_compute_flow(ai);
3437 space = isl_space_alloc(ctx, 0, 3, 3);
3438 mm.must = isl_map_empty(isl_space_copy(space));
3439 mm.may = isl_map_empty(space);
3441 isl_flow_foreach(flow, collect_must_may, &mm);
3443 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
3444 assert(map_is_equal(mm.must, str));
3445 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3446 assert(map_is_equal(mm.may, str));
3448 isl_map_free(mm.must);
3449 isl_map_free(mm.may);
3450 isl_flow_free(flow);
3453 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3454 map = isl_map_read_from_str(ctx, str);
3455 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3457 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3458 map = isl_map_read_from_str(ctx, str);
3459 ai = isl_access_info_add_source(ai, map, 0, &depth);
3461 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3462 map = isl_map_read_from_str(ctx, str);
3463 ai = isl_access_info_add_source(ai, map, 0, &depth);
3465 flow = isl_access_info_compute_flow(ai);
3466 space = isl_space_alloc(ctx, 0, 3, 3);
3467 mm.must = isl_map_empty(isl_space_copy(space));
3468 mm.may = isl_map_empty(space);
3470 isl_flow_foreach(flow, collect_must_may, &mm);
3472 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
3473 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3474 assert(map_is_equal(mm.may, str));
3475 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3476 assert(map_is_equal(mm.must, str));
3478 isl_map_free(mm.must);
3479 isl_map_free(mm.may);
3480 isl_flow_free(flow);
3483 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
3484 map = isl_map_read_from_str(ctx, str);
3485 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3487 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3488 map = isl_map_read_from_str(ctx, str);
3489 ai = isl_access_info_add_source(ai, map, 0, &depth);
3491 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
3492 map = isl_map_read_from_str(ctx, str);
3493 ai = isl_access_info_add_source(ai, map, 0, &depth);
3495 flow = isl_access_info_compute_flow(ai);
3496 space = isl_space_alloc(ctx, 0, 3, 3);
3497 mm.must = isl_map_empty(isl_space_copy(space));
3498 mm.may = isl_map_empty(space);
3500 isl_flow_foreach(flow, collect_must_may, &mm);
3502 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
3503 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
3504 assert(map_is_equal(mm.may, str));
3505 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3506 assert(map_is_equal(mm.must, str));
3508 isl_map_free(mm.must);
3509 isl_map_free(mm.may);
3510 isl_flow_free(flow);
3513 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
3514 map = isl_map_read_from_str(ctx, str);
3515 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3517 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3518 map = isl_map_read_from_str(ctx, str);
3519 ai = isl_access_info_add_source(ai, map, 0, &depth);
3521 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
3522 map = isl_map_read_from_str(ctx, str);
3523 ai = isl_access_info_add_source(ai, map, 0, &depth);
3525 flow = isl_access_info_compute_flow(ai);
3526 space = isl_space_alloc(ctx, 0, 3, 3);
3527 mm.must = isl_map_empty(isl_space_copy(space));
3528 mm.may = isl_map_empty(space);
3530 isl_flow_foreach(flow, collect_must_may, &mm);
3532 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
3533 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
3534 assert(map_is_equal(mm.may, str));
3535 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3536 assert(map_is_equal(mm.must, str));
3538 isl_map_free(mm.must);
3539 isl_map_free(mm.may);
3540 isl_flow_free(flow);
3543 depth = 5;
3545 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3546 map = isl_map_read_from_str(ctx, str);
3547 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
3549 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3550 map = isl_map_read_from_str(ctx, str);
3551 ai = isl_access_info_add_source(ai, map, 1, &depth);
3553 flow = isl_access_info_compute_flow(ai);
3554 space = isl_space_alloc(ctx, 0, 5, 5);
3555 mm.must = isl_map_empty(isl_space_copy(space));
3556 mm.may = isl_map_empty(space);
3558 isl_flow_foreach(flow, collect_must_may, &mm);
3560 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
3561 assert(map_is_equal(mm.must, str));
3562 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
3563 assert(map_is_equal(mm.may, str));
3565 isl_map_free(mm.must);
3566 isl_map_free(mm.may);
3567 isl_flow_free(flow);
3569 return 0;
3572 /* Check that the dependence analysis proceeds without errors.
3573 * Earlier versions of isl would break down during the analysis
3574 * due to the use of the wrong spaces.
3576 static int test_flow(isl_ctx *ctx)
3578 const char *str;
3579 isl_union_map *access, *schedule;
3580 isl_union_map *must_dep, *may_dep;
3581 int r;
3583 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
3584 access = isl_union_map_read_from_str(ctx, str);
3585 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
3586 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
3587 "S2[] -> [1,0,0,0]; "
3588 "S3[] -> [-1,0,0,0] }";
3589 schedule = isl_union_map_read_from_str(ctx, str);
3590 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
3591 isl_union_map_copy(access), schedule,
3592 &must_dep, &may_dep, NULL, NULL);
3593 isl_union_map_free(may_dep);
3594 isl_union_map_free(must_dep);
3596 return r;
3599 struct {
3600 const char *map;
3601 int sv;
3602 } sv_tests[] = {
3603 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
3604 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
3605 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
3606 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
3607 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
3608 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
3609 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3610 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3611 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
3614 int test_sv(isl_ctx *ctx)
3616 isl_union_map *umap;
3617 int i;
3618 int sv;
3620 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
3621 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
3622 sv = isl_union_map_is_single_valued(umap);
3623 isl_union_map_free(umap);
3624 if (sv < 0)
3625 return -1;
3626 if (sv_tests[i].sv && !sv)
3627 isl_die(ctx, isl_error_internal,
3628 "map not detected as single valued", return -1);
3629 if (!sv_tests[i].sv && sv)
3630 isl_die(ctx, isl_error_internal,
3631 "map detected as single valued", return -1);
3634 return 0;
3637 struct {
3638 const char *str;
3639 int bijective;
3640 } bijective_tests[] = {
3641 { "[N,M]->{[i,j] -> [i]}", 0 },
3642 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
3643 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
3644 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
3645 { "[N,M]->{[i,j] -> [j,i]}", 1 },
3646 { "[N,M]->{[i,j] -> [i+j]}", 0 },
3647 { "[N,M]->{[i,j] -> []}", 0 },
3648 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
3649 { "[N,M]->{[i,j] -> [2i]}", 0 },
3650 { "[N,M]->{[i,j] -> [i,i]}", 0 },
3651 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
3652 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
3653 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
3656 static int test_bijective(struct isl_ctx *ctx)
3658 isl_map *map;
3659 int i;
3660 int bijective;
3662 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
3663 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
3664 bijective = isl_map_is_bijective(map);
3665 isl_map_free(map);
3666 if (bijective < 0)
3667 return -1;
3668 if (bijective_tests[i].bijective && !bijective)
3669 isl_die(ctx, isl_error_internal,
3670 "map not detected as bijective", return -1);
3671 if (!bijective_tests[i].bijective && bijective)
3672 isl_die(ctx, isl_error_internal,
3673 "map detected as bijective", return -1);
3676 return 0;
3679 /* Inputs for isl_pw_qpolynomial_gist tests.
3680 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
3682 struct {
3683 const char *pwqp;
3684 const char *set;
3685 const char *gist;
3686 } pwqp_gist_tests[] = {
3687 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
3688 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
3689 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
3690 "{ [i] -> -1/2 + 1/2 * i }" },
3691 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
3694 /* Perform some basic isl_pw_qpolynomial_gist tests.
3696 static isl_stat test_pwqp_gist(isl_ctx *ctx)
3698 int i;
3699 const char *str;
3700 isl_set *set;
3701 isl_pw_qpolynomial *pwqp1, *pwqp2;
3702 isl_bool equal;
3704 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
3705 str = pwqp_gist_tests[i].pwqp;
3706 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3707 str = pwqp_gist_tests[i].set;
3708 set = isl_set_read_from_str(ctx, str);
3709 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
3710 str = pwqp_gist_tests[i].gist;
3711 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3712 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3713 equal = isl_pw_qpolynomial_is_zero(pwqp1);
3714 isl_pw_qpolynomial_free(pwqp1);
3716 if (equal < 0)
3717 return isl_stat_error;
3718 if (!equal)
3719 isl_die(ctx, isl_error_unknown,
3720 "unexpected result", return isl_stat_error);
3723 return isl_stat_ok;
3726 /* Perform a basic isl_pw_qpolynomial_max test.
3728 static isl_stat test_pwqp_max(isl_ctx *ctx)
3730 const char *str;
3731 isl_pw_qpolynomial *pwqp;
3732 isl_val *v;
3733 int ok;
3735 str = "{ [x=2:9, y] -> floor((x + 1)/4)^3 - floor((2x)/3)^2 }";
3736 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3737 v = isl_pw_qpolynomial_max(pwqp);
3738 ok = isl_val_cmp_si(v, -1) == 0;
3739 isl_val_free(v);
3741 if (!v)
3742 return isl_stat_error;
3743 if (!ok)
3744 isl_die(ctx, isl_error_unknown, "unexpected maximum",
3745 return isl_stat_error);
3747 return isl_stat_ok;
3750 static int test_pwqp(struct isl_ctx *ctx)
3752 const char *str;
3753 isl_set *set;
3754 isl_pw_qpolynomial *pwqp1, *pwqp2;
3755 int equal;
3757 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3758 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3760 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
3761 isl_dim_in, 1, 1);
3763 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3764 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3766 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3768 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3770 isl_pw_qpolynomial_free(pwqp1);
3772 if (test_pwqp_gist(ctx) < 0)
3773 return -1;
3775 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
3776 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3777 str = "{ [i] -> ([(2 * [i/2])/5]) }";
3778 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3780 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3782 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3784 isl_pw_qpolynomial_free(pwqp1);
3786 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3787 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3788 str = "{ [x] -> x }";
3789 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3791 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3793 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3795 isl_pw_qpolynomial_free(pwqp1);
3797 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3798 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3799 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3800 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3801 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3802 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3803 isl_pw_qpolynomial_free(pwqp1);
3805 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3806 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3807 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3808 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3809 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3810 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3811 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3812 isl_pw_qpolynomial_free(pwqp1);
3813 isl_pw_qpolynomial_free(pwqp2);
3814 if (equal < 0)
3815 return -1;
3816 if (!equal)
3817 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3819 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3820 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3821 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3822 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3823 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3824 isl_val_one(ctx));
3825 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3826 isl_pw_qpolynomial_free(pwqp1);
3827 isl_pw_qpolynomial_free(pwqp2);
3828 if (equal < 0)
3829 return -1;
3830 if (!equal)
3831 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3833 if (test_pwqp_max(ctx) < 0)
3834 return -1;
3836 return 0;
3839 static int test_split_periods(isl_ctx *ctx)
3841 const char *str;
3842 isl_pw_qpolynomial *pwqp;
3844 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3845 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3846 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3847 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3849 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3851 isl_pw_qpolynomial_free(pwqp);
3853 if (!pwqp)
3854 return -1;
3856 return 0;
3859 static int test_union(isl_ctx *ctx)
3861 const char *str;
3862 isl_union_set *uset1, *uset2;
3863 isl_union_map *umap1, *umap2;
3864 int equal;
3866 str = "{ [i] : 0 <= i <= 1 }";
3867 uset1 = isl_union_set_read_from_str(ctx, str);
3868 str = "{ [1] -> [0] }";
3869 umap1 = isl_union_map_read_from_str(ctx, str);
3871 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3872 equal = isl_union_map_is_equal(umap1, umap2);
3874 isl_union_map_free(umap1);
3875 isl_union_map_free(umap2);
3877 if (equal < 0)
3878 return -1;
3879 if (!equal)
3880 isl_die(ctx, isl_error_unknown, "union maps not equal",
3881 return -1);
3883 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3884 umap1 = isl_union_map_read_from_str(ctx, str);
3885 str = "{ A[i]; B[i] }";
3886 uset1 = isl_union_set_read_from_str(ctx, str);
3888 uset2 = isl_union_map_domain(umap1);
3890 equal = isl_union_set_is_equal(uset1, uset2);
3892 isl_union_set_free(uset1);
3893 isl_union_set_free(uset2);
3895 if (equal < 0)
3896 return -1;
3897 if (!equal)
3898 isl_die(ctx, isl_error_unknown, "union sets not equal",
3899 return -1);
3901 return 0;
3904 /* Inputs for basic isl_pw_qpolynomial_bound tests.
3905 * "type" is the type of bound that should be computed.
3906 * "poly" is a string representation of the input.
3907 * "bound" is a string representation of the expected result.
3908 * "tight" is set if the result is expected to be tight.
3910 static struct {
3911 int tight;
3912 enum isl_fold type;
3913 const char *poly;
3914 const char *bound;
3915 } bound_tests[] = {
3916 /* Check that computing a bound of a non-zero polynomial
3917 * over an unbounded domain does not produce a rational value.
3918 * In particular, check that the upper bound is infinity.
3920 { 0, isl_fold_max, "{ [m, n] -> -m * n }", "{ max(infty) }" },
3921 { 1, isl_fold_max, "{ [[a, b, c, d] -> [e]] -> 0 }",
3922 "{ [a, b, c, d] -> max(0) }" },
3923 { 1, isl_fold_max, "{ [[x] -> [x]] -> 1 : exists a : x = 2 a }",
3924 "{ [x] -> max(1) : x mod 2 = 0 }" },
3925 { 1, isl_fold_min, "{ [x=5:10] -> (x + 2)^2 }", "{ min(49) }" },
3926 { 1, isl_fold_max, "{ [0:10] -> 1 }", "{ max(1) }" },
3927 { 1, isl_fold_max, "{ [[m] -> [0:m]] -> m^2 }",
3928 "{ [m] -> max(m^2) : m >= 0 }" },
3931 /* Check that the bound computation can handle differences
3932 * in domain dimension names of the input polynomial and its domain.
3934 static isl_stat test_bound_space(isl_ctx *ctx)
3936 const char *str;
3937 isl_set *set;
3938 isl_pw_qpolynomial *pwqp;
3939 isl_pw_qpolynomial_fold *pwf;
3941 str = "{ [[c] -> [c]] }";
3942 set = isl_set_read_from_str(ctx, str);
3943 str = "{ [[a] -> [b]] -> 1 }";
3944 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3945 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
3946 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3947 isl_pw_qpolynomial_fold_free(pwf);
3949 return isl_stat_non_null(pwf);
3952 /* Perform basic isl_pw_qpolynomial_bound tests.
3954 static int test_bound(isl_ctx *ctx)
3956 int i;
3958 if (test_bound_space(ctx) < 0)
3959 return -1;
3961 for (i = 0; i < ARRAY_SIZE(bound_tests); ++i) {
3962 const char *str;
3963 enum isl_fold type;
3964 isl_bool equal, tight;
3965 isl_pw_qpolynomial *pwqp;
3966 isl_pw_qpolynomial_fold *pwf1, *pwf2;
3968 str = bound_tests[i].poly;
3969 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3970 type = bound_tests[i].type;
3971 pwf1 = isl_pw_qpolynomial_bound(pwqp, type, &tight);
3972 str = bound_tests[i].bound;
3973 pwf2 = isl_pw_qpolynomial_fold_read_from_str(ctx, str);
3974 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf1, pwf2);
3975 isl_pw_qpolynomial_fold_free(pwf2);
3976 isl_pw_qpolynomial_fold_free(pwf1);
3977 if (equal < 0)
3978 return -1;
3979 if (!equal)
3980 isl_die(ctx, isl_error_unknown,
3981 "incorrect bound result", return -1);
3982 if (bound_tests[i].tight && !tight)
3983 isl_die(ctx, isl_error_unknown,
3984 "bound unexpectedly not tight", return -1);
3987 return 0;
3990 /* isl_set is defined to isl_map internally, so the corresponding elements
3991 * are isl_basic_map objects.
3993 #undef EL_BASE
3994 #undef SET_BASE
3995 #define EL_BASE basic_map
3996 #define SET_BASE set
3997 #include "isl_test_list_templ.c"
3999 #undef EL_BASE
4000 #undef SET_BASE
4001 #define EL_BASE basic_set
4002 #define SET_BASE union_set
4003 #include "isl_test_list_templ.c"
4005 #undef EL_BASE
4006 #undef SET_BASE
4007 #define EL_BASE set
4008 #define SET_BASE union_set
4009 #include "isl_test_list_templ.c"
4011 #undef EL_BASE
4012 #undef SET_BASE
4013 #define EL_BASE basic_map
4014 #define SET_BASE map
4015 #include "isl_test_list_templ.c"
4017 #undef EL_BASE
4018 #undef SET_BASE
4019 #define EL_BASE map
4020 #define SET_BASE union_map
4021 #include "isl_test_list_templ.c"
4023 /* Check that the conversion from isl objects to lists works as expected.
4025 static int test_get_list(isl_ctx *ctx)
4027 if (test_get_list_basic_map_from_set(ctx, "{ [0]; [2]; [3] }"))
4028 return -1;
4029 if (test_get_list_basic_set_from_union_set(ctx, "{ A[0]; B[2]; B[3] }"))
4030 return -1;
4031 if (test_get_list_set_from_union_set(ctx, "{ A[0]; A[2]; B[3] }"))
4032 return -1;
4033 if (test_get_list_basic_map_from_map(ctx,
4034 "{ [0] -> [0]; [2] -> [0]; [3] -> [0] }"))
4035 return -1;
4036 if (test_get_list_map_from_union_map(ctx,
4037 "{ A[0] -> [0]; A[2] -> [0]; B[3] -> [0] }"))
4038 return -1;
4040 return 0;
4043 static int test_lift(isl_ctx *ctx)
4045 const char *str;
4046 isl_basic_map *bmap;
4047 isl_basic_set *bset;
4049 str = "{ [i0] : exists e0 : i0 = 4e0 }";
4050 bset = isl_basic_set_read_from_str(ctx, str);
4051 bset = isl_basic_set_lift(bset);
4052 bmap = isl_basic_map_from_range(bset);
4053 bset = isl_basic_map_domain(bmap);
4054 isl_basic_set_free(bset);
4056 return 0;
4059 /* Check that isl_set_is_subset is not confused by identical
4060 * integer divisions.
4061 * The call to isl_set_normalize ensures that the equality constraints
4062 * a = b = 0 are discovered, turning e0 and e1 into identical
4063 * integer divisions. Any further simplification would remove
4064 * the duplicate integer divisions.
4066 static isl_stat test_subset_duplicate_integer_divisions(isl_ctx *ctx)
4068 const char *str;
4069 isl_bool is_subset;
4070 isl_set *set1, *set2;
4072 str = "{ [a, b, c, d] : "
4073 "exists (e0 = floor((a + d)/4), e1 = floor((d)/4), "
4074 "e2 = floor((-a - d + 4 *floor((a + d)/4))/10), "
4075 "e3 = floor((-d + 4*floor((d)/4))/10): "
4076 "10e2 = -a - 2c - d + 4e0 and 10e3 = -2c - d + 4e1 and "
4077 "b >= 0 and a <= 0 and b <= a) }";
4078 set1 = isl_set_read_from_str(ctx, str);
4079 set2 = isl_set_read_from_str(ctx, str);
4080 set2 = isl_set_normalize(set2);
4082 is_subset = isl_set_is_subset(set1, set2);
4084 isl_set_free(set1);
4085 isl_set_free(set2);
4087 if (is_subset < 0)
4088 return isl_stat_error;
4089 if (!is_subset)
4090 isl_die(ctx, isl_error_unknown,
4091 "set is not considered to be a subset of itself",
4092 return isl_stat_error);
4094 return isl_stat_ok;
4097 struct {
4098 const char *set1;
4099 const char *set2;
4100 int subset;
4101 } subset_tests[] = {
4102 { "{ [112, 0] }",
4103 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
4104 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
4105 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
4106 { "{ [65] }",
4107 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
4108 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
4109 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
4110 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
4111 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
4112 "256e0 <= 255i and 256e0 >= -255 + 255i and "
4113 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
4114 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
4115 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
4116 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
4117 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
4118 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
4119 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
4120 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
4121 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
4122 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
4123 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
4124 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
4125 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
4126 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
4127 "4e0 <= -57 + i0 + i1)) or "
4128 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
4129 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
4130 "4e0 >= -61 + i0 + i1)) or "
4131 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
4132 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
4135 static int test_subset(isl_ctx *ctx)
4137 int i;
4138 isl_set *set1, *set2;
4139 int subset;
4141 if (test_subset_duplicate_integer_divisions(ctx) < 0)
4142 return -1;
4144 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
4145 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
4146 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
4147 subset = isl_set_is_subset(set1, set2);
4148 isl_set_free(set1);
4149 isl_set_free(set2);
4150 if (subset < 0)
4151 return -1;
4152 if (subset != subset_tests[i].subset)
4153 isl_die(ctx, isl_error_unknown,
4154 "incorrect subset result", return -1);
4157 return 0;
4160 /* Perform a set subtraction with a set that has a non-obviously empty disjunct.
4161 * Older versions of isl would fail on such cases.
4163 static isl_stat test_subtract_empty(isl_ctx *ctx)
4165 const char *str;
4166 isl_set *s1, *s2;
4168 s1 = isl_set_read_from_str(ctx, "{ [0] }");
4169 str = "{ [a] : (exists (e0, e1, e2: 1056e1 <= 32 + a - 33e0 and "
4170 "1089e1 >= a - 33e0 and 1089e1 <= 1 + a - 33e0 and "
4171 "33e2 >= -a + 33e0 + 1056e1 and "
4172 "33e2 < -2a + 66e0 + 2112e1)) or a = 0 }";
4173 s2 = isl_set_read_from_str(ctx, str);
4174 s1 = isl_set_subtract(s1, s2);
4175 isl_set_free(s1);
4177 return isl_stat_non_null(s1);
4180 struct {
4181 const char *minuend;
4182 const char *subtrahend;
4183 const char *difference;
4184 } subtract_domain_tests[] = {
4185 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
4186 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
4187 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
4190 static int test_subtract(isl_ctx *ctx)
4192 int i;
4193 isl_union_map *umap1, *umap2;
4194 isl_union_pw_multi_aff *upma1, *upma2;
4195 isl_union_set *uset;
4196 int equal;
4198 if (test_subtract_empty(ctx) < 0)
4199 return -1;
4201 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
4202 umap1 = isl_union_map_read_from_str(ctx,
4203 subtract_domain_tests[i].minuend);
4204 uset = isl_union_set_read_from_str(ctx,
4205 subtract_domain_tests[i].subtrahend);
4206 umap2 = isl_union_map_read_from_str(ctx,
4207 subtract_domain_tests[i].difference);
4208 umap1 = isl_union_map_subtract_domain(umap1, uset);
4209 equal = isl_union_map_is_equal(umap1, umap2);
4210 isl_union_map_free(umap1);
4211 isl_union_map_free(umap2);
4212 if (equal < 0)
4213 return -1;
4214 if (!equal)
4215 isl_die(ctx, isl_error_unknown,
4216 "incorrect subtract domain result", return -1);
4219 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
4220 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4221 subtract_domain_tests[i].minuend);
4222 uset = isl_union_set_read_from_str(ctx,
4223 subtract_domain_tests[i].subtrahend);
4224 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4225 subtract_domain_tests[i].difference);
4226 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
4227 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
4228 isl_union_pw_multi_aff_free(upma1);
4229 isl_union_pw_multi_aff_free(upma2);
4230 if (equal < 0)
4231 return -1;
4232 if (!equal)
4233 isl_die(ctx, isl_error_unknown,
4234 "incorrect subtract domain result", return -1);
4237 return 0;
4240 /* Check that intersecting the empty basic set with another basic set
4241 * does not increase the number of constraints. In particular,
4242 * the empty basic set should maintain its canonical representation.
4244 static int test_intersect_1(isl_ctx *ctx)
4246 isl_size n1, n2;
4247 isl_basic_set *bset1, *bset2;
4249 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
4250 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
4251 n1 = isl_basic_set_n_constraint(bset1);
4252 bset1 = isl_basic_set_intersect(bset1, bset2);
4253 n2 = isl_basic_set_n_constraint(bset1);
4254 isl_basic_set_free(bset1);
4255 if (n1 < 0 || n2 < 0)
4256 return -1;
4257 if (n1 != n2)
4258 isl_die(ctx, isl_error_unknown,
4259 "number of constraints of empty set changed",
4260 return -1);
4262 return 0;
4265 /* Check that intersecting a set with itself does not cause
4266 * an explosion in the number of disjuncts.
4268 static isl_stat test_intersect_2(isl_ctx *ctx)
4270 int i;
4271 isl_set *set;
4273 set = isl_set_read_from_str(ctx, "{ [x,y] : x >= 0 or y >= 0 }");
4274 for (i = 0; i < 100; ++i)
4275 set = isl_set_intersect(set, isl_set_copy(set));
4276 isl_set_free(set);
4277 if (!set)
4278 return isl_stat_error;
4279 return isl_stat_ok;
4282 /* Perform some intersection tests.
4284 static int test_intersect(isl_ctx *ctx)
4286 if (test_intersect_1(ctx) < 0)
4287 return -1;
4288 if (test_intersect_2(ctx) < 0)
4289 return -1;
4291 return 0;
4294 int test_factorize(isl_ctx *ctx)
4296 const char *str;
4297 isl_basic_set *bset;
4298 isl_factorizer *f;
4300 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
4301 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
4302 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
4303 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
4304 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
4305 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
4306 "3i5 >= -2i0 - i2 + 3i4 }";
4307 bset = isl_basic_set_read_from_str(ctx, str);
4308 f = isl_basic_set_factorizer(bset);
4309 isl_basic_set_free(bset);
4310 isl_factorizer_free(f);
4311 if (!f)
4312 isl_die(ctx, isl_error_unknown,
4313 "failed to construct factorizer", return -1);
4315 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
4316 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
4317 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
4318 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
4319 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
4320 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
4321 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
4322 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
4323 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
4324 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
4325 bset = isl_basic_set_read_from_str(ctx, str);
4326 f = isl_basic_set_factorizer(bset);
4327 isl_basic_set_free(bset);
4328 isl_factorizer_free(f);
4329 if (!f)
4330 isl_die(ctx, isl_error_unknown,
4331 "failed to construct factorizer", return -1);
4333 return 0;
4336 static isl_stat check_injective(__isl_take isl_map *map, void *user)
4338 int *injective = user;
4340 *injective = isl_map_is_injective(map);
4341 isl_map_free(map);
4343 if (*injective < 0 || !*injective)
4344 return isl_stat_error;
4346 return isl_stat_ok;
4349 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
4350 const char *r, const char *s, int tilable, int parallel)
4352 int i;
4353 isl_union_set *D;
4354 isl_union_map *W, *R, *S;
4355 isl_union_map *empty;
4356 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
4357 isl_union_map *validity, *proximity, *coincidence;
4358 isl_union_map *schedule;
4359 isl_union_map *test;
4360 isl_union_set *delta;
4361 isl_union_set *domain;
4362 isl_set *delta_set;
4363 isl_set *slice;
4364 isl_set *origin;
4365 isl_schedule_constraints *sc;
4366 isl_schedule *sched;
4367 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
4368 isl_size n;
4370 D = isl_union_set_read_from_str(ctx, d);
4371 W = isl_union_map_read_from_str(ctx, w);
4372 R = isl_union_map_read_from_str(ctx, r);
4373 S = isl_union_map_read_from_str(ctx, s);
4375 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
4376 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
4378 empty = isl_union_map_empty(isl_union_map_get_space(S));
4379 isl_union_map_compute_flow(isl_union_map_copy(R),
4380 isl_union_map_copy(W), empty,
4381 isl_union_map_copy(S),
4382 &dep_raw, NULL, NULL, NULL);
4383 isl_union_map_compute_flow(isl_union_map_copy(W),
4384 isl_union_map_copy(W),
4385 isl_union_map_copy(R),
4386 isl_union_map_copy(S),
4387 &dep_waw, &dep_war, NULL, NULL);
4389 dep = isl_union_map_union(dep_waw, dep_war);
4390 dep = isl_union_map_union(dep, dep_raw);
4391 validity = isl_union_map_copy(dep);
4392 coincidence = isl_union_map_copy(dep);
4393 proximity = isl_union_map_copy(dep);
4395 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
4396 sc = isl_schedule_constraints_set_validity(sc, validity);
4397 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
4398 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4399 sched = isl_schedule_constraints_compute_schedule(sc);
4400 schedule = isl_schedule_get_map(sched);
4401 isl_schedule_free(sched);
4402 isl_union_map_free(W);
4403 isl_union_map_free(R);
4404 isl_union_map_free(S);
4406 is_injection = 1;
4407 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
4409 domain = isl_union_map_domain(isl_union_map_copy(schedule));
4410 is_complete = isl_union_set_is_subset(D, domain);
4411 isl_union_set_free(D);
4412 isl_union_set_free(domain);
4414 test = isl_union_map_reverse(isl_union_map_copy(schedule));
4415 test = isl_union_map_apply_range(test, dep);
4416 test = isl_union_map_apply_range(test, schedule);
4418 delta = isl_union_map_deltas(test);
4419 n = isl_union_set_n_set(delta);
4420 if (n < 0) {
4421 isl_union_set_free(delta);
4422 return -1;
4424 if (n == 0) {
4425 is_tilable = 1;
4426 is_parallel = 1;
4427 is_nonneg = 1;
4428 isl_union_set_free(delta);
4429 } else {
4430 isl_size dim;
4432 delta_set = isl_set_from_union_set(delta);
4434 slice = isl_set_universe(isl_set_get_space(delta_set));
4435 for (i = 0; i < tilable; ++i)
4436 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
4437 is_tilable = isl_set_is_subset(delta_set, slice);
4438 isl_set_free(slice);
4440 slice = isl_set_universe(isl_set_get_space(delta_set));
4441 for (i = 0; i < parallel; ++i)
4442 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
4443 is_parallel = isl_set_is_subset(delta_set, slice);
4444 isl_set_free(slice);
4446 origin = isl_set_universe(isl_set_get_space(delta_set));
4447 dim = isl_set_dim(origin, isl_dim_set);
4448 if (dim < 0)
4449 origin = isl_set_free(origin);
4450 for (i = 0; i < dim; ++i)
4451 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
4453 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
4454 delta_set = isl_set_lexmin(delta_set);
4456 is_nonneg = isl_set_is_equal(delta_set, origin);
4458 isl_set_free(origin);
4459 isl_set_free(delta_set);
4462 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
4463 is_injection < 0 || is_complete < 0)
4464 return -1;
4465 if (!is_complete)
4466 isl_die(ctx, isl_error_unknown,
4467 "generated schedule incomplete", return -1);
4468 if (!is_injection)
4469 isl_die(ctx, isl_error_unknown,
4470 "generated schedule not injective on each statement",
4471 return -1);
4472 if (!is_nonneg)
4473 isl_die(ctx, isl_error_unknown,
4474 "negative dependences in generated schedule",
4475 return -1);
4476 if (!is_tilable)
4477 isl_die(ctx, isl_error_unknown,
4478 "generated schedule not as tilable as expected",
4479 return -1);
4480 if (!is_parallel)
4481 isl_die(ctx, isl_error_unknown,
4482 "generated schedule not as parallel as expected",
4483 return -1);
4485 return 0;
4488 /* Compute a schedule for the given instance set, validity constraints,
4489 * proximity constraints and context and return a corresponding union map
4490 * representation.
4492 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
4493 const char *domain, const char *validity, const char *proximity,
4494 const char *context)
4496 isl_set *con;
4497 isl_union_set *dom;
4498 isl_union_map *dep;
4499 isl_union_map *prox;
4500 isl_schedule_constraints *sc;
4501 isl_schedule *schedule;
4502 isl_union_map *sched;
4504 con = isl_set_read_from_str(ctx, context);
4505 dom = isl_union_set_read_from_str(ctx, domain);
4506 dep = isl_union_map_read_from_str(ctx, validity);
4507 prox = isl_union_map_read_from_str(ctx, proximity);
4508 sc = isl_schedule_constraints_on_domain(dom);
4509 sc = isl_schedule_constraints_set_context(sc, con);
4510 sc = isl_schedule_constraints_set_validity(sc, dep);
4511 sc = isl_schedule_constraints_set_proximity(sc, prox);
4512 schedule = isl_schedule_constraints_compute_schedule(sc);
4513 sched = isl_schedule_get_map(schedule);
4514 isl_schedule_free(schedule);
4516 return sched;
4519 /* Compute a schedule for the given instance set, validity constraints and
4520 * proximity constraints and return a corresponding union map representation.
4522 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
4523 const char *domain, const char *validity, const char *proximity)
4525 return compute_schedule_with_context(ctx, domain, validity, proximity,
4526 "{ : }");
4529 /* Check that a schedule can be constructed on the given domain
4530 * with the given validity and proximity constraints.
4532 static int test_has_schedule(isl_ctx *ctx, const char *domain,
4533 const char *validity, const char *proximity)
4535 isl_union_map *sched;
4537 sched = compute_schedule(ctx, domain, validity, proximity);
4538 if (!sched)
4539 return -1;
4541 isl_union_map_free(sched);
4542 return 0;
4545 int test_special_schedule(isl_ctx *ctx, const char *domain,
4546 const char *validity, const char *proximity, const char *expected_sched)
4548 isl_union_map *sched1, *sched2;
4549 int equal;
4551 sched1 = compute_schedule(ctx, domain, validity, proximity);
4552 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
4554 equal = isl_union_map_is_equal(sched1, sched2);
4555 isl_union_map_free(sched1);
4556 isl_union_map_free(sched2);
4558 if (equal < 0)
4559 return -1;
4560 if (!equal)
4561 isl_die(ctx, isl_error_unknown, "unexpected schedule",
4562 return -1);
4564 return 0;
4567 /* Check that the schedule map is properly padded, i.e., that the range
4568 * lives in a single space.
4570 static int test_padded_schedule(isl_ctx *ctx)
4572 const char *str;
4573 isl_union_set *D;
4574 isl_union_map *validity, *proximity;
4575 isl_schedule_constraints *sc;
4576 isl_schedule *sched;
4577 isl_union_map *umap;
4578 isl_union_set *range;
4579 isl_set *set;
4581 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
4582 D = isl_union_set_read_from_str(ctx, str);
4583 validity = isl_union_map_empty(isl_union_set_get_space(D));
4584 proximity = isl_union_map_copy(validity);
4585 sc = isl_schedule_constraints_on_domain(D);
4586 sc = isl_schedule_constraints_set_validity(sc, validity);
4587 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4588 sched = isl_schedule_constraints_compute_schedule(sc);
4589 umap = isl_schedule_get_map(sched);
4590 isl_schedule_free(sched);
4591 range = isl_union_map_range(umap);
4592 set = isl_set_from_union_set(range);
4593 isl_set_free(set);
4595 if (!set)
4596 return -1;
4598 return 0;
4601 /* Check that conditional validity constraints are also taken into
4602 * account across bands.
4603 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
4604 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
4605 * and then check that the adjacent order constraint C[2,1]->D[2,0]
4606 * is enforced by the rest of the schedule.
4608 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
4610 const char *str;
4611 isl_union_set *domain;
4612 isl_union_map *validity, *proximity, *condition;
4613 isl_union_map *sink, *source, *dep;
4614 isl_schedule_constraints *sc;
4615 isl_schedule *schedule;
4616 isl_union_access_info *access;
4617 isl_union_flow *flow;
4618 int empty;
4620 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4621 "A[k] : k >= 1 and k <= -1 + n; "
4622 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4623 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
4624 domain = isl_union_set_read_from_str(ctx, str);
4625 sc = isl_schedule_constraints_on_domain(domain);
4626 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
4627 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4628 "D[k, i] -> C[1 + k, i] : "
4629 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4630 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
4631 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
4632 validity = isl_union_map_read_from_str(ctx, str);
4633 sc = isl_schedule_constraints_set_validity(sc, validity);
4634 str = "[n] -> { C[k, i] -> D[k, i] : "
4635 "0 <= i <= -1 + k and k <= -1 + n }";
4636 proximity = isl_union_map_read_from_str(ctx, str);
4637 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4638 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
4639 "i <= -1 + k and i >= 1 and k <= -2 + n; "
4640 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
4641 "k <= -1 + n and i >= 0 and i <= -2 + k }";
4642 condition = isl_union_map_read_from_str(ctx, str);
4643 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
4644 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4645 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
4646 "i >= 0 and i <= -1 + k and k <= -1 + n and "
4647 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
4648 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
4649 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4650 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
4651 "k <= -1 + n and i >= 0 and i <= -1 + k and "
4652 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
4653 validity = isl_union_map_read_from_str(ctx, str);
4654 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4655 validity);
4656 schedule = isl_schedule_constraints_compute_schedule(sc);
4657 str = "{ D[2,0] -> [] }";
4658 sink = isl_union_map_read_from_str(ctx, str);
4659 access = isl_union_access_info_from_sink(sink);
4660 str = "{ C[2,1] -> [] }";
4661 source = isl_union_map_read_from_str(ctx, str);
4662 access = isl_union_access_info_set_must_source(access, source);
4663 access = isl_union_access_info_set_schedule(access, schedule);
4664 flow = isl_union_access_info_compute_flow(access);
4665 dep = isl_union_flow_get_must_dependence(flow);
4666 isl_union_flow_free(flow);
4667 empty = isl_union_map_is_empty(dep);
4668 isl_union_map_free(dep);
4670 if (empty < 0)
4671 return -1;
4672 if (empty)
4673 isl_die(ctx, isl_error_unknown,
4674 "conditional validity not respected", return -1);
4676 return 0;
4679 /* Check that the test for violated conditional validity constraints
4680 * is not confused by domain compression.
4681 * In particular, earlier versions of isl would apply
4682 * a schedule on the compressed domains to the original domains,
4683 * resulting in a failure to detect that the default schedule
4684 * violates the conditional validity constraints.
4686 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
4688 const char *str;
4689 isl_bool empty;
4690 isl_union_set *domain;
4691 isl_union_map *validity, *condition;
4692 isl_schedule_constraints *sc;
4693 isl_schedule *schedule;
4694 isl_union_map *umap;
4695 isl_map *map, *ge;
4697 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
4698 domain = isl_union_set_read_from_str(ctx, str);
4699 sc = isl_schedule_constraints_on_domain(domain);
4700 str = "{ B[1, i] -> A[0, i + 1] }";
4701 condition = isl_union_map_read_from_str(ctx, str);
4702 str = "{ A[0, i] -> B[1, i - 1] }";
4703 validity = isl_union_map_read_from_str(ctx, str);
4704 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4705 isl_union_map_copy(validity));
4706 schedule = isl_schedule_constraints_compute_schedule(sc);
4707 umap = isl_schedule_get_map(schedule);
4708 isl_schedule_free(schedule);
4709 validity = isl_union_map_apply_domain(validity,
4710 isl_union_map_copy(umap));
4711 validity = isl_union_map_apply_range(validity, umap);
4712 map = isl_map_from_union_map(validity);
4713 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4714 map = isl_map_intersect(map, ge);
4715 empty = isl_map_is_empty(map);
4716 isl_map_free(map);
4718 if (empty < 0)
4719 return -1;
4720 if (!empty)
4721 isl_die(ctx, isl_error_unknown,
4722 "conditional validity constraints not satisfied",
4723 return -1);
4725 return 0;
4728 /* Input for testing of schedule construction based on
4729 * conditional constraints.
4731 * domain is the iteration domain
4732 * flow are the flow dependences, which determine the validity and
4733 * proximity constraints
4734 * condition are the conditions on the conditional validity constraints
4735 * conditional_validity are the conditional validity constraints
4736 * outer_band_n is the expected number of members in the outer band
4738 struct {
4739 const char *domain;
4740 const char *flow;
4741 const char *condition;
4742 const char *conditional_validity;
4743 int outer_band_n;
4744 } live_range_tests[] = {
4745 /* Contrived example that illustrates that we need to keep
4746 * track of tagged condition dependences and
4747 * tagged conditional validity dependences
4748 * in isl_sched_edge separately.
4749 * In particular, the conditional validity constraints on A
4750 * cannot be satisfied,
4751 * but they can be ignored because there are no corresponding
4752 * condition constraints. However, we do have an additional
4753 * conditional validity constraint that maps to the same
4754 * dependence relation
4755 * as the condition constraint on B. If we did not make a distinction
4756 * between tagged condition and tagged conditional validity
4757 * dependences, then we
4758 * could end up treating this shared dependence as an condition
4759 * constraint on A, forcing a localization of the conditions,
4760 * which is impossible.
4762 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
4763 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
4764 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
4765 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4766 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4767 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
4770 /* TACO 2013 Fig. 7 */
4771 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4772 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4773 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4774 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
4775 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
4776 "0 <= i < n and 0 <= j < n - 1 }",
4777 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
4778 "0 <= i < n and 0 <= j < j' < n;"
4779 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
4780 "0 <= i < i' < n and 0 <= j,j' < n;"
4781 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
4782 "0 <= i,j,j' < n and j < j' }",
4785 /* TACO 2013 Fig. 7, without tags */
4786 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4787 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4788 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4789 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4790 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4791 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
4792 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
4793 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
4796 /* TACO 2013 Fig. 12 */
4797 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
4798 "S3[i,3] : 0 <= i <= 1 }",
4799 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
4800 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
4801 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
4802 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
4803 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4804 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
4805 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4806 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
4807 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
4808 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
4809 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
4814 /* Test schedule construction based on conditional constraints.
4815 * In particular, check the number of members in the outer band node
4816 * as an indication of whether tiling is possible or not.
4818 static int test_conditional_schedule_constraints(isl_ctx *ctx)
4820 int i;
4821 isl_union_set *domain;
4822 isl_union_map *condition;
4823 isl_union_map *flow;
4824 isl_union_map *validity;
4825 isl_schedule_constraints *sc;
4826 isl_schedule *schedule;
4827 isl_schedule_node *node;
4828 isl_size n_member;
4830 if (test_special_conditional_schedule_constraints(ctx) < 0)
4831 return -1;
4832 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
4833 return -1;
4835 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
4836 domain = isl_union_set_read_from_str(ctx,
4837 live_range_tests[i].domain);
4838 flow = isl_union_map_read_from_str(ctx,
4839 live_range_tests[i].flow);
4840 condition = isl_union_map_read_from_str(ctx,
4841 live_range_tests[i].condition);
4842 validity = isl_union_map_read_from_str(ctx,
4843 live_range_tests[i].conditional_validity);
4844 sc = isl_schedule_constraints_on_domain(domain);
4845 sc = isl_schedule_constraints_set_validity(sc,
4846 isl_union_map_copy(flow));
4847 sc = isl_schedule_constraints_set_proximity(sc, flow);
4848 sc = isl_schedule_constraints_set_conditional_validity(sc,
4849 condition, validity);
4850 schedule = isl_schedule_constraints_compute_schedule(sc);
4851 node = isl_schedule_get_root(schedule);
4852 while (node &&
4853 isl_schedule_node_get_type(node) != isl_schedule_node_band)
4854 node = isl_schedule_node_first_child(node);
4855 n_member = isl_schedule_node_band_n_member(node);
4856 isl_schedule_node_free(node);
4857 isl_schedule_free(schedule);
4859 if (!schedule || n_member < 0)
4860 return -1;
4861 if (n_member != live_range_tests[i].outer_band_n)
4862 isl_die(ctx, isl_error_unknown,
4863 "unexpected number of members in outer band",
4864 return -1);
4866 return 0;
4869 /* Check that the schedule computed for the given instance set and
4870 * dependence relation strongly satisfies the dependences.
4871 * In particular, check that no instance is scheduled before
4872 * or together with an instance on which it depends.
4873 * Earlier versions of isl would produce a schedule that
4874 * only weakly satisfies the dependences.
4876 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
4878 const char *domain, *dep;
4879 isl_union_map *D, *schedule;
4880 isl_map *map, *ge;
4881 int empty;
4883 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
4884 "A[i0] : 0 <= i0 <= 1 }";
4885 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
4886 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
4887 schedule = compute_schedule(ctx, domain, dep, dep);
4888 D = isl_union_map_read_from_str(ctx, dep);
4889 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
4890 D = isl_union_map_apply_range(D, schedule);
4891 map = isl_map_from_union_map(D);
4892 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4893 map = isl_map_intersect(map, ge);
4894 empty = isl_map_is_empty(map);
4895 isl_map_free(map);
4897 if (empty < 0)
4898 return -1;
4899 if (!empty)
4900 isl_die(ctx, isl_error_unknown,
4901 "dependences not strongly satisfied", return -1);
4903 return 0;
4906 /* Compute a schedule for input where the instance set constraints
4907 * conflict with the context constraints.
4908 * Earlier versions of isl did not properly handle this situation.
4910 static int test_conflicting_context_schedule(isl_ctx *ctx)
4912 isl_union_map *schedule;
4913 const char *domain, *context;
4915 domain = "[n] -> { A[] : n >= 0 }";
4916 context = "[n] -> { : n < 0 }";
4917 schedule = compute_schedule_with_context(ctx,
4918 domain, "{}", "{}", context);
4919 isl_union_map_free(schedule);
4921 if (!schedule)
4922 return -1;
4924 return 0;
4927 /* Check that a set of schedule constraints that only allow for
4928 * a coalescing schedule still produces a schedule even if the user
4929 * request a non-coalescing schedule. Earlier versions of isl
4930 * would not handle this case correctly.
4932 static int test_coalescing_schedule(isl_ctx *ctx)
4934 const char *domain, *dep;
4935 isl_union_set *I;
4936 isl_union_map *D;
4937 isl_schedule_constraints *sc;
4938 isl_schedule *schedule;
4939 int treat_coalescing;
4941 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4942 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4943 I = isl_union_set_read_from_str(ctx, domain);
4944 D = isl_union_map_read_from_str(ctx, dep);
4945 sc = isl_schedule_constraints_on_domain(I);
4946 sc = isl_schedule_constraints_set_validity(sc, D);
4947 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4948 isl_options_set_schedule_treat_coalescing(ctx, 1);
4949 schedule = isl_schedule_constraints_compute_schedule(sc);
4950 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4951 isl_schedule_free(schedule);
4952 if (!schedule)
4953 return -1;
4954 return 0;
4957 /* Check that the scheduler does not perform any needless
4958 * compound skewing. Earlier versions of isl would compute
4959 * schedules in terms of transformed schedule coefficients and
4960 * would not accurately keep track of the sum of the original
4961 * schedule coefficients. It could then produce the schedule
4962 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
4963 * for the input below instead of the schedule below.
4965 static int test_skewing_schedule(isl_ctx *ctx)
4967 const char *D, *V, *P, *S;
4969 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
4970 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
4971 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
4972 "-1 <= a-i + b-j + c-k <= 1 }";
4973 P = "{ }";
4974 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
4976 return test_special_schedule(ctx, D, V, P, S);
4979 int test_schedule(isl_ctx *ctx)
4981 const char *D, *W, *R, *V, *P, *S;
4982 int max_coincidence;
4983 int treat_coalescing;
4985 /* Handle resulting schedule with zero bands. */
4986 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4987 return -1;
4989 /* Jacobi */
4990 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4991 W = "{ S1[t,i] -> a[t,i] }";
4992 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4993 "S1[t,i] -> a[t-1,i+1] }";
4994 S = "{ S1[t,i] -> [t,i] }";
4995 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4996 return -1;
4998 /* Fig. 5 of CC2008 */
4999 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
5000 "j <= -1 + N }";
5001 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
5002 "j >= 2 and j <= -1 + N }";
5003 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
5004 "j >= 2 and j <= -1 + N; "
5005 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
5006 "j >= 2 and j <= -1 + N }";
5007 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
5008 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
5009 return -1;
5011 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
5012 W = "{ S1[i] -> a[i] }";
5013 R = "{ S2[i] -> a[i+1] }";
5014 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
5015 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
5016 return -1;
5018 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
5019 W = "{ S1[i] -> a[i] }";
5020 R = "{ S2[i] -> a[9-i] }";
5021 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
5022 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
5023 return -1;
5025 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
5026 W = "{ S1[i] -> a[i] }";
5027 R = "[N] -> { S2[i] -> a[N-1-i] }";
5028 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
5029 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
5030 return -1;
5032 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
5033 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
5034 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
5035 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
5036 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5037 return -1;
5039 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
5040 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
5041 R = "{ S2[i,j] -> a[i-1,j] }";
5042 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
5043 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
5044 return -1;
5046 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
5047 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
5048 R = "{ S2[i,j] -> a[i,j-1] }";
5049 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
5050 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
5051 return -1;
5053 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
5054 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
5055 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
5056 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
5057 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
5058 "S_0[] -> [0, 0, 0] }";
5059 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
5060 return -1;
5061 ctx->opt->schedule_parametric = 0;
5062 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5063 return -1;
5064 ctx->opt->schedule_parametric = 1;
5066 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
5067 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
5068 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
5069 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
5070 "S4[i] -> a[i,N] }";
5071 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
5072 "S4[i] -> [4,i,0] }";
5073 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
5074 isl_options_set_schedule_maximize_coincidence(ctx, 0);
5075 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
5076 return -1;
5077 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
5079 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
5080 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
5081 "j <= N }";
5082 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
5083 "j <= N; "
5084 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
5085 "j <= N }";
5086 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
5087 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5088 return -1;
5090 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
5091 " S_2[t] : t >= 0 and t <= -1 + N; "
5092 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
5093 "i <= -1 + N }";
5094 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
5095 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
5096 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
5097 "i >= 0 and i <= -1 + N }";
5098 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
5099 "i >= 0 and i <= -1 + N; "
5100 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
5101 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
5102 " S_0[t] -> [0, t, 0] }";
5104 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
5105 return -1;
5106 ctx->opt->schedule_parametric = 0;
5107 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5108 return -1;
5109 ctx->opt->schedule_parametric = 1;
5111 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
5112 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
5113 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
5114 return -1;
5116 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
5117 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
5118 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
5119 "j >= 0 and j <= -1 + N; "
5120 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
5121 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
5122 "j >= 0 and j <= -1 + N; "
5123 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
5124 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
5125 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5126 return -1;
5128 D = "{ S_0[i] : i >= 0 }";
5129 W = "{ S_0[i] -> a[i] : i >= 0 }";
5130 R = "{ S_0[i] -> a[0] : i >= 0 }";
5131 S = "{ S_0[i] -> [0, i, 0] }";
5132 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5133 return -1;
5135 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
5136 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
5137 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
5138 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
5139 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5140 return -1;
5142 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
5143 "k <= -1 + n and k >= 0 }";
5144 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
5145 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
5146 "k <= -1 + n and k >= 0; "
5147 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
5148 "k <= -1 + n and k >= 0; "
5149 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
5150 "k <= -1 + n and k >= 0 }";
5151 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
5152 ctx->opt->schedule_outer_coincidence = 1;
5153 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5154 return -1;
5155 ctx->opt->schedule_outer_coincidence = 0;
5157 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
5158 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
5159 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
5160 "Stmt_for_body24[i0, i1, 1, 0]:"
5161 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
5162 "Stmt_for_body7[i0, i1, i2]:"
5163 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
5164 "i2 <= 7 }";
5166 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
5167 "Stmt_for_body24[1, i1, i2, i3]:"
5168 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
5169 "i2 >= 1;"
5170 "Stmt_for_body24[0, i1, i2, i3] -> "
5171 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
5172 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
5173 "i3 >= 0;"
5174 "Stmt_for_body24[0, i1, i2, i3] ->"
5175 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
5176 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
5177 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
5178 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
5179 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
5180 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
5181 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
5182 "i1 <= 6 and i1 >= 0;"
5183 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
5184 "Stmt_for_body7[i0, i1, i2] -> "
5185 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
5186 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
5187 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
5188 "Stmt_for_body7[i0, i1, i2] -> "
5189 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
5190 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
5191 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
5192 P = V;
5194 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
5195 isl_options_set_schedule_treat_coalescing(ctx, 0);
5196 if (test_has_schedule(ctx, D, V, P) < 0)
5197 return -1;
5198 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
5200 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
5201 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
5202 "j >= 1 and j <= 7;"
5203 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
5204 "j >= 1 and j <= 8 }";
5205 P = "{ }";
5206 S = "{ S_0[i, j] -> [i + j, i] }";
5207 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5208 if (test_special_schedule(ctx, D, V, P, S) < 0)
5209 return -1;
5210 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5212 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
5213 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
5214 "j >= 0 and j <= -1 + i }";
5215 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
5216 "i <= -1 + N and j >= 0;"
5217 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
5218 "i <= -2 + N }";
5219 P = "{ }";
5220 S = "{ S_0[i, j] -> [i, j] }";
5221 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5222 if (test_special_schedule(ctx, D, V, P, S) < 0)
5223 return -1;
5224 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5226 /* Test both algorithms on a case with only proximity dependences. */
5227 D = "{ S[i,j] : 0 <= i <= 10 }";
5228 V = "{ }";
5229 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
5230 S = "{ S[i, j] -> [j, i] }";
5231 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5232 if (test_special_schedule(ctx, D, V, P, S) < 0)
5233 return -1;
5234 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5235 if (test_special_schedule(ctx, D, V, P, S) < 0)
5236 return -1;
5238 D = "{ A[a]; B[] }";
5239 V = "{}";
5240 P = "{ A[a] -> B[] }";
5241 if (test_has_schedule(ctx, D, V, P) < 0)
5242 return -1;
5244 if (test_padded_schedule(ctx) < 0)
5245 return -1;
5247 /* Check that check for progress is not confused by rational
5248 * solution.
5250 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
5251 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
5252 "i0 <= -2 + N; "
5253 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
5254 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
5255 P = "{}";
5256 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5257 if (test_has_schedule(ctx, D, V, P) < 0)
5258 return -1;
5259 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5261 /* Check that we allow schedule rows that are only non-trivial
5262 * on some full-dimensional domains.
5264 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
5265 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
5266 "S1[j] -> S2[1] : 0 <= j <= 1 }";
5267 P = "{}";
5268 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5269 if (test_has_schedule(ctx, D, V, P) < 0)
5270 return -1;
5271 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5273 if (test_conditional_schedule_constraints(ctx) < 0)
5274 return -1;
5276 if (test_strongly_satisfying_schedule(ctx) < 0)
5277 return -1;
5279 if (test_conflicting_context_schedule(ctx) < 0)
5280 return -1;
5282 if (test_coalescing_schedule(ctx) < 0)
5283 return -1;
5284 if (test_skewing_schedule(ctx) < 0)
5285 return -1;
5287 return 0;
5290 /* Perform scheduling tests using the whole component scheduler.
5292 static int test_schedule_whole(isl_ctx *ctx)
5294 int whole;
5295 int r;
5297 whole = isl_options_get_schedule_whole_component(ctx);
5298 isl_options_set_schedule_whole_component(ctx, 1);
5299 r = test_schedule(ctx);
5300 isl_options_set_schedule_whole_component(ctx, whole);
5302 return r;
5305 /* Perform scheduling tests using the incremental scheduler.
5307 static int test_schedule_incremental(isl_ctx *ctx)
5309 int whole;
5310 int r;
5312 whole = isl_options_get_schedule_whole_component(ctx);
5313 isl_options_set_schedule_whole_component(ctx, 0);
5314 r = test_schedule(ctx);
5315 isl_options_set_schedule_whole_component(ctx, whole);
5317 return r;
5320 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
5322 isl_union_map *umap;
5323 int test;
5325 umap = isl_union_map_read_from_str(ctx, str);
5326 test = isl_union_map_plain_is_injective(umap);
5327 isl_union_map_free(umap);
5328 if (test < 0)
5329 return -1;
5330 if (test == injective)
5331 return 0;
5332 if (injective)
5333 isl_die(ctx, isl_error_unknown,
5334 "map not detected as injective", return -1);
5335 else
5336 isl_die(ctx, isl_error_unknown,
5337 "map detected as injective", return -1);
5340 int test_injective(isl_ctx *ctx)
5342 const char *str;
5344 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
5345 return -1;
5346 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
5347 return -1;
5348 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
5349 return -1;
5350 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
5351 return -1;
5352 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
5353 return -1;
5354 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
5355 return -1;
5356 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
5357 return -1;
5358 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
5359 return -1;
5361 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
5362 if (test_plain_injective(ctx, str, 1))
5363 return -1;
5364 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
5365 if (test_plain_injective(ctx, str, 0))
5366 return -1;
5368 return 0;
5371 #undef BASE
5372 #define BASE aff
5373 #include "isl_test_plain_equal_templ.c"
5375 #undef BASE
5376 #define BASE pw_multi_aff
5377 #include "isl_test_plain_equal_templ.c"
5379 #undef BASE
5380 #define BASE union_pw_aff
5381 #include "isl_test_plain_equal_templ.c"
5383 /* Basic tests on isl_union_pw_aff.
5385 * In particular, check that isl_union_pw_aff_aff_on_domain
5386 * aligns the parameters of the input objects and
5387 * that isl_union_pw_aff_param_on_domain_id properly
5388 * introduces the parameter.
5390 static int test_upa(isl_ctx *ctx)
5392 const char *str;
5393 isl_id *id;
5394 isl_aff *aff;
5395 isl_union_set *domain;
5396 isl_union_pw_aff *upa;
5397 isl_stat ok;
5399 aff = isl_aff_read_from_str(ctx, "[N] -> { [N] }");
5400 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5401 domain = isl_union_set_read_from_str(ctx, str);
5402 upa = isl_union_pw_aff_aff_on_domain(domain, aff);
5403 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5404 ok = union_pw_aff_check_plain_equal(upa, str);
5405 isl_union_pw_aff_free(upa);
5406 if (ok < 0)
5407 return -1;
5409 id = isl_id_alloc(ctx, "N", NULL);
5410 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5411 domain = isl_union_set_read_from_str(ctx, str);
5412 upa = isl_union_pw_aff_param_on_domain_id(domain, id);
5413 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5414 ok = union_pw_aff_check_plain_equal(upa, str);
5415 isl_union_pw_aff_free(upa);
5416 if (ok < 0)
5417 return -1;
5419 return 0;
5422 struct {
5423 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5424 __isl_take isl_aff *aff2);
5425 } aff_bin_op[] = {
5426 ['+'] = { &isl_aff_add },
5427 ['-'] = { &isl_aff_sub },
5428 ['*'] = { &isl_aff_mul },
5429 ['/'] = { &isl_aff_div },
5432 struct {
5433 const char *arg1;
5434 unsigned char op;
5435 const char *arg2;
5436 const char *res;
5437 } aff_bin_tests[] = {
5438 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
5439 "{ [i] -> [2i] }" },
5440 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
5441 "{ [i] -> [0] }" },
5442 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
5443 "{ [i] -> [2i] }" },
5444 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
5445 "{ [i] -> [2i] }" },
5446 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
5447 "{ [i] -> [i/2] }" },
5448 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
5449 "{ [i] -> [i] }" },
5450 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
5451 "{ [i] -> [NaN] }" },
5452 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
5453 "{ [i] -> [NaN] }" },
5454 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
5455 "{ [i] -> [NaN] }" },
5456 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
5457 "{ [i] -> [NaN] }" },
5458 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
5459 "{ [i] -> [NaN] }" },
5460 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
5461 "{ [i] -> [NaN] }" },
5462 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
5463 "{ [i] -> [NaN] }" },
5464 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
5465 "{ [i] -> [NaN] }" },
5466 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
5467 "{ [i] -> [NaN] }" },
5468 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
5469 "{ [i] -> [NaN] }" },
5470 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
5471 "{ [i] -> [NaN] }" },
5472 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
5473 "{ [i] -> [NaN] }" },
5474 { "{ [i] -> [i] }", '/', "{ [i] -> [0] }",
5475 "{ [i] -> [NaN] }" },
5478 /* Perform some basic tests of binary operations on isl_aff objects.
5480 static int test_bin_aff(isl_ctx *ctx)
5482 int i;
5483 isl_aff *aff1, *aff2, *res;
5484 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5485 __isl_take isl_aff *aff2);
5486 int ok;
5488 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
5489 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
5490 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
5491 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
5492 fn = aff_bin_op[aff_bin_tests[i].op].fn;
5493 aff1 = fn(aff1, aff2);
5494 if (isl_aff_is_nan(res))
5495 ok = isl_aff_is_nan(aff1);
5496 else
5497 ok = isl_aff_plain_is_equal(aff1, res);
5498 isl_aff_free(aff1);
5499 isl_aff_free(res);
5500 if (ok < 0)
5501 return -1;
5502 if (!ok)
5503 isl_die(ctx, isl_error_unknown,
5504 "unexpected result", return -1);
5507 return 0;
5510 struct {
5511 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
5512 __isl_take isl_pw_aff *pa2);
5513 } pw_aff_bin_op[] = {
5514 ['m'] = { &isl_pw_aff_min },
5515 ['M'] = { &isl_pw_aff_max },
5518 /* Inputs for binary isl_pw_aff operation tests.
5519 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
5520 * defined by pw_aff_bin_op, and "res" is the expected result.
5522 struct {
5523 const char *arg1;
5524 unsigned char op;
5525 const char *arg2;
5526 const char *res;
5527 } pw_aff_bin_tests[] = {
5528 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
5529 "{ [i] -> [i] }" },
5530 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
5531 "{ [i] -> [i] }" },
5532 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
5533 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
5534 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
5535 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
5536 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
5537 "{ [i] -> [NaN] }" },
5538 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
5539 "{ [i] -> [NaN] }" },
5542 /* Perform some basic tests of binary operations on isl_pw_aff objects.
5544 static int test_bin_pw_aff(isl_ctx *ctx)
5546 int i;
5547 isl_bool ok;
5548 isl_pw_aff *pa1, *pa2, *res;
5550 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
5551 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
5552 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
5553 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
5554 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
5555 if (isl_pw_aff_involves_nan(res))
5556 ok = isl_pw_aff_involves_nan(pa1);
5557 else
5558 ok = isl_pw_aff_plain_is_equal(pa1, res);
5559 isl_pw_aff_free(pa1);
5560 isl_pw_aff_free(res);
5561 if (ok < 0)
5562 return -1;
5563 if (!ok)
5564 isl_die(ctx, isl_error_unknown,
5565 "unexpected result", return -1);
5568 return 0;
5571 /* Inputs for basic tests of test operations on
5572 * isl_union_pw_multi_aff objects.
5573 * "fn" is the function that is being tested.
5574 * "arg" is a string description of the input.
5575 * "res" is the expected result.
5577 static struct {
5578 isl_bool (*fn)(__isl_keep isl_union_pw_multi_aff *upma1);
5579 const char *arg;
5580 isl_bool res;
5581 } upma_test_tests[] = {
5582 { &isl_union_pw_multi_aff_involves_nan, "{ A[] -> [0]; B[0] -> [1] }",
5583 isl_bool_false },
5584 { &isl_union_pw_multi_aff_involves_nan, "{ A[] -> [NaN]; B[0] -> [1] }",
5585 isl_bool_true },
5586 { &isl_union_pw_multi_aff_involves_nan, "{ A[] -> [0]; B[0] -> [NaN] }",
5587 isl_bool_true },
5588 { &isl_union_pw_multi_aff_involves_nan,
5589 "{ A[] -> [0]; B[0] -> [1, NaN, 5] }",
5590 isl_bool_true },
5591 { &isl_union_pw_multi_aff_involves_locals,
5592 "{ A[] -> [0]; B[0] -> [1] }",
5593 isl_bool_false },
5594 { &isl_union_pw_multi_aff_involves_locals,
5595 "{ A[] -> [0]; B[x] -> [1] : x mod 2 = 0 }",
5596 isl_bool_true },
5597 { &isl_union_pw_multi_aff_involves_locals,
5598 "{ A[] -> [0]; B[x] -> [x // 2] }",
5599 isl_bool_true },
5600 { &isl_union_pw_multi_aff_involves_locals,
5601 "{ A[i] -> [i // 2]; B[0] -> [1] }",
5602 isl_bool_true },
5605 /* Perform some basic tests of test operations on
5606 * isl_union_pw_multi_aff objects.
5608 static isl_stat test_upma_test(isl_ctx *ctx)
5610 int i;
5611 isl_union_pw_multi_aff *upma;
5612 isl_bool res;
5614 for (i = 0; i < ARRAY_SIZE(upma_test_tests); ++i) {
5615 const char *str;
5617 str = upma_test_tests[i].arg;
5618 upma = isl_union_pw_multi_aff_read_from_str(ctx, str);
5619 res = upma_test_tests[i].fn(upma);
5620 isl_union_pw_multi_aff_free(upma);
5621 if (res < 0)
5622 return isl_stat_error;
5623 if (res != upma_test_tests[i].res)
5624 isl_die(ctx, isl_error_unknown,
5625 "unexpected result", return isl_stat_error);
5628 return isl_stat_ok;
5631 struct {
5632 __isl_give isl_union_pw_multi_aff *(*fn)(
5633 __isl_take isl_union_pw_multi_aff *upma1,
5634 __isl_take isl_union_pw_multi_aff *upma2);
5635 const char *arg1;
5636 const char *arg2;
5637 const char *res;
5638 } upma_bin_tests[] = {
5639 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
5640 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
5641 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
5642 "{ B[x] -> [2] : x >= 0 }",
5643 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
5644 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
5645 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
5646 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
5647 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
5648 "D[i] -> B[2] : i >= 5 }" },
5649 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5650 "{ B[x] -> C[2] : x > 0 }",
5651 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
5652 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5653 "{ B[x] -> A[2] : x >= 0 }",
5654 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
5656 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5657 "{ B[x] -> C[x + 2] }",
5658 "{ D[y] -> B[2y] }",
5659 "{ }" },
5661 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5662 "{ [A[x] -> B[x + 1]] -> C[x + 2] }",
5663 "{ D[y] -> B[2y] }",
5664 "{ }" },
5666 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5667 "{ [A[x] -> B[x + 1]] -> C[x + 2]; B[x] -> C[x + 2] }",
5668 "{ D[y] -> A[2y] }",
5669 "{ [D[y] -> B[2y + 1]] -> C[2y + 2] }" },
5671 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5672 "{ T[A[x] -> B[x + 1]] -> C[x + 2]; B[x] -> C[x + 2] }",
5673 "{ D[y] -> A[2y] }",
5674 "{ T[D[y] -> B[2y + 1]] -> C[2y + 2] }" },
5677 /* Perform some basic tests of binary operations on
5678 * isl_union_pw_multi_aff objects.
5680 static int test_bin_upma(isl_ctx *ctx)
5682 int i;
5683 isl_union_pw_multi_aff *upma1, *upma2, *res;
5684 int ok;
5686 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
5687 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5688 upma_bin_tests[i].arg1);
5689 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5690 upma_bin_tests[i].arg2);
5691 res = isl_union_pw_multi_aff_read_from_str(ctx,
5692 upma_bin_tests[i].res);
5693 upma1 = upma_bin_tests[i].fn(upma1, upma2);
5694 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
5695 isl_union_pw_multi_aff_free(upma1);
5696 isl_union_pw_multi_aff_free(res);
5697 if (ok < 0)
5698 return -1;
5699 if (!ok)
5700 isl_die(ctx, isl_error_unknown,
5701 "unexpected result", return -1);
5704 return 0;
5707 struct {
5708 __isl_give isl_union_pw_multi_aff *(*fn)(
5709 __isl_take isl_union_pw_multi_aff *upma1,
5710 __isl_take isl_union_pw_multi_aff *upma2);
5711 const char *arg1;
5712 const char *arg2;
5713 } upma_bin_fail_tests[] = {
5714 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5715 "{ B[x] -> C[2] : x >= 0 }" },
5718 /* Perform some basic tests of binary operations on
5719 * isl_union_pw_multi_aff objects that are expected to fail.
5721 static int test_bin_upma_fail(isl_ctx *ctx)
5723 int i, n;
5724 isl_union_pw_multi_aff *upma1, *upma2;
5725 int on_error;
5727 on_error = isl_options_get_on_error(ctx);
5728 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
5729 n = ARRAY_SIZE(upma_bin_fail_tests);
5730 for (i = 0; i < n; ++i) {
5731 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5732 upma_bin_fail_tests[i].arg1);
5733 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5734 upma_bin_fail_tests[i].arg2);
5735 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
5736 isl_union_pw_multi_aff_free(upma1);
5737 if (upma1)
5738 break;
5740 isl_options_set_on_error(ctx, on_error);
5741 if (i < n)
5742 isl_die(ctx, isl_error_unknown,
5743 "operation not expected to succeed", return -1);
5745 return 0;
5748 /* Inputs for basic tests of binary operations on
5749 * pairs of isl_union_pw_multi_aff and isl_union_set objects.
5750 * "fn" is the function that is being tested.
5751 * "arg1" and "arg2" are string descriptions of the inputs.
5752 * "res" is a string description of the expected result.
5754 struct {
5755 __isl_give isl_union_pw_multi_aff *(*fn)(
5756 __isl_take isl_union_pw_multi_aff *upma,
5757 __isl_take isl_union_set *uset);
5758 const char *arg1;
5759 const char *arg2;
5760 const char *res;
5761 } upma_uset_tests[] = {
5762 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5763 "{ A[i] -> B[i] }", "{ B[0] }",
5764 "{ }" },
5765 { &isl_union_pw_multi_aff_intersect_domain_wrapped_domain,
5766 "{ [A[i] -> B[i]] -> C[i + 1] }", "{ A[1]; B[0] }",
5767 "{ [A[1] -> B[1]] -> C[2] }" },
5768 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5769 "{ [A[i] -> B[i]] -> C[i + 1] }", "{ A[1]; B[0] }",
5770 "{ [A[0] -> B[0]] -> C[1] }" },
5771 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5772 "{ [A[i] -> B[i]] -> C[i + 1] }", "[N] -> { B[N] }",
5773 "[N] -> { [A[N] -> B[N]] -> C[N + 1] }" },
5774 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5775 "[M] -> { [A[M] -> B[M]] -> C[M + 1] }", "[N] -> { B[N] }",
5776 "[N, M] -> { [A[N] -> B[N]] -> C[N + 1] : N = M }" },
5777 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5778 "{ [A[] -> B[]] -> C[]; N[A[] -> B[]] -> D[]; [B[] -> A[]] -> E[] }",
5779 "{ B[] }",
5780 "{ [A[] -> B[]] -> C[]; N[A[] -> B[]] -> D[] }" },
5783 /* Perform some basic tests of binary operations on
5784 * pairs of isl_union_pw_multi_aff and isl_union_set objects.
5786 static isl_stat test_upma_uset(isl_ctx *ctx)
5788 int i;
5789 isl_bool ok;
5790 isl_union_pw_multi_aff *upma, *res;
5791 isl_union_set *uset;
5793 for (i = 0; i < ARRAY_SIZE(upma_uset_tests); ++i) {
5794 upma = isl_union_pw_multi_aff_read_from_str(ctx,
5795 upma_uset_tests[i].arg1);
5796 uset = isl_union_set_read_from_str(ctx,
5797 upma_uset_tests[i].arg2);
5798 res = isl_union_pw_multi_aff_read_from_str(ctx,
5799 upma_uset_tests[i].res);
5800 upma = upma_uset_tests[i].fn(upma, uset);
5801 ok = isl_union_pw_multi_aff_plain_is_equal(upma, res);
5802 isl_union_pw_multi_aff_free(upma);
5803 isl_union_pw_multi_aff_free(res);
5804 if (ok < 0)
5805 return isl_stat_error;
5806 if (!ok)
5807 isl_die(ctx, isl_error_unknown,
5808 "unexpected result", return isl_stat_error);
5811 return isl_stat_ok;
5814 /* Inputs for basic tests of unary operations on isl_multi_pw_aff objects.
5815 * "fn" is the function that is tested.
5816 * "arg" is a string description of the input.
5817 * "res" is a string description of the expected result.
5819 struct {
5820 __isl_give isl_multi_pw_aff *(*fn)(__isl_take isl_multi_pw_aff *mpa);
5821 const char *arg;
5822 const char *res;
5823 } mpa_un_tests[] = {
5824 { &isl_multi_pw_aff_range_factor_domain,
5825 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5826 "{ A[x] -> B[(1 : x >= 5)] }" },
5827 { &isl_multi_pw_aff_range_factor_range,
5828 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5829 "{ A[y] -> C[(2 : y <= 10)] }" },
5830 { &isl_multi_pw_aff_range_factor_domain,
5831 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5832 "{ A[x] -> B[(1 : x >= 5)] }" },
5833 { &isl_multi_pw_aff_range_factor_range,
5834 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5835 "{ A[y] -> C[] }" },
5836 { &isl_multi_pw_aff_range_factor_domain,
5837 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5838 "{ A[x] -> B[] }" },
5839 { &isl_multi_pw_aff_range_factor_range,
5840 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5841 "{ A[y] -> C[(2 : y <= 10)] }" },
5842 { &isl_multi_pw_aff_range_factor_domain,
5843 "{ A[x] -> [B[] -> C[]] }",
5844 "{ A[x] -> B[] }" },
5845 { &isl_multi_pw_aff_range_factor_range,
5846 "{ A[x] -> [B[] -> C[]] }",
5847 "{ A[y] -> C[] }" },
5848 { &isl_multi_pw_aff_factor_range,
5849 "{ [B[] -> C[]] }",
5850 "{ C[] }" },
5851 { &isl_multi_pw_aff_range_factor_domain,
5852 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5853 "{ A[x] -> B[] : x >= 0 }" },
5854 { &isl_multi_pw_aff_range_factor_range,
5855 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5856 "{ A[y] -> C[] : y >= 0 }" },
5857 { &isl_multi_pw_aff_factor_range,
5858 "[N] -> { [B[] -> C[]] : N >= 0 }",
5859 "[N] -> { C[] : N >= 0 }" },
5862 /* Perform some basic tests of unary operations on isl_multi_pw_aff objects.
5864 static int test_un_mpa(isl_ctx *ctx)
5866 int i;
5867 isl_bool ok;
5868 isl_multi_pw_aff *mpa, *res;
5870 for (i = 0; i < ARRAY_SIZE(mpa_un_tests); ++i) {
5871 mpa = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].arg);
5872 res = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].res);
5873 mpa = mpa_un_tests[i].fn(mpa);
5874 ok = isl_multi_pw_aff_plain_is_equal(mpa, res);
5875 isl_multi_pw_aff_free(mpa);
5876 isl_multi_pw_aff_free(res);
5877 if (ok < 0)
5878 return -1;
5879 if (!ok)
5880 isl_die(ctx, isl_error_unknown,
5881 "unexpected result", return -1);
5884 return 0;
5887 /* Inputs for basic tests of binary operations on isl_multi_pw_aff objects.
5888 * "fn" is the function that is tested.
5889 * "arg1" and "arg2" are string descriptions of the inputs.
5890 * "res" is a string description of the expected result.
5892 struct {
5893 __isl_give isl_multi_pw_aff *(*fn)(
5894 __isl_take isl_multi_pw_aff *mpa1,
5895 __isl_take isl_multi_pw_aff *mpa2);
5896 const char *arg1;
5897 const char *arg2;
5898 const char *res;
5899 } mpa_bin_tests[] = {
5900 { &isl_multi_pw_aff_add, "{ A[] -> [1] }", "{ A[] -> [2] }",
5901 "{ A[] -> [3] }" },
5902 { &isl_multi_pw_aff_add, "{ A[x] -> [(1 : x >= 5)] }",
5903 "{ A[x] -> [(x : x <= 10)] }",
5904 "{ A[x] -> [(1 + x : 5 <= x <= 10)] }" },
5905 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5906 "{ A[x] -> [] : x <= 10 }",
5907 "{ A[x] -> [] : 5 <= x <= 10 }" },
5908 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5909 "[N] -> { A[x] -> [] : x <= N }",
5910 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5911 { &isl_multi_pw_aff_add,
5912 "[N] -> { A[x] -> [] : x <= N }",
5913 "{ A[x] -> [] : x >= 5 }",
5914 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5915 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5916 "{ A[y] -> C[(2 : y <= 10)] }",
5917 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5918 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5919 "{ A[y] -> C[2] : y <= 10 }",
5920 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5921 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5922 "[N] -> { A[y] -> C[2] : y <= N }",
5923 "[N] -> { A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= N)]] }" },
5924 { &isl_multi_pw_aff_range_product, "[N] -> { A[x] -> B[1] : x >= N }",
5925 "{ A[y] -> C[2] : y <= 10 }",
5926 "[N] -> { A[x] -> [B[(1 : x >= N)] -> C[(2 : x <= 10)]] }" },
5927 { &isl_multi_pw_aff_range_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5928 "{ A[] -> [B[1] -> C[2]] }" },
5929 { &isl_multi_pw_aff_range_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5930 "{ A[] -> [B[] -> C[]] }" },
5931 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5932 "{ A[y] -> C[] : y <= 10 }",
5933 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] : x <= 10 }" },
5934 { &isl_multi_pw_aff_range_product, "{ A[y] -> C[] : y <= 10 }",
5935 "{ A[x] -> B[(1 : x >= 5)] }",
5936 "{ A[x] -> [C[] -> B[(1 : x >= 5)]] : x <= 10 }" },
5937 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5938 "{ A[y] -> C[(2 : y <= 10)] }",
5939 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[(2 : y <= 10)]] }" },
5940 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5941 "{ A[y] -> C[] : y <= 10 }",
5942 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= 10 }" },
5943 { &isl_multi_pw_aff_product, "{ A[y] -> C[] : y <= 10 }",
5944 "{ A[x] -> B[(1 : x >= 5)] }",
5945 "{ [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= 10 }" },
5946 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5947 "[N] -> { A[y] -> C[] : y <= N }",
5948 "[N] -> { [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= N }" },
5949 { &isl_multi_pw_aff_product, "[N] -> { A[y] -> C[] : y <= N }",
5950 "{ A[x] -> B[(1 : x >= 5)] }",
5951 "[N] -> { [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= N }" },
5952 { &isl_multi_pw_aff_product, "{ A[x] -> B[] : x >= 5 }",
5953 "{ A[y] -> C[] : y <= 10 }",
5954 "{ [A[x] -> A[y]] -> [B[] -> C[]] : x >= 5 and y <= 10 }" },
5955 { &isl_multi_pw_aff_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5956 "{ [A[] -> A[]] -> [B[1] -> C[2]] }" },
5957 { &isl_multi_pw_aff_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5958 "{ [A[] -> A[]] -> [B[] -> C[]] }" },
5959 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5960 "{ B[i,j] -> C[i + 2j] }", "{ A[a,b] -> B[b,a] }",
5961 "{ A[a,b] -> C[b + 2a] }" },
5962 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5963 "{ B[i,j] -> C[i + 2j] }",
5964 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5965 "{ A[a,b] -> C[(b + 2a : b > a)] }" },
5966 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5967 "{ B[i,j] -> C[(i + 2j : j > 4)] }",
5968 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5969 "{ A[a,b] -> C[(b + 2a : b > a > 4)] }" },
5970 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5971 "{ B[i,j] -> C[] }",
5972 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5973 "{ A[a,b] -> C[] }" },
5974 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5975 "{ B[i,j] -> C[] : i > j }",
5976 "{ A[a,b] -> B[b,a] }",
5977 "{ A[a,b] -> C[] : b > a }" },
5978 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5979 "{ B[i,j] -> C[] : j > 5 }",
5980 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5981 "{ A[a,b] -> C[] : b > a > 5 }" },
5982 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5983 "[N] -> { B[i,j] -> C[] : j > N }",
5984 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5985 "[N] -> { A[a,b] -> C[] : b > a > N }" },
5986 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5987 "[M,N] -> { B[] -> C[] : N > 5 }",
5988 "[M,N] -> { A[] -> B[] : M > N }",
5989 "[M,N] -> { A[] -> C[] : M > N > 5 }" },
5992 /* Perform some basic tests of binary operations on isl_multi_pw_aff objects.
5994 static int test_bin_mpa(isl_ctx *ctx)
5996 int i;
5997 isl_bool ok;
5998 isl_multi_pw_aff *mpa1, *mpa2, *res;
6000 for (i = 0; i < ARRAY_SIZE(mpa_bin_tests); ++i) {
6001 mpa1 = isl_multi_pw_aff_read_from_str(ctx,
6002 mpa_bin_tests[i].arg1);
6003 mpa2 = isl_multi_pw_aff_read_from_str(ctx,
6004 mpa_bin_tests[i].arg2);
6005 res = isl_multi_pw_aff_read_from_str(ctx,
6006 mpa_bin_tests[i].res);
6007 mpa1 = mpa_bin_tests[i].fn(mpa1, mpa2);
6008 ok = isl_multi_pw_aff_plain_is_equal(mpa1, res);
6009 isl_multi_pw_aff_free(mpa1);
6010 isl_multi_pw_aff_free(res);
6011 if (ok < 0)
6012 return -1;
6013 if (!ok)
6014 isl_die(ctx, isl_error_unknown,
6015 "unexpected result", return -1);
6018 return 0;
6021 /* Inputs for basic tests of unary operations on
6022 * isl_multi_union_pw_aff objects.
6023 * "fn" is the function that is tested.
6024 * "arg" is a string description of the input.
6025 * "res" is a string description of the expected result.
6027 struct {
6028 __isl_give isl_multi_union_pw_aff *(*fn)(
6029 __isl_take isl_multi_union_pw_aff *mupa);
6030 const char *arg;
6031 const char *res;
6032 } mupa_un_tests[] = {
6033 { &isl_multi_union_pw_aff_factor_range,
6034 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]",
6035 "C[{ A[] -> [2] }]" },
6036 { &isl_multi_union_pw_aff_factor_range,
6037 "[B[] -> C[{ A[] -> [2] }]]",
6038 "C[{ A[] -> [2] }]" },
6039 { &isl_multi_union_pw_aff_factor_range,
6040 "[B[{ A[] -> [1] }] -> C[]]",
6041 "C[]" },
6042 { &isl_multi_union_pw_aff_factor_range,
6043 "[B[] -> C[]]",
6044 "C[]" },
6045 { &isl_multi_union_pw_aff_factor_range,
6046 "([B[] -> C[]] : { A[x] : x >= 0 })",
6047 "(C[] : { A[x] : x >= 0 })" },
6048 { &isl_multi_union_pw_aff_factor_range,
6049 "[N] -> ([B[] -> C[]] : { A[x] : x <= N })",
6050 "[N] -> (C[] : { A[x] : x <= N })" },
6051 { &isl_multi_union_pw_aff_factor_range,
6052 "[N] -> ([B[] -> C[]] : { : N >= 0 })",
6053 "[N] -> (C[] : { : N >= 0 })" },
6056 /* Perform some basic tests of unary operations on
6057 * isl_multi_union_pw_aff objects.
6059 static int test_un_mupa(isl_ctx *ctx)
6061 int i;
6062 isl_bool ok;
6063 isl_multi_union_pw_aff *mupa, *res;
6065 for (i = 0; i < ARRAY_SIZE(mupa_un_tests); ++i) {
6066 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6067 mupa_un_tests[i].arg);
6068 res = isl_multi_union_pw_aff_read_from_str(ctx,
6069 mupa_un_tests[i].res);
6070 mupa = mupa_un_tests[i].fn(mupa);
6071 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6072 isl_multi_union_pw_aff_free(mupa);
6073 isl_multi_union_pw_aff_free(res);
6074 if (ok < 0)
6075 return -1;
6076 if (!ok)
6077 isl_die(ctx, isl_error_unknown,
6078 "unexpected result", return -1);
6081 return 0;
6084 /* Inputs for basic tests of binary operations on
6085 * isl_multi_union_pw_aff objects.
6086 * "fn" is the function that is tested.
6087 * "arg1" and "arg2" are string descriptions of the inputs.
6088 * "res" is a string description of the expected result.
6090 struct {
6091 __isl_give isl_multi_union_pw_aff *(*fn)(
6092 __isl_take isl_multi_union_pw_aff *mupa1,
6093 __isl_take isl_multi_union_pw_aff *mupa2);
6094 const char *arg1;
6095 const char *arg2;
6096 const char *res;
6097 } mupa_bin_tests[] = {
6098 { &isl_multi_union_pw_aff_add, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
6099 "[{ A[] -> [3] }]" },
6100 { &isl_multi_union_pw_aff_sub, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
6101 "[{ A[] -> [-1] }]" },
6102 { &isl_multi_union_pw_aff_add,
6103 "[{ A[] -> [1]; B[] -> [4] }]",
6104 "[{ A[] -> [2]; C[] -> [5] }]",
6105 "[{ A[] -> [3] }]" },
6106 { &isl_multi_union_pw_aff_union_add,
6107 "[{ A[] -> [1]; B[] -> [4] }]",
6108 "[{ A[] -> [2]; C[] -> [5] }]",
6109 "[{ A[] -> [3]; B[] -> [4]; C[] -> [5] }]" },
6110 { &isl_multi_union_pw_aff_add, "[{ A[x] -> [(1)] : x >= 5 }]",
6111 "[{ A[x] -> [(x)] : x <= 10 }]",
6112 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10 }]" },
6113 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
6114 "([] : { A[x] : x <= 10 })",
6115 "([] : { A[x] : 5 <= x <= 10 })" },
6116 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
6117 "[N] -> ([] : { A[x] : x <= N })",
6118 "[N] -> ([] : { A[x] : 5 <= x <= N })" },
6119 { &isl_multi_union_pw_aff_add, "[N] -> ([] : { A[x] : x >= N })",
6120 "([] : { A[x] : x <= 10 })",
6121 "[N] -> ([] : { A[x] : N <= x <= 10 })" },
6122 { &isl_multi_union_pw_aff_union_add, "[{ A[x] -> [(1)] : x >= 5 }]",
6123 "[{ A[x] -> [(x)] : x <= 10 }]",
6124 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10; "
6125 "A[x] -> [(1)] : x > 10; A[x] -> [(x)] : x < 5 }]" },
6126 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 5 })",
6127 "([] : { A[x] : x <= 10 })",
6128 "([] : { A[x] })" },
6129 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 0 })",
6130 "[N] -> ([] : { A[x] : x >= N })",
6131 "[N] -> ([] : { A[x] : x >= 0 or x >= N })" },
6132 { &isl_multi_union_pw_aff_union_add,
6133 "[N] -> ([] : { A[] : N >= 0})",
6134 "[N] -> ([] : { A[] : N <= 0})",
6135 "[N] -> ([] : { A[] })" },
6136 { &isl_multi_union_pw_aff_union_add,
6137 "[N] -> ([] : { A[] })",
6138 "[N] -> ([] : { : })",
6139 "[N] -> ([] : { : })" },
6140 { &isl_multi_union_pw_aff_union_add,
6141 "[N] -> ([] : { : })",
6142 "[N] -> ([] : { A[] })",
6143 "[N] -> ([] : { : })" },
6144 { &isl_multi_union_pw_aff_union_add,
6145 "[N] -> ([] : { : N >= 0})",
6146 "[N] -> ([] : { : N <= 0})",
6147 "[N] -> ([] : { : })" },
6148 { &isl_multi_union_pw_aff_range_product,
6149 "B[{ A[] -> [1] }]",
6150 "C[{ A[] -> [2] }]",
6151 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]" },
6152 { &isl_multi_union_pw_aff_range_product,
6153 "(B[] : { A[x] : x >= 5 })",
6154 "(C[] : { A[x] : x <= 10 })",
6155 "([B[] -> C[]] : { A[x] : 5 <= x <= 10 })" },
6156 { &isl_multi_union_pw_aff_range_product,
6157 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6158 "(C[] : { A[x] : x <= 10 })",
6159 "[B[{ A[x] -> [x + 1] : 5 <= x <= 10 }] -> C[]]" },
6160 { &isl_multi_union_pw_aff_range_product,
6161 "(C[] : { A[x] : x <= 10 })",
6162 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6163 "[C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= 10 }]]" },
6164 { &isl_multi_union_pw_aff_range_product,
6165 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6166 "[N] -> (C[] : { A[x] : x <= N })",
6167 "[N] -> [B[{ A[x] -> [x + 1] : 5 <= x <= N }] -> C[]]" },
6168 { &isl_multi_union_pw_aff_range_product,
6169 "[N] -> (C[] : { A[x] : x <= N })",
6170 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6171 "[N] -> [C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= N }]]" },
6172 { &isl_multi_union_pw_aff_range_product,
6173 "B[{ A[] -> [1]; D[] -> [3] }]",
6174 "C[{ A[] -> [2] }]",
6175 "[B[{ A[] -> [1]; D[] -> [3] }] -> C[{ A[] -> [2] }]]" },
6176 { &isl_multi_union_pw_aff_range_product,
6177 "B[] }]",
6178 "(C[] : { A[x] })",
6179 "([B[] -> C[]] : { A[x] })" },
6180 { &isl_multi_union_pw_aff_range_product,
6181 "(B[] : { A[x] })",
6182 "C[] }]",
6183 "([B[] -> C[]] : { A[x] })" },
6186 /* Perform some basic tests of binary operations on
6187 * isl_multi_union_pw_aff objects.
6189 static int test_bin_mupa(isl_ctx *ctx)
6191 int i;
6192 isl_bool ok;
6193 isl_multi_union_pw_aff *mupa1, *mupa2, *res;
6195 for (i = 0; i < ARRAY_SIZE(mupa_bin_tests); ++i) {
6196 mupa1 = isl_multi_union_pw_aff_read_from_str(ctx,
6197 mupa_bin_tests[i].arg1);
6198 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx,
6199 mupa_bin_tests[i].arg2);
6200 res = isl_multi_union_pw_aff_read_from_str(ctx,
6201 mupa_bin_tests[i].res);
6202 mupa1 = mupa_bin_tests[i].fn(mupa1, mupa2);
6203 ok = isl_multi_union_pw_aff_plain_is_equal(mupa1, res);
6204 isl_multi_union_pw_aff_free(mupa1);
6205 isl_multi_union_pw_aff_free(res);
6206 if (ok < 0)
6207 return -1;
6208 if (!ok)
6209 isl_die(ctx, isl_error_unknown,
6210 "unexpected result", return -1);
6213 return 0;
6216 /* Inputs for basic tests of binary operations on
6217 * pairs of isl_multi_union_pw_aff and isl_set objects.
6218 * "fn" is the function that is tested.
6219 * "arg1" and "arg2" are string descriptions of the inputs.
6220 * "res" is a string description of the expected result.
6222 struct {
6223 __isl_give isl_multi_union_pw_aff *(*fn)(
6224 __isl_take isl_multi_union_pw_aff *mupa,
6225 __isl_take isl_set *set);
6226 const char *arg1;
6227 const char *arg2;
6228 const char *res;
6229 } mupa_set_tests[] = {
6230 { &isl_multi_union_pw_aff_intersect_range,
6231 "C[{ B[i,j] -> [i + 2j] }]", "{ C[1] }",
6232 "C[{ B[i,j] -> [i + 2j] : i + 2j = 1 }]" },
6233 { &isl_multi_union_pw_aff_intersect_range,
6234 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[N] }",
6235 "[N] -> C[{ B[i,j] -> [i + 2j] : i + 2j = N }]" },
6236 { &isl_multi_union_pw_aff_intersect_range,
6237 "[N] -> C[{ B[i,j] -> [i + 2j + N] }]", "{ C[1] }",
6238 "[N] -> C[{ B[i,j] -> [i + 2j + N] : i + 2j + N = 1 }]" },
6239 { &isl_multi_union_pw_aff_intersect_range,
6240 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[x] : N >= 0 }",
6241 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0 }]" },
6242 { &isl_multi_union_pw_aff_intersect_range,
6243 "C[]", "{ C[] }", "C[]" },
6244 { &isl_multi_union_pw_aff_intersect_range,
6245 "[N] -> (C[] : { : N >= 0 })",
6246 "{ C[] }",
6247 "[N] -> (C[] : { : N >= 0 })" },
6248 { &isl_multi_union_pw_aff_intersect_range,
6249 "(C[] : { A[a,b] })",
6250 "{ C[] }",
6251 "(C[] : { A[a,b] })" },
6252 { &isl_multi_union_pw_aff_intersect_range,
6253 "[N] -> (C[] : { A[a,b] : a,b <= N })",
6254 "{ C[] }",
6255 "[N] -> (C[] : { A[a,b] : a,b <= N })" },
6256 { &isl_multi_union_pw_aff_intersect_range,
6257 "C[]",
6258 "[N] -> { C[] : N >= 0 }",
6259 "[N] -> (C[] : { : N >= 0 })" },
6260 { &isl_multi_union_pw_aff_intersect_range,
6261 "(C[] : { A[a,b] })",
6262 "[N] -> { C[] : N >= 0 }",
6263 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
6264 { &isl_multi_union_pw_aff_intersect_range,
6265 "[N] -> (C[] : { : N >= 0 })",
6266 "[N] -> { C[] : N < 1024 }",
6267 "[N] -> (C[] : { : 0 <= N < 1024 })" },
6268 { &isl_multi_union_pw_aff_intersect_params,
6269 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { : N >= 0 }",
6270 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0}]" },
6271 { &isl_multi_union_pw_aff_intersect_params,
6272 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "[N] -> { : N >= 0 }",
6273 "[N] -> C[{ B[i,j] -> [i + 2j] : 0 <= N <= 256 }]" },
6274 { &isl_multi_union_pw_aff_intersect_params,
6275 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "{ : }",
6276 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]" },
6277 { &isl_multi_union_pw_aff_intersect_params,
6278 "C[]", "[N] -> { : N >= 0 }",
6279 "[N] -> (C[] : { : N >= 0 })" },
6280 { &isl_multi_union_pw_aff_intersect_params,
6281 "(C[] : { A[a,b] })", "[N] -> { : N >= 0 }",
6282 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
6283 { &isl_multi_union_pw_aff_intersect_params,
6284 "[N] -> (C[] : { A[a,N] })", "{ : }",
6285 "[N] -> (C[] : { A[a,N] })" },
6286 { &isl_multi_union_pw_aff_intersect_params,
6287 "[N] -> (C[] : { A[a,b] : N <= 256 })", "[N] -> { : N >= 0 }",
6288 "[N] -> (C[] : { A[a,b] : 0 <= N <= 256 })" },
6291 /* Perform some basic tests of binary operations on
6292 * pairs of isl_multi_union_pw_aff and isl_set objects.
6294 static int test_mupa_set(isl_ctx *ctx)
6296 int i;
6297 isl_bool ok;
6298 isl_multi_union_pw_aff *mupa, *res;
6299 isl_set *set;
6301 for (i = 0; i < ARRAY_SIZE(mupa_set_tests); ++i) {
6302 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6303 mupa_set_tests[i].arg1);
6304 set = isl_set_read_from_str(ctx, mupa_set_tests[i].arg2);
6305 res = isl_multi_union_pw_aff_read_from_str(ctx,
6306 mupa_set_tests[i].res);
6307 mupa = mupa_set_tests[i].fn(mupa, set);
6308 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6309 isl_multi_union_pw_aff_free(mupa);
6310 isl_multi_union_pw_aff_free(res);
6311 if (ok < 0)
6312 return -1;
6313 if (!ok)
6314 isl_die(ctx, isl_error_unknown,
6315 "unexpected result", return -1);
6318 return 0;
6321 /* Inputs for basic tests of binary operations on
6322 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
6323 * "fn" is the function that is tested.
6324 * "arg1" and "arg2" are string descriptions of the inputs.
6325 * "res" is a string description of the expected result.
6327 struct {
6328 __isl_give isl_multi_union_pw_aff *(*fn)(
6329 __isl_take isl_multi_union_pw_aff *mupa,
6330 __isl_take isl_union_set *uset);
6331 const char *arg1;
6332 const char *arg2;
6333 const char *res;
6334 } mupa_uset_tests[] = {
6335 { &isl_multi_union_pw_aff_intersect_domain,
6336 "C[{ B[i,j] -> [i + 2j] }]", "{ B[i,i] }",
6337 "C[{ B[i,i] -> [3i] }]" },
6338 { &isl_multi_union_pw_aff_intersect_domain,
6339 "(C[] : { B[i,j] })", "{ B[i,i] }",
6340 "(C[] : { B[i,i] })" },
6341 { &isl_multi_union_pw_aff_intersect_domain,
6342 "(C[] : { B[i,j] })", "[N] -> { B[N,N] }",
6343 "[N] -> (C[] : { B[N,N] })" },
6344 { &isl_multi_union_pw_aff_intersect_domain,
6345 "C[]", "{ B[i,i] }",
6346 "(C[] : { B[i,i] })" },
6347 { &isl_multi_union_pw_aff_intersect_domain,
6348 "[N] -> (C[] : { : N >= 0 })", "{ B[i,i] }",
6349 "[N] -> (C[] : { B[i,i] : N >= 0 })" },
6352 /* Perform some basic tests of binary operations on
6353 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
6355 static int test_mupa_uset(isl_ctx *ctx)
6357 int i;
6358 isl_bool ok;
6359 isl_multi_union_pw_aff *mupa, *res;
6360 isl_union_set *uset;
6362 for (i = 0; i < ARRAY_SIZE(mupa_uset_tests); ++i) {
6363 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6364 mupa_uset_tests[i].arg1);
6365 uset = isl_union_set_read_from_str(ctx,
6366 mupa_uset_tests[i].arg2);
6367 res = isl_multi_union_pw_aff_read_from_str(ctx,
6368 mupa_uset_tests[i].res);
6369 mupa = mupa_uset_tests[i].fn(mupa, uset);
6370 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6371 isl_multi_union_pw_aff_free(mupa);
6372 isl_multi_union_pw_aff_free(res);
6373 if (ok < 0)
6374 return -1;
6375 if (!ok)
6376 isl_die(ctx, isl_error_unknown,
6377 "unexpected result", return -1);
6380 return 0;
6383 /* Inputs for basic tests of binary operations on
6384 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
6385 * "fn" is the function that is tested.
6386 * "arg1" and "arg2" are string descriptions of the inputs.
6387 * "res" is a string description of the expected result.
6389 struct {
6390 __isl_give isl_multi_union_pw_aff *(*fn)(
6391 __isl_take isl_multi_union_pw_aff *mupa,
6392 __isl_take isl_multi_aff *ma);
6393 const char *arg1;
6394 const char *arg2;
6395 const char *res;
6396 } mupa_ma_tests[] = {
6397 { &isl_multi_union_pw_aff_apply_multi_aff,
6398 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6399 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6400 "{ C[a,b] -> D[b,a] }",
6401 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
6402 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
6403 { &isl_multi_union_pw_aff_apply_multi_aff,
6404 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
6405 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6406 "{ C[a,b] -> D[b,a] }",
6407 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
6408 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
6409 { &isl_multi_union_pw_aff_apply_multi_aff,
6410 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6411 "[N] -> { C[a] -> D[a + N] }",
6412 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }] " },
6413 { &isl_multi_union_pw_aff_apply_multi_aff,
6414 "C[]",
6415 "{ C[] -> D[] }",
6416 "D[]" },
6417 { &isl_multi_union_pw_aff_apply_multi_aff,
6418 "[N] -> (C[] : { : N >= 0 })",
6419 "{ C[] -> D[] }",
6420 "[N] -> (D[] : { : N >= 0 })" },
6421 { &isl_multi_union_pw_aff_apply_multi_aff,
6422 "C[]",
6423 "[N] -> { C[] -> D[N] }",
6424 "[N] -> D[{ [N] }]" },
6425 { &isl_multi_union_pw_aff_apply_multi_aff,
6426 "(C[] : { A[i,j] : i >= j })",
6427 "{ C[] -> D[] }",
6428 "(D[] : { A[i,j] : i >= j })" },
6429 { &isl_multi_union_pw_aff_apply_multi_aff,
6430 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6431 "{ C[] -> D[] }",
6432 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6433 { &isl_multi_union_pw_aff_apply_multi_aff,
6434 "(C[] : { A[i,j] : i >= j })",
6435 "[N] -> { C[] -> D[N] }",
6436 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6439 /* Perform some basic tests of binary operations on
6440 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
6442 static int test_mupa_ma(isl_ctx *ctx)
6444 int i;
6445 isl_bool ok;
6446 isl_multi_union_pw_aff *mupa, *res;
6447 isl_multi_aff *ma;
6449 for (i = 0; i < ARRAY_SIZE(mupa_ma_tests); ++i) {
6450 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6451 mupa_ma_tests[i].arg1);
6452 ma = isl_multi_aff_read_from_str(ctx, mupa_ma_tests[i].arg2);
6453 res = isl_multi_union_pw_aff_read_from_str(ctx,
6454 mupa_ma_tests[i].res);
6455 mupa = mupa_ma_tests[i].fn(mupa, ma);
6456 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6457 isl_multi_union_pw_aff_free(mupa);
6458 isl_multi_union_pw_aff_free(res);
6459 if (ok < 0)
6460 return -1;
6461 if (!ok)
6462 isl_die(ctx, isl_error_unknown,
6463 "unexpected result", return -1);
6466 return 0;
6469 /* Inputs for basic tests of binary operations on
6470 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
6471 * "fn" is the function that is tested.
6472 * "arg1" and "arg2" are string descriptions of the inputs.
6473 * "res" is a string description of the expected result.
6475 struct {
6476 __isl_give isl_union_pw_aff *(*fn)(
6477 __isl_take isl_multi_union_pw_aff *mupa,
6478 __isl_take isl_pw_aff *pa);
6479 const char *arg1;
6480 const char *arg2;
6481 const char *res;
6482 } mupa_pa_tests[] = {
6483 { &isl_multi_union_pw_aff_apply_pw_aff,
6484 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6485 "[N] -> { C[a] -> [a + N] }",
6486 "[N] -> { A[i,j] -> [i + N]; B[i,j] -> [j + N] }" },
6487 { &isl_multi_union_pw_aff_apply_pw_aff,
6488 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6489 "{ C[a] -> [a] : a >= 0; C[a] -> [-a] : a < 0 }",
6490 "{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
6491 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }" },
6492 { &isl_multi_union_pw_aff_apply_pw_aff,
6493 "C[]",
6494 "[N] -> { C[] -> [N] }",
6495 "[N] -> { [N] }" },
6496 { &isl_multi_union_pw_aff_apply_pw_aff,
6497 "C[]",
6498 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
6499 "[N] -> { [N] : N >= 0; [-N] : N < 0 }" },
6500 { &isl_multi_union_pw_aff_apply_pw_aff,
6501 "[N] -> (C[] : { : N >= 0 })",
6502 "[N] -> { C[] -> [N] }",
6503 "[N] -> { [N] : N >= 0 }" },
6504 { &isl_multi_union_pw_aff_apply_pw_aff,
6505 "[N] -> (C[] : { : N >= 0 })",
6506 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
6507 "[N] -> { [N] : N >= 0 }" },
6508 { &isl_multi_union_pw_aff_apply_pw_aff,
6509 "[N] -> (C[] : { : N >= 0 })",
6510 "{ C[] -> [0] }",
6511 "[N] -> { [0] : N >= 0 }" },
6512 { &isl_multi_union_pw_aff_apply_pw_aff,
6513 "(C[] : { A[i,j] : i >= j })",
6514 "[N] -> { C[] -> [N] }",
6515 "[N] -> { A[i,j] -> [N] : i >= j }" },
6516 { &isl_multi_union_pw_aff_apply_pw_aff,
6517 "(C[] : { A[i,j] : i >= j })",
6518 "[N] -> { C[] -> [N] : N >= 0 }",
6519 "[N] -> { A[i,j] -> [N] : i >= j and N >= 0 }" },
6522 /* Perform some basic tests of binary operations on
6523 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
6525 static int test_mupa_pa(isl_ctx *ctx)
6527 int i;
6528 isl_bool ok;
6529 isl_multi_union_pw_aff *mupa;
6530 isl_union_pw_aff *upa, *res;
6531 isl_pw_aff *pa;
6533 for (i = 0; i < ARRAY_SIZE(mupa_pa_tests); ++i) {
6534 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6535 mupa_pa_tests[i].arg1);
6536 pa = isl_pw_aff_read_from_str(ctx, mupa_pa_tests[i].arg2);
6537 res = isl_union_pw_aff_read_from_str(ctx,
6538 mupa_pa_tests[i].res);
6539 upa = mupa_pa_tests[i].fn(mupa, pa);
6540 ok = isl_union_pw_aff_plain_is_equal(upa, res);
6541 isl_union_pw_aff_free(upa);
6542 isl_union_pw_aff_free(res);
6543 if (ok < 0)
6544 return -1;
6545 if (!ok)
6546 isl_die(ctx, isl_error_unknown,
6547 "unexpected result", return -1);
6550 return 0;
6553 /* Inputs for basic tests of binary operations on
6554 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6555 * "fn" is the function that is tested.
6556 * "arg1" and "arg2" are string descriptions of the inputs.
6557 * "res" is a string description of the expected result.
6559 struct {
6560 __isl_give isl_multi_union_pw_aff *(*fn)(
6561 __isl_take isl_multi_union_pw_aff *mupa,
6562 __isl_take isl_pw_multi_aff *pma);
6563 const char *arg1;
6564 const char *arg2;
6565 const char *res;
6566 } mupa_pma_tests[] = {
6567 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6568 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6569 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6570 "{ C[a,b] -> D[b,a] }",
6571 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
6572 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
6573 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6574 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
6575 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6576 "{ C[a,b] -> D[b,a] }",
6577 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
6578 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
6579 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6580 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6581 "[N] -> { C[a] -> D[a + N] }",
6582 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }]" },
6583 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6584 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6585 "{ C[a] -> D[a] : a >= 0; C[a] -> D[-a] : a < 0 }",
6586 "D[{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
6587 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }]" },
6588 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6589 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6590 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6591 "{ C[a,b] -> D[a,b] : a >= b; C[a,b] -> D[b,a] : a < b }",
6592 "D[{ A[i,j] -> [i] : i >= j; A[i,j] -> [j] : i < j; "
6593 "B[i,j] -> [j] : i <= j; B[i,j] -> [i] : i > j }, "
6594 "{ A[i,j] -> [j] : i >= j; A[i,j] -> [i] : i < j; "
6595 "B[i,j] -> [i] : i <= j; B[i,j] -> [j] : i > j }]" },
6596 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6597 "C[]",
6598 "{ C[] -> D[] }",
6599 "D[]" },
6600 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6601 "[N] -> (C[] : { : N >= 0 })",
6602 "{ C[] -> D[] }",
6603 "[N] -> (D[] : { : N >= 0 })" },
6604 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6605 "C[]",
6606 "[N] -> { C[] -> D[N] }",
6607 "[N] -> D[{ [N] }]" },
6608 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6609 "(C[] : { A[i,j] : i >= j })",
6610 "{ C[] -> D[] }",
6611 "(D[] : { A[i,j] : i >= j })" },
6612 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6613 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6614 "{ C[] -> D[] }",
6615 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6616 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6617 "(C[] : { A[i,j] : i >= j })",
6618 "[N] -> { C[] -> D[N] }",
6619 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6620 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6621 "C[]",
6622 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6623 "[N] -> D[{ [N] : N >= 0; [-N] : N < 0 }]" },
6624 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6625 "[N] -> (C[] : { : N >= 0 })",
6626 "[N] -> { C[] -> D[N] }",
6627 "[N] -> D[{ [N] : N >= 0 }]" },
6628 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6629 "[N] -> (C[] : { : N >= 0 })",
6630 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6631 "[N] -> D[{ [N] : N >= 0 }]" },
6632 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6633 "[N] -> (C[] : { : N >= 0 })",
6634 "{ C[] -> D[0] }",
6635 "[N] -> D[{ [0] : N >= 0 }]" },
6636 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6637 "(C[] : { A[i,j] : i >= j })",
6638 "[N] -> { C[] -> D[N] : N >= 0 }",
6639 "[N] -> D[{ A[i,j] -> [N] : i >= j and N >= 0 }]" },
6642 /* Perform some basic tests of binary operations on
6643 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6645 static int test_mupa_pma(isl_ctx *ctx)
6647 int i;
6648 isl_bool ok;
6649 isl_multi_union_pw_aff *mupa, *res;
6650 isl_pw_multi_aff *pma;
6652 for (i = 0; i < ARRAY_SIZE(mupa_pma_tests); ++i) {
6653 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6654 mupa_pma_tests[i].arg1);
6655 pma = isl_pw_multi_aff_read_from_str(ctx,
6656 mupa_pma_tests[i].arg2);
6657 res = isl_multi_union_pw_aff_read_from_str(ctx,
6658 mupa_pma_tests[i].res);
6659 mupa = mupa_pma_tests[i].fn(mupa, pma);
6660 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6661 isl_multi_union_pw_aff_free(mupa);
6662 isl_multi_union_pw_aff_free(res);
6663 if (ok < 0)
6664 return -1;
6665 if (!ok)
6666 isl_die(ctx, isl_error_unknown,
6667 "unexpected result", return -1);
6670 return 0;
6673 /* Inputs for basic tests of binary operations on
6674 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6675 * "fn" is the function that is tested.
6676 * "arg1" and "arg2" are string descriptions of the inputs.
6677 * "res" is a string description of the expected result.
6679 struct {
6680 __isl_give isl_multi_union_pw_aff *(*fn)(
6681 __isl_take isl_multi_union_pw_aff *mupa,
6682 __isl_take isl_union_pw_multi_aff *upma);
6683 const char *arg1;
6684 const char *arg2;
6685 const char *res;
6686 } mupa_upma_tests[] = {
6687 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6688 "C[{ B[i,j] -> [i + 2j] }]", "{ A[a,b] -> B[b,a] }",
6689 "C[{ A[a,b] -> [b + 2a] }]" },
6690 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6691 "C[{ B[i,j] -> [i + 2j] }]",
6692 "{ A[a,b] -> B[b,a] : b > a }",
6693 "C[{ A[a,b] -> [b + 2a] : b > a }]" },
6694 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6695 "C[{ B[i,j] -> [i + 2j] : j > 4 }]",
6696 "{ A[a,b] -> B[b,a] : b > a }",
6697 "C[{ A[a,b] -> [b + 2a] : b > a > 4 }]" },
6698 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6699 "C[{ B[i,j] -> [i + 2j] }]",
6700 "{ A[a,b] -> B[b,a] : a > b; A[a,b] -> B[a,b] : a <= b }",
6701 "C[{ A[a,b] -> [b + 2a] : a > b; A[a,b] -> [a + 2b] : a <= b }]" },
6702 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6703 "(C[] : { B[a,b] })",
6704 "{ A[a,b] -> B[b,a] }",
6705 "(C[] : { A[a,b] })" },
6706 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6707 "(C[] : { B[a,b] })",
6708 "{ B[a,b] -> A[b,a] }",
6709 "(C[] : { })" },
6710 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6711 "(C[] : { B[a,b] })",
6712 "{ A[a,b] -> B[b,a] : a > b }",
6713 "(C[] : { A[a,b] : a > b })" },
6714 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6715 "(C[] : { B[a,b] : a > b })",
6716 "{ A[a,b] -> B[b,a] }",
6717 "(C[] : { A[a,b] : b > a })" },
6718 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6719 "[N] -> (C[] : { B[a,b] : a > N })",
6720 "{ A[a,b] -> B[b,a] : a > b }",
6721 "[N] -> (C[] : { A[a,b] : a > b > N })" },
6722 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6723 "(C[] : { B[a,b] : a > b })",
6724 "[N] -> { A[a,b] -> B[b,a] : a > N }",
6725 "[N] -> (C[] : { A[a,b] : b > a > N })" },
6726 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6727 "C[]",
6728 "{ A[a,b] -> B[b,a] }",
6729 "C[]" },
6730 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6731 "[N] -> (C[] : { : N >= 0 })",
6732 "{ A[a,b] -> B[b,a] }",
6733 "[N] -> (C[] : { : N >= 0 })" },
6734 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6735 "C[]",
6736 "[N] -> { A[a,b] -> B[b,a] : N >= 0 }",
6737 "[N] -> (C[] : { : N >= 0 })" },
6740 /* Perform some basic tests of binary operations on
6741 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6743 static int test_mupa_upma(isl_ctx *ctx)
6745 int i;
6746 isl_bool ok;
6747 isl_multi_union_pw_aff *mupa, *res;
6748 isl_union_pw_multi_aff *upma;
6750 for (i = 0; i < ARRAY_SIZE(mupa_upma_tests); ++i) {
6751 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6752 mupa_upma_tests[i].arg1);
6753 upma = isl_union_pw_multi_aff_read_from_str(ctx,
6754 mupa_upma_tests[i].arg2);
6755 res = isl_multi_union_pw_aff_read_from_str(ctx,
6756 mupa_upma_tests[i].res);
6757 mupa = mupa_upma_tests[i].fn(mupa, upma);
6758 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6759 isl_multi_union_pw_aff_free(mupa);
6760 isl_multi_union_pw_aff_free(res);
6761 if (ok < 0)
6762 return -1;
6763 if (!ok)
6764 isl_die(ctx, isl_error_unknown,
6765 "unexpected result", return -1);
6768 return 0;
6771 /* Check that the input tuple of an isl_aff can be set properly.
6773 static isl_stat test_aff_set_tuple_id(isl_ctx *ctx)
6775 isl_id *id;
6776 isl_aff *aff;
6777 isl_stat equal;
6779 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x + 1] }");
6780 id = isl_id_alloc(ctx, "A", NULL);
6781 aff = isl_aff_set_tuple_id(aff, isl_dim_in, id);
6782 equal = aff_check_plain_equal(aff, "{ A[x] -> [x + 1] }");
6783 isl_aff_free(aff);
6784 if (equal < 0)
6785 return isl_stat_error;
6787 return isl_stat_ok;
6790 /* Check that affine expressions get normalized on addition/subtraction.
6791 * In particular, check that (final) unused integer divisions get removed
6792 * such that an expression derived from expressions with integer divisions
6793 * is found to be obviously equal to one that is created directly.
6795 static isl_stat test_aff_normalize(isl_ctx *ctx)
6797 isl_aff *aff, *aff2;
6798 isl_stat ok;
6800 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x//2] }");
6801 aff2 = isl_aff_read_from_str(ctx, "{ [x] -> [1 + x//2] }");
6802 aff = isl_aff_sub(aff2, aff);
6803 ok = aff_check_plain_equal(aff, "{ [x] -> [1] }");
6804 isl_aff_free(aff);
6806 return ok;
6809 int test_aff(isl_ctx *ctx)
6811 const char *str;
6812 isl_set *set;
6813 isl_space *space;
6814 isl_local_space *ls;
6815 isl_aff *aff;
6816 int zero;
6817 isl_stat equal;
6819 if (test_upa(ctx) < 0)
6820 return -1;
6821 if (test_bin_aff(ctx) < 0)
6822 return -1;
6823 if (test_bin_pw_aff(ctx) < 0)
6824 return -1;
6825 if (test_upma_test(ctx) < 0)
6826 return -1;
6827 if (test_bin_upma(ctx) < 0)
6828 return -1;
6829 if (test_bin_upma_fail(ctx) < 0)
6830 return -1;
6831 if (test_upma_uset(ctx) < 0)
6832 return -1;
6833 if (test_un_mpa(ctx) < 0)
6834 return -1;
6835 if (test_bin_mpa(ctx) < 0)
6836 return -1;
6837 if (test_un_mupa(ctx) < 0)
6838 return -1;
6839 if (test_bin_mupa(ctx) < 0)
6840 return -1;
6841 if (test_mupa_set(ctx) < 0)
6842 return -1;
6843 if (test_mupa_uset(ctx) < 0)
6844 return -1;
6845 if (test_mupa_ma(ctx) < 0)
6846 return -1;
6847 if (test_mupa_pa(ctx) < 0)
6848 return -1;
6849 if (test_mupa_pma(ctx) < 0)
6850 return -1;
6851 if (test_mupa_upma(ctx) < 0)
6852 return -1;
6854 space = isl_space_set_alloc(ctx, 0, 1);
6855 ls = isl_local_space_from_space(space);
6856 aff = isl_aff_zero_on_domain(ls);
6858 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6859 aff = isl_aff_scale_down_ui(aff, 3);
6860 aff = isl_aff_floor(aff);
6861 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6862 aff = isl_aff_scale_down_ui(aff, 2);
6863 aff = isl_aff_floor(aff);
6864 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6866 str = "{ [10] }";
6867 set = isl_set_read_from_str(ctx, str);
6868 aff = isl_aff_gist(aff, set);
6870 aff = isl_aff_add_constant_si(aff, -16);
6871 zero = isl_aff_plain_is_zero(aff);
6872 isl_aff_free(aff);
6874 if (zero < 0)
6875 return -1;
6876 if (!zero)
6877 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6879 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
6880 aff = isl_aff_scale_down_ui(aff, 64);
6881 aff = isl_aff_floor(aff);
6882 equal = aff_check_plain_equal(aff, "{ [-1] }");
6883 isl_aff_free(aff);
6884 if (equal < 0)
6885 return -1;
6887 if (test_aff_set_tuple_id(ctx) < 0)
6888 return -1;
6889 if (test_aff_normalize(ctx) < 0)
6890 return -1;
6892 return 0;
6895 /* Inputs for isl_set_bind tests.
6896 * "set" is the input set.
6897 * "tuple" is the binding tuple.
6898 * "res" is the expected result.
6900 static
6901 struct {
6902 const char *set;
6903 const char *tuple;
6904 const char *res;
6905 } bind_set_tests[] = {
6906 { "{ A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
6907 "{ A[M, N] }",
6908 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
6909 { "{ B[N, M] : M mod 2 = 0 and N mod 8 = 3 }",
6910 "{ B[N, M] }",
6911 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
6912 { "[M] -> { C[N] : M mod 2 = 0 and N mod 8 = 3 }",
6913 "{ C[N] }",
6914 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
6915 { "[M] -> { D[x, N] : x mod 2 = 0 and N mod 8 = 3 and M >= 0 }",
6916 "{ D[M, N] }",
6917 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 and M >= 0 }" },
6920 /* Perform basic isl_set_bind tests.
6922 static isl_stat test_bind_set(isl_ctx *ctx)
6924 int i;
6926 for (i = 0; i < ARRAY_SIZE(bind_set_tests); ++i) {
6927 const char *str;
6928 isl_set *set;
6929 isl_multi_id *tuple;
6930 isl_stat r;
6932 set = isl_set_read_from_str(ctx, bind_set_tests[i].set);
6933 str = bind_set_tests[i].tuple;
6934 tuple = isl_multi_id_read_from_str(ctx, str);
6935 set = isl_set_bind(set, tuple);
6936 r = set_check_equal(set, bind_set_tests[i].res);
6937 isl_set_free(set);
6938 if (r < 0)
6939 return isl_stat_error;
6942 return isl_stat_ok;
6945 /* Inputs for isl_map_bind_domain tests.
6946 * "map" is the input map.
6947 * "tuple" is the binding tuple.
6948 * "res" is the expected result.
6950 struct {
6951 const char *map;
6952 const char *tuple;
6953 const char *res;
6954 } bind_map_domain_tests[] = {
6955 { "{ A[M, N] -> [M + floor(N/2)] }",
6956 "{ A[M, N] }",
6957 "[M, N] -> { [M + floor(N/2)] }" },
6958 { "{ B[N, M] -> [M + floor(N/2)] }",
6959 "{ B[N, M] }",
6960 "[N, M] -> { [M + floor(N/2)] }" },
6961 { "[M] -> { C[N] -> [M + floor(N/2)] }",
6962 "{ C[N] }",
6963 "[M, N] -> { [M + floor(N/2)] }" },
6964 { "[M] -> { C[x, N] -> [x + floor(N/2)] }",
6965 "{ C[M, N] }",
6966 "[M, N] -> { [M + floor(N/2)] }" },
6967 { "[M] -> { C[x, N] -> [M + floor(N/2)] }",
6968 "{ C[M, N] }",
6969 "[M, N] -> { [M + floor(N/2)] }" },
6970 { "[A, M] -> { C[N, x] -> [x + floor(N/2)] }",
6971 "{ C[N, M] }",
6972 "[A, N, M] -> { [M + floor(N/2)] }" },
6975 /* Perform basic isl_map_bind_domain tests.
6977 static isl_stat test_bind_map_domain(isl_ctx *ctx)
6979 int i;
6981 for (i = 0; i < ARRAY_SIZE(bind_map_domain_tests); ++i) {
6982 const char *str;
6983 isl_map *map;
6984 isl_set *set;
6985 isl_multi_id *tuple;
6986 isl_stat r;
6988 str = bind_map_domain_tests[i].map;
6989 map = isl_map_read_from_str(ctx, str);
6990 str = bind_map_domain_tests[i].tuple;
6991 tuple = isl_multi_id_read_from_str(ctx, str);
6992 set = isl_map_bind_domain(map, tuple);
6993 str = bind_map_domain_tests[i].res;
6994 r = set_check_equal(set, str);
6995 isl_set_free(set);
6996 if (r < 0)
6997 return isl_stat_error;
7000 return isl_stat_ok;
7003 /* Inputs for isl_union_map_bind_range tests.
7004 * "map" is the input union map.
7005 * "tuple" is the binding tuple.
7006 * "res" is the expected result.
7008 struct {
7009 const char *map;
7010 const char *tuple;
7011 const char *res;
7012 } bind_umap_range_tests[] = {
7013 { "{ B[N, M] -> A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
7014 "{ A[M, N] }",
7015 "[M, N] -> { B[N, M] : M mod 2 = 0 and N mod 8 = 3 }" },
7016 { "{ B[N, M] -> A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
7017 "{ B[M, N] }",
7018 "{ }" },
7019 { "{ A[] -> B[]; C[] -> D[]; E[] -> B[] }",
7020 "{ B[] }",
7021 "{ A[]; E[] }" },
7024 /* Perform basic isl_union_map_bind_range tests.
7026 static isl_stat test_bind_umap_range(isl_ctx *ctx)
7028 int i;
7030 for (i = 0; i < ARRAY_SIZE(bind_umap_range_tests); ++i) {
7031 const char *str;
7032 isl_union_map *umap;
7033 isl_union_set *uset;
7034 isl_multi_id *tuple;
7035 isl_stat r;
7037 str = bind_umap_range_tests[i].map;
7038 umap = isl_union_map_read_from_str(ctx, str);
7039 str = bind_umap_range_tests[i].tuple;
7040 tuple = isl_multi_id_read_from_str(ctx, str);
7041 uset = isl_union_map_bind_range(umap, tuple);
7042 str = bind_umap_range_tests[i].res;
7043 r = uset_check_equal(uset, str);
7044 isl_union_set_free(uset);
7045 if (r < 0)
7046 return isl_stat_error;
7049 return isl_stat_ok;
7052 /* Inputs for isl_pw_multi_aff_bind_domain tests.
7053 * "pma" is the input expression.
7054 * "tuple" is the binding tuple.
7055 * "res" is the expected result.
7057 struct {
7058 const char *pma;
7059 const char *tuple;
7060 const char *res;
7061 } bind_pma_domain_tests[] = {
7062 { "{ A[M, N] -> [M + floor(N/2)] }",
7063 "{ A[M, N] }",
7064 "[M, N] -> { [M + floor(N/2)] }" },
7065 { "{ B[N, M] -> [M + floor(N/2)] }",
7066 "{ B[N, M] }",
7067 "[N, M] -> { [M + floor(N/2)] }" },
7068 { "[M] -> { C[N] -> [M + floor(N/2)] }",
7069 "{ C[N] }",
7070 "[M, N] -> { [M + floor(N/2)] }" },
7071 { "[M] -> { C[x, N] -> [x + floor(N/2)] }",
7072 "{ C[M, N] }",
7073 "[M, N] -> { [M + floor(N/2)] }" },
7074 { "[M] -> { C[x, N] -> [M + floor(N/2)] }",
7075 "{ C[M, N] }",
7076 "[M, N] -> { [M + floor(N/2)] }" },
7077 { "[A, M] -> { C[N, x] -> [x + floor(N/2)] }",
7078 "{ C[N, M] }",
7079 "[A, N, M] -> { [M + floor(N/2)] }" },
7082 /* Perform basic isl_pw_multi_aff_bind_domain tests.
7084 static isl_stat test_bind_pma_domain(isl_ctx *ctx)
7086 int i;
7088 for (i = 0; i < ARRAY_SIZE(bind_pma_domain_tests); ++i) {
7089 const char *str;
7090 isl_pw_multi_aff *pma;
7091 isl_multi_id *tuple;
7092 isl_stat r;
7094 str = bind_pma_domain_tests[i].pma;
7095 pma = isl_pw_multi_aff_read_from_str(ctx, str);
7096 str = bind_pma_domain_tests[i].tuple;
7097 tuple = isl_multi_id_read_from_str(ctx, str);
7098 pma = isl_pw_multi_aff_bind_domain(pma, tuple);
7099 str = bind_pma_domain_tests[i].res;
7100 r = pw_multi_aff_check_plain_equal(pma, str);
7101 isl_pw_multi_aff_free(pma);
7102 if (r < 0)
7103 return isl_stat_error;
7106 return isl_stat_ok;
7109 /* Inputs for isl_pw_multi_aff_bind_domain_wrapped_domain tests.
7110 * "pma" is the input expression.
7111 * "tuple" is the binding tuple.
7112 * "res" is the expected result.
7114 struct {
7115 const char *pma;
7116 const char *tuple;
7117 const char *res;
7118 } bind_pma_domain_wrapped_tests[] = {
7119 { "{ [A[M, N] -> B[]] -> [M + floor(N/2)] }",
7120 "{ A[M, N] }",
7121 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
7122 { "{ [B[N, M] -> D[]] -> [M + floor(N/2)] }",
7123 "{ B[N, M] }",
7124 "[N, M] -> { D[] -> [M + floor(N/2)] }" },
7125 { "[M] -> { [C[N] -> B[x]] -> [x + M + floor(N/2)] }",
7126 "{ C[N] }",
7127 "[M, N] -> { B[x] -> [x + M + floor(N/2)] }" },
7128 { "[M] -> { [C[x, N] -> B[]] -> [x + floor(N/2)] }",
7129 "{ C[M, N] }",
7130 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
7131 { "[M] -> { [C[x, N] -> B[]] -> [M + floor(N/2)] }",
7132 "{ C[M, N] }",
7133 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
7134 { "[A, M] -> { [C[N, x] -> B[]] -> [x + floor(N/2)] }",
7135 "{ C[N, M] }",
7136 "[A, N, M] -> { B[] -> [M + floor(N/2)] }" },
7139 /* Perform basic isl_pw_multi_aff_bind_domain_wrapped_domain tests.
7141 static isl_stat test_bind_pma_domain_wrapped(isl_ctx *ctx)
7143 int i;
7145 for (i = 0; i < ARRAY_SIZE(bind_pma_domain_wrapped_tests); ++i) {
7146 const char *str;
7147 isl_pw_multi_aff *pma;
7148 isl_multi_id *tuple;
7149 isl_stat r;
7151 str = bind_pma_domain_wrapped_tests[i].pma;
7152 pma = isl_pw_multi_aff_read_from_str(ctx, str);
7153 str = bind_pma_domain_wrapped_tests[i].tuple;
7154 tuple = isl_multi_id_read_from_str(ctx, str);
7155 pma = isl_pw_multi_aff_bind_domain_wrapped_domain(pma, tuple);
7156 str = bind_pma_domain_wrapped_tests[i].res;
7157 r = pw_multi_aff_check_plain_equal(pma, str);
7158 isl_pw_multi_aff_free(pma);
7159 if (r < 0)
7160 return isl_stat_error;
7163 return isl_stat_ok;
7166 /* Inputs for isl_aff_bind_id tests.
7167 * "aff" is the input expression.
7168 * "id" is the binding id.
7169 * "res" is the expected result.
7171 static
7172 struct {
7173 const char *aff;
7174 const char *id;
7175 const char *res;
7176 } bind_aff_tests[] = {
7177 { "{ [4] }", "M", "[M = 4] -> { : }" },
7178 { "{ B[x] -> [floor(x/2)] }", "M", "[M] -> { B[x] : M = floor(x/2) }" },
7179 { "[M] -> { [4] }", "M", "[M = 4] -> { : }" },
7180 { "[M] -> { [floor(M/2)] }", "M", "[M] -> { : floor(M/2) = M }" },
7181 { "{ [NaN] }", "M", "{ : false }" },
7182 { "{ A[x] -> [NaN] }", "M", "{ A[x] : false }" },
7185 /* Perform basic isl_aff_bind_id tests.
7187 static isl_stat test_bind_aff(isl_ctx *ctx)
7189 int i;
7191 for (i = 0; i < ARRAY_SIZE(bind_aff_tests); ++i) {
7192 isl_aff *aff;
7193 isl_set *res;
7194 isl_id *id;
7195 isl_stat r;
7197 aff = isl_aff_read_from_str(ctx, bind_aff_tests[i].aff);
7198 id = isl_id_read_from_str(ctx, bind_aff_tests[i].id);
7199 res = isl_set_from_basic_set(isl_aff_bind_id(aff, id));
7200 r = set_check_equal(res, bind_aff_tests[i].res);
7201 isl_set_free(res);
7202 if (r < 0)
7203 return isl_stat_error;
7206 return isl_stat_ok;
7209 /* Inputs for isl_pw_aff_bind_id tests.
7210 * "pa" is the input expression.
7211 * "id" is the binding id.
7212 * "res" is the expected result.
7214 static
7215 struct {
7216 const char *pa;
7217 const char *id;
7218 const char *res;
7219 } bind_pa_tests[] = {
7220 { "{ [4] }", "M", "[M = 4] -> { : }" },
7221 { "{ B[x] -> [floor(x/2)] }", "M", "[M] -> { B[x] : M = floor(x/2) }" },
7222 { "[M] -> { [4] }", "M", "[M = 4] -> { : }" },
7223 { "[M] -> { [floor(M/2)] }", "M", "[M] -> { : floor(M/2) = M }" },
7224 { "[M] -> { [M] : M >= 0; [-M] : M < 0 }", "M", "[M] -> { : M >= 0 }" },
7225 { "{ [NaN] }", "M", "{ : false }" },
7226 { "{ A[x] -> [NaN] }", "M", "{ A[x] : false }" },
7229 /* Perform basic isl_pw_aff_bind_id tests.
7231 static isl_stat test_bind_pa(isl_ctx *ctx)
7233 int i;
7235 for (i = 0; i < ARRAY_SIZE(bind_pa_tests); ++i) {
7236 isl_pw_aff *pa;
7237 isl_set *res;
7238 isl_id *id;
7239 isl_stat r;
7241 pa = isl_pw_aff_read_from_str(ctx, bind_pa_tests[i].pa);
7242 id = isl_id_read_from_str(ctx, bind_pa_tests[i].id);
7243 res = isl_pw_aff_bind_id(pa, id);
7244 r = set_check_equal(res, bind_pa_tests[i].res);
7245 isl_set_free(res);
7246 if (r < 0)
7247 return isl_stat_error;
7250 return isl_stat_ok;
7253 /* Inputs for isl_multi_union_pw_aff_bind tests.
7254 * "mupa" is the input expression.
7255 * "tuple" is the binding tuple.
7256 * "res" is the expected result.
7258 static
7259 struct {
7260 const char *mupa;
7261 const char *tuple;
7262 const char *res;
7263 } bind_mupa_tests[] = {
7264 { "A[{ [4] }, { [5] }]",
7265 "{ A[M, N] }",
7266 "[M = 4, N = 5] -> { : }" },
7267 { "A[{ B[x] -> [floor(x/2)] }, { B[y] -> [y + 5] }]",
7268 "{ A[M, N] }",
7269 "[M, N] -> { B[x] : M = floor(x/2) and N = x + 5 }" },
7270 { "[M] -> A[{ [4] }, { [M + 1] }]",
7271 "{ A[M, N] }",
7272 "[M = 4, N = 5] -> { : }" },
7275 /* Perform basic isl_multi_union_pw_aff_bind tests.
7277 static isl_stat test_bind_mupa(isl_ctx *ctx)
7279 int i;
7281 for (i = 0; i < ARRAY_SIZE(bind_mupa_tests); ++i) {
7282 const char *str;
7283 isl_multi_union_pw_aff *mupa;
7284 isl_union_set *res;
7285 isl_multi_id *tuple;
7286 isl_stat r;
7288 str = bind_mupa_tests[i].mupa;
7289 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7290 str = bind_mupa_tests[i].tuple;
7291 tuple = isl_multi_id_read_from_str(ctx, str);
7292 res = isl_multi_union_pw_aff_bind(mupa, tuple);
7293 r = uset_check_equal(res, bind_mupa_tests[i].res);
7294 isl_union_set_free(res);
7295 if (r < 0)
7296 return isl_stat_error;
7299 return isl_stat_ok;
7302 /* Perform tests that reinterpret dimensions as parameters.
7304 static int test_bind(isl_ctx *ctx)
7306 if (test_bind_set(ctx) < 0)
7307 return -1;
7308 if (test_bind_map_domain(ctx) < 0)
7309 return -1;
7310 if (test_bind_umap_range(ctx) < 0)
7311 return -1;
7312 if (test_bind_pma_domain(ctx) < 0)
7313 return -1;
7314 if (test_bind_pma_domain_wrapped(ctx) < 0)
7315 return -1;
7316 if (test_bind_aff(ctx) < 0)
7317 return -1;
7318 if (test_bind_pa(ctx) < 0)
7319 return -1;
7320 if (test_bind_mupa(ctx) < 0)
7321 return -1;
7323 return 0;
7326 /* Inputs for isl_set_unbind_params tests.
7327 * "set" is the input parameter domain.
7328 * "tuple" is the tuple of the constructed set.
7329 * "res" is the expected result.
7331 struct {
7332 const char *set;
7333 const char *tuple;
7334 const char *res;
7335 } unbind_set_tests[] = {
7336 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7337 "{ A[M, N] }",
7338 "{ A[M, N] : M mod 2 = 0 and N mod 8 = 3 }" },
7339 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7340 "{ B[N, M] }",
7341 "{ B[N, M] : M mod 2 = 0 and N mod 8 = 3 }" },
7342 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7343 "{ C[N] }",
7344 "[M] -> { C[N] : M mod 2 = 0 and N mod 8 = 3 }" },
7345 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7346 "{ D[T, N] }",
7347 "[M] -> { D[x, N] : M mod 2 = 0 and N mod 8 = 3 }" },
7350 /* Perform basic isl_set_unbind_params tests.
7352 static isl_stat test_unbind_set(isl_ctx *ctx)
7354 int i;
7356 for (i = 0; i < ARRAY_SIZE(unbind_set_tests); ++i) {
7357 const char *str;
7358 isl_set *set;
7359 isl_multi_id *tuple;
7360 isl_stat r;
7362 set = isl_set_read_from_str(ctx, unbind_set_tests[i].set);
7363 str = unbind_set_tests[i].tuple;
7364 tuple = isl_multi_id_read_from_str(ctx, str);
7365 set = isl_set_unbind_params(set, tuple);
7366 r = set_check_equal(set, unbind_set_tests[i].res);
7367 isl_set_free(set);
7368 if (r < 0)
7369 return isl_stat_error;
7372 return isl_stat_ok;
7375 /* Inputs for isl_aff_unbind_params_insert_domain tests.
7376 * "aff" is the input affine expression defined over a parameter domain.
7377 * "tuple" is the tuple of the domain that gets introduced.
7378 * "res" is the expected result.
7380 struct {
7381 const char *aff;
7382 const char *tuple;
7383 const char *res;
7384 } unbind_aff_tests[] = {
7385 { "[M, N] -> { [M + floor(N/2)] }",
7386 "{ A[M, N] }",
7387 "{ A[M, N] -> [M + floor(N/2)] }" },
7388 { "[M, N] -> { [M + floor(N/2)] }",
7389 "{ B[N, M] }",
7390 "{ B[N, M] -> [M + floor(N/2)] }" },
7391 { "[M, N] -> { [M + floor(N/2)] }",
7392 "{ C[N] }",
7393 "[M] -> { C[N] -> [M + floor(N/2)] }" },
7394 { "[M, N] -> { [M + floor(N/2)] }",
7395 "{ D[A, B, C, N, Z] }",
7396 "[M] -> { D[A, B, C, N, Z] -> [M + floor(N/2)] }" },
7399 /* Perform basic isl_aff_unbind_params_insert_domain tests.
7401 static isl_stat test_unbind_aff(isl_ctx *ctx)
7403 int i;
7405 for (i = 0; i < ARRAY_SIZE(unbind_aff_tests); ++i) {
7406 const char *str;
7407 isl_aff *aff;
7408 isl_multi_id *tuple;
7409 isl_stat r;
7411 aff = isl_aff_read_from_str(ctx, unbind_aff_tests[i].aff);
7412 str = unbind_aff_tests[i].tuple;
7413 tuple = isl_multi_id_read_from_str(ctx, str);
7414 aff = isl_aff_unbind_params_insert_domain(aff, tuple);
7415 r = aff_check_plain_equal(aff, unbind_aff_tests[i].res);
7416 isl_aff_free(aff);
7417 if (r < 0)
7418 return isl_stat_error;
7421 return isl_stat_ok;
7424 /* Inputs for isl_multi_aff_unbind_params_insert_domain tests.
7425 * "ma" is the input multi affine expression defined over a parameter domain.
7426 * "tuple" is the tuple of the domain that gets introduced.
7427 * "res" is the expected result.
7429 static struct {
7430 const char *ma;
7431 const char *tuple;
7432 const char *res;
7433 } unbind_multi_aff_tests[] = {
7434 { "[M, N] -> { T[M + floor(N/2)] }",
7435 "{ A[M, N] }",
7436 "{ A[M, N] -> T[M + floor(N/2)] }" },
7437 { "[M, N] -> { [M + floor(N/2)] }",
7438 "{ B[N, M] }",
7439 "{ B[N, M] -> [M + floor(N/2)] }" },
7440 { "[M, N] -> { [M + floor(N/2)] }",
7441 "{ C[N] }",
7442 "[M] -> { C[N] -> [M + floor(N/2)] }" },
7443 { "[M, N] -> { [M + floor(N/2)] }",
7444 "{ D[A, B, C, N, Z] }",
7445 "[M] -> { D[A, B, C, N, Z] -> [M + floor(N/2)] }" },
7446 { "[M, N] -> { T[M + floor(N/2), N + floor(M/3)] }",
7447 "{ A[M, N] }",
7448 "{ A[M, N] -> T[M + floor(N/2), N + floor(M/3)] }" },
7451 /* Perform basic isl_multi_aff_unbind_params_insert_domain tests.
7453 static isl_stat test_unbind_multi_aff(isl_ctx *ctx)
7455 int i;
7457 for (i = 0; i < ARRAY_SIZE(unbind_multi_aff_tests); ++i) {
7458 const char *str;
7459 isl_multi_aff *ma;
7460 isl_multi_id *tuple;
7461 isl_stat r;
7463 str = unbind_multi_aff_tests[i].ma;
7464 ma = isl_multi_aff_read_from_str(ctx, str);
7465 str = unbind_multi_aff_tests[i].tuple;
7466 tuple = isl_multi_id_read_from_str(ctx, str);
7467 ma = isl_multi_aff_unbind_params_insert_domain(ma, tuple);
7468 str = unbind_multi_aff_tests[i].res;
7469 r = multi_aff_check_plain_equal(ma, str);
7470 isl_multi_aff_free(ma);
7471 if (r < 0)
7472 return isl_stat_error;
7475 return isl_stat_ok;
7478 /* Perform tests that reinterpret parameters.
7480 static int test_unbind(isl_ctx *ctx)
7482 if (test_unbind_set(ctx) < 0)
7483 return -1;
7484 if (test_unbind_aff(ctx) < 0)
7485 return -1;
7486 if (test_unbind_multi_aff(ctx) < 0)
7487 return -1;
7489 return 0;
7492 /* Check that "pa" consists of a single expression.
7494 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
7496 isl_size n;
7498 n = isl_pw_aff_n_piece(pa);
7499 isl_pw_aff_free(pa);
7501 if (n < 0)
7502 return -1;
7503 if (n != 1)
7504 isl_die(ctx, isl_error_unknown, "expecting single expression",
7505 return -1);
7507 return 0;
7510 /* Check that the computation below results in a single expression.
7511 * One or two expressions may result depending on which constraint
7512 * ends up being considered as redundant with respect to the other
7513 * constraints after the projection that is performed internally
7514 * by isl_set_dim_min.
7516 static int test_dim_max_1(isl_ctx *ctx)
7518 const char *str;
7519 isl_set *set;
7520 isl_pw_aff *pa;
7522 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
7523 "-4a <= b <= 3 and b < n - 4a }";
7524 set = isl_set_read_from_str(ctx, str);
7525 pa = isl_set_dim_min(set, 0);
7526 return check_single_piece(ctx, pa);
7529 /* Check that the computation below results in a single expression.
7530 * The PIP problem corresponding to these constraints has a row
7531 * that causes a split of the solution domain. The solver should
7532 * first pick rows that split off empty parts such that the actual
7533 * solution domain does not get split.
7534 * Note that the description contains some redundant constraints.
7535 * If these constraints get removed first, then the row mentioned
7536 * above does not appear in the PIP problem.
7538 static int test_dim_max_2(isl_ctx *ctx)
7540 const char *str;
7541 isl_set *set;
7542 isl_pw_aff *pa;
7544 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
7545 "N > 0 and P >= 0 }";
7546 set = isl_set_read_from_str(ctx, str);
7547 pa = isl_set_dim_max(set, 0);
7548 return check_single_piece(ctx, pa);
7551 int test_dim_max(isl_ctx *ctx)
7553 int equal;
7554 const char *str;
7555 isl_set *set1, *set2;
7556 isl_set *set;
7557 isl_map *map;
7558 isl_pw_aff *pwaff;
7560 if (test_dim_max_1(ctx) < 0)
7561 return -1;
7562 if (test_dim_max_2(ctx) < 0)
7563 return -1;
7565 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
7566 set = isl_set_read_from_str(ctx, str);
7567 pwaff = isl_set_dim_max(set, 0);
7568 set1 = isl_set_from_pw_aff(pwaff);
7569 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
7570 set2 = isl_set_read_from_str(ctx, str);
7571 equal = isl_set_is_equal(set1, set2);
7572 isl_set_free(set1);
7573 isl_set_free(set2);
7574 if (equal < 0)
7575 return -1;
7576 if (!equal)
7577 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7579 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
7580 set = isl_set_read_from_str(ctx, str);
7581 pwaff = isl_set_dim_max(set, 0);
7582 set1 = isl_set_from_pw_aff(pwaff);
7583 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
7584 set2 = isl_set_read_from_str(ctx, str);
7585 equal = isl_set_is_equal(set1, set2);
7586 isl_set_free(set1);
7587 isl_set_free(set2);
7588 if (equal < 0)
7589 return -1;
7590 if (!equal)
7591 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7593 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
7594 set = isl_set_read_from_str(ctx, str);
7595 pwaff = isl_set_dim_max(set, 0);
7596 set1 = isl_set_from_pw_aff(pwaff);
7597 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
7598 set2 = isl_set_read_from_str(ctx, str);
7599 equal = isl_set_is_equal(set1, set2);
7600 isl_set_free(set1);
7601 isl_set_free(set2);
7602 if (equal < 0)
7603 return -1;
7604 if (!equal)
7605 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7607 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
7608 "0 <= i < N and 0 <= j < M }";
7609 map = isl_map_read_from_str(ctx, str);
7610 set = isl_map_range(map);
7612 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
7613 set1 = isl_set_from_pw_aff(pwaff);
7614 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
7615 set2 = isl_set_read_from_str(ctx, str);
7616 equal = isl_set_is_equal(set1, set2);
7617 isl_set_free(set1);
7618 isl_set_free(set2);
7620 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
7621 set1 = isl_set_from_pw_aff(pwaff);
7622 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
7623 set2 = isl_set_read_from_str(ctx, str);
7624 if (equal >= 0 && equal)
7625 equal = isl_set_is_equal(set1, set2);
7626 isl_set_free(set1);
7627 isl_set_free(set2);
7629 isl_set_free(set);
7631 if (equal < 0)
7632 return -1;
7633 if (!equal)
7634 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7636 /* Check that solutions are properly merged. */
7637 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
7638 "c <= -1 + n - 4a - 2b and c >= -2b and "
7639 "4a >= -4 + n and c >= 0 }";
7640 set = isl_set_read_from_str(ctx, str);
7641 pwaff = isl_set_dim_min(set, 2);
7642 set1 = isl_set_from_pw_aff(pwaff);
7643 str = "[n] -> { [(0)] : n >= 1 }";
7644 set2 = isl_set_read_from_str(ctx, str);
7645 equal = isl_set_is_equal(set1, set2);
7646 isl_set_free(set1);
7647 isl_set_free(set2);
7649 if (equal < 0)
7650 return -1;
7651 if (!equal)
7652 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7654 /* Check that empty solution lie in the right space. */
7655 str = "[n] -> { [t,a] : 1 = 0 }";
7656 set = isl_set_read_from_str(ctx, str);
7657 pwaff = isl_set_dim_max(set, 0);
7658 set1 = isl_set_from_pw_aff(pwaff);
7659 str = "[n] -> { [t] : 1 = 0 }";
7660 set2 = isl_set_read_from_str(ctx, str);
7661 equal = isl_set_is_equal(set1, set2);
7662 isl_set_free(set1);
7663 isl_set_free(set2);
7665 if (equal < 0)
7666 return -1;
7667 if (!equal)
7668 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7670 return 0;
7673 /* Basic test for isl_pw_multi_aff_product.
7675 * Check that multiple pieces are properly handled.
7677 static int test_product_pma(isl_ctx *ctx)
7679 isl_stat equal;
7680 const char *str;
7681 isl_pw_multi_aff *pma1, *pma2;
7683 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
7684 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
7685 str = "{ C[] -> D[] }";
7686 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
7687 pma1 = isl_pw_multi_aff_product(pma1, pma2);
7688 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
7689 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
7690 equal = pw_multi_aff_check_plain_equal(pma1, str);
7691 isl_pw_multi_aff_free(pma1);
7692 if (equal < 0)
7693 return -1;
7695 return 0;
7698 int test_product(isl_ctx *ctx)
7700 const char *str;
7701 isl_set *set;
7702 isl_union_set *uset1, *uset2;
7703 int ok;
7705 str = "{ A[i] }";
7706 set = isl_set_read_from_str(ctx, str);
7707 set = isl_set_product(set, isl_set_copy(set));
7708 ok = isl_set_is_wrapping(set);
7709 isl_set_free(set);
7710 if (ok < 0)
7711 return -1;
7712 if (!ok)
7713 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7715 str = "{ [] }";
7716 uset1 = isl_union_set_read_from_str(ctx, str);
7717 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
7718 str = "{ [[] -> []] }";
7719 uset2 = isl_union_set_read_from_str(ctx, str);
7720 ok = isl_union_set_is_equal(uset1, uset2);
7721 isl_union_set_free(uset1);
7722 isl_union_set_free(uset2);
7723 if (ok < 0)
7724 return -1;
7725 if (!ok)
7726 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7728 if (test_product_pma(ctx) < 0)
7729 return -1;
7731 return 0;
7734 /* Check that two sets are not considered disjoint just because
7735 * they have a different set of (named) parameters.
7737 static int test_disjoint(isl_ctx *ctx)
7739 const char *str;
7740 isl_set *set, *set2;
7741 int disjoint;
7743 str = "[n] -> { [[]->[]] }";
7744 set = isl_set_read_from_str(ctx, str);
7745 str = "{ [[]->[]] }";
7746 set2 = isl_set_read_from_str(ctx, str);
7747 disjoint = isl_set_is_disjoint(set, set2);
7748 isl_set_free(set);
7749 isl_set_free(set2);
7750 if (disjoint < 0)
7751 return -1;
7752 if (disjoint)
7753 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7755 return 0;
7758 /* Inputs for isl_pw_multi_aff_is_equal tests.
7759 * "f1" and "f2" are the two function that need to be compared.
7760 * "equal" is the expected result.
7762 struct {
7763 int equal;
7764 const char *f1;
7765 const char *f2;
7766 } pma_equal_tests[] = {
7767 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
7768 "[N] -> { [0] : 0 <= N <= 1 }" },
7769 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
7770 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
7771 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
7772 "[N] -> { [0] : 0 <= N <= 1 }" },
7773 { 0, "{ [NaN] }", "{ [NaN] }" },
7776 int test_equal(isl_ctx *ctx)
7778 int i;
7779 const char *str;
7780 isl_set *set, *set2;
7781 int equal;
7783 str = "{ S_6[i] }";
7784 set = isl_set_read_from_str(ctx, str);
7785 str = "{ S_7[i] }";
7786 set2 = isl_set_read_from_str(ctx, str);
7787 equal = isl_set_is_equal(set, set2);
7788 isl_set_free(set);
7789 isl_set_free(set2);
7790 if (equal < 0)
7791 return -1;
7792 if (equal)
7793 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7795 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
7796 int expected = pma_equal_tests[i].equal;
7797 isl_pw_multi_aff *f1, *f2;
7799 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
7800 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
7801 equal = isl_pw_multi_aff_is_equal(f1, f2);
7802 isl_pw_multi_aff_free(f1);
7803 isl_pw_multi_aff_free(f2);
7804 if (equal < 0)
7805 return -1;
7806 if (equal != expected)
7807 isl_die(ctx, isl_error_unknown,
7808 "unexpected equality result", return -1);
7811 return 0;
7814 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
7815 enum isl_dim_type type, unsigned pos, int fixed)
7817 isl_bool test;
7819 test = isl_map_plain_is_fixed(map, type, pos, NULL);
7820 isl_map_free(map);
7821 if (test < 0)
7822 return -1;
7823 if (test == fixed)
7824 return 0;
7825 if (fixed)
7826 isl_die(ctx, isl_error_unknown,
7827 "map not detected as fixed", return -1);
7828 else
7829 isl_die(ctx, isl_error_unknown,
7830 "map detected as fixed", return -1);
7833 int test_fixed(isl_ctx *ctx)
7835 const char *str;
7836 isl_map *map;
7838 str = "{ [i] -> [i] }";
7839 map = isl_map_read_from_str(ctx, str);
7840 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
7841 return -1;
7842 str = "{ [i] -> [1] }";
7843 map = isl_map_read_from_str(ctx, str);
7844 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7845 return -1;
7846 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
7847 map = isl_map_read_from_str(ctx, str);
7848 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7849 return -1;
7850 map = isl_map_read_from_str(ctx, str);
7851 map = isl_map_neg(map);
7852 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7853 return -1;
7855 return 0;
7858 struct isl_vertices_test_data {
7859 const char *set;
7860 int n;
7861 const char *vertex[6];
7862 } vertices_tests[] = {
7863 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
7864 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
7865 { "{ A[t, i] : t = 14 and i = 1 }",
7866 1, { "{ A[14, 1] }" } },
7867 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
7868 "c <= m and m <= n and m > 0 }",
7869 6, {
7870 "[n, m] -> { [n, m, m] : 0 < m <= n }",
7871 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
7872 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
7873 "[n, m] -> { [m, m, m] : 0 < m <= n }",
7874 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
7875 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
7876 } },
7877 /* An input with implicit equality constraints among the parameters. */
7878 { "[N, M] -> { [a, b] : M >= 3 and 9 + 3M <= a <= 29 + 2N + 11M and "
7879 "2b >= M + a and 5 - 2N - M + a <= 2b <= 3 + a and "
7880 "3b >= 15 + a }",
7881 2, {
7882 "[N, M] -> { [(21), (12)] : M = 3 and N >= 0 }",
7883 "[N, M] -> { [(61 + 2N), (32 + N)] : M = 3 and N >= 0 }",
7888 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
7890 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
7892 struct isl_vertices_test_data *data = user;
7893 isl_ctx *ctx;
7894 isl_multi_aff *ma;
7895 isl_basic_set *bset;
7896 isl_pw_multi_aff *pma;
7897 int i;
7898 isl_bool equal;
7900 ctx = isl_vertex_get_ctx(vertex);
7901 bset = isl_vertex_get_domain(vertex);
7902 ma = isl_vertex_get_expr(vertex);
7903 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
7905 for (i = 0; i < data->n; ++i) {
7906 isl_pw_multi_aff *pma_i;
7908 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
7909 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
7910 isl_pw_multi_aff_free(pma_i);
7912 if (equal < 0 || equal)
7913 break;
7916 isl_pw_multi_aff_free(pma);
7917 isl_vertex_free(vertex);
7919 if (equal < 0)
7920 return isl_stat_error;
7922 return equal ? isl_stat_ok : isl_stat_error;
7925 int test_vertices(isl_ctx *ctx)
7927 int i;
7929 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
7930 isl_basic_set *bset;
7931 isl_vertices *vertices;
7932 int ok = 1;
7933 isl_size n;
7935 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
7936 vertices = isl_basic_set_compute_vertices(bset);
7937 n = isl_vertices_get_n_vertices(vertices);
7938 if (vertices_tests[i].n != n)
7939 ok = 0;
7940 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
7941 &vertices_tests[i]) < 0)
7942 ok = 0;
7943 isl_vertices_free(vertices);
7944 isl_basic_set_free(bset);
7946 if (n < 0)
7947 return -1;
7948 if (!ok)
7949 isl_die(ctx, isl_error_unknown, "unexpected vertices",
7950 return -1);
7953 return 0;
7956 /* Inputs for basic tests of unary operations on isl_union_map.
7957 * "fn" is the function that is being tested.
7958 * "arg" is a string description of the input.
7959 * "res" is a string description of the expected result.
7961 static struct {
7962 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap);
7963 const char *arg;
7964 const char *res;
7965 } umap_un_tests[] = {
7966 { &isl_union_map_range_reverse,
7967 "{ A[] -> [B[] -> C[]]; A[] -> B[]; A[0] -> N[B[1] -> B[2]] }",
7968 "{ A[] -> [C[] -> B[]]; A[0] -> N[B[2] -> B[1]] }" },
7969 { &isl_union_map_range_reverse,
7970 "{ A[] -> N[B[] -> C[]] }",
7971 "{ A[] -> [C[] -> B[]] }" },
7972 { &isl_union_map_range_reverse,
7973 "{ A[] -> N[B[x] -> B[y]] }",
7974 "{ A[] -> N[B[*] -> B[*]] }" },
7977 /* Perform basic tests of unary operations on isl_union_map.
7979 static isl_stat test_un_union_map(isl_ctx *ctx)
7981 int i;
7983 for (i = 0; i < ARRAY_SIZE(umap_un_tests); ++i) {
7984 const char *str;
7985 isl_union_map *umap, *res;
7986 isl_bool equal;
7988 str = umap_un_tests[i].arg;
7989 umap = isl_union_map_read_from_str(ctx, str);
7990 str = umap_un_tests[i].res;
7991 res = isl_union_map_read_from_str(ctx, str);
7992 umap = umap_un_tests[i].fn(umap);
7993 equal = isl_union_map_is_equal(umap, res);
7994 isl_union_map_free(umap);
7995 isl_union_map_free(res);
7996 if (equal < 0)
7997 return isl_stat_error;
7998 if (!equal)
7999 isl_die(ctx, isl_error_unknown,
8000 "unexpected result", return isl_stat_error);
8003 return isl_stat_ok;
8006 /* Inputs for basic tests of binary operations on isl_union_map.
8007 * "fn" is the function that is being tested.
8008 * "arg1" and "arg2" are string descriptions of the inputs.
8009 * "res" is a string description of the expected result.
8011 static struct {
8012 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap1,
8013 __isl_take isl_union_map *umap2);
8014 const char *arg1;
8015 const char *arg2;
8016 const char *res;
8017 } umap_bin_tests[] = {
8018 { &isl_union_map_intersect,
8019 "[n] -> { A[i] -> [] : 0 <= i <= n; B[] -> [] }",
8020 "[m] -> { A[i] -> [] : 0 <= i <= m; C[] -> [] }",
8021 "[m, n] -> { A[i] -> [] : 0 <= i <= n and i <= m }" },
8022 { &isl_union_map_intersect_domain_factor_domain,
8023 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8024 "[N] -> { B[i] -> C[N] }",
8025 "{ }" },
8026 { &isl_union_map_intersect_domain_factor_domain,
8027 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8028 "[N] -> { A[i] -> C[N] }",
8029 "[N] -> { [A[N - 2] -> B[N - 1]] -> C[N] }" },
8030 { &isl_union_map_intersect_domain_factor_domain,
8031 "{ T[A[i] -> B[i + 1]] -> C[i + 2] }",
8032 "[N] -> { A[i] -> C[N] }",
8033 "[N] -> { T[A[N - 2] -> B[N - 1]] -> C[N] }" },
8034 { &isl_union_map_intersect_domain_factor_range,
8035 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8036 "[N] -> { B[i] -> C[N] }",
8037 "[N] -> { [A[N - 2] -> B[N - 1]] -> C[N] }" },
8038 { &isl_union_map_intersect_domain_factor_range,
8039 "{ T[A[i] -> B[i + 1]] -> C[i + 2] }",
8040 "[N] -> { B[i] -> C[N] }",
8041 "[N] -> { T[A[N - 2] -> B[N - 1]] -> C[N] }" },
8042 { &isl_union_map_intersect_domain_factor_range,
8043 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8044 "[N] -> { A[i] -> C[N] }",
8045 "{ }" },
8046 { &isl_union_map_intersect_range_factor_domain,
8047 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8048 "[N] -> { A[i] -> B[N] }",
8049 "[N] -> { A[N - 1] -> [B[N] -> C[N + 1]] }" },
8050 { &isl_union_map_intersect_range_factor_domain,
8051 "{ A[i] -> T[B[i + 1] -> C[i + 2]] }",
8052 "[N] -> { A[i] -> B[N] }",
8053 "[N] -> { A[N - 1] -> T[B[N] -> C[N + 1]] }" },
8054 { &isl_union_map_intersect_range_factor_domain,
8055 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8056 "[N] -> { A[i] -> C[N] }",
8057 "{ }" },
8058 { &isl_union_map_intersect_range_factor_range,
8059 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8060 "[N] -> { A[i] -> C[N] }",
8061 "[N] -> { A[N - 2] -> [B[N - 1] -> C[N]] }" },
8062 { &isl_union_map_intersect_range_factor_range,
8063 "{ A[i] -> T[B[i + 1] -> C[i + 2]] }",
8064 "[N] -> { A[i] -> C[N] }",
8065 "[N] -> { A[N - 2] -> T[B[N - 1] -> C[N]] }" },
8066 { &isl_union_map_intersect_range_factor_range,
8067 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8068 "[N] -> { A[i] -> B[N] }",
8069 "{ }" },
8072 /* Perform basic tests of binary operations on isl_union_map.
8074 static isl_stat test_bin_union_map(isl_ctx *ctx)
8076 int i;
8078 for (i = 0; i < ARRAY_SIZE(umap_bin_tests); ++i) {
8079 const char *str;
8080 isl_union_map *umap1, *umap2, *res;
8081 isl_bool equal;
8083 str = umap_bin_tests[i].arg1;
8084 umap1 = isl_union_map_read_from_str(ctx, str);
8085 str = umap_bin_tests[i].arg2;
8086 umap2 = isl_union_map_read_from_str(ctx, str);
8087 str = umap_bin_tests[i].res;
8088 res = isl_union_map_read_from_str(ctx, str);
8089 umap1 = umap_bin_tests[i].fn(umap1, umap2);
8090 equal = isl_union_map_is_equal(umap1, res);
8091 isl_union_map_free(umap1);
8092 isl_union_map_free(res);
8093 if (equal < 0)
8094 return isl_stat_error;
8095 if (!equal)
8096 isl_die(ctx, isl_error_unknown,
8097 "unexpected result", return isl_stat_error);
8100 return isl_stat_ok;
8103 /* Check that isl_union_set_contains finds space independently
8104 * of the parameters.
8106 static isl_stat test_union_set_contains(isl_ctx *ctx)
8108 const char *str;
8109 isl_bool ok;
8110 isl_space *space;
8111 isl_id *id;
8112 isl_union_set *uset;
8114 str = "[N] -> { A[0:N]; B[*,*] }";
8115 uset = isl_union_set_read_from_str(ctx, str);
8116 space = isl_space_unit(ctx);
8117 id = isl_id_alloc(ctx, "A", NULL);
8118 space = isl_space_add_named_tuple_id_ui(space, id, 1);
8119 ok = isl_union_set_contains(uset, space);
8120 isl_space_free(space);
8121 isl_union_set_free(uset);
8123 if (ok < 0)
8124 return isl_stat_error;
8125 if (!ok)
8126 isl_die(ctx, isl_error_unknown,
8127 "unexpected result", return isl_stat_error);
8129 return isl_stat_ok;
8132 /* Perform basic tests of operations on isl_union_map or isl_union_set.
8134 static int test_union_map(isl_ctx *ctx)
8136 if (test_un_union_map(ctx) < 0)
8137 return -1;
8138 if (test_bin_union_map(ctx) < 0)
8139 return -1;
8140 if (test_union_set_contains(ctx) < 0)
8141 return -1;
8142 return 0;
8145 int test_union_pw(isl_ctx *ctx)
8147 int equal;
8148 const char *str;
8149 isl_union_set *uset;
8150 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
8152 str = "{ [x] -> x^2 }";
8153 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
8154 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
8155 uset = isl_union_pw_qpolynomial_domain(upwqp1);
8156 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
8157 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
8158 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
8159 isl_union_pw_qpolynomial_free(upwqp1);
8160 isl_union_pw_qpolynomial_free(upwqp2);
8161 if (equal < 0)
8162 return -1;
8163 if (!equal)
8164 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
8166 return 0;
8169 /* Inputs for basic tests of functions that select
8170 * subparts of the domain of an isl_multi_union_pw_aff.
8171 * "fn" is the function that is tested.
8172 * "arg" is a string description of the input.
8173 * "res" is a string description of the expected result.
8175 struct {
8176 __isl_give isl_union_set *(*fn)(
8177 __isl_take isl_multi_union_pw_aff *mupa);
8178 const char *arg;
8179 const char *res;
8180 } un_locus_tests[] = {
8181 { &isl_multi_union_pw_aff_zero_union_set,
8182 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
8183 "{ A[0,j]; B[0,j] }" },
8184 { &isl_multi_union_pw_aff_zero_union_set,
8185 "F[{ A[i,j] -> [i-j]; B[i,j] -> [i-j] : i >= 0 }]",
8186 "{ A[i,i]; B[i,i] : i >= 0 }" },
8187 { &isl_multi_union_pw_aff_zero_union_set,
8188 "(F[] : { A[i,j]; B[i,i] : i >= 0 })",
8189 "{ A[i,j]; B[i,i] : i >= 0 }" },
8192 /* Perform some basic tests of functions that select
8193 * subparts of the domain of an isl_multi_union_pw_aff.
8195 static int test_un_locus(isl_ctx *ctx)
8197 int i;
8198 isl_bool ok;
8199 isl_union_set *uset, *res;
8200 isl_multi_union_pw_aff *mupa;
8202 for (i = 0; i < ARRAY_SIZE(un_locus_tests); ++i) {
8203 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
8204 un_locus_tests[i].arg);
8205 res = isl_union_set_read_from_str(ctx, un_locus_tests[i].res);
8206 uset = un_locus_tests[i].fn(mupa);
8207 ok = isl_union_set_is_equal(uset, res);
8208 isl_union_set_free(uset);
8209 isl_union_set_free(res);
8210 if (ok < 0)
8211 return -1;
8212 if (!ok)
8213 isl_die(ctx, isl_error_unknown,
8214 "unexpected result", return -1);
8217 return 0;
8220 /* Inputs for basic tests of functions that select
8221 * subparts of an isl_union_map based on a relation
8222 * specified by an isl_multi_union_pw_aff.
8223 * "fn" is the function that is tested.
8224 * "arg1" and "arg2" are string descriptions of the inputs.
8225 * "res" is a string description of the expected result.
8227 struct {
8228 __isl_give isl_union_map *(*fn)(
8229 __isl_take isl_union_map *umap,
8230 __isl_take isl_multi_union_pw_aff *mupa);
8231 const char *arg1;
8232 const char *arg2;
8233 const char *res;
8234 } bin_locus_tests[] = {
8235 { &isl_union_map_eq_at_multi_union_pw_aff,
8236 "{ A[i,j] -> B[i',j'] }",
8237 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
8238 "{ A[i,j] -> B[i,j'] }" },
8239 { &isl_union_map_eq_at_multi_union_pw_aff,
8240 "{ A[i,j] -> B[i',j'] }",
8241 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8242 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8243 "{ A[i,j] -> B[i,j] }" },
8244 { &isl_union_map_eq_at_multi_union_pw_aff,
8245 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
8246 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
8247 "{ A[i,j] -> B[i,j'] }" },
8248 { &isl_union_map_eq_at_multi_union_pw_aff,
8249 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
8250 "F[{ A[i,j] -> [i]; B[i,j] -> [i]; C[i,j] -> [0] }]",
8251 "{ A[i,j] -> B[i,j']; A[0,j] -> C[i',j'] }" },
8252 { &isl_union_map_eq_at_multi_union_pw_aff,
8253 "{ A[i,j] -> B[i',j'] }",
8254 "F[{ A[i,j] -> [i] : i > j; B[i,j] -> [i] }]",
8255 "{ A[i,j] -> B[i,j'] : i > j }" },
8256 { &isl_union_map_lex_le_at_multi_union_pw_aff,
8257 "{ A[i,j] -> B[i',j'] }",
8258 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8259 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8260 "{ A[i,j] -> B[i',j'] : i,j <<= i',j' }" },
8261 { &isl_union_map_lex_lt_at_multi_union_pw_aff,
8262 "{ A[i,j] -> B[i',j'] }",
8263 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8264 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8265 "{ A[i,j] -> B[i',j'] : i,j << i',j' }" },
8266 { &isl_union_map_lex_ge_at_multi_union_pw_aff,
8267 "{ A[i,j] -> B[i',j'] }",
8268 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8269 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8270 "{ A[i,j] -> B[i',j'] : i,j >>= i',j' }" },
8271 { &isl_union_map_lex_gt_at_multi_union_pw_aff,
8272 "{ A[i,j] -> B[i',j'] }",
8273 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8274 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8275 "{ A[i,j] -> B[i',j'] : i,j >> i',j' }" },
8276 { &isl_union_map_eq_at_multi_union_pw_aff,
8277 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
8278 "(F[] : { A[i,j]; B[i,j] })",
8279 "{ A[i,j] -> B[i',j'] }" },
8280 { &isl_union_map_eq_at_multi_union_pw_aff,
8281 "{ A[i,j] -> B[i',j'] }",
8282 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
8283 "{ A[i,j] -> B[i',j'] : i > j and i' < j' }" },
8284 { &isl_union_map_eq_at_multi_union_pw_aff,
8285 "[N] -> { A[i,j] -> B[i',j'] : i,i' <= N }",
8286 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
8287 "[N] -> { A[i,j] -> B[i',j'] : i > j and i' < j' and i,i' <= N }" },
8288 { &isl_union_map_eq_at_multi_union_pw_aff,
8289 "{ A[i,j] -> B[i',j'] }",
8290 "[N] -> (F[] : { A[i,j] : i < N; B[i,j] : i < N })",
8291 "[N] -> { A[i,j] -> B[i',j'] : i,i' < N }" },
8292 { &isl_union_map_eq_at_multi_union_pw_aff,
8293 "{ A[i,j] -> B[i',j'] }",
8294 "[N] -> (F[] : { : N >= 0 })",
8295 "[N] -> { A[i,j] -> B[i',j'] : N >= 0 }" },
8298 /* Perform some basic tests of functions that select
8299 * subparts of an isl_union_map based on a relation
8300 * specified by an isl_multi_union_pw_aff.
8302 static int test_bin_locus(isl_ctx *ctx)
8304 int i;
8305 isl_bool ok;
8306 isl_union_map *umap, *res;
8307 isl_multi_union_pw_aff *mupa;
8309 for (i = 0; i < ARRAY_SIZE(bin_locus_tests); ++i) {
8310 umap = isl_union_map_read_from_str(ctx,
8311 bin_locus_tests[i].arg1);
8312 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
8313 bin_locus_tests[i].arg2);
8314 res = isl_union_map_read_from_str(ctx, bin_locus_tests[i].res);
8315 umap = bin_locus_tests[i].fn(umap, mupa);
8316 ok = isl_union_map_is_equal(umap, res);
8317 isl_union_map_free(umap);
8318 isl_union_map_free(res);
8319 if (ok < 0)
8320 return -1;
8321 if (!ok)
8322 isl_die(ctx, isl_error_unknown,
8323 "unexpected result", return -1);
8326 return 0;
8329 /* Inputs for basic tests of functions that determine
8330 * the part of the domain where two isl_multi_aff objects
8331 * related to each other in a specific way.
8332 * "fn" is the function that is being tested.
8333 * "arg1" and "arg2" are string descriptions of the inputs.
8334 * "res" is a string description of the expected result.
8336 static struct {
8337 __isl_give isl_set *(*fn)(__isl_take isl_multi_aff *ma1,
8338 __isl_take isl_multi_aff *ma2);
8339 const char *arg1;
8340 const char *arg2;
8341 const char *res;
8342 } bin_locus_ma_tests[] = {
8343 { &isl_multi_aff_lex_le_set, "{ [] }", "{ [] }", "{ : }" },
8344 { &isl_multi_aff_lex_lt_set, "{ [] }", "{ [] }", "{ : false }" },
8345 { &isl_multi_aff_lex_le_set,
8346 "{ A[i] -> [i] }", "{ A[i] -> [0] }",
8347 "{ A[i] : i <= 0 }" },
8348 { &isl_multi_aff_lex_lt_set,
8349 "{ A[i] -> [i] }", "{ A[i] -> [0] }",
8350 "{ A[i] : i < 0 }" },
8351 { &isl_multi_aff_lex_le_set,
8352 "{ A[i] -> [i, i] }", "{ A[i] -> [0, 0] }",
8353 "{ A[i] : i <= 0 }" },
8354 { &isl_multi_aff_lex_le_set,
8355 "{ A[i] -> [i, 0] }", "{ A[i] -> [0, 0] }",
8356 "{ A[i] : i <= 0 }" },
8357 { &isl_multi_aff_lex_le_set,
8358 "{ A[i] -> [i, 1] }", "{ A[i] -> [0, 0] }",
8359 "{ A[i] : i < 0 }" },
8362 /* Perform some basic tests of functions that determine
8363 * the part of the domain where two isl_multi_aff objects
8364 * related to each other in a specific way.
8366 static isl_stat test_bin_locus_ma(isl_ctx *ctx)
8368 int i;
8370 for (i = 0; i < ARRAY_SIZE(bin_locus_ma_tests); ++i) {
8371 const char *str;
8372 isl_bool ok;
8373 isl_multi_aff *ma1, *ma2;
8374 isl_set *set, *res;
8376 str = bin_locus_ma_tests[i].arg1;
8377 ma1 = isl_multi_aff_read_from_str(ctx, str);
8378 str = bin_locus_ma_tests[i].arg2;
8379 ma2 = isl_multi_aff_read_from_str(ctx, str);
8380 res = isl_set_read_from_str(ctx, bin_locus_ma_tests[i].res);
8381 set = bin_locus_ma_tests[i].fn(ma1, ma2);
8382 ok = isl_set_is_equal(set, res);
8383 isl_set_free(set);
8384 isl_set_free(res);
8385 if (ok < 0)
8386 return isl_stat_error;
8387 if (!ok)
8388 isl_die(ctx, isl_error_unknown,
8389 "unexpected result", return isl_stat_error);
8392 return isl_stat_ok;
8395 /* Perform basic locus tests.
8397 static int test_locus(isl_ctx *ctx)
8399 if (test_un_locus(ctx) < 0)
8400 return -1;
8401 if (test_bin_locus(ctx) < 0)
8402 return -1;
8403 if (test_bin_locus_ma(ctx) < 0)
8404 return -1;
8405 return 0;
8408 /* Test that isl_union_pw_qpolynomial_eval picks up the function
8409 * defined over the correct domain space.
8411 static int test_eval_1(isl_ctx *ctx)
8413 const char *str;
8414 isl_point *pnt;
8415 isl_set *set;
8416 isl_union_pw_qpolynomial *upwqp;
8417 isl_val *v;
8418 int cmp;
8420 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
8421 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
8422 str = "{ A[6] }";
8423 set = isl_set_read_from_str(ctx, str);
8424 pnt = isl_set_sample_point(set);
8425 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
8426 cmp = isl_val_cmp_si(v, 36);
8427 isl_val_free(v);
8429 if (!v)
8430 return -1;
8431 if (cmp != 0)
8432 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
8434 return 0;
8437 /* Check that isl_qpolynomial_eval handles getting called on a void point.
8439 static int test_eval_2(isl_ctx *ctx)
8441 const char *str;
8442 isl_point *pnt;
8443 isl_set *set;
8444 isl_qpolynomial *qp;
8445 isl_val *v;
8446 isl_bool ok;
8448 str = "{ A[x] -> [x] }";
8449 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
8450 str = "{ A[x] : false }";
8451 set = isl_set_read_from_str(ctx, str);
8452 pnt = isl_set_sample_point(set);
8453 v = isl_qpolynomial_eval(qp, pnt);
8454 ok = isl_val_is_nan(v);
8455 isl_val_free(v);
8457 if (ok < 0)
8458 return -1;
8459 if (!ok)
8460 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
8462 return 0;
8465 /* Check that a polynomial (without local variables) can be evaluated
8466 * in a rational point.
8468 static isl_stat test_eval_3(isl_ctx *ctx)
8470 isl_pw_qpolynomial *pwqp;
8471 isl_point *pnt;
8472 isl_val *v;
8473 isl_stat r;
8475 pwqp = isl_pw_qpolynomial_read_from_str(ctx, "{ [x] -> x^2 }");
8476 pnt = isl_point_zero(isl_pw_qpolynomial_get_domain_space(pwqp));
8477 v = isl_val_read_from_str(ctx, "1/2");
8478 pnt = isl_point_set_coordinate_val(pnt, isl_dim_set, 0, v);
8479 v = isl_pw_qpolynomial_eval(pwqp, pnt);
8480 r = val_check_equal(v, "1/4");
8481 isl_val_free(v);
8483 return r;
8486 /* Inputs for isl_pw_aff_eval test.
8487 * "f" is the affine function.
8488 * "p" is the point where the function should be evaluated.
8489 * "res" is the expected result.
8491 struct {
8492 const char *f;
8493 const char *p;
8494 const char *res;
8495 } aff_eval_tests[] = {
8496 { "{ [i] -> [2 * i] }", "{ [4] }", "8" },
8497 { "{ [i] -> [2 * i] }", "{ [x] : false }", "NaN" },
8498 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [0] }", "0" },
8499 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [1] }", "1" },
8500 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [2] }", "3" },
8501 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [3] }", "5" },
8502 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [4] }", "7" },
8503 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [0] }", "0" },
8504 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [1] }", "0" },
8505 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [2] }", "0" },
8506 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [3] }", "0" },
8507 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [4] }", "1" },
8508 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [6] }", "1" },
8509 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [8] }", "2" },
8510 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [4] }", "4" },
8511 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [-2] }", "2" },
8512 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [0] }", "NaN" },
8513 { "[N] -> { [2 * N] }", "[N] -> { : N = 4 }", "8" },
8514 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 1] }", "1" },
8515 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 2] }", "3/2" },
8516 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [4] }", "4" },
8517 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [3] }", "NaN" },
8518 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [x] : false }", "NaN" },
8521 /* Perform basic isl_pw_aff_eval tests.
8523 static int test_eval_aff(isl_ctx *ctx)
8525 int i;
8527 for (i = 0; i < ARRAY_SIZE(aff_eval_tests); ++i) {
8528 isl_stat r;
8529 isl_pw_aff *pa;
8530 isl_set *set;
8531 isl_point *pnt;
8532 isl_val *v;
8534 pa = isl_pw_aff_read_from_str(ctx, aff_eval_tests[i].f);
8535 set = isl_set_read_from_str(ctx, aff_eval_tests[i].p);
8536 pnt = isl_set_sample_point(set);
8537 v = isl_pw_aff_eval(pa, pnt);
8538 r = val_check_equal(v, aff_eval_tests[i].res);
8539 isl_val_free(v);
8540 if (r < 0)
8541 return -1;
8543 return 0;
8546 /* Perform basic evaluation tests.
8548 static int test_eval(isl_ctx *ctx)
8550 if (test_eval_1(ctx) < 0)
8551 return -1;
8552 if (test_eval_2(ctx) < 0)
8553 return -1;
8554 if (test_eval_3(ctx) < 0)
8555 return -1;
8556 if (test_eval_aff(ctx) < 0)
8557 return -1;
8558 return 0;
8561 /* Descriptions of sets that are tested for reparsing after printing.
8563 const char *output_tests[] = {
8564 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
8565 "{ [x] : 1 = 0 }",
8566 "{ [x] : false }",
8567 "{ [x] : x mod 2 = 0 }",
8568 "{ [x] : x mod 2 = 1 }",
8569 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
8570 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
8571 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
8572 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
8573 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
8574 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
8577 /* Check that printing a set and reparsing a set from the printed output
8578 * results in the same set.
8580 static int test_output_set(isl_ctx *ctx)
8582 int i;
8583 char *str;
8584 isl_set *set1, *set2;
8585 isl_bool equal;
8587 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
8588 set1 = isl_set_read_from_str(ctx, output_tests[i]);
8589 str = isl_set_to_str(set1);
8590 set2 = isl_set_read_from_str(ctx, str);
8591 free(str);
8592 equal = isl_set_is_equal(set1, set2);
8593 isl_set_free(set1);
8594 isl_set_free(set2);
8595 if (equal < 0)
8596 return -1;
8597 if (!equal)
8598 isl_die(ctx, isl_error_unknown,
8599 "parsed output not the same", return -1);
8602 return 0;
8605 /* Check that an isl_multi_aff is printed using a consistent space.
8607 static isl_stat test_output_ma(isl_ctx *ctx)
8609 char *str;
8610 isl_bool equal;
8611 isl_aff *aff;
8612 isl_multi_aff *ma, *ma2;
8614 ma = isl_multi_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
8615 aff = isl_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
8616 ma = isl_multi_aff_set_aff(ma, 0, aff);
8617 str = isl_multi_aff_to_str(ma);
8618 ma2 = isl_multi_aff_read_from_str(ctx, str);
8619 free(str);
8620 equal = isl_multi_aff_plain_is_equal(ma, ma2);
8621 isl_multi_aff_free(ma2);
8622 isl_multi_aff_free(ma);
8624 if (equal < 0)
8625 return isl_stat_error;
8626 if (!equal)
8627 isl_die(ctx, isl_error_unknown, "bad conversion",
8628 return isl_stat_error);
8630 return isl_stat_ok;
8633 /* Check that an isl_multi_pw_aff is printed using a consistent space.
8635 static isl_stat test_output_mpa(isl_ctx *ctx)
8637 char *str;
8638 isl_bool equal;
8639 isl_pw_aff *pa;
8640 isl_multi_pw_aff *mpa, *mpa2;
8642 mpa = isl_multi_pw_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
8643 pa = isl_pw_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
8644 mpa = isl_multi_pw_aff_set_pw_aff(mpa, 0, pa);
8645 str = isl_multi_pw_aff_to_str(mpa);
8646 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
8647 free(str);
8648 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
8649 isl_multi_pw_aff_free(mpa2);
8650 isl_multi_pw_aff_free(mpa);
8652 if (equal < 0)
8653 return isl_stat_error;
8654 if (!equal)
8655 isl_die(ctx, isl_error_unknown, "bad conversion",
8656 return isl_stat_error);
8658 return isl_stat_ok;
8661 int test_output(isl_ctx *ctx)
8663 char *s;
8664 const char *str;
8665 isl_pw_aff *pa;
8666 isl_printer *p;
8667 int equal;
8669 if (test_output_set(ctx) < 0)
8670 return -1;
8671 if (test_output_ma(ctx) < 0)
8672 return -1;
8673 if (test_output_mpa(ctx) < 0)
8674 return -1;
8676 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
8677 pa = isl_pw_aff_read_from_str(ctx, str);
8679 p = isl_printer_to_str(ctx);
8680 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
8681 p = isl_printer_print_pw_aff(p, pa);
8682 s = isl_printer_get_str(p);
8683 isl_printer_free(p);
8684 isl_pw_aff_free(pa);
8685 if (!s)
8686 equal = -1;
8687 else
8688 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
8689 free(s);
8690 if (equal < 0)
8691 return -1;
8692 if (!equal)
8693 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
8695 return 0;
8698 int test_sample(isl_ctx *ctx)
8700 const char *str;
8701 isl_basic_set *bset1, *bset2;
8702 int empty, subset;
8704 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
8705 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
8706 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
8707 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
8708 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
8709 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
8710 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
8711 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
8712 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
8713 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
8714 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
8715 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
8716 "d - 1073741821e and "
8717 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
8718 "3j >= 1 - a + b + 2e and "
8719 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
8720 "3i <= 4 - a + 4b - e and "
8721 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
8722 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
8723 "c <= -1 + a and 3i >= -2 - a + 3e and "
8724 "1073741822e <= 1073741823 - a + 1073741822b + c and "
8725 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
8726 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
8727 "1073741823e >= 1 + 1073741823b - d and "
8728 "3i >= 1073741823b + c - 1073741823e - f and "
8729 "3i >= 1 + 2b + e + 3g }";
8730 bset1 = isl_basic_set_read_from_str(ctx, str);
8731 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
8732 empty = isl_basic_set_is_empty(bset2);
8733 subset = isl_basic_set_is_subset(bset2, bset1);
8734 isl_basic_set_free(bset1);
8735 isl_basic_set_free(bset2);
8736 if (empty < 0 || subset < 0)
8737 return -1;
8738 if (empty)
8739 isl_die(ctx, isl_error_unknown, "point not found", return -1);
8740 if (!subset)
8741 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
8743 return 0;
8746 /* Perform a projection on a basic set that is known to be empty
8747 * but that has not been assigned a canonical representation.
8748 * Earlier versions of isl would run into a stack overflow
8749 * on this example.
8751 static int test_empty_projection(isl_ctx *ctx)
8753 const char *str;
8754 isl_bool empty;
8755 isl_basic_set *bset;
8757 str = "{ [a, b, c, d, e, f, g, h] : 5f = 1 + 4a - b + 5c - d - 2e and "
8758 "3h = 2 + b + c and 14c >= 9 - 3a + 25b and "
8759 "4c <= 50 - 3a + 23b and 6b <= -39 + a and "
8760 "9g >= -6 + 3a + b + c and e < a + b - 2d and "
8761 "7d >= -5 + 2a + 2b and 5g >= -14 + a - 4b + d + 2e and "
8762 "9g <= -28 - 5b - 2c + 3d + 6e }";
8763 bset = isl_basic_set_read_from_str(ctx, str);
8764 empty = isl_basic_set_is_empty(bset);
8765 bset = isl_basic_set_params(bset);
8766 isl_basic_set_free(bset);
8768 if (empty < 0)
8769 return -1;
8771 return 0;
8774 int test_fixed_power(isl_ctx *ctx)
8776 const char *str;
8777 isl_map *map;
8778 isl_val *exp;
8779 int equal;
8781 str = "{ [i] -> [i + 1] }";
8782 map = isl_map_read_from_str(ctx, str);
8783 exp = isl_val_int_from_si(ctx, 23);
8784 map = isl_map_fixed_power_val(map, exp);
8785 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
8786 isl_map_free(map);
8787 if (equal < 0)
8788 return -1;
8790 return 0;
8793 int test_slice(isl_ctx *ctx)
8795 const char *str;
8796 isl_map *map;
8797 int equal;
8799 str = "{ [i] -> [j] }";
8800 map = isl_map_read_from_str(ctx, str);
8801 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
8802 equal = map_check_equal(map, "{ [i] -> [i] }");
8803 isl_map_free(map);
8804 if (equal < 0)
8805 return -1;
8807 str = "{ [i] -> [j] }";
8808 map = isl_map_read_from_str(ctx, str);
8809 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
8810 equal = map_check_equal(map, "{ [i] -> [j] }");
8811 isl_map_free(map);
8812 if (equal < 0)
8813 return -1;
8815 str = "{ [i] -> [j] }";
8816 map = isl_map_read_from_str(ctx, str);
8817 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
8818 equal = map_check_equal(map, "{ [i] -> [-i] }");
8819 isl_map_free(map);
8820 if (equal < 0)
8821 return -1;
8823 str = "{ [i] -> [j] }";
8824 map = isl_map_read_from_str(ctx, str);
8825 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
8826 equal = map_check_equal(map, "{ [0] -> [j] }");
8827 isl_map_free(map);
8828 if (equal < 0)
8829 return -1;
8831 str = "{ [i] -> [j] }";
8832 map = isl_map_read_from_str(ctx, str);
8833 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
8834 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
8835 isl_map_free(map);
8836 if (equal < 0)
8837 return -1;
8839 str = "{ [i] -> [j] }";
8840 map = isl_map_read_from_str(ctx, str);
8841 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
8842 equal = map_check_equal(map, "{ [i] -> [j] : false }");
8843 isl_map_free(map);
8844 if (equal < 0)
8845 return -1;
8847 return 0;
8850 int test_eliminate(isl_ctx *ctx)
8852 const char *str;
8853 isl_map *map;
8854 int equal;
8856 str = "{ [i] -> [j] : i = 2j }";
8857 map = isl_map_read_from_str(ctx, str);
8858 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
8859 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
8860 isl_map_free(map);
8861 if (equal < 0)
8862 return -1;
8864 return 0;
8867 /* Check basic functionality of isl_map_deltas_map.
8869 static int test_deltas_map(isl_ctx *ctx)
8871 const char *str;
8872 isl_map *map;
8873 int equal;
8875 str = "{ A[i] -> A[i + 1] }";
8876 map = isl_map_read_from_str(ctx, str);
8877 map = isl_map_deltas_map(map);
8878 equal = map_check_equal(map, "{ [A[i] -> A[i + 1]] -> A[1] }");
8879 isl_map_free(map);
8880 if (equal < 0)
8881 return -1;
8883 return 0;
8886 /* Check that isl_set_dim_residue_class detects that the values of j
8887 * in the set below are all odd and that it does not detect any spurious
8888 * strides.
8890 static int test_residue_class(isl_ctx *ctx)
8892 const char *str;
8893 isl_set *set;
8894 isl_int m, r;
8895 isl_stat res;
8897 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
8898 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
8899 set = isl_set_read_from_str(ctx, str);
8900 isl_int_init(m);
8901 isl_int_init(r);
8902 res = isl_set_dim_residue_class(set, 1, &m, &r);
8903 if (res >= 0 &&
8904 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
8905 isl_die(ctx, isl_error_unknown, "incorrect residue class",
8906 res = isl_stat_error);
8907 isl_int_clear(r);
8908 isl_int_clear(m);
8909 isl_set_free(set);
8911 return res;
8914 static int test_align_parameters_1(isl_ctx *ctx)
8916 const char *str;
8917 isl_space *space;
8918 isl_multi_aff *ma1, *ma2;
8919 int equal;
8921 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
8922 ma1 = isl_multi_aff_read_from_str(ctx, str);
8924 space = isl_space_params_alloc(ctx, 1);
8925 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
8926 ma1 = isl_multi_aff_align_params(ma1, space);
8928 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
8929 ma2 = isl_multi_aff_read_from_str(ctx, str);
8931 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
8933 isl_multi_aff_free(ma1);
8934 isl_multi_aff_free(ma2);
8936 if (equal < 0)
8937 return -1;
8938 if (!equal)
8939 isl_die(ctx, isl_error_unknown,
8940 "result not as expected", return -1);
8942 return 0;
8945 /* Check the isl_multi_*_from_*_list operation in case inputs
8946 * have unaligned parameters.
8947 * In particular, older versions of isl would simply fail
8948 * (without printing any error message).
8950 static isl_stat test_align_parameters_2(isl_ctx *ctx)
8952 isl_space *space;
8953 isl_map *map;
8954 isl_aff *aff;
8955 isl_multi_aff *ma;
8957 map = isl_map_read_from_str(ctx, "{ A[] -> M[x] }");
8958 space = isl_map_get_space(map);
8959 isl_map_free(map);
8961 aff = isl_aff_read_from_str(ctx, "[N] -> { A[] -> [N] }");
8962 ma = isl_multi_aff_from_aff_list(space, isl_aff_list_from_aff(aff));
8963 isl_multi_aff_free(ma);
8965 if (!ma)
8966 return isl_stat_error;
8967 return isl_stat_ok;
8970 /* Perform basic parameter alignment tests.
8972 static int test_align_parameters(isl_ctx *ctx)
8974 if (test_align_parameters_1(ctx) < 0)
8975 return -1;
8976 if (test_align_parameters_2(ctx) < 0)
8977 return -1;
8979 return 0;
8982 /* Check that isl_*_drop_unused_params actually drops the unused parameters
8983 * by comparing the result using isl_*_plain_is_equal.
8984 * Note that this assumes that isl_*_plain_is_equal does not consider
8985 * objects that only differ by unused parameters to be equal.
8987 int test_drop_unused_parameters(isl_ctx *ctx)
8989 const char *str_with, *str_without;
8990 isl_basic_set *bset1, *bset2;
8991 isl_set *set1, *set2;
8992 isl_pw_aff *pwa1, *pwa2;
8993 int equal;
8995 str_with = "[n, m, o] -> { [m] }";
8996 str_without = "[m] -> { [m] }";
8998 bset1 = isl_basic_set_read_from_str(ctx, str_with);
8999 bset2 = isl_basic_set_read_from_str(ctx, str_without);
9000 bset1 = isl_basic_set_drop_unused_params(bset1);
9001 equal = isl_basic_set_plain_is_equal(bset1, bset2);
9002 isl_basic_set_free(bset1);
9003 isl_basic_set_free(bset2);
9005 if (equal < 0)
9006 return -1;
9007 if (!equal)
9008 isl_die(ctx, isl_error_unknown,
9009 "result not as expected", return -1);
9011 set1 = isl_set_read_from_str(ctx, str_with);
9012 set2 = isl_set_read_from_str(ctx, str_without);
9013 set1 = isl_set_drop_unused_params(set1);
9014 equal = isl_set_plain_is_equal(set1, set2);
9015 isl_set_free(set1);
9016 isl_set_free(set2);
9018 if (equal < 0)
9019 return -1;
9020 if (!equal)
9021 isl_die(ctx, isl_error_unknown,
9022 "result not as expected", return -1);
9024 pwa1 = isl_pw_aff_read_from_str(ctx, str_with);
9025 pwa2 = isl_pw_aff_read_from_str(ctx, str_without);
9026 pwa1 = isl_pw_aff_drop_unused_params(pwa1);
9027 equal = isl_pw_aff_plain_is_equal(pwa1, pwa2);
9028 isl_pw_aff_free(pwa1);
9029 isl_pw_aff_free(pwa2);
9031 if (equal < 0)
9032 return -1;
9033 if (!equal)
9034 isl_die(ctx, isl_error_unknown,
9035 "result not as expected", return -1);
9037 return 0;
9040 static int test_list(isl_ctx *ctx)
9042 isl_id *a, *b, *c, *d, *id;
9043 isl_id_list *list;
9044 isl_size n;
9045 int ok;
9047 a = isl_id_alloc(ctx, "a", NULL);
9048 b = isl_id_alloc(ctx, "b", NULL);
9049 c = isl_id_alloc(ctx, "c", NULL);
9050 d = isl_id_alloc(ctx, "d", NULL);
9052 list = isl_id_list_alloc(ctx, 4);
9053 list = isl_id_list_add(list, b);
9054 list = isl_id_list_insert(list, 0, a);
9055 list = isl_id_list_add(list, c);
9056 list = isl_id_list_add(list, d);
9057 list = isl_id_list_drop(list, 1, 1);
9059 n = isl_id_list_n_id(list);
9060 if (n < 0)
9061 return -1;
9062 if (n != 3) {
9063 isl_id_list_free(list);
9064 isl_die(ctx, isl_error_unknown,
9065 "unexpected number of elements in list", return -1);
9068 id = isl_id_list_get_id(list, 0);
9069 ok = id == a;
9070 isl_id_free(id);
9071 id = isl_id_list_get_id(list, 1);
9072 ok = ok && id == c;
9073 isl_id_free(id);
9074 id = isl_id_list_get_id(list, 2);
9075 ok = ok && id == d;
9076 isl_id_free(id);
9078 isl_id_list_free(list);
9080 if (!ok)
9081 isl_die(ctx, isl_error_unknown,
9082 "unexpected elements in list", return -1);
9084 return 0;
9087 /* Check the conversion from an isl_multi_aff to an isl_basic_set.
9089 static isl_stat test_ma_conversion(isl_ctx *ctx)
9091 const char *str;
9092 isl_bool equal;
9093 isl_multi_aff *ma;
9094 isl_basic_set *bset1, *bset2;
9096 str = "[N] -> { A[0, N + 1] }";
9097 ma = isl_multi_aff_read_from_str(ctx, str);
9098 bset1 = isl_basic_set_read_from_str(ctx, str);
9099 bset2 = isl_basic_set_from_multi_aff(ma);
9100 equal = isl_basic_set_is_equal(bset1, bset2);
9101 isl_basic_set_free(bset1);
9102 isl_basic_set_free(bset2);
9103 if (equal < 0)
9104 return isl_stat_error;
9105 if (!equal)
9106 isl_die(ctx, isl_error_unknown, "bad conversion",
9107 return isl_stat_error);
9108 return isl_stat_ok;
9111 const char *set_conversion_tests[] = {
9112 "[N] -> { [i] : N - 1 <= 2 i <= N }",
9113 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
9114 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
9115 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
9116 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
9117 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
9118 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
9119 "-3 + c <= d <= 28 + c) }",
9122 /* Check that converting from isl_set to isl_pw_multi_aff and back
9123 * to isl_set produces the original isl_set.
9125 static int test_set_conversion(isl_ctx *ctx)
9127 int i;
9128 const char *str;
9129 isl_set *set1, *set2;
9130 isl_pw_multi_aff *pma;
9131 int equal;
9133 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
9134 str = set_conversion_tests[i];
9135 set1 = isl_set_read_from_str(ctx, str);
9136 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
9137 set2 = isl_set_from_pw_multi_aff(pma);
9138 equal = isl_set_is_equal(set1, set2);
9139 isl_set_free(set1);
9140 isl_set_free(set2);
9142 if (equal < 0)
9143 return -1;
9144 if (!equal)
9145 isl_die(ctx, isl_error_unknown, "bad conversion",
9146 return -1);
9149 return 0;
9152 const char *conversion_tests[] = {
9153 "{ [a, b, c, d] -> s0[a, b, e, f] : "
9154 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
9155 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
9156 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
9157 "9e <= -2 - 2a) }",
9158 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
9159 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
9160 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
9163 /* Check that converting from isl_map to isl_pw_multi_aff and back
9164 * to isl_map produces the original isl_map.
9166 static int test_map_conversion(isl_ctx *ctx)
9168 int i;
9169 isl_map *map1, *map2;
9170 isl_pw_multi_aff *pma;
9171 int equal;
9173 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
9174 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
9175 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
9176 map2 = isl_map_from_pw_multi_aff(pma);
9177 equal = isl_map_is_equal(map1, map2);
9178 isl_map_free(map1);
9179 isl_map_free(map2);
9181 if (equal < 0)
9182 return -1;
9183 if (!equal)
9184 isl_die(ctx, isl_error_unknown, "bad conversion",
9185 return -1);
9188 return 0;
9191 /* Descriptions of isl_pw_multi_aff objects for testing conversion
9192 * to isl_multi_pw_aff and back.
9194 const char *mpa_conversion_tests[] = {
9195 "{ [x] -> A[x] }",
9196 "{ [x] -> A[x] : x >= 0 }",
9197 "{ [x] -> A[x] : x >= 0; [x] -> A[-x] : x < 0 }",
9198 "{ [x] -> A[x, x + 1] }",
9199 "{ [x] -> A[] }",
9200 "{ [x] -> A[] : x >= 0 }",
9203 /* Check that conversion from isl_pw_multi_aff to isl_multi_pw_aff and
9204 * back to isl_pw_multi_aff preserves the original meaning.
9206 static int test_mpa_conversion(isl_ctx *ctx)
9208 int i;
9209 isl_pw_multi_aff *pma1, *pma2;
9210 isl_multi_pw_aff *mpa;
9211 int equal;
9213 for (i = 0; i < ARRAY_SIZE(mpa_conversion_tests); ++i) {
9214 const char *str;
9215 str = mpa_conversion_tests[i];
9216 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
9217 pma2 = isl_pw_multi_aff_copy(pma1);
9218 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma1);
9219 pma1 = isl_pw_multi_aff_from_multi_pw_aff(mpa);
9220 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
9221 isl_pw_multi_aff_free(pma1);
9222 isl_pw_multi_aff_free(pma2);
9224 if (equal < 0)
9225 return -1;
9226 if (!equal)
9227 isl_die(ctx, isl_error_unknown, "bad conversion",
9228 return -1);
9231 return 0;
9234 /* Descriptions of union maps that should be convertible
9235 * to an isl_multi_union_pw_aff.
9237 const char *umap_mupa_conversion_tests[] = {
9238 "{ [a, b, c, d] -> s0[a, b, e, f] : "
9239 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
9240 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
9241 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
9242 "9e <= -2 - 2a) }",
9243 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
9244 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
9245 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
9246 "{ A[] -> B[0]; C[] -> B[1] }",
9247 "{ A[] -> B[]; C[] -> B[] }",
9250 /* Check that converting from isl_union_map to isl_multi_union_pw_aff and back
9251 * to isl_union_map produces the original isl_union_map.
9253 static int test_union_map_mupa_conversion(isl_ctx *ctx)
9255 int i;
9256 isl_union_map *umap1, *umap2;
9257 isl_multi_union_pw_aff *mupa;
9258 int equal;
9260 for (i = 0; i < ARRAY_SIZE(umap_mupa_conversion_tests); ++i) {
9261 const char *str;
9262 str = umap_mupa_conversion_tests[i];
9263 umap1 = isl_union_map_read_from_str(ctx, str);
9264 umap2 = isl_union_map_copy(umap1);
9265 mupa = isl_multi_union_pw_aff_from_union_map(umap2);
9266 umap2 = isl_union_map_from_multi_union_pw_aff(mupa);
9267 equal = isl_union_map_is_equal(umap1, umap2);
9268 isl_union_map_free(umap1);
9269 isl_union_map_free(umap2);
9271 if (equal < 0)
9272 return -1;
9273 if (!equal)
9274 isl_die(ctx, isl_error_unknown, "bad conversion",
9275 return -1);
9278 return 0;
9281 static int test_conversion(isl_ctx *ctx)
9283 if (test_ma_conversion(ctx) < 0)
9284 return -1;
9285 if (test_set_conversion(ctx) < 0)
9286 return -1;
9287 if (test_map_conversion(ctx) < 0)
9288 return -1;
9289 if (test_mpa_conversion(ctx) < 0)
9290 return -1;
9291 if (test_union_map_mupa_conversion(ctx) < 0)
9292 return -1;
9293 return 0;
9296 /* Check that isl_basic_map_curry does not modify input.
9298 static int test_curry(isl_ctx *ctx)
9300 const char *str;
9301 isl_basic_map *bmap1, *bmap2;
9302 int equal;
9304 str = "{ [A[] -> B[]] -> C[] }";
9305 bmap1 = isl_basic_map_read_from_str(ctx, str);
9306 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
9307 equal = isl_basic_map_is_equal(bmap1, bmap2);
9308 isl_basic_map_free(bmap1);
9309 isl_basic_map_free(bmap2);
9311 if (equal < 0)
9312 return -1;
9313 if (equal)
9314 isl_die(ctx, isl_error_unknown,
9315 "curried map should not be equal to original",
9316 return -1);
9318 return 0;
9321 struct {
9322 const char *set;
9323 const char *ma;
9324 const char *res;
9325 } preimage_tests[] = {
9326 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
9327 "{ A[j,i] -> B[i,j] }",
9328 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
9329 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
9330 "{ A[a,b] -> B[a/2,b/6] }",
9331 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
9332 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
9333 "{ A[a,b] -> B[a/2,b/6] }",
9334 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
9335 "exists i,j : a = 2 i and b = 6 j }" },
9336 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
9337 "[n] -> { : 0 <= n <= 100 }" },
9338 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
9339 "{ A[a] -> B[2a] }",
9340 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
9341 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
9342 "{ A[a] -> B[([a/2])] }",
9343 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
9344 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
9345 "{ A[a] -> B[a,a,a/3] }",
9346 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
9347 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
9348 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
9351 static int test_preimage_basic_set(isl_ctx *ctx)
9353 int i;
9354 isl_basic_set *bset1, *bset2;
9355 isl_multi_aff *ma;
9356 int equal;
9358 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
9359 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
9360 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
9361 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
9362 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
9363 equal = isl_basic_set_is_equal(bset1, bset2);
9364 isl_basic_set_free(bset1);
9365 isl_basic_set_free(bset2);
9366 if (equal < 0)
9367 return -1;
9368 if (!equal)
9369 isl_die(ctx, isl_error_unknown, "bad preimage",
9370 return -1);
9373 return 0;
9376 struct {
9377 const char *map;
9378 const char *ma;
9379 const char *res;
9380 } preimage_domain_tests[] = {
9381 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
9382 "{ A[j,i] -> B[i,j] }",
9383 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
9384 { "{ B[i] -> C[i]; D[i] -> E[i] }",
9385 "{ A[i] -> B[i + 1] }",
9386 "{ A[i] -> C[i + 1] }" },
9387 { "{ B[i] -> C[i]; B[i] -> E[i] }",
9388 "{ A[i] -> B[i + 1] }",
9389 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
9390 { "{ B[i] -> C[([i/2])] }",
9391 "{ A[i] -> B[2i] }",
9392 "{ A[i] -> C[i] }" },
9393 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
9394 "{ A[i] -> B[([i/5]), ([i/7])] }",
9395 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
9396 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
9397 "[N] -> { A[] -> B[([N/5])] }",
9398 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
9399 { "{ B[i] -> C[i] : exists a : i = 5 a }",
9400 "{ A[i] -> B[2i] }",
9401 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
9402 { "{ B[i] -> C[i] : exists a : i = 2 a; "
9403 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
9404 "{ A[i] -> B[2i] }",
9405 "{ A[i] -> C[2i] }" },
9406 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
9407 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
9410 static int test_preimage_union_map(isl_ctx *ctx)
9412 int i;
9413 isl_union_map *umap1, *umap2;
9414 isl_multi_aff *ma;
9415 int equal;
9417 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
9418 umap1 = isl_union_map_read_from_str(ctx,
9419 preimage_domain_tests[i].map);
9420 ma = isl_multi_aff_read_from_str(ctx,
9421 preimage_domain_tests[i].ma);
9422 umap2 = isl_union_map_read_from_str(ctx,
9423 preimage_domain_tests[i].res);
9424 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
9425 equal = isl_union_map_is_equal(umap1, umap2);
9426 isl_union_map_free(umap1);
9427 isl_union_map_free(umap2);
9428 if (equal < 0)
9429 return -1;
9430 if (!equal)
9431 isl_die(ctx, isl_error_unknown, "bad preimage",
9432 return -1);
9435 return 0;
9438 static int test_preimage(isl_ctx *ctx)
9440 if (test_preimage_basic_set(ctx) < 0)
9441 return -1;
9442 if (test_preimage_union_map(ctx) < 0)
9443 return -1;
9445 return 0;
9448 struct {
9449 const char *ma1;
9450 const char *ma;
9451 const char *res;
9452 } pullback_tests[] = {
9453 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
9454 "{ A[a,b] -> C[b + 2a] }" },
9455 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
9456 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
9457 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
9458 "{ A[a] -> C[(a)/6] }" },
9459 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
9460 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
9461 "{ A[a] -> C[(2a)/3] }" },
9462 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
9463 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
9464 "{ A[i,j] -> C[i + j, i + j] }"},
9465 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
9466 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
9467 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
9468 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
9469 "{ [i, j] -> [floor((i)/3), j] }",
9470 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
9473 static int test_pullback(isl_ctx *ctx)
9475 int i;
9476 isl_multi_aff *ma1, *ma2;
9477 isl_multi_aff *ma;
9478 int equal;
9480 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
9481 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
9482 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
9483 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
9484 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
9485 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
9486 isl_multi_aff_free(ma1);
9487 isl_multi_aff_free(ma2);
9488 if (equal < 0)
9489 return -1;
9490 if (!equal)
9491 isl_die(ctx, isl_error_unknown, "bad pullback",
9492 return -1);
9495 return 0;
9498 /* Check that negation is printed correctly and that equal expressions
9499 * are correctly identified.
9501 static int test_ast(isl_ctx *ctx)
9503 isl_ast_expr *expr, *expr1, *expr2, *expr3;
9504 char *str;
9505 int ok, equal;
9507 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
9508 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
9509 expr = isl_ast_expr_add(expr1, expr2);
9510 expr2 = isl_ast_expr_copy(expr);
9511 expr = isl_ast_expr_neg(expr);
9512 expr2 = isl_ast_expr_neg(expr2);
9513 equal = isl_ast_expr_is_equal(expr, expr2);
9514 str = isl_ast_expr_to_C_str(expr);
9515 ok = str ? !strcmp(str, "-(A + B)") : -1;
9516 free(str);
9517 isl_ast_expr_free(expr);
9518 isl_ast_expr_free(expr2);
9520 if (ok < 0 || equal < 0)
9521 return -1;
9522 if (!equal)
9523 isl_die(ctx, isl_error_unknown,
9524 "equal expressions not considered equal", return -1);
9525 if (!ok)
9526 isl_die(ctx, isl_error_unknown,
9527 "isl_ast_expr printed incorrectly", return -1);
9529 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
9530 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
9531 expr = isl_ast_expr_add(expr1, expr2);
9532 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
9533 expr = isl_ast_expr_sub(expr3, expr);
9534 str = isl_ast_expr_to_C_str(expr);
9535 ok = str ? !strcmp(str, "C - (A + B)") : -1;
9536 free(str);
9537 isl_ast_expr_free(expr);
9539 if (ok < 0)
9540 return -1;
9541 if (!ok)
9542 isl_die(ctx, isl_error_unknown,
9543 "isl_ast_expr printed incorrectly", return -1);
9545 return 0;
9548 /* Check that isl_ast_build_expr_from_set returns a valid expression
9549 * for an empty set. Note that isl_ast_build_expr_from_set getting
9550 * called on an empty set probably indicates a bug in the caller.
9552 static int test_ast_build(isl_ctx *ctx)
9554 isl_set *set;
9555 isl_ast_build *build;
9556 isl_ast_expr *expr;
9558 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9559 build = isl_ast_build_from_context(set);
9561 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
9562 expr = isl_ast_build_expr_from_set(build, set);
9564 isl_ast_expr_free(expr);
9565 isl_ast_build_free(build);
9567 if (!expr)
9568 return -1;
9570 return 0;
9573 /* Internal data structure for before_for and after_for callbacks.
9575 * depth is the current depth
9576 * before is the number of times before_for has been called
9577 * after is the number of times after_for has been called
9579 struct isl_test_codegen_data {
9580 int depth;
9581 int before;
9582 int after;
9585 /* This function is called before each for loop in the AST generated
9586 * from test_ast_gen1.
9588 * Increment the number of calls and the depth.
9589 * Check that the space returned by isl_ast_build_get_schedule_space
9590 * matches the target space of the schedule returned by
9591 * isl_ast_build_get_schedule.
9592 * Return an isl_id that is checked by the corresponding call
9593 * to after_for.
9595 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
9596 void *user)
9598 struct isl_test_codegen_data *data = user;
9599 isl_ctx *ctx;
9600 isl_space *space;
9601 isl_union_map *schedule;
9602 isl_union_set *uset;
9603 isl_set *set;
9604 isl_bool empty;
9605 isl_size n;
9606 char name[] = "d0";
9608 ctx = isl_ast_build_get_ctx(build);
9610 if (data->before >= 3)
9611 isl_die(ctx, isl_error_unknown,
9612 "unexpected number of for nodes", return NULL);
9613 if (data->depth < 0 || data->depth >= 2)
9614 isl_die(ctx, isl_error_unknown,
9615 "unexpected depth", return NULL);
9617 snprintf(name, sizeof(name), "d%d", data->depth);
9618 data->before++;
9619 data->depth++;
9621 schedule = isl_ast_build_get_schedule(build);
9622 uset = isl_union_map_range(schedule);
9623 n = isl_union_set_n_set(uset);
9624 if (n != 1) {
9625 isl_union_set_free(uset);
9626 if (n < 0)
9627 return NULL;
9628 isl_die(ctx, isl_error_unknown,
9629 "expecting single range space", return NULL);
9632 space = isl_ast_build_get_schedule_space(build);
9633 set = isl_union_set_extract_set(uset, space);
9634 isl_union_set_free(uset);
9635 empty = isl_set_is_empty(set);
9636 isl_set_free(set);
9638 if (empty < 0)
9639 return NULL;
9640 if (empty)
9641 isl_die(ctx, isl_error_unknown,
9642 "spaces don't match", return NULL);
9644 return isl_id_alloc(ctx, name, NULL);
9647 /* This function is called after each for loop in the AST generated
9648 * from test_ast_gen1.
9650 * Increment the number of calls and decrement the depth.
9651 * Check that the annotation attached to the node matches
9652 * the isl_id returned by the corresponding call to before_for.
9654 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
9655 __isl_keep isl_ast_build *build, void *user)
9657 struct isl_test_codegen_data *data = user;
9658 isl_id *id;
9659 const char *name;
9660 int valid;
9662 data->after++;
9663 data->depth--;
9665 if (data->after > data->before)
9666 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
9667 "mismatch in number of for nodes",
9668 return isl_ast_node_free(node));
9670 id = isl_ast_node_get_annotation(node);
9671 if (!id)
9672 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
9673 "missing annotation", return isl_ast_node_free(node));
9675 name = isl_id_get_name(id);
9676 valid = name && atoi(name + 1) == data->depth;
9677 isl_id_free(id);
9679 if (!valid)
9680 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
9681 "wrong annotation", return isl_ast_node_free(node));
9683 return node;
9686 /* Check that the before_each_for and after_each_for callbacks
9687 * are called for each for loop in the generated code,
9688 * that they are called in the right order and that the isl_id
9689 * returned from the before_each_for callback is attached to
9690 * the isl_ast_node passed to the corresponding after_each_for call.
9692 static int test_ast_gen1(isl_ctx *ctx)
9694 const char *str;
9695 isl_set *set;
9696 isl_union_map *schedule;
9697 isl_ast_build *build;
9698 isl_ast_node *tree;
9699 struct isl_test_codegen_data data;
9701 str = "[N] -> { : N >= 10 }";
9702 set = isl_set_read_from_str(ctx, str);
9703 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
9704 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
9705 schedule = isl_union_map_read_from_str(ctx, str);
9707 data.before = 0;
9708 data.after = 0;
9709 data.depth = 0;
9710 build = isl_ast_build_from_context(set);
9711 build = isl_ast_build_set_before_each_for(build,
9712 &before_for, &data);
9713 build = isl_ast_build_set_after_each_for(build,
9714 &after_for, &data);
9715 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9716 isl_ast_build_free(build);
9717 if (!tree)
9718 return -1;
9720 isl_ast_node_free(tree);
9722 if (data.before != 3 || data.after != 3)
9723 isl_die(ctx, isl_error_unknown,
9724 "unexpected number of for nodes", return -1);
9726 return 0;
9729 /* Check that the AST generator handles domains that are integrally disjoint
9730 * but not rationally disjoint.
9732 static int test_ast_gen2(isl_ctx *ctx)
9734 const char *str;
9735 isl_set *set;
9736 isl_union_map *schedule;
9737 isl_union_map *options;
9738 isl_ast_build *build;
9739 isl_ast_node *tree;
9741 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
9742 schedule = isl_union_map_read_from_str(ctx, str);
9743 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9744 build = isl_ast_build_from_context(set);
9746 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
9747 options = isl_union_map_read_from_str(ctx, str);
9748 build = isl_ast_build_set_options(build, options);
9749 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9750 isl_ast_build_free(build);
9751 if (!tree)
9752 return -1;
9753 isl_ast_node_free(tree);
9755 return 0;
9758 /* Increment *user on each call.
9760 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
9761 __isl_keep isl_ast_build *build, void *user)
9763 int *n = user;
9765 (*n)++;
9767 return node;
9770 /* Test that unrolling tries to minimize the number of instances.
9771 * In particular, for the schedule given below, make sure it generates
9772 * 3 nodes (rather than 101).
9774 static int test_ast_gen3(isl_ctx *ctx)
9776 const char *str;
9777 isl_set *set;
9778 isl_union_map *schedule;
9779 isl_union_map *options;
9780 isl_ast_build *build;
9781 isl_ast_node *tree;
9782 int n_domain = 0;
9784 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
9785 schedule = isl_union_map_read_from_str(ctx, str);
9786 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9788 str = "{ [i] -> unroll[0] }";
9789 options = isl_union_map_read_from_str(ctx, str);
9791 build = isl_ast_build_from_context(set);
9792 build = isl_ast_build_set_options(build, options);
9793 build = isl_ast_build_set_at_each_domain(build,
9794 &count_domains, &n_domain);
9795 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9796 isl_ast_build_free(build);
9797 if (!tree)
9798 return -1;
9800 isl_ast_node_free(tree);
9802 if (n_domain != 3)
9803 isl_die(ctx, isl_error_unknown,
9804 "unexpected number of for nodes", return -1);
9806 return 0;
9809 /* Check that if the ast_build_exploit_nested_bounds options is set,
9810 * we do not get an outer if node in the generated AST,
9811 * while we do get such an outer if node if the options is not set.
9813 static int test_ast_gen4(isl_ctx *ctx)
9815 const char *str;
9816 isl_set *set;
9817 isl_union_map *schedule;
9818 isl_ast_build *build;
9819 isl_ast_node *tree;
9820 enum isl_ast_node_type type;
9821 int enb;
9823 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
9824 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
9826 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
9828 schedule = isl_union_map_read_from_str(ctx, str);
9829 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9830 build = isl_ast_build_from_context(set);
9831 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9832 isl_ast_build_free(build);
9833 if (!tree)
9834 return -1;
9836 type = isl_ast_node_get_type(tree);
9837 isl_ast_node_free(tree);
9839 if (type == isl_ast_node_if)
9840 isl_die(ctx, isl_error_unknown,
9841 "not expecting if node", return -1);
9843 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
9845 schedule = isl_union_map_read_from_str(ctx, str);
9846 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9847 build = isl_ast_build_from_context(set);
9848 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9849 isl_ast_build_free(build);
9850 if (!tree)
9851 return -1;
9853 type = isl_ast_node_get_type(tree);
9854 isl_ast_node_free(tree);
9856 if (type != isl_ast_node_if)
9857 isl_die(ctx, isl_error_unknown,
9858 "expecting if node", return -1);
9860 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
9862 return 0;
9865 /* This function is called for each leaf in the AST generated
9866 * from test_ast_gen5.
9868 * We finalize the AST generation by extending the outer schedule
9869 * with a zero-dimensional schedule. If this results in any for loops,
9870 * then this means that we did not pass along enough information
9871 * about the outer schedule to the inner AST generation.
9873 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
9874 void *user)
9876 isl_union_map *schedule, *extra;
9877 isl_ast_node *tree;
9879 schedule = isl_ast_build_get_schedule(build);
9880 extra = isl_union_map_copy(schedule);
9881 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
9882 schedule = isl_union_map_range_product(schedule, extra);
9883 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9884 isl_ast_build_free(build);
9886 if (!tree)
9887 return NULL;
9889 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
9890 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
9891 "code should not contain any for loop",
9892 return isl_ast_node_free(tree));
9894 return tree;
9897 /* Check that we do not lose any information when going back and
9898 * forth between internal and external schedule.
9900 * In particular, we create an AST where we unroll the only
9901 * non-constant dimension in the schedule. We therefore do
9902 * not expect any for loops in the AST. However, older versions
9903 * of isl would not pass along enough information about the outer
9904 * schedule when performing an inner code generation from a create_leaf
9905 * callback, resulting in the inner code generation producing a for loop.
9907 static int test_ast_gen5(isl_ctx *ctx)
9909 const char *str;
9910 isl_set *set;
9911 isl_union_map *schedule, *options;
9912 isl_ast_build *build;
9913 isl_ast_node *tree;
9915 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
9916 schedule = isl_union_map_read_from_str(ctx, str);
9918 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
9919 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
9920 options = isl_union_map_read_from_str(ctx, str);
9922 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9923 build = isl_ast_build_from_context(set);
9924 build = isl_ast_build_set_options(build, options);
9925 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
9926 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9927 isl_ast_build_free(build);
9928 isl_ast_node_free(tree);
9929 if (!tree)
9930 return -1;
9932 return 0;
9935 /* Check that the expression
9937 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
9939 * is not combined into
9941 * min(n/2, 0)
9943 * as this would result in n/2 being evaluated in parts of
9944 * the definition domain where n is not a multiple of 2.
9946 static int test_ast_expr(isl_ctx *ctx)
9948 const char *str;
9949 isl_pw_aff *pa;
9950 isl_ast_build *build;
9951 isl_ast_expr *expr;
9952 int min_max;
9953 int is_min;
9955 min_max = isl_options_get_ast_build_detect_min_max(ctx);
9956 isl_options_set_ast_build_detect_min_max(ctx, 1);
9958 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
9959 pa = isl_pw_aff_read_from_str(ctx, str);
9960 build = isl_ast_build_alloc(ctx);
9961 expr = isl_ast_build_expr_from_pw_aff(build, pa);
9962 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
9963 isl_ast_expr_get_op_type(expr) == isl_ast_expr_op_min;
9964 isl_ast_build_free(build);
9965 isl_ast_expr_free(expr);
9967 isl_options_set_ast_build_detect_min_max(ctx, min_max);
9969 if (!expr)
9970 return -1;
9971 if (is_min)
9972 isl_die(ctx, isl_error_unknown,
9973 "expressions should not be combined", return -1);
9975 return 0;
9978 static int test_ast_gen(isl_ctx *ctx)
9980 if (test_ast_gen1(ctx) < 0)
9981 return -1;
9982 if (test_ast_gen2(ctx) < 0)
9983 return -1;
9984 if (test_ast_gen3(ctx) < 0)
9985 return -1;
9986 if (test_ast_gen4(ctx) < 0)
9987 return -1;
9988 if (test_ast_gen5(ctx) < 0)
9989 return -1;
9990 if (test_ast_expr(ctx) < 0)
9991 return -1;
9992 return 0;
9995 /* Check if dropping output dimensions from an isl_pw_multi_aff
9996 * works properly.
9998 static int test_pw_multi_aff(isl_ctx *ctx)
10000 const char *str;
10001 isl_pw_multi_aff *pma1, *pma2;
10002 int equal;
10004 str = "{ [i,j] -> [i+j, 4i-j] }";
10005 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
10006 str = "{ [i,j] -> [4i-j] }";
10007 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
10009 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
10011 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
10013 isl_pw_multi_aff_free(pma1);
10014 isl_pw_multi_aff_free(pma2);
10015 if (equal < 0)
10016 return -1;
10017 if (!equal)
10018 isl_die(ctx, isl_error_unknown,
10019 "expressions not equal", return -1);
10021 return 0;
10024 /* Check that we can properly parse multi piecewise affine expressions
10025 * where the piecewise affine expressions have different domains.
10027 static int test_multi_pw_aff_1(isl_ctx *ctx)
10029 const char *str;
10030 isl_set *dom, *dom2;
10031 isl_multi_pw_aff *mpa1, *mpa2;
10032 isl_pw_aff *pa;
10033 int equal;
10034 int equal_domain;
10036 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
10037 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
10038 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
10039 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
10040 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
10041 str = "{ [i] -> [(i : i > 0), 2i] }";
10042 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
10044 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
10046 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
10047 dom = isl_pw_aff_domain(pa);
10048 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
10049 dom2 = isl_pw_aff_domain(pa);
10050 equal_domain = isl_set_is_equal(dom, dom2);
10052 isl_set_free(dom);
10053 isl_set_free(dom2);
10054 isl_multi_pw_aff_free(mpa1);
10055 isl_multi_pw_aff_free(mpa2);
10057 if (equal < 0)
10058 return -1;
10059 if (!equal)
10060 isl_die(ctx, isl_error_unknown,
10061 "expressions not equal", return -1);
10063 if (equal_domain < 0)
10064 return -1;
10065 if (equal_domain)
10066 isl_die(ctx, isl_error_unknown,
10067 "domains unexpectedly equal", return -1);
10069 return 0;
10072 /* Check that the dimensions in the explicit domain
10073 * of a multi piecewise affine expression are properly
10074 * taken into account.
10076 static int test_multi_pw_aff_2(isl_ctx *ctx)
10078 const char *str;
10079 isl_bool involves1, involves2, involves3, equal;
10080 isl_multi_pw_aff *mpa, *mpa1, *mpa2;
10082 str = "{ A[x,y] -> B[] : x >= y }";
10083 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
10084 involves1 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 2);
10085 mpa1 = isl_multi_pw_aff_copy(mpa);
10087 mpa = isl_multi_pw_aff_insert_dims(mpa, isl_dim_in, 0, 1);
10088 involves2 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 1);
10089 involves3 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 1, 2);
10090 str = "{ [a,x,y] -> B[] : x >= y }";
10091 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
10092 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
10093 isl_multi_pw_aff_free(mpa2);
10095 mpa = isl_multi_pw_aff_drop_dims(mpa, isl_dim_in, 0, 1);
10096 mpa = isl_multi_pw_aff_set_tuple_name(mpa, isl_dim_in, "A");
10097 if (equal >= 0 && equal)
10098 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa1);
10099 isl_multi_pw_aff_free(mpa1);
10100 isl_multi_pw_aff_free(mpa);
10102 if (involves1 < 0 || involves2 < 0 || involves3 < 0 || equal < 0)
10103 return -1;
10104 if (!equal)
10105 isl_die(ctx, isl_error_unknown,
10106 "incorrect result of dimension insertion/removal",
10107 return isl_stat_error);
10108 if (!involves1 || involves2 || !involves3)
10109 isl_die(ctx, isl_error_unknown,
10110 "incorrect characterization of involved dimensions",
10111 return isl_stat_error);
10113 return 0;
10116 /* Check that isl_multi_union_pw_aff_multi_val_on_domain
10117 * sets the explicit domain of a zero-dimensional result,
10118 * such that it can be converted to an isl_union_map.
10120 static isl_stat test_multi_pw_aff_3(isl_ctx *ctx)
10122 isl_space *space;
10123 isl_union_set *dom;
10124 isl_multi_val *mv;
10125 isl_multi_union_pw_aff *mupa;
10126 isl_union_map *umap;
10128 dom = isl_union_set_read_from_str(ctx, "{ A[]; B[] }");
10129 space = isl_union_set_get_space(dom);
10130 mv = isl_multi_val_zero(isl_space_set_from_params(space));
10131 mupa = isl_multi_union_pw_aff_multi_val_on_domain(dom, mv);
10132 umap = isl_union_map_from_multi_union_pw_aff(mupa);
10133 isl_union_map_free(umap);
10134 if (!umap)
10135 return isl_stat_error;
10137 return isl_stat_ok;
10140 /* String descriptions of boxes that
10141 * are used for reconstructing box maps from their lower and upper bounds.
10143 static const char *multi_pw_aff_box_tests[] = {
10144 "{ A[x, y] -> [] : x + y >= 0 }",
10145 "[N] -> { A[x, y] -> [x] : x + y <= N }",
10146 "[N] -> { A[x, y] -> [x : y] : x + y <= N }",
10149 /* Check that map representations of boxes can be reconstructed
10150 * from their lower and upper bounds.
10152 static isl_stat test_multi_pw_aff_box(isl_ctx *ctx)
10154 int i;
10156 for (i = 0; i < ARRAY_SIZE(multi_pw_aff_box_tests); ++i) {
10157 const char *str;
10158 isl_bool equal;
10159 isl_map *map, *box;
10160 isl_multi_pw_aff *min, *max;
10162 str = multi_pw_aff_box_tests[i];
10163 map = isl_map_read_from_str(ctx, str);
10164 min = isl_map_min_multi_pw_aff(isl_map_copy(map));
10165 max = isl_map_max_multi_pw_aff(isl_map_copy(map));
10166 box = isl_map_universe(isl_map_get_space(map));
10167 box = isl_map_lower_bound_multi_pw_aff(box, min);
10168 box = isl_map_upper_bound_multi_pw_aff(box, max);
10169 equal = isl_map_is_equal(map, box);
10170 isl_map_free(map);
10171 isl_map_free(box);
10172 if (equal < 0)
10173 return isl_stat_error;
10174 if (!equal)
10175 isl_die(ctx, isl_error_unknown,
10176 "unexpected result", return isl_stat_error);
10179 return isl_stat_ok;
10182 /* Perform some tests on multi piecewise affine expressions.
10184 static int test_multi_pw_aff(isl_ctx *ctx)
10186 if (test_multi_pw_aff_1(ctx) < 0)
10187 return -1;
10188 if (test_multi_pw_aff_2(ctx) < 0)
10189 return -1;
10190 if (test_multi_pw_aff_3(ctx) < 0)
10191 return -1;
10192 if (test_multi_pw_aff_box(ctx) < 0)
10193 return -1;
10194 return 0;
10197 /* This is a regression test for a bug where isl_basic_map_simplify
10198 * would end up in an infinite loop. In particular, we construct
10199 * an empty basic set that is not obviously empty.
10200 * isl_basic_set_is_empty marks the basic set as empty.
10201 * After projecting out i3, the variable can be dropped completely,
10202 * but isl_basic_map_simplify refrains from doing so if the basic set
10203 * is empty and would end up in an infinite loop if it didn't test
10204 * explicitly for empty basic maps in the outer loop.
10206 static int test_simplify_1(isl_ctx *ctx)
10208 const char *str;
10209 isl_basic_set *bset;
10210 int empty;
10212 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
10213 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
10214 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
10215 "i3 >= i2 }";
10216 bset = isl_basic_set_read_from_str(ctx, str);
10217 empty = isl_basic_set_is_empty(bset);
10218 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
10219 isl_basic_set_free(bset);
10220 if (!bset)
10221 return -1;
10222 if (!empty)
10223 isl_die(ctx, isl_error_unknown,
10224 "basic set should be empty", return -1);
10226 return 0;
10229 /* Check that the equality in the set description below
10230 * is simplified away.
10232 static int test_simplify_2(isl_ctx *ctx)
10234 const char *str;
10235 isl_basic_set *bset;
10236 isl_bool universe;
10238 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
10239 bset = isl_basic_set_read_from_str(ctx, str);
10240 universe = isl_basic_set_plain_is_universe(bset);
10241 isl_basic_set_free(bset);
10243 if (universe < 0)
10244 return -1;
10245 if (!universe)
10246 isl_die(ctx, isl_error_unknown,
10247 "equality not simplified away", return -1);
10248 return 0;
10251 /* Some simplification tests.
10253 static int test_simplify(isl_ctx *ctx)
10255 if (test_simplify_1(ctx) < 0)
10256 return -1;
10257 if (test_simplify_2(ctx) < 0)
10258 return -1;
10259 return 0;
10262 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
10263 * with gbr context would fail to disable the use of the shifted tableau
10264 * when transferring equalities for the input to the context, resulting
10265 * in invalid sample values.
10267 static int test_partial_lexmin(isl_ctx *ctx)
10269 const char *str;
10270 isl_basic_set *bset;
10271 isl_basic_map *bmap;
10272 isl_map *map;
10274 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
10275 bmap = isl_basic_map_read_from_str(ctx, str);
10276 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
10277 bset = isl_basic_set_read_from_str(ctx, str);
10278 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
10279 isl_map_free(map);
10281 if (!map)
10282 return -1;
10284 return 0;
10287 /* Check that the variable compression performed on the existentially
10288 * quantified variables inside isl_basic_set_compute_divs is not confused
10289 * by the implicit equalities among the parameters.
10291 static int test_compute_divs(isl_ctx *ctx)
10293 const char *str;
10294 isl_basic_set *bset;
10295 isl_set *set;
10297 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
10298 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
10299 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
10300 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
10301 bset = isl_basic_set_read_from_str(ctx, str);
10302 set = isl_basic_set_compute_divs(bset);
10303 isl_set_free(set);
10304 if (!set)
10305 return -1;
10307 return 0;
10310 /* Check that isl_schedule_get_map is not confused by a schedule tree
10311 * with divergent filter node parameters, as can result from a call
10312 * to isl_schedule_intersect_domain.
10314 static int test_schedule_tree(isl_ctx *ctx)
10316 const char *str;
10317 isl_union_set *uset;
10318 isl_schedule *sched1, *sched2;
10319 isl_union_map *umap;
10321 uset = isl_union_set_read_from_str(ctx, "{ A[i] }");
10322 sched1 = isl_schedule_from_domain(uset);
10323 uset = isl_union_set_read_from_str(ctx, "{ B[] }");
10324 sched2 = isl_schedule_from_domain(uset);
10326 sched1 = isl_schedule_sequence(sched1, sched2);
10327 str = "[n] -> { A[i] : 0 <= i < n; B[] }";
10328 uset = isl_union_set_read_from_str(ctx, str);
10329 sched1 = isl_schedule_intersect_domain(sched1, uset);
10330 umap = isl_schedule_get_map(sched1);
10331 isl_schedule_free(sched1);
10332 isl_union_map_free(umap);
10333 if (!umap)
10334 return -1;
10336 return 0;
10339 /* Check that a zero-dimensional prefix schedule keeps track
10340 * of the domain and outer filters.
10342 static int test_schedule_tree_prefix(isl_ctx *ctx)
10344 const char *str;
10345 isl_bool equal;
10346 isl_union_set *uset;
10347 isl_union_set_list *filters;
10348 isl_multi_union_pw_aff *mupa, *mupa2;
10349 isl_schedule_node *node;
10351 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
10352 uset = isl_union_set_read_from_str(ctx, str);
10353 node = isl_schedule_node_from_domain(uset);
10354 node = isl_schedule_node_child(node, 0);
10356 str = "{ S1[i,j] : i > j }";
10357 uset = isl_union_set_read_from_str(ctx, str);
10358 filters = isl_union_set_list_from_union_set(uset);
10359 str = "{ S1[i,j] : i <= j; S2[i,j] }";
10360 uset = isl_union_set_read_from_str(ctx, str);
10361 filters = isl_union_set_list_add(filters, uset);
10362 node = isl_schedule_node_insert_sequence(node, filters);
10364 node = isl_schedule_node_child(node, 0);
10365 node = isl_schedule_node_child(node, 0);
10366 mupa = isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
10367 str = "([] : { S1[i,j] : i > j })";
10368 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx, str);
10369 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
10370 isl_multi_union_pw_aff_free(mupa2);
10371 isl_multi_union_pw_aff_free(mupa);
10372 isl_schedule_node_free(node);
10374 if (equal < 0)
10375 return -1;
10376 if (!equal)
10377 isl_die(ctx, isl_error_unknown, "unexpected prefix schedule",
10378 return -1);
10380 return 0;
10383 /* Check that the reaching domain elements and the prefix schedule
10384 * at a leaf node are the same before and after grouping.
10386 static int test_schedule_tree_group_1(isl_ctx *ctx)
10388 int equal;
10389 const char *str;
10390 isl_id *id;
10391 isl_union_set *uset;
10392 isl_multi_union_pw_aff *mupa;
10393 isl_union_pw_multi_aff *upma1, *upma2;
10394 isl_union_set *domain1, *domain2;
10395 isl_union_map *umap1, *umap2;
10396 isl_schedule_node *node;
10398 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
10399 uset = isl_union_set_read_from_str(ctx, str);
10400 node = isl_schedule_node_from_domain(uset);
10401 node = isl_schedule_node_child(node, 0);
10402 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
10403 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10404 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10405 node = isl_schedule_node_child(node, 0);
10406 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
10407 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10408 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10409 node = isl_schedule_node_child(node, 0);
10410 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
10411 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
10412 domain1 = isl_schedule_node_get_domain(node);
10413 id = isl_id_alloc(ctx, "group", NULL);
10414 node = isl_schedule_node_parent(node);
10415 node = isl_schedule_node_group(node, id);
10416 node = isl_schedule_node_child(node, 0);
10417 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
10418 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
10419 domain2 = isl_schedule_node_get_domain(node);
10420 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
10421 if (equal >= 0 && equal)
10422 equal = isl_union_set_is_equal(domain1, domain2);
10423 if (equal >= 0 && equal)
10424 equal = isl_union_map_is_equal(umap1, umap2);
10425 isl_union_map_free(umap1);
10426 isl_union_map_free(umap2);
10427 isl_union_set_free(domain1);
10428 isl_union_set_free(domain2);
10429 isl_union_pw_multi_aff_free(upma1);
10430 isl_union_pw_multi_aff_free(upma2);
10431 isl_schedule_node_free(node);
10433 if (equal < 0)
10434 return -1;
10435 if (!equal)
10436 isl_die(ctx, isl_error_unknown,
10437 "expressions not equal", return -1);
10439 return 0;
10442 /* Check that we can have nested groupings and that the union map
10443 * schedule representation is the same before and after the grouping.
10444 * Note that after the grouping, the union map representation contains
10445 * the domain constraints from the ranges of the expansion nodes,
10446 * while they are missing from the union map representation of
10447 * the tree without expansion nodes.
10449 * Also check that the global expansion is as expected.
10451 static int test_schedule_tree_group_2(isl_ctx *ctx)
10453 int equal, equal_expansion;
10454 const char *str;
10455 isl_id *id;
10456 isl_union_set *uset;
10457 isl_union_map *umap1, *umap2;
10458 isl_union_map *expansion1, *expansion2;
10459 isl_union_set_list *filters;
10460 isl_multi_union_pw_aff *mupa;
10461 isl_schedule *schedule;
10462 isl_schedule_node *node;
10464 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
10465 "S3[i,j] : 0 <= i,j < 10 }";
10466 uset = isl_union_set_read_from_str(ctx, str);
10467 node = isl_schedule_node_from_domain(uset);
10468 node = isl_schedule_node_child(node, 0);
10469 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
10470 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10471 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10472 node = isl_schedule_node_child(node, 0);
10473 str = "{ S1[i,j] }";
10474 uset = isl_union_set_read_from_str(ctx, str);
10475 filters = isl_union_set_list_from_union_set(uset);
10476 str = "{ S2[i,j]; S3[i,j] }";
10477 uset = isl_union_set_read_from_str(ctx, str);
10478 filters = isl_union_set_list_add(filters, uset);
10479 node = isl_schedule_node_insert_sequence(node, filters);
10480 node = isl_schedule_node_child(node, 1);
10481 node = isl_schedule_node_child(node, 0);
10482 str = "{ S2[i,j] }";
10483 uset = isl_union_set_read_from_str(ctx, str);
10484 filters = isl_union_set_list_from_union_set(uset);
10485 str = "{ S3[i,j] }";
10486 uset = isl_union_set_read_from_str(ctx, str);
10487 filters = isl_union_set_list_add(filters, uset);
10488 node = isl_schedule_node_insert_sequence(node, filters);
10490 schedule = isl_schedule_node_get_schedule(node);
10491 umap1 = isl_schedule_get_map(schedule);
10492 uset = isl_schedule_get_domain(schedule);
10493 umap1 = isl_union_map_intersect_domain(umap1, uset);
10494 isl_schedule_free(schedule);
10496 node = isl_schedule_node_parent(node);
10497 node = isl_schedule_node_parent(node);
10498 id = isl_id_alloc(ctx, "group1", NULL);
10499 node = isl_schedule_node_group(node, id);
10500 node = isl_schedule_node_child(node, 1);
10501 node = isl_schedule_node_child(node, 0);
10502 id = isl_id_alloc(ctx, "group2", NULL);
10503 node = isl_schedule_node_group(node, id);
10505 schedule = isl_schedule_node_get_schedule(node);
10506 umap2 = isl_schedule_get_map(schedule);
10507 isl_schedule_free(schedule);
10509 node = isl_schedule_node_root(node);
10510 node = isl_schedule_node_child(node, 0);
10511 expansion1 = isl_schedule_node_get_subtree_expansion(node);
10512 isl_schedule_node_free(node);
10514 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
10515 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
10516 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
10518 expansion2 = isl_union_map_read_from_str(ctx, str);
10520 equal = isl_union_map_is_equal(umap1, umap2);
10521 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
10523 isl_union_map_free(umap1);
10524 isl_union_map_free(umap2);
10525 isl_union_map_free(expansion1);
10526 isl_union_map_free(expansion2);
10528 if (equal < 0 || equal_expansion < 0)
10529 return -1;
10530 if (!equal)
10531 isl_die(ctx, isl_error_unknown,
10532 "expressions not equal", return -1);
10533 if (!equal_expansion)
10534 isl_die(ctx, isl_error_unknown,
10535 "unexpected expansion", return -1);
10537 return 0;
10540 /* Some tests for the isl_schedule_node_group function.
10542 static int test_schedule_tree_group(isl_ctx *ctx)
10544 if (test_schedule_tree_group_1(ctx) < 0)
10545 return -1;
10546 if (test_schedule_tree_group_2(ctx) < 0)
10547 return -1;
10548 return 0;
10551 struct {
10552 const char *set;
10553 const char *dual;
10554 } coef_tests[] = {
10555 { "{ rat: [i] : 0 <= i <= 10 }",
10556 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
10557 { "{ rat: [i] : FALSE }",
10558 "{ rat: coefficients[[cst] -> [a]] }" },
10559 { "{ rat: [i] : }",
10560 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
10561 { "{ [0:,1,2:3] }",
10562 "{ rat: coefficients[[c_cst] -> [a, b, c]] : "
10563 "a >= 0 and 2c >= -c_cst - b and 3c >= -c_cst - b }" },
10564 { "[M, N] -> { [x = (1 - N):-1, -4x:(M - 4x)] }",
10565 "{ rat: coefficients[[c_cst, c_M = 0:, c_N = 0:] -> [a, b = -c_M:]] :"
10566 "4b >= -c_N + a and 4b >= -c_cst - 2c_N + a }" },
10567 { "{ rat : [x, y] : 1 <= 2x <= 9 and 2 <= 3y <= 16 }",
10568 "{ rat: coefficients[[c_cst] -> [c_x, c_y]] : "
10569 "4c_y >= -6c_cst - 3c_x and 4c_y >= -6c_cst - 27c_x and "
10570 "32c_y >= -6c_cst - 3c_x and 32c_y >= -6c_cst - 27c_x }" },
10571 { "{ [x, y, z] : 3y <= 2x - 2 and y >= -2 + 2x and 2y >= 2 - x }",
10572 "{ rat: coefficients[[cst] -> [a, b, c]] }" },
10575 struct {
10576 const char *set;
10577 const char *dual;
10578 } sol_tests[] = {
10579 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
10580 "{ rat: [i] : 0 <= i <= 10 }" },
10581 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
10582 "{ rat: [i] }" },
10583 { "{ rat: coefficients[[cst] -> [a]] }",
10584 "{ rat: [i] : FALSE }" },
10587 /* Test the basic functionality of isl_basic_set_coefficients and
10588 * isl_basic_set_solutions.
10590 static int test_dual(isl_ctx *ctx)
10592 int i;
10594 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
10595 int equal;
10596 isl_basic_set *bset1, *bset2;
10598 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
10599 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
10600 bset1 = isl_basic_set_coefficients(bset1);
10601 equal = isl_basic_set_is_equal(bset1, bset2);
10602 isl_basic_set_free(bset1);
10603 isl_basic_set_free(bset2);
10604 if (equal < 0)
10605 return -1;
10606 if (!equal)
10607 isl_die(ctx, isl_error_unknown,
10608 "incorrect dual", return -1);
10611 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
10612 int equal;
10613 isl_basic_set *bset1, *bset2;
10615 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
10616 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
10617 bset1 = isl_basic_set_solutions(bset1);
10618 equal = isl_basic_set_is_equal(bset1, bset2);
10619 isl_basic_set_free(bset1);
10620 isl_basic_set_free(bset2);
10621 if (equal < 0)
10622 return -1;
10623 if (!equal)
10624 isl_die(ctx, isl_error_unknown,
10625 "incorrect dual", return -1);
10628 return 0;
10631 struct {
10632 int scale_tile;
10633 int shift_point;
10634 const char *domain;
10635 const char *schedule;
10636 const char *sizes;
10637 const char *tile;
10638 const char *point;
10639 } tile_tests[] = {
10640 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
10641 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10642 "{ [32,32] }",
10643 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
10644 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10646 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
10647 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10648 "{ [32,32] }",
10649 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
10650 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10652 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
10653 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10654 "{ [32,32] }",
10655 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
10656 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
10658 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
10659 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10660 "{ [32,32] }",
10661 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
10662 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
10666 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
10667 * tile the band and then check if the tile and point bands have the
10668 * expected partial schedule.
10670 static int test_tile(isl_ctx *ctx)
10672 int i;
10673 int scale;
10674 int shift;
10676 scale = isl_options_get_tile_scale_tile_loops(ctx);
10677 shift = isl_options_get_tile_shift_point_loops(ctx);
10679 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
10680 int opt;
10681 int equal;
10682 const char *str;
10683 isl_union_set *domain;
10684 isl_multi_union_pw_aff *mupa, *mupa2;
10685 isl_schedule_node *node;
10686 isl_multi_val *sizes;
10688 opt = tile_tests[i].scale_tile;
10689 isl_options_set_tile_scale_tile_loops(ctx, opt);
10690 opt = tile_tests[i].shift_point;
10691 isl_options_set_tile_shift_point_loops(ctx, opt);
10693 str = tile_tests[i].domain;
10694 domain = isl_union_set_read_from_str(ctx, str);
10695 node = isl_schedule_node_from_domain(domain);
10696 node = isl_schedule_node_child(node, 0);
10697 str = tile_tests[i].schedule;
10698 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10699 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10700 str = tile_tests[i].sizes;
10701 sizes = isl_multi_val_read_from_str(ctx, str);
10702 node = isl_schedule_node_band_tile(node, sizes);
10704 str = tile_tests[i].tile;
10705 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10706 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
10707 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
10708 isl_multi_union_pw_aff_free(mupa);
10709 isl_multi_union_pw_aff_free(mupa2);
10711 node = isl_schedule_node_child(node, 0);
10713 str = tile_tests[i].point;
10714 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10715 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
10716 if (equal >= 0 && equal)
10717 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
10718 mupa2);
10719 isl_multi_union_pw_aff_free(mupa);
10720 isl_multi_union_pw_aff_free(mupa2);
10722 isl_schedule_node_free(node);
10724 if (equal < 0)
10725 return -1;
10726 if (!equal)
10727 isl_die(ctx, isl_error_unknown,
10728 "unexpected result", return -1);
10731 isl_options_set_tile_scale_tile_loops(ctx, scale);
10732 isl_options_set_tile_shift_point_loops(ctx, shift);
10734 return 0;
10737 /* Check that the domain hash of a space is equal to the hash
10738 * of the domain of the space, both ignoring parameters.
10740 static int test_domain_hash(isl_ctx *ctx)
10742 isl_map *map;
10743 isl_space *space;
10744 uint32_t hash1, hash2;
10746 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
10747 space = isl_map_get_space(map);
10748 isl_map_free(map);
10749 hash1 = isl_space_get_tuple_domain_hash(space);
10750 space = isl_space_domain(space);
10751 hash2 = isl_space_get_tuple_hash(space);
10752 isl_space_free(space);
10754 if (!space)
10755 return -1;
10756 if (hash1 != hash2)
10757 isl_die(ctx, isl_error_unknown,
10758 "domain hash not equal to hash of domain", return -1);
10760 return 0;
10763 /* Check that a universe basic set that is not obviously equal to the universe
10764 * is still recognized as being equal to the universe.
10766 static int test_universe(isl_ctx *ctx)
10768 const char *s;
10769 isl_basic_set *bset;
10770 isl_bool is_univ;
10772 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
10773 bset = isl_basic_set_read_from_str(ctx, s);
10774 is_univ = isl_basic_set_is_universe(bset);
10775 isl_basic_set_free(bset);
10777 if (is_univ < 0)
10778 return -1;
10779 if (!is_univ)
10780 isl_die(ctx, isl_error_unknown,
10781 "not recognized as universe set", return -1);
10783 return 0;
10786 /* Sets for which chambers are computed and checked.
10788 const char *chambers_tests[] = {
10789 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
10790 "z >= 0 and z <= C - y and z <= B - x - y }",
10793 /* Add the domain of "cell" to "cells".
10795 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
10797 isl_basic_set_list **cells = user;
10798 isl_basic_set *dom;
10800 dom = isl_cell_get_domain(cell);
10801 isl_cell_free(cell);
10802 *cells = isl_basic_set_list_add(*cells, dom);
10804 return *cells ? isl_stat_ok : isl_stat_error;
10807 /* Check that the elements of "list" are pairwise disjoint.
10809 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
10811 int i, j;
10812 isl_size n;
10814 n = isl_basic_set_list_n_basic_set(list);
10815 if (n < 0)
10816 return isl_stat_error;
10818 for (i = 0; i < n; ++i) {
10819 isl_basic_set *bset_i;
10821 bset_i = isl_basic_set_list_get_basic_set(list, i);
10822 for (j = i + 1; j < n; ++j) {
10823 isl_basic_set *bset_j;
10824 isl_bool disjoint;
10826 bset_j = isl_basic_set_list_get_basic_set(list, j);
10827 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
10828 isl_basic_set_free(bset_j);
10829 if (!disjoint)
10830 isl_die(isl_basic_set_list_get_ctx(list),
10831 isl_error_unknown, "not disjoint",
10832 break);
10833 if (disjoint < 0 || !disjoint)
10834 break;
10836 isl_basic_set_free(bset_i);
10837 if (j < n)
10838 return isl_stat_error;
10841 return isl_stat_ok;
10844 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
10845 * are pairwise disjoint.
10847 static int test_chambers(isl_ctx *ctx)
10849 int i;
10851 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
10852 isl_basic_set *bset;
10853 isl_vertices *vertices;
10854 isl_basic_set_list *cells;
10855 isl_stat ok;
10857 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
10858 vertices = isl_basic_set_compute_vertices(bset);
10859 cells = isl_basic_set_list_alloc(ctx, 0);
10860 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
10861 &cells) < 0)
10862 cells = isl_basic_set_list_free(cells);
10863 ok = check_pairwise_disjoint(cells);
10864 isl_basic_set_list_free(cells);
10865 isl_vertices_free(vertices);
10866 isl_basic_set_free(bset);
10868 if (ok < 0)
10869 return -1;
10872 return 0;
10875 struct {
10876 const char *name;
10877 int (*fn)(isl_ctx *ctx);
10878 } tests [] = {
10879 { "universe", &test_universe },
10880 { "domain hash", &test_domain_hash },
10881 { "dual", &test_dual },
10882 { "dependence analysis", &test_flow },
10883 { "val", &test_val },
10884 { "compute divs", &test_compute_divs },
10885 { "partial lexmin", &test_partial_lexmin },
10886 { "simplify", &test_simplify },
10887 { "curry", &test_curry },
10888 { "piecewise multi affine expressions", &test_pw_multi_aff },
10889 { "multi piecewise affine expressions", &test_multi_pw_aff },
10890 { "conversion", &test_conversion },
10891 { "list", &test_list },
10892 { "align parameters", &test_align_parameters },
10893 { "drop unused parameters", &test_drop_unused_parameters },
10894 { "preimage", &test_preimage },
10895 { "pullback", &test_pullback },
10896 { "AST", &test_ast },
10897 { "AST build", &test_ast_build },
10898 { "AST generation", &test_ast_gen },
10899 { "eliminate", &test_eliminate },
10900 { "deltas_map", &test_deltas_map },
10901 { "residue class", &test_residue_class },
10902 { "div", &test_div },
10903 { "slice", &test_slice },
10904 { "fixed power", &test_fixed_power },
10905 { "sample", &test_sample },
10906 { "empty projection", &test_empty_projection },
10907 { "output", &test_output },
10908 { "vertices", &test_vertices },
10909 { "chambers", &test_chambers },
10910 { "fixed", &test_fixed },
10911 { "equal", &test_equal },
10912 { "disjoint", &test_disjoint },
10913 { "product", &test_product },
10914 { "dim_max", &test_dim_max },
10915 { "affine", &test_aff },
10916 { "injective", &test_injective },
10917 { "schedule (whole component)", &test_schedule_whole },
10918 { "schedule (incremental)", &test_schedule_incremental },
10919 { "schedule tree", &test_schedule_tree },
10920 { "schedule tree prefix", &test_schedule_tree_prefix },
10921 { "schedule tree grouping", &test_schedule_tree_group },
10922 { "tile", &test_tile },
10923 { "union map", &test_union_map },
10924 { "union_pw", &test_union_pw },
10925 { "locus", &test_locus },
10926 { "eval", &test_eval },
10927 { "parse", &test_parse },
10928 { "single-valued", &test_sv },
10929 { "recession cone", &test_recession_cone },
10930 { "affine hull", &test_affine_hull },
10931 { "simple_hull", &test_simple_hull },
10932 { "box hull", &test_box_hull },
10933 { "coalesce", &test_coalesce },
10934 { "factorize", &test_factorize },
10935 { "subset", &test_subset },
10936 { "subtract", &test_subtract },
10937 { "intersect", &test_intersect },
10938 { "lexmin", &test_lexmin },
10939 { "min", &test_min },
10940 { "set lower bounds", &test_min_mpa },
10941 { "gist", &test_gist },
10942 { "piecewise quasi-polynomials", &test_pwqp },
10943 { "lift", &test_lift },
10944 { "bind parameters", &test_bind },
10945 { "unbind parameters", &test_unbind },
10946 { "bound", &test_bound },
10947 { "get lists", &test_get_list },
10948 { "union", &test_union },
10949 { "split periods", &test_split_periods },
10950 { "lexicographic order", &test_lex },
10951 { "bijectivity", &test_bijective },
10952 { "dataflow analysis", &test_dep },
10953 { "reading", &test_read },
10954 { "bounded", &test_bounded },
10955 { "construction", &test_construction },
10956 { "dimension manipulation", &test_dim },
10957 { "map application", &test_application },
10958 { "convex hull", &test_convex_hull },
10959 { "transitive closure", &test_closure },
10960 { "isl_bool", &test_isl_bool},
10963 int main(int argc, char **argv)
10965 int i;
10966 struct isl_ctx *ctx;
10967 struct isl_options *options;
10969 options = isl_options_new_with_defaults();
10970 assert(options);
10971 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
10973 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
10974 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
10975 printf("%s\n", tests[i].name);
10976 if (tests[i].fn(ctx) < 0)
10977 goto error;
10979 isl_ctx_free(ctx);
10980 return 0;
10981 error:
10982 isl_ctx_free(ctx);
10983 return -1;