add isl_space_check_equal_tuples
[isl.git] / isl_test.c
blobfc3380c001d58888ebb53147113dc7086a7e0605
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 #undef BASE
124 #define BASE multi_pw_aff
126 #include "check_reparse_templ.c"
128 #undef BASE
129 #define BASE pw_multi_aff
131 #include "check_reparse_templ.c"
133 /* Test parsing of piecewise multi affine expressions by printing
134 * the expressions and checking that parsing the output results
135 * in the same expression.
136 * Do this for an expression converted from a map with an output
137 * dimension name that is equal to an automatically generated name.
139 static isl_stat test_parse_pma(isl_ctx *ctx)
141 isl_map *map;
142 isl_pw_multi_aff *pma;
144 map = isl_map_read_from_str(ctx, "{ [a, a] -> [i1 = a + 1] }");
145 pma = isl_pw_multi_aff_from_map(map);
146 if (check_reparse_pw_multi_aff(ctx, pma) < 0)
147 return isl_stat_error;
149 return isl_stat_ok;
152 /* String descriptions of multi piecewise affine expressions
153 * that are used for testing printing and parsing.
155 const char *parse_multi_mpa_tests[] = {
156 "{ A[x, y] -> [] : x + y >= 0 }",
157 "{ A[x, y] -> B[] : x + y >= 0 }",
158 "{ A[x, y] -> [x] : x + y >= 0 }",
159 "[N] -> { A[x, y] -> [x] : x + y <= N }",
160 "{ A[x, y] -> [x, y] : x + y >= 0 }",
161 "{ A[x, y] -> [(x : x >= 0), (y : y >= 0)] : x + y >= 0 }",
162 "[N] -> { [] : N >= 0 }",
163 "[N] -> { [] : N >= 0 }",
164 "[N] -> { [N] : N >= 0 }",
165 "[N] -> { [N, N + 1] : N >= 0 }",
166 "[N, M] -> { [(N : N >= 0), (M : M >= 0)] : N + M >= 0 }",
169 /* Test parsing of multi piecewise affine expressions by printing
170 * the expressions and checking that parsing the output results
171 * in the same expression.
172 * Do this for a couple of manually constructed expressions,
173 * an expression converted from a map with an output dimension name
174 * that is equal to an automatically generated name, and
175 * a set of expressions parsed from strings.
177 static int test_parse_mpa(isl_ctx *ctx)
179 int i;
180 isl_space *space;
181 isl_set *dom;
182 isl_map *map;
183 isl_pw_multi_aff *pma;
184 isl_multi_pw_aff *mpa;
185 isl_stat r;
187 space = isl_space_set_alloc(ctx, 0, 0);
188 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
189 mpa = isl_multi_pw_aff_zero(space);
190 r = check_reparse_multi_pw_aff(ctx, mpa);
191 if (r < 0)
192 return -1;
194 space = isl_space_set_alloc(ctx, 1, 0);
195 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
196 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
197 dom = isl_set_universe(isl_space_params(isl_space_copy(space)));
198 dom = isl_set_lower_bound_si(dom, isl_dim_param, 0, 5);
199 mpa = isl_multi_pw_aff_zero(space);
200 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
201 r = check_reparse_multi_pw_aff(ctx, mpa);
202 if (r < 0)
203 return -1;
205 map = isl_map_read_from_str(ctx, "{ [a, a] -> [i1 = a + 1] }");
206 pma = isl_pw_multi_aff_from_map(map);
207 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
208 if (check_reparse_multi_pw_aff(ctx, mpa) < 0)
209 return -1;
211 for (i = 0; i < ARRAY_SIZE(parse_multi_mpa_tests); ++i) {
212 const char *str;
214 str = parse_multi_mpa_tests[i];
215 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
216 r = check_reparse_multi_pw_aff(ctx, mpa);
217 if (r < 0)
218 return -1;
221 return 0;
225 #undef BASE
226 #define BASE multi_union_pw_aff
228 #include "check_reparse_templ.c"
230 /* String descriptions of multi union piecewise affine expressions
231 * that are used for testing printing and parsing.
233 const char *parse_multi_mupa_tests[] = {
234 "[]",
235 "A[]",
236 "A[B[] -> C[]]",
237 "(A[] : { S[x] : x > 0; T[y] : y >= 0 })",
238 "(A[] : { })",
239 "[N] -> (A[] : { })",
240 "[N] -> (A[] : { : N >= 0 })",
241 "[N] -> (A[] : { S[x] : x > N; T[y] : y >= 0 })",
242 "(A[] : [N] -> { S[x] : x > N; T[y] : y >= 0 })",
243 "A[{ S[x] -> [x + 1]; T[x] -> [x] }]",
244 "(A[{ S[x] -> [x + 1]; T[x] -> [x] }] : "
245 "{ S[x] : x > 0; T[y] : y >= 0 })",
248 /* Test parsing of multi union piecewise affine expressions by printing
249 * the expressions and checking that parsing the output results
250 * in the same expression.
251 * Do this for a couple of manually constructed expressions and
252 * a set of expressions parsed from strings.
254 static int test_parse_mupa(isl_ctx *ctx)
256 int i;
257 isl_space *space;
258 isl_multi_union_pw_aff *mupa;
259 isl_set *dom;
260 isl_union_set *uset;
261 isl_stat r;
263 space = isl_space_set_alloc(ctx, 0, 0);
264 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
265 mupa = isl_multi_union_pw_aff_zero(space);
266 r = check_reparse_multi_union_pw_aff(ctx, mupa);
267 if (r < 0)
268 return -1;
270 space = isl_space_set_alloc(ctx, 1, 0);
271 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
272 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
273 dom = isl_set_universe(space);
274 dom = isl_set_lower_bound_si(dom, isl_dim_param, 0, 5);
275 uset = isl_union_set_from_set(dom);
276 space = isl_space_set_alloc(ctx, 1, 0);
277 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
278 space = isl_space_set_tuple_name(space, isl_dim_set, "B");
279 mupa = isl_multi_union_pw_aff_zero(space);
280 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, uset);
281 r = check_reparse_multi_union_pw_aff(ctx, mupa);
282 if (r < 0)
283 return -1;
285 for (i = 0; i < ARRAY_SIZE(parse_multi_mupa_tests); ++i) {
286 const char *str;
288 str = parse_multi_mupa_tests[i];
289 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
290 r = check_reparse_multi_union_pw_aff(ctx, mupa);
291 if (r < 0)
292 return -1;
295 return 0;
298 /* Test parsing of multi expressions.
300 static int test_parse_multi(isl_ctx *ctx)
302 if (test_parse_mpa(ctx) < 0)
303 return -1;
304 if (test_parse_mupa(ctx) < 0)
305 return -1;
307 return 0;
310 /* Pairs of binary relation representations that should represent
311 * the same binary relations.
313 struct {
314 const char *map1;
315 const char *map2;
316 } parse_map_equal_tests[] = {
317 { "{ [x,y] : [([x/2]+y)/3] >= 1 }",
318 "{ [x, y] : 2y >= 6 - x }" },
319 { "{ [x,y] : x <= min(y, 2*y+3) }",
320 "{ [x,y] : x <= y, 2*y + 3 }" },
321 { "{ [x,y] : x >= min(y, 2*y+3) }",
322 "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }" },
323 { "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }",
324 "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }" },
325 { "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }",
326 "{ [i,j] -> [min(i,j)] }" },
327 { "{ [i,j] : i != j }",
328 "{ [i,j] : i < j or i > j }" },
329 { "{ [i,j] : (i+1)*2 >= j }",
330 "{ [i, j] : j <= 2 + 2i }" },
331 { "{ [i] -> [i > 0 ? 4 : 5] }",
332 "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }" },
333 { "[N=2,M] -> { [i=[(M+N)/4]] }",
334 "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }" },
335 { "{ [x] : x >= 0 }",
336 "{ [x] : x-0 >= 0 }" },
337 { "{ [i] : ((i > 10)) }",
338 "{ [i] : i >= 11 }" },
339 { "{ [i] -> [0] }",
340 "{ [i] -> [0 * i] }" },
341 { "{ [a] -> [b] : (not false) }",
342 "{ [a] -> [b] : true }" },
343 { "{ [i] : i/2 <= 5 }",
344 "{ [i] : i <= 10 }" },
345 { "{Sym=[n] [i] : i <= n }",
346 "[n] -> { [i] : i <= n }" },
347 { "{ [*] }",
348 "{ [a] }" },
349 { "{ [i] : 2*floor(i/2) = i }",
350 "{ [i] : exists a : i = 2 a }" },
351 { "{ [a] -> [b] : a = 5 implies b = 5 }",
352 "{ [a] -> [b] : a != 5 or b = 5 }" },
353 { "{ [a] -> [a - 1 : a > 0] }",
354 "{ [a] -> [a - 1] : a > 0 }" },
355 { "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
356 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }" },
357 { "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
358 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
359 { "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
360 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
361 { "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
362 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
363 { "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
364 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
365 { "{ [a,b] -> [i,j] : a,b << i,j }",
366 "{ [a,b] -> [i,j] : a < i or (a = i and b < j) }" },
367 { "{ [a,b] -> [i,j] : a,b <<= i,j }",
368 "{ [a,b] -> [i,j] : a < i or (a = i and b <= j) }" },
369 { "{ [a,b] -> [i,j] : a,b >> i,j }",
370 "{ [a,b] -> [i,j] : a > i or (a = i and b > j) }" },
371 { "{ [a,b] -> [i,j] : a,b >>= i,j }",
372 "{ [a,b] -> [i,j] : a > i or (a = i and b >= j) }" },
373 { "{ [n] -> [i] : exists (a, b, c: 8b <= i - 32a and "
374 "8b >= -7 + i - 32 a and b >= 0 and b <= 3 and "
375 "8c < n - 32a and i < n and c >= 0 and "
376 "c <= 3 and c >= -4a) }",
377 "{ [n] -> [i] : 0 <= i < n }" },
378 { "{ [x] -> [] : exists (a, b: 0 <= a <= 1 and 0 <= b <= 3 and "
379 "2b <= x - 8a and 2b >= -1 + x - 8a) }",
380 "{ [x] -> [] : 0 <= x <= 15 }" },
381 { "{ [x] -> [x] : }",
382 "{ [x] -> [x] }" },
385 int test_parse(struct isl_ctx *ctx)
387 int i;
388 isl_map *map, *map2;
389 const char *str, *str2;
391 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
392 return -1;
393 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
394 return -1;
395 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
396 return -1;
397 if (test_parse_multi(ctx) < 0)
398 return -1;
399 if (test_parse_pma(ctx) < 0)
400 return -1;
402 str = "{ [i] -> [-i] }";
403 map = isl_map_read_from_str(ctx, str);
404 assert(map);
405 isl_map_free(map);
407 str = "{ A[i] -> L[([i/3])] }";
408 map = isl_map_read_from_str(ctx, str);
409 assert(map);
410 isl_map_free(map);
412 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
413 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
414 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
416 for (i = 0; i < ARRAY_SIZE(parse_map_equal_tests); ++i) {
417 str = parse_map_equal_tests[i].map1;
418 str2 = parse_map_equal_tests[i].map2;
419 if (test_parse_map_equal(ctx, str, str2) < 0)
420 return -1;
423 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
424 map = isl_map_read_from_str(ctx, str);
425 str = "{ [new, old] -> [o0, o1] : "
426 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
427 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
428 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
429 map2 = isl_map_read_from_str(ctx, str);
430 assert(isl_map_is_equal(map, map2));
431 isl_map_free(map);
432 isl_map_free(map2);
434 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
435 map = isl_map_read_from_str(ctx, str);
436 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
437 map2 = isl_map_read_from_str(ctx, str);
438 assert(isl_map_is_equal(map, map2));
439 isl_map_free(map);
440 isl_map_free(map2);
442 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
443 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
444 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
445 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
446 test_parse_pwaff(ctx, "{ [] -> [(100)] }");
448 return 0;
451 static int test_read(isl_ctx *ctx)
453 char *filename;
454 FILE *input;
455 isl_basic_set *bset1, *bset2;
456 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
457 int equal;
459 filename = get_filename(ctx, "set", "omega");
460 assert(filename);
461 input = fopen(filename, "r");
462 assert(input);
464 bset1 = isl_basic_set_read_from_file(ctx, input);
465 bset2 = isl_basic_set_read_from_str(ctx, str);
467 equal = isl_basic_set_is_equal(bset1, bset2);
469 isl_basic_set_free(bset1);
470 isl_basic_set_free(bset2);
471 free(filename);
473 fclose(input);
475 if (equal < 0)
476 return -1;
477 if (!equal)
478 isl_die(ctx, isl_error_unknown,
479 "read sets not equal", return -1);
481 return 0;
484 static int test_bounded(isl_ctx *ctx)
486 isl_set *set;
487 isl_bool bounded;
489 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
490 bounded = isl_set_is_bounded(set);
491 isl_set_free(set);
493 if (bounded < 0)
494 return -1;
495 if (!bounded)
496 isl_die(ctx, isl_error_unknown,
497 "set not considered bounded", return -1);
499 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
500 bounded = isl_set_is_bounded(set);
501 assert(!bounded);
502 isl_set_free(set);
504 if (bounded < 0)
505 return -1;
506 if (bounded)
507 isl_die(ctx, isl_error_unknown,
508 "set considered bounded", return -1);
510 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
511 bounded = isl_set_is_bounded(set);
512 isl_set_free(set);
514 if (bounded < 0)
515 return -1;
516 if (bounded)
517 isl_die(ctx, isl_error_unknown,
518 "set considered bounded", return -1);
520 return 0;
523 /* Construct the basic set { [i] : 5 <= i <= N } */
524 static int test_construction_1(isl_ctx *ctx)
526 isl_space *dim;
527 isl_local_space *ls;
528 isl_basic_set *bset;
529 isl_constraint *c;
531 dim = isl_space_set_alloc(ctx, 1, 1);
532 bset = isl_basic_set_universe(isl_space_copy(dim));
533 ls = isl_local_space_from_space(dim);
535 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
536 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
537 c = isl_constraint_set_coefficient_si(c, isl_dim_param, 0, 1);
538 bset = isl_basic_set_add_constraint(bset, c);
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_constant_si(c, -5);
543 bset = isl_basic_set_add_constraint(bset, c);
545 isl_local_space_free(ls);
546 isl_basic_set_free(bset);
548 return 0;
551 /* Construct the basic set { [x] : -100 <= x <= 100 }
552 * using isl_basic_set_{lower,upper}_bound_val and
553 * check that it is equal the same basic set parsed from a string.
555 static int test_construction_2(isl_ctx *ctx)
557 isl_bool equal;
558 isl_val *v;
559 isl_space *space;
560 isl_basic_set *bset1, *bset2;
562 v = isl_val_int_from_si(ctx, 100);
563 space = isl_space_set_alloc(ctx, 0, 1);
564 bset1 = isl_basic_set_universe(space);
565 bset1 = isl_basic_set_upper_bound_val(bset1, isl_dim_set, 0,
566 isl_val_copy(v));
567 bset1 = isl_basic_set_lower_bound_val(bset1, isl_dim_set, 0,
568 isl_val_neg(v));
569 bset2 = isl_basic_set_read_from_str(ctx, "{ [x] : -100 <= x <= 100 }");
570 equal = isl_basic_set_is_equal(bset1, bset2);
571 isl_basic_set_free(bset1);
572 isl_basic_set_free(bset2);
574 if (equal < 0)
575 return -1;
576 if (!equal)
577 isl_die(ctx, isl_error_unknown,
578 "failed construction", return -1);
580 return 0;
583 /* Basic tests for constructing basic sets.
585 static int test_construction(isl_ctx *ctx)
587 if (test_construction_1(ctx) < 0)
588 return -1;
589 if (test_construction_2(ctx) < 0)
590 return -1;
591 return 0;
594 static int test_dim(isl_ctx *ctx)
596 const char *str;
597 isl_map *map1, *map2;
598 int equal;
600 map1 = isl_map_read_from_str(ctx,
601 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
602 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
603 map2 = isl_map_read_from_str(ctx,
604 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
605 equal = isl_map_is_equal(map1, map2);
606 isl_map_free(map2);
608 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
609 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
610 if (equal >= 0 && equal)
611 equal = isl_map_is_equal(map1, map2);
613 isl_map_free(map1);
614 isl_map_free(map2);
616 if (equal < 0)
617 return -1;
618 if (!equal)
619 isl_die(ctx, isl_error_unknown,
620 "unexpected result", return -1);
622 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
623 map1 = isl_map_read_from_str(ctx, str);
624 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
625 map2 = isl_map_read_from_str(ctx, str);
626 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
627 equal = isl_map_is_equal(map1, map2);
628 isl_map_free(map1);
629 isl_map_free(map2);
631 if (equal < 0)
632 return -1;
633 if (!equal)
634 isl_die(ctx, isl_error_unknown,
635 "unexpected result", return -1);
637 return 0;
640 /* Check that "val" is equal to the value described by "str".
641 * If "str" is "NaN", then check for a NaN value explicitly.
643 static isl_stat val_check_equal(__isl_keep isl_val *val, const char *str)
645 isl_bool ok, is_nan;
646 isl_ctx *ctx;
647 isl_val *res;
649 if (!val)
650 return isl_stat_error;
652 ctx = isl_val_get_ctx(val);
653 res = isl_val_read_from_str(ctx, str);
654 is_nan = isl_val_is_nan(res);
655 if (is_nan < 0)
656 ok = isl_bool_error;
657 else if (is_nan)
658 ok = isl_val_is_nan(val);
659 else
660 ok = isl_val_eq(val, res);
661 isl_val_free(res);
662 if (ok < 0)
663 return isl_stat_error;
664 if (!ok)
665 isl_die(ctx, isl_error_unknown,
666 "unexpected result", return isl_stat_error);
667 return isl_stat_ok;
670 struct {
671 __isl_give isl_val *(*op)(__isl_take isl_val *v);
672 const char *arg;
673 const char *res;
674 } val_un_tests[] = {
675 { &isl_val_neg, "0", "0" },
676 { &isl_val_abs, "0", "0" },
677 { &isl_val_pow2, "0", "1" },
678 { &isl_val_floor, "0", "0" },
679 { &isl_val_ceil, "0", "0" },
680 { &isl_val_neg, "1", "-1" },
681 { &isl_val_neg, "-1", "1" },
682 { &isl_val_neg, "1/2", "-1/2" },
683 { &isl_val_neg, "-1/2", "1/2" },
684 { &isl_val_neg, "infty", "-infty" },
685 { &isl_val_neg, "-infty", "infty" },
686 { &isl_val_neg, "NaN", "NaN" },
687 { &isl_val_abs, "1", "1" },
688 { &isl_val_abs, "-1", "1" },
689 { &isl_val_abs, "1/2", "1/2" },
690 { &isl_val_abs, "-1/2", "1/2" },
691 { &isl_val_abs, "infty", "infty" },
692 { &isl_val_abs, "-infty", "infty" },
693 { &isl_val_abs, "NaN", "NaN" },
694 { &isl_val_floor, "1", "1" },
695 { &isl_val_floor, "-1", "-1" },
696 { &isl_val_floor, "1/2", "0" },
697 { &isl_val_floor, "-1/2", "-1" },
698 { &isl_val_floor, "infty", "infty" },
699 { &isl_val_floor, "-infty", "-infty" },
700 { &isl_val_floor, "NaN", "NaN" },
701 { &isl_val_ceil, "1", "1" },
702 { &isl_val_ceil, "-1", "-1" },
703 { &isl_val_ceil, "1/2", "1" },
704 { &isl_val_ceil, "-1/2", "0" },
705 { &isl_val_ceil, "infty", "infty" },
706 { &isl_val_ceil, "-infty", "-infty" },
707 { &isl_val_ceil, "NaN", "NaN" },
708 { &isl_val_pow2, "-3", "1/8" },
709 { &isl_val_pow2, "-1", "1/2" },
710 { &isl_val_pow2, "1", "2" },
711 { &isl_val_pow2, "2", "4" },
712 { &isl_val_pow2, "3", "8" },
713 { &isl_val_inv, "1", "1" },
714 { &isl_val_inv, "2", "1/2" },
715 { &isl_val_inv, "1/2", "2" },
716 { &isl_val_inv, "-2", "-1/2" },
717 { &isl_val_inv, "-1/2", "-2" },
718 { &isl_val_inv, "0", "NaN" },
719 { &isl_val_inv, "NaN", "NaN" },
720 { &isl_val_inv, "infty", "0" },
721 { &isl_val_inv, "-infty", "0" },
724 /* Perform some basic tests of unary operations on isl_val objects.
726 static int test_un_val(isl_ctx *ctx)
728 int i;
729 isl_val *v;
730 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
732 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
733 isl_stat r;
735 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
736 fn = val_un_tests[i].op;
737 v = fn(v);
738 r = val_check_equal(v, val_un_tests[i].res);
739 isl_val_free(v);
740 if (r < 0)
741 return -1;
744 return 0;
747 struct {
748 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
749 __isl_take isl_val *v2);
750 } val_bin_op[] = {
751 ['+'] = { &isl_val_add },
752 ['-'] = { &isl_val_sub },
753 ['*'] = { &isl_val_mul },
754 ['/'] = { &isl_val_div },
755 ['g'] = { &isl_val_gcd },
756 ['m'] = { &isl_val_min },
757 ['M'] = { &isl_val_max },
760 struct {
761 const char *arg1;
762 unsigned char op;
763 const char *arg2;
764 const char *res;
765 } val_bin_tests[] = {
766 { "0", '+', "0", "0" },
767 { "1", '+', "0", "1" },
768 { "1", '+', "1", "2" },
769 { "1", '-', "1", "0" },
770 { "1", '*', "1", "1" },
771 { "1", '/', "1", "1" },
772 { "2", '*', "3", "6" },
773 { "2", '*', "1/2", "1" },
774 { "2", '*', "1/3", "2/3" },
775 { "2/3", '*', "3/5", "2/5" },
776 { "2/3", '*', "7/5", "14/15" },
777 { "2", '/', "1/2", "4" },
778 { "-2", '/', "-1/2", "4" },
779 { "-2", '/', "1/2", "-4" },
780 { "2", '/', "-1/2", "-4" },
781 { "2", '/', "2", "1" },
782 { "2", '/', "3", "2/3" },
783 { "2/3", '/', "5/3", "2/5" },
784 { "2/3", '/', "5/7", "14/15" },
785 { "0", '/', "0", "NaN" },
786 { "42", '/', "0", "NaN" },
787 { "-42", '/', "0", "NaN" },
788 { "infty", '/', "0", "NaN" },
789 { "-infty", '/', "0", "NaN" },
790 { "NaN", '/', "0", "NaN" },
791 { "0", '/', "NaN", "NaN" },
792 { "42", '/', "NaN", "NaN" },
793 { "-42", '/', "NaN", "NaN" },
794 { "infty", '/', "NaN", "NaN" },
795 { "-infty", '/', "NaN", "NaN" },
796 { "NaN", '/', "NaN", "NaN" },
797 { "0", '/', "infty", "0" },
798 { "42", '/', "infty", "0" },
799 { "-42", '/', "infty", "0" },
800 { "infty", '/', "infty", "NaN" },
801 { "-infty", '/', "infty", "NaN" },
802 { "NaN", '/', "infty", "NaN" },
803 { "0", '/', "-infty", "0" },
804 { "42", '/', "-infty", "0" },
805 { "-42", '/', "-infty", "0" },
806 { "infty", '/', "-infty", "NaN" },
807 { "-infty", '/', "-infty", "NaN" },
808 { "NaN", '/', "-infty", "NaN" },
809 { "1", '-', "1/3", "2/3" },
810 { "1/3", '+', "1/2", "5/6" },
811 { "1/2", '+', "1/2", "1" },
812 { "3/4", '-', "1/4", "1/2" },
813 { "1/2", '-', "1/3", "1/6" },
814 { "infty", '+', "42", "infty" },
815 { "infty", '+', "infty", "infty" },
816 { "42", '+', "infty", "infty" },
817 { "infty", '-', "infty", "NaN" },
818 { "infty", '*', "infty", "infty" },
819 { "infty", '*', "-infty", "-infty" },
820 { "-infty", '*', "infty", "-infty" },
821 { "-infty", '*', "-infty", "infty" },
822 { "0", '*', "infty", "NaN" },
823 { "1", '*', "infty", "infty" },
824 { "infty", '*', "0", "NaN" },
825 { "infty", '*', "42", "infty" },
826 { "42", '-', "infty", "-infty" },
827 { "infty", '+', "-infty", "NaN" },
828 { "4", 'g', "6", "2" },
829 { "5", 'g', "6", "1" },
830 { "42", 'm', "3", "3" },
831 { "42", 'M', "3", "42" },
832 { "3", 'm', "42", "3" },
833 { "3", 'M', "42", "42" },
834 { "42", 'm', "infty", "42" },
835 { "42", 'M', "infty", "infty" },
836 { "42", 'm', "-infty", "-infty" },
837 { "42", 'M', "-infty", "42" },
838 { "42", 'm', "NaN", "NaN" },
839 { "42", 'M', "NaN", "NaN" },
840 { "infty", 'm', "-infty", "-infty" },
841 { "infty", 'M', "-infty", "infty" },
844 /* Perform some basic tests of binary operations on isl_val objects.
846 static int test_bin_val(isl_ctx *ctx)
848 int i;
849 isl_val *v1, *v2, *res;
850 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
851 __isl_take isl_val *v2);
852 int ok;
854 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
855 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
856 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
857 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
858 fn = val_bin_op[val_bin_tests[i].op].fn;
859 v1 = fn(v1, v2);
860 if (isl_val_is_nan(res))
861 ok = isl_val_is_nan(v1);
862 else
863 ok = isl_val_eq(v1, res);
864 isl_val_free(v1);
865 isl_val_free(res);
866 if (ok < 0)
867 return -1;
868 if (!ok)
869 isl_die(ctx, isl_error_unknown,
870 "unexpected result", return -1);
873 return 0;
876 /* Perform some basic tests on isl_val objects.
878 static int test_val(isl_ctx *ctx)
880 if (test_un_val(ctx) < 0)
881 return -1;
882 if (test_bin_val(ctx) < 0)
883 return -1;
884 return 0;
887 /* Sets described using existentially quantified variables that
888 * can also be described without.
890 static const char *elimination_tests[] = {
891 "{ [i,j] : 2 * [i/2] + 3 * [j/4] <= 10 and 2 i = j }",
892 "{ [m, w] : exists a : w - 2m - 5 <= 3a <= m - 2w }",
893 "{ [m, w] : exists a : w >= 0 and a < m and -1 + w <= a <= 2m - w }",
896 /* Check that redundant existentially quantified variables are
897 * getting removed.
899 static int test_elimination(isl_ctx *ctx)
901 int i;
902 isl_size n;
903 isl_basic_set *bset;
905 for (i = 0; i < ARRAY_SIZE(elimination_tests); ++i) {
906 bset = isl_basic_set_read_from_str(ctx, elimination_tests[i]);
907 n = isl_basic_set_dim(bset, isl_dim_div);
908 isl_basic_set_free(bset);
909 if (n < 0)
910 return -1;
911 if (n != 0)
912 isl_die(ctx, isl_error_unknown,
913 "expecting no existentials", return -1);
916 return 0;
919 static int test_div(isl_ctx *ctx)
921 const char *str;
922 int empty;
923 isl_space *dim;
924 isl_set *set;
925 isl_local_space *ls;
926 struct isl_basic_set *bset;
927 struct isl_constraint *c;
929 /* test 1 */
930 dim = isl_space_set_alloc(ctx, 0, 3);
931 bset = isl_basic_set_universe(isl_space_copy(dim));
932 ls = isl_local_space_from_space(dim);
934 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
935 c = isl_constraint_set_constant_si(c, -1);
936 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
937 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
938 bset = isl_basic_set_add_constraint(bset, c);
940 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
941 c = isl_constraint_set_constant_si(c, 1);
942 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
943 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
944 bset = isl_basic_set_add_constraint(bset, c);
946 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
948 assert(bset && bset->n_div == 1);
949 isl_local_space_free(ls);
950 isl_basic_set_free(bset);
952 /* test 2 */
953 dim = isl_space_set_alloc(ctx, 0, 3);
954 bset = isl_basic_set_universe(isl_space_copy(dim));
955 ls = isl_local_space_from_space(dim);
957 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
958 c = isl_constraint_set_constant_si(c, 1);
959 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
960 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
961 bset = isl_basic_set_add_constraint(bset, c);
963 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
964 c = isl_constraint_set_constant_si(c, -1);
965 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
966 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
967 bset = isl_basic_set_add_constraint(bset, c);
969 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
971 assert(bset && bset->n_div == 1);
972 isl_local_space_free(ls);
973 isl_basic_set_free(bset);
975 /* test 3 */
976 dim = isl_space_set_alloc(ctx, 0, 3);
977 bset = isl_basic_set_universe(isl_space_copy(dim));
978 ls = isl_local_space_from_space(dim);
980 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
981 c = isl_constraint_set_constant_si(c, 1);
982 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
983 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
984 bset = isl_basic_set_add_constraint(bset, c);
986 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
987 c = isl_constraint_set_constant_si(c, -3);
988 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
989 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 4);
990 bset = isl_basic_set_add_constraint(bset, c);
992 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
994 assert(bset && bset->n_div == 1);
995 isl_local_space_free(ls);
996 isl_basic_set_free(bset);
998 /* test 4 */
999 dim = isl_space_set_alloc(ctx, 0, 3);
1000 bset = isl_basic_set_universe(isl_space_copy(dim));
1001 ls = isl_local_space_from_space(dim);
1003 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1004 c = isl_constraint_set_constant_si(c, 2);
1005 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1006 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1007 bset = isl_basic_set_add_constraint(bset, c);
1009 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1010 c = isl_constraint_set_constant_si(c, -1);
1011 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1012 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
1013 bset = isl_basic_set_add_constraint(bset, c);
1015 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1017 assert(isl_basic_set_is_empty(bset));
1018 isl_local_space_free(ls);
1019 isl_basic_set_free(bset);
1021 /* test 5 */
1022 dim = isl_space_set_alloc(ctx, 0, 3);
1023 bset = isl_basic_set_universe(isl_space_copy(dim));
1024 ls = isl_local_space_from_space(dim);
1026 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1027 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1028 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
1029 bset = isl_basic_set_add_constraint(bset, c);
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, 1, -3);
1034 bset = isl_basic_set_add_constraint(bset, c);
1036 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1038 assert(bset && bset->n_div == 0);
1039 isl_basic_set_free(bset);
1040 isl_local_space_free(ls);
1042 /* test 6 */
1043 dim = isl_space_set_alloc(ctx, 0, 3);
1044 bset = isl_basic_set_universe(isl_space_copy(dim));
1045 ls = isl_local_space_from_space(dim);
1047 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1048 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1049 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
1050 bset = isl_basic_set_add_constraint(bset, c);
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, 1, -3);
1055 bset = isl_basic_set_add_constraint(bset, c);
1057 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1059 assert(bset && bset->n_div == 1);
1060 isl_basic_set_free(bset);
1061 isl_local_space_free(ls);
1063 /* test 7 */
1064 /* This test is a bit tricky. We set up an equality
1065 * a + 3b + 3c = 6 e0
1066 * Normalization of divs creates _two_ divs
1067 * a = 3 e0
1068 * c - b - e0 = 2 e1
1069 * Afterwards e0 is removed again because it has coefficient -1
1070 * and we end up with the original equality and div again.
1071 * Perhaps we can avoid the introduction of this temporary div.
1073 dim = isl_space_set_alloc(ctx, 0, 4);
1074 bset = isl_basic_set_universe(isl_space_copy(dim));
1075 ls = isl_local_space_from_space(dim);
1077 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1078 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1079 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1080 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -3);
1081 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 6);
1082 bset = isl_basic_set_add_constraint(bset, c);
1084 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
1086 /* Test disabled for now */
1088 assert(bset && bset->n_div == 1);
1090 isl_local_space_free(ls);
1091 isl_basic_set_free(bset);
1093 /* test 8 */
1094 dim = isl_space_set_alloc(ctx, 0, 5);
1095 bset = isl_basic_set_universe(isl_space_copy(dim));
1096 ls = isl_local_space_from_space(dim);
1098 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1099 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1100 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1101 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, -3);
1102 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 4, 6);
1103 bset = isl_basic_set_add_constraint(bset, c);
1105 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1106 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1107 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 1);
1108 c = isl_constraint_set_constant_si(c, 1);
1109 bset = isl_basic_set_add_constraint(bset, c);
1111 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
1113 /* Test disabled for now */
1115 assert(bset && bset->n_div == 1);
1117 isl_local_space_free(ls);
1118 isl_basic_set_free(bset);
1120 /* test 9 */
1121 dim = isl_space_set_alloc(ctx, 0, 4);
1122 bset = isl_basic_set_universe(isl_space_copy(dim));
1123 ls = isl_local_space_from_space(dim);
1125 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1126 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1127 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -1);
1128 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1129 bset = isl_basic_set_add_constraint(bset, c);
1131 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1132 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1133 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 3);
1134 c = isl_constraint_set_constant_si(c, 2);
1135 bset = isl_basic_set_add_constraint(bset, c);
1137 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
1139 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1141 assert(!isl_basic_set_is_empty(bset));
1143 isl_local_space_free(ls);
1144 isl_basic_set_free(bset);
1146 /* test 10 */
1147 dim = isl_space_set_alloc(ctx, 0, 3);
1148 bset = isl_basic_set_universe(isl_space_copy(dim));
1149 ls = isl_local_space_from_space(dim);
1151 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1152 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1153 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1154 bset = isl_basic_set_add_constraint(bset, c);
1156 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1158 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1160 isl_local_space_free(ls);
1161 isl_basic_set_free(bset);
1163 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1164 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
1165 set = isl_set_read_from_str(ctx, str);
1166 set = isl_set_compute_divs(set);
1167 isl_set_free(set);
1168 if (!set)
1169 return -1;
1171 if (test_elimination(ctx) < 0)
1172 return -1;
1174 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
1175 set = isl_set_read_from_str(ctx, str);
1176 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
1177 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
1178 empty = isl_set_is_empty(set);
1179 isl_set_free(set);
1180 if (empty < 0)
1181 return -1;
1182 if (!empty)
1183 isl_die(ctx, isl_error_unknown,
1184 "result not as accurate as expected", return -1);
1186 return 0;
1189 void test_application_case(struct isl_ctx *ctx, const char *name)
1191 char *filename;
1192 FILE *input;
1193 struct isl_basic_set *bset1, *bset2;
1194 struct isl_basic_map *bmap;
1196 filename = get_filename(ctx, name, "omega");
1197 assert(filename);
1198 input = fopen(filename, "r");
1199 assert(input);
1201 bset1 = isl_basic_set_read_from_file(ctx, input);
1202 bmap = isl_basic_map_read_from_file(ctx, input);
1204 bset1 = isl_basic_set_apply(bset1, bmap);
1206 bset2 = isl_basic_set_read_from_file(ctx, input);
1208 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1210 isl_basic_set_free(bset1);
1211 isl_basic_set_free(bset2);
1212 free(filename);
1214 fclose(input);
1217 static int test_application(isl_ctx *ctx)
1219 test_application_case(ctx, "application");
1220 test_application_case(ctx, "application2");
1222 return 0;
1225 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1227 char *filename;
1228 FILE *input;
1229 struct isl_basic_set *bset1, *bset2;
1231 filename = get_filename(ctx, name, "polylib");
1232 assert(filename);
1233 input = fopen(filename, "r");
1234 assert(input);
1236 bset1 = isl_basic_set_read_from_file(ctx, input);
1237 bset2 = isl_basic_set_read_from_file(ctx, input);
1239 bset1 = isl_basic_set_affine_hull(bset1);
1241 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1243 isl_basic_set_free(bset1);
1244 isl_basic_set_free(bset2);
1245 free(filename);
1247 fclose(input);
1250 int test_affine_hull(struct isl_ctx *ctx)
1252 const char *str;
1253 isl_set *set;
1254 isl_basic_set *bset, *bset2;
1255 isl_size n;
1256 isl_bool subset;
1258 test_affine_hull_case(ctx, "affine2");
1259 test_affine_hull_case(ctx, "affine");
1260 test_affine_hull_case(ctx, "affine3");
1262 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1263 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1264 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1265 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1266 set = isl_set_read_from_str(ctx, str);
1267 bset = isl_set_affine_hull(set);
1268 n = isl_basic_set_dim(bset, isl_dim_div);
1269 isl_basic_set_free(bset);
1270 if (n < 0)
1271 return -1;
1272 if (n != 0)
1273 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1274 return -1);
1276 /* Check that isl_map_affine_hull is not confused by
1277 * the reordering of divs in isl_map_align_divs.
1279 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1280 "32e0 = b and 32e1 = c); "
1281 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1282 set = isl_set_read_from_str(ctx, str);
1283 bset = isl_set_affine_hull(set);
1284 isl_basic_set_free(bset);
1285 if (!bset)
1286 return -1;
1288 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1289 "32e2 = 31 + 31e0 }";
1290 set = isl_set_read_from_str(ctx, str);
1291 bset = isl_set_affine_hull(set);
1292 str = "{ [a] : exists e : a = 32 e }";
1293 bset2 = isl_basic_set_read_from_str(ctx, str);
1294 subset = isl_basic_set_is_subset(bset, bset2);
1295 isl_basic_set_free(bset);
1296 isl_basic_set_free(bset2);
1297 if (subset < 0)
1298 return -1;
1299 if (!subset)
1300 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1301 return -1);
1303 return 0;
1306 /* Pairs of maps and the corresponding expected results of
1307 * isl_map_plain_unshifted_simple_hull.
1309 struct {
1310 const char *map;
1311 const char *hull;
1312 } plain_unshifted_simple_hull_tests[] = {
1313 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1314 "{ [i] -> [j] : i >= 1 }" },
1315 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1316 "(j mod 4 = 2 and k mod 6 = n) }",
1317 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1320 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1322 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1324 int i;
1325 isl_map *map;
1326 isl_basic_map *hull, *expected;
1327 isl_bool equal;
1329 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1330 const char *str;
1331 str = plain_unshifted_simple_hull_tests[i].map;
1332 map = isl_map_read_from_str(ctx, str);
1333 str = plain_unshifted_simple_hull_tests[i].hull;
1334 expected = isl_basic_map_read_from_str(ctx, str);
1335 hull = isl_map_plain_unshifted_simple_hull(map);
1336 equal = isl_basic_map_is_equal(hull, expected);
1337 isl_basic_map_free(hull);
1338 isl_basic_map_free(expected);
1339 if (equal < 0)
1340 return -1;
1341 if (!equal)
1342 isl_die(ctx, isl_error_unknown, "unexpected hull",
1343 return -1);
1346 return 0;
1349 /* Pairs of sets and the corresponding expected results of
1350 * isl_set_unshifted_simple_hull.
1352 struct {
1353 const char *set;
1354 const char *hull;
1355 } unshifted_simple_hull_tests[] = {
1356 { "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1357 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1360 /* Basic tests for isl_set_unshifted_simple_hull.
1362 static int test_unshifted_simple_hull(isl_ctx *ctx)
1364 int i;
1365 isl_set *set;
1366 isl_basic_set *hull, *expected;
1367 isl_bool equal;
1369 for (i = 0; i < ARRAY_SIZE(unshifted_simple_hull_tests); ++i) {
1370 const char *str;
1371 str = unshifted_simple_hull_tests[i].set;
1372 set = isl_set_read_from_str(ctx, str);
1373 str = unshifted_simple_hull_tests[i].hull;
1374 expected = isl_basic_set_read_from_str(ctx, str);
1375 hull = isl_set_unshifted_simple_hull(set);
1376 equal = isl_basic_set_is_equal(hull, expected);
1377 isl_basic_set_free(hull);
1378 isl_basic_set_free(expected);
1379 if (equal < 0)
1380 return -1;
1381 if (!equal)
1382 isl_die(ctx, isl_error_unknown, "unexpected hull",
1383 return -1);
1386 return 0;
1389 static int test_simple_hull(struct isl_ctx *ctx)
1391 const char *str;
1392 isl_set *set;
1393 isl_basic_set *bset;
1394 isl_bool is_empty;
1396 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1397 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1398 set = isl_set_read_from_str(ctx, str);
1399 bset = isl_set_simple_hull(set);
1400 is_empty = isl_basic_set_is_empty(bset);
1401 isl_basic_set_free(bset);
1403 if (is_empty == isl_bool_error)
1404 return -1;
1406 if (is_empty == isl_bool_false)
1407 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1408 return -1);
1410 if (test_plain_unshifted_simple_hull(ctx) < 0)
1411 return -1;
1412 if (test_unshifted_simple_hull(ctx) < 0)
1413 return -1;
1415 return 0;
1418 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1420 char *filename;
1421 FILE *input;
1422 struct isl_basic_set *bset1, *bset2;
1423 struct isl_set *set;
1425 filename = get_filename(ctx, name, "polylib");
1426 assert(filename);
1427 input = fopen(filename, "r");
1428 assert(input);
1430 bset1 = isl_basic_set_read_from_file(ctx, input);
1431 bset2 = isl_basic_set_read_from_file(ctx, input);
1433 set = isl_basic_set_union(bset1, bset2);
1434 bset1 = isl_set_convex_hull(set);
1436 bset2 = isl_basic_set_read_from_file(ctx, input);
1438 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1440 isl_basic_set_free(bset1);
1441 isl_basic_set_free(bset2);
1442 free(filename);
1444 fclose(input);
1447 struct {
1448 const char *set;
1449 const char *hull;
1450 } convex_hull_tests[] = {
1451 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1452 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1453 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1454 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1455 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1456 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1457 "i2 <= 5 and i2 >= 4; "
1458 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1459 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1460 "i2 <= 5 + i0 and i2 >= i0 }" },
1461 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1462 "{ [x, y] : 1 = 0 }" },
1463 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1464 "[x, y, 0] : x >= 0 and y < 0 }",
1465 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1466 { "{ [a, b, c] : a <= 1 and -a < b <= 1 and 0 <= c <= 2 - a - b and "
1467 "c <= a; "
1468 "[0, 2, 0]; [3, 1, 0] }",
1469 "{ [a, b, c] : b > -a and 2b >= -1 + a and 0 <= c <= a and "
1470 "5c <= 6 - a - 3b }" },
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) }" },
1878 { 0, "{ [x, y, z] : 0 <= x,y,z <= 100 and 0 < z <= 2 + 2x + 2y; "
1879 "[x, y, 0] : x,y <= 100 and y <= 9 + 11x and x <= 9 + 11y }" },
1882 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1883 * option turned off.
1885 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1887 int i;
1888 int r = 0;
1889 int bounded;
1891 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1892 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1894 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1895 const char *str = coalesce_unbounded_tests[i].str;
1896 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1897 if (test_coalesce_set(ctx, str, check_one) >= 0)
1898 continue;
1899 r = -1;
1900 break;
1903 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1905 return r;
1908 /* Inputs for coalescing tests.
1909 * "str" is a string representation of the input set.
1910 * "single_disjunct" is set if we expect the result to consist of
1911 * a single disjunct.
1913 struct {
1914 int single_disjunct;
1915 const char *str;
1916 } coalesce_tests[] = {
1917 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1918 "y >= x & x >= 2 & 5 >= y }" },
1919 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1920 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1921 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1922 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1923 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1924 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1925 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1926 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1927 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1928 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1929 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1930 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1931 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1932 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1933 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1934 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1935 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1936 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1937 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1938 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1939 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1940 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1941 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1942 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1943 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1944 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1945 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1946 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1947 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1948 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1949 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1950 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1951 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1952 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1953 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1954 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1955 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1956 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1957 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1958 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1959 "[o0, o1, o2, o3, o4, o5, o6]] : "
1960 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1961 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1962 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1963 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1964 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1965 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1966 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1967 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1968 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1969 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1970 "o6 >= i3 + i6 - o3 and M >= 0 and "
1971 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1972 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1973 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1974 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1975 "(o0 = 0 and M >= 2 and N >= 3) or "
1976 "(M = 0 and o0 = 0 and N >= 3) }" },
1977 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1978 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1979 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1980 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1981 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1982 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1983 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1984 "(y = 3 and x = 1) }" },
1985 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1986 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1987 "i1 <= M and i3 <= M and i4 <= M) or "
1988 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1989 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1990 "i4 <= -1 + M) }" },
1991 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1992 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1993 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1994 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1995 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1996 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1997 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1998 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1999 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
2000 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
2001 { 0, "{ [a, b] : exists e : 2e = a and "
2002 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
2003 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
2004 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
2005 "j >= 1 and j' <= i + j - i' and i >= 1; "
2006 "[1, 1, 1, 1] }" },
2007 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
2008 "[i,j] : exists a : j = 3a }" },
2009 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
2010 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
2011 "a >= 3) or "
2012 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
2013 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
2014 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
2015 "c <= 6 + 8a and a >= 3; "
2016 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
2017 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
2018 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2019 "[x,0] : 3 <= x <= 4 }" },
2020 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
2021 "[x,0] : 4 <= x <= 5 }" },
2022 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2023 "[x,0] : 3 <= x <= 5 }" },
2024 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
2025 "[x,0] : 3 <= x <= 4 }" },
2026 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
2027 "i1 <= 0; "
2028 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
2029 { 1, "{ [0,0]; [1,1] }" },
2030 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
2031 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
2032 "ii <= k;"
2033 "[k, 0, k] : k <= 6 and k >= 1 }" },
2034 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
2035 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
2036 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
2037 { 1, "[n] -> { [1] : n >= 0;"
2038 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
2039 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
2040 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
2041 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
2042 "2e0 <= x and 2e0 <= n);"
2043 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
2044 "n >= 0) }" },
2045 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
2046 "128e0 >= -134 + 127t1 and t1 >= 2 and "
2047 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
2048 "t1 = 1 }" },
2049 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
2050 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
2051 "[0, 0] }" },
2052 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
2053 "t1 >= 13 and t1 <= 16);"
2054 "[t1] : t1 <= 15 and t1 >= 12 }" },
2055 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
2056 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
2057 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
2058 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
2059 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
2060 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
2061 "i <= 4j + 2 }" },
2062 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
2063 "(exists (e0 : 3e0 = -2 + c0)) }" },
2064 { 0, "[n, b0, t0] -> "
2065 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2066 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2067 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2068 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2069 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
2070 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
2071 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
2072 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
2073 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2074 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2075 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2076 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
2077 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
2078 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
2079 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
2080 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
2081 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
2082 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
2083 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
2084 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
2085 { 0, "{ [i0, i1, i2] : "
2086 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
2087 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
2088 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
2089 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
2090 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
2091 "32e0 <= 31 + i0)) or "
2092 "i0 >= 0 }" },
2093 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
2094 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
2095 "2*floor((c)/2) = c and 0 <= a <= 192;"
2096 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
2098 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
2099 "(0 <= a <= b <= n) }" },
2100 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
2101 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
2102 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
2103 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2104 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2105 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2106 "b = 3 and 9e0 <= -19 + 2c)) }" },
2107 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2108 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2109 "(a = 4 and b = 3 and "
2110 "9*floor((-16 + 2c)/9) <= -19 + 2c) }" },
2111 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
2112 "(b = -1 + a and 0 < a <= 3 and "
2113 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2114 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2115 "b = 3 and 9e0 <= -19 + 2c)) }" },
2116 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2117 "[1, 0] }" },
2118 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2119 "[0, 1] }" },
2120 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2121 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2122 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
2123 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
2124 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
2125 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
2126 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
2127 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
2128 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
2129 { 1, "{ [a] : a <= 8 and "
2130 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
2131 { 1, "{ [x, y] : 2y = -x and x <= 0 or "
2132 "x <= -1 and 2y <= -x - 1 and 2y >= x - 1 }" },
2133 { 0, "{ [x, y] : 2y = -x and x <= 0 or "
2134 "x <= -2 and 2y <= -x - 1 and 2y >= x - 1 }" },
2135 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2136 "(a < 0 and 3*floor((a)/3) < a) }" },
2137 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2138 "(a < -1 and 3*floor((a)/3) < a) }" },
2139 { 1, "{ [a, b] : a <= 1024 and b >= 0 and "
2140 "((-31 - a + b <= 32*floor((-1 - a)/32) <= -33 + b and "
2141 "32*floor((-1 - a)/32) <= -16 + b + 16*floor((-1 - a)/16))"
2142 "or (2 <= a <= 15 and b < a)) }" },
2143 { 1, "{ [a] : a > 0 and ((16*floor((a)/16) < a and "
2144 "32*floor((a)/32) < a) or a <= 15) }" },
2145 { 1, "{ [a, b, c, d] : (-a + d) mod 64 = 0 and a <= 8 and b <= 1 and "
2146 "10 - a <= c <= 3 and d >= 5 and 9 - 64b <= d <= 70;"
2147 "[a, b = 1, c, d] : (-a + d) mod 64 = 0 and a <= 8 and c >= 4 and "
2148 "10 - a <= c <= 5 and 5 <= d <= 73 - c }" },
2149 { 1, "[n, m] -> { S_0[i] : (-n + i) mod 3 = 0 and m >= 3 + n and "
2150 "i >= n and 3*floor((2 + n + 2m)/3) <= n + 3m - i; "
2151 "S_0[n] : n <= m <= 2 + n }" },
2152 { 1, "{ [a, b] : exists (e0: 0 <= a <= 1 and b >= 0 and "
2153 "2e0 >= -5 + a + 2b and 2e0 >= -1 + a + b and "
2154 "2e0 <= a + b); "
2155 "[a, b] : exists (e0: 0 <= a <= 1 and 2e0 >= -5 + a + 2b and "
2156 "2e0 >= -1 - a + b and 2e0 <= -a + b and "
2157 "2e0 < -a + 2b) }" },
2158 { 1, "{ [i, j, i - 8j] : 8 <= i <= 63 and -7 + i <= 8j <= i; "
2159 "[i, 0, i] : 0 <= i <= 7 }" },
2160 { 1, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [1, 1] }" },
2161 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [0, 2] }" },
2162 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [-1, 3] }" },
2163 { 1, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [1, 1] }" },
2164 { 0, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [2, 1] }" },
2165 { 0, "{ [a, c] : (2 + a) mod 4 = 0 or "
2166 "(c = 4 + a and 4 * floor((a)/4) = a and a >= 0 and a <= 4) or "
2167 "(c = 3 + a and 4 * floor((-1 + a)/4) = -1 + a and "
2168 "a > 0 and a <= 5) }" },
2169 { 1, "{ [1, 0, 0]; [a, b, c] : -1 <= -a < b <= 0 and 2c > b }" },
2170 { 0, "{ [j, a, l] : a mod 2 = 0 and j <= 29 and a >= 2 and "
2171 "2a <= -5 + j and 32j + 2a + 2 <= 4l < 33j; "
2172 "[j, 0, l] : 4 <= j <= 29 and -3 + 33j <= 4l <= 33j }" },
2175 /* A specialized coalescing test case that would result
2176 * in a segmentation fault or a failed assertion in earlier versions of isl.
2178 static int test_coalesce_special(struct isl_ctx *ctx)
2180 const char *str;
2181 isl_map *map1, *map2;
2183 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2184 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
2185 "(y = 201 and o1 <= 239 and o1 >= 212) or "
2186 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
2187 "o1 <= 239 and o1 >= 212)) or "
2188 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
2189 "o1 <= 241 and o1 >= 240));"
2190 "[S_L220_OUT[] -> T7[]] -> "
2191 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
2192 "(y = 2 and o1 <= 241 and o1 >= 212) or "
2193 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
2194 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
2195 map1 = isl_map_read_from_str(ctx, str);
2196 map1 = isl_map_align_divs_internal(map1);
2197 map1 = isl_map_coalesce(map1);
2198 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2199 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
2200 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
2201 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
2202 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
2203 map2 = isl_map_read_from_str(ctx, str);
2204 map2 = isl_map_union(map2, map1);
2205 map2 = isl_map_align_divs_internal(map2);
2206 map2 = isl_map_coalesce(map2);
2207 isl_map_free(map2);
2208 if (!map2)
2209 return -1;
2211 return 0;
2214 /* A specialized coalescing test case that would result in an assertion
2215 * in an earlier version of isl.
2216 * The explicit call to isl_basic_set_union prevents the implicit
2217 * equality constraints in the first basic map from being detected prior
2218 * to the call to isl_set_coalesce, at least at the point
2219 * where this test case was introduced.
2221 static int test_coalesce_special2(struct isl_ctx *ctx)
2223 const char *str;
2224 isl_basic_set *bset1, *bset2;
2225 isl_set *set;
2227 str = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
2228 bset1 = isl_basic_set_read_from_str(ctx, str);
2229 str = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }" ;
2230 bset2 = isl_basic_set_read_from_str(ctx, str);
2231 set = isl_basic_set_union(bset1, bset2);
2232 set = isl_set_coalesce(set);
2233 isl_set_free(set);
2235 if (!set)
2236 return -1;
2237 return 0;
2240 /* Check that calling isl_set_coalesce does not leave other sets
2241 * that may share some information with the input to isl_set_coalesce
2242 * in an inconsistent state.
2243 * In particular, older versions of isl would modify all copies
2244 * of the basic sets in the isl_set_coalesce input in a way
2245 * that could leave them in an inconsistent state.
2246 * The result of printing any other set containing one of these
2247 * basic sets would then result in an invalid set description.
2249 static int test_coalesce_special3(isl_ctx *ctx)
2251 const char *str;
2252 char *s;
2253 isl_set *set1, *set2;
2254 isl_printer *p;
2256 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
2257 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
2258 set2 = isl_set_read_from_str(ctx, str);
2259 set1 = isl_set_union(set1, isl_set_copy(set2));
2260 set1 = isl_set_coalesce(set1);
2261 isl_set_free(set1);
2263 p = isl_printer_to_str(ctx);
2264 p = isl_printer_print_set(p, set2);
2265 isl_set_free(set2);
2266 s = isl_printer_get_str(p);
2267 isl_printer_free(p);
2268 set1 = isl_set_read_from_str(ctx, s);
2269 free(s);
2270 isl_set_free(set1);
2272 if (!set1)
2273 return -1;
2275 return 0;
2278 /* Check that calling isl_set_coalesce on the intersection of
2279 * the sets described by "s1" and "s2" does not leave other sets
2280 * that may share some information with the input to isl_set_coalesce
2281 * in an inconsistent state.
2282 * In particular, when isl_set_coalesce detects equality constraints,
2283 * it does not immediately perform Gaussian elimination on them,
2284 * but then it needs to ensure that it is performed at some point.
2285 * The input set has implicit equality constraints in the first disjunct.
2286 * It is constructed as an intersection, because otherwise
2287 * those equality constraints would already be detected during parsing.
2289 static isl_stat test_coalesce_intersection(isl_ctx *ctx,
2290 const char *s1, const char *s2)
2292 isl_set *set1, *set2;
2294 set1 = isl_set_read_from_str(ctx, s1);
2295 set2 = isl_set_read_from_str(ctx, s2);
2296 set1 = isl_set_intersect(set1, set2);
2297 isl_set_free(isl_set_coalesce(isl_set_copy(set1)));
2298 set1 = isl_set_coalesce(set1);
2299 isl_set_free(set1);
2301 if (!set1)
2302 return isl_stat_error;
2304 return isl_stat_ok;
2307 /* Check that calling isl_set_coalesce does not leave other sets
2308 * that may share some information with the input to isl_set_coalesce
2309 * in an inconsistent state, for the case where one disjunct
2310 * is a subset of the other.
2312 static isl_stat test_coalesce_special4(isl_ctx *ctx)
2314 const char *s1, *s2;
2316 s1 = "{ [a, b] : b <= 0 or a <= 1 }";
2317 s2 = "{ [a, b] : -1 <= -a < b }";
2318 return test_coalesce_intersection(ctx, s1, s2);
2321 /* Check that calling isl_set_coalesce does not leave other sets
2322 * that may share some information with the input to isl_set_coalesce
2323 * in an inconsistent state, for the case where two disjuncts
2324 * can be fused.
2326 static isl_stat test_coalesce_special5(isl_ctx *ctx)
2328 const char *s1, *s2;
2330 s1 = "{ [a, b, c] : b <= 0 }";
2331 s2 = "{ [a, b, c] : -1 <= -a < b and (c >= 0 or c < 0) }";
2332 return test_coalesce_intersection(ctx, s1, s2);
2335 /* Check that calling isl_set_coalesce does not leave other sets
2336 * that may share some information with the input to isl_set_coalesce
2337 * in an inconsistent state, for the case where two disjuncts
2338 * can be fused and where both disjuncts have implicit equality constraints.
2340 static isl_stat test_coalesce_special6(isl_ctx *ctx)
2342 const char *s1, *s2;
2344 s1 = "{ [a, b, c] : c <= 0 }";
2345 s2 = "{ [a, b, c] : 0 <= a <= b <= c or (0 <= b <= c and a > 0) }";
2346 return test_coalesce_intersection(ctx, s1, s2);
2349 /* Test the functionality of isl_set_coalesce.
2350 * That is, check that the output is always equal to the input
2351 * and in some cases that the result consists of a single disjunct.
2353 static int test_coalesce(struct isl_ctx *ctx)
2355 int i;
2357 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2358 const char *str = coalesce_tests[i].str;
2359 int check_one = coalesce_tests[i].single_disjunct;
2360 if (test_coalesce_set(ctx, str, check_one) < 0)
2361 return -1;
2364 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2365 return -1;
2366 if (test_coalesce_special(ctx) < 0)
2367 return -1;
2368 if (test_coalesce_special2(ctx) < 0)
2369 return -1;
2370 if (test_coalesce_special3(ctx) < 0)
2371 return -1;
2372 if (test_coalesce_special4(ctx) < 0)
2373 return -1;
2374 if (test_coalesce_special5(ctx) < 0)
2375 return -1;
2376 if (test_coalesce_special6(ctx) < 0)
2377 return -1;
2380 return 0;
2383 /* Construct a representation of the graph on the right of Figure 1
2384 * in "Computing the Transitive Closure of a Union of
2385 * Affine Integer Tuple Relations".
2387 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2389 isl_set *dom;
2390 isl_map *up, *right;
2392 dom = isl_set_read_from_str(ctx,
2393 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2394 "2 x - 3 y + 3 >= 0 }");
2395 right = isl_map_read_from_str(ctx,
2396 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2397 up = isl_map_read_from_str(ctx,
2398 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2399 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2400 right = isl_map_intersect_range(right, isl_set_copy(dom));
2401 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2402 up = isl_map_intersect_range(up, dom);
2403 return isl_map_union(up, right);
2406 /* Construct a representation of the power of the graph
2407 * on the right of Figure 1 in "Computing the Transitive Closure of
2408 * a Union of Affine Integer Tuple Relations".
2410 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2412 return isl_map_read_from_str(ctx,
2413 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2414 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2415 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2418 /* Construct a representation of the transitive closure of the graph
2419 * on the right of Figure 1 in "Computing the Transitive Closure of
2420 * a Union of Affine Integer Tuple Relations".
2422 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2424 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2427 static int test_closure(isl_ctx *ctx)
2429 const char *str;
2430 isl_map *map, *map2;
2431 isl_bool exact, equal;
2433 /* COCOA example 1 */
2434 map = isl_map_read_from_str(ctx,
2435 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2436 "1 <= i and i < n and 1 <= j and j < n or "
2437 "i2 = i + 1 and j2 = j - 1 and "
2438 "1 <= i and i < n and 2 <= j and j <= n }");
2439 map = isl_map_power(map, &exact);
2440 assert(exact);
2441 isl_map_free(map);
2443 /* COCOA example 1 */
2444 map = isl_map_read_from_str(ctx,
2445 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2446 "1 <= i and i < n and 1 <= j and j < n or "
2447 "i2 = i + 1 and j2 = j - 1 and "
2448 "1 <= i and i < n and 2 <= j and j <= n }");
2449 map = isl_map_transitive_closure(map, &exact);
2450 assert(exact);
2451 map2 = isl_map_read_from_str(ctx,
2452 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2453 "1 <= i and i < n and 1 <= j and j <= n and "
2454 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2455 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2456 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2457 assert(isl_map_is_equal(map, map2));
2458 isl_map_free(map2);
2459 isl_map_free(map);
2461 map = isl_map_read_from_str(ctx,
2462 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2463 " 0 <= y and y <= n }");
2464 map = isl_map_transitive_closure(map, &exact);
2465 map2 = isl_map_read_from_str(ctx,
2466 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2467 " 0 <= y and y <= n }");
2468 assert(isl_map_is_equal(map, map2));
2469 isl_map_free(map2);
2470 isl_map_free(map);
2472 /* COCOA example 2 */
2473 map = isl_map_read_from_str(ctx,
2474 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2475 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2476 "i2 = i + 2 and j2 = j - 2 and "
2477 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2478 map = isl_map_transitive_closure(map, &exact);
2479 assert(exact);
2480 map2 = isl_map_read_from_str(ctx,
2481 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2482 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2483 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2484 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2485 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2486 assert(isl_map_is_equal(map, map2));
2487 isl_map_free(map);
2488 isl_map_free(map2);
2490 /* COCOA Fig.2 left */
2491 map = isl_map_read_from_str(ctx,
2492 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2493 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2494 "j <= n or "
2495 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2496 "j <= 2 i - 3 and j <= n - 2 or "
2497 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2498 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2499 map = isl_map_transitive_closure(map, &exact);
2500 assert(exact);
2501 isl_map_free(map);
2503 /* COCOA Fig.2 right */
2504 map = isl_map_read_from_str(ctx,
2505 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2506 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2507 "j <= n or "
2508 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2509 "j <= 2 i - 4 and j <= n - 3 or "
2510 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2511 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2512 map = isl_map_power(map, &exact);
2513 assert(exact);
2514 isl_map_free(map);
2516 /* COCOA Fig.2 right */
2517 map = isl_map_read_from_str(ctx,
2518 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2519 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2520 "j <= n or "
2521 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2522 "j <= 2 i - 4 and j <= n - 3 or "
2523 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2524 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2525 map = isl_map_transitive_closure(map, &exact);
2526 assert(exact);
2527 map2 = isl_map_read_from_str(ctx,
2528 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2529 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2530 "j <= n and 3 + i + 2 j <= 3 n and "
2531 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2532 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2533 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2534 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2535 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2536 assert(isl_map_is_equal(map, map2));
2537 isl_map_free(map2);
2538 isl_map_free(map);
2540 map = cocoa_fig_1_right_graph(ctx);
2541 map = isl_map_transitive_closure(map, &exact);
2542 assert(exact);
2543 map2 = cocoa_fig_1_right_tc(ctx);
2544 assert(isl_map_is_equal(map, map2));
2545 isl_map_free(map2);
2546 isl_map_free(map);
2548 map = cocoa_fig_1_right_graph(ctx);
2549 map = isl_map_power(map, &exact);
2550 map2 = cocoa_fig_1_right_power(ctx);
2551 equal = isl_map_is_equal(map, map2);
2552 isl_map_free(map2);
2553 isl_map_free(map);
2554 if (equal < 0)
2555 return -1;
2556 if (!exact)
2557 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2558 if (!equal)
2559 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2561 /* COCOA Theorem 1 counter example */
2562 map = isl_map_read_from_str(ctx,
2563 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2564 "i2 = 1 and j2 = j or "
2565 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2566 map = isl_map_transitive_closure(map, &exact);
2567 assert(exact);
2568 isl_map_free(map);
2570 map = isl_map_read_from_str(ctx,
2571 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2572 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2573 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2574 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2575 map = isl_map_transitive_closure(map, &exact);
2576 assert(exact);
2577 isl_map_free(map);
2579 /* Kelly et al 1996, fig 12 */
2580 map = isl_map_read_from_str(ctx,
2581 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2582 "1 <= i,j,j+1 <= n or "
2583 "j = n and j2 = 1 and i2 = i + 1 and "
2584 "1 <= i,i+1 <= n }");
2585 map = isl_map_transitive_closure(map, &exact);
2586 assert(exact);
2587 map2 = isl_map_read_from_str(ctx,
2588 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2589 "1 <= i <= n and i = i2 or "
2590 "1 <= i < i2 <= n and 1 <= j <= n and "
2591 "1 <= j2 <= n }");
2592 assert(isl_map_is_equal(map, map2));
2593 isl_map_free(map2);
2594 isl_map_free(map);
2596 /* Omega's closure4 */
2597 map = isl_map_read_from_str(ctx,
2598 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2599 "1 <= x,y <= 10 or "
2600 "x2 = x + 1 and y2 = y and "
2601 "1 <= x <= 20 && 5 <= y <= 15 }");
2602 map = isl_map_transitive_closure(map, &exact);
2603 assert(exact);
2604 isl_map_free(map);
2606 map = isl_map_read_from_str(ctx,
2607 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2608 map = isl_map_transitive_closure(map, &exact);
2609 assert(!exact);
2610 map2 = isl_map_read_from_str(ctx,
2611 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2612 assert(isl_map_is_equal(map, map2));
2613 isl_map_free(map);
2614 isl_map_free(map2);
2616 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2617 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2618 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2619 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2620 map = isl_map_read_from_str(ctx, str);
2621 map = isl_map_transitive_closure(map, &exact);
2622 assert(exact);
2623 map2 = isl_map_read_from_str(ctx, str);
2624 assert(isl_map_is_equal(map, map2));
2625 isl_map_free(map);
2626 isl_map_free(map2);
2628 str = "{[0] -> [1]; [2] -> [3]}";
2629 map = isl_map_read_from_str(ctx, str);
2630 map = isl_map_transitive_closure(map, &exact);
2631 assert(exact);
2632 map2 = isl_map_read_from_str(ctx, str);
2633 assert(isl_map_is_equal(map, map2));
2634 isl_map_free(map);
2635 isl_map_free(map2);
2637 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2638 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2639 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2640 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2641 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2642 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2643 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2644 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2645 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2646 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2647 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2648 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2649 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2650 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2651 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2652 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2653 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2654 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2655 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2656 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2657 map = isl_map_read_from_str(ctx, str);
2658 map = isl_map_transitive_closure(map, NULL);
2659 assert(map);
2660 isl_map_free(map);
2662 return 0;
2665 /* Check that the actual result of a boolean operation is equal
2666 * to the expected result.
2668 static isl_stat check_bool(isl_ctx *ctx, isl_bool actual, isl_bool expected)
2670 if (actual != expected)
2671 isl_die(ctx, isl_error_unknown,
2672 "incorrect boolean operation", return isl_stat_error);
2673 return isl_stat_ok;
2676 /* Test operations on isl_bool values.
2678 * This tests:
2680 * isl_bool_not
2681 * isl_bool_ok
2683 static int test_isl_bool(isl_ctx *ctx)
2685 if (check_bool(ctx, isl_bool_not(isl_bool_true), isl_bool_false) < 0)
2686 return -1;
2687 if (check_bool(ctx, isl_bool_not(isl_bool_false), isl_bool_true) < 0)
2688 return -1;
2689 if (check_bool(ctx, isl_bool_not(isl_bool_error), isl_bool_error) < 0)
2690 return -1;
2691 if (check_bool(ctx, isl_bool_ok(0), isl_bool_false) < 0)
2692 return -1;
2693 if (check_bool(ctx, isl_bool_ok(1), isl_bool_true) < 0)
2694 return -1;
2695 if (check_bool(ctx, isl_bool_ok(-1), isl_bool_true) < 0)
2696 return -1;
2697 if (check_bool(ctx, isl_bool_ok(2), isl_bool_true) < 0)
2698 return -1;
2699 if (check_bool(ctx, isl_bool_ok(-2), isl_bool_true) < 0)
2700 return -1;
2702 return 0;
2705 static int test_lex(struct isl_ctx *ctx)
2707 isl_space *dim;
2708 isl_map *map;
2709 int empty;
2711 dim = isl_space_set_alloc(ctx, 0, 0);
2712 map = isl_map_lex_le(dim);
2713 empty = isl_map_is_empty(map);
2714 isl_map_free(map);
2716 if (empty < 0)
2717 return -1;
2718 if (empty)
2719 isl_die(ctx, isl_error_unknown,
2720 "expecting non-empty result", return -1);
2722 return 0;
2725 /* Inputs for isl_map_lexmin tests.
2726 * "map" is the input and "lexmin" is the expected result.
2728 struct {
2729 const char *map;
2730 const char *lexmin;
2731 } lexmin_tests [] = {
2732 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2733 "{ [x] -> [5] : 6 <= x <= 8; "
2734 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2735 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2736 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2737 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2738 "{ [x] -> [y] : (4y = x and x >= 0) or "
2739 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2740 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2741 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2742 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2743 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2744 /* Check that empty pieces are properly combined. */
2745 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2746 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2747 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2748 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2749 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2750 "x >= 4 }" },
2751 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2752 "a <= 255 and c <= 255 and d <= 255 - j and "
2753 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2754 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2755 "247d <= 247 + i and 248 - b <= 248d <= c and "
2756 "254d >= i - a + b and 254d >= -a + b and "
2757 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2758 "{ [i, k, j] -> "
2759 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2760 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2761 "247j >= 62738 - i and 509j <= 129795 + i and "
2762 "742j >= 188724 - i; "
2763 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2764 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2765 "16*floor((8 + b)/16) <= 7 + b; "
2766 "[a] -> [1] }",
2767 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2768 "[a] -> [b = 0] : 0 < a <= 509 }" },
2769 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2770 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2773 static int test_lexmin(struct isl_ctx *ctx)
2775 int i;
2776 int equal;
2777 const char *str;
2778 isl_basic_map *bmap;
2779 isl_map *map, *map2;
2780 isl_set *set;
2781 isl_set *set2;
2782 isl_pw_multi_aff *pma;
2784 str = "[p0, p1] -> { [] -> [] : "
2785 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2786 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2787 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2788 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2789 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2790 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2791 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2792 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2793 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2794 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2795 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2796 map = isl_map_read_from_str(ctx, str);
2797 map = isl_map_lexmin(map);
2798 isl_map_free(map);
2800 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2801 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2802 set = isl_set_read_from_str(ctx, str);
2803 set = isl_set_lexmax(set);
2804 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2805 set2 = isl_set_read_from_str(ctx, str);
2806 set = isl_set_intersect(set, set2);
2807 assert(!isl_set_is_empty(set));
2808 isl_set_free(set);
2810 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2811 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2812 map = isl_map_lexmin(map);
2813 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2814 equal = isl_map_is_equal(map, map2);
2815 isl_map_free(map);
2816 isl_map_free(map2);
2818 if (equal < 0)
2819 return -1;
2820 if (!equal)
2821 isl_die(ctx, isl_error_unknown,
2822 "unexpected result", return -1);
2825 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2826 " 8i' <= i and 8i' >= -7 + i }";
2827 bmap = isl_basic_map_read_from_str(ctx, str);
2828 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2829 map2 = isl_map_from_pw_multi_aff(pma);
2830 map = isl_map_from_basic_map(bmap);
2831 assert(isl_map_is_equal(map, map2));
2832 isl_map_free(map);
2833 isl_map_free(map2);
2835 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2836 " 8i' <= i and 8i' >= -7 + i }";
2837 set = isl_set_read_from_str(ctx, str);
2838 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2839 set2 = isl_set_from_pw_multi_aff(pma);
2840 equal = isl_set_is_equal(set, set2);
2841 isl_set_free(set);
2842 isl_set_free(set2);
2843 if (equal < 0)
2844 return -1;
2845 if (!equal)
2846 isl_die(ctx, isl_error_unknown,
2847 "unexpected difference between set and "
2848 "piecewise affine expression", return -1);
2850 return 0;
2853 /* A specialized isl_set_min_val test case that would return the wrong result
2854 * in earlier versions of isl.
2855 * The explicit call to isl_basic_set_union prevents the second basic set
2856 * from being determined to be empty prior to the call to isl_set_min_val,
2857 * at least at the point where this test case was introduced.
2859 static int test_min_special(isl_ctx *ctx)
2861 const char *str;
2862 isl_basic_set *bset1, *bset2;
2863 isl_set *set;
2864 isl_aff *obj;
2865 isl_val *res;
2866 int ok;
2868 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2869 bset1 = isl_basic_set_read_from_str(ctx, str);
2870 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2871 bset2 = isl_basic_set_read_from_str(ctx, str);
2872 set = isl_basic_set_union(bset1, bset2);
2873 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2875 res = isl_set_min_val(set, obj);
2876 ok = isl_val_cmp_si(res, 5) == 0;
2878 isl_aff_free(obj);
2879 isl_set_free(set);
2880 isl_val_free(res);
2882 if (!res)
2883 return -1;
2884 if (!ok)
2885 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2886 return -1);
2888 return 0;
2891 /* A specialized isl_set_min_val test case that would return an error
2892 * in earlier versions of isl.
2894 static int test_min_special2(isl_ctx *ctx)
2896 const char *str;
2897 isl_basic_set *bset;
2898 isl_aff *obj;
2899 isl_val *res;
2901 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
2902 bset = isl_basic_set_read_from_str(ctx, str);
2904 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
2906 res = isl_basic_set_max_val(bset, obj);
2908 isl_basic_set_free(bset);
2909 isl_aff_free(obj);
2910 isl_val_free(res);
2912 if (!res)
2913 return -1;
2915 return 0;
2918 struct {
2919 const char *set;
2920 const char *obj;
2921 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2922 __isl_keep isl_aff *obj);
2923 const char *res;
2924 } opt_tests[] = {
2925 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2926 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2927 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2928 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2929 &isl_set_max_val, "30" },
2933 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2934 * In particular, check the results on non-convex inputs.
2936 static int test_min(struct isl_ctx *ctx)
2938 int i;
2939 isl_set *set;
2940 isl_aff *obj;
2941 isl_val *val, *res;
2942 isl_bool ok;
2944 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2945 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2946 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2947 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2948 val = opt_tests[i].fn(set, obj);
2949 ok = isl_val_eq(res, val);
2950 isl_val_free(res);
2951 isl_val_free(val);
2952 isl_aff_free(obj);
2953 isl_set_free(set);
2955 if (ok < 0)
2956 return -1;
2957 if (!ok)
2958 isl_die(ctx, isl_error_unknown,
2959 "unexpected optimum", return -1);
2962 if (test_min_special(ctx) < 0)
2963 return -1;
2964 if (test_min_special2(ctx) < 0)
2965 return -1;
2967 return 0;
2970 struct must_may {
2971 isl_map *must;
2972 isl_map *may;
2975 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2976 void *dep_user, void *user)
2978 struct must_may *mm = (struct must_may *)user;
2980 if (must)
2981 mm->must = isl_map_union(mm->must, dep);
2982 else
2983 mm->may = isl_map_union(mm->may, dep);
2985 return isl_stat_ok;
2988 static int common_space(void *first, void *second)
2990 int depth = *(int *)first;
2991 return 2 * depth;
2994 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2996 isl_map *map2;
2997 int equal;
2999 if (!map)
3000 return -1;
3002 map2 = isl_map_read_from_str(map->ctx, str);
3003 equal = isl_map_is_equal(map, map2);
3004 isl_map_free(map2);
3006 return equal;
3009 static int map_check_equal(__isl_keep isl_map *map, const char *str)
3011 int equal;
3013 equal = map_is_equal(map, str);
3014 if (equal < 0)
3015 return -1;
3016 if (!equal)
3017 isl_die(isl_map_get_ctx(map), isl_error_unknown,
3018 "result not as expected", return -1);
3019 return 0;
3022 /* Is "set" equal to the set described by "str"?
3024 static isl_bool set_is_equal(__isl_keep isl_set *set, const char *str)
3026 isl_set *set2;
3027 isl_bool equal;
3029 if (!set)
3030 return isl_bool_error;
3032 set2 = isl_set_read_from_str(isl_set_get_ctx(set), str);
3033 equal = isl_set_is_equal(set, set2);
3034 isl_set_free(set2);
3036 return equal;
3039 /* Check that "set" is equal to the set described by "str".
3041 static isl_stat set_check_equal(__isl_keep isl_set *set, const char *str)
3043 isl_bool equal;
3045 equal = set_is_equal(set, str);
3046 if (equal < 0)
3047 return isl_stat_error;
3048 if (!equal)
3049 isl_die(isl_set_get_ctx(set), isl_error_unknown,
3050 "result not as expected", return isl_stat_error);
3051 return isl_stat_ok;
3054 static int test_dep(struct isl_ctx *ctx)
3056 const char *str;
3057 isl_space *dim;
3058 isl_map *map;
3059 isl_access_info *ai;
3060 isl_flow *flow;
3061 int depth;
3062 struct must_may mm;
3064 depth = 3;
3066 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3067 map = isl_map_read_from_str(ctx, str);
3068 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3070 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3071 map = isl_map_read_from_str(ctx, str);
3072 ai = isl_access_info_add_source(ai, map, 1, &depth);
3074 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3075 map = isl_map_read_from_str(ctx, str);
3076 ai = isl_access_info_add_source(ai, map, 1, &depth);
3078 flow = isl_access_info_compute_flow(ai);
3079 dim = isl_space_alloc(ctx, 0, 3, 3);
3080 mm.must = isl_map_empty(isl_space_copy(dim));
3081 mm.may = isl_map_empty(dim);
3083 isl_flow_foreach(flow, collect_must_may, &mm);
3085 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
3086 " [1,10,0] -> [2,5,0] }";
3087 assert(map_is_equal(mm.must, str));
3088 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3089 assert(map_is_equal(mm.may, str));
3091 isl_map_free(mm.must);
3092 isl_map_free(mm.may);
3093 isl_flow_free(flow);
3096 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3097 map = isl_map_read_from_str(ctx, str);
3098 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3100 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3101 map = isl_map_read_from_str(ctx, str);
3102 ai = isl_access_info_add_source(ai, map, 1, &depth);
3104 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3105 map = isl_map_read_from_str(ctx, str);
3106 ai = isl_access_info_add_source(ai, map, 0, &depth);
3108 flow = isl_access_info_compute_flow(ai);
3109 dim = isl_space_alloc(ctx, 0, 3, 3);
3110 mm.must = isl_map_empty(isl_space_copy(dim));
3111 mm.may = isl_map_empty(dim);
3113 isl_flow_foreach(flow, collect_must_may, &mm);
3115 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
3116 assert(map_is_equal(mm.must, str));
3117 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3118 assert(map_is_equal(mm.may, str));
3120 isl_map_free(mm.must);
3121 isl_map_free(mm.may);
3122 isl_flow_free(flow);
3125 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3126 map = isl_map_read_from_str(ctx, str);
3127 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3129 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3130 map = isl_map_read_from_str(ctx, str);
3131 ai = isl_access_info_add_source(ai, map, 0, &depth);
3133 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3134 map = isl_map_read_from_str(ctx, str);
3135 ai = isl_access_info_add_source(ai, map, 0, &depth);
3137 flow = isl_access_info_compute_flow(ai);
3138 dim = isl_space_alloc(ctx, 0, 3, 3);
3139 mm.must = isl_map_empty(isl_space_copy(dim));
3140 mm.may = isl_map_empty(dim);
3142 isl_flow_foreach(flow, collect_must_may, &mm);
3144 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
3145 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3146 assert(map_is_equal(mm.may, str));
3147 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3148 assert(map_is_equal(mm.must, str));
3150 isl_map_free(mm.must);
3151 isl_map_free(mm.may);
3152 isl_flow_free(flow);
3155 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
3156 map = isl_map_read_from_str(ctx, str);
3157 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3159 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3160 map = isl_map_read_from_str(ctx, str);
3161 ai = isl_access_info_add_source(ai, map, 0, &depth);
3163 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
3164 map = isl_map_read_from_str(ctx, str);
3165 ai = isl_access_info_add_source(ai, map, 0, &depth);
3167 flow = isl_access_info_compute_flow(ai);
3168 dim = isl_space_alloc(ctx, 0, 3, 3);
3169 mm.must = isl_map_empty(isl_space_copy(dim));
3170 mm.may = isl_map_empty(dim);
3172 isl_flow_foreach(flow, collect_must_may, &mm);
3174 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
3175 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
3176 assert(map_is_equal(mm.may, str));
3177 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3178 assert(map_is_equal(mm.must, str));
3180 isl_map_free(mm.must);
3181 isl_map_free(mm.may);
3182 isl_flow_free(flow);
3185 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
3186 map = isl_map_read_from_str(ctx, str);
3187 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3189 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3190 map = isl_map_read_from_str(ctx, str);
3191 ai = isl_access_info_add_source(ai, map, 0, &depth);
3193 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
3194 map = isl_map_read_from_str(ctx, str);
3195 ai = isl_access_info_add_source(ai, map, 0, &depth);
3197 flow = isl_access_info_compute_flow(ai);
3198 dim = isl_space_alloc(ctx, 0, 3, 3);
3199 mm.must = isl_map_empty(isl_space_copy(dim));
3200 mm.may = isl_map_empty(dim);
3202 isl_flow_foreach(flow, collect_must_may, &mm);
3204 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
3205 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
3206 assert(map_is_equal(mm.may, str));
3207 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3208 assert(map_is_equal(mm.must, str));
3210 isl_map_free(mm.must);
3211 isl_map_free(mm.may);
3212 isl_flow_free(flow);
3215 depth = 5;
3217 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3218 map = isl_map_read_from_str(ctx, str);
3219 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
3221 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3222 map = isl_map_read_from_str(ctx, str);
3223 ai = isl_access_info_add_source(ai, map, 1, &depth);
3225 flow = isl_access_info_compute_flow(ai);
3226 dim = isl_space_alloc(ctx, 0, 5, 5);
3227 mm.must = isl_map_empty(isl_space_copy(dim));
3228 mm.may = isl_map_empty(dim);
3230 isl_flow_foreach(flow, collect_must_may, &mm);
3232 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
3233 assert(map_is_equal(mm.must, str));
3234 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
3235 assert(map_is_equal(mm.may, str));
3237 isl_map_free(mm.must);
3238 isl_map_free(mm.may);
3239 isl_flow_free(flow);
3241 return 0;
3244 /* Check that the dependence analysis proceeds without errors.
3245 * Earlier versions of isl would break down during the analysis
3246 * due to the use of the wrong spaces.
3248 static int test_flow(isl_ctx *ctx)
3250 const char *str;
3251 isl_union_map *access, *schedule;
3252 isl_union_map *must_dep, *may_dep;
3253 int r;
3255 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
3256 access = isl_union_map_read_from_str(ctx, str);
3257 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
3258 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
3259 "S2[] -> [1,0,0,0]; "
3260 "S3[] -> [-1,0,0,0] }";
3261 schedule = isl_union_map_read_from_str(ctx, str);
3262 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
3263 isl_union_map_copy(access), schedule,
3264 &must_dep, &may_dep, NULL, NULL);
3265 isl_union_map_free(may_dep);
3266 isl_union_map_free(must_dep);
3268 return r;
3271 struct {
3272 const char *map;
3273 int sv;
3274 } sv_tests[] = {
3275 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
3276 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
3277 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
3278 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
3279 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
3280 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
3281 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3282 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3283 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
3286 int test_sv(isl_ctx *ctx)
3288 isl_union_map *umap;
3289 int i;
3290 int sv;
3292 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
3293 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
3294 sv = isl_union_map_is_single_valued(umap);
3295 isl_union_map_free(umap);
3296 if (sv < 0)
3297 return -1;
3298 if (sv_tests[i].sv && !sv)
3299 isl_die(ctx, isl_error_internal,
3300 "map not detected as single valued", return -1);
3301 if (!sv_tests[i].sv && sv)
3302 isl_die(ctx, isl_error_internal,
3303 "map detected as single valued", return -1);
3306 return 0;
3309 struct {
3310 const char *str;
3311 int bijective;
3312 } bijective_tests[] = {
3313 { "[N,M]->{[i,j] -> [i]}", 0 },
3314 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
3315 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
3316 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
3317 { "[N,M]->{[i,j] -> [j,i]}", 1 },
3318 { "[N,M]->{[i,j] -> [i+j]}", 0 },
3319 { "[N,M]->{[i,j] -> []}", 0 },
3320 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
3321 { "[N,M]->{[i,j] -> [2i]}", 0 },
3322 { "[N,M]->{[i,j] -> [i,i]}", 0 },
3323 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
3324 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
3325 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
3328 static int test_bijective(struct isl_ctx *ctx)
3330 isl_map *map;
3331 int i;
3332 int bijective;
3334 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
3335 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
3336 bijective = isl_map_is_bijective(map);
3337 isl_map_free(map);
3338 if (bijective < 0)
3339 return -1;
3340 if (bijective_tests[i].bijective && !bijective)
3341 isl_die(ctx, isl_error_internal,
3342 "map not detected as bijective", return -1);
3343 if (!bijective_tests[i].bijective && bijective)
3344 isl_die(ctx, isl_error_internal,
3345 "map detected as bijective", return -1);
3348 return 0;
3351 /* Inputs for isl_pw_qpolynomial_gist tests.
3352 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
3354 struct {
3355 const char *pwqp;
3356 const char *set;
3357 const char *gist;
3358 } pwqp_gist_tests[] = {
3359 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
3360 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
3361 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
3362 "{ [i] -> -1/2 + 1/2 * i }" },
3363 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
3366 static int test_pwqp(struct isl_ctx *ctx)
3368 int i;
3369 const char *str;
3370 isl_set *set;
3371 isl_pw_qpolynomial *pwqp1, *pwqp2;
3372 int equal;
3374 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3375 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3377 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
3378 isl_dim_in, 1, 1);
3380 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3381 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3383 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3385 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3387 isl_pw_qpolynomial_free(pwqp1);
3389 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
3390 str = pwqp_gist_tests[i].pwqp;
3391 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3392 str = pwqp_gist_tests[i].set;
3393 set = isl_set_read_from_str(ctx, str);
3394 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
3395 str = pwqp_gist_tests[i].gist;
3396 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3397 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3398 equal = isl_pw_qpolynomial_is_zero(pwqp1);
3399 isl_pw_qpolynomial_free(pwqp1);
3401 if (equal < 0)
3402 return -1;
3403 if (!equal)
3404 isl_die(ctx, isl_error_unknown,
3405 "unexpected result", return -1);
3408 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
3409 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3410 str = "{ [i] -> ([(2 * [i/2])/5]) }";
3411 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3413 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3415 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3417 isl_pw_qpolynomial_free(pwqp1);
3419 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3420 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3421 str = "{ [x] -> x }";
3422 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3424 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3426 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3428 isl_pw_qpolynomial_free(pwqp1);
3430 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3431 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3432 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3433 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3434 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3435 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3436 isl_pw_qpolynomial_free(pwqp1);
3438 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3439 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3440 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3441 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3442 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3443 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3444 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3445 isl_pw_qpolynomial_free(pwqp1);
3446 isl_pw_qpolynomial_free(pwqp2);
3447 if (equal < 0)
3448 return -1;
3449 if (!equal)
3450 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3452 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3453 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3454 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3455 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3456 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3457 isl_val_one(ctx));
3458 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3459 isl_pw_qpolynomial_free(pwqp1);
3460 isl_pw_qpolynomial_free(pwqp2);
3461 if (equal < 0)
3462 return -1;
3463 if (!equal)
3464 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3466 return 0;
3469 static int test_split_periods(isl_ctx *ctx)
3471 const char *str;
3472 isl_pw_qpolynomial *pwqp;
3474 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3475 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3476 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3477 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3479 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3481 isl_pw_qpolynomial_free(pwqp);
3483 if (!pwqp)
3484 return -1;
3486 return 0;
3489 static int test_union(isl_ctx *ctx)
3491 const char *str;
3492 isl_union_set *uset1, *uset2;
3493 isl_union_map *umap1, *umap2;
3494 int equal;
3496 str = "{ [i] : 0 <= i <= 1 }";
3497 uset1 = isl_union_set_read_from_str(ctx, str);
3498 str = "{ [1] -> [0] }";
3499 umap1 = isl_union_map_read_from_str(ctx, str);
3501 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3502 equal = isl_union_map_is_equal(umap1, umap2);
3504 isl_union_map_free(umap1);
3505 isl_union_map_free(umap2);
3507 if (equal < 0)
3508 return -1;
3509 if (!equal)
3510 isl_die(ctx, isl_error_unknown, "union maps not equal",
3511 return -1);
3513 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3514 umap1 = isl_union_map_read_from_str(ctx, str);
3515 str = "{ A[i]; B[i] }";
3516 uset1 = isl_union_set_read_from_str(ctx, str);
3518 uset2 = isl_union_map_domain(umap1);
3520 equal = isl_union_set_is_equal(uset1, uset2);
3522 isl_union_set_free(uset1);
3523 isl_union_set_free(uset2);
3525 if (equal < 0)
3526 return -1;
3527 if (!equal)
3528 isl_die(ctx, isl_error_unknown, "union sets not equal",
3529 return -1);
3531 return 0;
3534 /* Check that computing a bound of a non-zero polynomial over an unbounded
3535 * domain does not produce a rational value.
3536 * In particular, check that the upper bound is infinity.
3538 static int test_bound_unbounded_domain(isl_ctx *ctx)
3540 const char *str;
3541 isl_pw_qpolynomial *pwqp;
3542 isl_pw_qpolynomial_fold *pwf, *pwf2;
3543 isl_bool equal;
3545 str = "{ [m,n] -> -m * n }";
3546 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3547 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3548 str = "{ infty }";
3549 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3550 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3551 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
3552 isl_pw_qpolynomial_fold_free(pwf);
3553 isl_pw_qpolynomial_fold_free(pwf2);
3555 if (equal < 0)
3556 return -1;
3557 if (!equal)
3558 isl_die(ctx, isl_error_unknown,
3559 "expecting infinite polynomial bound", return -1);
3561 return 0;
3564 static int test_bound(isl_ctx *ctx)
3566 const char *str;
3567 isl_size dim;
3568 isl_pw_qpolynomial *pwqp;
3569 isl_pw_qpolynomial_fold *pwf;
3571 if (test_bound_unbounded_domain(ctx) < 0)
3572 return -1;
3574 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3575 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3576 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3577 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3578 isl_pw_qpolynomial_fold_free(pwf);
3579 if (dim < 0)
3580 return -1;
3581 if (dim != 4)
3582 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3583 return -1);
3585 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3586 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3587 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3588 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3589 isl_pw_qpolynomial_fold_free(pwf);
3590 if (dim < 0)
3591 return -1;
3592 if (dim != 1)
3593 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3594 return -1);
3596 return 0;
3599 /* isl_set is defined to isl_map internally, so the corresponding elements
3600 * are isl_basic_map objects.
3602 #undef EL_BASE
3603 #undef SET_BASE
3604 #define EL_BASE basic_map
3605 #define SET_BASE set
3606 #include "isl_test_list_templ.c"
3608 #undef EL_BASE
3609 #undef SET_BASE
3610 #define EL_BASE basic_set
3611 #define SET_BASE union_set
3612 #include "isl_test_list_templ.c"
3614 #undef EL_BASE
3615 #undef SET_BASE
3616 #define EL_BASE set
3617 #define SET_BASE union_set
3618 #include "isl_test_list_templ.c"
3620 #undef EL_BASE
3621 #undef SET_BASE
3622 #define EL_BASE basic_map
3623 #define SET_BASE map
3624 #include "isl_test_list_templ.c"
3626 #undef EL_BASE
3627 #undef SET_BASE
3628 #define EL_BASE map
3629 #define SET_BASE union_map
3630 #include "isl_test_list_templ.c"
3632 /* Check that the conversion from isl objects to lists works as expected.
3634 static int test_get_list(isl_ctx *ctx)
3636 if (test_get_list_basic_map_from_set(ctx, "{ [0]; [2]; [3] }"))
3637 return -1;
3638 if (test_get_list_basic_set_from_union_set(ctx, "{ A[0]; B[2]; B[3] }"))
3639 return -1;
3640 if (test_get_list_set_from_union_set(ctx, "{ A[0]; A[2]; B[3] }"))
3641 return -1;
3642 if (test_get_list_basic_map_from_map(ctx,
3643 "{ [0] -> [0]; [2] -> [0]; [3] -> [0] }"))
3644 return -1;
3645 if (test_get_list_map_from_union_map(ctx,
3646 "{ A[0] -> [0]; A[2] -> [0]; B[3] -> [0] }"))
3647 return -1;
3649 return 0;
3652 static int test_lift(isl_ctx *ctx)
3654 const char *str;
3655 isl_basic_map *bmap;
3656 isl_basic_set *bset;
3658 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3659 bset = isl_basic_set_read_from_str(ctx, str);
3660 bset = isl_basic_set_lift(bset);
3661 bmap = isl_basic_map_from_range(bset);
3662 bset = isl_basic_map_domain(bmap);
3663 isl_basic_set_free(bset);
3665 return 0;
3668 /* Check that isl_set_is_subset is not confused by identical
3669 * integer divisions.
3670 * The call to isl_set_normalize ensures that the equality constraints
3671 * a = b = 0 are discovered, turning e0 and e1 into identical
3672 * integer divisions. Any further simplification would remove
3673 * the duplicate integer divisions.
3675 static isl_stat test_subset_duplicate_integer_divisions(isl_ctx *ctx)
3677 const char *str;
3678 isl_bool is_subset;
3679 isl_set *set1, *set2;
3681 str = "{ [a, b, c, d] : "
3682 "exists (e0 = floor((a + d)/4), e1 = floor((d)/4), "
3683 "e2 = floor((-a - d + 4 *floor((a + d)/4))/10), "
3684 "e3 = floor((-d + 4*floor((d)/4))/10): "
3685 "10e2 = -a - 2c - d + 4e0 and 10e3 = -2c - d + 4e1 and "
3686 "b >= 0 and a <= 0 and b <= a) }";
3687 set1 = isl_set_read_from_str(ctx, str);
3688 set2 = isl_set_read_from_str(ctx, str);
3689 set2 = isl_set_normalize(set2);
3691 is_subset = isl_set_is_subset(set1, set2);
3693 isl_set_free(set1);
3694 isl_set_free(set2);
3696 if (is_subset < 0)
3697 return isl_stat_error;
3698 if (!is_subset)
3699 isl_die(ctx, isl_error_unknown,
3700 "set is not considered to be a subset of itself",
3701 return isl_stat_error);
3703 return isl_stat_ok;
3706 struct {
3707 const char *set1;
3708 const char *set2;
3709 int subset;
3710 } subset_tests[] = {
3711 { "{ [112, 0] }",
3712 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3713 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3714 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3715 { "{ [65] }",
3716 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3717 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3718 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3719 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3720 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3721 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3722 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3723 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3724 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3725 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3726 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3727 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3728 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3729 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3730 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3731 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3732 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3733 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3734 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3735 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3736 "4e0 <= -57 + i0 + i1)) or "
3737 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3738 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3739 "4e0 >= -61 + i0 + i1)) or "
3740 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3741 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3744 static int test_subset(isl_ctx *ctx)
3746 int i;
3747 isl_set *set1, *set2;
3748 int subset;
3750 if (test_subset_duplicate_integer_divisions(ctx) < 0)
3751 return -1;
3753 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3754 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3755 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3756 subset = isl_set_is_subset(set1, set2);
3757 isl_set_free(set1);
3758 isl_set_free(set2);
3759 if (subset < 0)
3760 return -1;
3761 if (subset != subset_tests[i].subset)
3762 isl_die(ctx, isl_error_unknown,
3763 "incorrect subset result", return -1);
3766 return 0;
3769 struct {
3770 const char *minuend;
3771 const char *subtrahend;
3772 const char *difference;
3773 } subtract_domain_tests[] = {
3774 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3775 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3776 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3779 static int test_subtract(isl_ctx *ctx)
3781 int i;
3782 isl_union_map *umap1, *umap2;
3783 isl_union_pw_multi_aff *upma1, *upma2;
3784 isl_union_set *uset;
3785 int equal;
3787 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3788 umap1 = isl_union_map_read_from_str(ctx,
3789 subtract_domain_tests[i].minuend);
3790 uset = isl_union_set_read_from_str(ctx,
3791 subtract_domain_tests[i].subtrahend);
3792 umap2 = isl_union_map_read_from_str(ctx,
3793 subtract_domain_tests[i].difference);
3794 umap1 = isl_union_map_subtract_domain(umap1, uset);
3795 equal = isl_union_map_is_equal(umap1, umap2);
3796 isl_union_map_free(umap1);
3797 isl_union_map_free(umap2);
3798 if (equal < 0)
3799 return -1;
3800 if (!equal)
3801 isl_die(ctx, isl_error_unknown,
3802 "incorrect subtract domain result", return -1);
3805 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3806 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3807 subtract_domain_tests[i].minuend);
3808 uset = isl_union_set_read_from_str(ctx,
3809 subtract_domain_tests[i].subtrahend);
3810 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3811 subtract_domain_tests[i].difference);
3812 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3813 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3814 isl_union_pw_multi_aff_free(upma1);
3815 isl_union_pw_multi_aff_free(upma2);
3816 if (equal < 0)
3817 return -1;
3818 if (!equal)
3819 isl_die(ctx, isl_error_unknown,
3820 "incorrect subtract domain result", return -1);
3823 return 0;
3826 /* Check that intersecting the empty basic set with another basic set
3827 * does not increase the number of constraints. In particular,
3828 * the empty basic set should maintain its canonical representation.
3830 static int test_intersect_1(isl_ctx *ctx)
3832 isl_size n1, n2;
3833 isl_basic_set *bset1, *bset2;
3835 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3836 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3837 n1 = isl_basic_set_n_constraint(bset1);
3838 bset1 = isl_basic_set_intersect(bset1, bset2);
3839 n2 = isl_basic_set_n_constraint(bset1);
3840 isl_basic_set_free(bset1);
3841 if (n1 < 0 || n2 < 0)
3842 return -1;
3843 if (n1 != n2)
3844 isl_die(ctx, isl_error_unknown,
3845 "number of constraints of empty set changed",
3846 return -1);
3848 return 0;
3851 /* Check that intersecting a set with itself does not cause
3852 * an explosion in the number of disjuncts.
3854 static isl_stat test_intersect_2(isl_ctx *ctx)
3856 int i;
3857 isl_set *set;
3859 set = isl_set_read_from_str(ctx, "{ [x,y] : x >= 0 or y >= 0 }");
3860 for (i = 0; i < 100; ++i)
3861 set = isl_set_intersect(set, isl_set_copy(set));
3862 isl_set_free(set);
3863 if (!set)
3864 return isl_stat_error;
3865 return isl_stat_ok;
3868 /* Perform some intersection tests.
3870 static int test_intersect(isl_ctx *ctx)
3872 if (test_intersect_1(ctx) < 0)
3873 return -1;
3874 if (test_intersect_2(ctx) < 0)
3875 return -1;
3877 return 0;
3880 int test_factorize(isl_ctx *ctx)
3882 const char *str;
3883 isl_basic_set *bset;
3884 isl_factorizer *f;
3886 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3887 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3888 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3889 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3890 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3891 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3892 "3i5 >= -2i0 - i2 + 3i4 }";
3893 bset = isl_basic_set_read_from_str(ctx, str);
3894 f = isl_basic_set_factorizer(bset);
3895 isl_basic_set_free(bset);
3896 isl_factorizer_free(f);
3897 if (!f)
3898 isl_die(ctx, isl_error_unknown,
3899 "failed to construct factorizer", return -1);
3901 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3902 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3903 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3904 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3905 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3906 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3907 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3908 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3909 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3910 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3911 bset = isl_basic_set_read_from_str(ctx, str);
3912 f = isl_basic_set_factorizer(bset);
3913 isl_basic_set_free(bset);
3914 isl_factorizer_free(f);
3915 if (!f)
3916 isl_die(ctx, isl_error_unknown,
3917 "failed to construct factorizer", return -1);
3919 return 0;
3922 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3924 int *injective = user;
3926 *injective = isl_map_is_injective(map);
3927 isl_map_free(map);
3929 if (*injective < 0 || !*injective)
3930 return isl_stat_error;
3932 return isl_stat_ok;
3935 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3936 const char *r, const char *s, int tilable, int parallel)
3938 int i;
3939 isl_union_set *D;
3940 isl_union_map *W, *R, *S;
3941 isl_union_map *empty;
3942 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3943 isl_union_map *validity, *proximity, *coincidence;
3944 isl_union_map *schedule;
3945 isl_union_map *test;
3946 isl_union_set *delta;
3947 isl_union_set *domain;
3948 isl_set *delta_set;
3949 isl_set *slice;
3950 isl_set *origin;
3951 isl_schedule_constraints *sc;
3952 isl_schedule *sched;
3953 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3954 isl_size n;
3956 D = isl_union_set_read_from_str(ctx, d);
3957 W = isl_union_map_read_from_str(ctx, w);
3958 R = isl_union_map_read_from_str(ctx, r);
3959 S = isl_union_map_read_from_str(ctx, s);
3961 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3962 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3964 empty = isl_union_map_empty(isl_union_map_get_space(S));
3965 isl_union_map_compute_flow(isl_union_map_copy(R),
3966 isl_union_map_copy(W), empty,
3967 isl_union_map_copy(S),
3968 &dep_raw, NULL, NULL, NULL);
3969 isl_union_map_compute_flow(isl_union_map_copy(W),
3970 isl_union_map_copy(W),
3971 isl_union_map_copy(R),
3972 isl_union_map_copy(S),
3973 &dep_waw, &dep_war, NULL, NULL);
3975 dep = isl_union_map_union(dep_waw, dep_war);
3976 dep = isl_union_map_union(dep, dep_raw);
3977 validity = isl_union_map_copy(dep);
3978 coincidence = isl_union_map_copy(dep);
3979 proximity = isl_union_map_copy(dep);
3981 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3982 sc = isl_schedule_constraints_set_validity(sc, validity);
3983 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3984 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3985 sched = isl_schedule_constraints_compute_schedule(sc);
3986 schedule = isl_schedule_get_map(sched);
3987 isl_schedule_free(sched);
3988 isl_union_map_free(W);
3989 isl_union_map_free(R);
3990 isl_union_map_free(S);
3992 is_injection = 1;
3993 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3995 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3996 is_complete = isl_union_set_is_subset(D, domain);
3997 isl_union_set_free(D);
3998 isl_union_set_free(domain);
4000 test = isl_union_map_reverse(isl_union_map_copy(schedule));
4001 test = isl_union_map_apply_range(test, dep);
4002 test = isl_union_map_apply_range(test, schedule);
4004 delta = isl_union_map_deltas(test);
4005 n = isl_union_set_n_set(delta);
4006 if (n < 0) {
4007 isl_union_set_free(delta);
4008 return -1;
4010 if (n == 0) {
4011 is_tilable = 1;
4012 is_parallel = 1;
4013 is_nonneg = 1;
4014 isl_union_set_free(delta);
4015 } else {
4016 isl_size dim;
4018 delta_set = isl_set_from_union_set(delta);
4020 slice = isl_set_universe(isl_set_get_space(delta_set));
4021 for (i = 0; i < tilable; ++i)
4022 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
4023 is_tilable = isl_set_is_subset(delta_set, slice);
4024 isl_set_free(slice);
4026 slice = isl_set_universe(isl_set_get_space(delta_set));
4027 for (i = 0; i < parallel; ++i)
4028 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
4029 is_parallel = isl_set_is_subset(delta_set, slice);
4030 isl_set_free(slice);
4032 origin = isl_set_universe(isl_set_get_space(delta_set));
4033 dim = isl_set_dim(origin, isl_dim_set);
4034 if (dim < 0)
4035 origin = isl_set_free(origin);
4036 for (i = 0; i < dim; ++i)
4037 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
4039 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
4040 delta_set = isl_set_lexmin(delta_set);
4042 is_nonneg = isl_set_is_equal(delta_set, origin);
4044 isl_set_free(origin);
4045 isl_set_free(delta_set);
4048 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
4049 is_injection < 0 || is_complete < 0)
4050 return -1;
4051 if (!is_complete)
4052 isl_die(ctx, isl_error_unknown,
4053 "generated schedule incomplete", return -1);
4054 if (!is_injection)
4055 isl_die(ctx, isl_error_unknown,
4056 "generated schedule not injective on each statement",
4057 return -1);
4058 if (!is_nonneg)
4059 isl_die(ctx, isl_error_unknown,
4060 "negative dependences in generated schedule",
4061 return -1);
4062 if (!is_tilable)
4063 isl_die(ctx, isl_error_unknown,
4064 "generated schedule not as tilable as expected",
4065 return -1);
4066 if (!is_parallel)
4067 isl_die(ctx, isl_error_unknown,
4068 "generated schedule not as parallel as expected",
4069 return -1);
4071 return 0;
4074 /* Compute a schedule for the given instance set, validity constraints,
4075 * proximity constraints and context and return a corresponding union map
4076 * representation.
4078 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
4079 const char *domain, const char *validity, const char *proximity,
4080 const char *context)
4082 isl_set *con;
4083 isl_union_set *dom;
4084 isl_union_map *dep;
4085 isl_union_map *prox;
4086 isl_schedule_constraints *sc;
4087 isl_schedule *schedule;
4088 isl_union_map *sched;
4090 con = isl_set_read_from_str(ctx, context);
4091 dom = isl_union_set_read_from_str(ctx, domain);
4092 dep = isl_union_map_read_from_str(ctx, validity);
4093 prox = isl_union_map_read_from_str(ctx, proximity);
4094 sc = isl_schedule_constraints_on_domain(dom);
4095 sc = isl_schedule_constraints_set_context(sc, con);
4096 sc = isl_schedule_constraints_set_validity(sc, dep);
4097 sc = isl_schedule_constraints_set_proximity(sc, prox);
4098 schedule = isl_schedule_constraints_compute_schedule(sc);
4099 sched = isl_schedule_get_map(schedule);
4100 isl_schedule_free(schedule);
4102 return sched;
4105 /* Compute a schedule for the given instance set, validity constraints and
4106 * proximity constraints and return a corresponding union map representation.
4108 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
4109 const char *domain, const char *validity, const char *proximity)
4111 return compute_schedule_with_context(ctx, domain, validity, proximity,
4112 "{ : }");
4115 /* Check that a schedule can be constructed on the given domain
4116 * with the given validity and proximity constraints.
4118 static int test_has_schedule(isl_ctx *ctx, const char *domain,
4119 const char *validity, const char *proximity)
4121 isl_union_map *sched;
4123 sched = compute_schedule(ctx, domain, validity, proximity);
4124 if (!sched)
4125 return -1;
4127 isl_union_map_free(sched);
4128 return 0;
4131 int test_special_schedule(isl_ctx *ctx, const char *domain,
4132 const char *validity, const char *proximity, const char *expected_sched)
4134 isl_union_map *sched1, *sched2;
4135 int equal;
4137 sched1 = compute_schedule(ctx, domain, validity, proximity);
4138 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
4140 equal = isl_union_map_is_equal(sched1, sched2);
4141 isl_union_map_free(sched1);
4142 isl_union_map_free(sched2);
4144 if (equal < 0)
4145 return -1;
4146 if (!equal)
4147 isl_die(ctx, isl_error_unknown, "unexpected schedule",
4148 return -1);
4150 return 0;
4153 /* Check that the schedule map is properly padded, i.e., that the range
4154 * lives in a single space.
4156 static int test_padded_schedule(isl_ctx *ctx)
4158 const char *str;
4159 isl_union_set *D;
4160 isl_union_map *validity, *proximity;
4161 isl_schedule_constraints *sc;
4162 isl_schedule *sched;
4163 isl_union_map *umap;
4164 isl_union_set *range;
4165 isl_set *set;
4167 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
4168 D = isl_union_set_read_from_str(ctx, str);
4169 validity = isl_union_map_empty(isl_union_set_get_space(D));
4170 proximity = isl_union_map_copy(validity);
4171 sc = isl_schedule_constraints_on_domain(D);
4172 sc = isl_schedule_constraints_set_validity(sc, validity);
4173 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4174 sched = isl_schedule_constraints_compute_schedule(sc);
4175 umap = isl_schedule_get_map(sched);
4176 isl_schedule_free(sched);
4177 range = isl_union_map_range(umap);
4178 set = isl_set_from_union_set(range);
4179 isl_set_free(set);
4181 if (!set)
4182 return -1;
4184 return 0;
4187 /* Check that conditional validity constraints are also taken into
4188 * account across bands.
4189 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
4190 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
4191 * and then check that the adjacent order constraint C[2,1]->D[2,0]
4192 * is enforced by the rest of the schedule.
4194 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
4196 const char *str;
4197 isl_union_set *domain;
4198 isl_union_map *validity, *proximity, *condition;
4199 isl_union_map *sink, *source, *dep;
4200 isl_schedule_constraints *sc;
4201 isl_schedule *schedule;
4202 isl_union_access_info *access;
4203 isl_union_flow *flow;
4204 int empty;
4206 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4207 "A[k] : k >= 1 and k <= -1 + n; "
4208 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4209 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
4210 domain = isl_union_set_read_from_str(ctx, str);
4211 sc = isl_schedule_constraints_on_domain(domain);
4212 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
4213 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4214 "D[k, i] -> C[1 + k, i] : "
4215 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4216 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
4217 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
4218 validity = isl_union_map_read_from_str(ctx, str);
4219 sc = isl_schedule_constraints_set_validity(sc, validity);
4220 str = "[n] -> { C[k, i] -> D[k, i] : "
4221 "0 <= i <= -1 + k and k <= -1 + n }";
4222 proximity = isl_union_map_read_from_str(ctx, str);
4223 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4224 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
4225 "i <= -1 + k and i >= 1 and k <= -2 + n; "
4226 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
4227 "k <= -1 + n and i >= 0 and i <= -2 + k }";
4228 condition = isl_union_map_read_from_str(ctx, str);
4229 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
4230 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4231 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
4232 "i >= 0 and i <= -1 + k and k <= -1 + n and "
4233 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
4234 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
4235 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4236 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
4237 "k <= -1 + n and i >= 0 and i <= -1 + k and "
4238 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
4239 validity = isl_union_map_read_from_str(ctx, str);
4240 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4241 validity);
4242 schedule = isl_schedule_constraints_compute_schedule(sc);
4243 str = "{ D[2,0] -> [] }";
4244 sink = isl_union_map_read_from_str(ctx, str);
4245 access = isl_union_access_info_from_sink(sink);
4246 str = "{ C[2,1] -> [] }";
4247 source = isl_union_map_read_from_str(ctx, str);
4248 access = isl_union_access_info_set_must_source(access, source);
4249 access = isl_union_access_info_set_schedule(access, schedule);
4250 flow = isl_union_access_info_compute_flow(access);
4251 dep = isl_union_flow_get_must_dependence(flow);
4252 isl_union_flow_free(flow);
4253 empty = isl_union_map_is_empty(dep);
4254 isl_union_map_free(dep);
4256 if (empty < 0)
4257 return -1;
4258 if (empty)
4259 isl_die(ctx, isl_error_unknown,
4260 "conditional validity not respected", return -1);
4262 return 0;
4265 /* Check that the test for violated conditional validity constraints
4266 * is not confused by domain compression.
4267 * In particular, earlier versions of isl would apply
4268 * a schedule on the compressed domains to the original domains,
4269 * resulting in a failure to detect that the default schedule
4270 * violates the conditional validity constraints.
4272 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
4274 const char *str;
4275 isl_bool empty;
4276 isl_union_set *domain;
4277 isl_union_map *validity, *condition;
4278 isl_schedule_constraints *sc;
4279 isl_schedule *schedule;
4280 isl_union_map *umap;
4281 isl_map *map, *ge;
4283 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
4284 domain = isl_union_set_read_from_str(ctx, str);
4285 sc = isl_schedule_constraints_on_domain(domain);
4286 str = "{ B[1, i] -> A[0, i + 1] }";
4287 condition = isl_union_map_read_from_str(ctx, str);
4288 str = "{ A[0, i] -> B[1, i - 1] }";
4289 validity = isl_union_map_read_from_str(ctx, str);
4290 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4291 isl_union_map_copy(validity));
4292 schedule = isl_schedule_constraints_compute_schedule(sc);
4293 umap = isl_schedule_get_map(schedule);
4294 isl_schedule_free(schedule);
4295 validity = isl_union_map_apply_domain(validity,
4296 isl_union_map_copy(umap));
4297 validity = isl_union_map_apply_range(validity, umap);
4298 map = isl_map_from_union_map(validity);
4299 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4300 map = isl_map_intersect(map, ge);
4301 empty = isl_map_is_empty(map);
4302 isl_map_free(map);
4304 if (empty < 0)
4305 return -1;
4306 if (!empty)
4307 isl_die(ctx, isl_error_unknown,
4308 "conditional validity constraints not satisfied",
4309 return -1);
4311 return 0;
4314 /* Input for testing of schedule construction based on
4315 * conditional constraints.
4317 * domain is the iteration domain
4318 * flow are the flow dependences, which determine the validity and
4319 * proximity constraints
4320 * condition are the conditions on the conditional validity constraints
4321 * conditional_validity are the conditional validity constraints
4322 * outer_band_n is the expected number of members in the outer band
4324 struct {
4325 const char *domain;
4326 const char *flow;
4327 const char *condition;
4328 const char *conditional_validity;
4329 int outer_band_n;
4330 } live_range_tests[] = {
4331 /* Contrived example that illustrates that we need to keep
4332 * track of tagged condition dependences and
4333 * tagged conditional validity dependences
4334 * in isl_sched_edge separately.
4335 * In particular, the conditional validity constraints on A
4336 * cannot be satisfied,
4337 * but they can be ignored because there are no corresponding
4338 * condition constraints. However, we do have an additional
4339 * conditional validity constraint that maps to the same
4340 * dependence relation
4341 * as the condition constraint on B. If we did not make a distinction
4342 * between tagged condition and tagged conditional validity
4343 * dependences, then we
4344 * could end up treating this shared dependence as an condition
4345 * constraint on A, forcing a localization of the conditions,
4346 * which is impossible.
4348 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
4349 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
4350 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
4351 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4352 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4353 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
4356 /* TACO 2013 Fig. 7 */
4357 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4358 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4359 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4360 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
4361 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
4362 "0 <= i < n and 0 <= j < n - 1 }",
4363 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
4364 "0 <= i < n and 0 <= j < j' < n;"
4365 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
4366 "0 <= i < i' < n and 0 <= j,j' < n;"
4367 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
4368 "0 <= i,j,j' < n and j < j' }",
4371 /* TACO 2013 Fig. 7, without tags */
4372 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4373 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4374 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4375 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4376 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4377 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
4378 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
4379 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
4382 /* TACO 2013 Fig. 12 */
4383 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
4384 "S3[i,3] : 0 <= i <= 1 }",
4385 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
4386 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
4387 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
4388 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
4389 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4390 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
4391 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4392 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
4393 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
4394 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
4395 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
4400 /* Test schedule construction based on conditional constraints.
4401 * In particular, check the number of members in the outer band node
4402 * as an indication of whether tiling is possible or not.
4404 static int test_conditional_schedule_constraints(isl_ctx *ctx)
4406 int i;
4407 isl_union_set *domain;
4408 isl_union_map *condition;
4409 isl_union_map *flow;
4410 isl_union_map *validity;
4411 isl_schedule_constraints *sc;
4412 isl_schedule *schedule;
4413 isl_schedule_node *node;
4414 isl_size n_member;
4416 if (test_special_conditional_schedule_constraints(ctx) < 0)
4417 return -1;
4418 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
4419 return -1;
4421 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
4422 domain = isl_union_set_read_from_str(ctx,
4423 live_range_tests[i].domain);
4424 flow = isl_union_map_read_from_str(ctx,
4425 live_range_tests[i].flow);
4426 condition = isl_union_map_read_from_str(ctx,
4427 live_range_tests[i].condition);
4428 validity = isl_union_map_read_from_str(ctx,
4429 live_range_tests[i].conditional_validity);
4430 sc = isl_schedule_constraints_on_domain(domain);
4431 sc = isl_schedule_constraints_set_validity(sc,
4432 isl_union_map_copy(flow));
4433 sc = isl_schedule_constraints_set_proximity(sc, flow);
4434 sc = isl_schedule_constraints_set_conditional_validity(sc,
4435 condition, validity);
4436 schedule = isl_schedule_constraints_compute_schedule(sc);
4437 node = isl_schedule_get_root(schedule);
4438 while (node &&
4439 isl_schedule_node_get_type(node) != isl_schedule_node_band)
4440 node = isl_schedule_node_first_child(node);
4441 n_member = isl_schedule_node_band_n_member(node);
4442 isl_schedule_node_free(node);
4443 isl_schedule_free(schedule);
4445 if (!schedule || n_member < 0)
4446 return -1;
4447 if (n_member != live_range_tests[i].outer_band_n)
4448 isl_die(ctx, isl_error_unknown,
4449 "unexpected number of members in outer band",
4450 return -1);
4452 return 0;
4455 /* Check that the schedule computed for the given instance set and
4456 * dependence relation strongly satisfies the dependences.
4457 * In particular, check that no instance is scheduled before
4458 * or together with an instance on which it depends.
4459 * Earlier versions of isl would produce a schedule that
4460 * only weakly satisfies the dependences.
4462 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
4464 const char *domain, *dep;
4465 isl_union_map *D, *schedule;
4466 isl_map *map, *ge;
4467 int empty;
4469 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
4470 "A[i0] : 0 <= i0 <= 1 }";
4471 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
4472 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
4473 schedule = compute_schedule(ctx, domain, dep, dep);
4474 D = isl_union_map_read_from_str(ctx, dep);
4475 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
4476 D = isl_union_map_apply_range(D, schedule);
4477 map = isl_map_from_union_map(D);
4478 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4479 map = isl_map_intersect(map, ge);
4480 empty = isl_map_is_empty(map);
4481 isl_map_free(map);
4483 if (empty < 0)
4484 return -1;
4485 if (!empty)
4486 isl_die(ctx, isl_error_unknown,
4487 "dependences not strongly satisfied", return -1);
4489 return 0;
4492 /* Compute a schedule for input where the instance set constraints
4493 * conflict with the context constraints.
4494 * Earlier versions of isl did not properly handle this situation.
4496 static int test_conflicting_context_schedule(isl_ctx *ctx)
4498 isl_union_map *schedule;
4499 const char *domain, *context;
4501 domain = "[n] -> { A[] : n >= 0 }";
4502 context = "[n] -> { : n < 0 }";
4503 schedule = compute_schedule_with_context(ctx,
4504 domain, "{}", "{}", context);
4505 isl_union_map_free(schedule);
4507 if (!schedule)
4508 return -1;
4510 return 0;
4513 /* Check that a set of schedule constraints that only allow for
4514 * a coalescing schedule still produces a schedule even if the user
4515 * request a non-coalescing schedule. Earlier versions of isl
4516 * would not handle this case correctly.
4518 static int test_coalescing_schedule(isl_ctx *ctx)
4520 const char *domain, *dep;
4521 isl_union_set *I;
4522 isl_union_map *D;
4523 isl_schedule_constraints *sc;
4524 isl_schedule *schedule;
4525 int treat_coalescing;
4527 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4528 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4529 I = isl_union_set_read_from_str(ctx, domain);
4530 D = isl_union_map_read_from_str(ctx, dep);
4531 sc = isl_schedule_constraints_on_domain(I);
4532 sc = isl_schedule_constraints_set_validity(sc, D);
4533 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4534 isl_options_set_schedule_treat_coalescing(ctx, 1);
4535 schedule = isl_schedule_constraints_compute_schedule(sc);
4536 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4537 isl_schedule_free(schedule);
4538 if (!schedule)
4539 return -1;
4540 return 0;
4543 /* Check that the scheduler does not perform any needless
4544 * compound skewing. Earlier versions of isl would compute
4545 * schedules in terms of transformed schedule coefficients and
4546 * would not accurately keep track of the sum of the original
4547 * schedule coefficients. It could then produce the schedule
4548 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
4549 * for the input below instead of the schedule below.
4551 static int test_skewing_schedule(isl_ctx *ctx)
4553 const char *D, *V, *P, *S;
4555 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
4556 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
4557 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
4558 "-1 <= a-i + b-j + c-k <= 1 }";
4559 P = "{ }";
4560 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
4562 return test_special_schedule(ctx, D, V, P, S);
4565 int test_schedule(isl_ctx *ctx)
4567 const char *D, *W, *R, *V, *P, *S;
4568 int max_coincidence;
4569 int treat_coalescing;
4571 /* Handle resulting schedule with zero bands. */
4572 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4573 return -1;
4575 /* Jacobi */
4576 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4577 W = "{ S1[t,i] -> a[t,i] }";
4578 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4579 "S1[t,i] -> a[t-1,i+1] }";
4580 S = "{ S1[t,i] -> [t,i] }";
4581 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4582 return -1;
4584 /* Fig. 5 of CC2008 */
4585 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
4586 "j <= -1 + N }";
4587 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
4588 "j >= 2 and j <= -1 + N }";
4589 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
4590 "j >= 2 and j <= -1 + N; "
4591 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
4592 "j >= 2 and j <= -1 + N }";
4593 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4594 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4595 return -1;
4597 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
4598 W = "{ S1[i] -> a[i] }";
4599 R = "{ S2[i] -> a[i+1] }";
4600 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4601 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4602 return -1;
4604 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
4605 W = "{ S1[i] -> a[i] }";
4606 R = "{ S2[i] -> a[9-i] }";
4607 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4608 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4609 return -1;
4611 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4612 W = "{ S1[i] -> a[i] }";
4613 R = "[N] -> { S2[i] -> a[N-1-i] }";
4614 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4615 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4616 return -1;
4618 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4619 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4620 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4621 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4622 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4623 return -1;
4625 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4626 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4627 R = "{ S2[i,j] -> a[i-1,j] }";
4628 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4629 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4630 return -1;
4632 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4633 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4634 R = "{ S2[i,j] -> a[i,j-1] }";
4635 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4636 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4637 return -1;
4639 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4640 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4641 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4642 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4643 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4644 "S_0[] -> [0, 0, 0] }";
4645 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4646 return -1;
4647 ctx->opt->schedule_parametric = 0;
4648 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4649 return -1;
4650 ctx->opt->schedule_parametric = 1;
4652 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4653 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4654 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4655 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4656 "S4[i] -> a[i,N] }";
4657 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4658 "S4[i] -> [4,i,0] }";
4659 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4660 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4661 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4662 return -1;
4663 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4665 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4666 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4667 "j <= N }";
4668 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4669 "j <= N; "
4670 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4671 "j <= N }";
4672 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4673 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4674 return -1;
4676 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4677 " S_2[t] : t >= 0 and t <= -1 + N; "
4678 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4679 "i <= -1 + N }";
4680 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4681 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4682 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4683 "i >= 0 and i <= -1 + N }";
4684 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4685 "i >= 0 and i <= -1 + N; "
4686 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4687 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4688 " S_0[t] -> [0, t, 0] }";
4690 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4691 return -1;
4692 ctx->opt->schedule_parametric = 0;
4693 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4694 return -1;
4695 ctx->opt->schedule_parametric = 1;
4697 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4698 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4699 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4700 return -1;
4702 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4703 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4704 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
4705 "j >= 0 and j <= -1 + N; "
4706 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4707 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4708 "j >= 0 and j <= -1 + N; "
4709 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4710 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4711 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4712 return -1;
4714 D = "{ S_0[i] : i >= 0 }";
4715 W = "{ S_0[i] -> a[i] : i >= 0 }";
4716 R = "{ S_0[i] -> a[0] : i >= 0 }";
4717 S = "{ S_0[i] -> [0, i, 0] }";
4718 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4719 return -1;
4721 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4722 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4723 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4724 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4725 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4726 return -1;
4728 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4729 "k <= -1 + n and k >= 0 }";
4730 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4731 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4732 "k <= -1 + n and k >= 0; "
4733 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4734 "k <= -1 + n and k >= 0; "
4735 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4736 "k <= -1 + n and k >= 0 }";
4737 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4738 ctx->opt->schedule_outer_coincidence = 1;
4739 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4740 return -1;
4741 ctx->opt->schedule_outer_coincidence = 0;
4743 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4744 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4745 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4746 "Stmt_for_body24[i0, i1, 1, 0]:"
4747 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4748 "Stmt_for_body7[i0, i1, i2]:"
4749 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4750 "i2 <= 7 }";
4752 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4753 "Stmt_for_body24[1, i1, i2, i3]:"
4754 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4755 "i2 >= 1;"
4756 "Stmt_for_body24[0, i1, i2, i3] -> "
4757 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4758 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4759 "i3 >= 0;"
4760 "Stmt_for_body24[0, i1, i2, i3] ->"
4761 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4762 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4763 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4764 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4765 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4766 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4767 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4768 "i1 <= 6 and i1 >= 0;"
4769 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4770 "Stmt_for_body7[i0, i1, i2] -> "
4771 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4772 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4773 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4774 "Stmt_for_body7[i0, i1, i2] -> "
4775 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4776 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4777 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4778 P = V;
4780 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4781 isl_options_set_schedule_treat_coalescing(ctx, 0);
4782 if (test_has_schedule(ctx, D, V, P) < 0)
4783 return -1;
4784 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4786 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4787 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4788 "j >= 1 and j <= 7;"
4789 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4790 "j >= 1 and j <= 8 }";
4791 P = "{ }";
4792 S = "{ S_0[i, j] -> [i + j, i] }";
4793 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4794 if (test_special_schedule(ctx, D, V, P, S) < 0)
4795 return -1;
4796 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4798 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4799 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4800 "j >= 0 and j <= -1 + i }";
4801 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4802 "i <= -1 + N and j >= 0;"
4803 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4804 "i <= -2 + N }";
4805 P = "{ }";
4806 S = "{ S_0[i, j] -> [i, j] }";
4807 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4808 if (test_special_schedule(ctx, D, V, P, S) < 0)
4809 return -1;
4810 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4812 /* Test both algorithms on a case with only proximity dependences. */
4813 D = "{ S[i,j] : 0 <= i <= 10 }";
4814 V = "{ }";
4815 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4816 S = "{ S[i, j] -> [j, i] }";
4817 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4818 if (test_special_schedule(ctx, D, V, P, S) < 0)
4819 return -1;
4820 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4821 if (test_special_schedule(ctx, D, V, P, S) < 0)
4822 return -1;
4824 D = "{ A[a]; B[] }";
4825 V = "{}";
4826 P = "{ A[a] -> B[] }";
4827 if (test_has_schedule(ctx, D, V, P) < 0)
4828 return -1;
4830 if (test_padded_schedule(ctx) < 0)
4831 return -1;
4833 /* Check that check for progress is not confused by rational
4834 * solution.
4836 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4837 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4838 "i0 <= -2 + N; "
4839 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4840 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4841 P = "{}";
4842 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4843 if (test_has_schedule(ctx, D, V, P) < 0)
4844 return -1;
4845 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4847 /* Check that we allow schedule rows that are only non-trivial
4848 * on some full-dimensional domains.
4850 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4851 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4852 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4853 P = "{}";
4854 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4855 if (test_has_schedule(ctx, D, V, P) < 0)
4856 return -1;
4857 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4859 if (test_conditional_schedule_constraints(ctx) < 0)
4860 return -1;
4862 if (test_strongly_satisfying_schedule(ctx) < 0)
4863 return -1;
4865 if (test_conflicting_context_schedule(ctx) < 0)
4866 return -1;
4868 if (test_coalescing_schedule(ctx) < 0)
4869 return -1;
4870 if (test_skewing_schedule(ctx) < 0)
4871 return -1;
4873 return 0;
4876 /* Perform scheduling tests using the whole component scheduler.
4878 static int test_schedule_whole(isl_ctx *ctx)
4880 int whole;
4881 int r;
4883 whole = isl_options_get_schedule_whole_component(ctx);
4884 isl_options_set_schedule_whole_component(ctx, 1);
4885 r = test_schedule(ctx);
4886 isl_options_set_schedule_whole_component(ctx, whole);
4888 return r;
4891 /* Perform scheduling tests using the incremental scheduler.
4893 static int test_schedule_incremental(isl_ctx *ctx)
4895 int whole;
4896 int r;
4898 whole = isl_options_get_schedule_whole_component(ctx);
4899 isl_options_set_schedule_whole_component(ctx, 0);
4900 r = test_schedule(ctx);
4901 isl_options_set_schedule_whole_component(ctx, whole);
4903 return r;
4906 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4908 isl_union_map *umap;
4909 int test;
4911 umap = isl_union_map_read_from_str(ctx, str);
4912 test = isl_union_map_plain_is_injective(umap);
4913 isl_union_map_free(umap);
4914 if (test < 0)
4915 return -1;
4916 if (test == injective)
4917 return 0;
4918 if (injective)
4919 isl_die(ctx, isl_error_unknown,
4920 "map not detected as injective", return -1);
4921 else
4922 isl_die(ctx, isl_error_unknown,
4923 "map detected as injective", return -1);
4926 int test_injective(isl_ctx *ctx)
4928 const char *str;
4930 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4931 return -1;
4932 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4933 return -1;
4934 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4935 return -1;
4936 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4937 return -1;
4938 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4939 return -1;
4940 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4941 return -1;
4942 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4943 return -1;
4944 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4945 return -1;
4947 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4948 if (test_plain_injective(ctx, str, 1))
4949 return -1;
4950 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4951 if (test_plain_injective(ctx, str, 0))
4952 return -1;
4954 return 0;
4957 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4959 isl_aff *aff2;
4960 int equal;
4962 if (!aff)
4963 return -1;
4965 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4966 equal = isl_aff_plain_is_equal(aff, aff2);
4967 isl_aff_free(aff2);
4969 return equal;
4972 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4974 int equal;
4976 equal = aff_plain_is_equal(aff, str);
4977 if (equal < 0)
4978 return -1;
4979 if (!equal)
4980 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4981 "result not as expected", return -1);
4982 return 0;
4985 /* Is "upa" obviously equal to the isl_union_pw_aff represented by "str"?
4987 static isl_bool union_pw_aff_plain_is_equal(__isl_keep isl_union_pw_aff *upa,
4988 const char *str)
4990 isl_ctx *ctx;
4991 isl_union_pw_aff *upa2;
4992 isl_bool equal;
4994 if (!upa)
4995 return isl_bool_error;
4997 ctx = isl_union_pw_aff_get_ctx(upa);
4998 upa2 = isl_union_pw_aff_read_from_str(ctx, str);
4999 equal = isl_union_pw_aff_plain_is_equal(upa, upa2);
5000 isl_union_pw_aff_free(upa2);
5002 return equal;
5005 /* Check that "upa" is obviously equal to the isl_union_pw_aff
5006 * represented by "str".
5008 static isl_stat union_pw_aff_check_plain_equal(__isl_keep isl_union_pw_aff *upa,
5009 const char *str)
5011 isl_bool equal;
5013 equal = union_pw_aff_plain_is_equal(upa, str);
5014 if (equal < 0)
5015 return isl_stat_error;
5016 if (!equal)
5017 isl_die(isl_union_pw_aff_get_ctx(upa), isl_error_unknown,
5018 "result not as expected", return isl_stat_error);
5019 return isl_stat_ok;
5022 /* Basic tests on isl_union_pw_aff.
5024 * In particular, check that isl_union_pw_aff_aff_on_domain
5025 * aligns the parameters of the input objects and
5026 * that isl_union_pw_aff_param_on_domain_id properly
5027 * introduces the parameter.
5029 static int test_upa(isl_ctx *ctx)
5031 const char *str;
5032 isl_id *id;
5033 isl_aff *aff;
5034 isl_union_set *domain;
5035 isl_union_pw_aff *upa;
5036 isl_stat ok;
5038 aff = isl_aff_read_from_str(ctx, "[N] -> { [N] }");
5039 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5040 domain = isl_union_set_read_from_str(ctx, str);
5041 upa = isl_union_pw_aff_aff_on_domain(domain, aff);
5042 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5043 ok = union_pw_aff_check_plain_equal(upa, str);
5044 isl_union_pw_aff_free(upa);
5045 if (ok < 0)
5046 return -1;
5048 id = isl_id_alloc(ctx, "N", NULL);
5049 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5050 domain = isl_union_set_read_from_str(ctx, str);
5051 upa = isl_union_pw_aff_param_on_domain_id(domain, id);
5052 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5053 ok = union_pw_aff_check_plain_equal(upa, str);
5054 isl_union_pw_aff_free(upa);
5055 if (ok < 0)
5056 return -1;
5058 return 0;
5061 struct {
5062 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5063 __isl_take isl_aff *aff2);
5064 } aff_bin_op[] = {
5065 ['+'] = { &isl_aff_add },
5066 ['-'] = { &isl_aff_sub },
5067 ['*'] = { &isl_aff_mul },
5068 ['/'] = { &isl_aff_div },
5071 struct {
5072 const char *arg1;
5073 unsigned char op;
5074 const char *arg2;
5075 const char *res;
5076 } aff_bin_tests[] = {
5077 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
5078 "{ [i] -> [2i] }" },
5079 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
5080 "{ [i] -> [0] }" },
5081 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
5082 "{ [i] -> [2i] }" },
5083 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
5084 "{ [i] -> [2i] }" },
5085 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
5086 "{ [i] -> [i/2] }" },
5087 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
5088 "{ [i] -> [i] }" },
5089 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
5090 "{ [i] -> [NaN] }" },
5091 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
5092 "{ [i] -> [NaN] }" },
5093 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
5094 "{ [i] -> [NaN] }" },
5095 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
5096 "{ [i] -> [NaN] }" },
5097 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
5098 "{ [i] -> [NaN] }" },
5099 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
5100 "{ [i] -> [NaN] }" },
5101 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
5102 "{ [i] -> [NaN] }" },
5103 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
5104 "{ [i] -> [NaN] }" },
5105 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
5106 "{ [i] -> [NaN] }" },
5107 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
5108 "{ [i] -> [NaN] }" },
5109 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
5110 "{ [i] -> [NaN] }" },
5111 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
5112 "{ [i] -> [NaN] }" },
5115 /* Perform some basic tests of binary operations on isl_aff objects.
5117 static int test_bin_aff(isl_ctx *ctx)
5119 int i;
5120 isl_aff *aff1, *aff2, *res;
5121 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5122 __isl_take isl_aff *aff2);
5123 int ok;
5125 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
5126 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
5127 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
5128 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
5129 fn = aff_bin_op[aff_bin_tests[i].op].fn;
5130 aff1 = fn(aff1, aff2);
5131 if (isl_aff_is_nan(res))
5132 ok = isl_aff_is_nan(aff1);
5133 else
5134 ok = isl_aff_plain_is_equal(aff1, res);
5135 isl_aff_free(aff1);
5136 isl_aff_free(res);
5137 if (ok < 0)
5138 return -1;
5139 if (!ok)
5140 isl_die(ctx, isl_error_unknown,
5141 "unexpected result", return -1);
5144 return 0;
5147 struct {
5148 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
5149 __isl_take isl_pw_aff *pa2);
5150 } pw_aff_bin_op[] = {
5151 ['m'] = { &isl_pw_aff_min },
5152 ['M'] = { &isl_pw_aff_max },
5155 /* Inputs for binary isl_pw_aff operation tests.
5156 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
5157 * defined by pw_aff_bin_op, and "res" is the expected result.
5159 struct {
5160 const char *arg1;
5161 unsigned char op;
5162 const char *arg2;
5163 const char *res;
5164 } pw_aff_bin_tests[] = {
5165 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
5166 "{ [i] -> [i] }" },
5167 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
5168 "{ [i] -> [i] }" },
5169 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
5170 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
5171 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
5172 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
5173 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
5174 "{ [i] -> [NaN] }" },
5175 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
5176 "{ [i] -> [NaN] }" },
5179 /* Perform some basic tests of binary operations on isl_pw_aff objects.
5181 static int test_bin_pw_aff(isl_ctx *ctx)
5183 int i;
5184 isl_bool ok;
5185 isl_pw_aff *pa1, *pa2, *res;
5187 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
5188 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
5189 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
5190 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
5191 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
5192 if (isl_pw_aff_involves_nan(res))
5193 ok = isl_pw_aff_involves_nan(pa1);
5194 else
5195 ok = isl_pw_aff_plain_is_equal(pa1, res);
5196 isl_pw_aff_free(pa1);
5197 isl_pw_aff_free(res);
5198 if (ok < 0)
5199 return -1;
5200 if (!ok)
5201 isl_die(ctx, isl_error_unknown,
5202 "unexpected result", return -1);
5205 return 0;
5208 struct {
5209 __isl_give isl_union_pw_multi_aff *(*fn)(
5210 __isl_take isl_union_pw_multi_aff *upma1,
5211 __isl_take isl_union_pw_multi_aff *upma2);
5212 const char *arg1;
5213 const char *arg2;
5214 const char *res;
5215 } upma_bin_tests[] = {
5216 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
5217 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
5218 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
5219 "{ B[x] -> [2] : x >= 0 }",
5220 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
5221 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
5222 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
5223 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
5224 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
5225 "D[i] -> B[2] : i >= 5 }" },
5226 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5227 "{ B[x] -> C[2] : x > 0 }",
5228 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
5229 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5230 "{ B[x] -> A[2] : x >= 0 }",
5231 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
5234 /* Perform some basic tests of binary operations on
5235 * isl_union_pw_multi_aff objects.
5237 static int test_bin_upma(isl_ctx *ctx)
5239 int i;
5240 isl_union_pw_multi_aff *upma1, *upma2, *res;
5241 int ok;
5243 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
5244 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5245 upma_bin_tests[i].arg1);
5246 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5247 upma_bin_tests[i].arg2);
5248 res = isl_union_pw_multi_aff_read_from_str(ctx,
5249 upma_bin_tests[i].res);
5250 upma1 = upma_bin_tests[i].fn(upma1, upma2);
5251 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
5252 isl_union_pw_multi_aff_free(upma1);
5253 isl_union_pw_multi_aff_free(res);
5254 if (ok < 0)
5255 return -1;
5256 if (!ok)
5257 isl_die(ctx, isl_error_unknown,
5258 "unexpected result", return -1);
5261 return 0;
5264 struct {
5265 __isl_give isl_union_pw_multi_aff *(*fn)(
5266 __isl_take isl_union_pw_multi_aff *upma1,
5267 __isl_take isl_union_pw_multi_aff *upma2);
5268 const char *arg1;
5269 const char *arg2;
5270 } upma_bin_fail_tests[] = {
5271 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5272 "{ B[x] -> C[2] : x >= 0 }" },
5275 /* Perform some basic tests of binary operations on
5276 * isl_union_pw_multi_aff objects that are expected to fail.
5278 static int test_bin_upma_fail(isl_ctx *ctx)
5280 int i, n;
5281 isl_union_pw_multi_aff *upma1, *upma2;
5282 int on_error;
5284 on_error = isl_options_get_on_error(ctx);
5285 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
5286 n = ARRAY_SIZE(upma_bin_fail_tests);
5287 for (i = 0; i < n; ++i) {
5288 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5289 upma_bin_fail_tests[i].arg1);
5290 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5291 upma_bin_fail_tests[i].arg2);
5292 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
5293 isl_union_pw_multi_aff_free(upma1);
5294 if (upma1)
5295 break;
5297 isl_options_set_on_error(ctx, on_error);
5298 if (i < n)
5299 isl_die(ctx, isl_error_unknown,
5300 "operation not expected to succeed", return -1);
5302 return 0;
5305 /* Inputs for basic tests of unary operations on isl_multi_pw_aff objects.
5306 * "fn" is the function that is tested.
5307 * "arg" is a string description of the input.
5308 * "res" is a string description of the expected result.
5310 struct {
5311 __isl_give isl_multi_pw_aff *(*fn)(__isl_take isl_multi_pw_aff *mpa);
5312 const char *arg;
5313 const char *res;
5314 } mpa_un_tests[] = {
5315 { &isl_multi_pw_aff_range_factor_domain,
5316 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5317 "{ A[x] -> B[(1 : x >= 5)] }" },
5318 { &isl_multi_pw_aff_range_factor_range,
5319 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5320 "{ A[y] -> C[(2 : y <= 10)] }" },
5321 { &isl_multi_pw_aff_range_factor_domain,
5322 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5323 "{ A[x] -> B[(1 : x >= 5)] }" },
5324 { &isl_multi_pw_aff_range_factor_range,
5325 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5326 "{ A[y] -> C[] }" },
5327 { &isl_multi_pw_aff_range_factor_domain,
5328 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5329 "{ A[x] -> B[] }" },
5330 { &isl_multi_pw_aff_range_factor_range,
5331 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5332 "{ A[y] -> C[(2 : y <= 10)] }" },
5333 { &isl_multi_pw_aff_range_factor_domain,
5334 "{ A[x] -> [B[] -> C[]] }",
5335 "{ A[x] -> B[] }" },
5336 { &isl_multi_pw_aff_range_factor_range,
5337 "{ A[x] -> [B[] -> C[]] }",
5338 "{ A[y] -> C[] }" },
5339 { &isl_multi_pw_aff_factor_range,
5340 "{ [B[] -> C[]] }",
5341 "{ C[] }" },
5342 { &isl_multi_pw_aff_range_factor_domain,
5343 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5344 "{ A[x] -> B[] : x >= 0 }" },
5345 { &isl_multi_pw_aff_range_factor_range,
5346 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5347 "{ A[y] -> C[] : y >= 0 }" },
5348 { &isl_multi_pw_aff_factor_range,
5349 "[N] -> { [B[] -> C[]] : N >= 0 }",
5350 "[N] -> { C[] : N >= 0 }" },
5353 /* Perform some basic tests of unary operations on isl_multi_pw_aff objects.
5355 static int test_un_mpa(isl_ctx *ctx)
5357 int i;
5358 isl_bool ok;
5359 isl_multi_pw_aff *mpa, *res;
5361 for (i = 0; i < ARRAY_SIZE(mpa_un_tests); ++i) {
5362 mpa = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].arg);
5363 res = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].res);
5364 mpa = mpa_un_tests[i].fn(mpa);
5365 ok = isl_multi_pw_aff_plain_is_equal(mpa, res);
5366 isl_multi_pw_aff_free(mpa);
5367 isl_multi_pw_aff_free(res);
5368 if (ok < 0)
5369 return -1;
5370 if (!ok)
5371 isl_die(ctx, isl_error_unknown,
5372 "unexpected result", return -1);
5375 return 0;
5378 /* Inputs for basic tests of binary operations on isl_multi_pw_aff objects.
5379 * "fn" is the function that is tested.
5380 * "arg1" and "arg2" are string descriptions of the inputs.
5381 * "res" is a string description of the expected result.
5383 struct {
5384 __isl_give isl_multi_pw_aff *(*fn)(
5385 __isl_take isl_multi_pw_aff *mpa1,
5386 __isl_take isl_multi_pw_aff *mpa2);
5387 const char *arg1;
5388 const char *arg2;
5389 const char *res;
5390 } mpa_bin_tests[] = {
5391 { &isl_multi_pw_aff_add, "{ A[] -> [1] }", "{ A[] -> [2] }",
5392 "{ A[] -> [3] }" },
5393 { &isl_multi_pw_aff_add, "{ A[x] -> [(1 : x >= 5)] }",
5394 "{ A[x] -> [(x : x <= 10)] }",
5395 "{ A[x] -> [(1 + x : 5 <= x <= 10)] }" },
5396 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5397 "{ A[x] -> [] : x <= 10 }",
5398 "{ A[x] -> [] : 5 <= x <= 10 }" },
5399 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5400 "[N] -> { A[x] -> [] : x <= N }",
5401 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5402 { &isl_multi_pw_aff_add,
5403 "[N] -> { A[x] -> [] : x <= N }",
5404 "{ A[x] -> [] : x >= 5 }",
5405 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5406 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5407 "{ A[y] -> C[(2 : y <= 10)] }",
5408 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5409 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5410 "{ A[y] -> C[2] : y <= 10 }",
5411 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5412 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5413 "[N] -> { A[y] -> C[2] : y <= N }",
5414 "[N] -> { A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= N)]] }" },
5415 { &isl_multi_pw_aff_range_product, "[N] -> { A[x] -> B[1] : x >= N }",
5416 "{ A[y] -> C[2] : y <= 10 }",
5417 "[N] -> { A[x] -> [B[(1 : x >= N)] -> C[(2 : x <= 10)]] }" },
5418 { &isl_multi_pw_aff_range_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5419 "{ A[] -> [B[1] -> C[2]] }" },
5420 { &isl_multi_pw_aff_range_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5421 "{ A[] -> [B[] -> C[]] }" },
5422 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5423 "{ A[y] -> C[] : y <= 10 }",
5424 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] : x <= 10 }" },
5425 { &isl_multi_pw_aff_range_product, "{ A[y] -> C[] : y <= 10 }",
5426 "{ A[x] -> B[(1 : x >= 5)] }",
5427 "{ A[x] -> [C[] -> B[(1 : x >= 5)]] : x <= 10 }" },
5428 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5429 "{ A[y] -> C[(2 : y <= 10)] }",
5430 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[(2 : y <= 10)]] }" },
5431 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5432 "{ A[y] -> C[] : y <= 10 }",
5433 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= 10 }" },
5434 { &isl_multi_pw_aff_product, "{ A[y] -> C[] : y <= 10 }",
5435 "{ A[x] -> B[(1 : x >= 5)] }",
5436 "{ [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= 10 }" },
5437 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5438 "[N] -> { A[y] -> C[] : y <= N }",
5439 "[N] -> { [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= N }" },
5440 { &isl_multi_pw_aff_product, "[N] -> { A[y] -> C[] : y <= N }",
5441 "{ A[x] -> B[(1 : x >= 5)] }",
5442 "[N] -> { [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= N }" },
5443 { &isl_multi_pw_aff_product, "{ A[x] -> B[] : x >= 5 }",
5444 "{ A[y] -> C[] : y <= 10 }",
5445 "{ [A[x] -> A[y]] -> [B[] -> C[]] : x >= 5 and y <= 10 }" },
5446 { &isl_multi_pw_aff_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5447 "{ [A[] -> A[]] -> [B[1] -> C[2]] }" },
5448 { &isl_multi_pw_aff_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5449 "{ [A[] -> A[]] -> [B[] -> C[]] }" },
5450 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5451 "{ B[i,j] -> C[i + 2j] }", "{ A[a,b] -> B[b,a] }",
5452 "{ A[a,b] -> C[b + 2a] }" },
5453 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5454 "{ B[i,j] -> C[i + 2j] }",
5455 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5456 "{ A[a,b] -> C[(b + 2a : b > a)] }" },
5457 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5458 "{ B[i,j] -> C[(i + 2j : j > 4)] }",
5459 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5460 "{ A[a,b] -> C[(b + 2a : b > a > 4)] }" },
5461 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5462 "{ B[i,j] -> C[] }",
5463 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5464 "{ A[a,b] -> C[] }" },
5465 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5466 "{ B[i,j] -> C[] : i > j }",
5467 "{ A[a,b] -> B[b,a] }",
5468 "{ A[a,b] -> C[] : b > a }" },
5469 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5470 "{ B[i,j] -> C[] : j > 5 }",
5471 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5472 "{ A[a,b] -> C[] : b > a > 5 }" },
5473 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5474 "[N] -> { B[i,j] -> C[] : j > N }",
5475 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5476 "[N] -> { A[a,b] -> C[] : b > a > N }" },
5477 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5478 "[M,N] -> { B[] -> C[] : N > 5 }",
5479 "[M,N] -> { A[] -> B[] : M > N }",
5480 "[M,N] -> { A[] -> C[] : M > N > 5 }" },
5483 /* Perform some basic tests of binary operations on isl_multi_pw_aff objects.
5485 static int test_bin_mpa(isl_ctx *ctx)
5487 int i;
5488 isl_bool ok;
5489 isl_multi_pw_aff *mpa1, *mpa2, *res;
5491 for (i = 0; i < ARRAY_SIZE(mpa_bin_tests); ++i) {
5492 mpa1 = isl_multi_pw_aff_read_from_str(ctx,
5493 mpa_bin_tests[i].arg1);
5494 mpa2 = isl_multi_pw_aff_read_from_str(ctx,
5495 mpa_bin_tests[i].arg2);
5496 res = isl_multi_pw_aff_read_from_str(ctx,
5497 mpa_bin_tests[i].res);
5498 mpa1 = mpa_bin_tests[i].fn(mpa1, mpa2);
5499 ok = isl_multi_pw_aff_plain_is_equal(mpa1, res);
5500 isl_multi_pw_aff_free(mpa1);
5501 isl_multi_pw_aff_free(res);
5502 if (ok < 0)
5503 return -1;
5504 if (!ok)
5505 isl_die(ctx, isl_error_unknown,
5506 "unexpected result", return -1);
5509 return 0;
5512 /* Inputs for basic tests of unary operations on
5513 * isl_multi_union_pw_aff objects.
5514 * "fn" is the function that is tested.
5515 * "arg" is a string description of the input.
5516 * "res" is a string description of the expected result.
5518 struct {
5519 __isl_give isl_multi_union_pw_aff *(*fn)(
5520 __isl_take isl_multi_union_pw_aff *mupa);
5521 const char *arg;
5522 const char *res;
5523 } mupa_un_tests[] = {
5524 { &isl_multi_union_pw_aff_factor_range,
5525 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]",
5526 "C[{ A[] -> [2] }]" },
5527 { &isl_multi_union_pw_aff_factor_range,
5528 "[B[] -> C[{ A[] -> [2] }]]",
5529 "C[{ A[] -> [2] }]" },
5530 { &isl_multi_union_pw_aff_factor_range,
5531 "[B[{ A[] -> [1] }] -> C[]]",
5532 "C[]" },
5533 { &isl_multi_union_pw_aff_factor_range,
5534 "[B[] -> C[]]",
5535 "C[]" },
5536 { &isl_multi_union_pw_aff_factor_range,
5537 "([B[] -> C[]] : { A[x] : x >= 0 })",
5538 "(C[] : { A[x] : x >= 0 })" },
5539 { &isl_multi_union_pw_aff_factor_range,
5540 "[N] -> ([B[] -> C[]] : { A[x] : x <= N })",
5541 "[N] -> (C[] : { A[x] : x <= N })" },
5542 { &isl_multi_union_pw_aff_factor_range,
5543 "[N] -> ([B[] -> C[]] : { : N >= 0 })",
5544 "[N] -> (C[] : { : N >= 0 })" },
5547 /* Perform some basic tests of unary operations on
5548 * isl_multi_union_pw_aff objects.
5550 static int test_un_mupa(isl_ctx *ctx)
5552 int i;
5553 isl_bool ok;
5554 isl_multi_union_pw_aff *mupa, *res;
5556 for (i = 0; i < ARRAY_SIZE(mupa_un_tests); ++i) {
5557 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5558 mupa_un_tests[i].arg);
5559 res = isl_multi_union_pw_aff_read_from_str(ctx,
5560 mupa_un_tests[i].res);
5561 mupa = mupa_un_tests[i].fn(mupa);
5562 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5563 isl_multi_union_pw_aff_free(mupa);
5564 isl_multi_union_pw_aff_free(res);
5565 if (ok < 0)
5566 return -1;
5567 if (!ok)
5568 isl_die(ctx, isl_error_unknown,
5569 "unexpected result", return -1);
5572 return 0;
5575 /* Inputs for basic tests of binary operations on
5576 * isl_multi_union_pw_aff objects.
5577 * "fn" is the function that is tested.
5578 * "arg1" and "arg2" are string descriptions of the inputs.
5579 * "res" is a string description of the expected result.
5581 struct {
5582 __isl_give isl_multi_union_pw_aff *(*fn)(
5583 __isl_take isl_multi_union_pw_aff *mupa1,
5584 __isl_take isl_multi_union_pw_aff *mupa2);
5585 const char *arg1;
5586 const char *arg2;
5587 const char *res;
5588 } mupa_bin_tests[] = {
5589 { &isl_multi_union_pw_aff_add, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5590 "[{ A[] -> [3] }]" },
5591 { &isl_multi_union_pw_aff_sub, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5592 "[{ A[] -> [-1] }]" },
5593 { &isl_multi_union_pw_aff_add,
5594 "[{ A[] -> [1]; B[] -> [4] }]",
5595 "[{ A[] -> [2]; C[] -> [5] }]",
5596 "[{ A[] -> [3] }]" },
5597 { &isl_multi_union_pw_aff_union_add,
5598 "[{ A[] -> [1]; B[] -> [4] }]",
5599 "[{ A[] -> [2]; C[] -> [5] }]",
5600 "[{ A[] -> [3]; B[] -> [4]; C[] -> [5] }]" },
5601 { &isl_multi_union_pw_aff_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5602 "[{ A[x] -> [(x)] : x <= 10 }]",
5603 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10 }]" },
5604 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5605 "([] : { A[x] : x <= 10 })",
5606 "([] : { A[x] : 5 <= x <= 10 })" },
5607 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5608 "[N] -> ([] : { A[x] : x <= N })",
5609 "[N] -> ([] : { A[x] : 5 <= x <= N })" },
5610 { &isl_multi_union_pw_aff_add, "[N] -> ([] : { A[x] : x >= N })",
5611 "([] : { A[x] : x <= 10 })",
5612 "[N] -> ([] : { A[x] : N <= x <= 10 })" },
5613 { &isl_multi_union_pw_aff_union_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5614 "[{ A[x] -> [(x)] : x <= 10 }]",
5615 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10; "
5616 "A[x] -> [(1)] : x > 10; A[x] -> [(x)] : x < 5 }]" },
5617 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 5 })",
5618 "([] : { A[x] : x <= 10 })",
5619 "([] : { A[x] })" },
5620 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 0 })",
5621 "[N] -> ([] : { A[x] : x >= N })",
5622 "[N] -> ([] : { A[x] : x >= 0 or x >= N })" },
5623 { &isl_multi_union_pw_aff_union_add,
5624 "[N] -> ([] : { A[] : N >= 0})",
5625 "[N] -> ([] : { A[] : N <= 0})",
5626 "[N] -> ([] : { A[] })" },
5627 { &isl_multi_union_pw_aff_union_add,
5628 "[N] -> ([] : { A[] })",
5629 "[N] -> ([] : { : })",
5630 "[N] -> ([] : { : })" },
5631 { &isl_multi_union_pw_aff_union_add,
5632 "[N] -> ([] : { : })",
5633 "[N] -> ([] : { A[] })",
5634 "[N] -> ([] : { : })" },
5635 { &isl_multi_union_pw_aff_union_add,
5636 "[N] -> ([] : { : N >= 0})",
5637 "[N] -> ([] : { : N <= 0})",
5638 "[N] -> ([] : { : })" },
5639 { &isl_multi_union_pw_aff_range_product,
5640 "B[{ A[] -> [1] }]",
5641 "C[{ A[] -> [2] }]",
5642 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]" },
5643 { &isl_multi_union_pw_aff_range_product,
5644 "(B[] : { A[x] : x >= 5 })",
5645 "(C[] : { A[x] : x <= 10 })",
5646 "([B[] -> C[]] : { A[x] : 5 <= x <= 10 })" },
5647 { &isl_multi_union_pw_aff_range_product,
5648 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5649 "(C[] : { A[x] : x <= 10 })",
5650 "[B[{ A[x] -> [x + 1] : 5 <= x <= 10 }] -> C[]]" },
5651 { &isl_multi_union_pw_aff_range_product,
5652 "(C[] : { A[x] : x <= 10 })",
5653 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5654 "[C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= 10 }]]" },
5655 { &isl_multi_union_pw_aff_range_product,
5656 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5657 "[N] -> (C[] : { A[x] : x <= N })",
5658 "[N] -> [B[{ A[x] -> [x + 1] : 5 <= x <= N }] -> C[]]" },
5659 { &isl_multi_union_pw_aff_range_product,
5660 "[N] -> (C[] : { A[x] : x <= N })",
5661 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5662 "[N] -> [C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= N }]]" },
5663 { &isl_multi_union_pw_aff_range_product,
5664 "B[{ A[] -> [1]; D[] -> [3] }]",
5665 "C[{ A[] -> [2] }]",
5666 "[B[{ A[] -> [1]; D[] -> [3] }] -> C[{ A[] -> [2] }]]" },
5667 { &isl_multi_union_pw_aff_range_product,
5668 "B[] }]",
5669 "(C[] : { A[x] })",
5670 "([B[] -> C[]] : { A[x] })" },
5671 { &isl_multi_union_pw_aff_range_product,
5672 "(B[] : { A[x] })",
5673 "C[] }]",
5674 "([B[] -> C[]] : { A[x] })" },
5677 /* Perform some basic tests of binary operations on
5678 * isl_multi_union_pw_aff objects.
5680 static int test_bin_mupa(isl_ctx *ctx)
5682 int i;
5683 isl_bool ok;
5684 isl_multi_union_pw_aff *mupa1, *mupa2, *res;
5686 for (i = 0; i < ARRAY_SIZE(mupa_bin_tests); ++i) {
5687 mupa1 = isl_multi_union_pw_aff_read_from_str(ctx,
5688 mupa_bin_tests[i].arg1);
5689 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx,
5690 mupa_bin_tests[i].arg2);
5691 res = isl_multi_union_pw_aff_read_from_str(ctx,
5692 mupa_bin_tests[i].res);
5693 mupa1 = mupa_bin_tests[i].fn(mupa1, mupa2);
5694 ok = isl_multi_union_pw_aff_plain_is_equal(mupa1, res);
5695 isl_multi_union_pw_aff_free(mupa1);
5696 isl_multi_union_pw_aff_free(res);
5697 if (ok < 0)
5698 return -1;
5699 if (!ok)
5700 isl_die(ctx, isl_error_unknown,
5701 "unexpected result", return -1);
5704 return 0;
5707 /* Inputs for basic tests of binary operations on
5708 * pairs of isl_multi_union_pw_aff and isl_set objects.
5709 * "fn" is the function that is tested.
5710 * "arg1" and "arg2" are string descriptions of the inputs.
5711 * "res" is a string description of the expected result.
5713 struct {
5714 __isl_give isl_multi_union_pw_aff *(*fn)(
5715 __isl_take isl_multi_union_pw_aff *mupa,
5716 __isl_take isl_set *set);
5717 const char *arg1;
5718 const char *arg2;
5719 const char *res;
5720 } mupa_set_tests[] = {
5721 { &isl_multi_union_pw_aff_intersect_range,
5722 "C[{ B[i,j] -> [i + 2j] }]", "{ C[1] }",
5723 "C[{ B[i,j] -> [i + 2j] : i + 2j = 1 }]" },
5724 { &isl_multi_union_pw_aff_intersect_range,
5725 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[N] }",
5726 "[N] -> C[{ B[i,j] -> [i + 2j] : i + 2j = N }]" },
5727 { &isl_multi_union_pw_aff_intersect_range,
5728 "[N] -> C[{ B[i,j] -> [i + 2j + N] }]", "{ C[1] }",
5729 "[N] -> C[{ B[i,j] -> [i + 2j + N] : i + 2j + N = 1 }]" },
5730 { &isl_multi_union_pw_aff_intersect_range,
5731 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[x] : N >= 0 }",
5732 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0 }]" },
5733 { &isl_multi_union_pw_aff_intersect_range,
5734 "C[]", "{ C[] }", "C[]" },
5735 { &isl_multi_union_pw_aff_intersect_range,
5736 "[N] -> (C[] : { : N >= 0 })",
5737 "{ C[] }",
5738 "[N] -> (C[] : { : N >= 0 })" },
5739 { &isl_multi_union_pw_aff_intersect_range,
5740 "(C[] : { A[a,b] })",
5741 "{ C[] }",
5742 "(C[] : { A[a,b] })" },
5743 { &isl_multi_union_pw_aff_intersect_range,
5744 "[N] -> (C[] : { A[a,b] : a,b <= N })",
5745 "{ C[] }",
5746 "[N] -> (C[] : { A[a,b] : a,b <= N })" },
5747 { &isl_multi_union_pw_aff_intersect_range,
5748 "C[]",
5749 "[N] -> { C[] : N >= 0 }",
5750 "[N] -> (C[] : { : N >= 0 })" },
5751 { &isl_multi_union_pw_aff_intersect_range,
5752 "(C[] : { A[a,b] })",
5753 "[N] -> { C[] : N >= 0 }",
5754 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5755 { &isl_multi_union_pw_aff_intersect_range,
5756 "[N] -> (C[] : { : N >= 0 })",
5757 "[N] -> { C[] : N < 1024 }",
5758 "[N] -> (C[] : { : 0 <= N < 1024 })" },
5759 { &isl_multi_union_pw_aff_intersect_params,
5760 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { : N >= 0 }",
5761 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0}]" },
5762 { &isl_multi_union_pw_aff_intersect_params,
5763 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "[N] -> { : N >= 0 }",
5764 "[N] -> C[{ B[i,j] -> [i + 2j] : 0 <= N <= 256 }]" },
5765 { &isl_multi_union_pw_aff_intersect_params,
5766 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "{ : }",
5767 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]" },
5768 { &isl_multi_union_pw_aff_intersect_params,
5769 "C[]", "[N] -> { : N >= 0 }",
5770 "[N] -> (C[] : { : N >= 0 })" },
5771 { &isl_multi_union_pw_aff_intersect_params,
5772 "(C[] : { A[a,b] })", "[N] -> { : N >= 0 }",
5773 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5774 { &isl_multi_union_pw_aff_intersect_params,
5775 "[N] -> (C[] : { A[a,N] })", "{ : }",
5776 "[N] -> (C[] : { A[a,N] })" },
5777 { &isl_multi_union_pw_aff_intersect_params,
5778 "[N] -> (C[] : { A[a,b] : N <= 256 })", "[N] -> { : N >= 0 }",
5779 "[N] -> (C[] : { A[a,b] : 0 <= N <= 256 })" },
5782 /* Perform some basic tests of binary operations on
5783 * pairs of isl_multi_union_pw_aff and isl_set objects.
5785 static int test_mupa_set(isl_ctx *ctx)
5787 int i;
5788 isl_bool ok;
5789 isl_multi_union_pw_aff *mupa, *res;
5790 isl_set *set;
5792 for (i = 0; i < ARRAY_SIZE(mupa_set_tests); ++i) {
5793 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5794 mupa_set_tests[i].arg1);
5795 set = isl_set_read_from_str(ctx, mupa_set_tests[i].arg2);
5796 res = isl_multi_union_pw_aff_read_from_str(ctx,
5797 mupa_set_tests[i].res);
5798 mupa = mupa_set_tests[i].fn(mupa, set);
5799 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5800 isl_multi_union_pw_aff_free(mupa);
5801 isl_multi_union_pw_aff_free(res);
5802 if (ok < 0)
5803 return -1;
5804 if (!ok)
5805 isl_die(ctx, isl_error_unknown,
5806 "unexpected result", return -1);
5809 return 0;
5812 /* Inputs for basic tests of binary operations on
5813 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5814 * "fn" is the function that is tested.
5815 * "arg1" and "arg2" are string descriptions of the inputs.
5816 * "res" is a string description of the expected result.
5818 struct {
5819 __isl_give isl_multi_union_pw_aff *(*fn)(
5820 __isl_take isl_multi_union_pw_aff *mupa,
5821 __isl_take isl_union_set *uset);
5822 const char *arg1;
5823 const char *arg2;
5824 const char *res;
5825 } mupa_uset_tests[] = {
5826 { &isl_multi_union_pw_aff_intersect_domain,
5827 "C[{ B[i,j] -> [i + 2j] }]", "{ B[i,i] }",
5828 "C[{ B[i,i] -> [3i] }]" },
5829 { &isl_multi_union_pw_aff_intersect_domain,
5830 "(C[] : { B[i,j] })", "{ B[i,i] }",
5831 "(C[] : { B[i,i] })" },
5832 { &isl_multi_union_pw_aff_intersect_domain,
5833 "(C[] : { B[i,j] })", "[N] -> { B[N,N] }",
5834 "[N] -> (C[] : { B[N,N] })" },
5835 { &isl_multi_union_pw_aff_intersect_domain,
5836 "C[]", "{ B[i,i] }",
5837 "(C[] : { B[i,i] })" },
5838 { &isl_multi_union_pw_aff_intersect_domain,
5839 "[N] -> (C[] : { : N >= 0 })", "{ B[i,i] }",
5840 "[N] -> (C[] : { B[i,i] : N >= 0 })" },
5843 /* Perform some basic tests of binary operations on
5844 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5846 static int test_mupa_uset(isl_ctx *ctx)
5848 int i;
5849 isl_bool ok;
5850 isl_multi_union_pw_aff *mupa, *res;
5851 isl_union_set *uset;
5853 for (i = 0; i < ARRAY_SIZE(mupa_uset_tests); ++i) {
5854 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5855 mupa_uset_tests[i].arg1);
5856 uset = isl_union_set_read_from_str(ctx,
5857 mupa_uset_tests[i].arg2);
5858 res = isl_multi_union_pw_aff_read_from_str(ctx,
5859 mupa_uset_tests[i].res);
5860 mupa = mupa_uset_tests[i].fn(mupa, uset);
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_multi_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_multi_union_pw_aff *(*fn)(
5882 __isl_take isl_multi_union_pw_aff *mupa,
5883 __isl_take isl_multi_aff *ma);
5884 const char *arg1;
5885 const char *arg2;
5886 const char *res;
5887 } mupa_ma_tests[] = {
5888 { &isl_multi_union_pw_aff_apply_multi_aff,
5889 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5890 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5891 "{ C[a,b] -> D[b,a] }",
5892 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
5893 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
5894 { &isl_multi_union_pw_aff_apply_multi_aff,
5895 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
5896 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5897 "{ C[a,b] -> D[b,a] }",
5898 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
5899 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
5900 { &isl_multi_union_pw_aff_apply_multi_aff,
5901 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5902 "[N] -> { C[a] -> D[a + N] }",
5903 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }] " },
5904 { &isl_multi_union_pw_aff_apply_multi_aff,
5905 "C[]",
5906 "{ C[] -> D[] }",
5907 "D[]" },
5908 { &isl_multi_union_pw_aff_apply_multi_aff,
5909 "[N] -> (C[] : { : N >= 0 })",
5910 "{ C[] -> D[] }",
5911 "[N] -> (D[] : { : N >= 0 })" },
5912 { &isl_multi_union_pw_aff_apply_multi_aff,
5913 "C[]",
5914 "[N] -> { C[] -> D[N] }",
5915 "[N] -> D[{ [N] }]" },
5916 { &isl_multi_union_pw_aff_apply_multi_aff,
5917 "(C[] : { A[i,j] : i >= j })",
5918 "{ C[] -> D[] }",
5919 "(D[] : { A[i,j] : i >= j })" },
5920 { &isl_multi_union_pw_aff_apply_multi_aff,
5921 "[N] -> (C[] : { A[i,j] : N >= 0 })",
5922 "{ C[] -> D[] }",
5923 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
5924 { &isl_multi_union_pw_aff_apply_multi_aff,
5925 "(C[] : { A[i,j] : i >= j })",
5926 "[N] -> { C[] -> D[N] }",
5927 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
5930 /* Perform some basic tests of binary operations on
5931 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5933 static int test_mupa_ma(isl_ctx *ctx)
5935 int i;
5936 isl_bool ok;
5937 isl_multi_union_pw_aff *mupa, *res;
5938 isl_multi_aff *ma;
5940 for (i = 0; i < ARRAY_SIZE(mupa_ma_tests); ++i) {
5941 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5942 mupa_ma_tests[i].arg1);
5943 ma = isl_multi_aff_read_from_str(ctx, mupa_ma_tests[i].arg2);
5944 res = isl_multi_union_pw_aff_read_from_str(ctx,
5945 mupa_ma_tests[i].res);
5946 mupa = mupa_ma_tests[i].fn(mupa, ma);
5947 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5948 isl_multi_union_pw_aff_free(mupa);
5949 isl_multi_union_pw_aff_free(res);
5950 if (ok < 0)
5951 return -1;
5952 if (!ok)
5953 isl_die(ctx, isl_error_unknown,
5954 "unexpected result", return -1);
5957 return 0;
5960 /* Inputs for basic tests of binary operations on
5961 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5962 * "fn" is the function that is tested.
5963 * "arg1" and "arg2" are string descriptions of the inputs.
5964 * "res" is a string description of the expected result.
5966 struct {
5967 __isl_give isl_union_pw_aff *(*fn)(
5968 __isl_take isl_multi_union_pw_aff *mupa,
5969 __isl_take isl_pw_aff *pa);
5970 const char *arg1;
5971 const char *arg2;
5972 const char *res;
5973 } mupa_pa_tests[] = {
5974 { &isl_multi_union_pw_aff_apply_pw_aff,
5975 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5976 "[N] -> { C[a] -> [a + N] }",
5977 "[N] -> { A[i,j] -> [i + N]; B[i,j] -> [j + N] }" },
5978 { &isl_multi_union_pw_aff_apply_pw_aff,
5979 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5980 "{ C[a] -> [a] : a >= 0; C[a] -> [-a] : a < 0 }",
5981 "{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
5982 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }" },
5983 { &isl_multi_union_pw_aff_apply_pw_aff,
5984 "C[]",
5985 "[N] -> { C[] -> [N] }",
5986 "[N] -> { [N] }" },
5987 { &isl_multi_union_pw_aff_apply_pw_aff,
5988 "C[]",
5989 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
5990 "[N] -> { [N] : N >= 0; [-N] : N < 0 }" },
5991 { &isl_multi_union_pw_aff_apply_pw_aff,
5992 "[N] -> (C[] : { : N >= 0 })",
5993 "[N] -> { C[] -> [N] }",
5994 "[N] -> { [N] : N >= 0 }" },
5995 { &isl_multi_union_pw_aff_apply_pw_aff,
5996 "[N] -> (C[] : { : N >= 0 })",
5997 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
5998 "[N] -> { [N] : N >= 0 }" },
5999 { &isl_multi_union_pw_aff_apply_pw_aff,
6000 "[N] -> (C[] : { : N >= 0 })",
6001 "{ C[] -> [0] }",
6002 "[N] -> { [0] : N >= 0 }" },
6003 { &isl_multi_union_pw_aff_apply_pw_aff,
6004 "(C[] : { A[i,j] : i >= j })",
6005 "[N] -> { C[] -> [N] }",
6006 "[N] -> { A[i,j] -> [N] : i >= j }" },
6007 { &isl_multi_union_pw_aff_apply_pw_aff,
6008 "(C[] : { A[i,j] : i >= j })",
6009 "[N] -> { C[] -> [N] : N >= 0 }",
6010 "[N] -> { A[i,j] -> [N] : i >= j and N >= 0 }" },
6013 /* Perform some basic tests of binary operations on
6014 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
6016 static int test_mupa_pa(isl_ctx *ctx)
6018 int i;
6019 isl_bool ok;
6020 isl_multi_union_pw_aff *mupa;
6021 isl_union_pw_aff *upa, *res;
6022 isl_pw_aff *pa;
6024 for (i = 0; i < ARRAY_SIZE(mupa_pa_tests); ++i) {
6025 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6026 mupa_pa_tests[i].arg1);
6027 pa = isl_pw_aff_read_from_str(ctx, mupa_pa_tests[i].arg2);
6028 res = isl_union_pw_aff_read_from_str(ctx,
6029 mupa_pa_tests[i].res);
6030 upa = mupa_pa_tests[i].fn(mupa, pa);
6031 ok = isl_union_pw_aff_plain_is_equal(upa, res);
6032 isl_union_pw_aff_free(upa);
6033 isl_union_pw_aff_free(res);
6034 if (ok < 0)
6035 return -1;
6036 if (!ok)
6037 isl_die(ctx, isl_error_unknown,
6038 "unexpected result", return -1);
6041 return 0;
6044 /* Inputs for basic tests of binary operations on
6045 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6046 * "fn" is the function that is tested.
6047 * "arg1" and "arg2" are string descriptions of the inputs.
6048 * "res" is a string description of the expected result.
6050 struct {
6051 __isl_give isl_multi_union_pw_aff *(*fn)(
6052 __isl_take isl_multi_union_pw_aff *mupa,
6053 __isl_take isl_pw_multi_aff *pma);
6054 const char *arg1;
6055 const char *arg2;
6056 const char *res;
6057 } mupa_pma_tests[] = {
6058 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6059 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6060 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6061 "{ C[a,b] -> D[b,a] }",
6062 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
6063 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
6064 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6065 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
6066 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6067 "{ C[a,b] -> D[b,a] }",
6068 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
6069 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
6070 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6071 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6072 "[N] -> { C[a] -> D[a + N] }",
6073 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }]" },
6074 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6075 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6076 "{ C[a] -> D[a] : a >= 0; C[a] -> D[-a] : a < 0 }",
6077 "D[{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
6078 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }]" },
6079 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6080 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6081 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6082 "{ C[a,b] -> D[a,b] : a >= b; C[a,b] -> D[b,a] : a < b }",
6083 "D[{ A[i,j] -> [i] : i >= j; A[i,j] -> [j] : i < j; "
6084 "B[i,j] -> [j] : i <= j; B[i,j] -> [i] : i > j }, "
6085 "{ A[i,j] -> [j] : i >= j; A[i,j] -> [i] : i < j; "
6086 "B[i,j] -> [i] : i <= j; B[i,j] -> [j] : i > j }]" },
6087 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6088 "C[]",
6089 "{ C[] -> D[] }",
6090 "D[]" },
6091 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6092 "[N] -> (C[] : { : N >= 0 })",
6093 "{ C[] -> D[] }",
6094 "[N] -> (D[] : { : N >= 0 })" },
6095 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6096 "C[]",
6097 "[N] -> { C[] -> D[N] }",
6098 "[N] -> D[{ [N] }]" },
6099 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6100 "(C[] : { A[i,j] : i >= j })",
6101 "{ C[] -> D[] }",
6102 "(D[] : { A[i,j] : i >= j })" },
6103 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6104 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6105 "{ C[] -> D[] }",
6106 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6107 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6108 "(C[] : { A[i,j] : i >= j })",
6109 "[N] -> { C[] -> D[N] }",
6110 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6111 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6112 "C[]",
6113 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6114 "[N] -> D[{ [N] : N >= 0; [-N] : N < 0 }]" },
6115 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6116 "[N] -> (C[] : { : N >= 0 })",
6117 "[N] -> { C[] -> D[N] }",
6118 "[N] -> D[{ [N] : N >= 0 }]" },
6119 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6120 "[N] -> (C[] : { : N >= 0 })",
6121 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6122 "[N] -> D[{ [N] : N >= 0 }]" },
6123 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6124 "[N] -> (C[] : { : N >= 0 })",
6125 "{ C[] -> D[0] }",
6126 "[N] -> D[{ [0] : N >= 0 }]" },
6127 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6128 "(C[] : { A[i,j] : i >= j })",
6129 "[N] -> { C[] -> D[N] : N >= 0 }",
6130 "[N] -> D[{ A[i,j] -> [N] : i >= j and N >= 0 }]" },
6133 /* Perform some basic tests of binary operations on
6134 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6136 static int test_mupa_pma(isl_ctx *ctx)
6138 int i;
6139 isl_bool ok;
6140 isl_multi_union_pw_aff *mupa, *res;
6141 isl_pw_multi_aff *pma;
6143 for (i = 0; i < ARRAY_SIZE(mupa_pma_tests); ++i) {
6144 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6145 mupa_pma_tests[i].arg1);
6146 pma = isl_pw_multi_aff_read_from_str(ctx,
6147 mupa_pma_tests[i].arg2);
6148 res = isl_multi_union_pw_aff_read_from_str(ctx,
6149 mupa_pma_tests[i].res);
6150 mupa = mupa_pma_tests[i].fn(mupa, pma);
6151 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6152 isl_multi_union_pw_aff_free(mupa);
6153 isl_multi_union_pw_aff_free(res);
6154 if (ok < 0)
6155 return -1;
6156 if (!ok)
6157 isl_die(ctx, isl_error_unknown,
6158 "unexpected result", return -1);
6161 return 0;
6164 /* Inputs for basic tests of binary operations on
6165 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6166 * "fn" is the function that is tested.
6167 * "arg1" and "arg2" are string descriptions of the inputs.
6168 * "res" is a string description of the expected result.
6170 struct {
6171 __isl_give isl_multi_union_pw_aff *(*fn)(
6172 __isl_take isl_multi_union_pw_aff *mupa,
6173 __isl_take isl_union_pw_multi_aff *upma);
6174 const char *arg1;
6175 const char *arg2;
6176 const char *res;
6177 } mupa_upma_tests[] = {
6178 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6179 "C[{ B[i,j] -> [i + 2j] }]", "{ A[a,b] -> B[b,a] }",
6180 "C[{ A[a,b] -> [b + 2a] }]" },
6181 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6182 "C[{ B[i,j] -> [i + 2j] }]",
6183 "{ A[a,b] -> B[b,a] : b > a }",
6184 "C[{ A[a,b] -> [b + 2a] : b > a }]" },
6185 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6186 "C[{ B[i,j] -> [i + 2j] : j > 4 }]",
6187 "{ A[a,b] -> B[b,a] : b > a }",
6188 "C[{ A[a,b] -> [b + 2a] : b > a > 4 }]" },
6189 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6190 "C[{ B[i,j] -> [i + 2j] }]",
6191 "{ A[a,b] -> B[b,a] : a > b; A[a,b] -> B[a,b] : a <= b }",
6192 "C[{ A[a,b] -> [b + 2a] : a > b; A[a,b] -> [a + 2b] : a <= b }]" },
6193 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6194 "(C[] : { B[a,b] })",
6195 "{ A[a,b] -> B[b,a] }",
6196 "(C[] : { A[a,b] })" },
6197 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6198 "(C[] : { B[a,b] })",
6199 "{ B[a,b] -> A[b,a] }",
6200 "(C[] : { })" },
6201 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6202 "(C[] : { B[a,b] })",
6203 "{ A[a,b] -> B[b,a] : a > b }",
6204 "(C[] : { A[a,b] : a > b })" },
6205 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6206 "(C[] : { B[a,b] : a > b })",
6207 "{ A[a,b] -> B[b,a] }",
6208 "(C[] : { A[a,b] : b > a })" },
6209 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6210 "[N] -> (C[] : { B[a,b] : a > N })",
6211 "{ A[a,b] -> B[b,a] : a > b }",
6212 "[N] -> (C[] : { A[a,b] : a > b > N })" },
6213 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6214 "(C[] : { B[a,b] : a > b })",
6215 "[N] -> { A[a,b] -> B[b,a] : a > N }",
6216 "[N] -> (C[] : { A[a,b] : b > a > N })" },
6217 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6218 "C[]",
6219 "{ A[a,b] -> B[b,a] }",
6220 "C[]" },
6221 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6222 "[N] -> (C[] : { : N >= 0 })",
6223 "{ A[a,b] -> B[b,a] }",
6224 "[N] -> (C[] : { : N >= 0 })" },
6225 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6226 "C[]",
6227 "[N] -> { A[a,b] -> B[b,a] : N >= 0 }",
6228 "[N] -> (C[] : { : N >= 0 })" },
6231 /* Perform some basic tests of binary operations on
6232 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6234 static int test_mupa_upma(isl_ctx *ctx)
6236 int i;
6237 isl_bool ok;
6238 isl_multi_union_pw_aff *mupa, *res;
6239 isl_union_pw_multi_aff *upma;
6241 for (i = 0; i < ARRAY_SIZE(mupa_upma_tests); ++i) {
6242 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6243 mupa_upma_tests[i].arg1);
6244 upma = isl_union_pw_multi_aff_read_from_str(ctx,
6245 mupa_upma_tests[i].arg2);
6246 res = isl_multi_union_pw_aff_read_from_str(ctx,
6247 mupa_upma_tests[i].res);
6248 mupa = mupa_upma_tests[i].fn(mupa, upma);
6249 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6250 isl_multi_union_pw_aff_free(mupa);
6251 isl_multi_union_pw_aff_free(res);
6252 if (ok < 0)
6253 return -1;
6254 if (!ok)
6255 isl_die(ctx, isl_error_unknown,
6256 "unexpected result", return -1);
6259 return 0;
6262 /* Check that the input tuple of an isl_aff can be set properly.
6264 static isl_stat test_aff_set_tuple_id(isl_ctx *ctx)
6266 isl_id *id;
6267 isl_aff *aff;
6268 int equal;
6270 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x + 1] }");
6271 id = isl_id_alloc(ctx, "A", NULL);
6272 aff = isl_aff_set_tuple_id(aff, isl_dim_in, id);
6273 equal = aff_check_plain_equal(aff, "{ A[x] -> [x + 1] }");
6274 isl_aff_free(aff);
6275 if (equal < 0)
6276 return isl_stat_error;
6278 return isl_stat_ok;
6281 int test_aff(isl_ctx *ctx)
6283 const char *str;
6284 isl_set *set;
6285 isl_space *space;
6286 isl_local_space *ls;
6287 isl_aff *aff;
6288 int zero, equal;
6290 if (test_upa(ctx) < 0)
6291 return -1;
6292 if (test_bin_aff(ctx) < 0)
6293 return -1;
6294 if (test_bin_pw_aff(ctx) < 0)
6295 return -1;
6296 if (test_bin_upma(ctx) < 0)
6297 return -1;
6298 if (test_bin_upma_fail(ctx) < 0)
6299 return -1;
6300 if (test_un_mpa(ctx) < 0)
6301 return -1;
6302 if (test_bin_mpa(ctx) < 0)
6303 return -1;
6304 if (test_un_mupa(ctx) < 0)
6305 return -1;
6306 if (test_bin_mupa(ctx) < 0)
6307 return -1;
6308 if (test_mupa_set(ctx) < 0)
6309 return -1;
6310 if (test_mupa_uset(ctx) < 0)
6311 return -1;
6312 if (test_mupa_ma(ctx) < 0)
6313 return -1;
6314 if (test_mupa_pa(ctx) < 0)
6315 return -1;
6316 if (test_mupa_pma(ctx) < 0)
6317 return -1;
6318 if (test_mupa_upma(ctx) < 0)
6319 return -1;
6321 space = isl_space_set_alloc(ctx, 0, 1);
6322 ls = isl_local_space_from_space(space);
6323 aff = isl_aff_zero_on_domain(ls);
6325 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6326 aff = isl_aff_scale_down_ui(aff, 3);
6327 aff = isl_aff_floor(aff);
6328 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6329 aff = isl_aff_scale_down_ui(aff, 2);
6330 aff = isl_aff_floor(aff);
6331 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6333 str = "{ [10] }";
6334 set = isl_set_read_from_str(ctx, str);
6335 aff = isl_aff_gist(aff, set);
6337 aff = isl_aff_add_constant_si(aff, -16);
6338 zero = isl_aff_plain_is_zero(aff);
6339 isl_aff_free(aff);
6341 if (zero < 0)
6342 return -1;
6343 if (!zero)
6344 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6346 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
6347 aff = isl_aff_scale_down_ui(aff, 64);
6348 aff = isl_aff_floor(aff);
6349 equal = aff_check_plain_equal(aff, "{ [-1] }");
6350 isl_aff_free(aff);
6351 if (equal < 0)
6352 return -1;
6354 if (test_aff_set_tuple_id(ctx) < 0)
6355 return -1;
6357 return 0;
6360 /* Inputs for isl_set_unbind_params tests.
6361 * "set" is the input parameter domain.
6362 * "tuple" is the tuple of the constructed set.
6363 * "res" is the expected result.
6365 struct {
6366 const char *set;
6367 const char *tuple;
6368 const char *res;
6369 } unbind_set_tests[] = {
6370 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
6371 "{ A[M, N] }",
6372 "{ A[M, N] : M mod 2 = 0 and N mod 8 = 3 }" },
6373 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
6374 "{ B[N, M] }",
6375 "{ B[N, M] : M mod 2 = 0 and N mod 8 = 3 }" },
6376 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
6377 "{ C[N] }",
6378 "[M] -> { C[N] : M mod 2 = 0 and N mod 8 = 3 }" },
6379 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
6380 "{ D[T, N] }",
6381 "[M] -> { D[x, N] : M mod 2 = 0 and N mod 8 = 3 }" },
6384 /* Perform basic isl_set_unbind_params tests.
6386 static isl_stat test_unbind_set(isl_ctx *ctx)
6388 int i;
6390 for (i = 0; i < ARRAY_SIZE(unbind_set_tests); ++i) {
6391 const char *str;
6392 isl_set *set;
6393 isl_multi_id *tuple;
6394 isl_stat r;
6396 set = isl_set_read_from_str(ctx, unbind_set_tests[i].set);
6397 str = unbind_set_tests[i].tuple;
6398 tuple = isl_multi_id_read_from_str(ctx, str);
6399 set = isl_set_unbind_params(set, tuple);
6400 r = set_check_equal(set, unbind_set_tests[i].res);
6401 isl_set_free(set);
6402 if (r < 0)
6403 return isl_stat_error;
6406 return isl_stat_ok;
6409 /* Inputs for isl_aff_unbind_params_insert_domain tests.
6410 * "aff" is the input affine expression defined over a parameter domain.
6411 * "tuple" is the tuple of the domain that gets introduced.
6412 * "res" is the expected result.
6414 struct {
6415 const char *aff;
6416 const char *tuple;
6417 const char *res;
6418 } unbind_aff_tests[] = {
6419 { "[M, N] -> { [M + floor(N/2)] }",
6420 "{ A[M, N] }",
6421 "{ A[M, N] -> [M + floor(N/2)] }" },
6422 { "[M, N] -> { [M + floor(N/2)] }",
6423 "{ B[N, M] }",
6424 "{ B[N, M] -> [M + floor(N/2)] }" },
6425 { "[M, N] -> { [M + floor(N/2)] }",
6426 "{ C[N] }",
6427 "[M] -> { C[N] -> [M + floor(N/2)] }" },
6428 { "[M, N] -> { [M + floor(N/2)] }",
6429 "{ D[A, B, C, N, Z] }",
6430 "[M] -> { D[A, B, C, N, Z] -> [M + floor(N/2)] }" },
6433 /* Perform basic isl_aff_unbind_params_insert_domain tests.
6435 static isl_stat test_unbind_aff(isl_ctx *ctx)
6437 int i;
6439 for (i = 0; i < ARRAY_SIZE(unbind_aff_tests); ++i) {
6440 const char *str;
6441 isl_aff *aff;
6442 isl_multi_id *tuple;
6443 isl_stat r;
6445 aff = isl_aff_read_from_str(ctx, unbind_aff_tests[i].aff);
6446 str = unbind_aff_tests[i].tuple;
6447 tuple = isl_multi_id_read_from_str(ctx, str);
6448 aff = isl_aff_unbind_params_insert_domain(aff, tuple);
6449 r = aff_check_plain_equal(aff, unbind_aff_tests[i].res);
6450 isl_aff_free(aff);
6451 if (r < 0)
6452 return isl_stat_error;
6455 return isl_stat_ok;
6458 /* Perform tests that reinterpret parameters.
6460 static int test_unbind(isl_ctx *ctx)
6462 if (test_unbind_set(ctx) < 0)
6463 return -1;
6464 if (test_unbind_aff(ctx) < 0)
6465 return -1;
6467 return 0;
6470 /* Check that "pa" consists of a single expression.
6472 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
6474 isl_size n;
6476 n = isl_pw_aff_n_piece(pa);
6477 isl_pw_aff_free(pa);
6479 if (n < 0)
6480 return -1;
6481 if (n != 1)
6482 isl_die(ctx, isl_error_unknown, "expecting single expression",
6483 return -1);
6485 return 0;
6488 /* Check that the computation below results in a single expression.
6489 * One or two expressions may result depending on which constraint
6490 * ends up being considered as redundant with respect to the other
6491 * constraints after the projection that is performed internally
6492 * by isl_set_dim_min.
6494 static int test_dim_max_1(isl_ctx *ctx)
6496 const char *str;
6497 isl_set *set;
6498 isl_pw_aff *pa;
6500 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
6501 "-4a <= b <= 3 and b < n - 4a }";
6502 set = isl_set_read_from_str(ctx, str);
6503 pa = isl_set_dim_min(set, 0);
6504 return check_single_piece(ctx, pa);
6507 /* Check that the computation below results in a single expression.
6508 * The PIP problem corresponding to these constraints has a row
6509 * that causes a split of the solution domain. The solver should
6510 * first pick rows that split off empty parts such that the actual
6511 * solution domain does not get split.
6512 * Note that the description contains some redundant constraints.
6513 * If these constraints get removed first, then the row mentioned
6514 * above does not appear in the PIP problem.
6516 static int test_dim_max_2(isl_ctx *ctx)
6518 const char *str;
6519 isl_set *set;
6520 isl_pw_aff *pa;
6522 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
6523 "N > 0 and P >= 0 }";
6524 set = isl_set_read_from_str(ctx, str);
6525 pa = isl_set_dim_max(set, 0);
6526 return check_single_piece(ctx, pa);
6529 int test_dim_max(isl_ctx *ctx)
6531 int equal;
6532 const char *str;
6533 isl_set *set1, *set2;
6534 isl_set *set;
6535 isl_map *map;
6536 isl_pw_aff *pwaff;
6538 if (test_dim_max_1(ctx) < 0)
6539 return -1;
6540 if (test_dim_max_2(ctx) < 0)
6541 return -1;
6543 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
6544 set = isl_set_read_from_str(ctx, str);
6545 pwaff = isl_set_dim_max(set, 0);
6546 set1 = isl_set_from_pw_aff(pwaff);
6547 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
6548 set2 = isl_set_read_from_str(ctx, str);
6549 equal = isl_set_is_equal(set1, set2);
6550 isl_set_free(set1);
6551 isl_set_free(set2);
6552 if (equal < 0)
6553 return -1;
6554 if (!equal)
6555 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6557 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
6558 set = isl_set_read_from_str(ctx, str);
6559 pwaff = isl_set_dim_max(set, 0);
6560 set1 = isl_set_from_pw_aff(pwaff);
6561 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6562 set2 = isl_set_read_from_str(ctx, str);
6563 equal = isl_set_is_equal(set1, set2);
6564 isl_set_free(set1);
6565 isl_set_free(set2);
6566 if (equal < 0)
6567 return -1;
6568 if (!equal)
6569 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6571 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
6572 set = isl_set_read_from_str(ctx, str);
6573 pwaff = isl_set_dim_max(set, 0);
6574 set1 = isl_set_from_pw_aff(pwaff);
6575 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6576 set2 = isl_set_read_from_str(ctx, str);
6577 equal = isl_set_is_equal(set1, set2);
6578 isl_set_free(set1);
6579 isl_set_free(set2);
6580 if (equal < 0)
6581 return -1;
6582 if (!equal)
6583 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6585 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
6586 "0 <= i < N and 0 <= j < M }";
6587 map = isl_map_read_from_str(ctx, str);
6588 set = isl_map_range(map);
6590 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
6591 set1 = isl_set_from_pw_aff(pwaff);
6592 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
6593 set2 = isl_set_read_from_str(ctx, str);
6594 equal = isl_set_is_equal(set1, set2);
6595 isl_set_free(set1);
6596 isl_set_free(set2);
6598 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
6599 set1 = isl_set_from_pw_aff(pwaff);
6600 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
6601 set2 = isl_set_read_from_str(ctx, str);
6602 if (equal >= 0 && equal)
6603 equal = isl_set_is_equal(set1, set2);
6604 isl_set_free(set1);
6605 isl_set_free(set2);
6607 isl_set_free(set);
6609 if (equal < 0)
6610 return -1;
6611 if (!equal)
6612 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6614 /* Check that solutions are properly merged. */
6615 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
6616 "c <= -1 + n - 4a - 2b and c >= -2b and "
6617 "4a >= -4 + n and c >= 0 }";
6618 set = isl_set_read_from_str(ctx, str);
6619 pwaff = isl_set_dim_min(set, 2);
6620 set1 = isl_set_from_pw_aff(pwaff);
6621 str = "[n] -> { [(0)] : n >= 1 }";
6622 set2 = isl_set_read_from_str(ctx, str);
6623 equal = isl_set_is_equal(set1, set2);
6624 isl_set_free(set1);
6625 isl_set_free(set2);
6627 if (equal < 0)
6628 return -1;
6629 if (!equal)
6630 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6632 /* Check that empty solution lie in the right space. */
6633 str = "[n] -> { [t,a] : 1 = 0 }";
6634 set = isl_set_read_from_str(ctx, str);
6635 pwaff = isl_set_dim_max(set, 0);
6636 set1 = isl_set_from_pw_aff(pwaff);
6637 str = "[n] -> { [t] : 1 = 0 }";
6638 set2 = isl_set_read_from_str(ctx, str);
6639 equal = isl_set_is_equal(set1, set2);
6640 isl_set_free(set1);
6641 isl_set_free(set2);
6643 if (equal < 0)
6644 return -1;
6645 if (!equal)
6646 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6648 return 0;
6651 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
6653 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
6654 const char *str)
6656 isl_ctx *ctx;
6657 isl_pw_multi_aff *pma2;
6658 int equal;
6660 if (!pma)
6661 return -1;
6663 ctx = isl_pw_multi_aff_get_ctx(pma);
6664 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6665 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
6666 isl_pw_multi_aff_free(pma2);
6668 return equal;
6671 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
6672 * represented by "str".
6674 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
6675 const char *str)
6677 int equal;
6679 equal = pw_multi_aff_plain_is_equal(pma, str);
6680 if (equal < 0)
6681 return -1;
6682 if (!equal)
6683 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
6684 "result not as expected", return -1);
6685 return 0;
6688 /* Basic test for isl_pw_multi_aff_product.
6690 * Check that multiple pieces are properly handled.
6692 static int test_product_pma(isl_ctx *ctx)
6694 int equal;
6695 const char *str;
6696 isl_pw_multi_aff *pma1, *pma2;
6698 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
6699 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6700 str = "{ C[] -> D[] }";
6701 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6702 pma1 = isl_pw_multi_aff_product(pma1, pma2);
6703 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
6704 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
6705 equal = pw_multi_aff_check_plain_equal(pma1, str);
6706 isl_pw_multi_aff_free(pma1);
6707 if (equal < 0)
6708 return -1;
6710 return 0;
6713 int test_product(isl_ctx *ctx)
6715 const char *str;
6716 isl_set *set;
6717 isl_union_set *uset1, *uset2;
6718 int ok;
6720 str = "{ A[i] }";
6721 set = isl_set_read_from_str(ctx, str);
6722 set = isl_set_product(set, isl_set_copy(set));
6723 ok = isl_set_is_wrapping(set);
6724 isl_set_free(set);
6725 if (ok < 0)
6726 return -1;
6727 if (!ok)
6728 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6730 str = "{ [] }";
6731 uset1 = isl_union_set_read_from_str(ctx, str);
6732 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
6733 str = "{ [[] -> []] }";
6734 uset2 = isl_union_set_read_from_str(ctx, str);
6735 ok = isl_union_set_is_equal(uset1, uset2);
6736 isl_union_set_free(uset1);
6737 isl_union_set_free(uset2);
6738 if (ok < 0)
6739 return -1;
6740 if (!ok)
6741 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6743 if (test_product_pma(ctx) < 0)
6744 return -1;
6746 return 0;
6749 /* Check that two sets are not considered disjoint just because
6750 * they have a different set of (named) parameters.
6752 static int test_disjoint(isl_ctx *ctx)
6754 const char *str;
6755 isl_set *set, *set2;
6756 int disjoint;
6758 str = "[n] -> { [[]->[]] }";
6759 set = isl_set_read_from_str(ctx, str);
6760 str = "{ [[]->[]] }";
6761 set2 = isl_set_read_from_str(ctx, str);
6762 disjoint = isl_set_is_disjoint(set, set2);
6763 isl_set_free(set);
6764 isl_set_free(set2);
6765 if (disjoint < 0)
6766 return -1;
6767 if (disjoint)
6768 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6770 return 0;
6773 /* Inputs for isl_pw_multi_aff_is_equal tests.
6774 * "f1" and "f2" are the two function that need to be compared.
6775 * "equal" is the expected result.
6777 struct {
6778 int equal;
6779 const char *f1;
6780 const char *f2;
6781 } pma_equal_tests[] = {
6782 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
6783 "[N] -> { [0] : 0 <= N <= 1 }" },
6784 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6785 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
6786 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6787 "[N] -> { [0] : 0 <= N <= 1 }" },
6788 { 0, "{ [NaN] }", "{ [NaN] }" },
6791 int test_equal(isl_ctx *ctx)
6793 int i;
6794 const char *str;
6795 isl_set *set, *set2;
6796 int equal;
6798 str = "{ S_6[i] }";
6799 set = isl_set_read_from_str(ctx, str);
6800 str = "{ S_7[i] }";
6801 set2 = isl_set_read_from_str(ctx, str);
6802 equal = isl_set_is_equal(set, set2);
6803 isl_set_free(set);
6804 isl_set_free(set2);
6805 if (equal < 0)
6806 return -1;
6807 if (equal)
6808 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6810 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
6811 int expected = pma_equal_tests[i].equal;
6812 isl_pw_multi_aff *f1, *f2;
6814 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
6815 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
6816 equal = isl_pw_multi_aff_is_equal(f1, f2);
6817 isl_pw_multi_aff_free(f1);
6818 isl_pw_multi_aff_free(f2);
6819 if (equal < 0)
6820 return -1;
6821 if (equal != expected)
6822 isl_die(ctx, isl_error_unknown,
6823 "unexpected equality result", return -1);
6826 return 0;
6829 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
6830 enum isl_dim_type type, unsigned pos, int fixed)
6832 isl_bool test;
6834 test = isl_map_plain_is_fixed(map, type, pos, NULL);
6835 isl_map_free(map);
6836 if (test < 0)
6837 return -1;
6838 if (test == fixed)
6839 return 0;
6840 if (fixed)
6841 isl_die(ctx, isl_error_unknown,
6842 "map not detected as fixed", return -1);
6843 else
6844 isl_die(ctx, isl_error_unknown,
6845 "map detected as fixed", return -1);
6848 int test_fixed(isl_ctx *ctx)
6850 const char *str;
6851 isl_map *map;
6853 str = "{ [i] -> [i] }";
6854 map = isl_map_read_from_str(ctx, str);
6855 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
6856 return -1;
6857 str = "{ [i] -> [1] }";
6858 map = isl_map_read_from_str(ctx, str);
6859 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6860 return -1;
6861 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
6862 map = isl_map_read_from_str(ctx, str);
6863 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6864 return -1;
6865 map = isl_map_read_from_str(ctx, str);
6866 map = isl_map_neg(map);
6867 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6868 return -1;
6870 return 0;
6873 struct isl_vertices_test_data {
6874 const char *set;
6875 int n;
6876 const char *vertex[6];
6877 } vertices_tests[] = {
6878 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
6879 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
6880 { "{ A[t, i] : t = 14 and i = 1 }",
6881 1, { "{ A[14, 1] }" } },
6882 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
6883 "c <= m and m <= n and m > 0 }",
6884 6, {
6885 "[n, m] -> { [n, m, m] : 0 < m <= n }",
6886 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
6887 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
6888 "[n, m] -> { [m, m, m] : 0 < m <= n }",
6889 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
6890 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
6891 } },
6894 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
6896 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
6898 struct isl_vertices_test_data *data = user;
6899 isl_ctx *ctx;
6900 isl_multi_aff *ma;
6901 isl_basic_set *bset;
6902 isl_pw_multi_aff *pma;
6903 int i;
6904 isl_bool equal;
6906 ctx = isl_vertex_get_ctx(vertex);
6907 bset = isl_vertex_get_domain(vertex);
6908 ma = isl_vertex_get_expr(vertex);
6909 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
6911 for (i = 0; i < data->n; ++i) {
6912 isl_pw_multi_aff *pma_i;
6914 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
6915 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
6916 isl_pw_multi_aff_free(pma_i);
6918 if (equal < 0 || equal)
6919 break;
6922 isl_pw_multi_aff_free(pma);
6923 isl_vertex_free(vertex);
6925 if (equal < 0)
6926 return isl_stat_error;
6928 return equal ? isl_stat_ok : isl_stat_error;
6931 int test_vertices(isl_ctx *ctx)
6933 int i;
6935 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
6936 isl_basic_set *bset;
6937 isl_vertices *vertices;
6938 int ok = 1;
6939 isl_size n;
6941 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
6942 vertices = isl_basic_set_compute_vertices(bset);
6943 n = isl_vertices_get_n_vertices(vertices);
6944 if (vertices_tests[i].n != n)
6945 ok = 0;
6946 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
6947 &vertices_tests[i]) < 0)
6948 ok = 0;
6949 isl_vertices_free(vertices);
6950 isl_basic_set_free(bset);
6952 if (n < 0)
6953 return -1;
6954 if (!ok)
6955 isl_die(ctx, isl_error_unknown, "unexpected vertices",
6956 return -1);
6959 return 0;
6962 int test_union_pw(isl_ctx *ctx)
6964 int equal;
6965 const char *str;
6966 isl_union_set *uset;
6967 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
6969 str = "{ [x] -> x^2 }";
6970 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
6971 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
6972 uset = isl_union_pw_qpolynomial_domain(upwqp1);
6973 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
6974 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
6975 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
6976 isl_union_pw_qpolynomial_free(upwqp1);
6977 isl_union_pw_qpolynomial_free(upwqp2);
6978 if (equal < 0)
6979 return -1;
6980 if (!equal)
6981 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6983 return 0;
6986 /* Inputs for basic tests of functions that select
6987 * subparts of the domain of an isl_multi_union_pw_aff.
6988 * "fn" is the function that is tested.
6989 * "arg" is a string description of the input.
6990 * "res" is a string description of the expected result.
6992 struct {
6993 __isl_give isl_union_set *(*fn)(
6994 __isl_take isl_multi_union_pw_aff *mupa);
6995 const char *arg;
6996 const char *res;
6997 } un_locus_tests[] = {
6998 { &isl_multi_union_pw_aff_zero_union_set,
6999 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
7000 "{ A[0,j]; B[0,j] }" },
7001 { &isl_multi_union_pw_aff_zero_union_set,
7002 "F[{ A[i,j] -> [i-j]; B[i,j] -> [i-j] : i >= 0 }]",
7003 "{ A[i,i]; B[i,i] : i >= 0 }" },
7004 { &isl_multi_union_pw_aff_zero_union_set,
7005 "(F[] : { A[i,j]; B[i,i] : i >= 0 })",
7006 "{ A[i,j]; B[i,i] : i >= 0 }" },
7009 /* Perform some basic tests of functions that select
7010 * subparts of the domain of an isl_multi_union_pw_aff.
7012 static int test_un_locus(isl_ctx *ctx)
7014 int i;
7015 isl_bool ok;
7016 isl_union_set *uset, *res;
7017 isl_multi_union_pw_aff *mupa;
7019 for (i = 0; i < ARRAY_SIZE(un_locus_tests); ++i) {
7020 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
7021 un_locus_tests[i].arg);
7022 res = isl_union_set_read_from_str(ctx, un_locus_tests[i].res);
7023 uset = un_locus_tests[i].fn(mupa);
7024 ok = isl_union_set_is_equal(uset, res);
7025 isl_union_set_free(uset);
7026 isl_union_set_free(res);
7027 if (ok < 0)
7028 return -1;
7029 if (!ok)
7030 isl_die(ctx, isl_error_unknown,
7031 "unexpected result", return -1);
7034 return 0;
7037 /* Inputs for basic tests of functions that select
7038 * subparts of an isl_union_map based on a relation
7039 * specified by an isl_multi_union_pw_aff.
7040 * "fn" is the function that is tested.
7041 * "arg1" and "arg2" are string descriptions of the inputs.
7042 * "res" is a string description of the expected result.
7044 struct {
7045 __isl_give isl_union_map *(*fn)(
7046 __isl_take isl_union_map *umap,
7047 __isl_take isl_multi_union_pw_aff *mupa);
7048 const char *arg1;
7049 const char *arg2;
7050 const char *res;
7051 } bin_locus_tests[] = {
7052 { &isl_union_map_eq_at_multi_union_pw_aff,
7053 "{ A[i,j] -> B[i',j'] }",
7054 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
7055 "{ A[i,j] -> B[i,j'] }" },
7056 { &isl_union_map_eq_at_multi_union_pw_aff,
7057 "{ A[i,j] -> B[i',j'] }",
7058 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
7059 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
7060 "{ A[i,j] -> B[i,j] }" },
7061 { &isl_union_map_eq_at_multi_union_pw_aff,
7062 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
7063 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
7064 "{ A[i,j] -> B[i,j'] }" },
7065 { &isl_union_map_eq_at_multi_union_pw_aff,
7066 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
7067 "F[{ A[i,j] -> [i]; B[i,j] -> [i]; C[i,j] -> [0] }]",
7068 "{ A[i,j] -> B[i,j']; A[0,j] -> C[i',j'] }" },
7069 { &isl_union_map_eq_at_multi_union_pw_aff,
7070 "{ A[i,j] -> B[i',j'] }",
7071 "F[{ A[i,j] -> [i] : i > j; B[i,j] -> [i] }]",
7072 "{ A[i,j] -> B[i,j'] : i > j }" },
7073 { &isl_union_map_lex_lt_at_multi_union_pw_aff,
7074 "{ A[i,j] -> B[i',j'] }",
7075 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
7076 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
7077 "{ A[i,j] -> B[i',j'] : i,j << i',j' }" },
7078 { &isl_union_map_lex_gt_at_multi_union_pw_aff,
7079 "{ A[i,j] -> B[i',j'] }",
7080 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
7081 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
7082 "{ A[i,j] -> B[i',j'] : i,j >> i',j' }" },
7083 { &isl_union_map_eq_at_multi_union_pw_aff,
7084 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
7085 "(F[] : { A[i,j]; B[i,j] })",
7086 "{ A[i,j] -> B[i',j'] }" },
7087 { &isl_union_map_eq_at_multi_union_pw_aff,
7088 "{ A[i,j] -> B[i',j'] }",
7089 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
7090 "{ A[i,j] -> B[i',j'] : i > j and i' < j' }" },
7091 { &isl_union_map_eq_at_multi_union_pw_aff,
7092 "[N] -> { A[i,j] -> B[i',j'] : i,i' <= N }",
7093 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
7094 "[N] -> { A[i,j] -> B[i',j'] : i > j and i' < j' and i,i' <= N }" },
7095 { &isl_union_map_eq_at_multi_union_pw_aff,
7096 "{ A[i,j] -> B[i',j'] }",
7097 "[N] -> (F[] : { A[i,j] : i < N; B[i,j] : i < N })",
7098 "[N] -> { A[i,j] -> B[i',j'] : i,i' < N }" },
7099 { &isl_union_map_eq_at_multi_union_pw_aff,
7100 "{ A[i,j] -> B[i',j'] }",
7101 "[N] -> (F[] : { : N >= 0 })",
7102 "[N] -> { A[i,j] -> B[i',j'] : N >= 0 }" },
7105 /* Perform some basic tests of functions that select
7106 * subparts of an isl_union_map based on a relation
7107 * specified by an isl_multi_union_pw_aff.
7109 static int test_bin_locus(isl_ctx *ctx)
7111 int i;
7112 isl_bool ok;
7113 isl_union_map *umap, *res;
7114 isl_multi_union_pw_aff *mupa;
7116 for (i = 0; i < ARRAY_SIZE(bin_locus_tests); ++i) {
7117 umap = isl_union_map_read_from_str(ctx,
7118 bin_locus_tests[i].arg1);
7119 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
7120 bin_locus_tests[i].arg2);
7121 res = isl_union_map_read_from_str(ctx, bin_locus_tests[i].res);
7122 umap = bin_locus_tests[i].fn(umap, mupa);
7123 ok = isl_union_map_is_equal(umap, res);
7124 isl_union_map_free(umap);
7125 isl_union_map_free(res);
7126 if (ok < 0)
7127 return -1;
7128 if (!ok)
7129 isl_die(ctx, isl_error_unknown,
7130 "unexpected result", return -1);
7133 return 0;
7136 /* Perform basic locus tests.
7138 static int test_locus(isl_ctx *ctx)
7140 if (test_un_locus(ctx) < 0)
7141 return -1;
7142 if (test_bin_locus(ctx) < 0)
7143 return -1;
7144 return 0;
7147 /* Test that isl_union_pw_qpolynomial_eval picks up the function
7148 * defined over the correct domain space.
7150 static int test_eval_1(isl_ctx *ctx)
7152 const char *str;
7153 isl_point *pnt;
7154 isl_set *set;
7155 isl_union_pw_qpolynomial *upwqp;
7156 isl_val *v;
7157 int cmp;
7159 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
7160 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
7161 str = "{ A[6] }";
7162 set = isl_set_read_from_str(ctx, str);
7163 pnt = isl_set_sample_point(set);
7164 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
7165 cmp = isl_val_cmp_si(v, 36);
7166 isl_val_free(v);
7168 if (!v)
7169 return -1;
7170 if (cmp != 0)
7171 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
7173 return 0;
7176 /* Check that isl_qpolynomial_eval handles getting called on a void point.
7178 static int test_eval_2(isl_ctx *ctx)
7180 const char *str;
7181 isl_point *pnt;
7182 isl_set *set;
7183 isl_qpolynomial *qp;
7184 isl_val *v;
7185 isl_bool ok;
7187 str = "{ A[x] -> [x] }";
7188 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
7189 str = "{ A[x] : false }";
7190 set = isl_set_read_from_str(ctx, str);
7191 pnt = isl_set_sample_point(set);
7192 v = isl_qpolynomial_eval(qp, pnt);
7193 ok = isl_val_is_nan(v);
7194 isl_val_free(v);
7196 if (ok < 0)
7197 return -1;
7198 if (!ok)
7199 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
7201 return 0;
7204 /* Check that a polynomial (without local variables) can be evaluated
7205 * in a rational point.
7207 static isl_stat test_eval_3(isl_ctx *ctx)
7209 isl_pw_qpolynomial *pwqp;
7210 isl_point *pnt;
7211 isl_val *v;
7212 isl_stat r;
7214 pwqp = isl_pw_qpolynomial_read_from_str(ctx, "{ [x] -> x^2 }");
7215 pnt = isl_point_zero(isl_pw_qpolynomial_get_domain_space(pwqp));
7216 v = isl_val_read_from_str(ctx, "1/2");
7217 pnt = isl_point_set_coordinate_val(pnt, isl_dim_set, 0, v);
7218 v = isl_pw_qpolynomial_eval(pwqp, pnt);
7219 r = val_check_equal(v, "1/4");
7220 isl_val_free(v);
7222 return r;
7225 /* Inputs for isl_pw_aff_eval test.
7226 * "f" is the affine function.
7227 * "p" is the point where the function should be evaluated.
7228 * "res" is the expected result.
7230 struct {
7231 const char *f;
7232 const char *p;
7233 const char *res;
7234 } aff_eval_tests[] = {
7235 { "{ [i] -> [2 * i] }", "{ [4] }", "8" },
7236 { "{ [i] -> [2 * i] }", "{ [x] : false }", "NaN" },
7237 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [0] }", "0" },
7238 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [1] }", "1" },
7239 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [2] }", "3" },
7240 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [3] }", "5" },
7241 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [4] }", "7" },
7242 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [0] }", "0" },
7243 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [1] }", "0" },
7244 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [2] }", "0" },
7245 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [3] }", "0" },
7246 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [4] }", "1" },
7247 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [6] }", "1" },
7248 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [8] }", "2" },
7249 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [4] }", "4" },
7250 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [-2] }", "2" },
7251 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [0] }", "NaN" },
7252 { "[N] -> { [2 * N] }", "[N] -> { : N = 4 }", "8" },
7253 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 1] }", "1" },
7254 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 2] }", "3/2" },
7255 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [4] }", "4" },
7256 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [3] }", "NaN" },
7257 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [x] : false }", "NaN" },
7260 /* Perform basic isl_pw_aff_eval tests.
7262 static int test_eval_aff(isl_ctx *ctx)
7264 int i;
7266 for (i = 0; i < ARRAY_SIZE(aff_eval_tests); ++i) {
7267 isl_stat r;
7268 isl_pw_aff *pa;
7269 isl_set *set;
7270 isl_point *pnt;
7271 isl_val *v;
7273 pa = isl_pw_aff_read_from_str(ctx, aff_eval_tests[i].f);
7274 set = isl_set_read_from_str(ctx, aff_eval_tests[i].p);
7275 pnt = isl_set_sample_point(set);
7276 v = isl_pw_aff_eval(pa, pnt);
7277 r = val_check_equal(v, aff_eval_tests[i].res);
7278 isl_val_free(v);
7279 if (r < 0)
7280 return -1;
7282 return 0;
7285 /* Perform basic evaluation tests.
7287 static int test_eval(isl_ctx *ctx)
7289 if (test_eval_1(ctx) < 0)
7290 return -1;
7291 if (test_eval_2(ctx) < 0)
7292 return -1;
7293 if (test_eval_3(ctx) < 0)
7294 return -1;
7295 if (test_eval_aff(ctx) < 0)
7296 return -1;
7297 return 0;
7300 /* Descriptions of sets that are tested for reparsing after printing.
7302 const char *output_tests[] = {
7303 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
7304 "{ [x] : 1 = 0 }",
7305 "{ [x] : false }",
7306 "{ [x] : x mod 2 = 0 }",
7307 "{ [x] : x mod 2 = 1 }",
7308 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
7309 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
7310 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7311 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7312 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
7313 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
7316 /* Check that printing a set and reparsing a set from the printed output
7317 * results in the same set.
7319 static int test_output_set(isl_ctx *ctx)
7321 int i;
7322 char *str;
7323 isl_set *set1, *set2;
7324 isl_bool equal;
7326 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
7327 set1 = isl_set_read_from_str(ctx, output_tests[i]);
7328 str = isl_set_to_str(set1);
7329 set2 = isl_set_read_from_str(ctx, str);
7330 free(str);
7331 equal = isl_set_is_equal(set1, set2);
7332 isl_set_free(set1);
7333 isl_set_free(set2);
7334 if (equal < 0)
7335 return -1;
7336 if (!equal)
7337 isl_die(ctx, isl_error_unknown,
7338 "parsed output not the same", return -1);
7341 return 0;
7344 /* Check that an isl_multi_aff is printed using a consistent space.
7346 static isl_stat test_output_ma(isl_ctx *ctx)
7348 char *str;
7349 isl_bool equal;
7350 isl_aff *aff;
7351 isl_multi_aff *ma, *ma2;
7353 ma = isl_multi_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7354 aff = isl_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7355 ma = isl_multi_aff_set_aff(ma, 0, aff);
7356 str = isl_multi_aff_to_str(ma);
7357 ma2 = isl_multi_aff_read_from_str(ctx, str);
7358 free(str);
7359 equal = isl_multi_aff_plain_is_equal(ma, ma2);
7360 isl_multi_aff_free(ma2);
7361 isl_multi_aff_free(ma);
7363 if (equal < 0)
7364 return isl_stat_error;
7365 if (!equal)
7366 isl_die(ctx, isl_error_unknown, "bad conversion",
7367 return isl_stat_error);
7369 return isl_stat_ok;
7372 /* Check that an isl_multi_pw_aff is printed using a consistent space.
7374 static isl_stat test_output_mpa(isl_ctx *ctx)
7376 char *str;
7377 isl_bool equal;
7378 isl_pw_aff *pa;
7379 isl_multi_pw_aff *mpa, *mpa2;
7381 mpa = isl_multi_pw_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7382 pa = isl_pw_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7383 mpa = isl_multi_pw_aff_set_pw_aff(mpa, 0, pa);
7384 str = isl_multi_pw_aff_to_str(mpa);
7385 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
7386 free(str);
7387 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
7388 isl_multi_pw_aff_free(mpa2);
7389 isl_multi_pw_aff_free(mpa);
7391 if (equal < 0)
7392 return isl_stat_error;
7393 if (!equal)
7394 isl_die(ctx, isl_error_unknown, "bad conversion",
7395 return isl_stat_error);
7397 return isl_stat_ok;
7400 int test_output(isl_ctx *ctx)
7402 char *s;
7403 const char *str;
7404 isl_pw_aff *pa;
7405 isl_printer *p;
7406 int equal;
7408 if (test_output_set(ctx) < 0)
7409 return -1;
7410 if (test_output_ma(ctx) < 0)
7411 return -1;
7412 if (test_output_mpa(ctx) < 0)
7413 return -1;
7415 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
7416 pa = isl_pw_aff_read_from_str(ctx, str);
7418 p = isl_printer_to_str(ctx);
7419 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
7420 p = isl_printer_print_pw_aff(p, pa);
7421 s = isl_printer_get_str(p);
7422 isl_printer_free(p);
7423 isl_pw_aff_free(pa);
7424 if (!s)
7425 equal = -1;
7426 else
7427 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
7428 free(s);
7429 if (equal < 0)
7430 return -1;
7431 if (!equal)
7432 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7434 return 0;
7437 int test_sample(isl_ctx *ctx)
7439 const char *str;
7440 isl_basic_set *bset1, *bset2;
7441 int empty, subset;
7443 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
7444 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
7445 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
7446 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
7447 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
7448 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
7449 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
7450 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
7451 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
7452 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
7453 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
7454 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
7455 "d - 1073741821e and "
7456 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
7457 "3j >= 1 - a + b + 2e and "
7458 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
7459 "3i <= 4 - a + 4b - e and "
7460 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
7461 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
7462 "c <= -1 + a and 3i >= -2 - a + 3e and "
7463 "1073741822e <= 1073741823 - a + 1073741822b + c and "
7464 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
7465 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
7466 "1073741823e >= 1 + 1073741823b - d and "
7467 "3i >= 1073741823b + c - 1073741823e - f and "
7468 "3i >= 1 + 2b + e + 3g }";
7469 bset1 = isl_basic_set_read_from_str(ctx, str);
7470 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
7471 empty = isl_basic_set_is_empty(bset2);
7472 subset = isl_basic_set_is_subset(bset2, bset1);
7473 isl_basic_set_free(bset1);
7474 isl_basic_set_free(bset2);
7475 if (empty < 0 || subset < 0)
7476 return -1;
7477 if (empty)
7478 isl_die(ctx, isl_error_unknown, "point not found", return -1);
7479 if (!subset)
7480 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
7482 return 0;
7485 int test_fixed_power(isl_ctx *ctx)
7487 const char *str;
7488 isl_map *map;
7489 isl_val *exp;
7490 int equal;
7492 str = "{ [i] -> [i + 1] }";
7493 map = isl_map_read_from_str(ctx, str);
7494 exp = isl_val_int_from_si(ctx, 23);
7495 map = isl_map_fixed_power_val(map, exp);
7496 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
7497 isl_map_free(map);
7498 if (equal < 0)
7499 return -1;
7501 return 0;
7504 int test_slice(isl_ctx *ctx)
7506 const char *str;
7507 isl_map *map;
7508 int equal;
7510 str = "{ [i] -> [j] }";
7511 map = isl_map_read_from_str(ctx, str);
7512 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
7513 equal = map_check_equal(map, "{ [i] -> [i] }");
7514 isl_map_free(map);
7515 if (equal < 0)
7516 return -1;
7518 str = "{ [i] -> [j] }";
7519 map = isl_map_read_from_str(ctx, str);
7520 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
7521 equal = map_check_equal(map, "{ [i] -> [j] }");
7522 isl_map_free(map);
7523 if (equal < 0)
7524 return -1;
7526 str = "{ [i] -> [j] }";
7527 map = isl_map_read_from_str(ctx, str);
7528 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
7529 equal = map_check_equal(map, "{ [i] -> [-i] }");
7530 isl_map_free(map);
7531 if (equal < 0)
7532 return -1;
7534 str = "{ [i] -> [j] }";
7535 map = isl_map_read_from_str(ctx, str);
7536 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
7537 equal = map_check_equal(map, "{ [0] -> [j] }");
7538 isl_map_free(map);
7539 if (equal < 0)
7540 return -1;
7542 str = "{ [i] -> [j] }";
7543 map = isl_map_read_from_str(ctx, str);
7544 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
7545 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
7546 isl_map_free(map);
7547 if (equal < 0)
7548 return -1;
7550 str = "{ [i] -> [j] }";
7551 map = isl_map_read_from_str(ctx, str);
7552 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
7553 equal = map_check_equal(map, "{ [i] -> [j] : false }");
7554 isl_map_free(map);
7555 if (equal < 0)
7556 return -1;
7558 return 0;
7561 int test_eliminate(isl_ctx *ctx)
7563 const char *str;
7564 isl_map *map;
7565 int equal;
7567 str = "{ [i] -> [j] : i = 2j }";
7568 map = isl_map_read_from_str(ctx, str);
7569 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
7570 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
7571 isl_map_free(map);
7572 if (equal < 0)
7573 return -1;
7575 return 0;
7578 /* Check basic functionality of isl_map_deltas_map.
7580 static int test_deltas_map(isl_ctx *ctx)
7582 const char *str;
7583 isl_map *map;
7584 int equal;
7586 str = "{ A[i] -> A[i + 1] }";
7587 map = isl_map_read_from_str(ctx, str);
7588 map = isl_map_deltas_map(map);
7589 equal = map_check_equal(map, "{ [A[i] -> A[i + 1]] -> A[1] }");
7590 isl_map_free(map);
7591 if (equal < 0)
7592 return -1;
7594 return 0;
7597 /* Check that isl_set_dim_residue_class detects that the values of j
7598 * in the set below are all odd and that it does not detect any spurious
7599 * strides.
7601 static int test_residue_class(isl_ctx *ctx)
7603 const char *str;
7604 isl_set *set;
7605 isl_int m, r;
7606 isl_stat res;
7608 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
7609 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
7610 set = isl_set_read_from_str(ctx, str);
7611 isl_int_init(m);
7612 isl_int_init(r);
7613 res = isl_set_dim_residue_class(set, 1, &m, &r);
7614 if (res >= 0 &&
7615 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
7616 isl_die(ctx, isl_error_unknown, "incorrect residue class",
7617 res = isl_stat_error);
7618 isl_int_clear(r);
7619 isl_int_clear(m);
7620 isl_set_free(set);
7622 return res;
7625 static int test_align_parameters_1(isl_ctx *ctx)
7627 const char *str;
7628 isl_space *space;
7629 isl_multi_aff *ma1, *ma2;
7630 int equal;
7632 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
7633 ma1 = isl_multi_aff_read_from_str(ctx, str);
7635 space = isl_space_params_alloc(ctx, 1);
7636 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
7637 ma1 = isl_multi_aff_align_params(ma1, space);
7639 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
7640 ma2 = isl_multi_aff_read_from_str(ctx, str);
7642 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
7644 isl_multi_aff_free(ma1);
7645 isl_multi_aff_free(ma2);
7647 if (equal < 0)
7648 return -1;
7649 if (!equal)
7650 isl_die(ctx, isl_error_unknown,
7651 "result not as expected", return -1);
7653 return 0;
7656 /* Check the isl_multi_*_from_*_list operation in case inputs
7657 * have unaligned parameters.
7658 * In particular, older versions of isl would simply fail
7659 * (without printing any error message).
7661 static isl_stat test_align_parameters_2(isl_ctx *ctx)
7663 isl_space *space;
7664 isl_map *map;
7665 isl_aff *aff;
7666 isl_multi_aff *ma;
7668 map = isl_map_read_from_str(ctx, "{ A[] -> M[x] }");
7669 space = isl_map_get_space(map);
7670 isl_map_free(map);
7672 aff = isl_aff_read_from_str(ctx, "[N] -> { A[] -> [N] }");
7673 ma = isl_multi_aff_from_aff_list(space, isl_aff_list_from_aff(aff));
7674 isl_multi_aff_free(ma);
7676 if (!ma)
7677 return isl_stat_error;
7678 return isl_stat_ok;
7681 /* Perform basic parameter alignment tests.
7683 static int test_align_parameters(isl_ctx *ctx)
7685 if (test_align_parameters_1(ctx) < 0)
7686 return -1;
7687 if (test_align_parameters_2(ctx) < 0)
7688 return -1;
7690 return 0;
7693 /* Check that isl_*_drop_unused_params actually drops the unused parameters
7694 * by comparing the result using isl_*_plain_is_equal.
7695 * Note that this assumes that isl_*_plain_is_equal does not consider
7696 * objects that only differ by unused parameters to be equal.
7698 int test_drop_unused_parameters(isl_ctx *ctx)
7700 const char *str_with, *str_without;
7701 isl_basic_set *bset1, *bset2;
7702 isl_set *set1, *set2;
7703 isl_pw_aff *pwa1, *pwa2;
7704 int equal;
7706 str_with = "[n, m, o] -> { [m] }";
7707 str_without = "[m] -> { [m] }";
7709 bset1 = isl_basic_set_read_from_str(ctx, str_with);
7710 bset2 = isl_basic_set_read_from_str(ctx, str_without);
7711 bset1 = isl_basic_set_drop_unused_params(bset1);
7712 equal = isl_basic_set_plain_is_equal(bset1, bset2);
7713 isl_basic_set_free(bset1);
7714 isl_basic_set_free(bset2);
7716 if (equal < 0)
7717 return -1;
7718 if (!equal)
7719 isl_die(ctx, isl_error_unknown,
7720 "result not as expected", return -1);
7722 set1 = isl_set_read_from_str(ctx, str_with);
7723 set2 = isl_set_read_from_str(ctx, str_without);
7724 set1 = isl_set_drop_unused_params(set1);
7725 equal = isl_set_plain_is_equal(set1, set2);
7726 isl_set_free(set1);
7727 isl_set_free(set2);
7729 if (equal < 0)
7730 return -1;
7731 if (!equal)
7732 isl_die(ctx, isl_error_unknown,
7733 "result not as expected", return -1);
7735 pwa1 = isl_pw_aff_read_from_str(ctx, str_with);
7736 pwa2 = isl_pw_aff_read_from_str(ctx, str_without);
7737 pwa1 = isl_pw_aff_drop_unused_params(pwa1);
7738 equal = isl_pw_aff_plain_is_equal(pwa1, pwa2);
7739 isl_pw_aff_free(pwa1);
7740 isl_pw_aff_free(pwa2);
7742 if (equal < 0)
7743 return -1;
7744 if (!equal)
7745 isl_die(ctx, isl_error_unknown,
7746 "result not as expected", return -1);
7748 return 0;
7751 static int test_list(isl_ctx *ctx)
7753 isl_id *a, *b, *c, *d, *id;
7754 isl_id_list *list;
7755 isl_size n;
7756 int ok;
7758 a = isl_id_alloc(ctx, "a", NULL);
7759 b = isl_id_alloc(ctx, "b", NULL);
7760 c = isl_id_alloc(ctx, "c", NULL);
7761 d = isl_id_alloc(ctx, "d", NULL);
7763 list = isl_id_list_alloc(ctx, 4);
7764 list = isl_id_list_add(list, b);
7765 list = isl_id_list_insert(list, 0, a);
7766 list = isl_id_list_add(list, c);
7767 list = isl_id_list_add(list, d);
7768 list = isl_id_list_drop(list, 1, 1);
7770 n = isl_id_list_n_id(list);
7771 if (n < 0)
7772 return -1;
7773 if (n != 3) {
7774 isl_id_list_free(list);
7775 isl_die(ctx, isl_error_unknown,
7776 "unexpected number of elements in list", return -1);
7779 id = isl_id_list_get_id(list, 0);
7780 ok = id == a;
7781 isl_id_free(id);
7782 id = isl_id_list_get_id(list, 1);
7783 ok = ok && id == c;
7784 isl_id_free(id);
7785 id = isl_id_list_get_id(list, 2);
7786 ok = ok && id == d;
7787 isl_id_free(id);
7789 isl_id_list_free(list);
7791 if (!ok)
7792 isl_die(ctx, isl_error_unknown,
7793 "unexpected elements in list", return -1);
7795 return 0;
7798 /* Check the conversion from an isl_multi_aff to an isl_basic_set.
7800 static isl_stat test_ma_conversion(isl_ctx *ctx)
7802 const char *str;
7803 isl_bool equal;
7804 isl_multi_aff *ma;
7805 isl_basic_set *bset1, *bset2;
7807 str = "[N] -> { A[0, N + 1] }";
7808 ma = isl_multi_aff_read_from_str(ctx, str);
7809 bset1 = isl_basic_set_read_from_str(ctx, str);
7810 bset2 = isl_basic_set_from_multi_aff(ma);
7811 equal = isl_basic_set_is_equal(bset1, bset2);
7812 isl_basic_set_free(bset1);
7813 isl_basic_set_free(bset2);
7814 if (equal < 0)
7815 return isl_stat_error;
7816 if (!equal)
7817 isl_die(ctx, isl_error_unknown, "bad conversion",
7818 return isl_stat_error);
7819 return isl_stat_ok;
7822 const char *set_conversion_tests[] = {
7823 "[N] -> { [i] : N - 1 <= 2 i <= N }",
7824 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
7825 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7826 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7827 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
7828 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
7829 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
7830 "-3 + c <= d <= 28 + c) }",
7833 /* Check that converting from isl_set to isl_pw_multi_aff and back
7834 * to isl_set produces the original isl_set.
7836 static int test_set_conversion(isl_ctx *ctx)
7838 int i;
7839 const char *str;
7840 isl_set *set1, *set2;
7841 isl_pw_multi_aff *pma;
7842 int equal;
7844 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
7845 str = set_conversion_tests[i];
7846 set1 = isl_set_read_from_str(ctx, str);
7847 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
7848 set2 = isl_set_from_pw_multi_aff(pma);
7849 equal = isl_set_is_equal(set1, set2);
7850 isl_set_free(set1);
7851 isl_set_free(set2);
7853 if (equal < 0)
7854 return -1;
7855 if (!equal)
7856 isl_die(ctx, isl_error_unknown, "bad conversion",
7857 return -1);
7860 return 0;
7863 const char *conversion_tests[] = {
7864 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7865 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7866 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7867 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7868 "9e <= -2 - 2a) }",
7869 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7870 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7871 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7874 /* Check that converting from isl_map to isl_pw_multi_aff and back
7875 * to isl_map produces the original isl_map.
7877 static int test_map_conversion(isl_ctx *ctx)
7879 int i;
7880 isl_map *map1, *map2;
7881 isl_pw_multi_aff *pma;
7882 int equal;
7884 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
7885 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
7886 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
7887 map2 = isl_map_from_pw_multi_aff(pma);
7888 equal = isl_map_is_equal(map1, map2);
7889 isl_map_free(map1);
7890 isl_map_free(map2);
7892 if (equal < 0)
7893 return -1;
7894 if (!equal)
7895 isl_die(ctx, isl_error_unknown, "bad conversion",
7896 return -1);
7899 return 0;
7902 /* Descriptions of isl_pw_multi_aff objects for testing conversion
7903 * to isl_multi_pw_aff and back.
7905 const char *mpa_conversion_tests[] = {
7906 "{ [x] -> A[x] }",
7907 "{ [x] -> A[x] : x >= 0 }",
7908 "{ [x] -> A[x] : x >= 0; [x] -> A[-x] : x < 0 }",
7909 "{ [x] -> A[x, x + 1] }",
7910 "{ [x] -> A[] }",
7911 "{ [x] -> A[] : x >= 0 }",
7914 /* Check that conversion from isl_pw_multi_aff to isl_multi_pw_aff and
7915 * back to isl_pw_multi_aff preserves the original meaning.
7917 static int test_mpa_conversion(isl_ctx *ctx)
7919 int i;
7920 isl_pw_multi_aff *pma1, *pma2;
7921 isl_multi_pw_aff *mpa;
7922 int equal;
7924 for (i = 0; i < ARRAY_SIZE(mpa_conversion_tests); ++i) {
7925 const char *str;
7926 str = mpa_conversion_tests[i];
7927 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
7928 pma2 = isl_pw_multi_aff_copy(pma1);
7929 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma1);
7930 pma1 = isl_pw_multi_aff_from_multi_pw_aff(mpa);
7931 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7932 isl_pw_multi_aff_free(pma1);
7933 isl_pw_multi_aff_free(pma2);
7935 if (equal < 0)
7936 return -1;
7937 if (!equal)
7938 isl_die(ctx, isl_error_unknown, "bad conversion",
7939 return -1);
7942 return 0;
7945 /* Descriptions of union maps that should be convertible
7946 * to an isl_multi_union_pw_aff.
7948 const char *umap_mupa_conversion_tests[] = {
7949 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7950 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7951 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7952 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7953 "9e <= -2 - 2a) }",
7954 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7955 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7956 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7957 "{ A[] -> B[0]; C[] -> B[1] }",
7958 "{ A[] -> B[]; C[] -> B[] }",
7961 /* Check that converting from isl_union_map to isl_multi_union_pw_aff and back
7962 * to isl_union_map produces the original isl_union_map.
7964 static int test_union_map_mupa_conversion(isl_ctx *ctx)
7966 int i;
7967 isl_union_map *umap1, *umap2;
7968 isl_multi_union_pw_aff *mupa;
7969 int equal;
7971 for (i = 0; i < ARRAY_SIZE(umap_mupa_conversion_tests); ++i) {
7972 const char *str;
7973 str = umap_mupa_conversion_tests[i];
7974 umap1 = isl_union_map_read_from_str(ctx, str);
7975 umap2 = isl_union_map_copy(umap1);
7976 mupa = isl_multi_union_pw_aff_from_union_map(umap2);
7977 umap2 = isl_union_map_from_multi_union_pw_aff(mupa);
7978 equal = isl_union_map_is_equal(umap1, umap2);
7979 isl_union_map_free(umap1);
7980 isl_union_map_free(umap2);
7982 if (equal < 0)
7983 return -1;
7984 if (!equal)
7985 isl_die(ctx, isl_error_unknown, "bad conversion",
7986 return -1);
7989 return 0;
7992 static int test_conversion(isl_ctx *ctx)
7994 if (test_ma_conversion(ctx) < 0)
7995 return -1;
7996 if (test_set_conversion(ctx) < 0)
7997 return -1;
7998 if (test_map_conversion(ctx) < 0)
7999 return -1;
8000 if (test_mpa_conversion(ctx) < 0)
8001 return -1;
8002 if (test_union_map_mupa_conversion(ctx) < 0)
8003 return -1;
8004 return 0;
8007 /* Check that isl_basic_map_curry does not modify input.
8009 static int test_curry(isl_ctx *ctx)
8011 const char *str;
8012 isl_basic_map *bmap1, *bmap2;
8013 int equal;
8015 str = "{ [A[] -> B[]] -> C[] }";
8016 bmap1 = isl_basic_map_read_from_str(ctx, str);
8017 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
8018 equal = isl_basic_map_is_equal(bmap1, bmap2);
8019 isl_basic_map_free(bmap1);
8020 isl_basic_map_free(bmap2);
8022 if (equal < 0)
8023 return -1;
8024 if (equal)
8025 isl_die(ctx, isl_error_unknown,
8026 "curried map should not be equal to original",
8027 return -1);
8029 return 0;
8032 struct {
8033 const char *set;
8034 const char *ma;
8035 const char *res;
8036 } preimage_tests[] = {
8037 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
8038 "{ A[j,i] -> B[i,j] }",
8039 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
8040 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
8041 "{ A[a,b] -> B[a/2,b/6] }",
8042 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
8043 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
8044 "{ A[a,b] -> B[a/2,b/6] }",
8045 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
8046 "exists i,j : a = 2 i and b = 6 j }" },
8047 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
8048 "[n] -> { : 0 <= n <= 100 }" },
8049 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
8050 "{ A[a] -> B[2a] }",
8051 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
8052 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
8053 "{ A[a] -> B[([a/2])] }",
8054 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
8055 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
8056 "{ A[a] -> B[a,a,a/3] }",
8057 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
8058 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
8059 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
8062 static int test_preimage_basic_set(isl_ctx *ctx)
8064 int i;
8065 isl_basic_set *bset1, *bset2;
8066 isl_multi_aff *ma;
8067 int equal;
8069 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
8070 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
8071 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
8072 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
8073 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
8074 equal = isl_basic_set_is_equal(bset1, bset2);
8075 isl_basic_set_free(bset1);
8076 isl_basic_set_free(bset2);
8077 if (equal < 0)
8078 return -1;
8079 if (!equal)
8080 isl_die(ctx, isl_error_unknown, "bad preimage",
8081 return -1);
8084 return 0;
8087 struct {
8088 const char *map;
8089 const char *ma;
8090 const char *res;
8091 } preimage_domain_tests[] = {
8092 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
8093 "{ A[j,i] -> B[i,j] }",
8094 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
8095 { "{ B[i] -> C[i]; D[i] -> E[i] }",
8096 "{ A[i] -> B[i + 1] }",
8097 "{ A[i] -> C[i + 1] }" },
8098 { "{ B[i] -> C[i]; B[i] -> E[i] }",
8099 "{ A[i] -> B[i + 1] }",
8100 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
8101 { "{ B[i] -> C[([i/2])] }",
8102 "{ A[i] -> B[2i] }",
8103 "{ A[i] -> C[i] }" },
8104 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
8105 "{ A[i] -> B[([i/5]), ([i/7])] }",
8106 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
8107 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
8108 "[N] -> { A[] -> B[([N/5])] }",
8109 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
8110 { "{ B[i] -> C[i] : exists a : i = 5 a }",
8111 "{ A[i] -> B[2i] }",
8112 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
8113 { "{ B[i] -> C[i] : exists a : i = 2 a; "
8114 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
8115 "{ A[i] -> B[2i] }",
8116 "{ A[i] -> C[2i] }" },
8117 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
8118 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
8121 static int test_preimage_union_map(isl_ctx *ctx)
8123 int i;
8124 isl_union_map *umap1, *umap2;
8125 isl_multi_aff *ma;
8126 int equal;
8128 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
8129 umap1 = isl_union_map_read_from_str(ctx,
8130 preimage_domain_tests[i].map);
8131 ma = isl_multi_aff_read_from_str(ctx,
8132 preimage_domain_tests[i].ma);
8133 umap2 = isl_union_map_read_from_str(ctx,
8134 preimage_domain_tests[i].res);
8135 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
8136 equal = isl_union_map_is_equal(umap1, umap2);
8137 isl_union_map_free(umap1);
8138 isl_union_map_free(umap2);
8139 if (equal < 0)
8140 return -1;
8141 if (!equal)
8142 isl_die(ctx, isl_error_unknown, "bad preimage",
8143 return -1);
8146 return 0;
8149 static int test_preimage(isl_ctx *ctx)
8151 if (test_preimage_basic_set(ctx) < 0)
8152 return -1;
8153 if (test_preimage_union_map(ctx) < 0)
8154 return -1;
8156 return 0;
8159 struct {
8160 const char *ma1;
8161 const char *ma;
8162 const char *res;
8163 } pullback_tests[] = {
8164 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
8165 "{ A[a,b] -> C[b + 2a] }" },
8166 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
8167 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
8168 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
8169 "{ A[a] -> C[(a)/6] }" },
8170 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
8171 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
8172 "{ A[a] -> C[(2a)/3] }" },
8173 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
8174 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
8175 "{ A[i,j] -> C[i + j, i + j] }"},
8176 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
8177 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
8178 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
8179 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
8180 "{ [i, j] -> [floor((i)/3), j] }",
8181 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
8184 static int test_pullback(isl_ctx *ctx)
8186 int i;
8187 isl_multi_aff *ma1, *ma2;
8188 isl_multi_aff *ma;
8189 int equal;
8191 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
8192 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
8193 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
8194 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
8195 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
8196 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
8197 isl_multi_aff_free(ma1);
8198 isl_multi_aff_free(ma2);
8199 if (equal < 0)
8200 return -1;
8201 if (!equal)
8202 isl_die(ctx, isl_error_unknown, "bad pullback",
8203 return -1);
8206 return 0;
8209 /* Check that negation is printed correctly and that equal expressions
8210 * are correctly identified.
8212 static int test_ast(isl_ctx *ctx)
8214 isl_ast_expr *expr, *expr1, *expr2, *expr3;
8215 char *str;
8216 int ok, equal;
8218 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
8219 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
8220 expr = isl_ast_expr_add(expr1, expr2);
8221 expr2 = isl_ast_expr_copy(expr);
8222 expr = isl_ast_expr_neg(expr);
8223 expr2 = isl_ast_expr_neg(expr2);
8224 equal = isl_ast_expr_is_equal(expr, expr2);
8225 str = isl_ast_expr_to_C_str(expr);
8226 ok = str ? !strcmp(str, "-(A + B)") : -1;
8227 free(str);
8228 isl_ast_expr_free(expr);
8229 isl_ast_expr_free(expr2);
8231 if (ok < 0 || equal < 0)
8232 return -1;
8233 if (!equal)
8234 isl_die(ctx, isl_error_unknown,
8235 "equal expressions not considered equal", return -1);
8236 if (!ok)
8237 isl_die(ctx, isl_error_unknown,
8238 "isl_ast_expr printed incorrectly", return -1);
8240 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
8241 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
8242 expr = isl_ast_expr_add(expr1, expr2);
8243 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
8244 expr = isl_ast_expr_sub(expr3, expr);
8245 str = isl_ast_expr_to_C_str(expr);
8246 ok = str ? !strcmp(str, "C - (A + B)") : -1;
8247 free(str);
8248 isl_ast_expr_free(expr);
8250 if (ok < 0)
8251 return -1;
8252 if (!ok)
8253 isl_die(ctx, isl_error_unknown,
8254 "isl_ast_expr printed incorrectly", return -1);
8256 return 0;
8259 /* Check that isl_ast_build_expr_from_set returns a valid expression
8260 * for an empty set. Note that isl_ast_build_expr_from_set getting
8261 * called on an empty set probably indicates a bug in the caller.
8263 static int test_ast_build(isl_ctx *ctx)
8265 isl_set *set;
8266 isl_ast_build *build;
8267 isl_ast_expr *expr;
8269 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8270 build = isl_ast_build_from_context(set);
8272 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
8273 expr = isl_ast_build_expr_from_set(build, set);
8275 isl_ast_expr_free(expr);
8276 isl_ast_build_free(build);
8278 if (!expr)
8279 return -1;
8281 return 0;
8284 /* Internal data structure for before_for and after_for callbacks.
8286 * depth is the current depth
8287 * before is the number of times before_for has been called
8288 * after is the number of times after_for has been called
8290 struct isl_test_codegen_data {
8291 int depth;
8292 int before;
8293 int after;
8296 /* This function is called before each for loop in the AST generated
8297 * from test_ast_gen1.
8299 * Increment the number of calls and the depth.
8300 * Check that the space returned by isl_ast_build_get_schedule_space
8301 * matches the target space of the schedule returned by
8302 * isl_ast_build_get_schedule.
8303 * Return an isl_id that is checked by the corresponding call
8304 * to after_for.
8306 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
8307 void *user)
8309 struct isl_test_codegen_data *data = user;
8310 isl_ctx *ctx;
8311 isl_space *space;
8312 isl_union_map *schedule;
8313 isl_union_set *uset;
8314 isl_set *set;
8315 isl_bool empty;
8316 isl_size n;
8317 char name[] = "d0";
8319 ctx = isl_ast_build_get_ctx(build);
8321 if (data->before >= 3)
8322 isl_die(ctx, isl_error_unknown,
8323 "unexpected number of for nodes", return NULL);
8324 if (data->depth >= 2)
8325 isl_die(ctx, isl_error_unknown,
8326 "unexpected depth", return NULL);
8328 snprintf(name, sizeof(name), "d%d", data->depth);
8329 data->before++;
8330 data->depth++;
8332 schedule = isl_ast_build_get_schedule(build);
8333 uset = isl_union_map_range(schedule);
8334 n = isl_union_set_n_set(uset);
8335 if (n != 1) {
8336 isl_union_set_free(uset);
8337 if (n < 0)
8338 return NULL;
8339 isl_die(ctx, isl_error_unknown,
8340 "expecting single range space", return NULL);
8343 space = isl_ast_build_get_schedule_space(build);
8344 set = isl_union_set_extract_set(uset, space);
8345 isl_union_set_free(uset);
8346 empty = isl_set_is_empty(set);
8347 isl_set_free(set);
8349 if (empty < 0)
8350 return NULL;
8351 if (empty)
8352 isl_die(ctx, isl_error_unknown,
8353 "spaces don't match", return NULL);
8355 return isl_id_alloc(ctx, name, NULL);
8358 /* This function is called after each for loop in the AST generated
8359 * from test_ast_gen1.
8361 * Increment the number of calls and decrement the depth.
8362 * Check that the annotation attached to the node matches
8363 * the isl_id returned by the corresponding call to before_for.
8365 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
8366 __isl_keep isl_ast_build *build, void *user)
8368 struct isl_test_codegen_data *data = user;
8369 isl_id *id;
8370 const char *name;
8371 int valid;
8373 data->after++;
8374 data->depth--;
8376 if (data->after > data->before)
8377 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8378 "mismatch in number of for nodes",
8379 return isl_ast_node_free(node));
8381 id = isl_ast_node_get_annotation(node);
8382 if (!id)
8383 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8384 "missing annotation", return isl_ast_node_free(node));
8386 name = isl_id_get_name(id);
8387 valid = name && atoi(name + 1) == data->depth;
8388 isl_id_free(id);
8390 if (!valid)
8391 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8392 "wrong annotation", return isl_ast_node_free(node));
8394 return node;
8397 /* Check that the before_each_for and after_each_for callbacks
8398 * are called for each for loop in the generated code,
8399 * that they are called in the right order and that the isl_id
8400 * returned from the before_each_for callback is attached to
8401 * the isl_ast_node passed to the corresponding after_each_for call.
8403 static int test_ast_gen1(isl_ctx *ctx)
8405 const char *str;
8406 isl_set *set;
8407 isl_union_map *schedule;
8408 isl_ast_build *build;
8409 isl_ast_node *tree;
8410 struct isl_test_codegen_data data;
8412 str = "[N] -> { : N >= 10 }";
8413 set = isl_set_read_from_str(ctx, str);
8414 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
8415 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
8416 schedule = isl_union_map_read_from_str(ctx, str);
8418 data.before = 0;
8419 data.after = 0;
8420 data.depth = 0;
8421 build = isl_ast_build_from_context(set);
8422 build = isl_ast_build_set_before_each_for(build,
8423 &before_for, &data);
8424 build = isl_ast_build_set_after_each_for(build,
8425 &after_for, &data);
8426 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8427 isl_ast_build_free(build);
8428 if (!tree)
8429 return -1;
8431 isl_ast_node_free(tree);
8433 if (data.before != 3 || data.after != 3)
8434 isl_die(ctx, isl_error_unknown,
8435 "unexpected number of for nodes", return -1);
8437 return 0;
8440 /* Check that the AST generator handles domains that are integrally disjoint
8441 * but not rationally disjoint.
8443 static int test_ast_gen2(isl_ctx *ctx)
8445 const char *str;
8446 isl_set *set;
8447 isl_union_map *schedule;
8448 isl_union_map *options;
8449 isl_ast_build *build;
8450 isl_ast_node *tree;
8452 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
8453 schedule = isl_union_map_read_from_str(ctx, str);
8454 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8455 build = isl_ast_build_from_context(set);
8457 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
8458 options = isl_union_map_read_from_str(ctx, str);
8459 build = isl_ast_build_set_options(build, options);
8460 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8461 isl_ast_build_free(build);
8462 if (!tree)
8463 return -1;
8464 isl_ast_node_free(tree);
8466 return 0;
8469 /* Increment *user on each call.
8471 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
8472 __isl_keep isl_ast_build *build, void *user)
8474 int *n = user;
8476 (*n)++;
8478 return node;
8481 /* Test that unrolling tries to minimize the number of instances.
8482 * In particular, for the schedule given below, make sure it generates
8483 * 3 nodes (rather than 101).
8485 static int test_ast_gen3(isl_ctx *ctx)
8487 const char *str;
8488 isl_set *set;
8489 isl_union_map *schedule;
8490 isl_union_map *options;
8491 isl_ast_build *build;
8492 isl_ast_node *tree;
8493 int n_domain = 0;
8495 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
8496 schedule = isl_union_map_read_from_str(ctx, str);
8497 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8499 str = "{ [i] -> unroll[0] }";
8500 options = isl_union_map_read_from_str(ctx, str);
8502 build = isl_ast_build_from_context(set);
8503 build = isl_ast_build_set_options(build, options);
8504 build = isl_ast_build_set_at_each_domain(build,
8505 &count_domains, &n_domain);
8506 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8507 isl_ast_build_free(build);
8508 if (!tree)
8509 return -1;
8511 isl_ast_node_free(tree);
8513 if (n_domain != 3)
8514 isl_die(ctx, isl_error_unknown,
8515 "unexpected number of for nodes", return -1);
8517 return 0;
8520 /* Check that if the ast_build_exploit_nested_bounds options is set,
8521 * we do not get an outer if node in the generated AST,
8522 * while we do get such an outer if node if the options is not set.
8524 static int test_ast_gen4(isl_ctx *ctx)
8526 const char *str;
8527 isl_set *set;
8528 isl_union_map *schedule;
8529 isl_ast_build *build;
8530 isl_ast_node *tree;
8531 enum isl_ast_node_type type;
8532 int enb;
8534 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
8535 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
8537 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
8539 schedule = isl_union_map_read_from_str(ctx, str);
8540 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8541 build = isl_ast_build_from_context(set);
8542 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8543 isl_ast_build_free(build);
8544 if (!tree)
8545 return -1;
8547 type = isl_ast_node_get_type(tree);
8548 isl_ast_node_free(tree);
8550 if (type == isl_ast_node_if)
8551 isl_die(ctx, isl_error_unknown,
8552 "not expecting if node", return -1);
8554 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
8556 schedule = isl_union_map_read_from_str(ctx, str);
8557 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8558 build = isl_ast_build_from_context(set);
8559 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8560 isl_ast_build_free(build);
8561 if (!tree)
8562 return -1;
8564 type = isl_ast_node_get_type(tree);
8565 isl_ast_node_free(tree);
8567 if (type != isl_ast_node_if)
8568 isl_die(ctx, isl_error_unknown,
8569 "expecting if node", return -1);
8571 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
8573 return 0;
8576 /* This function is called for each leaf in the AST generated
8577 * from test_ast_gen5.
8579 * We finalize the AST generation by extending the outer schedule
8580 * with a zero-dimensional schedule. If this results in any for loops,
8581 * then this means that we did not pass along enough information
8582 * about the outer schedule to the inner AST generation.
8584 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
8585 void *user)
8587 isl_union_map *schedule, *extra;
8588 isl_ast_node *tree;
8590 schedule = isl_ast_build_get_schedule(build);
8591 extra = isl_union_map_copy(schedule);
8592 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
8593 schedule = isl_union_map_range_product(schedule, extra);
8594 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8595 isl_ast_build_free(build);
8597 if (!tree)
8598 return NULL;
8600 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
8601 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
8602 "code should not contain any for loop",
8603 return isl_ast_node_free(tree));
8605 return tree;
8608 /* Check that we do not lose any information when going back and
8609 * forth between internal and external schedule.
8611 * In particular, we create an AST where we unroll the only
8612 * non-constant dimension in the schedule. We therefore do
8613 * not expect any for loops in the AST. However, older versions
8614 * of isl would not pass along enough information about the outer
8615 * schedule when performing an inner code generation from a create_leaf
8616 * callback, resulting in the inner code generation producing a for loop.
8618 static int test_ast_gen5(isl_ctx *ctx)
8620 const char *str;
8621 isl_set *set;
8622 isl_union_map *schedule, *options;
8623 isl_ast_build *build;
8624 isl_ast_node *tree;
8626 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
8627 schedule = isl_union_map_read_from_str(ctx, str);
8629 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
8630 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
8631 options = isl_union_map_read_from_str(ctx, str);
8633 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8634 build = isl_ast_build_from_context(set);
8635 build = isl_ast_build_set_options(build, options);
8636 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
8637 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8638 isl_ast_build_free(build);
8639 isl_ast_node_free(tree);
8640 if (!tree)
8641 return -1;
8643 return 0;
8646 /* Check that the expression
8648 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
8650 * is not combined into
8652 * min(n/2, 0)
8654 * as this would result in n/2 being evaluated in parts of
8655 * the definition domain where n is not a multiple of 2.
8657 static int test_ast_expr(isl_ctx *ctx)
8659 const char *str;
8660 isl_pw_aff *pa;
8661 isl_ast_build *build;
8662 isl_ast_expr *expr;
8663 int min_max;
8664 int is_min;
8666 min_max = isl_options_get_ast_build_detect_min_max(ctx);
8667 isl_options_set_ast_build_detect_min_max(ctx, 1);
8669 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
8670 pa = isl_pw_aff_read_from_str(ctx, str);
8671 build = isl_ast_build_alloc(ctx);
8672 expr = isl_ast_build_expr_from_pw_aff(build, pa);
8673 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
8674 isl_ast_expr_get_op_type(expr) == isl_ast_expr_op_min;
8675 isl_ast_build_free(build);
8676 isl_ast_expr_free(expr);
8678 isl_options_set_ast_build_detect_min_max(ctx, min_max);
8680 if (!expr)
8681 return -1;
8682 if (is_min)
8683 isl_die(ctx, isl_error_unknown,
8684 "expressions should not be combined", return -1);
8686 return 0;
8689 static int test_ast_gen(isl_ctx *ctx)
8691 if (test_ast_gen1(ctx) < 0)
8692 return -1;
8693 if (test_ast_gen2(ctx) < 0)
8694 return -1;
8695 if (test_ast_gen3(ctx) < 0)
8696 return -1;
8697 if (test_ast_gen4(ctx) < 0)
8698 return -1;
8699 if (test_ast_gen5(ctx) < 0)
8700 return -1;
8701 if (test_ast_expr(ctx) < 0)
8702 return -1;
8703 return 0;
8706 /* Check if dropping output dimensions from an isl_pw_multi_aff
8707 * works properly.
8709 static int test_pw_multi_aff(isl_ctx *ctx)
8711 const char *str;
8712 isl_pw_multi_aff *pma1, *pma2;
8713 int equal;
8715 str = "{ [i,j] -> [i+j, 4i-j] }";
8716 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
8717 str = "{ [i,j] -> [4i-j] }";
8718 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
8720 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
8722 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
8724 isl_pw_multi_aff_free(pma1);
8725 isl_pw_multi_aff_free(pma2);
8726 if (equal < 0)
8727 return -1;
8728 if (!equal)
8729 isl_die(ctx, isl_error_unknown,
8730 "expressions not equal", return -1);
8732 return 0;
8735 /* Check that we can properly parse multi piecewise affine expressions
8736 * where the piecewise affine expressions have different domains.
8738 static int test_multi_pw_aff_1(isl_ctx *ctx)
8740 const char *str;
8741 isl_set *dom, *dom2;
8742 isl_multi_pw_aff *mpa1, *mpa2;
8743 isl_pw_aff *pa;
8744 int equal;
8745 int equal_domain;
8747 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
8748 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
8749 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
8750 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
8751 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
8752 str = "{ [i] -> [(i : i > 0), 2i] }";
8753 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
8755 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
8757 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
8758 dom = isl_pw_aff_domain(pa);
8759 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
8760 dom2 = isl_pw_aff_domain(pa);
8761 equal_domain = isl_set_is_equal(dom, dom2);
8763 isl_set_free(dom);
8764 isl_set_free(dom2);
8765 isl_multi_pw_aff_free(mpa1);
8766 isl_multi_pw_aff_free(mpa2);
8768 if (equal < 0)
8769 return -1;
8770 if (!equal)
8771 isl_die(ctx, isl_error_unknown,
8772 "expressions not equal", return -1);
8774 if (equal_domain < 0)
8775 return -1;
8776 if (equal_domain)
8777 isl_die(ctx, isl_error_unknown,
8778 "domains unexpectedly equal", return -1);
8780 return 0;
8783 /* Check that the dimensions in the explicit domain
8784 * of a multi piecewise affine expression are properly
8785 * taken into account.
8787 static int test_multi_pw_aff_2(isl_ctx *ctx)
8789 const char *str;
8790 isl_bool involves1, involves2, involves3, equal;
8791 isl_multi_pw_aff *mpa, *mpa1, *mpa2;
8793 str = "{ A[x,y] -> B[] : x >= y }";
8794 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
8795 involves1 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 2);
8796 mpa1 = isl_multi_pw_aff_copy(mpa);
8798 mpa = isl_multi_pw_aff_insert_dims(mpa, isl_dim_in, 0, 1);
8799 involves2 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 1);
8800 involves3 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 1, 2);
8801 str = "{ [a,x,y] -> B[] : x >= y }";
8802 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
8803 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
8804 isl_multi_pw_aff_free(mpa2);
8806 mpa = isl_multi_pw_aff_drop_dims(mpa, isl_dim_in, 0, 1);
8807 mpa = isl_multi_pw_aff_set_tuple_name(mpa, isl_dim_in, "A");
8808 if (equal >= 0 && equal)
8809 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa1);
8810 isl_multi_pw_aff_free(mpa1);
8811 isl_multi_pw_aff_free(mpa);
8813 if (involves1 < 0 || involves2 < 0 || involves3 < 0 || equal < 0)
8814 return -1;
8815 if (!equal)
8816 isl_die(ctx, isl_error_unknown,
8817 "incorrect result of dimension insertion/removal",
8818 return isl_stat_error);
8819 if (!involves1 || involves2 || !involves3)
8820 isl_die(ctx, isl_error_unknown,
8821 "incorrect characterization of involved dimensions",
8822 return isl_stat_error);
8824 return 0;
8827 /* Check that isl_multi_union_pw_aff_multi_val_on_domain
8828 * sets the explicit domain of a zero-dimensional result,
8829 * such that it can be converted to an isl_union_map.
8831 static isl_stat test_multi_pw_aff_3(isl_ctx *ctx)
8833 isl_space *space;
8834 isl_union_set *dom;
8835 isl_multi_val *mv;
8836 isl_multi_union_pw_aff *mupa;
8837 isl_union_map *umap;
8839 dom = isl_union_set_read_from_str(ctx, "{ A[]; B[] }");
8840 space = isl_union_set_get_space(dom);
8841 mv = isl_multi_val_zero(isl_space_set_from_params(space));
8842 mupa = isl_multi_union_pw_aff_multi_val_on_domain(dom, mv);
8843 umap = isl_union_map_from_multi_union_pw_aff(mupa);
8844 isl_union_map_free(umap);
8845 if (!umap)
8846 return isl_stat_error;
8848 return isl_stat_ok;
8851 /* Perform some tests on multi piecewise affine expressions.
8853 static int test_multi_pw_aff(isl_ctx *ctx)
8855 if (test_multi_pw_aff_1(ctx) < 0)
8856 return -1;
8857 if (test_multi_pw_aff_2(ctx) < 0)
8858 return -1;
8859 if (test_multi_pw_aff_3(ctx) < 0)
8860 return -1;
8861 return 0;
8864 /* This is a regression test for a bug where isl_basic_map_simplify
8865 * would end up in an infinite loop. In particular, we construct
8866 * an empty basic set that is not obviously empty.
8867 * isl_basic_set_is_empty marks the basic set as empty.
8868 * After projecting out i3, the variable can be dropped completely,
8869 * but isl_basic_map_simplify refrains from doing so if the basic set
8870 * is empty and would end up in an infinite loop if it didn't test
8871 * explicitly for empty basic maps in the outer loop.
8873 static int test_simplify_1(isl_ctx *ctx)
8875 const char *str;
8876 isl_basic_set *bset;
8877 int empty;
8879 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
8880 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
8881 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
8882 "i3 >= i2 }";
8883 bset = isl_basic_set_read_from_str(ctx, str);
8884 empty = isl_basic_set_is_empty(bset);
8885 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
8886 isl_basic_set_free(bset);
8887 if (!bset)
8888 return -1;
8889 if (!empty)
8890 isl_die(ctx, isl_error_unknown,
8891 "basic set should be empty", return -1);
8893 return 0;
8896 /* Check that the equality in the set description below
8897 * is simplified away.
8899 static int test_simplify_2(isl_ctx *ctx)
8901 const char *str;
8902 isl_basic_set *bset;
8903 isl_bool universe;
8905 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
8906 bset = isl_basic_set_read_from_str(ctx, str);
8907 universe = isl_basic_set_plain_is_universe(bset);
8908 isl_basic_set_free(bset);
8910 if (universe < 0)
8911 return -1;
8912 if (!universe)
8913 isl_die(ctx, isl_error_unknown,
8914 "equality not simplified away", return -1);
8915 return 0;
8918 /* Some simplification tests.
8920 static int test_simplify(isl_ctx *ctx)
8922 if (test_simplify_1(ctx) < 0)
8923 return -1;
8924 if (test_simplify_2(ctx) < 0)
8925 return -1;
8926 return 0;
8929 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
8930 * with gbr context would fail to disable the use of the shifted tableau
8931 * when transferring equalities for the input to the context, resulting
8932 * in invalid sample values.
8934 static int test_partial_lexmin(isl_ctx *ctx)
8936 const char *str;
8937 isl_basic_set *bset;
8938 isl_basic_map *bmap;
8939 isl_map *map;
8941 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
8942 bmap = isl_basic_map_read_from_str(ctx, str);
8943 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
8944 bset = isl_basic_set_read_from_str(ctx, str);
8945 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
8946 isl_map_free(map);
8948 if (!map)
8949 return -1;
8951 return 0;
8954 /* Check that the variable compression performed on the existentially
8955 * quantified variables inside isl_basic_set_compute_divs is not confused
8956 * by the implicit equalities among the parameters.
8958 static int test_compute_divs(isl_ctx *ctx)
8960 const char *str;
8961 isl_basic_set *bset;
8962 isl_set *set;
8964 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
8965 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
8966 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
8967 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
8968 bset = isl_basic_set_read_from_str(ctx, str);
8969 set = isl_basic_set_compute_divs(bset);
8970 isl_set_free(set);
8971 if (!set)
8972 return -1;
8974 return 0;
8977 /* Check that isl_schedule_get_map is not confused by a schedule tree
8978 * with divergent filter node parameters, as can result from a call
8979 * to isl_schedule_intersect_domain.
8981 static int test_schedule_tree(isl_ctx *ctx)
8983 const char *str;
8984 isl_union_set *uset;
8985 isl_schedule *sched1, *sched2;
8986 isl_union_map *umap;
8988 uset = isl_union_set_read_from_str(ctx, "{ A[i] }");
8989 sched1 = isl_schedule_from_domain(uset);
8990 uset = isl_union_set_read_from_str(ctx, "{ B[] }");
8991 sched2 = isl_schedule_from_domain(uset);
8993 sched1 = isl_schedule_sequence(sched1, sched2);
8994 str = "[n] -> { A[i] : 0 <= i < n; B[] }";
8995 uset = isl_union_set_read_from_str(ctx, str);
8996 sched1 = isl_schedule_intersect_domain(sched1, uset);
8997 umap = isl_schedule_get_map(sched1);
8998 isl_schedule_free(sched1);
8999 isl_union_map_free(umap);
9000 if (!umap)
9001 return -1;
9003 return 0;
9006 /* Check that a zero-dimensional prefix schedule keeps track
9007 * of the domain and outer filters.
9009 static int test_schedule_tree_prefix(isl_ctx *ctx)
9011 const char *str;
9012 isl_bool equal;
9013 isl_union_set *uset;
9014 isl_union_set_list *filters;
9015 isl_multi_union_pw_aff *mupa, *mupa2;
9016 isl_schedule_node *node;
9018 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
9019 uset = isl_union_set_read_from_str(ctx, str);
9020 node = isl_schedule_node_from_domain(uset);
9021 node = isl_schedule_node_child(node, 0);
9023 str = "{ S1[i,j] : i > j }";
9024 uset = isl_union_set_read_from_str(ctx, str);
9025 filters = isl_union_set_list_from_union_set(uset);
9026 str = "{ S1[i,j] : i <= j; S2[i,j] }";
9027 uset = isl_union_set_read_from_str(ctx, str);
9028 filters = isl_union_set_list_add(filters, uset);
9029 node = isl_schedule_node_insert_sequence(node, filters);
9031 node = isl_schedule_node_child(node, 0);
9032 node = isl_schedule_node_child(node, 0);
9033 mupa = isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
9034 str = "([] : { S1[i,j] : i > j })";
9035 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx, str);
9036 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
9037 isl_multi_union_pw_aff_free(mupa2);
9038 isl_multi_union_pw_aff_free(mupa);
9039 isl_schedule_node_free(node);
9041 if (equal < 0)
9042 return -1;
9043 if (!equal)
9044 isl_die(ctx, isl_error_unknown, "unexpected prefix schedule",
9045 return -1);
9047 return 0;
9050 /* Check that the reaching domain elements and the prefix schedule
9051 * at a leaf node are the same before and after grouping.
9053 static int test_schedule_tree_group_1(isl_ctx *ctx)
9055 int equal;
9056 const char *str;
9057 isl_id *id;
9058 isl_union_set *uset;
9059 isl_multi_union_pw_aff *mupa;
9060 isl_union_pw_multi_aff *upma1, *upma2;
9061 isl_union_set *domain1, *domain2;
9062 isl_union_map *umap1, *umap2;
9063 isl_schedule_node *node;
9065 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
9066 uset = isl_union_set_read_from_str(ctx, str);
9067 node = isl_schedule_node_from_domain(uset);
9068 node = isl_schedule_node_child(node, 0);
9069 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
9070 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9071 node = isl_schedule_node_insert_partial_schedule(node, mupa);
9072 node = isl_schedule_node_child(node, 0);
9073 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
9074 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9075 node = isl_schedule_node_insert_partial_schedule(node, mupa);
9076 node = isl_schedule_node_child(node, 0);
9077 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
9078 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
9079 domain1 = isl_schedule_node_get_domain(node);
9080 id = isl_id_alloc(ctx, "group", NULL);
9081 node = isl_schedule_node_parent(node);
9082 node = isl_schedule_node_group(node, id);
9083 node = isl_schedule_node_child(node, 0);
9084 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
9085 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
9086 domain2 = isl_schedule_node_get_domain(node);
9087 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
9088 if (equal >= 0 && equal)
9089 equal = isl_union_set_is_equal(domain1, domain2);
9090 if (equal >= 0 && equal)
9091 equal = isl_union_map_is_equal(umap1, umap2);
9092 isl_union_map_free(umap1);
9093 isl_union_map_free(umap2);
9094 isl_union_set_free(domain1);
9095 isl_union_set_free(domain2);
9096 isl_union_pw_multi_aff_free(upma1);
9097 isl_union_pw_multi_aff_free(upma2);
9098 isl_schedule_node_free(node);
9100 if (equal < 0)
9101 return -1;
9102 if (!equal)
9103 isl_die(ctx, isl_error_unknown,
9104 "expressions not equal", return -1);
9106 return 0;
9109 /* Check that we can have nested groupings and that the union map
9110 * schedule representation is the same before and after the grouping.
9111 * Note that after the grouping, the union map representation contains
9112 * the domain constraints from the ranges of the expansion nodes,
9113 * while they are missing from the union map representation of
9114 * the tree without expansion nodes.
9116 * Also check that the global expansion is as expected.
9118 static int test_schedule_tree_group_2(isl_ctx *ctx)
9120 int equal, equal_expansion;
9121 const char *str;
9122 isl_id *id;
9123 isl_union_set *uset;
9124 isl_union_map *umap1, *umap2;
9125 isl_union_map *expansion1, *expansion2;
9126 isl_union_set_list *filters;
9127 isl_multi_union_pw_aff *mupa;
9128 isl_schedule *schedule;
9129 isl_schedule_node *node;
9131 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
9132 "S3[i,j] : 0 <= i,j < 10 }";
9133 uset = isl_union_set_read_from_str(ctx, str);
9134 node = isl_schedule_node_from_domain(uset);
9135 node = isl_schedule_node_child(node, 0);
9136 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
9137 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9138 node = isl_schedule_node_insert_partial_schedule(node, mupa);
9139 node = isl_schedule_node_child(node, 0);
9140 str = "{ S1[i,j] }";
9141 uset = isl_union_set_read_from_str(ctx, str);
9142 filters = isl_union_set_list_from_union_set(uset);
9143 str = "{ S2[i,j]; S3[i,j] }";
9144 uset = isl_union_set_read_from_str(ctx, str);
9145 filters = isl_union_set_list_add(filters, uset);
9146 node = isl_schedule_node_insert_sequence(node, filters);
9147 node = isl_schedule_node_child(node, 1);
9148 node = isl_schedule_node_child(node, 0);
9149 str = "{ S2[i,j] }";
9150 uset = isl_union_set_read_from_str(ctx, str);
9151 filters = isl_union_set_list_from_union_set(uset);
9152 str = "{ S3[i,j] }";
9153 uset = isl_union_set_read_from_str(ctx, str);
9154 filters = isl_union_set_list_add(filters, uset);
9155 node = isl_schedule_node_insert_sequence(node, filters);
9157 schedule = isl_schedule_node_get_schedule(node);
9158 umap1 = isl_schedule_get_map(schedule);
9159 uset = isl_schedule_get_domain(schedule);
9160 umap1 = isl_union_map_intersect_domain(umap1, uset);
9161 isl_schedule_free(schedule);
9163 node = isl_schedule_node_parent(node);
9164 node = isl_schedule_node_parent(node);
9165 id = isl_id_alloc(ctx, "group1", NULL);
9166 node = isl_schedule_node_group(node, id);
9167 node = isl_schedule_node_child(node, 1);
9168 node = isl_schedule_node_child(node, 0);
9169 id = isl_id_alloc(ctx, "group2", NULL);
9170 node = isl_schedule_node_group(node, id);
9172 schedule = isl_schedule_node_get_schedule(node);
9173 umap2 = isl_schedule_get_map(schedule);
9174 isl_schedule_free(schedule);
9176 node = isl_schedule_node_root(node);
9177 node = isl_schedule_node_child(node, 0);
9178 expansion1 = isl_schedule_node_get_subtree_expansion(node);
9179 isl_schedule_node_free(node);
9181 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
9182 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
9183 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
9185 expansion2 = isl_union_map_read_from_str(ctx, str);
9187 equal = isl_union_map_is_equal(umap1, umap2);
9188 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
9190 isl_union_map_free(umap1);
9191 isl_union_map_free(umap2);
9192 isl_union_map_free(expansion1);
9193 isl_union_map_free(expansion2);
9195 if (equal < 0 || equal_expansion < 0)
9196 return -1;
9197 if (!equal)
9198 isl_die(ctx, isl_error_unknown,
9199 "expressions not equal", return -1);
9200 if (!equal_expansion)
9201 isl_die(ctx, isl_error_unknown,
9202 "unexpected expansion", return -1);
9204 return 0;
9207 /* Some tests for the isl_schedule_node_group function.
9209 static int test_schedule_tree_group(isl_ctx *ctx)
9211 if (test_schedule_tree_group_1(ctx) < 0)
9212 return -1;
9213 if (test_schedule_tree_group_2(ctx) < 0)
9214 return -1;
9215 return 0;
9218 struct {
9219 const char *set;
9220 const char *dual;
9221 } coef_tests[] = {
9222 { "{ rat: [i] : 0 <= i <= 10 }",
9223 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
9224 { "{ rat: [i] : FALSE }",
9225 "{ rat: coefficients[[cst] -> [a]] }" },
9226 { "{ rat: [i] : }",
9227 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
9230 struct {
9231 const char *set;
9232 const char *dual;
9233 } sol_tests[] = {
9234 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
9235 "{ rat: [i] : 0 <= i <= 10 }" },
9236 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
9237 "{ rat: [i] }" },
9238 { "{ rat: coefficients[[cst] -> [a]] }",
9239 "{ rat: [i] : FALSE }" },
9242 /* Test the basic functionality of isl_basic_set_coefficients and
9243 * isl_basic_set_solutions.
9245 static int test_dual(isl_ctx *ctx)
9247 int i;
9249 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
9250 int equal;
9251 isl_basic_set *bset1, *bset2;
9253 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
9254 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
9255 bset1 = isl_basic_set_coefficients(bset1);
9256 equal = isl_basic_set_is_equal(bset1, bset2);
9257 isl_basic_set_free(bset1);
9258 isl_basic_set_free(bset2);
9259 if (equal < 0)
9260 return -1;
9261 if (!equal)
9262 isl_die(ctx, isl_error_unknown,
9263 "incorrect dual", return -1);
9266 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
9267 int equal;
9268 isl_basic_set *bset1, *bset2;
9270 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
9271 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
9272 bset1 = isl_basic_set_solutions(bset1);
9273 equal = isl_basic_set_is_equal(bset1, bset2);
9274 isl_basic_set_free(bset1);
9275 isl_basic_set_free(bset2);
9276 if (equal < 0)
9277 return -1;
9278 if (!equal)
9279 isl_die(ctx, isl_error_unknown,
9280 "incorrect dual", return -1);
9283 return 0;
9286 struct {
9287 int scale_tile;
9288 int shift_point;
9289 const char *domain;
9290 const char *schedule;
9291 const char *sizes;
9292 const char *tile;
9293 const char *point;
9294 } tile_tests[] = {
9295 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
9296 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9297 "{ [32,32] }",
9298 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
9299 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9301 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
9302 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9303 "{ [32,32] }",
9304 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9305 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9307 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9308 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9309 "{ [32,32] }",
9310 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
9311 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9313 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9314 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9315 "{ [32,32] }",
9316 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9317 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9321 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
9322 * tile the band and then check if the tile and point bands have the
9323 * expected partial schedule.
9325 static int test_tile(isl_ctx *ctx)
9327 int i;
9328 int scale;
9329 int shift;
9331 scale = isl_options_get_tile_scale_tile_loops(ctx);
9332 shift = isl_options_get_tile_shift_point_loops(ctx);
9334 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
9335 int opt;
9336 int equal;
9337 const char *str;
9338 isl_union_set *domain;
9339 isl_multi_union_pw_aff *mupa, *mupa2;
9340 isl_schedule_node *node;
9341 isl_multi_val *sizes;
9343 opt = tile_tests[i].scale_tile;
9344 isl_options_set_tile_scale_tile_loops(ctx, opt);
9345 opt = tile_tests[i].shift_point;
9346 isl_options_set_tile_shift_point_loops(ctx, opt);
9348 str = tile_tests[i].domain;
9349 domain = isl_union_set_read_from_str(ctx, str);
9350 node = isl_schedule_node_from_domain(domain);
9351 node = isl_schedule_node_child(node, 0);
9352 str = tile_tests[i].schedule;
9353 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9354 node = isl_schedule_node_insert_partial_schedule(node, mupa);
9355 str = tile_tests[i].sizes;
9356 sizes = isl_multi_val_read_from_str(ctx, str);
9357 node = isl_schedule_node_band_tile(node, sizes);
9359 str = tile_tests[i].tile;
9360 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9361 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9362 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
9363 isl_multi_union_pw_aff_free(mupa);
9364 isl_multi_union_pw_aff_free(mupa2);
9366 node = isl_schedule_node_child(node, 0);
9368 str = tile_tests[i].point;
9369 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9370 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9371 if (equal >= 0 && equal)
9372 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
9373 mupa2);
9374 isl_multi_union_pw_aff_free(mupa);
9375 isl_multi_union_pw_aff_free(mupa2);
9377 isl_schedule_node_free(node);
9379 if (equal < 0)
9380 return -1;
9381 if (!equal)
9382 isl_die(ctx, isl_error_unknown,
9383 "unexpected result", return -1);
9386 isl_options_set_tile_scale_tile_loops(ctx, scale);
9387 isl_options_set_tile_shift_point_loops(ctx, shift);
9389 return 0;
9392 /* Check that the domain hash of a space is equal to the hash
9393 * of the domain of the space.
9395 static int test_domain_hash(isl_ctx *ctx)
9397 isl_map *map;
9398 isl_space *space;
9399 uint32_t hash1, hash2;
9401 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
9402 space = isl_map_get_space(map);
9403 isl_map_free(map);
9404 hash1 = isl_space_get_domain_hash(space);
9405 space = isl_space_domain(space);
9406 hash2 = isl_space_get_hash(space);
9407 isl_space_free(space);
9409 if (!space)
9410 return -1;
9411 if (hash1 != hash2)
9412 isl_die(ctx, isl_error_unknown,
9413 "domain hash not equal to hash of domain", return -1);
9415 return 0;
9418 /* Check that a universe basic set that is not obviously equal to the universe
9419 * is still recognized as being equal to the universe.
9421 static int test_universe(isl_ctx *ctx)
9423 const char *s;
9424 isl_basic_set *bset;
9425 isl_bool is_univ;
9427 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
9428 bset = isl_basic_set_read_from_str(ctx, s);
9429 is_univ = isl_basic_set_is_universe(bset);
9430 isl_basic_set_free(bset);
9432 if (is_univ < 0)
9433 return -1;
9434 if (!is_univ)
9435 isl_die(ctx, isl_error_unknown,
9436 "not recognized as universe set", return -1);
9438 return 0;
9441 /* Sets for which chambers are computed and checked.
9443 const char *chambers_tests[] = {
9444 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
9445 "z >= 0 and z <= C - y and z <= B - x - y }",
9448 /* Add the domain of "cell" to "cells".
9450 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
9452 isl_basic_set_list **cells = user;
9453 isl_basic_set *dom;
9455 dom = isl_cell_get_domain(cell);
9456 isl_cell_free(cell);
9457 *cells = isl_basic_set_list_add(*cells, dom);
9459 return *cells ? isl_stat_ok : isl_stat_error;
9462 /* Check that the elements of "list" are pairwise disjoint.
9464 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
9466 int i, j;
9467 isl_size n;
9469 n = isl_basic_set_list_n_basic_set(list);
9470 if (n < 0)
9471 return isl_stat_error;
9473 for (i = 0; i < n; ++i) {
9474 isl_basic_set *bset_i;
9476 bset_i = isl_basic_set_list_get_basic_set(list, i);
9477 for (j = i + 1; j < n; ++j) {
9478 isl_basic_set *bset_j;
9479 isl_bool disjoint;
9481 bset_j = isl_basic_set_list_get_basic_set(list, j);
9482 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
9483 isl_basic_set_free(bset_j);
9484 if (!disjoint)
9485 isl_die(isl_basic_set_list_get_ctx(list),
9486 isl_error_unknown, "not disjoint",
9487 break);
9488 if (disjoint < 0 || !disjoint)
9489 break;
9491 isl_basic_set_free(bset_i);
9492 if (j < n)
9493 return isl_stat_error;
9496 return isl_stat_ok;
9499 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
9500 * are pairwise disjoint.
9502 static int test_chambers(isl_ctx *ctx)
9504 int i;
9506 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
9507 isl_basic_set *bset;
9508 isl_vertices *vertices;
9509 isl_basic_set_list *cells;
9510 isl_stat ok;
9512 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
9513 vertices = isl_basic_set_compute_vertices(bset);
9514 cells = isl_basic_set_list_alloc(ctx, 0);
9515 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
9516 &cells) < 0)
9517 cells = isl_basic_set_list_free(cells);
9518 ok = check_pairwise_disjoint(cells);
9519 isl_basic_set_list_free(cells);
9520 isl_vertices_free(vertices);
9521 isl_basic_set_free(bset);
9523 if (ok < 0)
9524 return -1;
9527 return 0;
9530 struct {
9531 const char *name;
9532 int (*fn)(isl_ctx *ctx);
9533 } tests [] = {
9534 { "universe", &test_universe },
9535 { "domain hash", &test_domain_hash },
9536 { "dual", &test_dual },
9537 { "dependence analysis", &test_flow },
9538 { "val", &test_val },
9539 { "compute divs", &test_compute_divs },
9540 { "partial lexmin", &test_partial_lexmin },
9541 { "simplify", &test_simplify },
9542 { "curry", &test_curry },
9543 { "piecewise multi affine expressions", &test_pw_multi_aff },
9544 { "multi piecewise affine expressions", &test_multi_pw_aff },
9545 { "conversion", &test_conversion },
9546 { "list", &test_list },
9547 { "align parameters", &test_align_parameters },
9548 { "drop unused parameters", &test_drop_unused_parameters },
9549 { "preimage", &test_preimage },
9550 { "pullback", &test_pullback },
9551 { "AST", &test_ast },
9552 { "AST build", &test_ast_build },
9553 { "AST generation", &test_ast_gen },
9554 { "eliminate", &test_eliminate },
9555 { "deltas_map", &test_deltas_map },
9556 { "residue class", &test_residue_class },
9557 { "div", &test_div },
9558 { "slice", &test_slice },
9559 { "fixed power", &test_fixed_power },
9560 { "sample", &test_sample },
9561 { "output", &test_output },
9562 { "vertices", &test_vertices },
9563 { "chambers", &test_chambers },
9564 { "fixed", &test_fixed },
9565 { "equal", &test_equal },
9566 { "disjoint", &test_disjoint },
9567 { "product", &test_product },
9568 { "dim_max", &test_dim_max },
9569 { "affine", &test_aff },
9570 { "injective", &test_injective },
9571 { "schedule (whole component)", &test_schedule_whole },
9572 { "schedule (incremental)", &test_schedule_incremental },
9573 { "schedule tree", &test_schedule_tree },
9574 { "schedule tree prefix", &test_schedule_tree_prefix },
9575 { "schedule tree grouping", &test_schedule_tree_group },
9576 { "tile", &test_tile },
9577 { "union_pw", &test_union_pw },
9578 { "locus", &test_locus },
9579 { "eval", &test_eval },
9580 { "parse", &test_parse },
9581 { "single-valued", &test_sv },
9582 { "affine hull", &test_affine_hull },
9583 { "simple_hull", &test_simple_hull },
9584 { "coalesce", &test_coalesce },
9585 { "factorize", &test_factorize },
9586 { "subset", &test_subset },
9587 { "subtract", &test_subtract },
9588 { "intersect", &test_intersect },
9589 { "lexmin", &test_lexmin },
9590 { "min", &test_min },
9591 { "gist", &test_gist },
9592 { "piecewise quasi-polynomials", &test_pwqp },
9593 { "lift", &test_lift },
9594 { "unbind parameters", &test_unbind },
9595 { "bound", &test_bound },
9596 { "get lists", &test_get_list },
9597 { "union", &test_union },
9598 { "split periods", &test_split_periods },
9599 { "lexicographic order", &test_lex },
9600 { "bijectivity", &test_bijective },
9601 { "dataflow analysis", &test_dep },
9602 { "reading", &test_read },
9603 { "bounded", &test_bounded },
9604 { "construction", &test_construction },
9605 { "dimension manipulation", &test_dim },
9606 { "map application", &test_application },
9607 { "convex hull", &test_convex_hull },
9608 { "transitive closure", &test_closure },
9609 { "isl_bool", &test_isl_bool},
9612 int main(int argc, char **argv)
9614 int i;
9615 struct isl_ctx *ctx;
9616 struct isl_options *options;
9618 options = isl_options_new_with_defaults();
9619 assert(options);
9620 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
9622 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
9623 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
9624 printf("%s\n", tests[i].name);
9625 if (tests[i].fn(ctx) < 0)
9626 goto error;
9628 isl_ctx_free(ctx);
9629 return 0;
9630 error:
9631 isl_ctx_free(ctx);
9632 return -1;