generator: extract out shared is_isl_primitive
[isl.git] / isl_test.c
blob2f64f2d493c79eb618603ad73e189ec0591a08ef
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 /* Check that the actual result of a boolean operation is equal
2606 * to the expected result.
2608 static isl_stat check_bool(isl_ctx *ctx, isl_bool actual, isl_bool expected)
2610 if (actual != expected)
2611 isl_die(ctx, isl_error_unknown,
2612 "incorrect boolean operation", return isl_stat_error);
2613 return isl_stat_ok;
2616 /* Test operations on isl_bool values.
2618 * This tests:
2620 * isl_bool_not
2621 * isl_bool_ok
2623 static int test_isl_bool(isl_ctx *ctx)
2625 if (check_bool(ctx, isl_bool_not(isl_bool_true), isl_bool_false) < 0)
2626 return -1;
2627 if (check_bool(ctx, isl_bool_not(isl_bool_false), isl_bool_true) < 0)
2628 return -1;
2629 if (check_bool(ctx, isl_bool_not(isl_bool_error), isl_bool_error) < 0)
2630 return -1;
2631 if (check_bool(ctx, isl_bool_ok(0), isl_bool_false) < 0)
2632 return -1;
2633 if (check_bool(ctx, isl_bool_ok(1), isl_bool_true) < 0)
2634 return -1;
2635 if (check_bool(ctx, isl_bool_ok(-1), isl_bool_true) < 0)
2636 return -1;
2637 if (check_bool(ctx, isl_bool_ok(2), isl_bool_true) < 0)
2638 return -1;
2639 if (check_bool(ctx, isl_bool_ok(-2), isl_bool_true) < 0)
2640 return -1;
2642 return 0;
2645 static int test_lex(struct isl_ctx *ctx)
2647 isl_space *dim;
2648 isl_map *map;
2649 int empty;
2651 dim = isl_space_set_alloc(ctx, 0, 0);
2652 map = isl_map_lex_le(dim);
2653 empty = isl_map_is_empty(map);
2654 isl_map_free(map);
2656 if (empty < 0)
2657 return -1;
2658 if (empty)
2659 isl_die(ctx, isl_error_unknown,
2660 "expecting non-empty result", return -1);
2662 return 0;
2665 /* Inputs for isl_map_lexmin tests.
2666 * "map" is the input and "lexmin" is the expected result.
2668 struct {
2669 const char *map;
2670 const char *lexmin;
2671 } lexmin_tests [] = {
2672 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2673 "{ [x] -> [5] : 6 <= x <= 8; "
2674 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2675 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2676 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2677 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2678 "{ [x] -> [y] : (4y = x and x >= 0) or "
2679 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2680 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2681 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2682 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2683 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2684 /* Check that empty pieces are properly combined. */
2685 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2686 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2687 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2688 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2689 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2690 "x >= 4 }" },
2691 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2692 "a <= 255 and c <= 255 and d <= 255 - j and "
2693 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2694 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2695 "247d <= 247 + i and 248 - b <= 248d <= c and "
2696 "254d >= i - a + b and 254d >= -a + b and "
2697 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2698 "{ [i, k, j] -> "
2699 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2700 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2701 "247j >= 62738 - i and 509j <= 129795 + i and "
2702 "742j >= 188724 - i; "
2703 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2704 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2705 "16*floor((8 + b)/16) <= 7 + b; "
2706 "[a] -> [1] }",
2707 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2708 "[a] -> [b = 0] : 0 < a <= 509 }" },
2709 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2710 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2713 static int test_lexmin(struct isl_ctx *ctx)
2715 int i;
2716 int equal;
2717 const char *str;
2718 isl_basic_map *bmap;
2719 isl_map *map, *map2;
2720 isl_set *set;
2721 isl_set *set2;
2722 isl_pw_multi_aff *pma;
2724 str = "[p0, p1] -> { [] -> [] : "
2725 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2726 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2727 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2728 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2729 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2730 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2731 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2732 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2733 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2734 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2735 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2736 map = isl_map_read_from_str(ctx, str);
2737 map = isl_map_lexmin(map);
2738 isl_map_free(map);
2740 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2741 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2742 set = isl_set_read_from_str(ctx, str);
2743 set = isl_set_lexmax(set);
2744 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2745 set2 = isl_set_read_from_str(ctx, str);
2746 set = isl_set_intersect(set, set2);
2747 assert(!isl_set_is_empty(set));
2748 isl_set_free(set);
2750 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2751 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2752 map = isl_map_lexmin(map);
2753 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2754 equal = isl_map_is_equal(map, map2);
2755 isl_map_free(map);
2756 isl_map_free(map2);
2758 if (equal < 0)
2759 return -1;
2760 if (!equal)
2761 isl_die(ctx, isl_error_unknown,
2762 "unexpected result", return -1);
2765 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2766 " 8i' <= i and 8i' >= -7 + i }";
2767 bmap = isl_basic_map_read_from_str(ctx, str);
2768 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2769 map2 = isl_map_from_pw_multi_aff(pma);
2770 map = isl_map_from_basic_map(bmap);
2771 assert(isl_map_is_equal(map, map2));
2772 isl_map_free(map);
2773 isl_map_free(map2);
2775 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2776 " 8i' <= i and 8i' >= -7 + i }";
2777 set = isl_set_read_from_str(ctx, str);
2778 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2779 set2 = isl_set_from_pw_multi_aff(pma);
2780 equal = isl_set_is_equal(set, set2);
2781 isl_set_free(set);
2782 isl_set_free(set2);
2783 if (equal < 0)
2784 return -1;
2785 if (!equal)
2786 isl_die(ctx, isl_error_unknown,
2787 "unexpected difference between set and "
2788 "piecewise affine expression", return -1);
2790 return 0;
2793 /* A specialized isl_set_min_val test case that would return the wrong result
2794 * in earlier versions of isl.
2795 * The explicit call to isl_basic_set_union prevents the second basic set
2796 * from being determined to be empty prior to the call to isl_set_min_val,
2797 * at least at the point where this test case was introduced.
2799 static int test_min_special(isl_ctx *ctx)
2801 const char *str;
2802 isl_basic_set *bset1, *bset2;
2803 isl_set *set;
2804 isl_aff *obj;
2805 isl_val *res;
2806 int ok;
2808 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2809 bset1 = isl_basic_set_read_from_str(ctx, str);
2810 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2811 bset2 = isl_basic_set_read_from_str(ctx, str);
2812 set = isl_basic_set_union(bset1, bset2);
2813 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2815 res = isl_set_min_val(set, obj);
2816 ok = isl_val_cmp_si(res, 5) == 0;
2818 isl_aff_free(obj);
2819 isl_set_free(set);
2820 isl_val_free(res);
2822 if (!res)
2823 return -1;
2824 if (!ok)
2825 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2826 return -1);
2828 return 0;
2831 /* A specialized isl_set_min_val test case that would return an error
2832 * in earlier versions of isl.
2834 static int test_min_special2(isl_ctx *ctx)
2836 const char *str;
2837 isl_basic_set *bset;
2838 isl_aff *obj;
2839 isl_val *res;
2841 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
2842 bset = isl_basic_set_read_from_str(ctx, str);
2844 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
2846 res = isl_basic_set_max_val(bset, obj);
2848 isl_basic_set_free(bset);
2849 isl_aff_free(obj);
2850 isl_val_free(res);
2852 if (!res)
2853 return -1;
2855 return 0;
2858 struct {
2859 const char *set;
2860 const char *obj;
2861 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2862 __isl_keep isl_aff *obj);
2863 const char *res;
2864 } opt_tests[] = {
2865 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2866 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2867 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2868 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2869 &isl_set_max_val, "30" },
2873 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2874 * In particular, check the results on non-convex inputs.
2876 static int test_min(struct isl_ctx *ctx)
2878 int i;
2879 isl_set *set;
2880 isl_aff *obj;
2881 isl_val *val, *res;
2882 isl_bool ok;
2884 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2885 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2886 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2887 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2888 val = opt_tests[i].fn(set, obj);
2889 ok = isl_val_eq(res, val);
2890 isl_val_free(res);
2891 isl_val_free(val);
2892 isl_aff_free(obj);
2893 isl_set_free(set);
2895 if (ok < 0)
2896 return -1;
2897 if (!ok)
2898 isl_die(ctx, isl_error_unknown,
2899 "unexpected optimum", return -1);
2902 if (test_min_special(ctx) < 0)
2903 return -1;
2904 if (test_min_special2(ctx) < 0)
2905 return -1;
2907 return 0;
2910 struct must_may {
2911 isl_map *must;
2912 isl_map *may;
2915 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2916 void *dep_user, void *user)
2918 struct must_may *mm = (struct must_may *)user;
2920 if (must)
2921 mm->must = isl_map_union(mm->must, dep);
2922 else
2923 mm->may = isl_map_union(mm->may, dep);
2925 return isl_stat_ok;
2928 static int common_space(void *first, void *second)
2930 int depth = *(int *)first;
2931 return 2 * depth;
2934 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2936 isl_map *map2;
2937 int equal;
2939 if (!map)
2940 return -1;
2942 map2 = isl_map_read_from_str(map->ctx, str);
2943 equal = isl_map_is_equal(map, map2);
2944 isl_map_free(map2);
2946 return equal;
2949 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2951 int equal;
2953 equal = map_is_equal(map, str);
2954 if (equal < 0)
2955 return -1;
2956 if (!equal)
2957 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2958 "result not as expected", return -1);
2959 return 0;
2962 static int test_dep(struct isl_ctx *ctx)
2964 const char *str;
2965 isl_space *dim;
2966 isl_map *map;
2967 isl_access_info *ai;
2968 isl_flow *flow;
2969 int depth;
2970 struct must_may mm;
2972 depth = 3;
2974 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2975 map = isl_map_read_from_str(ctx, str);
2976 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2978 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2979 map = isl_map_read_from_str(ctx, str);
2980 ai = isl_access_info_add_source(ai, map, 1, &depth);
2982 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2983 map = isl_map_read_from_str(ctx, str);
2984 ai = isl_access_info_add_source(ai, map, 1, &depth);
2986 flow = isl_access_info_compute_flow(ai);
2987 dim = isl_space_alloc(ctx, 0, 3, 3);
2988 mm.must = isl_map_empty(isl_space_copy(dim));
2989 mm.may = isl_map_empty(dim);
2991 isl_flow_foreach(flow, collect_must_may, &mm);
2993 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2994 " [1,10,0] -> [2,5,0] }";
2995 assert(map_is_equal(mm.must, str));
2996 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2997 assert(map_is_equal(mm.may, str));
2999 isl_map_free(mm.must);
3000 isl_map_free(mm.may);
3001 isl_flow_free(flow);
3004 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3005 map = isl_map_read_from_str(ctx, str);
3006 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3008 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3009 map = isl_map_read_from_str(ctx, str);
3010 ai = isl_access_info_add_source(ai, map, 1, &depth);
3012 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3013 map = isl_map_read_from_str(ctx, str);
3014 ai = isl_access_info_add_source(ai, map, 0, &depth);
3016 flow = isl_access_info_compute_flow(ai);
3017 dim = isl_space_alloc(ctx, 0, 3, 3);
3018 mm.must = isl_map_empty(isl_space_copy(dim));
3019 mm.may = isl_map_empty(dim);
3021 isl_flow_foreach(flow, collect_must_may, &mm);
3023 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
3024 assert(map_is_equal(mm.must, str));
3025 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3026 assert(map_is_equal(mm.may, str));
3028 isl_map_free(mm.must);
3029 isl_map_free(mm.may);
3030 isl_flow_free(flow);
3033 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3034 map = isl_map_read_from_str(ctx, str);
3035 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3037 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3038 map = isl_map_read_from_str(ctx, str);
3039 ai = isl_access_info_add_source(ai, map, 0, &depth);
3041 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3042 map = isl_map_read_from_str(ctx, str);
3043 ai = isl_access_info_add_source(ai, map, 0, &depth);
3045 flow = isl_access_info_compute_flow(ai);
3046 dim = isl_space_alloc(ctx, 0, 3, 3);
3047 mm.must = isl_map_empty(isl_space_copy(dim));
3048 mm.may = isl_map_empty(dim);
3050 isl_flow_foreach(flow, collect_must_may, &mm);
3052 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
3053 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3054 assert(map_is_equal(mm.may, str));
3055 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3056 assert(map_is_equal(mm.must, str));
3058 isl_map_free(mm.must);
3059 isl_map_free(mm.may);
3060 isl_flow_free(flow);
3063 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
3064 map = isl_map_read_from_str(ctx, str);
3065 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3067 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3068 map = isl_map_read_from_str(ctx, str);
3069 ai = isl_access_info_add_source(ai, map, 0, &depth);
3071 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
3072 map = isl_map_read_from_str(ctx, str);
3073 ai = isl_access_info_add_source(ai, map, 0, &depth);
3075 flow = isl_access_info_compute_flow(ai);
3076 dim = isl_space_alloc(ctx, 0, 3, 3);
3077 mm.must = isl_map_empty(isl_space_copy(dim));
3078 mm.may = isl_map_empty(dim);
3080 isl_flow_foreach(flow, collect_must_may, &mm);
3082 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
3083 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
3084 assert(map_is_equal(mm.may, str));
3085 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3086 assert(map_is_equal(mm.must, str));
3088 isl_map_free(mm.must);
3089 isl_map_free(mm.may);
3090 isl_flow_free(flow);
3093 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
3094 map = isl_map_read_from_str(ctx, str);
3095 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3097 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3098 map = isl_map_read_from_str(ctx, str);
3099 ai = isl_access_info_add_source(ai, map, 0, &depth);
3101 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
3102 map = isl_map_read_from_str(ctx, str);
3103 ai = isl_access_info_add_source(ai, map, 0, &depth);
3105 flow = isl_access_info_compute_flow(ai);
3106 dim = isl_space_alloc(ctx, 0, 3, 3);
3107 mm.must = isl_map_empty(isl_space_copy(dim));
3108 mm.may = isl_map_empty(dim);
3110 isl_flow_foreach(flow, collect_must_may, &mm);
3112 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
3113 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
3114 assert(map_is_equal(mm.may, str));
3115 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3116 assert(map_is_equal(mm.must, str));
3118 isl_map_free(mm.must);
3119 isl_map_free(mm.may);
3120 isl_flow_free(flow);
3123 depth = 5;
3125 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3126 map = isl_map_read_from_str(ctx, str);
3127 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
3129 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3130 map = isl_map_read_from_str(ctx, str);
3131 ai = isl_access_info_add_source(ai, map, 1, &depth);
3133 flow = isl_access_info_compute_flow(ai);
3134 dim = isl_space_alloc(ctx, 0, 5, 5);
3135 mm.must = isl_map_empty(isl_space_copy(dim));
3136 mm.may = isl_map_empty(dim);
3138 isl_flow_foreach(flow, collect_must_may, &mm);
3140 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
3141 assert(map_is_equal(mm.must, str));
3142 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
3143 assert(map_is_equal(mm.may, str));
3145 isl_map_free(mm.must);
3146 isl_map_free(mm.may);
3147 isl_flow_free(flow);
3149 return 0;
3152 /* Check that the dependence analysis proceeds without errors.
3153 * Earlier versions of isl would break down during the analysis
3154 * due to the use of the wrong spaces.
3156 static int test_flow(isl_ctx *ctx)
3158 const char *str;
3159 isl_union_map *access, *schedule;
3160 isl_union_map *must_dep, *may_dep;
3161 int r;
3163 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
3164 access = isl_union_map_read_from_str(ctx, str);
3165 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
3166 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
3167 "S2[] -> [1,0,0,0]; "
3168 "S3[] -> [-1,0,0,0] }";
3169 schedule = isl_union_map_read_from_str(ctx, str);
3170 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
3171 isl_union_map_copy(access), schedule,
3172 &must_dep, &may_dep, NULL, NULL);
3173 isl_union_map_free(may_dep);
3174 isl_union_map_free(must_dep);
3176 return r;
3179 struct {
3180 const char *map;
3181 int sv;
3182 } sv_tests[] = {
3183 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
3184 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
3185 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
3186 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
3187 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
3188 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
3189 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3190 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3191 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
3194 int test_sv(isl_ctx *ctx)
3196 isl_union_map *umap;
3197 int i;
3198 int sv;
3200 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
3201 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
3202 sv = isl_union_map_is_single_valued(umap);
3203 isl_union_map_free(umap);
3204 if (sv < 0)
3205 return -1;
3206 if (sv_tests[i].sv && !sv)
3207 isl_die(ctx, isl_error_internal,
3208 "map not detected as single valued", return -1);
3209 if (!sv_tests[i].sv && sv)
3210 isl_die(ctx, isl_error_internal,
3211 "map detected as single valued", return -1);
3214 return 0;
3217 struct {
3218 const char *str;
3219 int bijective;
3220 } bijective_tests[] = {
3221 { "[N,M]->{[i,j] -> [i]}", 0 },
3222 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
3223 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
3224 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
3225 { "[N,M]->{[i,j] -> [j,i]}", 1 },
3226 { "[N,M]->{[i,j] -> [i+j]}", 0 },
3227 { "[N,M]->{[i,j] -> []}", 0 },
3228 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
3229 { "[N,M]->{[i,j] -> [2i]}", 0 },
3230 { "[N,M]->{[i,j] -> [i,i]}", 0 },
3231 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
3232 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
3233 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
3236 static int test_bijective(struct isl_ctx *ctx)
3238 isl_map *map;
3239 int i;
3240 int bijective;
3242 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
3243 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
3244 bijective = isl_map_is_bijective(map);
3245 isl_map_free(map);
3246 if (bijective < 0)
3247 return -1;
3248 if (bijective_tests[i].bijective && !bijective)
3249 isl_die(ctx, isl_error_internal,
3250 "map not detected as bijective", return -1);
3251 if (!bijective_tests[i].bijective && bijective)
3252 isl_die(ctx, isl_error_internal,
3253 "map detected as bijective", return -1);
3256 return 0;
3259 /* Inputs for isl_pw_qpolynomial_gist tests.
3260 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
3262 struct {
3263 const char *pwqp;
3264 const char *set;
3265 const char *gist;
3266 } pwqp_gist_tests[] = {
3267 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
3268 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
3269 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
3270 "{ [i] -> -1/2 + 1/2 * i }" },
3271 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
3274 static int test_pwqp(struct isl_ctx *ctx)
3276 int i;
3277 const char *str;
3278 isl_set *set;
3279 isl_pw_qpolynomial *pwqp1, *pwqp2;
3280 int equal;
3282 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3283 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3285 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
3286 isl_dim_in, 1, 1);
3288 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3289 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3291 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3293 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3295 isl_pw_qpolynomial_free(pwqp1);
3297 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
3298 str = pwqp_gist_tests[i].pwqp;
3299 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3300 str = pwqp_gist_tests[i].set;
3301 set = isl_set_read_from_str(ctx, str);
3302 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
3303 str = pwqp_gist_tests[i].gist;
3304 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3305 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3306 equal = isl_pw_qpolynomial_is_zero(pwqp1);
3307 isl_pw_qpolynomial_free(pwqp1);
3309 if (equal < 0)
3310 return -1;
3311 if (!equal)
3312 isl_die(ctx, isl_error_unknown,
3313 "unexpected result", return -1);
3316 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
3317 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3318 str = "{ [i] -> ([(2 * [i/2])/5]) }";
3319 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3321 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3323 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3325 isl_pw_qpolynomial_free(pwqp1);
3327 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3328 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3329 str = "{ [x] -> x }";
3330 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3332 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3334 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3336 isl_pw_qpolynomial_free(pwqp1);
3338 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3339 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3340 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3341 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3342 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3343 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3344 isl_pw_qpolynomial_free(pwqp1);
3346 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3347 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3348 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3349 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3350 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3351 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3352 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3353 isl_pw_qpolynomial_free(pwqp1);
3354 isl_pw_qpolynomial_free(pwqp2);
3355 if (equal < 0)
3356 return -1;
3357 if (!equal)
3358 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3360 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3361 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3362 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3363 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3364 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3365 isl_val_one(ctx));
3366 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3367 isl_pw_qpolynomial_free(pwqp1);
3368 isl_pw_qpolynomial_free(pwqp2);
3369 if (equal < 0)
3370 return -1;
3371 if (!equal)
3372 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3374 return 0;
3377 static int test_split_periods(isl_ctx *ctx)
3379 const char *str;
3380 isl_pw_qpolynomial *pwqp;
3382 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3383 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3384 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3385 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3387 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3389 isl_pw_qpolynomial_free(pwqp);
3391 if (!pwqp)
3392 return -1;
3394 return 0;
3397 static int test_union(isl_ctx *ctx)
3399 const char *str;
3400 isl_union_set *uset1, *uset2;
3401 isl_union_map *umap1, *umap2;
3402 int equal;
3404 str = "{ [i] : 0 <= i <= 1 }";
3405 uset1 = isl_union_set_read_from_str(ctx, str);
3406 str = "{ [1] -> [0] }";
3407 umap1 = isl_union_map_read_from_str(ctx, str);
3409 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3410 equal = isl_union_map_is_equal(umap1, umap2);
3412 isl_union_map_free(umap1);
3413 isl_union_map_free(umap2);
3415 if (equal < 0)
3416 return -1;
3417 if (!equal)
3418 isl_die(ctx, isl_error_unknown, "union maps not equal",
3419 return -1);
3421 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3422 umap1 = isl_union_map_read_from_str(ctx, str);
3423 str = "{ A[i]; B[i] }";
3424 uset1 = isl_union_set_read_from_str(ctx, str);
3426 uset2 = isl_union_map_domain(umap1);
3428 equal = isl_union_set_is_equal(uset1, uset2);
3430 isl_union_set_free(uset1);
3431 isl_union_set_free(uset2);
3433 if (equal < 0)
3434 return -1;
3435 if (!equal)
3436 isl_die(ctx, isl_error_unknown, "union sets not equal",
3437 return -1);
3439 return 0;
3442 /* Check that computing a bound of a non-zero polynomial over an unbounded
3443 * domain does not produce a rational value.
3444 * In particular, check that the upper bound is infinity.
3446 static int test_bound_unbounded_domain(isl_ctx *ctx)
3448 const char *str;
3449 isl_pw_qpolynomial *pwqp;
3450 isl_pw_qpolynomial_fold *pwf, *pwf2;
3451 isl_bool equal;
3453 str = "{ [m,n] -> -m * n }";
3454 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3455 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3456 str = "{ infty }";
3457 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3458 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3459 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
3460 isl_pw_qpolynomial_fold_free(pwf);
3461 isl_pw_qpolynomial_fold_free(pwf2);
3463 if (equal < 0)
3464 return -1;
3465 if (!equal)
3466 isl_die(ctx, isl_error_unknown,
3467 "expecting infinite polynomial bound", return -1);
3469 return 0;
3472 static int test_bound(isl_ctx *ctx)
3474 const char *str;
3475 unsigned dim;
3476 isl_pw_qpolynomial *pwqp;
3477 isl_pw_qpolynomial_fold *pwf;
3479 if (test_bound_unbounded_domain(ctx) < 0)
3480 return -1;
3482 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3483 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3484 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3485 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3486 isl_pw_qpolynomial_fold_free(pwf);
3487 if (dim != 4)
3488 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3489 return -1);
3491 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3492 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3493 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3494 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3495 isl_pw_qpolynomial_fold_free(pwf);
3496 if (dim != 1)
3497 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3498 return -1);
3500 return 0;
3503 /* isl_set is defined to isl_map internally, so the corresponding elements
3504 * are isl_basic_map objects.
3506 #undef EL_BASE
3507 #undef SET_BASE
3508 #define EL_BASE basic_map
3509 #define SET_BASE set
3510 #include "isl_test_list_templ.c"
3512 #undef EL_BASE
3513 #undef SET_BASE
3514 #define EL_BASE basic_set
3515 #define SET_BASE union_set
3516 #include "isl_test_list_templ.c"
3518 #undef EL_BASE
3519 #undef SET_BASE
3520 #define EL_BASE set
3521 #define SET_BASE union_set
3522 #include "isl_test_list_templ.c"
3524 #undef EL_BASE
3525 #undef SET_BASE
3526 #define EL_BASE basic_map
3527 #define SET_BASE map
3528 #include "isl_test_list_templ.c"
3530 #undef EL_BASE
3531 #undef SET_BASE
3532 #define EL_BASE map
3533 #define SET_BASE union_map
3534 #include "isl_test_list_templ.c"
3536 /* Check that the conversion from isl objects to lists works as expected.
3538 static int test_get_list(isl_ctx *ctx)
3540 if (test_get_list_basic_map_from_set(ctx, "{ [0]; [2]; [3] }"))
3541 return -1;
3542 if (test_get_list_basic_set_from_union_set(ctx, "{ A[0]; B[2]; B[3] }"))
3543 return -1;
3544 if (test_get_list_set_from_union_set(ctx, "{ A[0]; A[2]; B[3] }"))
3545 return -1;
3546 if (test_get_list_basic_map_from_map(ctx,
3547 "{ [0] -> [0]; [2] -> [0]; [3] -> [0] }"))
3548 return -1;
3549 if (test_get_list_map_from_union_map(ctx,
3550 "{ A[0] -> [0]; A[2] -> [0]; B[3] -> [0] }"))
3551 return -1;
3553 return 0;
3556 static int test_lift(isl_ctx *ctx)
3558 const char *str;
3559 isl_basic_map *bmap;
3560 isl_basic_set *bset;
3562 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3563 bset = isl_basic_set_read_from_str(ctx, str);
3564 bset = isl_basic_set_lift(bset);
3565 bmap = isl_basic_map_from_range(bset);
3566 bset = isl_basic_map_domain(bmap);
3567 isl_basic_set_free(bset);
3569 return 0;
3572 /* Check that isl_set_is_subset is not confused by identical
3573 * integer divisions.
3574 * The call to isl_set_normalize ensures that the equality constraints
3575 * a = b = 0 are discovered, turning e0 and e1 into identical
3576 * integer divisions. Any further simplification would remove
3577 * the duplicate integer divisions.
3579 static isl_stat test_subset_duplicate_integer_divisions(isl_ctx *ctx)
3581 const char *str;
3582 isl_bool is_subset;
3583 isl_set *set1, *set2;
3585 str = "{ [a, b, c, d] : "
3586 "exists (e0 = floor((a + d)/4), e1 = floor((d)/4), "
3587 "e2 = floor((-a - d + 4 *floor((a + d)/4))/10), "
3588 "e3 = floor((-d + 4*floor((d)/4))/10): "
3589 "10e2 = -a - 2c - d + 4e0 and 10e3 = -2c - d + 4e1 and "
3590 "b >= 0 and a <= 0 and b <= a) }";
3591 set1 = isl_set_read_from_str(ctx, str);
3592 set2 = isl_set_read_from_str(ctx, str);
3593 set2 = isl_set_normalize(set2);
3595 is_subset = isl_set_is_subset(set1, set2);
3597 isl_set_free(set1);
3598 isl_set_free(set2);
3600 if (is_subset < 0)
3601 return isl_stat_error;
3602 if (!is_subset)
3603 isl_die(ctx, isl_error_unknown,
3604 "set is not considered to be a subset of itself",
3605 return isl_stat_error);
3607 return isl_stat_ok;
3610 struct {
3611 const char *set1;
3612 const char *set2;
3613 int subset;
3614 } subset_tests[] = {
3615 { "{ [112, 0] }",
3616 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3617 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3618 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3619 { "{ [65] }",
3620 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3621 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3622 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3623 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3624 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3625 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3626 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3627 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3628 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3629 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3630 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3631 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3632 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3633 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3634 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3635 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3636 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3637 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3638 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3639 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3640 "4e0 <= -57 + i0 + i1)) or "
3641 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3642 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3643 "4e0 >= -61 + i0 + i1)) or "
3644 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3645 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3648 static int test_subset(isl_ctx *ctx)
3650 int i;
3651 isl_set *set1, *set2;
3652 int subset;
3654 if (test_subset_duplicate_integer_divisions(ctx) < 0)
3655 return -1;
3657 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3658 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3659 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3660 subset = isl_set_is_subset(set1, set2);
3661 isl_set_free(set1);
3662 isl_set_free(set2);
3663 if (subset < 0)
3664 return -1;
3665 if (subset != subset_tests[i].subset)
3666 isl_die(ctx, isl_error_unknown,
3667 "incorrect subset result", return -1);
3670 return 0;
3673 struct {
3674 const char *minuend;
3675 const char *subtrahend;
3676 const char *difference;
3677 } subtract_domain_tests[] = {
3678 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3679 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3680 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3683 static int test_subtract(isl_ctx *ctx)
3685 int i;
3686 isl_union_map *umap1, *umap2;
3687 isl_union_pw_multi_aff *upma1, *upma2;
3688 isl_union_set *uset;
3689 int equal;
3691 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3692 umap1 = isl_union_map_read_from_str(ctx,
3693 subtract_domain_tests[i].minuend);
3694 uset = isl_union_set_read_from_str(ctx,
3695 subtract_domain_tests[i].subtrahend);
3696 umap2 = isl_union_map_read_from_str(ctx,
3697 subtract_domain_tests[i].difference);
3698 umap1 = isl_union_map_subtract_domain(umap1, uset);
3699 equal = isl_union_map_is_equal(umap1, umap2);
3700 isl_union_map_free(umap1);
3701 isl_union_map_free(umap2);
3702 if (equal < 0)
3703 return -1;
3704 if (!equal)
3705 isl_die(ctx, isl_error_unknown,
3706 "incorrect subtract domain result", return -1);
3709 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3710 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3711 subtract_domain_tests[i].minuend);
3712 uset = isl_union_set_read_from_str(ctx,
3713 subtract_domain_tests[i].subtrahend);
3714 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3715 subtract_domain_tests[i].difference);
3716 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3717 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3718 isl_union_pw_multi_aff_free(upma1);
3719 isl_union_pw_multi_aff_free(upma2);
3720 if (equal < 0)
3721 return -1;
3722 if (!equal)
3723 isl_die(ctx, isl_error_unknown,
3724 "incorrect subtract domain result", return -1);
3727 return 0;
3730 /* Check that intersecting the empty basic set with another basic set
3731 * does not increase the number of constraints. In particular,
3732 * the empty basic set should maintain its canonical representation.
3734 static int test_intersect_1(isl_ctx *ctx)
3736 int n1, n2;
3737 isl_basic_set *bset1, *bset2;
3739 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3740 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3741 n1 = isl_basic_set_n_constraint(bset1);
3742 bset1 = isl_basic_set_intersect(bset1, bset2);
3743 n2 = isl_basic_set_n_constraint(bset1);
3744 isl_basic_set_free(bset1);
3745 if (!bset1)
3746 return -1;
3747 if (n1 != n2)
3748 isl_die(ctx, isl_error_unknown,
3749 "number of constraints of empty set changed",
3750 return -1);
3752 return 0;
3755 /* Check that intersecting a set with itself does not cause
3756 * an explosion in the number of disjuncts.
3758 static isl_stat test_intersect_2(isl_ctx *ctx)
3760 int i;
3761 isl_set *set;
3763 set = isl_set_read_from_str(ctx, "{ [x,y] : x >= 0 or y >= 0 }");
3764 for (i = 0; i < 100; ++i)
3765 set = isl_set_intersect(set, isl_set_copy(set));
3766 isl_set_free(set);
3767 if (!set)
3768 return isl_stat_error;
3769 return isl_stat_ok;
3772 /* Perform some intersection tests.
3774 static int test_intersect(isl_ctx *ctx)
3776 if (test_intersect_1(ctx) < 0)
3777 return -1;
3778 if (test_intersect_2(ctx) < 0)
3779 return -1;
3781 return 0;
3784 int test_factorize(isl_ctx *ctx)
3786 const char *str;
3787 isl_basic_set *bset;
3788 isl_factorizer *f;
3790 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3791 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3792 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3793 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3794 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3795 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3796 "3i5 >= -2i0 - i2 + 3i4 }";
3797 bset = isl_basic_set_read_from_str(ctx, str);
3798 f = isl_basic_set_factorizer(bset);
3799 isl_basic_set_free(bset);
3800 isl_factorizer_free(f);
3801 if (!f)
3802 isl_die(ctx, isl_error_unknown,
3803 "failed to construct factorizer", return -1);
3805 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3806 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3807 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3808 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3809 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3810 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3811 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3812 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3813 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3814 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3815 bset = isl_basic_set_read_from_str(ctx, str);
3816 f = isl_basic_set_factorizer(bset);
3817 isl_basic_set_free(bset);
3818 isl_factorizer_free(f);
3819 if (!f)
3820 isl_die(ctx, isl_error_unknown,
3821 "failed to construct factorizer", return -1);
3823 return 0;
3826 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3828 int *injective = user;
3830 *injective = isl_map_is_injective(map);
3831 isl_map_free(map);
3833 if (*injective < 0 || !*injective)
3834 return isl_stat_error;
3836 return isl_stat_ok;
3839 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3840 const char *r, const char *s, int tilable, int parallel)
3842 int i;
3843 isl_union_set *D;
3844 isl_union_map *W, *R, *S;
3845 isl_union_map *empty;
3846 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3847 isl_union_map *validity, *proximity, *coincidence;
3848 isl_union_map *schedule;
3849 isl_union_map *test;
3850 isl_union_set *delta;
3851 isl_union_set *domain;
3852 isl_set *delta_set;
3853 isl_set *slice;
3854 isl_set *origin;
3855 isl_schedule_constraints *sc;
3856 isl_schedule *sched;
3857 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3859 D = isl_union_set_read_from_str(ctx, d);
3860 W = isl_union_map_read_from_str(ctx, w);
3861 R = isl_union_map_read_from_str(ctx, r);
3862 S = isl_union_map_read_from_str(ctx, s);
3864 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3865 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3867 empty = isl_union_map_empty(isl_union_map_get_space(S));
3868 isl_union_map_compute_flow(isl_union_map_copy(R),
3869 isl_union_map_copy(W), empty,
3870 isl_union_map_copy(S),
3871 &dep_raw, NULL, NULL, NULL);
3872 isl_union_map_compute_flow(isl_union_map_copy(W),
3873 isl_union_map_copy(W),
3874 isl_union_map_copy(R),
3875 isl_union_map_copy(S),
3876 &dep_waw, &dep_war, NULL, NULL);
3878 dep = isl_union_map_union(dep_waw, dep_war);
3879 dep = isl_union_map_union(dep, dep_raw);
3880 validity = isl_union_map_copy(dep);
3881 coincidence = isl_union_map_copy(dep);
3882 proximity = isl_union_map_copy(dep);
3884 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3885 sc = isl_schedule_constraints_set_validity(sc, validity);
3886 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3887 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3888 sched = isl_schedule_constraints_compute_schedule(sc);
3889 schedule = isl_schedule_get_map(sched);
3890 isl_schedule_free(sched);
3891 isl_union_map_free(W);
3892 isl_union_map_free(R);
3893 isl_union_map_free(S);
3895 is_injection = 1;
3896 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3898 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3899 is_complete = isl_union_set_is_subset(D, domain);
3900 isl_union_set_free(D);
3901 isl_union_set_free(domain);
3903 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3904 test = isl_union_map_apply_range(test, dep);
3905 test = isl_union_map_apply_range(test, schedule);
3907 delta = isl_union_map_deltas(test);
3908 if (isl_union_set_n_set(delta) == 0) {
3909 is_tilable = 1;
3910 is_parallel = 1;
3911 is_nonneg = 1;
3912 isl_union_set_free(delta);
3913 } else {
3914 delta_set = isl_set_from_union_set(delta);
3916 slice = isl_set_universe(isl_set_get_space(delta_set));
3917 for (i = 0; i < tilable; ++i)
3918 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3919 is_tilable = isl_set_is_subset(delta_set, slice);
3920 isl_set_free(slice);
3922 slice = isl_set_universe(isl_set_get_space(delta_set));
3923 for (i = 0; i < parallel; ++i)
3924 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3925 is_parallel = isl_set_is_subset(delta_set, slice);
3926 isl_set_free(slice);
3928 origin = isl_set_universe(isl_set_get_space(delta_set));
3929 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3930 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3932 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3933 delta_set = isl_set_lexmin(delta_set);
3935 is_nonneg = isl_set_is_equal(delta_set, origin);
3937 isl_set_free(origin);
3938 isl_set_free(delta_set);
3941 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3942 is_injection < 0 || is_complete < 0)
3943 return -1;
3944 if (!is_complete)
3945 isl_die(ctx, isl_error_unknown,
3946 "generated schedule incomplete", return -1);
3947 if (!is_injection)
3948 isl_die(ctx, isl_error_unknown,
3949 "generated schedule not injective on each statement",
3950 return -1);
3951 if (!is_nonneg)
3952 isl_die(ctx, isl_error_unknown,
3953 "negative dependences in generated schedule",
3954 return -1);
3955 if (!is_tilable)
3956 isl_die(ctx, isl_error_unknown,
3957 "generated schedule not as tilable as expected",
3958 return -1);
3959 if (!is_parallel)
3960 isl_die(ctx, isl_error_unknown,
3961 "generated schedule not as parallel as expected",
3962 return -1);
3964 return 0;
3967 /* Compute a schedule for the given instance set, validity constraints,
3968 * proximity constraints and context and return a corresponding union map
3969 * representation.
3971 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3972 const char *domain, const char *validity, const char *proximity,
3973 const char *context)
3975 isl_set *con;
3976 isl_union_set *dom;
3977 isl_union_map *dep;
3978 isl_union_map *prox;
3979 isl_schedule_constraints *sc;
3980 isl_schedule *schedule;
3981 isl_union_map *sched;
3983 con = isl_set_read_from_str(ctx, context);
3984 dom = isl_union_set_read_from_str(ctx, domain);
3985 dep = isl_union_map_read_from_str(ctx, validity);
3986 prox = isl_union_map_read_from_str(ctx, proximity);
3987 sc = isl_schedule_constraints_on_domain(dom);
3988 sc = isl_schedule_constraints_set_context(sc, con);
3989 sc = isl_schedule_constraints_set_validity(sc, dep);
3990 sc = isl_schedule_constraints_set_proximity(sc, prox);
3991 schedule = isl_schedule_constraints_compute_schedule(sc);
3992 sched = isl_schedule_get_map(schedule);
3993 isl_schedule_free(schedule);
3995 return sched;
3998 /* Compute a schedule for the given instance set, validity constraints and
3999 * proximity constraints and return a corresponding union map representation.
4001 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
4002 const char *domain, const char *validity, const char *proximity)
4004 return compute_schedule_with_context(ctx, domain, validity, proximity,
4005 "{ : }");
4008 /* Check that a schedule can be constructed on the given domain
4009 * with the given validity and proximity constraints.
4011 static int test_has_schedule(isl_ctx *ctx, const char *domain,
4012 const char *validity, const char *proximity)
4014 isl_union_map *sched;
4016 sched = compute_schedule(ctx, domain, validity, proximity);
4017 if (!sched)
4018 return -1;
4020 isl_union_map_free(sched);
4021 return 0;
4024 int test_special_schedule(isl_ctx *ctx, const char *domain,
4025 const char *validity, const char *proximity, const char *expected_sched)
4027 isl_union_map *sched1, *sched2;
4028 int equal;
4030 sched1 = compute_schedule(ctx, domain, validity, proximity);
4031 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
4033 equal = isl_union_map_is_equal(sched1, sched2);
4034 isl_union_map_free(sched1);
4035 isl_union_map_free(sched2);
4037 if (equal < 0)
4038 return -1;
4039 if (!equal)
4040 isl_die(ctx, isl_error_unknown, "unexpected schedule",
4041 return -1);
4043 return 0;
4046 /* Check that the schedule map is properly padded, i.e., that the range
4047 * lives in a single space.
4049 static int test_padded_schedule(isl_ctx *ctx)
4051 const char *str;
4052 isl_union_set *D;
4053 isl_union_map *validity, *proximity;
4054 isl_schedule_constraints *sc;
4055 isl_schedule *sched;
4056 isl_union_map *umap;
4057 isl_union_set *range;
4058 isl_set *set;
4060 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
4061 D = isl_union_set_read_from_str(ctx, str);
4062 validity = isl_union_map_empty(isl_union_set_get_space(D));
4063 proximity = isl_union_map_copy(validity);
4064 sc = isl_schedule_constraints_on_domain(D);
4065 sc = isl_schedule_constraints_set_validity(sc, validity);
4066 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4067 sched = isl_schedule_constraints_compute_schedule(sc);
4068 umap = isl_schedule_get_map(sched);
4069 isl_schedule_free(sched);
4070 range = isl_union_map_range(umap);
4071 set = isl_set_from_union_set(range);
4072 isl_set_free(set);
4074 if (!set)
4075 return -1;
4077 return 0;
4080 /* Check that conditional validity constraints are also taken into
4081 * account across bands.
4082 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
4083 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
4084 * and then check that the adjacent order constraint C[2,1]->D[2,0]
4085 * is enforced by the rest of the schedule.
4087 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
4089 const char *str;
4090 isl_union_set *domain;
4091 isl_union_map *validity, *proximity, *condition;
4092 isl_union_map *sink, *source, *dep;
4093 isl_schedule_constraints *sc;
4094 isl_schedule *schedule;
4095 isl_union_access_info *access;
4096 isl_union_flow *flow;
4097 int empty;
4099 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4100 "A[k] : k >= 1 and k <= -1 + n; "
4101 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4102 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
4103 domain = isl_union_set_read_from_str(ctx, str);
4104 sc = isl_schedule_constraints_on_domain(domain);
4105 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
4106 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4107 "D[k, i] -> C[1 + k, i] : "
4108 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4109 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
4110 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
4111 validity = isl_union_map_read_from_str(ctx, str);
4112 sc = isl_schedule_constraints_set_validity(sc, validity);
4113 str = "[n] -> { C[k, i] -> D[k, i] : "
4114 "0 <= i <= -1 + k and k <= -1 + n }";
4115 proximity = isl_union_map_read_from_str(ctx, str);
4116 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4117 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
4118 "i <= -1 + k and i >= 1 and k <= -2 + n; "
4119 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
4120 "k <= -1 + n and i >= 0 and i <= -2 + k }";
4121 condition = isl_union_map_read_from_str(ctx, str);
4122 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
4123 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4124 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
4125 "i >= 0 and i <= -1 + k and k <= -1 + n and "
4126 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
4127 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
4128 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4129 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
4130 "k <= -1 + n and i >= 0 and i <= -1 + k and "
4131 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
4132 validity = isl_union_map_read_from_str(ctx, str);
4133 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4134 validity);
4135 schedule = isl_schedule_constraints_compute_schedule(sc);
4136 str = "{ D[2,0] -> [] }";
4137 sink = isl_union_map_read_from_str(ctx, str);
4138 access = isl_union_access_info_from_sink(sink);
4139 str = "{ C[2,1] -> [] }";
4140 source = isl_union_map_read_from_str(ctx, str);
4141 access = isl_union_access_info_set_must_source(access, source);
4142 access = isl_union_access_info_set_schedule(access, schedule);
4143 flow = isl_union_access_info_compute_flow(access);
4144 dep = isl_union_flow_get_must_dependence(flow);
4145 isl_union_flow_free(flow);
4146 empty = isl_union_map_is_empty(dep);
4147 isl_union_map_free(dep);
4149 if (empty < 0)
4150 return -1;
4151 if (empty)
4152 isl_die(ctx, isl_error_unknown,
4153 "conditional validity not respected", return -1);
4155 return 0;
4158 /* Check that the test for violated conditional validity constraints
4159 * is not confused by domain compression.
4160 * In particular, earlier versions of isl would apply
4161 * a schedule on the compressed domains to the original domains,
4162 * resulting in a failure to detect that the default schedule
4163 * violates the conditional validity constraints.
4165 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
4167 const char *str;
4168 isl_bool empty;
4169 isl_union_set *domain;
4170 isl_union_map *validity, *condition;
4171 isl_schedule_constraints *sc;
4172 isl_schedule *schedule;
4173 isl_union_map *umap;
4174 isl_map *map, *ge;
4176 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
4177 domain = isl_union_set_read_from_str(ctx, str);
4178 sc = isl_schedule_constraints_on_domain(domain);
4179 str = "{ B[1, i] -> A[0, i + 1] }";
4180 condition = isl_union_map_read_from_str(ctx, str);
4181 str = "{ A[0, i] -> B[1, i - 1] }";
4182 validity = isl_union_map_read_from_str(ctx, str);
4183 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4184 isl_union_map_copy(validity));
4185 schedule = isl_schedule_constraints_compute_schedule(sc);
4186 umap = isl_schedule_get_map(schedule);
4187 isl_schedule_free(schedule);
4188 validity = isl_union_map_apply_domain(validity,
4189 isl_union_map_copy(umap));
4190 validity = isl_union_map_apply_range(validity, umap);
4191 map = isl_map_from_union_map(validity);
4192 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4193 map = isl_map_intersect(map, ge);
4194 empty = isl_map_is_empty(map);
4195 isl_map_free(map);
4197 if (empty < 0)
4198 return -1;
4199 if (!empty)
4200 isl_die(ctx, isl_error_unknown,
4201 "conditional validity constraints not satisfied",
4202 return -1);
4204 return 0;
4207 /* Input for testing of schedule construction based on
4208 * conditional constraints.
4210 * domain is the iteration domain
4211 * flow are the flow dependences, which determine the validity and
4212 * proximity constraints
4213 * condition are the conditions on the conditional validity constraints
4214 * conditional_validity are the conditional validity constraints
4215 * outer_band_n is the expected number of members in the outer band
4217 struct {
4218 const char *domain;
4219 const char *flow;
4220 const char *condition;
4221 const char *conditional_validity;
4222 int outer_band_n;
4223 } live_range_tests[] = {
4224 /* Contrived example that illustrates that we need to keep
4225 * track of tagged condition dependences and
4226 * tagged conditional validity dependences
4227 * in isl_sched_edge separately.
4228 * In particular, the conditional validity constraints on A
4229 * cannot be satisfied,
4230 * but they can be ignored because there are no corresponding
4231 * condition constraints. However, we do have an additional
4232 * conditional validity constraint that maps to the same
4233 * dependence relation
4234 * as the condition constraint on B. If we did not make a distinction
4235 * between tagged condition and tagged conditional validity
4236 * dependences, then we
4237 * could end up treating this shared dependence as an condition
4238 * constraint on A, forcing a localization of the conditions,
4239 * which is impossible.
4241 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
4242 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
4243 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
4244 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4245 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4246 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
4249 /* TACO 2013 Fig. 7 */
4250 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4251 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4252 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4253 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
4254 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
4255 "0 <= i < n and 0 <= j < n - 1 }",
4256 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
4257 "0 <= i < n and 0 <= j < j' < n;"
4258 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
4259 "0 <= i < i' < n and 0 <= j,j' < n;"
4260 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
4261 "0 <= i,j,j' < n and j < j' }",
4264 /* TACO 2013 Fig. 7, without tags */
4265 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4266 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4267 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4268 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4269 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4270 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
4271 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
4272 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
4275 /* TACO 2013 Fig. 12 */
4276 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
4277 "S3[i,3] : 0 <= i <= 1 }",
4278 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
4279 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
4280 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
4281 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
4282 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4283 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
4284 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4285 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
4286 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
4287 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
4288 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
4293 /* Test schedule construction based on conditional constraints.
4294 * In particular, check the number of members in the outer band node
4295 * as an indication of whether tiling is possible or not.
4297 static int test_conditional_schedule_constraints(isl_ctx *ctx)
4299 int i;
4300 isl_union_set *domain;
4301 isl_union_map *condition;
4302 isl_union_map *flow;
4303 isl_union_map *validity;
4304 isl_schedule_constraints *sc;
4305 isl_schedule *schedule;
4306 isl_schedule_node *node;
4307 int n_member;
4309 if (test_special_conditional_schedule_constraints(ctx) < 0)
4310 return -1;
4311 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
4312 return -1;
4314 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
4315 domain = isl_union_set_read_from_str(ctx,
4316 live_range_tests[i].domain);
4317 flow = isl_union_map_read_from_str(ctx,
4318 live_range_tests[i].flow);
4319 condition = isl_union_map_read_from_str(ctx,
4320 live_range_tests[i].condition);
4321 validity = isl_union_map_read_from_str(ctx,
4322 live_range_tests[i].conditional_validity);
4323 sc = isl_schedule_constraints_on_domain(domain);
4324 sc = isl_schedule_constraints_set_validity(sc,
4325 isl_union_map_copy(flow));
4326 sc = isl_schedule_constraints_set_proximity(sc, flow);
4327 sc = isl_schedule_constraints_set_conditional_validity(sc,
4328 condition, validity);
4329 schedule = isl_schedule_constraints_compute_schedule(sc);
4330 node = isl_schedule_get_root(schedule);
4331 while (node &&
4332 isl_schedule_node_get_type(node) != isl_schedule_node_band)
4333 node = isl_schedule_node_first_child(node);
4334 n_member = isl_schedule_node_band_n_member(node);
4335 isl_schedule_node_free(node);
4336 isl_schedule_free(schedule);
4338 if (!schedule)
4339 return -1;
4340 if (n_member != live_range_tests[i].outer_band_n)
4341 isl_die(ctx, isl_error_unknown,
4342 "unexpected number of members in outer band",
4343 return -1);
4345 return 0;
4348 /* Check that the schedule computed for the given instance set and
4349 * dependence relation strongly satisfies the dependences.
4350 * In particular, check that no instance is scheduled before
4351 * or together with an instance on which it depends.
4352 * Earlier versions of isl would produce a schedule that
4353 * only weakly satisfies the dependences.
4355 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
4357 const char *domain, *dep;
4358 isl_union_map *D, *schedule;
4359 isl_map *map, *ge;
4360 int empty;
4362 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
4363 "A[i0] : 0 <= i0 <= 1 }";
4364 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
4365 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
4366 schedule = compute_schedule(ctx, domain, dep, dep);
4367 D = isl_union_map_read_from_str(ctx, dep);
4368 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
4369 D = isl_union_map_apply_range(D, schedule);
4370 map = isl_map_from_union_map(D);
4371 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4372 map = isl_map_intersect(map, ge);
4373 empty = isl_map_is_empty(map);
4374 isl_map_free(map);
4376 if (empty < 0)
4377 return -1;
4378 if (!empty)
4379 isl_die(ctx, isl_error_unknown,
4380 "dependences not strongly satisfied", return -1);
4382 return 0;
4385 /* Compute a schedule for input where the instance set constraints
4386 * conflict with the context constraints.
4387 * Earlier versions of isl did not properly handle this situation.
4389 static int test_conflicting_context_schedule(isl_ctx *ctx)
4391 isl_union_map *schedule;
4392 const char *domain, *context;
4394 domain = "[n] -> { A[] : n >= 0 }";
4395 context = "[n] -> { : n < 0 }";
4396 schedule = compute_schedule_with_context(ctx,
4397 domain, "{}", "{}", context);
4398 isl_union_map_free(schedule);
4400 if (!schedule)
4401 return -1;
4403 return 0;
4406 /* Check that a set of schedule constraints that only allow for
4407 * a coalescing schedule still produces a schedule even if the user
4408 * request a non-coalescing schedule. Earlier versions of isl
4409 * would not handle this case correctly.
4411 static int test_coalescing_schedule(isl_ctx *ctx)
4413 const char *domain, *dep;
4414 isl_union_set *I;
4415 isl_union_map *D;
4416 isl_schedule_constraints *sc;
4417 isl_schedule *schedule;
4418 int treat_coalescing;
4420 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4421 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4422 I = isl_union_set_read_from_str(ctx, domain);
4423 D = isl_union_map_read_from_str(ctx, dep);
4424 sc = isl_schedule_constraints_on_domain(I);
4425 sc = isl_schedule_constraints_set_validity(sc, D);
4426 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4427 isl_options_set_schedule_treat_coalescing(ctx, 1);
4428 schedule = isl_schedule_constraints_compute_schedule(sc);
4429 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4430 isl_schedule_free(schedule);
4431 if (!schedule)
4432 return -1;
4433 return 0;
4436 /* Check that the scheduler does not perform any needless
4437 * compound skewing. Earlier versions of isl would compute
4438 * schedules in terms of transformed schedule coefficients and
4439 * would not accurately keep track of the sum of the original
4440 * schedule coefficients. It could then produce the schedule
4441 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
4442 * for the input below instead of the schedule below.
4444 static int test_skewing_schedule(isl_ctx *ctx)
4446 const char *D, *V, *P, *S;
4448 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
4449 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
4450 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
4451 "-1 <= a-i + b-j + c-k <= 1 }";
4452 P = "{ }";
4453 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
4455 return test_special_schedule(ctx, D, V, P, S);
4458 int test_schedule(isl_ctx *ctx)
4460 const char *D, *W, *R, *V, *P, *S;
4461 int max_coincidence;
4462 int treat_coalescing;
4464 /* Handle resulting schedule with zero bands. */
4465 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4466 return -1;
4468 /* Jacobi */
4469 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4470 W = "{ S1[t,i] -> a[t,i] }";
4471 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4472 "S1[t,i] -> a[t-1,i+1] }";
4473 S = "{ S1[t,i] -> [t,i] }";
4474 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4475 return -1;
4477 /* Fig. 5 of CC2008 */
4478 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
4479 "j <= -1 + N }";
4480 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
4481 "j >= 2 and j <= -1 + N }";
4482 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
4483 "j >= 2 and j <= -1 + N; "
4484 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
4485 "j >= 2 and j <= -1 + N }";
4486 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4487 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4488 return -1;
4490 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
4491 W = "{ S1[i] -> a[i] }";
4492 R = "{ S2[i] -> a[i+1] }";
4493 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4494 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4495 return -1;
4497 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
4498 W = "{ S1[i] -> a[i] }";
4499 R = "{ S2[i] -> a[9-i] }";
4500 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4501 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4502 return -1;
4504 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4505 W = "{ S1[i] -> a[i] }";
4506 R = "[N] -> { S2[i] -> a[N-1-i] }";
4507 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4508 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4509 return -1;
4511 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4512 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4513 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4514 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4515 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4516 return -1;
4518 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4519 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4520 R = "{ S2[i,j] -> a[i-1,j] }";
4521 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4522 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4523 return -1;
4525 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4526 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4527 R = "{ S2[i,j] -> a[i,j-1] }";
4528 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4529 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4530 return -1;
4532 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4533 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4534 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4535 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4536 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4537 "S_0[] -> [0, 0, 0] }";
4538 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4539 return -1;
4540 ctx->opt->schedule_parametric = 0;
4541 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4542 return -1;
4543 ctx->opt->schedule_parametric = 1;
4545 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4546 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4547 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4548 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4549 "S4[i] -> a[i,N] }";
4550 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4551 "S4[i] -> [4,i,0] }";
4552 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4553 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4554 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4555 return -1;
4556 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4558 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4559 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4560 "j <= N }";
4561 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4562 "j <= N; "
4563 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4564 "j <= N }";
4565 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4566 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4567 return -1;
4569 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4570 " S_2[t] : t >= 0 and t <= -1 + N; "
4571 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4572 "i <= -1 + N }";
4573 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4574 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4575 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4576 "i >= 0 and i <= -1 + N }";
4577 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4578 "i >= 0 and i <= -1 + N; "
4579 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4580 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4581 " S_0[t] -> [0, t, 0] }";
4583 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4584 return -1;
4585 ctx->opt->schedule_parametric = 0;
4586 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4587 return -1;
4588 ctx->opt->schedule_parametric = 1;
4590 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4591 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4592 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4593 return -1;
4595 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4596 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4597 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
4598 "j >= 0 and j <= -1 + N; "
4599 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4600 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4601 "j >= 0 and j <= -1 + N; "
4602 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4603 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4604 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4605 return -1;
4607 D = "{ S_0[i] : i >= 0 }";
4608 W = "{ S_0[i] -> a[i] : i >= 0 }";
4609 R = "{ S_0[i] -> a[0] : i >= 0 }";
4610 S = "{ S_0[i] -> [0, i, 0] }";
4611 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4612 return -1;
4614 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4615 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4616 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4617 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4618 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4619 return -1;
4621 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4622 "k <= -1 + n and k >= 0 }";
4623 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4624 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4625 "k <= -1 + n and k >= 0; "
4626 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4627 "k <= -1 + n and k >= 0; "
4628 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4629 "k <= -1 + n and k >= 0 }";
4630 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4631 ctx->opt->schedule_outer_coincidence = 1;
4632 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4633 return -1;
4634 ctx->opt->schedule_outer_coincidence = 0;
4636 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4637 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4638 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4639 "Stmt_for_body24[i0, i1, 1, 0]:"
4640 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4641 "Stmt_for_body7[i0, i1, i2]:"
4642 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4643 "i2 <= 7 }";
4645 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4646 "Stmt_for_body24[1, i1, i2, i3]:"
4647 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4648 "i2 >= 1;"
4649 "Stmt_for_body24[0, i1, i2, i3] -> "
4650 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4651 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4652 "i3 >= 0;"
4653 "Stmt_for_body24[0, i1, i2, i3] ->"
4654 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4655 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4656 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4657 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4658 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4659 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4660 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4661 "i1 <= 6 and i1 >= 0;"
4662 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4663 "Stmt_for_body7[i0, i1, i2] -> "
4664 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4665 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4666 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4667 "Stmt_for_body7[i0, i1, i2] -> "
4668 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4669 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4670 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4671 P = V;
4673 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4674 isl_options_set_schedule_treat_coalescing(ctx, 0);
4675 if (test_has_schedule(ctx, D, V, P) < 0)
4676 return -1;
4677 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4679 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4680 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4681 "j >= 1 and j <= 7;"
4682 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4683 "j >= 1 and j <= 8 }";
4684 P = "{ }";
4685 S = "{ S_0[i, j] -> [i + j, i] }";
4686 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4687 if (test_special_schedule(ctx, D, V, P, S) < 0)
4688 return -1;
4689 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4691 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4692 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4693 "j >= 0 and j <= -1 + i }";
4694 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4695 "i <= -1 + N and j >= 0;"
4696 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4697 "i <= -2 + N }";
4698 P = "{ }";
4699 S = "{ S_0[i, j] -> [i, j] }";
4700 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4701 if (test_special_schedule(ctx, D, V, P, S) < 0)
4702 return -1;
4703 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4705 /* Test both algorithms on a case with only proximity dependences. */
4706 D = "{ S[i,j] : 0 <= i <= 10 }";
4707 V = "{ }";
4708 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4709 S = "{ S[i, j] -> [j, i] }";
4710 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4711 if (test_special_schedule(ctx, D, V, P, S) < 0)
4712 return -1;
4713 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4714 if (test_special_schedule(ctx, D, V, P, S) < 0)
4715 return -1;
4717 D = "{ A[a]; B[] }";
4718 V = "{}";
4719 P = "{ A[a] -> B[] }";
4720 if (test_has_schedule(ctx, D, V, P) < 0)
4721 return -1;
4723 if (test_padded_schedule(ctx) < 0)
4724 return -1;
4726 /* Check that check for progress is not confused by rational
4727 * solution.
4729 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4730 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4731 "i0 <= -2 + N; "
4732 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4733 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4734 P = "{}";
4735 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4736 if (test_has_schedule(ctx, D, V, P) < 0)
4737 return -1;
4738 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4740 /* Check that we allow schedule rows that are only non-trivial
4741 * on some full-dimensional domains.
4743 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4744 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4745 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4746 P = "{}";
4747 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4748 if (test_has_schedule(ctx, D, V, P) < 0)
4749 return -1;
4750 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4752 if (test_conditional_schedule_constraints(ctx) < 0)
4753 return -1;
4755 if (test_strongly_satisfying_schedule(ctx) < 0)
4756 return -1;
4758 if (test_conflicting_context_schedule(ctx) < 0)
4759 return -1;
4761 if (test_coalescing_schedule(ctx) < 0)
4762 return -1;
4763 if (test_skewing_schedule(ctx) < 0)
4764 return -1;
4766 return 0;
4769 /* Perform scheduling tests using the whole component scheduler.
4771 static int test_schedule_whole(isl_ctx *ctx)
4773 int whole;
4774 int r;
4776 whole = isl_options_get_schedule_whole_component(ctx);
4777 isl_options_set_schedule_whole_component(ctx, 1);
4778 r = test_schedule(ctx);
4779 isl_options_set_schedule_whole_component(ctx, whole);
4781 return r;
4784 /* Perform scheduling tests using the incremental scheduler.
4786 static int test_schedule_incremental(isl_ctx *ctx)
4788 int whole;
4789 int r;
4791 whole = isl_options_get_schedule_whole_component(ctx);
4792 isl_options_set_schedule_whole_component(ctx, 0);
4793 r = test_schedule(ctx);
4794 isl_options_set_schedule_whole_component(ctx, whole);
4796 return r;
4799 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4801 isl_union_map *umap;
4802 int test;
4804 umap = isl_union_map_read_from_str(ctx, str);
4805 test = isl_union_map_plain_is_injective(umap);
4806 isl_union_map_free(umap);
4807 if (test < 0)
4808 return -1;
4809 if (test == injective)
4810 return 0;
4811 if (injective)
4812 isl_die(ctx, isl_error_unknown,
4813 "map not detected as injective", return -1);
4814 else
4815 isl_die(ctx, isl_error_unknown,
4816 "map detected as injective", return -1);
4819 int test_injective(isl_ctx *ctx)
4821 const char *str;
4823 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4824 return -1;
4825 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4826 return -1;
4827 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4828 return -1;
4829 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4830 return -1;
4831 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4832 return -1;
4833 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4834 return -1;
4835 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4836 return -1;
4837 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4838 return -1;
4840 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4841 if (test_plain_injective(ctx, str, 1))
4842 return -1;
4843 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4844 if (test_plain_injective(ctx, str, 0))
4845 return -1;
4847 return 0;
4850 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4852 isl_aff *aff2;
4853 int equal;
4855 if (!aff)
4856 return -1;
4858 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4859 equal = isl_aff_plain_is_equal(aff, aff2);
4860 isl_aff_free(aff2);
4862 return equal;
4865 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4867 int equal;
4869 equal = aff_plain_is_equal(aff, str);
4870 if (equal < 0)
4871 return -1;
4872 if (!equal)
4873 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4874 "result not as expected", return -1);
4875 return 0;
4878 /* Is "upa" obviously equal to the isl_union_pw_aff represented by "str"?
4880 static isl_bool union_pw_aff_plain_is_equal(__isl_keep isl_union_pw_aff *upa,
4881 const char *str)
4883 isl_ctx *ctx;
4884 isl_union_pw_aff *upa2;
4885 isl_bool equal;
4887 if (!upa)
4888 return isl_bool_error;
4890 ctx = isl_union_pw_aff_get_ctx(upa);
4891 upa2 = isl_union_pw_aff_read_from_str(ctx, str);
4892 equal = isl_union_pw_aff_plain_is_equal(upa, upa2);
4893 isl_union_pw_aff_free(upa2);
4895 return equal;
4898 /* Check that "upa" is obviously equal to the isl_union_pw_aff
4899 * represented by "str".
4901 static isl_stat union_pw_aff_check_plain_equal(__isl_keep isl_union_pw_aff *upa,
4902 const char *str)
4904 isl_bool equal;
4906 equal = union_pw_aff_plain_is_equal(upa, str);
4907 if (equal < 0)
4908 return isl_stat_error;
4909 if (!equal)
4910 isl_die(isl_union_pw_aff_get_ctx(upa), isl_error_unknown,
4911 "result not as expected", return isl_stat_error);
4912 return isl_stat_ok;
4915 /* Basic tests on isl_union_pw_aff.
4917 * In particular, check that isl_union_pw_aff_aff_on_domain
4918 * aligns the parameters of the input objects and
4919 * that isl_union_pw_aff_param_on_domain_id properly
4920 * introduces the parameter.
4922 static int test_upa(isl_ctx *ctx)
4924 const char *str;
4925 isl_id *id;
4926 isl_aff *aff;
4927 isl_union_set *domain;
4928 isl_union_pw_aff *upa;
4929 isl_stat ok;
4931 aff = isl_aff_read_from_str(ctx, "[N] -> { [N] }");
4932 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
4933 domain = isl_union_set_read_from_str(ctx, str);
4934 upa = isl_union_pw_aff_aff_on_domain(domain, aff);
4935 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
4936 ok = union_pw_aff_check_plain_equal(upa, str);
4937 isl_union_pw_aff_free(upa);
4938 if (ok < 0)
4939 return -1;
4941 id = isl_id_alloc(ctx, "N", NULL);
4942 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
4943 domain = isl_union_set_read_from_str(ctx, str);
4944 upa = isl_union_pw_aff_param_on_domain_id(domain, id);
4945 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
4946 ok = union_pw_aff_check_plain_equal(upa, str);
4947 isl_union_pw_aff_free(upa);
4948 if (ok < 0)
4949 return -1;
4951 return 0;
4954 struct {
4955 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4956 __isl_take isl_aff *aff2);
4957 } aff_bin_op[] = {
4958 ['+'] = { &isl_aff_add },
4959 ['-'] = { &isl_aff_sub },
4960 ['*'] = { &isl_aff_mul },
4961 ['/'] = { &isl_aff_div },
4964 struct {
4965 const char *arg1;
4966 unsigned char op;
4967 const char *arg2;
4968 const char *res;
4969 } aff_bin_tests[] = {
4970 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4971 "{ [i] -> [2i] }" },
4972 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4973 "{ [i] -> [0] }" },
4974 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4975 "{ [i] -> [2i] }" },
4976 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4977 "{ [i] -> [2i] }" },
4978 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4979 "{ [i] -> [i/2] }" },
4980 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4981 "{ [i] -> [i] }" },
4982 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4983 "{ [i] -> [NaN] }" },
4984 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
4985 "{ [i] -> [NaN] }" },
4986 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
4987 "{ [i] -> [NaN] }" },
4988 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
4989 "{ [i] -> [NaN] }" },
4990 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
4991 "{ [i] -> [NaN] }" },
4992 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
4993 "{ [i] -> [NaN] }" },
4994 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
4995 "{ [i] -> [NaN] }" },
4996 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
4997 "{ [i] -> [NaN] }" },
4998 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
4999 "{ [i] -> [NaN] }" },
5000 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
5001 "{ [i] -> [NaN] }" },
5002 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
5003 "{ [i] -> [NaN] }" },
5004 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
5005 "{ [i] -> [NaN] }" },
5008 /* Perform some basic tests of binary operations on isl_aff objects.
5010 static int test_bin_aff(isl_ctx *ctx)
5012 int i;
5013 isl_aff *aff1, *aff2, *res;
5014 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5015 __isl_take isl_aff *aff2);
5016 int ok;
5018 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
5019 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
5020 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
5021 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
5022 fn = aff_bin_op[aff_bin_tests[i].op].fn;
5023 aff1 = fn(aff1, aff2);
5024 if (isl_aff_is_nan(res))
5025 ok = isl_aff_is_nan(aff1);
5026 else
5027 ok = isl_aff_plain_is_equal(aff1, res);
5028 isl_aff_free(aff1);
5029 isl_aff_free(res);
5030 if (ok < 0)
5031 return -1;
5032 if (!ok)
5033 isl_die(ctx, isl_error_unknown,
5034 "unexpected result", return -1);
5037 return 0;
5040 struct {
5041 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
5042 __isl_take isl_pw_aff *pa2);
5043 } pw_aff_bin_op[] = {
5044 ['m'] = { &isl_pw_aff_min },
5045 ['M'] = { &isl_pw_aff_max },
5048 /* Inputs for binary isl_pw_aff operation tests.
5049 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
5050 * defined by pw_aff_bin_op, and "res" is the expected result.
5052 struct {
5053 const char *arg1;
5054 unsigned char op;
5055 const char *arg2;
5056 const char *res;
5057 } pw_aff_bin_tests[] = {
5058 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
5059 "{ [i] -> [i] }" },
5060 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
5061 "{ [i] -> [i] }" },
5062 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
5063 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
5064 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
5065 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
5066 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
5067 "{ [i] -> [NaN] }" },
5068 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
5069 "{ [i] -> [NaN] }" },
5072 /* Perform some basic tests of binary operations on isl_pw_aff objects.
5074 static int test_bin_pw_aff(isl_ctx *ctx)
5076 int i;
5077 isl_bool ok;
5078 isl_pw_aff *pa1, *pa2, *res;
5080 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
5081 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
5082 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
5083 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
5084 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
5085 if (isl_pw_aff_involves_nan(res))
5086 ok = isl_pw_aff_involves_nan(pa1);
5087 else
5088 ok = isl_pw_aff_plain_is_equal(pa1, res);
5089 isl_pw_aff_free(pa1);
5090 isl_pw_aff_free(res);
5091 if (ok < 0)
5092 return -1;
5093 if (!ok)
5094 isl_die(ctx, isl_error_unknown,
5095 "unexpected result", return -1);
5098 return 0;
5101 struct {
5102 __isl_give isl_union_pw_multi_aff *(*fn)(
5103 __isl_take isl_union_pw_multi_aff *upma1,
5104 __isl_take isl_union_pw_multi_aff *upma2);
5105 const char *arg1;
5106 const char *arg2;
5107 const char *res;
5108 } upma_bin_tests[] = {
5109 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
5110 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
5111 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
5112 "{ B[x] -> [2] : x >= 0 }",
5113 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
5114 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
5115 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
5116 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
5117 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
5118 "D[i] -> B[2] : i >= 5 }" },
5119 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5120 "{ B[x] -> C[2] : x > 0 }",
5121 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
5122 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5123 "{ B[x] -> A[2] : x >= 0 }",
5124 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
5127 /* Perform some basic tests of binary operations on
5128 * isl_union_pw_multi_aff objects.
5130 static int test_bin_upma(isl_ctx *ctx)
5132 int i;
5133 isl_union_pw_multi_aff *upma1, *upma2, *res;
5134 int ok;
5136 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
5137 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5138 upma_bin_tests[i].arg1);
5139 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5140 upma_bin_tests[i].arg2);
5141 res = isl_union_pw_multi_aff_read_from_str(ctx,
5142 upma_bin_tests[i].res);
5143 upma1 = upma_bin_tests[i].fn(upma1, upma2);
5144 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
5145 isl_union_pw_multi_aff_free(upma1);
5146 isl_union_pw_multi_aff_free(res);
5147 if (ok < 0)
5148 return -1;
5149 if (!ok)
5150 isl_die(ctx, isl_error_unknown,
5151 "unexpected result", return -1);
5154 return 0;
5157 struct {
5158 __isl_give isl_union_pw_multi_aff *(*fn)(
5159 __isl_take isl_union_pw_multi_aff *upma1,
5160 __isl_take isl_union_pw_multi_aff *upma2);
5161 const char *arg1;
5162 const char *arg2;
5163 } upma_bin_fail_tests[] = {
5164 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5165 "{ B[x] -> C[2] : x >= 0 }" },
5168 /* Perform some basic tests of binary operations on
5169 * isl_union_pw_multi_aff objects that are expected to fail.
5171 static int test_bin_upma_fail(isl_ctx *ctx)
5173 int i, n;
5174 isl_union_pw_multi_aff *upma1, *upma2;
5175 int on_error;
5177 on_error = isl_options_get_on_error(ctx);
5178 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
5179 n = ARRAY_SIZE(upma_bin_fail_tests);
5180 for (i = 0; i < n; ++i) {
5181 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5182 upma_bin_fail_tests[i].arg1);
5183 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5184 upma_bin_fail_tests[i].arg2);
5185 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
5186 isl_union_pw_multi_aff_free(upma1);
5187 if (upma1)
5188 break;
5190 isl_options_set_on_error(ctx, on_error);
5191 if (i < n)
5192 isl_die(ctx, isl_error_unknown,
5193 "operation not expected to succeed", return -1);
5195 return 0;
5198 /* Inputs for basic tests of unary operations on isl_multi_pw_aff objects.
5199 * "fn" is the function that is tested.
5200 * "arg" is a string description of the input.
5201 * "res" is a string description of the expected result.
5203 struct {
5204 __isl_give isl_multi_pw_aff *(*fn)(__isl_take isl_multi_pw_aff *mpa);
5205 const char *arg;
5206 const char *res;
5207 } mpa_un_tests[] = {
5208 { &isl_multi_pw_aff_range_factor_domain,
5209 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5210 "{ A[x] -> B[(1 : x >= 5)] }" },
5211 { &isl_multi_pw_aff_range_factor_range,
5212 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5213 "{ A[y] -> C[(2 : y <= 10)] }" },
5214 { &isl_multi_pw_aff_range_factor_domain,
5215 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5216 "{ A[x] -> B[(1 : x >= 5)] }" },
5217 { &isl_multi_pw_aff_range_factor_range,
5218 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5219 "{ A[y] -> C[] }" },
5220 { &isl_multi_pw_aff_range_factor_domain,
5221 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5222 "{ A[x] -> B[] }" },
5223 { &isl_multi_pw_aff_range_factor_range,
5224 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5225 "{ A[y] -> C[(2 : y <= 10)] }" },
5226 { &isl_multi_pw_aff_range_factor_domain,
5227 "{ A[x] -> [B[] -> C[]] }",
5228 "{ A[x] -> B[] }" },
5229 { &isl_multi_pw_aff_range_factor_range,
5230 "{ A[x] -> [B[] -> C[]] }",
5231 "{ A[y] -> C[] }" },
5232 { &isl_multi_pw_aff_factor_range,
5233 "{ [B[] -> C[]] }",
5234 "{ C[] }" },
5235 { &isl_multi_pw_aff_range_factor_domain,
5236 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5237 "{ A[x] -> B[] : x >= 0 }" },
5238 { &isl_multi_pw_aff_range_factor_range,
5239 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5240 "{ A[y] -> C[] : y >= 0 }" },
5241 { &isl_multi_pw_aff_factor_range,
5242 "[N] -> { [B[] -> C[]] : N >= 0 }",
5243 "[N] -> { C[] : N >= 0 }" },
5246 /* Perform some basic tests of unary operations on isl_multi_pw_aff objects.
5248 static int test_un_mpa(isl_ctx *ctx)
5250 int i;
5251 isl_bool ok;
5252 isl_multi_pw_aff *mpa, *res;
5254 for (i = 0; i < ARRAY_SIZE(mpa_un_tests); ++i) {
5255 mpa = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].arg);
5256 res = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].res);
5257 mpa = mpa_un_tests[i].fn(mpa);
5258 ok = isl_multi_pw_aff_plain_is_equal(mpa, res);
5259 isl_multi_pw_aff_free(mpa);
5260 isl_multi_pw_aff_free(res);
5261 if (ok < 0)
5262 return -1;
5263 if (!ok)
5264 isl_die(ctx, isl_error_unknown,
5265 "unexpected result", return -1);
5268 return 0;
5271 /* Inputs for basic tests of binary operations on isl_multi_pw_aff objects.
5272 * "fn" is the function that is tested.
5273 * "arg1" and "arg2" are string descriptions of the inputs.
5274 * "res" is a string description of the expected result.
5276 struct {
5277 __isl_give isl_multi_pw_aff *(*fn)(
5278 __isl_take isl_multi_pw_aff *mpa1,
5279 __isl_take isl_multi_pw_aff *mpa2);
5280 const char *arg1;
5281 const char *arg2;
5282 const char *res;
5283 } mpa_bin_tests[] = {
5284 { &isl_multi_pw_aff_add, "{ A[] -> [1] }", "{ A[] -> [2] }",
5285 "{ A[] -> [3] }" },
5286 { &isl_multi_pw_aff_add, "{ A[x] -> [(1 : x >= 5)] }",
5287 "{ A[x] -> [(x : x <= 10)] }",
5288 "{ A[x] -> [(1 + x : 5 <= x <= 10)] }" },
5289 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5290 "{ A[x] -> [] : x <= 10 }",
5291 "{ A[x] -> [] : 5 <= x <= 10 }" },
5292 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5293 "[N] -> { A[x] -> [] : x <= N }",
5294 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5295 { &isl_multi_pw_aff_add,
5296 "[N] -> { A[x] -> [] : x <= N }",
5297 "{ A[x] -> [] : x >= 5 }",
5298 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5299 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5300 "{ A[y] -> C[(2 : y <= 10)] }",
5301 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5302 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5303 "{ A[y] -> C[2] : y <= 10 }",
5304 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5305 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5306 "[N] -> { A[y] -> C[2] : y <= N }",
5307 "[N] -> { A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= N)]] }" },
5308 { &isl_multi_pw_aff_range_product, "[N] -> { A[x] -> B[1] : x >= N }",
5309 "{ A[y] -> C[2] : y <= 10 }",
5310 "[N] -> { A[x] -> [B[(1 : x >= N)] -> C[(2 : x <= 10)]] }" },
5311 { &isl_multi_pw_aff_range_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5312 "{ A[] -> [B[1] -> C[2]] }" },
5313 { &isl_multi_pw_aff_range_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5314 "{ A[] -> [B[] -> C[]] }" },
5315 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5316 "{ A[y] -> C[] : y <= 10 }",
5317 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] : x <= 10 }" },
5318 { &isl_multi_pw_aff_range_product, "{ A[y] -> C[] : y <= 10 }",
5319 "{ A[x] -> B[(1 : x >= 5)] }",
5320 "{ A[x] -> [C[] -> B[(1 : x >= 5)]] : x <= 10 }" },
5321 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5322 "{ A[y] -> C[(2 : y <= 10)] }",
5323 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[(2 : y <= 10)]] }" },
5324 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5325 "{ A[y] -> C[] : y <= 10 }",
5326 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= 10 }" },
5327 { &isl_multi_pw_aff_product, "{ A[y] -> C[] : y <= 10 }",
5328 "{ A[x] -> B[(1 : x >= 5)] }",
5329 "{ [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= 10 }" },
5330 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5331 "[N] -> { A[y] -> C[] : y <= N }",
5332 "[N] -> { [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= N }" },
5333 { &isl_multi_pw_aff_product, "[N] -> { A[y] -> C[] : y <= N }",
5334 "{ A[x] -> B[(1 : x >= 5)] }",
5335 "[N] -> { [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= N }" },
5336 { &isl_multi_pw_aff_product, "{ A[x] -> B[] : x >= 5 }",
5337 "{ A[y] -> C[] : y <= 10 }",
5338 "{ [A[x] -> A[y]] -> [B[] -> C[]] : x >= 5 and y <= 10 }" },
5339 { &isl_multi_pw_aff_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5340 "{ [A[] -> A[]] -> [B[1] -> C[2]] }" },
5341 { &isl_multi_pw_aff_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5342 "{ [A[] -> A[]] -> [B[] -> C[]] }" },
5343 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5344 "{ B[i,j] -> C[i + 2j] }", "{ A[a,b] -> B[b,a] }",
5345 "{ A[a,b] -> C[b + 2a] }" },
5346 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5347 "{ B[i,j] -> C[i + 2j] }",
5348 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5349 "{ A[a,b] -> C[(b + 2a : b > a)] }" },
5350 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5351 "{ B[i,j] -> C[(i + 2j : j > 4)] }",
5352 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5353 "{ A[a,b] -> C[(b + 2a : b > a > 4)] }" },
5354 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5355 "{ B[i,j] -> C[] }",
5356 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5357 "{ A[a,b] -> C[] }" },
5358 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5359 "{ B[i,j] -> C[] : i > j }",
5360 "{ A[a,b] -> B[b,a] }",
5361 "{ A[a,b] -> C[] : b > a }" },
5362 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5363 "{ B[i,j] -> C[] : j > 5 }",
5364 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5365 "{ A[a,b] -> C[] : b > a > 5 }" },
5366 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5367 "[N] -> { B[i,j] -> C[] : j > N }",
5368 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5369 "[N] -> { A[a,b] -> C[] : b > a > N }" },
5370 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5371 "[M,N] -> { B[] -> C[] : N > 5 }",
5372 "[M,N] -> { A[] -> B[] : M > N }",
5373 "[M,N] -> { A[] -> C[] : M > N > 5 }" },
5376 /* Perform some basic tests of binary operations on isl_multi_pw_aff objects.
5378 static int test_bin_mpa(isl_ctx *ctx)
5380 int i;
5381 isl_bool ok;
5382 isl_multi_pw_aff *mpa1, *mpa2, *res;
5384 for (i = 0; i < ARRAY_SIZE(mpa_bin_tests); ++i) {
5385 mpa1 = isl_multi_pw_aff_read_from_str(ctx,
5386 mpa_bin_tests[i].arg1);
5387 mpa2 = isl_multi_pw_aff_read_from_str(ctx,
5388 mpa_bin_tests[i].arg2);
5389 res = isl_multi_pw_aff_read_from_str(ctx,
5390 mpa_bin_tests[i].res);
5391 mpa1 = mpa_bin_tests[i].fn(mpa1, mpa2);
5392 ok = isl_multi_pw_aff_plain_is_equal(mpa1, res);
5393 isl_multi_pw_aff_free(mpa1);
5394 isl_multi_pw_aff_free(res);
5395 if (ok < 0)
5396 return -1;
5397 if (!ok)
5398 isl_die(ctx, isl_error_unknown,
5399 "unexpected result", return -1);
5402 return 0;
5405 /* Inputs for basic tests of unary operations on
5406 * isl_multi_union_pw_aff objects.
5407 * "fn" is the function that is tested.
5408 * "arg" is a string description of the input.
5409 * "res" is a string description of the expected result.
5411 struct {
5412 __isl_give isl_multi_union_pw_aff *(*fn)(
5413 __isl_take isl_multi_union_pw_aff *mupa);
5414 const char *arg;
5415 const char *res;
5416 } mupa_un_tests[] = {
5417 { &isl_multi_union_pw_aff_factor_range,
5418 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]",
5419 "C[{ A[] -> [2] }]" },
5420 { &isl_multi_union_pw_aff_factor_range,
5421 "[B[] -> C[{ A[] -> [2] }]]",
5422 "C[{ A[] -> [2] }]" },
5423 { &isl_multi_union_pw_aff_factor_range,
5424 "[B[{ A[] -> [1] }] -> C[]]",
5425 "C[]" },
5426 { &isl_multi_union_pw_aff_factor_range,
5427 "[B[] -> C[]]",
5428 "C[]" },
5429 { &isl_multi_union_pw_aff_factor_range,
5430 "([B[] -> C[]] : { A[x] : x >= 0 })",
5431 "(C[] : { A[x] : x >= 0 })" },
5432 { &isl_multi_union_pw_aff_factor_range,
5433 "[N] -> ([B[] -> C[]] : { A[x] : x <= N })",
5434 "[N] -> (C[] : { A[x] : x <= N })" },
5435 { &isl_multi_union_pw_aff_factor_range,
5436 "[N] -> ([B[] -> C[]] : { : N >= 0 })",
5437 "[N] -> (C[] : { : N >= 0 })" },
5440 /* Perform some basic tests of unary operations on
5441 * isl_multi_union_pw_aff objects.
5443 static int test_un_mupa(isl_ctx *ctx)
5445 int i;
5446 isl_bool ok;
5447 isl_multi_union_pw_aff *mupa, *res;
5449 for (i = 0; i < ARRAY_SIZE(mupa_un_tests); ++i) {
5450 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5451 mupa_un_tests[i].arg);
5452 res = isl_multi_union_pw_aff_read_from_str(ctx,
5453 mupa_un_tests[i].res);
5454 mupa = mupa_un_tests[i].fn(mupa);
5455 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5456 isl_multi_union_pw_aff_free(mupa);
5457 isl_multi_union_pw_aff_free(res);
5458 if (ok < 0)
5459 return -1;
5460 if (!ok)
5461 isl_die(ctx, isl_error_unknown,
5462 "unexpected result", return -1);
5465 return 0;
5468 /* Inputs for basic tests of binary operations on
5469 * isl_multi_union_pw_aff objects.
5470 * "fn" is the function that is tested.
5471 * "arg1" and "arg2" are string descriptions of the inputs.
5472 * "res" is a string description of the expected result.
5474 struct {
5475 __isl_give isl_multi_union_pw_aff *(*fn)(
5476 __isl_take isl_multi_union_pw_aff *mupa1,
5477 __isl_take isl_multi_union_pw_aff *mupa2);
5478 const char *arg1;
5479 const char *arg2;
5480 const char *res;
5481 } mupa_bin_tests[] = {
5482 { &isl_multi_union_pw_aff_add, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5483 "[{ A[] -> [3] }]" },
5484 { &isl_multi_union_pw_aff_sub, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5485 "[{ A[] -> [-1] }]" },
5486 { &isl_multi_union_pw_aff_add,
5487 "[{ A[] -> [1]; B[] -> [4] }]",
5488 "[{ A[] -> [2]; C[] -> [5] }]",
5489 "[{ A[] -> [3] }]" },
5490 { &isl_multi_union_pw_aff_union_add,
5491 "[{ A[] -> [1]; B[] -> [4] }]",
5492 "[{ A[] -> [2]; C[] -> [5] }]",
5493 "[{ A[] -> [3]; B[] -> [4]; C[] -> [5] }]" },
5494 { &isl_multi_union_pw_aff_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5495 "[{ A[x] -> [(x)] : x <= 10 }]",
5496 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10 }]" },
5497 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5498 "([] : { A[x] : x <= 10 })",
5499 "([] : { A[x] : 5 <= x <= 10 })" },
5500 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5501 "[N] -> ([] : { A[x] : x <= N })",
5502 "[N] -> ([] : { A[x] : 5 <= x <= N })" },
5503 { &isl_multi_union_pw_aff_add, "[N] -> ([] : { A[x] : x >= N })",
5504 "([] : { A[x] : x <= 10 })",
5505 "[N] -> ([] : { A[x] : N <= x <= 10 })" },
5506 { &isl_multi_union_pw_aff_union_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5507 "[{ A[x] -> [(x)] : x <= 10 }]",
5508 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10; "
5509 "A[x] -> [(1)] : x > 10; A[x] -> [(x)] : x < 5 }]" },
5510 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 5 })",
5511 "([] : { A[x] : x <= 10 })",
5512 "([] : { A[x] })" },
5513 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 0 })",
5514 "[N] -> ([] : { A[x] : x >= N })",
5515 "[N] -> ([] : { A[x] : x >= 0 or x >= N })" },
5516 { &isl_multi_union_pw_aff_union_add,
5517 "[N] -> ([] : { A[] : N >= 0})",
5518 "[N] -> ([] : { A[] : N <= 0})",
5519 "[N] -> ([] : { A[] })" },
5520 { &isl_multi_union_pw_aff_union_add,
5521 "[N] -> ([] : { A[] })",
5522 "[N] -> ([] : { : })",
5523 "[N] -> ([] : { : })" },
5524 { &isl_multi_union_pw_aff_union_add,
5525 "[N] -> ([] : { : })",
5526 "[N] -> ([] : { A[] })",
5527 "[N] -> ([] : { : })" },
5528 { &isl_multi_union_pw_aff_union_add,
5529 "[N] -> ([] : { : N >= 0})",
5530 "[N] -> ([] : { : N <= 0})",
5531 "[N] -> ([] : { : })" },
5532 { &isl_multi_union_pw_aff_range_product,
5533 "B[{ A[] -> [1] }]",
5534 "C[{ A[] -> [2] }]",
5535 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]" },
5536 { &isl_multi_union_pw_aff_range_product,
5537 "(B[] : { A[x] : x >= 5 })",
5538 "(C[] : { A[x] : x <= 10 })",
5539 "([B[] -> C[]] : { A[x] : 5 <= x <= 10 })" },
5540 { &isl_multi_union_pw_aff_range_product,
5541 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5542 "(C[] : { A[x] : x <= 10 })",
5543 "[B[{ A[x] -> [x + 1] : 5 <= x <= 10 }] -> C[]]" },
5544 { &isl_multi_union_pw_aff_range_product,
5545 "(C[] : { A[x] : x <= 10 })",
5546 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5547 "[C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= 10 }]]" },
5548 { &isl_multi_union_pw_aff_range_product,
5549 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5550 "[N] -> (C[] : { A[x] : x <= N })",
5551 "[N] -> [B[{ A[x] -> [x + 1] : 5 <= x <= N }] -> C[]]" },
5552 { &isl_multi_union_pw_aff_range_product,
5553 "[N] -> (C[] : { A[x] : x <= N })",
5554 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5555 "[N] -> [C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= N }]]" },
5556 { &isl_multi_union_pw_aff_range_product,
5557 "B[{ A[] -> [1]; D[] -> [3] }]",
5558 "C[{ A[] -> [2] }]",
5559 "[B[{ A[] -> [1]; D[] -> [3] }] -> C[{ A[] -> [2] }]]" },
5560 { &isl_multi_union_pw_aff_range_product,
5561 "B[] }]",
5562 "(C[] : { A[x] })",
5563 "([B[] -> C[]] : { A[x] })" },
5564 { &isl_multi_union_pw_aff_range_product,
5565 "(B[] : { A[x] })",
5566 "C[] }]",
5567 "([B[] -> C[]] : { A[x] })" },
5570 /* Perform some basic tests of binary operations on
5571 * isl_multi_union_pw_aff objects.
5573 static int test_bin_mupa(isl_ctx *ctx)
5575 int i;
5576 isl_bool ok;
5577 isl_multi_union_pw_aff *mupa1, *mupa2, *res;
5579 for (i = 0; i < ARRAY_SIZE(mupa_bin_tests); ++i) {
5580 mupa1 = isl_multi_union_pw_aff_read_from_str(ctx,
5581 mupa_bin_tests[i].arg1);
5582 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx,
5583 mupa_bin_tests[i].arg2);
5584 res = isl_multi_union_pw_aff_read_from_str(ctx,
5585 mupa_bin_tests[i].res);
5586 mupa1 = mupa_bin_tests[i].fn(mupa1, mupa2);
5587 ok = isl_multi_union_pw_aff_plain_is_equal(mupa1, res);
5588 isl_multi_union_pw_aff_free(mupa1);
5589 isl_multi_union_pw_aff_free(res);
5590 if (ok < 0)
5591 return -1;
5592 if (!ok)
5593 isl_die(ctx, isl_error_unknown,
5594 "unexpected result", return -1);
5597 return 0;
5600 /* Inputs for basic tests of binary operations on
5601 * pairs of isl_multi_union_pw_aff and isl_set objects.
5602 * "fn" is the function that is tested.
5603 * "arg1" and "arg2" are string descriptions of the inputs.
5604 * "res" is a string description of the expected result.
5606 struct {
5607 __isl_give isl_multi_union_pw_aff *(*fn)(
5608 __isl_take isl_multi_union_pw_aff *mupa,
5609 __isl_take isl_set *set);
5610 const char *arg1;
5611 const char *arg2;
5612 const char *res;
5613 } mupa_set_tests[] = {
5614 { &isl_multi_union_pw_aff_intersect_range,
5615 "C[{ B[i,j] -> [i + 2j] }]", "{ C[1] }",
5616 "C[{ B[i,j] -> [i + 2j] : i + 2j = 1 }]" },
5617 { &isl_multi_union_pw_aff_intersect_range,
5618 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[N] }",
5619 "[N] -> C[{ B[i,j] -> [i + 2j] : i + 2j = N }]" },
5620 { &isl_multi_union_pw_aff_intersect_range,
5621 "[N] -> C[{ B[i,j] -> [i + 2j + N] }]", "{ C[1] }",
5622 "[N] -> C[{ B[i,j] -> [i + 2j + N] : i + 2j + N = 1 }]" },
5623 { &isl_multi_union_pw_aff_intersect_range,
5624 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[x] : N >= 0 }",
5625 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0 }]" },
5626 { &isl_multi_union_pw_aff_intersect_range,
5627 "C[]", "{ C[] }", "C[]" },
5628 { &isl_multi_union_pw_aff_intersect_range,
5629 "[N] -> (C[] : { : N >= 0 })",
5630 "{ C[] }",
5631 "[N] -> (C[] : { : N >= 0 })" },
5632 { &isl_multi_union_pw_aff_intersect_range,
5633 "(C[] : { A[a,b] })",
5634 "{ C[] }",
5635 "(C[] : { A[a,b] })" },
5636 { &isl_multi_union_pw_aff_intersect_range,
5637 "[N] -> (C[] : { A[a,b] : a,b <= N })",
5638 "{ C[] }",
5639 "[N] -> (C[] : { A[a,b] : a,b <= N })" },
5640 { &isl_multi_union_pw_aff_intersect_range,
5641 "C[]",
5642 "[N] -> { C[] : N >= 0 }",
5643 "[N] -> (C[] : { : N >= 0 })" },
5644 { &isl_multi_union_pw_aff_intersect_range,
5645 "(C[] : { A[a,b] })",
5646 "[N] -> { C[] : N >= 0 }",
5647 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5648 { &isl_multi_union_pw_aff_intersect_range,
5649 "[N] -> (C[] : { : N >= 0 })",
5650 "[N] -> { C[] : N < 1024 }",
5651 "[N] -> (C[] : { : 0 <= N < 1024 })" },
5652 { &isl_multi_union_pw_aff_intersect_params,
5653 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { : N >= 0 }",
5654 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0}]" },
5655 { &isl_multi_union_pw_aff_intersect_params,
5656 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "[N] -> { : N >= 0 }",
5657 "[N] -> C[{ B[i,j] -> [i + 2j] : 0 <= N <= 256 }]" },
5658 { &isl_multi_union_pw_aff_intersect_params,
5659 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "{ : }",
5660 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]" },
5661 { &isl_multi_union_pw_aff_intersect_params,
5662 "C[]", "[N] -> { : N >= 0 }",
5663 "[N] -> (C[] : { : N >= 0 })" },
5664 { &isl_multi_union_pw_aff_intersect_params,
5665 "(C[] : { A[a,b] })", "[N] -> { : N >= 0 }",
5666 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5667 { &isl_multi_union_pw_aff_intersect_params,
5668 "[N] -> (C[] : { A[a,N] })", "{ : }",
5669 "[N] -> (C[] : { A[a,N] })" },
5670 { &isl_multi_union_pw_aff_intersect_params,
5671 "[N] -> (C[] : { A[a,b] : N <= 256 })", "[N] -> { : N >= 0 }",
5672 "[N] -> (C[] : { A[a,b] : 0 <= N <= 256 })" },
5675 /* Perform some basic tests of binary operations on
5676 * pairs of isl_multi_union_pw_aff and isl_set objects.
5678 static int test_mupa_set(isl_ctx *ctx)
5680 int i;
5681 isl_bool ok;
5682 isl_multi_union_pw_aff *mupa, *res;
5683 isl_set *set;
5685 for (i = 0; i < ARRAY_SIZE(mupa_set_tests); ++i) {
5686 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5687 mupa_set_tests[i].arg1);
5688 set = isl_set_read_from_str(ctx, mupa_set_tests[i].arg2);
5689 res = isl_multi_union_pw_aff_read_from_str(ctx,
5690 mupa_set_tests[i].res);
5691 mupa = mupa_set_tests[i].fn(mupa, set);
5692 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5693 isl_multi_union_pw_aff_free(mupa);
5694 isl_multi_union_pw_aff_free(res);
5695 if (ok < 0)
5696 return -1;
5697 if (!ok)
5698 isl_die(ctx, isl_error_unknown,
5699 "unexpected result", return -1);
5702 return 0;
5705 /* Inputs for basic tests of binary operations on
5706 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5707 * "fn" is the function that is tested.
5708 * "arg1" and "arg2" are string descriptions of the inputs.
5709 * "res" is a string description of the expected result.
5711 struct {
5712 __isl_give isl_multi_union_pw_aff *(*fn)(
5713 __isl_take isl_multi_union_pw_aff *mupa,
5714 __isl_take isl_union_set *uset);
5715 const char *arg1;
5716 const char *arg2;
5717 const char *res;
5718 } mupa_uset_tests[] = {
5719 { &isl_multi_union_pw_aff_intersect_domain,
5720 "C[{ B[i,j] -> [i + 2j] }]", "{ B[i,i] }",
5721 "C[{ B[i,i] -> [3i] }]" },
5722 { &isl_multi_union_pw_aff_intersect_domain,
5723 "(C[] : { B[i,j] })", "{ B[i,i] }",
5724 "(C[] : { B[i,i] })" },
5725 { &isl_multi_union_pw_aff_intersect_domain,
5726 "(C[] : { B[i,j] })", "[N] -> { B[N,N] }",
5727 "[N] -> (C[] : { B[N,N] })" },
5728 { &isl_multi_union_pw_aff_intersect_domain,
5729 "C[]", "{ B[i,i] }",
5730 "(C[] : { B[i,i] })" },
5731 { &isl_multi_union_pw_aff_intersect_domain,
5732 "[N] -> (C[] : { : N >= 0 })", "{ B[i,i] }",
5733 "[N] -> (C[] : { B[i,i] : N >= 0 })" },
5736 /* Perform some basic tests of binary operations on
5737 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5739 static int test_mupa_uset(isl_ctx *ctx)
5741 int i;
5742 isl_bool ok;
5743 isl_multi_union_pw_aff *mupa, *res;
5744 isl_union_set *uset;
5746 for (i = 0; i < ARRAY_SIZE(mupa_uset_tests); ++i) {
5747 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5748 mupa_uset_tests[i].arg1);
5749 uset = isl_union_set_read_from_str(ctx,
5750 mupa_uset_tests[i].arg2);
5751 res = isl_multi_union_pw_aff_read_from_str(ctx,
5752 mupa_uset_tests[i].res);
5753 mupa = mupa_uset_tests[i].fn(mupa, uset);
5754 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5755 isl_multi_union_pw_aff_free(mupa);
5756 isl_multi_union_pw_aff_free(res);
5757 if (ok < 0)
5758 return -1;
5759 if (!ok)
5760 isl_die(ctx, isl_error_unknown,
5761 "unexpected result", return -1);
5764 return 0;
5767 /* Inputs for basic tests of binary operations on
5768 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5769 * "fn" is the function that is tested.
5770 * "arg1" and "arg2" are string descriptions of the inputs.
5771 * "res" is a string description of the expected result.
5773 struct {
5774 __isl_give isl_multi_union_pw_aff *(*fn)(
5775 __isl_take isl_multi_union_pw_aff *mupa,
5776 __isl_take isl_multi_aff *ma);
5777 const char *arg1;
5778 const char *arg2;
5779 const char *res;
5780 } mupa_ma_tests[] = {
5781 { &isl_multi_union_pw_aff_apply_multi_aff,
5782 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5783 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5784 "{ C[a,b] -> D[b,a] }",
5785 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
5786 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
5787 { &isl_multi_union_pw_aff_apply_multi_aff,
5788 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
5789 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5790 "{ C[a,b] -> D[b,a] }",
5791 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
5792 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
5793 { &isl_multi_union_pw_aff_apply_multi_aff,
5794 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5795 "[N] -> { C[a] -> D[a + N] }",
5796 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }] " },
5797 { &isl_multi_union_pw_aff_apply_multi_aff,
5798 "C[]",
5799 "{ C[] -> D[] }",
5800 "D[]" },
5801 { &isl_multi_union_pw_aff_apply_multi_aff,
5802 "[N] -> (C[] : { : N >= 0 })",
5803 "{ C[] -> D[] }",
5804 "[N] -> (D[] : { : N >= 0 })" },
5805 { &isl_multi_union_pw_aff_apply_multi_aff,
5806 "C[]",
5807 "[N] -> { C[] -> D[N] }",
5808 "[N] -> D[{ [N] }]" },
5809 { &isl_multi_union_pw_aff_apply_multi_aff,
5810 "(C[] : { A[i,j] : i >= j })",
5811 "{ C[] -> D[] }",
5812 "(D[] : { A[i,j] : i >= j })" },
5813 { &isl_multi_union_pw_aff_apply_multi_aff,
5814 "[N] -> (C[] : { A[i,j] : N >= 0 })",
5815 "{ C[] -> D[] }",
5816 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
5817 { &isl_multi_union_pw_aff_apply_multi_aff,
5818 "(C[] : { A[i,j] : i >= j })",
5819 "[N] -> { C[] -> D[N] }",
5820 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
5823 /* Perform some basic tests of binary operations on
5824 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5826 static int test_mupa_ma(isl_ctx *ctx)
5828 int i;
5829 isl_bool ok;
5830 isl_multi_union_pw_aff *mupa, *res;
5831 isl_multi_aff *ma;
5833 for (i = 0; i < ARRAY_SIZE(mupa_ma_tests); ++i) {
5834 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5835 mupa_ma_tests[i].arg1);
5836 ma = isl_multi_aff_read_from_str(ctx, mupa_ma_tests[i].arg2);
5837 res = isl_multi_union_pw_aff_read_from_str(ctx,
5838 mupa_ma_tests[i].res);
5839 mupa = mupa_ma_tests[i].fn(mupa, ma);
5840 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5841 isl_multi_union_pw_aff_free(mupa);
5842 isl_multi_union_pw_aff_free(res);
5843 if (ok < 0)
5844 return -1;
5845 if (!ok)
5846 isl_die(ctx, isl_error_unknown,
5847 "unexpected result", return -1);
5850 return 0;
5853 /* Inputs for basic tests of binary operations on
5854 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5855 * "fn" is the function that is tested.
5856 * "arg1" and "arg2" are string descriptions of the inputs.
5857 * "res" is a string description of the expected result.
5859 struct {
5860 __isl_give isl_union_pw_aff *(*fn)(
5861 __isl_take isl_multi_union_pw_aff *mupa,
5862 __isl_take isl_pw_aff *pa);
5863 const char *arg1;
5864 const char *arg2;
5865 const char *res;
5866 } mupa_pa_tests[] = {
5867 { &isl_multi_union_pw_aff_apply_pw_aff,
5868 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5869 "[N] -> { C[a] -> [a + N] }",
5870 "[N] -> { A[i,j] -> [i + N]; B[i,j] -> [j + N] }" },
5871 { &isl_multi_union_pw_aff_apply_pw_aff,
5872 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5873 "{ C[a] -> [a] : a >= 0; C[a] -> [-a] : a < 0 }",
5874 "{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
5875 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }" },
5876 { &isl_multi_union_pw_aff_apply_pw_aff,
5877 "C[]",
5878 "[N] -> { C[] -> [N] }",
5879 "[N] -> { [N] }" },
5880 { &isl_multi_union_pw_aff_apply_pw_aff,
5881 "C[]",
5882 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
5883 "[N] -> { [N] : N >= 0; [-N] : N < 0 }" },
5884 { &isl_multi_union_pw_aff_apply_pw_aff,
5885 "[N] -> (C[] : { : N >= 0 })",
5886 "[N] -> { C[] -> [N] }",
5887 "[N] -> { [N] : N >= 0 }" },
5888 { &isl_multi_union_pw_aff_apply_pw_aff,
5889 "[N] -> (C[] : { : N >= 0 })",
5890 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
5891 "[N] -> { [N] : N >= 0 }" },
5892 { &isl_multi_union_pw_aff_apply_pw_aff,
5893 "[N] -> (C[] : { : N >= 0 })",
5894 "{ C[] -> [0] }",
5895 "[N] -> { [0] : N >= 0 }" },
5896 { &isl_multi_union_pw_aff_apply_pw_aff,
5897 "(C[] : { A[i,j] : i >= j })",
5898 "[N] -> { C[] -> [N] }",
5899 "[N] -> { A[i,j] -> [N] : i >= j }" },
5900 { &isl_multi_union_pw_aff_apply_pw_aff,
5901 "(C[] : { A[i,j] : i >= j })",
5902 "[N] -> { C[] -> [N] : N >= 0 }",
5903 "[N] -> { A[i,j] -> [N] : i >= j and N >= 0 }" },
5906 /* Perform some basic tests of binary operations on
5907 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5909 static int test_mupa_pa(isl_ctx *ctx)
5911 int i;
5912 isl_bool ok;
5913 isl_multi_union_pw_aff *mupa;
5914 isl_union_pw_aff *upa, *res;
5915 isl_pw_aff *pa;
5917 for (i = 0; i < ARRAY_SIZE(mupa_pa_tests); ++i) {
5918 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5919 mupa_pa_tests[i].arg1);
5920 pa = isl_pw_aff_read_from_str(ctx, mupa_pa_tests[i].arg2);
5921 res = isl_union_pw_aff_read_from_str(ctx,
5922 mupa_pa_tests[i].res);
5923 upa = mupa_pa_tests[i].fn(mupa, pa);
5924 ok = isl_union_pw_aff_plain_is_equal(upa, res);
5925 isl_union_pw_aff_free(upa);
5926 isl_union_pw_aff_free(res);
5927 if (ok < 0)
5928 return -1;
5929 if (!ok)
5930 isl_die(ctx, isl_error_unknown,
5931 "unexpected result", return -1);
5934 return 0;
5937 /* Inputs for basic tests of binary operations on
5938 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
5939 * "fn" is the function that is tested.
5940 * "arg1" and "arg2" are string descriptions of the inputs.
5941 * "res" is a string description of the expected result.
5943 struct {
5944 __isl_give isl_multi_union_pw_aff *(*fn)(
5945 __isl_take isl_multi_union_pw_aff *mupa,
5946 __isl_take isl_pw_multi_aff *pma);
5947 const char *arg1;
5948 const char *arg2;
5949 const char *res;
5950 } mupa_pma_tests[] = {
5951 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5952 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5953 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5954 "{ C[a,b] -> D[b,a] }",
5955 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
5956 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
5957 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5958 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
5959 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5960 "{ C[a,b] -> D[b,a] }",
5961 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
5962 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
5963 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5964 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5965 "[N] -> { C[a] -> D[a + N] }",
5966 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }]" },
5967 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5968 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5969 "{ C[a] -> D[a] : a >= 0; C[a] -> D[-a] : a < 0 }",
5970 "D[{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
5971 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }]" },
5972 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5973 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5974 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5975 "{ C[a,b] -> D[a,b] : a >= b; C[a,b] -> D[b,a] : a < b }",
5976 "D[{ A[i,j] -> [i] : i >= j; A[i,j] -> [j] : i < j; "
5977 "B[i,j] -> [j] : i <= j; B[i,j] -> [i] : i > j }, "
5978 "{ A[i,j] -> [j] : i >= j; A[i,j] -> [i] : i < j; "
5979 "B[i,j] -> [i] : i <= j; B[i,j] -> [j] : i > j }]" },
5980 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5981 "C[]",
5982 "{ C[] -> D[] }",
5983 "D[]" },
5984 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5985 "[N] -> (C[] : { : N >= 0 })",
5986 "{ C[] -> D[] }",
5987 "[N] -> (D[] : { : N >= 0 })" },
5988 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5989 "C[]",
5990 "[N] -> { C[] -> D[N] }",
5991 "[N] -> D[{ [N] }]" },
5992 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5993 "(C[] : { A[i,j] : i >= j })",
5994 "{ C[] -> D[] }",
5995 "(D[] : { A[i,j] : i >= j })" },
5996 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5997 "[N] -> (C[] : { A[i,j] : N >= 0 })",
5998 "{ C[] -> D[] }",
5999 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6000 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6001 "(C[] : { A[i,j] : i >= j })",
6002 "[N] -> { C[] -> D[N] }",
6003 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6004 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6005 "C[]",
6006 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6007 "[N] -> D[{ [N] : N >= 0; [-N] : N < 0 }]" },
6008 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6009 "[N] -> (C[] : { : N >= 0 })",
6010 "[N] -> { C[] -> D[N] }",
6011 "[N] -> D[{ [N] : N >= 0 }]" },
6012 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6013 "[N] -> (C[] : { : N >= 0 })",
6014 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6015 "[N] -> D[{ [N] : N >= 0 }]" },
6016 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6017 "[N] -> (C[] : { : N >= 0 })",
6018 "{ C[] -> D[0] }",
6019 "[N] -> D[{ [0] : N >= 0 }]" },
6020 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6021 "(C[] : { A[i,j] : i >= j })",
6022 "[N] -> { C[] -> D[N] : N >= 0 }",
6023 "[N] -> D[{ A[i,j] -> [N] : i >= j and N >= 0 }]" },
6026 /* Perform some basic tests of binary operations on
6027 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6029 static int test_mupa_pma(isl_ctx *ctx)
6031 int i;
6032 isl_bool ok;
6033 isl_multi_union_pw_aff *mupa, *res;
6034 isl_pw_multi_aff *pma;
6036 for (i = 0; i < ARRAY_SIZE(mupa_pma_tests); ++i) {
6037 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6038 mupa_pma_tests[i].arg1);
6039 pma = isl_pw_multi_aff_read_from_str(ctx,
6040 mupa_pma_tests[i].arg2);
6041 res = isl_multi_union_pw_aff_read_from_str(ctx,
6042 mupa_pma_tests[i].res);
6043 mupa = mupa_pma_tests[i].fn(mupa, pma);
6044 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6045 isl_multi_union_pw_aff_free(mupa);
6046 isl_multi_union_pw_aff_free(res);
6047 if (ok < 0)
6048 return -1;
6049 if (!ok)
6050 isl_die(ctx, isl_error_unknown,
6051 "unexpected result", return -1);
6054 return 0;
6057 /* Inputs for basic tests of binary operations on
6058 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6059 * "fn" is the function that is tested.
6060 * "arg1" and "arg2" are string descriptions of the inputs.
6061 * "res" is a string description of the expected result.
6063 struct {
6064 __isl_give isl_multi_union_pw_aff *(*fn)(
6065 __isl_take isl_multi_union_pw_aff *mupa,
6066 __isl_take isl_union_pw_multi_aff *upma);
6067 const char *arg1;
6068 const char *arg2;
6069 const char *res;
6070 } mupa_upma_tests[] = {
6071 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6072 "C[{ B[i,j] -> [i + 2j] }]", "{ A[a,b] -> B[b,a] }",
6073 "C[{ A[a,b] -> [b + 2a] }]" },
6074 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6075 "C[{ B[i,j] -> [i + 2j] }]",
6076 "{ A[a,b] -> B[b,a] : b > a }",
6077 "C[{ A[a,b] -> [b + 2a] : b > a }]" },
6078 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6079 "C[{ B[i,j] -> [i + 2j] : j > 4 }]",
6080 "{ A[a,b] -> B[b,a] : b > a }",
6081 "C[{ A[a,b] -> [b + 2a] : b > a > 4 }]" },
6082 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6083 "C[{ B[i,j] -> [i + 2j] }]",
6084 "{ A[a,b] -> B[b,a] : a > b; A[a,b] -> B[a,b] : a <= b }",
6085 "C[{ A[a,b] -> [b + 2a] : a > b; A[a,b] -> [a + 2b] : a <= b }]" },
6086 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6087 "(C[] : { B[a,b] })",
6088 "{ A[a,b] -> B[b,a] }",
6089 "(C[] : { A[a,b] })" },
6090 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6091 "(C[] : { B[a,b] })",
6092 "{ B[a,b] -> A[b,a] }",
6093 "(C[] : { })" },
6094 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6095 "(C[] : { B[a,b] })",
6096 "{ A[a,b] -> B[b,a] : a > b }",
6097 "(C[] : { A[a,b] : a > b })" },
6098 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6099 "(C[] : { B[a,b] : a > b })",
6100 "{ A[a,b] -> B[b,a] }",
6101 "(C[] : { A[a,b] : b > a })" },
6102 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6103 "[N] -> (C[] : { B[a,b] : a > N })",
6104 "{ A[a,b] -> B[b,a] : a > b }",
6105 "[N] -> (C[] : { A[a,b] : a > b > N })" },
6106 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6107 "(C[] : { B[a,b] : a > b })",
6108 "[N] -> { A[a,b] -> B[b,a] : a > N }",
6109 "[N] -> (C[] : { A[a,b] : b > a > N })" },
6110 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6111 "C[]",
6112 "{ A[a,b] -> B[b,a] }",
6113 "C[]" },
6114 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6115 "[N] -> (C[] : { : N >= 0 })",
6116 "{ A[a,b] -> B[b,a] }",
6117 "[N] -> (C[] : { : N >= 0 })" },
6118 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6119 "C[]",
6120 "[N] -> { A[a,b] -> B[b,a] : N >= 0 }",
6121 "[N] -> (C[] : { : N >= 0 })" },
6124 /* Perform some basic tests of binary operations on
6125 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6127 static int test_mupa_upma(isl_ctx *ctx)
6129 int i;
6130 isl_bool ok;
6131 isl_multi_union_pw_aff *mupa, *res;
6132 isl_union_pw_multi_aff *upma;
6134 for (i = 0; i < ARRAY_SIZE(mupa_upma_tests); ++i) {
6135 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6136 mupa_upma_tests[i].arg1);
6137 upma = isl_union_pw_multi_aff_read_from_str(ctx,
6138 mupa_upma_tests[i].arg2);
6139 res = isl_multi_union_pw_aff_read_from_str(ctx,
6140 mupa_upma_tests[i].res);
6141 mupa = mupa_upma_tests[i].fn(mupa, upma);
6142 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6143 isl_multi_union_pw_aff_free(mupa);
6144 isl_multi_union_pw_aff_free(res);
6145 if (ok < 0)
6146 return -1;
6147 if (!ok)
6148 isl_die(ctx, isl_error_unknown,
6149 "unexpected result", return -1);
6152 return 0;
6155 /* Check that the input tuple of an isl_aff can be set properly.
6157 static isl_stat test_aff_set_tuple_id(isl_ctx *ctx)
6159 isl_id *id;
6160 isl_aff *aff;
6161 int equal;
6163 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x + 1] }");
6164 id = isl_id_alloc(ctx, "A", NULL);
6165 aff = isl_aff_set_tuple_id(aff, isl_dim_in, id);
6166 equal = aff_check_plain_equal(aff, "{ A[x] -> [x + 1] }");
6167 isl_aff_free(aff);
6168 if (equal < 0)
6169 return isl_stat_error;
6171 return isl_stat_ok;
6174 int test_aff(isl_ctx *ctx)
6176 const char *str;
6177 isl_set *set;
6178 isl_space *space;
6179 isl_local_space *ls;
6180 isl_aff *aff;
6181 int zero, equal;
6183 if (test_upa(ctx) < 0)
6184 return -1;
6185 if (test_bin_aff(ctx) < 0)
6186 return -1;
6187 if (test_bin_pw_aff(ctx) < 0)
6188 return -1;
6189 if (test_bin_upma(ctx) < 0)
6190 return -1;
6191 if (test_bin_upma_fail(ctx) < 0)
6192 return -1;
6193 if (test_un_mpa(ctx) < 0)
6194 return -1;
6195 if (test_bin_mpa(ctx) < 0)
6196 return -1;
6197 if (test_un_mupa(ctx) < 0)
6198 return -1;
6199 if (test_bin_mupa(ctx) < 0)
6200 return -1;
6201 if (test_mupa_set(ctx) < 0)
6202 return -1;
6203 if (test_mupa_uset(ctx) < 0)
6204 return -1;
6205 if (test_mupa_ma(ctx) < 0)
6206 return -1;
6207 if (test_mupa_pa(ctx) < 0)
6208 return -1;
6209 if (test_mupa_pma(ctx) < 0)
6210 return -1;
6211 if (test_mupa_upma(ctx) < 0)
6212 return -1;
6214 space = isl_space_set_alloc(ctx, 0, 1);
6215 ls = isl_local_space_from_space(space);
6216 aff = isl_aff_zero_on_domain(ls);
6218 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6219 aff = isl_aff_scale_down_ui(aff, 3);
6220 aff = isl_aff_floor(aff);
6221 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6222 aff = isl_aff_scale_down_ui(aff, 2);
6223 aff = isl_aff_floor(aff);
6224 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6226 str = "{ [10] }";
6227 set = isl_set_read_from_str(ctx, str);
6228 aff = isl_aff_gist(aff, set);
6230 aff = isl_aff_add_constant_si(aff, -16);
6231 zero = isl_aff_plain_is_zero(aff);
6232 isl_aff_free(aff);
6234 if (zero < 0)
6235 return -1;
6236 if (!zero)
6237 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6239 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
6240 aff = isl_aff_scale_down_ui(aff, 64);
6241 aff = isl_aff_floor(aff);
6242 equal = aff_check_plain_equal(aff, "{ [-1] }");
6243 isl_aff_free(aff);
6244 if (equal < 0)
6245 return -1;
6247 if (test_aff_set_tuple_id(ctx) < 0)
6248 return -1;
6250 return 0;
6253 /* Check that "pa" consists of a single expression.
6255 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
6257 int n;
6259 n = isl_pw_aff_n_piece(pa);
6260 isl_pw_aff_free(pa);
6262 if (!pa)
6263 return -1;
6264 if (n != 1)
6265 isl_die(ctx, isl_error_unknown, "expecting single expression",
6266 return -1);
6268 return 0;
6271 /* Check that the computation below results in a single expression.
6272 * One or two expressions may result depending on which constraint
6273 * ends up being considered as redundant with respect to the other
6274 * constraints after the projection that is performed internally
6275 * by isl_set_dim_min.
6277 static int test_dim_max_1(isl_ctx *ctx)
6279 const char *str;
6280 isl_set *set;
6281 isl_pw_aff *pa;
6283 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
6284 "-4a <= b <= 3 and b < n - 4a }";
6285 set = isl_set_read_from_str(ctx, str);
6286 pa = isl_set_dim_min(set, 0);
6287 return check_single_piece(ctx, pa);
6290 /* Check that the computation below results in a single expression.
6291 * The PIP problem corresponding to these constraints has a row
6292 * that causes a split of the solution domain. The solver should
6293 * first pick rows that split off empty parts such that the actual
6294 * solution domain does not get split.
6295 * Note that the description contains some redundant constraints.
6296 * If these constraints get removed first, then the row mentioned
6297 * above does not appear in the PIP problem.
6299 static int test_dim_max_2(isl_ctx *ctx)
6301 const char *str;
6302 isl_set *set;
6303 isl_pw_aff *pa;
6305 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
6306 "N > 0 and P >= 0 }";
6307 set = isl_set_read_from_str(ctx, str);
6308 pa = isl_set_dim_max(set, 0);
6309 return check_single_piece(ctx, pa);
6312 int test_dim_max(isl_ctx *ctx)
6314 int equal;
6315 const char *str;
6316 isl_set *set1, *set2;
6317 isl_set *set;
6318 isl_map *map;
6319 isl_pw_aff *pwaff;
6321 if (test_dim_max_1(ctx) < 0)
6322 return -1;
6323 if (test_dim_max_2(ctx) < 0)
6324 return -1;
6326 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
6327 set = isl_set_read_from_str(ctx, str);
6328 pwaff = isl_set_dim_max(set, 0);
6329 set1 = isl_set_from_pw_aff(pwaff);
6330 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
6331 set2 = isl_set_read_from_str(ctx, str);
6332 equal = isl_set_is_equal(set1, set2);
6333 isl_set_free(set1);
6334 isl_set_free(set2);
6335 if (equal < 0)
6336 return -1;
6337 if (!equal)
6338 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6340 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
6341 set = isl_set_read_from_str(ctx, str);
6342 pwaff = isl_set_dim_max(set, 0);
6343 set1 = isl_set_from_pw_aff(pwaff);
6344 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6345 set2 = isl_set_read_from_str(ctx, str);
6346 equal = isl_set_is_equal(set1, set2);
6347 isl_set_free(set1);
6348 isl_set_free(set2);
6349 if (equal < 0)
6350 return -1;
6351 if (!equal)
6352 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6354 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
6355 set = isl_set_read_from_str(ctx, str);
6356 pwaff = isl_set_dim_max(set, 0);
6357 set1 = isl_set_from_pw_aff(pwaff);
6358 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6359 set2 = isl_set_read_from_str(ctx, str);
6360 equal = isl_set_is_equal(set1, set2);
6361 isl_set_free(set1);
6362 isl_set_free(set2);
6363 if (equal < 0)
6364 return -1;
6365 if (!equal)
6366 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6368 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
6369 "0 <= i < N and 0 <= j < M }";
6370 map = isl_map_read_from_str(ctx, str);
6371 set = isl_map_range(map);
6373 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
6374 set1 = isl_set_from_pw_aff(pwaff);
6375 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
6376 set2 = isl_set_read_from_str(ctx, str);
6377 equal = isl_set_is_equal(set1, set2);
6378 isl_set_free(set1);
6379 isl_set_free(set2);
6381 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
6382 set1 = isl_set_from_pw_aff(pwaff);
6383 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
6384 set2 = isl_set_read_from_str(ctx, str);
6385 if (equal >= 0 && equal)
6386 equal = isl_set_is_equal(set1, set2);
6387 isl_set_free(set1);
6388 isl_set_free(set2);
6390 isl_set_free(set);
6392 if (equal < 0)
6393 return -1;
6394 if (!equal)
6395 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6397 /* Check that solutions are properly merged. */
6398 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
6399 "c <= -1 + n - 4a - 2b and c >= -2b and "
6400 "4a >= -4 + n and c >= 0 }";
6401 set = isl_set_read_from_str(ctx, str);
6402 pwaff = isl_set_dim_min(set, 2);
6403 set1 = isl_set_from_pw_aff(pwaff);
6404 str = "[n] -> { [(0)] : n >= 1 }";
6405 set2 = isl_set_read_from_str(ctx, str);
6406 equal = isl_set_is_equal(set1, set2);
6407 isl_set_free(set1);
6408 isl_set_free(set2);
6410 if (equal < 0)
6411 return -1;
6412 if (!equal)
6413 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6415 /* Check that empty solution lie in the right space. */
6416 str = "[n] -> { [t,a] : 1 = 0 }";
6417 set = isl_set_read_from_str(ctx, str);
6418 pwaff = isl_set_dim_max(set, 0);
6419 set1 = isl_set_from_pw_aff(pwaff);
6420 str = "[n] -> { [t] : 1 = 0 }";
6421 set2 = isl_set_read_from_str(ctx, str);
6422 equal = isl_set_is_equal(set1, set2);
6423 isl_set_free(set1);
6424 isl_set_free(set2);
6426 if (equal < 0)
6427 return -1;
6428 if (!equal)
6429 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6431 return 0;
6434 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
6436 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
6437 const char *str)
6439 isl_ctx *ctx;
6440 isl_pw_multi_aff *pma2;
6441 int equal;
6443 if (!pma)
6444 return -1;
6446 ctx = isl_pw_multi_aff_get_ctx(pma);
6447 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6448 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
6449 isl_pw_multi_aff_free(pma2);
6451 return equal;
6454 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
6455 * represented by "str".
6457 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
6458 const char *str)
6460 int equal;
6462 equal = pw_multi_aff_plain_is_equal(pma, str);
6463 if (equal < 0)
6464 return -1;
6465 if (!equal)
6466 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
6467 "result not as expected", return -1);
6468 return 0;
6471 /* Basic test for isl_pw_multi_aff_product.
6473 * Check that multiple pieces are properly handled.
6475 static int test_product_pma(isl_ctx *ctx)
6477 int equal;
6478 const char *str;
6479 isl_pw_multi_aff *pma1, *pma2;
6481 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
6482 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6483 str = "{ C[] -> D[] }";
6484 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6485 pma1 = isl_pw_multi_aff_product(pma1, pma2);
6486 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
6487 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
6488 equal = pw_multi_aff_check_plain_equal(pma1, str);
6489 isl_pw_multi_aff_free(pma1);
6490 if (equal < 0)
6491 return -1;
6493 return 0;
6496 int test_product(isl_ctx *ctx)
6498 const char *str;
6499 isl_set *set;
6500 isl_union_set *uset1, *uset2;
6501 int ok;
6503 str = "{ A[i] }";
6504 set = isl_set_read_from_str(ctx, str);
6505 set = isl_set_product(set, isl_set_copy(set));
6506 ok = isl_set_is_wrapping(set);
6507 isl_set_free(set);
6508 if (ok < 0)
6509 return -1;
6510 if (!ok)
6511 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6513 str = "{ [] }";
6514 uset1 = isl_union_set_read_from_str(ctx, str);
6515 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
6516 str = "{ [[] -> []] }";
6517 uset2 = isl_union_set_read_from_str(ctx, str);
6518 ok = isl_union_set_is_equal(uset1, uset2);
6519 isl_union_set_free(uset1);
6520 isl_union_set_free(uset2);
6521 if (ok < 0)
6522 return -1;
6523 if (!ok)
6524 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6526 if (test_product_pma(ctx) < 0)
6527 return -1;
6529 return 0;
6532 /* Check that two sets are not considered disjoint just because
6533 * they have a different set of (named) parameters.
6535 static int test_disjoint(isl_ctx *ctx)
6537 const char *str;
6538 isl_set *set, *set2;
6539 int disjoint;
6541 str = "[n] -> { [[]->[]] }";
6542 set = isl_set_read_from_str(ctx, str);
6543 str = "{ [[]->[]] }";
6544 set2 = isl_set_read_from_str(ctx, str);
6545 disjoint = isl_set_is_disjoint(set, set2);
6546 isl_set_free(set);
6547 isl_set_free(set2);
6548 if (disjoint < 0)
6549 return -1;
6550 if (disjoint)
6551 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6553 return 0;
6556 /* Inputs for isl_pw_multi_aff_is_equal tests.
6557 * "f1" and "f2" are the two function that need to be compared.
6558 * "equal" is the expected result.
6560 struct {
6561 int equal;
6562 const char *f1;
6563 const char *f2;
6564 } pma_equal_tests[] = {
6565 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
6566 "[N] -> { [0] : 0 <= N <= 1 }" },
6567 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6568 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
6569 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6570 "[N] -> { [0] : 0 <= N <= 1 }" },
6571 { 0, "{ [NaN] }", "{ [NaN] }" },
6574 int test_equal(isl_ctx *ctx)
6576 int i;
6577 const char *str;
6578 isl_set *set, *set2;
6579 int equal;
6581 str = "{ S_6[i] }";
6582 set = isl_set_read_from_str(ctx, str);
6583 str = "{ S_7[i] }";
6584 set2 = isl_set_read_from_str(ctx, str);
6585 equal = isl_set_is_equal(set, set2);
6586 isl_set_free(set);
6587 isl_set_free(set2);
6588 if (equal < 0)
6589 return -1;
6590 if (equal)
6591 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6593 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
6594 int expected = pma_equal_tests[i].equal;
6595 isl_pw_multi_aff *f1, *f2;
6597 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
6598 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
6599 equal = isl_pw_multi_aff_is_equal(f1, f2);
6600 isl_pw_multi_aff_free(f1);
6601 isl_pw_multi_aff_free(f2);
6602 if (equal < 0)
6603 return -1;
6604 if (equal != expected)
6605 isl_die(ctx, isl_error_unknown,
6606 "unexpected equality result", return -1);
6609 return 0;
6612 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
6613 enum isl_dim_type type, unsigned pos, int fixed)
6615 isl_bool test;
6617 test = isl_map_plain_is_fixed(map, type, pos, NULL);
6618 isl_map_free(map);
6619 if (test < 0)
6620 return -1;
6621 if (test == fixed)
6622 return 0;
6623 if (fixed)
6624 isl_die(ctx, isl_error_unknown,
6625 "map not detected as fixed", return -1);
6626 else
6627 isl_die(ctx, isl_error_unknown,
6628 "map detected as fixed", return -1);
6631 int test_fixed(isl_ctx *ctx)
6633 const char *str;
6634 isl_map *map;
6636 str = "{ [i] -> [i] }";
6637 map = isl_map_read_from_str(ctx, str);
6638 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
6639 return -1;
6640 str = "{ [i] -> [1] }";
6641 map = isl_map_read_from_str(ctx, str);
6642 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6643 return -1;
6644 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
6645 map = isl_map_read_from_str(ctx, str);
6646 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6647 return -1;
6648 map = isl_map_read_from_str(ctx, str);
6649 map = isl_map_neg(map);
6650 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6651 return -1;
6653 return 0;
6656 struct isl_vertices_test_data {
6657 const char *set;
6658 int n;
6659 const char *vertex[6];
6660 } vertices_tests[] = {
6661 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
6662 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
6663 { "{ A[t, i] : t = 14 and i = 1 }",
6664 1, { "{ A[14, 1] }" } },
6665 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
6666 "c <= m and m <= n and m > 0 }",
6667 6, {
6668 "[n, m] -> { [n, m, m] : 0 < m <= n }",
6669 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
6670 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
6671 "[n, m] -> { [m, m, m] : 0 < m <= n }",
6672 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
6673 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
6674 } },
6677 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
6679 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
6681 struct isl_vertices_test_data *data = user;
6682 isl_ctx *ctx;
6683 isl_multi_aff *ma;
6684 isl_basic_set *bset;
6685 isl_pw_multi_aff *pma;
6686 int i;
6687 isl_bool equal;
6689 ctx = isl_vertex_get_ctx(vertex);
6690 bset = isl_vertex_get_domain(vertex);
6691 ma = isl_vertex_get_expr(vertex);
6692 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
6694 for (i = 0; i < data->n; ++i) {
6695 isl_pw_multi_aff *pma_i;
6697 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
6698 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
6699 isl_pw_multi_aff_free(pma_i);
6701 if (equal < 0 || equal)
6702 break;
6705 isl_pw_multi_aff_free(pma);
6706 isl_vertex_free(vertex);
6708 if (equal < 0)
6709 return isl_stat_error;
6711 return equal ? isl_stat_ok : isl_stat_error;
6714 int test_vertices(isl_ctx *ctx)
6716 int i;
6718 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
6719 isl_basic_set *bset;
6720 isl_vertices *vertices;
6721 int ok = 1;
6722 int n;
6724 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
6725 vertices = isl_basic_set_compute_vertices(bset);
6726 n = isl_vertices_get_n_vertices(vertices);
6727 if (vertices_tests[i].n != n)
6728 ok = 0;
6729 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
6730 &vertices_tests[i]) < 0)
6731 ok = 0;
6732 isl_vertices_free(vertices);
6733 isl_basic_set_free(bset);
6735 if (!vertices)
6736 return -1;
6737 if (!ok)
6738 isl_die(ctx, isl_error_unknown, "unexpected vertices",
6739 return -1);
6742 return 0;
6745 int test_union_pw(isl_ctx *ctx)
6747 int equal;
6748 const char *str;
6749 isl_union_set *uset;
6750 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
6752 str = "{ [x] -> x^2 }";
6753 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
6754 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
6755 uset = isl_union_pw_qpolynomial_domain(upwqp1);
6756 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
6757 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
6758 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
6759 isl_union_pw_qpolynomial_free(upwqp1);
6760 isl_union_pw_qpolynomial_free(upwqp2);
6761 if (equal < 0)
6762 return -1;
6763 if (!equal)
6764 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6766 return 0;
6769 /* Inputs for basic tests of functions that select
6770 * subparts of the domain of an isl_multi_union_pw_aff.
6771 * "fn" is the function that is tested.
6772 * "arg" is a string description of the input.
6773 * "res" is a string description of the expected result.
6775 struct {
6776 __isl_give isl_union_set *(*fn)(
6777 __isl_take isl_multi_union_pw_aff *mupa);
6778 const char *arg;
6779 const char *res;
6780 } un_locus_tests[] = {
6781 { &isl_multi_union_pw_aff_zero_union_set,
6782 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6783 "{ A[0,j]; B[0,j] }" },
6784 { &isl_multi_union_pw_aff_zero_union_set,
6785 "F[{ A[i,j] -> [i-j]; B[i,j] -> [i-j] : i >= 0 }]",
6786 "{ A[i,i]; B[i,i] : i >= 0 }" },
6787 { &isl_multi_union_pw_aff_zero_union_set,
6788 "(F[] : { A[i,j]; B[i,i] : i >= 0 })",
6789 "{ A[i,j]; B[i,i] : i >= 0 }" },
6792 /* Perform some basic tests of functions that select
6793 * subparts of the domain of an isl_multi_union_pw_aff.
6795 static int test_un_locus(isl_ctx *ctx)
6797 int i;
6798 isl_bool ok;
6799 isl_union_set *uset, *res;
6800 isl_multi_union_pw_aff *mupa;
6802 for (i = 0; i < ARRAY_SIZE(un_locus_tests); ++i) {
6803 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6804 un_locus_tests[i].arg);
6805 res = isl_union_set_read_from_str(ctx, un_locus_tests[i].res);
6806 uset = un_locus_tests[i].fn(mupa);
6807 ok = isl_union_set_is_equal(uset, res);
6808 isl_union_set_free(uset);
6809 isl_union_set_free(res);
6810 if (ok < 0)
6811 return -1;
6812 if (!ok)
6813 isl_die(ctx, isl_error_unknown,
6814 "unexpected result", return -1);
6817 return 0;
6820 /* Inputs for basic tests of functions that select
6821 * subparts of an isl_union_map based on a relation
6822 * specified by an isl_multi_union_pw_aff.
6823 * "fn" is the function that is tested.
6824 * "arg1" and "arg2" are string descriptions of the inputs.
6825 * "res" is a string description of the expected result.
6827 struct {
6828 __isl_give isl_union_map *(*fn)(
6829 __isl_take isl_union_map *umap,
6830 __isl_take isl_multi_union_pw_aff *mupa);
6831 const char *arg1;
6832 const char *arg2;
6833 const char *res;
6834 } bin_locus_tests[] = {
6835 { &isl_union_map_eq_at_multi_union_pw_aff,
6836 "{ A[i,j] -> B[i',j'] }",
6837 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6838 "{ A[i,j] -> B[i,j'] }" },
6839 { &isl_union_map_eq_at_multi_union_pw_aff,
6840 "{ A[i,j] -> B[i',j'] }",
6841 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6842 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6843 "{ A[i,j] -> B[i,j] }" },
6844 { &isl_union_map_eq_at_multi_union_pw_aff,
6845 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6846 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6847 "{ A[i,j] -> B[i,j'] }" },
6848 { &isl_union_map_eq_at_multi_union_pw_aff,
6849 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6850 "F[{ A[i,j] -> [i]; B[i,j] -> [i]; C[i,j] -> [0] }]",
6851 "{ A[i,j] -> B[i,j']; A[0,j] -> C[i',j'] }" },
6852 { &isl_union_map_eq_at_multi_union_pw_aff,
6853 "{ A[i,j] -> B[i',j'] }",
6854 "F[{ A[i,j] -> [i] : i > j; B[i,j] -> [i] }]",
6855 "{ A[i,j] -> B[i,j'] : i > j }" },
6856 { &isl_union_map_lex_lt_at_multi_union_pw_aff,
6857 "{ A[i,j] -> B[i',j'] }",
6858 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6859 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6860 "{ A[i,j] -> B[i',j'] : i,j << i',j' }" },
6861 { &isl_union_map_lex_gt_at_multi_union_pw_aff,
6862 "{ A[i,j] -> B[i',j'] }",
6863 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6864 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6865 "{ A[i,j] -> B[i',j'] : i,j >> i',j' }" },
6866 { &isl_union_map_eq_at_multi_union_pw_aff,
6867 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6868 "(F[] : { A[i,j]; B[i,j] })",
6869 "{ A[i,j] -> B[i',j'] }" },
6870 { &isl_union_map_eq_at_multi_union_pw_aff,
6871 "{ A[i,j] -> B[i',j'] }",
6872 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
6873 "{ A[i,j] -> B[i',j'] : i > j and i' < j' }" },
6874 { &isl_union_map_eq_at_multi_union_pw_aff,
6875 "[N] -> { A[i,j] -> B[i',j'] : i,i' <= N }",
6876 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
6877 "[N] -> { A[i,j] -> B[i',j'] : i > j and i' < j' and i,i' <= N }" },
6878 { &isl_union_map_eq_at_multi_union_pw_aff,
6879 "{ A[i,j] -> B[i',j'] }",
6880 "[N] -> (F[] : { A[i,j] : i < N; B[i,j] : i < N })",
6881 "[N] -> { A[i,j] -> B[i',j'] : i,i' < N }" },
6882 { &isl_union_map_eq_at_multi_union_pw_aff,
6883 "{ A[i,j] -> B[i',j'] }",
6884 "[N] -> (F[] : { : N >= 0 })",
6885 "[N] -> { A[i,j] -> B[i',j'] : N >= 0 }" },
6888 /* Perform some basic tests of functions that select
6889 * subparts of an isl_union_map based on a relation
6890 * specified by an isl_multi_union_pw_aff.
6892 static int test_bin_locus(isl_ctx *ctx)
6894 int i;
6895 isl_bool ok;
6896 isl_union_map *umap, *res;
6897 isl_multi_union_pw_aff *mupa;
6899 for (i = 0; i < ARRAY_SIZE(bin_locus_tests); ++i) {
6900 umap = isl_union_map_read_from_str(ctx,
6901 bin_locus_tests[i].arg1);
6902 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6903 bin_locus_tests[i].arg2);
6904 res = isl_union_map_read_from_str(ctx, bin_locus_tests[i].res);
6905 umap = bin_locus_tests[i].fn(umap, mupa);
6906 ok = isl_union_map_is_equal(umap, res);
6907 isl_union_map_free(umap);
6908 isl_union_map_free(res);
6909 if (ok < 0)
6910 return -1;
6911 if (!ok)
6912 isl_die(ctx, isl_error_unknown,
6913 "unexpected result", return -1);
6916 return 0;
6919 /* Perform basic locus tests.
6921 static int test_locus(isl_ctx *ctx)
6923 if (test_un_locus(ctx) < 0)
6924 return -1;
6925 if (test_bin_locus(ctx) < 0)
6926 return -1;
6927 return 0;
6930 /* Test that isl_union_pw_qpolynomial_eval picks up the function
6931 * defined over the correct domain space.
6933 static int test_eval_1(isl_ctx *ctx)
6935 const char *str;
6936 isl_point *pnt;
6937 isl_set *set;
6938 isl_union_pw_qpolynomial *upwqp;
6939 isl_val *v;
6940 int cmp;
6942 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
6943 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
6944 str = "{ A[6] }";
6945 set = isl_set_read_from_str(ctx, str);
6946 pnt = isl_set_sample_point(set);
6947 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
6948 cmp = isl_val_cmp_si(v, 36);
6949 isl_val_free(v);
6951 if (!v)
6952 return -1;
6953 if (cmp != 0)
6954 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
6956 return 0;
6959 /* Check that isl_qpolynomial_eval handles getting called on a void point.
6961 static int test_eval_2(isl_ctx *ctx)
6963 const char *str;
6964 isl_point *pnt;
6965 isl_set *set;
6966 isl_qpolynomial *qp;
6967 isl_val *v;
6968 isl_bool ok;
6970 str = "{ A[x] -> [x] }";
6971 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
6972 str = "{ A[x] : false }";
6973 set = isl_set_read_from_str(ctx, str);
6974 pnt = isl_set_sample_point(set);
6975 v = isl_qpolynomial_eval(qp, pnt);
6976 ok = isl_val_is_nan(v);
6977 isl_val_free(v);
6979 if (ok < 0)
6980 return -1;
6981 if (!ok)
6982 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
6984 return 0;
6987 /* Inputs for isl_pw_aff_eval test.
6988 * "f" is the affine function.
6989 * "p" is the point where the function should be evaluated.
6990 * "res" is the expected result.
6992 struct {
6993 const char *f;
6994 const char *p;
6995 const char *res;
6996 } aff_eval_tests[] = {
6997 { "{ [i] -> [2 * i] }", "{ [4] }", "8" },
6998 { "{ [i] -> [2 * i] }", "{ [x] : false }", "NaN" },
6999 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [0] }", "0" },
7000 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [1] }", "1" },
7001 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [2] }", "3" },
7002 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [3] }", "5" },
7003 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [4] }", "7" },
7004 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [0] }", "0" },
7005 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [1] }", "0" },
7006 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [2] }", "0" },
7007 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [3] }", "0" },
7008 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [4] }", "1" },
7009 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [6] }", "1" },
7010 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [8] }", "2" },
7011 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [4] }", "4" },
7012 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [-2] }", "2" },
7013 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [0] }", "NaN" },
7014 { "[N] -> { [2 * N] }", "[N] -> { : N = 4 }", "8" },
7015 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 1] }", "1" },
7016 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 2] }", "3/2" },
7017 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [4] }", "4" },
7018 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [3] }", "NaN" },
7019 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [x] : false }", "NaN" },
7022 /* Perform basic isl_pw_aff_eval tests.
7024 static int test_eval_aff(isl_ctx *ctx)
7026 int i;
7028 for (i = 0; i < ARRAY_SIZE(aff_eval_tests); ++i) {
7029 isl_stat r;
7030 isl_pw_aff *pa;
7031 isl_set *set;
7032 isl_point *pnt;
7033 isl_val *v;
7035 pa = isl_pw_aff_read_from_str(ctx, aff_eval_tests[i].f);
7036 set = isl_set_read_from_str(ctx, aff_eval_tests[i].p);
7037 pnt = isl_set_sample_point(set);
7038 v = isl_pw_aff_eval(pa, pnt);
7039 r = val_check_equal(v, aff_eval_tests[i].res);
7040 isl_val_free(v);
7041 if (r < 0)
7042 return -1;
7044 return 0;
7047 /* Perform basic evaluation tests.
7049 static int test_eval(isl_ctx *ctx)
7051 if (test_eval_1(ctx) < 0)
7052 return -1;
7053 if (test_eval_2(ctx) < 0)
7054 return -1;
7055 if (test_eval_aff(ctx) < 0)
7056 return -1;
7057 return 0;
7060 /* Descriptions of sets that are tested for reparsing after printing.
7062 const char *output_tests[] = {
7063 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
7064 "{ [x] : 1 = 0 }",
7065 "{ [x] : false }",
7066 "{ [x] : x mod 2 = 0 }",
7067 "{ [x] : x mod 2 = 1 }",
7068 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
7069 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
7070 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7071 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7072 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
7073 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
7076 /* Check that printing a set and reparsing a set from the printed output
7077 * results in the same set.
7079 static int test_output_set(isl_ctx *ctx)
7081 int i;
7082 char *str;
7083 isl_set *set1, *set2;
7084 isl_bool equal;
7086 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
7087 set1 = isl_set_read_from_str(ctx, output_tests[i]);
7088 str = isl_set_to_str(set1);
7089 set2 = isl_set_read_from_str(ctx, str);
7090 free(str);
7091 equal = isl_set_is_equal(set1, set2);
7092 isl_set_free(set1);
7093 isl_set_free(set2);
7094 if (equal < 0)
7095 return -1;
7096 if (!equal)
7097 isl_die(ctx, isl_error_unknown,
7098 "parsed output not the same", return -1);
7101 return 0;
7104 /* Check that an isl_multi_aff is printed using a consistent space.
7106 static isl_stat test_output_ma(isl_ctx *ctx)
7108 char *str;
7109 isl_bool equal;
7110 isl_aff *aff;
7111 isl_multi_aff *ma, *ma2;
7113 ma = isl_multi_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7114 aff = isl_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7115 ma = isl_multi_aff_set_aff(ma, 0, aff);
7116 str = isl_multi_aff_to_str(ma);
7117 ma2 = isl_multi_aff_read_from_str(ctx, str);
7118 free(str);
7119 equal = isl_multi_aff_plain_is_equal(ma, ma2);
7120 isl_multi_aff_free(ma2);
7121 isl_multi_aff_free(ma);
7123 if (equal < 0)
7124 return isl_stat_error;
7125 if (!equal)
7126 isl_die(ctx, isl_error_unknown, "bad conversion",
7127 return isl_stat_error);
7129 return isl_stat_ok;
7132 /* Check that an isl_multi_pw_aff is printed using a consistent space.
7134 static isl_stat test_output_mpa(isl_ctx *ctx)
7136 char *str;
7137 isl_bool equal;
7138 isl_pw_aff *pa;
7139 isl_multi_pw_aff *mpa, *mpa2;
7141 mpa = isl_multi_pw_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7142 pa = isl_pw_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7143 mpa = isl_multi_pw_aff_set_pw_aff(mpa, 0, pa);
7144 str = isl_multi_pw_aff_to_str(mpa);
7145 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
7146 free(str);
7147 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
7148 isl_multi_pw_aff_free(mpa2);
7149 isl_multi_pw_aff_free(mpa);
7151 if (equal < 0)
7152 return isl_stat_error;
7153 if (!equal)
7154 isl_die(ctx, isl_error_unknown, "bad conversion",
7155 return isl_stat_error);
7157 return isl_stat_ok;
7160 int test_output(isl_ctx *ctx)
7162 char *s;
7163 const char *str;
7164 isl_pw_aff *pa;
7165 isl_printer *p;
7166 int equal;
7168 if (test_output_set(ctx) < 0)
7169 return -1;
7170 if (test_output_ma(ctx) < 0)
7171 return -1;
7172 if (test_output_mpa(ctx) < 0)
7173 return -1;
7175 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
7176 pa = isl_pw_aff_read_from_str(ctx, str);
7178 p = isl_printer_to_str(ctx);
7179 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
7180 p = isl_printer_print_pw_aff(p, pa);
7181 s = isl_printer_get_str(p);
7182 isl_printer_free(p);
7183 isl_pw_aff_free(pa);
7184 if (!s)
7185 equal = -1;
7186 else
7187 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
7188 free(s);
7189 if (equal < 0)
7190 return -1;
7191 if (!equal)
7192 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7194 return 0;
7197 int test_sample(isl_ctx *ctx)
7199 const char *str;
7200 isl_basic_set *bset1, *bset2;
7201 int empty, subset;
7203 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
7204 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
7205 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
7206 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
7207 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
7208 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
7209 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
7210 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
7211 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
7212 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
7213 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
7214 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
7215 "d - 1073741821e and "
7216 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
7217 "3j >= 1 - a + b + 2e and "
7218 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
7219 "3i <= 4 - a + 4b - e and "
7220 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
7221 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
7222 "c <= -1 + a and 3i >= -2 - a + 3e and "
7223 "1073741822e <= 1073741823 - a + 1073741822b + c and "
7224 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
7225 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
7226 "1073741823e >= 1 + 1073741823b - d and "
7227 "3i >= 1073741823b + c - 1073741823e - f and "
7228 "3i >= 1 + 2b + e + 3g }";
7229 bset1 = isl_basic_set_read_from_str(ctx, str);
7230 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
7231 empty = isl_basic_set_is_empty(bset2);
7232 subset = isl_basic_set_is_subset(bset2, bset1);
7233 isl_basic_set_free(bset1);
7234 isl_basic_set_free(bset2);
7235 if (empty < 0 || subset < 0)
7236 return -1;
7237 if (empty)
7238 isl_die(ctx, isl_error_unknown, "point not found", return -1);
7239 if (!subset)
7240 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
7242 return 0;
7245 int test_fixed_power(isl_ctx *ctx)
7247 const char *str;
7248 isl_map *map;
7249 isl_val *exp;
7250 int equal;
7252 str = "{ [i] -> [i + 1] }";
7253 map = isl_map_read_from_str(ctx, str);
7254 exp = isl_val_int_from_si(ctx, 23);
7255 map = isl_map_fixed_power_val(map, exp);
7256 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
7257 isl_map_free(map);
7258 if (equal < 0)
7259 return -1;
7261 return 0;
7264 int test_slice(isl_ctx *ctx)
7266 const char *str;
7267 isl_map *map;
7268 int equal;
7270 str = "{ [i] -> [j] }";
7271 map = isl_map_read_from_str(ctx, str);
7272 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
7273 equal = map_check_equal(map, "{ [i] -> [i] }");
7274 isl_map_free(map);
7275 if (equal < 0)
7276 return -1;
7278 str = "{ [i] -> [j] }";
7279 map = isl_map_read_from_str(ctx, str);
7280 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
7281 equal = map_check_equal(map, "{ [i] -> [j] }");
7282 isl_map_free(map);
7283 if (equal < 0)
7284 return -1;
7286 str = "{ [i] -> [j] }";
7287 map = isl_map_read_from_str(ctx, str);
7288 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
7289 equal = map_check_equal(map, "{ [i] -> [-i] }");
7290 isl_map_free(map);
7291 if (equal < 0)
7292 return -1;
7294 str = "{ [i] -> [j] }";
7295 map = isl_map_read_from_str(ctx, str);
7296 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
7297 equal = map_check_equal(map, "{ [0] -> [j] }");
7298 isl_map_free(map);
7299 if (equal < 0)
7300 return -1;
7302 str = "{ [i] -> [j] }";
7303 map = isl_map_read_from_str(ctx, str);
7304 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
7305 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
7306 isl_map_free(map);
7307 if (equal < 0)
7308 return -1;
7310 str = "{ [i] -> [j] }";
7311 map = isl_map_read_from_str(ctx, str);
7312 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
7313 equal = map_check_equal(map, "{ [i] -> [j] : false }");
7314 isl_map_free(map);
7315 if (equal < 0)
7316 return -1;
7318 return 0;
7321 int test_eliminate(isl_ctx *ctx)
7323 const char *str;
7324 isl_map *map;
7325 int equal;
7327 str = "{ [i] -> [j] : i = 2j }";
7328 map = isl_map_read_from_str(ctx, str);
7329 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
7330 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
7331 isl_map_free(map);
7332 if (equal < 0)
7333 return -1;
7335 return 0;
7338 /* Check that isl_set_dim_residue_class detects that the values of j
7339 * in the set below are all odd and that it does not detect any spurious
7340 * strides.
7342 static int test_residue_class(isl_ctx *ctx)
7344 const char *str;
7345 isl_set *set;
7346 isl_int m, r;
7347 isl_stat res;
7349 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
7350 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
7351 set = isl_set_read_from_str(ctx, str);
7352 isl_int_init(m);
7353 isl_int_init(r);
7354 res = isl_set_dim_residue_class(set, 1, &m, &r);
7355 if (res >= 0 &&
7356 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
7357 isl_die(ctx, isl_error_unknown, "incorrect residue class",
7358 res = isl_stat_error);
7359 isl_int_clear(r);
7360 isl_int_clear(m);
7361 isl_set_free(set);
7363 return res;
7366 static int test_align_parameters_1(isl_ctx *ctx)
7368 const char *str;
7369 isl_space *space;
7370 isl_multi_aff *ma1, *ma2;
7371 int equal;
7373 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
7374 ma1 = isl_multi_aff_read_from_str(ctx, str);
7376 space = isl_space_params_alloc(ctx, 1);
7377 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
7378 ma1 = isl_multi_aff_align_params(ma1, space);
7380 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
7381 ma2 = isl_multi_aff_read_from_str(ctx, str);
7383 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
7385 isl_multi_aff_free(ma1);
7386 isl_multi_aff_free(ma2);
7388 if (equal < 0)
7389 return -1;
7390 if (!equal)
7391 isl_die(ctx, isl_error_unknown,
7392 "result not as expected", return -1);
7394 return 0;
7397 /* Check the isl_multi_*_from_*_list operation in case inputs
7398 * have unaligned parameters.
7399 * In particular, older versions of isl would simply fail
7400 * (without printing any error message).
7402 static isl_stat test_align_parameters_2(isl_ctx *ctx)
7404 isl_space *space;
7405 isl_map *map;
7406 isl_aff *aff;
7407 isl_multi_aff *ma;
7409 map = isl_map_read_from_str(ctx, "{ A[] -> M[x] }");
7410 space = isl_map_get_space(map);
7411 isl_map_free(map);
7413 aff = isl_aff_read_from_str(ctx, "[N] -> { A[] -> [N] }");
7414 ma = isl_multi_aff_from_aff_list(space, isl_aff_list_from_aff(aff));
7415 isl_multi_aff_free(ma);
7417 if (!ma)
7418 return isl_stat_error;
7419 return isl_stat_ok;
7422 /* Perform basic parameter alignment tests.
7424 static int test_align_parameters(isl_ctx *ctx)
7426 if (test_align_parameters_1(ctx) < 0)
7427 return -1;
7428 if (test_align_parameters_2(ctx) < 0)
7429 return -1;
7431 return 0;
7434 /* Check that isl_*_drop_unused_params actually drops the unused parameters
7435 * by comparing the result using isl_*_plain_is_equal.
7436 * Note that this assumes that isl_*_plain_is_equal does not consider
7437 * objects that only differ by unused parameters to be equal.
7439 int test_drop_unused_parameters(isl_ctx *ctx)
7441 const char *str_with, *str_without;
7442 isl_basic_set *bset1, *bset2;
7443 isl_set *set1, *set2;
7444 isl_pw_aff *pwa1, *pwa2;
7445 int equal;
7447 str_with = "[n, m, o] -> { [m] }";
7448 str_without = "[m] -> { [m] }";
7450 bset1 = isl_basic_set_read_from_str(ctx, str_with);
7451 bset2 = isl_basic_set_read_from_str(ctx, str_without);
7452 bset1 = isl_basic_set_drop_unused_params(bset1);
7453 equal = isl_basic_set_plain_is_equal(bset1, bset2);
7454 isl_basic_set_free(bset1);
7455 isl_basic_set_free(bset2);
7457 if (equal < 0)
7458 return -1;
7459 if (!equal)
7460 isl_die(ctx, isl_error_unknown,
7461 "result not as expected", return -1);
7463 set1 = isl_set_read_from_str(ctx, str_with);
7464 set2 = isl_set_read_from_str(ctx, str_without);
7465 set1 = isl_set_drop_unused_params(set1);
7466 equal = isl_set_plain_is_equal(set1, set2);
7467 isl_set_free(set1);
7468 isl_set_free(set2);
7470 if (equal < 0)
7471 return -1;
7472 if (!equal)
7473 isl_die(ctx, isl_error_unknown,
7474 "result not as expected", return -1);
7476 pwa1 = isl_pw_aff_read_from_str(ctx, str_with);
7477 pwa2 = isl_pw_aff_read_from_str(ctx, str_without);
7478 pwa1 = isl_pw_aff_drop_unused_params(pwa1);
7479 equal = isl_pw_aff_plain_is_equal(pwa1, pwa2);
7480 isl_pw_aff_free(pwa1);
7481 isl_pw_aff_free(pwa2);
7483 if (equal < 0)
7484 return -1;
7485 if (!equal)
7486 isl_die(ctx, isl_error_unknown,
7487 "result not as expected", return -1);
7489 return 0;
7492 static int test_list(isl_ctx *ctx)
7494 isl_id *a, *b, *c, *d, *id;
7495 isl_id_list *list;
7496 int ok;
7498 a = isl_id_alloc(ctx, "a", NULL);
7499 b = isl_id_alloc(ctx, "b", NULL);
7500 c = isl_id_alloc(ctx, "c", NULL);
7501 d = isl_id_alloc(ctx, "d", NULL);
7503 list = isl_id_list_alloc(ctx, 4);
7504 list = isl_id_list_add(list, b);
7505 list = isl_id_list_insert(list, 0, a);
7506 list = isl_id_list_add(list, c);
7507 list = isl_id_list_add(list, d);
7508 list = isl_id_list_drop(list, 1, 1);
7510 if (!list)
7511 return -1;
7512 if (isl_id_list_n_id(list) != 3) {
7513 isl_id_list_free(list);
7514 isl_die(ctx, isl_error_unknown,
7515 "unexpected number of elements in list", return -1);
7518 id = isl_id_list_get_id(list, 0);
7519 ok = id == a;
7520 isl_id_free(id);
7521 id = isl_id_list_get_id(list, 1);
7522 ok = ok && id == c;
7523 isl_id_free(id);
7524 id = isl_id_list_get_id(list, 2);
7525 ok = ok && id == d;
7526 isl_id_free(id);
7528 isl_id_list_free(list);
7530 if (!ok)
7531 isl_die(ctx, isl_error_unknown,
7532 "unexpected elements in list", return -1);
7534 return 0;
7537 const char *set_conversion_tests[] = {
7538 "[N] -> { [i] : N - 1 <= 2 i <= N }",
7539 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
7540 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7541 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7542 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
7543 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
7544 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
7545 "-3 + c <= d <= 28 + c) }",
7548 /* Check that converting from isl_set to isl_pw_multi_aff and back
7549 * to isl_set produces the original isl_set.
7551 static int test_set_conversion(isl_ctx *ctx)
7553 int i;
7554 const char *str;
7555 isl_set *set1, *set2;
7556 isl_pw_multi_aff *pma;
7557 int equal;
7559 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
7560 str = set_conversion_tests[i];
7561 set1 = isl_set_read_from_str(ctx, str);
7562 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
7563 set2 = isl_set_from_pw_multi_aff(pma);
7564 equal = isl_set_is_equal(set1, set2);
7565 isl_set_free(set1);
7566 isl_set_free(set2);
7568 if (equal < 0)
7569 return -1;
7570 if (!equal)
7571 isl_die(ctx, isl_error_unknown, "bad conversion",
7572 return -1);
7575 return 0;
7578 const char *conversion_tests[] = {
7579 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7580 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7581 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7582 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7583 "9e <= -2 - 2a) }",
7584 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7585 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7586 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7589 /* Check that converting from isl_map to isl_pw_multi_aff and back
7590 * to isl_map produces the original isl_map.
7592 static int test_map_conversion(isl_ctx *ctx)
7594 int i;
7595 isl_map *map1, *map2;
7596 isl_pw_multi_aff *pma;
7597 int equal;
7599 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
7600 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
7601 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
7602 map2 = isl_map_from_pw_multi_aff(pma);
7603 equal = isl_map_is_equal(map1, map2);
7604 isl_map_free(map1);
7605 isl_map_free(map2);
7607 if (equal < 0)
7608 return -1;
7609 if (!equal)
7610 isl_die(ctx, isl_error_unknown, "bad conversion",
7611 return -1);
7614 return 0;
7617 /* Descriptions of isl_pw_multi_aff objects for testing conversion
7618 * to isl_multi_pw_aff and back.
7620 const char *mpa_conversion_tests[] = {
7621 "{ [x] -> A[x] }",
7622 "{ [x] -> A[x] : x >= 0 }",
7623 "{ [x] -> A[x] : x >= 0; [x] -> A[-x] : x < 0 }",
7624 "{ [x] -> A[x, x + 1] }",
7625 "{ [x] -> A[] }",
7626 "{ [x] -> A[] : x >= 0 }",
7629 /* Check that conversion from isl_pw_multi_aff to isl_multi_pw_aff and
7630 * back to isl_pw_multi_aff preserves the original meaning.
7632 static int test_mpa_conversion(isl_ctx *ctx)
7634 int i;
7635 isl_pw_multi_aff *pma1, *pma2;
7636 isl_multi_pw_aff *mpa;
7637 int equal;
7639 for (i = 0; i < ARRAY_SIZE(mpa_conversion_tests); ++i) {
7640 const char *str;
7641 str = mpa_conversion_tests[i];
7642 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
7643 pma2 = isl_pw_multi_aff_copy(pma1);
7644 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma1);
7645 pma1 = isl_pw_multi_aff_from_multi_pw_aff(mpa);
7646 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7647 isl_pw_multi_aff_free(pma1);
7648 isl_pw_multi_aff_free(pma2);
7650 if (equal < 0)
7651 return -1;
7652 if (!equal)
7653 isl_die(ctx, isl_error_unknown, "bad conversion",
7654 return -1);
7657 return 0;
7660 /* Descriptions of union maps that should be convertible
7661 * to an isl_multi_union_pw_aff.
7663 const char *umap_mupa_conversion_tests[] = {
7664 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7665 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7666 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7667 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7668 "9e <= -2 - 2a) }",
7669 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7670 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7671 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7672 "{ A[] -> B[0]; C[] -> B[1] }",
7673 "{ A[] -> B[]; C[] -> B[] }",
7676 /* Check that converting from isl_union_map to isl_multi_union_pw_aff and back
7677 * to isl_union_map produces the original isl_union_map.
7679 static int test_union_map_mupa_conversion(isl_ctx *ctx)
7681 int i;
7682 isl_union_map *umap1, *umap2;
7683 isl_multi_union_pw_aff *mupa;
7684 int equal;
7686 for (i = 0; i < ARRAY_SIZE(umap_mupa_conversion_tests); ++i) {
7687 const char *str;
7688 str = umap_mupa_conversion_tests[i];
7689 umap1 = isl_union_map_read_from_str(ctx, str);
7690 umap2 = isl_union_map_copy(umap1);
7691 mupa = isl_multi_union_pw_aff_from_union_map(umap2);
7692 umap2 = isl_union_map_from_multi_union_pw_aff(mupa);
7693 equal = isl_union_map_is_equal(umap1, umap2);
7694 isl_union_map_free(umap1);
7695 isl_union_map_free(umap2);
7697 if (equal < 0)
7698 return -1;
7699 if (!equal)
7700 isl_die(ctx, isl_error_unknown, "bad conversion",
7701 return -1);
7704 return 0;
7707 static int test_conversion(isl_ctx *ctx)
7709 if (test_set_conversion(ctx) < 0)
7710 return -1;
7711 if (test_map_conversion(ctx) < 0)
7712 return -1;
7713 if (test_mpa_conversion(ctx) < 0)
7714 return -1;
7715 if (test_union_map_mupa_conversion(ctx) < 0)
7716 return -1;
7717 return 0;
7720 /* Check that isl_basic_map_curry does not modify input.
7722 static int test_curry(isl_ctx *ctx)
7724 const char *str;
7725 isl_basic_map *bmap1, *bmap2;
7726 int equal;
7728 str = "{ [A[] -> B[]] -> C[] }";
7729 bmap1 = isl_basic_map_read_from_str(ctx, str);
7730 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
7731 equal = isl_basic_map_is_equal(bmap1, bmap2);
7732 isl_basic_map_free(bmap1);
7733 isl_basic_map_free(bmap2);
7735 if (equal < 0)
7736 return -1;
7737 if (equal)
7738 isl_die(ctx, isl_error_unknown,
7739 "curried map should not be equal to original",
7740 return -1);
7742 return 0;
7745 struct {
7746 const char *set;
7747 const char *ma;
7748 const char *res;
7749 } preimage_tests[] = {
7750 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
7751 "{ A[j,i] -> B[i,j] }",
7752 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
7753 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
7754 "{ A[a,b] -> B[a/2,b/6] }",
7755 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
7756 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
7757 "{ A[a,b] -> B[a/2,b/6] }",
7758 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
7759 "exists i,j : a = 2 i and b = 6 j }" },
7760 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
7761 "[n] -> { : 0 <= n <= 100 }" },
7762 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
7763 "{ A[a] -> B[2a] }",
7764 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
7765 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
7766 "{ A[a] -> B[([a/2])] }",
7767 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
7768 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
7769 "{ A[a] -> B[a,a,a/3] }",
7770 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
7771 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
7772 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
7775 static int test_preimage_basic_set(isl_ctx *ctx)
7777 int i;
7778 isl_basic_set *bset1, *bset2;
7779 isl_multi_aff *ma;
7780 int equal;
7782 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
7783 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
7784 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
7785 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
7786 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
7787 equal = isl_basic_set_is_equal(bset1, bset2);
7788 isl_basic_set_free(bset1);
7789 isl_basic_set_free(bset2);
7790 if (equal < 0)
7791 return -1;
7792 if (!equal)
7793 isl_die(ctx, isl_error_unknown, "bad preimage",
7794 return -1);
7797 return 0;
7800 struct {
7801 const char *map;
7802 const char *ma;
7803 const char *res;
7804 } preimage_domain_tests[] = {
7805 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
7806 "{ A[j,i] -> B[i,j] }",
7807 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
7808 { "{ B[i] -> C[i]; D[i] -> E[i] }",
7809 "{ A[i] -> B[i + 1] }",
7810 "{ A[i] -> C[i + 1] }" },
7811 { "{ B[i] -> C[i]; B[i] -> E[i] }",
7812 "{ A[i] -> B[i + 1] }",
7813 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
7814 { "{ B[i] -> C[([i/2])] }",
7815 "{ A[i] -> B[2i] }",
7816 "{ A[i] -> C[i] }" },
7817 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
7818 "{ A[i] -> B[([i/5]), ([i/7])] }",
7819 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
7820 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
7821 "[N] -> { A[] -> B[([N/5])] }",
7822 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
7823 { "{ B[i] -> C[i] : exists a : i = 5 a }",
7824 "{ A[i] -> B[2i] }",
7825 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
7826 { "{ B[i] -> C[i] : exists a : i = 2 a; "
7827 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
7828 "{ A[i] -> B[2i] }",
7829 "{ A[i] -> C[2i] }" },
7830 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
7831 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
7834 static int test_preimage_union_map(isl_ctx *ctx)
7836 int i;
7837 isl_union_map *umap1, *umap2;
7838 isl_multi_aff *ma;
7839 int equal;
7841 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
7842 umap1 = isl_union_map_read_from_str(ctx,
7843 preimage_domain_tests[i].map);
7844 ma = isl_multi_aff_read_from_str(ctx,
7845 preimage_domain_tests[i].ma);
7846 umap2 = isl_union_map_read_from_str(ctx,
7847 preimage_domain_tests[i].res);
7848 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
7849 equal = isl_union_map_is_equal(umap1, umap2);
7850 isl_union_map_free(umap1);
7851 isl_union_map_free(umap2);
7852 if (equal < 0)
7853 return -1;
7854 if (!equal)
7855 isl_die(ctx, isl_error_unknown, "bad preimage",
7856 return -1);
7859 return 0;
7862 static int test_preimage(isl_ctx *ctx)
7864 if (test_preimage_basic_set(ctx) < 0)
7865 return -1;
7866 if (test_preimage_union_map(ctx) < 0)
7867 return -1;
7869 return 0;
7872 struct {
7873 const char *ma1;
7874 const char *ma;
7875 const char *res;
7876 } pullback_tests[] = {
7877 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
7878 "{ A[a,b] -> C[b + 2a] }" },
7879 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
7880 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
7881 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
7882 "{ A[a] -> C[(a)/6] }" },
7883 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
7884 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
7885 "{ A[a] -> C[(2a)/3] }" },
7886 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
7887 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
7888 "{ A[i,j] -> C[i + j, i + j] }"},
7889 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
7890 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
7891 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
7892 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
7893 "{ [i, j] -> [floor((i)/3), j] }",
7894 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
7897 static int test_pullback(isl_ctx *ctx)
7899 int i;
7900 isl_multi_aff *ma1, *ma2;
7901 isl_multi_aff *ma;
7902 int equal;
7904 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
7905 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
7906 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
7907 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
7908 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
7909 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
7910 isl_multi_aff_free(ma1);
7911 isl_multi_aff_free(ma2);
7912 if (equal < 0)
7913 return -1;
7914 if (!equal)
7915 isl_die(ctx, isl_error_unknown, "bad pullback",
7916 return -1);
7919 return 0;
7922 /* Check that negation is printed correctly and that equal expressions
7923 * are correctly identified.
7925 static int test_ast(isl_ctx *ctx)
7927 isl_ast_expr *expr, *expr1, *expr2, *expr3;
7928 char *str;
7929 int ok, equal;
7931 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
7932 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
7933 expr = isl_ast_expr_add(expr1, expr2);
7934 expr2 = isl_ast_expr_copy(expr);
7935 expr = isl_ast_expr_neg(expr);
7936 expr2 = isl_ast_expr_neg(expr2);
7937 equal = isl_ast_expr_is_equal(expr, expr2);
7938 str = isl_ast_expr_to_C_str(expr);
7939 ok = str ? !strcmp(str, "-(A + B)") : -1;
7940 free(str);
7941 isl_ast_expr_free(expr);
7942 isl_ast_expr_free(expr2);
7944 if (ok < 0 || equal < 0)
7945 return -1;
7946 if (!equal)
7947 isl_die(ctx, isl_error_unknown,
7948 "equal expressions not considered equal", return -1);
7949 if (!ok)
7950 isl_die(ctx, isl_error_unknown,
7951 "isl_ast_expr printed incorrectly", return -1);
7953 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
7954 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
7955 expr = isl_ast_expr_add(expr1, expr2);
7956 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
7957 expr = isl_ast_expr_sub(expr3, expr);
7958 str = isl_ast_expr_to_C_str(expr);
7959 ok = str ? !strcmp(str, "C - (A + B)") : -1;
7960 free(str);
7961 isl_ast_expr_free(expr);
7963 if (ok < 0)
7964 return -1;
7965 if (!ok)
7966 isl_die(ctx, isl_error_unknown,
7967 "isl_ast_expr printed incorrectly", return -1);
7969 return 0;
7972 /* Check that isl_ast_build_expr_from_set returns a valid expression
7973 * for an empty set. Note that isl_ast_build_expr_from_set getting
7974 * called on an empty set probably indicates a bug in the caller.
7976 static int test_ast_build(isl_ctx *ctx)
7978 isl_set *set;
7979 isl_ast_build *build;
7980 isl_ast_expr *expr;
7982 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
7983 build = isl_ast_build_from_context(set);
7985 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
7986 expr = isl_ast_build_expr_from_set(build, set);
7988 isl_ast_expr_free(expr);
7989 isl_ast_build_free(build);
7991 if (!expr)
7992 return -1;
7994 return 0;
7997 /* Internal data structure for before_for and after_for callbacks.
7999 * depth is the current depth
8000 * before is the number of times before_for has been called
8001 * after is the number of times after_for has been called
8003 struct isl_test_codegen_data {
8004 int depth;
8005 int before;
8006 int after;
8009 /* This function is called before each for loop in the AST generated
8010 * from test_ast_gen1.
8012 * Increment the number of calls and the depth.
8013 * Check that the space returned by isl_ast_build_get_schedule_space
8014 * matches the target space of the schedule returned by
8015 * isl_ast_build_get_schedule.
8016 * Return an isl_id that is checked by the corresponding call
8017 * to after_for.
8019 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
8020 void *user)
8022 struct isl_test_codegen_data *data = user;
8023 isl_ctx *ctx;
8024 isl_space *space;
8025 isl_union_map *schedule;
8026 isl_union_set *uset;
8027 isl_set *set;
8028 isl_bool empty;
8029 char name[] = "d0";
8031 ctx = isl_ast_build_get_ctx(build);
8033 if (data->before >= 3)
8034 isl_die(ctx, isl_error_unknown,
8035 "unexpected number of for nodes", return NULL);
8036 if (data->depth >= 2)
8037 isl_die(ctx, isl_error_unknown,
8038 "unexpected depth", return NULL);
8040 snprintf(name, sizeof(name), "d%d", data->depth);
8041 data->before++;
8042 data->depth++;
8044 schedule = isl_ast_build_get_schedule(build);
8045 uset = isl_union_map_range(schedule);
8046 if (!uset)
8047 return NULL;
8048 if (isl_union_set_n_set(uset) != 1) {
8049 isl_union_set_free(uset);
8050 isl_die(ctx, isl_error_unknown,
8051 "expecting single range space", return NULL);
8054 space = isl_ast_build_get_schedule_space(build);
8055 set = isl_union_set_extract_set(uset, space);
8056 isl_union_set_free(uset);
8057 empty = isl_set_is_empty(set);
8058 isl_set_free(set);
8060 if (empty < 0)
8061 return NULL;
8062 if (empty)
8063 isl_die(ctx, isl_error_unknown,
8064 "spaces don't match", return NULL);
8066 return isl_id_alloc(ctx, name, NULL);
8069 /* This function is called after each for loop in the AST generated
8070 * from test_ast_gen1.
8072 * Increment the number of calls and decrement the depth.
8073 * Check that the annotation attached to the node matches
8074 * the isl_id returned by the corresponding call to before_for.
8076 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
8077 __isl_keep isl_ast_build *build, void *user)
8079 struct isl_test_codegen_data *data = user;
8080 isl_id *id;
8081 const char *name;
8082 int valid;
8084 data->after++;
8085 data->depth--;
8087 if (data->after > data->before)
8088 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8089 "mismatch in number of for nodes",
8090 return isl_ast_node_free(node));
8092 id = isl_ast_node_get_annotation(node);
8093 if (!id)
8094 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8095 "missing annotation", return isl_ast_node_free(node));
8097 name = isl_id_get_name(id);
8098 valid = name && atoi(name + 1) == data->depth;
8099 isl_id_free(id);
8101 if (!valid)
8102 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8103 "wrong annotation", return isl_ast_node_free(node));
8105 return node;
8108 /* Check that the before_each_for and after_each_for callbacks
8109 * are called for each for loop in the generated code,
8110 * that they are called in the right order and that the isl_id
8111 * returned from the before_each_for callback is attached to
8112 * the isl_ast_node passed to the corresponding after_each_for call.
8114 static int test_ast_gen1(isl_ctx *ctx)
8116 const char *str;
8117 isl_set *set;
8118 isl_union_map *schedule;
8119 isl_ast_build *build;
8120 isl_ast_node *tree;
8121 struct isl_test_codegen_data data;
8123 str = "[N] -> { : N >= 10 }";
8124 set = isl_set_read_from_str(ctx, str);
8125 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
8126 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
8127 schedule = isl_union_map_read_from_str(ctx, str);
8129 data.before = 0;
8130 data.after = 0;
8131 data.depth = 0;
8132 build = isl_ast_build_from_context(set);
8133 build = isl_ast_build_set_before_each_for(build,
8134 &before_for, &data);
8135 build = isl_ast_build_set_after_each_for(build,
8136 &after_for, &data);
8137 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8138 isl_ast_build_free(build);
8139 if (!tree)
8140 return -1;
8142 isl_ast_node_free(tree);
8144 if (data.before != 3 || data.after != 3)
8145 isl_die(ctx, isl_error_unknown,
8146 "unexpected number of for nodes", return -1);
8148 return 0;
8151 /* Check that the AST generator handles domains that are integrally disjoint
8152 * but not rationally disjoint.
8154 static int test_ast_gen2(isl_ctx *ctx)
8156 const char *str;
8157 isl_set *set;
8158 isl_union_map *schedule;
8159 isl_union_map *options;
8160 isl_ast_build *build;
8161 isl_ast_node *tree;
8163 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
8164 schedule = isl_union_map_read_from_str(ctx, str);
8165 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8166 build = isl_ast_build_from_context(set);
8168 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
8169 options = isl_union_map_read_from_str(ctx, str);
8170 build = isl_ast_build_set_options(build, options);
8171 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8172 isl_ast_build_free(build);
8173 if (!tree)
8174 return -1;
8175 isl_ast_node_free(tree);
8177 return 0;
8180 /* Increment *user on each call.
8182 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
8183 __isl_keep isl_ast_build *build, void *user)
8185 int *n = user;
8187 (*n)++;
8189 return node;
8192 /* Test that unrolling tries to minimize the number of instances.
8193 * In particular, for the schedule given below, make sure it generates
8194 * 3 nodes (rather than 101).
8196 static int test_ast_gen3(isl_ctx *ctx)
8198 const char *str;
8199 isl_set *set;
8200 isl_union_map *schedule;
8201 isl_union_map *options;
8202 isl_ast_build *build;
8203 isl_ast_node *tree;
8204 int n_domain = 0;
8206 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
8207 schedule = isl_union_map_read_from_str(ctx, str);
8208 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8210 str = "{ [i] -> unroll[0] }";
8211 options = isl_union_map_read_from_str(ctx, str);
8213 build = isl_ast_build_from_context(set);
8214 build = isl_ast_build_set_options(build, options);
8215 build = isl_ast_build_set_at_each_domain(build,
8216 &count_domains, &n_domain);
8217 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8218 isl_ast_build_free(build);
8219 if (!tree)
8220 return -1;
8222 isl_ast_node_free(tree);
8224 if (n_domain != 3)
8225 isl_die(ctx, isl_error_unknown,
8226 "unexpected number of for nodes", return -1);
8228 return 0;
8231 /* Check that if the ast_build_exploit_nested_bounds options is set,
8232 * we do not get an outer if node in the generated AST,
8233 * while we do get such an outer if node if the options is not set.
8235 static int test_ast_gen4(isl_ctx *ctx)
8237 const char *str;
8238 isl_set *set;
8239 isl_union_map *schedule;
8240 isl_ast_build *build;
8241 isl_ast_node *tree;
8242 enum isl_ast_node_type type;
8243 int enb;
8245 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
8246 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
8248 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
8250 schedule = isl_union_map_read_from_str(ctx, str);
8251 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8252 build = isl_ast_build_from_context(set);
8253 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8254 isl_ast_build_free(build);
8255 if (!tree)
8256 return -1;
8258 type = isl_ast_node_get_type(tree);
8259 isl_ast_node_free(tree);
8261 if (type == isl_ast_node_if)
8262 isl_die(ctx, isl_error_unknown,
8263 "not expecting if node", return -1);
8265 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
8267 schedule = isl_union_map_read_from_str(ctx, str);
8268 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8269 build = isl_ast_build_from_context(set);
8270 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8271 isl_ast_build_free(build);
8272 if (!tree)
8273 return -1;
8275 type = isl_ast_node_get_type(tree);
8276 isl_ast_node_free(tree);
8278 if (type != isl_ast_node_if)
8279 isl_die(ctx, isl_error_unknown,
8280 "expecting if node", return -1);
8282 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
8284 return 0;
8287 /* This function is called for each leaf in the AST generated
8288 * from test_ast_gen5.
8290 * We finalize the AST generation by extending the outer schedule
8291 * with a zero-dimensional schedule. If this results in any for loops,
8292 * then this means that we did not pass along enough information
8293 * about the outer schedule to the inner AST generation.
8295 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
8296 void *user)
8298 isl_union_map *schedule, *extra;
8299 isl_ast_node *tree;
8301 schedule = isl_ast_build_get_schedule(build);
8302 extra = isl_union_map_copy(schedule);
8303 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
8304 schedule = isl_union_map_range_product(schedule, extra);
8305 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8306 isl_ast_build_free(build);
8308 if (!tree)
8309 return NULL;
8311 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
8312 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
8313 "code should not contain any for loop",
8314 return isl_ast_node_free(tree));
8316 return tree;
8319 /* Check that we do not lose any information when going back and
8320 * forth between internal and external schedule.
8322 * In particular, we create an AST where we unroll the only
8323 * non-constant dimension in the schedule. We therefore do
8324 * not expect any for loops in the AST. However, older versions
8325 * of isl would not pass along enough information about the outer
8326 * schedule when performing an inner code generation from a create_leaf
8327 * callback, resulting in the inner code generation producing a for loop.
8329 static int test_ast_gen5(isl_ctx *ctx)
8331 const char *str;
8332 isl_set *set;
8333 isl_union_map *schedule, *options;
8334 isl_ast_build *build;
8335 isl_ast_node *tree;
8337 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
8338 schedule = isl_union_map_read_from_str(ctx, str);
8340 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
8341 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
8342 options = isl_union_map_read_from_str(ctx, str);
8344 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8345 build = isl_ast_build_from_context(set);
8346 build = isl_ast_build_set_options(build, options);
8347 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
8348 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8349 isl_ast_build_free(build);
8350 isl_ast_node_free(tree);
8351 if (!tree)
8352 return -1;
8354 return 0;
8357 /* Check that the expression
8359 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
8361 * is not combined into
8363 * min(n/2, 0)
8365 * as this would result in n/2 being evaluated in parts of
8366 * the definition domain where n is not a multiple of 2.
8368 static int test_ast_expr(isl_ctx *ctx)
8370 const char *str;
8371 isl_pw_aff *pa;
8372 isl_ast_build *build;
8373 isl_ast_expr *expr;
8374 int min_max;
8375 int is_min;
8377 min_max = isl_options_get_ast_build_detect_min_max(ctx);
8378 isl_options_set_ast_build_detect_min_max(ctx, 1);
8380 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
8381 pa = isl_pw_aff_read_from_str(ctx, str);
8382 build = isl_ast_build_alloc(ctx);
8383 expr = isl_ast_build_expr_from_pw_aff(build, pa);
8384 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
8385 isl_ast_expr_get_op_type(expr) == isl_ast_op_min;
8386 isl_ast_build_free(build);
8387 isl_ast_expr_free(expr);
8389 isl_options_set_ast_build_detect_min_max(ctx, min_max);
8391 if (!expr)
8392 return -1;
8393 if (is_min)
8394 isl_die(ctx, isl_error_unknown,
8395 "expressions should not be combined", return -1);
8397 return 0;
8400 static int test_ast_gen(isl_ctx *ctx)
8402 if (test_ast_gen1(ctx) < 0)
8403 return -1;
8404 if (test_ast_gen2(ctx) < 0)
8405 return -1;
8406 if (test_ast_gen3(ctx) < 0)
8407 return -1;
8408 if (test_ast_gen4(ctx) < 0)
8409 return -1;
8410 if (test_ast_gen5(ctx) < 0)
8411 return -1;
8412 if (test_ast_expr(ctx) < 0)
8413 return -1;
8414 return 0;
8417 /* Check if dropping output dimensions from an isl_pw_multi_aff
8418 * works properly.
8420 static int test_pw_multi_aff(isl_ctx *ctx)
8422 const char *str;
8423 isl_pw_multi_aff *pma1, *pma2;
8424 int equal;
8426 str = "{ [i,j] -> [i+j, 4i-j] }";
8427 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
8428 str = "{ [i,j] -> [4i-j] }";
8429 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
8431 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
8433 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
8435 isl_pw_multi_aff_free(pma1);
8436 isl_pw_multi_aff_free(pma2);
8437 if (equal < 0)
8438 return -1;
8439 if (!equal)
8440 isl_die(ctx, isl_error_unknown,
8441 "expressions not equal", return -1);
8443 return 0;
8446 /* Check that we can properly parse multi piecewise affine expressions
8447 * where the piecewise affine expressions have different domains.
8449 static int test_multi_pw_aff_1(isl_ctx *ctx)
8451 const char *str;
8452 isl_set *dom, *dom2;
8453 isl_multi_pw_aff *mpa1, *mpa2;
8454 isl_pw_aff *pa;
8455 int equal;
8456 int equal_domain;
8458 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
8459 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
8460 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
8461 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
8462 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
8463 str = "{ [i] -> [(i : i > 0), 2i] }";
8464 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
8466 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
8468 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
8469 dom = isl_pw_aff_domain(pa);
8470 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
8471 dom2 = isl_pw_aff_domain(pa);
8472 equal_domain = isl_set_is_equal(dom, dom2);
8474 isl_set_free(dom);
8475 isl_set_free(dom2);
8476 isl_multi_pw_aff_free(mpa1);
8477 isl_multi_pw_aff_free(mpa2);
8479 if (equal < 0)
8480 return -1;
8481 if (!equal)
8482 isl_die(ctx, isl_error_unknown,
8483 "expressions not equal", return -1);
8485 if (equal_domain < 0)
8486 return -1;
8487 if (equal_domain)
8488 isl_die(ctx, isl_error_unknown,
8489 "domains unexpectedly equal", return -1);
8491 return 0;
8494 /* Check that the dimensions in the explicit domain
8495 * of a multi piecewise affine expression are properly
8496 * taken into account.
8498 static int test_multi_pw_aff_2(isl_ctx *ctx)
8500 const char *str;
8501 isl_bool involves1, involves2, involves3, equal;
8502 isl_multi_pw_aff *mpa, *mpa1, *mpa2;
8504 str = "{ A[x,y] -> B[] : x >= y }";
8505 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
8506 involves1 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 2);
8507 mpa1 = isl_multi_pw_aff_copy(mpa);
8509 mpa = isl_multi_pw_aff_insert_dims(mpa, isl_dim_in, 0, 1);
8510 involves2 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 1);
8511 involves3 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 1, 2);
8512 str = "{ [a,x,y] -> B[] : x >= y }";
8513 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
8514 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
8515 isl_multi_pw_aff_free(mpa2);
8517 mpa = isl_multi_pw_aff_drop_dims(mpa, isl_dim_in, 0, 1);
8518 mpa = isl_multi_pw_aff_set_tuple_name(mpa, isl_dim_in, "A");
8519 if (equal >= 0 && equal)
8520 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa1);
8521 isl_multi_pw_aff_free(mpa1);
8522 isl_multi_pw_aff_free(mpa);
8524 if (involves1 < 0 || involves2 < 0 || involves3 < 0 || equal < 0)
8525 return -1;
8526 if (!equal)
8527 isl_die(ctx, isl_error_unknown,
8528 "incorrect result of dimension insertion/removal",
8529 return isl_stat_error);
8530 if (!involves1 || involves2 || !involves3)
8531 isl_die(ctx, isl_error_unknown,
8532 "incorrect characterization of involved dimensions",
8533 return isl_stat_error);
8535 return 0;
8538 /* Check that isl_multi_union_pw_aff_multi_val_on_domain
8539 * sets the explicit domain of a zero-dimensional result,
8540 * such that it can be converted to an isl_union_map.
8542 static isl_stat test_multi_pw_aff_3(isl_ctx *ctx)
8544 isl_space *space;
8545 isl_union_set *dom;
8546 isl_multi_val *mv;
8547 isl_multi_union_pw_aff *mupa;
8548 isl_union_map *umap;
8550 dom = isl_union_set_read_from_str(ctx, "{ A[]; B[] }");
8551 space = isl_union_set_get_space(dom);
8552 mv = isl_multi_val_zero(isl_space_set_from_params(space));
8553 mupa = isl_multi_union_pw_aff_multi_val_on_domain(dom, mv);
8554 umap = isl_union_map_from_multi_union_pw_aff(mupa);
8555 isl_union_map_free(umap);
8556 if (!umap)
8557 return isl_stat_error;
8559 return isl_stat_ok;
8562 /* Perform some tests on multi piecewise affine expressions.
8564 static int test_multi_pw_aff(isl_ctx *ctx)
8566 if (test_multi_pw_aff_1(ctx) < 0)
8567 return -1;
8568 if (test_multi_pw_aff_2(ctx) < 0)
8569 return -1;
8570 if (test_multi_pw_aff_3(ctx) < 0)
8571 return -1;
8572 return 0;
8575 /* This is a regression test for a bug where isl_basic_map_simplify
8576 * would end up in an infinite loop. In particular, we construct
8577 * an empty basic set that is not obviously empty.
8578 * isl_basic_set_is_empty marks the basic set as empty.
8579 * After projecting out i3, the variable can be dropped completely,
8580 * but isl_basic_map_simplify refrains from doing so if the basic set
8581 * is empty and would end up in an infinite loop if it didn't test
8582 * explicitly for empty basic maps in the outer loop.
8584 static int test_simplify_1(isl_ctx *ctx)
8586 const char *str;
8587 isl_basic_set *bset;
8588 int empty;
8590 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
8591 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
8592 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
8593 "i3 >= i2 }";
8594 bset = isl_basic_set_read_from_str(ctx, str);
8595 empty = isl_basic_set_is_empty(bset);
8596 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
8597 isl_basic_set_free(bset);
8598 if (!bset)
8599 return -1;
8600 if (!empty)
8601 isl_die(ctx, isl_error_unknown,
8602 "basic set should be empty", return -1);
8604 return 0;
8607 /* Check that the equality in the set description below
8608 * is simplified away.
8610 static int test_simplify_2(isl_ctx *ctx)
8612 const char *str;
8613 isl_basic_set *bset;
8614 isl_bool universe;
8616 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
8617 bset = isl_basic_set_read_from_str(ctx, str);
8618 universe = isl_basic_set_plain_is_universe(bset);
8619 isl_basic_set_free(bset);
8621 if (universe < 0)
8622 return -1;
8623 if (!universe)
8624 isl_die(ctx, isl_error_unknown,
8625 "equality not simplified away", return -1);
8626 return 0;
8629 /* Some simplification tests.
8631 static int test_simplify(isl_ctx *ctx)
8633 if (test_simplify_1(ctx) < 0)
8634 return -1;
8635 if (test_simplify_2(ctx) < 0)
8636 return -1;
8637 return 0;
8640 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
8641 * with gbr context would fail to disable the use of the shifted tableau
8642 * when transferring equalities for the input to the context, resulting
8643 * in invalid sample values.
8645 static int test_partial_lexmin(isl_ctx *ctx)
8647 const char *str;
8648 isl_basic_set *bset;
8649 isl_basic_map *bmap;
8650 isl_map *map;
8652 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
8653 bmap = isl_basic_map_read_from_str(ctx, str);
8654 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
8655 bset = isl_basic_set_read_from_str(ctx, str);
8656 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
8657 isl_map_free(map);
8659 if (!map)
8660 return -1;
8662 return 0;
8665 /* Check that the variable compression performed on the existentially
8666 * quantified variables inside isl_basic_set_compute_divs is not confused
8667 * by the implicit equalities among the parameters.
8669 static int test_compute_divs(isl_ctx *ctx)
8671 const char *str;
8672 isl_basic_set *bset;
8673 isl_set *set;
8675 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
8676 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
8677 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
8678 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
8679 bset = isl_basic_set_read_from_str(ctx, str);
8680 set = isl_basic_set_compute_divs(bset);
8681 isl_set_free(set);
8682 if (!set)
8683 return -1;
8685 return 0;
8688 /* Check that isl_schedule_get_map is not confused by a schedule tree
8689 * with divergent filter node parameters, as can result from a call
8690 * to isl_schedule_intersect_domain.
8692 static int test_schedule_tree(isl_ctx *ctx)
8694 const char *str;
8695 isl_union_set *uset;
8696 isl_schedule *sched1, *sched2;
8697 isl_union_map *umap;
8699 uset = isl_union_set_read_from_str(ctx, "{ A[i] }");
8700 sched1 = isl_schedule_from_domain(uset);
8701 uset = isl_union_set_read_from_str(ctx, "{ B[] }");
8702 sched2 = isl_schedule_from_domain(uset);
8704 sched1 = isl_schedule_sequence(sched1, sched2);
8705 str = "[n] -> { A[i] : 0 <= i < n; B[] }";
8706 uset = isl_union_set_read_from_str(ctx, str);
8707 sched1 = isl_schedule_intersect_domain(sched1, uset);
8708 umap = isl_schedule_get_map(sched1);
8709 isl_schedule_free(sched1);
8710 isl_union_map_free(umap);
8711 if (!umap)
8712 return -1;
8714 return 0;
8717 /* Check that a zero-dimensional prefix schedule keeps track
8718 * of the domain and outer filters.
8720 static int test_schedule_tree_prefix(isl_ctx *ctx)
8722 const char *str;
8723 isl_bool equal;
8724 isl_union_set *uset;
8725 isl_union_set_list *filters;
8726 isl_multi_union_pw_aff *mupa, *mupa2;
8727 isl_schedule_node *node;
8729 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
8730 uset = isl_union_set_read_from_str(ctx, str);
8731 node = isl_schedule_node_from_domain(uset);
8732 node = isl_schedule_node_child(node, 0);
8734 str = "{ S1[i,j] : i > j }";
8735 uset = isl_union_set_read_from_str(ctx, str);
8736 filters = isl_union_set_list_from_union_set(uset);
8737 str = "{ S1[i,j] : i <= j; S2[i,j] }";
8738 uset = isl_union_set_read_from_str(ctx, str);
8739 filters = isl_union_set_list_add(filters, uset);
8740 node = isl_schedule_node_insert_sequence(node, filters);
8742 node = isl_schedule_node_child(node, 0);
8743 node = isl_schedule_node_child(node, 0);
8744 mupa = isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
8745 str = "([] : { S1[i,j] : i > j })";
8746 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx, str);
8747 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
8748 isl_multi_union_pw_aff_free(mupa2);
8749 isl_multi_union_pw_aff_free(mupa);
8750 isl_schedule_node_free(node);
8752 if (equal < 0)
8753 return -1;
8754 if (!equal)
8755 isl_die(ctx, isl_error_unknown, "unexpected prefix schedule",
8756 return -1);
8758 return 0;
8761 /* Check that the reaching domain elements and the prefix schedule
8762 * at a leaf node are the same before and after grouping.
8764 static int test_schedule_tree_group_1(isl_ctx *ctx)
8766 int equal;
8767 const char *str;
8768 isl_id *id;
8769 isl_union_set *uset;
8770 isl_multi_union_pw_aff *mupa;
8771 isl_union_pw_multi_aff *upma1, *upma2;
8772 isl_union_set *domain1, *domain2;
8773 isl_union_map *umap1, *umap2;
8774 isl_schedule_node *node;
8776 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
8777 uset = isl_union_set_read_from_str(ctx, str);
8778 node = isl_schedule_node_from_domain(uset);
8779 node = isl_schedule_node_child(node, 0);
8780 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
8781 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8782 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8783 node = isl_schedule_node_child(node, 0);
8784 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
8785 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8786 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8787 node = isl_schedule_node_child(node, 0);
8788 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
8789 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
8790 domain1 = isl_schedule_node_get_domain(node);
8791 id = isl_id_alloc(ctx, "group", NULL);
8792 node = isl_schedule_node_parent(node);
8793 node = isl_schedule_node_group(node, id);
8794 node = isl_schedule_node_child(node, 0);
8795 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
8796 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
8797 domain2 = isl_schedule_node_get_domain(node);
8798 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
8799 if (equal >= 0 && equal)
8800 equal = isl_union_set_is_equal(domain1, domain2);
8801 if (equal >= 0 && equal)
8802 equal = isl_union_map_is_equal(umap1, umap2);
8803 isl_union_map_free(umap1);
8804 isl_union_map_free(umap2);
8805 isl_union_set_free(domain1);
8806 isl_union_set_free(domain2);
8807 isl_union_pw_multi_aff_free(upma1);
8808 isl_union_pw_multi_aff_free(upma2);
8809 isl_schedule_node_free(node);
8811 if (equal < 0)
8812 return -1;
8813 if (!equal)
8814 isl_die(ctx, isl_error_unknown,
8815 "expressions not equal", return -1);
8817 return 0;
8820 /* Check that we can have nested groupings and that the union map
8821 * schedule representation is the same before and after the grouping.
8822 * Note that after the grouping, the union map representation contains
8823 * the domain constraints from the ranges of the expansion nodes,
8824 * while they are missing from the union map representation of
8825 * the tree without expansion nodes.
8827 * Also check that the global expansion is as expected.
8829 static int test_schedule_tree_group_2(isl_ctx *ctx)
8831 int equal, equal_expansion;
8832 const char *str;
8833 isl_id *id;
8834 isl_union_set *uset;
8835 isl_union_map *umap1, *umap2;
8836 isl_union_map *expansion1, *expansion2;
8837 isl_union_set_list *filters;
8838 isl_multi_union_pw_aff *mupa;
8839 isl_schedule *schedule;
8840 isl_schedule_node *node;
8842 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
8843 "S3[i,j] : 0 <= i,j < 10 }";
8844 uset = isl_union_set_read_from_str(ctx, str);
8845 node = isl_schedule_node_from_domain(uset);
8846 node = isl_schedule_node_child(node, 0);
8847 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
8848 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8849 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8850 node = isl_schedule_node_child(node, 0);
8851 str = "{ S1[i,j] }";
8852 uset = isl_union_set_read_from_str(ctx, str);
8853 filters = isl_union_set_list_from_union_set(uset);
8854 str = "{ S2[i,j]; S3[i,j] }";
8855 uset = isl_union_set_read_from_str(ctx, str);
8856 filters = isl_union_set_list_add(filters, uset);
8857 node = isl_schedule_node_insert_sequence(node, filters);
8858 node = isl_schedule_node_child(node, 1);
8859 node = isl_schedule_node_child(node, 0);
8860 str = "{ S2[i,j] }";
8861 uset = isl_union_set_read_from_str(ctx, str);
8862 filters = isl_union_set_list_from_union_set(uset);
8863 str = "{ S3[i,j] }";
8864 uset = isl_union_set_read_from_str(ctx, str);
8865 filters = isl_union_set_list_add(filters, uset);
8866 node = isl_schedule_node_insert_sequence(node, filters);
8868 schedule = isl_schedule_node_get_schedule(node);
8869 umap1 = isl_schedule_get_map(schedule);
8870 uset = isl_schedule_get_domain(schedule);
8871 umap1 = isl_union_map_intersect_domain(umap1, uset);
8872 isl_schedule_free(schedule);
8874 node = isl_schedule_node_parent(node);
8875 node = isl_schedule_node_parent(node);
8876 id = isl_id_alloc(ctx, "group1", NULL);
8877 node = isl_schedule_node_group(node, id);
8878 node = isl_schedule_node_child(node, 1);
8879 node = isl_schedule_node_child(node, 0);
8880 id = isl_id_alloc(ctx, "group2", NULL);
8881 node = isl_schedule_node_group(node, id);
8883 schedule = isl_schedule_node_get_schedule(node);
8884 umap2 = isl_schedule_get_map(schedule);
8885 isl_schedule_free(schedule);
8887 node = isl_schedule_node_root(node);
8888 node = isl_schedule_node_child(node, 0);
8889 expansion1 = isl_schedule_node_get_subtree_expansion(node);
8890 isl_schedule_node_free(node);
8892 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
8893 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
8894 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
8896 expansion2 = isl_union_map_read_from_str(ctx, str);
8898 equal = isl_union_map_is_equal(umap1, umap2);
8899 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
8901 isl_union_map_free(umap1);
8902 isl_union_map_free(umap2);
8903 isl_union_map_free(expansion1);
8904 isl_union_map_free(expansion2);
8906 if (equal < 0 || equal_expansion < 0)
8907 return -1;
8908 if (!equal)
8909 isl_die(ctx, isl_error_unknown,
8910 "expressions not equal", return -1);
8911 if (!equal_expansion)
8912 isl_die(ctx, isl_error_unknown,
8913 "unexpected expansion", return -1);
8915 return 0;
8918 /* Some tests for the isl_schedule_node_group function.
8920 static int test_schedule_tree_group(isl_ctx *ctx)
8922 if (test_schedule_tree_group_1(ctx) < 0)
8923 return -1;
8924 if (test_schedule_tree_group_2(ctx) < 0)
8925 return -1;
8926 return 0;
8929 struct {
8930 const char *set;
8931 const char *dual;
8932 } coef_tests[] = {
8933 { "{ rat: [i] : 0 <= i <= 10 }",
8934 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
8935 { "{ rat: [i] : FALSE }",
8936 "{ rat: coefficients[[cst] -> [a]] }" },
8937 { "{ rat: [i] : }",
8938 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
8941 struct {
8942 const char *set;
8943 const char *dual;
8944 } sol_tests[] = {
8945 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
8946 "{ rat: [i] : 0 <= i <= 10 }" },
8947 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
8948 "{ rat: [i] }" },
8949 { "{ rat: coefficients[[cst] -> [a]] }",
8950 "{ rat: [i] : FALSE }" },
8953 /* Test the basic functionality of isl_basic_set_coefficients and
8954 * isl_basic_set_solutions.
8956 static int test_dual(isl_ctx *ctx)
8958 int i;
8960 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
8961 int equal;
8962 isl_basic_set *bset1, *bset2;
8964 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
8965 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
8966 bset1 = isl_basic_set_coefficients(bset1);
8967 equal = isl_basic_set_is_equal(bset1, bset2);
8968 isl_basic_set_free(bset1);
8969 isl_basic_set_free(bset2);
8970 if (equal < 0)
8971 return -1;
8972 if (!equal)
8973 isl_die(ctx, isl_error_unknown,
8974 "incorrect dual", return -1);
8977 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
8978 int equal;
8979 isl_basic_set *bset1, *bset2;
8981 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
8982 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
8983 bset1 = isl_basic_set_solutions(bset1);
8984 equal = isl_basic_set_is_equal(bset1, bset2);
8985 isl_basic_set_free(bset1);
8986 isl_basic_set_free(bset2);
8987 if (equal < 0)
8988 return -1;
8989 if (!equal)
8990 isl_die(ctx, isl_error_unknown,
8991 "incorrect dual", return -1);
8994 return 0;
8997 struct {
8998 int scale_tile;
8999 int shift_point;
9000 const char *domain;
9001 const char *schedule;
9002 const char *sizes;
9003 const char *tile;
9004 const char *point;
9005 } tile_tests[] = {
9006 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
9007 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9008 "{ [32,32] }",
9009 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
9010 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9012 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
9013 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9014 "{ [32,32] }",
9015 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9016 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9018 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9019 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9020 "{ [32,32] }",
9021 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
9022 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9024 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9025 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9026 "{ [32,32] }",
9027 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9028 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9032 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
9033 * tile the band and then check if the tile and point bands have the
9034 * expected partial schedule.
9036 static int test_tile(isl_ctx *ctx)
9038 int i;
9039 int scale;
9040 int shift;
9042 scale = isl_options_get_tile_scale_tile_loops(ctx);
9043 shift = isl_options_get_tile_shift_point_loops(ctx);
9045 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
9046 int opt;
9047 int equal;
9048 const char *str;
9049 isl_union_set *domain;
9050 isl_multi_union_pw_aff *mupa, *mupa2;
9051 isl_schedule_node *node;
9052 isl_multi_val *sizes;
9054 opt = tile_tests[i].scale_tile;
9055 isl_options_set_tile_scale_tile_loops(ctx, opt);
9056 opt = tile_tests[i].shift_point;
9057 isl_options_set_tile_shift_point_loops(ctx, opt);
9059 str = tile_tests[i].domain;
9060 domain = isl_union_set_read_from_str(ctx, str);
9061 node = isl_schedule_node_from_domain(domain);
9062 node = isl_schedule_node_child(node, 0);
9063 str = tile_tests[i].schedule;
9064 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9065 node = isl_schedule_node_insert_partial_schedule(node, mupa);
9066 str = tile_tests[i].sizes;
9067 sizes = isl_multi_val_read_from_str(ctx, str);
9068 node = isl_schedule_node_band_tile(node, sizes);
9070 str = tile_tests[i].tile;
9071 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9072 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9073 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
9074 isl_multi_union_pw_aff_free(mupa);
9075 isl_multi_union_pw_aff_free(mupa2);
9077 node = isl_schedule_node_child(node, 0);
9079 str = tile_tests[i].point;
9080 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9081 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9082 if (equal >= 0 && equal)
9083 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
9084 mupa2);
9085 isl_multi_union_pw_aff_free(mupa);
9086 isl_multi_union_pw_aff_free(mupa2);
9088 isl_schedule_node_free(node);
9090 if (equal < 0)
9091 return -1;
9092 if (!equal)
9093 isl_die(ctx, isl_error_unknown,
9094 "unexpected result", return -1);
9097 isl_options_set_tile_scale_tile_loops(ctx, scale);
9098 isl_options_set_tile_shift_point_loops(ctx, shift);
9100 return 0;
9103 /* Check that the domain hash of a space is equal to the hash
9104 * of the domain of the space.
9106 static int test_domain_hash(isl_ctx *ctx)
9108 isl_map *map;
9109 isl_space *space;
9110 uint32_t hash1, hash2;
9112 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
9113 space = isl_map_get_space(map);
9114 isl_map_free(map);
9115 hash1 = isl_space_get_domain_hash(space);
9116 space = isl_space_domain(space);
9117 hash2 = isl_space_get_hash(space);
9118 isl_space_free(space);
9120 if (!space)
9121 return -1;
9122 if (hash1 != hash2)
9123 isl_die(ctx, isl_error_unknown,
9124 "domain hash not equal to hash of domain", return -1);
9126 return 0;
9129 /* Check that a universe basic set that is not obviously equal to the universe
9130 * is still recognized as being equal to the universe.
9132 static int test_universe(isl_ctx *ctx)
9134 const char *s;
9135 isl_basic_set *bset;
9136 isl_bool is_univ;
9138 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
9139 bset = isl_basic_set_read_from_str(ctx, s);
9140 is_univ = isl_basic_set_is_universe(bset);
9141 isl_basic_set_free(bset);
9143 if (is_univ < 0)
9144 return -1;
9145 if (!is_univ)
9146 isl_die(ctx, isl_error_unknown,
9147 "not recognized as universe set", return -1);
9149 return 0;
9152 /* Sets for which chambers are computed and checked.
9154 const char *chambers_tests[] = {
9155 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
9156 "z >= 0 and z <= C - y and z <= B - x - y }",
9159 /* Add the domain of "cell" to "cells".
9161 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
9163 isl_basic_set_list **cells = user;
9164 isl_basic_set *dom;
9166 dom = isl_cell_get_domain(cell);
9167 isl_cell_free(cell);
9168 *cells = isl_basic_set_list_add(*cells, dom);
9170 return *cells ? isl_stat_ok : isl_stat_error;
9173 /* Check that the elements of "list" are pairwise disjoint.
9175 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
9177 int i, j, n;
9179 if (!list)
9180 return isl_stat_error;
9182 n = isl_basic_set_list_n_basic_set(list);
9183 for (i = 0; i < n; ++i) {
9184 isl_basic_set *bset_i;
9186 bset_i = isl_basic_set_list_get_basic_set(list, i);
9187 for (j = i + 1; j < n; ++j) {
9188 isl_basic_set *bset_j;
9189 isl_bool disjoint;
9191 bset_j = isl_basic_set_list_get_basic_set(list, j);
9192 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
9193 isl_basic_set_free(bset_j);
9194 if (!disjoint)
9195 isl_die(isl_basic_set_list_get_ctx(list),
9196 isl_error_unknown, "not disjoint",
9197 break);
9198 if (disjoint < 0 || !disjoint)
9199 break;
9201 isl_basic_set_free(bset_i);
9202 if (j < n)
9203 return isl_stat_error;
9206 return isl_stat_ok;
9209 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
9210 * are pairwise disjoint.
9212 static int test_chambers(isl_ctx *ctx)
9214 int i;
9216 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
9217 isl_basic_set *bset;
9218 isl_vertices *vertices;
9219 isl_basic_set_list *cells;
9220 isl_stat ok;
9222 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
9223 vertices = isl_basic_set_compute_vertices(bset);
9224 cells = isl_basic_set_list_alloc(ctx, 0);
9225 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
9226 &cells) < 0)
9227 cells = isl_basic_set_list_free(cells);
9228 ok = check_pairwise_disjoint(cells);
9229 isl_basic_set_list_free(cells);
9230 isl_vertices_free(vertices);
9231 isl_basic_set_free(bset);
9233 if (ok < 0)
9234 return -1;
9237 return 0;
9240 struct {
9241 const char *name;
9242 int (*fn)(isl_ctx *ctx);
9243 } tests [] = {
9244 { "universe", &test_universe },
9245 { "domain hash", &test_domain_hash },
9246 { "dual", &test_dual },
9247 { "dependence analysis", &test_flow },
9248 { "val", &test_val },
9249 { "compute divs", &test_compute_divs },
9250 { "partial lexmin", &test_partial_lexmin },
9251 { "simplify", &test_simplify },
9252 { "curry", &test_curry },
9253 { "piecewise multi affine expressions", &test_pw_multi_aff },
9254 { "multi piecewise affine expressions", &test_multi_pw_aff },
9255 { "conversion", &test_conversion },
9256 { "list", &test_list },
9257 { "align parameters", &test_align_parameters },
9258 { "drop unused parameters", &test_drop_unused_parameters },
9259 { "preimage", &test_preimage },
9260 { "pullback", &test_pullback },
9261 { "AST", &test_ast },
9262 { "AST build", &test_ast_build },
9263 { "AST generation", &test_ast_gen },
9264 { "eliminate", &test_eliminate },
9265 { "residue class", &test_residue_class },
9266 { "div", &test_div },
9267 { "slice", &test_slice },
9268 { "fixed power", &test_fixed_power },
9269 { "sample", &test_sample },
9270 { "output", &test_output },
9271 { "vertices", &test_vertices },
9272 { "chambers", &test_chambers },
9273 { "fixed", &test_fixed },
9274 { "equal", &test_equal },
9275 { "disjoint", &test_disjoint },
9276 { "product", &test_product },
9277 { "dim_max", &test_dim_max },
9278 { "affine", &test_aff },
9279 { "injective", &test_injective },
9280 { "schedule (whole component)", &test_schedule_whole },
9281 { "schedule (incremental)", &test_schedule_incremental },
9282 { "schedule tree", &test_schedule_tree },
9283 { "schedule tree prefix", &test_schedule_tree_prefix },
9284 { "schedule tree grouping", &test_schedule_tree_group },
9285 { "tile", &test_tile },
9286 { "union_pw", &test_union_pw },
9287 { "locus", &test_locus },
9288 { "eval", &test_eval },
9289 { "parse", &test_parse },
9290 { "single-valued", &test_sv },
9291 { "affine hull", &test_affine_hull },
9292 { "simple_hull", &test_simple_hull },
9293 { "coalesce", &test_coalesce },
9294 { "factorize", &test_factorize },
9295 { "subset", &test_subset },
9296 { "subtract", &test_subtract },
9297 { "intersect", &test_intersect },
9298 { "lexmin", &test_lexmin },
9299 { "min", &test_min },
9300 { "gist", &test_gist },
9301 { "piecewise quasi-polynomials", &test_pwqp },
9302 { "lift", &test_lift },
9303 { "bound", &test_bound },
9304 { "get lists", &test_get_list },
9305 { "union", &test_union },
9306 { "split periods", &test_split_periods },
9307 { "lexicographic order", &test_lex },
9308 { "bijectivity", &test_bijective },
9309 { "dataflow analysis", &test_dep },
9310 { "reading", &test_read },
9311 { "bounded", &test_bounded },
9312 { "construction", &test_construction },
9313 { "dimension manipulation", &test_dim },
9314 { "map application", &test_application },
9315 { "convex hull", &test_convex_hull },
9316 { "transitive closure", &test_closure },
9317 { "isl_bool", &test_isl_bool},
9320 int main(int argc, char **argv)
9322 int i;
9323 struct isl_ctx *ctx;
9324 struct isl_options *options;
9326 options = isl_options_new_with_defaults();
9327 assert(options);
9328 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
9330 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
9331 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
9332 printf("%s\n", tests[i].name);
9333 if (tests[i].fn(ctx) < 0)
9334 goto error;
9336 isl_ctx_free(ctx);
9337 return 0;
9338 error:
9339 isl_ctx_free(ctx);
9340 return -1;