isl_transitive_closure.c: is_acyclic: return isl_bool
[isl.git] / isl_test.c
blob4f32588cfd19e67b8f10fc3dc11e83bb621710d6
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <assert.h>
19 #include <stdio.h>
20 #include <limits.h>
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl/id.h>
26 #include <isl/set.h>
27 #include <isl/flow.h>
28 #include <isl_constraint_private.h>
29 #include <isl/polynomial.h>
30 #include <isl/union_set.h>
31 #include <isl/union_map.h>
32 #include <isl_factorization.h>
33 #include <isl/schedule.h>
34 #include <isl/schedule_node.h>
35 #include <isl_options_private.h>
36 #include <isl_vertices_private.h>
37 #include <isl/ast_build.h>
38 #include <isl/val.h>
39 #include <isl/ilp.h>
40 #include <isl_ast_build_expr.h>
41 #include <isl/options.h>
43 #include "isl_srcdir.c"
45 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
47 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
48 char *filename;
49 int length;
50 char *pattern = "%s/test_inputs/%s.%s";
52 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
53 + strlen(suffix) + 1;
54 filename = isl_alloc_array(ctx, char, length);
56 if (!filename)
57 return NULL;
59 sprintf(filename, pattern, srcdir, name, suffix);
61 return filename;
64 void test_parse_map(isl_ctx *ctx, const char *str)
66 isl_map *map;
68 map = isl_map_read_from_str(ctx, str);
69 assert(map);
70 isl_map_free(map);
73 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
75 isl_map *map, *map2;
76 int equal;
78 map = isl_map_read_from_str(ctx, str);
79 map2 = isl_map_read_from_str(ctx, str2);
80 equal = isl_map_is_equal(map, map2);
81 isl_map_free(map);
82 isl_map_free(map2);
84 if (equal < 0)
85 return -1;
86 if (!equal)
87 isl_die(ctx, isl_error_unknown, "maps not equal",
88 return -1);
90 return 0;
93 void test_parse_pwqp(isl_ctx *ctx, const char *str)
95 isl_pw_qpolynomial *pwqp;
97 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
98 assert(pwqp);
99 isl_pw_qpolynomial_free(pwqp);
102 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
104 isl_pw_aff *pwaff;
106 pwaff = isl_pw_aff_read_from_str(ctx, str);
107 assert(pwaff);
108 isl_pw_aff_free(pwaff);
111 /* Check that we can read an isl_multi_val from "str" without errors.
113 static int test_parse_multi_val(isl_ctx *ctx, const char *str)
115 isl_multi_val *mv;
117 mv = isl_multi_val_read_from_str(ctx, str);
118 isl_multi_val_free(mv);
120 return mv ? 0 : -1;
123 /* Check that printing "mpa" and parsing the output results
124 * in the same expression.
126 static isl_stat check_reparse_mpa(isl_ctx *ctx,
127 __isl_take isl_multi_pw_aff *mpa)
129 char *str;
130 isl_bool equal;
131 isl_multi_pw_aff *mpa2;
133 str = isl_multi_pw_aff_to_str(mpa);
134 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
135 free(str);
136 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
137 isl_multi_pw_aff_free(mpa);
138 isl_multi_pw_aff_free(mpa2);
139 if (equal < 0)
140 return isl_stat_error;
141 if (!equal)
142 isl_die(ctx, isl_error_unknown,
143 "parsed function not equal to original",
144 return isl_stat_error);
146 return isl_stat_ok;
149 /* String descriptions of multi piecewise affine expressions
150 * that are used for testing printing and parsing.
152 const char *parse_multi_mpa_tests[] = {
153 "{ A[x, y] -> [] : x + y >= 0 }",
154 "{ A[x, y] -> B[] : x + y >= 0 }",
155 "{ A[x, y] -> [x] : x + y >= 0 }",
156 "[N] -> { A[x, y] -> [x] : x + y <= N }",
157 "{ A[x, y] -> [x, y] : x + y >= 0 }",
158 "{ A[x, y] -> [(x : x >= 0), (y : y >= 0)] : x + y >= 0 }",
159 "[N] -> { [] : N >= 0 }",
160 "[N] -> { [] : N >= 0 }",
161 "[N] -> { [N] : N >= 0 }",
162 "[N] -> { [N, N + 1] : N >= 0 }",
163 "[N, M] -> { [(N : N >= 0), (M : M >= 0)] : N + M >= 0 }",
166 /* Test parsing of multi piecewise affine expressions by printing
167 * the expressions and checking that parsing the output results
168 * in the same expression.
169 * Do this for a couple of manually constructed expressions and
170 * a set of expressions parsed from strings.
172 static int test_parse_mpa(isl_ctx *ctx)
174 int i;
175 isl_space *space;
176 isl_set *dom;
177 isl_multi_pw_aff *mpa;
178 isl_stat r;
180 space = isl_space_set_alloc(ctx, 0, 0);
181 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
182 mpa = isl_multi_pw_aff_zero(space);
183 r = check_reparse_mpa(ctx, mpa);
184 if (r < 0)
185 return -1;
187 space = isl_space_set_alloc(ctx, 1, 0);
188 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
189 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
190 dom = isl_set_universe(isl_space_params(isl_space_copy(space)));
191 dom = isl_set_lower_bound_si(dom, isl_dim_param, 0, 5);
192 mpa = isl_multi_pw_aff_zero(space);
193 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
194 r = check_reparse_mpa(ctx, mpa);
195 if (r < 0)
196 return -1;
198 for (i = 0; i < ARRAY_SIZE(parse_multi_mpa_tests); ++i) {
199 const char *str;
201 str = parse_multi_mpa_tests[i];
202 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
203 r = check_reparse_mpa(ctx, mpa);
204 if (r < 0)
205 return -1;
208 return 0;
211 /* Check that printing "mupa" and parsing the output results
212 * in the same expression.
214 static isl_stat check_reparse_mupa(isl_ctx *ctx,
215 __isl_take isl_multi_union_pw_aff *mupa)
217 char *str;
218 isl_bool equal;
219 isl_multi_union_pw_aff *mupa2;
221 str = isl_multi_union_pw_aff_to_str(mupa);
222 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx, str);
223 free(str);
224 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
225 isl_multi_union_pw_aff_free(mupa);
226 isl_multi_union_pw_aff_free(mupa2);
227 if (equal < 0)
228 return isl_stat_error;
229 if (!equal)
230 isl_die(ctx, isl_error_unknown,
231 "parsed function not equal to original",
232 return isl_stat_error);
234 return isl_stat_ok;
237 /* String descriptions of multi union piecewise affine expressions
238 * that are used for testing printing and parsing.
240 const char *parse_multi_mupa_tests[] = {
241 "[]",
242 "A[]",
243 "A[B[] -> C[]]",
244 "(A[] : { S[x] : x > 0; T[y] : y >= 0 })",
245 "(A[] : { })",
246 "[N] -> (A[] : { })",
247 "[N] -> (A[] : { : N >= 0 })",
248 "[N] -> (A[] : { S[x] : x > N; T[y] : y >= 0 })",
249 "(A[] : [N] -> { S[x] : x > N; T[y] : y >= 0 })",
250 "A[{ S[x] -> [x + 1]; T[x] -> [x] }]",
251 "(A[{ S[x] -> [x + 1]; T[x] -> [x] }] : "
252 "{ S[x] : x > 0; T[y] : y >= 0 })",
255 /* Test parsing of multi union piecewise affine expressions by printing
256 * the expressions and checking that parsing the output results
257 * in the same expression.
258 * Do this for a couple of manually constructed expressions and
259 * a set of expressions parsed from strings.
261 static int test_parse_mupa(isl_ctx *ctx)
263 int i;
264 isl_space *space;
265 isl_multi_union_pw_aff *mupa;
266 isl_set *dom;
267 isl_union_set *uset;
268 isl_stat r;
270 space = isl_space_set_alloc(ctx, 0, 0);
271 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
272 mupa = isl_multi_union_pw_aff_zero(space);
273 r = check_reparse_mupa(ctx, mupa);
274 if (r < 0)
275 return -1;
277 space = isl_space_set_alloc(ctx, 1, 0);
278 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
279 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
280 dom = isl_set_universe(space);
281 dom = isl_set_lower_bound_si(dom, isl_dim_param, 0, 5);
282 uset = isl_union_set_from_set(dom);
283 space = isl_space_set_alloc(ctx, 1, 0);
284 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
285 space = isl_space_set_tuple_name(space, isl_dim_set, "B");
286 mupa = isl_multi_union_pw_aff_zero(space);
287 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, uset);
288 r = check_reparse_mupa(ctx, mupa);
289 if (r < 0)
290 return -1;
292 for (i = 0; i < ARRAY_SIZE(parse_multi_mupa_tests); ++i) {
293 const char *str;
295 str = parse_multi_mupa_tests[i];
296 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
297 r = check_reparse_mupa(ctx, mupa);
298 if (r < 0)
299 return -1;
302 return 0;
305 /* Test parsing of multi expressions.
307 static int test_parse_multi(isl_ctx *ctx)
309 if (test_parse_mpa(ctx) < 0)
310 return -1;
311 if (test_parse_mupa(ctx) < 0)
312 return -1;
314 return 0;
317 /* Pairs of binary relation representations that should represent
318 * the same binary relations.
320 struct {
321 const char *map1;
322 const char *map2;
323 } parse_map_equal_tests[] = {
324 { "{ [x,y] : [([x/2]+y)/3] >= 1 }",
325 "{ [x, y] : 2y >= 6 - x }" },
326 { "{ [x,y] : x <= min(y, 2*y+3) }",
327 "{ [x,y] : x <= y, 2*y + 3 }" },
328 { "{ [x,y] : x >= min(y, 2*y+3) }",
329 "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }" },
330 { "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }",
331 "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }" },
332 { "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }",
333 "{ [i,j] -> [min(i,j)] }" },
334 { "{ [i,j] : i != j }",
335 "{ [i,j] : i < j or i > j }" },
336 { "{ [i,j] : (i+1)*2 >= j }",
337 "{ [i, j] : j <= 2 + 2i }" },
338 { "{ [i] -> [i > 0 ? 4 : 5] }",
339 "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }" },
340 { "[N=2,M] -> { [i=[(M+N)/4]] }",
341 "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }" },
342 { "{ [x] : x >= 0 }",
343 "{ [x] : x-0 >= 0 }" },
344 { "{ [i] : ((i > 10)) }",
345 "{ [i] : i >= 11 }" },
346 { "{ [i] -> [0] }",
347 "{ [i] -> [0 * i] }" },
348 { "{ [a] -> [b] : (not false) }",
349 "{ [a] -> [b] : true }" },
350 { "{ [i] : i/2 <= 5 }",
351 "{ [i] : i <= 10 }" },
352 { "{Sym=[n] [i] : i <= n }",
353 "[n] -> { [i] : i <= n }" },
354 { "{ [*] }",
355 "{ [a] }" },
356 { "{ [i] : 2*floor(i/2) = i }",
357 "{ [i] : exists a : i = 2 a }" },
358 { "{ [a] -> [b] : a = 5 implies b = 5 }",
359 "{ [a] -> [b] : a != 5 or b = 5 }" },
360 { "{ [a] -> [a - 1 : a > 0] }",
361 "{ [a] -> [a - 1] : a > 0 }" },
362 { "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
363 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }" },
364 { "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
365 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
366 { "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
367 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
368 { "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
369 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
370 { "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
371 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
372 { "{ [a,b] -> [i,j] : a,b << i,j }",
373 "{ [a,b] -> [i,j] : a < i or (a = i and b < j) }" },
374 { "{ [a,b] -> [i,j] : a,b <<= i,j }",
375 "{ [a,b] -> [i,j] : a < i or (a = i and b <= j) }" },
376 { "{ [a,b] -> [i,j] : a,b >> i,j }",
377 "{ [a,b] -> [i,j] : a > i or (a = i and b > j) }" },
378 { "{ [a,b] -> [i,j] : a,b >>= i,j }",
379 "{ [a,b] -> [i,j] : a > i or (a = i and b >= j) }" },
380 { "{ [n] -> [i] : exists (a, b, c: 8b <= i - 32a and "
381 "8b >= -7 + i - 32 a and b >= 0 and b <= 3 and "
382 "8c < n - 32a and i < n and c >= 0 and "
383 "c <= 3 and c >= -4a) }",
384 "{ [n] -> [i] : 0 <= i < n }" },
385 { "{ [x] -> [] : exists (a, b: 0 <= a <= 1 and 0 <= b <= 3 and "
386 "2b <= x - 8a and 2b >= -1 + x - 8a) }",
387 "{ [x] -> [] : 0 <= x <= 15 }" },
388 { "{ [x] -> [x] : }",
389 "{ [x] -> [x] }" },
392 int test_parse(struct isl_ctx *ctx)
394 int i;
395 isl_map *map, *map2;
396 const char *str, *str2;
398 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
399 return -1;
400 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
401 return -1;
402 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
403 return -1;
404 if (test_parse_multi(ctx) < 0)
405 return -1;
407 str = "{ [i] -> [-i] }";
408 map = isl_map_read_from_str(ctx, str);
409 assert(map);
410 isl_map_free(map);
412 str = "{ A[i] -> L[([i/3])] }";
413 map = isl_map_read_from_str(ctx, str);
414 assert(map);
415 isl_map_free(map);
417 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
418 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
419 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
421 for (i = 0; i < ARRAY_SIZE(parse_map_equal_tests); ++i) {
422 str = parse_map_equal_tests[i].map1;
423 str2 = parse_map_equal_tests[i].map2;
424 if (test_parse_map_equal(ctx, str, str2) < 0)
425 return -1;
428 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
429 map = isl_map_read_from_str(ctx, str);
430 str = "{ [new, old] -> [o0, o1] : "
431 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
432 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
433 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
434 map2 = isl_map_read_from_str(ctx, str);
435 assert(isl_map_is_equal(map, map2));
436 isl_map_free(map);
437 isl_map_free(map2);
439 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
440 map = isl_map_read_from_str(ctx, str);
441 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
442 map2 = isl_map_read_from_str(ctx, str);
443 assert(isl_map_is_equal(map, map2));
444 isl_map_free(map);
445 isl_map_free(map2);
447 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
448 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
449 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
450 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
451 test_parse_pwaff(ctx, "{ [] -> [(100)] }");
453 return 0;
456 static int test_read(isl_ctx *ctx)
458 char *filename;
459 FILE *input;
460 isl_basic_set *bset1, *bset2;
461 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
462 int equal;
464 filename = get_filename(ctx, "set", "omega");
465 assert(filename);
466 input = fopen(filename, "r");
467 assert(input);
469 bset1 = isl_basic_set_read_from_file(ctx, input);
470 bset2 = isl_basic_set_read_from_str(ctx, str);
472 equal = isl_basic_set_is_equal(bset1, bset2);
474 isl_basic_set_free(bset1);
475 isl_basic_set_free(bset2);
476 free(filename);
478 fclose(input);
480 if (equal < 0)
481 return -1;
482 if (!equal)
483 isl_die(ctx, isl_error_unknown,
484 "read sets not equal", return -1);
486 return 0;
489 static int test_bounded(isl_ctx *ctx)
491 isl_set *set;
492 isl_bool bounded;
494 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
495 bounded = isl_set_is_bounded(set);
496 isl_set_free(set);
498 if (bounded < 0)
499 return -1;
500 if (!bounded)
501 isl_die(ctx, isl_error_unknown,
502 "set not considered bounded", return -1);
504 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
505 bounded = isl_set_is_bounded(set);
506 assert(!bounded);
507 isl_set_free(set);
509 if (bounded < 0)
510 return -1;
511 if (bounded)
512 isl_die(ctx, isl_error_unknown,
513 "set considered bounded", return -1);
515 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
516 bounded = isl_set_is_bounded(set);
517 isl_set_free(set);
519 if (bounded < 0)
520 return -1;
521 if (bounded)
522 isl_die(ctx, isl_error_unknown,
523 "set considered bounded", return -1);
525 return 0;
528 /* Construct the basic set { [i] : 5 <= i <= N } */
529 static int test_construction_1(isl_ctx *ctx)
531 isl_space *dim;
532 isl_local_space *ls;
533 isl_basic_set *bset;
534 isl_constraint *c;
536 dim = isl_space_set_alloc(ctx, 1, 1);
537 bset = isl_basic_set_universe(isl_space_copy(dim));
538 ls = isl_local_space_from_space(dim);
540 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
541 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
542 c = isl_constraint_set_coefficient_si(c, isl_dim_param, 0, 1);
543 bset = isl_basic_set_add_constraint(bset, c);
545 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
546 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
547 c = isl_constraint_set_constant_si(c, -5);
548 bset = isl_basic_set_add_constraint(bset, c);
550 isl_local_space_free(ls);
551 isl_basic_set_free(bset);
553 return 0;
556 /* Construct the basic set { [x] : -100 <= x <= 100 }
557 * using isl_basic_set_{lower,upper}_bound_val and
558 * check that it is equal the same basic set parsed from a string.
560 static int test_construction_2(isl_ctx *ctx)
562 isl_bool equal;
563 isl_val *v;
564 isl_space *space;
565 isl_basic_set *bset1, *bset2;
567 v = isl_val_int_from_si(ctx, 100);
568 space = isl_space_set_alloc(ctx, 0, 1);
569 bset1 = isl_basic_set_universe(space);
570 bset1 = isl_basic_set_upper_bound_val(bset1, isl_dim_set, 0,
571 isl_val_copy(v));
572 bset1 = isl_basic_set_lower_bound_val(bset1, isl_dim_set, 0,
573 isl_val_neg(v));
574 bset2 = isl_basic_set_read_from_str(ctx, "{ [x] : -100 <= x <= 100 }");
575 equal = isl_basic_set_is_equal(bset1, bset2);
576 isl_basic_set_free(bset1);
577 isl_basic_set_free(bset2);
579 if (equal < 0)
580 return -1;
581 if (!equal)
582 isl_die(ctx, isl_error_unknown,
583 "failed construction", return -1);
585 return 0;
588 /* Basic tests for constructing basic sets.
590 static int test_construction(isl_ctx *ctx)
592 if (test_construction_1(ctx) < 0)
593 return -1;
594 if (test_construction_2(ctx) < 0)
595 return -1;
596 return 0;
599 static int test_dim(isl_ctx *ctx)
601 const char *str;
602 isl_map *map1, *map2;
603 int equal;
605 map1 = isl_map_read_from_str(ctx,
606 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
607 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
608 map2 = isl_map_read_from_str(ctx,
609 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
610 equal = isl_map_is_equal(map1, map2);
611 isl_map_free(map2);
613 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
614 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
615 if (equal >= 0 && equal)
616 equal = isl_map_is_equal(map1, map2);
618 isl_map_free(map1);
619 isl_map_free(map2);
621 if (equal < 0)
622 return -1;
623 if (!equal)
624 isl_die(ctx, isl_error_unknown,
625 "unexpected result", return -1);
627 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
628 map1 = isl_map_read_from_str(ctx, str);
629 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
630 map2 = isl_map_read_from_str(ctx, str);
631 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
632 equal = isl_map_is_equal(map1, map2);
633 isl_map_free(map1);
634 isl_map_free(map2);
636 if (equal < 0)
637 return -1;
638 if (!equal)
639 isl_die(ctx, isl_error_unknown,
640 "unexpected result", return -1);
642 return 0;
645 /* Check that "val" is equal to the value described by "str".
646 * If "str" is "NaN", then check for a NaN value explicitly.
648 static isl_stat val_check_equal(__isl_keep isl_val *val, const char *str)
650 isl_bool ok, is_nan;
651 isl_ctx *ctx;
652 isl_val *res;
654 if (!val)
655 return isl_stat_error;
657 ctx = isl_val_get_ctx(val);
658 res = isl_val_read_from_str(ctx, str);
659 is_nan = isl_val_is_nan(res);
660 if (is_nan < 0)
661 ok = isl_bool_error;
662 else if (is_nan)
663 ok = isl_val_is_nan(val);
664 else
665 ok = isl_val_eq(val, res);
666 isl_val_free(res);
667 if (ok < 0)
668 return isl_stat_error;
669 if (!ok)
670 isl_die(ctx, isl_error_unknown,
671 "unexpected result", return isl_stat_error);
672 return isl_stat_ok;
675 struct {
676 __isl_give isl_val *(*op)(__isl_take isl_val *v);
677 const char *arg;
678 const char *res;
679 } val_un_tests[] = {
680 { &isl_val_neg, "0", "0" },
681 { &isl_val_abs, "0", "0" },
682 { &isl_val_pow2, "0", "1" },
683 { &isl_val_floor, "0", "0" },
684 { &isl_val_ceil, "0", "0" },
685 { &isl_val_neg, "1", "-1" },
686 { &isl_val_neg, "-1", "1" },
687 { &isl_val_neg, "1/2", "-1/2" },
688 { &isl_val_neg, "-1/2", "1/2" },
689 { &isl_val_neg, "infty", "-infty" },
690 { &isl_val_neg, "-infty", "infty" },
691 { &isl_val_neg, "NaN", "NaN" },
692 { &isl_val_abs, "1", "1" },
693 { &isl_val_abs, "-1", "1" },
694 { &isl_val_abs, "1/2", "1/2" },
695 { &isl_val_abs, "-1/2", "1/2" },
696 { &isl_val_abs, "infty", "infty" },
697 { &isl_val_abs, "-infty", "infty" },
698 { &isl_val_abs, "NaN", "NaN" },
699 { &isl_val_floor, "1", "1" },
700 { &isl_val_floor, "-1", "-1" },
701 { &isl_val_floor, "1/2", "0" },
702 { &isl_val_floor, "-1/2", "-1" },
703 { &isl_val_floor, "infty", "infty" },
704 { &isl_val_floor, "-infty", "-infty" },
705 { &isl_val_floor, "NaN", "NaN" },
706 { &isl_val_ceil, "1", "1" },
707 { &isl_val_ceil, "-1", "-1" },
708 { &isl_val_ceil, "1/2", "1" },
709 { &isl_val_ceil, "-1/2", "0" },
710 { &isl_val_ceil, "infty", "infty" },
711 { &isl_val_ceil, "-infty", "-infty" },
712 { &isl_val_ceil, "NaN", "NaN" },
713 { &isl_val_pow2, "-3", "1/8" },
714 { &isl_val_pow2, "-1", "1/2" },
715 { &isl_val_pow2, "1", "2" },
716 { &isl_val_pow2, "2", "4" },
717 { &isl_val_pow2, "3", "8" },
718 { &isl_val_inv, "1", "1" },
719 { &isl_val_inv, "2", "1/2" },
720 { &isl_val_inv, "1/2", "2" },
721 { &isl_val_inv, "-2", "-1/2" },
722 { &isl_val_inv, "-1/2", "-2" },
723 { &isl_val_inv, "0", "NaN" },
724 { &isl_val_inv, "NaN", "NaN" },
725 { &isl_val_inv, "infty", "0" },
726 { &isl_val_inv, "-infty", "0" },
729 /* Perform some basic tests of unary operations on isl_val objects.
731 static int test_un_val(isl_ctx *ctx)
733 int i;
734 isl_val *v;
735 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
737 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
738 isl_stat r;
740 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
741 fn = val_un_tests[i].op;
742 v = fn(v);
743 r = val_check_equal(v, val_un_tests[i].res);
744 isl_val_free(v);
745 if (r < 0)
746 return -1;
749 return 0;
752 struct {
753 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
754 __isl_take isl_val *v2);
755 } val_bin_op[] = {
756 ['+'] = { &isl_val_add },
757 ['-'] = { &isl_val_sub },
758 ['*'] = { &isl_val_mul },
759 ['/'] = { &isl_val_div },
760 ['g'] = { &isl_val_gcd },
761 ['m'] = { &isl_val_min },
762 ['M'] = { &isl_val_max },
765 struct {
766 const char *arg1;
767 unsigned char op;
768 const char *arg2;
769 const char *res;
770 } val_bin_tests[] = {
771 { "0", '+', "0", "0" },
772 { "1", '+', "0", "1" },
773 { "1", '+', "1", "2" },
774 { "1", '-', "1", "0" },
775 { "1", '*', "1", "1" },
776 { "1", '/', "1", "1" },
777 { "2", '*', "3", "6" },
778 { "2", '*', "1/2", "1" },
779 { "2", '*', "1/3", "2/3" },
780 { "2/3", '*', "3/5", "2/5" },
781 { "2/3", '*', "7/5", "14/15" },
782 { "2", '/', "1/2", "4" },
783 { "-2", '/', "-1/2", "4" },
784 { "-2", '/', "1/2", "-4" },
785 { "2", '/', "-1/2", "-4" },
786 { "2", '/', "2", "1" },
787 { "2", '/', "3", "2/3" },
788 { "2/3", '/', "5/3", "2/5" },
789 { "2/3", '/', "5/7", "14/15" },
790 { "0", '/', "0", "NaN" },
791 { "42", '/', "0", "NaN" },
792 { "-42", '/', "0", "NaN" },
793 { "infty", '/', "0", "NaN" },
794 { "-infty", '/', "0", "NaN" },
795 { "NaN", '/', "0", "NaN" },
796 { "0", '/', "NaN", "NaN" },
797 { "42", '/', "NaN", "NaN" },
798 { "-42", '/', "NaN", "NaN" },
799 { "infty", '/', "NaN", "NaN" },
800 { "-infty", '/', "NaN", "NaN" },
801 { "NaN", '/', "NaN", "NaN" },
802 { "0", '/', "infty", "0" },
803 { "42", '/', "infty", "0" },
804 { "-42", '/', "infty", "0" },
805 { "infty", '/', "infty", "NaN" },
806 { "-infty", '/', "infty", "NaN" },
807 { "NaN", '/', "infty", "NaN" },
808 { "0", '/', "-infty", "0" },
809 { "42", '/', "-infty", "0" },
810 { "-42", '/', "-infty", "0" },
811 { "infty", '/', "-infty", "NaN" },
812 { "-infty", '/', "-infty", "NaN" },
813 { "NaN", '/', "-infty", "NaN" },
814 { "1", '-', "1/3", "2/3" },
815 { "1/3", '+', "1/2", "5/6" },
816 { "1/2", '+', "1/2", "1" },
817 { "3/4", '-', "1/4", "1/2" },
818 { "1/2", '-', "1/3", "1/6" },
819 { "infty", '+', "42", "infty" },
820 { "infty", '+', "infty", "infty" },
821 { "42", '+', "infty", "infty" },
822 { "infty", '-', "infty", "NaN" },
823 { "infty", '*', "infty", "infty" },
824 { "infty", '*', "-infty", "-infty" },
825 { "-infty", '*', "infty", "-infty" },
826 { "-infty", '*', "-infty", "infty" },
827 { "0", '*', "infty", "NaN" },
828 { "1", '*', "infty", "infty" },
829 { "infty", '*', "0", "NaN" },
830 { "infty", '*', "42", "infty" },
831 { "42", '-', "infty", "-infty" },
832 { "infty", '+', "-infty", "NaN" },
833 { "4", 'g', "6", "2" },
834 { "5", 'g', "6", "1" },
835 { "42", 'm', "3", "3" },
836 { "42", 'M', "3", "42" },
837 { "3", 'm', "42", "3" },
838 { "3", 'M', "42", "42" },
839 { "42", 'm', "infty", "42" },
840 { "42", 'M', "infty", "infty" },
841 { "42", 'm', "-infty", "-infty" },
842 { "42", 'M', "-infty", "42" },
843 { "42", 'm', "NaN", "NaN" },
844 { "42", 'M', "NaN", "NaN" },
845 { "infty", 'm', "-infty", "-infty" },
846 { "infty", 'M', "-infty", "infty" },
849 /* Perform some basic tests of binary operations on isl_val objects.
851 static int test_bin_val(isl_ctx *ctx)
853 int i;
854 isl_val *v1, *v2, *res;
855 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
856 __isl_take isl_val *v2);
857 int ok;
859 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
860 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
861 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
862 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
863 fn = val_bin_op[val_bin_tests[i].op].fn;
864 v1 = fn(v1, v2);
865 if (isl_val_is_nan(res))
866 ok = isl_val_is_nan(v1);
867 else
868 ok = isl_val_eq(v1, res);
869 isl_val_free(v1);
870 isl_val_free(res);
871 if (ok < 0)
872 return -1;
873 if (!ok)
874 isl_die(ctx, isl_error_unknown,
875 "unexpected result", return -1);
878 return 0;
881 /* Perform some basic tests on isl_val objects.
883 static int test_val(isl_ctx *ctx)
885 if (test_un_val(ctx) < 0)
886 return -1;
887 if (test_bin_val(ctx) < 0)
888 return -1;
889 return 0;
892 /* Sets described using existentially quantified variables that
893 * can also be described without.
895 static const char *elimination_tests[] = {
896 "{ [i,j] : 2 * [i/2] + 3 * [j/4] <= 10 and 2 i = j }",
897 "{ [m, w] : exists a : w - 2m - 5 <= 3a <= m - 2w }",
898 "{ [m, w] : exists a : w >= 0 and a < m and -1 + w <= a <= 2m - w }",
901 /* Check that redundant existentially quantified variables are
902 * getting removed.
904 static int test_elimination(isl_ctx *ctx)
906 int i;
907 unsigned n;
908 isl_basic_set *bset;
910 for (i = 0; i < ARRAY_SIZE(elimination_tests); ++i) {
911 bset = isl_basic_set_read_from_str(ctx, elimination_tests[i]);
912 n = isl_basic_set_dim(bset, isl_dim_div);
913 isl_basic_set_free(bset);
914 if (!bset)
915 return -1;
916 if (n != 0)
917 isl_die(ctx, isl_error_unknown,
918 "expecting no existentials", return -1);
921 return 0;
924 static int test_div(isl_ctx *ctx)
926 const char *str;
927 int empty;
928 isl_space *dim;
929 isl_set *set;
930 isl_local_space *ls;
931 struct isl_basic_set *bset;
932 struct isl_constraint *c;
934 /* test 1 */
935 dim = isl_space_set_alloc(ctx, 0, 3);
936 bset = isl_basic_set_universe(isl_space_copy(dim));
937 ls = isl_local_space_from_space(dim);
939 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
940 c = isl_constraint_set_constant_si(c, -1);
941 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
942 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
943 bset = isl_basic_set_add_constraint(bset, c);
945 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
946 c = isl_constraint_set_constant_si(c, 1);
947 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
948 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
949 bset = isl_basic_set_add_constraint(bset, c);
951 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
953 assert(bset && bset->n_div == 1);
954 isl_local_space_free(ls);
955 isl_basic_set_free(bset);
957 /* test 2 */
958 dim = isl_space_set_alloc(ctx, 0, 3);
959 bset = isl_basic_set_universe(isl_space_copy(dim));
960 ls = isl_local_space_from_space(dim);
962 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
963 c = isl_constraint_set_constant_si(c, 1);
964 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
965 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
966 bset = isl_basic_set_add_constraint(bset, c);
968 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
969 c = isl_constraint_set_constant_si(c, -1);
970 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
971 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
972 bset = isl_basic_set_add_constraint(bset, c);
974 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
976 assert(bset && bset->n_div == 1);
977 isl_local_space_free(ls);
978 isl_basic_set_free(bset);
980 /* test 3 */
981 dim = isl_space_set_alloc(ctx, 0, 3);
982 bset = isl_basic_set_universe(isl_space_copy(dim));
983 ls = isl_local_space_from_space(dim);
985 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
986 c = isl_constraint_set_constant_si(c, 1);
987 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
988 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
989 bset = isl_basic_set_add_constraint(bset, c);
991 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
992 c = isl_constraint_set_constant_si(c, -3);
993 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
994 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 4);
995 bset = isl_basic_set_add_constraint(bset, c);
997 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
999 assert(bset && bset->n_div == 1);
1000 isl_local_space_free(ls);
1001 isl_basic_set_free(bset);
1003 /* test 4 */
1004 dim = isl_space_set_alloc(ctx, 0, 3);
1005 bset = isl_basic_set_universe(isl_space_copy(dim));
1006 ls = isl_local_space_from_space(dim);
1008 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1009 c = isl_constraint_set_constant_si(c, 2);
1010 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1011 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1012 bset = isl_basic_set_add_constraint(bset, c);
1014 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1015 c = isl_constraint_set_constant_si(c, -1);
1016 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1017 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
1018 bset = isl_basic_set_add_constraint(bset, c);
1020 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1022 assert(isl_basic_set_is_empty(bset));
1023 isl_local_space_free(ls);
1024 isl_basic_set_free(bset);
1026 /* test 5 */
1027 dim = isl_space_set_alloc(ctx, 0, 3);
1028 bset = isl_basic_set_universe(isl_space_copy(dim));
1029 ls = isl_local_space_from_space(dim);
1031 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1032 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1033 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
1034 bset = isl_basic_set_add_constraint(bset, c);
1036 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1037 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1038 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1039 bset = isl_basic_set_add_constraint(bset, c);
1041 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1043 assert(bset && bset->n_div == 0);
1044 isl_basic_set_free(bset);
1045 isl_local_space_free(ls);
1047 /* test 6 */
1048 dim = isl_space_set_alloc(ctx, 0, 3);
1049 bset = isl_basic_set_universe(isl_space_copy(dim));
1050 ls = isl_local_space_from_space(dim);
1052 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1053 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1054 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
1055 bset = isl_basic_set_add_constraint(bset, c);
1057 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1058 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1059 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1060 bset = isl_basic_set_add_constraint(bset, c);
1062 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1064 assert(bset && bset->n_div == 1);
1065 isl_basic_set_free(bset);
1066 isl_local_space_free(ls);
1068 /* test 7 */
1069 /* This test is a bit tricky. We set up an equality
1070 * a + 3b + 3c = 6 e0
1071 * Normalization of divs creates _two_ divs
1072 * a = 3 e0
1073 * c - b - e0 = 2 e1
1074 * Afterwards e0 is removed again because it has coefficient -1
1075 * and we end up with the original equality and div again.
1076 * Perhaps we can avoid the introduction of this temporary div.
1078 dim = isl_space_set_alloc(ctx, 0, 4);
1079 bset = isl_basic_set_universe(isl_space_copy(dim));
1080 ls = isl_local_space_from_space(dim);
1082 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1083 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1084 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1085 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -3);
1086 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 6);
1087 bset = isl_basic_set_add_constraint(bset, c);
1089 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
1091 /* Test disabled for now */
1093 assert(bset && bset->n_div == 1);
1095 isl_local_space_free(ls);
1096 isl_basic_set_free(bset);
1098 /* test 8 */
1099 dim = isl_space_set_alloc(ctx, 0, 5);
1100 bset = isl_basic_set_universe(isl_space_copy(dim));
1101 ls = isl_local_space_from_space(dim);
1103 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1104 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1105 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1106 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, -3);
1107 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 4, 6);
1108 bset = isl_basic_set_add_constraint(bset, c);
1110 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1111 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1112 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 1);
1113 c = isl_constraint_set_constant_si(c, 1);
1114 bset = isl_basic_set_add_constraint(bset, c);
1116 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
1118 /* Test disabled for now */
1120 assert(bset && bset->n_div == 1);
1122 isl_local_space_free(ls);
1123 isl_basic_set_free(bset);
1125 /* test 9 */
1126 dim = isl_space_set_alloc(ctx, 0, 4);
1127 bset = isl_basic_set_universe(isl_space_copy(dim));
1128 ls = isl_local_space_from_space(dim);
1130 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1131 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1132 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -1);
1133 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1134 bset = isl_basic_set_add_constraint(bset, c);
1136 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1137 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1138 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 3);
1139 c = isl_constraint_set_constant_si(c, 2);
1140 bset = isl_basic_set_add_constraint(bset, c);
1142 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
1144 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1146 assert(!isl_basic_set_is_empty(bset));
1148 isl_local_space_free(ls);
1149 isl_basic_set_free(bset);
1151 /* test 10 */
1152 dim = isl_space_set_alloc(ctx, 0, 3);
1153 bset = isl_basic_set_universe(isl_space_copy(dim));
1154 ls = isl_local_space_from_space(dim);
1156 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1157 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1158 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1159 bset = isl_basic_set_add_constraint(bset, c);
1161 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1163 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1165 isl_local_space_free(ls);
1166 isl_basic_set_free(bset);
1168 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1169 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
1170 set = isl_set_read_from_str(ctx, str);
1171 set = isl_set_compute_divs(set);
1172 isl_set_free(set);
1173 if (!set)
1174 return -1;
1176 if (test_elimination(ctx) < 0)
1177 return -1;
1179 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
1180 set = isl_set_read_from_str(ctx, str);
1181 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
1182 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
1183 empty = isl_set_is_empty(set);
1184 isl_set_free(set);
1185 if (empty < 0)
1186 return -1;
1187 if (!empty)
1188 isl_die(ctx, isl_error_unknown,
1189 "result not as accurate as expected", return -1);
1191 return 0;
1194 void test_application_case(struct isl_ctx *ctx, const char *name)
1196 char *filename;
1197 FILE *input;
1198 struct isl_basic_set *bset1, *bset2;
1199 struct isl_basic_map *bmap;
1201 filename = get_filename(ctx, name, "omega");
1202 assert(filename);
1203 input = fopen(filename, "r");
1204 assert(input);
1206 bset1 = isl_basic_set_read_from_file(ctx, input);
1207 bmap = isl_basic_map_read_from_file(ctx, input);
1209 bset1 = isl_basic_set_apply(bset1, bmap);
1211 bset2 = isl_basic_set_read_from_file(ctx, input);
1213 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1215 isl_basic_set_free(bset1);
1216 isl_basic_set_free(bset2);
1217 free(filename);
1219 fclose(input);
1222 static int test_application(isl_ctx *ctx)
1224 test_application_case(ctx, "application");
1225 test_application_case(ctx, "application2");
1227 return 0;
1230 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1232 char *filename;
1233 FILE *input;
1234 struct isl_basic_set *bset1, *bset2;
1236 filename = get_filename(ctx, name, "polylib");
1237 assert(filename);
1238 input = fopen(filename, "r");
1239 assert(input);
1241 bset1 = isl_basic_set_read_from_file(ctx, input);
1242 bset2 = isl_basic_set_read_from_file(ctx, input);
1244 bset1 = isl_basic_set_affine_hull(bset1);
1246 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1248 isl_basic_set_free(bset1);
1249 isl_basic_set_free(bset2);
1250 free(filename);
1252 fclose(input);
1255 int test_affine_hull(struct isl_ctx *ctx)
1257 const char *str;
1258 isl_set *set;
1259 isl_basic_set *bset, *bset2;
1260 int n;
1261 isl_bool subset;
1263 test_affine_hull_case(ctx, "affine2");
1264 test_affine_hull_case(ctx, "affine");
1265 test_affine_hull_case(ctx, "affine3");
1267 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1268 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1269 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1270 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1271 set = isl_set_read_from_str(ctx, str);
1272 bset = isl_set_affine_hull(set);
1273 n = isl_basic_set_dim(bset, isl_dim_div);
1274 isl_basic_set_free(bset);
1275 if (n != 0)
1276 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1277 return -1);
1279 /* Check that isl_map_affine_hull is not confused by
1280 * the reordering of divs in isl_map_align_divs.
1282 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1283 "32e0 = b and 32e1 = c); "
1284 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1285 set = isl_set_read_from_str(ctx, str);
1286 bset = isl_set_affine_hull(set);
1287 isl_basic_set_free(bset);
1288 if (!bset)
1289 return -1;
1291 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1292 "32e2 = 31 + 31e0 }";
1293 set = isl_set_read_from_str(ctx, str);
1294 bset = isl_set_affine_hull(set);
1295 str = "{ [a] : exists e : a = 32 e }";
1296 bset2 = isl_basic_set_read_from_str(ctx, str);
1297 subset = isl_basic_set_is_subset(bset, bset2);
1298 isl_basic_set_free(bset);
1299 isl_basic_set_free(bset2);
1300 if (subset < 0)
1301 return -1;
1302 if (!subset)
1303 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1304 return -1);
1306 return 0;
1309 /* Pairs of maps and the corresponding expected results of
1310 * isl_map_plain_unshifted_simple_hull.
1312 struct {
1313 const char *map;
1314 const char *hull;
1315 } plain_unshifted_simple_hull_tests[] = {
1316 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1317 "{ [i] -> [j] : i >= 1 }" },
1318 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1319 "(j mod 4 = 2 and k mod 6 = n) }",
1320 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1323 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1325 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1327 int i;
1328 isl_map *map;
1329 isl_basic_map *hull, *expected;
1330 isl_bool equal;
1332 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1333 const char *str;
1334 str = plain_unshifted_simple_hull_tests[i].map;
1335 map = isl_map_read_from_str(ctx, str);
1336 str = plain_unshifted_simple_hull_tests[i].hull;
1337 expected = isl_basic_map_read_from_str(ctx, str);
1338 hull = isl_map_plain_unshifted_simple_hull(map);
1339 equal = isl_basic_map_is_equal(hull, expected);
1340 isl_basic_map_free(hull);
1341 isl_basic_map_free(expected);
1342 if (equal < 0)
1343 return -1;
1344 if (!equal)
1345 isl_die(ctx, isl_error_unknown, "unexpected hull",
1346 return -1);
1349 return 0;
1352 /* Pairs of sets and the corresponding expected results of
1353 * isl_set_unshifted_simple_hull.
1355 struct {
1356 const char *set;
1357 const char *hull;
1358 } unshifted_simple_hull_tests[] = {
1359 { "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1360 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1363 /* Basic tests for isl_set_unshifted_simple_hull.
1365 static int test_unshifted_simple_hull(isl_ctx *ctx)
1367 int i;
1368 isl_set *set;
1369 isl_basic_set *hull, *expected;
1370 isl_bool equal;
1372 for (i = 0; i < ARRAY_SIZE(unshifted_simple_hull_tests); ++i) {
1373 const char *str;
1374 str = unshifted_simple_hull_tests[i].set;
1375 set = isl_set_read_from_str(ctx, str);
1376 str = unshifted_simple_hull_tests[i].hull;
1377 expected = isl_basic_set_read_from_str(ctx, str);
1378 hull = isl_set_unshifted_simple_hull(set);
1379 equal = isl_basic_set_is_equal(hull, expected);
1380 isl_basic_set_free(hull);
1381 isl_basic_set_free(expected);
1382 if (equal < 0)
1383 return -1;
1384 if (!equal)
1385 isl_die(ctx, isl_error_unknown, "unexpected hull",
1386 return -1);
1389 return 0;
1392 static int test_simple_hull(struct isl_ctx *ctx)
1394 const char *str;
1395 isl_set *set;
1396 isl_basic_set *bset;
1397 isl_bool is_empty;
1399 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1400 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1401 set = isl_set_read_from_str(ctx, str);
1402 bset = isl_set_simple_hull(set);
1403 is_empty = isl_basic_set_is_empty(bset);
1404 isl_basic_set_free(bset);
1406 if (is_empty == isl_bool_error)
1407 return -1;
1409 if (is_empty == isl_bool_false)
1410 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1411 return -1);
1413 if (test_plain_unshifted_simple_hull(ctx) < 0)
1414 return -1;
1415 if (test_unshifted_simple_hull(ctx) < 0)
1416 return -1;
1418 return 0;
1421 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1423 char *filename;
1424 FILE *input;
1425 struct isl_basic_set *bset1, *bset2;
1426 struct isl_set *set;
1428 filename = get_filename(ctx, name, "polylib");
1429 assert(filename);
1430 input = fopen(filename, "r");
1431 assert(input);
1433 bset1 = isl_basic_set_read_from_file(ctx, input);
1434 bset2 = isl_basic_set_read_from_file(ctx, input);
1436 set = isl_basic_set_union(bset1, bset2);
1437 bset1 = isl_set_convex_hull(set);
1439 bset2 = isl_basic_set_read_from_file(ctx, input);
1441 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1443 isl_basic_set_free(bset1);
1444 isl_basic_set_free(bset2);
1445 free(filename);
1447 fclose(input);
1450 struct {
1451 const char *set;
1452 const char *hull;
1453 } convex_hull_tests[] = {
1454 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1455 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1456 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1457 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1458 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1459 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1460 "i2 <= 5 and i2 >= 4; "
1461 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1462 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1463 "i2 <= 5 + i0 and i2 >= i0 }" },
1464 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1465 "{ [x, y] : 1 = 0 }" },
1466 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1467 "[x, y, 0] : x >= 0 and y < 0 }",
1468 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1471 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1473 int i;
1474 int orig_convex = ctx->opt->convex;
1475 ctx->opt->convex = convex;
1477 test_convex_hull_case(ctx, "convex0");
1478 test_convex_hull_case(ctx, "convex1");
1479 test_convex_hull_case(ctx, "convex2");
1480 test_convex_hull_case(ctx, "convex3");
1481 test_convex_hull_case(ctx, "convex4");
1482 test_convex_hull_case(ctx, "convex5");
1483 test_convex_hull_case(ctx, "convex6");
1484 test_convex_hull_case(ctx, "convex7");
1485 test_convex_hull_case(ctx, "convex8");
1486 test_convex_hull_case(ctx, "convex9");
1487 test_convex_hull_case(ctx, "convex10");
1488 test_convex_hull_case(ctx, "convex11");
1489 test_convex_hull_case(ctx, "convex12");
1490 test_convex_hull_case(ctx, "convex13");
1491 test_convex_hull_case(ctx, "convex14");
1492 test_convex_hull_case(ctx, "convex15");
1494 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1495 isl_set *set1, *set2;
1496 int equal;
1498 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1499 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1500 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1501 equal = isl_set_is_equal(set1, set2);
1502 isl_set_free(set1);
1503 isl_set_free(set2);
1505 if (equal < 0)
1506 return -1;
1507 if (!equal)
1508 isl_die(ctx, isl_error_unknown,
1509 "unexpected convex hull", return -1);
1512 ctx->opt->convex = orig_convex;
1514 return 0;
1517 static int test_convex_hull(isl_ctx *ctx)
1519 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1520 return -1;
1521 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1522 return -1;
1523 return 0;
1526 void test_gist_case(struct isl_ctx *ctx, const char *name)
1528 char *filename;
1529 FILE *input;
1530 struct isl_basic_set *bset1, *bset2;
1532 filename = get_filename(ctx, name, "polylib");
1533 assert(filename);
1534 input = fopen(filename, "r");
1535 assert(input);
1537 bset1 = isl_basic_set_read_from_file(ctx, input);
1538 bset2 = isl_basic_set_read_from_file(ctx, input);
1540 bset1 = isl_basic_set_gist(bset1, bset2);
1542 bset2 = isl_basic_set_read_from_file(ctx, input);
1544 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1546 isl_basic_set_free(bset1);
1547 isl_basic_set_free(bset2);
1548 free(filename);
1550 fclose(input);
1553 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1555 struct {
1556 const char *map;
1557 const char *context;
1558 const char *gist;
1559 } plain_gist_tests[] = {
1560 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1561 "{ [i] -> [j] : i >= 1 }",
1562 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1563 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1564 "(j mod 4 = 2 and k mod 6 = n) }",
1565 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1566 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1567 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1568 "{ [i] -> [j] : i > j }",
1569 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1572 /* Basic tests for isl_map_plain_gist_basic_map.
1574 static int test_plain_gist(isl_ctx *ctx)
1576 int i;
1578 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1579 const char *str;
1580 int equal;
1581 isl_map *map, *gist;
1582 isl_basic_map *context;
1584 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1585 str = plain_gist_tests[i].context;
1586 context = isl_basic_map_read_from_str(ctx, str);
1587 map = isl_map_plain_gist_basic_map(map, context);
1588 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1589 equal = isl_map_is_equal(map, gist);
1590 isl_map_free(map);
1591 isl_map_free(gist);
1592 if (equal < 0)
1593 return -1;
1594 if (!equal)
1595 isl_die(ctx, isl_error_unknown,
1596 "incorrect gist result", return -1);
1599 return 0;
1602 /* Inputs for isl_basic_set_gist tests that are expected to fail.
1604 struct {
1605 const char *set;
1606 const char *context;
1607 } gist_fail_tests[] = {
1608 { "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1609 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }",
1610 "{ [i] : i >= 0 }" },
1613 /* Check that isl_basic_set_gist fails (gracefully) when expected.
1614 * In particular, the user should be able to recover from the failure.
1616 static isl_stat test_gist_fail(struct isl_ctx *ctx)
1618 int i, n;
1619 int on_error;
1621 on_error = isl_options_get_on_error(ctx);
1622 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
1623 n = ARRAY_SIZE(gist_fail_tests);
1624 for (i = 0; i < n; ++i) {
1625 const char *str;
1626 isl_basic_set *bset, *context;
1628 bset = isl_basic_set_read_from_str(ctx, gist_fail_tests[i].set);
1629 str = gist_fail_tests[i].context;
1630 context = isl_basic_set_read_from_str(ctx, str);
1631 bset = isl_basic_set_gist(bset, context);
1632 isl_basic_set_free(bset);
1633 if (bset)
1634 break;
1636 isl_options_set_on_error(ctx, on_error);
1637 if (i < n)
1638 isl_die(ctx, isl_error_unknown,
1639 "operation not expected to succeed",
1640 return isl_stat_error);
1642 return isl_stat_ok;
1645 struct {
1646 const char *set;
1647 const char *context;
1648 const char *gist;
1649 } gist_tests[] = {
1650 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1651 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1652 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1653 "{ [a, b, c] : a <= 15 }" },
1654 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1655 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1656 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1657 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1658 { "{ [m, n, a, b] : a <= 2147 + n }",
1659 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1660 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1661 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1662 "b >= 0) }",
1663 "{ [m, n, ku, kl] }" },
1664 { "{ [a, a, b] : a >= 10 }",
1665 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1666 "{ [a, a, b] : a >= 10 }" },
1667 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1668 "{ [0, j] : j >= 0 }" },
1669 /* Check that no constraints on i6 are introduced in the gist */
1670 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1671 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1672 "5e0 <= 381 - t1 and i4 <= 1) }",
1673 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1674 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1675 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1676 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1677 "20e0 >= 1511 - 4t1 - 5i4) }" },
1678 /* Check that no constraints on i6 are introduced in the gist */
1679 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1680 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1681 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1682 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1683 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1684 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1685 "20e1 <= 1530 - 4t1 - 5i4 and "
1686 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1687 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1688 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1689 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1690 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1691 "10e0 <= -91 + 5i4 + 4i6 and "
1692 "10e0 >= -105 + 5i4 + 4i6) }",
1693 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1694 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1695 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1696 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1697 "{ [a, b, q, p] : b >= 1 + a }",
1698 "{ [a, b, q, p] : false }" },
1699 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1700 "[n] -> { [x] : x mod 32 = 0 }",
1701 "[n] -> { [x = n] }" },
1702 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1703 "{ [x] : x mod 2 = 0 }" },
1704 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1705 "{ [x] : x mod 128 = 0 }" },
1706 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1707 "{ [x] : x mod 3200 = 0 }" },
1708 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1709 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1710 "{ [a, b, c = a] }" },
1711 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1712 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1713 "{ [a, b, c = a] : a mod 3 = 0 }" },
1714 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1715 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1716 "{ [x] }" },
1717 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1718 "{ [x,y] : 1 <= y <= 3 }",
1719 "{ [x,y] }" },
1722 /* Check that isl_set_gist behaves as expected.
1724 * For the test cases in gist_tests, besides checking that the result
1725 * is as expected, also check that applying the gist operation does
1726 * not modify the input set (an earlier version of isl would do that) and
1727 * that the test case is consistent, i.e., that the gist has the same
1728 * intersection with the context as the input set.
1730 static int test_gist(struct isl_ctx *ctx)
1732 int i;
1733 const char *str;
1734 isl_basic_set *bset1, *bset2;
1735 isl_map *map1, *map2;
1736 isl_bool equal;
1738 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1739 isl_bool equal_input, equal_intersection;
1740 isl_set *set1, *set2, *copy, *context;
1742 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1743 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1744 copy = isl_set_copy(set1);
1745 set1 = isl_set_gist(set1, isl_set_copy(context));
1746 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1747 equal = isl_set_is_equal(set1, set2);
1748 isl_set_free(set1);
1749 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1750 equal_input = isl_set_is_equal(set1, copy);
1751 isl_set_free(copy);
1752 set1 = isl_set_intersect(set1, isl_set_copy(context));
1753 set2 = isl_set_intersect(set2, context);
1754 equal_intersection = isl_set_is_equal(set1, set2);
1755 isl_set_free(set2);
1756 isl_set_free(set1);
1757 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1758 return -1;
1759 if (!equal)
1760 isl_die(ctx, isl_error_unknown,
1761 "incorrect gist result", return -1);
1762 if (!equal_input)
1763 isl_die(ctx, isl_error_unknown,
1764 "gist modified input", return -1);
1765 if (!equal_input)
1766 isl_die(ctx, isl_error_unknown,
1767 "inconsistent gist test case", return -1);
1770 if (test_gist_fail(ctx) < 0)
1771 return -1;
1773 test_gist_case(ctx, "gist1");
1775 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1776 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1777 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1778 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1779 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1780 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1781 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1782 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1783 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1784 bset1 = isl_basic_set_read_from_str(ctx, str);
1785 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1786 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1787 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1788 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1789 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1790 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1791 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1792 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1793 bset2 = isl_basic_set_read_from_str(ctx, str);
1794 bset1 = isl_basic_set_gist(bset1, bset2);
1795 assert(bset1 && bset1->n_div == 0);
1796 isl_basic_set_free(bset1);
1798 /* Check that the integer divisions of the second disjunct
1799 * do not spread to the first disjunct.
1801 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1802 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1803 "(exists (e0 = [(-1 + t1)/16], "
1804 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1805 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1806 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1807 "o0 <= 4294967295 and t1 <= -1)) }";
1808 map1 = isl_map_read_from_str(ctx, str);
1809 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1810 map2 = isl_map_read_from_str(ctx, str);
1811 map1 = isl_map_gist(map1, map2);
1812 if (!map1)
1813 return -1;
1814 if (map1->n != 1)
1815 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1816 isl_map_free(map1); return -1);
1817 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1818 isl_die(ctx, isl_error_unknown, "expecting single div",
1819 isl_map_free(map1); return -1);
1820 isl_map_free(map1);
1822 if (test_plain_gist(ctx) < 0)
1823 return -1;
1825 return 0;
1828 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1830 isl_set *set, *set2;
1831 int equal;
1832 int one;
1834 set = isl_set_read_from_str(ctx, str);
1835 set = isl_set_coalesce(set);
1836 set2 = isl_set_read_from_str(ctx, str);
1837 equal = isl_set_is_equal(set, set2);
1838 one = set && set->n == 1;
1839 isl_set_free(set);
1840 isl_set_free(set2);
1842 if (equal < 0)
1843 return -1;
1844 if (!equal)
1845 isl_die(ctx, isl_error_unknown,
1846 "coalesced set not equal to input", return -1);
1847 if (check_one && !one)
1848 isl_die(ctx, isl_error_unknown,
1849 "coalesced set should not be a union", return -1);
1851 return 0;
1854 /* Inputs for coalescing tests with unbounded wrapping.
1855 * "str" is a string representation of the input set.
1856 * "single_disjunct" is set if we expect the result to consist of
1857 * a single disjunct.
1859 struct {
1860 int single_disjunct;
1861 const char *str;
1862 } coalesce_unbounded_tests[] = {
1863 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1864 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1865 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1866 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1867 "-10 <= y <= 0}" },
1868 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1869 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1870 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1871 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1874 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1875 * option turned off.
1877 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1879 int i;
1880 int r = 0;
1881 int bounded;
1883 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1884 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1886 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1887 const char *str = coalesce_unbounded_tests[i].str;
1888 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1889 if (test_coalesce_set(ctx, str, check_one) >= 0)
1890 continue;
1891 r = -1;
1892 break;
1895 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1897 return r;
1900 /* Inputs for coalescing tests.
1901 * "str" is a string representation of the input set.
1902 * "single_disjunct" is set if we expect the result to consist of
1903 * a single disjunct.
1905 struct {
1906 int single_disjunct;
1907 const char *str;
1908 } coalesce_tests[] = {
1909 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1910 "y >= x & x >= 2 & 5 >= y }" },
1911 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1912 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1913 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1914 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1915 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1916 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1917 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1918 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1919 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1920 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1921 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1922 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1923 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1924 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1925 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1926 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1927 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1928 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1929 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1930 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1931 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1932 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1933 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1934 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1935 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1936 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1937 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1938 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1939 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1940 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1941 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1942 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1943 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1944 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1945 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1946 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1947 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1948 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1949 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1950 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1951 "[o0, o1, o2, o3, o4, o5, o6]] : "
1952 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1953 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1954 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1955 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1956 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1957 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1958 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1959 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1960 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1961 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1962 "o6 >= i3 + i6 - o3 and M >= 0 and "
1963 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1964 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1965 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1966 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1967 "(o0 = 0 and M >= 2 and N >= 3) or "
1968 "(M = 0 and o0 = 0 and N >= 3) }" },
1969 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1970 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1971 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1972 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1973 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1974 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1975 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1976 "(y = 3 and x = 1) }" },
1977 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1978 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1979 "i1 <= M and i3 <= M and i4 <= M) or "
1980 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1981 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1982 "i4 <= -1 + M) }" },
1983 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1984 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1985 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1986 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1987 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1988 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1989 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1990 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1991 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1992 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1993 { 0, "{ [a, b] : exists e : 2e = a and "
1994 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1995 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1996 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1997 "j >= 1 and j' <= i + j - i' and i >= 1; "
1998 "[1, 1, 1, 1] }" },
1999 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
2000 "[i,j] : exists a : j = 3a }" },
2001 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
2002 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
2003 "a >= 3) or "
2004 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
2005 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
2006 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
2007 "c <= 6 + 8a and a >= 3; "
2008 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
2009 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
2010 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2011 "[x,0] : 3 <= x <= 4 }" },
2012 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
2013 "[x,0] : 4 <= x <= 5 }" },
2014 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2015 "[x,0] : 3 <= x <= 5 }" },
2016 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
2017 "[x,0] : 3 <= x <= 4 }" },
2018 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
2019 "i1 <= 0; "
2020 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
2021 { 1, "{ [0,0]; [1,1] }" },
2022 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
2023 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
2024 "ii <= k;"
2025 "[k, 0, k] : k <= 6 and k >= 1 }" },
2026 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
2027 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
2028 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
2029 { 1, "[n] -> { [1] : n >= 0;"
2030 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
2031 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
2032 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
2033 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
2034 "2e0 <= x and 2e0 <= n);"
2035 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
2036 "n >= 0) }" },
2037 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
2038 "128e0 >= -134 + 127t1 and t1 >= 2 and "
2039 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
2040 "t1 = 1 }" },
2041 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
2042 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
2043 "[0, 0] }" },
2044 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
2045 "t1 >= 13 and t1 <= 16);"
2046 "[t1] : t1 <= 15 and t1 >= 12 }" },
2047 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
2048 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
2049 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
2050 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
2051 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
2052 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
2053 "i <= 4j + 2 }" },
2054 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
2055 "(exists (e0 : 3e0 = -2 + c0)) }" },
2056 { 0, "[n, b0, t0] -> "
2057 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2058 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2059 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2060 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2061 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
2062 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
2063 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
2064 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
2065 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2066 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2067 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2068 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
2069 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
2070 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
2071 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
2072 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
2073 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
2074 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
2075 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
2076 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
2077 { 0, "{ [i0, i1, i2] : "
2078 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
2079 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
2080 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
2081 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
2082 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
2083 "32e0 <= 31 + i0)) or "
2084 "i0 >= 0 }" },
2085 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
2086 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
2087 "2*floor((c)/2) = c and 0 <= a <= 192;"
2088 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
2090 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
2091 "(0 <= a <= b <= n) }" },
2092 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
2093 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
2094 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
2095 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2096 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2097 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2098 "b = 3 and 9e0 <= -19 + 2c)) }" },
2099 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2100 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2101 "(a = 4 and b = 3 and "
2102 "9*floor((-16 + 2c)/9) <= -19 + 2c) }" },
2103 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
2104 "(b = -1 + a and 0 < a <= 3 and "
2105 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2106 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2107 "b = 3 and 9e0 <= -19 + 2c)) }" },
2108 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2109 "[1, 0] }" },
2110 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2111 "[0, 1] }" },
2112 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2113 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2114 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
2115 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
2116 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
2117 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
2118 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
2119 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
2120 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
2121 { 1, "{ [a] : a <= 8 and "
2122 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
2123 { 1, "{ [x, y] : 2y = -x and x <= 0 or "
2124 "x <= -1 and 2y <= -x - 1 and 2y >= x - 1 }" },
2125 { 0, "{ [x, y] : 2y = -x and x <= 0 or "
2126 "x <= -2 and 2y <= -x - 1 and 2y >= x - 1 }" },
2127 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2128 "(a < 0 and 3*floor((a)/3) < a) }" },
2129 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2130 "(a < -1 and 3*floor((a)/3) < a) }" },
2131 { 1, "{ [a, b] : a <= 1024 and b >= 0 and "
2132 "((-31 - a + b <= 32*floor((-1 - a)/32) <= -33 + b and "
2133 "32*floor((-1 - a)/32) <= -16 + b + 16*floor((-1 - a)/16))"
2134 "or (2 <= a <= 15 and b < a)) }" },
2135 { 1, "{ [a] : a > 0 and ((16*floor((a)/16) < a and "
2136 "32*floor((a)/32) < a) or a <= 15) }" },
2137 { 1, "{ [a, b, c, d] : (-a + d) mod 64 = 0 and a <= 8 and b <= 1 and "
2138 "10 - a <= c <= 3 and d >= 5 and 9 - 64b <= d <= 70;"
2139 "[a, b = 1, c, d] : (-a + d) mod 64 = 0 and a <= 8 and c >= 4 and "
2140 "10 - a <= c <= 5 and 5 <= d <= 73 - c }" },
2141 { 1, "[n, m] -> { S_0[i] : (-n + i) mod 3 = 0 and m >= 3 + n and "
2142 "i >= n and 3*floor((2 + n + 2m)/3) <= n + 3m - i; "
2143 "S_0[n] : n <= m <= 2 + n }" },
2144 { 1, "{ [a, b] : exists (e0: 0 <= a <= 1 and b >= 0 and "
2145 "2e0 >= -5 + a + 2b and 2e0 >= -1 + a + b and "
2146 "2e0 <= a + b); "
2147 "[a, b] : exists (e0: 0 <= a <= 1 and 2e0 >= -5 + a + 2b and "
2148 "2e0 >= -1 - a + b and 2e0 <= -a + b and "
2149 "2e0 < -a + 2b) }" },
2150 { 1, "{ [i, j, i - 8j] : 8 <= i <= 63 and -7 + i <= 8j <= i; "
2151 "[i, 0, i] : 0 <= i <= 7 }" },
2152 { 1, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [1, 1] }" },
2153 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [0, 2] }" },
2154 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [-1, 3] }" },
2155 { 1, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [1, 1] }" },
2156 { 0, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [2, 1] }" },
2157 { 0, "{ [a, c] : (2 + a) mod 4 = 0 or "
2158 "(c = 4 + a and 4 * floor((a)/4) = a and a >= 0 and a <= 4) or "
2159 "(c = 3 + a and 4 * floor((-1 + a)/4) = -1 + a and "
2160 "a > 0 and a <= 5) }" },
2161 { 1, "{ [1, 0, 0]; [a, b, c] : -1 <= -a < b <= 0 and 2c > b }" },
2164 /* A specialized coalescing test case that would result
2165 * in a segmentation fault or a failed assertion in earlier versions of isl.
2167 static int test_coalesce_special(struct isl_ctx *ctx)
2169 const char *str;
2170 isl_map *map1, *map2;
2172 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2173 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
2174 "(y = 201 and o1 <= 239 and o1 >= 212) or "
2175 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
2176 "o1 <= 239 and o1 >= 212)) or "
2177 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
2178 "o1 <= 241 and o1 >= 240));"
2179 "[S_L220_OUT[] -> T7[]] -> "
2180 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
2181 "(y = 2 and o1 <= 241 and o1 >= 212) or "
2182 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
2183 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
2184 map1 = isl_map_read_from_str(ctx, str);
2185 map1 = isl_map_align_divs_internal(map1);
2186 map1 = isl_map_coalesce(map1);
2187 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2188 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
2189 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
2190 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
2191 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
2192 map2 = isl_map_read_from_str(ctx, str);
2193 map2 = isl_map_union(map2, map1);
2194 map2 = isl_map_align_divs_internal(map2);
2195 map2 = isl_map_coalesce(map2);
2196 isl_map_free(map2);
2197 if (!map2)
2198 return -1;
2200 return 0;
2203 /* A specialized coalescing test case that would result in an assertion
2204 * in an earlier version of isl.
2205 * The explicit call to isl_basic_set_union prevents the implicit
2206 * equality constraints in the first basic map from being detected prior
2207 * to the call to isl_set_coalesce, at least at the point
2208 * where this test case was introduced.
2210 static int test_coalesce_special2(struct isl_ctx *ctx)
2212 const char *str;
2213 isl_basic_set *bset1, *bset2;
2214 isl_set *set;
2216 str = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
2217 bset1 = isl_basic_set_read_from_str(ctx, str);
2218 str = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }" ;
2219 bset2 = isl_basic_set_read_from_str(ctx, str);
2220 set = isl_basic_set_union(bset1, bset2);
2221 set = isl_set_coalesce(set);
2222 isl_set_free(set);
2224 if (!set)
2225 return -1;
2226 return 0;
2229 /* Check that calling isl_set_coalesce does not leave other sets
2230 * that may share some information with the input to isl_set_coalesce
2231 * in an inconsistent state.
2232 * In particular, older versions of isl would modify all copies
2233 * of the basic sets in the isl_set_coalesce input in a way
2234 * that could leave them in an inconsistent state.
2235 * The result of printing any other set containing one of these
2236 * basic sets would then result in an invalid set description.
2238 static int test_coalesce_special3(isl_ctx *ctx)
2240 const char *str;
2241 char *s;
2242 isl_set *set1, *set2;
2243 isl_printer *p;
2245 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
2246 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
2247 set2 = isl_set_read_from_str(ctx, str);
2248 set1 = isl_set_union(set1, isl_set_copy(set2));
2249 set1 = isl_set_coalesce(set1);
2250 isl_set_free(set1);
2252 p = isl_printer_to_str(ctx);
2253 p = isl_printer_print_set(p, set2);
2254 isl_set_free(set2);
2255 s = isl_printer_get_str(p);
2256 isl_printer_free(p);
2257 set1 = isl_set_read_from_str(ctx, s);
2258 free(s);
2259 isl_set_free(set1);
2261 if (!set1)
2262 return -1;
2264 return 0;
2267 /* Check that calling isl_set_coalesce does not leave other sets
2268 * that may share some information with the input to isl_set_coalesce
2269 * in an inconsistent state.
2270 * In particular, when isl_set_coalesce detects equality constraints,
2271 * it does not immediately perform Gaussian elimination on them,
2272 * but then it needs to ensure that it is performed at some point.
2273 * The input set has implicit equality constraints in the first disjunct.
2274 * It is constructed as an intersection, because otherwise
2275 * those equality constraints would already be detected during parsing.
2277 static isl_stat test_coalesce_special4(isl_ctx *ctx)
2279 isl_set *set1, *set2;
2281 set1 = isl_set_read_from_str(ctx, "{ [a, b] : b <= 0 or a <= 1 }");
2282 set2 = isl_set_read_from_str(ctx, "{ [a, b] : -1 <= -a < b }");
2283 set1 = isl_set_intersect(set1, set2);
2284 isl_set_free(isl_set_coalesce(isl_set_copy(set1)));
2285 set1 = isl_set_coalesce(set1);
2286 isl_set_free(set1);
2288 if (!set1)
2289 return isl_stat_error;
2291 return isl_stat_ok;
2294 /* Test the functionality of isl_set_coalesce.
2295 * That is, check that the output is always equal to the input
2296 * and in some cases that the result consists of a single disjunct.
2298 static int test_coalesce(struct isl_ctx *ctx)
2300 int i;
2302 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2303 const char *str = coalesce_tests[i].str;
2304 int check_one = coalesce_tests[i].single_disjunct;
2305 if (test_coalesce_set(ctx, str, check_one) < 0)
2306 return -1;
2309 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2310 return -1;
2311 if (test_coalesce_special(ctx) < 0)
2312 return -1;
2313 if (test_coalesce_special2(ctx) < 0)
2314 return -1;
2315 if (test_coalesce_special3(ctx) < 0)
2316 return -1;
2317 if (test_coalesce_special4(ctx) < 0)
2318 return -1;
2320 return 0;
2323 /* Construct a representation of the graph on the right of Figure 1
2324 * in "Computing the Transitive Closure of a Union of
2325 * Affine Integer Tuple Relations".
2327 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2329 isl_set *dom;
2330 isl_map *up, *right;
2332 dom = isl_set_read_from_str(ctx,
2333 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2334 "2 x - 3 y + 3 >= 0 }");
2335 right = isl_map_read_from_str(ctx,
2336 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2337 up = isl_map_read_from_str(ctx,
2338 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2339 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2340 right = isl_map_intersect_range(right, isl_set_copy(dom));
2341 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2342 up = isl_map_intersect_range(up, dom);
2343 return isl_map_union(up, right);
2346 /* Construct a representation of the power of the graph
2347 * on the right of Figure 1 in "Computing the Transitive Closure of
2348 * a Union of Affine Integer Tuple Relations".
2350 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2352 return isl_map_read_from_str(ctx,
2353 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2354 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2355 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2358 /* Construct a representation of the transitive closure of the graph
2359 * on the right of Figure 1 in "Computing the Transitive Closure of
2360 * a Union of Affine Integer Tuple Relations".
2362 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2364 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2367 static int test_closure(isl_ctx *ctx)
2369 const char *str;
2370 isl_map *map, *map2;
2371 int exact, equal;
2373 /* COCOA example 1 */
2374 map = isl_map_read_from_str(ctx,
2375 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2376 "1 <= i and i < n and 1 <= j and j < n or "
2377 "i2 = i + 1 and j2 = j - 1 and "
2378 "1 <= i and i < n and 2 <= j and j <= n }");
2379 map = isl_map_power(map, &exact);
2380 assert(exact);
2381 isl_map_free(map);
2383 /* COCOA example 1 */
2384 map = isl_map_read_from_str(ctx,
2385 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2386 "1 <= i and i < n and 1 <= j and j < n or "
2387 "i2 = i + 1 and j2 = j - 1 and "
2388 "1 <= i and i < n and 2 <= j and j <= n }");
2389 map = isl_map_transitive_closure(map, &exact);
2390 assert(exact);
2391 map2 = isl_map_read_from_str(ctx,
2392 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2393 "1 <= i and i < n and 1 <= j and j <= n and "
2394 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2395 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2396 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2397 assert(isl_map_is_equal(map, map2));
2398 isl_map_free(map2);
2399 isl_map_free(map);
2401 map = isl_map_read_from_str(ctx,
2402 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2403 " 0 <= y and y <= n }");
2404 map = isl_map_transitive_closure(map, &exact);
2405 map2 = isl_map_read_from_str(ctx,
2406 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2407 " 0 <= y and y <= n }");
2408 assert(isl_map_is_equal(map, map2));
2409 isl_map_free(map2);
2410 isl_map_free(map);
2412 /* COCOA example 2 */
2413 map = isl_map_read_from_str(ctx,
2414 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2415 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2416 "i2 = i + 2 and j2 = j - 2 and "
2417 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2418 map = isl_map_transitive_closure(map, &exact);
2419 assert(exact);
2420 map2 = isl_map_read_from_str(ctx,
2421 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2422 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2423 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2424 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2425 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2426 assert(isl_map_is_equal(map, map2));
2427 isl_map_free(map);
2428 isl_map_free(map2);
2430 /* COCOA Fig.2 left */
2431 map = isl_map_read_from_str(ctx,
2432 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2433 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2434 "j <= n or "
2435 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2436 "j <= 2 i - 3 and j <= n - 2 or "
2437 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2438 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2439 map = isl_map_transitive_closure(map, &exact);
2440 assert(exact);
2441 isl_map_free(map);
2443 /* COCOA Fig.2 right */
2444 map = isl_map_read_from_str(ctx,
2445 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2446 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2447 "j <= n or "
2448 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2449 "j <= 2 i - 4 and j <= n - 3 or "
2450 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2451 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2452 map = isl_map_power(map, &exact);
2453 assert(exact);
2454 isl_map_free(map);
2456 /* COCOA Fig.2 right */
2457 map = isl_map_read_from_str(ctx,
2458 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2459 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2460 "j <= n or "
2461 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2462 "j <= 2 i - 4 and j <= n - 3 or "
2463 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2464 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2465 map = isl_map_transitive_closure(map, &exact);
2466 assert(exact);
2467 map2 = isl_map_read_from_str(ctx,
2468 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2469 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2470 "j <= n and 3 + i + 2 j <= 3 n and "
2471 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2472 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2473 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2474 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2475 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2476 assert(isl_map_is_equal(map, map2));
2477 isl_map_free(map2);
2478 isl_map_free(map);
2480 map = cocoa_fig_1_right_graph(ctx);
2481 map = isl_map_transitive_closure(map, &exact);
2482 assert(exact);
2483 map2 = cocoa_fig_1_right_tc(ctx);
2484 assert(isl_map_is_equal(map, map2));
2485 isl_map_free(map2);
2486 isl_map_free(map);
2488 map = cocoa_fig_1_right_graph(ctx);
2489 map = isl_map_power(map, &exact);
2490 map2 = cocoa_fig_1_right_power(ctx);
2491 equal = isl_map_is_equal(map, map2);
2492 isl_map_free(map2);
2493 isl_map_free(map);
2494 if (equal < 0)
2495 return -1;
2496 if (!exact)
2497 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2498 if (!equal)
2499 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2501 /* COCOA Theorem 1 counter example */
2502 map = isl_map_read_from_str(ctx,
2503 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2504 "i2 = 1 and j2 = j or "
2505 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2506 map = isl_map_transitive_closure(map, &exact);
2507 assert(exact);
2508 isl_map_free(map);
2510 map = isl_map_read_from_str(ctx,
2511 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2512 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2513 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2514 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2515 map = isl_map_transitive_closure(map, &exact);
2516 assert(exact);
2517 isl_map_free(map);
2519 /* Kelly et al 1996, fig 12 */
2520 map = isl_map_read_from_str(ctx,
2521 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2522 "1 <= i,j,j+1 <= n or "
2523 "j = n and j2 = 1 and i2 = i + 1 and "
2524 "1 <= i,i+1 <= n }");
2525 map = isl_map_transitive_closure(map, &exact);
2526 assert(exact);
2527 map2 = isl_map_read_from_str(ctx,
2528 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2529 "1 <= i <= n and i = i2 or "
2530 "1 <= i < i2 <= n and 1 <= j <= n and "
2531 "1 <= j2 <= n }");
2532 assert(isl_map_is_equal(map, map2));
2533 isl_map_free(map2);
2534 isl_map_free(map);
2536 /* Omega's closure4 */
2537 map = isl_map_read_from_str(ctx,
2538 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2539 "1 <= x,y <= 10 or "
2540 "x2 = x + 1 and y2 = y and "
2541 "1 <= x <= 20 && 5 <= y <= 15 }");
2542 map = isl_map_transitive_closure(map, &exact);
2543 assert(exact);
2544 isl_map_free(map);
2546 map = isl_map_read_from_str(ctx,
2547 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2548 map = isl_map_transitive_closure(map, &exact);
2549 assert(!exact);
2550 map2 = isl_map_read_from_str(ctx,
2551 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2552 assert(isl_map_is_equal(map, map2));
2553 isl_map_free(map);
2554 isl_map_free(map2);
2556 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2557 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2558 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2559 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2560 map = isl_map_read_from_str(ctx, str);
2561 map = isl_map_transitive_closure(map, &exact);
2562 assert(exact);
2563 map2 = isl_map_read_from_str(ctx, str);
2564 assert(isl_map_is_equal(map, map2));
2565 isl_map_free(map);
2566 isl_map_free(map2);
2568 str = "{[0] -> [1]; [2] -> [3]}";
2569 map = isl_map_read_from_str(ctx, str);
2570 map = isl_map_transitive_closure(map, &exact);
2571 assert(exact);
2572 map2 = isl_map_read_from_str(ctx, str);
2573 assert(isl_map_is_equal(map, map2));
2574 isl_map_free(map);
2575 isl_map_free(map2);
2577 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2578 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2579 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2580 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2581 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2582 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2583 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2584 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2585 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2586 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2587 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2588 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2589 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2590 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2591 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2592 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2593 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2594 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2595 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2596 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2597 map = isl_map_read_from_str(ctx, str);
2598 map = isl_map_transitive_closure(map, NULL);
2599 assert(map);
2600 isl_map_free(map);
2602 return 0;
2605 static int test_lex(struct isl_ctx *ctx)
2607 isl_space *dim;
2608 isl_map *map;
2609 int empty;
2611 dim = isl_space_set_alloc(ctx, 0, 0);
2612 map = isl_map_lex_le(dim);
2613 empty = isl_map_is_empty(map);
2614 isl_map_free(map);
2616 if (empty < 0)
2617 return -1;
2618 if (empty)
2619 isl_die(ctx, isl_error_unknown,
2620 "expecting non-empty result", return -1);
2622 return 0;
2625 /* Inputs for isl_map_lexmin tests.
2626 * "map" is the input and "lexmin" is the expected result.
2628 struct {
2629 const char *map;
2630 const char *lexmin;
2631 } lexmin_tests [] = {
2632 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2633 "{ [x] -> [5] : 6 <= x <= 8; "
2634 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2635 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2636 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2637 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2638 "{ [x] -> [y] : (4y = x and x >= 0) or "
2639 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2640 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2641 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2642 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2643 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2644 /* Check that empty pieces are properly combined. */
2645 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2646 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2647 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2648 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2649 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2650 "x >= 4 }" },
2651 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2652 "a <= 255 and c <= 255 and d <= 255 - j and "
2653 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2654 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2655 "247d <= 247 + i and 248 - b <= 248d <= c and "
2656 "254d >= i - a + b and 254d >= -a + b and "
2657 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2658 "{ [i, k, j] -> "
2659 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2660 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2661 "247j >= 62738 - i and 509j <= 129795 + i and "
2662 "742j >= 188724 - i; "
2663 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2664 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2665 "16*floor((8 + b)/16) <= 7 + b; "
2666 "[a] -> [1] }",
2667 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2668 "[a] -> [b = 0] : 0 < a <= 509 }" },
2669 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2670 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2673 static int test_lexmin(struct isl_ctx *ctx)
2675 int i;
2676 int equal;
2677 const char *str;
2678 isl_basic_map *bmap;
2679 isl_map *map, *map2;
2680 isl_set *set;
2681 isl_set *set2;
2682 isl_pw_multi_aff *pma;
2684 str = "[p0, p1] -> { [] -> [] : "
2685 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2686 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2687 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2688 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2689 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2690 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2691 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2692 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2693 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2694 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2695 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2696 map = isl_map_read_from_str(ctx, str);
2697 map = isl_map_lexmin(map);
2698 isl_map_free(map);
2700 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2701 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2702 set = isl_set_read_from_str(ctx, str);
2703 set = isl_set_lexmax(set);
2704 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2705 set2 = isl_set_read_from_str(ctx, str);
2706 set = isl_set_intersect(set, set2);
2707 assert(!isl_set_is_empty(set));
2708 isl_set_free(set);
2710 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2711 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2712 map = isl_map_lexmin(map);
2713 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2714 equal = isl_map_is_equal(map, map2);
2715 isl_map_free(map);
2716 isl_map_free(map2);
2718 if (equal < 0)
2719 return -1;
2720 if (!equal)
2721 isl_die(ctx, isl_error_unknown,
2722 "unexpected result", return -1);
2725 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2726 " 8i' <= i and 8i' >= -7 + i }";
2727 bmap = isl_basic_map_read_from_str(ctx, str);
2728 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2729 map2 = isl_map_from_pw_multi_aff(pma);
2730 map = isl_map_from_basic_map(bmap);
2731 assert(isl_map_is_equal(map, map2));
2732 isl_map_free(map);
2733 isl_map_free(map2);
2735 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2736 " 8i' <= i and 8i' >= -7 + i }";
2737 set = isl_set_read_from_str(ctx, str);
2738 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2739 set2 = isl_set_from_pw_multi_aff(pma);
2740 equal = isl_set_is_equal(set, set2);
2741 isl_set_free(set);
2742 isl_set_free(set2);
2743 if (equal < 0)
2744 return -1;
2745 if (!equal)
2746 isl_die(ctx, isl_error_unknown,
2747 "unexpected difference between set and "
2748 "piecewise affine expression", return -1);
2750 return 0;
2753 /* A specialized isl_set_min_val test case that would return the wrong result
2754 * in earlier versions of isl.
2755 * The explicit call to isl_basic_set_union prevents the second basic set
2756 * from being determined to be empty prior to the call to isl_set_min_val,
2757 * at least at the point where this test case was introduced.
2759 static int test_min_special(isl_ctx *ctx)
2761 const char *str;
2762 isl_basic_set *bset1, *bset2;
2763 isl_set *set;
2764 isl_aff *obj;
2765 isl_val *res;
2766 int ok;
2768 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2769 bset1 = isl_basic_set_read_from_str(ctx, str);
2770 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2771 bset2 = isl_basic_set_read_from_str(ctx, str);
2772 set = isl_basic_set_union(bset1, bset2);
2773 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2775 res = isl_set_min_val(set, obj);
2776 ok = isl_val_cmp_si(res, 5) == 0;
2778 isl_aff_free(obj);
2779 isl_set_free(set);
2780 isl_val_free(res);
2782 if (!res)
2783 return -1;
2784 if (!ok)
2785 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2786 return -1);
2788 return 0;
2791 /* A specialized isl_set_min_val test case that would return an error
2792 * in earlier versions of isl.
2794 static int test_min_special2(isl_ctx *ctx)
2796 const char *str;
2797 isl_basic_set *bset;
2798 isl_aff *obj;
2799 isl_val *res;
2801 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
2802 bset = isl_basic_set_read_from_str(ctx, str);
2804 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
2806 res = isl_basic_set_max_val(bset, obj);
2808 isl_basic_set_free(bset);
2809 isl_aff_free(obj);
2810 isl_val_free(res);
2812 if (!res)
2813 return -1;
2815 return 0;
2818 struct {
2819 const char *set;
2820 const char *obj;
2821 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2822 __isl_keep isl_aff *obj);
2823 const char *res;
2824 } opt_tests[] = {
2825 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2826 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2827 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2828 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2829 &isl_set_max_val, "30" },
2833 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2834 * In particular, check the results on non-convex inputs.
2836 static int test_min(struct isl_ctx *ctx)
2838 int i;
2839 isl_set *set;
2840 isl_aff *obj;
2841 isl_val *val, *res;
2842 isl_bool ok;
2844 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2845 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2846 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2847 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2848 val = opt_tests[i].fn(set, obj);
2849 ok = isl_val_eq(res, val);
2850 isl_val_free(res);
2851 isl_val_free(val);
2852 isl_aff_free(obj);
2853 isl_set_free(set);
2855 if (ok < 0)
2856 return -1;
2857 if (!ok)
2858 isl_die(ctx, isl_error_unknown,
2859 "unexpected optimum", return -1);
2862 if (test_min_special(ctx) < 0)
2863 return -1;
2864 if (test_min_special2(ctx) < 0)
2865 return -1;
2867 return 0;
2870 struct must_may {
2871 isl_map *must;
2872 isl_map *may;
2875 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2876 void *dep_user, void *user)
2878 struct must_may *mm = (struct must_may *)user;
2880 if (must)
2881 mm->must = isl_map_union(mm->must, dep);
2882 else
2883 mm->may = isl_map_union(mm->may, dep);
2885 return isl_stat_ok;
2888 static int common_space(void *first, void *second)
2890 int depth = *(int *)first;
2891 return 2 * depth;
2894 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2896 isl_map *map2;
2897 int equal;
2899 if (!map)
2900 return -1;
2902 map2 = isl_map_read_from_str(map->ctx, str);
2903 equal = isl_map_is_equal(map, map2);
2904 isl_map_free(map2);
2906 return equal;
2909 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2911 int equal;
2913 equal = map_is_equal(map, str);
2914 if (equal < 0)
2915 return -1;
2916 if (!equal)
2917 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2918 "result not as expected", return -1);
2919 return 0;
2922 static int test_dep(struct isl_ctx *ctx)
2924 const char *str;
2925 isl_space *dim;
2926 isl_map *map;
2927 isl_access_info *ai;
2928 isl_flow *flow;
2929 int depth;
2930 struct must_may mm;
2932 depth = 3;
2934 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2935 map = isl_map_read_from_str(ctx, str);
2936 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2938 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2939 map = isl_map_read_from_str(ctx, str);
2940 ai = isl_access_info_add_source(ai, map, 1, &depth);
2942 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2943 map = isl_map_read_from_str(ctx, str);
2944 ai = isl_access_info_add_source(ai, map, 1, &depth);
2946 flow = isl_access_info_compute_flow(ai);
2947 dim = isl_space_alloc(ctx, 0, 3, 3);
2948 mm.must = isl_map_empty(isl_space_copy(dim));
2949 mm.may = isl_map_empty(dim);
2951 isl_flow_foreach(flow, collect_must_may, &mm);
2953 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2954 " [1,10,0] -> [2,5,0] }";
2955 assert(map_is_equal(mm.must, str));
2956 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2957 assert(map_is_equal(mm.may, str));
2959 isl_map_free(mm.must);
2960 isl_map_free(mm.may);
2961 isl_flow_free(flow);
2964 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2965 map = isl_map_read_from_str(ctx, str);
2966 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2968 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2969 map = isl_map_read_from_str(ctx, str);
2970 ai = isl_access_info_add_source(ai, map, 1, &depth);
2972 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2973 map = isl_map_read_from_str(ctx, str);
2974 ai = isl_access_info_add_source(ai, map, 0, &depth);
2976 flow = isl_access_info_compute_flow(ai);
2977 dim = isl_space_alloc(ctx, 0, 3, 3);
2978 mm.must = isl_map_empty(isl_space_copy(dim));
2979 mm.may = isl_map_empty(dim);
2981 isl_flow_foreach(flow, collect_must_may, &mm);
2983 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2984 assert(map_is_equal(mm.must, str));
2985 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2986 assert(map_is_equal(mm.may, str));
2988 isl_map_free(mm.must);
2989 isl_map_free(mm.may);
2990 isl_flow_free(flow);
2993 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2994 map = isl_map_read_from_str(ctx, str);
2995 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2997 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2998 map = isl_map_read_from_str(ctx, str);
2999 ai = isl_access_info_add_source(ai, map, 0, &depth);
3001 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3002 map = isl_map_read_from_str(ctx, str);
3003 ai = isl_access_info_add_source(ai, map, 0, &depth);
3005 flow = isl_access_info_compute_flow(ai);
3006 dim = isl_space_alloc(ctx, 0, 3, 3);
3007 mm.must = isl_map_empty(isl_space_copy(dim));
3008 mm.may = isl_map_empty(dim);
3010 isl_flow_foreach(flow, collect_must_may, &mm);
3012 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
3013 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3014 assert(map_is_equal(mm.may, str));
3015 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3016 assert(map_is_equal(mm.must, str));
3018 isl_map_free(mm.must);
3019 isl_map_free(mm.may);
3020 isl_flow_free(flow);
3023 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
3024 map = isl_map_read_from_str(ctx, str);
3025 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3027 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3028 map = isl_map_read_from_str(ctx, str);
3029 ai = isl_access_info_add_source(ai, map, 0, &depth);
3031 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
3032 map = isl_map_read_from_str(ctx, str);
3033 ai = isl_access_info_add_source(ai, map, 0, &depth);
3035 flow = isl_access_info_compute_flow(ai);
3036 dim = isl_space_alloc(ctx, 0, 3, 3);
3037 mm.must = isl_map_empty(isl_space_copy(dim));
3038 mm.may = isl_map_empty(dim);
3040 isl_flow_foreach(flow, collect_must_may, &mm);
3042 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
3043 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
3044 assert(map_is_equal(mm.may, str));
3045 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3046 assert(map_is_equal(mm.must, str));
3048 isl_map_free(mm.must);
3049 isl_map_free(mm.may);
3050 isl_flow_free(flow);
3053 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
3054 map = isl_map_read_from_str(ctx, str);
3055 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3057 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3058 map = isl_map_read_from_str(ctx, str);
3059 ai = isl_access_info_add_source(ai, map, 0, &depth);
3061 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
3062 map = isl_map_read_from_str(ctx, str);
3063 ai = isl_access_info_add_source(ai, map, 0, &depth);
3065 flow = isl_access_info_compute_flow(ai);
3066 dim = isl_space_alloc(ctx, 0, 3, 3);
3067 mm.must = isl_map_empty(isl_space_copy(dim));
3068 mm.may = isl_map_empty(dim);
3070 isl_flow_foreach(flow, collect_must_may, &mm);
3072 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
3073 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
3074 assert(map_is_equal(mm.may, str));
3075 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3076 assert(map_is_equal(mm.must, str));
3078 isl_map_free(mm.must);
3079 isl_map_free(mm.may);
3080 isl_flow_free(flow);
3083 depth = 5;
3085 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3086 map = isl_map_read_from_str(ctx, str);
3087 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
3089 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3090 map = isl_map_read_from_str(ctx, str);
3091 ai = isl_access_info_add_source(ai, map, 1, &depth);
3093 flow = isl_access_info_compute_flow(ai);
3094 dim = isl_space_alloc(ctx, 0, 5, 5);
3095 mm.must = isl_map_empty(isl_space_copy(dim));
3096 mm.may = isl_map_empty(dim);
3098 isl_flow_foreach(flow, collect_must_may, &mm);
3100 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
3101 assert(map_is_equal(mm.must, str));
3102 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
3103 assert(map_is_equal(mm.may, str));
3105 isl_map_free(mm.must);
3106 isl_map_free(mm.may);
3107 isl_flow_free(flow);
3109 return 0;
3112 /* Check that the dependence analysis proceeds without errors.
3113 * Earlier versions of isl would break down during the analysis
3114 * due to the use of the wrong spaces.
3116 static int test_flow(isl_ctx *ctx)
3118 const char *str;
3119 isl_union_map *access, *schedule;
3120 isl_union_map *must_dep, *may_dep;
3121 int r;
3123 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
3124 access = isl_union_map_read_from_str(ctx, str);
3125 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
3126 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
3127 "S2[] -> [1,0,0,0]; "
3128 "S3[] -> [-1,0,0,0] }";
3129 schedule = isl_union_map_read_from_str(ctx, str);
3130 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
3131 isl_union_map_copy(access), schedule,
3132 &must_dep, &may_dep, NULL, NULL);
3133 isl_union_map_free(may_dep);
3134 isl_union_map_free(must_dep);
3136 return r;
3139 struct {
3140 const char *map;
3141 int sv;
3142 } sv_tests[] = {
3143 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
3144 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
3145 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
3146 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
3147 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
3148 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
3149 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3150 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3151 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
3154 int test_sv(isl_ctx *ctx)
3156 isl_union_map *umap;
3157 int i;
3158 int sv;
3160 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
3161 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
3162 sv = isl_union_map_is_single_valued(umap);
3163 isl_union_map_free(umap);
3164 if (sv < 0)
3165 return -1;
3166 if (sv_tests[i].sv && !sv)
3167 isl_die(ctx, isl_error_internal,
3168 "map not detected as single valued", return -1);
3169 if (!sv_tests[i].sv && sv)
3170 isl_die(ctx, isl_error_internal,
3171 "map detected as single valued", return -1);
3174 return 0;
3177 struct {
3178 const char *str;
3179 int bijective;
3180 } bijective_tests[] = {
3181 { "[N,M]->{[i,j] -> [i]}", 0 },
3182 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
3183 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
3184 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
3185 { "[N,M]->{[i,j] -> [j,i]}", 1 },
3186 { "[N,M]->{[i,j] -> [i+j]}", 0 },
3187 { "[N,M]->{[i,j] -> []}", 0 },
3188 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
3189 { "[N,M]->{[i,j] -> [2i]}", 0 },
3190 { "[N,M]->{[i,j] -> [i,i]}", 0 },
3191 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
3192 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
3193 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
3196 static int test_bijective(struct isl_ctx *ctx)
3198 isl_map *map;
3199 int i;
3200 int bijective;
3202 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
3203 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
3204 bijective = isl_map_is_bijective(map);
3205 isl_map_free(map);
3206 if (bijective < 0)
3207 return -1;
3208 if (bijective_tests[i].bijective && !bijective)
3209 isl_die(ctx, isl_error_internal,
3210 "map not detected as bijective", return -1);
3211 if (!bijective_tests[i].bijective && bijective)
3212 isl_die(ctx, isl_error_internal,
3213 "map detected as bijective", return -1);
3216 return 0;
3219 /* Inputs for isl_pw_qpolynomial_gist tests.
3220 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
3222 struct {
3223 const char *pwqp;
3224 const char *set;
3225 const char *gist;
3226 } pwqp_gist_tests[] = {
3227 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
3228 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
3229 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
3230 "{ [i] -> -1/2 + 1/2 * i }" },
3231 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
3234 static int test_pwqp(struct isl_ctx *ctx)
3236 int i;
3237 const char *str;
3238 isl_set *set;
3239 isl_pw_qpolynomial *pwqp1, *pwqp2;
3240 int equal;
3242 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3243 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3245 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
3246 isl_dim_in, 1, 1);
3248 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3249 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3251 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3253 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3255 isl_pw_qpolynomial_free(pwqp1);
3257 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
3258 str = pwqp_gist_tests[i].pwqp;
3259 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3260 str = pwqp_gist_tests[i].set;
3261 set = isl_set_read_from_str(ctx, str);
3262 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
3263 str = pwqp_gist_tests[i].gist;
3264 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3265 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3266 equal = isl_pw_qpolynomial_is_zero(pwqp1);
3267 isl_pw_qpolynomial_free(pwqp1);
3269 if (equal < 0)
3270 return -1;
3271 if (!equal)
3272 isl_die(ctx, isl_error_unknown,
3273 "unexpected result", return -1);
3276 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
3277 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3278 str = "{ [i] -> ([(2 * [i/2])/5]) }";
3279 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3281 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3283 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3285 isl_pw_qpolynomial_free(pwqp1);
3287 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3288 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3289 str = "{ [x] -> x }";
3290 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3292 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3294 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3296 isl_pw_qpolynomial_free(pwqp1);
3298 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3299 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3300 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3301 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3302 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3303 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3304 isl_pw_qpolynomial_free(pwqp1);
3306 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3307 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3308 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3309 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3310 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3311 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3312 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3313 isl_pw_qpolynomial_free(pwqp1);
3314 isl_pw_qpolynomial_free(pwqp2);
3315 if (equal < 0)
3316 return -1;
3317 if (!equal)
3318 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3320 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3321 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3322 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3323 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3324 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3325 isl_val_one(ctx));
3326 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3327 isl_pw_qpolynomial_free(pwqp1);
3328 isl_pw_qpolynomial_free(pwqp2);
3329 if (equal < 0)
3330 return -1;
3331 if (!equal)
3332 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3334 return 0;
3337 static int test_split_periods(isl_ctx *ctx)
3339 const char *str;
3340 isl_pw_qpolynomial *pwqp;
3342 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3343 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3344 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3345 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3347 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3349 isl_pw_qpolynomial_free(pwqp);
3351 if (!pwqp)
3352 return -1;
3354 return 0;
3357 static int test_union(isl_ctx *ctx)
3359 const char *str;
3360 isl_union_set *uset1, *uset2;
3361 isl_union_map *umap1, *umap2;
3362 int equal;
3364 str = "{ [i] : 0 <= i <= 1 }";
3365 uset1 = isl_union_set_read_from_str(ctx, str);
3366 str = "{ [1] -> [0] }";
3367 umap1 = isl_union_map_read_from_str(ctx, str);
3369 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3370 equal = isl_union_map_is_equal(umap1, umap2);
3372 isl_union_map_free(umap1);
3373 isl_union_map_free(umap2);
3375 if (equal < 0)
3376 return -1;
3377 if (!equal)
3378 isl_die(ctx, isl_error_unknown, "union maps not equal",
3379 return -1);
3381 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3382 umap1 = isl_union_map_read_from_str(ctx, str);
3383 str = "{ A[i]; B[i] }";
3384 uset1 = isl_union_set_read_from_str(ctx, str);
3386 uset2 = isl_union_map_domain(umap1);
3388 equal = isl_union_set_is_equal(uset1, uset2);
3390 isl_union_set_free(uset1);
3391 isl_union_set_free(uset2);
3393 if (equal < 0)
3394 return -1;
3395 if (!equal)
3396 isl_die(ctx, isl_error_unknown, "union sets not equal",
3397 return -1);
3399 return 0;
3402 /* Check that computing a bound of a non-zero polynomial over an unbounded
3403 * domain does not produce a rational value.
3404 * In particular, check that the upper bound is infinity.
3406 static int test_bound_unbounded_domain(isl_ctx *ctx)
3408 const char *str;
3409 isl_pw_qpolynomial *pwqp;
3410 isl_pw_qpolynomial_fold *pwf, *pwf2;
3411 isl_bool equal;
3413 str = "{ [m,n] -> -m * n }";
3414 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3415 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3416 str = "{ infty }";
3417 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3418 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3419 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
3420 isl_pw_qpolynomial_fold_free(pwf);
3421 isl_pw_qpolynomial_fold_free(pwf2);
3423 if (equal < 0)
3424 return -1;
3425 if (!equal)
3426 isl_die(ctx, isl_error_unknown,
3427 "expecting infinite polynomial bound", return -1);
3429 return 0;
3432 static int test_bound(isl_ctx *ctx)
3434 const char *str;
3435 unsigned dim;
3436 isl_pw_qpolynomial *pwqp;
3437 isl_pw_qpolynomial_fold *pwf;
3439 if (test_bound_unbounded_domain(ctx) < 0)
3440 return -1;
3442 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3443 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3444 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3445 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3446 isl_pw_qpolynomial_fold_free(pwf);
3447 if (dim != 4)
3448 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3449 return -1);
3451 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3452 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3453 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3454 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3455 isl_pw_qpolynomial_fold_free(pwf);
3456 if (dim != 1)
3457 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3458 return -1);
3460 return 0;
3463 /* isl_set is defined to isl_map internally, so the corresponding elements
3464 * are isl_basic_map objects.
3466 #undef EL_BASE
3467 #undef SET_BASE
3468 #define EL_BASE basic_map
3469 #define SET_BASE set
3470 #include "isl_test_list_templ.c"
3472 #undef EL_BASE
3473 #undef SET_BASE
3474 #define EL_BASE basic_set
3475 #define SET_BASE union_set
3476 #include "isl_test_list_templ.c"
3478 #undef EL_BASE
3479 #undef SET_BASE
3480 #define EL_BASE set
3481 #define SET_BASE union_set
3482 #include "isl_test_list_templ.c"
3484 #undef EL_BASE
3485 #undef SET_BASE
3486 #define EL_BASE basic_map
3487 #define SET_BASE map
3488 #include "isl_test_list_templ.c"
3490 #undef EL_BASE
3491 #undef SET_BASE
3492 #define EL_BASE map
3493 #define SET_BASE union_map
3494 #include "isl_test_list_templ.c"
3496 /* Check that the conversion from isl objects to lists works as expected.
3498 static int test_get_list(isl_ctx *ctx)
3500 if (test_get_list_basic_map_from_set(ctx, "{ [0]; [2]; [3] }"))
3501 return -1;
3502 if (test_get_list_basic_set_from_union_set(ctx, "{ A[0]; B[2]; B[3] }"))
3503 return -1;
3504 if (test_get_list_set_from_union_set(ctx, "{ A[0]; A[2]; B[3] }"))
3505 return -1;
3506 if (test_get_list_basic_map_from_map(ctx,
3507 "{ [0] -> [0]; [2] -> [0]; [3] -> [0] }"))
3508 return -1;
3509 if (test_get_list_map_from_union_map(ctx,
3510 "{ A[0] -> [0]; A[2] -> [0]; B[3] -> [0] }"))
3511 return -1;
3513 return 0;
3516 static int test_lift(isl_ctx *ctx)
3518 const char *str;
3519 isl_basic_map *bmap;
3520 isl_basic_set *bset;
3522 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3523 bset = isl_basic_set_read_from_str(ctx, str);
3524 bset = isl_basic_set_lift(bset);
3525 bmap = isl_basic_map_from_range(bset);
3526 bset = isl_basic_map_domain(bmap);
3527 isl_basic_set_free(bset);
3529 return 0;
3532 /* Check that isl_set_is_subset is not confused by identical
3533 * integer divisions.
3534 * The call to isl_set_normalize ensures that the equality constraints
3535 * a = b = 0 are discovered, turning e0 and e1 into identical
3536 * integer divisions. Any further simplification would remove
3537 * the duplicate integer divisions.
3539 static isl_stat test_subset_duplicate_integer_divisions(isl_ctx *ctx)
3541 const char *str;
3542 isl_bool is_subset;
3543 isl_set *set1, *set2;
3545 str = "{ [a, b, c, d] : "
3546 "exists (e0 = floor((a + d)/4), e1 = floor((d)/4), "
3547 "e2 = floor((-a - d + 4 *floor((a + d)/4))/10), "
3548 "e3 = floor((-d + 4*floor((d)/4))/10): "
3549 "10e2 = -a - 2c - d + 4e0 and 10e3 = -2c - d + 4e1 and "
3550 "b >= 0 and a <= 0 and b <= a) }";
3551 set1 = isl_set_read_from_str(ctx, str);
3552 set2 = isl_set_read_from_str(ctx, str);
3553 set2 = isl_set_normalize(set2);
3555 is_subset = isl_set_is_subset(set1, set2);
3557 isl_set_free(set1);
3558 isl_set_free(set2);
3560 if (is_subset < 0)
3561 return isl_stat_error;
3562 if (!is_subset)
3563 isl_die(ctx, isl_error_unknown,
3564 "set is not considered to be a subset of itself",
3565 return isl_stat_error);
3567 return isl_stat_ok;
3570 struct {
3571 const char *set1;
3572 const char *set2;
3573 int subset;
3574 } subset_tests[] = {
3575 { "{ [112, 0] }",
3576 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3577 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3578 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3579 { "{ [65] }",
3580 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3581 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3582 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3583 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3584 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3585 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3586 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3587 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3588 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3589 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3590 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3591 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3592 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3593 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3594 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3595 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3596 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3597 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3598 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3599 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3600 "4e0 <= -57 + i0 + i1)) or "
3601 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3602 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3603 "4e0 >= -61 + i0 + i1)) or "
3604 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3605 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3608 static int test_subset(isl_ctx *ctx)
3610 int i;
3611 isl_set *set1, *set2;
3612 int subset;
3614 if (test_subset_duplicate_integer_divisions(ctx) < 0)
3615 return -1;
3617 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3618 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3619 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3620 subset = isl_set_is_subset(set1, set2);
3621 isl_set_free(set1);
3622 isl_set_free(set2);
3623 if (subset < 0)
3624 return -1;
3625 if (subset != subset_tests[i].subset)
3626 isl_die(ctx, isl_error_unknown,
3627 "incorrect subset result", return -1);
3630 return 0;
3633 struct {
3634 const char *minuend;
3635 const char *subtrahend;
3636 const char *difference;
3637 } subtract_domain_tests[] = {
3638 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3639 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3640 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3643 static int test_subtract(isl_ctx *ctx)
3645 int i;
3646 isl_union_map *umap1, *umap2;
3647 isl_union_pw_multi_aff *upma1, *upma2;
3648 isl_union_set *uset;
3649 int equal;
3651 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3652 umap1 = isl_union_map_read_from_str(ctx,
3653 subtract_domain_tests[i].minuend);
3654 uset = isl_union_set_read_from_str(ctx,
3655 subtract_domain_tests[i].subtrahend);
3656 umap2 = isl_union_map_read_from_str(ctx,
3657 subtract_domain_tests[i].difference);
3658 umap1 = isl_union_map_subtract_domain(umap1, uset);
3659 equal = isl_union_map_is_equal(umap1, umap2);
3660 isl_union_map_free(umap1);
3661 isl_union_map_free(umap2);
3662 if (equal < 0)
3663 return -1;
3664 if (!equal)
3665 isl_die(ctx, isl_error_unknown,
3666 "incorrect subtract domain result", return -1);
3669 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3670 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3671 subtract_domain_tests[i].minuend);
3672 uset = isl_union_set_read_from_str(ctx,
3673 subtract_domain_tests[i].subtrahend);
3674 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3675 subtract_domain_tests[i].difference);
3676 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3677 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3678 isl_union_pw_multi_aff_free(upma1);
3679 isl_union_pw_multi_aff_free(upma2);
3680 if (equal < 0)
3681 return -1;
3682 if (!equal)
3683 isl_die(ctx, isl_error_unknown,
3684 "incorrect subtract domain result", return -1);
3687 return 0;
3690 /* Check that intersecting the empty basic set with another basic set
3691 * does not increase the number of constraints. In particular,
3692 * the empty basic set should maintain its canonical representation.
3694 static int test_intersect_1(isl_ctx *ctx)
3696 int n1, n2;
3697 isl_basic_set *bset1, *bset2;
3699 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3700 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3701 n1 = isl_basic_set_n_constraint(bset1);
3702 bset1 = isl_basic_set_intersect(bset1, bset2);
3703 n2 = isl_basic_set_n_constraint(bset1);
3704 isl_basic_set_free(bset1);
3705 if (!bset1)
3706 return -1;
3707 if (n1 != n2)
3708 isl_die(ctx, isl_error_unknown,
3709 "number of constraints of empty set changed",
3710 return -1);
3712 return 0;
3715 /* Check that intersecting a set with itself does not cause
3716 * an explosion in the number of disjuncts.
3718 static isl_stat test_intersect_2(isl_ctx *ctx)
3720 int i;
3721 isl_set *set;
3723 set = isl_set_read_from_str(ctx, "{ [x,y] : x >= 0 or y >= 0 }");
3724 for (i = 0; i < 100; ++i)
3725 set = isl_set_intersect(set, isl_set_copy(set));
3726 isl_set_free(set);
3727 if (!set)
3728 return isl_stat_error;
3729 return isl_stat_ok;
3732 /* Perform some intersection tests.
3734 static int test_intersect(isl_ctx *ctx)
3736 if (test_intersect_1(ctx) < 0)
3737 return -1;
3738 if (test_intersect_2(ctx) < 0)
3739 return -1;
3741 return 0;
3744 int test_factorize(isl_ctx *ctx)
3746 const char *str;
3747 isl_basic_set *bset;
3748 isl_factorizer *f;
3750 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3751 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3752 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3753 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3754 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3755 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3756 "3i5 >= -2i0 - i2 + 3i4 }";
3757 bset = isl_basic_set_read_from_str(ctx, str);
3758 f = isl_basic_set_factorizer(bset);
3759 isl_basic_set_free(bset);
3760 isl_factorizer_free(f);
3761 if (!f)
3762 isl_die(ctx, isl_error_unknown,
3763 "failed to construct factorizer", return -1);
3765 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3766 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3767 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3768 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3769 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3770 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3771 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3772 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3773 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3774 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3775 bset = isl_basic_set_read_from_str(ctx, str);
3776 f = isl_basic_set_factorizer(bset);
3777 isl_basic_set_free(bset);
3778 isl_factorizer_free(f);
3779 if (!f)
3780 isl_die(ctx, isl_error_unknown,
3781 "failed to construct factorizer", return -1);
3783 return 0;
3786 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3788 int *injective = user;
3790 *injective = isl_map_is_injective(map);
3791 isl_map_free(map);
3793 if (*injective < 0 || !*injective)
3794 return isl_stat_error;
3796 return isl_stat_ok;
3799 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3800 const char *r, const char *s, int tilable, int parallel)
3802 int i;
3803 isl_union_set *D;
3804 isl_union_map *W, *R, *S;
3805 isl_union_map *empty;
3806 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3807 isl_union_map *validity, *proximity, *coincidence;
3808 isl_union_map *schedule;
3809 isl_union_map *test;
3810 isl_union_set *delta;
3811 isl_union_set *domain;
3812 isl_set *delta_set;
3813 isl_set *slice;
3814 isl_set *origin;
3815 isl_schedule_constraints *sc;
3816 isl_schedule *sched;
3817 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3819 D = isl_union_set_read_from_str(ctx, d);
3820 W = isl_union_map_read_from_str(ctx, w);
3821 R = isl_union_map_read_from_str(ctx, r);
3822 S = isl_union_map_read_from_str(ctx, s);
3824 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3825 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3827 empty = isl_union_map_empty(isl_union_map_get_space(S));
3828 isl_union_map_compute_flow(isl_union_map_copy(R),
3829 isl_union_map_copy(W), empty,
3830 isl_union_map_copy(S),
3831 &dep_raw, NULL, NULL, NULL);
3832 isl_union_map_compute_flow(isl_union_map_copy(W),
3833 isl_union_map_copy(W),
3834 isl_union_map_copy(R),
3835 isl_union_map_copy(S),
3836 &dep_waw, &dep_war, NULL, NULL);
3838 dep = isl_union_map_union(dep_waw, dep_war);
3839 dep = isl_union_map_union(dep, dep_raw);
3840 validity = isl_union_map_copy(dep);
3841 coincidence = isl_union_map_copy(dep);
3842 proximity = isl_union_map_copy(dep);
3844 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3845 sc = isl_schedule_constraints_set_validity(sc, validity);
3846 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3847 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3848 sched = isl_schedule_constraints_compute_schedule(sc);
3849 schedule = isl_schedule_get_map(sched);
3850 isl_schedule_free(sched);
3851 isl_union_map_free(W);
3852 isl_union_map_free(R);
3853 isl_union_map_free(S);
3855 is_injection = 1;
3856 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3858 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3859 is_complete = isl_union_set_is_subset(D, domain);
3860 isl_union_set_free(D);
3861 isl_union_set_free(domain);
3863 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3864 test = isl_union_map_apply_range(test, dep);
3865 test = isl_union_map_apply_range(test, schedule);
3867 delta = isl_union_map_deltas(test);
3868 if (isl_union_set_n_set(delta) == 0) {
3869 is_tilable = 1;
3870 is_parallel = 1;
3871 is_nonneg = 1;
3872 isl_union_set_free(delta);
3873 } else {
3874 delta_set = isl_set_from_union_set(delta);
3876 slice = isl_set_universe(isl_set_get_space(delta_set));
3877 for (i = 0; i < tilable; ++i)
3878 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3879 is_tilable = isl_set_is_subset(delta_set, slice);
3880 isl_set_free(slice);
3882 slice = isl_set_universe(isl_set_get_space(delta_set));
3883 for (i = 0; i < parallel; ++i)
3884 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3885 is_parallel = isl_set_is_subset(delta_set, slice);
3886 isl_set_free(slice);
3888 origin = isl_set_universe(isl_set_get_space(delta_set));
3889 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3890 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3892 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3893 delta_set = isl_set_lexmin(delta_set);
3895 is_nonneg = isl_set_is_equal(delta_set, origin);
3897 isl_set_free(origin);
3898 isl_set_free(delta_set);
3901 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3902 is_injection < 0 || is_complete < 0)
3903 return -1;
3904 if (!is_complete)
3905 isl_die(ctx, isl_error_unknown,
3906 "generated schedule incomplete", return -1);
3907 if (!is_injection)
3908 isl_die(ctx, isl_error_unknown,
3909 "generated schedule not injective on each statement",
3910 return -1);
3911 if (!is_nonneg)
3912 isl_die(ctx, isl_error_unknown,
3913 "negative dependences in generated schedule",
3914 return -1);
3915 if (!is_tilable)
3916 isl_die(ctx, isl_error_unknown,
3917 "generated schedule not as tilable as expected",
3918 return -1);
3919 if (!is_parallel)
3920 isl_die(ctx, isl_error_unknown,
3921 "generated schedule not as parallel as expected",
3922 return -1);
3924 return 0;
3927 /* Compute a schedule for the given instance set, validity constraints,
3928 * proximity constraints and context and return a corresponding union map
3929 * representation.
3931 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3932 const char *domain, const char *validity, const char *proximity,
3933 const char *context)
3935 isl_set *con;
3936 isl_union_set *dom;
3937 isl_union_map *dep;
3938 isl_union_map *prox;
3939 isl_schedule_constraints *sc;
3940 isl_schedule *schedule;
3941 isl_union_map *sched;
3943 con = isl_set_read_from_str(ctx, context);
3944 dom = isl_union_set_read_from_str(ctx, domain);
3945 dep = isl_union_map_read_from_str(ctx, validity);
3946 prox = isl_union_map_read_from_str(ctx, proximity);
3947 sc = isl_schedule_constraints_on_domain(dom);
3948 sc = isl_schedule_constraints_set_context(sc, con);
3949 sc = isl_schedule_constraints_set_validity(sc, dep);
3950 sc = isl_schedule_constraints_set_proximity(sc, prox);
3951 schedule = isl_schedule_constraints_compute_schedule(sc);
3952 sched = isl_schedule_get_map(schedule);
3953 isl_schedule_free(schedule);
3955 return sched;
3958 /* Compute a schedule for the given instance set, validity constraints and
3959 * proximity constraints and return a corresponding union map representation.
3961 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3962 const char *domain, const char *validity, const char *proximity)
3964 return compute_schedule_with_context(ctx, domain, validity, proximity,
3965 "{ : }");
3968 /* Check that a schedule can be constructed on the given domain
3969 * with the given validity and proximity constraints.
3971 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3972 const char *validity, const char *proximity)
3974 isl_union_map *sched;
3976 sched = compute_schedule(ctx, domain, validity, proximity);
3977 if (!sched)
3978 return -1;
3980 isl_union_map_free(sched);
3981 return 0;
3984 int test_special_schedule(isl_ctx *ctx, const char *domain,
3985 const char *validity, const char *proximity, const char *expected_sched)
3987 isl_union_map *sched1, *sched2;
3988 int equal;
3990 sched1 = compute_schedule(ctx, domain, validity, proximity);
3991 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3993 equal = isl_union_map_is_equal(sched1, sched2);
3994 isl_union_map_free(sched1);
3995 isl_union_map_free(sched2);
3997 if (equal < 0)
3998 return -1;
3999 if (!equal)
4000 isl_die(ctx, isl_error_unknown, "unexpected schedule",
4001 return -1);
4003 return 0;
4006 /* Check that the schedule map is properly padded, i.e., that the range
4007 * lives in a single space.
4009 static int test_padded_schedule(isl_ctx *ctx)
4011 const char *str;
4012 isl_union_set *D;
4013 isl_union_map *validity, *proximity;
4014 isl_schedule_constraints *sc;
4015 isl_schedule *sched;
4016 isl_union_map *umap;
4017 isl_union_set *range;
4018 isl_set *set;
4020 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
4021 D = isl_union_set_read_from_str(ctx, str);
4022 validity = isl_union_map_empty(isl_union_set_get_space(D));
4023 proximity = isl_union_map_copy(validity);
4024 sc = isl_schedule_constraints_on_domain(D);
4025 sc = isl_schedule_constraints_set_validity(sc, validity);
4026 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4027 sched = isl_schedule_constraints_compute_schedule(sc);
4028 umap = isl_schedule_get_map(sched);
4029 isl_schedule_free(sched);
4030 range = isl_union_map_range(umap);
4031 set = isl_set_from_union_set(range);
4032 isl_set_free(set);
4034 if (!set)
4035 return -1;
4037 return 0;
4040 /* Check that conditional validity constraints are also taken into
4041 * account across bands.
4042 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
4043 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
4044 * and then check that the adjacent order constraint C[2,1]->D[2,0]
4045 * is enforced by the rest of the schedule.
4047 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
4049 const char *str;
4050 isl_union_set *domain;
4051 isl_union_map *validity, *proximity, *condition;
4052 isl_union_map *sink, *source, *dep;
4053 isl_schedule_constraints *sc;
4054 isl_schedule *schedule;
4055 isl_union_access_info *access;
4056 isl_union_flow *flow;
4057 int empty;
4059 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4060 "A[k] : k >= 1 and k <= -1 + n; "
4061 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4062 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
4063 domain = isl_union_set_read_from_str(ctx, str);
4064 sc = isl_schedule_constraints_on_domain(domain);
4065 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
4066 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4067 "D[k, i] -> C[1 + k, i] : "
4068 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4069 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
4070 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
4071 validity = isl_union_map_read_from_str(ctx, str);
4072 sc = isl_schedule_constraints_set_validity(sc, validity);
4073 str = "[n] -> { C[k, i] -> D[k, i] : "
4074 "0 <= i <= -1 + k and k <= -1 + n }";
4075 proximity = isl_union_map_read_from_str(ctx, str);
4076 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4077 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
4078 "i <= -1 + k and i >= 1 and k <= -2 + n; "
4079 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
4080 "k <= -1 + n and i >= 0 and i <= -2 + k }";
4081 condition = isl_union_map_read_from_str(ctx, str);
4082 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
4083 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4084 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
4085 "i >= 0 and i <= -1 + k and k <= -1 + n and "
4086 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
4087 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
4088 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4089 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
4090 "k <= -1 + n and i >= 0 and i <= -1 + k and "
4091 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
4092 validity = isl_union_map_read_from_str(ctx, str);
4093 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4094 validity);
4095 schedule = isl_schedule_constraints_compute_schedule(sc);
4096 str = "{ D[2,0] -> [] }";
4097 sink = isl_union_map_read_from_str(ctx, str);
4098 access = isl_union_access_info_from_sink(sink);
4099 str = "{ C[2,1] -> [] }";
4100 source = isl_union_map_read_from_str(ctx, str);
4101 access = isl_union_access_info_set_must_source(access, source);
4102 access = isl_union_access_info_set_schedule(access, schedule);
4103 flow = isl_union_access_info_compute_flow(access);
4104 dep = isl_union_flow_get_must_dependence(flow);
4105 isl_union_flow_free(flow);
4106 empty = isl_union_map_is_empty(dep);
4107 isl_union_map_free(dep);
4109 if (empty < 0)
4110 return -1;
4111 if (empty)
4112 isl_die(ctx, isl_error_unknown,
4113 "conditional validity not respected", return -1);
4115 return 0;
4118 /* Check that the test for violated conditional validity constraints
4119 * is not confused by domain compression.
4120 * In particular, earlier versions of isl would apply
4121 * a schedule on the compressed domains to the original domains,
4122 * resulting in a failure to detect that the default schedule
4123 * violates the conditional validity constraints.
4125 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
4127 const char *str;
4128 isl_bool empty;
4129 isl_union_set *domain;
4130 isl_union_map *validity, *condition;
4131 isl_schedule_constraints *sc;
4132 isl_schedule *schedule;
4133 isl_union_map *umap;
4134 isl_map *map, *ge;
4136 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
4137 domain = isl_union_set_read_from_str(ctx, str);
4138 sc = isl_schedule_constraints_on_domain(domain);
4139 str = "{ B[1, i] -> A[0, i + 1] }";
4140 condition = isl_union_map_read_from_str(ctx, str);
4141 str = "{ A[0, i] -> B[1, i - 1] }";
4142 validity = isl_union_map_read_from_str(ctx, str);
4143 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4144 isl_union_map_copy(validity));
4145 schedule = isl_schedule_constraints_compute_schedule(sc);
4146 umap = isl_schedule_get_map(schedule);
4147 isl_schedule_free(schedule);
4148 validity = isl_union_map_apply_domain(validity,
4149 isl_union_map_copy(umap));
4150 validity = isl_union_map_apply_range(validity, umap);
4151 map = isl_map_from_union_map(validity);
4152 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4153 map = isl_map_intersect(map, ge);
4154 empty = isl_map_is_empty(map);
4155 isl_map_free(map);
4157 if (empty < 0)
4158 return -1;
4159 if (!empty)
4160 isl_die(ctx, isl_error_unknown,
4161 "conditional validity constraints not satisfied",
4162 return -1);
4164 return 0;
4167 /* Input for testing of schedule construction based on
4168 * conditional constraints.
4170 * domain is the iteration domain
4171 * flow are the flow dependences, which determine the validity and
4172 * proximity constraints
4173 * condition are the conditions on the conditional validity constraints
4174 * conditional_validity are the conditional validity constraints
4175 * outer_band_n is the expected number of members in the outer band
4177 struct {
4178 const char *domain;
4179 const char *flow;
4180 const char *condition;
4181 const char *conditional_validity;
4182 int outer_band_n;
4183 } live_range_tests[] = {
4184 /* Contrived example that illustrates that we need to keep
4185 * track of tagged condition dependences and
4186 * tagged conditional validity dependences
4187 * in isl_sched_edge separately.
4188 * In particular, the conditional validity constraints on A
4189 * cannot be satisfied,
4190 * but they can be ignored because there are no corresponding
4191 * condition constraints. However, we do have an additional
4192 * conditional validity constraint that maps to the same
4193 * dependence relation
4194 * as the condition constraint on B. If we did not make a distinction
4195 * between tagged condition and tagged conditional validity
4196 * dependences, then we
4197 * could end up treating this shared dependence as an condition
4198 * constraint on A, forcing a localization of the conditions,
4199 * which is impossible.
4201 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
4202 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
4203 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
4204 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4205 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4206 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
4209 /* TACO 2013 Fig. 7 */
4210 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4211 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4212 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4213 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
4214 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
4215 "0 <= i < n and 0 <= j < n - 1 }",
4216 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
4217 "0 <= i < n and 0 <= j < j' < n;"
4218 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
4219 "0 <= i < i' < n and 0 <= j,j' < n;"
4220 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
4221 "0 <= i,j,j' < n and j < j' }",
4224 /* TACO 2013 Fig. 7, without tags */
4225 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4226 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4227 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4228 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4229 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4230 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
4231 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
4232 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
4235 /* TACO 2013 Fig. 12 */
4236 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
4237 "S3[i,3] : 0 <= i <= 1 }",
4238 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
4239 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
4240 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
4241 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
4242 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4243 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
4244 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4245 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
4246 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
4247 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
4248 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
4253 /* Test schedule construction based on conditional constraints.
4254 * In particular, check the number of members in the outer band node
4255 * as an indication of whether tiling is possible or not.
4257 static int test_conditional_schedule_constraints(isl_ctx *ctx)
4259 int i;
4260 isl_union_set *domain;
4261 isl_union_map *condition;
4262 isl_union_map *flow;
4263 isl_union_map *validity;
4264 isl_schedule_constraints *sc;
4265 isl_schedule *schedule;
4266 isl_schedule_node *node;
4267 int n_member;
4269 if (test_special_conditional_schedule_constraints(ctx) < 0)
4270 return -1;
4271 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
4272 return -1;
4274 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
4275 domain = isl_union_set_read_from_str(ctx,
4276 live_range_tests[i].domain);
4277 flow = isl_union_map_read_from_str(ctx,
4278 live_range_tests[i].flow);
4279 condition = isl_union_map_read_from_str(ctx,
4280 live_range_tests[i].condition);
4281 validity = isl_union_map_read_from_str(ctx,
4282 live_range_tests[i].conditional_validity);
4283 sc = isl_schedule_constraints_on_domain(domain);
4284 sc = isl_schedule_constraints_set_validity(sc,
4285 isl_union_map_copy(flow));
4286 sc = isl_schedule_constraints_set_proximity(sc, flow);
4287 sc = isl_schedule_constraints_set_conditional_validity(sc,
4288 condition, validity);
4289 schedule = isl_schedule_constraints_compute_schedule(sc);
4290 node = isl_schedule_get_root(schedule);
4291 while (node &&
4292 isl_schedule_node_get_type(node) != isl_schedule_node_band)
4293 node = isl_schedule_node_first_child(node);
4294 n_member = isl_schedule_node_band_n_member(node);
4295 isl_schedule_node_free(node);
4296 isl_schedule_free(schedule);
4298 if (!schedule)
4299 return -1;
4300 if (n_member != live_range_tests[i].outer_band_n)
4301 isl_die(ctx, isl_error_unknown,
4302 "unexpected number of members in outer band",
4303 return -1);
4305 return 0;
4308 /* Check that the schedule computed for the given instance set and
4309 * dependence relation strongly satisfies the dependences.
4310 * In particular, check that no instance is scheduled before
4311 * or together with an instance on which it depends.
4312 * Earlier versions of isl would produce a schedule that
4313 * only weakly satisfies the dependences.
4315 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
4317 const char *domain, *dep;
4318 isl_union_map *D, *schedule;
4319 isl_map *map, *ge;
4320 int empty;
4322 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
4323 "A[i0] : 0 <= i0 <= 1 }";
4324 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
4325 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
4326 schedule = compute_schedule(ctx, domain, dep, dep);
4327 D = isl_union_map_read_from_str(ctx, dep);
4328 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
4329 D = isl_union_map_apply_range(D, schedule);
4330 map = isl_map_from_union_map(D);
4331 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4332 map = isl_map_intersect(map, ge);
4333 empty = isl_map_is_empty(map);
4334 isl_map_free(map);
4336 if (empty < 0)
4337 return -1;
4338 if (!empty)
4339 isl_die(ctx, isl_error_unknown,
4340 "dependences not strongly satisfied", return -1);
4342 return 0;
4345 /* Compute a schedule for input where the instance set constraints
4346 * conflict with the context constraints.
4347 * Earlier versions of isl did not properly handle this situation.
4349 static int test_conflicting_context_schedule(isl_ctx *ctx)
4351 isl_union_map *schedule;
4352 const char *domain, *context;
4354 domain = "[n] -> { A[] : n >= 0 }";
4355 context = "[n] -> { : n < 0 }";
4356 schedule = compute_schedule_with_context(ctx,
4357 domain, "{}", "{}", context);
4358 isl_union_map_free(schedule);
4360 if (!schedule)
4361 return -1;
4363 return 0;
4366 /* Check that a set of schedule constraints that only allow for
4367 * a coalescing schedule still produces a schedule even if the user
4368 * request a non-coalescing schedule. Earlier versions of isl
4369 * would not handle this case correctly.
4371 static int test_coalescing_schedule(isl_ctx *ctx)
4373 const char *domain, *dep;
4374 isl_union_set *I;
4375 isl_union_map *D;
4376 isl_schedule_constraints *sc;
4377 isl_schedule *schedule;
4378 int treat_coalescing;
4380 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4381 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4382 I = isl_union_set_read_from_str(ctx, domain);
4383 D = isl_union_map_read_from_str(ctx, dep);
4384 sc = isl_schedule_constraints_on_domain(I);
4385 sc = isl_schedule_constraints_set_validity(sc, D);
4386 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4387 isl_options_set_schedule_treat_coalescing(ctx, 1);
4388 schedule = isl_schedule_constraints_compute_schedule(sc);
4389 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4390 isl_schedule_free(schedule);
4391 if (!schedule)
4392 return -1;
4393 return 0;
4396 /* Check that the scheduler does not perform any needless
4397 * compound skewing. Earlier versions of isl would compute
4398 * schedules in terms of transformed schedule coefficients and
4399 * would not accurately keep track of the sum of the original
4400 * schedule coefficients. It could then produce the schedule
4401 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
4402 * for the input below instead of the schedule below.
4404 static int test_skewing_schedule(isl_ctx *ctx)
4406 const char *D, *V, *P, *S;
4408 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
4409 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
4410 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
4411 "-1 <= a-i + b-j + c-k <= 1 }";
4412 P = "{ }";
4413 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
4415 return test_special_schedule(ctx, D, V, P, S);
4418 int test_schedule(isl_ctx *ctx)
4420 const char *D, *W, *R, *V, *P, *S;
4421 int max_coincidence;
4422 int treat_coalescing;
4424 /* Handle resulting schedule with zero bands. */
4425 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4426 return -1;
4428 /* Jacobi */
4429 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4430 W = "{ S1[t,i] -> a[t,i] }";
4431 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4432 "S1[t,i] -> a[t-1,i+1] }";
4433 S = "{ S1[t,i] -> [t,i] }";
4434 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4435 return -1;
4437 /* Fig. 5 of CC2008 */
4438 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
4439 "j <= -1 + N }";
4440 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
4441 "j >= 2 and j <= -1 + N }";
4442 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
4443 "j >= 2 and j <= -1 + N; "
4444 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
4445 "j >= 2 and j <= -1 + N }";
4446 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4447 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4448 return -1;
4450 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
4451 W = "{ S1[i] -> a[i] }";
4452 R = "{ S2[i] -> a[i+1] }";
4453 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4454 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4455 return -1;
4457 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
4458 W = "{ S1[i] -> a[i] }";
4459 R = "{ S2[i] -> a[9-i] }";
4460 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4461 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4462 return -1;
4464 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4465 W = "{ S1[i] -> a[i] }";
4466 R = "[N] -> { S2[i] -> a[N-1-i] }";
4467 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4468 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4469 return -1;
4471 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4472 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4473 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4474 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4475 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4476 return -1;
4478 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4479 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4480 R = "{ S2[i,j] -> a[i-1,j] }";
4481 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4482 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4483 return -1;
4485 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4486 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4487 R = "{ S2[i,j] -> a[i,j-1] }";
4488 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4489 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4490 return -1;
4492 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4493 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4494 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4495 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4496 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4497 "S_0[] -> [0, 0, 0] }";
4498 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4499 return -1;
4500 ctx->opt->schedule_parametric = 0;
4501 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4502 return -1;
4503 ctx->opt->schedule_parametric = 1;
4505 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4506 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4507 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4508 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4509 "S4[i] -> a[i,N] }";
4510 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4511 "S4[i] -> [4,i,0] }";
4512 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4513 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4514 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4515 return -1;
4516 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4518 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4519 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4520 "j <= N }";
4521 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4522 "j <= N; "
4523 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4524 "j <= N }";
4525 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4526 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4527 return -1;
4529 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4530 " S_2[t] : t >= 0 and t <= -1 + N; "
4531 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4532 "i <= -1 + N }";
4533 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4534 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4535 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4536 "i >= 0 and i <= -1 + N }";
4537 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4538 "i >= 0 and i <= -1 + N; "
4539 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4540 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4541 " S_0[t] -> [0, t, 0] }";
4543 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4544 return -1;
4545 ctx->opt->schedule_parametric = 0;
4546 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4547 return -1;
4548 ctx->opt->schedule_parametric = 1;
4550 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4551 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4552 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4553 return -1;
4555 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4556 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4557 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
4558 "j >= 0 and j <= -1 + N; "
4559 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4560 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4561 "j >= 0 and j <= -1 + N; "
4562 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4563 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4564 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4565 return -1;
4567 D = "{ S_0[i] : i >= 0 }";
4568 W = "{ S_0[i] -> a[i] : i >= 0 }";
4569 R = "{ S_0[i] -> a[0] : i >= 0 }";
4570 S = "{ S_0[i] -> [0, i, 0] }";
4571 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4572 return -1;
4574 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4575 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4576 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4577 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4578 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4579 return -1;
4581 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4582 "k <= -1 + n and k >= 0 }";
4583 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4584 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4585 "k <= -1 + n and k >= 0; "
4586 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4587 "k <= -1 + n and k >= 0; "
4588 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4589 "k <= -1 + n and k >= 0 }";
4590 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4591 ctx->opt->schedule_outer_coincidence = 1;
4592 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4593 return -1;
4594 ctx->opt->schedule_outer_coincidence = 0;
4596 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4597 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4598 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4599 "Stmt_for_body24[i0, i1, 1, 0]:"
4600 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4601 "Stmt_for_body7[i0, i1, i2]:"
4602 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4603 "i2 <= 7 }";
4605 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4606 "Stmt_for_body24[1, i1, i2, i3]:"
4607 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4608 "i2 >= 1;"
4609 "Stmt_for_body24[0, i1, i2, i3] -> "
4610 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4611 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4612 "i3 >= 0;"
4613 "Stmt_for_body24[0, i1, i2, i3] ->"
4614 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4615 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4616 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4617 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4618 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4619 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4620 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4621 "i1 <= 6 and i1 >= 0;"
4622 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4623 "Stmt_for_body7[i0, i1, i2] -> "
4624 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4625 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4626 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4627 "Stmt_for_body7[i0, i1, i2] -> "
4628 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4629 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4630 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4631 P = V;
4633 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4634 isl_options_set_schedule_treat_coalescing(ctx, 0);
4635 if (test_has_schedule(ctx, D, V, P) < 0)
4636 return -1;
4637 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4639 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4640 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4641 "j >= 1 and j <= 7;"
4642 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4643 "j >= 1 and j <= 8 }";
4644 P = "{ }";
4645 S = "{ S_0[i, j] -> [i + j, i] }";
4646 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4647 if (test_special_schedule(ctx, D, V, P, S) < 0)
4648 return -1;
4649 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4651 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4652 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4653 "j >= 0 and j <= -1 + i }";
4654 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4655 "i <= -1 + N and j >= 0;"
4656 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4657 "i <= -2 + N }";
4658 P = "{ }";
4659 S = "{ S_0[i, j] -> [i, j] }";
4660 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4661 if (test_special_schedule(ctx, D, V, P, S) < 0)
4662 return -1;
4663 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4665 /* Test both algorithms on a case with only proximity dependences. */
4666 D = "{ S[i,j] : 0 <= i <= 10 }";
4667 V = "{ }";
4668 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4669 S = "{ S[i, j] -> [j, i] }";
4670 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4671 if (test_special_schedule(ctx, D, V, P, S) < 0)
4672 return -1;
4673 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4674 if (test_special_schedule(ctx, D, V, P, S) < 0)
4675 return -1;
4677 D = "{ A[a]; B[] }";
4678 V = "{}";
4679 P = "{ A[a] -> B[] }";
4680 if (test_has_schedule(ctx, D, V, P) < 0)
4681 return -1;
4683 if (test_padded_schedule(ctx) < 0)
4684 return -1;
4686 /* Check that check for progress is not confused by rational
4687 * solution.
4689 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4690 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4691 "i0 <= -2 + N; "
4692 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4693 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4694 P = "{}";
4695 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4696 if (test_has_schedule(ctx, D, V, P) < 0)
4697 return -1;
4698 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4700 /* Check that we allow schedule rows that are only non-trivial
4701 * on some full-dimensional domains.
4703 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4704 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4705 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4706 P = "{}";
4707 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4708 if (test_has_schedule(ctx, D, V, P) < 0)
4709 return -1;
4710 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4712 if (test_conditional_schedule_constraints(ctx) < 0)
4713 return -1;
4715 if (test_strongly_satisfying_schedule(ctx) < 0)
4716 return -1;
4718 if (test_conflicting_context_schedule(ctx) < 0)
4719 return -1;
4721 if (test_coalescing_schedule(ctx) < 0)
4722 return -1;
4723 if (test_skewing_schedule(ctx) < 0)
4724 return -1;
4726 return 0;
4729 /* Perform scheduling tests using the whole component scheduler.
4731 static int test_schedule_whole(isl_ctx *ctx)
4733 int whole;
4734 int r;
4736 whole = isl_options_get_schedule_whole_component(ctx);
4737 isl_options_set_schedule_whole_component(ctx, 1);
4738 r = test_schedule(ctx);
4739 isl_options_set_schedule_whole_component(ctx, whole);
4741 return r;
4744 /* Perform scheduling tests using the incremental scheduler.
4746 static int test_schedule_incremental(isl_ctx *ctx)
4748 int whole;
4749 int r;
4751 whole = isl_options_get_schedule_whole_component(ctx);
4752 isl_options_set_schedule_whole_component(ctx, 0);
4753 r = test_schedule(ctx);
4754 isl_options_set_schedule_whole_component(ctx, whole);
4756 return r;
4759 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4761 isl_union_map *umap;
4762 int test;
4764 umap = isl_union_map_read_from_str(ctx, str);
4765 test = isl_union_map_plain_is_injective(umap);
4766 isl_union_map_free(umap);
4767 if (test < 0)
4768 return -1;
4769 if (test == injective)
4770 return 0;
4771 if (injective)
4772 isl_die(ctx, isl_error_unknown,
4773 "map not detected as injective", return -1);
4774 else
4775 isl_die(ctx, isl_error_unknown,
4776 "map detected as injective", return -1);
4779 int test_injective(isl_ctx *ctx)
4781 const char *str;
4783 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4784 return -1;
4785 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4786 return -1;
4787 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4788 return -1;
4789 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4790 return -1;
4791 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4792 return -1;
4793 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4794 return -1;
4795 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4796 return -1;
4797 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4798 return -1;
4800 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4801 if (test_plain_injective(ctx, str, 1))
4802 return -1;
4803 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4804 if (test_plain_injective(ctx, str, 0))
4805 return -1;
4807 return 0;
4810 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4812 isl_aff *aff2;
4813 int equal;
4815 if (!aff)
4816 return -1;
4818 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4819 equal = isl_aff_plain_is_equal(aff, aff2);
4820 isl_aff_free(aff2);
4822 return equal;
4825 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4827 int equal;
4829 equal = aff_plain_is_equal(aff, str);
4830 if (equal < 0)
4831 return -1;
4832 if (!equal)
4833 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4834 "result not as expected", return -1);
4835 return 0;
4838 /* Is "upa" obviously equal to the isl_union_pw_aff represented by "str"?
4840 static isl_bool union_pw_aff_plain_is_equal(__isl_keep isl_union_pw_aff *upa,
4841 const char *str)
4843 isl_ctx *ctx;
4844 isl_union_pw_aff *upa2;
4845 isl_bool equal;
4847 if (!upa)
4848 return isl_bool_error;
4850 ctx = isl_union_pw_aff_get_ctx(upa);
4851 upa2 = isl_union_pw_aff_read_from_str(ctx, str);
4852 equal = isl_union_pw_aff_plain_is_equal(upa, upa2);
4853 isl_union_pw_aff_free(upa2);
4855 return equal;
4858 /* Check that "upa" is obviously equal to the isl_union_pw_aff
4859 * represented by "str".
4861 static isl_stat union_pw_aff_check_plain_equal(__isl_keep isl_union_pw_aff *upa,
4862 const char *str)
4864 isl_bool equal;
4866 equal = union_pw_aff_plain_is_equal(upa, str);
4867 if (equal < 0)
4868 return isl_stat_error;
4869 if (!equal)
4870 isl_die(isl_union_pw_aff_get_ctx(upa), isl_error_unknown,
4871 "result not as expected", return isl_stat_error);
4872 return isl_stat_ok;
4875 /* Basic tests on isl_union_pw_aff.
4877 * In particular, check that isl_union_pw_aff_aff_on_domain
4878 * aligns the parameters of the input objects and
4879 * that isl_union_pw_aff_param_on_domain_id properly
4880 * introduces the parameter.
4882 static int test_upa(isl_ctx *ctx)
4884 const char *str;
4885 isl_id *id;
4886 isl_aff *aff;
4887 isl_union_set *domain;
4888 isl_union_pw_aff *upa;
4889 isl_stat ok;
4891 aff = isl_aff_read_from_str(ctx, "[N] -> { [N] }");
4892 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
4893 domain = isl_union_set_read_from_str(ctx, str);
4894 upa = isl_union_pw_aff_aff_on_domain(domain, aff);
4895 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
4896 ok = union_pw_aff_check_plain_equal(upa, str);
4897 isl_union_pw_aff_free(upa);
4898 if (ok < 0)
4899 return -1;
4901 id = isl_id_alloc(ctx, "N", NULL);
4902 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
4903 domain = isl_union_set_read_from_str(ctx, str);
4904 upa = isl_union_pw_aff_param_on_domain_id(domain, id);
4905 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
4906 ok = union_pw_aff_check_plain_equal(upa, str);
4907 isl_union_pw_aff_free(upa);
4908 if (ok < 0)
4909 return -1;
4911 return 0;
4914 struct {
4915 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4916 __isl_take isl_aff *aff2);
4917 } aff_bin_op[] = {
4918 ['+'] = { &isl_aff_add },
4919 ['-'] = { &isl_aff_sub },
4920 ['*'] = { &isl_aff_mul },
4921 ['/'] = { &isl_aff_div },
4924 struct {
4925 const char *arg1;
4926 unsigned char op;
4927 const char *arg2;
4928 const char *res;
4929 } aff_bin_tests[] = {
4930 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4931 "{ [i] -> [2i] }" },
4932 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4933 "{ [i] -> [0] }" },
4934 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4935 "{ [i] -> [2i] }" },
4936 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4937 "{ [i] -> [2i] }" },
4938 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4939 "{ [i] -> [i/2] }" },
4940 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4941 "{ [i] -> [i] }" },
4942 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4943 "{ [i] -> [NaN] }" },
4944 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
4945 "{ [i] -> [NaN] }" },
4946 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
4947 "{ [i] -> [NaN] }" },
4948 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
4949 "{ [i] -> [NaN] }" },
4950 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
4951 "{ [i] -> [NaN] }" },
4952 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
4953 "{ [i] -> [NaN] }" },
4954 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
4955 "{ [i] -> [NaN] }" },
4956 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
4957 "{ [i] -> [NaN] }" },
4958 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
4959 "{ [i] -> [NaN] }" },
4960 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
4961 "{ [i] -> [NaN] }" },
4962 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
4963 "{ [i] -> [NaN] }" },
4964 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
4965 "{ [i] -> [NaN] }" },
4968 /* Perform some basic tests of binary operations on isl_aff objects.
4970 static int test_bin_aff(isl_ctx *ctx)
4972 int i;
4973 isl_aff *aff1, *aff2, *res;
4974 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4975 __isl_take isl_aff *aff2);
4976 int ok;
4978 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
4979 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
4980 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
4981 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
4982 fn = aff_bin_op[aff_bin_tests[i].op].fn;
4983 aff1 = fn(aff1, aff2);
4984 if (isl_aff_is_nan(res))
4985 ok = isl_aff_is_nan(aff1);
4986 else
4987 ok = isl_aff_plain_is_equal(aff1, res);
4988 isl_aff_free(aff1);
4989 isl_aff_free(res);
4990 if (ok < 0)
4991 return -1;
4992 if (!ok)
4993 isl_die(ctx, isl_error_unknown,
4994 "unexpected result", return -1);
4997 return 0;
5000 struct {
5001 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
5002 __isl_take isl_pw_aff *pa2);
5003 } pw_aff_bin_op[] = {
5004 ['m'] = { &isl_pw_aff_min },
5005 ['M'] = { &isl_pw_aff_max },
5008 /* Inputs for binary isl_pw_aff operation tests.
5009 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
5010 * defined by pw_aff_bin_op, and "res" is the expected result.
5012 struct {
5013 const char *arg1;
5014 unsigned char op;
5015 const char *arg2;
5016 const char *res;
5017 } pw_aff_bin_tests[] = {
5018 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
5019 "{ [i] -> [i] }" },
5020 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
5021 "{ [i] -> [i] }" },
5022 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
5023 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
5024 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
5025 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
5026 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
5027 "{ [i] -> [NaN] }" },
5028 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
5029 "{ [i] -> [NaN] }" },
5032 /* Perform some basic tests of binary operations on isl_pw_aff objects.
5034 static int test_bin_pw_aff(isl_ctx *ctx)
5036 int i;
5037 isl_bool ok;
5038 isl_pw_aff *pa1, *pa2, *res;
5040 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
5041 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
5042 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
5043 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
5044 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
5045 if (isl_pw_aff_involves_nan(res))
5046 ok = isl_pw_aff_involves_nan(pa1);
5047 else
5048 ok = isl_pw_aff_plain_is_equal(pa1, res);
5049 isl_pw_aff_free(pa1);
5050 isl_pw_aff_free(res);
5051 if (ok < 0)
5052 return -1;
5053 if (!ok)
5054 isl_die(ctx, isl_error_unknown,
5055 "unexpected result", return -1);
5058 return 0;
5061 struct {
5062 __isl_give isl_union_pw_multi_aff *(*fn)(
5063 __isl_take isl_union_pw_multi_aff *upma1,
5064 __isl_take isl_union_pw_multi_aff *upma2);
5065 const char *arg1;
5066 const char *arg2;
5067 const char *res;
5068 } upma_bin_tests[] = {
5069 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
5070 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
5071 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
5072 "{ B[x] -> [2] : x >= 0 }",
5073 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
5074 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
5075 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
5076 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
5077 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
5078 "D[i] -> B[2] : i >= 5 }" },
5079 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5080 "{ B[x] -> C[2] : x > 0 }",
5081 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
5082 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5083 "{ B[x] -> A[2] : x >= 0 }",
5084 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
5087 /* Perform some basic tests of binary operations on
5088 * isl_union_pw_multi_aff objects.
5090 static int test_bin_upma(isl_ctx *ctx)
5092 int i;
5093 isl_union_pw_multi_aff *upma1, *upma2, *res;
5094 int ok;
5096 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
5097 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5098 upma_bin_tests[i].arg1);
5099 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5100 upma_bin_tests[i].arg2);
5101 res = isl_union_pw_multi_aff_read_from_str(ctx,
5102 upma_bin_tests[i].res);
5103 upma1 = upma_bin_tests[i].fn(upma1, upma2);
5104 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
5105 isl_union_pw_multi_aff_free(upma1);
5106 isl_union_pw_multi_aff_free(res);
5107 if (ok < 0)
5108 return -1;
5109 if (!ok)
5110 isl_die(ctx, isl_error_unknown,
5111 "unexpected result", return -1);
5114 return 0;
5117 struct {
5118 __isl_give isl_union_pw_multi_aff *(*fn)(
5119 __isl_take isl_union_pw_multi_aff *upma1,
5120 __isl_take isl_union_pw_multi_aff *upma2);
5121 const char *arg1;
5122 const char *arg2;
5123 } upma_bin_fail_tests[] = {
5124 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5125 "{ B[x] -> C[2] : x >= 0 }" },
5128 /* Perform some basic tests of binary operations on
5129 * isl_union_pw_multi_aff objects that are expected to fail.
5131 static int test_bin_upma_fail(isl_ctx *ctx)
5133 int i, n;
5134 isl_union_pw_multi_aff *upma1, *upma2;
5135 int on_error;
5137 on_error = isl_options_get_on_error(ctx);
5138 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
5139 n = ARRAY_SIZE(upma_bin_fail_tests);
5140 for (i = 0; i < n; ++i) {
5141 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5142 upma_bin_fail_tests[i].arg1);
5143 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5144 upma_bin_fail_tests[i].arg2);
5145 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
5146 isl_union_pw_multi_aff_free(upma1);
5147 if (upma1)
5148 break;
5150 isl_options_set_on_error(ctx, on_error);
5151 if (i < n)
5152 isl_die(ctx, isl_error_unknown,
5153 "operation not expected to succeed", return -1);
5155 return 0;
5158 /* Inputs for basic tests of unary operations on isl_multi_pw_aff objects.
5159 * "fn" is the function that is tested.
5160 * "arg" is a string description of the input.
5161 * "res" is a string description of the expected result.
5163 struct {
5164 __isl_give isl_multi_pw_aff *(*fn)(__isl_take isl_multi_pw_aff *mpa);
5165 const char *arg;
5166 const char *res;
5167 } mpa_un_tests[] = {
5168 { &isl_multi_pw_aff_range_factor_domain,
5169 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5170 "{ A[x] -> B[(1 : x >= 5)] }" },
5171 { &isl_multi_pw_aff_range_factor_range,
5172 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5173 "{ A[y] -> C[(2 : y <= 10)] }" },
5174 { &isl_multi_pw_aff_range_factor_domain,
5175 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5176 "{ A[x] -> B[(1 : x >= 5)] }" },
5177 { &isl_multi_pw_aff_range_factor_range,
5178 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5179 "{ A[y] -> C[] }" },
5180 { &isl_multi_pw_aff_range_factor_domain,
5181 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5182 "{ A[x] -> B[] }" },
5183 { &isl_multi_pw_aff_range_factor_range,
5184 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5185 "{ A[y] -> C[(2 : y <= 10)] }" },
5186 { &isl_multi_pw_aff_range_factor_domain,
5187 "{ A[x] -> [B[] -> C[]] }",
5188 "{ A[x] -> B[] }" },
5189 { &isl_multi_pw_aff_range_factor_range,
5190 "{ A[x] -> [B[] -> C[]] }",
5191 "{ A[y] -> C[] }" },
5192 { &isl_multi_pw_aff_factor_range,
5193 "{ [B[] -> C[]] }",
5194 "{ C[] }" },
5195 { &isl_multi_pw_aff_range_factor_domain,
5196 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5197 "{ A[x] -> B[] : x >= 0 }" },
5198 { &isl_multi_pw_aff_range_factor_range,
5199 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5200 "{ A[y] -> C[] : y >= 0 }" },
5201 { &isl_multi_pw_aff_factor_range,
5202 "[N] -> { [B[] -> C[]] : N >= 0 }",
5203 "[N] -> { C[] : N >= 0 }" },
5206 /* Perform some basic tests of unary operations on isl_multi_pw_aff objects.
5208 static int test_un_mpa(isl_ctx *ctx)
5210 int i;
5211 isl_bool ok;
5212 isl_multi_pw_aff *mpa, *res;
5214 for (i = 0; i < ARRAY_SIZE(mpa_un_tests); ++i) {
5215 mpa = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].arg);
5216 res = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].res);
5217 mpa = mpa_un_tests[i].fn(mpa);
5218 ok = isl_multi_pw_aff_plain_is_equal(mpa, res);
5219 isl_multi_pw_aff_free(mpa);
5220 isl_multi_pw_aff_free(res);
5221 if (ok < 0)
5222 return -1;
5223 if (!ok)
5224 isl_die(ctx, isl_error_unknown,
5225 "unexpected result", return -1);
5228 return 0;
5231 /* Inputs for basic tests of binary operations on isl_multi_pw_aff objects.
5232 * "fn" is the function that is tested.
5233 * "arg1" and "arg2" are string descriptions of the inputs.
5234 * "res" is a string description of the expected result.
5236 struct {
5237 __isl_give isl_multi_pw_aff *(*fn)(
5238 __isl_take isl_multi_pw_aff *mpa1,
5239 __isl_take isl_multi_pw_aff *mpa2);
5240 const char *arg1;
5241 const char *arg2;
5242 const char *res;
5243 } mpa_bin_tests[] = {
5244 { &isl_multi_pw_aff_add, "{ A[] -> [1] }", "{ A[] -> [2] }",
5245 "{ A[] -> [3] }" },
5246 { &isl_multi_pw_aff_add, "{ A[x] -> [(1 : x >= 5)] }",
5247 "{ A[x] -> [(x : x <= 10)] }",
5248 "{ A[x] -> [(1 + x : 5 <= x <= 10)] }" },
5249 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5250 "{ A[x] -> [] : x <= 10 }",
5251 "{ A[x] -> [] : 5 <= x <= 10 }" },
5252 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5253 "[N] -> { A[x] -> [] : x <= N }",
5254 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5255 { &isl_multi_pw_aff_add,
5256 "[N] -> { A[x] -> [] : x <= N }",
5257 "{ A[x] -> [] : x >= 5 }",
5258 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5259 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5260 "{ A[y] -> C[(2 : y <= 10)] }",
5261 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5262 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5263 "{ A[y] -> C[2] : y <= 10 }",
5264 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5265 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5266 "[N] -> { A[y] -> C[2] : y <= N }",
5267 "[N] -> { A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= N)]] }" },
5268 { &isl_multi_pw_aff_range_product, "[N] -> { A[x] -> B[1] : x >= N }",
5269 "{ A[y] -> C[2] : y <= 10 }",
5270 "[N] -> { A[x] -> [B[(1 : x >= N)] -> C[(2 : x <= 10)]] }" },
5271 { &isl_multi_pw_aff_range_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5272 "{ A[] -> [B[1] -> C[2]] }" },
5273 { &isl_multi_pw_aff_range_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5274 "{ A[] -> [B[] -> C[]] }" },
5275 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5276 "{ A[y] -> C[] : y <= 10 }",
5277 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] : x <= 10 }" },
5278 { &isl_multi_pw_aff_range_product, "{ A[y] -> C[] : y <= 10 }",
5279 "{ A[x] -> B[(1 : x >= 5)] }",
5280 "{ A[x] -> [C[] -> B[(1 : x >= 5)]] : x <= 10 }" },
5281 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5282 "{ A[y] -> C[(2 : y <= 10)] }",
5283 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[(2 : y <= 10)]] }" },
5284 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5285 "{ A[y] -> C[] : y <= 10 }",
5286 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= 10 }" },
5287 { &isl_multi_pw_aff_product, "{ A[y] -> C[] : y <= 10 }",
5288 "{ A[x] -> B[(1 : x >= 5)] }",
5289 "{ [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= 10 }" },
5290 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5291 "[N] -> { A[y] -> C[] : y <= N }",
5292 "[N] -> { [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= N }" },
5293 { &isl_multi_pw_aff_product, "[N] -> { A[y] -> C[] : y <= N }",
5294 "{ A[x] -> B[(1 : x >= 5)] }",
5295 "[N] -> { [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= N }" },
5296 { &isl_multi_pw_aff_product, "{ A[x] -> B[] : x >= 5 }",
5297 "{ A[y] -> C[] : y <= 10 }",
5298 "{ [A[x] -> A[y]] -> [B[] -> C[]] : x >= 5 and y <= 10 }" },
5299 { &isl_multi_pw_aff_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5300 "{ [A[] -> A[]] -> [B[1] -> C[2]] }" },
5301 { &isl_multi_pw_aff_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5302 "{ [A[] -> A[]] -> [B[] -> C[]] }" },
5303 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5304 "{ B[i,j] -> C[i + 2j] }", "{ A[a,b] -> B[b,a] }",
5305 "{ A[a,b] -> C[b + 2a] }" },
5306 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5307 "{ B[i,j] -> C[i + 2j] }",
5308 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5309 "{ A[a,b] -> C[(b + 2a : b > a)] }" },
5310 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5311 "{ B[i,j] -> C[(i + 2j : j > 4)] }",
5312 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5313 "{ A[a,b] -> C[(b + 2a : b > a > 4)] }" },
5314 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5315 "{ B[i,j] -> C[] }",
5316 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5317 "{ A[a,b] -> C[] }" },
5318 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5319 "{ B[i,j] -> C[] : i > j }",
5320 "{ A[a,b] -> B[b,a] }",
5321 "{ A[a,b] -> C[] : b > a }" },
5322 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5323 "{ B[i,j] -> C[] : j > 5 }",
5324 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5325 "{ A[a,b] -> C[] : b > a > 5 }" },
5326 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5327 "[N] -> { B[i,j] -> C[] : j > N }",
5328 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5329 "[N] -> { A[a,b] -> C[] : b > a > N }" },
5330 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5331 "[M,N] -> { B[] -> C[] : N > 5 }",
5332 "[M,N] -> { A[] -> B[] : M > N }",
5333 "[M,N] -> { A[] -> C[] : M > N > 5 }" },
5336 /* Perform some basic tests of binary operations on isl_multi_pw_aff objects.
5338 static int test_bin_mpa(isl_ctx *ctx)
5340 int i;
5341 isl_bool ok;
5342 isl_multi_pw_aff *mpa1, *mpa2, *res;
5344 for (i = 0; i < ARRAY_SIZE(mpa_bin_tests); ++i) {
5345 mpa1 = isl_multi_pw_aff_read_from_str(ctx,
5346 mpa_bin_tests[i].arg1);
5347 mpa2 = isl_multi_pw_aff_read_from_str(ctx,
5348 mpa_bin_tests[i].arg2);
5349 res = isl_multi_pw_aff_read_from_str(ctx,
5350 mpa_bin_tests[i].res);
5351 mpa1 = mpa_bin_tests[i].fn(mpa1, mpa2);
5352 ok = isl_multi_pw_aff_plain_is_equal(mpa1, res);
5353 isl_multi_pw_aff_free(mpa1);
5354 isl_multi_pw_aff_free(res);
5355 if (ok < 0)
5356 return -1;
5357 if (!ok)
5358 isl_die(ctx, isl_error_unknown,
5359 "unexpected result", return -1);
5362 return 0;
5365 /* Inputs for basic tests of unary operations on
5366 * isl_multi_union_pw_aff objects.
5367 * "fn" is the function that is tested.
5368 * "arg" is a string description of the input.
5369 * "res" is a string description of the expected result.
5371 struct {
5372 __isl_give isl_multi_union_pw_aff *(*fn)(
5373 __isl_take isl_multi_union_pw_aff *mupa);
5374 const char *arg;
5375 const char *res;
5376 } mupa_un_tests[] = {
5377 { &isl_multi_union_pw_aff_factor_range,
5378 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]",
5379 "C[{ A[] -> [2] }]" },
5380 { &isl_multi_union_pw_aff_factor_range,
5381 "[B[] -> C[{ A[] -> [2] }]]",
5382 "C[{ A[] -> [2] }]" },
5383 { &isl_multi_union_pw_aff_factor_range,
5384 "[B[{ A[] -> [1] }] -> C[]]",
5385 "C[]" },
5386 { &isl_multi_union_pw_aff_factor_range,
5387 "[B[] -> C[]]",
5388 "C[]" },
5389 { &isl_multi_union_pw_aff_factor_range,
5390 "([B[] -> C[]] : { A[x] : x >= 0 })",
5391 "(C[] : { A[x] : x >= 0 })" },
5392 { &isl_multi_union_pw_aff_factor_range,
5393 "[N] -> ([B[] -> C[]] : { A[x] : x <= N })",
5394 "[N] -> (C[] : { A[x] : x <= N })" },
5395 { &isl_multi_union_pw_aff_factor_range,
5396 "[N] -> ([B[] -> C[]] : { : N >= 0 })",
5397 "[N] -> (C[] : { : N >= 0 })" },
5400 /* Perform some basic tests of unary operations on
5401 * isl_multi_union_pw_aff objects.
5403 static int test_un_mupa(isl_ctx *ctx)
5405 int i;
5406 isl_bool ok;
5407 isl_multi_union_pw_aff *mupa, *res;
5409 for (i = 0; i < ARRAY_SIZE(mupa_un_tests); ++i) {
5410 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5411 mupa_un_tests[i].arg);
5412 res = isl_multi_union_pw_aff_read_from_str(ctx,
5413 mupa_un_tests[i].res);
5414 mupa = mupa_un_tests[i].fn(mupa);
5415 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5416 isl_multi_union_pw_aff_free(mupa);
5417 isl_multi_union_pw_aff_free(res);
5418 if (ok < 0)
5419 return -1;
5420 if (!ok)
5421 isl_die(ctx, isl_error_unknown,
5422 "unexpected result", return -1);
5425 return 0;
5428 /* Inputs for basic tests of binary operations on
5429 * isl_multi_union_pw_aff objects.
5430 * "fn" is the function that is tested.
5431 * "arg1" and "arg2" are string descriptions of the inputs.
5432 * "res" is a string description of the expected result.
5434 struct {
5435 __isl_give isl_multi_union_pw_aff *(*fn)(
5436 __isl_take isl_multi_union_pw_aff *mupa1,
5437 __isl_take isl_multi_union_pw_aff *mupa2);
5438 const char *arg1;
5439 const char *arg2;
5440 const char *res;
5441 } mupa_bin_tests[] = {
5442 { &isl_multi_union_pw_aff_add, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5443 "[{ A[] -> [3] }]" },
5444 { &isl_multi_union_pw_aff_sub, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5445 "[{ A[] -> [-1] }]" },
5446 { &isl_multi_union_pw_aff_add,
5447 "[{ A[] -> [1]; B[] -> [4] }]",
5448 "[{ A[] -> [2]; C[] -> [5] }]",
5449 "[{ A[] -> [3] }]" },
5450 { &isl_multi_union_pw_aff_union_add,
5451 "[{ A[] -> [1]; B[] -> [4] }]",
5452 "[{ A[] -> [2]; C[] -> [5] }]",
5453 "[{ A[] -> [3]; B[] -> [4]; C[] -> [5] }]" },
5454 { &isl_multi_union_pw_aff_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5455 "[{ A[x] -> [(x)] : x <= 10 }]",
5456 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10 }]" },
5457 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5458 "([] : { A[x] : x <= 10 })",
5459 "([] : { A[x] : 5 <= x <= 10 })" },
5460 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5461 "[N] -> ([] : { A[x] : x <= N })",
5462 "[N] -> ([] : { A[x] : 5 <= x <= N })" },
5463 { &isl_multi_union_pw_aff_add, "[N] -> ([] : { A[x] : x >= N })",
5464 "([] : { A[x] : x <= 10 })",
5465 "[N] -> ([] : { A[x] : N <= x <= 10 })" },
5466 { &isl_multi_union_pw_aff_union_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5467 "[{ A[x] -> [(x)] : x <= 10 }]",
5468 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10; "
5469 "A[x] -> [(1)] : x > 10; A[x] -> [(x)] : x < 5 }]" },
5470 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 5 })",
5471 "([] : { A[x] : x <= 10 })",
5472 "([] : { A[x] })" },
5473 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 0 })",
5474 "[N] -> ([] : { A[x] : x >= N })",
5475 "[N] -> ([] : { A[x] : x >= 0 or x >= N })" },
5476 { &isl_multi_union_pw_aff_union_add,
5477 "[N] -> ([] : { A[] : N >= 0})",
5478 "[N] -> ([] : { A[] : N <= 0})",
5479 "[N] -> ([] : { A[] })" },
5480 { &isl_multi_union_pw_aff_union_add,
5481 "[N] -> ([] : { A[] })",
5482 "[N] -> ([] : { : })",
5483 "[N] -> ([] : { : })" },
5484 { &isl_multi_union_pw_aff_union_add,
5485 "[N] -> ([] : { : })",
5486 "[N] -> ([] : { A[] })",
5487 "[N] -> ([] : { : })" },
5488 { &isl_multi_union_pw_aff_union_add,
5489 "[N] -> ([] : { : N >= 0})",
5490 "[N] -> ([] : { : N <= 0})",
5491 "[N] -> ([] : { : })" },
5492 { &isl_multi_union_pw_aff_range_product,
5493 "B[{ A[] -> [1] }]",
5494 "C[{ A[] -> [2] }]",
5495 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]" },
5496 { &isl_multi_union_pw_aff_range_product,
5497 "(B[] : { A[x] : x >= 5 })",
5498 "(C[] : { A[x] : x <= 10 })",
5499 "([B[] -> C[]] : { A[x] : 5 <= x <= 10 })" },
5500 { &isl_multi_union_pw_aff_range_product,
5501 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5502 "(C[] : { A[x] : x <= 10 })",
5503 "[B[{ A[x] -> [x + 1] : 5 <= x <= 10 }] -> C[]]" },
5504 { &isl_multi_union_pw_aff_range_product,
5505 "(C[] : { A[x] : x <= 10 })",
5506 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5507 "[C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= 10 }]]" },
5508 { &isl_multi_union_pw_aff_range_product,
5509 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5510 "[N] -> (C[] : { A[x] : x <= N })",
5511 "[N] -> [B[{ A[x] -> [x + 1] : 5 <= x <= N }] -> C[]]" },
5512 { &isl_multi_union_pw_aff_range_product,
5513 "[N] -> (C[] : { A[x] : x <= N })",
5514 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5515 "[N] -> [C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= N }]]" },
5516 { &isl_multi_union_pw_aff_range_product,
5517 "B[{ A[] -> [1]; D[] -> [3] }]",
5518 "C[{ A[] -> [2] }]",
5519 "[B[{ A[] -> [1]; D[] -> [3] }] -> C[{ A[] -> [2] }]]" },
5520 { &isl_multi_union_pw_aff_range_product,
5521 "B[] }]",
5522 "(C[] : { A[x] })",
5523 "([B[] -> C[]] : { A[x] })" },
5524 { &isl_multi_union_pw_aff_range_product,
5525 "(B[] : { A[x] })",
5526 "C[] }]",
5527 "([B[] -> C[]] : { A[x] })" },
5530 /* Perform some basic tests of binary operations on
5531 * isl_multi_union_pw_aff objects.
5533 static int test_bin_mupa(isl_ctx *ctx)
5535 int i;
5536 isl_bool ok;
5537 isl_multi_union_pw_aff *mupa1, *mupa2, *res;
5539 for (i = 0; i < ARRAY_SIZE(mupa_bin_tests); ++i) {
5540 mupa1 = isl_multi_union_pw_aff_read_from_str(ctx,
5541 mupa_bin_tests[i].arg1);
5542 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx,
5543 mupa_bin_tests[i].arg2);
5544 res = isl_multi_union_pw_aff_read_from_str(ctx,
5545 mupa_bin_tests[i].res);
5546 mupa1 = mupa_bin_tests[i].fn(mupa1, mupa2);
5547 ok = isl_multi_union_pw_aff_plain_is_equal(mupa1, res);
5548 isl_multi_union_pw_aff_free(mupa1);
5549 isl_multi_union_pw_aff_free(res);
5550 if (ok < 0)
5551 return -1;
5552 if (!ok)
5553 isl_die(ctx, isl_error_unknown,
5554 "unexpected result", return -1);
5557 return 0;
5560 /* Inputs for basic tests of binary operations on
5561 * pairs of isl_multi_union_pw_aff and isl_set objects.
5562 * "fn" is the function that is tested.
5563 * "arg1" and "arg2" are string descriptions of the inputs.
5564 * "res" is a string description of the expected result.
5566 struct {
5567 __isl_give isl_multi_union_pw_aff *(*fn)(
5568 __isl_take isl_multi_union_pw_aff *mupa,
5569 __isl_take isl_set *set);
5570 const char *arg1;
5571 const char *arg2;
5572 const char *res;
5573 } mupa_set_tests[] = {
5574 { &isl_multi_union_pw_aff_intersect_range,
5575 "C[{ B[i,j] -> [i + 2j] }]", "{ C[1] }",
5576 "C[{ B[i,j] -> [i + 2j] : i + 2j = 1 }]" },
5577 { &isl_multi_union_pw_aff_intersect_range,
5578 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[N] }",
5579 "[N] -> C[{ B[i,j] -> [i + 2j] : i + 2j = N }]" },
5580 { &isl_multi_union_pw_aff_intersect_range,
5581 "[N] -> C[{ B[i,j] -> [i + 2j + N] }]", "{ C[1] }",
5582 "[N] -> C[{ B[i,j] -> [i + 2j + N] : i + 2j + N = 1 }]" },
5583 { &isl_multi_union_pw_aff_intersect_range,
5584 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[x] : N >= 0 }",
5585 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0 }]" },
5586 { &isl_multi_union_pw_aff_intersect_range,
5587 "C[]", "{ C[] }", "C[]" },
5588 { &isl_multi_union_pw_aff_intersect_range,
5589 "[N] -> (C[] : { : N >= 0 })",
5590 "{ C[] }",
5591 "[N] -> (C[] : { : N >= 0 })" },
5592 { &isl_multi_union_pw_aff_intersect_range,
5593 "(C[] : { A[a,b] })",
5594 "{ C[] }",
5595 "(C[] : { A[a,b] })" },
5596 { &isl_multi_union_pw_aff_intersect_range,
5597 "[N] -> (C[] : { A[a,b] : a,b <= N })",
5598 "{ C[] }",
5599 "[N] -> (C[] : { A[a,b] : a,b <= N })" },
5600 { &isl_multi_union_pw_aff_intersect_range,
5601 "C[]",
5602 "[N] -> { C[] : N >= 0 }",
5603 "[N] -> (C[] : { : N >= 0 })" },
5604 { &isl_multi_union_pw_aff_intersect_range,
5605 "(C[] : { A[a,b] })",
5606 "[N] -> { C[] : N >= 0 }",
5607 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5608 { &isl_multi_union_pw_aff_intersect_range,
5609 "[N] -> (C[] : { : N >= 0 })",
5610 "[N] -> { C[] : N < 1024 }",
5611 "[N] -> (C[] : { : 0 <= N < 1024 })" },
5612 { &isl_multi_union_pw_aff_intersect_params,
5613 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { : N >= 0 }",
5614 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0}]" },
5615 { &isl_multi_union_pw_aff_intersect_params,
5616 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "[N] -> { : N >= 0 }",
5617 "[N] -> C[{ B[i,j] -> [i + 2j] : 0 <= N <= 256 }]" },
5618 { &isl_multi_union_pw_aff_intersect_params,
5619 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "{ : }",
5620 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]" },
5621 { &isl_multi_union_pw_aff_intersect_params,
5622 "C[]", "[N] -> { : N >= 0 }",
5623 "[N] -> (C[] : { : N >= 0 })" },
5624 { &isl_multi_union_pw_aff_intersect_params,
5625 "(C[] : { A[a,b] })", "[N] -> { : N >= 0 }",
5626 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5627 { &isl_multi_union_pw_aff_intersect_params,
5628 "[N] -> (C[] : { A[a,N] })", "{ : }",
5629 "[N] -> (C[] : { A[a,N] })" },
5630 { &isl_multi_union_pw_aff_intersect_params,
5631 "[N] -> (C[] : { A[a,b] : N <= 256 })", "[N] -> { : N >= 0 }",
5632 "[N] -> (C[] : { A[a,b] : 0 <= N <= 256 })" },
5635 /* Perform some basic tests of binary operations on
5636 * pairs of isl_multi_union_pw_aff and isl_set objects.
5638 static int test_mupa_set(isl_ctx *ctx)
5640 int i;
5641 isl_bool ok;
5642 isl_multi_union_pw_aff *mupa, *res;
5643 isl_set *set;
5645 for (i = 0; i < ARRAY_SIZE(mupa_set_tests); ++i) {
5646 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5647 mupa_set_tests[i].arg1);
5648 set = isl_set_read_from_str(ctx, mupa_set_tests[i].arg2);
5649 res = isl_multi_union_pw_aff_read_from_str(ctx,
5650 mupa_set_tests[i].res);
5651 mupa = mupa_set_tests[i].fn(mupa, set);
5652 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5653 isl_multi_union_pw_aff_free(mupa);
5654 isl_multi_union_pw_aff_free(res);
5655 if (ok < 0)
5656 return -1;
5657 if (!ok)
5658 isl_die(ctx, isl_error_unknown,
5659 "unexpected result", return -1);
5662 return 0;
5665 /* Inputs for basic tests of binary operations on
5666 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5667 * "fn" is the function that is tested.
5668 * "arg1" and "arg2" are string descriptions of the inputs.
5669 * "res" is a string description of the expected result.
5671 struct {
5672 __isl_give isl_multi_union_pw_aff *(*fn)(
5673 __isl_take isl_multi_union_pw_aff *mupa,
5674 __isl_take isl_union_set *uset);
5675 const char *arg1;
5676 const char *arg2;
5677 const char *res;
5678 } mupa_uset_tests[] = {
5679 { &isl_multi_union_pw_aff_intersect_domain,
5680 "C[{ B[i,j] -> [i + 2j] }]", "{ B[i,i] }",
5681 "C[{ B[i,i] -> [3i] }]" },
5682 { &isl_multi_union_pw_aff_intersect_domain,
5683 "(C[] : { B[i,j] })", "{ B[i,i] }",
5684 "(C[] : { B[i,i] })" },
5685 { &isl_multi_union_pw_aff_intersect_domain,
5686 "(C[] : { B[i,j] })", "[N] -> { B[N,N] }",
5687 "[N] -> (C[] : { B[N,N] })" },
5688 { &isl_multi_union_pw_aff_intersect_domain,
5689 "C[]", "{ B[i,i] }",
5690 "(C[] : { B[i,i] })" },
5691 { &isl_multi_union_pw_aff_intersect_domain,
5692 "[N] -> (C[] : { : N >= 0 })", "{ B[i,i] }",
5693 "[N] -> (C[] : { B[i,i] : N >= 0 })" },
5696 /* Perform some basic tests of binary operations on
5697 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5699 static int test_mupa_uset(isl_ctx *ctx)
5701 int i;
5702 isl_bool ok;
5703 isl_multi_union_pw_aff *mupa, *res;
5704 isl_union_set *uset;
5706 for (i = 0; i < ARRAY_SIZE(mupa_uset_tests); ++i) {
5707 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5708 mupa_uset_tests[i].arg1);
5709 uset = isl_union_set_read_from_str(ctx,
5710 mupa_uset_tests[i].arg2);
5711 res = isl_multi_union_pw_aff_read_from_str(ctx,
5712 mupa_uset_tests[i].res);
5713 mupa = mupa_uset_tests[i].fn(mupa, uset);
5714 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5715 isl_multi_union_pw_aff_free(mupa);
5716 isl_multi_union_pw_aff_free(res);
5717 if (ok < 0)
5718 return -1;
5719 if (!ok)
5720 isl_die(ctx, isl_error_unknown,
5721 "unexpected result", return -1);
5724 return 0;
5727 /* Inputs for basic tests of binary operations on
5728 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5729 * "fn" is the function that is tested.
5730 * "arg1" and "arg2" are string descriptions of the inputs.
5731 * "res" is a string description of the expected result.
5733 struct {
5734 __isl_give isl_multi_union_pw_aff *(*fn)(
5735 __isl_take isl_multi_union_pw_aff *mupa,
5736 __isl_take isl_multi_aff *ma);
5737 const char *arg1;
5738 const char *arg2;
5739 const char *res;
5740 } mupa_ma_tests[] = {
5741 { &isl_multi_union_pw_aff_apply_multi_aff,
5742 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5743 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5744 "{ C[a,b] -> D[b,a] }",
5745 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
5746 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
5747 { &isl_multi_union_pw_aff_apply_multi_aff,
5748 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
5749 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5750 "{ C[a,b] -> D[b,a] }",
5751 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
5752 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
5753 { &isl_multi_union_pw_aff_apply_multi_aff,
5754 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5755 "[N] -> { C[a] -> D[a + N] }",
5756 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }] " },
5757 { &isl_multi_union_pw_aff_apply_multi_aff,
5758 "C[]",
5759 "{ C[] -> D[] }",
5760 "D[]" },
5761 { &isl_multi_union_pw_aff_apply_multi_aff,
5762 "[N] -> (C[] : { : N >= 0 })",
5763 "{ C[] -> D[] }",
5764 "[N] -> (D[] : { : N >= 0 })" },
5765 { &isl_multi_union_pw_aff_apply_multi_aff,
5766 "C[]",
5767 "[N] -> { C[] -> D[N] }",
5768 "[N] -> D[{ [N] }]" },
5769 { &isl_multi_union_pw_aff_apply_multi_aff,
5770 "(C[] : { A[i,j] : i >= j })",
5771 "{ C[] -> D[] }",
5772 "(D[] : { A[i,j] : i >= j })" },
5773 { &isl_multi_union_pw_aff_apply_multi_aff,
5774 "[N] -> (C[] : { A[i,j] : N >= 0 })",
5775 "{ C[] -> D[] }",
5776 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
5777 { &isl_multi_union_pw_aff_apply_multi_aff,
5778 "(C[] : { A[i,j] : i >= j })",
5779 "[N] -> { C[] -> D[N] }",
5780 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
5783 /* Perform some basic tests of binary operations on
5784 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5786 static int test_mupa_ma(isl_ctx *ctx)
5788 int i;
5789 isl_bool ok;
5790 isl_multi_union_pw_aff *mupa, *res;
5791 isl_multi_aff *ma;
5793 for (i = 0; i < ARRAY_SIZE(mupa_ma_tests); ++i) {
5794 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5795 mupa_ma_tests[i].arg1);
5796 ma = isl_multi_aff_read_from_str(ctx, mupa_ma_tests[i].arg2);
5797 res = isl_multi_union_pw_aff_read_from_str(ctx,
5798 mupa_ma_tests[i].res);
5799 mupa = mupa_ma_tests[i].fn(mupa, ma);
5800 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5801 isl_multi_union_pw_aff_free(mupa);
5802 isl_multi_union_pw_aff_free(res);
5803 if (ok < 0)
5804 return -1;
5805 if (!ok)
5806 isl_die(ctx, isl_error_unknown,
5807 "unexpected result", return -1);
5810 return 0;
5813 /* Inputs for basic tests of binary operations on
5814 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5815 * "fn" is the function that is tested.
5816 * "arg1" and "arg2" are string descriptions of the inputs.
5817 * "res" is a string description of the expected result.
5819 struct {
5820 __isl_give isl_union_pw_aff *(*fn)(
5821 __isl_take isl_multi_union_pw_aff *mupa,
5822 __isl_take isl_pw_aff *pa);
5823 const char *arg1;
5824 const char *arg2;
5825 const char *res;
5826 } mupa_pa_tests[] = {
5827 { &isl_multi_union_pw_aff_apply_pw_aff,
5828 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5829 "[N] -> { C[a] -> [a + N] }",
5830 "[N] -> { A[i,j] -> [i + N]; B[i,j] -> [j + N] }" },
5831 { &isl_multi_union_pw_aff_apply_pw_aff,
5832 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5833 "{ C[a] -> [a] : a >= 0; C[a] -> [-a] : a < 0 }",
5834 "{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
5835 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }" },
5836 { &isl_multi_union_pw_aff_apply_pw_aff,
5837 "C[]",
5838 "[N] -> { C[] -> [N] }",
5839 "[N] -> { [N] }" },
5840 { &isl_multi_union_pw_aff_apply_pw_aff,
5841 "C[]",
5842 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
5843 "[N] -> { [N] : N >= 0; [-N] : N < 0 }" },
5844 { &isl_multi_union_pw_aff_apply_pw_aff,
5845 "[N] -> (C[] : { : N >= 0 })",
5846 "[N] -> { C[] -> [N] }",
5847 "[N] -> { [N] : N >= 0 }" },
5848 { &isl_multi_union_pw_aff_apply_pw_aff,
5849 "[N] -> (C[] : { : N >= 0 })",
5850 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
5851 "[N] -> { [N] : N >= 0 }" },
5852 { &isl_multi_union_pw_aff_apply_pw_aff,
5853 "[N] -> (C[] : { : N >= 0 })",
5854 "{ C[] -> [0] }",
5855 "[N] -> { [0] : N >= 0 }" },
5856 { &isl_multi_union_pw_aff_apply_pw_aff,
5857 "(C[] : { A[i,j] : i >= j })",
5858 "[N] -> { C[] -> [N] }",
5859 "[N] -> { A[i,j] -> [N] : i >= j }" },
5860 { &isl_multi_union_pw_aff_apply_pw_aff,
5861 "(C[] : { A[i,j] : i >= j })",
5862 "[N] -> { C[] -> [N] : N >= 0 }",
5863 "[N] -> { A[i,j] -> [N] : i >= j and N >= 0 }" },
5866 /* Perform some basic tests of binary operations on
5867 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5869 static int test_mupa_pa(isl_ctx *ctx)
5871 int i;
5872 isl_bool ok;
5873 isl_multi_union_pw_aff *mupa;
5874 isl_union_pw_aff *upa, *res;
5875 isl_pw_aff *pa;
5877 for (i = 0; i < ARRAY_SIZE(mupa_pa_tests); ++i) {
5878 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5879 mupa_pa_tests[i].arg1);
5880 pa = isl_pw_aff_read_from_str(ctx, mupa_pa_tests[i].arg2);
5881 res = isl_union_pw_aff_read_from_str(ctx,
5882 mupa_pa_tests[i].res);
5883 upa = mupa_pa_tests[i].fn(mupa, pa);
5884 ok = isl_union_pw_aff_plain_is_equal(upa, res);
5885 isl_union_pw_aff_free(upa);
5886 isl_union_pw_aff_free(res);
5887 if (ok < 0)
5888 return -1;
5889 if (!ok)
5890 isl_die(ctx, isl_error_unknown,
5891 "unexpected result", return -1);
5894 return 0;
5897 /* Inputs for basic tests of binary operations on
5898 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
5899 * "fn" is the function that is tested.
5900 * "arg1" and "arg2" are string descriptions of the inputs.
5901 * "res" is a string description of the expected result.
5903 struct {
5904 __isl_give isl_multi_union_pw_aff *(*fn)(
5905 __isl_take isl_multi_union_pw_aff *mupa,
5906 __isl_take isl_pw_multi_aff *pma);
5907 const char *arg1;
5908 const char *arg2;
5909 const char *res;
5910 } mupa_pma_tests[] = {
5911 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5912 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5913 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5914 "{ C[a,b] -> D[b,a] }",
5915 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
5916 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
5917 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5918 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
5919 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5920 "{ C[a,b] -> D[b,a] }",
5921 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
5922 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
5923 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5924 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5925 "[N] -> { C[a] -> D[a + N] }",
5926 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }]" },
5927 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5928 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5929 "{ C[a] -> D[a] : a >= 0; C[a] -> D[-a] : a < 0 }",
5930 "D[{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
5931 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }]" },
5932 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5933 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5934 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5935 "{ C[a,b] -> D[a,b] : a >= b; C[a,b] -> D[b,a] : a < b }",
5936 "D[{ A[i,j] -> [i] : i >= j; A[i,j] -> [j] : i < j; "
5937 "B[i,j] -> [j] : i <= j; B[i,j] -> [i] : i > j }, "
5938 "{ A[i,j] -> [j] : i >= j; A[i,j] -> [i] : i < j; "
5939 "B[i,j] -> [i] : i <= j; B[i,j] -> [j] : i > j }]" },
5940 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5941 "C[]",
5942 "{ C[] -> D[] }",
5943 "D[]" },
5944 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5945 "[N] -> (C[] : { : N >= 0 })",
5946 "{ C[] -> D[] }",
5947 "[N] -> (D[] : { : N >= 0 })" },
5948 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5949 "C[]",
5950 "[N] -> { C[] -> D[N] }",
5951 "[N] -> D[{ [N] }]" },
5952 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5953 "(C[] : { A[i,j] : i >= j })",
5954 "{ C[] -> D[] }",
5955 "(D[] : { A[i,j] : i >= j })" },
5956 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5957 "[N] -> (C[] : { A[i,j] : N >= 0 })",
5958 "{ C[] -> D[] }",
5959 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
5960 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5961 "(C[] : { A[i,j] : i >= j })",
5962 "[N] -> { C[] -> D[N] }",
5963 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
5964 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5965 "C[]",
5966 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
5967 "[N] -> D[{ [N] : N >= 0; [-N] : N < 0 }]" },
5968 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5969 "[N] -> (C[] : { : N >= 0 })",
5970 "[N] -> { C[] -> D[N] }",
5971 "[N] -> D[{ [N] : N >= 0 }]" },
5972 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5973 "[N] -> (C[] : { : N >= 0 })",
5974 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
5975 "[N] -> D[{ [N] : N >= 0 }]" },
5976 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5977 "[N] -> (C[] : { : N >= 0 })",
5978 "{ C[] -> D[0] }",
5979 "[N] -> D[{ [0] : N >= 0 }]" },
5980 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5981 "(C[] : { A[i,j] : i >= j })",
5982 "[N] -> { C[] -> D[N] : N >= 0 }",
5983 "[N] -> D[{ A[i,j] -> [N] : i >= j and N >= 0 }]" },
5986 /* Perform some basic tests of binary operations on
5987 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
5989 static int test_mupa_pma(isl_ctx *ctx)
5991 int i;
5992 isl_bool ok;
5993 isl_multi_union_pw_aff *mupa, *res;
5994 isl_pw_multi_aff *pma;
5996 for (i = 0; i < ARRAY_SIZE(mupa_pma_tests); ++i) {
5997 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5998 mupa_pma_tests[i].arg1);
5999 pma = isl_pw_multi_aff_read_from_str(ctx,
6000 mupa_pma_tests[i].arg2);
6001 res = isl_multi_union_pw_aff_read_from_str(ctx,
6002 mupa_pma_tests[i].res);
6003 mupa = mupa_pma_tests[i].fn(mupa, pma);
6004 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6005 isl_multi_union_pw_aff_free(mupa);
6006 isl_multi_union_pw_aff_free(res);
6007 if (ok < 0)
6008 return -1;
6009 if (!ok)
6010 isl_die(ctx, isl_error_unknown,
6011 "unexpected result", return -1);
6014 return 0;
6017 /* Inputs for basic tests of binary operations on
6018 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6019 * "fn" is the function that is tested.
6020 * "arg1" and "arg2" are string descriptions of the inputs.
6021 * "res" is a string description of the expected result.
6023 struct {
6024 __isl_give isl_multi_union_pw_aff *(*fn)(
6025 __isl_take isl_multi_union_pw_aff *mupa,
6026 __isl_take isl_union_pw_multi_aff *upma);
6027 const char *arg1;
6028 const char *arg2;
6029 const char *res;
6030 } mupa_upma_tests[] = {
6031 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6032 "C[{ B[i,j] -> [i + 2j] }]", "{ A[a,b] -> B[b,a] }",
6033 "C[{ A[a,b] -> [b + 2a] }]" },
6034 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6035 "C[{ B[i,j] -> [i + 2j] }]",
6036 "{ A[a,b] -> B[b,a] : b > a }",
6037 "C[{ A[a,b] -> [b + 2a] : b > a }]" },
6038 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6039 "C[{ B[i,j] -> [i + 2j] : j > 4 }]",
6040 "{ A[a,b] -> B[b,a] : b > a }",
6041 "C[{ A[a,b] -> [b + 2a] : b > a > 4 }]" },
6042 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6043 "C[{ B[i,j] -> [i + 2j] }]",
6044 "{ A[a,b] -> B[b,a] : a > b; A[a,b] -> B[a,b] : a <= b }",
6045 "C[{ A[a,b] -> [b + 2a] : a > b; A[a,b] -> [a + 2b] : a <= b }]" },
6046 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6047 "(C[] : { B[a,b] })",
6048 "{ A[a,b] -> B[b,a] }",
6049 "(C[] : { A[a,b] })" },
6050 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6051 "(C[] : { B[a,b] })",
6052 "{ B[a,b] -> A[b,a] }",
6053 "(C[] : { })" },
6054 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6055 "(C[] : { B[a,b] })",
6056 "{ A[a,b] -> B[b,a] : a > b }",
6057 "(C[] : { A[a,b] : a > b })" },
6058 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6059 "(C[] : { B[a,b] : a > b })",
6060 "{ A[a,b] -> B[b,a] }",
6061 "(C[] : { A[a,b] : b > a })" },
6062 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6063 "[N] -> (C[] : { B[a,b] : a > N })",
6064 "{ A[a,b] -> B[b,a] : a > b }",
6065 "[N] -> (C[] : { A[a,b] : a > b > N })" },
6066 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6067 "(C[] : { B[a,b] : a > b })",
6068 "[N] -> { A[a,b] -> B[b,a] : a > N }",
6069 "[N] -> (C[] : { A[a,b] : b > a > N })" },
6070 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6071 "C[]",
6072 "{ A[a,b] -> B[b,a] }",
6073 "C[]" },
6074 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6075 "[N] -> (C[] : { : N >= 0 })",
6076 "{ A[a,b] -> B[b,a] }",
6077 "[N] -> (C[] : { : N >= 0 })" },
6078 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6079 "C[]",
6080 "[N] -> { A[a,b] -> B[b,a] : N >= 0 }",
6081 "[N] -> (C[] : { : N >= 0 })" },
6084 /* Perform some basic tests of binary operations on
6085 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6087 static int test_mupa_upma(isl_ctx *ctx)
6089 int i;
6090 isl_bool ok;
6091 isl_multi_union_pw_aff *mupa, *res;
6092 isl_union_pw_multi_aff *upma;
6094 for (i = 0; i < ARRAY_SIZE(mupa_upma_tests); ++i) {
6095 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6096 mupa_upma_tests[i].arg1);
6097 upma = isl_union_pw_multi_aff_read_from_str(ctx,
6098 mupa_upma_tests[i].arg2);
6099 res = isl_multi_union_pw_aff_read_from_str(ctx,
6100 mupa_upma_tests[i].res);
6101 mupa = mupa_upma_tests[i].fn(mupa, upma);
6102 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6103 isl_multi_union_pw_aff_free(mupa);
6104 isl_multi_union_pw_aff_free(res);
6105 if (ok < 0)
6106 return -1;
6107 if (!ok)
6108 isl_die(ctx, isl_error_unknown,
6109 "unexpected result", return -1);
6112 return 0;
6115 /* Check that the input tuple of an isl_aff can be set properly.
6117 static isl_stat test_aff_set_tuple_id(isl_ctx *ctx)
6119 isl_id *id;
6120 isl_aff *aff;
6121 int equal;
6123 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x + 1] }");
6124 id = isl_id_alloc(ctx, "A", NULL);
6125 aff = isl_aff_set_tuple_id(aff, isl_dim_in, id);
6126 equal = aff_check_plain_equal(aff, "{ A[x] -> [x + 1] }");
6127 isl_aff_free(aff);
6128 if (equal < 0)
6129 return isl_stat_error;
6131 return isl_stat_ok;
6134 int test_aff(isl_ctx *ctx)
6136 const char *str;
6137 isl_set *set;
6138 isl_space *space;
6139 isl_local_space *ls;
6140 isl_aff *aff;
6141 int zero, equal;
6143 if (test_upa(ctx) < 0)
6144 return -1;
6145 if (test_bin_aff(ctx) < 0)
6146 return -1;
6147 if (test_bin_pw_aff(ctx) < 0)
6148 return -1;
6149 if (test_bin_upma(ctx) < 0)
6150 return -1;
6151 if (test_bin_upma_fail(ctx) < 0)
6152 return -1;
6153 if (test_un_mpa(ctx) < 0)
6154 return -1;
6155 if (test_bin_mpa(ctx) < 0)
6156 return -1;
6157 if (test_un_mupa(ctx) < 0)
6158 return -1;
6159 if (test_bin_mupa(ctx) < 0)
6160 return -1;
6161 if (test_mupa_set(ctx) < 0)
6162 return -1;
6163 if (test_mupa_uset(ctx) < 0)
6164 return -1;
6165 if (test_mupa_ma(ctx) < 0)
6166 return -1;
6167 if (test_mupa_pa(ctx) < 0)
6168 return -1;
6169 if (test_mupa_pma(ctx) < 0)
6170 return -1;
6171 if (test_mupa_upma(ctx) < 0)
6172 return -1;
6174 space = isl_space_set_alloc(ctx, 0, 1);
6175 ls = isl_local_space_from_space(space);
6176 aff = isl_aff_zero_on_domain(ls);
6178 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6179 aff = isl_aff_scale_down_ui(aff, 3);
6180 aff = isl_aff_floor(aff);
6181 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6182 aff = isl_aff_scale_down_ui(aff, 2);
6183 aff = isl_aff_floor(aff);
6184 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6186 str = "{ [10] }";
6187 set = isl_set_read_from_str(ctx, str);
6188 aff = isl_aff_gist(aff, set);
6190 aff = isl_aff_add_constant_si(aff, -16);
6191 zero = isl_aff_plain_is_zero(aff);
6192 isl_aff_free(aff);
6194 if (zero < 0)
6195 return -1;
6196 if (!zero)
6197 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6199 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
6200 aff = isl_aff_scale_down_ui(aff, 64);
6201 aff = isl_aff_floor(aff);
6202 equal = aff_check_plain_equal(aff, "{ [-1] }");
6203 isl_aff_free(aff);
6204 if (equal < 0)
6205 return -1;
6207 if (test_aff_set_tuple_id(ctx) < 0)
6208 return -1;
6210 return 0;
6213 /* Check that "pa" consists of a single expression.
6215 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
6217 int n;
6219 n = isl_pw_aff_n_piece(pa);
6220 isl_pw_aff_free(pa);
6222 if (!pa)
6223 return -1;
6224 if (n != 1)
6225 isl_die(ctx, isl_error_unknown, "expecting single expression",
6226 return -1);
6228 return 0;
6231 /* Check that the computation below results in a single expression.
6232 * One or two expressions may result depending on which constraint
6233 * ends up being considered as redundant with respect to the other
6234 * constraints after the projection that is performed internally
6235 * by isl_set_dim_min.
6237 static int test_dim_max_1(isl_ctx *ctx)
6239 const char *str;
6240 isl_set *set;
6241 isl_pw_aff *pa;
6243 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
6244 "-4a <= b <= 3 and b < n - 4a }";
6245 set = isl_set_read_from_str(ctx, str);
6246 pa = isl_set_dim_min(set, 0);
6247 return check_single_piece(ctx, pa);
6250 /* Check that the computation below results in a single expression.
6251 * The PIP problem corresponding to these constraints has a row
6252 * that causes a split of the solution domain. The solver should
6253 * first pick rows that split off empty parts such that the actual
6254 * solution domain does not get split.
6255 * Note that the description contains some redundant constraints.
6256 * If these constraints get removed first, then the row mentioned
6257 * above does not appear in the PIP problem.
6259 static int test_dim_max_2(isl_ctx *ctx)
6261 const char *str;
6262 isl_set *set;
6263 isl_pw_aff *pa;
6265 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
6266 "N > 0 and P >= 0 }";
6267 set = isl_set_read_from_str(ctx, str);
6268 pa = isl_set_dim_max(set, 0);
6269 return check_single_piece(ctx, pa);
6272 int test_dim_max(isl_ctx *ctx)
6274 int equal;
6275 const char *str;
6276 isl_set *set1, *set2;
6277 isl_set *set;
6278 isl_map *map;
6279 isl_pw_aff *pwaff;
6281 if (test_dim_max_1(ctx) < 0)
6282 return -1;
6283 if (test_dim_max_2(ctx) < 0)
6284 return -1;
6286 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
6287 set = isl_set_read_from_str(ctx, str);
6288 pwaff = isl_set_dim_max(set, 0);
6289 set1 = isl_set_from_pw_aff(pwaff);
6290 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
6291 set2 = isl_set_read_from_str(ctx, str);
6292 equal = isl_set_is_equal(set1, set2);
6293 isl_set_free(set1);
6294 isl_set_free(set2);
6295 if (equal < 0)
6296 return -1;
6297 if (!equal)
6298 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6300 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
6301 set = isl_set_read_from_str(ctx, str);
6302 pwaff = isl_set_dim_max(set, 0);
6303 set1 = isl_set_from_pw_aff(pwaff);
6304 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6305 set2 = isl_set_read_from_str(ctx, str);
6306 equal = isl_set_is_equal(set1, set2);
6307 isl_set_free(set1);
6308 isl_set_free(set2);
6309 if (equal < 0)
6310 return -1;
6311 if (!equal)
6312 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6314 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
6315 set = isl_set_read_from_str(ctx, str);
6316 pwaff = isl_set_dim_max(set, 0);
6317 set1 = isl_set_from_pw_aff(pwaff);
6318 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6319 set2 = isl_set_read_from_str(ctx, str);
6320 equal = isl_set_is_equal(set1, set2);
6321 isl_set_free(set1);
6322 isl_set_free(set2);
6323 if (equal < 0)
6324 return -1;
6325 if (!equal)
6326 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6328 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
6329 "0 <= i < N and 0 <= j < M }";
6330 map = isl_map_read_from_str(ctx, str);
6331 set = isl_map_range(map);
6333 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
6334 set1 = isl_set_from_pw_aff(pwaff);
6335 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
6336 set2 = isl_set_read_from_str(ctx, str);
6337 equal = isl_set_is_equal(set1, set2);
6338 isl_set_free(set1);
6339 isl_set_free(set2);
6341 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
6342 set1 = isl_set_from_pw_aff(pwaff);
6343 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
6344 set2 = isl_set_read_from_str(ctx, str);
6345 if (equal >= 0 && equal)
6346 equal = isl_set_is_equal(set1, set2);
6347 isl_set_free(set1);
6348 isl_set_free(set2);
6350 isl_set_free(set);
6352 if (equal < 0)
6353 return -1;
6354 if (!equal)
6355 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6357 /* Check that solutions are properly merged. */
6358 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
6359 "c <= -1 + n - 4a - 2b and c >= -2b and "
6360 "4a >= -4 + n and c >= 0 }";
6361 set = isl_set_read_from_str(ctx, str);
6362 pwaff = isl_set_dim_min(set, 2);
6363 set1 = isl_set_from_pw_aff(pwaff);
6364 str = "[n] -> { [(0)] : n >= 1 }";
6365 set2 = isl_set_read_from_str(ctx, str);
6366 equal = isl_set_is_equal(set1, set2);
6367 isl_set_free(set1);
6368 isl_set_free(set2);
6370 if (equal < 0)
6371 return -1;
6372 if (!equal)
6373 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6375 /* Check that empty solution lie in the right space. */
6376 str = "[n] -> { [t,a] : 1 = 0 }";
6377 set = isl_set_read_from_str(ctx, str);
6378 pwaff = isl_set_dim_max(set, 0);
6379 set1 = isl_set_from_pw_aff(pwaff);
6380 str = "[n] -> { [t] : 1 = 0 }";
6381 set2 = isl_set_read_from_str(ctx, str);
6382 equal = isl_set_is_equal(set1, set2);
6383 isl_set_free(set1);
6384 isl_set_free(set2);
6386 if (equal < 0)
6387 return -1;
6388 if (!equal)
6389 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6391 return 0;
6394 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
6396 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
6397 const char *str)
6399 isl_ctx *ctx;
6400 isl_pw_multi_aff *pma2;
6401 int equal;
6403 if (!pma)
6404 return -1;
6406 ctx = isl_pw_multi_aff_get_ctx(pma);
6407 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6408 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
6409 isl_pw_multi_aff_free(pma2);
6411 return equal;
6414 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
6415 * represented by "str".
6417 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
6418 const char *str)
6420 int equal;
6422 equal = pw_multi_aff_plain_is_equal(pma, str);
6423 if (equal < 0)
6424 return -1;
6425 if (!equal)
6426 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
6427 "result not as expected", return -1);
6428 return 0;
6431 /* Basic test for isl_pw_multi_aff_product.
6433 * Check that multiple pieces are properly handled.
6435 static int test_product_pma(isl_ctx *ctx)
6437 int equal;
6438 const char *str;
6439 isl_pw_multi_aff *pma1, *pma2;
6441 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
6442 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6443 str = "{ C[] -> D[] }";
6444 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6445 pma1 = isl_pw_multi_aff_product(pma1, pma2);
6446 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
6447 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
6448 equal = pw_multi_aff_check_plain_equal(pma1, str);
6449 isl_pw_multi_aff_free(pma1);
6450 if (equal < 0)
6451 return -1;
6453 return 0;
6456 int test_product(isl_ctx *ctx)
6458 const char *str;
6459 isl_set *set;
6460 isl_union_set *uset1, *uset2;
6461 int ok;
6463 str = "{ A[i] }";
6464 set = isl_set_read_from_str(ctx, str);
6465 set = isl_set_product(set, isl_set_copy(set));
6466 ok = isl_set_is_wrapping(set);
6467 isl_set_free(set);
6468 if (ok < 0)
6469 return -1;
6470 if (!ok)
6471 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6473 str = "{ [] }";
6474 uset1 = isl_union_set_read_from_str(ctx, str);
6475 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
6476 str = "{ [[] -> []] }";
6477 uset2 = isl_union_set_read_from_str(ctx, str);
6478 ok = isl_union_set_is_equal(uset1, uset2);
6479 isl_union_set_free(uset1);
6480 isl_union_set_free(uset2);
6481 if (ok < 0)
6482 return -1;
6483 if (!ok)
6484 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6486 if (test_product_pma(ctx) < 0)
6487 return -1;
6489 return 0;
6492 /* Check that two sets are not considered disjoint just because
6493 * they have a different set of (named) parameters.
6495 static int test_disjoint(isl_ctx *ctx)
6497 const char *str;
6498 isl_set *set, *set2;
6499 int disjoint;
6501 str = "[n] -> { [[]->[]] }";
6502 set = isl_set_read_from_str(ctx, str);
6503 str = "{ [[]->[]] }";
6504 set2 = isl_set_read_from_str(ctx, str);
6505 disjoint = isl_set_is_disjoint(set, set2);
6506 isl_set_free(set);
6507 isl_set_free(set2);
6508 if (disjoint < 0)
6509 return -1;
6510 if (disjoint)
6511 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6513 return 0;
6516 /* Inputs for isl_pw_multi_aff_is_equal tests.
6517 * "f1" and "f2" are the two function that need to be compared.
6518 * "equal" is the expected result.
6520 struct {
6521 int equal;
6522 const char *f1;
6523 const char *f2;
6524 } pma_equal_tests[] = {
6525 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
6526 "[N] -> { [0] : 0 <= N <= 1 }" },
6527 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6528 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
6529 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6530 "[N] -> { [0] : 0 <= N <= 1 }" },
6531 { 0, "{ [NaN] }", "{ [NaN] }" },
6534 int test_equal(isl_ctx *ctx)
6536 int i;
6537 const char *str;
6538 isl_set *set, *set2;
6539 int equal;
6541 str = "{ S_6[i] }";
6542 set = isl_set_read_from_str(ctx, str);
6543 str = "{ S_7[i] }";
6544 set2 = isl_set_read_from_str(ctx, str);
6545 equal = isl_set_is_equal(set, set2);
6546 isl_set_free(set);
6547 isl_set_free(set2);
6548 if (equal < 0)
6549 return -1;
6550 if (equal)
6551 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6553 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
6554 int expected = pma_equal_tests[i].equal;
6555 isl_pw_multi_aff *f1, *f2;
6557 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
6558 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
6559 equal = isl_pw_multi_aff_is_equal(f1, f2);
6560 isl_pw_multi_aff_free(f1);
6561 isl_pw_multi_aff_free(f2);
6562 if (equal < 0)
6563 return -1;
6564 if (equal != expected)
6565 isl_die(ctx, isl_error_unknown,
6566 "unexpected equality result", return -1);
6569 return 0;
6572 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
6573 enum isl_dim_type type, unsigned pos, int fixed)
6575 isl_bool test;
6577 test = isl_map_plain_is_fixed(map, type, pos, NULL);
6578 isl_map_free(map);
6579 if (test < 0)
6580 return -1;
6581 if (test == fixed)
6582 return 0;
6583 if (fixed)
6584 isl_die(ctx, isl_error_unknown,
6585 "map not detected as fixed", return -1);
6586 else
6587 isl_die(ctx, isl_error_unknown,
6588 "map detected as fixed", return -1);
6591 int test_fixed(isl_ctx *ctx)
6593 const char *str;
6594 isl_map *map;
6596 str = "{ [i] -> [i] }";
6597 map = isl_map_read_from_str(ctx, str);
6598 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
6599 return -1;
6600 str = "{ [i] -> [1] }";
6601 map = isl_map_read_from_str(ctx, str);
6602 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6603 return -1;
6604 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
6605 map = isl_map_read_from_str(ctx, str);
6606 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6607 return -1;
6608 map = isl_map_read_from_str(ctx, str);
6609 map = isl_map_neg(map);
6610 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6611 return -1;
6613 return 0;
6616 struct isl_vertices_test_data {
6617 const char *set;
6618 int n;
6619 const char *vertex[6];
6620 } vertices_tests[] = {
6621 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
6622 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
6623 { "{ A[t, i] : t = 14 and i = 1 }",
6624 1, { "{ A[14, 1] }" } },
6625 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
6626 "c <= m and m <= n and m > 0 }",
6627 6, {
6628 "[n, m] -> { [n, m, m] : 0 < m <= n }",
6629 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
6630 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
6631 "[n, m] -> { [m, m, m] : 0 < m <= n }",
6632 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
6633 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
6634 } },
6637 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
6639 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
6641 struct isl_vertices_test_data *data = user;
6642 isl_ctx *ctx;
6643 isl_multi_aff *ma;
6644 isl_basic_set *bset;
6645 isl_pw_multi_aff *pma;
6646 int i;
6647 isl_bool equal;
6649 ctx = isl_vertex_get_ctx(vertex);
6650 bset = isl_vertex_get_domain(vertex);
6651 ma = isl_vertex_get_expr(vertex);
6652 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
6654 for (i = 0; i < data->n; ++i) {
6655 isl_pw_multi_aff *pma_i;
6657 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
6658 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
6659 isl_pw_multi_aff_free(pma_i);
6661 if (equal < 0 || equal)
6662 break;
6665 isl_pw_multi_aff_free(pma);
6666 isl_vertex_free(vertex);
6668 if (equal < 0)
6669 return isl_stat_error;
6671 return equal ? isl_stat_ok : isl_stat_error;
6674 int test_vertices(isl_ctx *ctx)
6676 int i;
6678 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
6679 isl_basic_set *bset;
6680 isl_vertices *vertices;
6681 int ok = 1;
6682 int n;
6684 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
6685 vertices = isl_basic_set_compute_vertices(bset);
6686 n = isl_vertices_get_n_vertices(vertices);
6687 if (vertices_tests[i].n != n)
6688 ok = 0;
6689 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
6690 &vertices_tests[i]) < 0)
6691 ok = 0;
6692 isl_vertices_free(vertices);
6693 isl_basic_set_free(bset);
6695 if (!vertices)
6696 return -1;
6697 if (!ok)
6698 isl_die(ctx, isl_error_unknown, "unexpected vertices",
6699 return -1);
6702 return 0;
6705 int test_union_pw(isl_ctx *ctx)
6707 int equal;
6708 const char *str;
6709 isl_union_set *uset;
6710 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
6712 str = "{ [x] -> x^2 }";
6713 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
6714 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
6715 uset = isl_union_pw_qpolynomial_domain(upwqp1);
6716 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
6717 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
6718 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
6719 isl_union_pw_qpolynomial_free(upwqp1);
6720 isl_union_pw_qpolynomial_free(upwqp2);
6721 if (equal < 0)
6722 return -1;
6723 if (!equal)
6724 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6726 return 0;
6729 /* Inputs for basic tests of functions that select
6730 * subparts of the domain of an isl_multi_union_pw_aff.
6731 * "fn" is the function that is tested.
6732 * "arg" is a string description of the input.
6733 * "res" is a string description of the expected result.
6735 struct {
6736 __isl_give isl_union_set *(*fn)(
6737 __isl_take isl_multi_union_pw_aff *mupa);
6738 const char *arg;
6739 const char *res;
6740 } un_locus_tests[] = {
6741 { &isl_multi_union_pw_aff_zero_union_set,
6742 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6743 "{ A[0,j]; B[0,j] }" },
6744 { &isl_multi_union_pw_aff_zero_union_set,
6745 "F[{ A[i,j] -> [i-j]; B[i,j] -> [i-j] : i >= 0 }]",
6746 "{ A[i,i]; B[i,i] : i >= 0 }" },
6747 { &isl_multi_union_pw_aff_zero_union_set,
6748 "(F[] : { A[i,j]; B[i,i] : i >= 0 })",
6749 "{ A[i,j]; B[i,i] : i >= 0 }" },
6752 /* Perform some basic tests of functions that select
6753 * subparts of the domain of an isl_multi_union_pw_aff.
6755 static int test_un_locus(isl_ctx *ctx)
6757 int i;
6758 isl_bool ok;
6759 isl_union_set *uset, *res;
6760 isl_multi_union_pw_aff *mupa;
6762 for (i = 0; i < ARRAY_SIZE(un_locus_tests); ++i) {
6763 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6764 un_locus_tests[i].arg);
6765 res = isl_union_set_read_from_str(ctx, un_locus_tests[i].res);
6766 uset = un_locus_tests[i].fn(mupa);
6767 ok = isl_union_set_is_equal(uset, res);
6768 isl_union_set_free(uset);
6769 isl_union_set_free(res);
6770 if (ok < 0)
6771 return -1;
6772 if (!ok)
6773 isl_die(ctx, isl_error_unknown,
6774 "unexpected result", return -1);
6777 return 0;
6780 /* Inputs for basic tests of functions that select
6781 * subparts of an isl_union_map based on a relation
6782 * specified by an isl_multi_union_pw_aff.
6783 * "fn" is the function that is tested.
6784 * "arg1" and "arg2" are string descriptions of the inputs.
6785 * "res" is a string description of the expected result.
6787 struct {
6788 __isl_give isl_union_map *(*fn)(
6789 __isl_take isl_union_map *umap,
6790 __isl_take isl_multi_union_pw_aff *mupa);
6791 const char *arg1;
6792 const char *arg2;
6793 const char *res;
6794 } bin_locus_tests[] = {
6795 { &isl_union_map_eq_at_multi_union_pw_aff,
6796 "{ A[i,j] -> B[i',j'] }",
6797 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6798 "{ A[i,j] -> B[i,j'] }" },
6799 { &isl_union_map_eq_at_multi_union_pw_aff,
6800 "{ A[i,j] -> B[i',j'] }",
6801 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6802 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6803 "{ A[i,j] -> B[i,j] }" },
6804 { &isl_union_map_eq_at_multi_union_pw_aff,
6805 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6806 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6807 "{ A[i,j] -> B[i,j'] }" },
6808 { &isl_union_map_eq_at_multi_union_pw_aff,
6809 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6810 "F[{ A[i,j] -> [i]; B[i,j] -> [i]; C[i,j] -> [0] }]",
6811 "{ A[i,j] -> B[i,j']; A[0,j] -> C[i',j'] }" },
6812 { &isl_union_map_eq_at_multi_union_pw_aff,
6813 "{ A[i,j] -> B[i',j'] }",
6814 "F[{ A[i,j] -> [i] : i > j; B[i,j] -> [i] }]",
6815 "{ A[i,j] -> B[i,j'] : i > j }" },
6816 { &isl_union_map_lex_lt_at_multi_union_pw_aff,
6817 "{ A[i,j] -> B[i',j'] }",
6818 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6819 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6820 "{ A[i,j] -> B[i',j'] : i,j << i',j' }" },
6821 { &isl_union_map_lex_gt_at_multi_union_pw_aff,
6822 "{ A[i,j] -> B[i',j'] }",
6823 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6824 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6825 "{ A[i,j] -> B[i',j'] : i,j >> i',j' }" },
6826 { &isl_union_map_eq_at_multi_union_pw_aff,
6827 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6828 "(F[] : { A[i,j]; B[i,j] })",
6829 "{ A[i,j] -> B[i',j'] }" },
6830 { &isl_union_map_eq_at_multi_union_pw_aff,
6831 "{ A[i,j] -> B[i',j'] }",
6832 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
6833 "{ A[i,j] -> B[i',j'] : i > j and i' < j' }" },
6834 { &isl_union_map_eq_at_multi_union_pw_aff,
6835 "[N] -> { A[i,j] -> B[i',j'] : i,i' <= N }",
6836 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
6837 "[N] -> { A[i,j] -> B[i',j'] : i > j and i' < j' and i,i' <= N }" },
6838 { &isl_union_map_eq_at_multi_union_pw_aff,
6839 "{ A[i,j] -> B[i',j'] }",
6840 "[N] -> (F[] : { A[i,j] : i < N; B[i,j] : i < N })",
6841 "[N] -> { A[i,j] -> B[i',j'] : i,i' < N }" },
6842 { &isl_union_map_eq_at_multi_union_pw_aff,
6843 "{ A[i,j] -> B[i',j'] }",
6844 "[N] -> (F[] : { : N >= 0 })",
6845 "[N] -> { A[i,j] -> B[i',j'] : N >= 0 }" },
6848 /* Perform some basic tests of functions that select
6849 * subparts of an isl_union_map based on a relation
6850 * specified by an isl_multi_union_pw_aff.
6852 static int test_bin_locus(isl_ctx *ctx)
6854 int i;
6855 isl_bool ok;
6856 isl_union_map *umap, *res;
6857 isl_multi_union_pw_aff *mupa;
6859 for (i = 0; i < ARRAY_SIZE(bin_locus_tests); ++i) {
6860 umap = isl_union_map_read_from_str(ctx,
6861 bin_locus_tests[i].arg1);
6862 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6863 bin_locus_tests[i].arg2);
6864 res = isl_union_map_read_from_str(ctx, bin_locus_tests[i].res);
6865 umap = bin_locus_tests[i].fn(umap, mupa);
6866 ok = isl_union_map_is_equal(umap, res);
6867 isl_union_map_free(umap);
6868 isl_union_map_free(res);
6869 if (ok < 0)
6870 return -1;
6871 if (!ok)
6872 isl_die(ctx, isl_error_unknown,
6873 "unexpected result", return -1);
6876 return 0;
6879 /* Perform basic locus tests.
6881 static int test_locus(isl_ctx *ctx)
6883 if (test_un_locus(ctx) < 0)
6884 return -1;
6885 if (test_bin_locus(ctx) < 0)
6886 return -1;
6887 return 0;
6890 /* Test that isl_union_pw_qpolynomial_eval picks up the function
6891 * defined over the correct domain space.
6893 static int test_eval_1(isl_ctx *ctx)
6895 const char *str;
6896 isl_point *pnt;
6897 isl_set *set;
6898 isl_union_pw_qpolynomial *upwqp;
6899 isl_val *v;
6900 int cmp;
6902 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
6903 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
6904 str = "{ A[6] }";
6905 set = isl_set_read_from_str(ctx, str);
6906 pnt = isl_set_sample_point(set);
6907 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
6908 cmp = isl_val_cmp_si(v, 36);
6909 isl_val_free(v);
6911 if (!v)
6912 return -1;
6913 if (cmp != 0)
6914 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
6916 return 0;
6919 /* Check that isl_qpolynomial_eval handles getting called on a void point.
6921 static int test_eval_2(isl_ctx *ctx)
6923 const char *str;
6924 isl_point *pnt;
6925 isl_set *set;
6926 isl_qpolynomial *qp;
6927 isl_val *v;
6928 isl_bool ok;
6930 str = "{ A[x] -> [x] }";
6931 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
6932 str = "{ A[x] : false }";
6933 set = isl_set_read_from_str(ctx, str);
6934 pnt = isl_set_sample_point(set);
6935 v = isl_qpolynomial_eval(qp, pnt);
6936 ok = isl_val_is_nan(v);
6937 isl_val_free(v);
6939 if (ok < 0)
6940 return -1;
6941 if (!ok)
6942 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
6944 return 0;
6947 /* Inputs for isl_pw_aff_eval test.
6948 * "f" is the affine function.
6949 * "p" is the point where the function should be evaluated.
6950 * "res" is the expected result.
6952 struct {
6953 const char *f;
6954 const char *p;
6955 const char *res;
6956 } aff_eval_tests[] = {
6957 { "{ [i] -> [2 * i] }", "{ [4] }", "8" },
6958 { "{ [i] -> [2 * i] }", "{ [x] : false }", "NaN" },
6959 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [0] }", "0" },
6960 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [1] }", "1" },
6961 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [2] }", "3" },
6962 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [3] }", "5" },
6963 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [4] }", "7" },
6964 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [0] }", "0" },
6965 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [1] }", "0" },
6966 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [2] }", "0" },
6967 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [3] }", "0" },
6968 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [4] }", "1" },
6969 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [6] }", "1" },
6970 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [8] }", "2" },
6971 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [4] }", "4" },
6972 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [-2] }", "2" },
6973 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [0] }", "NaN" },
6974 { "[N] -> { [2 * N] }", "[N] -> { : N = 4 }", "8" },
6975 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 1] }", "1" },
6976 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 2] }", "3/2" },
6977 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [4] }", "4" },
6978 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [3] }", "NaN" },
6979 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [x] : false }", "NaN" },
6982 /* Perform basic isl_pw_aff_eval tests.
6984 static int test_eval_aff(isl_ctx *ctx)
6986 int i;
6988 for (i = 0; i < ARRAY_SIZE(aff_eval_tests); ++i) {
6989 isl_stat r;
6990 isl_pw_aff *pa;
6991 isl_set *set;
6992 isl_point *pnt;
6993 isl_val *v;
6995 pa = isl_pw_aff_read_from_str(ctx, aff_eval_tests[i].f);
6996 set = isl_set_read_from_str(ctx, aff_eval_tests[i].p);
6997 pnt = isl_set_sample_point(set);
6998 v = isl_pw_aff_eval(pa, pnt);
6999 r = val_check_equal(v, aff_eval_tests[i].res);
7000 isl_val_free(v);
7001 if (r < 0)
7002 return -1;
7004 return 0;
7007 /* Perform basic evaluation tests.
7009 static int test_eval(isl_ctx *ctx)
7011 if (test_eval_1(ctx) < 0)
7012 return -1;
7013 if (test_eval_2(ctx) < 0)
7014 return -1;
7015 if (test_eval_aff(ctx) < 0)
7016 return -1;
7017 return 0;
7020 /* Descriptions of sets that are tested for reparsing after printing.
7022 const char *output_tests[] = {
7023 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
7024 "{ [x] : 1 = 0 }",
7025 "{ [x] : false }",
7026 "{ [x] : x mod 2 = 0 }",
7027 "{ [x] : x mod 2 = 1 }",
7028 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
7029 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
7030 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7031 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7032 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
7033 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
7036 /* Check that printing a set and reparsing a set from the printed output
7037 * results in the same set.
7039 static int test_output_set(isl_ctx *ctx)
7041 int i;
7042 char *str;
7043 isl_set *set1, *set2;
7044 isl_bool equal;
7046 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
7047 set1 = isl_set_read_from_str(ctx, output_tests[i]);
7048 str = isl_set_to_str(set1);
7049 set2 = isl_set_read_from_str(ctx, str);
7050 free(str);
7051 equal = isl_set_is_equal(set1, set2);
7052 isl_set_free(set1);
7053 isl_set_free(set2);
7054 if (equal < 0)
7055 return -1;
7056 if (!equal)
7057 isl_die(ctx, isl_error_unknown,
7058 "parsed output not the same", return -1);
7061 return 0;
7064 /* Check that an isl_multi_aff is printed using a consistent space.
7066 static isl_stat test_output_ma(isl_ctx *ctx)
7068 char *str;
7069 isl_bool equal;
7070 isl_aff *aff;
7071 isl_multi_aff *ma, *ma2;
7073 ma = isl_multi_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7074 aff = isl_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7075 ma = isl_multi_aff_set_aff(ma, 0, aff);
7076 str = isl_multi_aff_to_str(ma);
7077 ma2 = isl_multi_aff_read_from_str(ctx, str);
7078 free(str);
7079 equal = isl_multi_aff_plain_is_equal(ma, ma2);
7080 isl_multi_aff_free(ma2);
7081 isl_multi_aff_free(ma);
7083 if (equal < 0)
7084 return isl_stat_error;
7085 if (!equal)
7086 isl_die(ctx, isl_error_unknown, "bad conversion",
7087 return isl_stat_error);
7089 return isl_stat_ok;
7092 /* Check that an isl_multi_pw_aff is printed using a consistent space.
7094 static isl_stat test_output_mpa(isl_ctx *ctx)
7096 char *str;
7097 isl_bool equal;
7098 isl_pw_aff *pa;
7099 isl_multi_pw_aff *mpa, *mpa2;
7101 mpa = isl_multi_pw_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7102 pa = isl_pw_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7103 mpa = isl_multi_pw_aff_set_pw_aff(mpa, 0, pa);
7104 str = isl_multi_pw_aff_to_str(mpa);
7105 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
7106 free(str);
7107 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
7108 isl_multi_pw_aff_free(mpa2);
7109 isl_multi_pw_aff_free(mpa);
7111 if (equal < 0)
7112 return isl_stat_error;
7113 if (!equal)
7114 isl_die(ctx, isl_error_unknown, "bad conversion",
7115 return isl_stat_error);
7117 return isl_stat_ok;
7120 int test_output(isl_ctx *ctx)
7122 char *s;
7123 const char *str;
7124 isl_pw_aff *pa;
7125 isl_printer *p;
7126 int equal;
7128 if (test_output_set(ctx) < 0)
7129 return -1;
7130 if (test_output_ma(ctx) < 0)
7131 return -1;
7132 if (test_output_mpa(ctx) < 0)
7133 return -1;
7135 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
7136 pa = isl_pw_aff_read_from_str(ctx, str);
7138 p = isl_printer_to_str(ctx);
7139 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
7140 p = isl_printer_print_pw_aff(p, pa);
7141 s = isl_printer_get_str(p);
7142 isl_printer_free(p);
7143 isl_pw_aff_free(pa);
7144 if (!s)
7145 equal = -1;
7146 else
7147 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
7148 free(s);
7149 if (equal < 0)
7150 return -1;
7151 if (!equal)
7152 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7154 return 0;
7157 int test_sample(isl_ctx *ctx)
7159 const char *str;
7160 isl_basic_set *bset1, *bset2;
7161 int empty, subset;
7163 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
7164 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
7165 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
7166 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
7167 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
7168 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
7169 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
7170 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
7171 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
7172 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
7173 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
7174 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
7175 "d - 1073741821e and "
7176 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
7177 "3j >= 1 - a + b + 2e and "
7178 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
7179 "3i <= 4 - a + 4b - e and "
7180 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
7181 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
7182 "c <= -1 + a and 3i >= -2 - a + 3e and "
7183 "1073741822e <= 1073741823 - a + 1073741822b + c and "
7184 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
7185 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
7186 "1073741823e >= 1 + 1073741823b - d and "
7187 "3i >= 1073741823b + c - 1073741823e - f and "
7188 "3i >= 1 + 2b + e + 3g }";
7189 bset1 = isl_basic_set_read_from_str(ctx, str);
7190 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
7191 empty = isl_basic_set_is_empty(bset2);
7192 subset = isl_basic_set_is_subset(bset2, bset1);
7193 isl_basic_set_free(bset1);
7194 isl_basic_set_free(bset2);
7195 if (empty < 0 || subset < 0)
7196 return -1;
7197 if (empty)
7198 isl_die(ctx, isl_error_unknown, "point not found", return -1);
7199 if (!subset)
7200 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
7202 return 0;
7205 int test_fixed_power(isl_ctx *ctx)
7207 const char *str;
7208 isl_map *map;
7209 isl_val *exp;
7210 int equal;
7212 str = "{ [i] -> [i + 1] }";
7213 map = isl_map_read_from_str(ctx, str);
7214 exp = isl_val_int_from_si(ctx, 23);
7215 map = isl_map_fixed_power_val(map, exp);
7216 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
7217 isl_map_free(map);
7218 if (equal < 0)
7219 return -1;
7221 return 0;
7224 int test_slice(isl_ctx *ctx)
7226 const char *str;
7227 isl_map *map;
7228 int equal;
7230 str = "{ [i] -> [j] }";
7231 map = isl_map_read_from_str(ctx, str);
7232 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
7233 equal = map_check_equal(map, "{ [i] -> [i] }");
7234 isl_map_free(map);
7235 if (equal < 0)
7236 return -1;
7238 str = "{ [i] -> [j] }";
7239 map = isl_map_read_from_str(ctx, str);
7240 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
7241 equal = map_check_equal(map, "{ [i] -> [j] }");
7242 isl_map_free(map);
7243 if (equal < 0)
7244 return -1;
7246 str = "{ [i] -> [j] }";
7247 map = isl_map_read_from_str(ctx, str);
7248 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
7249 equal = map_check_equal(map, "{ [i] -> [-i] }");
7250 isl_map_free(map);
7251 if (equal < 0)
7252 return -1;
7254 str = "{ [i] -> [j] }";
7255 map = isl_map_read_from_str(ctx, str);
7256 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
7257 equal = map_check_equal(map, "{ [0] -> [j] }");
7258 isl_map_free(map);
7259 if (equal < 0)
7260 return -1;
7262 str = "{ [i] -> [j] }";
7263 map = isl_map_read_from_str(ctx, str);
7264 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
7265 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
7266 isl_map_free(map);
7267 if (equal < 0)
7268 return -1;
7270 str = "{ [i] -> [j] }";
7271 map = isl_map_read_from_str(ctx, str);
7272 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
7273 equal = map_check_equal(map, "{ [i] -> [j] : false }");
7274 isl_map_free(map);
7275 if (equal < 0)
7276 return -1;
7278 return 0;
7281 int test_eliminate(isl_ctx *ctx)
7283 const char *str;
7284 isl_map *map;
7285 int equal;
7287 str = "{ [i] -> [j] : i = 2j }";
7288 map = isl_map_read_from_str(ctx, str);
7289 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
7290 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
7291 isl_map_free(map);
7292 if (equal < 0)
7293 return -1;
7295 return 0;
7298 /* Check that isl_set_dim_residue_class detects that the values of j
7299 * in the set below are all odd and that it does not detect any spurious
7300 * strides.
7302 static int test_residue_class(isl_ctx *ctx)
7304 const char *str;
7305 isl_set *set;
7306 isl_int m, r;
7307 isl_stat res;
7309 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
7310 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
7311 set = isl_set_read_from_str(ctx, str);
7312 isl_int_init(m);
7313 isl_int_init(r);
7314 res = isl_set_dim_residue_class(set, 1, &m, &r);
7315 if (res >= 0 &&
7316 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
7317 isl_die(ctx, isl_error_unknown, "incorrect residue class",
7318 res = isl_stat_error);
7319 isl_int_clear(r);
7320 isl_int_clear(m);
7321 isl_set_free(set);
7323 return res;
7326 static int test_align_parameters_1(isl_ctx *ctx)
7328 const char *str;
7329 isl_space *space;
7330 isl_multi_aff *ma1, *ma2;
7331 int equal;
7333 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
7334 ma1 = isl_multi_aff_read_from_str(ctx, str);
7336 space = isl_space_params_alloc(ctx, 1);
7337 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
7338 ma1 = isl_multi_aff_align_params(ma1, space);
7340 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
7341 ma2 = isl_multi_aff_read_from_str(ctx, str);
7343 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
7345 isl_multi_aff_free(ma1);
7346 isl_multi_aff_free(ma2);
7348 if (equal < 0)
7349 return -1;
7350 if (!equal)
7351 isl_die(ctx, isl_error_unknown,
7352 "result not as expected", return -1);
7354 return 0;
7357 /* Check the isl_multi_*_from_*_list operation in case inputs
7358 * have unaligned parameters.
7359 * In particular, older versions of isl would simply fail
7360 * (without printing any error message).
7362 static isl_stat test_align_parameters_2(isl_ctx *ctx)
7364 isl_space *space;
7365 isl_map *map;
7366 isl_aff *aff;
7367 isl_multi_aff *ma;
7369 map = isl_map_read_from_str(ctx, "{ A[] -> M[x] }");
7370 space = isl_map_get_space(map);
7371 isl_map_free(map);
7373 aff = isl_aff_read_from_str(ctx, "[N] -> { A[] -> [N] }");
7374 ma = isl_multi_aff_from_aff_list(space, isl_aff_list_from_aff(aff));
7375 isl_multi_aff_free(ma);
7377 if (!ma)
7378 return isl_stat_error;
7379 return isl_stat_ok;
7382 /* Perform basic parameter alignment tests.
7384 static int test_align_parameters(isl_ctx *ctx)
7386 if (test_align_parameters_1(ctx) < 0)
7387 return -1;
7388 if (test_align_parameters_2(ctx) < 0)
7389 return -1;
7391 return 0;
7394 /* Check that isl_*_drop_unused_params actually drops the unused parameters
7395 * by comparing the result using isl_*_plain_is_equal.
7396 * Note that this assumes that isl_*_plain_is_equal does not consider
7397 * objects that only differ by unused parameters to be equal.
7399 int test_drop_unused_parameters(isl_ctx *ctx)
7401 const char *str_with, *str_without;
7402 isl_basic_set *bset1, *bset2;
7403 isl_set *set1, *set2;
7404 isl_pw_aff *pwa1, *pwa2;
7405 int equal;
7407 str_with = "[n, m, o] -> { [m] }";
7408 str_without = "[m] -> { [m] }";
7410 bset1 = isl_basic_set_read_from_str(ctx, str_with);
7411 bset2 = isl_basic_set_read_from_str(ctx, str_without);
7412 bset1 = isl_basic_set_drop_unused_params(bset1);
7413 equal = isl_basic_set_plain_is_equal(bset1, bset2);
7414 isl_basic_set_free(bset1);
7415 isl_basic_set_free(bset2);
7417 if (equal < 0)
7418 return -1;
7419 if (!equal)
7420 isl_die(ctx, isl_error_unknown,
7421 "result not as expected", return -1);
7423 set1 = isl_set_read_from_str(ctx, str_with);
7424 set2 = isl_set_read_from_str(ctx, str_without);
7425 set1 = isl_set_drop_unused_params(set1);
7426 equal = isl_set_plain_is_equal(set1, set2);
7427 isl_set_free(set1);
7428 isl_set_free(set2);
7430 if (equal < 0)
7431 return -1;
7432 if (!equal)
7433 isl_die(ctx, isl_error_unknown,
7434 "result not as expected", return -1);
7436 pwa1 = isl_pw_aff_read_from_str(ctx, str_with);
7437 pwa2 = isl_pw_aff_read_from_str(ctx, str_without);
7438 pwa1 = isl_pw_aff_drop_unused_params(pwa1);
7439 equal = isl_pw_aff_plain_is_equal(pwa1, pwa2);
7440 isl_pw_aff_free(pwa1);
7441 isl_pw_aff_free(pwa2);
7443 if (equal < 0)
7444 return -1;
7445 if (!equal)
7446 isl_die(ctx, isl_error_unknown,
7447 "result not as expected", return -1);
7449 return 0;
7452 static int test_list(isl_ctx *ctx)
7454 isl_id *a, *b, *c, *d, *id;
7455 isl_id_list *list;
7456 int ok;
7458 a = isl_id_alloc(ctx, "a", NULL);
7459 b = isl_id_alloc(ctx, "b", NULL);
7460 c = isl_id_alloc(ctx, "c", NULL);
7461 d = isl_id_alloc(ctx, "d", NULL);
7463 list = isl_id_list_alloc(ctx, 4);
7464 list = isl_id_list_add(list, b);
7465 list = isl_id_list_insert(list, 0, a);
7466 list = isl_id_list_add(list, c);
7467 list = isl_id_list_add(list, d);
7468 list = isl_id_list_drop(list, 1, 1);
7470 if (!list)
7471 return -1;
7472 if (isl_id_list_n_id(list) != 3) {
7473 isl_id_list_free(list);
7474 isl_die(ctx, isl_error_unknown,
7475 "unexpected number of elements in list", return -1);
7478 id = isl_id_list_get_id(list, 0);
7479 ok = id == a;
7480 isl_id_free(id);
7481 id = isl_id_list_get_id(list, 1);
7482 ok = ok && id == c;
7483 isl_id_free(id);
7484 id = isl_id_list_get_id(list, 2);
7485 ok = ok && id == d;
7486 isl_id_free(id);
7488 isl_id_list_free(list);
7490 if (!ok)
7491 isl_die(ctx, isl_error_unknown,
7492 "unexpected elements in list", return -1);
7494 return 0;
7497 const char *set_conversion_tests[] = {
7498 "[N] -> { [i] : N - 1 <= 2 i <= N }",
7499 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
7500 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7501 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7502 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
7503 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
7504 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
7505 "-3 + c <= d <= 28 + c) }",
7508 /* Check that converting from isl_set to isl_pw_multi_aff and back
7509 * to isl_set produces the original isl_set.
7511 static int test_set_conversion(isl_ctx *ctx)
7513 int i;
7514 const char *str;
7515 isl_set *set1, *set2;
7516 isl_pw_multi_aff *pma;
7517 int equal;
7519 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
7520 str = set_conversion_tests[i];
7521 set1 = isl_set_read_from_str(ctx, str);
7522 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
7523 set2 = isl_set_from_pw_multi_aff(pma);
7524 equal = isl_set_is_equal(set1, set2);
7525 isl_set_free(set1);
7526 isl_set_free(set2);
7528 if (equal < 0)
7529 return -1;
7530 if (!equal)
7531 isl_die(ctx, isl_error_unknown, "bad conversion",
7532 return -1);
7535 return 0;
7538 const char *conversion_tests[] = {
7539 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7540 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7541 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7542 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7543 "9e <= -2 - 2a) }",
7544 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7545 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7546 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7549 /* Check that converting from isl_map to isl_pw_multi_aff and back
7550 * to isl_map produces the original isl_map.
7552 static int test_map_conversion(isl_ctx *ctx)
7554 int i;
7555 isl_map *map1, *map2;
7556 isl_pw_multi_aff *pma;
7557 int equal;
7559 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
7560 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
7561 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
7562 map2 = isl_map_from_pw_multi_aff(pma);
7563 equal = isl_map_is_equal(map1, map2);
7564 isl_map_free(map1);
7565 isl_map_free(map2);
7567 if (equal < 0)
7568 return -1;
7569 if (!equal)
7570 isl_die(ctx, isl_error_unknown, "bad conversion",
7571 return -1);
7574 return 0;
7577 /* Descriptions of isl_pw_multi_aff objects for testing conversion
7578 * to isl_multi_pw_aff and back.
7580 const char *mpa_conversion_tests[] = {
7581 "{ [x] -> A[x] }",
7582 "{ [x] -> A[x] : x >= 0 }",
7583 "{ [x] -> A[x] : x >= 0; [x] -> A[-x] : x < 0 }",
7584 "{ [x] -> A[x, x + 1] }",
7585 "{ [x] -> A[] }",
7586 "{ [x] -> A[] : x >= 0 }",
7589 /* Check that conversion from isl_pw_multi_aff to isl_multi_pw_aff and
7590 * back to isl_pw_multi_aff preserves the original meaning.
7592 static int test_mpa_conversion(isl_ctx *ctx)
7594 int i;
7595 isl_pw_multi_aff *pma1, *pma2;
7596 isl_multi_pw_aff *mpa;
7597 int equal;
7599 for (i = 0; i < ARRAY_SIZE(mpa_conversion_tests); ++i) {
7600 const char *str;
7601 str = mpa_conversion_tests[i];
7602 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
7603 pma2 = isl_pw_multi_aff_copy(pma1);
7604 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma1);
7605 pma1 = isl_pw_multi_aff_from_multi_pw_aff(mpa);
7606 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7607 isl_pw_multi_aff_free(pma1);
7608 isl_pw_multi_aff_free(pma2);
7610 if (equal < 0)
7611 return -1;
7612 if (!equal)
7613 isl_die(ctx, isl_error_unknown, "bad conversion",
7614 return -1);
7617 return 0;
7620 /* Descriptions of union maps that should be convertible
7621 * to an isl_multi_union_pw_aff.
7623 const char *umap_mupa_conversion_tests[] = {
7624 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7625 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7626 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7627 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7628 "9e <= -2 - 2a) }",
7629 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7630 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7631 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7632 "{ A[] -> B[0]; C[] -> B[1] }",
7633 "{ A[] -> B[]; C[] -> B[] }",
7636 /* Check that converting from isl_union_map to isl_multi_union_pw_aff and back
7637 * to isl_union_map produces the original isl_union_map.
7639 static int test_union_map_mupa_conversion(isl_ctx *ctx)
7641 int i;
7642 isl_union_map *umap1, *umap2;
7643 isl_multi_union_pw_aff *mupa;
7644 int equal;
7646 for (i = 0; i < ARRAY_SIZE(umap_mupa_conversion_tests); ++i) {
7647 const char *str;
7648 str = umap_mupa_conversion_tests[i];
7649 umap1 = isl_union_map_read_from_str(ctx, str);
7650 umap2 = isl_union_map_copy(umap1);
7651 mupa = isl_multi_union_pw_aff_from_union_map(umap2);
7652 umap2 = isl_union_map_from_multi_union_pw_aff(mupa);
7653 equal = isl_union_map_is_equal(umap1, umap2);
7654 isl_union_map_free(umap1);
7655 isl_union_map_free(umap2);
7657 if (equal < 0)
7658 return -1;
7659 if (!equal)
7660 isl_die(ctx, isl_error_unknown, "bad conversion",
7661 return -1);
7664 return 0;
7667 static int test_conversion(isl_ctx *ctx)
7669 if (test_set_conversion(ctx) < 0)
7670 return -1;
7671 if (test_map_conversion(ctx) < 0)
7672 return -1;
7673 if (test_mpa_conversion(ctx) < 0)
7674 return -1;
7675 if (test_union_map_mupa_conversion(ctx) < 0)
7676 return -1;
7677 return 0;
7680 /* Check that isl_basic_map_curry does not modify input.
7682 static int test_curry(isl_ctx *ctx)
7684 const char *str;
7685 isl_basic_map *bmap1, *bmap2;
7686 int equal;
7688 str = "{ [A[] -> B[]] -> C[] }";
7689 bmap1 = isl_basic_map_read_from_str(ctx, str);
7690 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
7691 equal = isl_basic_map_is_equal(bmap1, bmap2);
7692 isl_basic_map_free(bmap1);
7693 isl_basic_map_free(bmap2);
7695 if (equal < 0)
7696 return -1;
7697 if (equal)
7698 isl_die(ctx, isl_error_unknown,
7699 "curried map should not be equal to original",
7700 return -1);
7702 return 0;
7705 struct {
7706 const char *set;
7707 const char *ma;
7708 const char *res;
7709 } preimage_tests[] = {
7710 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
7711 "{ A[j,i] -> B[i,j] }",
7712 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
7713 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
7714 "{ A[a,b] -> B[a/2,b/6] }",
7715 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
7716 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
7717 "{ A[a,b] -> B[a/2,b/6] }",
7718 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
7719 "exists i,j : a = 2 i and b = 6 j }" },
7720 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
7721 "[n] -> { : 0 <= n <= 100 }" },
7722 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
7723 "{ A[a] -> B[2a] }",
7724 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
7725 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
7726 "{ A[a] -> B[([a/2])] }",
7727 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
7728 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
7729 "{ A[a] -> B[a,a,a/3] }",
7730 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
7731 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
7732 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
7735 static int test_preimage_basic_set(isl_ctx *ctx)
7737 int i;
7738 isl_basic_set *bset1, *bset2;
7739 isl_multi_aff *ma;
7740 int equal;
7742 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
7743 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
7744 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
7745 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
7746 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
7747 equal = isl_basic_set_is_equal(bset1, bset2);
7748 isl_basic_set_free(bset1);
7749 isl_basic_set_free(bset2);
7750 if (equal < 0)
7751 return -1;
7752 if (!equal)
7753 isl_die(ctx, isl_error_unknown, "bad preimage",
7754 return -1);
7757 return 0;
7760 struct {
7761 const char *map;
7762 const char *ma;
7763 const char *res;
7764 } preimage_domain_tests[] = {
7765 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
7766 "{ A[j,i] -> B[i,j] }",
7767 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
7768 { "{ B[i] -> C[i]; D[i] -> E[i] }",
7769 "{ A[i] -> B[i + 1] }",
7770 "{ A[i] -> C[i + 1] }" },
7771 { "{ B[i] -> C[i]; B[i] -> E[i] }",
7772 "{ A[i] -> B[i + 1] }",
7773 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
7774 { "{ B[i] -> C[([i/2])] }",
7775 "{ A[i] -> B[2i] }",
7776 "{ A[i] -> C[i] }" },
7777 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
7778 "{ A[i] -> B[([i/5]), ([i/7])] }",
7779 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
7780 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
7781 "[N] -> { A[] -> B[([N/5])] }",
7782 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
7783 { "{ B[i] -> C[i] : exists a : i = 5 a }",
7784 "{ A[i] -> B[2i] }",
7785 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
7786 { "{ B[i] -> C[i] : exists a : i = 2 a; "
7787 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
7788 "{ A[i] -> B[2i] }",
7789 "{ A[i] -> C[2i] }" },
7790 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
7791 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
7794 static int test_preimage_union_map(isl_ctx *ctx)
7796 int i;
7797 isl_union_map *umap1, *umap2;
7798 isl_multi_aff *ma;
7799 int equal;
7801 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
7802 umap1 = isl_union_map_read_from_str(ctx,
7803 preimage_domain_tests[i].map);
7804 ma = isl_multi_aff_read_from_str(ctx,
7805 preimage_domain_tests[i].ma);
7806 umap2 = isl_union_map_read_from_str(ctx,
7807 preimage_domain_tests[i].res);
7808 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
7809 equal = isl_union_map_is_equal(umap1, umap2);
7810 isl_union_map_free(umap1);
7811 isl_union_map_free(umap2);
7812 if (equal < 0)
7813 return -1;
7814 if (!equal)
7815 isl_die(ctx, isl_error_unknown, "bad preimage",
7816 return -1);
7819 return 0;
7822 static int test_preimage(isl_ctx *ctx)
7824 if (test_preimage_basic_set(ctx) < 0)
7825 return -1;
7826 if (test_preimage_union_map(ctx) < 0)
7827 return -1;
7829 return 0;
7832 struct {
7833 const char *ma1;
7834 const char *ma;
7835 const char *res;
7836 } pullback_tests[] = {
7837 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
7838 "{ A[a,b] -> C[b + 2a] }" },
7839 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
7840 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
7841 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
7842 "{ A[a] -> C[(a)/6] }" },
7843 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
7844 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
7845 "{ A[a] -> C[(2a)/3] }" },
7846 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
7847 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
7848 "{ A[i,j] -> C[i + j, i + j] }"},
7849 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
7850 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
7851 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
7852 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
7853 "{ [i, j] -> [floor((i)/3), j] }",
7854 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
7857 static int test_pullback(isl_ctx *ctx)
7859 int i;
7860 isl_multi_aff *ma1, *ma2;
7861 isl_multi_aff *ma;
7862 int equal;
7864 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
7865 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
7866 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
7867 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
7868 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
7869 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
7870 isl_multi_aff_free(ma1);
7871 isl_multi_aff_free(ma2);
7872 if (equal < 0)
7873 return -1;
7874 if (!equal)
7875 isl_die(ctx, isl_error_unknown, "bad pullback",
7876 return -1);
7879 return 0;
7882 /* Check that negation is printed correctly and that equal expressions
7883 * are correctly identified.
7885 static int test_ast(isl_ctx *ctx)
7887 isl_ast_expr *expr, *expr1, *expr2, *expr3;
7888 char *str;
7889 int ok, equal;
7891 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
7892 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
7893 expr = isl_ast_expr_add(expr1, expr2);
7894 expr2 = isl_ast_expr_copy(expr);
7895 expr = isl_ast_expr_neg(expr);
7896 expr2 = isl_ast_expr_neg(expr2);
7897 equal = isl_ast_expr_is_equal(expr, expr2);
7898 str = isl_ast_expr_to_C_str(expr);
7899 ok = str ? !strcmp(str, "-(A + B)") : -1;
7900 free(str);
7901 isl_ast_expr_free(expr);
7902 isl_ast_expr_free(expr2);
7904 if (ok < 0 || equal < 0)
7905 return -1;
7906 if (!equal)
7907 isl_die(ctx, isl_error_unknown,
7908 "equal expressions not considered equal", return -1);
7909 if (!ok)
7910 isl_die(ctx, isl_error_unknown,
7911 "isl_ast_expr printed incorrectly", return -1);
7913 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
7914 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
7915 expr = isl_ast_expr_add(expr1, expr2);
7916 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
7917 expr = isl_ast_expr_sub(expr3, expr);
7918 str = isl_ast_expr_to_C_str(expr);
7919 ok = str ? !strcmp(str, "C - (A + B)") : -1;
7920 free(str);
7921 isl_ast_expr_free(expr);
7923 if (ok < 0)
7924 return -1;
7925 if (!ok)
7926 isl_die(ctx, isl_error_unknown,
7927 "isl_ast_expr printed incorrectly", return -1);
7929 return 0;
7932 /* Check that isl_ast_build_expr_from_set returns a valid expression
7933 * for an empty set. Note that isl_ast_build_expr_from_set getting
7934 * called on an empty set probably indicates a bug in the caller.
7936 static int test_ast_build(isl_ctx *ctx)
7938 isl_set *set;
7939 isl_ast_build *build;
7940 isl_ast_expr *expr;
7942 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
7943 build = isl_ast_build_from_context(set);
7945 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
7946 expr = isl_ast_build_expr_from_set(build, set);
7948 isl_ast_expr_free(expr);
7949 isl_ast_build_free(build);
7951 if (!expr)
7952 return -1;
7954 return 0;
7957 /* Internal data structure for before_for and after_for callbacks.
7959 * depth is the current depth
7960 * before is the number of times before_for has been called
7961 * after is the number of times after_for has been called
7963 struct isl_test_codegen_data {
7964 int depth;
7965 int before;
7966 int after;
7969 /* This function is called before each for loop in the AST generated
7970 * from test_ast_gen1.
7972 * Increment the number of calls and the depth.
7973 * Check that the space returned by isl_ast_build_get_schedule_space
7974 * matches the target space of the schedule returned by
7975 * isl_ast_build_get_schedule.
7976 * Return an isl_id that is checked by the corresponding call
7977 * to after_for.
7979 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
7980 void *user)
7982 struct isl_test_codegen_data *data = user;
7983 isl_ctx *ctx;
7984 isl_space *space;
7985 isl_union_map *schedule;
7986 isl_union_set *uset;
7987 isl_set *set;
7988 isl_bool empty;
7989 char name[] = "d0";
7991 ctx = isl_ast_build_get_ctx(build);
7993 if (data->before >= 3)
7994 isl_die(ctx, isl_error_unknown,
7995 "unexpected number of for nodes", return NULL);
7996 if (data->depth >= 2)
7997 isl_die(ctx, isl_error_unknown,
7998 "unexpected depth", return NULL);
8000 snprintf(name, sizeof(name), "d%d", data->depth);
8001 data->before++;
8002 data->depth++;
8004 schedule = isl_ast_build_get_schedule(build);
8005 uset = isl_union_map_range(schedule);
8006 if (!uset)
8007 return NULL;
8008 if (isl_union_set_n_set(uset) != 1) {
8009 isl_union_set_free(uset);
8010 isl_die(ctx, isl_error_unknown,
8011 "expecting single range space", return NULL);
8014 space = isl_ast_build_get_schedule_space(build);
8015 set = isl_union_set_extract_set(uset, space);
8016 isl_union_set_free(uset);
8017 empty = isl_set_is_empty(set);
8018 isl_set_free(set);
8020 if (empty < 0)
8021 return NULL;
8022 if (empty)
8023 isl_die(ctx, isl_error_unknown,
8024 "spaces don't match", return NULL);
8026 return isl_id_alloc(ctx, name, NULL);
8029 /* This function is called after each for loop in the AST generated
8030 * from test_ast_gen1.
8032 * Increment the number of calls and decrement the depth.
8033 * Check that the annotation attached to the node matches
8034 * the isl_id returned by the corresponding call to before_for.
8036 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
8037 __isl_keep isl_ast_build *build, void *user)
8039 struct isl_test_codegen_data *data = user;
8040 isl_id *id;
8041 const char *name;
8042 int valid;
8044 data->after++;
8045 data->depth--;
8047 if (data->after > data->before)
8048 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8049 "mismatch in number of for nodes",
8050 return isl_ast_node_free(node));
8052 id = isl_ast_node_get_annotation(node);
8053 if (!id)
8054 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8055 "missing annotation", return isl_ast_node_free(node));
8057 name = isl_id_get_name(id);
8058 valid = name && atoi(name + 1) == data->depth;
8059 isl_id_free(id);
8061 if (!valid)
8062 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8063 "wrong annotation", return isl_ast_node_free(node));
8065 return node;
8068 /* Check that the before_each_for and after_each_for callbacks
8069 * are called for each for loop in the generated code,
8070 * that they are called in the right order and that the isl_id
8071 * returned from the before_each_for callback is attached to
8072 * the isl_ast_node passed to the corresponding after_each_for call.
8074 static int test_ast_gen1(isl_ctx *ctx)
8076 const char *str;
8077 isl_set *set;
8078 isl_union_map *schedule;
8079 isl_ast_build *build;
8080 isl_ast_node *tree;
8081 struct isl_test_codegen_data data;
8083 str = "[N] -> { : N >= 10 }";
8084 set = isl_set_read_from_str(ctx, str);
8085 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
8086 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
8087 schedule = isl_union_map_read_from_str(ctx, str);
8089 data.before = 0;
8090 data.after = 0;
8091 data.depth = 0;
8092 build = isl_ast_build_from_context(set);
8093 build = isl_ast_build_set_before_each_for(build,
8094 &before_for, &data);
8095 build = isl_ast_build_set_after_each_for(build,
8096 &after_for, &data);
8097 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8098 isl_ast_build_free(build);
8099 if (!tree)
8100 return -1;
8102 isl_ast_node_free(tree);
8104 if (data.before != 3 || data.after != 3)
8105 isl_die(ctx, isl_error_unknown,
8106 "unexpected number of for nodes", return -1);
8108 return 0;
8111 /* Check that the AST generator handles domains that are integrally disjoint
8112 * but not rationally disjoint.
8114 static int test_ast_gen2(isl_ctx *ctx)
8116 const char *str;
8117 isl_set *set;
8118 isl_union_map *schedule;
8119 isl_union_map *options;
8120 isl_ast_build *build;
8121 isl_ast_node *tree;
8123 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
8124 schedule = isl_union_map_read_from_str(ctx, str);
8125 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8126 build = isl_ast_build_from_context(set);
8128 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
8129 options = isl_union_map_read_from_str(ctx, str);
8130 build = isl_ast_build_set_options(build, options);
8131 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8132 isl_ast_build_free(build);
8133 if (!tree)
8134 return -1;
8135 isl_ast_node_free(tree);
8137 return 0;
8140 /* Increment *user on each call.
8142 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
8143 __isl_keep isl_ast_build *build, void *user)
8145 int *n = user;
8147 (*n)++;
8149 return node;
8152 /* Test that unrolling tries to minimize the number of instances.
8153 * In particular, for the schedule given below, make sure it generates
8154 * 3 nodes (rather than 101).
8156 static int test_ast_gen3(isl_ctx *ctx)
8158 const char *str;
8159 isl_set *set;
8160 isl_union_map *schedule;
8161 isl_union_map *options;
8162 isl_ast_build *build;
8163 isl_ast_node *tree;
8164 int n_domain = 0;
8166 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
8167 schedule = isl_union_map_read_from_str(ctx, str);
8168 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8170 str = "{ [i] -> unroll[0] }";
8171 options = isl_union_map_read_from_str(ctx, str);
8173 build = isl_ast_build_from_context(set);
8174 build = isl_ast_build_set_options(build, options);
8175 build = isl_ast_build_set_at_each_domain(build,
8176 &count_domains, &n_domain);
8177 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8178 isl_ast_build_free(build);
8179 if (!tree)
8180 return -1;
8182 isl_ast_node_free(tree);
8184 if (n_domain != 3)
8185 isl_die(ctx, isl_error_unknown,
8186 "unexpected number of for nodes", return -1);
8188 return 0;
8191 /* Check that if the ast_build_exploit_nested_bounds options is set,
8192 * we do not get an outer if node in the generated AST,
8193 * while we do get such an outer if node if the options is not set.
8195 static int test_ast_gen4(isl_ctx *ctx)
8197 const char *str;
8198 isl_set *set;
8199 isl_union_map *schedule;
8200 isl_ast_build *build;
8201 isl_ast_node *tree;
8202 enum isl_ast_node_type type;
8203 int enb;
8205 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
8206 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
8208 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
8210 schedule = isl_union_map_read_from_str(ctx, str);
8211 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8212 build = isl_ast_build_from_context(set);
8213 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8214 isl_ast_build_free(build);
8215 if (!tree)
8216 return -1;
8218 type = isl_ast_node_get_type(tree);
8219 isl_ast_node_free(tree);
8221 if (type == isl_ast_node_if)
8222 isl_die(ctx, isl_error_unknown,
8223 "not expecting if node", return -1);
8225 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
8227 schedule = isl_union_map_read_from_str(ctx, str);
8228 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8229 build = isl_ast_build_from_context(set);
8230 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8231 isl_ast_build_free(build);
8232 if (!tree)
8233 return -1;
8235 type = isl_ast_node_get_type(tree);
8236 isl_ast_node_free(tree);
8238 if (type != isl_ast_node_if)
8239 isl_die(ctx, isl_error_unknown,
8240 "expecting if node", return -1);
8242 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
8244 return 0;
8247 /* This function is called for each leaf in the AST generated
8248 * from test_ast_gen5.
8250 * We finalize the AST generation by extending the outer schedule
8251 * with a zero-dimensional schedule. If this results in any for loops,
8252 * then this means that we did not pass along enough information
8253 * about the outer schedule to the inner AST generation.
8255 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
8256 void *user)
8258 isl_union_map *schedule, *extra;
8259 isl_ast_node *tree;
8261 schedule = isl_ast_build_get_schedule(build);
8262 extra = isl_union_map_copy(schedule);
8263 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
8264 schedule = isl_union_map_range_product(schedule, extra);
8265 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8266 isl_ast_build_free(build);
8268 if (!tree)
8269 return NULL;
8271 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
8272 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
8273 "code should not contain any for loop",
8274 return isl_ast_node_free(tree));
8276 return tree;
8279 /* Check that we do not lose any information when going back and
8280 * forth between internal and external schedule.
8282 * In particular, we create an AST where we unroll the only
8283 * non-constant dimension in the schedule. We therefore do
8284 * not expect any for loops in the AST. However, older versions
8285 * of isl would not pass along enough information about the outer
8286 * schedule when performing an inner code generation from a create_leaf
8287 * callback, resulting in the inner code generation producing a for loop.
8289 static int test_ast_gen5(isl_ctx *ctx)
8291 const char *str;
8292 isl_set *set;
8293 isl_union_map *schedule, *options;
8294 isl_ast_build *build;
8295 isl_ast_node *tree;
8297 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
8298 schedule = isl_union_map_read_from_str(ctx, str);
8300 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
8301 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
8302 options = isl_union_map_read_from_str(ctx, str);
8304 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8305 build = isl_ast_build_from_context(set);
8306 build = isl_ast_build_set_options(build, options);
8307 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
8308 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8309 isl_ast_build_free(build);
8310 isl_ast_node_free(tree);
8311 if (!tree)
8312 return -1;
8314 return 0;
8317 /* Check that the expression
8319 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
8321 * is not combined into
8323 * min(n/2, 0)
8325 * as this would result in n/2 being evaluated in parts of
8326 * the definition domain where n is not a multiple of 2.
8328 static int test_ast_expr(isl_ctx *ctx)
8330 const char *str;
8331 isl_pw_aff *pa;
8332 isl_ast_build *build;
8333 isl_ast_expr *expr;
8334 int min_max;
8335 int is_min;
8337 min_max = isl_options_get_ast_build_detect_min_max(ctx);
8338 isl_options_set_ast_build_detect_min_max(ctx, 1);
8340 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
8341 pa = isl_pw_aff_read_from_str(ctx, str);
8342 build = isl_ast_build_alloc(ctx);
8343 expr = isl_ast_build_expr_from_pw_aff(build, pa);
8344 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
8345 isl_ast_expr_get_op_type(expr) == isl_ast_op_min;
8346 isl_ast_build_free(build);
8347 isl_ast_expr_free(expr);
8349 isl_options_set_ast_build_detect_min_max(ctx, min_max);
8351 if (!expr)
8352 return -1;
8353 if (is_min)
8354 isl_die(ctx, isl_error_unknown,
8355 "expressions should not be combined", return -1);
8357 return 0;
8360 static int test_ast_gen(isl_ctx *ctx)
8362 if (test_ast_gen1(ctx) < 0)
8363 return -1;
8364 if (test_ast_gen2(ctx) < 0)
8365 return -1;
8366 if (test_ast_gen3(ctx) < 0)
8367 return -1;
8368 if (test_ast_gen4(ctx) < 0)
8369 return -1;
8370 if (test_ast_gen5(ctx) < 0)
8371 return -1;
8372 if (test_ast_expr(ctx) < 0)
8373 return -1;
8374 return 0;
8377 /* Check if dropping output dimensions from an isl_pw_multi_aff
8378 * works properly.
8380 static int test_pw_multi_aff(isl_ctx *ctx)
8382 const char *str;
8383 isl_pw_multi_aff *pma1, *pma2;
8384 int equal;
8386 str = "{ [i,j] -> [i+j, 4i-j] }";
8387 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
8388 str = "{ [i,j] -> [4i-j] }";
8389 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
8391 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
8393 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
8395 isl_pw_multi_aff_free(pma1);
8396 isl_pw_multi_aff_free(pma2);
8397 if (equal < 0)
8398 return -1;
8399 if (!equal)
8400 isl_die(ctx, isl_error_unknown,
8401 "expressions not equal", return -1);
8403 return 0;
8406 /* Check that we can properly parse multi piecewise affine expressions
8407 * where the piecewise affine expressions have different domains.
8409 static int test_multi_pw_aff_1(isl_ctx *ctx)
8411 const char *str;
8412 isl_set *dom, *dom2;
8413 isl_multi_pw_aff *mpa1, *mpa2;
8414 isl_pw_aff *pa;
8415 int equal;
8416 int equal_domain;
8418 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
8419 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
8420 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
8421 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
8422 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
8423 str = "{ [i] -> [(i : i > 0), 2i] }";
8424 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
8426 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
8428 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
8429 dom = isl_pw_aff_domain(pa);
8430 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
8431 dom2 = isl_pw_aff_domain(pa);
8432 equal_domain = isl_set_is_equal(dom, dom2);
8434 isl_set_free(dom);
8435 isl_set_free(dom2);
8436 isl_multi_pw_aff_free(mpa1);
8437 isl_multi_pw_aff_free(mpa2);
8439 if (equal < 0)
8440 return -1;
8441 if (!equal)
8442 isl_die(ctx, isl_error_unknown,
8443 "expressions not equal", return -1);
8445 if (equal_domain < 0)
8446 return -1;
8447 if (equal_domain)
8448 isl_die(ctx, isl_error_unknown,
8449 "domains unexpectedly equal", return -1);
8451 return 0;
8454 /* Check that the dimensions in the explicit domain
8455 * of a multi piecewise affine expression are properly
8456 * taken into account.
8458 static int test_multi_pw_aff_2(isl_ctx *ctx)
8460 const char *str;
8461 isl_bool involves1, involves2, involves3, equal;
8462 isl_multi_pw_aff *mpa, *mpa1, *mpa2;
8464 str = "{ A[x,y] -> B[] : x >= y }";
8465 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
8466 involves1 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 2);
8467 mpa1 = isl_multi_pw_aff_copy(mpa);
8469 mpa = isl_multi_pw_aff_insert_dims(mpa, isl_dim_in, 0, 1);
8470 involves2 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 1);
8471 involves3 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 1, 2);
8472 str = "{ [a,x,y] -> B[] : x >= y }";
8473 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
8474 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
8475 isl_multi_pw_aff_free(mpa2);
8477 mpa = isl_multi_pw_aff_drop_dims(mpa, isl_dim_in, 0, 1);
8478 mpa = isl_multi_pw_aff_set_tuple_name(mpa, isl_dim_in, "A");
8479 if (equal >= 0 && equal)
8480 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa1);
8481 isl_multi_pw_aff_free(mpa1);
8482 isl_multi_pw_aff_free(mpa);
8484 if (involves1 < 0 || involves2 < 0 || involves3 < 0 || equal < 0)
8485 return -1;
8486 if (!equal)
8487 isl_die(ctx, isl_error_unknown,
8488 "incorrect result of dimension insertion/removal",
8489 return isl_stat_error);
8490 if (!involves1 || involves2 || !involves3)
8491 isl_die(ctx, isl_error_unknown,
8492 "incorrect characterization of involved dimensions",
8493 return isl_stat_error);
8495 return 0;
8498 /* Check that isl_multi_union_pw_aff_multi_val_on_domain
8499 * sets the explicit domain of a zero-dimensional result,
8500 * such that it can be converted to an isl_union_map.
8502 static isl_stat test_multi_pw_aff_3(isl_ctx *ctx)
8504 isl_space *space;
8505 isl_union_set *dom;
8506 isl_multi_val *mv;
8507 isl_multi_union_pw_aff *mupa;
8508 isl_union_map *umap;
8510 dom = isl_union_set_read_from_str(ctx, "{ A[]; B[] }");
8511 space = isl_union_set_get_space(dom);
8512 mv = isl_multi_val_zero(isl_space_set_from_params(space));
8513 mupa = isl_multi_union_pw_aff_multi_val_on_domain(dom, mv);
8514 umap = isl_union_map_from_multi_union_pw_aff(mupa);
8515 isl_union_map_free(umap);
8516 if (!umap)
8517 return isl_stat_error;
8519 return isl_stat_ok;
8522 /* Perform some tests on multi piecewise affine expressions.
8524 static int test_multi_pw_aff(isl_ctx *ctx)
8526 if (test_multi_pw_aff_1(ctx) < 0)
8527 return -1;
8528 if (test_multi_pw_aff_2(ctx) < 0)
8529 return -1;
8530 if (test_multi_pw_aff_3(ctx) < 0)
8531 return -1;
8532 return 0;
8535 /* This is a regression test for a bug where isl_basic_map_simplify
8536 * would end up in an infinite loop. In particular, we construct
8537 * an empty basic set that is not obviously empty.
8538 * isl_basic_set_is_empty marks the basic set as empty.
8539 * After projecting out i3, the variable can be dropped completely,
8540 * but isl_basic_map_simplify refrains from doing so if the basic set
8541 * is empty and would end up in an infinite loop if it didn't test
8542 * explicitly for empty basic maps in the outer loop.
8544 static int test_simplify_1(isl_ctx *ctx)
8546 const char *str;
8547 isl_basic_set *bset;
8548 int empty;
8550 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
8551 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
8552 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
8553 "i3 >= i2 }";
8554 bset = isl_basic_set_read_from_str(ctx, str);
8555 empty = isl_basic_set_is_empty(bset);
8556 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
8557 isl_basic_set_free(bset);
8558 if (!bset)
8559 return -1;
8560 if (!empty)
8561 isl_die(ctx, isl_error_unknown,
8562 "basic set should be empty", return -1);
8564 return 0;
8567 /* Check that the equality in the set description below
8568 * is simplified away.
8570 static int test_simplify_2(isl_ctx *ctx)
8572 const char *str;
8573 isl_basic_set *bset;
8574 isl_bool universe;
8576 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
8577 bset = isl_basic_set_read_from_str(ctx, str);
8578 universe = isl_basic_set_plain_is_universe(bset);
8579 isl_basic_set_free(bset);
8581 if (universe < 0)
8582 return -1;
8583 if (!universe)
8584 isl_die(ctx, isl_error_unknown,
8585 "equality not simplified away", return -1);
8586 return 0;
8589 /* Some simplification tests.
8591 static int test_simplify(isl_ctx *ctx)
8593 if (test_simplify_1(ctx) < 0)
8594 return -1;
8595 if (test_simplify_2(ctx) < 0)
8596 return -1;
8597 return 0;
8600 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
8601 * with gbr context would fail to disable the use of the shifted tableau
8602 * when transferring equalities for the input to the context, resulting
8603 * in invalid sample values.
8605 static int test_partial_lexmin(isl_ctx *ctx)
8607 const char *str;
8608 isl_basic_set *bset;
8609 isl_basic_map *bmap;
8610 isl_map *map;
8612 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
8613 bmap = isl_basic_map_read_from_str(ctx, str);
8614 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
8615 bset = isl_basic_set_read_from_str(ctx, str);
8616 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
8617 isl_map_free(map);
8619 if (!map)
8620 return -1;
8622 return 0;
8625 /* Check that the variable compression performed on the existentially
8626 * quantified variables inside isl_basic_set_compute_divs is not confused
8627 * by the implicit equalities among the parameters.
8629 static int test_compute_divs(isl_ctx *ctx)
8631 const char *str;
8632 isl_basic_set *bset;
8633 isl_set *set;
8635 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
8636 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
8637 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
8638 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
8639 bset = isl_basic_set_read_from_str(ctx, str);
8640 set = isl_basic_set_compute_divs(bset);
8641 isl_set_free(set);
8642 if (!set)
8643 return -1;
8645 return 0;
8648 /* Check that isl_schedule_get_map is not confused by a schedule tree
8649 * with divergent filter node parameters, as can result from a call
8650 * to isl_schedule_intersect_domain.
8652 static int test_schedule_tree(isl_ctx *ctx)
8654 const char *str;
8655 isl_union_set *uset;
8656 isl_schedule *sched1, *sched2;
8657 isl_union_map *umap;
8659 uset = isl_union_set_read_from_str(ctx, "{ A[i] }");
8660 sched1 = isl_schedule_from_domain(uset);
8661 uset = isl_union_set_read_from_str(ctx, "{ B[] }");
8662 sched2 = isl_schedule_from_domain(uset);
8664 sched1 = isl_schedule_sequence(sched1, sched2);
8665 str = "[n] -> { A[i] : 0 <= i < n; B[] }";
8666 uset = isl_union_set_read_from_str(ctx, str);
8667 sched1 = isl_schedule_intersect_domain(sched1, uset);
8668 umap = isl_schedule_get_map(sched1);
8669 isl_schedule_free(sched1);
8670 isl_union_map_free(umap);
8671 if (!umap)
8672 return -1;
8674 return 0;
8677 /* Check that a zero-dimensional prefix schedule keeps track
8678 * of the domain and outer filters.
8680 static int test_schedule_tree_prefix(isl_ctx *ctx)
8682 const char *str;
8683 isl_bool equal;
8684 isl_union_set *uset;
8685 isl_union_set_list *filters;
8686 isl_multi_union_pw_aff *mupa, *mupa2;
8687 isl_schedule_node *node;
8689 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
8690 uset = isl_union_set_read_from_str(ctx, str);
8691 node = isl_schedule_node_from_domain(uset);
8692 node = isl_schedule_node_child(node, 0);
8694 str = "{ S1[i,j] : i > j }";
8695 uset = isl_union_set_read_from_str(ctx, str);
8696 filters = isl_union_set_list_from_union_set(uset);
8697 str = "{ S1[i,j] : i <= j; S2[i,j] }";
8698 uset = isl_union_set_read_from_str(ctx, str);
8699 filters = isl_union_set_list_add(filters, uset);
8700 node = isl_schedule_node_insert_sequence(node, filters);
8702 node = isl_schedule_node_child(node, 0);
8703 node = isl_schedule_node_child(node, 0);
8704 mupa = isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
8705 str = "([] : { S1[i,j] : i > j })";
8706 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx, str);
8707 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
8708 isl_multi_union_pw_aff_free(mupa2);
8709 isl_multi_union_pw_aff_free(mupa);
8710 isl_schedule_node_free(node);
8712 if (equal < 0)
8713 return -1;
8714 if (!equal)
8715 isl_die(ctx, isl_error_unknown, "unexpected prefix schedule",
8716 return -1);
8718 return 0;
8721 /* Check that the reaching domain elements and the prefix schedule
8722 * at a leaf node are the same before and after grouping.
8724 static int test_schedule_tree_group_1(isl_ctx *ctx)
8726 int equal;
8727 const char *str;
8728 isl_id *id;
8729 isl_union_set *uset;
8730 isl_multi_union_pw_aff *mupa;
8731 isl_union_pw_multi_aff *upma1, *upma2;
8732 isl_union_set *domain1, *domain2;
8733 isl_union_map *umap1, *umap2;
8734 isl_schedule_node *node;
8736 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
8737 uset = isl_union_set_read_from_str(ctx, str);
8738 node = isl_schedule_node_from_domain(uset);
8739 node = isl_schedule_node_child(node, 0);
8740 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
8741 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8742 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8743 node = isl_schedule_node_child(node, 0);
8744 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
8745 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8746 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8747 node = isl_schedule_node_child(node, 0);
8748 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
8749 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
8750 domain1 = isl_schedule_node_get_domain(node);
8751 id = isl_id_alloc(ctx, "group", NULL);
8752 node = isl_schedule_node_parent(node);
8753 node = isl_schedule_node_group(node, id);
8754 node = isl_schedule_node_child(node, 0);
8755 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
8756 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
8757 domain2 = isl_schedule_node_get_domain(node);
8758 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
8759 if (equal >= 0 && equal)
8760 equal = isl_union_set_is_equal(domain1, domain2);
8761 if (equal >= 0 && equal)
8762 equal = isl_union_map_is_equal(umap1, umap2);
8763 isl_union_map_free(umap1);
8764 isl_union_map_free(umap2);
8765 isl_union_set_free(domain1);
8766 isl_union_set_free(domain2);
8767 isl_union_pw_multi_aff_free(upma1);
8768 isl_union_pw_multi_aff_free(upma2);
8769 isl_schedule_node_free(node);
8771 if (equal < 0)
8772 return -1;
8773 if (!equal)
8774 isl_die(ctx, isl_error_unknown,
8775 "expressions not equal", return -1);
8777 return 0;
8780 /* Check that we can have nested groupings and that the union map
8781 * schedule representation is the same before and after the grouping.
8782 * Note that after the grouping, the union map representation contains
8783 * the domain constraints from the ranges of the expansion nodes,
8784 * while they are missing from the union map representation of
8785 * the tree without expansion nodes.
8787 * Also check that the global expansion is as expected.
8789 static int test_schedule_tree_group_2(isl_ctx *ctx)
8791 int equal, equal_expansion;
8792 const char *str;
8793 isl_id *id;
8794 isl_union_set *uset;
8795 isl_union_map *umap1, *umap2;
8796 isl_union_map *expansion1, *expansion2;
8797 isl_union_set_list *filters;
8798 isl_multi_union_pw_aff *mupa;
8799 isl_schedule *schedule;
8800 isl_schedule_node *node;
8802 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
8803 "S3[i,j] : 0 <= i,j < 10 }";
8804 uset = isl_union_set_read_from_str(ctx, str);
8805 node = isl_schedule_node_from_domain(uset);
8806 node = isl_schedule_node_child(node, 0);
8807 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
8808 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8809 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8810 node = isl_schedule_node_child(node, 0);
8811 str = "{ S1[i,j] }";
8812 uset = isl_union_set_read_from_str(ctx, str);
8813 filters = isl_union_set_list_from_union_set(uset);
8814 str = "{ S2[i,j]; S3[i,j] }";
8815 uset = isl_union_set_read_from_str(ctx, str);
8816 filters = isl_union_set_list_add(filters, uset);
8817 node = isl_schedule_node_insert_sequence(node, filters);
8818 node = isl_schedule_node_child(node, 1);
8819 node = isl_schedule_node_child(node, 0);
8820 str = "{ S2[i,j] }";
8821 uset = isl_union_set_read_from_str(ctx, str);
8822 filters = isl_union_set_list_from_union_set(uset);
8823 str = "{ S3[i,j] }";
8824 uset = isl_union_set_read_from_str(ctx, str);
8825 filters = isl_union_set_list_add(filters, uset);
8826 node = isl_schedule_node_insert_sequence(node, filters);
8828 schedule = isl_schedule_node_get_schedule(node);
8829 umap1 = isl_schedule_get_map(schedule);
8830 uset = isl_schedule_get_domain(schedule);
8831 umap1 = isl_union_map_intersect_domain(umap1, uset);
8832 isl_schedule_free(schedule);
8834 node = isl_schedule_node_parent(node);
8835 node = isl_schedule_node_parent(node);
8836 id = isl_id_alloc(ctx, "group1", NULL);
8837 node = isl_schedule_node_group(node, id);
8838 node = isl_schedule_node_child(node, 1);
8839 node = isl_schedule_node_child(node, 0);
8840 id = isl_id_alloc(ctx, "group2", NULL);
8841 node = isl_schedule_node_group(node, id);
8843 schedule = isl_schedule_node_get_schedule(node);
8844 umap2 = isl_schedule_get_map(schedule);
8845 isl_schedule_free(schedule);
8847 node = isl_schedule_node_root(node);
8848 node = isl_schedule_node_child(node, 0);
8849 expansion1 = isl_schedule_node_get_subtree_expansion(node);
8850 isl_schedule_node_free(node);
8852 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
8853 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
8854 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
8856 expansion2 = isl_union_map_read_from_str(ctx, str);
8858 equal = isl_union_map_is_equal(umap1, umap2);
8859 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
8861 isl_union_map_free(umap1);
8862 isl_union_map_free(umap2);
8863 isl_union_map_free(expansion1);
8864 isl_union_map_free(expansion2);
8866 if (equal < 0 || equal_expansion < 0)
8867 return -1;
8868 if (!equal)
8869 isl_die(ctx, isl_error_unknown,
8870 "expressions not equal", return -1);
8871 if (!equal_expansion)
8872 isl_die(ctx, isl_error_unknown,
8873 "unexpected expansion", return -1);
8875 return 0;
8878 /* Some tests for the isl_schedule_node_group function.
8880 static int test_schedule_tree_group(isl_ctx *ctx)
8882 if (test_schedule_tree_group_1(ctx) < 0)
8883 return -1;
8884 if (test_schedule_tree_group_2(ctx) < 0)
8885 return -1;
8886 return 0;
8889 struct {
8890 const char *set;
8891 const char *dual;
8892 } coef_tests[] = {
8893 { "{ rat: [i] : 0 <= i <= 10 }",
8894 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
8895 { "{ rat: [i] : FALSE }",
8896 "{ rat: coefficients[[cst] -> [a]] }" },
8897 { "{ rat: [i] : }",
8898 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
8901 struct {
8902 const char *set;
8903 const char *dual;
8904 } sol_tests[] = {
8905 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
8906 "{ rat: [i] : 0 <= i <= 10 }" },
8907 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
8908 "{ rat: [i] }" },
8909 { "{ rat: coefficients[[cst] -> [a]] }",
8910 "{ rat: [i] : FALSE }" },
8913 /* Test the basic functionality of isl_basic_set_coefficients and
8914 * isl_basic_set_solutions.
8916 static int test_dual(isl_ctx *ctx)
8918 int i;
8920 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
8921 int equal;
8922 isl_basic_set *bset1, *bset2;
8924 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
8925 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
8926 bset1 = isl_basic_set_coefficients(bset1);
8927 equal = isl_basic_set_is_equal(bset1, bset2);
8928 isl_basic_set_free(bset1);
8929 isl_basic_set_free(bset2);
8930 if (equal < 0)
8931 return -1;
8932 if (!equal)
8933 isl_die(ctx, isl_error_unknown,
8934 "incorrect dual", return -1);
8937 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
8938 int equal;
8939 isl_basic_set *bset1, *bset2;
8941 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
8942 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
8943 bset1 = isl_basic_set_solutions(bset1);
8944 equal = isl_basic_set_is_equal(bset1, bset2);
8945 isl_basic_set_free(bset1);
8946 isl_basic_set_free(bset2);
8947 if (equal < 0)
8948 return -1;
8949 if (!equal)
8950 isl_die(ctx, isl_error_unknown,
8951 "incorrect dual", return -1);
8954 return 0;
8957 struct {
8958 int scale_tile;
8959 int shift_point;
8960 const char *domain;
8961 const char *schedule;
8962 const char *sizes;
8963 const char *tile;
8964 const char *point;
8965 } tile_tests[] = {
8966 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
8967 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
8968 "{ [32,32] }",
8969 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
8970 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
8972 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
8973 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
8974 "{ [32,32] }",
8975 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
8976 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
8978 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
8979 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
8980 "{ [32,32] }",
8981 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
8982 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
8984 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
8985 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
8986 "{ [32,32] }",
8987 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
8988 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
8992 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
8993 * tile the band and then check if the tile and point bands have the
8994 * expected partial schedule.
8996 static int test_tile(isl_ctx *ctx)
8998 int i;
8999 int scale;
9000 int shift;
9002 scale = isl_options_get_tile_scale_tile_loops(ctx);
9003 shift = isl_options_get_tile_shift_point_loops(ctx);
9005 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
9006 int opt;
9007 int equal;
9008 const char *str;
9009 isl_union_set *domain;
9010 isl_multi_union_pw_aff *mupa, *mupa2;
9011 isl_schedule_node *node;
9012 isl_multi_val *sizes;
9014 opt = tile_tests[i].scale_tile;
9015 isl_options_set_tile_scale_tile_loops(ctx, opt);
9016 opt = tile_tests[i].shift_point;
9017 isl_options_set_tile_shift_point_loops(ctx, opt);
9019 str = tile_tests[i].domain;
9020 domain = isl_union_set_read_from_str(ctx, str);
9021 node = isl_schedule_node_from_domain(domain);
9022 node = isl_schedule_node_child(node, 0);
9023 str = tile_tests[i].schedule;
9024 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9025 node = isl_schedule_node_insert_partial_schedule(node, mupa);
9026 str = tile_tests[i].sizes;
9027 sizes = isl_multi_val_read_from_str(ctx, str);
9028 node = isl_schedule_node_band_tile(node, sizes);
9030 str = tile_tests[i].tile;
9031 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9032 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9033 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
9034 isl_multi_union_pw_aff_free(mupa);
9035 isl_multi_union_pw_aff_free(mupa2);
9037 node = isl_schedule_node_child(node, 0);
9039 str = tile_tests[i].point;
9040 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9041 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9042 if (equal >= 0 && equal)
9043 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
9044 mupa2);
9045 isl_multi_union_pw_aff_free(mupa);
9046 isl_multi_union_pw_aff_free(mupa2);
9048 isl_schedule_node_free(node);
9050 if (equal < 0)
9051 return -1;
9052 if (!equal)
9053 isl_die(ctx, isl_error_unknown,
9054 "unexpected result", return -1);
9057 isl_options_set_tile_scale_tile_loops(ctx, scale);
9058 isl_options_set_tile_shift_point_loops(ctx, shift);
9060 return 0;
9063 /* Check that the domain hash of a space is equal to the hash
9064 * of the domain of the space.
9066 static int test_domain_hash(isl_ctx *ctx)
9068 isl_map *map;
9069 isl_space *space;
9070 uint32_t hash1, hash2;
9072 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
9073 space = isl_map_get_space(map);
9074 isl_map_free(map);
9075 hash1 = isl_space_get_domain_hash(space);
9076 space = isl_space_domain(space);
9077 hash2 = isl_space_get_hash(space);
9078 isl_space_free(space);
9080 if (!space)
9081 return -1;
9082 if (hash1 != hash2)
9083 isl_die(ctx, isl_error_unknown,
9084 "domain hash not equal to hash of domain", return -1);
9086 return 0;
9089 /* Check that a universe basic set that is not obviously equal to the universe
9090 * is still recognized as being equal to the universe.
9092 static int test_universe(isl_ctx *ctx)
9094 const char *s;
9095 isl_basic_set *bset;
9096 isl_bool is_univ;
9098 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
9099 bset = isl_basic_set_read_from_str(ctx, s);
9100 is_univ = isl_basic_set_is_universe(bset);
9101 isl_basic_set_free(bset);
9103 if (is_univ < 0)
9104 return -1;
9105 if (!is_univ)
9106 isl_die(ctx, isl_error_unknown,
9107 "not recognized as universe set", return -1);
9109 return 0;
9112 /* Sets for which chambers are computed and checked.
9114 const char *chambers_tests[] = {
9115 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
9116 "z >= 0 and z <= C - y and z <= B - x - y }",
9119 /* Add the domain of "cell" to "cells".
9121 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
9123 isl_basic_set_list **cells = user;
9124 isl_basic_set *dom;
9126 dom = isl_cell_get_domain(cell);
9127 isl_cell_free(cell);
9128 *cells = isl_basic_set_list_add(*cells, dom);
9130 return *cells ? isl_stat_ok : isl_stat_error;
9133 /* Check that the elements of "list" are pairwise disjoint.
9135 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
9137 int i, j, n;
9139 if (!list)
9140 return isl_stat_error;
9142 n = isl_basic_set_list_n_basic_set(list);
9143 for (i = 0; i < n; ++i) {
9144 isl_basic_set *bset_i;
9146 bset_i = isl_basic_set_list_get_basic_set(list, i);
9147 for (j = i + 1; j < n; ++j) {
9148 isl_basic_set *bset_j;
9149 isl_bool disjoint;
9151 bset_j = isl_basic_set_list_get_basic_set(list, j);
9152 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
9153 isl_basic_set_free(bset_j);
9154 if (!disjoint)
9155 isl_die(isl_basic_set_list_get_ctx(list),
9156 isl_error_unknown, "not disjoint",
9157 break);
9158 if (disjoint < 0 || !disjoint)
9159 break;
9161 isl_basic_set_free(bset_i);
9162 if (j < n)
9163 return isl_stat_error;
9166 return isl_stat_ok;
9169 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
9170 * are pairwise disjoint.
9172 static int test_chambers(isl_ctx *ctx)
9174 int i;
9176 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
9177 isl_basic_set *bset;
9178 isl_vertices *vertices;
9179 isl_basic_set_list *cells;
9180 isl_stat ok;
9182 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
9183 vertices = isl_basic_set_compute_vertices(bset);
9184 cells = isl_basic_set_list_alloc(ctx, 0);
9185 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
9186 &cells) < 0)
9187 cells = isl_basic_set_list_free(cells);
9188 ok = check_pairwise_disjoint(cells);
9189 isl_basic_set_list_free(cells);
9190 isl_vertices_free(vertices);
9191 isl_basic_set_free(bset);
9193 if (ok < 0)
9194 return -1;
9197 return 0;
9200 struct {
9201 const char *name;
9202 int (*fn)(isl_ctx *ctx);
9203 } tests [] = {
9204 { "universe", &test_universe },
9205 { "domain hash", &test_domain_hash },
9206 { "dual", &test_dual },
9207 { "dependence analysis", &test_flow },
9208 { "val", &test_val },
9209 { "compute divs", &test_compute_divs },
9210 { "partial lexmin", &test_partial_lexmin },
9211 { "simplify", &test_simplify },
9212 { "curry", &test_curry },
9213 { "piecewise multi affine expressions", &test_pw_multi_aff },
9214 { "multi piecewise affine expressions", &test_multi_pw_aff },
9215 { "conversion", &test_conversion },
9216 { "list", &test_list },
9217 { "align parameters", &test_align_parameters },
9218 { "drop unused parameters", &test_drop_unused_parameters },
9219 { "preimage", &test_preimage },
9220 { "pullback", &test_pullback },
9221 { "AST", &test_ast },
9222 { "AST build", &test_ast_build },
9223 { "AST generation", &test_ast_gen },
9224 { "eliminate", &test_eliminate },
9225 { "residue class", &test_residue_class },
9226 { "div", &test_div },
9227 { "slice", &test_slice },
9228 { "fixed power", &test_fixed_power },
9229 { "sample", &test_sample },
9230 { "output", &test_output },
9231 { "vertices", &test_vertices },
9232 { "chambers", &test_chambers },
9233 { "fixed", &test_fixed },
9234 { "equal", &test_equal },
9235 { "disjoint", &test_disjoint },
9236 { "product", &test_product },
9237 { "dim_max", &test_dim_max },
9238 { "affine", &test_aff },
9239 { "injective", &test_injective },
9240 { "schedule (whole component)", &test_schedule_whole },
9241 { "schedule (incremental)", &test_schedule_incremental },
9242 { "schedule tree", &test_schedule_tree },
9243 { "schedule tree prefix", &test_schedule_tree_prefix },
9244 { "schedule tree grouping", &test_schedule_tree_group },
9245 { "tile", &test_tile },
9246 { "union_pw", &test_union_pw },
9247 { "locus", &test_locus },
9248 { "eval", &test_eval },
9249 { "parse", &test_parse },
9250 { "single-valued", &test_sv },
9251 { "affine hull", &test_affine_hull },
9252 { "simple_hull", &test_simple_hull },
9253 { "coalesce", &test_coalesce },
9254 { "factorize", &test_factorize },
9255 { "subset", &test_subset },
9256 { "subtract", &test_subtract },
9257 { "intersect", &test_intersect },
9258 { "lexmin", &test_lexmin },
9259 { "min", &test_min },
9260 { "gist", &test_gist },
9261 { "piecewise quasi-polynomials", &test_pwqp },
9262 { "lift", &test_lift },
9263 { "bound", &test_bound },
9264 { "get lists", &test_get_list },
9265 { "union", &test_union },
9266 { "split periods", &test_split_periods },
9267 { "lexicographic order", &test_lex },
9268 { "bijectivity", &test_bijective },
9269 { "dataflow analysis", &test_dep },
9270 { "reading", &test_read },
9271 { "bounded", &test_bounded },
9272 { "construction", &test_construction },
9273 { "dimension manipulation", &test_dim },
9274 { "map application", &test_application },
9275 { "convex hull", &test_convex_hull },
9276 { "transitive closure", &test_closure },
9279 int main(int argc, char **argv)
9281 int i;
9282 struct isl_ctx *ctx;
9283 struct isl_options *options;
9285 options = isl_options_new_with_defaults();
9286 assert(options);
9287 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
9289 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
9290 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
9291 printf("%s\n", tests[i].name);
9292 if (tests[i].fn(ctx) < 0)
9293 goto error;
9295 isl_ctx_free(ctx);
9296 return 0;
9297 error:
9298 isl_ctx_free(ctx);
9299 return -1;