isl_union_*_align_params: handle inputs that are not parameter spaces
[isl.git] / isl_test.c
blob94e105e21b1344164c1b8eb77775c56721cf6588
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <assert.h>
11 #include <stdio.h>
12 #include <limits.h>
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_aff_private.h>
16 #include <isl/set.h>
17 #include <isl/flow.h>
18 #include <isl/constraint.h>
19 #include <isl/polynomial.h>
20 #include <isl/union_map.h>
21 #include <isl_factorization.h>
22 #include <isl/schedule.h>
23 #include <isl_options_private.h>
24 #include <isl/vertices.h>
25 #include <isl/ast_build.h>
26 #include <isl/val.h>
27 #include <isl/ilp.h>
29 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
31 static char *srcdir;
33 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
34 char *filename;
35 int length;
36 char *pattern = "%s/test_inputs/%s.%s";
38 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
39 + strlen(suffix) + 1;
40 filename = isl_alloc_array(ctx, char, length);
42 if (!filename)
43 return NULL;
45 sprintf(filename, pattern, srcdir, name, suffix);
47 return filename;
50 void test_parse_map(isl_ctx *ctx, const char *str)
52 isl_map *map;
54 map = isl_map_read_from_str(ctx, str);
55 assert(map);
56 isl_map_free(map);
59 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
61 isl_map *map, *map2;
62 int equal;
64 map = isl_map_read_from_str(ctx, str);
65 map2 = isl_map_read_from_str(ctx, str2);
66 equal = isl_map_is_equal(map, map2);
67 isl_map_free(map);
68 isl_map_free(map2);
70 if (equal < 0)
71 return -1;
72 if (!equal)
73 isl_die(ctx, isl_error_unknown, "maps not equal",
74 return -1);
76 return 0;
79 void test_parse_pwqp(isl_ctx *ctx, const char *str)
81 isl_pw_qpolynomial *pwqp;
83 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
84 assert(pwqp);
85 isl_pw_qpolynomial_free(pwqp);
88 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
90 isl_pw_aff *pwaff;
92 pwaff = isl_pw_aff_read_from_str(ctx, str);
93 assert(pwaff);
94 isl_pw_aff_free(pwaff);
97 int test_parse(struct isl_ctx *ctx)
99 isl_map *map, *map2;
100 const char *str, *str2;
102 str = "{ [i] -> [-i] }";
103 map = isl_map_read_from_str(ctx, str);
104 assert(map);
105 isl_map_free(map);
107 str = "{ A[i] -> L[([i/3])] }";
108 map = isl_map_read_from_str(ctx, str);
109 assert(map);
110 isl_map_free(map);
112 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
113 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
114 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
116 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
117 str2 = "{ [x, y] : 2y >= 6 - x }";
118 if (test_parse_map_equal(ctx, str, str2) < 0)
119 return -1;
121 if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
122 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
123 return -1;
124 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
125 if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
126 str) < 0)
127 return -1;
129 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
130 map = isl_map_read_from_str(ctx, str);
131 str = "{ [new, old] -> [o0, o1] : "
132 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
133 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
134 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
135 map2 = isl_map_read_from_str(ctx, str);
136 assert(isl_map_is_equal(map, map2));
137 isl_map_free(map);
138 isl_map_free(map2);
140 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
141 map = isl_map_read_from_str(ctx, str);
142 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
143 map2 = isl_map_read_from_str(ctx, str);
144 assert(isl_map_is_equal(map, map2));
145 isl_map_free(map);
146 isl_map_free(map2);
148 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
149 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
150 if (test_parse_map_equal(ctx, str, str2) < 0)
151 return -1;
153 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
154 str2 = "{ [i,j] -> [min(i,j)] }";
155 if (test_parse_map_equal(ctx, str, str2) < 0)
156 return -1;
158 str = "{ [i,j] : i != j }";
159 str2 = "{ [i,j] : i < j or i > j }";
160 if (test_parse_map_equal(ctx, str, str2) < 0)
161 return -1;
163 str = "{ [i,j] : (i+1)*2 >= j }";
164 str2 = "{ [i, j] : j <= 2 + 2i }";
165 if (test_parse_map_equal(ctx, str, str2) < 0)
166 return -1;
168 str = "{ [i] -> [i > 0 ? 4 : 5] }";
169 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
170 if (test_parse_map_equal(ctx, str, str2) < 0)
171 return -1;
173 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
174 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
175 if (test_parse_map_equal(ctx, str, str2) < 0)
176 return -1;
178 str = "{ [x] : x >= 0 }";
179 str2 = "{ [x] : x-0 >= 0 }";
180 if (test_parse_map_equal(ctx, str, str2) < 0)
181 return -1;
183 str = "{ [i] : ((i > 10)) }";
184 str2 = "{ [i] : i >= 11 }";
185 if (test_parse_map_equal(ctx, str, str2) < 0)
186 return -1;
188 str = "{ [i] -> [0] }";
189 str2 = "{ [i] -> [0 * i] }";
190 if (test_parse_map_equal(ctx, str, str2) < 0)
191 return -1;
193 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
194 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
195 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
196 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
198 if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
199 "{ [a] -> [b] : true }") < 0)
200 return -1;
202 if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
203 "{ [i] : i <= 10 }") < 0)
204 return -1;
206 if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
207 "[n] -> { [i] : i <= n }") < 0)
208 return -1;
210 if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
211 return -1;
213 if (test_parse_map_equal(ctx, "{ [i] : 2*floor(i/2) = i }",
214 "{ [i] : exists a : i = 2 a }") < 0)
215 return -1;
217 if (test_parse_map_equal(ctx, "{ [a] -> [b] : a = 5 implies b = 5 }",
218 "{ [a] -> [b] : a != 5 or b = 5 }") < 0)
219 return -1;
221 return 0;
224 void test_read(struct isl_ctx *ctx)
226 char *filename;
227 FILE *input;
228 struct isl_basic_set *bset1, *bset2;
229 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
231 filename = get_filename(ctx, "set", "omega");
232 assert(filename);
233 input = fopen(filename, "r");
234 assert(input);
236 bset1 = isl_basic_set_read_from_file(ctx, input);
237 bset2 = isl_basic_set_read_from_str(ctx, str);
239 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
241 isl_basic_set_free(bset1);
242 isl_basic_set_free(bset2);
243 free(filename);
245 fclose(input);
248 void test_bounded(struct isl_ctx *ctx)
250 isl_set *set;
251 int bounded;
253 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
254 bounded = isl_set_is_bounded(set);
255 assert(bounded);
256 isl_set_free(set);
258 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
259 bounded = isl_set_is_bounded(set);
260 assert(!bounded);
261 isl_set_free(set);
263 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
264 bounded = isl_set_is_bounded(set);
265 assert(!bounded);
266 isl_set_free(set);
269 /* Construct the basic set { [i] : 5 <= i <= N } */
270 void test_construction(struct isl_ctx *ctx)
272 isl_int v;
273 isl_space *dim;
274 isl_local_space *ls;
275 struct isl_basic_set *bset;
276 struct isl_constraint *c;
278 isl_int_init(v);
280 dim = isl_space_set_alloc(ctx, 1, 1);
281 bset = isl_basic_set_universe(isl_space_copy(dim));
282 ls = isl_local_space_from_space(dim);
284 c = isl_inequality_alloc(isl_local_space_copy(ls));
285 isl_int_set_si(v, -1);
286 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
287 isl_int_set_si(v, 1);
288 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
289 bset = isl_basic_set_add_constraint(bset, c);
291 c = isl_inequality_alloc(isl_local_space_copy(ls));
292 isl_int_set_si(v, 1);
293 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
294 isl_int_set_si(v, -5);
295 isl_constraint_set_constant(c, v);
296 bset = isl_basic_set_add_constraint(bset, c);
298 isl_local_space_free(ls);
299 isl_basic_set_free(bset);
301 isl_int_clear(v);
304 void test_dim(struct isl_ctx *ctx)
306 const char *str;
307 isl_map *map1, *map2;
309 map1 = isl_map_read_from_str(ctx,
310 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
311 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
312 map2 = isl_map_read_from_str(ctx,
313 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
314 assert(isl_map_is_equal(map1, map2));
315 isl_map_free(map2);
317 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
318 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
319 assert(isl_map_is_equal(map1, map2));
321 isl_map_free(map1);
322 isl_map_free(map2);
324 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
325 map1 = isl_map_read_from_str(ctx, str);
326 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
327 map2 = isl_map_read_from_str(ctx, str);
328 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
329 assert(isl_map_is_equal(map1, map2));
331 isl_map_free(map1);
332 isl_map_free(map2);
335 struct {
336 __isl_give isl_val *(*op)(__isl_take isl_val *v);
337 const char *arg;
338 const char *res;
339 } val_un_tests[] = {
340 { &isl_val_neg, "0", "0" },
341 { &isl_val_abs, "0", "0" },
342 { &isl_val_2exp, "0", "1" },
343 { &isl_val_floor, "0", "0" },
344 { &isl_val_ceil, "0", "0" },
345 { &isl_val_neg, "1", "-1" },
346 { &isl_val_neg, "-1", "1" },
347 { &isl_val_neg, "1/2", "-1/2" },
348 { &isl_val_neg, "-1/2", "1/2" },
349 { &isl_val_neg, "infty", "-infty" },
350 { &isl_val_neg, "-infty", "infty" },
351 { &isl_val_neg, "NaN", "NaN" },
352 { &isl_val_abs, "1", "1" },
353 { &isl_val_abs, "-1", "1" },
354 { &isl_val_abs, "1/2", "1/2" },
355 { &isl_val_abs, "-1/2", "1/2" },
356 { &isl_val_abs, "infty", "infty" },
357 { &isl_val_abs, "-infty", "infty" },
358 { &isl_val_abs, "NaN", "NaN" },
359 { &isl_val_floor, "1", "1" },
360 { &isl_val_floor, "-1", "-1" },
361 { &isl_val_floor, "1/2", "0" },
362 { &isl_val_floor, "-1/2", "-1" },
363 { &isl_val_floor, "infty", "infty" },
364 { &isl_val_floor, "-infty", "-infty" },
365 { &isl_val_floor, "NaN", "NaN" },
366 { &isl_val_ceil, "1", "1" },
367 { &isl_val_ceil, "-1", "-1" },
368 { &isl_val_ceil, "1/2", "1" },
369 { &isl_val_ceil, "-1/2", "0" },
370 { &isl_val_ceil, "infty", "infty" },
371 { &isl_val_ceil, "-infty", "-infty" },
372 { &isl_val_ceil, "NaN", "NaN" },
373 { &isl_val_2exp, "-3", "1/8" },
374 { &isl_val_2exp, "-1", "1/2" },
375 { &isl_val_2exp, "1", "2" },
376 { &isl_val_2exp, "2", "4" },
377 { &isl_val_2exp, "3", "8" },
380 /* Perform some basic tests of unary operations on isl_val objects.
382 static int test_un_val(isl_ctx *ctx)
384 int i;
385 isl_val *v, *res;
386 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
387 int ok;
389 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
390 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
391 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
392 fn = val_un_tests[i].op;
393 v = fn(v);
394 if (isl_val_is_nan(res))
395 ok = isl_val_is_nan(v);
396 else
397 ok = isl_val_eq(v, res);
398 isl_val_free(v);
399 isl_val_free(res);
400 if (ok < 0)
401 return -1;
402 if (!ok)
403 isl_die(ctx, isl_error_unknown,
404 "unexpected result", return -1);
407 return 0;
410 struct {
411 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
412 __isl_take isl_val *v2);
413 } val_bin_op[] = {
414 ['+'] = { &isl_val_add },
415 ['-'] = { &isl_val_sub },
416 ['*'] = { &isl_val_mul },
417 ['/'] = { &isl_val_div },
418 ['g'] = { &isl_val_gcd },
419 ['m'] = { &isl_val_min },
420 ['M'] = { &isl_val_max },
423 struct {
424 const char *arg1;
425 unsigned char op;
426 const char *arg2;
427 const char *res;
428 } val_bin_tests[] = {
429 { "0", '+', "0", "0" },
430 { "1", '+', "0", "1" },
431 { "1", '+', "1", "2" },
432 { "1", '-', "1", "0" },
433 { "1", '*', "1", "1" },
434 { "1", '/', "1", "1" },
435 { "2", '*', "3", "6" },
436 { "2", '*', "1/2", "1" },
437 { "2", '*', "1/3", "2/3" },
438 { "2/3", '*', "3/5", "2/5" },
439 { "2/3", '*', "7/5", "14/15" },
440 { "2", '/', "1/2", "4" },
441 { "-2", '/', "-1/2", "4" },
442 { "-2", '/', "1/2", "-4" },
443 { "2", '/', "-1/2", "-4" },
444 { "2", '/', "2", "1" },
445 { "2", '/', "3", "2/3" },
446 { "2/3", '/', "5/3", "2/5" },
447 { "2/3", '/', "5/7", "14/15" },
448 { "0", '/', "0", "NaN" },
449 { "42", '/', "0", "NaN" },
450 { "-42", '/', "0", "NaN" },
451 { "infty", '/', "0", "NaN" },
452 { "-infty", '/', "0", "NaN" },
453 { "NaN", '/', "0", "NaN" },
454 { "0", '/', "NaN", "NaN" },
455 { "42", '/', "NaN", "NaN" },
456 { "-42", '/', "NaN", "NaN" },
457 { "infty", '/', "NaN", "NaN" },
458 { "-infty", '/', "NaN", "NaN" },
459 { "NaN", '/', "NaN", "NaN" },
460 { "0", '/', "infty", "0" },
461 { "42", '/', "infty", "0" },
462 { "-42", '/', "infty", "0" },
463 { "infty", '/', "infty", "NaN" },
464 { "-infty", '/', "infty", "NaN" },
465 { "NaN", '/', "infty", "NaN" },
466 { "0", '/', "-infty", "0" },
467 { "42", '/', "-infty", "0" },
468 { "-42", '/', "-infty", "0" },
469 { "infty", '/', "-infty", "NaN" },
470 { "-infty", '/', "-infty", "NaN" },
471 { "NaN", '/', "-infty", "NaN" },
472 { "1", '-', "1/3", "2/3" },
473 { "1/3", '+', "1/2", "5/6" },
474 { "1/2", '+', "1/2", "1" },
475 { "3/4", '-', "1/4", "1/2" },
476 { "1/2", '-', "1/3", "1/6" },
477 { "infty", '+', "42", "infty" },
478 { "infty", '+', "infty", "infty" },
479 { "42", '+', "infty", "infty" },
480 { "infty", '-', "infty", "NaN" },
481 { "infty", '*', "infty", "infty" },
482 { "infty", '*', "-infty", "-infty" },
483 { "-infty", '*', "infty", "-infty" },
484 { "-infty", '*', "-infty", "infty" },
485 { "0", '*', "infty", "NaN" },
486 { "1", '*', "infty", "infty" },
487 { "infty", '*', "0", "NaN" },
488 { "infty", '*', "42", "infty" },
489 { "42", '-', "infty", "-infty" },
490 { "infty", '+', "-infty", "NaN" },
491 { "4", 'g', "6", "2" },
492 { "5", 'g', "6", "1" },
493 { "42", 'm', "3", "3" },
494 { "42", 'M', "3", "42" },
495 { "3", 'm', "42", "3" },
496 { "3", 'M', "42", "42" },
497 { "42", 'm', "infty", "42" },
498 { "42", 'M', "infty", "infty" },
499 { "42", 'm', "-infty", "-infty" },
500 { "42", 'M', "-infty", "42" },
501 { "42", 'm', "NaN", "NaN" },
502 { "42", 'M', "NaN", "NaN" },
503 { "infty", 'm', "-infty", "-infty" },
504 { "infty", 'M', "-infty", "infty" },
507 /* Perform some basic tests of binary operations on isl_val objects.
509 static int test_bin_val(isl_ctx *ctx)
511 int i;
512 isl_val *v1, *v2, *res;
513 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
514 __isl_take isl_val *v2);
515 int ok;
517 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
518 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
519 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
520 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
521 fn = val_bin_op[val_bin_tests[i].op].fn;
522 v1 = fn(v1, v2);
523 if (isl_val_is_nan(res))
524 ok = isl_val_is_nan(v1);
525 else
526 ok = isl_val_eq(v1, res);
527 isl_val_free(v1);
528 isl_val_free(res);
529 if (ok < 0)
530 return -1;
531 if (!ok)
532 isl_die(ctx, isl_error_unknown,
533 "unexpected result", return -1);
536 return 0;
539 /* Perform some basic tests on isl_val objects.
541 static int test_val(isl_ctx *ctx)
543 if (test_un_val(ctx) < 0)
544 return -1;
545 if (test_bin_val(ctx) < 0)
546 return -1;
547 return 0;
550 static int test_div(isl_ctx *ctx)
552 unsigned n;
553 const char *str;
554 int empty;
555 isl_int v;
556 isl_space *dim;
557 isl_set *set;
558 isl_local_space *ls;
559 struct isl_basic_set *bset;
560 struct isl_constraint *c;
562 isl_int_init(v);
564 /* test 1 */
565 dim = isl_space_set_alloc(ctx, 0, 3);
566 bset = isl_basic_set_universe(isl_space_copy(dim));
567 ls = isl_local_space_from_space(dim);
569 c = isl_equality_alloc(isl_local_space_copy(ls));
570 isl_int_set_si(v, -1);
571 isl_constraint_set_constant(c, v);
572 isl_int_set_si(v, 1);
573 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
574 isl_int_set_si(v, 3);
575 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
576 bset = isl_basic_set_add_constraint(bset, c);
578 c = isl_equality_alloc(isl_local_space_copy(ls));
579 isl_int_set_si(v, 1);
580 isl_constraint_set_constant(c, v);
581 isl_int_set_si(v, -1);
582 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
583 isl_int_set_si(v, 3);
584 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
585 bset = isl_basic_set_add_constraint(bset, c);
587 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
589 assert(bset && bset->n_div == 1);
590 isl_local_space_free(ls);
591 isl_basic_set_free(bset);
593 /* test 2 */
594 dim = isl_space_set_alloc(ctx, 0, 3);
595 bset = isl_basic_set_universe(isl_space_copy(dim));
596 ls = isl_local_space_from_space(dim);
598 c = isl_equality_alloc(isl_local_space_copy(ls));
599 isl_int_set_si(v, 1);
600 isl_constraint_set_constant(c, v);
601 isl_int_set_si(v, -1);
602 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
603 isl_int_set_si(v, 3);
604 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
605 bset = isl_basic_set_add_constraint(bset, c);
607 c = isl_equality_alloc(isl_local_space_copy(ls));
608 isl_int_set_si(v, -1);
609 isl_constraint_set_constant(c, v);
610 isl_int_set_si(v, 1);
611 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
612 isl_int_set_si(v, 3);
613 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
614 bset = isl_basic_set_add_constraint(bset, c);
616 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
618 assert(bset && bset->n_div == 1);
619 isl_local_space_free(ls);
620 isl_basic_set_free(bset);
622 /* test 3 */
623 dim = isl_space_set_alloc(ctx, 0, 3);
624 bset = isl_basic_set_universe(isl_space_copy(dim));
625 ls = isl_local_space_from_space(dim);
627 c = isl_equality_alloc(isl_local_space_copy(ls));
628 isl_int_set_si(v, 1);
629 isl_constraint_set_constant(c, v);
630 isl_int_set_si(v, -1);
631 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
632 isl_int_set_si(v, 3);
633 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
634 bset = isl_basic_set_add_constraint(bset, c);
636 c = isl_equality_alloc(isl_local_space_copy(ls));
637 isl_int_set_si(v, -3);
638 isl_constraint_set_constant(c, v);
639 isl_int_set_si(v, 1);
640 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
641 isl_int_set_si(v, 4);
642 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
643 bset = isl_basic_set_add_constraint(bset, c);
645 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
647 assert(bset && bset->n_div == 1);
648 isl_local_space_free(ls);
649 isl_basic_set_free(bset);
651 /* test 4 */
652 dim = isl_space_set_alloc(ctx, 0, 3);
653 bset = isl_basic_set_universe(isl_space_copy(dim));
654 ls = isl_local_space_from_space(dim);
656 c = isl_equality_alloc(isl_local_space_copy(ls));
657 isl_int_set_si(v, 2);
658 isl_constraint_set_constant(c, v);
659 isl_int_set_si(v, -1);
660 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
661 isl_int_set_si(v, 3);
662 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
663 bset = isl_basic_set_add_constraint(bset, c);
665 c = isl_equality_alloc(isl_local_space_copy(ls));
666 isl_int_set_si(v, -1);
667 isl_constraint_set_constant(c, v);
668 isl_int_set_si(v, 1);
669 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
670 isl_int_set_si(v, 6);
671 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
672 bset = isl_basic_set_add_constraint(bset, c);
674 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
676 assert(isl_basic_set_is_empty(bset));
677 isl_local_space_free(ls);
678 isl_basic_set_free(bset);
680 /* test 5 */
681 dim = isl_space_set_alloc(ctx, 0, 3);
682 bset = isl_basic_set_universe(isl_space_copy(dim));
683 ls = isl_local_space_from_space(dim);
685 c = isl_equality_alloc(isl_local_space_copy(ls));
686 isl_int_set_si(v, -1);
687 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
688 isl_int_set_si(v, 3);
689 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
690 bset = isl_basic_set_add_constraint(bset, c);
692 c = isl_equality_alloc(isl_local_space_copy(ls));
693 isl_int_set_si(v, 1);
694 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
695 isl_int_set_si(v, -3);
696 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
697 bset = isl_basic_set_add_constraint(bset, c);
699 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
701 assert(bset && bset->n_div == 0);
702 isl_basic_set_free(bset);
703 isl_local_space_free(ls);
705 /* test 6 */
706 dim = isl_space_set_alloc(ctx, 0, 3);
707 bset = isl_basic_set_universe(isl_space_copy(dim));
708 ls = isl_local_space_from_space(dim);
710 c = isl_equality_alloc(isl_local_space_copy(ls));
711 isl_int_set_si(v, -1);
712 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
713 isl_int_set_si(v, 6);
714 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
715 bset = isl_basic_set_add_constraint(bset, c);
717 c = isl_equality_alloc(isl_local_space_copy(ls));
718 isl_int_set_si(v, 1);
719 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
720 isl_int_set_si(v, -3);
721 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
722 bset = isl_basic_set_add_constraint(bset, c);
724 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
726 assert(bset && bset->n_div == 1);
727 isl_basic_set_free(bset);
728 isl_local_space_free(ls);
730 /* test 7 */
731 /* This test is a bit tricky. We set up an equality
732 * a + 3b + 3c = 6 e0
733 * Normalization of divs creates _two_ divs
734 * a = 3 e0
735 * c - b - e0 = 2 e1
736 * Afterwards e0 is removed again because it has coefficient -1
737 * and we end up with the original equality and div again.
738 * Perhaps we can avoid the introduction of this temporary div.
740 dim = isl_space_set_alloc(ctx, 0, 4);
741 bset = isl_basic_set_universe(isl_space_copy(dim));
742 ls = isl_local_space_from_space(dim);
744 c = isl_equality_alloc(isl_local_space_copy(ls));
745 isl_int_set_si(v, -1);
746 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
747 isl_int_set_si(v, -3);
748 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
749 isl_int_set_si(v, -3);
750 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
751 isl_int_set_si(v, 6);
752 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
753 bset = isl_basic_set_add_constraint(bset, c);
755 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
757 /* Test disabled for now */
759 assert(bset && bset->n_div == 1);
761 isl_local_space_free(ls);
762 isl_basic_set_free(bset);
764 /* test 8 */
765 dim = isl_space_set_alloc(ctx, 0, 5);
766 bset = isl_basic_set_universe(isl_space_copy(dim));
767 ls = isl_local_space_from_space(dim);
769 c = isl_equality_alloc(isl_local_space_copy(ls));
770 isl_int_set_si(v, -1);
771 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
772 isl_int_set_si(v, -3);
773 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
774 isl_int_set_si(v, -3);
775 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
776 isl_int_set_si(v, 6);
777 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
778 bset = isl_basic_set_add_constraint(bset, c);
780 c = isl_equality_alloc(isl_local_space_copy(ls));
781 isl_int_set_si(v, -1);
782 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
783 isl_int_set_si(v, 1);
784 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
785 isl_int_set_si(v, 1);
786 isl_constraint_set_constant(c, v);
787 bset = isl_basic_set_add_constraint(bset, c);
789 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
791 /* Test disabled for now */
793 assert(bset && bset->n_div == 1);
795 isl_local_space_free(ls);
796 isl_basic_set_free(bset);
798 /* test 9 */
799 dim = isl_space_set_alloc(ctx, 0, 4);
800 bset = isl_basic_set_universe(isl_space_copy(dim));
801 ls = isl_local_space_from_space(dim);
803 c = isl_equality_alloc(isl_local_space_copy(ls));
804 isl_int_set_si(v, 1);
805 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
806 isl_int_set_si(v, -1);
807 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
808 isl_int_set_si(v, -2);
809 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
810 bset = isl_basic_set_add_constraint(bset, c);
812 c = isl_equality_alloc(isl_local_space_copy(ls));
813 isl_int_set_si(v, -1);
814 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
815 isl_int_set_si(v, 3);
816 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
817 isl_int_set_si(v, 2);
818 isl_constraint_set_constant(c, v);
819 bset = isl_basic_set_add_constraint(bset, c);
821 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
823 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
825 assert(!isl_basic_set_is_empty(bset));
827 isl_local_space_free(ls);
828 isl_basic_set_free(bset);
830 /* test 10 */
831 dim = isl_space_set_alloc(ctx, 0, 3);
832 bset = isl_basic_set_universe(isl_space_copy(dim));
833 ls = isl_local_space_from_space(dim);
835 c = isl_equality_alloc(isl_local_space_copy(ls));
836 isl_int_set_si(v, 1);
837 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
838 isl_int_set_si(v, -2);
839 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
840 bset = isl_basic_set_add_constraint(bset, c);
842 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
844 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
846 isl_local_space_free(ls);
847 isl_basic_set_free(bset);
849 isl_int_clear(v);
851 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
852 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
853 set = isl_set_read_from_str(ctx, str);
854 set = isl_set_compute_divs(set);
855 isl_set_free(set);
856 if (!set)
857 return -1;
859 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
860 bset = isl_basic_set_read_from_str(ctx, str);
861 n = isl_basic_set_dim(bset, isl_dim_div);
862 isl_basic_set_free(bset);
863 if (!bset)
864 return -1;
865 if (n != 0)
866 isl_die(ctx, isl_error_unknown,
867 "expecting no existentials", return -1);
869 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
870 set = isl_set_read_from_str(ctx, str);
871 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
872 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
873 empty = isl_set_is_empty(set);
874 isl_set_free(set);
875 if (empty < 0)
876 return -1;
877 if (!empty)
878 isl_die(ctx, isl_error_unknown,
879 "result not as accurate as expected", return -1);
881 return 0;
884 void test_application_case(struct isl_ctx *ctx, const char *name)
886 char *filename;
887 FILE *input;
888 struct isl_basic_set *bset1, *bset2;
889 struct isl_basic_map *bmap;
891 filename = get_filename(ctx, name, "omega");
892 assert(filename);
893 input = fopen(filename, "r");
894 assert(input);
896 bset1 = isl_basic_set_read_from_file(ctx, input);
897 bmap = isl_basic_map_read_from_file(ctx, input);
899 bset1 = isl_basic_set_apply(bset1, bmap);
901 bset2 = isl_basic_set_read_from_file(ctx, input);
903 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
905 isl_basic_set_free(bset1);
906 isl_basic_set_free(bset2);
907 free(filename);
909 fclose(input);
912 void test_application(struct isl_ctx *ctx)
914 test_application_case(ctx, "application");
915 test_application_case(ctx, "application2");
918 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
920 char *filename;
921 FILE *input;
922 struct isl_basic_set *bset1, *bset2;
924 filename = get_filename(ctx, name, "polylib");
925 assert(filename);
926 input = fopen(filename, "r");
927 assert(input);
929 bset1 = isl_basic_set_read_from_file(ctx, input);
930 bset2 = isl_basic_set_read_from_file(ctx, input);
932 bset1 = isl_basic_set_affine_hull(bset1);
934 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
936 isl_basic_set_free(bset1);
937 isl_basic_set_free(bset2);
938 free(filename);
940 fclose(input);
943 int test_affine_hull(struct isl_ctx *ctx)
945 const char *str;
946 isl_set *set;
947 isl_basic_set *bset, *bset2;
948 int n;
949 int subset;
951 test_affine_hull_case(ctx, "affine2");
952 test_affine_hull_case(ctx, "affine");
953 test_affine_hull_case(ctx, "affine3");
955 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
956 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
957 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
958 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
959 set = isl_set_read_from_str(ctx, str);
960 bset = isl_set_affine_hull(set);
961 n = isl_basic_set_dim(bset, isl_dim_div);
962 isl_basic_set_free(bset);
963 if (n != 0)
964 isl_die(ctx, isl_error_unknown, "not expecting any divs",
965 return -1);
967 /* Check that isl_map_affine_hull is not confused by
968 * the reordering of divs in isl_map_align_divs.
970 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
971 "32e0 = b and 32e1 = c); "
972 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
973 set = isl_set_read_from_str(ctx, str);
974 bset = isl_set_affine_hull(set);
975 isl_basic_set_free(bset);
976 if (!bset)
977 return -1;
979 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
980 "32e2 = 31 + 31e0 }";
981 set = isl_set_read_from_str(ctx, str);
982 bset = isl_set_affine_hull(set);
983 str = "{ [a] : exists e : a = 32 e }";
984 bset2 = isl_basic_set_read_from_str(ctx, str);
985 subset = isl_basic_set_is_subset(bset, bset2);
986 isl_basic_set_free(bset);
987 isl_basic_set_free(bset2);
988 if (subset < 0)
989 return -1;
990 if (!subset)
991 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
992 return -1);
994 return 0;
997 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
999 char *filename;
1000 FILE *input;
1001 struct isl_basic_set *bset1, *bset2;
1002 struct isl_set *set;
1004 filename = get_filename(ctx, name, "polylib");
1005 assert(filename);
1006 input = fopen(filename, "r");
1007 assert(input);
1009 bset1 = isl_basic_set_read_from_file(ctx, input);
1010 bset2 = isl_basic_set_read_from_file(ctx, input);
1012 set = isl_basic_set_union(bset1, bset2);
1013 bset1 = isl_set_convex_hull(set);
1015 bset2 = isl_basic_set_read_from_file(ctx, input);
1017 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1019 isl_basic_set_free(bset1);
1020 isl_basic_set_free(bset2);
1021 free(filename);
1023 fclose(input);
1026 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
1028 const char *str1, *str2;
1029 isl_set *set1, *set2;
1030 int orig_convex = ctx->opt->convex;
1031 ctx->opt->convex = convex;
1033 test_convex_hull_case(ctx, "convex0");
1034 test_convex_hull_case(ctx, "convex1");
1035 test_convex_hull_case(ctx, "convex2");
1036 test_convex_hull_case(ctx, "convex3");
1037 test_convex_hull_case(ctx, "convex4");
1038 test_convex_hull_case(ctx, "convex5");
1039 test_convex_hull_case(ctx, "convex6");
1040 test_convex_hull_case(ctx, "convex7");
1041 test_convex_hull_case(ctx, "convex8");
1042 test_convex_hull_case(ctx, "convex9");
1043 test_convex_hull_case(ctx, "convex10");
1044 test_convex_hull_case(ctx, "convex11");
1045 test_convex_hull_case(ctx, "convex12");
1046 test_convex_hull_case(ctx, "convex13");
1047 test_convex_hull_case(ctx, "convex14");
1048 test_convex_hull_case(ctx, "convex15");
1050 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1051 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1052 "(i0 = 0 and i1 = 0 and i2 = 0) }";
1053 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
1054 set1 = isl_set_read_from_str(ctx, str1);
1055 set2 = isl_set_read_from_str(ctx, str2);
1056 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1057 assert(isl_set_is_equal(set1, set2));
1058 isl_set_free(set1);
1059 isl_set_free(set2);
1061 ctx->opt->convex = orig_convex;
1064 void test_convex_hull(struct isl_ctx *ctx)
1066 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
1067 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
1070 void test_gist_case(struct isl_ctx *ctx, const char *name)
1072 char *filename;
1073 FILE *input;
1074 struct isl_basic_set *bset1, *bset2;
1076 filename = get_filename(ctx, name, "polylib");
1077 assert(filename);
1078 input = fopen(filename, "r");
1079 assert(input);
1081 bset1 = isl_basic_set_read_from_file(ctx, input);
1082 bset2 = isl_basic_set_read_from_file(ctx, input);
1084 bset1 = isl_basic_set_gist(bset1, bset2);
1086 bset2 = isl_basic_set_read_from_file(ctx, input);
1088 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1090 isl_basic_set_free(bset1);
1091 isl_basic_set_free(bset2);
1092 free(filename);
1094 fclose(input);
1097 static int test_gist(struct isl_ctx *ctx)
1099 const char *str;
1100 isl_basic_set *bset1, *bset2;
1101 isl_map *map1, *map2;
1103 test_gist_case(ctx, "gist1");
1105 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1106 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1107 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1108 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1109 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1110 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1111 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1112 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1113 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1114 bset1 = isl_basic_set_read_from_str(ctx, str);
1115 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1116 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1117 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1118 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1119 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1120 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1121 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1122 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1123 bset2 = isl_basic_set_read_from_str(ctx, str);
1124 bset1 = isl_basic_set_gist(bset1, bset2);
1125 assert(bset1 && bset1->n_div == 0);
1126 isl_basic_set_free(bset1);
1128 /* Check that the integer divisions of the second disjunct
1129 * do not spread to the first disjunct.
1131 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1132 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1133 "(exists (e0 = [(-1 + t1)/16], "
1134 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1135 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1136 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1137 "o0 <= 4294967295 and t1 <= -1)) }";
1138 map1 = isl_map_read_from_str(ctx, str);
1139 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1140 map2 = isl_map_read_from_str(ctx, str);
1141 map1 = isl_map_gist(map1, map2);
1142 if (!map1)
1143 return -1;
1144 if (map1->n != 1)
1145 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1146 isl_map_free(map1); return -1);
1147 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1148 isl_die(ctx, isl_error_unknown, "expecting single div",
1149 isl_map_free(map1); return -1);
1150 isl_map_free(map1);
1152 return 0;
1155 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1157 isl_set *set, *set2;
1158 int equal;
1159 int one;
1161 set = isl_set_read_from_str(ctx, str);
1162 set = isl_set_coalesce(set);
1163 set2 = isl_set_read_from_str(ctx, str);
1164 equal = isl_set_is_equal(set, set2);
1165 one = set && set->n == 1;
1166 isl_set_free(set);
1167 isl_set_free(set2);
1169 if (equal < 0)
1170 return -1;
1171 if (!equal)
1172 isl_die(ctx, isl_error_unknown,
1173 "coalesced set not equal to input", return -1);
1174 if (check_one && !one)
1175 isl_die(ctx, isl_error_unknown,
1176 "coalesced set should not be a union", return -1);
1178 return 0;
1181 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1183 int r = 0;
1184 int bounded;
1186 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1187 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1189 if (test_coalesce_set(ctx,
1190 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1191 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1192 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1193 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1194 "-10 <= y <= 0}", 1) < 0)
1195 goto error;
1196 if (test_coalesce_set(ctx,
1197 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
1198 goto error;
1199 if (test_coalesce_set(ctx,
1200 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
1201 goto error;
1203 if (0) {
1204 error:
1205 r = -1;
1208 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1210 return r;
1213 /* Inputs for coalescing tests.
1214 * "str" is a string representation of the input set.
1215 * "single_disjunct" is set if we expect the result to consist of
1216 * a single disjunct.
1218 struct {
1219 int single_disjunct;
1220 const char *str;
1221 } coalesce_tests[] = {
1222 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1223 "y >= x & x >= 2 & 5 >= y }" },
1224 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1225 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1226 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1227 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1228 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1229 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1230 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1231 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1232 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1233 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1234 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1235 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1236 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1237 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1238 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1239 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1240 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1241 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1242 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1243 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1244 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1245 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1246 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1247 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1248 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1249 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1250 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1251 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1252 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1253 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1254 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1255 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1256 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1257 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1258 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1259 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1260 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1261 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1262 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1263 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1264 "[o0, o1, o2, o3, o4, o5, o6]] : "
1265 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1266 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1267 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1268 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1269 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1270 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1271 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1272 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1273 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1274 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1275 "o6 >= i3 + i6 - o3 and M >= 0 and "
1276 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1277 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1278 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1279 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1280 "(o0 = 0 and M >= 2 and N >= 3) or "
1281 "(M = 0 and o0 = 0 and N >= 3) }" },
1282 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1283 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1284 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1285 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1286 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1287 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1288 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1289 "(y = 3 and x = 1) }" },
1290 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1291 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1292 "i1 <= M and i3 <= M and i4 <= M) or "
1293 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1294 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1295 "i4 <= -1 + M) }" },
1296 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1297 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1298 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1299 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1300 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1301 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1302 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1303 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1304 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1305 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1306 { 0, "{ [a, b] : exists e : 2e = a and "
1307 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1308 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1309 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1310 "j >= 1 and j' <= i + j - i' and i >= 1; "
1311 "[1, 1, 1, 1] }" },
1312 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1313 "[i,j] : exists a : j = 3a }" },
1314 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1315 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1316 "a >= 3) or "
1317 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1318 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1319 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1320 "c <= 6 + 8a and a >= 3; "
1321 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1322 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1323 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1324 "[x,0] : 3 <= x <= 4 }" },
1325 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1326 "[x,0] : 4 <= x <= 5 }" },
1327 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1328 "[x,0] : 3 <= x <= 5 }" },
1329 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1330 "[x,0] : 3 <= x <= 4 }" },
1331 { 1 , "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1332 "i1 <= 0; "
1333 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1336 /* Test the functionality of isl_set_coalesce.
1337 * That is, check that the output is always equal to the input
1338 * and in some cases that the result consists of a single disjunct.
1340 static int test_coalesce(struct isl_ctx *ctx)
1342 int i;
1344 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1345 const char *str = coalesce_tests[i].str;
1346 int check_one = coalesce_tests[i].single_disjunct;
1347 if (test_coalesce_set(ctx, str, check_one) < 0)
1348 return -1;
1351 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1352 return -1;
1354 return 0;
1357 void test_closure(struct isl_ctx *ctx)
1359 const char *str;
1360 isl_set *dom;
1361 isl_map *up, *right;
1362 isl_map *map, *map2;
1363 int exact;
1365 /* COCOA example 1 */
1366 map = isl_map_read_from_str(ctx,
1367 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1368 "1 <= i and i < n and 1 <= j and j < n or "
1369 "i2 = i + 1 and j2 = j - 1 and "
1370 "1 <= i and i < n and 2 <= j and j <= n }");
1371 map = isl_map_power(map, &exact);
1372 assert(exact);
1373 isl_map_free(map);
1375 /* COCOA example 1 */
1376 map = isl_map_read_from_str(ctx,
1377 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1378 "1 <= i and i < n and 1 <= j and j < n or "
1379 "i2 = i + 1 and j2 = j - 1 and "
1380 "1 <= i and i < n and 2 <= j and j <= n }");
1381 map = isl_map_transitive_closure(map, &exact);
1382 assert(exact);
1383 map2 = isl_map_read_from_str(ctx,
1384 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1385 "1 <= i and i < n and 1 <= j and j <= n and "
1386 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1387 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1388 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1389 assert(isl_map_is_equal(map, map2));
1390 isl_map_free(map2);
1391 isl_map_free(map);
1393 map = isl_map_read_from_str(ctx,
1394 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1395 " 0 <= y and y <= n }");
1396 map = isl_map_transitive_closure(map, &exact);
1397 map2 = isl_map_read_from_str(ctx,
1398 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1399 " 0 <= y and y <= n }");
1400 assert(isl_map_is_equal(map, map2));
1401 isl_map_free(map2);
1402 isl_map_free(map);
1404 /* COCOA example 2 */
1405 map = isl_map_read_from_str(ctx,
1406 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1407 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1408 "i2 = i + 2 and j2 = j - 2 and "
1409 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1410 map = isl_map_transitive_closure(map, &exact);
1411 assert(exact);
1412 map2 = isl_map_read_from_str(ctx,
1413 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1414 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1415 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1416 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1417 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1418 assert(isl_map_is_equal(map, map2));
1419 isl_map_free(map);
1420 isl_map_free(map2);
1422 /* COCOA Fig.2 left */
1423 map = isl_map_read_from_str(ctx,
1424 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1425 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1426 "j <= n or "
1427 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1428 "j <= 2 i - 3 and j <= n - 2 or "
1429 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1430 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1431 map = isl_map_transitive_closure(map, &exact);
1432 assert(exact);
1433 isl_map_free(map);
1435 /* COCOA Fig.2 right */
1436 map = isl_map_read_from_str(ctx,
1437 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1438 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1439 "j <= n or "
1440 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1441 "j <= 2 i - 4 and j <= n - 3 or "
1442 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1443 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1444 map = isl_map_power(map, &exact);
1445 assert(exact);
1446 isl_map_free(map);
1448 /* COCOA Fig.2 right */
1449 map = isl_map_read_from_str(ctx,
1450 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1451 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1452 "j <= n or "
1453 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1454 "j <= 2 i - 4 and j <= n - 3 or "
1455 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1456 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1457 map = isl_map_transitive_closure(map, &exact);
1458 assert(exact);
1459 map2 = isl_map_read_from_str(ctx,
1460 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1461 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1462 "j <= n and 3 + i + 2 j <= 3 n and "
1463 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1464 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1465 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1466 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1467 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1468 assert(isl_map_is_equal(map, map2));
1469 isl_map_free(map2);
1470 isl_map_free(map);
1472 /* COCOA Fig.1 right */
1473 dom = isl_set_read_from_str(ctx,
1474 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1475 "2 x - 3 y + 3 >= 0 }");
1476 right = isl_map_read_from_str(ctx,
1477 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1478 up = isl_map_read_from_str(ctx,
1479 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1480 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1481 right = isl_map_intersect_range(right, isl_set_copy(dom));
1482 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1483 up = isl_map_intersect_range(up, dom);
1484 map = isl_map_union(up, right);
1485 map = isl_map_transitive_closure(map, &exact);
1486 assert(exact);
1487 map2 = isl_map_read_from_str(ctx,
1488 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1489 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1490 assert(isl_map_is_equal(map, map2));
1491 isl_map_free(map2);
1492 isl_map_free(map);
1494 /* COCOA Theorem 1 counter example */
1495 map = isl_map_read_from_str(ctx,
1496 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1497 "i2 = 1 and j2 = j or "
1498 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1499 map = isl_map_transitive_closure(map, &exact);
1500 assert(exact);
1501 isl_map_free(map);
1503 map = isl_map_read_from_str(ctx,
1504 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1505 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1506 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1507 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1508 map = isl_map_transitive_closure(map, &exact);
1509 assert(exact);
1510 isl_map_free(map);
1512 /* Kelly et al 1996, fig 12 */
1513 map = isl_map_read_from_str(ctx,
1514 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1515 "1 <= i,j,j+1 <= n or "
1516 "j = n and j2 = 1 and i2 = i + 1 and "
1517 "1 <= i,i+1 <= n }");
1518 map = isl_map_transitive_closure(map, &exact);
1519 assert(exact);
1520 map2 = isl_map_read_from_str(ctx,
1521 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1522 "1 <= i <= n and i = i2 or "
1523 "1 <= i < i2 <= n and 1 <= j <= n and "
1524 "1 <= j2 <= n }");
1525 assert(isl_map_is_equal(map, map2));
1526 isl_map_free(map2);
1527 isl_map_free(map);
1529 /* Omega's closure4 */
1530 map = isl_map_read_from_str(ctx,
1531 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1532 "1 <= x,y <= 10 or "
1533 "x2 = x + 1 and y2 = y and "
1534 "1 <= x <= 20 && 5 <= y <= 15 }");
1535 map = isl_map_transitive_closure(map, &exact);
1536 assert(exact);
1537 isl_map_free(map);
1539 map = isl_map_read_from_str(ctx,
1540 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1541 map = isl_map_transitive_closure(map, &exact);
1542 assert(!exact);
1543 map2 = isl_map_read_from_str(ctx,
1544 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1545 assert(isl_map_is_equal(map, map2));
1546 isl_map_free(map);
1547 isl_map_free(map2);
1549 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1550 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1551 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1552 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1553 map = isl_map_read_from_str(ctx, str);
1554 map = isl_map_transitive_closure(map, &exact);
1555 assert(exact);
1556 map2 = isl_map_read_from_str(ctx, str);
1557 assert(isl_map_is_equal(map, map2));
1558 isl_map_free(map);
1559 isl_map_free(map2);
1561 str = "{[0] -> [1]; [2] -> [3]}";
1562 map = isl_map_read_from_str(ctx, str);
1563 map = isl_map_transitive_closure(map, &exact);
1564 assert(exact);
1565 map2 = isl_map_read_from_str(ctx, str);
1566 assert(isl_map_is_equal(map, map2));
1567 isl_map_free(map);
1568 isl_map_free(map2);
1570 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1571 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1572 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1573 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1574 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1575 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1576 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1577 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1578 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1579 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1580 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1581 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1582 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1583 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1584 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1585 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1586 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1587 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1588 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1589 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1590 map = isl_map_read_from_str(ctx, str);
1591 map = isl_map_transitive_closure(map, NULL);
1592 assert(map);
1593 isl_map_free(map);
1596 void test_lex(struct isl_ctx *ctx)
1598 isl_space *dim;
1599 isl_map *map;
1601 dim = isl_space_set_alloc(ctx, 0, 0);
1602 map = isl_map_lex_le(dim);
1603 assert(!isl_map_is_empty(map));
1604 isl_map_free(map);
1607 static int test_lexmin(struct isl_ctx *ctx)
1609 int equal;
1610 const char *str;
1611 isl_basic_map *bmap;
1612 isl_map *map, *map2;
1613 isl_set *set;
1614 isl_set *set2;
1615 isl_pw_multi_aff *pma;
1617 str = "[p0, p1] -> { [] -> [] : "
1618 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1619 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1620 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1621 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1622 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1623 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1624 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1625 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1626 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1627 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1628 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1629 map = isl_map_read_from_str(ctx, str);
1630 map = isl_map_lexmin(map);
1631 isl_map_free(map);
1633 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1634 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1635 set = isl_set_read_from_str(ctx, str);
1636 set = isl_set_lexmax(set);
1637 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1638 set2 = isl_set_read_from_str(ctx, str);
1639 set = isl_set_intersect(set, set2);
1640 assert(!isl_set_is_empty(set));
1641 isl_set_free(set);
1643 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1644 map = isl_map_read_from_str(ctx, str);
1645 map = isl_map_lexmin(map);
1646 str = "{ [x] -> [5] : 6 <= x <= 8; "
1647 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1648 map2 = isl_map_read_from_str(ctx, str);
1649 assert(isl_map_is_equal(map, map2));
1650 isl_map_free(map);
1651 isl_map_free(map2);
1653 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1654 map = isl_map_read_from_str(ctx, str);
1655 map2 = isl_map_copy(map);
1656 map = isl_map_lexmin(map);
1657 assert(isl_map_is_equal(map, map2));
1658 isl_map_free(map);
1659 isl_map_free(map2);
1661 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1662 map = isl_map_read_from_str(ctx, str);
1663 map = isl_map_lexmin(map);
1664 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1665 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1666 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1667 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1668 map2 = isl_map_read_from_str(ctx, str);
1669 assert(isl_map_is_equal(map, map2));
1670 isl_map_free(map);
1671 isl_map_free(map2);
1673 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1674 " 8i' <= i and 8i' >= -7 + i }";
1675 bmap = isl_basic_map_read_from_str(ctx, str);
1676 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1677 map2 = isl_map_from_pw_multi_aff(pma);
1678 map = isl_map_from_basic_map(bmap);
1679 assert(isl_map_is_equal(map, map2));
1680 isl_map_free(map);
1681 isl_map_free(map2);
1683 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1684 map = isl_map_read_from_str(ctx, str);
1685 map = isl_map_lexmin(map);
1686 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1687 map2 = isl_map_read_from_str(ctx, str);
1688 assert(isl_map_is_equal(map, map2));
1689 isl_map_free(map);
1690 isl_map_free(map2);
1692 /* Check that empty pieces are properly combined. */
1693 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1694 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1695 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1696 map = isl_map_read_from_str(ctx, str);
1697 map = isl_map_lexmin(map);
1698 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1699 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1700 "x >= 4 }";
1701 map2 = isl_map_read_from_str(ctx, str);
1702 assert(isl_map_is_equal(map, map2));
1703 isl_map_free(map);
1704 isl_map_free(map2);
1706 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1707 " 8i' <= i and 8i' >= -7 + i }";
1708 set = isl_set_read_from_str(ctx, str);
1709 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1710 set2 = isl_set_from_pw_multi_aff(pma);
1711 equal = isl_set_is_equal(set, set2);
1712 isl_set_free(set);
1713 isl_set_free(set2);
1714 if (equal < 0)
1715 return -1;
1716 if (!equal)
1717 isl_die(ctx, isl_error_unknown,
1718 "unexpected difference between set and "
1719 "piecewise affine expression", return -1);
1721 return 0;
1724 /* Check that isl_set_min_val and isl_set_max_val compute the correct
1725 * result on non-convex inputs.
1727 static int test_min(struct isl_ctx *ctx)
1729 isl_set *set;
1730 isl_aff *aff;
1731 isl_val *val;
1732 int min_ok, max_ok;
1734 set = isl_set_read_from_str(ctx, "{ [-1]; [1] }");
1735 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x] }");
1736 val = isl_set_min_val(set, aff);
1737 min_ok = isl_val_is_negone(val);
1738 isl_val_free(val);
1739 val = isl_set_max_val(set, aff);
1740 max_ok = isl_val_is_one(val);
1741 isl_val_free(val);
1742 isl_aff_free(aff);
1743 isl_set_free(set);
1745 if (min_ok < 0 || max_ok < 0)
1746 return -1;
1747 if (!min_ok)
1748 isl_die(ctx, isl_error_unknown,
1749 "unexpected minimum", return -1);
1750 if (!max_ok)
1751 isl_die(ctx, isl_error_unknown,
1752 "unexpected maximum", return -1);
1754 return 0;
1757 struct must_may {
1758 isl_map *must;
1759 isl_map *may;
1762 static int collect_must_may(__isl_take isl_map *dep, int must,
1763 void *dep_user, void *user)
1765 struct must_may *mm = (struct must_may *)user;
1767 if (must)
1768 mm->must = isl_map_union(mm->must, dep);
1769 else
1770 mm->may = isl_map_union(mm->may, dep);
1772 return 0;
1775 static int common_space(void *first, void *second)
1777 int depth = *(int *)first;
1778 return 2 * depth;
1781 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1783 isl_map *map2;
1784 int equal;
1786 if (!map)
1787 return -1;
1789 map2 = isl_map_read_from_str(map->ctx, str);
1790 equal = isl_map_is_equal(map, map2);
1791 isl_map_free(map2);
1793 return equal;
1796 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1798 int equal;
1800 equal = map_is_equal(map, str);
1801 if (equal < 0)
1802 return -1;
1803 if (!equal)
1804 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1805 "result not as expected", return -1);
1806 return 0;
1809 void test_dep(struct isl_ctx *ctx)
1811 const char *str;
1812 isl_space *dim;
1813 isl_map *map;
1814 isl_access_info *ai;
1815 isl_flow *flow;
1816 int depth;
1817 struct must_may mm;
1819 depth = 3;
1821 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1822 map = isl_map_read_from_str(ctx, str);
1823 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1825 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1826 map = isl_map_read_from_str(ctx, str);
1827 ai = isl_access_info_add_source(ai, map, 1, &depth);
1829 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1830 map = isl_map_read_from_str(ctx, str);
1831 ai = isl_access_info_add_source(ai, map, 1, &depth);
1833 flow = isl_access_info_compute_flow(ai);
1834 dim = isl_space_alloc(ctx, 0, 3, 3);
1835 mm.must = isl_map_empty(isl_space_copy(dim));
1836 mm.may = isl_map_empty(dim);
1838 isl_flow_foreach(flow, collect_must_may, &mm);
1840 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1841 " [1,10,0] -> [2,5,0] }";
1842 assert(map_is_equal(mm.must, str));
1843 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1844 assert(map_is_equal(mm.may, str));
1846 isl_map_free(mm.must);
1847 isl_map_free(mm.may);
1848 isl_flow_free(flow);
1851 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1852 map = isl_map_read_from_str(ctx, str);
1853 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1855 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1856 map = isl_map_read_from_str(ctx, str);
1857 ai = isl_access_info_add_source(ai, map, 1, &depth);
1859 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1860 map = isl_map_read_from_str(ctx, str);
1861 ai = isl_access_info_add_source(ai, map, 0, &depth);
1863 flow = isl_access_info_compute_flow(ai);
1864 dim = isl_space_alloc(ctx, 0, 3, 3);
1865 mm.must = isl_map_empty(isl_space_copy(dim));
1866 mm.may = isl_map_empty(dim);
1868 isl_flow_foreach(flow, collect_must_may, &mm);
1870 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1871 assert(map_is_equal(mm.must, str));
1872 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1873 assert(map_is_equal(mm.may, str));
1875 isl_map_free(mm.must);
1876 isl_map_free(mm.may);
1877 isl_flow_free(flow);
1880 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1881 map = isl_map_read_from_str(ctx, str);
1882 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1884 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1885 map = isl_map_read_from_str(ctx, str);
1886 ai = isl_access_info_add_source(ai, map, 0, &depth);
1888 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1889 map = isl_map_read_from_str(ctx, str);
1890 ai = isl_access_info_add_source(ai, map, 0, &depth);
1892 flow = isl_access_info_compute_flow(ai);
1893 dim = isl_space_alloc(ctx, 0, 3, 3);
1894 mm.must = isl_map_empty(isl_space_copy(dim));
1895 mm.may = isl_map_empty(dim);
1897 isl_flow_foreach(flow, collect_must_may, &mm);
1899 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1900 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1901 assert(map_is_equal(mm.may, str));
1902 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1903 assert(map_is_equal(mm.must, str));
1905 isl_map_free(mm.must);
1906 isl_map_free(mm.may);
1907 isl_flow_free(flow);
1910 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1911 map = isl_map_read_from_str(ctx, str);
1912 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1914 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1915 map = isl_map_read_from_str(ctx, str);
1916 ai = isl_access_info_add_source(ai, map, 0, &depth);
1918 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1919 map = isl_map_read_from_str(ctx, str);
1920 ai = isl_access_info_add_source(ai, map, 0, &depth);
1922 flow = isl_access_info_compute_flow(ai);
1923 dim = isl_space_alloc(ctx, 0, 3, 3);
1924 mm.must = isl_map_empty(isl_space_copy(dim));
1925 mm.may = isl_map_empty(dim);
1927 isl_flow_foreach(flow, collect_must_may, &mm);
1929 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1930 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1931 assert(map_is_equal(mm.may, str));
1932 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1933 assert(map_is_equal(mm.must, str));
1935 isl_map_free(mm.must);
1936 isl_map_free(mm.may);
1937 isl_flow_free(flow);
1940 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1941 map = isl_map_read_from_str(ctx, str);
1942 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1944 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1945 map = isl_map_read_from_str(ctx, str);
1946 ai = isl_access_info_add_source(ai, map, 0, &depth);
1948 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1949 map = isl_map_read_from_str(ctx, str);
1950 ai = isl_access_info_add_source(ai, map, 0, &depth);
1952 flow = isl_access_info_compute_flow(ai);
1953 dim = isl_space_alloc(ctx, 0, 3, 3);
1954 mm.must = isl_map_empty(isl_space_copy(dim));
1955 mm.may = isl_map_empty(dim);
1957 isl_flow_foreach(flow, collect_must_may, &mm);
1959 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1960 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1961 assert(map_is_equal(mm.may, str));
1962 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1963 assert(map_is_equal(mm.must, str));
1965 isl_map_free(mm.must);
1966 isl_map_free(mm.may);
1967 isl_flow_free(flow);
1970 depth = 5;
1972 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1973 map = isl_map_read_from_str(ctx, str);
1974 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1976 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1977 map = isl_map_read_from_str(ctx, str);
1978 ai = isl_access_info_add_source(ai, map, 1, &depth);
1980 flow = isl_access_info_compute_flow(ai);
1981 dim = isl_space_alloc(ctx, 0, 5, 5);
1982 mm.must = isl_map_empty(isl_space_copy(dim));
1983 mm.may = isl_map_empty(dim);
1985 isl_flow_foreach(flow, collect_must_may, &mm);
1987 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1988 assert(map_is_equal(mm.must, str));
1989 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1990 assert(map_is_equal(mm.may, str));
1992 isl_map_free(mm.must);
1993 isl_map_free(mm.may);
1994 isl_flow_free(flow);
1997 int test_sv(isl_ctx *ctx)
1999 const char *str;
2000 isl_map *map;
2001 isl_union_map *umap;
2002 int sv;
2004 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
2005 map = isl_map_read_from_str(ctx, str);
2006 sv = isl_map_is_single_valued(map);
2007 isl_map_free(map);
2008 if (sv < 0)
2009 return -1;
2010 if (!sv)
2011 isl_die(ctx, isl_error_internal,
2012 "map not detected as single valued", return -1);
2014 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
2015 map = isl_map_read_from_str(ctx, str);
2016 sv = isl_map_is_single_valued(map);
2017 isl_map_free(map);
2018 if (sv < 0)
2019 return -1;
2020 if (sv)
2021 isl_die(ctx, isl_error_internal,
2022 "map detected as single valued", return -1);
2024 str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
2025 umap = isl_union_map_read_from_str(ctx, str);
2026 sv = isl_union_map_is_single_valued(umap);
2027 isl_union_map_free(umap);
2028 if (sv < 0)
2029 return -1;
2030 if (!sv)
2031 isl_die(ctx, isl_error_internal,
2032 "map not detected as single valued", return -1);
2034 str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
2035 umap = isl_union_map_read_from_str(ctx, str);
2036 sv = isl_union_map_is_single_valued(umap);
2037 isl_union_map_free(umap);
2038 if (sv < 0)
2039 return -1;
2040 if (sv)
2041 isl_die(ctx, isl_error_internal,
2042 "map detected as single valued", return -1);
2044 return 0;
2047 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
2049 isl_map *map;
2051 map = isl_map_read_from_str(ctx, str);
2052 if (bijective)
2053 assert(isl_map_is_bijective(map));
2054 else
2055 assert(!isl_map_is_bijective(map));
2056 isl_map_free(map);
2059 void test_bijective(struct isl_ctx *ctx)
2061 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
2062 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
2063 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
2064 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
2065 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
2066 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
2067 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
2068 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2069 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2070 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2071 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2072 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2073 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2076 static int test_pwqp(struct isl_ctx *ctx)
2078 const char *str;
2079 isl_set *set;
2080 isl_pw_qpolynomial *pwqp1, *pwqp2;
2081 int equal;
2083 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2084 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2086 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2087 isl_dim_in, 1, 1);
2089 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2090 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2092 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2094 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2096 isl_pw_qpolynomial_free(pwqp1);
2098 str = "{ [i] -> i }";
2099 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2100 str = "{ [k] : exists a : k = 2a }";
2101 set = isl_set_read_from_str(ctx, str);
2102 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2103 str = "{ [i] -> i }";
2104 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2106 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2108 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2110 isl_pw_qpolynomial_free(pwqp1);
2112 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
2113 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2114 str = "{ [10] }";
2115 set = isl_set_read_from_str(ctx, str);
2116 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2117 str = "{ [i] -> 16 }";
2118 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2120 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2122 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2124 isl_pw_qpolynomial_free(pwqp1);
2126 str = "{ [i] -> ([(i)/2]) }";
2127 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2128 str = "{ [k] : exists a : k = 2a+1 }";
2129 set = isl_set_read_from_str(ctx, str);
2130 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2131 str = "{ [i] -> -1/2 + 1/2 * i }";
2132 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2134 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2136 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2138 isl_pw_qpolynomial_free(pwqp1);
2140 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2141 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2142 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2143 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2145 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2147 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2149 isl_pw_qpolynomial_free(pwqp1);
2151 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2152 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2153 str = "{ [x] -> x }";
2154 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2156 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2158 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2160 isl_pw_qpolynomial_free(pwqp1);
2162 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2163 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2164 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2165 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2166 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2167 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2168 isl_pw_qpolynomial_free(pwqp1);
2170 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2171 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2172 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2173 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2174 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2175 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2176 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2177 isl_pw_qpolynomial_free(pwqp1);
2178 isl_pw_qpolynomial_free(pwqp2);
2179 if (equal < 0)
2180 return -1;
2181 if (!equal)
2182 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2184 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2185 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2186 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2187 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2188 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2189 isl_val_one(ctx));
2190 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2191 isl_pw_qpolynomial_free(pwqp1);
2192 isl_pw_qpolynomial_free(pwqp2);
2193 if (equal < 0)
2194 return -1;
2195 if (!equal)
2196 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2198 return 0;
2201 void test_split_periods(isl_ctx *ctx)
2203 const char *str;
2204 isl_pw_qpolynomial *pwqp;
2206 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2207 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2208 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2209 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2211 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2212 assert(pwqp);
2214 isl_pw_qpolynomial_free(pwqp);
2217 void test_union(isl_ctx *ctx)
2219 const char *str;
2220 isl_union_set *uset1, *uset2;
2221 isl_union_map *umap1, *umap2;
2223 str = "{ [i] : 0 <= i <= 1 }";
2224 uset1 = isl_union_set_read_from_str(ctx, str);
2225 str = "{ [1] -> [0] }";
2226 umap1 = isl_union_map_read_from_str(ctx, str);
2228 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2229 assert(isl_union_map_is_equal(umap1, umap2));
2231 isl_union_map_free(umap1);
2232 isl_union_map_free(umap2);
2234 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2235 umap1 = isl_union_map_read_from_str(ctx, str);
2236 str = "{ A[i]; B[i] }";
2237 uset1 = isl_union_set_read_from_str(ctx, str);
2239 uset2 = isl_union_map_domain(umap1);
2241 assert(isl_union_set_is_equal(uset1, uset2));
2243 isl_union_set_free(uset1);
2244 isl_union_set_free(uset2);
2247 void test_bound(isl_ctx *ctx)
2249 const char *str;
2250 isl_pw_qpolynomial *pwqp;
2251 isl_pw_qpolynomial_fold *pwf;
2253 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2254 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2255 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2256 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2257 isl_pw_qpolynomial_fold_free(pwf);
2259 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2260 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2261 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2262 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2263 isl_pw_qpolynomial_fold_free(pwf);
2266 void test_lift(isl_ctx *ctx)
2268 const char *str;
2269 isl_basic_map *bmap;
2270 isl_basic_set *bset;
2272 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2273 bset = isl_basic_set_read_from_str(ctx, str);
2274 bset = isl_basic_set_lift(bset);
2275 bmap = isl_basic_map_from_range(bset);
2276 bset = isl_basic_map_domain(bmap);
2277 isl_basic_set_free(bset);
2280 struct {
2281 const char *set1;
2282 const char *set2;
2283 int subset;
2284 } subset_tests[] = {
2285 { "{ [112, 0] }",
2286 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2287 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2288 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2289 { "{ [65] }",
2290 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2291 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2292 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2293 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2294 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2295 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2296 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2297 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2298 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2299 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2300 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2301 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2302 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2305 static int test_subset(isl_ctx *ctx)
2307 int i;
2308 isl_set *set1, *set2;
2309 int subset;
2311 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2312 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2313 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2314 subset = isl_set_is_subset(set1, set2);
2315 isl_set_free(set1);
2316 isl_set_free(set2);
2317 if (subset < 0)
2318 return -1;
2319 if (subset != subset_tests[i].subset)
2320 isl_die(ctx, isl_error_unknown,
2321 "incorrect subset result", return -1);
2324 return 0;
2327 struct {
2328 const char *minuend;
2329 const char *subtrahend;
2330 const char *difference;
2331 } subtract_domain_tests[] = {
2332 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2333 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2334 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2337 static int test_subtract(isl_ctx *ctx)
2339 int i;
2340 isl_union_map *umap1, *umap2;
2341 isl_union_set *uset;
2342 int equal;
2344 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2345 umap1 = isl_union_map_read_from_str(ctx,
2346 subtract_domain_tests[i].minuend);
2347 uset = isl_union_set_read_from_str(ctx,
2348 subtract_domain_tests[i].subtrahend);
2349 umap2 = isl_union_map_read_from_str(ctx,
2350 subtract_domain_tests[i].difference);
2351 umap1 = isl_union_map_subtract_domain(umap1, uset);
2352 equal = isl_union_map_is_equal(umap1, umap2);
2353 isl_union_map_free(umap1);
2354 isl_union_map_free(umap2);
2355 if (equal < 0)
2356 return -1;
2357 if (!equal)
2358 isl_die(ctx, isl_error_unknown,
2359 "incorrect subtract domain result", return -1);
2362 return 0;
2365 int test_factorize(isl_ctx *ctx)
2367 const char *str;
2368 isl_basic_set *bset;
2369 isl_factorizer *f;
2371 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2372 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2373 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2374 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2375 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2376 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2377 "3i5 >= -2i0 - i2 + 3i4 }";
2378 bset = isl_basic_set_read_from_str(ctx, str);
2379 f = isl_basic_set_factorizer(bset);
2380 isl_basic_set_free(bset);
2381 isl_factorizer_free(f);
2382 if (!f)
2383 isl_die(ctx, isl_error_unknown,
2384 "failed to construct factorizer", return -1);
2386 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2387 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2388 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2389 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2390 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2391 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2392 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2393 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2394 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2395 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2396 bset = isl_basic_set_read_from_str(ctx, str);
2397 f = isl_basic_set_factorizer(bset);
2398 isl_basic_set_free(bset);
2399 isl_factorizer_free(f);
2400 if (!f)
2401 isl_die(ctx, isl_error_unknown,
2402 "failed to construct factorizer", return -1);
2404 return 0;
2407 static int check_injective(__isl_take isl_map *map, void *user)
2409 int *injective = user;
2411 *injective = isl_map_is_injective(map);
2412 isl_map_free(map);
2414 if (*injective < 0 || !*injective)
2415 return -1;
2417 return 0;
2420 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2421 const char *r, const char *s, int tilable, int parallel)
2423 int i;
2424 isl_union_set *D;
2425 isl_union_map *W, *R, *S;
2426 isl_union_map *empty;
2427 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2428 isl_union_map *validity, *proximity;
2429 isl_union_map *schedule;
2430 isl_union_map *test;
2431 isl_union_set *delta;
2432 isl_union_set *domain;
2433 isl_set *delta_set;
2434 isl_set *slice;
2435 isl_set *origin;
2436 isl_schedule *sched;
2437 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2439 D = isl_union_set_read_from_str(ctx, d);
2440 W = isl_union_map_read_from_str(ctx, w);
2441 R = isl_union_map_read_from_str(ctx, r);
2442 S = isl_union_map_read_from_str(ctx, s);
2444 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2445 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2447 empty = isl_union_map_empty(isl_union_map_get_space(S));
2448 isl_union_map_compute_flow(isl_union_map_copy(R),
2449 isl_union_map_copy(W), empty,
2450 isl_union_map_copy(S),
2451 &dep_raw, NULL, NULL, NULL);
2452 isl_union_map_compute_flow(isl_union_map_copy(W),
2453 isl_union_map_copy(W),
2454 isl_union_map_copy(R),
2455 isl_union_map_copy(S),
2456 &dep_waw, &dep_war, NULL, NULL);
2458 dep = isl_union_map_union(dep_waw, dep_war);
2459 dep = isl_union_map_union(dep, dep_raw);
2460 validity = isl_union_map_copy(dep);
2461 proximity = isl_union_map_copy(dep);
2463 sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
2464 validity, proximity);
2465 schedule = isl_schedule_get_map(sched);
2466 isl_schedule_free(sched);
2467 isl_union_map_free(W);
2468 isl_union_map_free(R);
2469 isl_union_map_free(S);
2471 is_injection = 1;
2472 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2474 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2475 is_complete = isl_union_set_is_subset(D, domain);
2476 isl_union_set_free(D);
2477 isl_union_set_free(domain);
2479 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2480 test = isl_union_map_apply_range(test, dep);
2481 test = isl_union_map_apply_range(test, schedule);
2483 delta = isl_union_map_deltas(test);
2484 if (isl_union_set_n_set(delta) == 0) {
2485 is_tilable = 1;
2486 is_parallel = 1;
2487 is_nonneg = 1;
2488 isl_union_set_free(delta);
2489 } else {
2490 delta_set = isl_set_from_union_set(delta);
2492 slice = isl_set_universe(isl_set_get_space(delta_set));
2493 for (i = 0; i < tilable; ++i)
2494 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2495 is_tilable = isl_set_is_subset(delta_set, slice);
2496 isl_set_free(slice);
2498 slice = isl_set_universe(isl_set_get_space(delta_set));
2499 for (i = 0; i < parallel; ++i)
2500 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2501 is_parallel = isl_set_is_subset(delta_set, slice);
2502 isl_set_free(slice);
2504 origin = isl_set_universe(isl_set_get_space(delta_set));
2505 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2506 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2508 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2509 delta_set = isl_set_lexmin(delta_set);
2511 is_nonneg = isl_set_is_equal(delta_set, origin);
2513 isl_set_free(origin);
2514 isl_set_free(delta_set);
2517 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2518 is_injection < 0 || is_complete < 0)
2519 return -1;
2520 if (!is_complete)
2521 isl_die(ctx, isl_error_unknown,
2522 "generated schedule incomplete", return -1);
2523 if (!is_injection)
2524 isl_die(ctx, isl_error_unknown,
2525 "generated schedule not injective on each statement",
2526 return -1);
2527 if (!is_nonneg)
2528 isl_die(ctx, isl_error_unknown,
2529 "negative dependences in generated schedule",
2530 return -1);
2531 if (!is_tilable)
2532 isl_die(ctx, isl_error_unknown,
2533 "generated schedule not as tilable as expected",
2534 return -1);
2535 if (!is_parallel)
2536 isl_die(ctx, isl_error_unknown,
2537 "generated schedule not as parallel as expected",
2538 return -1);
2540 return 0;
2543 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2544 const char *domain, const char *validity, const char *proximity)
2546 isl_union_set *dom;
2547 isl_union_map *dep;
2548 isl_union_map *prox;
2549 isl_schedule *schedule;
2550 isl_union_map *sched;
2552 dom = isl_union_set_read_from_str(ctx, domain);
2553 dep = isl_union_map_read_from_str(ctx, validity);
2554 prox = isl_union_map_read_from_str(ctx, proximity);
2555 schedule = isl_union_set_compute_schedule(dom, dep, prox);
2556 sched = isl_schedule_get_map(schedule);
2557 isl_schedule_free(schedule);
2559 return sched;
2562 /* Check that a schedule can be constructed on the given domain
2563 * with the given validity and proximity constraints.
2565 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2566 const char *validity, const char *proximity)
2568 isl_union_map *sched;
2570 sched = compute_schedule(ctx, domain, validity, proximity);
2571 if (!sched)
2572 return -1;
2574 isl_union_map_free(sched);
2575 return 0;
2578 int test_special_schedule(isl_ctx *ctx, const char *domain,
2579 const char *validity, const char *proximity, const char *expected_sched)
2581 isl_union_map *sched1, *sched2;
2582 int equal;
2584 sched1 = compute_schedule(ctx, domain, validity, proximity);
2585 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2587 equal = isl_union_map_is_equal(sched1, sched2);
2588 isl_union_map_free(sched1);
2589 isl_union_map_free(sched2);
2591 if (equal < 0)
2592 return -1;
2593 if (!equal)
2594 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2595 return -1);
2597 return 0;
2600 /* Check that the schedule map is properly padded, even after being
2601 * reconstructed from the band forest.
2603 static int test_padded_schedule(isl_ctx *ctx)
2605 const char *str;
2606 isl_union_set *D;
2607 isl_union_map *validity, *proximity;
2608 isl_schedule *sched;
2609 isl_union_map *map1, *map2;
2610 isl_band_list *list;
2611 int equal;
2613 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2614 D = isl_union_set_read_from_str(ctx, str);
2615 validity = isl_union_map_empty(isl_union_set_get_space(D));
2616 proximity = isl_union_map_copy(validity);
2617 sched = isl_union_set_compute_schedule(D, validity, proximity);
2618 map1 = isl_schedule_get_map(sched);
2619 list = isl_schedule_get_band_forest(sched);
2620 isl_band_list_free(list);
2621 map2 = isl_schedule_get_map(sched);
2622 isl_schedule_free(sched);
2623 equal = isl_union_map_is_equal(map1, map2);
2624 isl_union_map_free(map1);
2625 isl_union_map_free(map2);
2627 if (equal < 0)
2628 return -1;
2629 if (!equal)
2630 isl_die(ctx, isl_error_unknown,
2631 "reconstructed schedule map not the same as original",
2632 return -1);
2634 return 0;
2637 int test_schedule(isl_ctx *ctx)
2639 const char *D, *W, *R, *V, *P, *S;
2641 /* Handle resulting schedule with zero bands. */
2642 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2643 return -1;
2645 /* Jacobi */
2646 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2647 W = "{ S1[t,i] -> a[t,i] }";
2648 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2649 "S1[t,i] -> a[t-1,i+1] }";
2650 S = "{ S1[t,i] -> [t,i] }";
2651 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2652 return -1;
2654 /* Fig. 5 of CC2008 */
2655 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2656 "j <= -1 + N }";
2657 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2658 "j >= 2 and j <= -1 + N }";
2659 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2660 "j >= 2 and j <= -1 + N; "
2661 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2662 "j >= 2 and j <= -1 + N }";
2663 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2664 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2665 return -1;
2667 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2668 W = "{ S1[i] -> a[i] }";
2669 R = "{ S2[i] -> a[i+1] }";
2670 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2671 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2672 return -1;
2674 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2675 W = "{ S1[i] -> a[i] }";
2676 R = "{ S2[i] -> a[9-i] }";
2677 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2678 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2679 return -1;
2681 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2682 W = "{ S1[i] -> a[i] }";
2683 R = "[N] -> { S2[i] -> a[N-1-i] }";
2684 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2685 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2686 return -1;
2688 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2689 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2690 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2691 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2692 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2693 return -1;
2695 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2696 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2697 R = "{ S2[i,j] -> a[i-1,j] }";
2698 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2699 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2700 return -1;
2702 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2703 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2704 R = "{ S2[i,j] -> a[i,j-1] }";
2705 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2706 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2707 return -1;
2709 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2710 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2711 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2712 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2713 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2714 "S_0[] -> [0, 0, 0] }";
2715 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2716 return -1;
2717 ctx->opt->schedule_parametric = 0;
2718 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2719 return -1;
2720 ctx->opt->schedule_parametric = 1;
2722 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2723 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2724 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2725 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2726 "S4[i] -> a[i,N] }";
2727 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2728 "S4[i] -> [4,i,0] }";
2729 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2730 return -1;
2732 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2733 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2734 "j <= N }";
2735 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2736 "j <= N; "
2737 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2738 "j <= N }";
2739 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2740 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2741 return -1;
2743 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2744 " S_2[t] : t >= 0 and t <= -1 + N; "
2745 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2746 "i <= -1 + N }";
2747 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2748 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2749 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2750 "i >= 0 and i <= -1 + N }";
2751 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2752 "i >= 0 and i <= -1 + N; "
2753 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2754 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2755 " S_0[t] -> [0, t, 0] }";
2757 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2758 return -1;
2759 ctx->opt->schedule_parametric = 0;
2760 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2761 return -1;
2762 ctx->opt->schedule_parametric = 1;
2764 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2765 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2766 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2767 return -1;
2769 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2770 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2771 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2772 "j >= 0 and j <= -1 + N; "
2773 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2774 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2775 "j >= 0 and j <= -1 + N; "
2776 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2777 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2778 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2779 return -1;
2781 D = "{ S_0[i] : i >= 0 }";
2782 W = "{ S_0[i] -> a[i] : i >= 0 }";
2783 R = "{ S_0[i] -> a[0] : i >= 0 }";
2784 S = "{ S_0[i] -> [0, i, 0] }";
2785 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2786 return -1;
2788 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2789 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2790 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2791 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2792 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2793 return -1;
2795 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2796 "k <= -1 + n and k >= 0 }";
2797 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
2798 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2799 "k <= -1 + n and k >= 0; "
2800 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2801 "k <= -1 + n and k >= 0; "
2802 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2803 "k <= -1 + n and k >= 0 }";
2804 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2805 ctx->opt->schedule_outer_zero_distance = 1;
2806 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2807 return -1;
2808 ctx->opt->schedule_outer_zero_distance = 0;
2810 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
2811 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
2812 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
2813 "Stmt_for_body24[i0, i1, 1, 0]:"
2814 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
2815 "Stmt_for_body7[i0, i1, i2]:"
2816 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
2817 "i2 <= 7 }";
2819 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
2820 "Stmt_for_body24[1, i1, i2, i3]:"
2821 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
2822 "i2 >= 1;"
2823 "Stmt_for_body24[0, i1, i2, i3] -> "
2824 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
2825 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
2826 "i3 >= 0;"
2827 "Stmt_for_body24[0, i1, i2, i3] ->"
2828 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
2829 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
2830 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
2831 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
2832 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
2833 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
2834 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
2835 "i1 <= 6 and i1 >= 0;"
2836 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
2837 "Stmt_for_body7[i0, i1, i2] -> "
2838 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
2839 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
2840 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
2841 "Stmt_for_body7[i0, i1, i2] -> "
2842 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
2843 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
2844 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
2845 P = V;
2846 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
2847 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
2848 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
2850 if (test_special_schedule(ctx, D, V, P, S) < 0)
2851 return -1;
2853 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
2854 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
2855 "j >= 1 and j <= 7;"
2856 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
2857 "j >= 1 and j <= 8 }";
2858 P = "{ }";
2859 S = "{ S_0[i, j] -> [i + j, j] }";
2860 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2861 if (test_special_schedule(ctx, D, V, P, S) < 0)
2862 return -1;
2863 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2865 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
2866 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
2867 "j >= 0 and j <= -1 + i }";
2868 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
2869 "i <= -1 + N and j >= 0;"
2870 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
2871 "i <= -2 + N }";
2872 P = "{ }";
2873 S = "{ S_0[i, j] -> [i, j] }";
2874 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2875 if (test_special_schedule(ctx, D, V, P, S) < 0)
2876 return -1;
2877 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2879 /* Test both algorithms on a case with only proximity dependences. */
2880 D = "{ S[i,j] : 0 <= i <= 10 }";
2881 V = "{ }";
2882 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2883 S = "{ S[i, j] -> [j, i] }";
2884 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2885 if (test_special_schedule(ctx, D, V, P, S) < 0)
2886 return -1;
2887 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2888 if (test_special_schedule(ctx, D, V, P, S) < 0)
2889 return -1;
2891 D = "{ A[a]; B[] }";
2892 V = "{}";
2893 P = "{ A[a] -> B[] }";
2894 if (test_has_schedule(ctx, D, V, P) < 0)
2895 return -1;
2897 if (test_padded_schedule(ctx) < 0)
2898 return -1;
2900 /* Check that check for progress is not confused by rational
2901 * solution.
2903 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
2904 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
2905 "i0 <= -2 + N; "
2906 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
2907 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
2908 P = "{}";
2909 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2910 if (test_has_schedule(ctx, D, V, P) < 0)
2911 return -1;
2912 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2914 return 0;
2917 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2919 isl_union_map *umap;
2920 int test;
2922 umap = isl_union_map_read_from_str(ctx, str);
2923 test = isl_union_map_plain_is_injective(umap);
2924 isl_union_map_free(umap);
2925 if (test < 0)
2926 return -1;
2927 if (test == injective)
2928 return 0;
2929 if (injective)
2930 isl_die(ctx, isl_error_unknown,
2931 "map not detected as injective", return -1);
2932 else
2933 isl_die(ctx, isl_error_unknown,
2934 "map detected as injective", return -1);
2937 int test_injective(isl_ctx *ctx)
2939 const char *str;
2941 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2942 return -1;
2943 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2944 return -1;
2945 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2946 return -1;
2947 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2948 return -1;
2949 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2950 return -1;
2951 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2952 return -1;
2953 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2954 return -1;
2955 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2956 return -1;
2958 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2959 if (test_plain_injective(ctx, str, 1))
2960 return -1;
2961 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2962 if (test_plain_injective(ctx, str, 0))
2963 return -1;
2965 return 0;
2968 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
2970 isl_aff *aff2;
2971 int equal;
2973 if (!aff)
2974 return -1;
2976 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
2977 equal = isl_aff_plain_is_equal(aff, aff2);
2978 isl_aff_free(aff2);
2980 return equal;
2983 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
2985 int equal;
2987 equal = aff_plain_is_equal(aff, str);
2988 if (equal < 0)
2989 return -1;
2990 if (!equal)
2991 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
2992 "result not as expected", return -1);
2993 return 0;
2996 int test_aff(isl_ctx *ctx)
2998 const char *str;
2999 isl_set *set;
3000 isl_space *space;
3001 isl_local_space *ls;
3002 isl_aff *aff;
3003 int zero, equal;
3005 space = isl_space_set_alloc(ctx, 0, 1);
3006 ls = isl_local_space_from_space(space);
3007 aff = isl_aff_zero_on_domain(ls);
3009 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3010 aff = isl_aff_scale_down_ui(aff, 3);
3011 aff = isl_aff_floor(aff);
3012 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3013 aff = isl_aff_scale_down_ui(aff, 2);
3014 aff = isl_aff_floor(aff);
3015 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3017 str = "{ [10] }";
3018 set = isl_set_read_from_str(ctx, str);
3019 aff = isl_aff_gist(aff, set);
3021 aff = isl_aff_add_constant_si(aff, -16);
3022 zero = isl_aff_plain_is_zero(aff);
3023 isl_aff_free(aff);
3025 if (zero < 0)
3026 return -1;
3027 if (!zero)
3028 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3030 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3031 aff = isl_aff_scale_down_ui(aff, 64);
3032 aff = isl_aff_floor(aff);
3033 equal = aff_check_plain_equal(aff, "{ [-1] }");
3034 isl_aff_free(aff);
3035 if (equal < 0)
3036 return -1;
3038 return 0;
3041 int test_dim_max(isl_ctx *ctx)
3043 int equal;
3044 const char *str;
3045 isl_set *set1, *set2;
3046 isl_set *set;
3047 isl_map *map;
3048 isl_pw_aff *pwaff;
3050 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3051 set = isl_set_read_from_str(ctx, str);
3052 pwaff = isl_set_dim_max(set, 0);
3053 set1 = isl_set_from_pw_aff(pwaff);
3054 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3055 set2 = isl_set_read_from_str(ctx, str);
3056 equal = isl_set_is_equal(set1, set2);
3057 isl_set_free(set1);
3058 isl_set_free(set2);
3059 if (equal < 0)
3060 return -1;
3061 if (!equal)
3062 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3064 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3065 set = isl_set_read_from_str(ctx, str);
3066 pwaff = isl_set_dim_max(set, 0);
3067 set1 = isl_set_from_pw_aff(pwaff);
3068 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3069 set2 = isl_set_read_from_str(ctx, str);
3070 equal = isl_set_is_equal(set1, set2);
3071 isl_set_free(set1);
3072 isl_set_free(set2);
3073 if (equal < 0)
3074 return -1;
3075 if (!equal)
3076 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3078 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3079 set = isl_set_read_from_str(ctx, str);
3080 pwaff = isl_set_dim_max(set, 0);
3081 set1 = isl_set_from_pw_aff(pwaff);
3082 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3083 set2 = isl_set_read_from_str(ctx, str);
3084 equal = isl_set_is_equal(set1, set2);
3085 isl_set_free(set1);
3086 isl_set_free(set2);
3087 if (equal < 0)
3088 return -1;
3089 if (!equal)
3090 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3092 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3093 "0 <= i < N and 0 <= j < M }";
3094 map = isl_map_read_from_str(ctx, str);
3095 set = isl_map_range(map);
3097 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3098 set1 = isl_set_from_pw_aff(pwaff);
3099 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3100 set2 = isl_set_read_from_str(ctx, str);
3101 equal = isl_set_is_equal(set1, set2);
3102 isl_set_free(set1);
3103 isl_set_free(set2);
3105 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3106 set1 = isl_set_from_pw_aff(pwaff);
3107 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3108 set2 = isl_set_read_from_str(ctx, str);
3109 if (equal >= 0 && equal)
3110 equal = isl_set_is_equal(set1, set2);
3111 isl_set_free(set1);
3112 isl_set_free(set2);
3114 isl_set_free(set);
3116 if (equal < 0)
3117 return -1;
3118 if (!equal)
3119 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3121 /* Check that solutions are properly merged. */
3122 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3123 "c <= -1 + n - 4a - 2b and c >= -2b and "
3124 "4a >= -4 + n and c >= 0 }";
3125 set = isl_set_read_from_str(ctx, str);
3126 pwaff = isl_set_dim_min(set, 2);
3127 set1 = isl_set_from_pw_aff(pwaff);
3128 str = "[n] -> { [(0)] : n >= 1 }";
3129 set2 = isl_set_read_from_str(ctx, str);
3130 equal = isl_set_is_equal(set1, set2);
3131 isl_set_free(set1);
3132 isl_set_free(set2);
3134 if (equal < 0)
3135 return -1;
3136 if (!equal)
3137 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3139 return 0;
3142 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
3144 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
3145 const char *str)
3147 isl_ctx *ctx;
3148 isl_pw_multi_aff *pma2;
3149 int equal;
3151 if (!pma)
3152 return -1;
3154 ctx = isl_pw_multi_aff_get_ctx(pma);
3155 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3156 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
3157 isl_pw_multi_aff_free(pma2);
3159 return equal;
3162 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
3163 * represented by "str".
3165 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
3166 const char *str)
3168 int equal;
3170 equal = pw_multi_aff_plain_is_equal(pma, str);
3171 if (equal < 0)
3172 return -1;
3173 if (!equal)
3174 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
3175 "result not as expected", return -1);
3176 return 0;
3179 /* Basic test for isl_pw_multi_aff_product.
3181 * Check that multiple pieces are properly handled.
3183 static int test_product_pma(isl_ctx *ctx)
3185 int equal;
3186 const char *str;
3187 isl_pw_multi_aff *pma1, *pma2;
3189 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
3190 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3191 str = "{ C[] -> D[] }";
3192 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3193 pma1 = isl_pw_multi_aff_product(pma1, pma2);
3194 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
3195 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
3196 equal = pw_multi_aff_check_plain_equal(pma1, str);
3197 isl_pw_multi_aff_free(pma1);
3198 if (equal < 0)
3199 return -1;
3201 return 0;
3204 int test_product(isl_ctx *ctx)
3206 const char *str;
3207 isl_set *set;
3208 isl_union_set *uset1, *uset2;
3209 int ok;
3211 str = "{ A[i] }";
3212 set = isl_set_read_from_str(ctx, str);
3213 set = isl_set_product(set, isl_set_copy(set));
3214 ok = isl_set_is_wrapping(set);
3215 isl_set_free(set);
3216 if (ok < 0)
3217 return -1;
3218 if (!ok)
3219 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3221 str = "{ [] }";
3222 uset1 = isl_union_set_read_from_str(ctx, str);
3223 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3224 str = "{ [[] -> []] }";
3225 uset2 = isl_union_set_read_from_str(ctx, str);
3226 ok = isl_union_set_is_equal(uset1, uset2);
3227 isl_union_set_free(uset1);
3228 isl_union_set_free(uset2);
3229 if (ok < 0)
3230 return -1;
3231 if (!ok)
3232 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3234 if (test_product_pma(ctx) < 0)
3235 return -1;
3237 return 0;
3240 /* Check that two sets are not considered disjoint just because
3241 * they have a different set of (named) parameters.
3243 static int test_disjoint(isl_ctx *ctx)
3245 const char *str;
3246 isl_set *set, *set2;
3247 int disjoint;
3249 str = "[n] -> { [[]->[]] }";
3250 set = isl_set_read_from_str(ctx, str);
3251 str = "{ [[]->[]] }";
3252 set2 = isl_set_read_from_str(ctx, str);
3253 disjoint = isl_set_is_disjoint(set, set2);
3254 isl_set_free(set);
3255 isl_set_free(set2);
3256 if (disjoint < 0)
3257 return -1;
3258 if (disjoint)
3259 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3261 return 0;
3264 int test_equal(isl_ctx *ctx)
3266 const char *str;
3267 isl_set *set, *set2;
3268 int equal;
3270 str = "{ S_6[i] }";
3271 set = isl_set_read_from_str(ctx, str);
3272 str = "{ S_7[i] }";
3273 set2 = isl_set_read_from_str(ctx, str);
3274 equal = isl_set_is_equal(set, set2);
3275 isl_set_free(set);
3276 isl_set_free(set2);
3277 if (equal < 0)
3278 return -1;
3279 if (equal)
3280 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3282 return 0;
3285 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3286 enum isl_dim_type type, unsigned pos, int fixed)
3288 int test;
3290 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3291 isl_map_free(map);
3292 if (test < 0)
3293 return -1;
3294 if (test == fixed)
3295 return 0;
3296 if (fixed)
3297 isl_die(ctx, isl_error_unknown,
3298 "map not detected as fixed", return -1);
3299 else
3300 isl_die(ctx, isl_error_unknown,
3301 "map detected as fixed", return -1);
3304 int test_fixed(isl_ctx *ctx)
3306 const char *str;
3307 isl_map *map;
3309 str = "{ [i] -> [i] }";
3310 map = isl_map_read_from_str(ctx, str);
3311 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3312 return -1;
3313 str = "{ [i] -> [1] }";
3314 map = isl_map_read_from_str(ctx, str);
3315 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3316 return -1;
3317 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3318 map = isl_map_read_from_str(ctx, str);
3319 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3320 return -1;
3321 map = isl_map_read_from_str(ctx, str);
3322 map = isl_map_neg(map);
3323 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3324 return -1;
3326 return 0;
3329 int test_vertices(isl_ctx *ctx)
3331 const char *str;
3332 isl_basic_set *bset;
3333 isl_vertices *vertices;
3335 str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
3336 bset = isl_basic_set_read_from_str(ctx, str);
3337 vertices = isl_basic_set_compute_vertices(bset);
3338 isl_basic_set_free(bset);
3339 isl_vertices_free(vertices);
3340 if (!vertices)
3341 return -1;
3343 str = "{ A[t, i] : t = 14 and i = 1 }";
3344 bset = isl_basic_set_read_from_str(ctx, str);
3345 vertices = isl_basic_set_compute_vertices(bset);
3346 isl_basic_set_free(bset);
3347 isl_vertices_free(vertices);
3348 if (!vertices)
3349 return -1;
3351 return 0;
3354 int test_union_pw(isl_ctx *ctx)
3356 int equal;
3357 const char *str;
3358 isl_union_set *uset;
3359 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3361 str = "{ [x] -> x^2 }";
3362 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3363 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3364 uset = isl_union_pw_qpolynomial_domain(upwqp1);
3365 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3366 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3367 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3368 isl_union_pw_qpolynomial_free(upwqp1);
3369 isl_union_pw_qpolynomial_free(upwqp2);
3370 if (equal < 0)
3371 return -1;
3372 if (!equal)
3373 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3375 return 0;
3378 int test_output(isl_ctx *ctx)
3380 char *s;
3381 const char *str;
3382 isl_pw_aff *pa;
3383 isl_printer *p;
3384 int equal;
3386 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3387 pa = isl_pw_aff_read_from_str(ctx, str);
3389 p = isl_printer_to_str(ctx);
3390 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3391 p = isl_printer_print_pw_aff(p, pa);
3392 s = isl_printer_get_str(p);
3393 isl_printer_free(p);
3394 isl_pw_aff_free(pa);
3395 if (!s)
3396 equal = -1;
3397 else
3398 equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
3399 free(s);
3400 if (equal < 0)
3401 return -1;
3402 if (!equal)
3403 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3405 return 0;
3408 int test_sample(isl_ctx *ctx)
3410 const char *str;
3411 isl_basic_set *bset1, *bset2;
3412 int empty, subset;
3414 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3415 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3416 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3417 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3418 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3419 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3420 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3421 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3422 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3423 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3424 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3425 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3426 "d - 1073741821e and "
3427 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3428 "3j >= 1 - a + b + 2e and "
3429 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3430 "3i <= 4 - a + 4b - e and "
3431 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3432 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3433 "c <= -1 + a and 3i >= -2 - a + 3e and "
3434 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3435 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3436 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3437 "1073741823e >= 1 + 1073741823b - d and "
3438 "3i >= 1073741823b + c - 1073741823e - f and "
3439 "3i >= 1 + 2b + e + 3g }";
3440 bset1 = isl_basic_set_read_from_str(ctx, str);
3441 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3442 empty = isl_basic_set_is_empty(bset2);
3443 subset = isl_basic_set_is_subset(bset2, bset1);
3444 isl_basic_set_free(bset1);
3445 isl_basic_set_free(bset2);
3446 if (empty < 0 || subset < 0)
3447 return -1;
3448 if (empty)
3449 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3450 if (!subset)
3451 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3453 return 0;
3456 int test_fixed_power(isl_ctx *ctx)
3458 const char *str;
3459 isl_map *map;
3460 isl_int exp;
3461 int equal;
3463 isl_int_init(exp);
3464 str = "{ [i] -> [i + 1] }";
3465 map = isl_map_read_from_str(ctx, str);
3466 isl_int_set_si(exp, 23);
3467 map = isl_map_fixed_power(map, exp);
3468 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3469 isl_int_clear(exp);
3470 isl_map_free(map);
3471 if (equal < 0)
3472 return -1;
3474 return 0;
3477 int test_slice(isl_ctx *ctx)
3479 const char *str;
3480 isl_map *map;
3481 int equal;
3483 str = "{ [i] -> [j] }";
3484 map = isl_map_read_from_str(ctx, str);
3485 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3486 equal = map_check_equal(map, "{ [i] -> [i] }");
3487 isl_map_free(map);
3488 if (equal < 0)
3489 return -1;
3491 str = "{ [i] -> [j] }";
3492 map = isl_map_read_from_str(ctx, str);
3493 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
3494 equal = map_check_equal(map, "{ [i] -> [j] }");
3495 isl_map_free(map);
3496 if (equal < 0)
3497 return -1;
3499 str = "{ [i] -> [j] }";
3500 map = isl_map_read_from_str(ctx, str);
3501 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
3502 equal = map_check_equal(map, "{ [i] -> [-i] }");
3503 isl_map_free(map);
3504 if (equal < 0)
3505 return -1;
3507 str = "{ [i] -> [j] }";
3508 map = isl_map_read_from_str(ctx, str);
3509 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
3510 equal = map_check_equal(map, "{ [0] -> [j] }");
3511 isl_map_free(map);
3512 if (equal < 0)
3513 return -1;
3515 str = "{ [i] -> [j] }";
3516 map = isl_map_read_from_str(ctx, str);
3517 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3518 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3519 isl_map_free(map);
3520 if (equal < 0)
3521 return -1;
3523 str = "{ [i] -> [j] }";
3524 map = isl_map_read_from_str(ctx, str);
3525 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3526 equal = map_check_equal(map, "{ [i] -> [j] : false }");
3527 isl_map_free(map);
3528 if (equal < 0)
3529 return -1;
3531 return 0;
3534 int test_eliminate(isl_ctx *ctx)
3536 const char *str;
3537 isl_map *map;
3538 int equal;
3540 str = "{ [i] -> [j] : i = 2j }";
3541 map = isl_map_read_from_str(ctx, str);
3542 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3543 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3544 isl_map_free(map);
3545 if (equal < 0)
3546 return -1;
3548 return 0;
3551 /* Check that isl_set_dim_residue_class detects that the values of j
3552 * in the set below are all odd and that it does not detect any spurious
3553 * strides.
3555 static int test_residue_class(isl_ctx *ctx)
3557 const char *str;
3558 isl_set *set;
3559 isl_int m, r;
3560 int res;
3562 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3563 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3564 set = isl_set_read_from_str(ctx, str);
3565 isl_int_init(m);
3566 isl_int_init(r);
3567 res = isl_set_dim_residue_class(set, 1, &m, &r);
3568 if (res >= 0 &&
3569 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3570 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3571 res = -1);
3572 isl_int_clear(r);
3573 isl_int_clear(m);
3574 isl_set_free(set);
3576 return res;
3579 int test_align_parameters(isl_ctx *ctx)
3581 const char *str;
3582 isl_space *space;
3583 isl_multi_aff *ma1, *ma2;
3584 int equal;
3586 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3587 ma1 = isl_multi_aff_read_from_str(ctx, str);
3589 space = isl_space_params_alloc(ctx, 1);
3590 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
3591 ma1 = isl_multi_aff_align_params(ma1, space);
3593 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3594 ma2 = isl_multi_aff_read_from_str(ctx, str);
3596 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3598 isl_multi_aff_free(ma1);
3599 isl_multi_aff_free(ma2);
3601 if (equal < 0)
3602 return -1;
3603 if (!equal)
3604 isl_die(ctx, isl_error_unknown,
3605 "result not as expected", return -1);
3607 return 0;
3610 static int test_list(isl_ctx *ctx)
3612 isl_id *a, *b, *c, *d, *id;
3613 isl_id_list *list;
3614 int ok;
3616 a = isl_id_alloc(ctx, "a", NULL);
3617 b = isl_id_alloc(ctx, "b", NULL);
3618 c = isl_id_alloc(ctx, "c", NULL);
3619 d = isl_id_alloc(ctx, "d", NULL);
3621 list = isl_id_list_alloc(ctx, 4);
3622 list = isl_id_list_add(list, a);
3623 list = isl_id_list_add(list, b);
3624 list = isl_id_list_add(list, c);
3625 list = isl_id_list_add(list, d);
3626 list = isl_id_list_drop(list, 1, 1);
3628 if (isl_id_list_n_id(list) != 3) {
3629 isl_id_list_free(list);
3630 isl_die(ctx, isl_error_unknown,
3631 "unexpected number of elements in list", return -1);
3634 id = isl_id_list_get_id(list, 0);
3635 ok = id == a;
3636 isl_id_free(id);
3637 id = isl_id_list_get_id(list, 1);
3638 ok = ok && id == c;
3639 isl_id_free(id);
3640 id = isl_id_list_get_id(list, 2);
3641 ok = ok && id == d;
3642 isl_id_free(id);
3644 isl_id_list_free(list);
3646 if (!ok)
3647 isl_die(ctx, isl_error_unknown,
3648 "unexpected elements in list", return -1);
3650 return 0;
3653 const char *set_conversion_tests[] = {
3654 "[N] -> { [i] : N - 1 <= 2 i <= N }",
3655 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
3656 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3657 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3660 /* Check that converting from isl_set to isl_pw_multi_aff and back
3661 * to isl_set produces the original isl_set.
3663 static int test_set_conversion(isl_ctx *ctx)
3665 int i;
3666 const char *str;
3667 isl_set *set1, *set2;
3668 isl_pw_multi_aff *pma;
3669 int equal;
3671 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
3672 str = set_conversion_tests[i];
3673 set1 = isl_set_read_from_str(ctx, str);
3674 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
3675 set2 = isl_set_from_pw_multi_aff(pma);
3676 equal = isl_set_is_equal(set1, set2);
3677 isl_set_free(set1);
3678 isl_set_free(set2);
3680 if (equal < 0)
3681 return -1;
3682 if (!equal)
3683 isl_die(ctx, isl_error_unknown, "bad conversion",
3684 return -1);
3687 return 0;
3690 /* Check that converting from isl_map to isl_pw_multi_aff and back
3691 * to isl_map produces the original isl_map.
3693 static int test_map_conversion(isl_ctx *ctx)
3695 const char *str;
3696 isl_map *map1, *map2;
3697 isl_pw_multi_aff *pma;
3698 int equal;
3700 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
3701 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
3702 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
3703 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
3704 "9e <= -2 - 2a) }";
3705 map1 = isl_map_read_from_str(ctx, str);
3706 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
3707 map2 = isl_map_from_pw_multi_aff(pma);
3708 equal = isl_map_is_equal(map1, map2);
3709 isl_map_free(map1);
3710 isl_map_free(map2);
3712 if (equal < 0)
3713 return -1;
3714 if (!equal)
3715 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
3717 return 0;
3720 static int test_conversion(isl_ctx *ctx)
3722 if (test_set_conversion(ctx) < 0)
3723 return -1;
3724 if (test_map_conversion(ctx) < 0)
3725 return -1;
3726 return 0;
3729 /* Check that isl_basic_map_curry does not modify input.
3731 static int test_curry(isl_ctx *ctx)
3733 const char *str;
3734 isl_basic_map *bmap1, *bmap2;
3735 int equal;
3737 str = "{ [A[] -> B[]] -> C[] }";
3738 bmap1 = isl_basic_map_read_from_str(ctx, str);
3739 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
3740 equal = isl_basic_map_is_equal(bmap1, bmap2);
3741 isl_basic_map_free(bmap1);
3742 isl_basic_map_free(bmap2);
3744 if (equal < 0)
3745 return -1;
3746 if (equal)
3747 isl_die(ctx, isl_error_unknown,
3748 "curried map should not be equal to original",
3749 return -1);
3751 return 0;
3754 struct {
3755 const char *set;
3756 const char *ma;
3757 const char *res;
3758 } preimage_tests[] = {
3759 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
3760 "{ A[j,i] -> B[i,j] }",
3761 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
3762 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3763 "{ A[a,b] -> B[a/2,b/6] }",
3764 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
3765 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3766 "{ A[a,b] -> B[a/2,b/6] }",
3767 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
3768 "exists i,j : a = 2 i and b = 6 j }" },
3769 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
3770 "[n] -> { : 0 <= n <= 100 }" },
3771 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3772 "{ A[a] -> B[2a] }",
3773 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
3774 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3775 "{ A[a] -> B[([a/2])] }",
3776 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
3777 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
3778 "{ A[a] -> B[a,a,a/3] }",
3779 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
3780 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
3781 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
3784 static int test_preimage_basic_set(isl_ctx *ctx)
3786 int i;
3787 isl_basic_set *bset1, *bset2;
3788 isl_multi_aff *ma;
3789 int equal;
3791 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
3792 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
3793 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
3794 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
3795 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
3796 equal = isl_basic_set_is_equal(bset1, bset2);
3797 isl_basic_set_free(bset1);
3798 isl_basic_set_free(bset2);
3799 if (equal < 0)
3800 return -1;
3801 if (!equal)
3802 isl_die(ctx, isl_error_unknown, "bad preimage",
3803 return -1);
3806 return 0;
3809 struct {
3810 const char *map;
3811 const char *ma;
3812 const char *res;
3813 } preimage_domain_tests[] = {
3814 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
3815 "{ A[j,i] -> B[i,j] }",
3816 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
3817 { "{ B[i] -> C[i]; D[i] -> E[i] }",
3818 "{ A[i] -> B[i + 1] }",
3819 "{ A[i] -> C[i + 1] }" },
3820 { "{ B[i] -> C[i]; B[i] -> E[i] }",
3821 "{ A[i] -> B[i + 1] }",
3822 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
3823 { "{ B[i] -> C[([i/2])] }",
3824 "{ A[i] -> B[2i] }",
3825 "{ A[i] -> C[i] }" },
3826 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
3827 "{ A[i] -> B[([i/5]), ([i/7])] }",
3828 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
3829 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
3830 "[N] -> { A[] -> B[([N/5])] }",
3831 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
3832 { "{ B[i] -> C[i] : exists a : i = 5 a }",
3833 "{ A[i] -> B[2i] }",
3834 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
3835 { "{ B[i] -> C[i] : exists a : i = 2 a; "
3836 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
3837 "{ A[i] -> B[2i] }",
3838 "{ A[i] -> C[2i] }" },
3841 static int test_preimage_union_map(isl_ctx *ctx)
3843 int i;
3844 isl_union_map *umap1, *umap2;
3845 isl_multi_aff *ma;
3846 int equal;
3848 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
3849 umap1 = isl_union_map_read_from_str(ctx,
3850 preimage_domain_tests[i].map);
3851 ma = isl_multi_aff_read_from_str(ctx,
3852 preimage_domain_tests[i].ma);
3853 umap2 = isl_union_map_read_from_str(ctx,
3854 preimage_domain_tests[i].res);
3855 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
3856 equal = isl_union_map_is_equal(umap1, umap2);
3857 isl_union_map_free(umap1);
3858 isl_union_map_free(umap2);
3859 if (equal < 0)
3860 return -1;
3861 if (!equal)
3862 isl_die(ctx, isl_error_unknown, "bad preimage",
3863 return -1);
3866 return 0;
3869 static int test_preimage(isl_ctx *ctx)
3871 if (test_preimage_basic_set(ctx) < 0)
3872 return -1;
3873 if (test_preimage_union_map(ctx) < 0)
3874 return -1;
3876 return 0;
3879 struct {
3880 const char *ma1;
3881 const char *ma;
3882 const char *res;
3883 } pullback_tests[] = {
3884 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
3885 "{ A[a,b] -> C[b + 2a] }" },
3886 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
3887 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
3888 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
3889 "{ A[a] -> C[(a)/6] }" },
3890 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
3891 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
3892 "{ A[a] -> C[(2a)/3] }" },
3893 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
3894 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
3895 "{ A[i,j] -> C[i + j, i + j] }"},
3896 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
3897 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
3898 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
3901 static int test_pullback(isl_ctx *ctx)
3903 int i;
3904 isl_multi_aff *ma1, *ma2;
3905 isl_multi_aff *ma;
3906 int equal;
3908 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
3909 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
3910 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
3911 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
3912 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
3913 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3914 isl_multi_aff_free(ma1);
3915 isl_multi_aff_free(ma2);
3916 if (equal < 0)
3917 return -1;
3918 if (!equal)
3919 isl_die(ctx, isl_error_unknown, "bad pullback",
3920 return -1);
3923 return 0;
3926 /* Check that negation is printed correctly.
3928 static int test_ast(isl_ctx *ctx)
3930 isl_ast_expr *expr, *expr1, *expr2, *expr3;
3931 char *str;
3932 int ok;
3934 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3935 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3936 expr = isl_ast_expr_add(expr1, expr2);
3937 expr = isl_ast_expr_neg(expr);
3938 str = isl_ast_expr_to_str(expr);
3939 ok = str ? !strcmp(str, "-(A + B)") : -1;
3940 free(str);
3941 isl_ast_expr_free(expr);
3943 if (ok < 0)
3944 return -1;
3945 if (!ok)
3946 isl_die(ctx, isl_error_unknown,
3947 "isl_ast_expr printed incorrectly", return -1);
3949 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3950 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3951 expr = isl_ast_expr_add(expr1, expr2);
3952 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
3953 expr = isl_ast_expr_sub(expr3, expr);
3954 str = isl_ast_expr_to_str(expr);
3955 ok = str ? !strcmp(str, "C - (A + B)") : -1;
3956 free(str);
3957 isl_ast_expr_free(expr);
3959 if (ok < 0)
3960 return -1;
3961 if (!ok)
3962 isl_die(ctx, isl_error_unknown,
3963 "isl_ast_expr printed incorrectly", return -1);
3965 return 0;
3968 /* Internal data structure for before_for and after_for callbacks.
3970 * depth is the current depth
3971 * before is the number of times before_for has been called
3972 * after is the number of times after_for has been called
3974 struct isl_test_codegen_data {
3975 int depth;
3976 int before;
3977 int after;
3980 /* This function is called before each for loop in the AST generated
3981 * from test_ast_gen1.
3983 * Increment the number of calls and the depth.
3984 * Check that the space returned by isl_ast_build_get_schedule_space
3985 * matches the target space of the schedule returned by
3986 * isl_ast_build_get_schedule.
3987 * Return an isl_id that is checked by the corresponding call
3988 * to after_for.
3990 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
3991 void *user)
3993 struct isl_test_codegen_data *data = user;
3994 isl_ctx *ctx;
3995 isl_space *space;
3996 isl_union_map *schedule;
3997 isl_union_set *uset;
3998 isl_set *set;
3999 int empty;
4000 char name[] = "d0";
4002 ctx = isl_ast_build_get_ctx(build);
4004 if (data->before >= 3)
4005 isl_die(ctx, isl_error_unknown,
4006 "unexpected number of for nodes", return NULL);
4007 if (data->depth >= 2)
4008 isl_die(ctx, isl_error_unknown,
4009 "unexpected depth", return NULL);
4011 snprintf(name, sizeof(name), "d%d", data->depth);
4012 data->before++;
4013 data->depth++;
4015 schedule = isl_ast_build_get_schedule(build);
4016 uset = isl_union_map_range(schedule);
4017 if (!uset)
4018 return NULL;
4019 if (isl_union_set_n_set(uset) != 1) {
4020 isl_union_set_free(uset);
4021 isl_die(ctx, isl_error_unknown,
4022 "expecting single range space", return NULL);
4025 space = isl_ast_build_get_schedule_space(build);
4026 set = isl_union_set_extract_set(uset, space);
4027 isl_union_set_free(uset);
4028 empty = isl_set_is_empty(set);
4029 isl_set_free(set);
4031 if (empty < 0)
4032 return NULL;
4033 if (empty)
4034 isl_die(ctx, isl_error_unknown,
4035 "spaces don't match", return NULL);
4037 return isl_id_alloc(ctx, name, NULL);
4040 /* This function is called after each for loop in the AST generated
4041 * from test_ast_gen1.
4043 * Increment the number of calls and decrement the depth.
4044 * Check that the annotation attached to the node matches
4045 * the isl_id returned by the corresponding call to before_for.
4047 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
4048 __isl_keep isl_ast_build *build, void *user)
4050 struct isl_test_codegen_data *data = user;
4051 isl_id *id;
4052 const char *name;
4053 int valid;
4055 data->after++;
4056 data->depth--;
4058 if (data->after > data->before)
4059 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4060 "mismatch in number of for nodes",
4061 return isl_ast_node_free(node));
4063 id = isl_ast_node_get_annotation(node);
4064 if (!id)
4065 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4066 "missing annotation", return isl_ast_node_free(node));
4068 name = isl_id_get_name(id);
4069 valid = name && atoi(name + 1) == data->depth;
4070 isl_id_free(id);
4072 if (!valid)
4073 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4074 "wrong annotation", return isl_ast_node_free(node));
4076 return node;
4079 /* Check that the before_each_for and after_each_for callbacks
4080 * are called for each for loop in the generated code,
4081 * that they are called in the right order and that the isl_id
4082 * returned from the before_each_for callback is attached to
4083 * the isl_ast_node passed to the corresponding after_each_for call.
4085 static int test_ast_gen1(isl_ctx *ctx)
4087 const char *str;
4088 isl_set *set;
4089 isl_union_map *schedule;
4090 isl_ast_build *build;
4091 isl_ast_node *tree;
4092 struct isl_test_codegen_data data;
4094 str = "[N] -> { : N >= 10 }";
4095 set = isl_set_read_from_str(ctx, str);
4096 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
4097 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
4098 schedule = isl_union_map_read_from_str(ctx, str);
4100 data.before = 0;
4101 data.after = 0;
4102 data.depth = 0;
4103 build = isl_ast_build_from_context(set);
4104 build = isl_ast_build_set_before_each_for(build,
4105 &before_for, &data);
4106 build = isl_ast_build_set_after_each_for(build,
4107 &after_for, &data);
4108 tree = isl_ast_build_ast_from_schedule(build, schedule);
4109 isl_ast_build_free(build);
4110 if (!tree)
4111 return -1;
4113 isl_ast_node_free(tree);
4115 if (data.before != 3 || data.after != 3)
4116 isl_die(ctx, isl_error_unknown,
4117 "unexpected number of for nodes", return -1);
4119 return 0;
4122 /* Check that the AST generator handles domains that are integrally disjoint
4123 * but not ratinoally disjoint.
4125 static int test_ast_gen2(isl_ctx *ctx)
4127 const char *str;
4128 isl_set *set;
4129 isl_union_map *schedule;
4130 isl_union_map *options;
4131 isl_ast_build *build;
4132 isl_ast_node *tree;
4134 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4135 schedule = isl_union_map_read_from_str(ctx, str);
4136 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4137 build = isl_ast_build_from_context(set);
4139 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4140 options = isl_union_map_read_from_str(ctx, str);
4141 build = isl_ast_build_set_options(build, options);
4142 tree = isl_ast_build_ast_from_schedule(build, schedule);
4143 isl_ast_build_free(build);
4144 if (!tree)
4145 return -1;
4146 isl_ast_node_free(tree);
4148 return 0;
4151 /* Increment *user on each call.
4153 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4154 __isl_keep isl_ast_build *build, void *user)
4156 int *n = user;
4158 (*n)++;
4160 return node;
4163 /* Test that unrolling tries to minimize the number of instances.
4164 * In particular, for the schedule given below, make sure it generates
4165 * 3 nodes (rather than 101).
4167 static int test_ast_gen3(isl_ctx *ctx)
4169 const char *str;
4170 isl_set *set;
4171 isl_union_map *schedule;
4172 isl_union_map *options;
4173 isl_ast_build *build;
4174 isl_ast_node *tree;
4175 int n_domain = 0;
4177 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4178 schedule = isl_union_map_read_from_str(ctx, str);
4179 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4181 str = "{ [i] -> unroll[0] }";
4182 options = isl_union_map_read_from_str(ctx, str);
4184 build = isl_ast_build_from_context(set);
4185 build = isl_ast_build_set_options(build, options);
4186 build = isl_ast_build_set_at_each_domain(build,
4187 &count_domains, &n_domain);
4188 tree = isl_ast_build_ast_from_schedule(build, schedule);
4189 isl_ast_build_free(build);
4190 if (!tree)
4191 return -1;
4193 isl_ast_node_free(tree);
4195 if (n_domain != 3)
4196 isl_die(ctx, isl_error_unknown,
4197 "unexpected number of for nodes", return -1);
4199 return 0;
4202 /* Check that if the ast_build_exploit_nested_bounds options is set,
4203 * we do not get an outer if node in the generated AST,
4204 * while we do get such an outer if node if the options is not set.
4206 static int test_ast_gen4(isl_ctx *ctx)
4208 const char *str;
4209 isl_set *set;
4210 isl_union_map *schedule;
4211 isl_ast_build *build;
4212 isl_ast_node *tree;
4213 enum isl_ast_node_type type;
4214 int enb;
4216 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4217 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4219 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4221 schedule = isl_union_map_read_from_str(ctx, str);
4222 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4223 build = isl_ast_build_from_context(set);
4224 tree = isl_ast_build_ast_from_schedule(build, schedule);
4225 isl_ast_build_free(build);
4226 if (!tree)
4227 return -1;
4229 type = isl_ast_node_get_type(tree);
4230 isl_ast_node_free(tree);
4232 if (type == isl_ast_node_if)
4233 isl_die(ctx, isl_error_unknown,
4234 "not expecting if node", return -1);
4236 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4238 schedule = isl_union_map_read_from_str(ctx, str);
4239 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4240 build = isl_ast_build_from_context(set);
4241 tree = isl_ast_build_ast_from_schedule(build, schedule);
4242 isl_ast_build_free(build);
4243 if (!tree)
4244 return -1;
4246 type = isl_ast_node_get_type(tree);
4247 isl_ast_node_free(tree);
4249 if (type != isl_ast_node_if)
4250 isl_die(ctx, isl_error_unknown,
4251 "expecting if node", return -1);
4253 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4255 return 0;
4258 /* This function is called for each leaf in the AST generated
4259 * from test_ast_gen5.
4261 * We finalize the AST generation by extending the outer schedule
4262 * with a zero-dimensional schedule. If this results in any for loops,
4263 * then this means that we did not pass along enough information
4264 * about the outer schedule to the inner AST generation.
4266 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4267 void *user)
4269 isl_union_map *schedule, *extra;
4270 isl_ast_node *tree;
4272 schedule = isl_ast_build_get_schedule(build);
4273 extra = isl_union_map_copy(schedule);
4274 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4275 schedule = isl_union_map_range_product(schedule, extra);
4276 tree = isl_ast_build_ast_from_schedule(build, schedule);
4277 isl_ast_build_free(build);
4279 if (!tree)
4280 return NULL;
4282 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4283 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4284 "code should not contain any for loop",
4285 return isl_ast_node_free(tree));
4287 return tree;
4290 /* Check that we do not lose any information when going back and
4291 * forth between internal and external schedule.
4293 * In particular, we create an AST where we unroll the only
4294 * non-constant dimension in the schedule. We therefore do
4295 * not expect any for loops in the AST. However, older versions
4296 * of isl would not pass along enough information about the outer
4297 * schedule when performing an inner code generation from a create_leaf
4298 * callback, resulting in the inner code generation producing a for loop.
4300 static int test_ast_gen5(isl_ctx *ctx)
4302 const char *str;
4303 isl_set *set;
4304 isl_union_map *schedule, *options;
4305 isl_ast_build *build;
4306 isl_ast_node *tree;
4308 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4309 schedule = isl_union_map_read_from_str(ctx, str);
4311 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4312 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4313 options = isl_union_map_read_from_str(ctx, str);
4315 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4316 build = isl_ast_build_from_context(set);
4317 build = isl_ast_build_set_options(build, options);
4318 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4319 tree = isl_ast_build_ast_from_schedule(build, schedule);
4320 isl_ast_build_free(build);
4321 isl_ast_node_free(tree);
4322 if (!tree)
4323 return -1;
4325 return 0;
4328 static int test_ast_gen(isl_ctx *ctx)
4330 if (test_ast_gen1(ctx) < 0)
4331 return -1;
4332 if (test_ast_gen2(ctx) < 0)
4333 return -1;
4334 if (test_ast_gen3(ctx) < 0)
4335 return -1;
4336 if (test_ast_gen4(ctx) < 0)
4337 return -1;
4338 if (test_ast_gen5(ctx) < 0)
4339 return -1;
4340 return 0;
4343 /* Check if dropping output dimensions from an isl_pw_multi_aff
4344 * works properly.
4346 static int test_pw_multi_aff(isl_ctx *ctx)
4348 const char *str;
4349 isl_pw_multi_aff *pma1, *pma2;
4350 int equal;
4352 str = "{ [i,j] -> [i+j, 4i-j] }";
4353 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4354 str = "{ [i,j] -> [4i-j] }";
4355 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4357 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4359 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4361 isl_pw_multi_aff_free(pma1);
4362 isl_pw_multi_aff_free(pma2);
4363 if (equal < 0)
4364 return -1;
4365 if (!equal)
4366 isl_die(ctx, isl_error_unknown,
4367 "expressions not equal", return -1);
4369 return 0;
4372 /* This is a regression test for a bug where isl_basic_map_simplify
4373 * would end up in an infinite loop. In particular, we construct
4374 * an empty basic set that is not obviously empty.
4375 * isl_basic_set_is_empty marks the basic set as empty.
4376 * After projecting out i3, the variable can be dropped completely,
4377 * but isl_basic_map_simplify refrains from doing so if the basic set
4378 * is empty and would end up in an infinite loop if it didn't test
4379 * explicitly for empty basic maps in the outer loop.
4381 static int test_simplify(isl_ctx *ctx)
4383 const char *str;
4384 isl_basic_set *bset;
4385 int empty;
4387 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
4388 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
4389 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
4390 "i3 >= i2 }";
4391 bset = isl_basic_set_read_from_str(ctx, str);
4392 empty = isl_basic_set_is_empty(bset);
4393 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
4394 isl_basic_set_free(bset);
4395 if (!bset)
4396 return -1;
4397 if (!empty)
4398 isl_die(ctx, isl_error_unknown,
4399 "basic set should be empty", return -1);
4401 return 0;
4404 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
4405 * with gbr context would fail to disable the use of the shifted tableau
4406 * when transferring equalities for the input to the context, resulting
4407 * in invalid sample values.
4409 static int test_partial_lexmin(isl_ctx *ctx)
4411 const char *str;
4412 isl_basic_set *bset;
4413 isl_basic_map *bmap;
4414 isl_map *map;
4416 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
4417 bmap = isl_basic_map_read_from_str(ctx, str);
4418 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
4419 bset = isl_basic_set_read_from_str(ctx, str);
4420 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
4421 isl_map_free(map);
4423 if (!map)
4424 return -1;
4426 return 0;
4429 /* Check that the variable compression performed on the existentially
4430 * quantified variables inside isl_basic_set_compute_divs is not confused
4431 * by the implicit equalities among the parameters.
4433 static int test_compute_divs(isl_ctx *ctx)
4435 const char *str;
4436 isl_basic_set *bset;
4437 isl_set *set;
4439 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
4440 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
4441 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
4442 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
4443 bset = isl_basic_set_read_from_str(ctx, str);
4444 set = isl_basic_set_compute_divs(bset);
4445 isl_set_free(set);
4446 if (!set)
4447 return -1;
4449 return 0;
4452 struct {
4453 const char *name;
4454 int (*fn)(isl_ctx *ctx);
4455 } tests [] = {
4456 { "val", &test_val },
4457 { "compute divs", &test_compute_divs },
4458 { "partial lexmin", &test_partial_lexmin },
4459 { "simplify", &test_simplify },
4460 { "curry", &test_curry },
4461 { "piecewise multi affine expressions", &test_pw_multi_aff },
4462 { "conversion", &test_conversion },
4463 { "list", &test_list },
4464 { "align parameters", &test_align_parameters },
4465 { "preimage", &test_preimage },
4466 { "pullback", &test_pullback },
4467 { "AST", &test_ast },
4468 { "AST generation", &test_ast_gen },
4469 { "eliminate", &test_eliminate },
4470 { "residue class", &test_residue_class },
4471 { "div", &test_div },
4472 { "slice", &test_slice },
4473 { "fixed power", &test_fixed_power },
4474 { "sample", &test_sample },
4475 { "output", &test_output },
4476 { "vertices", &test_vertices },
4477 { "fixed", &test_fixed },
4478 { "equal", &test_equal },
4479 { "disjoint", &test_disjoint },
4480 { "product", &test_product },
4481 { "dim_max", &test_dim_max },
4482 { "affine", &test_aff },
4483 { "injective", &test_injective },
4484 { "schedule", &test_schedule },
4485 { "union_pw", &test_union_pw },
4486 { "parse", &test_parse },
4487 { "single-valued", &test_sv },
4488 { "affine hull", &test_affine_hull },
4489 { "coalesce", &test_coalesce },
4490 { "factorize", &test_factorize },
4491 { "subset", &test_subset },
4492 { "subtract", &test_subtract },
4493 { "lexmin", &test_lexmin },
4494 { "min", &test_min },
4495 { "gist", &test_gist },
4496 { "piecewise quasi-polynomials", &test_pwqp },
4499 int main()
4501 int i;
4502 struct isl_ctx *ctx;
4504 srcdir = getenv("srcdir");
4505 assert(srcdir);
4507 ctx = isl_ctx_alloc();
4508 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
4509 printf("%s\n", tests[i].name);
4510 if (tests[i].fn(ctx) < 0)
4511 goto error;
4513 test_lift(ctx);
4514 test_bound(ctx);
4515 test_union(ctx);
4516 test_split_periods(ctx);
4517 test_lex(ctx);
4518 test_bijective(ctx);
4519 test_dep(ctx);
4520 test_read(ctx);
4521 test_bounded(ctx);
4522 test_construction(ctx);
4523 test_dim(ctx);
4524 test_application(ctx);
4525 test_convex_hull(ctx);
4526 test_closure(ctx);
4527 isl_ctx_free(ctx);
4528 return 0;
4529 error:
4530 isl_ctx_free(ctx);
4531 return -1;