add isl_ast_expr_from_val
[isl.git] / isl_test.c
blobdf7de06deb64d9519479a5e34c89350c462c1b0a
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 void test_pwqp(struct isl_ctx *ctx)
2009 const char *str;
2010 isl_set *set;
2011 isl_pw_qpolynomial *pwqp1, *pwqp2;
2013 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2014 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2016 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2017 isl_dim_in, 1, 1);
2019 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2020 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2022 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2024 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2026 isl_pw_qpolynomial_free(pwqp1);
2028 str = "{ [i] -> i }";
2029 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2030 str = "{ [k] : exists a : k = 2a }";
2031 set = isl_set_read_from_str(ctx, str);
2032 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2033 str = "{ [i] -> i }";
2034 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2036 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2038 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2040 isl_pw_qpolynomial_free(pwqp1);
2042 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
2043 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2044 str = "{ [10] }";
2045 set = isl_set_read_from_str(ctx, str);
2046 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2047 str = "{ [i] -> 16 }";
2048 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2050 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2052 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2054 isl_pw_qpolynomial_free(pwqp1);
2056 str = "{ [i] -> ([(i)/2]) }";
2057 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2058 str = "{ [k] : exists a : k = 2a+1 }";
2059 set = isl_set_read_from_str(ctx, str);
2060 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2061 str = "{ [i] -> -1/2 + 1/2 * i }";
2062 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2064 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2066 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2068 isl_pw_qpolynomial_free(pwqp1);
2070 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2071 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2072 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2073 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2075 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2077 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2079 isl_pw_qpolynomial_free(pwqp1);
2081 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2082 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2083 str = "{ [x] -> x }";
2084 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2086 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2088 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2090 isl_pw_qpolynomial_free(pwqp1);
2092 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2093 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2094 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2095 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2096 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2097 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2098 isl_pw_qpolynomial_free(pwqp1);
2101 void test_split_periods(isl_ctx *ctx)
2103 const char *str;
2104 isl_pw_qpolynomial *pwqp;
2106 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2107 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2108 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2109 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2111 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2112 assert(pwqp);
2114 isl_pw_qpolynomial_free(pwqp);
2117 void test_union(isl_ctx *ctx)
2119 const char *str;
2120 isl_union_set *uset1, *uset2;
2121 isl_union_map *umap1, *umap2;
2123 str = "{ [i] : 0 <= i <= 1 }";
2124 uset1 = isl_union_set_read_from_str(ctx, str);
2125 str = "{ [1] -> [0] }";
2126 umap1 = isl_union_map_read_from_str(ctx, str);
2128 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2129 assert(isl_union_map_is_equal(umap1, umap2));
2131 isl_union_map_free(umap1);
2132 isl_union_map_free(umap2);
2134 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2135 umap1 = isl_union_map_read_from_str(ctx, str);
2136 str = "{ A[i]; B[i] }";
2137 uset1 = isl_union_set_read_from_str(ctx, str);
2139 uset2 = isl_union_map_domain(umap1);
2141 assert(isl_union_set_is_equal(uset1, uset2));
2143 isl_union_set_free(uset1);
2144 isl_union_set_free(uset2);
2147 void test_bound(isl_ctx *ctx)
2149 const char *str;
2150 isl_pw_qpolynomial *pwqp;
2151 isl_pw_qpolynomial_fold *pwf;
2153 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2154 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2155 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2156 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2157 isl_pw_qpolynomial_fold_free(pwf);
2159 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2160 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2161 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2162 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2163 isl_pw_qpolynomial_fold_free(pwf);
2166 void test_lift(isl_ctx *ctx)
2168 const char *str;
2169 isl_basic_map *bmap;
2170 isl_basic_set *bset;
2172 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2173 bset = isl_basic_set_read_from_str(ctx, str);
2174 bset = isl_basic_set_lift(bset);
2175 bmap = isl_basic_map_from_range(bset);
2176 bset = isl_basic_map_domain(bmap);
2177 isl_basic_set_free(bset);
2180 struct {
2181 const char *set1;
2182 const char *set2;
2183 int subset;
2184 } subset_tests[] = {
2185 { "{ [112, 0] }",
2186 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2187 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2188 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2189 { "{ [65] }",
2190 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2191 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2192 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2193 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2194 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2195 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2196 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2197 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2198 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2199 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2200 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2201 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2202 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2205 static int test_subset(isl_ctx *ctx)
2207 int i;
2208 isl_set *set1, *set2;
2209 int subset;
2211 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2212 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2213 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2214 subset = isl_set_is_subset(set1, set2);
2215 isl_set_free(set1);
2216 isl_set_free(set2);
2217 if (subset < 0)
2218 return -1;
2219 if (subset != subset_tests[i].subset)
2220 isl_die(ctx, isl_error_unknown,
2221 "incorrect subset result", return -1);
2224 return 0;
2227 struct {
2228 const char *minuend;
2229 const char *subtrahend;
2230 const char *difference;
2231 } subtract_domain_tests[] = {
2232 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2233 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2234 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2237 static int test_subtract(isl_ctx *ctx)
2239 int i;
2240 isl_union_map *umap1, *umap2;
2241 isl_union_set *uset;
2242 int equal;
2244 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2245 umap1 = isl_union_map_read_from_str(ctx,
2246 subtract_domain_tests[i].minuend);
2247 uset = isl_union_set_read_from_str(ctx,
2248 subtract_domain_tests[i].subtrahend);
2249 umap2 = isl_union_map_read_from_str(ctx,
2250 subtract_domain_tests[i].difference);
2251 umap1 = isl_union_map_subtract_domain(umap1, uset);
2252 equal = isl_union_map_is_equal(umap1, umap2);
2253 isl_union_map_free(umap1);
2254 isl_union_map_free(umap2);
2255 if (equal < 0)
2256 return -1;
2257 if (!equal)
2258 isl_die(ctx, isl_error_unknown,
2259 "incorrect subtract domain result", return -1);
2262 return 0;
2265 int test_factorize(isl_ctx *ctx)
2267 const char *str;
2268 isl_basic_set *bset;
2269 isl_factorizer *f;
2271 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2272 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2273 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2274 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2275 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2276 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2277 "3i5 >= -2i0 - i2 + 3i4 }";
2278 bset = isl_basic_set_read_from_str(ctx, str);
2279 f = isl_basic_set_factorizer(bset);
2280 isl_basic_set_free(bset);
2281 isl_factorizer_free(f);
2282 if (!f)
2283 isl_die(ctx, isl_error_unknown,
2284 "failed to construct factorizer", return -1);
2286 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2287 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2288 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2289 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2290 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2291 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2292 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2293 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2294 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2295 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2296 bset = isl_basic_set_read_from_str(ctx, str);
2297 f = isl_basic_set_factorizer(bset);
2298 isl_basic_set_free(bset);
2299 isl_factorizer_free(f);
2300 if (!f)
2301 isl_die(ctx, isl_error_unknown,
2302 "failed to construct factorizer", return -1);
2304 return 0;
2307 static int check_injective(__isl_take isl_map *map, void *user)
2309 int *injective = user;
2311 *injective = isl_map_is_injective(map);
2312 isl_map_free(map);
2314 if (*injective < 0 || !*injective)
2315 return -1;
2317 return 0;
2320 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2321 const char *r, const char *s, int tilable, int parallel)
2323 int i;
2324 isl_union_set *D;
2325 isl_union_map *W, *R, *S;
2326 isl_union_map *empty;
2327 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2328 isl_union_map *validity, *proximity;
2329 isl_union_map *schedule;
2330 isl_union_map *test;
2331 isl_union_set *delta;
2332 isl_union_set *domain;
2333 isl_set *delta_set;
2334 isl_set *slice;
2335 isl_set *origin;
2336 isl_schedule *sched;
2337 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2339 D = isl_union_set_read_from_str(ctx, d);
2340 W = isl_union_map_read_from_str(ctx, w);
2341 R = isl_union_map_read_from_str(ctx, r);
2342 S = isl_union_map_read_from_str(ctx, s);
2344 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2345 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2347 empty = isl_union_map_empty(isl_union_map_get_space(S));
2348 isl_union_map_compute_flow(isl_union_map_copy(R),
2349 isl_union_map_copy(W), empty,
2350 isl_union_map_copy(S),
2351 &dep_raw, NULL, NULL, NULL);
2352 isl_union_map_compute_flow(isl_union_map_copy(W),
2353 isl_union_map_copy(W),
2354 isl_union_map_copy(R),
2355 isl_union_map_copy(S),
2356 &dep_waw, &dep_war, NULL, NULL);
2358 dep = isl_union_map_union(dep_waw, dep_war);
2359 dep = isl_union_map_union(dep, dep_raw);
2360 validity = isl_union_map_copy(dep);
2361 proximity = isl_union_map_copy(dep);
2363 sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
2364 validity, proximity);
2365 schedule = isl_schedule_get_map(sched);
2366 isl_schedule_free(sched);
2367 isl_union_map_free(W);
2368 isl_union_map_free(R);
2369 isl_union_map_free(S);
2371 is_injection = 1;
2372 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2374 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2375 is_complete = isl_union_set_is_subset(D, domain);
2376 isl_union_set_free(D);
2377 isl_union_set_free(domain);
2379 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2380 test = isl_union_map_apply_range(test, dep);
2381 test = isl_union_map_apply_range(test, schedule);
2383 delta = isl_union_map_deltas(test);
2384 if (isl_union_set_n_set(delta) == 0) {
2385 is_tilable = 1;
2386 is_parallel = 1;
2387 is_nonneg = 1;
2388 isl_union_set_free(delta);
2389 } else {
2390 delta_set = isl_set_from_union_set(delta);
2392 slice = isl_set_universe(isl_set_get_space(delta_set));
2393 for (i = 0; i < tilable; ++i)
2394 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2395 is_tilable = isl_set_is_subset(delta_set, slice);
2396 isl_set_free(slice);
2398 slice = isl_set_universe(isl_set_get_space(delta_set));
2399 for (i = 0; i < parallel; ++i)
2400 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2401 is_parallel = isl_set_is_subset(delta_set, slice);
2402 isl_set_free(slice);
2404 origin = isl_set_universe(isl_set_get_space(delta_set));
2405 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2406 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2408 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2409 delta_set = isl_set_lexmin(delta_set);
2411 is_nonneg = isl_set_is_equal(delta_set, origin);
2413 isl_set_free(origin);
2414 isl_set_free(delta_set);
2417 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2418 is_injection < 0 || is_complete < 0)
2419 return -1;
2420 if (!is_complete)
2421 isl_die(ctx, isl_error_unknown,
2422 "generated schedule incomplete", return -1);
2423 if (!is_injection)
2424 isl_die(ctx, isl_error_unknown,
2425 "generated schedule not injective on each statement",
2426 return -1);
2427 if (!is_nonneg)
2428 isl_die(ctx, isl_error_unknown,
2429 "negative dependences in generated schedule",
2430 return -1);
2431 if (!is_tilable)
2432 isl_die(ctx, isl_error_unknown,
2433 "generated schedule not as tilable as expected",
2434 return -1);
2435 if (!is_parallel)
2436 isl_die(ctx, isl_error_unknown,
2437 "generated schedule not as parallel as expected",
2438 return -1);
2440 return 0;
2443 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2444 const char *domain, const char *validity, const char *proximity)
2446 isl_union_set *dom;
2447 isl_union_map *dep;
2448 isl_union_map *prox;
2449 isl_schedule *schedule;
2450 isl_union_map *sched;
2452 dom = isl_union_set_read_from_str(ctx, domain);
2453 dep = isl_union_map_read_from_str(ctx, validity);
2454 prox = isl_union_map_read_from_str(ctx, proximity);
2455 schedule = isl_union_set_compute_schedule(dom, dep, prox);
2456 sched = isl_schedule_get_map(schedule);
2457 isl_schedule_free(schedule);
2459 return sched;
2462 /* Check that a schedule can be constructed on the given domain
2463 * with the given validity and proximity constraints.
2465 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2466 const char *validity, const char *proximity)
2468 isl_union_map *sched;
2470 sched = compute_schedule(ctx, domain, validity, proximity);
2471 if (!sched)
2472 return -1;
2474 isl_union_map_free(sched);
2475 return 0;
2478 int test_special_schedule(isl_ctx *ctx, const char *domain,
2479 const char *validity, const char *proximity, const char *expected_sched)
2481 isl_union_map *sched1, *sched2;
2482 int equal;
2484 sched1 = compute_schedule(ctx, domain, validity, proximity);
2485 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2487 equal = isl_union_map_is_equal(sched1, sched2);
2488 isl_union_map_free(sched1);
2489 isl_union_map_free(sched2);
2491 if (equal < 0)
2492 return -1;
2493 if (!equal)
2494 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2495 return -1);
2497 return 0;
2500 /* Check that the schedule map is properly padded, even after being
2501 * reconstructed from the band forest.
2503 static int test_padded_schedule(isl_ctx *ctx)
2505 const char *str;
2506 isl_union_set *D;
2507 isl_union_map *validity, *proximity;
2508 isl_schedule *sched;
2509 isl_union_map *map1, *map2;
2510 isl_band_list *list;
2511 int equal;
2513 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2514 D = isl_union_set_read_from_str(ctx, str);
2515 validity = isl_union_map_empty(isl_union_set_get_space(D));
2516 proximity = isl_union_map_copy(validity);
2517 sched = isl_union_set_compute_schedule(D, validity, proximity);
2518 map1 = isl_schedule_get_map(sched);
2519 list = isl_schedule_get_band_forest(sched);
2520 isl_band_list_free(list);
2521 map2 = isl_schedule_get_map(sched);
2522 isl_schedule_free(sched);
2523 equal = isl_union_map_is_equal(map1, map2);
2524 isl_union_map_free(map1);
2525 isl_union_map_free(map2);
2527 if (equal < 0)
2528 return -1;
2529 if (!equal)
2530 isl_die(ctx, isl_error_unknown,
2531 "reconstructed schedule map not the same as original",
2532 return -1);
2534 return 0;
2537 int test_schedule(isl_ctx *ctx)
2539 const char *D, *W, *R, *V, *P, *S;
2541 /* Handle resulting schedule with zero bands. */
2542 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2543 return -1;
2545 /* Jacobi */
2546 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2547 W = "{ S1[t,i] -> a[t,i] }";
2548 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2549 "S1[t,i] -> a[t-1,i+1] }";
2550 S = "{ S1[t,i] -> [t,i] }";
2551 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2552 return -1;
2554 /* Fig. 5 of CC2008 */
2555 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2556 "j <= -1 + N }";
2557 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2558 "j >= 2 and j <= -1 + N }";
2559 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2560 "j >= 2 and j <= -1 + N; "
2561 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2562 "j >= 2 and j <= -1 + N }";
2563 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2564 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2565 return -1;
2567 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2568 W = "{ S1[i] -> a[i] }";
2569 R = "{ S2[i] -> a[i+1] }";
2570 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2571 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2572 return -1;
2574 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2575 W = "{ S1[i] -> a[i] }";
2576 R = "{ S2[i] -> a[9-i] }";
2577 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2578 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2579 return -1;
2581 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2582 W = "{ S1[i] -> a[i] }";
2583 R = "[N] -> { S2[i] -> a[N-1-i] }";
2584 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2585 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2586 return -1;
2588 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2589 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2590 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2591 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2592 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2593 return -1;
2595 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2596 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2597 R = "{ S2[i,j] -> a[i-1,j] }";
2598 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2599 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2600 return -1;
2602 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2603 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2604 R = "{ S2[i,j] -> a[i,j-1] }";
2605 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2606 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2607 return -1;
2609 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2610 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2611 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2612 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2613 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2614 "S_0[] -> [0, 0, 0] }";
2615 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2616 return -1;
2617 ctx->opt->schedule_parametric = 0;
2618 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2619 return -1;
2620 ctx->opt->schedule_parametric = 1;
2622 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2623 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2624 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2625 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2626 "S4[i] -> a[i,N] }";
2627 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2628 "S4[i] -> [4,i,0] }";
2629 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2630 return -1;
2632 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2633 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2634 "j <= N }";
2635 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2636 "j <= N; "
2637 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2638 "j <= N }";
2639 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2640 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2641 return -1;
2643 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2644 " S_2[t] : t >= 0 and t <= -1 + N; "
2645 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2646 "i <= -1 + N }";
2647 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2648 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2649 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2650 "i >= 0 and i <= -1 + N }";
2651 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2652 "i >= 0 and i <= -1 + N; "
2653 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2654 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2655 " S_0[t] -> [0, t, 0] }";
2657 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2658 return -1;
2659 ctx->opt->schedule_parametric = 0;
2660 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2661 return -1;
2662 ctx->opt->schedule_parametric = 1;
2664 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2665 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2666 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2667 return -1;
2669 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2670 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2671 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2672 "j >= 0 and j <= -1 + N; "
2673 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2674 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2675 "j >= 0 and j <= -1 + N; "
2676 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2677 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2678 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2679 return -1;
2681 D = "{ S_0[i] : i >= 0 }";
2682 W = "{ S_0[i] -> a[i] : i >= 0 }";
2683 R = "{ S_0[i] -> a[0] : i >= 0 }";
2684 S = "{ S_0[i] -> [0, i, 0] }";
2685 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2686 return -1;
2688 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2689 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2690 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2691 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2692 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2693 return -1;
2695 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2696 "k <= -1 + n and k >= 0 }";
2697 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
2698 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2699 "k <= -1 + n and k >= 0; "
2700 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2701 "k <= -1 + n and k >= 0; "
2702 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2703 "k <= -1 + n and k >= 0 }";
2704 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2705 ctx->opt->schedule_outer_zero_distance = 1;
2706 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2707 return -1;
2708 ctx->opt->schedule_outer_zero_distance = 0;
2710 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
2711 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
2712 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
2713 "Stmt_for_body24[i0, i1, 1, 0]:"
2714 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
2715 "Stmt_for_body7[i0, i1, i2]:"
2716 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
2717 "i2 <= 7 }";
2719 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
2720 "Stmt_for_body24[1, i1, i2, i3]:"
2721 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
2722 "i2 >= 1;"
2723 "Stmt_for_body24[0, i1, i2, i3] -> "
2724 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
2725 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
2726 "i3 >= 0;"
2727 "Stmt_for_body24[0, i1, i2, i3] ->"
2728 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
2729 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
2730 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
2731 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
2732 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
2733 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
2734 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
2735 "i1 <= 6 and i1 >= 0;"
2736 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
2737 "Stmt_for_body7[i0, i1, i2] -> "
2738 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
2739 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
2740 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
2741 "Stmt_for_body7[i0, i1, i2] -> "
2742 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
2743 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
2744 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
2745 P = V;
2746 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
2747 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
2748 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
2750 if (test_special_schedule(ctx, D, V, P, S) < 0)
2751 return -1;
2753 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
2754 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
2755 "j >= 1 and j <= 7;"
2756 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
2757 "j >= 1 and j <= 8 }";
2758 P = "{ }";
2759 S = "{ S_0[i, j] -> [i + j, j] }";
2760 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2761 if (test_special_schedule(ctx, D, V, P, S) < 0)
2762 return -1;
2763 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2765 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
2766 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
2767 "j >= 0 and j <= -1 + i }";
2768 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
2769 "i <= -1 + N and j >= 0;"
2770 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
2771 "i <= -2 + N }";
2772 P = "{ }";
2773 S = "{ S_0[i, j] -> [i, j] }";
2774 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2775 if (test_special_schedule(ctx, D, V, P, S) < 0)
2776 return -1;
2777 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2779 /* Test both algorithms on a case with only proximity dependences. */
2780 D = "{ S[i,j] : 0 <= i <= 10 }";
2781 V = "{ }";
2782 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2783 S = "{ S[i, j] -> [j, i] }";
2784 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2785 if (test_special_schedule(ctx, D, V, P, S) < 0)
2786 return -1;
2787 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2788 if (test_special_schedule(ctx, D, V, P, S) < 0)
2789 return -1;
2791 D = "{ A[a]; B[] }";
2792 V = "{}";
2793 P = "{ A[a] -> B[] }";
2794 if (test_has_schedule(ctx, D, V, P) < 0)
2795 return -1;
2797 if (test_padded_schedule(ctx) < 0)
2798 return -1;
2800 /* Check that check for progress is not confused by rational
2801 * solution.
2803 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
2804 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
2805 "i0 <= -2 + N; "
2806 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
2807 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
2808 P = "{}";
2809 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2810 if (test_has_schedule(ctx, D, V, P) < 0)
2811 return -1;
2812 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2814 return 0;
2817 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2819 isl_union_map *umap;
2820 int test;
2822 umap = isl_union_map_read_from_str(ctx, str);
2823 test = isl_union_map_plain_is_injective(umap);
2824 isl_union_map_free(umap);
2825 if (test < 0)
2826 return -1;
2827 if (test == injective)
2828 return 0;
2829 if (injective)
2830 isl_die(ctx, isl_error_unknown,
2831 "map not detected as injective", return -1);
2832 else
2833 isl_die(ctx, isl_error_unknown,
2834 "map detected as injective", return -1);
2837 int test_injective(isl_ctx *ctx)
2839 const char *str;
2841 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2842 return -1;
2843 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2844 return -1;
2845 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2846 return -1;
2847 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2848 return -1;
2849 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2850 return -1;
2851 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2852 return -1;
2853 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2854 return -1;
2855 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2856 return -1;
2858 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2859 if (test_plain_injective(ctx, str, 1))
2860 return -1;
2861 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2862 if (test_plain_injective(ctx, str, 0))
2863 return -1;
2865 return 0;
2868 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
2870 isl_aff *aff2;
2871 int equal;
2873 if (!aff)
2874 return -1;
2876 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
2877 equal = isl_aff_plain_is_equal(aff, aff2);
2878 isl_aff_free(aff2);
2880 return equal;
2883 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
2885 int equal;
2887 equal = aff_plain_is_equal(aff, str);
2888 if (equal < 0)
2889 return -1;
2890 if (!equal)
2891 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
2892 "result not as expected", return -1);
2893 return 0;
2896 int test_aff(isl_ctx *ctx)
2898 const char *str;
2899 isl_set *set;
2900 isl_space *space;
2901 isl_local_space *ls;
2902 isl_aff *aff;
2903 int zero, equal;
2905 space = isl_space_set_alloc(ctx, 0, 1);
2906 ls = isl_local_space_from_space(space);
2907 aff = isl_aff_zero_on_domain(ls);
2909 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2910 aff = isl_aff_scale_down_ui(aff, 3);
2911 aff = isl_aff_floor(aff);
2912 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2913 aff = isl_aff_scale_down_ui(aff, 2);
2914 aff = isl_aff_floor(aff);
2915 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2917 str = "{ [10] }";
2918 set = isl_set_read_from_str(ctx, str);
2919 aff = isl_aff_gist(aff, set);
2921 aff = isl_aff_add_constant_si(aff, -16);
2922 zero = isl_aff_plain_is_zero(aff);
2923 isl_aff_free(aff);
2925 if (zero < 0)
2926 return -1;
2927 if (!zero)
2928 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2930 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
2931 aff = isl_aff_scale_down_ui(aff, 64);
2932 aff = isl_aff_floor(aff);
2933 equal = aff_check_plain_equal(aff, "{ [-1] }");
2934 isl_aff_free(aff);
2935 if (equal < 0)
2936 return -1;
2938 return 0;
2941 int test_dim_max(isl_ctx *ctx)
2943 int equal;
2944 const char *str;
2945 isl_set *set1, *set2;
2946 isl_set *set;
2947 isl_map *map;
2948 isl_pw_aff *pwaff;
2950 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
2951 set = isl_set_read_from_str(ctx, str);
2952 pwaff = isl_set_dim_max(set, 0);
2953 set1 = isl_set_from_pw_aff(pwaff);
2954 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
2955 set2 = isl_set_read_from_str(ctx, str);
2956 equal = isl_set_is_equal(set1, set2);
2957 isl_set_free(set1);
2958 isl_set_free(set2);
2959 if (equal < 0)
2960 return -1;
2961 if (!equal)
2962 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2964 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
2965 set = isl_set_read_from_str(ctx, str);
2966 pwaff = isl_set_dim_max(set, 0);
2967 set1 = isl_set_from_pw_aff(pwaff);
2968 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2969 set2 = isl_set_read_from_str(ctx, str);
2970 equal = isl_set_is_equal(set1, set2);
2971 isl_set_free(set1);
2972 isl_set_free(set2);
2973 if (equal < 0)
2974 return -1;
2975 if (!equal)
2976 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2978 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
2979 set = isl_set_read_from_str(ctx, str);
2980 pwaff = isl_set_dim_max(set, 0);
2981 set1 = isl_set_from_pw_aff(pwaff);
2982 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2983 set2 = isl_set_read_from_str(ctx, str);
2984 equal = isl_set_is_equal(set1, set2);
2985 isl_set_free(set1);
2986 isl_set_free(set2);
2987 if (equal < 0)
2988 return -1;
2989 if (!equal)
2990 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2992 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
2993 "0 <= i < N and 0 <= j < M }";
2994 map = isl_map_read_from_str(ctx, str);
2995 set = isl_map_range(map);
2997 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
2998 set1 = isl_set_from_pw_aff(pwaff);
2999 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
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);
3005 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3006 set1 = isl_set_from_pw_aff(pwaff);
3007 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3008 set2 = isl_set_read_from_str(ctx, str);
3009 if (equal >= 0 && equal)
3010 equal = isl_set_is_equal(set1, set2);
3011 isl_set_free(set1);
3012 isl_set_free(set2);
3014 isl_set_free(set);
3016 if (equal < 0)
3017 return -1;
3018 if (!equal)
3019 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3021 /* Check that solutions are properly merged. */
3022 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3023 "c <= -1 + n - 4a - 2b and c >= -2b and "
3024 "4a >= -4 + n and c >= 0 }";
3025 set = isl_set_read_from_str(ctx, str);
3026 pwaff = isl_set_dim_min(set, 2);
3027 set1 = isl_set_from_pw_aff(pwaff);
3028 str = "[n] -> { [(0)] : n >= 1 }";
3029 set2 = isl_set_read_from_str(ctx, str);
3030 equal = isl_set_is_equal(set1, set2);
3031 isl_set_free(set1);
3032 isl_set_free(set2);
3034 if (equal < 0)
3035 return -1;
3036 if (!equal)
3037 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3039 return 0;
3042 int test_product(isl_ctx *ctx)
3044 const char *str;
3045 isl_set *set;
3046 isl_union_set *uset1, *uset2;
3047 int ok;
3049 str = "{ A[i] }";
3050 set = isl_set_read_from_str(ctx, str);
3051 set = isl_set_product(set, isl_set_copy(set));
3052 ok = isl_set_is_wrapping(set);
3053 isl_set_free(set);
3054 if (ok < 0)
3055 return -1;
3056 if (!ok)
3057 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3059 str = "{ [] }";
3060 uset1 = isl_union_set_read_from_str(ctx, str);
3061 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3062 str = "{ [[] -> []] }";
3063 uset2 = isl_union_set_read_from_str(ctx, str);
3064 ok = isl_union_set_is_equal(uset1, uset2);
3065 isl_union_set_free(uset1);
3066 isl_union_set_free(uset2);
3067 if (ok < 0)
3068 return -1;
3069 if (!ok)
3070 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3072 return 0;
3075 int test_equal(isl_ctx *ctx)
3077 const char *str;
3078 isl_set *set, *set2;
3079 int equal;
3081 str = "{ S_6[i] }";
3082 set = isl_set_read_from_str(ctx, str);
3083 str = "{ S_7[i] }";
3084 set2 = isl_set_read_from_str(ctx, str);
3085 equal = isl_set_is_equal(set, set2);
3086 isl_set_free(set);
3087 isl_set_free(set2);
3088 if (equal < 0)
3089 return -1;
3090 if (equal)
3091 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3093 return 0;
3096 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3097 enum isl_dim_type type, unsigned pos, int fixed)
3099 int test;
3101 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3102 isl_map_free(map);
3103 if (test < 0)
3104 return -1;
3105 if (test == fixed)
3106 return 0;
3107 if (fixed)
3108 isl_die(ctx, isl_error_unknown,
3109 "map not detected as fixed", return -1);
3110 else
3111 isl_die(ctx, isl_error_unknown,
3112 "map detected as fixed", return -1);
3115 int test_fixed(isl_ctx *ctx)
3117 const char *str;
3118 isl_map *map;
3120 str = "{ [i] -> [i] }";
3121 map = isl_map_read_from_str(ctx, str);
3122 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3123 return -1;
3124 str = "{ [i] -> [1] }";
3125 map = isl_map_read_from_str(ctx, str);
3126 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3127 return -1;
3128 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3129 map = isl_map_read_from_str(ctx, str);
3130 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3131 return -1;
3132 map = isl_map_read_from_str(ctx, str);
3133 map = isl_map_neg(map);
3134 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3135 return -1;
3137 return 0;
3140 int test_vertices(isl_ctx *ctx)
3142 const char *str;
3143 isl_basic_set *bset;
3144 isl_vertices *vertices;
3146 str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
3147 bset = isl_basic_set_read_from_str(ctx, str);
3148 vertices = isl_basic_set_compute_vertices(bset);
3149 isl_basic_set_free(bset);
3150 isl_vertices_free(vertices);
3151 if (!vertices)
3152 return -1;
3154 str = "{ A[t, i] : t = 14 and i = 1 }";
3155 bset = isl_basic_set_read_from_str(ctx, str);
3156 vertices = isl_basic_set_compute_vertices(bset);
3157 isl_basic_set_free(bset);
3158 isl_vertices_free(vertices);
3159 if (!vertices)
3160 return -1;
3162 return 0;
3165 int test_union_pw(isl_ctx *ctx)
3167 int equal;
3168 const char *str;
3169 isl_union_set *uset;
3170 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3172 str = "{ [x] -> x^2 }";
3173 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3174 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3175 uset = isl_union_pw_qpolynomial_domain(upwqp1);
3176 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3177 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3178 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3179 isl_union_pw_qpolynomial_free(upwqp1);
3180 isl_union_pw_qpolynomial_free(upwqp2);
3181 if (equal < 0)
3182 return -1;
3183 if (!equal)
3184 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3186 return 0;
3189 int test_output(isl_ctx *ctx)
3191 char *s;
3192 const char *str;
3193 isl_pw_aff *pa;
3194 isl_printer *p;
3195 int equal;
3197 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3198 pa = isl_pw_aff_read_from_str(ctx, str);
3200 p = isl_printer_to_str(ctx);
3201 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3202 p = isl_printer_print_pw_aff(p, pa);
3203 s = isl_printer_get_str(p);
3204 isl_printer_free(p);
3205 isl_pw_aff_free(pa);
3206 if (!s)
3207 equal = -1;
3208 else
3209 equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
3210 free(s);
3211 if (equal < 0)
3212 return -1;
3213 if (!equal)
3214 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3216 return 0;
3219 int test_sample(isl_ctx *ctx)
3221 const char *str;
3222 isl_basic_set *bset1, *bset2;
3223 int empty, subset;
3225 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3226 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3227 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3228 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3229 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3230 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3231 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3232 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3233 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3234 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3235 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3236 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3237 "d - 1073741821e and "
3238 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3239 "3j >= 1 - a + b + 2e and "
3240 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3241 "3i <= 4 - a + 4b - e and "
3242 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3243 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3244 "c <= -1 + a and 3i >= -2 - a + 3e and "
3245 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3246 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3247 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3248 "1073741823e >= 1 + 1073741823b - d and "
3249 "3i >= 1073741823b + c - 1073741823e - f and "
3250 "3i >= 1 + 2b + e + 3g }";
3251 bset1 = isl_basic_set_read_from_str(ctx, str);
3252 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3253 empty = isl_basic_set_is_empty(bset2);
3254 subset = isl_basic_set_is_subset(bset2, bset1);
3255 isl_basic_set_free(bset1);
3256 isl_basic_set_free(bset2);
3257 if (empty < 0 || subset < 0)
3258 return -1;
3259 if (empty)
3260 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3261 if (!subset)
3262 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3264 return 0;
3267 int test_fixed_power(isl_ctx *ctx)
3269 const char *str;
3270 isl_map *map;
3271 isl_int exp;
3272 int equal;
3274 isl_int_init(exp);
3275 str = "{ [i] -> [i + 1] }";
3276 map = isl_map_read_from_str(ctx, str);
3277 isl_int_set_si(exp, 23);
3278 map = isl_map_fixed_power(map, exp);
3279 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3280 isl_int_clear(exp);
3281 isl_map_free(map);
3282 if (equal < 0)
3283 return -1;
3285 return 0;
3288 int test_slice(isl_ctx *ctx)
3290 const char *str;
3291 isl_map *map;
3292 int equal;
3294 str = "{ [i] -> [j] }";
3295 map = isl_map_read_from_str(ctx, str);
3296 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3297 equal = map_check_equal(map, "{ [i] -> [i] }");
3298 isl_map_free(map);
3299 if (equal < 0)
3300 return -1;
3302 str = "{ [i] -> [j] }";
3303 map = isl_map_read_from_str(ctx, str);
3304 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
3305 equal = map_check_equal(map, "{ [i] -> [j] }");
3306 isl_map_free(map);
3307 if (equal < 0)
3308 return -1;
3310 str = "{ [i] -> [j] }";
3311 map = isl_map_read_from_str(ctx, str);
3312 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
3313 equal = map_check_equal(map, "{ [i] -> [-i] }");
3314 isl_map_free(map);
3315 if (equal < 0)
3316 return -1;
3318 str = "{ [i] -> [j] }";
3319 map = isl_map_read_from_str(ctx, str);
3320 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
3321 equal = map_check_equal(map, "{ [0] -> [j] }");
3322 isl_map_free(map);
3323 if (equal < 0)
3324 return -1;
3326 str = "{ [i] -> [j] }";
3327 map = isl_map_read_from_str(ctx, str);
3328 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3329 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3330 isl_map_free(map);
3331 if (equal < 0)
3332 return -1;
3334 str = "{ [i] -> [j] }";
3335 map = isl_map_read_from_str(ctx, str);
3336 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3337 equal = map_check_equal(map, "{ [i] -> [j] : false }");
3338 isl_map_free(map);
3339 if (equal < 0)
3340 return -1;
3342 return 0;
3345 int test_eliminate(isl_ctx *ctx)
3347 const char *str;
3348 isl_map *map;
3349 int equal;
3351 str = "{ [i] -> [j] : i = 2j }";
3352 map = isl_map_read_from_str(ctx, str);
3353 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3354 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3355 isl_map_free(map);
3356 if (equal < 0)
3357 return -1;
3359 return 0;
3362 /* Check that isl_set_dim_residue_class detects that the values of j
3363 * in the set below are all odd and that it does not detect any spurious
3364 * strides.
3366 static int test_residue_class(isl_ctx *ctx)
3368 const char *str;
3369 isl_set *set;
3370 isl_int m, r;
3371 int res;
3373 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3374 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3375 set = isl_set_read_from_str(ctx, str);
3376 isl_int_init(m);
3377 isl_int_init(r);
3378 res = isl_set_dim_residue_class(set, 1, &m, &r);
3379 if (res >= 0 &&
3380 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3381 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3382 res = -1);
3383 isl_int_clear(r);
3384 isl_int_clear(m);
3385 isl_set_free(set);
3387 return res;
3390 int test_align_parameters(isl_ctx *ctx)
3392 const char *str;
3393 isl_space *space;
3394 isl_multi_aff *ma1, *ma2;
3395 int equal;
3397 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3398 ma1 = isl_multi_aff_read_from_str(ctx, str);
3400 space = isl_space_params_alloc(ctx, 1);
3401 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
3402 ma1 = isl_multi_aff_align_params(ma1, space);
3404 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3405 ma2 = isl_multi_aff_read_from_str(ctx, str);
3407 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3409 isl_multi_aff_free(ma1);
3410 isl_multi_aff_free(ma2);
3412 if (equal < 0)
3413 return -1;
3414 if (!equal)
3415 isl_die(ctx, isl_error_unknown,
3416 "result not as expected", return -1);
3418 return 0;
3421 static int test_list(isl_ctx *ctx)
3423 isl_id *a, *b, *c, *d, *id;
3424 isl_id_list *list;
3425 int ok;
3427 a = isl_id_alloc(ctx, "a", NULL);
3428 b = isl_id_alloc(ctx, "b", NULL);
3429 c = isl_id_alloc(ctx, "c", NULL);
3430 d = isl_id_alloc(ctx, "d", NULL);
3432 list = isl_id_list_alloc(ctx, 4);
3433 list = isl_id_list_add(list, a);
3434 list = isl_id_list_add(list, b);
3435 list = isl_id_list_add(list, c);
3436 list = isl_id_list_add(list, d);
3437 list = isl_id_list_drop(list, 1, 1);
3439 if (isl_id_list_n_id(list) != 3) {
3440 isl_id_list_free(list);
3441 isl_die(ctx, isl_error_unknown,
3442 "unexpected number of elements in list", return -1);
3445 id = isl_id_list_get_id(list, 0);
3446 ok = id == a;
3447 isl_id_free(id);
3448 id = isl_id_list_get_id(list, 1);
3449 ok = ok && id == c;
3450 isl_id_free(id);
3451 id = isl_id_list_get_id(list, 2);
3452 ok = ok && id == d;
3453 isl_id_free(id);
3455 isl_id_list_free(list);
3457 if (!ok)
3458 isl_die(ctx, isl_error_unknown,
3459 "unexpected elements in list", return -1);
3461 return 0;
3464 const char *set_conversion_tests[] = {
3465 "[N] -> { [i] : N - 1 <= 2 i <= N }",
3466 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
3467 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3468 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3471 /* Check that converting from isl_set to isl_pw_multi_aff and back
3472 * to isl_set produces the original isl_set.
3474 static int test_set_conversion(isl_ctx *ctx)
3476 int i;
3477 const char *str;
3478 isl_set *set1, *set2;
3479 isl_pw_multi_aff *pma;
3480 int equal;
3482 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
3483 str = set_conversion_tests[i];
3484 set1 = isl_set_read_from_str(ctx, str);
3485 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
3486 set2 = isl_set_from_pw_multi_aff(pma);
3487 equal = isl_set_is_equal(set1, set2);
3488 isl_set_free(set1);
3489 isl_set_free(set2);
3491 if (equal < 0)
3492 return -1;
3493 if (!equal)
3494 isl_die(ctx, isl_error_unknown, "bad conversion",
3495 return -1);
3498 return 0;
3501 /* Check that converting from isl_map to isl_pw_multi_aff and back
3502 * to isl_map produces the original isl_map.
3504 static int test_map_conversion(isl_ctx *ctx)
3506 const char *str;
3507 isl_map *map1, *map2;
3508 isl_pw_multi_aff *pma;
3509 int equal;
3511 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
3512 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
3513 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
3514 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
3515 "9e <= -2 - 2a) }";
3516 map1 = isl_map_read_from_str(ctx, str);
3517 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
3518 map2 = isl_map_from_pw_multi_aff(pma);
3519 equal = isl_map_is_equal(map1, map2);
3520 isl_map_free(map1);
3521 isl_map_free(map2);
3523 if (equal < 0)
3524 return -1;
3525 if (!equal)
3526 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
3528 return 0;
3531 static int test_conversion(isl_ctx *ctx)
3533 if (test_set_conversion(ctx) < 0)
3534 return -1;
3535 if (test_map_conversion(ctx) < 0)
3536 return -1;
3537 return 0;
3540 /* Check that isl_basic_map_curry does not modify input.
3542 static int test_curry(isl_ctx *ctx)
3544 const char *str;
3545 isl_basic_map *bmap1, *bmap2;
3546 int equal;
3548 str = "{ [A[] -> B[]] -> C[] }";
3549 bmap1 = isl_basic_map_read_from_str(ctx, str);
3550 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
3551 equal = isl_basic_map_is_equal(bmap1, bmap2);
3552 isl_basic_map_free(bmap1);
3553 isl_basic_map_free(bmap2);
3555 if (equal < 0)
3556 return -1;
3557 if (equal)
3558 isl_die(ctx, isl_error_unknown,
3559 "curried map should not be equal to original",
3560 return -1);
3562 return 0;
3565 struct {
3566 const char *set;
3567 const char *ma;
3568 const char *res;
3569 } preimage_tests[] = {
3570 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
3571 "{ A[j,i] -> B[i,j] }",
3572 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
3573 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3574 "{ A[a,b] -> B[a/2,b/6] }",
3575 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
3576 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3577 "{ A[a,b] -> B[a/2,b/6] }",
3578 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
3579 "exists i,j : a = 2 i and b = 6 j }" },
3580 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
3581 "[n] -> { : 0 <= n <= 100 }" },
3582 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3583 "{ A[a] -> B[2a] }",
3584 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
3585 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3586 "{ A[a] -> B[([a/2])] }",
3587 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
3588 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
3589 "{ A[a] -> B[a,a,a/3] }",
3590 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
3591 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
3592 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
3595 static int test_preimage_basic_set(isl_ctx *ctx)
3597 int i;
3598 isl_basic_set *bset1, *bset2;
3599 isl_multi_aff *ma;
3600 int equal;
3602 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
3603 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
3604 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
3605 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
3606 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
3607 equal = isl_basic_set_is_equal(bset1, bset2);
3608 isl_basic_set_free(bset1);
3609 isl_basic_set_free(bset2);
3610 if (equal < 0)
3611 return -1;
3612 if (!equal)
3613 isl_die(ctx, isl_error_unknown, "bad preimage",
3614 return -1);
3617 return 0;
3620 struct {
3621 const char *map;
3622 const char *ma;
3623 const char *res;
3624 } preimage_domain_tests[] = {
3625 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
3626 "{ A[j,i] -> B[i,j] }",
3627 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
3628 { "{ B[i] -> C[i]; D[i] -> E[i] }",
3629 "{ A[i] -> B[i + 1] }",
3630 "{ A[i] -> C[i + 1] }" },
3631 { "{ B[i] -> C[i]; B[i] -> E[i] }",
3632 "{ A[i] -> B[i + 1] }",
3633 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
3634 { "{ B[i] -> C[([i/2])] }",
3635 "{ A[i] -> B[2i] }",
3636 "{ A[i] -> C[i] }" },
3637 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
3638 "{ A[i] -> B[([i/5]), ([i/7])] }",
3639 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
3640 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
3641 "[N] -> { A[] -> B[([N/5])] }",
3642 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
3643 { "{ B[i] -> C[i] : exists a : i = 5 a }",
3644 "{ A[i] -> B[2i] }",
3645 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
3646 { "{ B[i] -> C[i] : exists a : i = 2 a; "
3647 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
3648 "{ A[i] -> B[2i] }",
3649 "{ A[i] -> C[2i] }" },
3652 static int test_preimage_union_map(isl_ctx *ctx)
3654 int i;
3655 isl_union_map *umap1, *umap2;
3656 isl_multi_aff *ma;
3657 int equal;
3659 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
3660 umap1 = isl_union_map_read_from_str(ctx,
3661 preimage_domain_tests[i].map);
3662 ma = isl_multi_aff_read_from_str(ctx,
3663 preimage_domain_tests[i].ma);
3664 umap2 = isl_union_map_read_from_str(ctx,
3665 preimage_domain_tests[i].res);
3666 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
3667 equal = isl_union_map_is_equal(umap1, umap2);
3668 isl_union_map_free(umap1);
3669 isl_union_map_free(umap2);
3670 if (equal < 0)
3671 return -1;
3672 if (!equal)
3673 isl_die(ctx, isl_error_unknown, "bad preimage",
3674 return -1);
3677 return 0;
3680 static int test_preimage(isl_ctx *ctx)
3682 if (test_preimage_basic_set(ctx) < 0)
3683 return -1;
3684 if (test_preimage_union_map(ctx) < 0)
3685 return -1;
3687 return 0;
3690 struct {
3691 const char *ma1;
3692 const char *ma;
3693 const char *res;
3694 } pullback_tests[] = {
3695 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
3696 "{ A[a,b] -> C[b + 2a] }" },
3697 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
3698 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
3699 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
3700 "{ A[a] -> C[(a)/6] }" },
3701 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
3702 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
3703 "{ A[a] -> C[(2a)/3] }" },
3704 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
3705 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
3706 "{ A[i,j] -> C[i + j, i + j] }"},
3707 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
3708 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
3709 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
3712 static int test_pullback(isl_ctx *ctx)
3714 int i;
3715 isl_multi_aff *ma1, *ma2;
3716 isl_multi_aff *ma;
3717 int equal;
3719 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
3720 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
3721 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
3722 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
3723 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
3724 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3725 isl_multi_aff_free(ma1);
3726 isl_multi_aff_free(ma2);
3727 if (equal < 0)
3728 return -1;
3729 if (!equal)
3730 isl_die(ctx, isl_error_unknown, "bad pullback",
3731 return -1);
3734 return 0;
3737 /* Check that negation is printed correctly.
3739 static int test_ast(isl_ctx *ctx)
3741 isl_ast_expr *expr, *expr1, *expr2, *expr3;
3742 char *str;
3743 int ok;
3745 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3746 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3747 expr = isl_ast_expr_add(expr1, expr2);
3748 expr = isl_ast_expr_neg(expr);
3749 str = isl_ast_expr_to_str(expr);
3750 ok = str ? !strcmp(str, "-(A + B)") : -1;
3751 free(str);
3752 isl_ast_expr_free(expr);
3754 if (ok < 0)
3755 return -1;
3756 if (!ok)
3757 isl_die(ctx, isl_error_unknown,
3758 "isl_ast_expr printed incorrectly", return -1);
3760 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3761 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3762 expr = isl_ast_expr_add(expr1, expr2);
3763 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
3764 expr = isl_ast_expr_sub(expr3, expr);
3765 str = isl_ast_expr_to_str(expr);
3766 ok = str ? !strcmp(str, "C - (A + B)") : -1;
3767 free(str);
3768 isl_ast_expr_free(expr);
3770 if (ok < 0)
3771 return -1;
3772 if (!ok)
3773 isl_die(ctx, isl_error_unknown,
3774 "isl_ast_expr printed incorrectly", return -1);
3776 return 0;
3779 /* Internal data structure for before_for and after_for callbacks.
3781 * depth is the current depth
3782 * before is the number of times before_for has been called
3783 * after is the number of times after_for has been called
3785 struct isl_test_codegen_data {
3786 int depth;
3787 int before;
3788 int after;
3791 /* This function is called before each for loop in the AST generated
3792 * from test_ast_gen1.
3794 * Increment the number of calls and the depth.
3795 * Check that the space returned by isl_ast_build_get_schedule_space
3796 * matches the target space of the schedule returned by
3797 * isl_ast_build_get_schedule.
3798 * Return an isl_id that is checked by the corresponding call
3799 * to after_for.
3801 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
3802 void *user)
3804 struct isl_test_codegen_data *data = user;
3805 isl_ctx *ctx;
3806 isl_space *space;
3807 isl_union_map *schedule;
3808 isl_union_set *uset;
3809 isl_set *set;
3810 int empty;
3811 char name[] = "d0";
3813 ctx = isl_ast_build_get_ctx(build);
3815 if (data->before >= 3)
3816 isl_die(ctx, isl_error_unknown,
3817 "unexpected number of for nodes", return NULL);
3818 if (data->depth >= 2)
3819 isl_die(ctx, isl_error_unknown,
3820 "unexpected depth", return NULL);
3822 snprintf(name, sizeof(name), "d%d", data->depth);
3823 data->before++;
3824 data->depth++;
3826 schedule = isl_ast_build_get_schedule(build);
3827 uset = isl_union_map_range(schedule);
3828 if (!uset)
3829 return NULL;
3830 if (isl_union_set_n_set(uset) != 1) {
3831 isl_union_set_free(uset);
3832 isl_die(ctx, isl_error_unknown,
3833 "expecting single range space", return NULL);
3836 space = isl_ast_build_get_schedule_space(build);
3837 set = isl_union_set_extract_set(uset, space);
3838 isl_union_set_free(uset);
3839 empty = isl_set_is_empty(set);
3840 isl_set_free(set);
3842 if (empty < 0)
3843 return NULL;
3844 if (empty)
3845 isl_die(ctx, isl_error_unknown,
3846 "spaces don't match", return NULL);
3848 return isl_id_alloc(ctx, name, NULL);
3851 /* This function is called after each for loop in the AST generated
3852 * from test_ast_gen1.
3854 * Increment the number of calls and decrement the depth.
3855 * Check that the annotation attached to the node matches
3856 * the isl_id returned by the corresponding call to before_for.
3858 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
3859 __isl_keep isl_ast_build *build, void *user)
3861 struct isl_test_codegen_data *data = user;
3862 isl_id *id;
3863 const char *name;
3864 int valid;
3866 data->after++;
3867 data->depth--;
3869 if (data->after > data->before)
3870 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3871 "mismatch in number of for nodes",
3872 return isl_ast_node_free(node));
3874 id = isl_ast_node_get_annotation(node);
3875 if (!id)
3876 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3877 "missing annotation", return isl_ast_node_free(node));
3879 name = isl_id_get_name(id);
3880 valid = name && atoi(name + 1) == data->depth;
3881 isl_id_free(id);
3883 if (!valid)
3884 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3885 "wrong annotation", return isl_ast_node_free(node));
3887 return node;
3890 /* Check that the before_each_for and after_each_for callbacks
3891 * are called for each for loop in the generated code,
3892 * that they are called in the right order and that the isl_id
3893 * returned from the before_each_for callback is attached to
3894 * the isl_ast_node passed to the corresponding after_each_for call.
3896 static int test_ast_gen1(isl_ctx *ctx)
3898 const char *str;
3899 isl_set *set;
3900 isl_union_map *schedule;
3901 isl_ast_build *build;
3902 isl_ast_node *tree;
3903 struct isl_test_codegen_data data;
3905 str = "[N] -> { : N >= 10 }";
3906 set = isl_set_read_from_str(ctx, str);
3907 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
3908 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
3909 schedule = isl_union_map_read_from_str(ctx, str);
3911 data.before = 0;
3912 data.after = 0;
3913 data.depth = 0;
3914 build = isl_ast_build_from_context(set);
3915 build = isl_ast_build_set_before_each_for(build,
3916 &before_for, &data);
3917 build = isl_ast_build_set_after_each_for(build,
3918 &after_for, &data);
3919 tree = isl_ast_build_ast_from_schedule(build, schedule);
3920 isl_ast_build_free(build);
3921 if (!tree)
3922 return -1;
3924 isl_ast_node_free(tree);
3926 if (data.before != 3 || data.after != 3)
3927 isl_die(ctx, isl_error_unknown,
3928 "unexpected number of for nodes", return -1);
3930 return 0;
3933 /* Check that the AST generator handles domains that are integrally disjoint
3934 * but not ratinoally disjoint.
3936 static int test_ast_gen2(isl_ctx *ctx)
3938 const char *str;
3939 isl_set *set;
3940 isl_union_map *schedule;
3941 isl_union_map *options;
3942 isl_ast_build *build;
3943 isl_ast_node *tree;
3945 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
3946 schedule = isl_union_map_read_from_str(ctx, str);
3947 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3948 build = isl_ast_build_from_context(set);
3950 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
3951 options = isl_union_map_read_from_str(ctx, str);
3952 build = isl_ast_build_set_options(build, options);
3953 tree = isl_ast_build_ast_from_schedule(build, schedule);
3954 isl_ast_build_free(build);
3955 if (!tree)
3956 return -1;
3957 isl_ast_node_free(tree);
3959 return 0;
3962 /* Increment *user on each call.
3964 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
3965 __isl_keep isl_ast_build *build, void *user)
3967 int *n = user;
3969 (*n)++;
3971 return node;
3974 /* Test that unrolling tries to minimize the number of instances.
3975 * In particular, for the schedule given below, make sure it generates
3976 * 3 nodes (rather than 101).
3978 static int test_ast_gen3(isl_ctx *ctx)
3980 const char *str;
3981 isl_set *set;
3982 isl_union_map *schedule;
3983 isl_union_map *options;
3984 isl_ast_build *build;
3985 isl_ast_node *tree;
3986 int n_domain = 0;
3988 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
3989 schedule = isl_union_map_read_from_str(ctx, str);
3990 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3992 str = "{ [i] -> unroll[0] }";
3993 options = isl_union_map_read_from_str(ctx, str);
3995 build = isl_ast_build_from_context(set);
3996 build = isl_ast_build_set_options(build, options);
3997 build = isl_ast_build_set_at_each_domain(build,
3998 &count_domains, &n_domain);
3999 tree = isl_ast_build_ast_from_schedule(build, schedule);
4000 isl_ast_build_free(build);
4001 if (!tree)
4002 return -1;
4004 isl_ast_node_free(tree);
4006 if (n_domain != 3)
4007 isl_die(ctx, isl_error_unknown,
4008 "unexpected number of for nodes", return -1);
4010 return 0;
4013 /* Check that if the ast_build_exploit_nested_bounds options is set,
4014 * we do not get an outer if node in the generated AST,
4015 * while we do get such an outer if node if the options is not set.
4017 static int test_ast_gen4(isl_ctx *ctx)
4019 const char *str;
4020 isl_set *set;
4021 isl_union_map *schedule;
4022 isl_ast_build *build;
4023 isl_ast_node *tree;
4024 enum isl_ast_node_type type;
4025 int enb;
4027 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4028 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4030 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4032 schedule = isl_union_map_read_from_str(ctx, str);
4033 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4034 build = isl_ast_build_from_context(set);
4035 tree = isl_ast_build_ast_from_schedule(build, schedule);
4036 isl_ast_build_free(build);
4037 if (!tree)
4038 return -1;
4040 type = isl_ast_node_get_type(tree);
4041 isl_ast_node_free(tree);
4043 if (type == isl_ast_node_if)
4044 isl_die(ctx, isl_error_unknown,
4045 "not expecting if node", return -1);
4047 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
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 "expecting if node", return -1);
4064 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4066 return 0;
4069 /* This function is called for each leaf in the AST generated
4070 * from test_ast_gen5.
4072 * We finalize the AST generation by extending the outer schedule
4073 * with a zero-dimensional schedule. If this results in any for loops,
4074 * then this means that we did not pass along enough information
4075 * about the outer schedule to the inner AST generation.
4077 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4078 void *user)
4080 isl_union_map *schedule, *extra;
4081 isl_ast_node *tree;
4083 schedule = isl_ast_build_get_schedule(build);
4084 extra = isl_union_map_copy(schedule);
4085 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4086 schedule = isl_union_map_range_product(schedule, extra);
4087 tree = isl_ast_build_ast_from_schedule(build, schedule);
4088 isl_ast_build_free(build);
4090 if (!tree)
4091 return NULL;
4093 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4094 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4095 "code should not contain any for loop",
4096 return isl_ast_node_free(tree));
4098 return tree;
4101 /* Check that we do not lose any information when going back and
4102 * forth between internal and external schedule.
4104 * In particular, we create an AST where we unroll the only
4105 * non-constant dimension in the schedule. We therefore do
4106 * not expect any for loops in the AST. However, older versions
4107 * of isl would not pass along enough information about the outer
4108 * schedule when performing an inner code generation from a create_leaf
4109 * callback, resulting in the inner code generation producing a for loop.
4111 static int test_ast_gen5(isl_ctx *ctx)
4113 const char *str;
4114 isl_set *set;
4115 isl_union_map *schedule, *options;
4116 isl_ast_build *build;
4117 isl_ast_node *tree;
4119 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4120 schedule = isl_union_map_read_from_str(ctx, str);
4122 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4123 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4124 options = isl_union_map_read_from_str(ctx, str);
4126 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4127 build = isl_ast_build_from_context(set);
4128 build = isl_ast_build_set_options(build, options);
4129 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4130 tree = isl_ast_build_ast_from_schedule(build, schedule);
4131 isl_ast_build_free(build);
4132 isl_ast_node_free(tree);
4133 if (!tree)
4134 return -1;
4136 return 0;
4139 static int test_ast_gen(isl_ctx *ctx)
4141 if (test_ast_gen1(ctx) < 0)
4142 return -1;
4143 if (test_ast_gen2(ctx) < 0)
4144 return -1;
4145 if (test_ast_gen3(ctx) < 0)
4146 return -1;
4147 if (test_ast_gen4(ctx) < 0)
4148 return -1;
4149 if (test_ast_gen5(ctx) < 0)
4150 return -1;
4151 return 0;
4154 /* Check if dropping output dimensions from an isl_pw_multi_aff
4155 * works properly.
4157 static int test_pw_multi_aff(isl_ctx *ctx)
4159 const char *str;
4160 isl_pw_multi_aff *pma1, *pma2;
4161 int equal;
4163 str = "{ [i,j] -> [i+j, 4i-j] }";
4164 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4165 str = "{ [i,j] -> [4i-j] }";
4166 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4168 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4170 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4172 isl_pw_multi_aff_free(pma1);
4173 isl_pw_multi_aff_free(pma2);
4174 if (equal < 0)
4175 return -1;
4176 if (!equal)
4177 isl_die(ctx, isl_error_unknown,
4178 "expressions not equal", return -1);
4180 return 0;
4183 /* This is a regression test for a bug where isl_basic_map_simplify
4184 * would end up in an infinite loop. In particular, we construct
4185 * an empty basic set that is not obviously empty.
4186 * isl_basic_set_is_empty marks the basic set as empty.
4187 * After projecting out i3, the variable can be dropped completely,
4188 * but isl_basic_map_simplify refrains from doing so if the basic set
4189 * is empty and would end up in an infinite loop if it didn't test
4190 * explicitly for empty basic maps in the outer loop.
4192 static int test_simplify(isl_ctx *ctx)
4194 const char *str;
4195 isl_basic_set *bset;
4196 int empty;
4198 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
4199 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
4200 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
4201 "i3 >= i2 }";
4202 bset = isl_basic_set_read_from_str(ctx, str);
4203 empty = isl_basic_set_is_empty(bset);
4204 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
4205 isl_basic_set_free(bset);
4206 if (!bset)
4207 return -1;
4208 if (!empty)
4209 isl_die(ctx, isl_error_unknown,
4210 "basic set should be empty", return -1);
4212 return 0;
4215 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
4216 * with gbr context would fail to disable the use of the shifted tableau
4217 * when transferring equalities for the input to the context, resulting
4218 * in invalid sample values.
4220 static int test_partial_lexmin(isl_ctx *ctx)
4222 const char *str;
4223 isl_basic_set *bset;
4224 isl_basic_map *bmap;
4225 isl_map *map;
4227 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
4228 bmap = isl_basic_map_read_from_str(ctx, str);
4229 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
4230 bset = isl_basic_set_read_from_str(ctx, str);
4231 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
4232 isl_map_free(map);
4234 if (!map)
4235 return -1;
4237 return 0;
4240 /* Check that the variable compression performed on the existentially
4241 * quantified variables inside isl_basic_set_compute_divs is not confused
4242 * by the implicit equalities among the parameters.
4244 static int test_compute_divs(isl_ctx *ctx)
4246 const char *str;
4247 isl_basic_set *bset;
4248 isl_set *set;
4250 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
4251 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
4252 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
4253 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
4254 bset = isl_basic_set_read_from_str(ctx, str);
4255 set = isl_basic_set_compute_divs(bset);
4256 isl_set_free(set);
4257 if (!set)
4258 return -1;
4260 return 0;
4263 struct {
4264 const char *name;
4265 int (*fn)(isl_ctx *ctx);
4266 } tests [] = {
4267 { "val", &test_val },
4268 { "compute divs", &test_compute_divs },
4269 { "partial lexmin", &test_partial_lexmin },
4270 { "simplify", &test_simplify },
4271 { "curry", &test_curry },
4272 { "piecewise multi affine expressions", &test_pw_multi_aff },
4273 { "conversion", &test_conversion },
4274 { "list", &test_list },
4275 { "align parameters", &test_align_parameters },
4276 { "preimage", &test_preimage },
4277 { "pullback", &test_pullback },
4278 { "AST", &test_ast },
4279 { "AST generation", &test_ast_gen },
4280 { "eliminate", &test_eliminate },
4281 { "residue class", &test_residue_class },
4282 { "div", &test_div },
4283 { "slice", &test_slice },
4284 { "fixed power", &test_fixed_power },
4285 { "sample", &test_sample },
4286 { "output", &test_output },
4287 { "vertices", &test_vertices },
4288 { "fixed", &test_fixed },
4289 { "equal", &test_equal },
4290 { "product", &test_product },
4291 { "dim_max", &test_dim_max },
4292 { "affine", &test_aff },
4293 { "injective", &test_injective },
4294 { "schedule", &test_schedule },
4295 { "union_pw", &test_union_pw },
4296 { "parse", &test_parse },
4297 { "single-valued", &test_sv },
4298 { "affine hull", &test_affine_hull },
4299 { "coalesce", &test_coalesce },
4300 { "factorize", &test_factorize },
4301 { "subset", &test_subset },
4302 { "subtract", &test_subtract },
4303 { "lexmin", &test_lexmin },
4306 int main()
4308 int i;
4309 struct isl_ctx *ctx;
4311 srcdir = getenv("srcdir");
4312 assert(srcdir);
4314 ctx = isl_ctx_alloc();
4315 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
4316 printf("%s\n", tests[i].name);
4317 if (tests[i].fn(ctx) < 0)
4318 goto error;
4320 test_lift(ctx);
4321 test_bound(ctx);
4322 test_union(ctx);
4323 test_split_periods(ctx);
4324 test_pwqp(ctx);
4325 test_lex(ctx);
4326 test_bijective(ctx);
4327 test_dep(ctx);
4328 test_read(ctx);
4329 test_bounded(ctx);
4330 test_construction(ctx);
4331 test_dim(ctx);
4332 test_application(ctx);
4333 test_convex_hull(ctx);
4334 test_gist(ctx);
4335 test_closure(ctx);
4336 isl_ctx_free(ctx);
4337 return 0;
4338 error:
4339 isl_ctx_free(ctx);
4340 return -1;