properly fix isl_pw_*_gist_params
[isl.git] / isl_test.c
blob9c3817b25a220d7cdd9fd50fcc06895ce0e53f4b
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 /* Check that the union of the basic sets described by "str1" and "str2"
2422 * can be coalesced and that the result is equal to the union.
2423 * The explicit call to isl_basic_set_union prevents the implicit
2424 * equality constraints in the basic maps from being detected prior
2425 * to the call to isl_set_coalesce, at least at the point
2426 * where this function was introduced.
2428 static isl_stat test_coalesce_union(isl_ctx *ctx, const char *str1,
2429 const char *str2)
2431 isl_basic_set *bset1, *bset2;
2432 isl_set *set, *set2;
2433 isl_bool equal;
2435 bset1 = isl_basic_set_read_from_str(ctx, str1);
2436 bset2 = isl_basic_set_read_from_str(ctx, str2);
2437 set = isl_basic_set_union(bset1, bset2);
2438 set = isl_set_coalesce(set);
2440 bset1 = isl_basic_set_read_from_str(ctx, str1);
2441 bset2 = isl_basic_set_read_from_str(ctx, str2);
2442 set2 = isl_basic_set_union(bset1, bset2);
2444 equal = isl_set_is_equal(set, set2);
2445 isl_set_free(set);
2446 isl_set_free(set2);
2448 if (equal < 0)
2449 return isl_stat_error;
2450 if (!equal)
2451 isl_die(ctx, isl_error_unknown,
2452 "coalesced set not equal to input",
2453 return isl_stat_error);
2455 return isl_stat_non_null(set);
2458 /* A specialized coalescing test case that would result in an assertion
2459 * in an earlier version of isl. Use test_coalesce_union with
2460 * an explicit call to isl_basic_set_union to prevent the implicit
2461 * equality constraints in the first basic map from being detected prior
2462 * to the call to isl_set_coalesce, at least at the point
2463 * where this test case was introduced.
2465 static isl_stat test_coalesce_special2(struct isl_ctx *ctx)
2467 const char *str1;
2468 const char *str2;
2470 str1 = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
2471 str2 = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }";
2472 return test_coalesce_union(ctx, str1, str2);
2475 /* Check that calling isl_set_coalesce does not leave other sets
2476 * that may share some information with the input to isl_set_coalesce
2477 * in an inconsistent state.
2478 * In particular, older versions of isl would modify all copies
2479 * of the basic sets in the isl_set_coalesce input in a way
2480 * that could leave them in an inconsistent state.
2481 * The result of printing any other set containing one of these
2482 * basic sets would then result in an invalid set description.
2484 static int test_coalesce_special3(isl_ctx *ctx)
2486 const char *str;
2487 char *s;
2488 isl_set *set1, *set2;
2489 isl_printer *p;
2491 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
2492 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
2493 set2 = isl_set_read_from_str(ctx, str);
2494 set1 = isl_set_union(set1, isl_set_copy(set2));
2495 set1 = isl_set_coalesce(set1);
2496 isl_set_free(set1);
2498 p = isl_printer_to_str(ctx);
2499 p = isl_printer_print_set(p, set2);
2500 isl_set_free(set2);
2501 s = isl_printer_get_str(p);
2502 isl_printer_free(p);
2503 set1 = isl_set_read_from_str(ctx, s);
2504 free(s);
2505 isl_set_free(set1);
2507 if (!set1)
2508 return -1;
2510 return 0;
2513 /* Check that calling isl_set_coalesce on the intersection of
2514 * the sets described by "s1" and "s2" does not leave other sets
2515 * that may share some information with the input to isl_set_coalesce
2516 * in an inconsistent state.
2517 * In particular, when isl_set_coalesce detects equality constraints,
2518 * it does not immediately perform Gaussian elimination on them,
2519 * but then it needs to ensure that it is performed at some point.
2520 * The input set has implicit equality constraints in the first disjunct.
2521 * It is constructed as an intersection, because otherwise
2522 * those equality constraints would already be detected during parsing.
2524 static isl_stat test_coalesce_intersection(isl_ctx *ctx,
2525 const char *s1, const char *s2)
2527 isl_set *set1, *set2;
2529 set1 = isl_set_read_from_str(ctx, s1);
2530 set2 = isl_set_read_from_str(ctx, s2);
2531 set1 = isl_set_intersect(set1, set2);
2532 isl_set_free(isl_set_coalesce(isl_set_copy(set1)));
2533 set1 = isl_set_coalesce(set1);
2534 isl_set_free(set1);
2536 if (!set1)
2537 return isl_stat_error;
2539 return isl_stat_ok;
2542 /* Check that calling isl_set_coalesce does not leave other sets
2543 * that may share some information with the input to isl_set_coalesce
2544 * in an inconsistent state, for the case where one disjunct
2545 * is a subset of the other.
2547 static isl_stat test_coalesce_special4(isl_ctx *ctx)
2549 const char *s1, *s2;
2551 s1 = "{ [a, b] : b <= 0 or a <= 1 }";
2552 s2 = "{ [a, b] : -1 <= -a < b }";
2553 return test_coalesce_intersection(ctx, s1, s2);
2556 /* Check that calling isl_set_coalesce does not leave other sets
2557 * that may share some information with the input to isl_set_coalesce
2558 * in an inconsistent state, for the case where two disjuncts
2559 * can be fused.
2561 static isl_stat test_coalesce_special5(isl_ctx *ctx)
2563 const char *s1, *s2;
2565 s1 = "{ [a, b, c] : b <= 0 }";
2566 s2 = "{ [a, b, c] : -1 <= -a < b and (c >= 0 or c < 0) }";
2567 return test_coalesce_intersection(ctx, s1, s2);
2570 /* Check that calling isl_set_coalesce does not leave other sets
2571 * that may share some information with the input to isl_set_coalesce
2572 * in an inconsistent state, for the case where two disjuncts
2573 * can be fused and where both disjuncts have implicit equality constraints.
2575 static isl_stat test_coalesce_special6(isl_ctx *ctx)
2577 const char *s1, *s2;
2579 s1 = "{ [a, b, c] : c <= 0 }";
2580 s2 = "{ [a, b, c] : 0 <= a <= b <= c or (0 <= b <= c and a > 0) }";
2581 return test_coalesce_intersection(ctx, s1, s2);
2584 /* A specialized coalescing test case that would result in an assertion failure
2585 * in an earlier version of isl. Use test_coalesce_union with
2586 * an explicit call to isl_basic_set_union to prevent the implicit
2587 * equality constraints in the basic maps from being detected prior
2588 * to the call to isl_set_coalesce, at least at the point
2589 * where this test case was introduced.
2591 static isl_stat test_coalesce_special7(isl_ctx *ctx)
2593 const char *str1;
2594 const char *str2;
2596 str1 = "{ [a, b, c=0:17] : a <= 7 and 2b <= 11 - a and "
2597 "c <= -7 + 2a and 2c >= - 3 + 3a - 2b }";
2598 str2 = "{ [a, b, c] : c > -15a and c >= -7 + 2a and c < 0 and "
2599 "3c <= -5 + 5a - 3b and 2b >= 11 - a }";
2600 return test_coalesce_union(ctx, str1, str2);
2603 /* A specialized coalescing test case that would result in a disjunct
2604 * getting dropped in an earlier version of isl. Use test_coalesce_union with
2605 * an explicit call to isl_basic_set_union to prevent the implicit
2606 * equality constraints in the basic maps from being detected prior
2607 * to the call to isl_set_coalesce, at least at the point
2608 * where this test case was introduced.
2610 static isl_stat test_coalesce_special8(isl_ctx *ctx)
2612 const char *str1;
2613 const char *str2;
2615 str1 = "{ [a, b, c] : 2c <= -a and b >= -a and b <= 5 and "
2616 "6c > -7a and 11c >= -5a - b and a <= 3 }";
2617 str2 = "{ [a, b, c] : 6c > -7a and b >= -a and b <= 5 and "
2618 "11c >= -5a - b and a >= 4 and 2b <= a and 2c <= -a }";
2619 return test_coalesce_union(ctx, str1, str2);
2622 /* Test the functionality of isl_set_coalesce.
2623 * That is, check that the output is always equal to the input
2624 * and in some cases that the result consists of a single disjunct.
2626 static int test_coalesce(struct isl_ctx *ctx)
2628 int i;
2630 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2631 const char *str = coalesce_tests[i].str;
2632 int check_one = coalesce_tests[i].single_disjunct;
2633 if (test_coalesce_set(ctx, str, check_one) < 0)
2634 return -1;
2637 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2638 return -1;
2639 if (test_coalesce_special(ctx) < 0)
2640 return -1;
2641 if (test_coalesce_special2(ctx) < 0)
2642 return -1;
2643 if (test_coalesce_special3(ctx) < 0)
2644 return -1;
2645 if (test_coalesce_special4(ctx) < 0)
2646 return -1;
2647 if (test_coalesce_special5(ctx) < 0)
2648 return -1;
2649 if (test_coalesce_special6(ctx) < 0)
2650 return -1;
2651 if (test_coalesce_special7(ctx) < 0)
2652 return -1;
2653 if (test_coalesce_special8(ctx) < 0)
2654 return -1;
2656 return 0;
2659 /* Construct a representation of the graph on the right of Figure 1
2660 * in "Computing the Transitive Closure of a Union of
2661 * Affine Integer Tuple Relations".
2663 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2665 isl_set *dom;
2666 isl_map *up, *right;
2668 dom = isl_set_read_from_str(ctx,
2669 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2670 "2 x - 3 y + 3 >= 0 }");
2671 right = isl_map_read_from_str(ctx,
2672 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2673 up = isl_map_read_from_str(ctx,
2674 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2675 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2676 right = isl_map_intersect_range(right, isl_set_copy(dom));
2677 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2678 up = isl_map_intersect_range(up, dom);
2679 return isl_map_union(up, right);
2682 /* Construct a representation of the power of the graph
2683 * on the right of Figure 1 in "Computing the Transitive Closure of
2684 * a Union of Affine Integer Tuple Relations".
2686 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2688 return isl_map_read_from_str(ctx,
2689 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2690 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2691 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2694 /* Construct a representation of the transitive closure of the graph
2695 * on the right of Figure 1 in "Computing the Transitive Closure of
2696 * a Union of Affine Integer Tuple Relations".
2698 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2700 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2703 static int test_closure(isl_ctx *ctx)
2705 const char *str;
2706 isl_map *map, *map2;
2707 isl_bool exact, equal;
2709 /* COCOA example 1 */
2710 map = isl_map_read_from_str(ctx,
2711 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2712 "1 <= i and i < n and 1 <= j and j < n or "
2713 "i2 = i + 1 and j2 = j - 1 and "
2714 "1 <= i and i < n and 2 <= j and j <= n }");
2715 map = isl_map_power(map, &exact);
2716 assert(exact);
2717 isl_map_free(map);
2719 /* COCOA example 1 */
2720 map = isl_map_read_from_str(ctx,
2721 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2722 "1 <= i and i < n and 1 <= j and j < n or "
2723 "i2 = i + 1 and j2 = j - 1 and "
2724 "1 <= i and i < n and 2 <= j and j <= n }");
2725 map = isl_map_transitive_closure(map, &exact);
2726 assert(exact);
2727 map2 = isl_map_read_from_str(ctx,
2728 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2729 "1 <= i and i < n and 1 <= j and j <= n and "
2730 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2731 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2732 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2733 assert(isl_map_is_equal(map, map2));
2734 isl_map_free(map2);
2735 isl_map_free(map);
2737 map = isl_map_read_from_str(ctx,
2738 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2739 " 0 <= y and y <= n }");
2740 map = isl_map_transitive_closure(map, &exact);
2741 map2 = isl_map_read_from_str(ctx,
2742 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2743 " 0 <= y and y <= n }");
2744 assert(isl_map_is_equal(map, map2));
2745 isl_map_free(map2);
2746 isl_map_free(map);
2748 /* COCOA example 2 */
2749 map = isl_map_read_from_str(ctx,
2750 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2751 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2752 "i2 = i + 2 and j2 = j - 2 and "
2753 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2754 map = isl_map_transitive_closure(map, &exact);
2755 assert(exact);
2756 map2 = isl_map_read_from_str(ctx,
2757 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2758 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2759 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2760 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2761 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2762 assert(isl_map_is_equal(map, map2));
2763 isl_map_free(map);
2764 isl_map_free(map2);
2766 /* COCOA Fig.2 left */
2767 map = isl_map_read_from_str(ctx,
2768 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2769 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2770 "j <= n or "
2771 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2772 "j <= 2 i - 3 and j <= n - 2 or "
2773 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2774 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2775 map = isl_map_transitive_closure(map, &exact);
2776 assert(exact);
2777 isl_map_free(map);
2779 /* COCOA Fig.2 right */
2780 map = isl_map_read_from_str(ctx,
2781 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2782 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2783 "j <= n or "
2784 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2785 "j <= 2 i - 4 and j <= n - 3 or "
2786 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2787 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2788 map = isl_map_power(map, &exact);
2789 assert(exact);
2790 isl_map_free(map);
2792 /* COCOA Fig.2 right */
2793 map = isl_map_read_from_str(ctx,
2794 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2795 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2796 "j <= n or "
2797 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2798 "j <= 2 i - 4 and j <= n - 3 or "
2799 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2800 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2801 map = isl_map_transitive_closure(map, &exact);
2802 assert(exact);
2803 map2 = isl_map_read_from_str(ctx,
2804 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2805 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2806 "j <= n and 3 + i + 2 j <= 3 n and "
2807 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2808 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2809 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2810 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2811 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2812 assert(isl_map_is_equal(map, map2));
2813 isl_map_free(map2);
2814 isl_map_free(map);
2816 map = cocoa_fig_1_right_graph(ctx);
2817 map = isl_map_transitive_closure(map, &exact);
2818 assert(exact);
2819 map2 = cocoa_fig_1_right_tc(ctx);
2820 assert(isl_map_is_equal(map, map2));
2821 isl_map_free(map2);
2822 isl_map_free(map);
2824 map = cocoa_fig_1_right_graph(ctx);
2825 map = isl_map_power(map, &exact);
2826 map2 = cocoa_fig_1_right_power(ctx);
2827 equal = isl_map_is_equal(map, map2);
2828 isl_map_free(map2);
2829 isl_map_free(map);
2830 if (equal < 0)
2831 return -1;
2832 if (!exact)
2833 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2834 if (!equal)
2835 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2837 /* COCOA Theorem 1 counter example */
2838 map = isl_map_read_from_str(ctx,
2839 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2840 "i2 = 1 and j2 = j or "
2841 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2842 map = isl_map_transitive_closure(map, &exact);
2843 assert(exact);
2844 isl_map_free(map);
2846 map = isl_map_read_from_str(ctx,
2847 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2848 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2849 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2850 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2851 map = isl_map_transitive_closure(map, &exact);
2852 assert(exact);
2853 isl_map_free(map);
2855 /* Kelly et al 1996, fig 12 */
2856 map = isl_map_read_from_str(ctx,
2857 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2858 "1 <= i,j,j+1 <= n or "
2859 "j = n and j2 = 1 and i2 = i + 1 and "
2860 "1 <= i,i+1 <= n }");
2861 map = isl_map_transitive_closure(map, &exact);
2862 assert(exact);
2863 map2 = isl_map_read_from_str(ctx,
2864 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2865 "1 <= i <= n and i = i2 or "
2866 "1 <= i < i2 <= n and 1 <= j <= n and "
2867 "1 <= j2 <= n }");
2868 assert(isl_map_is_equal(map, map2));
2869 isl_map_free(map2);
2870 isl_map_free(map);
2872 /* Omega's closure4 */
2873 map = isl_map_read_from_str(ctx,
2874 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2875 "1 <= x,y <= 10 or "
2876 "x2 = x + 1 and y2 = y and "
2877 "1 <= x <= 20 && 5 <= y <= 15 }");
2878 map = isl_map_transitive_closure(map, &exact);
2879 assert(exact);
2880 isl_map_free(map);
2882 map = isl_map_read_from_str(ctx,
2883 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2884 map = isl_map_transitive_closure(map, &exact);
2885 assert(!exact);
2886 map2 = isl_map_read_from_str(ctx,
2887 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2888 assert(isl_map_is_equal(map, map2));
2889 isl_map_free(map);
2890 isl_map_free(map2);
2892 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2893 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2894 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2895 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2896 map = isl_map_read_from_str(ctx, str);
2897 map = isl_map_transitive_closure(map, &exact);
2898 assert(exact);
2899 map2 = isl_map_read_from_str(ctx, str);
2900 assert(isl_map_is_equal(map, map2));
2901 isl_map_free(map);
2902 isl_map_free(map2);
2904 str = "{[0] -> [1]; [2] -> [3]}";
2905 map = isl_map_read_from_str(ctx, str);
2906 map = isl_map_transitive_closure(map, &exact);
2907 assert(exact);
2908 map2 = isl_map_read_from_str(ctx, str);
2909 assert(isl_map_is_equal(map, map2));
2910 isl_map_free(map);
2911 isl_map_free(map2);
2913 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2914 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2915 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2916 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2917 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2918 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2919 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2920 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2921 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2922 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2923 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2924 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2925 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2926 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2927 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2928 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2929 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2930 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2931 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2932 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2933 map = isl_map_read_from_str(ctx, str);
2934 map = isl_map_transitive_closure(map, NULL);
2935 assert(map);
2936 isl_map_free(map);
2938 return 0;
2941 /* Check that the actual result of a boolean operation is equal
2942 * to the expected result.
2944 static isl_stat check_bool(isl_ctx *ctx, isl_bool actual, isl_bool expected)
2946 if (actual != expected)
2947 isl_die(ctx, isl_error_unknown,
2948 "incorrect boolean operation", return isl_stat_error);
2949 return isl_stat_ok;
2952 /* Test operations on isl_bool values.
2954 * This tests:
2956 * isl_bool_not
2957 * isl_bool_ok
2959 static int test_isl_bool(isl_ctx *ctx)
2961 if (check_bool(ctx, isl_bool_not(isl_bool_true), isl_bool_false) < 0)
2962 return -1;
2963 if (check_bool(ctx, isl_bool_not(isl_bool_false), isl_bool_true) < 0)
2964 return -1;
2965 if (check_bool(ctx, isl_bool_not(isl_bool_error), isl_bool_error) < 0)
2966 return -1;
2967 if (check_bool(ctx, isl_bool_ok(0), isl_bool_false) < 0)
2968 return -1;
2969 if (check_bool(ctx, isl_bool_ok(1), isl_bool_true) < 0)
2970 return -1;
2971 if (check_bool(ctx, isl_bool_ok(-1), isl_bool_true) < 0)
2972 return -1;
2973 if (check_bool(ctx, isl_bool_ok(2), isl_bool_true) < 0)
2974 return -1;
2975 if (check_bool(ctx, isl_bool_ok(-2), isl_bool_true) < 0)
2976 return -1;
2978 return 0;
2981 static int test_lex(struct isl_ctx *ctx)
2983 isl_space *space;
2984 isl_map *map;
2985 int empty;
2987 space = isl_space_set_alloc(ctx, 0, 0);
2988 map = isl_map_lex_le(space);
2989 empty = isl_map_is_empty(map);
2990 isl_map_free(map);
2992 if (empty < 0)
2993 return -1;
2994 if (empty)
2995 isl_die(ctx, isl_error_unknown,
2996 "expecting non-empty result", return -1);
2998 return 0;
3001 /* Inputs for isl_map_lexmin tests.
3002 * "map" is the input and "lexmin" is the expected result.
3004 struct {
3005 const char *map;
3006 const char *lexmin;
3007 } lexmin_tests [] = {
3008 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
3009 "{ [x] -> [5] : 6 <= x <= 8; "
3010 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
3011 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
3012 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
3013 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
3014 "{ [x] -> [y] : (4y = x and x >= 0) or "
3015 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
3016 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
3017 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
3018 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
3019 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
3020 /* Check that empty pieces are properly combined. */
3021 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
3022 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
3023 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
3024 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
3025 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
3026 "x >= 4 }" },
3027 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
3028 "a <= 255 and c <= 255 and d <= 255 - j and "
3029 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
3030 "247d <= 247 + k - j and 247d <= 247 + k - b and "
3031 "247d <= 247 + i and 248 - b <= 248d <= c and "
3032 "254d >= i - a + b and 254d >= -a + b and "
3033 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
3034 "{ [i, k, j] -> "
3035 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
3036 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
3037 "247j >= 62738 - i and 509j <= 129795 + i and "
3038 "742j >= 188724 - i; "
3039 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
3040 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
3041 "16*floor((8 + b)/16) <= 7 + b; "
3042 "[a] -> [1] }",
3043 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
3044 "[a] -> [b = 0] : 0 < a <= 509 }" },
3045 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
3046 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
3047 { "{ rat: [i] : 21 <= 2i <= 29 or i = 5 }", "{ rat: [5] }" },
3050 static int test_lexmin(struct isl_ctx *ctx)
3052 int i;
3053 int equal;
3054 const char *str;
3055 isl_basic_map *bmap;
3056 isl_map *map, *map2;
3057 isl_set *set;
3058 isl_set *set2;
3059 isl_pw_multi_aff *pma;
3061 str = "[p0, p1] -> { [] -> [] : "
3062 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
3063 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
3064 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
3065 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
3066 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
3067 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
3068 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
3069 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
3070 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
3071 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
3072 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
3073 map = isl_map_read_from_str(ctx, str);
3074 map = isl_map_lexmin(map);
3075 isl_map_free(map);
3076 if (!map)
3077 return -1;
3079 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
3080 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
3081 set = isl_set_read_from_str(ctx, str);
3082 set = isl_set_lexmax(set);
3083 str = "[C] -> { [obj,a,b,c] : C = 8 }";
3084 set2 = isl_set_read_from_str(ctx, str);
3085 set = isl_set_intersect(set, set2);
3086 assert(!isl_set_is_empty(set));
3087 isl_set_free(set);
3089 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
3090 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
3091 map = isl_map_lexmin(map);
3092 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
3093 equal = isl_map_is_equal(map, map2);
3094 isl_map_free(map);
3095 isl_map_free(map2);
3097 if (equal < 0)
3098 return -1;
3099 if (!equal)
3100 isl_die(ctx, isl_error_unknown,
3101 "unexpected result", return -1);
3104 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
3105 " 8i' <= i and 8i' >= -7 + i }";
3106 bmap = isl_basic_map_read_from_str(ctx, str);
3107 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
3108 map2 = isl_map_from_pw_multi_aff(pma);
3109 map = isl_map_from_basic_map(bmap);
3110 assert(isl_map_is_equal(map, map2));
3111 isl_map_free(map);
3112 isl_map_free(map2);
3114 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
3115 " 8i' <= i and 8i' >= -7 + i }";
3116 set = isl_set_read_from_str(ctx, str);
3117 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
3118 set2 = isl_set_from_pw_multi_aff(pma);
3119 equal = isl_set_is_equal(set, set2);
3120 isl_set_free(set);
3121 isl_set_free(set2);
3122 if (equal < 0)
3123 return -1;
3124 if (!equal)
3125 isl_die(ctx, isl_error_unknown,
3126 "unexpected difference between set and "
3127 "piecewise affine expression", return -1);
3129 return 0;
3132 /* Inputs for isl_pw_multi_aff_max_multi_val tests.
3133 * "pma" is the input.
3134 * "res" is the expected result.
3136 static struct {
3137 const char *pma;
3138 const char *res;
3139 } opt_pw_tests[] = {
3140 { "{ [-1] -> [-1]; [1] -> [1] }", "{ [1] }" },
3141 { "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] : "
3142 "0 <= a, b <= 100 and b mod 2 = 0}", "{ [30] }" },
3143 { "[N] -> { [i,j] -> A[i, -i, i + j] : 0 <= i,j <= N <= 10 }",
3144 "{ A[10, 0, 20] }" },
3145 { "[N] -> {A[N, -N, 2N] : 0 <= N }", "{ A[infty, 0, infty] }" },
3148 /* Perform basic isl_pw_multi_aff_max_multi_val tests.
3150 static isl_stat test_pw_max(struct isl_ctx *ctx)
3152 int i;
3153 isl_pw_multi_aff *pma;
3154 isl_multi_val *mv;
3155 isl_stat r;
3157 for (i = 0; i < ARRAY_SIZE(opt_pw_tests); ++i) {
3158 pma = isl_pw_multi_aff_read_from_str(ctx, opt_pw_tests[i].pma);
3159 mv = isl_pw_multi_aff_max_multi_val(pma);
3160 r = multi_val_check_plain_equal(mv, opt_pw_tests[i].res);
3161 isl_multi_val_free(mv);
3163 if (r < 0)
3164 return isl_stat_error;
3167 return isl_stat_ok;
3170 /* A specialized isl_set_min_val test case that would return the wrong result
3171 * in earlier versions of isl.
3172 * The explicit call to isl_basic_set_union prevents the second basic set
3173 * from being determined to be empty prior to the call to isl_set_min_val,
3174 * at least at the point where this test case was introduced.
3176 static int test_min_special(isl_ctx *ctx)
3178 const char *str;
3179 isl_basic_set *bset1, *bset2;
3180 isl_set *set;
3181 isl_aff *obj;
3182 isl_val *res;
3183 int ok;
3185 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
3186 bset1 = isl_basic_set_read_from_str(ctx, str);
3187 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
3188 bset2 = isl_basic_set_read_from_str(ctx, str);
3189 set = isl_basic_set_union(bset1, bset2);
3190 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
3192 res = isl_set_min_val(set, obj);
3193 ok = isl_val_cmp_si(res, 5) == 0;
3195 isl_aff_free(obj);
3196 isl_set_free(set);
3197 isl_val_free(res);
3199 if (!res)
3200 return -1;
3201 if (!ok)
3202 isl_die(ctx, isl_error_unknown, "unexpected minimum",
3203 return -1);
3205 return 0;
3208 /* A specialized isl_set_min_val test case that would return an error
3209 * in earlier versions of isl.
3211 static int test_min_special2(isl_ctx *ctx)
3213 const char *str;
3214 isl_basic_set *bset;
3215 isl_aff *obj;
3216 isl_val *res;
3218 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
3219 bset = isl_basic_set_read_from_str(ctx, str);
3221 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
3223 res = isl_basic_set_max_val(bset, obj);
3225 isl_basic_set_free(bset);
3226 isl_aff_free(obj);
3227 isl_val_free(res);
3229 if (!res)
3230 return -1;
3232 return 0;
3235 /* Check that the result of isl_set_min_multi_pw_aff
3236 * on the union of the sets with string descriptions "s1" and "s2"
3237 * consists of a single expression (on a single cell).
3239 static isl_stat check_single_expr_min(isl_ctx *ctx, const char *s1,
3240 const char *s2)
3242 isl_size n;
3243 isl_set *set1, *set2;
3244 isl_multi_pw_aff *mpa;
3245 isl_pw_multi_aff *pma;
3247 set1 = isl_set_read_from_str(ctx, s1);
3248 set2 = isl_set_read_from_str(ctx, s2);
3249 set1 = isl_set_union(set1, set2);
3250 mpa = isl_set_min_multi_pw_aff(set1);
3251 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
3252 n = isl_pw_multi_aff_n_piece(pma);
3253 isl_pw_multi_aff_free(pma);
3255 if (n < 0)
3256 return isl_stat_error;
3257 if (n != 1)
3258 isl_die(ctx, isl_error_unknown, "expecting single expression",
3259 return isl_stat_error);
3260 return isl_stat_ok;
3263 /* A specialized isl_set_min_multi_pw_aff test that checks
3264 * that the minimum of 2N and 3N for N >= 0 is represented
3265 * by a single expression, without splitting off the special case N = 0.
3266 * Do this for both orderings.
3268 static int test_min_mpa(isl_ctx *ctx)
3270 const char *s1, *s2;
3272 s1 = "[N=0:] -> { [1, 3N:] }";
3273 s2 = "[N=0:] -> { [10, 2N:] }";
3274 if (check_single_expr_min(ctx, s1, s2) < 0)
3275 return -1;
3276 if (check_single_expr_min(ctx, s2, s1) < 0)
3277 return -1;
3279 return 0;
3282 struct {
3283 const char *set;
3284 const char *obj;
3285 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
3286 __isl_keep isl_aff *obj);
3287 const char *res;
3288 } opt_tests[] = {
3289 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
3290 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
3291 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
3292 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
3293 &isl_set_max_val, "30" },
3297 /* Perform basic isl_set_min_val and isl_set_max_val tests.
3298 * In particular, check the results on non-convex inputs.
3300 static int test_min(struct isl_ctx *ctx)
3302 int i;
3303 isl_set *set;
3304 isl_aff *obj;
3305 isl_val *val, *res;
3306 isl_bool ok;
3308 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
3309 set = isl_set_read_from_str(ctx, opt_tests[i].set);
3310 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
3311 res = isl_val_read_from_str(ctx, opt_tests[i].res);
3312 val = opt_tests[i].fn(set, obj);
3313 ok = isl_val_eq(res, val);
3314 isl_val_free(res);
3315 isl_val_free(val);
3316 isl_aff_free(obj);
3317 isl_set_free(set);
3319 if (ok < 0)
3320 return -1;
3321 if (!ok)
3322 isl_die(ctx, isl_error_unknown,
3323 "unexpected optimum", return -1);
3326 if (test_pw_max(ctx) < 0)
3327 return -1;
3328 if (test_min_special(ctx) < 0)
3329 return -1;
3330 if (test_min_special2(ctx) < 0)
3331 return -1;
3333 return 0;
3336 struct must_may {
3337 isl_map *must;
3338 isl_map *may;
3341 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
3342 void *dep_user, void *user)
3344 struct must_may *mm = (struct must_may *)user;
3346 if (must)
3347 mm->must = isl_map_union(mm->must, dep);
3348 else
3349 mm->may = isl_map_union(mm->may, dep);
3351 return isl_stat_ok;
3354 static int common_space(void *first, void *second)
3356 int depth = *(int *)first;
3357 return 2 * depth;
3360 static int map_is_equal(__isl_keep isl_map *map, const char *str)
3362 isl_map *map2;
3363 int equal;
3365 if (!map)
3366 return -1;
3368 map2 = isl_map_read_from_str(map->ctx, str);
3369 equal = isl_map_is_equal(map, map2);
3370 isl_map_free(map2);
3372 return equal;
3375 static int map_check_equal(__isl_keep isl_map *map, const char *str)
3377 int equal;
3379 equal = map_is_equal(map, str);
3380 if (equal < 0)
3381 return -1;
3382 if (!equal)
3383 isl_die(isl_map_get_ctx(map), isl_error_unknown,
3384 "result not as expected", return -1);
3385 return 0;
3388 /* Is "set" equal to the set described by "str"?
3390 static isl_bool set_is_equal(__isl_keep isl_set *set, const char *str)
3392 isl_set *set2;
3393 isl_bool equal;
3395 if (!set)
3396 return isl_bool_error;
3398 set2 = isl_set_read_from_str(isl_set_get_ctx(set), str);
3399 equal = isl_set_is_equal(set, set2);
3400 isl_set_free(set2);
3402 return equal;
3405 /* Check that "set" is equal to the set described by "str".
3407 static isl_stat set_check_equal(__isl_keep isl_set *set, const char *str)
3409 isl_bool equal;
3411 equal = set_is_equal(set, str);
3412 if (equal < 0)
3413 return isl_stat_error;
3414 if (!equal)
3415 isl_die(isl_set_get_ctx(set), isl_error_unknown,
3416 "result not as expected", return isl_stat_error);
3417 return isl_stat_ok;
3420 /* Is "uset" equal to the union set described by "str"?
3422 static isl_bool uset_is_equal(__isl_keep isl_union_set *uset, const char *str)
3424 isl_union_set *uset2;
3425 isl_bool equal;
3427 if (!uset)
3428 return isl_bool_error;
3430 uset2 = isl_union_set_read_from_str(isl_union_set_get_ctx(uset), str);
3431 equal = isl_union_set_is_equal(uset, uset2);
3432 isl_union_set_free(uset2);
3434 return equal;
3437 /* Check that "uset" is equal to the union set described by "str".
3439 static isl_stat uset_check_equal(__isl_keep isl_union_set *uset,
3440 const char *str)
3442 isl_bool equal;
3444 equal = uset_is_equal(uset, str);
3445 if (equal < 0)
3446 return isl_stat_error;
3447 if (!equal)
3448 isl_die(isl_union_set_get_ctx(uset), isl_error_unknown,
3449 "result not as expected", return isl_stat_error);
3450 return isl_stat_ok;
3453 static int test_dep(struct isl_ctx *ctx)
3455 const char *str;
3456 isl_space *space;
3457 isl_map *map;
3458 isl_access_info *ai;
3459 isl_flow *flow;
3460 int depth;
3461 struct must_may mm;
3463 depth = 3;
3465 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3466 map = isl_map_read_from_str(ctx, str);
3467 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3469 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3470 map = isl_map_read_from_str(ctx, str);
3471 ai = isl_access_info_add_source(ai, map, 1, &depth);
3473 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3474 map = isl_map_read_from_str(ctx, str);
3475 ai = isl_access_info_add_source(ai, map, 1, &depth);
3477 flow = isl_access_info_compute_flow(ai);
3478 space = isl_space_alloc(ctx, 0, 3, 3);
3479 mm.must = isl_map_empty(isl_space_copy(space));
3480 mm.may = isl_map_empty(space);
3482 isl_flow_foreach(flow, collect_must_may, &mm);
3484 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
3485 " [1,10,0] -> [2,5,0] }";
3486 assert(map_is_equal(mm.must, str));
3487 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3488 assert(map_is_equal(mm.may, str));
3490 isl_map_free(mm.must);
3491 isl_map_free(mm.may);
3492 isl_flow_free(flow);
3495 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3496 map = isl_map_read_from_str(ctx, str);
3497 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3499 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3500 map = isl_map_read_from_str(ctx, str);
3501 ai = isl_access_info_add_source(ai, map, 1, &depth);
3503 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3504 map = isl_map_read_from_str(ctx, str);
3505 ai = isl_access_info_add_source(ai, map, 0, &depth);
3507 flow = isl_access_info_compute_flow(ai);
3508 space = isl_space_alloc(ctx, 0, 3, 3);
3509 mm.must = isl_map_empty(isl_space_copy(space));
3510 mm.may = isl_map_empty(space);
3512 isl_flow_foreach(flow, collect_must_may, &mm);
3514 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
3515 assert(map_is_equal(mm.must, str));
3516 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3517 assert(map_is_equal(mm.may, str));
3519 isl_map_free(mm.must);
3520 isl_map_free(mm.may);
3521 isl_flow_free(flow);
3524 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3525 map = isl_map_read_from_str(ctx, str);
3526 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3528 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3529 map = isl_map_read_from_str(ctx, str);
3530 ai = isl_access_info_add_source(ai, map, 0, &depth);
3532 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3533 map = isl_map_read_from_str(ctx, str);
3534 ai = isl_access_info_add_source(ai, map, 0, &depth);
3536 flow = isl_access_info_compute_flow(ai);
3537 space = isl_space_alloc(ctx, 0, 3, 3);
3538 mm.must = isl_map_empty(isl_space_copy(space));
3539 mm.may = isl_map_empty(space);
3541 isl_flow_foreach(flow, collect_must_may, &mm);
3543 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
3544 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3545 assert(map_is_equal(mm.may, str));
3546 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3547 assert(map_is_equal(mm.must, str));
3549 isl_map_free(mm.must);
3550 isl_map_free(mm.may);
3551 isl_flow_free(flow);
3554 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
3555 map = isl_map_read_from_str(ctx, str);
3556 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3558 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3559 map = isl_map_read_from_str(ctx, str);
3560 ai = isl_access_info_add_source(ai, map, 0, &depth);
3562 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
3563 map = isl_map_read_from_str(ctx, str);
3564 ai = isl_access_info_add_source(ai, map, 0, &depth);
3566 flow = isl_access_info_compute_flow(ai);
3567 space = isl_space_alloc(ctx, 0, 3, 3);
3568 mm.must = isl_map_empty(isl_space_copy(space));
3569 mm.may = isl_map_empty(space);
3571 isl_flow_foreach(flow, collect_must_may, &mm);
3573 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
3574 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
3575 assert(map_is_equal(mm.may, str));
3576 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3577 assert(map_is_equal(mm.must, str));
3579 isl_map_free(mm.must);
3580 isl_map_free(mm.may);
3581 isl_flow_free(flow);
3584 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
3585 map = isl_map_read_from_str(ctx, str);
3586 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3588 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3589 map = isl_map_read_from_str(ctx, str);
3590 ai = isl_access_info_add_source(ai, map, 0, &depth);
3592 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
3593 map = isl_map_read_from_str(ctx, str);
3594 ai = isl_access_info_add_source(ai, map, 0, &depth);
3596 flow = isl_access_info_compute_flow(ai);
3597 space = isl_space_alloc(ctx, 0, 3, 3);
3598 mm.must = isl_map_empty(isl_space_copy(space));
3599 mm.may = isl_map_empty(space);
3601 isl_flow_foreach(flow, collect_must_may, &mm);
3603 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
3604 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
3605 assert(map_is_equal(mm.may, str));
3606 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3607 assert(map_is_equal(mm.must, str));
3609 isl_map_free(mm.must);
3610 isl_map_free(mm.may);
3611 isl_flow_free(flow);
3614 depth = 5;
3616 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3617 map = isl_map_read_from_str(ctx, str);
3618 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
3620 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3621 map = isl_map_read_from_str(ctx, str);
3622 ai = isl_access_info_add_source(ai, map, 1, &depth);
3624 flow = isl_access_info_compute_flow(ai);
3625 space = isl_space_alloc(ctx, 0, 5, 5);
3626 mm.must = isl_map_empty(isl_space_copy(space));
3627 mm.may = isl_map_empty(space);
3629 isl_flow_foreach(flow, collect_must_may, &mm);
3631 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
3632 assert(map_is_equal(mm.must, str));
3633 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
3634 assert(map_is_equal(mm.may, str));
3636 isl_map_free(mm.must);
3637 isl_map_free(mm.may);
3638 isl_flow_free(flow);
3640 return 0;
3643 /* Check that the dependence analysis proceeds without errors.
3644 * Earlier versions of isl would break down during the analysis
3645 * due to the use of the wrong spaces.
3647 static int test_flow(isl_ctx *ctx)
3649 const char *str;
3650 isl_union_map *access, *schedule;
3651 isl_union_map *must_dep, *may_dep;
3652 int r;
3654 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
3655 access = isl_union_map_read_from_str(ctx, str);
3656 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
3657 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
3658 "S2[] -> [1,0,0,0]; "
3659 "S3[] -> [-1,0,0,0] }";
3660 schedule = isl_union_map_read_from_str(ctx, str);
3661 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
3662 isl_union_map_copy(access), schedule,
3663 &must_dep, &may_dep, NULL, NULL);
3664 isl_union_map_free(may_dep);
3665 isl_union_map_free(must_dep);
3667 return r;
3670 struct {
3671 const char *map;
3672 int sv;
3673 } sv_tests[] = {
3674 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
3675 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
3676 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
3677 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
3678 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
3679 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
3680 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3681 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3682 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
3685 int test_sv(isl_ctx *ctx)
3687 isl_union_map *umap;
3688 int i;
3689 int sv;
3691 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
3692 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
3693 sv = isl_union_map_is_single_valued(umap);
3694 isl_union_map_free(umap);
3695 if (sv < 0)
3696 return -1;
3697 if (sv_tests[i].sv && !sv)
3698 isl_die(ctx, isl_error_internal,
3699 "map not detected as single valued", return -1);
3700 if (!sv_tests[i].sv && sv)
3701 isl_die(ctx, isl_error_internal,
3702 "map detected as single valued", return -1);
3705 return 0;
3708 struct {
3709 const char *str;
3710 int bijective;
3711 } bijective_tests[] = {
3712 { "[N,M]->{[i,j] -> [i]}", 0 },
3713 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
3714 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
3715 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
3716 { "[N,M]->{[i,j] -> [j,i]}", 1 },
3717 { "[N,M]->{[i,j] -> [i+j]}", 0 },
3718 { "[N,M]->{[i,j] -> []}", 0 },
3719 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
3720 { "[N,M]->{[i,j] -> [2i]}", 0 },
3721 { "[N,M]->{[i,j] -> [i,i]}", 0 },
3722 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
3723 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
3724 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
3727 static int test_bijective(struct isl_ctx *ctx)
3729 isl_map *map;
3730 int i;
3731 int bijective;
3733 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
3734 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
3735 bijective = isl_map_is_bijective(map);
3736 isl_map_free(map);
3737 if (bijective < 0)
3738 return -1;
3739 if (bijective_tests[i].bijective && !bijective)
3740 isl_die(ctx, isl_error_internal,
3741 "map not detected as bijective", return -1);
3742 if (!bijective_tests[i].bijective && bijective)
3743 isl_die(ctx, isl_error_internal,
3744 "map detected as bijective", return -1);
3747 return 0;
3750 /* Inputs for isl_pw_qpolynomial_gist tests.
3751 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
3753 struct {
3754 const char *pwqp;
3755 const char *set;
3756 const char *gist;
3757 } pwqp_gist_tests[] = {
3758 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
3759 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
3760 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
3761 "{ [i] -> -1/2 + 1/2 * i }" },
3762 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
3765 /* Perform some basic isl_pw_qpolynomial_gist tests.
3767 static isl_stat test_pwqp_gist(isl_ctx *ctx)
3769 int i;
3770 const char *str;
3771 isl_set *set;
3772 isl_pw_qpolynomial *pwqp1, *pwqp2;
3773 isl_bool equal;
3775 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
3776 str = pwqp_gist_tests[i].pwqp;
3777 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3778 str = pwqp_gist_tests[i].set;
3779 set = isl_set_read_from_str(ctx, str);
3780 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
3781 str = pwqp_gist_tests[i].gist;
3782 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3783 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3784 equal = isl_pw_qpolynomial_is_zero(pwqp1);
3785 isl_pw_qpolynomial_free(pwqp1);
3787 if (equal < 0)
3788 return isl_stat_error;
3789 if (!equal)
3790 isl_die(ctx, isl_error_unknown,
3791 "unexpected result", return isl_stat_error);
3794 return isl_stat_ok;
3797 /* Perform a basic isl_pw_qpolynomial_max test.
3799 static isl_stat test_pwqp_max(isl_ctx *ctx)
3801 const char *str;
3802 isl_pw_qpolynomial *pwqp;
3803 isl_val *v;
3804 int ok;
3806 str = "{ [x=2:9, y] -> floor((x + 1)/4)^3 - floor((2x)/3)^2 }";
3807 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3808 v = isl_pw_qpolynomial_max(pwqp);
3809 ok = isl_val_cmp_si(v, -1) == 0;
3810 isl_val_free(v);
3812 if (!v)
3813 return isl_stat_error;
3814 if (!ok)
3815 isl_die(ctx, isl_error_unknown, "unexpected maximum",
3816 return isl_stat_error);
3818 return isl_stat_ok;
3821 static int test_pwqp(struct isl_ctx *ctx)
3823 const char *str;
3824 isl_set *set;
3825 isl_pw_qpolynomial *pwqp1, *pwqp2;
3826 int equal;
3828 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3829 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3831 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
3832 isl_dim_in, 1, 1);
3834 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3835 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3837 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3839 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3841 isl_pw_qpolynomial_free(pwqp1);
3843 if (test_pwqp_gist(ctx) < 0)
3844 return -1;
3846 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
3847 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3848 str = "{ [i] -> ([(2 * [i/2])/5]) }";
3849 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3851 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3853 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3855 isl_pw_qpolynomial_free(pwqp1);
3857 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3858 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3859 str = "{ [x] -> x }";
3860 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3862 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3864 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3866 isl_pw_qpolynomial_free(pwqp1);
3868 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3869 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3870 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3871 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3872 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3873 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3874 isl_pw_qpolynomial_free(pwqp1);
3876 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3877 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3878 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3879 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3880 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3881 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3882 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3883 isl_pw_qpolynomial_free(pwqp1);
3884 isl_pw_qpolynomial_free(pwqp2);
3885 if (equal < 0)
3886 return -1;
3887 if (!equal)
3888 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3890 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3891 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3892 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3893 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3894 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3895 isl_val_one(ctx));
3896 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3897 isl_pw_qpolynomial_free(pwqp1);
3898 isl_pw_qpolynomial_free(pwqp2);
3899 if (equal < 0)
3900 return -1;
3901 if (!equal)
3902 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3904 if (test_pwqp_max(ctx) < 0)
3905 return -1;
3907 return 0;
3910 static int test_split_periods(isl_ctx *ctx)
3912 const char *str;
3913 isl_pw_qpolynomial *pwqp;
3915 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3916 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3917 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3918 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3920 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3922 isl_pw_qpolynomial_free(pwqp);
3924 if (!pwqp)
3925 return -1;
3927 return 0;
3930 static int test_union(isl_ctx *ctx)
3932 const char *str;
3933 isl_union_set *uset1, *uset2;
3934 isl_union_map *umap1, *umap2;
3935 int equal;
3937 str = "{ [i] : 0 <= i <= 1 }";
3938 uset1 = isl_union_set_read_from_str(ctx, str);
3939 str = "{ [1] -> [0] }";
3940 umap1 = isl_union_map_read_from_str(ctx, str);
3942 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3943 equal = isl_union_map_is_equal(umap1, umap2);
3945 isl_union_map_free(umap1);
3946 isl_union_map_free(umap2);
3948 if (equal < 0)
3949 return -1;
3950 if (!equal)
3951 isl_die(ctx, isl_error_unknown, "union maps not equal",
3952 return -1);
3954 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3955 umap1 = isl_union_map_read_from_str(ctx, str);
3956 str = "{ A[i]; B[i] }";
3957 uset1 = isl_union_set_read_from_str(ctx, str);
3959 uset2 = isl_union_map_domain(umap1);
3961 equal = isl_union_set_is_equal(uset1, uset2);
3963 isl_union_set_free(uset1);
3964 isl_union_set_free(uset2);
3966 if (equal < 0)
3967 return -1;
3968 if (!equal)
3969 isl_die(ctx, isl_error_unknown, "union sets not equal",
3970 return -1);
3972 return 0;
3975 /* Inputs for basic isl_pw_qpolynomial_bound tests.
3976 * "type" is the type of bound that should be computed.
3977 * "poly" is a string representation of the input.
3978 * "bound" is a string representation of the expected result.
3979 * "tight" is set if the result is expected to be tight.
3981 static struct {
3982 int tight;
3983 enum isl_fold type;
3984 const char *poly;
3985 const char *bound;
3986 } bound_tests[] = {
3987 /* Check that computing a bound of a non-zero polynomial
3988 * over an unbounded domain does not produce a rational value.
3989 * In particular, check that the upper bound is infinity.
3991 { 0, isl_fold_max, "{ [m, n] -> -m * n }", "{ max(infty) }" },
3992 { 1, isl_fold_max, "{ [[a, b, c, d] -> [e]] -> 0 }",
3993 "{ [a, b, c, d] -> max(0) }" },
3994 { 1, isl_fold_max, "{ [[x] -> [x]] -> 1 : exists a : x = 2 a }",
3995 "{ [x] -> max(1) : x mod 2 = 0 }" },
3996 { 1, isl_fold_min, "{ [x=5:10] -> (x + 2)^2 }", "{ min(49) }" },
3997 { 1, isl_fold_max, "{ [0:10] -> 1 }", "{ max(1) }" },
3998 { 1, isl_fold_max, "{ [[m] -> [0:m]] -> m^2 }",
3999 "{ [m] -> max(m^2) : m >= 0 }" },
4002 /* Check that the bound computation can handle differences
4003 * in domain dimension names of the input polynomial and its domain.
4005 static isl_stat test_bound_space(isl_ctx *ctx)
4007 const char *str;
4008 isl_set *set;
4009 isl_pw_qpolynomial *pwqp;
4010 isl_pw_qpolynomial_fold *pwf;
4012 str = "{ [[c] -> [c]] }";
4013 set = isl_set_read_from_str(ctx, str);
4014 str = "{ [[a] -> [b]] -> 1 }";
4015 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
4016 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
4017 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
4018 isl_pw_qpolynomial_fold_free(pwf);
4020 return isl_stat_non_null(pwf);
4023 /* Perform basic isl_pw_qpolynomial_bound tests.
4025 static int test_bound(isl_ctx *ctx)
4027 int i;
4029 if (test_bound_space(ctx) < 0)
4030 return -1;
4032 for (i = 0; i < ARRAY_SIZE(bound_tests); ++i) {
4033 const char *str;
4034 enum isl_fold type;
4035 isl_bool equal, tight;
4036 isl_pw_qpolynomial *pwqp;
4037 isl_pw_qpolynomial_fold *pwf1, *pwf2;
4039 str = bound_tests[i].poly;
4040 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
4041 type = bound_tests[i].type;
4042 pwf1 = isl_pw_qpolynomial_bound(pwqp, type, &tight);
4043 str = bound_tests[i].bound;
4044 pwf2 = isl_pw_qpolynomial_fold_read_from_str(ctx, str);
4045 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf1, pwf2);
4046 isl_pw_qpolynomial_fold_free(pwf2);
4047 isl_pw_qpolynomial_fold_free(pwf1);
4048 if (equal < 0)
4049 return -1;
4050 if (!equal)
4051 isl_die(ctx, isl_error_unknown,
4052 "incorrect bound result", return -1);
4053 if (bound_tests[i].tight && !tight)
4054 isl_die(ctx, isl_error_unknown,
4055 "bound unexpectedly not tight", return -1);
4058 return 0;
4061 /* isl_set is defined to isl_map internally, so the corresponding elements
4062 * are isl_basic_map objects.
4064 #undef EL_BASE
4065 #undef SET_BASE
4066 #define EL_BASE basic_map
4067 #define SET_BASE set
4068 #include "isl_test_list_templ.c"
4070 #undef EL_BASE
4071 #undef SET_BASE
4072 #define EL_BASE basic_set
4073 #define SET_BASE union_set
4074 #include "isl_test_list_templ.c"
4076 #undef EL_BASE
4077 #undef SET_BASE
4078 #define EL_BASE set
4079 #define SET_BASE union_set
4080 #include "isl_test_list_templ.c"
4082 #undef EL_BASE
4083 #undef SET_BASE
4084 #define EL_BASE basic_map
4085 #define SET_BASE map
4086 #include "isl_test_list_templ.c"
4088 #undef EL_BASE
4089 #undef SET_BASE
4090 #define EL_BASE map
4091 #define SET_BASE union_map
4092 #include "isl_test_list_templ.c"
4094 /* Check that the conversion from isl objects to lists works as expected.
4096 static int test_get_list(isl_ctx *ctx)
4098 if (test_get_list_basic_map_from_set(ctx, "{ [0]; [2]; [3] }"))
4099 return -1;
4100 if (test_get_list_basic_set_from_union_set(ctx, "{ A[0]; B[2]; B[3] }"))
4101 return -1;
4102 if (test_get_list_set_from_union_set(ctx, "{ A[0]; A[2]; B[3] }"))
4103 return -1;
4104 if (test_get_list_basic_map_from_map(ctx,
4105 "{ [0] -> [0]; [2] -> [0]; [3] -> [0] }"))
4106 return -1;
4107 if (test_get_list_map_from_union_map(ctx,
4108 "{ A[0] -> [0]; A[2] -> [0]; B[3] -> [0] }"))
4109 return -1;
4111 return 0;
4114 static int test_lift(isl_ctx *ctx)
4116 const char *str;
4117 isl_basic_map *bmap;
4118 isl_basic_set *bset;
4120 str = "{ [i0] : exists e0 : i0 = 4e0 }";
4121 bset = isl_basic_set_read_from_str(ctx, str);
4122 bset = isl_basic_set_lift(bset);
4123 bmap = isl_basic_map_from_range(bset);
4124 bset = isl_basic_map_domain(bmap);
4125 isl_basic_set_free(bset);
4127 return 0;
4130 /* Check that isl_set_is_subset is not confused by identical
4131 * integer divisions.
4132 * The call to isl_set_normalize ensures that the equality constraints
4133 * a = b = 0 are discovered, turning e0 and e1 into identical
4134 * integer divisions. Any further simplification would remove
4135 * the duplicate integer divisions.
4137 static isl_stat test_subset_duplicate_integer_divisions(isl_ctx *ctx)
4139 const char *str;
4140 isl_bool is_subset;
4141 isl_set *set1, *set2;
4143 str = "{ [a, b, c, d] : "
4144 "exists (e0 = floor((a + d)/4), e1 = floor((d)/4), "
4145 "e2 = floor((-a - d + 4 *floor((a + d)/4))/10), "
4146 "e3 = floor((-d + 4*floor((d)/4))/10): "
4147 "10e2 = -a - 2c - d + 4e0 and 10e3 = -2c - d + 4e1 and "
4148 "b >= 0 and a <= 0 and b <= a) }";
4149 set1 = isl_set_read_from_str(ctx, str);
4150 set2 = isl_set_read_from_str(ctx, str);
4151 set2 = isl_set_normalize(set2);
4153 is_subset = isl_set_is_subset(set1, set2);
4155 isl_set_free(set1);
4156 isl_set_free(set2);
4158 if (is_subset < 0)
4159 return isl_stat_error;
4160 if (!is_subset)
4161 isl_die(ctx, isl_error_unknown,
4162 "set is not considered to be a subset of itself",
4163 return isl_stat_error);
4165 return isl_stat_ok;
4168 struct {
4169 const char *set1;
4170 const char *set2;
4171 int subset;
4172 } subset_tests[] = {
4173 { "{ [112, 0] }",
4174 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
4175 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
4176 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
4177 { "{ [65] }",
4178 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
4179 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
4180 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
4181 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
4182 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
4183 "256e0 <= 255i and 256e0 >= -255 + 255i and "
4184 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
4185 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
4186 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
4187 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
4188 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
4189 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
4190 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
4191 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
4192 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
4193 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
4194 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
4195 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
4196 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
4197 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
4198 "4e0 <= -57 + i0 + i1)) or "
4199 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
4200 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
4201 "4e0 >= -61 + i0 + i1)) or "
4202 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
4203 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
4206 static int test_subset(isl_ctx *ctx)
4208 int i;
4209 isl_set *set1, *set2;
4210 int subset;
4212 if (test_subset_duplicate_integer_divisions(ctx) < 0)
4213 return -1;
4215 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
4216 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
4217 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
4218 subset = isl_set_is_subset(set1, set2);
4219 isl_set_free(set1);
4220 isl_set_free(set2);
4221 if (subset < 0)
4222 return -1;
4223 if (subset != subset_tests[i].subset)
4224 isl_die(ctx, isl_error_unknown,
4225 "incorrect subset result", return -1);
4228 return 0;
4231 /* Perform a set subtraction with a set that has a non-obviously empty disjunct.
4232 * Older versions of isl would fail on such cases.
4234 static isl_stat test_subtract_empty(isl_ctx *ctx)
4236 const char *str;
4237 isl_set *s1, *s2;
4239 s1 = isl_set_read_from_str(ctx, "{ [0] }");
4240 str = "{ [a] : (exists (e0, e1, e2: 1056e1 <= 32 + a - 33e0 and "
4241 "1089e1 >= a - 33e0 and 1089e1 <= 1 + a - 33e0 and "
4242 "33e2 >= -a + 33e0 + 1056e1 and "
4243 "33e2 < -2a + 66e0 + 2112e1)) or a = 0 }";
4244 s2 = isl_set_read_from_str(ctx, str);
4245 s1 = isl_set_subtract(s1, s2);
4246 isl_set_free(s1);
4248 return isl_stat_non_null(s1);
4251 struct {
4252 const char *minuend;
4253 const char *subtrahend;
4254 const char *difference;
4255 } subtract_domain_tests[] = {
4256 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
4257 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
4258 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
4261 static int test_subtract(isl_ctx *ctx)
4263 int i;
4264 isl_union_map *umap1, *umap2;
4265 isl_union_pw_multi_aff *upma1, *upma2;
4266 isl_union_set *uset;
4267 int equal;
4269 if (test_subtract_empty(ctx) < 0)
4270 return -1;
4272 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
4273 umap1 = isl_union_map_read_from_str(ctx,
4274 subtract_domain_tests[i].minuend);
4275 uset = isl_union_set_read_from_str(ctx,
4276 subtract_domain_tests[i].subtrahend);
4277 umap2 = isl_union_map_read_from_str(ctx,
4278 subtract_domain_tests[i].difference);
4279 umap1 = isl_union_map_subtract_domain(umap1, uset);
4280 equal = isl_union_map_is_equal(umap1, umap2);
4281 isl_union_map_free(umap1);
4282 isl_union_map_free(umap2);
4283 if (equal < 0)
4284 return -1;
4285 if (!equal)
4286 isl_die(ctx, isl_error_unknown,
4287 "incorrect subtract domain result", return -1);
4290 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
4291 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4292 subtract_domain_tests[i].minuend);
4293 uset = isl_union_set_read_from_str(ctx,
4294 subtract_domain_tests[i].subtrahend);
4295 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4296 subtract_domain_tests[i].difference);
4297 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
4298 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
4299 isl_union_pw_multi_aff_free(upma1);
4300 isl_union_pw_multi_aff_free(upma2);
4301 if (equal < 0)
4302 return -1;
4303 if (!equal)
4304 isl_die(ctx, isl_error_unknown,
4305 "incorrect subtract domain result", return -1);
4308 return 0;
4311 /* Check that intersecting the empty basic set with another basic set
4312 * does not increase the number of constraints. In particular,
4313 * the empty basic set should maintain its canonical representation.
4315 static int test_intersect_1(isl_ctx *ctx)
4317 isl_size n1, n2;
4318 isl_basic_set *bset1, *bset2;
4320 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
4321 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
4322 n1 = isl_basic_set_n_constraint(bset1);
4323 bset1 = isl_basic_set_intersect(bset1, bset2);
4324 n2 = isl_basic_set_n_constraint(bset1);
4325 isl_basic_set_free(bset1);
4326 if (n1 < 0 || n2 < 0)
4327 return -1;
4328 if (n1 != n2)
4329 isl_die(ctx, isl_error_unknown,
4330 "number of constraints of empty set changed",
4331 return -1);
4333 return 0;
4336 /* Check that intersecting a set with itself does not cause
4337 * an explosion in the number of disjuncts.
4339 static isl_stat test_intersect_2(isl_ctx *ctx)
4341 int i;
4342 isl_set *set;
4344 set = isl_set_read_from_str(ctx, "{ [x,y] : x >= 0 or y >= 0 }");
4345 for (i = 0; i < 100; ++i)
4346 set = isl_set_intersect(set, isl_set_copy(set));
4347 isl_set_free(set);
4348 if (!set)
4349 return isl_stat_error;
4350 return isl_stat_ok;
4353 /* Perform some intersection tests.
4355 static int test_intersect(isl_ctx *ctx)
4357 if (test_intersect_1(ctx) < 0)
4358 return -1;
4359 if (test_intersect_2(ctx) < 0)
4360 return -1;
4362 return 0;
4365 int test_factorize(isl_ctx *ctx)
4367 const char *str;
4368 isl_basic_set *bset;
4369 isl_factorizer *f;
4371 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
4372 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
4373 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
4374 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
4375 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
4376 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
4377 "3i5 >= -2i0 - i2 + 3i4 }";
4378 bset = isl_basic_set_read_from_str(ctx, str);
4379 f = isl_basic_set_factorizer(bset);
4380 isl_basic_set_free(bset);
4381 isl_factorizer_free(f);
4382 if (!f)
4383 isl_die(ctx, isl_error_unknown,
4384 "failed to construct factorizer", return -1);
4386 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
4387 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
4388 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
4389 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
4390 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
4391 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
4392 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
4393 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
4394 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
4395 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
4396 bset = isl_basic_set_read_from_str(ctx, str);
4397 f = isl_basic_set_factorizer(bset);
4398 isl_basic_set_free(bset);
4399 isl_factorizer_free(f);
4400 if (!f)
4401 isl_die(ctx, isl_error_unknown,
4402 "failed to construct factorizer", return -1);
4404 return 0;
4407 static isl_stat check_injective(__isl_take isl_map *map, void *user)
4409 int *injective = user;
4411 *injective = isl_map_is_injective(map);
4412 isl_map_free(map);
4414 if (*injective < 0 || !*injective)
4415 return isl_stat_error;
4417 return isl_stat_ok;
4420 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
4421 const char *r, const char *s, int tilable, int parallel)
4423 int i;
4424 isl_union_set *D;
4425 isl_union_map *W, *R, *S;
4426 isl_union_map *empty;
4427 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
4428 isl_union_map *validity, *proximity, *coincidence;
4429 isl_union_map *schedule;
4430 isl_union_map *test;
4431 isl_union_set *delta;
4432 isl_union_set *domain;
4433 isl_set *delta_set;
4434 isl_set *slice;
4435 isl_set *origin;
4436 isl_schedule_constraints *sc;
4437 isl_schedule *sched;
4438 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
4439 isl_size n;
4441 D = isl_union_set_read_from_str(ctx, d);
4442 W = isl_union_map_read_from_str(ctx, w);
4443 R = isl_union_map_read_from_str(ctx, r);
4444 S = isl_union_map_read_from_str(ctx, s);
4446 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
4447 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
4449 empty = isl_union_map_empty(isl_union_map_get_space(S));
4450 isl_union_map_compute_flow(isl_union_map_copy(R),
4451 isl_union_map_copy(W), empty,
4452 isl_union_map_copy(S),
4453 &dep_raw, NULL, NULL, NULL);
4454 isl_union_map_compute_flow(isl_union_map_copy(W),
4455 isl_union_map_copy(W),
4456 isl_union_map_copy(R),
4457 isl_union_map_copy(S),
4458 &dep_waw, &dep_war, NULL, NULL);
4460 dep = isl_union_map_union(dep_waw, dep_war);
4461 dep = isl_union_map_union(dep, dep_raw);
4462 validity = isl_union_map_copy(dep);
4463 coincidence = isl_union_map_copy(dep);
4464 proximity = isl_union_map_copy(dep);
4466 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
4467 sc = isl_schedule_constraints_set_validity(sc, validity);
4468 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
4469 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4470 sched = isl_schedule_constraints_compute_schedule(sc);
4471 schedule = isl_schedule_get_map(sched);
4472 isl_schedule_free(sched);
4473 isl_union_map_free(W);
4474 isl_union_map_free(R);
4475 isl_union_map_free(S);
4477 is_injection = 1;
4478 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
4480 domain = isl_union_map_domain(isl_union_map_copy(schedule));
4481 is_complete = isl_union_set_is_subset(D, domain);
4482 isl_union_set_free(D);
4483 isl_union_set_free(domain);
4485 test = isl_union_map_reverse(isl_union_map_copy(schedule));
4486 test = isl_union_map_apply_range(test, dep);
4487 test = isl_union_map_apply_range(test, schedule);
4489 delta = isl_union_map_deltas(test);
4490 n = isl_union_set_n_set(delta);
4491 if (n < 0) {
4492 isl_union_set_free(delta);
4493 return -1;
4495 if (n == 0) {
4496 is_tilable = 1;
4497 is_parallel = 1;
4498 is_nonneg = 1;
4499 isl_union_set_free(delta);
4500 } else {
4501 isl_size dim;
4503 delta_set = isl_set_from_union_set(delta);
4505 slice = isl_set_universe(isl_set_get_space(delta_set));
4506 for (i = 0; i < tilable; ++i)
4507 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
4508 is_tilable = isl_set_is_subset(delta_set, slice);
4509 isl_set_free(slice);
4511 slice = isl_set_universe(isl_set_get_space(delta_set));
4512 for (i = 0; i < parallel; ++i)
4513 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
4514 is_parallel = isl_set_is_subset(delta_set, slice);
4515 isl_set_free(slice);
4517 origin = isl_set_universe(isl_set_get_space(delta_set));
4518 dim = isl_set_dim(origin, isl_dim_set);
4519 if (dim < 0)
4520 origin = isl_set_free(origin);
4521 for (i = 0; i < dim; ++i)
4522 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
4524 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
4525 delta_set = isl_set_lexmin(delta_set);
4527 is_nonneg = isl_set_is_equal(delta_set, origin);
4529 isl_set_free(origin);
4530 isl_set_free(delta_set);
4533 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
4534 is_injection < 0 || is_complete < 0)
4535 return -1;
4536 if (!is_complete)
4537 isl_die(ctx, isl_error_unknown,
4538 "generated schedule incomplete", return -1);
4539 if (!is_injection)
4540 isl_die(ctx, isl_error_unknown,
4541 "generated schedule not injective on each statement",
4542 return -1);
4543 if (!is_nonneg)
4544 isl_die(ctx, isl_error_unknown,
4545 "negative dependences in generated schedule",
4546 return -1);
4547 if (!is_tilable)
4548 isl_die(ctx, isl_error_unknown,
4549 "generated schedule not as tilable as expected",
4550 return -1);
4551 if (!is_parallel)
4552 isl_die(ctx, isl_error_unknown,
4553 "generated schedule not as parallel as expected",
4554 return -1);
4556 return 0;
4559 /* Compute a schedule for the given instance set, validity constraints,
4560 * proximity constraints and context and return a corresponding union map
4561 * representation.
4563 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
4564 const char *domain, const char *validity, const char *proximity,
4565 const char *context)
4567 isl_set *con;
4568 isl_union_set *dom;
4569 isl_union_map *dep;
4570 isl_union_map *prox;
4571 isl_schedule_constraints *sc;
4572 isl_schedule *schedule;
4573 isl_union_map *sched;
4575 con = isl_set_read_from_str(ctx, context);
4576 dom = isl_union_set_read_from_str(ctx, domain);
4577 dep = isl_union_map_read_from_str(ctx, validity);
4578 prox = isl_union_map_read_from_str(ctx, proximity);
4579 sc = isl_schedule_constraints_on_domain(dom);
4580 sc = isl_schedule_constraints_set_context(sc, con);
4581 sc = isl_schedule_constraints_set_validity(sc, dep);
4582 sc = isl_schedule_constraints_set_proximity(sc, prox);
4583 schedule = isl_schedule_constraints_compute_schedule(sc);
4584 sched = isl_schedule_get_map(schedule);
4585 isl_schedule_free(schedule);
4587 return sched;
4590 /* Compute a schedule for the given instance set, validity constraints and
4591 * proximity constraints and return a corresponding union map representation.
4593 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
4594 const char *domain, const char *validity, const char *proximity)
4596 return compute_schedule_with_context(ctx, domain, validity, proximity,
4597 "{ : }");
4600 /* Check that a schedule can be constructed on the given domain
4601 * with the given validity and proximity constraints.
4603 static int test_has_schedule(isl_ctx *ctx, const char *domain,
4604 const char *validity, const char *proximity)
4606 isl_union_map *sched;
4608 sched = compute_schedule(ctx, domain, validity, proximity);
4609 if (!sched)
4610 return -1;
4612 isl_union_map_free(sched);
4613 return 0;
4616 int test_special_schedule(isl_ctx *ctx, const char *domain,
4617 const char *validity, const char *proximity, const char *expected_sched)
4619 isl_union_map *sched1, *sched2;
4620 int equal;
4622 sched1 = compute_schedule(ctx, domain, validity, proximity);
4623 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
4625 equal = isl_union_map_is_equal(sched1, sched2);
4626 isl_union_map_free(sched1);
4627 isl_union_map_free(sched2);
4629 if (equal < 0)
4630 return -1;
4631 if (!equal)
4632 isl_die(ctx, isl_error_unknown, "unexpected schedule",
4633 return -1);
4635 return 0;
4638 /* Check that the schedule map is properly padded, i.e., that the range
4639 * lives in a single space.
4641 static int test_padded_schedule(isl_ctx *ctx)
4643 const char *str;
4644 isl_union_set *D;
4645 isl_union_map *validity, *proximity;
4646 isl_schedule_constraints *sc;
4647 isl_schedule *sched;
4648 isl_union_map *umap;
4649 isl_union_set *range;
4650 isl_set *set;
4652 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
4653 D = isl_union_set_read_from_str(ctx, str);
4654 validity = isl_union_map_empty(isl_union_set_get_space(D));
4655 proximity = isl_union_map_copy(validity);
4656 sc = isl_schedule_constraints_on_domain(D);
4657 sc = isl_schedule_constraints_set_validity(sc, validity);
4658 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4659 sched = isl_schedule_constraints_compute_schedule(sc);
4660 umap = isl_schedule_get_map(sched);
4661 isl_schedule_free(sched);
4662 range = isl_union_map_range(umap);
4663 set = isl_set_from_union_set(range);
4664 isl_set_free(set);
4666 if (!set)
4667 return -1;
4669 return 0;
4672 /* Check that conditional validity constraints are also taken into
4673 * account across bands.
4674 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
4675 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
4676 * and then check that the adjacent order constraint C[2,1]->D[2,0]
4677 * is enforced by the rest of the schedule.
4679 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
4681 const char *str;
4682 isl_union_set *domain;
4683 isl_union_map *validity, *proximity, *condition;
4684 isl_union_map *sink, *source, *dep;
4685 isl_schedule_constraints *sc;
4686 isl_schedule *schedule;
4687 isl_union_access_info *access;
4688 isl_union_flow *flow;
4689 int empty;
4691 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4692 "A[k] : k >= 1 and k <= -1 + n; "
4693 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4694 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
4695 domain = isl_union_set_read_from_str(ctx, str);
4696 sc = isl_schedule_constraints_on_domain(domain);
4697 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
4698 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4699 "D[k, i] -> C[1 + k, i] : "
4700 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4701 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
4702 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
4703 validity = isl_union_map_read_from_str(ctx, str);
4704 sc = isl_schedule_constraints_set_validity(sc, validity);
4705 str = "[n] -> { C[k, i] -> D[k, i] : "
4706 "0 <= i <= -1 + k and k <= -1 + n }";
4707 proximity = isl_union_map_read_from_str(ctx, str);
4708 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4709 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
4710 "i <= -1 + k and i >= 1 and k <= -2 + n; "
4711 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
4712 "k <= -1 + n and i >= 0 and i <= -2 + k }";
4713 condition = isl_union_map_read_from_str(ctx, str);
4714 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
4715 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4716 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
4717 "i >= 0 and i <= -1 + k and k <= -1 + n and "
4718 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
4719 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
4720 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4721 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
4722 "k <= -1 + n and i >= 0 and i <= -1 + k and "
4723 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
4724 validity = isl_union_map_read_from_str(ctx, str);
4725 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4726 validity);
4727 schedule = isl_schedule_constraints_compute_schedule(sc);
4728 str = "{ D[2,0] -> [] }";
4729 sink = isl_union_map_read_from_str(ctx, str);
4730 access = isl_union_access_info_from_sink(sink);
4731 str = "{ C[2,1] -> [] }";
4732 source = isl_union_map_read_from_str(ctx, str);
4733 access = isl_union_access_info_set_must_source(access, source);
4734 access = isl_union_access_info_set_schedule(access, schedule);
4735 flow = isl_union_access_info_compute_flow(access);
4736 dep = isl_union_flow_get_must_dependence(flow);
4737 isl_union_flow_free(flow);
4738 empty = isl_union_map_is_empty(dep);
4739 isl_union_map_free(dep);
4741 if (empty < 0)
4742 return -1;
4743 if (empty)
4744 isl_die(ctx, isl_error_unknown,
4745 "conditional validity not respected", return -1);
4747 return 0;
4750 /* Check that the test for violated conditional validity constraints
4751 * is not confused by domain compression.
4752 * In particular, earlier versions of isl would apply
4753 * a schedule on the compressed domains to the original domains,
4754 * resulting in a failure to detect that the default schedule
4755 * violates the conditional validity constraints.
4757 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
4759 const char *str;
4760 isl_bool empty;
4761 isl_union_set *domain;
4762 isl_union_map *validity, *condition;
4763 isl_schedule_constraints *sc;
4764 isl_schedule *schedule;
4765 isl_union_map *umap;
4766 isl_map *map, *ge;
4768 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
4769 domain = isl_union_set_read_from_str(ctx, str);
4770 sc = isl_schedule_constraints_on_domain(domain);
4771 str = "{ B[1, i] -> A[0, i + 1] }";
4772 condition = isl_union_map_read_from_str(ctx, str);
4773 str = "{ A[0, i] -> B[1, i - 1] }";
4774 validity = isl_union_map_read_from_str(ctx, str);
4775 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4776 isl_union_map_copy(validity));
4777 schedule = isl_schedule_constraints_compute_schedule(sc);
4778 umap = isl_schedule_get_map(schedule);
4779 isl_schedule_free(schedule);
4780 validity = isl_union_map_apply_domain(validity,
4781 isl_union_map_copy(umap));
4782 validity = isl_union_map_apply_range(validity, umap);
4783 map = isl_map_from_union_map(validity);
4784 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4785 map = isl_map_intersect(map, ge);
4786 empty = isl_map_is_empty(map);
4787 isl_map_free(map);
4789 if (empty < 0)
4790 return -1;
4791 if (!empty)
4792 isl_die(ctx, isl_error_unknown,
4793 "conditional validity constraints not satisfied",
4794 return -1);
4796 return 0;
4799 /* Input for testing of schedule construction based on
4800 * conditional constraints.
4802 * domain is the iteration domain
4803 * flow are the flow dependences, which determine the validity and
4804 * proximity constraints
4805 * condition are the conditions on the conditional validity constraints
4806 * conditional_validity are the conditional validity constraints
4807 * outer_band_n is the expected number of members in the outer band
4809 struct {
4810 const char *domain;
4811 const char *flow;
4812 const char *condition;
4813 const char *conditional_validity;
4814 int outer_band_n;
4815 } live_range_tests[] = {
4816 /* Contrived example that illustrates that we need to keep
4817 * track of tagged condition dependences and
4818 * tagged conditional validity dependences
4819 * in isl_sched_edge separately.
4820 * In particular, the conditional validity constraints on A
4821 * cannot be satisfied,
4822 * but they can be ignored because there are no corresponding
4823 * condition constraints. However, we do have an additional
4824 * conditional validity constraint that maps to the same
4825 * dependence relation
4826 * as the condition constraint on B. If we did not make a distinction
4827 * between tagged condition and tagged conditional validity
4828 * dependences, then we
4829 * could end up treating this shared dependence as an condition
4830 * constraint on A, forcing a localization of the conditions,
4831 * which is impossible.
4833 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
4834 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
4835 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
4836 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4837 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4838 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
4841 /* TACO 2013 Fig. 7 */
4842 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4843 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4844 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4845 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
4846 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
4847 "0 <= i < n and 0 <= j < n - 1 }",
4848 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
4849 "0 <= i < n and 0 <= j < j' < n;"
4850 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
4851 "0 <= i < i' < n and 0 <= j,j' < n;"
4852 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
4853 "0 <= i,j,j' < n and j < j' }",
4856 /* TACO 2013 Fig. 7, without tags */
4857 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4858 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4859 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4860 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4861 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4862 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
4863 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
4864 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
4867 /* TACO 2013 Fig. 12 */
4868 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
4869 "S3[i,3] : 0 <= i <= 1 }",
4870 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
4871 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
4872 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
4873 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
4874 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4875 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
4876 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4877 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
4878 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
4879 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
4880 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
4885 /* Test schedule construction based on conditional constraints.
4886 * In particular, check the number of members in the outer band node
4887 * as an indication of whether tiling is possible or not.
4889 static int test_conditional_schedule_constraints(isl_ctx *ctx)
4891 int i;
4892 isl_union_set *domain;
4893 isl_union_map *condition;
4894 isl_union_map *flow;
4895 isl_union_map *validity;
4896 isl_schedule_constraints *sc;
4897 isl_schedule *schedule;
4898 isl_schedule_node *node;
4899 isl_size n_member;
4901 if (test_special_conditional_schedule_constraints(ctx) < 0)
4902 return -1;
4903 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
4904 return -1;
4906 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
4907 domain = isl_union_set_read_from_str(ctx,
4908 live_range_tests[i].domain);
4909 flow = isl_union_map_read_from_str(ctx,
4910 live_range_tests[i].flow);
4911 condition = isl_union_map_read_from_str(ctx,
4912 live_range_tests[i].condition);
4913 validity = isl_union_map_read_from_str(ctx,
4914 live_range_tests[i].conditional_validity);
4915 sc = isl_schedule_constraints_on_domain(domain);
4916 sc = isl_schedule_constraints_set_validity(sc,
4917 isl_union_map_copy(flow));
4918 sc = isl_schedule_constraints_set_proximity(sc, flow);
4919 sc = isl_schedule_constraints_set_conditional_validity(sc,
4920 condition, validity);
4921 schedule = isl_schedule_constraints_compute_schedule(sc);
4922 node = isl_schedule_get_root(schedule);
4923 while (node &&
4924 isl_schedule_node_get_type(node) != isl_schedule_node_band)
4925 node = isl_schedule_node_first_child(node);
4926 n_member = isl_schedule_node_band_n_member(node);
4927 isl_schedule_node_free(node);
4928 isl_schedule_free(schedule);
4930 if (!schedule || n_member < 0)
4931 return -1;
4932 if (n_member != live_range_tests[i].outer_band_n)
4933 isl_die(ctx, isl_error_unknown,
4934 "unexpected number of members in outer band",
4935 return -1);
4937 return 0;
4940 /* Check that the schedule computed for the given instance set and
4941 * dependence relation strongly satisfies the dependences.
4942 * In particular, check that no instance is scheduled before
4943 * or together with an instance on which it depends.
4944 * Earlier versions of isl would produce a schedule that
4945 * only weakly satisfies the dependences.
4947 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
4949 const char *domain, *dep;
4950 isl_union_map *D, *schedule;
4951 isl_map *map, *ge;
4952 int empty;
4954 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
4955 "A[i0] : 0 <= i0 <= 1 }";
4956 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
4957 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
4958 schedule = compute_schedule(ctx, domain, dep, dep);
4959 D = isl_union_map_read_from_str(ctx, dep);
4960 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
4961 D = isl_union_map_apply_range(D, schedule);
4962 map = isl_map_from_union_map(D);
4963 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4964 map = isl_map_intersect(map, ge);
4965 empty = isl_map_is_empty(map);
4966 isl_map_free(map);
4968 if (empty < 0)
4969 return -1;
4970 if (!empty)
4971 isl_die(ctx, isl_error_unknown,
4972 "dependences not strongly satisfied", return -1);
4974 return 0;
4977 /* Compute a schedule for input where the instance set constraints
4978 * conflict with the context constraints.
4979 * Earlier versions of isl did not properly handle this situation.
4981 static int test_conflicting_context_schedule(isl_ctx *ctx)
4983 isl_union_map *schedule;
4984 const char *domain, *context;
4986 domain = "[n] -> { A[] : n >= 0 }";
4987 context = "[n] -> { : n < 0 }";
4988 schedule = compute_schedule_with_context(ctx,
4989 domain, "{}", "{}", context);
4990 isl_union_map_free(schedule);
4992 if (!schedule)
4993 return -1;
4995 return 0;
4998 /* Check that a set of schedule constraints that only allow for
4999 * a coalescing schedule still produces a schedule even if the user
5000 * request a non-coalescing schedule. Earlier versions of isl
5001 * would not handle this case correctly.
5003 static int test_coalescing_schedule(isl_ctx *ctx)
5005 const char *domain, *dep;
5006 isl_union_set *I;
5007 isl_union_map *D;
5008 isl_schedule_constraints *sc;
5009 isl_schedule *schedule;
5010 int treat_coalescing;
5012 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
5013 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
5014 I = isl_union_set_read_from_str(ctx, domain);
5015 D = isl_union_map_read_from_str(ctx, dep);
5016 sc = isl_schedule_constraints_on_domain(I);
5017 sc = isl_schedule_constraints_set_validity(sc, D);
5018 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
5019 isl_options_set_schedule_treat_coalescing(ctx, 1);
5020 schedule = isl_schedule_constraints_compute_schedule(sc);
5021 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
5022 isl_schedule_free(schedule);
5023 if (!schedule)
5024 return -1;
5025 return 0;
5028 /* Check that the scheduler does not perform any needless
5029 * compound skewing. Earlier versions of isl would compute
5030 * schedules in terms of transformed schedule coefficients and
5031 * would not accurately keep track of the sum of the original
5032 * schedule coefficients. It could then produce the schedule
5033 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
5034 * for the input below instead of the schedule below.
5036 static int test_skewing_schedule(isl_ctx *ctx)
5038 const char *D, *V, *P, *S;
5040 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
5041 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
5042 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
5043 "-1 <= a-i + b-j + c-k <= 1 }";
5044 P = "{ }";
5045 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
5047 return test_special_schedule(ctx, D, V, P, S);
5050 int test_schedule(isl_ctx *ctx)
5052 const char *D, *W, *R, *V, *P, *S;
5053 int max_coincidence;
5054 int treat_coalescing;
5056 /* Handle resulting schedule with zero bands. */
5057 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
5058 return -1;
5060 /* Jacobi */
5061 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
5062 W = "{ S1[t,i] -> a[t,i] }";
5063 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
5064 "S1[t,i] -> a[t-1,i+1] }";
5065 S = "{ S1[t,i] -> [t,i] }";
5066 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
5067 return -1;
5069 /* Fig. 5 of CC2008 */
5070 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
5071 "j <= -1 + N }";
5072 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
5073 "j >= 2 and j <= -1 + N }";
5074 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
5075 "j >= 2 and j <= -1 + N; "
5076 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
5077 "j >= 2 and j <= -1 + N }";
5078 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
5079 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
5080 return -1;
5082 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
5083 W = "{ S1[i] -> a[i] }";
5084 R = "{ S2[i] -> a[i+1] }";
5085 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
5086 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
5087 return -1;
5089 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
5090 W = "{ S1[i] -> a[i] }";
5091 R = "{ S2[i] -> a[9-i] }";
5092 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
5093 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
5094 return -1;
5096 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
5097 W = "{ S1[i] -> a[i] }";
5098 R = "[N] -> { S2[i] -> a[N-1-i] }";
5099 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
5100 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
5101 return -1;
5103 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
5104 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
5105 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
5106 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
5107 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5108 return -1;
5110 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
5111 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
5112 R = "{ S2[i,j] -> a[i-1,j] }";
5113 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
5114 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
5115 return -1;
5117 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
5118 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
5119 R = "{ S2[i,j] -> a[i,j-1] }";
5120 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
5121 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
5122 return -1;
5124 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
5125 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
5126 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
5127 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
5128 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
5129 "S_0[] -> [0, 0, 0] }";
5130 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
5131 return -1;
5132 ctx->opt->schedule_parametric = 0;
5133 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5134 return -1;
5135 ctx->opt->schedule_parametric = 1;
5137 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
5138 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
5139 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
5140 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
5141 "S4[i] -> a[i,N] }";
5142 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
5143 "S4[i] -> [4,i,0] }";
5144 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
5145 isl_options_set_schedule_maximize_coincidence(ctx, 0);
5146 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
5147 return -1;
5148 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
5150 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
5151 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
5152 "j <= N }";
5153 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
5154 "j <= N; "
5155 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
5156 "j <= N }";
5157 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
5158 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5159 return -1;
5161 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
5162 " S_2[t] : t >= 0 and t <= -1 + N; "
5163 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
5164 "i <= -1 + N }";
5165 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
5166 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
5167 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
5168 "i >= 0 and i <= -1 + N }";
5169 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
5170 "i >= 0 and i <= -1 + N; "
5171 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
5172 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
5173 " S_0[t] -> [0, t, 0] }";
5175 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
5176 return -1;
5177 ctx->opt->schedule_parametric = 0;
5178 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5179 return -1;
5180 ctx->opt->schedule_parametric = 1;
5182 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
5183 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
5184 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
5185 return -1;
5187 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
5188 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
5189 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
5190 "j >= 0 and j <= -1 + N; "
5191 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
5192 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
5193 "j >= 0 and j <= -1 + N; "
5194 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
5195 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
5196 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5197 return -1;
5199 D = "{ S_0[i] : i >= 0 }";
5200 W = "{ S_0[i] -> a[i] : i >= 0 }";
5201 R = "{ S_0[i] -> a[0] : i >= 0 }";
5202 S = "{ S_0[i] -> [0, i, 0] }";
5203 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5204 return -1;
5206 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
5207 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
5208 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
5209 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
5210 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5211 return -1;
5213 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
5214 "k <= -1 + n and k >= 0 }";
5215 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
5216 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
5217 "k <= -1 + n and k >= 0; "
5218 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
5219 "k <= -1 + n and k >= 0; "
5220 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
5221 "k <= -1 + n and k >= 0 }";
5222 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
5223 ctx->opt->schedule_outer_coincidence = 1;
5224 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5225 return -1;
5226 ctx->opt->schedule_outer_coincidence = 0;
5228 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
5229 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
5230 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
5231 "Stmt_for_body24[i0, i1, 1, 0]:"
5232 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
5233 "Stmt_for_body7[i0, i1, i2]:"
5234 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
5235 "i2 <= 7 }";
5237 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
5238 "Stmt_for_body24[1, i1, i2, i3]:"
5239 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
5240 "i2 >= 1;"
5241 "Stmt_for_body24[0, i1, i2, i3] -> "
5242 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
5243 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
5244 "i3 >= 0;"
5245 "Stmt_for_body24[0, i1, i2, i3] ->"
5246 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
5247 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
5248 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
5249 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
5250 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
5251 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
5252 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
5253 "i1 <= 6 and i1 >= 0;"
5254 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
5255 "Stmt_for_body7[i0, i1, i2] -> "
5256 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
5257 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
5258 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
5259 "Stmt_for_body7[i0, i1, i2] -> "
5260 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
5261 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
5262 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
5263 P = V;
5265 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
5266 isl_options_set_schedule_treat_coalescing(ctx, 0);
5267 if (test_has_schedule(ctx, D, V, P) < 0)
5268 return -1;
5269 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
5271 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
5272 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
5273 "j >= 1 and j <= 7;"
5274 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
5275 "j >= 1 and j <= 8 }";
5276 P = "{ }";
5277 S = "{ S_0[i, j] -> [i + j, i] }";
5278 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5279 if (test_special_schedule(ctx, D, V, P, S) < 0)
5280 return -1;
5281 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5283 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
5284 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
5285 "j >= 0 and j <= -1 + i }";
5286 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
5287 "i <= -1 + N and j >= 0;"
5288 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
5289 "i <= -2 + N }";
5290 P = "{ }";
5291 S = "{ S_0[i, j] -> [i, j] }";
5292 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5293 if (test_special_schedule(ctx, D, V, P, S) < 0)
5294 return -1;
5295 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5297 /* Test both algorithms on a case with only proximity dependences. */
5298 D = "{ S[i,j] : 0 <= i <= 10 }";
5299 V = "{ }";
5300 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
5301 S = "{ S[i, j] -> [j, i] }";
5302 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5303 if (test_special_schedule(ctx, D, V, P, S) < 0)
5304 return -1;
5305 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5306 if (test_special_schedule(ctx, D, V, P, S) < 0)
5307 return -1;
5309 D = "{ A[a]; B[] }";
5310 V = "{}";
5311 P = "{ A[a] -> B[] }";
5312 if (test_has_schedule(ctx, D, V, P) < 0)
5313 return -1;
5315 if (test_padded_schedule(ctx) < 0)
5316 return -1;
5318 /* Check that check for progress is not confused by rational
5319 * solution.
5321 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
5322 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
5323 "i0 <= -2 + N; "
5324 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
5325 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
5326 P = "{}";
5327 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5328 if (test_has_schedule(ctx, D, V, P) < 0)
5329 return -1;
5330 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5332 /* Check that we allow schedule rows that are only non-trivial
5333 * on some full-dimensional domains.
5335 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
5336 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
5337 "S1[j] -> S2[1] : 0 <= j <= 1 }";
5338 P = "{}";
5339 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5340 if (test_has_schedule(ctx, D, V, P) < 0)
5341 return -1;
5342 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5344 if (test_conditional_schedule_constraints(ctx) < 0)
5345 return -1;
5347 if (test_strongly_satisfying_schedule(ctx) < 0)
5348 return -1;
5350 if (test_conflicting_context_schedule(ctx) < 0)
5351 return -1;
5353 if (test_coalescing_schedule(ctx) < 0)
5354 return -1;
5355 if (test_skewing_schedule(ctx) < 0)
5356 return -1;
5358 return 0;
5361 /* Perform scheduling tests using the whole component scheduler.
5363 static int test_schedule_whole(isl_ctx *ctx)
5365 int whole;
5366 int r;
5368 whole = isl_options_get_schedule_whole_component(ctx);
5369 isl_options_set_schedule_whole_component(ctx, 1);
5370 r = test_schedule(ctx);
5371 isl_options_set_schedule_whole_component(ctx, whole);
5373 return r;
5376 /* Perform scheduling tests using the incremental scheduler.
5378 static int test_schedule_incremental(isl_ctx *ctx)
5380 int whole;
5381 int r;
5383 whole = isl_options_get_schedule_whole_component(ctx);
5384 isl_options_set_schedule_whole_component(ctx, 0);
5385 r = test_schedule(ctx);
5386 isl_options_set_schedule_whole_component(ctx, whole);
5388 return r;
5391 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
5393 isl_union_map *umap;
5394 int test;
5396 umap = isl_union_map_read_from_str(ctx, str);
5397 test = isl_union_map_plain_is_injective(umap);
5398 isl_union_map_free(umap);
5399 if (test < 0)
5400 return -1;
5401 if (test == injective)
5402 return 0;
5403 if (injective)
5404 isl_die(ctx, isl_error_unknown,
5405 "map not detected as injective", return -1);
5406 else
5407 isl_die(ctx, isl_error_unknown,
5408 "map detected as injective", return -1);
5411 int test_injective(isl_ctx *ctx)
5413 const char *str;
5415 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
5416 return -1;
5417 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
5418 return -1;
5419 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
5420 return -1;
5421 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
5422 return -1;
5423 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
5424 return -1;
5425 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
5426 return -1;
5427 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
5428 return -1;
5429 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
5430 return -1;
5432 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
5433 if (test_plain_injective(ctx, str, 1))
5434 return -1;
5435 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
5436 if (test_plain_injective(ctx, str, 0))
5437 return -1;
5439 return 0;
5442 #undef BASE
5443 #define BASE aff
5444 #include "isl_test_plain_equal_templ.c"
5446 #undef BASE
5447 #define BASE pw_multi_aff
5448 #include "isl_test_plain_equal_templ.c"
5450 #undef BASE
5451 #define BASE union_pw_aff
5452 #include "isl_test_plain_equal_templ.c"
5454 /* Basic tests on isl_union_pw_aff.
5456 * In particular, check that isl_union_pw_aff_aff_on_domain
5457 * aligns the parameters of the input objects and
5458 * that isl_union_pw_aff_param_on_domain_id properly
5459 * introduces the parameter.
5461 static int test_upa(isl_ctx *ctx)
5463 const char *str;
5464 isl_id *id;
5465 isl_aff *aff;
5466 isl_union_set *domain;
5467 isl_union_pw_aff *upa;
5468 isl_stat ok;
5470 aff = isl_aff_read_from_str(ctx, "[N] -> { [N] }");
5471 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5472 domain = isl_union_set_read_from_str(ctx, str);
5473 upa = isl_union_pw_aff_aff_on_domain(domain, aff);
5474 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5475 ok = union_pw_aff_check_plain_equal(upa, str);
5476 isl_union_pw_aff_free(upa);
5477 if (ok < 0)
5478 return -1;
5480 id = isl_id_alloc(ctx, "N", NULL);
5481 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5482 domain = isl_union_set_read_from_str(ctx, str);
5483 upa = isl_union_pw_aff_param_on_domain_id(domain, id);
5484 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5485 ok = union_pw_aff_check_plain_equal(upa, str);
5486 isl_union_pw_aff_free(upa);
5487 if (ok < 0)
5488 return -1;
5490 return 0;
5493 struct {
5494 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5495 __isl_take isl_aff *aff2);
5496 } aff_bin_op[] = {
5497 ['+'] = { &isl_aff_add },
5498 ['-'] = { &isl_aff_sub },
5499 ['*'] = { &isl_aff_mul },
5500 ['/'] = { &isl_aff_div },
5503 struct {
5504 const char *arg1;
5505 unsigned char op;
5506 const char *arg2;
5507 const char *res;
5508 } aff_bin_tests[] = {
5509 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
5510 "{ [i] -> [2i] }" },
5511 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
5512 "{ [i] -> [0] }" },
5513 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
5514 "{ [i] -> [2i] }" },
5515 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
5516 "{ [i] -> [2i] }" },
5517 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
5518 "{ [i] -> [i/2] }" },
5519 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
5520 "{ [i] -> [i] }" },
5521 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
5522 "{ [i] -> [NaN] }" },
5523 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
5524 "{ [i] -> [NaN] }" },
5525 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
5526 "{ [i] -> [NaN] }" },
5527 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
5528 "{ [i] -> [NaN] }" },
5529 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
5530 "{ [i] -> [NaN] }" },
5531 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
5532 "{ [i] -> [NaN] }" },
5533 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
5534 "{ [i] -> [NaN] }" },
5535 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
5536 "{ [i] -> [NaN] }" },
5537 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
5538 "{ [i] -> [NaN] }" },
5539 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
5540 "{ [i] -> [NaN] }" },
5541 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
5542 "{ [i] -> [NaN] }" },
5543 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
5544 "{ [i] -> [NaN] }" },
5545 { "{ [i] -> [i] }", '/', "{ [i] -> [0] }",
5546 "{ [i] -> [NaN] }" },
5549 /* Perform some basic tests of binary operations on isl_aff objects.
5551 static int test_bin_aff(isl_ctx *ctx)
5553 int i;
5554 isl_aff *aff1, *aff2, *res;
5555 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5556 __isl_take isl_aff *aff2);
5557 int ok;
5559 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
5560 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
5561 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
5562 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
5563 fn = aff_bin_op[aff_bin_tests[i].op].fn;
5564 aff1 = fn(aff1, aff2);
5565 if (isl_aff_is_nan(res))
5566 ok = isl_aff_is_nan(aff1);
5567 else
5568 ok = isl_aff_plain_is_equal(aff1, res);
5569 isl_aff_free(aff1);
5570 isl_aff_free(res);
5571 if (ok < 0)
5572 return -1;
5573 if (!ok)
5574 isl_die(ctx, isl_error_unknown,
5575 "unexpected result", return -1);
5578 return 0;
5581 struct {
5582 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
5583 __isl_take isl_pw_aff *pa2);
5584 } pw_aff_bin_op[] = {
5585 ['m'] = { &isl_pw_aff_min },
5586 ['M'] = { &isl_pw_aff_max },
5589 /* Inputs for binary isl_pw_aff operation tests.
5590 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
5591 * defined by pw_aff_bin_op, and "res" is the expected result.
5593 struct {
5594 const char *arg1;
5595 unsigned char op;
5596 const char *arg2;
5597 const char *res;
5598 } pw_aff_bin_tests[] = {
5599 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
5600 "{ [i] -> [i] }" },
5601 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
5602 "{ [i] -> [i] }" },
5603 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
5604 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
5605 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
5606 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
5607 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
5608 "{ [i] -> [NaN] }" },
5609 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
5610 "{ [i] -> [NaN] }" },
5613 /* Perform some basic tests of binary operations on isl_pw_aff objects.
5615 static int test_bin_pw_aff(isl_ctx *ctx)
5617 int i;
5618 isl_bool ok;
5619 isl_pw_aff *pa1, *pa2, *res;
5621 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
5622 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
5623 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
5624 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
5625 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
5626 if (isl_pw_aff_involves_nan(res))
5627 ok = isl_pw_aff_involves_nan(pa1);
5628 else
5629 ok = isl_pw_aff_plain_is_equal(pa1, res);
5630 isl_pw_aff_free(pa1);
5631 isl_pw_aff_free(res);
5632 if (ok < 0)
5633 return -1;
5634 if (!ok)
5635 isl_die(ctx, isl_error_unknown,
5636 "unexpected result", return -1);
5639 return 0;
5642 /* Inputs for basic tests of test operations on
5643 * isl_union_pw_multi_aff objects.
5644 * "fn" is the function that is being tested.
5645 * "arg" is a string description of the input.
5646 * "res" is the expected result.
5648 static struct {
5649 isl_bool (*fn)(__isl_keep isl_union_pw_multi_aff *upma1);
5650 const char *arg;
5651 isl_bool res;
5652 } upma_test_tests[] = {
5653 { &isl_union_pw_multi_aff_involves_nan, "{ A[] -> [0]; B[0] -> [1] }",
5654 isl_bool_false },
5655 { &isl_union_pw_multi_aff_involves_nan, "{ A[] -> [NaN]; B[0] -> [1] }",
5656 isl_bool_true },
5657 { &isl_union_pw_multi_aff_involves_nan, "{ A[] -> [0]; B[0] -> [NaN] }",
5658 isl_bool_true },
5659 { &isl_union_pw_multi_aff_involves_nan,
5660 "{ A[] -> [0]; B[0] -> [1, NaN, 5] }",
5661 isl_bool_true },
5662 { &isl_union_pw_multi_aff_involves_locals,
5663 "{ A[] -> [0]; B[0] -> [1] }",
5664 isl_bool_false },
5665 { &isl_union_pw_multi_aff_involves_locals,
5666 "{ A[] -> [0]; B[x] -> [1] : x mod 2 = 0 }",
5667 isl_bool_true },
5668 { &isl_union_pw_multi_aff_involves_locals,
5669 "{ A[] -> [0]; B[x] -> [x // 2] }",
5670 isl_bool_true },
5671 { &isl_union_pw_multi_aff_involves_locals,
5672 "{ A[i] -> [i // 2]; B[0] -> [1] }",
5673 isl_bool_true },
5676 /* Perform some basic tests of test operations on
5677 * isl_union_pw_multi_aff objects.
5679 static isl_stat test_upma_test(isl_ctx *ctx)
5681 int i;
5682 isl_union_pw_multi_aff *upma;
5683 isl_bool res;
5685 for (i = 0; i < ARRAY_SIZE(upma_test_tests); ++i) {
5686 const char *str;
5688 str = upma_test_tests[i].arg;
5689 upma = isl_union_pw_multi_aff_read_from_str(ctx, str);
5690 res = upma_test_tests[i].fn(upma);
5691 isl_union_pw_multi_aff_free(upma);
5692 if (res < 0)
5693 return isl_stat_error;
5694 if (res != upma_test_tests[i].res)
5695 isl_die(ctx, isl_error_unknown,
5696 "unexpected result", return isl_stat_error);
5699 return isl_stat_ok;
5702 struct {
5703 __isl_give isl_union_pw_multi_aff *(*fn)(
5704 __isl_take isl_union_pw_multi_aff *upma1,
5705 __isl_take isl_union_pw_multi_aff *upma2);
5706 const char *arg1;
5707 const char *arg2;
5708 const char *res;
5709 } upma_bin_tests[] = {
5710 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
5711 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
5712 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
5713 "{ B[x] -> [2] : x >= 0 }",
5714 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
5715 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
5716 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
5717 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
5718 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
5719 "D[i] -> B[2] : i >= 5 }" },
5720 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5721 "{ B[x] -> C[2] : x > 0 }",
5722 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
5723 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5724 "{ B[x] -> A[2] : x >= 0 }",
5725 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
5727 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5728 "{ B[x] -> C[x + 2] }",
5729 "{ D[y] -> B[2y] }",
5730 "{ }" },
5732 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5733 "{ [A[x] -> B[x + 1]] -> C[x + 2] }",
5734 "{ D[y] -> B[2y] }",
5735 "{ }" },
5737 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5738 "{ [A[x] -> B[x + 1]] -> C[x + 2]; B[x] -> C[x + 2] }",
5739 "{ D[y] -> A[2y] }",
5740 "{ [D[y] -> B[2y + 1]] -> C[2y + 2] }" },
5742 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5743 "{ T[A[x] -> B[x + 1]] -> C[x + 2]; B[x] -> C[x + 2] }",
5744 "{ D[y] -> A[2y] }",
5745 "{ T[D[y] -> B[2y + 1]] -> C[2y + 2] }" },
5748 /* Perform some basic tests of binary operations on
5749 * isl_union_pw_multi_aff objects.
5751 static int test_bin_upma(isl_ctx *ctx)
5753 int i;
5754 isl_union_pw_multi_aff *upma1, *upma2, *res;
5755 int ok;
5757 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
5758 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5759 upma_bin_tests[i].arg1);
5760 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5761 upma_bin_tests[i].arg2);
5762 res = isl_union_pw_multi_aff_read_from_str(ctx,
5763 upma_bin_tests[i].res);
5764 upma1 = upma_bin_tests[i].fn(upma1, upma2);
5765 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
5766 isl_union_pw_multi_aff_free(upma1);
5767 isl_union_pw_multi_aff_free(res);
5768 if (ok < 0)
5769 return -1;
5770 if (!ok)
5771 isl_die(ctx, isl_error_unknown,
5772 "unexpected result", return -1);
5775 return 0;
5778 struct {
5779 __isl_give isl_union_pw_multi_aff *(*fn)(
5780 __isl_take isl_union_pw_multi_aff *upma1,
5781 __isl_take isl_union_pw_multi_aff *upma2);
5782 const char *arg1;
5783 const char *arg2;
5784 } upma_bin_fail_tests[] = {
5785 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5786 "{ B[x] -> C[2] : x >= 0 }" },
5789 /* Perform some basic tests of binary operations on
5790 * isl_union_pw_multi_aff objects that are expected to fail.
5792 static int test_bin_upma_fail(isl_ctx *ctx)
5794 int i, n;
5795 isl_union_pw_multi_aff *upma1, *upma2;
5796 int on_error;
5798 on_error = isl_options_get_on_error(ctx);
5799 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
5800 n = ARRAY_SIZE(upma_bin_fail_tests);
5801 for (i = 0; i < n; ++i) {
5802 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5803 upma_bin_fail_tests[i].arg1);
5804 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5805 upma_bin_fail_tests[i].arg2);
5806 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
5807 isl_union_pw_multi_aff_free(upma1);
5808 if (upma1)
5809 break;
5811 isl_options_set_on_error(ctx, on_error);
5812 if (i < n)
5813 isl_die(ctx, isl_error_unknown,
5814 "operation not expected to succeed", return -1);
5816 return 0;
5819 /* Inputs for basic tests of binary operations on
5820 * pairs of isl_union_pw_multi_aff and isl_union_set objects.
5821 * "fn" is the function that is being tested.
5822 * "arg1" and "arg2" are string descriptions of the inputs.
5823 * "res" is a string description of the expected result.
5825 struct {
5826 __isl_give isl_union_pw_multi_aff *(*fn)(
5827 __isl_take isl_union_pw_multi_aff *upma,
5828 __isl_take isl_union_set *uset);
5829 const char *arg1;
5830 const char *arg2;
5831 const char *res;
5832 } upma_uset_tests[] = {
5833 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5834 "{ A[i] -> B[i] }", "{ B[0] }",
5835 "{ }" },
5836 { &isl_union_pw_multi_aff_intersect_domain_wrapped_domain,
5837 "{ [A[i] -> B[i]] -> C[i + 1] }", "{ A[1]; B[0] }",
5838 "{ [A[1] -> B[1]] -> C[2] }" },
5839 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5840 "{ [A[i] -> B[i]] -> C[i + 1] }", "{ A[1]; B[0] }",
5841 "{ [A[0] -> B[0]] -> C[1] }" },
5842 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5843 "{ [A[i] -> B[i]] -> C[i + 1] }", "[N] -> { B[N] }",
5844 "[N] -> { [A[N] -> B[N]] -> C[N + 1] }" },
5845 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5846 "[M] -> { [A[M] -> B[M]] -> C[M + 1] }", "[N] -> { B[N] }",
5847 "[N, M] -> { [A[N] -> B[N]] -> C[N + 1] : N = M }" },
5848 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5849 "{ [A[] -> B[]] -> C[]; N[A[] -> B[]] -> D[]; [B[] -> A[]] -> E[] }",
5850 "{ B[] }",
5851 "{ [A[] -> B[]] -> C[]; N[A[] -> B[]] -> D[] }" },
5854 /* Perform some basic tests of binary operations on
5855 * pairs of isl_union_pw_multi_aff and isl_union_set objects.
5857 static isl_stat test_upma_uset(isl_ctx *ctx)
5859 int i;
5860 isl_bool ok;
5861 isl_union_pw_multi_aff *upma, *res;
5862 isl_union_set *uset;
5864 for (i = 0; i < ARRAY_SIZE(upma_uset_tests); ++i) {
5865 upma = isl_union_pw_multi_aff_read_from_str(ctx,
5866 upma_uset_tests[i].arg1);
5867 uset = isl_union_set_read_from_str(ctx,
5868 upma_uset_tests[i].arg2);
5869 res = isl_union_pw_multi_aff_read_from_str(ctx,
5870 upma_uset_tests[i].res);
5871 upma = upma_uset_tests[i].fn(upma, uset);
5872 ok = isl_union_pw_multi_aff_plain_is_equal(upma, res);
5873 isl_union_pw_multi_aff_free(upma);
5874 isl_union_pw_multi_aff_free(res);
5875 if (ok < 0)
5876 return isl_stat_error;
5877 if (!ok)
5878 isl_die(ctx, isl_error_unknown,
5879 "unexpected result", return isl_stat_error);
5882 return isl_stat_ok;
5885 /* Inputs for basic tests of unary operations on isl_multi_pw_aff objects.
5886 * "fn" is the function that is tested.
5887 * "arg" is a string description of the input.
5888 * "res" is a string description of the expected result.
5890 struct {
5891 __isl_give isl_multi_pw_aff *(*fn)(__isl_take isl_multi_pw_aff *mpa);
5892 const char *arg;
5893 const char *res;
5894 } mpa_un_tests[] = {
5895 { &isl_multi_pw_aff_range_factor_domain,
5896 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5897 "{ A[x] -> B[(1 : x >= 5)] }" },
5898 { &isl_multi_pw_aff_range_factor_range,
5899 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5900 "{ A[y] -> C[(2 : y <= 10)] }" },
5901 { &isl_multi_pw_aff_range_factor_domain,
5902 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5903 "{ A[x] -> B[(1 : x >= 5)] }" },
5904 { &isl_multi_pw_aff_range_factor_range,
5905 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5906 "{ A[y] -> C[] }" },
5907 { &isl_multi_pw_aff_range_factor_domain,
5908 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5909 "{ A[x] -> B[] }" },
5910 { &isl_multi_pw_aff_range_factor_range,
5911 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5912 "{ A[y] -> C[(2 : y <= 10)] }" },
5913 { &isl_multi_pw_aff_range_factor_domain,
5914 "{ A[x] -> [B[] -> C[]] }",
5915 "{ A[x] -> B[] }" },
5916 { &isl_multi_pw_aff_range_factor_range,
5917 "{ A[x] -> [B[] -> C[]] }",
5918 "{ A[y] -> C[] }" },
5919 { &isl_multi_pw_aff_factor_range,
5920 "{ [B[] -> C[]] }",
5921 "{ C[] }" },
5922 { &isl_multi_pw_aff_range_factor_domain,
5923 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5924 "{ A[x] -> B[] : x >= 0 }" },
5925 { &isl_multi_pw_aff_range_factor_range,
5926 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5927 "{ A[y] -> C[] : y >= 0 }" },
5928 { &isl_multi_pw_aff_factor_range,
5929 "[N] -> { [B[] -> C[]] : N >= 0 }",
5930 "[N] -> { C[] : N >= 0 }" },
5933 /* Perform some basic tests of unary operations on isl_multi_pw_aff objects.
5935 static int test_un_mpa(isl_ctx *ctx)
5937 int i;
5938 isl_bool ok;
5939 isl_multi_pw_aff *mpa, *res;
5941 for (i = 0; i < ARRAY_SIZE(mpa_un_tests); ++i) {
5942 mpa = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].arg);
5943 res = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].res);
5944 mpa = mpa_un_tests[i].fn(mpa);
5945 ok = isl_multi_pw_aff_plain_is_equal(mpa, res);
5946 isl_multi_pw_aff_free(mpa);
5947 isl_multi_pw_aff_free(res);
5948 if (ok < 0)
5949 return -1;
5950 if (!ok)
5951 isl_die(ctx, isl_error_unknown,
5952 "unexpected result", return -1);
5955 return 0;
5958 /* Inputs for basic tests of binary operations on isl_multi_pw_aff objects.
5959 * "fn" is the function that is tested.
5960 * "arg1" and "arg2" are string descriptions of the inputs.
5961 * "res" is a string description of the expected result.
5963 struct {
5964 __isl_give isl_multi_pw_aff *(*fn)(
5965 __isl_take isl_multi_pw_aff *mpa1,
5966 __isl_take isl_multi_pw_aff *mpa2);
5967 const char *arg1;
5968 const char *arg2;
5969 const char *res;
5970 } mpa_bin_tests[] = {
5971 { &isl_multi_pw_aff_add, "{ A[] -> [1] }", "{ A[] -> [2] }",
5972 "{ A[] -> [3] }" },
5973 { &isl_multi_pw_aff_add, "{ A[x] -> [(1 : x >= 5)] }",
5974 "{ A[x] -> [(x : x <= 10)] }",
5975 "{ A[x] -> [(1 + x : 5 <= x <= 10)] }" },
5976 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5977 "{ A[x] -> [] : x <= 10 }",
5978 "{ A[x] -> [] : 5 <= x <= 10 }" },
5979 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5980 "[N] -> { A[x] -> [] : x <= N }",
5981 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5982 { &isl_multi_pw_aff_add,
5983 "[N] -> { A[x] -> [] : x <= N }",
5984 "{ A[x] -> [] : x >= 5 }",
5985 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5986 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5987 "{ A[y] -> C[(2 : y <= 10)] }",
5988 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5989 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5990 "{ A[y] -> C[2] : y <= 10 }",
5991 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5992 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5993 "[N] -> { A[y] -> C[2] : y <= N }",
5994 "[N] -> { A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= N)]] }" },
5995 { &isl_multi_pw_aff_range_product, "[N] -> { A[x] -> B[1] : x >= N }",
5996 "{ A[y] -> C[2] : y <= 10 }",
5997 "[N] -> { A[x] -> [B[(1 : x >= N)] -> C[(2 : x <= 10)]] }" },
5998 { &isl_multi_pw_aff_range_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5999 "{ A[] -> [B[1] -> C[2]] }" },
6000 { &isl_multi_pw_aff_range_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
6001 "{ A[] -> [B[] -> C[]] }" },
6002 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
6003 "{ A[y] -> C[] : y <= 10 }",
6004 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] : x <= 10 }" },
6005 { &isl_multi_pw_aff_range_product, "{ A[y] -> C[] : y <= 10 }",
6006 "{ A[x] -> B[(1 : x >= 5)] }",
6007 "{ A[x] -> [C[] -> B[(1 : x >= 5)]] : x <= 10 }" },
6008 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
6009 "{ A[y] -> C[(2 : y <= 10)] }",
6010 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[(2 : y <= 10)]] }" },
6011 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
6012 "{ A[y] -> C[] : y <= 10 }",
6013 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= 10 }" },
6014 { &isl_multi_pw_aff_product, "{ A[y] -> C[] : y <= 10 }",
6015 "{ A[x] -> B[(1 : x >= 5)] }",
6016 "{ [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= 10 }" },
6017 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
6018 "[N] -> { A[y] -> C[] : y <= N }",
6019 "[N] -> { [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= N }" },
6020 { &isl_multi_pw_aff_product, "[N] -> { A[y] -> C[] : y <= N }",
6021 "{ A[x] -> B[(1 : x >= 5)] }",
6022 "[N] -> { [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= N }" },
6023 { &isl_multi_pw_aff_product, "{ A[x] -> B[] : x >= 5 }",
6024 "{ A[y] -> C[] : y <= 10 }",
6025 "{ [A[x] -> A[y]] -> [B[] -> C[]] : x >= 5 and y <= 10 }" },
6026 { &isl_multi_pw_aff_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
6027 "{ [A[] -> A[]] -> [B[1] -> C[2]] }" },
6028 { &isl_multi_pw_aff_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
6029 "{ [A[] -> A[]] -> [B[] -> C[]] }" },
6030 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6031 "{ B[i,j] -> C[i + 2j] }", "{ A[a,b] -> B[b,a] }",
6032 "{ A[a,b] -> C[b + 2a] }" },
6033 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6034 "{ B[i,j] -> C[i + 2j] }",
6035 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6036 "{ A[a,b] -> C[(b + 2a : b > a)] }" },
6037 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6038 "{ B[i,j] -> C[(i + 2j : j > 4)] }",
6039 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6040 "{ A[a,b] -> C[(b + 2a : b > a > 4)] }" },
6041 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6042 "{ B[i,j] -> C[] }",
6043 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6044 "{ A[a,b] -> C[] }" },
6045 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6046 "{ B[i,j] -> C[] : i > j }",
6047 "{ A[a,b] -> B[b,a] }",
6048 "{ A[a,b] -> C[] : b > a }" },
6049 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6050 "{ B[i,j] -> C[] : j > 5 }",
6051 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6052 "{ A[a,b] -> C[] : b > a > 5 }" },
6053 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6054 "[N] -> { B[i,j] -> C[] : j > N }",
6055 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6056 "[N] -> { A[a,b] -> C[] : b > a > N }" },
6057 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6058 "[M,N] -> { B[] -> C[] : N > 5 }",
6059 "[M,N] -> { A[] -> B[] : M > N }",
6060 "[M,N] -> { A[] -> C[] : M > N > 5 }" },
6063 /* Perform some basic tests of binary operations on isl_multi_pw_aff objects.
6065 static int test_bin_mpa(isl_ctx *ctx)
6067 int i;
6068 isl_bool ok;
6069 isl_multi_pw_aff *mpa1, *mpa2, *res;
6071 for (i = 0; i < ARRAY_SIZE(mpa_bin_tests); ++i) {
6072 mpa1 = isl_multi_pw_aff_read_from_str(ctx,
6073 mpa_bin_tests[i].arg1);
6074 mpa2 = isl_multi_pw_aff_read_from_str(ctx,
6075 mpa_bin_tests[i].arg2);
6076 res = isl_multi_pw_aff_read_from_str(ctx,
6077 mpa_bin_tests[i].res);
6078 mpa1 = mpa_bin_tests[i].fn(mpa1, mpa2);
6079 ok = isl_multi_pw_aff_plain_is_equal(mpa1, res);
6080 isl_multi_pw_aff_free(mpa1);
6081 isl_multi_pw_aff_free(res);
6082 if (ok < 0)
6083 return -1;
6084 if (!ok)
6085 isl_die(ctx, isl_error_unknown,
6086 "unexpected result", return -1);
6089 return 0;
6092 /* Inputs for basic tests of unary operations on
6093 * isl_multi_union_pw_aff objects.
6094 * "fn" is the function that is tested.
6095 * "arg" is a string description of the input.
6096 * "res" is a string description of the expected result.
6098 struct {
6099 __isl_give isl_multi_union_pw_aff *(*fn)(
6100 __isl_take isl_multi_union_pw_aff *mupa);
6101 const char *arg;
6102 const char *res;
6103 } mupa_un_tests[] = {
6104 { &isl_multi_union_pw_aff_factor_range,
6105 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]",
6106 "C[{ A[] -> [2] }]" },
6107 { &isl_multi_union_pw_aff_factor_range,
6108 "[B[] -> C[{ A[] -> [2] }]]",
6109 "C[{ A[] -> [2] }]" },
6110 { &isl_multi_union_pw_aff_factor_range,
6111 "[B[{ A[] -> [1] }] -> C[]]",
6112 "C[]" },
6113 { &isl_multi_union_pw_aff_factor_range,
6114 "[B[] -> C[]]",
6115 "C[]" },
6116 { &isl_multi_union_pw_aff_factor_range,
6117 "([B[] -> C[]] : { A[x] : x >= 0 })",
6118 "(C[] : { A[x] : x >= 0 })" },
6119 { &isl_multi_union_pw_aff_factor_range,
6120 "[N] -> ([B[] -> C[]] : { A[x] : x <= N })",
6121 "[N] -> (C[] : { A[x] : x <= N })" },
6122 { &isl_multi_union_pw_aff_factor_range,
6123 "[N] -> ([B[] -> C[]] : { : N >= 0 })",
6124 "[N] -> (C[] : { : N >= 0 })" },
6127 /* Perform some basic tests of unary operations on
6128 * isl_multi_union_pw_aff objects.
6130 static int test_un_mupa(isl_ctx *ctx)
6132 int i;
6133 isl_bool ok;
6134 isl_multi_union_pw_aff *mupa, *res;
6136 for (i = 0; i < ARRAY_SIZE(mupa_un_tests); ++i) {
6137 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6138 mupa_un_tests[i].arg);
6139 res = isl_multi_union_pw_aff_read_from_str(ctx,
6140 mupa_un_tests[i].res);
6141 mupa = mupa_un_tests[i].fn(mupa);
6142 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6143 isl_multi_union_pw_aff_free(mupa);
6144 isl_multi_union_pw_aff_free(res);
6145 if (ok < 0)
6146 return -1;
6147 if (!ok)
6148 isl_die(ctx, isl_error_unknown,
6149 "unexpected result", return -1);
6152 return 0;
6155 /* Inputs for basic tests of binary operations on
6156 * isl_multi_union_pw_aff objects.
6157 * "fn" is the function that is tested.
6158 * "arg1" and "arg2" are string descriptions of the inputs.
6159 * "res" is a string description of the expected result.
6161 struct {
6162 __isl_give isl_multi_union_pw_aff *(*fn)(
6163 __isl_take isl_multi_union_pw_aff *mupa1,
6164 __isl_take isl_multi_union_pw_aff *mupa2);
6165 const char *arg1;
6166 const char *arg2;
6167 const char *res;
6168 } mupa_bin_tests[] = {
6169 { &isl_multi_union_pw_aff_add, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
6170 "[{ A[] -> [3] }]" },
6171 { &isl_multi_union_pw_aff_sub, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
6172 "[{ A[] -> [-1] }]" },
6173 { &isl_multi_union_pw_aff_add,
6174 "[{ A[] -> [1]; B[] -> [4] }]",
6175 "[{ A[] -> [2]; C[] -> [5] }]",
6176 "[{ A[] -> [3] }]" },
6177 { &isl_multi_union_pw_aff_union_add,
6178 "[{ A[] -> [1]; B[] -> [4] }]",
6179 "[{ A[] -> [2]; C[] -> [5] }]",
6180 "[{ A[] -> [3]; B[] -> [4]; C[] -> [5] }]" },
6181 { &isl_multi_union_pw_aff_add, "[{ A[x] -> [(1)] : x >= 5 }]",
6182 "[{ A[x] -> [(x)] : x <= 10 }]",
6183 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10 }]" },
6184 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
6185 "([] : { A[x] : x <= 10 })",
6186 "([] : { A[x] : 5 <= x <= 10 })" },
6187 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
6188 "[N] -> ([] : { A[x] : x <= N })",
6189 "[N] -> ([] : { A[x] : 5 <= x <= N })" },
6190 { &isl_multi_union_pw_aff_add, "[N] -> ([] : { A[x] : x >= N })",
6191 "([] : { A[x] : x <= 10 })",
6192 "[N] -> ([] : { A[x] : N <= x <= 10 })" },
6193 { &isl_multi_union_pw_aff_union_add, "[{ A[x] -> [(1)] : x >= 5 }]",
6194 "[{ A[x] -> [(x)] : x <= 10 }]",
6195 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10; "
6196 "A[x] -> [(1)] : x > 10; A[x] -> [(x)] : x < 5 }]" },
6197 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 5 })",
6198 "([] : { A[x] : x <= 10 })",
6199 "([] : { A[x] })" },
6200 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 0 })",
6201 "[N] -> ([] : { A[x] : x >= N })",
6202 "[N] -> ([] : { A[x] : x >= 0 or x >= N })" },
6203 { &isl_multi_union_pw_aff_union_add,
6204 "[N] -> ([] : { A[] : N >= 0})",
6205 "[N] -> ([] : { A[] : N <= 0})",
6206 "[N] -> ([] : { A[] })" },
6207 { &isl_multi_union_pw_aff_union_add,
6208 "[N] -> ([] : { A[] })",
6209 "[N] -> ([] : { : })",
6210 "[N] -> ([] : { : })" },
6211 { &isl_multi_union_pw_aff_union_add,
6212 "[N] -> ([] : { : })",
6213 "[N] -> ([] : { A[] })",
6214 "[N] -> ([] : { : })" },
6215 { &isl_multi_union_pw_aff_union_add,
6216 "[N] -> ([] : { : N >= 0})",
6217 "[N] -> ([] : { : N <= 0})",
6218 "[N] -> ([] : { : })" },
6219 { &isl_multi_union_pw_aff_range_product,
6220 "B[{ A[] -> [1] }]",
6221 "C[{ A[] -> [2] }]",
6222 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]" },
6223 { &isl_multi_union_pw_aff_range_product,
6224 "(B[] : { A[x] : x >= 5 })",
6225 "(C[] : { A[x] : x <= 10 })",
6226 "([B[] -> C[]] : { A[x] : 5 <= x <= 10 })" },
6227 { &isl_multi_union_pw_aff_range_product,
6228 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6229 "(C[] : { A[x] : x <= 10 })",
6230 "[B[{ A[x] -> [x + 1] : 5 <= x <= 10 }] -> C[]]" },
6231 { &isl_multi_union_pw_aff_range_product,
6232 "(C[] : { A[x] : x <= 10 })",
6233 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6234 "[C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= 10 }]]" },
6235 { &isl_multi_union_pw_aff_range_product,
6236 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6237 "[N] -> (C[] : { A[x] : x <= N })",
6238 "[N] -> [B[{ A[x] -> [x + 1] : 5 <= x <= N }] -> C[]]" },
6239 { &isl_multi_union_pw_aff_range_product,
6240 "[N] -> (C[] : { A[x] : x <= N })",
6241 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6242 "[N] -> [C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= N }]]" },
6243 { &isl_multi_union_pw_aff_range_product,
6244 "B[{ A[] -> [1]; D[] -> [3] }]",
6245 "C[{ A[] -> [2] }]",
6246 "[B[{ A[] -> [1]; D[] -> [3] }] -> C[{ A[] -> [2] }]]" },
6247 { &isl_multi_union_pw_aff_range_product,
6248 "B[] }]",
6249 "(C[] : { A[x] })",
6250 "([B[] -> C[]] : { A[x] })" },
6251 { &isl_multi_union_pw_aff_range_product,
6252 "(B[] : { A[x] })",
6253 "C[] }]",
6254 "([B[] -> C[]] : { A[x] })" },
6257 /* Perform some basic tests of binary operations on
6258 * isl_multi_union_pw_aff objects.
6260 static int test_bin_mupa(isl_ctx *ctx)
6262 int i;
6263 isl_bool ok;
6264 isl_multi_union_pw_aff *mupa1, *mupa2, *res;
6266 for (i = 0; i < ARRAY_SIZE(mupa_bin_tests); ++i) {
6267 mupa1 = isl_multi_union_pw_aff_read_from_str(ctx,
6268 mupa_bin_tests[i].arg1);
6269 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx,
6270 mupa_bin_tests[i].arg2);
6271 res = isl_multi_union_pw_aff_read_from_str(ctx,
6272 mupa_bin_tests[i].res);
6273 mupa1 = mupa_bin_tests[i].fn(mupa1, mupa2);
6274 ok = isl_multi_union_pw_aff_plain_is_equal(mupa1, res);
6275 isl_multi_union_pw_aff_free(mupa1);
6276 isl_multi_union_pw_aff_free(res);
6277 if (ok < 0)
6278 return -1;
6279 if (!ok)
6280 isl_die(ctx, isl_error_unknown,
6281 "unexpected result", return -1);
6284 return 0;
6287 /* Inputs for basic tests of binary operations on
6288 * pairs of isl_multi_union_pw_aff and isl_set objects.
6289 * "fn" is the function that is tested.
6290 * "arg1" and "arg2" are string descriptions of the inputs.
6291 * "res" is a string description of the expected result.
6293 struct {
6294 __isl_give isl_multi_union_pw_aff *(*fn)(
6295 __isl_take isl_multi_union_pw_aff *mupa,
6296 __isl_take isl_set *set);
6297 const char *arg1;
6298 const char *arg2;
6299 const char *res;
6300 } mupa_set_tests[] = {
6301 { &isl_multi_union_pw_aff_intersect_range,
6302 "C[{ B[i,j] -> [i + 2j] }]", "{ C[1] }",
6303 "C[{ B[i,j] -> [i + 2j] : i + 2j = 1 }]" },
6304 { &isl_multi_union_pw_aff_intersect_range,
6305 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[N] }",
6306 "[N] -> C[{ B[i,j] -> [i + 2j] : i + 2j = N }]" },
6307 { &isl_multi_union_pw_aff_intersect_range,
6308 "[N] -> C[{ B[i,j] -> [i + 2j + N] }]", "{ C[1] }",
6309 "[N] -> C[{ B[i,j] -> [i + 2j + N] : i + 2j + N = 1 }]" },
6310 { &isl_multi_union_pw_aff_intersect_range,
6311 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[x] : N >= 0 }",
6312 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0 }]" },
6313 { &isl_multi_union_pw_aff_intersect_range,
6314 "C[]", "{ C[] }", "C[]" },
6315 { &isl_multi_union_pw_aff_intersect_range,
6316 "[N] -> (C[] : { : N >= 0 })",
6317 "{ C[] }",
6318 "[N] -> (C[] : { : N >= 0 })" },
6319 { &isl_multi_union_pw_aff_intersect_range,
6320 "(C[] : { A[a,b] })",
6321 "{ C[] }",
6322 "(C[] : { A[a,b] })" },
6323 { &isl_multi_union_pw_aff_intersect_range,
6324 "[N] -> (C[] : { A[a,b] : a,b <= N })",
6325 "{ C[] }",
6326 "[N] -> (C[] : { A[a,b] : a,b <= N })" },
6327 { &isl_multi_union_pw_aff_intersect_range,
6328 "C[]",
6329 "[N] -> { C[] : N >= 0 }",
6330 "[N] -> (C[] : { : N >= 0 })" },
6331 { &isl_multi_union_pw_aff_intersect_range,
6332 "(C[] : { A[a,b] })",
6333 "[N] -> { C[] : N >= 0 }",
6334 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
6335 { &isl_multi_union_pw_aff_intersect_range,
6336 "[N] -> (C[] : { : N >= 0 })",
6337 "[N] -> { C[] : N < 1024 }",
6338 "[N] -> (C[] : { : 0 <= N < 1024 })" },
6339 { &isl_multi_union_pw_aff_intersect_params,
6340 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { : N >= 0 }",
6341 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0}]" },
6342 { &isl_multi_union_pw_aff_intersect_params,
6343 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "[N] -> { : N >= 0 }",
6344 "[N] -> C[{ B[i,j] -> [i + 2j] : 0 <= N <= 256 }]" },
6345 { &isl_multi_union_pw_aff_intersect_params,
6346 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "{ : }",
6347 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]" },
6348 { &isl_multi_union_pw_aff_intersect_params,
6349 "C[]", "[N] -> { : N >= 0 }",
6350 "[N] -> (C[] : { : N >= 0 })" },
6351 { &isl_multi_union_pw_aff_intersect_params,
6352 "(C[] : { A[a,b] })", "[N] -> { : N >= 0 }",
6353 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
6354 { &isl_multi_union_pw_aff_intersect_params,
6355 "[N] -> (C[] : { A[a,N] })", "{ : }",
6356 "[N] -> (C[] : { A[a,N] })" },
6357 { &isl_multi_union_pw_aff_intersect_params,
6358 "[N] -> (C[] : { A[a,b] : N <= 256 })", "[N] -> { : N >= 0 }",
6359 "[N] -> (C[] : { A[a,b] : 0 <= N <= 256 })" },
6362 /* Perform some basic tests of binary operations on
6363 * pairs of isl_multi_union_pw_aff and isl_set objects.
6365 static int test_mupa_set(isl_ctx *ctx)
6367 int i;
6368 isl_bool ok;
6369 isl_multi_union_pw_aff *mupa, *res;
6370 isl_set *set;
6372 for (i = 0; i < ARRAY_SIZE(mupa_set_tests); ++i) {
6373 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6374 mupa_set_tests[i].arg1);
6375 set = isl_set_read_from_str(ctx, mupa_set_tests[i].arg2);
6376 res = isl_multi_union_pw_aff_read_from_str(ctx,
6377 mupa_set_tests[i].res);
6378 mupa = mupa_set_tests[i].fn(mupa, set);
6379 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6380 isl_multi_union_pw_aff_free(mupa);
6381 isl_multi_union_pw_aff_free(res);
6382 if (ok < 0)
6383 return -1;
6384 if (!ok)
6385 isl_die(ctx, isl_error_unknown,
6386 "unexpected result", return -1);
6389 return 0;
6392 /* Inputs for basic tests of binary operations on
6393 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
6394 * "fn" is the function that is tested.
6395 * "arg1" and "arg2" are string descriptions of the inputs.
6396 * "res" is a string description of the expected result.
6398 struct {
6399 __isl_give isl_multi_union_pw_aff *(*fn)(
6400 __isl_take isl_multi_union_pw_aff *mupa,
6401 __isl_take isl_union_set *uset);
6402 const char *arg1;
6403 const char *arg2;
6404 const char *res;
6405 } mupa_uset_tests[] = {
6406 { &isl_multi_union_pw_aff_intersect_domain,
6407 "C[{ B[i,j] -> [i + 2j] }]", "{ B[i,i] }",
6408 "C[{ B[i,i] -> [3i] }]" },
6409 { &isl_multi_union_pw_aff_intersect_domain,
6410 "(C[] : { B[i,j] })", "{ B[i,i] }",
6411 "(C[] : { B[i,i] })" },
6412 { &isl_multi_union_pw_aff_intersect_domain,
6413 "(C[] : { B[i,j] })", "[N] -> { B[N,N] }",
6414 "[N] -> (C[] : { B[N,N] })" },
6415 { &isl_multi_union_pw_aff_intersect_domain,
6416 "C[]", "{ B[i,i] }",
6417 "(C[] : { B[i,i] })" },
6418 { &isl_multi_union_pw_aff_intersect_domain,
6419 "[N] -> (C[] : { : N >= 0 })", "{ B[i,i] }",
6420 "[N] -> (C[] : { B[i,i] : N >= 0 })" },
6423 /* Perform some basic tests of binary operations on
6424 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
6426 static int test_mupa_uset(isl_ctx *ctx)
6428 int i;
6429 isl_bool ok;
6430 isl_multi_union_pw_aff *mupa, *res;
6431 isl_union_set *uset;
6433 for (i = 0; i < ARRAY_SIZE(mupa_uset_tests); ++i) {
6434 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6435 mupa_uset_tests[i].arg1);
6436 uset = isl_union_set_read_from_str(ctx,
6437 mupa_uset_tests[i].arg2);
6438 res = isl_multi_union_pw_aff_read_from_str(ctx,
6439 mupa_uset_tests[i].res);
6440 mupa = mupa_uset_tests[i].fn(mupa, uset);
6441 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6442 isl_multi_union_pw_aff_free(mupa);
6443 isl_multi_union_pw_aff_free(res);
6444 if (ok < 0)
6445 return -1;
6446 if (!ok)
6447 isl_die(ctx, isl_error_unknown,
6448 "unexpected result", return -1);
6451 return 0;
6454 /* Inputs for basic tests of binary operations on
6455 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
6456 * "fn" is the function that is tested.
6457 * "arg1" and "arg2" are string descriptions of the inputs.
6458 * "res" is a string description of the expected result.
6460 struct {
6461 __isl_give isl_multi_union_pw_aff *(*fn)(
6462 __isl_take isl_multi_union_pw_aff *mupa,
6463 __isl_take isl_multi_aff *ma);
6464 const char *arg1;
6465 const char *arg2;
6466 const char *res;
6467 } mupa_ma_tests[] = {
6468 { &isl_multi_union_pw_aff_apply_multi_aff,
6469 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6470 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6471 "{ C[a,b] -> D[b,a] }",
6472 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
6473 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
6474 { &isl_multi_union_pw_aff_apply_multi_aff,
6475 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
6476 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6477 "{ C[a,b] -> D[b,a] }",
6478 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
6479 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
6480 { &isl_multi_union_pw_aff_apply_multi_aff,
6481 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6482 "[N] -> { C[a] -> D[a + N] }",
6483 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }] " },
6484 { &isl_multi_union_pw_aff_apply_multi_aff,
6485 "C[]",
6486 "{ C[] -> D[] }",
6487 "D[]" },
6488 { &isl_multi_union_pw_aff_apply_multi_aff,
6489 "[N] -> (C[] : { : N >= 0 })",
6490 "{ C[] -> D[] }",
6491 "[N] -> (D[] : { : N >= 0 })" },
6492 { &isl_multi_union_pw_aff_apply_multi_aff,
6493 "C[]",
6494 "[N] -> { C[] -> D[N] }",
6495 "[N] -> D[{ [N] }]" },
6496 { &isl_multi_union_pw_aff_apply_multi_aff,
6497 "(C[] : { A[i,j] : i >= j })",
6498 "{ C[] -> D[] }",
6499 "(D[] : { A[i,j] : i >= j })" },
6500 { &isl_multi_union_pw_aff_apply_multi_aff,
6501 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6502 "{ C[] -> D[] }",
6503 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6504 { &isl_multi_union_pw_aff_apply_multi_aff,
6505 "(C[] : { A[i,j] : i >= j })",
6506 "[N] -> { C[] -> D[N] }",
6507 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6510 /* Perform some basic tests of binary operations on
6511 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
6513 static int test_mupa_ma(isl_ctx *ctx)
6515 int i;
6516 isl_bool ok;
6517 isl_multi_union_pw_aff *mupa, *res;
6518 isl_multi_aff *ma;
6520 for (i = 0; i < ARRAY_SIZE(mupa_ma_tests); ++i) {
6521 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6522 mupa_ma_tests[i].arg1);
6523 ma = isl_multi_aff_read_from_str(ctx, mupa_ma_tests[i].arg2);
6524 res = isl_multi_union_pw_aff_read_from_str(ctx,
6525 mupa_ma_tests[i].res);
6526 mupa = mupa_ma_tests[i].fn(mupa, ma);
6527 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6528 isl_multi_union_pw_aff_free(mupa);
6529 isl_multi_union_pw_aff_free(res);
6530 if (ok < 0)
6531 return -1;
6532 if (!ok)
6533 isl_die(ctx, isl_error_unknown,
6534 "unexpected result", return -1);
6537 return 0;
6540 /* Inputs for basic tests of binary operations on
6541 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
6542 * "fn" is the function that is tested.
6543 * "arg1" and "arg2" are string descriptions of the inputs.
6544 * "res" is a string description of the expected result.
6546 struct {
6547 __isl_give isl_union_pw_aff *(*fn)(
6548 __isl_take isl_multi_union_pw_aff *mupa,
6549 __isl_take isl_pw_aff *pa);
6550 const char *arg1;
6551 const char *arg2;
6552 const char *res;
6553 } mupa_pa_tests[] = {
6554 { &isl_multi_union_pw_aff_apply_pw_aff,
6555 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6556 "[N] -> { C[a] -> [a + N] }",
6557 "[N] -> { A[i,j] -> [i + N]; B[i,j] -> [j + N] }" },
6558 { &isl_multi_union_pw_aff_apply_pw_aff,
6559 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6560 "{ C[a] -> [a] : a >= 0; C[a] -> [-a] : a < 0 }",
6561 "{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
6562 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }" },
6563 { &isl_multi_union_pw_aff_apply_pw_aff,
6564 "C[]",
6565 "[N] -> { C[] -> [N] }",
6566 "[N] -> { [N] }" },
6567 { &isl_multi_union_pw_aff_apply_pw_aff,
6568 "C[]",
6569 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
6570 "[N] -> { [N] : N >= 0; [-N] : N < 0 }" },
6571 { &isl_multi_union_pw_aff_apply_pw_aff,
6572 "[N] -> (C[] : { : N >= 0 })",
6573 "[N] -> { C[] -> [N] }",
6574 "[N] -> { [N] : N >= 0 }" },
6575 { &isl_multi_union_pw_aff_apply_pw_aff,
6576 "[N] -> (C[] : { : N >= 0 })",
6577 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
6578 "[N] -> { [N] : N >= 0 }" },
6579 { &isl_multi_union_pw_aff_apply_pw_aff,
6580 "[N] -> (C[] : { : N >= 0 })",
6581 "{ C[] -> [0] }",
6582 "[N] -> { [0] : N >= 0 }" },
6583 { &isl_multi_union_pw_aff_apply_pw_aff,
6584 "(C[] : { A[i,j] : i >= j })",
6585 "[N] -> { C[] -> [N] }",
6586 "[N] -> { A[i,j] -> [N] : i >= j }" },
6587 { &isl_multi_union_pw_aff_apply_pw_aff,
6588 "(C[] : { A[i,j] : i >= j })",
6589 "[N] -> { C[] -> [N] : N >= 0 }",
6590 "[N] -> { A[i,j] -> [N] : i >= j and N >= 0 }" },
6593 /* Perform some basic tests of binary operations on
6594 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
6596 static int test_mupa_pa(isl_ctx *ctx)
6598 int i;
6599 isl_bool ok;
6600 isl_multi_union_pw_aff *mupa;
6601 isl_union_pw_aff *upa, *res;
6602 isl_pw_aff *pa;
6604 for (i = 0; i < ARRAY_SIZE(mupa_pa_tests); ++i) {
6605 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6606 mupa_pa_tests[i].arg1);
6607 pa = isl_pw_aff_read_from_str(ctx, mupa_pa_tests[i].arg2);
6608 res = isl_union_pw_aff_read_from_str(ctx,
6609 mupa_pa_tests[i].res);
6610 upa = mupa_pa_tests[i].fn(mupa, pa);
6611 ok = isl_union_pw_aff_plain_is_equal(upa, res);
6612 isl_union_pw_aff_free(upa);
6613 isl_union_pw_aff_free(res);
6614 if (ok < 0)
6615 return -1;
6616 if (!ok)
6617 isl_die(ctx, isl_error_unknown,
6618 "unexpected result", return -1);
6621 return 0;
6624 /* Inputs for basic tests of binary operations on
6625 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6626 * "fn" is the function that is tested.
6627 * "arg1" and "arg2" are string descriptions of the inputs.
6628 * "res" is a string description of the expected result.
6630 struct {
6631 __isl_give isl_multi_union_pw_aff *(*fn)(
6632 __isl_take isl_multi_union_pw_aff *mupa,
6633 __isl_take isl_pw_multi_aff *pma);
6634 const char *arg1;
6635 const char *arg2;
6636 const char *res;
6637 } mupa_pma_tests[] = {
6638 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6639 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6640 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6641 "{ C[a,b] -> D[b,a] }",
6642 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
6643 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
6644 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6645 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
6646 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6647 "{ C[a,b] -> D[b,a] }",
6648 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
6649 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
6650 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6651 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6652 "[N] -> { C[a] -> D[a + N] }",
6653 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }]" },
6654 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6655 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6656 "{ C[a] -> D[a] : a >= 0; C[a] -> D[-a] : a < 0 }",
6657 "D[{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
6658 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }]" },
6659 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6660 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6661 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6662 "{ C[a,b] -> D[a,b] : a >= b; C[a,b] -> D[b,a] : a < b }",
6663 "D[{ A[i,j] -> [i] : i >= j; A[i,j] -> [j] : i < j; "
6664 "B[i,j] -> [j] : i <= j; B[i,j] -> [i] : i > j }, "
6665 "{ A[i,j] -> [j] : i >= j; A[i,j] -> [i] : i < j; "
6666 "B[i,j] -> [i] : i <= j; B[i,j] -> [j] : i > j }]" },
6667 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6668 "C[]",
6669 "{ C[] -> D[] }",
6670 "D[]" },
6671 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6672 "[N] -> (C[] : { : N >= 0 })",
6673 "{ C[] -> D[] }",
6674 "[N] -> (D[] : { : N >= 0 })" },
6675 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6676 "C[]",
6677 "[N] -> { C[] -> D[N] }",
6678 "[N] -> D[{ [N] }]" },
6679 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6680 "(C[] : { A[i,j] : i >= j })",
6681 "{ C[] -> D[] }",
6682 "(D[] : { A[i,j] : i >= j })" },
6683 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6684 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6685 "{ C[] -> D[] }",
6686 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6687 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6688 "(C[] : { A[i,j] : i >= j })",
6689 "[N] -> { C[] -> D[N] }",
6690 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6691 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6692 "C[]",
6693 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6694 "[N] -> D[{ [N] : N >= 0; [-N] : N < 0 }]" },
6695 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6696 "[N] -> (C[] : { : N >= 0 })",
6697 "[N] -> { C[] -> D[N] }",
6698 "[N] -> D[{ [N] : N >= 0 }]" },
6699 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6700 "[N] -> (C[] : { : N >= 0 })",
6701 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6702 "[N] -> D[{ [N] : N >= 0 }]" },
6703 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6704 "[N] -> (C[] : { : N >= 0 })",
6705 "{ C[] -> D[0] }",
6706 "[N] -> D[{ [0] : N >= 0 }]" },
6707 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6708 "(C[] : { A[i,j] : i >= j })",
6709 "[N] -> { C[] -> D[N] : N >= 0 }",
6710 "[N] -> D[{ A[i,j] -> [N] : i >= j and N >= 0 }]" },
6713 /* Perform some basic tests of binary operations on
6714 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6716 static int test_mupa_pma(isl_ctx *ctx)
6718 int i;
6719 isl_bool ok;
6720 isl_multi_union_pw_aff *mupa, *res;
6721 isl_pw_multi_aff *pma;
6723 for (i = 0; i < ARRAY_SIZE(mupa_pma_tests); ++i) {
6724 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6725 mupa_pma_tests[i].arg1);
6726 pma = isl_pw_multi_aff_read_from_str(ctx,
6727 mupa_pma_tests[i].arg2);
6728 res = isl_multi_union_pw_aff_read_from_str(ctx,
6729 mupa_pma_tests[i].res);
6730 mupa = mupa_pma_tests[i].fn(mupa, pma);
6731 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6732 isl_multi_union_pw_aff_free(mupa);
6733 isl_multi_union_pw_aff_free(res);
6734 if (ok < 0)
6735 return -1;
6736 if (!ok)
6737 isl_die(ctx, isl_error_unknown,
6738 "unexpected result", return -1);
6741 return 0;
6744 /* Inputs for basic tests of binary operations on
6745 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6746 * "fn" is the function that is tested.
6747 * "arg1" and "arg2" are string descriptions of the inputs.
6748 * "res" is a string description of the expected result.
6750 struct {
6751 __isl_give isl_multi_union_pw_aff *(*fn)(
6752 __isl_take isl_multi_union_pw_aff *mupa,
6753 __isl_take isl_union_pw_multi_aff *upma);
6754 const char *arg1;
6755 const char *arg2;
6756 const char *res;
6757 } mupa_upma_tests[] = {
6758 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6759 "C[{ B[i,j] -> [i + 2j] }]", "{ A[a,b] -> B[b,a] }",
6760 "C[{ A[a,b] -> [b + 2a] }]" },
6761 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6762 "C[{ B[i,j] -> [i + 2j] }]",
6763 "{ A[a,b] -> B[b,a] : b > a }",
6764 "C[{ A[a,b] -> [b + 2a] : b > a }]" },
6765 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6766 "C[{ B[i,j] -> [i + 2j] : j > 4 }]",
6767 "{ A[a,b] -> B[b,a] : b > a }",
6768 "C[{ A[a,b] -> [b + 2a] : b > a > 4 }]" },
6769 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6770 "C[{ B[i,j] -> [i + 2j] }]",
6771 "{ A[a,b] -> B[b,a] : a > b; A[a,b] -> B[a,b] : a <= b }",
6772 "C[{ A[a,b] -> [b + 2a] : a > b; A[a,b] -> [a + 2b] : a <= b }]" },
6773 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6774 "(C[] : { B[a,b] })",
6775 "{ A[a,b] -> B[b,a] }",
6776 "(C[] : { A[a,b] })" },
6777 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6778 "(C[] : { B[a,b] })",
6779 "{ B[a,b] -> A[b,a] }",
6780 "(C[] : { })" },
6781 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6782 "(C[] : { B[a,b] })",
6783 "{ A[a,b] -> B[b,a] : a > b }",
6784 "(C[] : { A[a,b] : a > b })" },
6785 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6786 "(C[] : { B[a,b] : a > b })",
6787 "{ A[a,b] -> B[b,a] }",
6788 "(C[] : { A[a,b] : b > a })" },
6789 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6790 "[N] -> (C[] : { B[a,b] : a > N })",
6791 "{ A[a,b] -> B[b,a] : a > b }",
6792 "[N] -> (C[] : { A[a,b] : a > b > N })" },
6793 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6794 "(C[] : { B[a,b] : a > b })",
6795 "[N] -> { A[a,b] -> B[b,a] : a > N }",
6796 "[N] -> (C[] : { A[a,b] : b > a > N })" },
6797 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6798 "C[]",
6799 "{ A[a,b] -> B[b,a] }",
6800 "C[]" },
6801 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6802 "[N] -> (C[] : { : N >= 0 })",
6803 "{ A[a,b] -> B[b,a] }",
6804 "[N] -> (C[] : { : N >= 0 })" },
6805 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6806 "C[]",
6807 "[N] -> { A[a,b] -> B[b,a] : N >= 0 }",
6808 "[N] -> (C[] : { : N >= 0 })" },
6811 /* Perform some basic tests of binary operations on
6812 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6814 static int test_mupa_upma(isl_ctx *ctx)
6816 int i;
6817 isl_bool ok;
6818 isl_multi_union_pw_aff *mupa, *res;
6819 isl_union_pw_multi_aff *upma;
6821 for (i = 0; i < ARRAY_SIZE(mupa_upma_tests); ++i) {
6822 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6823 mupa_upma_tests[i].arg1);
6824 upma = isl_union_pw_multi_aff_read_from_str(ctx,
6825 mupa_upma_tests[i].arg2);
6826 res = isl_multi_union_pw_aff_read_from_str(ctx,
6827 mupa_upma_tests[i].res);
6828 mupa = mupa_upma_tests[i].fn(mupa, upma);
6829 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6830 isl_multi_union_pw_aff_free(mupa);
6831 isl_multi_union_pw_aff_free(res);
6832 if (ok < 0)
6833 return -1;
6834 if (!ok)
6835 isl_die(ctx, isl_error_unknown,
6836 "unexpected result", return -1);
6839 return 0;
6842 /* Check that the input tuple of an isl_aff can be set properly.
6844 static isl_stat test_aff_set_tuple_id(isl_ctx *ctx)
6846 isl_id *id;
6847 isl_aff *aff;
6848 isl_stat equal;
6850 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x + 1] }");
6851 id = isl_id_alloc(ctx, "A", NULL);
6852 aff = isl_aff_set_tuple_id(aff, isl_dim_in, id);
6853 equal = aff_check_plain_equal(aff, "{ A[x] -> [x + 1] }");
6854 isl_aff_free(aff);
6855 if (equal < 0)
6856 return isl_stat_error;
6858 return isl_stat_ok;
6861 /* Check that affine expressions get normalized on addition/subtraction.
6862 * In particular, check that (final) unused integer divisions get removed
6863 * such that an expression derived from expressions with integer divisions
6864 * is found to be obviously equal to one that is created directly.
6866 static isl_stat test_aff_normalize(isl_ctx *ctx)
6868 isl_aff *aff, *aff2;
6869 isl_stat ok;
6871 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x//2] }");
6872 aff2 = isl_aff_read_from_str(ctx, "{ [x] -> [1 + x//2] }");
6873 aff = isl_aff_sub(aff2, aff);
6874 ok = aff_check_plain_equal(aff, "{ [x] -> [1] }");
6875 isl_aff_free(aff);
6877 return ok;
6880 int test_aff(isl_ctx *ctx)
6882 const char *str;
6883 isl_set *set;
6884 isl_space *space;
6885 isl_local_space *ls;
6886 isl_aff *aff;
6887 int zero;
6888 isl_stat equal;
6890 if (test_upa(ctx) < 0)
6891 return -1;
6892 if (test_bin_aff(ctx) < 0)
6893 return -1;
6894 if (test_bin_pw_aff(ctx) < 0)
6895 return -1;
6896 if (test_upma_test(ctx) < 0)
6897 return -1;
6898 if (test_bin_upma(ctx) < 0)
6899 return -1;
6900 if (test_bin_upma_fail(ctx) < 0)
6901 return -1;
6902 if (test_upma_uset(ctx) < 0)
6903 return -1;
6904 if (test_un_mpa(ctx) < 0)
6905 return -1;
6906 if (test_bin_mpa(ctx) < 0)
6907 return -1;
6908 if (test_un_mupa(ctx) < 0)
6909 return -1;
6910 if (test_bin_mupa(ctx) < 0)
6911 return -1;
6912 if (test_mupa_set(ctx) < 0)
6913 return -1;
6914 if (test_mupa_uset(ctx) < 0)
6915 return -1;
6916 if (test_mupa_ma(ctx) < 0)
6917 return -1;
6918 if (test_mupa_pa(ctx) < 0)
6919 return -1;
6920 if (test_mupa_pma(ctx) < 0)
6921 return -1;
6922 if (test_mupa_upma(ctx) < 0)
6923 return -1;
6925 space = isl_space_set_alloc(ctx, 0, 1);
6926 ls = isl_local_space_from_space(space);
6927 aff = isl_aff_zero_on_domain(ls);
6929 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6930 aff = isl_aff_scale_down_ui(aff, 3);
6931 aff = isl_aff_floor(aff);
6932 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6933 aff = isl_aff_scale_down_ui(aff, 2);
6934 aff = isl_aff_floor(aff);
6935 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6937 str = "{ [10] }";
6938 set = isl_set_read_from_str(ctx, str);
6939 aff = isl_aff_gist(aff, set);
6941 aff = isl_aff_add_constant_si(aff, -16);
6942 zero = isl_aff_plain_is_zero(aff);
6943 isl_aff_free(aff);
6945 if (zero < 0)
6946 return -1;
6947 if (!zero)
6948 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6950 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
6951 aff = isl_aff_scale_down_ui(aff, 64);
6952 aff = isl_aff_floor(aff);
6953 equal = aff_check_plain_equal(aff, "{ [-1] }");
6954 isl_aff_free(aff);
6955 if (equal < 0)
6956 return -1;
6958 if (test_aff_set_tuple_id(ctx) < 0)
6959 return -1;
6960 if (test_aff_normalize(ctx) < 0)
6961 return -1;
6963 return 0;
6966 /* Inputs for isl_set_bind tests.
6967 * "set" is the input set.
6968 * "tuple" is the binding tuple.
6969 * "res" is the expected result.
6971 static
6972 struct {
6973 const char *set;
6974 const char *tuple;
6975 const char *res;
6976 } bind_set_tests[] = {
6977 { "{ A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
6978 "{ A[M, N] }",
6979 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
6980 { "{ B[N, M] : M mod 2 = 0 and N mod 8 = 3 }",
6981 "{ B[N, M] }",
6982 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
6983 { "[M] -> { C[N] : M mod 2 = 0 and N mod 8 = 3 }",
6984 "{ C[N] }",
6985 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
6986 { "[M] -> { D[x, N] : x mod 2 = 0 and N mod 8 = 3 and M >= 0 }",
6987 "{ D[M, N] }",
6988 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 and M >= 0 }" },
6991 /* Perform basic isl_set_bind tests.
6993 static isl_stat test_bind_set(isl_ctx *ctx)
6995 int i;
6997 for (i = 0; i < ARRAY_SIZE(bind_set_tests); ++i) {
6998 const char *str;
6999 isl_set *set;
7000 isl_multi_id *tuple;
7001 isl_stat r;
7003 set = isl_set_read_from_str(ctx, bind_set_tests[i].set);
7004 str = bind_set_tests[i].tuple;
7005 tuple = isl_multi_id_read_from_str(ctx, str);
7006 set = isl_set_bind(set, tuple);
7007 r = set_check_equal(set, bind_set_tests[i].res);
7008 isl_set_free(set);
7009 if (r < 0)
7010 return isl_stat_error;
7013 return isl_stat_ok;
7016 /* Inputs for isl_map_bind_domain tests.
7017 * "map" is the input map.
7018 * "tuple" is the binding tuple.
7019 * "res" is the expected result.
7021 struct {
7022 const char *map;
7023 const char *tuple;
7024 const char *res;
7025 } bind_map_domain_tests[] = {
7026 { "{ A[M, N] -> [M + floor(N/2)] }",
7027 "{ A[M, N] }",
7028 "[M, N] -> { [M + floor(N/2)] }" },
7029 { "{ B[N, M] -> [M + floor(N/2)] }",
7030 "{ B[N, M] }",
7031 "[N, M] -> { [M + floor(N/2)] }" },
7032 { "[M] -> { C[N] -> [M + floor(N/2)] }",
7033 "{ C[N] }",
7034 "[M, N] -> { [M + floor(N/2)] }" },
7035 { "[M] -> { C[x, N] -> [x + floor(N/2)] }",
7036 "{ C[M, N] }",
7037 "[M, N] -> { [M + floor(N/2)] }" },
7038 { "[M] -> { C[x, N] -> [M + floor(N/2)] }",
7039 "{ C[M, N] }",
7040 "[M, N] -> { [M + floor(N/2)] }" },
7041 { "[A, M] -> { C[N, x] -> [x + floor(N/2)] }",
7042 "{ C[N, M] }",
7043 "[A, N, M] -> { [M + floor(N/2)] }" },
7046 /* Perform basic isl_map_bind_domain tests.
7048 static isl_stat test_bind_map_domain(isl_ctx *ctx)
7050 int i;
7052 for (i = 0; i < ARRAY_SIZE(bind_map_domain_tests); ++i) {
7053 const char *str;
7054 isl_map *map;
7055 isl_set *set;
7056 isl_multi_id *tuple;
7057 isl_stat r;
7059 str = bind_map_domain_tests[i].map;
7060 map = isl_map_read_from_str(ctx, str);
7061 str = bind_map_domain_tests[i].tuple;
7062 tuple = isl_multi_id_read_from_str(ctx, str);
7063 set = isl_map_bind_domain(map, tuple);
7064 str = bind_map_domain_tests[i].res;
7065 r = set_check_equal(set, str);
7066 isl_set_free(set);
7067 if (r < 0)
7068 return isl_stat_error;
7071 return isl_stat_ok;
7074 /* Inputs for isl_union_map_bind_range tests.
7075 * "map" is the input union map.
7076 * "tuple" is the binding tuple.
7077 * "res" is the expected result.
7079 struct {
7080 const char *map;
7081 const char *tuple;
7082 const char *res;
7083 } bind_umap_range_tests[] = {
7084 { "{ B[N, M] -> A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
7085 "{ A[M, N] }",
7086 "[M, N] -> { B[N, M] : M mod 2 = 0 and N mod 8 = 3 }" },
7087 { "{ B[N, M] -> A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
7088 "{ B[M, N] }",
7089 "{ }" },
7090 { "{ A[] -> B[]; C[] -> D[]; E[] -> B[] }",
7091 "{ B[] }",
7092 "{ A[]; E[] }" },
7095 /* Perform basic isl_union_map_bind_range tests.
7097 static isl_stat test_bind_umap_range(isl_ctx *ctx)
7099 int i;
7101 for (i = 0; i < ARRAY_SIZE(bind_umap_range_tests); ++i) {
7102 const char *str;
7103 isl_union_map *umap;
7104 isl_union_set *uset;
7105 isl_multi_id *tuple;
7106 isl_stat r;
7108 str = bind_umap_range_tests[i].map;
7109 umap = isl_union_map_read_from_str(ctx, str);
7110 str = bind_umap_range_tests[i].tuple;
7111 tuple = isl_multi_id_read_from_str(ctx, str);
7112 uset = isl_union_map_bind_range(umap, tuple);
7113 str = bind_umap_range_tests[i].res;
7114 r = uset_check_equal(uset, str);
7115 isl_union_set_free(uset);
7116 if (r < 0)
7117 return isl_stat_error;
7120 return isl_stat_ok;
7123 /* Inputs for isl_pw_multi_aff_bind_domain tests.
7124 * "pma" is the input expression.
7125 * "tuple" is the binding tuple.
7126 * "res" is the expected result.
7128 struct {
7129 const char *pma;
7130 const char *tuple;
7131 const char *res;
7132 } bind_pma_domain_tests[] = {
7133 { "{ A[M, N] -> [M + floor(N/2)] }",
7134 "{ A[M, N] }",
7135 "[M, N] -> { [M + floor(N/2)] }" },
7136 { "{ B[N, M] -> [M + floor(N/2)] }",
7137 "{ B[N, M] }",
7138 "[N, M] -> { [M + floor(N/2)] }" },
7139 { "[M] -> { C[N] -> [M + floor(N/2)] }",
7140 "{ C[N] }",
7141 "[M, N] -> { [M + floor(N/2)] }" },
7142 { "[M] -> { C[x, N] -> [x + floor(N/2)] }",
7143 "{ C[M, N] }",
7144 "[M, N] -> { [M + floor(N/2)] }" },
7145 { "[M] -> { C[x, N] -> [M + floor(N/2)] }",
7146 "{ C[M, N] }",
7147 "[M, N] -> { [M + floor(N/2)] }" },
7148 { "[A, M] -> { C[N, x] -> [x + floor(N/2)] }",
7149 "{ C[N, M] }",
7150 "[A, N, M] -> { [M + floor(N/2)] }" },
7153 /* Perform basic isl_pw_multi_aff_bind_domain tests.
7155 static isl_stat test_bind_pma_domain(isl_ctx *ctx)
7157 int i;
7159 for (i = 0; i < ARRAY_SIZE(bind_pma_domain_tests); ++i) {
7160 const char *str;
7161 isl_pw_multi_aff *pma;
7162 isl_multi_id *tuple;
7163 isl_stat r;
7165 str = bind_pma_domain_tests[i].pma;
7166 pma = isl_pw_multi_aff_read_from_str(ctx, str);
7167 str = bind_pma_domain_tests[i].tuple;
7168 tuple = isl_multi_id_read_from_str(ctx, str);
7169 pma = isl_pw_multi_aff_bind_domain(pma, tuple);
7170 str = bind_pma_domain_tests[i].res;
7171 r = pw_multi_aff_check_plain_equal(pma, str);
7172 isl_pw_multi_aff_free(pma);
7173 if (r < 0)
7174 return isl_stat_error;
7177 return isl_stat_ok;
7180 /* Inputs for isl_pw_multi_aff_bind_domain_wrapped_domain tests.
7181 * "pma" is the input expression.
7182 * "tuple" is the binding tuple.
7183 * "res" is the expected result.
7185 struct {
7186 const char *pma;
7187 const char *tuple;
7188 const char *res;
7189 } bind_pma_domain_wrapped_tests[] = {
7190 { "{ [A[M, N] -> B[]] -> [M + floor(N/2)] }",
7191 "{ A[M, N] }",
7192 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
7193 { "{ [B[N, M] -> D[]] -> [M + floor(N/2)] }",
7194 "{ B[N, M] }",
7195 "[N, M] -> { D[] -> [M + floor(N/2)] }" },
7196 { "[M] -> { [C[N] -> B[x]] -> [x + M + floor(N/2)] }",
7197 "{ C[N] }",
7198 "[M, N] -> { B[x] -> [x + M + floor(N/2)] }" },
7199 { "[M] -> { [C[x, N] -> B[]] -> [x + floor(N/2)] }",
7200 "{ C[M, N] }",
7201 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
7202 { "[M] -> { [C[x, N] -> B[]] -> [M + floor(N/2)] }",
7203 "{ C[M, N] }",
7204 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
7205 { "[A, M] -> { [C[N, x] -> B[]] -> [x + floor(N/2)] }",
7206 "{ C[N, M] }",
7207 "[A, N, M] -> { B[] -> [M + floor(N/2)] }" },
7210 /* Perform basic isl_pw_multi_aff_bind_domain_wrapped_domain tests.
7212 static isl_stat test_bind_pma_domain_wrapped(isl_ctx *ctx)
7214 int i;
7216 for (i = 0; i < ARRAY_SIZE(bind_pma_domain_wrapped_tests); ++i) {
7217 const char *str;
7218 isl_pw_multi_aff *pma;
7219 isl_multi_id *tuple;
7220 isl_stat r;
7222 str = bind_pma_domain_wrapped_tests[i].pma;
7223 pma = isl_pw_multi_aff_read_from_str(ctx, str);
7224 str = bind_pma_domain_wrapped_tests[i].tuple;
7225 tuple = isl_multi_id_read_from_str(ctx, str);
7226 pma = isl_pw_multi_aff_bind_domain_wrapped_domain(pma, tuple);
7227 str = bind_pma_domain_wrapped_tests[i].res;
7228 r = pw_multi_aff_check_plain_equal(pma, str);
7229 isl_pw_multi_aff_free(pma);
7230 if (r < 0)
7231 return isl_stat_error;
7234 return isl_stat_ok;
7237 /* Inputs for isl_aff_bind_id tests.
7238 * "aff" is the input expression.
7239 * "id" is the binding id.
7240 * "res" is the expected result.
7242 static
7243 struct {
7244 const char *aff;
7245 const char *id;
7246 const char *res;
7247 } bind_aff_tests[] = {
7248 { "{ [4] }", "M", "[M = 4] -> { : }" },
7249 { "{ B[x] -> [floor(x/2)] }", "M", "[M] -> { B[x] : M = floor(x/2) }" },
7250 { "[M] -> { [4] }", "M", "[M = 4] -> { : }" },
7251 { "[M] -> { [floor(M/2)] }", "M", "[M] -> { : floor(M/2) = M }" },
7252 { "{ [NaN] }", "M", "{ : false }" },
7253 { "{ A[x] -> [NaN] }", "M", "{ A[x] : false }" },
7256 /* Perform basic isl_aff_bind_id tests.
7258 static isl_stat test_bind_aff(isl_ctx *ctx)
7260 int i;
7262 for (i = 0; i < ARRAY_SIZE(bind_aff_tests); ++i) {
7263 isl_aff *aff;
7264 isl_set *res;
7265 isl_id *id;
7266 isl_stat r;
7268 aff = isl_aff_read_from_str(ctx, bind_aff_tests[i].aff);
7269 id = isl_id_read_from_str(ctx, bind_aff_tests[i].id);
7270 res = isl_set_from_basic_set(isl_aff_bind_id(aff, id));
7271 r = set_check_equal(res, bind_aff_tests[i].res);
7272 isl_set_free(res);
7273 if (r < 0)
7274 return isl_stat_error;
7277 return isl_stat_ok;
7280 /* Inputs for isl_pw_aff_bind_id tests.
7281 * "pa" is the input expression.
7282 * "id" is the binding id.
7283 * "res" is the expected result.
7285 static
7286 struct {
7287 const char *pa;
7288 const char *id;
7289 const char *res;
7290 } bind_pa_tests[] = {
7291 { "{ [4] }", "M", "[M = 4] -> { : }" },
7292 { "{ B[x] -> [floor(x/2)] }", "M", "[M] -> { B[x] : M = floor(x/2) }" },
7293 { "[M] -> { [4] }", "M", "[M = 4] -> { : }" },
7294 { "[M] -> { [floor(M/2)] }", "M", "[M] -> { : floor(M/2) = M }" },
7295 { "[M] -> { [M] : M >= 0; [-M] : M < 0 }", "M", "[M] -> { : M >= 0 }" },
7296 { "{ [NaN] }", "M", "{ : false }" },
7297 { "{ A[x] -> [NaN] }", "M", "{ A[x] : false }" },
7300 /* Perform basic isl_pw_aff_bind_id tests.
7302 static isl_stat test_bind_pa(isl_ctx *ctx)
7304 int i;
7306 for (i = 0; i < ARRAY_SIZE(bind_pa_tests); ++i) {
7307 isl_pw_aff *pa;
7308 isl_set *res;
7309 isl_id *id;
7310 isl_stat r;
7312 pa = isl_pw_aff_read_from_str(ctx, bind_pa_tests[i].pa);
7313 id = isl_id_read_from_str(ctx, bind_pa_tests[i].id);
7314 res = isl_pw_aff_bind_id(pa, id);
7315 r = set_check_equal(res, bind_pa_tests[i].res);
7316 isl_set_free(res);
7317 if (r < 0)
7318 return isl_stat_error;
7321 return isl_stat_ok;
7324 /* Inputs for isl_multi_union_pw_aff_bind tests.
7325 * "mupa" is the input expression.
7326 * "tuple" is the binding tuple.
7327 * "res" is the expected result.
7329 static
7330 struct {
7331 const char *mupa;
7332 const char *tuple;
7333 const char *res;
7334 } bind_mupa_tests[] = {
7335 { "A[{ [4] }, { [5] }]",
7336 "{ A[M, N] }",
7337 "[M = 4, N = 5] -> { : }" },
7338 { "A[{ B[x] -> [floor(x/2)] }, { B[y] -> [y + 5] }]",
7339 "{ A[M, N] }",
7340 "[M, N] -> { B[x] : M = floor(x/2) and N = x + 5 }" },
7341 { "[M] -> A[{ [4] }, { [M + 1] }]",
7342 "{ A[M, N] }",
7343 "[M = 4, N = 5] -> { : }" },
7346 /* Perform basic isl_multi_union_pw_aff_bind tests.
7348 static isl_stat test_bind_mupa(isl_ctx *ctx)
7350 int i;
7352 for (i = 0; i < ARRAY_SIZE(bind_mupa_tests); ++i) {
7353 const char *str;
7354 isl_multi_union_pw_aff *mupa;
7355 isl_union_set *res;
7356 isl_multi_id *tuple;
7357 isl_stat r;
7359 str = bind_mupa_tests[i].mupa;
7360 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7361 str = bind_mupa_tests[i].tuple;
7362 tuple = isl_multi_id_read_from_str(ctx, str);
7363 res = isl_multi_union_pw_aff_bind(mupa, tuple);
7364 r = uset_check_equal(res, bind_mupa_tests[i].res);
7365 isl_union_set_free(res);
7366 if (r < 0)
7367 return isl_stat_error;
7370 return isl_stat_ok;
7373 /* Perform tests that reinterpret dimensions as parameters.
7375 static int test_bind(isl_ctx *ctx)
7377 if (test_bind_set(ctx) < 0)
7378 return -1;
7379 if (test_bind_map_domain(ctx) < 0)
7380 return -1;
7381 if (test_bind_umap_range(ctx) < 0)
7382 return -1;
7383 if (test_bind_pma_domain(ctx) < 0)
7384 return -1;
7385 if (test_bind_pma_domain_wrapped(ctx) < 0)
7386 return -1;
7387 if (test_bind_aff(ctx) < 0)
7388 return -1;
7389 if (test_bind_pa(ctx) < 0)
7390 return -1;
7391 if (test_bind_mupa(ctx) < 0)
7392 return -1;
7394 return 0;
7397 /* Inputs for isl_set_unbind_params tests.
7398 * "set" is the input parameter domain.
7399 * "tuple" is the tuple of the constructed set.
7400 * "res" is the expected result.
7402 struct {
7403 const char *set;
7404 const char *tuple;
7405 const char *res;
7406 } unbind_set_tests[] = {
7407 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7408 "{ A[M, N] }",
7409 "{ A[M, N] : M mod 2 = 0 and N mod 8 = 3 }" },
7410 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7411 "{ B[N, M] }",
7412 "{ B[N, M] : M mod 2 = 0 and N mod 8 = 3 }" },
7413 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7414 "{ C[N] }",
7415 "[M] -> { C[N] : M mod 2 = 0 and N mod 8 = 3 }" },
7416 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7417 "{ D[T, N] }",
7418 "[M] -> { D[x, N] : M mod 2 = 0 and N mod 8 = 3 }" },
7421 /* Perform basic isl_set_unbind_params tests.
7423 static isl_stat test_unbind_set(isl_ctx *ctx)
7425 int i;
7427 for (i = 0; i < ARRAY_SIZE(unbind_set_tests); ++i) {
7428 const char *str;
7429 isl_set *set;
7430 isl_multi_id *tuple;
7431 isl_stat r;
7433 set = isl_set_read_from_str(ctx, unbind_set_tests[i].set);
7434 str = unbind_set_tests[i].tuple;
7435 tuple = isl_multi_id_read_from_str(ctx, str);
7436 set = isl_set_unbind_params(set, tuple);
7437 r = set_check_equal(set, unbind_set_tests[i].res);
7438 isl_set_free(set);
7439 if (r < 0)
7440 return isl_stat_error;
7443 return isl_stat_ok;
7446 /* Inputs for isl_aff_unbind_params_insert_domain tests.
7447 * "aff" is the input affine expression defined over a parameter domain.
7448 * "tuple" is the tuple of the domain that gets introduced.
7449 * "res" is the expected result.
7451 struct {
7452 const char *aff;
7453 const char *tuple;
7454 const char *res;
7455 } unbind_aff_tests[] = {
7456 { "[M, N] -> { [M + floor(N/2)] }",
7457 "{ A[M, N] }",
7458 "{ A[M, N] -> [M + floor(N/2)] }" },
7459 { "[M, N] -> { [M + floor(N/2)] }",
7460 "{ B[N, M] }",
7461 "{ B[N, M] -> [M + floor(N/2)] }" },
7462 { "[M, N] -> { [M + floor(N/2)] }",
7463 "{ C[N] }",
7464 "[M] -> { C[N] -> [M + floor(N/2)] }" },
7465 { "[M, N] -> { [M + floor(N/2)] }",
7466 "{ D[A, B, C, N, Z] }",
7467 "[M] -> { D[A, B, C, N, Z] -> [M + floor(N/2)] }" },
7470 /* Perform basic isl_aff_unbind_params_insert_domain tests.
7472 static isl_stat test_unbind_aff(isl_ctx *ctx)
7474 int i;
7476 for (i = 0; i < ARRAY_SIZE(unbind_aff_tests); ++i) {
7477 const char *str;
7478 isl_aff *aff;
7479 isl_multi_id *tuple;
7480 isl_stat r;
7482 aff = isl_aff_read_from_str(ctx, unbind_aff_tests[i].aff);
7483 str = unbind_aff_tests[i].tuple;
7484 tuple = isl_multi_id_read_from_str(ctx, str);
7485 aff = isl_aff_unbind_params_insert_domain(aff, tuple);
7486 r = aff_check_plain_equal(aff, unbind_aff_tests[i].res);
7487 isl_aff_free(aff);
7488 if (r < 0)
7489 return isl_stat_error;
7492 return isl_stat_ok;
7495 /* Inputs for isl_multi_aff_unbind_params_insert_domain tests.
7496 * "ma" is the input multi affine expression defined over a parameter domain.
7497 * "tuple" is the tuple of the domain that gets introduced.
7498 * "res" is the expected result.
7500 static struct {
7501 const char *ma;
7502 const char *tuple;
7503 const char *res;
7504 } unbind_multi_aff_tests[] = {
7505 { "[M, N] -> { T[M + floor(N/2)] }",
7506 "{ A[M, N] }",
7507 "{ A[M, N] -> T[M + floor(N/2)] }" },
7508 { "[M, N] -> { [M + floor(N/2)] }",
7509 "{ B[N, M] }",
7510 "{ B[N, M] -> [M + floor(N/2)] }" },
7511 { "[M, N] -> { [M + floor(N/2)] }",
7512 "{ C[N] }",
7513 "[M] -> { C[N] -> [M + floor(N/2)] }" },
7514 { "[M, N] -> { [M + floor(N/2)] }",
7515 "{ D[A, B, C, N, Z] }",
7516 "[M] -> { D[A, B, C, N, Z] -> [M + floor(N/2)] }" },
7517 { "[M, N] -> { T[M + floor(N/2), N + floor(M/3)] }",
7518 "{ A[M, N] }",
7519 "{ A[M, N] -> T[M + floor(N/2), N + floor(M/3)] }" },
7522 /* Perform basic isl_multi_aff_unbind_params_insert_domain tests.
7524 static isl_stat test_unbind_multi_aff(isl_ctx *ctx)
7526 int i;
7528 for (i = 0; i < ARRAY_SIZE(unbind_multi_aff_tests); ++i) {
7529 const char *str;
7530 isl_multi_aff *ma;
7531 isl_multi_id *tuple;
7532 isl_stat r;
7534 str = unbind_multi_aff_tests[i].ma;
7535 ma = isl_multi_aff_read_from_str(ctx, str);
7536 str = unbind_multi_aff_tests[i].tuple;
7537 tuple = isl_multi_id_read_from_str(ctx, str);
7538 ma = isl_multi_aff_unbind_params_insert_domain(ma, tuple);
7539 str = unbind_multi_aff_tests[i].res;
7540 r = multi_aff_check_plain_equal(ma, str);
7541 isl_multi_aff_free(ma);
7542 if (r < 0)
7543 return isl_stat_error;
7546 return isl_stat_ok;
7549 /* Perform tests that reinterpret parameters.
7551 static int test_unbind(isl_ctx *ctx)
7553 if (test_unbind_set(ctx) < 0)
7554 return -1;
7555 if (test_unbind_aff(ctx) < 0)
7556 return -1;
7557 if (test_unbind_multi_aff(ctx) < 0)
7558 return -1;
7560 return 0;
7563 /* Check that "pa" consists of a single expression.
7565 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
7567 isl_size n;
7569 n = isl_pw_aff_n_piece(pa);
7570 isl_pw_aff_free(pa);
7572 if (n < 0)
7573 return -1;
7574 if (n != 1)
7575 isl_die(ctx, isl_error_unknown, "expecting single expression",
7576 return -1);
7578 return 0;
7581 /* Check that the computation below results in a single expression.
7582 * One or two expressions may result depending on which constraint
7583 * ends up being considered as redundant with respect to the other
7584 * constraints after the projection that is performed internally
7585 * by isl_set_dim_min.
7587 static int test_dim_max_1(isl_ctx *ctx)
7589 const char *str;
7590 isl_set *set;
7591 isl_pw_aff *pa;
7593 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
7594 "-4a <= b <= 3 and b < n - 4a }";
7595 set = isl_set_read_from_str(ctx, str);
7596 pa = isl_set_dim_min(set, 0);
7597 return check_single_piece(ctx, pa);
7600 /* Check that the computation below results in a single expression.
7601 * The PIP problem corresponding to these constraints has a row
7602 * that causes a split of the solution domain. The solver should
7603 * first pick rows that split off empty parts such that the actual
7604 * solution domain does not get split.
7605 * Note that the description contains some redundant constraints.
7606 * If these constraints get removed first, then the row mentioned
7607 * above does not appear in the PIP problem.
7609 static int test_dim_max_2(isl_ctx *ctx)
7611 const char *str;
7612 isl_set *set;
7613 isl_pw_aff *pa;
7615 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
7616 "N > 0 and P >= 0 }";
7617 set = isl_set_read_from_str(ctx, str);
7618 pa = isl_set_dim_max(set, 0);
7619 return check_single_piece(ctx, pa);
7622 int test_dim_max(isl_ctx *ctx)
7624 int equal;
7625 const char *str;
7626 isl_set *set1, *set2;
7627 isl_set *set;
7628 isl_map *map;
7629 isl_pw_aff *pwaff;
7631 if (test_dim_max_1(ctx) < 0)
7632 return -1;
7633 if (test_dim_max_2(ctx) < 0)
7634 return -1;
7636 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
7637 set = isl_set_read_from_str(ctx, str);
7638 pwaff = isl_set_dim_max(set, 0);
7639 set1 = isl_set_from_pw_aff(pwaff);
7640 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
7641 set2 = isl_set_read_from_str(ctx, str);
7642 equal = isl_set_is_equal(set1, set2);
7643 isl_set_free(set1);
7644 isl_set_free(set2);
7645 if (equal < 0)
7646 return -1;
7647 if (!equal)
7648 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7650 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
7651 set = isl_set_read_from_str(ctx, str);
7652 pwaff = isl_set_dim_max(set, 0);
7653 set1 = isl_set_from_pw_aff(pwaff);
7654 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
7655 set2 = isl_set_read_from_str(ctx, str);
7656 equal = isl_set_is_equal(set1, set2);
7657 isl_set_free(set1);
7658 isl_set_free(set2);
7659 if (equal < 0)
7660 return -1;
7661 if (!equal)
7662 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7664 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
7665 set = isl_set_read_from_str(ctx, str);
7666 pwaff = isl_set_dim_max(set, 0);
7667 set1 = isl_set_from_pw_aff(pwaff);
7668 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
7669 set2 = isl_set_read_from_str(ctx, str);
7670 equal = isl_set_is_equal(set1, set2);
7671 isl_set_free(set1);
7672 isl_set_free(set2);
7673 if (equal < 0)
7674 return -1;
7675 if (!equal)
7676 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7678 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
7679 "0 <= i < N and 0 <= j < M }";
7680 map = isl_map_read_from_str(ctx, str);
7681 set = isl_map_range(map);
7683 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
7684 set1 = isl_set_from_pw_aff(pwaff);
7685 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
7686 set2 = isl_set_read_from_str(ctx, str);
7687 equal = isl_set_is_equal(set1, set2);
7688 isl_set_free(set1);
7689 isl_set_free(set2);
7691 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
7692 set1 = isl_set_from_pw_aff(pwaff);
7693 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
7694 set2 = isl_set_read_from_str(ctx, str);
7695 if (equal >= 0 && equal)
7696 equal = isl_set_is_equal(set1, set2);
7697 isl_set_free(set1);
7698 isl_set_free(set2);
7700 isl_set_free(set);
7702 if (equal < 0)
7703 return -1;
7704 if (!equal)
7705 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7707 /* Check that solutions are properly merged. */
7708 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
7709 "c <= -1 + n - 4a - 2b and c >= -2b and "
7710 "4a >= -4 + n and c >= 0 }";
7711 set = isl_set_read_from_str(ctx, str);
7712 pwaff = isl_set_dim_min(set, 2);
7713 set1 = isl_set_from_pw_aff(pwaff);
7714 str = "[n] -> { [(0)] : n >= 1 }";
7715 set2 = isl_set_read_from_str(ctx, str);
7716 equal = isl_set_is_equal(set1, set2);
7717 isl_set_free(set1);
7718 isl_set_free(set2);
7720 if (equal < 0)
7721 return -1;
7722 if (!equal)
7723 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7725 /* Check that empty solution lie in the right space. */
7726 str = "[n] -> { [t,a] : 1 = 0 }";
7727 set = isl_set_read_from_str(ctx, str);
7728 pwaff = isl_set_dim_max(set, 0);
7729 set1 = isl_set_from_pw_aff(pwaff);
7730 str = "[n] -> { [t] : 1 = 0 }";
7731 set2 = isl_set_read_from_str(ctx, str);
7732 equal = isl_set_is_equal(set1, set2);
7733 isl_set_free(set1);
7734 isl_set_free(set2);
7736 if (equal < 0)
7737 return -1;
7738 if (!equal)
7739 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7741 return 0;
7744 /* Basic test for isl_pw_multi_aff_product.
7746 * Check that multiple pieces are properly handled.
7748 static int test_product_pma(isl_ctx *ctx)
7750 isl_stat equal;
7751 const char *str;
7752 isl_pw_multi_aff *pma1, *pma2;
7754 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
7755 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
7756 str = "{ C[] -> D[] }";
7757 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
7758 pma1 = isl_pw_multi_aff_product(pma1, pma2);
7759 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
7760 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
7761 equal = pw_multi_aff_check_plain_equal(pma1, str);
7762 isl_pw_multi_aff_free(pma1);
7763 if (equal < 0)
7764 return -1;
7766 return 0;
7769 int test_product(isl_ctx *ctx)
7771 const char *str;
7772 isl_set *set;
7773 isl_union_set *uset1, *uset2;
7774 int ok;
7776 str = "{ A[i] }";
7777 set = isl_set_read_from_str(ctx, str);
7778 set = isl_set_product(set, isl_set_copy(set));
7779 ok = isl_set_is_wrapping(set);
7780 isl_set_free(set);
7781 if (ok < 0)
7782 return -1;
7783 if (!ok)
7784 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7786 str = "{ [] }";
7787 uset1 = isl_union_set_read_from_str(ctx, str);
7788 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
7789 str = "{ [[] -> []] }";
7790 uset2 = isl_union_set_read_from_str(ctx, str);
7791 ok = isl_union_set_is_equal(uset1, uset2);
7792 isl_union_set_free(uset1);
7793 isl_union_set_free(uset2);
7794 if (ok < 0)
7795 return -1;
7796 if (!ok)
7797 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7799 if (test_product_pma(ctx) < 0)
7800 return -1;
7802 return 0;
7805 /* Check that two sets are not considered disjoint just because
7806 * they have a different set of (named) parameters.
7808 static int test_disjoint(isl_ctx *ctx)
7810 const char *str;
7811 isl_set *set, *set2;
7812 int disjoint;
7814 str = "[n] -> { [[]->[]] }";
7815 set = isl_set_read_from_str(ctx, str);
7816 str = "{ [[]->[]] }";
7817 set2 = isl_set_read_from_str(ctx, str);
7818 disjoint = isl_set_is_disjoint(set, set2);
7819 isl_set_free(set);
7820 isl_set_free(set2);
7821 if (disjoint < 0)
7822 return -1;
7823 if (disjoint)
7824 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7826 return 0;
7829 /* Inputs for isl_pw_multi_aff_is_equal tests.
7830 * "f1" and "f2" are the two function that need to be compared.
7831 * "equal" is the expected result.
7833 struct {
7834 int equal;
7835 const char *f1;
7836 const char *f2;
7837 } pma_equal_tests[] = {
7838 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
7839 "[N] -> { [0] : 0 <= N <= 1 }" },
7840 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
7841 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
7842 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
7843 "[N] -> { [0] : 0 <= N <= 1 }" },
7844 { 0, "{ [NaN] }", "{ [NaN] }" },
7847 int test_equal(isl_ctx *ctx)
7849 int i;
7850 const char *str;
7851 isl_set *set, *set2;
7852 int equal;
7854 str = "{ S_6[i] }";
7855 set = isl_set_read_from_str(ctx, str);
7856 str = "{ S_7[i] }";
7857 set2 = isl_set_read_from_str(ctx, str);
7858 equal = isl_set_is_equal(set, set2);
7859 isl_set_free(set);
7860 isl_set_free(set2);
7861 if (equal < 0)
7862 return -1;
7863 if (equal)
7864 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7866 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
7867 int expected = pma_equal_tests[i].equal;
7868 isl_pw_multi_aff *f1, *f2;
7870 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
7871 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
7872 equal = isl_pw_multi_aff_is_equal(f1, f2);
7873 isl_pw_multi_aff_free(f1);
7874 isl_pw_multi_aff_free(f2);
7875 if (equal < 0)
7876 return -1;
7877 if (equal != expected)
7878 isl_die(ctx, isl_error_unknown,
7879 "unexpected equality result", return -1);
7882 return 0;
7885 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
7886 enum isl_dim_type type, unsigned pos, int fixed)
7888 isl_bool test;
7890 test = isl_map_plain_is_fixed(map, type, pos, NULL);
7891 isl_map_free(map);
7892 if (test < 0)
7893 return -1;
7894 if (test == fixed)
7895 return 0;
7896 if (fixed)
7897 isl_die(ctx, isl_error_unknown,
7898 "map not detected as fixed", return -1);
7899 else
7900 isl_die(ctx, isl_error_unknown,
7901 "map detected as fixed", return -1);
7904 int test_fixed(isl_ctx *ctx)
7906 const char *str;
7907 isl_map *map;
7909 str = "{ [i] -> [i] }";
7910 map = isl_map_read_from_str(ctx, str);
7911 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
7912 return -1;
7913 str = "{ [i] -> [1] }";
7914 map = isl_map_read_from_str(ctx, str);
7915 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7916 return -1;
7917 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
7918 map = isl_map_read_from_str(ctx, str);
7919 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7920 return -1;
7921 map = isl_map_read_from_str(ctx, str);
7922 map = isl_map_neg(map);
7923 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7924 return -1;
7926 return 0;
7929 struct isl_vertices_test_data {
7930 const char *set;
7931 int n;
7932 const char *vertex[6];
7933 } vertices_tests[] = {
7934 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
7935 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
7936 { "{ A[t, i] : t = 14 and i = 1 }",
7937 1, { "{ A[14, 1] }" } },
7938 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
7939 "c <= m and m <= n and m > 0 }",
7940 6, {
7941 "[n, m] -> { [n, m, m] : 0 < m <= n }",
7942 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
7943 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
7944 "[n, m] -> { [m, m, m] : 0 < m <= n }",
7945 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
7946 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
7947 } },
7948 /* An input with implicit equality constraints among the parameters. */
7949 { "[N, M] -> { [a, b] : M >= 3 and 9 + 3M <= a <= 29 + 2N + 11M and "
7950 "2b >= M + a and 5 - 2N - M + a <= 2b <= 3 + a and "
7951 "3b >= 15 + a }",
7952 2, {
7953 "[N, M] -> { [(21), (12)] : M = 3 and N >= 0 }",
7954 "[N, M] -> { [(61 + 2N), (32 + N)] : M = 3 and N >= 0 }",
7959 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
7961 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
7963 struct isl_vertices_test_data *data = user;
7964 isl_ctx *ctx;
7965 isl_multi_aff *ma;
7966 isl_basic_set *bset;
7967 isl_pw_multi_aff *pma;
7968 int i;
7969 isl_bool equal;
7971 ctx = isl_vertex_get_ctx(vertex);
7972 bset = isl_vertex_get_domain(vertex);
7973 ma = isl_vertex_get_expr(vertex);
7974 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
7976 for (i = 0; i < data->n; ++i) {
7977 isl_pw_multi_aff *pma_i;
7979 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
7980 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
7981 isl_pw_multi_aff_free(pma_i);
7983 if (equal < 0 || equal)
7984 break;
7987 isl_pw_multi_aff_free(pma);
7988 isl_vertex_free(vertex);
7990 if (equal < 0)
7991 return isl_stat_error;
7993 return equal ? isl_stat_ok : isl_stat_error;
7996 int test_vertices(isl_ctx *ctx)
7998 int i;
8000 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
8001 isl_basic_set *bset;
8002 isl_vertices *vertices;
8003 int ok = 1;
8004 isl_size n;
8006 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
8007 vertices = isl_basic_set_compute_vertices(bset);
8008 n = isl_vertices_get_n_vertices(vertices);
8009 if (vertices_tests[i].n != n)
8010 ok = 0;
8011 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
8012 &vertices_tests[i]) < 0)
8013 ok = 0;
8014 isl_vertices_free(vertices);
8015 isl_basic_set_free(bset);
8017 if (n < 0)
8018 return -1;
8019 if (!ok)
8020 isl_die(ctx, isl_error_unknown, "unexpected vertices",
8021 return -1);
8024 return 0;
8027 /* Inputs for basic tests of unary operations on isl_union_map.
8028 * "fn" is the function that is being tested.
8029 * "arg" is a string description of the input.
8030 * "res" is a string description of the expected result.
8032 static struct {
8033 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap);
8034 const char *arg;
8035 const char *res;
8036 } umap_un_tests[] = {
8037 { &isl_union_map_range_reverse,
8038 "{ A[] -> [B[] -> C[]]; A[] -> B[]; A[0] -> N[B[1] -> B[2]] }",
8039 "{ A[] -> [C[] -> B[]]; A[0] -> N[B[2] -> B[1]] }" },
8040 { &isl_union_map_range_reverse,
8041 "{ A[] -> N[B[] -> C[]] }",
8042 "{ A[] -> [C[] -> B[]] }" },
8043 { &isl_union_map_range_reverse,
8044 "{ A[] -> N[B[x] -> B[y]] }",
8045 "{ A[] -> N[B[*] -> B[*]] }" },
8048 /* Perform basic tests of unary operations on isl_union_map.
8050 static isl_stat test_un_union_map(isl_ctx *ctx)
8052 int i;
8054 for (i = 0; i < ARRAY_SIZE(umap_un_tests); ++i) {
8055 const char *str;
8056 isl_union_map *umap, *res;
8057 isl_bool equal;
8059 str = umap_un_tests[i].arg;
8060 umap = isl_union_map_read_from_str(ctx, str);
8061 str = umap_un_tests[i].res;
8062 res = isl_union_map_read_from_str(ctx, str);
8063 umap = umap_un_tests[i].fn(umap);
8064 equal = isl_union_map_is_equal(umap, res);
8065 isl_union_map_free(umap);
8066 isl_union_map_free(res);
8067 if (equal < 0)
8068 return isl_stat_error;
8069 if (!equal)
8070 isl_die(ctx, isl_error_unknown,
8071 "unexpected result", return isl_stat_error);
8074 return isl_stat_ok;
8077 /* Inputs for basic tests of binary operations on isl_union_map.
8078 * "fn" is the function that is being tested.
8079 * "arg1" and "arg2" are string descriptions of the inputs.
8080 * "res" is a string description of the expected result.
8082 static struct {
8083 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap1,
8084 __isl_take isl_union_map *umap2);
8085 const char *arg1;
8086 const char *arg2;
8087 const char *res;
8088 } umap_bin_tests[] = {
8089 { &isl_union_map_intersect,
8090 "[n] -> { A[i] -> [] : 0 <= i <= n; B[] -> [] }",
8091 "[m] -> { A[i] -> [] : 0 <= i <= m; C[] -> [] }",
8092 "[m, n] -> { A[i] -> [] : 0 <= i <= n and i <= m }" },
8093 { &isl_union_map_intersect_domain_factor_domain,
8094 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8095 "[N] -> { B[i] -> C[N] }",
8096 "{ }" },
8097 { &isl_union_map_intersect_domain_factor_domain,
8098 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8099 "[N] -> { A[i] -> C[N] }",
8100 "[N] -> { [A[N - 2] -> B[N - 1]] -> C[N] }" },
8101 { &isl_union_map_intersect_domain_factor_domain,
8102 "{ T[A[i] -> B[i + 1]] -> C[i + 2] }",
8103 "[N] -> { A[i] -> C[N] }",
8104 "[N] -> { T[A[N - 2] -> B[N - 1]] -> C[N] }" },
8105 { &isl_union_map_intersect_domain_factor_range,
8106 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8107 "[N] -> { B[i] -> C[N] }",
8108 "[N] -> { [A[N - 2] -> B[N - 1]] -> C[N] }" },
8109 { &isl_union_map_intersect_domain_factor_range,
8110 "{ T[A[i] -> B[i + 1]] -> C[i + 2] }",
8111 "[N] -> { B[i] -> C[N] }",
8112 "[N] -> { T[A[N - 2] -> B[N - 1]] -> C[N] }" },
8113 { &isl_union_map_intersect_domain_factor_range,
8114 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8115 "[N] -> { A[i] -> C[N] }",
8116 "{ }" },
8117 { &isl_union_map_intersect_range_factor_domain,
8118 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8119 "[N] -> { A[i] -> B[N] }",
8120 "[N] -> { A[N - 1] -> [B[N] -> C[N + 1]] }" },
8121 { &isl_union_map_intersect_range_factor_domain,
8122 "{ A[i] -> T[B[i + 1] -> C[i + 2]] }",
8123 "[N] -> { A[i] -> B[N] }",
8124 "[N] -> { A[N - 1] -> T[B[N] -> C[N + 1]] }" },
8125 { &isl_union_map_intersect_range_factor_domain,
8126 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8127 "[N] -> { A[i] -> C[N] }",
8128 "{ }" },
8129 { &isl_union_map_intersect_range_factor_range,
8130 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8131 "[N] -> { A[i] -> C[N] }",
8132 "[N] -> { A[N - 2] -> [B[N - 1] -> C[N]] }" },
8133 { &isl_union_map_intersect_range_factor_range,
8134 "{ A[i] -> T[B[i + 1] -> C[i + 2]] }",
8135 "[N] -> { A[i] -> C[N] }",
8136 "[N] -> { A[N - 2] -> T[B[N - 1] -> C[N]] }" },
8137 { &isl_union_map_intersect_range_factor_range,
8138 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8139 "[N] -> { A[i] -> B[N] }",
8140 "{ }" },
8143 /* Perform basic tests of binary operations on isl_union_map.
8145 static isl_stat test_bin_union_map(isl_ctx *ctx)
8147 int i;
8149 for (i = 0; i < ARRAY_SIZE(umap_bin_tests); ++i) {
8150 const char *str;
8151 isl_union_map *umap1, *umap2, *res;
8152 isl_bool equal;
8154 str = umap_bin_tests[i].arg1;
8155 umap1 = isl_union_map_read_from_str(ctx, str);
8156 str = umap_bin_tests[i].arg2;
8157 umap2 = isl_union_map_read_from_str(ctx, str);
8158 str = umap_bin_tests[i].res;
8159 res = isl_union_map_read_from_str(ctx, str);
8160 umap1 = umap_bin_tests[i].fn(umap1, umap2);
8161 equal = isl_union_map_is_equal(umap1, res);
8162 isl_union_map_free(umap1);
8163 isl_union_map_free(res);
8164 if (equal < 0)
8165 return isl_stat_error;
8166 if (!equal)
8167 isl_die(ctx, isl_error_unknown,
8168 "unexpected result", return isl_stat_error);
8171 return isl_stat_ok;
8174 /* Check that isl_union_set_contains finds space independently
8175 * of the parameters.
8177 static isl_stat test_union_set_contains(isl_ctx *ctx)
8179 const char *str;
8180 isl_bool ok;
8181 isl_space *space;
8182 isl_id *id;
8183 isl_union_set *uset;
8185 str = "[N] -> { A[0:N]; B[*,*] }";
8186 uset = isl_union_set_read_from_str(ctx, str);
8187 space = isl_space_unit(ctx);
8188 id = isl_id_alloc(ctx, "A", NULL);
8189 space = isl_space_add_named_tuple_id_ui(space, id, 1);
8190 ok = isl_union_set_contains(uset, space);
8191 isl_space_free(space);
8192 isl_union_set_free(uset);
8194 if (ok < 0)
8195 return isl_stat_error;
8196 if (!ok)
8197 isl_die(ctx, isl_error_unknown,
8198 "unexpected result", return isl_stat_error);
8200 return isl_stat_ok;
8203 /* Perform basic tests of operations on isl_union_map or isl_union_set.
8205 static int test_union_map(isl_ctx *ctx)
8207 if (test_un_union_map(ctx) < 0)
8208 return -1;
8209 if (test_bin_union_map(ctx) < 0)
8210 return -1;
8211 if (test_union_set_contains(ctx) < 0)
8212 return -1;
8213 return 0;
8216 int test_union_pw(isl_ctx *ctx)
8218 int equal;
8219 const char *str;
8220 isl_union_set *uset;
8221 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
8223 str = "{ [x] -> x^2 }";
8224 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
8225 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
8226 uset = isl_union_pw_qpolynomial_domain(upwqp1);
8227 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
8228 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
8229 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
8230 isl_union_pw_qpolynomial_free(upwqp1);
8231 isl_union_pw_qpolynomial_free(upwqp2);
8232 if (equal < 0)
8233 return -1;
8234 if (!equal)
8235 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
8237 return 0;
8240 /* Inputs for basic tests of functions that select
8241 * subparts of the domain of an isl_multi_union_pw_aff.
8242 * "fn" is the function that is tested.
8243 * "arg" is a string description of the input.
8244 * "res" is a string description of the expected result.
8246 struct {
8247 __isl_give isl_union_set *(*fn)(
8248 __isl_take isl_multi_union_pw_aff *mupa);
8249 const char *arg;
8250 const char *res;
8251 } un_locus_tests[] = {
8252 { &isl_multi_union_pw_aff_zero_union_set,
8253 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
8254 "{ A[0,j]; B[0,j] }" },
8255 { &isl_multi_union_pw_aff_zero_union_set,
8256 "F[{ A[i,j] -> [i-j]; B[i,j] -> [i-j] : i >= 0 }]",
8257 "{ A[i,i]; B[i,i] : i >= 0 }" },
8258 { &isl_multi_union_pw_aff_zero_union_set,
8259 "(F[] : { A[i,j]; B[i,i] : i >= 0 })",
8260 "{ A[i,j]; B[i,i] : i >= 0 }" },
8263 /* Perform some basic tests of functions that select
8264 * subparts of the domain of an isl_multi_union_pw_aff.
8266 static int test_un_locus(isl_ctx *ctx)
8268 int i;
8269 isl_bool ok;
8270 isl_union_set *uset, *res;
8271 isl_multi_union_pw_aff *mupa;
8273 for (i = 0; i < ARRAY_SIZE(un_locus_tests); ++i) {
8274 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
8275 un_locus_tests[i].arg);
8276 res = isl_union_set_read_from_str(ctx, un_locus_tests[i].res);
8277 uset = un_locus_tests[i].fn(mupa);
8278 ok = isl_union_set_is_equal(uset, res);
8279 isl_union_set_free(uset);
8280 isl_union_set_free(res);
8281 if (ok < 0)
8282 return -1;
8283 if (!ok)
8284 isl_die(ctx, isl_error_unknown,
8285 "unexpected result", return -1);
8288 return 0;
8291 /* Inputs for basic tests of functions that select
8292 * subparts of an isl_union_map based on a relation
8293 * specified by an isl_multi_union_pw_aff.
8294 * "fn" is the function that is tested.
8295 * "arg1" and "arg2" are string descriptions of the inputs.
8296 * "res" is a string description of the expected result.
8298 struct {
8299 __isl_give isl_union_map *(*fn)(
8300 __isl_take isl_union_map *umap,
8301 __isl_take isl_multi_union_pw_aff *mupa);
8302 const char *arg1;
8303 const char *arg2;
8304 const char *res;
8305 } bin_locus_tests[] = {
8306 { &isl_union_map_eq_at_multi_union_pw_aff,
8307 "{ A[i,j] -> B[i',j'] }",
8308 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
8309 "{ A[i,j] -> B[i,j'] }" },
8310 { &isl_union_map_eq_at_multi_union_pw_aff,
8311 "{ A[i,j] -> B[i',j'] }",
8312 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8313 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8314 "{ A[i,j] -> B[i,j] }" },
8315 { &isl_union_map_eq_at_multi_union_pw_aff,
8316 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
8317 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
8318 "{ A[i,j] -> B[i,j'] }" },
8319 { &isl_union_map_eq_at_multi_union_pw_aff,
8320 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
8321 "F[{ A[i,j] -> [i]; B[i,j] -> [i]; C[i,j] -> [0] }]",
8322 "{ A[i,j] -> B[i,j']; A[0,j] -> C[i',j'] }" },
8323 { &isl_union_map_eq_at_multi_union_pw_aff,
8324 "{ A[i,j] -> B[i',j'] }",
8325 "F[{ A[i,j] -> [i] : i > j; B[i,j] -> [i] }]",
8326 "{ A[i,j] -> B[i,j'] : i > j }" },
8327 { &isl_union_map_lex_le_at_multi_union_pw_aff,
8328 "{ A[i,j] -> B[i',j'] }",
8329 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8330 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8331 "{ A[i,j] -> B[i',j'] : i,j <<= i',j' }" },
8332 { &isl_union_map_lex_lt_at_multi_union_pw_aff,
8333 "{ A[i,j] -> B[i',j'] }",
8334 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8335 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8336 "{ A[i,j] -> B[i',j'] : i,j << i',j' }" },
8337 { &isl_union_map_lex_ge_at_multi_union_pw_aff,
8338 "{ A[i,j] -> B[i',j'] }",
8339 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8340 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8341 "{ A[i,j] -> B[i',j'] : i,j >>= i',j' }" },
8342 { &isl_union_map_lex_gt_at_multi_union_pw_aff,
8343 "{ A[i,j] -> B[i',j'] }",
8344 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8345 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8346 "{ A[i,j] -> B[i',j'] : i,j >> i',j' }" },
8347 { &isl_union_map_eq_at_multi_union_pw_aff,
8348 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
8349 "(F[] : { A[i,j]; B[i,j] })",
8350 "{ A[i,j] -> B[i',j'] }" },
8351 { &isl_union_map_eq_at_multi_union_pw_aff,
8352 "{ A[i,j] -> B[i',j'] }",
8353 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
8354 "{ A[i,j] -> B[i',j'] : i > j and i' < j' }" },
8355 { &isl_union_map_eq_at_multi_union_pw_aff,
8356 "[N] -> { A[i,j] -> B[i',j'] : i,i' <= N }",
8357 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
8358 "[N] -> { A[i,j] -> B[i',j'] : i > j and i' < j' and i,i' <= N }" },
8359 { &isl_union_map_eq_at_multi_union_pw_aff,
8360 "{ A[i,j] -> B[i',j'] }",
8361 "[N] -> (F[] : { A[i,j] : i < N; B[i,j] : i < N })",
8362 "[N] -> { A[i,j] -> B[i',j'] : i,i' < N }" },
8363 { &isl_union_map_eq_at_multi_union_pw_aff,
8364 "{ A[i,j] -> B[i',j'] }",
8365 "[N] -> (F[] : { : N >= 0 })",
8366 "[N] -> { A[i,j] -> B[i',j'] : N >= 0 }" },
8369 /* Perform some basic tests of functions that select
8370 * subparts of an isl_union_map based on a relation
8371 * specified by an isl_multi_union_pw_aff.
8373 static int test_bin_locus(isl_ctx *ctx)
8375 int i;
8376 isl_bool ok;
8377 isl_union_map *umap, *res;
8378 isl_multi_union_pw_aff *mupa;
8380 for (i = 0; i < ARRAY_SIZE(bin_locus_tests); ++i) {
8381 umap = isl_union_map_read_from_str(ctx,
8382 bin_locus_tests[i].arg1);
8383 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
8384 bin_locus_tests[i].arg2);
8385 res = isl_union_map_read_from_str(ctx, bin_locus_tests[i].res);
8386 umap = bin_locus_tests[i].fn(umap, mupa);
8387 ok = isl_union_map_is_equal(umap, res);
8388 isl_union_map_free(umap);
8389 isl_union_map_free(res);
8390 if (ok < 0)
8391 return -1;
8392 if (!ok)
8393 isl_die(ctx, isl_error_unknown,
8394 "unexpected result", return -1);
8397 return 0;
8400 /* Inputs for basic tests of functions that determine
8401 * the part of the domain where two isl_multi_aff objects
8402 * related to each other in a specific way.
8403 * "fn" is the function that is being tested.
8404 * "arg1" and "arg2" are string descriptions of the inputs.
8405 * "res" is a string description of the expected result.
8407 static struct {
8408 __isl_give isl_set *(*fn)(__isl_take isl_multi_aff *ma1,
8409 __isl_take isl_multi_aff *ma2);
8410 const char *arg1;
8411 const char *arg2;
8412 const char *res;
8413 } bin_locus_ma_tests[] = {
8414 { &isl_multi_aff_lex_le_set, "{ [] }", "{ [] }", "{ : }" },
8415 { &isl_multi_aff_lex_lt_set, "{ [] }", "{ [] }", "{ : false }" },
8416 { &isl_multi_aff_lex_le_set,
8417 "{ A[i] -> [i] }", "{ A[i] -> [0] }",
8418 "{ A[i] : i <= 0 }" },
8419 { &isl_multi_aff_lex_lt_set,
8420 "{ A[i] -> [i] }", "{ A[i] -> [0] }",
8421 "{ A[i] : i < 0 }" },
8422 { &isl_multi_aff_lex_le_set,
8423 "{ A[i] -> [i, i] }", "{ A[i] -> [0, 0] }",
8424 "{ A[i] : i <= 0 }" },
8425 { &isl_multi_aff_lex_le_set,
8426 "{ A[i] -> [i, 0] }", "{ A[i] -> [0, 0] }",
8427 "{ A[i] : i <= 0 }" },
8428 { &isl_multi_aff_lex_le_set,
8429 "{ A[i] -> [i, 1] }", "{ A[i] -> [0, 0] }",
8430 "{ A[i] : i < 0 }" },
8433 /* Perform some basic tests of functions that determine
8434 * the part of the domain where two isl_multi_aff objects
8435 * related to each other in a specific way.
8437 static isl_stat test_bin_locus_ma(isl_ctx *ctx)
8439 int i;
8441 for (i = 0; i < ARRAY_SIZE(bin_locus_ma_tests); ++i) {
8442 const char *str;
8443 isl_bool ok;
8444 isl_multi_aff *ma1, *ma2;
8445 isl_set *set, *res;
8447 str = bin_locus_ma_tests[i].arg1;
8448 ma1 = isl_multi_aff_read_from_str(ctx, str);
8449 str = bin_locus_ma_tests[i].arg2;
8450 ma2 = isl_multi_aff_read_from_str(ctx, str);
8451 res = isl_set_read_from_str(ctx, bin_locus_ma_tests[i].res);
8452 set = bin_locus_ma_tests[i].fn(ma1, ma2);
8453 ok = isl_set_is_equal(set, res);
8454 isl_set_free(set);
8455 isl_set_free(res);
8456 if (ok < 0)
8457 return isl_stat_error;
8458 if (!ok)
8459 isl_die(ctx, isl_error_unknown,
8460 "unexpected result", return isl_stat_error);
8463 return isl_stat_ok;
8466 /* Perform basic locus tests.
8468 static int test_locus(isl_ctx *ctx)
8470 if (test_un_locus(ctx) < 0)
8471 return -1;
8472 if (test_bin_locus(ctx) < 0)
8473 return -1;
8474 if (test_bin_locus_ma(ctx) < 0)
8475 return -1;
8476 return 0;
8479 /* Test that isl_union_pw_qpolynomial_eval picks up the function
8480 * defined over the correct domain space.
8482 static int test_eval_1(isl_ctx *ctx)
8484 const char *str;
8485 isl_point *pnt;
8486 isl_set *set;
8487 isl_union_pw_qpolynomial *upwqp;
8488 isl_val *v;
8489 int cmp;
8491 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
8492 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
8493 str = "{ A[6] }";
8494 set = isl_set_read_from_str(ctx, str);
8495 pnt = isl_set_sample_point(set);
8496 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
8497 cmp = isl_val_cmp_si(v, 36);
8498 isl_val_free(v);
8500 if (!v)
8501 return -1;
8502 if (cmp != 0)
8503 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
8505 return 0;
8508 /* Check that isl_qpolynomial_eval handles getting called on a void point.
8510 static int test_eval_2(isl_ctx *ctx)
8512 const char *str;
8513 isl_point *pnt;
8514 isl_set *set;
8515 isl_qpolynomial *qp;
8516 isl_val *v;
8517 isl_bool ok;
8519 str = "{ A[x] -> [x] }";
8520 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
8521 str = "{ A[x] : false }";
8522 set = isl_set_read_from_str(ctx, str);
8523 pnt = isl_set_sample_point(set);
8524 v = isl_qpolynomial_eval(qp, pnt);
8525 ok = isl_val_is_nan(v);
8526 isl_val_free(v);
8528 if (ok < 0)
8529 return -1;
8530 if (!ok)
8531 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
8533 return 0;
8536 /* Check that a polynomial (without local variables) can be evaluated
8537 * in a rational point.
8539 static isl_stat test_eval_3(isl_ctx *ctx)
8541 isl_pw_qpolynomial *pwqp;
8542 isl_point *pnt;
8543 isl_val *v;
8544 isl_stat r;
8546 pwqp = isl_pw_qpolynomial_read_from_str(ctx, "{ [x] -> x^2 }");
8547 pnt = isl_point_zero(isl_pw_qpolynomial_get_domain_space(pwqp));
8548 v = isl_val_read_from_str(ctx, "1/2");
8549 pnt = isl_point_set_coordinate_val(pnt, isl_dim_set, 0, v);
8550 v = isl_pw_qpolynomial_eval(pwqp, pnt);
8551 r = val_check_equal(v, "1/4");
8552 isl_val_free(v);
8554 return r;
8557 /* Inputs for isl_pw_aff_eval test.
8558 * "f" is the affine function.
8559 * "p" is the point where the function should be evaluated.
8560 * "res" is the expected result.
8562 struct {
8563 const char *f;
8564 const char *p;
8565 const char *res;
8566 } aff_eval_tests[] = {
8567 { "{ [i] -> [2 * i] }", "{ [4] }", "8" },
8568 { "{ [i] -> [2 * i] }", "{ [x] : false }", "NaN" },
8569 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [0] }", "0" },
8570 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [1] }", "1" },
8571 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [2] }", "3" },
8572 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [3] }", "5" },
8573 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [4] }", "7" },
8574 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [0] }", "0" },
8575 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [1] }", "0" },
8576 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [2] }", "0" },
8577 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [3] }", "0" },
8578 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [4] }", "1" },
8579 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [6] }", "1" },
8580 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [8] }", "2" },
8581 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [4] }", "4" },
8582 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [-2] }", "2" },
8583 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [0] }", "NaN" },
8584 { "[N] -> { [2 * N] }", "[N] -> { : N = 4 }", "8" },
8585 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 1] }", "1" },
8586 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 2] }", "3/2" },
8587 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [4] }", "4" },
8588 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [3] }", "NaN" },
8589 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [x] : false }", "NaN" },
8592 /* Perform basic isl_pw_aff_eval tests.
8594 static int test_eval_aff(isl_ctx *ctx)
8596 int i;
8598 for (i = 0; i < ARRAY_SIZE(aff_eval_tests); ++i) {
8599 isl_stat r;
8600 isl_pw_aff *pa;
8601 isl_set *set;
8602 isl_point *pnt;
8603 isl_val *v;
8605 pa = isl_pw_aff_read_from_str(ctx, aff_eval_tests[i].f);
8606 set = isl_set_read_from_str(ctx, aff_eval_tests[i].p);
8607 pnt = isl_set_sample_point(set);
8608 v = isl_pw_aff_eval(pa, pnt);
8609 r = val_check_equal(v, aff_eval_tests[i].res);
8610 isl_val_free(v);
8611 if (r < 0)
8612 return -1;
8614 return 0;
8617 /* Perform basic evaluation tests.
8619 static int test_eval(isl_ctx *ctx)
8621 if (test_eval_1(ctx) < 0)
8622 return -1;
8623 if (test_eval_2(ctx) < 0)
8624 return -1;
8625 if (test_eval_3(ctx) < 0)
8626 return -1;
8627 if (test_eval_aff(ctx) < 0)
8628 return -1;
8629 return 0;
8632 /* Descriptions of sets that are tested for reparsing after printing.
8634 const char *output_tests[] = {
8635 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
8636 "{ [x] : 1 = 0 }",
8637 "{ [x] : false }",
8638 "{ [x] : x mod 2 = 0 }",
8639 "{ [x] : x mod 2 = 1 }",
8640 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
8641 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
8642 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
8643 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
8644 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
8645 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
8648 /* Check that printing a set and reparsing a set from the printed output
8649 * results in the same set.
8651 static int test_output_set(isl_ctx *ctx)
8653 int i;
8654 char *str;
8655 isl_set *set1, *set2;
8656 isl_bool equal;
8658 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
8659 set1 = isl_set_read_from_str(ctx, output_tests[i]);
8660 str = isl_set_to_str(set1);
8661 set2 = isl_set_read_from_str(ctx, str);
8662 free(str);
8663 equal = isl_set_is_equal(set1, set2);
8664 isl_set_free(set1);
8665 isl_set_free(set2);
8666 if (equal < 0)
8667 return -1;
8668 if (!equal)
8669 isl_die(ctx, isl_error_unknown,
8670 "parsed output not the same", return -1);
8673 return 0;
8676 /* Check that an isl_multi_aff is printed using a consistent space.
8678 static isl_stat test_output_ma(isl_ctx *ctx)
8680 char *str;
8681 isl_bool equal;
8682 isl_aff *aff;
8683 isl_multi_aff *ma, *ma2;
8685 ma = isl_multi_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
8686 aff = isl_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
8687 ma = isl_multi_aff_set_aff(ma, 0, aff);
8688 str = isl_multi_aff_to_str(ma);
8689 ma2 = isl_multi_aff_read_from_str(ctx, str);
8690 free(str);
8691 equal = isl_multi_aff_plain_is_equal(ma, ma2);
8692 isl_multi_aff_free(ma2);
8693 isl_multi_aff_free(ma);
8695 if (equal < 0)
8696 return isl_stat_error;
8697 if (!equal)
8698 isl_die(ctx, isl_error_unknown, "bad conversion",
8699 return isl_stat_error);
8701 return isl_stat_ok;
8704 /* Check that an isl_multi_pw_aff is printed using a consistent space.
8706 static isl_stat test_output_mpa(isl_ctx *ctx)
8708 char *str;
8709 isl_bool equal;
8710 isl_pw_aff *pa;
8711 isl_multi_pw_aff *mpa, *mpa2;
8713 mpa = isl_multi_pw_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
8714 pa = isl_pw_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
8715 mpa = isl_multi_pw_aff_set_pw_aff(mpa, 0, pa);
8716 str = isl_multi_pw_aff_to_str(mpa);
8717 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
8718 free(str);
8719 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
8720 isl_multi_pw_aff_free(mpa2);
8721 isl_multi_pw_aff_free(mpa);
8723 if (equal < 0)
8724 return isl_stat_error;
8725 if (!equal)
8726 isl_die(ctx, isl_error_unknown, "bad conversion",
8727 return isl_stat_error);
8729 return isl_stat_ok;
8732 int test_output(isl_ctx *ctx)
8734 char *s;
8735 const char *str;
8736 isl_pw_aff *pa;
8737 isl_printer *p;
8738 int equal;
8740 if (test_output_set(ctx) < 0)
8741 return -1;
8742 if (test_output_ma(ctx) < 0)
8743 return -1;
8744 if (test_output_mpa(ctx) < 0)
8745 return -1;
8747 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
8748 pa = isl_pw_aff_read_from_str(ctx, str);
8750 p = isl_printer_to_str(ctx);
8751 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
8752 p = isl_printer_print_pw_aff(p, pa);
8753 s = isl_printer_get_str(p);
8754 isl_printer_free(p);
8755 isl_pw_aff_free(pa);
8756 if (!s)
8757 equal = -1;
8758 else
8759 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
8760 free(s);
8761 if (equal < 0)
8762 return -1;
8763 if (!equal)
8764 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
8766 return 0;
8769 int test_sample(isl_ctx *ctx)
8771 const char *str;
8772 isl_basic_set *bset1, *bset2;
8773 int empty, subset;
8775 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
8776 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
8777 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
8778 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
8779 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
8780 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
8781 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
8782 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
8783 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
8784 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
8785 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
8786 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
8787 "d - 1073741821e and "
8788 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
8789 "3j >= 1 - a + b + 2e and "
8790 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
8791 "3i <= 4 - a + 4b - e and "
8792 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
8793 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
8794 "c <= -1 + a and 3i >= -2 - a + 3e and "
8795 "1073741822e <= 1073741823 - a + 1073741822b + c and "
8796 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
8797 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
8798 "1073741823e >= 1 + 1073741823b - d and "
8799 "3i >= 1073741823b + c - 1073741823e - f and "
8800 "3i >= 1 + 2b + e + 3g }";
8801 bset1 = isl_basic_set_read_from_str(ctx, str);
8802 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
8803 empty = isl_basic_set_is_empty(bset2);
8804 subset = isl_basic_set_is_subset(bset2, bset1);
8805 isl_basic_set_free(bset1);
8806 isl_basic_set_free(bset2);
8807 if (empty < 0 || subset < 0)
8808 return -1;
8809 if (empty)
8810 isl_die(ctx, isl_error_unknown, "point not found", return -1);
8811 if (!subset)
8812 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
8814 return 0;
8817 /* Perform a projection on a basic set that is known to be empty
8818 * but that has not been assigned a canonical representation.
8819 * Earlier versions of isl would run into a stack overflow
8820 * on this example.
8822 static int test_empty_projection(isl_ctx *ctx)
8824 const char *str;
8825 isl_bool empty;
8826 isl_basic_set *bset;
8828 str = "{ [a, b, c, d, e, f, g, h] : 5f = 1 + 4a - b + 5c - d - 2e and "
8829 "3h = 2 + b + c and 14c >= 9 - 3a + 25b and "
8830 "4c <= 50 - 3a + 23b and 6b <= -39 + a and "
8831 "9g >= -6 + 3a + b + c and e < a + b - 2d and "
8832 "7d >= -5 + 2a + 2b and 5g >= -14 + a - 4b + d + 2e and "
8833 "9g <= -28 - 5b - 2c + 3d + 6e }";
8834 bset = isl_basic_set_read_from_str(ctx, str);
8835 empty = isl_basic_set_is_empty(bset);
8836 bset = isl_basic_set_params(bset);
8837 isl_basic_set_free(bset);
8839 if (empty < 0)
8840 return -1;
8842 return 0;
8845 int test_fixed_power(isl_ctx *ctx)
8847 const char *str;
8848 isl_map *map;
8849 isl_val *exp;
8850 int equal;
8852 str = "{ [i] -> [i + 1] }";
8853 map = isl_map_read_from_str(ctx, str);
8854 exp = isl_val_int_from_si(ctx, 23);
8855 map = isl_map_fixed_power_val(map, exp);
8856 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
8857 isl_map_free(map);
8858 if (equal < 0)
8859 return -1;
8861 return 0;
8864 int test_slice(isl_ctx *ctx)
8866 const char *str;
8867 isl_map *map;
8868 int equal;
8870 str = "{ [i] -> [j] }";
8871 map = isl_map_read_from_str(ctx, str);
8872 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
8873 equal = map_check_equal(map, "{ [i] -> [i] }");
8874 isl_map_free(map);
8875 if (equal < 0)
8876 return -1;
8878 str = "{ [i] -> [j] }";
8879 map = isl_map_read_from_str(ctx, str);
8880 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
8881 equal = map_check_equal(map, "{ [i] -> [j] }");
8882 isl_map_free(map);
8883 if (equal < 0)
8884 return -1;
8886 str = "{ [i] -> [j] }";
8887 map = isl_map_read_from_str(ctx, str);
8888 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
8889 equal = map_check_equal(map, "{ [i] -> [-i] }");
8890 isl_map_free(map);
8891 if (equal < 0)
8892 return -1;
8894 str = "{ [i] -> [j] }";
8895 map = isl_map_read_from_str(ctx, str);
8896 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
8897 equal = map_check_equal(map, "{ [0] -> [j] }");
8898 isl_map_free(map);
8899 if (equal < 0)
8900 return -1;
8902 str = "{ [i] -> [j] }";
8903 map = isl_map_read_from_str(ctx, str);
8904 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
8905 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
8906 isl_map_free(map);
8907 if (equal < 0)
8908 return -1;
8910 str = "{ [i] -> [j] }";
8911 map = isl_map_read_from_str(ctx, str);
8912 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
8913 equal = map_check_equal(map, "{ [i] -> [j] : false }");
8914 isl_map_free(map);
8915 if (equal < 0)
8916 return -1;
8918 return 0;
8921 int test_eliminate(isl_ctx *ctx)
8923 const char *str;
8924 isl_map *map;
8925 int equal;
8927 str = "{ [i] -> [j] : i = 2j }";
8928 map = isl_map_read_from_str(ctx, str);
8929 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
8930 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
8931 isl_map_free(map);
8932 if (equal < 0)
8933 return -1;
8935 return 0;
8938 /* Check basic functionality of isl_map_deltas_map.
8940 static int test_deltas_map(isl_ctx *ctx)
8942 const char *str;
8943 isl_map *map;
8944 int equal;
8946 str = "{ A[i] -> A[i + 1] }";
8947 map = isl_map_read_from_str(ctx, str);
8948 map = isl_map_deltas_map(map);
8949 equal = map_check_equal(map, "{ [A[i] -> A[i + 1]] -> A[1] }");
8950 isl_map_free(map);
8951 if (equal < 0)
8952 return -1;
8954 return 0;
8957 /* Check that isl_set_dim_residue_class detects that the values of j
8958 * in the set below are all odd and that it does not detect any spurious
8959 * strides.
8961 static int test_residue_class(isl_ctx *ctx)
8963 const char *str;
8964 isl_set *set;
8965 isl_int m, r;
8966 isl_stat res;
8968 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
8969 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
8970 set = isl_set_read_from_str(ctx, str);
8971 isl_int_init(m);
8972 isl_int_init(r);
8973 res = isl_set_dim_residue_class(set, 1, &m, &r);
8974 if (res >= 0 &&
8975 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
8976 isl_die(ctx, isl_error_unknown, "incorrect residue class",
8977 res = isl_stat_error);
8978 isl_int_clear(r);
8979 isl_int_clear(m);
8980 isl_set_free(set);
8982 return res;
8985 static int test_align_parameters_1(isl_ctx *ctx)
8987 const char *str;
8988 isl_space *space;
8989 isl_multi_aff *ma1, *ma2;
8990 int equal;
8992 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
8993 ma1 = isl_multi_aff_read_from_str(ctx, str);
8995 space = isl_space_params_alloc(ctx, 1);
8996 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
8997 ma1 = isl_multi_aff_align_params(ma1, space);
8999 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
9000 ma2 = isl_multi_aff_read_from_str(ctx, str);
9002 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
9004 isl_multi_aff_free(ma1);
9005 isl_multi_aff_free(ma2);
9007 if (equal < 0)
9008 return -1;
9009 if (!equal)
9010 isl_die(ctx, isl_error_unknown,
9011 "result not as expected", return -1);
9013 return 0;
9016 /* Check the isl_multi_*_from_*_list operation in case inputs
9017 * have unaligned parameters.
9018 * In particular, older versions of isl would simply fail
9019 * (without printing any error message).
9021 static isl_stat test_align_parameters_2(isl_ctx *ctx)
9023 isl_space *space;
9024 isl_map *map;
9025 isl_aff *aff;
9026 isl_multi_aff *ma;
9028 map = isl_map_read_from_str(ctx, "{ A[] -> M[x] }");
9029 space = isl_map_get_space(map);
9030 isl_map_free(map);
9032 aff = isl_aff_read_from_str(ctx, "[N] -> { A[] -> [N] }");
9033 ma = isl_multi_aff_from_aff_list(space, isl_aff_list_from_aff(aff));
9034 isl_multi_aff_free(ma);
9036 if (!ma)
9037 return isl_stat_error;
9038 return isl_stat_ok;
9041 /* Perform basic parameter alignment tests.
9043 static int test_align_parameters(isl_ctx *ctx)
9045 if (test_align_parameters_1(ctx) < 0)
9046 return -1;
9047 if (test_align_parameters_2(ctx) < 0)
9048 return -1;
9050 return 0;
9053 /* Check that isl_*_drop_unused_params actually drops the unused parameters
9054 * by comparing the result using isl_*_plain_is_equal.
9055 * Note that this assumes that isl_*_plain_is_equal does not consider
9056 * objects that only differ by unused parameters to be equal.
9058 int test_drop_unused_parameters(isl_ctx *ctx)
9060 const char *str_with, *str_without;
9061 isl_basic_set *bset1, *bset2;
9062 isl_set *set1, *set2;
9063 isl_pw_aff *pwa1, *pwa2;
9064 int equal;
9066 str_with = "[n, m, o] -> { [m] }";
9067 str_without = "[m] -> { [m] }";
9069 bset1 = isl_basic_set_read_from_str(ctx, str_with);
9070 bset2 = isl_basic_set_read_from_str(ctx, str_without);
9071 bset1 = isl_basic_set_drop_unused_params(bset1);
9072 equal = isl_basic_set_plain_is_equal(bset1, bset2);
9073 isl_basic_set_free(bset1);
9074 isl_basic_set_free(bset2);
9076 if (equal < 0)
9077 return -1;
9078 if (!equal)
9079 isl_die(ctx, isl_error_unknown,
9080 "result not as expected", return -1);
9082 set1 = isl_set_read_from_str(ctx, str_with);
9083 set2 = isl_set_read_from_str(ctx, str_without);
9084 set1 = isl_set_drop_unused_params(set1);
9085 equal = isl_set_plain_is_equal(set1, set2);
9086 isl_set_free(set1);
9087 isl_set_free(set2);
9089 if (equal < 0)
9090 return -1;
9091 if (!equal)
9092 isl_die(ctx, isl_error_unknown,
9093 "result not as expected", return -1);
9095 pwa1 = isl_pw_aff_read_from_str(ctx, str_with);
9096 pwa2 = isl_pw_aff_read_from_str(ctx, str_without);
9097 pwa1 = isl_pw_aff_drop_unused_params(pwa1);
9098 equal = isl_pw_aff_plain_is_equal(pwa1, pwa2);
9099 isl_pw_aff_free(pwa1);
9100 isl_pw_aff_free(pwa2);
9102 if (equal < 0)
9103 return -1;
9104 if (!equal)
9105 isl_die(ctx, isl_error_unknown,
9106 "result not as expected", return -1);
9108 return 0;
9111 static int test_list(isl_ctx *ctx)
9113 isl_id *a, *b, *c, *d, *id;
9114 isl_id_list *list;
9115 isl_size n;
9116 int ok;
9118 a = isl_id_alloc(ctx, "a", NULL);
9119 b = isl_id_alloc(ctx, "b", NULL);
9120 c = isl_id_alloc(ctx, "c", NULL);
9121 d = isl_id_alloc(ctx, "d", NULL);
9123 list = isl_id_list_alloc(ctx, 4);
9124 list = isl_id_list_add(list, b);
9125 list = isl_id_list_insert(list, 0, a);
9126 list = isl_id_list_add(list, c);
9127 list = isl_id_list_add(list, d);
9128 list = isl_id_list_drop(list, 1, 1);
9130 n = isl_id_list_n_id(list);
9131 if (n < 0)
9132 return -1;
9133 if (n != 3) {
9134 isl_id_list_free(list);
9135 isl_die(ctx, isl_error_unknown,
9136 "unexpected number of elements in list", return -1);
9139 id = isl_id_list_get_id(list, 0);
9140 ok = id == a;
9141 isl_id_free(id);
9142 id = isl_id_list_get_id(list, 1);
9143 ok = ok && id == c;
9144 isl_id_free(id);
9145 id = isl_id_list_get_id(list, 2);
9146 ok = ok && id == d;
9147 isl_id_free(id);
9149 isl_id_list_free(list);
9151 if (!ok)
9152 isl_die(ctx, isl_error_unknown,
9153 "unexpected elements in list", return -1);
9155 return 0;
9158 /* Check the conversion from an isl_multi_aff to an isl_basic_set.
9160 static isl_stat test_ma_conversion(isl_ctx *ctx)
9162 const char *str;
9163 isl_bool equal;
9164 isl_multi_aff *ma;
9165 isl_basic_set *bset1, *bset2;
9167 str = "[N] -> { A[0, N + 1] }";
9168 ma = isl_multi_aff_read_from_str(ctx, str);
9169 bset1 = isl_basic_set_read_from_str(ctx, str);
9170 bset2 = isl_basic_set_from_multi_aff(ma);
9171 equal = isl_basic_set_is_equal(bset1, bset2);
9172 isl_basic_set_free(bset1);
9173 isl_basic_set_free(bset2);
9174 if (equal < 0)
9175 return isl_stat_error;
9176 if (!equal)
9177 isl_die(ctx, isl_error_unknown, "bad conversion",
9178 return isl_stat_error);
9179 return isl_stat_ok;
9182 const char *set_conversion_tests[] = {
9183 "[N] -> { [i] : N - 1 <= 2 i <= N }",
9184 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
9185 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
9186 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
9187 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
9188 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
9189 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
9190 "-3 + c <= d <= 28 + c) }",
9193 /* Check that converting from isl_set to isl_pw_multi_aff and back
9194 * to isl_set produces the original isl_set.
9196 static int test_set_conversion(isl_ctx *ctx)
9198 int i;
9199 const char *str;
9200 isl_set *set1, *set2;
9201 isl_pw_multi_aff *pma;
9202 int equal;
9204 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
9205 str = set_conversion_tests[i];
9206 set1 = isl_set_read_from_str(ctx, str);
9207 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
9208 set2 = isl_set_from_pw_multi_aff(pma);
9209 equal = isl_set_is_equal(set1, set2);
9210 isl_set_free(set1);
9211 isl_set_free(set2);
9213 if (equal < 0)
9214 return -1;
9215 if (!equal)
9216 isl_die(ctx, isl_error_unknown, "bad conversion",
9217 return -1);
9220 return 0;
9223 const char *conversion_tests[] = {
9224 "{ [a, b, c, d] -> s0[a, b, e, f] : "
9225 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
9226 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
9227 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
9228 "9e <= -2 - 2a) }",
9229 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
9230 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
9231 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
9234 /* Check that converting from isl_map to isl_pw_multi_aff and back
9235 * to isl_map produces the original isl_map.
9237 static int test_map_conversion(isl_ctx *ctx)
9239 int i;
9240 isl_map *map1, *map2;
9241 isl_pw_multi_aff *pma;
9242 int equal;
9244 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
9245 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
9246 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
9247 map2 = isl_map_from_pw_multi_aff(pma);
9248 equal = isl_map_is_equal(map1, map2);
9249 isl_map_free(map1);
9250 isl_map_free(map2);
9252 if (equal < 0)
9253 return -1;
9254 if (!equal)
9255 isl_die(ctx, isl_error_unknown, "bad conversion",
9256 return -1);
9259 return 0;
9262 /* Descriptions of isl_pw_multi_aff objects for testing conversion
9263 * to isl_multi_pw_aff and back.
9265 const char *mpa_conversion_tests[] = {
9266 "{ [x] -> A[x] }",
9267 "{ [x] -> A[x] : x >= 0 }",
9268 "{ [x] -> A[x] : x >= 0; [x] -> A[-x] : x < 0 }",
9269 "{ [x] -> A[x, x + 1] }",
9270 "{ [x] -> A[] }",
9271 "{ [x] -> A[] : x >= 0 }",
9274 /* Check that conversion from isl_pw_multi_aff to isl_multi_pw_aff and
9275 * back to isl_pw_multi_aff preserves the original meaning.
9277 static int test_mpa_conversion(isl_ctx *ctx)
9279 int i;
9280 isl_pw_multi_aff *pma1, *pma2;
9281 isl_multi_pw_aff *mpa;
9282 int equal;
9284 for (i = 0; i < ARRAY_SIZE(mpa_conversion_tests); ++i) {
9285 const char *str;
9286 str = mpa_conversion_tests[i];
9287 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
9288 pma2 = isl_pw_multi_aff_copy(pma1);
9289 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma1);
9290 pma1 = isl_pw_multi_aff_from_multi_pw_aff(mpa);
9291 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
9292 isl_pw_multi_aff_free(pma1);
9293 isl_pw_multi_aff_free(pma2);
9295 if (equal < 0)
9296 return -1;
9297 if (!equal)
9298 isl_die(ctx, isl_error_unknown, "bad conversion",
9299 return -1);
9302 return 0;
9305 /* Descriptions of union maps that should be convertible
9306 * to an isl_multi_union_pw_aff.
9308 const char *umap_mupa_conversion_tests[] = {
9309 "{ [a, b, c, d] -> s0[a, b, e, f] : "
9310 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
9311 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
9312 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
9313 "9e <= -2 - 2a) }",
9314 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
9315 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
9316 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
9317 "{ A[] -> B[0]; C[] -> B[1] }",
9318 "{ A[] -> B[]; C[] -> B[] }",
9321 /* Check that converting from isl_union_map to isl_multi_union_pw_aff and back
9322 * to isl_union_map produces the original isl_union_map.
9324 static int test_union_map_mupa_conversion(isl_ctx *ctx)
9326 int i;
9327 isl_union_map *umap1, *umap2;
9328 isl_multi_union_pw_aff *mupa;
9329 int equal;
9331 for (i = 0; i < ARRAY_SIZE(umap_mupa_conversion_tests); ++i) {
9332 const char *str;
9333 str = umap_mupa_conversion_tests[i];
9334 umap1 = isl_union_map_read_from_str(ctx, str);
9335 umap2 = isl_union_map_copy(umap1);
9336 mupa = isl_multi_union_pw_aff_from_union_map(umap2);
9337 umap2 = isl_union_map_from_multi_union_pw_aff(mupa);
9338 equal = isl_union_map_is_equal(umap1, umap2);
9339 isl_union_map_free(umap1);
9340 isl_union_map_free(umap2);
9342 if (equal < 0)
9343 return -1;
9344 if (!equal)
9345 isl_die(ctx, isl_error_unknown, "bad conversion",
9346 return -1);
9349 return 0;
9352 static int test_conversion(isl_ctx *ctx)
9354 if (test_ma_conversion(ctx) < 0)
9355 return -1;
9356 if (test_set_conversion(ctx) < 0)
9357 return -1;
9358 if (test_map_conversion(ctx) < 0)
9359 return -1;
9360 if (test_mpa_conversion(ctx) < 0)
9361 return -1;
9362 if (test_union_map_mupa_conversion(ctx) < 0)
9363 return -1;
9364 return 0;
9367 /* Check that isl_basic_map_curry does not modify input.
9369 static int test_curry(isl_ctx *ctx)
9371 const char *str;
9372 isl_basic_map *bmap1, *bmap2;
9373 int equal;
9375 str = "{ [A[] -> B[]] -> C[] }";
9376 bmap1 = isl_basic_map_read_from_str(ctx, str);
9377 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
9378 equal = isl_basic_map_is_equal(bmap1, bmap2);
9379 isl_basic_map_free(bmap1);
9380 isl_basic_map_free(bmap2);
9382 if (equal < 0)
9383 return -1;
9384 if (equal)
9385 isl_die(ctx, isl_error_unknown,
9386 "curried map should not be equal to original",
9387 return -1);
9389 return 0;
9392 struct {
9393 const char *set;
9394 const char *ma;
9395 const char *res;
9396 } preimage_tests[] = {
9397 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
9398 "{ A[j,i] -> B[i,j] }",
9399 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
9400 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
9401 "{ A[a,b] -> B[a/2,b/6] }",
9402 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
9403 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
9404 "{ A[a,b] -> B[a/2,b/6] }",
9405 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
9406 "exists i,j : a = 2 i and b = 6 j }" },
9407 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
9408 "[n] -> { : 0 <= n <= 100 }" },
9409 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
9410 "{ A[a] -> B[2a] }",
9411 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
9412 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
9413 "{ A[a] -> B[([a/2])] }",
9414 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
9415 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
9416 "{ A[a] -> B[a,a,a/3] }",
9417 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
9418 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
9419 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
9422 static int test_preimage_basic_set(isl_ctx *ctx)
9424 int i;
9425 isl_basic_set *bset1, *bset2;
9426 isl_multi_aff *ma;
9427 int equal;
9429 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
9430 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
9431 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
9432 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
9433 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
9434 equal = isl_basic_set_is_equal(bset1, bset2);
9435 isl_basic_set_free(bset1);
9436 isl_basic_set_free(bset2);
9437 if (equal < 0)
9438 return -1;
9439 if (!equal)
9440 isl_die(ctx, isl_error_unknown, "bad preimage",
9441 return -1);
9444 return 0;
9447 struct {
9448 const char *map;
9449 const char *ma;
9450 const char *res;
9451 } preimage_domain_tests[] = {
9452 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
9453 "{ A[j,i] -> B[i,j] }",
9454 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
9455 { "{ B[i] -> C[i]; D[i] -> E[i] }",
9456 "{ A[i] -> B[i + 1] }",
9457 "{ A[i] -> C[i + 1] }" },
9458 { "{ B[i] -> C[i]; B[i] -> E[i] }",
9459 "{ A[i] -> B[i + 1] }",
9460 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
9461 { "{ B[i] -> C[([i/2])] }",
9462 "{ A[i] -> B[2i] }",
9463 "{ A[i] -> C[i] }" },
9464 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
9465 "{ A[i] -> B[([i/5]), ([i/7])] }",
9466 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
9467 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
9468 "[N] -> { A[] -> B[([N/5])] }",
9469 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
9470 { "{ B[i] -> C[i] : exists a : i = 5 a }",
9471 "{ A[i] -> B[2i] }",
9472 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
9473 { "{ B[i] -> C[i] : exists a : i = 2 a; "
9474 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
9475 "{ A[i] -> B[2i] }",
9476 "{ A[i] -> C[2i] }" },
9477 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
9478 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
9481 static int test_preimage_union_map(isl_ctx *ctx)
9483 int i;
9484 isl_union_map *umap1, *umap2;
9485 isl_multi_aff *ma;
9486 int equal;
9488 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
9489 umap1 = isl_union_map_read_from_str(ctx,
9490 preimage_domain_tests[i].map);
9491 ma = isl_multi_aff_read_from_str(ctx,
9492 preimage_domain_tests[i].ma);
9493 umap2 = isl_union_map_read_from_str(ctx,
9494 preimage_domain_tests[i].res);
9495 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
9496 equal = isl_union_map_is_equal(umap1, umap2);
9497 isl_union_map_free(umap1);
9498 isl_union_map_free(umap2);
9499 if (equal < 0)
9500 return -1;
9501 if (!equal)
9502 isl_die(ctx, isl_error_unknown, "bad preimage",
9503 return -1);
9506 return 0;
9509 static int test_preimage(isl_ctx *ctx)
9511 if (test_preimage_basic_set(ctx) < 0)
9512 return -1;
9513 if (test_preimage_union_map(ctx) < 0)
9514 return -1;
9516 return 0;
9519 struct {
9520 const char *ma1;
9521 const char *ma;
9522 const char *res;
9523 } pullback_tests[] = {
9524 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
9525 "{ A[a,b] -> C[b + 2a] }" },
9526 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
9527 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
9528 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
9529 "{ A[a] -> C[(a)/6] }" },
9530 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
9531 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
9532 "{ A[a] -> C[(2a)/3] }" },
9533 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
9534 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
9535 "{ A[i,j] -> C[i + j, i + j] }"},
9536 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
9537 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
9538 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
9539 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
9540 "{ [i, j] -> [floor((i)/3), j] }",
9541 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
9544 static int test_pullback(isl_ctx *ctx)
9546 int i;
9547 isl_multi_aff *ma1, *ma2;
9548 isl_multi_aff *ma;
9549 int equal;
9551 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
9552 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
9553 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
9554 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
9555 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
9556 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
9557 isl_multi_aff_free(ma1);
9558 isl_multi_aff_free(ma2);
9559 if (equal < 0)
9560 return -1;
9561 if (!equal)
9562 isl_die(ctx, isl_error_unknown, "bad pullback",
9563 return -1);
9566 return 0;
9569 /* Check that negation is printed correctly and that equal expressions
9570 * are correctly identified.
9572 static int test_ast(isl_ctx *ctx)
9574 isl_ast_expr *expr, *expr1, *expr2, *expr3;
9575 char *str;
9576 int ok, equal;
9578 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
9579 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
9580 expr = isl_ast_expr_add(expr1, expr2);
9581 expr2 = isl_ast_expr_copy(expr);
9582 expr = isl_ast_expr_neg(expr);
9583 expr2 = isl_ast_expr_neg(expr2);
9584 equal = isl_ast_expr_is_equal(expr, expr2);
9585 str = isl_ast_expr_to_C_str(expr);
9586 ok = str ? !strcmp(str, "-(A + B)") : -1;
9587 free(str);
9588 isl_ast_expr_free(expr);
9589 isl_ast_expr_free(expr2);
9591 if (ok < 0 || equal < 0)
9592 return -1;
9593 if (!equal)
9594 isl_die(ctx, isl_error_unknown,
9595 "equal expressions not considered equal", return -1);
9596 if (!ok)
9597 isl_die(ctx, isl_error_unknown,
9598 "isl_ast_expr printed incorrectly", return -1);
9600 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
9601 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
9602 expr = isl_ast_expr_add(expr1, expr2);
9603 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
9604 expr = isl_ast_expr_sub(expr3, expr);
9605 str = isl_ast_expr_to_C_str(expr);
9606 ok = str ? !strcmp(str, "C - (A + B)") : -1;
9607 free(str);
9608 isl_ast_expr_free(expr);
9610 if (ok < 0)
9611 return -1;
9612 if (!ok)
9613 isl_die(ctx, isl_error_unknown,
9614 "isl_ast_expr printed incorrectly", return -1);
9616 return 0;
9619 /* Check that isl_ast_build_expr_from_set returns a valid expression
9620 * for an empty set. Note that isl_ast_build_expr_from_set getting
9621 * called on an empty set probably indicates a bug in the caller.
9623 static int test_ast_build(isl_ctx *ctx)
9625 isl_set *set;
9626 isl_ast_build *build;
9627 isl_ast_expr *expr;
9629 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9630 build = isl_ast_build_from_context(set);
9632 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
9633 expr = isl_ast_build_expr_from_set(build, set);
9635 isl_ast_expr_free(expr);
9636 isl_ast_build_free(build);
9638 if (!expr)
9639 return -1;
9641 return 0;
9644 /* Internal data structure for before_for and after_for callbacks.
9646 * depth is the current depth
9647 * before is the number of times before_for has been called
9648 * after is the number of times after_for has been called
9650 struct isl_test_codegen_data {
9651 int depth;
9652 int before;
9653 int after;
9656 /* This function is called before each for loop in the AST generated
9657 * from test_ast_gen1.
9659 * Increment the number of calls and the depth.
9660 * Check that the space returned by isl_ast_build_get_schedule_space
9661 * matches the target space of the schedule returned by
9662 * isl_ast_build_get_schedule.
9663 * Return an isl_id that is checked by the corresponding call
9664 * to after_for.
9666 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
9667 void *user)
9669 struct isl_test_codegen_data *data = user;
9670 isl_ctx *ctx;
9671 isl_space *space;
9672 isl_union_map *schedule;
9673 isl_union_set *uset;
9674 isl_set *set;
9675 isl_bool empty;
9676 isl_size n;
9677 char name[] = "d0";
9679 ctx = isl_ast_build_get_ctx(build);
9681 if (data->before >= 3)
9682 isl_die(ctx, isl_error_unknown,
9683 "unexpected number of for nodes", return NULL);
9684 if (data->depth < 0 || data->depth >= 2)
9685 isl_die(ctx, isl_error_unknown,
9686 "unexpected depth", return NULL);
9688 snprintf(name, sizeof(name), "d%d", data->depth);
9689 data->before++;
9690 data->depth++;
9692 schedule = isl_ast_build_get_schedule(build);
9693 uset = isl_union_map_range(schedule);
9694 n = isl_union_set_n_set(uset);
9695 if (n != 1) {
9696 isl_union_set_free(uset);
9697 if (n < 0)
9698 return NULL;
9699 isl_die(ctx, isl_error_unknown,
9700 "expecting single range space", return NULL);
9703 space = isl_ast_build_get_schedule_space(build);
9704 set = isl_union_set_extract_set(uset, space);
9705 isl_union_set_free(uset);
9706 empty = isl_set_is_empty(set);
9707 isl_set_free(set);
9709 if (empty < 0)
9710 return NULL;
9711 if (empty)
9712 isl_die(ctx, isl_error_unknown,
9713 "spaces don't match", return NULL);
9715 return isl_id_alloc(ctx, name, NULL);
9718 /* This function is called after each for loop in the AST generated
9719 * from test_ast_gen1.
9721 * Increment the number of calls and decrement the depth.
9722 * Check that the annotation attached to the node matches
9723 * the isl_id returned by the corresponding call to before_for.
9725 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
9726 __isl_keep isl_ast_build *build, void *user)
9728 struct isl_test_codegen_data *data = user;
9729 isl_id *id;
9730 const char *name;
9731 int valid;
9733 data->after++;
9734 data->depth--;
9736 if (data->after > data->before)
9737 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
9738 "mismatch in number of for nodes",
9739 return isl_ast_node_free(node));
9741 id = isl_ast_node_get_annotation(node);
9742 if (!id)
9743 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
9744 "missing annotation", return isl_ast_node_free(node));
9746 name = isl_id_get_name(id);
9747 valid = name && atoi(name + 1) == data->depth;
9748 isl_id_free(id);
9750 if (!valid)
9751 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
9752 "wrong annotation", return isl_ast_node_free(node));
9754 return node;
9757 /* Check that the before_each_for and after_each_for callbacks
9758 * are called for each for loop in the generated code,
9759 * that they are called in the right order and that the isl_id
9760 * returned from the before_each_for callback is attached to
9761 * the isl_ast_node passed to the corresponding after_each_for call.
9763 static int test_ast_gen1(isl_ctx *ctx)
9765 const char *str;
9766 isl_set *set;
9767 isl_union_map *schedule;
9768 isl_ast_build *build;
9769 isl_ast_node *tree;
9770 struct isl_test_codegen_data data;
9772 str = "[N] -> { : N >= 10 }";
9773 set = isl_set_read_from_str(ctx, str);
9774 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
9775 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
9776 schedule = isl_union_map_read_from_str(ctx, str);
9778 data.before = 0;
9779 data.after = 0;
9780 data.depth = 0;
9781 build = isl_ast_build_from_context(set);
9782 build = isl_ast_build_set_before_each_for(build,
9783 &before_for, &data);
9784 build = isl_ast_build_set_after_each_for(build,
9785 &after_for, &data);
9786 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9787 isl_ast_build_free(build);
9788 if (!tree)
9789 return -1;
9791 isl_ast_node_free(tree);
9793 if (data.before != 3 || data.after != 3)
9794 isl_die(ctx, isl_error_unknown,
9795 "unexpected number of for nodes", return -1);
9797 return 0;
9800 /* Check that the AST generator handles domains that are integrally disjoint
9801 * but not rationally disjoint.
9803 static int test_ast_gen2(isl_ctx *ctx)
9805 const char *str;
9806 isl_set *set;
9807 isl_union_map *schedule;
9808 isl_union_map *options;
9809 isl_ast_build *build;
9810 isl_ast_node *tree;
9812 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
9813 schedule = isl_union_map_read_from_str(ctx, str);
9814 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9815 build = isl_ast_build_from_context(set);
9817 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
9818 options = isl_union_map_read_from_str(ctx, str);
9819 build = isl_ast_build_set_options(build, options);
9820 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9821 isl_ast_build_free(build);
9822 if (!tree)
9823 return -1;
9824 isl_ast_node_free(tree);
9826 return 0;
9829 /* Increment *user on each call.
9831 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
9832 __isl_keep isl_ast_build *build, void *user)
9834 int *n = user;
9836 (*n)++;
9838 return node;
9841 /* Test that unrolling tries to minimize the number of instances.
9842 * In particular, for the schedule given below, make sure it generates
9843 * 3 nodes (rather than 101).
9845 static int test_ast_gen3(isl_ctx *ctx)
9847 const char *str;
9848 isl_set *set;
9849 isl_union_map *schedule;
9850 isl_union_map *options;
9851 isl_ast_build *build;
9852 isl_ast_node *tree;
9853 int n_domain = 0;
9855 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
9856 schedule = isl_union_map_read_from_str(ctx, str);
9857 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9859 str = "{ [i] -> unroll[0] }";
9860 options = isl_union_map_read_from_str(ctx, str);
9862 build = isl_ast_build_from_context(set);
9863 build = isl_ast_build_set_options(build, options);
9864 build = isl_ast_build_set_at_each_domain(build,
9865 &count_domains, &n_domain);
9866 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9867 isl_ast_build_free(build);
9868 if (!tree)
9869 return -1;
9871 isl_ast_node_free(tree);
9873 if (n_domain != 3)
9874 isl_die(ctx, isl_error_unknown,
9875 "unexpected number of for nodes", return -1);
9877 return 0;
9880 /* Check that if the ast_build_exploit_nested_bounds options is set,
9881 * we do not get an outer if node in the generated AST,
9882 * while we do get such an outer if node if the options is not set.
9884 static int test_ast_gen4(isl_ctx *ctx)
9886 const char *str;
9887 isl_set *set;
9888 isl_union_map *schedule;
9889 isl_ast_build *build;
9890 isl_ast_node *tree;
9891 enum isl_ast_node_type type;
9892 int enb;
9894 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
9895 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
9897 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
9899 schedule = isl_union_map_read_from_str(ctx, str);
9900 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9901 build = isl_ast_build_from_context(set);
9902 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9903 isl_ast_build_free(build);
9904 if (!tree)
9905 return -1;
9907 type = isl_ast_node_get_type(tree);
9908 isl_ast_node_free(tree);
9910 if (type == isl_ast_node_if)
9911 isl_die(ctx, isl_error_unknown,
9912 "not expecting if node", return -1);
9914 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
9916 schedule = isl_union_map_read_from_str(ctx, str);
9917 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9918 build = isl_ast_build_from_context(set);
9919 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9920 isl_ast_build_free(build);
9921 if (!tree)
9922 return -1;
9924 type = isl_ast_node_get_type(tree);
9925 isl_ast_node_free(tree);
9927 if (type != isl_ast_node_if)
9928 isl_die(ctx, isl_error_unknown,
9929 "expecting if node", return -1);
9931 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
9933 return 0;
9936 /* This function is called for each leaf in the AST generated
9937 * from test_ast_gen5.
9939 * We finalize the AST generation by extending the outer schedule
9940 * with a zero-dimensional schedule. If this results in any for loops,
9941 * then this means that we did not pass along enough information
9942 * about the outer schedule to the inner AST generation.
9944 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
9945 void *user)
9947 isl_union_map *schedule, *extra;
9948 isl_ast_node *tree;
9950 schedule = isl_ast_build_get_schedule(build);
9951 extra = isl_union_map_copy(schedule);
9952 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
9953 schedule = isl_union_map_range_product(schedule, extra);
9954 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9955 isl_ast_build_free(build);
9957 if (!tree)
9958 return NULL;
9960 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
9961 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
9962 "code should not contain any for loop",
9963 return isl_ast_node_free(tree));
9965 return tree;
9968 /* Check that we do not lose any information when going back and
9969 * forth between internal and external schedule.
9971 * In particular, we create an AST where we unroll the only
9972 * non-constant dimension in the schedule. We therefore do
9973 * not expect any for loops in the AST. However, older versions
9974 * of isl would not pass along enough information about the outer
9975 * schedule when performing an inner code generation from a create_leaf
9976 * callback, resulting in the inner code generation producing a for loop.
9978 static int test_ast_gen5(isl_ctx *ctx)
9980 const char *str;
9981 isl_set *set;
9982 isl_union_map *schedule, *options;
9983 isl_ast_build *build;
9984 isl_ast_node *tree;
9986 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
9987 schedule = isl_union_map_read_from_str(ctx, str);
9989 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
9990 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
9991 options = isl_union_map_read_from_str(ctx, str);
9993 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9994 build = isl_ast_build_from_context(set);
9995 build = isl_ast_build_set_options(build, options);
9996 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
9997 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9998 isl_ast_build_free(build);
9999 isl_ast_node_free(tree);
10000 if (!tree)
10001 return -1;
10003 return 0;
10006 /* Check that the expression
10008 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
10010 * is not combined into
10012 * min(n/2, 0)
10014 * as this would result in n/2 being evaluated in parts of
10015 * the definition domain where n is not a multiple of 2.
10017 static int test_ast_expr(isl_ctx *ctx)
10019 const char *str;
10020 isl_pw_aff *pa;
10021 isl_ast_build *build;
10022 isl_ast_expr *expr;
10023 int min_max;
10024 int is_min;
10026 min_max = isl_options_get_ast_build_detect_min_max(ctx);
10027 isl_options_set_ast_build_detect_min_max(ctx, 1);
10029 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
10030 pa = isl_pw_aff_read_from_str(ctx, str);
10031 build = isl_ast_build_alloc(ctx);
10032 expr = isl_ast_build_expr_from_pw_aff(build, pa);
10033 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
10034 isl_ast_expr_get_op_type(expr) == isl_ast_expr_op_min;
10035 isl_ast_build_free(build);
10036 isl_ast_expr_free(expr);
10038 isl_options_set_ast_build_detect_min_max(ctx, min_max);
10040 if (!expr)
10041 return -1;
10042 if (is_min)
10043 isl_die(ctx, isl_error_unknown,
10044 "expressions should not be combined", return -1);
10046 return 0;
10049 static int test_ast_gen(isl_ctx *ctx)
10051 if (test_ast_gen1(ctx) < 0)
10052 return -1;
10053 if (test_ast_gen2(ctx) < 0)
10054 return -1;
10055 if (test_ast_gen3(ctx) < 0)
10056 return -1;
10057 if (test_ast_gen4(ctx) < 0)
10058 return -1;
10059 if (test_ast_gen5(ctx) < 0)
10060 return -1;
10061 if (test_ast_expr(ctx) < 0)
10062 return -1;
10063 return 0;
10066 /* Check if dropping output dimensions from an isl_pw_multi_aff
10067 * works properly.
10069 static int test_pw_multi_aff(isl_ctx *ctx)
10071 const char *str;
10072 isl_pw_multi_aff *pma1, *pma2;
10073 int equal;
10075 str = "{ [i,j] -> [i+j, 4i-j] }";
10076 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
10077 str = "{ [i,j] -> [4i-j] }";
10078 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
10080 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
10082 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
10084 isl_pw_multi_aff_free(pma1);
10085 isl_pw_multi_aff_free(pma2);
10086 if (equal < 0)
10087 return -1;
10088 if (!equal)
10089 isl_die(ctx, isl_error_unknown,
10090 "expressions not equal", return -1);
10092 return 0;
10095 /* Check that we can properly parse multi piecewise affine expressions
10096 * where the piecewise affine expressions have different domains.
10098 static int test_multi_pw_aff_1(isl_ctx *ctx)
10100 const char *str;
10101 isl_set *dom, *dom2;
10102 isl_multi_pw_aff *mpa1, *mpa2;
10103 isl_pw_aff *pa;
10104 int equal;
10105 int equal_domain;
10107 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
10108 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
10109 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
10110 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
10111 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
10112 str = "{ [i] -> [(i : i > 0), 2i] }";
10113 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
10115 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
10117 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
10118 dom = isl_pw_aff_domain(pa);
10119 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
10120 dom2 = isl_pw_aff_domain(pa);
10121 equal_domain = isl_set_is_equal(dom, dom2);
10123 isl_set_free(dom);
10124 isl_set_free(dom2);
10125 isl_multi_pw_aff_free(mpa1);
10126 isl_multi_pw_aff_free(mpa2);
10128 if (equal < 0)
10129 return -1;
10130 if (!equal)
10131 isl_die(ctx, isl_error_unknown,
10132 "expressions not equal", return -1);
10134 if (equal_domain < 0)
10135 return -1;
10136 if (equal_domain)
10137 isl_die(ctx, isl_error_unknown,
10138 "domains unexpectedly equal", return -1);
10140 return 0;
10143 /* Check that the dimensions in the explicit domain
10144 * of a multi piecewise affine expression are properly
10145 * taken into account.
10147 static int test_multi_pw_aff_2(isl_ctx *ctx)
10149 const char *str;
10150 isl_bool involves1, involves2, involves3, equal;
10151 isl_multi_pw_aff *mpa, *mpa1, *mpa2;
10153 str = "{ A[x,y] -> B[] : x >= y }";
10154 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
10155 involves1 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 2);
10156 mpa1 = isl_multi_pw_aff_copy(mpa);
10158 mpa = isl_multi_pw_aff_insert_dims(mpa, isl_dim_in, 0, 1);
10159 involves2 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 1);
10160 involves3 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 1, 2);
10161 str = "{ [a,x,y] -> B[] : x >= y }";
10162 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
10163 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
10164 isl_multi_pw_aff_free(mpa2);
10166 mpa = isl_multi_pw_aff_drop_dims(mpa, isl_dim_in, 0, 1);
10167 mpa = isl_multi_pw_aff_set_tuple_name(mpa, isl_dim_in, "A");
10168 if (equal >= 0 && equal)
10169 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa1);
10170 isl_multi_pw_aff_free(mpa1);
10171 isl_multi_pw_aff_free(mpa);
10173 if (involves1 < 0 || involves2 < 0 || involves3 < 0 || equal < 0)
10174 return -1;
10175 if (!equal)
10176 isl_die(ctx, isl_error_unknown,
10177 "incorrect result of dimension insertion/removal",
10178 return isl_stat_error);
10179 if (!involves1 || involves2 || !involves3)
10180 isl_die(ctx, isl_error_unknown,
10181 "incorrect characterization of involved dimensions",
10182 return isl_stat_error);
10184 return 0;
10187 /* Check that isl_multi_union_pw_aff_multi_val_on_domain
10188 * sets the explicit domain of a zero-dimensional result,
10189 * such that it can be converted to an isl_union_map.
10191 static isl_stat test_multi_pw_aff_3(isl_ctx *ctx)
10193 isl_space *space;
10194 isl_union_set *dom;
10195 isl_multi_val *mv;
10196 isl_multi_union_pw_aff *mupa;
10197 isl_union_map *umap;
10199 dom = isl_union_set_read_from_str(ctx, "{ A[]; B[] }");
10200 space = isl_union_set_get_space(dom);
10201 mv = isl_multi_val_zero(isl_space_set_from_params(space));
10202 mupa = isl_multi_union_pw_aff_multi_val_on_domain(dom, mv);
10203 umap = isl_union_map_from_multi_union_pw_aff(mupa);
10204 isl_union_map_free(umap);
10205 if (!umap)
10206 return isl_stat_error;
10208 return isl_stat_ok;
10211 /* String descriptions of boxes that
10212 * are used for reconstructing box maps from their lower and upper bounds.
10214 static const char *multi_pw_aff_box_tests[] = {
10215 "{ A[x, y] -> [] : x + y >= 0 }",
10216 "[N] -> { A[x, y] -> [x] : x + y <= N }",
10217 "[N] -> { A[x, y] -> [x : y] : x + y <= N }",
10220 /* Check that map representations of boxes can be reconstructed
10221 * from their lower and upper bounds.
10223 static isl_stat test_multi_pw_aff_box(isl_ctx *ctx)
10225 int i;
10227 for (i = 0; i < ARRAY_SIZE(multi_pw_aff_box_tests); ++i) {
10228 const char *str;
10229 isl_bool equal;
10230 isl_map *map, *box;
10231 isl_multi_pw_aff *min, *max;
10233 str = multi_pw_aff_box_tests[i];
10234 map = isl_map_read_from_str(ctx, str);
10235 min = isl_map_min_multi_pw_aff(isl_map_copy(map));
10236 max = isl_map_max_multi_pw_aff(isl_map_copy(map));
10237 box = isl_map_universe(isl_map_get_space(map));
10238 box = isl_map_lower_bound_multi_pw_aff(box, min);
10239 box = isl_map_upper_bound_multi_pw_aff(box, max);
10240 equal = isl_map_is_equal(map, box);
10241 isl_map_free(map);
10242 isl_map_free(box);
10243 if (equal < 0)
10244 return isl_stat_error;
10245 if (!equal)
10246 isl_die(ctx, isl_error_unknown,
10247 "unexpected result", return isl_stat_error);
10250 return isl_stat_ok;
10253 /* Perform some tests on multi piecewise affine expressions.
10255 static int test_multi_pw_aff(isl_ctx *ctx)
10257 if (test_multi_pw_aff_1(ctx) < 0)
10258 return -1;
10259 if (test_multi_pw_aff_2(ctx) < 0)
10260 return -1;
10261 if (test_multi_pw_aff_3(ctx) < 0)
10262 return -1;
10263 if (test_multi_pw_aff_box(ctx) < 0)
10264 return -1;
10265 return 0;
10268 /* This is a regression test for a bug where isl_basic_map_simplify
10269 * would end up in an infinite loop. In particular, we construct
10270 * an empty basic set that is not obviously empty.
10271 * isl_basic_set_is_empty marks the basic set as empty.
10272 * After projecting out i3, the variable can be dropped completely,
10273 * but isl_basic_map_simplify refrains from doing so if the basic set
10274 * is empty and would end up in an infinite loop if it didn't test
10275 * explicitly for empty basic maps in the outer loop.
10277 static int test_simplify_1(isl_ctx *ctx)
10279 const char *str;
10280 isl_basic_set *bset;
10281 int empty;
10283 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
10284 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
10285 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
10286 "i3 >= i2 }";
10287 bset = isl_basic_set_read_from_str(ctx, str);
10288 empty = isl_basic_set_is_empty(bset);
10289 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
10290 isl_basic_set_free(bset);
10291 if (!bset)
10292 return -1;
10293 if (!empty)
10294 isl_die(ctx, isl_error_unknown,
10295 "basic set should be empty", return -1);
10297 return 0;
10300 /* Check that the equality in the set description below
10301 * is simplified away.
10303 static int test_simplify_2(isl_ctx *ctx)
10305 const char *str;
10306 isl_basic_set *bset;
10307 isl_bool universe;
10309 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
10310 bset = isl_basic_set_read_from_str(ctx, str);
10311 universe = isl_basic_set_plain_is_universe(bset);
10312 isl_basic_set_free(bset);
10314 if (universe < 0)
10315 return -1;
10316 if (!universe)
10317 isl_die(ctx, isl_error_unknown,
10318 "equality not simplified away", return -1);
10319 return 0;
10322 /* Some simplification tests.
10324 static int test_simplify(isl_ctx *ctx)
10326 if (test_simplify_1(ctx) < 0)
10327 return -1;
10328 if (test_simplify_2(ctx) < 0)
10329 return -1;
10330 return 0;
10333 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
10334 * with gbr context would fail to disable the use of the shifted tableau
10335 * when transferring equalities for the input to the context, resulting
10336 * in invalid sample values.
10338 static int test_partial_lexmin(isl_ctx *ctx)
10340 const char *str;
10341 isl_basic_set *bset;
10342 isl_basic_map *bmap;
10343 isl_map *map;
10345 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
10346 bmap = isl_basic_map_read_from_str(ctx, str);
10347 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
10348 bset = isl_basic_set_read_from_str(ctx, str);
10349 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
10350 isl_map_free(map);
10352 if (!map)
10353 return -1;
10355 return 0;
10358 /* Check that the variable compression performed on the existentially
10359 * quantified variables inside isl_basic_set_compute_divs is not confused
10360 * by the implicit equalities among the parameters.
10362 static int test_compute_divs(isl_ctx *ctx)
10364 const char *str;
10365 isl_basic_set *bset;
10366 isl_set *set;
10368 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
10369 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
10370 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
10371 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
10372 bset = isl_basic_set_read_from_str(ctx, str);
10373 set = isl_basic_set_compute_divs(bset);
10374 isl_set_free(set);
10375 if (!set)
10376 return -1;
10378 return 0;
10381 /* Check that isl_schedule_get_map is not confused by a schedule tree
10382 * with divergent filter node parameters, as can result from a call
10383 * to isl_schedule_intersect_domain.
10385 static int test_schedule_tree(isl_ctx *ctx)
10387 const char *str;
10388 isl_union_set *uset;
10389 isl_schedule *sched1, *sched2;
10390 isl_union_map *umap;
10392 uset = isl_union_set_read_from_str(ctx, "{ A[i] }");
10393 sched1 = isl_schedule_from_domain(uset);
10394 uset = isl_union_set_read_from_str(ctx, "{ B[] }");
10395 sched2 = isl_schedule_from_domain(uset);
10397 sched1 = isl_schedule_sequence(sched1, sched2);
10398 str = "[n] -> { A[i] : 0 <= i < n; B[] }";
10399 uset = isl_union_set_read_from_str(ctx, str);
10400 sched1 = isl_schedule_intersect_domain(sched1, uset);
10401 umap = isl_schedule_get_map(sched1);
10402 isl_schedule_free(sched1);
10403 isl_union_map_free(umap);
10404 if (!umap)
10405 return -1;
10407 return 0;
10410 /* Check that a zero-dimensional prefix schedule keeps track
10411 * of the domain and outer filters.
10413 static int test_schedule_tree_prefix(isl_ctx *ctx)
10415 const char *str;
10416 isl_bool equal;
10417 isl_union_set *uset;
10418 isl_union_set_list *filters;
10419 isl_multi_union_pw_aff *mupa, *mupa2;
10420 isl_schedule_node *node;
10422 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
10423 uset = isl_union_set_read_from_str(ctx, str);
10424 node = isl_schedule_node_from_domain(uset);
10425 node = isl_schedule_node_child(node, 0);
10427 str = "{ S1[i,j] : i > j }";
10428 uset = isl_union_set_read_from_str(ctx, str);
10429 filters = isl_union_set_list_from_union_set(uset);
10430 str = "{ S1[i,j] : i <= j; S2[i,j] }";
10431 uset = isl_union_set_read_from_str(ctx, str);
10432 filters = isl_union_set_list_add(filters, uset);
10433 node = isl_schedule_node_insert_sequence(node, filters);
10435 node = isl_schedule_node_child(node, 0);
10436 node = isl_schedule_node_child(node, 0);
10437 mupa = isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
10438 str = "([] : { S1[i,j] : i > j })";
10439 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx, str);
10440 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
10441 isl_multi_union_pw_aff_free(mupa2);
10442 isl_multi_union_pw_aff_free(mupa);
10443 isl_schedule_node_free(node);
10445 if (equal < 0)
10446 return -1;
10447 if (!equal)
10448 isl_die(ctx, isl_error_unknown, "unexpected prefix schedule",
10449 return -1);
10451 return 0;
10454 /* Check that the reaching domain elements and the prefix schedule
10455 * at a leaf node are the same before and after grouping.
10457 static int test_schedule_tree_group_1(isl_ctx *ctx)
10459 int equal;
10460 const char *str;
10461 isl_id *id;
10462 isl_union_set *uset;
10463 isl_multi_union_pw_aff *mupa;
10464 isl_union_pw_multi_aff *upma1, *upma2;
10465 isl_union_set *domain1, *domain2;
10466 isl_union_map *umap1, *umap2;
10467 isl_schedule_node *node;
10469 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
10470 uset = isl_union_set_read_from_str(ctx, str);
10471 node = isl_schedule_node_from_domain(uset);
10472 node = isl_schedule_node_child(node, 0);
10473 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
10474 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10475 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10476 node = isl_schedule_node_child(node, 0);
10477 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
10478 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10479 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10480 node = isl_schedule_node_child(node, 0);
10481 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
10482 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
10483 domain1 = isl_schedule_node_get_domain(node);
10484 id = isl_id_alloc(ctx, "group", NULL);
10485 node = isl_schedule_node_parent(node);
10486 node = isl_schedule_node_group(node, id);
10487 node = isl_schedule_node_child(node, 0);
10488 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
10489 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
10490 domain2 = isl_schedule_node_get_domain(node);
10491 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
10492 if (equal >= 0 && equal)
10493 equal = isl_union_set_is_equal(domain1, domain2);
10494 if (equal >= 0 && equal)
10495 equal = isl_union_map_is_equal(umap1, umap2);
10496 isl_union_map_free(umap1);
10497 isl_union_map_free(umap2);
10498 isl_union_set_free(domain1);
10499 isl_union_set_free(domain2);
10500 isl_union_pw_multi_aff_free(upma1);
10501 isl_union_pw_multi_aff_free(upma2);
10502 isl_schedule_node_free(node);
10504 if (equal < 0)
10505 return -1;
10506 if (!equal)
10507 isl_die(ctx, isl_error_unknown,
10508 "expressions not equal", return -1);
10510 return 0;
10513 /* Check that we can have nested groupings and that the union map
10514 * schedule representation is the same before and after the grouping.
10515 * Note that after the grouping, the union map representation contains
10516 * the domain constraints from the ranges of the expansion nodes,
10517 * while they are missing from the union map representation of
10518 * the tree without expansion nodes.
10520 * Also check that the global expansion is as expected.
10522 static int test_schedule_tree_group_2(isl_ctx *ctx)
10524 int equal, equal_expansion;
10525 const char *str;
10526 isl_id *id;
10527 isl_union_set *uset;
10528 isl_union_map *umap1, *umap2;
10529 isl_union_map *expansion1, *expansion2;
10530 isl_union_set_list *filters;
10531 isl_multi_union_pw_aff *mupa;
10532 isl_schedule *schedule;
10533 isl_schedule_node *node;
10535 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
10536 "S3[i,j] : 0 <= i,j < 10 }";
10537 uset = isl_union_set_read_from_str(ctx, str);
10538 node = isl_schedule_node_from_domain(uset);
10539 node = isl_schedule_node_child(node, 0);
10540 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
10541 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10542 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10543 node = isl_schedule_node_child(node, 0);
10544 str = "{ S1[i,j] }";
10545 uset = isl_union_set_read_from_str(ctx, str);
10546 filters = isl_union_set_list_from_union_set(uset);
10547 str = "{ S2[i,j]; S3[i,j] }";
10548 uset = isl_union_set_read_from_str(ctx, str);
10549 filters = isl_union_set_list_add(filters, uset);
10550 node = isl_schedule_node_insert_sequence(node, filters);
10551 node = isl_schedule_node_child(node, 1);
10552 node = isl_schedule_node_child(node, 0);
10553 str = "{ S2[i,j] }";
10554 uset = isl_union_set_read_from_str(ctx, str);
10555 filters = isl_union_set_list_from_union_set(uset);
10556 str = "{ S3[i,j] }";
10557 uset = isl_union_set_read_from_str(ctx, str);
10558 filters = isl_union_set_list_add(filters, uset);
10559 node = isl_schedule_node_insert_sequence(node, filters);
10561 schedule = isl_schedule_node_get_schedule(node);
10562 umap1 = isl_schedule_get_map(schedule);
10563 uset = isl_schedule_get_domain(schedule);
10564 umap1 = isl_union_map_intersect_domain(umap1, uset);
10565 isl_schedule_free(schedule);
10567 node = isl_schedule_node_parent(node);
10568 node = isl_schedule_node_parent(node);
10569 id = isl_id_alloc(ctx, "group1", NULL);
10570 node = isl_schedule_node_group(node, id);
10571 node = isl_schedule_node_child(node, 1);
10572 node = isl_schedule_node_child(node, 0);
10573 id = isl_id_alloc(ctx, "group2", NULL);
10574 node = isl_schedule_node_group(node, id);
10576 schedule = isl_schedule_node_get_schedule(node);
10577 umap2 = isl_schedule_get_map(schedule);
10578 isl_schedule_free(schedule);
10580 node = isl_schedule_node_root(node);
10581 node = isl_schedule_node_child(node, 0);
10582 expansion1 = isl_schedule_node_get_subtree_expansion(node);
10583 isl_schedule_node_free(node);
10585 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
10586 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
10587 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
10589 expansion2 = isl_union_map_read_from_str(ctx, str);
10591 equal = isl_union_map_is_equal(umap1, umap2);
10592 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
10594 isl_union_map_free(umap1);
10595 isl_union_map_free(umap2);
10596 isl_union_map_free(expansion1);
10597 isl_union_map_free(expansion2);
10599 if (equal < 0 || equal_expansion < 0)
10600 return -1;
10601 if (!equal)
10602 isl_die(ctx, isl_error_unknown,
10603 "expressions not equal", return -1);
10604 if (!equal_expansion)
10605 isl_die(ctx, isl_error_unknown,
10606 "unexpected expansion", return -1);
10608 return 0;
10611 /* Some tests for the isl_schedule_node_group function.
10613 static int test_schedule_tree_group(isl_ctx *ctx)
10615 if (test_schedule_tree_group_1(ctx) < 0)
10616 return -1;
10617 if (test_schedule_tree_group_2(ctx) < 0)
10618 return -1;
10619 return 0;
10622 struct {
10623 const char *set;
10624 const char *dual;
10625 } coef_tests[] = {
10626 { "{ rat: [i] : 0 <= i <= 10 }",
10627 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
10628 { "{ rat: [i] : FALSE }",
10629 "{ rat: coefficients[[cst] -> [a]] }" },
10630 { "{ rat: [i] : }",
10631 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
10632 { "{ [0:,1,2:3] }",
10633 "{ rat: coefficients[[c_cst] -> [a, b, c]] : "
10634 "a >= 0 and 2c >= -c_cst - b and 3c >= -c_cst - b }" },
10635 { "[M, N] -> { [x = (1 - N):-1, -4x:(M - 4x)] }",
10636 "{ rat: coefficients[[c_cst, c_M = 0:, c_N = 0:] -> [a, b = -c_M:]] :"
10637 "4b >= -c_N + a and 4b >= -c_cst - 2c_N + a }" },
10638 { "{ rat : [x, y] : 1 <= 2x <= 9 and 2 <= 3y <= 16 }",
10639 "{ rat: coefficients[[c_cst] -> [c_x, c_y]] : "
10640 "4c_y >= -6c_cst - 3c_x and 4c_y >= -6c_cst - 27c_x and "
10641 "32c_y >= -6c_cst - 3c_x and 32c_y >= -6c_cst - 27c_x }" },
10642 { "{ [x, y, z] : 3y <= 2x - 2 and y >= -2 + 2x and 2y >= 2 - x }",
10643 "{ rat: coefficients[[cst] -> [a, b, c]] }" },
10646 struct {
10647 const char *set;
10648 const char *dual;
10649 } sol_tests[] = {
10650 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
10651 "{ rat: [i] : 0 <= i <= 10 }" },
10652 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
10653 "{ rat: [i] }" },
10654 { "{ rat: coefficients[[cst] -> [a]] }",
10655 "{ rat: [i] : FALSE }" },
10658 /* Test the basic functionality of isl_basic_set_coefficients and
10659 * isl_basic_set_solutions.
10661 static int test_dual(isl_ctx *ctx)
10663 int i;
10665 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
10666 int equal;
10667 isl_basic_set *bset1, *bset2;
10669 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
10670 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
10671 bset1 = isl_basic_set_coefficients(bset1);
10672 equal = isl_basic_set_is_equal(bset1, bset2);
10673 isl_basic_set_free(bset1);
10674 isl_basic_set_free(bset2);
10675 if (equal < 0)
10676 return -1;
10677 if (!equal)
10678 isl_die(ctx, isl_error_unknown,
10679 "incorrect dual", return -1);
10682 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
10683 int equal;
10684 isl_basic_set *bset1, *bset2;
10686 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
10687 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
10688 bset1 = isl_basic_set_solutions(bset1);
10689 equal = isl_basic_set_is_equal(bset1, bset2);
10690 isl_basic_set_free(bset1);
10691 isl_basic_set_free(bset2);
10692 if (equal < 0)
10693 return -1;
10694 if (!equal)
10695 isl_die(ctx, isl_error_unknown,
10696 "incorrect dual", return -1);
10699 return 0;
10702 struct {
10703 int scale_tile;
10704 int shift_point;
10705 const char *domain;
10706 const char *schedule;
10707 const char *sizes;
10708 const char *tile;
10709 const char *point;
10710 } tile_tests[] = {
10711 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
10712 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10713 "{ [32,32] }",
10714 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
10715 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10717 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
10718 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10719 "{ [32,32] }",
10720 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
10721 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10723 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
10724 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10725 "{ [32,32] }",
10726 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
10727 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
10729 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
10730 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10731 "{ [32,32] }",
10732 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
10733 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
10737 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
10738 * tile the band and then check if the tile and point bands have the
10739 * expected partial schedule.
10741 static int test_tile(isl_ctx *ctx)
10743 int i;
10744 int scale;
10745 int shift;
10747 scale = isl_options_get_tile_scale_tile_loops(ctx);
10748 shift = isl_options_get_tile_shift_point_loops(ctx);
10750 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
10751 int opt;
10752 int equal;
10753 const char *str;
10754 isl_union_set *domain;
10755 isl_multi_union_pw_aff *mupa, *mupa2;
10756 isl_schedule_node *node;
10757 isl_multi_val *sizes;
10759 opt = tile_tests[i].scale_tile;
10760 isl_options_set_tile_scale_tile_loops(ctx, opt);
10761 opt = tile_tests[i].shift_point;
10762 isl_options_set_tile_shift_point_loops(ctx, opt);
10764 str = tile_tests[i].domain;
10765 domain = isl_union_set_read_from_str(ctx, str);
10766 node = isl_schedule_node_from_domain(domain);
10767 node = isl_schedule_node_child(node, 0);
10768 str = tile_tests[i].schedule;
10769 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10770 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10771 str = tile_tests[i].sizes;
10772 sizes = isl_multi_val_read_from_str(ctx, str);
10773 node = isl_schedule_node_band_tile(node, sizes);
10775 str = tile_tests[i].tile;
10776 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10777 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
10778 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
10779 isl_multi_union_pw_aff_free(mupa);
10780 isl_multi_union_pw_aff_free(mupa2);
10782 node = isl_schedule_node_child(node, 0);
10784 str = tile_tests[i].point;
10785 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10786 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
10787 if (equal >= 0 && equal)
10788 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
10789 mupa2);
10790 isl_multi_union_pw_aff_free(mupa);
10791 isl_multi_union_pw_aff_free(mupa2);
10793 isl_schedule_node_free(node);
10795 if (equal < 0)
10796 return -1;
10797 if (!equal)
10798 isl_die(ctx, isl_error_unknown,
10799 "unexpected result", return -1);
10802 isl_options_set_tile_scale_tile_loops(ctx, scale);
10803 isl_options_set_tile_shift_point_loops(ctx, shift);
10805 return 0;
10808 /* Check that the domain hash of a space is equal to the hash
10809 * of the domain of the space, both ignoring parameters.
10811 static int test_domain_hash(isl_ctx *ctx)
10813 isl_map *map;
10814 isl_space *space;
10815 uint32_t hash1, hash2;
10817 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
10818 space = isl_map_get_space(map);
10819 isl_map_free(map);
10820 hash1 = isl_space_get_tuple_domain_hash(space);
10821 space = isl_space_domain(space);
10822 hash2 = isl_space_get_tuple_hash(space);
10823 isl_space_free(space);
10825 if (!space)
10826 return -1;
10827 if (hash1 != hash2)
10828 isl_die(ctx, isl_error_unknown,
10829 "domain hash not equal to hash of domain", return -1);
10831 return 0;
10834 /* Check that a universe basic set that is not obviously equal to the universe
10835 * is still recognized as being equal to the universe.
10837 static int test_universe(isl_ctx *ctx)
10839 const char *s;
10840 isl_basic_set *bset;
10841 isl_bool is_univ;
10843 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
10844 bset = isl_basic_set_read_from_str(ctx, s);
10845 is_univ = isl_basic_set_is_universe(bset);
10846 isl_basic_set_free(bset);
10848 if (is_univ < 0)
10849 return -1;
10850 if (!is_univ)
10851 isl_die(ctx, isl_error_unknown,
10852 "not recognized as universe set", return -1);
10854 return 0;
10857 /* Sets for which chambers are computed and checked.
10859 const char *chambers_tests[] = {
10860 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
10861 "z >= 0 and z <= C - y and z <= B - x - y }",
10864 /* Add the domain of "cell" to "cells".
10866 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
10868 isl_basic_set_list **cells = user;
10869 isl_basic_set *dom;
10871 dom = isl_cell_get_domain(cell);
10872 isl_cell_free(cell);
10873 *cells = isl_basic_set_list_add(*cells, dom);
10875 return *cells ? isl_stat_ok : isl_stat_error;
10878 /* Check that the elements of "list" are pairwise disjoint.
10880 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
10882 int i, j;
10883 isl_size n;
10885 n = isl_basic_set_list_n_basic_set(list);
10886 if (n < 0)
10887 return isl_stat_error;
10889 for (i = 0; i < n; ++i) {
10890 isl_basic_set *bset_i;
10892 bset_i = isl_basic_set_list_get_basic_set(list, i);
10893 for (j = i + 1; j < n; ++j) {
10894 isl_basic_set *bset_j;
10895 isl_bool disjoint;
10897 bset_j = isl_basic_set_list_get_basic_set(list, j);
10898 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
10899 isl_basic_set_free(bset_j);
10900 if (!disjoint)
10901 isl_die(isl_basic_set_list_get_ctx(list),
10902 isl_error_unknown, "not disjoint",
10903 break);
10904 if (disjoint < 0 || !disjoint)
10905 break;
10907 isl_basic_set_free(bset_i);
10908 if (j < n)
10909 return isl_stat_error;
10912 return isl_stat_ok;
10915 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
10916 * are pairwise disjoint.
10918 static int test_chambers(isl_ctx *ctx)
10920 int i;
10922 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
10923 isl_basic_set *bset;
10924 isl_vertices *vertices;
10925 isl_basic_set_list *cells;
10926 isl_stat ok;
10928 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
10929 vertices = isl_basic_set_compute_vertices(bset);
10930 cells = isl_basic_set_list_alloc(ctx, 0);
10931 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
10932 &cells) < 0)
10933 cells = isl_basic_set_list_free(cells);
10934 ok = check_pairwise_disjoint(cells);
10935 isl_basic_set_list_free(cells);
10936 isl_vertices_free(vertices);
10937 isl_basic_set_free(bset);
10939 if (ok < 0)
10940 return -1;
10943 return 0;
10946 struct {
10947 const char *name;
10948 int (*fn)(isl_ctx *ctx);
10949 } tests [] = {
10950 { "universe", &test_universe },
10951 { "domain hash", &test_domain_hash },
10952 { "dual", &test_dual },
10953 { "dependence analysis", &test_flow },
10954 { "val", &test_val },
10955 { "compute divs", &test_compute_divs },
10956 { "partial lexmin", &test_partial_lexmin },
10957 { "simplify", &test_simplify },
10958 { "curry", &test_curry },
10959 { "piecewise multi affine expressions", &test_pw_multi_aff },
10960 { "multi piecewise affine expressions", &test_multi_pw_aff },
10961 { "conversion", &test_conversion },
10962 { "list", &test_list },
10963 { "align parameters", &test_align_parameters },
10964 { "drop unused parameters", &test_drop_unused_parameters },
10965 { "preimage", &test_preimage },
10966 { "pullback", &test_pullback },
10967 { "AST", &test_ast },
10968 { "AST build", &test_ast_build },
10969 { "AST generation", &test_ast_gen },
10970 { "eliminate", &test_eliminate },
10971 { "deltas_map", &test_deltas_map },
10972 { "residue class", &test_residue_class },
10973 { "div", &test_div },
10974 { "slice", &test_slice },
10975 { "fixed power", &test_fixed_power },
10976 { "sample", &test_sample },
10977 { "empty projection", &test_empty_projection },
10978 { "output", &test_output },
10979 { "vertices", &test_vertices },
10980 { "chambers", &test_chambers },
10981 { "fixed", &test_fixed },
10982 { "equal", &test_equal },
10983 { "disjoint", &test_disjoint },
10984 { "product", &test_product },
10985 { "dim_max", &test_dim_max },
10986 { "affine", &test_aff },
10987 { "injective", &test_injective },
10988 { "schedule (whole component)", &test_schedule_whole },
10989 { "schedule (incremental)", &test_schedule_incremental },
10990 { "schedule tree", &test_schedule_tree },
10991 { "schedule tree prefix", &test_schedule_tree_prefix },
10992 { "schedule tree grouping", &test_schedule_tree_group },
10993 { "tile", &test_tile },
10994 { "union map", &test_union_map },
10995 { "union_pw", &test_union_pw },
10996 { "locus", &test_locus },
10997 { "eval", &test_eval },
10998 { "parse", &test_parse },
10999 { "single-valued", &test_sv },
11000 { "recession cone", &test_recession_cone },
11001 { "affine hull", &test_affine_hull },
11002 { "simple_hull", &test_simple_hull },
11003 { "box hull", &test_box_hull },
11004 { "coalesce", &test_coalesce },
11005 { "factorize", &test_factorize },
11006 { "subset", &test_subset },
11007 { "subtract", &test_subtract },
11008 { "intersect", &test_intersect },
11009 { "lexmin", &test_lexmin },
11010 { "min", &test_min },
11011 { "set lower bounds", &test_min_mpa },
11012 { "gist", &test_gist },
11013 { "piecewise quasi-polynomials", &test_pwqp },
11014 { "lift", &test_lift },
11015 { "bind parameters", &test_bind },
11016 { "unbind parameters", &test_unbind },
11017 { "bound", &test_bound },
11018 { "get lists", &test_get_list },
11019 { "union", &test_union },
11020 { "split periods", &test_split_periods },
11021 { "lexicographic order", &test_lex },
11022 { "bijectivity", &test_bijective },
11023 { "dataflow analysis", &test_dep },
11024 { "reading", &test_read },
11025 { "bounded", &test_bounded },
11026 { "construction", &test_construction },
11027 { "dimension manipulation", &test_dim },
11028 { "map application", &test_application },
11029 { "convex hull", &test_convex_hull },
11030 { "transitive closure", &test_closure },
11031 { "isl_bool", &test_isl_bool},
11034 int main(int argc, char **argv)
11036 int i;
11037 struct isl_ctx *ctx;
11038 struct isl_options *options;
11040 options = isl_options_new_with_defaults();
11041 assert(options);
11042 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
11044 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
11045 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
11046 printf("%s\n", tests[i].name);
11047 if (tests[i].fn(ctx) < 0)
11048 goto error;
11050 isl_ctx_free(ctx);
11051 return 0;
11052 error:
11053 isl_ctx_free(ctx);
11054 return -1;