document isl_{set,map}_set_tuple_name
[isl.git] / isl_test.c
blobdd5ce5909f0189a939d96ea1b0e63f3693d4b1ac
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>
32 #include <isl/ilp.h>
33 #include <isl_ast_build_expr.h>
35 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
37 static char *srcdir;
39 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
40 char *filename;
41 int length;
42 char *pattern = "%s/test_inputs/%s.%s";
44 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
45 + strlen(suffix) + 1;
46 filename = isl_alloc_array(ctx, char, length);
48 if (!filename)
49 return NULL;
51 sprintf(filename, pattern, srcdir, name, suffix);
53 return filename;
56 void test_parse_map(isl_ctx *ctx, const char *str)
58 isl_map *map;
60 map = isl_map_read_from_str(ctx, str);
61 assert(map);
62 isl_map_free(map);
65 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
67 isl_map *map, *map2;
68 int equal;
70 map = isl_map_read_from_str(ctx, str);
71 map2 = isl_map_read_from_str(ctx, str2);
72 equal = isl_map_is_equal(map, map2);
73 isl_map_free(map);
74 isl_map_free(map2);
76 if (equal < 0)
77 return -1;
78 if (!equal)
79 isl_die(ctx, isl_error_unknown, "maps not equal",
80 return -1);
82 return 0;
85 void test_parse_pwqp(isl_ctx *ctx, const char *str)
87 isl_pw_qpolynomial *pwqp;
89 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
90 assert(pwqp);
91 isl_pw_qpolynomial_free(pwqp);
94 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
96 isl_pw_aff *pwaff;
98 pwaff = isl_pw_aff_read_from_str(ctx, str);
99 assert(pwaff);
100 isl_pw_aff_free(pwaff);
103 int test_parse(struct isl_ctx *ctx)
105 isl_map *map, *map2;
106 const char *str, *str2;
108 str = "{ [i] -> [-i] }";
109 map = isl_map_read_from_str(ctx, str);
110 assert(map);
111 isl_map_free(map);
113 str = "{ A[i] -> L[([i/3])] }";
114 map = isl_map_read_from_str(ctx, str);
115 assert(map);
116 isl_map_free(map);
118 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
119 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
120 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
122 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
123 str2 = "{ [x, y] : 2y >= 6 - x }";
124 if (test_parse_map_equal(ctx, str, str2) < 0)
125 return -1;
127 if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
128 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
129 return -1;
130 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
131 if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
132 str) < 0)
133 return -1;
135 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
136 map = isl_map_read_from_str(ctx, str);
137 str = "{ [new, old] -> [o0, o1] : "
138 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
139 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
140 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
141 map2 = isl_map_read_from_str(ctx, str);
142 assert(isl_map_is_equal(map, map2));
143 isl_map_free(map);
144 isl_map_free(map2);
146 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
147 map = isl_map_read_from_str(ctx, str);
148 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
149 map2 = isl_map_read_from_str(ctx, str);
150 assert(isl_map_is_equal(map, map2));
151 isl_map_free(map);
152 isl_map_free(map2);
154 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
155 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
156 if (test_parse_map_equal(ctx, str, str2) < 0)
157 return -1;
159 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
160 str2 = "{ [i,j] -> [min(i,j)] }";
161 if (test_parse_map_equal(ctx, str, str2) < 0)
162 return -1;
164 str = "{ [i,j] : i != j }";
165 str2 = "{ [i,j] : i < j or i > j }";
166 if (test_parse_map_equal(ctx, str, str2) < 0)
167 return -1;
169 str = "{ [i,j] : (i+1)*2 >= j }";
170 str2 = "{ [i, j] : j <= 2 + 2i }";
171 if (test_parse_map_equal(ctx, str, str2) < 0)
172 return -1;
174 str = "{ [i] -> [i > 0 ? 4 : 5] }";
175 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
176 if (test_parse_map_equal(ctx, str, str2) < 0)
177 return -1;
179 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
180 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
181 if (test_parse_map_equal(ctx, str, str2) < 0)
182 return -1;
184 str = "{ [x] : x >= 0 }";
185 str2 = "{ [x] : x-0 >= 0 }";
186 if (test_parse_map_equal(ctx, str, str2) < 0)
187 return -1;
189 str = "{ [i] : ((i > 10)) }";
190 str2 = "{ [i] : i >= 11 }";
191 if (test_parse_map_equal(ctx, str, str2) < 0)
192 return -1;
194 str = "{ [i] -> [0] }";
195 str2 = "{ [i] -> [0 * i] }";
196 if (test_parse_map_equal(ctx, str, str2) < 0)
197 return -1;
199 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
200 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
201 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
202 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
204 if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
205 "{ [a] -> [b] : true }") < 0)
206 return -1;
208 if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
209 "{ [i] : i <= 10 }") < 0)
210 return -1;
212 if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
213 "[n] -> { [i] : i <= n }") < 0)
214 return -1;
216 if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
217 return -1;
219 if (test_parse_map_equal(ctx, "{ [i] : 2*floor(i/2) = i }",
220 "{ [i] : exists a : i = 2 a }") < 0)
221 return -1;
223 if (test_parse_map_equal(ctx, "{ [a] -> [b] : a = 5 implies b = 5 }",
224 "{ [a] -> [b] : a != 5 or b = 5 }") < 0)
225 return -1;
227 if (test_parse_map_equal(ctx, "{ [a] -> [a - 1 : a > 0] }",
228 "{ [a] -> [a - 1] : a > 0 }") < 0)
229 return -1;
230 if (test_parse_map_equal(ctx,
231 "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
232 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }") < 0)
233 return -1;
234 if (test_parse_map_equal(ctx,
235 "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
236 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
237 return -1;
238 if (test_parse_map_equal(ctx,
239 "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
240 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
241 return -1;
242 if (test_parse_map_equal(ctx,
243 "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
244 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
245 return -1;
246 if (test_parse_map_equal(ctx,
247 "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
248 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
249 return -1;
251 return 0;
254 void test_read(struct isl_ctx *ctx)
256 char *filename;
257 FILE *input;
258 struct isl_basic_set *bset1, *bset2;
259 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
261 filename = get_filename(ctx, "set", "omega");
262 assert(filename);
263 input = fopen(filename, "r");
264 assert(input);
266 bset1 = isl_basic_set_read_from_file(ctx, input);
267 bset2 = isl_basic_set_read_from_str(ctx, str);
269 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
271 isl_basic_set_free(bset1);
272 isl_basic_set_free(bset2);
273 free(filename);
275 fclose(input);
278 void test_bounded(struct isl_ctx *ctx)
280 isl_set *set;
281 int bounded;
283 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
284 bounded = isl_set_is_bounded(set);
285 assert(bounded);
286 isl_set_free(set);
288 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
289 bounded = isl_set_is_bounded(set);
290 assert(!bounded);
291 isl_set_free(set);
293 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
294 bounded = isl_set_is_bounded(set);
295 assert(!bounded);
296 isl_set_free(set);
299 /* Construct the basic set { [i] : 5 <= i <= N } */
300 void test_construction(struct isl_ctx *ctx)
302 isl_int v;
303 isl_space *dim;
304 isl_local_space *ls;
305 struct isl_basic_set *bset;
306 struct isl_constraint *c;
308 isl_int_init(v);
310 dim = isl_space_set_alloc(ctx, 1, 1);
311 bset = isl_basic_set_universe(isl_space_copy(dim));
312 ls = isl_local_space_from_space(dim);
314 c = isl_inequality_alloc(isl_local_space_copy(ls));
315 isl_int_set_si(v, -1);
316 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
317 isl_int_set_si(v, 1);
318 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
319 bset = isl_basic_set_add_constraint(bset, c);
321 c = isl_inequality_alloc(isl_local_space_copy(ls));
322 isl_int_set_si(v, 1);
323 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
324 isl_int_set_si(v, -5);
325 isl_constraint_set_constant(c, v);
326 bset = isl_basic_set_add_constraint(bset, c);
328 isl_local_space_free(ls);
329 isl_basic_set_free(bset);
331 isl_int_clear(v);
334 void test_dim(struct isl_ctx *ctx)
336 const char *str;
337 isl_map *map1, *map2;
339 map1 = isl_map_read_from_str(ctx,
340 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
341 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
342 map2 = isl_map_read_from_str(ctx,
343 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
344 assert(isl_map_is_equal(map1, map2));
345 isl_map_free(map2);
347 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
348 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
349 assert(isl_map_is_equal(map1, map2));
351 isl_map_free(map1);
352 isl_map_free(map2);
354 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
355 map1 = isl_map_read_from_str(ctx, str);
356 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
357 map2 = isl_map_read_from_str(ctx, str);
358 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
359 assert(isl_map_is_equal(map1, map2));
361 isl_map_free(map1);
362 isl_map_free(map2);
365 struct {
366 __isl_give isl_val *(*op)(__isl_take isl_val *v);
367 const char *arg;
368 const char *res;
369 } val_un_tests[] = {
370 { &isl_val_neg, "0", "0" },
371 { &isl_val_abs, "0", "0" },
372 { &isl_val_2exp, "0", "1" },
373 { &isl_val_floor, "0", "0" },
374 { &isl_val_ceil, "0", "0" },
375 { &isl_val_neg, "1", "-1" },
376 { &isl_val_neg, "-1", "1" },
377 { &isl_val_neg, "1/2", "-1/2" },
378 { &isl_val_neg, "-1/2", "1/2" },
379 { &isl_val_neg, "infty", "-infty" },
380 { &isl_val_neg, "-infty", "infty" },
381 { &isl_val_neg, "NaN", "NaN" },
382 { &isl_val_abs, "1", "1" },
383 { &isl_val_abs, "-1", "1" },
384 { &isl_val_abs, "1/2", "1/2" },
385 { &isl_val_abs, "-1/2", "1/2" },
386 { &isl_val_abs, "infty", "infty" },
387 { &isl_val_abs, "-infty", "infty" },
388 { &isl_val_abs, "NaN", "NaN" },
389 { &isl_val_floor, "1", "1" },
390 { &isl_val_floor, "-1", "-1" },
391 { &isl_val_floor, "1/2", "0" },
392 { &isl_val_floor, "-1/2", "-1" },
393 { &isl_val_floor, "infty", "infty" },
394 { &isl_val_floor, "-infty", "-infty" },
395 { &isl_val_floor, "NaN", "NaN" },
396 { &isl_val_ceil, "1", "1" },
397 { &isl_val_ceil, "-1", "-1" },
398 { &isl_val_ceil, "1/2", "1" },
399 { &isl_val_ceil, "-1/2", "0" },
400 { &isl_val_ceil, "infty", "infty" },
401 { &isl_val_ceil, "-infty", "-infty" },
402 { &isl_val_ceil, "NaN", "NaN" },
403 { &isl_val_2exp, "-3", "1/8" },
404 { &isl_val_2exp, "-1", "1/2" },
405 { &isl_val_2exp, "1", "2" },
406 { &isl_val_2exp, "2", "4" },
407 { &isl_val_2exp, "3", "8" },
410 /* Perform some basic tests of unary operations on isl_val objects.
412 static int test_un_val(isl_ctx *ctx)
414 int i;
415 isl_val *v, *res;
416 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
417 int ok;
419 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
420 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
421 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
422 fn = val_un_tests[i].op;
423 v = fn(v);
424 if (isl_val_is_nan(res))
425 ok = isl_val_is_nan(v);
426 else
427 ok = isl_val_eq(v, res);
428 isl_val_free(v);
429 isl_val_free(res);
430 if (ok < 0)
431 return -1;
432 if (!ok)
433 isl_die(ctx, isl_error_unknown,
434 "unexpected result", return -1);
437 return 0;
440 struct {
441 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
442 __isl_take isl_val *v2);
443 } val_bin_op[] = {
444 ['+'] = { &isl_val_add },
445 ['-'] = { &isl_val_sub },
446 ['*'] = { &isl_val_mul },
447 ['/'] = { &isl_val_div },
448 ['g'] = { &isl_val_gcd },
449 ['m'] = { &isl_val_min },
450 ['M'] = { &isl_val_max },
453 struct {
454 const char *arg1;
455 unsigned char op;
456 const char *arg2;
457 const char *res;
458 } val_bin_tests[] = {
459 { "0", '+', "0", "0" },
460 { "1", '+', "0", "1" },
461 { "1", '+', "1", "2" },
462 { "1", '-', "1", "0" },
463 { "1", '*', "1", "1" },
464 { "1", '/', "1", "1" },
465 { "2", '*', "3", "6" },
466 { "2", '*', "1/2", "1" },
467 { "2", '*', "1/3", "2/3" },
468 { "2/3", '*', "3/5", "2/5" },
469 { "2/3", '*', "7/5", "14/15" },
470 { "2", '/', "1/2", "4" },
471 { "-2", '/', "-1/2", "4" },
472 { "-2", '/', "1/2", "-4" },
473 { "2", '/', "-1/2", "-4" },
474 { "2", '/', "2", "1" },
475 { "2", '/', "3", "2/3" },
476 { "2/3", '/', "5/3", "2/5" },
477 { "2/3", '/', "5/7", "14/15" },
478 { "0", '/', "0", "NaN" },
479 { "42", '/', "0", "NaN" },
480 { "-42", '/', "0", "NaN" },
481 { "infty", '/', "0", "NaN" },
482 { "-infty", '/', "0", "NaN" },
483 { "NaN", '/', "0", "NaN" },
484 { "0", '/', "NaN", "NaN" },
485 { "42", '/', "NaN", "NaN" },
486 { "-42", '/', "NaN", "NaN" },
487 { "infty", '/', "NaN", "NaN" },
488 { "-infty", '/', "NaN", "NaN" },
489 { "NaN", '/', "NaN", "NaN" },
490 { "0", '/', "infty", "0" },
491 { "42", '/', "infty", "0" },
492 { "-42", '/', "infty", "0" },
493 { "infty", '/', "infty", "NaN" },
494 { "-infty", '/', "infty", "NaN" },
495 { "NaN", '/', "infty", "NaN" },
496 { "0", '/', "-infty", "0" },
497 { "42", '/', "-infty", "0" },
498 { "-42", '/', "-infty", "0" },
499 { "infty", '/', "-infty", "NaN" },
500 { "-infty", '/', "-infty", "NaN" },
501 { "NaN", '/', "-infty", "NaN" },
502 { "1", '-', "1/3", "2/3" },
503 { "1/3", '+', "1/2", "5/6" },
504 { "1/2", '+', "1/2", "1" },
505 { "3/4", '-', "1/4", "1/2" },
506 { "1/2", '-', "1/3", "1/6" },
507 { "infty", '+', "42", "infty" },
508 { "infty", '+', "infty", "infty" },
509 { "42", '+', "infty", "infty" },
510 { "infty", '-', "infty", "NaN" },
511 { "infty", '*', "infty", "infty" },
512 { "infty", '*', "-infty", "-infty" },
513 { "-infty", '*', "infty", "-infty" },
514 { "-infty", '*', "-infty", "infty" },
515 { "0", '*', "infty", "NaN" },
516 { "1", '*', "infty", "infty" },
517 { "infty", '*', "0", "NaN" },
518 { "infty", '*', "42", "infty" },
519 { "42", '-', "infty", "-infty" },
520 { "infty", '+', "-infty", "NaN" },
521 { "4", 'g', "6", "2" },
522 { "5", 'g', "6", "1" },
523 { "42", 'm', "3", "3" },
524 { "42", 'M', "3", "42" },
525 { "3", 'm', "42", "3" },
526 { "3", 'M', "42", "42" },
527 { "42", 'm', "infty", "42" },
528 { "42", 'M', "infty", "infty" },
529 { "42", 'm', "-infty", "-infty" },
530 { "42", 'M', "-infty", "42" },
531 { "42", 'm', "NaN", "NaN" },
532 { "42", 'M', "NaN", "NaN" },
533 { "infty", 'm', "-infty", "-infty" },
534 { "infty", 'M', "-infty", "infty" },
537 /* Perform some basic tests of binary operations on isl_val objects.
539 static int test_bin_val(isl_ctx *ctx)
541 int i;
542 isl_val *v1, *v2, *res;
543 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
544 __isl_take isl_val *v2);
545 int ok;
547 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
548 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
549 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
550 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
551 fn = val_bin_op[val_bin_tests[i].op].fn;
552 v1 = fn(v1, v2);
553 if (isl_val_is_nan(res))
554 ok = isl_val_is_nan(v1);
555 else
556 ok = isl_val_eq(v1, res);
557 isl_val_free(v1);
558 isl_val_free(res);
559 if (ok < 0)
560 return -1;
561 if (!ok)
562 isl_die(ctx, isl_error_unknown,
563 "unexpected result", return -1);
566 return 0;
569 /* Perform some basic tests on isl_val objects.
571 static int test_val(isl_ctx *ctx)
573 if (test_un_val(ctx) < 0)
574 return -1;
575 if (test_bin_val(ctx) < 0)
576 return -1;
577 return 0;
580 static int test_div(isl_ctx *ctx)
582 unsigned n;
583 const char *str;
584 int empty;
585 isl_int v;
586 isl_space *dim;
587 isl_set *set;
588 isl_local_space *ls;
589 struct isl_basic_set *bset;
590 struct isl_constraint *c;
592 isl_int_init(v);
594 /* test 1 */
595 dim = isl_space_set_alloc(ctx, 0, 3);
596 bset = isl_basic_set_universe(isl_space_copy(dim));
597 ls = isl_local_space_from_space(dim);
599 c = isl_equality_alloc(isl_local_space_copy(ls));
600 isl_int_set_si(v, -1);
601 isl_constraint_set_constant(c, v);
602 isl_int_set_si(v, 1);
603 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
604 isl_int_set_si(v, 3);
605 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
606 bset = isl_basic_set_add_constraint(bset, c);
608 c = isl_equality_alloc(isl_local_space_copy(ls));
609 isl_int_set_si(v, 1);
610 isl_constraint_set_constant(c, v);
611 isl_int_set_si(v, -1);
612 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
613 isl_int_set_si(v, 3);
614 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
615 bset = isl_basic_set_add_constraint(bset, c);
617 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
619 assert(bset && bset->n_div == 1);
620 isl_local_space_free(ls);
621 isl_basic_set_free(bset);
623 /* test 2 */
624 dim = isl_space_set_alloc(ctx, 0, 3);
625 bset = isl_basic_set_universe(isl_space_copy(dim));
626 ls = isl_local_space_from_space(dim);
628 c = isl_equality_alloc(isl_local_space_copy(ls));
629 isl_int_set_si(v, 1);
630 isl_constraint_set_constant(c, v);
631 isl_int_set_si(v, -1);
632 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
633 isl_int_set_si(v, 3);
634 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
635 bset = isl_basic_set_add_constraint(bset, c);
637 c = isl_equality_alloc(isl_local_space_copy(ls));
638 isl_int_set_si(v, -1);
639 isl_constraint_set_constant(c, v);
640 isl_int_set_si(v, 1);
641 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
642 isl_int_set_si(v, 3);
643 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
644 bset = isl_basic_set_add_constraint(bset, c);
646 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
648 assert(bset && bset->n_div == 1);
649 isl_local_space_free(ls);
650 isl_basic_set_free(bset);
652 /* test 3 */
653 dim = isl_space_set_alloc(ctx, 0, 3);
654 bset = isl_basic_set_universe(isl_space_copy(dim));
655 ls = isl_local_space_from_space(dim);
657 c = isl_equality_alloc(isl_local_space_copy(ls));
658 isl_int_set_si(v, 1);
659 isl_constraint_set_constant(c, v);
660 isl_int_set_si(v, -1);
661 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
662 isl_int_set_si(v, 3);
663 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
664 bset = isl_basic_set_add_constraint(bset, c);
666 c = isl_equality_alloc(isl_local_space_copy(ls));
667 isl_int_set_si(v, -3);
668 isl_constraint_set_constant(c, v);
669 isl_int_set_si(v, 1);
670 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
671 isl_int_set_si(v, 4);
672 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
673 bset = isl_basic_set_add_constraint(bset, c);
675 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
677 assert(bset && bset->n_div == 1);
678 isl_local_space_free(ls);
679 isl_basic_set_free(bset);
681 /* test 4 */
682 dim = isl_space_set_alloc(ctx, 0, 3);
683 bset = isl_basic_set_universe(isl_space_copy(dim));
684 ls = isl_local_space_from_space(dim);
686 c = isl_equality_alloc(isl_local_space_copy(ls));
687 isl_int_set_si(v, 2);
688 isl_constraint_set_constant(c, v);
689 isl_int_set_si(v, -1);
690 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
691 isl_int_set_si(v, 3);
692 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
693 bset = isl_basic_set_add_constraint(bset, c);
695 c = isl_equality_alloc(isl_local_space_copy(ls));
696 isl_int_set_si(v, -1);
697 isl_constraint_set_constant(c, v);
698 isl_int_set_si(v, 1);
699 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
700 isl_int_set_si(v, 6);
701 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
702 bset = isl_basic_set_add_constraint(bset, c);
704 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
706 assert(isl_basic_set_is_empty(bset));
707 isl_local_space_free(ls);
708 isl_basic_set_free(bset);
710 /* test 5 */
711 dim = isl_space_set_alloc(ctx, 0, 3);
712 bset = isl_basic_set_universe(isl_space_copy(dim));
713 ls = isl_local_space_from_space(dim);
715 c = isl_equality_alloc(isl_local_space_copy(ls));
716 isl_int_set_si(v, -1);
717 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
718 isl_int_set_si(v, 3);
719 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
720 bset = isl_basic_set_add_constraint(bset, c);
722 c = isl_equality_alloc(isl_local_space_copy(ls));
723 isl_int_set_si(v, 1);
724 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
725 isl_int_set_si(v, -3);
726 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
727 bset = isl_basic_set_add_constraint(bset, c);
729 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
731 assert(bset && bset->n_div == 0);
732 isl_basic_set_free(bset);
733 isl_local_space_free(ls);
735 /* test 6 */
736 dim = isl_space_set_alloc(ctx, 0, 3);
737 bset = isl_basic_set_universe(isl_space_copy(dim));
738 ls = isl_local_space_from_space(dim);
740 c = isl_equality_alloc(isl_local_space_copy(ls));
741 isl_int_set_si(v, -1);
742 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
743 isl_int_set_si(v, 6);
744 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
745 bset = isl_basic_set_add_constraint(bset, c);
747 c = isl_equality_alloc(isl_local_space_copy(ls));
748 isl_int_set_si(v, 1);
749 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
750 isl_int_set_si(v, -3);
751 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
752 bset = isl_basic_set_add_constraint(bset, c);
754 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
756 assert(bset && bset->n_div == 1);
757 isl_basic_set_free(bset);
758 isl_local_space_free(ls);
760 /* test 7 */
761 /* This test is a bit tricky. We set up an equality
762 * a + 3b + 3c = 6 e0
763 * Normalization of divs creates _two_ divs
764 * a = 3 e0
765 * c - b - e0 = 2 e1
766 * Afterwards e0 is removed again because it has coefficient -1
767 * and we end up with the original equality and div again.
768 * Perhaps we can avoid the introduction of this temporary div.
770 dim = isl_space_set_alloc(ctx, 0, 4);
771 bset = isl_basic_set_universe(isl_space_copy(dim));
772 ls = isl_local_space_from_space(dim);
774 c = isl_equality_alloc(isl_local_space_copy(ls));
775 isl_int_set_si(v, -1);
776 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
777 isl_int_set_si(v, -3);
778 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
779 isl_int_set_si(v, -3);
780 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
781 isl_int_set_si(v, 6);
782 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
783 bset = isl_basic_set_add_constraint(bset, c);
785 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
787 /* Test disabled for now */
789 assert(bset && bset->n_div == 1);
791 isl_local_space_free(ls);
792 isl_basic_set_free(bset);
794 /* test 8 */
795 dim = isl_space_set_alloc(ctx, 0, 5);
796 bset = isl_basic_set_universe(isl_space_copy(dim));
797 ls = isl_local_space_from_space(dim);
799 c = isl_equality_alloc(isl_local_space_copy(ls));
800 isl_int_set_si(v, -1);
801 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
802 isl_int_set_si(v, -3);
803 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
804 isl_int_set_si(v, -3);
805 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
806 isl_int_set_si(v, 6);
807 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
808 bset = isl_basic_set_add_constraint(bset, c);
810 c = isl_equality_alloc(isl_local_space_copy(ls));
811 isl_int_set_si(v, -1);
812 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
813 isl_int_set_si(v, 1);
814 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
815 isl_int_set_si(v, 1);
816 isl_constraint_set_constant(c, v);
817 bset = isl_basic_set_add_constraint(bset, c);
819 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
821 /* Test disabled for now */
823 assert(bset && bset->n_div == 1);
825 isl_local_space_free(ls);
826 isl_basic_set_free(bset);
828 /* test 9 */
829 dim = isl_space_set_alloc(ctx, 0, 4);
830 bset = isl_basic_set_universe(isl_space_copy(dim));
831 ls = isl_local_space_from_space(dim);
833 c = isl_equality_alloc(isl_local_space_copy(ls));
834 isl_int_set_si(v, 1);
835 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
836 isl_int_set_si(v, -1);
837 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
838 isl_int_set_si(v, -2);
839 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
840 bset = isl_basic_set_add_constraint(bset, c);
842 c = isl_equality_alloc(isl_local_space_copy(ls));
843 isl_int_set_si(v, -1);
844 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
845 isl_int_set_si(v, 3);
846 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
847 isl_int_set_si(v, 2);
848 isl_constraint_set_constant(c, v);
849 bset = isl_basic_set_add_constraint(bset, c);
851 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
853 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
855 assert(!isl_basic_set_is_empty(bset));
857 isl_local_space_free(ls);
858 isl_basic_set_free(bset);
860 /* test 10 */
861 dim = isl_space_set_alloc(ctx, 0, 3);
862 bset = isl_basic_set_universe(isl_space_copy(dim));
863 ls = isl_local_space_from_space(dim);
865 c = isl_equality_alloc(isl_local_space_copy(ls));
866 isl_int_set_si(v, 1);
867 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
868 isl_int_set_si(v, -2);
869 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
870 bset = isl_basic_set_add_constraint(bset, c);
872 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
874 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
876 isl_local_space_free(ls);
877 isl_basic_set_free(bset);
879 isl_int_clear(v);
881 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
882 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
883 set = isl_set_read_from_str(ctx, str);
884 set = isl_set_compute_divs(set);
885 isl_set_free(set);
886 if (!set)
887 return -1;
889 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
890 bset = isl_basic_set_read_from_str(ctx, str);
891 n = isl_basic_set_dim(bset, isl_dim_div);
892 isl_basic_set_free(bset);
893 if (!bset)
894 return -1;
895 if (n != 0)
896 isl_die(ctx, isl_error_unknown,
897 "expecting no existentials", return -1);
899 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
900 set = isl_set_read_from_str(ctx, str);
901 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
902 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
903 empty = isl_set_is_empty(set);
904 isl_set_free(set);
905 if (empty < 0)
906 return -1;
907 if (!empty)
908 isl_die(ctx, isl_error_unknown,
909 "result not as accurate as expected", return -1);
911 return 0;
914 void test_application_case(struct isl_ctx *ctx, const char *name)
916 char *filename;
917 FILE *input;
918 struct isl_basic_set *bset1, *bset2;
919 struct isl_basic_map *bmap;
921 filename = get_filename(ctx, name, "omega");
922 assert(filename);
923 input = fopen(filename, "r");
924 assert(input);
926 bset1 = isl_basic_set_read_from_file(ctx, input);
927 bmap = isl_basic_map_read_from_file(ctx, input);
929 bset1 = isl_basic_set_apply(bset1, bmap);
931 bset2 = isl_basic_set_read_from_file(ctx, input);
933 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
935 isl_basic_set_free(bset1);
936 isl_basic_set_free(bset2);
937 free(filename);
939 fclose(input);
942 void test_application(struct isl_ctx *ctx)
944 test_application_case(ctx, "application");
945 test_application_case(ctx, "application2");
948 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
950 char *filename;
951 FILE *input;
952 struct isl_basic_set *bset1, *bset2;
954 filename = get_filename(ctx, name, "polylib");
955 assert(filename);
956 input = fopen(filename, "r");
957 assert(input);
959 bset1 = isl_basic_set_read_from_file(ctx, input);
960 bset2 = isl_basic_set_read_from_file(ctx, input);
962 bset1 = isl_basic_set_affine_hull(bset1);
964 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
966 isl_basic_set_free(bset1);
967 isl_basic_set_free(bset2);
968 free(filename);
970 fclose(input);
973 int test_affine_hull(struct isl_ctx *ctx)
975 const char *str;
976 isl_set *set;
977 isl_basic_set *bset, *bset2;
978 int n;
979 int subset;
981 test_affine_hull_case(ctx, "affine2");
982 test_affine_hull_case(ctx, "affine");
983 test_affine_hull_case(ctx, "affine3");
985 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
986 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
987 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
988 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
989 set = isl_set_read_from_str(ctx, str);
990 bset = isl_set_affine_hull(set);
991 n = isl_basic_set_dim(bset, isl_dim_div);
992 isl_basic_set_free(bset);
993 if (n != 0)
994 isl_die(ctx, isl_error_unknown, "not expecting any divs",
995 return -1);
997 /* Check that isl_map_affine_hull is not confused by
998 * the reordering of divs in isl_map_align_divs.
1000 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1001 "32e0 = b and 32e1 = c); "
1002 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1003 set = isl_set_read_from_str(ctx, str);
1004 bset = isl_set_affine_hull(set);
1005 isl_basic_set_free(bset);
1006 if (!bset)
1007 return -1;
1009 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1010 "32e2 = 31 + 31e0 }";
1011 set = isl_set_read_from_str(ctx, str);
1012 bset = isl_set_affine_hull(set);
1013 str = "{ [a] : exists e : a = 32 e }";
1014 bset2 = isl_basic_set_read_from_str(ctx, str);
1015 subset = isl_basic_set_is_subset(bset, bset2);
1016 isl_basic_set_free(bset);
1017 isl_basic_set_free(bset2);
1018 if (subset < 0)
1019 return -1;
1020 if (!subset)
1021 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1022 return -1);
1024 return 0;
1027 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1029 char *filename;
1030 FILE *input;
1031 struct isl_basic_set *bset1, *bset2;
1032 struct isl_set *set;
1034 filename = get_filename(ctx, name, "polylib");
1035 assert(filename);
1036 input = fopen(filename, "r");
1037 assert(input);
1039 bset1 = isl_basic_set_read_from_file(ctx, input);
1040 bset2 = isl_basic_set_read_from_file(ctx, input);
1042 set = isl_basic_set_union(bset1, bset2);
1043 bset1 = isl_set_convex_hull(set);
1045 bset2 = isl_basic_set_read_from_file(ctx, input);
1047 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1049 isl_basic_set_free(bset1);
1050 isl_basic_set_free(bset2);
1051 free(filename);
1053 fclose(input);
1056 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
1058 const char *str1, *str2;
1059 isl_set *set1, *set2;
1060 int orig_convex = ctx->opt->convex;
1061 ctx->opt->convex = convex;
1063 test_convex_hull_case(ctx, "convex0");
1064 test_convex_hull_case(ctx, "convex1");
1065 test_convex_hull_case(ctx, "convex2");
1066 test_convex_hull_case(ctx, "convex3");
1067 test_convex_hull_case(ctx, "convex4");
1068 test_convex_hull_case(ctx, "convex5");
1069 test_convex_hull_case(ctx, "convex6");
1070 test_convex_hull_case(ctx, "convex7");
1071 test_convex_hull_case(ctx, "convex8");
1072 test_convex_hull_case(ctx, "convex9");
1073 test_convex_hull_case(ctx, "convex10");
1074 test_convex_hull_case(ctx, "convex11");
1075 test_convex_hull_case(ctx, "convex12");
1076 test_convex_hull_case(ctx, "convex13");
1077 test_convex_hull_case(ctx, "convex14");
1078 test_convex_hull_case(ctx, "convex15");
1080 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1081 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1082 "(i0 = 0 and i1 = 0 and i2 = 0) }";
1083 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
1084 set1 = isl_set_read_from_str(ctx, str1);
1085 set2 = isl_set_read_from_str(ctx, str2);
1086 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1087 assert(isl_set_is_equal(set1, set2));
1088 isl_set_free(set1);
1089 isl_set_free(set2);
1091 ctx->opt->convex = orig_convex;
1094 void test_convex_hull(struct isl_ctx *ctx)
1096 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
1097 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
1100 void test_gist_case(struct isl_ctx *ctx, const char *name)
1102 char *filename;
1103 FILE *input;
1104 struct isl_basic_set *bset1, *bset2;
1106 filename = get_filename(ctx, name, "polylib");
1107 assert(filename);
1108 input = fopen(filename, "r");
1109 assert(input);
1111 bset1 = isl_basic_set_read_from_file(ctx, input);
1112 bset2 = isl_basic_set_read_from_file(ctx, input);
1114 bset1 = isl_basic_set_gist(bset1, bset2);
1116 bset2 = isl_basic_set_read_from_file(ctx, input);
1118 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1120 isl_basic_set_free(bset1);
1121 isl_basic_set_free(bset2);
1122 free(filename);
1124 fclose(input);
1127 static int test_gist(struct isl_ctx *ctx)
1129 const char *str;
1130 isl_basic_set *bset1, *bset2;
1131 isl_map *map1, *map2;
1133 test_gist_case(ctx, "gist1");
1135 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1136 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1137 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1138 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1139 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1140 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1141 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1142 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1143 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1144 bset1 = isl_basic_set_read_from_str(ctx, str);
1145 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1146 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1147 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1148 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1149 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1150 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1151 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1152 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1153 bset2 = isl_basic_set_read_from_str(ctx, str);
1154 bset1 = isl_basic_set_gist(bset1, bset2);
1155 assert(bset1 && bset1->n_div == 0);
1156 isl_basic_set_free(bset1);
1158 /* Check that the integer divisions of the second disjunct
1159 * do not spread to the first disjunct.
1161 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1162 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1163 "(exists (e0 = [(-1 + t1)/16], "
1164 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1165 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1166 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1167 "o0 <= 4294967295 and t1 <= -1)) }";
1168 map1 = isl_map_read_from_str(ctx, str);
1169 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1170 map2 = isl_map_read_from_str(ctx, str);
1171 map1 = isl_map_gist(map1, map2);
1172 if (!map1)
1173 return -1;
1174 if (map1->n != 1)
1175 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1176 isl_map_free(map1); return -1);
1177 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1178 isl_die(ctx, isl_error_unknown, "expecting single div",
1179 isl_map_free(map1); return -1);
1180 isl_map_free(map1);
1182 return 0;
1185 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1187 isl_set *set, *set2;
1188 int equal;
1189 int one;
1191 set = isl_set_read_from_str(ctx, str);
1192 set = isl_set_coalesce(set);
1193 set2 = isl_set_read_from_str(ctx, str);
1194 equal = isl_set_is_equal(set, set2);
1195 one = set && set->n == 1;
1196 isl_set_free(set);
1197 isl_set_free(set2);
1199 if (equal < 0)
1200 return -1;
1201 if (!equal)
1202 isl_die(ctx, isl_error_unknown,
1203 "coalesced set not equal to input", return -1);
1204 if (check_one && !one)
1205 isl_die(ctx, isl_error_unknown,
1206 "coalesced set should not be a union", return -1);
1208 return 0;
1211 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1213 int r = 0;
1214 int bounded;
1216 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1217 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1219 if (test_coalesce_set(ctx,
1220 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1221 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1222 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1223 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1224 "-10 <= y <= 0}", 1) < 0)
1225 goto error;
1226 if (test_coalesce_set(ctx,
1227 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
1228 goto error;
1229 if (test_coalesce_set(ctx,
1230 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
1231 goto error;
1233 if (0) {
1234 error:
1235 r = -1;
1238 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1240 return r;
1243 /* Inputs for coalescing tests.
1244 * "str" is a string representation of the input set.
1245 * "single_disjunct" is set if we expect the result to consist of
1246 * a single disjunct.
1248 struct {
1249 int single_disjunct;
1250 const char *str;
1251 } coalesce_tests[] = {
1252 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1253 "y >= x & x >= 2 & 5 >= y }" },
1254 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1255 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1256 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1257 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1258 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1259 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1260 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1261 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1262 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1263 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1264 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1265 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1266 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1267 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1268 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1269 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1270 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1271 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1272 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1273 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1274 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1275 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1276 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1277 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1278 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1279 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1280 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1281 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1282 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1283 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1284 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1285 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1286 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1287 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1288 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1289 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1290 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1291 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1292 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1293 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1294 "[o0, o1, o2, o3, o4, o5, o6]] : "
1295 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1296 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1297 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1298 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1299 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1300 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1301 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1302 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1303 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1304 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1305 "o6 >= i3 + i6 - o3 and M >= 0 and "
1306 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1307 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1308 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1309 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1310 "(o0 = 0 and M >= 2 and N >= 3) or "
1311 "(M = 0 and o0 = 0 and N >= 3) }" },
1312 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1313 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1314 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1315 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1316 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1317 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1318 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1319 "(y = 3 and x = 1) }" },
1320 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1321 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1322 "i1 <= M and i3 <= M and i4 <= M) or "
1323 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1324 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1325 "i4 <= -1 + M) }" },
1326 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1327 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1328 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1329 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1330 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1331 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1332 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1333 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1334 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1335 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1336 { 0, "{ [a, b] : exists e : 2e = a and "
1337 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1338 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1339 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1340 "j >= 1 and j' <= i + j - i' and i >= 1; "
1341 "[1, 1, 1, 1] }" },
1342 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1343 "[i,j] : exists a : j = 3a }" },
1344 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1345 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1346 "a >= 3) or "
1347 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1348 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1349 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1350 "c <= 6 + 8a and a >= 3; "
1351 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1352 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1353 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1354 "[x,0] : 3 <= x <= 4 }" },
1355 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1356 "[x,0] : 4 <= x <= 5 }" },
1357 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1358 "[x,0] : 3 <= x <= 5 }" },
1359 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1360 "[x,0] : 3 <= x <= 4 }" },
1361 { 1 , "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1362 "i1 <= 0; "
1363 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1366 /* Test the functionality of isl_set_coalesce.
1367 * That is, check that the output is always equal to the input
1368 * and in some cases that the result consists of a single disjunct.
1370 static int test_coalesce(struct isl_ctx *ctx)
1372 int i;
1374 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1375 const char *str = coalesce_tests[i].str;
1376 int check_one = coalesce_tests[i].single_disjunct;
1377 if (test_coalesce_set(ctx, str, check_one) < 0)
1378 return -1;
1381 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1382 return -1;
1384 return 0;
1387 void test_closure(struct isl_ctx *ctx)
1389 const char *str;
1390 isl_set *dom;
1391 isl_map *up, *right;
1392 isl_map *map, *map2;
1393 int exact;
1395 /* COCOA example 1 */
1396 map = isl_map_read_from_str(ctx,
1397 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1398 "1 <= i and i < n and 1 <= j and j < n or "
1399 "i2 = i + 1 and j2 = j - 1 and "
1400 "1 <= i and i < n and 2 <= j and j <= n }");
1401 map = isl_map_power(map, &exact);
1402 assert(exact);
1403 isl_map_free(map);
1405 /* COCOA example 1 */
1406 map = isl_map_read_from_str(ctx,
1407 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1408 "1 <= i and i < n and 1 <= j and j < n or "
1409 "i2 = i + 1 and j2 = j - 1 and "
1410 "1 <= i and i < n and 2 <= j and j <= n }");
1411 map = isl_map_transitive_closure(map, &exact);
1412 assert(exact);
1413 map2 = isl_map_read_from_str(ctx,
1414 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1415 "1 <= i and i < n and 1 <= j and j <= n and "
1416 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1417 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1418 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1419 assert(isl_map_is_equal(map, map2));
1420 isl_map_free(map2);
1421 isl_map_free(map);
1423 map = isl_map_read_from_str(ctx,
1424 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1425 " 0 <= y and y <= n }");
1426 map = isl_map_transitive_closure(map, &exact);
1427 map2 = isl_map_read_from_str(ctx,
1428 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1429 " 0 <= y and y <= n }");
1430 assert(isl_map_is_equal(map, map2));
1431 isl_map_free(map2);
1432 isl_map_free(map);
1434 /* COCOA example 2 */
1435 map = isl_map_read_from_str(ctx,
1436 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1437 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1438 "i2 = i + 2 and j2 = j - 2 and "
1439 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1440 map = isl_map_transitive_closure(map, &exact);
1441 assert(exact);
1442 map2 = isl_map_read_from_str(ctx,
1443 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1444 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1445 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1446 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1447 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1448 assert(isl_map_is_equal(map, map2));
1449 isl_map_free(map);
1450 isl_map_free(map2);
1452 /* COCOA Fig.2 left */
1453 map = isl_map_read_from_str(ctx,
1454 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1455 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1456 "j <= n or "
1457 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1458 "j <= 2 i - 3 and j <= n - 2 or "
1459 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1460 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1461 map = isl_map_transitive_closure(map, &exact);
1462 assert(exact);
1463 isl_map_free(map);
1465 /* COCOA Fig.2 right */
1466 map = isl_map_read_from_str(ctx,
1467 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1468 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1469 "j <= n or "
1470 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1471 "j <= 2 i - 4 and j <= n - 3 or "
1472 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1473 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1474 map = isl_map_power(map, &exact);
1475 assert(exact);
1476 isl_map_free(map);
1478 /* COCOA Fig.2 right */
1479 map = isl_map_read_from_str(ctx,
1480 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1481 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1482 "j <= n or "
1483 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1484 "j <= 2 i - 4 and j <= n - 3 or "
1485 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1486 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1487 map = isl_map_transitive_closure(map, &exact);
1488 assert(exact);
1489 map2 = isl_map_read_from_str(ctx,
1490 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1491 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1492 "j <= n and 3 + i + 2 j <= 3 n and "
1493 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1494 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1495 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1496 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1497 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1498 assert(isl_map_is_equal(map, map2));
1499 isl_map_free(map2);
1500 isl_map_free(map);
1502 /* COCOA Fig.1 right */
1503 dom = isl_set_read_from_str(ctx,
1504 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1505 "2 x - 3 y + 3 >= 0 }");
1506 right = isl_map_read_from_str(ctx,
1507 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1508 up = isl_map_read_from_str(ctx,
1509 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1510 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1511 right = isl_map_intersect_range(right, isl_set_copy(dom));
1512 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1513 up = isl_map_intersect_range(up, dom);
1514 map = isl_map_union(up, right);
1515 map = isl_map_transitive_closure(map, &exact);
1516 assert(exact);
1517 map2 = isl_map_read_from_str(ctx,
1518 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1519 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1520 assert(isl_map_is_equal(map, map2));
1521 isl_map_free(map2);
1522 isl_map_free(map);
1524 /* COCOA Theorem 1 counter example */
1525 map = isl_map_read_from_str(ctx,
1526 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1527 "i2 = 1 and j2 = j or "
1528 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1529 map = isl_map_transitive_closure(map, &exact);
1530 assert(exact);
1531 isl_map_free(map);
1533 map = isl_map_read_from_str(ctx,
1534 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1535 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1536 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1537 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1538 map = isl_map_transitive_closure(map, &exact);
1539 assert(exact);
1540 isl_map_free(map);
1542 /* Kelly et al 1996, fig 12 */
1543 map = isl_map_read_from_str(ctx,
1544 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1545 "1 <= i,j,j+1 <= n or "
1546 "j = n and j2 = 1 and i2 = i + 1 and "
1547 "1 <= i,i+1 <= n }");
1548 map = isl_map_transitive_closure(map, &exact);
1549 assert(exact);
1550 map2 = isl_map_read_from_str(ctx,
1551 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1552 "1 <= i <= n and i = i2 or "
1553 "1 <= i < i2 <= n and 1 <= j <= n and "
1554 "1 <= j2 <= n }");
1555 assert(isl_map_is_equal(map, map2));
1556 isl_map_free(map2);
1557 isl_map_free(map);
1559 /* Omega's closure4 */
1560 map = isl_map_read_from_str(ctx,
1561 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1562 "1 <= x,y <= 10 or "
1563 "x2 = x + 1 and y2 = y and "
1564 "1 <= x <= 20 && 5 <= y <= 15 }");
1565 map = isl_map_transitive_closure(map, &exact);
1566 assert(exact);
1567 isl_map_free(map);
1569 map = isl_map_read_from_str(ctx,
1570 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1571 map = isl_map_transitive_closure(map, &exact);
1572 assert(!exact);
1573 map2 = isl_map_read_from_str(ctx,
1574 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1575 assert(isl_map_is_equal(map, map2));
1576 isl_map_free(map);
1577 isl_map_free(map2);
1579 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1580 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1581 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1582 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1583 map = isl_map_read_from_str(ctx, str);
1584 map = isl_map_transitive_closure(map, &exact);
1585 assert(exact);
1586 map2 = isl_map_read_from_str(ctx, str);
1587 assert(isl_map_is_equal(map, map2));
1588 isl_map_free(map);
1589 isl_map_free(map2);
1591 str = "{[0] -> [1]; [2] -> [3]}";
1592 map = isl_map_read_from_str(ctx, str);
1593 map = isl_map_transitive_closure(map, &exact);
1594 assert(exact);
1595 map2 = isl_map_read_from_str(ctx, str);
1596 assert(isl_map_is_equal(map, map2));
1597 isl_map_free(map);
1598 isl_map_free(map2);
1600 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1601 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1602 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1603 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1604 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1605 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1606 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1607 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1608 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1609 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1610 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1611 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1612 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1613 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1614 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1615 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1616 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1617 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1618 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1619 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1620 map = isl_map_read_from_str(ctx, str);
1621 map = isl_map_transitive_closure(map, NULL);
1622 assert(map);
1623 isl_map_free(map);
1626 void test_lex(struct isl_ctx *ctx)
1628 isl_space *dim;
1629 isl_map *map;
1631 dim = isl_space_set_alloc(ctx, 0, 0);
1632 map = isl_map_lex_le(dim);
1633 assert(!isl_map_is_empty(map));
1634 isl_map_free(map);
1637 static int test_lexmin(struct isl_ctx *ctx)
1639 int equal;
1640 const char *str;
1641 isl_basic_map *bmap;
1642 isl_map *map, *map2;
1643 isl_set *set;
1644 isl_set *set2;
1645 isl_pw_multi_aff *pma;
1647 str = "[p0, p1] -> { [] -> [] : "
1648 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1649 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1650 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1651 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1652 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1653 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1654 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1655 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1656 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1657 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1658 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1659 map = isl_map_read_from_str(ctx, str);
1660 map = isl_map_lexmin(map);
1661 isl_map_free(map);
1663 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1664 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1665 set = isl_set_read_from_str(ctx, str);
1666 set = isl_set_lexmax(set);
1667 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1668 set2 = isl_set_read_from_str(ctx, str);
1669 set = isl_set_intersect(set, set2);
1670 assert(!isl_set_is_empty(set));
1671 isl_set_free(set);
1673 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1674 map = isl_map_read_from_str(ctx, str);
1675 map = isl_map_lexmin(map);
1676 str = "{ [x] -> [5] : 6 <= x <= 8; "
1677 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1678 map2 = isl_map_read_from_str(ctx, str);
1679 assert(isl_map_is_equal(map, map2));
1680 isl_map_free(map);
1681 isl_map_free(map2);
1683 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1684 map = isl_map_read_from_str(ctx, str);
1685 map2 = isl_map_copy(map);
1686 map = isl_map_lexmin(map);
1687 assert(isl_map_is_equal(map, map2));
1688 isl_map_free(map);
1689 isl_map_free(map2);
1691 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1692 map = isl_map_read_from_str(ctx, str);
1693 map = isl_map_lexmin(map);
1694 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1695 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1696 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1697 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1698 map2 = isl_map_read_from_str(ctx, str);
1699 assert(isl_map_is_equal(map, map2));
1700 isl_map_free(map);
1701 isl_map_free(map2);
1703 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1704 " 8i' <= i and 8i' >= -7 + i }";
1705 bmap = isl_basic_map_read_from_str(ctx, str);
1706 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1707 map2 = isl_map_from_pw_multi_aff(pma);
1708 map = isl_map_from_basic_map(bmap);
1709 assert(isl_map_is_equal(map, map2));
1710 isl_map_free(map);
1711 isl_map_free(map2);
1713 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1714 map = isl_map_read_from_str(ctx, str);
1715 map = isl_map_lexmin(map);
1716 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1717 map2 = isl_map_read_from_str(ctx, str);
1718 assert(isl_map_is_equal(map, map2));
1719 isl_map_free(map);
1720 isl_map_free(map2);
1722 /* Check that empty pieces are properly combined. */
1723 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1724 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1725 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1726 map = isl_map_read_from_str(ctx, str);
1727 map = isl_map_lexmin(map);
1728 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1729 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1730 "x >= 4 }";
1731 map2 = isl_map_read_from_str(ctx, str);
1732 assert(isl_map_is_equal(map, map2));
1733 isl_map_free(map);
1734 isl_map_free(map2);
1736 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1737 " 8i' <= i and 8i' >= -7 + i }";
1738 set = isl_set_read_from_str(ctx, str);
1739 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1740 set2 = isl_set_from_pw_multi_aff(pma);
1741 equal = isl_set_is_equal(set, set2);
1742 isl_set_free(set);
1743 isl_set_free(set2);
1744 if (equal < 0)
1745 return -1;
1746 if (!equal)
1747 isl_die(ctx, isl_error_unknown,
1748 "unexpected difference between set and "
1749 "piecewise affine expression", return -1);
1751 return 0;
1754 /* Check that isl_set_min_val and isl_set_max_val compute the correct
1755 * result on non-convex inputs.
1757 static int test_min(struct isl_ctx *ctx)
1759 isl_set *set;
1760 isl_aff *aff;
1761 isl_val *val;
1762 int min_ok, max_ok;
1764 set = isl_set_read_from_str(ctx, "{ [-1]; [1] }");
1765 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x] }");
1766 val = isl_set_min_val(set, aff);
1767 min_ok = isl_val_is_negone(val);
1768 isl_val_free(val);
1769 val = isl_set_max_val(set, aff);
1770 max_ok = isl_val_is_one(val);
1771 isl_val_free(val);
1772 isl_aff_free(aff);
1773 isl_set_free(set);
1775 if (min_ok < 0 || max_ok < 0)
1776 return -1;
1777 if (!min_ok)
1778 isl_die(ctx, isl_error_unknown,
1779 "unexpected minimum", return -1);
1780 if (!max_ok)
1781 isl_die(ctx, isl_error_unknown,
1782 "unexpected maximum", return -1);
1784 return 0;
1787 struct must_may {
1788 isl_map *must;
1789 isl_map *may;
1792 static int collect_must_may(__isl_take isl_map *dep, int must,
1793 void *dep_user, void *user)
1795 struct must_may *mm = (struct must_may *)user;
1797 if (must)
1798 mm->must = isl_map_union(mm->must, dep);
1799 else
1800 mm->may = isl_map_union(mm->may, dep);
1802 return 0;
1805 static int common_space(void *first, void *second)
1807 int depth = *(int *)first;
1808 return 2 * depth;
1811 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1813 isl_map *map2;
1814 int equal;
1816 if (!map)
1817 return -1;
1819 map2 = isl_map_read_from_str(map->ctx, str);
1820 equal = isl_map_is_equal(map, map2);
1821 isl_map_free(map2);
1823 return equal;
1826 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1828 int equal;
1830 equal = map_is_equal(map, str);
1831 if (equal < 0)
1832 return -1;
1833 if (!equal)
1834 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1835 "result not as expected", return -1);
1836 return 0;
1839 void test_dep(struct isl_ctx *ctx)
1841 const char *str;
1842 isl_space *dim;
1843 isl_map *map;
1844 isl_access_info *ai;
1845 isl_flow *flow;
1846 int depth;
1847 struct must_may mm;
1849 depth = 3;
1851 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1852 map = isl_map_read_from_str(ctx, str);
1853 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1855 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1856 map = isl_map_read_from_str(ctx, str);
1857 ai = isl_access_info_add_source(ai, map, 1, &depth);
1859 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1860 map = isl_map_read_from_str(ctx, str);
1861 ai = isl_access_info_add_source(ai, map, 1, &depth);
1863 flow = isl_access_info_compute_flow(ai);
1864 dim = isl_space_alloc(ctx, 0, 3, 3);
1865 mm.must = isl_map_empty(isl_space_copy(dim));
1866 mm.may = isl_map_empty(dim);
1868 isl_flow_foreach(flow, collect_must_may, &mm);
1870 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1871 " [1,10,0] -> [2,5,0] }";
1872 assert(map_is_equal(mm.must, str));
1873 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1874 assert(map_is_equal(mm.may, str));
1876 isl_map_free(mm.must);
1877 isl_map_free(mm.may);
1878 isl_flow_free(flow);
1881 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1882 map = isl_map_read_from_str(ctx, str);
1883 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1885 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1886 map = isl_map_read_from_str(ctx, str);
1887 ai = isl_access_info_add_source(ai, map, 1, &depth);
1889 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1890 map = isl_map_read_from_str(ctx, str);
1891 ai = isl_access_info_add_source(ai, map, 0, &depth);
1893 flow = isl_access_info_compute_flow(ai);
1894 dim = isl_space_alloc(ctx, 0, 3, 3);
1895 mm.must = isl_map_empty(isl_space_copy(dim));
1896 mm.may = isl_map_empty(dim);
1898 isl_flow_foreach(flow, collect_must_may, &mm);
1900 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1901 assert(map_is_equal(mm.must, str));
1902 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1903 assert(map_is_equal(mm.may, str));
1905 isl_map_free(mm.must);
1906 isl_map_free(mm.may);
1907 isl_flow_free(flow);
1910 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1911 map = isl_map_read_from_str(ctx, str);
1912 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1914 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1915 map = isl_map_read_from_str(ctx, str);
1916 ai = isl_access_info_add_source(ai, map, 0, &depth);
1918 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1919 map = isl_map_read_from_str(ctx, str);
1920 ai = isl_access_info_add_source(ai, map, 0, &depth);
1922 flow = isl_access_info_compute_flow(ai);
1923 dim = isl_space_alloc(ctx, 0, 3, 3);
1924 mm.must = isl_map_empty(isl_space_copy(dim));
1925 mm.may = isl_map_empty(dim);
1927 isl_flow_foreach(flow, collect_must_may, &mm);
1929 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1930 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1931 assert(map_is_equal(mm.may, str));
1932 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1933 assert(map_is_equal(mm.must, str));
1935 isl_map_free(mm.must);
1936 isl_map_free(mm.may);
1937 isl_flow_free(flow);
1940 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1941 map = isl_map_read_from_str(ctx, str);
1942 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1944 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1945 map = isl_map_read_from_str(ctx, str);
1946 ai = isl_access_info_add_source(ai, map, 0, &depth);
1948 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1949 map = isl_map_read_from_str(ctx, str);
1950 ai = isl_access_info_add_source(ai, map, 0, &depth);
1952 flow = isl_access_info_compute_flow(ai);
1953 dim = isl_space_alloc(ctx, 0, 3, 3);
1954 mm.must = isl_map_empty(isl_space_copy(dim));
1955 mm.may = isl_map_empty(dim);
1957 isl_flow_foreach(flow, collect_must_may, &mm);
1959 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1960 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1961 assert(map_is_equal(mm.may, str));
1962 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1963 assert(map_is_equal(mm.must, str));
1965 isl_map_free(mm.must);
1966 isl_map_free(mm.may);
1967 isl_flow_free(flow);
1970 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1971 map = isl_map_read_from_str(ctx, str);
1972 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1974 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1975 map = isl_map_read_from_str(ctx, str);
1976 ai = isl_access_info_add_source(ai, map, 0, &depth);
1978 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1979 map = isl_map_read_from_str(ctx, str);
1980 ai = isl_access_info_add_source(ai, map, 0, &depth);
1982 flow = isl_access_info_compute_flow(ai);
1983 dim = isl_space_alloc(ctx, 0, 3, 3);
1984 mm.must = isl_map_empty(isl_space_copy(dim));
1985 mm.may = isl_map_empty(dim);
1987 isl_flow_foreach(flow, collect_must_may, &mm);
1989 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1990 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1991 assert(map_is_equal(mm.may, str));
1992 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1993 assert(map_is_equal(mm.must, str));
1995 isl_map_free(mm.must);
1996 isl_map_free(mm.may);
1997 isl_flow_free(flow);
2000 depth = 5;
2002 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2003 map = isl_map_read_from_str(ctx, str);
2004 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2006 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2007 map = isl_map_read_from_str(ctx, str);
2008 ai = isl_access_info_add_source(ai, map, 1, &depth);
2010 flow = isl_access_info_compute_flow(ai);
2011 dim = isl_space_alloc(ctx, 0, 5, 5);
2012 mm.must = isl_map_empty(isl_space_copy(dim));
2013 mm.may = isl_map_empty(dim);
2015 isl_flow_foreach(flow, collect_must_may, &mm);
2017 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2018 assert(map_is_equal(mm.must, str));
2019 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2020 assert(map_is_equal(mm.may, str));
2022 isl_map_free(mm.must);
2023 isl_map_free(mm.may);
2024 isl_flow_free(flow);
2027 /* Check that the dependence analysis proceeds without errors.
2028 * Earlier versions of isl would break down during the analysis
2029 * due to the use of the wrong spaces.
2031 static int test_flow(isl_ctx *ctx)
2033 const char *str;
2034 isl_union_map *access, *schedule;
2035 isl_union_map *must_dep, *may_dep;
2036 int r;
2038 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2039 access = isl_union_map_read_from_str(ctx, str);
2040 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2041 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2042 "S2[] -> [1,0,0,0]; "
2043 "S3[] -> [-1,0,0,0] }";
2044 schedule = isl_union_map_read_from_str(ctx, str);
2045 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2046 isl_union_map_copy(access), schedule,
2047 &must_dep, &may_dep, NULL, NULL);
2048 isl_union_map_free(may_dep);
2049 isl_union_map_free(must_dep);
2051 return r;
2054 int test_sv(isl_ctx *ctx)
2056 const char *str;
2057 isl_map *map;
2058 isl_union_map *umap;
2059 int sv;
2061 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
2062 map = isl_map_read_from_str(ctx, str);
2063 sv = isl_map_is_single_valued(map);
2064 isl_map_free(map);
2065 if (sv < 0)
2066 return -1;
2067 if (!sv)
2068 isl_die(ctx, isl_error_internal,
2069 "map not detected as single valued", return -1);
2071 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
2072 map = isl_map_read_from_str(ctx, str);
2073 sv = isl_map_is_single_valued(map);
2074 isl_map_free(map);
2075 if (sv < 0)
2076 return -1;
2077 if (sv)
2078 isl_die(ctx, isl_error_internal,
2079 "map detected as single valued", return -1);
2081 str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
2082 umap = isl_union_map_read_from_str(ctx, str);
2083 sv = isl_union_map_is_single_valued(umap);
2084 isl_union_map_free(umap);
2085 if (sv < 0)
2086 return -1;
2087 if (!sv)
2088 isl_die(ctx, isl_error_internal,
2089 "map not detected as single valued", return -1);
2091 str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
2092 umap = isl_union_map_read_from_str(ctx, str);
2093 sv = isl_union_map_is_single_valued(umap);
2094 isl_union_map_free(umap);
2095 if (sv < 0)
2096 return -1;
2097 if (sv)
2098 isl_die(ctx, isl_error_internal,
2099 "map detected as single valued", return -1);
2101 return 0;
2104 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
2106 isl_map *map;
2108 map = isl_map_read_from_str(ctx, str);
2109 if (bijective)
2110 assert(isl_map_is_bijective(map));
2111 else
2112 assert(!isl_map_is_bijective(map));
2113 isl_map_free(map);
2116 void test_bijective(struct isl_ctx *ctx)
2118 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
2119 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
2120 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
2121 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
2122 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
2123 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
2124 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
2125 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2126 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2127 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2128 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2129 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2130 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2133 static int test_pwqp(struct isl_ctx *ctx)
2135 const char *str;
2136 isl_set *set;
2137 isl_pw_qpolynomial *pwqp1, *pwqp2;
2138 int equal;
2140 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2141 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2143 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2144 isl_dim_in, 1, 1);
2146 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2147 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2149 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2151 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2153 isl_pw_qpolynomial_free(pwqp1);
2155 str = "{ [i] -> i }";
2156 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2157 str = "{ [k] : exists a : k = 2a }";
2158 set = isl_set_read_from_str(ctx, str);
2159 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2160 str = "{ [i] -> i }";
2161 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2163 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2165 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2167 isl_pw_qpolynomial_free(pwqp1);
2169 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
2170 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2171 str = "{ [10] }";
2172 set = isl_set_read_from_str(ctx, str);
2173 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2174 str = "{ [i] -> 16 }";
2175 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2177 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2179 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2181 isl_pw_qpolynomial_free(pwqp1);
2183 str = "{ [i] -> ([(i)/2]) }";
2184 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2185 str = "{ [k] : exists a : k = 2a+1 }";
2186 set = isl_set_read_from_str(ctx, str);
2187 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2188 str = "{ [i] -> -1/2 + 1/2 * i }";
2189 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2191 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2193 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2195 isl_pw_qpolynomial_free(pwqp1);
2197 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2198 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2199 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2200 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2202 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2204 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2206 isl_pw_qpolynomial_free(pwqp1);
2208 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2209 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2210 str = "{ [x] -> x }";
2211 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2213 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2215 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2217 isl_pw_qpolynomial_free(pwqp1);
2219 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2220 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2221 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2222 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2223 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2224 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2225 isl_pw_qpolynomial_free(pwqp1);
2227 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2228 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2229 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2230 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2231 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2232 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2233 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2234 isl_pw_qpolynomial_free(pwqp1);
2235 isl_pw_qpolynomial_free(pwqp2);
2236 if (equal < 0)
2237 return -1;
2238 if (!equal)
2239 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2241 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2242 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2243 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2244 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2245 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2246 isl_val_one(ctx));
2247 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2248 isl_pw_qpolynomial_free(pwqp1);
2249 isl_pw_qpolynomial_free(pwqp2);
2250 if (equal < 0)
2251 return -1;
2252 if (!equal)
2253 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2255 return 0;
2258 void test_split_periods(isl_ctx *ctx)
2260 const char *str;
2261 isl_pw_qpolynomial *pwqp;
2263 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2264 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2265 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2266 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2268 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2269 assert(pwqp);
2271 isl_pw_qpolynomial_free(pwqp);
2274 void test_union(isl_ctx *ctx)
2276 const char *str;
2277 isl_union_set *uset1, *uset2;
2278 isl_union_map *umap1, *umap2;
2280 str = "{ [i] : 0 <= i <= 1 }";
2281 uset1 = isl_union_set_read_from_str(ctx, str);
2282 str = "{ [1] -> [0] }";
2283 umap1 = isl_union_map_read_from_str(ctx, str);
2285 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2286 assert(isl_union_map_is_equal(umap1, umap2));
2288 isl_union_map_free(umap1);
2289 isl_union_map_free(umap2);
2291 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2292 umap1 = isl_union_map_read_from_str(ctx, str);
2293 str = "{ A[i]; B[i] }";
2294 uset1 = isl_union_set_read_from_str(ctx, str);
2296 uset2 = isl_union_map_domain(umap1);
2298 assert(isl_union_set_is_equal(uset1, uset2));
2300 isl_union_set_free(uset1);
2301 isl_union_set_free(uset2);
2304 void test_bound(isl_ctx *ctx)
2306 const char *str;
2307 isl_pw_qpolynomial *pwqp;
2308 isl_pw_qpolynomial_fold *pwf;
2310 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2311 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2312 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2313 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2314 isl_pw_qpolynomial_fold_free(pwf);
2316 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2317 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2318 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2319 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2320 isl_pw_qpolynomial_fold_free(pwf);
2323 void test_lift(isl_ctx *ctx)
2325 const char *str;
2326 isl_basic_map *bmap;
2327 isl_basic_set *bset;
2329 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2330 bset = isl_basic_set_read_from_str(ctx, str);
2331 bset = isl_basic_set_lift(bset);
2332 bmap = isl_basic_map_from_range(bset);
2333 bset = isl_basic_map_domain(bmap);
2334 isl_basic_set_free(bset);
2337 struct {
2338 const char *set1;
2339 const char *set2;
2340 int subset;
2341 } subset_tests[] = {
2342 { "{ [112, 0] }",
2343 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2344 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2345 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2346 { "{ [65] }",
2347 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2348 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2349 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2350 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2351 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2352 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2353 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2354 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2355 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2356 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2357 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2358 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2359 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2360 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2361 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2362 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2363 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2364 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2365 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2366 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2367 "4e0 <= -57 + i0 + i1)) or "
2368 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2369 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2370 "4e0 >= -61 + i0 + i1)) or "
2371 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2374 static int test_subset(isl_ctx *ctx)
2376 int i;
2377 isl_set *set1, *set2;
2378 int subset;
2380 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2381 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2382 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2383 subset = isl_set_is_subset(set1, set2);
2384 isl_set_free(set1);
2385 isl_set_free(set2);
2386 if (subset < 0)
2387 return -1;
2388 if (subset != subset_tests[i].subset)
2389 isl_die(ctx, isl_error_unknown,
2390 "incorrect subset result", return -1);
2393 return 0;
2396 struct {
2397 const char *minuend;
2398 const char *subtrahend;
2399 const char *difference;
2400 } subtract_domain_tests[] = {
2401 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2402 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2403 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2406 static int test_subtract(isl_ctx *ctx)
2408 int i;
2409 isl_union_map *umap1, *umap2;
2410 isl_union_set *uset;
2411 int equal;
2413 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2414 umap1 = isl_union_map_read_from_str(ctx,
2415 subtract_domain_tests[i].minuend);
2416 uset = isl_union_set_read_from_str(ctx,
2417 subtract_domain_tests[i].subtrahend);
2418 umap2 = isl_union_map_read_from_str(ctx,
2419 subtract_domain_tests[i].difference);
2420 umap1 = isl_union_map_subtract_domain(umap1, uset);
2421 equal = isl_union_map_is_equal(umap1, umap2);
2422 isl_union_map_free(umap1);
2423 isl_union_map_free(umap2);
2424 if (equal < 0)
2425 return -1;
2426 if (!equal)
2427 isl_die(ctx, isl_error_unknown,
2428 "incorrect subtract domain result", return -1);
2431 return 0;
2434 int test_factorize(isl_ctx *ctx)
2436 const char *str;
2437 isl_basic_set *bset;
2438 isl_factorizer *f;
2440 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2441 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2442 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2443 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2444 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2445 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2446 "3i5 >= -2i0 - i2 + 3i4 }";
2447 bset = isl_basic_set_read_from_str(ctx, str);
2448 f = isl_basic_set_factorizer(bset);
2449 isl_basic_set_free(bset);
2450 isl_factorizer_free(f);
2451 if (!f)
2452 isl_die(ctx, isl_error_unknown,
2453 "failed to construct factorizer", return -1);
2455 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2456 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2457 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2458 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2459 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2460 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2461 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2462 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2463 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2464 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2465 bset = isl_basic_set_read_from_str(ctx, str);
2466 f = isl_basic_set_factorizer(bset);
2467 isl_basic_set_free(bset);
2468 isl_factorizer_free(f);
2469 if (!f)
2470 isl_die(ctx, isl_error_unknown,
2471 "failed to construct factorizer", return -1);
2473 return 0;
2476 static int check_injective(__isl_take isl_map *map, void *user)
2478 int *injective = user;
2480 *injective = isl_map_is_injective(map);
2481 isl_map_free(map);
2483 if (*injective < 0 || !*injective)
2484 return -1;
2486 return 0;
2489 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2490 const char *r, const char *s, int tilable, int parallel)
2492 int i;
2493 isl_union_set *D;
2494 isl_union_map *W, *R, *S;
2495 isl_union_map *empty;
2496 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2497 isl_union_map *validity, *proximity, *coincidence;
2498 isl_union_map *schedule;
2499 isl_union_map *test;
2500 isl_union_set *delta;
2501 isl_union_set *domain;
2502 isl_set *delta_set;
2503 isl_set *slice;
2504 isl_set *origin;
2505 isl_schedule_constraints *sc;
2506 isl_schedule *sched;
2507 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2509 D = isl_union_set_read_from_str(ctx, d);
2510 W = isl_union_map_read_from_str(ctx, w);
2511 R = isl_union_map_read_from_str(ctx, r);
2512 S = isl_union_map_read_from_str(ctx, s);
2514 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2515 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2517 empty = isl_union_map_empty(isl_union_map_get_space(S));
2518 isl_union_map_compute_flow(isl_union_map_copy(R),
2519 isl_union_map_copy(W), empty,
2520 isl_union_map_copy(S),
2521 &dep_raw, NULL, NULL, NULL);
2522 isl_union_map_compute_flow(isl_union_map_copy(W),
2523 isl_union_map_copy(W),
2524 isl_union_map_copy(R),
2525 isl_union_map_copy(S),
2526 &dep_waw, &dep_war, NULL, NULL);
2528 dep = isl_union_map_union(dep_waw, dep_war);
2529 dep = isl_union_map_union(dep, dep_raw);
2530 validity = isl_union_map_copy(dep);
2531 coincidence = isl_union_map_copy(dep);
2532 proximity = isl_union_map_copy(dep);
2534 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2535 sc = isl_schedule_constraints_set_validity(sc, validity);
2536 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
2537 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2538 sched = isl_schedule_constraints_compute_schedule(sc);
2539 schedule = isl_schedule_get_map(sched);
2540 isl_schedule_free(sched);
2541 isl_union_map_free(W);
2542 isl_union_map_free(R);
2543 isl_union_map_free(S);
2545 is_injection = 1;
2546 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2548 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2549 is_complete = isl_union_set_is_subset(D, domain);
2550 isl_union_set_free(D);
2551 isl_union_set_free(domain);
2553 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2554 test = isl_union_map_apply_range(test, dep);
2555 test = isl_union_map_apply_range(test, schedule);
2557 delta = isl_union_map_deltas(test);
2558 if (isl_union_set_n_set(delta) == 0) {
2559 is_tilable = 1;
2560 is_parallel = 1;
2561 is_nonneg = 1;
2562 isl_union_set_free(delta);
2563 } else {
2564 delta_set = isl_set_from_union_set(delta);
2566 slice = isl_set_universe(isl_set_get_space(delta_set));
2567 for (i = 0; i < tilable; ++i)
2568 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2569 is_tilable = isl_set_is_subset(delta_set, slice);
2570 isl_set_free(slice);
2572 slice = isl_set_universe(isl_set_get_space(delta_set));
2573 for (i = 0; i < parallel; ++i)
2574 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2575 is_parallel = isl_set_is_subset(delta_set, slice);
2576 isl_set_free(slice);
2578 origin = isl_set_universe(isl_set_get_space(delta_set));
2579 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2580 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2582 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2583 delta_set = isl_set_lexmin(delta_set);
2585 is_nonneg = isl_set_is_equal(delta_set, origin);
2587 isl_set_free(origin);
2588 isl_set_free(delta_set);
2591 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2592 is_injection < 0 || is_complete < 0)
2593 return -1;
2594 if (!is_complete)
2595 isl_die(ctx, isl_error_unknown,
2596 "generated schedule incomplete", return -1);
2597 if (!is_injection)
2598 isl_die(ctx, isl_error_unknown,
2599 "generated schedule not injective on each statement",
2600 return -1);
2601 if (!is_nonneg)
2602 isl_die(ctx, isl_error_unknown,
2603 "negative dependences in generated schedule",
2604 return -1);
2605 if (!is_tilable)
2606 isl_die(ctx, isl_error_unknown,
2607 "generated schedule not as tilable as expected",
2608 return -1);
2609 if (!is_parallel)
2610 isl_die(ctx, isl_error_unknown,
2611 "generated schedule not as parallel as expected",
2612 return -1);
2614 return 0;
2617 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2618 const char *domain, const char *validity, const char *proximity)
2620 isl_union_set *dom;
2621 isl_union_map *dep;
2622 isl_union_map *prox;
2623 isl_schedule_constraints *sc;
2624 isl_schedule *schedule;
2625 isl_union_map *sched;
2627 dom = isl_union_set_read_from_str(ctx, domain);
2628 dep = isl_union_map_read_from_str(ctx, validity);
2629 prox = isl_union_map_read_from_str(ctx, proximity);
2630 sc = isl_schedule_constraints_on_domain(dom);
2631 sc = isl_schedule_constraints_set_validity(sc, dep);
2632 sc = isl_schedule_constraints_set_proximity(sc, prox);
2633 schedule = isl_schedule_constraints_compute_schedule(sc);
2634 sched = isl_schedule_get_map(schedule);
2635 isl_schedule_free(schedule);
2637 return sched;
2640 /* Check that a schedule can be constructed on the given domain
2641 * with the given validity and proximity constraints.
2643 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2644 const char *validity, const char *proximity)
2646 isl_union_map *sched;
2648 sched = compute_schedule(ctx, domain, validity, proximity);
2649 if (!sched)
2650 return -1;
2652 isl_union_map_free(sched);
2653 return 0;
2656 int test_special_schedule(isl_ctx *ctx, const char *domain,
2657 const char *validity, const char *proximity, const char *expected_sched)
2659 isl_union_map *sched1, *sched2;
2660 int equal;
2662 sched1 = compute_schedule(ctx, domain, validity, proximity);
2663 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2665 equal = isl_union_map_is_equal(sched1, sched2);
2666 isl_union_map_free(sched1);
2667 isl_union_map_free(sched2);
2669 if (equal < 0)
2670 return -1;
2671 if (!equal)
2672 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2673 return -1);
2675 return 0;
2678 /* Check that the schedule map is properly padded, even after being
2679 * reconstructed from the band forest.
2681 static int test_padded_schedule(isl_ctx *ctx)
2683 const char *str;
2684 isl_union_set *D;
2685 isl_union_map *validity, *proximity;
2686 isl_schedule_constraints *sc;
2687 isl_schedule *sched;
2688 isl_union_map *map1, *map2;
2689 isl_band_list *list;
2690 int equal;
2692 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2693 D = isl_union_set_read_from_str(ctx, str);
2694 validity = isl_union_map_empty(isl_union_set_get_space(D));
2695 proximity = isl_union_map_copy(validity);
2696 sc = isl_schedule_constraints_on_domain(D);
2697 sc = isl_schedule_constraints_set_validity(sc, validity);
2698 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2699 sched = isl_schedule_constraints_compute_schedule(sc);
2700 map1 = isl_schedule_get_map(sched);
2701 list = isl_schedule_get_band_forest(sched);
2702 isl_band_list_free(list);
2703 map2 = isl_schedule_get_map(sched);
2704 isl_schedule_free(sched);
2705 equal = isl_union_map_is_equal(map1, map2);
2706 isl_union_map_free(map1);
2707 isl_union_map_free(map2);
2709 if (equal < 0)
2710 return -1;
2711 if (!equal)
2712 isl_die(ctx, isl_error_unknown,
2713 "reconstructed schedule map not the same as original",
2714 return -1);
2716 return 0;
2719 /* Input for testing of schedule construction based on
2720 * conditional constraints.
2722 * domain is the iteration domain
2723 * flow are the flow dependences, which determine the validity and
2724 * proximity constraints
2725 * condition are the conditions on the conditional validity constraints
2726 * conditional_validity are the conditional validity constraints
2727 * outer_band_n is the expected number of members in the outer band
2729 struct {
2730 const char *domain;
2731 const char *flow;
2732 const char *condition;
2733 const char *conditional_validity;
2734 int outer_band_n;
2735 } live_range_tests[] = {
2736 /* Contrived example that illustrates that we need to keep
2737 * track of tagged condition dependences and
2738 * tagged conditional validity dependences
2739 * in isl_sched_edge separately.
2740 * In particular, the conditional validity constraints on A
2741 * cannot be satisfied,
2742 * but they can be ignored because there are no corresponding
2743 * condition constraints. However, we do have an additional
2744 * conditional validity constraint that maps to the same
2745 * dependence relation
2746 * as the condition constraint on B. If we did not make a distinction
2747 * between tagged condition and tagged conditional validity
2748 * dependences, then we
2749 * could end up treating this shared dependence as an condition
2750 * constraint on A, forcing a localization of the conditions,
2751 * which is impossible.
2753 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
2754 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
2755 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
2756 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2757 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2758 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
2761 /* TACO 2013 Fig. 7 */
2762 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2763 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2764 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2765 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
2766 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
2767 "0 <= i < n and 0 <= j < n - 1 }",
2768 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
2769 "0 <= i < n and 0 <= j < j' < n;"
2770 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
2771 "0 <= i < i' < n and 0 <= j,j' < n;"
2772 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
2773 "0 <= i,j,j' < n and j < j' }",
2776 /* TACO 2013 Fig. 7, without tags */
2777 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2778 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2779 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2780 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2781 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2782 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
2783 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
2784 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
2787 /* TACO 2013 Fig. 12 */
2788 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
2789 "S3[i,3] : 0 <= i <= 1 }",
2790 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
2791 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
2792 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
2793 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
2794 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2795 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
2796 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2797 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
2798 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
2799 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
2800 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
2805 /* Test schedule construction based on conditional constraints.
2806 * In particular, check the number of members in the outer band
2807 * as an indication of whether tiling is possible or not.
2809 static int test_conditional_schedule_constraints(isl_ctx *ctx)
2811 int i;
2812 isl_union_set *domain;
2813 isl_union_map *condition;
2814 isl_union_map *flow;
2815 isl_union_map *validity;
2816 isl_schedule_constraints *sc;
2817 isl_schedule *schedule;
2818 isl_band_list *list;
2819 isl_band *band;
2820 int n_member;
2822 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
2823 domain = isl_union_set_read_from_str(ctx,
2824 live_range_tests[i].domain);
2825 flow = isl_union_map_read_from_str(ctx,
2826 live_range_tests[i].flow);
2827 condition = isl_union_map_read_from_str(ctx,
2828 live_range_tests[i].condition);
2829 validity = isl_union_map_read_from_str(ctx,
2830 live_range_tests[i].conditional_validity);
2831 sc = isl_schedule_constraints_on_domain(domain);
2832 sc = isl_schedule_constraints_set_validity(sc,
2833 isl_union_map_copy(flow));
2834 sc = isl_schedule_constraints_set_proximity(sc, flow);
2835 sc = isl_schedule_constraints_set_conditional_validity(sc,
2836 condition, validity);
2837 schedule = isl_schedule_constraints_compute_schedule(sc);
2838 list = isl_schedule_get_band_forest(schedule);
2839 band = isl_band_list_get_band(list, 0);
2840 n_member = isl_band_n_member(band);
2841 isl_band_free(band);
2842 isl_band_list_free(list);
2843 isl_schedule_free(schedule);
2845 if (!schedule)
2846 return -1;
2847 if (n_member != live_range_tests[i].outer_band_n)
2848 isl_die(ctx, isl_error_unknown,
2849 "unexpected number of members in outer band",
2850 return -1);
2852 return 0;
2855 int test_schedule(isl_ctx *ctx)
2857 const char *D, *W, *R, *V, *P, *S;
2859 /* Handle resulting schedule with zero bands. */
2860 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2861 return -1;
2863 /* Jacobi */
2864 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2865 W = "{ S1[t,i] -> a[t,i] }";
2866 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2867 "S1[t,i] -> a[t-1,i+1] }";
2868 S = "{ S1[t,i] -> [t,i] }";
2869 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2870 return -1;
2872 /* Fig. 5 of CC2008 */
2873 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2874 "j <= -1 + N }";
2875 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2876 "j >= 2 and j <= -1 + N }";
2877 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2878 "j >= 2 and j <= -1 + N; "
2879 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2880 "j >= 2 and j <= -1 + N }";
2881 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2882 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2883 return -1;
2885 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2886 W = "{ S1[i] -> a[i] }";
2887 R = "{ S2[i] -> a[i+1] }";
2888 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2889 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2890 return -1;
2892 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2893 W = "{ S1[i] -> a[i] }";
2894 R = "{ S2[i] -> a[9-i] }";
2895 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2896 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2897 return -1;
2899 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2900 W = "{ S1[i] -> a[i] }";
2901 R = "[N] -> { S2[i] -> a[N-1-i] }";
2902 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2903 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2904 return -1;
2906 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2907 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2908 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2909 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2910 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2911 return -1;
2913 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2914 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2915 R = "{ S2[i,j] -> a[i-1,j] }";
2916 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2917 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2918 return -1;
2920 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2921 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2922 R = "{ S2[i,j] -> a[i,j-1] }";
2923 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2924 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2925 return -1;
2927 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2928 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2929 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2930 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2931 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2932 "S_0[] -> [0, 0, 0] }";
2933 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2934 return -1;
2935 ctx->opt->schedule_parametric = 0;
2936 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2937 return -1;
2938 ctx->opt->schedule_parametric = 1;
2940 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2941 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2942 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2943 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2944 "S4[i] -> a[i,N] }";
2945 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2946 "S4[i] -> [4,i,0] }";
2947 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2948 return -1;
2950 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2951 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2952 "j <= N }";
2953 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2954 "j <= N; "
2955 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2956 "j <= N }";
2957 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2958 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2959 return -1;
2961 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2962 " S_2[t] : t >= 0 and t <= -1 + N; "
2963 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2964 "i <= -1 + N }";
2965 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2966 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2967 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2968 "i >= 0 and i <= -1 + N }";
2969 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2970 "i >= 0 and i <= -1 + N; "
2971 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2972 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2973 " S_0[t] -> [0, t, 0] }";
2975 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2976 return -1;
2977 ctx->opt->schedule_parametric = 0;
2978 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2979 return -1;
2980 ctx->opt->schedule_parametric = 1;
2982 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2983 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2984 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2985 return -1;
2987 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2988 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2989 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2990 "j >= 0 and j <= -1 + N; "
2991 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2992 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2993 "j >= 0 and j <= -1 + N; "
2994 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2995 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2996 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2997 return -1;
2999 D = "{ S_0[i] : i >= 0 }";
3000 W = "{ S_0[i] -> a[i] : i >= 0 }";
3001 R = "{ S_0[i] -> a[0] : i >= 0 }";
3002 S = "{ S_0[i] -> [0, i, 0] }";
3003 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3004 return -1;
3006 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3007 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3008 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3009 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3010 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3011 return -1;
3013 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3014 "k <= -1 + n and k >= 0 }";
3015 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3016 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3017 "k <= -1 + n and k >= 0; "
3018 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3019 "k <= -1 + n and k >= 0; "
3020 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3021 "k <= -1 + n and k >= 0 }";
3022 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3023 ctx->opt->schedule_outer_coincidence = 1;
3024 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3025 return -1;
3026 ctx->opt->schedule_outer_coincidence = 0;
3028 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3029 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3030 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3031 "Stmt_for_body24[i0, i1, 1, 0]:"
3032 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3033 "Stmt_for_body7[i0, i1, i2]:"
3034 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3035 "i2 <= 7 }";
3037 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3038 "Stmt_for_body24[1, i1, i2, i3]:"
3039 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3040 "i2 >= 1;"
3041 "Stmt_for_body24[0, i1, i2, i3] -> "
3042 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3043 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3044 "i3 >= 0;"
3045 "Stmt_for_body24[0, i1, i2, i3] ->"
3046 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3047 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3048 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3049 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3050 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3051 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3052 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3053 "i1 <= 6 and i1 >= 0;"
3054 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3055 "Stmt_for_body7[i0, i1, i2] -> "
3056 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3057 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3058 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3059 "Stmt_for_body7[i0, i1, i2] -> "
3060 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3061 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3062 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3063 P = V;
3064 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3065 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3066 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3068 if (test_special_schedule(ctx, D, V, P, S) < 0)
3069 return -1;
3071 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3072 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3073 "j >= 1 and j <= 7;"
3074 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3075 "j >= 1 and j <= 8 }";
3076 P = "{ }";
3077 S = "{ S_0[i, j] -> [i + j, j] }";
3078 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3079 if (test_special_schedule(ctx, D, V, P, S) < 0)
3080 return -1;
3081 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3083 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3084 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3085 "j >= 0 and j <= -1 + i }";
3086 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3087 "i <= -1 + N and j >= 0;"
3088 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3089 "i <= -2 + N }";
3090 P = "{ }";
3091 S = "{ S_0[i, j] -> [i, j] }";
3092 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3093 if (test_special_schedule(ctx, D, V, P, S) < 0)
3094 return -1;
3095 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3097 /* Test both algorithms on a case with only proximity dependences. */
3098 D = "{ S[i,j] : 0 <= i <= 10 }";
3099 V = "{ }";
3100 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3101 S = "{ S[i, j] -> [j, i] }";
3102 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3103 if (test_special_schedule(ctx, D, V, P, S) < 0)
3104 return -1;
3105 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3106 if (test_special_schedule(ctx, D, V, P, S) < 0)
3107 return -1;
3109 D = "{ A[a]; B[] }";
3110 V = "{}";
3111 P = "{ A[a] -> B[] }";
3112 if (test_has_schedule(ctx, D, V, P) < 0)
3113 return -1;
3115 if (test_padded_schedule(ctx) < 0)
3116 return -1;
3118 /* Check that check for progress is not confused by rational
3119 * solution.
3121 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3122 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3123 "i0 <= -2 + N; "
3124 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3125 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3126 P = "{}";
3127 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3128 if (test_has_schedule(ctx, D, V, P) < 0)
3129 return -1;
3130 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3132 if (test_conditional_schedule_constraints(ctx) < 0)
3133 return -1;
3135 return 0;
3138 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3140 isl_union_map *umap;
3141 int test;
3143 umap = isl_union_map_read_from_str(ctx, str);
3144 test = isl_union_map_plain_is_injective(umap);
3145 isl_union_map_free(umap);
3146 if (test < 0)
3147 return -1;
3148 if (test == injective)
3149 return 0;
3150 if (injective)
3151 isl_die(ctx, isl_error_unknown,
3152 "map not detected as injective", return -1);
3153 else
3154 isl_die(ctx, isl_error_unknown,
3155 "map detected as injective", return -1);
3158 int test_injective(isl_ctx *ctx)
3160 const char *str;
3162 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3163 return -1;
3164 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3165 return -1;
3166 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3167 return -1;
3168 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3169 return -1;
3170 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3171 return -1;
3172 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3173 return -1;
3174 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3175 return -1;
3176 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3177 return -1;
3179 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3180 if (test_plain_injective(ctx, str, 1))
3181 return -1;
3182 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3183 if (test_plain_injective(ctx, str, 0))
3184 return -1;
3186 return 0;
3189 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3191 isl_aff *aff2;
3192 int equal;
3194 if (!aff)
3195 return -1;
3197 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3198 equal = isl_aff_plain_is_equal(aff, aff2);
3199 isl_aff_free(aff2);
3201 return equal;
3204 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3206 int equal;
3208 equal = aff_plain_is_equal(aff, str);
3209 if (equal < 0)
3210 return -1;
3211 if (!equal)
3212 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3213 "result not as expected", return -1);
3214 return 0;
3217 struct {
3218 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3219 __isl_take isl_aff *aff2);
3220 } aff_bin_op[] = {
3221 ['+'] = { &isl_aff_add },
3222 ['-'] = { &isl_aff_sub },
3223 ['*'] = { &isl_aff_mul },
3226 struct {
3227 const char *arg1;
3228 unsigned char op;
3229 const char *arg2;
3230 const char *res;
3231 } aff_bin_tests[] = {
3232 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
3233 "{ [i] -> [2i] }" },
3234 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
3235 "{ [i] -> [0] }" },
3236 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
3237 "{ [i] -> [2i] }" },
3238 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
3239 "{ [i] -> [2i] }" },
3240 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
3241 "{ [i] -> [NaN] }" },
3242 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
3243 "{ [i] -> [NaN] }" },
3244 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
3245 "{ [i] -> [NaN] }" },
3246 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
3247 "{ [i] -> [NaN] }" },
3248 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
3249 "{ [i] -> [NaN] }" },
3250 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
3251 "{ [i] -> [NaN] }" },
3252 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
3253 "{ [i] -> [NaN] }" },
3254 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
3255 "{ [i] -> [NaN] }" },
3258 /* Perform some basic tests of binary operations on isl_aff objects.
3260 static int test_bin_aff(isl_ctx *ctx)
3262 int i;
3263 isl_aff *aff1, *aff2, *res;
3264 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3265 __isl_take isl_aff *aff2);
3266 int ok;
3268 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
3269 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
3270 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
3271 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
3272 fn = aff_bin_op[aff_bin_tests[i].op].fn;
3273 aff1 = fn(aff1, aff2);
3274 if (isl_aff_is_nan(res))
3275 ok = isl_aff_is_nan(aff1);
3276 else
3277 ok = isl_aff_plain_is_equal(aff1, res);
3278 isl_aff_free(aff1);
3279 isl_aff_free(res);
3280 if (ok < 0)
3281 return -1;
3282 if (!ok)
3283 isl_die(ctx, isl_error_unknown,
3284 "unexpected result", return -1);
3287 return 0;
3290 int test_aff(isl_ctx *ctx)
3292 const char *str;
3293 isl_set *set;
3294 isl_space *space;
3295 isl_local_space *ls;
3296 isl_aff *aff;
3297 int zero, equal;
3299 if (test_bin_aff(ctx) < 0)
3300 return -1;
3302 space = isl_space_set_alloc(ctx, 0, 1);
3303 ls = isl_local_space_from_space(space);
3304 aff = isl_aff_zero_on_domain(ls);
3306 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3307 aff = isl_aff_scale_down_ui(aff, 3);
3308 aff = isl_aff_floor(aff);
3309 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3310 aff = isl_aff_scale_down_ui(aff, 2);
3311 aff = isl_aff_floor(aff);
3312 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3314 str = "{ [10] }";
3315 set = isl_set_read_from_str(ctx, str);
3316 aff = isl_aff_gist(aff, set);
3318 aff = isl_aff_add_constant_si(aff, -16);
3319 zero = isl_aff_plain_is_zero(aff);
3320 isl_aff_free(aff);
3322 if (zero < 0)
3323 return -1;
3324 if (!zero)
3325 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3327 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3328 aff = isl_aff_scale_down_ui(aff, 64);
3329 aff = isl_aff_floor(aff);
3330 equal = aff_check_plain_equal(aff, "{ [-1] }");
3331 isl_aff_free(aff);
3332 if (equal < 0)
3333 return -1;
3335 return 0;
3338 int test_dim_max(isl_ctx *ctx)
3340 int equal;
3341 const char *str;
3342 isl_set *set1, *set2;
3343 isl_set *set;
3344 isl_map *map;
3345 isl_pw_aff *pwaff;
3347 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3348 set = isl_set_read_from_str(ctx, str);
3349 pwaff = isl_set_dim_max(set, 0);
3350 set1 = isl_set_from_pw_aff(pwaff);
3351 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3352 set2 = isl_set_read_from_str(ctx, str);
3353 equal = isl_set_is_equal(set1, set2);
3354 isl_set_free(set1);
3355 isl_set_free(set2);
3356 if (equal < 0)
3357 return -1;
3358 if (!equal)
3359 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3361 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3362 set = isl_set_read_from_str(ctx, str);
3363 pwaff = isl_set_dim_max(set, 0);
3364 set1 = isl_set_from_pw_aff(pwaff);
3365 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3366 set2 = isl_set_read_from_str(ctx, str);
3367 equal = isl_set_is_equal(set1, set2);
3368 isl_set_free(set1);
3369 isl_set_free(set2);
3370 if (equal < 0)
3371 return -1;
3372 if (!equal)
3373 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3375 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3376 set = isl_set_read_from_str(ctx, str);
3377 pwaff = isl_set_dim_max(set, 0);
3378 set1 = isl_set_from_pw_aff(pwaff);
3379 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3380 set2 = isl_set_read_from_str(ctx, str);
3381 equal = isl_set_is_equal(set1, set2);
3382 isl_set_free(set1);
3383 isl_set_free(set2);
3384 if (equal < 0)
3385 return -1;
3386 if (!equal)
3387 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3389 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3390 "0 <= i < N and 0 <= j < M }";
3391 map = isl_map_read_from_str(ctx, str);
3392 set = isl_map_range(map);
3394 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3395 set1 = isl_set_from_pw_aff(pwaff);
3396 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3397 set2 = isl_set_read_from_str(ctx, str);
3398 equal = isl_set_is_equal(set1, set2);
3399 isl_set_free(set1);
3400 isl_set_free(set2);
3402 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3403 set1 = isl_set_from_pw_aff(pwaff);
3404 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3405 set2 = isl_set_read_from_str(ctx, str);
3406 if (equal >= 0 && equal)
3407 equal = isl_set_is_equal(set1, set2);
3408 isl_set_free(set1);
3409 isl_set_free(set2);
3411 isl_set_free(set);
3413 if (equal < 0)
3414 return -1;
3415 if (!equal)
3416 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3418 /* Check that solutions are properly merged. */
3419 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3420 "c <= -1 + n - 4a - 2b and c >= -2b and "
3421 "4a >= -4 + n and c >= 0 }";
3422 set = isl_set_read_from_str(ctx, str);
3423 pwaff = isl_set_dim_min(set, 2);
3424 set1 = isl_set_from_pw_aff(pwaff);
3425 str = "[n] -> { [(0)] : n >= 1 }";
3426 set2 = isl_set_read_from_str(ctx, str);
3427 equal = isl_set_is_equal(set1, set2);
3428 isl_set_free(set1);
3429 isl_set_free(set2);
3431 if (equal < 0)
3432 return -1;
3433 if (!equal)
3434 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3436 /* Check that empty solution lie in the right space. */
3437 str = "[n] -> { [t,a] : 1 = 0 }";
3438 set = isl_set_read_from_str(ctx, str);
3439 pwaff = isl_set_dim_max(set, 0);
3440 set1 = isl_set_from_pw_aff(pwaff);
3441 str = "[n] -> { [t] : 1 = 0 }";
3442 set2 = isl_set_read_from_str(ctx, str);
3443 equal = isl_set_is_equal(set1, set2);
3444 isl_set_free(set1);
3445 isl_set_free(set2);
3447 if (equal < 0)
3448 return -1;
3449 if (!equal)
3450 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3452 return 0;
3455 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
3457 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
3458 const char *str)
3460 isl_ctx *ctx;
3461 isl_pw_multi_aff *pma2;
3462 int equal;
3464 if (!pma)
3465 return -1;
3467 ctx = isl_pw_multi_aff_get_ctx(pma);
3468 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3469 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
3470 isl_pw_multi_aff_free(pma2);
3472 return equal;
3475 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
3476 * represented by "str".
3478 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
3479 const char *str)
3481 int equal;
3483 equal = pw_multi_aff_plain_is_equal(pma, str);
3484 if (equal < 0)
3485 return -1;
3486 if (!equal)
3487 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
3488 "result not as expected", return -1);
3489 return 0;
3492 /* Basic test for isl_pw_multi_aff_product.
3494 * Check that multiple pieces are properly handled.
3496 static int test_product_pma(isl_ctx *ctx)
3498 int equal;
3499 const char *str;
3500 isl_pw_multi_aff *pma1, *pma2;
3502 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
3503 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3504 str = "{ C[] -> D[] }";
3505 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3506 pma1 = isl_pw_multi_aff_product(pma1, pma2);
3507 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
3508 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
3509 equal = pw_multi_aff_check_plain_equal(pma1, str);
3510 isl_pw_multi_aff_free(pma1);
3511 if (equal < 0)
3512 return -1;
3514 return 0;
3517 int test_product(isl_ctx *ctx)
3519 const char *str;
3520 isl_set *set;
3521 isl_union_set *uset1, *uset2;
3522 int ok;
3524 str = "{ A[i] }";
3525 set = isl_set_read_from_str(ctx, str);
3526 set = isl_set_product(set, isl_set_copy(set));
3527 ok = isl_set_is_wrapping(set);
3528 isl_set_free(set);
3529 if (ok < 0)
3530 return -1;
3531 if (!ok)
3532 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3534 str = "{ [] }";
3535 uset1 = isl_union_set_read_from_str(ctx, str);
3536 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3537 str = "{ [[] -> []] }";
3538 uset2 = isl_union_set_read_from_str(ctx, str);
3539 ok = isl_union_set_is_equal(uset1, uset2);
3540 isl_union_set_free(uset1);
3541 isl_union_set_free(uset2);
3542 if (ok < 0)
3543 return -1;
3544 if (!ok)
3545 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3547 if (test_product_pma(ctx) < 0)
3548 return -1;
3550 return 0;
3553 /* Check that two sets are not considered disjoint just because
3554 * they have a different set of (named) parameters.
3556 static int test_disjoint(isl_ctx *ctx)
3558 const char *str;
3559 isl_set *set, *set2;
3560 int disjoint;
3562 str = "[n] -> { [[]->[]] }";
3563 set = isl_set_read_from_str(ctx, str);
3564 str = "{ [[]->[]] }";
3565 set2 = isl_set_read_from_str(ctx, str);
3566 disjoint = isl_set_is_disjoint(set, set2);
3567 isl_set_free(set);
3568 isl_set_free(set2);
3569 if (disjoint < 0)
3570 return -1;
3571 if (disjoint)
3572 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3574 return 0;
3577 int test_equal(isl_ctx *ctx)
3579 const char *str;
3580 isl_set *set, *set2;
3581 int equal;
3583 str = "{ S_6[i] }";
3584 set = isl_set_read_from_str(ctx, str);
3585 str = "{ S_7[i] }";
3586 set2 = isl_set_read_from_str(ctx, str);
3587 equal = isl_set_is_equal(set, set2);
3588 isl_set_free(set);
3589 isl_set_free(set2);
3590 if (equal < 0)
3591 return -1;
3592 if (equal)
3593 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3595 return 0;
3598 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3599 enum isl_dim_type type, unsigned pos, int fixed)
3601 int test;
3603 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3604 isl_map_free(map);
3605 if (test < 0)
3606 return -1;
3607 if (test == fixed)
3608 return 0;
3609 if (fixed)
3610 isl_die(ctx, isl_error_unknown,
3611 "map not detected as fixed", return -1);
3612 else
3613 isl_die(ctx, isl_error_unknown,
3614 "map detected as fixed", return -1);
3617 int test_fixed(isl_ctx *ctx)
3619 const char *str;
3620 isl_map *map;
3622 str = "{ [i] -> [i] }";
3623 map = isl_map_read_from_str(ctx, str);
3624 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3625 return -1;
3626 str = "{ [i] -> [1] }";
3627 map = isl_map_read_from_str(ctx, str);
3628 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3629 return -1;
3630 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3631 map = isl_map_read_from_str(ctx, str);
3632 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3633 return -1;
3634 map = isl_map_read_from_str(ctx, str);
3635 map = isl_map_neg(map);
3636 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3637 return -1;
3639 return 0;
3642 struct isl_vertices_test_data {
3643 const char *set;
3644 int n;
3645 const char *vertex[2];
3646 } vertices_tests[] = {
3647 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
3648 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
3649 { "{ A[t, i] : t = 14 and i = 1 }",
3650 1, { "{ A[14, 1] }" } },
3653 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
3655 static int find_vertex(__isl_take isl_vertex *vertex, void *user)
3657 struct isl_vertices_test_data *data = user;
3658 isl_ctx *ctx;
3659 isl_multi_aff *ma;
3660 isl_basic_set *bset;
3661 isl_pw_multi_aff *pma;
3662 int i;
3663 int equal;
3665 ctx = isl_vertex_get_ctx(vertex);
3666 bset = isl_vertex_get_domain(vertex);
3667 ma = isl_vertex_get_expr(vertex);
3668 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
3670 for (i = 0; i < data->n; ++i) {
3671 isl_pw_multi_aff *pma_i;
3673 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
3674 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
3675 isl_pw_multi_aff_free(pma_i);
3677 if (equal < 0 || equal)
3678 break;
3681 isl_pw_multi_aff_free(pma);
3682 isl_vertex_free(vertex);
3684 if (equal < 0)
3685 return -1;
3687 return equal ? 0 : - 1;
3690 int test_vertices(isl_ctx *ctx)
3692 int i;
3694 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
3695 isl_basic_set *bset;
3696 isl_vertices *vertices;
3697 int ok = 1;
3698 int n;
3700 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
3701 vertices = isl_basic_set_compute_vertices(bset);
3702 n = isl_vertices_get_n_vertices(vertices);
3703 if (vertices_tests[i].n != n)
3704 ok = 0;
3705 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
3706 &vertices_tests[i]) < 0)
3707 ok = 0;
3708 isl_vertices_free(vertices);
3709 isl_basic_set_free(bset);
3711 if (!vertices)
3712 return -1;
3713 if (!ok)
3714 isl_die(ctx, isl_error_unknown, "unexpected vertices",
3715 return -1);
3718 return 0;
3721 int test_union_pw(isl_ctx *ctx)
3723 int equal;
3724 const char *str;
3725 isl_union_set *uset;
3726 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3728 str = "{ [x] -> x^2 }";
3729 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3730 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3731 uset = isl_union_pw_qpolynomial_domain(upwqp1);
3732 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3733 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3734 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3735 isl_union_pw_qpolynomial_free(upwqp1);
3736 isl_union_pw_qpolynomial_free(upwqp2);
3737 if (equal < 0)
3738 return -1;
3739 if (!equal)
3740 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3742 return 0;
3745 int test_output(isl_ctx *ctx)
3747 char *s;
3748 const char *str;
3749 isl_pw_aff *pa;
3750 isl_printer *p;
3751 int equal;
3753 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3754 pa = isl_pw_aff_read_from_str(ctx, str);
3756 p = isl_printer_to_str(ctx);
3757 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3758 p = isl_printer_print_pw_aff(p, pa);
3759 s = isl_printer_get_str(p);
3760 isl_printer_free(p);
3761 isl_pw_aff_free(pa);
3762 if (!s)
3763 equal = -1;
3764 else
3765 equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
3766 free(s);
3767 if (equal < 0)
3768 return -1;
3769 if (!equal)
3770 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3772 return 0;
3775 int test_sample(isl_ctx *ctx)
3777 const char *str;
3778 isl_basic_set *bset1, *bset2;
3779 int empty, subset;
3781 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3782 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3783 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3784 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3785 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3786 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3787 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3788 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3789 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3790 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3791 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3792 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3793 "d - 1073741821e and "
3794 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3795 "3j >= 1 - a + b + 2e and "
3796 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3797 "3i <= 4 - a + 4b - e and "
3798 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3799 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3800 "c <= -1 + a and 3i >= -2 - a + 3e and "
3801 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3802 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3803 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3804 "1073741823e >= 1 + 1073741823b - d and "
3805 "3i >= 1073741823b + c - 1073741823e - f and "
3806 "3i >= 1 + 2b + e + 3g }";
3807 bset1 = isl_basic_set_read_from_str(ctx, str);
3808 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3809 empty = isl_basic_set_is_empty(bset2);
3810 subset = isl_basic_set_is_subset(bset2, bset1);
3811 isl_basic_set_free(bset1);
3812 isl_basic_set_free(bset2);
3813 if (empty < 0 || subset < 0)
3814 return -1;
3815 if (empty)
3816 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3817 if (!subset)
3818 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3820 return 0;
3823 int test_fixed_power(isl_ctx *ctx)
3825 const char *str;
3826 isl_map *map;
3827 isl_int exp;
3828 int equal;
3830 isl_int_init(exp);
3831 str = "{ [i] -> [i + 1] }";
3832 map = isl_map_read_from_str(ctx, str);
3833 isl_int_set_si(exp, 23);
3834 map = isl_map_fixed_power(map, exp);
3835 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3836 isl_int_clear(exp);
3837 isl_map_free(map);
3838 if (equal < 0)
3839 return -1;
3841 return 0;
3844 int test_slice(isl_ctx *ctx)
3846 const char *str;
3847 isl_map *map;
3848 int equal;
3850 str = "{ [i] -> [j] }";
3851 map = isl_map_read_from_str(ctx, str);
3852 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3853 equal = map_check_equal(map, "{ [i] -> [i] }");
3854 isl_map_free(map);
3855 if (equal < 0)
3856 return -1;
3858 str = "{ [i] -> [j] }";
3859 map = isl_map_read_from_str(ctx, str);
3860 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
3861 equal = map_check_equal(map, "{ [i] -> [j] }");
3862 isl_map_free(map);
3863 if (equal < 0)
3864 return -1;
3866 str = "{ [i] -> [j] }";
3867 map = isl_map_read_from_str(ctx, str);
3868 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
3869 equal = map_check_equal(map, "{ [i] -> [-i] }");
3870 isl_map_free(map);
3871 if (equal < 0)
3872 return -1;
3874 str = "{ [i] -> [j] }";
3875 map = isl_map_read_from_str(ctx, str);
3876 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
3877 equal = map_check_equal(map, "{ [0] -> [j] }");
3878 isl_map_free(map);
3879 if (equal < 0)
3880 return -1;
3882 str = "{ [i] -> [j] }";
3883 map = isl_map_read_from_str(ctx, str);
3884 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3885 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3886 isl_map_free(map);
3887 if (equal < 0)
3888 return -1;
3890 str = "{ [i] -> [j] }";
3891 map = isl_map_read_from_str(ctx, str);
3892 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3893 equal = map_check_equal(map, "{ [i] -> [j] : false }");
3894 isl_map_free(map);
3895 if (equal < 0)
3896 return -1;
3898 return 0;
3901 int test_eliminate(isl_ctx *ctx)
3903 const char *str;
3904 isl_map *map;
3905 int equal;
3907 str = "{ [i] -> [j] : i = 2j }";
3908 map = isl_map_read_from_str(ctx, str);
3909 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3910 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3911 isl_map_free(map);
3912 if (equal < 0)
3913 return -1;
3915 return 0;
3918 /* Check that isl_set_dim_residue_class detects that the values of j
3919 * in the set below are all odd and that it does not detect any spurious
3920 * strides.
3922 static int test_residue_class(isl_ctx *ctx)
3924 const char *str;
3925 isl_set *set;
3926 isl_int m, r;
3927 int res;
3929 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3930 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3931 set = isl_set_read_from_str(ctx, str);
3932 isl_int_init(m);
3933 isl_int_init(r);
3934 res = isl_set_dim_residue_class(set, 1, &m, &r);
3935 if (res >= 0 &&
3936 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3937 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3938 res = -1);
3939 isl_int_clear(r);
3940 isl_int_clear(m);
3941 isl_set_free(set);
3943 return res;
3946 int test_align_parameters(isl_ctx *ctx)
3948 const char *str;
3949 isl_space *space;
3950 isl_multi_aff *ma1, *ma2;
3951 int equal;
3953 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3954 ma1 = isl_multi_aff_read_from_str(ctx, str);
3956 space = isl_space_params_alloc(ctx, 1);
3957 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
3958 ma1 = isl_multi_aff_align_params(ma1, space);
3960 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3961 ma2 = isl_multi_aff_read_from_str(ctx, str);
3963 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3965 isl_multi_aff_free(ma1);
3966 isl_multi_aff_free(ma2);
3968 if (equal < 0)
3969 return -1;
3970 if (!equal)
3971 isl_die(ctx, isl_error_unknown,
3972 "result not as expected", return -1);
3974 return 0;
3977 static int test_list(isl_ctx *ctx)
3979 isl_id *a, *b, *c, *d, *id;
3980 isl_id_list *list;
3981 int ok;
3983 a = isl_id_alloc(ctx, "a", NULL);
3984 b = isl_id_alloc(ctx, "b", NULL);
3985 c = isl_id_alloc(ctx, "c", NULL);
3986 d = isl_id_alloc(ctx, "d", NULL);
3988 list = isl_id_list_alloc(ctx, 4);
3989 list = isl_id_list_add(list, a);
3990 list = isl_id_list_add(list, b);
3991 list = isl_id_list_add(list, c);
3992 list = isl_id_list_add(list, d);
3993 list = isl_id_list_drop(list, 1, 1);
3995 if (isl_id_list_n_id(list) != 3) {
3996 isl_id_list_free(list);
3997 isl_die(ctx, isl_error_unknown,
3998 "unexpected number of elements in list", return -1);
4001 id = isl_id_list_get_id(list, 0);
4002 ok = id == a;
4003 isl_id_free(id);
4004 id = isl_id_list_get_id(list, 1);
4005 ok = ok && id == c;
4006 isl_id_free(id);
4007 id = isl_id_list_get_id(list, 2);
4008 ok = ok && id == d;
4009 isl_id_free(id);
4011 isl_id_list_free(list);
4013 if (!ok)
4014 isl_die(ctx, isl_error_unknown,
4015 "unexpected elements in list", return -1);
4017 return 0;
4020 const char *set_conversion_tests[] = {
4021 "[N] -> { [i] : N - 1 <= 2 i <= N }",
4022 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
4023 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4024 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4027 /* Check that converting from isl_set to isl_pw_multi_aff and back
4028 * to isl_set produces the original isl_set.
4030 static int test_set_conversion(isl_ctx *ctx)
4032 int i;
4033 const char *str;
4034 isl_set *set1, *set2;
4035 isl_pw_multi_aff *pma;
4036 int equal;
4038 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
4039 str = set_conversion_tests[i];
4040 set1 = isl_set_read_from_str(ctx, str);
4041 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
4042 set2 = isl_set_from_pw_multi_aff(pma);
4043 equal = isl_set_is_equal(set1, set2);
4044 isl_set_free(set1);
4045 isl_set_free(set2);
4047 if (equal < 0)
4048 return -1;
4049 if (!equal)
4050 isl_die(ctx, isl_error_unknown, "bad conversion",
4051 return -1);
4054 return 0;
4057 /* Check that converting from isl_map to isl_pw_multi_aff and back
4058 * to isl_map produces the original isl_map.
4060 static int test_map_conversion(isl_ctx *ctx)
4062 const char *str;
4063 isl_map *map1, *map2;
4064 isl_pw_multi_aff *pma;
4065 int equal;
4067 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
4068 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
4069 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
4070 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
4071 "9e <= -2 - 2a) }";
4072 map1 = isl_map_read_from_str(ctx, str);
4073 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
4074 map2 = isl_map_from_pw_multi_aff(pma);
4075 equal = isl_map_is_equal(map1, map2);
4076 isl_map_free(map1);
4077 isl_map_free(map2);
4079 if (equal < 0)
4080 return -1;
4081 if (!equal)
4082 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
4084 return 0;
4087 static int test_conversion(isl_ctx *ctx)
4089 if (test_set_conversion(ctx) < 0)
4090 return -1;
4091 if (test_map_conversion(ctx) < 0)
4092 return -1;
4093 return 0;
4096 /* Check that isl_basic_map_curry does not modify input.
4098 static int test_curry(isl_ctx *ctx)
4100 const char *str;
4101 isl_basic_map *bmap1, *bmap2;
4102 int equal;
4104 str = "{ [A[] -> B[]] -> C[] }";
4105 bmap1 = isl_basic_map_read_from_str(ctx, str);
4106 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
4107 equal = isl_basic_map_is_equal(bmap1, bmap2);
4108 isl_basic_map_free(bmap1);
4109 isl_basic_map_free(bmap2);
4111 if (equal < 0)
4112 return -1;
4113 if (equal)
4114 isl_die(ctx, isl_error_unknown,
4115 "curried map should not be equal to original",
4116 return -1);
4118 return 0;
4121 struct {
4122 const char *set;
4123 const char *ma;
4124 const char *res;
4125 } preimage_tests[] = {
4126 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
4127 "{ A[j,i] -> B[i,j] }",
4128 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
4129 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4130 "{ A[a,b] -> B[a/2,b/6] }",
4131 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
4132 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4133 "{ A[a,b] -> B[a/2,b/6] }",
4134 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
4135 "exists i,j : a = 2 i and b = 6 j }" },
4136 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
4137 "[n] -> { : 0 <= n <= 100 }" },
4138 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4139 "{ A[a] -> B[2a] }",
4140 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
4141 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4142 "{ A[a] -> B[([a/2])] }",
4143 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
4144 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
4145 "{ A[a] -> B[a,a,a/3] }",
4146 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
4147 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
4148 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
4151 static int test_preimage_basic_set(isl_ctx *ctx)
4153 int i;
4154 isl_basic_set *bset1, *bset2;
4155 isl_multi_aff *ma;
4156 int equal;
4158 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
4159 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
4160 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
4161 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
4162 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
4163 equal = isl_basic_set_is_equal(bset1, bset2);
4164 isl_basic_set_free(bset1);
4165 isl_basic_set_free(bset2);
4166 if (equal < 0)
4167 return -1;
4168 if (!equal)
4169 isl_die(ctx, isl_error_unknown, "bad preimage",
4170 return -1);
4173 return 0;
4176 struct {
4177 const char *map;
4178 const char *ma;
4179 const char *res;
4180 } preimage_domain_tests[] = {
4181 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
4182 "{ A[j,i] -> B[i,j] }",
4183 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
4184 { "{ B[i] -> C[i]; D[i] -> E[i] }",
4185 "{ A[i] -> B[i + 1] }",
4186 "{ A[i] -> C[i + 1] }" },
4187 { "{ B[i] -> C[i]; B[i] -> E[i] }",
4188 "{ A[i] -> B[i + 1] }",
4189 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
4190 { "{ B[i] -> C[([i/2])] }",
4191 "{ A[i] -> B[2i] }",
4192 "{ A[i] -> C[i] }" },
4193 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
4194 "{ A[i] -> B[([i/5]), ([i/7])] }",
4195 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
4196 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
4197 "[N] -> { A[] -> B[([N/5])] }",
4198 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
4199 { "{ B[i] -> C[i] : exists a : i = 5 a }",
4200 "{ A[i] -> B[2i] }",
4201 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
4202 { "{ B[i] -> C[i] : exists a : i = 2 a; "
4203 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
4204 "{ A[i] -> B[2i] }",
4205 "{ A[i] -> C[2i] }" },
4208 static int test_preimage_union_map(isl_ctx *ctx)
4210 int i;
4211 isl_union_map *umap1, *umap2;
4212 isl_multi_aff *ma;
4213 int equal;
4215 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
4216 umap1 = isl_union_map_read_from_str(ctx,
4217 preimage_domain_tests[i].map);
4218 ma = isl_multi_aff_read_from_str(ctx,
4219 preimage_domain_tests[i].ma);
4220 umap2 = isl_union_map_read_from_str(ctx,
4221 preimage_domain_tests[i].res);
4222 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
4223 equal = isl_union_map_is_equal(umap1, umap2);
4224 isl_union_map_free(umap1);
4225 isl_union_map_free(umap2);
4226 if (equal < 0)
4227 return -1;
4228 if (!equal)
4229 isl_die(ctx, isl_error_unknown, "bad preimage",
4230 return -1);
4233 return 0;
4236 static int test_preimage(isl_ctx *ctx)
4238 if (test_preimage_basic_set(ctx) < 0)
4239 return -1;
4240 if (test_preimage_union_map(ctx) < 0)
4241 return -1;
4243 return 0;
4246 struct {
4247 const char *ma1;
4248 const char *ma;
4249 const char *res;
4250 } pullback_tests[] = {
4251 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
4252 "{ A[a,b] -> C[b + 2a] }" },
4253 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
4254 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
4255 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
4256 "{ A[a] -> C[(a)/6] }" },
4257 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
4258 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
4259 "{ A[a] -> C[(2a)/3] }" },
4260 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
4261 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
4262 "{ A[i,j] -> C[i + j, i + j] }"},
4263 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
4264 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
4265 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
4268 static int test_pullback(isl_ctx *ctx)
4270 int i;
4271 isl_multi_aff *ma1, *ma2;
4272 isl_multi_aff *ma;
4273 int equal;
4275 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
4276 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
4277 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
4278 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
4279 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
4280 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4281 isl_multi_aff_free(ma1);
4282 isl_multi_aff_free(ma2);
4283 if (equal < 0)
4284 return -1;
4285 if (!equal)
4286 isl_die(ctx, isl_error_unknown, "bad pullback",
4287 return -1);
4290 return 0;
4293 /* Check that negation is printed correctly.
4295 static int test_ast(isl_ctx *ctx)
4297 isl_ast_expr *expr, *expr1, *expr2, *expr3;
4298 char *str;
4299 int ok;
4301 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4302 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4303 expr = isl_ast_expr_add(expr1, expr2);
4304 expr = isl_ast_expr_neg(expr);
4305 str = isl_ast_expr_to_str(expr);
4306 ok = str ? !strcmp(str, "-(A + B)") : -1;
4307 free(str);
4308 isl_ast_expr_free(expr);
4310 if (ok < 0)
4311 return -1;
4312 if (!ok)
4313 isl_die(ctx, isl_error_unknown,
4314 "isl_ast_expr printed incorrectly", return -1);
4316 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4317 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4318 expr = isl_ast_expr_add(expr1, expr2);
4319 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
4320 expr = isl_ast_expr_sub(expr3, expr);
4321 str = isl_ast_expr_to_str(expr);
4322 ok = str ? !strcmp(str, "C - (A + B)") : -1;
4323 free(str);
4324 isl_ast_expr_free(expr);
4326 if (ok < 0)
4327 return -1;
4328 if (!ok)
4329 isl_die(ctx, isl_error_unknown,
4330 "isl_ast_expr printed incorrectly", return -1);
4332 return 0;
4335 /* Check that isl_ast_build_expr_from_set returns a valid expression
4336 * for an empty set. Note that isl_ast_build_expr_from_set getting
4337 * called on an empty set probably indicates a bug in the caller.
4339 static int test_ast_build(isl_ctx *ctx)
4341 isl_set *set;
4342 isl_ast_build *build;
4343 isl_ast_expr *expr;
4345 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4346 build = isl_ast_build_from_context(set);
4348 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
4349 expr = isl_ast_build_expr_from_set(build, set);
4351 isl_ast_expr_free(expr);
4352 isl_ast_build_free(build);
4354 if (!expr)
4355 return -1;
4357 return 0;
4360 /* Internal data structure for before_for and after_for callbacks.
4362 * depth is the current depth
4363 * before is the number of times before_for has been called
4364 * after is the number of times after_for has been called
4366 struct isl_test_codegen_data {
4367 int depth;
4368 int before;
4369 int after;
4372 /* This function is called before each for loop in the AST generated
4373 * from test_ast_gen1.
4375 * Increment the number of calls and the depth.
4376 * Check that the space returned by isl_ast_build_get_schedule_space
4377 * matches the target space of the schedule returned by
4378 * isl_ast_build_get_schedule.
4379 * Return an isl_id that is checked by the corresponding call
4380 * to after_for.
4382 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
4383 void *user)
4385 struct isl_test_codegen_data *data = user;
4386 isl_ctx *ctx;
4387 isl_space *space;
4388 isl_union_map *schedule;
4389 isl_union_set *uset;
4390 isl_set *set;
4391 int empty;
4392 char name[] = "d0";
4394 ctx = isl_ast_build_get_ctx(build);
4396 if (data->before >= 3)
4397 isl_die(ctx, isl_error_unknown,
4398 "unexpected number of for nodes", return NULL);
4399 if (data->depth >= 2)
4400 isl_die(ctx, isl_error_unknown,
4401 "unexpected depth", return NULL);
4403 snprintf(name, sizeof(name), "d%d", data->depth);
4404 data->before++;
4405 data->depth++;
4407 schedule = isl_ast_build_get_schedule(build);
4408 uset = isl_union_map_range(schedule);
4409 if (!uset)
4410 return NULL;
4411 if (isl_union_set_n_set(uset) != 1) {
4412 isl_union_set_free(uset);
4413 isl_die(ctx, isl_error_unknown,
4414 "expecting single range space", return NULL);
4417 space = isl_ast_build_get_schedule_space(build);
4418 set = isl_union_set_extract_set(uset, space);
4419 isl_union_set_free(uset);
4420 empty = isl_set_is_empty(set);
4421 isl_set_free(set);
4423 if (empty < 0)
4424 return NULL;
4425 if (empty)
4426 isl_die(ctx, isl_error_unknown,
4427 "spaces don't match", return NULL);
4429 return isl_id_alloc(ctx, name, NULL);
4432 /* This function is called after each for loop in the AST generated
4433 * from test_ast_gen1.
4435 * Increment the number of calls and decrement the depth.
4436 * Check that the annotation attached to the node matches
4437 * the isl_id returned by the corresponding call to before_for.
4439 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
4440 __isl_keep isl_ast_build *build, void *user)
4442 struct isl_test_codegen_data *data = user;
4443 isl_id *id;
4444 const char *name;
4445 int valid;
4447 data->after++;
4448 data->depth--;
4450 if (data->after > data->before)
4451 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4452 "mismatch in number of for nodes",
4453 return isl_ast_node_free(node));
4455 id = isl_ast_node_get_annotation(node);
4456 if (!id)
4457 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4458 "missing annotation", return isl_ast_node_free(node));
4460 name = isl_id_get_name(id);
4461 valid = name && atoi(name + 1) == data->depth;
4462 isl_id_free(id);
4464 if (!valid)
4465 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4466 "wrong annotation", return isl_ast_node_free(node));
4468 return node;
4471 /* Check that the before_each_for and after_each_for callbacks
4472 * are called for each for loop in the generated code,
4473 * that they are called in the right order and that the isl_id
4474 * returned from the before_each_for callback is attached to
4475 * the isl_ast_node passed to the corresponding after_each_for call.
4477 static int test_ast_gen1(isl_ctx *ctx)
4479 const char *str;
4480 isl_set *set;
4481 isl_union_map *schedule;
4482 isl_ast_build *build;
4483 isl_ast_node *tree;
4484 struct isl_test_codegen_data data;
4486 str = "[N] -> { : N >= 10 }";
4487 set = isl_set_read_from_str(ctx, str);
4488 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
4489 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
4490 schedule = isl_union_map_read_from_str(ctx, str);
4492 data.before = 0;
4493 data.after = 0;
4494 data.depth = 0;
4495 build = isl_ast_build_from_context(set);
4496 build = isl_ast_build_set_before_each_for(build,
4497 &before_for, &data);
4498 build = isl_ast_build_set_after_each_for(build,
4499 &after_for, &data);
4500 tree = isl_ast_build_ast_from_schedule(build, schedule);
4501 isl_ast_build_free(build);
4502 if (!tree)
4503 return -1;
4505 isl_ast_node_free(tree);
4507 if (data.before != 3 || data.after != 3)
4508 isl_die(ctx, isl_error_unknown,
4509 "unexpected number of for nodes", return -1);
4511 return 0;
4514 /* Check that the AST generator handles domains that are integrally disjoint
4515 * but not ratinoally disjoint.
4517 static int test_ast_gen2(isl_ctx *ctx)
4519 const char *str;
4520 isl_set *set;
4521 isl_union_map *schedule;
4522 isl_union_map *options;
4523 isl_ast_build *build;
4524 isl_ast_node *tree;
4526 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4527 schedule = isl_union_map_read_from_str(ctx, str);
4528 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4529 build = isl_ast_build_from_context(set);
4531 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4532 options = isl_union_map_read_from_str(ctx, str);
4533 build = isl_ast_build_set_options(build, options);
4534 tree = isl_ast_build_ast_from_schedule(build, schedule);
4535 isl_ast_build_free(build);
4536 if (!tree)
4537 return -1;
4538 isl_ast_node_free(tree);
4540 return 0;
4543 /* Increment *user on each call.
4545 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4546 __isl_keep isl_ast_build *build, void *user)
4548 int *n = user;
4550 (*n)++;
4552 return node;
4555 /* Test that unrolling tries to minimize the number of instances.
4556 * In particular, for the schedule given below, make sure it generates
4557 * 3 nodes (rather than 101).
4559 static int test_ast_gen3(isl_ctx *ctx)
4561 const char *str;
4562 isl_set *set;
4563 isl_union_map *schedule;
4564 isl_union_map *options;
4565 isl_ast_build *build;
4566 isl_ast_node *tree;
4567 int n_domain = 0;
4569 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4570 schedule = isl_union_map_read_from_str(ctx, str);
4571 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4573 str = "{ [i] -> unroll[0] }";
4574 options = isl_union_map_read_from_str(ctx, str);
4576 build = isl_ast_build_from_context(set);
4577 build = isl_ast_build_set_options(build, options);
4578 build = isl_ast_build_set_at_each_domain(build,
4579 &count_domains, &n_domain);
4580 tree = isl_ast_build_ast_from_schedule(build, schedule);
4581 isl_ast_build_free(build);
4582 if (!tree)
4583 return -1;
4585 isl_ast_node_free(tree);
4587 if (n_domain != 3)
4588 isl_die(ctx, isl_error_unknown,
4589 "unexpected number of for nodes", return -1);
4591 return 0;
4594 /* Check that if the ast_build_exploit_nested_bounds options is set,
4595 * we do not get an outer if node in the generated AST,
4596 * while we do get such an outer if node if the options is not set.
4598 static int test_ast_gen4(isl_ctx *ctx)
4600 const char *str;
4601 isl_set *set;
4602 isl_union_map *schedule;
4603 isl_ast_build *build;
4604 isl_ast_node *tree;
4605 enum isl_ast_node_type type;
4606 int enb;
4608 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4609 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4611 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4613 schedule = isl_union_map_read_from_str(ctx, str);
4614 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4615 build = isl_ast_build_from_context(set);
4616 tree = isl_ast_build_ast_from_schedule(build, schedule);
4617 isl_ast_build_free(build);
4618 if (!tree)
4619 return -1;
4621 type = isl_ast_node_get_type(tree);
4622 isl_ast_node_free(tree);
4624 if (type == isl_ast_node_if)
4625 isl_die(ctx, isl_error_unknown,
4626 "not expecting if node", return -1);
4628 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4630 schedule = isl_union_map_read_from_str(ctx, str);
4631 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4632 build = isl_ast_build_from_context(set);
4633 tree = isl_ast_build_ast_from_schedule(build, schedule);
4634 isl_ast_build_free(build);
4635 if (!tree)
4636 return -1;
4638 type = isl_ast_node_get_type(tree);
4639 isl_ast_node_free(tree);
4641 if (type != isl_ast_node_if)
4642 isl_die(ctx, isl_error_unknown,
4643 "expecting if node", return -1);
4645 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4647 return 0;
4650 /* This function is called for each leaf in the AST generated
4651 * from test_ast_gen5.
4653 * We finalize the AST generation by extending the outer schedule
4654 * with a zero-dimensional schedule. If this results in any for loops,
4655 * then this means that we did not pass along enough information
4656 * about the outer schedule to the inner AST generation.
4658 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4659 void *user)
4661 isl_union_map *schedule, *extra;
4662 isl_ast_node *tree;
4664 schedule = isl_ast_build_get_schedule(build);
4665 extra = isl_union_map_copy(schedule);
4666 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4667 schedule = isl_union_map_range_product(schedule, extra);
4668 tree = isl_ast_build_ast_from_schedule(build, schedule);
4669 isl_ast_build_free(build);
4671 if (!tree)
4672 return NULL;
4674 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4675 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4676 "code should not contain any for loop",
4677 return isl_ast_node_free(tree));
4679 return tree;
4682 /* Check that we do not lose any information when going back and
4683 * forth between internal and external schedule.
4685 * In particular, we create an AST where we unroll the only
4686 * non-constant dimension in the schedule. We therefore do
4687 * not expect any for loops in the AST. However, older versions
4688 * of isl would not pass along enough information about the outer
4689 * schedule when performing an inner code generation from a create_leaf
4690 * callback, resulting in the inner code generation producing a for loop.
4692 static int test_ast_gen5(isl_ctx *ctx)
4694 const char *str;
4695 isl_set *set;
4696 isl_union_map *schedule, *options;
4697 isl_ast_build *build;
4698 isl_ast_node *tree;
4700 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4701 schedule = isl_union_map_read_from_str(ctx, str);
4703 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4704 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4705 options = isl_union_map_read_from_str(ctx, str);
4707 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4708 build = isl_ast_build_from_context(set);
4709 build = isl_ast_build_set_options(build, options);
4710 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4711 tree = isl_ast_build_ast_from_schedule(build, schedule);
4712 isl_ast_build_free(build);
4713 isl_ast_node_free(tree);
4714 if (!tree)
4715 return -1;
4717 return 0;
4720 static int test_ast_gen(isl_ctx *ctx)
4722 if (test_ast_gen1(ctx) < 0)
4723 return -1;
4724 if (test_ast_gen2(ctx) < 0)
4725 return -1;
4726 if (test_ast_gen3(ctx) < 0)
4727 return -1;
4728 if (test_ast_gen4(ctx) < 0)
4729 return -1;
4730 if (test_ast_gen5(ctx) < 0)
4731 return -1;
4732 return 0;
4735 /* Check if dropping output dimensions from an isl_pw_multi_aff
4736 * works properly.
4738 static int test_pw_multi_aff(isl_ctx *ctx)
4740 const char *str;
4741 isl_pw_multi_aff *pma1, *pma2;
4742 int equal;
4744 str = "{ [i,j] -> [i+j, 4i-j] }";
4745 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4746 str = "{ [i,j] -> [4i-j] }";
4747 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4749 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4751 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4753 isl_pw_multi_aff_free(pma1);
4754 isl_pw_multi_aff_free(pma2);
4755 if (equal < 0)
4756 return -1;
4757 if (!equal)
4758 isl_die(ctx, isl_error_unknown,
4759 "expressions not equal", return -1);
4761 return 0;
4764 /* Check that we can properly parse multi piecewise affine expressions
4765 * where the piecewise affine expressions have different domains.
4767 static int test_multi_pw_aff(isl_ctx *ctx)
4769 const char *str;
4770 isl_set *dom, *dom2;
4771 isl_multi_pw_aff *mpa1, *mpa2;
4772 isl_pw_aff *pa;
4773 int equal;
4774 int equal_domain;
4776 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
4777 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
4778 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
4779 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
4780 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
4781 str = "{ [i] -> [(i : i > 0), 2i] }";
4782 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
4784 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
4786 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
4787 dom = isl_pw_aff_domain(pa);
4788 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
4789 dom2 = isl_pw_aff_domain(pa);
4790 equal_domain = isl_set_is_equal(dom, dom2);
4792 isl_set_free(dom);
4793 isl_set_free(dom2);
4794 isl_multi_pw_aff_free(mpa1);
4795 isl_multi_pw_aff_free(mpa2);
4797 if (equal < 0)
4798 return -1;
4799 if (!equal)
4800 isl_die(ctx, isl_error_unknown,
4801 "expressions not equal", return -1);
4803 if (equal_domain < 0)
4804 return -1;
4805 if (equal_domain)
4806 isl_die(ctx, isl_error_unknown,
4807 "domains unexpectedly equal", return -1);
4809 return 0;
4812 /* This is a regression test for a bug where isl_basic_map_simplify
4813 * would end up in an infinite loop. In particular, we construct
4814 * an empty basic set that is not obviously empty.
4815 * isl_basic_set_is_empty marks the basic set as empty.
4816 * After projecting out i3, the variable can be dropped completely,
4817 * but isl_basic_map_simplify refrains from doing so if the basic set
4818 * is empty and would end up in an infinite loop if it didn't test
4819 * explicitly for empty basic maps in the outer loop.
4821 static int test_simplify(isl_ctx *ctx)
4823 const char *str;
4824 isl_basic_set *bset;
4825 int empty;
4827 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
4828 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
4829 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
4830 "i3 >= i2 }";
4831 bset = isl_basic_set_read_from_str(ctx, str);
4832 empty = isl_basic_set_is_empty(bset);
4833 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
4834 isl_basic_set_free(bset);
4835 if (!bset)
4836 return -1;
4837 if (!empty)
4838 isl_die(ctx, isl_error_unknown,
4839 "basic set should be empty", return -1);
4841 return 0;
4844 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
4845 * with gbr context would fail to disable the use of the shifted tableau
4846 * when transferring equalities for the input to the context, resulting
4847 * in invalid sample values.
4849 static int test_partial_lexmin(isl_ctx *ctx)
4851 const char *str;
4852 isl_basic_set *bset;
4853 isl_basic_map *bmap;
4854 isl_map *map;
4856 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
4857 bmap = isl_basic_map_read_from_str(ctx, str);
4858 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
4859 bset = isl_basic_set_read_from_str(ctx, str);
4860 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
4861 isl_map_free(map);
4863 if (!map)
4864 return -1;
4866 return 0;
4869 /* Check that the variable compression performed on the existentially
4870 * quantified variables inside isl_basic_set_compute_divs is not confused
4871 * by the implicit equalities among the parameters.
4873 static int test_compute_divs(isl_ctx *ctx)
4875 const char *str;
4876 isl_basic_set *bset;
4877 isl_set *set;
4879 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
4880 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
4881 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
4882 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
4883 bset = isl_basic_set_read_from_str(ctx, str);
4884 set = isl_basic_set_compute_divs(bset);
4885 isl_set_free(set);
4886 if (!set)
4887 return -1;
4889 return 0;
4892 struct {
4893 const char *name;
4894 int (*fn)(isl_ctx *ctx);
4895 } tests [] = {
4896 { "dependence analysis", &test_flow },
4897 { "val", &test_val },
4898 { "compute divs", &test_compute_divs },
4899 { "partial lexmin", &test_partial_lexmin },
4900 { "simplify", &test_simplify },
4901 { "curry", &test_curry },
4902 { "piecewise multi affine expressions", &test_pw_multi_aff },
4903 { "multi piecewise affine expressions", &test_multi_pw_aff },
4904 { "conversion", &test_conversion },
4905 { "list", &test_list },
4906 { "align parameters", &test_align_parameters },
4907 { "preimage", &test_preimage },
4908 { "pullback", &test_pullback },
4909 { "AST", &test_ast },
4910 { "AST build", &test_ast_build },
4911 { "AST generation", &test_ast_gen },
4912 { "eliminate", &test_eliminate },
4913 { "residue class", &test_residue_class },
4914 { "div", &test_div },
4915 { "slice", &test_slice },
4916 { "fixed power", &test_fixed_power },
4917 { "sample", &test_sample },
4918 { "output", &test_output },
4919 { "vertices", &test_vertices },
4920 { "fixed", &test_fixed },
4921 { "equal", &test_equal },
4922 { "disjoint", &test_disjoint },
4923 { "product", &test_product },
4924 { "dim_max", &test_dim_max },
4925 { "affine", &test_aff },
4926 { "injective", &test_injective },
4927 { "schedule", &test_schedule },
4928 { "union_pw", &test_union_pw },
4929 { "parse", &test_parse },
4930 { "single-valued", &test_sv },
4931 { "affine hull", &test_affine_hull },
4932 { "coalesce", &test_coalesce },
4933 { "factorize", &test_factorize },
4934 { "subset", &test_subset },
4935 { "subtract", &test_subtract },
4936 { "lexmin", &test_lexmin },
4937 { "min", &test_min },
4938 { "gist", &test_gist },
4939 { "piecewise quasi-polynomials", &test_pwqp },
4942 int main()
4944 int i;
4945 struct isl_ctx *ctx;
4947 srcdir = getenv("srcdir");
4948 assert(srcdir);
4950 ctx = isl_ctx_alloc();
4951 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
4952 printf("%s\n", tests[i].name);
4953 if (tests[i].fn(ctx) < 0)
4954 goto error;
4956 test_lift(ctx);
4957 test_bound(ctx);
4958 test_union(ctx);
4959 test_split_periods(ctx);
4960 test_lex(ctx);
4961 test_bijective(ctx);
4962 test_dep(ctx);
4963 test_read(ctx);
4964 test_bounded(ctx);
4965 test_construction(ctx);
4966 test_dim(ctx);
4967 test_application(ctx);
4968 test_convex_hull(ctx);
4969 test_closure(ctx);
4970 isl_ctx_free(ctx);
4971 return 0;
4972 error:
4973 isl_ctx_free(ctx);
4974 return -1;