generalize and export isl_map_to_basic_set
[isl.git] / isl_test.c
blobf1a790dd51e451426946522fcea352306623c3ff
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <assert.h>
16 #include <stdio.h>
17 #include <limits.h>
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
20 #include <isl_aff_private.h>
21 #include <isl/set.h>
22 #include <isl/flow.h>
23 #include <isl_constraint_private.h>
24 #include <isl/polynomial.h>
25 #include <isl/union_map.h>
26 #include <isl_factorization.h>
27 #include <isl/schedule.h>
28 #include <isl_options_private.h>
29 #include <isl/vertices.h>
30 #include <isl/ast_build.h>
31 #include <isl/val.h>
33 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
35 static char *srcdir;
37 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
38 char *filename;
39 int length;
40 char *pattern = "%s/test_inputs/%s.%s";
42 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
43 + strlen(suffix) + 1;
44 filename = isl_alloc_array(ctx, char, length);
46 if (!filename)
47 return NULL;
49 sprintf(filename, pattern, srcdir, name, suffix);
51 return filename;
54 void test_parse_map(isl_ctx *ctx, const char *str)
56 isl_map *map;
58 map = isl_map_read_from_str(ctx, str);
59 assert(map);
60 isl_map_free(map);
63 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
65 isl_map *map, *map2;
66 int equal;
68 map = isl_map_read_from_str(ctx, str);
69 map2 = isl_map_read_from_str(ctx, str2);
70 equal = isl_map_is_equal(map, map2);
71 isl_map_free(map);
72 isl_map_free(map2);
74 if (equal < 0)
75 return -1;
76 if (!equal)
77 isl_die(ctx, isl_error_unknown, "maps not equal",
78 return -1);
80 return 0;
83 void test_parse_pwqp(isl_ctx *ctx, const char *str)
85 isl_pw_qpolynomial *pwqp;
87 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
88 assert(pwqp);
89 isl_pw_qpolynomial_free(pwqp);
92 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
94 isl_pw_aff *pwaff;
96 pwaff = isl_pw_aff_read_from_str(ctx, str);
97 assert(pwaff);
98 isl_pw_aff_free(pwaff);
101 int test_parse(struct isl_ctx *ctx)
103 isl_map *map, *map2;
104 const char *str, *str2;
106 str = "{ [i] -> [-i] }";
107 map = isl_map_read_from_str(ctx, str);
108 assert(map);
109 isl_map_free(map);
111 str = "{ A[i] -> L[([i/3])] }";
112 map = isl_map_read_from_str(ctx, str);
113 assert(map);
114 isl_map_free(map);
116 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
117 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
118 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
120 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
121 str2 = "{ [x, y] : 2y >= 6 - x }";
122 if (test_parse_map_equal(ctx, str, str2) < 0)
123 return -1;
125 if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
126 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
127 return -1;
128 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
129 if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
130 str) < 0)
131 return -1;
133 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
134 map = isl_map_read_from_str(ctx, str);
135 str = "{ [new, old] -> [o0, o1] : "
136 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
137 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
138 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
139 map2 = isl_map_read_from_str(ctx, str);
140 assert(isl_map_is_equal(map, map2));
141 isl_map_free(map);
142 isl_map_free(map2);
144 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
145 map = isl_map_read_from_str(ctx, str);
146 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
147 map2 = isl_map_read_from_str(ctx, str);
148 assert(isl_map_is_equal(map, map2));
149 isl_map_free(map);
150 isl_map_free(map2);
152 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
153 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
154 if (test_parse_map_equal(ctx, str, str2) < 0)
155 return -1;
157 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
158 str2 = "{ [i,j] -> [min(i,j)] }";
159 if (test_parse_map_equal(ctx, str, str2) < 0)
160 return -1;
162 str = "{ [i,j] : i != j }";
163 str2 = "{ [i,j] : i < j or i > j }";
164 if (test_parse_map_equal(ctx, str, str2) < 0)
165 return -1;
167 str = "{ [i,j] : (i+1)*2 >= j }";
168 str2 = "{ [i, j] : j <= 2 + 2i }";
169 if (test_parse_map_equal(ctx, str, str2) < 0)
170 return -1;
172 str = "{ [i] -> [i > 0 ? 4 : 5] }";
173 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
174 if (test_parse_map_equal(ctx, str, str2) < 0)
175 return -1;
177 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
178 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
179 if (test_parse_map_equal(ctx, str, str2) < 0)
180 return -1;
182 str = "{ [x] : x >= 0 }";
183 str2 = "{ [x] : x-0 >= 0 }";
184 if (test_parse_map_equal(ctx, str, str2) < 0)
185 return -1;
187 str = "{ [i] : ((i > 10)) }";
188 str2 = "{ [i] : i >= 11 }";
189 if (test_parse_map_equal(ctx, str, str2) < 0)
190 return -1;
192 str = "{ [i] -> [0] }";
193 str2 = "{ [i] -> [0 * i] }";
194 if (test_parse_map_equal(ctx, str, str2) < 0)
195 return -1;
197 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
198 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
199 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
200 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
202 if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
203 "{ [a] -> [b] : true }") < 0)
204 return -1;
206 if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
207 "{ [i] : i <= 10 }") < 0)
208 return -1;
210 if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
211 "[n] -> { [i] : i <= n }") < 0)
212 return -1;
214 if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
215 return -1;
217 if (test_parse_map_equal(ctx, "{ [i] : 2*floor(i/2) = i }",
218 "{ [i] : exists a : i = 2 a }") < 0)
219 return -1;
221 if (test_parse_map_equal(ctx, "{ [a] -> [b] : a = 5 implies b = 5 }",
222 "{ [a] -> [b] : a != 5 or b = 5 }") < 0)
223 return -1;
225 if (test_parse_map_equal(ctx, "{ [a] -> [a - 1 : a > 0] }",
226 "{ [a] -> [a - 1] : a > 0 }") < 0)
227 return -1;
228 if (test_parse_map_equal(ctx,
229 "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
230 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }") < 0)
231 return -1;
232 if (test_parse_map_equal(ctx,
233 "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
234 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
235 return -1;
236 if (test_parse_map_equal(ctx,
237 "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
238 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
239 return -1;
240 if (test_parse_map_equal(ctx,
241 "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
242 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
243 return -1;
244 if (test_parse_map_equal(ctx,
245 "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
246 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
247 return -1;
249 return 0;
252 void test_read(struct isl_ctx *ctx)
254 char *filename;
255 FILE *input;
256 struct isl_basic_set *bset1, *bset2;
257 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
259 filename = get_filename(ctx, "set", "omega");
260 assert(filename);
261 input = fopen(filename, "r");
262 assert(input);
264 bset1 = isl_basic_set_read_from_file(ctx, input);
265 bset2 = isl_basic_set_read_from_str(ctx, str);
267 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
269 isl_basic_set_free(bset1);
270 isl_basic_set_free(bset2);
271 free(filename);
273 fclose(input);
276 void test_bounded(struct isl_ctx *ctx)
278 isl_set *set;
279 int bounded;
281 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
282 bounded = isl_set_is_bounded(set);
283 assert(bounded);
284 isl_set_free(set);
286 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
287 bounded = isl_set_is_bounded(set);
288 assert(!bounded);
289 isl_set_free(set);
291 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
292 bounded = isl_set_is_bounded(set);
293 assert(!bounded);
294 isl_set_free(set);
297 /* Construct the basic set { [i] : 5 <= i <= N } */
298 void test_construction(struct isl_ctx *ctx)
300 isl_int v;
301 isl_space *dim;
302 isl_local_space *ls;
303 struct isl_basic_set *bset;
304 struct isl_constraint *c;
306 isl_int_init(v);
308 dim = isl_space_set_alloc(ctx, 1, 1);
309 bset = isl_basic_set_universe(isl_space_copy(dim));
310 ls = isl_local_space_from_space(dim);
312 c = isl_inequality_alloc(isl_local_space_copy(ls));
313 isl_int_set_si(v, -1);
314 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
315 isl_int_set_si(v, 1);
316 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
317 bset = isl_basic_set_add_constraint(bset, c);
319 c = isl_inequality_alloc(isl_local_space_copy(ls));
320 isl_int_set_si(v, 1);
321 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
322 isl_int_set_si(v, -5);
323 isl_constraint_set_constant(c, v);
324 bset = isl_basic_set_add_constraint(bset, c);
326 isl_local_space_free(ls);
327 isl_basic_set_free(bset);
329 isl_int_clear(v);
332 void test_dim(struct isl_ctx *ctx)
334 const char *str;
335 isl_map *map1, *map2;
337 map1 = isl_map_read_from_str(ctx,
338 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
339 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
340 map2 = isl_map_read_from_str(ctx,
341 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
342 assert(isl_map_is_equal(map1, map2));
343 isl_map_free(map2);
345 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
346 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
347 assert(isl_map_is_equal(map1, map2));
349 isl_map_free(map1);
350 isl_map_free(map2);
352 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
353 map1 = isl_map_read_from_str(ctx, str);
354 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
355 map2 = isl_map_read_from_str(ctx, str);
356 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
357 assert(isl_map_is_equal(map1, map2));
359 isl_map_free(map1);
360 isl_map_free(map2);
363 struct {
364 __isl_give isl_val *(*op)(__isl_take isl_val *v);
365 const char *arg;
366 const char *res;
367 } val_un_tests[] = {
368 { &isl_val_neg, "0", "0" },
369 { &isl_val_abs, "0", "0" },
370 { &isl_val_2exp, "0", "1" },
371 { &isl_val_floor, "0", "0" },
372 { &isl_val_ceil, "0", "0" },
373 { &isl_val_neg, "1", "-1" },
374 { &isl_val_neg, "-1", "1" },
375 { &isl_val_neg, "1/2", "-1/2" },
376 { &isl_val_neg, "-1/2", "1/2" },
377 { &isl_val_neg, "infty", "-infty" },
378 { &isl_val_neg, "-infty", "infty" },
379 { &isl_val_neg, "NaN", "NaN" },
380 { &isl_val_abs, "1", "1" },
381 { &isl_val_abs, "-1", "1" },
382 { &isl_val_abs, "1/2", "1/2" },
383 { &isl_val_abs, "-1/2", "1/2" },
384 { &isl_val_abs, "infty", "infty" },
385 { &isl_val_abs, "-infty", "infty" },
386 { &isl_val_abs, "NaN", "NaN" },
387 { &isl_val_floor, "1", "1" },
388 { &isl_val_floor, "-1", "-1" },
389 { &isl_val_floor, "1/2", "0" },
390 { &isl_val_floor, "-1/2", "-1" },
391 { &isl_val_floor, "infty", "infty" },
392 { &isl_val_floor, "-infty", "-infty" },
393 { &isl_val_floor, "NaN", "NaN" },
394 { &isl_val_ceil, "1", "1" },
395 { &isl_val_ceil, "-1", "-1" },
396 { &isl_val_ceil, "1/2", "1" },
397 { &isl_val_ceil, "-1/2", "0" },
398 { &isl_val_ceil, "infty", "infty" },
399 { &isl_val_ceil, "-infty", "-infty" },
400 { &isl_val_ceil, "NaN", "NaN" },
401 { &isl_val_2exp, "-3", "1/8" },
402 { &isl_val_2exp, "-1", "1/2" },
403 { &isl_val_2exp, "1", "2" },
404 { &isl_val_2exp, "2", "4" },
405 { &isl_val_2exp, "3", "8" },
408 /* Perform some basic tests of unary operations on isl_val objects.
410 static int test_un_val(isl_ctx *ctx)
412 int i;
413 isl_val *v, *res;
414 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
415 int ok;
417 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
418 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
419 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
420 fn = val_un_tests[i].op;
421 v = fn(v);
422 if (isl_val_is_nan(res))
423 ok = isl_val_is_nan(v);
424 else
425 ok = isl_val_eq(v, res);
426 isl_val_free(v);
427 isl_val_free(res);
428 if (ok < 0)
429 return -1;
430 if (!ok)
431 isl_die(ctx, isl_error_unknown,
432 "unexpected result", return -1);
435 return 0;
438 struct {
439 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
440 __isl_take isl_val *v2);
441 } val_bin_op[] = {
442 ['+'] = { &isl_val_add },
443 ['-'] = { &isl_val_sub },
444 ['*'] = { &isl_val_mul },
445 ['/'] = { &isl_val_div },
446 ['g'] = { &isl_val_gcd },
447 ['m'] = { &isl_val_min },
448 ['M'] = { &isl_val_max },
451 struct {
452 const char *arg1;
453 unsigned char op;
454 const char *arg2;
455 const char *res;
456 } val_bin_tests[] = {
457 { "0", '+', "0", "0" },
458 { "1", '+', "0", "1" },
459 { "1", '+', "1", "2" },
460 { "1", '-', "1", "0" },
461 { "1", '*', "1", "1" },
462 { "1", '/', "1", "1" },
463 { "2", '*', "3", "6" },
464 { "2", '*', "1/2", "1" },
465 { "2", '*', "1/3", "2/3" },
466 { "2/3", '*', "3/5", "2/5" },
467 { "2/3", '*', "7/5", "14/15" },
468 { "2", '/', "1/2", "4" },
469 { "-2", '/', "-1/2", "4" },
470 { "-2", '/', "1/2", "-4" },
471 { "2", '/', "-1/2", "-4" },
472 { "2", '/', "2", "1" },
473 { "2", '/', "3", "2/3" },
474 { "2/3", '/', "5/3", "2/5" },
475 { "2/3", '/', "5/7", "14/15" },
476 { "0", '/', "0", "NaN" },
477 { "42", '/', "0", "NaN" },
478 { "-42", '/', "0", "NaN" },
479 { "infty", '/', "0", "NaN" },
480 { "-infty", '/', "0", "NaN" },
481 { "NaN", '/', "0", "NaN" },
482 { "0", '/', "NaN", "NaN" },
483 { "42", '/', "NaN", "NaN" },
484 { "-42", '/', "NaN", "NaN" },
485 { "infty", '/', "NaN", "NaN" },
486 { "-infty", '/', "NaN", "NaN" },
487 { "NaN", '/', "NaN", "NaN" },
488 { "0", '/', "infty", "0" },
489 { "42", '/', "infty", "0" },
490 { "-42", '/', "infty", "0" },
491 { "infty", '/', "infty", "NaN" },
492 { "-infty", '/', "infty", "NaN" },
493 { "NaN", '/', "infty", "NaN" },
494 { "0", '/', "-infty", "0" },
495 { "42", '/', "-infty", "0" },
496 { "-42", '/', "-infty", "0" },
497 { "infty", '/', "-infty", "NaN" },
498 { "-infty", '/', "-infty", "NaN" },
499 { "NaN", '/', "-infty", "NaN" },
500 { "1", '-', "1/3", "2/3" },
501 { "1/3", '+', "1/2", "5/6" },
502 { "1/2", '+', "1/2", "1" },
503 { "3/4", '-', "1/4", "1/2" },
504 { "1/2", '-', "1/3", "1/6" },
505 { "infty", '+', "42", "infty" },
506 { "infty", '+', "infty", "infty" },
507 { "42", '+', "infty", "infty" },
508 { "infty", '-', "infty", "NaN" },
509 { "infty", '*', "infty", "infty" },
510 { "infty", '*', "-infty", "-infty" },
511 { "-infty", '*', "infty", "-infty" },
512 { "-infty", '*', "-infty", "infty" },
513 { "0", '*', "infty", "NaN" },
514 { "1", '*', "infty", "infty" },
515 { "infty", '*', "0", "NaN" },
516 { "infty", '*', "42", "infty" },
517 { "42", '-', "infty", "-infty" },
518 { "infty", '+', "-infty", "NaN" },
519 { "4", 'g', "6", "2" },
520 { "5", 'g', "6", "1" },
521 { "42", 'm', "3", "3" },
522 { "42", 'M', "3", "42" },
523 { "3", 'm', "42", "3" },
524 { "3", 'M', "42", "42" },
525 { "42", 'm', "infty", "42" },
526 { "42", 'M', "infty", "infty" },
527 { "42", 'm', "-infty", "-infty" },
528 { "42", 'M', "-infty", "42" },
529 { "42", 'm', "NaN", "NaN" },
530 { "42", 'M', "NaN", "NaN" },
531 { "infty", 'm', "-infty", "-infty" },
532 { "infty", 'M', "-infty", "infty" },
535 /* Perform some basic tests of binary operations on isl_val objects.
537 static int test_bin_val(isl_ctx *ctx)
539 int i;
540 isl_val *v1, *v2, *res;
541 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
542 __isl_take isl_val *v2);
543 int ok;
545 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
546 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
547 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
548 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
549 fn = val_bin_op[val_bin_tests[i].op].fn;
550 v1 = fn(v1, v2);
551 if (isl_val_is_nan(res))
552 ok = isl_val_is_nan(v1);
553 else
554 ok = isl_val_eq(v1, res);
555 isl_val_free(v1);
556 isl_val_free(res);
557 if (ok < 0)
558 return -1;
559 if (!ok)
560 isl_die(ctx, isl_error_unknown,
561 "unexpected result", return -1);
564 return 0;
567 /* Perform some basic tests on isl_val objects.
569 static int test_val(isl_ctx *ctx)
571 if (test_un_val(ctx) < 0)
572 return -1;
573 if (test_bin_val(ctx) < 0)
574 return -1;
575 return 0;
578 static int test_div(isl_ctx *ctx)
580 unsigned n;
581 const char *str;
582 int empty;
583 isl_int v;
584 isl_space *dim;
585 isl_set *set;
586 isl_local_space *ls;
587 struct isl_basic_set *bset;
588 struct isl_constraint *c;
590 isl_int_init(v);
592 /* test 1 */
593 dim = isl_space_set_alloc(ctx, 0, 3);
594 bset = isl_basic_set_universe(isl_space_copy(dim));
595 ls = isl_local_space_from_space(dim);
597 c = isl_equality_alloc(isl_local_space_copy(ls));
598 isl_int_set_si(v, -1);
599 isl_constraint_set_constant(c, v);
600 isl_int_set_si(v, 1);
601 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
602 isl_int_set_si(v, 3);
603 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
604 bset = isl_basic_set_add_constraint(bset, c);
606 c = isl_equality_alloc(isl_local_space_copy(ls));
607 isl_int_set_si(v, 1);
608 isl_constraint_set_constant(c, v);
609 isl_int_set_si(v, -1);
610 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
611 isl_int_set_si(v, 3);
612 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
613 bset = isl_basic_set_add_constraint(bset, c);
615 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
617 assert(bset && bset->n_div == 1);
618 isl_local_space_free(ls);
619 isl_basic_set_free(bset);
621 /* test 2 */
622 dim = isl_space_set_alloc(ctx, 0, 3);
623 bset = isl_basic_set_universe(isl_space_copy(dim));
624 ls = isl_local_space_from_space(dim);
626 c = isl_equality_alloc(isl_local_space_copy(ls));
627 isl_int_set_si(v, 1);
628 isl_constraint_set_constant(c, v);
629 isl_int_set_si(v, -1);
630 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
631 isl_int_set_si(v, 3);
632 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
633 bset = isl_basic_set_add_constraint(bset, c);
635 c = isl_equality_alloc(isl_local_space_copy(ls));
636 isl_int_set_si(v, -1);
637 isl_constraint_set_constant(c, v);
638 isl_int_set_si(v, 1);
639 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
640 isl_int_set_si(v, 3);
641 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
642 bset = isl_basic_set_add_constraint(bset, c);
644 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
646 assert(bset && bset->n_div == 1);
647 isl_local_space_free(ls);
648 isl_basic_set_free(bset);
650 /* test 3 */
651 dim = isl_space_set_alloc(ctx, 0, 3);
652 bset = isl_basic_set_universe(isl_space_copy(dim));
653 ls = isl_local_space_from_space(dim);
655 c = isl_equality_alloc(isl_local_space_copy(ls));
656 isl_int_set_si(v, 1);
657 isl_constraint_set_constant(c, v);
658 isl_int_set_si(v, -1);
659 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
660 isl_int_set_si(v, 3);
661 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
662 bset = isl_basic_set_add_constraint(bset, c);
664 c = isl_equality_alloc(isl_local_space_copy(ls));
665 isl_int_set_si(v, -3);
666 isl_constraint_set_constant(c, v);
667 isl_int_set_si(v, 1);
668 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
669 isl_int_set_si(v, 4);
670 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
671 bset = isl_basic_set_add_constraint(bset, c);
673 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
675 assert(bset && bset->n_div == 1);
676 isl_local_space_free(ls);
677 isl_basic_set_free(bset);
679 /* test 4 */
680 dim = isl_space_set_alloc(ctx, 0, 3);
681 bset = isl_basic_set_universe(isl_space_copy(dim));
682 ls = isl_local_space_from_space(dim);
684 c = isl_equality_alloc(isl_local_space_copy(ls));
685 isl_int_set_si(v, 2);
686 isl_constraint_set_constant(c, v);
687 isl_int_set_si(v, -1);
688 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
689 isl_int_set_si(v, 3);
690 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
691 bset = isl_basic_set_add_constraint(bset, c);
693 c = isl_equality_alloc(isl_local_space_copy(ls));
694 isl_int_set_si(v, -1);
695 isl_constraint_set_constant(c, v);
696 isl_int_set_si(v, 1);
697 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
698 isl_int_set_si(v, 6);
699 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
700 bset = isl_basic_set_add_constraint(bset, c);
702 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
704 assert(isl_basic_set_is_empty(bset));
705 isl_local_space_free(ls);
706 isl_basic_set_free(bset);
708 /* test 5 */
709 dim = isl_space_set_alloc(ctx, 0, 3);
710 bset = isl_basic_set_universe(isl_space_copy(dim));
711 ls = isl_local_space_from_space(dim);
713 c = isl_equality_alloc(isl_local_space_copy(ls));
714 isl_int_set_si(v, -1);
715 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
716 isl_int_set_si(v, 3);
717 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
718 bset = isl_basic_set_add_constraint(bset, c);
720 c = isl_equality_alloc(isl_local_space_copy(ls));
721 isl_int_set_si(v, 1);
722 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
723 isl_int_set_si(v, -3);
724 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
725 bset = isl_basic_set_add_constraint(bset, c);
727 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
729 assert(bset && bset->n_div == 0);
730 isl_basic_set_free(bset);
731 isl_local_space_free(ls);
733 /* test 6 */
734 dim = isl_space_set_alloc(ctx, 0, 3);
735 bset = isl_basic_set_universe(isl_space_copy(dim));
736 ls = isl_local_space_from_space(dim);
738 c = isl_equality_alloc(isl_local_space_copy(ls));
739 isl_int_set_si(v, -1);
740 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
741 isl_int_set_si(v, 6);
742 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
743 bset = isl_basic_set_add_constraint(bset, c);
745 c = isl_equality_alloc(isl_local_space_copy(ls));
746 isl_int_set_si(v, 1);
747 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
748 isl_int_set_si(v, -3);
749 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
750 bset = isl_basic_set_add_constraint(bset, c);
752 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
754 assert(bset && bset->n_div == 1);
755 isl_basic_set_free(bset);
756 isl_local_space_free(ls);
758 /* test 7 */
759 /* This test is a bit tricky. We set up an equality
760 * a + 3b + 3c = 6 e0
761 * Normalization of divs creates _two_ divs
762 * a = 3 e0
763 * c - b - e0 = 2 e1
764 * Afterwards e0 is removed again because it has coefficient -1
765 * and we end up with the original equality and div again.
766 * Perhaps we can avoid the introduction of this temporary div.
768 dim = isl_space_set_alloc(ctx, 0, 4);
769 bset = isl_basic_set_universe(isl_space_copy(dim));
770 ls = isl_local_space_from_space(dim);
772 c = isl_equality_alloc(isl_local_space_copy(ls));
773 isl_int_set_si(v, -1);
774 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
775 isl_int_set_si(v, -3);
776 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
777 isl_int_set_si(v, -3);
778 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
779 isl_int_set_si(v, 6);
780 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
781 bset = isl_basic_set_add_constraint(bset, c);
783 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
785 /* Test disabled for now */
787 assert(bset && bset->n_div == 1);
789 isl_local_space_free(ls);
790 isl_basic_set_free(bset);
792 /* test 8 */
793 dim = isl_space_set_alloc(ctx, 0, 5);
794 bset = isl_basic_set_universe(isl_space_copy(dim));
795 ls = isl_local_space_from_space(dim);
797 c = isl_equality_alloc(isl_local_space_copy(ls));
798 isl_int_set_si(v, -1);
799 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
800 isl_int_set_si(v, -3);
801 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
802 isl_int_set_si(v, -3);
803 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
804 isl_int_set_si(v, 6);
805 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
806 bset = isl_basic_set_add_constraint(bset, c);
808 c = isl_equality_alloc(isl_local_space_copy(ls));
809 isl_int_set_si(v, -1);
810 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
811 isl_int_set_si(v, 1);
812 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
813 isl_int_set_si(v, 1);
814 isl_constraint_set_constant(c, v);
815 bset = isl_basic_set_add_constraint(bset, c);
817 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
819 /* Test disabled for now */
821 assert(bset && bset->n_div == 1);
823 isl_local_space_free(ls);
824 isl_basic_set_free(bset);
826 /* test 9 */
827 dim = isl_space_set_alloc(ctx, 0, 4);
828 bset = isl_basic_set_universe(isl_space_copy(dim));
829 ls = isl_local_space_from_space(dim);
831 c = isl_equality_alloc(isl_local_space_copy(ls));
832 isl_int_set_si(v, 1);
833 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
834 isl_int_set_si(v, -1);
835 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
836 isl_int_set_si(v, -2);
837 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
838 bset = isl_basic_set_add_constraint(bset, c);
840 c = isl_equality_alloc(isl_local_space_copy(ls));
841 isl_int_set_si(v, -1);
842 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
843 isl_int_set_si(v, 3);
844 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
845 isl_int_set_si(v, 2);
846 isl_constraint_set_constant(c, v);
847 bset = isl_basic_set_add_constraint(bset, c);
849 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
851 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
853 assert(!isl_basic_set_is_empty(bset));
855 isl_local_space_free(ls);
856 isl_basic_set_free(bset);
858 /* test 10 */
859 dim = isl_space_set_alloc(ctx, 0, 3);
860 bset = isl_basic_set_universe(isl_space_copy(dim));
861 ls = isl_local_space_from_space(dim);
863 c = isl_equality_alloc(isl_local_space_copy(ls));
864 isl_int_set_si(v, 1);
865 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
866 isl_int_set_si(v, -2);
867 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
868 bset = isl_basic_set_add_constraint(bset, c);
870 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
872 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
874 isl_local_space_free(ls);
875 isl_basic_set_free(bset);
877 isl_int_clear(v);
879 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
880 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
881 set = isl_set_read_from_str(ctx, str);
882 set = isl_set_compute_divs(set);
883 isl_set_free(set);
884 if (!set)
885 return -1;
887 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
888 bset = isl_basic_set_read_from_str(ctx, str);
889 n = isl_basic_set_dim(bset, isl_dim_div);
890 isl_basic_set_free(bset);
891 if (!bset)
892 return -1;
893 if (n != 0)
894 isl_die(ctx, isl_error_unknown,
895 "expecting no existentials", return -1);
897 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
898 set = isl_set_read_from_str(ctx, str);
899 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
900 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
901 empty = isl_set_is_empty(set);
902 isl_set_free(set);
903 if (empty < 0)
904 return -1;
905 if (!empty)
906 isl_die(ctx, isl_error_unknown,
907 "result not as accurate as expected", return -1);
909 return 0;
912 void test_application_case(struct isl_ctx *ctx, const char *name)
914 char *filename;
915 FILE *input;
916 struct isl_basic_set *bset1, *bset2;
917 struct isl_basic_map *bmap;
919 filename = get_filename(ctx, name, "omega");
920 assert(filename);
921 input = fopen(filename, "r");
922 assert(input);
924 bset1 = isl_basic_set_read_from_file(ctx, input);
925 bmap = isl_basic_map_read_from_file(ctx, input);
927 bset1 = isl_basic_set_apply(bset1, bmap);
929 bset2 = isl_basic_set_read_from_file(ctx, input);
931 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
933 isl_basic_set_free(bset1);
934 isl_basic_set_free(bset2);
935 free(filename);
937 fclose(input);
940 void test_application(struct isl_ctx *ctx)
942 test_application_case(ctx, "application");
943 test_application_case(ctx, "application2");
946 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
948 char *filename;
949 FILE *input;
950 struct isl_basic_set *bset1, *bset2;
952 filename = get_filename(ctx, name, "polylib");
953 assert(filename);
954 input = fopen(filename, "r");
955 assert(input);
957 bset1 = isl_basic_set_read_from_file(ctx, input);
958 bset2 = isl_basic_set_read_from_file(ctx, input);
960 bset1 = isl_basic_set_affine_hull(bset1);
962 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
964 isl_basic_set_free(bset1);
965 isl_basic_set_free(bset2);
966 free(filename);
968 fclose(input);
971 int test_affine_hull(struct isl_ctx *ctx)
973 const char *str;
974 isl_set *set;
975 isl_basic_set *bset, *bset2;
976 int n;
977 int subset;
979 test_affine_hull_case(ctx, "affine2");
980 test_affine_hull_case(ctx, "affine");
981 test_affine_hull_case(ctx, "affine3");
983 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
984 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
985 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
986 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
987 set = isl_set_read_from_str(ctx, str);
988 bset = isl_set_affine_hull(set);
989 n = isl_basic_set_dim(bset, isl_dim_div);
990 isl_basic_set_free(bset);
991 if (n != 0)
992 isl_die(ctx, isl_error_unknown, "not expecting any divs",
993 return -1);
995 /* Check that isl_map_affine_hull is not confused by
996 * the reordering of divs in isl_map_align_divs.
998 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
999 "32e0 = b and 32e1 = c); "
1000 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1001 set = isl_set_read_from_str(ctx, str);
1002 bset = isl_set_affine_hull(set);
1003 isl_basic_set_free(bset);
1004 if (!bset)
1005 return -1;
1007 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1008 "32e2 = 31 + 31e0 }";
1009 set = isl_set_read_from_str(ctx, str);
1010 bset = isl_set_affine_hull(set);
1011 str = "{ [a] : exists e : a = 32 e }";
1012 bset2 = isl_basic_set_read_from_str(ctx, str);
1013 subset = isl_basic_set_is_subset(bset, bset2);
1014 isl_basic_set_free(bset);
1015 isl_basic_set_free(bset2);
1016 if (subset < 0)
1017 return -1;
1018 if (!subset)
1019 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1020 return -1);
1022 return 0;
1025 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1027 char *filename;
1028 FILE *input;
1029 struct isl_basic_set *bset1, *bset2;
1030 struct isl_set *set;
1032 filename = get_filename(ctx, name, "polylib");
1033 assert(filename);
1034 input = fopen(filename, "r");
1035 assert(input);
1037 bset1 = isl_basic_set_read_from_file(ctx, input);
1038 bset2 = isl_basic_set_read_from_file(ctx, input);
1040 set = isl_basic_set_union(bset1, bset2);
1041 bset1 = isl_set_convex_hull(set);
1043 bset2 = isl_basic_set_read_from_file(ctx, input);
1045 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1047 isl_basic_set_free(bset1);
1048 isl_basic_set_free(bset2);
1049 free(filename);
1051 fclose(input);
1054 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
1056 const char *str1, *str2;
1057 isl_set *set1, *set2;
1058 int orig_convex = ctx->opt->convex;
1059 ctx->opt->convex = convex;
1061 test_convex_hull_case(ctx, "convex0");
1062 test_convex_hull_case(ctx, "convex1");
1063 test_convex_hull_case(ctx, "convex2");
1064 test_convex_hull_case(ctx, "convex3");
1065 test_convex_hull_case(ctx, "convex4");
1066 test_convex_hull_case(ctx, "convex5");
1067 test_convex_hull_case(ctx, "convex6");
1068 test_convex_hull_case(ctx, "convex7");
1069 test_convex_hull_case(ctx, "convex8");
1070 test_convex_hull_case(ctx, "convex9");
1071 test_convex_hull_case(ctx, "convex10");
1072 test_convex_hull_case(ctx, "convex11");
1073 test_convex_hull_case(ctx, "convex12");
1074 test_convex_hull_case(ctx, "convex13");
1075 test_convex_hull_case(ctx, "convex14");
1076 test_convex_hull_case(ctx, "convex15");
1078 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1079 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1080 "(i0 = 0 and i1 = 0 and i2 = 0) }";
1081 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
1082 set1 = isl_set_read_from_str(ctx, str1);
1083 set2 = isl_set_read_from_str(ctx, str2);
1084 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1085 assert(isl_set_is_equal(set1, set2));
1086 isl_set_free(set1);
1087 isl_set_free(set2);
1089 ctx->opt->convex = orig_convex;
1092 void test_convex_hull(struct isl_ctx *ctx)
1094 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
1095 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
1098 void test_gist_case(struct isl_ctx *ctx, const char *name)
1100 char *filename;
1101 FILE *input;
1102 struct isl_basic_set *bset1, *bset2;
1104 filename = get_filename(ctx, name, "polylib");
1105 assert(filename);
1106 input = fopen(filename, "r");
1107 assert(input);
1109 bset1 = isl_basic_set_read_from_file(ctx, input);
1110 bset2 = isl_basic_set_read_from_file(ctx, input);
1112 bset1 = isl_basic_set_gist(bset1, bset2);
1114 bset2 = isl_basic_set_read_from_file(ctx, input);
1116 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1118 isl_basic_set_free(bset1);
1119 isl_basic_set_free(bset2);
1120 free(filename);
1122 fclose(input);
1125 static int test_gist(struct isl_ctx *ctx)
1127 const char *str;
1128 isl_basic_set *bset1, *bset2;
1129 isl_map *map1, *map2;
1131 test_gist_case(ctx, "gist1");
1133 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1134 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1135 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1136 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1137 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1138 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1139 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1140 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1141 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1142 bset1 = isl_basic_set_read_from_str(ctx, str);
1143 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1144 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1145 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1146 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1147 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1148 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1149 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1150 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1151 bset2 = isl_basic_set_read_from_str(ctx, str);
1152 bset1 = isl_basic_set_gist(bset1, bset2);
1153 assert(bset1 && bset1->n_div == 0);
1154 isl_basic_set_free(bset1);
1156 /* Check that the integer divisions of the second disjunct
1157 * do not spread to the first disjunct.
1159 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1160 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1161 "(exists (e0 = [(-1 + t1)/16], "
1162 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1163 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1164 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1165 "o0 <= 4294967295 and t1 <= -1)) }";
1166 map1 = isl_map_read_from_str(ctx, str);
1167 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1168 map2 = isl_map_read_from_str(ctx, str);
1169 map1 = isl_map_gist(map1, map2);
1170 if (!map1)
1171 return -1;
1172 if (map1->n != 1)
1173 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1174 isl_map_free(map1); return -1);
1175 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1176 isl_die(ctx, isl_error_unknown, "expecting single div",
1177 isl_map_free(map1); return -1);
1178 isl_map_free(map1);
1180 return 0;
1183 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1185 isl_set *set, *set2;
1186 int equal;
1187 int one;
1189 set = isl_set_read_from_str(ctx, str);
1190 set = isl_set_coalesce(set);
1191 set2 = isl_set_read_from_str(ctx, str);
1192 equal = isl_set_is_equal(set, set2);
1193 one = set && set->n == 1;
1194 isl_set_free(set);
1195 isl_set_free(set2);
1197 if (equal < 0)
1198 return -1;
1199 if (!equal)
1200 isl_die(ctx, isl_error_unknown,
1201 "coalesced set not equal to input", return -1);
1202 if (check_one && !one)
1203 isl_die(ctx, isl_error_unknown,
1204 "coalesced set should not be a union", return -1);
1206 return 0;
1209 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1211 int r = 0;
1212 int bounded;
1214 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1215 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1217 if (test_coalesce_set(ctx,
1218 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1219 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1220 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1221 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1222 "-10 <= y <= 0}", 1) < 0)
1223 goto error;
1224 if (test_coalesce_set(ctx,
1225 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
1226 goto error;
1227 if (test_coalesce_set(ctx,
1228 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
1229 goto error;
1231 if (0) {
1232 error:
1233 r = -1;
1236 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1238 return r;
1241 /* Inputs for coalescing tests.
1242 * "str" is a string representation of the input set.
1243 * "single_disjunct" is set if we expect the result to consist of
1244 * a single disjunct.
1246 struct {
1247 int single_disjunct;
1248 const char *str;
1249 } coalesce_tests[] = {
1250 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1251 "y >= x & x >= 2 & 5 >= y }" },
1252 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1253 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1254 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1255 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1256 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1257 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1258 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1259 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1260 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1261 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1262 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1263 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1264 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1265 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1266 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1267 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1268 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1269 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1270 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1271 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1272 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1273 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1274 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1275 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1276 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1277 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1278 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1279 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1280 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1281 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1282 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1283 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1284 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1285 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1286 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1287 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1288 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1289 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1290 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1291 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1292 "[o0, o1, o2, o3, o4, o5, o6]] : "
1293 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1294 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1295 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1296 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1297 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1298 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1299 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1300 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1301 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1302 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1303 "o6 >= i3 + i6 - o3 and M >= 0 and "
1304 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1305 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1306 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1307 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1308 "(o0 = 0 and M >= 2 and N >= 3) or "
1309 "(M = 0 and o0 = 0 and N >= 3) }" },
1310 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1311 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1312 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1313 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1314 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1315 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1316 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1317 "(y = 3 and x = 1) }" },
1318 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1319 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1320 "i1 <= M and i3 <= M and i4 <= M) or "
1321 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1322 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1323 "i4 <= -1 + M) }" },
1324 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1325 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1326 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1327 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1328 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1329 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1330 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1331 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1332 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1333 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1334 { 0, "{ [a, b] : exists e : 2e = a and "
1335 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1336 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1337 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1338 "j >= 1 and j' <= i + j - i' and i >= 1; "
1339 "[1, 1, 1, 1] }" },
1340 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1341 "[i,j] : exists a : j = 3a }" },
1342 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1343 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1344 "a >= 3) or "
1345 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1346 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1347 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1348 "c <= 6 + 8a and a >= 3; "
1349 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1350 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1351 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1352 "[x,0] : 3 <= x <= 4 }" },
1353 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1354 "[x,0] : 4 <= x <= 5 }" },
1355 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1356 "[x,0] : 3 <= x <= 5 }" },
1357 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1358 "[x,0] : 3 <= x <= 4 }" },
1359 { 1 , "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1360 "i1 <= 0; "
1361 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1364 /* Test the functionality of isl_set_coalesce.
1365 * That is, check that the output is always equal to the input
1366 * and in some cases that the result consists of a single disjunct.
1368 static int test_coalesce(struct isl_ctx *ctx)
1370 int i;
1372 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1373 const char *str = coalesce_tests[i].str;
1374 int check_one = coalesce_tests[i].single_disjunct;
1375 if (test_coalesce_set(ctx, str, check_one) < 0)
1376 return -1;
1379 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1380 return -1;
1382 return 0;
1385 void test_closure(struct isl_ctx *ctx)
1387 const char *str;
1388 isl_set *dom;
1389 isl_map *up, *right;
1390 isl_map *map, *map2;
1391 int exact;
1393 /* COCOA example 1 */
1394 map = isl_map_read_from_str(ctx,
1395 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1396 "1 <= i and i < n and 1 <= j and j < n or "
1397 "i2 = i + 1 and j2 = j - 1 and "
1398 "1 <= i and i < n and 2 <= j and j <= n }");
1399 map = isl_map_power(map, &exact);
1400 assert(exact);
1401 isl_map_free(map);
1403 /* COCOA example 1 */
1404 map = isl_map_read_from_str(ctx,
1405 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1406 "1 <= i and i < n and 1 <= j and j < n or "
1407 "i2 = i + 1 and j2 = j - 1 and "
1408 "1 <= i and i < n and 2 <= j and j <= n }");
1409 map = isl_map_transitive_closure(map, &exact);
1410 assert(exact);
1411 map2 = isl_map_read_from_str(ctx,
1412 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1413 "1 <= i and i < n and 1 <= j and j <= n and "
1414 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1415 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1416 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1417 assert(isl_map_is_equal(map, map2));
1418 isl_map_free(map2);
1419 isl_map_free(map);
1421 map = isl_map_read_from_str(ctx,
1422 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1423 " 0 <= y and y <= n }");
1424 map = isl_map_transitive_closure(map, &exact);
1425 map2 = isl_map_read_from_str(ctx,
1426 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1427 " 0 <= y and y <= n }");
1428 assert(isl_map_is_equal(map, map2));
1429 isl_map_free(map2);
1430 isl_map_free(map);
1432 /* COCOA example 2 */
1433 map = isl_map_read_from_str(ctx,
1434 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1435 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1436 "i2 = i + 2 and j2 = j - 2 and "
1437 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1438 map = isl_map_transitive_closure(map, &exact);
1439 assert(exact);
1440 map2 = isl_map_read_from_str(ctx,
1441 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1442 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1443 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1444 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1445 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1446 assert(isl_map_is_equal(map, map2));
1447 isl_map_free(map);
1448 isl_map_free(map2);
1450 /* COCOA Fig.2 left */
1451 map = isl_map_read_from_str(ctx,
1452 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1453 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1454 "j <= n or "
1455 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1456 "j <= 2 i - 3 and j <= n - 2 or "
1457 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1458 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1459 map = isl_map_transitive_closure(map, &exact);
1460 assert(exact);
1461 isl_map_free(map);
1463 /* COCOA Fig.2 right */
1464 map = isl_map_read_from_str(ctx,
1465 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1466 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1467 "j <= n or "
1468 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1469 "j <= 2 i - 4 and j <= n - 3 or "
1470 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1471 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1472 map = isl_map_power(map, &exact);
1473 assert(exact);
1474 isl_map_free(map);
1476 /* COCOA Fig.2 right */
1477 map = isl_map_read_from_str(ctx,
1478 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1479 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1480 "j <= n or "
1481 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1482 "j <= 2 i - 4 and j <= n - 3 or "
1483 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1484 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1485 map = isl_map_transitive_closure(map, &exact);
1486 assert(exact);
1487 map2 = isl_map_read_from_str(ctx,
1488 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1489 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1490 "j <= n and 3 + i + 2 j <= 3 n and "
1491 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1492 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1493 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1494 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1495 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1496 assert(isl_map_is_equal(map, map2));
1497 isl_map_free(map2);
1498 isl_map_free(map);
1500 /* COCOA Fig.1 right */
1501 dom = isl_set_read_from_str(ctx,
1502 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1503 "2 x - 3 y + 3 >= 0 }");
1504 right = isl_map_read_from_str(ctx,
1505 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1506 up = isl_map_read_from_str(ctx,
1507 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1508 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1509 right = isl_map_intersect_range(right, isl_set_copy(dom));
1510 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1511 up = isl_map_intersect_range(up, dom);
1512 map = isl_map_union(up, right);
1513 map = isl_map_transitive_closure(map, &exact);
1514 assert(exact);
1515 map2 = isl_map_read_from_str(ctx,
1516 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1517 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1518 assert(isl_map_is_equal(map, map2));
1519 isl_map_free(map2);
1520 isl_map_free(map);
1522 /* COCOA Theorem 1 counter example */
1523 map = isl_map_read_from_str(ctx,
1524 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1525 "i2 = 1 and j2 = j or "
1526 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1527 map = isl_map_transitive_closure(map, &exact);
1528 assert(exact);
1529 isl_map_free(map);
1531 map = isl_map_read_from_str(ctx,
1532 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1533 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1534 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1535 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1536 map = isl_map_transitive_closure(map, &exact);
1537 assert(exact);
1538 isl_map_free(map);
1540 /* Kelly et al 1996, fig 12 */
1541 map = isl_map_read_from_str(ctx,
1542 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1543 "1 <= i,j,j+1 <= n or "
1544 "j = n and j2 = 1 and i2 = i + 1 and "
1545 "1 <= i,i+1 <= n }");
1546 map = isl_map_transitive_closure(map, &exact);
1547 assert(exact);
1548 map2 = isl_map_read_from_str(ctx,
1549 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1550 "1 <= i <= n and i = i2 or "
1551 "1 <= i < i2 <= n and 1 <= j <= n and "
1552 "1 <= j2 <= n }");
1553 assert(isl_map_is_equal(map, map2));
1554 isl_map_free(map2);
1555 isl_map_free(map);
1557 /* Omega's closure4 */
1558 map = isl_map_read_from_str(ctx,
1559 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1560 "1 <= x,y <= 10 or "
1561 "x2 = x + 1 and y2 = y and "
1562 "1 <= x <= 20 && 5 <= y <= 15 }");
1563 map = isl_map_transitive_closure(map, &exact);
1564 assert(exact);
1565 isl_map_free(map);
1567 map = isl_map_read_from_str(ctx,
1568 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1569 map = isl_map_transitive_closure(map, &exact);
1570 assert(!exact);
1571 map2 = isl_map_read_from_str(ctx,
1572 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1573 assert(isl_map_is_equal(map, map2));
1574 isl_map_free(map);
1575 isl_map_free(map2);
1577 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1578 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1579 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1580 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1581 map = isl_map_read_from_str(ctx, str);
1582 map = isl_map_transitive_closure(map, &exact);
1583 assert(exact);
1584 map2 = isl_map_read_from_str(ctx, str);
1585 assert(isl_map_is_equal(map, map2));
1586 isl_map_free(map);
1587 isl_map_free(map2);
1589 str = "{[0] -> [1]; [2] -> [3]}";
1590 map = isl_map_read_from_str(ctx, str);
1591 map = isl_map_transitive_closure(map, &exact);
1592 assert(exact);
1593 map2 = isl_map_read_from_str(ctx, str);
1594 assert(isl_map_is_equal(map, map2));
1595 isl_map_free(map);
1596 isl_map_free(map2);
1598 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1599 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1600 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1601 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1602 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1603 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1604 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1605 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1606 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1607 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1608 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1609 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1610 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1611 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1612 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1613 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1614 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1615 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1616 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1617 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1618 map = isl_map_read_from_str(ctx, str);
1619 map = isl_map_transitive_closure(map, NULL);
1620 assert(map);
1621 isl_map_free(map);
1624 void test_lex(struct isl_ctx *ctx)
1626 isl_space *dim;
1627 isl_map *map;
1629 dim = isl_space_set_alloc(ctx, 0, 0);
1630 map = isl_map_lex_le(dim);
1631 assert(!isl_map_is_empty(map));
1632 isl_map_free(map);
1635 static int test_lexmin(struct isl_ctx *ctx)
1637 int equal;
1638 const char *str;
1639 isl_basic_map *bmap;
1640 isl_map *map, *map2;
1641 isl_set *set;
1642 isl_set *set2;
1643 isl_pw_multi_aff *pma;
1645 str = "[p0, p1] -> { [] -> [] : "
1646 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1647 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1648 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1649 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1650 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1651 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1652 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1653 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1654 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1655 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1656 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1657 map = isl_map_read_from_str(ctx, str);
1658 map = isl_map_lexmin(map);
1659 isl_map_free(map);
1661 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1662 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1663 set = isl_set_read_from_str(ctx, str);
1664 set = isl_set_lexmax(set);
1665 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1666 set2 = isl_set_read_from_str(ctx, str);
1667 set = isl_set_intersect(set, set2);
1668 assert(!isl_set_is_empty(set));
1669 isl_set_free(set);
1671 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1672 map = isl_map_read_from_str(ctx, str);
1673 map = isl_map_lexmin(map);
1674 str = "{ [x] -> [5] : 6 <= x <= 8; "
1675 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1676 map2 = isl_map_read_from_str(ctx, str);
1677 assert(isl_map_is_equal(map, map2));
1678 isl_map_free(map);
1679 isl_map_free(map2);
1681 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1682 map = isl_map_read_from_str(ctx, str);
1683 map2 = isl_map_copy(map);
1684 map = isl_map_lexmin(map);
1685 assert(isl_map_is_equal(map, map2));
1686 isl_map_free(map);
1687 isl_map_free(map2);
1689 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1690 map = isl_map_read_from_str(ctx, str);
1691 map = isl_map_lexmin(map);
1692 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1693 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1694 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1695 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1696 map2 = isl_map_read_from_str(ctx, str);
1697 assert(isl_map_is_equal(map, map2));
1698 isl_map_free(map);
1699 isl_map_free(map2);
1701 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1702 " 8i' <= i and 8i' >= -7 + i }";
1703 bmap = isl_basic_map_read_from_str(ctx, str);
1704 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1705 map2 = isl_map_from_pw_multi_aff(pma);
1706 map = isl_map_from_basic_map(bmap);
1707 assert(isl_map_is_equal(map, map2));
1708 isl_map_free(map);
1709 isl_map_free(map2);
1711 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1712 map = isl_map_read_from_str(ctx, str);
1713 map = isl_map_lexmin(map);
1714 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1715 map2 = isl_map_read_from_str(ctx, str);
1716 assert(isl_map_is_equal(map, map2));
1717 isl_map_free(map);
1718 isl_map_free(map2);
1720 /* Check that empty pieces are properly combined. */
1721 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1722 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1723 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1724 map = isl_map_read_from_str(ctx, str);
1725 map = isl_map_lexmin(map);
1726 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1727 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1728 "x >= 4 }";
1729 map2 = isl_map_read_from_str(ctx, str);
1730 assert(isl_map_is_equal(map, map2));
1731 isl_map_free(map);
1732 isl_map_free(map2);
1734 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1735 " 8i' <= i and 8i' >= -7 + i }";
1736 set = isl_set_read_from_str(ctx, str);
1737 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1738 set2 = isl_set_from_pw_multi_aff(pma);
1739 equal = isl_set_is_equal(set, set2);
1740 isl_set_free(set);
1741 isl_set_free(set2);
1742 if (equal < 0)
1743 return -1;
1744 if (!equal)
1745 isl_die(ctx, isl_error_unknown,
1746 "unexpected difference between set and "
1747 "piecewise affine expression", return -1);
1749 return 0;
1752 struct must_may {
1753 isl_map *must;
1754 isl_map *may;
1757 static int collect_must_may(__isl_take isl_map *dep, int must,
1758 void *dep_user, void *user)
1760 struct must_may *mm = (struct must_may *)user;
1762 if (must)
1763 mm->must = isl_map_union(mm->must, dep);
1764 else
1765 mm->may = isl_map_union(mm->may, dep);
1767 return 0;
1770 static int common_space(void *first, void *second)
1772 int depth = *(int *)first;
1773 return 2 * depth;
1776 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1778 isl_map *map2;
1779 int equal;
1781 if (!map)
1782 return -1;
1784 map2 = isl_map_read_from_str(map->ctx, str);
1785 equal = isl_map_is_equal(map, map2);
1786 isl_map_free(map2);
1788 return equal;
1791 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1793 int equal;
1795 equal = map_is_equal(map, str);
1796 if (equal < 0)
1797 return -1;
1798 if (!equal)
1799 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1800 "result not as expected", return -1);
1801 return 0;
1804 void test_dep(struct isl_ctx *ctx)
1806 const char *str;
1807 isl_space *dim;
1808 isl_map *map;
1809 isl_access_info *ai;
1810 isl_flow *flow;
1811 int depth;
1812 struct must_may mm;
1814 depth = 3;
1816 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1817 map = isl_map_read_from_str(ctx, str);
1818 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1820 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1821 map = isl_map_read_from_str(ctx, str);
1822 ai = isl_access_info_add_source(ai, map, 1, &depth);
1824 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1825 map = isl_map_read_from_str(ctx, str);
1826 ai = isl_access_info_add_source(ai, map, 1, &depth);
1828 flow = isl_access_info_compute_flow(ai);
1829 dim = isl_space_alloc(ctx, 0, 3, 3);
1830 mm.must = isl_map_empty(isl_space_copy(dim));
1831 mm.may = isl_map_empty(dim);
1833 isl_flow_foreach(flow, collect_must_may, &mm);
1835 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1836 " [1,10,0] -> [2,5,0] }";
1837 assert(map_is_equal(mm.must, str));
1838 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1839 assert(map_is_equal(mm.may, str));
1841 isl_map_free(mm.must);
1842 isl_map_free(mm.may);
1843 isl_flow_free(flow);
1846 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1847 map = isl_map_read_from_str(ctx, str);
1848 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1850 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1851 map = isl_map_read_from_str(ctx, str);
1852 ai = isl_access_info_add_source(ai, map, 1, &depth);
1854 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1855 map = isl_map_read_from_str(ctx, str);
1856 ai = isl_access_info_add_source(ai, map, 0, &depth);
1858 flow = isl_access_info_compute_flow(ai);
1859 dim = isl_space_alloc(ctx, 0, 3, 3);
1860 mm.must = isl_map_empty(isl_space_copy(dim));
1861 mm.may = isl_map_empty(dim);
1863 isl_flow_foreach(flow, collect_must_may, &mm);
1865 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1866 assert(map_is_equal(mm.must, str));
1867 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1868 assert(map_is_equal(mm.may, str));
1870 isl_map_free(mm.must);
1871 isl_map_free(mm.may);
1872 isl_flow_free(flow);
1875 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1876 map = isl_map_read_from_str(ctx, str);
1877 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1879 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1880 map = isl_map_read_from_str(ctx, str);
1881 ai = isl_access_info_add_source(ai, map, 0, &depth);
1883 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1884 map = isl_map_read_from_str(ctx, str);
1885 ai = isl_access_info_add_source(ai, map, 0, &depth);
1887 flow = isl_access_info_compute_flow(ai);
1888 dim = isl_space_alloc(ctx, 0, 3, 3);
1889 mm.must = isl_map_empty(isl_space_copy(dim));
1890 mm.may = isl_map_empty(dim);
1892 isl_flow_foreach(flow, collect_must_may, &mm);
1894 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1895 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1896 assert(map_is_equal(mm.may, str));
1897 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1898 assert(map_is_equal(mm.must, str));
1900 isl_map_free(mm.must);
1901 isl_map_free(mm.may);
1902 isl_flow_free(flow);
1905 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1906 map = isl_map_read_from_str(ctx, str);
1907 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1909 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1910 map = isl_map_read_from_str(ctx, str);
1911 ai = isl_access_info_add_source(ai, map, 0, &depth);
1913 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1914 map = isl_map_read_from_str(ctx, str);
1915 ai = isl_access_info_add_source(ai, map, 0, &depth);
1917 flow = isl_access_info_compute_flow(ai);
1918 dim = isl_space_alloc(ctx, 0, 3, 3);
1919 mm.must = isl_map_empty(isl_space_copy(dim));
1920 mm.may = isl_map_empty(dim);
1922 isl_flow_foreach(flow, collect_must_may, &mm);
1924 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1925 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1926 assert(map_is_equal(mm.may, str));
1927 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1928 assert(map_is_equal(mm.must, str));
1930 isl_map_free(mm.must);
1931 isl_map_free(mm.may);
1932 isl_flow_free(flow);
1935 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1936 map = isl_map_read_from_str(ctx, str);
1937 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1939 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1940 map = isl_map_read_from_str(ctx, str);
1941 ai = isl_access_info_add_source(ai, map, 0, &depth);
1943 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1944 map = isl_map_read_from_str(ctx, str);
1945 ai = isl_access_info_add_source(ai, map, 0, &depth);
1947 flow = isl_access_info_compute_flow(ai);
1948 dim = isl_space_alloc(ctx, 0, 3, 3);
1949 mm.must = isl_map_empty(isl_space_copy(dim));
1950 mm.may = isl_map_empty(dim);
1952 isl_flow_foreach(flow, collect_must_may, &mm);
1954 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1955 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1956 assert(map_is_equal(mm.may, str));
1957 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1958 assert(map_is_equal(mm.must, str));
1960 isl_map_free(mm.must);
1961 isl_map_free(mm.may);
1962 isl_flow_free(flow);
1965 depth = 5;
1967 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1968 map = isl_map_read_from_str(ctx, str);
1969 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1971 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1972 map = isl_map_read_from_str(ctx, str);
1973 ai = isl_access_info_add_source(ai, map, 1, &depth);
1975 flow = isl_access_info_compute_flow(ai);
1976 dim = isl_space_alloc(ctx, 0, 5, 5);
1977 mm.must = isl_map_empty(isl_space_copy(dim));
1978 mm.may = isl_map_empty(dim);
1980 isl_flow_foreach(flow, collect_must_may, &mm);
1982 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1983 assert(map_is_equal(mm.must, str));
1984 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1985 assert(map_is_equal(mm.may, str));
1987 isl_map_free(mm.must);
1988 isl_map_free(mm.may);
1989 isl_flow_free(flow);
1992 int test_sv(isl_ctx *ctx)
1994 const char *str;
1995 isl_map *map;
1996 isl_union_map *umap;
1997 int sv;
1999 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
2000 map = isl_map_read_from_str(ctx, str);
2001 sv = isl_map_is_single_valued(map);
2002 isl_map_free(map);
2003 if (sv < 0)
2004 return -1;
2005 if (!sv)
2006 isl_die(ctx, isl_error_internal,
2007 "map not detected as single valued", return -1);
2009 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
2010 map = isl_map_read_from_str(ctx, str);
2011 sv = isl_map_is_single_valued(map);
2012 isl_map_free(map);
2013 if (sv < 0)
2014 return -1;
2015 if (sv)
2016 isl_die(ctx, isl_error_internal,
2017 "map detected as single valued", return -1);
2019 str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
2020 umap = isl_union_map_read_from_str(ctx, str);
2021 sv = isl_union_map_is_single_valued(umap);
2022 isl_union_map_free(umap);
2023 if (sv < 0)
2024 return -1;
2025 if (!sv)
2026 isl_die(ctx, isl_error_internal,
2027 "map not detected as single valued", return -1);
2029 str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
2030 umap = isl_union_map_read_from_str(ctx, str);
2031 sv = isl_union_map_is_single_valued(umap);
2032 isl_union_map_free(umap);
2033 if (sv < 0)
2034 return -1;
2035 if (sv)
2036 isl_die(ctx, isl_error_internal,
2037 "map detected as single valued", return -1);
2039 return 0;
2042 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
2044 isl_map *map;
2046 map = isl_map_read_from_str(ctx, str);
2047 if (bijective)
2048 assert(isl_map_is_bijective(map));
2049 else
2050 assert(!isl_map_is_bijective(map));
2051 isl_map_free(map);
2054 void test_bijective(struct isl_ctx *ctx)
2056 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
2057 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
2058 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
2059 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
2060 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
2061 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
2062 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
2063 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2064 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2065 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2066 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2067 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2068 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2071 static int test_pwqp(struct isl_ctx *ctx)
2073 const char *str;
2074 isl_set *set;
2075 isl_pw_qpolynomial *pwqp1, *pwqp2;
2076 int equal;
2078 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2079 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2081 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2082 isl_dim_in, 1, 1);
2084 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2085 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2087 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2089 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2091 isl_pw_qpolynomial_free(pwqp1);
2093 str = "{ [i] -> i }";
2094 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2095 str = "{ [k] : exists a : k = 2a }";
2096 set = isl_set_read_from_str(ctx, str);
2097 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2098 str = "{ [i] -> i }";
2099 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2101 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2103 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2105 isl_pw_qpolynomial_free(pwqp1);
2107 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
2108 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2109 str = "{ [10] }";
2110 set = isl_set_read_from_str(ctx, str);
2111 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2112 str = "{ [i] -> 16 }";
2113 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2115 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2117 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2119 isl_pw_qpolynomial_free(pwqp1);
2121 str = "{ [i] -> ([(i)/2]) }";
2122 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2123 str = "{ [k] : exists a : k = 2a+1 }";
2124 set = isl_set_read_from_str(ctx, str);
2125 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2126 str = "{ [i] -> -1/2 + 1/2 * i }";
2127 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2129 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2131 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2133 isl_pw_qpolynomial_free(pwqp1);
2135 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2136 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2137 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2138 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2140 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2142 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2144 isl_pw_qpolynomial_free(pwqp1);
2146 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2147 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2148 str = "{ [x] -> x }";
2149 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2151 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2153 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2155 isl_pw_qpolynomial_free(pwqp1);
2157 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2158 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2159 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2160 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2161 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2162 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2163 isl_pw_qpolynomial_free(pwqp1);
2165 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2166 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2167 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2168 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2169 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2170 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2171 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2172 isl_pw_qpolynomial_free(pwqp1);
2173 isl_pw_qpolynomial_free(pwqp2);
2174 if (equal < 0)
2175 return -1;
2176 if (!equal)
2177 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2179 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2180 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2181 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2182 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2183 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2184 isl_val_one(ctx));
2185 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2186 isl_pw_qpolynomial_free(pwqp1);
2187 isl_pw_qpolynomial_free(pwqp2);
2188 if (equal < 0)
2189 return -1;
2190 if (!equal)
2191 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2193 return 0;
2196 void test_split_periods(isl_ctx *ctx)
2198 const char *str;
2199 isl_pw_qpolynomial *pwqp;
2201 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2202 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2203 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2204 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2206 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2207 assert(pwqp);
2209 isl_pw_qpolynomial_free(pwqp);
2212 void test_union(isl_ctx *ctx)
2214 const char *str;
2215 isl_union_set *uset1, *uset2;
2216 isl_union_map *umap1, *umap2;
2218 str = "{ [i] : 0 <= i <= 1 }";
2219 uset1 = isl_union_set_read_from_str(ctx, str);
2220 str = "{ [1] -> [0] }";
2221 umap1 = isl_union_map_read_from_str(ctx, str);
2223 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2224 assert(isl_union_map_is_equal(umap1, umap2));
2226 isl_union_map_free(umap1);
2227 isl_union_map_free(umap2);
2229 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2230 umap1 = isl_union_map_read_from_str(ctx, str);
2231 str = "{ A[i]; B[i] }";
2232 uset1 = isl_union_set_read_from_str(ctx, str);
2234 uset2 = isl_union_map_domain(umap1);
2236 assert(isl_union_set_is_equal(uset1, uset2));
2238 isl_union_set_free(uset1);
2239 isl_union_set_free(uset2);
2242 void test_bound(isl_ctx *ctx)
2244 const char *str;
2245 isl_pw_qpolynomial *pwqp;
2246 isl_pw_qpolynomial_fold *pwf;
2248 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2249 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2250 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2251 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2252 isl_pw_qpolynomial_fold_free(pwf);
2254 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2255 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2256 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2257 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2258 isl_pw_qpolynomial_fold_free(pwf);
2261 void test_lift(isl_ctx *ctx)
2263 const char *str;
2264 isl_basic_map *bmap;
2265 isl_basic_set *bset;
2267 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2268 bset = isl_basic_set_read_from_str(ctx, str);
2269 bset = isl_basic_set_lift(bset);
2270 bmap = isl_basic_map_from_range(bset);
2271 bset = isl_basic_map_domain(bmap);
2272 isl_basic_set_free(bset);
2275 struct {
2276 const char *set1;
2277 const char *set2;
2278 int subset;
2279 } subset_tests[] = {
2280 { "{ [112, 0] }",
2281 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2282 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2283 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2284 { "{ [65] }",
2285 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2286 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2287 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2288 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2289 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2290 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2291 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2292 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2293 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2294 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2295 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2296 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2297 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2300 static int test_subset(isl_ctx *ctx)
2302 int i;
2303 isl_set *set1, *set2;
2304 int subset;
2306 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2307 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2308 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2309 subset = isl_set_is_subset(set1, set2);
2310 isl_set_free(set1);
2311 isl_set_free(set2);
2312 if (subset < 0)
2313 return -1;
2314 if (subset != subset_tests[i].subset)
2315 isl_die(ctx, isl_error_unknown,
2316 "incorrect subset result", return -1);
2319 return 0;
2322 struct {
2323 const char *minuend;
2324 const char *subtrahend;
2325 const char *difference;
2326 } subtract_domain_tests[] = {
2327 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2328 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2329 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2332 static int test_subtract(isl_ctx *ctx)
2334 int i;
2335 isl_union_map *umap1, *umap2;
2336 isl_union_set *uset;
2337 int equal;
2339 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2340 umap1 = isl_union_map_read_from_str(ctx,
2341 subtract_domain_tests[i].minuend);
2342 uset = isl_union_set_read_from_str(ctx,
2343 subtract_domain_tests[i].subtrahend);
2344 umap2 = isl_union_map_read_from_str(ctx,
2345 subtract_domain_tests[i].difference);
2346 umap1 = isl_union_map_subtract_domain(umap1, uset);
2347 equal = isl_union_map_is_equal(umap1, umap2);
2348 isl_union_map_free(umap1);
2349 isl_union_map_free(umap2);
2350 if (equal < 0)
2351 return -1;
2352 if (!equal)
2353 isl_die(ctx, isl_error_unknown,
2354 "incorrect subtract domain result", return -1);
2357 return 0;
2360 int test_factorize(isl_ctx *ctx)
2362 const char *str;
2363 isl_basic_set *bset;
2364 isl_factorizer *f;
2366 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2367 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2368 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2369 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2370 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2371 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2372 "3i5 >= -2i0 - i2 + 3i4 }";
2373 bset = isl_basic_set_read_from_str(ctx, str);
2374 f = isl_basic_set_factorizer(bset);
2375 isl_basic_set_free(bset);
2376 isl_factorizer_free(f);
2377 if (!f)
2378 isl_die(ctx, isl_error_unknown,
2379 "failed to construct factorizer", return -1);
2381 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2382 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2383 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2384 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2385 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2386 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2387 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2388 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2389 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2390 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2391 bset = isl_basic_set_read_from_str(ctx, str);
2392 f = isl_basic_set_factorizer(bset);
2393 isl_basic_set_free(bset);
2394 isl_factorizer_free(f);
2395 if (!f)
2396 isl_die(ctx, isl_error_unknown,
2397 "failed to construct factorizer", return -1);
2399 return 0;
2402 static int check_injective(__isl_take isl_map *map, void *user)
2404 int *injective = user;
2406 *injective = isl_map_is_injective(map);
2407 isl_map_free(map);
2409 if (*injective < 0 || !*injective)
2410 return -1;
2412 return 0;
2415 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2416 const char *r, const char *s, int tilable, int parallel)
2418 int i;
2419 isl_union_set *D;
2420 isl_union_map *W, *R, *S;
2421 isl_union_map *empty;
2422 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2423 isl_union_map *validity, *proximity;
2424 isl_union_map *schedule;
2425 isl_union_map *test;
2426 isl_union_set *delta;
2427 isl_union_set *domain;
2428 isl_set *delta_set;
2429 isl_set *slice;
2430 isl_set *origin;
2431 isl_schedule *sched;
2432 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2434 D = isl_union_set_read_from_str(ctx, d);
2435 W = isl_union_map_read_from_str(ctx, w);
2436 R = isl_union_map_read_from_str(ctx, r);
2437 S = isl_union_map_read_from_str(ctx, s);
2439 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2440 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2442 empty = isl_union_map_empty(isl_union_map_get_space(S));
2443 isl_union_map_compute_flow(isl_union_map_copy(R),
2444 isl_union_map_copy(W), empty,
2445 isl_union_map_copy(S),
2446 &dep_raw, NULL, NULL, NULL);
2447 isl_union_map_compute_flow(isl_union_map_copy(W),
2448 isl_union_map_copy(W),
2449 isl_union_map_copy(R),
2450 isl_union_map_copy(S),
2451 &dep_waw, &dep_war, NULL, NULL);
2453 dep = isl_union_map_union(dep_waw, dep_war);
2454 dep = isl_union_map_union(dep, dep_raw);
2455 validity = isl_union_map_copy(dep);
2456 proximity = isl_union_map_copy(dep);
2458 sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
2459 validity, proximity);
2460 schedule = isl_schedule_get_map(sched);
2461 isl_schedule_free(sched);
2462 isl_union_map_free(W);
2463 isl_union_map_free(R);
2464 isl_union_map_free(S);
2466 is_injection = 1;
2467 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2469 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2470 is_complete = isl_union_set_is_subset(D, domain);
2471 isl_union_set_free(D);
2472 isl_union_set_free(domain);
2474 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2475 test = isl_union_map_apply_range(test, dep);
2476 test = isl_union_map_apply_range(test, schedule);
2478 delta = isl_union_map_deltas(test);
2479 if (isl_union_set_n_set(delta) == 0) {
2480 is_tilable = 1;
2481 is_parallel = 1;
2482 is_nonneg = 1;
2483 isl_union_set_free(delta);
2484 } else {
2485 delta_set = isl_set_from_union_set(delta);
2487 slice = isl_set_universe(isl_set_get_space(delta_set));
2488 for (i = 0; i < tilable; ++i)
2489 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2490 is_tilable = isl_set_is_subset(delta_set, slice);
2491 isl_set_free(slice);
2493 slice = isl_set_universe(isl_set_get_space(delta_set));
2494 for (i = 0; i < parallel; ++i)
2495 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2496 is_parallel = isl_set_is_subset(delta_set, slice);
2497 isl_set_free(slice);
2499 origin = isl_set_universe(isl_set_get_space(delta_set));
2500 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2501 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2503 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2504 delta_set = isl_set_lexmin(delta_set);
2506 is_nonneg = isl_set_is_equal(delta_set, origin);
2508 isl_set_free(origin);
2509 isl_set_free(delta_set);
2512 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2513 is_injection < 0 || is_complete < 0)
2514 return -1;
2515 if (!is_complete)
2516 isl_die(ctx, isl_error_unknown,
2517 "generated schedule incomplete", return -1);
2518 if (!is_injection)
2519 isl_die(ctx, isl_error_unknown,
2520 "generated schedule not injective on each statement",
2521 return -1);
2522 if (!is_nonneg)
2523 isl_die(ctx, isl_error_unknown,
2524 "negative dependences in generated schedule",
2525 return -1);
2526 if (!is_tilable)
2527 isl_die(ctx, isl_error_unknown,
2528 "generated schedule not as tilable as expected",
2529 return -1);
2530 if (!is_parallel)
2531 isl_die(ctx, isl_error_unknown,
2532 "generated schedule not as parallel as expected",
2533 return -1);
2535 return 0;
2538 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2539 const char *domain, const char *validity, const char *proximity)
2541 isl_union_set *dom;
2542 isl_union_map *dep;
2543 isl_union_map *prox;
2544 isl_schedule *schedule;
2545 isl_union_map *sched;
2547 dom = isl_union_set_read_from_str(ctx, domain);
2548 dep = isl_union_map_read_from_str(ctx, validity);
2549 prox = isl_union_map_read_from_str(ctx, proximity);
2550 schedule = isl_union_set_compute_schedule(dom, dep, prox);
2551 sched = isl_schedule_get_map(schedule);
2552 isl_schedule_free(schedule);
2554 return sched;
2557 /* Check that a schedule can be constructed on the given domain
2558 * with the given validity and proximity constraints.
2560 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2561 const char *validity, const char *proximity)
2563 isl_union_map *sched;
2565 sched = compute_schedule(ctx, domain, validity, proximity);
2566 if (!sched)
2567 return -1;
2569 isl_union_map_free(sched);
2570 return 0;
2573 int test_special_schedule(isl_ctx *ctx, const char *domain,
2574 const char *validity, const char *proximity, const char *expected_sched)
2576 isl_union_map *sched1, *sched2;
2577 int equal;
2579 sched1 = compute_schedule(ctx, domain, validity, proximity);
2580 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2582 equal = isl_union_map_is_equal(sched1, sched2);
2583 isl_union_map_free(sched1);
2584 isl_union_map_free(sched2);
2586 if (equal < 0)
2587 return -1;
2588 if (!equal)
2589 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2590 return -1);
2592 return 0;
2595 /* Check that the schedule map is properly padded, even after being
2596 * reconstructed from the band forest.
2598 static int test_padded_schedule(isl_ctx *ctx)
2600 const char *str;
2601 isl_union_set *D;
2602 isl_union_map *validity, *proximity;
2603 isl_schedule *sched;
2604 isl_union_map *map1, *map2;
2605 isl_band_list *list;
2606 int equal;
2608 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2609 D = isl_union_set_read_from_str(ctx, str);
2610 validity = isl_union_map_empty(isl_union_set_get_space(D));
2611 proximity = isl_union_map_copy(validity);
2612 sched = isl_union_set_compute_schedule(D, validity, proximity);
2613 map1 = isl_schedule_get_map(sched);
2614 list = isl_schedule_get_band_forest(sched);
2615 isl_band_list_free(list);
2616 map2 = isl_schedule_get_map(sched);
2617 isl_schedule_free(sched);
2618 equal = isl_union_map_is_equal(map1, map2);
2619 isl_union_map_free(map1);
2620 isl_union_map_free(map2);
2622 if (equal < 0)
2623 return -1;
2624 if (!equal)
2625 isl_die(ctx, isl_error_unknown,
2626 "reconstructed schedule map not the same as original",
2627 return -1);
2629 return 0;
2632 int test_schedule(isl_ctx *ctx)
2634 const char *D, *W, *R, *V, *P, *S;
2636 /* Handle resulting schedule with zero bands. */
2637 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2638 return -1;
2640 /* Jacobi */
2641 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2642 W = "{ S1[t,i] -> a[t,i] }";
2643 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2644 "S1[t,i] -> a[t-1,i+1] }";
2645 S = "{ S1[t,i] -> [t,i] }";
2646 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2647 return -1;
2649 /* Fig. 5 of CC2008 */
2650 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2651 "j <= -1 + N }";
2652 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2653 "j >= 2 and j <= -1 + N }";
2654 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2655 "j >= 2 and j <= -1 + N; "
2656 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2657 "j >= 2 and j <= -1 + N }";
2658 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2659 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2660 return -1;
2662 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2663 W = "{ S1[i] -> a[i] }";
2664 R = "{ S2[i] -> a[i+1] }";
2665 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2666 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2667 return -1;
2669 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2670 W = "{ S1[i] -> a[i] }";
2671 R = "{ S2[i] -> a[9-i] }";
2672 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2673 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2674 return -1;
2676 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2677 W = "{ S1[i] -> a[i] }";
2678 R = "[N] -> { S2[i] -> a[N-1-i] }";
2679 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2680 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2681 return -1;
2683 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2684 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2685 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2686 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2687 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2688 return -1;
2690 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2691 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2692 R = "{ S2[i,j] -> a[i-1,j] }";
2693 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2694 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2695 return -1;
2697 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2698 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2699 R = "{ S2[i,j] -> a[i,j-1] }";
2700 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2701 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2702 return -1;
2704 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2705 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2706 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2707 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2708 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2709 "S_0[] -> [0, 0, 0] }";
2710 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2711 return -1;
2712 ctx->opt->schedule_parametric = 0;
2713 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2714 return -1;
2715 ctx->opt->schedule_parametric = 1;
2717 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2718 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2719 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2720 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2721 "S4[i] -> a[i,N] }";
2722 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2723 "S4[i] -> [4,i,0] }";
2724 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2725 return -1;
2727 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2728 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2729 "j <= N }";
2730 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2731 "j <= N; "
2732 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2733 "j <= N }";
2734 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2735 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2736 return -1;
2738 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2739 " S_2[t] : t >= 0 and t <= -1 + N; "
2740 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2741 "i <= -1 + N }";
2742 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2743 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2744 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2745 "i >= 0 and i <= -1 + N }";
2746 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2747 "i >= 0 and i <= -1 + N; "
2748 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2749 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2750 " S_0[t] -> [0, t, 0] }";
2752 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2753 return -1;
2754 ctx->opt->schedule_parametric = 0;
2755 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2756 return -1;
2757 ctx->opt->schedule_parametric = 1;
2759 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2760 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2761 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2762 return -1;
2764 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2765 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2766 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2767 "j >= 0 and j <= -1 + N; "
2768 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2769 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2770 "j >= 0 and j <= -1 + N; "
2771 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2772 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2773 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2774 return -1;
2776 D = "{ S_0[i] : i >= 0 }";
2777 W = "{ S_0[i] -> a[i] : i >= 0 }";
2778 R = "{ S_0[i] -> a[0] : i >= 0 }";
2779 S = "{ S_0[i] -> [0, i, 0] }";
2780 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2781 return -1;
2783 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2784 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2785 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2786 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2787 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2788 return -1;
2790 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2791 "k <= -1 + n and k >= 0 }";
2792 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
2793 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2794 "k <= -1 + n and k >= 0; "
2795 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2796 "k <= -1 + n and k >= 0; "
2797 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2798 "k <= -1 + n and k >= 0 }";
2799 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2800 ctx->opt->schedule_outer_zero_distance = 1;
2801 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2802 return -1;
2803 ctx->opt->schedule_outer_zero_distance = 0;
2805 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
2806 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
2807 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
2808 "Stmt_for_body24[i0, i1, 1, 0]:"
2809 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
2810 "Stmt_for_body7[i0, i1, i2]:"
2811 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
2812 "i2 <= 7 }";
2814 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
2815 "Stmt_for_body24[1, i1, i2, i3]:"
2816 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
2817 "i2 >= 1;"
2818 "Stmt_for_body24[0, i1, i2, i3] -> "
2819 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
2820 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
2821 "i3 >= 0;"
2822 "Stmt_for_body24[0, i1, i2, i3] ->"
2823 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
2824 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
2825 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
2826 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
2827 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
2828 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
2829 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
2830 "i1 <= 6 and i1 >= 0;"
2831 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
2832 "Stmt_for_body7[i0, i1, i2] -> "
2833 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
2834 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
2835 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
2836 "Stmt_for_body7[i0, i1, i2] -> "
2837 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
2838 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
2839 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
2840 P = V;
2841 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
2842 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
2843 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
2845 if (test_special_schedule(ctx, D, V, P, S) < 0)
2846 return -1;
2848 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
2849 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
2850 "j >= 1 and j <= 7;"
2851 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
2852 "j >= 1 and j <= 8 }";
2853 P = "{ }";
2854 S = "{ S_0[i, j] -> [i + j, j] }";
2855 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2856 if (test_special_schedule(ctx, D, V, P, S) < 0)
2857 return -1;
2858 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2860 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
2861 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
2862 "j >= 0 and j <= -1 + i }";
2863 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
2864 "i <= -1 + N and j >= 0;"
2865 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
2866 "i <= -2 + N }";
2867 P = "{ }";
2868 S = "{ S_0[i, j] -> [i, j] }";
2869 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2870 if (test_special_schedule(ctx, D, V, P, S) < 0)
2871 return -1;
2872 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2874 /* Test both algorithms on a case with only proximity dependences. */
2875 D = "{ S[i,j] : 0 <= i <= 10 }";
2876 V = "{ }";
2877 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2878 S = "{ S[i, j] -> [j, i] }";
2879 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2880 if (test_special_schedule(ctx, D, V, P, S) < 0)
2881 return -1;
2882 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2883 if (test_special_schedule(ctx, D, V, P, S) < 0)
2884 return -1;
2886 D = "{ A[a]; B[] }";
2887 V = "{}";
2888 P = "{ A[a] -> B[] }";
2889 if (test_has_schedule(ctx, D, V, P) < 0)
2890 return -1;
2892 if (test_padded_schedule(ctx) < 0)
2893 return -1;
2895 /* Check that check for progress is not confused by rational
2896 * solution.
2898 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
2899 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
2900 "i0 <= -2 + N; "
2901 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
2902 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
2903 P = "{}";
2904 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2905 if (test_has_schedule(ctx, D, V, P) < 0)
2906 return -1;
2907 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2909 return 0;
2912 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2914 isl_union_map *umap;
2915 int test;
2917 umap = isl_union_map_read_from_str(ctx, str);
2918 test = isl_union_map_plain_is_injective(umap);
2919 isl_union_map_free(umap);
2920 if (test < 0)
2921 return -1;
2922 if (test == injective)
2923 return 0;
2924 if (injective)
2925 isl_die(ctx, isl_error_unknown,
2926 "map not detected as injective", return -1);
2927 else
2928 isl_die(ctx, isl_error_unknown,
2929 "map detected as injective", return -1);
2932 int test_injective(isl_ctx *ctx)
2934 const char *str;
2936 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2937 return -1;
2938 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2939 return -1;
2940 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2941 return -1;
2942 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2943 return -1;
2944 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2945 return -1;
2946 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2947 return -1;
2948 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2949 return -1;
2950 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2951 return -1;
2953 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2954 if (test_plain_injective(ctx, str, 1))
2955 return -1;
2956 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2957 if (test_plain_injective(ctx, str, 0))
2958 return -1;
2960 return 0;
2963 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
2965 isl_aff *aff2;
2966 int equal;
2968 if (!aff)
2969 return -1;
2971 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
2972 equal = isl_aff_plain_is_equal(aff, aff2);
2973 isl_aff_free(aff2);
2975 return equal;
2978 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
2980 int equal;
2982 equal = aff_plain_is_equal(aff, str);
2983 if (equal < 0)
2984 return -1;
2985 if (!equal)
2986 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
2987 "result not as expected", return -1);
2988 return 0;
2991 int test_aff(isl_ctx *ctx)
2993 const char *str;
2994 isl_set *set;
2995 isl_space *space;
2996 isl_local_space *ls;
2997 isl_aff *aff;
2998 int zero, equal;
3000 space = isl_space_set_alloc(ctx, 0, 1);
3001 ls = isl_local_space_from_space(space);
3002 aff = isl_aff_zero_on_domain(ls);
3004 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3005 aff = isl_aff_scale_down_ui(aff, 3);
3006 aff = isl_aff_floor(aff);
3007 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3008 aff = isl_aff_scale_down_ui(aff, 2);
3009 aff = isl_aff_floor(aff);
3010 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3012 str = "{ [10] }";
3013 set = isl_set_read_from_str(ctx, str);
3014 aff = isl_aff_gist(aff, set);
3016 aff = isl_aff_add_constant_si(aff, -16);
3017 zero = isl_aff_plain_is_zero(aff);
3018 isl_aff_free(aff);
3020 if (zero < 0)
3021 return -1;
3022 if (!zero)
3023 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3025 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3026 aff = isl_aff_scale_down_ui(aff, 64);
3027 aff = isl_aff_floor(aff);
3028 equal = aff_check_plain_equal(aff, "{ [-1] }");
3029 isl_aff_free(aff);
3030 if (equal < 0)
3031 return -1;
3033 return 0;
3036 int test_dim_max(isl_ctx *ctx)
3038 int equal;
3039 const char *str;
3040 isl_set *set1, *set2;
3041 isl_set *set;
3042 isl_map *map;
3043 isl_pw_aff *pwaff;
3045 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3046 set = isl_set_read_from_str(ctx, str);
3047 pwaff = isl_set_dim_max(set, 0);
3048 set1 = isl_set_from_pw_aff(pwaff);
3049 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3050 set2 = isl_set_read_from_str(ctx, str);
3051 equal = isl_set_is_equal(set1, set2);
3052 isl_set_free(set1);
3053 isl_set_free(set2);
3054 if (equal < 0)
3055 return -1;
3056 if (!equal)
3057 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3059 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3060 set = isl_set_read_from_str(ctx, str);
3061 pwaff = isl_set_dim_max(set, 0);
3062 set1 = isl_set_from_pw_aff(pwaff);
3063 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3064 set2 = isl_set_read_from_str(ctx, str);
3065 equal = isl_set_is_equal(set1, set2);
3066 isl_set_free(set1);
3067 isl_set_free(set2);
3068 if (equal < 0)
3069 return -1;
3070 if (!equal)
3071 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3073 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3074 set = isl_set_read_from_str(ctx, str);
3075 pwaff = isl_set_dim_max(set, 0);
3076 set1 = isl_set_from_pw_aff(pwaff);
3077 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3078 set2 = isl_set_read_from_str(ctx, str);
3079 equal = isl_set_is_equal(set1, set2);
3080 isl_set_free(set1);
3081 isl_set_free(set2);
3082 if (equal < 0)
3083 return -1;
3084 if (!equal)
3085 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3087 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3088 "0 <= i < N and 0 <= j < M }";
3089 map = isl_map_read_from_str(ctx, str);
3090 set = isl_map_range(map);
3092 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3093 set1 = isl_set_from_pw_aff(pwaff);
3094 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3095 set2 = isl_set_read_from_str(ctx, str);
3096 equal = isl_set_is_equal(set1, set2);
3097 isl_set_free(set1);
3098 isl_set_free(set2);
3100 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3101 set1 = isl_set_from_pw_aff(pwaff);
3102 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3103 set2 = isl_set_read_from_str(ctx, str);
3104 if (equal >= 0 && equal)
3105 equal = isl_set_is_equal(set1, set2);
3106 isl_set_free(set1);
3107 isl_set_free(set2);
3109 isl_set_free(set);
3111 if (equal < 0)
3112 return -1;
3113 if (!equal)
3114 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3116 /* Check that solutions are properly merged. */
3117 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3118 "c <= -1 + n - 4a - 2b and c >= -2b and "
3119 "4a >= -4 + n and c >= 0 }";
3120 set = isl_set_read_from_str(ctx, str);
3121 pwaff = isl_set_dim_min(set, 2);
3122 set1 = isl_set_from_pw_aff(pwaff);
3123 str = "[n] -> { [(0)] : n >= 1 }";
3124 set2 = isl_set_read_from_str(ctx, str);
3125 equal = isl_set_is_equal(set1, set2);
3126 isl_set_free(set1);
3127 isl_set_free(set2);
3129 if (equal < 0)
3130 return -1;
3131 if (!equal)
3132 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3134 return 0;
3137 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
3139 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
3140 const char *str)
3142 isl_ctx *ctx;
3143 isl_pw_multi_aff *pma2;
3144 int equal;
3146 if (!pma)
3147 return -1;
3149 ctx = isl_pw_multi_aff_get_ctx(pma);
3150 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3151 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
3152 isl_pw_multi_aff_free(pma2);
3154 return equal;
3157 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
3158 * represented by "str".
3160 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
3161 const char *str)
3163 int equal;
3165 equal = pw_multi_aff_plain_is_equal(pma, str);
3166 if (equal < 0)
3167 return -1;
3168 if (!equal)
3169 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
3170 "result not as expected", return -1);
3171 return 0;
3174 /* Basic test for isl_pw_multi_aff_product.
3176 * Check that multiple pieces are properly handled.
3178 static int test_product_pma(isl_ctx *ctx)
3180 int equal;
3181 const char *str;
3182 isl_pw_multi_aff *pma1, *pma2;
3184 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
3185 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3186 str = "{ C[] -> D[] }";
3187 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3188 pma1 = isl_pw_multi_aff_product(pma1, pma2);
3189 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
3190 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
3191 equal = pw_multi_aff_check_plain_equal(pma1, str);
3192 isl_pw_multi_aff_free(pma1);
3193 if (equal < 0)
3194 return -1;
3196 return 0;
3199 int test_product(isl_ctx *ctx)
3201 const char *str;
3202 isl_set *set;
3203 isl_union_set *uset1, *uset2;
3204 int ok;
3206 str = "{ A[i] }";
3207 set = isl_set_read_from_str(ctx, str);
3208 set = isl_set_product(set, isl_set_copy(set));
3209 ok = isl_set_is_wrapping(set);
3210 isl_set_free(set);
3211 if (ok < 0)
3212 return -1;
3213 if (!ok)
3214 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3216 str = "{ [] }";
3217 uset1 = isl_union_set_read_from_str(ctx, str);
3218 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3219 str = "{ [[] -> []] }";
3220 uset2 = isl_union_set_read_from_str(ctx, str);
3221 ok = isl_union_set_is_equal(uset1, uset2);
3222 isl_union_set_free(uset1);
3223 isl_union_set_free(uset2);
3224 if (ok < 0)
3225 return -1;
3226 if (!ok)
3227 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3229 if (test_product_pma(ctx) < 0)
3230 return -1;
3232 return 0;
3235 int test_equal(isl_ctx *ctx)
3237 const char *str;
3238 isl_set *set, *set2;
3239 int equal;
3241 str = "{ S_6[i] }";
3242 set = isl_set_read_from_str(ctx, str);
3243 str = "{ S_7[i] }";
3244 set2 = isl_set_read_from_str(ctx, str);
3245 equal = isl_set_is_equal(set, set2);
3246 isl_set_free(set);
3247 isl_set_free(set2);
3248 if (equal < 0)
3249 return -1;
3250 if (equal)
3251 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3253 return 0;
3256 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3257 enum isl_dim_type type, unsigned pos, int fixed)
3259 int test;
3261 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3262 isl_map_free(map);
3263 if (test < 0)
3264 return -1;
3265 if (test == fixed)
3266 return 0;
3267 if (fixed)
3268 isl_die(ctx, isl_error_unknown,
3269 "map not detected as fixed", return -1);
3270 else
3271 isl_die(ctx, isl_error_unknown,
3272 "map detected as fixed", return -1);
3275 int test_fixed(isl_ctx *ctx)
3277 const char *str;
3278 isl_map *map;
3280 str = "{ [i] -> [i] }";
3281 map = isl_map_read_from_str(ctx, str);
3282 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3283 return -1;
3284 str = "{ [i] -> [1] }";
3285 map = isl_map_read_from_str(ctx, str);
3286 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3287 return -1;
3288 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3289 map = isl_map_read_from_str(ctx, str);
3290 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3291 return -1;
3292 map = isl_map_read_from_str(ctx, str);
3293 map = isl_map_neg(map);
3294 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3295 return -1;
3297 return 0;
3300 int test_vertices(isl_ctx *ctx)
3302 const char *str;
3303 isl_basic_set *bset;
3304 isl_vertices *vertices;
3306 str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
3307 bset = isl_basic_set_read_from_str(ctx, str);
3308 vertices = isl_basic_set_compute_vertices(bset);
3309 isl_basic_set_free(bset);
3310 isl_vertices_free(vertices);
3311 if (!vertices)
3312 return -1;
3314 str = "{ A[t, i] : t = 14 and i = 1 }";
3315 bset = isl_basic_set_read_from_str(ctx, str);
3316 vertices = isl_basic_set_compute_vertices(bset);
3317 isl_basic_set_free(bset);
3318 isl_vertices_free(vertices);
3319 if (!vertices)
3320 return -1;
3322 return 0;
3325 int test_union_pw(isl_ctx *ctx)
3327 int equal;
3328 const char *str;
3329 isl_union_set *uset;
3330 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3332 str = "{ [x] -> x^2 }";
3333 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3334 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3335 uset = isl_union_pw_qpolynomial_domain(upwqp1);
3336 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3337 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3338 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3339 isl_union_pw_qpolynomial_free(upwqp1);
3340 isl_union_pw_qpolynomial_free(upwqp2);
3341 if (equal < 0)
3342 return -1;
3343 if (!equal)
3344 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3346 return 0;
3349 int test_output(isl_ctx *ctx)
3351 char *s;
3352 const char *str;
3353 isl_pw_aff *pa;
3354 isl_printer *p;
3355 int equal;
3357 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3358 pa = isl_pw_aff_read_from_str(ctx, str);
3360 p = isl_printer_to_str(ctx);
3361 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3362 p = isl_printer_print_pw_aff(p, pa);
3363 s = isl_printer_get_str(p);
3364 isl_printer_free(p);
3365 isl_pw_aff_free(pa);
3366 if (!s)
3367 equal = -1;
3368 else
3369 equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
3370 free(s);
3371 if (equal < 0)
3372 return -1;
3373 if (!equal)
3374 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3376 return 0;
3379 int test_sample(isl_ctx *ctx)
3381 const char *str;
3382 isl_basic_set *bset1, *bset2;
3383 int empty, subset;
3385 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3386 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3387 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3388 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3389 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3390 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3391 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3392 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3393 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3394 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3395 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3396 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3397 "d - 1073741821e and "
3398 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3399 "3j >= 1 - a + b + 2e and "
3400 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3401 "3i <= 4 - a + 4b - e and "
3402 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3403 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3404 "c <= -1 + a and 3i >= -2 - a + 3e and "
3405 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3406 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3407 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3408 "1073741823e >= 1 + 1073741823b - d and "
3409 "3i >= 1073741823b + c - 1073741823e - f and "
3410 "3i >= 1 + 2b + e + 3g }";
3411 bset1 = isl_basic_set_read_from_str(ctx, str);
3412 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3413 empty = isl_basic_set_is_empty(bset2);
3414 subset = isl_basic_set_is_subset(bset2, bset1);
3415 isl_basic_set_free(bset1);
3416 isl_basic_set_free(bset2);
3417 if (empty < 0 || subset < 0)
3418 return -1;
3419 if (empty)
3420 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3421 if (!subset)
3422 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3424 return 0;
3427 int test_fixed_power(isl_ctx *ctx)
3429 const char *str;
3430 isl_map *map;
3431 isl_int exp;
3432 int equal;
3434 isl_int_init(exp);
3435 str = "{ [i] -> [i + 1] }";
3436 map = isl_map_read_from_str(ctx, str);
3437 isl_int_set_si(exp, 23);
3438 map = isl_map_fixed_power(map, exp);
3439 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3440 isl_int_clear(exp);
3441 isl_map_free(map);
3442 if (equal < 0)
3443 return -1;
3445 return 0;
3448 int test_slice(isl_ctx *ctx)
3450 const char *str;
3451 isl_map *map;
3452 int equal;
3454 str = "{ [i] -> [j] }";
3455 map = isl_map_read_from_str(ctx, str);
3456 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3457 equal = map_check_equal(map, "{ [i] -> [i] }");
3458 isl_map_free(map);
3459 if (equal < 0)
3460 return -1;
3462 str = "{ [i] -> [j] }";
3463 map = isl_map_read_from_str(ctx, str);
3464 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
3465 equal = map_check_equal(map, "{ [i] -> [j] }");
3466 isl_map_free(map);
3467 if (equal < 0)
3468 return -1;
3470 str = "{ [i] -> [j] }";
3471 map = isl_map_read_from_str(ctx, str);
3472 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
3473 equal = map_check_equal(map, "{ [i] -> [-i] }");
3474 isl_map_free(map);
3475 if (equal < 0)
3476 return -1;
3478 str = "{ [i] -> [j] }";
3479 map = isl_map_read_from_str(ctx, str);
3480 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
3481 equal = map_check_equal(map, "{ [0] -> [j] }");
3482 isl_map_free(map);
3483 if (equal < 0)
3484 return -1;
3486 str = "{ [i] -> [j] }";
3487 map = isl_map_read_from_str(ctx, str);
3488 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3489 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3490 isl_map_free(map);
3491 if (equal < 0)
3492 return -1;
3494 str = "{ [i] -> [j] }";
3495 map = isl_map_read_from_str(ctx, str);
3496 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3497 equal = map_check_equal(map, "{ [i] -> [j] : false }");
3498 isl_map_free(map);
3499 if (equal < 0)
3500 return -1;
3502 return 0;
3505 int test_eliminate(isl_ctx *ctx)
3507 const char *str;
3508 isl_map *map;
3509 int equal;
3511 str = "{ [i] -> [j] : i = 2j }";
3512 map = isl_map_read_from_str(ctx, str);
3513 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3514 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3515 isl_map_free(map);
3516 if (equal < 0)
3517 return -1;
3519 return 0;
3522 /* Check that isl_set_dim_residue_class detects that the values of j
3523 * in the set below are all odd and that it does not detect any spurious
3524 * strides.
3526 static int test_residue_class(isl_ctx *ctx)
3528 const char *str;
3529 isl_set *set;
3530 isl_int m, r;
3531 int res;
3533 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3534 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3535 set = isl_set_read_from_str(ctx, str);
3536 isl_int_init(m);
3537 isl_int_init(r);
3538 res = isl_set_dim_residue_class(set, 1, &m, &r);
3539 if (res >= 0 &&
3540 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3541 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3542 res = -1);
3543 isl_int_clear(r);
3544 isl_int_clear(m);
3545 isl_set_free(set);
3547 return res;
3550 int test_align_parameters(isl_ctx *ctx)
3552 const char *str;
3553 isl_space *space;
3554 isl_multi_aff *ma1, *ma2;
3555 int equal;
3557 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3558 ma1 = isl_multi_aff_read_from_str(ctx, str);
3560 space = isl_space_params_alloc(ctx, 1);
3561 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
3562 ma1 = isl_multi_aff_align_params(ma1, space);
3564 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3565 ma2 = isl_multi_aff_read_from_str(ctx, str);
3567 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3569 isl_multi_aff_free(ma1);
3570 isl_multi_aff_free(ma2);
3572 if (equal < 0)
3573 return -1;
3574 if (!equal)
3575 isl_die(ctx, isl_error_unknown,
3576 "result not as expected", return -1);
3578 return 0;
3581 static int test_list(isl_ctx *ctx)
3583 isl_id *a, *b, *c, *d, *id;
3584 isl_id_list *list;
3585 int ok;
3587 a = isl_id_alloc(ctx, "a", NULL);
3588 b = isl_id_alloc(ctx, "b", NULL);
3589 c = isl_id_alloc(ctx, "c", NULL);
3590 d = isl_id_alloc(ctx, "d", NULL);
3592 list = isl_id_list_alloc(ctx, 4);
3593 list = isl_id_list_add(list, a);
3594 list = isl_id_list_add(list, b);
3595 list = isl_id_list_add(list, c);
3596 list = isl_id_list_add(list, d);
3597 list = isl_id_list_drop(list, 1, 1);
3599 if (isl_id_list_n_id(list) != 3) {
3600 isl_id_list_free(list);
3601 isl_die(ctx, isl_error_unknown,
3602 "unexpected number of elements in list", return -1);
3605 id = isl_id_list_get_id(list, 0);
3606 ok = id == a;
3607 isl_id_free(id);
3608 id = isl_id_list_get_id(list, 1);
3609 ok = ok && id == c;
3610 isl_id_free(id);
3611 id = isl_id_list_get_id(list, 2);
3612 ok = ok && id == d;
3613 isl_id_free(id);
3615 isl_id_list_free(list);
3617 if (!ok)
3618 isl_die(ctx, isl_error_unknown,
3619 "unexpected elements in list", return -1);
3621 return 0;
3624 const char *set_conversion_tests[] = {
3625 "[N] -> { [i] : N - 1 <= 2 i <= N }",
3626 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
3627 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3628 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3631 /* Check that converting from isl_set to isl_pw_multi_aff and back
3632 * to isl_set produces the original isl_set.
3634 static int test_set_conversion(isl_ctx *ctx)
3636 int i;
3637 const char *str;
3638 isl_set *set1, *set2;
3639 isl_pw_multi_aff *pma;
3640 int equal;
3642 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
3643 str = set_conversion_tests[i];
3644 set1 = isl_set_read_from_str(ctx, str);
3645 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
3646 set2 = isl_set_from_pw_multi_aff(pma);
3647 equal = isl_set_is_equal(set1, set2);
3648 isl_set_free(set1);
3649 isl_set_free(set2);
3651 if (equal < 0)
3652 return -1;
3653 if (!equal)
3654 isl_die(ctx, isl_error_unknown, "bad conversion",
3655 return -1);
3658 return 0;
3661 /* Check that converting from isl_map to isl_pw_multi_aff and back
3662 * to isl_map produces the original isl_map.
3664 static int test_map_conversion(isl_ctx *ctx)
3666 const char *str;
3667 isl_map *map1, *map2;
3668 isl_pw_multi_aff *pma;
3669 int equal;
3671 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
3672 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
3673 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
3674 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
3675 "9e <= -2 - 2a) }";
3676 map1 = isl_map_read_from_str(ctx, str);
3677 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
3678 map2 = isl_map_from_pw_multi_aff(pma);
3679 equal = isl_map_is_equal(map1, map2);
3680 isl_map_free(map1);
3681 isl_map_free(map2);
3683 if (equal < 0)
3684 return -1;
3685 if (!equal)
3686 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
3688 return 0;
3691 static int test_conversion(isl_ctx *ctx)
3693 if (test_set_conversion(ctx) < 0)
3694 return -1;
3695 if (test_map_conversion(ctx) < 0)
3696 return -1;
3697 return 0;
3700 /* Check that isl_basic_map_curry does not modify input.
3702 static int test_curry(isl_ctx *ctx)
3704 const char *str;
3705 isl_basic_map *bmap1, *bmap2;
3706 int equal;
3708 str = "{ [A[] -> B[]] -> C[] }";
3709 bmap1 = isl_basic_map_read_from_str(ctx, str);
3710 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
3711 equal = isl_basic_map_is_equal(bmap1, bmap2);
3712 isl_basic_map_free(bmap1);
3713 isl_basic_map_free(bmap2);
3715 if (equal < 0)
3716 return -1;
3717 if (equal)
3718 isl_die(ctx, isl_error_unknown,
3719 "curried map should not be equal to original",
3720 return -1);
3722 return 0;
3725 struct {
3726 const char *set;
3727 const char *ma;
3728 const char *res;
3729 } preimage_tests[] = {
3730 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
3731 "{ A[j,i] -> B[i,j] }",
3732 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
3733 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3734 "{ A[a,b] -> B[a/2,b/6] }",
3735 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
3736 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3737 "{ A[a,b] -> B[a/2,b/6] }",
3738 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
3739 "exists i,j : a = 2 i and b = 6 j }" },
3740 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
3741 "[n] -> { : 0 <= n <= 100 }" },
3742 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3743 "{ A[a] -> B[2a] }",
3744 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
3745 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3746 "{ A[a] -> B[([a/2])] }",
3747 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
3748 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
3749 "{ A[a] -> B[a,a,a/3] }",
3750 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
3751 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
3752 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
3755 static int test_preimage_basic_set(isl_ctx *ctx)
3757 int i;
3758 isl_basic_set *bset1, *bset2;
3759 isl_multi_aff *ma;
3760 int equal;
3762 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
3763 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
3764 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
3765 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
3766 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
3767 equal = isl_basic_set_is_equal(bset1, bset2);
3768 isl_basic_set_free(bset1);
3769 isl_basic_set_free(bset2);
3770 if (equal < 0)
3771 return -1;
3772 if (!equal)
3773 isl_die(ctx, isl_error_unknown, "bad preimage",
3774 return -1);
3777 return 0;
3780 struct {
3781 const char *map;
3782 const char *ma;
3783 const char *res;
3784 } preimage_domain_tests[] = {
3785 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
3786 "{ A[j,i] -> B[i,j] }",
3787 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
3788 { "{ B[i] -> C[i]; D[i] -> E[i] }",
3789 "{ A[i] -> B[i + 1] }",
3790 "{ A[i] -> C[i + 1] }" },
3791 { "{ B[i] -> C[i]; B[i] -> E[i] }",
3792 "{ A[i] -> B[i + 1] }",
3793 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
3794 { "{ B[i] -> C[([i/2])] }",
3795 "{ A[i] -> B[2i] }",
3796 "{ A[i] -> C[i] }" },
3797 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
3798 "{ A[i] -> B[([i/5]), ([i/7])] }",
3799 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
3800 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
3801 "[N] -> { A[] -> B[([N/5])] }",
3802 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
3803 { "{ B[i] -> C[i] : exists a : i = 5 a }",
3804 "{ A[i] -> B[2i] }",
3805 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
3806 { "{ B[i] -> C[i] : exists a : i = 2 a; "
3807 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
3808 "{ A[i] -> B[2i] }",
3809 "{ A[i] -> C[2i] }" },
3812 static int test_preimage_union_map(isl_ctx *ctx)
3814 int i;
3815 isl_union_map *umap1, *umap2;
3816 isl_multi_aff *ma;
3817 int equal;
3819 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
3820 umap1 = isl_union_map_read_from_str(ctx,
3821 preimage_domain_tests[i].map);
3822 ma = isl_multi_aff_read_from_str(ctx,
3823 preimage_domain_tests[i].ma);
3824 umap2 = isl_union_map_read_from_str(ctx,
3825 preimage_domain_tests[i].res);
3826 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
3827 equal = isl_union_map_is_equal(umap1, umap2);
3828 isl_union_map_free(umap1);
3829 isl_union_map_free(umap2);
3830 if (equal < 0)
3831 return -1;
3832 if (!equal)
3833 isl_die(ctx, isl_error_unknown, "bad preimage",
3834 return -1);
3837 return 0;
3840 static int test_preimage(isl_ctx *ctx)
3842 if (test_preimage_basic_set(ctx) < 0)
3843 return -1;
3844 if (test_preimage_union_map(ctx) < 0)
3845 return -1;
3847 return 0;
3850 struct {
3851 const char *ma1;
3852 const char *ma;
3853 const char *res;
3854 } pullback_tests[] = {
3855 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
3856 "{ A[a,b] -> C[b + 2a] }" },
3857 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
3858 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
3859 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
3860 "{ A[a] -> C[(a)/6] }" },
3861 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
3862 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
3863 "{ A[a] -> C[(2a)/3] }" },
3864 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
3865 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
3866 "{ A[i,j] -> C[i + j, i + j] }"},
3867 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
3868 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
3869 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
3872 static int test_pullback(isl_ctx *ctx)
3874 int i;
3875 isl_multi_aff *ma1, *ma2;
3876 isl_multi_aff *ma;
3877 int equal;
3879 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
3880 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
3881 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
3882 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
3883 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
3884 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3885 isl_multi_aff_free(ma1);
3886 isl_multi_aff_free(ma2);
3887 if (equal < 0)
3888 return -1;
3889 if (!equal)
3890 isl_die(ctx, isl_error_unknown, "bad pullback",
3891 return -1);
3894 return 0;
3897 /* Check that negation is printed correctly.
3899 static int test_ast(isl_ctx *ctx)
3901 isl_ast_expr *expr, *expr1, *expr2, *expr3;
3902 char *str;
3903 int ok;
3905 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3906 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3907 expr = isl_ast_expr_add(expr1, expr2);
3908 expr = isl_ast_expr_neg(expr);
3909 str = isl_ast_expr_to_str(expr);
3910 ok = str ? !strcmp(str, "-(A + B)") : -1;
3911 free(str);
3912 isl_ast_expr_free(expr);
3914 if (ok < 0)
3915 return -1;
3916 if (!ok)
3917 isl_die(ctx, isl_error_unknown,
3918 "isl_ast_expr printed incorrectly", return -1);
3920 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3921 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3922 expr = isl_ast_expr_add(expr1, expr2);
3923 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
3924 expr = isl_ast_expr_sub(expr3, expr);
3925 str = isl_ast_expr_to_str(expr);
3926 ok = str ? !strcmp(str, "C - (A + B)") : -1;
3927 free(str);
3928 isl_ast_expr_free(expr);
3930 if (ok < 0)
3931 return -1;
3932 if (!ok)
3933 isl_die(ctx, isl_error_unknown,
3934 "isl_ast_expr printed incorrectly", return -1);
3936 return 0;
3939 /* Internal data structure for before_for and after_for callbacks.
3941 * depth is the current depth
3942 * before is the number of times before_for has been called
3943 * after is the number of times after_for has been called
3945 struct isl_test_codegen_data {
3946 int depth;
3947 int before;
3948 int after;
3951 /* This function is called before each for loop in the AST generated
3952 * from test_ast_gen1.
3954 * Increment the number of calls and the depth.
3955 * Check that the space returned by isl_ast_build_get_schedule_space
3956 * matches the target space of the schedule returned by
3957 * isl_ast_build_get_schedule.
3958 * Return an isl_id that is checked by the corresponding call
3959 * to after_for.
3961 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
3962 void *user)
3964 struct isl_test_codegen_data *data = user;
3965 isl_ctx *ctx;
3966 isl_space *space;
3967 isl_union_map *schedule;
3968 isl_union_set *uset;
3969 isl_set *set;
3970 int empty;
3971 char name[] = "d0";
3973 ctx = isl_ast_build_get_ctx(build);
3975 if (data->before >= 3)
3976 isl_die(ctx, isl_error_unknown,
3977 "unexpected number of for nodes", return NULL);
3978 if (data->depth >= 2)
3979 isl_die(ctx, isl_error_unknown,
3980 "unexpected depth", return NULL);
3982 snprintf(name, sizeof(name), "d%d", data->depth);
3983 data->before++;
3984 data->depth++;
3986 schedule = isl_ast_build_get_schedule(build);
3987 uset = isl_union_map_range(schedule);
3988 if (!uset)
3989 return NULL;
3990 if (isl_union_set_n_set(uset) != 1) {
3991 isl_union_set_free(uset);
3992 isl_die(ctx, isl_error_unknown,
3993 "expecting single range space", return NULL);
3996 space = isl_ast_build_get_schedule_space(build);
3997 set = isl_union_set_extract_set(uset, space);
3998 isl_union_set_free(uset);
3999 empty = isl_set_is_empty(set);
4000 isl_set_free(set);
4002 if (empty < 0)
4003 return NULL;
4004 if (empty)
4005 isl_die(ctx, isl_error_unknown,
4006 "spaces don't match", return NULL);
4008 return isl_id_alloc(ctx, name, NULL);
4011 /* This function is called after each for loop in the AST generated
4012 * from test_ast_gen1.
4014 * Increment the number of calls and decrement the depth.
4015 * Check that the annotation attached to the node matches
4016 * the isl_id returned by the corresponding call to before_for.
4018 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
4019 __isl_keep isl_ast_build *build, void *user)
4021 struct isl_test_codegen_data *data = user;
4022 isl_id *id;
4023 const char *name;
4024 int valid;
4026 data->after++;
4027 data->depth--;
4029 if (data->after > data->before)
4030 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4031 "mismatch in number of for nodes",
4032 return isl_ast_node_free(node));
4034 id = isl_ast_node_get_annotation(node);
4035 if (!id)
4036 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4037 "missing annotation", return isl_ast_node_free(node));
4039 name = isl_id_get_name(id);
4040 valid = name && atoi(name + 1) == data->depth;
4041 isl_id_free(id);
4043 if (!valid)
4044 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4045 "wrong annotation", return isl_ast_node_free(node));
4047 return node;
4050 /* Check that the before_each_for and after_each_for callbacks
4051 * are called for each for loop in the generated code,
4052 * that they are called in the right order and that the isl_id
4053 * returned from the before_each_for callback is attached to
4054 * the isl_ast_node passed to the corresponding after_each_for call.
4056 static int test_ast_gen1(isl_ctx *ctx)
4058 const char *str;
4059 isl_set *set;
4060 isl_union_map *schedule;
4061 isl_ast_build *build;
4062 isl_ast_node *tree;
4063 struct isl_test_codegen_data data;
4065 str = "[N] -> { : N >= 10 }";
4066 set = isl_set_read_from_str(ctx, str);
4067 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
4068 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
4069 schedule = isl_union_map_read_from_str(ctx, str);
4071 data.before = 0;
4072 data.after = 0;
4073 data.depth = 0;
4074 build = isl_ast_build_from_context(set);
4075 build = isl_ast_build_set_before_each_for(build,
4076 &before_for, &data);
4077 build = isl_ast_build_set_after_each_for(build,
4078 &after_for, &data);
4079 tree = isl_ast_build_ast_from_schedule(build, schedule);
4080 isl_ast_build_free(build);
4081 if (!tree)
4082 return -1;
4084 isl_ast_node_free(tree);
4086 if (data.before != 3 || data.after != 3)
4087 isl_die(ctx, isl_error_unknown,
4088 "unexpected number of for nodes", return -1);
4090 return 0;
4093 /* Check that the AST generator handles domains that are integrally disjoint
4094 * but not ratinoally disjoint.
4096 static int test_ast_gen2(isl_ctx *ctx)
4098 const char *str;
4099 isl_set *set;
4100 isl_union_map *schedule;
4101 isl_union_map *options;
4102 isl_ast_build *build;
4103 isl_ast_node *tree;
4105 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4106 schedule = isl_union_map_read_from_str(ctx, str);
4107 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4108 build = isl_ast_build_from_context(set);
4110 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4111 options = isl_union_map_read_from_str(ctx, str);
4112 build = isl_ast_build_set_options(build, options);
4113 tree = isl_ast_build_ast_from_schedule(build, schedule);
4114 isl_ast_build_free(build);
4115 if (!tree)
4116 return -1;
4117 isl_ast_node_free(tree);
4119 return 0;
4122 /* Increment *user on each call.
4124 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4125 __isl_keep isl_ast_build *build, void *user)
4127 int *n = user;
4129 (*n)++;
4131 return node;
4134 /* Test that unrolling tries to minimize the number of instances.
4135 * In particular, for the schedule given below, make sure it generates
4136 * 3 nodes (rather than 101).
4138 static int test_ast_gen3(isl_ctx *ctx)
4140 const char *str;
4141 isl_set *set;
4142 isl_union_map *schedule;
4143 isl_union_map *options;
4144 isl_ast_build *build;
4145 isl_ast_node *tree;
4146 int n_domain = 0;
4148 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4149 schedule = isl_union_map_read_from_str(ctx, str);
4150 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4152 str = "{ [i] -> unroll[0] }";
4153 options = isl_union_map_read_from_str(ctx, str);
4155 build = isl_ast_build_from_context(set);
4156 build = isl_ast_build_set_options(build, options);
4157 build = isl_ast_build_set_at_each_domain(build,
4158 &count_domains, &n_domain);
4159 tree = isl_ast_build_ast_from_schedule(build, schedule);
4160 isl_ast_build_free(build);
4161 if (!tree)
4162 return -1;
4164 isl_ast_node_free(tree);
4166 if (n_domain != 3)
4167 isl_die(ctx, isl_error_unknown,
4168 "unexpected number of for nodes", return -1);
4170 return 0;
4173 /* Check that if the ast_build_exploit_nested_bounds options is set,
4174 * we do not get an outer if node in the generated AST,
4175 * while we do get such an outer if node if the options is not set.
4177 static int test_ast_gen4(isl_ctx *ctx)
4179 const char *str;
4180 isl_set *set;
4181 isl_union_map *schedule;
4182 isl_ast_build *build;
4183 isl_ast_node *tree;
4184 enum isl_ast_node_type type;
4185 int enb;
4187 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4188 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4190 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4192 schedule = isl_union_map_read_from_str(ctx, str);
4193 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4194 build = isl_ast_build_from_context(set);
4195 tree = isl_ast_build_ast_from_schedule(build, schedule);
4196 isl_ast_build_free(build);
4197 if (!tree)
4198 return -1;
4200 type = isl_ast_node_get_type(tree);
4201 isl_ast_node_free(tree);
4203 if (type == isl_ast_node_if)
4204 isl_die(ctx, isl_error_unknown,
4205 "not expecting if node", return -1);
4207 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4209 schedule = isl_union_map_read_from_str(ctx, str);
4210 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4211 build = isl_ast_build_from_context(set);
4212 tree = isl_ast_build_ast_from_schedule(build, schedule);
4213 isl_ast_build_free(build);
4214 if (!tree)
4215 return -1;
4217 type = isl_ast_node_get_type(tree);
4218 isl_ast_node_free(tree);
4220 if (type != isl_ast_node_if)
4221 isl_die(ctx, isl_error_unknown,
4222 "expecting if node", return -1);
4224 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4226 return 0;
4229 /* This function is called for each leaf in the AST generated
4230 * from test_ast_gen5.
4232 * We finalize the AST generation by extending the outer schedule
4233 * with a zero-dimensional schedule. If this results in any for loops,
4234 * then this means that we did not pass along enough information
4235 * about the outer schedule to the inner AST generation.
4237 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4238 void *user)
4240 isl_union_map *schedule, *extra;
4241 isl_ast_node *tree;
4243 schedule = isl_ast_build_get_schedule(build);
4244 extra = isl_union_map_copy(schedule);
4245 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4246 schedule = isl_union_map_range_product(schedule, extra);
4247 tree = isl_ast_build_ast_from_schedule(build, schedule);
4248 isl_ast_build_free(build);
4250 if (!tree)
4251 return NULL;
4253 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4254 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4255 "code should not contain any for loop",
4256 return isl_ast_node_free(tree));
4258 return tree;
4261 /* Check that we do not lose any information when going back and
4262 * forth between internal and external schedule.
4264 * In particular, we create an AST where we unroll the only
4265 * non-constant dimension in the schedule. We therefore do
4266 * not expect any for loops in the AST. However, older versions
4267 * of isl would not pass along enough information about the outer
4268 * schedule when performing an inner code generation from a create_leaf
4269 * callback, resulting in the inner code generation producing a for loop.
4271 static int test_ast_gen5(isl_ctx *ctx)
4273 const char *str;
4274 isl_set *set;
4275 isl_union_map *schedule, *options;
4276 isl_ast_build *build;
4277 isl_ast_node *tree;
4279 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4280 schedule = isl_union_map_read_from_str(ctx, str);
4282 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4283 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4284 options = isl_union_map_read_from_str(ctx, str);
4286 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4287 build = isl_ast_build_from_context(set);
4288 build = isl_ast_build_set_options(build, options);
4289 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4290 tree = isl_ast_build_ast_from_schedule(build, schedule);
4291 isl_ast_build_free(build);
4292 isl_ast_node_free(tree);
4293 if (!tree)
4294 return -1;
4296 return 0;
4299 static int test_ast_gen(isl_ctx *ctx)
4301 if (test_ast_gen1(ctx) < 0)
4302 return -1;
4303 if (test_ast_gen2(ctx) < 0)
4304 return -1;
4305 if (test_ast_gen3(ctx) < 0)
4306 return -1;
4307 if (test_ast_gen4(ctx) < 0)
4308 return -1;
4309 if (test_ast_gen5(ctx) < 0)
4310 return -1;
4311 return 0;
4314 /* Check if dropping output dimensions from an isl_pw_multi_aff
4315 * works properly.
4317 static int test_pw_multi_aff(isl_ctx *ctx)
4319 const char *str;
4320 isl_pw_multi_aff *pma1, *pma2;
4321 int equal;
4323 str = "{ [i,j] -> [i+j, 4i-j] }";
4324 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4325 str = "{ [i,j] -> [4i-j] }";
4326 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4328 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4330 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4332 isl_pw_multi_aff_free(pma1);
4333 isl_pw_multi_aff_free(pma2);
4334 if (equal < 0)
4335 return -1;
4336 if (!equal)
4337 isl_die(ctx, isl_error_unknown,
4338 "expressions not equal", return -1);
4340 return 0;
4343 /* Check that we can properly parse multi piecewise affine expressions
4344 * where the piecewise affine expressions have different domains.
4346 static int test_multi_pw_aff(isl_ctx *ctx)
4348 const char *str;
4349 isl_set *dom, *dom2;
4350 isl_multi_pw_aff *mpa1, *mpa2;
4351 isl_pw_aff *pa;
4352 int equal;
4353 int equal_domain;
4355 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
4356 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
4357 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
4358 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
4359 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
4360 str = "{ [i] -> [(i : i > 0), 2i] }";
4361 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
4363 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
4365 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
4366 dom = isl_pw_aff_domain(pa);
4367 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
4368 dom2 = isl_pw_aff_domain(pa);
4369 equal_domain = isl_set_is_equal(dom, dom2);
4371 isl_set_free(dom);
4372 isl_set_free(dom2);
4373 isl_multi_pw_aff_free(mpa1);
4374 isl_multi_pw_aff_free(mpa2);
4376 if (equal < 0)
4377 return -1;
4378 if (!equal)
4379 isl_die(ctx, isl_error_unknown,
4380 "expressions not equal", return -1);
4382 if (equal_domain < 0)
4383 return -1;
4384 if (equal_domain)
4385 isl_die(ctx, isl_error_unknown,
4386 "domains unexpectedly equal", return -1);
4388 return 0;
4391 /* This is a regression test for a bug where isl_basic_map_simplify
4392 * would end up in an infinite loop. In particular, we construct
4393 * an empty basic set that is not obviously empty.
4394 * isl_basic_set_is_empty marks the basic set as empty.
4395 * After projecting out i3, the variable can be dropped completely,
4396 * but isl_basic_map_simplify refrains from doing so if the basic set
4397 * is empty and would end up in an infinite loop if it didn't test
4398 * explicitly for empty basic maps in the outer loop.
4400 static int test_simplify(isl_ctx *ctx)
4402 const char *str;
4403 isl_basic_set *bset;
4404 int empty;
4406 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
4407 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
4408 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
4409 "i3 >= i2 }";
4410 bset = isl_basic_set_read_from_str(ctx, str);
4411 empty = isl_basic_set_is_empty(bset);
4412 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
4413 isl_basic_set_free(bset);
4414 if (!bset)
4415 return -1;
4416 if (!empty)
4417 isl_die(ctx, isl_error_unknown,
4418 "basic set should be empty", return -1);
4420 return 0;
4423 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
4424 * with gbr context would fail to disable the use of the shifted tableau
4425 * when transferring equalities for the input to the context, resulting
4426 * in invalid sample values.
4428 static int test_partial_lexmin(isl_ctx *ctx)
4430 const char *str;
4431 isl_basic_set *bset;
4432 isl_basic_map *bmap;
4433 isl_map *map;
4435 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
4436 bmap = isl_basic_map_read_from_str(ctx, str);
4437 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
4438 bset = isl_basic_set_read_from_str(ctx, str);
4439 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
4440 isl_map_free(map);
4442 if (!map)
4443 return -1;
4445 return 0;
4448 /* Check that the variable compression performed on the existentially
4449 * quantified variables inside isl_basic_set_compute_divs is not confused
4450 * by the implicit equalities among the parameters.
4452 static int test_compute_divs(isl_ctx *ctx)
4454 const char *str;
4455 isl_basic_set *bset;
4456 isl_set *set;
4458 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
4459 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
4460 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
4461 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
4462 bset = isl_basic_set_read_from_str(ctx, str);
4463 set = isl_basic_set_compute_divs(bset);
4464 isl_set_free(set);
4465 if (!set)
4466 return -1;
4468 return 0;
4471 struct {
4472 const char *name;
4473 int (*fn)(isl_ctx *ctx);
4474 } tests [] = {
4475 { "val", &test_val },
4476 { "compute divs", &test_compute_divs },
4477 { "partial lexmin", &test_partial_lexmin },
4478 { "simplify", &test_simplify },
4479 { "curry", &test_curry },
4480 { "piecewise multi affine expressions", &test_pw_multi_aff },
4481 { "multi piecewise affine expressions", &test_multi_pw_aff },
4482 { "conversion", &test_conversion },
4483 { "list", &test_list },
4484 { "align parameters", &test_align_parameters },
4485 { "preimage", &test_preimage },
4486 { "pullback", &test_pullback },
4487 { "AST", &test_ast },
4488 { "AST generation", &test_ast_gen },
4489 { "eliminate", &test_eliminate },
4490 { "residue class", &test_residue_class },
4491 { "div", &test_div },
4492 { "slice", &test_slice },
4493 { "fixed power", &test_fixed_power },
4494 { "sample", &test_sample },
4495 { "output", &test_output },
4496 { "vertices", &test_vertices },
4497 { "fixed", &test_fixed },
4498 { "equal", &test_equal },
4499 { "product", &test_product },
4500 { "dim_max", &test_dim_max },
4501 { "affine", &test_aff },
4502 { "injective", &test_injective },
4503 { "schedule", &test_schedule },
4504 { "union_pw", &test_union_pw },
4505 { "parse", &test_parse },
4506 { "single-valued", &test_sv },
4507 { "affine hull", &test_affine_hull },
4508 { "coalesce", &test_coalesce },
4509 { "factorize", &test_factorize },
4510 { "subset", &test_subset },
4511 { "subtract", &test_subtract },
4512 { "lexmin", &test_lexmin },
4513 { "gist", &test_gist },
4514 { "piecewise quasi-polynomials", &test_pwqp },
4517 int main()
4519 int i;
4520 struct isl_ctx *ctx;
4522 srcdir = getenv("srcdir");
4523 assert(srcdir);
4525 ctx = isl_ctx_alloc();
4526 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
4527 printf("%s\n", tests[i].name);
4528 if (tests[i].fn(ctx) < 0)
4529 goto error;
4531 test_lift(ctx);
4532 test_bound(ctx);
4533 test_union(ctx);
4534 test_split_periods(ctx);
4535 test_lex(ctx);
4536 test_bijective(ctx);
4537 test_dep(ctx);
4538 test_read(ctx);
4539 test_bounded(ctx);
4540 test_construction(ctx);
4541 test_dim(ctx);
4542 test_application(ctx);
4543 test_convex_hull(ctx);
4544 test_closure(ctx);
4545 isl_ctx_free(ctx);
4546 return 0;
4547 error:
4548 isl_ctx_free(ctx);
4549 return -1;