isl_poly_is_neginfty: use isl_bool_ok
[isl.git] / isl_test.c
blob9aa33d874f4a33ae773192439da381372cb5899a
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 isl_size 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 (n1 < 0 || n2 < 0)
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;
3868 isl_size n;
3870 D = isl_union_set_read_from_str(ctx, d);
3871 W = isl_union_map_read_from_str(ctx, w);
3872 R = isl_union_map_read_from_str(ctx, r);
3873 S = isl_union_map_read_from_str(ctx, s);
3875 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3876 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3878 empty = isl_union_map_empty(isl_union_map_get_space(S));
3879 isl_union_map_compute_flow(isl_union_map_copy(R),
3880 isl_union_map_copy(W), empty,
3881 isl_union_map_copy(S),
3882 &dep_raw, NULL, NULL, NULL);
3883 isl_union_map_compute_flow(isl_union_map_copy(W),
3884 isl_union_map_copy(W),
3885 isl_union_map_copy(R),
3886 isl_union_map_copy(S),
3887 &dep_waw, &dep_war, NULL, NULL);
3889 dep = isl_union_map_union(dep_waw, dep_war);
3890 dep = isl_union_map_union(dep, dep_raw);
3891 validity = isl_union_map_copy(dep);
3892 coincidence = isl_union_map_copy(dep);
3893 proximity = isl_union_map_copy(dep);
3895 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3896 sc = isl_schedule_constraints_set_validity(sc, validity);
3897 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3898 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3899 sched = isl_schedule_constraints_compute_schedule(sc);
3900 schedule = isl_schedule_get_map(sched);
3901 isl_schedule_free(sched);
3902 isl_union_map_free(W);
3903 isl_union_map_free(R);
3904 isl_union_map_free(S);
3906 is_injection = 1;
3907 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3909 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3910 is_complete = isl_union_set_is_subset(D, domain);
3911 isl_union_set_free(D);
3912 isl_union_set_free(domain);
3914 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3915 test = isl_union_map_apply_range(test, dep);
3916 test = isl_union_map_apply_range(test, schedule);
3918 delta = isl_union_map_deltas(test);
3919 n = isl_union_set_n_set(delta);
3920 if (n < 0) {
3921 isl_union_set_free(delta);
3922 return -1;
3924 if (n == 0) {
3925 is_tilable = 1;
3926 is_parallel = 1;
3927 is_nonneg = 1;
3928 isl_union_set_free(delta);
3929 } else {
3930 isl_size dim;
3932 delta_set = isl_set_from_union_set(delta);
3934 slice = isl_set_universe(isl_set_get_space(delta_set));
3935 for (i = 0; i < tilable; ++i)
3936 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3937 is_tilable = isl_set_is_subset(delta_set, slice);
3938 isl_set_free(slice);
3940 slice = isl_set_universe(isl_set_get_space(delta_set));
3941 for (i = 0; i < parallel; ++i)
3942 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3943 is_parallel = isl_set_is_subset(delta_set, slice);
3944 isl_set_free(slice);
3946 origin = isl_set_universe(isl_set_get_space(delta_set));
3947 dim = isl_set_dim(origin, isl_dim_set);
3948 if (dim < 0)
3949 origin = isl_set_free(origin);
3950 for (i = 0; i < dim; ++i)
3951 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3953 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3954 delta_set = isl_set_lexmin(delta_set);
3956 is_nonneg = isl_set_is_equal(delta_set, origin);
3958 isl_set_free(origin);
3959 isl_set_free(delta_set);
3962 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3963 is_injection < 0 || is_complete < 0)
3964 return -1;
3965 if (!is_complete)
3966 isl_die(ctx, isl_error_unknown,
3967 "generated schedule incomplete", return -1);
3968 if (!is_injection)
3969 isl_die(ctx, isl_error_unknown,
3970 "generated schedule not injective on each statement",
3971 return -1);
3972 if (!is_nonneg)
3973 isl_die(ctx, isl_error_unknown,
3974 "negative dependences in generated schedule",
3975 return -1);
3976 if (!is_tilable)
3977 isl_die(ctx, isl_error_unknown,
3978 "generated schedule not as tilable as expected",
3979 return -1);
3980 if (!is_parallel)
3981 isl_die(ctx, isl_error_unknown,
3982 "generated schedule not as parallel as expected",
3983 return -1);
3985 return 0;
3988 /* Compute a schedule for the given instance set, validity constraints,
3989 * proximity constraints and context and return a corresponding union map
3990 * representation.
3992 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3993 const char *domain, const char *validity, const char *proximity,
3994 const char *context)
3996 isl_set *con;
3997 isl_union_set *dom;
3998 isl_union_map *dep;
3999 isl_union_map *prox;
4000 isl_schedule_constraints *sc;
4001 isl_schedule *schedule;
4002 isl_union_map *sched;
4004 con = isl_set_read_from_str(ctx, context);
4005 dom = isl_union_set_read_from_str(ctx, domain);
4006 dep = isl_union_map_read_from_str(ctx, validity);
4007 prox = isl_union_map_read_from_str(ctx, proximity);
4008 sc = isl_schedule_constraints_on_domain(dom);
4009 sc = isl_schedule_constraints_set_context(sc, con);
4010 sc = isl_schedule_constraints_set_validity(sc, dep);
4011 sc = isl_schedule_constraints_set_proximity(sc, prox);
4012 schedule = isl_schedule_constraints_compute_schedule(sc);
4013 sched = isl_schedule_get_map(schedule);
4014 isl_schedule_free(schedule);
4016 return sched;
4019 /* Compute a schedule for the given instance set, validity constraints and
4020 * proximity constraints and return a corresponding union map representation.
4022 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
4023 const char *domain, const char *validity, const char *proximity)
4025 return compute_schedule_with_context(ctx, domain, validity, proximity,
4026 "{ : }");
4029 /* Check that a schedule can be constructed on the given domain
4030 * with the given validity and proximity constraints.
4032 static int test_has_schedule(isl_ctx *ctx, const char *domain,
4033 const char *validity, const char *proximity)
4035 isl_union_map *sched;
4037 sched = compute_schedule(ctx, domain, validity, proximity);
4038 if (!sched)
4039 return -1;
4041 isl_union_map_free(sched);
4042 return 0;
4045 int test_special_schedule(isl_ctx *ctx, const char *domain,
4046 const char *validity, const char *proximity, const char *expected_sched)
4048 isl_union_map *sched1, *sched2;
4049 int equal;
4051 sched1 = compute_schedule(ctx, domain, validity, proximity);
4052 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
4054 equal = isl_union_map_is_equal(sched1, sched2);
4055 isl_union_map_free(sched1);
4056 isl_union_map_free(sched2);
4058 if (equal < 0)
4059 return -1;
4060 if (!equal)
4061 isl_die(ctx, isl_error_unknown, "unexpected schedule",
4062 return -1);
4064 return 0;
4067 /* Check that the schedule map is properly padded, i.e., that the range
4068 * lives in a single space.
4070 static int test_padded_schedule(isl_ctx *ctx)
4072 const char *str;
4073 isl_union_set *D;
4074 isl_union_map *validity, *proximity;
4075 isl_schedule_constraints *sc;
4076 isl_schedule *sched;
4077 isl_union_map *umap;
4078 isl_union_set *range;
4079 isl_set *set;
4081 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
4082 D = isl_union_set_read_from_str(ctx, str);
4083 validity = isl_union_map_empty(isl_union_set_get_space(D));
4084 proximity = isl_union_map_copy(validity);
4085 sc = isl_schedule_constraints_on_domain(D);
4086 sc = isl_schedule_constraints_set_validity(sc, validity);
4087 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4088 sched = isl_schedule_constraints_compute_schedule(sc);
4089 umap = isl_schedule_get_map(sched);
4090 isl_schedule_free(sched);
4091 range = isl_union_map_range(umap);
4092 set = isl_set_from_union_set(range);
4093 isl_set_free(set);
4095 if (!set)
4096 return -1;
4098 return 0;
4101 /* Check that conditional validity constraints are also taken into
4102 * account across bands.
4103 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
4104 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
4105 * and then check that the adjacent order constraint C[2,1]->D[2,0]
4106 * is enforced by the rest of the schedule.
4108 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
4110 const char *str;
4111 isl_union_set *domain;
4112 isl_union_map *validity, *proximity, *condition;
4113 isl_union_map *sink, *source, *dep;
4114 isl_schedule_constraints *sc;
4115 isl_schedule *schedule;
4116 isl_union_access_info *access;
4117 isl_union_flow *flow;
4118 int empty;
4120 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4121 "A[k] : k >= 1 and k <= -1 + n; "
4122 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4123 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
4124 domain = isl_union_set_read_from_str(ctx, str);
4125 sc = isl_schedule_constraints_on_domain(domain);
4126 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
4127 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4128 "D[k, i] -> C[1 + k, i] : "
4129 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4130 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
4131 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
4132 validity = isl_union_map_read_from_str(ctx, str);
4133 sc = isl_schedule_constraints_set_validity(sc, validity);
4134 str = "[n] -> { C[k, i] -> D[k, i] : "
4135 "0 <= i <= -1 + k and k <= -1 + n }";
4136 proximity = isl_union_map_read_from_str(ctx, str);
4137 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4138 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
4139 "i <= -1 + k and i >= 1 and k <= -2 + n; "
4140 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
4141 "k <= -1 + n and i >= 0 and i <= -2 + k }";
4142 condition = isl_union_map_read_from_str(ctx, str);
4143 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
4144 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4145 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
4146 "i >= 0 and i <= -1 + k and k <= -1 + n and "
4147 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
4148 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
4149 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4150 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
4151 "k <= -1 + n and i >= 0 and i <= -1 + k and "
4152 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
4153 validity = isl_union_map_read_from_str(ctx, str);
4154 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4155 validity);
4156 schedule = isl_schedule_constraints_compute_schedule(sc);
4157 str = "{ D[2,0] -> [] }";
4158 sink = isl_union_map_read_from_str(ctx, str);
4159 access = isl_union_access_info_from_sink(sink);
4160 str = "{ C[2,1] -> [] }";
4161 source = isl_union_map_read_from_str(ctx, str);
4162 access = isl_union_access_info_set_must_source(access, source);
4163 access = isl_union_access_info_set_schedule(access, schedule);
4164 flow = isl_union_access_info_compute_flow(access);
4165 dep = isl_union_flow_get_must_dependence(flow);
4166 isl_union_flow_free(flow);
4167 empty = isl_union_map_is_empty(dep);
4168 isl_union_map_free(dep);
4170 if (empty < 0)
4171 return -1;
4172 if (empty)
4173 isl_die(ctx, isl_error_unknown,
4174 "conditional validity not respected", return -1);
4176 return 0;
4179 /* Check that the test for violated conditional validity constraints
4180 * is not confused by domain compression.
4181 * In particular, earlier versions of isl would apply
4182 * a schedule on the compressed domains to the original domains,
4183 * resulting in a failure to detect that the default schedule
4184 * violates the conditional validity constraints.
4186 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
4188 const char *str;
4189 isl_bool empty;
4190 isl_union_set *domain;
4191 isl_union_map *validity, *condition;
4192 isl_schedule_constraints *sc;
4193 isl_schedule *schedule;
4194 isl_union_map *umap;
4195 isl_map *map, *ge;
4197 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
4198 domain = isl_union_set_read_from_str(ctx, str);
4199 sc = isl_schedule_constraints_on_domain(domain);
4200 str = "{ B[1, i] -> A[0, i + 1] }";
4201 condition = isl_union_map_read_from_str(ctx, str);
4202 str = "{ A[0, i] -> B[1, i - 1] }";
4203 validity = isl_union_map_read_from_str(ctx, str);
4204 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4205 isl_union_map_copy(validity));
4206 schedule = isl_schedule_constraints_compute_schedule(sc);
4207 umap = isl_schedule_get_map(schedule);
4208 isl_schedule_free(schedule);
4209 validity = isl_union_map_apply_domain(validity,
4210 isl_union_map_copy(umap));
4211 validity = isl_union_map_apply_range(validity, umap);
4212 map = isl_map_from_union_map(validity);
4213 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4214 map = isl_map_intersect(map, ge);
4215 empty = isl_map_is_empty(map);
4216 isl_map_free(map);
4218 if (empty < 0)
4219 return -1;
4220 if (!empty)
4221 isl_die(ctx, isl_error_unknown,
4222 "conditional validity constraints not satisfied",
4223 return -1);
4225 return 0;
4228 /* Input for testing of schedule construction based on
4229 * conditional constraints.
4231 * domain is the iteration domain
4232 * flow are the flow dependences, which determine the validity and
4233 * proximity constraints
4234 * condition are the conditions on the conditional validity constraints
4235 * conditional_validity are the conditional validity constraints
4236 * outer_band_n is the expected number of members in the outer band
4238 struct {
4239 const char *domain;
4240 const char *flow;
4241 const char *condition;
4242 const char *conditional_validity;
4243 int outer_band_n;
4244 } live_range_tests[] = {
4245 /* Contrived example that illustrates that we need to keep
4246 * track of tagged condition dependences and
4247 * tagged conditional validity dependences
4248 * in isl_sched_edge separately.
4249 * In particular, the conditional validity constraints on A
4250 * cannot be satisfied,
4251 * but they can be ignored because there are no corresponding
4252 * condition constraints. However, we do have an additional
4253 * conditional validity constraint that maps to the same
4254 * dependence relation
4255 * as the condition constraint on B. If we did not make a distinction
4256 * between tagged condition and tagged conditional validity
4257 * dependences, then we
4258 * could end up treating this shared dependence as an condition
4259 * constraint on A, forcing a localization of the conditions,
4260 * which is impossible.
4262 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
4263 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
4264 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
4265 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4266 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4267 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
4270 /* TACO 2013 Fig. 7 */
4271 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4272 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4273 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4274 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
4275 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
4276 "0 <= i < n and 0 <= j < n - 1 }",
4277 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
4278 "0 <= i < n and 0 <= j < j' < n;"
4279 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
4280 "0 <= i < i' < n and 0 <= j,j' < n;"
4281 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
4282 "0 <= i,j,j' < n and j < j' }",
4285 /* TACO 2013 Fig. 7, without tags */
4286 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4287 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4288 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4289 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4290 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4291 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
4292 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
4293 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
4296 /* TACO 2013 Fig. 12 */
4297 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
4298 "S3[i,3] : 0 <= i <= 1 }",
4299 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
4300 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
4301 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
4302 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
4303 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4304 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
4305 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4306 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
4307 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
4308 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
4309 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
4314 /* Test schedule construction based on conditional constraints.
4315 * In particular, check the number of members in the outer band node
4316 * as an indication of whether tiling is possible or not.
4318 static int test_conditional_schedule_constraints(isl_ctx *ctx)
4320 int i;
4321 isl_union_set *domain;
4322 isl_union_map *condition;
4323 isl_union_map *flow;
4324 isl_union_map *validity;
4325 isl_schedule_constraints *sc;
4326 isl_schedule *schedule;
4327 isl_schedule_node *node;
4328 isl_size n_member;
4330 if (test_special_conditional_schedule_constraints(ctx) < 0)
4331 return -1;
4332 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
4333 return -1;
4335 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
4336 domain = isl_union_set_read_from_str(ctx,
4337 live_range_tests[i].domain);
4338 flow = isl_union_map_read_from_str(ctx,
4339 live_range_tests[i].flow);
4340 condition = isl_union_map_read_from_str(ctx,
4341 live_range_tests[i].condition);
4342 validity = isl_union_map_read_from_str(ctx,
4343 live_range_tests[i].conditional_validity);
4344 sc = isl_schedule_constraints_on_domain(domain);
4345 sc = isl_schedule_constraints_set_validity(sc,
4346 isl_union_map_copy(flow));
4347 sc = isl_schedule_constraints_set_proximity(sc, flow);
4348 sc = isl_schedule_constraints_set_conditional_validity(sc,
4349 condition, validity);
4350 schedule = isl_schedule_constraints_compute_schedule(sc);
4351 node = isl_schedule_get_root(schedule);
4352 while (node &&
4353 isl_schedule_node_get_type(node) != isl_schedule_node_band)
4354 node = isl_schedule_node_first_child(node);
4355 n_member = isl_schedule_node_band_n_member(node);
4356 isl_schedule_node_free(node);
4357 isl_schedule_free(schedule);
4359 if (!schedule || n_member < 0)
4360 return -1;
4361 if (n_member != live_range_tests[i].outer_band_n)
4362 isl_die(ctx, isl_error_unknown,
4363 "unexpected number of members in outer band",
4364 return -1);
4366 return 0;
4369 /* Check that the schedule computed for the given instance set and
4370 * dependence relation strongly satisfies the dependences.
4371 * In particular, check that no instance is scheduled before
4372 * or together with an instance on which it depends.
4373 * Earlier versions of isl would produce a schedule that
4374 * only weakly satisfies the dependences.
4376 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
4378 const char *domain, *dep;
4379 isl_union_map *D, *schedule;
4380 isl_map *map, *ge;
4381 int empty;
4383 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
4384 "A[i0] : 0 <= i0 <= 1 }";
4385 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
4386 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
4387 schedule = compute_schedule(ctx, domain, dep, dep);
4388 D = isl_union_map_read_from_str(ctx, dep);
4389 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
4390 D = isl_union_map_apply_range(D, schedule);
4391 map = isl_map_from_union_map(D);
4392 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4393 map = isl_map_intersect(map, ge);
4394 empty = isl_map_is_empty(map);
4395 isl_map_free(map);
4397 if (empty < 0)
4398 return -1;
4399 if (!empty)
4400 isl_die(ctx, isl_error_unknown,
4401 "dependences not strongly satisfied", return -1);
4403 return 0;
4406 /* Compute a schedule for input where the instance set constraints
4407 * conflict with the context constraints.
4408 * Earlier versions of isl did not properly handle this situation.
4410 static int test_conflicting_context_schedule(isl_ctx *ctx)
4412 isl_union_map *schedule;
4413 const char *domain, *context;
4415 domain = "[n] -> { A[] : n >= 0 }";
4416 context = "[n] -> { : n < 0 }";
4417 schedule = compute_schedule_with_context(ctx,
4418 domain, "{}", "{}", context);
4419 isl_union_map_free(schedule);
4421 if (!schedule)
4422 return -1;
4424 return 0;
4427 /* Check that a set of schedule constraints that only allow for
4428 * a coalescing schedule still produces a schedule even if the user
4429 * request a non-coalescing schedule. Earlier versions of isl
4430 * would not handle this case correctly.
4432 static int test_coalescing_schedule(isl_ctx *ctx)
4434 const char *domain, *dep;
4435 isl_union_set *I;
4436 isl_union_map *D;
4437 isl_schedule_constraints *sc;
4438 isl_schedule *schedule;
4439 int treat_coalescing;
4441 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4442 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4443 I = isl_union_set_read_from_str(ctx, domain);
4444 D = isl_union_map_read_from_str(ctx, dep);
4445 sc = isl_schedule_constraints_on_domain(I);
4446 sc = isl_schedule_constraints_set_validity(sc, D);
4447 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4448 isl_options_set_schedule_treat_coalescing(ctx, 1);
4449 schedule = isl_schedule_constraints_compute_schedule(sc);
4450 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4451 isl_schedule_free(schedule);
4452 if (!schedule)
4453 return -1;
4454 return 0;
4457 /* Check that the scheduler does not perform any needless
4458 * compound skewing. Earlier versions of isl would compute
4459 * schedules in terms of transformed schedule coefficients and
4460 * would not accurately keep track of the sum of the original
4461 * schedule coefficients. It could then produce the schedule
4462 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
4463 * for the input below instead of the schedule below.
4465 static int test_skewing_schedule(isl_ctx *ctx)
4467 const char *D, *V, *P, *S;
4469 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
4470 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
4471 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
4472 "-1 <= a-i + b-j + c-k <= 1 }";
4473 P = "{ }";
4474 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
4476 return test_special_schedule(ctx, D, V, P, S);
4479 int test_schedule(isl_ctx *ctx)
4481 const char *D, *W, *R, *V, *P, *S;
4482 int max_coincidence;
4483 int treat_coalescing;
4485 /* Handle resulting schedule with zero bands. */
4486 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4487 return -1;
4489 /* Jacobi */
4490 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4491 W = "{ S1[t,i] -> a[t,i] }";
4492 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4493 "S1[t,i] -> a[t-1,i+1] }";
4494 S = "{ S1[t,i] -> [t,i] }";
4495 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4496 return -1;
4498 /* Fig. 5 of CC2008 */
4499 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
4500 "j <= -1 + N }";
4501 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
4502 "j >= 2 and j <= -1 + N }";
4503 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
4504 "j >= 2 and j <= -1 + N; "
4505 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
4506 "j >= 2 and j <= -1 + N }";
4507 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4508 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4509 return -1;
4511 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
4512 W = "{ S1[i] -> a[i] }";
4513 R = "{ S2[i] -> a[i+1] }";
4514 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4515 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4516 return -1;
4518 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
4519 W = "{ S1[i] -> a[i] }";
4520 R = "{ S2[i] -> a[9-i] }";
4521 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4522 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4523 return -1;
4525 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4526 W = "{ S1[i] -> a[i] }";
4527 R = "[N] -> { S2[i] -> a[N-1-i] }";
4528 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4529 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4530 return -1;
4532 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4533 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4534 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4535 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4536 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4537 return -1;
4539 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4540 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4541 R = "{ S2[i,j] -> a[i-1,j] }";
4542 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4543 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4544 return -1;
4546 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4547 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4548 R = "{ S2[i,j] -> a[i,j-1] }";
4549 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4550 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4551 return -1;
4553 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4554 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4555 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4556 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4557 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4558 "S_0[] -> [0, 0, 0] }";
4559 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4560 return -1;
4561 ctx->opt->schedule_parametric = 0;
4562 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4563 return -1;
4564 ctx->opt->schedule_parametric = 1;
4566 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4567 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4568 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4569 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4570 "S4[i] -> a[i,N] }";
4571 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4572 "S4[i] -> [4,i,0] }";
4573 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4574 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4575 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4576 return -1;
4577 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4579 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4580 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4581 "j <= N }";
4582 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4583 "j <= N; "
4584 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4585 "j <= N }";
4586 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4587 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4588 return -1;
4590 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4591 " S_2[t] : t >= 0 and t <= -1 + N; "
4592 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4593 "i <= -1 + N }";
4594 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4595 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4596 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4597 "i >= 0 and i <= -1 + N }";
4598 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4599 "i >= 0 and i <= -1 + N; "
4600 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4601 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4602 " S_0[t] -> [0, t, 0] }";
4604 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4605 return -1;
4606 ctx->opt->schedule_parametric = 0;
4607 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4608 return -1;
4609 ctx->opt->schedule_parametric = 1;
4611 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4612 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4613 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4614 return -1;
4616 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4617 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4618 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
4619 "j >= 0 and j <= -1 + N; "
4620 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4621 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4622 "j >= 0 and j <= -1 + N; "
4623 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4624 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4625 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4626 return -1;
4628 D = "{ S_0[i] : i >= 0 }";
4629 W = "{ S_0[i] -> a[i] : i >= 0 }";
4630 R = "{ S_0[i] -> a[0] : i >= 0 }";
4631 S = "{ S_0[i] -> [0, i, 0] }";
4632 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4633 return -1;
4635 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4636 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4637 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4638 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4639 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4640 return -1;
4642 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4643 "k <= -1 + n and k >= 0 }";
4644 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4645 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4646 "k <= -1 + n and k >= 0; "
4647 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4648 "k <= -1 + n and k >= 0; "
4649 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4650 "k <= -1 + n and k >= 0 }";
4651 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4652 ctx->opt->schedule_outer_coincidence = 1;
4653 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4654 return -1;
4655 ctx->opt->schedule_outer_coincidence = 0;
4657 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4658 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4659 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4660 "Stmt_for_body24[i0, i1, 1, 0]:"
4661 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4662 "Stmt_for_body7[i0, i1, i2]:"
4663 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4664 "i2 <= 7 }";
4666 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4667 "Stmt_for_body24[1, i1, i2, i3]:"
4668 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4669 "i2 >= 1;"
4670 "Stmt_for_body24[0, i1, i2, i3] -> "
4671 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4672 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4673 "i3 >= 0;"
4674 "Stmt_for_body24[0, i1, i2, i3] ->"
4675 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4676 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4677 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4678 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4679 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4680 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4681 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4682 "i1 <= 6 and i1 >= 0;"
4683 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4684 "Stmt_for_body7[i0, i1, i2] -> "
4685 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4686 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4687 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4688 "Stmt_for_body7[i0, i1, i2] -> "
4689 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4690 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4691 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4692 P = V;
4694 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4695 isl_options_set_schedule_treat_coalescing(ctx, 0);
4696 if (test_has_schedule(ctx, D, V, P) < 0)
4697 return -1;
4698 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4700 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4701 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4702 "j >= 1 and j <= 7;"
4703 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4704 "j >= 1 and j <= 8 }";
4705 P = "{ }";
4706 S = "{ S_0[i, j] -> [i + j, i] }";
4707 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4708 if (test_special_schedule(ctx, D, V, P, S) < 0)
4709 return -1;
4710 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4712 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4713 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4714 "j >= 0 and j <= -1 + i }";
4715 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4716 "i <= -1 + N and j >= 0;"
4717 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4718 "i <= -2 + N }";
4719 P = "{ }";
4720 S = "{ S_0[i, j] -> [i, j] }";
4721 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4722 if (test_special_schedule(ctx, D, V, P, S) < 0)
4723 return -1;
4724 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4726 /* Test both algorithms on a case with only proximity dependences. */
4727 D = "{ S[i,j] : 0 <= i <= 10 }";
4728 V = "{ }";
4729 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4730 S = "{ S[i, j] -> [j, i] }";
4731 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4732 if (test_special_schedule(ctx, D, V, P, S) < 0)
4733 return -1;
4734 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4735 if (test_special_schedule(ctx, D, V, P, S) < 0)
4736 return -1;
4738 D = "{ A[a]; B[] }";
4739 V = "{}";
4740 P = "{ A[a] -> B[] }";
4741 if (test_has_schedule(ctx, D, V, P) < 0)
4742 return -1;
4744 if (test_padded_schedule(ctx) < 0)
4745 return -1;
4747 /* Check that check for progress is not confused by rational
4748 * solution.
4750 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4751 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4752 "i0 <= -2 + N; "
4753 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4754 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4755 P = "{}";
4756 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4757 if (test_has_schedule(ctx, D, V, P) < 0)
4758 return -1;
4759 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4761 /* Check that we allow schedule rows that are only non-trivial
4762 * on some full-dimensional domains.
4764 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4765 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4766 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4767 P = "{}";
4768 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4769 if (test_has_schedule(ctx, D, V, P) < 0)
4770 return -1;
4771 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4773 if (test_conditional_schedule_constraints(ctx) < 0)
4774 return -1;
4776 if (test_strongly_satisfying_schedule(ctx) < 0)
4777 return -1;
4779 if (test_conflicting_context_schedule(ctx) < 0)
4780 return -1;
4782 if (test_coalescing_schedule(ctx) < 0)
4783 return -1;
4784 if (test_skewing_schedule(ctx) < 0)
4785 return -1;
4787 return 0;
4790 /* Perform scheduling tests using the whole component scheduler.
4792 static int test_schedule_whole(isl_ctx *ctx)
4794 int whole;
4795 int r;
4797 whole = isl_options_get_schedule_whole_component(ctx);
4798 isl_options_set_schedule_whole_component(ctx, 1);
4799 r = test_schedule(ctx);
4800 isl_options_set_schedule_whole_component(ctx, whole);
4802 return r;
4805 /* Perform scheduling tests using the incremental scheduler.
4807 static int test_schedule_incremental(isl_ctx *ctx)
4809 int whole;
4810 int r;
4812 whole = isl_options_get_schedule_whole_component(ctx);
4813 isl_options_set_schedule_whole_component(ctx, 0);
4814 r = test_schedule(ctx);
4815 isl_options_set_schedule_whole_component(ctx, whole);
4817 return r;
4820 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4822 isl_union_map *umap;
4823 int test;
4825 umap = isl_union_map_read_from_str(ctx, str);
4826 test = isl_union_map_plain_is_injective(umap);
4827 isl_union_map_free(umap);
4828 if (test < 0)
4829 return -1;
4830 if (test == injective)
4831 return 0;
4832 if (injective)
4833 isl_die(ctx, isl_error_unknown,
4834 "map not detected as injective", return -1);
4835 else
4836 isl_die(ctx, isl_error_unknown,
4837 "map detected as injective", return -1);
4840 int test_injective(isl_ctx *ctx)
4842 const char *str;
4844 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4845 return -1;
4846 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4847 return -1;
4848 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4849 return -1;
4850 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4851 return -1;
4852 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4853 return -1;
4854 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4855 return -1;
4856 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4857 return -1;
4858 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4859 return -1;
4861 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4862 if (test_plain_injective(ctx, str, 1))
4863 return -1;
4864 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4865 if (test_plain_injective(ctx, str, 0))
4866 return -1;
4868 return 0;
4871 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4873 isl_aff *aff2;
4874 int equal;
4876 if (!aff)
4877 return -1;
4879 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4880 equal = isl_aff_plain_is_equal(aff, aff2);
4881 isl_aff_free(aff2);
4883 return equal;
4886 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4888 int equal;
4890 equal = aff_plain_is_equal(aff, str);
4891 if (equal < 0)
4892 return -1;
4893 if (!equal)
4894 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4895 "result not as expected", return -1);
4896 return 0;
4899 /* Is "upa" obviously equal to the isl_union_pw_aff represented by "str"?
4901 static isl_bool union_pw_aff_plain_is_equal(__isl_keep isl_union_pw_aff *upa,
4902 const char *str)
4904 isl_ctx *ctx;
4905 isl_union_pw_aff *upa2;
4906 isl_bool equal;
4908 if (!upa)
4909 return isl_bool_error;
4911 ctx = isl_union_pw_aff_get_ctx(upa);
4912 upa2 = isl_union_pw_aff_read_from_str(ctx, str);
4913 equal = isl_union_pw_aff_plain_is_equal(upa, upa2);
4914 isl_union_pw_aff_free(upa2);
4916 return equal;
4919 /* Check that "upa" is obviously equal to the isl_union_pw_aff
4920 * represented by "str".
4922 static isl_stat union_pw_aff_check_plain_equal(__isl_keep isl_union_pw_aff *upa,
4923 const char *str)
4925 isl_bool equal;
4927 equal = union_pw_aff_plain_is_equal(upa, str);
4928 if (equal < 0)
4929 return isl_stat_error;
4930 if (!equal)
4931 isl_die(isl_union_pw_aff_get_ctx(upa), isl_error_unknown,
4932 "result not as expected", return isl_stat_error);
4933 return isl_stat_ok;
4936 /* Basic tests on isl_union_pw_aff.
4938 * In particular, check that isl_union_pw_aff_aff_on_domain
4939 * aligns the parameters of the input objects and
4940 * that isl_union_pw_aff_param_on_domain_id properly
4941 * introduces the parameter.
4943 static int test_upa(isl_ctx *ctx)
4945 const char *str;
4946 isl_id *id;
4947 isl_aff *aff;
4948 isl_union_set *domain;
4949 isl_union_pw_aff *upa;
4950 isl_stat ok;
4952 aff = isl_aff_read_from_str(ctx, "[N] -> { [N] }");
4953 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
4954 domain = isl_union_set_read_from_str(ctx, str);
4955 upa = isl_union_pw_aff_aff_on_domain(domain, aff);
4956 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
4957 ok = union_pw_aff_check_plain_equal(upa, str);
4958 isl_union_pw_aff_free(upa);
4959 if (ok < 0)
4960 return -1;
4962 id = isl_id_alloc(ctx, "N", NULL);
4963 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
4964 domain = isl_union_set_read_from_str(ctx, str);
4965 upa = isl_union_pw_aff_param_on_domain_id(domain, id);
4966 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
4967 ok = union_pw_aff_check_plain_equal(upa, str);
4968 isl_union_pw_aff_free(upa);
4969 if (ok < 0)
4970 return -1;
4972 return 0;
4975 struct {
4976 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4977 __isl_take isl_aff *aff2);
4978 } aff_bin_op[] = {
4979 ['+'] = { &isl_aff_add },
4980 ['-'] = { &isl_aff_sub },
4981 ['*'] = { &isl_aff_mul },
4982 ['/'] = { &isl_aff_div },
4985 struct {
4986 const char *arg1;
4987 unsigned char op;
4988 const char *arg2;
4989 const char *res;
4990 } aff_bin_tests[] = {
4991 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4992 "{ [i] -> [2i] }" },
4993 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4994 "{ [i] -> [0] }" },
4995 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4996 "{ [i] -> [2i] }" },
4997 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4998 "{ [i] -> [2i] }" },
4999 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
5000 "{ [i] -> [i/2] }" },
5001 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
5002 "{ [i] -> [i] }" },
5003 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
5004 "{ [i] -> [NaN] }" },
5005 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
5006 "{ [i] -> [NaN] }" },
5007 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
5008 "{ [i] -> [NaN] }" },
5009 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
5010 "{ [i] -> [NaN] }" },
5011 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
5012 "{ [i] -> [NaN] }" },
5013 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
5014 "{ [i] -> [NaN] }" },
5015 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
5016 "{ [i] -> [NaN] }" },
5017 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
5018 "{ [i] -> [NaN] }" },
5019 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
5020 "{ [i] -> [NaN] }" },
5021 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
5022 "{ [i] -> [NaN] }" },
5023 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
5024 "{ [i] -> [NaN] }" },
5025 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
5026 "{ [i] -> [NaN] }" },
5029 /* Perform some basic tests of binary operations on isl_aff objects.
5031 static int test_bin_aff(isl_ctx *ctx)
5033 int i;
5034 isl_aff *aff1, *aff2, *res;
5035 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5036 __isl_take isl_aff *aff2);
5037 int ok;
5039 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
5040 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
5041 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
5042 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
5043 fn = aff_bin_op[aff_bin_tests[i].op].fn;
5044 aff1 = fn(aff1, aff2);
5045 if (isl_aff_is_nan(res))
5046 ok = isl_aff_is_nan(aff1);
5047 else
5048 ok = isl_aff_plain_is_equal(aff1, res);
5049 isl_aff_free(aff1);
5050 isl_aff_free(res);
5051 if (ok < 0)
5052 return -1;
5053 if (!ok)
5054 isl_die(ctx, isl_error_unknown,
5055 "unexpected result", return -1);
5058 return 0;
5061 struct {
5062 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
5063 __isl_take isl_pw_aff *pa2);
5064 } pw_aff_bin_op[] = {
5065 ['m'] = { &isl_pw_aff_min },
5066 ['M'] = { &isl_pw_aff_max },
5069 /* Inputs for binary isl_pw_aff operation tests.
5070 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
5071 * defined by pw_aff_bin_op, and "res" is the expected result.
5073 struct {
5074 const char *arg1;
5075 unsigned char op;
5076 const char *arg2;
5077 const char *res;
5078 } pw_aff_bin_tests[] = {
5079 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
5080 "{ [i] -> [i] }" },
5081 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
5082 "{ [i] -> [i] }" },
5083 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
5084 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
5085 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
5086 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
5087 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
5088 "{ [i] -> [NaN] }" },
5089 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
5090 "{ [i] -> [NaN] }" },
5093 /* Perform some basic tests of binary operations on isl_pw_aff objects.
5095 static int test_bin_pw_aff(isl_ctx *ctx)
5097 int i;
5098 isl_bool ok;
5099 isl_pw_aff *pa1, *pa2, *res;
5101 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
5102 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
5103 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
5104 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
5105 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
5106 if (isl_pw_aff_involves_nan(res))
5107 ok = isl_pw_aff_involves_nan(pa1);
5108 else
5109 ok = isl_pw_aff_plain_is_equal(pa1, res);
5110 isl_pw_aff_free(pa1);
5111 isl_pw_aff_free(res);
5112 if (ok < 0)
5113 return -1;
5114 if (!ok)
5115 isl_die(ctx, isl_error_unknown,
5116 "unexpected result", return -1);
5119 return 0;
5122 struct {
5123 __isl_give isl_union_pw_multi_aff *(*fn)(
5124 __isl_take isl_union_pw_multi_aff *upma1,
5125 __isl_take isl_union_pw_multi_aff *upma2);
5126 const char *arg1;
5127 const char *arg2;
5128 const char *res;
5129 } upma_bin_tests[] = {
5130 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
5131 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
5132 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
5133 "{ B[x] -> [2] : x >= 0 }",
5134 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
5135 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
5136 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
5137 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
5138 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
5139 "D[i] -> B[2] : i >= 5 }" },
5140 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5141 "{ B[x] -> C[2] : x > 0 }",
5142 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
5143 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5144 "{ B[x] -> A[2] : x >= 0 }",
5145 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
5148 /* Perform some basic tests of binary operations on
5149 * isl_union_pw_multi_aff objects.
5151 static int test_bin_upma(isl_ctx *ctx)
5153 int i;
5154 isl_union_pw_multi_aff *upma1, *upma2, *res;
5155 int ok;
5157 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
5158 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5159 upma_bin_tests[i].arg1);
5160 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5161 upma_bin_tests[i].arg2);
5162 res = isl_union_pw_multi_aff_read_from_str(ctx,
5163 upma_bin_tests[i].res);
5164 upma1 = upma_bin_tests[i].fn(upma1, upma2);
5165 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
5166 isl_union_pw_multi_aff_free(upma1);
5167 isl_union_pw_multi_aff_free(res);
5168 if (ok < 0)
5169 return -1;
5170 if (!ok)
5171 isl_die(ctx, isl_error_unknown,
5172 "unexpected result", return -1);
5175 return 0;
5178 struct {
5179 __isl_give isl_union_pw_multi_aff *(*fn)(
5180 __isl_take isl_union_pw_multi_aff *upma1,
5181 __isl_take isl_union_pw_multi_aff *upma2);
5182 const char *arg1;
5183 const char *arg2;
5184 } upma_bin_fail_tests[] = {
5185 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5186 "{ B[x] -> C[2] : x >= 0 }" },
5189 /* Perform some basic tests of binary operations on
5190 * isl_union_pw_multi_aff objects that are expected to fail.
5192 static int test_bin_upma_fail(isl_ctx *ctx)
5194 int i, n;
5195 isl_union_pw_multi_aff *upma1, *upma2;
5196 int on_error;
5198 on_error = isl_options_get_on_error(ctx);
5199 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
5200 n = ARRAY_SIZE(upma_bin_fail_tests);
5201 for (i = 0; i < n; ++i) {
5202 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5203 upma_bin_fail_tests[i].arg1);
5204 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5205 upma_bin_fail_tests[i].arg2);
5206 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
5207 isl_union_pw_multi_aff_free(upma1);
5208 if (upma1)
5209 break;
5211 isl_options_set_on_error(ctx, on_error);
5212 if (i < n)
5213 isl_die(ctx, isl_error_unknown,
5214 "operation not expected to succeed", return -1);
5216 return 0;
5219 /* Inputs for basic tests of unary operations on isl_multi_pw_aff objects.
5220 * "fn" is the function that is tested.
5221 * "arg" is a string description of the input.
5222 * "res" is a string description of the expected result.
5224 struct {
5225 __isl_give isl_multi_pw_aff *(*fn)(__isl_take isl_multi_pw_aff *mpa);
5226 const char *arg;
5227 const char *res;
5228 } mpa_un_tests[] = {
5229 { &isl_multi_pw_aff_range_factor_domain,
5230 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5231 "{ A[x] -> B[(1 : x >= 5)] }" },
5232 { &isl_multi_pw_aff_range_factor_range,
5233 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5234 "{ A[y] -> C[(2 : y <= 10)] }" },
5235 { &isl_multi_pw_aff_range_factor_domain,
5236 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5237 "{ A[x] -> B[(1 : x >= 5)] }" },
5238 { &isl_multi_pw_aff_range_factor_range,
5239 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5240 "{ A[y] -> C[] }" },
5241 { &isl_multi_pw_aff_range_factor_domain,
5242 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5243 "{ A[x] -> B[] }" },
5244 { &isl_multi_pw_aff_range_factor_range,
5245 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5246 "{ A[y] -> C[(2 : y <= 10)] }" },
5247 { &isl_multi_pw_aff_range_factor_domain,
5248 "{ A[x] -> [B[] -> C[]] }",
5249 "{ A[x] -> B[] }" },
5250 { &isl_multi_pw_aff_range_factor_range,
5251 "{ A[x] -> [B[] -> C[]] }",
5252 "{ A[y] -> C[] }" },
5253 { &isl_multi_pw_aff_factor_range,
5254 "{ [B[] -> C[]] }",
5255 "{ C[] }" },
5256 { &isl_multi_pw_aff_range_factor_domain,
5257 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5258 "{ A[x] -> B[] : x >= 0 }" },
5259 { &isl_multi_pw_aff_range_factor_range,
5260 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5261 "{ A[y] -> C[] : y >= 0 }" },
5262 { &isl_multi_pw_aff_factor_range,
5263 "[N] -> { [B[] -> C[]] : N >= 0 }",
5264 "[N] -> { C[] : N >= 0 }" },
5267 /* Perform some basic tests of unary operations on isl_multi_pw_aff objects.
5269 static int test_un_mpa(isl_ctx *ctx)
5271 int i;
5272 isl_bool ok;
5273 isl_multi_pw_aff *mpa, *res;
5275 for (i = 0; i < ARRAY_SIZE(mpa_un_tests); ++i) {
5276 mpa = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].arg);
5277 res = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].res);
5278 mpa = mpa_un_tests[i].fn(mpa);
5279 ok = isl_multi_pw_aff_plain_is_equal(mpa, res);
5280 isl_multi_pw_aff_free(mpa);
5281 isl_multi_pw_aff_free(res);
5282 if (ok < 0)
5283 return -1;
5284 if (!ok)
5285 isl_die(ctx, isl_error_unknown,
5286 "unexpected result", return -1);
5289 return 0;
5292 /* Inputs for basic tests of binary operations on isl_multi_pw_aff objects.
5293 * "fn" is the function that is tested.
5294 * "arg1" and "arg2" are string descriptions of the inputs.
5295 * "res" is a string description of the expected result.
5297 struct {
5298 __isl_give isl_multi_pw_aff *(*fn)(
5299 __isl_take isl_multi_pw_aff *mpa1,
5300 __isl_take isl_multi_pw_aff *mpa2);
5301 const char *arg1;
5302 const char *arg2;
5303 const char *res;
5304 } mpa_bin_tests[] = {
5305 { &isl_multi_pw_aff_add, "{ A[] -> [1] }", "{ A[] -> [2] }",
5306 "{ A[] -> [3] }" },
5307 { &isl_multi_pw_aff_add, "{ A[x] -> [(1 : x >= 5)] }",
5308 "{ A[x] -> [(x : x <= 10)] }",
5309 "{ A[x] -> [(1 + x : 5 <= x <= 10)] }" },
5310 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5311 "{ A[x] -> [] : x <= 10 }",
5312 "{ A[x] -> [] : 5 <= x <= 10 }" },
5313 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5314 "[N] -> { A[x] -> [] : x <= N }",
5315 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5316 { &isl_multi_pw_aff_add,
5317 "[N] -> { A[x] -> [] : x <= N }",
5318 "{ A[x] -> [] : x >= 5 }",
5319 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5320 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5321 "{ A[y] -> C[(2 : y <= 10)] }",
5322 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5323 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5324 "{ A[y] -> C[2] : y <= 10 }",
5325 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5326 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5327 "[N] -> { A[y] -> C[2] : y <= N }",
5328 "[N] -> { A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= N)]] }" },
5329 { &isl_multi_pw_aff_range_product, "[N] -> { A[x] -> B[1] : x >= N }",
5330 "{ A[y] -> C[2] : y <= 10 }",
5331 "[N] -> { A[x] -> [B[(1 : x >= N)] -> C[(2 : x <= 10)]] }" },
5332 { &isl_multi_pw_aff_range_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5333 "{ A[] -> [B[1] -> C[2]] }" },
5334 { &isl_multi_pw_aff_range_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5335 "{ A[] -> [B[] -> C[]] }" },
5336 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5337 "{ A[y] -> C[] : y <= 10 }",
5338 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] : x <= 10 }" },
5339 { &isl_multi_pw_aff_range_product, "{ A[y] -> C[] : y <= 10 }",
5340 "{ A[x] -> B[(1 : x >= 5)] }",
5341 "{ A[x] -> [C[] -> B[(1 : x >= 5)]] : x <= 10 }" },
5342 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5343 "{ A[y] -> C[(2 : y <= 10)] }",
5344 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[(2 : y <= 10)]] }" },
5345 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5346 "{ A[y] -> C[] : y <= 10 }",
5347 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= 10 }" },
5348 { &isl_multi_pw_aff_product, "{ A[y] -> C[] : y <= 10 }",
5349 "{ A[x] -> B[(1 : x >= 5)] }",
5350 "{ [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= 10 }" },
5351 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5352 "[N] -> { A[y] -> C[] : y <= N }",
5353 "[N] -> { [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= N }" },
5354 { &isl_multi_pw_aff_product, "[N] -> { A[y] -> C[] : y <= N }",
5355 "{ A[x] -> B[(1 : x >= 5)] }",
5356 "[N] -> { [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= N }" },
5357 { &isl_multi_pw_aff_product, "{ A[x] -> B[] : x >= 5 }",
5358 "{ A[y] -> C[] : y <= 10 }",
5359 "{ [A[x] -> A[y]] -> [B[] -> C[]] : x >= 5 and y <= 10 }" },
5360 { &isl_multi_pw_aff_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5361 "{ [A[] -> A[]] -> [B[1] -> C[2]] }" },
5362 { &isl_multi_pw_aff_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5363 "{ [A[] -> A[]] -> [B[] -> C[]] }" },
5364 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5365 "{ B[i,j] -> C[i + 2j] }", "{ A[a,b] -> B[b,a] }",
5366 "{ A[a,b] -> C[b + 2a] }" },
5367 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5368 "{ B[i,j] -> C[i + 2j] }",
5369 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5370 "{ A[a,b] -> C[(b + 2a : b > a)] }" },
5371 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5372 "{ B[i,j] -> C[(i + 2j : j > 4)] }",
5373 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5374 "{ A[a,b] -> C[(b + 2a : b > a > 4)] }" },
5375 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5376 "{ B[i,j] -> C[] }",
5377 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5378 "{ A[a,b] -> C[] }" },
5379 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5380 "{ B[i,j] -> C[] : i > j }",
5381 "{ A[a,b] -> B[b,a] }",
5382 "{ A[a,b] -> C[] : b > a }" },
5383 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5384 "{ B[i,j] -> C[] : j > 5 }",
5385 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5386 "{ A[a,b] -> C[] : b > a > 5 }" },
5387 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5388 "[N] -> { B[i,j] -> C[] : j > N }",
5389 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5390 "[N] -> { A[a,b] -> C[] : b > a > N }" },
5391 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5392 "[M,N] -> { B[] -> C[] : N > 5 }",
5393 "[M,N] -> { A[] -> B[] : M > N }",
5394 "[M,N] -> { A[] -> C[] : M > N > 5 }" },
5397 /* Perform some basic tests of binary operations on isl_multi_pw_aff objects.
5399 static int test_bin_mpa(isl_ctx *ctx)
5401 int i;
5402 isl_bool ok;
5403 isl_multi_pw_aff *mpa1, *mpa2, *res;
5405 for (i = 0; i < ARRAY_SIZE(mpa_bin_tests); ++i) {
5406 mpa1 = isl_multi_pw_aff_read_from_str(ctx,
5407 mpa_bin_tests[i].arg1);
5408 mpa2 = isl_multi_pw_aff_read_from_str(ctx,
5409 mpa_bin_tests[i].arg2);
5410 res = isl_multi_pw_aff_read_from_str(ctx,
5411 mpa_bin_tests[i].res);
5412 mpa1 = mpa_bin_tests[i].fn(mpa1, mpa2);
5413 ok = isl_multi_pw_aff_plain_is_equal(mpa1, res);
5414 isl_multi_pw_aff_free(mpa1);
5415 isl_multi_pw_aff_free(res);
5416 if (ok < 0)
5417 return -1;
5418 if (!ok)
5419 isl_die(ctx, isl_error_unknown,
5420 "unexpected result", return -1);
5423 return 0;
5426 /* Inputs for basic tests of unary operations on
5427 * isl_multi_union_pw_aff objects.
5428 * "fn" is the function that is tested.
5429 * "arg" is a string description of the input.
5430 * "res" is a string description of the expected result.
5432 struct {
5433 __isl_give isl_multi_union_pw_aff *(*fn)(
5434 __isl_take isl_multi_union_pw_aff *mupa);
5435 const char *arg;
5436 const char *res;
5437 } mupa_un_tests[] = {
5438 { &isl_multi_union_pw_aff_factor_range,
5439 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]",
5440 "C[{ A[] -> [2] }]" },
5441 { &isl_multi_union_pw_aff_factor_range,
5442 "[B[] -> C[{ A[] -> [2] }]]",
5443 "C[{ A[] -> [2] }]" },
5444 { &isl_multi_union_pw_aff_factor_range,
5445 "[B[{ A[] -> [1] }] -> C[]]",
5446 "C[]" },
5447 { &isl_multi_union_pw_aff_factor_range,
5448 "[B[] -> C[]]",
5449 "C[]" },
5450 { &isl_multi_union_pw_aff_factor_range,
5451 "([B[] -> C[]] : { A[x] : x >= 0 })",
5452 "(C[] : { A[x] : x >= 0 })" },
5453 { &isl_multi_union_pw_aff_factor_range,
5454 "[N] -> ([B[] -> C[]] : { A[x] : x <= N })",
5455 "[N] -> (C[] : { A[x] : x <= N })" },
5456 { &isl_multi_union_pw_aff_factor_range,
5457 "[N] -> ([B[] -> C[]] : { : N >= 0 })",
5458 "[N] -> (C[] : { : N >= 0 })" },
5461 /* Perform some basic tests of unary operations on
5462 * isl_multi_union_pw_aff objects.
5464 static int test_un_mupa(isl_ctx *ctx)
5466 int i;
5467 isl_bool ok;
5468 isl_multi_union_pw_aff *mupa, *res;
5470 for (i = 0; i < ARRAY_SIZE(mupa_un_tests); ++i) {
5471 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5472 mupa_un_tests[i].arg);
5473 res = isl_multi_union_pw_aff_read_from_str(ctx,
5474 mupa_un_tests[i].res);
5475 mupa = mupa_un_tests[i].fn(mupa);
5476 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5477 isl_multi_union_pw_aff_free(mupa);
5478 isl_multi_union_pw_aff_free(res);
5479 if (ok < 0)
5480 return -1;
5481 if (!ok)
5482 isl_die(ctx, isl_error_unknown,
5483 "unexpected result", return -1);
5486 return 0;
5489 /* Inputs for basic tests of binary operations on
5490 * isl_multi_union_pw_aff objects.
5491 * "fn" is the function that is tested.
5492 * "arg1" and "arg2" are string descriptions of the inputs.
5493 * "res" is a string description of the expected result.
5495 struct {
5496 __isl_give isl_multi_union_pw_aff *(*fn)(
5497 __isl_take isl_multi_union_pw_aff *mupa1,
5498 __isl_take isl_multi_union_pw_aff *mupa2);
5499 const char *arg1;
5500 const char *arg2;
5501 const char *res;
5502 } mupa_bin_tests[] = {
5503 { &isl_multi_union_pw_aff_add, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5504 "[{ A[] -> [3] }]" },
5505 { &isl_multi_union_pw_aff_sub, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5506 "[{ A[] -> [-1] }]" },
5507 { &isl_multi_union_pw_aff_add,
5508 "[{ A[] -> [1]; B[] -> [4] }]",
5509 "[{ A[] -> [2]; C[] -> [5] }]",
5510 "[{ A[] -> [3] }]" },
5511 { &isl_multi_union_pw_aff_union_add,
5512 "[{ A[] -> [1]; B[] -> [4] }]",
5513 "[{ A[] -> [2]; C[] -> [5] }]",
5514 "[{ A[] -> [3]; B[] -> [4]; C[] -> [5] }]" },
5515 { &isl_multi_union_pw_aff_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5516 "[{ A[x] -> [(x)] : x <= 10 }]",
5517 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10 }]" },
5518 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5519 "([] : { A[x] : x <= 10 })",
5520 "([] : { A[x] : 5 <= x <= 10 })" },
5521 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5522 "[N] -> ([] : { A[x] : x <= N })",
5523 "[N] -> ([] : { A[x] : 5 <= x <= N })" },
5524 { &isl_multi_union_pw_aff_add, "[N] -> ([] : { A[x] : x >= N })",
5525 "([] : { A[x] : x <= 10 })",
5526 "[N] -> ([] : { A[x] : N <= x <= 10 })" },
5527 { &isl_multi_union_pw_aff_union_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5528 "[{ A[x] -> [(x)] : x <= 10 }]",
5529 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10; "
5530 "A[x] -> [(1)] : x > 10; A[x] -> [(x)] : x < 5 }]" },
5531 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 5 })",
5532 "([] : { A[x] : x <= 10 })",
5533 "([] : { A[x] })" },
5534 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 0 })",
5535 "[N] -> ([] : { A[x] : x >= N })",
5536 "[N] -> ([] : { A[x] : x >= 0 or x >= N })" },
5537 { &isl_multi_union_pw_aff_union_add,
5538 "[N] -> ([] : { A[] : N >= 0})",
5539 "[N] -> ([] : { A[] : N <= 0})",
5540 "[N] -> ([] : { A[] })" },
5541 { &isl_multi_union_pw_aff_union_add,
5542 "[N] -> ([] : { A[] })",
5543 "[N] -> ([] : { : })",
5544 "[N] -> ([] : { : })" },
5545 { &isl_multi_union_pw_aff_union_add,
5546 "[N] -> ([] : { : })",
5547 "[N] -> ([] : { A[] })",
5548 "[N] -> ([] : { : })" },
5549 { &isl_multi_union_pw_aff_union_add,
5550 "[N] -> ([] : { : N >= 0})",
5551 "[N] -> ([] : { : N <= 0})",
5552 "[N] -> ([] : { : })" },
5553 { &isl_multi_union_pw_aff_range_product,
5554 "B[{ A[] -> [1] }]",
5555 "C[{ A[] -> [2] }]",
5556 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]" },
5557 { &isl_multi_union_pw_aff_range_product,
5558 "(B[] : { A[x] : x >= 5 })",
5559 "(C[] : { A[x] : x <= 10 })",
5560 "([B[] -> C[]] : { A[x] : 5 <= x <= 10 })" },
5561 { &isl_multi_union_pw_aff_range_product,
5562 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5563 "(C[] : { A[x] : x <= 10 })",
5564 "[B[{ A[x] -> [x + 1] : 5 <= x <= 10 }] -> C[]]" },
5565 { &isl_multi_union_pw_aff_range_product,
5566 "(C[] : { A[x] : x <= 10 })",
5567 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5568 "[C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= 10 }]]" },
5569 { &isl_multi_union_pw_aff_range_product,
5570 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5571 "[N] -> (C[] : { A[x] : x <= N })",
5572 "[N] -> [B[{ A[x] -> [x + 1] : 5 <= x <= N }] -> C[]]" },
5573 { &isl_multi_union_pw_aff_range_product,
5574 "[N] -> (C[] : { A[x] : x <= N })",
5575 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5576 "[N] -> [C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= N }]]" },
5577 { &isl_multi_union_pw_aff_range_product,
5578 "B[{ A[] -> [1]; D[] -> [3] }]",
5579 "C[{ A[] -> [2] }]",
5580 "[B[{ A[] -> [1]; D[] -> [3] }] -> C[{ A[] -> [2] }]]" },
5581 { &isl_multi_union_pw_aff_range_product,
5582 "B[] }]",
5583 "(C[] : { A[x] })",
5584 "([B[] -> C[]] : { A[x] })" },
5585 { &isl_multi_union_pw_aff_range_product,
5586 "(B[] : { A[x] })",
5587 "C[] }]",
5588 "([B[] -> C[]] : { A[x] })" },
5591 /* Perform some basic tests of binary operations on
5592 * isl_multi_union_pw_aff objects.
5594 static int test_bin_mupa(isl_ctx *ctx)
5596 int i;
5597 isl_bool ok;
5598 isl_multi_union_pw_aff *mupa1, *mupa2, *res;
5600 for (i = 0; i < ARRAY_SIZE(mupa_bin_tests); ++i) {
5601 mupa1 = isl_multi_union_pw_aff_read_from_str(ctx,
5602 mupa_bin_tests[i].arg1);
5603 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx,
5604 mupa_bin_tests[i].arg2);
5605 res = isl_multi_union_pw_aff_read_from_str(ctx,
5606 mupa_bin_tests[i].res);
5607 mupa1 = mupa_bin_tests[i].fn(mupa1, mupa2);
5608 ok = isl_multi_union_pw_aff_plain_is_equal(mupa1, res);
5609 isl_multi_union_pw_aff_free(mupa1);
5610 isl_multi_union_pw_aff_free(res);
5611 if (ok < 0)
5612 return -1;
5613 if (!ok)
5614 isl_die(ctx, isl_error_unknown,
5615 "unexpected result", return -1);
5618 return 0;
5621 /* Inputs for basic tests of binary operations on
5622 * pairs of isl_multi_union_pw_aff and isl_set objects.
5623 * "fn" is the function that is tested.
5624 * "arg1" and "arg2" are string descriptions of the inputs.
5625 * "res" is a string description of the expected result.
5627 struct {
5628 __isl_give isl_multi_union_pw_aff *(*fn)(
5629 __isl_take isl_multi_union_pw_aff *mupa,
5630 __isl_take isl_set *set);
5631 const char *arg1;
5632 const char *arg2;
5633 const char *res;
5634 } mupa_set_tests[] = {
5635 { &isl_multi_union_pw_aff_intersect_range,
5636 "C[{ B[i,j] -> [i + 2j] }]", "{ C[1] }",
5637 "C[{ B[i,j] -> [i + 2j] : i + 2j = 1 }]" },
5638 { &isl_multi_union_pw_aff_intersect_range,
5639 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[N] }",
5640 "[N] -> C[{ B[i,j] -> [i + 2j] : i + 2j = N }]" },
5641 { &isl_multi_union_pw_aff_intersect_range,
5642 "[N] -> C[{ B[i,j] -> [i + 2j + N] }]", "{ C[1] }",
5643 "[N] -> C[{ B[i,j] -> [i + 2j + N] : i + 2j + N = 1 }]" },
5644 { &isl_multi_union_pw_aff_intersect_range,
5645 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[x] : N >= 0 }",
5646 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0 }]" },
5647 { &isl_multi_union_pw_aff_intersect_range,
5648 "C[]", "{ C[] }", "C[]" },
5649 { &isl_multi_union_pw_aff_intersect_range,
5650 "[N] -> (C[] : { : N >= 0 })",
5651 "{ C[] }",
5652 "[N] -> (C[] : { : N >= 0 })" },
5653 { &isl_multi_union_pw_aff_intersect_range,
5654 "(C[] : { A[a,b] })",
5655 "{ C[] }",
5656 "(C[] : { A[a,b] })" },
5657 { &isl_multi_union_pw_aff_intersect_range,
5658 "[N] -> (C[] : { A[a,b] : a,b <= N })",
5659 "{ C[] }",
5660 "[N] -> (C[] : { A[a,b] : a,b <= N })" },
5661 { &isl_multi_union_pw_aff_intersect_range,
5662 "C[]",
5663 "[N] -> { C[] : N >= 0 }",
5664 "[N] -> (C[] : { : N >= 0 })" },
5665 { &isl_multi_union_pw_aff_intersect_range,
5666 "(C[] : { A[a,b] })",
5667 "[N] -> { C[] : N >= 0 }",
5668 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5669 { &isl_multi_union_pw_aff_intersect_range,
5670 "[N] -> (C[] : { : N >= 0 })",
5671 "[N] -> { C[] : N < 1024 }",
5672 "[N] -> (C[] : { : 0 <= N < 1024 })" },
5673 { &isl_multi_union_pw_aff_intersect_params,
5674 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { : N >= 0 }",
5675 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0}]" },
5676 { &isl_multi_union_pw_aff_intersect_params,
5677 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "[N] -> { : N >= 0 }",
5678 "[N] -> C[{ B[i,j] -> [i + 2j] : 0 <= N <= 256 }]" },
5679 { &isl_multi_union_pw_aff_intersect_params,
5680 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "{ : }",
5681 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]" },
5682 { &isl_multi_union_pw_aff_intersect_params,
5683 "C[]", "[N] -> { : N >= 0 }",
5684 "[N] -> (C[] : { : N >= 0 })" },
5685 { &isl_multi_union_pw_aff_intersect_params,
5686 "(C[] : { A[a,b] })", "[N] -> { : N >= 0 }",
5687 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5688 { &isl_multi_union_pw_aff_intersect_params,
5689 "[N] -> (C[] : { A[a,N] })", "{ : }",
5690 "[N] -> (C[] : { A[a,N] })" },
5691 { &isl_multi_union_pw_aff_intersect_params,
5692 "[N] -> (C[] : { A[a,b] : N <= 256 })", "[N] -> { : N >= 0 }",
5693 "[N] -> (C[] : { A[a,b] : 0 <= N <= 256 })" },
5696 /* Perform some basic tests of binary operations on
5697 * pairs of isl_multi_union_pw_aff and isl_set objects.
5699 static int test_mupa_set(isl_ctx *ctx)
5701 int i;
5702 isl_bool ok;
5703 isl_multi_union_pw_aff *mupa, *res;
5704 isl_set *set;
5706 for (i = 0; i < ARRAY_SIZE(mupa_set_tests); ++i) {
5707 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5708 mupa_set_tests[i].arg1);
5709 set = isl_set_read_from_str(ctx, mupa_set_tests[i].arg2);
5710 res = isl_multi_union_pw_aff_read_from_str(ctx,
5711 mupa_set_tests[i].res);
5712 mupa = mupa_set_tests[i].fn(mupa, set);
5713 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5714 isl_multi_union_pw_aff_free(mupa);
5715 isl_multi_union_pw_aff_free(res);
5716 if (ok < 0)
5717 return -1;
5718 if (!ok)
5719 isl_die(ctx, isl_error_unknown,
5720 "unexpected result", return -1);
5723 return 0;
5726 /* Inputs for basic tests of binary operations on
5727 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5728 * "fn" is the function that is tested.
5729 * "arg1" and "arg2" are string descriptions of the inputs.
5730 * "res" is a string description of the expected result.
5732 struct {
5733 __isl_give isl_multi_union_pw_aff *(*fn)(
5734 __isl_take isl_multi_union_pw_aff *mupa,
5735 __isl_take isl_union_set *uset);
5736 const char *arg1;
5737 const char *arg2;
5738 const char *res;
5739 } mupa_uset_tests[] = {
5740 { &isl_multi_union_pw_aff_intersect_domain,
5741 "C[{ B[i,j] -> [i + 2j] }]", "{ B[i,i] }",
5742 "C[{ B[i,i] -> [3i] }]" },
5743 { &isl_multi_union_pw_aff_intersect_domain,
5744 "(C[] : { B[i,j] })", "{ B[i,i] }",
5745 "(C[] : { B[i,i] })" },
5746 { &isl_multi_union_pw_aff_intersect_domain,
5747 "(C[] : { B[i,j] })", "[N] -> { B[N,N] }",
5748 "[N] -> (C[] : { B[N,N] })" },
5749 { &isl_multi_union_pw_aff_intersect_domain,
5750 "C[]", "{ B[i,i] }",
5751 "(C[] : { B[i,i] })" },
5752 { &isl_multi_union_pw_aff_intersect_domain,
5753 "[N] -> (C[] : { : N >= 0 })", "{ B[i,i] }",
5754 "[N] -> (C[] : { B[i,i] : N >= 0 })" },
5757 /* Perform some basic tests of binary operations on
5758 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5760 static int test_mupa_uset(isl_ctx *ctx)
5762 int i;
5763 isl_bool ok;
5764 isl_multi_union_pw_aff *mupa, *res;
5765 isl_union_set *uset;
5767 for (i = 0; i < ARRAY_SIZE(mupa_uset_tests); ++i) {
5768 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5769 mupa_uset_tests[i].arg1);
5770 uset = isl_union_set_read_from_str(ctx,
5771 mupa_uset_tests[i].arg2);
5772 res = isl_multi_union_pw_aff_read_from_str(ctx,
5773 mupa_uset_tests[i].res);
5774 mupa = mupa_uset_tests[i].fn(mupa, uset);
5775 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5776 isl_multi_union_pw_aff_free(mupa);
5777 isl_multi_union_pw_aff_free(res);
5778 if (ok < 0)
5779 return -1;
5780 if (!ok)
5781 isl_die(ctx, isl_error_unknown,
5782 "unexpected result", return -1);
5785 return 0;
5788 /* Inputs for basic tests of binary operations on
5789 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5790 * "fn" is the function that is tested.
5791 * "arg1" and "arg2" are string descriptions of the inputs.
5792 * "res" is a string description of the expected result.
5794 struct {
5795 __isl_give isl_multi_union_pw_aff *(*fn)(
5796 __isl_take isl_multi_union_pw_aff *mupa,
5797 __isl_take isl_multi_aff *ma);
5798 const char *arg1;
5799 const char *arg2;
5800 const char *res;
5801 } mupa_ma_tests[] = {
5802 { &isl_multi_union_pw_aff_apply_multi_aff,
5803 "C[{ A[i,j] -> [i]; 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]; B[i,j] -> [i] }, "
5807 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
5808 { &isl_multi_union_pw_aff_apply_multi_aff,
5809 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
5810 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5811 "{ C[a,b] -> D[b,a] }",
5812 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
5813 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
5814 { &isl_multi_union_pw_aff_apply_multi_aff,
5815 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5816 "[N] -> { C[a] -> D[a + N] }",
5817 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }] " },
5818 { &isl_multi_union_pw_aff_apply_multi_aff,
5819 "C[]",
5820 "{ C[] -> D[] }",
5821 "D[]" },
5822 { &isl_multi_union_pw_aff_apply_multi_aff,
5823 "[N] -> (C[] : { : N >= 0 })",
5824 "{ C[] -> D[] }",
5825 "[N] -> (D[] : { : N >= 0 })" },
5826 { &isl_multi_union_pw_aff_apply_multi_aff,
5827 "C[]",
5828 "[N] -> { C[] -> D[N] }",
5829 "[N] -> D[{ [N] }]" },
5830 { &isl_multi_union_pw_aff_apply_multi_aff,
5831 "(C[] : { A[i,j] : i >= j })",
5832 "{ C[] -> D[] }",
5833 "(D[] : { A[i,j] : i >= j })" },
5834 { &isl_multi_union_pw_aff_apply_multi_aff,
5835 "[N] -> (C[] : { A[i,j] : N >= 0 })",
5836 "{ C[] -> D[] }",
5837 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
5838 { &isl_multi_union_pw_aff_apply_multi_aff,
5839 "(C[] : { A[i,j] : i >= j })",
5840 "[N] -> { C[] -> D[N] }",
5841 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
5844 /* Perform some basic tests of binary operations on
5845 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5847 static int test_mupa_ma(isl_ctx *ctx)
5849 int i;
5850 isl_bool ok;
5851 isl_multi_union_pw_aff *mupa, *res;
5852 isl_multi_aff *ma;
5854 for (i = 0; i < ARRAY_SIZE(mupa_ma_tests); ++i) {
5855 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5856 mupa_ma_tests[i].arg1);
5857 ma = isl_multi_aff_read_from_str(ctx, mupa_ma_tests[i].arg2);
5858 res = isl_multi_union_pw_aff_read_from_str(ctx,
5859 mupa_ma_tests[i].res);
5860 mupa = mupa_ma_tests[i].fn(mupa, ma);
5861 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5862 isl_multi_union_pw_aff_free(mupa);
5863 isl_multi_union_pw_aff_free(res);
5864 if (ok < 0)
5865 return -1;
5866 if (!ok)
5867 isl_die(ctx, isl_error_unknown,
5868 "unexpected result", return -1);
5871 return 0;
5874 /* Inputs for basic tests of binary operations on
5875 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5876 * "fn" is the function that is tested.
5877 * "arg1" and "arg2" are string descriptions of the inputs.
5878 * "res" is a string description of the expected result.
5880 struct {
5881 __isl_give isl_union_pw_aff *(*fn)(
5882 __isl_take isl_multi_union_pw_aff *mupa,
5883 __isl_take isl_pw_aff *pa);
5884 const char *arg1;
5885 const char *arg2;
5886 const char *res;
5887 } mupa_pa_tests[] = {
5888 { &isl_multi_union_pw_aff_apply_pw_aff,
5889 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5890 "[N] -> { C[a] -> [a + N] }",
5891 "[N] -> { A[i,j] -> [i + N]; B[i,j] -> [j + N] }" },
5892 { &isl_multi_union_pw_aff_apply_pw_aff,
5893 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5894 "{ C[a] -> [a] : a >= 0; C[a] -> [-a] : a < 0 }",
5895 "{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
5896 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }" },
5897 { &isl_multi_union_pw_aff_apply_pw_aff,
5898 "C[]",
5899 "[N] -> { C[] -> [N] }",
5900 "[N] -> { [N] }" },
5901 { &isl_multi_union_pw_aff_apply_pw_aff,
5902 "C[]",
5903 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
5904 "[N] -> { [N] : N >= 0; [-N] : N < 0 }" },
5905 { &isl_multi_union_pw_aff_apply_pw_aff,
5906 "[N] -> (C[] : { : N >= 0 })",
5907 "[N] -> { C[] -> [N] }",
5908 "[N] -> { [N] : N >= 0 }" },
5909 { &isl_multi_union_pw_aff_apply_pw_aff,
5910 "[N] -> (C[] : { : N >= 0 })",
5911 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
5912 "[N] -> { [N] : N >= 0 }" },
5913 { &isl_multi_union_pw_aff_apply_pw_aff,
5914 "[N] -> (C[] : { : N >= 0 })",
5915 "{ C[] -> [0] }",
5916 "[N] -> { [0] : N >= 0 }" },
5917 { &isl_multi_union_pw_aff_apply_pw_aff,
5918 "(C[] : { A[i,j] : i >= j })",
5919 "[N] -> { C[] -> [N] }",
5920 "[N] -> { A[i,j] -> [N] : i >= j }" },
5921 { &isl_multi_union_pw_aff_apply_pw_aff,
5922 "(C[] : { A[i,j] : i >= j })",
5923 "[N] -> { C[] -> [N] : N >= 0 }",
5924 "[N] -> { A[i,j] -> [N] : i >= j and N >= 0 }" },
5927 /* Perform some basic tests of binary operations on
5928 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5930 static int test_mupa_pa(isl_ctx *ctx)
5932 int i;
5933 isl_bool ok;
5934 isl_multi_union_pw_aff *mupa;
5935 isl_union_pw_aff *upa, *res;
5936 isl_pw_aff *pa;
5938 for (i = 0; i < ARRAY_SIZE(mupa_pa_tests); ++i) {
5939 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5940 mupa_pa_tests[i].arg1);
5941 pa = isl_pw_aff_read_from_str(ctx, mupa_pa_tests[i].arg2);
5942 res = isl_union_pw_aff_read_from_str(ctx,
5943 mupa_pa_tests[i].res);
5944 upa = mupa_pa_tests[i].fn(mupa, pa);
5945 ok = isl_union_pw_aff_plain_is_equal(upa, res);
5946 isl_union_pw_aff_free(upa);
5947 isl_union_pw_aff_free(res);
5948 if (ok < 0)
5949 return -1;
5950 if (!ok)
5951 isl_die(ctx, isl_error_unknown,
5952 "unexpected result", return -1);
5955 return 0;
5958 /* Inputs for basic tests of binary operations on
5959 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
5960 * "fn" is the function that is tested.
5961 * "arg1" and "arg2" are string descriptions of the inputs.
5962 * "res" is a string description of the expected result.
5964 struct {
5965 __isl_give isl_multi_union_pw_aff *(*fn)(
5966 __isl_take isl_multi_union_pw_aff *mupa,
5967 __isl_take isl_pw_multi_aff *pma);
5968 const char *arg1;
5969 const char *arg2;
5970 const char *res;
5971 } mupa_pma_tests[] = {
5972 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5973 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5974 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5975 "{ C[a,b] -> D[b,a] }",
5976 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
5977 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
5978 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5979 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
5980 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5981 "{ C[a,b] -> D[b,a] }",
5982 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
5983 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
5984 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5985 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5986 "[N] -> { C[a] -> D[a + N] }",
5987 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }]" },
5988 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5989 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5990 "{ C[a] -> D[a] : a >= 0; C[a] -> D[-a] : a < 0 }",
5991 "D[{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
5992 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }]" },
5993 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5994 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5995 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5996 "{ C[a,b] -> D[a,b] : a >= b; C[a,b] -> D[b,a] : a < b }",
5997 "D[{ A[i,j] -> [i] : i >= j; A[i,j] -> [j] : i < j; "
5998 "B[i,j] -> [j] : i <= j; B[i,j] -> [i] : i > j }, "
5999 "{ A[i,j] -> [j] : i >= j; A[i,j] -> [i] : i < j; "
6000 "B[i,j] -> [i] : i <= j; B[i,j] -> [j] : i > j }]" },
6001 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6002 "C[]",
6003 "{ C[] -> D[] }",
6004 "D[]" },
6005 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6006 "[N] -> (C[] : { : N >= 0 })",
6007 "{ C[] -> D[] }",
6008 "[N] -> (D[] : { : N >= 0 })" },
6009 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6010 "C[]",
6011 "[N] -> { C[] -> D[N] }",
6012 "[N] -> D[{ [N] }]" },
6013 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6014 "(C[] : { A[i,j] : i >= j })",
6015 "{ C[] -> D[] }",
6016 "(D[] : { A[i,j] : i >= j })" },
6017 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6018 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6019 "{ C[] -> D[] }",
6020 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6021 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6022 "(C[] : { A[i,j] : i >= j })",
6023 "[N] -> { C[] -> D[N] }",
6024 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6025 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6026 "C[]",
6027 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6028 "[N] -> D[{ [N] : N >= 0; [-N] : N < 0 }]" },
6029 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6030 "[N] -> (C[] : { : N >= 0 })",
6031 "[N] -> { C[] -> D[N] }",
6032 "[N] -> D[{ [N] : N >= 0 }]" },
6033 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6034 "[N] -> (C[] : { : N >= 0 })",
6035 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6036 "[N] -> D[{ [N] : N >= 0 }]" },
6037 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6038 "[N] -> (C[] : { : N >= 0 })",
6039 "{ C[] -> D[0] }",
6040 "[N] -> D[{ [0] : N >= 0 }]" },
6041 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6042 "(C[] : { A[i,j] : i >= j })",
6043 "[N] -> { C[] -> D[N] : N >= 0 }",
6044 "[N] -> D[{ A[i,j] -> [N] : i >= j and N >= 0 }]" },
6047 /* Perform some basic tests of binary operations on
6048 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6050 static int test_mupa_pma(isl_ctx *ctx)
6052 int i;
6053 isl_bool ok;
6054 isl_multi_union_pw_aff *mupa, *res;
6055 isl_pw_multi_aff *pma;
6057 for (i = 0; i < ARRAY_SIZE(mupa_pma_tests); ++i) {
6058 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6059 mupa_pma_tests[i].arg1);
6060 pma = isl_pw_multi_aff_read_from_str(ctx,
6061 mupa_pma_tests[i].arg2);
6062 res = isl_multi_union_pw_aff_read_from_str(ctx,
6063 mupa_pma_tests[i].res);
6064 mupa = mupa_pma_tests[i].fn(mupa, pma);
6065 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6066 isl_multi_union_pw_aff_free(mupa);
6067 isl_multi_union_pw_aff_free(res);
6068 if (ok < 0)
6069 return -1;
6070 if (!ok)
6071 isl_die(ctx, isl_error_unknown,
6072 "unexpected result", return -1);
6075 return 0;
6078 /* Inputs for basic tests of binary operations on
6079 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6080 * "fn" is the function that is tested.
6081 * "arg1" and "arg2" are string descriptions of the inputs.
6082 * "res" is a string description of the expected result.
6084 struct {
6085 __isl_give isl_multi_union_pw_aff *(*fn)(
6086 __isl_take isl_multi_union_pw_aff *mupa,
6087 __isl_take isl_union_pw_multi_aff *upma);
6088 const char *arg1;
6089 const char *arg2;
6090 const char *res;
6091 } mupa_upma_tests[] = {
6092 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6093 "C[{ B[i,j] -> [i + 2j] }]", "{ A[a,b] -> B[b,a] }",
6094 "C[{ A[a,b] -> [b + 2a] }]" },
6095 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6096 "C[{ B[i,j] -> [i + 2j] }]",
6097 "{ A[a,b] -> B[b,a] : b > a }",
6098 "C[{ A[a,b] -> [b + 2a] : b > a }]" },
6099 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6100 "C[{ B[i,j] -> [i + 2j] : j > 4 }]",
6101 "{ A[a,b] -> B[b,a] : b > a }",
6102 "C[{ A[a,b] -> [b + 2a] : b > a > 4 }]" },
6103 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6104 "C[{ B[i,j] -> [i + 2j] }]",
6105 "{ A[a,b] -> B[b,a] : a > b; A[a,b] -> B[a,b] : a <= b }",
6106 "C[{ A[a,b] -> [b + 2a] : a > b; A[a,b] -> [a + 2b] : a <= b }]" },
6107 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6108 "(C[] : { B[a,b] })",
6109 "{ A[a,b] -> B[b,a] }",
6110 "(C[] : { A[a,b] })" },
6111 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6112 "(C[] : { B[a,b] })",
6113 "{ B[a,b] -> A[b,a] }",
6114 "(C[] : { })" },
6115 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6116 "(C[] : { B[a,b] })",
6117 "{ A[a,b] -> B[b,a] : a > b }",
6118 "(C[] : { A[a,b] : a > b })" },
6119 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6120 "(C[] : { B[a,b] : a > b })",
6121 "{ A[a,b] -> B[b,a] }",
6122 "(C[] : { A[a,b] : b > a })" },
6123 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6124 "[N] -> (C[] : { B[a,b] : a > N })",
6125 "{ A[a,b] -> B[b,a] : a > b }",
6126 "[N] -> (C[] : { A[a,b] : a > b > N })" },
6127 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6128 "(C[] : { B[a,b] : a > b })",
6129 "[N] -> { A[a,b] -> B[b,a] : a > N }",
6130 "[N] -> (C[] : { A[a,b] : b > a > N })" },
6131 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6132 "C[]",
6133 "{ A[a,b] -> B[b,a] }",
6134 "C[]" },
6135 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6136 "[N] -> (C[] : { : N >= 0 })",
6137 "{ A[a,b] -> B[b,a] }",
6138 "[N] -> (C[] : { : N >= 0 })" },
6139 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6140 "C[]",
6141 "[N] -> { A[a,b] -> B[b,a] : N >= 0 }",
6142 "[N] -> (C[] : { : N >= 0 })" },
6145 /* Perform some basic tests of binary operations on
6146 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6148 static int test_mupa_upma(isl_ctx *ctx)
6150 int i;
6151 isl_bool ok;
6152 isl_multi_union_pw_aff *mupa, *res;
6153 isl_union_pw_multi_aff *upma;
6155 for (i = 0; i < ARRAY_SIZE(mupa_upma_tests); ++i) {
6156 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6157 mupa_upma_tests[i].arg1);
6158 upma = isl_union_pw_multi_aff_read_from_str(ctx,
6159 mupa_upma_tests[i].arg2);
6160 res = isl_multi_union_pw_aff_read_from_str(ctx,
6161 mupa_upma_tests[i].res);
6162 mupa = mupa_upma_tests[i].fn(mupa, upma);
6163 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6164 isl_multi_union_pw_aff_free(mupa);
6165 isl_multi_union_pw_aff_free(res);
6166 if (ok < 0)
6167 return -1;
6168 if (!ok)
6169 isl_die(ctx, isl_error_unknown,
6170 "unexpected result", return -1);
6173 return 0;
6176 /* Check that the input tuple of an isl_aff can be set properly.
6178 static isl_stat test_aff_set_tuple_id(isl_ctx *ctx)
6180 isl_id *id;
6181 isl_aff *aff;
6182 int equal;
6184 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x + 1] }");
6185 id = isl_id_alloc(ctx, "A", NULL);
6186 aff = isl_aff_set_tuple_id(aff, isl_dim_in, id);
6187 equal = aff_check_plain_equal(aff, "{ A[x] -> [x + 1] }");
6188 isl_aff_free(aff);
6189 if (equal < 0)
6190 return isl_stat_error;
6192 return isl_stat_ok;
6195 int test_aff(isl_ctx *ctx)
6197 const char *str;
6198 isl_set *set;
6199 isl_space *space;
6200 isl_local_space *ls;
6201 isl_aff *aff;
6202 int zero, equal;
6204 if (test_upa(ctx) < 0)
6205 return -1;
6206 if (test_bin_aff(ctx) < 0)
6207 return -1;
6208 if (test_bin_pw_aff(ctx) < 0)
6209 return -1;
6210 if (test_bin_upma(ctx) < 0)
6211 return -1;
6212 if (test_bin_upma_fail(ctx) < 0)
6213 return -1;
6214 if (test_un_mpa(ctx) < 0)
6215 return -1;
6216 if (test_bin_mpa(ctx) < 0)
6217 return -1;
6218 if (test_un_mupa(ctx) < 0)
6219 return -1;
6220 if (test_bin_mupa(ctx) < 0)
6221 return -1;
6222 if (test_mupa_set(ctx) < 0)
6223 return -1;
6224 if (test_mupa_uset(ctx) < 0)
6225 return -1;
6226 if (test_mupa_ma(ctx) < 0)
6227 return -1;
6228 if (test_mupa_pa(ctx) < 0)
6229 return -1;
6230 if (test_mupa_pma(ctx) < 0)
6231 return -1;
6232 if (test_mupa_upma(ctx) < 0)
6233 return -1;
6235 space = isl_space_set_alloc(ctx, 0, 1);
6236 ls = isl_local_space_from_space(space);
6237 aff = isl_aff_zero_on_domain(ls);
6239 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6240 aff = isl_aff_scale_down_ui(aff, 3);
6241 aff = isl_aff_floor(aff);
6242 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6243 aff = isl_aff_scale_down_ui(aff, 2);
6244 aff = isl_aff_floor(aff);
6245 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6247 str = "{ [10] }";
6248 set = isl_set_read_from_str(ctx, str);
6249 aff = isl_aff_gist(aff, set);
6251 aff = isl_aff_add_constant_si(aff, -16);
6252 zero = isl_aff_plain_is_zero(aff);
6253 isl_aff_free(aff);
6255 if (zero < 0)
6256 return -1;
6257 if (!zero)
6258 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6260 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
6261 aff = isl_aff_scale_down_ui(aff, 64);
6262 aff = isl_aff_floor(aff);
6263 equal = aff_check_plain_equal(aff, "{ [-1] }");
6264 isl_aff_free(aff);
6265 if (equal < 0)
6266 return -1;
6268 if (test_aff_set_tuple_id(ctx) < 0)
6269 return -1;
6271 return 0;
6274 /* Check that "pa" consists of a single expression.
6276 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
6278 isl_size n;
6280 n = isl_pw_aff_n_piece(pa);
6281 isl_pw_aff_free(pa);
6283 if (n < 0)
6284 return -1;
6285 if (n != 1)
6286 isl_die(ctx, isl_error_unknown, "expecting single expression",
6287 return -1);
6289 return 0;
6292 /* Check that the computation below results in a single expression.
6293 * One or two expressions may result depending on which constraint
6294 * ends up being considered as redundant with respect to the other
6295 * constraints after the projection that is performed internally
6296 * by isl_set_dim_min.
6298 static int test_dim_max_1(isl_ctx *ctx)
6300 const char *str;
6301 isl_set *set;
6302 isl_pw_aff *pa;
6304 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
6305 "-4a <= b <= 3 and b < n - 4a }";
6306 set = isl_set_read_from_str(ctx, str);
6307 pa = isl_set_dim_min(set, 0);
6308 return check_single_piece(ctx, pa);
6311 /* Check that the computation below results in a single expression.
6312 * The PIP problem corresponding to these constraints has a row
6313 * that causes a split of the solution domain. The solver should
6314 * first pick rows that split off empty parts such that the actual
6315 * solution domain does not get split.
6316 * Note that the description contains some redundant constraints.
6317 * If these constraints get removed first, then the row mentioned
6318 * above does not appear in the PIP problem.
6320 static int test_dim_max_2(isl_ctx *ctx)
6322 const char *str;
6323 isl_set *set;
6324 isl_pw_aff *pa;
6326 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
6327 "N > 0 and P >= 0 }";
6328 set = isl_set_read_from_str(ctx, str);
6329 pa = isl_set_dim_max(set, 0);
6330 return check_single_piece(ctx, pa);
6333 int test_dim_max(isl_ctx *ctx)
6335 int equal;
6336 const char *str;
6337 isl_set *set1, *set2;
6338 isl_set *set;
6339 isl_map *map;
6340 isl_pw_aff *pwaff;
6342 if (test_dim_max_1(ctx) < 0)
6343 return -1;
6344 if (test_dim_max_2(ctx) < 0)
6345 return -1;
6347 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
6348 set = isl_set_read_from_str(ctx, str);
6349 pwaff = isl_set_dim_max(set, 0);
6350 set1 = isl_set_from_pw_aff(pwaff);
6351 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
6352 set2 = isl_set_read_from_str(ctx, str);
6353 equal = isl_set_is_equal(set1, set2);
6354 isl_set_free(set1);
6355 isl_set_free(set2);
6356 if (equal < 0)
6357 return -1;
6358 if (!equal)
6359 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6361 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
6362 set = isl_set_read_from_str(ctx, str);
6363 pwaff = isl_set_dim_max(set, 0);
6364 set1 = isl_set_from_pw_aff(pwaff);
6365 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6366 set2 = isl_set_read_from_str(ctx, str);
6367 equal = isl_set_is_equal(set1, set2);
6368 isl_set_free(set1);
6369 isl_set_free(set2);
6370 if (equal < 0)
6371 return -1;
6372 if (!equal)
6373 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6375 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
6376 set = isl_set_read_from_str(ctx, str);
6377 pwaff = isl_set_dim_max(set, 0);
6378 set1 = isl_set_from_pw_aff(pwaff);
6379 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6380 set2 = isl_set_read_from_str(ctx, str);
6381 equal = isl_set_is_equal(set1, set2);
6382 isl_set_free(set1);
6383 isl_set_free(set2);
6384 if (equal < 0)
6385 return -1;
6386 if (!equal)
6387 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6389 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
6390 "0 <= i < N and 0 <= j < M }";
6391 map = isl_map_read_from_str(ctx, str);
6392 set = isl_map_range(map);
6394 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
6395 set1 = isl_set_from_pw_aff(pwaff);
6396 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
6397 set2 = isl_set_read_from_str(ctx, str);
6398 equal = isl_set_is_equal(set1, set2);
6399 isl_set_free(set1);
6400 isl_set_free(set2);
6402 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
6403 set1 = isl_set_from_pw_aff(pwaff);
6404 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
6405 set2 = isl_set_read_from_str(ctx, str);
6406 if (equal >= 0 && equal)
6407 equal = isl_set_is_equal(set1, set2);
6408 isl_set_free(set1);
6409 isl_set_free(set2);
6411 isl_set_free(set);
6413 if (equal < 0)
6414 return -1;
6415 if (!equal)
6416 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6418 /* Check that solutions are properly merged. */
6419 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
6420 "c <= -1 + n - 4a - 2b and c >= -2b and "
6421 "4a >= -4 + n and c >= 0 }";
6422 set = isl_set_read_from_str(ctx, str);
6423 pwaff = isl_set_dim_min(set, 2);
6424 set1 = isl_set_from_pw_aff(pwaff);
6425 str = "[n] -> { [(0)] : n >= 1 }";
6426 set2 = isl_set_read_from_str(ctx, str);
6427 equal = isl_set_is_equal(set1, set2);
6428 isl_set_free(set1);
6429 isl_set_free(set2);
6431 if (equal < 0)
6432 return -1;
6433 if (!equal)
6434 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6436 /* Check that empty solution lie in the right space. */
6437 str = "[n] -> { [t,a] : 1 = 0 }";
6438 set = isl_set_read_from_str(ctx, str);
6439 pwaff = isl_set_dim_max(set, 0);
6440 set1 = isl_set_from_pw_aff(pwaff);
6441 str = "[n] -> { [t] : 1 = 0 }";
6442 set2 = isl_set_read_from_str(ctx, str);
6443 equal = isl_set_is_equal(set1, set2);
6444 isl_set_free(set1);
6445 isl_set_free(set2);
6447 if (equal < 0)
6448 return -1;
6449 if (!equal)
6450 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6452 return 0;
6455 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
6457 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
6458 const char *str)
6460 isl_ctx *ctx;
6461 isl_pw_multi_aff *pma2;
6462 int equal;
6464 if (!pma)
6465 return -1;
6467 ctx = isl_pw_multi_aff_get_ctx(pma);
6468 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6469 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
6470 isl_pw_multi_aff_free(pma2);
6472 return equal;
6475 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
6476 * represented by "str".
6478 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
6479 const char *str)
6481 int equal;
6483 equal = pw_multi_aff_plain_is_equal(pma, str);
6484 if (equal < 0)
6485 return -1;
6486 if (!equal)
6487 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
6488 "result not as expected", return -1);
6489 return 0;
6492 /* Basic test for isl_pw_multi_aff_product.
6494 * Check that multiple pieces are properly handled.
6496 static int test_product_pma(isl_ctx *ctx)
6498 int equal;
6499 const char *str;
6500 isl_pw_multi_aff *pma1, *pma2;
6502 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
6503 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6504 str = "{ C[] -> D[] }";
6505 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6506 pma1 = isl_pw_multi_aff_product(pma1, pma2);
6507 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
6508 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
6509 equal = pw_multi_aff_check_plain_equal(pma1, str);
6510 isl_pw_multi_aff_free(pma1);
6511 if (equal < 0)
6512 return -1;
6514 return 0;
6517 int test_product(isl_ctx *ctx)
6519 const char *str;
6520 isl_set *set;
6521 isl_union_set *uset1, *uset2;
6522 int ok;
6524 str = "{ A[i] }";
6525 set = isl_set_read_from_str(ctx, str);
6526 set = isl_set_product(set, isl_set_copy(set));
6527 ok = isl_set_is_wrapping(set);
6528 isl_set_free(set);
6529 if (ok < 0)
6530 return -1;
6531 if (!ok)
6532 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6534 str = "{ [] }";
6535 uset1 = isl_union_set_read_from_str(ctx, str);
6536 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
6537 str = "{ [[] -> []] }";
6538 uset2 = isl_union_set_read_from_str(ctx, str);
6539 ok = isl_union_set_is_equal(uset1, uset2);
6540 isl_union_set_free(uset1);
6541 isl_union_set_free(uset2);
6542 if (ok < 0)
6543 return -1;
6544 if (!ok)
6545 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6547 if (test_product_pma(ctx) < 0)
6548 return -1;
6550 return 0;
6553 /* Check that two sets are not considered disjoint just because
6554 * they have a different set of (named) parameters.
6556 static int test_disjoint(isl_ctx *ctx)
6558 const char *str;
6559 isl_set *set, *set2;
6560 int disjoint;
6562 str = "[n] -> { [[]->[]] }";
6563 set = isl_set_read_from_str(ctx, str);
6564 str = "{ [[]->[]] }";
6565 set2 = isl_set_read_from_str(ctx, str);
6566 disjoint = isl_set_is_disjoint(set, set2);
6567 isl_set_free(set);
6568 isl_set_free(set2);
6569 if (disjoint < 0)
6570 return -1;
6571 if (disjoint)
6572 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6574 return 0;
6577 /* Inputs for isl_pw_multi_aff_is_equal tests.
6578 * "f1" and "f2" are the two function that need to be compared.
6579 * "equal" is the expected result.
6581 struct {
6582 int equal;
6583 const char *f1;
6584 const char *f2;
6585 } pma_equal_tests[] = {
6586 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
6587 "[N] -> { [0] : 0 <= N <= 1 }" },
6588 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6589 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
6590 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6591 "[N] -> { [0] : 0 <= N <= 1 }" },
6592 { 0, "{ [NaN] }", "{ [NaN] }" },
6595 int test_equal(isl_ctx *ctx)
6597 int i;
6598 const char *str;
6599 isl_set *set, *set2;
6600 int equal;
6602 str = "{ S_6[i] }";
6603 set = isl_set_read_from_str(ctx, str);
6604 str = "{ S_7[i] }";
6605 set2 = isl_set_read_from_str(ctx, str);
6606 equal = isl_set_is_equal(set, set2);
6607 isl_set_free(set);
6608 isl_set_free(set2);
6609 if (equal < 0)
6610 return -1;
6611 if (equal)
6612 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6614 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
6615 int expected = pma_equal_tests[i].equal;
6616 isl_pw_multi_aff *f1, *f2;
6618 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
6619 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
6620 equal = isl_pw_multi_aff_is_equal(f1, f2);
6621 isl_pw_multi_aff_free(f1);
6622 isl_pw_multi_aff_free(f2);
6623 if (equal < 0)
6624 return -1;
6625 if (equal != expected)
6626 isl_die(ctx, isl_error_unknown,
6627 "unexpected equality result", return -1);
6630 return 0;
6633 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
6634 enum isl_dim_type type, unsigned pos, int fixed)
6636 isl_bool test;
6638 test = isl_map_plain_is_fixed(map, type, pos, NULL);
6639 isl_map_free(map);
6640 if (test < 0)
6641 return -1;
6642 if (test == fixed)
6643 return 0;
6644 if (fixed)
6645 isl_die(ctx, isl_error_unknown,
6646 "map not detected as fixed", return -1);
6647 else
6648 isl_die(ctx, isl_error_unknown,
6649 "map detected as fixed", return -1);
6652 int test_fixed(isl_ctx *ctx)
6654 const char *str;
6655 isl_map *map;
6657 str = "{ [i] -> [i] }";
6658 map = isl_map_read_from_str(ctx, str);
6659 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
6660 return -1;
6661 str = "{ [i] -> [1] }";
6662 map = isl_map_read_from_str(ctx, str);
6663 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6664 return -1;
6665 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
6666 map = isl_map_read_from_str(ctx, str);
6667 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6668 return -1;
6669 map = isl_map_read_from_str(ctx, str);
6670 map = isl_map_neg(map);
6671 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6672 return -1;
6674 return 0;
6677 struct isl_vertices_test_data {
6678 const char *set;
6679 int n;
6680 const char *vertex[6];
6681 } vertices_tests[] = {
6682 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
6683 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
6684 { "{ A[t, i] : t = 14 and i = 1 }",
6685 1, { "{ A[14, 1] }" } },
6686 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
6687 "c <= m and m <= n and m > 0 }",
6688 6, {
6689 "[n, m] -> { [n, m, m] : 0 < m <= n }",
6690 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
6691 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
6692 "[n, m] -> { [m, m, m] : 0 < m <= n }",
6693 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
6694 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
6695 } },
6698 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
6700 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
6702 struct isl_vertices_test_data *data = user;
6703 isl_ctx *ctx;
6704 isl_multi_aff *ma;
6705 isl_basic_set *bset;
6706 isl_pw_multi_aff *pma;
6707 int i;
6708 isl_bool equal;
6710 ctx = isl_vertex_get_ctx(vertex);
6711 bset = isl_vertex_get_domain(vertex);
6712 ma = isl_vertex_get_expr(vertex);
6713 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
6715 for (i = 0; i < data->n; ++i) {
6716 isl_pw_multi_aff *pma_i;
6718 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
6719 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
6720 isl_pw_multi_aff_free(pma_i);
6722 if (equal < 0 || equal)
6723 break;
6726 isl_pw_multi_aff_free(pma);
6727 isl_vertex_free(vertex);
6729 if (equal < 0)
6730 return isl_stat_error;
6732 return equal ? isl_stat_ok : isl_stat_error;
6735 int test_vertices(isl_ctx *ctx)
6737 int i;
6739 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
6740 isl_basic_set *bset;
6741 isl_vertices *vertices;
6742 int ok = 1;
6743 isl_size n;
6745 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
6746 vertices = isl_basic_set_compute_vertices(bset);
6747 n = isl_vertices_get_n_vertices(vertices);
6748 if (vertices_tests[i].n != n)
6749 ok = 0;
6750 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
6751 &vertices_tests[i]) < 0)
6752 ok = 0;
6753 isl_vertices_free(vertices);
6754 isl_basic_set_free(bset);
6756 if (n < 0)
6757 return -1;
6758 if (!ok)
6759 isl_die(ctx, isl_error_unknown, "unexpected vertices",
6760 return -1);
6763 return 0;
6766 int test_union_pw(isl_ctx *ctx)
6768 int equal;
6769 const char *str;
6770 isl_union_set *uset;
6771 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
6773 str = "{ [x] -> x^2 }";
6774 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
6775 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
6776 uset = isl_union_pw_qpolynomial_domain(upwqp1);
6777 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
6778 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
6779 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
6780 isl_union_pw_qpolynomial_free(upwqp1);
6781 isl_union_pw_qpolynomial_free(upwqp2);
6782 if (equal < 0)
6783 return -1;
6784 if (!equal)
6785 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6787 return 0;
6790 /* Inputs for basic tests of functions that select
6791 * subparts of the domain of an isl_multi_union_pw_aff.
6792 * "fn" is the function that is tested.
6793 * "arg" is a string description of the input.
6794 * "res" is a string description of the expected result.
6796 struct {
6797 __isl_give isl_union_set *(*fn)(
6798 __isl_take isl_multi_union_pw_aff *mupa);
6799 const char *arg;
6800 const char *res;
6801 } un_locus_tests[] = {
6802 { &isl_multi_union_pw_aff_zero_union_set,
6803 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6804 "{ A[0,j]; B[0,j] }" },
6805 { &isl_multi_union_pw_aff_zero_union_set,
6806 "F[{ A[i,j] -> [i-j]; B[i,j] -> [i-j] : i >= 0 }]",
6807 "{ A[i,i]; B[i,i] : i >= 0 }" },
6808 { &isl_multi_union_pw_aff_zero_union_set,
6809 "(F[] : { A[i,j]; B[i,i] : i >= 0 })",
6810 "{ A[i,j]; B[i,i] : i >= 0 }" },
6813 /* Perform some basic tests of functions that select
6814 * subparts of the domain of an isl_multi_union_pw_aff.
6816 static int test_un_locus(isl_ctx *ctx)
6818 int i;
6819 isl_bool ok;
6820 isl_union_set *uset, *res;
6821 isl_multi_union_pw_aff *mupa;
6823 for (i = 0; i < ARRAY_SIZE(un_locus_tests); ++i) {
6824 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6825 un_locus_tests[i].arg);
6826 res = isl_union_set_read_from_str(ctx, un_locus_tests[i].res);
6827 uset = un_locus_tests[i].fn(mupa);
6828 ok = isl_union_set_is_equal(uset, res);
6829 isl_union_set_free(uset);
6830 isl_union_set_free(res);
6831 if (ok < 0)
6832 return -1;
6833 if (!ok)
6834 isl_die(ctx, isl_error_unknown,
6835 "unexpected result", return -1);
6838 return 0;
6841 /* Inputs for basic tests of functions that select
6842 * subparts of an isl_union_map based on a relation
6843 * specified by an isl_multi_union_pw_aff.
6844 * "fn" is the function that is tested.
6845 * "arg1" and "arg2" are string descriptions of the inputs.
6846 * "res" is a string description of the expected result.
6848 struct {
6849 __isl_give isl_union_map *(*fn)(
6850 __isl_take isl_union_map *umap,
6851 __isl_take isl_multi_union_pw_aff *mupa);
6852 const char *arg1;
6853 const char *arg2;
6854 const char *res;
6855 } bin_locus_tests[] = {
6856 { &isl_union_map_eq_at_multi_union_pw_aff,
6857 "{ A[i,j] -> B[i',j'] }",
6858 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6859 "{ A[i,j] -> B[i,j'] }" },
6860 { &isl_union_map_eq_at_multi_union_pw_aff,
6861 "{ A[i,j] -> B[i',j'] }",
6862 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6863 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6864 "{ A[i,j] -> B[i,j] }" },
6865 { &isl_union_map_eq_at_multi_union_pw_aff,
6866 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6867 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6868 "{ A[i,j] -> B[i,j'] }" },
6869 { &isl_union_map_eq_at_multi_union_pw_aff,
6870 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6871 "F[{ A[i,j] -> [i]; B[i,j] -> [i]; C[i,j] -> [0] }]",
6872 "{ A[i,j] -> B[i,j']; A[0,j] -> C[i',j'] }" },
6873 { &isl_union_map_eq_at_multi_union_pw_aff,
6874 "{ A[i,j] -> B[i',j'] }",
6875 "F[{ A[i,j] -> [i] : i > j; B[i,j] -> [i] }]",
6876 "{ A[i,j] -> B[i,j'] : i > j }" },
6877 { &isl_union_map_lex_lt_at_multi_union_pw_aff,
6878 "{ A[i,j] -> B[i',j'] }",
6879 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6880 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6881 "{ A[i,j] -> B[i',j'] : i,j << i',j' }" },
6882 { &isl_union_map_lex_gt_at_multi_union_pw_aff,
6883 "{ A[i,j] -> B[i',j'] }",
6884 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6885 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6886 "{ A[i,j] -> B[i',j'] : i,j >> i',j' }" },
6887 { &isl_union_map_eq_at_multi_union_pw_aff,
6888 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6889 "(F[] : { A[i,j]; B[i,j] })",
6890 "{ A[i,j] -> B[i',j'] }" },
6891 { &isl_union_map_eq_at_multi_union_pw_aff,
6892 "{ A[i,j] -> B[i',j'] }",
6893 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
6894 "{ A[i,j] -> B[i',j'] : i > j and i' < j' }" },
6895 { &isl_union_map_eq_at_multi_union_pw_aff,
6896 "[N] -> { A[i,j] -> B[i',j'] : i,i' <= N }",
6897 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
6898 "[N] -> { A[i,j] -> B[i',j'] : i > j and i' < j' and i,i' <= N }" },
6899 { &isl_union_map_eq_at_multi_union_pw_aff,
6900 "{ A[i,j] -> B[i',j'] }",
6901 "[N] -> (F[] : { A[i,j] : i < N; B[i,j] : i < N })",
6902 "[N] -> { A[i,j] -> B[i',j'] : i,i' < N }" },
6903 { &isl_union_map_eq_at_multi_union_pw_aff,
6904 "{ A[i,j] -> B[i',j'] }",
6905 "[N] -> (F[] : { : N >= 0 })",
6906 "[N] -> { A[i,j] -> B[i',j'] : N >= 0 }" },
6909 /* Perform some basic tests of functions that select
6910 * subparts of an isl_union_map based on a relation
6911 * specified by an isl_multi_union_pw_aff.
6913 static int test_bin_locus(isl_ctx *ctx)
6915 int i;
6916 isl_bool ok;
6917 isl_union_map *umap, *res;
6918 isl_multi_union_pw_aff *mupa;
6920 for (i = 0; i < ARRAY_SIZE(bin_locus_tests); ++i) {
6921 umap = isl_union_map_read_from_str(ctx,
6922 bin_locus_tests[i].arg1);
6923 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6924 bin_locus_tests[i].arg2);
6925 res = isl_union_map_read_from_str(ctx, bin_locus_tests[i].res);
6926 umap = bin_locus_tests[i].fn(umap, mupa);
6927 ok = isl_union_map_is_equal(umap, res);
6928 isl_union_map_free(umap);
6929 isl_union_map_free(res);
6930 if (ok < 0)
6931 return -1;
6932 if (!ok)
6933 isl_die(ctx, isl_error_unknown,
6934 "unexpected result", return -1);
6937 return 0;
6940 /* Perform basic locus tests.
6942 static int test_locus(isl_ctx *ctx)
6944 if (test_un_locus(ctx) < 0)
6945 return -1;
6946 if (test_bin_locus(ctx) < 0)
6947 return -1;
6948 return 0;
6951 /* Test that isl_union_pw_qpolynomial_eval picks up the function
6952 * defined over the correct domain space.
6954 static int test_eval_1(isl_ctx *ctx)
6956 const char *str;
6957 isl_point *pnt;
6958 isl_set *set;
6959 isl_union_pw_qpolynomial *upwqp;
6960 isl_val *v;
6961 int cmp;
6963 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
6964 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
6965 str = "{ A[6] }";
6966 set = isl_set_read_from_str(ctx, str);
6967 pnt = isl_set_sample_point(set);
6968 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
6969 cmp = isl_val_cmp_si(v, 36);
6970 isl_val_free(v);
6972 if (!v)
6973 return -1;
6974 if (cmp != 0)
6975 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
6977 return 0;
6980 /* Check that isl_qpolynomial_eval handles getting called on a void point.
6982 static int test_eval_2(isl_ctx *ctx)
6984 const char *str;
6985 isl_point *pnt;
6986 isl_set *set;
6987 isl_qpolynomial *qp;
6988 isl_val *v;
6989 isl_bool ok;
6991 str = "{ A[x] -> [x] }";
6992 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
6993 str = "{ A[x] : false }";
6994 set = isl_set_read_from_str(ctx, str);
6995 pnt = isl_set_sample_point(set);
6996 v = isl_qpolynomial_eval(qp, pnt);
6997 ok = isl_val_is_nan(v);
6998 isl_val_free(v);
7000 if (ok < 0)
7001 return -1;
7002 if (!ok)
7003 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
7005 return 0;
7008 /* Inputs for isl_pw_aff_eval test.
7009 * "f" is the affine function.
7010 * "p" is the point where the function should be evaluated.
7011 * "res" is the expected result.
7013 struct {
7014 const char *f;
7015 const char *p;
7016 const char *res;
7017 } aff_eval_tests[] = {
7018 { "{ [i] -> [2 * i] }", "{ [4] }", "8" },
7019 { "{ [i] -> [2 * i] }", "{ [x] : false }", "NaN" },
7020 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [0] }", "0" },
7021 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [1] }", "1" },
7022 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [2] }", "3" },
7023 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [3] }", "5" },
7024 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [4] }", "7" },
7025 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [0] }", "0" },
7026 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [1] }", "0" },
7027 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [2] }", "0" },
7028 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [3] }", "0" },
7029 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [4] }", "1" },
7030 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [6] }", "1" },
7031 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [8] }", "2" },
7032 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [4] }", "4" },
7033 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [-2] }", "2" },
7034 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [0] }", "NaN" },
7035 { "[N] -> { [2 * N] }", "[N] -> { : N = 4 }", "8" },
7036 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 1] }", "1" },
7037 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 2] }", "3/2" },
7038 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [4] }", "4" },
7039 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [3] }", "NaN" },
7040 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [x] : false }", "NaN" },
7043 /* Perform basic isl_pw_aff_eval tests.
7045 static int test_eval_aff(isl_ctx *ctx)
7047 int i;
7049 for (i = 0; i < ARRAY_SIZE(aff_eval_tests); ++i) {
7050 isl_stat r;
7051 isl_pw_aff *pa;
7052 isl_set *set;
7053 isl_point *pnt;
7054 isl_val *v;
7056 pa = isl_pw_aff_read_from_str(ctx, aff_eval_tests[i].f);
7057 set = isl_set_read_from_str(ctx, aff_eval_tests[i].p);
7058 pnt = isl_set_sample_point(set);
7059 v = isl_pw_aff_eval(pa, pnt);
7060 r = val_check_equal(v, aff_eval_tests[i].res);
7061 isl_val_free(v);
7062 if (r < 0)
7063 return -1;
7065 return 0;
7068 /* Perform basic evaluation tests.
7070 static int test_eval(isl_ctx *ctx)
7072 if (test_eval_1(ctx) < 0)
7073 return -1;
7074 if (test_eval_2(ctx) < 0)
7075 return -1;
7076 if (test_eval_aff(ctx) < 0)
7077 return -1;
7078 return 0;
7081 /* Descriptions of sets that are tested for reparsing after printing.
7083 const char *output_tests[] = {
7084 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
7085 "{ [x] : 1 = 0 }",
7086 "{ [x] : false }",
7087 "{ [x] : x mod 2 = 0 }",
7088 "{ [x] : x mod 2 = 1 }",
7089 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
7090 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
7091 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7092 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7093 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
7094 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
7097 /* Check that printing a set and reparsing a set from the printed output
7098 * results in the same set.
7100 static int test_output_set(isl_ctx *ctx)
7102 int i;
7103 char *str;
7104 isl_set *set1, *set2;
7105 isl_bool equal;
7107 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
7108 set1 = isl_set_read_from_str(ctx, output_tests[i]);
7109 str = isl_set_to_str(set1);
7110 set2 = isl_set_read_from_str(ctx, str);
7111 free(str);
7112 equal = isl_set_is_equal(set1, set2);
7113 isl_set_free(set1);
7114 isl_set_free(set2);
7115 if (equal < 0)
7116 return -1;
7117 if (!equal)
7118 isl_die(ctx, isl_error_unknown,
7119 "parsed output not the same", return -1);
7122 return 0;
7125 /* Check that an isl_multi_aff is printed using a consistent space.
7127 static isl_stat test_output_ma(isl_ctx *ctx)
7129 char *str;
7130 isl_bool equal;
7131 isl_aff *aff;
7132 isl_multi_aff *ma, *ma2;
7134 ma = isl_multi_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7135 aff = isl_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7136 ma = isl_multi_aff_set_aff(ma, 0, aff);
7137 str = isl_multi_aff_to_str(ma);
7138 ma2 = isl_multi_aff_read_from_str(ctx, str);
7139 free(str);
7140 equal = isl_multi_aff_plain_is_equal(ma, ma2);
7141 isl_multi_aff_free(ma2);
7142 isl_multi_aff_free(ma);
7144 if (equal < 0)
7145 return isl_stat_error;
7146 if (!equal)
7147 isl_die(ctx, isl_error_unknown, "bad conversion",
7148 return isl_stat_error);
7150 return isl_stat_ok;
7153 /* Check that an isl_multi_pw_aff is printed using a consistent space.
7155 static isl_stat test_output_mpa(isl_ctx *ctx)
7157 char *str;
7158 isl_bool equal;
7159 isl_pw_aff *pa;
7160 isl_multi_pw_aff *mpa, *mpa2;
7162 mpa = isl_multi_pw_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7163 pa = isl_pw_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7164 mpa = isl_multi_pw_aff_set_pw_aff(mpa, 0, pa);
7165 str = isl_multi_pw_aff_to_str(mpa);
7166 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
7167 free(str);
7168 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
7169 isl_multi_pw_aff_free(mpa2);
7170 isl_multi_pw_aff_free(mpa);
7172 if (equal < 0)
7173 return isl_stat_error;
7174 if (!equal)
7175 isl_die(ctx, isl_error_unknown, "bad conversion",
7176 return isl_stat_error);
7178 return isl_stat_ok;
7181 int test_output(isl_ctx *ctx)
7183 char *s;
7184 const char *str;
7185 isl_pw_aff *pa;
7186 isl_printer *p;
7187 int equal;
7189 if (test_output_set(ctx) < 0)
7190 return -1;
7191 if (test_output_ma(ctx) < 0)
7192 return -1;
7193 if (test_output_mpa(ctx) < 0)
7194 return -1;
7196 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
7197 pa = isl_pw_aff_read_from_str(ctx, str);
7199 p = isl_printer_to_str(ctx);
7200 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
7201 p = isl_printer_print_pw_aff(p, pa);
7202 s = isl_printer_get_str(p);
7203 isl_printer_free(p);
7204 isl_pw_aff_free(pa);
7205 if (!s)
7206 equal = -1;
7207 else
7208 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
7209 free(s);
7210 if (equal < 0)
7211 return -1;
7212 if (!equal)
7213 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7215 return 0;
7218 int test_sample(isl_ctx *ctx)
7220 const char *str;
7221 isl_basic_set *bset1, *bset2;
7222 int empty, subset;
7224 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
7225 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
7226 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
7227 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
7228 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
7229 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
7230 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
7231 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
7232 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
7233 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
7234 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
7235 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
7236 "d - 1073741821e and "
7237 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
7238 "3j >= 1 - a + b + 2e and "
7239 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
7240 "3i <= 4 - a + 4b - e and "
7241 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
7242 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
7243 "c <= -1 + a and 3i >= -2 - a + 3e and "
7244 "1073741822e <= 1073741823 - a + 1073741822b + c and "
7245 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
7246 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
7247 "1073741823e >= 1 + 1073741823b - d and "
7248 "3i >= 1073741823b + c - 1073741823e - f and "
7249 "3i >= 1 + 2b + e + 3g }";
7250 bset1 = isl_basic_set_read_from_str(ctx, str);
7251 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
7252 empty = isl_basic_set_is_empty(bset2);
7253 subset = isl_basic_set_is_subset(bset2, bset1);
7254 isl_basic_set_free(bset1);
7255 isl_basic_set_free(bset2);
7256 if (empty < 0 || subset < 0)
7257 return -1;
7258 if (empty)
7259 isl_die(ctx, isl_error_unknown, "point not found", return -1);
7260 if (!subset)
7261 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
7263 return 0;
7266 int test_fixed_power(isl_ctx *ctx)
7268 const char *str;
7269 isl_map *map;
7270 isl_val *exp;
7271 int equal;
7273 str = "{ [i] -> [i + 1] }";
7274 map = isl_map_read_from_str(ctx, str);
7275 exp = isl_val_int_from_si(ctx, 23);
7276 map = isl_map_fixed_power_val(map, exp);
7277 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
7278 isl_map_free(map);
7279 if (equal < 0)
7280 return -1;
7282 return 0;
7285 int test_slice(isl_ctx *ctx)
7287 const char *str;
7288 isl_map *map;
7289 int equal;
7291 str = "{ [i] -> [j] }";
7292 map = isl_map_read_from_str(ctx, str);
7293 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
7294 equal = map_check_equal(map, "{ [i] -> [i] }");
7295 isl_map_free(map);
7296 if (equal < 0)
7297 return -1;
7299 str = "{ [i] -> [j] }";
7300 map = isl_map_read_from_str(ctx, str);
7301 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
7302 equal = map_check_equal(map, "{ [i] -> [j] }");
7303 isl_map_free(map);
7304 if (equal < 0)
7305 return -1;
7307 str = "{ [i] -> [j] }";
7308 map = isl_map_read_from_str(ctx, str);
7309 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
7310 equal = map_check_equal(map, "{ [i] -> [-i] }");
7311 isl_map_free(map);
7312 if (equal < 0)
7313 return -1;
7315 str = "{ [i] -> [j] }";
7316 map = isl_map_read_from_str(ctx, str);
7317 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
7318 equal = map_check_equal(map, "{ [0] -> [j] }");
7319 isl_map_free(map);
7320 if (equal < 0)
7321 return -1;
7323 str = "{ [i] -> [j] }";
7324 map = isl_map_read_from_str(ctx, str);
7325 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
7326 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
7327 isl_map_free(map);
7328 if (equal < 0)
7329 return -1;
7331 str = "{ [i] -> [j] }";
7332 map = isl_map_read_from_str(ctx, str);
7333 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
7334 equal = map_check_equal(map, "{ [i] -> [j] : false }");
7335 isl_map_free(map);
7336 if (equal < 0)
7337 return -1;
7339 return 0;
7342 int test_eliminate(isl_ctx *ctx)
7344 const char *str;
7345 isl_map *map;
7346 int equal;
7348 str = "{ [i] -> [j] : i = 2j }";
7349 map = isl_map_read_from_str(ctx, str);
7350 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
7351 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
7352 isl_map_free(map);
7353 if (equal < 0)
7354 return -1;
7356 return 0;
7359 /* Check that isl_set_dim_residue_class detects that the values of j
7360 * in the set below are all odd and that it does not detect any spurious
7361 * strides.
7363 static int test_residue_class(isl_ctx *ctx)
7365 const char *str;
7366 isl_set *set;
7367 isl_int m, r;
7368 isl_stat res;
7370 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
7371 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
7372 set = isl_set_read_from_str(ctx, str);
7373 isl_int_init(m);
7374 isl_int_init(r);
7375 res = isl_set_dim_residue_class(set, 1, &m, &r);
7376 if (res >= 0 &&
7377 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
7378 isl_die(ctx, isl_error_unknown, "incorrect residue class",
7379 res = isl_stat_error);
7380 isl_int_clear(r);
7381 isl_int_clear(m);
7382 isl_set_free(set);
7384 return res;
7387 static int test_align_parameters_1(isl_ctx *ctx)
7389 const char *str;
7390 isl_space *space;
7391 isl_multi_aff *ma1, *ma2;
7392 int equal;
7394 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
7395 ma1 = isl_multi_aff_read_from_str(ctx, str);
7397 space = isl_space_params_alloc(ctx, 1);
7398 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
7399 ma1 = isl_multi_aff_align_params(ma1, space);
7401 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
7402 ma2 = isl_multi_aff_read_from_str(ctx, str);
7404 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
7406 isl_multi_aff_free(ma1);
7407 isl_multi_aff_free(ma2);
7409 if (equal < 0)
7410 return -1;
7411 if (!equal)
7412 isl_die(ctx, isl_error_unknown,
7413 "result not as expected", return -1);
7415 return 0;
7418 /* Check the isl_multi_*_from_*_list operation in case inputs
7419 * have unaligned parameters.
7420 * In particular, older versions of isl would simply fail
7421 * (without printing any error message).
7423 static isl_stat test_align_parameters_2(isl_ctx *ctx)
7425 isl_space *space;
7426 isl_map *map;
7427 isl_aff *aff;
7428 isl_multi_aff *ma;
7430 map = isl_map_read_from_str(ctx, "{ A[] -> M[x] }");
7431 space = isl_map_get_space(map);
7432 isl_map_free(map);
7434 aff = isl_aff_read_from_str(ctx, "[N] -> { A[] -> [N] }");
7435 ma = isl_multi_aff_from_aff_list(space, isl_aff_list_from_aff(aff));
7436 isl_multi_aff_free(ma);
7438 if (!ma)
7439 return isl_stat_error;
7440 return isl_stat_ok;
7443 /* Perform basic parameter alignment tests.
7445 static int test_align_parameters(isl_ctx *ctx)
7447 if (test_align_parameters_1(ctx) < 0)
7448 return -1;
7449 if (test_align_parameters_2(ctx) < 0)
7450 return -1;
7452 return 0;
7455 /* Check that isl_*_drop_unused_params actually drops the unused parameters
7456 * by comparing the result using isl_*_plain_is_equal.
7457 * Note that this assumes that isl_*_plain_is_equal does not consider
7458 * objects that only differ by unused parameters to be equal.
7460 int test_drop_unused_parameters(isl_ctx *ctx)
7462 const char *str_with, *str_without;
7463 isl_basic_set *bset1, *bset2;
7464 isl_set *set1, *set2;
7465 isl_pw_aff *pwa1, *pwa2;
7466 int equal;
7468 str_with = "[n, m, o] -> { [m] }";
7469 str_without = "[m] -> { [m] }";
7471 bset1 = isl_basic_set_read_from_str(ctx, str_with);
7472 bset2 = isl_basic_set_read_from_str(ctx, str_without);
7473 bset1 = isl_basic_set_drop_unused_params(bset1);
7474 equal = isl_basic_set_plain_is_equal(bset1, bset2);
7475 isl_basic_set_free(bset1);
7476 isl_basic_set_free(bset2);
7478 if (equal < 0)
7479 return -1;
7480 if (!equal)
7481 isl_die(ctx, isl_error_unknown,
7482 "result not as expected", return -1);
7484 set1 = isl_set_read_from_str(ctx, str_with);
7485 set2 = isl_set_read_from_str(ctx, str_without);
7486 set1 = isl_set_drop_unused_params(set1);
7487 equal = isl_set_plain_is_equal(set1, set2);
7488 isl_set_free(set1);
7489 isl_set_free(set2);
7491 if (equal < 0)
7492 return -1;
7493 if (!equal)
7494 isl_die(ctx, isl_error_unknown,
7495 "result not as expected", return -1);
7497 pwa1 = isl_pw_aff_read_from_str(ctx, str_with);
7498 pwa2 = isl_pw_aff_read_from_str(ctx, str_without);
7499 pwa1 = isl_pw_aff_drop_unused_params(pwa1);
7500 equal = isl_pw_aff_plain_is_equal(pwa1, pwa2);
7501 isl_pw_aff_free(pwa1);
7502 isl_pw_aff_free(pwa2);
7504 if (equal < 0)
7505 return -1;
7506 if (!equal)
7507 isl_die(ctx, isl_error_unknown,
7508 "result not as expected", return -1);
7510 return 0;
7513 static int test_list(isl_ctx *ctx)
7515 isl_id *a, *b, *c, *d, *id;
7516 isl_id_list *list;
7517 isl_size n;
7518 int ok;
7520 a = isl_id_alloc(ctx, "a", NULL);
7521 b = isl_id_alloc(ctx, "b", NULL);
7522 c = isl_id_alloc(ctx, "c", NULL);
7523 d = isl_id_alloc(ctx, "d", NULL);
7525 list = isl_id_list_alloc(ctx, 4);
7526 list = isl_id_list_add(list, b);
7527 list = isl_id_list_insert(list, 0, a);
7528 list = isl_id_list_add(list, c);
7529 list = isl_id_list_add(list, d);
7530 list = isl_id_list_drop(list, 1, 1);
7532 n = isl_id_list_n_id(list);
7533 if (n < 0)
7534 return -1;
7535 if (n != 3) {
7536 isl_id_list_free(list);
7537 isl_die(ctx, isl_error_unknown,
7538 "unexpected number of elements in list", return -1);
7541 id = isl_id_list_get_id(list, 0);
7542 ok = id == a;
7543 isl_id_free(id);
7544 id = isl_id_list_get_id(list, 1);
7545 ok = ok && id == c;
7546 isl_id_free(id);
7547 id = isl_id_list_get_id(list, 2);
7548 ok = ok && id == d;
7549 isl_id_free(id);
7551 isl_id_list_free(list);
7553 if (!ok)
7554 isl_die(ctx, isl_error_unknown,
7555 "unexpected elements in list", return -1);
7557 return 0;
7560 const char *set_conversion_tests[] = {
7561 "[N] -> { [i] : N - 1 <= 2 i <= N }",
7562 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
7563 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7564 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7565 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
7566 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
7567 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
7568 "-3 + c <= d <= 28 + c) }",
7571 /* Check that converting from isl_set to isl_pw_multi_aff and back
7572 * to isl_set produces the original isl_set.
7574 static int test_set_conversion(isl_ctx *ctx)
7576 int i;
7577 const char *str;
7578 isl_set *set1, *set2;
7579 isl_pw_multi_aff *pma;
7580 int equal;
7582 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
7583 str = set_conversion_tests[i];
7584 set1 = isl_set_read_from_str(ctx, str);
7585 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
7586 set2 = isl_set_from_pw_multi_aff(pma);
7587 equal = isl_set_is_equal(set1, set2);
7588 isl_set_free(set1);
7589 isl_set_free(set2);
7591 if (equal < 0)
7592 return -1;
7593 if (!equal)
7594 isl_die(ctx, isl_error_unknown, "bad conversion",
7595 return -1);
7598 return 0;
7601 const char *conversion_tests[] = {
7602 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7603 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7604 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7605 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7606 "9e <= -2 - 2a) }",
7607 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7608 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7609 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7612 /* Check that converting from isl_map to isl_pw_multi_aff and back
7613 * to isl_map produces the original isl_map.
7615 static int test_map_conversion(isl_ctx *ctx)
7617 int i;
7618 isl_map *map1, *map2;
7619 isl_pw_multi_aff *pma;
7620 int equal;
7622 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
7623 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
7624 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
7625 map2 = isl_map_from_pw_multi_aff(pma);
7626 equal = isl_map_is_equal(map1, map2);
7627 isl_map_free(map1);
7628 isl_map_free(map2);
7630 if (equal < 0)
7631 return -1;
7632 if (!equal)
7633 isl_die(ctx, isl_error_unknown, "bad conversion",
7634 return -1);
7637 return 0;
7640 /* Descriptions of isl_pw_multi_aff objects for testing conversion
7641 * to isl_multi_pw_aff and back.
7643 const char *mpa_conversion_tests[] = {
7644 "{ [x] -> A[x] }",
7645 "{ [x] -> A[x] : x >= 0 }",
7646 "{ [x] -> A[x] : x >= 0; [x] -> A[-x] : x < 0 }",
7647 "{ [x] -> A[x, x + 1] }",
7648 "{ [x] -> A[] }",
7649 "{ [x] -> A[] : x >= 0 }",
7652 /* Check that conversion from isl_pw_multi_aff to isl_multi_pw_aff and
7653 * back to isl_pw_multi_aff preserves the original meaning.
7655 static int test_mpa_conversion(isl_ctx *ctx)
7657 int i;
7658 isl_pw_multi_aff *pma1, *pma2;
7659 isl_multi_pw_aff *mpa;
7660 int equal;
7662 for (i = 0; i < ARRAY_SIZE(mpa_conversion_tests); ++i) {
7663 const char *str;
7664 str = mpa_conversion_tests[i];
7665 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
7666 pma2 = isl_pw_multi_aff_copy(pma1);
7667 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma1);
7668 pma1 = isl_pw_multi_aff_from_multi_pw_aff(mpa);
7669 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7670 isl_pw_multi_aff_free(pma1);
7671 isl_pw_multi_aff_free(pma2);
7673 if (equal < 0)
7674 return -1;
7675 if (!equal)
7676 isl_die(ctx, isl_error_unknown, "bad conversion",
7677 return -1);
7680 return 0;
7683 /* Descriptions of union maps that should be convertible
7684 * to an isl_multi_union_pw_aff.
7686 const char *umap_mupa_conversion_tests[] = {
7687 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7688 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7689 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7690 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7691 "9e <= -2 - 2a) }",
7692 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7693 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7694 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7695 "{ A[] -> B[0]; C[] -> B[1] }",
7696 "{ A[] -> B[]; C[] -> B[] }",
7699 /* Check that converting from isl_union_map to isl_multi_union_pw_aff and back
7700 * to isl_union_map produces the original isl_union_map.
7702 static int test_union_map_mupa_conversion(isl_ctx *ctx)
7704 int i;
7705 isl_union_map *umap1, *umap2;
7706 isl_multi_union_pw_aff *mupa;
7707 int equal;
7709 for (i = 0; i < ARRAY_SIZE(umap_mupa_conversion_tests); ++i) {
7710 const char *str;
7711 str = umap_mupa_conversion_tests[i];
7712 umap1 = isl_union_map_read_from_str(ctx, str);
7713 umap2 = isl_union_map_copy(umap1);
7714 mupa = isl_multi_union_pw_aff_from_union_map(umap2);
7715 umap2 = isl_union_map_from_multi_union_pw_aff(mupa);
7716 equal = isl_union_map_is_equal(umap1, umap2);
7717 isl_union_map_free(umap1);
7718 isl_union_map_free(umap2);
7720 if (equal < 0)
7721 return -1;
7722 if (!equal)
7723 isl_die(ctx, isl_error_unknown, "bad conversion",
7724 return -1);
7727 return 0;
7730 static int test_conversion(isl_ctx *ctx)
7732 if (test_set_conversion(ctx) < 0)
7733 return -1;
7734 if (test_map_conversion(ctx) < 0)
7735 return -1;
7736 if (test_mpa_conversion(ctx) < 0)
7737 return -1;
7738 if (test_union_map_mupa_conversion(ctx) < 0)
7739 return -1;
7740 return 0;
7743 /* Check that isl_basic_map_curry does not modify input.
7745 static int test_curry(isl_ctx *ctx)
7747 const char *str;
7748 isl_basic_map *bmap1, *bmap2;
7749 int equal;
7751 str = "{ [A[] -> B[]] -> C[] }";
7752 bmap1 = isl_basic_map_read_from_str(ctx, str);
7753 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
7754 equal = isl_basic_map_is_equal(bmap1, bmap2);
7755 isl_basic_map_free(bmap1);
7756 isl_basic_map_free(bmap2);
7758 if (equal < 0)
7759 return -1;
7760 if (equal)
7761 isl_die(ctx, isl_error_unknown,
7762 "curried map should not be equal to original",
7763 return -1);
7765 return 0;
7768 struct {
7769 const char *set;
7770 const char *ma;
7771 const char *res;
7772 } preimage_tests[] = {
7773 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
7774 "{ A[j,i] -> B[i,j] }",
7775 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
7776 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
7777 "{ A[a,b] -> B[a/2,b/6] }",
7778 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
7779 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
7780 "{ A[a,b] -> B[a/2,b/6] }",
7781 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
7782 "exists i,j : a = 2 i and b = 6 j }" },
7783 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
7784 "[n] -> { : 0 <= n <= 100 }" },
7785 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
7786 "{ A[a] -> B[2a] }",
7787 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
7788 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
7789 "{ A[a] -> B[([a/2])] }",
7790 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
7791 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
7792 "{ A[a] -> B[a,a,a/3] }",
7793 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
7794 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
7795 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
7798 static int test_preimage_basic_set(isl_ctx *ctx)
7800 int i;
7801 isl_basic_set *bset1, *bset2;
7802 isl_multi_aff *ma;
7803 int equal;
7805 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
7806 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
7807 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
7808 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
7809 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
7810 equal = isl_basic_set_is_equal(bset1, bset2);
7811 isl_basic_set_free(bset1);
7812 isl_basic_set_free(bset2);
7813 if (equal < 0)
7814 return -1;
7815 if (!equal)
7816 isl_die(ctx, isl_error_unknown, "bad preimage",
7817 return -1);
7820 return 0;
7823 struct {
7824 const char *map;
7825 const char *ma;
7826 const char *res;
7827 } preimage_domain_tests[] = {
7828 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
7829 "{ A[j,i] -> B[i,j] }",
7830 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
7831 { "{ B[i] -> C[i]; D[i] -> E[i] }",
7832 "{ A[i] -> B[i + 1] }",
7833 "{ A[i] -> C[i + 1] }" },
7834 { "{ B[i] -> C[i]; B[i] -> E[i] }",
7835 "{ A[i] -> B[i + 1] }",
7836 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
7837 { "{ B[i] -> C[([i/2])] }",
7838 "{ A[i] -> B[2i] }",
7839 "{ A[i] -> C[i] }" },
7840 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
7841 "{ A[i] -> B[([i/5]), ([i/7])] }",
7842 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
7843 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
7844 "[N] -> { A[] -> B[([N/5])] }",
7845 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
7846 { "{ B[i] -> C[i] : exists a : i = 5 a }",
7847 "{ A[i] -> B[2i] }",
7848 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
7849 { "{ B[i] -> C[i] : exists a : i = 2 a; "
7850 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
7851 "{ A[i] -> B[2i] }",
7852 "{ A[i] -> C[2i] }" },
7853 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
7854 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
7857 static int test_preimage_union_map(isl_ctx *ctx)
7859 int i;
7860 isl_union_map *umap1, *umap2;
7861 isl_multi_aff *ma;
7862 int equal;
7864 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
7865 umap1 = isl_union_map_read_from_str(ctx,
7866 preimage_domain_tests[i].map);
7867 ma = isl_multi_aff_read_from_str(ctx,
7868 preimage_domain_tests[i].ma);
7869 umap2 = isl_union_map_read_from_str(ctx,
7870 preimage_domain_tests[i].res);
7871 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
7872 equal = isl_union_map_is_equal(umap1, umap2);
7873 isl_union_map_free(umap1);
7874 isl_union_map_free(umap2);
7875 if (equal < 0)
7876 return -1;
7877 if (!equal)
7878 isl_die(ctx, isl_error_unknown, "bad preimage",
7879 return -1);
7882 return 0;
7885 static int test_preimage(isl_ctx *ctx)
7887 if (test_preimage_basic_set(ctx) < 0)
7888 return -1;
7889 if (test_preimage_union_map(ctx) < 0)
7890 return -1;
7892 return 0;
7895 struct {
7896 const char *ma1;
7897 const char *ma;
7898 const char *res;
7899 } pullback_tests[] = {
7900 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
7901 "{ A[a,b] -> C[b + 2a] }" },
7902 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
7903 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
7904 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
7905 "{ A[a] -> C[(a)/6] }" },
7906 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
7907 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
7908 "{ A[a] -> C[(2a)/3] }" },
7909 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
7910 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
7911 "{ A[i,j] -> C[i + j, i + j] }"},
7912 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
7913 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
7914 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
7915 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
7916 "{ [i, j] -> [floor((i)/3), j] }",
7917 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
7920 static int test_pullback(isl_ctx *ctx)
7922 int i;
7923 isl_multi_aff *ma1, *ma2;
7924 isl_multi_aff *ma;
7925 int equal;
7927 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
7928 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
7929 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
7930 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
7931 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
7932 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
7933 isl_multi_aff_free(ma1);
7934 isl_multi_aff_free(ma2);
7935 if (equal < 0)
7936 return -1;
7937 if (!equal)
7938 isl_die(ctx, isl_error_unknown, "bad pullback",
7939 return -1);
7942 return 0;
7945 /* Check that negation is printed correctly and that equal expressions
7946 * are correctly identified.
7948 static int test_ast(isl_ctx *ctx)
7950 isl_ast_expr *expr, *expr1, *expr2, *expr3;
7951 char *str;
7952 int ok, equal;
7954 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
7955 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
7956 expr = isl_ast_expr_add(expr1, expr2);
7957 expr2 = isl_ast_expr_copy(expr);
7958 expr = isl_ast_expr_neg(expr);
7959 expr2 = isl_ast_expr_neg(expr2);
7960 equal = isl_ast_expr_is_equal(expr, expr2);
7961 str = isl_ast_expr_to_C_str(expr);
7962 ok = str ? !strcmp(str, "-(A + B)") : -1;
7963 free(str);
7964 isl_ast_expr_free(expr);
7965 isl_ast_expr_free(expr2);
7967 if (ok < 0 || equal < 0)
7968 return -1;
7969 if (!equal)
7970 isl_die(ctx, isl_error_unknown,
7971 "equal expressions not considered equal", return -1);
7972 if (!ok)
7973 isl_die(ctx, isl_error_unknown,
7974 "isl_ast_expr printed incorrectly", return -1);
7976 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
7977 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
7978 expr = isl_ast_expr_add(expr1, expr2);
7979 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
7980 expr = isl_ast_expr_sub(expr3, expr);
7981 str = isl_ast_expr_to_C_str(expr);
7982 ok = str ? !strcmp(str, "C - (A + B)") : -1;
7983 free(str);
7984 isl_ast_expr_free(expr);
7986 if (ok < 0)
7987 return -1;
7988 if (!ok)
7989 isl_die(ctx, isl_error_unknown,
7990 "isl_ast_expr printed incorrectly", return -1);
7992 return 0;
7995 /* Check that isl_ast_build_expr_from_set returns a valid expression
7996 * for an empty set. Note that isl_ast_build_expr_from_set getting
7997 * called on an empty set probably indicates a bug in the caller.
7999 static int test_ast_build(isl_ctx *ctx)
8001 isl_set *set;
8002 isl_ast_build *build;
8003 isl_ast_expr *expr;
8005 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8006 build = isl_ast_build_from_context(set);
8008 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
8009 expr = isl_ast_build_expr_from_set(build, set);
8011 isl_ast_expr_free(expr);
8012 isl_ast_build_free(build);
8014 if (!expr)
8015 return -1;
8017 return 0;
8020 /* Internal data structure for before_for and after_for callbacks.
8022 * depth is the current depth
8023 * before is the number of times before_for has been called
8024 * after is the number of times after_for has been called
8026 struct isl_test_codegen_data {
8027 int depth;
8028 int before;
8029 int after;
8032 /* This function is called before each for loop in the AST generated
8033 * from test_ast_gen1.
8035 * Increment the number of calls and the depth.
8036 * Check that the space returned by isl_ast_build_get_schedule_space
8037 * matches the target space of the schedule returned by
8038 * isl_ast_build_get_schedule.
8039 * Return an isl_id that is checked by the corresponding call
8040 * to after_for.
8042 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
8043 void *user)
8045 struct isl_test_codegen_data *data = user;
8046 isl_ctx *ctx;
8047 isl_space *space;
8048 isl_union_map *schedule;
8049 isl_union_set *uset;
8050 isl_set *set;
8051 isl_bool empty;
8052 isl_size n;
8053 char name[] = "d0";
8055 ctx = isl_ast_build_get_ctx(build);
8057 if (data->before >= 3)
8058 isl_die(ctx, isl_error_unknown,
8059 "unexpected number of for nodes", return NULL);
8060 if (data->depth >= 2)
8061 isl_die(ctx, isl_error_unknown,
8062 "unexpected depth", return NULL);
8064 snprintf(name, sizeof(name), "d%d", data->depth);
8065 data->before++;
8066 data->depth++;
8068 schedule = isl_ast_build_get_schedule(build);
8069 uset = isl_union_map_range(schedule);
8070 n = isl_union_set_n_set(uset);
8071 if (n != 1) {
8072 isl_union_set_free(uset);
8073 if (n < 0)
8074 return NULL;
8075 isl_die(ctx, isl_error_unknown,
8076 "expecting single range space", return NULL);
8079 space = isl_ast_build_get_schedule_space(build);
8080 set = isl_union_set_extract_set(uset, space);
8081 isl_union_set_free(uset);
8082 empty = isl_set_is_empty(set);
8083 isl_set_free(set);
8085 if (empty < 0)
8086 return NULL;
8087 if (empty)
8088 isl_die(ctx, isl_error_unknown,
8089 "spaces don't match", return NULL);
8091 return isl_id_alloc(ctx, name, NULL);
8094 /* This function is called after each for loop in the AST generated
8095 * from test_ast_gen1.
8097 * Increment the number of calls and decrement the depth.
8098 * Check that the annotation attached to the node matches
8099 * the isl_id returned by the corresponding call to before_for.
8101 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
8102 __isl_keep isl_ast_build *build, void *user)
8104 struct isl_test_codegen_data *data = user;
8105 isl_id *id;
8106 const char *name;
8107 int valid;
8109 data->after++;
8110 data->depth--;
8112 if (data->after > data->before)
8113 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8114 "mismatch in number of for nodes",
8115 return isl_ast_node_free(node));
8117 id = isl_ast_node_get_annotation(node);
8118 if (!id)
8119 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8120 "missing annotation", return isl_ast_node_free(node));
8122 name = isl_id_get_name(id);
8123 valid = name && atoi(name + 1) == data->depth;
8124 isl_id_free(id);
8126 if (!valid)
8127 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8128 "wrong annotation", return isl_ast_node_free(node));
8130 return node;
8133 /* Check that the before_each_for and after_each_for callbacks
8134 * are called for each for loop in the generated code,
8135 * that they are called in the right order and that the isl_id
8136 * returned from the before_each_for callback is attached to
8137 * the isl_ast_node passed to the corresponding after_each_for call.
8139 static int test_ast_gen1(isl_ctx *ctx)
8141 const char *str;
8142 isl_set *set;
8143 isl_union_map *schedule;
8144 isl_ast_build *build;
8145 isl_ast_node *tree;
8146 struct isl_test_codegen_data data;
8148 str = "[N] -> { : N >= 10 }";
8149 set = isl_set_read_from_str(ctx, str);
8150 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
8151 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
8152 schedule = isl_union_map_read_from_str(ctx, str);
8154 data.before = 0;
8155 data.after = 0;
8156 data.depth = 0;
8157 build = isl_ast_build_from_context(set);
8158 build = isl_ast_build_set_before_each_for(build,
8159 &before_for, &data);
8160 build = isl_ast_build_set_after_each_for(build,
8161 &after_for, &data);
8162 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8163 isl_ast_build_free(build);
8164 if (!tree)
8165 return -1;
8167 isl_ast_node_free(tree);
8169 if (data.before != 3 || data.after != 3)
8170 isl_die(ctx, isl_error_unknown,
8171 "unexpected number of for nodes", return -1);
8173 return 0;
8176 /* Check that the AST generator handles domains that are integrally disjoint
8177 * but not rationally disjoint.
8179 static int test_ast_gen2(isl_ctx *ctx)
8181 const char *str;
8182 isl_set *set;
8183 isl_union_map *schedule;
8184 isl_union_map *options;
8185 isl_ast_build *build;
8186 isl_ast_node *tree;
8188 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
8189 schedule = isl_union_map_read_from_str(ctx, str);
8190 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8191 build = isl_ast_build_from_context(set);
8193 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
8194 options = isl_union_map_read_from_str(ctx, str);
8195 build = isl_ast_build_set_options(build, options);
8196 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8197 isl_ast_build_free(build);
8198 if (!tree)
8199 return -1;
8200 isl_ast_node_free(tree);
8202 return 0;
8205 /* Increment *user on each call.
8207 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
8208 __isl_keep isl_ast_build *build, void *user)
8210 int *n = user;
8212 (*n)++;
8214 return node;
8217 /* Test that unrolling tries to minimize the number of instances.
8218 * In particular, for the schedule given below, make sure it generates
8219 * 3 nodes (rather than 101).
8221 static int test_ast_gen3(isl_ctx *ctx)
8223 const char *str;
8224 isl_set *set;
8225 isl_union_map *schedule;
8226 isl_union_map *options;
8227 isl_ast_build *build;
8228 isl_ast_node *tree;
8229 int n_domain = 0;
8231 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
8232 schedule = isl_union_map_read_from_str(ctx, str);
8233 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8235 str = "{ [i] -> unroll[0] }";
8236 options = isl_union_map_read_from_str(ctx, str);
8238 build = isl_ast_build_from_context(set);
8239 build = isl_ast_build_set_options(build, options);
8240 build = isl_ast_build_set_at_each_domain(build,
8241 &count_domains, &n_domain);
8242 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8243 isl_ast_build_free(build);
8244 if (!tree)
8245 return -1;
8247 isl_ast_node_free(tree);
8249 if (n_domain != 3)
8250 isl_die(ctx, isl_error_unknown,
8251 "unexpected number of for nodes", return -1);
8253 return 0;
8256 /* Check that if the ast_build_exploit_nested_bounds options is set,
8257 * we do not get an outer if node in the generated AST,
8258 * while we do get such an outer if node if the options is not set.
8260 static int test_ast_gen4(isl_ctx *ctx)
8262 const char *str;
8263 isl_set *set;
8264 isl_union_map *schedule;
8265 isl_ast_build *build;
8266 isl_ast_node *tree;
8267 enum isl_ast_node_type type;
8268 int enb;
8270 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
8271 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
8273 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
8275 schedule = isl_union_map_read_from_str(ctx, str);
8276 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8277 build = isl_ast_build_from_context(set);
8278 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8279 isl_ast_build_free(build);
8280 if (!tree)
8281 return -1;
8283 type = isl_ast_node_get_type(tree);
8284 isl_ast_node_free(tree);
8286 if (type == isl_ast_node_if)
8287 isl_die(ctx, isl_error_unknown,
8288 "not expecting if node", return -1);
8290 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
8292 schedule = isl_union_map_read_from_str(ctx, str);
8293 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8294 build = isl_ast_build_from_context(set);
8295 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8296 isl_ast_build_free(build);
8297 if (!tree)
8298 return -1;
8300 type = isl_ast_node_get_type(tree);
8301 isl_ast_node_free(tree);
8303 if (type != isl_ast_node_if)
8304 isl_die(ctx, isl_error_unknown,
8305 "expecting if node", return -1);
8307 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
8309 return 0;
8312 /* This function is called for each leaf in the AST generated
8313 * from test_ast_gen5.
8315 * We finalize the AST generation by extending the outer schedule
8316 * with a zero-dimensional schedule. If this results in any for loops,
8317 * then this means that we did not pass along enough information
8318 * about the outer schedule to the inner AST generation.
8320 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
8321 void *user)
8323 isl_union_map *schedule, *extra;
8324 isl_ast_node *tree;
8326 schedule = isl_ast_build_get_schedule(build);
8327 extra = isl_union_map_copy(schedule);
8328 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
8329 schedule = isl_union_map_range_product(schedule, extra);
8330 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8331 isl_ast_build_free(build);
8333 if (!tree)
8334 return NULL;
8336 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
8337 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
8338 "code should not contain any for loop",
8339 return isl_ast_node_free(tree));
8341 return tree;
8344 /* Check that we do not lose any information when going back and
8345 * forth between internal and external schedule.
8347 * In particular, we create an AST where we unroll the only
8348 * non-constant dimension in the schedule. We therefore do
8349 * not expect any for loops in the AST. However, older versions
8350 * of isl would not pass along enough information about the outer
8351 * schedule when performing an inner code generation from a create_leaf
8352 * callback, resulting in the inner code generation producing a for loop.
8354 static int test_ast_gen5(isl_ctx *ctx)
8356 const char *str;
8357 isl_set *set;
8358 isl_union_map *schedule, *options;
8359 isl_ast_build *build;
8360 isl_ast_node *tree;
8362 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
8363 schedule = isl_union_map_read_from_str(ctx, str);
8365 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
8366 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
8367 options = isl_union_map_read_from_str(ctx, str);
8369 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8370 build = isl_ast_build_from_context(set);
8371 build = isl_ast_build_set_options(build, options);
8372 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
8373 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8374 isl_ast_build_free(build);
8375 isl_ast_node_free(tree);
8376 if (!tree)
8377 return -1;
8379 return 0;
8382 /* Check that the expression
8384 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
8386 * is not combined into
8388 * min(n/2, 0)
8390 * as this would result in n/2 being evaluated in parts of
8391 * the definition domain where n is not a multiple of 2.
8393 static int test_ast_expr(isl_ctx *ctx)
8395 const char *str;
8396 isl_pw_aff *pa;
8397 isl_ast_build *build;
8398 isl_ast_expr *expr;
8399 int min_max;
8400 int is_min;
8402 min_max = isl_options_get_ast_build_detect_min_max(ctx);
8403 isl_options_set_ast_build_detect_min_max(ctx, 1);
8405 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
8406 pa = isl_pw_aff_read_from_str(ctx, str);
8407 build = isl_ast_build_alloc(ctx);
8408 expr = isl_ast_build_expr_from_pw_aff(build, pa);
8409 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
8410 isl_ast_expr_get_op_type(expr) == isl_ast_op_min;
8411 isl_ast_build_free(build);
8412 isl_ast_expr_free(expr);
8414 isl_options_set_ast_build_detect_min_max(ctx, min_max);
8416 if (!expr)
8417 return -1;
8418 if (is_min)
8419 isl_die(ctx, isl_error_unknown,
8420 "expressions should not be combined", return -1);
8422 return 0;
8425 static int test_ast_gen(isl_ctx *ctx)
8427 if (test_ast_gen1(ctx) < 0)
8428 return -1;
8429 if (test_ast_gen2(ctx) < 0)
8430 return -1;
8431 if (test_ast_gen3(ctx) < 0)
8432 return -1;
8433 if (test_ast_gen4(ctx) < 0)
8434 return -1;
8435 if (test_ast_gen5(ctx) < 0)
8436 return -1;
8437 if (test_ast_expr(ctx) < 0)
8438 return -1;
8439 return 0;
8442 /* Check if dropping output dimensions from an isl_pw_multi_aff
8443 * works properly.
8445 static int test_pw_multi_aff(isl_ctx *ctx)
8447 const char *str;
8448 isl_pw_multi_aff *pma1, *pma2;
8449 int equal;
8451 str = "{ [i,j] -> [i+j, 4i-j] }";
8452 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
8453 str = "{ [i,j] -> [4i-j] }";
8454 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
8456 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
8458 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
8460 isl_pw_multi_aff_free(pma1);
8461 isl_pw_multi_aff_free(pma2);
8462 if (equal < 0)
8463 return -1;
8464 if (!equal)
8465 isl_die(ctx, isl_error_unknown,
8466 "expressions not equal", return -1);
8468 return 0;
8471 /* Check that we can properly parse multi piecewise affine expressions
8472 * where the piecewise affine expressions have different domains.
8474 static int test_multi_pw_aff_1(isl_ctx *ctx)
8476 const char *str;
8477 isl_set *dom, *dom2;
8478 isl_multi_pw_aff *mpa1, *mpa2;
8479 isl_pw_aff *pa;
8480 int equal;
8481 int equal_domain;
8483 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
8484 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
8485 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
8486 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
8487 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
8488 str = "{ [i] -> [(i : i > 0), 2i] }";
8489 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
8491 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
8493 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
8494 dom = isl_pw_aff_domain(pa);
8495 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
8496 dom2 = isl_pw_aff_domain(pa);
8497 equal_domain = isl_set_is_equal(dom, dom2);
8499 isl_set_free(dom);
8500 isl_set_free(dom2);
8501 isl_multi_pw_aff_free(mpa1);
8502 isl_multi_pw_aff_free(mpa2);
8504 if (equal < 0)
8505 return -1;
8506 if (!equal)
8507 isl_die(ctx, isl_error_unknown,
8508 "expressions not equal", return -1);
8510 if (equal_domain < 0)
8511 return -1;
8512 if (equal_domain)
8513 isl_die(ctx, isl_error_unknown,
8514 "domains unexpectedly equal", return -1);
8516 return 0;
8519 /* Check that the dimensions in the explicit domain
8520 * of a multi piecewise affine expression are properly
8521 * taken into account.
8523 static int test_multi_pw_aff_2(isl_ctx *ctx)
8525 const char *str;
8526 isl_bool involves1, involves2, involves3, equal;
8527 isl_multi_pw_aff *mpa, *mpa1, *mpa2;
8529 str = "{ A[x,y] -> B[] : x >= y }";
8530 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
8531 involves1 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 2);
8532 mpa1 = isl_multi_pw_aff_copy(mpa);
8534 mpa = isl_multi_pw_aff_insert_dims(mpa, isl_dim_in, 0, 1);
8535 involves2 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 1);
8536 involves3 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 1, 2);
8537 str = "{ [a,x,y] -> B[] : x >= y }";
8538 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
8539 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
8540 isl_multi_pw_aff_free(mpa2);
8542 mpa = isl_multi_pw_aff_drop_dims(mpa, isl_dim_in, 0, 1);
8543 mpa = isl_multi_pw_aff_set_tuple_name(mpa, isl_dim_in, "A");
8544 if (equal >= 0 && equal)
8545 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa1);
8546 isl_multi_pw_aff_free(mpa1);
8547 isl_multi_pw_aff_free(mpa);
8549 if (involves1 < 0 || involves2 < 0 || involves3 < 0 || equal < 0)
8550 return -1;
8551 if (!equal)
8552 isl_die(ctx, isl_error_unknown,
8553 "incorrect result of dimension insertion/removal",
8554 return isl_stat_error);
8555 if (!involves1 || involves2 || !involves3)
8556 isl_die(ctx, isl_error_unknown,
8557 "incorrect characterization of involved dimensions",
8558 return isl_stat_error);
8560 return 0;
8563 /* Check that isl_multi_union_pw_aff_multi_val_on_domain
8564 * sets the explicit domain of a zero-dimensional result,
8565 * such that it can be converted to an isl_union_map.
8567 static isl_stat test_multi_pw_aff_3(isl_ctx *ctx)
8569 isl_space *space;
8570 isl_union_set *dom;
8571 isl_multi_val *mv;
8572 isl_multi_union_pw_aff *mupa;
8573 isl_union_map *umap;
8575 dom = isl_union_set_read_from_str(ctx, "{ A[]; B[] }");
8576 space = isl_union_set_get_space(dom);
8577 mv = isl_multi_val_zero(isl_space_set_from_params(space));
8578 mupa = isl_multi_union_pw_aff_multi_val_on_domain(dom, mv);
8579 umap = isl_union_map_from_multi_union_pw_aff(mupa);
8580 isl_union_map_free(umap);
8581 if (!umap)
8582 return isl_stat_error;
8584 return isl_stat_ok;
8587 /* Perform some tests on multi piecewise affine expressions.
8589 static int test_multi_pw_aff(isl_ctx *ctx)
8591 if (test_multi_pw_aff_1(ctx) < 0)
8592 return -1;
8593 if (test_multi_pw_aff_2(ctx) < 0)
8594 return -1;
8595 if (test_multi_pw_aff_3(ctx) < 0)
8596 return -1;
8597 return 0;
8600 /* This is a regression test for a bug where isl_basic_map_simplify
8601 * would end up in an infinite loop. In particular, we construct
8602 * an empty basic set that is not obviously empty.
8603 * isl_basic_set_is_empty marks the basic set as empty.
8604 * After projecting out i3, the variable can be dropped completely,
8605 * but isl_basic_map_simplify refrains from doing so if the basic set
8606 * is empty and would end up in an infinite loop if it didn't test
8607 * explicitly for empty basic maps in the outer loop.
8609 static int test_simplify_1(isl_ctx *ctx)
8611 const char *str;
8612 isl_basic_set *bset;
8613 int empty;
8615 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
8616 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
8617 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
8618 "i3 >= i2 }";
8619 bset = isl_basic_set_read_from_str(ctx, str);
8620 empty = isl_basic_set_is_empty(bset);
8621 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
8622 isl_basic_set_free(bset);
8623 if (!bset)
8624 return -1;
8625 if (!empty)
8626 isl_die(ctx, isl_error_unknown,
8627 "basic set should be empty", return -1);
8629 return 0;
8632 /* Check that the equality in the set description below
8633 * is simplified away.
8635 static int test_simplify_2(isl_ctx *ctx)
8637 const char *str;
8638 isl_basic_set *bset;
8639 isl_bool universe;
8641 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
8642 bset = isl_basic_set_read_from_str(ctx, str);
8643 universe = isl_basic_set_plain_is_universe(bset);
8644 isl_basic_set_free(bset);
8646 if (universe < 0)
8647 return -1;
8648 if (!universe)
8649 isl_die(ctx, isl_error_unknown,
8650 "equality not simplified away", return -1);
8651 return 0;
8654 /* Some simplification tests.
8656 static int test_simplify(isl_ctx *ctx)
8658 if (test_simplify_1(ctx) < 0)
8659 return -1;
8660 if (test_simplify_2(ctx) < 0)
8661 return -1;
8662 return 0;
8665 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
8666 * with gbr context would fail to disable the use of the shifted tableau
8667 * when transferring equalities for the input to the context, resulting
8668 * in invalid sample values.
8670 static int test_partial_lexmin(isl_ctx *ctx)
8672 const char *str;
8673 isl_basic_set *bset;
8674 isl_basic_map *bmap;
8675 isl_map *map;
8677 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
8678 bmap = isl_basic_map_read_from_str(ctx, str);
8679 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
8680 bset = isl_basic_set_read_from_str(ctx, str);
8681 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
8682 isl_map_free(map);
8684 if (!map)
8685 return -1;
8687 return 0;
8690 /* Check that the variable compression performed on the existentially
8691 * quantified variables inside isl_basic_set_compute_divs is not confused
8692 * by the implicit equalities among the parameters.
8694 static int test_compute_divs(isl_ctx *ctx)
8696 const char *str;
8697 isl_basic_set *bset;
8698 isl_set *set;
8700 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
8701 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
8702 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
8703 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
8704 bset = isl_basic_set_read_from_str(ctx, str);
8705 set = isl_basic_set_compute_divs(bset);
8706 isl_set_free(set);
8707 if (!set)
8708 return -1;
8710 return 0;
8713 /* Check that isl_schedule_get_map is not confused by a schedule tree
8714 * with divergent filter node parameters, as can result from a call
8715 * to isl_schedule_intersect_domain.
8717 static int test_schedule_tree(isl_ctx *ctx)
8719 const char *str;
8720 isl_union_set *uset;
8721 isl_schedule *sched1, *sched2;
8722 isl_union_map *umap;
8724 uset = isl_union_set_read_from_str(ctx, "{ A[i] }");
8725 sched1 = isl_schedule_from_domain(uset);
8726 uset = isl_union_set_read_from_str(ctx, "{ B[] }");
8727 sched2 = isl_schedule_from_domain(uset);
8729 sched1 = isl_schedule_sequence(sched1, sched2);
8730 str = "[n] -> { A[i] : 0 <= i < n; B[] }";
8731 uset = isl_union_set_read_from_str(ctx, str);
8732 sched1 = isl_schedule_intersect_domain(sched1, uset);
8733 umap = isl_schedule_get_map(sched1);
8734 isl_schedule_free(sched1);
8735 isl_union_map_free(umap);
8736 if (!umap)
8737 return -1;
8739 return 0;
8742 /* Check that a zero-dimensional prefix schedule keeps track
8743 * of the domain and outer filters.
8745 static int test_schedule_tree_prefix(isl_ctx *ctx)
8747 const char *str;
8748 isl_bool equal;
8749 isl_union_set *uset;
8750 isl_union_set_list *filters;
8751 isl_multi_union_pw_aff *mupa, *mupa2;
8752 isl_schedule_node *node;
8754 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
8755 uset = isl_union_set_read_from_str(ctx, str);
8756 node = isl_schedule_node_from_domain(uset);
8757 node = isl_schedule_node_child(node, 0);
8759 str = "{ S1[i,j] : i > j }";
8760 uset = isl_union_set_read_from_str(ctx, str);
8761 filters = isl_union_set_list_from_union_set(uset);
8762 str = "{ S1[i,j] : i <= j; S2[i,j] }";
8763 uset = isl_union_set_read_from_str(ctx, str);
8764 filters = isl_union_set_list_add(filters, uset);
8765 node = isl_schedule_node_insert_sequence(node, filters);
8767 node = isl_schedule_node_child(node, 0);
8768 node = isl_schedule_node_child(node, 0);
8769 mupa = isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
8770 str = "([] : { S1[i,j] : i > j })";
8771 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx, str);
8772 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
8773 isl_multi_union_pw_aff_free(mupa2);
8774 isl_multi_union_pw_aff_free(mupa);
8775 isl_schedule_node_free(node);
8777 if (equal < 0)
8778 return -1;
8779 if (!equal)
8780 isl_die(ctx, isl_error_unknown, "unexpected prefix schedule",
8781 return -1);
8783 return 0;
8786 /* Check that the reaching domain elements and the prefix schedule
8787 * at a leaf node are the same before and after grouping.
8789 static int test_schedule_tree_group_1(isl_ctx *ctx)
8791 int equal;
8792 const char *str;
8793 isl_id *id;
8794 isl_union_set *uset;
8795 isl_multi_union_pw_aff *mupa;
8796 isl_union_pw_multi_aff *upma1, *upma2;
8797 isl_union_set *domain1, *domain2;
8798 isl_union_map *umap1, *umap2;
8799 isl_schedule_node *node;
8801 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
8802 uset = isl_union_set_read_from_str(ctx, str);
8803 node = isl_schedule_node_from_domain(uset);
8804 node = isl_schedule_node_child(node, 0);
8805 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
8806 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8807 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8808 node = isl_schedule_node_child(node, 0);
8809 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
8810 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8811 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8812 node = isl_schedule_node_child(node, 0);
8813 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
8814 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
8815 domain1 = isl_schedule_node_get_domain(node);
8816 id = isl_id_alloc(ctx, "group", NULL);
8817 node = isl_schedule_node_parent(node);
8818 node = isl_schedule_node_group(node, id);
8819 node = isl_schedule_node_child(node, 0);
8820 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
8821 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
8822 domain2 = isl_schedule_node_get_domain(node);
8823 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
8824 if (equal >= 0 && equal)
8825 equal = isl_union_set_is_equal(domain1, domain2);
8826 if (equal >= 0 && equal)
8827 equal = isl_union_map_is_equal(umap1, umap2);
8828 isl_union_map_free(umap1);
8829 isl_union_map_free(umap2);
8830 isl_union_set_free(domain1);
8831 isl_union_set_free(domain2);
8832 isl_union_pw_multi_aff_free(upma1);
8833 isl_union_pw_multi_aff_free(upma2);
8834 isl_schedule_node_free(node);
8836 if (equal < 0)
8837 return -1;
8838 if (!equal)
8839 isl_die(ctx, isl_error_unknown,
8840 "expressions not equal", return -1);
8842 return 0;
8845 /* Check that we can have nested groupings and that the union map
8846 * schedule representation is the same before and after the grouping.
8847 * Note that after the grouping, the union map representation contains
8848 * the domain constraints from the ranges of the expansion nodes,
8849 * while they are missing from the union map representation of
8850 * the tree without expansion nodes.
8852 * Also check that the global expansion is as expected.
8854 static int test_schedule_tree_group_2(isl_ctx *ctx)
8856 int equal, equal_expansion;
8857 const char *str;
8858 isl_id *id;
8859 isl_union_set *uset;
8860 isl_union_map *umap1, *umap2;
8861 isl_union_map *expansion1, *expansion2;
8862 isl_union_set_list *filters;
8863 isl_multi_union_pw_aff *mupa;
8864 isl_schedule *schedule;
8865 isl_schedule_node *node;
8867 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
8868 "S3[i,j] : 0 <= i,j < 10 }";
8869 uset = isl_union_set_read_from_str(ctx, str);
8870 node = isl_schedule_node_from_domain(uset);
8871 node = isl_schedule_node_child(node, 0);
8872 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
8873 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8874 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8875 node = isl_schedule_node_child(node, 0);
8876 str = "{ S1[i,j] }";
8877 uset = isl_union_set_read_from_str(ctx, str);
8878 filters = isl_union_set_list_from_union_set(uset);
8879 str = "{ S2[i,j]; S3[i,j] }";
8880 uset = isl_union_set_read_from_str(ctx, str);
8881 filters = isl_union_set_list_add(filters, uset);
8882 node = isl_schedule_node_insert_sequence(node, filters);
8883 node = isl_schedule_node_child(node, 1);
8884 node = isl_schedule_node_child(node, 0);
8885 str = "{ S2[i,j] }";
8886 uset = isl_union_set_read_from_str(ctx, str);
8887 filters = isl_union_set_list_from_union_set(uset);
8888 str = "{ S3[i,j] }";
8889 uset = isl_union_set_read_from_str(ctx, str);
8890 filters = isl_union_set_list_add(filters, uset);
8891 node = isl_schedule_node_insert_sequence(node, filters);
8893 schedule = isl_schedule_node_get_schedule(node);
8894 umap1 = isl_schedule_get_map(schedule);
8895 uset = isl_schedule_get_domain(schedule);
8896 umap1 = isl_union_map_intersect_domain(umap1, uset);
8897 isl_schedule_free(schedule);
8899 node = isl_schedule_node_parent(node);
8900 node = isl_schedule_node_parent(node);
8901 id = isl_id_alloc(ctx, "group1", NULL);
8902 node = isl_schedule_node_group(node, id);
8903 node = isl_schedule_node_child(node, 1);
8904 node = isl_schedule_node_child(node, 0);
8905 id = isl_id_alloc(ctx, "group2", NULL);
8906 node = isl_schedule_node_group(node, id);
8908 schedule = isl_schedule_node_get_schedule(node);
8909 umap2 = isl_schedule_get_map(schedule);
8910 isl_schedule_free(schedule);
8912 node = isl_schedule_node_root(node);
8913 node = isl_schedule_node_child(node, 0);
8914 expansion1 = isl_schedule_node_get_subtree_expansion(node);
8915 isl_schedule_node_free(node);
8917 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
8918 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
8919 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
8921 expansion2 = isl_union_map_read_from_str(ctx, str);
8923 equal = isl_union_map_is_equal(umap1, umap2);
8924 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
8926 isl_union_map_free(umap1);
8927 isl_union_map_free(umap2);
8928 isl_union_map_free(expansion1);
8929 isl_union_map_free(expansion2);
8931 if (equal < 0 || equal_expansion < 0)
8932 return -1;
8933 if (!equal)
8934 isl_die(ctx, isl_error_unknown,
8935 "expressions not equal", return -1);
8936 if (!equal_expansion)
8937 isl_die(ctx, isl_error_unknown,
8938 "unexpected expansion", return -1);
8940 return 0;
8943 /* Some tests for the isl_schedule_node_group function.
8945 static int test_schedule_tree_group(isl_ctx *ctx)
8947 if (test_schedule_tree_group_1(ctx) < 0)
8948 return -1;
8949 if (test_schedule_tree_group_2(ctx) < 0)
8950 return -1;
8951 return 0;
8954 struct {
8955 const char *set;
8956 const char *dual;
8957 } coef_tests[] = {
8958 { "{ rat: [i] : 0 <= i <= 10 }",
8959 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
8960 { "{ rat: [i] : FALSE }",
8961 "{ rat: coefficients[[cst] -> [a]] }" },
8962 { "{ rat: [i] : }",
8963 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
8966 struct {
8967 const char *set;
8968 const char *dual;
8969 } sol_tests[] = {
8970 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
8971 "{ rat: [i] : 0 <= i <= 10 }" },
8972 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
8973 "{ rat: [i] }" },
8974 { "{ rat: coefficients[[cst] -> [a]] }",
8975 "{ rat: [i] : FALSE }" },
8978 /* Test the basic functionality of isl_basic_set_coefficients and
8979 * isl_basic_set_solutions.
8981 static int test_dual(isl_ctx *ctx)
8983 int i;
8985 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
8986 int equal;
8987 isl_basic_set *bset1, *bset2;
8989 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
8990 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
8991 bset1 = isl_basic_set_coefficients(bset1);
8992 equal = isl_basic_set_is_equal(bset1, bset2);
8993 isl_basic_set_free(bset1);
8994 isl_basic_set_free(bset2);
8995 if (equal < 0)
8996 return -1;
8997 if (!equal)
8998 isl_die(ctx, isl_error_unknown,
8999 "incorrect dual", return -1);
9002 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
9003 int equal;
9004 isl_basic_set *bset1, *bset2;
9006 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
9007 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
9008 bset1 = isl_basic_set_solutions(bset1);
9009 equal = isl_basic_set_is_equal(bset1, bset2);
9010 isl_basic_set_free(bset1);
9011 isl_basic_set_free(bset2);
9012 if (equal < 0)
9013 return -1;
9014 if (!equal)
9015 isl_die(ctx, isl_error_unknown,
9016 "incorrect dual", return -1);
9019 return 0;
9022 struct {
9023 int scale_tile;
9024 int shift_point;
9025 const char *domain;
9026 const char *schedule;
9027 const char *sizes;
9028 const char *tile;
9029 const char *point;
9030 } tile_tests[] = {
9031 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
9032 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9033 "{ [32,32] }",
9034 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
9035 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9037 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
9038 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9039 "{ [32,32] }",
9040 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9041 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9043 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9044 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9045 "{ [32,32] }",
9046 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
9047 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9049 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9050 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9051 "{ [32,32] }",
9052 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9053 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9057 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
9058 * tile the band and then check if the tile and point bands have the
9059 * expected partial schedule.
9061 static int test_tile(isl_ctx *ctx)
9063 int i;
9064 int scale;
9065 int shift;
9067 scale = isl_options_get_tile_scale_tile_loops(ctx);
9068 shift = isl_options_get_tile_shift_point_loops(ctx);
9070 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
9071 int opt;
9072 int equal;
9073 const char *str;
9074 isl_union_set *domain;
9075 isl_multi_union_pw_aff *mupa, *mupa2;
9076 isl_schedule_node *node;
9077 isl_multi_val *sizes;
9079 opt = tile_tests[i].scale_tile;
9080 isl_options_set_tile_scale_tile_loops(ctx, opt);
9081 opt = tile_tests[i].shift_point;
9082 isl_options_set_tile_shift_point_loops(ctx, opt);
9084 str = tile_tests[i].domain;
9085 domain = isl_union_set_read_from_str(ctx, str);
9086 node = isl_schedule_node_from_domain(domain);
9087 node = isl_schedule_node_child(node, 0);
9088 str = tile_tests[i].schedule;
9089 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9090 node = isl_schedule_node_insert_partial_schedule(node, mupa);
9091 str = tile_tests[i].sizes;
9092 sizes = isl_multi_val_read_from_str(ctx, str);
9093 node = isl_schedule_node_band_tile(node, sizes);
9095 str = tile_tests[i].tile;
9096 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9097 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9098 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
9099 isl_multi_union_pw_aff_free(mupa);
9100 isl_multi_union_pw_aff_free(mupa2);
9102 node = isl_schedule_node_child(node, 0);
9104 str = tile_tests[i].point;
9105 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9106 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9107 if (equal >= 0 && equal)
9108 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
9109 mupa2);
9110 isl_multi_union_pw_aff_free(mupa);
9111 isl_multi_union_pw_aff_free(mupa2);
9113 isl_schedule_node_free(node);
9115 if (equal < 0)
9116 return -1;
9117 if (!equal)
9118 isl_die(ctx, isl_error_unknown,
9119 "unexpected result", return -1);
9122 isl_options_set_tile_scale_tile_loops(ctx, scale);
9123 isl_options_set_tile_shift_point_loops(ctx, shift);
9125 return 0;
9128 /* Check that the domain hash of a space is equal to the hash
9129 * of the domain of the space.
9131 static int test_domain_hash(isl_ctx *ctx)
9133 isl_map *map;
9134 isl_space *space;
9135 uint32_t hash1, hash2;
9137 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
9138 space = isl_map_get_space(map);
9139 isl_map_free(map);
9140 hash1 = isl_space_get_domain_hash(space);
9141 space = isl_space_domain(space);
9142 hash2 = isl_space_get_hash(space);
9143 isl_space_free(space);
9145 if (!space)
9146 return -1;
9147 if (hash1 != hash2)
9148 isl_die(ctx, isl_error_unknown,
9149 "domain hash not equal to hash of domain", return -1);
9151 return 0;
9154 /* Check that a universe basic set that is not obviously equal to the universe
9155 * is still recognized as being equal to the universe.
9157 static int test_universe(isl_ctx *ctx)
9159 const char *s;
9160 isl_basic_set *bset;
9161 isl_bool is_univ;
9163 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
9164 bset = isl_basic_set_read_from_str(ctx, s);
9165 is_univ = isl_basic_set_is_universe(bset);
9166 isl_basic_set_free(bset);
9168 if (is_univ < 0)
9169 return -1;
9170 if (!is_univ)
9171 isl_die(ctx, isl_error_unknown,
9172 "not recognized as universe set", return -1);
9174 return 0;
9177 /* Sets for which chambers are computed and checked.
9179 const char *chambers_tests[] = {
9180 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
9181 "z >= 0 and z <= C - y and z <= B - x - y }",
9184 /* Add the domain of "cell" to "cells".
9186 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
9188 isl_basic_set_list **cells = user;
9189 isl_basic_set *dom;
9191 dom = isl_cell_get_domain(cell);
9192 isl_cell_free(cell);
9193 *cells = isl_basic_set_list_add(*cells, dom);
9195 return *cells ? isl_stat_ok : isl_stat_error;
9198 /* Check that the elements of "list" are pairwise disjoint.
9200 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
9202 int i, j;
9203 isl_size n;
9205 n = isl_basic_set_list_n_basic_set(list);
9206 if (n < 0)
9207 return isl_stat_error;
9209 for (i = 0; i < n; ++i) {
9210 isl_basic_set *bset_i;
9212 bset_i = isl_basic_set_list_get_basic_set(list, i);
9213 for (j = i + 1; j < n; ++j) {
9214 isl_basic_set *bset_j;
9215 isl_bool disjoint;
9217 bset_j = isl_basic_set_list_get_basic_set(list, j);
9218 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
9219 isl_basic_set_free(bset_j);
9220 if (!disjoint)
9221 isl_die(isl_basic_set_list_get_ctx(list),
9222 isl_error_unknown, "not disjoint",
9223 break);
9224 if (disjoint < 0 || !disjoint)
9225 break;
9227 isl_basic_set_free(bset_i);
9228 if (j < n)
9229 return isl_stat_error;
9232 return isl_stat_ok;
9235 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
9236 * are pairwise disjoint.
9238 static int test_chambers(isl_ctx *ctx)
9240 int i;
9242 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
9243 isl_basic_set *bset;
9244 isl_vertices *vertices;
9245 isl_basic_set_list *cells;
9246 isl_stat ok;
9248 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
9249 vertices = isl_basic_set_compute_vertices(bset);
9250 cells = isl_basic_set_list_alloc(ctx, 0);
9251 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
9252 &cells) < 0)
9253 cells = isl_basic_set_list_free(cells);
9254 ok = check_pairwise_disjoint(cells);
9255 isl_basic_set_list_free(cells);
9256 isl_vertices_free(vertices);
9257 isl_basic_set_free(bset);
9259 if (ok < 0)
9260 return -1;
9263 return 0;
9266 struct {
9267 const char *name;
9268 int (*fn)(isl_ctx *ctx);
9269 } tests [] = {
9270 { "universe", &test_universe },
9271 { "domain hash", &test_domain_hash },
9272 { "dual", &test_dual },
9273 { "dependence analysis", &test_flow },
9274 { "val", &test_val },
9275 { "compute divs", &test_compute_divs },
9276 { "partial lexmin", &test_partial_lexmin },
9277 { "simplify", &test_simplify },
9278 { "curry", &test_curry },
9279 { "piecewise multi affine expressions", &test_pw_multi_aff },
9280 { "multi piecewise affine expressions", &test_multi_pw_aff },
9281 { "conversion", &test_conversion },
9282 { "list", &test_list },
9283 { "align parameters", &test_align_parameters },
9284 { "drop unused parameters", &test_drop_unused_parameters },
9285 { "preimage", &test_preimage },
9286 { "pullback", &test_pullback },
9287 { "AST", &test_ast },
9288 { "AST build", &test_ast_build },
9289 { "AST generation", &test_ast_gen },
9290 { "eliminate", &test_eliminate },
9291 { "residue class", &test_residue_class },
9292 { "div", &test_div },
9293 { "slice", &test_slice },
9294 { "fixed power", &test_fixed_power },
9295 { "sample", &test_sample },
9296 { "output", &test_output },
9297 { "vertices", &test_vertices },
9298 { "chambers", &test_chambers },
9299 { "fixed", &test_fixed },
9300 { "equal", &test_equal },
9301 { "disjoint", &test_disjoint },
9302 { "product", &test_product },
9303 { "dim_max", &test_dim_max },
9304 { "affine", &test_aff },
9305 { "injective", &test_injective },
9306 { "schedule (whole component)", &test_schedule_whole },
9307 { "schedule (incremental)", &test_schedule_incremental },
9308 { "schedule tree", &test_schedule_tree },
9309 { "schedule tree prefix", &test_schedule_tree_prefix },
9310 { "schedule tree grouping", &test_schedule_tree_group },
9311 { "tile", &test_tile },
9312 { "union_pw", &test_union_pw },
9313 { "locus", &test_locus },
9314 { "eval", &test_eval },
9315 { "parse", &test_parse },
9316 { "single-valued", &test_sv },
9317 { "affine hull", &test_affine_hull },
9318 { "simple_hull", &test_simple_hull },
9319 { "coalesce", &test_coalesce },
9320 { "factorize", &test_factorize },
9321 { "subset", &test_subset },
9322 { "subtract", &test_subtract },
9323 { "intersect", &test_intersect },
9324 { "lexmin", &test_lexmin },
9325 { "min", &test_min },
9326 { "gist", &test_gist },
9327 { "piecewise quasi-polynomials", &test_pwqp },
9328 { "lift", &test_lift },
9329 { "bound", &test_bound },
9330 { "get lists", &test_get_list },
9331 { "union", &test_union },
9332 { "split periods", &test_split_periods },
9333 { "lexicographic order", &test_lex },
9334 { "bijectivity", &test_bijective },
9335 { "dataflow analysis", &test_dep },
9336 { "reading", &test_read },
9337 { "bounded", &test_bounded },
9338 { "construction", &test_construction },
9339 { "dimension manipulation", &test_dim },
9340 { "map application", &test_application },
9341 { "convex hull", &test_convex_hull },
9342 { "transitive closure", &test_closure },
9343 { "isl_bool", &test_isl_bool},
9346 int main(int argc, char **argv)
9348 int i;
9349 struct isl_ctx *ctx;
9350 struct isl_options *options;
9352 options = isl_options_new_with_defaults();
9353 assert(options);
9354 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
9356 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
9357 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
9358 printf("%s\n", tests[i].name);
9359 if (tests[i].fn(ctx) < 0)
9360 goto error;
9362 isl_ctx_free(ctx);
9363 return 0;
9364 error:
9365 isl_ctx_free(ctx);
9366 return -1;