isl_mat_{cols,rows}: return isl_size
[isl.git] / isl_test.c
blob978d3bdcfb61b99ca7b602f7022d8c2c9e2440be
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 isl_size 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 (n < 0)
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 isl_size 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 return -1;
1277 if (n != 0)
1278 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1279 return -1);
1281 /* Check that isl_map_affine_hull is not confused by
1282 * the reordering of divs in isl_map_align_divs.
1284 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1285 "32e0 = b and 32e1 = c); "
1286 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1287 set = isl_set_read_from_str(ctx, str);
1288 bset = isl_set_affine_hull(set);
1289 isl_basic_set_free(bset);
1290 if (!bset)
1291 return -1;
1293 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1294 "32e2 = 31 + 31e0 }";
1295 set = isl_set_read_from_str(ctx, str);
1296 bset = isl_set_affine_hull(set);
1297 str = "{ [a] : exists e : a = 32 e }";
1298 bset2 = isl_basic_set_read_from_str(ctx, str);
1299 subset = isl_basic_set_is_subset(bset, bset2);
1300 isl_basic_set_free(bset);
1301 isl_basic_set_free(bset2);
1302 if (subset < 0)
1303 return -1;
1304 if (!subset)
1305 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1306 return -1);
1308 return 0;
1311 /* Pairs of maps and the corresponding expected results of
1312 * isl_map_plain_unshifted_simple_hull.
1314 struct {
1315 const char *map;
1316 const char *hull;
1317 } plain_unshifted_simple_hull_tests[] = {
1318 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1319 "{ [i] -> [j] : i >= 1 }" },
1320 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1321 "(j mod 4 = 2 and k mod 6 = n) }",
1322 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1325 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1327 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1329 int i;
1330 isl_map *map;
1331 isl_basic_map *hull, *expected;
1332 isl_bool equal;
1334 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1335 const char *str;
1336 str = plain_unshifted_simple_hull_tests[i].map;
1337 map = isl_map_read_from_str(ctx, str);
1338 str = plain_unshifted_simple_hull_tests[i].hull;
1339 expected = isl_basic_map_read_from_str(ctx, str);
1340 hull = isl_map_plain_unshifted_simple_hull(map);
1341 equal = isl_basic_map_is_equal(hull, expected);
1342 isl_basic_map_free(hull);
1343 isl_basic_map_free(expected);
1344 if (equal < 0)
1345 return -1;
1346 if (!equal)
1347 isl_die(ctx, isl_error_unknown, "unexpected hull",
1348 return -1);
1351 return 0;
1354 /* Pairs of sets and the corresponding expected results of
1355 * isl_set_unshifted_simple_hull.
1357 struct {
1358 const char *set;
1359 const char *hull;
1360 } unshifted_simple_hull_tests[] = {
1361 { "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1362 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1365 /* Basic tests for isl_set_unshifted_simple_hull.
1367 static int test_unshifted_simple_hull(isl_ctx *ctx)
1369 int i;
1370 isl_set *set;
1371 isl_basic_set *hull, *expected;
1372 isl_bool equal;
1374 for (i = 0; i < ARRAY_SIZE(unshifted_simple_hull_tests); ++i) {
1375 const char *str;
1376 str = unshifted_simple_hull_tests[i].set;
1377 set = isl_set_read_from_str(ctx, str);
1378 str = unshifted_simple_hull_tests[i].hull;
1379 expected = isl_basic_set_read_from_str(ctx, str);
1380 hull = isl_set_unshifted_simple_hull(set);
1381 equal = isl_basic_set_is_equal(hull, expected);
1382 isl_basic_set_free(hull);
1383 isl_basic_set_free(expected);
1384 if (equal < 0)
1385 return -1;
1386 if (!equal)
1387 isl_die(ctx, isl_error_unknown, "unexpected hull",
1388 return -1);
1391 return 0;
1394 static int test_simple_hull(struct isl_ctx *ctx)
1396 const char *str;
1397 isl_set *set;
1398 isl_basic_set *bset;
1399 isl_bool is_empty;
1401 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1402 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1403 set = isl_set_read_from_str(ctx, str);
1404 bset = isl_set_simple_hull(set);
1405 is_empty = isl_basic_set_is_empty(bset);
1406 isl_basic_set_free(bset);
1408 if (is_empty == isl_bool_error)
1409 return -1;
1411 if (is_empty == isl_bool_false)
1412 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1413 return -1);
1415 if (test_plain_unshifted_simple_hull(ctx) < 0)
1416 return -1;
1417 if (test_unshifted_simple_hull(ctx) < 0)
1418 return -1;
1420 return 0;
1423 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1425 char *filename;
1426 FILE *input;
1427 struct isl_basic_set *bset1, *bset2;
1428 struct isl_set *set;
1430 filename = get_filename(ctx, name, "polylib");
1431 assert(filename);
1432 input = fopen(filename, "r");
1433 assert(input);
1435 bset1 = isl_basic_set_read_from_file(ctx, input);
1436 bset2 = isl_basic_set_read_from_file(ctx, input);
1438 set = isl_basic_set_union(bset1, bset2);
1439 bset1 = isl_set_convex_hull(set);
1441 bset2 = isl_basic_set_read_from_file(ctx, input);
1443 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1445 isl_basic_set_free(bset1);
1446 isl_basic_set_free(bset2);
1447 free(filename);
1449 fclose(input);
1452 struct {
1453 const char *set;
1454 const char *hull;
1455 } convex_hull_tests[] = {
1456 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1457 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1458 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1459 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1460 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1461 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1462 "i2 <= 5 and i2 >= 4; "
1463 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1464 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1465 "i2 <= 5 + i0 and i2 >= i0 }" },
1466 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1467 "{ [x, y] : 1 = 0 }" },
1468 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1469 "[x, y, 0] : x >= 0 and y < 0 }",
1470 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1473 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1475 int i;
1476 int orig_convex = ctx->opt->convex;
1477 ctx->opt->convex = convex;
1479 test_convex_hull_case(ctx, "convex0");
1480 test_convex_hull_case(ctx, "convex1");
1481 test_convex_hull_case(ctx, "convex2");
1482 test_convex_hull_case(ctx, "convex3");
1483 test_convex_hull_case(ctx, "convex4");
1484 test_convex_hull_case(ctx, "convex5");
1485 test_convex_hull_case(ctx, "convex6");
1486 test_convex_hull_case(ctx, "convex7");
1487 test_convex_hull_case(ctx, "convex8");
1488 test_convex_hull_case(ctx, "convex9");
1489 test_convex_hull_case(ctx, "convex10");
1490 test_convex_hull_case(ctx, "convex11");
1491 test_convex_hull_case(ctx, "convex12");
1492 test_convex_hull_case(ctx, "convex13");
1493 test_convex_hull_case(ctx, "convex14");
1494 test_convex_hull_case(ctx, "convex15");
1496 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1497 isl_set *set1, *set2;
1498 int equal;
1500 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1501 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1502 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1503 equal = isl_set_is_equal(set1, set2);
1504 isl_set_free(set1);
1505 isl_set_free(set2);
1507 if (equal < 0)
1508 return -1;
1509 if (!equal)
1510 isl_die(ctx, isl_error_unknown,
1511 "unexpected convex hull", return -1);
1514 ctx->opt->convex = orig_convex;
1516 return 0;
1519 static int test_convex_hull(isl_ctx *ctx)
1521 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1522 return -1;
1523 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1524 return -1;
1525 return 0;
1528 void test_gist_case(struct isl_ctx *ctx, const char *name)
1530 char *filename;
1531 FILE *input;
1532 struct isl_basic_set *bset1, *bset2;
1534 filename = get_filename(ctx, name, "polylib");
1535 assert(filename);
1536 input = fopen(filename, "r");
1537 assert(input);
1539 bset1 = isl_basic_set_read_from_file(ctx, input);
1540 bset2 = isl_basic_set_read_from_file(ctx, input);
1542 bset1 = isl_basic_set_gist(bset1, bset2);
1544 bset2 = isl_basic_set_read_from_file(ctx, input);
1546 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1548 isl_basic_set_free(bset1);
1549 isl_basic_set_free(bset2);
1550 free(filename);
1552 fclose(input);
1555 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1557 struct {
1558 const char *map;
1559 const char *context;
1560 const char *gist;
1561 } plain_gist_tests[] = {
1562 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1563 "{ [i] -> [j] : i >= 1 }",
1564 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1565 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1566 "(j mod 4 = 2 and k mod 6 = n) }",
1567 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1568 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1569 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1570 "{ [i] -> [j] : i > j }",
1571 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1574 /* Basic tests for isl_map_plain_gist_basic_map.
1576 static int test_plain_gist(isl_ctx *ctx)
1578 int i;
1580 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1581 const char *str;
1582 int equal;
1583 isl_map *map, *gist;
1584 isl_basic_map *context;
1586 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1587 str = plain_gist_tests[i].context;
1588 context = isl_basic_map_read_from_str(ctx, str);
1589 map = isl_map_plain_gist_basic_map(map, context);
1590 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1591 equal = isl_map_is_equal(map, gist);
1592 isl_map_free(map);
1593 isl_map_free(gist);
1594 if (equal < 0)
1595 return -1;
1596 if (!equal)
1597 isl_die(ctx, isl_error_unknown,
1598 "incorrect gist result", return -1);
1601 return 0;
1604 /* Inputs for isl_basic_set_gist tests that are expected to fail.
1606 struct {
1607 const char *set;
1608 const char *context;
1609 } gist_fail_tests[] = {
1610 { "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1611 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }",
1612 "{ [i] : i >= 0 }" },
1615 /* Check that isl_basic_set_gist fails (gracefully) when expected.
1616 * In particular, the user should be able to recover from the failure.
1618 static isl_stat test_gist_fail(struct isl_ctx *ctx)
1620 int i, n;
1621 int on_error;
1623 on_error = isl_options_get_on_error(ctx);
1624 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
1625 n = ARRAY_SIZE(gist_fail_tests);
1626 for (i = 0; i < n; ++i) {
1627 const char *str;
1628 isl_basic_set *bset, *context;
1630 bset = isl_basic_set_read_from_str(ctx, gist_fail_tests[i].set);
1631 str = gist_fail_tests[i].context;
1632 context = isl_basic_set_read_from_str(ctx, str);
1633 bset = isl_basic_set_gist(bset, context);
1634 isl_basic_set_free(bset);
1635 if (bset)
1636 break;
1638 isl_options_set_on_error(ctx, on_error);
1639 if (i < n)
1640 isl_die(ctx, isl_error_unknown,
1641 "operation not expected to succeed",
1642 return isl_stat_error);
1644 return isl_stat_ok;
1647 struct {
1648 const char *set;
1649 const char *context;
1650 const char *gist;
1651 } gist_tests[] = {
1652 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1653 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1654 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1655 "{ [a, b, c] : a <= 15 }" },
1656 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1657 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1658 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1659 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1660 { "{ [m, n, a, b] : a <= 2147 + n }",
1661 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1662 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1663 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1664 "b >= 0) }",
1665 "{ [m, n, ku, kl] }" },
1666 { "{ [a, a, b] : a >= 10 }",
1667 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1668 "{ [a, a, b] : a >= 10 }" },
1669 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1670 "{ [0, j] : j >= 0 }" },
1671 /* Check that no constraints on i6 are introduced in the gist */
1672 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1673 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1674 "5e0 <= 381 - t1 and i4 <= 1) }",
1675 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1676 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1677 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1678 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1679 "20e0 >= 1511 - 4t1 - 5i4) }" },
1680 /* Check that no constraints on i6 are introduced in the gist */
1681 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1682 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1683 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1684 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1685 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1686 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1687 "20e1 <= 1530 - 4t1 - 5i4 and "
1688 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1689 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1690 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1691 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1692 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1693 "10e0 <= -91 + 5i4 + 4i6 and "
1694 "10e0 >= -105 + 5i4 + 4i6) }",
1695 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1696 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1697 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1698 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1699 "{ [a, b, q, p] : b >= 1 + a }",
1700 "{ [a, b, q, p] : false }" },
1701 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1702 "[n] -> { [x] : x mod 32 = 0 }",
1703 "[n] -> { [x = n] }" },
1704 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1705 "{ [x] : x mod 2 = 0 }" },
1706 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1707 "{ [x] : x mod 128 = 0 }" },
1708 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1709 "{ [x] : x mod 3200 = 0 }" },
1710 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1711 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1712 "{ [a, b, c = a] }" },
1713 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1714 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1715 "{ [a, b, c = a] : a mod 3 = 0 }" },
1716 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1717 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1718 "{ [x] }" },
1719 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1720 "{ [x,y] : 1 <= y <= 3 }",
1721 "{ [x,y] }" },
1724 /* Check that isl_set_gist behaves as expected.
1726 * For the test cases in gist_tests, besides checking that the result
1727 * is as expected, also check that applying the gist operation does
1728 * not modify the input set (an earlier version of isl would do that) and
1729 * that the test case is consistent, i.e., that the gist has the same
1730 * intersection with the context as the input set.
1732 static int test_gist(struct isl_ctx *ctx)
1734 int i;
1735 const char *str;
1736 isl_basic_set *bset1, *bset2;
1737 isl_map *map1, *map2;
1738 isl_bool equal;
1739 isl_size n_div;
1741 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1742 isl_bool equal_input, equal_intersection;
1743 isl_set *set1, *set2, *copy, *context;
1745 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1746 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1747 copy = isl_set_copy(set1);
1748 set1 = isl_set_gist(set1, isl_set_copy(context));
1749 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1750 equal = isl_set_is_equal(set1, set2);
1751 isl_set_free(set1);
1752 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1753 equal_input = isl_set_is_equal(set1, copy);
1754 isl_set_free(copy);
1755 set1 = isl_set_intersect(set1, isl_set_copy(context));
1756 set2 = isl_set_intersect(set2, context);
1757 equal_intersection = isl_set_is_equal(set1, set2);
1758 isl_set_free(set2);
1759 isl_set_free(set1);
1760 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1761 return -1;
1762 if (!equal)
1763 isl_die(ctx, isl_error_unknown,
1764 "incorrect gist result", return -1);
1765 if (!equal_input)
1766 isl_die(ctx, isl_error_unknown,
1767 "gist modified input", return -1);
1768 if (!equal_input)
1769 isl_die(ctx, isl_error_unknown,
1770 "inconsistent gist test case", return -1);
1773 if (test_gist_fail(ctx) < 0)
1774 return -1;
1776 test_gist_case(ctx, "gist1");
1778 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1779 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1780 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1781 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1782 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1783 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1784 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1785 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1786 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1787 bset1 = isl_basic_set_read_from_str(ctx, str);
1788 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1789 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1790 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1791 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1792 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1793 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1794 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1795 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1796 bset2 = isl_basic_set_read_from_str(ctx, str);
1797 bset1 = isl_basic_set_gist(bset1, bset2);
1798 assert(bset1 && bset1->n_div == 0);
1799 isl_basic_set_free(bset1);
1801 /* Check that the integer divisions of the second disjunct
1802 * do not spread to the first disjunct.
1804 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1805 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1806 "(exists (e0 = [(-1 + t1)/16], "
1807 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1808 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1809 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1810 "o0 <= 4294967295 and t1 <= -1)) }";
1811 map1 = isl_map_read_from_str(ctx, str);
1812 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1813 map2 = isl_map_read_from_str(ctx, str);
1814 map1 = isl_map_gist(map1, map2);
1815 if (!map1)
1816 return -1;
1817 if (map1->n != 1)
1818 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1819 isl_map_free(map1); return -1);
1820 n_div = isl_basic_map_dim(map1->p[0], isl_dim_div);
1821 isl_map_free(map1);
1822 if (n_div < 0)
1823 return -1;
1824 if (n_div != 1)
1825 isl_die(ctx, isl_error_unknown, "expecting single div",
1826 return -1);
1828 if (test_plain_gist(ctx) < 0)
1829 return -1;
1831 return 0;
1834 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1836 isl_set *set, *set2;
1837 int equal;
1838 int one;
1840 set = isl_set_read_from_str(ctx, str);
1841 set = isl_set_coalesce(set);
1842 set2 = isl_set_read_from_str(ctx, str);
1843 equal = isl_set_is_equal(set, set2);
1844 one = set && set->n == 1;
1845 isl_set_free(set);
1846 isl_set_free(set2);
1848 if (equal < 0)
1849 return -1;
1850 if (!equal)
1851 isl_die(ctx, isl_error_unknown,
1852 "coalesced set not equal to input", return -1);
1853 if (check_one && !one)
1854 isl_die(ctx, isl_error_unknown,
1855 "coalesced set should not be a union", return -1);
1857 return 0;
1860 /* Inputs for coalescing tests with unbounded wrapping.
1861 * "str" is a string representation of the input set.
1862 * "single_disjunct" is set if we expect the result to consist of
1863 * a single disjunct.
1865 struct {
1866 int single_disjunct;
1867 const char *str;
1868 } coalesce_unbounded_tests[] = {
1869 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1870 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1871 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1872 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1873 "-10 <= y <= 0}" },
1874 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1875 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1876 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1877 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1880 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1881 * option turned off.
1883 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1885 int i;
1886 int r = 0;
1887 int bounded;
1889 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1890 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1892 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1893 const char *str = coalesce_unbounded_tests[i].str;
1894 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1895 if (test_coalesce_set(ctx, str, check_one) >= 0)
1896 continue;
1897 r = -1;
1898 break;
1901 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1903 return r;
1906 /* Inputs for coalescing tests.
1907 * "str" is a string representation of the input set.
1908 * "single_disjunct" is set if we expect the result to consist of
1909 * a single disjunct.
1911 struct {
1912 int single_disjunct;
1913 const char *str;
1914 } coalesce_tests[] = {
1915 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1916 "y >= x & x >= 2 & 5 >= y }" },
1917 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1918 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1919 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1920 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1921 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1922 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1923 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1924 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1925 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1926 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1927 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1928 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1929 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1930 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1931 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1932 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1933 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1934 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1935 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1936 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1937 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1938 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1939 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1940 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1941 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1942 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1943 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1944 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1945 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1946 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1947 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1948 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1949 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1950 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1951 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1952 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1953 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1954 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1955 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1956 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1957 "[o0, o1, o2, o3, o4, o5, o6]] : "
1958 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1959 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1960 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1961 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1962 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1963 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1964 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1965 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1966 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1967 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1968 "o6 >= i3 + i6 - o3 and M >= 0 and "
1969 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1970 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1971 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1972 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1973 "(o0 = 0 and M >= 2 and N >= 3) or "
1974 "(M = 0 and o0 = 0 and N >= 3) }" },
1975 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1976 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1977 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1978 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1979 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1980 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1981 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1982 "(y = 3 and x = 1) }" },
1983 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1984 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1985 "i1 <= M and i3 <= M and i4 <= M) or "
1986 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1987 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1988 "i4 <= -1 + M) }" },
1989 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1990 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1991 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1992 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1993 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1994 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1995 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1996 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1997 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1998 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1999 { 0, "{ [a, b] : exists e : 2e = a and "
2000 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
2001 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
2002 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
2003 "j >= 1 and j' <= i + j - i' and i >= 1; "
2004 "[1, 1, 1, 1] }" },
2005 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
2006 "[i,j] : exists a : j = 3a }" },
2007 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
2008 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
2009 "a >= 3) or "
2010 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
2011 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
2012 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
2013 "c <= 6 + 8a and a >= 3; "
2014 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
2015 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
2016 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2017 "[x,0] : 3 <= x <= 4 }" },
2018 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
2019 "[x,0] : 4 <= x <= 5 }" },
2020 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2021 "[x,0] : 3 <= x <= 5 }" },
2022 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
2023 "[x,0] : 3 <= x <= 4 }" },
2024 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
2025 "i1 <= 0; "
2026 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
2027 { 1, "{ [0,0]; [1,1] }" },
2028 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
2029 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
2030 "ii <= k;"
2031 "[k, 0, k] : k <= 6 and k >= 1 }" },
2032 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
2033 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
2034 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
2035 { 1, "[n] -> { [1] : n >= 0;"
2036 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
2037 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
2038 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
2039 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
2040 "2e0 <= x and 2e0 <= n);"
2041 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
2042 "n >= 0) }" },
2043 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
2044 "128e0 >= -134 + 127t1 and t1 >= 2 and "
2045 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
2046 "t1 = 1 }" },
2047 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
2048 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
2049 "[0, 0] }" },
2050 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
2051 "t1 >= 13 and t1 <= 16);"
2052 "[t1] : t1 <= 15 and t1 >= 12 }" },
2053 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
2054 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
2055 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
2056 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
2057 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
2058 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
2059 "i <= 4j + 2 }" },
2060 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
2061 "(exists (e0 : 3e0 = -2 + c0)) }" },
2062 { 0, "[n, b0, t0] -> "
2063 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2064 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2065 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2066 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2067 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
2068 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
2069 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
2070 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
2071 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2072 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2073 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2074 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
2075 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
2076 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
2077 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
2078 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
2079 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
2080 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
2081 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
2082 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
2083 { 0, "{ [i0, i1, i2] : "
2084 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
2085 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
2086 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
2087 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
2088 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
2089 "32e0 <= 31 + i0)) or "
2090 "i0 >= 0 }" },
2091 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
2092 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
2093 "2*floor((c)/2) = c and 0 <= a <= 192;"
2094 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
2096 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
2097 "(0 <= a <= b <= n) }" },
2098 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
2099 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
2100 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
2101 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2102 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2103 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2104 "b = 3 and 9e0 <= -19 + 2c)) }" },
2105 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2106 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2107 "(a = 4 and b = 3 and "
2108 "9*floor((-16 + 2c)/9) <= -19 + 2c) }" },
2109 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
2110 "(b = -1 + a and 0 < a <= 3 and "
2111 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2112 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2113 "b = 3 and 9e0 <= -19 + 2c)) }" },
2114 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2115 "[1, 0] }" },
2116 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2117 "[0, 1] }" },
2118 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2119 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2120 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
2121 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
2122 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
2123 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
2124 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
2125 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
2126 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
2127 { 1, "{ [a] : a <= 8 and "
2128 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
2129 { 1, "{ [x, y] : 2y = -x and x <= 0 or "
2130 "x <= -1 and 2y <= -x - 1 and 2y >= x - 1 }" },
2131 { 0, "{ [x, y] : 2y = -x and x <= 0 or "
2132 "x <= -2 and 2y <= -x - 1 and 2y >= x - 1 }" },
2133 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2134 "(a < 0 and 3*floor((a)/3) < a) }" },
2135 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2136 "(a < -1 and 3*floor((a)/3) < a) }" },
2137 { 1, "{ [a, b] : a <= 1024 and b >= 0 and "
2138 "((-31 - a + b <= 32*floor((-1 - a)/32) <= -33 + b and "
2139 "32*floor((-1 - a)/32) <= -16 + b + 16*floor((-1 - a)/16))"
2140 "or (2 <= a <= 15 and b < a)) }" },
2141 { 1, "{ [a] : a > 0 and ((16*floor((a)/16) < a and "
2142 "32*floor((a)/32) < a) or a <= 15) }" },
2143 { 1, "{ [a, b, c, d] : (-a + d) mod 64 = 0 and a <= 8 and b <= 1 and "
2144 "10 - a <= c <= 3 and d >= 5 and 9 - 64b <= d <= 70;"
2145 "[a, b = 1, c, d] : (-a + d) mod 64 = 0 and a <= 8 and c >= 4 and "
2146 "10 - a <= c <= 5 and 5 <= d <= 73 - c }" },
2147 { 1, "[n, m] -> { S_0[i] : (-n + i) mod 3 = 0 and m >= 3 + n and "
2148 "i >= n and 3*floor((2 + n + 2m)/3) <= n + 3m - i; "
2149 "S_0[n] : n <= m <= 2 + n }" },
2150 { 1, "{ [a, b] : exists (e0: 0 <= a <= 1 and b >= 0 and "
2151 "2e0 >= -5 + a + 2b and 2e0 >= -1 + a + b and "
2152 "2e0 <= a + b); "
2153 "[a, b] : exists (e0: 0 <= a <= 1 and 2e0 >= -5 + a + 2b and "
2154 "2e0 >= -1 - a + b and 2e0 <= -a + b and "
2155 "2e0 < -a + 2b) }" },
2156 { 1, "{ [i, j, i - 8j] : 8 <= i <= 63 and -7 + i <= 8j <= i; "
2157 "[i, 0, i] : 0 <= i <= 7 }" },
2158 { 1, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [1, 1] }" },
2159 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [0, 2] }" },
2160 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [-1, 3] }" },
2161 { 1, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [1, 1] }" },
2162 { 0, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [2, 1] }" },
2163 { 0, "{ [a, c] : (2 + a) mod 4 = 0 or "
2164 "(c = 4 + a and 4 * floor((a)/4) = a and a >= 0 and a <= 4) or "
2165 "(c = 3 + a and 4 * floor((-1 + a)/4) = -1 + a and "
2166 "a > 0 and a <= 5) }" },
2167 { 1, "{ [1, 0, 0]; [a, b, c] : -1 <= -a < b <= 0 and 2c > b }" },
2170 /* A specialized coalescing test case that would result
2171 * in a segmentation fault or a failed assertion in earlier versions of isl.
2173 static int test_coalesce_special(struct isl_ctx *ctx)
2175 const char *str;
2176 isl_map *map1, *map2;
2178 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2179 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
2180 "(y = 201 and o1 <= 239 and o1 >= 212) or "
2181 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
2182 "o1 <= 239 and o1 >= 212)) or "
2183 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
2184 "o1 <= 241 and o1 >= 240));"
2185 "[S_L220_OUT[] -> T7[]] -> "
2186 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
2187 "(y = 2 and o1 <= 241 and o1 >= 212) or "
2188 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
2189 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
2190 map1 = isl_map_read_from_str(ctx, str);
2191 map1 = isl_map_align_divs_internal(map1);
2192 map1 = isl_map_coalesce(map1);
2193 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2194 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
2195 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
2196 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
2197 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
2198 map2 = isl_map_read_from_str(ctx, str);
2199 map2 = isl_map_union(map2, map1);
2200 map2 = isl_map_align_divs_internal(map2);
2201 map2 = isl_map_coalesce(map2);
2202 isl_map_free(map2);
2203 if (!map2)
2204 return -1;
2206 return 0;
2209 /* A specialized coalescing test case that would result in an assertion
2210 * in an earlier version of isl.
2211 * The explicit call to isl_basic_set_union prevents the implicit
2212 * equality constraints in the first basic map from being detected prior
2213 * to the call to isl_set_coalesce, at least at the point
2214 * where this test case was introduced.
2216 static int test_coalesce_special2(struct isl_ctx *ctx)
2218 const char *str;
2219 isl_basic_set *bset1, *bset2;
2220 isl_set *set;
2222 str = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
2223 bset1 = isl_basic_set_read_from_str(ctx, str);
2224 str = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }" ;
2225 bset2 = isl_basic_set_read_from_str(ctx, str);
2226 set = isl_basic_set_union(bset1, bset2);
2227 set = isl_set_coalesce(set);
2228 isl_set_free(set);
2230 if (!set)
2231 return -1;
2232 return 0;
2235 /* Check that calling isl_set_coalesce does not leave other sets
2236 * that may share some information with the input to isl_set_coalesce
2237 * in an inconsistent state.
2238 * In particular, older versions of isl would modify all copies
2239 * of the basic sets in the isl_set_coalesce input in a way
2240 * that could leave them in an inconsistent state.
2241 * The result of printing any other set containing one of these
2242 * basic sets would then result in an invalid set description.
2244 static int test_coalesce_special3(isl_ctx *ctx)
2246 const char *str;
2247 char *s;
2248 isl_set *set1, *set2;
2249 isl_printer *p;
2251 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
2252 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
2253 set2 = isl_set_read_from_str(ctx, str);
2254 set1 = isl_set_union(set1, isl_set_copy(set2));
2255 set1 = isl_set_coalesce(set1);
2256 isl_set_free(set1);
2258 p = isl_printer_to_str(ctx);
2259 p = isl_printer_print_set(p, set2);
2260 isl_set_free(set2);
2261 s = isl_printer_get_str(p);
2262 isl_printer_free(p);
2263 set1 = isl_set_read_from_str(ctx, s);
2264 free(s);
2265 isl_set_free(set1);
2267 if (!set1)
2268 return -1;
2270 return 0;
2273 /* Check that calling isl_set_coalesce does not leave other sets
2274 * that may share some information with the input to isl_set_coalesce
2275 * in an inconsistent state.
2276 * In particular, when isl_set_coalesce detects equality constraints,
2277 * it does not immediately perform Gaussian elimination on them,
2278 * but then it needs to ensure that it is performed at some point.
2279 * The input set has implicit equality constraints in the first disjunct.
2280 * It is constructed as an intersection, because otherwise
2281 * those equality constraints would already be detected during parsing.
2283 static isl_stat test_coalesce_special4(isl_ctx *ctx)
2285 isl_set *set1, *set2;
2287 set1 = isl_set_read_from_str(ctx, "{ [a, b] : b <= 0 or a <= 1 }");
2288 set2 = isl_set_read_from_str(ctx, "{ [a, b] : -1 <= -a < b }");
2289 set1 = isl_set_intersect(set1, set2);
2290 isl_set_free(isl_set_coalesce(isl_set_copy(set1)));
2291 set1 = isl_set_coalesce(set1);
2292 isl_set_free(set1);
2294 if (!set1)
2295 return isl_stat_error;
2297 return isl_stat_ok;
2300 /* Test the functionality of isl_set_coalesce.
2301 * That is, check that the output is always equal to the input
2302 * and in some cases that the result consists of a single disjunct.
2304 static int test_coalesce(struct isl_ctx *ctx)
2306 int i;
2308 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2309 const char *str = coalesce_tests[i].str;
2310 int check_one = coalesce_tests[i].single_disjunct;
2311 if (test_coalesce_set(ctx, str, check_one) < 0)
2312 return -1;
2315 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2316 return -1;
2317 if (test_coalesce_special(ctx) < 0)
2318 return -1;
2319 if (test_coalesce_special2(ctx) < 0)
2320 return -1;
2321 if (test_coalesce_special3(ctx) < 0)
2322 return -1;
2323 if (test_coalesce_special4(ctx) < 0)
2324 return -1;
2326 return 0;
2329 /* Construct a representation of the graph on the right of Figure 1
2330 * in "Computing the Transitive Closure of a Union of
2331 * Affine Integer Tuple Relations".
2333 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2335 isl_set *dom;
2336 isl_map *up, *right;
2338 dom = isl_set_read_from_str(ctx,
2339 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2340 "2 x - 3 y + 3 >= 0 }");
2341 right = isl_map_read_from_str(ctx,
2342 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2343 up = isl_map_read_from_str(ctx,
2344 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2345 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2346 right = isl_map_intersect_range(right, isl_set_copy(dom));
2347 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2348 up = isl_map_intersect_range(up, dom);
2349 return isl_map_union(up, right);
2352 /* Construct a representation of the power of the graph
2353 * on the right of Figure 1 in "Computing the Transitive Closure of
2354 * a Union of Affine Integer Tuple Relations".
2356 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2358 return isl_map_read_from_str(ctx,
2359 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2360 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2361 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2364 /* Construct a representation of the transitive closure of the graph
2365 * on the right of Figure 1 in "Computing the Transitive Closure of
2366 * a Union of Affine Integer Tuple Relations".
2368 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2370 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2373 static int test_closure(isl_ctx *ctx)
2375 const char *str;
2376 isl_map *map, *map2;
2377 int exact, equal;
2379 /* COCOA example 1 */
2380 map = isl_map_read_from_str(ctx,
2381 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2382 "1 <= i and i < n and 1 <= j and j < n or "
2383 "i2 = i + 1 and j2 = j - 1 and "
2384 "1 <= i and i < n and 2 <= j and j <= n }");
2385 map = isl_map_power(map, &exact);
2386 assert(exact);
2387 isl_map_free(map);
2389 /* COCOA example 1 */
2390 map = isl_map_read_from_str(ctx,
2391 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2392 "1 <= i and i < n and 1 <= j and j < n or "
2393 "i2 = i + 1 and j2 = j - 1 and "
2394 "1 <= i and i < n and 2 <= j and j <= n }");
2395 map = isl_map_transitive_closure(map, &exact);
2396 assert(exact);
2397 map2 = isl_map_read_from_str(ctx,
2398 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2399 "1 <= i and i < n and 1 <= j and j <= n and "
2400 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2401 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2402 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2403 assert(isl_map_is_equal(map, map2));
2404 isl_map_free(map2);
2405 isl_map_free(map);
2407 map = isl_map_read_from_str(ctx,
2408 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2409 " 0 <= y and y <= n }");
2410 map = isl_map_transitive_closure(map, &exact);
2411 map2 = isl_map_read_from_str(ctx,
2412 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2413 " 0 <= y and y <= n }");
2414 assert(isl_map_is_equal(map, map2));
2415 isl_map_free(map2);
2416 isl_map_free(map);
2418 /* COCOA example 2 */
2419 map = isl_map_read_from_str(ctx,
2420 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2421 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2422 "i2 = i + 2 and j2 = j - 2 and "
2423 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2424 map = isl_map_transitive_closure(map, &exact);
2425 assert(exact);
2426 map2 = isl_map_read_from_str(ctx,
2427 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2428 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2429 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2430 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2431 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2432 assert(isl_map_is_equal(map, map2));
2433 isl_map_free(map);
2434 isl_map_free(map2);
2436 /* COCOA Fig.2 left */
2437 map = isl_map_read_from_str(ctx,
2438 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2439 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2440 "j <= n or "
2441 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2442 "j <= 2 i - 3 and j <= n - 2 or "
2443 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2444 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2445 map = isl_map_transitive_closure(map, &exact);
2446 assert(exact);
2447 isl_map_free(map);
2449 /* COCOA Fig.2 right */
2450 map = isl_map_read_from_str(ctx,
2451 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2452 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2453 "j <= n or "
2454 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2455 "j <= 2 i - 4 and j <= n - 3 or "
2456 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2457 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2458 map = isl_map_power(map, &exact);
2459 assert(exact);
2460 isl_map_free(map);
2462 /* COCOA Fig.2 right */
2463 map = isl_map_read_from_str(ctx,
2464 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2465 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2466 "j <= n or "
2467 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2468 "j <= 2 i - 4 and j <= n - 3 or "
2469 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2470 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2471 map = isl_map_transitive_closure(map, &exact);
2472 assert(exact);
2473 map2 = isl_map_read_from_str(ctx,
2474 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2475 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2476 "j <= n and 3 + i + 2 j <= 3 n and "
2477 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2478 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2479 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2480 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2481 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2482 assert(isl_map_is_equal(map, map2));
2483 isl_map_free(map2);
2484 isl_map_free(map);
2486 map = cocoa_fig_1_right_graph(ctx);
2487 map = isl_map_transitive_closure(map, &exact);
2488 assert(exact);
2489 map2 = cocoa_fig_1_right_tc(ctx);
2490 assert(isl_map_is_equal(map, map2));
2491 isl_map_free(map2);
2492 isl_map_free(map);
2494 map = cocoa_fig_1_right_graph(ctx);
2495 map = isl_map_power(map, &exact);
2496 map2 = cocoa_fig_1_right_power(ctx);
2497 equal = isl_map_is_equal(map, map2);
2498 isl_map_free(map2);
2499 isl_map_free(map);
2500 if (equal < 0)
2501 return -1;
2502 if (!exact)
2503 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2504 if (!equal)
2505 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2507 /* COCOA Theorem 1 counter example */
2508 map = isl_map_read_from_str(ctx,
2509 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2510 "i2 = 1 and j2 = j or "
2511 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2512 map = isl_map_transitive_closure(map, &exact);
2513 assert(exact);
2514 isl_map_free(map);
2516 map = isl_map_read_from_str(ctx,
2517 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2518 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2519 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2520 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2521 map = isl_map_transitive_closure(map, &exact);
2522 assert(exact);
2523 isl_map_free(map);
2525 /* Kelly et al 1996, fig 12 */
2526 map = isl_map_read_from_str(ctx,
2527 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2528 "1 <= i,j,j+1 <= n or "
2529 "j = n and j2 = 1 and i2 = i + 1 and "
2530 "1 <= i,i+1 <= n }");
2531 map = isl_map_transitive_closure(map, &exact);
2532 assert(exact);
2533 map2 = isl_map_read_from_str(ctx,
2534 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2535 "1 <= i <= n and i = i2 or "
2536 "1 <= i < i2 <= n and 1 <= j <= n and "
2537 "1 <= j2 <= n }");
2538 assert(isl_map_is_equal(map, map2));
2539 isl_map_free(map2);
2540 isl_map_free(map);
2542 /* Omega's closure4 */
2543 map = isl_map_read_from_str(ctx,
2544 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2545 "1 <= x,y <= 10 or "
2546 "x2 = x + 1 and y2 = y and "
2547 "1 <= x <= 20 && 5 <= y <= 15 }");
2548 map = isl_map_transitive_closure(map, &exact);
2549 assert(exact);
2550 isl_map_free(map);
2552 map = isl_map_read_from_str(ctx,
2553 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2554 map = isl_map_transitive_closure(map, &exact);
2555 assert(!exact);
2556 map2 = isl_map_read_from_str(ctx,
2557 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2558 assert(isl_map_is_equal(map, map2));
2559 isl_map_free(map);
2560 isl_map_free(map2);
2562 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2563 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2564 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2565 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2566 map = isl_map_read_from_str(ctx, str);
2567 map = isl_map_transitive_closure(map, &exact);
2568 assert(exact);
2569 map2 = isl_map_read_from_str(ctx, str);
2570 assert(isl_map_is_equal(map, map2));
2571 isl_map_free(map);
2572 isl_map_free(map2);
2574 str = "{[0] -> [1]; [2] -> [3]}";
2575 map = isl_map_read_from_str(ctx, str);
2576 map = isl_map_transitive_closure(map, &exact);
2577 assert(exact);
2578 map2 = isl_map_read_from_str(ctx, str);
2579 assert(isl_map_is_equal(map, map2));
2580 isl_map_free(map);
2581 isl_map_free(map2);
2583 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2584 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2585 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2586 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2587 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2588 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2589 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2590 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2591 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2592 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2593 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2594 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2595 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2596 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2597 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2598 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2599 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2600 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2601 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2602 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2603 map = isl_map_read_from_str(ctx, str);
2604 map = isl_map_transitive_closure(map, NULL);
2605 assert(map);
2606 isl_map_free(map);
2608 return 0;
2611 /* Check that the actual result of a boolean operation is equal
2612 * to the expected result.
2614 static isl_stat check_bool(isl_ctx *ctx, isl_bool actual, isl_bool expected)
2616 if (actual != expected)
2617 isl_die(ctx, isl_error_unknown,
2618 "incorrect boolean operation", return isl_stat_error);
2619 return isl_stat_ok;
2622 /* Test operations on isl_bool values.
2624 * This tests:
2626 * isl_bool_not
2627 * isl_bool_ok
2629 static int test_isl_bool(isl_ctx *ctx)
2631 if (check_bool(ctx, isl_bool_not(isl_bool_true), isl_bool_false) < 0)
2632 return -1;
2633 if (check_bool(ctx, isl_bool_not(isl_bool_false), isl_bool_true) < 0)
2634 return -1;
2635 if (check_bool(ctx, isl_bool_not(isl_bool_error), isl_bool_error) < 0)
2636 return -1;
2637 if (check_bool(ctx, isl_bool_ok(0), isl_bool_false) < 0)
2638 return -1;
2639 if (check_bool(ctx, isl_bool_ok(1), isl_bool_true) < 0)
2640 return -1;
2641 if (check_bool(ctx, isl_bool_ok(-1), isl_bool_true) < 0)
2642 return -1;
2643 if (check_bool(ctx, isl_bool_ok(2), isl_bool_true) < 0)
2644 return -1;
2645 if (check_bool(ctx, isl_bool_ok(-2), isl_bool_true) < 0)
2646 return -1;
2648 return 0;
2651 static int test_lex(struct isl_ctx *ctx)
2653 isl_space *dim;
2654 isl_map *map;
2655 int empty;
2657 dim = isl_space_set_alloc(ctx, 0, 0);
2658 map = isl_map_lex_le(dim);
2659 empty = isl_map_is_empty(map);
2660 isl_map_free(map);
2662 if (empty < 0)
2663 return -1;
2664 if (empty)
2665 isl_die(ctx, isl_error_unknown,
2666 "expecting non-empty result", return -1);
2668 return 0;
2671 /* Inputs for isl_map_lexmin tests.
2672 * "map" is the input and "lexmin" is the expected result.
2674 struct {
2675 const char *map;
2676 const char *lexmin;
2677 } lexmin_tests [] = {
2678 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2679 "{ [x] -> [5] : 6 <= x <= 8; "
2680 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2681 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2682 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2683 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2684 "{ [x] -> [y] : (4y = x and x >= 0) or "
2685 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2686 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2687 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2688 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2689 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2690 /* Check that empty pieces are properly combined. */
2691 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2692 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2693 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2694 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2695 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2696 "x >= 4 }" },
2697 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2698 "a <= 255 and c <= 255 and d <= 255 - j and "
2699 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2700 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2701 "247d <= 247 + i and 248 - b <= 248d <= c and "
2702 "254d >= i - a + b and 254d >= -a + b and "
2703 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2704 "{ [i, k, j] -> "
2705 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2706 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2707 "247j >= 62738 - i and 509j <= 129795 + i and "
2708 "742j >= 188724 - i; "
2709 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2710 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2711 "16*floor((8 + b)/16) <= 7 + b; "
2712 "[a] -> [1] }",
2713 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2714 "[a] -> [b = 0] : 0 < a <= 509 }" },
2715 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2716 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2719 static int test_lexmin(struct isl_ctx *ctx)
2721 int i;
2722 int equal;
2723 const char *str;
2724 isl_basic_map *bmap;
2725 isl_map *map, *map2;
2726 isl_set *set;
2727 isl_set *set2;
2728 isl_pw_multi_aff *pma;
2730 str = "[p0, p1] -> { [] -> [] : "
2731 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2732 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2733 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2734 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2735 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2736 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2737 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2738 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2739 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2740 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2741 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2742 map = isl_map_read_from_str(ctx, str);
2743 map = isl_map_lexmin(map);
2744 isl_map_free(map);
2746 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2747 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2748 set = isl_set_read_from_str(ctx, str);
2749 set = isl_set_lexmax(set);
2750 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2751 set2 = isl_set_read_from_str(ctx, str);
2752 set = isl_set_intersect(set, set2);
2753 assert(!isl_set_is_empty(set));
2754 isl_set_free(set);
2756 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2757 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2758 map = isl_map_lexmin(map);
2759 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2760 equal = isl_map_is_equal(map, map2);
2761 isl_map_free(map);
2762 isl_map_free(map2);
2764 if (equal < 0)
2765 return -1;
2766 if (!equal)
2767 isl_die(ctx, isl_error_unknown,
2768 "unexpected result", return -1);
2771 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2772 " 8i' <= i and 8i' >= -7 + i }";
2773 bmap = isl_basic_map_read_from_str(ctx, str);
2774 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2775 map2 = isl_map_from_pw_multi_aff(pma);
2776 map = isl_map_from_basic_map(bmap);
2777 assert(isl_map_is_equal(map, map2));
2778 isl_map_free(map);
2779 isl_map_free(map2);
2781 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2782 " 8i' <= i and 8i' >= -7 + i }";
2783 set = isl_set_read_from_str(ctx, str);
2784 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2785 set2 = isl_set_from_pw_multi_aff(pma);
2786 equal = isl_set_is_equal(set, set2);
2787 isl_set_free(set);
2788 isl_set_free(set2);
2789 if (equal < 0)
2790 return -1;
2791 if (!equal)
2792 isl_die(ctx, isl_error_unknown,
2793 "unexpected difference between set and "
2794 "piecewise affine expression", return -1);
2796 return 0;
2799 /* A specialized isl_set_min_val test case that would return the wrong result
2800 * in earlier versions of isl.
2801 * The explicit call to isl_basic_set_union prevents the second basic set
2802 * from being determined to be empty prior to the call to isl_set_min_val,
2803 * at least at the point where this test case was introduced.
2805 static int test_min_special(isl_ctx *ctx)
2807 const char *str;
2808 isl_basic_set *bset1, *bset2;
2809 isl_set *set;
2810 isl_aff *obj;
2811 isl_val *res;
2812 int ok;
2814 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2815 bset1 = isl_basic_set_read_from_str(ctx, str);
2816 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2817 bset2 = isl_basic_set_read_from_str(ctx, str);
2818 set = isl_basic_set_union(bset1, bset2);
2819 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2821 res = isl_set_min_val(set, obj);
2822 ok = isl_val_cmp_si(res, 5) == 0;
2824 isl_aff_free(obj);
2825 isl_set_free(set);
2826 isl_val_free(res);
2828 if (!res)
2829 return -1;
2830 if (!ok)
2831 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2832 return -1);
2834 return 0;
2837 /* A specialized isl_set_min_val test case that would return an error
2838 * in earlier versions of isl.
2840 static int test_min_special2(isl_ctx *ctx)
2842 const char *str;
2843 isl_basic_set *bset;
2844 isl_aff *obj;
2845 isl_val *res;
2847 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
2848 bset = isl_basic_set_read_from_str(ctx, str);
2850 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
2852 res = isl_basic_set_max_val(bset, obj);
2854 isl_basic_set_free(bset);
2855 isl_aff_free(obj);
2856 isl_val_free(res);
2858 if (!res)
2859 return -1;
2861 return 0;
2864 struct {
2865 const char *set;
2866 const char *obj;
2867 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2868 __isl_keep isl_aff *obj);
2869 const char *res;
2870 } opt_tests[] = {
2871 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2872 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2873 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2874 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2875 &isl_set_max_val, "30" },
2879 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2880 * In particular, check the results on non-convex inputs.
2882 static int test_min(struct isl_ctx *ctx)
2884 int i;
2885 isl_set *set;
2886 isl_aff *obj;
2887 isl_val *val, *res;
2888 isl_bool ok;
2890 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2891 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2892 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2893 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2894 val = opt_tests[i].fn(set, obj);
2895 ok = isl_val_eq(res, val);
2896 isl_val_free(res);
2897 isl_val_free(val);
2898 isl_aff_free(obj);
2899 isl_set_free(set);
2901 if (ok < 0)
2902 return -1;
2903 if (!ok)
2904 isl_die(ctx, isl_error_unknown,
2905 "unexpected optimum", return -1);
2908 if (test_min_special(ctx) < 0)
2909 return -1;
2910 if (test_min_special2(ctx) < 0)
2911 return -1;
2913 return 0;
2916 struct must_may {
2917 isl_map *must;
2918 isl_map *may;
2921 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2922 void *dep_user, void *user)
2924 struct must_may *mm = (struct must_may *)user;
2926 if (must)
2927 mm->must = isl_map_union(mm->must, dep);
2928 else
2929 mm->may = isl_map_union(mm->may, dep);
2931 return isl_stat_ok;
2934 static int common_space(void *first, void *second)
2936 int depth = *(int *)first;
2937 return 2 * depth;
2940 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2942 isl_map *map2;
2943 int equal;
2945 if (!map)
2946 return -1;
2948 map2 = isl_map_read_from_str(map->ctx, str);
2949 equal = isl_map_is_equal(map, map2);
2950 isl_map_free(map2);
2952 return equal;
2955 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2957 int equal;
2959 equal = map_is_equal(map, str);
2960 if (equal < 0)
2961 return -1;
2962 if (!equal)
2963 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2964 "result not as expected", return -1);
2965 return 0;
2968 static int test_dep(struct isl_ctx *ctx)
2970 const char *str;
2971 isl_space *dim;
2972 isl_map *map;
2973 isl_access_info *ai;
2974 isl_flow *flow;
2975 int depth;
2976 struct must_may mm;
2978 depth = 3;
2980 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2981 map = isl_map_read_from_str(ctx, str);
2982 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2984 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2985 map = isl_map_read_from_str(ctx, str);
2986 ai = isl_access_info_add_source(ai, map, 1, &depth);
2988 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2989 map = isl_map_read_from_str(ctx, str);
2990 ai = isl_access_info_add_source(ai, map, 1, &depth);
2992 flow = isl_access_info_compute_flow(ai);
2993 dim = isl_space_alloc(ctx, 0, 3, 3);
2994 mm.must = isl_map_empty(isl_space_copy(dim));
2995 mm.may = isl_map_empty(dim);
2997 isl_flow_foreach(flow, collect_must_may, &mm);
2999 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
3000 " [1,10,0] -> [2,5,0] }";
3001 assert(map_is_equal(mm.must, str));
3002 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3003 assert(map_is_equal(mm.may, str));
3005 isl_map_free(mm.must);
3006 isl_map_free(mm.may);
3007 isl_flow_free(flow);
3010 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3011 map = isl_map_read_from_str(ctx, str);
3012 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3014 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3015 map = isl_map_read_from_str(ctx, str);
3016 ai = isl_access_info_add_source(ai, map, 1, &depth);
3018 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3019 map = isl_map_read_from_str(ctx, str);
3020 ai = isl_access_info_add_source(ai, map, 0, &depth);
3022 flow = isl_access_info_compute_flow(ai);
3023 dim = isl_space_alloc(ctx, 0, 3, 3);
3024 mm.must = isl_map_empty(isl_space_copy(dim));
3025 mm.may = isl_map_empty(dim);
3027 isl_flow_foreach(flow, collect_must_may, &mm);
3029 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
3030 assert(map_is_equal(mm.must, str));
3031 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3032 assert(map_is_equal(mm.may, str));
3034 isl_map_free(mm.must);
3035 isl_map_free(mm.may);
3036 isl_flow_free(flow);
3039 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3040 map = isl_map_read_from_str(ctx, str);
3041 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3043 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3044 map = isl_map_read_from_str(ctx, str);
3045 ai = isl_access_info_add_source(ai, map, 0, &depth);
3047 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3048 map = isl_map_read_from_str(ctx, str);
3049 ai = isl_access_info_add_source(ai, map, 0, &depth);
3051 flow = isl_access_info_compute_flow(ai);
3052 dim = isl_space_alloc(ctx, 0, 3, 3);
3053 mm.must = isl_map_empty(isl_space_copy(dim));
3054 mm.may = isl_map_empty(dim);
3056 isl_flow_foreach(flow, collect_must_may, &mm);
3058 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
3059 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3060 assert(map_is_equal(mm.may, str));
3061 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3062 assert(map_is_equal(mm.must, str));
3064 isl_map_free(mm.must);
3065 isl_map_free(mm.may);
3066 isl_flow_free(flow);
3069 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
3070 map = isl_map_read_from_str(ctx, str);
3071 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3073 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3074 map = isl_map_read_from_str(ctx, str);
3075 ai = isl_access_info_add_source(ai, map, 0, &depth);
3077 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
3078 map = isl_map_read_from_str(ctx, str);
3079 ai = isl_access_info_add_source(ai, map, 0, &depth);
3081 flow = isl_access_info_compute_flow(ai);
3082 dim = isl_space_alloc(ctx, 0, 3, 3);
3083 mm.must = isl_map_empty(isl_space_copy(dim));
3084 mm.may = isl_map_empty(dim);
3086 isl_flow_foreach(flow, collect_must_may, &mm);
3088 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
3089 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
3090 assert(map_is_equal(mm.may, str));
3091 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3092 assert(map_is_equal(mm.must, str));
3094 isl_map_free(mm.must);
3095 isl_map_free(mm.may);
3096 isl_flow_free(flow);
3099 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
3100 map = isl_map_read_from_str(ctx, str);
3101 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3103 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3104 map = isl_map_read_from_str(ctx, str);
3105 ai = isl_access_info_add_source(ai, map, 0, &depth);
3107 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
3108 map = isl_map_read_from_str(ctx, str);
3109 ai = isl_access_info_add_source(ai, map, 0, &depth);
3111 flow = isl_access_info_compute_flow(ai);
3112 dim = isl_space_alloc(ctx, 0, 3, 3);
3113 mm.must = isl_map_empty(isl_space_copy(dim));
3114 mm.may = isl_map_empty(dim);
3116 isl_flow_foreach(flow, collect_must_may, &mm);
3118 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
3119 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
3120 assert(map_is_equal(mm.may, str));
3121 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3122 assert(map_is_equal(mm.must, str));
3124 isl_map_free(mm.must);
3125 isl_map_free(mm.may);
3126 isl_flow_free(flow);
3129 depth = 5;
3131 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3132 map = isl_map_read_from_str(ctx, str);
3133 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
3135 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3136 map = isl_map_read_from_str(ctx, str);
3137 ai = isl_access_info_add_source(ai, map, 1, &depth);
3139 flow = isl_access_info_compute_flow(ai);
3140 dim = isl_space_alloc(ctx, 0, 5, 5);
3141 mm.must = isl_map_empty(isl_space_copy(dim));
3142 mm.may = isl_map_empty(dim);
3144 isl_flow_foreach(flow, collect_must_may, &mm);
3146 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
3147 assert(map_is_equal(mm.must, str));
3148 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
3149 assert(map_is_equal(mm.may, str));
3151 isl_map_free(mm.must);
3152 isl_map_free(mm.may);
3153 isl_flow_free(flow);
3155 return 0;
3158 /* Check that the dependence analysis proceeds without errors.
3159 * Earlier versions of isl would break down during the analysis
3160 * due to the use of the wrong spaces.
3162 static int test_flow(isl_ctx *ctx)
3164 const char *str;
3165 isl_union_map *access, *schedule;
3166 isl_union_map *must_dep, *may_dep;
3167 int r;
3169 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
3170 access = isl_union_map_read_from_str(ctx, str);
3171 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
3172 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
3173 "S2[] -> [1,0,0,0]; "
3174 "S3[] -> [-1,0,0,0] }";
3175 schedule = isl_union_map_read_from_str(ctx, str);
3176 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
3177 isl_union_map_copy(access), schedule,
3178 &must_dep, &may_dep, NULL, NULL);
3179 isl_union_map_free(may_dep);
3180 isl_union_map_free(must_dep);
3182 return r;
3185 struct {
3186 const char *map;
3187 int sv;
3188 } sv_tests[] = {
3189 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
3190 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
3191 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
3192 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
3193 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
3194 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
3195 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3196 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3197 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
3200 int test_sv(isl_ctx *ctx)
3202 isl_union_map *umap;
3203 int i;
3204 int sv;
3206 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
3207 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
3208 sv = isl_union_map_is_single_valued(umap);
3209 isl_union_map_free(umap);
3210 if (sv < 0)
3211 return -1;
3212 if (sv_tests[i].sv && !sv)
3213 isl_die(ctx, isl_error_internal,
3214 "map not detected as single valued", return -1);
3215 if (!sv_tests[i].sv && sv)
3216 isl_die(ctx, isl_error_internal,
3217 "map detected as single valued", return -1);
3220 return 0;
3223 struct {
3224 const char *str;
3225 int bijective;
3226 } bijective_tests[] = {
3227 { "[N,M]->{[i,j] -> [i]}", 0 },
3228 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
3229 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
3230 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
3231 { "[N,M]->{[i,j] -> [j,i]}", 1 },
3232 { "[N,M]->{[i,j] -> [i+j]}", 0 },
3233 { "[N,M]->{[i,j] -> []}", 0 },
3234 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
3235 { "[N,M]->{[i,j] -> [2i]}", 0 },
3236 { "[N,M]->{[i,j] -> [i,i]}", 0 },
3237 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
3238 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
3239 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
3242 static int test_bijective(struct isl_ctx *ctx)
3244 isl_map *map;
3245 int i;
3246 int bijective;
3248 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
3249 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
3250 bijective = isl_map_is_bijective(map);
3251 isl_map_free(map);
3252 if (bijective < 0)
3253 return -1;
3254 if (bijective_tests[i].bijective && !bijective)
3255 isl_die(ctx, isl_error_internal,
3256 "map not detected as bijective", return -1);
3257 if (!bijective_tests[i].bijective && bijective)
3258 isl_die(ctx, isl_error_internal,
3259 "map detected as bijective", return -1);
3262 return 0;
3265 /* Inputs for isl_pw_qpolynomial_gist tests.
3266 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
3268 struct {
3269 const char *pwqp;
3270 const char *set;
3271 const char *gist;
3272 } pwqp_gist_tests[] = {
3273 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
3274 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
3275 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
3276 "{ [i] -> -1/2 + 1/2 * i }" },
3277 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
3280 static int test_pwqp(struct isl_ctx *ctx)
3282 int i;
3283 const char *str;
3284 isl_set *set;
3285 isl_pw_qpolynomial *pwqp1, *pwqp2;
3286 int equal;
3288 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3289 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3291 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
3292 isl_dim_in, 1, 1);
3294 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3295 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3297 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3299 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3301 isl_pw_qpolynomial_free(pwqp1);
3303 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
3304 str = pwqp_gist_tests[i].pwqp;
3305 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3306 str = pwqp_gist_tests[i].set;
3307 set = isl_set_read_from_str(ctx, str);
3308 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
3309 str = pwqp_gist_tests[i].gist;
3310 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3311 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3312 equal = isl_pw_qpolynomial_is_zero(pwqp1);
3313 isl_pw_qpolynomial_free(pwqp1);
3315 if (equal < 0)
3316 return -1;
3317 if (!equal)
3318 isl_die(ctx, isl_error_unknown,
3319 "unexpected result", return -1);
3322 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
3323 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3324 str = "{ [i] -> ([(2 * [i/2])/5]) }";
3325 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3327 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3329 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3331 isl_pw_qpolynomial_free(pwqp1);
3333 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3334 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3335 str = "{ [x] -> x }";
3336 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3338 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3340 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3342 isl_pw_qpolynomial_free(pwqp1);
3344 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3345 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3346 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3347 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3348 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3349 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3350 isl_pw_qpolynomial_free(pwqp1);
3352 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3353 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3354 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3355 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3356 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3357 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3358 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3359 isl_pw_qpolynomial_free(pwqp1);
3360 isl_pw_qpolynomial_free(pwqp2);
3361 if (equal < 0)
3362 return -1;
3363 if (!equal)
3364 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3366 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3367 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3368 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3369 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3370 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3371 isl_val_one(ctx));
3372 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3373 isl_pw_qpolynomial_free(pwqp1);
3374 isl_pw_qpolynomial_free(pwqp2);
3375 if (equal < 0)
3376 return -1;
3377 if (!equal)
3378 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3380 return 0;
3383 static int test_split_periods(isl_ctx *ctx)
3385 const char *str;
3386 isl_pw_qpolynomial *pwqp;
3388 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3389 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3390 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3391 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3393 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3395 isl_pw_qpolynomial_free(pwqp);
3397 if (!pwqp)
3398 return -1;
3400 return 0;
3403 static int test_union(isl_ctx *ctx)
3405 const char *str;
3406 isl_union_set *uset1, *uset2;
3407 isl_union_map *umap1, *umap2;
3408 int equal;
3410 str = "{ [i] : 0 <= i <= 1 }";
3411 uset1 = isl_union_set_read_from_str(ctx, str);
3412 str = "{ [1] -> [0] }";
3413 umap1 = isl_union_map_read_from_str(ctx, str);
3415 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3416 equal = isl_union_map_is_equal(umap1, umap2);
3418 isl_union_map_free(umap1);
3419 isl_union_map_free(umap2);
3421 if (equal < 0)
3422 return -1;
3423 if (!equal)
3424 isl_die(ctx, isl_error_unknown, "union maps not equal",
3425 return -1);
3427 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3428 umap1 = isl_union_map_read_from_str(ctx, str);
3429 str = "{ A[i]; B[i] }";
3430 uset1 = isl_union_set_read_from_str(ctx, str);
3432 uset2 = isl_union_map_domain(umap1);
3434 equal = isl_union_set_is_equal(uset1, uset2);
3436 isl_union_set_free(uset1);
3437 isl_union_set_free(uset2);
3439 if (equal < 0)
3440 return -1;
3441 if (!equal)
3442 isl_die(ctx, isl_error_unknown, "union sets not equal",
3443 return -1);
3445 return 0;
3448 /* Check that computing a bound of a non-zero polynomial over an unbounded
3449 * domain does not produce a rational value.
3450 * In particular, check that the upper bound is infinity.
3452 static int test_bound_unbounded_domain(isl_ctx *ctx)
3454 const char *str;
3455 isl_pw_qpolynomial *pwqp;
3456 isl_pw_qpolynomial_fold *pwf, *pwf2;
3457 isl_bool equal;
3459 str = "{ [m,n] -> -m * n }";
3460 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3461 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3462 str = "{ infty }";
3463 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3464 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3465 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
3466 isl_pw_qpolynomial_fold_free(pwf);
3467 isl_pw_qpolynomial_fold_free(pwf2);
3469 if (equal < 0)
3470 return -1;
3471 if (!equal)
3472 isl_die(ctx, isl_error_unknown,
3473 "expecting infinite polynomial bound", return -1);
3475 return 0;
3478 static int test_bound(isl_ctx *ctx)
3480 const char *str;
3481 isl_size dim;
3482 isl_pw_qpolynomial *pwqp;
3483 isl_pw_qpolynomial_fold *pwf;
3485 if (test_bound_unbounded_domain(ctx) < 0)
3486 return -1;
3488 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3489 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3490 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3491 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3492 isl_pw_qpolynomial_fold_free(pwf);
3493 if (dim < 0)
3494 return -1;
3495 if (dim != 4)
3496 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3497 return -1);
3499 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3500 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3501 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3502 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3503 isl_pw_qpolynomial_fold_free(pwf);
3504 if (dim < 0)
3505 return -1;
3506 if (dim != 1)
3507 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3508 return -1);
3510 return 0;
3513 /* isl_set is defined to isl_map internally, so the corresponding elements
3514 * are isl_basic_map objects.
3516 #undef EL_BASE
3517 #undef SET_BASE
3518 #define EL_BASE basic_map
3519 #define SET_BASE set
3520 #include "isl_test_list_templ.c"
3522 #undef EL_BASE
3523 #undef SET_BASE
3524 #define EL_BASE basic_set
3525 #define SET_BASE union_set
3526 #include "isl_test_list_templ.c"
3528 #undef EL_BASE
3529 #undef SET_BASE
3530 #define EL_BASE set
3531 #define SET_BASE union_set
3532 #include "isl_test_list_templ.c"
3534 #undef EL_BASE
3535 #undef SET_BASE
3536 #define EL_BASE basic_map
3537 #define SET_BASE map
3538 #include "isl_test_list_templ.c"
3540 #undef EL_BASE
3541 #undef SET_BASE
3542 #define EL_BASE map
3543 #define SET_BASE union_map
3544 #include "isl_test_list_templ.c"
3546 /* Check that the conversion from isl objects to lists works as expected.
3548 static int test_get_list(isl_ctx *ctx)
3550 if (test_get_list_basic_map_from_set(ctx, "{ [0]; [2]; [3] }"))
3551 return -1;
3552 if (test_get_list_basic_set_from_union_set(ctx, "{ A[0]; B[2]; B[3] }"))
3553 return -1;
3554 if (test_get_list_set_from_union_set(ctx, "{ A[0]; A[2]; B[3] }"))
3555 return -1;
3556 if (test_get_list_basic_map_from_map(ctx,
3557 "{ [0] -> [0]; [2] -> [0]; [3] -> [0] }"))
3558 return -1;
3559 if (test_get_list_map_from_union_map(ctx,
3560 "{ A[0] -> [0]; A[2] -> [0]; B[3] -> [0] }"))
3561 return -1;
3563 return 0;
3566 static int test_lift(isl_ctx *ctx)
3568 const char *str;
3569 isl_basic_map *bmap;
3570 isl_basic_set *bset;
3572 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3573 bset = isl_basic_set_read_from_str(ctx, str);
3574 bset = isl_basic_set_lift(bset);
3575 bmap = isl_basic_map_from_range(bset);
3576 bset = isl_basic_map_domain(bmap);
3577 isl_basic_set_free(bset);
3579 return 0;
3582 /* Check that isl_set_is_subset is not confused by identical
3583 * integer divisions.
3584 * The call to isl_set_normalize ensures that the equality constraints
3585 * a = b = 0 are discovered, turning e0 and e1 into identical
3586 * integer divisions. Any further simplification would remove
3587 * the duplicate integer divisions.
3589 static isl_stat test_subset_duplicate_integer_divisions(isl_ctx *ctx)
3591 const char *str;
3592 isl_bool is_subset;
3593 isl_set *set1, *set2;
3595 str = "{ [a, b, c, d] : "
3596 "exists (e0 = floor((a + d)/4), e1 = floor((d)/4), "
3597 "e2 = floor((-a - d + 4 *floor((a + d)/4))/10), "
3598 "e3 = floor((-d + 4*floor((d)/4))/10): "
3599 "10e2 = -a - 2c - d + 4e0 and 10e3 = -2c - d + 4e1 and "
3600 "b >= 0 and a <= 0 and b <= a) }";
3601 set1 = isl_set_read_from_str(ctx, str);
3602 set2 = isl_set_read_from_str(ctx, str);
3603 set2 = isl_set_normalize(set2);
3605 is_subset = isl_set_is_subset(set1, set2);
3607 isl_set_free(set1);
3608 isl_set_free(set2);
3610 if (is_subset < 0)
3611 return isl_stat_error;
3612 if (!is_subset)
3613 isl_die(ctx, isl_error_unknown,
3614 "set is not considered to be a subset of itself",
3615 return isl_stat_error);
3617 return isl_stat_ok;
3620 struct {
3621 const char *set1;
3622 const char *set2;
3623 int subset;
3624 } subset_tests[] = {
3625 { "{ [112, 0] }",
3626 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3627 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3628 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3629 { "{ [65] }",
3630 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3631 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3632 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3633 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3634 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3635 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3636 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3637 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3638 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3639 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3640 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3641 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3642 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3643 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3644 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3645 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3646 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3647 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3648 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3649 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3650 "4e0 <= -57 + i0 + i1)) or "
3651 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3652 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3653 "4e0 >= -61 + i0 + i1)) or "
3654 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3655 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3658 static int test_subset(isl_ctx *ctx)
3660 int i;
3661 isl_set *set1, *set2;
3662 int subset;
3664 if (test_subset_duplicate_integer_divisions(ctx) < 0)
3665 return -1;
3667 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3668 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3669 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3670 subset = isl_set_is_subset(set1, set2);
3671 isl_set_free(set1);
3672 isl_set_free(set2);
3673 if (subset < 0)
3674 return -1;
3675 if (subset != subset_tests[i].subset)
3676 isl_die(ctx, isl_error_unknown,
3677 "incorrect subset result", return -1);
3680 return 0;
3683 struct {
3684 const char *minuend;
3685 const char *subtrahend;
3686 const char *difference;
3687 } subtract_domain_tests[] = {
3688 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3689 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3690 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3693 static int test_subtract(isl_ctx *ctx)
3695 int i;
3696 isl_union_map *umap1, *umap2;
3697 isl_union_pw_multi_aff *upma1, *upma2;
3698 isl_union_set *uset;
3699 int equal;
3701 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3702 umap1 = isl_union_map_read_from_str(ctx,
3703 subtract_domain_tests[i].minuend);
3704 uset = isl_union_set_read_from_str(ctx,
3705 subtract_domain_tests[i].subtrahend);
3706 umap2 = isl_union_map_read_from_str(ctx,
3707 subtract_domain_tests[i].difference);
3708 umap1 = isl_union_map_subtract_domain(umap1, uset);
3709 equal = isl_union_map_is_equal(umap1, umap2);
3710 isl_union_map_free(umap1);
3711 isl_union_map_free(umap2);
3712 if (equal < 0)
3713 return -1;
3714 if (!equal)
3715 isl_die(ctx, isl_error_unknown,
3716 "incorrect subtract domain result", return -1);
3719 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3720 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3721 subtract_domain_tests[i].minuend);
3722 uset = isl_union_set_read_from_str(ctx,
3723 subtract_domain_tests[i].subtrahend);
3724 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3725 subtract_domain_tests[i].difference);
3726 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3727 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3728 isl_union_pw_multi_aff_free(upma1);
3729 isl_union_pw_multi_aff_free(upma2);
3730 if (equal < 0)
3731 return -1;
3732 if (!equal)
3733 isl_die(ctx, isl_error_unknown,
3734 "incorrect subtract domain result", return -1);
3737 return 0;
3740 /* Check that intersecting the empty basic set with another basic set
3741 * does not increase the number of constraints. In particular,
3742 * the empty basic set should maintain its canonical representation.
3744 static int test_intersect_1(isl_ctx *ctx)
3746 int n1, n2;
3747 isl_basic_set *bset1, *bset2;
3749 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3750 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3751 n1 = isl_basic_set_n_constraint(bset1);
3752 bset1 = isl_basic_set_intersect(bset1, bset2);
3753 n2 = isl_basic_set_n_constraint(bset1);
3754 isl_basic_set_free(bset1);
3755 if (!bset1)
3756 return -1;
3757 if (n1 != n2)
3758 isl_die(ctx, isl_error_unknown,
3759 "number of constraints of empty set changed",
3760 return -1);
3762 return 0;
3765 /* Check that intersecting a set with itself does not cause
3766 * an explosion in the number of disjuncts.
3768 static isl_stat test_intersect_2(isl_ctx *ctx)
3770 int i;
3771 isl_set *set;
3773 set = isl_set_read_from_str(ctx, "{ [x,y] : x >= 0 or y >= 0 }");
3774 for (i = 0; i < 100; ++i)
3775 set = isl_set_intersect(set, isl_set_copy(set));
3776 isl_set_free(set);
3777 if (!set)
3778 return isl_stat_error;
3779 return isl_stat_ok;
3782 /* Perform some intersection tests.
3784 static int test_intersect(isl_ctx *ctx)
3786 if (test_intersect_1(ctx) < 0)
3787 return -1;
3788 if (test_intersect_2(ctx) < 0)
3789 return -1;
3791 return 0;
3794 int test_factorize(isl_ctx *ctx)
3796 const char *str;
3797 isl_basic_set *bset;
3798 isl_factorizer *f;
3800 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3801 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3802 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3803 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3804 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3805 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3806 "3i5 >= -2i0 - i2 + 3i4 }";
3807 bset = isl_basic_set_read_from_str(ctx, str);
3808 f = isl_basic_set_factorizer(bset);
3809 isl_basic_set_free(bset);
3810 isl_factorizer_free(f);
3811 if (!f)
3812 isl_die(ctx, isl_error_unknown,
3813 "failed to construct factorizer", return -1);
3815 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3816 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3817 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3818 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3819 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3820 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3821 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3822 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3823 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3824 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3825 bset = isl_basic_set_read_from_str(ctx, str);
3826 f = isl_basic_set_factorizer(bset);
3827 isl_basic_set_free(bset);
3828 isl_factorizer_free(f);
3829 if (!f)
3830 isl_die(ctx, isl_error_unknown,
3831 "failed to construct factorizer", return -1);
3833 return 0;
3836 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3838 int *injective = user;
3840 *injective = isl_map_is_injective(map);
3841 isl_map_free(map);
3843 if (*injective < 0 || !*injective)
3844 return isl_stat_error;
3846 return isl_stat_ok;
3849 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3850 const char *r, const char *s, int tilable, int parallel)
3852 int i;
3853 isl_union_set *D;
3854 isl_union_map *W, *R, *S;
3855 isl_union_map *empty;
3856 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3857 isl_union_map *validity, *proximity, *coincidence;
3858 isl_union_map *schedule;
3859 isl_union_map *test;
3860 isl_union_set *delta;
3861 isl_union_set *domain;
3862 isl_set *delta_set;
3863 isl_set *slice;
3864 isl_set *origin;
3865 isl_schedule_constraints *sc;
3866 isl_schedule *sched;
3867 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3869 D = isl_union_set_read_from_str(ctx, d);
3870 W = isl_union_map_read_from_str(ctx, w);
3871 R = isl_union_map_read_from_str(ctx, r);
3872 S = isl_union_map_read_from_str(ctx, s);
3874 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3875 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3877 empty = isl_union_map_empty(isl_union_map_get_space(S));
3878 isl_union_map_compute_flow(isl_union_map_copy(R),
3879 isl_union_map_copy(W), empty,
3880 isl_union_map_copy(S),
3881 &dep_raw, NULL, NULL, NULL);
3882 isl_union_map_compute_flow(isl_union_map_copy(W),
3883 isl_union_map_copy(W),
3884 isl_union_map_copy(R),
3885 isl_union_map_copy(S),
3886 &dep_waw, &dep_war, NULL, NULL);
3888 dep = isl_union_map_union(dep_waw, dep_war);
3889 dep = isl_union_map_union(dep, dep_raw);
3890 validity = isl_union_map_copy(dep);
3891 coincidence = isl_union_map_copy(dep);
3892 proximity = isl_union_map_copy(dep);
3894 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3895 sc = isl_schedule_constraints_set_validity(sc, validity);
3896 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3897 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3898 sched = isl_schedule_constraints_compute_schedule(sc);
3899 schedule = isl_schedule_get_map(sched);
3900 isl_schedule_free(sched);
3901 isl_union_map_free(W);
3902 isl_union_map_free(R);
3903 isl_union_map_free(S);
3905 is_injection = 1;
3906 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3908 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3909 is_complete = isl_union_set_is_subset(D, domain);
3910 isl_union_set_free(D);
3911 isl_union_set_free(domain);
3913 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3914 test = isl_union_map_apply_range(test, dep);
3915 test = isl_union_map_apply_range(test, schedule);
3917 delta = isl_union_map_deltas(test);
3918 if (isl_union_set_n_set(delta) == 0) {
3919 is_tilable = 1;
3920 is_parallel = 1;
3921 is_nonneg = 1;
3922 isl_union_set_free(delta);
3923 } else {
3924 isl_size dim;
3926 delta_set = isl_set_from_union_set(delta);
3928 slice = isl_set_universe(isl_set_get_space(delta_set));
3929 for (i = 0; i < tilable; ++i)
3930 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3931 is_tilable = isl_set_is_subset(delta_set, slice);
3932 isl_set_free(slice);
3934 slice = isl_set_universe(isl_set_get_space(delta_set));
3935 for (i = 0; i < parallel; ++i)
3936 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3937 is_parallel = isl_set_is_subset(delta_set, slice);
3938 isl_set_free(slice);
3940 origin = isl_set_universe(isl_set_get_space(delta_set));
3941 dim = isl_set_dim(origin, isl_dim_set);
3942 if (dim < 0)
3943 origin = isl_set_free(origin);
3944 for (i = 0; i < dim; ++i)
3945 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3947 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3948 delta_set = isl_set_lexmin(delta_set);
3950 is_nonneg = isl_set_is_equal(delta_set, origin);
3952 isl_set_free(origin);
3953 isl_set_free(delta_set);
3956 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3957 is_injection < 0 || is_complete < 0)
3958 return -1;
3959 if (!is_complete)
3960 isl_die(ctx, isl_error_unknown,
3961 "generated schedule incomplete", return -1);
3962 if (!is_injection)
3963 isl_die(ctx, isl_error_unknown,
3964 "generated schedule not injective on each statement",
3965 return -1);
3966 if (!is_nonneg)
3967 isl_die(ctx, isl_error_unknown,
3968 "negative dependences in generated schedule",
3969 return -1);
3970 if (!is_tilable)
3971 isl_die(ctx, isl_error_unknown,
3972 "generated schedule not as tilable as expected",
3973 return -1);
3974 if (!is_parallel)
3975 isl_die(ctx, isl_error_unknown,
3976 "generated schedule not as parallel as expected",
3977 return -1);
3979 return 0;
3982 /* Compute a schedule for the given instance set, validity constraints,
3983 * proximity constraints and context and return a corresponding union map
3984 * representation.
3986 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3987 const char *domain, const char *validity, const char *proximity,
3988 const char *context)
3990 isl_set *con;
3991 isl_union_set *dom;
3992 isl_union_map *dep;
3993 isl_union_map *prox;
3994 isl_schedule_constraints *sc;
3995 isl_schedule *schedule;
3996 isl_union_map *sched;
3998 con = isl_set_read_from_str(ctx, context);
3999 dom = isl_union_set_read_from_str(ctx, domain);
4000 dep = isl_union_map_read_from_str(ctx, validity);
4001 prox = isl_union_map_read_from_str(ctx, proximity);
4002 sc = isl_schedule_constraints_on_domain(dom);
4003 sc = isl_schedule_constraints_set_context(sc, con);
4004 sc = isl_schedule_constraints_set_validity(sc, dep);
4005 sc = isl_schedule_constraints_set_proximity(sc, prox);
4006 schedule = isl_schedule_constraints_compute_schedule(sc);
4007 sched = isl_schedule_get_map(schedule);
4008 isl_schedule_free(schedule);
4010 return sched;
4013 /* Compute a schedule for the given instance set, validity constraints and
4014 * proximity constraints and return a corresponding union map representation.
4016 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
4017 const char *domain, const char *validity, const char *proximity)
4019 return compute_schedule_with_context(ctx, domain, validity, proximity,
4020 "{ : }");
4023 /* Check that a schedule can be constructed on the given domain
4024 * with the given validity and proximity constraints.
4026 static int test_has_schedule(isl_ctx *ctx, const char *domain,
4027 const char *validity, const char *proximity)
4029 isl_union_map *sched;
4031 sched = compute_schedule(ctx, domain, validity, proximity);
4032 if (!sched)
4033 return -1;
4035 isl_union_map_free(sched);
4036 return 0;
4039 int test_special_schedule(isl_ctx *ctx, const char *domain,
4040 const char *validity, const char *proximity, const char *expected_sched)
4042 isl_union_map *sched1, *sched2;
4043 int equal;
4045 sched1 = compute_schedule(ctx, domain, validity, proximity);
4046 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
4048 equal = isl_union_map_is_equal(sched1, sched2);
4049 isl_union_map_free(sched1);
4050 isl_union_map_free(sched2);
4052 if (equal < 0)
4053 return -1;
4054 if (!equal)
4055 isl_die(ctx, isl_error_unknown, "unexpected schedule",
4056 return -1);
4058 return 0;
4061 /* Check that the schedule map is properly padded, i.e., that the range
4062 * lives in a single space.
4064 static int test_padded_schedule(isl_ctx *ctx)
4066 const char *str;
4067 isl_union_set *D;
4068 isl_union_map *validity, *proximity;
4069 isl_schedule_constraints *sc;
4070 isl_schedule *sched;
4071 isl_union_map *umap;
4072 isl_union_set *range;
4073 isl_set *set;
4075 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
4076 D = isl_union_set_read_from_str(ctx, str);
4077 validity = isl_union_map_empty(isl_union_set_get_space(D));
4078 proximity = isl_union_map_copy(validity);
4079 sc = isl_schedule_constraints_on_domain(D);
4080 sc = isl_schedule_constraints_set_validity(sc, validity);
4081 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4082 sched = isl_schedule_constraints_compute_schedule(sc);
4083 umap = isl_schedule_get_map(sched);
4084 isl_schedule_free(sched);
4085 range = isl_union_map_range(umap);
4086 set = isl_set_from_union_set(range);
4087 isl_set_free(set);
4089 if (!set)
4090 return -1;
4092 return 0;
4095 /* Check that conditional validity constraints are also taken into
4096 * account across bands.
4097 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
4098 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
4099 * and then check that the adjacent order constraint C[2,1]->D[2,0]
4100 * is enforced by the rest of the schedule.
4102 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
4104 const char *str;
4105 isl_union_set *domain;
4106 isl_union_map *validity, *proximity, *condition;
4107 isl_union_map *sink, *source, *dep;
4108 isl_schedule_constraints *sc;
4109 isl_schedule *schedule;
4110 isl_union_access_info *access;
4111 isl_union_flow *flow;
4112 int empty;
4114 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4115 "A[k] : k >= 1 and k <= -1 + n; "
4116 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4117 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
4118 domain = isl_union_set_read_from_str(ctx, str);
4119 sc = isl_schedule_constraints_on_domain(domain);
4120 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
4121 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4122 "D[k, i] -> C[1 + k, i] : "
4123 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4124 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
4125 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
4126 validity = isl_union_map_read_from_str(ctx, str);
4127 sc = isl_schedule_constraints_set_validity(sc, validity);
4128 str = "[n] -> { C[k, i] -> D[k, i] : "
4129 "0 <= i <= -1 + k and k <= -1 + n }";
4130 proximity = isl_union_map_read_from_str(ctx, str);
4131 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4132 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
4133 "i <= -1 + k and i >= 1 and k <= -2 + n; "
4134 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
4135 "k <= -1 + n and i >= 0 and i <= -2 + k }";
4136 condition = isl_union_map_read_from_str(ctx, str);
4137 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
4138 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4139 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
4140 "i >= 0 and i <= -1 + k and k <= -1 + n and "
4141 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
4142 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
4143 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4144 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
4145 "k <= -1 + n and i >= 0 and i <= -1 + k and "
4146 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
4147 validity = isl_union_map_read_from_str(ctx, str);
4148 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4149 validity);
4150 schedule = isl_schedule_constraints_compute_schedule(sc);
4151 str = "{ D[2,0] -> [] }";
4152 sink = isl_union_map_read_from_str(ctx, str);
4153 access = isl_union_access_info_from_sink(sink);
4154 str = "{ C[2,1] -> [] }";
4155 source = isl_union_map_read_from_str(ctx, str);
4156 access = isl_union_access_info_set_must_source(access, source);
4157 access = isl_union_access_info_set_schedule(access, schedule);
4158 flow = isl_union_access_info_compute_flow(access);
4159 dep = isl_union_flow_get_must_dependence(flow);
4160 isl_union_flow_free(flow);
4161 empty = isl_union_map_is_empty(dep);
4162 isl_union_map_free(dep);
4164 if (empty < 0)
4165 return -1;
4166 if (empty)
4167 isl_die(ctx, isl_error_unknown,
4168 "conditional validity not respected", return -1);
4170 return 0;
4173 /* Check that the test for violated conditional validity constraints
4174 * is not confused by domain compression.
4175 * In particular, earlier versions of isl would apply
4176 * a schedule on the compressed domains to the original domains,
4177 * resulting in a failure to detect that the default schedule
4178 * violates the conditional validity constraints.
4180 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
4182 const char *str;
4183 isl_bool empty;
4184 isl_union_set *domain;
4185 isl_union_map *validity, *condition;
4186 isl_schedule_constraints *sc;
4187 isl_schedule *schedule;
4188 isl_union_map *umap;
4189 isl_map *map, *ge;
4191 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
4192 domain = isl_union_set_read_from_str(ctx, str);
4193 sc = isl_schedule_constraints_on_domain(domain);
4194 str = "{ B[1, i] -> A[0, i + 1] }";
4195 condition = isl_union_map_read_from_str(ctx, str);
4196 str = "{ A[0, i] -> B[1, i - 1] }";
4197 validity = isl_union_map_read_from_str(ctx, str);
4198 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4199 isl_union_map_copy(validity));
4200 schedule = isl_schedule_constraints_compute_schedule(sc);
4201 umap = isl_schedule_get_map(schedule);
4202 isl_schedule_free(schedule);
4203 validity = isl_union_map_apply_domain(validity,
4204 isl_union_map_copy(umap));
4205 validity = isl_union_map_apply_range(validity, umap);
4206 map = isl_map_from_union_map(validity);
4207 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4208 map = isl_map_intersect(map, ge);
4209 empty = isl_map_is_empty(map);
4210 isl_map_free(map);
4212 if (empty < 0)
4213 return -1;
4214 if (!empty)
4215 isl_die(ctx, isl_error_unknown,
4216 "conditional validity constraints not satisfied",
4217 return -1);
4219 return 0;
4222 /* Input for testing of schedule construction based on
4223 * conditional constraints.
4225 * domain is the iteration domain
4226 * flow are the flow dependences, which determine the validity and
4227 * proximity constraints
4228 * condition are the conditions on the conditional validity constraints
4229 * conditional_validity are the conditional validity constraints
4230 * outer_band_n is the expected number of members in the outer band
4232 struct {
4233 const char *domain;
4234 const char *flow;
4235 const char *condition;
4236 const char *conditional_validity;
4237 int outer_band_n;
4238 } live_range_tests[] = {
4239 /* Contrived example that illustrates that we need to keep
4240 * track of tagged condition dependences and
4241 * tagged conditional validity dependences
4242 * in isl_sched_edge separately.
4243 * In particular, the conditional validity constraints on A
4244 * cannot be satisfied,
4245 * but they can be ignored because there are no corresponding
4246 * condition constraints. However, we do have an additional
4247 * conditional validity constraint that maps to the same
4248 * dependence relation
4249 * as the condition constraint on B. If we did not make a distinction
4250 * between tagged condition and tagged conditional validity
4251 * dependences, then we
4252 * could end up treating this shared dependence as an condition
4253 * constraint on A, forcing a localization of the conditions,
4254 * which is impossible.
4256 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
4257 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
4258 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
4259 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4260 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4261 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
4264 /* TACO 2013 Fig. 7 */
4265 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4266 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4267 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4268 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
4269 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
4270 "0 <= i < n and 0 <= j < n - 1 }",
4271 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
4272 "0 <= i < n and 0 <= j < j' < n;"
4273 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
4274 "0 <= i < i' < n and 0 <= j,j' < n;"
4275 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
4276 "0 <= i,j,j' < n and j < j' }",
4279 /* TACO 2013 Fig. 7, without tags */
4280 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4281 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4282 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4283 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4284 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4285 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
4286 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
4287 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
4290 /* TACO 2013 Fig. 12 */
4291 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
4292 "S3[i,3] : 0 <= i <= 1 }",
4293 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
4294 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
4295 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
4296 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
4297 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4298 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
4299 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4300 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
4301 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
4302 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
4303 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
4308 /* Test schedule construction based on conditional constraints.
4309 * In particular, check the number of members in the outer band node
4310 * as an indication of whether tiling is possible or not.
4312 static int test_conditional_schedule_constraints(isl_ctx *ctx)
4314 int i;
4315 isl_union_set *domain;
4316 isl_union_map *condition;
4317 isl_union_map *flow;
4318 isl_union_map *validity;
4319 isl_schedule_constraints *sc;
4320 isl_schedule *schedule;
4321 isl_schedule_node *node;
4322 int n_member;
4324 if (test_special_conditional_schedule_constraints(ctx) < 0)
4325 return -1;
4326 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
4327 return -1;
4329 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
4330 domain = isl_union_set_read_from_str(ctx,
4331 live_range_tests[i].domain);
4332 flow = isl_union_map_read_from_str(ctx,
4333 live_range_tests[i].flow);
4334 condition = isl_union_map_read_from_str(ctx,
4335 live_range_tests[i].condition);
4336 validity = isl_union_map_read_from_str(ctx,
4337 live_range_tests[i].conditional_validity);
4338 sc = isl_schedule_constraints_on_domain(domain);
4339 sc = isl_schedule_constraints_set_validity(sc,
4340 isl_union_map_copy(flow));
4341 sc = isl_schedule_constraints_set_proximity(sc, flow);
4342 sc = isl_schedule_constraints_set_conditional_validity(sc,
4343 condition, validity);
4344 schedule = isl_schedule_constraints_compute_schedule(sc);
4345 node = isl_schedule_get_root(schedule);
4346 while (node &&
4347 isl_schedule_node_get_type(node) != isl_schedule_node_band)
4348 node = isl_schedule_node_first_child(node);
4349 n_member = isl_schedule_node_band_n_member(node);
4350 isl_schedule_node_free(node);
4351 isl_schedule_free(schedule);
4353 if (!schedule)
4354 return -1;
4355 if (n_member != live_range_tests[i].outer_band_n)
4356 isl_die(ctx, isl_error_unknown,
4357 "unexpected number of members in outer band",
4358 return -1);
4360 return 0;
4363 /* Check that the schedule computed for the given instance set and
4364 * dependence relation strongly satisfies the dependences.
4365 * In particular, check that no instance is scheduled before
4366 * or together with an instance on which it depends.
4367 * Earlier versions of isl would produce a schedule that
4368 * only weakly satisfies the dependences.
4370 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
4372 const char *domain, *dep;
4373 isl_union_map *D, *schedule;
4374 isl_map *map, *ge;
4375 int empty;
4377 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
4378 "A[i0] : 0 <= i0 <= 1 }";
4379 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
4380 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
4381 schedule = compute_schedule(ctx, domain, dep, dep);
4382 D = isl_union_map_read_from_str(ctx, dep);
4383 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
4384 D = isl_union_map_apply_range(D, schedule);
4385 map = isl_map_from_union_map(D);
4386 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4387 map = isl_map_intersect(map, ge);
4388 empty = isl_map_is_empty(map);
4389 isl_map_free(map);
4391 if (empty < 0)
4392 return -1;
4393 if (!empty)
4394 isl_die(ctx, isl_error_unknown,
4395 "dependences not strongly satisfied", return -1);
4397 return 0;
4400 /* Compute a schedule for input where the instance set constraints
4401 * conflict with the context constraints.
4402 * Earlier versions of isl did not properly handle this situation.
4404 static int test_conflicting_context_schedule(isl_ctx *ctx)
4406 isl_union_map *schedule;
4407 const char *domain, *context;
4409 domain = "[n] -> { A[] : n >= 0 }";
4410 context = "[n] -> { : n < 0 }";
4411 schedule = compute_schedule_with_context(ctx,
4412 domain, "{}", "{}", context);
4413 isl_union_map_free(schedule);
4415 if (!schedule)
4416 return -1;
4418 return 0;
4421 /* Check that a set of schedule constraints that only allow for
4422 * a coalescing schedule still produces a schedule even if the user
4423 * request a non-coalescing schedule. Earlier versions of isl
4424 * would not handle this case correctly.
4426 static int test_coalescing_schedule(isl_ctx *ctx)
4428 const char *domain, *dep;
4429 isl_union_set *I;
4430 isl_union_map *D;
4431 isl_schedule_constraints *sc;
4432 isl_schedule *schedule;
4433 int treat_coalescing;
4435 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4436 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4437 I = isl_union_set_read_from_str(ctx, domain);
4438 D = isl_union_map_read_from_str(ctx, dep);
4439 sc = isl_schedule_constraints_on_domain(I);
4440 sc = isl_schedule_constraints_set_validity(sc, D);
4441 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4442 isl_options_set_schedule_treat_coalescing(ctx, 1);
4443 schedule = isl_schedule_constraints_compute_schedule(sc);
4444 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4445 isl_schedule_free(schedule);
4446 if (!schedule)
4447 return -1;
4448 return 0;
4451 /* Check that the scheduler does not perform any needless
4452 * compound skewing. Earlier versions of isl would compute
4453 * schedules in terms of transformed schedule coefficients and
4454 * would not accurately keep track of the sum of the original
4455 * schedule coefficients. It could then produce the schedule
4456 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
4457 * for the input below instead of the schedule below.
4459 static int test_skewing_schedule(isl_ctx *ctx)
4461 const char *D, *V, *P, *S;
4463 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
4464 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
4465 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
4466 "-1 <= a-i + b-j + c-k <= 1 }";
4467 P = "{ }";
4468 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
4470 return test_special_schedule(ctx, D, V, P, S);
4473 int test_schedule(isl_ctx *ctx)
4475 const char *D, *W, *R, *V, *P, *S;
4476 int max_coincidence;
4477 int treat_coalescing;
4479 /* Handle resulting schedule with zero bands. */
4480 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4481 return -1;
4483 /* Jacobi */
4484 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4485 W = "{ S1[t,i] -> a[t,i] }";
4486 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4487 "S1[t,i] -> a[t-1,i+1] }";
4488 S = "{ S1[t,i] -> [t,i] }";
4489 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4490 return -1;
4492 /* Fig. 5 of CC2008 */
4493 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
4494 "j <= -1 + N }";
4495 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
4496 "j >= 2 and j <= -1 + N }";
4497 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
4498 "j >= 2 and j <= -1 + N; "
4499 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
4500 "j >= 2 and j <= -1 + N }";
4501 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4502 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4503 return -1;
4505 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
4506 W = "{ S1[i] -> a[i] }";
4507 R = "{ S2[i] -> a[i+1] }";
4508 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4509 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4510 return -1;
4512 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
4513 W = "{ S1[i] -> a[i] }";
4514 R = "{ S2[i] -> a[9-i] }";
4515 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4516 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4517 return -1;
4519 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4520 W = "{ S1[i] -> a[i] }";
4521 R = "[N] -> { S2[i] -> a[N-1-i] }";
4522 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4523 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4524 return -1;
4526 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4527 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4528 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4529 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4530 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4531 return -1;
4533 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4534 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4535 R = "{ S2[i,j] -> a[i-1,j] }";
4536 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4537 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4538 return -1;
4540 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4541 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4542 R = "{ S2[i,j] -> a[i,j-1] }";
4543 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4544 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4545 return -1;
4547 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4548 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4549 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4550 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4551 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4552 "S_0[] -> [0, 0, 0] }";
4553 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4554 return -1;
4555 ctx->opt->schedule_parametric = 0;
4556 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4557 return -1;
4558 ctx->opt->schedule_parametric = 1;
4560 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4561 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4562 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4563 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4564 "S4[i] -> a[i,N] }";
4565 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4566 "S4[i] -> [4,i,0] }";
4567 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4568 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4569 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4570 return -1;
4571 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4573 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4574 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4575 "j <= N }";
4576 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4577 "j <= N; "
4578 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4579 "j <= N }";
4580 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4581 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4582 return -1;
4584 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4585 " S_2[t] : t >= 0 and t <= -1 + N; "
4586 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4587 "i <= -1 + N }";
4588 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4589 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4590 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4591 "i >= 0 and i <= -1 + N }";
4592 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4593 "i >= 0 and i <= -1 + N; "
4594 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4595 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4596 " S_0[t] -> [0, t, 0] }";
4598 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4599 return -1;
4600 ctx->opt->schedule_parametric = 0;
4601 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4602 return -1;
4603 ctx->opt->schedule_parametric = 1;
4605 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4606 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4607 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4608 return -1;
4610 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4611 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4612 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
4613 "j >= 0 and j <= -1 + N; "
4614 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4615 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4616 "j >= 0 and j <= -1 + N; "
4617 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4618 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4619 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4620 return -1;
4622 D = "{ S_0[i] : i >= 0 }";
4623 W = "{ S_0[i] -> a[i] : i >= 0 }";
4624 R = "{ S_0[i] -> a[0] : i >= 0 }";
4625 S = "{ S_0[i] -> [0, i, 0] }";
4626 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4627 return -1;
4629 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4630 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4631 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4632 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4633 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4634 return -1;
4636 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4637 "k <= -1 + n and k >= 0 }";
4638 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4639 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4640 "k <= -1 + n and k >= 0; "
4641 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4642 "k <= -1 + n and k >= 0; "
4643 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4644 "k <= -1 + n and k >= 0 }";
4645 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4646 ctx->opt->schedule_outer_coincidence = 1;
4647 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4648 return -1;
4649 ctx->opt->schedule_outer_coincidence = 0;
4651 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4652 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4653 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4654 "Stmt_for_body24[i0, i1, 1, 0]:"
4655 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4656 "Stmt_for_body7[i0, i1, i2]:"
4657 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4658 "i2 <= 7 }";
4660 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4661 "Stmt_for_body24[1, i1, i2, i3]:"
4662 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4663 "i2 >= 1;"
4664 "Stmt_for_body24[0, i1, i2, i3] -> "
4665 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4666 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4667 "i3 >= 0;"
4668 "Stmt_for_body24[0, i1, i2, i3] ->"
4669 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4670 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4671 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4672 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4673 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4674 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4675 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4676 "i1 <= 6 and i1 >= 0;"
4677 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4678 "Stmt_for_body7[i0, i1, i2] -> "
4679 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4680 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4681 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4682 "Stmt_for_body7[i0, i1, i2] -> "
4683 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4684 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4685 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4686 P = V;
4688 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4689 isl_options_set_schedule_treat_coalescing(ctx, 0);
4690 if (test_has_schedule(ctx, D, V, P) < 0)
4691 return -1;
4692 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4694 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4695 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4696 "j >= 1 and j <= 7;"
4697 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4698 "j >= 1 and j <= 8 }";
4699 P = "{ }";
4700 S = "{ S_0[i, j] -> [i + j, i] }";
4701 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4702 if (test_special_schedule(ctx, D, V, P, S) < 0)
4703 return -1;
4704 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4706 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4707 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4708 "j >= 0 and j <= -1 + i }";
4709 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4710 "i <= -1 + N and j >= 0;"
4711 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4712 "i <= -2 + N }";
4713 P = "{ }";
4714 S = "{ S_0[i, j] -> [i, j] }";
4715 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4716 if (test_special_schedule(ctx, D, V, P, S) < 0)
4717 return -1;
4718 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4720 /* Test both algorithms on a case with only proximity dependences. */
4721 D = "{ S[i,j] : 0 <= i <= 10 }";
4722 V = "{ }";
4723 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4724 S = "{ S[i, j] -> [j, i] }";
4725 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4726 if (test_special_schedule(ctx, D, V, P, S) < 0)
4727 return -1;
4728 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4729 if (test_special_schedule(ctx, D, V, P, S) < 0)
4730 return -1;
4732 D = "{ A[a]; B[] }";
4733 V = "{}";
4734 P = "{ A[a] -> B[] }";
4735 if (test_has_schedule(ctx, D, V, P) < 0)
4736 return -1;
4738 if (test_padded_schedule(ctx) < 0)
4739 return -1;
4741 /* Check that check for progress is not confused by rational
4742 * solution.
4744 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4745 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4746 "i0 <= -2 + N; "
4747 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4748 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4749 P = "{}";
4750 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4751 if (test_has_schedule(ctx, D, V, P) < 0)
4752 return -1;
4753 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4755 /* Check that we allow schedule rows that are only non-trivial
4756 * on some full-dimensional domains.
4758 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4759 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4760 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4761 P = "{}";
4762 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4763 if (test_has_schedule(ctx, D, V, P) < 0)
4764 return -1;
4765 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4767 if (test_conditional_schedule_constraints(ctx) < 0)
4768 return -1;
4770 if (test_strongly_satisfying_schedule(ctx) < 0)
4771 return -1;
4773 if (test_conflicting_context_schedule(ctx) < 0)
4774 return -1;
4776 if (test_coalescing_schedule(ctx) < 0)
4777 return -1;
4778 if (test_skewing_schedule(ctx) < 0)
4779 return -1;
4781 return 0;
4784 /* Perform scheduling tests using the whole component scheduler.
4786 static int test_schedule_whole(isl_ctx *ctx)
4788 int whole;
4789 int r;
4791 whole = isl_options_get_schedule_whole_component(ctx);
4792 isl_options_set_schedule_whole_component(ctx, 1);
4793 r = test_schedule(ctx);
4794 isl_options_set_schedule_whole_component(ctx, whole);
4796 return r;
4799 /* Perform scheduling tests using the incremental scheduler.
4801 static int test_schedule_incremental(isl_ctx *ctx)
4803 int whole;
4804 int r;
4806 whole = isl_options_get_schedule_whole_component(ctx);
4807 isl_options_set_schedule_whole_component(ctx, 0);
4808 r = test_schedule(ctx);
4809 isl_options_set_schedule_whole_component(ctx, whole);
4811 return r;
4814 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4816 isl_union_map *umap;
4817 int test;
4819 umap = isl_union_map_read_from_str(ctx, str);
4820 test = isl_union_map_plain_is_injective(umap);
4821 isl_union_map_free(umap);
4822 if (test < 0)
4823 return -1;
4824 if (test == injective)
4825 return 0;
4826 if (injective)
4827 isl_die(ctx, isl_error_unknown,
4828 "map not detected as injective", return -1);
4829 else
4830 isl_die(ctx, isl_error_unknown,
4831 "map detected as injective", return -1);
4834 int test_injective(isl_ctx *ctx)
4836 const char *str;
4838 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4839 return -1;
4840 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4841 return -1;
4842 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4843 return -1;
4844 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4845 return -1;
4846 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4847 return -1;
4848 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4849 return -1;
4850 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4851 return -1;
4852 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4853 return -1;
4855 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4856 if (test_plain_injective(ctx, str, 1))
4857 return -1;
4858 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4859 if (test_plain_injective(ctx, str, 0))
4860 return -1;
4862 return 0;
4865 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4867 isl_aff *aff2;
4868 int equal;
4870 if (!aff)
4871 return -1;
4873 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4874 equal = isl_aff_plain_is_equal(aff, aff2);
4875 isl_aff_free(aff2);
4877 return equal;
4880 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4882 int equal;
4884 equal = aff_plain_is_equal(aff, str);
4885 if (equal < 0)
4886 return -1;
4887 if (!equal)
4888 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4889 "result not as expected", return -1);
4890 return 0;
4893 /* Is "upa" obviously equal to the isl_union_pw_aff represented by "str"?
4895 static isl_bool union_pw_aff_plain_is_equal(__isl_keep isl_union_pw_aff *upa,
4896 const char *str)
4898 isl_ctx *ctx;
4899 isl_union_pw_aff *upa2;
4900 isl_bool equal;
4902 if (!upa)
4903 return isl_bool_error;
4905 ctx = isl_union_pw_aff_get_ctx(upa);
4906 upa2 = isl_union_pw_aff_read_from_str(ctx, str);
4907 equal = isl_union_pw_aff_plain_is_equal(upa, upa2);
4908 isl_union_pw_aff_free(upa2);
4910 return equal;
4913 /* Check that "upa" is obviously equal to the isl_union_pw_aff
4914 * represented by "str".
4916 static isl_stat union_pw_aff_check_plain_equal(__isl_keep isl_union_pw_aff *upa,
4917 const char *str)
4919 isl_bool equal;
4921 equal = union_pw_aff_plain_is_equal(upa, str);
4922 if (equal < 0)
4923 return isl_stat_error;
4924 if (!equal)
4925 isl_die(isl_union_pw_aff_get_ctx(upa), isl_error_unknown,
4926 "result not as expected", return isl_stat_error);
4927 return isl_stat_ok;
4930 /* Basic tests on isl_union_pw_aff.
4932 * In particular, check that isl_union_pw_aff_aff_on_domain
4933 * aligns the parameters of the input objects and
4934 * that isl_union_pw_aff_param_on_domain_id properly
4935 * introduces the parameter.
4937 static int test_upa(isl_ctx *ctx)
4939 const char *str;
4940 isl_id *id;
4941 isl_aff *aff;
4942 isl_union_set *domain;
4943 isl_union_pw_aff *upa;
4944 isl_stat ok;
4946 aff = isl_aff_read_from_str(ctx, "[N] -> { [N] }");
4947 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
4948 domain = isl_union_set_read_from_str(ctx, str);
4949 upa = isl_union_pw_aff_aff_on_domain(domain, aff);
4950 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
4951 ok = union_pw_aff_check_plain_equal(upa, str);
4952 isl_union_pw_aff_free(upa);
4953 if (ok < 0)
4954 return -1;
4956 id = isl_id_alloc(ctx, "N", NULL);
4957 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
4958 domain = isl_union_set_read_from_str(ctx, str);
4959 upa = isl_union_pw_aff_param_on_domain_id(domain, id);
4960 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
4961 ok = union_pw_aff_check_plain_equal(upa, str);
4962 isl_union_pw_aff_free(upa);
4963 if (ok < 0)
4964 return -1;
4966 return 0;
4969 struct {
4970 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4971 __isl_take isl_aff *aff2);
4972 } aff_bin_op[] = {
4973 ['+'] = { &isl_aff_add },
4974 ['-'] = { &isl_aff_sub },
4975 ['*'] = { &isl_aff_mul },
4976 ['/'] = { &isl_aff_div },
4979 struct {
4980 const char *arg1;
4981 unsigned char op;
4982 const char *arg2;
4983 const char *res;
4984 } aff_bin_tests[] = {
4985 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4986 "{ [i] -> [2i] }" },
4987 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4988 "{ [i] -> [0] }" },
4989 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4990 "{ [i] -> [2i] }" },
4991 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4992 "{ [i] -> [2i] }" },
4993 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4994 "{ [i] -> [i/2] }" },
4995 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4996 "{ [i] -> [i] }" },
4997 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4998 "{ [i] -> [NaN] }" },
4999 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
5000 "{ [i] -> [NaN] }" },
5001 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
5002 "{ [i] -> [NaN] }" },
5003 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
5004 "{ [i] -> [NaN] }" },
5005 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
5006 "{ [i] -> [NaN] }" },
5007 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
5008 "{ [i] -> [NaN] }" },
5009 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
5010 "{ [i] -> [NaN] }" },
5011 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
5012 "{ [i] -> [NaN] }" },
5013 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
5014 "{ [i] -> [NaN] }" },
5015 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
5016 "{ [i] -> [NaN] }" },
5017 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
5018 "{ [i] -> [NaN] }" },
5019 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
5020 "{ [i] -> [NaN] }" },
5023 /* Perform some basic tests of binary operations on isl_aff objects.
5025 static int test_bin_aff(isl_ctx *ctx)
5027 int i;
5028 isl_aff *aff1, *aff2, *res;
5029 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5030 __isl_take isl_aff *aff2);
5031 int ok;
5033 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
5034 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
5035 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
5036 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
5037 fn = aff_bin_op[aff_bin_tests[i].op].fn;
5038 aff1 = fn(aff1, aff2);
5039 if (isl_aff_is_nan(res))
5040 ok = isl_aff_is_nan(aff1);
5041 else
5042 ok = isl_aff_plain_is_equal(aff1, res);
5043 isl_aff_free(aff1);
5044 isl_aff_free(res);
5045 if (ok < 0)
5046 return -1;
5047 if (!ok)
5048 isl_die(ctx, isl_error_unknown,
5049 "unexpected result", return -1);
5052 return 0;
5055 struct {
5056 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
5057 __isl_take isl_pw_aff *pa2);
5058 } pw_aff_bin_op[] = {
5059 ['m'] = { &isl_pw_aff_min },
5060 ['M'] = { &isl_pw_aff_max },
5063 /* Inputs for binary isl_pw_aff operation tests.
5064 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
5065 * defined by pw_aff_bin_op, and "res" is the expected result.
5067 struct {
5068 const char *arg1;
5069 unsigned char op;
5070 const char *arg2;
5071 const char *res;
5072 } pw_aff_bin_tests[] = {
5073 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
5074 "{ [i] -> [i] }" },
5075 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
5076 "{ [i] -> [i] }" },
5077 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
5078 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
5079 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
5080 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
5081 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
5082 "{ [i] -> [NaN] }" },
5083 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
5084 "{ [i] -> [NaN] }" },
5087 /* Perform some basic tests of binary operations on isl_pw_aff objects.
5089 static int test_bin_pw_aff(isl_ctx *ctx)
5091 int i;
5092 isl_bool ok;
5093 isl_pw_aff *pa1, *pa2, *res;
5095 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
5096 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
5097 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
5098 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
5099 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
5100 if (isl_pw_aff_involves_nan(res))
5101 ok = isl_pw_aff_involves_nan(pa1);
5102 else
5103 ok = isl_pw_aff_plain_is_equal(pa1, res);
5104 isl_pw_aff_free(pa1);
5105 isl_pw_aff_free(res);
5106 if (ok < 0)
5107 return -1;
5108 if (!ok)
5109 isl_die(ctx, isl_error_unknown,
5110 "unexpected result", return -1);
5113 return 0;
5116 struct {
5117 __isl_give isl_union_pw_multi_aff *(*fn)(
5118 __isl_take isl_union_pw_multi_aff *upma1,
5119 __isl_take isl_union_pw_multi_aff *upma2);
5120 const char *arg1;
5121 const char *arg2;
5122 const char *res;
5123 } upma_bin_tests[] = {
5124 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
5125 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
5126 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
5127 "{ B[x] -> [2] : x >= 0 }",
5128 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
5129 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
5130 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
5131 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
5132 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
5133 "D[i] -> B[2] : i >= 5 }" },
5134 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5135 "{ B[x] -> C[2] : x > 0 }",
5136 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
5137 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5138 "{ B[x] -> A[2] : x >= 0 }",
5139 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
5142 /* Perform some basic tests of binary operations on
5143 * isl_union_pw_multi_aff objects.
5145 static int test_bin_upma(isl_ctx *ctx)
5147 int i;
5148 isl_union_pw_multi_aff *upma1, *upma2, *res;
5149 int ok;
5151 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
5152 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5153 upma_bin_tests[i].arg1);
5154 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5155 upma_bin_tests[i].arg2);
5156 res = isl_union_pw_multi_aff_read_from_str(ctx,
5157 upma_bin_tests[i].res);
5158 upma1 = upma_bin_tests[i].fn(upma1, upma2);
5159 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
5160 isl_union_pw_multi_aff_free(upma1);
5161 isl_union_pw_multi_aff_free(res);
5162 if (ok < 0)
5163 return -1;
5164 if (!ok)
5165 isl_die(ctx, isl_error_unknown,
5166 "unexpected result", return -1);
5169 return 0;
5172 struct {
5173 __isl_give isl_union_pw_multi_aff *(*fn)(
5174 __isl_take isl_union_pw_multi_aff *upma1,
5175 __isl_take isl_union_pw_multi_aff *upma2);
5176 const char *arg1;
5177 const char *arg2;
5178 } upma_bin_fail_tests[] = {
5179 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5180 "{ B[x] -> C[2] : x >= 0 }" },
5183 /* Perform some basic tests of binary operations on
5184 * isl_union_pw_multi_aff objects that are expected to fail.
5186 static int test_bin_upma_fail(isl_ctx *ctx)
5188 int i, n;
5189 isl_union_pw_multi_aff *upma1, *upma2;
5190 int on_error;
5192 on_error = isl_options_get_on_error(ctx);
5193 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
5194 n = ARRAY_SIZE(upma_bin_fail_tests);
5195 for (i = 0; i < n; ++i) {
5196 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5197 upma_bin_fail_tests[i].arg1);
5198 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5199 upma_bin_fail_tests[i].arg2);
5200 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
5201 isl_union_pw_multi_aff_free(upma1);
5202 if (upma1)
5203 break;
5205 isl_options_set_on_error(ctx, on_error);
5206 if (i < n)
5207 isl_die(ctx, isl_error_unknown,
5208 "operation not expected to succeed", return -1);
5210 return 0;
5213 /* Inputs for basic tests of unary operations on isl_multi_pw_aff objects.
5214 * "fn" is the function that is tested.
5215 * "arg" is a string description of the input.
5216 * "res" is a string description of the expected result.
5218 struct {
5219 __isl_give isl_multi_pw_aff *(*fn)(__isl_take isl_multi_pw_aff *mpa);
5220 const char *arg;
5221 const char *res;
5222 } mpa_un_tests[] = {
5223 { &isl_multi_pw_aff_range_factor_domain,
5224 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5225 "{ A[x] -> B[(1 : x >= 5)] }" },
5226 { &isl_multi_pw_aff_range_factor_range,
5227 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5228 "{ A[y] -> C[(2 : y <= 10)] }" },
5229 { &isl_multi_pw_aff_range_factor_domain,
5230 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5231 "{ A[x] -> B[(1 : x >= 5)] }" },
5232 { &isl_multi_pw_aff_range_factor_range,
5233 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5234 "{ A[y] -> C[] }" },
5235 { &isl_multi_pw_aff_range_factor_domain,
5236 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5237 "{ A[x] -> B[] }" },
5238 { &isl_multi_pw_aff_range_factor_range,
5239 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5240 "{ A[y] -> C[(2 : y <= 10)] }" },
5241 { &isl_multi_pw_aff_range_factor_domain,
5242 "{ A[x] -> [B[] -> C[]] }",
5243 "{ A[x] -> B[] }" },
5244 { &isl_multi_pw_aff_range_factor_range,
5245 "{ A[x] -> [B[] -> C[]] }",
5246 "{ A[y] -> C[] }" },
5247 { &isl_multi_pw_aff_factor_range,
5248 "{ [B[] -> C[]] }",
5249 "{ C[] }" },
5250 { &isl_multi_pw_aff_range_factor_domain,
5251 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5252 "{ A[x] -> B[] : x >= 0 }" },
5253 { &isl_multi_pw_aff_range_factor_range,
5254 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5255 "{ A[y] -> C[] : y >= 0 }" },
5256 { &isl_multi_pw_aff_factor_range,
5257 "[N] -> { [B[] -> C[]] : N >= 0 }",
5258 "[N] -> { C[] : N >= 0 }" },
5261 /* Perform some basic tests of unary operations on isl_multi_pw_aff objects.
5263 static int test_un_mpa(isl_ctx *ctx)
5265 int i;
5266 isl_bool ok;
5267 isl_multi_pw_aff *mpa, *res;
5269 for (i = 0; i < ARRAY_SIZE(mpa_un_tests); ++i) {
5270 mpa = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].arg);
5271 res = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].res);
5272 mpa = mpa_un_tests[i].fn(mpa);
5273 ok = isl_multi_pw_aff_plain_is_equal(mpa, res);
5274 isl_multi_pw_aff_free(mpa);
5275 isl_multi_pw_aff_free(res);
5276 if (ok < 0)
5277 return -1;
5278 if (!ok)
5279 isl_die(ctx, isl_error_unknown,
5280 "unexpected result", return -1);
5283 return 0;
5286 /* Inputs for basic tests of binary operations on isl_multi_pw_aff objects.
5287 * "fn" is the function that is tested.
5288 * "arg1" and "arg2" are string descriptions of the inputs.
5289 * "res" is a string description of the expected result.
5291 struct {
5292 __isl_give isl_multi_pw_aff *(*fn)(
5293 __isl_take isl_multi_pw_aff *mpa1,
5294 __isl_take isl_multi_pw_aff *mpa2);
5295 const char *arg1;
5296 const char *arg2;
5297 const char *res;
5298 } mpa_bin_tests[] = {
5299 { &isl_multi_pw_aff_add, "{ A[] -> [1] }", "{ A[] -> [2] }",
5300 "{ A[] -> [3] }" },
5301 { &isl_multi_pw_aff_add, "{ A[x] -> [(1 : x >= 5)] }",
5302 "{ A[x] -> [(x : x <= 10)] }",
5303 "{ A[x] -> [(1 + x : 5 <= x <= 10)] }" },
5304 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5305 "{ A[x] -> [] : x <= 10 }",
5306 "{ A[x] -> [] : 5 <= x <= 10 }" },
5307 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5308 "[N] -> { A[x] -> [] : x <= N }",
5309 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5310 { &isl_multi_pw_aff_add,
5311 "[N] -> { A[x] -> [] : x <= N }",
5312 "{ A[x] -> [] : x >= 5 }",
5313 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5314 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5315 "{ A[y] -> C[(2 : y <= 10)] }",
5316 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5317 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5318 "{ A[y] -> C[2] : y <= 10 }",
5319 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5320 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5321 "[N] -> { A[y] -> C[2] : y <= N }",
5322 "[N] -> { A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= N)]] }" },
5323 { &isl_multi_pw_aff_range_product, "[N] -> { A[x] -> B[1] : x >= N }",
5324 "{ A[y] -> C[2] : y <= 10 }",
5325 "[N] -> { A[x] -> [B[(1 : x >= N)] -> C[(2 : x <= 10)]] }" },
5326 { &isl_multi_pw_aff_range_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5327 "{ A[] -> [B[1] -> C[2]] }" },
5328 { &isl_multi_pw_aff_range_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5329 "{ A[] -> [B[] -> C[]] }" },
5330 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5331 "{ A[y] -> C[] : y <= 10 }",
5332 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] : x <= 10 }" },
5333 { &isl_multi_pw_aff_range_product, "{ A[y] -> C[] : y <= 10 }",
5334 "{ A[x] -> B[(1 : x >= 5)] }",
5335 "{ A[x] -> [C[] -> B[(1 : x >= 5)]] : x <= 10 }" },
5336 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5337 "{ A[y] -> C[(2 : y <= 10)] }",
5338 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[(2 : y <= 10)]] }" },
5339 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5340 "{ A[y] -> C[] : y <= 10 }",
5341 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= 10 }" },
5342 { &isl_multi_pw_aff_product, "{ A[y] -> C[] : y <= 10 }",
5343 "{ A[x] -> B[(1 : x >= 5)] }",
5344 "{ [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= 10 }" },
5345 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5346 "[N] -> { A[y] -> C[] : y <= N }",
5347 "[N] -> { [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= N }" },
5348 { &isl_multi_pw_aff_product, "[N] -> { A[y] -> C[] : y <= N }",
5349 "{ A[x] -> B[(1 : x >= 5)] }",
5350 "[N] -> { [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= N }" },
5351 { &isl_multi_pw_aff_product, "{ A[x] -> B[] : x >= 5 }",
5352 "{ A[y] -> C[] : y <= 10 }",
5353 "{ [A[x] -> A[y]] -> [B[] -> C[]] : x >= 5 and y <= 10 }" },
5354 { &isl_multi_pw_aff_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5355 "{ [A[] -> A[]] -> [B[1] -> C[2]] }" },
5356 { &isl_multi_pw_aff_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5357 "{ [A[] -> A[]] -> [B[] -> C[]] }" },
5358 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5359 "{ B[i,j] -> C[i + 2j] }", "{ A[a,b] -> B[b,a] }",
5360 "{ A[a,b] -> C[b + 2a] }" },
5361 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5362 "{ B[i,j] -> C[i + 2j] }",
5363 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5364 "{ A[a,b] -> C[(b + 2a : b > a)] }" },
5365 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5366 "{ B[i,j] -> C[(i + 2j : j > 4)] }",
5367 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5368 "{ A[a,b] -> C[(b + 2a : b > a > 4)] }" },
5369 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5370 "{ B[i,j] -> C[] }",
5371 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5372 "{ A[a,b] -> C[] }" },
5373 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5374 "{ B[i,j] -> C[] : i > j }",
5375 "{ A[a,b] -> B[b,a] }",
5376 "{ A[a,b] -> C[] : b > a }" },
5377 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5378 "{ B[i,j] -> C[] : j > 5 }",
5379 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5380 "{ A[a,b] -> C[] : b > a > 5 }" },
5381 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5382 "[N] -> { B[i,j] -> C[] : j > N }",
5383 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5384 "[N] -> { A[a,b] -> C[] : b > a > N }" },
5385 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5386 "[M,N] -> { B[] -> C[] : N > 5 }",
5387 "[M,N] -> { A[] -> B[] : M > N }",
5388 "[M,N] -> { A[] -> C[] : M > N > 5 }" },
5391 /* Perform some basic tests of binary operations on isl_multi_pw_aff objects.
5393 static int test_bin_mpa(isl_ctx *ctx)
5395 int i;
5396 isl_bool ok;
5397 isl_multi_pw_aff *mpa1, *mpa2, *res;
5399 for (i = 0; i < ARRAY_SIZE(mpa_bin_tests); ++i) {
5400 mpa1 = isl_multi_pw_aff_read_from_str(ctx,
5401 mpa_bin_tests[i].arg1);
5402 mpa2 = isl_multi_pw_aff_read_from_str(ctx,
5403 mpa_bin_tests[i].arg2);
5404 res = isl_multi_pw_aff_read_from_str(ctx,
5405 mpa_bin_tests[i].res);
5406 mpa1 = mpa_bin_tests[i].fn(mpa1, mpa2);
5407 ok = isl_multi_pw_aff_plain_is_equal(mpa1, res);
5408 isl_multi_pw_aff_free(mpa1);
5409 isl_multi_pw_aff_free(res);
5410 if (ok < 0)
5411 return -1;
5412 if (!ok)
5413 isl_die(ctx, isl_error_unknown,
5414 "unexpected result", return -1);
5417 return 0;
5420 /* Inputs for basic tests of unary operations on
5421 * isl_multi_union_pw_aff objects.
5422 * "fn" is the function that is tested.
5423 * "arg" is a string description of the input.
5424 * "res" is a string description of the expected result.
5426 struct {
5427 __isl_give isl_multi_union_pw_aff *(*fn)(
5428 __isl_take isl_multi_union_pw_aff *mupa);
5429 const char *arg;
5430 const char *res;
5431 } mupa_un_tests[] = {
5432 { &isl_multi_union_pw_aff_factor_range,
5433 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]",
5434 "C[{ A[] -> [2] }]" },
5435 { &isl_multi_union_pw_aff_factor_range,
5436 "[B[] -> C[{ A[] -> [2] }]]",
5437 "C[{ A[] -> [2] }]" },
5438 { &isl_multi_union_pw_aff_factor_range,
5439 "[B[{ A[] -> [1] }] -> C[]]",
5440 "C[]" },
5441 { &isl_multi_union_pw_aff_factor_range,
5442 "[B[] -> C[]]",
5443 "C[]" },
5444 { &isl_multi_union_pw_aff_factor_range,
5445 "([B[] -> C[]] : { A[x] : x >= 0 })",
5446 "(C[] : { A[x] : x >= 0 })" },
5447 { &isl_multi_union_pw_aff_factor_range,
5448 "[N] -> ([B[] -> C[]] : { A[x] : x <= N })",
5449 "[N] -> (C[] : { A[x] : x <= N })" },
5450 { &isl_multi_union_pw_aff_factor_range,
5451 "[N] -> ([B[] -> C[]] : { : N >= 0 })",
5452 "[N] -> (C[] : { : N >= 0 })" },
5455 /* Perform some basic tests of unary operations on
5456 * isl_multi_union_pw_aff objects.
5458 static int test_un_mupa(isl_ctx *ctx)
5460 int i;
5461 isl_bool ok;
5462 isl_multi_union_pw_aff *mupa, *res;
5464 for (i = 0; i < ARRAY_SIZE(mupa_un_tests); ++i) {
5465 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5466 mupa_un_tests[i].arg);
5467 res = isl_multi_union_pw_aff_read_from_str(ctx,
5468 mupa_un_tests[i].res);
5469 mupa = mupa_un_tests[i].fn(mupa);
5470 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5471 isl_multi_union_pw_aff_free(mupa);
5472 isl_multi_union_pw_aff_free(res);
5473 if (ok < 0)
5474 return -1;
5475 if (!ok)
5476 isl_die(ctx, isl_error_unknown,
5477 "unexpected result", return -1);
5480 return 0;
5483 /* Inputs for basic tests of binary operations on
5484 * isl_multi_union_pw_aff objects.
5485 * "fn" is the function that is tested.
5486 * "arg1" and "arg2" are string descriptions of the inputs.
5487 * "res" is a string description of the expected result.
5489 struct {
5490 __isl_give isl_multi_union_pw_aff *(*fn)(
5491 __isl_take isl_multi_union_pw_aff *mupa1,
5492 __isl_take isl_multi_union_pw_aff *mupa2);
5493 const char *arg1;
5494 const char *arg2;
5495 const char *res;
5496 } mupa_bin_tests[] = {
5497 { &isl_multi_union_pw_aff_add, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5498 "[{ A[] -> [3] }]" },
5499 { &isl_multi_union_pw_aff_sub, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5500 "[{ A[] -> [-1] }]" },
5501 { &isl_multi_union_pw_aff_add,
5502 "[{ A[] -> [1]; B[] -> [4] }]",
5503 "[{ A[] -> [2]; C[] -> [5] }]",
5504 "[{ A[] -> [3] }]" },
5505 { &isl_multi_union_pw_aff_union_add,
5506 "[{ A[] -> [1]; B[] -> [4] }]",
5507 "[{ A[] -> [2]; C[] -> [5] }]",
5508 "[{ A[] -> [3]; B[] -> [4]; C[] -> [5] }]" },
5509 { &isl_multi_union_pw_aff_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5510 "[{ A[x] -> [(x)] : x <= 10 }]",
5511 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10 }]" },
5512 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5513 "([] : { A[x] : x <= 10 })",
5514 "([] : { A[x] : 5 <= x <= 10 })" },
5515 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5516 "[N] -> ([] : { A[x] : x <= N })",
5517 "[N] -> ([] : { A[x] : 5 <= x <= N })" },
5518 { &isl_multi_union_pw_aff_add, "[N] -> ([] : { A[x] : x >= N })",
5519 "([] : { A[x] : x <= 10 })",
5520 "[N] -> ([] : { A[x] : N <= x <= 10 })" },
5521 { &isl_multi_union_pw_aff_union_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5522 "[{ A[x] -> [(x)] : x <= 10 }]",
5523 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10; "
5524 "A[x] -> [(1)] : x > 10; A[x] -> [(x)] : x < 5 }]" },
5525 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 5 })",
5526 "([] : { A[x] : x <= 10 })",
5527 "([] : { A[x] })" },
5528 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 0 })",
5529 "[N] -> ([] : { A[x] : x >= N })",
5530 "[N] -> ([] : { A[x] : x >= 0 or x >= N })" },
5531 { &isl_multi_union_pw_aff_union_add,
5532 "[N] -> ([] : { A[] : N >= 0})",
5533 "[N] -> ([] : { A[] : N <= 0})",
5534 "[N] -> ([] : { A[] })" },
5535 { &isl_multi_union_pw_aff_union_add,
5536 "[N] -> ([] : { A[] })",
5537 "[N] -> ([] : { : })",
5538 "[N] -> ([] : { : })" },
5539 { &isl_multi_union_pw_aff_union_add,
5540 "[N] -> ([] : { : })",
5541 "[N] -> ([] : { A[] })",
5542 "[N] -> ([] : { : })" },
5543 { &isl_multi_union_pw_aff_union_add,
5544 "[N] -> ([] : { : N >= 0})",
5545 "[N] -> ([] : { : N <= 0})",
5546 "[N] -> ([] : { : })" },
5547 { &isl_multi_union_pw_aff_range_product,
5548 "B[{ A[] -> [1] }]",
5549 "C[{ A[] -> [2] }]",
5550 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]" },
5551 { &isl_multi_union_pw_aff_range_product,
5552 "(B[] : { A[x] : x >= 5 })",
5553 "(C[] : { A[x] : x <= 10 })",
5554 "([B[] -> C[]] : { A[x] : 5 <= x <= 10 })" },
5555 { &isl_multi_union_pw_aff_range_product,
5556 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5557 "(C[] : { A[x] : x <= 10 })",
5558 "[B[{ A[x] -> [x + 1] : 5 <= x <= 10 }] -> C[]]" },
5559 { &isl_multi_union_pw_aff_range_product,
5560 "(C[] : { A[x] : x <= 10 })",
5561 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5562 "[C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= 10 }]]" },
5563 { &isl_multi_union_pw_aff_range_product,
5564 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5565 "[N] -> (C[] : { A[x] : x <= N })",
5566 "[N] -> [B[{ A[x] -> [x + 1] : 5 <= x <= N }] -> C[]]" },
5567 { &isl_multi_union_pw_aff_range_product,
5568 "[N] -> (C[] : { A[x] : x <= N })",
5569 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5570 "[N] -> [C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= N }]]" },
5571 { &isl_multi_union_pw_aff_range_product,
5572 "B[{ A[] -> [1]; D[] -> [3] }]",
5573 "C[{ A[] -> [2] }]",
5574 "[B[{ A[] -> [1]; D[] -> [3] }] -> C[{ A[] -> [2] }]]" },
5575 { &isl_multi_union_pw_aff_range_product,
5576 "B[] }]",
5577 "(C[] : { A[x] })",
5578 "([B[] -> C[]] : { A[x] })" },
5579 { &isl_multi_union_pw_aff_range_product,
5580 "(B[] : { A[x] })",
5581 "C[] }]",
5582 "([B[] -> C[]] : { A[x] })" },
5585 /* Perform some basic tests of binary operations on
5586 * isl_multi_union_pw_aff objects.
5588 static int test_bin_mupa(isl_ctx *ctx)
5590 int i;
5591 isl_bool ok;
5592 isl_multi_union_pw_aff *mupa1, *mupa2, *res;
5594 for (i = 0; i < ARRAY_SIZE(mupa_bin_tests); ++i) {
5595 mupa1 = isl_multi_union_pw_aff_read_from_str(ctx,
5596 mupa_bin_tests[i].arg1);
5597 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx,
5598 mupa_bin_tests[i].arg2);
5599 res = isl_multi_union_pw_aff_read_from_str(ctx,
5600 mupa_bin_tests[i].res);
5601 mupa1 = mupa_bin_tests[i].fn(mupa1, mupa2);
5602 ok = isl_multi_union_pw_aff_plain_is_equal(mupa1, res);
5603 isl_multi_union_pw_aff_free(mupa1);
5604 isl_multi_union_pw_aff_free(res);
5605 if (ok < 0)
5606 return -1;
5607 if (!ok)
5608 isl_die(ctx, isl_error_unknown,
5609 "unexpected result", return -1);
5612 return 0;
5615 /* Inputs for basic tests of binary operations on
5616 * pairs of isl_multi_union_pw_aff and isl_set objects.
5617 * "fn" is the function that is tested.
5618 * "arg1" and "arg2" are string descriptions of the inputs.
5619 * "res" is a string description of the expected result.
5621 struct {
5622 __isl_give isl_multi_union_pw_aff *(*fn)(
5623 __isl_take isl_multi_union_pw_aff *mupa,
5624 __isl_take isl_set *set);
5625 const char *arg1;
5626 const char *arg2;
5627 const char *res;
5628 } mupa_set_tests[] = {
5629 { &isl_multi_union_pw_aff_intersect_range,
5630 "C[{ B[i,j] -> [i + 2j] }]", "{ C[1] }",
5631 "C[{ B[i,j] -> [i + 2j] : i + 2j = 1 }]" },
5632 { &isl_multi_union_pw_aff_intersect_range,
5633 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[N] }",
5634 "[N] -> C[{ B[i,j] -> [i + 2j] : i + 2j = N }]" },
5635 { &isl_multi_union_pw_aff_intersect_range,
5636 "[N] -> C[{ B[i,j] -> [i + 2j + N] }]", "{ C[1] }",
5637 "[N] -> C[{ B[i,j] -> [i + 2j + N] : i + 2j + N = 1 }]" },
5638 { &isl_multi_union_pw_aff_intersect_range,
5639 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[x] : N >= 0 }",
5640 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0 }]" },
5641 { &isl_multi_union_pw_aff_intersect_range,
5642 "C[]", "{ C[] }", "C[]" },
5643 { &isl_multi_union_pw_aff_intersect_range,
5644 "[N] -> (C[] : { : N >= 0 })",
5645 "{ C[] }",
5646 "[N] -> (C[] : { : N >= 0 })" },
5647 { &isl_multi_union_pw_aff_intersect_range,
5648 "(C[] : { A[a,b] })",
5649 "{ C[] }",
5650 "(C[] : { A[a,b] })" },
5651 { &isl_multi_union_pw_aff_intersect_range,
5652 "[N] -> (C[] : { A[a,b] : a,b <= N })",
5653 "{ C[] }",
5654 "[N] -> (C[] : { A[a,b] : a,b <= N })" },
5655 { &isl_multi_union_pw_aff_intersect_range,
5656 "C[]",
5657 "[N] -> { C[] : N >= 0 }",
5658 "[N] -> (C[] : { : N >= 0 })" },
5659 { &isl_multi_union_pw_aff_intersect_range,
5660 "(C[] : { A[a,b] })",
5661 "[N] -> { C[] : N >= 0 }",
5662 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5663 { &isl_multi_union_pw_aff_intersect_range,
5664 "[N] -> (C[] : { : N >= 0 })",
5665 "[N] -> { C[] : N < 1024 }",
5666 "[N] -> (C[] : { : 0 <= N < 1024 })" },
5667 { &isl_multi_union_pw_aff_intersect_params,
5668 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { : N >= 0 }",
5669 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0}]" },
5670 { &isl_multi_union_pw_aff_intersect_params,
5671 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "[N] -> { : N >= 0 }",
5672 "[N] -> C[{ B[i,j] -> [i + 2j] : 0 <= N <= 256 }]" },
5673 { &isl_multi_union_pw_aff_intersect_params,
5674 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "{ : }",
5675 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]" },
5676 { &isl_multi_union_pw_aff_intersect_params,
5677 "C[]", "[N] -> { : N >= 0 }",
5678 "[N] -> (C[] : { : N >= 0 })" },
5679 { &isl_multi_union_pw_aff_intersect_params,
5680 "(C[] : { A[a,b] })", "[N] -> { : N >= 0 }",
5681 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5682 { &isl_multi_union_pw_aff_intersect_params,
5683 "[N] -> (C[] : { A[a,N] })", "{ : }",
5684 "[N] -> (C[] : { A[a,N] })" },
5685 { &isl_multi_union_pw_aff_intersect_params,
5686 "[N] -> (C[] : { A[a,b] : N <= 256 })", "[N] -> { : N >= 0 }",
5687 "[N] -> (C[] : { A[a,b] : 0 <= N <= 256 })" },
5690 /* Perform some basic tests of binary operations on
5691 * pairs of isl_multi_union_pw_aff and isl_set objects.
5693 static int test_mupa_set(isl_ctx *ctx)
5695 int i;
5696 isl_bool ok;
5697 isl_multi_union_pw_aff *mupa, *res;
5698 isl_set *set;
5700 for (i = 0; i < ARRAY_SIZE(mupa_set_tests); ++i) {
5701 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5702 mupa_set_tests[i].arg1);
5703 set = isl_set_read_from_str(ctx, mupa_set_tests[i].arg2);
5704 res = isl_multi_union_pw_aff_read_from_str(ctx,
5705 mupa_set_tests[i].res);
5706 mupa = mupa_set_tests[i].fn(mupa, set);
5707 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5708 isl_multi_union_pw_aff_free(mupa);
5709 isl_multi_union_pw_aff_free(res);
5710 if (ok < 0)
5711 return -1;
5712 if (!ok)
5713 isl_die(ctx, isl_error_unknown,
5714 "unexpected result", return -1);
5717 return 0;
5720 /* Inputs for basic tests of binary operations on
5721 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5722 * "fn" is the function that is tested.
5723 * "arg1" and "arg2" are string descriptions of the inputs.
5724 * "res" is a string description of the expected result.
5726 struct {
5727 __isl_give isl_multi_union_pw_aff *(*fn)(
5728 __isl_take isl_multi_union_pw_aff *mupa,
5729 __isl_take isl_union_set *uset);
5730 const char *arg1;
5731 const char *arg2;
5732 const char *res;
5733 } mupa_uset_tests[] = {
5734 { &isl_multi_union_pw_aff_intersect_domain,
5735 "C[{ B[i,j] -> [i + 2j] }]", "{ B[i,i] }",
5736 "C[{ B[i,i] -> [3i] }]" },
5737 { &isl_multi_union_pw_aff_intersect_domain,
5738 "(C[] : { B[i,j] })", "{ B[i,i] }",
5739 "(C[] : { B[i,i] })" },
5740 { &isl_multi_union_pw_aff_intersect_domain,
5741 "(C[] : { B[i,j] })", "[N] -> { B[N,N] }",
5742 "[N] -> (C[] : { B[N,N] })" },
5743 { &isl_multi_union_pw_aff_intersect_domain,
5744 "C[]", "{ B[i,i] }",
5745 "(C[] : { B[i,i] })" },
5746 { &isl_multi_union_pw_aff_intersect_domain,
5747 "[N] -> (C[] : { : N >= 0 })", "{ B[i,i] }",
5748 "[N] -> (C[] : { B[i,i] : N >= 0 })" },
5751 /* Perform some basic tests of binary operations on
5752 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5754 static int test_mupa_uset(isl_ctx *ctx)
5756 int i;
5757 isl_bool ok;
5758 isl_multi_union_pw_aff *mupa, *res;
5759 isl_union_set *uset;
5761 for (i = 0; i < ARRAY_SIZE(mupa_uset_tests); ++i) {
5762 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5763 mupa_uset_tests[i].arg1);
5764 uset = isl_union_set_read_from_str(ctx,
5765 mupa_uset_tests[i].arg2);
5766 res = isl_multi_union_pw_aff_read_from_str(ctx,
5767 mupa_uset_tests[i].res);
5768 mupa = mupa_uset_tests[i].fn(mupa, uset);
5769 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5770 isl_multi_union_pw_aff_free(mupa);
5771 isl_multi_union_pw_aff_free(res);
5772 if (ok < 0)
5773 return -1;
5774 if (!ok)
5775 isl_die(ctx, isl_error_unknown,
5776 "unexpected result", return -1);
5779 return 0;
5782 /* Inputs for basic tests of binary operations on
5783 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5784 * "fn" is the function that is tested.
5785 * "arg1" and "arg2" are string descriptions of the inputs.
5786 * "res" is a string description of the expected result.
5788 struct {
5789 __isl_give isl_multi_union_pw_aff *(*fn)(
5790 __isl_take isl_multi_union_pw_aff *mupa,
5791 __isl_take isl_multi_aff *ma);
5792 const char *arg1;
5793 const char *arg2;
5794 const char *res;
5795 } mupa_ma_tests[] = {
5796 { &isl_multi_union_pw_aff_apply_multi_aff,
5797 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5798 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5799 "{ C[a,b] -> D[b,a] }",
5800 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
5801 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
5802 { &isl_multi_union_pw_aff_apply_multi_aff,
5803 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
5804 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5805 "{ C[a,b] -> D[b,a] }",
5806 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
5807 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
5808 { &isl_multi_union_pw_aff_apply_multi_aff,
5809 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5810 "[N] -> { C[a] -> D[a + N] }",
5811 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }] " },
5812 { &isl_multi_union_pw_aff_apply_multi_aff,
5813 "C[]",
5814 "{ C[] -> D[] }",
5815 "D[]" },
5816 { &isl_multi_union_pw_aff_apply_multi_aff,
5817 "[N] -> (C[] : { : N >= 0 })",
5818 "{ C[] -> D[] }",
5819 "[N] -> (D[] : { : N >= 0 })" },
5820 { &isl_multi_union_pw_aff_apply_multi_aff,
5821 "C[]",
5822 "[N] -> { C[] -> D[N] }",
5823 "[N] -> D[{ [N] }]" },
5824 { &isl_multi_union_pw_aff_apply_multi_aff,
5825 "(C[] : { A[i,j] : i >= j })",
5826 "{ C[] -> D[] }",
5827 "(D[] : { A[i,j] : i >= j })" },
5828 { &isl_multi_union_pw_aff_apply_multi_aff,
5829 "[N] -> (C[] : { A[i,j] : N >= 0 })",
5830 "{ C[] -> D[] }",
5831 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
5832 { &isl_multi_union_pw_aff_apply_multi_aff,
5833 "(C[] : { A[i,j] : i >= j })",
5834 "[N] -> { C[] -> D[N] }",
5835 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
5838 /* Perform some basic tests of binary operations on
5839 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5841 static int test_mupa_ma(isl_ctx *ctx)
5843 int i;
5844 isl_bool ok;
5845 isl_multi_union_pw_aff *mupa, *res;
5846 isl_multi_aff *ma;
5848 for (i = 0; i < ARRAY_SIZE(mupa_ma_tests); ++i) {
5849 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5850 mupa_ma_tests[i].arg1);
5851 ma = isl_multi_aff_read_from_str(ctx, mupa_ma_tests[i].arg2);
5852 res = isl_multi_union_pw_aff_read_from_str(ctx,
5853 mupa_ma_tests[i].res);
5854 mupa = mupa_ma_tests[i].fn(mupa, ma);
5855 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5856 isl_multi_union_pw_aff_free(mupa);
5857 isl_multi_union_pw_aff_free(res);
5858 if (ok < 0)
5859 return -1;
5860 if (!ok)
5861 isl_die(ctx, isl_error_unknown,
5862 "unexpected result", return -1);
5865 return 0;
5868 /* Inputs for basic tests of binary operations on
5869 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5870 * "fn" is the function that is tested.
5871 * "arg1" and "arg2" are string descriptions of the inputs.
5872 * "res" is a string description of the expected result.
5874 struct {
5875 __isl_give isl_union_pw_aff *(*fn)(
5876 __isl_take isl_multi_union_pw_aff *mupa,
5877 __isl_take isl_pw_aff *pa);
5878 const char *arg1;
5879 const char *arg2;
5880 const char *res;
5881 } mupa_pa_tests[] = {
5882 { &isl_multi_union_pw_aff_apply_pw_aff,
5883 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5884 "[N] -> { C[a] -> [a + N] }",
5885 "[N] -> { A[i,j] -> [i + N]; B[i,j] -> [j + N] }" },
5886 { &isl_multi_union_pw_aff_apply_pw_aff,
5887 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5888 "{ C[a] -> [a] : a >= 0; C[a] -> [-a] : a < 0 }",
5889 "{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
5890 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }" },
5891 { &isl_multi_union_pw_aff_apply_pw_aff,
5892 "C[]",
5893 "[N] -> { C[] -> [N] }",
5894 "[N] -> { [N] }" },
5895 { &isl_multi_union_pw_aff_apply_pw_aff,
5896 "C[]",
5897 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
5898 "[N] -> { [N] : N >= 0; [-N] : N < 0 }" },
5899 { &isl_multi_union_pw_aff_apply_pw_aff,
5900 "[N] -> (C[] : { : N >= 0 })",
5901 "[N] -> { C[] -> [N] }",
5902 "[N] -> { [N] : N >= 0 }" },
5903 { &isl_multi_union_pw_aff_apply_pw_aff,
5904 "[N] -> (C[] : { : N >= 0 })",
5905 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
5906 "[N] -> { [N] : N >= 0 }" },
5907 { &isl_multi_union_pw_aff_apply_pw_aff,
5908 "[N] -> (C[] : { : N >= 0 })",
5909 "{ C[] -> [0] }",
5910 "[N] -> { [0] : N >= 0 }" },
5911 { &isl_multi_union_pw_aff_apply_pw_aff,
5912 "(C[] : { A[i,j] : i >= j })",
5913 "[N] -> { C[] -> [N] }",
5914 "[N] -> { A[i,j] -> [N] : i >= j }" },
5915 { &isl_multi_union_pw_aff_apply_pw_aff,
5916 "(C[] : { A[i,j] : i >= j })",
5917 "[N] -> { C[] -> [N] : N >= 0 }",
5918 "[N] -> { A[i,j] -> [N] : i >= j and N >= 0 }" },
5921 /* Perform some basic tests of binary operations on
5922 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5924 static int test_mupa_pa(isl_ctx *ctx)
5926 int i;
5927 isl_bool ok;
5928 isl_multi_union_pw_aff *mupa;
5929 isl_union_pw_aff *upa, *res;
5930 isl_pw_aff *pa;
5932 for (i = 0; i < ARRAY_SIZE(mupa_pa_tests); ++i) {
5933 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5934 mupa_pa_tests[i].arg1);
5935 pa = isl_pw_aff_read_from_str(ctx, mupa_pa_tests[i].arg2);
5936 res = isl_union_pw_aff_read_from_str(ctx,
5937 mupa_pa_tests[i].res);
5938 upa = mupa_pa_tests[i].fn(mupa, pa);
5939 ok = isl_union_pw_aff_plain_is_equal(upa, res);
5940 isl_union_pw_aff_free(upa);
5941 isl_union_pw_aff_free(res);
5942 if (ok < 0)
5943 return -1;
5944 if (!ok)
5945 isl_die(ctx, isl_error_unknown,
5946 "unexpected result", return -1);
5949 return 0;
5952 /* Inputs for basic tests of binary operations on
5953 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
5954 * "fn" is the function that is tested.
5955 * "arg1" and "arg2" are string descriptions of the inputs.
5956 * "res" is a string description of the expected result.
5958 struct {
5959 __isl_give isl_multi_union_pw_aff *(*fn)(
5960 __isl_take isl_multi_union_pw_aff *mupa,
5961 __isl_take isl_pw_multi_aff *pma);
5962 const char *arg1;
5963 const char *arg2;
5964 const char *res;
5965 } mupa_pma_tests[] = {
5966 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5967 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5968 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5969 "{ C[a,b] -> D[b,a] }",
5970 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
5971 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
5972 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5973 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
5974 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5975 "{ C[a,b] -> D[b,a] }",
5976 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
5977 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
5978 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5979 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5980 "[N] -> { C[a] -> D[a + N] }",
5981 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }]" },
5982 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5983 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5984 "{ C[a] -> D[a] : a >= 0; C[a] -> D[-a] : a < 0 }",
5985 "D[{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
5986 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }]" },
5987 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5988 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5989 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5990 "{ C[a,b] -> D[a,b] : a >= b; C[a,b] -> D[b,a] : a < b }",
5991 "D[{ A[i,j] -> [i] : i >= j; A[i,j] -> [j] : i < j; "
5992 "B[i,j] -> [j] : i <= j; B[i,j] -> [i] : i > j }, "
5993 "{ A[i,j] -> [j] : i >= j; A[i,j] -> [i] : i < j; "
5994 "B[i,j] -> [i] : i <= j; B[i,j] -> [j] : i > j }]" },
5995 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5996 "C[]",
5997 "{ C[] -> D[] }",
5998 "D[]" },
5999 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6000 "[N] -> (C[] : { : N >= 0 })",
6001 "{ C[] -> D[] }",
6002 "[N] -> (D[] : { : N >= 0 })" },
6003 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6004 "C[]",
6005 "[N] -> { C[] -> D[N] }",
6006 "[N] -> D[{ [N] }]" },
6007 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6008 "(C[] : { A[i,j] : i >= j })",
6009 "{ C[] -> D[] }",
6010 "(D[] : { A[i,j] : i >= j })" },
6011 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6012 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6013 "{ C[] -> D[] }",
6014 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6015 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6016 "(C[] : { A[i,j] : i >= j })",
6017 "[N] -> { C[] -> D[N] }",
6018 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6019 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6020 "C[]",
6021 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6022 "[N] -> D[{ [N] : N >= 0; [-N] : N < 0 }]" },
6023 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6024 "[N] -> (C[] : { : N >= 0 })",
6025 "[N] -> { C[] -> D[N] }",
6026 "[N] -> D[{ [N] : N >= 0 }]" },
6027 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6028 "[N] -> (C[] : { : N >= 0 })",
6029 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6030 "[N] -> D[{ [N] : N >= 0 }]" },
6031 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6032 "[N] -> (C[] : { : N >= 0 })",
6033 "{ C[] -> D[0] }",
6034 "[N] -> D[{ [0] : N >= 0 }]" },
6035 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6036 "(C[] : { A[i,j] : i >= j })",
6037 "[N] -> { C[] -> D[N] : N >= 0 }",
6038 "[N] -> D[{ A[i,j] -> [N] : i >= j and N >= 0 }]" },
6041 /* Perform some basic tests of binary operations on
6042 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6044 static int test_mupa_pma(isl_ctx *ctx)
6046 int i;
6047 isl_bool ok;
6048 isl_multi_union_pw_aff *mupa, *res;
6049 isl_pw_multi_aff *pma;
6051 for (i = 0; i < ARRAY_SIZE(mupa_pma_tests); ++i) {
6052 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6053 mupa_pma_tests[i].arg1);
6054 pma = isl_pw_multi_aff_read_from_str(ctx,
6055 mupa_pma_tests[i].arg2);
6056 res = isl_multi_union_pw_aff_read_from_str(ctx,
6057 mupa_pma_tests[i].res);
6058 mupa = mupa_pma_tests[i].fn(mupa, pma);
6059 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6060 isl_multi_union_pw_aff_free(mupa);
6061 isl_multi_union_pw_aff_free(res);
6062 if (ok < 0)
6063 return -1;
6064 if (!ok)
6065 isl_die(ctx, isl_error_unknown,
6066 "unexpected result", return -1);
6069 return 0;
6072 /* Inputs for basic tests of binary operations on
6073 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6074 * "fn" is the function that is tested.
6075 * "arg1" and "arg2" are string descriptions of the inputs.
6076 * "res" is a string description of the expected result.
6078 struct {
6079 __isl_give isl_multi_union_pw_aff *(*fn)(
6080 __isl_take isl_multi_union_pw_aff *mupa,
6081 __isl_take isl_union_pw_multi_aff *upma);
6082 const char *arg1;
6083 const char *arg2;
6084 const char *res;
6085 } mupa_upma_tests[] = {
6086 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6087 "C[{ B[i,j] -> [i + 2j] }]", "{ A[a,b] -> B[b,a] }",
6088 "C[{ A[a,b] -> [b + 2a] }]" },
6089 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6090 "C[{ B[i,j] -> [i + 2j] }]",
6091 "{ A[a,b] -> B[b,a] : b > a }",
6092 "C[{ A[a,b] -> [b + 2a] : b > a }]" },
6093 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6094 "C[{ B[i,j] -> [i + 2j] : j > 4 }]",
6095 "{ A[a,b] -> B[b,a] : b > a }",
6096 "C[{ A[a,b] -> [b + 2a] : b > a > 4 }]" },
6097 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6098 "C[{ B[i,j] -> [i + 2j] }]",
6099 "{ A[a,b] -> B[b,a] : a > b; A[a,b] -> B[a,b] : a <= b }",
6100 "C[{ A[a,b] -> [b + 2a] : a > b; A[a,b] -> [a + 2b] : a <= b }]" },
6101 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6102 "(C[] : { B[a,b] })",
6103 "{ A[a,b] -> B[b,a] }",
6104 "(C[] : { A[a,b] })" },
6105 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6106 "(C[] : { B[a,b] })",
6107 "{ B[a,b] -> A[b,a] }",
6108 "(C[] : { })" },
6109 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6110 "(C[] : { B[a,b] })",
6111 "{ A[a,b] -> B[b,a] : a > b }",
6112 "(C[] : { A[a,b] : a > b })" },
6113 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6114 "(C[] : { B[a,b] : a > b })",
6115 "{ A[a,b] -> B[b,a] }",
6116 "(C[] : { A[a,b] : b > a })" },
6117 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6118 "[N] -> (C[] : { B[a,b] : a > N })",
6119 "{ A[a,b] -> B[b,a] : a > b }",
6120 "[N] -> (C[] : { A[a,b] : a > b > N })" },
6121 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6122 "(C[] : { B[a,b] : a > b })",
6123 "[N] -> { A[a,b] -> B[b,a] : a > N }",
6124 "[N] -> (C[] : { A[a,b] : b > a > N })" },
6125 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6126 "C[]",
6127 "{ A[a,b] -> B[b,a] }",
6128 "C[]" },
6129 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6130 "[N] -> (C[] : { : N >= 0 })",
6131 "{ A[a,b] -> B[b,a] }",
6132 "[N] -> (C[] : { : N >= 0 })" },
6133 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6134 "C[]",
6135 "[N] -> { A[a,b] -> B[b,a] : N >= 0 }",
6136 "[N] -> (C[] : { : N >= 0 })" },
6139 /* Perform some basic tests of binary operations on
6140 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6142 static int test_mupa_upma(isl_ctx *ctx)
6144 int i;
6145 isl_bool ok;
6146 isl_multi_union_pw_aff *mupa, *res;
6147 isl_union_pw_multi_aff *upma;
6149 for (i = 0; i < ARRAY_SIZE(mupa_upma_tests); ++i) {
6150 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6151 mupa_upma_tests[i].arg1);
6152 upma = isl_union_pw_multi_aff_read_from_str(ctx,
6153 mupa_upma_tests[i].arg2);
6154 res = isl_multi_union_pw_aff_read_from_str(ctx,
6155 mupa_upma_tests[i].res);
6156 mupa = mupa_upma_tests[i].fn(mupa, upma);
6157 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6158 isl_multi_union_pw_aff_free(mupa);
6159 isl_multi_union_pw_aff_free(res);
6160 if (ok < 0)
6161 return -1;
6162 if (!ok)
6163 isl_die(ctx, isl_error_unknown,
6164 "unexpected result", return -1);
6167 return 0;
6170 /* Check that the input tuple of an isl_aff can be set properly.
6172 static isl_stat test_aff_set_tuple_id(isl_ctx *ctx)
6174 isl_id *id;
6175 isl_aff *aff;
6176 int equal;
6178 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x + 1] }");
6179 id = isl_id_alloc(ctx, "A", NULL);
6180 aff = isl_aff_set_tuple_id(aff, isl_dim_in, id);
6181 equal = aff_check_plain_equal(aff, "{ A[x] -> [x + 1] }");
6182 isl_aff_free(aff);
6183 if (equal < 0)
6184 return isl_stat_error;
6186 return isl_stat_ok;
6189 int test_aff(isl_ctx *ctx)
6191 const char *str;
6192 isl_set *set;
6193 isl_space *space;
6194 isl_local_space *ls;
6195 isl_aff *aff;
6196 int zero, equal;
6198 if (test_upa(ctx) < 0)
6199 return -1;
6200 if (test_bin_aff(ctx) < 0)
6201 return -1;
6202 if (test_bin_pw_aff(ctx) < 0)
6203 return -1;
6204 if (test_bin_upma(ctx) < 0)
6205 return -1;
6206 if (test_bin_upma_fail(ctx) < 0)
6207 return -1;
6208 if (test_un_mpa(ctx) < 0)
6209 return -1;
6210 if (test_bin_mpa(ctx) < 0)
6211 return -1;
6212 if (test_un_mupa(ctx) < 0)
6213 return -1;
6214 if (test_bin_mupa(ctx) < 0)
6215 return -1;
6216 if (test_mupa_set(ctx) < 0)
6217 return -1;
6218 if (test_mupa_uset(ctx) < 0)
6219 return -1;
6220 if (test_mupa_ma(ctx) < 0)
6221 return -1;
6222 if (test_mupa_pa(ctx) < 0)
6223 return -1;
6224 if (test_mupa_pma(ctx) < 0)
6225 return -1;
6226 if (test_mupa_upma(ctx) < 0)
6227 return -1;
6229 space = isl_space_set_alloc(ctx, 0, 1);
6230 ls = isl_local_space_from_space(space);
6231 aff = isl_aff_zero_on_domain(ls);
6233 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6234 aff = isl_aff_scale_down_ui(aff, 3);
6235 aff = isl_aff_floor(aff);
6236 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6237 aff = isl_aff_scale_down_ui(aff, 2);
6238 aff = isl_aff_floor(aff);
6239 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6241 str = "{ [10] }";
6242 set = isl_set_read_from_str(ctx, str);
6243 aff = isl_aff_gist(aff, set);
6245 aff = isl_aff_add_constant_si(aff, -16);
6246 zero = isl_aff_plain_is_zero(aff);
6247 isl_aff_free(aff);
6249 if (zero < 0)
6250 return -1;
6251 if (!zero)
6252 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6254 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
6255 aff = isl_aff_scale_down_ui(aff, 64);
6256 aff = isl_aff_floor(aff);
6257 equal = aff_check_plain_equal(aff, "{ [-1] }");
6258 isl_aff_free(aff);
6259 if (equal < 0)
6260 return -1;
6262 if (test_aff_set_tuple_id(ctx) < 0)
6263 return -1;
6265 return 0;
6268 /* Check that "pa" consists of a single expression.
6270 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
6272 int n;
6274 n = isl_pw_aff_n_piece(pa);
6275 isl_pw_aff_free(pa);
6277 if (!pa)
6278 return -1;
6279 if (n != 1)
6280 isl_die(ctx, isl_error_unknown, "expecting single expression",
6281 return -1);
6283 return 0;
6286 /* Check that the computation below results in a single expression.
6287 * One or two expressions may result depending on which constraint
6288 * ends up being considered as redundant with respect to the other
6289 * constraints after the projection that is performed internally
6290 * by isl_set_dim_min.
6292 static int test_dim_max_1(isl_ctx *ctx)
6294 const char *str;
6295 isl_set *set;
6296 isl_pw_aff *pa;
6298 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
6299 "-4a <= b <= 3 and b < n - 4a }";
6300 set = isl_set_read_from_str(ctx, str);
6301 pa = isl_set_dim_min(set, 0);
6302 return check_single_piece(ctx, pa);
6305 /* Check that the computation below results in a single expression.
6306 * The PIP problem corresponding to these constraints has a row
6307 * that causes a split of the solution domain. The solver should
6308 * first pick rows that split off empty parts such that the actual
6309 * solution domain does not get split.
6310 * Note that the description contains some redundant constraints.
6311 * If these constraints get removed first, then the row mentioned
6312 * above does not appear in the PIP problem.
6314 static int test_dim_max_2(isl_ctx *ctx)
6316 const char *str;
6317 isl_set *set;
6318 isl_pw_aff *pa;
6320 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
6321 "N > 0 and P >= 0 }";
6322 set = isl_set_read_from_str(ctx, str);
6323 pa = isl_set_dim_max(set, 0);
6324 return check_single_piece(ctx, pa);
6327 int test_dim_max(isl_ctx *ctx)
6329 int equal;
6330 const char *str;
6331 isl_set *set1, *set2;
6332 isl_set *set;
6333 isl_map *map;
6334 isl_pw_aff *pwaff;
6336 if (test_dim_max_1(ctx) < 0)
6337 return -1;
6338 if (test_dim_max_2(ctx) < 0)
6339 return -1;
6341 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
6342 set = isl_set_read_from_str(ctx, str);
6343 pwaff = isl_set_dim_max(set, 0);
6344 set1 = isl_set_from_pw_aff(pwaff);
6345 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
6346 set2 = isl_set_read_from_str(ctx, str);
6347 equal = isl_set_is_equal(set1, set2);
6348 isl_set_free(set1);
6349 isl_set_free(set2);
6350 if (equal < 0)
6351 return -1;
6352 if (!equal)
6353 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6355 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
6356 set = isl_set_read_from_str(ctx, str);
6357 pwaff = isl_set_dim_max(set, 0);
6358 set1 = isl_set_from_pw_aff(pwaff);
6359 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6360 set2 = isl_set_read_from_str(ctx, str);
6361 equal = isl_set_is_equal(set1, set2);
6362 isl_set_free(set1);
6363 isl_set_free(set2);
6364 if (equal < 0)
6365 return -1;
6366 if (!equal)
6367 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6369 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
6370 set = isl_set_read_from_str(ctx, str);
6371 pwaff = isl_set_dim_max(set, 0);
6372 set1 = isl_set_from_pw_aff(pwaff);
6373 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6374 set2 = isl_set_read_from_str(ctx, str);
6375 equal = isl_set_is_equal(set1, set2);
6376 isl_set_free(set1);
6377 isl_set_free(set2);
6378 if (equal < 0)
6379 return -1;
6380 if (!equal)
6381 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6383 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
6384 "0 <= i < N and 0 <= j < M }";
6385 map = isl_map_read_from_str(ctx, str);
6386 set = isl_map_range(map);
6388 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
6389 set1 = isl_set_from_pw_aff(pwaff);
6390 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
6391 set2 = isl_set_read_from_str(ctx, str);
6392 equal = isl_set_is_equal(set1, set2);
6393 isl_set_free(set1);
6394 isl_set_free(set2);
6396 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
6397 set1 = isl_set_from_pw_aff(pwaff);
6398 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
6399 set2 = isl_set_read_from_str(ctx, str);
6400 if (equal >= 0 && equal)
6401 equal = isl_set_is_equal(set1, set2);
6402 isl_set_free(set1);
6403 isl_set_free(set2);
6405 isl_set_free(set);
6407 if (equal < 0)
6408 return -1;
6409 if (!equal)
6410 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6412 /* Check that solutions are properly merged. */
6413 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
6414 "c <= -1 + n - 4a - 2b and c >= -2b and "
6415 "4a >= -4 + n and c >= 0 }";
6416 set = isl_set_read_from_str(ctx, str);
6417 pwaff = isl_set_dim_min(set, 2);
6418 set1 = isl_set_from_pw_aff(pwaff);
6419 str = "[n] -> { [(0)] : n >= 1 }";
6420 set2 = isl_set_read_from_str(ctx, str);
6421 equal = isl_set_is_equal(set1, set2);
6422 isl_set_free(set1);
6423 isl_set_free(set2);
6425 if (equal < 0)
6426 return -1;
6427 if (!equal)
6428 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6430 /* Check that empty solution lie in the right space. */
6431 str = "[n] -> { [t,a] : 1 = 0 }";
6432 set = isl_set_read_from_str(ctx, str);
6433 pwaff = isl_set_dim_max(set, 0);
6434 set1 = isl_set_from_pw_aff(pwaff);
6435 str = "[n] -> { [t] : 1 = 0 }";
6436 set2 = isl_set_read_from_str(ctx, str);
6437 equal = isl_set_is_equal(set1, set2);
6438 isl_set_free(set1);
6439 isl_set_free(set2);
6441 if (equal < 0)
6442 return -1;
6443 if (!equal)
6444 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6446 return 0;
6449 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
6451 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
6452 const char *str)
6454 isl_ctx *ctx;
6455 isl_pw_multi_aff *pma2;
6456 int equal;
6458 if (!pma)
6459 return -1;
6461 ctx = isl_pw_multi_aff_get_ctx(pma);
6462 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6463 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
6464 isl_pw_multi_aff_free(pma2);
6466 return equal;
6469 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
6470 * represented by "str".
6472 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
6473 const char *str)
6475 int equal;
6477 equal = pw_multi_aff_plain_is_equal(pma, str);
6478 if (equal < 0)
6479 return -1;
6480 if (!equal)
6481 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
6482 "result not as expected", return -1);
6483 return 0;
6486 /* Basic test for isl_pw_multi_aff_product.
6488 * Check that multiple pieces are properly handled.
6490 static int test_product_pma(isl_ctx *ctx)
6492 int equal;
6493 const char *str;
6494 isl_pw_multi_aff *pma1, *pma2;
6496 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
6497 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6498 str = "{ C[] -> D[] }";
6499 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6500 pma1 = isl_pw_multi_aff_product(pma1, pma2);
6501 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
6502 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
6503 equal = pw_multi_aff_check_plain_equal(pma1, str);
6504 isl_pw_multi_aff_free(pma1);
6505 if (equal < 0)
6506 return -1;
6508 return 0;
6511 int test_product(isl_ctx *ctx)
6513 const char *str;
6514 isl_set *set;
6515 isl_union_set *uset1, *uset2;
6516 int ok;
6518 str = "{ A[i] }";
6519 set = isl_set_read_from_str(ctx, str);
6520 set = isl_set_product(set, isl_set_copy(set));
6521 ok = isl_set_is_wrapping(set);
6522 isl_set_free(set);
6523 if (ok < 0)
6524 return -1;
6525 if (!ok)
6526 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6528 str = "{ [] }";
6529 uset1 = isl_union_set_read_from_str(ctx, str);
6530 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
6531 str = "{ [[] -> []] }";
6532 uset2 = isl_union_set_read_from_str(ctx, str);
6533 ok = isl_union_set_is_equal(uset1, uset2);
6534 isl_union_set_free(uset1);
6535 isl_union_set_free(uset2);
6536 if (ok < 0)
6537 return -1;
6538 if (!ok)
6539 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6541 if (test_product_pma(ctx) < 0)
6542 return -1;
6544 return 0;
6547 /* Check that two sets are not considered disjoint just because
6548 * they have a different set of (named) parameters.
6550 static int test_disjoint(isl_ctx *ctx)
6552 const char *str;
6553 isl_set *set, *set2;
6554 int disjoint;
6556 str = "[n] -> { [[]->[]] }";
6557 set = isl_set_read_from_str(ctx, str);
6558 str = "{ [[]->[]] }";
6559 set2 = isl_set_read_from_str(ctx, str);
6560 disjoint = isl_set_is_disjoint(set, set2);
6561 isl_set_free(set);
6562 isl_set_free(set2);
6563 if (disjoint < 0)
6564 return -1;
6565 if (disjoint)
6566 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6568 return 0;
6571 /* Inputs for isl_pw_multi_aff_is_equal tests.
6572 * "f1" and "f2" are the two function that need to be compared.
6573 * "equal" is the expected result.
6575 struct {
6576 int equal;
6577 const char *f1;
6578 const char *f2;
6579 } pma_equal_tests[] = {
6580 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
6581 "[N] -> { [0] : 0 <= N <= 1 }" },
6582 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6583 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
6584 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6585 "[N] -> { [0] : 0 <= N <= 1 }" },
6586 { 0, "{ [NaN] }", "{ [NaN] }" },
6589 int test_equal(isl_ctx *ctx)
6591 int i;
6592 const char *str;
6593 isl_set *set, *set2;
6594 int equal;
6596 str = "{ S_6[i] }";
6597 set = isl_set_read_from_str(ctx, str);
6598 str = "{ S_7[i] }";
6599 set2 = isl_set_read_from_str(ctx, str);
6600 equal = isl_set_is_equal(set, set2);
6601 isl_set_free(set);
6602 isl_set_free(set2);
6603 if (equal < 0)
6604 return -1;
6605 if (equal)
6606 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6608 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
6609 int expected = pma_equal_tests[i].equal;
6610 isl_pw_multi_aff *f1, *f2;
6612 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
6613 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
6614 equal = isl_pw_multi_aff_is_equal(f1, f2);
6615 isl_pw_multi_aff_free(f1);
6616 isl_pw_multi_aff_free(f2);
6617 if (equal < 0)
6618 return -1;
6619 if (equal != expected)
6620 isl_die(ctx, isl_error_unknown,
6621 "unexpected equality result", return -1);
6624 return 0;
6627 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
6628 enum isl_dim_type type, unsigned pos, int fixed)
6630 isl_bool test;
6632 test = isl_map_plain_is_fixed(map, type, pos, NULL);
6633 isl_map_free(map);
6634 if (test < 0)
6635 return -1;
6636 if (test == fixed)
6637 return 0;
6638 if (fixed)
6639 isl_die(ctx, isl_error_unknown,
6640 "map not detected as fixed", return -1);
6641 else
6642 isl_die(ctx, isl_error_unknown,
6643 "map detected as fixed", return -1);
6646 int test_fixed(isl_ctx *ctx)
6648 const char *str;
6649 isl_map *map;
6651 str = "{ [i] -> [i] }";
6652 map = isl_map_read_from_str(ctx, str);
6653 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
6654 return -1;
6655 str = "{ [i] -> [1] }";
6656 map = isl_map_read_from_str(ctx, str);
6657 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6658 return -1;
6659 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
6660 map = isl_map_read_from_str(ctx, str);
6661 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6662 return -1;
6663 map = isl_map_read_from_str(ctx, str);
6664 map = isl_map_neg(map);
6665 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6666 return -1;
6668 return 0;
6671 struct isl_vertices_test_data {
6672 const char *set;
6673 int n;
6674 const char *vertex[6];
6675 } vertices_tests[] = {
6676 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
6677 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
6678 { "{ A[t, i] : t = 14 and i = 1 }",
6679 1, { "{ A[14, 1] }" } },
6680 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
6681 "c <= m and m <= n and m > 0 }",
6682 6, {
6683 "[n, m] -> { [n, m, m] : 0 < m <= n }",
6684 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
6685 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
6686 "[n, m] -> { [m, m, m] : 0 < m <= n }",
6687 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
6688 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
6689 } },
6692 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
6694 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
6696 struct isl_vertices_test_data *data = user;
6697 isl_ctx *ctx;
6698 isl_multi_aff *ma;
6699 isl_basic_set *bset;
6700 isl_pw_multi_aff *pma;
6701 int i;
6702 isl_bool equal;
6704 ctx = isl_vertex_get_ctx(vertex);
6705 bset = isl_vertex_get_domain(vertex);
6706 ma = isl_vertex_get_expr(vertex);
6707 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
6709 for (i = 0; i < data->n; ++i) {
6710 isl_pw_multi_aff *pma_i;
6712 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
6713 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
6714 isl_pw_multi_aff_free(pma_i);
6716 if (equal < 0 || equal)
6717 break;
6720 isl_pw_multi_aff_free(pma);
6721 isl_vertex_free(vertex);
6723 if (equal < 0)
6724 return isl_stat_error;
6726 return equal ? isl_stat_ok : isl_stat_error;
6729 int test_vertices(isl_ctx *ctx)
6731 int i;
6733 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
6734 isl_basic_set *bset;
6735 isl_vertices *vertices;
6736 int ok = 1;
6737 int n;
6739 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
6740 vertices = isl_basic_set_compute_vertices(bset);
6741 n = isl_vertices_get_n_vertices(vertices);
6742 if (vertices_tests[i].n != n)
6743 ok = 0;
6744 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
6745 &vertices_tests[i]) < 0)
6746 ok = 0;
6747 isl_vertices_free(vertices);
6748 isl_basic_set_free(bset);
6750 if (!vertices)
6751 return -1;
6752 if (!ok)
6753 isl_die(ctx, isl_error_unknown, "unexpected vertices",
6754 return -1);
6757 return 0;
6760 int test_union_pw(isl_ctx *ctx)
6762 int equal;
6763 const char *str;
6764 isl_union_set *uset;
6765 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
6767 str = "{ [x] -> x^2 }";
6768 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
6769 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
6770 uset = isl_union_pw_qpolynomial_domain(upwqp1);
6771 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
6772 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
6773 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
6774 isl_union_pw_qpolynomial_free(upwqp1);
6775 isl_union_pw_qpolynomial_free(upwqp2);
6776 if (equal < 0)
6777 return -1;
6778 if (!equal)
6779 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6781 return 0;
6784 /* Inputs for basic tests of functions that select
6785 * subparts of the domain of an isl_multi_union_pw_aff.
6786 * "fn" is the function that is tested.
6787 * "arg" is a string description of the input.
6788 * "res" is a string description of the expected result.
6790 struct {
6791 __isl_give isl_union_set *(*fn)(
6792 __isl_take isl_multi_union_pw_aff *mupa);
6793 const char *arg;
6794 const char *res;
6795 } un_locus_tests[] = {
6796 { &isl_multi_union_pw_aff_zero_union_set,
6797 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6798 "{ A[0,j]; B[0,j] }" },
6799 { &isl_multi_union_pw_aff_zero_union_set,
6800 "F[{ A[i,j] -> [i-j]; B[i,j] -> [i-j] : i >= 0 }]",
6801 "{ A[i,i]; B[i,i] : i >= 0 }" },
6802 { &isl_multi_union_pw_aff_zero_union_set,
6803 "(F[] : { A[i,j]; B[i,i] : i >= 0 })",
6804 "{ A[i,j]; B[i,i] : i >= 0 }" },
6807 /* Perform some basic tests of functions that select
6808 * subparts of the domain of an isl_multi_union_pw_aff.
6810 static int test_un_locus(isl_ctx *ctx)
6812 int i;
6813 isl_bool ok;
6814 isl_union_set *uset, *res;
6815 isl_multi_union_pw_aff *mupa;
6817 for (i = 0; i < ARRAY_SIZE(un_locus_tests); ++i) {
6818 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6819 un_locus_tests[i].arg);
6820 res = isl_union_set_read_from_str(ctx, un_locus_tests[i].res);
6821 uset = un_locus_tests[i].fn(mupa);
6822 ok = isl_union_set_is_equal(uset, res);
6823 isl_union_set_free(uset);
6824 isl_union_set_free(res);
6825 if (ok < 0)
6826 return -1;
6827 if (!ok)
6828 isl_die(ctx, isl_error_unknown,
6829 "unexpected result", return -1);
6832 return 0;
6835 /* Inputs for basic tests of functions that select
6836 * subparts of an isl_union_map based on a relation
6837 * specified by an isl_multi_union_pw_aff.
6838 * "fn" is the function that is tested.
6839 * "arg1" and "arg2" are string descriptions of the inputs.
6840 * "res" is a string description of the expected result.
6842 struct {
6843 __isl_give isl_union_map *(*fn)(
6844 __isl_take isl_union_map *umap,
6845 __isl_take isl_multi_union_pw_aff *mupa);
6846 const char *arg1;
6847 const char *arg2;
6848 const char *res;
6849 } bin_locus_tests[] = {
6850 { &isl_union_map_eq_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] -> B[i,j'] }" },
6854 { &isl_union_map_eq_at_multi_union_pw_aff,
6855 "{ A[i,j] -> B[i',j'] }",
6856 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6857 "{ A[i,j] -> [j]; B[i,j] -> [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']; A[i,j] -> C[i',j'] }",
6861 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6862 "{ A[i,j] -> B[i,j'] }" },
6863 { &isl_union_map_eq_at_multi_union_pw_aff,
6864 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6865 "F[{ A[i,j] -> [i]; B[i,j] -> [i]; C[i,j] -> [0] }]",
6866 "{ A[i,j] -> B[i,j']; A[0,j] -> C[i',j'] }" },
6867 { &isl_union_map_eq_at_multi_union_pw_aff,
6868 "{ A[i,j] -> B[i',j'] }",
6869 "F[{ A[i,j] -> [i] : i > j; B[i,j] -> [i] }]",
6870 "{ A[i,j] -> B[i,j'] : i > j }" },
6871 { &isl_union_map_lex_lt_at_multi_union_pw_aff,
6872 "{ A[i,j] -> B[i',j'] }",
6873 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6874 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6875 "{ A[i,j] -> B[i',j'] : i,j << i',j' }" },
6876 { &isl_union_map_lex_gt_at_multi_union_pw_aff,
6877 "{ A[i,j] -> B[i',j'] }",
6878 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6879 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6880 "{ A[i,j] -> B[i',j'] : i,j >> i',j' }" },
6881 { &isl_union_map_eq_at_multi_union_pw_aff,
6882 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6883 "(F[] : { A[i,j]; B[i,j] })",
6884 "{ A[i,j] -> B[i',j'] }" },
6885 { &isl_union_map_eq_at_multi_union_pw_aff,
6886 "{ A[i,j] -> B[i',j'] }",
6887 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
6888 "{ A[i,j] -> B[i',j'] : i > j and i' < j' }" },
6889 { &isl_union_map_eq_at_multi_union_pw_aff,
6890 "[N] -> { A[i,j] -> B[i',j'] : i,i' <= N }",
6891 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
6892 "[N] -> { A[i,j] -> B[i',j'] : i > j and i' < j' and i,i' <= N }" },
6893 { &isl_union_map_eq_at_multi_union_pw_aff,
6894 "{ A[i,j] -> B[i',j'] }",
6895 "[N] -> (F[] : { A[i,j] : i < N; B[i,j] : i < N })",
6896 "[N] -> { A[i,j] -> B[i',j'] : i,i' < N }" },
6897 { &isl_union_map_eq_at_multi_union_pw_aff,
6898 "{ A[i,j] -> B[i',j'] }",
6899 "[N] -> (F[] : { : N >= 0 })",
6900 "[N] -> { A[i,j] -> B[i',j'] : N >= 0 }" },
6903 /* Perform some basic tests of functions that select
6904 * subparts of an isl_union_map based on a relation
6905 * specified by an isl_multi_union_pw_aff.
6907 static int test_bin_locus(isl_ctx *ctx)
6909 int i;
6910 isl_bool ok;
6911 isl_union_map *umap, *res;
6912 isl_multi_union_pw_aff *mupa;
6914 for (i = 0; i < ARRAY_SIZE(bin_locus_tests); ++i) {
6915 umap = isl_union_map_read_from_str(ctx,
6916 bin_locus_tests[i].arg1);
6917 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6918 bin_locus_tests[i].arg2);
6919 res = isl_union_map_read_from_str(ctx, bin_locus_tests[i].res);
6920 umap = bin_locus_tests[i].fn(umap, mupa);
6921 ok = isl_union_map_is_equal(umap, res);
6922 isl_union_map_free(umap);
6923 isl_union_map_free(res);
6924 if (ok < 0)
6925 return -1;
6926 if (!ok)
6927 isl_die(ctx, isl_error_unknown,
6928 "unexpected result", return -1);
6931 return 0;
6934 /* Perform basic locus tests.
6936 static int test_locus(isl_ctx *ctx)
6938 if (test_un_locus(ctx) < 0)
6939 return -1;
6940 if (test_bin_locus(ctx) < 0)
6941 return -1;
6942 return 0;
6945 /* Test that isl_union_pw_qpolynomial_eval picks up the function
6946 * defined over the correct domain space.
6948 static int test_eval_1(isl_ctx *ctx)
6950 const char *str;
6951 isl_point *pnt;
6952 isl_set *set;
6953 isl_union_pw_qpolynomial *upwqp;
6954 isl_val *v;
6955 int cmp;
6957 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
6958 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
6959 str = "{ A[6] }";
6960 set = isl_set_read_from_str(ctx, str);
6961 pnt = isl_set_sample_point(set);
6962 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
6963 cmp = isl_val_cmp_si(v, 36);
6964 isl_val_free(v);
6966 if (!v)
6967 return -1;
6968 if (cmp != 0)
6969 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
6971 return 0;
6974 /* Check that isl_qpolynomial_eval handles getting called on a void point.
6976 static int test_eval_2(isl_ctx *ctx)
6978 const char *str;
6979 isl_point *pnt;
6980 isl_set *set;
6981 isl_qpolynomial *qp;
6982 isl_val *v;
6983 isl_bool ok;
6985 str = "{ A[x] -> [x] }";
6986 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
6987 str = "{ A[x] : false }";
6988 set = isl_set_read_from_str(ctx, str);
6989 pnt = isl_set_sample_point(set);
6990 v = isl_qpolynomial_eval(qp, pnt);
6991 ok = isl_val_is_nan(v);
6992 isl_val_free(v);
6994 if (ok < 0)
6995 return -1;
6996 if (!ok)
6997 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
6999 return 0;
7002 /* Inputs for isl_pw_aff_eval test.
7003 * "f" is the affine function.
7004 * "p" is the point where the function should be evaluated.
7005 * "res" is the expected result.
7007 struct {
7008 const char *f;
7009 const char *p;
7010 const char *res;
7011 } aff_eval_tests[] = {
7012 { "{ [i] -> [2 * i] }", "{ [4] }", "8" },
7013 { "{ [i] -> [2 * i] }", "{ [x] : false }", "NaN" },
7014 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [0] }", "0" },
7015 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [1] }", "1" },
7016 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [2] }", "3" },
7017 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [3] }", "5" },
7018 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [4] }", "7" },
7019 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [0] }", "0" },
7020 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [1] }", "0" },
7021 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [2] }", "0" },
7022 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [3] }", "0" },
7023 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [4] }", "1" },
7024 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [6] }", "1" },
7025 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [8] }", "2" },
7026 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [4] }", "4" },
7027 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [-2] }", "2" },
7028 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [0] }", "NaN" },
7029 { "[N] -> { [2 * N] }", "[N] -> { : N = 4 }", "8" },
7030 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 1] }", "1" },
7031 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 2] }", "3/2" },
7032 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [4] }", "4" },
7033 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [3] }", "NaN" },
7034 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [x] : false }", "NaN" },
7037 /* Perform basic isl_pw_aff_eval tests.
7039 static int test_eval_aff(isl_ctx *ctx)
7041 int i;
7043 for (i = 0; i < ARRAY_SIZE(aff_eval_tests); ++i) {
7044 isl_stat r;
7045 isl_pw_aff *pa;
7046 isl_set *set;
7047 isl_point *pnt;
7048 isl_val *v;
7050 pa = isl_pw_aff_read_from_str(ctx, aff_eval_tests[i].f);
7051 set = isl_set_read_from_str(ctx, aff_eval_tests[i].p);
7052 pnt = isl_set_sample_point(set);
7053 v = isl_pw_aff_eval(pa, pnt);
7054 r = val_check_equal(v, aff_eval_tests[i].res);
7055 isl_val_free(v);
7056 if (r < 0)
7057 return -1;
7059 return 0;
7062 /* Perform basic evaluation tests.
7064 static int test_eval(isl_ctx *ctx)
7066 if (test_eval_1(ctx) < 0)
7067 return -1;
7068 if (test_eval_2(ctx) < 0)
7069 return -1;
7070 if (test_eval_aff(ctx) < 0)
7071 return -1;
7072 return 0;
7075 /* Descriptions of sets that are tested for reparsing after printing.
7077 const char *output_tests[] = {
7078 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
7079 "{ [x] : 1 = 0 }",
7080 "{ [x] : false }",
7081 "{ [x] : x mod 2 = 0 }",
7082 "{ [x] : x mod 2 = 1 }",
7083 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
7084 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
7085 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7086 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7087 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
7088 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
7091 /* Check that printing a set and reparsing a set from the printed output
7092 * results in the same set.
7094 static int test_output_set(isl_ctx *ctx)
7096 int i;
7097 char *str;
7098 isl_set *set1, *set2;
7099 isl_bool equal;
7101 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
7102 set1 = isl_set_read_from_str(ctx, output_tests[i]);
7103 str = isl_set_to_str(set1);
7104 set2 = isl_set_read_from_str(ctx, str);
7105 free(str);
7106 equal = isl_set_is_equal(set1, set2);
7107 isl_set_free(set1);
7108 isl_set_free(set2);
7109 if (equal < 0)
7110 return -1;
7111 if (!equal)
7112 isl_die(ctx, isl_error_unknown,
7113 "parsed output not the same", return -1);
7116 return 0;
7119 /* Check that an isl_multi_aff is printed using a consistent space.
7121 static isl_stat test_output_ma(isl_ctx *ctx)
7123 char *str;
7124 isl_bool equal;
7125 isl_aff *aff;
7126 isl_multi_aff *ma, *ma2;
7128 ma = isl_multi_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7129 aff = isl_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7130 ma = isl_multi_aff_set_aff(ma, 0, aff);
7131 str = isl_multi_aff_to_str(ma);
7132 ma2 = isl_multi_aff_read_from_str(ctx, str);
7133 free(str);
7134 equal = isl_multi_aff_plain_is_equal(ma, ma2);
7135 isl_multi_aff_free(ma2);
7136 isl_multi_aff_free(ma);
7138 if (equal < 0)
7139 return isl_stat_error;
7140 if (!equal)
7141 isl_die(ctx, isl_error_unknown, "bad conversion",
7142 return isl_stat_error);
7144 return isl_stat_ok;
7147 /* Check that an isl_multi_pw_aff is printed using a consistent space.
7149 static isl_stat test_output_mpa(isl_ctx *ctx)
7151 char *str;
7152 isl_bool equal;
7153 isl_pw_aff *pa;
7154 isl_multi_pw_aff *mpa, *mpa2;
7156 mpa = isl_multi_pw_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7157 pa = isl_pw_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7158 mpa = isl_multi_pw_aff_set_pw_aff(mpa, 0, pa);
7159 str = isl_multi_pw_aff_to_str(mpa);
7160 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
7161 free(str);
7162 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
7163 isl_multi_pw_aff_free(mpa2);
7164 isl_multi_pw_aff_free(mpa);
7166 if (equal < 0)
7167 return isl_stat_error;
7168 if (!equal)
7169 isl_die(ctx, isl_error_unknown, "bad conversion",
7170 return isl_stat_error);
7172 return isl_stat_ok;
7175 int test_output(isl_ctx *ctx)
7177 char *s;
7178 const char *str;
7179 isl_pw_aff *pa;
7180 isl_printer *p;
7181 int equal;
7183 if (test_output_set(ctx) < 0)
7184 return -1;
7185 if (test_output_ma(ctx) < 0)
7186 return -1;
7187 if (test_output_mpa(ctx) < 0)
7188 return -1;
7190 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
7191 pa = isl_pw_aff_read_from_str(ctx, str);
7193 p = isl_printer_to_str(ctx);
7194 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
7195 p = isl_printer_print_pw_aff(p, pa);
7196 s = isl_printer_get_str(p);
7197 isl_printer_free(p);
7198 isl_pw_aff_free(pa);
7199 if (!s)
7200 equal = -1;
7201 else
7202 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
7203 free(s);
7204 if (equal < 0)
7205 return -1;
7206 if (!equal)
7207 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7209 return 0;
7212 int test_sample(isl_ctx *ctx)
7214 const char *str;
7215 isl_basic_set *bset1, *bset2;
7216 int empty, subset;
7218 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
7219 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
7220 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
7221 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
7222 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
7223 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
7224 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
7225 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
7226 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
7227 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
7228 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
7229 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
7230 "d - 1073741821e and "
7231 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
7232 "3j >= 1 - a + b + 2e and "
7233 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
7234 "3i <= 4 - a + 4b - e and "
7235 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
7236 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
7237 "c <= -1 + a and 3i >= -2 - a + 3e and "
7238 "1073741822e <= 1073741823 - a + 1073741822b + c and "
7239 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
7240 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
7241 "1073741823e >= 1 + 1073741823b - d and "
7242 "3i >= 1073741823b + c - 1073741823e - f and "
7243 "3i >= 1 + 2b + e + 3g }";
7244 bset1 = isl_basic_set_read_from_str(ctx, str);
7245 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
7246 empty = isl_basic_set_is_empty(bset2);
7247 subset = isl_basic_set_is_subset(bset2, bset1);
7248 isl_basic_set_free(bset1);
7249 isl_basic_set_free(bset2);
7250 if (empty < 0 || subset < 0)
7251 return -1;
7252 if (empty)
7253 isl_die(ctx, isl_error_unknown, "point not found", return -1);
7254 if (!subset)
7255 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
7257 return 0;
7260 int test_fixed_power(isl_ctx *ctx)
7262 const char *str;
7263 isl_map *map;
7264 isl_val *exp;
7265 int equal;
7267 str = "{ [i] -> [i + 1] }";
7268 map = isl_map_read_from_str(ctx, str);
7269 exp = isl_val_int_from_si(ctx, 23);
7270 map = isl_map_fixed_power_val(map, exp);
7271 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
7272 isl_map_free(map);
7273 if (equal < 0)
7274 return -1;
7276 return 0;
7279 int test_slice(isl_ctx *ctx)
7281 const char *str;
7282 isl_map *map;
7283 int equal;
7285 str = "{ [i] -> [j] }";
7286 map = isl_map_read_from_str(ctx, str);
7287 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
7288 equal = map_check_equal(map, "{ [i] -> [i] }");
7289 isl_map_free(map);
7290 if (equal < 0)
7291 return -1;
7293 str = "{ [i] -> [j] }";
7294 map = isl_map_read_from_str(ctx, str);
7295 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
7296 equal = map_check_equal(map, "{ [i] -> [j] }");
7297 isl_map_free(map);
7298 if (equal < 0)
7299 return -1;
7301 str = "{ [i] -> [j] }";
7302 map = isl_map_read_from_str(ctx, str);
7303 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
7304 equal = map_check_equal(map, "{ [i] -> [-i] }");
7305 isl_map_free(map);
7306 if (equal < 0)
7307 return -1;
7309 str = "{ [i] -> [j] }";
7310 map = isl_map_read_from_str(ctx, str);
7311 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
7312 equal = map_check_equal(map, "{ [0] -> [j] }");
7313 isl_map_free(map);
7314 if (equal < 0)
7315 return -1;
7317 str = "{ [i] -> [j] }";
7318 map = isl_map_read_from_str(ctx, str);
7319 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
7320 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
7321 isl_map_free(map);
7322 if (equal < 0)
7323 return -1;
7325 str = "{ [i] -> [j] }";
7326 map = isl_map_read_from_str(ctx, str);
7327 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
7328 equal = map_check_equal(map, "{ [i] -> [j] : false }");
7329 isl_map_free(map);
7330 if (equal < 0)
7331 return -1;
7333 return 0;
7336 int test_eliminate(isl_ctx *ctx)
7338 const char *str;
7339 isl_map *map;
7340 int equal;
7342 str = "{ [i] -> [j] : i = 2j }";
7343 map = isl_map_read_from_str(ctx, str);
7344 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
7345 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
7346 isl_map_free(map);
7347 if (equal < 0)
7348 return -1;
7350 return 0;
7353 /* Check that isl_set_dim_residue_class detects that the values of j
7354 * in the set below are all odd and that it does not detect any spurious
7355 * strides.
7357 static int test_residue_class(isl_ctx *ctx)
7359 const char *str;
7360 isl_set *set;
7361 isl_int m, r;
7362 isl_stat res;
7364 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
7365 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
7366 set = isl_set_read_from_str(ctx, str);
7367 isl_int_init(m);
7368 isl_int_init(r);
7369 res = isl_set_dim_residue_class(set, 1, &m, &r);
7370 if (res >= 0 &&
7371 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
7372 isl_die(ctx, isl_error_unknown, "incorrect residue class",
7373 res = isl_stat_error);
7374 isl_int_clear(r);
7375 isl_int_clear(m);
7376 isl_set_free(set);
7378 return res;
7381 static int test_align_parameters_1(isl_ctx *ctx)
7383 const char *str;
7384 isl_space *space;
7385 isl_multi_aff *ma1, *ma2;
7386 int equal;
7388 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
7389 ma1 = isl_multi_aff_read_from_str(ctx, str);
7391 space = isl_space_params_alloc(ctx, 1);
7392 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
7393 ma1 = isl_multi_aff_align_params(ma1, space);
7395 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
7396 ma2 = isl_multi_aff_read_from_str(ctx, str);
7398 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
7400 isl_multi_aff_free(ma1);
7401 isl_multi_aff_free(ma2);
7403 if (equal < 0)
7404 return -1;
7405 if (!equal)
7406 isl_die(ctx, isl_error_unknown,
7407 "result not as expected", return -1);
7409 return 0;
7412 /* Check the isl_multi_*_from_*_list operation in case inputs
7413 * have unaligned parameters.
7414 * In particular, older versions of isl would simply fail
7415 * (without printing any error message).
7417 static isl_stat test_align_parameters_2(isl_ctx *ctx)
7419 isl_space *space;
7420 isl_map *map;
7421 isl_aff *aff;
7422 isl_multi_aff *ma;
7424 map = isl_map_read_from_str(ctx, "{ A[] -> M[x] }");
7425 space = isl_map_get_space(map);
7426 isl_map_free(map);
7428 aff = isl_aff_read_from_str(ctx, "[N] -> { A[] -> [N] }");
7429 ma = isl_multi_aff_from_aff_list(space, isl_aff_list_from_aff(aff));
7430 isl_multi_aff_free(ma);
7432 if (!ma)
7433 return isl_stat_error;
7434 return isl_stat_ok;
7437 /* Perform basic parameter alignment tests.
7439 static int test_align_parameters(isl_ctx *ctx)
7441 if (test_align_parameters_1(ctx) < 0)
7442 return -1;
7443 if (test_align_parameters_2(ctx) < 0)
7444 return -1;
7446 return 0;
7449 /* Check that isl_*_drop_unused_params actually drops the unused parameters
7450 * by comparing the result using isl_*_plain_is_equal.
7451 * Note that this assumes that isl_*_plain_is_equal does not consider
7452 * objects that only differ by unused parameters to be equal.
7454 int test_drop_unused_parameters(isl_ctx *ctx)
7456 const char *str_with, *str_without;
7457 isl_basic_set *bset1, *bset2;
7458 isl_set *set1, *set2;
7459 isl_pw_aff *pwa1, *pwa2;
7460 int equal;
7462 str_with = "[n, m, o] -> { [m] }";
7463 str_without = "[m] -> { [m] }";
7465 bset1 = isl_basic_set_read_from_str(ctx, str_with);
7466 bset2 = isl_basic_set_read_from_str(ctx, str_without);
7467 bset1 = isl_basic_set_drop_unused_params(bset1);
7468 equal = isl_basic_set_plain_is_equal(bset1, bset2);
7469 isl_basic_set_free(bset1);
7470 isl_basic_set_free(bset2);
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 set1 = isl_set_read_from_str(ctx, str_with);
7479 set2 = isl_set_read_from_str(ctx, str_without);
7480 set1 = isl_set_drop_unused_params(set1);
7481 equal = isl_set_plain_is_equal(set1, set2);
7482 isl_set_free(set1);
7483 isl_set_free(set2);
7485 if (equal < 0)
7486 return -1;
7487 if (!equal)
7488 isl_die(ctx, isl_error_unknown,
7489 "result not as expected", return -1);
7491 pwa1 = isl_pw_aff_read_from_str(ctx, str_with);
7492 pwa2 = isl_pw_aff_read_from_str(ctx, str_without);
7493 pwa1 = isl_pw_aff_drop_unused_params(pwa1);
7494 equal = isl_pw_aff_plain_is_equal(pwa1, pwa2);
7495 isl_pw_aff_free(pwa1);
7496 isl_pw_aff_free(pwa2);
7498 if (equal < 0)
7499 return -1;
7500 if (!equal)
7501 isl_die(ctx, isl_error_unknown,
7502 "result not as expected", return -1);
7504 return 0;
7507 static int test_list(isl_ctx *ctx)
7509 isl_id *a, *b, *c, *d, *id;
7510 isl_id_list *list;
7511 int ok;
7513 a = isl_id_alloc(ctx, "a", NULL);
7514 b = isl_id_alloc(ctx, "b", NULL);
7515 c = isl_id_alloc(ctx, "c", NULL);
7516 d = isl_id_alloc(ctx, "d", NULL);
7518 list = isl_id_list_alloc(ctx, 4);
7519 list = isl_id_list_add(list, b);
7520 list = isl_id_list_insert(list, 0, a);
7521 list = isl_id_list_add(list, c);
7522 list = isl_id_list_add(list, d);
7523 list = isl_id_list_drop(list, 1, 1);
7525 if (!list)
7526 return -1;
7527 if (isl_id_list_n_id(list) != 3) {
7528 isl_id_list_free(list);
7529 isl_die(ctx, isl_error_unknown,
7530 "unexpected number of elements in list", return -1);
7533 id = isl_id_list_get_id(list, 0);
7534 ok = id == a;
7535 isl_id_free(id);
7536 id = isl_id_list_get_id(list, 1);
7537 ok = ok && id == c;
7538 isl_id_free(id);
7539 id = isl_id_list_get_id(list, 2);
7540 ok = ok && id == d;
7541 isl_id_free(id);
7543 isl_id_list_free(list);
7545 if (!ok)
7546 isl_die(ctx, isl_error_unknown,
7547 "unexpected elements in list", return -1);
7549 return 0;
7552 const char *set_conversion_tests[] = {
7553 "[N] -> { [i] : N - 1 <= 2 i <= N }",
7554 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
7555 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7556 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7557 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
7558 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
7559 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
7560 "-3 + c <= d <= 28 + c) }",
7563 /* Check that converting from isl_set to isl_pw_multi_aff and back
7564 * to isl_set produces the original isl_set.
7566 static int test_set_conversion(isl_ctx *ctx)
7568 int i;
7569 const char *str;
7570 isl_set *set1, *set2;
7571 isl_pw_multi_aff *pma;
7572 int equal;
7574 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
7575 str = set_conversion_tests[i];
7576 set1 = isl_set_read_from_str(ctx, str);
7577 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
7578 set2 = isl_set_from_pw_multi_aff(pma);
7579 equal = isl_set_is_equal(set1, set2);
7580 isl_set_free(set1);
7581 isl_set_free(set2);
7583 if (equal < 0)
7584 return -1;
7585 if (!equal)
7586 isl_die(ctx, isl_error_unknown, "bad conversion",
7587 return -1);
7590 return 0;
7593 const char *conversion_tests[] = {
7594 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7595 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7596 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7597 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7598 "9e <= -2 - 2a) }",
7599 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7600 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7601 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7604 /* Check that converting from isl_map to isl_pw_multi_aff and back
7605 * to isl_map produces the original isl_map.
7607 static int test_map_conversion(isl_ctx *ctx)
7609 int i;
7610 isl_map *map1, *map2;
7611 isl_pw_multi_aff *pma;
7612 int equal;
7614 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
7615 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
7616 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
7617 map2 = isl_map_from_pw_multi_aff(pma);
7618 equal = isl_map_is_equal(map1, map2);
7619 isl_map_free(map1);
7620 isl_map_free(map2);
7622 if (equal < 0)
7623 return -1;
7624 if (!equal)
7625 isl_die(ctx, isl_error_unknown, "bad conversion",
7626 return -1);
7629 return 0;
7632 /* Descriptions of isl_pw_multi_aff objects for testing conversion
7633 * to isl_multi_pw_aff and back.
7635 const char *mpa_conversion_tests[] = {
7636 "{ [x] -> A[x] }",
7637 "{ [x] -> A[x] : x >= 0 }",
7638 "{ [x] -> A[x] : x >= 0; [x] -> A[-x] : x < 0 }",
7639 "{ [x] -> A[x, x + 1] }",
7640 "{ [x] -> A[] }",
7641 "{ [x] -> A[] : x >= 0 }",
7644 /* Check that conversion from isl_pw_multi_aff to isl_multi_pw_aff and
7645 * back to isl_pw_multi_aff preserves the original meaning.
7647 static int test_mpa_conversion(isl_ctx *ctx)
7649 int i;
7650 isl_pw_multi_aff *pma1, *pma2;
7651 isl_multi_pw_aff *mpa;
7652 int equal;
7654 for (i = 0; i < ARRAY_SIZE(mpa_conversion_tests); ++i) {
7655 const char *str;
7656 str = mpa_conversion_tests[i];
7657 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
7658 pma2 = isl_pw_multi_aff_copy(pma1);
7659 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma1);
7660 pma1 = isl_pw_multi_aff_from_multi_pw_aff(mpa);
7661 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7662 isl_pw_multi_aff_free(pma1);
7663 isl_pw_multi_aff_free(pma2);
7665 if (equal < 0)
7666 return -1;
7667 if (!equal)
7668 isl_die(ctx, isl_error_unknown, "bad conversion",
7669 return -1);
7672 return 0;
7675 /* Descriptions of union maps that should be convertible
7676 * to an isl_multi_union_pw_aff.
7678 const char *umap_mupa_conversion_tests[] = {
7679 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7680 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7681 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7682 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7683 "9e <= -2 - 2a) }",
7684 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7685 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7686 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7687 "{ A[] -> B[0]; C[] -> B[1] }",
7688 "{ A[] -> B[]; C[] -> B[] }",
7691 /* Check that converting from isl_union_map to isl_multi_union_pw_aff and back
7692 * to isl_union_map produces the original isl_union_map.
7694 static int test_union_map_mupa_conversion(isl_ctx *ctx)
7696 int i;
7697 isl_union_map *umap1, *umap2;
7698 isl_multi_union_pw_aff *mupa;
7699 int equal;
7701 for (i = 0; i < ARRAY_SIZE(umap_mupa_conversion_tests); ++i) {
7702 const char *str;
7703 str = umap_mupa_conversion_tests[i];
7704 umap1 = isl_union_map_read_from_str(ctx, str);
7705 umap2 = isl_union_map_copy(umap1);
7706 mupa = isl_multi_union_pw_aff_from_union_map(umap2);
7707 umap2 = isl_union_map_from_multi_union_pw_aff(mupa);
7708 equal = isl_union_map_is_equal(umap1, umap2);
7709 isl_union_map_free(umap1);
7710 isl_union_map_free(umap2);
7712 if (equal < 0)
7713 return -1;
7714 if (!equal)
7715 isl_die(ctx, isl_error_unknown, "bad conversion",
7716 return -1);
7719 return 0;
7722 static int test_conversion(isl_ctx *ctx)
7724 if (test_set_conversion(ctx) < 0)
7725 return -1;
7726 if (test_map_conversion(ctx) < 0)
7727 return -1;
7728 if (test_mpa_conversion(ctx) < 0)
7729 return -1;
7730 if (test_union_map_mupa_conversion(ctx) < 0)
7731 return -1;
7732 return 0;
7735 /* Check that isl_basic_map_curry does not modify input.
7737 static int test_curry(isl_ctx *ctx)
7739 const char *str;
7740 isl_basic_map *bmap1, *bmap2;
7741 int equal;
7743 str = "{ [A[] -> B[]] -> C[] }";
7744 bmap1 = isl_basic_map_read_from_str(ctx, str);
7745 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
7746 equal = isl_basic_map_is_equal(bmap1, bmap2);
7747 isl_basic_map_free(bmap1);
7748 isl_basic_map_free(bmap2);
7750 if (equal < 0)
7751 return -1;
7752 if (equal)
7753 isl_die(ctx, isl_error_unknown,
7754 "curried map should not be equal to original",
7755 return -1);
7757 return 0;
7760 struct {
7761 const char *set;
7762 const char *ma;
7763 const char *res;
7764 } preimage_tests[] = {
7765 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
7766 "{ A[j,i] -> B[i,j] }",
7767 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
7768 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
7769 "{ A[a,b] -> B[a/2,b/6] }",
7770 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
7771 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
7772 "{ A[a,b] -> B[a/2,b/6] }",
7773 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
7774 "exists i,j : a = 2 i and b = 6 j }" },
7775 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
7776 "[n] -> { : 0 <= n <= 100 }" },
7777 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
7778 "{ A[a] -> B[2a] }",
7779 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
7780 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
7781 "{ A[a] -> B[([a/2])] }",
7782 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
7783 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
7784 "{ A[a] -> B[a,a,a/3] }",
7785 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
7786 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
7787 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
7790 static int test_preimage_basic_set(isl_ctx *ctx)
7792 int i;
7793 isl_basic_set *bset1, *bset2;
7794 isl_multi_aff *ma;
7795 int equal;
7797 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
7798 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
7799 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
7800 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
7801 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
7802 equal = isl_basic_set_is_equal(bset1, bset2);
7803 isl_basic_set_free(bset1);
7804 isl_basic_set_free(bset2);
7805 if (equal < 0)
7806 return -1;
7807 if (!equal)
7808 isl_die(ctx, isl_error_unknown, "bad preimage",
7809 return -1);
7812 return 0;
7815 struct {
7816 const char *map;
7817 const char *ma;
7818 const char *res;
7819 } preimage_domain_tests[] = {
7820 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
7821 "{ A[j,i] -> B[i,j] }",
7822 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
7823 { "{ B[i] -> C[i]; D[i] -> E[i] }",
7824 "{ A[i] -> B[i + 1] }",
7825 "{ A[i] -> C[i + 1] }" },
7826 { "{ B[i] -> C[i]; B[i] -> E[i] }",
7827 "{ A[i] -> B[i + 1] }",
7828 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
7829 { "{ B[i] -> C[([i/2])] }",
7830 "{ A[i] -> B[2i] }",
7831 "{ A[i] -> C[i] }" },
7832 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
7833 "{ A[i] -> B[([i/5]), ([i/7])] }",
7834 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
7835 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
7836 "[N] -> { A[] -> B[([N/5])] }",
7837 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
7838 { "{ B[i] -> C[i] : exists a : i = 5 a }",
7839 "{ A[i] -> B[2i] }",
7840 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
7841 { "{ B[i] -> C[i] : exists a : i = 2 a; "
7842 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
7843 "{ A[i] -> B[2i] }",
7844 "{ A[i] -> C[2i] }" },
7845 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
7846 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
7849 static int test_preimage_union_map(isl_ctx *ctx)
7851 int i;
7852 isl_union_map *umap1, *umap2;
7853 isl_multi_aff *ma;
7854 int equal;
7856 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
7857 umap1 = isl_union_map_read_from_str(ctx,
7858 preimage_domain_tests[i].map);
7859 ma = isl_multi_aff_read_from_str(ctx,
7860 preimage_domain_tests[i].ma);
7861 umap2 = isl_union_map_read_from_str(ctx,
7862 preimage_domain_tests[i].res);
7863 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
7864 equal = isl_union_map_is_equal(umap1, umap2);
7865 isl_union_map_free(umap1);
7866 isl_union_map_free(umap2);
7867 if (equal < 0)
7868 return -1;
7869 if (!equal)
7870 isl_die(ctx, isl_error_unknown, "bad preimage",
7871 return -1);
7874 return 0;
7877 static int test_preimage(isl_ctx *ctx)
7879 if (test_preimage_basic_set(ctx) < 0)
7880 return -1;
7881 if (test_preimage_union_map(ctx) < 0)
7882 return -1;
7884 return 0;
7887 struct {
7888 const char *ma1;
7889 const char *ma;
7890 const char *res;
7891 } pullback_tests[] = {
7892 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
7893 "{ A[a,b] -> C[b + 2a] }" },
7894 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
7895 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
7896 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
7897 "{ A[a] -> C[(a)/6] }" },
7898 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
7899 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
7900 "{ A[a] -> C[(2a)/3] }" },
7901 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
7902 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
7903 "{ A[i,j] -> C[i + j, i + j] }"},
7904 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
7905 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
7906 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
7907 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
7908 "{ [i, j] -> [floor((i)/3), j] }",
7909 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
7912 static int test_pullback(isl_ctx *ctx)
7914 int i;
7915 isl_multi_aff *ma1, *ma2;
7916 isl_multi_aff *ma;
7917 int equal;
7919 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
7920 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
7921 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
7922 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
7923 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
7924 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
7925 isl_multi_aff_free(ma1);
7926 isl_multi_aff_free(ma2);
7927 if (equal < 0)
7928 return -1;
7929 if (!equal)
7930 isl_die(ctx, isl_error_unknown, "bad pullback",
7931 return -1);
7934 return 0;
7937 /* Check that negation is printed correctly and that equal expressions
7938 * are correctly identified.
7940 static int test_ast(isl_ctx *ctx)
7942 isl_ast_expr *expr, *expr1, *expr2, *expr3;
7943 char *str;
7944 int ok, equal;
7946 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
7947 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
7948 expr = isl_ast_expr_add(expr1, expr2);
7949 expr2 = isl_ast_expr_copy(expr);
7950 expr = isl_ast_expr_neg(expr);
7951 expr2 = isl_ast_expr_neg(expr2);
7952 equal = isl_ast_expr_is_equal(expr, expr2);
7953 str = isl_ast_expr_to_C_str(expr);
7954 ok = str ? !strcmp(str, "-(A + B)") : -1;
7955 free(str);
7956 isl_ast_expr_free(expr);
7957 isl_ast_expr_free(expr2);
7959 if (ok < 0 || equal < 0)
7960 return -1;
7961 if (!equal)
7962 isl_die(ctx, isl_error_unknown,
7963 "equal expressions not considered equal", return -1);
7964 if (!ok)
7965 isl_die(ctx, isl_error_unknown,
7966 "isl_ast_expr printed incorrectly", return -1);
7968 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
7969 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
7970 expr = isl_ast_expr_add(expr1, expr2);
7971 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
7972 expr = isl_ast_expr_sub(expr3, expr);
7973 str = isl_ast_expr_to_C_str(expr);
7974 ok = str ? !strcmp(str, "C - (A + B)") : -1;
7975 free(str);
7976 isl_ast_expr_free(expr);
7978 if (ok < 0)
7979 return -1;
7980 if (!ok)
7981 isl_die(ctx, isl_error_unknown,
7982 "isl_ast_expr printed incorrectly", return -1);
7984 return 0;
7987 /* Check that isl_ast_build_expr_from_set returns a valid expression
7988 * for an empty set. Note that isl_ast_build_expr_from_set getting
7989 * called on an empty set probably indicates a bug in the caller.
7991 static int test_ast_build(isl_ctx *ctx)
7993 isl_set *set;
7994 isl_ast_build *build;
7995 isl_ast_expr *expr;
7997 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
7998 build = isl_ast_build_from_context(set);
8000 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
8001 expr = isl_ast_build_expr_from_set(build, set);
8003 isl_ast_expr_free(expr);
8004 isl_ast_build_free(build);
8006 if (!expr)
8007 return -1;
8009 return 0;
8012 /* Internal data structure for before_for and after_for callbacks.
8014 * depth is the current depth
8015 * before is the number of times before_for has been called
8016 * after is the number of times after_for has been called
8018 struct isl_test_codegen_data {
8019 int depth;
8020 int before;
8021 int after;
8024 /* This function is called before each for loop in the AST generated
8025 * from test_ast_gen1.
8027 * Increment the number of calls and the depth.
8028 * Check that the space returned by isl_ast_build_get_schedule_space
8029 * matches the target space of the schedule returned by
8030 * isl_ast_build_get_schedule.
8031 * Return an isl_id that is checked by the corresponding call
8032 * to after_for.
8034 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
8035 void *user)
8037 struct isl_test_codegen_data *data = user;
8038 isl_ctx *ctx;
8039 isl_space *space;
8040 isl_union_map *schedule;
8041 isl_union_set *uset;
8042 isl_set *set;
8043 isl_bool empty;
8044 char name[] = "d0";
8046 ctx = isl_ast_build_get_ctx(build);
8048 if (data->before >= 3)
8049 isl_die(ctx, isl_error_unknown,
8050 "unexpected number of for nodes", return NULL);
8051 if (data->depth >= 2)
8052 isl_die(ctx, isl_error_unknown,
8053 "unexpected depth", return NULL);
8055 snprintf(name, sizeof(name), "d%d", data->depth);
8056 data->before++;
8057 data->depth++;
8059 schedule = isl_ast_build_get_schedule(build);
8060 uset = isl_union_map_range(schedule);
8061 if (!uset)
8062 return NULL;
8063 if (isl_union_set_n_set(uset) != 1) {
8064 isl_union_set_free(uset);
8065 isl_die(ctx, isl_error_unknown,
8066 "expecting single range space", return NULL);
8069 space = isl_ast_build_get_schedule_space(build);
8070 set = isl_union_set_extract_set(uset, space);
8071 isl_union_set_free(uset);
8072 empty = isl_set_is_empty(set);
8073 isl_set_free(set);
8075 if (empty < 0)
8076 return NULL;
8077 if (empty)
8078 isl_die(ctx, isl_error_unknown,
8079 "spaces don't match", return NULL);
8081 return isl_id_alloc(ctx, name, NULL);
8084 /* This function is called after each for loop in the AST generated
8085 * from test_ast_gen1.
8087 * Increment the number of calls and decrement the depth.
8088 * Check that the annotation attached to the node matches
8089 * the isl_id returned by the corresponding call to before_for.
8091 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
8092 __isl_keep isl_ast_build *build, void *user)
8094 struct isl_test_codegen_data *data = user;
8095 isl_id *id;
8096 const char *name;
8097 int valid;
8099 data->after++;
8100 data->depth--;
8102 if (data->after > data->before)
8103 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8104 "mismatch in number of for nodes",
8105 return isl_ast_node_free(node));
8107 id = isl_ast_node_get_annotation(node);
8108 if (!id)
8109 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8110 "missing annotation", return isl_ast_node_free(node));
8112 name = isl_id_get_name(id);
8113 valid = name && atoi(name + 1) == data->depth;
8114 isl_id_free(id);
8116 if (!valid)
8117 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8118 "wrong annotation", return isl_ast_node_free(node));
8120 return node;
8123 /* Check that the before_each_for and after_each_for callbacks
8124 * are called for each for loop in the generated code,
8125 * that they are called in the right order and that the isl_id
8126 * returned from the before_each_for callback is attached to
8127 * the isl_ast_node passed to the corresponding after_each_for call.
8129 static int test_ast_gen1(isl_ctx *ctx)
8131 const char *str;
8132 isl_set *set;
8133 isl_union_map *schedule;
8134 isl_ast_build *build;
8135 isl_ast_node *tree;
8136 struct isl_test_codegen_data data;
8138 str = "[N] -> { : N >= 10 }";
8139 set = isl_set_read_from_str(ctx, str);
8140 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
8141 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
8142 schedule = isl_union_map_read_from_str(ctx, str);
8144 data.before = 0;
8145 data.after = 0;
8146 data.depth = 0;
8147 build = isl_ast_build_from_context(set);
8148 build = isl_ast_build_set_before_each_for(build,
8149 &before_for, &data);
8150 build = isl_ast_build_set_after_each_for(build,
8151 &after_for, &data);
8152 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8153 isl_ast_build_free(build);
8154 if (!tree)
8155 return -1;
8157 isl_ast_node_free(tree);
8159 if (data.before != 3 || data.after != 3)
8160 isl_die(ctx, isl_error_unknown,
8161 "unexpected number of for nodes", return -1);
8163 return 0;
8166 /* Check that the AST generator handles domains that are integrally disjoint
8167 * but not rationally disjoint.
8169 static int test_ast_gen2(isl_ctx *ctx)
8171 const char *str;
8172 isl_set *set;
8173 isl_union_map *schedule;
8174 isl_union_map *options;
8175 isl_ast_build *build;
8176 isl_ast_node *tree;
8178 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
8179 schedule = isl_union_map_read_from_str(ctx, str);
8180 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8181 build = isl_ast_build_from_context(set);
8183 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
8184 options = isl_union_map_read_from_str(ctx, str);
8185 build = isl_ast_build_set_options(build, options);
8186 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8187 isl_ast_build_free(build);
8188 if (!tree)
8189 return -1;
8190 isl_ast_node_free(tree);
8192 return 0;
8195 /* Increment *user on each call.
8197 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
8198 __isl_keep isl_ast_build *build, void *user)
8200 int *n = user;
8202 (*n)++;
8204 return node;
8207 /* Test that unrolling tries to minimize the number of instances.
8208 * In particular, for the schedule given below, make sure it generates
8209 * 3 nodes (rather than 101).
8211 static int test_ast_gen3(isl_ctx *ctx)
8213 const char *str;
8214 isl_set *set;
8215 isl_union_map *schedule;
8216 isl_union_map *options;
8217 isl_ast_build *build;
8218 isl_ast_node *tree;
8219 int n_domain = 0;
8221 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
8222 schedule = isl_union_map_read_from_str(ctx, str);
8223 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8225 str = "{ [i] -> unroll[0] }";
8226 options = isl_union_map_read_from_str(ctx, str);
8228 build = isl_ast_build_from_context(set);
8229 build = isl_ast_build_set_options(build, options);
8230 build = isl_ast_build_set_at_each_domain(build,
8231 &count_domains, &n_domain);
8232 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8233 isl_ast_build_free(build);
8234 if (!tree)
8235 return -1;
8237 isl_ast_node_free(tree);
8239 if (n_domain != 3)
8240 isl_die(ctx, isl_error_unknown,
8241 "unexpected number of for nodes", return -1);
8243 return 0;
8246 /* Check that if the ast_build_exploit_nested_bounds options is set,
8247 * we do not get an outer if node in the generated AST,
8248 * while we do get such an outer if node if the options is not set.
8250 static int test_ast_gen4(isl_ctx *ctx)
8252 const char *str;
8253 isl_set *set;
8254 isl_union_map *schedule;
8255 isl_ast_build *build;
8256 isl_ast_node *tree;
8257 enum isl_ast_node_type type;
8258 int enb;
8260 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
8261 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
8263 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
8265 schedule = isl_union_map_read_from_str(ctx, str);
8266 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8267 build = isl_ast_build_from_context(set);
8268 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8269 isl_ast_build_free(build);
8270 if (!tree)
8271 return -1;
8273 type = isl_ast_node_get_type(tree);
8274 isl_ast_node_free(tree);
8276 if (type == isl_ast_node_if)
8277 isl_die(ctx, isl_error_unknown,
8278 "not expecting if node", return -1);
8280 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
8282 schedule = isl_union_map_read_from_str(ctx, str);
8283 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8284 build = isl_ast_build_from_context(set);
8285 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8286 isl_ast_build_free(build);
8287 if (!tree)
8288 return -1;
8290 type = isl_ast_node_get_type(tree);
8291 isl_ast_node_free(tree);
8293 if (type != isl_ast_node_if)
8294 isl_die(ctx, isl_error_unknown,
8295 "expecting if node", return -1);
8297 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
8299 return 0;
8302 /* This function is called for each leaf in the AST generated
8303 * from test_ast_gen5.
8305 * We finalize the AST generation by extending the outer schedule
8306 * with a zero-dimensional schedule. If this results in any for loops,
8307 * then this means that we did not pass along enough information
8308 * about the outer schedule to the inner AST generation.
8310 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
8311 void *user)
8313 isl_union_map *schedule, *extra;
8314 isl_ast_node *tree;
8316 schedule = isl_ast_build_get_schedule(build);
8317 extra = isl_union_map_copy(schedule);
8318 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
8319 schedule = isl_union_map_range_product(schedule, extra);
8320 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8321 isl_ast_build_free(build);
8323 if (!tree)
8324 return NULL;
8326 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
8327 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
8328 "code should not contain any for loop",
8329 return isl_ast_node_free(tree));
8331 return tree;
8334 /* Check that we do not lose any information when going back and
8335 * forth between internal and external schedule.
8337 * In particular, we create an AST where we unroll the only
8338 * non-constant dimension in the schedule. We therefore do
8339 * not expect any for loops in the AST. However, older versions
8340 * of isl would not pass along enough information about the outer
8341 * schedule when performing an inner code generation from a create_leaf
8342 * callback, resulting in the inner code generation producing a for loop.
8344 static int test_ast_gen5(isl_ctx *ctx)
8346 const char *str;
8347 isl_set *set;
8348 isl_union_map *schedule, *options;
8349 isl_ast_build *build;
8350 isl_ast_node *tree;
8352 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
8353 schedule = isl_union_map_read_from_str(ctx, str);
8355 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
8356 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
8357 options = isl_union_map_read_from_str(ctx, str);
8359 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8360 build = isl_ast_build_from_context(set);
8361 build = isl_ast_build_set_options(build, options);
8362 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
8363 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8364 isl_ast_build_free(build);
8365 isl_ast_node_free(tree);
8366 if (!tree)
8367 return -1;
8369 return 0;
8372 /* Check that the expression
8374 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
8376 * is not combined into
8378 * min(n/2, 0)
8380 * as this would result in n/2 being evaluated in parts of
8381 * the definition domain where n is not a multiple of 2.
8383 static int test_ast_expr(isl_ctx *ctx)
8385 const char *str;
8386 isl_pw_aff *pa;
8387 isl_ast_build *build;
8388 isl_ast_expr *expr;
8389 int min_max;
8390 int is_min;
8392 min_max = isl_options_get_ast_build_detect_min_max(ctx);
8393 isl_options_set_ast_build_detect_min_max(ctx, 1);
8395 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
8396 pa = isl_pw_aff_read_from_str(ctx, str);
8397 build = isl_ast_build_alloc(ctx);
8398 expr = isl_ast_build_expr_from_pw_aff(build, pa);
8399 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
8400 isl_ast_expr_get_op_type(expr) == isl_ast_op_min;
8401 isl_ast_build_free(build);
8402 isl_ast_expr_free(expr);
8404 isl_options_set_ast_build_detect_min_max(ctx, min_max);
8406 if (!expr)
8407 return -1;
8408 if (is_min)
8409 isl_die(ctx, isl_error_unknown,
8410 "expressions should not be combined", return -1);
8412 return 0;
8415 static int test_ast_gen(isl_ctx *ctx)
8417 if (test_ast_gen1(ctx) < 0)
8418 return -1;
8419 if (test_ast_gen2(ctx) < 0)
8420 return -1;
8421 if (test_ast_gen3(ctx) < 0)
8422 return -1;
8423 if (test_ast_gen4(ctx) < 0)
8424 return -1;
8425 if (test_ast_gen5(ctx) < 0)
8426 return -1;
8427 if (test_ast_expr(ctx) < 0)
8428 return -1;
8429 return 0;
8432 /* Check if dropping output dimensions from an isl_pw_multi_aff
8433 * works properly.
8435 static int test_pw_multi_aff(isl_ctx *ctx)
8437 const char *str;
8438 isl_pw_multi_aff *pma1, *pma2;
8439 int equal;
8441 str = "{ [i,j] -> [i+j, 4i-j] }";
8442 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
8443 str = "{ [i,j] -> [4i-j] }";
8444 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
8446 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
8448 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
8450 isl_pw_multi_aff_free(pma1);
8451 isl_pw_multi_aff_free(pma2);
8452 if (equal < 0)
8453 return -1;
8454 if (!equal)
8455 isl_die(ctx, isl_error_unknown,
8456 "expressions not equal", return -1);
8458 return 0;
8461 /* Check that we can properly parse multi piecewise affine expressions
8462 * where the piecewise affine expressions have different domains.
8464 static int test_multi_pw_aff_1(isl_ctx *ctx)
8466 const char *str;
8467 isl_set *dom, *dom2;
8468 isl_multi_pw_aff *mpa1, *mpa2;
8469 isl_pw_aff *pa;
8470 int equal;
8471 int equal_domain;
8473 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
8474 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
8475 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
8476 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
8477 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
8478 str = "{ [i] -> [(i : i > 0), 2i] }";
8479 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
8481 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
8483 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
8484 dom = isl_pw_aff_domain(pa);
8485 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
8486 dom2 = isl_pw_aff_domain(pa);
8487 equal_domain = isl_set_is_equal(dom, dom2);
8489 isl_set_free(dom);
8490 isl_set_free(dom2);
8491 isl_multi_pw_aff_free(mpa1);
8492 isl_multi_pw_aff_free(mpa2);
8494 if (equal < 0)
8495 return -1;
8496 if (!equal)
8497 isl_die(ctx, isl_error_unknown,
8498 "expressions not equal", return -1);
8500 if (equal_domain < 0)
8501 return -1;
8502 if (equal_domain)
8503 isl_die(ctx, isl_error_unknown,
8504 "domains unexpectedly equal", return -1);
8506 return 0;
8509 /* Check that the dimensions in the explicit domain
8510 * of a multi piecewise affine expression are properly
8511 * taken into account.
8513 static int test_multi_pw_aff_2(isl_ctx *ctx)
8515 const char *str;
8516 isl_bool involves1, involves2, involves3, equal;
8517 isl_multi_pw_aff *mpa, *mpa1, *mpa2;
8519 str = "{ A[x,y] -> B[] : x >= y }";
8520 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
8521 involves1 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 2);
8522 mpa1 = isl_multi_pw_aff_copy(mpa);
8524 mpa = isl_multi_pw_aff_insert_dims(mpa, isl_dim_in, 0, 1);
8525 involves2 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 1);
8526 involves3 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 1, 2);
8527 str = "{ [a,x,y] -> B[] : x >= y }";
8528 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
8529 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
8530 isl_multi_pw_aff_free(mpa2);
8532 mpa = isl_multi_pw_aff_drop_dims(mpa, isl_dim_in, 0, 1);
8533 mpa = isl_multi_pw_aff_set_tuple_name(mpa, isl_dim_in, "A");
8534 if (equal >= 0 && equal)
8535 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa1);
8536 isl_multi_pw_aff_free(mpa1);
8537 isl_multi_pw_aff_free(mpa);
8539 if (involves1 < 0 || involves2 < 0 || involves3 < 0 || equal < 0)
8540 return -1;
8541 if (!equal)
8542 isl_die(ctx, isl_error_unknown,
8543 "incorrect result of dimension insertion/removal",
8544 return isl_stat_error);
8545 if (!involves1 || involves2 || !involves3)
8546 isl_die(ctx, isl_error_unknown,
8547 "incorrect characterization of involved dimensions",
8548 return isl_stat_error);
8550 return 0;
8553 /* Check that isl_multi_union_pw_aff_multi_val_on_domain
8554 * sets the explicit domain of a zero-dimensional result,
8555 * such that it can be converted to an isl_union_map.
8557 static isl_stat test_multi_pw_aff_3(isl_ctx *ctx)
8559 isl_space *space;
8560 isl_union_set *dom;
8561 isl_multi_val *mv;
8562 isl_multi_union_pw_aff *mupa;
8563 isl_union_map *umap;
8565 dom = isl_union_set_read_from_str(ctx, "{ A[]; B[] }");
8566 space = isl_union_set_get_space(dom);
8567 mv = isl_multi_val_zero(isl_space_set_from_params(space));
8568 mupa = isl_multi_union_pw_aff_multi_val_on_domain(dom, mv);
8569 umap = isl_union_map_from_multi_union_pw_aff(mupa);
8570 isl_union_map_free(umap);
8571 if (!umap)
8572 return isl_stat_error;
8574 return isl_stat_ok;
8577 /* Perform some tests on multi piecewise affine expressions.
8579 static int test_multi_pw_aff(isl_ctx *ctx)
8581 if (test_multi_pw_aff_1(ctx) < 0)
8582 return -1;
8583 if (test_multi_pw_aff_2(ctx) < 0)
8584 return -1;
8585 if (test_multi_pw_aff_3(ctx) < 0)
8586 return -1;
8587 return 0;
8590 /* This is a regression test for a bug where isl_basic_map_simplify
8591 * would end up in an infinite loop. In particular, we construct
8592 * an empty basic set that is not obviously empty.
8593 * isl_basic_set_is_empty marks the basic set as empty.
8594 * After projecting out i3, the variable can be dropped completely,
8595 * but isl_basic_map_simplify refrains from doing so if the basic set
8596 * is empty and would end up in an infinite loop if it didn't test
8597 * explicitly for empty basic maps in the outer loop.
8599 static int test_simplify_1(isl_ctx *ctx)
8601 const char *str;
8602 isl_basic_set *bset;
8603 int empty;
8605 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
8606 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
8607 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
8608 "i3 >= i2 }";
8609 bset = isl_basic_set_read_from_str(ctx, str);
8610 empty = isl_basic_set_is_empty(bset);
8611 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
8612 isl_basic_set_free(bset);
8613 if (!bset)
8614 return -1;
8615 if (!empty)
8616 isl_die(ctx, isl_error_unknown,
8617 "basic set should be empty", return -1);
8619 return 0;
8622 /* Check that the equality in the set description below
8623 * is simplified away.
8625 static int test_simplify_2(isl_ctx *ctx)
8627 const char *str;
8628 isl_basic_set *bset;
8629 isl_bool universe;
8631 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
8632 bset = isl_basic_set_read_from_str(ctx, str);
8633 universe = isl_basic_set_plain_is_universe(bset);
8634 isl_basic_set_free(bset);
8636 if (universe < 0)
8637 return -1;
8638 if (!universe)
8639 isl_die(ctx, isl_error_unknown,
8640 "equality not simplified away", return -1);
8641 return 0;
8644 /* Some simplification tests.
8646 static int test_simplify(isl_ctx *ctx)
8648 if (test_simplify_1(ctx) < 0)
8649 return -1;
8650 if (test_simplify_2(ctx) < 0)
8651 return -1;
8652 return 0;
8655 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
8656 * with gbr context would fail to disable the use of the shifted tableau
8657 * when transferring equalities for the input to the context, resulting
8658 * in invalid sample values.
8660 static int test_partial_lexmin(isl_ctx *ctx)
8662 const char *str;
8663 isl_basic_set *bset;
8664 isl_basic_map *bmap;
8665 isl_map *map;
8667 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
8668 bmap = isl_basic_map_read_from_str(ctx, str);
8669 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
8670 bset = isl_basic_set_read_from_str(ctx, str);
8671 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
8672 isl_map_free(map);
8674 if (!map)
8675 return -1;
8677 return 0;
8680 /* Check that the variable compression performed on the existentially
8681 * quantified variables inside isl_basic_set_compute_divs is not confused
8682 * by the implicit equalities among the parameters.
8684 static int test_compute_divs(isl_ctx *ctx)
8686 const char *str;
8687 isl_basic_set *bset;
8688 isl_set *set;
8690 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
8691 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
8692 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
8693 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
8694 bset = isl_basic_set_read_from_str(ctx, str);
8695 set = isl_basic_set_compute_divs(bset);
8696 isl_set_free(set);
8697 if (!set)
8698 return -1;
8700 return 0;
8703 /* Check that isl_schedule_get_map is not confused by a schedule tree
8704 * with divergent filter node parameters, as can result from a call
8705 * to isl_schedule_intersect_domain.
8707 static int test_schedule_tree(isl_ctx *ctx)
8709 const char *str;
8710 isl_union_set *uset;
8711 isl_schedule *sched1, *sched2;
8712 isl_union_map *umap;
8714 uset = isl_union_set_read_from_str(ctx, "{ A[i] }");
8715 sched1 = isl_schedule_from_domain(uset);
8716 uset = isl_union_set_read_from_str(ctx, "{ B[] }");
8717 sched2 = isl_schedule_from_domain(uset);
8719 sched1 = isl_schedule_sequence(sched1, sched2);
8720 str = "[n] -> { A[i] : 0 <= i < n; B[] }";
8721 uset = isl_union_set_read_from_str(ctx, str);
8722 sched1 = isl_schedule_intersect_domain(sched1, uset);
8723 umap = isl_schedule_get_map(sched1);
8724 isl_schedule_free(sched1);
8725 isl_union_map_free(umap);
8726 if (!umap)
8727 return -1;
8729 return 0;
8732 /* Check that a zero-dimensional prefix schedule keeps track
8733 * of the domain and outer filters.
8735 static int test_schedule_tree_prefix(isl_ctx *ctx)
8737 const char *str;
8738 isl_bool equal;
8739 isl_union_set *uset;
8740 isl_union_set_list *filters;
8741 isl_multi_union_pw_aff *mupa, *mupa2;
8742 isl_schedule_node *node;
8744 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
8745 uset = isl_union_set_read_from_str(ctx, str);
8746 node = isl_schedule_node_from_domain(uset);
8747 node = isl_schedule_node_child(node, 0);
8749 str = "{ S1[i,j] : i > j }";
8750 uset = isl_union_set_read_from_str(ctx, str);
8751 filters = isl_union_set_list_from_union_set(uset);
8752 str = "{ S1[i,j] : i <= j; S2[i,j] }";
8753 uset = isl_union_set_read_from_str(ctx, str);
8754 filters = isl_union_set_list_add(filters, uset);
8755 node = isl_schedule_node_insert_sequence(node, filters);
8757 node = isl_schedule_node_child(node, 0);
8758 node = isl_schedule_node_child(node, 0);
8759 mupa = isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
8760 str = "([] : { S1[i,j] : i > j })";
8761 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx, str);
8762 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
8763 isl_multi_union_pw_aff_free(mupa2);
8764 isl_multi_union_pw_aff_free(mupa);
8765 isl_schedule_node_free(node);
8767 if (equal < 0)
8768 return -1;
8769 if (!equal)
8770 isl_die(ctx, isl_error_unknown, "unexpected prefix schedule",
8771 return -1);
8773 return 0;
8776 /* Check that the reaching domain elements and the prefix schedule
8777 * at a leaf node are the same before and after grouping.
8779 static int test_schedule_tree_group_1(isl_ctx *ctx)
8781 int equal;
8782 const char *str;
8783 isl_id *id;
8784 isl_union_set *uset;
8785 isl_multi_union_pw_aff *mupa;
8786 isl_union_pw_multi_aff *upma1, *upma2;
8787 isl_union_set *domain1, *domain2;
8788 isl_union_map *umap1, *umap2;
8789 isl_schedule_node *node;
8791 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
8792 uset = isl_union_set_read_from_str(ctx, str);
8793 node = isl_schedule_node_from_domain(uset);
8794 node = isl_schedule_node_child(node, 0);
8795 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
8796 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8797 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8798 node = isl_schedule_node_child(node, 0);
8799 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
8800 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8801 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8802 node = isl_schedule_node_child(node, 0);
8803 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
8804 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
8805 domain1 = isl_schedule_node_get_domain(node);
8806 id = isl_id_alloc(ctx, "group", NULL);
8807 node = isl_schedule_node_parent(node);
8808 node = isl_schedule_node_group(node, id);
8809 node = isl_schedule_node_child(node, 0);
8810 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
8811 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
8812 domain2 = isl_schedule_node_get_domain(node);
8813 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
8814 if (equal >= 0 && equal)
8815 equal = isl_union_set_is_equal(domain1, domain2);
8816 if (equal >= 0 && equal)
8817 equal = isl_union_map_is_equal(umap1, umap2);
8818 isl_union_map_free(umap1);
8819 isl_union_map_free(umap2);
8820 isl_union_set_free(domain1);
8821 isl_union_set_free(domain2);
8822 isl_union_pw_multi_aff_free(upma1);
8823 isl_union_pw_multi_aff_free(upma2);
8824 isl_schedule_node_free(node);
8826 if (equal < 0)
8827 return -1;
8828 if (!equal)
8829 isl_die(ctx, isl_error_unknown,
8830 "expressions not equal", return -1);
8832 return 0;
8835 /* Check that we can have nested groupings and that the union map
8836 * schedule representation is the same before and after the grouping.
8837 * Note that after the grouping, the union map representation contains
8838 * the domain constraints from the ranges of the expansion nodes,
8839 * while they are missing from the union map representation of
8840 * the tree without expansion nodes.
8842 * Also check that the global expansion is as expected.
8844 static int test_schedule_tree_group_2(isl_ctx *ctx)
8846 int equal, equal_expansion;
8847 const char *str;
8848 isl_id *id;
8849 isl_union_set *uset;
8850 isl_union_map *umap1, *umap2;
8851 isl_union_map *expansion1, *expansion2;
8852 isl_union_set_list *filters;
8853 isl_multi_union_pw_aff *mupa;
8854 isl_schedule *schedule;
8855 isl_schedule_node *node;
8857 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
8858 "S3[i,j] : 0 <= i,j < 10 }";
8859 uset = isl_union_set_read_from_str(ctx, str);
8860 node = isl_schedule_node_from_domain(uset);
8861 node = isl_schedule_node_child(node, 0);
8862 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
8863 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8864 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8865 node = isl_schedule_node_child(node, 0);
8866 str = "{ S1[i,j] }";
8867 uset = isl_union_set_read_from_str(ctx, str);
8868 filters = isl_union_set_list_from_union_set(uset);
8869 str = "{ S2[i,j]; S3[i,j] }";
8870 uset = isl_union_set_read_from_str(ctx, str);
8871 filters = isl_union_set_list_add(filters, uset);
8872 node = isl_schedule_node_insert_sequence(node, filters);
8873 node = isl_schedule_node_child(node, 1);
8874 node = isl_schedule_node_child(node, 0);
8875 str = "{ S2[i,j] }";
8876 uset = isl_union_set_read_from_str(ctx, str);
8877 filters = isl_union_set_list_from_union_set(uset);
8878 str = "{ S3[i,j] }";
8879 uset = isl_union_set_read_from_str(ctx, str);
8880 filters = isl_union_set_list_add(filters, uset);
8881 node = isl_schedule_node_insert_sequence(node, filters);
8883 schedule = isl_schedule_node_get_schedule(node);
8884 umap1 = isl_schedule_get_map(schedule);
8885 uset = isl_schedule_get_domain(schedule);
8886 umap1 = isl_union_map_intersect_domain(umap1, uset);
8887 isl_schedule_free(schedule);
8889 node = isl_schedule_node_parent(node);
8890 node = isl_schedule_node_parent(node);
8891 id = isl_id_alloc(ctx, "group1", NULL);
8892 node = isl_schedule_node_group(node, id);
8893 node = isl_schedule_node_child(node, 1);
8894 node = isl_schedule_node_child(node, 0);
8895 id = isl_id_alloc(ctx, "group2", NULL);
8896 node = isl_schedule_node_group(node, id);
8898 schedule = isl_schedule_node_get_schedule(node);
8899 umap2 = isl_schedule_get_map(schedule);
8900 isl_schedule_free(schedule);
8902 node = isl_schedule_node_root(node);
8903 node = isl_schedule_node_child(node, 0);
8904 expansion1 = isl_schedule_node_get_subtree_expansion(node);
8905 isl_schedule_node_free(node);
8907 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
8908 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
8909 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
8911 expansion2 = isl_union_map_read_from_str(ctx, str);
8913 equal = isl_union_map_is_equal(umap1, umap2);
8914 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
8916 isl_union_map_free(umap1);
8917 isl_union_map_free(umap2);
8918 isl_union_map_free(expansion1);
8919 isl_union_map_free(expansion2);
8921 if (equal < 0 || equal_expansion < 0)
8922 return -1;
8923 if (!equal)
8924 isl_die(ctx, isl_error_unknown,
8925 "expressions not equal", return -1);
8926 if (!equal_expansion)
8927 isl_die(ctx, isl_error_unknown,
8928 "unexpected expansion", return -1);
8930 return 0;
8933 /* Some tests for the isl_schedule_node_group function.
8935 static int test_schedule_tree_group(isl_ctx *ctx)
8937 if (test_schedule_tree_group_1(ctx) < 0)
8938 return -1;
8939 if (test_schedule_tree_group_2(ctx) < 0)
8940 return -1;
8941 return 0;
8944 struct {
8945 const char *set;
8946 const char *dual;
8947 } coef_tests[] = {
8948 { "{ rat: [i] : 0 <= i <= 10 }",
8949 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
8950 { "{ rat: [i] : FALSE }",
8951 "{ rat: coefficients[[cst] -> [a]] }" },
8952 { "{ rat: [i] : }",
8953 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
8956 struct {
8957 const char *set;
8958 const char *dual;
8959 } sol_tests[] = {
8960 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
8961 "{ rat: [i] : 0 <= i <= 10 }" },
8962 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
8963 "{ rat: [i] }" },
8964 { "{ rat: coefficients[[cst] -> [a]] }",
8965 "{ rat: [i] : FALSE }" },
8968 /* Test the basic functionality of isl_basic_set_coefficients and
8969 * isl_basic_set_solutions.
8971 static int test_dual(isl_ctx *ctx)
8973 int i;
8975 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
8976 int equal;
8977 isl_basic_set *bset1, *bset2;
8979 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
8980 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
8981 bset1 = isl_basic_set_coefficients(bset1);
8982 equal = isl_basic_set_is_equal(bset1, bset2);
8983 isl_basic_set_free(bset1);
8984 isl_basic_set_free(bset2);
8985 if (equal < 0)
8986 return -1;
8987 if (!equal)
8988 isl_die(ctx, isl_error_unknown,
8989 "incorrect dual", return -1);
8992 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
8993 int equal;
8994 isl_basic_set *bset1, *bset2;
8996 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
8997 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
8998 bset1 = isl_basic_set_solutions(bset1);
8999 equal = isl_basic_set_is_equal(bset1, bset2);
9000 isl_basic_set_free(bset1);
9001 isl_basic_set_free(bset2);
9002 if (equal < 0)
9003 return -1;
9004 if (!equal)
9005 isl_die(ctx, isl_error_unknown,
9006 "incorrect dual", return -1);
9009 return 0;
9012 struct {
9013 int scale_tile;
9014 int shift_point;
9015 const char *domain;
9016 const char *schedule;
9017 const char *sizes;
9018 const char *tile;
9019 const char *point;
9020 } tile_tests[] = {
9021 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
9022 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9023 "{ [32,32] }",
9024 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
9025 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9027 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
9028 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9029 "{ [32,32] }",
9030 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9031 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9033 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9034 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9035 "{ [32,32] }",
9036 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
9037 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9039 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9040 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9041 "{ [32,32] }",
9042 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9043 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9047 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
9048 * tile the band and then check if the tile and point bands have the
9049 * expected partial schedule.
9051 static int test_tile(isl_ctx *ctx)
9053 int i;
9054 int scale;
9055 int shift;
9057 scale = isl_options_get_tile_scale_tile_loops(ctx);
9058 shift = isl_options_get_tile_shift_point_loops(ctx);
9060 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
9061 int opt;
9062 int equal;
9063 const char *str;
9064 isl_union_set *domain;
9065 isl_multi_union_pw_aff *mupa, *mupa2;
9066 isl_schedule_node *node;
9067 isl_multi_val *sizes;
9069 opt = tile_tests[i].scale_tile;
9070 isl_options_set_tile_scale_tile_loops(ctx, opt);
9071 opt = tile_tests[i].shift_point;
9072 isl_options_set_tile_shift_point_loops(ctx, opt);
9074 str = tile_tests[i].domain;
9075 domain = isl_union_set_read_from_str(ctx, str);
9076 node = isl_schedule_node_from_domain(domain);
9077 node = isl_schedule_node_child(node, 0);
9078 str = tile_tests[i].schedule;
9079 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9080 node = isl_schedule_node_insert_partial_schedule(node, mupa);
9081 str = tile_tests[i].sizes;
9082 sizes = isl_multi_val_read_from_str(ctx, str);
9083 node = isl_schedule_node_band_tile(node, sizes);
9085 str = tile_tests[i].tile;
9086 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9087 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9088 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
9089 isl_multi_union_pw_aff_free(mupa);
9090 isl_multi_union_pw_aff_free(mupa2);
9092 node = isl_schedule_node_child(node, 0);
9094 str = tile_tests[i].point;
9095 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9096 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9097 if (equal >= 0 && equal)
9098 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
9099 mupa2);
9100 isl_multi_union_pw_aff_free(mupa);
9101 isl_multi_union_pw_aff_free(mupa2);
9103 isl_schedule_node_free(node);
9105 if (equal < 0)
9106 return -1;
9107 if (!equal)
9108 isl_die(ctx, isl_error_unknown,
9109 "unexpected result", return -1);
9112 isl_options_set_tile_scale_tile_loops(ctx, scale);
9113 isl_options_set_tile_shift_point_loops(ctx, shift);
9115 return 0;
9118 /* Check that the domain hash of a space is equal to the hash
9119 * of the domain of the space.
9121 static int test_domain_hash(isl_ctx *ctx)
9123 isl_map *map;
9124 isl_space *space;
9125 uint32_t hash1, hash2;
9127 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
9128 space = isl_map_get_space(map);
9129 isl_map_free(map);
9130 hash1 = isl_space_get_domain_hash(space);
9131 space = isl_space_domain(space);
9132 hash2 = isl_space_get_hash(space);
9133 isl_space_free(space);
9135 if (!space)
9136 return -1;
9137 if (hash1 != hash2)
9138 isl_die(ctx, isl_error_unknown,
9139 "domain hash not equal to hash of domain", return -1);
9141 return 0;
9144 /* Check that a universe basic set that is not obviously equal to the universe
9145 * is still recognized as being equal to the universe.
9147 static int test_universe(isl_ctx *ctx)
9149 const char *s;
9150 isl_basic_set *bset;
9151 isl_bool is_univ;
9153 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
9154 bset = isl_basic_set_read_from_str(ctx, s);
9155 is_univ = isl_basic_set_is_universe(bset);
9156 isl_basic_set_free(bset);
9158 if (is_univ < 0)
9159 return -1;
9160 if (!is_univ)
9161 isl_die(ctx, isl_error_unknown,
9162 "not recognized as universe set", return -1);
9164 return 0;
9167 /* Sets for which chambers are computed and checked.
9169 const char *chambers_tests[] = {
9170 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
9171 "z >= 0 and z <= C - y and z <= B - x - y }",
9174 /* Add the domain of "cell" to "cells".
9176 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
9178 isl_basic_set_list **cells = user;
9179 isl_basic_set *dom;
9181 dom = isl_cell_get_domain(cell);
9182 isl_cell_free(cell);
9183 *cells = isl_basic_set_list_add(*cells, dom);
9185 return *cells ? isl_stat_ok : isl_stat_error;
9188 /* Check that the elements of "list" are pairwise disjoint.
9190 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
9192 int i, j, n;
9194 if (!list)
9195 return isl_stat_error;
9197 n = isl_basic_set_list_n_basic_set(list);
9198 for (i = 0; i < n; ++i) {
9199 isl_basic_set *bset_i;
9201 bset_i = isl_basic_set_list_get_basic_set(list, i);
9202 for (j = i + 1; j < n; ++j) {
9203 isl_basic_set *bset_j;
9204 isl_bool disjoint;
9206 bset_j = isl_basic_set_list_get_basic_set(list, j);
9207 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
9208 isl_basic_set_free(bset_j);
9209 if (!disjoint)
9210 isl_die(isl_basic_set_list_get_ctx(list),
9211 isl_error_unknown, "not disjoint",
9212 break);
9213 if (disjoint < 0 || !disjoint)
9214 break;
9216 isl_basic_set_free(bset_i);
9217 if (j < n)
9218 return isl_stat_error;
9221 return isl_stat_ok;
9224 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
9225 * are pairwise disjoint.
9227 static int test_chambers(isl_ctx *ctx)
9229 int i;
9231 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
9232 isl_basic_set *bset;
9233 isl_vertices *vertices;
9234 isl_basic_set_list *cells;
9235 isl_stat ok;
9237 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
9238 vertices = isl_basic_set_compute_vertices(bset);
9239 cells = isl_basic_set_list_alloc(ctx, 0);
9240 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
9241 &cells) < 0)
9242 cells = isl_basic_set_list_free(cells);
9243 ok = check_pairwise_disjoint(cells);
9244 isl_basic_set_list_free(cells);
9245 isl_vertices_free(vertices);
9246 isl_basic_set_free(bset);
9248 if (ok < 0)
9249 return -1;
9252 return 0;
9255 struct {
9256 const char *name;
9257 int (*fn)(isl_ctx *ctx);
9258 } tests [] = {
9259 { "universe", &test_universe },
9260 { "domain hash", &test_domain_hash },
9261 { "dual", &test_dual },
9262 { "dependence analysis", &test_flow },
9263 { "val", &test_val },
9264 { "compute divs", &test_compute_divs },
9265 { "partial lexmin", &test_partial_lexmin },
9266 { "simplify", &test_simplify },
9267 { "curry", &test_curry },
9268 { "piecewise multi affine expressions", &test_pw_multi_aff },
9269 { "multi piecewise affine expressions", &test_multi_pw_aff },
9270 { "conversion", &test_conversion },
9271 { "list", &test_list },
9272 { "align parameters", &test_align_parameters },
9273 { "drop unused parameters", &test_drop_unused_parameters },
9274 { "preimage", &test_preimage },
9275 { "pullback", &test_pullback },
9276 { "AST", &test_ast },
9277 { "AST build", &test_ast_build },
9278 { "AST generation", &test_ast_gen },
9279 { "eliminate", &test_eliminate },
9280 { "residue class", &test_residue_class },
9281 { "div", &test_div },
9282 { "slice", &test_slice },
9283 { "fixed power", &test_fixed_power },
9284 { "sample", &test_sample },
9285 { "output", &test_output },
9286 { "vertices", &test_vertices },
9287 { "chambers", &test_chambers },
9288 { "fixed", &test_fixed },
9289 { "equal", &test_equal },
9290 { "disjoint", &test_disjoint },
9291 { "product", &test_product },
9292 { "dim_max", &test_dim_max },
9293 { "affine", &test_aff },
9294 { "injective", &test_injective },
9295 { "schedule (whole component)", &test_schedule_whole },
9296 { "schedule (incremental)", &test_schedule_incremental },
9297 { "schedule tree", &test_schedule_tree },
9298 { "schedule tree prefix", &test_schedule_tree_prefix },
9299 { "schedule tree grouping", &test_schedule_tree_group },
9300 { "tile", &test_tile },
9301 { "union_pw", &test_union_pw },
9302 { "locus", &test_locus },
9303 { "eval", &test_eval },
9304 { "parse", &test_parse },
9305 { "single-valued", &test_sv },
9306 { "affine hull", &test_affine_hull },
9307 { "simple_hull", &test_simple_hull },
9308 { "coalesce", &test_coalesce },
9309 { "factorize", &test_factorize },
9310 { "subset", &test_subset },
9311 { "subtract", &test_subtract },
9312 { "intersect", &test_intersect },
9313 { "lexmin", &test_lexmin },
9314 { "min", &test_min },
9315 { "gist", &test_gist },
9316 { "piecewise quasi-polynomials", &test_pwqp },
9317 { "lift", &test_lift },
9318 { "bound", &test_bound },
9319 { "get lists", &test_get_list },
9320 { "union", &test_union },
9321 { "split periods", &test_split_periods },
9322 { "lexicographic order", &test_lex },
9323 { "bijectivity", &test_bijective },
9324 { "dataflow analysis", &test_dep },
9325 { "reading", &test_read },
9326 { "bounded", &test_bounded },
9327 { "construction", &test_construction },
9328 { "dimension manipulation", &test_dim },
9329 { "map application", &test_application },
9330 { "convex hull", &test_convex_hull },
9331 { "transitive closure", &test_closure },
9332 { "isl_bool", &test_isl_bool},
9335 int main(int argc, char **argv)
9337 int i;
9338 struct isl_ctx *ctx;
9339 struct isl_options *options;
9341 options = isl_options_new_with_defaults();
9342 assert(options);
9343 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
9345 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
9346 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
9347 printf("%s\n", tests[i].name);
9348 if (tests[i].fn(ctx) < 0)
9349 goto error;
9351 isl_ctx_free(ctx);
9352 return 0;
9353 error:
9354 isl_ctx_free(ctx);
9355 return -1;