add test case for isl_pw_qpolynomial_intersect_domain
[isl.git] / isl_test.c
blob1e90cbdd2d004ecb0352052dc8cc0231577a68b4
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>
28 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
30 static char *srcdir;
32 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
33 char *filename;
34 int length;
35 char *pattern = "%s/test_inputs/%s.%s";
37 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
38 + strlen(suffix) + 1;
39 filename = isl_alloc_array(ctx, char, length);
41 if (!filename)
42 return NULL;
44 sprintf(filename, pattern, srcdir, name, suffix);
46 return filename;
49 void test_parse_map(isl_ctx *ctx, const char *str)
51 isl_map *map;
53 map = isl_map_read_from_str(ctx, str);
54 assert(map);
55 isl_map_free(map);
58 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
60 isl_map *map, *map2;
61 int equal;
63 map = isl_map_read_from_str(ctx, str);
64 map2 = isl_map_read_from_str(ctx, str2);
65 equal = isl_map_is_equal(map, map2);
66 isl_map_free(map);
67 isl_map_free(map2);
69 if (equal < 0)
70 return -1;
71 if (!equal)
72 isl_die(ctx, isl_error_unknown, "maps not equal",
73 return -1);
75 return 0;
78 void test_parse_pwqp(isl_ctx *ctx, const char *str)
80 isl_pw_qpolynomial *pwqp;
82 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
83 assert(pwqp);
84 isl_pw_qpolynomial_free(pwqp);
87 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
89 isl_pw_aff *pwaff;
91 pwaff = isl_pw_aff_read_from_str(ctx, str);
92 assert(pwaff);
93 isl_pw_aff_free(pwaff);
96 int test_parse(struct isl_ctx *ctx)
98 isl_map *map, *map2;
99 const char *str, *str2;
101 str = "{ [i] -> [-i] }";
102 map = isl_map_read_from_str(ctx, str);
103 assert(map);
104 isl_map_free(map);
106 str = "{ A[i] -> L[([i/3])] }";
107 map = isl_map_read_from_str(ctx, str);
108 assert(map);
109 isl_map_free(map);
111 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
112 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
113 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
115 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
116 str2 = "{ [x, y] : 2y >= 6 - x }";
117 if (test_parse_map_equal(ctx, str, str2) < 0)
118 return -1;
120 if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
121 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
122 return -1;
123 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
124 if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
125 str) < 0)
126 return -1;
128 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
129 map = isl_map_read_from_str(ctx, str);
130 str = "{ [new, old] -> [o0, o1] : "
131 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
132 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
133 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
134 map2 = isl_map_read_from_str(ctx, str);
135 assert(isl_map_is_equal(map, map2));
136 isl_map_free(map);
137 isl_map_free(map2);
139 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
140 map = isl_map_read_from_str(ctx, str);
141 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
142 map2 = isl_map_read_from_str(ctx, str);
143 assert(isl_map_is_equal(map, map2));
144 isl_map_free(map);
145 isl_map_free(map2);
147 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
148 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
149 if (test_parse_map_equal(ctx, str, str2) < 0)
150 return -1;
152 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
153 str2 = "{ [i,j] -> [min(i,j)] }";
154 if (test_parse_map_equal(ctx, str, str2) < 0)
155 return -1;
157 str = "{ [i,j] : i != j }";
158 str2 = "{ [i,j] : i < j or i > j }";
159 if (test_parse_map_equal(ctx, str, str2) < 0)
160 return -1;
162 str = "{ [i,j] : (i+1)*2 >= j }";
163 str2 = "{ [i, j] : j <= 2 + 2i }";
164 if (test_parse_map_equal(ctx, str, str2) < 0)
165 return -1;
167 str = "{ [i] -> [i > 0 ? 4 : 5] }";
168 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
169 if (test_parse_map_equal(ctx, str, str2) < 0)
170 return -1;
172 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
173 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
174 if (test_parse_map_equal(ctx, str, str2) < 0)
175 return -1;
177 str = "{ [x] : x >= 0 }";
178 str2 = "{ [x] : x-0 >= 0 }";
179 if (test_parse_map_equal(ctx, str, str2) < 0)
180 return -1;
182 str = "{ [i] : ((i > 10)) }";
183 str2 = "{ [i] : i >= 11 }";
184 if (test_parse_map_equal(ctx, str, str2) < 0)
185 return -1;
187 str = "{ [i] -> [0] }";
188 str2 = "{ [i] -> [0 * i] }";
189 if (test_parse_map_equal(ctx, str, str2) < 0)
190 return -1;
192 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
193 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
194 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
195 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
197 if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
198 "{ [a] -> [b] : true }") < 0)
199 return -1;
201 if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
202 "{ [i] : i <= 10 }") < 0)
203 return -1;
205 if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
206 "[n] -> { [i] : i <= n }") < 0)
207 return -1;
209 if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
210 return -1;
212 return 0;
215 void test_read(struct isl_ctx *ctx)
217 char *filename;
218 FILE *input;
219 struct isl_basic_set *bset1, *bset2;
220 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
222 filename = get_filename(ctx, "set", "omega");
223 assert(filename);
224 input = fopen(filename, "r");
225 assert(input);
227 bset1 = isl_basic_set_read_from_file(ctx, input);
228 bset2 = isl_basic_set_read_from_str(ctx, str);
230 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
232 isl_basic_set_free(bset1);
233 isl_basic_set_free(bset2);
234 free(filename);
236 fclose(input);
239 void test_bounded(struct isl_ctx *ctx)
241 isl_set *set;
242 int bounded;
244 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
245 bounded = isl_set_is_bounded(set);
246 assert(bounded);
247 isl_set_free(set);
249 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
250 bounded = isl_set_is_bounded(set);
251 assert(!bounded);
252 isl_set_free(set);
254 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
255 bounded = isl_set_is_bounded(set);
256 assert(!bounded);
257 isl_set_free(set);
260 /* Construct the basic set { [i] : 5 <= i <= N } */
261 void test_construction(struct isl_ctx *ctx)
263 isl_int v;
264 isl_space *dim;
265 isl_local_space *ls;
266 struct isl_basic_set *bset;
267 struct isl_constraint *c;
269 isl_int_init(v);
271 dim = isl_space_set_alloc(ctx, 1, 1);
272 bset = isl_basic_set_universe(isl_space_copy(dim));
273 ls = isl_local_space_from_space(dim);
275 c = isl_inequality_alloc(isl_local_space_copy(ls));
276 isl_int_set_si(v, -1);
277 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
278 isl_int_set_si(v, 1);
279 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
280 bset = isl_basic_set_add_constraint(bset, c);
282 c = isl_inequality_alloc(isl_local_space_copy(ls));
283 isl_int_set_si(v, 1);
284 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
285 isl_int_set_si(v, -5);
286 isl_constraint_set_constant(c, v);
287 bset = isl_basic_set_add_constraint(bset, c);
289 isl_local_space_free(ls);
290 isl_basic_set_free(bset);
292 isl_int_clear(v);
295 void test_dim(struct isl_ctx *ctx)
297 const char *str;
298 isl_map *map1, *map2;
300 map1 = isl_map_read_from_str(ctx,
301 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
302 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
303 map2 = isl_map_read_from_str(ctx,
304 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
305 assert(isl_map_is_equal(map1, map2));
306 isl_map_free(map2);
308 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
309 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
310 assert(isl_map_is_equal(map1, map2));
312 isl_map_free(map1);
313 isl_map_free(map2);
315 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
316 map1 = isl_map_read_from_str(ctx, str);
317 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
318 map2 = isl_map_read_from_str(ctx, str);
319 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
320 assert(isl_map_is_equal(map1, map2));
322 isl_map_free(map1);
323 isl_map_free(map2);
326 struct {
327 __isl_give isl_val *(*op)(__isl_take isl_val *v);
328 const char *arg;
329 const char *res;
330 } val_un_tests[] = {
331 { &isl_val_neg, "0", "0" },
332 { &isl_val_abs, "0", "0" },
333 { &isl_val_2exp, "0", "1" },
334 { &isl_val_floor, "0", "0" },
335 { &isl_val_ceil, "0", "0" },
336 { &isl_val_neg, "1", "-1" },
337 { &isl_val_neg, "-1", "1" },
338 { &isl_val_neg, "1/2", "-1/2" },
339 { &isl_val_neg, "-1/2", "1/2" },
340 { &isl_val_neg, "infty", "-infty" },
341 { &isl_val_neg, "-infty", "infty" },
342 { &isl_val_neg, "NaN", "NaN" },
343 { &isl_val_abs, "1", "1" },
344 { &isl_val_abs, "-1", "1" },
345 { &isl_val_abs, "1/2", "1/2" },
346 { &isl_val_abs, "-1/2", "1/2" },
347 { &isl_val_abs, "infty", "infty" },
348 { &isl_val_abs, "-infty", "infty" },
349 { &isl_val_abs, "NaN", "NaN" },
350 { &isl_val_floor, "1", "1" },
351 { &isl_val_floor, "-1", "-1" },
352 { &isl_val_floor, "1/2", "0" },
353 { &isl_val_floor, "-1/2", "-1" },
354 { &isl_val_floor, "infty", "infty" },
355 { &isl_val_floor, "-infty", "-infty" },
356 { &isl_val_floor, "NaN", "NaN" },
357 { &isl_val_ceil, "1", "1" },
358 { &isl_val_ceil, "-1", "-1" },
359 { &isl_val_ceil, "1/2", "1" },
360 { &isl_val_ceil, "-1/2", "0" },
361 { &isl_val_ceil, "infty", "infty" },
362 { &isl_val_ceil, "-infty", "-infty" },
363 { &isl_val_ceil, "NaN", "NaN" },
364 { &isl_val_2exp, "-3", "1/8" },
365 { &isl_val_2exp, "-1", "1/2" },
366 { &isl_val_2exp, "1", "2" },
367 { &isl_val_2exp, "2", "4" },
368 { &isl_val_2exp, "3", "8" },
371 /* Perform some basic tests of unary operations on isl_val objects.
373 static int test_un_val(isl_ctx *ctx)
375 int i;
376 isl_val *v, *res;
377 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
378 int ok;
380 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
381 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
382 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
383 fn = val_un_tests[i].op;
384 v = fn(v);
385 if (isl_val_is_nan(res))
386 ok = isl_val_is_nan(v);
387 else
388 ok = isl_val_eq(v, res);
389 isl_val_free(v);
390 isl_val_free(res);
391 if (ok < 0)
392 return -1;
393 if (!ok)
394 isl_die(ctx, isl_error_unknown,
395 "unexpected result", return -1);
398 return 0;
401 struct {
402 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
403 __isl_take isl_val *v2);
404 } val_bin_op[] = {
405 ['+'] = { &isl_val_add },
406 ['-'] = { &isl_val_sub },
407 ['*'] = { &isl_val_mul },
408 ['/'] = { &isl_val_div },
409 ['g'] = { &isl_val_gcd },
410 ['m'] = { &isl_val_min },
411 ['M'] = { &isl_val_max },
414 struct {
415 const char *arg1;
416 unsigned char op;
417 const char *arg2;
418 const char *res;
419 } val_bin_tests[] = {
420 { "0", '+', "0", "0" },
421 { "1", '+', "0", "1" },
422 { "1", '+', "1", "2" },
423 { "1", '-', "1", "0" },
424 { "1", '*', "1", "1" },
425 { "1", '/', "1", "1" },
426 { "2", '*', "3", "6" },
427 { "2", '*', "1/2", "1" },
428 { "2", '*', "1/3", "2/3" },
429 { "2/3", '*', "3/5", "2/5" },
430 { "2/3", '*', "7/5", "14/15" },
431 { "2", '/', "1/2", "4" },
432 { "-2", '/', "-1/2", "4" },
433 { "-2", '/', "1/2", "-4" },
434 { "2", '/', "-1/2", "-4" },
435 { "2", '/', "2", "1" },
436 { "2", '/', "3", "2/3" },
437 { "2/3", '/', "5/3", "2/5" },
438 { "2/3", '/', "5/7", "14/15" },
439 { "0", '/', "0", "NaN" },
440 { "42", '/', "0", "NaN" },
441 { "-42", '/', "0", "NaN" },
442 { "infty", '/', "0", "NaN" },
443 { "-infty", '/', "0", "NaN" },
444 { "NaN", '/', "0", "NaN" },
445 { "0", '/', "NaN", "NaN" },
446 { "42", '/', "NaN", "NaN" },
447 { "-42", '/', "NaN", "NaN" },
448 { "infty", '/', "NaN", "NaN" },
449 { "-infty", '/', "NaN", "NaN" },
450 { "NaN", '/', "NaN", "NaN" },
451 { "0", '/', "infty", "0" },
452 { "42", '/', "infty", "0" },
453 { "-42", '/', "infty", "0" },
454 { "infty", '/', "infty", "NaN" },
455 { "-infty", '/', "infty", "NaN" },
456 { "NaN", '/', "infty", "NaN" },
457 { "0", '/', "-infty", "0" },
458 { "42", '/', "-infty", "0" },
459 { "-42", '/', "-infty", "0" },
460 { "infty", '/', "-infty", "NaN" },
461 { "-infty", '/', "-infty", "NaN" },
462 { "NaN", '/', "-infty", "NaN" },
463 { "1", '-', "1/3", "2/3" },
464 { "1/3", '+', "1/2", "5/6" },
465 { "1/2", '+', "1/2", "1" },
466 { "3/4", '-', "1/4", "1/2" },
467 { "1/2", '-', "1/3", "1/6" },
468 { "infty", '+', "42", "infty" },
469 { "infty", '+', "infty", "infty" },
470 { "42", '+', "infty", "infty" },
471 { "infty", '-', "infty", "NaN" },
472 { "infty", '*', "infty", "infty" },
473 { "infty", '*', "-infty", "-infty" },
474 { "-infty", '*', "infty", "-infty" },
475 { "-infty", '*', "-infty", "infty" },
476 { "0", '*', "infty", "NaN" },
477 { "1", '*', "infty", "infty" },
478 { "infty", '*', "0", "NaN" },
479 { "infty", '*', "42", "infty" },
480 { "42", '-', "infty", "-infty" },
481 { "infty", '+', "-infty", "NaN" },
482 { "4", 'g', "6", "2" },
483 { "5", 'g', "6", "1" },
484 { "42", 'm', "3", "3" },
485 { "42", 'M', "3", "42" },
486 { "3", 'm', "42", "3" },
487 { "3", 'M', "42", "42" },
488 { "42", 'm', "infty", "42" },
489 { "42", 'M', "infty", "infty" },
490 { "42", 'm', "-infty", "-infty" },
491 { "42", 'M', "-infty", "42" },
492 { "42", 'm', "NaN", "NaN" },
493 { "42", 'M', "NaN", "NaN" },
494 { "infty", 'm', "-infty", "-infty" },
495 { "infty", 'M', "-infty", "infty" },
498 /* Perform some basic tests of binary operations on isl_val objects.
500 static int test_bin_val(isl_ctx *ctx)
502 int i;
503 isl_val *v1, *v2, *res;
504 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
505 __isl_take isl_val *v2);
506 int ok;
508 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
509 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
510 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
511 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
512 fn = val_bin_op[val_bin_tests[i].op].fn;
513 v1 = fn(v1, v2);
514 if (isl_val_is_nan(res))
515 ok = isl_val_is_nan(v1);
516 else
517 ok = isl_val_eq(v1, res);
518 isl_val_free(v1);
519 isl_val_free(res);
520 if (ok < 0)
521 return -1;
522 if (!ok)
523 isl_die(ctx, isl_error_unknown,
524 "unexpected result", return -1);
527 return 0;
530 /* Perform some basic tests on isl_val objects.
532 static int test_val(isl_ctx *ctx)
534 if (test_un_val(ctx) < 0)
535 return -1;
536 if (test_bin_val(ctx) < 0)
537 return -1;
538 return 0;
541 static int test_div(isl_ctx *ctx)
543 unsigned n;
544 const char *str;
545 int empty;
546 isl_int v;
547 isl_space *dim;
548 isl_set *set;
549 isl_local_space *ls;
550 struct isl_basic_set *bset;
551 struct isl_constraint *c;
553 isl_int_init(v);
555 /* test 1 */
556 dim = isl_space_set_alloc(ctx, 0, 3);
557 bset = isl_basic_set_universe(isl_space_copy(dim));
558 ls = isl_local_space_from_space(dim);
560 c = isl_equality_alloc(isl_local_space_copy(ls));
561 isl_int_set_si(v, -1);
562 isl_constraint_set_constant(c, v);
563 isl_int_set_si(v, 1);
564 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
565 isl_int_set_si(v, 3);
566 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
567 bset = isl_basic_set_add_constraint(bset, c);
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, 2, v);
576 bset = isl_basic_set_add_constraint(bset, c);
578 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
580 assert(bset && bset->n_div == 1);
581 isl_local_space_free(ls);
582 isl_basic_set_free(bset);
584 /* test 2 */
585 dim = isl_space_set_alloc(ctx, 0, 3);
586 bset = isl_basic_set_universe(isl_space_copy(dim));
587 ls = isl_local_space_from_space(dim);
589 c = isl_equality_alloc(isl_local_space_copy(ls));
590 isl_int_set_si(v, 1);
591 isl_constraint_set_constant(c, v);
592 isl_int_set_si(v, -1);
593 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
594 isl_int_set_si(v, 3);
595 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
596 bset = isl_basic_set_add_constraint(bset, c);
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, 2, v);
605 bset = isl_basic_set_add_constraint(bset, c);
607 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
609 assert(bset && bset->n_div == 1);
610 isl_local_space_free(ls);
611 isl_basic_set_free(bset);
613 /* test 3 */
614 dim = isl_space_set_alloc(ctx, 0, 3);
615 bset = isl_basic_set_universe(isl_space_copy(dim));
616 ls = isl_local_space_from_space(dim);
618 c = isl_equality_alloc(isl_local_space_copy(ls));
619 isl_int_set_si(v, 1);
620 isl_constraint_set_constant(c, v);
621 isl_int_set_si(v, -1);
622 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
623 isl_int_set_si(v, 3);
624 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
625 bset = isl_basic_set_add_constraint(bset, c);
627 c = isl_equality_alloc(isl_local_space_copy(ls));
628 isl_int_set_si(v, -3);
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, 4);
633 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
634 bset = isl_basic_set_add_constraint(bset, c);
636 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
638 assert(bset && bset->n_div == 1);
639 isl_local_space_free(ls);
640 isl_basic_set_free(bset);
642 /* test 4 */
643 dim = isl_space_set_alloc(ctx, 0, 3);
644 bset = isl_basic_set_universe(isl_space_copy(dim));
645 ls = isl_local_space_from_space(dim);
647 c = isl_equality_alloc(isl_local_space_copy(ls));
648 isl_int_set_si(v, 2);
649 isl_constraint_set_constant(c, v);
650 isl_int_set_si(v, -1);
651 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
652 isl_int_set_si(v, 3);
653 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
654 bset = isl_basic_set_add_constraint(bset, c);
656 c = isl_equality_alloc(isl_local_space_copy(ls));
657 isl_int_set_si(v, -1);
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, 6);
662 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
663 bset = isl_basic_set_add_constraint(bset, c);
665 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
667 assert(isl_basic_set_is_empty(bset));
668 isl_local_space_free(ls);
669 isl_basic_set_free(bset);
671 /* test 5 */
672 dim = isl_space_set_alloc(ctx, 0, 3);
673 bset = isl_basic_set_universe(isl_space_copy(dim));
674 ls = isl_local_space_from_space(dim);
676 c = isl_equality_alloc(isl_local_space_copy(ls));
677 isl_int_set_si(v, -1);
678 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
679 isl_int_set_si(v, 3);
680 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
681 bset = isl_basic_set_add_constraint(bset, c);
683 c = isl_equality_alloc(isl_local_space_copy(ls));
684 isl_int_set_si(v, 1);
685 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
686 isl_int_set_si(v, -3);
687 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
688 bset = isl_basic_set_add_constraint(bset, c);
690 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
692 assert(bset && bset->n_div == 0);
693 isl_basic_set_free(bset);
694 isl_local_space_free(ls);
696 /* test 6 */
697 dim = isl_space_set_alloc(ctx, 0, 3);
698 bset = isl_basic_set_universe(isl_space_copy(dim));
699 ls = isl_local_space_from_space(dim);
701 c = isl_equality_alloc(isl_local_space_copy(ls));
702 isl_int_set_si(v, -1);
703 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
704 isl_int_set_si(v, 6);
705 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
706 bset = isl_basic_set_add_constraint(bset, c);
708 c = isl_equality_alloc(isl_local_space_copy(ls));
709 isl_int_set_si(v, 1);
710 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
711 isl_int_set_si(v, -3);
712 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
713 bset = isl_basic_set_add_constraint(bset, c);
715 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
717 assert(bset && bset->n_div == 1);
718 isl_basic_set_free(bset);
719 isl_local_space_free(ls);
721 /* test 7 */
722 /* This test is a bit tricky. We set up an equality
723 * a + 3b + 3c = 6 e0
724 * Normalization of divs creates _two_ divs
725 * a = 3 e0
726 * c - b - e0 = 2 e1
727 * Afterwards e0 is removed again because it has coefficient -1
728 * and we end up with the original equality and div again.
729 * Perhaps we can avoid the introduction of this temporary div.
731 dim = isl_space_set_alloc(ctx, 0, 4);
732 bset = isl_basic_set_universe(isl_space_copy(dim));
733 ls = isl_local_space_from_space(dim);
735 c = isl_equality_alloc(isl_local_space_copy(ls));
736 isl_int_set_si(v, -1);
737 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
738 isl_int_set_si(v, -3);
739 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
740 isl_int_set_si(v, -3);
741 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
742 isl_int_set_si(v, 6);
743 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
744 bset = isl_basic_set_add_constraint(bset, c);
746 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
748 /* Test disabled for now */
750 assert(bset && bset->n_div == 1);
752 isl_local_space_free(ls);
753 isl_basic_set_free(bset);
755 /* test 8 */
756 dim = isl_space_set_alloc(ctx, 0, 5);
757 bset = isl_basic_set_universe(isl_space_copy(dim));
758 ls = isl_local_space_from_space(dim);
760 c = isl_equality_alloc(isl_local_space_copy(ls));
761 isl_int_set_si(v, -1);
762 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
763 isl_int_set_si(v, -3);
764 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
765 isl_int_set_si(v, -3);
766 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
767 isl_int_set_si(v, 6);
768 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
769 bset = isl_basic_set_add_constraint(bset, c);
771 c = isl_equality_alloc(isl_local_space_copy(ls));
772 isl_int_set_si(v, -1);
773 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
774 isl_int_set_si(v, 1);
775 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
776 isl_int_set_si(v, 1);
777 isl_constraint_set_constant(c, v);
778 bset = isl_basic_set_add_constraint(bset, c);
780 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
782 /* Test disabled for now */
784 assert(bset && bset->n_div == 1);
786 isl_local_space_free(ls);
787 isl_basic_set_free(bset);
789 /* test 9 */
790 dim = isl_space_set_alloc(ctx, 0, 4);
791 bset = isl_basic_set_universe(isl_space_copy(dim));
792 ls = isl_local_space_from_space(dim);
794 c = isl_equality_alloc(isl_local_space_copy(ls));
795 isl_int_set_si(v, 1);
796 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
797 isl_int_set_si(v, -1);
798 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
799 isl_int_set_si(v, -2);
800 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
801 bset = isl_basic_set_add_constraint(bset, c);
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, 3);
807 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
808 isl_int_set_si(v, 2);
809 isl_constraint_set_constant(c, v);
810 bset = isl_basic_set_add_constraint(bset, c);
812 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
814 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
816 assert(!isl_basic_set_is_empty(bset));
818 isl_local_space_free(ls);
819 isl_basic_set_free(bset);
821 /* test 10 */
822 dim = isl_space_set_alloc(ctx, 0, 3);
823 bset = isl_basic_set_universe(isl_space_copy(dim));
824 ls = isl_local_space_from_space(dim);
826 c = isl_equality_alloc(isl_local_space_copy(ls));
827 isl_int_set_si(v, 1);
828 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
829 isl_int_set_si(v, -2);
830 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
831 bset = isl_basic_set_add_constraint(bset, c);
833 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
835 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
837 isl_local_space_free(ls);
838 isl_basic_set_free(bset);
840 isl_int_clear(v);
842 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
843 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
844 set = isl_set_read_from_str(ctx, str);
845 set = isl_set_compute_divs(set);
846 isl_set_free(set);
847 if (!set)
848 return -1;
850 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
851 bset = isl_basic_set_read_from_str(ctx, str);
852 n = isl_basic_set_dim(bset, isl_dim_div);
853 isl_basic_set_free(bset);
854 if (!bset)
855 return -1;
856 if (n != 0)
857 isl_die(ctx, isl_error_unknown,
858 "expecting no existentials", return -1);
860 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
861 set = isl_set_read_from_str(ctx, str);
862 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
863 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
864 empty = isl_set_is_empty(set);
865 isl_set_free(set);
866 if (empty < 0)
867 return -1;
868 if (!empty)
869 isl_die(ctx, isl_error_unknown,
870 "result not as accurate as expected", return -1);
872 return 0;
875 void test_application_case(struct isl_ctx *ctx, const char *name)
877 char *filename;
878 FILE *input;
879 struct isl_basic_set *bset1, *bset2;
880 struct isl_basic_map *bmap;
882 filename = get_filename(ctx, name, "omega");
883 assert(filename);
884 input = fopen(filename, "r");
885 assert(input);
887 bset1 = isl_basic_set_read_from_file(ctx, input);
888 bmap = isl_basic_map_read_from_file(ctx, input);
890 bset1 = isl_basic_set_apply(bset1, bmap);
892 bset2 = isl_basic_set_read_from_file(ctx, input);
894 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
896 isl_basic_set_free(bset1);
897 isl_basic_set_free(bset2);
898 free(filename);
900 fclose(input);
903 void test_application(struct isl_ctx *ctx)
905 test_application_case(ctx, "application");
906 test_application_case(ctx, "application2");
909 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
911 char *filename;
912 FILE *input;
913 struct isl_basic_set *bset1, *bset2;
915 filename = get_filename(ctx, name, "polylib");
916 assert(filename);
917 input = fopen(filename, "r");
918 assert(input);
920 bset1 = isl_basic_set_read_from_file(ctx, input);
921 bset2 = isl_basic_set_read_from_file(ctx, input);
923 bset1 = isl_basic_set_affine_hull(bset1);
925 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
927 isl_basic_set_free(bset1);
928 isl_basic_set_free(bset2);
929 free(filename);
931 fclose(input);
934 int test_affine_hull(struct isl_ctx *ctx)
936 const char *str;
937 isl_set *set;
938 isl_basic_set *bset, *bset2;
939 int n;
940 int subset;
942 test_affine_hull_case(ctx, "affine2");
943 test_affine_hull_case(ctx, "affine");
944 test_affine_hull_case(ctx, "affine3");
946 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
947 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
948 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
949 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
950 set = isl_set_read_from_str(ctx, str);
951 bset = isl_set_affine_hull(set);
952 n = isl_basic_set_dim(bset, isl_dim_div);
953 isl_basic_set_free(bset);
954 if (n != 0)
955 isl_die(ctx, isl_error_unknown, "not expecting any divs",
956 return -1);
958 /* Check that isl_map_affine_hull is not confused by
959 * the reordering of divs in isl_map_align_divs.
961 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
962 "32e0 = b and 32e1 = c); "
963 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
964 set = isl_set_read_from_str(ctx, str);
965 bset = isl_set_affine_hull(set);
966 isl_basic_set_free(bset);
967 if (!bset)
968 return -1;
970 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
971 "32e2 = 31 + 31e0 }";
972 set = isl_set_read_from_str(ctx, str);
973 bset = isl_set_affine_hull(set);
974 str = "{ [a] : exists e : a = 32 e }";
975 bset2 = isl_basic_set_read_from_str(ctx, str);
976 subset = isl_basic_set_is_subset(bset, bset2);
977 isl_basic_set_free(bset);
978 isl_basic_set_free(bset2);
979 if (subset < 0)
980 return -1;
981 if (!subset)
982 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
983 return -1);
985 return 0;
988 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
990 char *filename;
991 FILE *input;
992 struct isl_basic_set *bset1, *bset2;
993 struct isl_set *set;
995 filename = get_filename(ctx, name, "polylib");
996 assert(filename);
997 input = fopen(filename, "r");
998 assert(input);
1000 bset1 = isl_basic_set_read_from_file(ctx, input);
1001 bset2 = isl_basic_set_read_from_file(ctx, input);
1003 set = isl_basic_set_union(bset1, bset2);
1004 bset1 = isl_set_convex_hull(set);
1006 bset2 = isl_basic_set_read_from_file(ctx, input);
1008 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1010 isl_basic_set_free(bset1);
1011 isl_basic_set_free(bset2);
1012 free(filename);
1014 fclose(input);
1017 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
1019 const char *str1, *str2;
1020 isl_set *set1, *set2;
1021 int orig_convex = ctx->opt->convex;
1022 ctx->opt->convex = convex;
1024 test_convex_hull_case(ctx, "convex0");
1025 test_convex_hull_case(ctx, "convex1");
1026 test_convex_hull_case(ctx, "convex2");
1027 test_convex_hull_case(ctx, "convex3");
1028 test_convex_hull_case(ctx, "convex4");
1029 test_convex_hull_case(ctx, "convex5");
1030 test_convex_hull_case(ctx, "convex6");
1031 test_convex_hull_case(ctx, "convex7");
1032 test_convex_hull_case(ctx, "convex8");
1033 test_convex_hull_case(ctx, "convex9");
1034 test_convex_hull_case(ctx, "convex10");
1035 test_convex_hull_case(ctx, "convex11");
1036 test_convex_hull_case(ctx, "convex12");
1037 test_convex_hull_case(ctx, "convex13");
1038 test_convex_hull_case(ctx, "convex14");
1039 test_convex_hull_case(ctx, "convex15");
1041 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1042 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1043 "(i0 = 0 and i1 = 0 and i2 = 0) }";
1044 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
1045 set1 = isl_set_read_from_str(ctx, str1);
1046 set2 = isl_set_read_from_str(ctx, str2);
1047 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1048 assert(isl_set_is_equal(set1, set2));
1049 isl_set_free(set1);
1050 isl_set_free(set2);
1052 ctx->opt->convex = orig_convex;
1055 void test_convex_hull(struct isl_ctx *ctx)
1057 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
1058 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
1061 void test_gist_case(struct isl_ctx *ctx, const char *name)
1063 char *filename;
1064 FILE *input;
1065 struct isl_basic_set *bset1, *bset2;
1067 filename = get_filename(ctx, name, "polylib");
1068 assert(filename);
1069 input = fopen(filename, "r");
1070 assert(input);
1072 bset1 = isl_basic_set_read_from_file(ctx, input);
1073 bset2 = isl_basic_set_read_from_file(ctx, input);
1075 bset1 = isl_basic_set_gist(bset1, bset2);
1077 bset2 = isl_basic_set_read_from_file(ctx, input);
1079 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1081 isl_basic_set_free(bset1);
1082 isl_basic_set_free(bset2);
1083 free(filename);
1085 fclose(input);
1088 void test_gist(struct isl_ctx *ctx)
1090 const char *str;
1091 isl_basic_set *bset1, *bset2;
1093 test_gist_case(ctx, "gist1");
1095 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1096 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1097 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1098 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1099 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1100 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1101 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1102 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1103 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1104 bset1 = isl_basic_set_read_from_str(ctx, str);
1105 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1106 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1107 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1108 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1109 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1110 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1111 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1112 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1113 bset2 = isl_basic_set_read_from_str(ctx, str);
1114 bset1 = isl_basic_set_gist(bset1, bset2);
1115 assert(bset1 && bset1->n_div == 0);
1116 isl_basic_set_free(bset1);
1119 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1121 isl_set *set, *set2;
1122 int equal;
1123 int one;
1125 set = isl_set_read_from_str(ctx, str);
1126 set = isl_set_coalesce(set);
1127 set2 = isl_set_read_from_str(ctx, str);
1128 equal = isl_set_is_equal(set, set2);
1129 one = set && set->n == 1;
1130 isl_set_free(set);
1131 isl_set_free(set2);
1133 if (equal < 0)
1134 return -1;
1135 if (!equal)
1136 isl_die(ctx, isl_error_unknown,
1137 "coalesced set not equal to input", return -1);
1138 if (check_one && !one)
1139 isl_die(ctx, isl_error_unknown,
1140 "coalesced set should not be a union", return -1);
1142 return 0;
1145 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1147 int r = 0;
1148 int bounded;
1150 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1151 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1153 if (test_coalesce_set(ctx,
1154 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1155 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1156 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1157 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1158 "-10 <= y <= 0}", 1) < 0)
1159 goto error;
1160 if (test_coalesce_set(ctx,
1161 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
1162 goto error;
1163 if (test_coalesce_set(ctx,
1164 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
1165 goto error;
1167 if (0) {
1168 error:
1169 r = -1;
1172 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1174 return r;
1177 /* Inputs for coalescing tests.
1178 * "str" is a string representation of the input set.
1179 * "single_disjunct" is set if we expect the result to consist of
1180 * a single disjunct.
1182 struct {
1183 int single_disjunct;
1184 const char *str;
1185 } coalesce_tests[] = {
1186 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1187 "y >= x & x >= 2 & 5 >= y }" },
1188 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1189 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1190 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1191 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1192 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1193 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1194 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1195 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1196 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1197 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1198 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1199 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1200 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1201 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1202 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1203 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1204 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1205 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1206 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1207 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1208 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1209 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1210 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1211 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1212 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1213 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1214 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1215 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1216 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1217 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1218 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1219 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1220 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1221 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1222 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1223 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1224 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1225 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1226 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1227 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1228 "[o0, o1, o2, o3, o4, o5, o6]] : "
1229 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1230 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1231 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1232 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1233 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1234 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1235 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1236 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1237 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1238 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1239 "o6 >= i3 + i6 - o3 and M >= 0 and "
1240 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1241 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1242 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1243 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1244 "(o0 = 0 and M >= 2 and N >= 3) or "
1245 "(M = 0 and o0 = 0 and N >= 3) }" },
1246 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1247 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1248 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1249 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1250 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1251 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1252 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1253 "(y = 3 and x = 1) }" },
1254 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1255 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1256 "i1 <= M and i3 <= M and i4 <= M) or "
1257 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1258 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1259 "i4 <= -1 + M) }" },
1260 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1261 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1262 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1263 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1264 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1265 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1266 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1267 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1268 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1269 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1270 { 0, "{ [a, b] : exists e : 2e = a and "
1271 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1272 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1273 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1274 "j >= 1 and j' <= i + j - i' and i >= 1; "
1275 "[1, 1, 1, 1] }" },
1276 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1277 "[i,j] : exists a : j = 3a }" },
1278 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1279 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1280 "a >= 3) or "
1281 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1282 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1283 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1284 "c <= 6 + 8a and a >= 3; "
1285 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1286 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1287 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1288 "[x,0] : 3 <= x <= 4 }" },
1289 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1290 "[x,0] : 4 <= x <= 5 }" },
1291 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1292 "[x,0] : 3 <= x <= 5 }" },
1293 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1294 "[x,0] : 3 <= x <= 4 }" },
1295 { 1 , "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1296 "i1 <= 0; "
1297 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1300 /* Test the functionality of isl_set_coalesce.
1301 * That is, check that the output is always equal to the input
1302 * and in some cases that the result consists of a single disjunct.
1304 static int test_coalesce(struct isl_ctx *ctx)
1306 int i;
1308 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1309 const char *str = coalesce_tests[i].str;
1310 int check_one = coalesce_tests[i].single_disjunct;
1311 if (test_coalesce_set(ctx, str, check_one) < 0)
1312 return -1;
1315 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1316 return -1;
1318 return 0;
1321 void test_closure(struct isl_ctx *ctx)
1323 const char *str;
1324 isl_set *dom;
1325 isl_map *up, *right;
1326 isl_map *map, *map2;
1327 int exact;
1329 /* COCOA example 1 */
1330 map = isl_map_read_from_str(ctx,
1331 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1332 "1 <= i and i < n and 1 <= j and j < n or "
1333 "i2 = i + 1 and j2 = j - 1 and "
1334 "1 <= i and i < n and 2 <= j and j <= n }");
1335 map = isl_map_power(map, &exact);
1336 assert(exact);
1337 isl_map_free(map);
1339 /* COCOA example 1 */
1340 map = isl_map_read_from_str(ctx,
1341 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1342 "1 <= i and i < n and 1 <= j and j < n or "
1343 "i2 = i + 1 and j2 = j - 1 and "
1344 "1 <= i and i < n and 2 <= j and j <= n }");
1345 map = isl_map_transitive_closure(map, &exact);
1346 assert(exact);
1347 map2 = isl_map_read_from_str(ctx,
1348 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1349 "1 <= i and i < n and 1 <= j and j <= n and "
1350 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1351 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1352 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1353 assert(isl_map_is_equal(map, map2));
1354 isl_map_free(map2);
1355 isl_map_free(map);
1357 map = isl_map_read_from_str(ctx,
1358 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1359 " 0 <= y and y <= n }");
1360 map = isl_map_transitive_closure(map, &exact);
1361 map2 = isl_map_read_from_str(ctx,
1362 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1363 " 0 <= y and y <= n }");
1364 assert(isl_map_is_equal(map, map2));
1365 isl_map_free(map2);
1366 isl_map_free(map);
1368 /* COCOA example 2 */
1369 map = isl_map_read_from_str(ctx,
1370 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1371 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1372 "i2 = i + 2 and j2 = j - 2 and "
1373 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1374 map = isl_map_transitive_closure(map, &exact);
1375 assert(exact);
1376 map2 = isl_map_read_from_str(ctx,
1377 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1378 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1379 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1380 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1381 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1382 assert(isl_map_is_equal(map, map2));
1383 isl_map_free(map);
1384 isl_map_free(map2);
1386 /* COCOA Fig.2 left */
1387 map = isl_map_read_from_str(ctx,
1388 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1389 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1390 "j <= n or "
1391 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1392 "j <= 2 i - 3 and j <= n - 2 or "
1393 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1394 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1395 map = isl_map_transitive_closure(map, &exact);
1396 assert(exact);
1397 isl_map_free(map);
1399 /* COCOA Fig.2 right */
1400 map = isl_map_read_from_str(ctx,
1401 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1402 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1403 "j <= n or "
1404 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1405 "j <= 2 i - 4 and j <= n - 3 or "
1406 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1407 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1408 map = isl_map_power(map, &exact);
1409 assert(exact);
1410 isl_map_free(map);
1412 /* COCOA Fig.2 right */
1413 map = isl_map_read_from_str(ctx,
1414 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1415 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1416 "j <= n or "
1417 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1418 "j <= 2 i - 4 and j <= n - 3 or "
1419 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1420 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1421 map = isl_map_transitive_closure(map, &exact);
1422 assert(exact);
1423 map2 = isl_map_read_from_str(ctx,
1424 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1425 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1426 "j <= n and 3 + i + 2 j <= 3 n and "
1427 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1428 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1429 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1430 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1431 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1432 assert(isl_map_is_equal(map, map2));
1433 isl_map_free(map2);
1434 isl_map_free(map);
1436 /* COCOA Fig.1 right */
1437 dom = isl_set_read_from_str(ctx,
1438 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1439 "2 x - 3 y + 3 >= 0 }");
1440 right = isl_map_read_from_str(ctx,
1441 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1442 up = isl_map_read_from_str(ctx,
1443 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1444 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1445 right = isl_map_intersect_range(right, isl_set_copy(dom));
1446 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1447 up = isl_map_intersect_range(up, dom);
1448 map = isl_map_union(up, right);
1449 map = isl_map_transitive_closure(map, &exact);
1450 assert(exact);
1451 map2 = isl_map_read_from_str(ctx,
1452 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1453 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1454 assert(isl_map_is_equal(map, map2));
1455 isl_map_free(map2);
1456 isl_map_free(map);
1458 /* COCOA Theorem 1 counter example */
1459 map = isl_map_read_from_str(ctx,
1460 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1461 "i2 = 1 and j2 = j or "
1462 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1463 map = isl_map_transitive_closure(map, &exact);
1464 assert(exact);
1465 isl_map_free(map);
1467 map = isl_map_read_from_str(ctx,
1468 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1469 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1470 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1471 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1472 map = isl_map_transitive_closure(map, &exact);
1473 assert(exact);
1474 isl_map_free(map);
1476 /* Kelly et al 1996, fig 12 */
1477 map = isl_map_read_from_str(ctx,
1478 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1479 "1 <= i,j,j+1 <= n or "
1480 "j = n and j2 = 1 and i2 = i + 1 and "
1481 "1 <= i,i+1 <= n }");
1482 map = isl_map_transitive_closure(map, &exact);
1483 assert(exact);
1484 map2 = isl_map_read_from_str(ctx,
1485 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1486 "1 <= i <= n and i = i2 or "
1487 "1 <= i < i2 <= n and 1 <= j <= n and "
1488 "1 <= j2 <= n }");
1489 assert(isl_map_is_equal(map, map2));
1490 isl_map_free(map2);
1491 isl_map_free(map);
1493 /* Omega's closure4 */
1494 map = isl_map_read_from_str(ctx,
1495 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1496 "1 <= x,y <= 10 or "
1497 "x2 = x + 1 and y2 = y and "
1498 "1 <= x <= 20 && 5 <= y <= 15 }");
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 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1505 map = isl_map_transitive_closure(map, &exact);
1506 assert(!exact);
1507 map2 = isl_map_read_from_str(ctx,
1508 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1509 assert(isl_map_is_equal(map, map2));
1510 isl_map_free(map);
1511 isl_map_free(map2);
1513 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1514 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1515 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1516 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1517 map = isl_map_read_from_str(ctx, str);
1518 map = isl_map_transitive_closure(map, &exact);
1519 assert(exact);
1520 map2 = isl_map_read_from_str(ctx, str);
1521 assert(isl_map_is_equal(map, map2));
1522 isl_map_free(map);
1523 isl_map_free(map2);
1525 str = "{[0] -> [1]; [2] -> [3]}";
1526 map = isl_map_read_from_str(ctx, str);
1527 map = isl_map_transitive_closure(map, &exact);
1528 assert(exact);
1529 map2 = isl_map_read_from_str(ctx, str);
1530 assert(isl_map_is_equal(map, map2));
1531 isl_map_free(map);
1532 isl_map_free(map2);
1534 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1535 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1536 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1537 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1538 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1539 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1540 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1541 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1542 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1543 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1544 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1545 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1546 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1547 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1548 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1549 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1550 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1551 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1552 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1553 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1554 map = isl_map_read_from_str(ctx, str);
1555 map = isl_map_transitive_closure(map, NULL);
1556 assert(map);
1557 isl_map_free(map);
1560 void test_lex(struct isl_ctx *ctx)
1562 isl_space *dim;
1563 isl_map *map;
1565 dim = isl_space_set_alloc(ctx, 0, 0);
1566 map = isl_map_lex_le(dim);
1567 assert(!isl_map_is_empty(map));
1568 isl_map_free(map);
1571 static int test_lexmin(struct isl_ctx *ctx)
1573 int equal;
1574 const char *str;
1575 isl_basic_map *bmap;
1576 isl_map *map, *map2;
1577 isl_set *set;
1578 isl_set *set2;
1579 isl_pw_multi_aff *pma;
1581 str = "[p0, p1] -> { [] -> [] : "
1582 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1583 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1584 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1585 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1586 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1587 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1588 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1589 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1590 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1591 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1592 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1593 map = isl_map_read_from_str(ctx, str);
1594 map = isl_map_lexmin(map);
1595 isl_map_free(map);
1597 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1598 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1599 set = isl_set_read_from_str(ctx, str);
1600 set = isl_set_lexmax(set);
1601 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1602 set2 = isl_set_read_from_str(ctx, str);
1603 set = isl_set_intersect(set, set2);
1604 assert(!isl_set_is_empty(set));
1605 isl_set_free(set);
1607 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1608 map = isl_map_read_from_str(ctx, str);
1609 map = isl_map_lexmin(map);
1610 str = "{ [x] -> [5] : 6 <= x <= 8; "
1611 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1612 map2 = isl_map_read_from_str(ctx, str);
1613 assert(isl_map_is_equal(map, map2));
1614 isl_map_free(map);
1615 isl_map_free(map2);
1617 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1618 map = isl_map_read_from_str(ctx, str);
1619 map2 = isl_map_copy(map);
1620 map = isl_map_lexmin(map);
1621 assert(isl_map_is_equal(map, map2));
1622 isl_map_free(map);
1623 isl_map_free(map2);
1625 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1626 map = isl_map_read_from_str(ctx, str);
1627 map = isl_map_lexmin(map);
1628 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1629 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1630 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1631 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1632 map2 = isl_map_read_from_str(ctx, str);
1633 assert(isl_map_is_equal(map, map2));
1634 isl_map_free(map);
1635 isl_map_free(map2);
1637 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1638 " 8i' <= i and 8i' >= -7 + i }";
1639 bmap = isl_basic_map_read_from_str(ctx, str);
1640 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1641 map2 = isl_map_from_pw_multi_aff(pma);
1642 map = isl_map_from_basic_map(bmap);
1643 assert(isl_map_is_equal(map, map2));
1644 isl_map_free(map);
1645 isl_map_free(map2);
1647 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1648 map = isl_map_read_from_str(ctx, str);
1649 map = isl_map_lexmin(map);
1650 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1651 map2 = isl_map_read_from_str(ctx, str);
1652 assert(isl_map_is_equal(map, map2));
1653 isl_map_free(map);
1654 isl_map_free(map2);
1656 /* Check that empty pieces are properly combined. */
1657 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1658 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1659 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1660 map = isl_map_read_from_str(ctx, str);
1661 map = isl_map_lexmin(map);
1662 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1663 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1664 "x >= 4 }";
1665 map2 = isl_map_read_from_str(ctx, str);
1666 assert(isl_map_is_equal(map, map2));
1667 isl_map_free(map);
1668 isl_map_free(map2);
1670 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1671 " 8i' <= i and 8i' >= -7 + i }";
1672 set = isl_set_read_from_str(ctx, str);
1673 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1674 set2 = isl_set_from_pw_multi_aff(pma);
1675 equal = isl_set_is_equal(set, set2);
1676 isl_set_free(set);
1677 isl_set_free(set2);
1678 if (equal < 0)
1679 return -1;
1680 if (!equal)
1681 isl_die(ctx, isl_error_unknown,
1682 "unexpected difference between set and "
1683 "piecewise affine expression", return -1);
1685 return 0;
1688 struct must_may {
1689 isl_map *must;
1690 isl_map *may;
1693 static int collect_must_may(__isl_take isl_map *dep, int must,
1694 void *dep_user, void *user)
1696 struct must_may *mm = (struct must_may *)user;
1698 if (must)
1699 mm->must = isl_map_union(mm->must, dep);
1700 else
1701 mm->may = isl_map_union(mm->may, dep);
1703 return 0;
1706 static int common_space(void *first, void *second)
1708 int depth = *(int *)first;
1709 return 2 * depth;
1712 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1714 isl_map *map2;
1715 int equal;
1717 if (!map)
1718 return -1;
1720 map2 = isl_map_read_from_str(map->ctx, str);
1721 equal = isl_map_is_equal(map, map2);
1722 isl_map_free(map2);
1724 return equal;
1727 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1729 int equal;
1731 equal = map_is_equal(map, str);
1732 if (equal < 0)
1733 return -1;
1734 if (!equal)
1735 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1736 "result not as expected", return -1);
1737 return 0;
1740 void test_dep(struct isl_ctx *ctx)
1742 const char *str;
1743 isl_space *dim;
1744 isl_map *map;
1745 isl_access_info *ai;
1746 isl_flow *flow;
1747 int depth;
1748 struct must_may mm;
1750 depth = 3;
1752 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1753 map = isl_map_read_from_str(ctx, str);
1754 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1756 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1757 map = isl_map_read_from_str(ctx, str);
1758 ai = isl_access_info_add_source(ai, map, 1, &depth);
1760 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1761 map = isl_map_read_from_str(ctx, str);
1762 ai = isl_access_info_add_source(ai, map, 1, &depth);
1764 flow = isl_access_info_compute_flow(ai);
1765 dim = isl_space_alloc(ctx, 0, 3, 3);
1766 mm.must = isl_map_empty(isl_space_copy(dim));
1767 mm.may = isl_map_empty(dim);
1769 isl_flow_foreach(flow, collect_must_may, &mm);
1771 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1772 " [1,10,0] -> [2,5,0] }";
1773 assert(map_is_equal(mm.must, str));
1774 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1775 assert(map_is_equal(mm.may, str));
1777 isl_map_free(mm.must);
1778 isl_map_free(mm.may);
1779 isl_flow_free(flow);
1782 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1783 map = isl_map_read_from_str(ctx, str);
1784 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1786 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1787 map = isl_map_read_from_str(ctx, str);
1788 ai = isl_access_info_add_source(ai, map, 1, &depth);
1790 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1791 map = isl_map_read_from_str(ctx, str);
1792 ai = isl_access_info_add_source(ai, map, 0, &depth);
1794 flow = isl_access_info_compute_flow(ai);
1795 dim = isl_space_alloc(ctx, 0, 3, 3);
1796 mm.must = isl_map_empty(isl_space_copy(dim));
1797 mm.may = isl_map_empty(dim);
1799 isl_flow_foreach(flow, collect_must_may, &mm);
1801 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1802 assert(map_is_equal(mm.must, str));
1803 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1804 assert(map_is_equal(mm.may, str));
1806 isl_map_free(mm.must);
1807 isl_map_free(mm.may);
1808 isl_flow_free(flow);
1811 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1812 map = isl_map_read_from_str(ctx, str);
1813 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1815 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1816 map = isl_map_read_from_str(ctx, str);
1817 ai = isl_access_info_add_source(ai, map, 0, &depth);
1819 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1820 map = isl_map_read_from_str(ctx, str);
1821 ai = isl_access_info_add_source(ai, map, 0, &depth);
1823 flow = isl_access_info_compute_flow(ai);
1824 dim = isl_space_alloc(ctx, 0, 3, 3);
1825 mm.must = isl_map_empty(isl_space_copy(dim));
1826 mm.may = isl_map_empty(dim);
1828 isl_flow_foreach(flow, collect_must_may, &mm);
1830 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1831 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1832 assert(map_is_equal(mm.may, str));
1833 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1834 assert(map_is_equal(mm.must, str));
1836 isl_map_free(mm.must);
1837 isl_map_free(mm.may);
1838 isl_flow_free(flow);
1841 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1842 map = isl_map_read_from_str(ctx, str);
1843 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1845 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1846 map = isl_map_read_from_str(ctx, str);
1847 ai = isl_access_info_add_source(ai, map, 0, &depth);
1849 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1850 map = isl_map_read_from_str(ctx, str);
1851 ai = isl_access_info_add_source(ai, map, 0, &depth);
1853 flow = isl_access_info_compute_flow(ai);
1854 dim = isl_space_alloc(ctx, 0, 3, 3);
1855 mm.must = isl_map_empty(isl_space_copy(dim));
1856 mm.may = isl_map_empty(dim);
1858 isl_flow_foreach(flow, collect_must_may, &mm);
1860 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1861 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1862 assert(map_is_equal(mm.may, str));
1863 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1864 assert(map_is_equal(mm.must, str));
1866 isl_map_free(mm.must);
1867 isl_map_free(mm.may);
1868 isl_flow_free(flow);
1871 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1872 map = isl_map_read_from_str(ctx, str);
1873 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1875 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1876 map = isl_map_read_from_str(ctx, str);
1877 ai = isl_access_info_add_source(ai, map, 0, &depth);
1879 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1880 map = isl_map_read_from_str(ctx, str);
1881 ai = isl_access_info_add_source(ai, map, 0, &depth);
1883 flow = isl_access_info_compute_flow(ai);
1884 dim = isl_space_alloc(ctx, 0, 3, 3);
1885 mm.must = isl_map_empty(isl_space_copy(dim));
1886 mm.may = isl_map_empty(dim);
1888 isl_flow_foreach(flow, collect_must_may, &mm);
1890 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1891 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1892 assert(map_is_equal(mm.may, str));
1893 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1894 assert(map_is_equal(mm.must, str));
1896 isl_map_free(mm.must);
1897 isl_map_free(mm.may);
1898 isl_flow_free(flow);
1901 depth = 5;
1903 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1904 map = isl_map_read_from_str(ctx, str);
1905 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1907 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1908 map = isl_map_read_from_str(ctx, str);
1909 ai = isl_access_info_add_source(ai, map, 1, &depth);
1911 flow = isl_access_info_compute_flow(ai);
1912 dim = isl_space_alloc(ctx, 0, 5, 5);
1913 mm.must = isl_map_empty(isl_space_copy(dim));
1914 mm.may = isl_map_empty(dim);
1916 isl_flow_foreach(flow, collect_must_may, &mm);
1918 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1919 assert(map_is_equal(mm.must, str));
1920 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1921 assert(map_is_equal(mm.may, str));
1923 isl_map_free(mm.must);
1924 isl_map_free(mm.may);
1925 isl_flow_free(flow);
1928 int test_sv(isl_ctx *ctx)
1930 const char *str;
1931 isl_map *map;
1932 isl_union_map *umap;
1933 int sv;
1935 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1936 map = isl_map_read_from_str(ctx, str);
1937 sv = isl_map_is_single_valued(map);
1938 isl_map_free(map);
1939 if (sv < 0)
1940 return -1;
1941 if (!sv)
1942 isl_die(ctx, isl_error_internal,
1943 "map not detected as single valued", return -1);
1945 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1946 map = isl_map_read_from_str(ctx, str);
1947 sv = isl_map_is_single_valued(map);
1948 isl_map_free(map);
1949 if (sv < 0)
1950 return -1;
1951 if (sv)
1952 isl_die(ctx, isl_error_internal,
1953 "map detected as single valued", return -1);
1955 str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
1956 umap = isl_union_map_read_from_str(ctx, str);
1957 sv = isl_union_map_is_single_valued(umap);
1958 isl_union_map_free(umap);
1959 if (sv < 0)
1960 return -1;
1961 if (!sv)
1962 isl_die(ctx, isl_error_internal,
1963 "map not detected as single valued", return -1);
1965 str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
1966 umap = isl_union_map_read_from_str(ctx, str);
1967 sv = isl_union_map_is_single_valued(umap);
1968 isl_union_map_free(umap);
1969 if (sv < 0)
1970 return -1;
1971 if (sv)
1972 isl_die(ctx, isl_error_internal,
1973 "map detected as single valued", return -1);
1975 return 0;
1978 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1980 isl_map *map;
1982 map = isl_map_read_from_str(ctx, str);
1983 if (bijective)
1984 assert(isl_map_is_bijective(map));
1985 else
1986 assert(!isl_map_is_bijective(map));
1987 isl_map_free(map);
1990 void test_bijective(struct isl_ctx *ctx)
1992 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1993 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1994 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1995 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1996 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1997 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1998 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1999 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2000 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2001 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2002 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2003 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2004 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2007 static int test_pwqp(struct isl_ctx *ctx)
2009 const char *str;
2010 isl_set *set;
2011 isl_pw_qpolynomial *pwqp1, *pwqp2;
2012 int equal;
2014 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2015 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2017 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2018 isl_dim_in, 1, 1);
2020 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2021 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2023 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2025 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2027 isl_pw_qpolynomial_free(pwqp1);
2029 str = "{ [i] -> i }";
2030 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2031 str = "{ [k] : exists a : k = 2a }";
2032 set = isl_set_read_from_str(ctx, str);
2033 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2034 str = "{ [i] -> i }";
2035 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2037 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2039 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2041 isl_pw_qpolynomial_free(pwqp1);
2043 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
2044 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2045 str = "{ [10] }";
2046 set = isl_set_read_from_str(ctx, str);
2047 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2048 str = "{ [i] -> 16 }";
2049 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2051 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2053 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2055 isl_pw_qpolynomial_free(pwqp1);
2057 str = "{ [i] -> ([(i)/2]) }";
2058 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2059 str = "{ [k] : exists a : k = 2a+1 }";
2060 set = isl_set_read_from_str(ctx, str);
2061 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2062 str = "{ [i] -> -1/2 + 1/2 * i }";
2063 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2065 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2067 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2069 isl_pw_qpolynomial_free(pwqp1);
2071 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2072 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2073 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2074 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2076 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2078 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2080 isl_pw_qpolynomial_free(pwqp1);
2082 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2083 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2084 str = "{ [x] -> x }";
2085 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2087 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2089 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2091 isl_pw_qpolynomial_free(pwqp1);
2093 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2094 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2095 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2096 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2097 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2098 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2099 isl_pw_qpolynomial_free(pwqp1);
2101 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2102 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2103 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2104 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2105 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2106 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2107 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2108 isl_pw_qpolynomial_free(pwqp1);
2109 isl_pw_qpolynomial_free(pwqp2);
2110 if (equal < 0)
2111 return -1;
2112 if (!equal)
2113 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2115 return 0;
2118 void test_split_periods(isl_ctx *ctx)
2120 const char *str;
2121 isl_pw_qpolynomial *pwqp;
2123 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2124 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2125 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2126 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2128 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2129 assert(pwqp);
2131 isl_pw_qpolynomial_free(pwqp);
2134 void test_union(isl_ctx *ctx)
2136 const char *str;
2137 isl_union_set *uset1, *uset2;
2138 isl_union_map *umap1, *umap2;
2140 str = "{ [i] : 0 <= i <= 1 }";
2141 uset1 = isl_union_set_read_from_str(ctx, str);
2142 str = "{ [1] -> [0] }";
2143 umap1 = isl_union_map_read_from_str(ctx, str);
2145 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2146 assert(isl_union_map_is_equal(umap1, umap2));
2148 isl_union_map_free(umap1);
2149 isl_union_map_free(umap2);
2151 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2152 umap1 = isl_union_map_read_from_str(ctx, str);
2153 str = "{ A[i]; B[i] }";
2154 uset1 = isl_union_set_read_from_str(ctx, str);
2156 uset2 = isl_union_map_domain(umap1);
2158 assert(isl_union_set_is_equal(uset1, uset2));
2160 isl_union_set_free(uset1);
2161 isl_union_set_free(uset2);
2164 void test_bound(isl_ctx *ctx)
2166 const char *str;
2167 isl_pw_qpolynomial *pwqp;
2168 isl_pw_qpolynomial_fold *pwf;
2170 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2171 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2172 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2173 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2174 isl_pw_qpolynomial_fold_free(pwf);
2176 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2177 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2178 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2179 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2180 isl_pw_qpolynomial_fold_free(pwf);
2183 void test_lift(isl_ctx *ctx)
2185 const char *str;
2186 isl_basic_map *bmap;
2187 isl_basic_set *bset;
2189 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2190 bset = isl_basic_set_read_from_str(ctx, str);
2191 bset = isl_basic_set_lift(bset);
2192 bmap = isl_basic_map_from_range(bset);
2193 bset = isl_basic_map_domain(bmap);
2194 isl_basic_set_free(bset);
2197 struct {
2198 const char *set1;
2199 const char *set2;
2200 int subset;
2201 } subset_tests[] = {
2202 { "{ [112, 0] }",
2203 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2204 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2205 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2206 { "{ [65] }",
2207 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2208 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2209 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2210 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2211 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2212 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2213 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2214 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2215 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2216 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2217 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2218 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2219 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2222 static int test_subset(isl_ctx *ctx)
2224 int i;
2225 isl_set *set1, *set2;
2226 int subset;
2228 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2229 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2230 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2231 subset = isl_set_is_subset(set1, set2);
2232 isl_set_free(set1);
2233 isl_set_free(set2);
2234 if (subset < 0)
2235 return -1;
2236 if (subset != subset_tests[i].subset)
2237 isl_die(ctx, isl_error_unknown,
2238 "incorrect subset result", return -1);
2241 return 0;
2244 struct {
2245 const char *minuend;
2246 const char *subtrahend;
2247 const char *difference;
2248 } subtract_domain_tests[] = {
2249 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2250 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2251 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2254 static int test_subtract(isl_ctx *ctx)
2256 int i;
2257 isl_union_map *umap1, *umap2;
2258 isl_union_set *uset;
2259 int equal;
2261 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2262 umap1 = isl_union_map_read_from_str(ctx,
2263 subtract_domain_tests[i].minuend);
2264 uset = isl_union_set_read_from_str(ctx,
2265 subtract_domain_tests[i].subtrahend);
2266 umap2 = isl_union_map_read_from_str(ctx,
2267 subtract_domain_tests[i].difference);
2268 umap1 = isl_union_map_subtract_domain(umap1, uset);
2269 equal = isl_union_map_is_equal(umap1, umap2);
2270 isl_union_map_free(umap1);
2271 isl_union_map_free(umap2);
2272 if (equal < 0)
2273 return -1;
2274 if (!equal)
2275 isl_die(ctx, isl_error_unknown,
2276 "incorrect subtract domain result", return -1);
2279 return 0;
2282 int test_factorize(isl_ctx *ctx)
2284 const char *str;
2285 isl_basic_set *bset;
2286 isl_factorizer *f;
2288 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2289 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2290 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2291 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2292 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2293 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2294 "3i5 >= -2i0 - i2 + 3i4 }";
2295 bset = isl_basic_set_read_from_str(ctx, str);
2296 f = isl_basic_set_factorizer(bset);
2297 isl_basic_set_free(bset);
2298 isl_factorizer_free(f);
2299 if (!f)
2300 isl_die(ctx, isl_error_unknown,
2301 "failed to construct factorizer", return -1);
2303 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2304 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2305 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2306 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2307 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2308 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2309 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2310 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2311 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2312 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2313 bset = isl_basic_set_read_from_str(ctx, str);
2314 f = isl_basic_set_factorizer(bset);
2315 isl_basic_set_free(bset);
2316 isl_factorizer_free(f);
2317 if (!f)
2318 isl_die(ctx, isl_error_unknown,
2319 "failed to construct factorizer", return -1);
2321 return 0;
2324 static int check_injective(__isl_take isl_map *map, void *user)
2326 int *injective = user;
2328 *injective = isl_map_is_injective(map);
2329 isl_map_free(map);
2331 if (*injective < 0 || !*injective)
2332 return -1;
2334 return 0;
2337 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2338 const char *r, const char *s, int tilable, int parallel)
2340 int i;
2341 isl_union_set *D;
2342 isl_union_map *W, *R, *S;
2343 isl_union_map *empty;
2344 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2345 isl_union_map *validity, *proximity;
2346 isl_union_map *schedule;
2347 isl_union_map *test;
2348 isl_union_set *delta;
2349 isl_union_set *domain;
2350 isl_set *delta_set;
2351 isl_set *slice;
2352 isl_set *origin;
2353 isl_schedule *sched;
2354 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2356 D = isl_union_set_read_from_str(ctx, d);
2357 W = isl_union_map_read_from_str(ctx, w);
2358 R = isl_union_map_read_from_str(ctx, r);
2359 S = isl_union_map_read_from_str(ctx, s);
2361 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2362 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2364 empty = isl_union_map_empty(isl_union_map_get_space(S));
2365 isl_union_map_compute_flow(isl_union_map_copy(R),
2366 isl_union_map_copy(W), empty,
2367 isl_union_map_copy(S),
2368 &dep_raw, NULL, NULL, NULL);
2369 isl_union_map_compute_flow(isl_union_map_copy(W),
2370 isl_union_map_copy(W),
2371 isl_union_map_copy(R),
2372 isl_union_map_copy(S),
2373 &dep_waw, &dep_war, NULL, NULL);
2375 dep = isl_union_map_union(dep_waw, dep_war);
2376 dep = isl_union_map_union(dep, dep_raw);
2377 validity = isl_union_map_copy(dep);
2378 proximity = isl_union_map_copy(dep);
2380 sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
2381 validity, proximity);
2382 schedule = isl_schedule_get_map(sched);
2383 isl_schedule_free(sched);
2384 isl_union_map_free(W);
2385 isl_union_map_free(R);
2386 isl_union_map_free(S);
2388 is_injection = 1;
2389 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2391 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2392 is_complete = isl_union_set_is_subset(D, domain);
2393 isl_union_set_free(D);
2394 isl_union_set_free(domain);
2396 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2397 test = isl_union_map_apply_range(test, dep);
2398 test = isl_union_map_apply_range(test, schedule);
2400 delta = isl_union_map_deltas(test);
2401 if (isl_union_set_n_set(delta) == 0) {
2402 is_tilable = 1;
2403 is_parallel = 1;
2404 is_nonneg = 1;
2405 isl_union_set_free(delta);
2406 } else {
2407 delta_set = isl_set_from_union_set(delta);
2409 slice = isl_set_universe(isl_set_get_space(delta_set));
2410 for (i = 0; i < tilable; ++i)
2411 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2412 is_tilable = isl_set_is_subset(delta_set, slice);
2413 isl_set_free(slice);
2415 slice = isl_set_universe(isl_set_get_space(delta_set));
2416 for (i = 0; i < parallel; ++i)
2417 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2418 is_parallel = isl_set_is_subset(delta_set, slice);
2419 isl_set_free(slice);
2421 origin = isl_set_universe(isl_set_get_space(delta_set));
2422 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2423 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2425 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2426 delta_set = isl_set_lexmin(delta_set);
2428 is_nonneg = isl_set_is_equal(delta_set, origin);
2430 isl_set_free(origin);
2431 isl_set_free(delta_set);
2434 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2435 is_injection < 0 || is_complete < 0)
2436 return -1;
2437 if (!is_complete)
2438 isl_die(ctx, isl_error_unknown,
2439 "generated schedule incomplete", return -1);
2440 if (!is_injection)
2441 isl_die(ctx, isl_error_unknown,
2442 "generated schedule not injective on each statement",
2443 return -1);
2444 if (!is_nonneg)
2445 isl_die(ctx, isl_error_unknown,
2446 "negative dependences in generated schedule",
2447 return -1);
2448 if (!is_tilable)
2449 isl_die(ctx, isl_error_unknown,
2450 "generated schedule not as tilable as expected",
2451 return -1);
2452 if (!is_parallel)
2453 isl_die(ctx, isl_error_unknown,
2454 "generated schedule not as parallel as expected",
2455 return -1);
2457 return 0;
2460 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2461 const char *domain, const char *validity, const char *proximity)
2463 isl_union_set *dom;
2464 isl_union_map *dep;
2465 isl_union_map *prox;
2466 isl_schedule *schedule;
2467 isl_union_map *sched;
2469 dom = isl_union_set_read_from_str(ctx, domain);
2470 dep = isl_union_map_read_from_str(ctx, validity);
2471 prox = isl_union_map_read_from_str(ctx, proximity);
2472 schedule = isl_union_set_compute_schedule(dom, dep, prox);
2473 sched = isl_schedule_get_map(schedule);
2474 isl_schedule_free(schedule);
2476 return sched;
2479 /* Check that a schedule can be constructed on the given domain
2480 * with the given validity and proximity constraints.
2482 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2483 const char *validity, const char *proximity)
2485 isl_union_map *sched;
2487 sched = compute_schedule(ctx, domain, validity, proximity);
2488 if (!sched)
2489 return -1;
2491 isl_union_map_free(sched);
2492 return 0;
2495 int test_special_schedule(isl_ctx *ctx, const char *domain,
2496 const char *validity, const char *proximity, const char *expected_sched)
2498 isl_union_map *sched1, *sched2;
2499 int equal;
2501 sched1 = compute_schedule(ctx, domain, validity, proximity);
2502 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2504 equal = isl_union_map_is_equal(sched1, sched2);
2505 isl_union_map_free(sched1);
2506 isl_union_map_free(sched2);
2508 if (equal < 0)
2509 return -1;
2510 if (!equal)
2511 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2512 return -1);
2514 return 0;
2517 /* Check that the schedule map is properly padded, even after being
2518 * reconstructed from the band forest.
2520 static int test_padded_schedule(isl_ctx *ctx)
2522 const char *str;
2523 isl_union_set *D;
2524 isl_union_map *validity, *proximity;
2525 isl_schedule *sched;
2526 isl_union_map *map1, *map2;
2527 isl_band_list *list;
2528 int equal;
2530 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2531 D = isl_union_set_read_from_str(ctx, str);
2532 validity = isl_union_map_empty(isl_union_set_get_space(D));
2533 proximity = isl_union_map_copy(validity);
2534 sched = isl_union_set_compute_schedule(D, validity, proximity);
2535 map1 = isl_schedule_get_map(sched);
2536 list = isl_schedule_get_band_forest(sched);
2537 isl_band_list_free(list);
2538 map2 = isl_schedule_get_map(sched);
2539 isl_schedule_free(sched);
2540 equal = isl_union_map_is_equal(map1, map2);
2541 isl_union_map_free(map1);
2542 isl_union_map_free(map2);
2544 if (equal < 0)
2545 return -1;
2546 if (!equal)
2547 isl_die(ctx, isl_error_unknown,
2548 "reconstructed schedule map not the same as original",
2549 return -1);
2551 return 0;
2554 int test_schedule(isl_ctx *ctx)
2556 const char *D, *W, *R, *V, *P, *S;
2558 /* Handle resulting schedule with zero bands. */
2559 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2560 return -1;
2562 /* Jacobi */
2563 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2564 W = "{ S1[t,i] -> a[t,i] }";
2565 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2566 "S1[t,i] -> a[t-1,i+1] }";
2567 S = "{ S1[t,i] -> [t,i] }";
2568 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2569 return -1;
2571 /* Fig. 5 of CC2008 */
2572 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2573 "j <= -1 + N }";
2574 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2575 "j >= 2 and j <= -1 + N }";
2576 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2577 "j >= 2 and j <= -1 + N; "
2578 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2579 "j >= 2 and j <= -1 + N }";
2580 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2581 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2582 return -1;
2584 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2585 W = "{ S1[i] -> a[i] }";
2586 R = "{ S2[i] -> a[i+1] }";
2587 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2588 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2589 return -1;
2591 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2592 W = "{ S1[i] -> a[i] }";
2593 R = "{ S2[i] -> a[9-i] }";
2594 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2595 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2596 return -1;
2598 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2599 W = "{ S1[i] -> a[i] }";
2600 R = "[N] -> { S2[i] -> a[N-1-i] }";
2601 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2602 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2603 return -1;
2605 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2606 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2607 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2608 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2609 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2610 return -1;
2612 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2613 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2614 R = "{ S2[i,j] -> a[i-1,j] }";
2615 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2616 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2617 return -1;
2619 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2620 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2621 R = "{ S2[i,j] -> a[i,j-1] }";
2622 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2623 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2624 return -1;
2626 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2627 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2628 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2629 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2630 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2631 "S_0[] -> [0, 0, 0] }";
2632 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2633 return -1;
2634 ctx->opt->schedule_parametric = 0;
2635 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2636 return -1;
2637 ctx->opt->schedule_parametric = 1;
2639 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2640 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2641 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2642 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2643 "S4[i] -> a[i,N] }";
2644 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2645 "S4[i] -> [4,i,0] }";
2646 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2647 return -1;
2649 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2650 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2651 "j <= N }";
2652 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2653 "j <= N; "
2654 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2655 "j <= N }";
2656 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2657 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2658 return -1;
2660 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2661 " S_2[t] : t >= 0 and t <= -1 + N; "
2662 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2663 "i <= -1 + N }";
2664 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2665 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2666 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2667 "i >= 0 and i <= -1 + N }";
2668 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2669 "i >= 0 and i <= -1 + N; "
2670 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2671 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2672 " S_0[t] -> [0, t, 0] }";
2674 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2675 return -1;
2676 ctx->opt->schedule_parametric = 0;
2677 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2678 return -1;
2679 ctx->opt->schedule_parametric = 1;
2681 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2682 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2683 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2684 return -1;
2686 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2687 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2688 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2689 "j >= 0 and j <= -1 + N; "
2690 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2691 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2692 "j >= 0 and j <= -1 + N; "
2693 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2694 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2695 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2696 return -1;
2698 D = "{ S_0[i] : i >= 0 }";
2699 W = "{ S_0[i] -> a[i] : i >= 0 }";
2700 R = "{ S_0[i] -> a[0] : i >= 0 }";
2701 S = "{ S_0[i] -> [0, i, 0] }";
2702 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2703 return -1;
2705 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2706 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2707 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2708 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2709 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2710 return -1;
2712 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2713 "k <= -1 + n and k >= 0 }";
2714 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
2715 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2716 "k <= -1 + n and k >= 0; "
2717 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2718 "k <= -1 + n and k >= 0; "
2719 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2720 "k <= -1 + n and k >= 0 }";
2721 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2722 ctx->opt->schedule_outer_zero_distance = 1;
2723 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2724 return -1;
2725 ctx->opt->schedule_outer_zero_distance = 0;
2727 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
2728 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
2729 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
2730 "Stmt_for_body24[i0, i1, 1, 0]:"
2731 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
2732 "Stmt_for_body7[i0, i1, i2]:"
2733 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
2734 "i2 <= 7 }";
2736 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
2737 "Stmt_for_body24[1, i1, i2, i3]:"
2738 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
2739 "i2 >= 1;"
2740 "Stmt_for_body24[0, i1, i2, i3] -> "
2741 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
2742 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
2743 "i3 >= 0;"
2744 "Stmt_for_body24[0, i1, i2, i3] ->"
2745 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
2746 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
2747 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
2748 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
2749 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
2750 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
2751 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
2752 "i1 <= 6 and i1 >= 0;"
2753 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
2754 "Stmt_for_body7[i0, i1, i2] -> "
2755 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
2756 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
2757 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
2758 "Stmt_for_body7[i0, i1, i2] -> "
2759 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
2760 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
2761 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
2762 P = V;
2763 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
2764 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
2765 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
2767 if (test_special_schedule(ctx, D, V, P, S) < 0)
2768 return -1;
2770 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
2771 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
2772 "j >= 1 and j <= 7;"
2773 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
2774 "j >= 1 and j <= 8 }";
2775 P = "{ }";
2776 S = "{ S_0[i, j] -> [i + j, j] }";
2777 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2778 if (test_special_schedule(ctx, D, V, P, S) < 0)
2779 return -1;
2780 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2782 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
2783 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
2784 "j >= 0 and j <= -1 + i }";
2785 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
2786 "i <= -1 + N and j >= 0;"
2787 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
2788 "i <= -2 + N }";
2789 P = "{ }";
2790 S = "{ S_0[i, j] -> [i, j] }";
2791 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2792 if (test_special_schedule(ctx, D, V, P, S) < 0)
2793 return -1;
2794 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2796 /* Test both algorithms on a case with only proximity dependences. */
2797 D = "{ S[i,j] : 0 <= i <= 10 }";
2798 V = "{ }";
2799 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2800 S = "{ S[i, j] -> [j, i] }";
2801 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2802 if (test_special_schedule(ctx, D, V, P, S) < 0)
2803 return -1;
2804 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2805 if (test_special_schedule(ctx, D, V, P, S) < 0)
2806 return -1;
2808 D = "{ A[a]; B[] }";
2809 V = "{}";
2810 P = "{ A[a] -> B[] }";
2811 if (test_has_schedule(ctx, D, V, P) < 0)
2812 return -1;
2814 if (test_padded_schedule(ctx) < 0)
2815 return -1;
2817 /* Check that check for progress is not confused by rational
2818 * solution.
2820 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
2821 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
2822 "i0 <= -2 + N; "
2823 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
2824 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
2825 P = "{}";
2826 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2827 if (test_has_schedule(ctx, D, V, P) < 0)
2828 return -1;
2829 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2831 return 0;
2834 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2836 isl_union_map *umap;
2837 int test;
2839 umap = isl_union_map_read_from_str(ctx, str);
2840 test = isl_union_map_plain_is_injective(umap);
2841 isl_union_map_free(umap);
2842 if (test < 0)
2843 return -1;
2844 if (test == injective)
2845 return 0;
2846 if (injective)
2847 isl_die(ctx, isl_error_unknown,
2848 "map not detected as injective", return -1);
2849 else
2850 isl_die(ctx, isl_error_unknown,
2851 "map detected as injective", return -1);
2854 int test_injective(isl_ctx *ctx)
2856 const char *str;
2858 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2859 return -1;
2860 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2861 return -1;
2862 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2863 return -1;
2864 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2865 return -1;
2866 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2867 return -1;
2868 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2869 return -1;
2870 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2871 return -1;
2872 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2873 return -1;
2875 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2876 if (test_plain_injective(ctx, str, 1))
2877 return -1;
2878 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2879 if (test_plain_injective(ctx, str, 0))
2880 return -1;
2882 return 0;
2885 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
2887 isl_aff *aff2;
2888 int equal;
2890 if (!aff)
2891 return -1;
2893 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
2894 equal = isl_aff_plain_is_equal(aff, aff2);
2895 isl_aff_free(aff2);
2897 return equal;
2900 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
2902 int equal;
2904 equal = aff_plain_is_equal(aff, str);
2905 if (equal < 0)
2906 return -1;
2907 if (!equal)
2908 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
2909 "result not as expected", return -1);
2910 return 0;
2913 int test_aff(isl_ctx *ctx)
2915 const char *str;
2916 isl_set *set;
2917 isl_space *space;
2918 isl_local_space *ls;
2919 isl_aff *aff;
2920 int zero, equal;
2922 space = isl_space_set_alloc(ctx, 0, 1);
2923 ls = isl_local_space_from_space(space);
2924 aff = isl_aff_zero_on_domain(ls);
2926 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2927 aff = isl_aff_scale_down_ui(aff, 3);
2928 aff = isl_aff_floor(aff);
2929 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2930 aff = isl_aff_scale_down_ui(aff, 2);
2931 aff = isl_aff_floor(aff);
2932 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2934 str = "{ [10] }";
2935 set = isl_set_read_from_str(ctx, str);
2936 aff = isl_aff_gist(aff, set);
2938 aff = isl_aff_add_constant_si(aff, -16);
2939 zero = isl_aff_plain_is_zero(aff);
2940 isl_aff_free(aff);
2942 if (zero < 0)
2943 return -1;
2944 if (!zero)
2945 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2947 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
2948 aff = isl_aff_scale_down_ui(aff, 64);
2949 aff = isl_aff_floor(aff);
2950 equal = aff_check_plain_equal(aff, "{ [-1] }");
2951 isl_aff_free(aff);
2952 if (equal < 0)
2953 return -1;
2955 return 0;
2958 int test_dim_max(isl_ctx *ctx)
2960 int equal;
2961 const char *str;
2962 isl_set *set1, *set2;
2963 isl_set *set;
2964 isl_map *map;
2965 isl_pw_aff *pwaff;
2967 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
2968 set = isl_set_read_from_str(ctx, str);
2969 pwaff = isl_set_dim_max(set, 0);
2970 set1 = isl_set_from_pw_aff(pwaff);
2971 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
2972 set2 = isl_set_read_from_str(ctx, str);
2973 equal = isl_set_is_equal(set1, set2);
2974 isl_set_free(set1);
2975 isl_set_free(set2);
2976 if (equal < 0)
2977 return -1;
2978 if (!equal)
2979 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2981 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
2982 set = isl_set_read_from_str(ctx, str);
2983 pwaff = isl_set_dim_max(set, 0);
2984 set1 = isl_set_from_pw_aff(pwaff);
2985 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2986 set2 = isl_set_read_from_str(ctx, str);
2987 equal = isl_set_is_equal(set1, set2);
2988 isl_set_free(set1);
2989 isl_set_free(set2);
2990 if (equal < 0)
2991 return -1;
2992 if (!equal)
2993 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2995 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
2996 set = isl_set_read_from_str(ctx, str);
2997 pwaff = isl_set_dim_max(set, 0);
2998 set1 = isl_set_from_pw_aff(pwaff);
2999 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3000 set2 = isl_set_read_from_str(ctx, str);
3001 equal = isl_set_is_equal(set1, set2);
3002 isl_set_free(set1);
3003 isl_set_free(set2);
3004 if (equal < 0)
3005 return -1;
3006 if (!equal)
3007 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3009 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3010 "0 <= i < N and 0 <= j < M }";
3011 map = isl_map_read_from_str(ctx, str);
3012 set = isl_map_range(map);
3014 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3015 set1 = isl_set_from_pw_aff(pwaff);
3016 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3017 set2 = isl_set_read_from_str(ctx, str);
3018 equal = isl_set_is_equal(set1, set2);
3019 isl_set_free(set1);
3020 isl_set_free(set2);
3022 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3023 set1 = isl_set_from_pw_aff(pwaff);
3024 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3025 set2 = isl_set_read_from_str(ctx, str);
3026 if (equal >= 0 && equal)
3027 equal = isl_set_is_equal(set1, set2);
3028 isl_set_free(set1);
3029 isl_set_free(set2);
3031 isl_set_free(set);
3033 if (equal < 0)
3034 return -1;
3035 if (!equal)
3036 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3038 /* Check that solutions are properly merged. */
3039 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3040 "c <= -1 + n - 4a - 2b and c >= -2b and "
3041 "4a >= -4 + n and c >= 0 }";
3042 set = isl_set_read_from_str(ctx, str);
3043 pwaff = isl_set_dim_min(set, 2);
3044 set1 = isl_set_from_pw_aff(pwaff);
3045 str = "[n] -> { [(0)] : n >= 1 }";
3046 set2 = isl_set_read_from_str(ctx, str);
3047 equal = isl_set_is_equal(set1, set2);
3048 isl_set_free(set1);
3049 isl_set_free(set2);
3051 if (equal < 0)
3052 return -1;
3053 if (!equal)
3054 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3056 return 0;
3059 int test_product(isl_ctx *ctx)
3061 const char *str;
3062 isl_set *set;
3063 isl_union_set *uset1, *uset2;
3064 int ok;
3066 str = "{ A[i] }";
3067 set = isl_set_read_from_str(ctx, str);
3068 set = isl_set_product(set, isl_set_copy(set));
3069 ok = isl_set_is_wrapping(set);
3070 isl_set_free(set);
3071 if (ok < 0)
3072 return -1;
3073 if (!ok)
3074 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3076 str = "{ [] }";
3077 uset1 = isl_union_set_read_from_str(ctx, str);
3078 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3079 str = "{ [[] -> []] }";
3080 uset2 = isl_union_set_read_from_str(ctx, str);
3081 ok = isl_union_set_is_equal(uset1, uset2);
3082 isl_union_set_free(uset1);
3083 isl_union_set_free(uset2);
3084 if (ok < 0)
3085 return -1;
3086 if (!ok)
3087 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3089 return 0;
3092 int test_equal(isl_ctx *ctx)
3094 const char *str;
3095 isl_set *set, *set2;
3096 int equal;
3098 str = "{ S_6[i] }";
3099 set = isl_set_read_from_str(ctx, str);
3100 str = "{ S_7[i] }";
3101 set2 = isl_set_read_from_str(ctx, str);
3102 equal = isl_set_is_equal(set, set2);
3103 isl_set_free(set);
3104 isl_set_free(set2);
3105 if (equal < 0)
3106 return -1;
3107 if (equal)
3108 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3110 return 0;
3113 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3114 enum isl_dim_type type, unsigned pos, int fixed)
3116 int test;
3118 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3119 isl_map_free(map);
3120 if (test < 0)
3121 return -1;
3122 if (test == fixed)
3123 return 0;
3124 if (fixed)
3125 isl_die(ctx, isl_error_unknown,
3126 "map not detected as fixed", return -1);
3127 else
3128 isl_die(ctx, isl_error_unknown,
3129 "map detected as fixed", return -1);
3132 int test_fixed(isl_ctx *ctx)
3134 const char *str;
3135 isl_map *map;
3137 str = "{ [i] -> [i] }";
3138 map = isl_map_read_from_str(ctx, str);
3139 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3140 return -1;
3141 str = "{ [i] -> [1] }";
3142 map = isl_map_read_from_str(ctx, str);
3143 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3144 return -1;
3145 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3146 map = isl_map_read_from_str(ctx, str);
3147 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3148 return -1;
3149 map = isl_map_read_from_str(ctx, str);
3150 map = isl_map_neg(map);
3151 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3152 return -1;
3154 return 0;
3157 int test_vertices(isl_ctx *ctx)
3159 const char *str;
3160 isl_basic_set *bset;
3161 isl_vertices *vertices;
3163 str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
3164 bset = isl_basic_set_read_from_str(ctx, str);
3165 vertices = isl_basic_set_compute_vertices(bset);
3166 isl_basic_set_free(bset);
3167 isl_vertices_free(vertices);
3168 if (!vertices)
3169 return -1;
3171 str = "{ A[t, i] : t = 14 and i = 1 }";
3172 bset = isl_basic_set_read_from_str(ctx, str);
3173 vertices = isl_basic_set_compute_vertices(bset);
3174 isl_basic_set_free(bset);
3175 isl_vertices_free(vertices);
3176 if (!vertices)
3177 return -1;
3179 return 0;
3182 int test_union_pw(isl_ctx *ctx)
3184 int equal;
3185 const char *str;
3186 isl_union_set *uset;
3187 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3189 str = "{ [x] -> x^2 }";
3190 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3191 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3192 uset = isl_union_pw_qpolynomial_domain(upwqp1);
3193 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3194 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3195 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3196 isl_union_pw_qpolynomial_free(upwqp1);
3197 isl_union_pw_qpolynomial_free(upwqp2);
3198 if (equal < 0)
3199 return -1;
3200 if (!equal)
3201 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3203 return 0;
3206 int test_output(isl_ctx *ctx)
3208 char *s;
3209 const char *str;
3210 isl_pw_aff *pa;
3211 isl_printer *p;
3212 int equal;
3214 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3215 pa = isl_pw_aff_read_from_str(ctx, str);
3217 p = isl_printer_to_str(ctx);
3218 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3219 p = isl_printer_print_pw_aff(p, pa);
3220 s = isl_printer_get_str(p);
3221 isl_printer_free(p);
3222 isl_pw_aff_free(pa);
3223 if (!s)
3224 equal = -1;
3225 else
3226 equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
3227 free(s);
3228 if (equal < 0)
3229 return -1;
3230 if (!equal)
3231 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3233 return 0;
3236 int test_sample(isl_ctx *ctx)
3238 const char *str;
3239 isl_basic_set *bset1, *bset2;
3240 int empty, subset;
3242 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3243 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3244 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3245 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3246 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3247 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3248 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3249 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3250 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3251 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3252 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3253 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3254 "d - 1073741821e and "
3255 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3256 "3j >= 1 - a + b + 2e and "
3257 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3258 "3i <= 4 - a + 4b - e and "
3259 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3260 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3261 "c <= -1 + a and 3i >= -2 - a + 3e and "
3262 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3263 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3264 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3265 "1073741823e >= 1 + 1073741823b - d and "
3266 "3i >= 1073741823b + c - 1073741823e - f and "
3267 "3i >= 1 + 2b + e + 3g }";
3268 bset1 = isl_basic_set_read_from_str(ctx, str);
3269 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3270 empty = isl_basic_set_is_empty(bset2);
3271 subset = isl_basic_set_is_subset(bset2, bset1);
3272 isl_basic_set_free(bset1);
3273 isl_basic_set_free(bset2);
3274 if (empty < 0 || subset < 0)
3275 return -1;
3276 if (empty)
3277 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3278 if (!subset)
3279 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3281 return 0;
3284 int test_fixed_power(isl_ctx *ctx)
3286 const char *str;
3287 isl_map *map;
3288 isl_int exp;
3289 int equal;
3291 isl_int_init(exp);
3292 str = "{ [i] -> [i + 1] }";
3293 map = isl_map_read_from_str(ctx, str);
3294 isl_int_set_si(exp, 23);
3295 map = isl_map_fixed_power(map, exp);
3296 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3297 isl_int_clear(exp);
3298 isl_map_free(map);
3299 if (equal < 0)
3300 return -1;
3302 return 0;
3305 int test_slice(isl_ctx *ctx)
3307 const char *str;
3308 isl_map *map;
3309 int equal;
3311 str = "{ [i] -> [j] }";
3312 map = isl_map_read_from_str(ctx, str);
3313 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3314 equal = map_check_equal(map, "{ [i] -> [i] }");
3315 isl_map_free(map);
3316 if (equal < 0)
3317 return -1;
3319 str = "{ [i] -> [j] }";
3320 map = isl_map_read_from_str(ctx, str);
3321 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
3322 equal = map_check_equal(map, "{ [i] -> [j] }");
3323 isl_map_free(map);
3324 if (equal < 0)
3325 return -1;
3327 str = "{ [i] -> [j] }";
3328 map = isl_map_read_from_str(ctx, str);
3329 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
3330 equal = map_check_equal(map, "{ [i] -> [-i] }");
3331 isl_map_free(map);
3332 if (equal < 0)
3333 return -1;
3335 str = "{ [i] -> [j] }";
3336 map = isl_map_read_from_str(ctx, str);
3337 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
3338 equal = map_check_equal(map, "{ [0] -> [j] }");
3339 isl_map_free(map);
3340 if (equal < 0)
3341 return -1;
3343 str = "{ [i] -> [j] }";
3344 map = isl_map_read_from_str(ctx, str);
3345 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3346 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3347 isl_map_free(map);
3348 if (equal < 0)
3349 return -1;
3351 str = "{ [i] -> [j] }";
3352 map = isl_map_read_from_str(ctx, str);
3353 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3354 equal = map_check_equal(map, "{ [i] -> [j] : false }");
3355 isl_map_free(map);
3356 if (equal < 0)
3357 return -1;
3359 return 0;
3362 int test_eliminate(isl_ctx *ctx)
3364 const char *str;
3365 isl_map *map;
3366 int equal;
3368 str = "{ [i] -> [j] : i = 2j }";
3369 map = isl_map_read_from_str(ctx, str);
3370 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3371 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3372 isl_map_free(map);
3373 if (equal < 0)
3374 return -1;
3376 return 0;
3379 /* Check that isl_set_dim_residue_class detects that the values of j
3380 * in the set below are all odd and that it does not detect any spurious
3381 * strides.
3383 static int test_residue_class(isl_ctx *ctx)
3385 const char *str;
3386 isl_set *set;
3387 isl_int m, r;
3388 int res;
3390 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3391 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3392 set = isl_set_read_from_str(ctx, str);
3393 isl_int_init(m);
3394 isl_int_init(r);
3395 res = isl_set_dim_residue_class(set, 1, &m, &r);
3396 if (res >= 0 &&
3397 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3398 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3399 res = -1);
3400 isl_int_clear(r);
3401 isl_int_clear(m);
3402 isl_set_free(set);
3404 return res;
3407 int test_align_parameters(isl_ctx *ctx)
3409 const char *str;
3410 isl_space *space;
3411 isl_multi_aff *ma1, *ma2;
3412 int equal;
3414 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3415 ma1 = isl_multi_aff_read_from_str(ctx, str);
3417 space = isl_space_params_alloc(ctx, 1);
3418 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
3419 ma1 = isl_multi_aff_align_params(ma1, space);
3421 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3422 ma2 = isl_multi_aff_read_from_str(ctx, str);
3424 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3426 isl_multi_aff_free(ma1);
3427 isl_multi_aff_free(ma2);
3429 if (equal < 0)
3430 return -1;
3431 if (!equal)
3432 isl_die(ctx, isl_error_unknown,
3433 "result not as expected", return -1);
3435 return 0;
3438 static int test_list(isl_ctx *ctx)
3440 isl_id *a, *b, *c, *d, *id;
3441 isl_id_list *list;
3442 int ok;
3444 a = isl_id_alloc(ctx, "a", NULL);
3445 b = isl_id_alloc(ctx, "b", NULL);
3446 c = isl_id_alloc(ctx, "c", NULL);
3447 d = isl_id_alloc(ctx, "d", NULL);
3449 list = isl_id_list_alloc(ctx, 4);
3450 list = isl_id_list_add(list, a);
3451 list = isl_id_list_add(list, b);
3452 list = isl_id_list_add(list, c);
3453 list = isl_id_list_add(list, d);
3454 list = isl_id_list_drop(list, 1, 1);
3456 if (isl_id_list_n_id(list) != 3) {
3457 isl_id_list_free(list);
3458 isl_die(ctx, isl_error_unknown,
3459 "unexpected number of elements in list", return -1);
3462 id = isl_id_list_get_id(list, 0);
3463 ok = id == a;
3464 isl_id_free(id);
3465 id = isl_id_list_get_id(list, 1);
3466 ok = ok && id == c;
3467 isl_id_free(id);
3468 id = isl_id_list_get_id(list, 2);
3469 ok = ok && id == d;
3470 isl_id_free(id);
3472 isl_id_list_free(list);
3474 if (!ok)
3475 isl_die(ctx, isl_error_unknown,
3476 "unexpected elements in list", return -1);
3478 return 0;
3481 const char *set_conversion_tests[] = {
3482 "[N] -> { [i] : N - 1 <= 2 i <= N }",
3483 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
3484 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3485 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3488 /* Check that converting from isl_set to isl_pw_multi_aff and back
3489 * to isl_set produces the original isl_set.
3491 static int test_set_conversion(isl_ctx *ctx)
3493 int i;
3494 const char *str;
3495 isl_set *set1, *set2;
3496 isl_pw_multi_aff *pma;
3497 int equal;
3499 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
3500 str = set_conversion_tests[i];
3501 set1 = isl_set_read_from_str(ctx, str);
3502 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
3503 set2 = isl_set_from_pw_multi_aff(pma);
3504 equal = isl_set_is_equal(set1, set2);
3505 isl_set_free(set1);
3506 isl_set_free(set2);
3508 if (equal < 0)
3509 return -1;
3510 if (!equal)
3511 isl_die(ctx, isl_error_unknown, "bad conversion",
3512 return -1);
3515 return 0;
3518 /* Check that converting from isl_map to isl_pw_multi_aff and back
3519 * to isl_map produces the original isl_map.
3521 static int test_map_conversion(isl_ctx *ctx)
3523 const char *str;
3524 isl_map *map1, *map2;
3525 isl_pw_multi_aff *pma;
3526 int equal;
3528 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
3529 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
3530 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
3531 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
3532 "9e <= -2 - 2a) }";
3533 map1 = isl_map_read_from_str(ctx, str);
3534 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
3535 map2 = isl_map_from_pw_multi_aff(pma);
3536 equal = isl_map_is_equal(map1, map2);
3537 isl_map_free(map1);
3538 isl_map_free(map2);
3540 if (equal < 0)
3541 return -1;
3542 if (!equal)
3543 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
3545 return 0;
3548 static int test_conversion(isl_ctx *ctx)
3550 if (test_set_conversion(ctx) < 0)
3551 return -1;
3552 if (test_map_conversion(ctx) < 0)
3553 return -1;
3554 return 0;
3557 /* Check that isl_basic_map_curry does not modify input.
3559 static int test_curry(isl_ctx *ctx)
3561 const char *str;
3562 isl_basic_map *bmap1, *bmap2;
3563 int equal;
3565 str = "{ [A[] -> B[]] -> C[] }";
3566 bmap1 = isl_basic_map_read_from_str(ctx, str);
3567 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
3568 equal = isl_basic_map_is_equal(bmap1, bmap2);
3569 isl_basic_map_free(bmap1);
3570 isl_basic_map_free(bmap2);
3572 if (equal < 0)
3573 return -1;
3574 if (equal)
3575 isl_die(ctx, isl_error_unknown,
3576 "curried map should not be equal to original",
3577 return -1);
3579 return 0;
3582 struct {
3583 const char *set;
3584 const char *ma;
3585 const char *res;
3586 } preimage_tests[] = {
3587 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
3588 "{ A[j,i] -> B[i,j] }",
3589 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
3590 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3591 "{ A[a,b] -> B[a/2,b/6] }",
3592 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
3593 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3594 "{ A[a,b] -> B[a/2,b/6] }",
3595 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
3596 "exists i,j : a = 2 i and b = 6 j }" },
3597 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
3598 "[n] -> { : 0 <= n <= 100 }" },
3599 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3600 "{ A[a] -> B[2a] }",
3601 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
3602 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3603 "{ A[a] -> B[([a/2])] }",
3604 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
3605 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
3606 "{ A[a] -> B[a,a,a/3] }",
3607 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
3608 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
3609 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
3612 static int test_preimage_basic_set(isl_ctx *ctx)
3614 int i;
3615 isl_basic_set *bset1, *bset2;
3616 isl_multi_aff *ma;
3617 int equal;
3619 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
3620 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
3621 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
3622 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
3623 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
3624 equal = isl_basic_set_is_equal(bset1, bset2);
3625 isl_basic_set_free(bset1);
3626 isl_basic_set_free(bset2);
3627 if (equal < 0)
3628 return -1;
3629 if (!equal)
3630 isl_die(ctx, isl_error_unknown, "bad preimage",
3631 return -1);
3634 return 0;
3637 struct {
3638 const char *map;
3639 const char *ma;
3640 const char *res;
3641 } preimage_domain_tests[] = {
3642 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
3643 "{ A[j,i] -> B[i,j] }",
3644 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
3645 { "{ B[i] -> C[i]; D[i] -> E[i] }",
3646 "{ A[i] -> B[i + 1] }",
3647 "{ A[i] -> C[i + 1] }" },
3648 { "{ B[i] -> C[i]; B[i] -> E[i] }",
3649 "{ A[i] -> B[i + 1] }",
3650 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
3651 { "{ B[i] -> C[([i/2])] }",
3652 "{ A[i] -> B[2i] }",
3653 "{ A[i] -> C[i] }" },
3654 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
3655 "{ A[i] -> B[([i/5]), ([i/7])] }",
3656 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
3657 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
3658 "[N] -> { A[] -> B[([N/5])] }",
3659 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
3660 { "{ B[i] -> C[i] : exists a : i = 5 a }",
3661 "{ A[i] -> B[2i] }",
3662 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
3663 { "{ B[i] -> C[i] : exists a : i = 2 a; "
3664 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
3665 "{ A[i] -> B[2i] }",
3666 "{ A[i] -> C[2i] }" },
3669 static int test_preimage_union_map(isl_ctx *ctx)
3671 int i;
3672 isl_union_map *umap1, *umap2;
3673 isl_multi_aff *ma;
3674 int equal;
3676 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
3677 umap1 = isl_union_map_read_from_str(ctx,
3678 preimage_domain_tests[i].map);
3679 ma = isl_multi_aff_read_from_str(ctx,
3680 preimage_domain_tests[i].ma);
3681 umap2 = isl_union_map_read_from_str(ctx,
3682 preimage_domain_tests[i].res);
3683 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
3684 equal = isl_union_map_is_equal(umap1, umap2);
3685 isl_union_map_free(umap1);
3686 isl_union_map_free(umap2);
3687 if (equal < 0)
3688 return -1;
3689 if (!equal)
3690 isl_die(ctx, isl_error_unknown, "bad preimage",
3691 return -1);
3694 return 0;
3697 static int test_preimage(isl_ctx *ctx)
3699 if (test_preimage_basic_set(ctx) < 0)
3700 return -1;
3701 if (test_preimage_union_map(ctx) < 0)
3702 return -1;
3704 return 0;
3707 struct {
3708 const char *ma1;
3709 const char *ma;
3710 const char *res;
3711 } pullback_tests[] = {
3712 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
3713 "{ A[a,b] -> C[b + 2a] }" },
3714 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
3715 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
3716 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
3717 "{ A[a] -> C[(a)/6] }" },
3718 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
3719 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
3720 "{ A[a] -> C[(2a)/3] }" },
3721 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
3722 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
3723 "{ A[i,j] -> C[i + j, i + j] }"},
3724 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
3725 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
3726 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
3729 static int test_pullback(isl_ctx *ctx)
3731 int i;
3732 isl_multi_aff *ma1, *ma2;
3733 isl_multi_aff *ma;
3734 int equal;
3736 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
3737 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
3738 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
3739 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
3740 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
3741 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3742 isl_multi_aff_free(ma1);
3743 isl_multi_aff_free(ma2);
3744 if (equal < 0)
3745 return -1;
3746 if (!equal)
3747 isl_die(ctx, isl_error_unknown, "bad pullback",
3748 return -1);
3751 return 0;
3754 /* Check that negation is printed correctly.
3756 static int test_ast(isl_ctx *ctx)
3758 isl_ast_expr *expr, *expr1, *expr2, *expr3;
3759 char *str;
3760 int ok;
3762 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3763 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3764 expr = isl_ast_expr_add(expr1, expr2);
3765 expr = isl_ast_expr_neg(expr);
3766 str = isl_ast_expr_to_str(expr);
3767 ok = str ? !strcmp(str, "-(A + B)") : -1;
3768 free(str);
3769 isl_ast_expr_free(expr);
3771 if (ok < 0)
3772 return -1;
3773 if (!ok)
3774 isl_die(ctx, isl_error_unknown,
3775 "isl_ast_expr printed incorrectly", return -1);
3777 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3778 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3779 expr = isl_ast_expr_add(expr1, expr2);
3780 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
3781 expr = isl_ast_expr_sub(expr3, expr);
3782 str = isl_ast_expr_to_str(expr);
3783 ok = str ? !strcmp(str, "C - (A + B)") : -1;
3784 free(str);
3785 isl_ast_expr_free(expr);
3787 if (ok < 0)
3788 return -1;
3789 if (!ok)
3790 isl_die(ctx, isl_error_unknown,
3791 "isl_ast_expr printed incorrectly", return -1);
3793 return 0;
3796 /* Internal data structure for before_for and after_for callbacks.
3798 * depth is the current depth
3799 * before is the number of times before_for has been called
3800 * after is the number of times after_for has been called
3802 struct isl_test_codegen_data {
3803 int depth;
3804 int before;
3805 int after;
3808 /* This function is called before each for loop in the AST generated
3809 * from test_ast_gen1.
3811 * Increment the number of calls and the depth.
3812 * Check that the space returned by isl_ast_build_get_schedule_space
3813 * matches the target space of the schedule returned by
3814 * isl_ast_build_get_schedule.
3815 * Return an isl_id that is checked by the corresponding call
3816 * to after_for.
3818 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
3819 void *user)
3821 struct isl_test_codegen_data *data = user;
3822 isl_ctx *ctx;
3823 isl_space *space;
3824 isl_union_map *schedule;
3825 isl_union_set *uset;
3826 isl_set *set;
3827 int empty;
3828 char name[] = "d0";
3830 ctx = isl_ast_build_get_ctx(build);
3832 if (data->before >= 3)
3833 isl_die(ctx, isl_error_unknown,
3834 "unexpected number of for nodes", return NULL);
3835 if (data->depth >= 2)
3836 isl_die(ctx, isl_error_unknown,
3837 "unexpected depth", return NULL);
3839 snprintf(name, sizeof(name), "d%d", data->depth);
3840 data->before++;
3841 data->depth++;
3843 schedule = isl_ast_build_get_schedule(build);
3844 uset = isl_union_map_range(schedule);
3845 if (!uset)
3846 return NULL;
3847 if (isl_union_set_n_set(uset) != 1) {
3848 isl_union_set_free(uset);
3849 isl_die(ctx, isl_error_unknown,
3850 "expecting single range space", return NULL);
3853 space = isl_ast_build_get_schedule_space(build);
3854 set = isl_union_set_extract_set(uset, space);
3855 isl_union_set_free(uset);
3856 empty = isl_set_is_empty(set);
3857 isl_set_free(set);
3859 if (empty < 0)
3860 return NULL;
3861 if (empty)
3862 isl_die(ctx, isl_error_unknown,
3863 "spaces don't match", return NULL);
3865 return isl_id_alloc(ctx, name, NULL);
3868 /* This function is called after each for loop in the AST generated
3869 * from test_ast_gen1.
3871 * Increment the number of calls and decrement the depth.
3872 * Check that the annotation attached to the node matches
3873 * the isl_id returned by the corresponding call to before_for.
3875 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
3876 __isl_keep isl_ast_build *build, void *user)
3878 struct isl_test_codegen_data *data = user;
3879 isl_id *id;
3880 const char *name;
3881 int valid;
3883 data->after++;
3884 data->depth--;
3886 if (data->after > data->before)
3887 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3888 "mismatch in number of for nodes",
3889 return isl_ast_node_free(node));
3891 id = isl_ast_node_get_annotation(node);
3892 if (!id)
3893 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3894 "missing annotation", return isl_ast_node_free(node));
3896 name = isl_id_get_name(id);
3897 valid = name && atoi(name + 1) == data->depth;
3898 isl_id_free(id);
3900 if (!valid)
3901 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3902 "wrong annotation", return isl_ast_node_free(node));
3904 return node;
3907 /* Check that the before_each_for and after_each_for callbacks
3908 * are called for each for loop in the generated code,
3909 * that they are called in the right order and that the isl_id
3910 * returned from the before_each_for callback is attached to
3911 * the isl_ast_node passed to the corresponding after_each_for call.
3913 static int test_ast_gen1(isl_ctx *ctx)
3915 const char *str;
3916 isl_set *set;
3917 isl_union_map *schedule;
3918 isl_ast_build *build;
3919 isl_ast_node *tree;
3920 struct isl_test_codegen_data data;
3922 str = "[N] -> { : N >= 10 }";
3923 set = isl_set_read_from_str(ctx, str);
3924 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
3925 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
3926 schedule = isl_union_map_read_from_str(ctx, str);
3928 data.before = 0;
3929 data.after = 0;
3930 data.depth = 0;
3931 build = isl_ast_build_from_context(set);
3932 build = isl_ast_build_set_before_each_for(build,
3933 &before_for, &data);
3934 build = isl_ast_build_set_after_each_for(build,
3935 &after_for, &data);
3936 tree = isl_ast_build_ast_from_schedule(build, schedule);
3937 isl_ast_build_free(build);
3938 if (!tree)
3939 return -1;
3941 isl_ast_node_free(tree);
3943 if (data.before != 3 || data.after != 3)
3944 isl_die(ctx, isl_error_unknown,
3945 "unexpected number of for nodes", return -1);
3947 return 0;
3950 /* Check that the AST generator handles domains that are integrally disjoint
3951 * but not ratinoally disjoint.
3953 static int test_ast_gen2(isl_ctx *ctx)
3955 const char *str;
3956 isl_set *set;
3957 isl_union_map *schedule;
3958 isl_union_map *options;
3959 isl_ast_build *build;
3960 isl_ast_node *tree;
3962 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
3963 schedule = isl_union_map_read_from_str(ctx, str);
3964 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3965 build = isl_ast_build_from_context(set);
3967 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
3968 options = isl_union_map_read_from_str(ctx, str);
3969 build = isl_ast_build_set_options(build, options);
3970 tree = isl_ast_build_ast_from_schedule(build, schedule);
3971 isl_ast_build_free(build);
3972 if (!tree)
3973 return -1;
3974 isl_ast_node_free(tree);
3976 return 0;
3979 /* Increment *user on each call.
3981 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
3982 __isl_keep isl_ast_build *build, void *user)
3984 int *n = user;
3986 (*n)++;
3988 return node;
3991 /* Test that unrolling tries to minimize the number of instances.
3992 * In particular, for the schedule given below, make sure it generates
3993 * 3 nodes (rather than 101).
3995 static int test_ast_gen3(isl_ctx *ctx)
3997 const char *str;
3998 isl_set *set;
3999 isl_union_map *schedule;
4000 isl_union_map *options;
4001 isl_ast_build *build;
4002 isl_ast_node *tree;
4003 int n_domain = 0;
4005 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4006 schedule = isl_union_map_read_from_str(ctx, str);
4007 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4009 str = "{ [i] -> unroll[0] }";
4010 options = isl_union_map_read_from_str(ctx, str);
4012 build = isl_ast_build_from_context(set);
4013 build = isl_ast_build_set_options(build, options);
4014 build = isl_ast_build_set_at_each_domain(build,
4015 &count_domains, &n_domain);
4016 tree = isl_ast_build_ast_from_schedule(build, schedule);
4017 isl_ast_build_free(build);
4018 if (!tree)
4019 return -1;
4021 isl_ast_node_free(tree);
4023 if (n_domain != 3)
4024 isl_die(ctx, isl_error_unknown,
4025 "unexpected number of for nodes", return -1);
4027 return 0;
4030 /* Check that if the ast_build_exploit_nested_bounds options is set,
4031 * we do not get an outer if node in the generated AST,
4032 * while we do get such an outer if node if the options is not set.
4034 static int test_ast_gen4(isl_ctx *ctx)
4036 const char *str;
4037 isl_set *set;
4038 isl_union_map *schedule;
4039 isl_ast_build *build;
4040 isl_ast_node *tree;
4041 enum isl_ast_node_type type;
4042 int enb;
4044 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4045 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4047 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4049 schedule = isl_union_map_read_from_str(ctx, str);
4050 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4051 build = isl_ast_build_from_context(set);
4052 tree = isl_ast_build_ast_from_schedule(build, schedule);
4053 isl_ast_build_free(build);
4054 if (!tree)
4055 return -1;
4057 type = isl_ast_node_get_type(tree);
4058 isl_ast_node_free(tree);
4060 if (type == isl_ast_node_if)
4061 isl_die(ctx, isl_error_unknown,
4062 "not expecting if node", return -1);
4064 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4066 schedule = isl_union_map_read_from_str(ctx, str);
4067 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4068 build = isl_ast_build_from_context(set);
4069 tree = isl_ast_build_ast_from_schedule(build, schedule);
4070 isl_ast_build_free(build);
4071 if (!tree)
4072 return -1;
4074 type = isl_ast_node_get_type(tree);
4075 isl_ast_node_free(tree);
4077 if (type != isl_ast_node_if)
4078 isl_die(ctx, isl_error_unknown,
4079 "expecting if node", return -1);
4081 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4083 return 0;
4086 /* This function is called for each leaf in the AST generated
4087 * from test_ast_gen5.
4089 * We finalize the AST generation by extending the outer schedule
4090 * with a zero-dimensional schedule. If this results in any for loops,
4091 * then this means that we did not pass along enough information
4092 * about the outer schedule to the inner AST generation.
4094 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4095 void *user)
4097 isl_union_map *schedule, *extra;
4098 isl_ast_node *tree;
4100 schedule = isl_ast_build_get_schedule(build);
4101 extra = isl_union_map_copy(schedule);
4102 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4103 schedule = isl_union_map_range_product(schedule, extra);
4104 tree = isl_ast_build_ast_from_schedule(build, schedule);
4105 isl_ast_build_free(build);
4107 if (!tree)
4108 return NULL;
4110 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4111 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4112 "code should not contain any for loop",
4113 return isl_ast_node_free(tree));
4115 return tree;
4118 /* Check that we do not lose any information when going back and
4119 * forth between internal and external schedule.
4121 * In particular, we create an AST where we unroll the only
4122 * non-constant dimension in the schedule. We therefore do
4123 * not expect any for loops in the AST. However, older versions
4124 * of isl would not pass along enough information about the outer
4125 * schedule when performing an inner code generation from a create_leaf
4126 * callback, resulting in the inner code generation producing a for loop.
4128 static int test_ast_gen5(isl_ctx *ctx)
4130 const char *str;
4131 isl_set *set;
4132 isl_union_map *schedule, *options;
4133 isl_ast_build *build;
4134 isl_ast_node *tree;
4136 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4137 schedule = isl_union_map_read_from_str(ctx, str);
4139 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4140 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4141 options = isl_union_map_read_from_str(ctx, str);
4143 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4144 build = isl_ast_build_from_context(set);
4145 build = isl_ast_build_set_options(build, options);
4146 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4147 tree = isl_ast_build_ast_from_schedule(build, schedule);
4148 isl_ast_build_free(build);
4149 isl_ast_node_free(tree);
4150 if (!tree)
4151 return -1;
4153 return 0;
4156 static int test_ast_gen(isl_ctx *ctx)
4158 if (test_ast_gen1(ctx) < 0)
4159 return -1;
4160 if (test_ast_gen2(ctx) < 0)
4161 return -1;
4162 if (test_ast_gen3(ctx) < 0)
4163 return -1;
4164 if (test_ast_gen4(ctx) < 0)
4165 return -1;
4166 if (test_ast_gen5(ctx) < 0)
4167 return -1;
4168 return 0;
4171 /* Check if dropping output dimensions from an isl_pw_multi_aff
4172 * works properly.
4174 static int test_pw_multi_aff(isl_ctx *ctx)
4176 const char *str;
4177 isl_pw_multi_aff *pma1, *pma2;
4178 int equal;
4180 str = "{ [i,j] -> [i+j, 4i-j] }";
4181 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4182 str = "{ [i,j] -> [4i-j] }";
4183 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4185 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4187 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4189 isl_pw_multi_aff_free(pma1);
4190 isl_pw_multi_aff_free(pma2);
4191 if (equal < 0)
4192 return -1;
4193 if (!equal)
4194 isl_die(ctx, isl_error_unknown,
4195 "expressions not equal", return -1);
4197 return 0;
4200 /* This is a regression test for a bug where isl_basic_map_simplify
4201 * would end up in an infinite loop. In particular, we construct
4202 * an empty basic set that is not obviously empty.
4203 * isl_basic_set_is_empty marks the basic set as empty.
4204 * After projecting out i3, the variable can be dropped completely,
4205 * but isl_basic_map_simplify refrains from doing so if the basic set
4206 * is empty and would end up in an infinite loop if it didn't test
4207 * explicitly for empty basic maps in the outer loop.
4209 static int test_simplify(isl_ctx *ctx)
4211 const char *str;
4212 isl_basic_set *bset;
4213 int empty;
4215 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
4216 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
4217 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
4218 "i3 >= i2 }";
4219 bset = isl_basic_set_read_from_str(ctx, str);
4220 empty = isl_basic_set_is_empty(bset);
4221 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
4222 isl_basic_set_free(bset);
4223 if (!bset)
4224 return -1;
4225 if (!empty)
4226 isl_die(ctx, isl_error_unknown,
4227 "basic set should be empty", return -1);
4229 return 0;
4232 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
4233 * with gbr context would fail to disable the use of the shifted tableau
4234 * when transferring equalities for the input to the context, resulting
4235 * in invalid sample values.
4237 static int test_partial_lexmin(isl_ctx *ctx)
4239 const char *str;
4240 isl_basic_set *bset;
4241 isl_basic_map *bmap;
4242 isl_map *map;
4244 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
4245 bmap = isl_basic_map_read_from_str(ctx, str);
4246 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
4247 bset = isl_basic_set_read_from_str(ctx, str);
4248 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
4249 isl_map_free(map);
4251 if (!map)
4252 return -1;
4254 return 0;
4257 /* Check that the variable compression performed on the existentially
4258 * quantified variables inside isl_basic_set_compute_divs is not confused
4259 * by the implicit equalities among the parameters.
4261 static int test_compute_divs(isl_ctx *ctx)
4263 const char *str;
4264 isl_basic_set *bset;
4265 isl_set *set;
4267 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
4268 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
4269 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
4270 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
4271 bset = isl_basic_set_read_from_str(ctx, str);
4272 set = isl_basic_set_compute_divs(bset);
4273 isl_set_free(set);
4274 if (!set)
4275 return -1;
4277 return 0;
4280 struct {
4281 const char *name;
4282 int (*fn)(isl_ctx *ctx);
4283 } tests [] = {
4284 { "val", &test_val },
4285 { "compute divs", &test_compute_divs },
4286 { "partial lexmin", &test_partial_lexmin },
4287 { "simplify", &test_simplify },
4288 { "curry", &test_curry },
4289 { "piecewise multi affine expressions", &test_pw_multi_aff },
4290 { "conversion", &test_conversion },
4291 { "list", &test_list },
4292 { "align parameters", &test_align_parameters },
4293 { "preimage", &test_preimage },
4294 { "pullback", &test_pullback },
4295 { "AST", &test_ast },
4296 { "AST generation", &test_ast_gen },
4297 { "eliminate", &test_eliminate },
4298 { "residue class", &test_residue_class },
4299 { "div", &test_div },
4300 { "slice", &test_slice },
4301 { "fixed power", &test_fixed_power },
4302 { "sample", &test_sample },
4303 { "output", &test_output },
4304 { "vertices", &test_vertices },
4305 { "fixed", &test_fixed },
4306 { "equal", &test_equal },
4307 { "product", &test_product },
4308 { "dim_max", &test_dim_max },
4309 { "affine", &test_aff },
4310 { "injective", &test_injective },
4311 { "schedule", &test_schedule },
4312 { "union_pw", &test_union_pw },
4313 { "parse", &test_parse },
4314 { "single-valued", &test_sv },
4315 { "affine hull", &test_affine_hull },
4316 { "coalesce", &test_coalesce },
4317 { "factorize", &test_factorize },
4318 { "subset", &test_subset },
4319 { "subtract", &test_subtract },
4320 { "lexmin", &test_lexmin },
4321 { "piecewise quasi-polynomials", &test_pwqp },
4324 int main()
4326 int i;
4327 struct isl_ctx *ctx;
4329 srcdir = getenv("srcdir");
4330 assert(srcdir);
4332 ctx = isl_ctx_alloc();
4333 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
4334 printf("%s\n", tests[i].name);
4335 if (tests[i].fn(ctx) < 0)
4336 goto error;
4338 test_lift(ctx);
4339 test_bound(ctx);
4340 test_union(ctx);
4341 test_split_periods(ctx);
4342 test_lex(ctx);
4343 test_bijective(ctx);
4344 test_dep(ctx);
4345 test_read(ctx);
4346 test_bounded(ctx);
4347 test_construction(ctx);
4348 test_dim(ctx);
4349 test_application(ctx);
4350 test_convex_hull(ctx);
4351 test_gist(ctx);
4352 test_closure(ctx);
4353 isl_ctx_free(ctx);
4354 return 0;
4355 error:
4356 isl_ctx_free(ctx);
4357 return -1;