test isl_bool_not
[isl.git] / isl_test.c
blob5060d63cc39a5e81bad66bff5175933a12f2e778
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
2622 static int test_isl_bool(isl_ctx *ctx)
2624 if (check_bool(ctx, isl_bool_not(isl_bool_true), isl_bool_false) < 0)
2625 return -1;
2626 if (check_bool(ctx, isl_bool_not(isl_bool_false), isl_bool_true) < 0)
2627 return -1;
2628 if (check_bool(ctx, isl_bool_not(isl_bool_error), isl_bool_error) < 0)
2629 return -1;
2631 return 0;
2634 static int test_lex(struct isl_ctx *ctx)
2636 isl_space *dim;
2637 isl_map *map;
2638 int empty;
2640 dim = isl_space_set_alloc(ctx, 0, 0);
2641 map = isl_map_lex_le(dim);
2642 empty = isl_map_is_empty(map);
2643 isl_map_free(map);
2645 if (empty < 0)
2646 return -1;
2647 if (empty)
2648 isl_die(ctx, isl_error_unknown,
2649 "expecting non-empty result", return -1);
2651 return 0;
2654 /* Inputs for isl_map_lexmin tests.
2655 * "map" is the input and "lexmin" is the expected result.
2657 struct {
2658 const char *map;
2659 const char *lexmin;
2660 } lexmin_tests [] = {
2661 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2662 "{ [x] -> [5] : 6 <= x <= 8; "
2663 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2664 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2665 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2666 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2667 "{ [x] -> [y] : (4y = x and x >= 0) or "
2668 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2669 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2670 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2671 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2672 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2673 /* Check that empty pieces are properly combined. */
2674 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2675 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2676 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2677 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2678 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2679 "x >= 4 }" },
2680 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2681 "a <= 255 and c <= 255 and d <= 255 - j and "
2682 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2683 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2684 "247d <= 247 + i and 248 - b <= 248d <= c and "
2685 "254d >= i - a + b and 254d >= -a + b and "
2686 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2687 "{ [i, k, j] -> "
2688 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2689 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2690 "247j >= 62738 - i and 509j <= 129795 + i and "
2691 "742j >= 188724 - i; "
2692 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2693 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2694 "16*floor((8 + b)/16) <= 7 + b; "
2695 "[a] -> [1] }",
2696 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2697 "[a] -> [b = 0] : 0 < a <= 509 }" },
2698 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2699 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2702 static int test_lexmin(struct isl_ctx *ctx)
2704 int i;
2705 int equal;
2706 const char *str;
2707 isl_basic_map *bmap;
2708 isl_map *map, *map2;
2709 isl_set *set;
2710 isl_set *set2;
2711 isl_pw_multi_aff *pma;
2713 str = "[p0, p1] -> { [] -> [] : "
2714 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2715 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2716 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2717 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2718 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2719 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2720 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2721 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2722 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2723 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2724 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2725 map = isl_map_read_from_str(ctx, str);
2726 map = isl_map_lexmin(map);
2727 isl_map_free(map);
2729 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2730 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2731 set = isl_set_read_from_str(ctx, str);
2732 set = isl_set_lexmax(set);
2733 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2734 set2 = isl_set_read_from_str(ctx, str);
2735 set = isl_set_intersect(set, set2);
2736 assert(!isl_set_is_empty(set));
2737 isl_set_free(set);
2739 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2740 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2741 map = isl_map_lexmin(map);
2742 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2743 equal = isl_map_is_equal(map, map2);
2744 isl_map_free(map);
2745 isl_map_free(map2);
2747 if (equal < 0)
2748 return -1;
2749 if (!equal)
2750 isl_die(ctx, isl_error_unknown,
2751 "unexpected result", return -1);
2754 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2755 " 8i' <= i and 8i' >= -7 + i }";
2756 bmap = isl_basic_map_read_from_str(ctx, str);
2757 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2758 map2 = isl_map_from_pw_multi_aff(pma);
2759 map = isl_map_from_basic_map(bmap);
2760 assert(isl_map_is_equal(map, map2));
2761 isl_map_free(map);
2762 isl_map_free(map2);
2764 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2765 " 8i' <= i and 8i' >= -7 + i }";
2766 set = isl_set_read_from_str(ctx, str);
2767 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2768 set2 = isl_set_from_pw_multi_aff(pma);
2769 equal = isl_set_is_equal(set, set2);
2770 isl_set_free(set);
2771 isl_set_free(set2);
2772 if (equal < 0)
2773 return -1;
2774 if (!equal)
2775 isl_die(ctx, isl_error_unknown,
2776 "unexpected difference between set and "
2777 "piecewise affine expression", return -1);
2779 return 0;
2782 /* A specialized isl_set_min_val test case that would return the wrong result
2783 * in earlier versions of isl.
2784 * The explicit call to isl_basic_set_union prevents the second basic set
2785 * from being determined to be empty prior to the call to isl_set_min_val,
2786 * at least at the point where this test case was introduced.
2788 static int test_min_special(isl_ctx *ctx)
2790 const char *str;
2791 isl_basic_set *bset1, *bset2;
2792 isl_set *set;
2793 isl_aff *obj;
2794 isl_val *res;
2795 int ok;
2797 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2798 bset1 = isl_basic_set_read_from_str(ctx, str);
2799 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2800 bset2 = isl_basic_set_read_from_str(ctx, str);
2801 set = isl_basic_set_union(bset1, bset2);
2802 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2804 res = isl_set_min_val(set, obj);
2805 ok = isl_val_cmp_si(res, 5) == 0;
2807 isl_aff_free(obj);
2808 isl_set_free(set);
2809 isl_val_free(res);
2811 if (!res)
2812 return -1;
2813 if (!ok)
2814 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2815 return -1);
2817 return 0;
2820 /* A specialized isl_set_min_val test case that would return an error
2821 * in earlier versions of isl.
2823 static int test_min_special2(isl_ctx *ctx)
2825 const char *str;
2826 isl_basic_set *bset;
2827 isl_aff *obj;
2828 isl_val *res;
2830 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
2831 bset = isl_basic_set_read_from_str(ctx, str);
2833 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
2835 res = isl_basic_set_max_val(bset, obj);
2837 isl_basic_set_free(bset);
2838 isl_aff_free(obj);
2839 isl_val_free(res);
2841 if (!res)
2842 return -1;
2844 return 0;
2847 struct {
2848 const char *set;
2849 const char *obj;
2850 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2851 __isl_keep isl_aff *obj);
2852 const char *res;
2853 } opt_tests[] = {
2854 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2855 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2856 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2857 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2858 &isl_set_max_val, "30" },
2862 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2863 * In particular, check the results on non-convex inputs.
2865 static int test_min(struct isl_ctx *ctx)
2867 int i;
2868 isl_set *set;
2869 isl_aff *obj;
2870 isl_val *val, *res;
2871 isl_bool ok;
2873 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2874 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2875 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2876 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2877 val = opt_tests[i].fn(set, obj);
2878 ok = isl_val_eq(res, val);
2879 isl_val_free(res);
2880 isl_val_free(val);
2881 isl_aff_free(obj);
2882 isl_set_free(set);
2884 if (ok < 0)
2885 return -1;
2886 if (!ok)
2887 isl_die(ctx, isl_error_unknown,
2888 "unexpected optimum", return -1);
2891 if (test_min_special(ctx) < 0)
2892 return -1;
2893 if (test_min_special2(ctx) < 0)
2894 return -1;
2896 return 0;
2899 struct must_may {
2900 isl_map *must;
2901 isl_map *may;
2904 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2905 void *dep_user, void *user)
2907 struct must_may *mm = (struct must_may *)user;
2909 if (must)
2910 mm->must = isl_map_union(mm->must, dep);
2911 else
2912 mm->may = isl_map_union(mm->may, dep);
2914 return isl_stat_ok;
2917 static int common_space(void *first, void *second)
2919 int depth = *(int *)first;
2920 return 2 * depth;
2923 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2925 isl_map *map2;
2926 int equal;
2928 if (!map)
2929 return -1;
2931 map2 = isl_map_read_from_str(map->ctx, str);
2932 equal = isl_map_is_equal(map, map2);
2933 isl_map_free(map2);
2935 return equal;
2938 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2940 int equal;
2942 equal = map_is_equal(map, str);
2943 if (equal < 0)
2944 return -1;
2945 if (!equal)
2946 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2947 "result not as expected", return -1);
2948 return 0;
2951 static int test_dep(struct isl_ctx *ctx)
2953 const char *str;
2954 isl_space *dim;
2955 isl_map *map;
2956 isl_access_info *ai;
2957 isl_flow *flow;
2958 int depth;
2959 struct must_may mm;
2961 depth = 3;
2963 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2964 map = isl_map_read_from_str(ctx, str);
2965 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2967 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2968 map = isl_map_read_from_str(ctx, str);
2969 ai = isl_access_info_add_source(ai, map, 1, &depth);
2971 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2972 map = isl_map_read_from_str(ctx, str);
2973 ai = isl_access_info_add_source(ai, map, 1, &depth);
2975 flow = isl_access_info_compute_flow(ai);
2976 dim = isl_space_alloc(ctx, 0, 3, 3);
2977 mm.must = isl_map_empty(isl_space_copy(dim));
2978 mm.may = isl_map_empty(dim);
2980 isl_flow_foreach(flow, collect_must_may, &mm);
2982 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2983 " [1,10,0] -> [2,5,0] }";
2984 assert(map_is_equal(mm.must, str));
2985 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2986 assert(map_is_equal(mm.may, str));
2988 isl_map_free(mm.must);
2989 isl_map_free(mm.may);
2990 isl_flow_free(flow);
2993 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2994 map = isl_map_read_from_str(ctx, str);
2995 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2997 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2998 map = isl_map_read_from_str(ctx, str);
2999 ai = isl_access_info_add_source(ai, map, 1, &depth);
3001 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3002 map = isl_map_read_from_str(ctx, str);
3003 ai = isl_access_info_add_source(ai, map, 0, &depth);
3005 flow = isl_access_info_compute_flow(ai);
3006 dim = isl_space_alloc(ctx, 0, 3, 3);
3007 mm.must = isl_map_empty(isl_space_copy(dim));
3008 mm.may = isl_map_empty(dim);
3010 isl_flow_foreach(flow, collect_must_may, &mm);
3012 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
3013 assert(map_is_equal(mm.must, str));
3014 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3015 assert(map_is_equal(mm.may, str));
3017 isl_map_free(mm.must);
3018 isl_map_free(mm.may);
3019 isl_flow_free(flow);
3022 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3023 map = isl_map_read_from_str(ctx, str);
3024 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3026 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3027 map = isl_map_read_from_str(ctx, str);
3028 ai = isl_access_info_add_source(ai, map, 0, &depth);
3030 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3031 map = isl_map_read_from_str(ctx, str);
3032 ai = isl_access_info_add_source(ai, map, 0, &depth);
3034 flow = isl_access_info_compute_flow(ai);
3035 dim = isl_space_alloc(ctx, 0, 3, 3);
3036 mm.must = isl_map_empty(isl_space_copy(dim));
3037 mm.may = isl_map_empty(dim);
3039 isl_flow_foreach(flow, collect_must_may, &mm);
3041 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
3042 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3043 assert(map_is_equal(mm.may, str));
3044 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3045 assert(map_is_equal(mm.must, str));
3047 isl_map_free(mm.must);
3048 isl_map_free(mm.may);
3049 isl_flow_free(flow);
3052 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
3053 map = isl_map_read_from_str(ctx, str);
3054 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3056 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3057 map = isl_map_read_from_str(ctx, str);
3058 ai = isl_access_info_add_source(ai, map, 0, &depth);
3060 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
3061 map = isl_map_read_from_str(ctx, str);
3062 ai = isl_access_info_add_source(ai, map, 0, &depth);
3064 flow = isl_access_info_compute_flow(ai);
3065 dim = isl_space_alloc(ctx, 0, 3, 3);
3066 mm.must = isl_map_empty(isl_space_copy(dim));
3067 mm.may = isl_map_empty(dim);
3069 isl_flow_foreach(flow, collect_must_may, &mm);
3071 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
3072 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
3073 assert(map_is_equal(mm.may, str));
3074 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3075 assert(map_is_equal(mm.must, str));
3077 isl_map_free(mm.must);
3078 isl_map_free(mm.may);
3079 isl_flow_free(flow);
3082 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
3083 map = isl_map_read_from_str(ctx, str);
3084 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3086 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3087 map = isl_map_read_from_str(ctx, str);
3088 ai = isl_access_info_add_source(ai, map, 0, &depth);
3090 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
3091 map = isl_map_read_from_str(ctx, str);
3092 ai = isl_access_info_add_source(ai, map, 0, &depth);
3094 flow = isl_access_info_compute_flow(ai);
3095 dim = isl_space_alloc(ctx, 0, 3, 3);
3096 mm.must = isl_map_empty(isl_space_copy(dim));
3097 mm.may = isl_map_empty(dim);
3099 isl_flow_foreach(flow, collect_must_may, &mm);
3101 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
3102 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
3103 assert(map_is_equal(mm.may, str));
3104 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3105 assert(map_is_equal(mm.must, str));
3107 isl_map_free(mm.must);
3108 isl_map_free(mm.may);
3109 isl_flow_free(flow);
3112 depth = 5;
3114 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3115 map = isl_map_read_from_str(ctx, str);
3116 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
3118 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3119 map = isl_map_read_from_str(ctx, str);
3120 ai = isl_access_info_add_source(ai, map, 1, &depth);
3122 flow = isl_access_info_compute_flow(ai);
3123 dim = isl_space_alloc(ctx, 0, 5, 5);
3124 mm.must = isl_map_empty(isl_space_copy(dim));
3125 mm.may = isl_map_empty(dim);
3127 isl_flow_foreach(flow, collect_must_may, &mm);
3129 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
3130 assert(map_is_equal(mm.must, str));
3131 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
3132 assert(map_is_equal(mm.may, str));
3134 isl_map_free(mm.must);
3135 isl_map_free(mm.may);
3136 isl_flow_free(flow);
3138 return 0;
3141 /* Check that the dependence analysis proceeds without errors.
3142 * Earlier versions of isl would break down during the analysis
3143 * due to the use of the wrong spaces.
3145 static int test_flow(isl_ctx *ctx)
3147 const char *str;
3148 isl_union_map *access, *schedule;
3149 isl_union_map *must_dep, *may_dep;
3150 int r;
3152 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
3153 access = isl_union_map_read_from_str(ctx, str);
3154 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
3155 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
3156 "S2[] -> [1,0,0,0]; "
3157 "S3[] -> [-1,0,0,0] }";
3158 schedule = isl_union_map_read_from_str(ctx, str);
3159 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
3160 isl_union_map_copy(access), schedule,
3161 &must_dep, &may_dep, NULL, NULL);
3162 isl_union_map_free(may_dep);
3163 isl_union_map_free(must_dep);
3165 return r;
3168 struct {
3169 const char *map;
3170 int sv;
3171 } sv_tests[] = {
3172 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
3173 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
3174 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
3175 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
3176 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
3177 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
3178 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3179 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3180 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
3183 int test_sv(isl_ctx *ctx)
3185 isl_union_map *umap;
3186 int i;
3187 int sv;
3189 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
3190 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
3191 sv = isl_union_map_is_single_valued(umap);
3192 isl_union_map_free(umap);
3193 if (sv < 0)
3194 return -1;
3195 if (sv_tests[i].sv && !sv)
3196 isl_die(ctx, isl_error_internal,
3197 "map not detected as single valued", return -1);
3198 if (!sv_tests[i].sv && sv)
3199 isl_die(ctx, isl_error_internal,
3200 "map detected as single valued", return -1);
3203 return 0;
3206 struct {
3207 const char *str;
3208 int bijective;
3209 } bijective_tests[] = {
3210 { "[N,M]->{[i,j] -> [i]}", 0 },
3211 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
3212 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
3213 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
3214 { "[N,M]->{[i,j] -> [j,i]}", 1 },
3215 { "[N,M]->{[i,j] -> [i+j]}", 0 },
3216 { "[N,M]->{[i,j] -> []}", 0 },
3217 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
3218 { "[N,M]->{[i,j] -> [2i]}", 0 },
3219 { "[N,M]->{[i,j] -> [i,i]}", 0 },
3220 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
3221 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
3222 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
3225 static int test_bijective(struct isl_ctx *ctx)
3227 isl_map *map;
3228 int i;
3229 int bijective;
3231 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
3232 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
3233 bijective = isl_map_is_bijective(map);
3234 isl_map_free(map);
3235 if (bijective < 0)
3236 return -1;
3237 if (bijective_tests[i].bijective && !bijective)
3238 isl_die(ctx, isl_error_internal,
3239 "map not detected as bijective", return -1);
3240 if (!bijective_tests[i].bijective && bijective)
3241 isl_die(ctx, isl_error_internal,
3242 "map detected as bijective", return -1);
3245 return 0;
3248 /* Inputs for isl_pw_qpolynomial_gist tests.
3249 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
3251 struct {
3252 const char *pwqp;
3253 const char *set;
3254 const char *gist;
3255 } pwqp_gist_tests[] = {
3256 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
3257 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
3258 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
3259 "{ [i] -> -1/2 + 1/2 * i }" },
3260 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
3263 static int test_pwqp(struct isl_ctx *ctx)
3265 int i;
3266 const char *str;
3267 isl_set *set;
3268 isl_pw_qpolynomial *pwqp1, *pwqp2;
3269 int equal;
3271 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3272 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3274 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
3275 isl_dim_in, 1, 1);
3277 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3278 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3280 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3282 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3284 isl_pw_qpolynomial_free(pwqp1);
3286 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
3287 str = pwqp_gist_tests[i].pwqp;
3288 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3289 str = pwqp_gist_tests[i].set;
3290 set = isl_set_read_from_str(ctx, str);
3291 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
3292 str = pwqp_gist_tests[i].gist;
3293 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3294 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3295 equal = isl_pw_qpolynomial_is_zero(pwqp1);
3296 isl_pw_qpolynomial_free(pwqp1);
3298 if (equal < 0)
3299 return -1;
3300 if (!equal)
3301 isl_die(ctx, isl_error_unknown,
3302 "unexpected result", return -1);
3305 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
3306 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3307 str = "{ [i] -> ([(2 * [i/2])/5]) }";
3308 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3310 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3312 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3314 isl_pw_qpolynomial_free(pwqp1);
3316 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3317 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3318 str = "{ [x] -> x }";
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 = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3328 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3329 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3330 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3331 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3332 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3333 isl_pw_qpolynomial_free(pwqp1);
3335 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3336 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3337 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3338 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3339 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3340 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3341 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3342 isl_pw_qpolynomial_free(pwqp1);
3343 isl_pw_qpolynomial_free(pwqp2);
3344 if (equal < 0)
3345 return -1;
3346 if (!equal)
3347 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3349 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3350 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3351 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3352 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3353 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3354 isl_val_one(ctx));
3355 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3356 isl_pw_qpolynomial_free(pwqp1);
3357 isl_pw_qpolynomial_free(pwqp2);
3358 if (equal < 0)
3359 return -1;
3360 if (!equal)
3361 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3363 return 0;
3366 static int test_split_periods(isl_ctx *ctx)
3368 const char *str;
3369 isl_pw_qpolynomial *pwqp;
3371 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3372 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3373 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3374 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3376 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3378 isl_pw_qpolynomial_free(pwqp);
3380 if (!pwqp)
3381 return -1;
3383 return 0;
3386 static int test_union(isl_ctx *ctx)
3388 const char *str;
3389 isl_union_set *uset1, *uset2;
3390 isl_union_map *umap1, *umap2;
3391 int equal;
3393 str = "{ [i] : 0 <= i <= 1 }";
3394 uset1 = isl_union_set_read_from_str(ctx, str);
3395 str = "{ [1] -> [0] }";
3396 umap1 = isl_union_map_read_from_str(ctx, str);
3398 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3399 equal = isl_union_map_is_equal(umap1, umap2);
3401 isl_union_map_free(umap1);
3402 isl_union_map_free(umap2);
3404 if (equal < 0)
3405 return -1;
3406 if (!equal)
3407 isl_die(ctx, isl_error_unknown, "union maps not equal",
3408 return -1);
3410 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3411 umap1 = isl_union_map_read_from_str(ctx, str);
3412 str = "{ A[i]; B[i] }";
3413 uset1 = isl_union_set_read_from_str(ctx, str);
3415 uset2 = isl_union_map_domain(umap1);
3417 equal = isl_union_set_is_equal(uset1, uset2);
3419 isl_union_set_free(uset1);
3420 isl_union_set_free(uset2);
3422 if (equal < 0)
3423 return -1;
3424 if (!equal)
3425 isl_die(ctx, isl_error_unknown, "union sets not equal",
3426 return -1);
3428 return 0;
3431 /* Check that computing a bound of a non-zero polynomial over an unbounded
3432 * domain does not produce a rational value.
3433 * In particular, check that the upper bound is infinity.
3435 static int test_bound_unbounded_domain(isl_ctx *ctx)
3437 const char *str;
3438 isl_pw_qpolynomial *pwqp;
3439 isl_pw_qpolynomial_fold *pwf, *pwf2;
3440 isl_bool equal;
3442 str = "{ [m,n] -> -m * n }";
3443 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3444 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3445 str = "{ infty }";
3446 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3447 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3448 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
3449 isl_pw_qpolynomial_fold_free(pwf);
3450 isl_pw_qpolynomial_fold_free(pwf2);
3452 if (equal < 0)
3453 return -1;
3454 if (!equal)
3455 isl_die(ctx, isl_error_unknown,
3456 "expecting infinite polynomial bound", return -1);
3458 return 0;
3461 static int test_bound(isl_ctx *ctx)
3463 const char *str;
3464 unsigned dim;
3465 isl_pw_qpolynomial *pwqp;
3466 isl_pw_qpolynomial_fold *pwf;
3468 if (test_bound_unbounded_domain(ctx) < 0)
3469 return -1;
3471 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3472 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3473 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3474 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3475 isl_pw_qpolynomial_fold_free(pwf);
3476 if (dim != 4)
3477 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3478 return -1);
3480 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3481 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3482 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3483 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3484 isl_pw_qpolynomial_fold_free(pwf);
3485 if (dim != 1)
3486 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3487 return -1);
3489 return 0;
3492 /* isl_set is defined to isl_map internally, so the corresponding elements
3493 * are isl_basic_map objects.
3495 #undef EL_BASE
3496 #undef SET_BASE
3497 #define EL_BASE basic_map
3498 #define SET_BASE set
3499 #include "isl_test_list_templ.c"
3501 #undef EL_BASE
3502 #undef SET_BASE
3503 #define EL_BASE basic_set
3504 #define SET_BASE union_set
3505 #include "isl_test_list_templ.c"
3507 #undef EL_BASE
3508 #undef SET_BASE
3509 #define EL_BASE set
3510 #define SET_BASE union_set
3511 #include "isl_test_list_templ.c"
3513 #undef EL_BASE
3514 #undef SET_BASE
3515 #define EL_BASE basic_map
3516 #define SET_BASE map
3517 #include "isl_test_list_templ.c"
3519 #undef EL_BASE
3520 #undef SET_BASE
3521 #define EL_BASE map
3522 #define SET_BASE union_map
3523 #include "isl_test_list_templ.c"
3525 /* Check that the conversion from isl objects to lists works as expected.
3527 static int test_get_list(isl_ctx *ctx)
3529 if (test_get_list_basic_map_from_set(ctx, "{ [0]; [2]; [3] }"))
3530 return -1;
3531 if (test_get_list_basic_set_from_union_set(ctx, "{ A[0]; B[2]; B[3] }"))
3532 return -1;
3533 if (test_get_list_set_from_union_set(ctx, "{ A[0]; A[2]; B[3] }"))
3534 return -1;
3535 if (test_get_list_basic_map_from_map(ctx,
3536 "{ [0] -> [0]; [2] -> [0]; [3] -> [0] }"))
3537 return -1;
3538 if (test_get_list_map_from_union_map(ctx,
3539 "{ A[0] -> [0]; A[2] -> [0]; B[3] -> [0] }"))
3540 return -1;
3542 return 0;
3545 static int test_lift(isl_ctx *ctx)
3547 const char *str;
3548 isl_basic_map *bmap;
3549 isl_basic_set *bset;
3551 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3552 bset = isl_basic_set_read_from_str(ctx, str);
3553 bset = isl_basic_set_lift(bset);
3554 bmap = isl_basic_map_from_range(bset);
3555 bset = isl_basic_map_domain(bmap);
3556 isl_basic_set_free(bset);
3558 return 0;
3561 /* Check that isl_set_is_subset is not confused by identical
3562 * integer divisions.
3563 * The call to isl_set_normalize ensures that the equality constraints
3564 * a = b = 0 are discovered, turning e0 and e1 into identical
3565 * integer divisions. Any further simplification would remove
3566 * the duplicate integer divisions.
3568 static isl_stat test_subset_duplicate_integer_divisions(isl_ctx *ctx)
3570 const char *str;
3571 isl_bool is_subset;
3572 isl_set *set1, *set2;
3574 str = "{ [a, b, c, d] : "
3575 "exists (e0 = floor((a + d)/4), e1 = floor((d)/4), "
3576 "e2 = floor((-a - d + 4 *floor((a + d)/4))/10), "
3577 "e3 = floor((-d + 4*floor((d)/4))/10): "
3578 "10e2 = -a - 2c - d + 4e0 and 10e3 = -2c - d + 4e1 and "
3579 "b >= 0 and a <= 0 and b <= a) }";
3580 set1 = isl_set_read_from_str(ctx, str);
3581 set2 = isl_set_read_from_str(ctx, str);
3582 set2 = isl_set_normalize(set2);
3584 is_subset = isl_set_is_subset(set1, set2);
3586 isl_set_free(set1);
3587 isl_set_free(set2);
3589 if (is_subset < 0)
3590 return isl_stat_error;
3591 if (!is_subset)
3592 isl_die(ctx, isl_error_unknown,
3593 "set is not considered to be a subset of itself",
3594 return isl_stat_error);
3596 return isl_stat_ok;
3599 struct {
3600 const char *set1;
3601 const char *set2;
3602 int subset;
3603 } subset_tests[] = {
3604 { "{ [112, 0] }",
3605 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3606 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3607 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3608 { "{ [65] }",
3609 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3610 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3611 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3612 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3613 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3614 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3615 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3616 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3617 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3618 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3619 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3620 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3621 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3622 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3623 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3624 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3625 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3626 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3627 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3628 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3629 "4e0 <= -57 + i0 + i1)) or "
3630 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3631 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3632 "4e0 >= -61 + i0 + i1)) or "
3633 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3634 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3637 static int test_subset(isl_ctx *ctx)
3639 int i;
3640 isl_set *set1, *set2;
3641 int subset;
3643 if (test_subset_duplicate_integer_divisions(ctx) < 0)
3644 return -1;
3646 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3647 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3648 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3649 subset = isl_set_is_subset(set1, set2);
3650 isl_set_free(set1);
3651 isl_set_free(set2);
3652 if (subset < 0)
3653 return -1;
3654 if (subset != subset_tests[i].subset)
3655 isl_die(ctx, isl_error_unknown,
3656 "incorrect subset result", return -1);
3659 return 0;
3662 struct {
3663 const char *minuend;
3664 const char *subtrahend;
3665 const char *difference;
3666 } subtract_domain_tests[] = {
3667 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3668 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3669 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3672 static int test_subtract(isl_ctx *ctx)
3674 int i;
3675 isl_union_map *umap1, *umap2;
3676 isl_union_pw_multi_aff *upma1, *upma2;
3677 isl_union_set *uset;
3678 int equal;
3680 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3681 umap1 = isl_union_map_read_from_str(ctx,
3682 subtract_domain_tests[i].minuend);
3683 uset = isl_union_set_read_from_str(ctx,
3684 subtract_domain_tests[i].subtrahend);
3685 umap2 = isl_union_map_read_from_str(ctx,
3686 subtract_domain_tests[i].difference);
3687 umap1 = isl_union_map_subtract_domain(umap1, uset);
3688 equal = isl_union_map_is_equal(umap1, umap2);
3689 isl_union_map_free(umap1);
3690 isl_union_map_free(umap2);
3691 if (equal < 0)
3692 return -1;
3693 if (!equal)
3694 isl_die(ctx, isl_error_unknown,
3695 "incorrect subtract domain result", return -1);
3698 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3699 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3700 subtract_domain_tests[i].minuend);
3701 uset = isl_union_set_read_from_str(ctx,
3702 subtract_domain_tests[i].subtrahend);
3703 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3704 subtract_domain_tests[i].difference);
3705 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3706 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3707 isl_union_pw_multi_aff_free(upma1);
3708 isl_union_pw_multi_aff_free(upma2);
3709 if (equal < 0)
3710 return -1;
3711 if (!equal)
3712 isl_die(ctx, isl_error_unknown,
3713 "incorrect subtract domain result", return -1);
3716 return 0;
3719 /* Check that intersecting the empty basic set with another basic set
3720 * does not increase the number of constraints. In particular,
3721 * the empty basic set should maintain its canonical representation.
3723 static int test_intersect_1(isl_ctx *ctx)
3725 int n1, n2;
3726 isl_basic_set *bset1, *bset2;
3728 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3729 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3730 n1 = isl_basic_set_n_constraint(bset1);
3731 bset1 = isl_basic_set_intersect(bset1, bset2);
3732 n2 = isl_basic_set_n_constraint(bset1);
3733 isl_basic_set_free(bset1);
3734 if (!bset1)
3735 return -1;
3736 if (n1 != n2)
3737 isl_die(ctx, isl_error_unknown,
3738 "number of constraints of empty set changed",
3739 return -1);
3741 return 0;
3744 /* Check that intersecting a set with itself does not cause
3745 * an explosion in the number of disjuncts.
3747 static isl_stat test_intersect_2(isl_ctx *ctx)
3749 int i;
3750 isl_set *set;
3752 set = isl_set_read_from_str(ctx, "{ [x,y] : x >= 0 or y >= 0 }");
3753 for (i = 0; i < 100; ++i)
3754 set = isl_set_intersect(set, isl_set_copy(set));
3755 isl_set_free(set);
3756 if (!set)
3757 return isl_stat_error;
3758 return isl_stat_ok;
3761 /* Perform some intersection tests.
3763 static int test_intersect(isl_ctx *ctx)
3765 if (test_intersect_1(ctx) < 0)
3766 return -1;
3767 if (test_intersect_2(ctx) < 0)
3768 return -1;
3770 return 0;
3773 int test_factorize(isl_ctx *ctx)
3775 const char *str;
3776 isl_basic_set *bset;
3777 isl_factorizer *f;
3779 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3780 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3781 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3782 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3783 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3784 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3785 "3i5 >= -2i0 - i2 + 3i4 }";
3786 bset = isl_basic_set_read_from_str(ctx, str);
3787 f = isl_basic_set_factorizer(bset);
3788 isl_basic_set_free(bset);
3789 isl_factorizer_free(f);
3790 if (!f)
3791 isl_die(ctx, isl_error_unknown,
3792 "failed to construct factorizer", return -1);
3794 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3795 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3796 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3797 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3798 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3799 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3800 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3801 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3802 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3803 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3804 bset = isl_basic_set_read_from_str(ctx, str);
3805 f = isl_basic_set_factorizer(bset);
3806 isl_basic_set_free(bset);
3807 isl_factorizer_free(f);
3808 if (!f)
3809 isl_die(ctx, isl_error_unknown,
3810 "failed to construct factorizer", return -1);
3812 return 0;
3815 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3817 int *injective = user;
3819 *injective = isl_map_is_injective(map);
3820 isl_map_free(map);
3822 if (*injective < 0 || !*injective)
3823 return isl_stat_error;
3825 return isl_stat_ok;
3828 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3829 const char *r, const char *s, int tilable, int parallel)
3831 int i;
3832 isl_union_set *D;
3833 isl_union_map *W, *R, *S;
3834 isl_union_map *empty;
3835 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3836 isl_union_map *validity, *proximity, *coincidence;
3837 isl_union_map *schedule;
3838 isl_union_map *test;
3839 isl_union_set *delta;
3840 isl_union_set *domain;
3841 isl_set *delta_set;
3842 isl_set *slice;
3843 isl_set *origin;
3844 isl_schedule_constraints *sc;
3845 isl_schedule *sched;
3846 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3848 D = isl_union_set_read_from_str(ctx, d);
3849 W = isl_union_map_read_from_str(ctx, w);
3850 R = isl_union_map_read_from_str(ctx, r);
3851 S = isl_union_map_read_from_str(ctx, s);
3853 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3854 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3856 empty = isl_union_map_empty(isl_union_map_get_space(S));
3857 isl_union_map_compute_flow(isl_union_map_copy(R),
3858 isl_union_map_copy(W), empty,
3859 isl_union_map_copy(S),
3860 &dep_raw, NULL, NULL, NULL);
3861 isl_union_map_compute_flow(isl_union_map_copy(W),
3862 isl_union_map_copy(W),
3863 isl_union_map_copy(R),
3864 isl_union_map_copy(S),
3865 &dep_waw, &dep_war, NULL, NULL);
3867 dep = isl_union_map_union(dep_waw, dep_war);
3868 dep = isl_union_map_union(dep, dep_raw);
3869 validity = isl_union_map_copy(dep);
3870 coincidence = isl_union_map_copy(dep);
3871 proximity = isl_union_map_copy(dep);
3873 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3874 sc = isl_schedule_constraints_set_validity(sc, validity);
3875 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3876 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3877 sched = isl_schedule_constraints_compute_schedule(sc);
3878 schedule = isl_schedule_get_map(sched);
3879 isl_schedule_free(sched);
3880 isl_union_map_free(W);
3881 isl_union_map_free(R);
3882 isl_union_map_free(S);
3884 is_injection = 1;
3885 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3887 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3888 is_complete = isl_union_set_is_subset(D, domain);
3889 isl_union_set_free(D);
3890 isl_union_set_free(domain);
3892 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3893 test = isl_union_map_apply_range(test, dep);
3894 test = isl_union_map_apply_range(test, schedule);
3896 delta = isl_union_map_deltas(test);
3897 if (isl_union_set_n_set(delta) == 0) {
3898 is_tilable = 1;
3899 is_parallel = 1;
3900 is_nonneg = 1;
3901 isl_union_set_free(delta);
3902 } else {
3903 delta_set = isl_set_from_union_set(delta);
3905 slice = isl_set_universe(isl_set_get_space(delta_set));
3906 for (i = 0; i < tilable; ++i)
3907 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3908 is_tilable = isl_set_is_subset(delta_set, slice);
3909 isl_set_free(slice);
3911 slice = isl_set_universe(isl_set_get_space(delta_set));
3912 for (i = 0; i < parallel; ++i)
3913 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3914 is_parallel = isl_set_is_subset(delta_set, slice);
3915 isl_set_free(slice);
3917 origin = isl_set_universe(isl_set_get_space(delta_set));
3918 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3919 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3921 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3922 delta_set = isl_set_lexmin(delta_set);
3924 is_nonneg = isl_set_is_equal(delta_set, origin);
3926 isl_set_free(origin);
3927 isl_set_free(delta_set);
3930 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3931 is_injection < 0 || is_complete < 0)
3932 return -1;
3933 if (!is_complete)
3934 isl_die(ctx, isl_error_unknown,
3935 "generated schedule incomplete", return -1);
3936 if (!is_injection)
3937 isl_die(ctx, isl_error_unknown,
3938 "generated schedule not injective on each statement",
3939 return -1);
3940 if (!is_nonneg)
3941 isl_die(ctx, isl_error_unknown,
3942 "negative dependences in generated schedule",
3943 return -1);
3944 if (!is_tilable)
3945 isl_die(ctx, isl_error_unknown,
3946 "generated schedule not as tilable as expected",
3947 return -1);
3948 if (!is_parallel)
3949 isl_die(ctx, isl_error_unknown,
3950 "generated schedule not as parallel as expected",
3951 return -1);
3953 return 0;
3956 /* Compute a schedule for the given instance set, validity constraints,
3957 * proximity constraints and context and return a corresponding union map
3958 * representation.
3960 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3961 const char *domain, const char *validity, const char *proximity,
3962 const char *context)
3964 isl_set *con;
3965 isl_union_set *dom;
3966 isl_union_map *dep;
3967 isl_union_map *prox;
3968 isl_schedule_constraints *sc;
3969 isl_schedule *schedule;
3970 isl_union_map *sched;
3972 con = isl_set_read_from_str(ctx, context);
3973 dom = isl_union_set_read_from_str(ctx, domain);
3974 dep = isl_union_map_read_from_str(ctx, validity);
3975 prox = isl_union_map_read_from_str(ctx, proximity);
3976 sc = isl_schedule_constraints_on_domain(dom);
3977 sc = isl_schedule_constraints_set_context(sc, con);
3978 sc = isl_schedule_constraints_set_validity(sc, dep);
3979 sc = isl_schedule_constraints_set_proximity(sc, prox);
3980 schedule = isl_schedule_constraints_compute_schedule(sc);
3981 sched = isl_schedule_get_map(schedule);
3982 isl_schedule_free(schedule);
3984 return sched;
3987 /* Compute a schedule for the given instance set, validity constraints and
3988 * proximity constraints and return a corresponding union map representation.
3990 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3991 const char *domain, const char *validity, const char *proximity)
3993 return compute_schedule_with_context(ctx, domain, validity, proximity,
3994 "{ : }");
3997 /* Check that a schedule can be constructed on the given domain
3998 * with the given validity and proximity constraints.
4000 static int test_has_schedule(isl_ctx *ctx, const char *domain,
4001 const char *validity, const char *proximity)
4003 isl_union_map *sched;
4005 sched = compute_schedule(ctx, domain, validity, proximity);
4006 if (!sched)
4007 return -1;
4009 isl_union_map_free(sched);
4010 return 0;
4013 int test_special_schedule(isl_ctx *ctx, const char *domain,
4014 const char *validity, const char *proximity, const char *expected_sched)
4016 isl_union_map *sched1, *sched2;
4017 int equal;
4019 sched1 = compute_schedule(ctx, domain, validity, proximity);
4020 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
4022 equal = isl_union_map_is_equal(sched1, sched2);
4023 isl_union_map_free(sched1);
4024 isl_union_map_free(sched2);
4026 if (equal < 0)
4027 return -1;
4028 if (!equal)
4029 isl_die(ctx, isl_error_unknown, "unexpected schedule",
4030 return -1);
4032 return 0;
4035 /* Check that the schedule map is properly padded, i.e., that the range
4036 * lives in a single space.
4038 static int test_padded_schedule(isl_ctx *ctx)
4040 const char *str;
4041 isl_union_set *D;
4042 isl_union_map *validity, *proximity;
4043 isl_schedule_constraints *sc;
4044 isl_schedule *sched;
4045 isl_union_map *umap;
4046 isl_union_set *range;
4047 isl_set *set;
4049 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
4050 D = isl_union_set_read_from_str(ctx, str);
4051 validity = isl_union_map_empty(isl_union_set_get_space(D));
4052 proximity = isl_union_map_copy(validity);
4053 sc = isl_schedule_constraints_on_domain(D);
4054 sc = isl_schedule_constraints_set_validity(sc, validity);
4055 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4056 sched = isl_schedule_constraints_compute_schedule(sc);
4057 umap = isl_schedule_get_map(sched);
4058 isl_schedule_free(sched);
4059 range = isl_union_map_range(umap);
4060 set = isl_set_from_union_set(range);
4061 isl_set_free(set);
4063 if (!set)
4064 return -1;
4066 return 0;
4069 /* Check that conditional validity constraints are also taken into
4070 * account across bands.
4071 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
4072 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
4073 * and then check that the adjacent order constraint C[2,1]->D[2,0]
4074 * is enforced by the rest of the schedule.
4076 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
4078 const char *str;
4079 isl_union_set *domain;
4080 isl_union_map *validity, *proximity, *condition;
4081 isl_union_map *sink, *source, *dep;
4082 isl_schedule_constraints *sc;
4083 isl_schedule *schedule;
4084 isl_union_access_info *access;
4085 isl_union_flow *flow;
4086 int empty;
4088 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4089 "A[k] : k >= 1 and k <= -1 + n; "
4090 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4091 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
4092 domain = isl_union_set_read_from_str(ctx, str);
4093 sc = isl_schedule_constraints_on_domain(domain);
4094 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
4095 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4096 "D[k, i] -> C[1 + k, i] : "
4097 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4098 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
4099 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
4100 validity = isl_union_map_read_from_str(ctx, str);
4101 sc = isl_schedule_constraints_set_validity(sc, validity);
4102 str = "[n] -> { C[k, i] -> D[k, i] : "
4103 "0 <= i <= -1 + k and k <= -1 + n }";
4104 proximity = isl_union_map_read_from_str(ctx, str);
4105 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4106 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
4107 "i <= -1 + k and i >= 1 and k <= -2 + n; "
4108 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
4109 "k <= -1 + n and i >= 0 and i <= -2 + k }";
4110 condition = isl_union_map_read_from_str(ctx, str);
4111 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
4112 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4113 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
4114 "i >= 0 and i <= -1 + k and k <= -1 + n and "
4115 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
4116 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
4117 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4118 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
4119 "k <= -1 + n and i >= 0 and i <= -1 + k and "
4120 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
4121 validity = isl_union_map_read_from_str(ctx, str);
4122 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4123 validity);
4124 schedule = isl_schedule_constraints_compute_schedule(sc);
4125 str = "{ D[2,0] -> [] }";
4126 sink = isl_union_map_read_from_str(ctx, str);
4127 access = isl_union_access_info_from_sink(sink);
4128 str = "{ C[2,1] -> [] }";
4129 source = isl_union_map_read_from_str(ctx, str);
4130 access = isl_union_access_info_set_must_source(access, source);
4131 access = isl_union_access_info_set_schedule(access, schedule);
4132 flow = isl_union_access_info_compute_flow(access);
4133 dep = isl_union_flow_get_must_dependence(flow);
4134 isl_union_flow_free(flow);
4135 empty = isl_union_map_is_empty(dep);
4136 isl_union_map_free(dep);
4138 if (empty < 0)
4139 return -1;
4140 if (empty)
4141 isl_die(ctx, isl_error_unknown,
4142 "conditional validity not respected", return -1);
4144 return 0;
4147 /* Check that the test for violated conditional validity constraints
4148 * is not confused by domain compression.
4149 * In particular, earlier versions of isl would apply
4150 * a schedule on the compressed domains to the original domains,
4151 * resulting in a failure to detect that the default schedule
4152 * violates the conditional validity constraints.
4154 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
4156 const char *str;
4157 isl_bool empty;
4158 isl_union_set *domain;
4159 isl_union_map *validity, *condition;
4160 isl_schedule_constraints *sc;
4161 isl_schedule *schedule;
4162 isl_union_map *umap;
4163 isl_map *map, *ge;
4165 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
4166 domain = isl_union_set_read_from_str(ctx, str);
4167 sc = isl_schedule_constraints_on_domain(domain);
4168 str = "{ B[1, i] -> A[0, i + 1] }";
4169 condition = isl_union_map_read_from_str(ctx, str);
4170 str = "{ A[0, i] -> B[1, i - 1] }";
4171 validity = isl_union_map_read_from_str(ctx, str);
4172 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4173 isl_union_map_copy(validity));
4174 schedule = isl_schedule_constraints_compute_schedule(sc);
4175 umap = isl_schedule_get_map(schedule);
4176 isl_schedule_free(schedule);
4177 validity = isl_union_map_apply_domain(validity,
4178 isl_union_map_copy(umap));
4179 validity = isl_union_map_apply_range(validity, umap);
4180 map = isl_map_from_union_map(validity);
4181 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4182 map = isl_map_intersect(map, ge);
4183 empty = isl_map_is_empty(map);
4184 isl_map_free(map);
4186 if (empty < 0)
4187 return -1;
4188 if (!empty)
4189 isl_die(ctx, isl_error_unknown,
4190 "conditional validity constraints not satisfied",
4191 return -1);
4193 return 0;
4196 /* Input for testing of schedule construction based on
4197 * conditional constraints.
4199 * domain is the iteration domain
4200 * flow are the flow dependences, which determine the validity and
4201 * proximity constraints
4202 * condition are the conditions on the conditional validity constraints
4203 * conditional_validity are the conditional validity constraints
4204 * outer_band_n is the expected number of members in the outer band
4206 struct {
4207 const char *domain;
4208 const char *flow;
4209 const char *condition;
4210 const char *conditional_validity;
4211 int outer_band_n;
4212 } live_range_tests[] = {
4213 /* Contrived example that illustrates that we need to keep
4214 * track of tagged condition dependences and
4215 * tagged conditional validity dependences
4216 * in isl_sched_edge separately.
4217 * In particular, the conditional validity constraints on A
4218 * cannot be satisfied,
4219 * but they can be ignored because there are no corresponding
4220 * condition constraints. However, we do have an additional
4221 * conditional validity constraint that maps to the same
4222 * dependence relation
4223 * as the condition constraint on B. If we did not make a distinction
4224 * between tagged condition and tagged conditional validity
4225 * dependences, then we
4226 * could end up treating this shared dependence as an condition
4227 * constraint on A, forcing a localization of the conditions,
4228 * which is impossible.
4230 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
4231 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
4232 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
4233 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4234 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4235 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
4238 /* TACO 2013 Fig. 7 */
4239 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4240 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4241 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4242 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
4243 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
4244 "0 <= i < n and 0 <= j < n - 1 }",
4245 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
4246 "0 <= i < n and 0 <= j < j' < n;"
4247 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
4248 "0 <= i < i' < n and 0 <= j,j' < n;"
4249 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
4250 "0 <= i,j,j' < n and j < j' }",
4253 /* TACO 2013 Fig. 7, without tags */
4254 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4255 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4256 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4257 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4258 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4259 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
4260 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
4261 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
4264 /* TACO 2013 Fig. 12 */
4265 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
4266 "S3[i,3] : 0 <= i <= 1 }",
4267 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
4268 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
4269 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
4270 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
4271 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4272 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
4273 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4274 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
4275 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
4276 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
4277 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
4282 /* Test schedule construction based on conditional constraints.
4283 * In particular, check the number of members in the outer band node
4284 * as an indication of whether tiling is possible or not.
4286 static int test_conditional_schedule_constraints(isl_ctx *ctx)
4288 int i;
4289 isl_union_set *domain;
4290 isl_union_map *condition;
4291 isl_union_map *flow;
4292 isl_union_map *validity;
4293 isl_schedule_constraints *sc;
4294 isl_schedule *schedule;
4295 isl_schedule_node *node;
4296 int n_member;
4298 if (test_special_conditional_schedule_constraints(ctx) < 0)
4299 return -1;
4300 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
4301 return -1;
4303 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
4304 domain = isl_union_set_read_from_str(ctx,
4305 live_range_tests[i].domain);
4306 flow = isl_union_map_read_from_str(ctx,
4307 live_range_tests[i].flow);
4308 condition = isl_union_map_read_from_str(ctx,
4309 live_range_tests[i].condition);
4310 validity = isl_union_map_read_from_str(ctx,
4311 live_range_tests[i].conditional_validity);
4312 sc = isl_schedule_constraints_on_domain(domain);
4313 sc = isl_schedule_constraints_set_validity(sc,
4314 isl_union_map_copy(flow));
4315 sc = isl_schedule_constraints_set_proximity(sc, flow);
4316 sc = isl_schedule_constraints_set_conditional_validity(sc,
4317 condition, validity);
4318 schedule = isl_schedule_constraints_compute_schedule(sc);
4319 node = isl_schedule_get_root(schedule);
4320 while (node &&
4321 isl_schedule_node_get_type(node) != isl_schedule_node_band)
4322 node = isl_schedule_node_first_child(node);
4323 n_member = isl_schedule_node_band_n_member(node);
4324 isl_schedule_node_free(node);
4325 isl_schedule_free(schedule);
4327 if (!schedule)
4328 return -1;
4329 if (n_member != live_range_tests[i].outer_band_n)
4330 isl_die(ctx, isl_error_unknown,
4331 "unexpected number of members in outer band",
4332 return -1);
4334 return 0;
4337 /* Check that the schedule computed for the given instance set and
4338 * dependence relation strongly satisfies the dependences.
4339 * In particular, check that no instance is scheduled before
4340 * or together with an instance on which it depends.
4341 * Earlier versions of isl would produce a schedule that
4342 * only weakly satisfies the dependences.
4344 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
4346 const char *domain, *dep;
4347 isl_union_map *D, *schedule;
4348 isl_map *map, *ge;
4349 int empty;
4351 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
4352 "A[i0] : 0 <= i0 <= 1 }";
4353 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
4354 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
4355 schedule = compute_schedule(ctx, domain, dep, dep);
4356 D = isl_union_map_read_from_str(ctx, dep);
4357 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
4358 D = isl_union_map_apply_range(D, schedule);
4359 map = isl_map_from_union_map(D);
4360 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4361 map = isl_map_intersect(map, ge);
4362 empty = isl_map_is_empty(map);
4363 isl_map_free(map);
4365 if (empty < 0)
4366 return -1;
4367 if (!empty)
4368 isl_die(ctx, isl_error_unknown,
4369 "dependences not strongly satisfied", return -1);
4371 return 0;
4374 /* Compute a schedule for input where the instance set constraints
4375 * conflict with the context constraints.
4376 * Earlier versions of isl did not properly handle this situation.
4378 static int test_conflicting_context_schedule(isl_ctx *ctx)
4380 isl_union_map *schedule;
4381 const char *domain, *context;
4383 domain = "[n] -> { A[] : n >= 0 }";
4384 context = "[n] -> { : n < 0 }";
4385 schedule = compute_schedule_with_context(ctx,
4386 domain, "{}", "{}", context);
4387 isl_union_map_free(schedule);
4389 if (!schedule)
4390 return -1;
4392 return 0;
4395 /* Check that a set of schedule constraints that only allow for
4396 * a coalescing schedule still produces a schedule even if the user
4397 * request a non-coalescing schedule. Earlier versions of isl
4398 * would not handle this case correctly.
4400 static int test_coalescing_schedule(isl_ctx *ctx)
4402 const char *domain, *dep;
4403 isl_union_set *I;
4404 isl_union_map *D;
4405 isl_schedule_constraints *sc;
4406 isl_schedule *schedule;
4407 int treat_coalescing;
4409 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4410 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4411 I = isl_union_set_read_from_str(ctx, domain);
4412 D = isl_union_map_read_from_str(ctx, dep);
4413 sc = isl_schedule_constraints_on_domain(I);
4414 sc = isl_schedule_constraints_set_validity(sc, D);
4415 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4416 isl_options_set_schedule_treat_coalescing(ctx, 1);
4417 schedule = isl_schedule_constraints_compute_schedule(sc);
4418 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4419 isl_schedule_free(schedule);
4420 if (!schedule)
4421 return -1;
4422 return 0;
4425 /* Check that the scheduler does not perform any needless
4426 * compound skewing. Earlier versions of isl would compute
4427 * schedules in terms of transformed schedule coefficients and
4428 * would not accurately keep track of the sum of the original
4429 * schedule coefficients. It could then produce the schedule
4430 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
4431 * for the input below instead of the schedule below.
4433 static int test_skewing_schedule(isl_ctx *ctx)
4435 const char *D, *V, *P, *S;
4437 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
4438 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
4439 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
4440 "-1 <= a-i + b-j + c-k <= 1 }";
4441 P = "{ }";
4442 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
4444 return test_special_schedule(ctx, D, V, P, S);
4447 int test_schedule(isl_ctx *ctx)
4449 const char *D, *W, *R, *V, *P, *S;
4450 int max_coincidence;
4451 int treat_coalescing;
4453 /* Handle resulting schedule with zero bands. */
4454 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4455 return -1;
4457 /* Jacobi */
4458 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4459 W = "{ S1[t,i] -> a[t,i] }";
4460 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4461 "S1[t,i] -> a[t-1,i+1] }";
4462 S = "{ S1[t,i] -> [t,i] }";
4463 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4464 return -1;
4466 /* Fig. 5 of CC2008 */
4467 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
4468 "j <= -1 + N }";
4469 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
4470 "j >= 2 and j <= -1 + N }";
4471 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
4472 "j >= 2 and j <= -1 + N; "
4473 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
4474 "j >= 2 and j <= -1 + N }";
4475 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4476 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4477 return -1;
4479 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
4480 W = "{ S1[i] -> a[i] }";
4481 R = "{ S2[i] -> a[i+1] }";
4482 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4483 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4484 return -1;
4486 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
4487 W = "{ S1[i] -> a[i] }";
4488 R = "{ S2[i] -> a[9-i] }";
4489 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4490 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4491 return -1;
4493 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4494 W = "{ S1[i] -> a[i] }";
4495 R = "[N] -> { S2[i] -> a[N-1-i] }";
4496 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4497 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4498 return -1;
4500 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4501 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4502 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4503 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4504 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4505 return -1;
4507 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4508 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4509 R = "{ S2[i,j] -> a[i-1,j] }";
4510 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4511 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4512 return -1;
4514 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4515 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4516 R = "{ S2[i,j] -> a[i,j-1] }";
4517 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4518 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4519 return -1;
4521 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4522 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4523 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4524 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4525 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4526 "S_0[] -> [0, 0, 0] }";
4527 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4528 return -1;
4529 ctx->opt->schedule_parametric = 0;
4530 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4531 return -1;
4532 ctx->opt->schedule_parametric = 1;
4534 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4535 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4536 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4537 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4538 "S4[i] -> a[i,N] }";
4539 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4540 "S4[i] -> [4,i,0] }";
4541 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4542 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4543 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4544 return -1;
4545 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4547 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4548 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4549 "j <= N }";
4550 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4551 "j <= N; "
4552 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4553 "j <= N }";
4554 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4555 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4556 return -1;
4558 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4559 " S_2[t] : t >= 0 and t <= -1 + N; "
4560 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4561 "i <= -1 + N }";
4562 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4563 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4564 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4565 "i >= 0 and i <= -1 + N }";
4566 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4567 "i >= 0 and i <= -1 + N; "
4568 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4569 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4570 " S_0[t] -> [0, t, 0] }";
4572 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4573 return -1;
4574 ctx->opt->schedule_parametric = 0;
4575 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4576 return -1;
4577 ctx->opt->schedule_parametric = 1;
4579 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4580 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4581 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4582 return -1;
4584 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4585 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4586 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
4587 "j >= 0 and j <= -1 + N; "
4588 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4589 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4590 "j >= 0 and j <= -1 + N; "
4591 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4592 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4593 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4594 return -1;
4596 D = "{ S_0[i] : i >= 0 }";
4597 W = "{ S_0[i] -> a[i] : i >= 0 }";
4598 R = "{ S_0[i] -> a[0] : i >= 0 }";
4599 S = "{ S_0[i] -> [0, i, 0] }";
4600 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4601 return -1;
4603 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4604 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4605 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4606 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4607 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4608 return -1;
4610 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4611 "k <= -1 + n and k >= 0 }";
4612 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4613 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4614 "k <= -1 + n and k >= 0; "
4615 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4616 "k <= -1 + n and k >= 0; "
4617 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4618 "k <= -1 + n and k >= 0 }";
4619 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4620 ctx->opt->schedule_outer_coincidence = 1;
4621 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4622 return -1;
4623 ctx->opt->schedule_outer_coincidence = 0;
4625 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4626 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4627 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4628 "Stmt_for_body24[i0, i1, 1, 0]:"
4629 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4630 "Stmt_for_body7[i0, i1, i2]:"
4631 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4632 "i2 <= 7 }";
4634 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4635 "Stmt_for_body24[1, i1, i2, i3]:"
4636 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4637 "i2 >= 1;"
4638 "Stmt_for_body24[0, i1, i2, i3] -> "
4639 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4640 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4641 "i3 >= 0;"
4642 "Stmt_for_body24[0, i1, i2, i3] ->"
4643 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4644 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4645 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4646 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4647 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4648 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4649 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4650 "i1 <= 6 and i1 >= 0;"
4651 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4652 "Stmt_for_body7[i0, i1, i2] -> "
4653 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4654 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4655 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4656 "Stmt_for_body7[i0, i1, i2] -> "
4657 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4658 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4659 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4660 P = V;
4662 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4663 isl_options_set_schedule_treat_coalescing(ctx, 0);
4664 if (test_has_schedule(ctx, D, V, P) < 0)
4665 return -1;
4666 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4668 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4669 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4670 "j >= 1 and j <= 7;"
4671 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4672 "j >= 1 and j <= 8 }";
4673 P = "{ }";
4674 S = "{ S_0[i, j] -> [i + j, i] }";
4675 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4676 if (test_special_schedule(ctx, D, V, P, S) < 0)
4677 return -1;
4678 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4680 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4681 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4682 "j >= 0 and j <= -1 + i }";
4683 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4684 "i <= -1 + N and j >= 0;"
4685 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4686 "i <= -2 + N }";
4687 P = "{ }";
4688 S = "{ S_0[i, j] -> [i, j] }";
4689 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4690 if (test_special_schedule(ctx, D, V, P, S) < 0)
4691 return -1;
4692 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4694 /* Test both algorithms on a case with only proximity dependences. */
4695 D = "{ S[i,j] : 0 <= i <= 10 }";
4696 V = "{ }";
4697 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4698 S = "{ S[i, j] -> [j, i] }";
4699 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4700 if (test_special_schedule(ctx, D, V, P, S) < 0)
4701 return -1;
4702 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4703 if (test_special_schedule(ctx, D, V, P, S) < 0)
4704 return -1;
4706 D = "{ A[a]; B[] }";
4707 V = "{}";
4708 P = "{ A[a] -> B[] }";
4709 if (test_has_schedule(ctx, D, V, P) < 0)
4710 return -1;
4712 if (test_padded_schedule(ctx) < 0)
4713 return -1;
4715 /* Check that check for progress is not confused by rational
4716 * solution.
4718 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4719 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4720 "i0 <= -2 + N; "
4721 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4722 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4723 P = "{}";
4724 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4725 if (test_has_schedule(ctx, D, V, P) < 0)
4726 return -1;
4727 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4729 /* Check that we allow schedule rows that are only non-trivial
4730 * on some full-dimensional domains.
4732 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4733 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4734 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4735 P = "{}";
4736 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4737 if (test_has_schedule(ctx, D, V, P) < 0)
4738 return -1;
4739 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4741 if (test_conditional_schedule_constraints(ctx) < 0)
4742 return -1;
4744 if (test_strongly_satisfying_schedule(ctx) < 0)
4745 return -1;
4747 if (test_conflicting_context_schedule(ctx) < 0)
4748 return -1;
4750 if (test_coalescing_schedule(ctx) < 0)
4751 return -1;
4752 if (test_skewing_schedule(ctx) < 0)
4753 return -1;
4755 return 0;
4758 /* Perform scheduling tests using the whole component scheduler.
4760 static int test_schedule_whole(isl_ctx *ctx)
4762 int whole;
4763 int r;
4765 whole = isl_options_get_schedule_whole_component(ctx);
4766 isl_options_set_schedule_whole_component(ctx, 1);
4767 r = test_schedule(ctx);
4768 isl_options_set_schedule_whole_component(ctx, whole);
4770 return r;
4773 /* Perform scheduling tests using the incremental scheduler.
4775 static int test_schedule_incremental(isl_ctx *ctx)
4777 int whole;
4778 int r;
4780 whole = isl_options_get_schedule_whole_component(ctx);
4781 isl_options_set_schedule_whole_component(ctx, 0);
4782 r = test_schedule(ctx);
4783 isl_options_set_schedule_whole_component(ctx, whole);
4785 return r;
4788 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4790 isl_union_map *umap;
4791 int test;
4793 umap = isl_union_map_read_from_str(ctx, str);
4794 test = isl_union_map_plain_is_injective(umap);
4795 isl_union_map_free(umap);
4796 if (test < 0)
4797 return -1;
4798 if (test == injective)
4799 return 0;
4800 if (injective)
4801 isl_die(ctx, isl_error_unknown,
4802 "map not detected as injective", return -1);
4803 else
4804 isl_die(ctx, isl_error_unknown,
4805 "map detected as injective", return -1);
4808 int test_injective(isl_ctx *ctx)
4810 const char *str;
4812 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4813 return -1;
4814 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4815 return -1;
4816 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4817 return -1;
4818 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4819 return -1;
4820 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4821 return -1;
4822 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4823 return -1;
4824 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4825 return -1;
4826 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4827 return -1;
4829 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4830 if (test_plain_injective(ctx, str, 1))
4831 return -1;
4832 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4833 if (test_plain_injective(ctx, str, 0))
4834 return -1;
4836 return 0;
4839 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4841 isl_aff *aff2;
4842 int equal;
4844 if (!aff)
4845 return -1;
4847 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4848 equal = isl_aff_plain_is_equal(aff, aff2);
4849 isl_aff_free(aff2);
4851 return equal;
4854 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4856 int equal;
4858 equal = aff_plain_is_equal(aff, str);
4859 if (equal < 0)
4860 return -1;
4861 if (!equal)
4862 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4863 "result not as expected", return -1);
4864 return 0;
4867 /* Is "upa" obviously equal to the isl_union_pw_aff represented by "str"?
4869 static isl_bool union_pw_aff_plain_is_equal(__isl_keep isl_union_pw_aff *upa,
4870 const char *str)
4872 isl_ctx *ctx;
4873 isl_union_pw_aff *upa2;
4874 isl_bool equal;
4876 if (!upa)
4877 return isl_bool_error;
4879 ctx = isl_union_pw_aff_get_ctx(upa);
4880 upa2 = isl_union_pw_aff_read_from_str(ctx, str);
4881 equal = isl_union_pw_aff_plain_is_equal(upa, upa2);
4882 isl_union_pw_aff_free(upa2);
4884 return equal;
4887 /* Check that "upa" is obviously equal to the isl_union_pw_aff
4888 * represented by "str".
4890 static isl_stat union_pw_aff_check_plain_equal(__isl_keep isl_union_pw_aff *upa,
4891 const char *str)
4893 isl_bool equal;
4895 equal = union_pw_aff_plain_is_equal(upa, str);
4896 if (equal < 0)
4897 return isl_stat_error;
4898 if (!equal)
4899 isl_die(isl_union_pw_aff_get_ctx(upa), isl_error_unknown,
4900 "result not as expected", return isl_stat_error);
4901 return isl_stat_ok;
4904 /* Basic tests on isl_union_pw_aff.
4906 * In particular, check that isl_union_pw_aff_aff_on_domain
4907 * aligns the parameters of the input objects and
4908 * that isl_union_pw_aff_param_on_domain_id properly
4909 * introduces the parameter.
4911 static int test_upa(isl_ctx *ctx)
4913 const char *str;
4914 isl_id *id;
4915 isl_aff *aff;
4916 isl_union_set *domain;
4917 isl_union_pw_aff *upa;
4918 isl_stat ok;
4920 aff = isl_aff_read_from_str(ctx, "[N] -> { [N] }");
4921 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
4922 domain = isl_union_set_read_from_str(ctx, str);
4923 upa = isl_union_pw_aff_aff_on_domain(domain, aff);
4924 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
4925 ok = union_pw_aff_check_plain_equal(upa, str);
4926 isl_union_pw_aff_free(upa);
4927 if (ok < 0)
4928 return -1;
4930 id = isl_id_alloc(ctx, "N", NULL);
4931 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
4932 domain = isl_union_set_read_from_str(ctx, str);
4933 upa = isl_union_pw_aff_param_on_domain_id(domain, id);
4934 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
4935 ok = union_pw_aff_check_plain_equal(upa, str);
4936 isl_union_pw_aff_free(upa);
4937 if (ok < 0)
4938 return -1;
4940 return 0;
4943 struct {
4944 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4945 __isl_take isl_aff *aff2);
4946 } aff_bin_op[] = {
4947 ['+'] = { &isl_aff_add },
4948 ['-'] = { &isl_aff_sub },
4949 ['*'] = { &isl_aff_mul },
4950 ['/'] = { &isl_aff_div },
4953 struct {
4954 const char *arg1;
4955 unsigned char op;
4956 const char *arg2;
4957 const char *res;
4958 } aff_bin_tests[] = {
4959 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4960 "{ [i] -> [2i] }" },
4961 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4962 "{ [i] -> [0] }" },
4963 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4964 "{ [i] -> [2i] }" },
4965 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4966 "{ [i] -> [2i] }" },
4967 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4968 "{ [i] -> [i/2] }" },
4969 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4970 "{ [i] -> [i] }" },
4971 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4972 "{ [i] -> [NaN] }" },
4973 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
4974 "{ [i] -> [NaN] }" },
4975 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
4976 "{ [i] -> [NaN] }" },
4977 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
4978 "{ [i] -> [NaN] }" },
4979 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
4980 "{ [i] -> [NaN] }" },
4981 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
4982 "{ [i] -> [NaN] }" },
4983 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
4984 "{ [i] -> [NaN] }" },
4985 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
4986 "{ [i] -> [NaN] }" },
4987 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
4988 "{ [i] -> [NaN] }" },
4989 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
4990 "{ [i] -> [NaN] }" },
4991 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
4992 "{ [i] -> [NaN] }" },
4993 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
4994 "{ [i] -> [NaN] }" },
4997 /* Perform some basic tests of binary operations on isl_aff objects.
4999 static int test_bin_aff(isl_ctx *ctx)
5001 int i;
5002 isl_aff *aff1, *aff2, *res;
5003 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5004 __isl_take isl_aff *aff2);
5005 int ok;
5007 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
5008 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
5009 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
5010 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
5011 fn = aff_bin_op[aff_bin_tests[i].op].fn;
5012 aff1 = fn(aff1, aff2);
5013 if (isl_aff_is_nan(res))
5014 ok = isl_aff_is_nan(aff1);
5015 else
5016 ok = isl_aff_plain_is_equal(aff1, res);
5017 isl_aff_free(aff1);
5018 isl_aff_free(res);
5019 if (ok < 0)
5020 return -1;
5021 if (!ok)
5022 isl_die(ctx, isl_error_unknown,
5023 "unexpected result", return -1);
5026 return 0;
5029 struct {
5030 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
5031 __isl_take isl_pw_aff *pa2);
5032 } pw_aff_bin_op[] = {
5033 ['m'] = { &isl_pw_aff_min },
5034 ['M'] = { &isl_pw_aff_max },
5037 /* Inputs for binary isl_pw_aff operation tests.
5038 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
5039 * defined by pw_aff_bin_op, and "res" is the expected result.
5041 struct {
5042 const char *arg1;
5043 unsigned char op;
5044 const char *arg2;
5045 const char *res;
5046 } pw_aff_bin_tests[] = {
5047 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
5048 "{ [i] -> [i] }" },
5049 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
5050 "{ [i] -> [i] }" },
5051 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
5052 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
5053 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
5054 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
5055 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
5056 "{ [i] -> [NaN] }" },
5057 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
5058 "{ [i] -> [NaN] }" },
5061 /* Perform some basic tests of binary operations on isl_pw_aff objects.
5063 static int test_bin_pw_aff(isl_ctx *ctx)
5065 int i;
5066 isl_bool ok;
5067 isl_pw_aff *pa1, *pa2, *res;
5069 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
5070 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
5071 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
5072 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
5073 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
5074 if (isl_pw_aff_involves_nan(res))
5075 ok = isl_pw_aff_involves_nan(pa1);
5076 else
5077 ok = isl_pw_aff_plain_is_equal(pa1, res);
5078 isl_pw_aff_free(pa1);
5079 isl_pw_aff_free(res);
5080 if (ok < 0)
5081 return -1;
5082 if (!ok)
5083 isl_die(ctx, isl_error_unknown,
5084 "unexpected result", return -1);
5087 return 0;
5090 struct {
5091 __isl_give isl_union_pw_multi_aff *(*fn)(
5092 __isl_take isl_union_pw_multi_aff *upma1,
5093 __isl_take isl_union_pw_multi_aff *upma2);
5094 const char *arg1;
5095 const char *arg2;
5096 const char *res;
5097 } upma_bin_tests[] = {
5098 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
5099 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
5100 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
5101 "{ B[x] -> [2] : x >= 0 }",
5102 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
5103 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
5104 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
5105 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
5106 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
5107 "D[i] -> B[2] : i >= 5 }" },
5108 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5109 "{ B[x] -> C[2] : x > 0 }",
5110 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
5111 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5112 "{ B[x] -> A[2] : x >= 0 }",
5113 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
5116 /* Perform some basic tests of binary operations on
5117 * isl_union_pw_multi_aff objects.
5119 static int test_bin_upma(isl_ctx *ctx)
5121 int i;
5122 isl_union_pw_multi_aff *upma1, *upma2, *res;
5123 int ok;
5125 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
5126 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5127 upma_bin_tests[i].arg1);
5128 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5129 upma_bin_tests[i].arg2);
5130 res = isl_union_pw_multi_aff_read_from_str(ctx,
5131 upma_bin_tests[i].res);
5132 upma1 = upma_bin_tests[i].fn(upma1, upma2);
5133 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
5134 isl_union_pw_multi_aff_free(upma1);
5135 isl_union_pw_multi_aff_free(res);
5136 if (ok < 0)
5137 return -1;
5138 if (!ok)
5139 isl_die(ctx, isl_error_unknown,
5140 "unexpected result", return -1);
5143 return 0;
5146 struct {
5147 __isl_give isl_union_pw_multi_aff *(*fn)(
5148 __isl_take isl_union_pw_multi_aff *upma1,
5149 __isl_take isl_union_pw_multi_aff *upma2);
5150 const char *arg1;
5151 const char *arg2;
5152 } upma_bin_fail_tests[] = {
5153 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5154 "{ B[x] -> C[2] : x >= 0 }" },
5157 /* Perform some basic tests of binary operations on
5158 * isl_union_pw_multi_aff objects that are expected to fail.
5160 static int test_bin_upma_fail(isl_ctx *ctx)
5162 int i, n;
5163 isl_union_pw_multi_aff *upma1, *upma2;
5164 int on_error;
5166 on_error = isl_options_get_on_error(ctx);
5167 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
5168 n = ARRAY_SIZE(upma_bin_fail_tests);
5169 for (i = 0; i < n; ++i) {
5170 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5171 upma_bin_fail_tests[i].arg1);
5172 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5173 upma_bin_fail_tests[i].arg2);
5174 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
5175 isl_union_pw_multi_aff_free(upma1);
5176 if (upma1)
5177 break;
5179 isl_options_set_on_error(ctx, on_error);
5180 if (i < n)
5181 isl_die(ctx, isl_error_unknown,
5182 "operation not expected to succeed", return -1);
5184 return 0;
5187 /* Inputs for basic tests of unary operations on isl_multi_pw_aff objects.
5188 * "fn" is the function that is tested.
5189 * "arg" is a string description of the input.
5190 * "res" is a string description of the expected result.
5192 struct {
5193 __isl_give isl_multi_pw_aff *(*fn)(__isl_take isl_multi_pw_aff *mpa);
5194 const char *arg;
5195 const char *res;
5196 } mpa_un_tests[] = {
5197 { &isl_multi_pw_aff_range_factor_domain,
5198 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5199 "{ A[x] -> B[(1 : x >= 5)] }" },
5200 { &isl_multi_pw_aff_range_factor_range,
5201 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5202 "{ A[y] -> C[(2 : y <= 10)] }" },
5203 { &isl_multi_pw_aff_range_factor_domain,
5204 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5205 "{ A[x] -> B[(1 : x >= 5)] }" },
5206 { &isl_multi_pw_aff_range_factor_range,
5207 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5208 "{ A[y] -> C[] }" },
5209 { &isl_multi_pw_aff_range_factor_domain,
5210 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5211 "{ A[x] -> B[] }" },
5212 { &isl_multi_pw_aff_range_factor_range,
5213 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5214 "{ A[y] -> C[(2 : y <= 10)] }" },
5215 { &isl_multi_pw_aff_range_factor_domain,
5216 "{ A[x] -> [B[] -> C[]] }",
5217 "{ A[x] -> B[] }" },
5218 { &isl_multi_pw_aff_range_factor_range,
5219 "{ A[x] -> [B[] -> C[]] }",
5220 "{ A[y] -> C[] }" },
5221 { &isl_multi_pw_aff_factor_range,
5222 "{ [B[] -> C[]] }",
5223 "{ C[] }" },
5224 { &isl_multi_pw_aff_range_factor_domain,
5225 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5226 "{ A[x] -> B[] : x >= 0 }" },
5227 { &isl_multi_pw_aff_range_factor_range,
5228 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5229 "{ A[y] -> C[] : y >= 0 }" },
5230 { &isl_multi_pw_aff_factor_range,
5231 "[N] -> { [B[] -> C[]] : N >= 0 }",
5232 "[N] -> { C[] : N >= 0 }" },
5235 /* Perform some basic tests of unary operations on isl_multi_pw_aff objects.
5237 static int test_un_mpa(isl_ctx *ctx)
5239 int i;
5240 isl_bool ok;
5241 isl_multi_pw_aff *mpa, *res;
5243 for (i = 0; i < ARRAY_SIZE(mpa_un_tests); ++i) {
5244 mpa = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].arg);
5245 res = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].res);
5246 mpa = mpa_un_tests[i].fn(mpa);
5247 ok = isl_multi_pw_aff_plain_is_equal(mpa, res);
5248 isl_multi_pw_aff_free(mpa);
5249 isl_multi_pw_aff_free(res);
5250 if (ok < 0)
5251 return -1;
5252 if (!ok)
5253 isl_die(ctx, isl_error_unknown,
5254 "unexpected result", return -1);
5257 return 0;
5260 /* Inputs for basic tests of binary operations on isl_multi_pw_aff objects.
5261 * "fn" is the function that is tested.
5262 * "arg1" and "arg2" are string descriptions of the inputs.
5263 * "res" is a string description of the expected result.
5265 struct {
5266 __isl_give isl_multi_pw_aff *(*fn)(
5267 __isl_take isl_multi_pw_aff *mpa1,
5268 __isl_take isl_multi_pw_aff *mpa2);
5269 const char *arg1;
5270 const char *arg2;
5271 const char *res;
5272 } mpa_bin_tests[] = {
5273 { &isl_multi_pw_aff_add, "{ A[] -> [1] }", "{ A[] -> [2] }",
5274 "{ A[] -> [3] }" },
5275 { &isl_multi_pw_aff_add, "{ A[x] -> [(1 : x >= 5)] }",
5276 "{ A[x] -> [(x : x <= 10)] }",
5277 "{ A[x] -> [(1 + x : 5 <= x <= 10)] }" },
5278 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5279 "{ A[x] -> [] : x <= 10 }",
5280 "{ A[x] -> [] : 5 <= x <= 10 }" },
5281 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5282 "[N] -> { A[x] -> [] : x <= N }",
5283 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5284 { &isl_multi_pw_aff_add,
5285 "[N] -> { A[x] -> [] : x <= N }",
5286 "{ A[x] -> [] : x >= 5 }",
5287 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5288 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5289 "{ A[y] -> C[(2 : y <= 10)] }",
5290 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5291 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5292 "{ A[y] -> C[2] : y <= 10 }",
5293 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5294 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5295 "[N] -> { A[y] -> C[2] : y <= N }",
5296 "[N] -> { A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= N)]] }" },
5297 { &isl_multi_pw_aff_range_product, "[N] -> { A[x] -> B[1] : x >= N }",
5298 "{ A[y] -> C[2] : y <= 10 }",
5299 "[N] -> { A[x] -> [B[(1 : x >= N)] -> C[(2 : x <= 10)]] }" },
5300 { &isl_multi_pw_aff_range_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5301 "{ A[] -> [B[1] -> C[2]] }" },
5302 { &isl_multi_pw_aff_range_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5303 "{ A[] -> [B[] -> C[]] }" },
5304 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5305 "{ A[y] -> C[] : y <= 10 }",
5306 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] : x <= 10 }" },
5307 { &isl_multi_pw_aff_range_product, "{ A[y] -> C[] : y <= 10 }",
5308 "{ A[x] -> B[(1 : x >= 5)] }",
5309 "{ A[x] -> [C[] -> B[(1 : x >= 5)]] : x <= 10 }" },
5310 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5311 "{ A[y] -> C[(2 : y <= 10)] }",
5312 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[(2 : y <= 10)]] }" },
5313 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5314 "{ A[y] -> C[] : y <= 10 }",
5315 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= 10 }" },
5316 { &isl_multi_pw_aff_product, "{ A[y] -> C[] : y <= 10 }",
5317 "{ A[x] -> B[(1 : x >= 5)] }",
5318 "{ [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= 10 }" },
5319 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5320 "[N] -> { A[y] -> C[] : y <= N }",
5321 "[N] -> { [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= N }" },
5322 { &isl_multi_pw_aff_product, "[N] -> { A[y] -> C[] : y <= N }",
5323 "{ A[x] -> B[(1 : x >= 5)] }",
5324 "[N] -> { [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= N }" },
5325 { &isl_multi_pw_aff_product, "{ A[x] -> B[] : x >= 5 }",
5326 "{ A[y] -> C[] : y <= 10 }",
5327 "{ [A[x] -> A[y]] -> [B[] -> C[]] : x >= 5 and y <= 10 }" },
5328 { &isl_multi_pw_aff_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5329 "{ [A[] -> A[]] -> [B[1] -> C[2]] }" },
5330 { &isl_multi_pw_aff_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5331 "{ [A[] -> A[]] -> [B[] -> C[]] }" },
5332 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5333 "{ B[i,j] -> C[i + 2j] }", "{ A[a,b] -> B[b,a] }",
5334 "{ A[a,b] -> C[b + 2a] }" },
5335 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5336 "{ B[i,j] -> C[i + 2j] }",
5337 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5338 "{ A[a,b] -> C[(b + 2a : b > a)] }" },
5339 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5340 "{ B[i,j] -> C[(i + 2j : j > 4)] }",
5341 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5342 "{ A[a,b] -> C[(b + 2a : b > a > 4)] }" },
5343 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5344 "{ B[i,j] -> C[] }",
5345 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5346 "{ A[a,b] -> C[] }" },
5347 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5348 "{ B[i,j] -> C[] : i > j }",
5349 "{ A[a,b] -> B[b,a] }",
5350 "{ A[a,b] -> C[] : b > a }" },
5351 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5352 "{ B[i,j] -> C[] : j > 5 }",
5353 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5354 "{ A[a,b] -> C[] : b > a > 5 }" },
5355 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5356 "[N] -> { B[i,j] -> C[] : j > N }",
5357 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5358 "[N] -> { A[a,b] -> C[] : b > a > N }" },
5359 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5360 "[M,N] -> { B[] -> C[] : N > 5 }",
5361 "[M,N] -> { A[] -> B[] : M > N }",
5362 "[M,N] -> { A[] -> C[] : M > N > 5 }" },
5365 /* Perform some basic tests of binary operations on isl_multi_pw_aff objects.
5367 static int test_bin_mpa(isl_ctx *ctx)
5369 int i;
5370 isl_bool ok;
5371 isl_multi_pw_aff *mpa1, *mpa2, *res;
5373 for (i = 0; i < ARRAY_SIZE(mpa_bin_tests); ++i) {
5374 mpa1 = isl_multi_pw_aff_read_from_str(ctx,
5375 mpa_bin_tests[i].arg1);
5376 mpa2 = isl_multi_pw_aff_read_from_str(ctx,
5377 mpa_bin_tests[i].arg2);
5378 res = isl_multi_pw_aff_read_from_str(ctx,
5379 mpa_bin_tests[i].res);
5380 mpa1 = mpa_bin_tests[i].fn(mpa1, mpa2);
5381 ok = isl_multi_pw_aff_plain_is_equal(mpa1, res);
5382 isl_multi_pw_aff_free(mpa1);
5383 isl_multi_pw_aff_free(res);
5384 if (ok < 0)
5385 return -1;
5386 if (!ok)
5387 isl_die(ctx, isl_error_unknown,
5388 "unexpected result", return -1);
5391 return 0;
5394 /* Inputs for basic tests of unary operations on
5395 * isl_multi_union_pw_aff objects.
5396 * "fn" is the function that is tested.
5397 * "arg" is a string description of the input.
5398 * "res" is a string description of the expected result.
5400 struct {
5401 __isl_give isl_multi_union_pw_aff *(*fn)(
5402 __isl_take isl_multi_union_pw_aff *mupa);
5403 const char *arg;
5404 const char *res;
5405 } mupa_un_tests[] = {
5406 { &isl_multi_union_pw_aff_factor_range,
5407 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]",
5408 "C[{ A[] -> [2] }]" },
5409 { &isl_multi_union_pw_aff_factor_range,
5410 "[B[] -> C[{ A[] -> [2] }]]",
5411 "C[{ A[] -> [2] }]" },
5412 { &isl_multi_union_pw_aff_factor_range,
5413 "[B[{ A[] -> [1] }] -> C[]]",
5414 "C[]" },
5415 { &isl_multi_union_pw_aff_factor_range,
5416 "[B[] -> C[]]",
5417 "C[]" },
5418 { &isl_multi_union_pw_aff_factor_range,
5419 "([B[] -> C[]] : { A[x] : x >= 0 })",
5420 "(C[] : { A[x] : x >= 0 })" },
5421 { &isl_multi_union_pw_aff_factor_range,
5422 "[N] -> ([B[] -> C[]] : { A[x] : x <= N })",
5423 "[N] -> (C[] : { A[x] : x <= N })" },
5424 { &isl_multi_union_pw_aff_factor_range,
5425 "[N] -> ([B[] -> C[]] : { : N >= 0 })",
5426 "[N] -> (C[] : { : N >= 0 })" },
5429 /* Perform some basic tests of unary operations on
5430 * isl_multi_union_pw_aff objects.
5432 static int test_un_mupa(isl_ctx *ctx)
5434 int i;
5435 isl_bool ok;
5436 isl_multi_union_pw_aff *mupa, *res;
5438 for (i = 0; i < ARRAY_SIZE(mupa_un_tests); ++i) {
5439 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5440 mupa_un_tests[i].arg);
5441 res = isl_multi_union_pw_aff_read_from_str(ctx,
5442 mupa_un_tests[i].res);
5443 mupa = mupa_un_tests[i].fn(mupa);
5444 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5445 isl_multi_union_pw_aff_free(mupa);
5446 isl_multi_union_pw_aff_free(res);
5447 if (ok < 0)
5448 return -1;
5449 if (!ok)
5450 isl_die(ctx, isl_error_unknown,
5451 "unexpected result", return -1);
5454 return 0;
5457 /* Inputs for basic tests of binary operations on
5458 * isl_multi_union_pw_aff objects.
5459 * "fn" is the function that is tested.
5460 * "arg1" and "arg2" are string descriptions of the inputs.
5461 * "res" is a string description of the expected result.
5463 struct {
5464 __isl_give isl_multi_union_pw_aff *(*fn)(
5465 __isl_take isl_multi_union_pw_aff *mupa1,
5466 __isl_take isl_multi_union_pw_aff *mupa2);
5467 const char *arg1;
5468 const char *arg2;
5469 const char *res;
5470 } mupa_bin_tests[] = {
5471 { &isl_multi_union_pw_aff_add, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5472 "[{ A[] -> [3] }]" },
5473 { &isl_multi_union_pw_aff_sub, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5474 "[{ A[] -> [-1] }]" },
5475 { &isl_multi_union_pw_aff_add,
5476 "[{ A[] -> [1]; B[] -> [4] }]",
5477 "[{ A[] -> [2]; C[] -> [5] }]",
5478 "[{ A[] -> [3] }]" },
5479 { &isl_multi_union_pw_aff_union_add,
5480 "[{ A[] -> [1]; B[] -> [4] }]",
5481 "[{ A[] -> [2]; C[] -> [5] }]",
5482 "[{ A[] -> [3]; B[] -> [4]; C[] -> [5] }]" },
5483 { &isl_multi_union_pw_aff_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5484 "[{ A[x] -> [(x)] : x <= 10 }]",
5485 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10 }]" },
5486 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5487 "([] : { A[x] : x <= 10 })",
5488 "([] : { A[x] : 5 <= x <= 10 })" },
5489 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5490 "[N] -> ([] : { A[x] : x <= N })",
5491 "[N] -> ([] : { A[x] : 5 <= x <= N })" },
5492 { &isl_multi_union_pw_aff_add, "[N] -> ([] : { A[x] : x >= N })",
5493 "([] : { A[x] : x <= 10 })",
5494 "[N] -> ([] : { A[x] : N <= x <= 10 })" },
5495 { &isl_multi_union_pw_aff_union_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5496 "[{ A[x] -> [(x)] : x <= 10 }]",
5497 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10; "
5498 "A[x] -> [(1)] : x > 10; A[x] -> [(x)] : x < 5 }]" },
5499 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 5 })",
5500 "([] : { A[x] : x <= 10 })",
5501 "([] : { A[x] })" },
5502 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 0 })",
5503 "[N] -> ([] : { A[x] : x >= N })",
5504 "[N] -> ([] : { A[x] : x >= 0 or x >= N })" },
5505 { &isl_multi_union_pw_aff_union_add,
5506 "[N] -> ([] : { A[] : N >= 0})",
5507 "[N] -> ([] : { A[] : N <= 0})",
5508 "[N] -> ([] : { A[] })" },
5509 { &isl_multi_union_pw_aff_union_add,
5510 "[N] -> ([] : { A[] })",
5511 "[N] -> ([] : { : })",
5512 "[N] -> ([] : { : })" },
5513 { &isl_multi_union_pw_aff_union_add,
5514 "[N] -> ([] : { : })",
5515 "[N] -> ([] : { A[] })",
5516 "[N] -> ([] : { : })" },
5517 { &isl_multi_union_pw_aff_union_add,
5518 "[N] -> ([] : { : N >= 0})",
5519 "[N] -> ([] : { : N <= 0})",
5520 "[N] -> ([] : { : })" },
5521 { &isl_multi_union_pw_aff_range_product,
5522 "B[{ A[] -> [1] }]",
5523 "C[{ A[] -> [2] }]",
5524 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]" },
5525 { &isl_multi_union_pw_aff_range_product,
5526 "(B[] : { A[x] : x >= 5 })",
5527 "(C[] : { A[x] : x <= 10 })",
5528 "([B[] -> C[]] : { A[x] : 5 <= x <= 10 })" },
5529 { &isl_multi_union_pw_aff_range_product,
5530 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5531 "(C[] : { A[x] : x <= 10 })",
5532 "[B[{ A[x] -> [x + 1] : 5 <= x <= 10 }] -> C[]]" },
5533 { &isl_multi_union_pw_aff_range_product,
5534 "(C[] : { A[x] : x <= 10 })",
5535 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5536 "[C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= 10 }]]" },
5537 { &isl_multi_union_pw_aff_range_product,
5538 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5539 "[N] -> (C[] : { A[x] : x <= N })",
5540 "[N] -> [B[{ A[x] -> [x + 1] : 5 <= x <= N }] -> C[]]" },
5541 { &isl_multi_union_pw_aff_range_product,
5542 "[N] -> (C[] : { A[x] : x <= N })",
5543 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5544 "[N] -> [C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= N }]]" },
5545 { &isl_multi_union_pw_aff_range_product,
5546 "B[{ A[] -> [1]; D[] -> [3] }]",
5547 "C[{ A[] -> [2] }]",
5548 "[B[{ A[] -> [1]; D[] -> [3] }] -> C[{ A[] -> [2] }]]" },
5549 { &isl_multi_union_pw_aff_range_product,
5550 "B[] }]",
5551 "(C[] : { A[x] })",
5552 "([B[] -> C[]] : { A[x] })" },
5553 { &isl_multi_union_pw_aff_range_product,
5554 "(B[] : { A[x] })",
5555 "C[] }]",
5556 "([B[] -> C[]] : { A[x] })" },
5559 /* Perform some basic tests of binary operations on
5560 * isl_multi_union_pw_aff objects.
5562 static int test_bin_mupa(isl_ctx *ctx)
5564 int i;
5565 isl_bool ok;
5566 isl_multi_union_pw_aff *mupa1, *mupa2, *res;
5568 for (i = 0; i < ARRAY_SIZE(mupa_bin_tests); ++i) {
5569 mupa1 = isl_multi_union_pw_aff_read_from_str(ctx,
5570 mupa_bin_tests[i].arg1);
5571 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx,
5572 mupa_bin_tests[i].arg2);
5573 res = isl_multi_union_pw_aff_read_from_str(ctx,
5574 mupa_bin_tests[i].res);
5575 mupa1 = mupa_bin_tests[i].fn(mupa1, mupa2);
5576 ok = isl_multi_union_pw_aff_plain_is_equal(mupa1, res);
5577 isl_multi_union_pw_aff_free(mupa1);
5578 isl_multi_union_pw_aff_free(res);
5579 if (ok < 0)
5580 return -1;
5581 if (!ok)
5582 isl_die(ctx, isl_error_unknown,
5583 "unexpected result", return -1);
5586 return 0;
5589 /* Inputs for basic tests of binary operations on
5590 * pairs of isl_multi_union_pw_aff and isl_set objects.
5591 * "fn" is the function that is tested.
5592 * "arg1" and "arg2" are string descriptions of the inputs.
5593 * "res" is a string description of the expected result.
5595 struct {
5596 __isl_give isl_multi_union_pw_aff *(*fn)(
5597 __isl_take isl_multi_union_pw_aff *mupa,
5598 __isl_take isl_set *set);
5599 const char *arg1;
5600 const char *arg2;
5601 const char *res;
5602 } mupa_set_tests[] = {
5603 { &isl_multi_union_pw_aff_intersect_range,
5604 "C[{ B[i,j] -> [i + 2j] }]", "{ C[1] }",
5605 "C[{ B[i,j] -> [i + 2j] : i + 2j = 1 }]" },
5606 { &isl_multi_union_pw_aff_intersect_range,
5607 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[N] }",
5608 "[N] -> C[{ B[i,j] -> [i + 2j] : i + 2j = N }]" },
5609 { &isl_multi_union_pw_aff_intersect_range,
5610 "[N] -> C[{ B[i,j] -> [i + 2j + N] }]", "{ C[1] }",
5611 "[N] -> C[{ B[i,j] -> [i + 2j + N] : i + 2j + N = 1 }]" },
5612 { &isl_multi_union_pw_aff_intersect_range,
5613 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[x] : N >= 0 }",
5614 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0 }]" },
5615 { &isl_multi_union_pw_aff_intersect_range,
5616 "C[]", "{ C[] }", "C[]" },
5617 { &isl_multi_union_pw_aff_intersect_range,
5618 "[N] -> (C[] : { : N >= 0 })",
5619 "{ C[] }",
5620 "[N] -> (C[] : { : N >= 0 })" },
5621 { &isl_multi_union_pw_aff_intersect_range,
5622 "(C[] : { A[a,b] })",
5623 "{ C[] }",
5624 "(C[] : { A[a,b] })" },
5625 { &isl_multi_union_pw_aff_intersect_range,
5626 "[N] -> (C[] : { A[a,b] : a,b <= N })",
5627 "{ C[] }",
5628 "[N] -> (C[] : { A[a,b] : a,b <= N })" },
5629 { &isl_multi_union_pw_aff_intersect_range,
5630 "C[]",
5631 "[N] -> { C[] : N >= 0 }",
5632 "[N] -> (C[] : { : N >= 0 })" },
5633 { &isl_multi_union_pw_aff_intersect_range,
5634 "(C[] : { A[a,b] })",
5635 "[N] -> { C[] : N >= 0 }",
5636 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5637 { &isl_multi_union_pw_aff_intersect_range,
5638 "[N] -> (C[] : { : N >= 0 })",
5639 "[N] -> { C[] : N < 1024 }",
5640 "[N] -> (C[] : { : 0 <= N < 1024 })" },
5641 { &isl_multi_union_pw_aff_intersect_params,
5642 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { : N >= 0 }",
5643 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0}]" },
5644 { &isl_multi_union_pw_aff_intersect_params,
5645 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "[N] -> { : N >= 0 }",
5646 "[N] -> C[{ B[i,j] -> [i + 2j] : 0 <= N <= 256 }]" },
5647 { &isl_multi_union_pw_aff_intersect_params,
5648 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "{ : }",
5649 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]" },
5650 { &isl_multi_union_pw_aff_intersect_params,
5651 "C[]", "[N] -> { : N >= 0 }",
5652 "[N] -> (C[] : { : N >= 0 })" },
5653 { &isl_multi_union_pw_aff_intersect_params,
5654 "(C[] : { A[a,b] })", "[N] -> { : N >= 0 }",
5655 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5656 { &isl_multi_union_pw_aff_intersect_params,
5657 "[N] -> (C[] : { A[a,N] })", "{ : }",
5658 "[N] -> (C[] : { A[a,N] })" },
5659 { &isl_multi_union_pw_aff_intersect_params,
5660 "[N] -> (C[] : { A[a,b] : N <= 256 })", "[N] -> { : N >= 0 }",
5661 "[N] -> (C[] : { A[a,b] : 0 <= N <= 256 })" },
5664 /* Perform some basic tests of binary operations on
5665 * pairs of isl_multi_union_pw_aff and isl_set objects.
5667 static int test_mupa_set(isl_ctx *ctx)
5669 int i;
5670 isl_bool ok;
5671 isl_multi_union_pw_aff *mupa, *res;
5672 isl_set *set;
5674 for (i = 0; i < ARRAY_SIZE(mupa_set_tests); ++i) {
5675 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5676 mupa_set_tests[i].arg1);
5677 set = isl_set_read_from_str(ctx, mupa_set_tests[i].arg2);
5678 res = isl_multi_union_pw_aff_read_from_str(ctx,
5679 mupa_set_tests[i].res);
5680 mupa = mupa_set_tests[i].fn(mupa, set);
5681 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5682 isl_multi_union_pw_aff_free(mupa);
5683 isl_multi_union_pw_aff_free(res);
5684 if (ok < 0)
5685 return -1;
5686 if (!ok)
5687 isl_die(ctx, isl_error_unknown,
5688 "unexpected result", return -1);
5691 return 0;
5694 /* Inputs for basic tests of binary operations on
5695 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5696 * "fn" is the function that is tested.
5697 * "arg1" and "arg2" are string descriptions of the inputs.
5698 * "res" is a string description of the expected result.
5700 struct {
5701 __isl_give isl_multi_union_pw_aff *(*fn)(
5702 __isl_take isl_multi_union_pw_aff *mupa,
5703 __isl_take isl_union_set *uset);
5704 const char *arg1;
5705 const char *arg2;
5706 const char *res;
5707 } mupa_uset_tests[] = {
5708 { &isl_multi_union_pw_aff_intersect_domain,
5709 "C[{ B[i,j] -> [i + 2j] }]", "{ B[i,i] }",
5710 "C[{ B[i,i] -> [3i] }]" },
5711 { &isl_multi_union_pw_aff_intersect_domain,
5712 "(C[] : { B[i,j] })", "{ B[i,i] }",
5713 "(C[] : { B[i,i] })" },
5714 { &isl_multi_union_pw_aff_intersect_domain,
5715 "(C[] : { B[i,j] })", "[N] -> { B[N,N] }",
5716 "[N] -> (C[] : { B[N,N] })" },
5717 { &isl_multi_union_pw_aff_intersect_domain,
5718 "C[]", "{ B[i,i] }",
5719 "(C[] : { B[i,i] })" },
5720 { &isl_multi_union_pw_aff_intersect_domain,
5721 "[N] -> (C[] : { : N >= 0 })", "{ B[i,i] }",
5722 "[N] -> (C[] : { B[i,i] : N >= 0 })" },
5725 /* Perform some basic tests of binary operations on
5726 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5728 static int test_mupa_uset(isl_ctx *ctx)
5730 int i;
5731 isl_bool ok;
5732 isl_multi_union_pw_aff *mupa, *res;
5733 isl_union_set *uset;
5735 for (i = 0; i < ARRAY_SIZE(mupa_uset_tests); ++i) {
5736 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5737 mupa_uset_tests[i].arg1);
5738 uset = isl_union_set_read_from_str(ctx,
5739 mupa_uset_tests[i].arg2);
5740 res = isl_multi_union_pw_aff_read_from_str(ctx,
5741 mupa_uset_tests[i].res);
5742 mupa = mupa_uset_tests[i].fn(mupa, uset);
5743 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5744 isl_multi_union_pw_aff_free(mupa);
5745 isl_multi_union_pw_aff_free(res);
5746 if (ok < 0)
5747 return -1;
5748 if (!ok)
5749 isl_die(ctx, isl_error_unknown,
5750 "unexpected result", return -1);
5753 return 0;
5756 /* Inputs for basic tests of binary operations on
5757 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5758 * "fn" is the function that is tested.
5759 * "arg1" and "arg2" are string descriptions of the inputs.
5760 * "res" is a string description of the expected result.
5762 struct {
5763 __isl_give isl_multi_union_pw_aff *(*fn)(
5764 __isl_take isl_multi_union_pw_aff *mupa,
5765 __isl_take isl_multi_aff *ma);
5766 const char *arg1;
5767 const char *arg2;
5768 const char *res;
5769 } mupa_ma_tests[] = {
5770 { &isl_multi_union_pw_aff_apply_multi_aff,
5771 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5772 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5773 "{ C[a,b] -> D[b,a] }",
5774 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
5775 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
5776 { &isl_multi_union_pw_aff_apply_multi_aff,
5777 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
5778 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5779 "{ C[a,b] -> D[b,a] }",
5780 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
5781 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
5782 { &isl_multi_union_pw_aff_apply_multi_aff,
5783 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5784 "[N] -> { C[a] -> D[a + N] }",
5785 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }] " },
5786 { &isl_multi_union_pw_aff_apply_multi_aff,
5787 "C[]",
5788 "{ C[] -> D[] }",
5789 "D[]" },
5790 { &isl_multi_union_pw_aff_apply_multi_aff,
5791 "[N] -> (C[] : { : N >= 0 })",
5792 "{ C[] -> D[] }",
5793 "[N] -> (D[] : { : N >= 0 })" },
5794 { &isl_multi_union_pw_aff_apply_multi_aff,
5795 "C[]",
5796 "[N] -> { C[] -> D[N] }",
5797 "[N] -> D[{ [N] }]" },
5798 { &isl_multi_union_pw_aff_apply_multi_aff,
5799 "(C[] : { A[i,j] : i >= j })",
5800 "{ C[] -> D[] }",
5801 "(D[] : { A[i,j] : i >= j })" },
5802 { &isl_multi_union_pw_aff_apply_multi_aff,
5803 "[N] -> (C[] : { A[i,j] : N >= 0 })",
5804 "{ C[] -> D[] }",
5805 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
5806 { &isl_multi_union_pw_aff_apply_multi_aff,
5807 "(C[] : { A[i,j] : i >= j })",
5808 "[N] -> { C[] -> D[N] }",
5809 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
5812 /* Perform some basic tests of binary operations on
5813 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5815 static int test_mupa_ma(isl_ctx *ctx)
5817 int i;
5818 isl_bool ok;
5819 isl_multi_union_pw_aff *mupa, *res;
5820 isl_multi_aff *ma;
5822 for (i = 0; i < ARRAY_SIZE(mupa_ma_tests); ++i) {
5823 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5824 mupa_ma_tests[i].arg1);
5825 ma = isl_multi_aff_read_from_str(ctx, mupa_ma_tests[i].arg2);
5826 res = isl_multi_union_pw_aff_read_from_str(ctx,
5827 mupa_ma_tests[i].res);
5828 mupa = mupa_ma_tests[i].fn(mupa, ma);
5829 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5830 isl_multi_union_pw_aff_free(mupa);
5831 isl_multi_union_pw_aff_free(res);
5832 if (ok < 0)
5833 return -1;
5834 if (!ok)
5835 isl_die(ctx, isl_error_unknown,
5836 "unexpected result", return -1);
5839 return 0;
5842 /* Inputs for basic tests of binary operations on
5843 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5844 * "fn" is the function that is tested.
5845 * "arg1" and "arg2" are string descriptions of the inputs.
5846 * "res" is a string description of the expected result.
5848 struct {
5849 __isl_give isl_union_pw_aff *(*fn)(
5850 __isl_take isl_multi_union_pw_aff *mupa,
5851 __isl_take isl_pw_aff *pa);
5852 const char *arg1;
5853 const char *arg2;
5854 const char *res;
5855 } mupa_pa_tests[] = {
5856 { &isl_multi_union_pw_aff_apply_pw_aff,
5857 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5858 "[N] -> { C[a] -> [a + N] }",
5859 "[N] -> { A[i,j] -> [i + N]; B[i,j] -> [j + N] }" },
5860 { &isl_multi_union_pw_aff_apply_pw_aff,
5861 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5862 "{ C[a] -> [a] : a >= 0; C[a] -> [-a] : a < 0 }",
5863 "{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
5864 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }" },
5865 { &isl_multi_union_pw_aff_apply_pw_aff,
5866 "C[]",
5867 "[N] -> { C[] -> [N] }",
5868 "[N] -> { [N] }" },
5869 { &isl_multi_union_pw_aff_apply_pw_aff,
5870 "C[]",
5871 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
5872 "[N] -> { [N] : N >= 0; [-N] : N < 0 }" },
5873 { &isl_multi_union_pw_aff_apply_pw_aff,
5874 "[N] -> (C[] : { : N >= 0 })",
5875 "[N] -> { C[] -> [N] }",
5876 "[N] -> { [N] : N >= 0 }" },
5877 { &isl_multi_union_pw_aff_apply_pw_aff,
5878 "[N] -> (C[] : { : N >= 0 })",
5879 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
5880 "[N] -> { [N] : N >= 0 }" },
5881 { &isl_multi_union_pw_aff_apply_pw_aff,
5882 "[N] -> (C[] : { : N >= 0 })",
5883 "{ C[] -> [0] }",
5884 "[N] -> { [0] : N >= 0 }" },
5885 { &isl_multi_union_pw_aff_apply_pw_aff,
5886 "(C[] : { A[i,j] : i >= j })",
5887 "[N] -> { C[] -> [N] }",
5888 "[N] -> { A[i,j] -> [N] : i >= j }" },
5889 { &isl_multi_union_pw_aff_apply_pw_aff,
5890 "(C[] : { A[i,j] : i >= j })",
5891 "[N] -> { C[] -> [N] : N >= 0 }",
5892 "[N] -> { A[i,j] -> [N] : i >= j and N >= 0 }" },
5895 /* Perform some basic tests of binary operations on
5896 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5898 static int test_mupa_pa(isl_ctx *ctx)
5900 int i;
5901 isl_bool ok;
5902 isl_multi_union_pw_aff *mupa;
5903 isl_union_pw_aff *upa, *res;
5904 isl_pw_aff *pa;
5906 for (i = 0; i < ARRAY_SIZE(mupa_pa_tests); ++i) {
5907 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5908 mupa_pa_tests[i].arg1);
5909 pa = isl_pw_aff_read_from_str(ctx, mupa_pa_tests[i].arg2);
5910 res = isl_union_pw_aff_read_from_str(ctx,
5911 mupa_pa_tests[i].res);
5912 upa = mupa_pa_tests[i].fn(mupa, pa);
5913 ok = isl_union_pw_aff_plain_is_equal(upa, res);
5914 isl_union_pw_aff_free(upa);
5915 isl_union_pw_aff_free(res);
5916 if (ok < 0)
5917 return -1;
5918 if (!ok)
5919 isl_die(ctx, isl_error_unknown,
5920 "unexpected result", return -1);
5923 return 0;
5926 /* Inputs for basic tests of binary operations on
5927 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
5928 * "fn" is the function that is tested.
5929 * "arg1" and "arg2" are string descriptions of the inputs.
5930 * "res" is a string description of the expected result.
5932 struct {
5933 __isl_give isl_multi_union_pw_aff *(*fn)(
5934 __isl_take isl_multi_union_pw_aff *mupa,
5935 __isl_take isl_pw_multi_aff *pma);
5936 const char *arg1;
5937 const char *arg2;
5938 const char *res;
5939 } mupa_pma_tests[] = {
5940 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5941 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5942 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5943 "{ C[a,b] -> D[b,a] }",
5944 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
5945 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
5946 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5947 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
5948 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5949 "{ C[a,b] -> D[b,a] }",
5950 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
5951 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
5952 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5953 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5954 "[N] -> { C[a] -> D[a + N] }",
5955 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }]" },
5956 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5957 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5958 "{ C[a] -> D[a] : a >= 0; C[a] -> D[-a] : a < 0 }",
5959 "D[{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
5960 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }]" },
5961 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5962 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5963 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5964 "{ C[a,b] -> D[a,b] : a >= b; C[a,b] -> D[b,a] : a < b }",
5965 "D[{ A[i,j] -> [i] : i >= j; A[i,j] -> [j] : i < j; "
5966 "B[i,j] -> [j] : i <= j; B[i,j] -> [i] : i > j }, "
5967 "{ A[i,j] -> [j] : i >= j; A[i,j] -> [i] : i < j; "
5968 "B[i,j] -> [i] : i <= j; B[i,j] -> [j] : i > j }]" },
5969 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5970 "C[]",
5971 "{ C[] -> D[] }",
5972 "D[]" },
5973 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5974 "[N] -> (C[] : { : N >= 0 })",
5975 "{ C[] -> D[] }",
5976 "[N] -> (D[] : { : N >= 0 })" },
5977 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5978 "C[]",
5979 "[N] -> { C[] -> D[N] }",
5980 "[N] -> D[{ [N] }]" },
5981 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5982 "(C[] : { A[i,j] : i >= j })",
5983 "{ C[] -> D[] }",
5984 "(D[] : { A[i,j] : i >= j })" },
5985 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5986 "[N] -> (C[] : { A[i,j] : N >= 0 })",
5987 "{ C[] -> D[] }",
5988 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
5989 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5990 "(C[] : { A[i,j] : i >= j })",
5991 "[N] -> { C[] -> D[N] }",
5992 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
5993 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5994 "C[]",
5995 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
5996 "[N] -> D[{ [N] : N >= 0; [-N] : N < 0 }]" },
5997 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5998 "[N] -> (C[] : { : N >= 0 })",
5999 "[N] -> { C[] -> D[N] }",
6000 "[N] -> D[{ [N] : N >= 0 }]" },
6001 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6002 "[N] -> (C[] : { : N >= 0 })",
6003 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6004 "[N] -> D[{ [N] : N >= 0 }]" },
6005 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6006 "[N] -> (C[] : { : N >= 0 })",
6007 "{ C[] -> D[0] }",
6008 "[N] -> D[{ [0] : N >= 0 }]" },
6009 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6010 "(C[] : { A[i,j] : i >= j })",
6011 "[N] -> { C[] -> D[N] : N >= 0 }",
6012 "[N] -> D[{ A[i,j] -> [N] : i >= j and N >= 0 }]" },
6015 /* Perform some basic tests of binary operations on
6016 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6018 static int test_mupa_pma(isl_ctx *ctx)
6020 int i;
6021 isl_bool ok;
6022 isl_multi_union_pw_aff *mupa, *res;
6023 isl_pw_multi_aff *pma;
6025 for (i = 0; i < ARRAY_SIZE(mupa_pma_tests); ++i) {
6026 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6027 mupa_pma_tests[i].arg1);
6028 pma = isl_pw_multi_aff_read_from_str(ctx,
6029 mupa_pma_tests[i].arg2);
6030 res = isl_multi_union_pw_aff_read_from_str(ctx,
6031 mupa_pma_tests[i].res);
6032 mupa = mupa_pma_tests[i].fn(mupa, pma);
6033 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6034 isl_multi_union_pw_aff_free(mupa);
6035 isl_multi_union_pw_aff_free(res);
6036 if (ok < 0)
6037 return -1;
6038 if (!ok)
6039 isl_die(ctx, isl_error_unknown,
6040 "unexpected result", return -1);
6043 return 0;
6046 /* Inputs for basic tests of binary operations on
6047 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6048 * "fn" is the function that is tested.
6049 * "arg1" and "arg2" are string descriptions of the inputs.
6050 * "res" is a string description of the expected result.
6052 struct {
6053 __isl_give isl_multi_union_pw_aff *(*fn)(
6054 __isl_take isl_multi_union_pw_aff *mupa,
6055 __isl_take isl_union_pw_multi_aff *upma);
6056 const char *arg1;
6057 const char *arg2;
6058 const char *res;
6059 } mupa_upma_tests[] = {
6060 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6061 "C[{ B[i,j] -> [i + 2j] }]", "{ A[a,b] -> B[b,a] }",
6062 "C[{ A[a,b] -> [b + 2a] }]" },
6063 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6064 "C[{ B[i,j] -> [i + 2j] }]",
6065 "{ A[a,b] -> B[b,a] : b > a }",
6066 "C[{ A[a,b] -> [b + 2a] : b > a }]" },
6067 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6068 "C[{ B[i,j] -> [i + 2j] : j > 4 }]",
6069 "{ A[a,b] -> B[b,a] : b > a }",
6070 "C[{ A[a,b] -> [b + 2a] : b > a > 4 }]" },
6071 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6072 "C[{ B[i,j] -> [i + 2j] }]",
6073 "{ A[a,b] -> B[b,a] : a > b; A[a,b] -> B[a,b] : a <= b }",
6074 "C[{ A[a,b] -> [b + 2a] : a > b; A[a,b] -> [a + 2b] : a <= b }]" },
6075 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6076 "(C[] : { B[a,b] })",
6077 "{ A[a,b] -> B[b,a] }",
6078 "(C[] : { A[a,b] })" },
6079 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6080 "(C[] : { B[a,b] })",
6081 "{ B[a,b] -> A[b,a] }",
6082 "(C[] : { })" },
6083 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6084 "(C[] : { B[a,b] })",
6085 "{ A[a,b] -> B[b,a] : a > b }",
6086 "(C[] : { A[a,b] : a > b })" },
6087 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6088 "(C[] : { B[a,b] : a > b })",
6089 "{ A[a,b] -> B[b,a] }",
6090 "(C[] : { A[a,b] : b > a })" },
6091 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6092 "[N] -> (C[] : { B[a,b] : a > N })",
6093 "{ A[a,b] -> B[b,a] : a > b }",
6094 "[N] -> (C[] : { A[a,b] : a > b > N })" },
6095 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6096 "(C[] : { B[a,b] : a > b })",
6097 "[N] -> { A[a,b] -> B[b,a] : a > N }",
6098 "[N] -> (C[] : { A[a,b] : b > a > N })" },
6099 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6100 "C[]",
6101 "{ A[a,b] -> B[b,a] }",
6102 "C[]" },
6103 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6104 "[N] -> (C[] : { : N >= 0 })",
6105 "{ A[a,b] -> B[b,a] }",
6106 "[N] -> (C[] : { : N >= 0 })" },
6107 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6108 "C[]",
6109 "[N] -> { A[a,b] -> B[b,a] : N >= 0 }",
6110 "[N] -> (C[] : { : N >= 0 })" },
6113 /* Perform some basic tests of binary operations on
6114 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6116 static int test_mupa_upma(isl_ctx *ctx)
6118 int i;
6119 isl_bool ok;
6120 isl_multi_union_pw_aff *mupa, *res;
6121 isl_union_pw_multi_aff *upma;
6123 for (i = 0; i < ARRAY_SIZE(mupa_upma_tests); ++i) {
6124 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6125 mupa_upma_tests[i].arg1);
6126 upma = isl_union_pw_multi_aff_read_from_str(ctx,
6127 mupa_upma_tests[i].arg2);
6128 res = isl_multi_union_pw_aff_read_from_str(ctx,
6129 mupa_upma_tests[i].res);
6130 mupa = mupa_upma_tests[i].fn(mupa, upma);
6131 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6132 isl_multi_union_pw_aff_free(mupa);
6133 isl_multi_union_pw_aff_free(res);
6134 if (ok < 0)
6135 return -1;
6136 if (!ok)
6137 isl_die(ctx, isl_error_unknown,
6138 "unexpected result", return -1);
6141 return 0;
6144 /* Check that the input tuple of an isl_aff can be set properly.
6146 static isl_stat test_aff_set_tuple_id(isl_ctx *ctx)
6148 isl_id *id;
6149 isl_aff *aff;
6150 int equal;
6152 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x + 1] }");
6153 id = isl_id_alloc(ctx, "A", NULL);
6154 aff = isl_aff_set_tuple_id(aff, isl_dim_in, id);
6155 equal = aff_check_plain_equal(aff, "{ A[x] -> [x + 1] }");
6156 isl_aff_free(aff);
6157 if (equal < 0)
6158 return isl_stat_error;
6160 return isl_stat_ok;
6163 int test_aff(isl_ctx *ctx)
6165 const char *str;
6166 isl_set *set;
6167 isl_space *space;
6168 isl_local_space *ls;
6169 isl_aff *aff;
6170 int zero, equal;
6172 if (test_upa(ctx) < 0)
6173 return -1;
6174 if (test_bin_aff(ctx) < 0)
6175 return -1;
6176 if (test_bin_pw_aff(ctx) < 0)
6177 return -1;
6178 if (test_bin_upma(ctx) < 0)
6179 return -1;
6180 if (test_bin_upma_fail(ctx) < 0)
6181 return -1;
6182 if (test_un_mpa(ctx) < 0)
6183 return -1;
6184 if (test_bin_mpa(ctx) < 0)
6185 return -1;
6186 if (test_un_mupa(ctx) < 0)
6187 return -1;
6188 if (test_bin_mupa(ctx) < 0)
6189 return -1;
6190 if (test_mupa_set(ctx) < 0)
6191 return -1;
6192 if (test_mupa_uset(ctx) < 0)
6193 return -1;
6194 if (test_mupa_ma(ctx) < 0)
6195 return -1;
6196 if (test_mupa_pa(ctx) < 0)
6197 return -1;
6198 if (test_mupa_pma(ctx) < 0)
6199 return -1;
6200 if (test_mupa_upma(ctx) < 0)
6201 return -1;
6203 space = isl_space_set_alloc(ctx, 0, 1);
6204 ls = isl_local_space_from_space(space);
6205 aff = isl_aff_zero_on_domain(ls);
6207 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6208 aff = isl_aff_scale_down_ui(aff, 3);
6209 aff = isl_aff_floor(aff);
6210 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6211 aff = isl_aff_scale_down_ui(aff, 2);
6212 aff = isl_aff_floor(aff);
6213 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6215 str = "{ [10] }";
6216 set = isl_set_read_from_str(ctx, str);
6217 aff = isl_aff_gist(aff, set);
6219 aff = isl_aff_add_constant_si(aff, -16);
6220 zero = isl_aff_plain_is_zero(aff);
6221 isl_aff_free(aff);
6223 if (zero < 0)
6224 return -1;
6225 if (!zero)
6226 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6228 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
6229 aff = isl_aff_scale_down_ui(aff, 64);
6230 aff = isl_aff_floor(aff);
6231 equal = aff_check_plain_equal(aff, "{ [-1] }");
6232 isl_aff_free(aff);
6233 if (equal < 0)
6234 return -1;
6236 if (test_aff_set_tuple_id(ctx) < 0)
6237 return -1;
6239 return 0;
6242 /* Check that "pa" consists of a single expression.
6244 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
6246 int n;
6248 n = isl_pw_aff_n_piece(pa);
6249 isl_pw_aff_free(pa);
6251 if (!pa)
6252 return -1;
6253 if (n != 1)
6254 isl_die(ctx, isl_error_unknown, "expecting single expression",
6255 return -1);
6257 return 0;
6260 /* Check that the computation below results in a single expression.
6261 * One or two expressions may result depending on which constraint
6262 * ends up being considered as redundant with respect to the other
6263 * constraints after the projection that is performed internally
6264 * by isl_set_dim_min.
6266 static int test_dim_max_1(isl_ctx *ctx)
6268 const char *str;
6269 isl_set *set;
6270 isl_pw_aff *pa;
6272 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
6273 "-4a <= b <= 3 and b < n - 4a }";
6274 set = isl_set_read_from_str(ctx, str);
6275 pa = isl_set_dim_min(set, 0);
6276 return check_single_piece(ctx, pa);
6279 /* Check that the computation below results in a single expression.
6280 * The PIP problem corresponding to these constraints has a row
6281 * that causes a split of the solution domain. The solver should
6282 * first pick rows that split off empty parts such that the actual
6283 * solution domain does not get split.
6284 * Note that the description contains some redundant constraints.
6285 * If these constraints get removed first, then the row mentioned
6286 * above does not appear in the PIP problem.
6288 static int test_dim_max_2(isl_ctx *ctx)
6290 const char *str;
6291 isl_set *set;
6292 isl_pw_aff *pa;
6294 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
6295 "N > 0 and P >= 0 }";
6296 set = isl_set_read_from_str(ctx, str);
6297 pa = isl_set_dim_max(set, 0);
6298 return check_single_piece(ctx, pa);
6301 int test_dim_max(isl_ctx *ctx)
6303 int equal;
6304 const char *str;
6305 isl_set *set1, *set2;
6306 isl_set *set;
6307 isl_map *map;
6308 isl_pw_aff *pwaff;
6310 if (test_dim_max_1(ctx) < 0)
6311 return -1;
6312 if (test_dim_max_2(ctx) < 0)
6313 return -1;
6315 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
6316 set = isl_set_read_from_str(ctx, str);
6317 pwaff = isl_set_dim_max(set, 0);
6318 set1 = isl_set_from_pw_aff(pwaff);
6319 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
6320 set2 = isl_set_read_from_str(ctx, str);
6321 equal = isl_set_is_equal(set1, set2);
6322 isl_set_free(set1);
6323 isl_set_free(set2);
6324 if (equal < 0)
6325 return -1;
6326 if (!equal)
6327 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6329 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
6330 set = isl_set_read_from_str(ctx, str);
6331 pwaff = isl_set_dim_max(set, 0);
6332 set1 = isl_set_from_pw_aff(pwaff);
6333 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6334 set2 = isl_set_read_from_str(ctx, str);
6335 equal = isl_set_is_equal(set1, set2);
6336 isl_set_free(set1);
6337 isl_set_free(set2);
6338 if (equal < 0)
6339 return -1;
6340 if (!equal)
6341 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6343 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
6344 set = isl_set_read_from_str(ctx, str);
6345 pwaff = isl_set_dim_max(set, 0);
6346 set1 = isl_set_from_pw_aff(pwaff);
6347 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6348 set2 = isl_set_read_from_str(ctx, str);
6349 equal = isl_set_is_equal(set1, set2);
6350 isl_set_free(set1);
6351 isl_set_free(set2);
6352 if (equal < 0)
6353 return -1;
6354 if (!equal)
6355 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6357 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
6358 "0 <= i < N and 0 <= j < M }";
6359 map = isl_map_read_from_str(ctx, str);
6360 set = isl_map_range(map);
6362 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
6363 set1 = isl_set_from_pw_aff(pwaff);
6364 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
6365 set2 = isl_set_read_from_str(ctx, str);
6366 equal = isl_set_is_equal(set1, set2);
6367 isl_set_free(set1);
6368 isl_set_free(set2);
6370 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
6371 set1 = isl_set_from_pw_aff(pwaff);
6372 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
6373 set2 = isl_set_read_from_str(ctx, str);
6374 if (equal >= 0 && equal)
6375 equal = isl_set_is_equal(set1, set2);
6376 isl_set_free(set1);
6377 isl_set_free(set2);
6379 isl_set_free(set);
6381 if (equal < 0)
6382 return -1;
6383 if (!equal)
6384 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6386 /* Check that solutions are properly merged. */
6387 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
6388 "c <= -1 + n - 4a - 2b and c >= -2b and "
6389 "4a >= -4 + n and c >= 0 }";
6390 set = isl_set_read_from_str(ctx, str);
6391 pwaff = isl_set_dim_min(set, 2);
6392 set1 = isl_set_from_pw_aff(pwaff);
6393 str = "[n] -> { [(0)] : n >= 1 }";
6394 set2 = isl_set_read_from_str(ctx, str);
6395 equal = isl_set_is_equal(set1, set2);
6396 isl_set_free(set1);
6397 isl_set_free(set2);
6399 if (equal < 0)
6400 return -1;
6401 if (!equal)
6402 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6404 /* Check that empty solution lie in the right space. */
6405 str = "[n] -> { [t,a] : 1 = 0 }";
6406 set = isl_set_read_from_str(ctx, str);
6407 pwaff = isl_set_dim_max(set, 0);
6408 set1 = isl_set_from_pw_aff(pwaff);
6409 str = "[n] -> { [t] : 1 = 0 }";
6410 set2 = isl_set_read_from_str(ctx, str);
6411 equal = isl_set_is_equal(set1, set2);
6412 isl_set_free(set1);
6413 isl_set_free(set2);
6415 if (equal < 0)
6416 return -1;
6417 if (!equal)
6418 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6420 return 0;
6423 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
6425 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
6426 const char *str)
6428 isl_ctx *ctx;
6429 isl_pw_multi_aff *pma2;
6430 int equal;
6432 if (!pma)
6433 return -1;
6435 ctx = isl_pw_multi_aff_get_ctx(pma);
6436 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6437 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
6438 isl_pw_multi_aff_free(pma2);
6440 return equal;
6443 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
6444 * represented by "str".
6446 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
6447 const char *str)
6449 int equal;
6451 equal = pw_multi_aff_plain_is_equal(pma, str);
6452 if (equal < 0)
6453 return -1;
6454 if (!equal)
6455 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
6456 "result not as expected", return -1);
6457 return 0;
6460 /* Basic test for isl_pw_multi_aff_product.
6462 * Check that multiple pieces are properly handled.
6464 static int test_product_pma(isl_ctx *ctx)
6466 int equal;
6467 const char *str;
6468 isl_pw_multi_aff *pma1, *pma2;
6470 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
6471 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6472 str = "{ C[] -> D[] }";
6473 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6474 pma1 = isl_pw_multi_aff_product(pma1, pma2);
6475 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
6476 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
6477 equal = pw_multi_aff_check_plain_equal(pma1, str);
6478 isl_pw_multi_aff_free(pma1);
6479 if (equal < 0)
6480 return -1;
6482 return 0;
6485 int test_product(isl_ctx *ctx)
6487 const char *str;
6488 isl_set *set;
6489 isl_union_set *uset1, *uset2;
6490 int ok;
6492 str = "{ A[i] }";
6493 set = isl_set_read_from_str(ctx, str);
6494 set = isl_set_product(set, isl_set_copy(set));
6495 ok = isl_set_is_wrapping(set);
6496 isl_set_free(set);
6497 if (ok < 0)
6498 return -1;
6499 if (!ok)
6500 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6502 str = "{ [] }";
6503 uset1 = isl_union_set_read_from_str(ctx, str);
6504 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
6505 str = "{ [[] -> []] }";
6506 uset2 = isl_union_set_read_from_str(ctx, str);
6507 ok = isl_union_set_is_equal(uset1, uset2);
6508 isl_union_set_free(uset1);
6509 isl_union_set_free(uset2);
6510 if (ok < 0)
6511 return -1;
6512 if (!ok)
6513 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6515 if (test_product_pma(ctx) < 0)
6516 return -1;
6518 return 0;
6521 /* Check that two sets are not considered disjoint just because
6522 * they have a different set of (named) parameters.
6524 static int test_disjoint(isl_ctx *ctx)
6526 const char *str;
6527 isl_set *set, *set2;
6528 int disjoint;
6530 str = "[n] -> { [[]->[]] }";
6531 set = isl_set_read_from_str(ctx, str);
6532 str = "{ [[]->[]] }";
6533 set2 = isl_set_read_from_str(ctx, str);
6534 disjoint = isl_set_is_disjoint(set, set2);
6535 isl_set_free(set);
6536 isl_set_free(set2);
6537 if (disjoint < 0)
6538 return -1;
6539 if (disjoint)
6540 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6542 return 0;
6545 /* Inputs for isl_pw_multi_aff_is_equal tests.
6546 * "f1" and "f2" are the two function that need to be compared.
6547 * "equal" is the expected result.
6549 struct {
6550 int equal;
6551 const char *f1;
6552 const char *f2;
6553 } pma_equal_tests[] = {
6554 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
6555 "[N] -> { [0] : 0 <= N <= 1 }" },
6556 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6557 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
6558 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6559 "[N] -> { [0] : 0 <= N <= 1 }" },
6560 { 0, "{ [NaN] }", "{ [NaN] }" },
6563 int test_equal(isl_ctx *ctx)
6565 int i;
6566 const char *str;
6567 isl_set *set, *set2;
6568 int equal;
6570 str = "{ S_6[i] }";
6571 set = isl_set_read_from_str(ctx, str);
6572 str = "{ S_7[i] }";
6573 set2 = isl_set_read_from_str(ctx, str);
6574 equal = isl_set_is_equal(set, set2);
6575 isl_set_free(set);
6576 isl_set_free(set2);
6577 if (equal < 0)
6578 return -1;
6579 if (equal)
6580 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6582 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
6583 int expected = pma_equal_tests[i].equal;
6584 isl_pw_multi_aff *f1, *f2;
6586 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
6587 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
6588 equal = isl_pw_multi_aff_is_equal(f1, f2);
6589 isl_pw_multi_aff_free(f1);
6590 isl_pw_multi_aff_free(f2);
6591 if (equal < 0)
6592 return -1;
6593 if (equal != expected)
6594 isl_die(ctx, isl_error_unknown,
6595 "unexpected equality result", return -1);
6598 return 0;
6601 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
6602 enum isl_dim_type type, unsigned pos, int fixed)
6604 isl_bool test;
6606 test = isl_map_plain_is_fixed(map, type, pos, NULL);
6607 isl_map_free(map);
6608 if (test < 0)
6609 return -1;
6610 if (test == fixed)
6611 return 0;
6612 if (fixed)
6613 isl_die(ctx, isl_error_unknown,
6614 "map not detected as fixed", return -1);
6615 else
6616 isl_die(ctx, isl_error_unknown,
6617 "map detected as fixed", return -1);
6620 int test_fixed(isl_ctx *ctx)
6622 const char *str;
6623 isl_map *map;
6625 str = "{ [i] -> [i] }";
6626 map = isl_map_read_from_str(ctx, str);
6627 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
6628 return -1;
6629 str = "{ [i] -> [1] }";
6630 map = isl_map_read_from_str(ctx, str);
6631 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6632 return -1;
6633 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
6634 map = isl_map_read_from_str(ctx, str);
6635 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6636 return -1;
6637 map = isl_map_read_from_str(ctx, str);
6638 map = isl_map_neg(map);
6639 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6640 return -1;
6642 return 0;
6645 struct isl_vertices_test_data {
6646 const char *set;
6647 int n;
6648 const char *vertex[6];
6649 } vertices_tests[] = {
6650 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
6651 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
6652 { "{ A[t, i] : t = 14 and i = 1 }",
6653 1, { "{ A[14, 1] }" } },
6654 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
6655 "c <= m and m <= n and m > 0 }",
6656 6, {
6657 "[n, m] -> { [n, m, m] : 0 < m <= n }",
6658 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
6659 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
6660 "[n, m] -> { [m, m, m] : 0 < m <= n }",
6661 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
6662 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
6663 } },
6666 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
6668 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
6670 struct isl_vertices_test_data *data = user;
6671 isl_ctx *ctx;
6672 isl_multi_aff *ma;
6673 isl_basic_set *bset;
6674 isl_pw_multi_aff *pma;
6675 int i;
6676 isl_bool equal;
6678 ctx = isl_vertex_get_ctx(vertex);
6679 bset = isl_vertex_get_domain(vertex);
6680 ma = isl_vertex_get_expr(vertex);
6681 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
6683 for (i = 0; i < data->n; ++i) {
6684 isl_pw_multi_aff *pma_i;
6686 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
6687 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
6688 isl_pw_multi_aff_free(pma_i);
6690 if (equal < 0 || equal)
6691 break;
6694 isl_pw_multi_aff_free(pma);
6695 isl_vertex_free(vertex);
6697 if (equal < 0)
6698 return isl_stat_error;
6700 return equal ? isl_stat_ok : isl_stat_error;
6703 int test_vertices(isl_ctx *ctx)
6705 int i;
6707 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
6708 isl_basic_set *bset;
6709 isl_vertices *vertices;
6710 int ok = 1;
6711 int n;
6713 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
6714 vertices = isl_basic_set_compute_vertices(bset);
6715 n = isl_vertices_get_n_vertices(vertices);
6716 if (vertices_tests[i].n != n)
6717 ok = 0;
6718 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
6719 &vertices_tests[i]) < 0)
6720 ok = 0;
6721 isl_vertices_free(vertices);
6722 isl_basic_set_free(bset);
6724 if (!vertices)
6725 return -1;
6726 if (!ok)
6727 isl_die(ctx, isl_error_unknown, "unexpected vertices",
6728 return -1);
6731 return 0;
6734 int test_union_pw(isl_ctx *ctx)
6736 int equal;
6737 const char *str;
6738 isl_union_set *uset;
6739 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
6741 str = "{ [x] -> x^2 }";
6742 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
6743 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
6744 uset = isl_union_pw_qpolynomial_domain(upwqp1);
6745 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
6746 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
6747 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
6748 isl_union_pw_qpolynomial_free(upwqp1);
6749 isl_union_pw_qpolynomial_free(upwqp2);
6750 if (equal < 0)
6751 return -1;
6752 if (!equal)
6753 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6755 return 0;
6758 /* Inputs for basic tests of functions that select
6759 * subparts of the domain of an isl_multi_union_pw_aff.
6760 * "fn" is the function that is tested.
6761 * "arg" is a string description of the input.
6762 * "res" is a string description of the expected result.
6764 struct {
6765 __isl_give isl_union_set *(*fn)(
6766 __isl_take isl_multi_union_pw_aff *mupa);
6767 const char *arg;
6768 const char *res;
6769 } un_locus_tests[] = {
6770 { &isl_multi_union_pw_aff_zero_union_set,
6771 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6772 "{ A[0,j]; B[0,j] }" },
6773 { &isl_multi_union_pw_aff_zero_union_set,
6774 "F[{ A[i,j] -> [i-j]; B[i,j] -> [i-j] : i >= 0 }]",
6775 "{ A[i,i]; B[i,i] : i >= 0 }" },
6776 { &isl_multi_union_pw_aff_zero_union_set,
6777 "(F[] : { A[i,j]; B[i,i] : i >= 0 })",
6778 "{ A[i,j]; B[i,i] : i >= 0 }" },
6781 /* Perform some basic tests of functions that select
6782 * subparts of the domain of an isl_multi_union_pw_aff.
6784 static int test_un_locus(isl_ctx *ctx)
6786 int i;
6787 isl_bool ok;
6788 isl_union_set *uset, *res;
6789 isl_multi_union_pw_aff *mupa;
6791 for (i = 0; i < ARRAY_SIZE(un_locus_tests); ++i) {
6792 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6793 un_locus_tests[i].arg);
6794 res = isl_union_set_read_from_str(ctx, un_locus_tests[i].res);
6795 uset = un_locus_tests[i].fn(mupa);
6796 ok = isl_union_set_is_equal(uset, res);
6797 isl_union_set_free(uset);
6798 isl_union_set_free(res);
6799 if (ok < 0)
6800 return -1;
6801 if (!ok)
6802 isl_die(ctx, isl_error_unknown,
6803 "unexpected result", return -1);
6806 return 0;
6809 /* Inputs for basic tests of functions that select
6810 * subparts of an isl_union_map based on a relation
6811 * specified by an isl_multi_union_pw_aff.
6812 * "fn" is the function that is tested.
6813 * "arg1" and "arg2" are string descriptions of the inputs.
6814 * "res" is a string description of the expected result.
6816 struct {
6817 __isl_give isl_union_map *(*fn)(
6818 __isl_take isl_union_map *umap,
6819 __isl_take isl_multi_union_pw_aff *mupa);
6820 const char *arg1;
6821 const char *arg2;
6822 const char *res;
6823 } bin_locus_tests[] = {
6824 { &isl_union_map_eq_at_multi_union_pw_aff,
6825 "{ A[i,j] -> B[i',j'] }",
6826 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6827 "{ A[i,j] -> B[i,j'] }" },
6828 { &isl_union_map_eq_at_multi_union_pw_aff,
6829 "{ A[i,j] -> B[i',j'] }",
6830 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6831 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6832 "{ A[i,j] -> B[i,j] }" },
6833 { &isl_union_map_eq_at_multi_union_pw_aff,
6834 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6835 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6836 "{ A[i,j] -> B[i,j'] }" },
6837 { &isl_union_map_eq_at_multi_union_pw_aff,
6838 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6839 "F[{ A[i,j] -> [i]; B[i,j] -> [i]; C[i,j] -> [0] }]",
6840 "{ A[i,j] -> B[i,j']; A[0,j] -> C[i',j'] }" },
6841 { &isl_union_map_eq_at_multi_union_pw_aff,
6842 "{ A[i,j] -> B[i',j'] }",
6843 "F[{ A[i,j] -> [i] : i > j; B[i,j] -> [i] }]",
6844 "{ A[i,j] -> B[i,j'] : i > j }" },
6845 { &isl_union_map_lex_lt_at_multi_union_pw_aff,
6846 "{ A[i,j] -> B[i',j'] }",
6847 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6848 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6849 "{ A[i,j] -> B[i',j'] : i,j << i',j' }" },
6850 { &isl_union_map_lex_gt_at_multi_union_pw_aff,
6851 "{ A[i,j] -> B[i',j'] }",
6852 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6853 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6854 "{ A[i,j] -> B[i',j'] : i,j >> i',j' }" },
6855 { &isl_union_map_eq_at_multi_union_pw_aff,
6856 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6857 "(F[] : { A[i,j]; B[i,j] })",
6858 "{ A[i,j] -> B[i',j'] }" },
6859 { &isl_union_map_eq_at_multi_union_pw_aff,
6860 "{ A[i,j] -> B[i',j'] }",
6861 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
6862 "{ A[i,j] -> B[i',j'] : i > j and i' < j' }" },
6863 { &isl_union_map_eq_at_multi_union_pw_aff,
6864 "[N] -> { A[i,j] -> B[i',j'] : i,i' <= N }",
6865 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
6866 "[N] -> { A[i,j] -> B[i',j'] : i > j and i' < j' and i,i' <= N }" },
6867 { &isl_union_map_eq_at_multi_union_pw_aff,
6868 "{ A[i,j] -> B[i',j'] }",
6869 "[N] -> (F[] : { A[i,j] : i < N; B[i,j] : i < N })",
6870 "[N] -> { A[i,j] -> B[i',j'] : i,i' < N }" },
6871 { &isl_union_map_eq_at_multi_union_pw_aff,
6872 "{ A[i,j] -> B[i',j'] }",
6873 "[N] -> (F[] : { : N >= 0 })",
6874 "[N] -> { A[i,j] -> B[i',j'] : N >= 0 }" },
6877 /* Perform some basic tests of functions that select
6878 * subparts of an isl_union_map based on a relation
6879 * specified by an isl_multi_union_pw_aff.
6881 static int test_bin_locus(isl_ctx *ctx)
6883 int i;
6884 isl_bool ok;
6885 isl_union_map *umap, *res;
6886 isl_multi_union_pw_aff *mupa;
6888 for (i = 0; i < ARRAY_SIZE(bin_locus_tests); ++i) {
6889 umap = isl_union_map_read_from_str(ctx,
6890 bin_locus_tests[i].arg1);
6891 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6892 bin_locus_tests[i].arg2);
6893 res = isl_union_map_read_from_str(ctx, bin_locus_tests[i].res);
6894 umap = bin_locus_tests[i].fn(umap, mupa);
6895 ok = isl_union_map_is_equal(umap, res);
6896 isl_union_map_free(umap);
6897 isl_union_map_free(res);
6898 if (ok < 0)
6899 return -1;
6900 if (!ok)
6901 isl_die(ctx, isl_error_unknown,
6902 "unexpected result", return -1);
6905 return 0;
6908 /* Perform basic locus tests.
6910 static int test_locus(isl_ctx *ctx)
6912 if (test_un_locus(ctx) < 0)
6913 return -1;
6914 if (test_bin_locus(ctx) < 0)
6915 return -1;
6916 return 0;
6919 /* Test that isl_union_pw_qpolynomial_eval picks up the function
6920 * defined over the correct domain space.
6922 static int test_eval_1(isl_ctx *ctx)
6924 const char *str;
6925 isl_point *pnt;
6926 isl_set *set;
6927 isl_union_pw_qpolynomial *upwqp;
6928 isl_val *v;
6929 int cmp;
6931 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
6932 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
6933 str = "{ A[6] }";
6934 set = isl_set_read_from_str(ctx, str);
6935 pnt = isl_set_sample_point(set);
6936 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
6937 cmp = isl_val_cmp_si(v, 36);
6938 isl_val_free(v);
6940 if (!v)
6941 return -1;
6942 if (cmp != 0)
6943 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
6945 return 0;
6948 /* Check that isl_qpolynomial_eval handles getting called on a void point.
6950 static int test_eval_2(isl_ctx *ctx)
6952 const char *str;
6953 isl_point *pnt;
6954 isl_set *set;
6955 isl_qpolynomial *qp;
6956 isl_val *v;
6957 isl_bool ok;
6959 str = "{ A[x] -> [x] }";
6960 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
6961 str = "{ A[x] : false }";
6962 set = isl_set_read_from_str(ctx, str);
6963 pnt = isl_set_sample_point(set);
6964 v = isl_qpolynomial_eval(qp, pnt);
6965 ok = isl_val_is_nan(v);
6966 isl_val_free(v);
6968 if (ok < 0)
6969 return -1;
6970 if (!ok)
6971 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
6973 return 0;
6976 /* Inputs for isl_pw_aff_eval test.
6977 * "f" is the affine function.
6978 * "p" is the point where the function should be evaluated.
6979 * "res" is the expected result.
6981 struct {
6982 const char *f;
6983 const char *p;
6984 const char *res;
6985 } aff_eval_tests[] = {
6986 { "{ [i] -> [2 * i] }", "{ [4] }", "8" },
6987 { "{ [i] -> [2 * i] }", "{ [x] : false }", "NaN" },
6988 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [0] }", "0" },
6989 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [1] }", "1" },
6990 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [2] }", "3" },
6991 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [3] }", "5" },
6992 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [4] }", "7" },
6993 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [0] }", "0" },
6994 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [1] }", "0" },
6995 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [2] }", "0" },
6996 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [3] }", "0" },
6997 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [4] }", "1" },
6998 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [6] }", "1" },
6999 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [8] }", "2" },
7000 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [4] }", "4" },
7001 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [-2] }", "2" },
7002 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [0] }", "NaN" },
7003 { "[N] -> { [2 * N] }", "[N] -> { : N = 4 }", "8" },
7004 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 1] }", "1" },
7005 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 2] }", "3/2" },
7006 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [4] }", "4" },
7007 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [3] }", "NaN" },
7008 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [x] : false }", "NaN" },
7011 /* Perform basic isl_pw_aff_eval tests.
7013 static int test_eval_aff(isl_ctx *ctx)
7015 int i;
7017 for (i = 0; i < ARRAY_SIZE(aff_eval_tests); ++i) {
7018 isl_stat r;
7019 isl_pw_aff *pa;
7020 isl_set *set;
7021 isl_point *pnt;
7022 isl_val *v;
7024 pa = isl_pw_aff_read_from_str(ctx, aff_eval_tests[i].f);
7025 set = isl_set_read_from_str(ctx, aff_eval_tests[i].p);
7026 pnt = isl_set_sample_point(set);
7027 v = isl_pw_aff_eval(pa, pnt);
7028 r = val_check_equal(v, aff_eval_tests[i].res);
7029 isl_val_free(v);
7030 if (r < 0)
7031 return -1;
7033 return 0;
7036 /* Perform basic evaluation tests.
7038 static int test_eval(isl_ctx *ctx)
7040 if (test_eval_1(ctx) < 0)
7041 return -1;
7042 if (test_eval_2(ctx) < 0)
7043 return -1;
7044 if (test_eval_aff(ctx) < 0)
7045 return -1;
7046 return 0;
7049 /* Descriptions of sets that are tested for reparsing after printing.
7051 const char *output_tests[] = {
7052 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
7053 "{ [x] : 1 = 0 }",
7054 "{ [x] : false }",
7055 "{ [x] : x mod 2 = 0 }",
7056 "{ [x] : x mod 2 = 1 }",
7057 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
7058 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
7059 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7060 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7061 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
7062 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
7065 /* Check that printing a set and reparsing a set from the printed output
7066 * results in the same set.
7068 static int test_output_set(isl_ctx *ctx)
7070 int i;
7071 char *str;
7072 isl_set *set1, *set2;
7073 isl_bool equal;
7075 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
7076 set1 = isl_set_read_from_str(ctx, output_tests[i]);
7077 str = isl_set_to_str(set1);
7078 set2 = isl_set_read_from_str(ctx, str);
7079 free(str);
7080 equal = isl_set_is_equal(set1, set2);
7081 isl_set_free(set1);
7082 isl_set_free(set2);
7083 if (equal < 0)
7084 return -1;
7085 if (!equal)
7086 isl_die(ctx, isl_error_unknown,
7087 "parsed output not the same", return -1);
7090 return 0;
7093 /* Check that an isl_multi_aff is printed using a consistent space.
7095 static isl_stat test_output_ma(isl_ctx *ctx)
7097 char *str;
7098 isl_bool equal;
7099 isl_aff *aff;
7100 isl_multi_aff *ma, *ma2;
7102 ma = isl_multi_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7103 aff = isl_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7104 ma = isl_multi_aff_set_aff(ma, 0, aff);
7105 str = isl_multi_aff_to_str(ma);
7106 ma2 = isl_multi_aff_read_from_str(ctx, str);
7107 free(str);
7108 equal = isl_multi_aff_plain_is_equal(ma, ma2);
7109 isl_multi_aff_free(ma2);
7110 isl_multi_aff_free(ma);
7112 if (equal < 0)
7113 return isl_stat_error;
7114 if (!equal)
7115 isl_die(ctx, isl_error_unknown, "bad conversion",
7116 return isl_stat_error);
7118 return isl_stat_ok;
7121 /* Check that an isl_multi_pw_aff is printed using a consistent space.
7123 static isl_stat test_output_mpa(isl_ctx *ctx)
7125 char *str;
7126 isl_bool equal;
7127 isl_pw_aff *pa;
7128 isl_multi_pw_aff *mpa, *mpa2;
7130 mpa = isl_multi_pw_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7131 pa = isl_pw_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7132 mpa = isl_multi_pw_aff_set_pw_aff(mpa, 0, pa);
7133 str = isl_multi_pw_aff_to_str(mpa);
7134 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
7135 free(str);
7136 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
7137 isl_multi_pw_aff_free(mpa2);
7138 isl_multi_pw_aff_free(mpa);
7140 if (equal < 0)
7141 return isl_stat_error;
7142 if (!equal)
7143 isl_die(ctx, isl_error_unknown, "bad conversion",
7144 return isl_stat_error);
7146 return isl_stat_ok;
7149 int test_output(isl_ctx *ctx)
7151 char *s;
7152 const char *str;
7153 isl_pw_aff *pa;
7154 isl_printer *p;
7155 int equal;
7157 if (test_output_set(ctx) < 0)
7158 return -1;
7159 if (test_output_ma(ctx) < 0)
7160 return -1;
7161 if (test_output_mpa(ctx) < 0)
7162 return -1;
7164 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
7165 pa = isl_pw_aff_read_from_str(ctx, str);
7167 p = isl_printer_to_str(ctx);
7168 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
7169 p = isl_printer_print_pw_aff(p, pa);
7170 s = isl_printer_get_str(p);
7171 isl_printer_free(p);
7172 isl_pw_aff_free(pa);
7173 if (!s)
7174 equal = -1;
7175 else
7176 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
7177 free(s);
7178 if (equal < 0)
7179 return -1;
7180 if (!equal)
7181 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7183 return 0;
7186 int test_sample(isl_ctx *ctx)
7188 const char *str;
7189 isl_basic_set *bset1, *bset2;
7190 int empty, subset;
7192 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
7193 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
7194 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
7195 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
7196 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
7197 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
7198 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
7199 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
7200 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
7201 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
7202 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
7203 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
7204 "d - 1073741821e and "
7205 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
7206 "3j >= 1 - a + b + 2e and "
7207 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
7208 "3i <= 4 - a + 4b - e and "
7209 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
7210 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
7211 "c <= -1 + a and 3i >= -2 - a + 3e and "
7212 "1073741822e <= 1073741823 - a + 1073741822b + c and "
7213 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
7214 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
7215 "1073741823e >= 1 + 1073741823b - d and "
7216 "3i >= 1073741823b + c - 1073741823e - f and "
7217 "3i >= 1 + 2b + e + 3g }";
7218 bset1 = isl_basic_set_read_from_str(ctx, str);
7219 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
7220 empty = isl_basic_set_is_empty(bset2);
7221 subset = isl_basic_set_is_subset(bset2, bset1);
7222 isl_basic_set_free(bset1);
7223 isl_basic_set_free(bset2);
7224 if (empty < 0 || subset < 0)
7225 return -1;
7226 if (empty)
7227 isl_die(ctx, isl_error_unknown, "point not found", return -1);
7228 if (!subset)
7229 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
7231 return 0;
7234 int test_fixed_power(isl_ctx *ctx)
7236 const char *str;
7237 isl_map *map;
7238 isl_val *exp;
7239 int equal;
7241 str = "{ [i] -> [i + 1] }";
7242 map = isl_map_read_from_str(ctx, str);
7243 exp = isl_val_int_from_si(ctx, 23);
7244 map = isl_map_fixed_power_val(map, exp);
7245 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
7246 isl_map_free(map);
7247 if (equal < 0)
7248 return -1;
7250 return 0;
7253 int test_slice(isl_ctx *ctx)
7255 const char *str;
7256 isl_map *map;
7257 int equal;
7259 str = "{ [i] -> [j] }";
7260 map = isl_map_read_from_str(ctx, str);
7261 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
7262 equal = map_check_equal(map, "{ [i] -> [i] }");
7263 isl_map_free(map);
7264 if (equal < 0)
7265 return -1;
7267 str = "{ [i] -> [j] }";
7268 map = isl_map_read_from_str(ctx, str);
7269 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
7270 equal = map_check_equal(map, "{ [i] -> [j] }");
7271 isl_map_free(map);
7272 if (equal < 0)
7273 return -1;
7275 str = "{ [i] -> [j] }";
7276 map = isl_map_read_from_str(ctx, str);
7277 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
7278 equal = map_check_equal(map, "{ [i] -> [-i] }");
7279 isl_map_free(map);
7280 if (equal < 0)
7281 return -1;
7283 str = "{ [i] -> [j] }";
7284 map = isl_map_read_from_str(ctx, str);
7285 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
7286 equal = map_check_equal(map, "{ [0] -> [j] }");
7287 isl_map_free(map);
7288 if (equal < 0)
7289 return -1;
7291 str = "{ [i] -> [j] }";
7292 map = isl_map_read_from_str(ctx, str);
7293 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
7294 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
7295 isl_map_free(map);
7296 if (equal < 0)
7297 return -1;
7299 str = "{ [i] -> [j] }";
7300 map = isl_map_read_from_str(ctx, str);
7301 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
7302 equal = map_check_equal(map, "{ [i] -> [j] : false }");
7303 isl_map_free(map);
7304 if (equal < 0)
7305 return -1;
7307 return 0;
7310 int test_eliminate(isl_ctx *ctx)
7312 const char *str;
7313 isl_map *map;
7314 int equal;
7316 str = "{ [i] -> [j] : i = 2j }";
7317 map = isl_map_read_from_str(ctx, str);
7318 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
7319 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
7320 isl_map_free(map);
7321 if (equal < 0)
7322 return -1;
7324 return 0;
7327 /* Check that isl_set_dim_residue_class detects that the values of j
7328 * in the set below are all odd and that it does not detect any spurious
7329 * strides.
7331 static int test_residue_class(isl_ctx *ctx)
7333 const char *str;
7334 isl_set *set;
7335 isl_int m, r;
7336 isl_stat res;
7338 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
7339 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
7340 set = isl_set_read_from_str(ctx, str);
7341 isl_int_init(m);
7342 isl_int_init(r);
7343 res = isl_set_dim_residue_class(set, 1, &m, &r);
7344 if (res >= 0 &&
7345 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
7346 isl_die(ctx, isl_error_unknown, "incorrect residue class",
7347 res = isl_stat_error);
7348 isl_int_clear(r);
7349 isl_int_clear(m);
7350 isl_set_free(set);
7352 return res;
7355 static int test_align_parameters_1(isl_ctx *ctx)
7357 const char *str;
7358 isl_space *space;
7359 isl_multi_aff *ma1, *ma2;
7360 int equal;
7362 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
7363 ma1 = isl_multi_aff_read_from_str(ctx, str);
7365 space = isl_space_params_alloc(ctx, 1);
7366 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
7367 ma1 = isl_multi_aff_align_params(ma1, space);
7369 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
7370 ma2 = isl_multi_aff_read_from_str(ctx, str);
7372 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
7374 isl_multi_aff_free(ma1);
7375 isl_multi_aff_free(ma2);
7377 if (equal < 0)
7378 return -1;
7379 if (!equal)
7380 isl_die(ctx, isl_error_unknown,
7381 "result not as expected", return -1);
7383 return 0;
7386 /* Check the isl_multi_*_from_*_list operation in case inputs
7387 * have unaligned parameters.
7388 * In particular, older versions of isl would simply fail
7389 * (without printing any error message).
7391 static isl_stat test_align_parameters_2(isl_ctx *ctx)
7393 isl_space *space;
7394 isl_map *map;
7395 isl_aff *aff;
7396 isl_multi_aff *ma;
7398 map = isl_map_read_from_str(ctx, "{ A[] -> M[x] }");
7399 space = isl_map_get_space(map);
7400 isl_map_free(map);
7402 aff = isl_aff_read_from_str(ctx, "[N] -> { A[] -> [N] }");
7403 ma = isl_multi_aff_from_aff_list(space, isl_aff_list_from_aff(aff));
7404 isl_multi_aff_free(ma);
7406 if (!ma)
7407 return isl_stat_error;
7408 return isl_stat_ok;
7411 /* Perform basic parameter alignment tests.
7413 static int test_align_parameters(isl_ctx *ctx)
7415 if (test_align_parameters_1(ctx) < 0)
7416 return -1;
7417 if (test_align_parameters_2(ctx) < 0)
7418 return -1;
7420 return 0;
7423 /* Check that isl_*_drop_unused_params actually drops the unused parameters
7424 * by comparing the result using isl_*_plain_is_equal.
7425 * Note that this assumes that isl_*_plain_is_equal does not consider
7426 * objects that only differ by unused parameters to be equal.
7428 int test_drop_unused_parameters(isl_ctx *ctx)
7430 const char *str_with, *str_without;
7431 isl_basic_set *bset1, *bset2;
7432 isl_set *set1, *set2;
7433 isl_pw_aff *pwa1, *pwa2;
7434 int equal;
7436 str_with = "[n, m, o] -> { [m] }";
7437 str_without = "[m] -> { [m] }";
7439 bset1 = isl_basic_set_read_from_str(ctx, str_with);
7440 bset2 = isl_basic_set_read_from_str(ctx, str_without);
7441 bset1 = isl_basic_set_drop_unused_params(bset1);
7442 equal = isl_basic_set_plain_is_equal(bset1, bset2);
7443 isl_basic_set_free(bset1);
7444 isl_basic_set_free(bset2);
7446 if (equal < 0)
7447 return -1;
7448 if (!equal)
7449 isl_die(ctx, isl_error_unknown,
7450 "result not as expected", return -1);
7452 set1 = isl_set_read_from_str(ctx, str_with);
7453 set2 = isl_set_read_from_str(ctx, str_without);
7454 set1 = isl_set_drop_unused_params(set1);
7455 equal = isl_set_plain_is_equal(set1, set2);
7456 isl_set_free(set1);
7457 isl_set_free(set2);
7459 if (equal < 0)
7460 return -1;
7461 if (!equal)
7462 isl_die(ctx, isl_error_unknown,
7463 "result not as expected", return -1);
7465 pwa1 = isl_pw_aff_read_from_str(ctx, str_with);
7466 pwa2 = isl_pw_aff_read_from_str(ctx, str_without);
7467 pwa1 = isl_pw_aff_drop_unused_params(pwa1);
7468 equal = isl_pw_aff_plain_is_equal(pwa1, pwa2);
7469 isl_pw_aff_free(pwa1);
7470 isl_pw_aff_free(pwa2);
7472 if (equal < 0)
7473 return -1;
7474 if (!equal)
7475 isl_die(ctx, isl_error_unknown,
7476 "result not as expected", return -1);
7478 return 0;
7481 static int test_list(isl_ctx *ctx)
7483 isl_id *a, *b, *c, *d, *id;
7484 isl_id_list *list;
7485 int ok;
7487 a = isl_id_alloc(ctx, "a", NULL);
7488 b = isl_id_alloc(ctx, "b", NULL);
7489 c = isl_id_alloc(ctx, "c", NULL);
7490 d = isl_id_alloc(ctx, "d", NULL);
7492 list = isl_id_list_alloc(ctx, 4);
7493 list = isl_id_list_add(list, b);
7494 list = isl_id_list_insert(list, 0, a);
7495 list = isl_id_list_add(list, c);
7496 list = isl_id_list_add(list, d);
7497 list = isl_id_list_drop(list, 1, 1);
7499 if (!list)
7500 return -1;
7501 if (isl_id_list_n_id(list) != 3) {
7502 isl_id_list_free(list);
7503 isl_die(ctx, isl_error_unknown,
7504 "unexpected number of elements in list", return -1);
7507 id = isl_id_list_get_id(list, 0);
7508 ok = id == a;
7509 isl_id_free(id);
7510 id = isl_id_list_get_id(list, 1);
7511 ok = ok && id == c;
7512 isl_id_free(id);
7513 id = isl_id_list_get_id(list, 2);
7514 ok = ok && id == d;
7515 isl_id_free(id);
7517 isl_id_list_free(list);
7519 if (!ok)
7520 isl_die(ctx, isl_error_unknown,
7521 "unexpected elements in list", return -1);
7523 return 0;
7526 const char *set_conversion_tests[] = {
7527 "[N] -> { [i] : N - 1 <= 2 i <= N }",
7528 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
7529 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7530 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7531 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
7532 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
7533 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
7534 "-3 + c <= d <= 28 + c) }",
7537 /* Check that converting from isl_set to isl_pw_multi_aff and back
7538 * to isl_set produces the original isl_set.
7540 static int test_set_conversion(isl_ctx *ctx)
7542 int i;
7543 const char *str;
7544 isl_set *set1, *set2;
7545 isl_pw_multi_aff *pma;
7546 int equal;
7548 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
7549 str = set_conversion_tests[i];
7550 set1 = isl_set_read_from_str(ctx, str);
7551 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
7552 set2 = isl_set_from_pw_multi_aff(pma);
7553 equal = isl_set_is_equal(set1, set2);
7554 isl_set_free(set1);
7555 isl_set_free(set2);
7557 if (equal < 0)
7558 return -1;
7559 if (!equal)
7560 isl_die(ctx, isl_error_unknown, "bad conversion",
7561 return -1);
7564 return 0;
7567 const char *conversion_tests[] = {
7568 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7569 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7570 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7571 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7572 "9e <= -2 - 2a) }",
7573 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7574 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7575 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7578 /* Check that converting from isl_map to isl_pw_multi_aff and back
7579 * to isl_map produces the original isl_map.
7581 static int test_map_conversion(isl_ctx *ctx)
7583 int i;
7584 isl_map *map1, *map2;
7585 isl_pw_multi_aff *pma;
7586 int equal;
7588 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
7589 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
7590 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
7591 map2 = isl_map_from_pw_multi_aff(pma);
7592 equal = isl_map_is_equal(map1, map2);
7593 isl_map_free(map1);
7594 isl_map_free(map2);
7596 if (equal < 0)
7597 return -1;
7598 if (!equal)
7599 isl_die(ctx, isl_error_unknown, "bad conversion",
7600 return -1);
7603 return 0;
7606 /* Descriptions of isl_pw_multi_aff objects for testing conversion
7607 * to isl_multi_pw_aff and back.
7609 const char *mpa_conversion_tests[] = {
7610 "{ [x] -> A[x] }",
7611 "{ [x] -> A[x] : x >= 0 }",
7612 "{ [x] -> A[x] : x >= 0; [x] -> A[-x] : x < 0 }",
7613 "{ [x] -> A[x, x + 1] }",
7614 "{ [x] -> A[] }",
7615 "{ [x] -> A[] : x >= 0 }",
7618 /* Check that conversion from isl_pw_multi_aff to isl_multi_pw_aff and
7619 * back to isl_pw_multi_aff preserves the original meaning.
7621 static int test_mpa_conversion(isl_ctx *ctx)
7623 int i;
7624 isl_pw_multi_aff *pma1, *pma2;
7625 isl_multi_pw_aff *mpa;
7626 int equal;
7628 for (i = 0; i < ARRAY_SIZE(mpa_conversion_tests); ++i) {
7629 const char *str;
7630 str = mpa_conversion_tests[i];
7631 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
7632 pma2 = isl_pw_multi_aff_copy(pma1);
7633 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma1);
7634 pma1 = isl_pw_multi_aff_from_multi_pw_aff(mpa);
7635 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7636 isl_pw_multi_aff_free(pma1);
7637 isl_pw_multi_aff_free(pma2);
7639 if (equal < 0)
7640 return -1;
7641 if (!equal)
7642 isl_die(ctx, isl_error_unknown, "bad conversion",
7643 return -1);
7646 return 0;
7649 /* Descriptions of union maps that should be convertible
7650 * to an isl_multi_union_pw_aff.
7652 const char *umap_mupa_conversion_tests[] = {
7653 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7654 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7655 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7656 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7657 "9e <= -2 - 2a) }",
7658 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7659 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7660 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7661 "{ A[] -> B[0]; C[] -> B[1] }",
7662 "{ A[] -> B[]; C[] -> B[] }",
7665 /* Check that converting from isl_union_map to isl_multi_union_pw_aff and back
7666 * to isl_union_map produces the original isl_union_map.
7668 static int test_union_map_mupa_conversion(isl_ctx *ctx)
7670 int i;
7671 isl_union_map *umap1, *umap2;
7672 isl_multi_union_pw_aff *mupa;
7673 int equal;
7675 for (i = 0; i < ARRAY_SIZE(umap_mupa_conversion_tests); ++i) {
7676 const char *str;
7677 str = umap_mupa_conversion_tests[i];
7678 umap1 = isl_union_map_read_from_str(ctx, str);
7679 umap2 = isl_union_map_copy(umap1);
7680 mupa = isl_multi_union_pw_aff_from_union_map(umap2);
7681 umap2 = isl_union_map_from_multi_union_pw_aff(mupa);
7682 equal = isl_union_map_is_equal(umap1, umap2);
7683 isl_union_map_free(umap1);
7684 isl_union_map_free(umap2);
7686 if (equal < 0)
7687 return -1;
7688 if (!equal)
7689 isl_die(ctx, isl_error_unknown, "bad conversion",
7690 return -1);
7693 return 0;
7696 static int test_conversion(isl_ctx *ctx)
7698 if (test_set_conversion(ctx) < 0)
7699 return -1;
7700 if (test_map_conversion(ctx) < 0)
7701 return -1;
7702 if (test_mpa_conversion(ctx) < 0)
7703 return -1;
7704 if (test_union_map_mupa_conversion(ctx) < 0)
7705 return -1;
7706 return 0;
7709 /* Check that isl_basic_map_curry does not modify input.
7711 static int test_curry(isl_ctx *ctx)
7713 const char *str;
7714 isl_basic_map *bmap1, *bmap2;
7715 int equal;
7717 str = "{ [A[] -> B[]] -> C[] }";
7718 bmap1 = isl_basic_map_read_from_str(ctx, str);
7719 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
7720 equal = isl_basic_map_is_equal(bmap1, bmap2);
7721 isl_basic_map_free(bmap1);
7722 isl_basic_map_free(bmap2);
7724 if (equal < 0)
7725 return -1;
7726 if (equal)
7727 isl_die(ctx, isl_error_unknown,
7728 "curried map should not be equal to original",
7729 return -1);
7731 return 0;
7734 struct {
7735 const char *set;
7736 const char *ma;
7737 const char *res;
7738 } preimage_tests[] = {
7739 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
7740 "{ A[j,i] -> B[i,j] }",
7741 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
7742 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
7743 "{ A[a,b] -> B[a/2,b/6] }",
7744 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
7745 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
7746 "{ A[a,b] -> B[a/2,b/6] }",
7747 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
7748 "exists i,j : a = 2 i and b = 6 j }" },
7749 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
7750 "[n] -> { : 0 <= n <= 100 }" },
7751 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
7752 "{ A[a] -> B[2a] }",
7753 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
7754 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
7755 "{ A[a] -> B[([a/2])] }",
7756 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
7757 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
7758 "{ A[a] -> B[a,a,a/3] }",
7759 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
7760 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
7761 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
7764 static int test_preimage_basic_set(isl_ctx *ctx)
7766 int i;
7767 isl_basic_set *bset1, *bset2;
7768 isl_multi_aff *ma;
7769 int equal;
7771 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
7772 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
7773 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
7774 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
7775 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
7776 equal = isl_basic_set_is_equal(bset1, bset2);
7777 isl_basic_set_free(bset1);
7778 isl_basic_set_free(bset2);
7779 if (equal < 0)
7780 return -1;
7781 if (!equal)
7782 isl_die(ctx, isl_error_unknown, "bad preimage",
7783 return -1);
7786 return 0;
7789 struct {
7790 const char *map;
7791 const char *ma;
7792 const char *res;
7793 } preimage_domain_tests[] = {
7794 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
7795 "{ A[j,i] -> B[i,j] }",
7796 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
7797 { "{ B[i] -> C[i]; D[i] -> E[i] }",
7798 "{ A[i] -> B[i + 1] }",
7799 "{ A[i] -> C[i + 1] }" },
7800 { "{ B[i] -> C[i]; B[i] -> E[i] }",
7801 "{ A[i] -> B[i + 1] }",
7802 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
7803 { "{ B[i] -> C[([i/2])] }",
7804 "{ A[i] -> B[2i] }",
7805 "{ A[i] -> C[i] }" },
7806 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
7807 "{ A[i] -> B[([i/5]), ([i/7])] }",
7808 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
7809 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
7810 "[N] -> { A[] -> B[([N/5])] }",
7811 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
7812 { "{ B[i] -> C[i] : exists a : i = 5 a }",
7813 "{ A[i] -> B[2i] }",
7814 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
7815 { "{ B[i] -> C[i] : exists a : i = 2 a; "
7816 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
7817 "{ A[i] -> B[2i] }",
7818 "{ A[i] -> C[2i] }" },
7819 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
7820 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
7823 static int test_preimage_union_map(isl_ctx *ctx)
7825 int i;
7826 isl_union_map *umap1, *umap2;
7827 isl_multi_aff *ma;
7828 int equal;
7830 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
7831 umap1 = isl_union_map_read_from_str(ctx,
7832 preimage_domain_tests[i].map);
7833 ma = isl_multi_aff_read_from_str(ctx,
7834 preimage_domain_tests[i].ma);
7835 umap2 = isl_union_map_read_from_str(ctx,
7836 preimage_domain_tests[i].res);
7837 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
7838 equal = isl_union_map_is_equal(umap1, umap2);
7839 isl_union_map_free(umap1);
7840 isl_union_map_free(umap2);
7841 if (equal < 0)
7842 return -1;
7843 if (!equal)
7844 isl_die(ctx, isl_error_unknown, "bad preimage",
7845 return -1);
7848 return 0;
7851 static int test_preimage(isl_ctx *ctx)
7853 if (test_preimage_basic_set(ctx) < 0)
7854 return -1;
7855 if (test_preimage_union_map(ctx) < 0)
7856 return -1;
7858 return 0;
7861 struct {
7862 const char *ma1;
7863 const char *ma;
7864 const char *res;
7865 } pullback_tests[] = {
7866 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
7867 "{ A[a,b] -> C[b + 2a] }" },
7868 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
7869 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
7870 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
7871 "{ A[a] -> C[(a)/6] }" },
7872 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
7873 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
7874 "{ A[a] -> C[(2a)/3] }" },
7875 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
7876 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
7877 "{ A[i,j] -> C[i + j, i + j] }"},
7878 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
7879 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
7880 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
7881 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
7882 "{ [i, j] -> [floor((i)/3), j] }",
7883 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
7886 static int test_pullback(isl_ctx *ctx)
7888 int i;
7889 isl_multi_aff *ma1, *ma2;
7890 isl_multi_aff *ma;
7891 int equal;
7893 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
7894 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
7895 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
7896 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
7897 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
7898 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
7899 isl_multi_aff_free(ma1);
7900 isl_multi_aff_free(ma2);
7901 if (equal < 0)
7902 return -1;
7903 if (!equal)
7904 isl_die(ctx, isl_error_unknown, "bad pullback",
7905 return -1);
7908 return 0;
7911 /* Check that negation is printed correctly and that equal expressions
7912 * are correctly identified.
7914 static int test_ast(isl_ctx *ctx)
7916 isl_ast_expr *expr, *expr1, *expr2, *expr3;
7917 char *str;
7918 int ok, equal;
7920 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
7921 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
7922 expr = isl_ast_expr_add(expr1, expr2);
7923 expr2 = isl_ast_expr_copy(expr);
7924 expr = isl_ast_expr_neg(expr);
7925 expr2 = isl_ast_expr_neg(expr2);
7926 equal = isl_ast_expr_is_equal(expr, expr2);
7927 str = isl_ast_expr_to_C_str(expr);
7928 ok = str ? !strcmp(str, "-(A + B)") : -1;
7929 free(str);
7930 isl_ast_expr_free(expr);
7931 isl_ast_expr_free(expr2);
7933 if (ok < 0 || equal < 0)
7934 return -1;
7935 if (!equal)
7936 isl_die(ctx, isl_error_unknown,
7937 "equal expressions not considered equal", return -1);
7938 if (!ok)
7939 isl_die(ctx, isl_error_unknown,
7940 "isl_ast_expr printed incorrectly", return -1);
7942 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
7943 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
7944 expr = isl_ast_expr_add(expr1, expr2);
7945 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
7946 expr = isl_ast_expr_sub(expr3, expr);
7947 str = isl_ast_expr_to_C_str(expr);
7948 ok = str ? !strcmp(str, "C - (A + B)") : -1;
7949 free(str);
7950 isl_ast_expr_free(expr);
7952 if (ok < 0)
7953 return -1;
7954 if (!ok)
7955 isl_die(ctx, isl_error_unknown,
7956 "isl_ast_expr printed incorrectly", return -1);
7958 return 0;
7961 /* Check that isl_ast_build_expr_from_set returns a valid expression
7962 * for an empty set. Note that isl_ast_build_expr_from_set getting
7963 * called on an empty set probably indicates a bug in the caller.
7965 static int test_ast_build(isl_ctx *ctx)
7967 isl_set *set;
7968 isl_ast_build *build;
7969 isl_ast_expr *expr;
7971 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
7972 build = isl_ast_build_from_context(set);
7974 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
7975 expr = isl_ast_build_expr_from_set(build, set);
7977 isl_ast_expr_free(expr);
7978 isl_ast_build_free(build);
7980 if (!expr)
7981 return -1;
7983 return 0;
7986 /* Internal data structure for before_for and after_for callbacks.
7988 * depth is the current depth
7989 * before is the number of times before_for has been called
7990 * after is the number of times after_for has been called
7992 struct isl_test_codegen_data {
7993 int depth;
7994 int before;
7995 int after;
7998 /* This function is called before each for loop in the AST generated
7999 * from test_ast_gen1.
8001 * Increment the number of calls and the depth.
8002 * Check that the space returned by isl_ast_build_get_schedule_space
8003 * matches the target space of the schedule returned by
8004 * isl_ast_build_get_schedule.
8005 * Return an isl_id that is checked by the corresponding call
8006 * to after_for.
8008 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
8009 void *user)
8011 struct isl_test_codegen_data *data = user;
8012 isl_ctx *ctx;
8013 isl_space *space;
8014 isl_union_map *schedule;
8015 isl_union_set *uset;
8016 isl_set *set;
8017 isl_bool empty;
8018 char name[] = "d0";
8020 ctx = isl_ast_build_get_ctx(build);
8022 if (data->before >= 3)
8023 isl_die(ctx, isl_error_unknown,
8024 "unexpected number of for nodes", return NULL);
8025 if (data->depth >= 2)
8026 isl_die(ctx, isl_error_unknown,
8027 "unexpected depth", return NULL);
8029 snprintf(name, sizeof(name), "d%d", data->depth);
8030 data->before++;
8031 data->depth++;
8033 schedule = isl_ast_build_get_schedule(build);
8034 uset = isl_union_map_range(schedule);
8035 if (!uset)
8036 return NULL;
8037 if (isl_union_set_n_set(uset) != 1) {
8038 isl_union_set_free(uset);
8039 isl_die(ctx, isl_error_unknown,
8040 "expecting single range space", return NULL);
8043 space = isl_ast_build_get_schedule_space(build);
8044 set = isl_union_set_extract_set(uset, space);
8045 isl_union_set_free(uset);
8046 empty = isl_set_is_empty(set);
8047 isl_set_free(set);
8049 if (empty < 0)
8050 return NULL;
8051 if (empty)
8052 isl_die(ctx, isl_error_unknown,
8053 "spaces don't match", return NULL);
8055 return isl_id_alloc(ctx, name, NULL);
8058 /* This function is called after each for loop in the AST generated
8059 * from test_ast_gen1.
8061 * Increment the number of calls and decrement the depth.
8062 * Check that the annotation attached to the node matches
8063 * the isl_id returned by the corresponding call to before_for.
8065 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
8066 __isl_keep isl_ast_build *build, void *user)
8068 struct isl_test_codegen_data *data = user;
8069 isl_id *id;
8070 const char *name;
8071 int valid;
8073 data->after++;
8074 data->depth--;
8076 if (data->after > data->before)
8077 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8078 "mismatch in number of for nodes",
8079 return isl_ast_node_free(node));
8081 id = isl_ast_node_get_annotation(node);
8082 if (!id)
8083 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8084 "missing annotation", return isl_ast_node_free(node));
8086 name = isl_id_get_name(id);
8087 valid = name && atoi(name + 1) == data->depth;
8088 isl_id_free(id);
8090 if (!valid)
8091 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8092 "wrong annotation", return isl_ast_node_free(node));
8094 return node;
8097 /* Check that the before_each_for and after_each_for callbacks
8098 * are called for each for loop in the generated code,
8099 * that they are called in the right order and that the isl_id
8100 * returned from the before_each_for callback is attached to
8101 * the isl_ast_node passed to the corresponding after_each_for call.
8103 static int test_ast_gen1(isl_ctx *ctx)
8105 const char *str;
8106 isl_set *set;
8107 isl_union_map *schedule;
8108 isl_ast_build *build;
8109 isl_ast_node *tree;
8110 struct isl_test_codegen_data data;
8112 str = "[N] -> { : N >= 10 }";
8113 set = isl_set_read_from_str(ctx, str);
8114 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
8115 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
8116 schedule = isl_union_map_read_from_str(ctx, str);
8118 data.before = 0;
8119 data.after = 0;
8120 data.depth = 0;
8121 build = isl_ast_build_from_context(set);
8122 build = isl_ast_build_set_before_each_for(build,
8123 &before_for, &data);
8124 build = isl_ast_build_set_after_each_for(build,
8125 &after_for, &data);
8126 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8127 isl_ast_build_free(build);
8128 if (!tree)
8129 return -1;
8131 isl_ast_node_free(tree);
8133 if (data.before != 3 || data.after != 3)
8134 isl_die(ctx, isl_error_unknown,
8135 "unexpected number of for nodes", return -1);
8137 return 0;
8140 /* Check that the AST generator handles domains that are integrally disjoint
8141 * but not rationally disjoint.
8143 static int test_ast_gen2(isl_ctx *ctx)
8145 const char *str;
8146 isl_set *set;
8147 isl_union_map *schedule;
8148 isl_union_map *options;
8149 isl_ast_build *build;
8150 isl_ast_node *tree;
8152 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
8153 schedule = isl_union_map_read_from_str(ctx, str);
8154 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8155 build = isl_ast_build_from_context(set);
8157 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
8158 options = isl_union_map_read_from_str(ctx, str);
8159 build = isl_ast_build_set_options(build, options);
8160 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8161 isl_ast_build_free(build);
8162 if (!tree)
8163 return -1;
8164 isl_ast_node_free(tree);
8166 return 0;
8169 /* Increment *user on each call.
8171 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
8172 __isl_keep isl_ast_build *build, void *user)
8174 int *n = user;
8176 (*n)++;
8178 return node;
8181 /* Test that unrolling tries to minimize the number of instances.
8182 * In particular, for the schedule given below, make sure it generates
8183 * 3 nodes (rather than 101).
8185 static int test_ast_gen3(isl_ctx *ctx)
8187 const char *str;
8188 isl_set *set;
8189 isl_union_map *schedule;
8190 isl_union_map *options;
8191 isl_ast_build *build;
8192 isl_ast_node *tree;
8193 int n_domain = 0;
8195 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
8196 schedule = isl_union_map_read_from_str(ctx, str);
8197 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8199 str = "{ [i] -> unroll[0] }";
8200 options = isl_union_map_read_from_str(ctx, str);
8202 build = isl_ast_build_from_context(set);
8203 build = isl_ast_build_set_options(build, options);
8204 build = isl_ast_build_set_at_each_domain(build,
8205 &count_domains, &n_domain);
8206 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8207 isl_ast_build_free(build);
8208 if (!tree)
8209 return -1;
8211 isl_ast_node_free(tree);
8213 if (n_domain != 3)
8214 isl_die(ctx, isl_error_unknown,
8215 "unexpected number of for nodes", return -1);
8217 return 0;
8220 /* Check that if the ast_build_exploit_nested_bounds options is set,
8221 * we do not get an outer if node in the generated AST,
8222 * while we do get such an outer if node if the options is not set.
8224 static int test_ast_gen4(isl_ctx *ctx)
8226 const char *str;
8227 isl_set *set;
8228 isl_union_map *schedule;
8229 isl_ast_build *build;
8230 isl_ast_node *tree;
8231 enum isl_ast_node_type type;
8232 int enb;
8234 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
8235 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
8237 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
8239 schedule = isl_union_map_read_from_str(ctx, str);
8240 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8241 build = isl_ast_build_from_context(set);
8242 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8243 isl_ast_build_free(build);
8244 if (!tree)
8245 return -1;
8247 type = isl_ast_node_get_type(tree);
8248 isl_ast_node_free(tree);
8250 if (type == isl_ast_node_if)
8251 isl_die(ctx, isl_error_unknown,
8252 "not expecting if node", return -1);
8254 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
8256 schedule = isl_union_map_read_from_str(ctx, str);
8257 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8258 build = isl_ast_build_from_context(set);
8259 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8260 isl_ast_build_free(build);
8261 if (!tree)
8262 return -1;
8264 type = isl_ast_node_get_type(tree);
8265 isl_ast_node_free(tree);
8267 if (type != isl_ast_node_if)
8268 isl_die(ctx, isl_error_unknown,
8269 "expecting if node", return -1);
8271 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
8273 return 0;
8276 /* This function is called for each leaf in the AST generated
8277 * from test_ast_gen5.
8279 * We finalize the AST generation by extending the outer schedule
8280 * with a zero-dimensional schedule. If this results in any for loops,
8281 * then this means that we did not pass along enough information
8282 * about the outer schedule to the inner AST generation.
8284 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
8285 void *user)
8287 isl_union_map *schedule, *extra;
8288 isl_ast_node *tree;
8290 schedule = isl_ast_build_get_schedule(build);
8291 extra = isl_union_map_copy(schedule);
8292 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
8293 schedule = isl_union_map_range_product(schedule, extra);
8294 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8295 isl_ast_build_free(build);
8297 if (!tree)
8298 return NULL;
8300 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
8301 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
8302 "code should not contain any for loop",
8303 return isl_ast_node_free(tree));
8305 return tree;
8308 /* Check that we do not lose any information when going back and
8309 * forth between internal and external schedule.
8311 * In particular, we create an AST where we unroll the only
8312 * non-constant dimension in the schedule. We therefore do
8313 * not expect any for loops in the AST. However, older versions
8314 * of isl would not pass along enough information about the outer
8315 * schedule when performing an inner code generation from a create_leaf
8316 * callback, resulting in the inner code generation producing a for loop.
8318 static int test_ast_gen5(isl_ctx *ctx)
8320 const char *str;
8321 isl_set *set;
8322 isl_union_map *schedule, *options;
8323 isl_ast_build *build;
8324 isl_ast_node *tree;
8326 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
8327 schedule = isl_union_map_read_from_str(ctx, str);
8329 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
8330 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
8331 options = isl_union_map_read_from_str(ctx, str);
8333 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8334 build = isl_ast_build_from_context(set);
8335 build = isl_ast_build_set_options(build, options);
8336 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
8337 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8338 isl_ast_build_free(build);
8339 isl_ast_node_free(tree);
8340 if (!tree)
8341 return -1;
8343 return 0;
8346 /* Check that the expression
8348 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
8350 * is not combined into
8352 * min(n/2, 0)
8354 * as this would result in n/2 being evaluated in parts of
8355 * the definition domain where n is not a multiple of 2.
8357 static int test_ast_expr(isl_ctx *ctx)
8359 const char *str;
8360 isl_pw_aff *pa;
8361 isl_ast_build *build;
8362 isl_ast_expr *expr;
8363 int min_max;
8364 int is_min;
8366 min_max = isl_options_get_ast_build_detect_min_max(ctx);
8367 isl_options_set_ast_build_detect_min_max(ctx, 1);
8369 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
8370 pa = isl_pw_aff_read_from_str(ctx, str);
8371 build = isl_ast_build_alloc(ctx);
8372 expr = isl_ast_build_expr_from_pw_aff(build, pa);
8373 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
8374 isl_ast_expr_get_op_type(expr) == isl_ast_op_min;
8375 isl_ast_build_free(build);
8376 isl_ast_expr_free(expr);
8378 isl_options_set_ast_build_detect_min_max(ctx, min_max);
8380 if (!expr)
8381 return -1;
8382 if (is_min)
8383 isl_die(ctx, isl_error_unknown,
8384 "expressions should not be combined", return -1);
8386 return 0;
8389 static int test_ast_gen(isl_ctx *ctx)
8391 if (test_ast_gen1(ctx) < 0)
8392 return -1;
8393 if (test_ast_gen2(ctx) < 0)
8394 return -1;
8395 if (test_ast_gen3(ctx) < 0)
8396 return -1;
8397 if (test_ast_gen4(ctx) < 0)
8398 return -1;
8399 if (test_ast_gen5(ctx) < 0)
8400 return -1;
8401 if (test_ast_expr(ctx) < 0)
8402 return -1;
8403 return 0;
8406 /* Check if dropping output dimensions from an isl_pw_multi_aff
8407 * works properly.
8409 static int test_pw_multi_aff(isl_ctx *ctx)
8411 const char *str;
8412 isl_pw_multi_aff *pma1, *pma2;
8413 int equal;
8415 str = "{ [i,j] -> [i+j, 4i-j] }";
8416 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
8417 str = "{ [i,j] -> [4i-j] }";
8418 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
8420 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
8422 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
8424 isl_pw_multi_aff_free(pma1);
8425 isl_pw_multi_aff_free(pma2);
8426 if (equal < 0)
8427 return -1;
8428 if (!equal)
8429 isl_die(ctx, isl_error_unknown,
8430 "expressions not equal", return -1);
8432 return 0;
8435 /* Check that we can properly parse multi piecewise affine expressions
8436 * where the piecewise affine expressions have different domains.
8438 static int test_multi_pw_aff_1(isl_ctx *ctx)
8440 const char *str;
8441 isl_set *dom, *dom2;
8442 isl_multi_pw_aff *mpa1, *mpa2;
8443 isl_pw_aff *pa;
8444 int equal;
8445 int equal_domain;
8447 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
8448 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
8449 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
8450 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
8451 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
8452 str = "{ [i] -> [(i : i > 0), 2i] }";
8453 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
8455 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
8457 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
8458 dom = isl_pw_aff_domain(pa);
8459 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
8460 dom2 = isl_pw_aff_domain(pa);
8461 equal_domain = isl_set_is_equal(dom, dom2);
8463 isl_set_free(dom);
8464 isl_set_free(dom2);
8465 isl_multi_pw_aff_free(mpa1);
8466 isl_multi_pw_aff_free(mpa2);
8468 if (equal < 0)
8469 return -1;
8470 if (!equal)
8471 isl_die(ctx, isl_error_unknown,
8472 "expressions not equal", return -1);
8474 if (equal_domain < 0)
8475 return -1;
8476 if (equal_domain)
8477 isl_die(ctx, isl_error_unknown,
8478 "domains unexpectedly equal", return -1);
8480 return 0;
8483 /* Check that the dimensions in the explicit domain
8484 * of a multi piecewise affine expression are properly
8485 * taken into account.
8487 static int test_multi_pw_aff_2(isl_ctx *ctx)
8489 const char *str;
8490 isl_bool involves1, involves2, involves3, equal;
8491 isl_multi_pw_aff *mpa, *mpa1, *mpa2;
8493 str = "{ A[x,y] -> B[] : x >= y }";
8494 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
8495 involves1 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 2);
8496 mpa1 = isl_multi_pw_aff_copy(mpa);
8498 mpa = isl_multi_pw_aff_insert_dims(mpa, isl_dim_in, 0, 1);
8499 involves2 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 1);
8500 involves3 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 1, 2);
8501 str = "{ [a,x,y] -> B[] : x >= y }";
8502 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
8503 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
8504 isl_multi_pw_aff_free(mpa2);
8506 mpa = isl_multi_pw_aff_drop_dims(mpa, isl_dim_in, 0, 1);
8507 mpa = isl_multi_pw_aff_set_tuple_name(mpa, isl_dim_in, "A");
8508 if (equal >= 0 && equal)
8509 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa1);
8510 isl_multi_pw_aff_free(mpa1);
8511 isl_multi_pw_aff_free(mpa);
8513 if (involves1 < 0 || involves2 < 0 || involves3 < 0 || equal < 0)
8514 return -1;
8515 if (!equal)
8516 isl_die(ctx, isl_error_unknown,
8517 "incorrect result of dimension insertion/removal",
8518 return isl_stat_error);
8519 if (!involves1 || involves2 || !involves3)
8520 isl_die(ctx, isl_error_unknown,
8521 "incorrect characterization of involved dimensions",
8522 return isl_stat_error);
8524 return 0;
8527 /* Check that isl_multi_union_pw_aff_multi_val_on_domain
8528 * sets the explicit domain of a zero-dimensional result,
8529 * such that it can be converted to an isl_union_map.
8531 static isl_stat test_multi_pw_aff_3(isl_ctx *ctx)
8533 isl_space *space;
8534 isl_union_set *dom;
8535 isl_multi_val *mv;
8536 isl_multi_union_pw_aff *mupa;
8537 isl_union_map *umap;
8539 dom = isl_union_set_read_from_str(ctx, "{ A[]; B[] }");
8540 space = isl_union_set_get_space(dom);
8541 mv = isl_multi_val_zero(isl_space_set_from_params(space));
8542 mupa = isl_multi_union_pw_aff_multi_val_on_domain(dom, mv);
8543 umap = isl_union_map_from_multi_union_pw_aff(mupa);
8544 isl_union_map_free(umap);
8545 if (!umap)
8546 return isl_stat_error;
8548 return isl_stat_ok;
8551 /* Perform some tests on multi piecewise affine expressions.
8553 static int test_multi_pw_aff(isl_ctx *ctx)
8555 if (test_multi_pw_aff_1(ctx) < 0)
8556 return -1;
8557 if (test_multi_pw_aff_2(ctx) < 0)
8558 return -1;
8559 if (test_multi_pw_aff_3(ctx) < 0)
8560 return -1;
8561 return 0;
8564 /* This is a regression test for a bug where isl_basic_map_simplify
8565 * would end up in an infinite loop. In particular, we construct
8566 * an empty basic set that is not obviously empty.
8567 * isl_basic_set_is_empty marks the basic set as empty.
8568 * After projecting out i3, the variable can be dropped completely,
8569 * but isl_basic_map_simplify refrains from doing so if the basic set
8570 * is empty and would end up in an infinite loop if it didn't test
8571 * explicitly for empty basic maps in the outer loop.
8573 static int test_simplify_1(isl_ctx *ctx)
8575 const char *str;
8576 isl_basic_set *bset;
8577 int empty;
8579 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
8580 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
8581 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
8582 "i3 >= i2 }";
8583 bset = isl_basic_set_read_from_str(ctx, str);
8584 empty = isl_basic_set_is_empty(bset);
8585 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
8586 isl_basic_set_free(bset);
8587 if (!bset)
8588 return -1;
8589 if (!empty)
8590 isl_die(ctx, isl_error_unknown,
8591 "basic set should be empty", return -1);
8593 return 0;
8596 /* Check that the equality in the set description below
8597 * is simplified away.
8599 static int test_simplify_2(isl_ctx *ctx)
8601 const char *str;
8602 isl_basic_set *bset;
8603 isl_bool universe;
8605 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
8606 bset = isl_basic_set_read_from_str(ctx, str);
8607 universe = isl_basic_set_plain_is_universe(bset);
8608 isl_basic_set_free(bset);
8610 if (universe < 0)
8611 return -1;
8612 if (!universe)
8613 isl_die(ctx, isl_error_unknown,
8614 "equality not simplified away", return -1);
8615 return 0;
8618 /* Some simplification tests.
8620 static int test_simplify(isl_ctx *ctx)
8622 if (test_simplify_1(ctx) < 0)
8623 return -1;
8624 if (test_simplify_2(ctx) < 0)
8625 return -1;
8626 return 0;
8629 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
8630 * with gbr context would fail to disable the use of the shifted tableau
8631 * when transferring equalities for the input to the context, resulting
8632 * in invalid sample values.
8634 static int test_partial_lexmin(isl_ctx *ctx)
8636 const char *str;
8637 isl_basic_set *bset;
8638 isl_basic_map *bmap;
8639 isl_map *map;
8641 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
8642 bmap = isl_basic_map_read_from_str(ctx, str);
8643 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
8644 bset = isl_basic_set_read_from_str(ctx, str);
8645 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
8646 isl_map_free(map);
8648 if (!map)
8649 return -1;
8651 return 0;
8654 /* Check that the variable compression performed on the existentially
8655 * quantified variables inside isl_basic_set_compute_divs is not confused
8656 * by the implicit equalities among the parameters.
8658 static int test_compute_divs(isl_ctx *ctx)
8660 const char *str;
8661 isl_basic_set *bset;
8662 isl_set *set;
8664 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
8665 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
8666 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
8667 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
8668 bset = isl_basic_set_read_from_str(ctx, str);
8669 set = isl_basic_set_compute_divs(bset);
8670 isl_set_free(set);
8671 if (!set)
8672 return -1;
8674 return 0;
8677 /* Check that isl_schedule_get_map is not confused by a schedule tree
8678 * with divergent filter node parameters, as can result from a call
8679 * to isl_schedule_intersect_domain.
8681 static int test_schedule_tree(isl_ctx *ctx)
8683 const char *str;
8684 isl_union_set *uset;
8685 isl_schedule *sched1, *sched2;
8686 isl_union_map *umap;
8688 uset = isl_union_set_read_from_str(ctx, "{ A[i] }");
8689 sched1 = isl_schedule_from_domain(uset);
8690 uset = isl_union_set_read_from_str(ctx, "{ B[] }");
8691 sched2 = isl_schedule_from_domain(uset);
8693 sched1 = isl_schedule_sequence(sched1, sched2);
8694 str = "[n] -> { A[i] : 0 <= i < n; B[] }";
8695 uset = isl_union_set_read_from_str(ctx, str);
8696 sched1 = isl_schedule_intersect_domain(sched1, uset);
8697 umap = isl_schedule_get_map(sched1);
8698 isl_schedule_free(sched1);
8699 isl_union_map_free(umap);
8700 if (!umap)
8701 return -1;
8703 return 0;
8706 /* Check that a zero-dimensional prefix schedule keeps track
8707 * of the domain and outer filters.
8709 static int test_schedule_tree_prefix(isl_ctx *ctx)
8711 const char *str;
8712 isl_bool equal;
8713 isl_union_set *uset;
8714 isl_union_set_list *filters;
8715 isl_multi_union_pw_aff *mupa, *mupa2;
8716 isl_schedule_node *node;
8718 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
8719 uset = isl_union_set_read_from_str(ctx, str);
8720 node = isl_schedule_node_from_domain(uset);
8721 node = isl_schedule_node_child(node, 0);
8723 str = "{ S1[i,j] : i > j }";
8724 uset = isl_union_set_read_from_str(ctx, str);
8725 filters = isl_union_set_list_from_union_set(uset);
8726 str = "{ S1[i,j] : i <= j; S2[i,j] }";
8727 uset = isl_union_set_read_from_str(ctx, str);
8728 filters = isl_union_set_list_add(filters, uset);
8729 node = isl_schedule_node_insert_sequence(node, filters);
8731 node = isl_schedule_node_child(node, 0);
8732 node = isl_schedule_node_child(node, 0);
8733 mupa = isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
8734 str = "([] : { S1[i,j] : i > j })";
8735 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx, str);
8736 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
8737 isl_multi_union_pw_aff_free(mupa2);
8738 isl_multi_union_pw_aff_free(mupa);
8739 isl_schedule_node_free(node);
8741 if (equal < 0)
8742 return -1;
8743 if (!equal)
8744 isl_die(ctx, isl_error_unknown, "unexpected prefix schedule",
8745 return -1);
8747 return 0;
8750 /* Check that the reaching domain elements and the prefix schedule
8751 * at a leaf node are the same before and after grouping.
8753 static int test_schedule_tree_group_1(isl_ctx *ctx)
8755 int equal;
8756 const char *str;
8757 isl_id *id;
8758 isl_union_set *uset;
8759 isl_multi_union_pw_aff *mupa;
8760 isl_union_pw_multi_aff *upma1, *upma2;
8761 isl_union_set *domain1, *domain2;
8762 isl_union_map *umap1, *umap2;
8763 isl_schedule_node *node;
8765 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
8766 uset = isl_union_set_read_from_str(ctx, str);
8767 node = isl_schedule_node_from_domain(uset);
8768 node = isl_schedule_node_child(node, 0);
8769 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
8770 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8771 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8772 node = isl_schedule_node_child(node, 0);
8773 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
8774 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8775 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8776 node = isl_schedule_node_child(node, 0);
8777 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
8778 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
8779 domain1 = isl_schedule_node_get_domain(node);
8780 id = isl_id_alloc(ctx, "group", NULL);
8781 node = isl_schedule_node_parent(node);
8782 node = isl_schedule_node_group(node, id);
8783 node = isl_schedule_node_child(node, 0);
8784 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
8785 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
8786 domain2 = isl_schedule_node_get_domain(node);
8787 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
8788 if (equal >= 0 && equal)
8789 equal = isl_union_set_is_equal(domain1, domain2);
8790 if (equal >= 0 && equal)
8791 equal = isl_union_map_is_equal(umap1, umap2);
8792 isl_union_map_free(umap1);
8793 isl_union_map_free(umap2);
8794 isl_union_set_free(domain1);
8795 isl_union_set_free(domain2);
8796 isl_union_pw_multi_aff_free(upma1);
8797 isl_union_pw_multi_aff_free(upma2);
8798 isl_schedule_node_free(node);
8800 if (equal < 0)
8801 return -1;
8802 if (!equal)
8803 isl_die(ctx, isl_error_unknown,
8804 "expressions not equal", return -1);
8806 return 0;
8809 /* Check that we can have nested groupings and that the union map
8810 * schedule representation is the same before and after the grouping.
8811 * Note that after the grouping, the union map representation contains
8812 * the domain constraints from the ranges of the expansion nodes,
8813 * while they are missing from the union map representation of
8814 * the tree without expansion nodes.
8816 * Also check that the global expansion is as expected.
8818 static int test_schedule_tree_group_2(isl_ctx *ctx)
8820 int equal, equal_expansion;
8821 const char *str;
8822 isl_id *id;
8823 isl_union_set *uset;
8824 isl_union_map *umap1, *umap2;
8825 isl_union_map *expansion1, *expansion2;
8826 isl_union_set_list *filters;
8827 isl_multi_union_pw_aff *mupa;
8828 isl_schedule *schedule;
8829 isl_schedule_node *node;
8831 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
8832 "S3[i,j] : 0 <= i,j < 10 }";
8833 uset = isl_union_set_read_from_str(ctx, str);
8834 node = isl_schedule_node_from_domain(uset);
8835 node = isl_schedule_node_child(node, 0);
8836 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
8837 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8838 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8839 node = isl_schedule_node_child(node, 0);
8840 str = "{ S1[i,j] }";
8841 uset = isl_union_set_read_from_str(ctx, str);
8842 filters = isl_union_set_list_from_union_set(uset);
8843 str = "{ S2[i,j]; S3[i,j] }";
8844 uset = isl_union_set_read_from_str(ctx, str);
8845 filters = isl_union_set_list_add(filters, uset);
8846 node = isl_schedule_node_insert_sequence(node, filters);
8847 node = isl_schedule_node_child(node, 1);
8848 node = isl_schedule_node_child(node, 0);
8849 str = "{ S2[i,j] }";
8850 uset = isl_union_set_read_from_str(ctx, str);
8851 filters = isl_union_set_list_from_union_set(uset);
8852 str = "{ S3[i,j] }";
8853 uset = isl_union_set_read_from_str(ctx, str);
8854 filters = isl_union_set_list_add(filters, uset);
8855 node = isl_schedule_node_insert_sequence(node, filters);
8857 schedule = isl_schedule_node_get_schedule(node);
8858 umap1 = isl_schedule_get_map(schedule);
8859 uset = isl_schedule_get_domain(schedule);
8860 umap1 = isl_union_map_intersect_domain(umap1, uset);
8861 isl_schedule_free(schedule);
8863 node = isl_schedule_node_parent(node);
8864 node = isl_schedule_node_parent(node);
8865 id = isl_id_alloc(ctx, "group1", NULL);
8866 node = isl_schedule_node_group(node, id);
8867 node = isl_schedule_node_child(node, 1);
8868 node = isl_schedule_node_child(node, 0);
8869 id = isl_id_alloc(ctx, "group2", NULL);
8870 node = isl_schedule_node_group(node, id);
8872 schedule = isl_schedule_node_get_schedule(node);
8873 umap2 = isl_schedule_get_map(schedule);
8874 isl_schedule_free(schedule);
8876 node = isl_schedule_node_root(node);
8877 node = isl_schedule_node_child(node, 0);
8878 expansion1 = isl_schedule_node_get_subtree_expansion(node);
8879 isl_schedule_node_free(node);
8881 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
8882 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
8883 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
8885 expansion2 = isl_union_map_read_from_str(ctx, str);
8887 equal = isl_union_map_is_equal(umap1, umap2);
8888 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
8890 isl_union_map_free(umap1);
8891 isl_union_map_free(umap2);
8892 isl_union_map_free(expansion1);
8893 isl_union_map_free(expansion2);
8895 if (equal < 0 || equal_expansion < 0)
8896 return -1;
8897 if (!equal)
8898 isl_die(ctx, isl_error_unknown,
8899 "expressions not equal", return -1);
8900 if (!equal_expansion)
8901 isl_die(ctx, isl_error_unknown,
8902 "unexpected expansion", return -1);
8904 return 0;
8907 /* Some tests for the isl_schedule_node_group function.
8909 static int test_schedule_tree_group(isl_ctx *ctx)
8911 if (test_schedule_tree_group_1(ctx) < 0)
8912 return -1;
8913 if (test_schedule_tree_group_2(ctx) < 0)
8914 return -1;
8915 return 0;
8918 struct {
8919 const char *set;
8920 const char *dual;
8921 } coef_tests[] = {
8922 { "{ rat: [i] : 0 <= i <= 10 }",
8923 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
8924 { "{ rat: [i] : FALSE }",
8925 "{ rat: coefficients[[cst] -> [a]] }" },
8926 { "{ rat: [i] : }",
8927 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
8930 struct {
8931 const char *set;
8932 const char *dual;
8933 } sol_tests[] = {
8934 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
8935 "{ rat: [i] : 0 <= i <= 10 }" },
8936 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
8937 "{ rat: [i] }" },
8938 { "{ rat: coefficients[[cst] -> [a]] }",
8939 "{ rat: [i] : FALSE }" },
8942 /* Test the basic functionality of isl_basic_set_coefficients and
8943 * isl_basic_set_solutions.
8945 static int test_dual(isl_ctx *ctx)
8947 int i;
8949 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
8950 int equal;
8951 isl_basic_set *bset1, *bset2;
8953 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
8954 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
8955 bset1 = isl_basic_set_coefficients(bset1);
8956 equal = isl_basic_set_is_equal(bset1, bset2);
8957 isl_basic_set_free(bset1);
8958 isl_basic_set_free(bset2);
8959 if (equal < 0)
8960 return -1;
8961 if (!equal)
8962 isl_die(ctx, isl_error_unknown,
8963 "incorrect dual", return -1);
8966 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
8967 int equal;
8968 isl_basic_set *bset1, *bset2;
8970 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
8971 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
8972 bset1 = isl_basic_set_solutions(bset1);
8973 equal = isl_basic_set_is_equal(bset1, bset2);
8974 isl_basic_set_free(bset1);
8975 isl_basic_set_free(bset2);
8976 if (equal < 0)
8977 return -1;
8978 if (!equal)
8979 isl_die(ctx, isl_error_unknown,
8980 "incorrect dual", return -1);
8983 return 0;
8986 struct {
8987 int scale_tile;
8988 int shift_point;
8989 const char *domain;
8990 const char *schedule;
8991 const char *sizes;
8992 const char *tile;
8993 const char *point;
8994 } tile_tests[] = {
8995 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
8996 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
8997 "{ [32,32] }",
8998 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
8999 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9001 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
9002 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9003 "{ [32,32] }",
9004 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9005 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9007 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9008 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9009 "{ [32,32] }",
9010 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
9011 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9013 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9014 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9015 "{ [32,32] }",
9016 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9017 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9021 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
9022 * tile the band and then check if the tile and point bands have the
9023 * expected partial schedule.
9025 static int test_tile(isl_ctx *ctx)
9027 int i;
9028 int scale;
9029 int shift;
9031 scale = isl_options_get_tile_scale_tile_loops(ctx);
9032 shift = isl_options_get_tile_shift_point_loops(ctx);
9034 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
9035 int opt;
9036 int equal;
9037 const char *str;
9038 isl_union_set *domain;
9039 isl_multi_union_pw_aff *mupa, *mupa2;
9040 isl_schedule_node *node;
9041 isl_multi_val *sizes;
9043 opt = tile_tests[i].scale_tile;
9044 isl_options_set_tile_scale_tile_loops(ctx, opt);
9045 opt = tile_tests[i].shift_point;
9046 isl_options_set_tile_shift_point_loops(ctx, opt);
9048 str = tile_tests[i].domain;
9049 domain = isl_union_set_read_from_str(ctx, str);
9050 node = isl_schedule_node_from_domain(domain);
9051 node = isl_schedule_node_child(node, 0);
9052 str = tile_tests[i].schedule;
9053 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9054 node = isl_schedule_node_insert_partial_schedule(node, mupa);
9055 str = tile_tests[i].sizes;
9056 sizes = isl_multi_val_read_from_str(ctx, str);
9057 node = isl_schedule_node_band_tile(node, sizes);
9059 str = tile_tests[i].tile;
9060 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9061 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9062 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
9063 isl_multi_union_pw_aff_free(mupa);
9064 isl_multi_union_pw_aff_free(mupa2);
9066 node = isl_schedule_node_child(node, 0);
9068 str = tile_tests[i].point;
9069 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9070 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9071 if (equal >= 0 && equal)
9072 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
9073 mupa2);
9074 isl_multi_union_pw_aff_free(mupa);
9075 isl_multi_union_pw_aff_free(mupa2);
9077 isl_schedule_node_free(node);
9079 if (equal < 0)
9080 return -1;
9081 if (!equal)
9082 isl_die(ctx, isl_error_unknown,
9083 "unexpected result", return -1);
9086 isl_options_set_tile_scale_tile_loops(ctx, scale);
9087 isl_options_set_tile_shift_point_loops(ctx, shift);
9089 return 0;
9092 /* Check that the domain hash of a space is equal to the hash
9093 * of the domain of the space.
9095 static int test_domain_hash(isl_ctx *ctx)
9097 isl_map *map;
9098 isl_space *space;
9099 uint32_t hash1, hash2;
9101 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
9102 space = isl_map_get_space(map);
9103 isl_map_free(map);
9104 hash1 = isl_space_get_domain_hash(space);
9105 space = isl_space_domain(space);
9106 hash2 = isl_space_get_hash(space);
9107 isl_space_free(space);
9109 if (!space)
9110 return -1;
9111 if (hash1 != hash2)
9112 isl_die(ctx, isl_error_unknown,
9113 "domain hash not equal to hash of domain", return -1);
9115 return 0;
9118 /* Check that a universe basic set that is not obviously equal to the universe
9119 * is still recognized as being equal to the universe.
9121 static int test_universe(isl_ctx *ctx)
9123 const char *s;
9124 isl_basic_set *bset;
9125 isl_bool is_univ;
9127 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
9128 bset = isl_basic_set_read_from_str(ctx, s);
9129 is_univ = isl_basic_set_is_universe(bset);
9130 isl_basic_set_free(bset);
9132 if (is_univ < 0)
9133 return -1;
9134 if (!is_univ)
9135 isl_die(ctx, isl_error_unknown,
9136 "not recognized as universe set", return -1);
9138 return 0;
9141 /* Sets for which chambers are computed and checked.
9143 const char *chambers_tests[] = {
9144 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
9145 "z >= 0 and z <= C - y and z <= B - x - y }",
9148 /* Add the domain of "cell" to "cells".
9150 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
9152 isl_basic_set_list **cells = user;
9153 isl_basic_set *dom;
9155 dom = isl_cell_get_domain(cell);
9156 isl_cell_free(cell);
9157 *cells = isl_basic_set_list_add(*cells, dom);
9159 return *cells ? isl_stat_ok : isl_stat_error;
9162 /* Check that the elements of "list" are pairwise disjoint.
9164 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
9166 int i, j, n;
9168 if (!list)
9169 return isl_stat_error;
9171 n = isl_basic_set_list_n_basic_set(list);
9172 for (i = 0; i < n; ++i) {
9173 isl_basic_set *bset_i;
9175 bset_i = isl_basic_set_list_get_basic_set(list, i);
9176 for (j = i + 1; j < n; ++j) {
9177 isl_basic_set *bset_j;
9178 isl_bool disjoint;
9180 bset_j = isl_basic_set_list_get_basic_set(list, j);
9181 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
9182 isl_basic_set_free(bset_j);
9183 if (!disjoint)
9184 isl_die(isl_basic_set_list_get_ctx(list),
9185 isl_error_unknown, "not disjoint",
9186 break);
9187 if (disjoint < 0 || !disjoint)
9188 break;
9190 isl_basic_set_free(bset_i);
9191 if (j < n)
9192 return isl_stat_error;
9195 return isl_stat_ok;
9198 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
9199 * are pairwise disjoint.
9201 static int test_chambers(isl_ctx *ctx)
9203 int i;
9205 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
9206 isl_basic_set *bset;
9207 isl_vertices *vertices;
9208 isl_basic_set_list *cells;
9209 isl_stat ok;
9211 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
9212 vertices = isl_basic_set_compute_vertices(bset);
9213 cells = isl_basic_set_list_alloc(ctx, 0);
9214 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
9215 &cells) < 0)
9216 cells = isl_basic_set_list_free(cells);
9217 ok = check_pairwise_disjoint(cells);
9218 isl_basic_set_list_free(cells);
9219 isl_vertices_free(vertices);
9220 isl_basic_set_free(bset);
9222 if (ok < 0)
9223 return -1;
9226 return 0;
9229 struct {
9230 const char *name;
9231 int (*fn)(isl_ctx *ctx);
9232 } tests [] = {
9233 { "universe", &test_universe },
9234 { "domain hash", &test_domain_hash },
9235 { "dual", &test_dual },
9236 { "dependence analysis", &test_flow },
9237 { "val", &test_val },
9238 { "compute divs", &test_compute_divs },
9239 { "partial lexmin", &test_partial_lexmin },
9240 { "simplify", &test_simplify },
9241 { "curry", &test_curry },
9242 { "piecewise multi affine expressions", &test_pw_multi_aff },
9243 { "multi piecewise affine expressions", &test_multi_pw_aff },
9244 { "conversion", &test_conversion },
9245 { "list", &test_list },
9246 { "align parameters", &test_align_parameters },
9247 { "drop unused parameters", &test_drop_unused_parameters },
9248 { "preimage", &test_preimage },
9249 { "pullback", &test_pullback },
9250 { "AST", &test_ast },
9251 { "AST build", &test_ast_build },
9252 { "AST generation", &test_ast_gen },
9253 { "eliminate", &test_eliminate },
9254 { "residue class", &test_residue_class },
9255 { "div", &test_div },
9256 { "slice", &test_slice },
9257 { "fixed power", &test_fixed_power },
9258 { "sample", &test_sample },
9259 { "output", &test_output },
9260 { "vertices", &test_vertices },
9261 { "chambers", &test_chambers },
9262 { "fixed", &test_fixed },
9263 { "equal", &test_equal },
9264 { "disjoint", &test_disjoint },
9265 { "product", &test_product },
9266 { "dim_max", &test_dim_max },
9267 { "affine", &test_aff },
9268 { "injective", &test_injective },
9269 { "schedule (whole component)", &test_schedule_whole },
9270 { "schedule (incremental)", &test_schedule_incremental },
9271 { "schedule tree", &test_schedule_tree },
9272 { "schedule tree prefix", &test_schedule_tree_prefix },
9273 { "schedule tree grouping", &test_schedule_tree_group },
9274 { "tile", &test_tile },
9275 { "union_pw", &test_union_pw },
9276 { "locus", &test_locus },
9277 { "eval", &test_eval },
9278 { "parse", &test_parse },
9279 { "single-valued", &test_sv },
9280 { "affine hull", &test_affine_hull },
9281 { "simple_hull", &test_simple_hull },
9282 { "coalesce", &test_coalesce },
9283 { "factorize", &test_factorize },
9284 { "subset", &test_subset },
9285 { "subtract", &test_subtract },
9286 { "intersect", &test_intersect },
9287 { "lexmin", &test_lexmin },
9288 { "min", &test_min },
9289 { "gist", &test_gist },
9290 { "piecewise quasi-polynomials", &test_pwqp },
9291 { "lift", &test_lift },
9292 { "bound", &test_bound },
9293 { "get lists", &test_get_list },
9294 { "union", &test_union },
9295 { "split periods", &test_split_periods },
9296 { "lexicographic order", &test_lex },
9297 { "bijectivity", &test_bijective },
9298 { "dataflow analysis", &test_dep },
9299 { "reading", &test_read },
9300 { "bounded", &test_bounded },
9301 { "construction", &test_construction },
9302 { "dimension manipulation", &test_dim },
9303 { "map application", &test_application },
9304 { "convex hull", &test_convex_hull },
9305 { "transitive closure", &test_closure },
9306 { "isl_bool", &test_isl_bool},
9309 int main(int argc, char **argv)
9311 int i;
9312 struct isl_ctx *ctx;
9313 struct isl_options *options;
9315 options = isl_options_new_with_defaults();
9316 assert(options);
9317 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
9319 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
9320 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
9321 printf("%s\n", tests[i].name);
9322 if (tests[i].fn(ctx) < 0)
9323 goto error;
9325 isl_ctx_free(ctx);
9326 return 0;
9327 error:
9328 isl_ctx_free(ctx);
9329 return -1;