isl_aff.c: fix typos in comments
[isl.git] / isl_test.c
blob877d5409099b32b44162f627132e3547d52bf310
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] }" },
383 { "{ [x=4:5] -> [x + 1] }",
384 "{ [x] -> [x + 1] : 4 <= x <= 5 }" },
385 { "{ [x=4:5] -> [x + 1 : x + 1] }",
386 "{ [x=4:5] -> [x + 1] }" },
387 { "{ [x] -> [x - 1 : x + 1] }",
388 "{ [x] -> [y] : x - 1 <= y <= x + 1 }" },
391 int test_parse(struct isl_ctx *ctx)
393 int i;
394 isl_map *map, *map2;
395 const char *str, *str2;
397 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
398 return -1;
399 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
400 return -1;
401 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
402 return -1;
403 if (test_parse_multi(ctx) < 0)
404 return -1;
405 if (test_parse_pma(ctx) < 0)
406 return -1;
408 str = "{ [i] -> [-i] }";
409 map = isl_map_read_from_str(ctx, str);
410 assert(map);
411 isl_map_free(map);
413 str = "{ A[i] -> L[([i/3])] }";
414 map = isl_map_read_from_str(ctx, str);
415 assert(map);
416 isl_map_free(map);
418 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
419 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
420 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
422 for (i = 0; i < ARRAY_SIZE(parse_map_equal_tests); ++i) {
423 str = parse_map_equal_tests[i].map1;
424 str2 = parse_map_equal_tests[i].map2;
425 if (test_parse_map_equal(ctx, str, str2) < 0)
426 return -1;
429 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
430 map = isl_map_read_from_str(ctx, str);
431 str = "{ [new, old] -> [o0, o1] : "
432 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
433 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
434 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
435 map2 = isl_map_read_from_str(ctx, str);
436 assert(isl_map_is_equal(map, map2));
437 isl_map_free(map);
438 isl_map_free(map2);
440 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
441 map = isl_map_read_from_str(ctx, str);
442 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
443 map2 = isl_map_read_from_str(ctx, str);
444 assert(isl_map_is_equal(map, map2));
445 isl_map_free(map);
446 isl_map_free(map2);
448 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
449 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
450 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
451 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
452 test_parse_pwaff(ctx, "{ [] -> [(100)] }");
454 return 0;
457 static int test_read(isl_ctx *ctx)
459 char *filename;
460 FILE *input;
461 isl_basic_set *bset1, *bset2;
462 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
463 int equal;
465 filename = get_filename(ctx, "set", "omega");
466 assert(filename);
467 input = fopen(filename, "r");
468 assert(input);
470 bset1 = isl_basic_set_read_from_file(ctx, input);
471 bset2 = isl_basic_set_read_from_str(ctx, str);
473 equal = isl_basic_set_is_equal(bset1, bset2);
475 isl_basic_set_free(bset1);
476 isl_basic_set_free(bset2);
477 free(filename);
479 fclose(input);
481 if (equal < 0)
482 return -1;
483 if (!equal)
484 isl_die(ctx, isl_error_unknown,
485 "read sets not equal", return -1);
487 return 0;
490 static int test_bounded(isl_ctx *ctx)
492 isl_set *set;
493 isl_bool bounded;
495 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
496 bounded = isl_set_is_bounded(set);
497 isl_set_free(set);
499 if (bounded < 0)
500 return -1;
501 if (!bounded)
502 isl_die(ctx, isl_error_unknown,
503 "set not considered bounded", return -1);
505 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
506 bounded = isl_set_is_bounded(set);
507 assert(!bounded);
508 isl_set_free(set);
510 if (bounded < 0)
511 return -1;
512 if (bounded)
513 isl_die(ctx, isl_error_unknown,
514 "set considered bounded", return -1);
516 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
517 bounded = isl_set_is_bounded(set);
518 isl_set_free(set);
520 if (bounded < 0)
521 return -1;
522 if (bounded)
523 isl_die(ctx, isl_error_unknown,
524 "set considered bounded", return -1);
526 return 0;
529 /* Construct the basic set { [i] : 5 <= i <= N } */
530 static int test_construction_1(isl_ctx *ctx)
532 isl_space *dim;
533 isl_local_space *ls;
534 isl_basic_set *bset;
535 isl_constraint *c;
537 dim = isl_space_set_alloc(ctx, 1, 1);
538 bset = isl_basic_set_universe(isl_space_copy(dim));
539 ls = isl_local_space_from_space(dim);
541 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
542 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
543 c = isl_constraint_set_coefficient_si(c, isl_dim_param, 0, 1);
544 bset = isl_basic_set_add_constraint(bset, c);
546 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
547 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
548 c = isl_constraint_set_constant_si(c, -5);
549 bset = isl_basic_set_add_constraint(bset, c);
551 isl_local_space_free(ls);
552 isl_basic_set_free(bset);
554 return 0;
557 /* Construct the basic set { [x] : -100 <= x <= 100 }
558 * using isl_basic_set_{lower,upper}_bound_val and
559 * check that it is equal the same basic set parsed from a string.
561 static int test_construction_2(isl_ctx *ctx)
563 isl_bool equal;
564 isl_val *v;
565 isl_space *space;
566 isl_basic_set *bset1, *bset2;
568 v = isl_val_int_from_si(ctx, 100);
569 space = isl_space_set_alloc(ctx, 0, 1);
570 bset1 = isl_basic_set_universe(space);
571 bset1 = isl_basic_set_upper_bound_val(bset1, isl_dim_set, 0,
572 isl_val_copy(v));
573 bset1 = isl_basic_set_lower_bound_val(bset1, isl_dim_set, 0,
574 isl_val_neg(v));
575 bset2 = isl_basic_set_read_from_str(ctx, "{ [x] : -100 <= x <= 100 }");
576 equal = isl_basic_set_is_equal(bset1, bset2);
577 isl_basic_set_free(bset1);
578 isl_basic_set_free(bset2);
580 if (equal < 0)
581 return -1;
582 if (!equal)
583 isl_die(ctx, isl_error_unknown,
584 "failed construction", return -1);
586 return 0;
589 /* Basic tests for constructing basic sets.
591 static int test_construction(isl_ctx *ctx)
593 if (test_construction_1(ctx) < 0)
594 return -1;
595 if (test_construction_2(ctx) < 0)
596 return -1;
597 return 0;
600 static int test_dim(isl_ctx *ctx)
602 const char *str;
603 isl_map *map1, *map2;
604 int equal;
606 map1 = isl_map_read_from_str(ctx,
607 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
608 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
609 map2 = isl_map_read_from_str(ctx,
610 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
611 equal = isl_map_is_equal(map1, map2);
612 isl_map_free(map2);
614 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
615 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
616 if (equal >= 0 && equal)
617 equal = isl_map_is_equal(map1, map2);
619 isl_map_free(map1);
620 isl_map_free(map2);
622 if (equal < 0)
623 return -1;
624 if (!equal)
625 isl_die(ctx, isl_error_unknown,
626 "unexpected result", return -1);
628 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
629 map1 = isl_map_read_from_str(ctx, str);
630 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
631 map2 = isl_map_read_from_str(ctx, str);
632 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
633 equal = isl_map_is_equal(map1, map2);
634 isl_map_free(map1);
635 isl_map_free(map2);
637 if (equal < 0)
638 return -1;
639 if (!equal)
640 isl_die(ctx, isl_error_unknown,
641 "unexpected result", return -1);
643 return 0;
646 /* Check that "val" is equal to the value described by "str".
647 * If "str" is "NaN", then check for a NaN value explicitly.
649 static isl_stat val_check_equal(__isl_keep isl_val *val, const char *str)
651 isl_bool ok, is_nan;
652 isl_ctx *ctx;
653 isl_val *res;
655 if (!val)
656 return isl_stat_error;
658 ctx = isl_val_get_ctx(val);
659 res = isl_val_read_from_str(ctx, str);
660 is_nan = isl_val_is_nan(res);
661 if (is_nan < 0)
662 ok = isl_bool_error;
663 else if (is_nan)
664 ok = isl_val_is_nan(val);
665 else
666 ok = isl_val_eq(val, res);
667 isl_val_free(res);
668 if (ok < 0)
669 return isl_stat_error;
670 if (!ok)
671 isl_die(ctx, isl_error_unknown,
672 "unexpected result", return isl_stat_error);
673 return isl_stat_ok;
676 struct {
677 __isl_give isl_val *(*op)(__isl_take isl_val *v);
678 const char *arg;
679 const char *res;
680 } val_un_tests[] = {
681 { &isl_val_neg, "0", "0" },
682 { &isl_val_abs, "0", "0" },
683 { &isl_val_pow2, "0", "1" },
684 { &isl_val_floor, "0", "0" },
685 { &isl_val_ceil, "0", "0" },
686 { &isl_val_neg, "1", "-1" },
687 { &isl_val_neg, "-1", "1" },
688 { &isl_val_neg, "1/2", "-1/2" },
689 { &isl_val_neg, "-1/2", "1/2" },
690 { &isl_val_neg, "infty", "-infty" },
691 { &isl_val_neg, "-infty", "infty" },
692 { &isl_val_neg, "NaN", "NaN" },
693 { &isl_val_abs, "1", "1" },
694 { &isl_val_abs, "-1", "1" },
695 { &isl_val_abs, "1/2", "1/2" },
696 { &isl_val_abs, "-1/2", "1/2" },
697 { &isl_val_abs, "infty", "infty" },
698 { &isl_val_abs, "-infty", "infty" },
699 { &isl_val_abs, "NaN", "NaN" },
700 { &isl_val_floor, "1", "1" },
701 { &isl_val_floor, "-1", "-1" },
702 { &isl_val_floor, "1/2", "0" },
703 { &isl_val_floor, "-1/2", "-1" },
704 { &isl_val_floor, "infty", "infty" },
705 { &isl_val_floor, "-infty", "-infty" },
706 { &isl_val_floor, "NaN", "NaN" },
707 { &isl_val_ceil, "1", "1" },
708 { &isl_val_ceil, "-1", "-1" },
709 { &isl_val_ceil, "1/2", "1" },
710 { &isl_val_ceil, "-1/2", "0" },
711 { &isl_val_ceil, "infty", "infty" },
712 { &isl_val_ceil, "-infty", "-infty" },
713 { &isl_val_ceil, "NaN", "NaN" },
714 { &isl_val_pow2, "-3", "1/8" },
715 { &isl_val_pow2, "-1", "1/2" },
716 { &isl_val_pow2, "1", "2" },
717 { &isl_val_pow2, "2", "4" },
718 { &isl_val_pow2, "3", "8" },
719 { &isl_val_inv, "1", "1" },
720 { &isl_val_inv, "2", "1/2" },
721 { &isl_val_inv, "1/2", "2" },
722 { &isl_val_inv, "-2", "-1/2" },
723 { &isl_val_inv, "-1/2", "-2" },
724 { &isl_val_inv, "0", "NaN" },
725 { &isl_val_inv, "NaN", "NaN" },
726 { &isl_val_inv, "infty", "0" },
727 { &isl_val_inv, "-infty", "0" },
730 /* Perform some basic tests of unary operations on isl_val objects.
732 static int test_un_val(isl_ctx *ctx)
734 int i;
735 isl_val *v;
736 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
738 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
739 isl_stat r;
741 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
742 fn = val_un_tests[i].op;
743 v = fn(v);
744 r = val_check_equal(v, val_un_tests[i].res);
745 isl_val_free(v);
746 if (r < 0)
747 return -1;
750 return 0;
753 struct {
754 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
755 __isl_take isl_val *v2);
756 } val_bin_op[] = {
757 ['+'] = { &isl_val_add },
758 ['-'] = { &isl_val_sub },
759 ['*'] = { &isl_val_mul },
760 ['/'] = { &isl_val_div },
761 ['g'] = { &isl_val_gcd },
762 ['m'] = { &isl_val_min },
763 ['M'] = { &isl_val_max },
766 struct {
767 const char *arg1;
768 unsigned char op;
769 const char *arg2;
770 const char *res;
771 } val_bin_tests[] = {
772 { "0", '+', "0", "0" },
773 { "1", '+', "0", "1" },
774 { "1", '+', "1", "2" },
775 { "1", '-', "1", "0" },
776 { "1", '*', "1", "1" },
777 { "1", '/', "1", "1" },
778 { "2", '*', "3", "6" },
779 { "2", '*', "1/2", "1" },
780 { "2", '*', "1/3", "2/3" },
781 { "2/3", '*', "3/5", "2/5" },
782 { "2/3", '*', "7/5", "14/15" },
783 { "2", '/', "1/2", "4" },
784 { "-2", '/', "-1/2", "4" },
785 { "-2", '/', "1/2", "-4" },
786 { "2", '/', "-1/2", "-4" },
787 { "2", '/', "2", "1" },
788 { "2", '/', "3", "2/3" },
789 { "2/3", '/', "5/3", "2/5" },
790 { "2/3", '/', "5/7", "14/15" },
791 { "0", '/', "0", "NaN" },
792 { "42", '/', "0", "NaN" },
793 { "-42", '/', "0", "NaN" },
794 { "infty", '/', "0", "NaN" },
795 { "-infty", '/', "0", "NaN" },
796 { "NaN", '/', "0", "NaN" },
797 { "0", '/', "NaN", "NaN" },
798 { "42", '/', "NaN", "NaN" },
799 { "-42", '/', "NaN", "NaN" },
800 { "infty", '/', "NaN", "NaN" },
801 { "-infty", '/', "NaN", "NaN" },
802 { "NaN", '/', "NaN", "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 { "0", '/', "-infty", "0" },
810 { "42", '/', "-infty", "0" },
811 { "-42", '/', "-infty", "0" },
812 { "infty", '/', "-infty", "NaN" },
813 { "-infty", '/', "-infty", "NaN" },
814 { "NaN", '/', "-infty", "NaN" },
815 { "1", '-', "1/3", "2/3" },
816 { "1/3", '+', "1/2", "5/6" },
817 { "1/2", '+', "1/2", "1" },
818 { "3/4", '-', "1/4", "1/2" },
819 { "1/2", '-', "1/3", "1/6" },
820 { "infty", '+', "42", "infty" },
821 { "infty", '+', "infty", "infty" },
822 { "42", '+', "infty", "infty" },
823 { "infty", '-', "infty", "NaN" },
824 { "infty", '*', "infty", "infty" },
825 { "infty", '*', "-infty", "-infty" },
826 { "-infty", '*', "infty", "-infty" },
827 { "-infty", '*', "-infty", "infty" },
828 { "0", '*', "infty", "NaN" },
829 { "1", '*', "infty", "infty" },
830 { "infty", '*', "0", "NaN" },
831 { "infty", '*', "42", "infty" },
832 { "42", '-', "infty", "-infty" },
833 { "infty", '+', "-infty", "NaN" },
834 { "4", 'g', "6", "2" },
835 { "5", 'g', "6", "1" },
836 { "42", 'm', "3", "3" },
837 { "42", 'M', "3", "42" },
838 { "3", 'm', "42", "3" },
839 { "3", 'M', "42", "42" },
840 { "42", 'm', "infty", "42" },
841 { "42", 'M', "infty", "infty" },
842 { "42", 'm', "-infty", "-infty" },
843 { "42", 'M', "-infty", "42" },
844 { "42", 'm', "NaN", "NaN" },
845 { "42", 'M', "NaN", "NaN" },
846 { "infty", 'm', "-infty", "-infty" },
847 { "infty", 'M', "-infty", "infty" },
850 /* Perform some basic tests of binary operations on isl_val objects.
852 static int test_bin_val(isl_ctx *ctx)
854 int i;
855 isl_val *v1, *v2, *res;
856 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
857 __isl_take isl_val *v2);
858 int ok;
860 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
861 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
862 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
863 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
864 fn = val_bin_op[val_bin_tests[i].op].fn;
865 v1 = fn(v1, v2);
866 if (isl_val_is_nan(res))
867 ok = isl_val_is_nan(v1);
868 else
869 ok = isl_val_eq(v1, res);
870 isl_val_free(v1);
871 isl_val_free(res);
872 if (ok < 0)
873 return -1;
874 if (!ok)
875 isl_die(ctx, isl_error_unknown,
876 "unexpected result", return -1);
879 return 0;
882 /* Perform some basic tests on isl_val objects.
884 static int test_val(isl_ctx *ctx)
886 if (test_un_val(ctx) < 0)
887 return -1;
888 if (test_bin_val(ctx) < 0)
889 return -1;
890 return 0;
893 /* Sets described using existentially quantified variables that
894 * can also be described without.
896 static const char *elimination_tests[] = {
897 "{ [i,j] : 2 * [i/2] + 3 * [j/4] <= 10 and 2 i = j }",
898 "{ [m, w] : exists a : w - 2m - 5 <= 3a <= m - 2w }",
899 "{ [m, w] : exists a : w >= 0 and a < m and -1 + w <= a <= 2m - w }",
902 /* Check that redundant existentially quantified variables are
903 * getting removed.
905 static int test_elimination(isl_ctx *ctx)
907 int i;
908 isl_size n;
909 isl_basic_set *bset;
911 for (i = 0; i < ARRAY_SIZE(elimination_tests); ++i) {
912 bset = isl_basic_set_read_from_str(ctx, elimination_tests[i]);
913 n = isl_basic_set_dim(bset, isl_dim_div);
914 isl_basic_set_free(bset);
915 if (n < 0)
916 return -1;
917 if (n != 0)
918 isl_die(ctx, isl_error_unknown,
919 "expecting no existentials", return -1);
922 return 0;
925 static int test_div(isl_ctx *ctx)
927 const char *str;
928 int empty;
929 isl_space *dim;
930 isl_set *set;
931 isl_local_space *ls;
932 struct isl_basic_set *bset;
933 struct isl_constraint *c;
935 /* test 1 */
936 dim = isl_space_set_alloc(ctx, 0, 3);
937 bset = isl_basic_set_universe(isl_space_copy(dim));
938 ls = isl_local_space_from_space(dim);
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, 1, 3);
944 bset = isl_basic_set_add_constraint(bset, c);
946 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
947 c = isl_constraint_set_constant_si(c, 1);
948 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
949 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
950 bset = isl_basic_set_add_constraint(bset, c);
952 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
954 assert(bset && bset->n_div == 1);
955 isl_local_space_free(ls);
956 isl_basic_set_free(bset);
958 /* test 2 */
959 dim = isl_space_set_alloc(ctx, 0, 3);
960 bset = isl_basic_set_universe(isl_space_copy(dim));
961 ls = isl_local_space_from_space(dim);
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, 1, 3);
967 bset = isl_basic_set_add_constraint(bset, c);
969 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
970 c = isl_constraint_set_constant_si(c, -1);
971 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
972 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
973 bset = isl_basic_set_add_constraint(bset, c);
975 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
977 assert(bset && bset->n_div == 1);
978 isl_local_space_free(ls);
979 isl_basic_set_free(bset);
981 /* test 3 */
982 dim = isl_space_set_alloc(ctx, 0, 3);
983 bset = isl_basic_set_universe(isl_space_copy(dim));
984 ls = isl_local_space_from_space(dim);
986 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
987 c = isl_constraint_set_constant_si(c, 1);
988 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
989 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
990 bset = isl_basic_set_add_constraint(bset, c);
992 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
993 c = isl_constraint_set_constant_si(c, -3);
994 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
995 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 4);
996 bset = isl_basic_set_add_constraint(bset, c);
998 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1000 assert(bset && bset->n_div == 1);
1001 isl_local_space_free(ls);
1002 isl_basic_set_free(bset);
1004 /* test 4 */
1005 dim = isl_space_set_alloc(ctx, 0, 3);
1006 bset = isl_basic_set_universe(isl_space_copy(dim));
1007 ls = isl_local_space_from_space(dim);
1009 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1010 c = isl_constraint_set_constant_si(c, 2);
1011 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1012 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1013 bset = isl_basic_set_add_constraint(bset, c);
1015 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1016 c = isl_constraint_set_constant_si(c, -1);
1017 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1018 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
1019 bset = isl_basic_set_add_constraint(bset, c);
1021 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1023 assert(isl_basic_set_is_empty(bset));
1024 isl_local_space_free(ls);
1025 isl_basic_set_free(bset);
1027 /* test 5 */
1028 dim = isl_space_set_alloc(ctx, 0, 3);
1029 bset = isl_basic_set_universe(isl_space_copy(dim));
1030 ls = isl_local_space_from_space(dim);
1032 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1033 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1034 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
1035 bset = isl_basic_set_add_constraint(bset, c);
1037 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1038 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1039 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1040 bset = isl_basic_set_add_constraint(bset, c);
1042 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1044 assert(bset && bset->n_div == 0);
1045 isl_basic_set_free(bset);
1046 isl_local_space_free(ls);
1048 /* test 6 */
1049 dim = isl_space_set_alloc(ctx, 0, 3);
1050 bset = isl_basic_set_universe(isl_space_copy(dim));
1051 ls = isl_local_space_from_space(dim);
1053 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1054 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1055 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
1056 bset = isl_basic_set_add_constraint(bset, c);
1058 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1059 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1060 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1061 bset = isl_basic_set_add_constraint(bset, c);
1063 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1065 assert(bset && bset->n_div == 1);
1066 isl_basic_set_free(bset);
1067 isl_local_space_free(ls);
1069 /* test 7 */
1070 /* This test is a bit tricky. We set up an equality
1071 * a + 3b + 3c = 6 e0
1072 * Normalization of divs creates _two_ divs
1073 * a = 3 e0
1074 * c - b - e0 = 2 e1
1075 * Afterwards e0 is removed again because it has coefficient -1
1076 * and we end up with the original equality and div again.
1077 * Perhaps we can avoid the introduction of this temporary div.
1079 dim = isl_space_set_alloc(ctx, 0, 4);
1080 bset = isl_basic_set_universe(isl_space_copy(dim));
1081 ls = isl_local_space_from_space(dim);
1083 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1084 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1085 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1086 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -3);
1087 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 6);
1088 bset = isl_basic_set_add_constraint(bset, c);
1090 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
1092 /* Test disabled for now */
1094 assert(bset && bset->n_div == 1);
1096 isl_local_space_free(ls);
1097 isl_basic_set_free(bset);
1099 /* test 8 */
1100 dim = isl_space_set_alloc(ctx, 0, 5);
1101 bset = isl_basic_set_universe(isl_space_copy(dim));
1102 ls = isl_local_space_from_space(dim);
1104 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1105 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1106 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1107 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, -3);
1108 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 4, 6);
1109 bset = isl_basic_set_add_constraint(bset, c);
1111 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1112 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1113 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 1);
1114 c = isl_constraint_set_constant_si(c, 1);
1115 bset = isl_basic_set_add_constraint(bset, c);
1117 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
1119 /* Test disabled for now */
1121 assert(bset && bset->n_div == 1);
1123 isl_local_space_free(ls);
1124 isl_basic_set_free(bset);
1126 /* test 9 */
1127 dim = isl_space_set_alloc(ctx, 0, 4);
1128 bset = isl_basic_set_universe(isl_space_copy(dim));
1129 ls = isl_local_space_from_space(dim);
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, 1, -1);
1134 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1135 bset = isl_basic_set_add_constraint(bset, c);
1137 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1138 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1139 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 3);
1140 c = isl_constraint_set_constant_si(c, 2);
1141 bset = isl_basic_set_add_constraint(bset, c);
1143 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
1145 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1147 assert(!isl_basic_set_is_empty(bset));
1149 isl_local_space_free(ls);
1150 isl_basic_set_free(bset);
1152 /* test 10 */
1153 dim = isl_space_set_alloc(ctx, 0, 3);
1154 bset = isl_basic_set_universe(isl_space_copy(dim));
1155 ls = isl_local_space_from_space(dim);
1157 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1158 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1159 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1160 bset = isl_basic_set_add_constraint(bset, c);
1162 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1164 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1166 isl_local_space_free(ls);
1167 isl_basic_set_free(bset);
1169 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1170 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
1171 set = isl_set_read_from_str(ctx, str);
1172 set = isl_set_compute_divs(set);
1173 isl_set_free(set);
1174 if (!set)
1175 return -1;
1177 if (test_elimination(ctx) < 0)
1178 return -1;
1180 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
1181 set = isl_set_read_from_str(ctx, str);
1182 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
1183 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
1184 empty = isl_set_is_empty(set);
1185 isl_set_free(set);
1186 if (empty < 0)
1187 return -1;
1188 if (!empty)
1189 isl_die(ctx, isl_error_unknown,
1190 "result not as accurate as expected", return -1);
1192 return 0;
1195 void test_application_case(struct isl_ctx *ctx, const char *name)
1197 char *filename;
1198 FILE *input;
1199 struct isl_basic_set *bset1, *bset2;
1200 struct isl_basic_map *bmap;
1202 filename = get_filename(ctx, name, "omega");
1203 assert(filename);
1204 input = fopen(filename, "r");
1205 assert(input);
1207 bset1 = isl_basic_set_read_from_file(ctx, input);
1208 bmap = isl_basic_map_read_from_file(ctx, input);
1210 bset1 = isl_basic_set_apply(bset1, bmap);
1212 bset2 = isl_basic_set_read_from_file(ctx, input);
1214 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1216 isl_basic_set_free(bset1);
1217 isl_basic_set_free(bset2);
1218 free(filename);
1220 fclose(input);
1223 static int test_application(isl_ctx *ctx)
1225 test_application_case(ctx, "application");
1226 test_application_case(ctx, "application2");
1228 return 0;
1231 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1233 char *filename;
1234 FILE *input;
1235 struct isl_basic_set *bset1, *bset2;
1237 filename = get_filename(ctx, name, "polylib");
1238 assert(filename);
1239 input = fopen(filename, "r");
1240 assert(input);
1242 bset1 = isl_basic_set_read_from_file(ctx, input);
1243 bset2 = isl_basic_set_read_from_file(ctx, input);
1245 bset1 = isl_basic_set_affine_hull(bset1);
1247 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1249 isl_basic_set_free(bset1);
1250 isl_basic_set_free(bset2);
1251 free(filename);
1253 fclose(input);
1256 int test_affine_hull(struct isl_ctx *ctx)
1258 const char *str;
1259 isl_set *set;
1260 isl_basic_set *bset, *bset2;
1261 isl_size n;
1262 isl_bool subset;
1264 test_affine_hull_case(ctx, "affine2");
1265 test_affine_hull_case(ctx, "affine");
1266 test_affine_hull_case(ctx, "affine3");
1268 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1269 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1270 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1271 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1272 set = isl_set_read_from_str(ctx, str);
1273 bset = isl_set_affine_hull(set);
1274 n = isl_basic_set_dim(bset, isl_dim_div);
1275 isl_basic_set_free(bset);
1276 if (n < 0)
1277 return -1;
1278 if (n != 0)
1279 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1280 return -1);
1282 /* Check that isl_map_affine_hull is not confused by
1283 * the reordering of divs in isl_map_align_divs.
1285 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1286 "32e0 = b and 32e1 = c); "
1287 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1288 set = isl_set_read_from_str(ctx, str);
1289 bset = isl_set_affine_hull(set);
1290 isl_basic_set_free(bset);
1291 if (!bset)
1292 return -1;
1294 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1295 "32e2 = 31 + 31e0 }";
1296 set = isl_set_read_from_str(ctx, str);
1297 bset = isl_set_affine_hull(set);
1298 str = "{ [a] : exists e : a = 32 e }";
1299 bset2 = isl_basic_set_read_from_str(ctx, str);
1300 subset = isl_basic_set_is_subset(bset, bset2);
1301 isl_basic_set_free(bset);
1302 isl_basic_set_free(bset2);
1303 if (subset < 0)
1304 return -1;
1305 if (!subset)
1306 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1307 return -1);
1309 return 0;
1312 /* Pairs of maps and the corresponding expected results of
1313 * isl_map_plain_unshifted_simple_hull.
1315 struct {
1316 const char *map;
1317 const char *hull;
1318 } plain_unshifted_simple_hull_tests[] = {
1319 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1320 "{ [i] -> [j] : i >= 1 }" },
1321 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1322 "(j mod 4 = 2 and k mod 6 = n) }",
1323 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1326 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1328 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1330 int i;
1331 isl_map *map;
1332 isl_basic_map *hull, *expected;
1333 isl_bool equal;
1335 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1336 const char *str;
1337 str = plain_unshifted_simple_hull_tests[i].map;
1338 map = isl_map_read_from_str(ctx, str);
1339 str = plain_unshifted_simple_hull_tests[i].hull;
1340 expected = isl_basic_map_read_from_str(ctx, str);
1341 hull = isl_map_plain_unshifted_simple_hull(map);
1342 equal = isl_basic_map_is_equal(hull, expected);
1343 isl_basic_map_free(hull);
1344 isl_basic_map_free(expected);
1345 if (equal < 0)
1346 return -1;
1347 if (!equal)
1348 isl_die(ctx, isl_error_unknown, "unexpected hull",
1349 return -1);
1352 return 0;
1355 /* Pairs of sets and the corresponding expected results of
1356 * isl_set_unshifted_simple_hull.
1358 struct {
1359 const char *set;
1360 const char *hull;
1361 } unshifted_simple_hull_tests[] = {
1362 { "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1363 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1366 /* Basic tests for isl_set_unshifted_simple_hull.
1368 static int test_unshifted_simple_hull(isl_ctx *ctx)
1370 int i;
1371 isl_set *set;
1372 isl_basic_set *hull, *expected;
1373 isl_bool equal;
1375 for (i = 0; i < ARRAY_SIZE(unshifted_simple_hull_tests); ++i) {
1376 const char *str;
1377 str = unshifted_simple_hull_tests[i].set;
1378 set = isl_set_read_from_str(ctx, str);
1379 str = unshifted_simple_hull_tests[i].hull;
1380 expected = isl_basic_set_read_from_str(ctx, str);
1381 hull = isl_set_unshifted_simple_hull(set);
1382 equal = isl_basic_set_is_equal(hull, expected);
1383 isl_basic_set_free(hull);
1384 isl_basic_set_free(expected);
1385 if (equal < 0)
1386 return -1;
1387 if (!equal)
1388 isl_die(ctx, isl_error_unknown, "unexpected hull",
1389 return -1);
1392 return 0;
1395 static int test_simple_hull(struct isl_ctx *ctx)
1397 const char *str;
1398 isl_set *set;
1399 isl_basic_set *bset;
1400 isl_bool is_empty;
1402 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1403 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1404 set = isl_set_read_from_str(ctx, str);
1405 bset = isl_set_simple_hull(set);
1406 is_empty = isl_basic_set_is_empty(bset);
1407 isl_basic_set_free(bset);
1409 if (is_empty == isl_bool_error)
1410 return -1;
1412 if (is_empty == isl_bool_false)
1413 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1414 return -1);
1416 if (test_plain_unshifted_simple_hull(ctx) < 0)
1417 return -1;
1418 if (test_unshifted_simple_hull(ctx) < 0)
1419 return -1;
1421 return 0;
1424 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1426 char *filename;
1427 FILE *input;
1428 struct isl_basic_set *bset1, *bset2;
1429 struct isl_set *set;
1431 filename = get_filename(ctx, name, "polylib");
1432 assert(filename);
1433 input = fopen(filename, "r");
1434 assert(input);
1436 bset1 = isl_basic_set_read_from_file(ctx, input);
1437 bset2 = isl_basic_set_read_from_file(ctx, input);
1439 set = isl_basic_set_union(bset1, bset2);
1440 bset1 = isl_set_convex_hull(set);
1442 bset2 = isl_basic_set_read_from_file(ctx, input);
1444 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1446 isl_basic_set_free(bset1);
1447 isl_basic_set_free(bset2);
1448 free(filename);
1450 fclose(input);
1453 struct {
1454 const char *set;
1455 const char *hull;
1456 } convex_hull_tests[] = {
1457 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1458 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1459 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1460 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1461 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1462 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1463 "i2 <= 5 and i2 >= 4; "
1464 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1465 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1466 "i2 <= 5 + i0 and i2 >= i0 }" },
1467 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1468 "{ [x, y] : 1 = 0 }" },
1469 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1470 "[x, y, 0] : x >= 0 and y < 0 }",
1471 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1472 { "{ [a, b, c] : a <= 1 and -a < b <= 1 and 0 <= c <= 2 - a - b and "
1473 "c <= a; "
1474 "[0, 2, 0]; [3, 1, 0] }",
1475 "{ [a, b, c] : b > -a and 2b >= -1 + a and 0 <= c <= a and "
1476 "5c <= 6 - a - 3b }" },
1479 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1481 int i;
1482 int orig_convex = ctx->opt->convex;
1483 ctx->opt->convex = convex;
1485 test_convex_hull_case(ctx, "convex0");
1486 test_convex_hull_case(ctx, "convex1");
1487 test_convex_hull_case(ctx, "convex2");
1488 test_convex_hull_case(ctx, "convex3");
1489 test_convex_hull_case(ctx, "convex4");
1490 test_convex_hull_case(ctx, "convex5");
1491 test_convex_hull_case(ctx, "convex6");
1492 test_convex_hull_case(ctx, "convex7");
1493 test_convex_hull_case(ctx, "convex8");
1494 test_convex_hull_case(ctx, "convex9");
1495 test_convex_hull_case(ctx, "convex10");
1496 test_convex_hull_case(ctx, "convex11");
1497 test_convex_hull_case(ctx, "convex12");
1498 test_convex_hull_case(ctx, "convex13");
1499 test_convex_hull_case(ctx, "convex14");
1500 test_convex_hull_case(ctx, "convex15");
1502 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1503 isl_set *set1, *set2;
1504 int equal;
1506 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1507 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1508 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1509 equal = isl_set_is_equal(set1, set2);
1510 isl_set_free(set1);
1511 isl_set_free(set2);
1513 if (equal < 0)
1514 return -1;
1515 if (!equal)
1516 isl_die(ctx, isl_error_unknown,
1517 "unexpected convex hull", return -1);
1520 ctx->opt->convex = orig_convex;
1522 return 0;
1525 static int test_convex_hull(isl_ctx *ctx)
1527 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1528 return -1;
1529 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1530 return -1;
1531 return 0;
1534 void test_gist_case(struct isl_ctx *ctx, const char *name)
1536 char *filename;
1537 FILE *input;
1538 struct isl_basic_set *bset1, *bset2;
1540 filename = get_filename(ctx, name, "polylib");
1541 assert(filename);
1542 input = fopen(filename, "r");
1543 assert(input);
1545 bset1 = isl_basic_set_read_from_file(ctx, input);
1546 bset2 = isl_basic_set_read_from_file(ctx, input);
1548 bset1 = isl_basic_set_gist(bset1, bset2);
1550 bset2 = isl_basic_set_read_from_file(ctx, input);
1552 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1554 isl_basic_set_free(bset1);
1555 isl_basic_set_free(bset2);
1556 free(filename);
1558 fclose(input);
1561 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1563 struct {
1564 const char *map;
1565 const char *context;
1566 const char *gist;
1567 } plain_gist_tests[] = {
1568 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1569 "{ [i] -> [j] : i >= 1 }",
1570 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1571 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1572 "(j mod 4 = 2 and k mod 6 = n) }",
1573 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1574 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1575 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1576 "{ [i] -> [j] : i > j }",
1577 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1580 /* Basic tests for isl_map_plain_gist_basic_map.
1582 static int test_plain_gist(isl_ctx *ctx)
1584 int i;
1586 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1587 const char *str;
1588 int equal;
1589 isl_map *map, *gist;
1590 isl_basic_map *context;
1592 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1593 str = plain_gist_tests[i].context;
1594 context = isl_basic_map_read_from_str(ctx, str);
1595 map = isl_map_plain_gist_basic_map(map, context);
1596 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1597 equal = isl_map_is_equal(map, gist);
1598 isl_map_free(map);
1599 isl_map_free(gist);
1600 if (equal < 0)
1601 return -1;
1602 if (!equal)
1603 isl_die(ctx, isl_error_unknown,
1604 "incorrect gist result", return -1);
1607 return 0;
1610 /* Inputs for isl_basic_set_gist tests that are expected to fail.
1612 struct {
1613 const char *set;
1614 const char *context;
1615 } gist_fail_tests[] = {
1616 { "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1617 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }",
1618 "{ [i] : i >= 0 }" },
1621 /* Check that isl_basic_set_gist fails (gracefully) when expected.
1622 * In particular, the user should be able to recover from the failure.
1624 static isl_stat test_gist_fail(struct isl_ctx *ctx)
1626 int i, n;
1627 int on_error;
1629 on_error = isl_options_get_on_error(ctx);
1630 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
1631 n = ARRAY_SIZE(gist_fail_tests);
1632 for (i = 0; i < n; ++i) {
1633 const char *str;
1634 isl_basic_set *bset, *context;
1636 bset = isl_basic_set_read_from_str(ctx, gist_fail_tests[i].set);
1637 str = gist_fail_tests[i].context;
1638 context = isl_basic_set_read_from_str(ctx, str);
1639 bset = isl_basic_set_gist(bset, context);
1640 isl_basic_set_free(bset);
1641 if (bset)
1642 break;
1644 isl_options_set_on_error(ctx, on_error);
1645 if (i < n)
1646 isl_die(ctx, isl_error_unknown,
1647 "operation not expected to succeed",
1648 return isl_stat_error);
1650 return isl_stat_ok;
1653 struct {
1654 const char *set;
1655 const char *context;
1656 const char *gist;
1657 } gist_tests[] = {
1658 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1659 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1660 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1661 "{ [a, b, c] : a <= 15 }" },
1662 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1663 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1664 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1665 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1666 { "{ [m, n, a, b] : a <= 2147 + n }",
1667 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1668 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1669 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1670 "b >= 0) }",
1671 "{ [m, n, ku, kl] }" },
1672 { "{ [a, a, b] : a >= 10 }",
1673 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1674 "{ [a, a, b] : a >= 10 }" },
1675 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1676 "{ [0, j] : j >= 0 }" },
1677 /* Check that no constraints on i6 are introduced in the gist */
1678 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1679 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1680 "5e0 <= 381 - t1 and i4 <= 1) }",
1681 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1682 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1683 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1684 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1685 "20e0 >= 1511 - 4t1 - 5i4) }" },
1686 /* Check that no constraints on i6 are introduced in the gist */
1687 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1688 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1689 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1690 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1691 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1692 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1693 "20e1 <= 1530 - 4t1 - 5i4 and "
1694 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1695 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1696 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1697 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1698 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1699 "10e0 <= -91 + 5i4 + 4i6 and "
1700 "10e0 >= -105 + 5i4 + 4i6) }",
1701 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1702 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1703 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1704 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1705 "{ [a, b, q, p] : b >= 1 + a }",
1706 "{ [a, b, q, p] : false }" },
1707 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1708 "[n] -> { [x] : x mod 32 = 0 }",
1709 "[n] -> { [x = n] }" },
1710 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1711 "{ [x] : x mod 2 = 0 }" },
1712 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1713 "{ [x] : x mod 128 = 0 }" },
1714 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1715 "{ [x] : x mod 3200 = 0 }" },
1716 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1717 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1718 "{ [a, b, c = a] }" },
1719 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1720 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1721 "{ [a, b, c = a] : a mod 3 = 0 }" },
1722 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1723 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1724 "{ [x] }" },
1725 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1726 "{ [x,y] : 1 <= y <= 3 }",
1727 "{ [x,y] }" },
1730 /* Check that isl_set_gist behaves as expected.
1732 * For the test cases in gist_tests, besides checking that the result
1733 * is as expected, also check that applying the gist operation does
1734 * not modify the input set (an earlier version of isl would do that) and
1735 * that the test case is consistent, i.e., that the gist has the same
1736 * intersection with the context as the input set.
1738 static int test_gist(struct isl_ctx *ctx)
1740 int i;
1741 const char *str;
1742 isl_basic_set *bset1, *bset2;
1743 isl_map *map1, *map2;
1744 isl_bool equal;
1745 isl_size n_div;
1747 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1748 isl_bool equal_input, equal_intersection;
1749 isl_set *set1, *set2, *copy, *context;
1751 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1752 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1753 copy = isl_set_copy(set1);
1754 set1 = isl_set_gist(set1, isl_set_copy(context));
1755 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1756 equal = isl_set_is_equal(set1, set2);
1757 isl_set_free(set1);
1758 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1759 equal_input = isl_set_is_equal(set1, copy);
1760 isl_set_free(copy);
1761 set1 = isl_set_intersect(set1, isl_set_copy(context));
1762 set2 = isl_set_intersect(set2, context);
1763 equal_intersection = isl_set_is_equal(set1, set2);
1764 isl_set_free(set2);
1765 isl_set_free(set1);
1766 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1767 return -1;
1768 if (!equal)
1769 isl_die(ctx, isl_error_unknown,
1770 "incorrect gist result", return -1);
1771 if (!equal_input)
1772 isl_die(ctx, isl_error_unknown,
1773 "gist modified input", return -1);
1774 if (!equal_input)
1775 isl_die(ctx, isl_error_unknown,
1776 "inconsistent gist test case", return -1);
1779 if (test_gist_fail(ctx) < 0)
1780 return -1;
1782 test_gist_case(ctx, "gist1");
1784 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1785 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1786 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1787 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1788 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1789 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1790 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1791 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1792 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1793 bset1 = isl_basic_set_read_from_str(ctx, str);
1794 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1795 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1796 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1797 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1798 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1799 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1800 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1801 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1802 bset2 = isl_basic_set_read_from_str(ctx, str);
1803 bset1 = isl_basic_set_gist(bset1, bset2);
1804 assert(bset1 && bset1->n_div == 0);
1805 isl_basic_set_free(bset1);
1807 /* Check that the integer divisions of the second disjunct
1808 * do not spread to the first disjunct.
1810 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1811 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1812 "(exists (e0 = [(-1 + t1)/16], "
1813 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1814 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1815 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1816 "o0 <= 4294967295 and t1 <= -1)) }";
1817 map1 = isl_map_read_from_str(ctx, str);
1818 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1819 map2 = isl_map_read_from_str(ctx, str);
1820 map1 = isl_map_gist(map1, map2);
1821 if (!map1)
1822 return -1;
1823 if (map1->n != 1)
1824 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1825 isl_map_free(map1); return -1);
1826 n_div = isl_basic_map_dim(map1->p[0], isl_dim_div);
1827 isl_map_free(map1);
1828 if (n_div < 0)
1829 return -1;
1830 if (n_div != 1)
1831 isl_die(ctx, isl_error_unknown, "expecting single div",
1832 return -1);
1834 if (test_plain_gist(ctx) < 0)
1835 return -1;
1837 return 0;
1840 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1842 isl_set *set, *set2;
1843 int equal;
1844 int one;
1846 set = isl_set_read_from_str(ctx, str);
1847 set = isl_set_coalesce(set);
1848 set2 = isl_set_read_from_str(ctx, str);
1849 equal = isl_set_is_equal(set, set2);
1850 one = set && set->n == 1;
1851 isl_set_free(set);
1852 isl_set_free(set2);
1854 if (equal < 0)
1855 return -1;
1856 if (!equal)
1857 isl_die(ctx, isl_error_unknown,
1858 "coalesced set not equal to input", return -1);
1859 if (check_one && !one)
1860 isl_die(ctx, isl_error_unknown,
1861 "coalesced set should not be a union", return -1);
1863 return 0;
1866 /* Inputs for coalescing tests with unbounded wrapping.
1867 * "str" is a string representation of the input set.
1868 * "single_disjunct" is set if we expect the result to consist of
1869 * a single disjunct.
1871 struct {
1872 int single_disjunct;
1873 const char *str;
1874 } coalesce_unbounded_tests[] = {
1875 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1876 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1877 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1878 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1879 "-10 <= y <= 0}" },
1880 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1881 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1882 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1883 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1884 { 0, "{ [x, y, z] : 0 <= x,y,z <= 100 and 0 < z <= 2 + 2x + 2y; "
1885 "[x, y, 0] : x,y <= 100 and y <= 9 + 11x and x <= 9 + 11y }" },
1888 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1889 * option turned off.
1891 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1893 int i;
1894 int r = 0;
1895 int bounded;
1897 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1898 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1900 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1901 const char *str = coalesce_unbounded_tests[i].str;
1902 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1903 if (test_coalesce_set(ctx, str, check_one) >= 0)
1904 continue;
1905 r = -1;
1906 break;
1909 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1911 return r;
1914 /* Inputs for coalescing tests.
1915 * "str" is a string representation of the input set.
1916 * "single_disjunct" is set if we expect the result to consist of
1917 * a single disjunct.
1919 struct {
1920 int single_disjunct;
1921 const char *str;
1922 } coalesce_tests[] = {
1923 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1924 "y >= x & x >= 2 & 5 >= y }" },
1925 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1926 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1927 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1928 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1929 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1930 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1931 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1932 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1933 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1934 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1935 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1936 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1937 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1938 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1939 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1940 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1941 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1942 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1943 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1944 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1945 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1946 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1947 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1948 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1949 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1950 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1951 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1952 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1953 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1954 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1955 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1956 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1957 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1958 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1959 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1960 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1961 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1962 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1963 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1964 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1965 "[o0, o1, o2, o3, o4, o5, o6]] : "
1966 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1967 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1968 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1969 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1970 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1971 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1972 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1973 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1974 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1975 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1976 "o6 >= i3 + i6 - o3 and M >= 0 and "
1977 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1978 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1979 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1980 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1981 "(o0 = 0 and M >= 2 and N >= 3) or "
1982 "(M = 0 and o0 = 0 and N >= 3) }" },
1983 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1984 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1985 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1986 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1987 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1988 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1989 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1990 "(y = 3 and x = 1) }" },
1991 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1992 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1993 "i1 <= M and i3 <= M and i4 <= M) or "
1994 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1995 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1996 "i4 <= -1 + M) }" },
1997 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1998 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1999 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
2000 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
2001 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
2002 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
2003 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
2004 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
2005 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
2006 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
2007 { 0, "{ [a, b] : exists e : 2e = a and "
2008 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
2009 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
2010 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
2011 "j >= 1 and j' <= i + j - i' and i >= 1; "
2012 "[1, 1, 1, 1] }" },
2013 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
2014 "[i,j] : exists a : j = 3a }" },
2015 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
2016 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
2017 "a >= 3) or "
2018 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
2019 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
2020 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
2021 "c <= 6 + 8a and a >= 3; "
2022 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
2023 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
2024 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2025 "[x,0] : 3 <= x <= 4 }" },
2026 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
2027 "[x,0] : 4 <= x <= 5 }" },
2028 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2029 "[x,0] : 3 <= x <= 5 }" },
2030 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
2031 "[x,0] : 3 <= x <= 4 }" },
2032 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
2033 "i1 <= 0; "
2034 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
2035 { 1, "{ [0,0]; [1,1] }" },
2036 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
2037 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
2038 "ii <= k;"
2039 "[k, 0, k] : k <= 6 and k >= 1 }" },
2040 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
2041 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
2042 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
2043 { 1, "[n] -> { [1] : n >= 0;"
2044 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
2045 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
2046 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
2047 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
2048 "2e0 <= x and 2e0 <= n);"
2049 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
2050 "n >= 0) }" },
2051 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
2052 "128e0 >= -134 + 127t1 and t1 >= 2 and "
2053 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
2054 "t1 = 1 }" },
2055 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
2056 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
2057 "[0, 0] }" },
2058 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
2059 "t1 >= 13 and t1 <= 16);"
2060 "[t1] : t1 <= 15 and t1 >= 12 }" },
2061 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
2062 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
2063 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
2064 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
2065 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
2066 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
2067 "i <= 4j + 2 }" },
2068 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
2069 "(exists (e0 : 3e0 = -2 + c0)) }" },
2070 { 0, "[n, b0, t0] -> "
2071 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2072 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2073 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2074 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2075 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
2076 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
2077 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
2078 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
2079 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2080 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2081 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2082 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
2083 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
2084 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
2085 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
2086 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
2087 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
2088 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
2089 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
2090 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
2091 { 0, "{ [i0, i1, i2] : "
2092 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
2093 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
2094 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
2095 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
2096 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
2097 "32e0 <= 31 + i0)) or "
2098 "i0 >= 0 }" },
2099 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
2100 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
2101 "2*floor((c)/2) = c and 0 <= a <= 192;"
2102 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
2104 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
2105 "(0 <= a <= b <= n) }" },
2106 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
2107 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
2108 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
2109 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2110 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2111 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2112 "b = 3 and 9e0 <= -19 + 2c)) }" },
2113 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2114 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2115 "(a = 4 and b = 3 and "
2116 "9*floor((-16 + 2c)/9) <= -19 + 2c) }" },
2117 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
2118 "(b = -1 + a and 0 < a <= 3 and "
2119 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2120 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2121 "b = 3 and 9e0 <= -19 + 2c)) }" },
2122 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2123 "[1, 0] }" },
2124 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2125 "[0, 1] }" },
2126 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2127 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2128 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
2129 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
2130 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
2131 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
2132 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
2133 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
2134 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
2135 { 1, "{ [a] : a <= 8 and "
2136 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
2137 { 1, "{ [x, y] : 2y = -x and x <= 0 or "
2138 "x <= -1 and 2y <= -x - 1 and 2y >= x - 1 }" },
2139 { 0, "{ [x, y] : 2y = -x and x <= 0 or "
2140 "x <= -2 and 2y <= -x - 1 and 2y >= x - 1 }" },
2141 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2142 "(a < 0 and 3*floor((a)/3) < a) }" },
2143 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2144 "(a < -1 and 3*floor((a)/3) < a) }" },
2145 { 1, "{ [a, b] : a <= 1024 and b >= 0 and "
2146 "((-31 - a + b <= 32*floor((-1 - a)/32) <= -33 + b and "
2147 "32*floor((-1 - a)/32) <= -16 + b + 16*floor((-1 - a)/16))"
2148 "or (2 <= a <= 15 and b < a)) }" },
2149 { 1, "{ [a] : a > 0 and ((16*floor((a)/16) < a and "
2150 "32*floor((a)/32) < a) or a <= 15) }" },
2151 { 1, "{ [a, b, c, d] : (-a + d) mod 64 = 0 and a <= 8 and b <= 1 and "
2152 "10 - a <= c <= 3 and d >= 5 and 9 - 64b <= d <= 70;"
2153 "[a, b = 1, c, d] : (-a + d) mod 64 = 0 and a <= 8 and c >= 4 and "
2154 "10 - a <= c <= 5 and 5 <= d <= 73 - c }" },
2155 { 1, "[n, m] -> { S_0[i] : (-n + i) mod 3 = 0 and m >= 3 + n and "
2156 "i >= n and 3*floor((2 + n + 2m)/3) <= n + 3m - i; "
2157 "S_0[n] : n <= m <= 2 + n }" },
2158 { 1, "{ [a, b] : exists (e0: 0 <= a <= 1 and b >= 0 and "
2159 "2e0 >= -5 + a + 2b and 2e0 >= -1 + a + b and "
2160 "2e0 <= a + b); "
2161 "[a, b] : exists (e0: 0 <= a <= 1 and 2e0 >= -5 + a + 2b and "
2162 "2e0 >= -1 - a + b and 2e0 <= -a + b and "
2163 "2e0 < -a + 2b) }" },
2164 { 1, "{ [i, j, i - 8j] : 8 <= i <= 63 and -7 + i <= 8j <= i; "
2165 "[i, 0, i] : 0 <= i <= 7 }" },
2166 { 1, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [1, 1] }" },
2167 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [0, 2] }" },
2168 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [-1, 3] }" },
2169 { 1, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [1, 1] }" },
2170 { 0, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [2, 1] }" },
2171 { 0, "{ [a, c] : (2 + a) mod 4 = 0 or "
2172 "(c = 4 + a and 4 * floor((a)/4) = a and a >= 0 and a <= 4) or "
2173 "(c = 3 + a and 4 * floor((-1 + a)/4) = -1 + a and "
2174 "a > 0 and a <= 5) }" },
2175 { 1, "{ [1, 0, 0]; [a, b, c] : -1 <= -a < b <= 0 and 2c > b }" },
2176 { 0, "{ [j, a, l] : a mod 2 = 0 and j <= 29 and a >= 2 and "
2177 "2a <= -5 + j and 32j + 2a + 2 <= 4l < 33j; "
2178 "[j, 0, l] : 4 <= j <= 29 and -3 + 33j <= 4l <= 33j }" },
2181 /* A specialized coalescing test case that would result
2182 * in a segmentation fault or a failed assertion in earlier versions of isl.
2184 static int test_coalesce_special(struct isl_ctx *ctx)
2186 const char *str;
2187 isl_map *map1, *map2;
2189 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2190 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
2191 "(y = 201 and o1 <= 239 and o1 >= 212) or "
2192 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
2193 "o1 <= 239 and o1 >= 212)) or "
2194 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
2195 "o1 <= 241 and o1 >= 240));"
2196 "[S_L220_OUT[] -> T7[]] -> "
2197 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
2198 "(y = 2 and o1 <= 241 and o1 >= 212) or "
2199 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
2200 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
2201 map1 = isl_map_read_from_str(ctx, str);
2202 map1 = isl_map_align_divs_internal(map1);
2203 map1 = isl_map_coalesce(map1);
2204 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2205 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
2206 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
2207 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
2208 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
2209 map2 = isl_map_read_from_str(ctx, str);
2210 map2 = isl_map_union(map2, map1);
2211 map2 = isl_map_align_divs_internal(map2);
2212 map2 = isl_map_coalesce(map2);
2213 isl_map_free(map2);
2214 if (!map2)
2215 return -1;
2217 return 0;
2220 /* A specialized coalescing test case that would result in an assertion
2221 * in an earlier version of isl.
2222 * The explicit call to isl_basic_set_union prevents the implicit
2223 * equality constraints in the first basic map from being detected prior
2224 * to the call to isl_set_coalesce, at least at the point
2225 * where this test case was introduced.
2227 static int test_coalesce_special2(struct isl_ctx *ctx)
2229 const char *str;
2230 isl_basic_set *bset1, *bset2;
2231 isl_set *set;
2233 str = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
2234 bset1 = isl_basic_set_read_from_str(ctx, str);
2235 str = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }" ;
2236 bset2 = isl_basic_set_read_from_str(ctx, str);
2237 set = isl_basic_set_union(bset1, bset2);
2238 set = isl_set_coalesce(set);
2239 isl_set_free(set);
2241 if (!set)
2242 return -1;
2243 return 0;
2246 /* Check that calling isl_set_coalesce does not leave other sets
2247 * that may share some information with the input to isl_set_coalesce
2248 * in an inconsistent state.
2249 * In particular, older versions of isl would modify all copies
2250 * of the basic sets in the isl_set_coalesce input in a way
2251 * that could leave them in an inconsistent state.
2252 * The result of printing any other set containing one of these
2253 * basic sets would then result in an invalid set description.
2255 static int test_coalesce_special3(isl_ctx *ctx)
2257 const char *str;
2258 char *s;
2259 isl_set *set1, *set2;
2260 isl_printer *p;
2262 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
2263 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
2264 set2 = isl_set_read_from_str(ctx, str);
2265 set1 = isl_set_union(set1, isl_set_copy(set2));
2266 set1 = isl_set_coalesce(set1);
2267 isl_set_free(set1);
2269 p = isl_printer_to_str(ctx);
2270 p = isl_printer_print_set(p, set2);
2271 isl_set_free(set2);
2272 s = isl_printer_get_str(p);
2273 isl_printer_free(p);
2274 set1 = isl_set_read_from_str(ctx, s);
2275 free(s);
2276 isl_set_free(set1);
2278 if (!set1)
2279 return -1;
2281 return 0;
2284 /* Check that calling isl_set_coalesce on the intersection of
2285 * the sets described by "s1" and "s2" does not leave other sets
2286 * that may share some information with the input to isl_set_coalesce
2287 * in an inconsistent state.
2288 * In particular, when isl_set_coalesce detects equality constraints,
2289 * it does not immediately perform Gaussian elimination on them,
2290 * but then it needs to ensure that it is performed at some point.
2291 * The input set has implicit equality constraints in the first disjunct.
2292 * It is constructed as an intersection, because otherwise
2293 * those equality constraints would already be detected during parsing.
2295 static isl_stat test_coalesce_intersection(isl_ctx *ctx,
2296 const char *s1, const char *s2)
2298 isl_set *set1, *set2;
2300 set1 = isl_set_read_from_str(ctx, s1);
2301 set2 = isl_set_read_from_str(ctx, s2);
2302 set1 = isl_set_intersect(set1, set2);
2303 isl_set_free(isl_set_coalesce(isl_set_copy(set1)));
2304 set1 = isl_set_coalesce(set1);
2305 isl_set_free(set1);
2307 if (!set1)
2308 return isl_stat_error;
2310 return isl_stat_ok;
2313 /* Check that calling isl_set_coalesce does not leave other sets
2314 * that may share some information with the input to isl_set_coalesce
2315 * in an inconsistent state, for the case where one disjunct
2316 * is a subset of the other.
2318 static isl_stat test_coalesce_special4(isl_ctx *ctx)
2320 const char *s1, *s2;
2322 s1 = "{ [a, b] : b <= 0 or a <= 1 }";
2323 s2 = "{ [a, b] : -1 <= -a < b }";
2324 return test_coalesce_intersection(ctx, s1, s2);
2327 /* Check that calling isl_set_coalesce does not leave other sets
2328 * that may share some information with the input to isl_set_coalesce
2329 * in an inconsistent state, for the case where two disjuncts
2330 * can be fused.
2332 static isl_stat test_coalesce_special5(isl_ctx *ctx)
2334 const char *s1, *s2;
2336 s1 = "{ [a, b, c] : b <= 0 }";
2337 s2 = "{ [a, b, c] : -1 <= -a < b and (c >= 0 or c < 0) }";
2338 return test_coalesce_intersection(ctx, s1, s2);
2341 /* Check that calling isl_set_coalesce does not leave other sets
2342 * that may share some information with the input to isl_set_coalesce
2343 * in an inconsistent state, for the case where two disjuncts
2344 * can be fused and where both disjuncts have implicit equality constraints.
2346 static isl_stat test_coalesce_special6(isl_ctx *ctx)
2348 const char *s1, *s2;
2350 s1 = "{ [a, b, c] : c <= 0 }";
2351 s2 = "{ [a, b, c] : 0 <= a <= b <= c or (0 <= b <= c and a > 0) }";
2352 return test_coalesce_intersection(ctx, s1, s2);
2355 /* Test the functionality of isl_set_coalesce.
2356 * That is, check that the output is always equal to the input
2357 * and in some cases that the result consists of a single disjunct.
2359 static int test_coalesce(struct isl_ctx *ctx)
2361 int i;
2363 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2364 const char *str = coalesce_tests[i].str;
2365 int check_one = coalesce_tests[i].single_disjunct;
2366 if (test_coalesce_set(ctx, str, check_one) < 0)
2367 return -1;
2370 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2371 return -1;
2372 if (test_coalesce_special(ctx) < 0)
2373 return -1;
2374 if (test_coalesce_special2(ctx) < 0)
2375 return -1;
2376 if (test_coalesce_special3(ctx) < 0)
2377 return -1;
2378 if (test_coalesce_special4(ctx) < 0)
2379 return -1;
2380 if (test_coalesce_special5(ctx) < 0)
2381 return -1;
2382 if (test_coalesce_special6(ctx) < 0)
2383 return -1;
2386 return 0;
2389 /* Construct a representation of the graph on the right of Figure 1
2390 * in "Computing the Transitive Closure of a Union of
2391 * Affine Integer Tuple Relations".
2393 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2395 isl_set *dom;
2396 isl_map *up, *right;
2398 dom = isl_set_read_from_str(ctx,
2399 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2400 "2 x - 3 y + 3 >= 0 }");
2401 right = isl_map_read_from_str(ctx,
2402 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2403 up = isl_map_read_from_str(ctx,
2404 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2405 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2406 right = isl_map_intersect_range(right, isl_set_copy(dom));
2407 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2408 up = isl_map_intersect_range(up, dom);
2409 return isl_map_union(up, right);
2412 /* Construct a representation of the power of the graph
2413 * on the right of Figure 1 in "Computing the Transitive Closure of
2414 * a Union of Affine Integer Tuple Relations".
2416 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2418 return isl_map_read_from_str(ctx,
2419 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2420 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2421 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2424 /* Construct a representation of the transitive closure of the graph
2425 * on the right of Figure 1 in "Computing the Transitive Closure of
2426 * a Union of Affine Integer Tuple Relations".
2428 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2430 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2433 static int test_closure(isl_ctx *ctx)
2435 const char *str;
2436 isl_map *map, *map2;
2437 isl_bool exact, equal;
2439 /* COCOA example 1 */
2440 map = isl_map_read_from_str(ctx,
2441 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2442 "1 <= i and i < n and 1 <= j and j < n or "
2443 "i2 = i + 1 and j2 = j - 1 and "
2444 "1 <= i and i < n and 2 <= j and j <= n }");
2445 map = isl_map_power(map, &exact);
2446 assert(exact);
2447 isl_map_free(map);
2449 /* COCOA example 1 */
2450 map = isl_map_read_from_str(ctx,
2451 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2452 "1 <= i and i < n and 1 <= j and j < n or "
2453 "i2 = i + 1 and j2 = j - 1 and "
2454 "1 <= i and i < n and 2 <= j and j <= n }");
2455 map = isl_map_transitive_closure(map, &exact);
2456 assert(exact);
2457 map2 = isl_map_read_from_str(ctx,
2458 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2459 "1 <= i and i < n and 1 <= j and j <= n and "
2460 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2461 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2462 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2463 assert(isl_map_is_equal(map, map2));
2464 isl_map_free(map2);
2465 isl_map_free(map);
2467 map = isl_map_read_from_str(ctx,
2468 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2469 " 0 <= y and y <= n }");
2470 map = isl_map_transitive_closure(map, &exact);
2471 map2 = isl_map_read_from_str(ctx,
2472 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2473 " 0 <= y and y <= n }");
2474 assert(isl_map_is_equal(map, map2));
2475 isl_map_free(map2);
2476 isl_map_free(map);
2478 /* COCOA example 2 */
2479 map = isl_map_read_from_str(ctx,
2480 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2481 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2482 "i2 = i + 2 and j2 = j - 2 and "
2483 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2484 map = isl_map_transitive_closure(map, &exact);
2485 assert(exact);
2486 map2 = isl_map_read_from_str(ctx,
2487 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2488 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2489 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2490 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2491 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2492 assert(isl_map_is_equal(map, map2));
2493 isl_map_free(map);
2494 isl_map_free(map2);
2496 /* COCOA Fig.2 left */
2497 map = isl_map_read_from_str(ctx,
2498 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2499 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2500 "j <= n or "
2501 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2502 "j <= 2 i - 3 and j <= n - 2 or "
2503 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2504 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2505 map = isl_map_transitive_closure(map, &exact);
2506 assert(exact);
2507 isl_map_free(map);
2509 /* COCOA Fig.2 right */
2510 map = isl_map_read_from_str(ctx,
2511 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2512 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2513 "j <= n or "
2514 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2515 "j <= 2 i - 4 and j <= n - 3 or "
2516 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2517 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2518 map = isl_map_power(map, &exact);
2519 assert(exact);
2520 isl_map_free(map);
2522 /* COCOA Fig.2 right */
2523 map = isl_map_read_from_str(ctx,
2524 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2525 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2526 "j <= n or "
2527 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2528 "j <= 2 i - 4 and j <= n - 3 or "
2529 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2530 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2531 map = isl_map_transitive_closure(map, &exact);
2532 assert(exact);
2533 map2 = isl_map_read_from_str(ctx,
2534 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2535 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2536 "j <= n and 3 + i + 2 j <= 3 n and "
2537 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2538 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2539 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2540 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2541 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2542 assert(isl_map_is_equal(map, map2));
2543 isl_map_free(map2);
2544 isl_map_free(map);
2546 map = cocoa_fig_1_right_graph(ctx);
2547 map = isl_map_transitive_closure(map, &exact);
2548 assert(exact);
2549 map2 = cocoa_fig_1_right_tc(ctx);
2550 assert(isl_map_is_equal(map, map2));
2551 isl_map_free(map2);
2552 isl_map_free(map);
2554 map = cocoa_fig_1_right_graph(ctx);
2555 map = isl_map_power(map, &exact);
2556 map2 = cocoa_fig_1_right_power(ctx);
2557 equal = isl_map_is_equal(map, map2);
2558 isl_map_free(map2);
2559 isl_map_free(map);
2560 if (equal < 0)
2561 return -1;
2562 if (!exact)
2563 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2564 if (!equal)
2565 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2567 /* COCOA Theorem 1 counter example */
2568 map = isl_map_read_from_str(ctx,
2569 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2570 "i2 = 1 and j2 = j or "
2571 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2572 map = isl_map_transitive_closure(map, &exact);
2573 assert(exact);
2574 isl_map_free(map);
2576 map = isl_map_read_from_str(ctx,
2577 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2578 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2579 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2580 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2581 map = isl_map_transitive_closure(map, &exact);
2582 assert(exact);
2583 isl_map_free(map);
2585 /* Kelly et al 1996, fig 12 */
2586 map = isl_map_read_from_str(ctx,
2587 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2588 "1 <= i,j,j+1 <= n or "
2589 "j = n and j2 = 1 and i2 = i + 1 and "
2590 "1 <= i,i+1 <= n }");
2591 map = isl_map_transitive_closure(map, &exact);
2592 assert(exact);
2593 map2 = isl_map_read_from_str(ctx,
2594 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2595 "1 <= i <= n and i = i2 or "
2596 "1 <= i < i2 <= n and 1 <= j <= n and "
2597 "1 <= j2 <= n }");
2598 assert(isl_map_is_equal(map, map2));
2599 isl_map_free(map2);
2600 isl_map_free(map);
2602 /* Omega's closure4 */
2603 map = isl_map_read_from_str(ctx,
2604 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2605 "1 <= x,y <= 10 or "
2606 "x2 = x + 1 and y2 = y and "
2607 "1 <= x <= 20 && 5 <= y <= 15 }");
2608 map = isl_map_transitive_closure(map, &exact);
2609 assert(exact);
2610 isl_map_free(map);
2612 map = isl_map_read_from_str(ctx,
2613 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2614 map = isl_map_transitive_closure(map, &exact);
2615 assert(!exact);
2616 map2 = isl_map_read_from_str(ctx,
2617 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2618 assert(isl_map_is_equal(map, map2));
2619 isl_map_free(map);
2620 isl_map_free(map2);
2622 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2623 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2624 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2625 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2626 map = isl_map_read_from_str(ctx, str);
2627 map = isl_map_transitive_closure(map, &exact);
2628 assert(exact);
2629 map2 = isl_map_read_from_str(ctx, str);
2630 assert(isl_map_is_equal(map, map2));
2631 isl_map_free(map);
2632 isl_map_free(map2);
2634 str = "{[0] -> [1]; [2] -> [3]}";
2635 map = isl_map_read_from_str(ctx, str);
2636 map = isl_map_transitive_closure(map, &exact);
2637 assert(exact);
2638 map2 = isl_map_read_from_str(ctx, str);
2639 assert(isl_map_is_equal(map, map2));
2640 isl_map_free(map);
2641 isl_map_free(map2);
2643 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2644 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2645 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2646 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2647 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2648 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2649 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2650 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2651 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2652 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2653 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2654 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2655 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2656 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2657 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2658 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2659 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2660 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2661 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2662 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2663 map = isl_map_read_from_str(ctx, str);
2664 map = isl_map_transitive_closure(map, NULL);
2665 assert(map);
2666 isl_map_free(map);
2668 return 0;
2671 /* Check that the actual result of a boolean operation is equal
2672 * to the expected result.
2674 static isl_stat check_bool(isl_ctx *ctx, isl_bool actual, isl_bool expected)
2676 if (actual != expected)
2677 isl_die(ctx, isl_error_unknown,
2678 "incorrect boolean operation", return isl_stat_error);
2679 return isl_stat_ok;
2682 /* Test operations on isl_bool values.
2684 * This tests:
2686 * isl_bool_not
2687 * isl_bool_ok
2689 static int test_isl_bool(isl_ctx *ctx)
2691 if (check_bool(ctx, isl_bool_not(isl_bool_true), isl_bool_false) < 0)
2692 return -1;
2693 if (check_bool(ctx, isl_bool_not(isl_bool_false), isl_bool_true) < 0)
2694 return -1;
2695 if (check_bool(ctx, isl_bool_not(isl_bool_error), isl_bool_error) < 0)
2696 return -1;
2697 if (check_bool(ctx, isl_bool_ok(0), isl_bool_false) < 0)
2698 return -1;
2699 if (check_bool(ctx, isl_bool_ok(1), isl_bool_true) < 0)
2700 return -1;
2701 if (check_bool(ctx, isl_bool_ok(-1), isl_bool_true) < 0)
2702 return -1;
2703 if (check_bool(ctx, isl_bool_ok(2), isl_bool_true) < 0)
2704 return -1;
2705 if (check_bool(ctx, isl_bool_ok(-2), isl_bool_true) < 0)
2706 return -1;
2708 return 0;
2711 static int test_lex(struct isl_ctx *ctx)
2713 isl_space *dim;
2714 isl_map *map;
2715 int empty;
2717 dim = isl_space_set_alloc(ctx, 0, 0);
2718 map = isl_map_lex_le(dim);
2719 empty = isl_map_is_empty(map);
2720 isl_map_free(map);
2722 if (empty < 0)
2723 return -1;
2724 if (empty)
2725 isl_die(ctx, isl_error_unknown,
2726 "expecting non-empty result", return -1);
2728 return 0;
2731 /* Inputs for isl_map_lexmin tests.
2732 * "map" is the input and "lexmin" is the expected result.
2734 struct {
2735 const char *map;
2736 const char *lexmin;
2737 } lexmin_tests [] = {
2738 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2739 "{ [x] -> [5] : 6 <= x <= 8; "
2740 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2741 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2742 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2743 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2744 "{ [x] -> [y] : (4y = x and x >= 0) or "
2745 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2746 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2747 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2748 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2749 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2750 /* Check that empty pieces are properly combined. */
2751 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2752 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2753 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2754 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2755 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2756 "x >= 4 }" },
2757 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2758 "a <= 255 and c <= 255 and d <= 255 - j and "
2759 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2760 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2761 "247d <= 247 + i and 248 - b <= 248d <= c and "
2762 "254d >= i - a + b and 254d >= -a + b and "
2763 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2764 "{ [i, k, j] -> "
2765 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2766 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2767 "247j >= 62738 - i and 509j <= 129795 + i and "
2768 "742j >= 188724 - i; "
2769 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2770 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2771 "16*floor((8 + b)/16) <= 7 + b; "
2772 "[a] -> [1] }",
2773 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2774 "[a] -> [b = 0] : 0 < a <= 509 }" },
2775 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2776 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2779 static int test_lexmin(struct isl_ctx *ctx)
2781 int i;
2782 int equal;
2783 const char *str;
2784 isl_basic_map *bmap;
2785 isl_map *map, *map2;
2786 isl_set *set;
2787 isl_set *set2;
2788 isl_pw_multi_aff *pma;
2790 str = "[p0, p1] -> { [] -> [] : "
2791 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2792 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2793 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2794 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2795 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2796 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2797 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2798 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2799 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2800 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2801 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2802 map = isl_map_read_from_str(ctx, str);
2803 map = isl_map_lexmin(map);
2804 isl_map_free(map);
2805 if (!map)
2806 return -1;
2808 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2809 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2810 set = isl_set_read_from_str(ctx, str);
2811 set = isl_set_lexmax(set);
2812 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2813 set2 = isl_set_read_from_str(ctx, str);
2814 set = isl_set_intersect(set, set2);
2815 assert(!isl_set_is_empty(set));
2816 isl_set_free(set);
2818 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2819 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2820 map = isl_map_lexmin(map);
2821 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2822 equal = isl_map_is_equal(map, map2);
2823 isl_map_free(map);
2824 isl_map_free(map2);
2826 if (equal < 0)
2827 return -1;
2828 if (!equal)
2829 isl_die(ctx, isl_error_unknown,
2830 "unexpected result", return -1);
2833 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2834 " 8i' <= i and 8i' >= -7 + i }";
2835 bmap = isl_basic_map_read_from_str(ctx, str);
2836 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2837 map2 = isl_map_from_pw_multi_aff(pma);
2838 map = isl_map_from_basic_map(bmap);
2839 assert(isl_map_is_equal(map, map2));
2840 isl_map_free(map);
2841 isl_map_free(map2);
2843 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2844 " 8i' <= i and 8i' >= -7 + i }";
2845 set = isl_set_read_from_str(ctx, str);
2846 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2847 set2 = isl_set_from_pw_multi_aff(pma);
2848 equal = isl_set_is_equal(set, set2);
2849 isl_set_free(set);
2850 isl_set_free(set2);
2851 if (equal < 0)
2852 return -1;
2853 if (!equal)
2854 isl_die(ctx, isl_error_unknown,
2855 "unexpected difference between set and "
2856 "piecewise affine expression", return -1);
2858 return 0;
2861 /* A specialized isl_set_min_val test case that would return the wrong result
2862 * in earlier versions of isl.
2863 * The explicit call to isl_basic_set_union prevents the second basic set
2864 * from being determined to be empty prior to the call to isl_set_min_val,
2865 * at least at the point where this test case was introduced.
2867 static int test_min_special(isl_ctx *ctx)
2869 const char *str;
2870 isl_basic_set *bset1, *bset2;
2871 isl_set *set;
2872 isl_aff *obj;
2873 isl_val *res;
2874 int ok;
2876 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2877 bset1 = isl_basic_set_read_from_str(ctx, str);
2878 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2879 bset2 = isl_basic_set_read_from_str(ctx, str);
2880 set = isl_basic_set_union(bset1, bset2);
2881 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2883 res = isl_set_min_val(set, obj);
2884 ok = isl_val_cmp_si(res, 5) == 0;
2886 isl_aff_free(obj);
2887 isl_set_free(set);
2888 isl_val_free(res);
2890 if (!res)
2891 return -1;
2892 if (!ok)
2893 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2894 return -1);
2896 return 0;
2899 /* A specialized isl_set_min_val test case that would return an error
2900 * in earlier versions of isl.
2902 static int test_min_special2(isl_ctx *ctx)
2904 const char *str;
2905 isl_basic_set *bset;
2906 isl_aff *obj;
2907 isl_val *res;
2909 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
2910 bset = isl_basic_set_read_from_str(ctx, str);
2912 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
2914 res = isl_basic_set_max_val(bset, obj);
2916 isl_basic_set_free(bset);
2917 isl_aff_free(obj);
2918 isl_val_free(res);
2920 if (!res)
2921 return -1;
2923 return 0;
2926 struct {
2927 const char *set;
2928 const char *obj;
2929 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2930 __isl_keep isl_aff *obj);
2931 const char *res;
2932 } opt_tests[] = {
2933 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2934 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2935 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2936 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2937 &isl_set_max_val, "30" },
2941 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2942 * In particular, check the results on non-convex inputs.
2944 static int test_min(struct isl_ctx *ctx)
2946 int i;
2947 isl_set *set;
2948 isl_aff *obj;
2949 isl_val *val, *res;
2950 isl_bool ok;
2952 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2953 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2954 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2955 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2956 val = opt_tests[i].fn(set, obj);
2957 ok = isl_val_eq(res, val);
2958 isl_val_free(res);
2959 isl_val_free(val);
2960 isl_aff_free(obj);
2961 isl_set_free(set);
2963 if (ok < 0)
2964 return -1;
2965 if (!ok)
2966 isl_die(ctx, isl_error_unknown,
2967 "unexpected optimum", return -1);
2970 if (test_min_special(ctx) < 0)
2971 return -1;
2972 if (test_min_special2(ctx) < 0)
2973 return -1;
2975 return 0;
2978 struct must_may {
2979 isl_map *must;
2980 isl_map *may;
2983 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2984 void *dep_user, void *user)
2986 struct must_may *mm = (struct must_may *)user;
2988 if (must)
2989 mm->must = isl_map_union(mm->must, dep);
2990 else
2991 mm->may = isl_map_union(mm->may, dep);
2993 return isl_stat_ok;
2996 static int common_space(void *first, void *second)
2998 int depth = *(int *)first;
2999 return 2 * depth;
3002 static int map_is_equal(__isl_keep isl_map *map, const char *str)
3004 isl_map *map2;
3005 int equal;
3007 if (!map)
3008 return -1;
3010 map2 = isl_map_read_from_str(map->ctx, str);
3011 equal = isl_map_is_equal(map, map2);
3012 isl_map_free(map2);
3014 return equal;
3017 static int map_check_equal(__isl_keep isl_map *map, const char *str)
3019 int equal;
3021 equal = map_is_equal(map, str);
3022 if (equal < 0)
3023 return -1;
3024 if (!equal)
3025 isl_die(isl_map_get_ctx(map), isl_error_unknown,
3026 "result not as expected", return -1);
3027 return 0;
3030 /* Is "set" equal to the set described by "str"?
3032 static isl_bool set_is_equal(__isl_keep isl_set *set, const char *str)
3034 isl_set *set2;
3035 isl_bool equal;
3037 if (!set)
3038 return isl_bool_error;
3040 set2 = isl_set_read_from_str(isl_set_get_ctx(set), str);
3041 equal = isl_set_is_equal(set, set2);
3042 isl_set_free(set2);
3044 return equal;
3047 /* Check that "set" is equal to the set described by "str".
3049 static isl_stat set_check_equal(__isl_keep isl_set *set, const char *str)
3051 isl_bool equal;
3053 equal = set_is_equal(set, str);
3054 if (equal < 0)
3055 return isl_stat_error;
3056 if (!equal)
3057 isl_die(isl_set_get_ctx(set), isl_error_unknown,
3058 "result not as expected", return isl_stat_error);
3059 return isl_stat_ok;
3062 /* Is "uset" equal to the union set described by "str"?
3064 static isl_bool uset_is_equal(__isl_keep isl_union_set *uset, const char *str)
3066 isl_union_set *uset2;
3067 isl_bool equal;
3069 if (!uset)
3070 return isl_bool_error;
3072 uset2 = isl_union_set_read_from_str(isl_union_set_get_ctx(uset), str);
3073 equal = isl_union_set_is_equal(uset, uset2);
3074 isl_union_set_free(uset2);
3076 return equal;
3079 /* Check that "uset" is equal to the union set described by "str".
3081 static isl_stat uset_check_equal(__isl_keep isl_union_set *uset,
3082 const char *str)
3084 isl_bool equal;
3086 equal = uset_is_equal(uset, str);
3087 if (equal < 0)
3088 return isl_stat_error;
3089 if (!equal)
3090 isl_die(isl_union_set_get_ctx(uset), isl_error_unknown,
3091 "result not as expected", return isl_stat_error);
3092 return isl_stat_ok;
3095 static int test_dep(struct isl_ctx *ctx)
3097 const char *str;
3098 isl_space *dim;
3099 isl_map *map;
3100 isl_access_info *ai;
3101 isl_flow *flow;
3102 int depth;
3103 struct must_may mm;
3105 depth = 3;
3107 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3108 map = isl_map_read_from_str(ctx, str);
3109 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3111 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3112 map = isl_map_read_from_str(ctx, str);
3113 ai = isl_access_info_add_source(ai, map, 1, &depth);
3115 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3116 map = isl_map_read_from_str(ctx, str);
3117 ai = isl_access_info_add_source(ai, map, 1, &depth);
3119 flow = isl_access_info_compute_flow(ai);
3120 dim = isl_space_alloc(ctx, 0, 3, 3);
3121 mm.must = isl_map_empty(isl_space_copy(dim));
3122 mm.may = isl_map_empty(dim);
3124 isl_flow_foreach(flow, collect_must_may, &mm);
3126 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
3127 " [1,10,0] -> [2,5,0] }";
3128 assert(map_is_equal(mm.must, str));
3129 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3130 assert(map_is_equal(mm.may, str));
3132 isl_map_free(mm.must);
3133 isl_map_free(mm.may);
3134 isl_flow_free(flow);
3137 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3138 map = isl_map_read_from_str(ctx, str);
3139 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3141 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3142 map = isl_map_read_from_str(ctx, str);
3143 ai = isl_access_info_add_source(ai, map, 1, &depth);
3145 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3146 map = isl_map_read_from_str(ctx, str);
3147 ai = isl_access_info_add_source(ai, map, 0, &depth);
3149 flow = isl_access_info_compute_flow(ai);
3150 dim = isl_space_alloc(ctx, 0, 3, 3);
3151 mm.must = isl_map_empty(isl_space_copy(dim));
3152 mm.may = isl_map_empty(dim);
3154 isl_flow_foreach(flow, collect_must_may, &mm);
3156 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
3157 assert(map_is_equal(mm.must, str));
3158 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3159 assert(map_is_equal(mm.may, str));
3161 isl_map_free(mm.must);
3162 isl_map_free(mm.may);
3163 isl_flow_free(flow);
3166 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3167 map = isl_map_read_from_str(ctx, str);
3168 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3170 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3171 map = isl_map_read_from_str(ctx, str);
3172 ai = isl_access_info_add_source(ai, map, 0, &depth);
3174 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3175 map = isl_map_read_from_str(ctx, str);
3176 ai = isl_access_info_add_source(ai, map, 0, &depth);
3178 flow = isl_access_info_compute_flow(ai);
3179 dim = isl_space_alloc(ctx, 0, 3, 3);
3180 mm.must = isl_map_empty(isl_space_copy(dim));
3181 mm.may = isl_map_empty(dim);
3183 isl_flow_foreach(flow, collect_must_may, &mm);
3185 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
3186 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3187 assert(map_is_equal(mm.may, str));
3188 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3189 assert(map_is_equal(mm.must, str));
3191 isl_map_free(mm.must);
3192 isl_map_free(mm.may);
3193 isl_flow_free(flow);
3196 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
3197 map = isl_map_read_from_str(ctx, str);
3198 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3200 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3201 map = isl_map_read_from_str(ctx, str);
3202 ai = isl_access_info_add_source(ai, map, 0, &depth);
3204 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
3205 map = isl_map_read_from_str(ctx, str);
3206 ai = isl_access_info_add_source(ai, map, 0, &depth);
3208 flow = isl_access_info_compute_flow(ai);
3209 dim = isl_space_alloc(ctx, 0, 3, 3);
3210 mm.must = isl_map_empty(isl_space_copy(dim));
3211 mm.may = isl_map_empty(dim);
3213 isl_flow_foreach(flow, collect_must_may, &mm);
3215 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
3216 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
3217 assert(map_is_equal(mm.may, str));
3218 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3219 assert(map_is_equal(mm.must, str));
3221 isl_map_free(mm.must);
3222 isl_map_free(mm.may);
3223 isl_flow_free(flow);
3226 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
3227 map = isl_map_read_from_str(ctx, str);
3228 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3230 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3231 map = isl_map_read_from_str(ctx, str);
3232 ai = isl_access_info_add_source(ai, map, 0, &depth);
3234 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
3235 map = isl_map_read_from_str(ctx, str);
3236 ai = isl_access_info_add_source(ai, map, 0, &depth);
3238 flow = isl_access_info_compute_flow(ai);
3239 dim = isl_space_alloc(ctx, 0, 3, 3);
3240 mm.must = isl_map_empty(isl_space_copy(dim));
3241 mm.may = isl_map_empty(dim);
3243 isl_flow_foreach(flow, collect_must_may, &mm);
3245 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
3246 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
3247 assert(map_is_equal(mm.may, str));
3248 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3249 assert(map_is_equal(mm.must, str));
3251 isl_map_free(mm.must);
3252 isl_map_free(mm.may);
3253 isl_flow_free(flow);
3256 depth = 5;
3258 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3259 map = isl_map_read_from_str(ctx, str);
3260 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
3262 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3263 map = isl_map_read_from_str(ctx, str);
3264 ai = isl_access_info_add_source(ai, map, 1, &depth);
3266 flow = isl_access_info_compute_flow(ai);
3267 dim = isl_space_alloc(ctx, 0, 5, 5);
3268 mm.must = isl_map_empty(isl_space_copy(dim));
3269 mm.may = isl_map_empty(dim);
3271 isl_flow_foreach(flow, collect_must_may, &mm);
3273 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
3274 assert(map_is_equal(mm.must, str));
3275 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
3276 assert(map_is_equal(mm.may, str));
3278 isl_map_free(mm.must);
3279 isl_map_free(mm.may);
3280 isl_flow_free(flow);
3282 return 0;
3285 /* Check that the dependence analysis proceeds without errors.
3286 * Earlier versions of isl would break down during the analysis
3287 * due to the use of the wrong spaces.
3289 static int test_flow(isl_ctx *ctx)
3291 const char *str;
3292 isl_union_map *access, *schedule;
3293 isl_union_map *must_dep, *may_dep;
3294 int r;
3296 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
3297 access = isl_union_map_read_from_str(ctx, str);
3298 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
3299 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
3300 "S2[] -> [1,0,0,0]; "
3301 "S3[] -> [-1,0,0,0] }";
3302 schedule = isl_union_map_read_from_str(ctx, str);
3303 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
3304 isl_union_map_copy(access), schedule,
3305 &must_dep, &may_dep, NULL, NULL);
3306 isl_union_map_free(may_dep);
3307 isl_union_map_free(must_dep);
3309 return r;
3312 struct {
3313 const char *map;
3314 int sv;
3315 } sv_tests[] = {
3316 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
3317 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
3318 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
3319 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
3320 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
3321 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
3322 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3323 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3324 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
3327 int test_sv(isl_ctx *ctx)
3329 isl_union_map *umap;
3330 int i;
3331 int sv;
3333 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
3334 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
3335 sv = isl_union_map_is_single_valued(umap);
3336 isl_union_map_free(umap);
3337 if (sv < 0)
3338 return -1;
3339 if (sv_tests[i].sv && !sv)
3340 isl_die(ctx, isl_error_internal,
3341 "map not detected as single valued", return -1);
3342 if (!sv_tests[i].sv && sv)
3343 isl_die(ctx, isl_error_internal,
3344 "map detected as single valued", return -1);
3347 return 0;
3350 struct {
3351 const char *str;
3352 int bijective;
3353 } bijective_tests[] = {
3354 { "[N,M]->{[i,j] -> [i]}", 0 },
3355 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
3356 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
3357 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
3358 { "[N,M]->{[i,j] -> [j,i]}", 1 },
3359 { "[N,M]->{[i,j] -> [i+j]}", 0 },
3360 { "[N,M]->{[i,j] -> []}", 0 },
3361 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
3362 { "[N,M]->{[i,j] -> [2i]}", 0 },
3363 { "[N,M]->{[i,j] -> [i,i]}", 0 },
3364 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
3365 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
3366 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
3369 static int test_bijective(struct isl_ctx *ctx)
3371 isl_map *map;
3372 int i;
3373 int bijective;
3375 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
3376 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
3377 bijective = isl_map_is_bijective(map);
3378 isl_map_free(map);
3379 if (bijective < 0)
3380 return -1;
3381 if (bijective_tests[i].bijective && !bijective)
3382 isl_die(ctx, isl_error_internal,
3383 "map not detected as bijective", return -1);
3384 if (!bijective_tests[i].bijective && bijective)
3385 isl_die(ctx, isl_error_internal,
3386 "map detected as bijective", return -1);
3389 return 0;
3392 /* Inputs for isl_pw_qpolynomial_gist tests.
3393 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
3395 struct {
3396 const char *pwqp;
3397 const char *set;
3398 const char *gist;
3399 } pwqp_gist_tests[] = {
3400 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
3401 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
3402 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
3403 "{ [i] -> -1/2 + 1/2 * i }" },
3404 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
3407 static int test_pwqp(struct isl_ctx *ctx)
3409 int i;
3410 const char *str;
3411 isl_set *set;
3412 isl_pw_qpolynomial *pwqp1, *pwqp2;
3413 int equal;
3415 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3416 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3418 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
3419 isl_dim_in, 1, 1);
3421 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
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 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
3431 str = pwqp_gist_tests[i].pwqp;
3432 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3433 str = pwqp_gist_tests[i].set;
3434 set = isl_set_read_from_str(ctx, str);
3435 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
3436 str = pwqp_gist_tests[i].gist;
3437 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3438 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3439 equal = isl_pw_qpolynomial_is_zero(pwqp1);
3440 isl_pw_qpolynomial_free(pwqp1);
3442 if (equal < 0)
3443 return -1;
3444 if (!equal)
3445 isl_die(ctx, isl_error_unknown,
3446 "unexpected result", return -1);
3449 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
3450 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3451 str = "{ [i] -> ([(2 * [i/2])/5]) }";
3452 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3454 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3456 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3458 isl_pw_qpolynomial_free(pwqp1);
3460 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3461 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3462 str = "{ [x] -> x }";
3463 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3465 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3467 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3469 isl_pw_qpolynomial_free(pwqp1);
3471 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3472 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3473 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3474 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3475 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3476 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3477 isl_pw_qpolynomial_free(pwqp1);
3479 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3480 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3481 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3482 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3483 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3484 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3485 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3486 isl_pw_qpolynomial_free(pwqp1);
3487 isl_pw_qpolynomial_free(pwqp2);
3488 if (equal < 0)
3489 return -1;
3490 if (!equal)
3491 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3493 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3494 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3495 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3496 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3497 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3498 isl_val_one(ctx));
3499 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3500 isl_pw_qpolynomial_free(pwqp1);
3501 isl_pw_qpolynomial_free(pwqp2);
3502 if (equal < 0)
3503 return -1;
3504 if (!equal)
3505 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3507 return 0;
3510 static int test_split_periods(isl_ctx *ctx)
3512 const char *str;
3513 isl_pw_qpolynomial *pwqp;
3515 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3516 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3517 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3518 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3520 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3522 isl_pw_qpolynomial_free(pwqp);
3524 if (!pwqp)
3525 return -1;
3527 return 0;
3530 static int test_union(isl_ctx *ctx)
3532 const char *str;
3533 isl_union_set *uset1, *uset2;
3534 isl_union_map *umap1, *umap2;
3535 int equal;
3537 str = "{ [i] : 0 <= i <= 1 }";
3538 uset1 = isl_union_set_read_from_str(ctx, str);
3539 str = "{ [1] -> [0] }";
3540 umap1 = isl_union_map_read_from_str(ctx, str);
3542 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3543 equal = isl_union_map_is_equal(umap1, umap2);
3545 isl_union_map_free(umap1);
3546 isl_union_map_free(umap2);
3548 if (equal < 0)
3549 return -1;
3550 if (!equal)
3551 isl_die(ctx, isl_error_unknown, "union maps not equal",
3552 return -1);
3554 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3555 umap1 = isl_union_map_read_from_str(ctx, str);
3556 str = "{ A[i]; B[i] }";
3557 uset1 = isl_union_set_read_from_str(ctx, str);
3559 uset2 = isl_union_map_domain(umap1);
3561 equal = isl_union_set_is_equal(uset1, uset2);
3563 isl_union_set_free(uset1);
3564 isl_union_set_free(uset2);
3566 if (equal < 0)
3567 return -1;
3568 if (!equal)
3569 isl_die(ctx, isl_error_unknown, "union sets not equal",
3570 return -1);
3572 return 0;
3575 /* Check that computing a bound of a non-zero polynomial over an unbounded
3576 * domain does not produce a rational value.
3577 * In particular, check that the upper bound is infinity.
3579 static int test_bound_unbounded_domain(isl_ctx *ctx)
3581 const char *str;
3582 isl_pw_qpolynomial *pwqp;
3583 isl_pw_qpolynomial_fold *pwf, *pwf2;
3584 isl_bool equal;
3586 str = "{ [m,n] -> -m * n }";
3587 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3588 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3589 str = "{ infty }";
3590 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3591 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3592 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
3593 isl_pw_qpolynomial_fold_free(pwf);
3594 isl_pw_qpolynomial_fold_free(pwf2);
3596 if (equal < 0)
3597 return -1;
3598 if (!equal)
3599 isl_die(ctx, isl_error_unknown,
3600 "expecting infinite polynomial bound", return -1);
3602 return 0;
3605 static int test_bound(isl_ctx *ctx)
3607 const char *str;
3608 isl_size dim;
3609 isl_pw_qpolynomial *pwqp;
3610 isl_pw_qpolynomial_fold *pwf;
3612 if (test_bound_unbounded_domain(ctx) < 0)
3613 return -1;
3615 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3616 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3617 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3618 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3619 isl_pw_qpolynomial_fold_free(pwf);
3620 if (dim < 0)
3621 return -1;
3622 if (dim != 4)
3623 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3624 return -1);
3626 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3627 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3628 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3629 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3630 isl_pw_qpolynomial_fold_free(pwf);
3631 if (dim < 0)
3632 return -1;
3633 if (dim != 1)
3634 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3635 return -1);
3637 return 0;
3640 /* isl_set is defined to isl_map internally, so the corresponding elements
3641 * are isl_basic_map objects.
3643 #undef EL_BASE
3644 #undef SET_BASE
3645 #define EL_BASE basic_map
3646 #define SET_BASE set
3647 #include "isl_test_list_templ.c"
3649 #undef EL_BASE
3650 #undef SET_BASE
3651 #define EL_BASE basic_set
3652 #define SET_BASE union_set
3653 #include "isl_test_list_templ.c"
3655 #undef EL_BASE
3656 #undef SET_BASE
3657 #define EL_BASE set
3658 #define SET_BASE union_set
3659 #include "isl_test_list_templ.c"
3661 #undef EL_BASE
3662 #undef SET_BASE
3663 #define EL_BASE basic_map
3664 #define SET_BASE map
3665 #include "isl_test_list_templ.c"
3667 #undef EL_BASE
3668 #undef SET_BASE
3669 #define EL_BASE map
3670 #define SET_BASE union_map
3671 #include "isl_test_list_templ.c"
3673 /* Check that the conversion from isl objects to lists works as expected.
3675 static int test_get_list(isl_ctx *ctx)
3677 if (test_get_list_basic_map_from_set(ctx, "{ [0]; [2]; [3] }"))
3678 return -1;
3679 if (test_get_list_basic_set_from_union_set(ctx, "{ A[0]; B[2]; B[3] }"))
3680 return -1;
3681 if (test_get_list_set_from_union_set(ctx, "{ A[0]; A[2]; B[3] }"))
3682 return -1;
3683 if (test_get_list_basic_map_from_map(ctx,
3684 "{ [0] -> [0]; [2] -> [0]; [3] -> [0] }"))
3685 return -1;
3686 if (test_get_list_map_from_union_map(ctx,
3687 "{ A[0] -> [0]; A[2] -> [0]; B[3] -> [0] }"))
3688 return -1;
3690 return 0;
3693 static int test_lift(isl_ctx *ctx)
3695 const char *str;
3696 isl_basic_map *bmap;
3697 isl_basic_set *bset;
3699 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3700 bset = isl_basic_set_read_from_str(ctx, str);
3701 bset = isl_basic_set_lift(bset);
3702 bmap = isl_basic_map_from_range(bset);
3703 bset = isl_basic_map_domain(bmap);
3704 isl_basic_set_free(bset);
3706 return 0;
3709 /* Check that isl_set_is_subset is not confused by identical
3710 * integer divisions.
3711 * The call to isl_set_normalize ensures that the equality constraints
3712 * a = b = 0 are discovered, turning e0 and e1 into identical
3713 * integer divisions. Any further simplification would remove
3714 * the duplicate integer divisions.
3716 static isl_stat test_subset_duplicate_integer_divisions(isl_ctx *ctx)
3718 const char *str;
3719 isl_bool is_subset;
3720 isl_set *set1, *set2;
3722 str = "{ [a, b, c, d] : "
3723 "exists (e0 = floor((a + d)/4), e1 = floor((d)/4), "
3724 "e2 = floor((-a - d + 4 *floor((a + d)/4))/10), "
3725 "e3 = floor((-d + 4*floor((d)/4))/10): "
3726 "10e2 = -a - 2c - d + 4e0 and 10e3 = -2c - d + 4e1 and "
3727 "b >= 0 and a <= 0 and b <= a) }";
3728 set1 = isl_set_read_from_str(ctx, str);
3729 set2 = isl_set_read_from_str(ctx, str);
3730 set2 = isl_set_normalize(set2);
3732 is_subset = isl_set_is_subset(set1, set2);
3734 isl_set_free(set1);
3735 isl_set_free(set2);
3737 if (is_subset < 0)
3738 return isl_stat_error;
3739 if (!is_subset)
3740 isl_die(ctx, isl_error_unknown,
3741 "set is not considered to be a subset of itself",
3742 return isl_stat_error);
3744 return isl_stat_ok;
3747 struct {
3748 const char *set1;
3749 const char *set2;
3750 int subset;
3751 } subset_tests[] = {
3752 { "{ [112, 0] }",
3753 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3754 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3755 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3756 { "{ [65] }",
3757 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3758 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3759 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3760 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3761 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3762 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3763 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3764 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3765 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3766 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3767 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3768 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3769 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3770 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3771 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3772 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3773 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3774 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3775 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3776 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3777 "4e0 <= -57 + i0 + i1)) or "
3778 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3779 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3780 "4e0 >= -61 + i0 + i1)) or "
3781 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3782 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3785 static int test_subset(isl_ctx *ctx)
3787 int i;
3788 isl_set *set1, *set2;
3789 int subset;
3791 if (test_subset_duplicate_integer_divisions(ctx) < 0)
3792 return -1;
3794 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3795 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3796 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3797 subset = isl_set_is_subset(set1, set2);
3798 isl_set_free(set1);
3799 isl_set_free(set2);
3800 if (subset < 0)
3801 return -1;
3802 if (subset != subset_tests[i].subset)
3803 isl_die(ctx, isl_error_unknown,
3804 "incorrect subset result", return -1);
3807 return 0;
3810 struct {
3811 const char *minuend;
3812 const char *subtrahend;
3813 const char *difference;
3814 } subtract_domain_tests[] = {
3815 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3816 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3817 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3820 static int test_subtract(isl_ctx *ctx)
3822 int i;
3823 isl_union_map *umap1, *umap2;
3824 isl_union_pw_multi_aff *upma1, *upma2;
3825 isl_union_set *uset;
3826 int equal;
3828 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3829 umap1 = isl_union_map_read_from_str(ctx,
3830 subtract_domain_tests[i].minuend);
3831 uset = isl_union_set_read_from_str(ctx,
3832 subtract_domain_tests[i].subtrahend);
3833 umap2 = isl_union_map_read_from_str(ctx,
3834 subtract_domain_tests[i].difference);
3835 umap1 = isl_union_map_subtract_domain(umap1, uset);
3836 equal = isl_union_map_is_equal(umap1, umap2);
3837 isl_union_map_free(umap1);
3838 isl_union_map_free(umap2);
3839 if (equal < 0)
3840 return -1;
3841 if (!equal)
3842 isl_die(ctx, isl_error_unknown,
3843 "incorrect subtract domain result", return -1);
3846 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3847 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3848 subtract_domain_tests[i].minuend);
3849 uset = isl_union_set_read_from_str(ctx,
3850 subtract_domain_tests[i].subtrahend);
3851 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3852 subtract_domain_tests[i].difference);
3853 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3854 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3855 isl_union_pw_multi_aff_free(upma1);
3856 isl_union_pw_multi_aff_free(upma2);
3857 if (equal < 0)
3858 return -1;
3859 if (!equal)
3860 isl_die(ctx, isl_error_unknown,
3861 "incorrect subtract domain result", return -1);
3864 return 0;
3867 /* Check that intersecting the empty basic set with another basic set
3868 * does not increase the number of constraints. In particular,
3869 * the empty basic set should maintain its canonical representation.
3871 static int test_intersect_1(isl_ctx *ctx)
3873 isl_size n1, n2;
3874 isl_basic_set *bset1, *bset2;
3876 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3877 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3878 n1 = isl_basic_set_n_constraint(bset1);
3879 bset1 = isl_basic_set_intersect(bset1, bset2);
3880 n2 = isl_basic_set_n_constraint(bset1);
3881 isl_basic_set_free(bset1);
3882 if (n1 < 0 || n2 < 0)
3883 return -1;
3884 if (n1 != n2)
3885 isl_die(ctx, isl_error_unknown,
3886 "number of constraints of empty set changed",
3887 return -1);
3889 return 0;
3892 /* Check that intersecting a set with itself does not cause
3893 * an explosion in the number of disjuncts.
3895 static isl_stat test_intersect_2(isl_ctx *ctx)
3897 int i;
3898 isl_set *set;
3900 set = isl_set_read_from_str(ctx, "{ [x,y] : x >= 0 or y >= 0 }");
3901 for (i = 0; i < 100; ++i)
3902 set = isl_set_intersect(set, isl_set_copy(set));
3903 isl_set_free(set);
3904 if (!set)
3905 return isl_stat_error;
3906 return isl_stat_ok;
3909 /* Perform some intersection tests.
3911 static int test_intersect(isl_ctx *ctx)
3913 if (test_intersect_1(ctx) < 0)
3914 return -1;
3915 if (test_intersect_2(ctx) < 0)
3916 return -1;
3918 return 0;
3921 int test_factorize(isl_ctx *ctx)
3923 const char *str;
3924 isl_basic_set *bset;
3925 isl_factorizer *f;
3927 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3928 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3929 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3930 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3931 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3932 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3933 "3i5 >= -2i0 - i2 + 3i4 }";
3934 bset = isl_basic_set_read_from_str(ctx, str);
3935 f = isl_basic_set_factorizer(bset);
3936 isl_basic_set_free(bset);
3937 isl_factorizer_free(f);
3938 if (!f)
3939 isl_die(ctx, isl_error_unknown,
3940 "failed to construct factorizer", return -1);
3942 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3943 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3944 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3945 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3946 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3947 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3948 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3949 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3950 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3951 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3952 bset = isl_basic_set_read_from_str(ctx, str);
3953 f = isl_basic_set_factorizer(bset);
3954 isl_basic_set_free(bset);
3955 isl_factorizer_free(f);
3956 if (!f)
3957 isl_die(ctx, isl_error_unknown,
3958 "failed to construct factorizer", return -1);
3960 return 0;
3963 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3965 int *injective = user;
3967 *injective = isl_map_is_injective(map);
3968 isl_map_free(map);
3970 if (*injective < 0 || !*injective)
3971 return isl_stat_error;
3973 return isl_stat_ok;
3976 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3977 const char *r, const char *s, int tilable, int parallel)
3979 int i;
3980 isl_union_set *D;
3981 isl_union_map *W, *R, *S;
3982 isl_union_map *empty;
3983 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3984 isl_union_map *validity, *proximity, *coincidence;
3985 isl_union_map *schedule;
3986 isl_union_map *test;
3987 isl_union_set *delta;
3988 isl_union_set *domain;
3989 isl_set *delta_set;
3990 isl_set *slice;
3991 isl_set *origin;
3992 isl_schedule_constraints *sc;
3993 isl_schedule *sched;
3994 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3995 isl_size n;
3997 D = isl_union_set_read_from_str(ctx, d);
3998 W = isl_union_map_read_from_str(ctx, w);
3999 R = isl_union_map_read_from_str(ctx, r);
4000 S = isl_union_map_read_from_str(ctx, s);
4002 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
4003 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
4005 empty = isl_union_map_empty(isl_union_map_get_space(S));
4006 isl_union_map_compute_flow(isl_union_map_copy(R),
4007 isl_union_map_copy(W), empty,
4008 isl_union_map_copy(S),
4009 &dep_raw, NULL, NULL, NULL);
4010 isl_union_map_compute_flow(isl_union_map_copy(W),
4011 isl_union_map_copy(W),
4012 isl_union_map_copy(R),
4013 isl_union_map_copy(S),
4014 &dep_waw, &dep_war, NULL, NULL);
4016 dep = isl_union_map_union(dep_waw, dep_war);
4017 dep = isl_union_map_union(dep, dep_raw);
4018 validity = isl_union_map_copy(dep);
4019 coincidence = isl_union_map_copy(dep);
4020 proximity = isl_union_map_copy(dep);
4022 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
4023 sc = isl_schedule_constraints_set_validity(sc, validity);
4024 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
4025 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4026 sched = isl_schedule_constraints_compute_schedule(sc);
4027 schedule = isl_schedule_get_map(sched);
4028 isl_schedule_free(sched);
4029 isl_union_map_free(W);
4030 isl_union_map_free(R);
4031 isl_union_map_free(S);
4033 is_injection = 1;
4034 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
4036 domain = isl_union_map_domain(isl_union_map_copy(schedule));
4037 is_complete = isl_union_set_is_subset(D, domain);
4038 isl_union_set_free(D);
4039 isl_union_set_free(domain);
4041 test = isl_union_map_reverse(isl_union_map_copy(schedule));
4042 test = isl_union_map_apply_range(test, dep);
4043 test = isl_union_map_apply_range(test, schedule);
4045 delta = isl_union_map_deltas(test);
4046 n = isl_union_set_n_set(delta);
4047 if (n < 0) {
4048 isl_union_set_free(delta);
4049 return -1;
4051 if (n == 0) {
4052 is_tilable = 1;
4053 is_parallel = 1;
4054 is_nonneg = 1;
4055 isl_union_set_free(delta);
4056 } else {
4057 isl_size dim;
4059 delta_set = isl_set_from_union_set(delta);
4061 slice = isl_set_universe(isl_set_get_space(delta_set));
4062 for (i = 0; i < tilable; ++i)
4063 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
4064 is_tilable = isl_set_is_subset(delta_set, slice);
4065 isl_set_free(slice);
4067 slice = isl_set_universe(isl_set_get_space(delta_set));
4068 for (i = 0; i < parallel; ++i)
4069 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
4070 is_parallel = isl_set_is_subset(delta_set, slice);
4071 isl_set_free(slice);
4073 origin = isl_set_universe(isl_set_get_space(delta_set));
4074 dim = isl_set_dim(origin, isl_dim_set);
4075 if (dim < 0)
4076 origin = isl_set_free(origin);
4077 for (i = 0; i < dim; ++i)
4078 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
4080 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
4081 delta_set = isl_set_lexmin(delta_set);
4083 is_nonneg = isl_set_is_equal(delta_set, origin);
4085 isl_set_free(origin);
4086 isl_set_free(delta_set);
4089 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
4090 is_injection < 0 || is_complete < 0)
4091 return -1;
4092 if (!is_complete)
4093 isl_die(ctx, isl_error_unknown,
4094 "generated schedule incomplete", return -1);
4095 if (!is_injection)
4096 isl_die(ctx, isl_error_unknown,
4097 "generated schedule not injective on each statement",
4098 return -1);
4099 if (!is_nonneg)
4100 isl_die(ctx, isl_error_unknown,
4101 "negative dependences in generated schedule",
4102 return -1);
4103 if (!is_tilable)
4104 isl_die(ctx, isl_error_unknown,
4105 "generated schedule not as tilable as expected",
4106 return -1);
4107 if (!is_parallel)
4108 isl_die(ctx, isl_error_unknown,
4109 "generated schedule not as parallel as expected",
4110 return -1);
4112 return 0;
4115 /* Compute a schedule for the given instance set, validity constraints,
4116 * proximity constraints and context and return a corresponding union map
4117 * representation.
4119 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
4120 const char *domain, const char *validity, const char *proximity,
4121 const char *context)
4123 isl_set *con;
4124 isl_union_set *dom;
4125 isl_union_map *dep;
4126 isl_union_map *prox;
4127 isl_schedule_constraints *sc;
4128 isl_schedule *schedule;
4129 isl_union_map *sched;
4131 con = isl_set_read_from_str(ctx, context);
4132 dom = isl_union_set_read_from_str(ctx, domain);
4133 dep = isl_union_map_read_from_str(ctx, validity);
4134 prox = isl_union_map_read_from_str(ctx, proximity);
4135 sc = isl_schedule_constraints_on_domain(dom);
4136 sc = isl_schedule_constraints_set_context(sc, con);
4137 sc = isl_schedule_constraints_set_validity(sc, dep);
4138 sc = isl_schedule_constraints_set_proximity(sc, prox);
4139 schedule = isl_schedule_constraints_compute_schedule(sc);
4140 sched = isl_schedule_get_map(schedule);
4141 isl_schedule_free(schedule);
4143 return sched;
4146 /* Compute a schedule for the given instance set, validity constraints and
4147 * proximity constraints and return a corresponding union map representation.
4149 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
4150 const char *domain, const char *validity, const char *proximity)
4152 return compute_schedule_with_context(ctx, domain, validity, proximity,
4153 "{ : }");
4156 /* Check that a schedule can be constructed on the given domain
4157 * with the given validity and proximity constraints.
4159 static int test_has_schedule(isl_ctx *ctx, const char *domain,
4160 const char *validity, const char *proximity)
4162 isl_union_map *sched;
4164 sched = compute_schedule(ctx, domain, validity, proximity);
4165 if (!sched)
4166 return -1;
4168 isl_union_map_free(sched);
4169 return 0;
4172 int test_special_schedule(isl_ctx *ctx, const char *domain,
4173 const char *validity, const char *proximity, const char *expected_sched)
4175 isl_union_map *sched1, *sched2;
4176 int equal;
4178 sched1 = compute_schedule(ctx, domain, validity, proximity);
4179 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
4181 equal = isl_union_map_is_equal(sched1, sched2);
4182 isl_union_map_free(sched1);
4183 isl_union_map_free(sched2);
4185 if (equal < 0)
4186 return -1;
4187 if (!equal)
4188 isl_die(ctx, isl_error_unknown, "unexpected schedule",
4189 return -1);
4191 return 0;
4194 /* Check that the schedule map is properly padded, i.e., that the range
4195 * lives in a single space.
4197 static int test_padded_schedule(isl_ctx *ctx)
4199 const char *str;
4200 isl_union_set *D;
4201 isl_union_map *validity, *proximity;
4202 isl_schedule_constraints *sc;
4203 isl_schedule *sched;
4204 isl_union_map *umap;
4205 isl_union_set *range;
4206 isl_set *set;
4208 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
4209 D = isl_union_set_read_from_str(ctx, str);
4210 validity = isl_union_map_empty(isl_union_set_get_space(D));
4211 proximity = isl_union_map_copy(validity);
4212 sc = isl_schedule_constraints_on_domain(D);
4213 sc = isl_schedule_constraints_set_validity(sc, validity);
4214 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4215 sched = isl_schedule_constraints_compute_schedule(sc);
4216 umap = isl_schedule_get_map(sched);
4217 isl_schedule_free(sched);
4218 range = isl_union_map_range(umap);
4219 set = isl_set_from_union_set(range);
4220 isl_set_free(set);
4222 if (!set)
4223 return -1;
4225 return 0;
4228 /* Check that conditional validity constraints are also taken into
4229 * account across bands.
4230 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
4231 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
4232 * and then check that the adjacent order constraint C[2,1]->D[2,0]
4233 * is enforced by the rest of the schedule.
4235 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
4237 const char *str;
4238 isl_union_set *domain;
4239 isl_union_map *validity, *proximity, *condition;
4240 isl_union_map *sink, *source, *dep;
4241 isl_schedule_constraints *sc;
4242 isl_schedule *schedule;
4243 isl_union_access_info *access;
4244 isl_union_flow *flow;
4245 int empty;
4247 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4248 "A[k] : k >= 1 and k <= -1 + n; "
4249 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4250 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
4251 domain = isl_union_set_read_from_str(ctx, str);
4252 sc = isl_schedule_constraints_on_domain(domain);
4253 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
4254 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4255 "D[k, i] -> C[1 + k, i] : "
4256 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4257 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
4258 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
4259 validity = isl_union_map_read_from_str(ctx, str);
4260 sc = isl_schedule_constraints_set_validity(sc, validity);
4261 str = "[n] -> { C[k, i] -> D[k, i] : "
4262 "0 <= i <= -1 + k and k <= -1 + n }";
4263 proximity = isl_union_map_read_from_str(ctx, str);
4264 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4265 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
4266 "i <= -1 + k and i >= 1 and k <= -2 + n; "
4267 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
4268 "k <= -1 + n and i >= 0 and i <= -2 + k }";
4269 condition = isl_union_map_read_from_str(ctx, str);
4270 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
4271 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4272 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
4273 "i >= 0 and i <= -1 + k and k <= -1 + n and "
4274 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
4275 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
4276 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4277 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
4278 "k <= -1 + n and i >= 0 and i <= -1 + k and "
4279 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
4280 validity = isl_union_map_read_from_str(ctx, str);
4281 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4282 validity);
4283 schedule = isl_schedule_constraints_compute_schedule(sc);
4284 str = "{ D[2,0] -> [] }";
4285 sink = isl_union_map_read_from_str(ctx, str);
4286 access = isl_union_access_info_from_sink(sink);
4287 str = "{ C[2,1] -> [] }";
4288 source = isl_union_map_read_from_str(ctx, str);
4289 access = isl_union_access_info_set_must_source(access, source);
4290 access = isl_union_access_info_set_schedule(access, schedule);
4291 flow = isl_union_access_info_compute_flow(access);
4292 dep = isl_union_flow_get_must_dependence(flow);
4293 isl_union_flow_free(flow);
4294 empty = isl_union_map_is_empty(dep);
4295 isl_union_map_free(dep);
4297 if (empty < 0)
4298 return -1;
4299 if (empty)
4300 isl_die(ctx, isl_error_unknown,
4301 "conditional validity not respected", return -1);
4303 return 0;
4306 /* Check that the test for violated conditional validity constraints
4307 * is not confused by domain compression.
4308 * In particular, earlier versions of isl would apply
4309 * a schedule on the compressed domains to the original domains,
4310 * resulting in a failure to detect that the default schedule
4311 * violates the conditional validity constraints.
4313 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
4315 const char *str;
4316 isl_bool empty;
4317 isl_union_set *domain;
4318 isl_union_map *validity, *condition;
4319 isl_schedule_constraints *sc;
4320 isl_schedule *schedule;
4321 isl_union_map *umap;
4322 isl_map *map, *ge;
4324 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
4325 domain = isl_union_set_read_from_str(ctx, str);
4326 sc = isl_schedule_constraints_on_domain(domain);
4327 str = "{ B[1, i] -> A[0, i + 1] }";
4328 condition = isl_union_map_read_from_str(ctx, str);
4329 str = "{ A[0, i] -> B[1, i - 1] }";
4330 validity = isl_union_map_read_from_str(ctx, str);
4331 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4332 isl_union_map_copy(validity));
4333 schedule = isl_schedule_constraints_compute_schedule(sc);
4334 umap = isl_schedule_get_map(schedule);
4335 isl_schedule_free(schedule);
4336 validity = isl_union_map_apply_domain(validity,
4337 isl_union_map_copy(umap));
4338 validity = isl_union_map_apply_range(validity, umap);
4339 map = isl_map_from_union_map(validity);
4340 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4341 map = isl_map_intersect(map, ge);
4342 empty = isl_map_is_empty(map);
4343 isl_map_free(map);
4345 if (empty < 0)
4346 return -1;
4347 if (!empty)
4348 isl_die(ctx, isl_error_unknown,
4349 "conditional validity constraints not satisfied",
4350 return -1);
4352 return 0;
4355 /* Input for testing of schedule construction based on
4356 * conditional constraints.
4358 * domain is the iteration domain
4359 * flow are the flow dependences, which determine the validity and
4360 * proximity constraints
4361 * condition are the conditions on the conditional validity constraints
4362 * conditional_validity are the conditional validity constraints
4363 * outer_band_n is the expected number of members in the outer band
4365 struct {
4366 const char *domain;
4367 const char *flow;
4368 const char *condition;
4369 const char *conditional_validity;
4370 int outer_band_n;
4371 } live_range_tests[] = {
4372 /* Contrived example that illustrates that we need to keep
4373 * track of tagged condition dependences and
4374 * tagged conditional validity dependences
4375 * in isl_sched_edge separately.
4376 * In particular, the conditional validity constraints on A
4377 * cannot be satisfied,
4378 * but they can be ignored because there are no corresponding
4379 * condition constraints. However, we do have an additional
4380 * conditional validity constraint that maps to the same
4381 * dependence relation
4382 * as the condition constraint on B. If we did not make a distinction
4383 * between tagged condition and tagged conditional validity
4384 * dependences, then we
4385 * could end up treating this shared dependence as an condition
4386 * constraint on A, forcing a localization of the conditions,
4387 * which is impossible.
4389 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
4390 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
4391 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
4392 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4393 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4394 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
4397 /* TACO 2013 Fig. 7 */
4398 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4399 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4400 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4401 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
4402 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
4403 "0 <= i < n and 0 <= j < n - 1 }",
4404 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
4405 "0 <= i < n and 0 <= j < j' < n;"
4406 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
4407 "0 <= i < i' < n and 0 <= j,j' < n;"
4408 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
4409 "0 <= i,j,j' < n and j < j' }",
4412 /* TACO 2013 Fig. 7, without tags */
4413 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4414 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4415 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4416 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4417 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4418 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
4419 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
4420 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
4423 /* TACO 2013 Fig. 12 */
4424 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
4425 "S3[i,3] : 0 <= i <= 1 }",
4426 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
4427 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
4428 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
4429 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
4430 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4431 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
4432 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4433 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
4434 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
4435 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
4436 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
4441 /* Test schedule construction based on conditional constraints.
4442 * In particular, check the number of members in the outer band node
4443 * as an indication of whether tiling is possible or not.
4445 static int test_conditional_schedule_constraints(isl_ctx *ctx)
4447 int i;
4448 isl_union_set *domain;
4449 isl_union_map *condition;
4450 isl_union_map *flow;
4451 isl_union_map *validity;
4452 isl_schedule_constraints *sc;
4453 isl_schedule *schedule;
4454 isl_schedule_node *node;
4455 isl_size n_member;
4457 if (test_special_conditional_schedule_constraints(ctx) < 0)
4458 return -1;
4459 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
4460 return -1;
4462 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
4463 domain = isl_union_set_read_from_str(ctx,
4464 live_range_tests[i].domain);
4465 flow = isl_union_map_read_from_str(ctx,
4466 live_range_tests[i].flow);
4467 condition = isl_union_map_read_from_str(ctx,
4468 live_range_tests[i].condition);
4469 validity = isl_union_map_read_from_str(ctx,
4470 live_range_tests[i].conditional_validity);
4471 sc = isl_schedule_constraints_on_domain(domain);
4472 sc = isl_schedule_constraints_set_validity(sc,
4473 isl_union_map_copy(flow));
4474 sc = isl_schedule_constraints_set_proximity(sc, flow);
4475 sc = isl_schedule_constraints_set_conditional_validity(sc,
4476 condition, validity);
4477 schedule = isl_schedule_constraints_compute_schedule(sc);
4478 node = isl_schedule_get_root(schedule);
4479 while (node &&
4480 isl_schedule_node_get_type(node) != isl_schedule_node_band)
4481 node = isl_schedule_node_first_child(node);
4482 n_member = isl_schedule_node_band_n_member(node);
4483 isl_schedule_node_free(node);
4484 isl_schedule_free(schedule);
4486 if (!schedule || n_member < 0)
4487 return -1;
4488 if (n_member != live_range_tests[i].outer_band_n)
4489 isl_die(ctx, isl_error_unknown,
4490 "unexpected number of members in outer band",
4491 return -1);
4493 return 0;
4496 /* Check that the schedule computed for the given instance set and
4497 * dependence relation strongly satisfies the dependences.
4498 * In particular, check that no instance is scheduled before
4499 * or together with an instance on which it depends.
4500 * Earlier versions of isl would produce a schedule that
4501 * only weakly satisfies the dependences.
4503 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
4505 const char *domain, *dep;
4506 isl_union_map *D, *schedule;
4507 isl_map *map, *ge;
4508 int empty;
4510 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
4511 "A[i0] : 0 <= i0 <= 1 }";
4512 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
4513 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
4514 schedule = compute_schedule(ctx, domain, dep, dep);
4515 D = isl_union_map_read_from_str(ctx, dep);
4516 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
4517 D = isl_union_map_apply_range(D, schedule);
4518 map = isl_map_from_union_map(D);
4519 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4520 map = isl_map_intersect(map, ge);
4521 empty = isl_map_is_empty(map);
4522 isl_map_free(map);
4524 if (empty < 0)
4525 return -1;
4526 if (!empty)
4527 isl_die(ctx, isl_error_unknown,
4528 "dependences not strongly satisfied", return -1);
4530 return 0;
4533 /* Compute a schedule for input where the instance set constraints
4534 * conflict with the context constraints.
4535 * Earlier versions of isl did not properly handle this situation.
4537 static int test_conflicting_context_schedule(isl_ctx *ctx)
4539 isl_union_map *schedule;
4540 const char *domain, *context;
4542 domain = "[n] -> { A[] : n >= 0 }";
4543 context = "[n] -> { : n < 0 }";
4544 schedule = compute_schedule_with_context(ctx,
4545 domain, "{}", "{}", context);
4546 isl_union_map_free(schedule);
4548 if (!schedule)
4549 return -1;
4551 return 0;
4554 /* Check that a set of schedule constraints that only allow for
4555 * a coalescing schedule still produces a schedule even if the user
4556 * request a non-coalescing schedule. Earlier versions of isl
4557 * would not handle this case correctly.
4559 static int test_coalescing_schedule(isl_ctx *ctx)
4561 const char *domain, *dep;
4562 isl_union_set *I;
4563 isl_union_map *D;
4564 isl_schedule_constraints *sc;
4565 isl_schedule *schedule;
4566 int treat_coalescing;
4568 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4569 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4570 I = isl_union_set_read_from_str(ctx, domain);
4571 D = isl_union_map_read_from_str(ctx, dep);
4572 sc = isl_schedule_constraints_on_domain(I);
4573 sc = isl_schedule_constraints_set_validity(sc, D);
4574 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4575 isl_options_set_schedule_treat_coalescing(ctx, 1);
4576 schedule = isl_schedule_constraints_compute_schedule(sc);
4577 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4578 isl_schedule_free(schedule);
4579 if (!schedule)
4580 return -1;
4581 return 0;
4584 /* Check that the scheduler does not perform any needless
4585 * compound skewing. Earlier versions of isl would compute
4586 * schedules in terms of transformed schedule coefficients and
4587 * would not accurately keep track of the sum of the original
4588 * schedule coefficients. It could then produce the schedule
4589 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
4590 * for the input below instead of the schedule below.
4592 static int test_skewing_schedule(isl_ctx *ctx)
4594 const char *D, *V, *P, *S;
4596 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
4597 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
4598 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
4599 "-1 <= a-i + b-j + c-k <= 1 }";
4600 P = "{ }";
4601 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
4603 return test_special_schedule(ctx, D, V, P, S);
4606 int test_schedule(isl_ctx *ctx)
4608 const char *D, *W, *R, *V, *P, *S;
4609 int max_coincidence;
4610 int treat_coalescing;
4612 /* Handle resulting schedule with zero bands. */
4613 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4614 return -1;
4616 /* Jacobi */
4617 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4618 W = "{ S1[t,i] -> a[t,i] }";
4619 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4620 "S1[t,i] -> a[t-1,i+1] }";
4621 S = "{ S1[t,i] -> [t,i] }";
4622 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4623 return -1;
4625 /* Fig. 5 of CC2008 */
4626 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
4627 "j <= -1 + N }";
4628 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
4629 "j >= 2 and j <= -1 + N }";
4630 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
4631 "j >= 2 and j <= -1 + N; "
4632 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
4633 "j >= 2 and j <= -1 + N }";
4634 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4635 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4636 return -1;
4638 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
4639 W = "{ S1[i] -> a[i] }";
4640 R = "{ S2[i] -> a[i+1] }";
4641 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4642 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4643 return -1;
4645 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
4646 W = "{ S1[i] -> a[i] }";
4647 R = "{ S2[i] -> a[9-i] }";
4648 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4649 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4650 return -1;
4652 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4653 W = "{ S1[i] -> a[i] }";
4654 R = "[N] -> { S2[i] -> a[N-1-i] }";
4655 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4656 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4657 return -1;
4659 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4660 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4661 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4662 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4663 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4664 return -1;
4666 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4667 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4668 R = "{ S2[i,j] -> a[i-1,j] }";
4669 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4670 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4671 return -1;
4673 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4674 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4675 R = "{ S2[i,j] -> a[i,j-1] }";
4676 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4677 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4678 return -1;
4680 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4681 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4682 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4683 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4684 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4685 "S_0[] -> [0, 0, 0] }";
4686 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4687 return -1;
4688 ctx->opt->schedule_parametric = 0;
4689 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4690 return -1;
4691 ctx->opt->schedule_parametric = 1;
4693 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4694 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4695 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4696 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4697 "S4[i] -> a[i,N] }";
4698 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4699 "S4[i] -> [4,i,0] }";
4700 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4701 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4702 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4703 return -1;
4704 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4706 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4707 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4708 "j <= N }";
4709 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4710 "j <= N; "
4711 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4712 "j <= N }";
4713 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4714 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4715 return -1;
4717 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4718 " S_2[t] : t >= 0 and t <= -1 + N; "
4719 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4720 "i <= -1 + N }";
4721 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4722 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4723 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4724 "i >= 0 and i <= -1 + N }";
4725 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4726 "i >= 0 and i <= -1 + N; "
4727 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4728 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4729 " S_0[t] -> [0, t, 0] }";
4731 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4732 return -1;
4733 ctx->opt->schedule_parametric = 0;
4734 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4735 return -1;
4736 ctx->opt->schedule_parametric = 1;
4738 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4739 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4740 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4741 return -1;
4743 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4744 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4745 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
4746 "j >= 0 and j <= -1 + N; "
4747 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4748 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4749 "j >= 0 and j <= -1 + N; "
4750 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4751 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4752 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4753 return -1;
4755 D = "{ S_0[i] : i >= 0 }";
4756 W = "{ S_0[i] -> a[i] : i >= 0 }";
4757 R = "{ S_0[i] -> a[0] : i >= 0 }";
4758 S = "{ S_0[i] -> [0, i, 0] }";
4759 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4760 return -1;
4762 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4763 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4764 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4765 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4766 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4767 return -1;
4769 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4770 "k <= -1 + n and k >= 0 }";
4771 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4772 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4773 "k <= -1 + n and k >= 0; "
4774 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4775 "k <= -1 + n and k >= 0; "
4776 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4777 "k <= -1 + n and k >= 0 }";
4778 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4779 ctx->opt->schedule_outer_coincidence = 1;
4780 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4781 return -1;
4782 ctx->opt->schedule_outer_coincidence = 0;
4784 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4785 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4786 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4787 "Stmt_for_body24[i0, i1, 1, 0]:"
4788 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4789 "Stmt_for_body7[i0, i1, i2]:"
4790 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4791 "i2 <= 7 }";
4793 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4794 "Stmt_for_body24[1, i1, i2, i3]:"
4795 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4796 "i2 >= 1;"
4797 "Stmt_for_body24[0, i1, i2, i3] -> "
4798 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4799 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4800 "i3 >= 0;"
4801 "Stmt_for_body24[0, i1, i2, i3] ->"
4802 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4803 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4804 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4805 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4806 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4807 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4808 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4809 "i1 <= 6 and i1 >= 0;"
4810 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4811 "Stmt_for_body7[i0, i1, i2] -> "
4812 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4813 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4814 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4815 "Stmt_for_body7[i0, i1, i2] -> "
4816 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4817 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4818 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4819 P = V;
4821 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4822 isl_options_set_schedule_treat_coalescing(ctx, 0);
4823 if (test_has_schedule(ctx, D, V, P) < 0)
4824 return -1;
4825 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4827 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4828 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4829 "j >= 1 and j <= 7;"
4830 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4831 "j >= 1 and j <= 8 }";
4832 P = "{ }";
4833 S = "{ S_0[i, j] -> [i + j, i] }";
4834 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4835 if (test_special_schedule(ctx, D, V, P, S) < 0)
4836 return -1;
4837 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4839 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4840 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4841 "j >= 0 and j <= -1 + i }";
4842 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4843 "i <= -1 + N and j >= 0;"
4844 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4845 "i <= -2 + N }";
4846 P = "{ }";
4847 S = "{ S_0[i, j] -> [i, j] }";
4848 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4849 if (test_special_schedule(ctx, D, V, P, S) < 0)
4850 return -1;
4851 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4853 /* Test both algorithms on a case with only proximity dependences. */
4854 D = "{ S[i,j] : 0 <= i <= 10 }";
4855 V = "{ }";
4856 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4857 S = "{ S[i, j] -> [j, i] }";
4858 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4859 if (test_special_schedule(ctx, D, V, P, S) < 0)
4860 return -1;
4861 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4862 if (test_special_schedule(ctx, D, V, P, S) < 0)
4863 return -1;
4865 D = "{ A[a]; B[] }";
4866 V = "{}";
4867 P = "{ A[a] -> B[] }";
4868 if (test_has_schedule(ctx, D, V, P) < 0)
4869 return -1;
4871 if (test_padded_schedule(ctx) < 0)
4872 return -1;
4874 /* Check that check for progress is not confused by rational
4875 * solution.
4877 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4878 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4879 "i0 <= -2 + N; "
4880 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4881 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4882 P = "{}";
4883 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4884 if (test_has_schedule(ctx, D, V, P) < 0)
4885 return -1;
4886 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4888 /* Check that we allow schedule rows that are only non-trivial
4889 * on some full-dimensional domains.
4891 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4892 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4893 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4894 P = "{}";
4895 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4896 if (test_has_schedule(ctx, D, V, P) < 0)
4897 return -1;
4898 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4900 if (test_conditional_schedule_constraints(ctx) < 0)
4901 return -1;
4903 if (test_strongly_satisfying_schedule(ctx) < 0)
4904 return -1;
4906 if (test_conflicting_context_schedule(ctx) < 0)
4907 return -1;
4909 if (test_coalescing_schedule(ctx) < 0)
4910 return -1;
4911 if (test_skewing_schedule(ctx) < 0)
4912 return -1;
4914 return 0;
4917 /* Perform scheduling tests using the whole component scheduler.
4919 static int test_schedule_whole(isl_ctx *ctx)
4921 int whole;
4922 int r;
4924 whole = isl_options_get_schedule_whole_component(ctx);
4925 isl_options_set_schedule_whole_component(ctx, 1);
4926 r = test_schedule(ctx);
4927 isl_options_set_schedule_whole_component(ctx, whole);
4929 return r;
4932 /* Perform scheduling tests using the incremental scheduler.
4934 static int test_schedule_incremental(isl_ctx *ctx)
4936 int whole;
4937 int r;
4939 whole = isl_options_get_schedule_whole_component(ctx);
4940 isl_options_set_schedule_whole_component(ctx, 0);
4941 r = test_schedule(ctx);
4942 isl_options_set_schedule_whole_component(ctx, whole);
4944 return r;
4947 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4949 isl_union_map *umap;
4950 int test;
4952 umap = isl_union_map_read_from_str(ctx, str);
4953 test = isl_union_map_plain_is_injective(umap);
4954 isl_union_map_free(umap);
4955 if (test < 0)
4956 return -1;
4957 if (test == injective)
4958 return 0;
4959 if (injective)
4960 isl_die(ctx, isl_error_unknown,
4961 "map not detected as injective", return -1);
4962 else
4963 isl_die(ctx, isl_error_unknown,
4964 "map detected as injective", return -1);
4967 int test_injective(isl_ctx *ctx)
4969 const char *str;
4971 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4972 return -1;
4973 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4974 return -1;
4975 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4976 return -1;
4977 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4978 return -1;
4979 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4980 return -1;
4981 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4982 return -1;
4983 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4984 return -1;
4985 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4986 return -1;
4988 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4989 if (test_plain_injective(ctx, str, 1))
4990 return -1;
4991 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4992 if (test_plain_injective(ctx, str, 0))
4993 return -1;
4995 return 0;
4998 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
5000 isl_aff *aff2;
5001 int equal;
5003 if (!aff)
5004 return -1;
5006 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
5007 equal = isl_aff_plain_is_equal(aff, aff2);
5008 isl_aff_free(aff2);
5010 return equal;
5013 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
5015 int equal;
5017 equal = aff_plain_is_equal(aff, str);
5018 if (equal < 0)
5019 return -1;
5020 if (!equal)
5021 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
5022 "result not as expected", return -1);
5023 return 0;
5026 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
5028 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
5029 const char *str)
5031 isl_ctx *ctx;
5032 isl_pw_multi_aff *pma2;
5033 int equal;
5035 if (!pma)
5036 return -1;
5038 ctx = isl_pw_multi_aff_get_ctx(pma);
5039 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5040 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
5041 isl_pw_multi_aff_free(pma2);
5043 return equal;
5046 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
5047 * represented by "str".
5049 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
5050 const char *str)
5052 int equal;
5054 equal = pw_multi_aff_plain_is_equal(pma, str);
5055 if (equal < 0)
5056 return -1;
5057 if (!equal)
5058 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
5059 "result not as expected", return -1);
5060 return 0;
5063 /* Is "upa" obviously equal to the isl_union_pw_aff represented by "str"?
5065 static isl_bool union_pw_aff_plain_is_equal(__isl_keep isl_union_pw_aff *upa,
5066 const char *str)
5068 isl_ctx *ctx;
5069 isl_union_pw_aff *upa2;
5070 isl_bool equal;
5072 if (!upa)
5073 return isl_bool_error;
5075 ctx = isl_union_pw_aff_get_ctx(upa);
5076 upa2 = isl_union_pw_aff_read_from_str(ctx, str);
5077 equal = isl_union_pw_aff_plain_is_equal(upa, upa2);
5078 isl_union_pw_aff_free(upa2);
5080 return equal;
5083 /* Check that "upa" is obviously equal to the isl_union_pw_aff
5084 * represented by "str".
5086 static isl_stat union_pw_aff_check_plain_equal(__isl_keep isl_union_pw_aff *upa,
5087 const char *str)
5089 isl_bool equal;
5091 equal = union_pw_aff_plain_is_equal(upa, str);
5092 if (equal < 0)
5093 return isl_stat_error;
5094 if (!equal)
5095 isl_die(isl_union_pw_aff_get_ctx(upa), isl_error_unknown,
5096 "result not as expected", return isl_stat_error);
5097 return isl_stat_ok;
5100 /* Basic tests on isl_union_pw_aff.
5102 * In particular, check that isl_union_pw_aff_aff_on_domain
5103 * aligns the parameters of the input objects and
5104 * that isl_union_pw_aff_param_on_domain_id properly
5105 * introduces the parameter.
5107 static int test_upa(isl_ctx *ctx)
5109 const char *str;
5110 isl_id *id;
5111 isl_aff *aff;
5112 isl_union_set *domain;
5113 isl_union_pw_aff *upa;
5114 isl_stat ok;
5116 aff = isl_aff_read_from_str(ctx, "[N] -> { [N] }");
5117 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5118 domain = isl_union_set_read_from_str(ctx, str);
5119 upa = isl_union_pw_aff_aff_on_domain(domain, aff);
5120 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5121 ok = union_pw_aff_check_plain_equal(upa, str);
5122 isl_union_pw_aff_free(upa);
5123 if (ok < 0)
5124 return -1;
5126 id = isl_id_alloc(ctx, "N", NULL);
5127 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5128 domain = isl_union_set_read_from_str(ctx, str);
5129 upa = isl_union_pw_aff_param_on_domain_id(domain, id);
5130 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5131 ok = union_pw_aff_check_plain_equal(upa, str);
5132 isl_union_pw_aff_free(upa);
5133 if (ok < 0)
5134 return -1;
5136 return 0;
5139 struct {
5140 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5141 __isl_take isl_aff *aff2);
5142 } aff_bin_op[] = {
5143 ['+'] = { &isl_aff_add },
5144 ['-'] = { &isl_aff_sub },
5145 ['*'] = { &isl_aff_mul },
5146 ['/'] = { &isl_aff_div },
5149 struct {
5150 const char *arg1;
5151 unsigned char op;
5152 const char *arg2;
5153 const char *res;
5154 } aff_bin_tests[] = {
5155 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
5156 "{ [i] -> [2i] }" },
5157 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
5158 "{ [i] -> [0] }" },
5159 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
5160 "{ [i] -> [2i] }" },
5161 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
5162 "{ [i] -> [2i] }" },
5163 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
5164 "{ [i] -> [i/2] }" },
5165 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
5166 "{ [i] -> [i] }" },
5167 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
5168 "{ [i] -> [NaN] }" },
5169 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
5170 "{ [i] -> [NaN] }" },
5171 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
5172 "{ [i] -> [NaN] }" },
5173 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
5174 "{ [i] -> [NaN] }" },
5175 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
5176 "{ [i] -> [NaN] }" },
5177 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
5178 "{ [i] -> [NaN] }" },
5179 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
5180 "{ [i] -> [NaN] }" },
5181 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
5182 "{ [i] -> [NaN] }" },
5183 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
5184 "{ [i] -> [NaN] }" },
5185 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
5186 "{ [i] -> [NaN] }" },
5187 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
5188 "{ [i] -> [NaN] }" },
5189 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
5190 "{ [i] -> [NaN] }" },
5193 /* Perform some basic tests of binary operations on isl_aff objects.
5195 static int test_bin_aff(isl_ctx *ctx)
5197 int i;
5198 isl_aff *aff1, *aff2, *res;
5199 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5200 __isl_take isl_aff *aff2);
5201 int ok;
5203 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
5204 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
5205 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
5206 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
5207 fn = aff_bin_op[aff_bin_tests[i].op].fn;
5208 aff1 = fn(aff1, aff2);
5209 if (isl_aff_is_nan(res))
5210 ok = isl_aff_is_nan(aff1);
5211 else
5212 ok = isl_aff_plain_is_equal(aff1, res);
5213 isl_aff_free(aff1);
5214 isl_aff_free(res);
5215 if (ok < 0)
5216 return -1;
5217 if (!ok)
5218 isl_die(ctx, isl_error_unknown,
5219 "unexpected result", return -1);
5222 return 0;
5225 struct {
5226 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
5227 __isl_take isl_pw_aff *pa2);
5228 } pw_aff_bin_op[] = {
5229 ['m'] = { &isl_pw_aff_min },
5230 ['M'] = { &isl_pw_aff_max },
5233 /* Inputs for binary isl_pw_aff operation tests.
5234 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
5235 * defined by pw_aff_bin_op, and "res" is the expected result.
5237 struct {
5238 const char *arg1;
5239 unsigned char op;
5240 const char *arg2;
5241 const char *res;
5242 } pw_aff_bin_tests[] = {
5243 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
5244 "{ [i] -> [i] }" },
5245 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
5246 "{ [i] -> [i] }" },
5247 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
5248 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
5249 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
5250 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
5251 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
5252 "{ [i] -> [NaN] }" },
5253 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
5254 "{ [i] -> [NaN] }" },
5257 /* Perform some basic tests of binary operations on isl_pw_aff objects.
5259 static int test_bin_pw_aff(isl_ctx *ctx)
5261 int i;
5262 isl_bool ok;
5263 isl_pw_aff *pa1, *pa2, *res;
5265 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
5266 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
5267 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
5268 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
5269 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
5270 if (isl_pw_aff_involves_nan(res))
5271 ok = isl_pw_aff_involves_nan(pa1);
5272 else
5273 ok = isl_pw_aff_plain_is_equal(pa1, res);
5274 isl_pw_aff_free(pa1);
5275 isl_pw_aff_free(res);
5276 if (ok < 0)
5277 return -1;
5278 if (!ok)
5279 isl_die(ctx, isl_error_unknown,
5280 "unexpected result", return -1);
5283 return 0;
5286 struct {
5287 __isl_give isl_union_pw_multi_aff *(*fn)(
5288 __isl_take isl_union_pw_multi_aff *upma1,
5289 __isl_take isl_union_pw_multi_aff *upma2);
5290 const char *arg1;
5291 const char *arg2;
5292 const char *res;
5293 } upma_bin_tests[] = {
5294 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
5295 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
5296 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
5297 "{ B[x] -> [2] : x >= 0 }",
5298 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
5299 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
5300 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
5301 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
5302 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
5303 "D[i] -> B[2] : i >= 5 }" },
5304 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5305 "{ B[x] -> C[2] : x > 0 }",
5306 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
5307 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5308 "{ B[x] -> A[2] : x >= 0 }",
5309 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
5312 /* Perform some basic tests of binary operations on
5313 * isl_union_pw_multi_aff objects.
5315 static int test_bin_upma(isl_ctx *ctx)
5317 int i;
5318 isl_union_pw_multi_aff *upma1, *upma2, *res;
5319 int ok;
5321 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
5322 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5323 upma_bin_tests[i].arg1);
5324 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5325 upma_bin_tests[i].arg2);
5326 res = isl_union_pw_multi_aff_read_from_str(ctx,
5327 upma_bin_tests[i].res);
5328 upma1 = upma_bin_tests[i].fn(upma1, upma2);
5329 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
5330 isl_union_pw_multi_aff_free(upma1);
5331 isl_union_pw_multi_aff_free(res);
5332 if (ok < 0)
5333 return -1;
5334 if (!ok)
5335 isl_die(ctx, isl_error_unknown,
5336 "unexpected result", return -1);
5339 return 0;
5342 struct {
5343 __isl_give isl_union_pw_multi_aff *(*fn)(
5344 __isl_take isl_union_pw_multi_aff *upma1,
5345 __isl_take isl_union_pw_multi_aff *upma2);
5346 const char *arg1;
5347 const char *arg2;
5348 } upma_bin_fail_tests[] = {
5349 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5350 "{ B[x] -> C[2] : x >= 0 }" },
5353 /* Perform some basic tests of binary operations on
5354 * isl_union_pw_multi_aff objects that are expected to fail.
5356 static int test_bin_upma_fail(isl_ctx *ctx)
5358 int i, n;
5359 isl_union_pw_multi_aff *upma1, *upma2;
5360 int on_error;
5362 on_error = isl_options_get_on_error(ctx);
5363 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
5364 n = ARRAY_SIZE(upma_bin_fail_tests);
5365 for (i = 0; i < n; ++i) {
5366 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5367 upma_bin_fail_tests[i].arg1);
5368 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5369 upma_bin_fail_tests[i].arg2);
5370 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
5371 isl_union_pw_multi_aff_free(upma1);
5372 if (upma1)
5373 break;
5375 isl_options_set_on_error(ctx, on_error);
5376 if (i < n)
5377 isl_die(ctx, isl_error_unknown,
5378 "operation not expected to succeed", return -1);
5380 return 0;
5383 /* Inputs for basic tests of unary operations on isl_multi_pw_aff objects.
5384 * "fn" is the function that is tested.
5385 * "arg" is a string description of the input.
5386 * "res" is a string description of the expected result.
5388 struct {
5389 __isl_give isl_multi_pw_aff *(*fn)(__isl_take isl_multi_pw_aff *mpa);
5390 const char *arg;
5391 const char *res;
5392 } mpa_un_tests[] = {
5393 { &isl_multi_pw_aff_range_factor_domain,
5394 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5395 "{ A[x] -> B[(1 : x >= 5)] }" },
5396 { &isl_multi_pw_aff_range_factor_range,
5397 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5398 "{ A[y] -> C[(2 : y <= 10)] }" },
5399 { &isl_multi_pw_aff_range_factor_domain,
5400 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5401 "{ A[x] -> B[(1 : x >= 5)] }" },
5402 { &isl_multi_pw_aff_range_factor_range,
5403 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5404 "{ A[y] -> C[] }" },
5405 { &isl_multi_pw_aff_range_factor_domain,
5406 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5407 "{ A[x] -> B[] }" },
5408 { &isl_multi_pw_aff_range_factor_range,
5409 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5410 "{ A[y] -> C[(2 : y <= 10)] }" },
5411 { &isl_multi_pw_aff_range_factor_domain,
5412 "{ A[x] -> [B[] -> C[]] }",
5413 "{ A[x] -> B[] }" },
5414 { &isl_multi_pw_aff_range_factor_range,
5415 "{ A[x] -> [B[] -> C[]] }",
5416 "{ A[y] -> C[] }" },
5417 { &isl_multi_pw_aff_factor_range,
5418 "{ [B[] -> C[]] }",
5419 "{ C[] }" },
5420 { &isl_multi_pw_aff_range_factor_domain,
5421 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5422 "{ A[x] -> B[] : x >= 0 }" },
5423 { &isl_multi_pw_aff_range_factor_range,
5424 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5425 "{ A[y] -> C[] : y >= 0 }" },
5426 { &isl_multi_pw_aff_factor_range,
5427 "[N] -> { [B[] -> C[]] : N >= 0 }",
5428 "[N] -> { C[] : N >= 0 }" },
5431 /* Perform some basic tests of unary operations on isl_multi_pw_aff objects.
5433 static int test_un_mpa(isl_ctx *ctx)
5435 int i;
5436 isl_bool ok;
5437 isl_multi_pw_aff *mpa, *res;
5439 for (i = 0; i < ARRAY_SIZE(mpa_un_tests); ++i) {
5440 mpa = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].arg);
5441 res = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].res);
5442 mpa = mpa_un_tests[i].fn(mpa);
5443 ok = isl_multi_pw_aff_plain_is_equal(mpa, res);
5444 isl_multi_pw_aff_free(mpa);
5445 isl_multi_pw_aff_free(res);
5446 if (ok < 0)
5447 return -1;
5448 if (!ok)
5449 isl_die(ctx, isl_error_unknown,
5450 "unexpected result", return -1);
5453 return 0;
5456 /* Inputs for basic tests of binary operations on isl_multi_pw_aff objects.
5457 * "fn" is the function that is tested.
5458 * "arg1" and "arg2" are string descriptions of the inputs.
5459 * "res" is a string description of the expected result.
5461 struct {
5462 __isl_give isl_multi_pw_aff *(*fn)(
5463 __isl_take isl_multi_pw_aff *mpa1,
5464 __isl_take isl_multi_pw_aff *mpa2);
5465 const char *arg1;
5466 const char *arg2;
5467 const char *res;
5468 } mpa_bin_tests[] = {
5469 { &isl_multi_pw_aff_add, "{ A[] -> [1] }", "{ A[] -> [2] }",
5470 "{ A[] -> [3] }" },
5471 { &isl_multi_pw_aff_add, "{ A[x] -> [(1 : x >= 5)] }",
5472 "{ A[x] -> [(x : x <= 10)] }",
5473 "{ A[x] -> [(1 + x : 5 <= x <= 10)] }" },
5474 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5475 "{ A[x] -> [] : x <= 10 }",
5476 "{ A[x] -> [] : 5 <= x <= 10 }" },
5477 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5478 "[N] -> { A[x] -> [] : x <= N }",
5479 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5480 { &isl_multi_pw_aff_add,
5481 "[N] -> { A[x] -> [] : x <= N }",
5482 "{ A[x] -> [] : x >= 5 }",
5483 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5484 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5485 "{ A[y] -> C[(2 : y <= 10)] }",
5486 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5487 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5488 "{ A[y] -> C[2] : y <= 10 }",
5489 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5490 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5491 "[N] -> { A[y] -> C[2] : y <= N }",
5492 "[N] -> { A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= N)]] }" },
5493 { &isl_multi_pw_aff_range_product, "[N] -> { A[x] -> B[1] : x >= N }",
5494 "{ A[y] -> C[2] : y <= 10 }",
5495 "[N] -> { A[x] -> [B[(1 : x >= N)] -> C[(2 : x <= 10)]] }" },
5496 { &isl_multi_pw_aff_range_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5497 "{ A[] -> [B[1] -> C[2]] }" },
5498 { &isl_multi_pw_aff_range_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5499 "{ A[] -> [B[] -> C[]] }" },
5500 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5501 "{ A[y] -> C[] : y <= 10 }",
5502 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] : x <= 10 }" },
5503 { &isl_multi_pw_aff_range_product, "{ A[y] -> C[] : y <= 10 }",
5504 "{ A[x] -> B[(1 : x >= 5)] }",
5505 "{ A[x] -> [C[] -> B[(1 : x >= 5)]] : x <= 10 }" },
5506 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5507 "{ A[y] -> C[(2 : y <= 10)] }",
5508 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[(2 : y <= 10)]] }" },
5509 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5510 "{ A[y] -> C[] : y <= 10 }",
5511 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= 10 }" },
5512 { &isl_multi_pw_aff_product, "{ A[y] -> C[] : y <= 10 }",
5513 "{ A[x] -> B[(1 : x >= 5)] }",
5514 "{ [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= 10 }" },
5515 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5516 "[N] -> { A[y] -> C[] : y <= N }",
5517 "[N] -> { [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= N }" },
5518 { &isl_multi_pw_aff_product, "[N] -> { A[y] -> C[] : y <= N }",
5519 "{ A[x] -> B[(1 : x >= 5)] }",
5520 "[N] -> { [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= N }" },
5521 { &isl_multi_pw_aff_product, "{ A[x] -> B[] : x >= 5 }",
5522 "{ A[y] -> C[] : y <= 10 }",
5523 "{ [A[x] -> A[y]] -> [B[] -> C[]] : x >= 5 and y <= 10 }" },
5524 { &isl_multi_pw_aff_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5525 "{ [A[] -> A[]] -> [B[1] -> C[2]] }" },
5526 { &isl_multi_pw_aff_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5527 "{ [A[] -> A[]] -> [B[] -> C[]] }" },
5528 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5529 "{ B[i,j] -> C[i + 2j] }", "{ A[a,b] -> B[b,a] }",
5530 "{ A[a,b] -> C[b + 2a] }" },
5531 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5532 "{ B[i,j] -> C[i + 2j] }",
5533 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5534 "{ A[a,b] -> C[(b + 2a : b > a)] }" },
5535 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5536 "{ B[i,j] -> C[(i + 2j : j > 4)] }",
5537 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5538 "{ A[a,b] -> C[(b + 2a : b > a > 4)] }" },
5539 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5540 "{ B[i,j] -> C[] }",
5541 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5542 "{ A[a,b] -> C[] }" },
5543 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5544 "{ B[i,j] -> C[] : i > j }",
5545 "{ A[a,b] -> B[b,a] }",
5546 "{ A[a,b] -> C[] : b > a }" },
5547 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5548 "{ B[i,j] -> C[] : j > 5 }",
5549 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5550 "{ A[a,b] -> C[] : b > a > 5 }" },
5551 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5552 "[N] -> { B[i,j] -> C[] : j > N }",
5553 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5554 "[N] -> { A[a,b] -> C[] : b > a > N }" },
5555 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5556 "[M,N] -> { B[] -> C[] : N > 5 }",
5557 "[M,N] -> { A[] -> B[] : M > N }",
5558 "[M,N] -> { A[] -> C[] : M > N > 5 }" },
5561 /* Perform some basic tests of binary operations on isl_multi_pw_aff objects.
5563 static int test_bin_mpa(isl_ctx *ctx)
5565 int i;
5566 isl_bool ok;
5567 isl_multi_pw_aff *mpa1, *mpa2, *res;
5569 for (i = 0; i < ARRAY_SIZE(mpa_bin_tests); ++i) {
5570 mpa1 = isl_multi_pw_aff_read_from_str(ctx,
5571 mpa_bin_tests[i].arg1);
5572 mpa2 = isl_multi_pw_aff_read_from_str(ctx,
5573 mpa_bin_tests[i].arg2);
5574 res = isl_multi_pw_aff_read_from_str(ctx,
5575 mpa_bin_tests[i].res);
5576 mpa1 = mpa_bin_tests[i].fn(mpa1, mpa2);
5577 ok = isl_multi_pw_aff_plain_is_equal(mpa1, res);
5578 isl_multi_pw_aff_free(mpa1);
5579 isl_multi_pw_aff_free(res);
5580 if (ok < 0)
5581 return -1;
5582 if (!ok)
5583 isl_die(ctx, isl_error_unknown,
5584 "unexpected result", return -1);
5587 return 0;
5590 /* Inputs for basic tests of unary operations on
5591 * isl_multi_union_pw_aff objects.
5592 * "fn" is the function that is tested.
5593 * "arg" is a string description of the input.
5594 * "res" is a string description of the expected result.
5596 struct {
5597 __isl_give isl_multi_union_pw_aff *(*fn)(
5598 __isl_take isl_multi_union_pw_aff *mupa);
5599 const char *arg;
5600 const char *res;
5601 } mupa_un_tests[] = {
5602 { &isl_multi_union_pw_aff_factor_range,
5603 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]",
5604 "C[{ A[] -> [2] }]" },
5605 { &isl_multi_union_pw_aff_factor_range,
5606 "[B[] -> C[{ A[] -> [2] }]]",
5607 "C[{ A[] -> [2] }]" },
5608 { &isl_multi_union_pw_aff_factor_range,
5609 "[B[{ A[] -> [1] }] -> C[]]",
5610 "C[]" },
5611 { &isl_multi_union_pw_aff_factor_range,
5612 "[B[] -> C[]]",
5613 "C[]" },
5614 { &isl_multi_union_pw_aff_factor_range,
5615 "([B[] -> C[]] : { A[x] : x >= 0 })",
5616 "(C[] : { A[x] : x >= 0 })" },
5617 { &isl_multi_union_pw_aff_factor_range,
5618 "[N] -> ([B[] -> C[]] : { A[x] : x <= N })",
5619 "[N] -> (C[] : { A[x] : x <= N })" },
5620 { &isl_multi_union_pw_aff_factor_range,
5621 "[N] -> ([B[] -> C[]] : { : N >= 0 })",
5622 "[N] -> (C[] : { : N >= 0 })" },
5625 /* Perform some basic tests of unary operations on
5626 * isl_multi_union_pw_aff objects.
5628 static int test_un_mupa(isl_ctx *ctx)
5630 int i;
5631 isl_bool ok;
5632 isl_multi_union_pw_aff *mupa, *res;
5634 for (i = 0; i < ARRAY_SIZE(mupa_un_tests); ++i) {
5635 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5636 mupa_un_tests[i].arg);
5637 res = isl_multi_union_pw_aff_read_from_str(ctx,
5638 mupa_un_tests[i].res);
5639 mupa = mupa_un_tests[i].fn(mupa);
5640 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5641 isl_multi_union_pw_aff_free(mupa);
5642 isl_multi_union_pw_aff_free(res);
5643 if (ok < 0)
5644 return -1;
5645 if (!ok)
5646 isl_die(ctx, isl_error_unknown,
5647 "unexpected result", return -1);
5650 return 0;
5653 /* Inputs for basic tests of binary operations on
5654 * isl_multi_union_pw_aff objects.
5655 * "fn" is the function that is tested.
5656 * "arg1" and "arg2" are string descriptions of the inputs.
5657 * "res" is a string description of the expected result.
5659 struct {
5660 __isl_give isl_multi_union_pw_aff *(*fn)(
5661 __isl_take isl_multi_union_pw_aff *mupa1,
5662 __isl_take isl_multi_union_pw_aff *mupa2);
5663 const char *arg1;
5664 const char *arg2;
5665 const char *res;
5666 } mupa_bin_tests[] = {
5667 { &isl_multi_union_pw_aff_add, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5668 "[{ A[] -> [3] }]" },
5669 { &isl_multi_union_pw_aff_sub, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5670 "[{ A[] -> [-1] }]" },
5671 { &isl_multi_union_pw_aff_add,
5672 "[{ A[] -> [1]; B[] -> [4] }]",
5673 "[{ A[] -> [2]; C[] -> [5] }]",
5674 "[{ A[] -> [3] }]" },
5675 { &isl_multi_union_pw_aff_union_add,
5676 "[{ A[] -> [1]; B[] -> [4] }]",
5677 "[{ A[] -> [2]; C[] -> [5] }]",
5678 "[{ A[] -> [3]; B[] -> [4]; C[] -> [5] }]" },
5679 { &isl_multi_union_pw_aff_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5680 "[{ A[x] -> [(x)] : x <= 10 }]",
5681 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10 }]" },
5682 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5683 "([] : { A[x] : x <= 10 })",
5684 "([] : { A[x] : 5 <= x <= 10 })" },
5685 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5686 "[N] -> ([] : { A[x] : x <= N })",
5687 "[N] -> ([] : { A[x] : 5 <= x <= N })" },
5688 { &isl_multi_union_pw_aff_add, "[N] -> ([] : { A[x] : x >= N })",
5689 "([] : { A[x] : x <= 10 })",
5690 "[N] -> ([] : { A[x] : N <= x <= 10 })" },
5691 { &isl_multi_union_pw_aff_union_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5692 "[{ A[x] -> [(x)] : x <= 10 }]",
5693 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10; "
5694 "A[x] -> [(1)] : x > 10; A[x] -> [(x)] : x < 5 }]" },
5695 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 5 })",
5696 "([] : { A[x] : x <= 10 })",
5697 "([] : { A[x] })" },
5698 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 0 })",
5699 "[N] -> ([] : { A[x] : x >= N })",
5700 "[N] -> ([] : { A[x] : x >= 0 or x >= N })" },
5701 { &isl_multi_union_pw_aff_union_add,
5702 "[N] -> ([] : { A[] : N >= 0})",
5703 "[N] -> ([] : { A[] : N <= 0})",
5704 "[N] -> ([] : { A[] })" },
5705 { &isl_multi_union_pw_aff_union_add,
5706 "[N] -> ([] : { A[] })",
5707 "[N] -> ([] : { : })",
5708 "[N] -> ([] : { : })" },
5709 { &isl_multi_union_pw_aff_union_add,
5710 "[N] -> ([] : { : })",
5711 "[N] -> ([] : { A[] })",
5712 "[N] -> ([] : { : })" },
5713 { &isl_multi_union_pw_aff_union_add,
5714 "[N] -> ([] : { : N >= 0})",
5715 "[N] -> ([] : { : N <= 0})",
5716 "[N] -> ([] : { : })" },
5717 { &isl_multi_union_pw_aff_range_product,
5718 "B[{ A[] -> [1] }]",
5719 "C[{ A[] -> [2] }]",
5720 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]" },
5721 { &isl_multi_union_pw_aff_range_product,
5722 "(B[] : { A[x] : x >= 5 })",
5723 "(C[] : { A[x] : x <= 10 })",
5724 "([B[] -> C[]] : { A[x] : 5 <= x <= 10 })" },
5725 { &isl_multi_union_pw_aff_range_product,
5726 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5727 "(C[] : { A[x] : x <= 10 })",
5728 "[B[{ A[x] -> [x + 1] : 5 <= x <= 10 }] -> C[]]" },
5729 { &isl_multi_union_pw_aff_range_product,
5730 "(C[] : { A[x] : x <= 10 })",
5731 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5732 "[C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= 10 }]]" },
5733 { &isl_multi_union_pw_aff_range_product,
5734 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5735 "[N] -> (C[] : { A[x] : x <= N })",
5736 "[N] -> [B[{ A[x] -> [x + 1] : 5 <= x <= N }] -> C[]]" },
5737 { &isl_multi_union_pw_aff_range_product,
5738 "[N] -> (C[] : { A[x] : x <= N })",
5739 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5740 "[N] -> [C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= N }]]" },
5741 { &isl_multi_union_pw_aff_range_product,
5742 "B[{ A[] -> [1]; D[] -> [3] }]",
5743 "C[{ A[] -> [2] }]",
5744 "[B[{ A[] -> [1]; D[] -> [3] }] -> C[{ A[] -> [2] }]]" },
5745 { &isl_multi_union_pw_aff_range_product,
5746 "B[] }]",
5747 "(C[] : { A[x] })",
5748 "([B[] -> C[]] : { A[x] })" },
5749 { &isl_multi_union_pw_aff_range_product,
5750 "(B[] : { A[x] })",
5751 "C[] }]",
5752 "([B[] -> C[]] : { A[x] })" },
5755 /* Perform some basic tests of binary operations on
5756 * isl_multi_union_pw_aff objects.
5758 static int test_bin_mupa(isl_ctx *ctx)
5760 int i;
5761 isl_bool ok;
5762 isl_multi_union_pw_aff *mupa1, *mupa2, *res;
5764 for (i = 0; i < ARRAY_SIZE(mupa_bin_tests); ++i) {
5765 mupa1 = isl_multi_union_pw_aff_read_from_str(ctx,
5766 mupa_bin_tests[i].arg1);
5767 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx,
5768 mupa_bin_tests[i].arg2);
5769 res = isl_multi_union_pw_aff_read_from_str(ctx,
5770 mupa_bin_tests[i].res);
5771 mupa1 = mupa_bin_tests[i].fn(mupa1, mupa2);
5772 ok = isl_multi_union_pw_aff_plain_is_equal(mupa1, res);
5773 isl_multi_union_pw_aff_free(mupa1);
5774 isl_multi_union_pw_aff_free(res);
5775 if (ok < 0)
5776 return -1;
5777 if (!ok)
5778 isl_die(ctx, isl_error_unknown,
5779 "unexpected result", return -1);
5782 return 0;
5785 /* Inputs for basic tests of binary operations on
5786 * pairs of isl_multi_union_pw_aff and isl_set objects.
5787 * "fn" is the function that is tested.
5788 * "arg1" and "arg2" are string descriptions of the inputs.
5789 * "res" is a string description of the expected result.
5791 struct {
5792 __isl_give isl_multi_union_pw_aff *(*fn)(
5793 __isl_take isl_multi_union_pw_aff *mupa,
5794 __isl_take isl_set *set);
5795 const char *arg1;
5796 const char *arg2;
5797 const char *res;
5798 } mupa_set_tests[] = {
5799 { &isl_multi_union_pw_aff_intersect_range,
5800 "C[{ B[i,j] -> [i + 2j] }]", "{ C[1] }",
5801 "C[{ B[i,j] -> [i + 2j] : i + 2j = 1 }]" },
5802 { &isl_multi_union_pw_aff_intersect_range,
5803 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[N] }",
5804 "[N] -> C[{ B[i,j] -> [i + 2j] : i + 2j = N }]" },
5805 { &isl_multi_union_pw_aff_intersect_range,
5806 "[N] -> C[{ B[i,j] -> [i + 2j + N] }]", "{ C[1] }",
5807 "[N] -> C[{ B[i,j] -> [i + 2j + N] : i + 2j + N = 1 }]" },
5808 { &isl_multi_union_pw_aff_intersect_range,
5809 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[x] : N >= 0 }",
5810 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0 }]" },
5811 { &isl_multi_union_pw_aff_intersect_range,
5812 "C[]", "{ C[] }", "C[]" },
5813 { &isl_multi_union_pw_aff_intersect_range,
5814 "[N] -> (C[] : { : N >= 0 })",
5815 "{ C[] }",
5816 "[N] -> (C[] : { : N >= 0 })" },
5817 { &isl_multi_union_pw_aff_intersect_range,
5818 "(C[] : { A[a,b] })",
5819 "{ C[] }",
5820 "(C[] : { A[a,b] })" },
5821 { &isl_multi_union_pw_aff_intersect_range,
5822 "[N] -> (C[] : { A[a,b] : a,b <= N })",
5823 "{ C[] }",
5824 "[N] -> (C[] : { A[a,b] : a,b <= N })" },
5825 { &isl_multi_union_pw_aff_intersect_range,
5826 "C[]",
5827 "[N] -> { C[] : N >= 0 }",
5828 "[N] -> (C[] : { : N >= 0 })" },
5829 { &isl_multi_union_pw_aff_intersect_range,
5830 "(C[] : { A[a,b] })",
5831 "[N] -> { C[] : N >= 0 }",
5832 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5833 { &isl_multi_union_pw_aff_intersect_range,
5834 "[N] -> (C[] : { : N >= 0 })",
5835 "[N] -> { C[] : N < 1024 }",
5836 "[N] -> (C[] : { : 0 <= N < 1024 })" },
5837 { &isl_multi_union_pw_aff_intersect_params,
5838 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { : N >= 0 }",
5839 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0}]" },
5840 { &isl_multi_union_pw_aff_intersect_params,
5841 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "[N] -> { : N >= 0 }",
5842 "[N] -> C[{ B[i,j] -> [i + 2j] : 0 <= N <= 256 }]" },
5843 { &isl_multi_union_pw_aff_intersect_params,
5844 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "{ : }",
5845 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]" },
5846 { &isl_multi_union_pw_aff_intersect_params,
5847 "C[]", "[N] -> { : N >= 0 }",
5848 "[N] -> (C[] : { : N >= 0 })" },
5849 { &isl_multi_union_pw_aff_intersect_params,
5850 "(C[] : { A[a,b] })", "[N] -> { : N >= 0 }",
5851 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5852 { &isl_multi_union_pw_aff_intersect_params,
5853 "[N] -> (C[] : { A[a,N] })", "{ : }",
5854 "[N] -> (C[] : { A[a,N] })" },
5855 { &isl_multi_union_pw_aff_intersect_params,
5856 "[N] -> (C[] : { A[a,b] : N <= 256 })", "[N] -> { : N >= 0 }",
5857 "[N] -> (C[] : { A[a,b] : 0 <= N <= 256 })" },
5860 /* Perform some basic tests of binary operations on
5861 * pairs of isl_multi_union_pw_aff and isl_set objects.
5863 static int test_mupa_set(isl_ctx *ctx)
5865 int i;
5866 isl_bool ok;
5867 isl_multi_union_pw_aff *mupa, *res;
5868 isl_set *set;
5870 for (i = 0; i < ARRAY_SIZE(mupa_set_tests); ++i) {
5871 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5872 mupa_set_tests[i].arg1);
5873 set = isl_set_read_from_str(ctx, mupa_set_tests[i].arg2);
5874 res = isl_multi_union_pw_aff_read_from_str(ctx,
5875 mupa_set_tests[i].res);
5876 mupa = mupa_set_tests[i].fn(mupa, set);
5877 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5878 isl_multi_union_pw_aff_free(mupa);
5879 isl_multi_union_pw_aff_free(res);
5880 if (ok < 0)
5881 return -1;
5882 if (!ok)
5883 isl_die(ctx, isl_error_unknown,
5884 "unexpected result", return -1);
5887 return 0;
5890 /* Inputs for basic tests of binary operations on
5891 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5892 * "fn" is the function that is tested.
5893 * "arg1" and "arg2" are string descriptions of the inputs.
5894 * "res" is a string description of the expected result.
5896 struct {
5897 __isl_give isl_multi_union_pw_aff *(*fn)(
5898 __isl_take isl_multi_union_pw_aff *mupa,
5899 __isl_take isl_union_set *uset);
5900 const char *arg1;
5901 const char *arg2;
5902 const char *res;
5903 } mupa_uset_tests[] = {
5904 { &isl_multi_union_pw_aff_intersect_domain,
5905 "C[{ B[i,j] -> [i + 2j] }]", "{ B[i,i] }",
5906 "C[{ B[i,i] -> [3i] }]" },
5907 { &isl_multi_union_pw_aff_intersect_domain,
5908 "(C[] : { B[i,j] })", "{ B[i,i] }",
5909 "(C[] : { B[i,i] })" },
5910 { &isl_multi_union_pw_aff_intersect_domain,
5911 "(C[] : { B[i,j] })", "[N] -> { B[N,N] }",
5912 "[N] -> (C[] : { B[N,N] })" },
5913 { &isl_multi_union_pw_aff_intersect_domain,
5914 "C[]", "{ B[i,i] }",
5915 "(C[] : { B[i,i] })" },
5916 { &isl_multi_union_pw_aff_intersect_domain,
5917 "[N] -> (C[] : { : N >= 0 })", "{ B[i,i] }",
5918 "[N] -> (C[] : { B[i,i] : N >= 0 })" },
5921 /* Perform some basic tests of binary operations on
5922 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5924 static int test_mupa_uset(isl_ctx *ctx)
5926 int i;
5927 isl_bool ok;
5928 isl_multi_union_pw_aff *mupa, *res;
5929 isl_union_set *uset;
5931 for (i = 0; i < ARRAY_SIZE(mupa_uset_tests); ++i) {
5932 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5933 mupa_uset_tests[i].arg1);
5934 uset = isl_union_set_read_from_str(ctx,
5935 mupa_uset_tests[i].arg2);
5936 res = isl_multi_union_pw_aff_read_from_str(ctx,
5937 mupa_uset_tests[i].res);
5938 mupa = mupa_uset_tests[i].fn(mupa, uset);
5939 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5940 isl_multi_union_pw_aff_free(mupa);
5941 isl_multi_union_pw_aff_free(res);
5942 if (ok < 0)
5943 return -1;
5944 if (!ok)
5945 isl_die(ctx, isl_error_unknown,
5946 "unexpected result", return -1);
5949 return 0;
5952 /* Inputs for basic tests of binary operations on
5953 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5954 * "fn" is the function that is tested.
5955 * "arg1" and "arg2" are string descriptions of the inputs.
5956 * "res" is a string description of the expected result.
5958 struct {
5959 __isl_give isl_multi_union_pw_aff *(*fn)(
5960 __isl_take isl_multi_union_pw_aff *mupa,
5961 __isl_take isl_multi_aff *ma);
5962 const char *arg1;
5963 const char *arg2;
5964 const char *res;
5965 } mupa_ma_tests[] = {
5966 { &isl_multi_union_pw_aff_apply_multi_aff,
5967 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5968 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5969 "{ C[a,b] -> D[b,a] }",
5970 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
5971 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
5972 { &isl_multi_union_pw_aff_apply_multi_aff,
5973 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
5974 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5975 "{ C[a,b] -> D[b,a] }",
5976 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
5977 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
5978 { &isl_multi_union_pw_aff_apply_multi_aff,
5979 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5980 "[N] -> { C[a] -> D[a + N] }",
5981 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }] " },
5982 { &isl_multi_union_pw_aff_apply_multi_aff,
5983 "C[]",
5984 "{ C[] -> D[] }",
5985 "D[]" },
5986 { &isl_multi_union_pw_aff_apply_multi_aff,
5987 "[N] -> (C[] : { : N >= 0 })",
5988 "{ C[] -> D[] }",
5989 "[N] -> (D[] : { : N >= 0 })" },
5990 { &isl_multi_union_pw_aff_apply_multi_aff,
5991 "C[]",
5992 "[N] -> { C[] -> D[N] }",
5993 "[N] -> D[{ [N] }]" },
5994 { &isl_multi_union_pw_aff_apply_multi_aff,
5995 "(C[] : { A[i,j] : i >= j })",
5996 "{ C[] -> D[] }",
5997 "(D[] : { A[i,j] : i >= j })" },
5998 { &isl_multi_union_pw_aff_apply_multi_aff,
5999 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6000 "{ C[] -> D[] }",
6001 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6002 { &isl_multi_union_pw_aff_apply_multi_aff,
6003 "(C[] : { A[i,j] : i >= j })",
6004 "[N] -> { C[] -> D[N] }",
6005 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6008 /* Perform some basic tests of binary operations on
6009 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
6011 static int test_mupa_ma(isl_ctx *ctx)
6013 int i;
6014 isl_bool ok;
6015 isl_multi_union_pw_aff *mupa, *res;
6016 isl_multi_aff *ma;
6018 for (i = 0; i < ARRAY_SIZE(mupa_ma_tests); ++i) {
6019 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6020 mupa_ma_tests[i].arg1);
6021 ma = isl_multi_aff_read_from_str(ctx, mupa_ma_tests[i].arg2);
6022 res = isl_multi_union_pw_aff_read_from_str(ctx,
6023 mupa_ma_tests[i].res);
6024 mupa = mupa_ma_tests[i].fn(mupa, ma);
6025 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6026 isl_multi_union_pw_aff_free(mupa);
6027 isl_multi_union_pw_aff_free(res);
6028 if (ok < 0)
6029 return -1;
6030 if (!ok)
6031 isl_die(ctx, isl_error_unknown,
6032 "unexpected result", return -1);
6035 return 0;
6038 /* Inputs for basic tests of binary operations on
6039 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
6040 * "fn" is the function that is tested.
6041 * "arg1" and "arg2" are string descriptions of the inputs.
6042 * "res" is a string description of the expected result.
6044 struct {
6045 __isl_give isl_union_pw_aff *(*fn)(
6046 __isl_take isl_multi_union_pw_aff *mupa,
6047 __isl_take isl_pw_aff *pa);
6048 const char *arg1;
6049 const char *arg2;
6050 const char *res;
6051 } mupa_pa_tests[] = {
6052 { &isl_multi_union_pw_aff_apply_pw_aff,
6053 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6054 "[N] -> { C[a] -> [a + N] }",
6055 "[N] -> { A[i,j] -> [i + N]; B[i,j] -> [j + N] }" },
6056 { &isl_multi_union_pw_aff_apply_pw_aff,
6057 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6058 "{ C[a] -> [a] : a >= 0; C[a] -> [-a] : a < 0 }",
6059 "{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
6060 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }" },
6061 { &isl_multi_union_pw_aff_apply_pw_aff,
6062 "C[]",
6063 "[N] -> { C[] -> [N] }",
6064 "[N] -> { [N] }" },
6065 { &isl_multi_union_pw_aff_apply_pw_aff,
6066 "C[]",
6067 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
6068 "[N] -> { [N] : N >= 0; [-N] : N < 0 }" },
6069 { &isl_multi_union_pw_aff_apply_pw_aff,
6070 "[N] -> (C[] : { : N >= 0 })",
6071 "[N] -> { C[] -> [N] }",
6072 "[N] -> { [N] : N >= 0 }" },
6073 { &isl_multi_union_pw_aff_apply_pw_aff,
6074 "[N] -> (C[] : { : N >= 0 })",
6075 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
6076 "[N] -> { [N] : N >= 0 }" },
6077 { &isl_multi_union_pw_aff_apply_pw_aff,
6078 "[N] -> (C[] : { : N >= 0 })",
6079 "{ C[] -> [0] }",
6080 "[N] -> { [0] : N >= 0 }" },
6081 { &isl_multi_union_pw_aff_apply_pw_aff,
6082 "(C[] : { A[i,j] : i >= j })",
6083 "[N] -> { C[] -> [N] }",
6084 "[N] -> { A[i,j] -> [N] : i >= j }" },
6085 { &isl_multi_union_pw_aff_apply_pw_aff,
6086 "(C[] : { A[i,j] : i >= j })",
6087 "[N] -> { C[] -> [N] : N >= 0 }",
6088 "[N] -> { A[i,j] -> [N] : i >= j and N >= 0 }" },
6091 /* Perform some basic tests of binary operations on
6092 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
6094 static int test_mupa_pa(isl_ctx *ctx)
6096 int i;
6097 isl_bool ok;
6098 isl_multi_union_pw_aff *mupa;
6099 isl_union_pw_aff *upa, *res;
6100 isl_pw_aff *pa;
6102 for (i = 0; i < ARRAY_SIZE(mupa_pa_tests); ++i) {
6103 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6104 mupa_pa_tests[i].arg1);
6105 pa = isl_pw_aff_read_from_str(ctx, mupa_pa_tests[i].arg2);
6106 res = isl_union_pw_aff_read_from_str(ctx,
6107 mupa_pa_tests[i].res);
6108 upa = mupa_pa_tests[i].fn(mupa, pa);
6109 ok = isl_union_pw_aff_plain_is_equal(upa, res);
6110 isl_union_pw_aff_free(upa);
6111 isl_union_pw_aff_free(res);
6112 if (ok < 0)
6113 return -1;
6114 if (!ok)
6115 isl_die(ctx, isl_error_unknown,
6116 "unexpected result", return -1);
6119 return 0;
6122 /* Inputs for basic tests of binary operations on
6123 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6124 * "fn" is the function that is tested.
6125 * "arg1" and "arg2" are string descriptions of the inputs.
6126 * "res" is a string description of the expected result.
6128 struct {
6129 __isl_give isl_multi_union_pw_aff *(*fn)(
6130 __isl_take isl_multi_union_pw_aff *mupa,
6131 __isl_take isl_pw_multi_aff *pma);
6132 const char *arg1;
6133 const char *arg2;
6134 const char *res;
6135 } mupa_pma_tests[] = {
6136 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6137 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6138 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6139 "{ C[a,b] -> D[b,a] }",
6140 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
6141 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
6142 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6143 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
6144 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6145 "{ C[a,b] -> D[b,a] }",
6146 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
6147 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
6148 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6149 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6150 "[N] -> { C[a] -> D[a + N] }",
6151 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }]" },
6152 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6153 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6154 "{ C[a] -> D[a] : a >= 0; C[a] -> D[-a] : a < 0 }",
6155 "D[{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
6156 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }]" },
6157 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6158 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6159 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6160 "{ C[a,b] -> D[a,b] : a >= b; C[a,b] -> D[b,a] : a < b }",
6161 "D[{ A[i,j] -> [i] : i >= j; A[i,j] -> [j] : i < j; "
6162 "B[i,j] -> [j] : i <= j; B[i,j] -> [i] : i > j }, "
6163 "{ A[i,j] -> [j] : i >= j; A[i,j] -> [i] : i < j; "
6164 "B[i,j] -> [i] : i <= j; B[i,j] -> [j] : i > j }]" },
6165 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6166 "C[]",
6167 "{ C[] -> D[] }",
6168 "D[]" },
6169 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6170 "[N] -> (C[] : { : N >= 0 })",
6171 "{ C[] -> D[] }",
6172 "[N] -> (D[] : { : N >= 0 })" },
6173 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6174 "C[]",
6175 "[N] -> { C[] -> D[N] }",
6176 "[N] -> D[{ [N] }]" },
6177 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6178 "(C[] : { A[i,j] : i >= j })",
6179 "{ C[] -> D[] }",
6180 "(D[] : { A[i,j] : i >= j })" },
6181 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6182 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6183 "{ C[] -> D[] }",
6184 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6185 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6186 "(C[] : { A[i,j] : i >= j })",
6187 "[N] -> { C[] -> D[N] }",
6188 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6189 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6190 "C[]",
6191 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6192 "[N] -> D[{ [N] : N >= 0; [-N] : N < 0 }]" },
6193 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6194 "[N] -> (C[] : { : N >= 0 })",
6195 "[N] -> { C[] -> D[N] }",
6196 "[N] -> D[{ [N] : N >= 0 }]" },
6197 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6198 "[N] -> (C[] : { : N >= 0 })",
6199 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6200 "[N] -> D[{ [N] : N >= 0 }]" },
6201 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6202 "[N] -> (C[] : { : N >= 0 })",
6203 "{ C[] -> D[0] }",
6204 "[N] -> D[{ [0] : N >= 0 }]" },
6205 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6206 "(C[] : { A[i,j] : i >= j })",
6207 "[N] -> { C[] -> D[N] : N >= 0 }",
6208 "[N] -> D[{ A[i,j] -> [N] : i >= j and N >= 0 }]" },
6211 /* Perform some basic tests of binary operations on
6212 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6214 static int test_mupa_pma(isl_ctx *ctx)
6216 int i;
6217 isl_bool ok;
6218 isl_multi_union_pw_aff *mupa, *res;
6219 isl_pw_multi_aff *pma;
6221 for (i = 0; i < ARRAY_SIZE(mupa_pma_tests); ++i) {
6222 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6223 mupa_pma_tests[i].arg1);
6224 pma = isl_pw_multi_aff_read_from_str(ctx,
6225 mupa_pma_tests[i].arg2);
6226 res = isl_multi_union_pw_aff_read_from_str(ctx,
6227 mupa_pma_tests[i].res);
6228 mupa = mupa_pma_tests[i].fn(mupa, pma);
6229 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6230 isl_multi_union_pw_aff_free(mupa);
6231 isl_multi_union_pw_aff_free(res);
6232 if (ok < 0)
6233 return -1;
6234 if (!ok)
6235 isl_die(ctx, isl_error_unknown,
6236 "unexpected result", return -1);
6239 return 0;
6242 /* Inputs for basic tests of binary operations on
6243 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6244 * "fn" is the function that is tested.
6245 * "arg1" and "arg2" are string descriptions of the inputs.
6246 * "res" is a string description of the expected result.
6248 struct {
6249 __isl_give isl_multi_union_pw_aff *(*fn)(
6250 __isl_take isl_multi_union_pw_aff *mupa,
6251 __isl_take isl_union_pw_multi_aff *upma);
6252 const char *arg1;
6253 const char *arg2;
6254 const char *res;
6255 } mupa_upma_tests[] = {
6256 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6257 "C[{ B[i,j] -> [i + 2j] }]", "{ A[a,b] -> B[b,a] }",
6258 "C[{ A[a,b] -> [b + 2a] }]" },
6259 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6260 "C[{ B[i,j] -> [i + 2j] }]",
6261 "{ A[a,b] -> B[b,a] : b > a }",
6262 "C[{ A[a,b] -> [b + 2a] : b > a }]" },
6263 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6264 "C[{ B[i,j] -> [i + 2j] : j > 4 }]",
6265 "{ A[a,b] -> B[b,a] : b > a }",
6266 "C[{ A[a,b] -> [b + 2a] : b > a > 4 }]" },
6267 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6268 "C[{ B[i,j] -> [i + 2j] }]",
6269 "{ A[a,b] -> B[b,a] : a > b; A[a,b] -> B[a,b] : a <= b }",
6270 "C[{ A[a,b] -> [b + 2a] : a > b; A[a,b] -> [a + 2b] : a <= b }]" },
6271 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6272 "(C[] : { B[a,b] })",
6273 "{ A[a,b] -> B[b,a] }",
6274 "(C[] : { A[a,b] })" },
6275 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6276 "(C[] : { B[a,b] })",
6277 "{ B[a,b] -> A[b,a] }",
6278 "(C[] : { })" },
6279 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6280 "(C[] : { B[a,b] })",
6281 "{ A[a,b] -> B[b,a] : a > b }",
6282 "(C[] : { A[a,b] : a > b })" },
6283 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6284 "(C[] : { B[a,b] : a > b })",
6285 "{ A[a,b] -> B[b,a] }",
6286 "(C[] : { A[a,b] : b > a })" },
6287 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6288 "[N] -> (C[] : { B[a,b] : a > N })",
6289 "{ A[a,b] -> B[b,a] : a > b }",
6290 "[N] -> (C[] : { A[a,b] : a > b > N })" },
6291 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6292 "(C[] : { B[a,b] : a > b })",
6293 "[N] -> { A[a,b] -> B[b,a] : a > N }",
6294 "[N] -> (C[] : { A[a,b] : b > a > N })" },
6295 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6296 "C[]",
6297 "{ A[a,b] -> B[b,a] }",
6298 "C[]" },
6299 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6300 "[N] -> (C[] : { : N >= 0 })",
6301 "{ A[a,b] -> B[b,a] }",
6302 "[N] -> (C[] : { : N >= 0 })" },
6303 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6304 "C[]",
6305 "[N] -> { A[a,b] -> B[b,a] : N >= 0 }",
6306 "[N] -> (C[] : { : N >= 0 })" },
6309 /* Perform some basic tests of binary operations on
6310 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6312 static int test_mupa_upma(isl_ctx *ctx)
6314 int i;
6315 isl_bool ok;
6316 isl_multi_union_pw_aff *mupa, *res;
6317 isl_union_pw_multi_aff *upma;
6319 for (i = 0; i < ARRAY_SIZE(mupa_upma_tests); ++i) {
6320 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6321 mupa_upma_tests[i].arg1);
6322 upma = isl_union_pw_multi_aff_read_from_str(ctx,
6323 mupa_upma_tests[i].arg2);
6324 res = isl_multi_union_pw_aff_read_from_str(ctx,
6325 mupa_upma_tests[i].res);
6326 mupa = mupa_upma_tests[i].fn(mupa, upma);
6327 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6328 isl_multi_union_pw_aff_free(mupa);
6329 isl_multi_union_pw_aff_free(res);
6330 if (ok < 0)
6331 return -1;
6332 if (!ok)
6333 isl_die(ctx, isl_error_unknown,
6334 "unexpected result", return -1);
6337 return 0;
6340 /* Check that the input tuple of an isl_aff can be set properly.
6342 static isl_stat test_aff_set_tuple_id(isl_ctx *ctx)
6344 isl_id *id;
6345 isl_aff *aff;
6346 int equal;
6348 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x + 1] }");
6349 id = isl_id_alloc(ctx, "A", NULL);
6350 aff = isl_aff_set_tuple_id(aff, isl_dim_in, id);
6351 equal = aff_check_plain_equal(aff, "{ A[x] -> [x + 1] }");
6352 isl_aff_free(aff);
6353 if (equal < 0)
6354 return isl_stat_error;
6356 return isl_stat_ok;
6359 int test_aff(isl_ctx *ctx)
6361 const char *str;
6362 isl_set *set;
6363 isl_space *space;
6364 isl_local_space *ls;
6365 isl_aff *aff;
6366 int zero, equal;
6368 if (test_upa(ctx) < 0)
6369 return -1;
6370 if (test_bin_aff(ctx) < 0)
6371 return -1;
6372 if (test_bin_pw_aff(ctx) < 0)
6373 return -1;
6374 if (test_bin_upma(ctx) < 0)
6375 return -1;
6376 if (test_bin_upma_fail(ctx) < 0)
6377 return -1;
6378 if (test_un_mpa(ctx) < 0)
6379 return -1;
6380 if (test_bin_mpa(ctx) < 0)
6381 return -1;
6382 if (test_un_mupa(ctx) < 0)
6383 return -1;
6384 if (test_bin_mupa(ctx) < 0)
6385 return -1;
6386 if (test_mupa_set(ctx) < 0)
6387 return -1;
6388 if (test_mupa_uset(ctx) < 0)
6389 return -1;
6390 if (test_mupa_ma(ctx) < 0)
6391 return -1;
6392 if (test_mupa_pa(ctx) < 0)
6393 return -1;
6394 if (test_mupa_pma(ctx) < 0)
6395 return -1;
6396 if (test_mupa_upma(ctx) < 0)
6397 return -1;
6399 space = isl_space_set_alloc(ctx, 0, 1);
6400 ls = isl_local_space_from_space(space);
6401 aff = isl_aff_zero_on_domain(ls);
6403 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6404 aff = isl_aff_scale_down_ui(aff, 3);
6405 aff = isl_aff_floor(aff);
6406 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6407 aff = isl_aff_scale_down_ui(aff, 2);
6408 aff = isl_aff_floor(aff);
6409 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6411 str = "{ [10] }";
6412 set = isl_set_read_from_str(ctx, str);
6413 aff = isl_aff_gist(aff, set);
6415 aff = isl_aff_add_constant_si(aff, -16);
6416 zero = isl_aff_plain_is_zero(aff);
6417 isl_aff_free(aff);
6419 if (zero < 0)
6420 return -1;
6421 if (!zero)
6422 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6424 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
6425 aff = isl_aff_scale_down_ui(aff, 64);
6426 aff = isl_aff_floor(aff);
6427 equal = aff_check_plain_equal(aff, "{ [-1] }");
6428 isl_aff_free(aff);
6429 if (equal < 0)
6430 return -1;
6432 if (test_aff_set_tuple_id(ctx) < 0)
6433 return -1;
6435 return 0;
6438 /* Inputs for isl_set_bind tests.
6439 * "set" is the input set.
6440 * "tuple" is the binding tuple.
6441 * "res" is the expected result.
6443 static
6444 struct {
6445 const char *set;
6446 const char *tuple;
6447 const char *res;
6448 } bind_set_tests[] = {
6449 { "{ A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
6450 "{ A[M, N] }",
6451 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
6452 { "{ B[N, M] : M mod 2 = 0 and N mod 8 = 3 }",
6453 "{ B[N, M] }",
6454 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
6455 { "[M] -> { C[N] : M mod 2 = 0 and N mod 8 = 3 }",
6456 "{ C[N] }",
6457 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
6458 { "[M] -> { D[x, N] : x mod 2 = 0 and N mod 8 = 3 and M >= 0 }",
6459 "{ D[M, N] }",
6460 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 and M >= 0 }" },
6463 /* Perform basic isl_set_bind tests.
6465 static isl_stat test_bind_set(isl_ctx *ctx)
6467 int i;
6469 for (i = 0; i < ARRAY_SIZE(bind_set_tests); ++i) {
6470 const char *str;
6471 isl_set *set;
6472 isl_multi_id *tuple;
6473 isl_stat r;
6475 set = isl_set_read_from_str(ctx, bind_set_tests[i].set);
6476 str = bind_set_tests[i].tuple;
6477 tuple = isl_multi_id_read_from_str(ctx, str);
6478 set = isl_set_bind(set, tuple);
6479 r = set_check_equal(set, bind_set_tests[i].res);
6480 isl_set_free(set);
6481 if (r < 0)
6482 return isl_stat_error;
6485 return isl_stat_ok;
6488 /* Inputs for isl_map_bind_domain tests.
6489 * "map" is the input map.
6490 * "tuple" is the binding tuple.
6491 * "res" is the expected result.
6493 struct {
6494 const char *map;
6495 const char *tuple;
6496 const char *res;
6497 } bind_map_domain_tests[] = {
6498 { "{ A[M, N] -> [M + floor(N/2)] }",
6499 "{ A[M, N] }",
6500 "[M, N] -> { [M + floor(N/2)] }" },
6501 { "{ B[N, M] -> [M + floor(N/2)] }",
6502 "{ B[N, M] }",
6503 "[N, M] -> { [M + floor(N/2)] }" },
6504 { "[M] -> { C[N] -> [M + floor(N/2)] }",
6505 "{ C[N] }",
6506 "[M, N] -> { [M + floor(N/2)] }" },
6507 { "[M] -> { C[x, N] -> [x + floor(N/2)] }",
6508 "{ C[M, N] }",
6509 "[M, N] -> { [M + floor(N/2)] }" },
6510 { "[M] -> { C[x, N] -> [M + floor(N/2)] }",
6511 "{ C[M, N] }",
6512 "[M, N] -> { [M + floor(N/2)] }" },
6513 { "[A, M] -> { C[N, x] -> [x + floor(N/2)] }",
6514 "{ C[N, M] }",
6515 "[A, N, M] -> { [M + floor(N/2)] }" },
6518 /* Perform basic isl_map_bind_domain tests.
6520 static isl_stat test_bind_map_domain(isl_ctx *ctx)
6522 int i;
6524 for (i = 0; i < ARRAY_SIZE(bind_map_domain_tests); ++i) {
6525 const char *str;
6526 isl_map *map;
6527 isl_set *set;
6528 isl_multi_id *tuple;
6529 isl_stat r;
6531 str = bind_map_domain_tests[i].map;
6532 map = isl_map_read_from_str(ctx, str);
6533 str = bind_map_domain_tests[i].tuple;
6534 tuple = isl_multi_id_read_from_str(ctx, str);
6535 set = isl_map_bind_domain(map, tuple);
6536 str = bind_map_domain_tests[i].res;
6537 r = set_check_equal(set, str);
6538 isl_set_free(set);
6539 if (r < 0)
6540 return isl_stat_error;
6543 return isl_stat_ok;
6546 /* Inputs for isl_union_map_bind_range tests.
6547 * "map" is the input union map.
6548 * "tuple" is the binding tuple.
6549 * "res" is the expected result.
6551 struct {
6552 const char *map;
6553 const char *tuple;
6554 const char *res;
6555 } bind_umap_range_tests[] = {
6556 { "{ B[N, M] -> A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
6557 "{ A[M, N] }",
6558 "[M, N] -> { B[N, M] : M mod 2 = 0 and N mod 8 = 3 }" },
6559 { "{ B[N, M] -> A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
6560 "{ B[M, N] }",
6561 "{ }" },
6562 { "{ A[] -> B[]; C[] -> D[]; E[] -> B[] }",
6563 "{ B[] }",
6564 "{ A[]; E[] }" },
6567 /* Perform basic isl_union_map_bind_range tests.
6569 static isl_stat test_bind_umap_range(isl_ctx *ctx)
6571 int i;
6573 for (i = 0; i < ARRAY_SIZE(bind_umap_range_tests); ++i) {
6574 const char *str;
6575 isl_union_map *umap;
6576 isl_union_set *uset;
6577 isl_multi_id *tuple;
6578 isl_stat r;
6580 str = bind_umap_range_tests[i].map;
6581 umap = isl_union_map_read_from_str(ctx, str);
6582 str = bind_umap_range_tests[i].tuple;
6583 tuple = isl_multi_id_read_from_str(ctx, str);
6584 uset = isl_union_map_bind_range(umap, tuple);
6585 str = bind_umap_range_tests[i].res;
6586 r = uset_check_equal(uset, str);
6587 isl_union_set_free(uset);
6588 if (r < 0)
6589 return isl_stat_error;
6592 return isl_stat_ok;
6595 /* Inputs for isl_pw_multi_aff_bind_domain tests.
6596 * "pma" is the input expression.
6597 * "tuple" is the binding tuple.
6598 * "res" is the expected result.
6600 struct {
6601 const char *pma;
6602 const char *tuple;
6603 const char *res;
6604 } bind_pma_domain_tests[] = {
6605 { "{ A[M, N] -> [M + floor(N/2)] }",
6606 "{ A[M, N] }",
6607 "[M, N] -> { [M + floor(N/2)] }" },
6608 { "{ B[N, M] -> [M + floor(N/2)] }",
6609 "{ B[N, M] }",
6610 "[N, M] -> { [M + floor(N/2)] }" },
6611 { "[M] -> { C[N] -> [M + floor(N/2)] }",
6612 "{ C[N] }",
6613 "[M, N] -> { [M + floor(N/2)] }" },
6614 { "[M] -> { C[x, N] -> [x + floor(N/2)] }",
6615 "{ C[M, N] }",
6616 "[M, N] -> { [M + floor(N/2)] }" },
6617 { "[M] -> { C[x, N] -> [M + floor(N/2)] }",
6618 "{ C[M, N] }",
6619 "[M, N] -> { [M + floor(N/2)] }" },
6620 { "[A, M] -> { C[N, x] -> [x + floor(N/2)] }",
6621 "{ C[N, M] }",
6622 "[A, N, M] -> { [M + floor(N/2)] }" },
6625 /* Perform basic isl_pw_multi_aff_bind_domain tests.
6627 static isl_stat test_bind_pma_domain(isl_ctx *ctx)
6629 int i;
6631 for (i = 0; i < ARRAY_SIZE(bind_pma_domain_tests); ++i) {
6632 const char *str;
6633 isl_pw_multi_aff *pma;
6634 isl_multi_id *tuple;
6635 isl_stat r;
6637 str = bind_pma_domain_tests[i].pma;
6638 pma = isl_pw_multi_aff_read_from_str(ctx, str);
6639 str = bind_pma_domain_tests[i].tuple;
6640 tuple = isl_multi_id_read_from_str(ctx, str);
6641 pma = isl_pw_multi_aff_bind_domain(pma, tuple);
6642 str = bind_pma_domain_tests[i].res;
6643 r = pw_multi_aff_check_plain_equal(pma, str);
6644 isl_pw_multi_aff_free(pma);
6645 if (r < 0)
6646 return isl_stat_error;
6649 return isl_stat_ok;
6652 /* Inputs for isl_pw_multi_aff_bind_domain_wrapped_domain tests.
6653 * "pma" is the input expression.
6654 * "tuple" is the binding tuple.
6655 * "res" is the expected result.
6657 struct {
6658 const char *pma;
6659 const char *tuple;
6660 const char *res;
6661 } bind_pma_domain_wrapped_tests[] = {
6662 { "{ [A[M, N] -> B[]] -> [M + floor(N/2)] }",
6663 "{ A[M, N] }",
6664 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
6665 { "{ [B[N, M] -> D[]] -> [M + floor(N/2)] }",
6666 "{ B[N, M] }",
6667 "[N, M] -> { D[] -> [M + floor(N/2)] }" },
6668 { "[M] -> { [C[N] -> B[x]] -> [x + M + floor(N/2)] }",
6669 "{ C[N] }",
6670 "[M, N] -> { B[x] -> [x + M + floor(N/2)] }" },
6671 { "[M] -> { [C[x, N] -> B[]] -> [x + floor(N/2)] }",
6672 "{ C[M, N] }",
6673 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
6674 { "[M] -> { [C[x, N] -> B[]] -> [M + floor(N/2)] }",
6675 "{ C[M, N] }",
6676 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
6677 { "[A, M] -> { [C[N, x] -> B[]] -> [x + floor(N/2)] }",
6678 "{ C[N, M] }",
6679 "[A, N, M] -> { B[] -> [M + floor(N/2)] }" },
6682 /* Perform basic isl_pw_multi_aff_bind_domain_wrapped_domain tests.
6684 static isl_stat test_bind_pma_domain_wrapped(isl_ctx *ctx)
6686 int i;
6688 for (i = 0; i < ARRAY_SIZE(bind_pma_domain_wrapped_tests); ++i) {
6689 const char *str;
6690 isl_pw_multi_aff *pma;
6691 isl_multi_id *tuple;
6692 isl_stat r;
6694 str = bind_pma_domain_wrapped_tests[i].pma;
6695 pma = isl_pw_multi_aff_read_from_str(ctx, str);
6696 str = bind_pma_domain_wrapped_tests[i].tuple;
6697 tuple = isl_multi_id_read_from_str(ctx, str);
6698 pma = isl_pw_multi_aff_bind_domain_wrapped_domain(pma, tuple);
6699 str = bind_pma_domain_wrapped_tests[i].res;
6700 r = pw_multi_aff_check_plain_equal(pma, str);
6701 isl_pw_multi_aff_free(pma);
6702 if (r < 0)
6703 return isl_stat_error;
6706 return isl_stat_ok;
6709 /* Inputs for isl_aff_bind_id tests.
6710 * "aff" is the input expression.
6711 * "id" is the binding id.
6712 * "res" is the expected result.
6714 static
6715 struct {
6716 const char *aff;
6717 const char *id;
6718 const char *res;
6719 } bind_aff_tests[] = {
6720 { "{ [4] }", "M", "[M = 4] -> { : }" },
6721 { "{ B[x] -> [floor(x/2)] }", "M", "[M] -> { B[x] : M = floor(x/2) }" },
6722 { "[M] -> { [4] }", "M", "[M = 4] -> { : }" },
6723 { "[M] -> { [floor(M/2)] }", "M", "[M] -> { : floor(M/2) = M }" },
6724 { "{ [NaN] }", "M", "{ : false }" },
6725 { "{ A[x] -> [NaN] }", "M", "{ A[x] : false }" },
6728 /* Perform basic isl_aff_bind_id tests.
6730 static isl_stat test_bind_aff(isl_ctx *ctx)
6732 int i;
6734 for (i = 0; i < ARRAY_SIZE(bind_aff_tests); ++i) {
6735 isl_aff *aff;
6736 isl_set *res;
6737 isl_id *id;
6738 isl_stat r;
6740 aff = isl_aff_read_from_str(ctx, bind_aff_tests[i].aff);
6741 id = isl_id_read_from_str(ctx, bind_aff_tests[i].id);
6742 res = isl_set_from_basic_set(isl_aff_bind_id(aff, id));
6743 r = set_check_equal(res, bind_aff_tests[i].res);
6744 isl_set_free(res);
6745 if (r < 0)
6746 return isl_stat_error;
6749 return isl_stat_ok;
6752 /* Inputs for isl_pw_aff_bind_id tests.
6753 * "pa" is the input expression.
6754 * "id" is the binding id.
6755 * "res" is the expected result.
6757 static
6758 struct {
6759 const char *pa;
6760 const char *id;
6761 const char *res;
6762 } bind_pa_tests[] = {
6763 { "{ [4] }", "M", "[M = 4] -> { : }" },
6764 { "{ B[x] -> [floor(x/2)] }", "M", "[M] -> { B[x] : M = floor(x/2) }" },
6765 { "[M] -> { [4] }", "M", "[M = 4] -> { : }" },
6766 { "[M] -> { [floor(M/2)] }", "M", "[M] -> { : floor(M/2) = M }" },
6767 { "[M] -> { [M] : M >= 0; [-M] : M < 0 }", "M", "[M] -> { : M >= 0 }" },
6768 { "{ [NaN] }", "M", "{ : false }" },
6769 { "{ A[x] -> [NaN] }", "M", "{ A[x] : false }" },
6772 /* Perform basic isl_pw_aff_bind_id tests.
6774 static isl_stat test_bind_pa(isl_ctx *ctx)
6776 int i;
6778 for (i = 0; i < ARRAY_SIZE(bind_pa_tests); ++i) {
6779 isl_pw_aff *pa;
6780 isl_set *res;
6781 isl_id *id;
6782 isl_stat r;
6784 pa = isl_pw_aff_read_from_str(ctx, bind_pa_tests[i].pa);
6785 id = isl_id_read_from_str(ctx, bind_pa_tests[i].id);
6786 res = isl_pw_aff_bind_id(pa, id);
6787 r = set_check_equal(res, bind_pa_tests[i].res);
6788 isl_set_free(res);
6789 if (r < 0)
6790 return isl_stat_error;
6793 return isl_stat_ok;
6796 /* Inputs for isl_multi_union_pw_aff_bind tests.
6797 * "mupa" is the input expression.
6798 * "tuple" is the binding tuple.
6799 * "res" is the expected result.
6801 static
6802 struct {
6803 const char *mupa;
6804 const char *tuple;
6805 const char *res;
6806 } bind_mupa_tests[] = {
6807 { "A[{ [4] }, { [5] }]",
6808 "{ A[M, N] }",
6809 "[M = 4, N = 5] -> { : }" },
6810 { "A[{ B[x] -> [floor(x/2)] }, { B[y] -> [y + 5] }]",
6811 "{ A[M, N] }",
6812 "[M, N] -> { B[x] : M = floor(x/2) and N = x + 5 }" },
6813 { "[M] -> A[{ [4] }, { [M + 1] }]",
6814 "{ A[M, N] }",
6815 "[M = 4, N = 5] -> { : }" },
6818 /* Perform basic isl_multi_union_pw_aff_bind tests.
6820 static isl_stat test_bind_mupa(isl_ctx *ctx)
6822 int i;
6824 for (i = 0; i < ARRAY_SIZE(bind_mupa_tests); ++i) {
6825 const char *str;
6826 isl_multi_union_pw_aff *mupa;
6827 isl_union_set *res;
6828 isl_multi_id *tuple;
6829 isl_stat r;
6831 str = bind_mupa_tests[i].mupa;
6832 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6833 str = bind_mupa_tests[i].tuple;
6834 tuple = isl_multi_id_read_from_str(ctx, str);
6835 res = isl_multi_union_pw_aff_bind(mupa, tuple);
6836 r = uset_check_equal(res, bind_mupa_tests[i].res);
6837 isl_union_set_free(res);
6838 if (r < 0)
6839 return isl_stat_error;
6842 return isl_stat_ok;
6845 /* Perform tests that reinterpret dimensions as parameters.
6847 static int test_bind(isl_ctx *ctx)
6849 if (test_bind_set(ctx) < 0)
6850 return -1;
6851 if (test_bind_map_domain(ctx) < 0)
6852 return -1;
6853 if (test_bind_umap_range(ctx) < 0)
6854 return -1;
6855 if (test_bind_pma_domain(ctx) < 0)
6856 return -1;
6857 if (test_bind_pma_domain_wrapped(ctx) < 0)
6858 return -1;
6859 if (test_bind_aff(ctx) < 0)
6860 return -1;
6861 if (test_bind_pa(ctx) < 0)
6862 return -1;
6863 if (test_bind_mupa(ctx) < 0)
6864 return -1;
6866 return 0;
6869 /* Inputs for isl_set_unbind_params tests.
6870 * "set" is the input parameter domain.
6871 * "tuple" is the tuple of the constructed set.
6872 * "res" is the expected result.
6874 struct {
6875 const char *set;
6876 const char *tuple;
6877 const char *res;
6878 } unbind_set_tests[] = {
6879 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
6880 "{ A[M, N] }",
6881 "{ A[M, N] : M mod 2 = 0 and N mod 8 = 3 }" },
6882 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
6883 "{ B[N, M] }",
6884 "{ B[N, M] : M mod 2 = 0 and N mod 8 = 3 }" },
6885 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
6886 "{ C[N] }",
6887 "[M] -> { C[N] : M mod 2 = 0 and N mod 8 = 3 }" },
6888 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
6889 "{ D[T, N] }",
6890 "[M] -> { D[x, N] : M mod 2 = 0 and N mod 8 = 3 }" },
6893 /* Perform basic isl_set_unbind_params tests.
6895 static isl_stat test_unbind_set(isl_ctx *ctx)
6897 int i;
6899 for (i = 0; i < ARRAY_SIZE(unbind_set_tests); ++i) {
6900 const char *str;
6901 isl_set *set;
6902 isl_multi_id *tuple;
6903 isl_stat r;
6905 set = isl_set_read_from_str(ctx, unbind_set_tests[i].set);
6906 str = unbind_set_tests[i].tuple;
6907 tuple = isl_multi_id_read_from_str(ctx, str);
6908 set = isl_set_unbind_params(set, tuple);
6909 r = set_check_equal(set, unbind_set_tests[i].res);
6910 isl_set_free(set);
6911 if (r < 0)
6912 return isl_stat_error;
6915 return isl_stat_ok;
6918 /* Inputs for isl_aff_unbind_params_insert_domain tests.
6919 * "aff" is the input affine expression defined over a parameter domain.
6920 * "tuple" is the tuple of the domain that gets introduced.
6921 * "res" is the expected result.
6923 struct {
6924 const char *aff;
6925 const char *tuple;
6926 const char *res;
6927 } unbind_aff_tests[] = {
6928 { "[M, N] -> { [M + floor(N/2)] }",
6929 "{ A[M, N] }",
6930 "{ A[M, N] -> [M + floor(N/2)] }" },
6931 { "[M, N] -> { [M + floor(N/2)] }",
6932 "{ B[N, M] }",
6933 "{ B[N, M] -> [M + floor(N/2)] }" },
6934 { "[M, N] -> { [M + floor(N/2)] }",
6935 "{ C[N] }",
6936 "[M] -> { C[N] -> [M + floor(N/2)] }" },
6937 { "[M, N] -> { [M + floor(N/2)] }",
6938 "{ D[A, B, C, N, Z] }",
6939 "[M] -> { D[A, B, C, N, Z] -> [M + floor(N/2)] }" },
6942 /* Perform basic isl_aff_unbind_params_insert_domain tests.
6944 static isl_stat test_unbind_aff(isl_ctx *ctx)
6946 int i;
6948 for (i = 0; i < ARRAY_SIZE(unbind_aff_tests); ++i) {
6949 const char *str;
6950 isl_aff *aff;
6951 isl_multi_id *tuple;
6952 isl_stat r;
6954 aff = isl_aff_read_from_str(ctx, unbind_aff_tests[i].aff);
6955 str = unbind_aff_tests[i].tuple;
6956 tuple = isl_multi_id_read_from_str(ctx, str);
6957 aff = isl_aff_unbind_params_insert_domain(aff, tuple);
6958 r = aff_check_plain_equal(aff, unbind_aff_tests[i].res);
6959 isl_aff_free(aff);
6960 if (r < 0)
6961 return isl_stat_error;
6964 return isl_stat_ok;
6967 /* Perform tests that reinterpret parameters.
6969 static int test_unbind(isl_ctx *ctx)
6971 if (test_unbind_set(ctx) < 0)
6972 return -1;
6973 if (test_unbind_aff(ctx) < 0)
6974 return -1;
6976 return 0;
6979 /* Check that "pa" consists of a single expression.
6981 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
6983 isl_size n;
6985 n = isl_pw_aff_n_piece(pa);
6986 isl_pw_aff_free(pa);
6988 if (n < 0)
6989 return -1;
6990 if (n != 1)
6991 isl_die(ctx, isl_error_unknown, "expecting single expression",
6992 return -1);
6994 return 0;
6997 /* Check that the computation below results in a single expression.
6998 * One or two expressions may result depending on which constraint
6999 * ends up being considered as redundant with respect to the other
7000 * constraints after the projection that is performed internally
7001 * by isl_set_dim_min.
7003 static int test_dim_max_1(isl_ctx *ctx)
7005 const char *str;
7006 isl_set *set;
7007 isl_pw_aff *pa;
7009 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
7010 "-4a <= b <= 3 and b < n - 4a }";
7011 set = isl_set_read_from_str(ctx, str);
7012 pa = isl_set_dim_min(set, 0);
7013 return check_single_piece(ctx, pa);
7016 /* Check that the computation below results in a single expression.
7017 * The PIP problem corresponding to these constraints has a row
7018 * that causes a split of the solution domain. The solver should
7019 * first pick rows that split off empty parts such that the actual
7020 * solution domain does not get split.
7021 * Note that the description contains some redundant constraints.
7022 * If these constraints get removed first, then the row mentioned
7023 * above does not appear in the PIP problem.
7025 static int test_dim_max_2(isl_ctx *ctx)
7027 const char *str;
7028 isl_set *set;
7029 isl_pw_aff *pa;
7031 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
7032 "N > 0 and P >= 0 }";
7033 set = isl_set_read_from_str(ctx, str);
7034 pa = isl_set_dim_max(set, 0);
7035 return check_single_piece(ctx, pa);
7038 int test_dim_max(isl_ctx *ctx)
7040 int equal;
7041 const char *str;
7042 isl_set *set1, *set2;
7043 isl_set *set;
7044 isl_map *map;
7045 isl_pw_aff *pwaff;
7047 if (test_dim_max_1(ctx) < 0)
7048 return -1;
7049 if (test_dim_max_2(ctx) < 0)
7050 return -1;
7052 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
7053 set = isl_set_read_from_str(ctx, str);
7054 pwaff = isl_set_dim_max(set, 0);
7055 set1 = isl_set_from_pw_aff(pwaff);
7056 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
7057 set2 = isl_set_read_from_str(ctx, str);
7058 equal = isl_set_is_equal(set1, set2);
7059 isl_set_free(set1);
7060 isl_set_free(set2);
7061 if (equal < 0)
7062 return -1;
7063 if (!equal)
7064 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7066 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
7067 set = isl_set_read_from_str(ctx, str);
7068 pwaff = isl_set_dim_max(set, 0);
7069 set1 = isl_set_from_pw_aff(pwaff);
7070 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
7071 set2 = isl_set_read_from_str(ctx, str);
7072 equal = isl_set_is_equal(set1, set2);
7073 isl_set_free(set1);
7074 isl_set_free(set2);
7075 if (equal < 0)
7076 return -1;
7077 if (!equal)
7078 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7080 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
7081 set = isl_set_read_from_str(ctx, str);
7082 pwaff = isl_set_dim_max(set, 0);
7083 set1 = isl_set_from_pw_aff(pwaff);
7084 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
7085 set2 = isl_set_read_from_str(ctx, str);
7086 equal = isl_set_is_equal(set1, set2);
7087 isl_set_free(set1);
7088 isl_set_free(set2);
7089 if (equal < 0)
7090 return -1;
7091 if (!equal)
7092 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7094 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
7095 "0 <= i < N and 0 <= j < M }";
7096 map = isl_map_read_from_str(ctx, str);
7097 set = isl_map_range(map);
7099 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
7100 set1 = isl_set_from_pw_aff(pwaff);
7101 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
7102 set2 = isl_set_read_from_str(ctx, str);
7103 equal = isl_set_is_equal(set1, set2);
7104 isl_set_free(set1);
7105 isl_set_free(set2);
7107 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
7108 set1 = isl_set_from_pw_aff(pwaff);
7109 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
7110 set2 = isl_set_read_from_str(ctx, str);
7111 if (equal >= 0 && equal)
7112 equal = isl_set_is_equal(set1, set2);
7113 isl_set_free(set1);
7114 isl_set_free(set2);
7116 isl_set_free(set);
7118 if (equal < 0)
7119 return -1;
7120 if (!equal)
7121 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7123 /* Check that solutions are properly merged. */
7124 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
7125 "c <= -1 + n - 4a - 2b and c >= -2b and "
7126 "4a >= -4 + n and c >= 0 }";
7127 set = isl_set_read_from_str(ctx, str);
7128 pwaff = isl_set_dim_min(set, 2);
7129 set1 = isl_set_from_pw_aff(pwaff);
7130 str = "[n] -> { [(0)] : n >= 1 }";
7131 set2 = isl_set_read_from_str(ctx, str);
7132 equal = isl_set_is_equal(set1, set2);
7133 isl_set_free(set1);
7134 isl_set_free(set2);
7136 if (equal < 0)
7137 return -1;
7138 if (!equal)
7139 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7141 /* Check that empty solution lie in the right space. */
7142 str = "[n] -> { [t,a] : 1 = 0 }";
7143 set = isl_set_read_from_str(ctx, str);
7144 pwaff = isl_set_dim_max(set, 0);
7145 set1 = isl_set_from_pw_aff(pwaff);
7146 str = "[n] -> { [t] : 1 = 0 }";
7147 set2 = isl_set_read_from_str(ctx, str);
7148 equal = isl_set_is_equal(set1, set2);
7149 isl_set_free(set1);
7150 isl_set_free(set2);
7152 if (equal < 0)
7153 return -1;
7154 if (!equal)
7155 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7157 return 0;
7160 /* Basic test for isl_pw_multi_aff_product.
7162 * Check that multiple pieces are properly handled.
7164 static int test_product_pma(isl_ctx *ctx)
7166 int equal;
7167 const char *str;
7168 isl_pw_multi_aff *pma1, *pma2;
7170 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
7171 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
7172 str = "{ C[] -> D[] }";
7173 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
7174 pma1 = isl_pw_multi_aff_product(pma1, pma2);
7175 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
7176 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
7177 equal = pw_multi_aff_check_plain_equal(pma1, str);
7178 isl_pw_multi_aff_free(pma1);
7179 if (equal < 0)
7180 return -1;
7182 return 0;
7185 int test_product(isl_ctx *ctx)
7187 const char *str;
7188 isl_set *set;
7189 isl_union_set *uset1, *uset2;
7190 int ok;
7192 str = "{ A[i] }";
7193 set = isl_set_read_from_str(ctx, str);
7194 set = isl_set_product(set, isl_set_copy(set));
7195 ok = isl_set_is_wrapping(set);
7196 isl_set_free(set);
7197 if (ok < 0)
7198 return -1;
7199 if (!ok)
7200 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7202 str = "{ [] }";
7203 uset1 = isl_union_set_read_from_str(ctx, str);
7204 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
7205 str = "{ [[] -> []] }";
7206 uset2 = isl_union_set_read_from_str(ctx, str);
7207 ok = isl_union_set_is_equal(uset1, uset2);
7208 isl_union_set_free(uset1);
7209 isl_union_set_free(uset2);
7210 if (ok < 0)
7211 return -1;
7212 if (!ok)
7213 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7215 if (test_product_pma(ctx) < 0)
7216 return -1;
7218 return 0;
7221 /* Check that two sets are not considered disjoint just because
7222 * they have a different set of (named) parameters.
7224 static int test_disjoint(isl_ctx *ctx)
7226 const char *str;
7227 isl_set *set, *set2;
7228 int disjoint;
7230 str = "[n] -> { [[]->[]] }";
7231 set = isl_set_read_from_str(ctx, str);
7232 str = "{ [[]->[]] }";
7233 set2 = isl_set_read_from_str(ctx, str);
7234 disjoint = isl_set_is_disjoint(set, set2);
7235 isl_set_free(set);
7236 isl_set_free(set2);
7237 if (disjoint < 0)
7238 return -1;
7239 if (disjoint)
7240 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7242 return 0;
7245 /* Inputs for isl_pw_multi_aff_is_equal tests.
7246 * "f1" and "f2" are the two function that need to be compared.
7247 * "equal" is the expected result.
7249 struct {
7250 int equal;
7251 const char *f1;
7252 const char *f2;
7253 } pma_equal_tests[] = {
7254 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
7255 "[N] -> { [0] : 0 <= N <= 1 }" },
7256 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
7257 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
7258 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
7259 "[N] -> { [0] : 0 <= N <= 1 }" },
7260 { 0, "{ [NaN] }", "{ [NaN] }" },
7263 int test_equal(isl_ctx *ctx)
7265 int i;
7266 const char *str;
7267 isl_set *set, *set2;
7268 int equal;
7270 str = "{ S_6[i] }";
7271 set = isl_set_read_from_str(ctx, str);
7272 str = "{ S_7[i] }";
7273 set2 = isl_set_read_from_str(ctx, str);
7274 equal = isl_set_is_equal(set, set2);
7275 isl_set_free(set);
7276 isl_set_free(set2);
7277 if (equal < 0)
7278 return -1;
7279 if (equal)
7280 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7282 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
7283 int expected = pma_equal_tests[i].equal;
7284 isl_pw_multi_aff *f1, *f2;
7286 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
7287 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
7288 equal = isl_pw_multi_aff_is_equal(f1, f2);
7289 isl_pw_multi_aff_free(f1);
7290 isl_pw_multi_aff_free(f2);
7291 if (equal < 0)
7292 return -1;
7293 if (equal != expected)
7294 isl_die(ctx, isl_error_unknown,
7295 "unexpected equality result", return -1);
7298 return 0;
7301 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
7302 enum isl_dim_type type, unsigned pos, int fixed)
7304 isl_bool test;
7306 test = isl_map_plain_is_fixed(map, type, pos, NULL);
7307 isl_map_free(map);
7308 if (test < 0)
7309 return -1;
7310 if (test == fixed)
7311 return 0;
7312 if (fixed)
7313 isl_die(ctx, isl_error_unknown,
7314 "map not detected as fixed", return -1);
7315 else
7316 isl_die(ctx, isl_error_unknown,
7317 "map detected as fixed", return -1);
7320 int test_fixed(isl_ctx *ctx)
7322 const char *str;
7323 isl_map *map;
7325 str = "{ [i] -> [i] }";
7326 map = isl_map_read_from_str(ctx, str);
7327 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
7328 return -1;
7329 str = "{ [i] -> [1] }";
7330 map = isl_map_read_from_str(ctx, str);
7331 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7332 return -1;
7333 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
7334 map = isl_map_read_from_str(ctx, str);
7335 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7336 return -1;
7337 map = isl_map_read_from_str(ctx, str);
7338 map = isl_map_neg(map);
7339 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7340 return -1;
7342 return 0;
7345 struct isl_vertices_test_data {
7346 const char *set;
7347 int n;
7348 const char *vertex[6];
7349 } vertices_tests[] = {
7350 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
7351 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
7352 { "{ A[t, i] : t = 14 and i = 1 }",
7353 1, { "{ A[14, 1] }" } },
7354 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
7355 "c <= m and m <= n and m > 0 }",
7356 6, {
7357 "[n, m] -> { [n, m, m] : 0 < m <= n }",
7358 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
7359 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
7360 "[n, m] -> { [m, m, m] : 0 < m <= n }",
7361 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
7362 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
7363 } },
7366 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
7368 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
7370 struct isl_vertices_test_data *data = user;
7371 isl_ctx *ctx;
7372 isl_multi_aff *ma;
7373 isl_basic_set *bset;
7374 isl_pw_multi_aff *pma;
7375 int i;
7376 isl_bool equal;
7378 ctx = isl_vertex_get_ctx(vertex);
7379 bset = isl_vertex_get_domain(vertex);
7380 ma = isl_vertex_get_expr(vertex);
7381 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
7383 for (i = 0; i < data->n; ++i) {
7384 isl_pw_multi_aff *pma_i;
7386 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
7387 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
7388 isl_pw_multi_aff_free(pma_i);
7390 if (equal < 0 || equal)
7391 break;
7394 isl_pw_multi_aff_free(pma);
7395 isl_vertex_free(vertex);
7397 if (equal < 0)
7398 return isl_stat_error;
7400 return equal ? isl_stat_ok : isl_stat_error;
7403 int test_vertices(isl_ctx *ctx)
7405 int i;
7407 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
7408 isl_basic_set *bset;
7409 isl_vertices *vertices;
7410 int ok = 1;
7411 isl_size n;
7413 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
7414 vertices = isl_basic_set_compute_vertices(bset);
7415 n = isl_vertices_get_n_vertices(vertices);
7416 if (vertices_tests[i].n != n)
7417 ok = 0;
7418 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
7419 &vertices_tests[i]) < 0)
7420 ok = 0;
7421 isl_vertices_free(vertices);
7422 isl_basic_set_free(bset);
7424 if (n < 0)
7425 return -1;
7426 if (!ok)
7427 isl_die(ctx, isl_error_unknown, "unexpected vertices",
7428 return -1);
7431 return 0;
7434 int test_union_pw(isl_ctx *ctx)
7436 int equal;
7437 const char *str;
7438 isl_union_set *uset;
7439 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
7441 str = "{ [x] -> x^2 }";
7442 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
7443 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
7444 uset = isl_union_pw_qpolynomial_domain(upwqp1);
7445 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
7446 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
7447 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
7448 isl_union_pw_qpolynomial_free(upwqp1);
7449 isl_union_pw_qpolynomial_free(upwqp2);
7450 if (equal < 0)
7451 return -1;
7452 if (!equal)
7453 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7455 return 0;
7458 /* Inputs for basic tests of functions that select
7459 * subparts of the domain of an isl_multi_union_pw_aff.
7460 * "fn" is the function that is tested.
7461 * "arg" is a string description of the input.
7462 * "res" is a string description of the expected result.
7464 struct {
7465 __isl_give isl_union_set *(*fn)(
7466 __isl_take isl_multi_union_pw_aff *mupa);
7467 const char *arg;
7468 const char *res;
7469 } un_locus_tests[] = {
7470 { &isl_multi_union_pw_aff_zero_union_set,
7471 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
7472 "{ A[0,j]; B[0,j] }" },
7473 { &isl_multi_union_pw_aff_zero_union_set,
7474 "F[{ A[i,j] -> [i-j]; B[i,j] -> [i-j] : i >= 0 }]",
7475 "{ A[i,i]; B[i,i] : i >= 0 }" },
7476 { &isl_multi_union_pw_aff_zero_union_set,
7477 "(F[] : { A[i,j]; B[i,i] : i >= 0 })",
7478 "{ A[i,j]; B[i,i] : i >= 0 }" },
7481 /* Perform some basic tests of functions that select
7482 * subparts of the domain of an isl_multi_union_pw_aff.
7484 static int test_un_locus(isl_ctx *ctx)
7486 int i;
7487 isl_bool ok;
7488 isl_union_set *uset, *res;
7489 isl_multi_union_pw_aff *mupa;
7491 for (i = 0; i < ARRAY_SIZE(un_locus_tests); ++i) {
7492 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
7493 un_locus_tests[i].arg);
7494 res = isl_union_set_read_from_str(ctx, un_locus_tests[i].res);
7495 uset = un_locus_tests[i].fn(mupa);
7496 ok = isl_union_set_is_equal(uset, res);
7497 isl_union_set_free(uset);
7498 isl_union_set_free(res);
7499 if (ok < 0)
7500 return -1;
7501 if (!ok)
7502 isl_die(ctx, isl_error_unknown,
7503 "unexpected result", return -1);
7506 return 0;
7509 /* Inputs for basic tests of functions that select
7510 * subparts of an isl_union_map based on a relation
7511 * specified by an isl_multi_union_pw_aff.
7512 * "fn" is the function that is tested.
7513 * "arg1" and "arg2" are string descriptions of the inputs.
7514 * "res" is a string description of the expected result.
7516 struct {
7517 __isl_give isl_union_map *(*fn)(
7518 __isl_take isl_union_map *umap,
7519 __isl_take isl_multi_union_pw_aff *mupa);
7520 const char *arg1;
7521 const char *arg2;
7522 const char *res;
7523 } bin_locus_tests[] = {
7524 { &isl_union_map_eq_at_multi_union_pw_aff,
7525 "{ A[i,j] -> B[i',j'] }",
7526 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
7527 "{ A[i,j] -> B[i,j'] }" },
7528 { &isl_union_map_eq_at_multi_union_pw_aff,
7529 "{ A[i,j] -> B[i',j'] }",
7530 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
7531 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
7532 "{ A[i,j] -> B[i,j] }" },
7533 { &isl_union_map_eq_at_multi_union_pw_aff,
7534 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
7535 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
7536 "{ A[i,j] -> B[i,j'] }" },
7537 { &isl_union_map_eq_at_multi_union_pw_aff,
7538 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
7539 "F[{ A[i,j] -> [i]; B[i,j] -> [i]; C[i,j] -> [0] }]",
7540 "{ A[i,j] -> B[i,j']; A[0,j] -> C[i',j'] }" },
7541 { &isl_union_map_eq_at_multi_union_pw_aff,
7542 "{ A[i,j] -> B[i',j'] }",
7543 "F[{ A[i,j] -> [i] : i > j; B[i,j] -> [i] }]",
7544 "{ A[i,j] -> B[i,j'] : i > j }" },
7545 { &isl_union_map_lex_lt_at_multi_union_pw_aff,
7546 "{ A[i,j] -> B[i',j'] }",
7547 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
7548 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
7549 "{ A[i,j] -> B[i',j'] : i,j << i',j' }" },
7550 { &isl_union_map_lex_gt_at_multi_union_pw_aff,
7551 "{ A[i,j] -> B[i',j'] }",
7552 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
7553 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
7554 "{ A[i,j] -> B[i',j'] : i,j >> i',j' }" },
7555 { &isl_union_map_eq_at_multi_union_pw_aff,
7556 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
7557 "(F[] : { A[i,j]; B[i,j] })",
7558 "{ A[i,j] -> B[i',j'] }" },
7559 { &isl_union_map_eq_at_multi_union_pw_aff,
7560 "{ A[i,j] -> B[i',j'] }",
7561 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
7562 "{ A[i,j] -> B[i',j'] : i > j and i' < j' }" },
7563 { &isl_union_map_eq_at_multi_union_pw_aff,
7564 "[N] -> { A[i,j] -> B[i',j'] : i,i' <= N }",
7565 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
7566 "[N] -> { A[i,j] -> B[i',j'] : i > j and i' < j' and i,i' <= N }" },
7567 { &isl_union_map_eq_at_multi_union_pw_aff,
7568 "{ A[i,j] -> B[i',j'] }",
7569 "[N] -> (F[] : { A[i,j] : i < N; B[i,j] : i < N })",
7570 "[N] -> { A[i,j] -> B[i',j'] : i,i' < N }" },
7571 { &isl_union_map_eq_at_multi_union_pw_aff,
7572 "{ A[i,j] -> B[i',j'] }",
7573 "[N] -> (F[] : { : N >= 0 })",
7574 "[N] -> { A[i,j] -> B[i',j'] : N >= 0 }" },
7577 /* Perform some basic tests of functions that select
7578 * subparts of an isl_union_map based on a relation
7579 * specified by an isl_multi_union_pw_aff.
7581 static int test_bin_locus(isl_ctx *ctx)
7583 int i;
7584 isl_bool ok;
7585 isl_union_map *umap, *res;
7586 isl_multi_union_pw_aff *mupa;
7588 for (i = 0; i < ARRAY_SIZE(bin_locus_tests); ++i) {
7589 umap = isl_union_map_read_from_str(ctx,
7590 bin_locus_tests[i].arg1);
7591 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
7592 bin_locus_tests[i].arg2);
7593 res = isl_union_map_read_from_str(ctx, bin_locus_tests[i].res);
7594 umap = bin_locus_tests[i].fn(umap, mupa);
7595 ok = isl_union_map_is_equal(umap, res);
7596 isl_union_map_free(umap);
7597 isl_union_map_free(res);
7598 if (ok < 0)
7599 return -1;
7600 if (!ok)
7601 isl_die(ctx, isl_error_unknown,
7602 "unexpected result", return -1);
7605 return 0;
7608 /* Perform basic locus tests.
7610 static int test_locus(isl_ctx *ctx)
7612 if (test_un_locus(ctx) < 0)
7613 return -1;
7614 if (test_bin_locus(ctx) < 0)
7615 return -1;
7616 return 0;
7619 /* Test that isl_union_pw_qpolynomial_eval picks up the function
7620 * defined over the correct domain space.
7622 static int test_eval_1(isl_ctx *ctx)
7624 const char *str;
7625 isl_point *pnt;
7626 isl_set *set;
7627 isl_union_pw_qpolynomial *upwqp;
7628 isl_val *v;
7629 int cmp;
7631 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
7632 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
7633 str = "{ A[6] }";
7634 set = isl_set_read_from_str(ctx, str);
7635 pnt = isl_set_sample_point(set);
7636 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
7637 cmp = isl_val_cmp_si(v, 36);
7638 isl_val_free(v);
7640 if (!v)
7641 return -1;
7642 if (cmp != 0)
7643 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
7645 return 0;
7648 /* Check that isl_qpolynomial_eval handles getting called on a void point.
7650 static int test_eval_2(isl_ctx *ctx)
7652 const char *str;
7653 isl_point *pnt;
7654 isl_set *set;
7655 isl_qpolynomial *qp;
7656 isl_val *v;
7657 isl_bool ok;
7659 str = "{ A[x] -> [x] }";
7660 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
7661 str = "{ A[x] : false }";
7662 set = isl_set_read_from_str(ctx, str);
7663 pnt = isl_set_sample_point(set);
7664 v = isl_qpolynomial_eval(qp, pnt);
7665 ok = isl_val_is_nan(v);
7666 isl_val_free(v);
7668 if (ok < 0)
7669 return -1;
7670 if (!ok)
7671 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
7673 return 0;
7676 /* Check that a polynomial (without local variables) can be evaluated
7677 * in a rational point.
7679 static isl_stat test_eval_3(isl_ctx *ctx)
7681 isl_pw_qpolynomial *pwqp;
7682 isl_point *pnt;
7683 isl_val *v;
7684 isl_stat r;
7686 pwqp = isl_pw_qpolynomial_read_from_str(ctx, "{ [x] -> x^2 }");
7687 pnt = isl_point_zero(isl_pw_qpolynomial_get_domain_space(pwqp));
7688 v = isl_val_read_from_str(ctx, "1/2");
7689 pnt = isl_point_set_coordinate_val(pnt, isl_dim_set, 0, v);
7690 v = isl_pw_qpolynomial_eval(pwqp, pnt);
7691 r = val_check_equal(v, "1/4");
7692 isl_val_free(v);
7694 return r;
7697 /* Inputs for isl_pw_aff_eval test.
7698 * "f" is the affine function.
7699 * "p" is the point where the function should be evaluated.
7700 * "res" is the expected result.
7702 struct {
7703 const char *f;
7704 const char *p;
7705 const char *res;
7706 } aff_eval_tests[] = {
7707 { "{ [i] -> [2 * i] }", "{ [4] }", "8" },
7708 { "{ [i] -> [2 * i] }", "{ [x] : false }", "NaN" },
7709 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [0] }", "0" },
7710 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [1] }", "1" },
7711 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [2] }", "3" },
7712 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [3] }", "5" },
7713 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [4] }", "7" },
7714 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [0] }", "0" },
7715 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [1] }", "0" },
7716 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [2] }", "0" },
7717 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [3] }", "0" },
7718 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [4] }", "1" },
7719 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [6] }", "1" },
7720 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [8] }", "2" },
7721 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [4] }", "4" },
7722 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [-2] }", "2" },
7723 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [0] }", "NaN" },
7724 { "[N] -> { [2 * N] }", "[N] -> { : N = 4 }", "8" },
7725 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 1] }", "1" },
7726 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 2] }", "3/2" },
7727 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [4] }", "4" },
7728 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [3] }", "NaN" },
7729 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [x] : false }", "NaN" },
7732 /* Perform basic isl_pw_aff_eval tests.
7734 static int test_eval_aff(isl_ctx *ctx)
7736 int i;
7738 for (i = 0; i < ARRAY_SIZE(aff_eval_tests); ++i) {
7739 isl_stat r;
7740 isl_pw_aff *pa;
7741 isl_set *set;
7742 isl_point *pnt;
7743 isl_val *v;
7745 pa = isl_pw_aff_read_from_str(ctx, aff_eval_tests[i].f);
7746 set = isl_set_read_from_str(ctx, aff_eval_tests[i].p);
7747 pnt = isl_set_sample_point(set);
7748 v = isl_pw_aff_eval(pa, pnt);
7749 r = val_check_equal(v, aff_eval_tests[i].res);
7750 isl_val_free(v);
7751 if (r < 0)
7752 return -1;
7754 return 0;
7757 /* Perform basic evaluation tests.
7759 static int test_eval(isl_ctx *ctx)
7761 if (test_eval_1(ctx) < 0)
7762 return -1;
7763 if (test_eval_2(ctx) < 0)
7764 return -1;
7765 if (test_eval_3(ctx) < 0)
7766 return -1;
7767 if (test_eval_aff(ctx) < 0)
7768 return -1;
7769 return 0;
7772 /* Descriptions of sets that are tested for reparsing after printing.
7774 const char *output_tests[] = {
7775 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
7776 "{ [x] : 1 = 0 }",
7777 "{ [x] : false }",
7778 "{ [x] : x mod 2 = 0 }",
7779 "{ [x] : x mod 2 = 1 }",
7780 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
7781 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
7782 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7783 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7784 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
7785 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
7788 /* Check that printing a set and reparsing a set from the printed output
7789 * results in the same set.
7791 static int test_output_set(isl_ctx *ctx)
7793 int i;
7794 char *str;
7795 isl_set *set1, *set2;
7796 isl_bool equal;
7798 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
7799 set1 = isl_set_read_from_str(ctx, output_tests[i]);
7800 str = isl_set_to_str(set1);
7801 set2 = isl_set_read_from_str(ctx, str);
7802 free(str);
7803 equal = isl_set_is_equal(set1, set2);
7804 isl_set_free(set1);
7805 isl_set_free(set2);
7806 if (equal < 0)
7807 return -1;
7808 if (!equal)
7809 isl_die(ctx, isl_error_unknown,
7810 "parsed output not the same", return -1);
7813 return 0;
7816 /* Check that an isl_multi_aff is printed using a consistent space.
7818 static isl_stat test_output_ma(isl_ctx *ctx)
7820 char *str;
7821 isl_bool equal;
7822 isl_aff *aff;
7823 isl_multi_aff *ma, *ma2;
7825 ma = isl_multi_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7826 aff = isl_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7827 ma = isl_multi_aff_set_aff(ma, 0, aff);
7828 str = isl_multi_aff_to_str(ma);
7829 ma2 = isl_multi_aff_read_from_str(ctx, str);
7830 free(str);
7831 equal = isl_multi_aff_plain_is_equal(ma, ma2);
7832 isl_multi_aff_free(ma2);
7833 isl_multi_aff_free(ma);
7835 if (equal < 0)
7836 return isl_stat_error;
7837 if (!equal)
7838 isl_die(ctx, isl_error_unknown, "bad conversion",
7839 return isl_stat_error);
7841 return isl_stat_ok;
7844 /* Check that an isl_multi_pw_aff is printed using a consistent space.
7846 static isl_stat test_output_mpa(isl_ctx *ctx)
7848 char *str;
7849 isl_bool equal;
7850 isl_pw_aff *pa;
7851 isl_multi_pw_aff *mpa, *mpa2;
7853 mpa = isl_multi_pw_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7854 pa = isl_pw_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7855 mpa = isl_multi_pw_aff_set_pw_aff(mpa, 0, pa);
7856 str = isl_multi_pw_aff_to_str(mpa);
7857 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
7858 free(str);
7859 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
7860 isl_multi_pw_aff_free(mpa2);
7861 isl_multi_pw_aff_free(mpa);
7863 if (equal < 0)
7864 return isl_stat_error;
7865 if (!equal)
7866 isl_die(ctx, isl_error_unknown, "bad conversion",
7867 return isl_stat_error);
7869 return isl_stat_ok;
7872 int test_output(isl_ctx *ctx)
7874 char *s;
7875 const char *str;
7876 isl_pw_aff *pa;
7877 isl_printer *p;
7878 int equal;
7880 if (test_output_set(ctx) < 0)
7881 return -1;
7882 if (test_output_ma(ctx) < 0)
7883 return -1;
7884 if (test_output_mpa(ctx) < 0)
7885 return -1;
7887 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
7888 pa = isl_pw_aff_read_from_str(ctx, str);
7890 p = isl_printer_to_str(ctx);
7891 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
7892 p = isl_printer_print_pw_aff(p, pa);
7893 s = isl_printer_get_str(p);
7894 isl_printer_free(p);
7895 isl_pw_aff_free(pa);
7896 if (!s)
7897 equal = -1;
7898 else
7899 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
7900 free(s);
7901 if (equal < 0)
7902 return -1;
7903 if (!equal)
7904 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7906 return 0;
7909 int test_sample(isl_ctx *ctx)
7911 const char *str;
7912 isl_basic_set *bset1, *bset2;
7913 int empty, subset;
7915 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
7916 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
7917 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
7918 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
7919 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
7920 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
7921 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
7922 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
7923 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
7924 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
7925 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
7926 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
7927 "d - 1073741821e and "
7928 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
7929 "3j >= 1 - a + b + 2e and "
7930 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
7931 "3i <= 4 - a + 4b - e and "
7932 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
7933 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
7934 "c <= -1 + a and 3i >= -2 - a + 3e and "
7935 "1073741822e <= 1073741823 - a + 1073741822b + c and "
7936 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
7937 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
7938 "1073741823e >= 1 + 1073741823b - d and "
7939 "3i >= 1073741823b + c - 1073741823e - f and "
7940 "3i >= 1 + 2b + e + 3g }";
7941 bset1 = isl_basic_set_read_from_str(ctx, str);
7942 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
7943 empty = isl_basic_set_is_empty(bset2);
7944 subset = isl_basic_set_is_subset(bset2, bset1);
7945 isl_basic_set_free(bset1);
7946 isl_basic_set_free(bset2);
7947 if (empty < 0 || subset < 0)
7948 return -1;
7949 if (empty)
7950 isl_die(ctx, isl_error_unknown, "point not found", return -1);
7951 if (!subset)
7952 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
7954 return 0;
7957 int test_fixed_power(isl_ctx *ctx)
7959 const char *str;
7960 isl_map *map;
7961 isl_val *exp;
7962 int equal;
7964 str = "{ [i] -> [i + 1] }";
7965 map = isl_map_read_from_str(ctx, str);
7966 exp = isl_val_int_from_si(ctx, 23);
7967 map = isl_map_fixed_power_val(map, exp);
7968 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
7969 isl_map_free(map);
7970 if (equal < 0)
7971 return -1;
7973 return 0;
7976 int test_slice(isl_ctx *ctx)
7978 const char *str;
7979 isl_map *map;
7980 int equal;
7982 str = "{ [i] -> [j] }";
7983 map = isl_map_read_from_str(ctx, str);
7984 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
7985 equal = map_check_equal(map, "{ [i] -> [i] }");
7986 isl_map_free(map);
7987 if (equal < 0)
7988 return -1;
7990 str = "{ [i] -> [j] }";
7991 map = isl_map_read_from_str(ctx, str);
7992 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
7993 equal = map_check_equal(map, "{ [i] -> [j] }");
7994 isl_map_free(map);
7995 if (equal < 0)
7996 return -1;
7998 str = "{ [i] -> [j] }";
7999 map = isl_map_read_from_str(ctx, str);
8000 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
8001 equal = map_check_equal(map, "{ [i] -> [-i] }");
8002 isl_map_free(map);
8003 if (equal < 0)
8004 return -1;
8006 str = "{ [i] -> [j] }";
8007 map = isl_map_read_from_str(ctx, str);
8008 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
8009 equal = map_check_equal(map, "{ [0] -> [j] }");
8010 isl_map_free(map);
8011 if (equal < 0)
8012 return -1;
8014 str = "{ [i] -> [j] }";
8015 map = isl_map_read_from_str(ctx, str);
8016 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
8017 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
8018 isl_map_free(map);
8019 if (equal < 0)
8020 return -1;
8022 str = "{ [i] -> [j] }";
8023 map = isl_map_read_from_str(ctx, str);
8024 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
8025 equal = map_check_equal(map, "{ [i] -> [j] : false }");
8026 isl_map_free(map);
8027 if (equal < 0)
8028 return -1;
8030 return 0;
8033 int test_eliminate(isl_ctx *ctx)
8035 const char *str;
8036 isl_map *map;
8037 int equal;
8039 str = "{ [i] -> [j] : i = 2j }";
8040 map = isl_map_read_from_str(ctx, str);
8041 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
8042 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
8043 isl_map_free(map);
8044 if (equal < 0)
8045 return -1;
8047 return 0;
8050 /* Check basic functionality of isl_map_deltas_map.
8052 static int test_deltas_map(isl_ctx *ctx)
8054 const char *str;
8055 isl_map *map;
8056 int equal;
8058 str = "{ A[i] -> A[i + 1] }";
8059 map = isl_map_read_from_str(ctx, str);
8060 map = isl_map_deltas_map(map);
8061 equal = map_check_equal(map, "{ [A[i] -> A[i + 1]] -> A[1] }");
8062 isl_map_free(map);
8063 if (equal < 0)
8064 return -1;
8066 return 0;
8069 /* Check that isl_set_dim_residue_class detects that the values of j
8070 * in the set below are all odd and that it does not detect any spurious
8071 * strides.
8073 static int test_residue_class(isl_ctx *ctx)
8075 const char *str;
8076 isl_set *set;
8077 isl_int m, r;
8078 isl_stat res;
8080 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
8081 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
8082 set = isl_set_read_from_str(ctx, str);
8083 isl_int_init(m);
8084 isl_int_init(r);
8085 res = isl_set_dim_residue_class(set, 1, &m, &r);
8086 if (res >= 0 &&
8087 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
8088 isl_die(ctx, isl_error_unknown, "incorrect residue class",
8089 res = isl_stat_error);
8090 isl_int_clear(r);
8091 isl_int_clear(m);
8092 isl_set_free(set);
8094 return res;
8097 static int test_align_parameters_1(isl_ctx *ctx)
8099 const char *str;
8100 isl_space *space;
8101 isl_multi_aff *ma1, *ma2;
8102 int equal;
8104 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
8105 ma1 = isl_multi_aff_read_from_str(ctx, str);
8107 space = isl_space_params_alloc(ctx, 1);
8108 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
8109 ma1 = isl_multi_aff_align_params(ma1, space);
8111 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
8112 ma2 = isl_multi_aff_read_from_str(ctx, str);
8114 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
8116 isl_multi_aff_free(ma1);
8117 isl_multi_aff_free(ma2);
8119 if (equal < 0)
8120 return -1;
8121 if (!equal)
8122 isl_die(ctx, isl_error_unknown,
8123 "result not as expected", return -1);
8125 return 0;
8128 /* Check the isl_multi_*_from_*_list operation in case inputs
8129 * have unaligned parameters.
8130 * In particular, older versions of isl would simply fail
8131 * (without printing any error message).
8133 static isl_stat test_align_parameters_2(isl_ctx *ctx)
8135 isl_space *space;
8136 isl_map *map;
8137 isl_aff *aff;
8138 isl_multi_aff *ma;
8140 map = isl_map_read_from_str(ctx, "{ A[] -> M[x] }");
8141 space = isl_map_get_space(map);
8142 isl_map_free(map);
8144 aff = isl_aff_read_from_str(ctx, "[N] -> { A[] -> [N] }");
8145 ma = isl_multi_aff_from_aff_list(space, isl_aff_list_from_aff(aff));
8146 isl_multi_aff_free(ma);
8148 if (!ma)
8149 return isl_stat_error;
8150 return isl_stat_ok;
8153 /* Perform basic parameter alignment tests.
8155 static int test_align_parameters(isl_ctx *ctx)
8157 if (test_align_parameters_1(ctx) < 0)
8158 return -1;
8159 if (test_align_parameters_2(ctx) < 0)
8160 return -1;
8162 return 0;
8165 /* Check that isl_*_drop_unused_params actually drops the unused parameters
8166 * by comparing the result using isl_*_plain_is_equal.
8167 * Note that this assumes that isl_*_plain_is_equal does not consider
8168 * objects that only differ by unused parameters to be equal.
8170 int test_drop_unused_parameters(isl_ctx *ctx)
8172 const char *str_with, *str_without;
8173 isl_basic_set *bset1, *bset2;
8174 isl_set *set1, *set2;
8175 isl_pw_aff *pwa1, *pwa2;
8176 int equal;
8178 str_with = "[n, m, o] -> { [m] }";
8179 str_without = "[m] -> { [m] }";
8181 bset1 = isl_basic_set_read_from_str(ctx, str_with);
8182 bset2 = isl_basic_set_read_from_str(ctx, str_without);
8183 bset1 = isl_basic_set_drop_unused_params(bset1);
8184 equal = isl_basic_set_plain_is_equal(bset1, bset2);
8185 isl_basic_set_free(bset1);
8186 isl_basic_set_free(bset2);
8188 if (equal < 0)
8189 return -1;
8190 if (!equal)
8191 isl_die(ctx, isl_error_unknown,
8192 "result not as expected", return -1);
8194 set1 = isl_set_read_from_str(ctx, str_with);
8195 set2 = isl_set_read_from_str(ctx, str_without);
8196 set1 = isl_set_drop_unused_params(set1);
8197 equal = isl_set_plain_is_equal(set1, set2);
8198 isl_set_free(set1);
8199 isl_set_free(set2);
8201 if (equal < 0)
8202 return -1;
8203 if (!equal)
8204 isl_die(ctx, isl_error_unknown,
8205 "result not as expected", return -1);
8207 pwa1 = isl_pw_aff_read_from_str(ctx, str_with);
8208 pwa2 = isl_pw_aff_read_from_str(ctx, str_without);
8209 pwa1 = isl_pw_aff_drop_unused_params(pwa1);
8210 equal = isl_pw_aff_plain_is_equal(pwa1, pwa2);
8211 isl_pw_aff_free(pwa1);
8212 isl_pw_aff_free(pwa2);
8214 if (equal < 0)
8215 return -1;
8216 if (!equal)
8217 isl_die(ctx, isl_error_unknown,
8218 "result not as expected", return -1);
8220 return 0;
8223 static int test_list(isl_ctx *ctx)
8225 isl_id *a, *b, *c, *d, *id;
8226 isl_id_list *list;
8227 isl_size n;
8228 int ok;
8230 a = isl_id_alloc(ctx, "a", NULL);
8231 b = isl_id_alloc(ctx, "b", NULL);
8232 c = isl_id_alloc(ctx, "c", NULL);
8233 d = isl_id_alloc(ctx, "d", NULL);
8235 list = isl_id_list_alloc(ctx, 4);
8236 list = isl_id_list_add(list, b);
8237 list = isl_id_list_insert(list, 0, a);
8238 list = isl_id_list_add(list, c);
8239 list = isl_id_list_add(list, d);
8240 list = isl_id_list_drop(list, 1, 1);
8242 n = isl_id_list_n_id(list);
8243 if (n < 0)
8244 return -1;
8245 if (n != 3) {
8246 isl_id_list_free(list);
8247 isl_die(ctx, isl_error_unknown,
8248 "unexpected number of elements in list", return -1);
8251 id = isl_id_list_get_id(list, 0);
8252 ok = id == a;
8253 isl_id_free(id);
8254 id = isl_id_list_get_id(list, 1);
8255 ok = ok && id == c;
8256 isl_id_free(id);
8257 id = isl_id_list_get_id(list, 2);
8258 ok = ok && id == d;
8259 isl_id_free(id);
8261 isl_id_list_free(list);
8263 if (!ok)
8264 isl_die(ctx, isl_error_unknown,
8265 "unexpected elements in list", return -1);
8267 return 0;
8270 /* Check the conversion from an isl_multi_aff to an isl_basic_set.
8272 static isl_stat test_ma_conversion(isl_ctx *ctx)
8274 const char *str;
8275 isl_bool equal;
8276 isl_multi_aff *ma;
8277 isl_basic_set *bset1, *bset2;
8279 str = "[N] -> { A[0, N + 1] }";
8280 ma = isl_multi_aff_read_from_str(ctx, str);
8281 bset1 = isl_basic_set_read_from_str(ctx, str);
8282 bset2 = isl_basic_set_from_multi_aff(ma);
8283 equal = isl_basic_set_is_equal(bset1, bset2);
8284 isl_basic_set_free(bset1);
8285 isl_basic_set_free(bset2);
8286 if (equal < 0)
8287 return isl_stat_error;
8288 if (!equal)
8289 isl_die(ctx, isl_error_unknown, "bad conversion",
8290 return isl_stat_error);
8291 return isl_stat_ok;
8294 const char *set_conversion_tests[] = {
8295 "[N] -> { [i] : N - 1 <= 2 i <= N }",
8296 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
8297 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
8298 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
8299 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
8300 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
8301 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
8302 "-3 + c <= d <= 28 + c) }",
8305 /* Check that converting from isl_set to isl_pw_multi_aff and back
8306 * to isl_set produces the original isl_set.
8308 static int test_set_conversion(isl_ctx *ctx)
8310 int i;
8311 const char *str;
8312 isl_set *set1, *set2;
8313 isl_pw_multi_aff *pma;
8314 int equal;
8316 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
8317 str = set_conversion_tests[i];
8318 set1 = isl_set_read_from_str(ctx, str);
8319 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
8320 set2 = isl_set_from_pw_multi_aff(pma);
8321 equal = isl_set_is_equal(set1, set2);
8322 isl_set_free(set1);
8323 isl_set_free(set2);
8325 if (equal < 0)
8326 return -1;
8327 if (!equal)
8328 isl_die(ctx, isl_error_unknown, "bad conversion",
8329 return -1);
8332 return 0;
8335 const char *conversion_tests[] = {
8336 "{ [a, b, c, d] -> s0[a, b, e, f] : "
8337 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
8338 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
8339 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
8340 "9e <= -2 - 2a) }",
8341 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
8342 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
8343 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
8346 /* Check that converting from isl_map to isl_pw_multi_aff and back
8347 * to isl_map produces the original isl_map.
8349 static int test_map_conversion(isl_ctx *ctx)
8351 int i;
8352 isl_map *map1, *map2;
8353 isl_pw_multi_aff *pma;
8354 int equal;
8356 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
8357 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
8358 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
8359 map2 = isl_map_from_pw_multi_aff(pma);
8360 equal = isl_map_is_equal(map1, map2);
8361 isl_map_free(map1);
8362 isl_map_free(map2);
8364 if (equal < 0)
8365 return -1;
8366 if (!equal)
8367 isl_die(ctx, isl_error_unknown, "bad conversion",
8368 return -1);
8371 return 0;
8374 /* Descriptions of isl_pw_multi_aff objects for testing conversion
8375 * to isl_multi_pw_aff and back.
8377 const char *mpa_conversion_tests[] = {
8378 "{ [x] -> A[x] }",
8379 "{ [x] -> A[x] : x >= 0 }",
8380 "{ [x] -> A[x] : x >= 0; [x] -> A[-x] : x < 0 }",
8381 "{ [x] -> A[x, x + 1] }",
8382 "{ [x] -> A[] }",
8383 "{ [x] -> A[] : x >= 0 }",
8386 /* Check that conversion from isl_pw_multi_aff to isl_multi_pw_aff and
8387 * back to isl_pw_multi_aff preserves the original meaning.
8389 static int test_mpa_conversion(isl_ctx *ctx)
8391 int i;
8392 isl_pw_multi_aff *pma1, *pma2;
8393 isl_multi_pw_aff *mpa;
8394 int equal;
8396 for (i = 0; i < ARRAY_SIZE(mpa_conversion_tests); ++i) {
8397 const char *str;
8398 str = mpa_conversion_tests[i];
8399 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
8400 pma2 = isl_pw_multi_aff_copy(pma1);
8401 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma1);
8402 pma1 = isl_pw_multi_aff_from_multi_pw_aff(mpa);
8403 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
8404 isl_pw_multi_aff_free(pma1);
8405 isl_pw_multi_aff_free(pma2);
8407 if (equal < 0)
8408 return -1;
8409 if (!equal)
8410 isl_die(ctx, isl_error_unknown, "bad conversion",
8411 return -1);
8414 return 0;
8417 /* Descriptions of union maps that should be convertible
8418 * to an isl_multi_union_pw_aff.
8420 const char *umap_mupa_conversion_tests[] = {
8421 "{ [a, b, c, d] -> s0[a, b, e, f] : "
8422 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
8423 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
8424 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
8425 "9e <= -2 - 2a) }",
8426 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
8427 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
8428 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
8429 "{ A[] -> B[0]; C[] -> B[1] }",
8430 "{ A[] -> B[]; C[] -> B[] }",
8433 /* Check that converting from isl_union_map to isl_multi_union_pw_aff and back
8434 * to isl_union_map produces the original isl_union_map.
8436 static int test_union_map_mupa_conversion(isl_ctx *ctx)
8438 int i;
8439 isl_union_map *umap1, *umap2;
8440 isl_multi_union_pw_aff *mupa;
8441 int equal;
8443 for (i = 0; i < ARRAY_SIZE(umap_mupa_conversion_tests); ++i) {
8444 const char *str;
8445 str = umap_mupa_conversion_tests[i];
8446 umap1 = isl_union_map_read_from_str(ctx, str);
8447 umap2 = isl_union_map_copy(umap1);
8448 mupa = isl_multi_union_pw_aff_from_union_map(umap2);
8449 umap2 = isl_union_map_from_multi_union_pw_aff(mupa);
8450 equal = isl_union_map_is_equal(umap1, umap2);
8451 isl_union_map_free(umap1);
8452 isl_union_map_free(umap2);
8454 if (equal < 0)
8455 return -1;
8456 if (!equal)
8457 isl_die(ctx, isl_error_unknown, "bad conversion",
8458 return -1);
8461 return 0;
8464 static int test_conversion(isl_ctx *ctx)
8466 if (test_ma_conversion(ctx) < 0)
8467 return -1;
8468 if (test_set_conversion(ctx) < 0)
8469 return -1;
8470 if (test_map_conversion(ctx) < 0)
8471 return -1;
8472 if (test_mpa_conversion(ctx) < 0)
8473 return -1;
8474 if (test_union_map_mupa_conversion(ctx) < 0)
8475 return -1;
8476 return 0;
8479 /* Check that isl_basic_map_curry does not modify input.
8481 static int test_curry(isl_ctx *ctx)
8483 const char *str;
8484 isl_basic_map *bmap1, *bmap2;
8485 int equal;
8487 str = "{ [A[] -> B[]] -> C[] }";
8488 bmap1 = isl_basic_map_read_from_str(ctx, str);
8489 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
8490 equal = isl_basic_map_is_equal(bmap1, bmap2);
8491 isl_basic_map_free(bmap1);
8492 isl_basic_map_free(bmap2);
8494 if (equal < 0)
8495 return -1;
8496 if (equal)
8497 isl_die(ctx, isl_error_unknown,
8498 "curried map should not be equal to original",
8499 return -1);
8501 return 0;
8504 struct {
8505 const char *set;
8506 const char *ma;
8507 const char *res;
8508 } preimage_tests[] = {
8509 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
8510 "{ A[j,i] -> B[i,j] }",
8511 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
8512 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
8513 "{ A[a,b] -> B[a/2,b/6] }",
8514 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
8515 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
8516 "{ A[a,b] -> B[a/2,b/6] }",
8517 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
8518 "exists i,j : a = 2 i and b = 6 j }" },
8519 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
8520 "[n] -> { : 0 <= n <= 100 }" },
8521 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
8522 "{ A[a] -> B[2a] }",
8523 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
8524 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
8525 "{ A[a] -> B[([a/2])] }",
8526 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
8527 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
8528 "{ A[a] -> B[a,a,a/3] }",
8529 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
8530 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
8531 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
8534 static int test_preimage_basic_set(isl_ctx *ctx)
8536 int i;
8537 isl_basic_set *bset1, *bset2;
8538 isl_multi_aff *ma;
8539 int equal;
8541 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
8542 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
8543 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
8544 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
8545 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
8546 equal = isl_basic_set_is_equal(bset1, bset2);
8547 isl_basic_set_free(bset1);
8548 isl_basic_set_free(bset2);
8549 if (equal < 0)
8550 return -1;
8551 if (!equal)
8552 isl_die(ctx, isl_error_unknown, "bad preimage",
8553 return -1);
8556 return 0;
8559 struct {
8560 const char *map;
8561 const char *ma;
8562 const char *res;
8563 } preimage_domain_tests[] = {
8564 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
8565 "{ A[j,i] -> B[i,j] }",
8566 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
8567 { "{ B[i] -> C[i]; D[i] -> E[i] }",
8568 "{ A[i] -> B[i + 1] }",
8569 "{ A[i] -> C[i + 1] }" },
8570 { "{ B[i] -> C[i]; B[i] -> E[i] }",
8571 "{ A[i] -> B[i + 1] }",
8572 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
8573 { "{ B[i] -> C[([i/2])] }",
8574 "{ A[i] -> B[2i] }",
8575 "{ A[i] -> C[i] }" },
8576 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
8577 "{ A[i] -> B[([i/5]), ([i/7])] }",
8578 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
8579 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
8580 "[N] -> { A[] -> B[([N/5])] }",
8581 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
8582 { "{ B[i] -> C[i] : exists a : i = 5 a }",
8583 "{ A[i] -> B[2i] }",
8584 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
8585 { "{ B[i] -> C[i] : exists a : i = 2 a; "
8586 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
8587 "{ A[i] -> B[2i] }",
8588 "{ A[i] -> C[2i] }" },
8589 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
8590 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
8593 static int test_preimage_union_map(isl_ctx *ctx)
8595 int i;
8596 isl_union_map *umap1, *umap2;
8597 isl_multi_aff *ma;
8598 int equal;
8600 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
8601 umap1 = isl_union_map_read_from_str(ctx,
8602 preimage_domain_tests[i].map);
8603 ma = isl_multi_aff_read_from_str(ctx,
8604 preimage_domain_tests[i].ma);
8605 umap2 = isl_union_map_read_from_str(ctx,
8606 preimage_domain_tests[i].res);
8607 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
8608 equal = isl_union_map_is_equal(umap1, umap2);
8609 isl_union_map_free(umap1);
8610 isl_union_map_free(umap2);
8611 if (equal < 0)
8612 return -1;
8613 if (!equal)
8614 isl_die(ctx, isl_error_unknown, "bad preimage",
8615 return -1);
8618 return 0;
8621 static int test_preimage(isl_ctx *ctx)
8623 if (test_preimage_basic_set(ctx) < 0)
8624 return -1;
8625 if (test_preimage_union_map(ctx) < 0)
8626 return -1;
8628 return 0;
8631 struct {
8632 const char *ma1;
8633 const char *ma;
8634 const char *res;
8635 } pullback_tests[] = {
8636 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
8637 "{ A[a,b] -> C[b + 2a] }" },
8638 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
8639 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
8640 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
8641 "{ A[a] -> C[(a)/6] }" },
8642 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
8643 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
8644 "{ A[a] -> C[(2a)/3] }" },
8645 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
8646 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
8647 "{ A[i,j] -> C[i + j, i + j] }"},
8648 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
8649 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
8650 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
8651 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
8652 "{ [i, j] -> [floor((i)/3), j] }",
8653 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
8656 static int test_pullback(isl_ctx *ctx)
8658 int i;
8659 isl_multi_aff *ma1, *ma2;
8660 isl_multi_aff *ma;
8661 int equal;
8663 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
8664 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
8665 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
8666 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
8667 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
8668 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
8669 isl_multi_aff_free(ma1);
8670 isl_multi_aff_free(ma2);
8671 if (equal < 0)
8672 return -1;
8673 if (!equal)
8674 isl_die(ctx, isl_error_unknown, "bad pullback",
8675 return -1);
8678 return 0;
8681 /* Check that negation is printed correctly and that equal expressions
8682 * are correctly identified.
8684 static int test_ast(isl_ctx *ctx)
8686 isl_ast_expr *expr, *expr1, *expr2, *expr3;
8687 char *str;
8688 int ok, equal;
8690 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
8691 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
8692 expr = isl_ast_expr_add(expr1, expr2);
8693 expr2 = isl_ast_expr_copy(expr);
8694 expr = isl_ast_expr_neg(expr);
8695 expr2 = isl_ast_expr_neg(expr2);
8696 equal = isl_ast_expr_is_equal(expr, expr2);
8697 str = isl_ast_expr_to_C_str(expr);
8698 ok = str ? !strcmp(str, "-(A + B)") : -1;
8699 free(str);
8700 isl_ast_expr_free(expr);
8701 isl_ast_expr_free(expr2);
8703 if (ok < 0 || equal < 0)
8704 return -1;
8705 if (!equal)
8706 isl_die(ctx, isl_error_unknown,
8707 "equal expressions not considered equal", return -1);
8708 if (!ok)
8709 isl_die(ctx, isl_error_unknown,
8710 "isl_ast_expr printed incorrectly", return -1);
8712 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
8713 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
8714 expr = isl_ast_expr_add(expr1, expr2);
8715 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
8716 expr = isl_ast_expr_sub(expr3, expr);
8717 str = isl_ast_expr_to_C_str(expr);
8718 ok = str ? !strcmp(str, "C - (A + B)") : -1;
8719 free(str);
8720 isl_ast_expr_free(expr);
8722 if (ok < 0)
8723 return -1;
8724 if (!ok)
8725 isl_die(ctx, isl_error_unknown,
8726 "isl_ast_expr printed incorrectly", return -1);
8728 return 0;
8731 /* Check that isl_ast_build_expr_from_set returns a valid expression
8732 * for an empty set. Note that isl_ast_build_expr_from_set getting
8733 * called on an empty set probably indicates a bug in the caller.
8735 static int test_ast_build(isl_ctx *ctx)
8737 isl_set *set;
8738 isl_ast_build *build;
8739 isl_ast_expr *expr;
8741 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8742 build = isl_ast_build_from_context(set);
8744 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
8745 expr = isl_ast_build_expr_from_set(build, set);
8747 isl_ast_expr_free(expr);
8748 isl_ast_build_free(build);
8750 if (!expr)
8751 return -1;
8753 return 0;
8756 /* Internal data structure for before_for and after_for callbacks.
8758 * depth is the current depth
8759 * before is the number of times before_for has been called
8760 * after is the number of times after_for has been called
8762 struct isl_test_codegen_data {
8763 int depth;
8764 int before;
8765 int after;
8768 /* This function is called before each for loop in the AST generated
8769 * from test_ast_gen1.
8771 * Increment the number of calls and the depth.
8772 * Check that the space returned by isl_ast_build_get_schedule_space
8773 * matches the target space of the schedule returned by
8774 * isl_ast_build_get_schedule.
8775 * Return an isl_id that is checked by the corresponding call
8776 * to after_for.
8778 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
8779 void *user)
8781 struct isl_test_codegen_data *data = user;
8782 isl_ctx *ctx;
8783 isl_space *space;
8784 isl_union_map *schedule;
8785 isl_union_set *uset;
8786 isl_set *set;
8787 isl_bool empty;
8788 isl_size n;
8789 char name[] = "d0";
8791 ctx = isl_ast_build_get_ctx(build);
8793 if (data->before >= 3)
8794 isl_die(ctx, isl_error_unknown,
8795 "unexpected number of for nodes", return NULL);
8796 if (data->depth >= 2)
8797 isl_die(ctx, isl_error_unknown,
8798 "unexpected depth", return NULL);
8800 snprintf(name, sizeof(name), "d%d", data->depth);
8801 data->before++;
8802 data->depth++;
8804 schedule = isl_ast_build_get_schedule(build);
8805 uset = isl_union_map_range(schedule);
8806 n = isl_union_set_n_set(uset);
8807 if (n != 1) {
8808 isl_union_set_free(uset);
8809 if (n < 0)
8810 return NULL;
8811 isl_die(ctx, isl_error_unknown,
8812 "expecting single range space", return NULL);
8815 space = isl_ast_build_get_schedule_space(build);
8816 set = isl_union_set_extract_set(uset, space);
8817 isl_union_set_free(uset);
8818 empty = isl_set_is_empty(set);
8819 isl_set_free(set);
8821 if (empty < 0)
8822 return NULL;
8823 if (empty)
8824 isl_die(ctx, isl_error_unknown,
8825 "spaces don't match", return NULL);
8827 return isl_id_alloc(ctx, name, NULL);
8830 /* This function is called after each for loop in the AST generated
8831 * from test_ast_gen1.
8833 * Increment the number of calls and decrement the depth.
8834 * Check that the annotation attached to the node matches
8835 * the isl_id returned by the corresponding call to before_for.
8837 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
8838 __isl_keep isl_ast_build *build, void *user)
8840 struct isl_test_codegen_data *data = user;
8841 isl_id *id;
8842 const char *name;
8843 int valid;
8845 data->after++;
8846 data->depth--;
8848 if (data->after > data->before)
8849 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8850 "mismatch in number of for nodes",
8851 return isl_ast_node_free(node));
8853 id = isl_ast_node_get_annotation(node);
8854 if (!id)
8855 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8856 "missing annotation", return isl_ast_node_free(node));
8858 name = isl_id_get_name(id);
8859 valid = name && atoi(name + 1) == data->depth;
8860 isl_id_free(id);
8862 if (!valid)
8863 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8864 "wrong annotation", return isl_ast_node_free(node));
8866 return node;
8869 /* Check that the before_each_for and after_each_for callbacks
8870 * are called for each for loop in the generated code,
8871 * that they are called in the right order and that the isl_id
8872 * returned from the before_each_for callback is attached to
8873 * the isl_ast_node passed to the corresponding after_each_for call.
8875 static int test_ast_gen1(isl_ctx *ctx)
8877 const char *str;
8878 isl_set *set;
8879 isl_union_map *schedule;
8880 isl_ast_build *build;
8881 isl_ast_node *tree;
8882 struct isl_test_codegen_data data;
8884 str = "[N] -> { : N >= 10 }";
8885 set = isl_set_read_from_str(ctx, str);
8886 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
8887 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
8888 schedule = isl_union_map_read_from_str(ctx, str);
8890 data.before = 0;
8891 data.after = 0;
8892 data.depth = 0;
8893 build = isl_ast_build_from_context(set);
8894 build = isl_ast_build_set_before_each_for(build,
8895 &before_for, &data);
8896 build = isl_ast_build_set_after_each_for(build,
8897 &after_for, &data);
8898 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8899 isl_ast_build_free(build);
8900 if (!tree)
8901 return -1;
8903 isl_ast_node_free(tree);
8905 if (data.before != 3 || data.after != 3)
8906 isl_die(ctx, isl_error_unknown,
8907 "unexpected number of for nodes", return -1);
8909 return 0;
8912 /* Check that the AST generator handles domains that are integrally disjoint
8913 * but not rationally disjoint.
8915 static int test_ast_gen2(isl_ctx *ctx)
8917 const char *str;
8918 isl_set *set;
8919 isl_union_map *schedule;
8920 isl_union_map *options;
8921 isl_ast_build *build;
8922 isl_ast_node *tree;
8924 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
8925 schedule = isl_union_map_read_from_str(ctx, str);
8926 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8927 build = isl_ast_build_from_context(set);
8929 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
8930 options = isl_union_map_read_from_str(ctx, str);
8931 build = isl_ast_build_set_options(build, options);
8932 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8933 isl_ast_build_free(build);
8934 if (!tree)
8935 return -1;
8936 isl_ast_node_free(tree);
8938 return 0;
8941 /* Increment *user on each call.
8943 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
8944 __isl_keep isl_ast_build *build, void *user)
8946 int *n = user;
8948 (*n)++;
8950 return node;
8953 /* Test that unrolling tries to minimize the number of instances.
8954 * In particular, for the schedule given below, make sure it generates
8955 * 3 nodes (rather than 101).
8957 static int test_ast_gen3(isl_ctx *ctx)
8959 const char *str;
8960 isl_set *set;
8961 isl_union_map *schedule;
8962 isl_union_map *options;
8963 isl_ast_build *build;
8964 isl_ast_node *tree;
8965 int n_domain = 0;
8967 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
8968 schedule = isl_union_map_read_from_str(ctx, str);
8969 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8971 str = "{ [i] -> unroll[0] }";
8972 options = isl_union_map_read_from_str(ctx, str);
8974 build = isl_ast_build_from_context(set);
8975 build = isl_ast_build_set_options(build, options);
8976 build = isl_ast_build_set_at_each_domain(build,
8977 &count_domains, &n_domain);
8978 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8979 isl_ast_build_free(build);
8980 if (!tree)
8981 return -1;
8983 isl_ast_node_free(tree);
8985 if (n_domain != 3)
8986 isl_die(ctx, isl_error_unknown,
8987 "unexpected number of for nodes", return -1);
8989 return 0;
8992 /* Check that if the ast_build_exploit_nested_bounds options is set,
8993 * we do not get an outer if node in the generated AST,
8994 * while we do get such an outer if node if the options is not set.
8996 static int test_ast_gen4(isl_ctx *ctx)
8998 const char *str;
8999 isl_set *set;
9000 isl_union_map *schedule;
9001 isl_ast_build *build;
9002 isl_ast_node *tree;
9003 enum isl_ast_node_type type;
9004 int enb;
9006 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
9007 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
9009 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
9011 schedule = isl_union_map_read_from_str(ctx, str);
9012 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9013 build = isl_ast_build_from_context(set);
9014 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9015 isl_ast_build_free(build);
9016 if (!tree)
9017 return -1;
9019 type = isl_ast_node_get_type(tree);
9020 isl_ast_node_free(tree);
9022 if (type == isl_ast_node_if)
9023 isl_die(ctx, isl_error_unknown,
9024 "not expecting if node", return -1);
9026 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
9028 schedule = isl_union_map_read_from_str(ctx, str);
9029 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9030 build = isl_ast_build_from_context(set);
9031 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9032 isl_ast_build_free(build);
9033 if (!tree)
9034 return -1;
9036 type = isl_ast_node_get_type(tree);
9037 isl_ast_node_free(tree);
9039 if (type != isl_ast_node_if)
9040 isl_die(ctx, isl_error_unknown,
9041 "expecting if node", return -1);
9043 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
9045 return 0;
9048 /* This function is called for each leaf in the AST generated
9049 * from test_ast_gen5.
9051 * We finalize the AST generation by extending the outer schedule
9052 * with a zero-dimensional schedule. If this results in any for loops,
9053 * then this means that we did not pass along enough information
9054 * about the outer schedule to the inner AST generation.
9056 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
9057 void *user)
9059 isl_union_map *schedule, *extra;
9060 isl_ast_node *tree;
9062 schedule = isl_ast_build_get_schedule(build);
9063 extra = isl_union_map_copy(schedule);
9064 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
9065 schedule = isl_union_map_range_product(schedule, extra);
9066 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9067 isl_ast_build_free(build);
9069 if (!tree)
9070 return NULL;
9072 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
9073 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
9074 "code should not contain any for loop",
9075 return isl_ast_node_free(tree));
9077 return tree;
9080 /* Check that we do not lose any information when going back and
9081 * forth between internal and external schedule.
9083 * In particular, we create an AST where we unroll the only
9084 * non-constant dimension in the schedule. We therefore do
9085 * not expect any for loops in the AST. However, older versions
9086 * of isl would not pass along enough information about the outer
9087 * schedule when performing an inner code generation from a create_leaf
9088 * callback, resulting in the inner code generation producing a for loop.
9090 static int test_ast_gen5(isl_ctx *ctx)
9092 const char *str;
9093 isl_set *set;
9094 isl_union_map *schedule, *options;
9095 isl_ast_build *build;
9096 isl_ast_node *tree;
9098 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
9099 schedule = isl_union_map_read_from_str(ctx, str);
9101 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
9102 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
9103 options = isl_union_map_read_from_str(ctx, str);
9105 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9106 build = isl_ast_build_from_context(set);
9107 build = isl_ast_build_set_options(build, options);
9108 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
9109 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9110 isl_ast_build_free(build);
9111 isl_ast_node_free(tree);
9112 if (!tree)
9113 return -1;
9115 return 0;
9118 /* Check that the expression
9120 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
9122 * is not combined into
9124 * min(n/2, 0)
9126 * as this would result in n/2 being evaluated in parts of
9127 * the definition domain where n is not a multiple of 2.
9129 static int test_ast_expr(isl_ctx *ctx)
9131 const char *str;
9132 isl_pw_aff *pa;
9133 isl_ast_build *build;
9134 isl_ast_expr *expr;
9135 int min_max;
9136 int is_min;
9138 min_max = isl_options_get_ast_build_detect_min_max(ctx);
9139 isl_options_set_ast_build_detect_min_max(ctx, 1);
9141 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
9142 pa = isl_pw_aff_read_from_str(ctx, str);
9143 build = isl_ast_build_alloc(ctx);
9144 expr = isl_ast_build_expr_from_pw_aff(build, pa);
9145 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
9146 isl_ast_expr_get_op_type(expr) == isl_ast_expr_op_min;
9147 isl_ast_build_free(build);
9148 isl_ast_expr_free(expr);
9150 isl_options_set_ast_build_detect_min_max(ctx, min_max);
9152 if (!expr)
9153 return -1;
9154 if (is_min)
9155 isl_die(ctx, isl_error_unknown,
9156 "expressions should not be combined", return -1);
9158 return 0;
9161 static int test_ast_gen(isl_ctx *ctx)
9163 if (test_ast_gen1(ctx) < 0)
9164 return -1;
9165 if (test_ast_gen2(ctx) < 0)
9166 return -1;
9167 if (test_ast_gen3(ctx) < 0)
9168 return -1;
9169 if (test_ast_gen4(ctx) < 0)
9170 return -1;
9171 if (test_ast_gen5(ctx) < 0)
9172 return -1;
9173 if (test_ast_expr(ctx) < 0)
9174 return -1;
9175 return 0;
9178 /* Check if dropping output dimensions from an isl_pw_multi_aff
9179 * works properly.
9181 static int test_pw_multi_aff(isl_ctx *ctx)
9183 const char *str;
9184 isl_pw_multi_aff *pma1, *pma2;
9185 int equal;
9187 str = "{ [i,j] -> [i+j, 4i-j] }";
9188 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
9189 str = "{ [i,j] -> [4i-j] }";
9190 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
9192 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
9194 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
9196 isl_pw_multi_aff_free(pma1);
9197 isl_pw_multi_aff_free(pma2);
9198 if (equal < 0)
9199 return -1;
9200 if (!equal)
9201 isl_die(ctx, isl_error_unknown,
9202 "expressions not equal", return -1);
9204 return 0;
9207 /* Check that we can properly parse multi piecewise affine expressions
9208 * where the piecewise affine expressions have different domains.
9210 static int test_multi_pw_aff_1(isl_ctx *ctx)
9212 const char *str;
9213 isl_set *dom, *dom2;
9214 isl_multi_pw_aff *mpa1, *mpa2;
9215 isl_pw_aff *pa;
9216 int equal;
9217 int equal_domain;
9219 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
9220 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
9221 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
9222 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
9223 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
9224 str = "{ [i] -> [(i : i > 0), 2i] }";
9225 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
9227 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
9229 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
9230 dom = isl_pw_aff_domain(pa);
9231 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
9232 dom2 = isl_pw_aff_domain(pa);
9233 equal_domain = isl_set_is_equal(dom, dom2);
9235 isl_set_free(dom);
9236 isl_set_free(dom2);
9237 isl_multi_pw_aff_free(mpa1);
9238 isl_multi_pw_aff_free(mpa2);
9240 if (equal < 0)
9241 return -1;
9242 if (!equal)
9243 isl_die(ctx, isl_error_unknown,
9244 "expressions not equal", return -1);
9246 if (equal_domain < 0)
9247 return -1;
9248 if (equal_domain)
9249 isl_die(ctx, isl_error_unknown,
9250 "domains unexpectedly equal", return -1);
9252 return 0;
9255 /* Check that the dimensions in the explicit domain
9256 * of a multi piecewise affine expression are properly
9257 * taken into account.
9259 static int test_multi_pw_aff_2(isl_ctx *ctx)
9261 const char *str;
9262 isl_bool involves1, involves2, involves3, equal;
9263 isl_multi_pw_aff *mpa, *mpa1, *mpa2;
9265 str = "{ A[x,y] -> B[] : x >= y }";
9266 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
9267 involves1 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 2);
9268 mpa1 = isl_multi_pw_aff_copy(mpa);
9270 mpa = isl_multi_pw_aff_insert_dims(mpa, isl_dim_in, 0, 1);
9271 involves2 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 1);
9272 involves3 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 1, 2);
9273 str = "{ [a,x,y] -> B[] : x >= y }";
9274 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
9275 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
9276 isl_multi_pw_aff_free(mpa2);
9278 mpa = isl_multi_pw_aff_drop_dims(mpa, isl_dim_in, 0, 1);
9279 mpa = isl_multi_pw_aff_set_tuple_name(mpa, isl_dim_in, "A");
9280 if (equal >= 0 && equal)
9281 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa1);
9282 isl_multi_pw_aff_free(mpa1);
9283 isl_multi_pw_aff_free(mpa);
9285 if (involves1 < 0 || involves2 < 0 || involves3 < 0 || equal < 0)
9286 return -1;
9287 if (!equal)
9288 isl_die(ctx, isl_error_unknown,
9289 "incorrect result of dimension insertion/removal",
9290 return isl_stat_error);
9291 if (!involves1 || involves2 || !involves3)
9292 isl_die(ctx, isl_error_unknown,
9293 "incorrect characterization of involved dimensions",
9294 return isl_stat_error);
9296 return 0;
9299 /* Check that isl_multi_union_pw_aff_multi_val_on_domain
9300 * sets the explicit domain of a zero-dimensional result,
9301 * such that it can be converted to an isl_union_map.
9303 static isl_stat test_multi_pw_aff_3(isl_ctx *ctx)
9305 isl_space *space;
9306 isl_union_set *dom;
9307 isl_multi_val *mv;
9308 isl_multi_union_pw_aff *mupa;
9309 isl_union_map *umap;
9311 dom = isl_union_set_read_from_str(ctx, "{ A[]; B[] }");
9312 space = isl_union_set_get_space(dom);
9313 mv = isl_multi_val_zero(isl_space_set_from_params(space));
9314 mupa = isl_multi_union_pw_aff_multi_val_on_domain(dom, mv);
9315 umap = isl_union_map_from_multi_union_pw_aff(mupa);
9316 isl_union_map_free(umap);
9317 if (!umap)
9318 return isl_stat_error;
9320 return isl_stat_ok;
9323 /* Perform some tests on multi piecewise affine expressions.
9325 static int test_multi_pw_aff(isl_ctx *ctx)
9327 if (test_multi_pw_aff_1(ctx) < 0)
9328 return -1;
9329 if (test_multi_pw_aff_2(ctx) < 0)
9330 return -1;
9331 if (test_multi_pw_aff_3(ctx) < 0)
9332 return -1;
9333 return 0;
9336 /* This is a regression test for a bug where isl_basic_map_simplify
9337 * would end up in an infinite loop. In particular, we construct
9338 * an empty basic set that is not obviously empty.
9339 * isl_basic_set_is_empty marks the basic set as empty.
9340 * After projecting out i3, the variable can be dropped completely,
9341 * but isl_basic_map_simplify refrains from doing so if the basic set
9342 * is empty and would end up in an infinite loop if it didn't test
9343 * explicitly for empty basic maps in the outer loop.
9345 static int test_simplify_1(isl_ctx *ctx)
9347 const char *str;
9348 isl_basic_set *bset;
9349 int empty;
9351 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
9352 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
9353 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
9354 "i3 >= i2 }";
9355 bset = isl_basic_set_read_from_str(ctx, str);
9356 empty = isl_basic_set_is_empty(bset);
9357 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
9358 isl_basic_set_free(bset);
9359 if (!bset)
9360 return -1;
9361 if (!empty)
9362 isl_die(ctx, isl_error_unknown,
9363 "basic set should be empty", return -1);
9365 return 0;
9368 /* Check that the equality in the set description below
9369 * is simplified away.
9371 static int test_simplify_2(isl_ctx *ctx)
9373 const char *str;
9374 isl_basic_set *bset;
9375 isl_bool universe;
9377 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
9378 bset = isl_basic_set_read_from_str(ctx, str);
9379 universe = isl_basic_set_plain_is_universe(bset);
9380 isl_basic_set_free(bset);
9382 if (universe < 0)
9383 return -1;
9384 if (!universe)
9385 isl_die(ctx, isl_error_unknown,
9386 "equality not simplified away", return -1);
9387 return 0;
9390 /* Some simplification tests.
9392 static int test_simplify(isl_ctx *ctx)
9394 if (test_simplify_1(ctx) < 0)
9395 return -1;
9396 if (test_simplify_2(ctx) < 0)
9397 return -1;
9398 return 0;
9401 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
9402 * with gbr context would fail to disable the use of the shifted tableau
9403 * when transferring equalities for the input to the context, resulting
9404 * in invalid sample values.
9406 static int test_partial_lexmin(isl_ctx *ctx)
9408 const char *str;
9409 isl_basic_set *bset;
9410 isl_basic_map *bmap;
9411 isl_map *map;
9413 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
9414 bmap = isl_basic_map_read_from_str(ctx, str);
9415 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
9416 bset = isl_basic_set_read_from_str(ctx, str);
9417 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
9418 isl_map_free(map);
9420 if (!map)
9421 return -1;
9423 return 0;
9426 /* Check that the variable compression performed on the existentially
9427 * quantified variables inside isl_basic_set_compute_divs is not confused
9428 * by the implicit equalities among the parameters.
9430 static int test_compute_divs(isl_ctx *ctx)
9432 const char *str;
9433 isl_basic_set *bset;
9434 isl_set *set;
9436 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
9437 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
9438 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
9439 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
9440 bset = isl_basic_set_read_from_str(ctx, str);
9441 set = isl_basic_set_compute_divs(bset);
9442 isl_set_free(set);
9443 if (!set)
9444 return -1;
9446 return 0;
9449 /* Check that isl_schedule_get_map is not confused by a schedule tree
9450 * with divergent filter node parameters, as can result from a call
9451 * to isl_schedule_intersect_domain.
9453 static int test_schedule_tree(isl_ctx *ctx)
9455 const char *str;
9456 isl_union_set *uset;
9457 isl_schedule *sched1, *sched2;
9458 isl_union_map *umap;
9460 uset = isl_union_set_read_from_str(ctx, "{ A[i] }");
9461 sched1 = isl_schedule_from_domain(uset);
9462 uset = isl_union_set_read_from_str(ctx, "{ B[] }");
9463 sched2 = isl_schedule_from_domain(uset);
9465 sched1 = isl_schedule_sequence(sched1, sched2);
9466 str = "[n] -> { A[i] : 0 <= i < n; B[] }";
9467 uset = isl_union_set_read_from_str(ctx, str);
9468 sched1 = isl_schedule_intersect_domain(sched1, uset);
9469 umap = isl_schedule_get_map(sched1);
9470 isl_schedule_free(sched1);
9471 isl_union_map_free(umap);
9472 if (!umap)
9473 return -1;
9475 return 0;
9478 /* Check that a zero-dimensional prefix schedule keeps track
9479 * of the domain and outer filters.
9481 static int test_schedule_tree_prefix(isl_ctx *ctx)
9483 const char *str;
9484 isl_bool equal;
9485 isl_union_set *uset;
9486 isl_union_set_list *filters;
9487 isl_multi_union_pw_aff *mupa, *mupa2;
9488 isl_schedule_node *node;
9490 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
9491 uset = isl_union_set_read_from_str(ctx, str);
9492 node = isl_schedule_node_from_domain(uset);
9493 node = isl_schedule_node_child(node, 0);
9495 str = "{ S1[i,j] : i > j }";
9496 uset = isl_union_set_read_from_str(ctx, str);
9497 filters = isl_union_set_list_from_union_set(uset);
9498 str = "{ S1[i,j] : i <= j; S2[i,j] }";
9499 uset = isl_union_set_read_from_str(ctx, str);
9500 filters = isl_union_set_list_add(filters, uset);
9501 node = isl_schedule_node_insert_sequence(node, filters);
9503 node = isl_schedule_node_child(node, 0);
9504 node = isl_schedule_node_child(node, 0);
9505 mupa = isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
9506 str = "([] : { S1[i,j] : i > j })";
9507 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx, str);
9508 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
9509 isl_multi_union_pw_aff_free(mupa2);
9510 isl_multi_union_pw_aff_free(mupa);
9511 isl_schedule_node_free(node);
9513 if (equal < 0)
9514 return -1;
9515 if (!equal)
9516 isl_die(ctx, isl_error_unknown, "unexpected prefix schedule",
9517 return -1);
9519 return 0;
9522 /* Check that the reaching domain elements and the prefix schedule
9523 * at a leaf node are the same before and after grouping.
9525 static int test_schedule_tree_group_1(isl_ctx *ctx)
9527 int equal;
9528 const char *str;
9529 isl_id *id;
9530 isl_union_set *uset;
9531 isl_multi_union_pw_aff *mupa;
9532 isl_union_pw_multi_aff *upma1, *upma2;
9533 isl_union_set *domain1, *domain2;
9534 isl_union_map *umap1, *umap2;
9535 isl_schedule_node *node;
9537 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
9538 uset = isl_union_set_read_from_str(ctx, str);
9539 node = isl_schedule_node_from_domain(uset);
9540 node = isl_schedule_node_child(node, 0);
9541 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
9542 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9543 node = isl_schedule_node_insert_partial_schedule(node, mupa);
9544 node = isl_schedule_node_child(node, 0);
9545 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
9546 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9547 node = isl_schedule_node_insert_partial_schedule(node, mupa);
9548 node = isl_schedule_node_child(node, 0);
9549 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
9550 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
9551 domain1 = isl_schedule_node_get_domain(node);
9552 id = isl_id_alloc(ctx, "group", NULL);
9553 node = isl_schedule_node_parent(node);
9554 node = isl_schedule_node_group(node, id);
9555 node = isl_schedule_node_child(node, 0);
9556 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
9557 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
9558 domain2 = isl_schedule_node_get_domain(node);
9559 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
9560 if (equal >= 0 && equal)
9561 equal = isl_union_set_is_equal(domain1, domain2);
9562 if (equal >= 0 && equal)
9563 equal = isl_union_map_is_equal(umap1, umap2);
9564 isl_union_map_free(umap1);
9565 isl_union_map_free(umap2);
9566 isl_union_set_free(domain1);
9567 isl_union_set_free(domain2);
9568 isl_union_pw_multi_aff_free(upma1);
9569 isl_union_pw_multi_aff_free(upma2);
9570 isl_schedule_node_free(node);
9572 if (equal < 0)
9573 return -1;
9574 if (!equal)
9575 isl_die(ctx, isl_error_unknown,
9576 "expressions not equal", return -1);
9578 return 0;
9581 /* Check that we can have nested groupings and that the union map
9582 * schedule representation is the same before and after the grouping.
9583 * Note that after the grouping, the union map representation contains
9584 * the domain constraints from the ranges of the expansion nodes,
9585 * while they are missing from the union map representation of
9586 * the tree without expansion nodes.
9588 * Also check that the global expansion is as expected.
9590 static int test_schedule_tree_group_2(isl_ctx *ctx)
9592 int equal, equal_expansion;
9593 const char *str;
9594 isl_id *id;
9595 isl_union_set *uset;
9596 isl_union_map *umap1, *umap2;
9597 isl_union_map *expansion1, *expansion2;
9598 isl_union_set_list *filters;
9599 isl_multi_union_pw_aff *mupa;
9600 isl_schedule *schedule;
9601 isl_schedule_node *node;
9603 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
9604 "S3[i,j] : 0 <= i,j < 10 }";
9605 uset = isl_union_set_read_from_str(ctx, str);
9606 node = isl_schedule_node_from_domain(uset);
9607 node = isl_schedule_node_child(node, 0);
9608 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
9609 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9610 node = isl_schedule_node_insert_partial_schedule(node, mupa);
9611 node = isl_schedule_node_child(node, 0);
9612 str = "{ S1[i,j] }";
9613 uset = isl_union_set_read_from_str(ctx, str);
9614 filters = isl_union_set_list_from_union_set(uset);
9615 str = "{ S2[i,j]; S3[i,j] }";
9616 uset = isl_union_set_read_from_str(ctx, str);
9617 filters = isl_union_set_list_add(filters, uset);
9618 node = isl_schedule_node_insert_sequence(node, filters);
9619 node = isl_schedule_node_child(node, 1);
9620 node = isl_schedule_node_child(node, 0);
9621 str = "{ S2[i,j] }";
9622 uset = isl_union_set_read_from_str(ctx, str);
9623 filters = isl_union_set_list_from_union_set(uset);
9624 str = "{ S3[i,j] }";
9625 uset = isl_union_set_read_from_str(ctx, str);
9626 filters = isl_union_set_list_add(filters, uset);
9627 node = isl_schedule_node_insert_sequence(node, filters);
9629 schedule = isl_schedule_node_get_schedule(node);
9630 umap1 = isl_schedule_get_map(schedule);
9631 uset = isl_schedule_get_domain(schedule);
9632 umap1 = isl_union_map_intersect_domain(umap1, uset);
9633 isl_schedule_free(schedule);
9635 node = isl_schedule_node_parent(node);
9636 node = isl_schedule_node_parent(node);
9637 id = isl_id_alloc(ctx, "group1", NULL);
9638 node = isl_schedule_node_group(node, id);
9639 node = isl_schedule_node_child(node, 1);
9640 node = isl_schedule_node_child(node, 0);
9641 id = isl_id_alloc(ctx, "group2", NULL);
9642 node = isl_schedule_node_group(node, id);
9644 schedule = isl_schedule_node_get_schedule(node);
9645 umap2 = isl_schedule_get_map(schedule);
9646 isl_schedule_free(schedule);
9648 node = isl_schedule_node_root(node);
9649 node = isl_schedule_node_child(node, 0);
9650 expansion1 = isl_schedule_node_get_subtree_expansion(node);
9651 isl_schedule_node_free(node);
9653 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
9654 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
9655 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
9657 expansion2 = isl_union_map_read_from_str(ctx, str);
9659 equal = isl_union_map_is_equal(umap1, umap2);
9660 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
9662 isl_union_map_free(umap1);
9663 isl_union_map_free(umap2);
9664 isl_union_map_free(expansion1);
9665 isl_union_map_free(expansion2);
9667 if (equal < 0 || equal_expansion < 0)
9668 return -1;
9669 if (!equal)
9670 isl_die(ctx, isl_error_unknown,
9671 "expressions not equal", return -1);
9672 if (!equal_expansion)
9673 isl_die(ctx, isl_error_unknown,
9674 "unexpected expansion", return -1);
9676 return 0;
9679 /* Some tests for the isl_schedule_node_group function.
9681 static int test_schedule_tree_group(isl_ctx *ctx)
9683 if (test_schedule_tree_group_1(ctx) < 0)
9684 return -1;
9685 if (test_schedule_tree_group_2(ctx) < 0)
9686 return -1;
9687 return 0;
9690 struct {
9691 const char *set;
9692 const char *dual;
9693 } coef_tests[] = {
9694 { "{ rat: [i] : 0 <= i <= 10 }",
9695 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
9696 { "{ rat: [i] : FALSE }",
9697 "{ rat: coefficients[[cst] -> [a]] }" },
9698 { "{ rat: [i] : }",
9699 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
9702 struct {
9703 const char *set;
9704 const char *dual;
9705 } sol_tests[] = {
9706 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
9707 "{ rat: [i] : 0 <= i <= 10 }" },
9708 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
9709 "{ rat: [i] }" },
9710 { "{ rat: coefficients[[cst] -> [a]] }",
9711 "{ rat: [i] : FALSE }" },
9714 /* Test the basic functionality of isl_basic_set_coefficients and
9715 * isl_basic_set_solutions.
9717 static int test_dual(isl_ctx *ctx)
9719 int i;
9721 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
9722 int equal;
9723 isl_basic_set *bset1, *bset2;
9725 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
9726 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
9727 bset1 = isl_basic_set_coefficients(bset1);
9728 equal = isl_basic_set_is_equal(bset1, bset2);
9729 isl_basic_set_free(bset1);
9730 isl_basic_set_free(bset2);
9731 if (equal < 0)
9732 return -1;
9733 if (!equal)
9734 isl_die(ctx, isl_error_unknown,
9735 "incorrect dual", return -1);
9738 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
9739 int equal;
9740 isl_basic_set *bset1, *bset2;
9742 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
9743 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
9744 bset1 = isl_basic_set_solutions(bset1);
9745 equal = isl_basic_set_is_equal(bset1, bset2);
9746 isl_basic_set_free(bset1);
9747 isl_basic_set_free(bset2);
9748 if (equal < 0)
9749 return -1;
9750 if (!equal)
9751 isl_die(ctx, isl_error_unknown,
9752 "incorrect dual", return -1);
9755 return 0;
9758 struct {
9759 int scale_tile;
9760 int shift_point;
9761 const char *domain;
9762 const char *schedule;
9763 const char *sizes;
9764 const char *tile;
9765 const char *point;
9766 } tile_tests[] = {
9767 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
9768 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9769 "{ [32,32] }",
9770 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
9771 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9773 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
9774 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9775 "{ [32,32] }",
9776 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9777 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9779 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9780 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9781 "{ [32,32] }",
9782 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
9783 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9785 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9786 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9787 "{ [32,32] }",
9788 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9789 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9793 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
9794 * tile the band and then check if the tile and point bands have the
9795 * expected partial schedule.
9797 static int test_tile(isl_ctx *ctx)
9799 int i;
9800 int scale;
9801 int shift;
9803 scale = isl_options_get_tile_scale_tile_loops(ctx);
9804 shift = isl_options_get_tile_shift_point_loops(ctx);
9806 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
9807 int opt;
9808 int equal;
9809 const char *str;
9810 isl_union_set *domain;
9811 isl_multi_union_pw_aff *mupa, *mupa2;
9812 isl_schedule_node *node;
9813 isl_multi_val *sizes;
9815 opt = tile_tests[i].scale_tile;
9816 isl_options_set_tile_scale_tile_loops(ctx, opt);
9817 opt = tile_tests[i].shift_point;
9818 isl_options_set_tile_shift_point_loops(ctx, opt);
9820 str = tile_tests[i].domain;
9821 domain = isl_union_set_read_from_str(ctx, str);
9822 node = isl_schedule_node_from_domain(domain);
9823 node = isl_schedule_node_child(node, 0);
9824 str = tile_tests[i].schedule;
9825 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9826 node = isl_schedule_node_insert_partial_schedule(node, mupa);
9827 str = tile_tests[i].sizes;
9828 sizes = isl_multi_val_read_from_str(ctx, str);
9829 node = isl_schedule_node_band_tile(node, sizes);
9831 str = tile_tests[i].tile;
9832 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9833 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9834 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
9835 isl_multi_union_pw_aff_free(mupa);
9836 isl_multi_union_pw_aff_free(mupa2);
9838 node = isl_schedule_node_child(node, 0);
9840 str = tile_tests[i].point;
9841 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9842 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9843 if (equal >= 0 && equal)
9844 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
9845 mupa2);
9846 isl_multi_union_pw_aff_free(mupa);
9847 isl_multi_union_pw_aff_free(mupa2);
9849 isl_schedule_node_free(node);
9851 if (equal < 0)
9852 return -1;
9853 if (!equal)
9854 isl_die(ctx, isl_error_unknown,
9855 "unexpected result", return -1);
9858 isl_options_set_tile_scale_tile_loops(ctx, scale);
9859 isl_options_set_tile_shift_point_loops(ctx, shift);
9861 return 0;
9864 /* Check that the domain hash of a space is equal to the hash
9865 * of the domain of the space.
9867 static int test_domain_hash(isl_ctx *ctx)
9869 isl_map *map;
9870 isl_space *space;
9871 uint32_t hash1, hash2;
9873 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
9874 space = isl_map_get_space(map);
9875 isl_map_free(map);
9876 hash1 = isl_space_get_domain_hash(space);
9877 space = isl_space_domain(space);
9878 hash2 = isl_space_get_hash(space);
9879 isl_space_free(space);
9881 if (!space)
9882 return -1;
9883 if (hash1 != hash2)
9884 isl_die(ctx, isl_error_unknown,
9885 "domain hash not equal to hash of domain", return -1);
9887 return 0;
9890 /* Check that a universe basic set that is not obviously equal to the universe
9891 * is still recognized as being equal to the universe.
9893 static int test_universe(isl_ctx *ctx)
9895 const char *s;
9896 isl_basic_set *bset;
9897 isl_bool is_univ;
9899 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
9900 bset = isl_basic_set_read_from_str(ctx, s);
9901 is_univ = isl_basic_set_is_universe(bset);
9902 isl_basic_set_free(bset);
9904 if (is_univ < 0)
9905 return -1;
9906 if (!is_univ)
9907 isl_die(ctx, isl_error_unknown,
9908 "not recognized as universe set", return -1);
9910 return 0;
9913 /* Sets for which chambers are computed and checked.
9915 const char *chambers_tests[] = {
9916 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
9917 "z >= 0 and z <= C - y and z <= B - x - y }",
9920 /* Add the domain of "cell" to "cells".
9922 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
9924 isl_basic_set_list **cells = user;
9925 isl_basic_set *dom;
9927 dom = isl_cell_get_domain(cell);
9928 isl_cell_free(cell);
9929 *cells = isl_basic_set_list_add(*cells, dom);
9931 return *cells ? isl_stat_ok : isl_stat_error;
9934 /* Check that the elements of "list" are pairwise disjoint.
9936 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
9938 int i, j;
9939 isl_size n;
9941 n = isl_basic_set_list_n_basic_set(list);
9942 if (n < 0)
9943 return isl_stat_error;
9945 for (i = 0; i < n; ++i) {
9946 isl_basic_set *bset_i;
9948 bset_i = isl_basic_set_list_get_basic_set(list, i);
9949 for (j = i + 1; j < n; ++j) {
9950 isl_basic_set *bset_j;
9951 isl_bool disjoint;
9953 bset_j = isl_basic_set_list_get_basic_set(list, j);
9954 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
9955 isl_basic_set_free(bset_j);
9956 if (!disjoint)
9957 isl_die(isl_basic_set_list_get_ctx(list),
9958 isl_error_unknown, "not disjoint",
9959 break);
9960 if (disjoint < 0 || !disjoint)
9961 break;
9963 isl_basic_set_free(bset_i);
9964 if (j < n)
9965 return isl_stat_error;
9968 return isl_stat_ok;
9971 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
9972 * are pairwise disjoint.
9974 static int test_chambers(isl_ctx *ctx)
9976 int i;
9978 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
9979 isl_basic_set *bset;
9980 isl_vertices *vertices;
9981 isl_basic_set_list *cells;
9982 isl_stat ok;
9984 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
9985 vertices = isl_basic_set_compute_vertices(bset);
9986 cells = isl_basic_set_list_alloc(ctx, 0);
9987 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
9988 &cells) < 0)
9989 cells = isl_basic_set_list_free(cells);
9990 ok = check_pairwise_disjoint(cells);
9991 isl_basic_set_list_free(cells);
9992 isl_vertices_free(vertices);
9993 isl_basic_set_free(bset);
9995 if (ok < 0)
9996 return -1;
9999 return 0;
10002 struct {
10003 const char *name;
10004 int (*fn)(isl_ctx *ctx);
10005 } tests [] = {
10006 { "universe", &test_universe },
10007 { "domain hash", &test_domain_hash },
10008 { "dual", &test_dual },
10009 { "dependence analysis", &test_flow },
10010 { "val", &test_val },
10011 { "compute divs", &test_compute_divs },
10012 { "partial lexmin", &test_partial_lexmin },
10013 { "simplify", &test_simplify },
10014 { "curry", &test_curry },
10015 { "piecewise multi affine expressions", &test_pw_multi_aff },
10016 { "multi piecewise affine expressions", &test_multi_pw_aff },
10017 { "conversion", &test_conversion },
10018 { "list", &test_list },
10019 { "align parameters", &test_align_parameters },
10020 { "drop unused parameters", &test_drop_unused_parameters },
10021 { "preimage", &test_preimage },
10022 { "pullback", &test_pullback },
10023 { "AST", &test_ast },
10024 { "AST build", &test_ast_build },
10025 { "AST generation", &test_ast_gen },
10026 { "eliminate", &test_eliminate },
10027 { "deltas_map", &test_deltas_map },
10028 { "residue class", &test_residue_class },
10029 { "div", &test_div },
10030 { "slice", &test_slice },
10031 { "fixed power", &test_fixed_power },
10032 { "sample", &test_sample },
10033 { "output", &test_output },
10034 { "vertices", &test_vertices },
10035 { "chambers", &test_chambers },
10036 { "fixed", &test_fixed },
10037 { "equal", &test_equal },
10038 { "disjoint", &test_disjoint },
10039 { "product", &test_product },
10040 { "dim_max", &test_dim_max },
10041 { "affine", &test_aff },
10042 { "injective", &test_injective },
10043 { "schedule (whole component)", &test_schedule_whole },
10044 { "schedule (incremental)", &test_schedule_incremental },
10045 { "schedule tree", &test_schedule_tree },
10046 { "schedule tree prefix", &test_schedule_tree_prefix },
10047 { "schedule tree grouping", &test_schedule_tree_group },
10048 { "tile", &test_tile },
10049 { "union_pw", &test_union_pw },
10050 { "locus", &test_locus },
10051 { "eval", &test_eval },
10052 { "parse", &test_parse },
10053 { "single-valued", &test_sv },
10054 { "affine hull", &test_affine_hull },
10055 { "simple_hull", &test_simple_hull },
10056 { "coalesce", &test_coalesce },
10057 { "factorize", &test_factorize },
10058 { "subset", &test_subset },
10059 { "subtract", &test_subtract },
10060 { "intersect", &test_intersect },
10061 { "lexmin", &test_lexmin },
10062 { "min", &test_min },
10063 { "gist", &test_gist },
10064 { "piecewise quasi-polynomials", &test_pwqp },
10065 { "lift", &test_lift },
10066 { "bind parameters", &test_bind },
10067 { "unbind parameters", &test_unbind },
10068 { "bound", &test_bound },
10069 { "get lists", &test_get_list },
10070 { "union", &test_union },
10071 { "split periods", &test_split_periods },
10072 { "lexicographic order", &test_lex },
10073 { "bijectivity", &test_bijective },
10074 { "dataflow analysis", &test_dep },
10075 { "reading", &test_read },
10076 { "bounded", &test_bounded },
10077 { "construction", &test_construction },
10078 { "dimension manipulation", &test_dim },
10079 { "map application", &test_application },
10080 { "convex hull", &test_convex_hull },
10081 { "transitive closure", &test_closure },
10082 { "isl_bool", &test_isl_bool},
10085 int main(int argc, char **argv)
10087 int i;
10088 struct isl_ctx *ctx;
10089 struct isl_options *options;
10091 options = isl_options_new_with_defaults();
10092 assert(options);
10093 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
10095 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
10096 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
10097 printf("%s\n", tests[i].name);
10098 if (tests[i].fn(ctx) < 0)
10099 goto error;
10101 isl_ctx_free(ctx);
10102 return 0;
10103 error:
10104 isl_ctx_free(ctx);
10105 return -1;