isl_input.c: extract out next_signed_value*
[isl.git] / isl_test.c
bloba60868db77258fc52a55b7065492c0bc9ba7a601
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
6 * Copyright 2022 Cerebras Systems
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
17 * and Cerebras Systems, 1237 E Arques Ave, Sunnyvale, CA, USA
20 #include <assert.h>
21 #include <stdio.h>
22 #include <limits.h>
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
25 #include <isl_aff_private.h>
26 #include <isl_space_private.h>
27 #include <isl/id.h>
28 #include <isl/set.h>
29 #include <isl/flow.h>
30 #include <isl_constraint_private.h>
31 #include <isl/polynomial.h>
32 #include <isl/union_set.h>
33 #include <isl/union_map.h>
34 #include <isl_factorization.h>
35 #include <isl/schedule.h>
36 #include <isl/schedule_node.h>
37 #include <isl_options_private.h>
38 #include <isl_vertices_private.h>
39 #include <isl/ast_build.h>
40 #include <isl/val.h>
41 #include <isl/ilp.h>
42 #include <isl_ast_build_expr.h>
43 #include <isl/options.h>
45 #include "isl_srcdir.c"
47 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
49 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
50 char *filename;
51 int length;
52 char *pattern = "%s/test_inputs/%s.%s";
54 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
55 + strlen(suffix) + 1;
56 filename = isl_alloc_array(ctx, char, length);
58 if (!filename)
59 return NULL;
61 sprintf(filename, pattern, srcdir, name, suffix);
63 return filename;
66 void test_parse_map(isl_ctx *ctx, const char *str)
68 isl_map *map;
70 map = isl_map_read_from_str(ctx, str);
71 assert(map);
72 isl_map_free(map);
75 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
77 isl_map *map, *map2;
78 int equal;
80 map = isl_map_read_from_str(ctx, str);
81 map2 = isl_map_read_from_str(ctx, str2);
82 equal = isl_map_is_equal(map, map2);
83 isl_map_free(map);
84 isl_map_free(map2);
86 if (equal < 0)
87 return -1;
88 if (!equal)
89 isl_die(ctx, isl_error_unknown, "maps not equal",
90 return -1);
92 return 0;
95 void test_parse_pwqp(isl_ctx *ctx, const char *str)
97 isl_pw_qpolynomial *pwqp;
99 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
100 assert(pwqp);
101 isl_pw_qpolynomial_free(pwqp);
104 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
106 isl_pw_aff *pwaff;
108 pwaff = isl_pw_aff_read_from_str(ctx, str);
109 assert(pwaff);
110 isl_pw_aff_free(pwaff);
113 /* Check that we can read an isl_multi_val from "str" without errors.
115 static int test_parse_multi_val(isl_ctx *ctx, const char *str)
117 isl_multi_val *mv;
119 mv = isl_multi_val_read_from_str(ctx, str);
120 isl_multi_val_free(mv);
122 return mv ? 0 : -1;
125 /* String descriptions of multi piecewise affine expressions
126 * that are used for testing printing and parsing.
128 static const char *reparse_multi_pw_aff_tests[] = {
129 "{ A[x, y] -> [] : x + y >= 0 }",
130 "{ A[x, y] -> B[] : x + y >= 0 }",
131 "{ A[x, y] -> [x] : x + y >= 0 }",
132 "[N] -> { A[x, y] -> [x] : x + y <= N }",
133 "{ A[x, y] -> [x, y] : x + y >= 0 }",
134 "{ A[x, y] -> [(x : x >= 0), (y : y >= 0)] : x + y >= 0 }",
135 "[N] -> { [] : N >= 0 }",
136 "[N] -> { [] : N >= 0 }",
137 "[N] -> { [N] : N >= 0 }",
138 "[N] -> { [N, N + 1] : N >= 0 }",
139 "[N, M] -> { [(N : N >= 0), (M : M >= 0)] : N + M >= 0 }",
140 "{ [a] -> [b = a] }",
141 "{ [a] -> [b = a] : a >= 0 }",
144 #undef BASE
145 #define BASE multi_pw_aff
147 #include "check_reparse_templ.c"
148 #include "check_reparse_test_templ.c"
150 /* String descriptions that cannot be parsed
151 * as multi piecewise affine expressions.
153 static const char *parse_multi_pw_aff_fail_tests[] = {
154 "{ [a] -> [b] : b = a }",
155 "{ [a] -> [b = a] : b >= 0 }",
158 #include "check_parse_fail_test_templ.c"
160 /* String descriptions of piecewise multi affine expressions
161 * that are used for testing printing and parsing.
163 static const char *reparse_pw_multi_aff_tests[] = {
164 "{ [x] -> [x] }",
165 "{ [x] -> [x % 4] }",
166 "{ [x] -> [x % 4] : x mod 3 = 1 }",
167 "{ [x, x] -> [x % 4] }",
168 "{ [x, x + 1] -> [x % 4] : x mod 3 = 1 }",
169 "{ [x, x mod 2] -> [x % 4] }",
170 "{ [a] -> [a//2] : exists (e0: 8*floor((-a + e0)/8) <= -8 - a + 8e0) }",
173 #undef BASE
174 #define BASE pw_multi_aff
176 #include "check_reparse_templ.c"
177 #include "check_reparse_test_templ.c"
179 /* Test parsing of piecewise multi affine expressions by printing
180 * the expressions and checking that parsing the output results
181 * in the same expression.
182 * Do this for an expression converted from a map with an output
183 * dimension name that is equal to an automatically generated name, and
184 * a set of expressions parsed from strings.
186 static isl_stat test_parse_pma(isl_ctx *ctx)
188 isl_map *map;
189 isl_pw_multi_aff *pma;
191 map = isl_map_read_from_str(ctx, "{ [a, a] -> [i1 = a + 1] }");
192 pma = isl_pw_multi_aff_from_map(map);
193 if (check_reparse_pw_multi_aff(ctx, pma) < 0)
194 return isl_stat_error;
196 if (check_reparse_pw_multi_aff_tests(ctx) < 0)
197 return isl_stat_error;
199 return isl_stat_ok;
202 /* String descriptions that cannot be parsed
203 * as union piecewise multi affine expressions.
205 static const char *parse_union_pw_multi_aff_fail_tests[] = {
206 "{ [a] -> [b] : b = a }",
207 "{ [a] -> [b = a] : b >= 0 }",
210 #undef BASE
211 #define BASE union_pw_multi_aff
213 #include "check_parse_fail_test_templ.c"
215 /* Test parsing of union piecewise multi affine expressions.
217 * In particular, check some cases where parsing is supposed to fail.
219 static isl_stat test_parse_upma(isl_ctx *ctx)
221 if (check_parse_union_pw_multi_aff_fail_tests(ctx) < 0)
222 return isl_stat_error;
224 return isl_stat_ok;
227 /* Test parsing of multi piecewise affine expressions by printing
228 * the expressions and checking that parsing the output results
229 * in the same expression.
230 * Do this for a couple of manually constructed expressions,
231 * an expression converted from a map with an output dimension name
232 * that is equal to an automatically generated name, and
233 * a set of expressions parsed from strings.
235 * Additionally, check some cases where parsing is supposed to fail.
237 static int test_parse_mpa(isl_ctx *ctx)
239 isl_space *space;
240 isl_set *dom;
241 isl_map *map;
242 isl_pw_multi_aff *pma;
243 isl_multi_pw_aff *mpa;
244 isl_stat r;
246 space = isl_space_set_alloc(ctx, 0, 0);
247 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
248 mpa = isl_multi_pw_aff_zero(space);
249 r = check_reparse_multi_pw_aff(ctx, mpa);
250 if (r < 0)
251 return -1;
253 space = isl_space_set_alloc(ctx, 1, 0);
254 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
255 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
256 dom = isl_set_universe(isl_space_params(isl_space_copy(space)));
257 dom = isl_set_lower_bound_si(dom, isl_dim_param, 0, 5);
258 mpa = isl_multi_pw_aff_zero(space);
259 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
260 r = check_reparse_multi_pw_aff(ctx, mpa);
261 if (r < 0)
262 return -1;
264 map = isl_map_read_from_str(ctx, "{ [a, a] -> [i1 = a + 1] }");
265 pma = isl_pw_multi_aff_from_map(map);
266 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
267 if (check_reparse_multi_pw_aff(ctx, mpa) < 0)
268 return -1;
270 if (check_reparse_multi_pw_aff_tests(ctx) < 0)
271 return -1;
272 if (check_parse_multi_pw_aff_fail_tests(ctx) < 0)
273 return -1;
275 return 0;
278 /* String descriptions of multi union piecewise affine expressions
279 * that are used for testing printing and parsing.
281 static const char *reparse_multi_union_pw_aff_tests[] = {
282 "[]",
283 "A[]",
284 "A[B[] -> C[]]",
285 "(A[] : { S[x] : x > 0; T[y] : y >= 0 })",
286 "(A[] : { })",
287 "[N] -> (A[] : { })",
288 "[N] -> (A[] : { : N >= 0 })",
289 "[N] -> (A[] : { S[x] : x > N; T[y] : y >= 0 })",
290 "(A[] : [N] -> { S[x] : x > N; T[y] : y >= 0 })",
291 "A[{ S[x] -> [x + 1]; T[x] -> [x] }]",
292 "(A[{ S[x] -> [x + 1]; T[x] -> [x] }] : "
293 "{ S[x] : x > 0; T[y] : y >= 0 })",
296 #undef BASE
297 #define BASE multi_union_pw_aff
299 #include "check_reparse_templ.c"
300 #include "check_reparse_test_templ.c"
302 /* Test parsing of multi union piecewise affine expressions by printing
303 * the expressions and checking that parsing the output results
304 * in the same expression.
305 * Do this for a couple of manually constructed expressions and
306 * a set of expressions parsed from strings.
308 static int test_parse_mupa(isl_ctx *ctx)
310 isl_space *space;
311 isl_multi_union_pw_aff *mupa;
312 isl_set *dom;
313 isl_union_set *uset;
314 isl_stat r;
316 space = isl_space_set_alloc(ctx, 0, 0);
317 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
318 mupa = isl_multi_union_pw_aff_zero(space);
319 r = check_reparse_multi_union_pw_aff(ctx, mupa);
320 if (r < 0)
321 return -1;
323 space = isl_space_set_alloc(ctx, 1, 0);
324 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
325 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
326 dom = isl_set_universe(space);
327 dom = isl_set_lower_bound_si(dom, isl_dim_param, 0, 5);
328 uset = isl_union_set_from_set(dom);
329 space = isl_space_set_alloc(ctx, 1, 0);
330 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
331 space = isl_space_set_tuple_name(space, isl_dim_set, "B");
332 mupa = isl_multi_union_pw_aff_zero(space);
333 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, uset);
334 r = check_reparse_multi_union_pw_aff(ctx, mupa);
335 if (r < 0)
336 return -1;
338 if (check_reparse_multi_union_pw_aff_tests(ctx) < 0)
339 return -1;
341 return 0;
344 /* Test parsing of multi expressions.
346 static int test_parse_multi(isl_ctx *ctx)
348 if (test_parse_mpa(ctx) < 0)
349 return -1;
350 if (test_parse_mupa(ctx) < 0)
351 return -1;
353 return 0;
356 /* Pairs of binary relation representations that should represent
357 * the same binary relations.
359 struct {
360 const char *map1;
361 const char *map2;
362 } parse_map_equal_tests[] = {
363 { "{ [x,y] : [([x/2]+y)/3] >= 1 }",
364 "{ [x, y] : 2y >= 6 - x }" },
365 { "{ [x,y] : x <= min(y, 2*y+3) }",
366 "{ [x,y] : x <= y, 2*y + 3 }" },
367 { "{ [x,y] : x >= min(y, 2*y+3) }",
368 "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }" },
369 { "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }",
370 "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }" },
371 { "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }",
372 "{ [i,j] -> [min(i,j)] }" },
373 { "{ [i,j] : i != j }",
374 "{ [i,j] : i < j or i > j }" },
375 { "{ [i,j] : (i+1)*2 >= j }",
376 "{ [i, j] : j <= 2 + 2i }" },
377 { "{ [i] -> [i > 0 ? 4 : 5] }",
378 "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }" },
379 { "[N=2,M] -> { [i=[(M+N)/4]] }",
380 "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }" },
381 { "{ [x] : x >= 0 }",
382 "{ [x] : x-0 >= 0 }" },
383 { "{ [i] : ((i > 10)) }",
384 "{ [i] : i >= 11 }" },
385 { "{ [i] -> [0] }",
386 "{ [i] -> [0 * i] }" },
387 { "{ [a] -> [b] : (not false) }",
388 "{ [a] -> [b] : true }" },
389 { "{ [i] : i/2 <= 5 }",
390 "{ [i] : i <= 10 }" },
391 { "{Sym=[n] [i] : i <= n }",
392 "[n] -> { [i] : i <= n }" },
393 { "{ [*] }",
394 "{ [a] }" },
395 { "{ [i] : 2*floor(i/2) = i }",
396 "{ [i] : exists a : i = 2 a }" },
397 { "{ [a] -> [b] : a = 5 implies b = 5 }",
398 "{ [a] -> [b] : a != 5 or b = 5 }" },
399 { "{ [a] -> [a - 1 : a > 0] }",
400 "{ [a] -> [a - 1] : a > 0 }" },
401 { "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
402 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }" },
403 { "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
404 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
405 { "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
406 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
407 { "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
408 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
409 { "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
410 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
411 { "{ [a,b] -> [i,j] : a,b << i,j }",
412 "{ [a,b] -> [i,j] : a < i or (a = i and b < j) }" },
413 { "{ [a,b] -> [i,j] : a,b <<= i,j }",
414 "{ [a,b] -> [i,j] : a < i or (a = i and b <= j) }" },
415 { "{ [a,b] -> [i,j] : a,b >> i,j }",
416 "{ [a,b] -> [i,j] : a > i or (a = i and b > j) }" },
417 { "{ [a,b] -> [i,j] : a,b >>= i,j }",
418 "{ [a,b] -> [i,j] : a > i or (a = i and b >= j) }" },
419 { "{ [n] -> [i] : exists (a, b, c: 8b <= i - 32a and "
420 "8b >= -7 + i - 32 a and b >= 0 and b <= 3 and "
421 "8c < n - 32a and i < n and c >= 0 and "
422 "c <= 3 and c >= -4a) }",
423 "{ [n] -> [i] : 0 <= i < n }" },
424 { "{ [x] -> [] : exists (a, b: 0 <= a <= 1 and 0 <= b <= 3 and "
425 "2b <= x - 8a and 2b >= -1 + x - 8a) }",
426 "{ [x] -> [] : 0 <= x <= 15 }" },
427 { "{ [x] -> [x] : }",
428 "{ [x] -> [x] }" },
429 { "{ [x=4:5] -> [x + 1] }",
430 "{ [x] -> [x + 1] : 4 <= x <= 5 }" },
431 { "{ [x=4:5] -> [x + 1 : x + 1] }",
432 "{ [x=4:5] -> [x + 1] }" },
433 { "{ [x] -> [x - 1 : x + 1] }",
434 "{ [x] -> [y] : x - 1 <= y <= x + 1 }" },
435 { "{ [x=4:] -> [x + 1] }",
436 "{ [x] -> [x + 1] : 4 <= x }" },
437 { "{ [x=:5] -> [x + 1] }",
438 "{ [x] -> [x + 1] : x <= 5 }" },
439 { "{ [x=:] -> [x + 1] }",
440 "{ [x] -> [x + 1] }" },
441 { "{ [:] -> [:] }",
442 "{ [x] -> [y] }" },
443 { "{ [x, x//4] }",
444 "{ [x, floor(x/4)] }" },
445 { "{ [10//4] }",
446 "{ [2] }" },
447 { "[a, b, c, d] -> { [max(a,b,c,d)] }",
448 "[a, b, c, d] -> { [a] : b < a and c < a and d < a; "
449 "[b] : b >= a and c < b and d < b; "
450 "[c] : c >= a and c >= b and d < c; "
451 "[d] : d >= a and d >= b and d >= c }" },
452 { "[a, b, c, d] -> { [min(a,b,c,d)] }",
453 "[a, b, c, d] -> { [a] : b >= a and c >= a and d >= a; "
454 "[b] : b < a and c >= b and d >= b; "
455 "[c] : c < b and c < a and d >= c; "
456 "[d] : d < c and d < b and d < a }" },
459 int test_parse(struct isl_ctx *ctx)
461 int i;
462 isl_map *map, *map2;
463 const char *str, *str2;
465 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
466 return -1;
467 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
468 return -1;
469 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
470 return -1;
471 if (test_parse_multi(ctx) < 0)
472 return -1;
473 if (test_parse_pma(ctx) < 0)
474 return -1;
475 if (test_parse_upma(ctx) < 0)
476 return -1;
478 str = "{ [i] -> [-i] }";
479 map = isl_map_read_from_str(ctx, str);
480 assert(map);
481 isl_map_free(map);
483 str = "{ A[i] -> L[([i/3])] }";
484 map = isl_map_read_from_str(ctx, str);
485 assert(map);
486 isl_map_free(map);
488 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
489 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
490 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
492 for (i = 0; i < ARRAY_SIZE(parse_map_equal_tests); ++i) {
493 str = parse_map_equal_tests[i].map1;
494 str2 = parse_map_equal_tests[i].map2;
495 if (test_parse_map_equal(ctx, str, str2) < 0)
496 return -1;
499 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
500 map = isl_map_read_from_str(ctx, str);
501 str = "{ [new, old] -> [o0, o1] : "
502 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
503 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
504 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
505 map2 = isl_map_read_from_str(ctx, str);
506 assert(isl_map_is_equal(map, map2));
507 isl_map_free(map);
508 isl_map_free(map2);
510 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
511 map = isl_map_read_from_str(ctx, str);
512 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
513 map2 = isl_map_read_from_str(ctx, str);
514 assert(isl_map_is_equal(map, map2));
515 isl_map_free(map);
516 isl_map_free(map2);
518 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
519 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
520 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
521 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
522 test_parse_pwaff(ctx, "{ [] -> [(100)] }");
524 return 0;
527 static int test_read(isl_ctx *ctx)
529 char *filename;
530 FILE *input;
531 isl_basic_set *bset1, *bset2;
532 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
533 int equal;
535 filename = get_filename(ctx, "set", "omega");
536 assert(filename);
537 input = fopen(filename, "r");
538 assert(input);
540 bset1 = isl_basic_set_read_from_file(ctx, input);
541 bset2 = isl_basic_set_read_from_str(ctx, str);
543 equal = isl_basic_set_is_equal(bset1, bset2);
545 isl_basic_set_free(bset1);
546 isl_basic_set_free(bset2);
547 free(filename);
549 fclose(input);
551 if (equal < 0)
552 return -1;
553 if (!equal)
554 isl_die(ctx, isl_error_unknown,
555 "read sets not equal", return -1);
557 return 0;
560 static int test_bounded(isl_ctx *ctx)
562 isl_set *set;
563 isl_bool bounded;
565 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
566 bounded = isl_set_is_bounded(set);
567 isl_set_free(set);
569 if (bounded < 0)
570 return -1;
571 if (!bounded)
572 isl_die(ctx, isl_error_unknown,
573 "set not considered bounded", return -1);
575 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
576 bounded = isl_set_is_bounded(set);
577 assert(!bounded);
578 isl_set_free(set);
580 if (bounded < 0)
581 return -1;
582 if (bounded)
583 isl_die(ctx, isl_error_unknown,
584 "set considered bounded", return -1);
586 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
587 bounded = isl_set_is_bounded(set);
588 isl_set_free(set);
590 if (bounded < 0)
591 return -1;
592 if (bounded)
593 isl_die(ctx, isl_error_unknown,
594 "set considered bounded", return -1);
596 return 0;
599 /* Construct the basic set { [i] : 5 <= i <= N } */
600 static int test_construction_1(isl_ctx *ctx)
602 isl_space *space;
603 isl_local_space *ls;
604 isl_basic_set *bset;
605 isl_constraint *c;
607 space = isl_space_set_alloc(ctx, 1, 1);
608 bset = isl_basic_set_universe(isl_space_copy(space));
609 ls = isl_local_space_from_space(space);
611 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
612 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
613 c = isl_constraint_set_coefficient_si(c, isl_dim_param, 0, 1);
614 bset = isl_basic_set_add_constraint(bset, c);
616 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
617 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
618 c = isl_constraint_set_constant_si(c, -5);
619 bset = isl_basic_set_add_constraint(bset, c);
621 isl_local_space_free(ls);
622 isl_basic_set_free(bset);
624 return 0;
627 /* Construct the basic set { [x] : -100 <= x <= 100 }
628 * using isl_basic_set_{lower,upper}_bound_val and
629 * check that it is equal the same basic set parsed from a string.
631 static int test_construction_2(isl_ctx *ctx)
633 isl_bool equal;
634 isl_val *v;
635 isl_space *space;
636 isl_basic_set *bset1, *bset2;
638 v = isl_val_int_from_si(ctx, 100);
639 space = isl_space_set_alloc(ctx, 0, 1);
640 bset1 = isl_basic_set_universe(space);
641 bset1 = isl_basic_set_upper_bound_val(bset1, isl_dim_set, 0,
642 isl_val_copy(v));
643 bset1 = isl_basic_set_lower_bound_val(bset1, isl_dim_set, 0,
644 isl_val_neg(v));
645 bset2 = isl_basic_set_read_from_str(ctx, "{ [x] : -100 <= x <= 100 }");
646 equal = isl_basic_set_is_equal(bset1, bset2);
647 isl_basic_set_free(bset1);
648 isl_basic_set_free(bset2);
650 if (equal < 0)
651 return -1;
652 if (!equal)
653 isl_die(ctx, isl_error_unknown,
654 "failed construction", return -1);
656 return 0;
659 /* Basic tests for constructing basic sets.
661 static int test_construction(isl_ctx *ctx)
663 if (test_construction_1(ctx) < 0)
664 return -1;
665 if (test_construction_2(ctx) < 0)
666 return -1;
667 return 0;
670 static int test_dim(isl_ctx *ctx)
672 const char *str;
673 isl_map *map1, *map2;
674 int equal;
676 map1 = isl_map_read_from_str(ctx,
677 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
678 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
679 map2 = isl_map_read_from_str(ctx,
680 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
681 equal = isl_map_is_equal(map1, map2);
682 isl_map_free(map2);
684 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
685 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
686 if (equal >= 0 && equal)
687 equal = isl_map_is_equal(map1, map2);
689 isl_map_free(map1);
690 isl_map_free(map2);
692 if (equal < 0)
693 return -1;
694 if (!equal)
695 isl_die(ctx, isl_error_unknown,
696 "unexpected result", return -1);
698 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
699 map1 = isl_map_read_from_str(ctx, str);
700 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
701 map2 = isl_map_read_from_str(ctx, str);
702 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
703 equal = isl_map_is_equal(map1, map2);
704 isl_map_free(map1);
705 isl_map_free(map2);
707 if (equal < 0)
708 return -1;
709 if (!equal)
710 isl_die(ctx, isl_error_unknown,
711 "unexpected result", return -1);
713 return 0;
716 #undef BASE
717 #define BASE multi_val
718 #include "isl_test_plain_equal_templ.c"
720 #undef BASE
721 #define BASE multi_aff
722 #include "isl_test_plain_equal_templ.c"
724 /* Check that "val" is equal to the value described by "str".
725 * If "str" is "NaN", then check for a NaN value explicitly.
727 static isl_stat val_check_equal(__isl_keep isl_val *val, const char *str)
729 isl_bool ok, is_nan;
730 isl_ctx *ctx;
731 isl_val *res;
733 if (!val)
734 return isl_stat_error;
736 ctx = isl_val_get_ctx(val);
737 res = isl_val_read_from_str(ctx, str);
738 is_nan = isl_val_is_nan(res);
739 if (is_nan < 0)
740 ok = isl_bool_error;
741 else if (is_nan)
742 ok = isl_val_is_nan(val);
743 else
744 ok = isl_val_eq(val, res);
745 isl_val_free(res);
746 if (ok < 0)
747 return isl_stat_error;
748 if (!ok)
749 isl_die(ctx, isl_error_unknown,
750 "unexpected result", return isl_stat_error);
751 return isl_stat_ok;
754 struct {
755 __isl_give isl_val *(*op)(__isl_take isl_val *v);
756 const char *arg;
757 const char *res;
758 } val_un_tests[] = {
759 { &isl_val_neg, "0", "0" },
760 { &isl_val_abs, "0", "0" },
761 { &isl_val_pow2, "0", "1" },
762 { &isl_val_floor, "0", "0" },
763 { &isl_val_ceil, "0", "0" },
764 { &isl_val_neg, "1", "-1" },
765 { &isl_val_neg, "-1", "1" },
766 { &isl_val_neg, "1/2", "-1/2" },
767 { &isl_val_neg, "-1/2", "1/2" },
768 { &isl_val_neg, "infty", "-infty" },
769 { &isl_val_neg, "-infty", "infty" },
770 { &isl_val_neg, "NaN", "NaN" },
771 { &isl_val_abs, "1", "1" },
772 { &isl_val_abs, "-1", "1" },
773 { &isl_val_abs, "1/2", "1/2" },
774 { &isl_val_abs, "-1/2", "1/2" },
775 { &isl_val_abs, "infty", "infty" },
776 { &isl_val_abs, "-infty", "infty" },
777 { &isl_val_abs, "NaN", "NaN" },
778 { &isl_val_floor, "1", "1" },
779 { &isl_val_floor, "-1", "-1" },
780 { &isl_val_floor, "1/2", "0" },
781 { &isl_val_floor, "-1/2", "-1" },
782 { &isl_val_floor, "infty", "infty" },
783 { &isl_val_floor, "-infty", "-infty" },
784 { &isl_val_floor, "NaN", "NaN" },
785 { &isl_val_ceil, "1", "1" },
786 { &isl_val_ceil, "-1", "-1" },
787 { &isl_val_ceil, "1/2", "1" },
788 { &isl_val_ceil, "-1/2", "0" },
789 { &isl_val_ceil, "infty", "infty" },
790 { &isl_val_ceil, "-infty", "-infty" },
791 { &isl_val_ceil, "NaN", "NaN" },
792 { &isl_val_pow2, "-3", "1/8" },
793 { &isl_val_pow2, "-1", "1/2" },
794 { &isl_val_pow2, "1", "2" },
795 { &isl_val_pow2, "2", "4" },
796 { &isl_val_pow2, "3", "8" },
797 { &isl_val_inv, "1", "1" },
798 { &isl_val_inv, "2", "1/2" },
799 { &isl_val_inv, "1/2", "2" },
800 { &isl_val_inv, "-2", "-1/2" },
801 { &isl_val_inv, "-1/2", "-2" },
802 { &isl_val_inv, "0", "NaN" },
803 { &isl_val_inv, "NaN", "NaN" },
804 { &isl_val_inv, "infty", "0" },
805 { &isl_val_inv, "-infty", "0" },
808 /* Perform some basic tests of unary operations on isl_val objects.
810 static int test_un_val(isl_ctx *ctx)
812 int i;
813 isl_val *v;
814 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
816 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
817 isl_stat r;
819 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
820 fn = val_un_tests[i].op;
821 v = fn(v);
822 r = val_check_equal(v, val_un_tests[i].res);
823 isl_val_free(v);
824 if (r < 0)
825 return -1;
828 return 0;
831 struct {
832 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
833 __isl_take isl_val *v2);
834 } val_bin_op[] = {
835 ['+'] = { &isl_val_add },
836 ['-'] = { &isl_val_sub },
837 ['*'] = { &isl_val_mul },
838 ['/'] = { &isl_val_div },
839 ['g'] = { &isl_val_gcd },
840 ['m'] = { &isl_val_min },
841 ['M'] = { &isl_val_max },
844 struct {
845 const char *arg1;
846 unsigned char op;
847 const char *arg2;
848 const char *res;
849 } val_bin_tests[] = {
850 { "0", '+', "0", "0" },
851 { "1", '+', "0", "1" },
852 { "1", '+', "1", "2" },
853 { "1", '-', "1", "0" },
854 { "1", '*', "1", "1" },
855 { "1", '/', "1", "1" },
856 { "2", '*', "3", "6" },
857 { "2", '*', "1/2", "1" },
858 { "2", '*', "1/3", "2/3" },
859 { "2/3", '*', "3/5", "2/5" },
860 { "2/3", '*', "7/5", "14/15" },
861 { "2", '/', "1/2", "4" },
862 { "-2", '/', "-1/2", "4" },
863 { "-2", '/', "1/2", "-4" },
864 { "2", '/', "-1/2", "-4" },
865 { "2", '/', "2", "1" },
866 { "2", '/', "3", "2/3" },
867 { "2/3", '/', "5/3", "2/5" },
868 { "2/3", '/', "5/7", "14/15" },
869 { "0", '/', "0", "NaN" },
870 { "42", '/', "0", "NaN" },
871 { "-42", '/', "0", "NaN" },
872 { "infty", '/', "0", "NaN" },
873 { "-infty", '/', "0", "NaN" },
874 { "NaN", '/', "0", "NaN" },
875 { "0", '/', "NaN", "NaN" },
876 { "42", '/', "NaN", "NaN" },
877 { "-42", '/', "NaN", "NaN" },
878 { "infty", '/', "NaN", "NaN" },
879 { "-infty", '/', "NaN", "NaN" },
880 { "NaN", '/', "NaN", "NaN" },
881 { "0", '/', "infty", "0" },
882 { "42", '/', "infty", "0" },
883 { "-42", '/', "infty", "0" },
884 { "infty", '/', "infty", "NaN" },
885 { "-infty", '/', "infty", "NaN" },
886 { "NaN", '/', "infty", "NaN" },
887 { "0", '/', "-infty", "0" },
888 { "42", '/', "-infty", "0" },
889 { "-42", '/', "-infty", "0" },
890 { "infty", '/', "-infty", "NaN" },
891 { "-infty", '/', "-infty", "NaN" },
892 { "NaN", '/', "-infty", "NaN" },
893 { "1", '-', "1/3", "2/3" },
894 { "1/3", '+', "1/2", "5/6" },
895 { "1/2", '+', "1/2", "1" },
896 { "3/4", '-', "1/4", "1/2" },
897 { "1/2", '-', "1/3", "1/6" },
898 { "infty", '+', "42", "infty" },
899 { "infty", '+', "infty", "infty" },
900 { "42", '+', "infty", "infty" },
901 { "infty", '-', "infty", "NaN" },
902 { "infty", '*', "infty", "infty" },
903 { "infty", '*', "-infty", "-infty" },
904 { "-infty", '*', "infty", "-infty" },
905 { "-infty", '*', "-infty", "infty" },
906 { "0", '*', "infty", "NaN" },
907 { "1", '*', "infty", "infty" },
908 { "infty", '*', "0", "NaN" },
909 { "infty", '*', "42", "infty" },
910 { "42", '-', "infty", "-infty" },
911 { "infty", '+', "-infty", "NaN" },
912 { "4", 'g', "6", "2" },
913 { "5", 'g', "6", "1" },
914 { "42", 'm', "3", "3" },
915 { "42", 'M', "3", "42" },
916 { "3", 'm', "42", "3" },
917 { "3", 'M', "42", "42" },
918 { "42", 'm', "infty", "42" },
919 { "42", 'M', "infty", "infty" },
920 { "42", 'm', "-infty", "-infty" },
921 { "42", 'M', "-infty", "42" },
922 { "42", 'm', "NaN", "NaN" },
923 { "42", 'M', "NaN", "NaN" },
924 { "infty", 'm', "-infty", "-infty" },
925 { "infty", 'M', "-infty", "infty" },
928 /* Perform some basic tests of binary operations on isl_val objects.
930 static int test_bin_val(isl_ctx *ctx)
932 int i;
933 isl_val *v1, *v2, *res;
934 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
935 __isl_take isl_val *v2);
936 int ok;
938 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
939 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
940 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
941 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
942 fn = val_bin_op[val_bin_tests[i].op].fn;
943 v1 = fn(v1, v2);
944 if (isl_val_is_nan(res))
945 ok = isl_val_is_nan(v1);
946 else
947 ok = isl_val_eq(v1, res);
948 isl_val_free(v1);
949 isl_val_free(res);
950 if (ok < 0)
951 return -1;
952 if (!ok)
953 isl_die(ctx, isl_error_unknown,
954 "unexpected result", return -1);
957 return 0;
960 /* Perform some basic tests on isl_val objects.
962 static int test_val(isl_ctx *ctx)
964 if (test_un_val(ctx) < 0)
965 return -1;
966 if (test_bin_val(ctx) < 0)
967 return -1;
968 return 0;
971 /* Sets described using existentially quantified variables that
972 * can also be described without.
974 static const char *elimination_tests[] = {
975 "{ [i,j] : 2 * [i/2] + 3 * [j/4] <= 10 and 2 i = j }",
976 "{ [m, w] : exists a : w - 2m - 5 <= 3a <= m - 2w }",
977 "{ [m, w] : exists a : w >= 0 and a < m and -1 + w <= a <= 2m - w }",
980 /* Check that redundant existentially quantified variables are
981 * getting removed.
983 static int test_elimination(isl_ctx *ctx)
985 int i;
986 isl_size n;
987 isl_basic_set *bset;
989 for (i = 0; i < ARRAY_SIZE(elimination_tests); ++i) {
990 bset = isl_basic_set_read_from_str(ctx, elimination_tests[i]);
991 n = isl_basic_set_dim(bset, isl_dim_div);
992 isl_basic_set_free(bset);
993 if (n < 0)
994 return -1;
995 if (n != 0)
996 isl_die(ctx, isl_error_unknown,
997 "expecting no existentials", return -1);
1000 return 0;
1003 static int test_div(isl_ctx *ctx)
1005 const char *str;
1006 int empty;
1007 isl_space *space;
1008 isl_set *set;
1009 isl_local_space *ls;
1010 struct isl_basic_set *bset;
1011 struct isl_constraint *c;
1013 /* test 1 */
1014 space = isl_space_set_alloc(ctx, 0, 3);
1015 bset = isl_basic_set_universe(isl_space_copy(space));
1016 ls = isl_local_space_from_space(space);
1018 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1019 c = isl_constraint_set_constant_si(c, -1);
1020 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1021 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1022 bset = isl_basic_set_add_constraint(bset, c);
1024 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1025 c = isl_constraint_set_constant_si(c, 1);
1026 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1027 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
1028 bset = isl_basic_set_add_constraint(bset, c);
1030 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1032 assert(bset && bset->n_div == 1);
1033 isl_local_space_free(ls);
1034 isl_basic_set_free(bset);
1036 /* test 2 */
1037 space = isl_space_set_alloc(ctx, 0, 3);
1038 bset = isl_basic_set_universe(isl_space_copy(space));
1039 ls = isl_local_space_from_space(space);
1041 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1042 c = isl_constraint_set_constant_si(c, 1);
1043 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1044 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1045 bset = isl_basic_set_add_constraint(bset, c);
1047 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1048 c = isl_constraint_set_constant_si(c, -1);
1049 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1050 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
1051 bset = isl_basic_set_add_constraint(bset, c);
1053 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1055 assert(bset && bset->n_div == 1);
1056 isl_local_space_free(ls);
1057 isl_basic_set_free(bset);
1059 /* test 3 */
1060 space = isl_space_set_alloc(ctx, 0, 3);
1061 bset = isl_basic_set_universe(isl_space_copy(space));
1062 ls = isl_local_space_from_space(space);
1064 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1065 c = isl_constraint_set_constant_si(c, 1);
1066 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1067 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1068 bset = isl_basic_set_add_constraint(bset, c);
1070 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1071 c = isl_constraint_set_constant_si(c, -3);
1072 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1073 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 4);
1074 bset = isl_basic_set_add_constraint(bset, c);
1076 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1078 assert(bset && bset->n_div == 1);
1079 isl_local_space_free(ls);
1080 isl_basic_set_free(bset);
1082 /* test 4 */
1083 space = isl_space_set_alloc(ctx, 0, 3);
1084 bset = isl_basic_set_universe(isl_space_copy(space));
1085 ls = isl_local_space_from_space(space);
1087 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1088 c = isl_constraint_set_constant_si(c, 2);
1089 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1090 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1091 bset = isl_basic_set_add_constraint(bset, c);
1093 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1094 c = isl_constraint_set_constant_si(c, -1);
1095 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1096 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
1097 bset = isl_basic_set_add_constraint(bset, c);
1099 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1101 assert(isl_basic_set_is_empty(bset));
1102 isl_local_space_free(ls);
1103 isl_basic_set_free(bset);
1105 /* test 5 */
1106 space = isl_space_set_alloc(ctx, 0, 3);
1107 bset = isl_basic_set_universe(isl_space_copy(space));
1108 ls = isl_local_space_from_space(space);
1110 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1111 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1112 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
1113 bset = isl_basic_set_add_constraint(bset, c);
1115 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1116 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1117 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1118 bset = isl_basic_set_add_constraint(bset, c);
1120 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1122 assert(bset && bset->n_div == 0);
1123 isl_basic_set_free(bset);
1124 isl_local_space_free(ls);
1126 /* test 6 */
1127 space = isl_space_set_alloc(ctx, 0, 3);
1128 bset = isl_basic_set_universe(isl_space_copy(space));
1129 ls = isl_local_space_from_space(space);
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, 2, 6);
1134 bset = isl_basic_set_add_constraint(bset, c);
1136 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1137 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1138 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1139 bset = isl_basic_set_add_constraint(bset, c);
1141 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1143 assert(bset && bset->n_div == 1);
1144 isl_basic_set_free(bset);
1145 isl_local_space_free(ls);
1147 /* test 7 */
1148 /* This test is a bit tricky. We set up an equality
1149 * a + 3b + 3c = 6 e0
1150 * Normalization of divs creates _two_ divs
1151 * a = 3 e0
1152 * c - b - e0 = 2 e1
1153 * Afterwards e0 is removed again because it has coefficient -1
1154 * and we end up with the original equality and div again.
1155 * Perhaps we can avoid the introduction of this temporary div.
1157 space = isl_space_set_alloc(ctx, 0, 4);
1158 bset = isl_basic_set_universe(isl_space_copy(space));
1159 ls = isl_local_space_from_space(space);
1161 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1162 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1163 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1164 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -3);
1165 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 6);
1166 bset = isl_basic_set_add_constraint(bset, c);
1168 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
1170 /* Test disabled for now */
1172 assert(bset && bset->n_div == 1);
1174 isl_local_space_free(ls);
1175 isl_basic_set_free(bset);
1177 /* test 8 */
1178 space = isl_space_set_alloc(ctx, 0, 5);
1179 bset = isl_basic_set_universe(isl_space_copy(space));
1180 ls = isl_local_space_from_space(space);
1182 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1183 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1184 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1185 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, -3);
1186 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 4, 6);
1187 bset = isl_basic_set_add_constraint(bset, c);
1189 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1190 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1191 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 1);
1192 c = isl_constraint_set_constant_si(c, 1);
1193 bset = isl_basic_set_add_constraint(bset, c);
1195 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
1197 /* Test disabled for now */
1199 assert(bset && bset->n_div == 1);
1201 isl_local_space_free(ls);
1202 isl_basic_set_free(bset);
1204 /* test 9 */
1205 space = isl_space_set_alloc(ctx, 0, 4);
1206 bset = isl_basic_set_universe(isl_space_copy(space));
1207 ls = isl_local_space_from_space(space);
1209 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1210 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1211 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -1);
1212 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1213 bset = isl_basic_set_add_constraint(bset, c);
1215 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1216 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1217 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 3);
1218 c = isl_constraint_set_constant_si(c, 2);
1219 bset = isl_basic_set_add_constraint(bset, c);
1221 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
1223 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1225 assert(!isl_basic_set_is_empty(bset));
1227 isl_local_space_free(ls);
1228 isl_basic_set_free(bset);
1230 /* test 10 */
1231 space = isl_space_set_alloc(ctx, 0, 3);
1232 bset = isl_basic_set_universe(isl_space_copy(space));
1233 ls = isl_local_space_from_space(space);
1235 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1236 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1237 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1238 bset = isl_basic_set_add_constraint(bset, c);
1240 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1242 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1244 isl_local_space_free(ls);
1245 isl_basic_set_free(bset);
1247 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1248 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
1249 set = isl_set_read_from_str(ctx, str);
1250 set = isl_set_compute_divs(set);
1251 isl_set_free(set);
1252 if (!set)
1253 return -1;
1255 if (test_elimination(ctx) < 0)
1256 return -1;
1258 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
1259 set = isl_set_read_from_str(ctx, str);
1260 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
1261 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
1262 empty = isl_set_is_empty(set);
1263 isl_set_free(set);
1264 if (empty < 0)
1265 return -1;
1266 if (!empty)
1267 isl_die(ctx, isl_error_unknown,
1268 "result not as accurate as expected", return -1);
1270 return 0;
1273 void test_application_case(struct isl_ctx *ctx, const char *name)
1275 char *filename;
1276 FILE *input;
1277 struct isl_basic_set *bset1, *bset2;
1278 struct isl_basic_map *bmap;
1280 filename = get_filename(ctx, name, "omega");
1281 assert(filename);
1282 input = fopen(filename, "r");
1283 assert(input);
1285 bset1 = isl_basic_set_read_from_file(ctx, input);
1286 bmap = isl_basic_map_read_from_file(ctx, input);
1288 bset1 = isl_basic_set_apply(bset1, bmap);
1290 bset2 = isl_basic_set_read_from_file(ctx, input);
1292 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1294 isl_basic_set_free(bset1);
1295 isl_basic_set_free(bset2);
1296 free(filename);
1298 fclose(input);
1301 static int test_application(isl_ctx *ctx)
1303 test_application_case(ctx, "application");
1304 test_application_case(ctx, "application2");
1306 return 0;
1309 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1311 char *filename;
1312 FILE *input;
1313 struct isl_basic_set *bset1, *bset2;
1315 filename = get_filename(ctx, name, "polylib");
1316 assert(filename);
1317 input = fopen(filename, "r");
1318 assert(input);
1320 bset1 = isl_basic_set_read_from_file(ctx, input);
1321 bset2 = isl_basic_set_read_from_file(ctx, input);
1323 bset1 = isl_basic_set_affine_hull(bset1);
1325 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1327 isl_basic_set_free(bset1);
1328 isl_basic_set_free(bset2);
1329 free(filename);
1331 fclose(input);
1334 /* Pairs of sets and the corresponding expected results of
1335 * isl_basic_set_recession_cone.
1337 struct {
1338 const char *set;
1339 const char *cone;
1340 } recession_cone_tests[] = {
1341 { "{ [i] : 0 <= i <= 10 }", "{ [0] }" },
1342 { "{ [i] : 0 <= i }", "{ [i] : 0 <= i }" },
1343 { "{ [i] : i <= 10 }", "{ [i] : i <= 0 }" },
1344 { "{ [i] : false }", "{ [i] : false }" },
1347 /* Perform some basic isl_basic_set_recession_cone tests.
1349 static int test_recession_cone(struct isl_ctx *ctx)
1351 int i;
1353 for (i = 0; i < ARRAY_SIZE(recession_cone_tests); ++i) {
1354 const char *str;
1355 isl_basic_set *bset;
1356 isl_basic_set *cone, *expected;
1357 isl_bool equal;
1359 str = recession_cone_tests[i].set;
1360 bset = isl_basic_set_read_from_str(ctx, str);
1361 str = recession_cone_tests[i].cone;
1362 expected = isl_basic_set_read_from_str(ctx, str);
1363 cone = isl_basic_set_recession_cone(bset);
1364 equal = isl_basic_set_is_equal(cone, expected);
1365 isl_basic_set_free(cone);
1366 isl_basic_set_free(expected);
1367 if (equal < 0)
1368 return -1;
1369 if (!equal)
1370 isl_die(ctx, isl_error_unknown, "unexpected cone",
1371 return -1);
1374 return 0;
1377 int test_affine_hull(struct isl_ctx *ctx)
1379 const char *str;
1380 isl_set *set;
1381 isl_basic_set *bset, *bset2;
1382 isl_size n;
1383 isl_bool subset;
1385 test_affine_hull_case(ctx, "affine2");
1386 test_affine_hull_case(ctx, "affine");
1387 test_affine_hull_case(ctx, "affine3");
1389 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1390 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1391 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1392 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1393 set = isl_set_read_from_str(ctx, str);
1394 bset = isl_set_affine_hull(set);
1395 n = isl_basic_set_dim(bset, isl_dim_div);
1396 isl_basic_set_free(bset);
1397 if (n < 0)
1398 return -1;
1399 if (n != 0)
1400 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1401 return -1);
1403 /* Check that isl_map_affine_hull is not confused by
1404 * the reordering of divs in isl_map_align_divs.
1406 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1407 "32e0 = b and 32e1 = c); "
1408 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1409 set = isl_set_read_from_str(ctx, str);
1410 bset = isl_set_affine_hull(set);
1411 isl_basic_set_free(bset);
1412 if (!bset)
1413 return -1;
1415 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1416 "32e2 = 31 + 31e0 }";
1417 set = isl_set_read_from_str(ctx, str);
1418 bset = isl_set_affine_hull(set);
1419 str = "{ [a] : exists e : a = 32 e }";
1420 bset2 = isl_basic_set_read_from_str(ctx, str);
1421 subset = isl_basic_set_is_subset(bset, bset2);
1422 isl_basic_set_free(bset);
1423 isl_basic_set_free(bset2);
1424 if (subset < 0)
1425 return -1;
1426 if (!subset)
1427 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1428 return -1);
1430 return 0;
1433 /* Test a special case of isl_set_plain_unshifted_simple_hull
1434 * where older versions of isl would include a redundant constraint
1435 * in the result.
1436 * Check that the result does not have any constraints.
1438 static isl_stat test_plain_unshifted_simple_hull_special(isl_ctx *ctx)
1440 const char *str;
1441 isl_bool is_universe;
1442 isl_set *set;
1443 isl_basic_set *bset;
1445 str = "{[x, y] : x = 0 or 2*((x+y)//2) <= y + 2 }";
1446 set = isl_set_read_from_str(ctx, str);
1447 bset = isl_set_plain_unshifted_simple_hull(set);
1448 is_universe = isl_basic_set_plain_is_universe(bset);
1449 isl_basic_set_free(bset);
1451 if (is_universe < 0)
1452 return isl_stat_error;
1453 if (!is_universe)
1454 isl_die(ctx, isl_error_unknown,
1455 "hull should not have any constraints",
1456 return isl_stat_error);
1458 return isl_stat_ok;
1461 /* Inputs for simple hull tests, consisting of
1462 * the specific simple hull function, the input set and the expected result.
1464 struct {
1465 __isl_give isl_basic_set *(*fn)(__isl_take isl_set *set);
1466 const char *set;
1467 const char *hull;
1468 } simple_hull_tests[] = {
1469 { &isl_set_plain_unshifted_simple_hull,
1470 "{ [i,j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1471 "{ [i,j] : i >= 1 }" },
1472 { &isl_set_plain_unshifted_simple_hull,
1473 "{ [n,i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1474 "(j mod 4 = 2 and k mod 6 = n) }",
1475 "{ [n,i,j,k] : j mod 4 = 2 }" },
1476 { &isl_set_unshifted_simple_hull,
1477 "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1478 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1479 { &isl_set_simple_hull,
1480 "{ [a, b] : b <= 0 and "
1481 "2*floor((-2*floor((b)/2))/5) >= a - floor((b)/2); "
1482 "[a, b] : a mod 2 = 0 }",
1483 "{ [a, b] }" },
1486 /* Basic tests for various simple hull functions.
1488 static int test_various_simple_hull(isl_ctx *ctx)
1490 int i;
1491 isl_set *set;
1492 isl_basic_set *hull, *expected;
1493 isl_bool equal;
1495 for (i = 0; i < ARRAY_SIZE(simple_hull_tests); ++i) {
1496 const char *str;
1497 str = simple_hull_tests[i].set;
1498 set = isl_set_read_from_str(ctx, str);
1499 str = simple_hull_tests[i].hull;
1500 expected = isl_basic_set_read_from_str(ctx, str);
1501 hull = simple_hull_tests[i].fn(set);
1502 equal = isl_basic_set_is_equal(hull, expected);
1503 isl_basic_set_free(hull);
1504 isl_basic_set_free(expected);
1505 if (equal < 0)
1506 return -1;
1507 if (!equal)
1508 isl_die(ctx, isl_error_unknown, "unexpected hull",
1509 return -1);
1512 return 0;
1515 static int test_simple_hull(struct isl_ctx *ctx)
1517 const char *str;
1518 isl_set *set;
1519 isl_basic_set *bset;
1520 isl_bool is_empty;
1522 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1523 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1524 set = isl_set_read_from_str(ctx, str);
1525 bset = isl_set_simple_hull(set);
1526 is_empty = isl_basic_set_is_empty(bset);
1527 isl_basic_set_free(bset);
1529 if (is_empty == isl_bool_error)
1530 return -1;
1532 if (is_empty == isl_bool_false)
1533 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1534 return -1);
1536 if (test_plain_unshifted_simple_hull_special(ctx) < 0)
1537 return -1;
1538 if (test_various_simple_hull(ctx) < 0)
1539 return -1;
1541 return 0;
1544 /* Inputs for isl_set_get_simple_fixed_box_hull tests.
1545 * "set" is the input set.
1546 * "offset" is the expected box offset.
1547 * "size" is the expected box size.
1549 static struct {
1550 const char *set;
1551 const char *offset;
1552 const char *size;
1553 } box_hull_tests[] = {
1554 { "{ S[x, y] : 0 <= x, y < 10 }", "{ S[0, 0] }", "{ S[10, 10] }" },
1555 { "[N] -> { S[x, y] : N <= x, y < N + 10 }",
1556 "[N] -> { S[N, N] }", "{ S[10, 10] }" },
1557 { "{ S[x, y] : 0 <= x + y, x - y < 10 }",
1558 "{ S[0, -4] }", "{ S[10, 9] }" },
1559 { "{ [i=0:10] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1560 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }",
1561 "{ [3] }", "{ [8] }" },
1562 { "[N] -> { [w = 0:17] : exists (e0: w < 2N and "
1563 "-1 + w <= e0 <= w and 2e0 >= N + w and w <= 2e0 <= 15 + w) }",
1564 "[N] -> { [N] }", "{ [9] }" },
1567 /* Perform basic isl_set_get_simple_fixed_box_hull tests.
1569 static int test_box_hull(struct isl_ctx *ctx)
1571 int i;
1573 for (i = 0; i < ARRAY_SIZE(box_hull_tests); ++i) {
1574 const char *str;
1575 isl_stat r;
1576 isl_set *set;
1577 isl_multi_aff *offset;
1578 isl_multi_val *size;
1579 isl_fixed_box *box;
1581 set = isl_set_read_from_str(ctx, box_hull_tests[i].set);
1582 box = isl_set_get_simple_fixed_box_hull(set);
1583 offset = isl_fixed_box_get_offset(box);
1584 size = isl_fixed_box_get_size(box);
1585 str = box_hull_tests[i].offset;
1586 r = multi_aff_check_plain_equal(offset, str);
1587 str = box_hull_tests[i].size;
1588 if (r >= 0)
1589 r = multi_val_check_plain_equal(size, str);
1590 isl_multi_aff_free(offset);
1591 isl_multi_val_free(size);
1592 isl_fixed_box_free(box);
1593 isl_set_free(set);
1594 if (r < 0)
1595 return -1;
1598 return 0;
1601 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1603 char *filename;
1604 FILE *input;
1605 struct isl_basic_set *bset1, *bset2;
1606 struct isl_set *set;
1608 filename = get_filename(ctx, name, "polylib");
1609 assert(filename);
1610 input = fopen(filename, "r");
1611 assert(input);
1613 bset1 = isl_basic_set_read_from_file(ctx, input);
1614 bset2 = isl_basic_set_read_from_file(ctx, input);
1616 set = isl_basic_set_union(bset1, bset2);
1617 bset1 = isl_set_convex_hull(set);
1619 bset2 = isl_basic_set_read_from_file(ctx, input);
1621 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1623 isl_basic_set_free(bset1);
1624 isl_basic_set_free(bset2);
1625 free(filename);
1627 fclose(input);
1630 struct {
1631 const char *set;
1632 const char *hull;
1633 } convex_hull_tests[] = {
1634 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1635 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1636 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1637 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1638 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1639 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1640 "i2 <= 5 and i2 >= 4; "
1641 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1642 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1643 "i2 <= 5 + i0 and i2 >= i0 }" },
1644 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1645 "{ [x, y] : 1 = 0 }" },
1646 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1647 "[x, y, 0] : x >= 0 and y < 0 }",
1648 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1649 { "{ [a, b, c] : a <= 1 and -a < b <= 1 and 0 <= c <= 2 - a - b and "
1650 "c <= a; "
1651 "[0, 2, 0]; [3, 1, 0] }",
1652 "{ [a, b, c] : b > -a and 2b >= -1 + a and 0 <= c <= a and "
1653 "5c <= 6 - a - 3b }" },
1656 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1658 int i;
1659 int orig_convex = ctx->opt->convex;
1660 ctx->opt->convex = convex;
1662 test_convex_hull_case(ctx, "convex0");
1663 test_convex_hull_case(ctx, "convex1");
1664 test_convex_hull_case(ctx, "convex2");
1665 test_convex_hull_case(ctx, "convex3");
1666 test_convex_hull_case(ctx, "convex4");
1667 test_convex_hull_case(ctx, "convex5");
1668 test_convex_hull_case(ctx, "convex6");
1669 test_convex_hull_case(ctx, "convex7");
1670 test_convex_hull_case(ctx, "convex8");
1671 test_convex_hull_case(ctx, "convex9");
1672 test_convex_hull_case(ctx, "convex10");
1673 test_convex_hull_case(ctx, "convex11");
1674 test_convex_hull_case(ctx, "convex12");
1675 test_convex_hull_case(ctx, "convex13");
1676 test_convex_hull_case(ctx, "convex14");
1677 test_convex_hull_case(ctx, "convex15");
1679 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1680 isl_set *set1, *set2;
1681 int equal;
1683 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1684 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1685 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1686 equal = isl_set_is_equal(set1, set2);
1687 isl_set_free(set1);
1688 isl_set_free(set2);
1690 if (equal < 0)
1691 return -1;
1692 if (!equal)
1693 isl_die(ctx, isl_error_unknown,
1694 "unexpected convex hull", return -1);
1697 ctx->opt->convex = orig_convex;
1699 return 0;
1702 static int test_convex_hull(isl_ctx *ctx)
1704 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1705 return -1;
1706 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1707 return -1;
1708 return 0;
1711 /* Check that computing the gist of "map" with respect to "context"
1712 * does not make any copy of "map" get marked empty.
1713 * Earlier versions of isl would end up doing that.
1715 static isl_stat test_gist_empty_pair(isl_ctx *ctx, const char *map,
1716 const char *context)
1718 isl_map *m1, *m2, *m3;
1719 isl_bool empty_before, empty_after;
1721 m1 = isl_map_read_from_str(ctx, map);
1722 m2 = isl_map_read_from_str(ctx, context);
1723 m3 = isl_map_copy(m1);
1724 empty_before = isl_map_is_empty(m3);
1725 m1 = isl_map_gist(m1, m2);
1726 empty_after = isl_map_is_empty(m3);
1727 isl_map_free(m1);
1728 isl_map_free(m3);
1730 if (empty_before < 0 || empty_after < 0)
1731 return isl_stat_error;
1732 if (empty_before)
1733 isl_die(ctx, isl_error_unknown, "map should not be empty",
1734 return isl_stat_error);
1735 if (empty_after)
1736 isl_die(ctx, isl_error_unknown, "map should still not be empty",
1737 return isl_stat_error);
1739 return isl_stat_ok;
1742 /* Check that computing a gist does not make any copy of the input
1743 * get marked empty.
1744 * Earlier versions of isl would end up doing that on some pairs of inputs.
1746 static isl_stat test_gist_empty(isl_ctx *ctx)
1748 const char *map, *context;
1750 map = "{ [] -> [a, b, c] : 2b = 1 + a }";
1751 context = "{ [] -> [a, b, c] : 2c = 2 + a }";
1752 if (test_gist_empty_pair(ctx, map, context) < 0)
1753 return isl_stat_error;
1754 map = "{ [] -> [0, 0] }";
1755 context = "{ [] -> [a, b] : a > b }";
1756 if (test_gist_empty_pair(ctx, map, context) < 0)
1757 return isl_stat_error;
1759 return isl_stat_ok;
1762 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1764 struct {
1765 const char *map;
1766 const char *context;
1767 const char *gist;
1768 } plain_gist_tests[] = {
1769 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1770 "{ [i] -> [j] : i >= 1 }",
1771 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1772 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1773 "(j mod 4 = 2 and k mod 6 = n) }",
1774 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1775 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1776 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1777 "{ [i] -> [j] : i > j }",
1778 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1781 /* Basic tests for isl_map_plain_gist_basic_map.
1783 static int test_plain_gist(isl_ctx *ctx)
1785 int i;
1787 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1788 const char *str;
1789 int equal;
1790 isl_map *map, *gist;
1791 isl_basic_map *context;
1793 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1794 str = plain_gist_tests[i].context;
1795 context = isl_basic_map_read_from_str(ctx, str);
1796 map = isl_map_plain_gist_basic_map(map, context);
1797 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1798 equal = isl_map_is_equal(map, gist);
1799 isl_map_free(map);
1800 isl_map_free(gist);
1801 if (equal < 0)
1802 return -1;
1803 if (!equal)
1804 isl_die(ctx, isl_error_unknown,
1805 "incorrect gist result", return -1);
1808 return 0;
1811 /* Inputs for isl_basic_set_gist tests that are expected to fail.
1813 struct {
1814 const char *set;
1815 const char *context;
1816 } gist_fail_tests[] = {
1817 { "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1818 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }",
1819 "{ [i] : i >= 0 }" },
1822 /* Check that isl_basic_set_gist fails (gracefully) when expected.
1823 * In particular, the user should be able to recover from the failure.
1825 static isl_stat test_gist_fail(struct isl_ctx *ctx)
1827 int i, n;
1828 int on_error;
1830 on_error = isl_options_get_on_error(ctx);
1831 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
1832 n = ARRAY_SIZE(gist_fail_tests);
1833 for (i = 0; i < n; ++i) {
1834 const char *str;
1835 isl_basic_set *bset, *context;
1837 bset = isl_basic_set_read_from_str(ctx, gist_fail_tests[i].set);
1838 str = gist_fail_tests[i].context;
1839 context = isl_basic_set_read_from_str(ctx, str);
1840 bset = isl_basic_set_gist(bset, context);
1841 isl_basic_set_free(bset);
1842 if (bset)
1843 break;
1845 isl_options_set_on_error(ctx, on_error);
1846 if (i < n)
1847 isl_die(ctx, isl_error_unknown,
1848 "operation not expected to succeed",
1849 return isl_stat_error);
1851 return isl_stat_ok;
1854 struct {
1855 const char *set;
1856 const char *context;
1857 const char *gist;
1858 } gist_tests[] = {
1859 { "{ [1, -1, 3] }",
1860 "{ [1, b, 2 - b] : -1 <= b <= 2 }",
1861 "{ [a, -1, c] }" },
1862 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1863 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1864 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1865 "{ [a, b, c] : a <= 15 }" },
1866 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1867 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1868 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1869 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1870 { "{ [m, n, a, b] : a <= 2147 + n }",
1871 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1872 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1873 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1874 "b >= 0) }",
1875 "{ [m, n, ku, kl] }" },
1876 { "{ [a, a, b] : a >= 10 }",
1877 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1878 "{ [a, a, b] : a >= 10 }" },
1879 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1880 "{ [0, j] : j >= 0 }" },
1881 /* Check that no constraints on i6 are introduced in the gist */
1882 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1883 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1884 "5e0 <= 381 - t1 and i4 <= 1) }",
1885 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1886 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1887 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1888 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1889 "20e0 >= 1511 - 4t1 - 5i4) }" },
1890 /* Check that no constraints on i6 are introduced in the gist */
1891 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1892 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1893 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1894 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1895 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1896 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1897 "20e1 <= 1530 - 4t1 - 5i4 and "
1898 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1899 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1900 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1901 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1902 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1903 "10e0 <= -91 + 5i4 + 4i6 and "
1904 "10e0 >= -105 + 5i4 + 4i6) }",
1905 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1906 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1907 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1908 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1909 "{ [a, b, q, p] : b >= 1 + a }",
1910 "{ [a, b, q, p] : false }" },
1911 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1912 "[n] -> { [x] : x mod 32 = 0 }",
1913 "[n] -> { [x = n] }" },
1914 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1915 "{ [x] : x mod 2 = 0 }" },
1916 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1917 "{ [x] : x mod 128 = 0 }" },
1918 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1919 "{ [x] : x mod 3200 = 0 }" },
1920 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1921 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1922 "{ [a, b, c = a] }" },
1923 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1924 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1925 "{ [a, b, c = a] : a mod 3 = 0 }" },
1926 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1927 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1928 "{ [x] }" },
1929 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1930 "{ [x,y] : 1 <= y <= 3 }",
1931 "{ [x,y] }" },
1934 /* Check that isl_set_gist behaves as expected.
1936 * For the test cases in gist_tests, besides checking that the result
1937 * is as expected, also check that applying the gist operation does
1938 * not modify the input set (an earlier version of isl would do that) and
1939 * that the test case is consistent, i.e., that the gist has the same
1940 * intersection with the context as the input set.
1942 static int test_gist(struct isl_ctx *ctx)
1944 int i;
1945 const char *str;
1946 isl_basic_set *bset1, *bset2;
1947 isl_map *map1, *map2;
1948 isl_bool equal;
1949 isl_size n_div;
1951 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1952 isl_bool equal_input, equal_intersection;
1953 isl_set *set1, *set2, *copy, *context;
1955 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1956 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1957 copy = isl_set_copy(set1);
1958 set1 = isl_set_gist(set1, isl_set_copy(context));
1959 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1960 equal = isl_set_is_equal(set1, set2);
1961 isl_set_free(set1);
1962 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1963 equal_input = isl_set_is_equal(set1, copy);
1964 isl_set_free(copy);
1965 set1 = isl_set_intersect(set1, isl_set_copy(context));
1966 set2 = isl_set_intersect(set2, context);
1967 equal_intersection = isl_set_is_equal(set1, set2);
1968 isl_set_free(set2);
1969 isl_set_free(set1);
1970 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1971 return -1;
1972 if (!equal)
1973 isl_die(ctx, isl_error_unknown,
1974 "incorrect gist result", return -1);
1975 if (!equal_input)
1976 isl_die(ctx, isl_error_unknown,
1977 "gist modified input", return -1);
1978 if (!equal_input)
1979 isl_die(ctx, isl_error_unknown,
1980 "inconsistent gist test case", return -1);
1983 if (test_gist_fail(ctx) < 0)
1984 return -1;
1986 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1987 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1988 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1989 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1990 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1991 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1992 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1993 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1994 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1995 bset1 = isl_basic_set_read_from_str(ctx, str);
1996 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1997 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1998 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1999 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
2000 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
2001 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
2002 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
2003 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
2004 bset2 = isl_basic_set_read_from_str(ctx, str);
2005 bset1 = isl_basic_set_gist(bset1, bset2);
2006 assert(bset1 && bset1->n_div == 0);
2007 isl_basic_set_free(bset1);
2009 /* Check that the integer divisions of the second disjunct
2010 * do not spread to the first disjunct.
2012 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
2013 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
2014 "(exists (e0 = [(-1 + t1)/16], "
2015 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
2016 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
2017 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
2018 "o0 <= 4294967295 and t1 <= -1)) }";
2019 map1 = isl_map_read_from_str(ctx, str);
2020 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
2021 map2 = isl_map_read_from_str(ctx, str);
2022 map1 = isl_map_gist(map1, map2);
2023 if (!map1)
2024 return -1;
2025 if (map1->n != 1)
2026 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
2027 isl_map_free(map1); return -1);
2028 n_div = isl_basic_map_dim(map1->p[0], isl_dim_div);
2029 isl_map_free(map1);
2030 if (n_div < 0)
2031 return -1;
2032 if (n_div != 1)
2033 isl_die(ctx, isl_error_unknown, "expecting single div",
2034 return -1);
2036 if (test_gist_empty(ctx) < 0)
2037 return -1;
2038 if (test_plain_gist(ctx) < 0)
2039 return -1;
2041 return 0;
2044 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
2046 isl_set *set, *set2;
2047 int equal;
2048 int one;
2050 set = isl_set_read_from_str(ctx, str);
2051 set = isl_set_coalesce(set);
2052 set2 = isl_set_read_from_str(ctx, str);
2053 equal = isl_set_is_equal(set, set2);
2054 one = set && set->n == 1;
2055 isl_set_free(set);
2056 isl_set_free(set2);
2058 if (equal < 0)
2059 return -1;
2060 if (!equal)
2061 isl_die(ctx, isl_error_unknown,
2062 "coalesced set not equal to input", return -1);
2063 if (check_one && !one)
2064 isl_die(ctx, isl_error_unknown,
2065 "coalesced set should not be a union", return -1);
2067 return 0;
2070 /* Inputs for coalescing tests with unbounded wrapping.
2071 * "str" is a string representation of the input set.
2072 * "single_disjunct" is set if we expect the result to consist of
2073 * a single disjunct.
2075 struct {
2076 int single_disjunct;
2077 const char *str;
2078 } coalesce_unbounded_tests[] = {
2079 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
2080 "-x - y + 1 >= 0 and -3 <= z <= 3;"
2081 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
2082 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
2083 "-10 <= y <= 0}" },
2084 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
2085 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
2086 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
2087 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
2088 { 0, "{ [x, y, z] : 0 <= x,y,z <= 100 and 0 < z <= 2 + 2x + 2y; "
2089 "[x, y, 0] : x,y <= 100 and y <= 9 + 11x and x <= 9 + 11y }" },
2090 { 1, "{ [0:1, 0:1]; [0, 2:3] }" },
2091 { 1, "{ [0:1, 0:1]; [0, 2:3]; [1, -2:-1] }" },
2092 { 1, "{ [0:3, 0:1]; [1:2, 2:5] }" },
2093 { 1, "{ [0:3, 0:1]; [0:2, 2:5] }" },
2094 { 1, "{ [0:3, 0:1]; [1:3, 2:5] }" },
2095 { 0, "{ [0:3, 0:1]; [1:4, 2:5] }" },
2096 { 0, "{ [0:3, 0:1]; [1:5, 2:5] }" },
2099 /* Test the functionality of isl_set_coalesce with the bounded wrapping
2100 * option turned off.
2102 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
2104 int i;
2105 int r = 0;
2106 int bounded;
2108 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
2109 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
2111 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
2112 const char *str = coalesce_unbounded_tests[i].str;
2113 int check_one = coalesce_unbounded_tests[i].single_disjunct;
2114 if (test_coalesce_set(ctx, str, check_one) >= 0)
2115 continue;
2116 r = -1;
2117 break;
2120 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
2122 return r;
2125 /* Inputs for coalescing tests.
2126 * "str" is a string representation of the input set.
2127 * "single_disjunct" is set if we expect the result to consist of
2128 * a single disjunct.
2130 struct {
2131 int single_disjunct;
2132 const char *str;
2133 } coalesce_tests[] = {
2134 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
2135 "y >= x & x >= 2 & 5 >= y }" },
2136 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
2137 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
2138 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
2139 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
2140 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
2141 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
2142 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
2143 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
2144 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
2145 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
2146 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
2147 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
2148 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
2149 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
2150 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
2151 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
2152 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
2153 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
2154 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
2155 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
2156 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
2157 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
2158 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
2159 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
2160 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
2161 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
2162 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
2163 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
2164 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
2165 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
2166 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
2167 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
2168 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
2169 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
2170 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
2171 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
2172 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
2173 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
2174 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
2175 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
2176 "[o0, o1, o2, o3, o4, o5, o6]] : "
2177 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
2178 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
2179 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
2180 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
2181 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
2182 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
2183 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
2184 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
2185 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
2186 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
2187 "o6 >= i3 + i6 - o3 and M >= 0 and "
2188 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
2189 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
2190 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
2191 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
2192 "(o0 = 0 and M >= 2 and N >= 3) or "
2193 "(M = 0 and o0 = 0 and N >= 3) }" },
2194 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
2195 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
2196 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
2197 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
2198 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
2199 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
2200 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
2201 "(y = 3 and x = 1) }" },
2202 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
2203 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
2204 "i1 <= M and i3 <= M and i4 <= M) or "
2205 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
2206 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
2207 "i4 <= -1 + M) }" },
2208 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
2209 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
2210 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
2211 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
2212 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
2213 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
2214 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
2215 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
2216 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
2217 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
2218 { 0, "{ [a, b] : exists e : 2e = a and "
2219 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
2220 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
2221 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
2222 "j >= 1 and j' <= i + j - i' and i >= 1; "
2223 "[1, 1, 1, 1] }" },
2224 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
2225 "[i,j] : exists a : j = 3a }" },
2226 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
2227 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
2228 "a >= 3) or "
2229 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
2230 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
2231 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
2232 "c <= 6 + 8a and a >= 3; "
2233 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
2234 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
2235 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2236 "[x,0] : 3 <= x <= 4 }" },
2237 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
2238 "[x,0] : 4 <= x <= 5 }" },
2239 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2240 "[x,0] : 3 <= x <= 5 }" },
2241 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
2242 "[x,0] : 3 <= x <= 4 }" },
2243 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
2244 "i1 <= 0; "
2245 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
2246 { 1, "{ [0,0]; [1,1] }" },
2247 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
2248 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
2249 "ii <= k;"
2250 "[k, 0, k] : k <= 6 and k >= 1 }" },
2251 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
2252 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
2253 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
2254 { 1, "[n] -> { [1] : n >= 0;"
2255 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
2256 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
2257 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
2258 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
2259 "2e0 <= x and 2e0 <= n);"
2260 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
2261 "n >= 0) }" },
2262 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
2263 "128e0 >= -134 + 127t1 and t1 >= 2 and "
2264 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
2265 "t1 = 1 }" },
2266 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
2267 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
2268 "[0, 0] }" },
2269 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
2270 "t1 >= 13 and t1 <= 16);"
2271 "[t1] : t1 <= 15 and t1 >= 12 }" },
2272 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
2273 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
2274 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
2275 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
2276 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
2277 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
2278 "i <= 4j + 2 }" },
2279 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
2280 "(exists (e0 : 3e0 = -2 + c0)) }" },
2281 { 0, "[n, b0, t0] -> "
2282 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2283 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2284 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2285 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2286 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
2287 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
2288 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
2289 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
2290 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2291 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2292 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2293 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
2294 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
2295 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
2296 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
2297 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
2298 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
2299 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
2300 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
2301 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
2302 { 0, "{ [i0, i1, i2] : "
2303 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
2304 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
2305 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
2306 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
2307 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
2308 "32e0 <= 31 + i0)) or "
2309 "i0 >= 0 }" },
2310 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
2311 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
2312 "2*floor((c)/2) = c and 0 <= a <= 192;"
2313 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
2315 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
2316 "(0 <= a <= b <= n) }" },
2317 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
2318 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
2319 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
2320 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2321 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2322 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2323 "b = 3 and 9e0 <= -19 + 2c)) }" },
2324 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2325 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2326 "(a = 4 and b = 3 and "
2327 "9*floor((-16 + 2c)/9) <= -19 + 2c) }" },
2328 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
2329 "(b = -1 + a and 0 < a <= 3 and "
2330 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2331 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2332 "b = 3 and 9e0 <= -19 + 2c)) }" },
2333 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2334 "[1, 0] }" },
2335 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2336 "[0, 1] }" },
2337 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2338 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2339 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
2340 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
2341 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
2342 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
2343 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
2344 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
2345 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
2346 { 1, "{ [a] : a <= 8 and "
2347 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
2348 { 1, "{ [x, y] : 2y = -x and x <= 0 or "
2349 "x <= -1 and 2y <= -x - 1 and 2y >= x - 1 }" },
2350 { 0, "{ [x, y] : 2y = -x and x <= 0 or "
2351 "x <= -2 and 2y <= -x - 1 and 2y >= x - 1 }" },
2352 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2353 "(a < 0 and 3*floor((a)/3) < a) }" },
2354 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2355 "(a < -1 and 3*floor((a)/3) < a) }" },
2356 { 1, "{ [a, b] : a <= 1024 and b >= 0 and "
2357 "((-31 - a + b <= 32*floor((-1 - a)/32) <= -33 + b and "
2358 "32*floor((-1 - a)/32) <= -16 + b + 16*floor((-1 - a)/16))"
2359 "or (2 <= a <= 15 and b < a)) }" },
2360 { 1, "{ [a] : a > 0 and ((16*floor((a)/16) < a and "
2361 "32*floor((a)/32) < a) or a <= 15) }" },
2362 { 1, "{ [a, b, c, d] : (-a + d) mod 64 = 0 and a <= 8 and b <= 1 and "
2363 "10 - a <= c <= 3 and d >= 5 and 9 - 64b <= d <= 70;"
2364 "[a, b = 1, c, d] : (-a + d) mod 64 = 0 and a <= 8 and c >= 4 and "
2365 "10 - a <= c <= 5 and 5 <= d <= 73 - c }" },
2366 { 1, "[n, m] -> { S_0[i] : (-n + i) mod 3 = 0 and m >= 3 + n and "
2367 "i >= n and 3*floor((2 + n + 2m)/3) <= n + 3m - i; "
2368 "S_0[n] : n <= m <= 2 + n }" },
2369 { 1, "{ [a, b] : exists (e0: 0 <= a <= 1 and b >= 0 and "
2370 "2e0 >= -5 + a + 2b and 2e0 >= -1 + a + b and "
2371 "2e0 <= a + b); "
2372 "[a, b] : exists (e0: 0 <= a <= 1 and 2e0 >= -5 + a + 2b and "
2373 "2e0 >= -1 - a + b and 2e0 <= -a + b and "
2374 "2e0 < -a + 2b) }" },
2375 { 1, "{ [i, j, i - 8j] : 8 <= i <= 63 and -7 + i <= 8j <= i; "
2376 "[i, 0, i] : 0 <= i <= 7 }" },
2377 { 1, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [1, 1] }" },
2378 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [0, 2] }" },
2379 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [-1, 3] }" },
2380 { 1, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [1, 1] }" },
2381 { 0, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [2, 1] }" },
2382 { 0, "{ [a, c] : (2 + a) mod 4 = 0 or "
2383 "(c = 4 + a and 4 * floor((a)/4) = a and a >= 0 and a <= 4) or "
2384 "(c = 3 + a and 4 * floor((-1 + a)/4) = -1 + a and "
2385 "a > 0 and a <= 5) }" },
2386 { 1, "{ [1, 0, 0]; [a, b, c] : -1 <= -a < b <= 0 and 2c > b }" },
2387 { 0, "{ [j, a, l] : a mod 2 = 0 and j <= 29 and a >= 2 and "
2388 "2a <= -5 + j and 32j + 2a + 2 <= 4l < 33j; "
2389 "[j, 0, l] : 4 <= j <= 29 and -3 + 33j <= 4l <= 33j }" },
2390 { 0, "{ [0:1, 0:1]; [0, 2:3] }" },
2391 { 1, "{ [a] : (a = 0 or ((1 + a) mod 2 = 0 and 0 < a <= 15) or "
2392 "((a) mod 2 = 0 and 0 < a <= 15)) }" },
2393 { 1, "{ rat: [0:2]; rat: [1:3] }" },
2396 /* A specialized coalescing test case that would result
2397 * in a segmentation fault or a failed assertion in earlier versions of isl.
2399 static int test_coalesce_special(struct isl_ctx *ctx)
2401 const char *str;
2402 isl_map *map1, *map2;
2404 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2405 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
2406 "(y = 201 and o1 <= 239 and o1 >= 212) or "
2407 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
2408 "o1 <= 239 and o1 >= 212)) or "
2409 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
2410 "o1 <= 241 and o1 >= 240));"
2411 "[S_L220_OUT[] -> T7[]] -> "
2412 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
2413 "(y = 2 and o1 <= 241 and o1 >= 212) or "
2414 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
2415 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
2416 map1 = isl_map_read_from_str(ctx, str);
2417 map1 = isl_map_align_divs_internal(map1);
2418 map1 = isl_map_coalesce(map1);
2419 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2420 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
2421 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
2422 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
2423 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
2424 map2 = isl_map_read_from_str(ctx, str);
2425 map2 = isl_map_union(map2, map1);
2426 map2 = isl_map_align_divs_internal(map2);
2427 map2 = isl_map_coalesce(map2);
2428 isl_map_free(map2);
2429 if (!map2)
2430 return -1;
2432 return 0;
2435 /* Check that the union of the basic sets described by "str1" and "str2"
2436 * can be coalesced and that the result is equal to the union.
2437 * The explicit call to isl_basic_set_union prevents the implicit
2438 * equality constraints in the basic maps from being detected prior
2439 * to the call to isl_set_coalesce, at least at the point
2440 * where this function was introduced.
2442 static isl_stat test_coalesce_union(isl_ctx *ctx, const char *str1,
2443 const char *str2)
2445 isl_basic_set *bset1, *bset2;
2446 isl_set *set, *set2;
2447 isl_bool equal;
2449 bset1 = isl_basic_set_read_from_str(ctx, str1);
2450 bset2 = isl_basic_set_read_from_str(ctx, str2);
2451 set = isl_basic_set_union(bset1, bset2);
2452 set = isl_set_coalesce(set);
2454 bset1 = isl_basic_set_read_from_str(ctx, str1);
2455 bset2 = isl_basic_set_read_from_str(ctx, str2);
2456 set2 = isl_basic_set_union(bset1, bset2);
2458 equal = isl_set_is_equal(set, set2);
2459 isl_set_free(set);
2460 isl_set_free(set2);
2462 if (equal < 0)
2463 return isl_stat_error;
2464 if (!equal)
2465 isl_die(ctx, isl_error_unknown,
2466 "coalesced set not equal to input",
2467 return isl_stat_error);
2469 return isl_stat_non_null(set);
2472 /* A specialized coalescing test case that would result in an assertion
2473 * in an earlier version of isl. Use test_coalesce_union with
2474 * an explicit call to isl_basic_set_union to prevent the implicit
2475 * equality constraints in the first basic map from being detected prior
2476 * to the call to isl_set_coalesce, at least at the point
2477 * where this test case was introduced.
2479 static isl_stat test_coalesce_special2(struct isl_ctx *ctx)
2481 const char *str1;
2482 const char *str2;
2484 str1 = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
2485 str2 = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }";
2486 return test_coalesce_union(ctx, str1, str2);
2489 /* Check that calling isl_set_coalesce does not leave other sets
2490 * that may share some information with the input to isl_set_coalesce
2491 * in an inconsistent state.
2492 * In particular, older versions of isl would modify all copies
2493 * of the basic sets in the isl_set_coalesce input in a way
2494 * that could leave them in an inconsistent state.
2495 * The result of printing any other set containing one of these
2496 * basic sets would then result in an invalid set description.
2498 static int test_coalesce_special3(isl_ctx *ctx)
2500 const char *str;
2501 char *s;
2502 isl_set *set1, *set2;
2503 isl_printer *p;
2505 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
2506 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
2507 set2 = isl_set_read_from_str(ctx, str);
2508 set1 = isl_set_union(set1, isl_set_copy(set2));
2509 set1 = isl_set_coalesce(set1);
2510 isl_set_free(set1);
2512 p = isl_printer_to_str(ctx);
2513 p = isl_printer_print_set(p, set2);
2514 isl_set_free(set2);
2515 s = isl_printer_get_str(p);
2516 isl_printer_free(p);
2517 set1 = isl_set_read_from_str(ctx, s);
2518 free(s);
2519 isl_set_free(set1);
2521 if (!set1)
2522 return -1;
2524 return 0;
2527 /* Check that calling isl_set_coalesce on the intersection of
2528 * the sets described by "s1" and "s2" does not leave other sets
2529 * that may share some information with the input to isl_set_coalesce
2530 * in an inconsistent state.
2531 * In particular, when isl_set_coalesce detects equality constraints,
2532 * it does not immediately perform Gaussian elimination on them,
2533 * but then it needs to ensure that it is performed at some point.
2534 * The input set has implicit equality constraints in the first disjunct.
2535 * It is constructed as an intersection, because otherwise
2536 * those equality constraints would already be detected during parsing.
2538 static isl_stat test_coalesce_intersection(isl_ctx *ctx,
2539 const char *s1, const char *s2)
2541 isl_set *set1, *set2;
2543 set1 = isl_set_read_from_str(ctx, s1);
2544 set2 = isl_set_read_from_str(ctx, s2);
2545 set1 = isl_set_intersect(set1, set2);
2546 isl_set_free(isl_set_coalesce(isl_set_copy(set1)));
2547 set1 = isl_set_coalesce(set1);
2548 isl_set_free(set1);
2550 if (!set1)
2551 return isl_stat_error;
2553 return isl_stat_ok;
2556 /* Check that calling isl_set_coalesce does not leave other sets
2557 * that may share some information with the input to isl_set_coalesce
2558 * in an inconsistent state, for the case where one disjunct
2559 * is a subset of the other.
2561 static isl_stat test_coalesce_special4(isl_ctx *ctx)
2563 const char *s1, *s2;
2565 s1 = "{ [a, b] : b <= 0 or a <= 1 }";
2566 s2 = "{ [a, b] : -1 <= -a < b }";
2567 return test_coalesce_intersection(ctx, s1, s2);
2570 /* Check that calling isl_set_coalesce does not leave other sets
2571 * that may share some information with the input to isl_set_coalesce
2572 * in an inconsistent state, for the case where two disjuncts
2573 * can be fused.
2575 static isl_stat test_coalesce_special5(isl_ctx *ctx)
2577 const char *s1, *s2;
2579 s1 = "{ [a, b, c] : b <= 0 }";
2580 s2 = "{ [a, b, c] : -1 <= -a < b and (c >= 0 or c < 0) }";
2581 return test_coalesce_intersection(ctx, s1, s2);
2584 /* Check that calling isl_set_coalesce does not leave other sets
2585 * that may share some information with the input to isl_set_coalesce
2586 * in an inconsistent state, for the case where two disjuncts
2587 * can be fused and where both disjuncts have implicit equality constraints.
2589 static isl_stat test_coalesce_special6(isl_ctx *ctx)
2591 const char *s1, *s2;
2593 s1 = "{ [a, b, c] : c <= 0 }";
2594 s2 = "{ [a, b, c] : 0 <= a <= b <= c or (0 <= b <= c and a > 0) }";
2595 return test_coalesce_intersection(ctx, s1, s2);
2598 /* A specialized coalescing test case that would result in an assertion failure
2599 * in an earlier version of isl. Use test_coalesce_union with
2600 * an explicit call to isl_basic_set_union to prevent the implicit
2601 * equality constraints in the basic maps from being detected prior
2602 * to the call to isl_set_coalesce, at least at the point
2603 * where this test case was introduced.
2605 static isl_stat test_coalesce_special7(isl_ctx *ctx)
2607 const char *str1;
2608 const char *str2;
2610 str1 = "{ [a, b, c=0:17] : a <= 7 and 2b <= 11 - a and "
2611 "c <= -7 + 2a and 2c >= - 3 + 3a - 2b }";
2612 str2 = "{ [a, b, c] : c > -15a and c >= -7 + 2a and c < 0 and "
2613 "3c <= -5 + 5a - 3b and 2b >= 11 - a }";
2614 return test_coalesce_union(ctx, str1, str2);
2617 /* A specialized coalescing test case that would result in a disjunct
2618 * getting dropped in an earlier version of isl. Use test_coalesce_union with
2619 * an explicit call to isl_basic_set_union to prevent the implicit
2620 * equality constraints in the basic maps from being detected prior
2621 * to the call to isl_set_coalesce, at least at the point
2622 * where this test case was introduced.
2624 static isl_stat test_coalesce_special8(isl_ctx *ctx)
2626 const char *str1;
2627 const char *str2;
2629 str1 = "{ [a, b, c] : 2c <= -a and b >= -a and b <= 5 and "
2630 "6c > -7a and 11c >= -5a - b and a <= 3 }";
2631 str2 = "{ [a, b, c] : 6c > -7a and b >= -a and b <= 5 and "
2632 "11c >= -5a - b and a >= 4 and 2b <= a and 2c <= -a }";
2633 return test_coalesce_union(ctx, str1, str2);
2636 /* Test the functionality of isl_set_coalesce.
2637 * That is, check that the output is always equal to the input
2638 * and in some cases that the result consists of a single disjunct.
2640 static int test_coalesce(struct isl_ctx *ctx)
2642 int i;
2644 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2645 const char *str = coalesce_tests[i].str;
2646 int check_one = coalesce_tests[i].single_disjunct;
2647 if (test_coalesce_set(ctx, str, check_one) < 0)
2648 return -1;
2651 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2652 return -1;
2653 if (test_coalesce_special(ctx) < 0)
2654 return -1;
2655 if (test_coalesce_special2(ctx) < 0)
2656 return -1;
2657 if (test_coalesce_special3(ctx) < 0)
2658 return -1;
2659 if (test_coalesce_special4(ctx) < 0)
2660 return -1;
2661 if (test_coalesce_special5(ctx) < 0)
2662 return -1;
2663 if (test_coalesce_special6(ctx) < 0)
2664 return -1;
2665 if (test_coalesce_special7(ctx) < 0)
2666 return -1;
2667 if (test_coalesce_special8(ctx) < 0)
2668 return -1;
2670 return 0;
2673 /* Construct a representation of the graph on the right of Figure 1
2674 * in "Computing the Transitive Closure of a Union of
2675 * Affine Integer Tuple Relations".
2677 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2679 isl_set *dom;
2680 isl_map *up, *right;
2682 dom = isl_set_read_from_str(ctx,
2683 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2684 "2 x - 3 y + 3 >= 0 }");
2685 right = isl_map_read_from_str(ctx,
2686 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2687 up = isl_map_read_from_str(ctx,
2688 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2689 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2690 right = isl_map_intersect_range(right, isl_set_copy(dom));
2691 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2692 up = isl_map_intersect_range(up, dom);
2693 return isl_map_union(up, right);
2696 /* Construct a representation of the power of the graph
2697 * on the right of Figure 1 in "Computing the Transitive Closure of
2698 * a Union of Affine Integer Tuple Relations".
2700 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2702 return isl_map_read_from_str(ctx,
2703 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2704 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2705 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2708 /* Construct a representation of the transitive closure of the graph
2709 * on the right of Figure 1 in "Computing the Transitive Closure of
2710 * a Union of Affine Integer Tuple Relations".
2712 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2714 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2717 static int test_closure(isl_ctx *ctx)
2719 const char *str;
2720 isl_map *map, *map2;
2721 isl_bool exact, equal;
2723 /* COCOA example 1 */
2724 map = isl_map_read_from_str(ctx,
2725 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2726 "1 <= i and i < n and 1 <= j and j < n or "
2727 "i2 = i + 1 and j2 = j - 1 and "
2728 "1 <= i and i < n and 2 <= j and j <= n }");
2729 map = isl_map_power(map, &exact);
2730 assert(exact);
2731 isl_map_free(map);
2733 /* COCOA example 1 */
2734 map = isl_map_read_from_str(ctx,
2735 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2736 "1 <= i and i < n and 1 <= j and j < n or "
2737 "i2 = i + 1 and j2 = j - 1 and "
2738 "1 <= i and i < n and 2 <= j and j <= n }");
2739 map = isl_map_transitive_closure(map, &exact);
2740 assert(exact);
2741 map2 = isl_map_read_from_str(ctx,
2742 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2743 "1 <= i and i < n and 1 <= j and j <= n and "
2744 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2745 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2746 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2747 assert(isl_map_is_equal(map, map2));
2748 isl_map_free(map2);
2749 isl_map_free(map);
2751 map = isl_map_read_from_str(ctx,
2752 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2753 " 0 <= y and y <= n }");
2754 map = isl_map_transitive_closure(map, &exact);
2755 map2 = isl_map_read_from_str(ctx,
2756 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2757 " 0 <= y and y <= n }");
2758 assert(isl_map_is_equal(map, map2));
2759 isl_map_free(map2);
2760 isl_map_free(map);
2762 /* COCOA example 2 */
2763 map = isl_map_read_from_str(ctx,
2764 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2765 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2766 "i2 = i + 2 and j2 = j - 2 and "
2767 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2768 map = isl_map_transitive_closure(map, &exact);
2769 assert(exact);
2770 map2 = isl_map_read_from_str(ctx,
2771 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2772 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2773 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2774 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2775 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2776 assert(isl_map_is_equal(map, map2));
2777 isl_map_free(map);
2778 isl_map_free(map2);
2780 /* COCOA Fig.2 left */
2781 map = isl_map_read_from_str(ctx,
2782 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2783 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2784 "j <= n or "
2785 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2786 "j <= 2 i - 3 and j <= n - 2 or "
2787 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2788 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2789 map = isl_map_transitive_closure(map, &exact);
2790 assert(exact);
2791 isl_map_free(map);
2793 /* COCOA Fig.2 right */
2794 map = isl_map_read_from_str(ctx,
2795 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2796 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2797 "j <= n or "
2798 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2799 "j <= 2 i - 4 and j <= n - 3 or "
2800 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2801 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2802 map = isl_map_power(map, &exact);
2803 assert(exact);
2804 isl_map_free(map);
2806 /* COCOA Fig.2 right */
2807 map = isl_map_read_from_str(ctx,
2808 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2809 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2810 "j <= n or "
2811 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2812 "j <= 2 i - 4 and j <= n - 3 or "
2813 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2814 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2815 map = isl_map_transitive_closure(map, &exact);
2816 assert(exact);
2817 map2 = isl_map_read_from_str(ctx,
2818 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2819 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2820 "j <= n and 3 + i + 2 j <= 3 n and "
2821 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2822 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2823 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2824 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2825 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2826 assert(isl_map_is_equal(map, map2));
2827 isl_map_free(map2);
2828 isl_map_free(map);
2830 map = cocoa_fig_1_right_graph(ctx);
2831 map = isl_map_transitive_closure(map, &exact);
2832 assert(exact);
2833 map2 = cocoa_fig_1_right_tc(ctx);
2834 assert(isl_map_is_equal(map, map2));
2835 isl_map_free(map2);
2836 isl_map_free(map);
2838 map = cocoa_fig_1_right_graph(ctx);
2839 map = isl_map_power(map, &exact);
2840 map2 = cocoa_fig_1_right_power(ctx);
2841 equal = isl_map_is_equal(map, map2);
2842 isl_map_free(map2);
2843 isl_map_free(map);
2844 if (equal < 0)
2845 return -1;
2846 if (!exact)
2847 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2848 if (!equal)
2849 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2851 /* COCOA Theorem 1 counter example */
2852 map = isl_map_read_from_str(ctx,
2853 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2854 "i2 = 1 and j2 = j or "
2855 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2856 map = isl_map_transitive_closure(map, &exact);
2857 assert(exact);
2858 isl_map_free(map);
2860 map = isl_map_read_from_str(ctx,
2861 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2862 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2863 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2864 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2865 map = isl_map_transitive_closure(map, &exact);
2866 assert(exact);
2867 isl_map_free(map);
2869 /* Kelly et al 1996, fig 12 */
2870 map = isl_map_read_from_str(ctx,
2871 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2872 "1 <= i,j,j+1 <= n or "
2873 "j = n and j2 = 1 and i2 = i + 1 and "
2874 "1 <= i,i+1 <= n }");
2875 map = isl_map_transitive_closure(map, &exact);
2876 assert(exact);
2877 map2 = isl_map_read_from_str(ctx,
2878 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2879 "1 <= i <= n and i = i2 or "
2880 "1 <= i < i2 <= n and 1 <= j <= n and "
2881 "1 <= j2 <= n }");
2882 assert(isl_map_is_equal(map, map2));
2883 isl_map_free(map2);
2884 isl_map_free(map);
2886 /* Omega's closure4 */
2887 map = isl_map_read_from_str(ctx,
2888 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2889 "1 <= x,y <= 10 or "
2890 "x2 = x + 1 and y2 = y and "
2891 "1 <= x <= 20 && 5 <= y <= 15 }");
2892 map = isl_map_transitive_closure(map, &exact);
2893 assert(exact);
2894 isl_map_free(map);
2896 map = isl_map_read_from_str(ctx,
2897 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2898 map = isl_map_transitive_closure(map, &exact);
2899 assert(!exact);
2900 map2 = isl_map_read_from_str(ctx,
2901 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2902 assert(isl_map_is_equal(map, map2));
2903 isl_map_free(map);
2904 isl_map_free(map2);
2906 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2907 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2908 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2909 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2910 map = isl_map_read_from_str(ctx, str);
2911 map = isl_map_transitive_closure(map, &exact);
2912 assert(exact);
2913 map2 = isl_map_read_from_str(ctx, str);
2914 assert(isl_map_is_equal(map, map2));
2915 isl_map_free(map);
2916 isl_map_free(map2);
2918 str = "{[0] -> [1]; [2] -> [3]}";
2919 map = isl_map_read_from_str(ctx, str);
2920 map = isl_map_transitive_closure(map, &exact);
2921 assert(exact);
2922 map2 = isl_map_read_from_str(ctx, str);
2923 assert(isl_map_is_equal(map, map2));
2924 isl_map_free(map);
2925 isl_map_free(map2);
2927 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2928 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2929 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2930 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2931 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2932 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2933 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2934 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2935 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2936 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2937 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2938 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2939 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2940 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2941 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2942 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2943 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2944 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2945 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2946 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2947 map = isl_map_read_from_str(ctx, str);
2948 map = isl_map_transitive_closure(map, NULL);
2949 assert(map);
2950 isl_map_free(map);
2952 return 0;
2955 /* Check that the actual result of a boolean operation is equal
2956 * to the expected result.
2958 static isl_stat check_bool(isl_ctx *ctx, isl_bool actual, isl_bool expected)
2960 if (actual != expected)
2961 isl_die(ctx, isl_error_unknown,
2962 "incorrect boolean operation", return isl_stat_error);
2963 return isl_stat_ok;
2966 /* Test operations on isl_bool values.
2968 * This tests:
2970 * isl_bool_not
2971 * isl_bool_ok
2973 static int test_isl_bool(isl_ctx *ctx)
2975 if (check_bool(ctx, isl_bool_not(isl_bool_true), isl_bool_false) < 0)
2976 return -1;
2977 if (check_bool(ctx, isl_bool_not(isl_bool_false), isl_bool_true) < 0)
2978 return -1;
2979 if (check_bool(ctx, isl_bool_not(isl_bool_error), isl_bool_error) < 0)
2980 return -1;
2981 if (check_bool(ctx, isl_bool_ok(0), isl_bool_false) < 0)
2982 return -1;
2983 if (check_bool(ctx, isl_bool_ok(1), isl_bool_true) < 0)
2984 return -1;
2985 if (check_bool(ctx, isl_bool_ok(-1), isl_bool_true) < 0)
2986 return -1;
2987 if (check_bool(ctx, isl_bool_ok(2), isl_bool_true) < 0)
2988 return -1;
2989 if (check_bool(ctx, isl_bool_ok(-2), isl_bool_true) < 0)
2990 return -1;
2992 return 0;
2995 static int test_lex(struct isl_ctx *ctx)
2997 isl_space *space;
2998 isl_map *map;
2999 int empty;
3001 space = isl_space_set_alloc(ctx, 0, 0);
3002 map = isl_map_lex_le(space);
3003 empty = isl_map_is_empty(map);
3004 isl_map_free(map);
3006 if (empty < 0)
3007 return -1;
3008 if (empty)
3009 isl_die(ctx, isl_error_unknown,
3010 "expecting non-empty result", return -1);
3012 return 0;
3015 /* Inputs for isl_map_lexmin tests.
3016 * "map" is the input and "lexmin" is the expected result.
3018 struct {
3019 const char *map;
3020 const char *lexmin;
3021 } lexmin_tests [] = {
3022 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
3023 "{ [x] -> [5] : 6 <= x <= 8; "
3024 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
3025 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
3026 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
3027 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
3028 "{ [x] -> [y] : (4y = x and x >= 0) or "
3029 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
3030 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
3031 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
3032 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
3033 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
3034 /* Check that empty pieces are properly combined. */
3035 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
3036 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
3037 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
3038 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
3039 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
3040 "x >= 4 }" },
3041 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
3042 "a <= 255 and c <= 255 and d <= 255 - j and "
3043 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
3044 "247d <= 247 + k - j and 247d <= 247 + k - b and "
3045 "247d <= 247 + i and 248 - b <= 248d <= c and "
3046 "254d >= i - a + b and 254d >= -a + b and "
3047 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
3048 "{ [i, k, j] -> "
3049 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
3050 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
3051 "247j >= 62738 - i and 509j <= 129795 + i and "
3052 "742j >= 188724 - i; "
3053 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
3054 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
3055 "16*floor((8 + b)/16) <= 7 + b; "
3056 "[a] -> [1] }",
3057 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
3058 "[a] -> [b = 0] : 0 < a <= 509 }" },
3059 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
3060 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
3061 { "{ rat: [i] : 21 <= 2i <= 29 or i = 5 }", "{ rat: [5] }" },
3064 static int test_lexmin(struct isl_ctx *ctx)
3066 int i;
3067 int equal;
3068 const char *str;
3069 isl_basic_map *bmap;
3070 isl_map *map, *map2;
3071 isl_set *set;
3072 isl_set *set2;
3073 isl_pw_multi_aff *pma;
3075 str = "[p0, p1] -> { [] -> [] : "
3076 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
3077 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
3078 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
3079 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
3080 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
3081 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
3082 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
3083 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
3084 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
3085 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
3086 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
3087 map = isl_map_read_from_str(ctx, str);
3088 map = isl_map_lexmin(map);
3089 isl_map_free(map);
3090 if (!map)
3091 return -1;
3093 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
3094 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
3095 set = isl_set_read_from_str(ctx, str);
3096 set = isl_set_lexmax(set);
3097 str = "[C] -> { [obj,a,b,c] : C = 8 }";
3098 set2 = isl_set_read_from_str(ctx, str);
3099 set = isl_set_intersect(set, set2);
3100 assert(!isl_set_is_empty(set));
3101 isl_set_free(set);
3103 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
3104 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
3105 map = isl_map_lexmin(map);
3106 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
3107 equal = isl_map_is_equal(map, map2);
3108 isl_map_free(map);
3109 isl_map_free(map2);
3111 if (equal < 0)
3112 return -1;
3113 if (!equal)
3114 isl_die(ctx, isl_error_unknown,
3115 "unexpected result", return -1);
3118 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
3119 " 8i' <= i and 8i' >= -7 + i }";
3120 bmap = isl_basic_map_read_from_str(ctx, str);
3121 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
3122 map2 = isl_map_from_pw_multi_aff(pma);
3123 map = isl_map_from_basic_map(bmap);
3124 assert(isl_map_is_equal(map, map2));
3125 isl_map_free(map);
3126 isl_map_free(map2);
3128 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
3129 " 8i' <= i and 8i' >= -7 + i }";
3130 set = isl_set_read_from_str(ctx, str);
3131 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
3132 set2 = isl_set_from_pw_multi_aff(pma);
3133 equal = isl_set_is_equal(set, set2);
3134 isl_set_free(set);
3135 isl_set_free(set2);
3136 if (equal < 0)
3137 return -1;
3138 if (!equal)
3139 isl_die(ctx, isl_error_unknown,
3140 "unexpected difference between set and "
3141 "piecewise affine expression", return -1);
3143 return 0;
3146 /* Inputs for isl_pw_multi_aff_max_multi_val tests.
3147 * "pma" is the input.
3148 * "res" is the expected result.
3150 static struct {
3151 const char *pma;
3152 const char *res;
3153 } opt_pw_tests[] = {
3154 { "{ [-1] -> [-1]; [1] -> [1] }", "{ [1] }" },
3155 { "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] : "
3156 "0 <= a, b <= 100 and b mod 2 = 0}", "{ [30] }" },
3157 { "[N] -> { [i,j] -> A[i, -i, i + j] : 0 <= i,j <= N <= 10 }",
3158 "{ A[10, 0, 20] }" },
3159 { "[N] -> {A[N, -N, 2N] : 0 <= N }", "{ A[infty, 0, infty] }" },
3162 /* Perform basic isl_pw_multi_aff_max_multi_val tests.
3164 static isl_stat test_pw_max(struct isl_ctx *ctx)
3166 int i;
3167 isl_pw_multi_aff *pma;
3168 isl_multi_val *mv;
3169 isl_stat r;
3171 for (i = 0; i < ARRAY_SIZE(opt_pw_tests); ++i) {
3172 pma = isl_pw_multi_aff_read_from_str(ctx, opt_pw_tests[i].pma);
3173 mv = isl_pw_multi_aff_max_multi_val(pma);
3174 r = multi_val_check_plain_equal(mv, opt_pw_tests[i].res);
3175 isl_multi_val_free(mv);
3177 if (r < 0)
3178 return isl_stat_error;
3181 return isl_stat_ok;
3184 /* A specialized isl_set_min_val test case that would return the wrong result
3185 * in earlier versions of isl.
3186 * The explicit call to isl_basic_set_union prevents the second basic set
3187 * from being determined to be empty prior to the call to isl_set_min_val,
3188 * at least at the point where this test case was introduced.
3190 static int test_min_special(isl_ctx *ctx)
3192 const char *str;
3193 isl_basic_set *bset1, *bset2;
3194 isl_set *set;
3195 isl_aff *obj;
3196 isl_val *res;
3197 int ok;
3199 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
3200 bset1 = isl_basic_set_read_from_str(ctx, str);
3201 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
3202 bset2 = isl_basic_set_read_from_str(ctx, str);
3203 set = isl_basic_set_union(bset1, bset2);
3204 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
3206 res = isl_set_min_val(set, obj);
3207 ok = isl_val_cmp_si(res, 5) == 0;
3209 isl_aff_free(obj);
3210 isl_set_free(set);
3211 isl_val_free(res);
3213 if (!res)
3214 return -1;
3215 if (!ok)
3216 isl_die(ctx, isl_error_unknown, "unexpected minimum",
3217 return -1);
3219 return 0;
3222 /* A specialized isl_set_min_val test case that would return an error
3223 * in earlier versions of isl.
3225 static int test_min_special2(isl_ctx *ctx)
3227 const char *str;
3228 isl_basic_set *bset;
3229 isl_aff *obj;
3230 isl_val *res;
3232 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
3233 bset = isl_basic_set_read_from_str(ctx, str);
3235 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
3237 res = isl_basic_set_max_val(bset, obj);
3239 isl_basic_set_free(bset);
3240 isl_aff_free(obj);
3241 isl_val_free(res);
3243 if (!res)
3244 return -1;
3246 return 0;
3249 /* Check that the result of isl_set_min_multi_pw_aff
3250 * on the union of the sets with string descriptions "s1" and "s2"
3251 * consists of a single expression (on a single cell).
3253 static isl_stat check_single_expr_min(isl_ctx *ctx, const char *s1,
3254 const char *s2)
3256 isl_size n;
3257 isl_set *set1, *set2;
3258 isl_multi_pw_aff *mpa;
3259 isl_pw_multi_aff *pma;
3261 set1 = isl_set_read_from_str(ctx, s1);
3262 set2 = isl_set_read_from_str(ctx, s2);
3263 set1 = isl_set_union(set1, set2);
3264 mpa = isl_set_min_multi_pw_aff(set1);
3265 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
3266 n = isl_pw_multi_aff_n_piece(pma);
3267 isl_pw_multi_aff_free(pma);
3269 if (n < 0)
3270 return isl_stat_error;
3271 if (n != 1)
3272 isl_die(ctx, isl_error_unknown, "expecting single expression",
3273 return isl_stat_error);
3274 return isl_stat_ok;
3277 /* A specialized isl_set_min_multi_pw_aff test that checks
3278 * that the minimum of 2N and 3N for N >= 0 is represented
3279 * by a single expression, without splitting off the special case N = 0.
3280 * Do this for both orderings.
3282 static int test_min_mpa(isl_ctx *ctx)
3284 const char *s1, *s2;
3286 s1 = "[N=0:] -> { [1, 3N:] }";
3287 s2 = "[N=0:] -> { [10, 2N:] }";
3288 if (check_single_expr_min(ctx, s1, s2) < 0)
3289 return -1;
3290 if (check_single_expr_min(ctx, s2, s1) < 0)
3291 return -1;
3293 return 0;
3296 struct {
3297 const char *set;
3298 const char *obj;
3299 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
3300 __isl_keep isl_aff *obj);
3301 const char *res;
3302 } opt_tests[] = {
3303 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
3304 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
3305 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
3306 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
3307 &isl_set_max_val, "30" },
3311 /* Perform basic isl_set_min_val and isl_set_max_val tests.
3312 * In particular, check the results on non-convex inputs.
3314 static int test_min(struct isl_ctx *ctx)
3316 int i;
3317 isl_set *set;
3318 isl_aff *obj;
3319 isl_val *val, *res;
3320 isl_bool ok;
3322 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
3323 set = isl_set_read_from_str(ctx, opt_tests[i].set);
3324 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
3325 res = isl_val_read_from_str(ctx, opt_tests[i].res);
3326 val = opt_tests[i].fn(set, obj);
3327 ok = isl_val_eq(res, val);
3328 isl_val_free(res);
3329 isl_val_free(val);
3330 isl_aff_free(obj);
3331 isl_set_free(set);
3333 if (ok < 0)
3334 return -1;
3335 if (!ok)
3336 isl_die(ctx, isl_error_unknown,
3337 "unexpected optimum", return -1);
3340 if (test_pw_max(ctx) < 0)
3341 return -1;
3342 if (test_min_special(ctx) < 0)
3343 return -1;
3344 if (test_min_special2(ctx) < 0)
3345 return -1;
3347 return 0;
3350 struct must_may {
3351 isl_map *must;
3352 isl_map *may;
3355 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
3356 void *dep_user, void *user)
3358 struct must_may *mm = (struct must_may *)user;
3360 if (must)
3361 mm->must = isl_map_union(mm->must, dep);
3362 else
3363 mm->may = isl_map_union(mm->may, dep);
3365 return isl_stat_ok;
3368 static int common_space(void *first, void *second)
3370 int depth = *(int *)first;
3371 return 2 * depth;
3374 static int map_is_equal(__isl_keep isl_map *map, const char *str)
3376 isl_map *map2;
3377 int equal;
3379 if (!map)
3380 return -1;
3382 map2 = isl_map_read_from_str(map->ctx, str);
3383 equal = isl_map_is_equal(map, map2);
3384 isl_map_free(map2);
3386 return equal;
3389 static int map_check_equal(__isl_keep isl_map *map, const char *str)
3391 int equal;
3393 equal = map_is_equal(map, str);
3394 if (equal < 0)
3395 return -1;
3396 if (!equal)
3397 isl_die(isl_map_get_ctx(map), isl_error_unknown,
3398 "result not as expected", return -1);
3399 return 0;
3402 /* Is "set" equal to the set described by "str"?
3404 static isl_bool set_is_equal(__isl_keep isl_set *set, const char *str)
3406 isl_set *set2;
3407 isl_bool equal;
3409 if (!set)
3410 return isl_bool_error;
3412 set2 = isl_set_read_from_str(isl_set_get_ctx(set), str);
3413 equal = isl_set_is_equal(set, set2);
3414 isl_set_free(set2);
3416 return equal;
3419 /* Check that "set" is equal to the set described by "str".
3421 static isl_stat set_check_equal(__isl_keep isl_set *set, const char *str)
3423 isl_bool equal;
3425 equal = set_is_equal(set, str);
3426 if (equal < 0)
3427 return isl_stat_error;
3428 if (!equal)
3429 isl_die(isl_set_get_ctx(set), isl_error_unknown,
3430 "result not as expected", return isl_stat_error);
3431 return isl_stat_ok;
3434 /* Is "uset" equal to the union set described by "str"?
3436 static isl_bool uset_is_equal(__isl_keep isl_union_set *uset, const char *str)
3438 isl_union_set *uset2;
3439 isl_bool equal;
3441 if (!uset)
3442 return isl_bool_error;
3444 uset2 = isl_union_set_read_from_str(isl_union_set_get_ctx(uset), str);
3445 equal = isl_union_set_is_equal(uset, uset2);
3446 isl_union_set_free(uset2);
3448 return equal;
3451 /* Check that "uset" is equal to the union set described by "str".
3453 static isl_stat uset_check_equal(__isl_keep isl_union_set *uset,
3454 const char *str)
3456 isl_bool equal;
3458 equal = uset_is_equal(uset, str);
3459 if (equal < 0)
3460 return isl_stat_error;
3461 if (!equal)
3462 isl_die(isl_union_set_get_ctx(uset), isl_error_unknown,
3463 "result not as expected", return isl_stat_error);
3464 return isl_stat_ok;
3467 static int test_dep(struct isl_ctx *ctx)
3469 const char *str;
3470 isl_space *space;
3471 isl_map *map;
3472 isl_access_info *ai;
3473 isl_flow *flow;
3474 int depth;
3475 struct must_may mm;
3477 depth = 3;
3479 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3480 map = isl_map_read_from_str(ctx, str);
3481 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3483 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3484 map = isl_map_read_from_str(ctx, str);
3485 ai = isl_access_info_add_source(ai, map, 1, &depth);
3487 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3488 map = isl_map_read_from_str(ctx, str);
3489 ai = isl_access_info_add_source(ai, map, 1, &depth);
3491 flow = isl_access_info_compute_flow(ai);
3492 space = isl_space_alloc(ctx, 0, 3, 3);
3493 mm.must = isl_map_empty(isl_space_copy(space));
3494 mm.may = isl_map_empty(space);
3496 isl_flow_foreach(flow, collect_must_may, &mm);
3498 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
3499 " [1,10,0] -> [2,5,0] }";
3500 assert(map_is_equal(mm.must, str));
3501 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3502 assert(map_is_equal(mm.may, str));
3504 isl_map_free(mm.must);
3505 isl_map_free(mm.may);
3506 isl_flow_free(flow);
3509 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3510 map = isl_map_read_from_str(ctx, str);
3511 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3513 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3514 map = isl_map_read_from_str(ctx, str);
3515 ai = isl_access_info_add_source(ai, map, 1, &depth);
3517 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3518 map = isl_map_read_from_str(ctx, str);
3519 ai = isl_access_info_add_source(ai, map, 0, &depth);
3521 flow = isl_access_info_compute_flow(ai);
3522 space = isl_space_alloc(ctx, 0, 3, 3);
3523 mm.must = isl_map_empty(isl_space_copy(space));
3524 mm.may = isl_map_empty(space);
3526 isl_flow_foreach(flow, collect_must_may, &mm);
3528 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
3529 assert(map_is_equal(mm.must, str));
3530 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3531 assert(map_is_equal(mm.may, str));
3533 isl_map_free(mm.must);
3534 isl_map_free(mm.may);
3535 isl_flow_free(flow);
3538 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3539 map = isl_map_read_from_str(ctx, str);
3540 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3542 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3543 map = isl_map_read_from_str(ctx, str);
3544 ai = isl_access_info_add_source(ai, map, 0, &depth);
3546 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3547 map = isl_map_read_from_str(ctx, str);
3548 ai = isl_access_info_add_source(ai, map, 0, &depth);
3550 flow = isl_access_info_compute_flow(ai);
3551 space = isl_space_alloc(ctx, 0, 3, 3);
3552 mm.must = isl_map_empty(isl_space_copy(space));
3553 mm.may = isl_map_empty(space);
3555 isl_flow_foreach(flow, collect_must_may, &mm);
3557 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
3558 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3559 assert(map_is_equal(mm.may, str));
3560 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3561 assert(map_is_equal(mm.must, str));
3563 isl_map_free(mm.must);
3564 isl_map_free(mm.may);
3565 isl_flow_free(flow);
3568 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
3569 map = isl_map_read_from_str(ctx, str);
3570 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3572 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3573 map = isl_map_read_from_str(ctx, str);
3574 ai = isl_access_info_add_source(ai, map, 0, &depth);
3576 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
3577 map = isl_map_read_from_str(ctx, str);
3578 ai = isl_access_info_add_source(ai, map, 0, &depth);
3580 flow = isl_access_info_compute_flow(ai);
3581 space = isl_space_alloc(ctx, 0, 3, 3);
3582 mm.must = isl_map_empty(isl_space_copy(space));
3583 mm.may = isl_map_empty(space);
3585 isl_flow_foreach(flow, collect_must_may, &mm);
3587 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
3588 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
3589 assert(map_is_equal(mm.may, str));
3590 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3591 assert(map_is_equal(mm.must, str));
3593 isl_map_free(mm.must);
3594 isl_map_free(mm.may);
3595 isl_flow_free(flow);
3598 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
3599 map = isl_map_read_from_str(ctx, str);
3600 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3602 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3603 map = isl_map_read_from_str(ctx, str);
3604 ai = isl_access_info_add_source(ai, map, 0, &depth);
3606 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
3607 map = isl_map_read_from_str(ctx, str);
3608 ai = isl_access_info_add_source(ai, map, 0, &depth);
3610 flow = isl_access_info_compute_flow(ai);
3611 space = isl_space_alloc(ctx, 0, 3, 3);
3612 mm.must = isl_map_empty(isl_space_copy(space));
3613 mm.may = isl_map_empty(space);
3615 isl_flow_foreach(flow, collect_must_may, &mm);
3617 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
3618 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
3619 assert(map_is_equal(mm.may, str));
3620 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3621 assert(map_is_equal(mm.must, str));
3623 isl_map_free(mm.must);
3624 isl_map_free(mm.may);
3625 isl_flow_free(flow);
3628 depth = 5;
3630 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3631 map = isl_map_read_from_str(ctx, str);
3632 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
3634 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3635 map = isl_map_read_from_str(ctx, str);
3636 ai = isl_access_info_add_source(ai, map, 1, &depth);
3638 flow = isl_access_info_compute_flow(ai);
3639 space = isl_space_alloc(ctx, 0, 5, 5);
3640 mm.must = isl_map_empty(isl_space_copy(space));
3641 mm.may = isl_map_empty(space);
3643 isl_flow_foreach(flow, collect_must_may, &mm);
3645 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
3646 assert(map_is_equal(mm.must, str));
3647 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
3648 assert(map_is_equal(mm.may, str));
3650 isl_map_free(mm.must);
3651 isl_map_free(mm.may);
3652 isl_flow_free(flow);
3654 return 0;
3657 /* Check that the dependence analysis proceeds without errors.
3658 * Earlier versions of isl would break down during the analysis
3659 * due to the use of the wrong spaces.
3661 static int test_flow(isl_ctx *ctx)
3663 const char *str;
3664 isl_union_map *access, *schedule;
3665 isl_union_map *must_dep, *may_dep;
3666 int r;
3668 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
3669 access = isl_union_map_read_from_str(ctx, str);
3670 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
3671 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
3672 "S2[] -> [1,0,0,0]; "
3673 "S3[] -> [-1,0,0,0] }";
3674 schedule = isl_union_map_read_from_str(ctx, str);
3675 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
3676 isl_union_map_copy(access), schedule,
3677 &must_dep, &may_dep, NULL, NULL);
3678 isl_union_map_free(may_dep);
3679 isl_union_map_free(must_dep);
3681 return r;
3684 struct {
3685 const char *map;
3686 int sv;
3687 } sv_tests[] = {
3688 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
3689 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
3690 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
3691 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
3692 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
3693 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
3694 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3695 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3696 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
3699 int test_sv(isl_ctx *ctx)
3701 isl_union_map *umap;
3702 int i;
3703 int sv;
3705 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
3706 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
3707 sv = isl_union_map_is_single_valued(umap);
3708 isl_union_map_free(umap);
3709 if (sv < 0)
3710 return -1;
3711 if (sv_tests[i].sv && !sv)
3712 isl_die(ctx, isl_error_internal,
3713 "map not detected as single valued", return -1);
3714 if (!sv_tests[i].sv && sv)
3715 isl_die(ctx, isl_error_internal,
3716 "map detected as single valued", return -1);
3719 return 0;
3722 struct {
3723 const char *str;
3724 int bijective;
3725 } bijective_tests[] = {
3726 { "[N,M]->{[i,j] -> [i]}", 0 },
3727 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
3728 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
3729 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
3730 { "[N,M]->{[i,j] -> [j,i]}", 1 },
3731 { "[N,M]->{[i,j] -> [i+j]}", 0 },
3732 { "[N,M]->{[i,j] -> []}", 0 },
3733 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
3734 { "[N,M]->{[i,j] -> [2i]}", 0 },
3735 { "[N,M]->{[i,j] -> [i,i]}", 0 },
3736 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
3737 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
3738 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
3741 static int test_bijective(struct isl_ctx *ctx)
3743 isl_map *map;
3744 int i;
3745 int bijective;
3747 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
3748 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
3749 bijective = isl_map_is_bijective(map);
3750 isl_map_free(map);
3751 if (bijective < 0)
3752 return -1;
3753 if (bijective_tests[i].bijective && !bijective)
3754 isl_die(ctx, isl_error_internal,
3755 "map not detected as bijective", return -1);
3756 if (!bijective_tests[i].bijective && bijective)
3757 isl_die(ctx, isl_error_internal,
3758 "map detected as bijective", return -1);
3761 return 0;
3764 /* Inputs for isl_pw_qpolynomial_gist tests.
3765 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
3767 struct {
3768 const char *pwqp;
3769 const char *set;
3770 const char *gist;
3771 } pwqp_gist_tests[] = {
3772 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
3773 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
3774 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
3775 "{ [i] -> -1/2 + 1/2 * i }" },
3776 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
3777 { "{ [i] -> i^2 : i > 0; [i] -> i^2 : i < 0 }", "{ [i] : i != 0 }",
3778 "{ [i] -> i^2 }" },
3781 /* Perform some basic isl_pw_qpolynomial_gist tests.
3783 static isl_stat test_pwqp_gist(isl_ctx *ctx)
3785 int i;
3786 const char *str;
3787 isl_set *set;
3788 isl_pw_qpolynomial *pwqp1, *pwqp2;
3789 isl_bool equal;
3791 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
3792 str = pwqp_gist_tests[i].pwqp;
3793 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3794 str = pwqp_gist_tests[i].set;
3795 set = isl_set_read_from_str(ctx, str);
3796 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
3797 str = pwqp_gist_tests[i].gist;
3798 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3799 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3800 equal = isl_pw_qpolynomial_is_zero(pwqp1);
3801 isl_pw_qpolynomial_free(pwqp1);
3803 if (equal < 0)
3804 return isl_stat_error;
3805 if (!equal)
3806 isl_die(ctx, isl_error_unknown,
3807 "unexpected result", return isl_stat_error);
3810 return isl_stat_ok;
3813 /* Perform a basic isl_pw_qpolynomial_max test.
3815 static isl_stat test_pwqp_max(isl_ctx *ctx)
3817 const char *str;
3818 isl_pw_qpolynomial *pwqp;
3819 isl_val *v;
3820 int ok;
3822 str = "{ [x=2:9, y] -> floor((x + 1)/4)^3 - floor((2x)/3)^2 }";
3823 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3824 v = isl_pw_qpolynomial_max(pwqp);
3825 ok = isl_val_cmp_si(v, -1) == 0;
3826 isl_val_free(v);
3828 if (!v)
3829 return isl_stat_error;
3830 if (!ok)
3831 isl_die(ctx, isl_error_unknown, "unexpected maximum",
3832 return isl_stat_error);
3834 return isl_stat_ok;
3837 static int test_pwqp(struct isl_ctx *ctx)
3839 const char *str;
3840 isl_set *set;
3841 isl_pw_qpolynomial *pwqp1, *pwqp2;
3842 int equal;
3844 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3845 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3847 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
3848 isl_dim_in, 1, 1);
3850 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3851 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3853 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3855 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3857 isl_pw_qpolynomial_free(pwqp1);
3859 if (test_pwqp_gist(ctx) < 0)
3860 return -1;
3862 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
3863 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3864 str = "{ [i] -> ([(2 * [i/2])/5]) }";
3865 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3867 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3869 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3871 isl_pw_qpolynomial_free(pwqp1);
3873 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3874 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3875 str = "{ [x] -> x }";
3876 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3878 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3880 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3882 isl_pw_qpolynomial_free(pwqp1);
3884 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3885 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3886 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3887 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3888 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3889 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3890 isl_pw_qpolynomial_free(pwqp1);
3892 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3893 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3894 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3895 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3896 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3897 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3898 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3899 isl_pw_qpolynomial_free(pwqp1);
3900 isl_pw_qpolynomial_free(pwqp2);
3901 if (equal < 0)
3902 return -1;
3903 if (!equal)
3904 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3906 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3907 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3908 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3909 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3910 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3911 isl_val_one(ctx));
3912 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3913 isl_pw_qpolynomial_free(pwqp1);
3914 isl_pw_qpolynomial_free(pwqp2);
3915 if (equal < 0)
3916 return -1;
3917 if (!equal)
3918 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3920 if (test_pwqp_max(ctx) < 0)
3921 return -1;
3923 return 0;
3926 static int test_split_periods(isl_ctx *ctx)
3928 const char *str;
3929 isl_pw_qpolynomial *pwqp;
3931 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3932 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3933 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3934 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3936 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3938 isl_pw_qpolynomial_free(pwqp);
3940 if (!pwqp)
3941 return -1;
3943 return 0;
3946 static int test_union(isl_ctx *ctx)
3948 const char *str;
3949 isl_union_set *uset1, *uset2;
3950 isl_union_map *umap1, *umap2;
3951 int equal;
3953 str = "{ [i] : 0 <= i <= 1 }";
3954 uset1 = isl_union_set_read_from_str(ctx, str);
3955 str = "{ [1] -> [0] }";
3956 umap1 = isl_union_map_read_from_str(ctx, str);
3958 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3959 equal = isl_union_map_is_equal(umap1, umap2);
3961 isl_union_map_free(umap1);
3962 isl_union_map_free(umap2);
3964 if (equal < 0)
3965 return -1;
3966 if (!equal)
3967 isl_die(ctx, isl_error_unknown, "union maps not equal",
3968 return -1);
3970 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3971 umap1 = isl_union_map_read_from_str(ctx, str);
3972 str = "{ A[i]; B[i] }";
3973 uset1 = isl_union_set_read_from_str(ctx, str);
3975 uset2 = isl_union_map_domain(umap1);
3977 equal = isl_union_set_is_equal(uset1, uset2);
3979 isl_union_set_free(uset1);
3980 isl_union_set_free(uset2);
3982 if (equal < 0)
3983 return -1;
3984 if (!equal)
3985 isl_die(ctx, isl_error_unknown, "union sets not equal",
3986 return -1);
3988 return 0;
3991 /* Inputs for basic isl_pw_qpolynomial_bound tests.
3992 * "type" is the type of bound that should be computed.
3993 * "poly" is a string representation of the input.
3994 * "bound" is a string representation of the expected result.
3995 * "tight" is set if the result is expected to be tight.
3997 static struct {
3998 int tight;
3999 enum isl_fold type;
4000 const char *poly;
4001 const char *bound;
4002 } bound_tests[] = {
4003 /* Check that computing a bound of a non-zero polynomial
4004 * over an unbounded domain does not produce a rational value.
4005 * In particular, check that the upper bound is infinity.
4007 { 0, isl_fold_max, "{ [m, n] -> -m * n }", "{ max(infty) }" },
4008 { 1, isl_fold_max, "{ [[a, b, c, d] -> [e]] -> 0 }",
4009 "{ [a, b, c, d] -> max(0) }" },
4010 { 1, isl_fold_max, "{ [[x] -> [x]] -> 1 : exists a : x = 2 a }",
4011 "{ [x] -> max(1) : x mod 2 = 0 }" },
4012 { 1, isl_fold_min, "{ [x=5:10] -> (x + 2)^2 }", "{ min(49) }" },
4013 { 1, isl_fold_max, "{ [0:10] -> 1 }", "{ max(1) }" },
4014 { 1, isl_fold_max, "{ [[m] -> [0:m]] -> m^2 }",
4015 "{ [m] -> max(m^2) : m >= 0 }" },
4018 /* Check that the bound computation can handle differences
4019 * in domain dimension names of the input polynomial and its domain.
4021 static isl_stat test_bound_space(isl_ctx *ctx)
4023 const char *str;
4024 isl_set *set;
4025 isl_pw_qpolynomial *pwqp;
4026 isl_pw_qpolynomial_fold *pwf;
4028 str = "{ [[c] -> [c]] }";
4029 set = isl_set_read_from_str(ctx, str);
4030 str = "{ [[a] -> [b]] -> 1 }";
4031 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
4032 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
4033 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
4034 isl_pw_qpolynomial_fold_free(pwf);
4036 return isl_stat_non_null(pwf);
4039 /* Perform basic isl_pw_qpolynomial_bound tests.
4041 static int test_bound(isl_ctx *ctx)
4043 int i;
4045 if (test_bound_space(ctx) < 0)
4046 return -1;
4048 for (i = 0; i < ARRAY_SIZE(bound_tests); ++i) {
4049 const char *str;
4050 enum isl_fold type;
4051 isl_bool equal, tight;
4052 isl_pw_qpolynomial *pwqp;
4053 isl_pw_qpolynomial_fold *pwf1, *pwf2;
4055 str = bound_tests[i].poly;
4056 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
4057 type = bound_tests[i].type;
4058 pwf1 = isl_pw_qpolynomial_bound(pwqp, type, &tight);
4059 str = bound_tests[i].bound;
4060 pwf2 = isl_pw_qpolynomial_fold_read_from_str(ctx, str);
4061 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf1, pwf2);
4062 isl_pw_qpolynomial_fold_free(pwf2);
4063 isl_pw_qpolynomial_fold_free(pwf1);
4064 if (equal < 0)
4065 return -1;
4066 if (!equal)
4067 isl_die(ctx, isl_error_unknown,
4068 "incorrect bound result", return -1);
4069 if (bound_tests[i].tight && !tight)
4070 isl_die(ctx, isl_error_unknown,
4071 "bound unexpectedly not tight", return -1);
4074 return 0;
4077 /* isl_set is defined to isl_map internally, so the corresponding elements
4078 * are isl_basic_map objects.
4080 #undef EL_BASE
4081 #undef SET_BASE
4082 #define EL_BASE basic_map
4083 #define SET_BASE set
4084 #include "isl_test_list_templ.c"
4086 #undef EL_BASE
4087 #undef SET_BASE
4088 #define EL_BASE basic_set
4089 #define SET_BASE union_set
4090 #include "isl_test_list_templ.c"
4092 #undef EL_BASE
4093 #undef SET_BASE
4094 #define EL_BASE set
4095 #define SET_BASE union_set
4096 #include "isl_test_list_templ.c"
4098 #undef EL_BASE
4099 #undef SET_BASE
4100 #define EL_BASE basic_map
4101 #define SET_BASE map
4102 #include "isl_test_list_templ.c"
4104 #undef EL_BASE
4105 #undef SET_BASE
4106 #define EL_BASE map
4107 #define SET_BASE union_map
4108 #include "isl_test_list_templ.c"
4110 /* Check that the conversion from isl objects to lists works as expected.
4112 static int test_get_list(isl_ctx *ctx)
4114 if (test_get_list_basic_map_from_set(ctx, "{ [0]; [2]; [3] }"))
4115 return -1;
4116 if (test_get_list_basic_set_from_union_set(ctx, "{ A[0]; B[2]; B[3] }"))
4117 return -1;
4118 if (test_get_list_set_from_union_set(ctx, "{ A[0]; A[2]; B[3] }"))
4119 return -1;
4120 if (test_get_list_basic_map_from_map(ctx,
4121 "{ [0] -> [0]; [2] -> [0]; [3] -> [0] }"))
4122 return -1;
4123 if (test_get_list_map_from_union_map(ctx,
4124 "{ A[0] -> [0]; A[2] -> [0]; B[3] -> [0] }"))
4125 return -1;
4127 return 0;
4130 static int test_lift(isl_ctx *ctx)
4132 const char *str;
4133 isl_basic_map *bmap;
4134 isl_basic_set *bset;
4136 str = "{ [i0] : exists e0 : i0 = 4e0 }";
4137 bset = isl_basic_set_read_from_str(ctx, str);
4138 bset = isl_basic_set_lift(bset);
4139 bmap = isl_basic_map_from_range(bset);
4140 bset = isl_basic_map_domain(bmap);
4141 isl_basic_set_free(bset);
4143 return 0;
4146 /* Check that isl_set_is_subset is not confused by identical
4147 * integer divisions.
4148 * The call to isl_set_normalize ensures that the equality constraints
4149 * a = b = 0 are discovered, turning e0 and e1 into identical
4150 * integer divisions. Any further simplification would remove
4151 * the duplicate integer divisions.
4153 static isl_stat test_subset_duplicate_integer_divisions(isl_ctx *ctx)
4155 const char *str;
4156 isl_bool is_subset;
4157 isl_set *set1, *set2;
4159 str = "{ [a, b, c, d] : "
4160 "exists (e0 = floor((a + d)/4), e1 = floor((d)/4), "
4161 "e2 = floor((-a - d + 4 *floor((a + d)/4))/10), "
4162 "e3 = floor((-d + 4*floor((d)/4))/10): "
4163 "10e2 = -a - 2c - d + 4e0 and 10e3 = -2c - d + 4e1 and "
4164 "b >= 0 and a <= 0 and b <= a) }";
4165 set1 = isl_set_read_from_str(ctx, str);
4166 set2 = isl_set_read_from_str(ctx, str);
4167 set2 = isl_set_normalize(set2);
4169 is_subset = isl_set_is_subset(set1, set2);
4171 isl_set_free(set1);
4172 isl_set_free(set2);
4174 if (is_subset < 0)
4175 return isl_stat_error;
4176 if (!is_subset)
4177 isl_die(ctx, isl_error_unknown,
4178 "set is not considered to be a subset of itself",
4179 return isl_stat_error);
4181 return isl_stat_ok;
4184 struct {
4185 const char *set1;
4186 const char *set2;
4187 int subset;
4188 } subset_tests[] = {
4189 { "{ [112, 0] }",
4190 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
4191 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
4192 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
4193 { "{ [65] }",
4194 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
4195 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
4196 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
4197 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
4198 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
4199 "256e0 <= 255i and 256e0 >= -255 + 255i and "
4200 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
4201 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
4202 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
4203 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
4204 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
4205 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
4206 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
4207 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
4208 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
4209 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
4210 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
4211 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
4212 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
4213 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
4214 "4e0 <= -57 + i0 + i1)) or "
4215 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
4216 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
4217 "4e0 >= -61 + i0 + i1)) or "
4218 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
4219 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
4222 static int test_subset(isl_ctx *ctx)
4224 int i;
4225 isl_set *set1, *set2;
4226 int subset;
4228 if (test_subset_duplicate_integer_divisions(ctx) < 0)
4229 return -1;
4231 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
4232 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
4233 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
4234 subset = isl_set_is_subset(set1, set2);
4235 isl_set_free(set1);
4236 isl_set_free(set2);
4237 if (subset < 0)
4238 return -1;
4239 if (subset != subset_tests[i].subset)
4240 isl_die(ctx, isl_error_unknown,
4241 "incorrect subset result", return -1);
4244 return 0;
4247 /* Perform a set subtraction with a set that has a non-obviously empty disjunct.
4248 * Older versions of isl would fail on such cases.
4250 static isl_stat test_subtract_empty(isl_ctx *ctx)
4252 const char *str;
4253 isl_set *s1, *s2;
4255 s1 = isl_set_read_from_str(ctx, "{ [0] }");
4256 str = "{ [a] : (exists (e0, e1, e2: 1056e1 <= 32 + a - 33e0 and "
4257 "1089e1 >= a - 33e0 and 1089e1 <= 1 + a - 33e0 and "
4258 "33e2 >= -a + 33e0 + 1056e1 and "
4259 "33e2 < -2a + 66e0 + 2112e1)) or a = 0 }";
4260 s2 = isl_set_read_from_str(ctx, str);
4261 s1 = isl_set_subtract(s1, s2);
4262 isl_set_free(s1);
4264 return isl_stat_non_null(s1);
4267 struct {
4268 const char *minuend;
4269 const char *subtrahend;
4270 const char *difference;
4271 } subtract_domain_tests[] = {
4272 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
4273 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
4274 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
4277 static int test_subtract(isl_ctx *ctx)
4279 int i;
4280 isl_union_map *umap1, *umap2;
4281 isl_union_pw_multi_aff *upma1, *upma2;
4282 isl_union_set *uset;
4283 int equal;
4285 if (test_subtract_empty(ctx) < 0)
4286 return -1;
4288 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
4289 umap1 = isl_union_map_read_from_str(ctx,
4290 subtract_domain_tests[i].minuend);
4291 uset = isl_union_set_read_from_str(ctx,
4292 subtract_domain_tests[i].subtrahend);
4293 umap2 = isl_union_map_read_from_str(ctx,
4294 subtract_domain_tests[i].difference);
4295 umap1 = isl_union_map_subtract_domain(umap1, uset);
4296 equal = isl_union_map_is_equal(umap1, umap2);
4297 isl_union_map_free(umap1);
4298 isl_union_map_free(umap2);
4299 if (equal < 0)
4300 return -1;
4301 if (!equal)
4302 isl_die(ctx, isl_error_unknown,
4303 "incorrect subtract domain result", return -1);
4306 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
4307 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4308 subtract_domain_tests[i].minuend);
4309 uset = isl_union_set_read_from_str(ctx,
4310 subtract_domain_tests[i].subtrahend);
4311 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4312 subtract_domain_tests[i].difference);
4313 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
4314 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
4315 isl_union_pw_multi_aff_free(upma1);
4316 isl_union_pw_multi_aff_free(upma2);
4317 if (equal < 0)
4318 return -1;
4319 if (!equal)
4320 isl_die(ctx, isl_error_unknown,
4321 "incorrect subtract domain result", return -1);
4324 return 0;
4327 /* Check that intersecting the empty basic set with another basic set
4328 * does not increase the number of constraints. In particular,
4329 * the empty basic set should maintain its canonical representation.
4331 static int test_intersect_1(isl_ctx *ctx)
4333 isl_size n1, n2;
4334 isl_basic_set *bset1, *bset2;
4336 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
4337 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
4338 n1 = isl_basic_set_n_constraint(bset1);
4339 bset1 = isl_basic_set_intersect(bset1, bset2);
4340 n2 = isl_basic_set_n_constraint(bset1);
4341 isl_basic_set_free(bset1);
4342 if (n1 < 0 || n2 < 0)
4343 return -1;
4344 if (n1 != n2)
4345 isl_die(ctx, isl_error_unknown,
4346 "number of constraints of empty set changed",
4347 return -1);
4349 return 0;
4352 /* Check that intersecting a set with itself does not cause
4353 * an explosion in the number of disjuncts.
4355 static isl_stat test_intersect_2(isl_ctx *ctx)
4357 int i;
4358 isl_set *set;
4360 set = isl_set_read_from_str(ctx, "{ [x,y] : x >= 0 or y >= 0 }");
4361 for (i = 0; i < 100; ++i)
4362 set = isl_set_intersect(set, isl_set_copy(set));
4363 isl_set_free(set);
4364 if (!set)
4365 return isl_stat_error;
4366 return isl_stat_ok;
4369 /* Perform some intersection tests.
4371 static int test_intersect(isl_ctx *ctx)
4373 if (test_intersect_1(ctx) < 0)
4374 return -1;
4375 if (test_intersect_2(ctx) < 0)
4376 return -1;
4378 return 0;
4381 int test_factorize(isl_ctx *ctx)
4383 const char *str;
4384 isl_basic_set *bset;
4385 isl_factorizer *f;
4387 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
4388 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
4389 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
4390 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
4391 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
4392 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
4393 "3i5 >= -2i0 - i2 + 3i4 }";
4394 bset = isl_basic_set_read_from_str(ctx, str);
4395 f = isl_basic_set_factorizer(bset);
4396 isl_basic_set_free(bset);
4397 isl_factorizer_free(f);
4398 if (!f)
4399 isl_die(ctx, isl_error_unknown,
4400 "failed to construct factorizer", return -1);
4402 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
4403 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
4404 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
4405 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
4406 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
4407 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
4408 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
4409 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
4410 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
4411 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
4412 bset = isl_basic_set_read_from_str(ctx, str);
4413 f = isl_basic_set_factorizer(bset);
4414 isl_basic_set_free(bset);
4415 isl_factorizer_free(f);
4416 if (!f)
4417 isl_die(ctx, isl_error_unknown,
4418 "failed to construct factorizer", return -1);
4420 return 0;
4423 static isl_stat check_injective(__isl_take isl_map *map, void *user)
4425 int *injective = user;
4427 *injective = isl_map_is_injective(map);
4428 isl_map_free(map);
4430 if (*injective < 0 || !*injective)
4431 return isl_stat_error;
4433 return isl_stat_ok;
4436 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
4437 const char *r, const char *s, int tilable, int parallel)
4439 int i;
4440 isl_union_set *D;
4441 isl_union_map *W, *R, *S;
4442 isl_union_map *empty;
4443 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
4444 isl_union_map *validity, *proximity, *coincidence;
4445 isl_union_map *schedule;
4446 isl_union_map *test;
4447 isl_union_set *delta;
4448 isl_union_set *domain;
4449 isl_set *delta_set;
4450 isl_set *slice;
4451 isl_set *origin;
4452 isl_schedule_constraints *sc;
4453 isl_schedule *sched;
4454 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
4455 isl_size n;
4457 D = isl_union_set_read_from_str(ctx, d);
4458 W = isl_union_map_read_from_str(ctx, w);
4459 R = isl_union_map_read_from_str(ctx, r);
4460 S = isl_union_map_read_from_str(ctx, s);
4462 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
4463 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
4465 empty = isl_union_map_empty(isl_union_map_get_space(S));
4466 isl_union_map_compute_flow(isl_union_map_copy(R),
4467 isl_union_map_copy(W), empty,
4468 isl_union_map_copy(S),
4469 &dep_raw, NULL, NULL, NULL);
4470 isl_union_map_compute_flow(isl_union_map_copy(W),
4471 isl_union_map_copy(W),
4472 isl_union_map_copy(R),
4473 isl_union_map_copy(S),
4474 &dep_waw, &dep_war, NULL, NULL);
4476 dep = isl_union_map_union(dep_waw, dep_war);
4477 dep = isl_union_map_union(dep, dep_raw);
4478 validity = isl_union_map_copy(dep);
4479 coincidence = isl_union_map_copy(dep);
4480 proximity = isl_union_map_copy(dep);
4482 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
4483 sc = isl_schedule_constraints_set_validity(sc, validity);
4484 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
4485 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4486 sched = isl_schedule_constraints_compute_schedule(sc);
4487 schedule = isl_schedule_get_map(sched);
4488 isl_schedule_free(sched);
4489 isl_union_map_free(W);
4490 isl_union_map_free(R);
4491 isl_union_map_free(S);
4493 is_injection = 1;
4494 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
4496 domain = isl_union_map_domain(isl_union_map_copy(schedule));
4497 is_complete = isl_union_set_is_subset(D, domain);
4498 isl_union_set_free(D);
4499 isl_union_set_free(domain);
4501 test = isl_union_map_reverse(isl_union_map_copy(schedule));
4502 test = isl_union_map_apply_range(test, dep);
4503 test = isl_union_map_apply_range(test, schedule);
4505 delta = isl_union_map_deltas(test);
4506 n = isl_union_set_n_set(delta);
4507 if (n < 0) {
4508 isl_union_set_free(delta);
4509 return -1;
4511 if (n == 0) {
4512 is_tilable = 1;
4513 is_parallel = 1;
4514 is_nonneg = 1;
4515 isl_union_set_free(delta);
4516 } else {
4517 isl_size dim;
4519 delta_set = isl_set_from_union_set(delta);
4521 slice = isl_set_universe(isl_set_get_space(delta_set));
4522 for (i = 0; i < tilable; ++i)
4523 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
4524 is_tilable = isl_set_is_subset(delta_set, slice);
4525 isl_set_free(slice);
4527 slice = isl_set_universe(isl_set_get_space(delta_set));
4528 for (i = 0; i < parallel; ++i)
4529 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
4530 is_parallel = isl_set_is_subset(delta_set, slice);
4531 isl_set_free(slice);
4533 origin = isl_set_universe(isl_set_get_space(delta_set));
4534 dim = isl_set_dim(origin, isl_dim_set);
4535 if (dim < 0)
4536 origin = isl_set_free(origin);
4537 for (i = 0; i < dim; ++i)
4538 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
4540 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
4541 delta_set = isl_set_lexmin(delta_set);
4543 is_nonneg = isl_set_is_equal(delta_set, origin);
4545 isl_set_free(origin);
4546 isl_set_free(delta_set);
4549 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
4550 is_injection < 0 || is_complete < 0)
4551 return -1;
4552 if (!is_complete)
4553 isl_die(ctx, isl_error_unknown,
4554 "generated schedule incomplete", return -1);
4555 if (!is_injection)
4556 isl_die(ctx, isl_error_unknown,
4557 "generated schedule not injective on each statement",
4558 return -1);
4559 if (!is_nonneg)
4560 isl_die(ctx, isl_error_unknown,
4561 "negative dependences in generated schedule",
4562 return -1);
4563 if (!is_tilable)
4564 isl_die(ctx, isl_error_unknown,
4565 "generated schedule not as tilable as expected",
4566 return -1);
4567 if (!is_parallel)
4568 isl_die(ctx, isl_error_unknown,
4569 "generated schedule not as parallel as expected",
4570 return -1);
4572 return 0;
4575 /* Compute a schedule for the given instance set, validity constraints,
4576 * proximity constraints and context and return a corresponding union map
4577 * representation.
4579 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
4580 const char *domain, const char *validity, const char *proximity,
4581 const char *context)
4583 isl_set *con;
4584 isl_union_set *dom;
4585 isl_union_map *dep;
4586 isl_union_map *prox;
4587 isl_schedule_constraints *sc;
4588 isl_schedule *schedule;
4589 isl_union_map *sched;
4591 con = isl_set_read_from_str(ctx, context);
4592 dom = isl_union_set_read_from_str(ctx, domain);
4593 dep = isl_union_map_read_from_str(ctx, validity);
4594 prox = isl_union_map_read_from_str(ctx, proximity);
4595 sc = isl_schedule_constraints_on_domain(dom);
4596 sc = isl_schedule_constraints_set_context(sc, con);
4597 sc = isl_schedule_constraints_set_validity(sc, dep);
4598 sc = isl_schedule_constraints_set_proximity(sc, prox);
4599 schedule = isl_schedule_constraints_compute_schedule(sc);
4600 sched = isl_schedule_get_map(schedule);
4601 isl_schedule_free(schedule);
4603 return sched;
4606 /* Compute a schedule for the given instance set, validity constraints and
4607 * proximity constraints and return a corresponding union map representation.
4609 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
4610 const char *domain, const char *validity, const char *proximity)
4612 return compute_schedule_with_context(ctx, domain, validity, proximity,
4613 "{ : }");
4616 /* Check that a schedule can be constructed on the given domain
4617 * with the given validity and proximity constraints.
4619 static int test_has_schedule(isl_ctx *ctx, const char *domain,
4620 const char *validity, const char *proximity)
4622 isl_union_map *sched;
4624 sched = compute_schedule(ctx, domain, validity, proximity);
4625 if (!sched)
4626 return -1;
4628 isl_union_map_free(sched);
4629 return 0;
4632 int test_special_schedule(isl_ctx *ctx, const char *domain,
4633 const char *validity, const char *proximity, const char *expected_sched)
4635 isl_union_map *sched1, *sched2;
4636 int equal;
4638 sched1 = compute_schedule(ctx, domain, validity, proximity);
4639 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
4641 equal = isl_union_map_is_equal(sched1, sched2);
4642 isl_union_map_free(sched1);
4643 isl_union_map_free(sched2);
4645 if (equal < 0)
4646 return -1;
4647 if (!equal)
4648 isl_die(ctx, isl_error_unknown, "unexpected schedule",
4649 return -1);
4651 return 0;
4654 /* Check that the schedule map is properly padded, i.e., that the range
4655 * lives in a single space.
4657 static int test_padded_schedule(isl_ctx *ctx)
4659 const char *str;
4660 isl_union_set *D;
4661 isl_union_map *validity, *proximity;
4662 isl_schedule_constraints *sc;
4663 isl_schedule *sched;
4664 isl_union_map *umap;
4665 isl_union_set *range;
4666 isl_set *set;
4668 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
4669 D = isl_union_set_read_from_str(ctx, str);
4670 validity = isl_union_map_empty(isl_union_set_get_space(D));
4671 proximity = isl_union_map_copy(validity);
4672 sc = isl_schedule_constraints_on_domain(D);
4673 sc = isl_schedule_constraints_set_validity(sc, validity);
4674 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4675 sched = isl_schedule_constraints_compute_schedule(sc);
4676 umap = isl_schedule_get_map(sched);
4677 isl_schedule_free(sched);
4678 range = isl_union_map_range(umap);
4679 set = isl_set_from_union_set(range);
4680 isl_set_free(set);
4682 if (!set)
4683 return -1;
4685 return 0;
4688 /* Check that conditional validity constraints are also taken into
4689 * account across bands.
4690 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
4691 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
4692 * and then check that the adjacent order constraint C[2,1]->D[2,0]
4693 * is enforced by the rest of the schedule.
4695 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
4697 const char *str;
4698 isl_union_set *domain;
4699 isl_union_map *validity, *proximity, *condition;
4700 isl_union_map *sink, *source, *dep;
4701 isl_schedule_constraints *sc;
4702 isl_schedule *schedule;
4703 isl_union_access_info *access;
4704 isl_union_flow *flow;
4705 int empty;
4707 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4708 "A[k] : k >= 1 and k <= -1 + n; "
4709 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4710 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
4711 domain = isl_union_set_read_from_str(ctx, str);
4712 sc = isl_schedule_constraints_on_domain(domain);
4713 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
4714 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4715 "D[k, i] -> C[1 + k, i] : "
4716 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4717 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
4718 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
4719 validity = isl_union_map_read_from_str(ctx, str);
4720 sc = isl_schedule_constraints_set_validity(sc, validity);
4721 str = "[n] -> { C[k, i] -> D[k, i] : "
4722 "0 <= i <= -1 + k and k <= -1 + n }";
4723 proximity = isl_union_map_read_from_str(ctx, str);
4724 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4725 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
4726 "i <= -1 + k and i >= 1 and k <= -2 + n; "
4727 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
4728 "k <= -1 + n and i >= 0 and i <= -2 + k }";
4729 condition = isl_union_map_read_from_str(ctx, str);
4730 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
4731 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4732 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
4733 "i >= 0 and i <= -1 + k and k <= -1 + n and "
4734 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
4735 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
4736 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4737 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
4738 "k <= -1 + n and i >= 0 and i <= -1 + k and "
4739 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
4740 validity = isl_union_map_read_from_str(ctx, str);
4741 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4742 validity);
4743 schedule = isl_schedule_constraints_compute_schedule(sc);
4744 str = "{ D[2,0] -> [] }";
4745 sink = isl_union_map_read_from_str(ctx, str);
4746 access = isl_union_access_info_from_sink(sink);
4747 str = "{ C[2,1] -> [] }";
4748 source = isl_union_map_read_from_str(ctx, str);
4749 access = isl_union_access_info_set_must_source(access, source);
4750 access = isl_union_access_info_set_schedule(access, schedule);
4751 flow = isl_union_access_info_compute_flow(access);
4752 dep = isl_union_flow_get_must_dependence(flow);
4753 isl_union_flow_free(flow);
4754 empty = isl_union_map_is_empty(dep);
4755 isl_union_map_free(dep);
4757 if (empty < 0)
4758 return -1;
4759 if (empty)
4760 isl_die(ctx, isl_error_unknown,
4761 "conditional validity not respected", return -1);
4763 return 0;
4766 /* Check that the test for violated conditional validity constraints
4767 * is not confused by domain compression.
4768 * In particular, earlier versions of isl would apply
4769 * a schedule on the compressed domains to the original domains,
4770 * resulting in a failure to detect that the default schedule
4771 * violates the conditional validity constraints.
4773 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
4775 const char *str;
4776 isl_bool empty;
4777 isl_union_set *domain;
4778 isl_union_map *validity, *condition;
4779 isl_schedule_constraints *sc;
4780 isl_schedule *schedule;
4781 isl_union_map *umap;
4782 isl_map *map, *ge;
4784 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
4785 domain = isl_union_set_read_from_str(ctx, str);
4786 sc = isl_schedule_constraints_on_domain(domain);
4787 str = "{ B[1, i] -> A[0, i + 1] }";
4788 condition = isl_union_map_read_from_str(ctx, str);
4789 str = "{ A[0, i] -> B[1, i - 1] }";
4790 validity = isl_union_map_read_from_str(ctx, str);
4791 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4792 isl_union_map_copy(validity));
4793 schedule = isl_schedule_constraints_compute_schedule(sc);
4794 umap = isl_schedule_get_map(schedule);
4795 isl_schedule_free(schedule);
4796 validity = isl_union_map_apply_domain(validity,
4797 isl_union_map_copy(umap));
4798 validity = isl_union_map_apply_range(validity, umap);
4799 map = isl_map_from_union_map(validity);
4800 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4801 map = isl_map_intersect(map, ge);
4802 empty = isl_map_is_empty(map);
4803 isl_map_free(map);
4805 if (empty < 0)
4806 return -1;
4807 if (!empty)
4808 isl_die(ctx, isl_error_unknown,
4809 "conditional validity constraints not satisfied",
4810 return -1);
4812 return 0;
4815 /* Input for testing of schedule construction based on
4816 * conditional constraints.
4818 * domain is the iteration domain
4819 * flow are the flow dependences, which determine the validity and
4820 * proximity constraints
4821 * condition are the conditions on the conditional validity constraints
4822 * conditional_validity are the conditional validity constraints
4823 * outer_band_n is the expected number of members in the outer band
4825 struct {
4826 const char *domain;
4827 const char *flow;
4828 const char *condition;
4829 const char *conditional_validity;
4830 int outer_band_n;
4831 } live_range_tests[] = {
4832 /* Contrived example that illustrates that we need to keep
4833 * track of tagged condition dependences and
4834 * tagged conditional validity dependences
4835 * in isl_sched_edge separately.
4836 * In particular, the conditional validity constraints on A
4837 * cannot be satisfied,
4838 * but they can be ignored because there are no corresponding
4839 * condition constraints. However, we do have an additional
4840 * conditional validity constraint that maps to the same
4841 * dependence relation
4842 * as the condition constraint on B. If we did not make a distinction
4843 * between tagged condition and tagged conditional validity
4844 * dependences, then we
4845 * could end up treating this shared dependence as an condition
4846 * constraint on A, forcing a localization of the conditions,
4847 * which is impossible.
4849 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
4850 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
4851 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
4852 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4853 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4854 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
4857 /* TACO 2013 Fig. 7 */
4858 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4859 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4860 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4861 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
4862 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
4863 "0 <= i < n and 0 <= j < n - 1 }",
4864 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
4865 "0 <= i < n and 0 <= j < j' < n;"
4866 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
4867 "0 <= i < i' < n and 0 <= j,j' < n;"
4868 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
4869 "0 <= i,j,j' < n and j < j' }",
4872 /* TACO 2013 Fig. 7, without tags */
4873 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4874 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4875 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4876 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4877 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4878 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
4879 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
4880 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
4883 /* TACO 2013 Fig. 12 */
4884 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
4885 "S3[i,3] : 0 <= i <= 1 }",
4886 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
4887 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
4888 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
4889 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
4890 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4891 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
4892 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4893 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
4894 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
4895 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
4896 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
4901 /* Test schedule construction based on conditional constraints.
4902 * In particular, check the number of members in the outer band node
4903 * as an indication of whether tiling is possible or not.
4905 static int test_conditional_schedule_constraints(isl_ctx *ctx)
4907 int i;
4908 isl_union_set *domain;
4909 isl_union_map *condition;
4910 isl_union_map *flow;
4911 isl_union_map *validity;
4912 isl_schedule_constraints *sc;
4913 isl_schedule *schedule;
4914 isl_schedule_node *node;
4915 isl_size n_member;
4917 if (test_special_conditional_schedule_constraints(ctx) < 0)
4918 return -1;
4919 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
4920 return -1;
4922 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
4923 domain = isl_union_set_read_from_str(ctx,
4924 live_range_tests[i].domain);
4925 flow = isl_union_map_read_from_str(ctx,
4926 live_range_tests[i].flow);
4927 condition = isl_union_map_read_from_str(ctx,
4928 live_range_tests[i].condition);
4929 validity = isl_union_map_read_from_str(ctx,
4930 live_range_tests[i].conditional_validity);
4931 sc = isl_schedule_constraints_on_domain(domain);
4932 sc = isl_schedule_constraints_set_validity(sc,
4933 isl_union_map_copy(flow));
4934 sc = isl_schedule_constraints_set_proximity(sc, flow);
4935 sc = isl_schedule_constraints_set_conditional_validity(sc,
4936 condition, validity);
4937 schedule = isl_schedule_constraints_compute_schedule(sc);
4938 node = isl_schedule_get_root(schedule);
4939 while (node &&
4940 isl_schedule_node_get_type(node) != isl_schedule_node_band)
4941 node = isl_schedule_node_first_child(node);
4942 n_member = isl_schedule_node_band_n_member(node);
4943 isl_schedule_node_free(node);
4944 isl_schedule_free(schedule);
4946 if (!schedule || n_member < 0)
4947 return -1;
4948 if (n_member != live_range_tests[i].outer_band_n)
4949 isl_die(ctx, isl_error_unknown,
4950 "unexpected number of members in outer band",
4951 return -1);
4953 return 0;
4956 /* Check that the schedule computed for the given instance set and
4957 * dependence relation strongly satisfies the dependences.
4958 * In particular, check that no instance is scheduled before
4959 * or together with an instance on which it depends.
4960 * Earlier versions of isl would produce a schedule that
4961 * only weakly satisfies the dependences.
4963 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
4965 const char *domain, *dep;
4966 isl_union_map *D, *schedule;
4967 isl_map *map, *ge;
4968 int empty;
4970 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
4971 "A[i0] : 0 <= i0 <= 1 }";
4972 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
4973 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
4974 schedule = compute_schedule(ctx, domain, dep, dep);
4975 D = isl_union_map_read_from_str(ctx, dep);
4976 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
4977 D = isl_union_map_apply_range(D, schedule);
4978 map = isl_map_from_union_map(D);
4979 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4980 map = isl_map_intersect(map, ge);
4981 empty = isl_map_is_empty(map);
4982 isl_map_free(map);
4984 if (empty < 0)
4985 return -1;
4986 if (!empty)
4987 isl_die(ctx, isl_error_unknown,
4988 "dependences not strongly satisfied", return -1);
4990 return 0;
4993 /* Compute a schedule for input where the instance set constraints
4994 * conflict with the context constraints.
4995 * Earlier versions of isl did not properly handle this situation.
4997 static int test_conflicting_context_schedule(isl_ctx *ctx)
4999 isl_union_map *schedule;
5000 const char *domain, *context;
5002 domain = "[n] -> { A[] : n >= 0 }";
5003 context = "[n] -> { : n < 0 }";
5004 schedule = compute_schedule_with_context(ctx,
5005 domain, "{}", "{}", context);
5006 isl_union_map_free(schedule);
5008 if (!schedule)
5009 return -1;
5011 return 0;
5014 /* Check that a set of schedule constraints that only allow for
5015 * a coalescing schedule still produces a schedule even if the user
5016 * request a non-coalescing schedule. Earlier versions of isl
5017 * would not handle this case correctly.
5019 static int test_coalescing_schedule(isl_ctx *ctx)
5021 const char *domain, *dep;
5022 isl_union_set *I;
5023 isl_union_map *D;
5024 isl_schedule_constraints *sc;
5025 isl_schedule *schedule;
5026 int treat_coalescing;
5028 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
5029 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
5030 I = isl_union_set_read_from_str(ctx, domain);
5031 D = isl_union_map_read_from_str(ctx, dep);
5032 sc = isl_schedule_constraints_on_domain(I);
5033 sc = isl_schedule_constraints_set_validity(sc, D);
5034 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
5035 isl_options_set_schedule_treat_coalescing(ctx, 1);
5036 schedule = isl_schedule_constraints_compute_schedule(sc);
5037 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
5038 isl_schedule_free(schedule);
5039 if (!schedule)
5040 return -1;
5041 return 0;
5044 /* Check that the scheduler does not perform any needless
5045 * compound skewing. Earlier versions of isl would compute
5046 * schedules in terms of transformed schedule coefficients and
5047 * would not accurately keep track of the sum of the original
5048 * schedule coefficients. It could then produce the schedule
5049 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
5050 * for the input below instead of the schedule below.
5052 static int test_skewing_schedule(isl_ctx *ctx)
5054 const char *D, *V, *P, *S;
5056 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
5057 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
5058 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
5059 "-1 <= a-i + b-j + c-k <= 1 }";
5060 P = "{ }";
5061 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
5063 return test_special_schedule(ctx, D, V, P, S);
5066 int test_schedule(isl_ctx *ctx)
5068 const char *D, *W, *R, *V, *P, *S;
5069 int max_coincidence;
5070 int treat_coalescing;
5072 /* Handle resulting schedule with zero bands. */
5073 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
5074 return -1;
5076 /* Jacobi */
5077 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
5078 W = "{ S1[t,i] -> a[t,i] }";
5079 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
5080 "S1[t,i] -> a[t-1,i+1] }";
5081 S = "{ S1[t,i] -> [t,i] }";
5082 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
5083 return -1;
5085 /* Fig. 5 of CC2008 */
5086 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
5087 "j <= -1 + N }";
5088 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
5089 "j >= 2 and j <= -1 + N }";
5090 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
5091 "j >= 2 and j <= -1 + N; "
5092 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
5093 "j >= 2 and j <= -1 + N }";
5094 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
5095 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
5096 return -1;
5098 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
5099 W = "{ S1[i] -> a[i] }";
5100 R = "{ S2[i] -> a[i+1] }";
5101 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
5102 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
5103 return -1;
5105 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
5106 W = "{ S1[i] -> a[i] }";
5107 R = "{ S2[i] -> a[9-i] }";
5108 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
5109 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
5110 return -1;
5112 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
5113 W = "{ S1[i] -> a[i] }";
5114 R = "[N] -> { S2[i] -> a[N-1-i] }";
5115 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
5116 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
5117 return -1;
5119 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
5120 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
5121 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
5122 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
5123 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5124 return -1;
5126 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
5127 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
5128 R = "{ S2[i,j] -> a[i-1,j] }";
5129 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
5130 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
5131 return -1;
5133 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
5134 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
5135 R = "{ S2[i,j] -> a[i,j-1] }";
5136 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
5137 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
5138 return -1;
5140 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
5141 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
5142 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
5143 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
5144 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
5145 "S_0[] -> [0, 0, 0] }";
5146 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
5147 return -1;
5148 ctx->opt->schedule_parametric = 0;
5149 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5150 return -1;
5151 ctx->opt->schedule_parametric = 1;
5153 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
5154 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
5155 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
5156 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
5157 "S4[i] -> a[i,N] }";
5158 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
5159 "S4[i] -> [4,i,0] }";
5160 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
5161 isl_options_set_schedule_maximize_coincidence(ctx, 0);
5162 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
5163 return -1;
5164 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
5166 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
5167 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
5168 "j <= N }";
5169 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
5170 "j <= N; "
5171 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
5172 "j <= N }";
5173 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
5174 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5175 return -1;
5177 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
5178 " S_2[t] : t >= 0 and t <= -1 + N; "
5179 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
5180 "i <= -1 + N }";
5181 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
5182 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
5183 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
5184 "i >= 0 and i <= -1 + N }";
5185 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
5186 "i >= 0 and i <= -1 + N; "
5187 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
5188 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
5189 " S_0[t] -> [0, t, 0] }";
5191 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
5192 return -1;
5193 ctx->opt->schedule_parametric = 0;
5194 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5195 return -1;
5196 ctx->opt->schedule_parametric = 1;
5198 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
5199 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
5200 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
5201 return -1;
5203 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
5204 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
5205 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
5206 "j >= 0 and j <= -1 + N; "
5207 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
5208 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
5209 "j >= 0 and j <= -1 + N; "
5210 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
5211 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
5212 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5213 return -1;
5215 D = "{ S_0[i] : i >= 0 }";
5216 W = "{ S_0[i] -> a[i] : i >= 0 }";
5217 R = "{ S_0[i] -> a[0] : i >= 0 }";
5218 S = "{ S_0[i] -> [0, i, 0] }";
5219 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5220 return -1;
5222 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
5223 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
5224 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
5225 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
5226 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5227 return -1;
5229 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
5230 "k <= -1 + n and k >= 0 }";
5231 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
5232 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
5233 "k <= -1 + n and k >= 0; "
5234 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
5235 "k <= -1 + n and k >= 0; "
5236 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
5237 "k <= -1 + n and k >= 0 }";
5238 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
5239 ctx->opt->schedule_outer_coincidence = 1;
5240 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5241 return -1;
5242 ctx->opt->schedule_outer_coincidence = 0;
5244 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
5245 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
5246 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
5247 "Stmt_for_body24[i0, i1, 1, 0]:"
5248 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
5249 "Stmt_for_body7[i0, i1, i2]:"
5250 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
5251 "i2 <= 7 }";
5253 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
5254 "Stmt_for_body24[1, i1, i2, i3]:"
5255 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
5256 "i2 >= 1;"
5257 "Stmt_for_body24[0, i1, i2, i3] -> "
5258 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
5259 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
5260 "i3 >= 0;"
5261 "Stmt_for_body24[0, i1, i2, i3] ->"
5262 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
5263 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
5264 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
5265 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
5266 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
5267 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
5268 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
5269 "i1 <= 6 and i1 >= 0;"
5270 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
5271 "Stmt_for_body7[i0, i1, i2] -> "
5272 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
5273 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
5274 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
5275 "Stmt_for_body7[i0, i1, i2] -> "
5276 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
5277 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
5278 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
5279 P = V;
5281 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
5282 isl_options_set_schedule_treat_coalescing(ctx, 0);
5283 if (test_has_schedule(ctx, D, V, P) < 0)
5284 return -1;
5285 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
5287 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
5288 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
5289 "j >= 1 and j <= 7;"
5290 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
5291 "j >= 1 and j <= 8 }";
5292 P = "{ }";
5293 S = "{ S_0[i, j] -> [i + j, i] }";
5294 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5295 if (test_special_schedule(ctx, D, V, P, S) < 0)
5296 return -1;
5297 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5299 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
5300 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
5301 "j >= 0 and j <= -1 + i }";
5302 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
5303 "i <= -1 + N and j >= 0;"
5304 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
5305 "i <= -2 + N }";
5306 P = "{ }";
5307 S = "{ S_0[i, j] -> [i, j] }";
5308 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5309 if (test_special_schedule(ctx, D, V, P, S) < 0)
5310 return -1;
5311 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5313 /* Test both algorithms on a case with only proximity dependences. */
5314 D = "{ S[i,j] : 0 <= i <= 10 }";
5315 V = "{ }";
5316 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
5317 S = "{ S[i, j] -> [j, i] }";
5318 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5319 if (test_special_schedule(ctx, D, V, P, S) < 0)
5320 return -1;
5321 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5322 if (test_special_schedule(ctx, D, V, P, S) < 0)
5323 return -1;
5325 D = "{ A[a]; B[] }";
5326 V = "{}";
5327 P = "{ A[a] -> B[] }";
5328 if (test_has_schedule(ctx, D, V, P) < 0)
5329 return -1;
5331 if (test_padded_schedule(ctx) < 0)
5332 return -1;
5334 /* Check that check for progress is not confused by rational
5335 * solution.
5337 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
5338 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
5339 "i0 <= -2 + N; "
5340 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
5341 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
5342 P = "{}";
5343 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5344 if (test_has_schedule(ctx, D, V, P) < 0)
5345 return -1;
5346 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5348 /* Check that we allow schedule rows that are only non-trivial
5349 * on some full-dimensional domains.
5351 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
5352 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
5353 "S1[j] -> S2[1] : 0 <= j <= 1 }";
5354 P = "{}";
5355 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5356 if (test_has_schedule(ctx, D, V, P) < 0)
5357 return -1;
5358 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5360 if (test_conditional_schedule_constraints(ctx) < 0)
5361 return -1;
5363 if (test_strongly_satisfying_schedule(ctx) < 0)
5364 return -1;
5366 if (test_conflicting_context_schedule(ctx) < 0)
5367 return -1;
5369 if (test_coalescing_schedule(ctx) < 0)
5370 return -1;
5371 if (test_skewing_schedule(ctx) < 0)
5372 return -1;
5374 return 0;
5377 /* Perform scheduling tests using the whole component scheduler.
5379 static int test_schedule_whole(isl_ctx *ctx)
5381 int whole;
5382 int r;
5384 whole = isl_options_get_schedule_whole_component(ctx);
5385 isl_options_set_schedule_whole_component(ctx, 1);
5386 r = test_schedule(ctx);
5387 isl_options_set_schedule_whole_component(ctx, whole);
5389 return r;
5392 /* Perform scheduling tests using the incremental scheduler.
5394 static int test_schedule_incremental(isl_ctx *ctx)
5396 int whole;
5397 int r;
5399 whole = isl_options_get_schedule_whole_component(ctx);
5400 isl_options_set_schedule_whole_component(ctx, 0);
5401 r = test_schedule(ctx);
5402 isl_options_set_schedule_whole_component(ctx, whole);
5404 return r;
5407 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
5409 isl_union_map *umap;
5410 int test;
5412 umap = isl_union_map_read_from_str(ctx, str);
5413 test = isl_union_map_plain_is_injective(umap);
5414 isl_union_map_free(umap);
5415 if (test < 0)
5416 return -1;
5417 if (test == injective)
5418 return 0;
5419 if (injective)
5420 isl_die(ctx, isl_error_unknown,
5421 "map not detected as injective", return -1);
5422 else
5423 isl_die(ctx, isl_error_unknown,
5424 "map detected as injective", return -1);
5427 int test_injective(isl_ctx *ctx)
5429 const char *str;
5431 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
5432 return -1;
5433 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
5434 return -1;
5435 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
5436 return -1;
5437 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
5438 return -1;
5439 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
5440 return -1;
5441 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
5442 return -1;
5443 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
5444 return -1;
5445 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
5446 return -1;
5448 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
5449 if (test_plain_injective(ctx, str, 1))
5450 return -1;
5451 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
5452 if (test_plain_injective(ctx, str, 0))
5453 return -1;
5455 return 0;
5458 #undef BASE
5459 #define BASE aff
5460 #include "isl_test_plain_equal_templ.c"
5462 #undef BASE
5463 #define BASE pw_multi_aff
5464 #include "isl_test_plain_equal_templ.c"
5466 #undef BASE
5467 #define BASE union_pw_aff
5468 #include "isl_test_plain_equal_templ.c"
5470 /* Basic tests on isl_union_pw_aff.
5472 * In particular, check that isl_union_pw_aff_aff_on_domain
5473 * aligns the parameters of the input objects and
5474 * that isl_union_pw_aff_param_on_domain_id properly
5475 * introduces the parameter.
5477 static int test_upa(isl_ctx *ctx)
5479 const char *str;
5480 isl_id *id;
5481 isl_aff *aff;
5482 isl_union_set *domain;
5483 isl_union_pw_aff *upa;
5484 isl_stat ok;
5486 aff = isl_aff_read_from_str(ctx, "[N] -> { [N] }");
5487 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5488 domain = isl_union_set_read_from_str(ctx, str);
5489 upa = isl_union_pw_aff_aff_on_domain(domain, aff);
5490 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5491 ok = union_pw_aff_check_plain_equal(upa, str);
5492 isl_union_pw_aff_free(upa);
5493 if (ok < 0)
5494 return -1;
5496 id = isl_id_alloc(ctx, "N", NULL);
5497 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5498 domain = isl_union_set_read_from_str(ctx, str);
5499 upa = isl_union_pw_aff_param_on_domain_id(domain, id);
5500 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5501 ok = union_pw_aff_check_plain_equal(upa, str);
5502 isl_union_pw_aff_free(upa);
5503 if (ok < 0)
5504 return -1;
5506 return 0;
5509 struct {
5510 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5511 __isl_take isl_aff *aff2);
5512 } aff_bin_op[] = {
5513 ['+'] = { &isl_aff_add },
5514 ['-'] = { &isl_aff_sub },
5515 ['*'] = { &isl_aff_mul },
5516 ['/'] = { &isl_aff_div },
5519 struct {
5520 const char *arg1;
5521 unsigned char op;
5522 const char *arg2;
5523 const char *res;
5524 } aff_bin_tests[] = {
5525 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
5526 "{ [i] -> [2i] }" },
5527 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
5528 "{ [i] -> [0] }" },
5529 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
5530 "{ [i] -> [2i] }" },
5531 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
5532 "{ [i] -> [2i] }" },
5533 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
5534 "{ [i] -> [i/2] }" },
5535 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
5536 "{ [i] -> [i] }" },
5537 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
5538 "{ [i] -> [NaN] }" },
5539 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
5540 "{ [i] -> [NaN] }" },
5541 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
5542 "{ [i] -> [NaN] }" },
5543 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
5544 "{ [i] -> [NaN] }" },
5545 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
5546 "{ [i] -> [NaN] }" },
5547 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
5548 "{ [i] -> [NaN] }" },
5549 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
5550 "{ [i] -> [NaN] }" },
5551 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
5552 "{ [i] -> [NaN] }" },
5553 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
5554 "{ [i] -> [NaN] }" },
5555 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
5556 "{ [i] -> [NaN] }" },
5557 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
5558 "{ [i] -> [NaN] }" },
5559 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
5560 "{ [i] -> [NaN] }" },
5561 { "{ [i] -> [i] }", '/', "{ [i] -> [0] }",
5562 "{ [i] -> [NaN] }" },
5565 /* Perform some basic tests of binary operations on isl_aff objects.
5567 static int test_bin_aff(isl_ctx *ctx)
5569 int i;
5570 isl_aff *aff1, *aff2, *res;
5571 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5572 __isl_take isl_aff *aff2);
5573 int ok;
5575 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
5576 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
5577 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
5578 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
5579 fn = aff_bin_op[aff_bin_tests[i].op].fn;
5580 aff1 = fn(aff1, aff2);
5581 if (isl_aff_is_nan(res))
5582 ok = isl_aff_is_nan(aff1);
5583 else
5584 ok = isl_aff_plain_is_equal(aff1, res);
5585 isl_aff_free(aff1);
5586 isl_aff_free(res);
5587 if (ok < 0)
5588 return -1;
5589 if (!ok)
5590 isl_die(ctx, isl_error_unknown,
5591 "unexpected result", return -1);
5594 return 0;
5597 struct {
5598 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
5599 __isl_take isl_pw_aff *pa2);
5600 } pw_aff_bin_op[] = {
5601 ['m'] = { &isl_pw_aff_min },
5602 ['M'] = { &isl_pw_aff_max },
5605 /* Inputs for binary isl_pw_aff operation tests.
5606 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
5607 * defined by pw_aff_bin_op, and "res" is the expected result.
5609 struct {
5610 const char *arg1;
5611 unsigned char op;
5612 const char *arg2;
5613 const char *res;
5614 } pw_aff_bin_tests[] = {
5615 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
5616 "{ [i] -> [i] }" },
5617 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
5618 "{ [i] -> [i] }" },
5619 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
5620 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
5621 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
5622 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
5623 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
5624 "{ [i] -> [NaN] }" },
5625 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
5626 "{ [i] -> [NaN] }" },
5629 /* Perform some basic tests of binary operations on isl_pw_aff objects.
5631 static int test_bin_pw_aff(isl_ctx *ctx)
5633 int i;
5634 isl_bool ok;
5635 isl_pw_aff *pa1, *pa2, *res;
5637 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
5638 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
5639 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
5640 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
5641 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
5642 if (isl_pw_aff_involves_nan(res))
5643 ok = isl_pw_aff_involves_nan(pa1);
5644 else
5645 ok = isl_pw_aff_plain_is_equal(pa1, res);
5646 isl_pw_aff_free(pa1);
5647 isl_pw_aff_free(res);
5648 if (ok < 0)
5649 return -1;
5650 if (!ok)
5651 isl_die(ctx, isl_error_unknown,
5652 "unexpected result", return -1);
5655 return 0;
5658 /* Inputs for basic tests of test operations on
5659 * isl_union_pw_multi_aff objects.
5660 * "fn" is the function that is being tested.
5661 * "arg" is a string description of the input.
5662 * "res" is the expected result.
5664 static struct {
5665 isl_bool (*fn)(__isl_keep isl_union_pw_multi_aff *upma1);
5666 const char *arg;
5667 isl_bool res;
5668 } upma_test_tests[] = {
5669 { &isl_union_pw_multi_aff_involves_nan, "{ A[] -> [0]; B[0] -> [1] }",
5670 isl_bool_false },
5671 { &isl_union_pw_multi_aff_involves_nan, "{ A[] -> [NaN]; B[0] -> [1] }",
5672 isl_bool_true },
5673 { &isl_union_pw_multi_aff_involves_nan, "{ A[] -> [0]; B[0] -> [NaN] }",
5674 isl_bool_true },
5675 { &isl_union_pw_multi_aff_involves_nan,
5676 "{ A[] -> [0]; B[0] -> [1, NaN, 5] }",
5677 isl_bool_true },
5678 { &isl_union_pw_multi_aff_involves_locals,
5679 "{ A[] -> [0]; B[0] -> [1] }",
5680 isl_bool_false },
5681 { &isl_union_pw_multi_aff_involves_locals,
5682 "{ A[] -> [0]; B[x] -> [1] : x mod 2 = 0 }",
5683 isl_bool_true },
5684 { &isl_union_pw_multi_aff_involves_locals,
5685 "{ A[] -> [0]; B[x] -> [x // 2] }",
5686 isl_bool_true },
5687 { &isl_union_pw_multi_aff_involves_locals,
5688 "{ A[i] -> [i // 2]; B[0] -> [1] }",
5689 isl_bool_true },
5692 /* Perform some basic tests of test operations on
5693 * isl_union_pw_multi_aff objects.
5695 static isl_stat test_upma_test(isl_ctx *ctx)
5697 int i;
5698 isl_union_pw_multi_aff *upma;
5699 isl_bool res;
5701 for (i = 0; i < ARRAY_SIZE(upma_test_tests); ++i) {
5702 const char *str;
5704 str = upma_test_tests[i].arg;
5705 upma = isl_union_pw_multi_aff_read_from_str(ctx, str);
5706 res = upma_test_tests[i].fn(upma);
5707 isl_union_pw_multi_aff_free(upma);
5708 if (res < 0)
5709 return isl_stat_error;
5710 if (res != upma_test_tests[i].res)
5711 isl_die(ctx, isl_error_unknown,
5712 "unexpected result", return isl_stat_error);
5715 return isl_stat_ok;
5718 struct {
5719 __isl_give isl_union_pw_multi_aff *(*fn)(
5720 __isl_take isl_union_pw_multi_aff *upma1,
5721 __isl_take isl_union_pw_multi_aff *upma2);
5722 const char *arg1;
5723 const char *arg2;
5724 const char *res;
5725 } upma_bin_tests[] = {
5726 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
5727 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
5728 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
5729 "{ B[x] -> [2] : x >= 0 }",
5730 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
5731 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
5732 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
5733 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
5734 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
5735 "D[i] -> B[2] : i >= 5 }" },
5736 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5737 "{ B[x] -> C[2] : x > 0 }",
5738 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
5739 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5740 "{ B[x] -> A[2] : x >= 0 }",
5741 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
5743 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5744 "{ B[x] -> C[x + 2] }",
5745 "{ D[y] -> B[2y] }",
5746 "{ }" },
5748 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5749 "{ [A[x] -> B[x + 1]] -> C[x + 2] }",
5750 "{ D[y] -> B[2y] }",
5751 "{ }" },
5753 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5754 "{ [A[x] -> B[x + 1]] -> C[x + 2]; B[x] -> C[x + 2] }",
5755 "{ D[y] -> A[2y] }",
5756 "{ [D[y] -> B[2y + 1]] -> C[2y + 2] }" },
5758 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5759 "{ T[A[x] -> B[x + 1]] -> C[x + 2]; B[x] -> C[x + 2] }",
5760 "{ D[y] -> A[2y] }",
5761 "{ T[D[y] -> B[2y + 1]] -> C[2y + 2] }" },
5764 /* Perform some basic tests of binary operations on
5765 * isl_union_pw_multi_aff objects.
5767 static int test_bin_upma(isl_ctx *ctx)
5769 int i;
5770 isl_union_pw_multi_aff *upma1, *upma2, *res;
5771 int ok;
5773 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
5774 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5775 upma_bin_tests[i].arg1);
5776 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5777 upma_bin_tests[i].arg2);
5778 res = isl_union_pw_multi_aff_read_from_str(ctx,
5779 upma_bin_tests[i].res);
5780 upma1 = upma_bin_tests[i].fn(upma1, upma2);
5781 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
5782 isl_union_pw_multi_aff_free(upma1);
5783 isl_union_pw_multi_aff_free(res);
5784 if (ok < 0)
5785 return -1;
5786 if (!ok)
5787 isl_die(ctx, isl_error_unknown,
5788 "unexpected result", return -1);
5791 return 0;
5794 struct {
5795 __isl_give isl_union_pw_multi_aff *(*fn)(
5796 __isl_take isl_union_pw_multi_aff *upma1,
5797 __isl_take isl_union_pw_multi_aff *upma2);
5798 const char *arg1;
5799 const char *arg2;
5800 } upma_bin_fail_tests[] = {
5801 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5802 "{ B[x] -> C[2] : x >= 0 }" },
5805 /* Perform some basic tests of binary operations on
5806 * isl_union_pw_multi_aff objects that are expected to fail.
5808 static int test_bin_upma_fail(isl_ctx *ctx)
5810 int i, n;
5811 isl_union_pw_multi_aff *upma1, *upma2;
5812 int on_error;
5814 on_error = isl_options_get_on_error(ctx);
5815 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
5816 n = ARRAY_SIZE(upma_bin_fail_tests);
5817 for (i = 0; i < n; ++i) {
5818 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5819 upma_bin_fail_tests[i].arg1);
5820 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5821 upma_bin_fail_tests[i].arg2);
5822 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
5823 isl_union_pw_multi_aff_free(upma1);
5824 if (upma1)
5825 break;
5827 isl_options_set_on_error(ctx, on_error);
5828 if (i < n)
5829 isl_die(ctx, isl_error_unknown,
5830 "operation not expected to succeed", return -1);
5832 return 0;
5835 /* Inputs for basic tests of binary operations on
5836 * pairs of isl_union_pw_multi_aff and isl_union_set objects.
5837 * "fn" is the function that is being tested.
5838 * "arg1" and "arg2" are string descriptions of the inputs.
5839 * "res" is a string description of the expected result.
5841 struct {
5842 __isl_give isl_union_pw_multi_aff *(*fn)(
5843 __isl_take isl_union_pw_multi_aff *upma,
5844 __isl_take isl_union_set *uset);
5845 const char *arg1;
5846 const char *arg2;
5847 const char *res;
5848 } upma_uset_tests[] = {
5849 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5850 "{ A[i] -> B[i] }", "{ B[0] }",
5851 "{ }" },
5852 { &isl_union_pw_multi_aff_intersect_domain_wrapped_domain,
5853 "{ [A[i] -> B[i]] -> C[i + 1] }", "{ A[1]; B[0] }",
5854 "{ [A[1] -> B[1]] -> C[2] }" },
5855 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5856 "{ [A[i] -> B[i]] -> C[i + 1] }", "{ A[1]; B[0] }",
5857 "{ [A[0] -> B[0]] -> C[1] }" },
5858 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5859 "{ [A[i] -> B[i]] -> C[i + 1] }", "[N] -> { B[N] }",
5860 "[N] -> { [A[N] -> B[N]] -> C[N + 1] }" },
5861 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5862 "[M] -> { [A[M] -> B[M]] -> C[M + 1] }", "[N] -> { B[N] }",
5863 "[N, M] -> { [A[N] -> B[N]] -> C[N + 1] : N = M }" },
5864 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5865 "{ [A[] -> B[]] -> C[]; N[A[] -> B[]] -> D[]; [B[] -> A[]] -> E[] }",
5866 "{ B[] }",
5867 "{ [A[] -> B[]] -> C[]; N[A[] -> B[]] -> D[] }" },
5870 /* Perform some basic tests of binary operations on
5871 * pairs of isl_union_pw_multi_aff and isl_union_set objects.
5873 static isl_stat test_upma_uset(isl_ctx *ctx)
5875 int i;
5876 isl_bool ok;
5877 isl_union_pw_multi_aff *upma, *res;
5878 isl_union_set *uset;
5880 for (i = 0; i < ARRAY_SIZE(upma_uset_tests); ++i) {
5881 upma = isl_union_pw_multi_aff_read_from_str(ctx,
5882 upma_uset_tests[i].arg1);
5883 uset = isl_union_set_read_from_str(ctx,
5884 upma_uset_tests[i].arg2);
5885 res = isl_union_pw_multi_aff_read_from_str(ctx,
5886 upma_uset_tests[i].res);
5887 upma = upma_uset_tests[i].fn(upma, uset);
5888 ok = isl_union_pw_multi_aff_plain_is_equal(upma, res);
5889 isl_union_pw_multi_aff_free(upma);
5890 isl_union_pw_multi_aff_free(res);
5891 if (ok < 0)
5892 return isl_stat_error;
5893 if (!ok)
5894 isl_die(ctx, isl_error_unknown,
5895 "unexpected result", return isl_stat_error);
5898 return isl_stat_ok;
5901 /* Inputs for basic tests of unary operations on isl_multi_pw_aff objects.
5902 * "fn" is the function that is tested.
5903 * "arg" is a string description of the input.
5904 * "res" is a string description of the expected result.
5906 struct {
5907 __isl_give isl_multi_pw_aff *(*fn)(__isl_take isl_multi_pw_aff *mpa);
5908 const char *arg;
5909 const char *res;
5910 } mpa_un_tests[] = {
5911 { &isl_multi_pw_aff_range_factor_domain,
5912 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5913 "{ A[x] -> B[(1 : x >= 5)] }" },
5914 { &isl_multi_pw_aff_range_factor_range,
5915 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5916 "{ A[y] -> C[(2 : y <= 10)] }" },
5917 { &isl_multi_pw_aff_range_factor_domain,
5918 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5919 "{ A[x] -> B[(1 : x >= 5)] }" },
5920 { &isl_multi_pw_aff_range_factor_range,
5921 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5922 "{ A[y] -> C[] }" },
5923 { &isl_multi_pw_aff_range_factor_domain,
5924 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5925 "{ A[x] -> B[] }" },
5926 { &isl_multi_pw_aff_range_factor_range,
5927 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5928 "{ A[y] -> C[(2 : y <= 10)] }" },
5929 { &isl_multi_pw_aff_range_factor_domain,
5930 "{ A[x] -> [B[] -> C[]] }",
5931 "{ A[x] -> B[] }" },
5932 { &isl_multi_pw_aff_range_factor_range,
5933 "{ A[x] -> [B[] -> C[]] }",
5934 "{ A[y] -> C[] }" },
5935 { &isl_multi_pw_aff_factor_range,
5936 "{ [B[] -> C[]] }",
5937 "{ C[] }" },
5938 { &isl_multi_pw_aff_range_factor_domain,
5939 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5940 "{ A[x] -> B[] : x >= 0 }" },
5941 { &isl_multi_pw_aff_range_factor_range,
5942 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5943 "{ A[y] -> C[] : y >= 0 }" },
5944 { &isl_multi_pw_aff_factor_range,
5945 "[N] -> { [B[] -> C[]] : N >= 0 }",
5946 "[N] -> { C[] : N >= 0 }" },
5949 /* Perform some basic tests of unary operations on isl_multi_pw_aff objects.
5951 static int test_un_mpa(isl_ctx *ctx)
5953 int i;
5954 isl_bool ok;
5955 isl_multi_pw_aff *mpa, *res;
5957 for (i = 0; i < ARRAY_SIZE(mpa_un_tests); ++i) {
5958 mpa = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].arg);
5959 res = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].res);
5960 mpa = mpa_un_tests[i].fn(mpa);
5961 ok = isl_multi_pw_aff_plain_is_equal(mpa, res);
5962 isl_multi_pw_aff_free(mpa);
5963 isl_multi_pw_aff_free(res);
5964 if (ok < 0)
5965 return -1;
5966 if (!ok)
5967 isl_die(ctx, isl_error_unknown,
5968 "unexpected result", return -1);
5971 return 0;
5974 /* Inputs for basic tests of binary operations on isl_multi_pw_aff objects.
5975 * "fn" is the function that is tested.
5976 * "arg1" and "arg2" are string descriptions of the inputs.
5977 * "res" is a string description of the expected result.
5979 struct {
5980 __isl_give isl_multi_pw_aff *(*fn)(
5981 __isl_take isl_multi_pw_aff *mpa1,
5982 __isl_take isl_multi_pw_aff *mpa2);
5983 const char *arg1;
5984 const char *arg2;
5985 const char *res;
5986 } mpa_bin_tests[] = {
5987 { &isl_multi_pw_aff_add, "{ A[] -> [1] }", "{ A[] -> [2] }",
5988 "{ A[] -> [3] }" },
5989 { &isl_multi_pw_aff_add, "{ A[x] -> [(1 : x >= 5)] }",
5990 "{ A[x] -> [(x : x <= 10)] }",
5991 "{ A[x] -> [(1 + x : 5 <= x <= 10)] }" },
5992 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5993 "{ A[x] -> [] : x <= 10 }",
5994 "{ A[x] -> [] : 5 <= x <= 10 }" },
5995 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5996 "[N] -> { A[x] -> [] : x <= N }",
5997 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5998 { &isl_multi_pw_aff_add,
5999 "[N] -> { A[x] -> [] : x <= N }",
6000 "{ A[x] -> [] : x >= 5 }",
6001 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
6002 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
6003 "{ A[y] -> C[(2 : y <= 10)] }",
6004 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
6005 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
6006 "{ A[y] -> C[2] : y <= 10 }",
6007 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
6008 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
6009 "[N] -> { A[y] -> C[2] : y <= N }",
6010 "[N] -> { A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= N)]] }" },
6011 { &isl_multi_pw_aff_range_product, "[N] -> { A[x] -> B[1] : x >= N }",
6012 "{ A[y] -> C[2] : y <= 10 }",
6013 "[N] -> { A[x] -> [B[(1 : x >= N)] -> C[(2 : x <= 10)]] }" },
6014 { &isl_multi_pw_aff_range_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
6015 "{ A[] -> [B[1] -> C[2]] }" },
6016 { &isl_multi_pw_aff_range_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
6017 "{ A[] -> [B[] -> C[]] }" },
6018 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
6019 "{ A[y] -> C[] : y <= 10 }",
6020 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] : x <= 10 }" },
6021 { &isl_multi_pw_aff_range_product, "{ A[y] -> C[] : y <= 10 }",
6022 "{ A[x] -> B[(1 : x >= 5)] }",
6023 "{ A[x] -> [C[] -> B[(1 : x >= 5)]] : x <= 10 }" },
6024 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
6025 "{ A[y] -> C[(2 : y <= 10)] }",
6026 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[(2 : y <= 10)]] }" },
6027 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
6028 "{ A[y] -> C[] : y <= 10 }",
6029 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= 10 }" },
6030 { &isl_multi_pw_aff_product, "{ A[y] -> C[] : y <= 10 }",
6031 "{ A[x] -> B[(1 : x >= 5)] }",
6032 "{ [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= 10 }" },
6033 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
6034 "[N] -> { A[y] -> C[] : y <= N }",
6035 "[N] -> { [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= N }" },
6036 { &isl_multi_pw_aff_product, "[N] -> { A[y] -> C[] : y <= N }",
6037 "{ A[x] -> B[(1 : x >= 5)] }",
6038 "[N] -> { [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= N }" },
6039 { &isl_multi_pw_aff_product, "{ A[x] -> B[] : x >= 5 }",
6040 "{ A[y] -> C[] : y <= 10 }",
6041 "{ [A[x] -> A[y]] -> [B[] -> C[]] : x >= 5 and y <= 10 }" },
6042 { &isl_multi_pw_aff_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
6043 "{ [A[] -> A[]] -> [B[1] -> C[2]] }" },
6044 { &isl_multi_pw_aff_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
6045 "{ [A[] -> A[]] -> [B[] -> C[]] }" },
6046 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6047 "{ B[i,j] -> C[i + 2j] }", "{ A[a,b] -> B[b,a] }",
6048 "{ A[a,b] -> C[b + 2a] }" },
6049 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6050 "{ B[i,j] -> C[i + 2j] }",
6051 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6052 "{ A[a,b] -> C[(b + 2a : b > a)] }" },
6053 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6054 "{ B[i,j] -> C[(i + 2j : j > 4)] }",
6055 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6056 "{ A[a,b] -> C[(b + 2a : b > a > 4)] }" },
6057 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6058 "{ B[i,j] -> C[] }",
6059 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6060 "{ A[a,b] -> C[] }" },
6061 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6062 "{ B[i,j] -> C[] : i > j }",
6063 "{ A[a,b] -> B[b,a] }",
6064 "{ A[a,b] -> C[] : b > a }" },
6065 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6066 "{ B[i,j] -> C[] : j > 5 }",
6067 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6068 "{ A[a,b] -> C[] : b > a > 5 }" },
6069 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6070 "[N] -> { B[i,j] -> C[] : j > N }",
6071 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6072 "[N] -> { A[a,b] -> C[] : b > a > N }" },
6073 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6074 "[M,N] -> { B[] -> C[] : N > 5 }",
6075 "[M,N] -> { A[] -> B[] : M > N }",
6076 "[M,N] -> { A[] -> C[] : M > N > 5 }" },
6079 /* Perform some basic tests of binary operations on isl_multi_pw_aff objects.
6081 static int test_bin_mpa(isl_ctx *ctx)
6083 int i;
6084 isl_bool ok;
6085 isl_multi_pw_aff *mpa1, *mpa2, *res;
6087 for (i = 0; i < ARRAY_SIZE(mpa_bin_tests); ++i) {
6088 mpa1 = isl_multi_pw_aff_read_from_str(ctx,
6089 mpa_bin_tests[i].arg1);
6090 mpa2 = isl_multi_pw_aff_read_from_str(ctx,
6091 mpa_bin_tests[i].arg2);
6092 res = isl_multi_pw_aff_read_from_str(ctx,
6093 mpa_bin_tests[i].res);
6094 mpa1 = mpa_bin_tests[i].fn(mpa1, mpa2);
6095 ok = isl_multi_pw_aff_plain_is_equal(mpa1, res);
6096 isl_multi_pw_aff_free(mpa1);
6097 isl_multi_pw_aff_free(res);
6098 if (ok < 0)
6099 return -1;
6100 if (!ok)
6101 isl_die(ctx, isl_error_unknown,
6102 "unexpected result", return -1);
6105 return 0;
6108 /* Inputs for basic tests of unary operations on
6109 * isl_multi_union_pw_aff objects.
6110 * "fn" is the function that is tested.
6111 * "arg" is a string description of the input.
6112 * "res" is a string description of the expected result.
6114 struct {
6115 __isl_give isl_multi_union_pw_aff *(*fn)(
6116 __isl_take isl_multi_union_pw_aff *mupa);
6117 const char *arg;
6118 const char *res;
6119 } mupa_un_tests[] = {
6120 { &isl_multi_union_pw_aff_factor_range,
6121 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]",
6122 "C[{ A[] -> [2] }]" },
6123 { &isl_multi_union_pw_aff_factor_range,
6124 "[B[] -> C[{ A[] -> [2] }]]",
6125 "C[{ A[] -> [2] }]" },
6126 { &isl_multi_union_pw_aff_factor_range,
6127 "[B[{ A[] -> [1] }] -> C[]]",
6128 "C[]" },
6129 { &isl_multi_union_pw_aff_factor_range,
6130 "[B[] -> C[]]",
6131 "C[]" },
6132 { &isl_multi_union_pw_aff_factor_range,
6133 "([B[] -> C[]] : { A[x] : x >= 0 })",
6134 "(C[] : { A[x] : x >= 0 })" },
6135 { &isl_multi_union_pw_aff_factor_range,
6136 "[N] -> ([B[] -> C[]] : { A[x] : x <= N })",
6137 "[N] -> (C[] : { A[x] : x <= N })" },
6138 { &isl_multi_union_pw_aff_factor_range,
6139 "[N] -> ([B[] -> C[]] : { : N >= 0 })",
6140 "[N] -> (C[] : { : N >= 0 })" },
6143 /* Perform some basic tests of unary operations on
6144 * isl_multi_union_pw_aff objects.
6146 static int test_un_mupa(isl_ctx *ctx)
6148 int i;
6149 isl_bool ok;
6150 isl_multi_union_pw_aff *mupa, *res;
6152 for (i = 0; i < ARRAY_SIZE(mupa_un_tests); ++i) {
6153 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6154 mupa_un_tests[i].arg);
6155 res = isl_multi_union_pw_aff_read_from_str(ctx,
6156 mupa_un_tests[i].res);
6157 mupa = mupa_un_tests[i].fn(mupa);
6158 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6159 isl_multi_union_pw_aff_free(mupa);
6160 isl_multi_union_pw_aff_free(res);
6161 if (ok < 0)
6162 return -1;
6163 if (!ok)
6164 isl_die(ctx, isl_error_unknown,
6165 "unexpected result", return -1);
6168 return 0;
6171 /* Inputs for basic tests of binary operations on
6172 * isl_multi_union_pw_aff objects.
6173 * "fn" is the function that is tested.
6174 * "arg1" and "arg2" are string descriptions of the inputs.
6175 * "res" is a string description of the expected result.
6177 struct {
6178 __isl_give isl_multi_union_pw_aff *(*fn)(
6179 __isl_take isl_multi_union_pw_aff *mupa1,
6180 __isl_take isl_multi_union_pw_aff *mupa2);
6181 const char *arg1;
6182 const char *arg2;
6183 const char *res;
6184 } mupa_bin_tests[] = {
6185 { &isl_multi_union_pw_aff_add, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
6186 "[{ A[] -> [3] }]" },
6187 { &isl_multi_union_pw_aff_sub, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
6188 "[{ A[] -> [-1] }]" },
6189 { &isl_multi_union_pw_aff_add,
6190 "[{ A[] -> [1]; B[] -> [4] }]",
6191 "[{ A[] -> [2]; C[] -> [5] }]",
6192 "[{ A[] -> [3] }]" },
6193 { &isl_multi_union_pw_aff_union_add,
6194 "[{ A[] -> [1]; B[] -> [4] }]",
6195 "[{ A[] -> [2]; C[] -> [5] }]",
6196 "[{ A[] -> [3]; B[] -> [4]; C[] -> [5] }]" },
6197 { &isl_multi_union_pw_aff_add, "[{ A[x] -> [(1)] : x >= 5 }]",
6198 "[{ A[x] -> [(x)] : x <= 10 }]",
6199 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10 }]" },
6200 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
6201 "([] : { A[x] : x <= 10 })",
6202 "([] : { A[x] : 5 <= x <= 10 })" },
6203 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
6204 "[N] -> ([] : { A[x] : x <= N })",
6205 "[N] -> ([] : { A[x] : 5 <= x <= N })" },
6206 { &isl_multi_union_pw_aff_add, "[N] -> ([] : { A[x] : x >= N })",
6207 "([] : { A[x] : x <= 10 })",
6208 "[N] -> ([] : { A[x] : N <= x <= 10 })" },
6209 { &isl_multi_union_pw_aff_union_add, "[{ A[x] -> [(1)] : x >= 5 }]",
6210 "[{ A[x] -> [(x)] : x <= 10 }]",
6211 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10; "
6212 "A[x] -> [(1)] : x > 10; A[x] -> [(x)] : x < 5 }]" },
6213 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 5 })",
6214 "([] : { A[x] : x <= 10 })",
6215 "([] : { A[x] })" },
6216 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 0 })",
6217 "[N] -> ([] : { A[x] : x >= N })",
6218 "[N] -> ([] : { A[x] : x >= 0 or x >= N })" },
6219 { &isl_multi_union_pw_aff_union_add,
6220 "[N] -> ([] : { A[] : N >= 0})",
6221 "[N] -> ([] : { A[] : N <= 0})",
6222 "[N] -> ([] : { A[] })" },
6223 { &isl_multi_union_pw_aff_union_add,
6224 "[N] -> ([] : { A[] })",
6225 "[N] -> ([] : { : })",
6226 "[N] -> ([] : { : })" },
6227 { &isl_multi_union_pw_aff_union_add,
6228 "[N] -> ([] : { : })",
6229 "[N] -> ([] : { A[] })",
6230 "[N] -> ([] : { : })" },
6231 { &isl_multi_union_pw_aff_union_add,
6232 "[N] -> ([] : { : N >= 0})",
6233 "[N] -> ([] : { : N <= 0})",
6234 "[N] -> ([] : { : })" },
6235 { &isl_multi_union_pw_aff_range_product,
6236 "B[{ A[] -> [1] }]",
6237 "C[{ A[] -> [2] }]",
6238 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]" },
6239 { &isl_multi_union_pw_aff_range_product,
6240 "(B[] : { A[x] : x >= 5 })",
6241 "(C[] : { A[x] : x <= 10 })",
6242 "([B[] -> C[]] : { A[x] : 5 <= x <= 10 })" },
6243 { &isl_multi_union_pw_aff_range_product,
6244 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6245 "(C[] : { A[x] : x <= 10 })",
6246 "[B[{ A[x] -> [x + 1] : 5 <= x <= 10 }] -> C[]]" },
6247 { &isl_multi_union_pw_aff_range_product,
6248 "(C[] : { A[x] : x <= 10 })",
6249 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6250 "[C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= 10 }]]" },
6251 { &isl_multi_union_pw_aff_range_product,
6252 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6253 "[N] -> (C[] : { A[x] : x <= N })",
6254 "[N] -> [B[{ A[x] -> [x + 1] : 5 <= x <= N }] -> C[]]" },
6255 { &isl_multi_union_pw_aff_range_product,
6256 "[N] -> (C[] : { A[x] : x <= N })",
6257 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6258 "[N] -> [C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= N }]]" },
6259 { &isl_multi_union_pw_aff_range_product,
6260 "B[{ A[] -> [1]; D[] -> [3] }]",
6261 "C[{ A[] -> [2] }]",
6262 "[B[{ A[] -> [1]; D[] -> [3] }] -> C[{ A[] -> [2] }]]" },
6263 { &isl_multi_union_pw_aff_range_product,
6264 "B[] }]",
6265 "(C[] : { A[x] })",
6266 "([B[] -> C[]] : { A[x] })" },
6267 { &isl_multi_union_pw_aff_range_product,
6268 "(B[] : { A[x] })",
6269 "C[] }]",
6270 "([B[] -> C[]] : { A[x] })" },
6273 /* Perform some basic tests of binary operations on
6274 * isl_multi_union_pw_aff objects.
6276 static int test_bin_mupa(isl_ctx *ctx)
6278 int i;
6279 isl_bool ok;
6280 isl_multi_union_pw_aff *mupa1, *mupa2, *res;
6282 for (i = 0; i < ARRAY_SIZE(mupa_bin_tests); ++i) {
6283 mupa1 = isl_multi_union_pw_aff_read_from_str(ctx,
6284 mupa_bin_tests[i].arg1);
6285 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx,
6286 mupa_bin_tests[i].arg2);
6287 res = isl_multi_union_pw_aff_read_from_str(ctx,
6288 mupa_bin_tests[i].res);
6289 mupa1 = mupa_bin_tests[i].fn(mupa1, mupa2);
6290 ok = isl_multi_union_pw_aff_plain_is_equal(mupa1, res);
6291 isl_multi_union_pw_aff_free(mupa1);
6292 isl_multi_union_pw_aff_free(res);
6293 if (ok < 0)
6294 return -1;
6295 if (!ok)
6296 isl_die(ctx, isl_error_unknown,
6297 "unexpected result", return -1);
6300 return 0;
6303 /* Inputs for basic tests of binary operations on
6304 * pairs of isl_multi_union_pw_aff and isl_set objects.
6305 * "fn" is the function that is tested.
6306 * "arg1" and "arg2" are string descriptions of the inputs.
6307 * "res" is a string description of the expected result.
6309 struct {
6310 __isl_give isl_multi_union_pw_aff *(*fn)(
6311 __isl_take isl_multi_union_pw_aff *mupa,
6312 __isl_take isl_set *set);
6313 const char *arg1;
6314 const char *arg2;
6315 const char *res;
6316 } mupa_set_tests[] = {
6317 { &isl_multi_union_pw_aff_intersect_range,
6318 "C[{ B[i,j] -> [i + 2j] }]", "{ C[1] }",
6319 "C[{ B[i,j] -> [i + 2j] : i + 2j = 1 }]" },
6320 { &isl_multi_union_pw_aff_intersect_range,
6321 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[N] }",
6322 "[N] -> C[{ B[i,j] -> [i + 2j] : i + 2j = N }]" },
6323 { &isl_multi_union_pw_aff_intersect_range,
6324 "[N] -> C[{ B[i,j] -> [i + 2j + N] }]", "{ C[1] }",
6325 "[N] -> C[{ B[i,j] -> [i + 2j + N] : i + 2j + N = 1 }]" },
6326 { &isl_multi_union_pw_aff_intersect_range,
6327 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[x] : N >= 0 }",
6328 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0 }]" },
6329 { &isl_multi_union_pw_aff_intersect_range,
6330 "C[]", "{ C[] }", "C[]" },
6331 { &isl_multi_union_pw_aff_intersect_range,
6332 "[N] -> (C[] : { : N >= 0 })",
6333 "{ C[] }",
6334 "[N] -> (C[] : { : N >= 0 })" },
6335 { &isl_multi_union_pw_aff_intersect_range,
6336 "(C[] : { A[a,b] })",
6337 "{ C[] }",
6338 "(C[] : { A[a,b] })" },
6339 { &isl_multi_union_pw_aff_intersect_range,
6340 "[N] -> (C[] : { A[a,b] : a,b <= N })",
6341 "{ C[] }",
6342 "[N] -> (C[] : { A[a,b] : a,b <= N })" },
6343 { &isl_multi_union_pw_aff_intersect_range,
6344 "C[]",
6345 "[N] -> { C[] : N >= 0 }",
6346 "[N] -> (C[] : { : N >= 0 })" },
6347 { &isl_multi_union_pw_aff_intersect_range,
6348 "(C[] : { A[a,b] })",
6349 "[N] -> { C[] : N >= 0 }",
6350 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
6351 { &isl_multi_union_pw_aff_intersect_range,
6352 "[N] -> (C[] : { : N >= 0 })",
6353 "[N] -> { C[] : N < 1024 }",
6354 "[N] -> (C[] : { : 0 <= N < 1024 })" },
6355 { &isl_multi_union_pw_aff_intersect_params,
6356 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { : N >= 0 }",
6357 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0}]" },
6358 { &isl_multi_union_pw_aff_intersect_params,
6359 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "[N] -> { : N >= 0 }",
6360 "[N] -> C[{ B[i,j] -> [i + 2j] : 0 <= N <= 256 }]" },
6361 { &isl_multi_union_pw_aff_intersect_params,
6362 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "{ : }",
6363 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]" },
6364 { &isl_multi_union_pw_aff_intersect_params,
6365 "C[]", "[N] -> { : N >= 0 }",
6366 "[N] -> (C[] : { : N >= 0 })" },
6367 { &isl_multi_union_pw_aff_intersect_params,
6368 "(C[] : { A[a,b] })", "[N] -> { : N >= 0 }",
6369 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
6370 { &isl_multi_union_pw_aff_intersect_params,
6371 "[N] -> (C[] : { A[a,N] })", "{ : }",
6372 "[N] -> (C[] : { A[a,N] })" },
6373 { &isl_multi_union_pw_aff_intersect_params,
6374 "[N] -> (C[] : { A[a,b] : N <= 256 })", "[N] -> { : N >= 0 }",
6375 "[N] -> (C[] : { A[a,b] : 0 <= N <= 256 })" },
6378 /* Perform some basic tests of binary operations on
6379 * pairs of isl_multi_union_pw_aff and isl_set objects.
6381 static int test_mupa_set(isl_ctx *ctx)
6383 int i;
6384 isl_bool ok;
6385 isl_multi_union_pw_aff *mupa, *res;
6386 isl_set *set;
6388 for (i = 0; i < ARRAY_SIZE(mupa_set_tests); ++i) {
6389 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6390 mupa_set_tests[i].arg1);
6391 set = isl_set_read_from_str(ctx, mupa_set_tests[i].arg2);
6392 res = isl_multi_union_pw_aff_read_from_str(ctx,
6393 mupa_set_tests[i].res);
6394 mupa = mupa_set_tests[i].fn(mupa, set);
6395 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6396 isl_multi_union_pw_aff_free(mupa);
6397 isl_multi_union_pw_aff_free(res);
6398 if (ok < 0)
6399 return -1;
6400 if (!ok)
6401 isl_die(ctx, isl_error_unknown,
6402 "unexpected result", return -1);
6405 return 0;
6408 /* Inputs for basic tests of binary operations on
6409 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
6410 * "fn" is the function that is tested.
6411 * "arg1" and "arg2" are string descriptions of the inputs.
6412 * "res" is a string description of the expected result.
6414 struct {
6415 __isl_give isl_multi_union_pw_aff *(*fn)(
6416 __isl_take isl_multi_union_pw_aff *mupa,
6417 __isl_take isl_union_set *uset);
6418 const char *arg1;
6419 const char *arg2;
6420 const char *res;
6421 } mupa_uset_tests[] = {
6422 { &isl_multi_union_pw_aff_intersect_domain,
6423 "C[{ B[i,j] -> [i + 2j] }]", "{ B[i,i] }",
6424 "C[{ B[i,i] -> [3i] }]" },
6425 { &isl_multi_union_pw_aff_intersect_domain,
6426 "(C[] : { B[i,j] })", "{ B[i,i] }",
6427 "(C[] : { B[i,i] })" },
6428 { &isl_multi_union_pw_aff_intersect_domain,
6429 "(C[] : { B[i,j] })", "[N] -> { B[N,N] }",
6430 "[N] -> (C[] : { B[N,N] })" },
6431 { &isl_multi_union_pw_aff_intersect_domain,
6432 "C[]", "{ B[i,i] }",
6433 "(C[] : { B[i,i] })" },
6434 { &isl_multi_union_pw_aff_intersect_domain,
6435 "[N] -> (C[] : { : N >= 0 })", "{ B[i,i] }",
6436 "[N] -> (C[] : { B[i,i] : N >= 0 })" },
6439 /* Perform some basic tests of binary operations on
6440 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
6442 static int test_mupa_uset(isl_ctx *ctx)
6444 int i;
6445 isl_bool ok;
6446 isl_multi_union_pw_aff *mupa, *res;
6447 isl_union_set *uset;
6449 for (i = 0; i < ARRAY_SIZE(mupa_uset_tests); ++i) {
6450 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6451 mupa_uset_tests[i].arg1);
6452 uset = isl_union_set_read_from_str(ctx,
6453 mupa_uset_tests[i].arg2);
6454 res = isl_multi_union_pw_aff_read_from_str(ctx,
6455 mupa_uset_tests[i].res);
6456 mupa = mupa_uset_tests[i].fn(mupa, uset);
6457 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6458 isl_multi_union_pw_aff_free(mupa);
6459 isl_multi_union_pw_aff_free(res);
6460 if (ok < 0)
6461 return -1;
6462 if (!ok)
6463 isl_die(ctx, isl_error_unknown,
6464 "unexpected result", return -1);
6467 return 0;
6470 /* Inputs for basic tests of binary operations on
6471 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
6472 * "fn" is the function that is tested.
6473 * "arg1" and "arg2" are string descriptions of the inputs.
6474 * "res" is a string description of the expected result.
6476 struct {
6477 __isl_give isl_multi_union_pw_aff *(*fn)(
6478 __isl_take isl_multi_union_pw_aff *mupa,
6479 __isl_take isl_multi_aff *ma);
6480 const char *arg1;
6481 const char *arg2;
6482 const char *res;
6483 } mupa_ma_tests[] = {
6484 { &isl_multi_union_pw_aff_apply_multi_aff,
6485 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6486 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6487 "{ C[a,b] -> D[b,a] }",
6488 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
6489 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
6490 { &isl_multi_union_pw_aff_apply_multi_aff,
6491 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
6492 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6493 "{ C[a,b] -> D[b,a] }",
6494 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
6495 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
6496 { &isl_multi_union_pw_aff_apply_multi_aff,
6497 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6498 "[N] -> { C[a] -> D[a + N] }",
6499 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }] " },
6500 { &isl_multi_union_pw_aff_apply_multi_aff,
6501 "C[]",
6502 "{ C[] -> D[] }",
6503 "D[]" },
6504 { &isl_multi_union_pw_aff_apply_multi_aff,
6505 "[N] -> (C[] : { : N >= 0 })",
6506 "{ C[] -> D[] }",
6507 "[N] -> (D[] : { : N >= 0 })" },
6508 { &isl_multi_union_pw_aff_apply_multi_aff,
6509 "C[]",
6510 "[N] -> { C[] -> D[N] }",
6511 "[N] -> D[{ [N] }]" },
6512 { &isl_multi_union_pw_aff_apply_multi_aff,
6513 "(C[] : { A[i,j] : i >= j })",
6514 "{ C[] -> D[] }",
6515 "(D[] : { A[i,j] : i >= j })" },
6516 { &isl_multi_union_pw_aff_apply_multi_aff,
6517 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6518 "{ C[] -> D[] }",
6519 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6520 { &isl_multi_union_pw_aff_apply_multi_aff,
6521 "(C[] : { A[i,j] : i >= j })",
6522 "[N] -> { C[] -> D[N] }",
6523 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6526 /* Perform some basic tests of binary operations on
6527 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
6529 static int test_mupa_ma(isl_ctx *ctx)
6531 int i;
6532 isl_bool ok;
6533 isl_multi_union_pw_aff *mupa, *res;
6534 isl_multi_aff *ma;
6536 for (i = 0; i < ARRAY_SIZE(mupa_ma_tests); ++i) {
6537 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6538 mupa_ma_tests[i].arg1);
6539 ma = isl_multi_aff_read_from_str(ctx, mupa_ma_tests[i].arg2);
6540 res = isl_multi_union_pw_aff_read_from_str(ctx,
6541 mupa_ma_tests[i].res);
6542 mupa = mupa_ma_tests[i].fn(mupa, ma);
6543 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6544 isl_multi_union_pw_aff_free(mupa);
6545 isl_multi_union_pw_aff_free(res);
6546 if (ok < 0)
6547 return -1;
6548 if (!ok)
6549 isl_die(ctx, isl_error_unknown,
6550 "unexpected result", return -1);
6553 return 0;
6556 /* Inputs for basic tests of binary operations on
6557 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
6558 * "fn" is the function that is tested.
6559 * "arg1" and "arg2" are string descriptions of the inputs.
6560 * "res" is a string description of the expected result.
6562 struct {
6563 __isl_give isl_union_pw_aff *(*fn)(
6564 __isl_take isl_multi_union_pw_aff *mupa,
6565 __isl_take isl_pw_aff *pa);
6566 const char *arg1;
6567 const char *arg2;
6568 const char *res;
6569 } mupa_pa_tests[] = {
6570 { &isl_multi_union_pw_aff_apply_pw_aff,
6571 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6572 "[N] -> { C[a] -> [a + N] }",
6573 "[N] -> { A[i,j] -> [i + N]; B[i,j] -> [j + N] }" },
6574 { &isl_multi_union_pw_aff_apply_pw_aff,
6575 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6576 "{ C[a] -> [a] : a >= 0; C[a] -> [-a] : a < 0 }",
6577 "{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
6578 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }" },
6579 { &isl_multi_union_pw_aff_apply_pw_aff,
6580 "C[]",
6581 "[N] -> { C[] -> [N] }",
6582 "[N] -> { [N] }" },
6583 { &isl_multi_union_pw_aff_apply_pw_aff,
6584 "C[]",
6585 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
6586 "[N] -> { [N] : N >= 0; [-N] : N < 0 }" },
6587 { &isl_multi_union_pw_aff_apply_pw_aff,
6588 "[N] -> (C[] : { : N >= 0 })",
6589 "[N] -> { C[] -> [N] }",
6590 "[N] -> { [N] : N >= 0 }" },
6591 { &isl_multi_union_pw_aff_apply_pw_aff,
6592 "[N] -> (C[] : { : N >= 0 })",
6593 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
6594 "[N] -> { [N] : N >= 0 }" },
6595 { &isl_multi_union_pw_aff_apply_pw_aff,
6596 "[N] -> (C[] : { : N >= 0 })",
6597 "{ C[] -> [0] }",
6598 "[N] -> { [0] : N >= 0 }" },
6599 { &isl_multi_union_pw_aff_apply_pw_aff,
6600 "(C[] : { A[i,j] : i >= j })",
6601 "[N] -> { C[] -> [N] }",
6602 "[N] -> { A[i,j] -> [N] : i >= j }" },
6603 { &isl_multi_union_pw_aff_apply_pw_aff,
6604 "(C[] : { A[i,j] : i >= j })",
6605 "[N] -> { C[] -> [N] : N >= 0 }",
6606 "[N] -> { A[i,j] -> [N] : i >= j and N >= 0 }" },
6609 /* Perform some basic tests of binary operations on
6610 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
6612 static int test_mupa_pa(isl_ctx *ctx)
6614 int i;
6615 isl_bool ok;
6616 isl_multi_union_pw_aff *mupa;
6617 isl_union_pw_aff *upa, *res;
6618 isl_pw_aff *pa;
6620 for (i = 0; i < ARRAY_SIZE(mupa_pa_tests); ++i) {
6621 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6622 mupa_pa_tests[i].arg1);
6623 pa = isl_pw_aff_read_from_str(ctx, mupa_pa_tests[i].arg2);
6624 res = isl_union_pw_aff_read_from_str(ctx,
6625 mupa_pa_tests[i].res);
6626 upa = mupa_pa_tests[i].fn(mupa, pa);
6627 ok = isl_union_pw_aff_plain_is_equal(upa, res);
6628 isl_union_pw_aff_free(upa);
6629 isl_union_pw_aff_free(res);
6630 if (ok < 0)
6631 return -1;
6632 if (!ok)
6633 isl_die(ctx, isl_error_unknown,
6634 "unexpected result", return -1);
6637 return 0;
6640 /* Inputs for basic tests of binary operations on
6641 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6642 * "fn" is the function that is tested.
6643 * "arg1" and "arg2" are string descriptions of the inputs.
6644 * "res" is a string description of the expected result.
6646 struct {
6647 __isl_give isl_multi_union_pw_aff *(*fn)(
6648 __isl_take isl_multi_union_pw_aff *mupa,
6649 __isl_take isl_pw_multi_aff *pma);
6650 const char *arg1;
6651 const char *arg2;
6652 const char *res;
6653 } mupa_pma_tests[] = {
6654 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6655 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6656 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6657 "{ C[a,b] -> D[b,a] }",
6658 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
6659 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
6660 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6661 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
6662 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6663 "{ C[a,b] -> D[b,a] }",
6664 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
6665 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
6666 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6667 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6668 "[N] -> { C[a] -> D[a + N] }",
6669 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }]" },
6670 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6671 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6672 "{ C[a] -> D[a] : a >= 0; C[a] -> D[-a] : a < 0 }",
6673 "D[{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
6674 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }]" },
6675 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6676 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6677 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6678 "{ C[a,b] -> D[a,b] : a >= b; C[a,b] -> D[b,a] : a < b }",
6679 "D[{ A[i,j] -> [i] : i >= j; A[i,j] -> [j] : i < j; "
6680 "B[i,j] -> [j] : i <= j; B[i,j] -> [i] : i > j }, "
6681 "{ A[i,j] -> [j] : i >= j; A[i,j] -> [i] : i < j; "
6682 "B[i,j] -> [i] : i <= j; B[i,j] -> [j] : i > j }]" },
6683 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6684 "C[]",
6685 "{ C[] -> D[] }",
6686 "D[]" },
6687 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6688 "[N] -> (C[] : { : N >= 0 })",
6689 "{ C[] -> D[] }",
6690 "[N] -> (D[] : { : N >= 0 })" },
6691 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6692 "C[]",
6693 "[N] -> { C[] -> D[N] }",
6694 "[N] -> D[{ [N] }]" },
6695 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6696 "(C[] : { A[i,j] : i >= j })",
6697 "{ C[] -> D[] }",
6698 "(D[] : { A[i,j] : i >= j })" },
6699 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6700 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6701 "{ C[] -> D[] }",
6702 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6703 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6704 "(C[] : { A[i,j] : i >= j })",
6705 "[N] -> { C[] -> D[N] }",
6706 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6707 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6708 "C[]",
6709 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6710 "[N] -> D[{ [N] : N >= 0; [-N] : N < 0 }]" },
6711 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6712 "[N] -> (C[] : { : N >= 0 })",
6713 "[N] -> { C[] -> D[N] }",
6714 "[N] -> D[{ [N] : N >= 0 }]" },
6715 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6716 "[N] -> (C[] : { : N >= 0 })",
6717 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6718 "[N] -> D[{ [N] : N >= 0 }]" },
6719 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6720 "[N] -> (C[] : { : N >= 0 })",
6721 "{ C[] -> D[0] }",
6722 "[N] -> D[{ [0] : N >= 0 }]" },
6723 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6724 "(C[] : { A[i,j] : i >= j })",
6725 "[N] -> { C[] -> D[N] : N >= 0 }",
6726 "[N] -> D[{ A[i,j] -> [N] : i >= j and N >= 0 }]" },
6729 /* Perform some basic tests of binary operations on
6730 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6732 static int test_mupa_pma(isl_ctx *ctx)
6734 int i;
6735 isl_bool ok;
6736 isl_multi_union_pw_aff *mupa, *res;
6737 isl_pw_multi_aff *pma;
6739 for (i = 0; i < ARRAY_SIZE(mupa_pma_tests); ++i) {
6740 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6741 mupa_pma_tests[i].arg1);
6742 pma = isl_pw_multi_aff_read_from_str(ctx,
6743 mupa_pma_tests[i].arg2);
6744 res = isl_multi_union_pw_aff_read_from_str(ctx,
6745 mupa_pma_tests[i].res);
6746 mupa = mupa_pma_tests[i].fn(mupa, pma);
6747 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6748 isl_multi_union_pw_aff_free(mupa);
6749 isl_multi_union_pw_aff_free(res);
6750 if (ok < 0)
6751 return -1;
6752 if (!ok)
6753 isl_die(ctx, isl_error_unknown,
6754 "unexpected result", return -1);
6757 return 0;
6760 /* Inputs for basic tests of binary operations on
6761 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6762 * "fn" is the function that is tested.
6763 * "arg1" and "arg2" are string descriptions of the inputs.
6764 * "res" is a string description of the expected result.
6766 struct {
6767 __isl_give isl_multi_union_pw_aff *(*fn)(
6768 __isl_take isl_multi_union_pw_aff *mupa,
6769 __isl_take isl_union_pw_multi_aff *upma);
6770 const char *arg1;
6771 const char *arg2;
6772 const char *res;
6773 } mupa_upma_tests[] = {
6774 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6775 "C[{ B[i,j] -> [i + 2j] }]", "{ A[a,b] -> B[b,a] }",
6776 "C[{ A[a,b] -> [b + 2a] }]" },
6777 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6778 "C[{ B[i,j] -> [i + 2j] }]",
6779 "{ A[a,b] -> B[b,a] : b > a }",
6780 "C[{ A[a,b] -> [b + 2a] : b > a }]" },
6781 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6782 "C[{ B[i,j] -> [i + 2j] : j > 4 }]",
6783 "{ A[a,b] -> B[b,a] : b > a }",
6784 "C[{ A[a,b] -> [b + 2a] : b > a > 4 }]" },
6785 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6786 "C[{ B[i,j] -> [i + 2j] }]",
6787 "{ A[a,b] -> B[b,a] : a > b; A[a,b] -> B[a,b] : a <= b }",
6788 "C[{ A[a,b] -> [b + 2a] : a > b; A[a,b] -> [a + 2b] : a <= b }]" },
6789 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6790 "(C[] : { B[a,b] })",
6791 "{ A[a,b] -> B[b,a] }",
6792 "(C[] : { A[a,b] })" },
6793 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6794 "(C[] : { B[a,b] })",
6795 "{ B[a,b] -> A[b,a] }",
6796 "(C[] : { })" },
6797 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6798 "(C[] : { B[a,b] })",
6799 "{ A[a,b] -> B[b,a] : a > b }",
6800 "(C[] : { A[a,b] : a > b })" },
6801 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6802 "(C[] : { B[a,b] : a > b })",
6803 "{ A[a,b] -> B[b,a] }",
6804 "(C[] : { A[a,b] : b > a })" },
6805 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6806 "[N] -> (C[] : { B[a,b] : a > N })",
6807 "{ A[a,b] -> B[b,a] : a > b }",
6808 "[N] -> (C[] : { A[a,b] : a > b > N })" },
6809 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6810 "(C[] : { B[a,b] : a > b })",
6811 "[N] -> { A[a,b] -> B[b,a] : a > N }",
6812 "[N] -> (C[] : { A[a,b] : b > a > N })" },
6813 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6814 "C[]",
6815 "{ A[a,b] -> B[b,a] }",
6816 "C[]" },
6817 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6818 "[N] -> (C[] : { : N >= 0 })",
6819 "{ A[a,b] -> B[b,a] }",
6820 "[N] -> (C[] : { : N >= 0 })" },
6821 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6822 "C[]",
6823 "[N] -> { A[a,b] -> B[b,a] : N >= 0 }",
6824 "[N] -> (C[] : { : N >= 0 })" },
6827 /* Perform some basic tests of binary operations on
6828 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6830 static int test_mupa_upma(isl_ctx *ctx)
6832 int i;
6833 isl_bool ok;
6834 isl_multi_union_pw_aff *mupa, *res;
6835 isl_union_pw_multi_aff *upma;
6837 for (i = 0; i < ARRAY_SIZE(mupa_upma_tests); ++i) {
6838 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6839 mupa_upma_tests[i].arg1);
6840 upma = isl_union_pw_multi_aff_read_from_str(ctx,
6841 mupa_upma_tests[i].arg2);
6842 res = isl_multi_union_pw_aff_read_from_str(ctx,
6843 mupa_upma_tests[i].res);
6844 mupa = mupa_upma_tests[i].fn(mupa, upma);
6845 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6846 isl_multi_union_pw_aff_free(mupa);
6847 isl_multi_union_pw_aff_free(res);
6848 if (ok < 0)
6849 return -1;
6850 if (!ok)
6851 isl_die(ctx, isl_error_unknown,
6852 "unexpected result", return -1);
6855 return 0;
6858 /* Check that the input tuple of an isl_aff can be set properly.
6860 static isl_stat test_aff_set_tuple_id(isl_ctx *ctx)
6862 isl_id *id;
6863 isl_aff *aff;
6864 isl_stat equal;
6866 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x + 1] }");
6867 id = isl_id_alloc(ctx, "A", NULL);
6868 aff = isl_aff_set_tuple_id(aff, isl_dim_in, id);
6869 equal = aff_check_plain_equal(aff, "{ A[x] -> [x + 1] }");
6870 isl_aff_free(aff);
6871 if (equal < 0)
6872 return isl_stat_error;
6874 return isl_stat_ok;
6877 /* Check that affine expressions get normalized on addition/subtraction.
6878 * In particular, check that (final) unused integer divisions get removed
6879 * such that an expression derived from expressions with integer divisions
6880 * is found to be obviously equal to one that is created directly.
6882 static isl_stat test_aff_normalize(isl_ctx *ctx)
6884 isl_aff *aff, *aff2;
6885 isl_stat ok;
6887 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x//2] }");
6888 aff2 = isl_aff_read_from_str(ctx, "{ [x] -> [1 + x//2] }");
6889 aff = isl_aff_sub(aff2, aff);
6890 ok = aff_check_plain_equal(aff, "{ [x] -> [1] }");
6891 isl_aff_free(aff);
6893 return ok;
6896 int test_aff(isl_ctx *ctx)
6898 const char *str;
6899 isl_set *set;
6900 isl_space *space;
6901 isl_local_space *ls;
6902 isl_aff *aff;
6903 int zero;
6904 isl_stat equal;
6906 if (test_upa(ctx) < 0)
6907 return -1;
6908 if (test_bin_aff(ctx) < 0)
6909 return -1;
6910 if (test_bin_pw_aff(ctx) < 0)
6911 return -1;
6912 if (test_upma_test(ctx) < 0)
6913 return -1;
6914 if (test_bin_upma(ctx) < 0)
6915 return -1;
6916 if (test_bin_upma_fail(ctx) < 0)
6917 return -1;
6918 if (test_upma_uset(ctx) < 0)
6919 return -1;
6920 if (test_un_mpa(ctx) < 0)
6921 return -1;
6922 if (test_bin_mpa(ctx) < 0)
6923 return -1;
6924 if (test_un_mupa(ctx) < 0)
6925 return -1;
6926 if (test_bin_mupa(ctx) < 0)
6927 return -1;
6928 if (test_mupa_set(ctx) < 0)
6929 return -1;
6930 if (test_mupa_uset(ctx) < 0)
6931 return -1;
6932 if (test_mupa_ma(ctx) < 0)
6933 return -1;
6934 if (test_mupa_pa(ctx) < 0)
6935 return -1;
6936 if (test_mupa_pma(ctx) < 0)
6937 return -1;
6938 if (test_mupa_upma(ctx) < 0)
6939 return -1;
6941 space = isl_space_set_alloc(ctx, 0, 1);
6942 ls = isl_local_space_from_space(space);
6943 aff = isl_aff_zero_on_domain(ls);
6945 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6946 aff = isl_aff_scale_down_ui(aff, 3);
6947 aff = isl_aff_floor(aff);
6948 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6949 aff = isl_aff_scale_down_ui(aff, 2);
6950 aff = isl_aff_floor(aff);
6951 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6953 str = "{ [10] }";
6954 set = isl_set_read_from_str(ctx, str);
6955 aff = isl_aff_gist(aff, set);
6957 aff = isl_aff_add_constant_si(aff, -16);
6958 zero = isl_aff_plain_is_zero(aff);
6959 isl_aff_free(aff);
6961 if (zero < 0)
6962 return -1;
6963 if (!zero)
6964 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6966 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
6967 aff = isl_aff_scale_down_ui(aff, 64);
6968 aff = isl_aff_floor(aff);
6969 equal = aff_check_plain_equal(aff, "{ [-1] }");
6970 isl_aff_free(aff);
6971 if (equal < 0)
6972 return -1;
6974 if (test_aff_set_tuple_id(ctx) < 0)
6975 return -1;
6976 if (test_aff_normalize(ctx) < 0)
6977 return -1;
6979 return 0;
6982 /* Inputs for isl_set_bind tests.
6983 * "set" is the input set.
6984 * "tuple" is the binding tuple.
6985 * "res" is the expected result.
6987 static
6988 struct {
6989 const char *set;
6990 const char *tuple;
6991 const char *res;
6992 } bind_set_tests[] = {
6993 { "{ A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
6994 "{ A[M, N] }",
6995 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
6996 { "{ B[N, M] : M mod 2 = 0 and N mod 8 = 3 }",
6997 "{ B[N, M] }",
6998 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
6999 { "[M] -> { C[N] : M mod 2 = 0 and N mod 8 = 3 }",
7000 "{ C[N] }",
7001 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
7002 { "[M] -> { D[x, N] : x mod 2 = 0 and N mod 8 = 3 and M >= 0 }",
7003 "{ D[M, N] }",
7004 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 and M >= 0 }" },
7007 /* Perform basic isl_set_bind tests.
7009 static isl_stat test_bind_set(isl_ctx *ctx)
7011 int i;
7013 for (i = 0; i < ARRAY_SIZE(bind_set_tests); ++i) {
7014 const char *str;
7015 isl_set *set;
7016 isl_multi_id *tuple;
7017 isl_stat r;
7019 set = isl_set_read_from_str(ctx, bind_set_tests[i].set);
7020 str = bind_set_tests[i].tuple;
7021 tuple = isl_multi_id_read_from_str(ctx, str);
7022 set = isl_set_bind(set, tuple);
7023 r = set_check_equal(set, bind_set_tests[i].res);
7024 isl_set_free(set);
7025 if (r < 0)
7026 return isl_stat_error;
7029 return isl_stat_ok;
7032 /* Inputs for isl_map_bind_domain tests.
7033 * "map" is the input map.
7034 * "tuple" is the binding tuple.
7035 * "res" is the expected result.
7037 struct {
7038 const char *map;
7039 const char *tuple;
7040 const char *res;
7041 } bind_map_domain_tests[] = {
7042 { "{ A[M, N] -> [M + floor(N/2)] }",
7043 "{ A[M, N] }",
7044 "[M, N] -> { [M + floor(N/2)] }" },
7045 { "{ B[N, M] -> [M + floor(N/2)] }",
7046 "{ B[N, M] }",
7047 "[N, M] -> { [M + floor(N/2)] }" },
7048 { "[M] -> { C[N] -> [M + floor(N/2)] }",
7049 "{ C[N] }",
7050 "[M, N] -> { [M + floor(N/2)] }" },
7051 { "[M] -> { C[x, N] -> [x + floor(N/2)] }",
7052 "{ C[M, N] }",
7053 "[M, N] -> { [M + floor(N/2)] }" },
7054 { "[M] -> { C[x, N] -> [M + floor(N/2)] }",
7055 "{ C[M, N] }",
7056 "[M, N] -> { [M + floor(N/2)] }" },
7057 { "[A, M] -> { C[N, x] -> [x + floor(N/2)] }",
7058 "{ C[N, M] }",
7059 "[A, N, M] -> { [M + floor(N/2)] }" },
7062 /* Perform basic isl_map_bind_domain tests.
7064 static isl_stat test_bind_map_domain(isl_ctx *ctx)
7066 int i;
7068 for (i = 0; i < ARRAY_SIZE(bind_map_domain_tests); ++i) {
7069 const char *str;
7070 isl_map *map;
7071 isl_set *set;
7072 isl_multi_id *tuple;
7073 isl_stat r;
7075 str = bind_map_domain_tests[i].map;
7076 map = isl_map_read_from_str(ctx, str);
7077 str = bind_map_domain_tests[i].tuple;
7078 tuple = isl_multi_id_read_from_str(ctx, str);
7079 set = isl_map_bind_domain(map, tuple);
7080 str = bind_map_domain_tests[i].res;
7081 r = set_check_equal(set, str);
7082 isl_set_free(set);
7083 if (r < 0)
7084 return isl_stat_error;
7087 return isl_stat_ok;
7090 /* Inputs for isl_union_map_bind_range tests.
7091 * "map" is the input union map.
7092 * "tuple" is the binding tuple.
7093 * "res" is the expected result.
7095 struct {
7096 const char *map;
7097 const char *tuple;
7098 const char *res;
7099 } bind_umap_range_tests[] = {
7100 { "{ B[N, M] -> A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
7101 "{ A[M, N] }",
7102 "[M, N] -> { B[N, M] : M mod 2 = 0 and N mod 8 = 3 }" },
7103 { "{ B[N, M] -> A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
7104 "{ B[M, N] }",
7105 "{ }" },
7106 { "{ A[] -> B[]; C[] -> D[]; E[] -> B[] }",
7107 "{ B[] }",
7108 "{ A[]; E[] }" },
7111 /* Perform basic isl_union_map_bind_range tests.
7113 static isl_stat test_bind_umap_range(isl_ctx *ctx)
7115 int i;
7117 for (i = 0; i < ARRAY_SIZE(bind_umap_range_tests); ++i) {
7118 const char *str;
7119 isl_union_map *umap;
7120 isl_union_set *uset;
7121 isl_multi_id *tuple;
7122 isl_stat r;
7124 str = bind_umap_range_tests[i].map;
7125 umap = isl_union_map_read_from_str(ctx, str);
7126 str = bind_umap_range_tests[i].tuple;
7127 tuple = isl_multi_id_read_from_str(ctx, str);
7128 uset = isl_union_map_bind_range(umap, tuple);
7129 str = bind_umap_range_tests[i].res;
7130 r = uset_check_equal(uset, str);
7131 isl_union_set_free(uset);
7132 if (r < 0)
7133 return isl_stat_error;
7136 return isl_stat_ok;
7139 /* Inputs for isl_pw_multi_aff_bind_domain tests.
7140 * "pma" is the input expression.
7141 * "tuple" is the binding tuple.
7142 * "res" is the expected result.
7144 struct {
7145 const char *pma;
7146 const char *tuple;
7147 const char *res;
7148 } bind_pma_domain_tests[] = {
7149 { "{ A[M, N] -> [M + floor(N/2)] }",
7150 "{ A[M, N] }",
7151 "[M, N] -> { [M + floor(N/2)] }" },
7152 { "{ B[N, M] -> [M + floor(N/2)] }",
7153 "{ B[N, M] }",
7154 "[N, M] -> { [M + floor(N/2)] }" },
7155 { "[M] -> { C[N] -> [M + floor(N/2)] }",
7156 "{ C[N] }",
7157 "[M, N] -> { [M + floor(N/2)] }" },
7158 { "[M] -> { C[x, N] -> [x + floor(N/2)] }",
7159 "{ C[M, N] }",
7160 "[M, N] -> { [M + floor(N/2)] }" },
7161 { "[M] -> { C[x, N] -> [M + floor(N/2)] }",
7162 "{ C[M, N] }",
7163 "[M, N] -> { [M + floor(N/2)] }" },
7164 { "[A, M] -> { C[N, x] -> [x + floor(N/2)] }",
7165 "{ C[N, M] }",
7166 "[A, N, M] -> { [M + floor(N/2)] }" },
7169 /* Perform basic isl_pw_multi_aff_bind_domain tests.
7171 static isl_stat test_bind_pma_domain(isl_ctx *ctx)
7173 int i;
7175 for (i = 0; i < ARRAY_SIZE(bind_pma_domain_tests); ++i) {
7176 const char *str;
7177 isl_pw_multi_aff *pma;
7178 isl_multi_id *tuple;
7179 isl_stat r;
7181 str = bind_pma_domain_tests[i].pma;
7182 pma = isl_pw_multi_aff_read_from_str(ctx, str);
7183 str = bind_pma_domain_tests[i].tuple;
7184 tuple = isl_multi_id_read_from_str(ctx, str);
7185 pma = isl_pw_multi_aff_bind_domain(pma, tuple);
7186 str = bind_pma_domain_tests[i].res;
7187 r = pw_multi_aff_check_plain_equal(pma, str);
7188 isl_pw_multi_aff_free(pma);
7189 if (r < 0)
7190 return isl_stat_error;
7193 return isl_stat_ok;
7196 /* Inputs for isl_pw_multi_aff_bind_domain_wrapped_domain tests.
7197 * "pma" is the input expression.
7198 * "tuple" is the binding tuple.
7199 * "res" is the expected result.
7201 struct {
7202 const char *pma;
7203 const char *tuple;
7204 const char *res;
7205 } bind_pma_domain_wrapped_tests[] = {
7206 { "{ [A[M, N] -> B[]] -> [M + floor(N/2)] }",
7207 "{ A[M, N] }",
7208 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
7209 { "{ [B[N, M] -> D[]] -> [M + floor(N/2)] }",
7210 "{ B[N, M] }",
7211 "[N, M] -> { D[] -> [M + floor(N/2)] }" },
7212 { "[M] -> { [C[N] -> B[x]] -> [x + M + floor(N/2)] }",
7213 "{ C[N] }",
7214 "[M, N] -> { B[x] -> [x + M + floor(N/2)] }" },
7215 { "[M] -> { [C[x, N] -> B[]] -> [x + floor(N/2)] }",
7216 "{ C[M, N] }",
7217 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
7218 { "[M] -> { [C[x, N] -> B[]] -> [M + floor(N/2)] }",
7219 "{ C[M, N] }",
7220 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
7221 { "[A, M] -> { [C[N, x] -> B[]] -> [x + floor(N/2)] }",
7222 "{ C[N, M] }",
7223 "[A, N, M] -> { B[] -> [M + floor(N/2)] }" },
7226 /* Perform basic isl_pw_multi_aff_bind_domain_wrapped_domain tests.
7228 static isl_stat test_bind_pma_domain_wrapped(isl_ctx *ctx)
7230 int i;
7232 for (i = 0; i < ARRAY_SIZE(bind_pma_domain_wrapped_tests); ++i) {
7233 const char *str;
7234 isl_pw_multi_aff *pma;
7235 isl_multi_id *tuple;
7236 isl_stat r;
7238 str = bind_pma_domain_wrapped_tests[i].pma;
7239 pma = isl_pw_multi_aff_read_from_str(ctx, str);
7240 str = bind_pma_domain_wrapped_tests[i].tuple;
7241 tuple = isl_multi_id_read_from_str(ctx, str);
7242 pma = isl_pw_multi_aff_bind_domain_wrapped_domain(pma, tuple);
7243 str = bind_pma_domain_wrapped_tests[i].res;
7244 r = pw_multi_aff_check_plain_equal(pma, str);
7245 isl_pw_multi_aff_free(pma);
7246 if (r < 0)
7247 return isl_stat_error;
7250 return isl_stat_ok;
7253 /* Inputs for isl_aff_bind_id tests.
7254 * "aff" is the input expression.
7255 * "id" is the binding id.
7256 * "res" is the expected result.
7258 static
7259 struct {
7260 const char *aff;
7261 const char *id;
7262 const char *res;
7263 } bind_aff_tests[] = {
7264 { "{ [4] }", "M", "[M = 4] -> { : }" },
7265 { "{ B[x] -> [floor(x/2)] }", "M", "[M] -> { B[x] : M = floor(x/2) }" },
7266 { "[M] -> { [4] }", "M", "[M = 4] -> { : }" },
7267 { "[M] -> { [floor(M/2)] }", "M", "[M] -> { : floor(M/2) = M }" },
7268 { "{ [NaN] }", "M", "{ : false }" },
7269 { "{ A[x] -> [NaN] }", "M", "{ A[x] : false }" },
7272 /* Perform basic isl_aff_bind_id tests.
7274 static isl_stat test_bind_aff(isl_ctx *ctx)
7276 int i;
7278 for (i = 0; i < ARRAY_SIZE(bind_aff_tests); ++i) {
7279 isl_aff *aff;
7280 isl_set *res;
7281 isl_id *id;
7282 isl_stat r;
7284 aff = isl_aff_read_from_str(ctx, bind_aff_tests[i].aff);
7285 id = isl_id_read_from_str(ctx, bind_aff_tests[i].id);
7286 res = isl_set_from_basic_set(isl_aff_bind_id(aff, id));
7287 r = set_check_equal(res, bind_aff_tests[i].res);
7288 isl_set_free(res);
7289 if (r < 0)
7290 return isl_stat_error;
7293 return isl_stat_ok;
7296 /* Inputs for isl_pw_aff_bind_id tests.
7297 * "pa" is the input expression.
7298 * "id" is the binding id.
7299 * "res" is the expected result.
7301 static
7302 struct {
7303 const char *pa;
7304 const char *id;
7305 const char *res;
7306 } bind_pa_tests[] = {
7307 { "{ [4] }", "M", "[M = 4] -> { : }" },
7308 { "{ B[x] -> [floor(x/2)] }", "M", "[M] -> { B[x] : M = floor(x/2) }" },
7309 { "[M] -> { [4] }", "M", "[M = 4] -> { : }" },
7310 { "[M] -> { [floor(M/2)] }", "M", "[M] -> { : floor(M/2) = M }" },
7311 { "[M] -> { [M] : M >= 0; [-M] : M < 0 }", "M", "[M] -> { : M >= 0 }" },
7312 { "{ [NaN] }", "M", "{ : false }" },
7313 { "{ A[x] -> [NaN] }", "M", "{ A[x] : false }" },
7316 /* Perform basic isl_pw_aff_bind_id tests.
7318 static isl_stat test_bind_pa(isl_ctx *ctx)
7320 int i;
7322 for (i = 0; i < ARRAY_SIZE(bind_pa_tests); ++i) {
7323 isl_pw_aff *pa;
7324 isl_set *res;
7325 isl_id *id;
7326 isl_stat r;
7328 pa = isl_pw_aff_read_from_str(ctx, bind_pa_tests[i].pa);
7329 id = isl_id_read_from_str(ctx, bind_pa_tests[i].id);
7330 res = isl_pw_aff_bind_id(pa, id);
7331 r = set_check_equal(res, bind_pa_tests[i].res);
7332 isl_set_free(res);
7333 if (r < 0)
7334 return isl_stat_error;
7337 return isl_stat_ok;
7340 /* Inputs for isl_multi_union_pw_aff_bind tests.
7341 * "mupa" is the input expression.
7342 * "tuple" is the binding tuple.
7343 * "res" is the expected result.
7345 static
7346 struct {
7347 const char *mupa;
7348 const char *tuple;
7349 const char *res;
7350 } bind_mupa_tests[] = {
7351 { "A[{ [4] }, { [5] }]",
7352 "{ A[M, N] }",
7353 "[M = 4, N = 5] -> { : }" },
7354 { "A[{ B[x] -> [floor(x/2)] }, { B[y] -> [y + 5] }]",
7355 "{ A[M, N] }",
7356 "[M, N] -> { B[x] : M = floor(x/2) and N = x + 5 }" },
7357 { "[M] -> A[{ [4] }, { [M + 1] }]",
7358 "{ A[M, N] }",
7359 "[M = 4, N = 5] -> { : }" },
7362 /* Perform basic isl_multi_union_pw_aff_bind tests.
7364 static isl_stat test_bind_mupa(isl_ctx *ctx)
7366 int i;
7368 for (i = 0; i < ARRAY_SIZE(bind_mupa_tests); ++i) {
7369 const char *str;
7370 isl_multi_union_pw_aff *mupa;
7371 isl_union_set *res;
7372 isl_multi_id *tuple;
7373 isl_stat r;
7375 str = bind_mupa_tests[i].mupa;
7376 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7377 str = bind_mupa_tests[i].tuple;
7378 tuple = isl_multi_id_read_from_str(ctx, str);
7379 res = isl_multi_union_pw_aff_bind(mupa, tuple);
7380 r = uset_check_equal(res, bind_mupa_tests[i].res);
7381 isl_union_set_free(res);
7382 if (r < 0)
7383 return isl_stat_error;
7386 return isl_stat_ok;
7389 /* Perform tests that reinterpret dimensions as parameters.
7391 static int test_bind(isl_ctx *ctx)
7393 if (test_bind_set(ctx) < 0)
7394 return -1;
7395 if (test_bind_map_domain(ctx) < 0)
7396 return -1;
7397 if (test_bind_umap_range(ctx) < 0)
7398 return -1;
7399 if (test_bind_pma_domain(ctx) < 0)
7400 return -1;
7401 if (test_bind_pma_domain_wrapped(ctx) < 0)
7402 return -1;
7403 if (test_bind_aff(ctx) < 0)
7404 return -1;
7405 if (test_bind_pa(ctx) < 0)
7406 return -1;
7407 if (test_bind_mupa(ctx) < 0)
7408 return -1;
7410 return 0;
7413 /* Inputs for isl_set_unbind_params tests.
7414 * "set" is the input parameter domain.
7415 * "tuple" is the tuple of the constructed set.
7416 * "res" is the expected result.
7418 struct {
7419 const char *set;
7420 const char *tuple;
7421 const char *res;
7422 } unbind_set_tests[] = {
7423 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7424 "{ A[M, N] }",
7425 "{ A[M, N] : M mod 2 = 0 and N mod 8 = 3 }" },
7426 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7427 "{ B[N, M] }",
7428 "{ B[N, M] : M mod 2 = 0 and N mod 8 = 3 }" },
7429 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7430 "{ C[N] }",
7431 "[M] -> { C[N] : M mod 2 = 0 and N mod 8 = 3 }" },
7432 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7433 "{ D[T, N] }",
7434 "[M] -> { D[x, N] : M mod 2 = 0 and N mod 8 = 3 }" },
7437 /* Perform basic isl_set_unbind_params tests.
7439 static isl_stat test_unbind_set(isl_ctx *ctx)
7441 int i;
7443 for (i = 0; i < ARRAY_SIZE(unbind_set_tests); ++i) {
7444 const char *str;
7445 isl_set *set;
7446 isl_multi_id *tuple;
7447 isl_stat r;
7449 set = isl_set_read_from_str(ctx, unbind_set_tests[i].set);
7450 str = unbind_set_tests[i].tuple;
7451 tuple = isl_multi_id_read_from_str(ctx, str);
7452 set = isl_set_unbind_params(set, tuple);
7453 r = set_check_equal(set, unbind_set_tests[i].res);
7454 isl_set_free(set);
7455 if (r < 0)
7456 return isl_stat_error;
7459 return isl_stat_ok;
7462 /* Inputs for isl_aff_unbind_params_insert_domain tests.
7463 * "aff" is the input affine expression defined over a parameter domain.
7464 * "tuple" is the tuple of the domain that gets introduced.
7465 * "res" is the expected result.
7467 struct {
7468 const char *aff;
7469 const char *tuple;
7470 const char *res;
7471 } unbind_aff_tests[] = {
7472 { "[M, N] -> { [M + floor(N/2)] }",
7473 "{ A[M, N] }",
7474 "{ A[M, N] -> [M + floor(N/2)] }" },
7475 { "[M, N] -> { [M + floor(N/2)] }",
7476 "{ B[N, M] }",
7477 "{ B[N, M] -> [M + floor(N/2)] }" },
7478 { "[M, N] -> { [M + floor(N/2)] }",
7479 "{ C[N] }",
7480 "[M] -> { C[N] -> [M + floor(N/2)] }" },
7481 { "[M, N] -> { [M + floor(N/2)] }",
7482 "{ D[A, B, C, N, Z] }",
7483 "[M] -> { D[A, B, C, N, Z] -> [M + floor(N/2)] }" },
7486 /* Perform basic isl_aff_unbind_params_insert_domain tests.
7488 static isl_stat test_unbind_aff(isl_ctx *ctx)
7490 int i;
7492 for (i = 0; i < ARRAY_SIZE(unbind_aff_tests); ++i) {
7493 const char *str;
7494 isl_aff *aff;
7495 isl_multi_id *tuple;
7496 isl_stat r;
7498 aff = isl_aff_read_from_str(ctx, unbind_aff_tests[i].aff);
7499 str = unbind_aff_tests[i].tuple;
7500 tuple = isl_multi_id_read_from_str(ctx, str);
7501 aff = isl_aff_unbind_params_insert_domain(aff, tuple);
7502 r = aff_check_plain_equal(aff, unbind_aff_tests[i].res);
7503 isl_aff_free(aff);
7504 if (r < 0)
7505 return isl_stat_error;
7508 return isl_stat_ok;
7511 /* Inputs for isl_multi_aff_unbind_params_insert_domain tests.
7512 * "ma" is the input multi affine expression defined over a parameter domain.
7513 * "tuple" is the tuple of the domain that gets introduced.
7514 * "res" is the expected result.
7516 static struct {
7517 const char *ma;
7518 const char *tuple;
7519 const char *res;
7520 } unbind_multi_aff_tests[] = {
7521 { "[M, N] -> { T[M + floor(N/2)] }",
7522 "{ A[M, N] }",
7523 "{ A[M, N] -> T[M + floor(N/2)] }" },
7524 { "[M, N] -> { [M + floor(N/2)] }",
7525 "{ B[N, M] }",
7526 "{ B[N, M] -> [M + floor(N/2)] }" },
7527 { "[M, N] -> { [M + floor(N/2)] }",
7528 "{ C[N] }",
7529 "[M] -> { C[N] -> [M + floor(N/2)] }" },
7530 { "[M, N] -> { [M + floor(N/2)] }",
7531 "{ D[A, B, C, N, Z] }",
7532 "[M] -> { D[A, B, C, N, Z] -> [M + floor(N/2)] }" },
7533 { "[M, N] -> { T[M + floor(N/2), N + floor(M/3)] }",
7534 "{ A[M, N] }",
7535 "{ A[M, N] -> T[M + floor(N/2), N + floor(M/3)] }" },
7538 /* Perform basic isl_multi_aff_unbind_params_insert_domain tests.
7540 static isl_stat test_unbind_multi_aff(isl_ctx *ctx)
7542 int i;
7544 for (i = 0; i < ARRAY_SIZE(unbind_multi_aff_tests); ++i) {
7545 const char *str;
7546 isl_multi_aff *ma;
7547 isl_multi_id *tuple;
7548 isl_stat r;
7550 str = unbind_multi_aff_tests[i].ma;
7551 ma = isl_multi_aff_read_from_str(ctx, str);
7552 str = unbind_multi_aff_tests[i].tuple;
7553 tuple = isl_multi_id_read_from_str(ctx, str);
7554 ma = isl_multi_aff_unbind_params_insert_domain(ma, tuple);
7555 str = unbind_multi_aff_tests[i].res;
7556 r = multi_aff_check_plain_equal(ma, str);
7557 isl_multi_aff_free(ma);
7558 if (r < 0)
7559 return isl_stat_error;
7562 return isl_stat_ok;
7565 /* Perform tests that reinterpret parameters.
7567 static int test_unbind(isl_ctx *ctx)
7569 if (test_unbind_set(ctx) < 0)
7570 return -1;
7571 if (test_unbind_aff(ctx) < 0)
7572 return -1;
7573 if (test_unbind_multi_aff(ctx) < 0)
7574 return -1;
7576 return 0;
7579 /* Check that "pa" consists of a single expression.
7581 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
7583 isl_size n;
7585 n = isl_pw_aff_n_piece(pa);
7586 isl_pw_aff_free(pa);
7588 if (n < 0)
7589 return -1;
7590 if (n != 1)
7591 isl_die(ctx, isl_error_unknown, "expecting single expression",
7592 return -1);
7594 return 0;
7597 /* Check that the computation below results in a single expression.
7598 * One or two expressions may result depending on which constraint
7599 * ends up being considered as redundant with respect to the other
7600 * constraints after the projection that is performed internally
7601 * by isl_set_dim_min.
7603 static int test_dim_max_1(isl_ctx *ctx)
7605 const char *str;
7606 isl_set *set;
7607 isl_pw_aff *pa;
7609 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
7610 "-4a <= b <= 3 and b < n - 4a }";
7611 set = isl_set_read_from_str(ctx, str);
7612 pa = isl_set_dim_min(set, 0);
7613 return check_single_piece(ctx, pa);
7616 /* Check that the computation below results in a single expression.
7617 * The PIP problem corresponding to these constraints has a row
7618 * that causes a split of the solution domain. The solver should
7619 * first pick rows that split off empty parts such that the actual
7620 * solution domain does not get split.
7621 * Note that the description contains some redundant constraints.
7622 * If these constraints get removed first, then the row mentioned
7623 * above does not appear in the PIP problem.
7625 static int test_dim_max_2(isl_ctx *ctx)
7627 const char *str;
7628 isl_set *set;
7629 isl_pw_aff *pa;
7631 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
7632 "N > 0 and P >= 0 }";
7633 set = isl_set_read_from_str(ctx, str);
7634 pa = isl_set_dim_max(set, 0);
7635 return check_single_piece(ctx, pa);
7638 int test_dim_max(isl_ctx *ctx)
7640 int equal;
7641 const char *str;
7642 isl_set *set1, *set2;
7643 isl_set *set;
7644 isl_map *map;
7645 isl_pw_aff *pwaff;
7647 if (test_dim_max_1(ctx) < 0)
7648 return -1;
7649 if (test_dim_max_2(ctx) < 0)
7650 return -1;
7652 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
7653 set = isl_set_read_from_str(ctx, str);
7654 pwaff = isl_set_dim_max(set, 0);
7655 set1 = isl_set_from_pw_aff(pwaff);
7656 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
7657 set2 = isl_set_read_from_str(ctx, str);
7658 equal = isl_set_is_equal(set1, set2);
7659 isl_set_free(set1);
7660 isl_set_free(set2);
7661 if (equal < 0)
7662 return -1;
7663 if (!equal)
7664 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7666 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
7667 set = isl_set_read_from_str(ctx, str);
7668 pwaff = isl_set_dim_max(set, 0);
7669 set1 = isl_set_from_pw_aff(pwaff);
7670 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
7671 set2 = isl_set_read_from_str(ctx, str);
7672 equal = isl_set_is_equal(set1, set2);
7673 isl_set_free(set1);
7674 isl_set_free(set2);
7675 if (equal < 0)
7676 return -1;
7677 if (!equal)
7678 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7680 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
7681 set = isl_set_read_from_str(ctx, str);
7682 pwaff = isl_set_dim_max(set, 0);
7683 set1 = isl_set_from_pw_aff(pwaff);
7684 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
7685 set2 = isl_set_read_from_str(ctx, str);
7686 equal = isl_set_is_equal(set1, set2);
7687 isl_set_free(set1);
7688 isl_set_free(set2);
7689 if (equal < 0)
7690 return -1;
7691 if (!equal)
7692 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7694 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
7695 "0 <= i < N and 0 <= j < M }";
7696 map = isl_map_read_from_str(ctx, str);
7697 set = isl_map_range(map);
7699 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
7700 set1 = isl_set_from_pw_aff(pwaff);
7701 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
7702 set2 = isl_set_read_from_str(ctx, str);
7703 equal = isl_set_is_equal(set1, set2);
7704 isl_set_free(set1);
7705 isl_set_free(set2);
7707 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
7708 set1 = isl_set_from_pw_aff(pwaff);
7709 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
7710 set2 = isl_set_read_from_str(ctx, str);
7711 if (equal >= 0 && equal)
7712 equal = isl_set_is_equal(set1, set2);
7713 isl_set_free(set1);
7714 isl_set_free(set2);
7716 isl_set_free(set);
7718 if (equal < 0)
7719 return -1;
7720 if (!equal)
7721 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7723 /* Check that solutions are properly merged. */
7724 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
7725 "c <= -1 + n - 4a - 2b and c >= -2b and "
7726 "4a >= -4 + n and c >= 0 }";
7727 set = isl_set_read_from_str(ctx, str);
7728 pwaff = isl_set_dim_min(set, 2);
7729 set1 = isl_set_from_pw_aff(pwaff);
7730 str = "[n] -> { [(0)] : n >= 1 }";
7731 set2 = isl_set_read_from_str(ctx, str);
7732 equal = isl_set_is_equal(set1, set2);
7733 isl_set_free(set1);
7734 isl_set_free(set2);
7736 if (equal < 0)
7737 return -1;
7738 if (!equal)
7739 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7741 /* Check that empty solution lie in the right space. */
7742 str = "[n] -> { [t,a] : 1 = 0 }";
7743 set = isl_set_read_from_str(ctx, str);
7744 pwaff = isl_set_dim_max(set, 0);
7745 set1 = isl_set_from_pw_aff(pwaff);
7746 str = "[n] -> { [t] : 1 = 0 }";
7747 set2 = isl_set_read_from_str(ctx, str);
7748 equal = isl_set_is_equal(set1, set2);
7749 isl_set_free(set1);
7750 isl_set_free(set2);
7752 if (equal < 0)
7753 return -1;
7754 if (!equal)
7755 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7757 return 0;
7760 /* Basic test for isl_pw_multi_aff_product.
7762 * Check that multiple pieces are properly handled.
7764 static int test_product_pma(isl_ctx *ctx)
7766 isl_stat equal;
7767 const char *str;
7768 isl_pw_multi_aff *pma1, *pma2;
7770 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
7771 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
7772 str = "{ C[] -> D[] }";
7773 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
7774 pma1 = isl_pw_multi_aff_product(pma1, pma2);
7775 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
7776 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
7777 equal = pw_multi_aff_check_plain_equal(pma1, str);
7778 isl_pw_multi_aff_free(pma1);
7779 if (equal < 0)
7780 return -1;
7782 return 0;
7785 int test_product(isl_ctx *ctx)
7787 const char *str;
7788 isl_set *set;
7789 isl_union_set *uset1, *uset2;
7790 int ok;
7792 str = "{ A[i] }";
7793 set = isl_set_read_from_str(ctx, str);
7794 set = isl_set_product(set, isl_set_copy(set));
7795 ok = isl_set_is_wrapping(set);
7796 isl_set_free(set);
7797 if (ok < 0)
7798 return -1;
7799 if (!ok)
7800 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7802 str = "{ [] }";
7803 uset1 = isl_union_set_read_from_str(ctx, str);
7804 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
7805 str = "{ [[] -> []] }";
7806 uset2 = isl_union_set_read_from_str(ctx, str);
7807 ok = isl_union_set_is_equal(uset1, uset2);
7808 isl_union_set_free(uset1);
7809 isl_union_set_free(uset2);
7810 if (ok < 0)
7811 return -1;
7812 if (!ok)
7813 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7815 if (test_product_pma(ctx) < 0)
7816 return -1;
7818 return 0;
7821 /* Check that two sets are not considered disjoint just because
7822 * they have a different set of (named) parameters.
7824 static int test_disjoint(isl_ctx *ctx)
7826 const char *str;
7827 isl_set *set, *set2;
7828 int disjoint;
7830 str = "[n] -> { [[]->[]] }";
7831 set = isl_set_read_from_str(ctx, str);
7832 str = "{ [[]->[]] }";
7833 set2 = isl_set_read_from_str(ctx, str);
7834 disjoint = isl_set_is_disjoint(set, set2);
7835 isl_set_free(set);
7836 isl_set_free(set2);
7837 if (disjoint < 0)
7838 return -1;
7839 if (disjoint)
7840 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7842 return 0;
7845 /* Inputs for isl_pw_multi_aff_is_equal tests.
7846 * "f1" and "f2" are the two function that need to be compared.
7847 * "equal" is the expected result.
7849 struct {
7850 int equal;
7851 const char *f1;
7852 const char *f2;
7853 } pma_equal_tests[] = {
7854 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
7855 "[N] -> { [0] : 0 <= N <= 1 }" },
7856 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
7857 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
7858 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
7859 "[N] -> { [0] : 0 <= N <= 1 }" },
7860 { 0, "{ [NaN] }", "{ [NaN] }" },
7863 int test_equal(isl_ctx *ctx)
7865 int i;
7866 const char *str;
7867 isl_set *set, *set2;
7868 int equal;
7870 str = "{ S_6[i] }";
7871 set = isl_set_read_from_str(ctx, str);
7872 str = "{ S_7[i] }";
7873 set2 = isl_set_read_from_str(ctx, str);
7874 equal = isl_set_is_equal(set, set2);
7875 isl_set_free(set);
7876 isl_set_free(set2);
7877 if (equal < 0)
7878 return -1;
7879 if (equal)
7880 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7882 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
7883 int expected = pma_equal_tests[i].equal;
7884 isl_pw_multi_aff *f1, *f2;
7886 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
7887 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
7888 equal = isl_pw_multi_aff_is_equal(f1, f2);
7889 isl_pw_multi_aff_free(f1);
7890 isl_pw_multi_aff_free(f2);
7891 if (equal < 0)
7892 return -1;
7893 if (equal != expected)
7894 isl_die(ctx, isl_error_unknown,
7895 "unexpected equality result", return -1);
7898 return 0;
7901 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
7902 enum isl_dim_type type, unsigned pos, int fixed)
7904 isl_bool test;
7906 test = isl_map_plain_is_fixed(map, type, pos, NULL);
7907 isl_map_free(map);
7908 if (test < 0)
7909 return -1;
7910 if (test == fixed)
7911 return 0;
7912 if (fixed)
7913 isl_die(ctx, isl_error_unknown,
7914 "map not detected as fixed", return -1);
7915 else
7916 isl_die(ctx, isl_error_unknown,
7917 "map detected as fixed", return -1);
7920 int test_fixed(isl_ctx *ctx)
7922 const char *str;
7923 isl_map *map;
7925 str = "{ [i] -> [i] }";
7926 map = isl_map_read_from_str(ctx, str);
7927 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
7928 return -1;
7929 str = "{ [i] -> [1] }";
7930 map = isl_map_read_from_str(ctx, str);
7931 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7932 return -1;
7933 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
7934 map = isl_map_read_from_str(ctx, str);
7935 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7936 return -1;
7937 map = isl_map_read_from_str(ctx, str);
7938 map = isl_map_neg(map);
7939 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7940 return -1;
7942 return 0;
7945 struct isl_vertices_test_data {
7946 const char *set;
7947 int n;
7948 const char *vertex[6];
7949 } vertices_tests[] = {
7950 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
7951 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
7952 { "{ A[t, i] : t = 14 and i = 1 }",
7953 1, { "{ A[14, 1] }" } },
7954 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
7955 "c <= m and m <= n and m > 0 }",
7956 6, {
7957 "[n, m] -> { [n, m, m] : 0 < m <= n }",
7958 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
7959 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
7960 "[n, m] -> { [m, m, m] : 0 < m <= n }",
7961 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
7962 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
7963 } },
7964 /* An input with implicit equality constraints among the parameters. */
7965 { "[N, M] -> { [a, b] : M >= 3 and 9 + 3M <= a <= 29 + 2N + 11M and "
7966 "2b >= M + a and 5 - 2N - M + a <= 2b <= 3 + a and "
7967 "3b >= 15 + a }",
7968 2, {
7969 "[N, M] -> { [(21), (12)] : M = 3 and N >= 0 }",
7970 "[N, M] -> { [(61 + 2N), (32 + N)] : M = 3 and N >= 0 }",
7975 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
7977 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
7979 struct isl_vertices_test_data *data = user;
7980 isl_ctx *ctx;
7981 isl_multi_aff *ma;
7982 isl_basic_set *bset;
7983 isl_pw_multi_aff *pma;
7984 int i;
7985 isl_bool equal;
7987 ctx = isl_vertex_get_ctx(vertex);
7988 bset = isl_vertex_get_domain(vertex);
7989 ma = isl_vertex_get_expr(vertex);
7990 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
7992 for (i = 0; i < data->n; ++i) {
7993 isl_pw_multi_aff *pma_i;
7995 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
7996 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
7997 isl_pw_multi_aff_free(pma_i);
7999 if (equal < 0 || equal)
8000 break;
8003 isl_pw_multi_aff_free(pma);
8004 isl_vertex_free(vertex);
8006 if (equal < 0)
8007 return isl_stat_error;
8009 return equal ? isl_stat_ok : isl_stat_error;
8012 int test_vertices(isl_ctx *ctx)
8014 int i;
8016 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
8017 isl_basic_set *bset;
8018 isl_vertices *vertices;
8019 int ok = 1;
8020 isl_size n;
8022 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
8023 vertices = isl_basic_set_compute_vertices(bset);
8024 n = isl_vertices_get_n_vertices(vertices);
8025 if (vertices_tests[i].n != n)
8026 ok = 0;
8027 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
8028 &vertices_tests[i]) < 0)
8029 ok = 0;
8030 isl_vertices_free(vertices);
8031 isl_basic_set_free(bset);
8033 if (n < 0)
8034 return -1;
8035 if (!ok)
8036 isl_die(ctx, isl_error_unknown, "unexpected vertices",
8037 return -1);
8040 return 0;
8043 /* Inputs for basic tests of unary operations on isl_union_map.
8044 * "fn" is the function that is being tested.
8045 * "arg" is a string description of the input.
8046 * "res" is a string description of the expected result.
8048 static struct {
8049 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap);
8050 const char *arg;
8051 const char *res;
8052 } umap_un_tests[] = {
8053 { &isl_union_map_range_reverse,
8054 "{ A[] -> [B[] -> C[]]; A[] -> B[]; A[0] -> N[B[1] -> B[2]] }",
8055 "{ A[] -> [C[] -> B[]]; A[0] -> N[B[2] -> B[1]] }" },
8056 { &isl_union_map_range_reverse,
8057 "{ A[] -> N[B[] -> C[]] }",
8058 "{ A[] -> [C[] -> B[]] }" },
8059 { &isl_union_map_range_reverse,
8060 "{ A[] -> N[B[x] -> B[y]] }",
8061 "{ A[] -> N[B[*] -> B[*]] }" },
8064 /* Perform basic tests of unary operations on isl_union_map.
8066 static isl_stat test_un_union_map(isl_ctx *ctx)
8068 int i;
8070 for (i = 0; i < ARRAY_SIZE(umap_un_tests); ++i) {
8071 const char *str;
8072 isl_union_map *umap, *res;
8073 isl_bool equal;
8075 str = umap_un_tests[i].arg;
8076 umap = isl_union_map_read_from_str(ctx, str);
8077 str = umap_un_tests[i].res;
8078 res = isl_union_map_read_from_str(ctx, str);
8079 umap = umap_un_tests[i].fn(umap);
8080 equal = isl_union_map_is_equal(umap, res);
8081 isl_union_map_free(umap);
8082 isl_union_map_free(res);
8083 if (equal < 0)
8084 return isl_stat_error;
8085 if (!equal)
8086 isl_die(ctx, isl_error_unknown,
8087 "unexpected result", return isl_stat_error);
8090 return isl_stat_ok;
8093 /* Inputs for basic tests of binary operations on isl_union_map.
8094 * "fn" is the function that is being tested.
8095 * "arg1" and "arg2" are string descriptions of the inputs.
8096 * "res" is a string description of the expected result.
8098 static struct {
8099 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap1,
8100 __isl_take isl_union_map *umap2);
8101 const char *arg1;
8102 const char *arg2;
8103 const char *res;
8104 } umap_bin_tests[] = {
8105 { &isl_union_map_intersect,
8106 "[n] -> { A[i] -> [] : 0 <= i <= n; B[] -> [] }",
8107 "[m] -> { A[i] -> [] : 0 <= i <= m; C[] -> [] }",
8108 "[m, n] -> { A[i] -> [] : 0 <= i <= n and i <= m }" },
8109 { &isl_union_map_intersect_domain_factor_domain,
8110 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8111 "[N] -> { B[i] -> C[N] }",
8112 "{ }" },
8113 { &isl_union_map_intersect_domain_factor_domain,
8114 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8115 "[N] -> { A[i] -> C[N] }",
8116 "[N] -> { [A[N - 2] -> B[N - 1]] -> C[N] }" },
8117 { &isl_union_map_intersect_domain_factor_domain,
8118 "{ T[A[i] -> B[i + 1]] -> C[i + 2] }",
8119 "[N] -> { A[i] -> C[N] }",
8120 "[N] -> { T[A[N - 2] -> B[N - 1]] -> C[N] }" },
8121 { &isl_union_map_intersect_domain_factor_range,
8122 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8123 "[N] -> { B[i] -> C[N] }",
8124 "[N] -> { [A[N - 2] -> B[N - 1]] -> C[N] }" },
8125 { &isl_union_map_intersect_domain_factor_range,
8126 "{ T[A[i] -> B[i + 1]] -> C[i + 2] }",
8127 "[N] -> { B[i] -> C[N] }",
8128 "[N] -> { T[A[N - 2] -> B[N - 1]] -> C[N] }" },
8129 { &isl_union_map_intersect_domain_factor_range,
8130 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8131 "[N] -> { A[i] -> C[N] }",
8132 "{ }" },
8133 { &isl_union_map_intersect_range_factor_domain,
8134 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8135 "[N] -> { A[i] -> B[N] }",
8136 "[N] -> { A[N - 1] -> [B[N] -> C[N + 1]] }" },
8137 { &isl_union_map_intersect_range_factor_domain,
8138 "{ A[i] -> T[B[i + 1] -> C[i + 2]] }",
8139 "[N] -> { A[i] -> B[N] }",
8140 "[N] -> { A[N - 1] -> T[B[N] -> C[N + 1]] }" },
8141 { &isl_union_map_intersect_range_factor_domain,
8142 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8143 "[N] -> { A[i] -> C[N] }",
8144 "{ }" },
8145 { &isl_union_map_intersect_range_factor_range,
8146 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8147 "[N] -> { A[i] -> C[N] }",
8148 "[N] -> { A[N - 2] -> [B[N - 1] -> C[N]] }" },
8149 { &isl_union_map_intersect_range_factor_range,
8150 "{ A[i] -> T[B[i + 1] -> C[i + 2]] }",
8151 "[N] -> { A[i] -> C[N] }",
8152 "[N] -> { A[N - 2] -> T[B[N - 1] -> C[N]] }" },
8153 { &isl_union_map_intersect_range_factor_range,
8154 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8155 "[N] -> { A[i] -> B[N] }",
8156 "{ }" },
8159 /* Perform basic tests of binary operations on isl_union_map.
8161 static isl_stat test_bin_union_map(isl_ctx *ctx)
8163 int i;
8165 for (i = 0; i < ARRAY_SIZE(umap_bin_tests); ++i) {
8166 const char *str;
8167 isl_union_map *umap1, *umap2, *res;
8168 isl_bool equal;
8170 str = umap_bin_tests[i].arg1;
8171 umap1 = isl_union_map_read_from_str(ctx, str);
8172 str = umap_bin_tests[i].arg2;
8173 umap2 = isl_union_map_read_from_str(ctx, str);
8174 str = umap_bin_tests[i].res;
8175 res = isl_union_map_read_from_str(ctx, str);
8176 umap1 = umap_bin_tests[i].fn(umap1, umap2);
8177 equal = isl_union_map_is_equal(umap1, res);
8178 isl_union_map_free(umap1);
8179 isl_union_map_free(res);
8180 if (equal < 0)
8181 return isl_stat_error;
8182 if (!equal)
8183 isl_die(ctx, isl_error_unknown,
8184 "unexpected result", return isl_stat_error);
8187 return isl_stat_ok;
8190 /* Check that isl_union_set_contains finds space independently
8191 * of the parameters.
8193 static isl_stat test_union_set_contains(isl_ctx *ctx)
8195 const char *str;
8196 isl_bool ok;
8197 isl_space *space;
8198 isl_id *id;
8199 isl_union_set *uset;
8201 str = "[N] -> { A[0:N]; B[*,*] }";
8202 uset = isl_union_set_read_from_str(ctx, str);
8203 space = isl_space_unit(ctx);
8204 id = isl_id_alloc(ctx, "A", NULL);
8205 space = isl_space_add_named_tuple_id_ui(space, id, 1);
8206 ok = isl_union_set_contains(uset, space);
8207 isl_space_free(space);
8208 isl_union_set_free(uset);
8210 if (ok < 0)
8211 return isl_stat_error;
8212 if (!ok)
8213 isl_die(ctx, isl_error_unknown,
8214 "unexpected result", return isl_stat_error);
8216 return isl_stat_ok;
8219 /* Perform basic tests of operations on isl_union_map or isl_union_set.
8221 static int test_union_map(isl_ctx *ctx)
8223 if (test_un_union_map(ctx) < 0)
8224 return -1;
8225 if (test_bin_union_map(ctx) < 0)
8226 return -1;
8227 if (test_union_set_contains(ctx) < 0)
8228 return -1;
8229 return 0;
8232 #undef BASE
8233 #define BASE union_pw_qpolynomial
8234 #include "isl_test_plain_equal_templ.c"
8236 /* Check that the result of applying "fn" to "a" and "b"
8237 * in (obviously) equal to "res".
8239 static isl_stat test_union_pw_op(isl_ctx *ctx, const char *a, const char *b,
8240 __isl_give isl_union_pw_qpolynomial *(*fn)(
8241 __isl_take isl_union_pw_qpolynomial *upwqp1,
8242 __isl_take isl_union_pw_qpolynomial *upwqp2),
8243 const char *res)
8245 isl_stat r;
8246 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
8248 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, a);
8249 upwqp2 = isl_union_pw_qpolynomial_read_from_str(ctx, b);
8250 upwqp1 = fn(upwqp1, upwqp2);
8251 r = union_pw_qpolynomial_check_plain_equal(upwqp1, res);
8252 isl_union_pw_qpolynomial_free(upwqp1);
8254 return r;
8257 int test_union_pw(isl_ctx *ctx)
8259 int equal;
8260 const char *str;
8261 isl_union_set *uset;
8262 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
8263 const char *a, *b;
8265 str = "{ [x] -> x^2 }";
8266 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
8267 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
8268 uset = isl_union_pw_qpolynomial_domain(upwqp1);
8269 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
8270 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
8271 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
8272 isl_union_pw_qpolynomial_free(upwqp1);
8273 isl_union_pw_qpolynomial_free(upwqp2);
8274 if (equal < 0)
8275 return -1;
8276 if (!equal)
8277 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
8279 a = "{ A[x] -> x^2 : x >= 0; B[x] -> x }";
8280 b = "{ A[x] -> x }";
8281 str = "{ A[x] -> x^2 + x : x >= 0; A[x] -> x : x < 0; B[x] -> x }";
8282 if (test_union_pw_op(ctx, a, b, &isl_union_pw_qpolynomial_add, str) < 0)
8283 return -1;
8284 str = "{ A[x] -> x^2 - x : x >= 0; A[x] -> -x : x < 0; B[x] -> x }";
8285 if (test_union_pw_op(ctx, a, b, &isl_union_pw_qpolynomial_sub, str) < 0)
8286 return -1;
8288 str = "{ A[x] -> 0 }";
8289 a = "{ A[x] -> 1 }";
8290 b = "{ A[x] -> -1 }";
8291 if (test_union_pw_op(ctx, a, b, &isl_union_pw_qpolynomial_add, str) < 0)
8292 return -1;
8293 a = "{ A[x] -> 1 }";
8294 b = "{ A[x] -> 1 }";
8295 if (test_union_pw_op(ctx, a, b, &isl_union_pw_qpolynomial_sub, str) < 0)
8296 return -1;
8298 return 0;
8301 /* Inputs for basic tests of functions that select
8302 * subparts of the domain of an isl_multi_union_pw_aff.
8303 * "fn" is the function that is tested.
8304 * "arg" is a string description of the input.
8305 * "res" is a string description of the expected result.
8307 struct {
8308 __isl_give isl_union_set *(*fn)(
8309 __isl_take isl_multi_union_pw_aff *mupa);
8310 const char *arg;
8311 const char *res;
8312 } un_locus_tests[] = {
8313 { &isl_multi_union_pw_aff_zero_union_set,
8314 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
8315 "{ A[0,j]; B[0,j] }" },
8316 { &isl_multi_union_pw_aff_zero_union_set,
8317 "F[{ A[i,j] -> [i-j]; B[i,j] -> [i-j] : i >= 0 }]",
8318 "{ A[i,i]; B[i,i] : i >= 0 }" },
8319 { &isl_multi_union_pw_aff_zero_union_set,
8320 "(F[] : { A[i,j]; B[i,i] : i >= 0 })",
8321 "{ A[i,j]; B[i,i] : i >= 0 }" },
8324 /* Perform some basic tests of functions that select
8325 * subparts of the domain of an isl_multi_union_pw_aff.
8327 static int test_un_locus(isl_ctx *ctx)
8329 int i;
8330 isl_bool ok;
8331 isl_union_set *uset, *res;
8332 isl_multi_union_pw_aff *mupa;
8334 for (i = 0; i < ARRAY_SIZE(un_locus_tests); ++i) {
8335 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
8336 un_locus_tests[i].arg);
8337 res = isl_union_set_read_from_str(ctx, un_locus_tests[i].res);
8338 uset = un_locus_tests[i].fn(mupa);
8339 ok = isl_union_set_is_equal(uset, res);
8340 isl_union_set_free(uset);
8341 isl_union_set_free(res);
8342 if (ok < 0)
8343 return -1;
8344 if (!ok)
8345 isl_die(ctx, isl_error_unknown,
8346 "unexpected result", return -1);
8349 return 0;
8352 /* Inputs for basic tests of functions that select
8353 * subparts of an isl_union_map based on a relation
8354 * specified by an isl_multi_union_pw_aff.
8355 * "fn" is the function that is tested.
8356 * "arg1" and "arg2" are string descriptions of the inputs.
8357 * "res" is a string description of the expected result.
8359 struct {
8360 __isl_give isl_union_map *(*fn)(
8361 __isl_take isl_union_map *umap,
8362 __isl_take isl_multi_union_pw_aff *mupa);
8363 const char *arg1;
8364 const char *arg2;
8365 const char *res;
8366 } bin_locus_tests[] = {
8367 { &isl_union_map_eq_at_multi_union_pw_aff,
8368 "{ A[i,j] -> B[i',j'] }",
8369 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
8370 "{ A[i,j] -> B[i,j'] }" },
8371 { &isl_union_map_eq_at_multi_union_pw_aff,
8372 "{ A[i,j] -> B[i',j'] }",
8373 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8374 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8375 "{ A[i,j] -> B[i,j] }" },
8376 { &isl_union_map_eq_at_multi_union_pw_aff,
8377 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
8378 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
8379 "{ A[i,j] -> B[i,j'] }" },
8380 { &isl_union_map_eq_at_multi_union_pw_aff,
8381 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
8382 "F[{ A[i,j] -> [i]; B[i,j] -> [i]; C[i,j] -> [0] }]",
8383 "{ A[i,j] -> B[i,j']; A[0,j] -> C[i',j'] }" },
8384 { &isl_union_map_eq_at_multi_union_pw_aff,
8385 "{ A[i,j] -> B[i',j'] }",
8386 "F[{ A[i,j] -> [i] : i > j; B[i,j] -> [i] }]",
8387 "{ A[i,j] -> B[i,j'] : i > j }" },
8388 { &isl_union_map_lex_le_at_multi_union_pw_aff,
8389 "{ A[i,j] -> B[i',j'] }",
8390 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8391 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8392 "{ A[i,j] -> B[i',j'] : i,j <<= i',j' }" },
8393 { &isl_union_map_lex_lt_at_multi_union_pw_aff,
8394 "{ A[i,j] -> B[i',j'] }",
8395 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8396 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8397 "{ A[i,j] -> B[i',j'] : i,j << i',j' }" },
8398 { &isl_union_map_lex_ge_at_multi_union_pw_aff,
8399 "{ A[i,j] -> B[i',j'] }",
8400 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8401 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8402 "{ A[i,j] -> B[i',j'] : i,j >>= i',j' }" },
8403 { &isl_union_map_lex_gt_at_multi_union_pw_aff,
8404 "{ A[i,j] -> B[i',j'] }",
8405 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8406 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8407 "{ A[i,j] -> B[i',j'] : i,j >> i',j' }" },
8408 { &isl_union_map_eq_at_multi_union_pw_aff,
8409 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
8410 "(F[] : { A[i,j]; B[i,j] })",
8411 "{ A[i,j] -> B[i',j'] }" },
8412 { &isl_union_map_eq_at_multi_union_pw_aff,
8413 "{ A[i,j] -> B[i',j'] }",
8414 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
8415 "{ A[i,j] -> B[i',j'] : i > j and i' < j' }" },
8416 { &isl_union_map_eq_at_multi_union_pw_aff,
8417 "[N] -> { A[i,j] -> B[i',j'] : i,i' <= N }",
8418 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
8419 "[N] -> { A[i,j] -> B[i',j'] : i > j and i' < j' and i,i' <= N }" },
8420 { &isl_union_map_eq_at_multi_union_pw_aff,
8421 "{ A[i,j] -> B[i',j'] }",
8422 "[N] -> (F[] : { A[i,j] : i < N; B[i,j] : i < N })",
8423 "[N] -> { A[i,j] -> B[i',j'] : i,i' < N }" },
8424 { &isl_union_map_eq_at_multi_union_pw_aff,
8425 "{ A[i,j] -> B[i',j'] }",
8426 "[N] -> (F[] : { : N >= 0 })",
8427 "[N] -> { A[i,j] -> B[i',j'] : N >= 0 }" },
8430 /* Perform some basic tests of functions that select
8431 * subparts of an isl_union_map based on a relation
8432 * specified by an isl_multi_union_pw_aff.
8434 static int test_bin_locus(isl_ctx *ctx)
8436 int i;
8437 isl_bool ok;
8438 isl_union_map *umap, *res;
8439 isl_multi_union_pw_aff *mupa;
8441 for (i = 0; i < ARRAY_SIZE(bin_locus_tests); ++i) {
8442 umap = isl_union_map_read_from_str(ctx,
8443 bin_locus_tests[i].arg1);
8444 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
8445 bin_locus_tests[i].arg2);
8446 res = isl_union_map_read_from_str(ctx, bin_locus_tests[i].res);
8447 umap = bin_locus_tests[i].fn(umap, mupa);
8448 ok = isl_union_map_is_equal(umap, res);
8449 isl_union_map_free(umap);
8450 isl_union_map_free(res);
8451 if (ok < 0)
8452 return -1;
8453 if (!ok)
8454 isl_die(ctx, isl_error_unknown,
8455 "unexpected result", return -1);
8458 return 0;
8461 /* Inputs for basic tests of functions that determine
8462 * the part of the domain where two isl_multi_aff objects
8463 * related to each other in a specific way.
8464 * "fn" is the function that is being tested.
8465 * "arg1" and "arg2" are string descriptions of the inputs.
8466 * "res" is a string description of the expected result.
8468 static struct {
8469 __isl_give isl_set *(*fn)(__isl_take isl_multi_aff *ma1,
8470 __isl_take isl_multi_aff *ma2);
8471 const char *arg1;
8472 const char *arg2;
8473 const char *res;
8474 } bin_locus_ma_tests[] = {
8475 { &isl_multi_aff_lex_le_set, "{ [] }", "{ [] }", "{ : }" },
8476 { &isl_multi_aff_lex_lt_set, "{ [] }", "{ [] }", "{ : false }" },
8477 { &isl_multi_aff_lex_le_set,
8478 "{ A[i] -> [i] }", "{ A[i] -> [0] }",
8479 "{ A[i] : i <= 0 }" },
8480 { &isl_multi_aff_lex_lt_set,
8481 "{ A[i] -> [i] }", "{ A[i] -> [0] }",
8482 "{ A[i] : i < 0 }" },
8483 { &isl_multi_aff_lex_le_set,
8484 "{ A[i] -> [i, i] }", "{ A[i] -> [0, 0] }",
8485 "{ A[i] : i <= 0 }" },
8486 { &isl_multi_aff_lex_le_set,
8487 "{ A[i] -> [i, 0] }", "{ A[i] -> [0, 0] }",
8488 "{ A[i] : i <= 0 }" },
8489 { &isl_multi_aff_lex_le_set,
8490 "{ A[i] -> [i, 1] }", "{ A[i] -> [0, 0] }",
8491 "{ A[i] : i < 0 }" },
8494 /* Perform some basic tests of functions that determine
8495 * the part of the domain where two isl_multi_aff objects
8496 * related to each other in a specific way.
8498 static isl_stat test_bin_locus_ma(isl_ctx *ctx)
8500 int i;
8502 for (i = 0; i < ARRAY_SIZE(bin_locus_ma_tests); ++i) {
8503 const char *str;
8504 isl_bool ok;
8505 isl_multi_aff *ma1, *ma2;
8506 isl_set *set, *res;
8508 str = bin_locus_ma_tests[i].arg1;
8509 ma1 = isl_multi_aff_read_from_str(ctx, str);
8510 str = bin_locus_ma_tests[i].arg2;
8511 ma2 = isl_multi_aff_read_from_str(ctx, str);
8512 res = isl_set_read_from_str(ctx, bin_locus_ma_tests[i].res);
8513 set = bin_locus_ma_tests[i].fn(ma1, ma2);
8514 ok = isl_set_is_equal(set, res);
8515 isl_set_free(set);
8516 isl_set_free(res);
8517 if (ok < 0)
8518 return isl_stat_error;
8519 if (!ok)
8520 isl_die(ctx, isl_error_unknown,
8521 "unexpected result", return isl_stat_error);
8524 return isl_stat_ok;
8527 /* Perform basic locus tests.
8529 static int test_locus(isl_ctx *ctx)
8531 if (test_un_locus(ctx) < 0)
8532 return -1;
8533 if (test_bin_locus(ctx) < 0)
8534 return -1;
8535 if (test_bin_locus_ma(ctx) < 0)
8536 return -1;
8537 return 0;
8540 /* Test that isl_union_pw_qpolynomial_eval picks up the function
8541 * defined over the correct domain space.
8543 static int test_eval_1(isl_ctx *ctx)
8545 const char *str;
8546 isl_point *pnt;
8547 isl_set *set;
8548 isl_union_pw_qpolynomial *upwqp;
8549 isl_val *v;
8550 int cmp;
8552 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
8553 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
8554 str = "{ A[6] }";
8555 set = isl_set_read_from_str(ctx, str);
8556 pnt = isl_set_sample_point(set);
8557 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
8558 cmp = isl_val_cmp_si(v, 36);
8559 isl_val_free(v);
8561 if (!v)
8562 return -1;
8563 if (cmp != 0)
8564 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
8566 return 0;
8569 /* Check that isl_qpolynomial_eval handles getting called on a void point.
8571 static int test_eval_2(isl_ctx *ctx)
8573 const char *str;
8574 isl_point *pnt;
8575 isl_set *set;
8576 isl_qpolynomial *qp;
8577 isl_val *v;
8578 isl_bool ok;
8580 str = "{ A[x] -> [x] }";
8581 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
8582 str = "{ A[x] : false }";
8583 set = isl_set_read_from_str(ctx, str);
8584 pnt = isl_set_sample_point(set);
8585 v = isl_qpolynomial_eval(qp, pnt);
8586 ok = isl_val_is_nan(v);
8587 isl_val_free(v);
8589 if (ok < 0)
8590 return -1;
8591 if (!ok)
8592 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
8594 return 0;
8597 /* Check that a polynomial (without local variables) can be evaluated
8598 * in a rational point.
8600 static isl_stat test_eval_3(isl_ctx *ctx)
8602 isl_pw_qpolynomial *pwqp;
8603 isl_point *pnt;
8604 isl_val *v;
8605 isl_stat r;
8607 pwqp = isl_pw_qpolynomial_read_from_str(ctx, "{ [x] -> x^2 }");
8608 pnt = isl_point_zero(isl_pw_qpolynomial_get_domain_space(pwqp));
8609 v = isl_val_read_from_str(ctx, "1/2");
8610 pnt = isl_point_set_coordinate_val(pnt, isl_dim_set, 0, v);
8611 v = isl_pw_qpolynomial_eval(pwqp, pnt);
8612 r = val_check_equal(v, "1/4");
8613 isl_val_free(v);
8615 return r;
8618 /* Inputs for isl_pw_aff_eval test.
8619 * "f" is the affine function.
8620 * "p" is the point where the function should be evaluated.
8621 * "res" is the expected result.
8623 struct {
8624 const char *f;
8625 const char *p;
8626 const char *res;
8627 } aff_eval_tests[] = {
8628 { "{ [i] -> [2 * i] }", "{ [4] }", "8" },
8629 { "{ [i] -> [2 * i] }", "{ [x] : false }", "NaN" },
8630 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [0] }", "0" },
8631 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [1] }", "1" },
8632 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [2] }", "3" },
8633 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [3] }", "5" },
8634 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [4] }", "7" },
8635 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [0] }", "0" },
8636 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [1] }", "0" },
8637 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [2] }", "0" },
8638 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [3] }", "0" },
8639 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [4] }", "1" },
8640 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [6] }", "1" },
8641 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [8] }", "2" },
8642 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [4] }", "4" },
8643 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [-2] }", "2" },
8644 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [0] }", "NaN" },
8645 { "[N] -> { [2 * N] }", "[N] -> { : N = 4 }", "8" },
8646 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 1] }", "1" },
8647 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 2] }", "3/2" },
8648 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [4] }", "4" },
8649 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [3] }", "NaN" },
8650 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [x] : false }", "NaN" },
8651 { "[m, n] -> { [2m + 3n] }", "[n=1, m=10] -> { : }", "23" },
8654 /* Perform basic isl_pw_aff_eval tests.
8656 static int test_eval_aff(isl_ctx *ctx)
8658 int i;
8660 for (i = 0; i < ARRAY_SIZE(aff_eval_tests); ++i) {
8661 isl_stat r;
8662 isl_pw_aff *pa;
8663 isl_set *set;
8664 isl_point *pnt;
8665 isl_val *v;
8667 pa = isl_pw_aff_read_from_str(ctx, aff_eval_tests[i].f);
8668 set = isl_set_read_from_str(ctx, aff_eval_tests[i].p);
8669 pnt = isl_set_sample_point(set);
8670 v = isl_pw_aff_eval(pa, pnt);
8671 r = val_check_equal(v, aff_eval_tests[i].res);
8672 isl_val_free(v);
8673 if (r < 0)
8674 return -1;
8676 return 0;
8679 /* Perform basic evaluation tests.
8681 static int test_eval(isl_ctx *ctx)
8683 if (test_eval_1(ctx) < 0)
8684 return -1;
8685 if (test_eval_2(ctx) < 0)
8686 return -1;
8687 if (test_eval_3(ctx) < 0)
8688 return -1;
8689 if (test_eval_aff(ctx) < 0)
8690 return -1;
8691 return 0;
8694 /* Descriptions of sets that are tested for reparsing after printing.
8696 const char *output_tests[] = {
8697 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
8698 "{ [x] : 1 = 0 }",
8699 "{ [x] : false }",
8700 "{ [x] : x mod 2 = 0 }",
8701 "{ [x] : x mod 2 = 1 }",
8702 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
8703 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
8704 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
8705 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
8706 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
8707 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
8710 /* Check that printing a set and reparsing a set from the printed output
8711 * results in the same set.
8713 static int test_output_set(isl_ctx *ctx)
8715 int i;
8716 char *str;
8717 isl_set *set1, *set2;
8718 isl_bool equal;
8720 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
8721 set1 = isl_set_read_from_str(ctx, output_tests[i]);
8722 str = isl_set_to_str(set1);
8723 set2 = isl_set_read_from_str(ctx, str);
8724 free(str);
8725 equal = isl_set_is_equal(set1, set2);
8726 isl_set_free(set1);
8727 isl_set_free(set2);
8728 if (equal < 0)
8729 return -1;
8730 if (!equal)
8731 isl_die(ctx, isl_error_unknown,
8732 "parsed output not the same", return -1);
8735 return 0;
8738 /* Check that an isl_multi_aff is printed using a consistent space.
8740 static isl_stat test_output_ma(isl_ctx *ctx)
8742 char *str;
8743 isl_bool equal;
8744 isl_aff *aff;
8745 isl_multi_aff *ma, *ma2;
8747 ma = isl_multi_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
8748 aff = isl_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
8749 ma = isl_multi_aff_set_aff(ma, 0, aff);
8750 str = isl_multi_aff_to_str(ma);
8751 ma2 = isl_multi_aff_read_from_str(ctx, str);
8752 free(str);
8753 equal = isl_multi_aff_plain_is_equal(ma, ma2);
8754 isl_multi_aff_free(ma2);
8755 isl_multi_aff_free(ma);
8757 if (equal < 0)
8758 return isl_stat_error;
8759 if (!equal)
8760 isl_die(ctx, isl_error_unknown, "bad conversion",
8761 return isl_stat_error);
8763 return isl_stat_ok;
8766 /* Check that an isl_multi_pw_aff is printed using a consistent space.
8768 static isl_stat test_output_mpa(isl_ctx *ctx)
8770 char *str;
8771 isl_bool equal;
8772 isl_pw_aff *pa;
8773 isl_multi_pw_aff *mpa, *mpa2;
8775 mpa = isl_multi_pw_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
8776 pa = isl_pw_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
8777 mpa = isl_multi_pw_aff_set_pw_aff(mpa, 0, pa);
8778 str = isl_multi_pw_aff_to_str(mpa);
8779 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
8780 free(str);
8781 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
8782 isl_multi_pw_aff_free(mpa2);
8783 isl_multi_pw_aff_free(mpa);
8785 if (equal < 0)
8786 return isl_stat_error;
8787 if (!equal)
8788 isl_die(ctx, isl_error_unknown, "bad conversion",
8789 return isl_stat_error);
8791 return isl_stat_ok;
8794 int test_output(isl_ctx *ctx)
8796 char *s;
8797 const char *str;
8798 isl_pw_aff *pa;
8799 isl_printer *p;
8800 int equal;
8802 if (test_output_set(ctx) < 0)
8803 return -1;
8804 if (test_output_ma(ctx) < 0)
8805 return -1;
8806 if (test_output_mpa(ctx) < 0)
8807 return -1;
8809 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
8810 pa = isl_pw_aff_read_from_str(ctx, str);
8812 p = isl_printer_to_str(ctx);
8813 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
8814 p = isl_printer_print_pw_aff(p, pa);
8815 s = isl_printer_get_str(p);
8816 isl_printer_free(p);
8817 isl_pw_aff_free(pa);
8818 if (!s)
8819 equal = -1;
8820 else
8821 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
8822 free(s);
8823 if (equal < 0)
8824 return -1;
8825 if (!equal)
8826 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
8828 return 0;
8831 int test_sample(isl_ctx *ctx)
8833 const char *str;
8834 isl_basic_set *bset1, *bset2;
8835 int empty, subset;
8837 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
8838 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
8839 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
8840 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
8841 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
8842 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
8843 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
8844 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
8845 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
8846 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
8847 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
8848 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
8849 "d - 1073741821e and "
8850 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
8851 "3j >= 1 - a + b + 2e and "
8852 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
8853 "3i <= 4 - a + 4b - e and "
8854 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
8855 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
8856 "c <= -1 + a and 3i >= -2 - a + 3e and "
8857 "1073741822e <= 1073741823 - a + 1073741822b + c and "
8858 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
8859 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
8860 "1073741823e >= 1 + 1073741823b - d and "
8861 "3i >= 1073741823b + c - 1073741823e - f and "
8862 "3i >= 1 + 2b + e + 3g }";
8863 bset1 = isl_basic_set_read_from_str(ctx, str);
8864 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
8865 empty = isl_basic_set_is_empty(bset2);
8866 subset = isl_basic_set_is_subset(bset2, bset1);
8867 isl_basic_set_free(bset1);
8868 isl_basic_set_free(bset2);
8869 if (empty < 0 || subset < 0)
8870 return -1;
8871 if (empty)
8872 isl_die(ctx, isl_error_unknown, "point not found", return -1);
8873 if (!subset)
8874 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
8876 return 0;
8879 /* Perform a projection on a basic set that is known to be empty
8880 * but that has not been assigned a canonical representation.
8881 * Earlier versions of isl would run into a stack overflow
8882 * on this example.
8884 static int test_empty_projection(isl_ctx *ctx)
8886 const char *str;
8887 isl_bool empty;
8888 isl_basic_set *bset;
8890 str = "{ [a, b, c, d, e, f, g, h] : 5f = 1 + 4a - b + 5c - d - 2e and "
8891 "3h = 2 + b + c and 14c >= 9 - 3a + 25b and "
8892 "4c <= 50 - 3a + 23b and 6b <= -39 + a and "
8893 "9g >= -6 + 3a + b + c and e < a + b - 2d and "
8894 "7d >= -5 + 2a + 2b and 5g >= -14 + a - 4b + d + 2e and "
8895 "9g <= -28 - 5b - 2c + 3d + 6e }";
8896 bset = isl_basic_set_read_from_str(ctx, str);
8897 empty = isl_basic_set_is_empty(bset);
8898 bset = isl_basic_set_params(bset);
8899 isl_basic_set_free(bset);
8901 if (empty < 0)
8902 return -1;
8904 return 0;
8907 int test_fixed_power(isl_ctx *ctx)
8909 const char *str;
8910 isl_map *map;
8911 isl_val *exp;
8912 int equal;
8914 str = "{ [i] -> [i + 1] }";
8915 map = isl_map_read_from_str(ctx, str);
8916 exp = isl_val_int_from_si(ctx, 23);
8917 map = isl_map_fixed_power_val(map, exp);
8918 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
8919 isl_map_free(map);
8920 if (equal < 0)
8921 return -1;
8923 return 0;
8926 int test_slice(isl_ctx *ctx)
8928 const char *str;
8929 isl_map *map;
8930 int equal;
8932 str = "{ [i] -> [j] }";
8933 map = isl_map_read_from_str(ctx, str);
8934 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
8935 equal = map_check_equal(map, "{ [i] -> [i] }");
8936 isl_map_free(map);
8937 if (equal < 0)
8938 return -1;
8940 str = "{ [i] -> [j] }";
8941 map = isl_map_read_from_str(ctx, str);
8942 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
8943 equal = map_check_equal(map, "{ [i] -> [j] }");
8944 isl_map_free(map);
8945 if (equal < 0)
8946 return -1;
8948 str = "{ [i] -> [j] }";
8949 map = isl_map_read_from_str(ctx, str);
8950 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
8951 equal = map_check_equal(map, "{ [i] -> [-i] }");
8952 isl_map_free(map);
8953 if (equal < 0)
8954 return -1;
8956 str = "{ [i] -> [j] }";
8957 map = isl_map_read_from_str(ctx, str);
8958 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
8959 equal = map_check_equal(map, "{ [0] -> [j] }");
8960 isl_map_free(map);
8961 if (equal < 0)
8962 return -1;
8964 str = "{ [i] -> [j] }";
8965 map = isl_map_read_from_str(ctx, str);
8966 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
8967 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
8968 isl_map_free(map);
8969 if (equal < 0)
8970 return -1;
8972 str = "{ [i] -> [j] }";
8973 map = isl_map_read_from_str(ctx, str);
8974 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
8975 equal = map_check_equal(map, "{ [i] -> [j] : false }");
8976 isl_map_free(map);
8977 if (equal < 0)
8978 return -1;
8980 return 0;
8983 int test_eliminate(isl_ctx *ctx)
8985 const char *str;
8986 isl_map *map;
8987 int equal;
8989 str = "{ [i] -> [j] : i = 2j }";
8990 map = isl_map_read_from_str(ctx, str);
8991 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
8992 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
8993 isl_map_free(map);
8994 if (equal < 0)
8995 return -1;
8997 return 0;
9000 /* Check basic functionality of isl_map_deltas_map.
9002 static int test_deltas_map(isl_ctx *ctx)
9004 const char *str;
9005 isl_map *map;
9006 int equal;
9008 str = "{ A[i] -> A[i + 1] }";
9009 map = isl_map_read_from_str(ctx, str);
9010 map = isl_map_deltas_map(map);
9011 equal = map_check_equal(map, "{ [A[i] -> A[i + 1]] -> A[1] }");
9012 isl_map_free(map);
9013 if (equal < 0)
9014 return -1;
9016 return 0;
9019 /* Check that isl_set_dim_residue_class detects that the values of j
9020 * in the set below are all odd and that it does not detect any spurious
9021 * strides.
9023 static int test_residue_class(isl_ctx *ctx)
9025 const char *str;
9026 isl_set *set;
9027 isl_int m, r;
9028 isl_stat res;
9030 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
9031 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
9032 set = isl_set_read_from_str(ctx, str);
9033 isl_int_init(m);
9034 isl_int_init(r);
9035 res = isl_set_dim_residue_class(set, 1, &m, &r);
9036 if (res >= 0 &&
9037 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
9038 isl_die(ctx, isl_error_unknown, "incorrect residue class",
9039 res = isl_stat_error);
9040 isl_int_clear(r);
9041 isl_int_clear(m);
9042 isl_set_free(set);
9044 return res;
9047 static int test_align_parameters_1(isl_ctx *ctx)
9049 const char *str;
9050 isl_space *space;
9051 isl_multi_aff *ma1, *ma2;
9052 int equal;
9054 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
9055 ma1 = isl_multi_aff_read_from_str(ctx, str);
9057 space = isl_space_params_alloc(ctx, 1);
9058 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
9059 ma1 = isl_multi_aff_align_params(ma1, space);
9061 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
9062 ma2 = isl_multi_aff_read_from_str(ctx, str);
9064 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
9066 isl_multi_aff_free(ma1);
9067 isl_multi_aff_free(ma2);
9069 if (equal < 0)
9070 return -1;
9071 if (!equal)
9072 isl_die(ctx, isl_error_unknown,
9073 "result not as expected", return -1);
9075 return 0;
9078 /* Check the isl_multi_*_from_*_list operation in case inputs
9079 * have unaligned parameters.
9080 * In particular, older versions of isl would simply fail
9081 * (without printing any error message).
9083 static isl_stat test_align_parameters_2(isl_ctx *ctx)
9085 isl_space *space;
9086 isl_map *map;
9087 isl_aff *aff;
9088 isl_multi_aff *ma;
9090 map = isl_map_read_from_str(ctx, "{ A[] -> M[x] }");
9091 space = isl_map_get_space(map);
9092 isl_map_free(map);
9094 aff = isl_aff_read_from_str(ctx, "[N] -> { A[] -> [N] }");
9095 ma = isl_multi_aff_from_aff_list(space, isl_aff_list_from_aff(aff));
9096 isl_multi_aff_free(ma);
9098 if (!ma)
9099 return isl_stat_error;
9100 return isl_stat_ok;
9103 /* Perform basic parameter alignment tests.
9105 static int test_align_parameters(isl_ctx *ctx)
9107 if (test_align_parameters_1(ctx) < 0)
9108 return -1;
9109 if (test_align_parameters_2(ctx) < 0)
9110 return -1;
9112 return 0;
9115 /* Check that isl_*_drop_unused_params actually drops the unused parameters
9116 * by comparing the result using isl_*_plain_is_equal.
9117 * Note that this assumes that isl_*_plain_is_equal does not consider
9118 * objects that only differ by unused parameters to be equal.
9120 int test_drop_unused_parameters(isl_ctx *ctx)
9122 const char *str_with, *str_without;
9123 isl_basic_set *bset1, *bset2;
9124 isl_set *set1, *set2;
9125 isl_pw_aff *pwa1, *pwa2;
9126 int equal;
9128 str_with = "[n, m, o] -> { [m] }";
9129 str_without = "[m] -> { [m] }";
9131 bset1 = isl_basic_set_read_from_str(ctx, str_with);
9132 bset2 = isl_basic_set_read_from_str(ctx, str_without);
9133 bset1 = isl_basic_set_drop_unused_params(bset1);
9134 equal = isl_basic_set_plain_is_equal(bset1, bset2);
9135 isl_basic_set_free(bset1);
9136 isl_basic_set_free(bset2);
9138 if (equal < 0)
9139 return -1;
9140 if (!equal)
9141 isl_die(ctx, isl_error_unknown,
9142 "result not as expected", return -1);
9144 set1 = isl_set_read_from_str(ctx, str_with);
9145 set2 = isl_set_read_from_str(ctx, str_without);
9146 set1 = isl_set_drop_unused_params(set1);
9147 equal = isl_set_plain_is_equal(set1, set2);
9148 isl_set_free(set1);
9149 isl_set_free(set2);
9151 if (equal < 0)
9152 return -1;
9153 if (!equal)
9154 isl_die(ctx, isl_error_unknown,
9155 "result not as expected", return -1);
9157 pwa1 = isl_pw_aff_read_from_str(ctx, str_with);
9158 pwa2 = isl_pw_aff_read_from_str(ctx, str_without);
9159 pwa1 = isl_pw_aff_drop_unused_params(pwa1);
9160 equal = isl_pw_aff_plain_is_equal(pwa1, pwa2);
9161 isl_pw_aff_free(pwa1);
9162 isl_pw_aff_free(pwa2);
9164 if (equal < 0)
9165 return -1;
9166 if (!equal)
9167 isl_die(ctx, isl_error_unknown,
9168 "result not as expected", return -1);
9170 return 0;
9173 static int test_list(isl_ctx *ctx)
9175 isl_id *a, *b, *c, *d, *id;
9176 isl_id_list *list;
9177 isl_size n;
9178 int ok;
9180 a = isl_id_alloc(ctx, "a", NULL);
9181 b = isl_id_alloc(ctx, "b", NULL);
9182 c = isl_id_alloc(ctx, "c", NULL);
9183 d = isl_id_alloc(ctx, "d", NULL);
9185 list = isl_id_list_alloc(ctx, 4);
9186 list = isl_id_list_add(list, b);
9187 list = isl_id_list_insert(list, 0, a);
9188 list = isl_id_list_add(list, c);
9189 list = isl_id_list_add(list, d);
9190 list = isl_id_list_drop(list, 1, 1);
9192 n = isl_id_list_n_id(list);
9193 if (n < 0)
9194 return -1;
9195 if (n != 3) {
9196 isl_id_list_free(list);
9197 isl_die(ctx, isl_error_unknown,
9198 "unexpected number of elements in list", return -1);
9201 id = isl_id_list_get_id(list, 0);
9202 ok = id == a;
9203 isl_id_free(id);
9204 id = isl_id_list_get_id(list, 1);
9205 ok = ok && id == c;
9206 isl_id_free(id);
9207 id = isl_id_list_get_id(list, 2);
9208 ok = ok && id == d;
9209 isl_id_free(id);
9211 isl_id_list_free(list);
9213 if (!ok)
9214 isl_die(ctx, isl_error_unknown,
9215 "unexpected elements in list", return -1);
9217 return 0;
9220 /* Check the conversion from an isl_multi_aff to an isl_basic_set.
9222 static isl_stat test_ma_conversion(isl_ctx *ctx)
9224 const char *str;
9225 isl_bool equal;
9226 isl_multi_aff *ma;
9227 isl_basic_set *bset1, *bset2;
9229 str = "[N] -> { A[0, N + 1] }";
9230 ma = isl_multi_aff_read_from_str(ctx, str);
9231 bset1 = isl_basic_set_read_from_str(ctx, str);
9232 bset2 = isl_basic_set_from_multi_aff(ma);
9233 equal = isl_basic_set_is_equal(bset1, bset2);
9234 isl_basic_set_free(bset1);
9235 isl_basic_set_free(bset2);
9236 if (equal < 0)
9237 return isl_stat_error;
9238 if (!equal)
9239 isl_die(ctx, isl_error_unknown, "bad conversion",
9240 return isl_stat_error);
9241 return isl_stat_ok;
9244 const char *set_conversion_tests[] = {
9245 "[N] -> { [i] : N - 1 <= 2 i <= N }",
9246 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
9247 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
9248 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
9249 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
9250 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
9251 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
9252 "-3 + c <= d <= 28 + c) }",
9255 /* Check that converting from isl_set to isl_pw_multi_aff and back
9256 * to isl_set produces the original isl_set.
9258 static int test_set_conversion(isl_ctx *ctx)
9260 int i;
9261 const char *str;
9262 isl_set *set1, *set2;
9263 isl_pw_multi_aff *pma;
9264 int equal;
9266 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
9267 str = set_conversion_tests[i];
9268 set1 = isl_set_read_from_str(ctx, str);
9269 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
9270 set2 = isl_set_from_pw_multi_aff(pma);
9271 equal = isl_set_is_equal(set1, set2);
9272 isl_set_free(set1);
9273 isl_set_free(set2);
9275 if (equal < 0)
9276 return -1;
9277 if (!equal)
9278 isl_die(ctx, isl_error_unknown, "bad conversion",
9279 return -1);
9282 return 0;
9285 const char *conversion_tests[] = {
9286 "{ [a, b, c, d] -> s0[a, b, e, f] : "
9287 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
9288 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
9289 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
9290 "9e <= -2 - 2a) }",
9291 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
9292 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
9293 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
9296 /* Check that converting from isl_map to isl_pw_multi_aff and back
9297 * to isl_map produces the original isl_map.
9299 static int test_map_conversion(isl_ctx *ctx)
9301 int i;
9302 isl_map *map1, *map2;
9303 isl_pw_multi_aff *pma;
9304 int equal;
9306 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
9307 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
9308 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
9309 map2 = isl_map_from_pw_multi_aff(pma);
9310 equal = isl_map_is_equal(map1, map2);
9311 isl_map_free(map1);
9312 isl_map_free(map2);
9314 if (equal < 0)
9315 return -1;
9316 if (!equal)
9317 isl_die(ctx, isl_error_unknown, "bad conversion",
9318 return -1);
9321 return 0;
9324 /* Descriptions of isl_pw_multi_aff objects for testing conversion
9325 * to isl_multi_pw_aff and back.
9327 const char *mpa_conversion_tests[] = {
9328 "{ [x] -> A[x] }",
9329 "{ [x] -> A[x] : x >= 0 }",
9330 "{ [x] -> A[x] : x >= 0; [x] -> A[-x] : x < 0 }",
9331 "{ [x] -> A[x, x + 1] }",
9332 "{ [x] -> A[] }",
9333 "{ [x] -> A[] : x >= 0 }",
9336 /* Check that conversion from isl_pw_multi_aff to isl_multi_pw_aff and
9337 * back to isl_pw_multi_aff preserves the original meaning.
9339 static int test_mpa_conversion(isl_ctx *ctx)
9341 int i;
9342 isl_pw_multi_aff *pma1, *pma2;
9343 isl_multi_pw_aff *mpa;
9344 int equal;
9346 for (i = 0; i < ARRAY_SIZE(mpa_conversion_tests); ++i) {
9347 const char *str;
9348 str = mpa_conversion_tests[i];
9349 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
9350 pma2 = isl_pw_multi_aff_copy(pma1);
9351 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma1);
9352 pma1 = isl_pw_multi_aff_from_multi_pw_aff(mpa);
9353 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
9354 isl_pw_multi_aff_free(pma1);
9355 isl_pw_multi_aff_free(pma2);
9357 if (equal < 0)
9358 return -1;
9359 if (!equal)
9360 isl_die(ctx, isl_error_unknown, "bad conversion",
9361 return -1);
9364 return 0;
9367 /* Descriptions of union maps that should be convertible
9368 * to an isl_multi_union_pw_aff.
9370 const char *umap_mupa_conversion_tests[] = {
9371 "{ [a, b, c, d] -> s0[a, b, e, f] : "
9372 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
9373 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
9374 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
9375 "9e <= -2 - 2a) }",
9376 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
9377 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
9378 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
9379 "{ A[] -> B[0]; C[] -> B[1] }",
9380 "{ A[] -> B[]; C[] -> B[] }",
9383 /* Check that converting from isl_union_map to isl_multi_union_pw_aff and back
9384 * to isl_union_map produces the original isl_union_map.
9386 static int test_union_map_mupa_conversion(isl_ctx *ctx)
9388 int i;
9389 isl_union_map *umap1, *umap2;
9390 isl_multi_union_pw_aff *mupa;
9391 int equal;
9393 for (i = 0; i < ARRAY_SIZE(umap_mupa_conversion_tests); ++i) {
9394 const char *str;
9395 str = umap_mupa_conversion_tests[i];
9396 umap1 = isl_union_map_read_from_str(ctx, str);
9397 umap2 = isl_union_map_copy(umap1);
9398 mupa = isl_multi_union_pw_aff_from_union_map(umap2);
9399 umap2 = isl_union_map_from_multi_union_pw_aff(mupa);
9400 equal = isl_union_map_is_equal(umap1, umap2);
9401 isl_union_map_free(umap1);
9402 isl_union_map_free(umap2);
9404 if (equal < 0)
9405 return -1;
9406 if (!equal)
9407 isl_die(ctx, isl_error_unknown, "bad conversion",
9408 return -1);
9411 return 0;
9414 static int test_conversion(isl_ctx *ctx)
9416 if (test_ma_conversion(ctx) < 0)
9417 return -1;
9418 if (test_set_conversion(ctx) < 0)
9419 return -1;
9420 if (test_map_conversion(ctx) < 0)
9421 return -1;
9422 if (test_mpa_conversion(ctx) < 0)
9423 return -1;
9424 if (test_union_map_mupa_conversion(ctx) < 0)
9425 return -1;
9426 return 0;
9429 /* Check that isl_basic_map_curry does not modify input.
9431 static int test_curry(isl_ctx *ctx)
9433 const char *str;
9434 isl_basic_map *bmap1, *bmap2;
9435 int equal;
9437 str = "{ [A[] -> B[]] -> C[] }";
9438 bmap1 = isl_basic_map_read_from_str(ctx, str);
9439 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
9440 equal = isl_basic_map_is_equal(bmap1, bmap2);
9441 isl_basic_map_free(bmap1);
9442 isl_basic_map_free(bmap2);
9444 if (equal < 0)
9445 return -1;
9446 if (equal)
9447 isl_die(ctx, isl_error_unknown,
9448 "curried map should not be equal to original",
9449 return -1);
9451 return 0;
9454 struct {
9455 const char *ma1;
9456 const char *ma;
9457 const char *res;
9458 } pullback_tests[] = {
9459 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
9460 "{ A[a,b] -> C[b + 2a] }" },
9461 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
9462 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
9463 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
9464 "{ A[a] -> C[(a)/6] }" },
9465 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
9466 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
9467 "{ A[a] -> C[(2a)/3] }" },
9468 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
9469 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
9470 "{ A[i,j] -> C[i + j, i + j] }"},
9471 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
9472 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
9473 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
9474 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
9475 "{ [i, j] -> [floor((i)/3), j] }",
9476 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
9479 static int test_pullback(isl_ctx *ctx)
9481 int i;
9482 isl_multi_aff *ma1, *ma2;
9483 isl_multi_aff *ma;
9484 int equal;
9486 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
9487 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
9488 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
9489 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
9490 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
9491 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
9492 isl_multi_aff_free(ma1);
9493 isl_multi_aff_free(ma2);
9494 if (equal < 0)
9495 return -1;
9496 if (!equal)
9497 isl_die(ctx, isl_error_unknown, "bad pullback",
9498 return -1);
9501 return 0;
9504 /* Check that negation is printed correctly and that equal expressions
9505 * are correctly identified.
9507 static int test_ast(isl_ctx *ctx)
9509 isl_ast_expr *expr, *expr1, *expr2, *expr3;
9510 char *str;
9511 int ok, equal;
9513 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
9514 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
9515 expr = isl_ast_expr_add(expr1, expr2);
9516 expr2 = isl_ast_expr_copy(expr);
9517 expr = isl_ast_expr_neg(expr);
9518 expr2 = isl_ast_expr_neg(expr2);
9519 equal = isl_ast_expr_is_equal(expr, expr2);
9520 str = isl_ast_expr_to_C_str(expr);
9521 ok = str ? !strcmp(str, "-(A + B)") : -1;
9522 free(str);
9523 isl_ast_expr_free(expr);
9524 isl_ast_expr_free(expr2);
9526 if (ok < 0 || equal < 0)
9527 return -1;
9528 if (!equal)
9529 isl_die(ctx, isl_error_unknown,
9530 "equal expressions not considered equal", return -1);
9531 if (!ok)
9532 isl_die(ctx, isl_error_unknown,
9533 "isl_ast_expr printed incorrectly", return -1);
9535 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
9536 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
9537 expr = isl_ast_expr_add(expr1, expr2);
9538 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
9539 expr = isl_ast_expr_sub(expr3, expr);
9540 str = isl_ast_expr_to_C_str(expr);
9541 ok = str ? !strcmp(str, "C - (A + B)") : -1;
9542 free(str);
9543 isl_ast_expr_free(expr);
9545 if (ok < 0)
9546 return -1;
9547 if (!ok)
9548 isl_die(ctx, isl_error_unknown,
9549 "isl_ast_expr printed incorrectly", return -1);
9551 return 0;
9554 /* Check that isl_ast_build_expr_from_set returns a valid expression
9555 * for an empty set. Note that isl_ast_build_expr_from_set getting
9556 * called on an empty set probably indicates a bug in the caller.
9558 static int test_ast_build(isl_ctx *ctx)
9560 isl_set *set;
9561 isl_ast_build *build;
9562 isl_ast_expr *expr;
9564 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9565 build = isl_ast_build_from_context(set);
9567 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
9568 expr = isl_ast_build_expr_from_set(build, set);
9570 isl_ast_expr_free(expr);
9571 isl_ast_build_free(build);
9573 if (!expr)
9574 return -1;
9576 return 0;
9579 /* Internal data structure for before_for and after_for callbacks.
9581 * depth is the current depth
9582 * before is the number of times before_for has been called
9583 * after is the number of times after_for has been called
9585 struct isl_test_codegen_data {
9586 int depth;
9587 int before;
9588 int after;
9591 /* This function is called before each for loop in the AST generated
9592 * from test_ast_gen1.
9594 * Increment the number of calls and the depth.
9595 * Check that the space returned by isl_ast_build_get_schedule_space
9596 * matches the target space of the schedule returned by
9597 * isl_ast_build_get_schedule.
9598 * Return an isl_id that is checked by the corresponding call
9599 * to after_for.
9601 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
9602 void *user)
9604 struct isl_test_codegen_data *data = user;
9605 isl_ctx *ctx;
9606 isl_space *space;
9607 isl_union_map *schedule;
9608 isl_union_set *uset;
9609 isl_set *set;
9610 isl_bool empty;
9611 isl_size n;
9612 char name[] = "d0";
9614 ctx = isl_ast_build_get_ctx(build);
9616 if (data->before >= 3)
9617 isl_die(ctx, isl_error_unknown,
9618 "unexpected number of for nodes", return NULL);
9619 if (data->depth < 0 || data->depth >= 2)
9620 isl_die(ctx, isl_error_unknown,
9621 "unexpected depth", return NULL);
9623 snprintf(name, sizeof(name), "d%d", data->depth);
9624 data->before++;
9625 data->depth++;
9627 schedule = isl_ast_build_get_schedule(build);
9628 uset = isl_union_map_range(schedule);
9629 n = isl_union_set_n_set(uset);
9630 if (n != 1) {
9631 isl_union_set_free(uset);
9632 if (n < 0)
9633 return NULL;
9634 isl_die(ctx, isl_error_unknown,
9635 "expecting single range space", return NULL);
9638 space = isl_ast_build_get_schedule_space(build);
9639 set = isl_union_set_extract_set(uset, space);
9640 isl_union_set_free(uset);
9641 empty = isl_set_is_empty(set);
9642 isl_set_free(set);
9644 if (empty < 0)
9645 return NULL;
9646 if (empty)
9647 isl_die(ctx, isl_error_unknown,
9648 "spaces don't match", return NULL);
9650 return isl_id_alloc(ctx, name, NULL);
9653 /* This function is called after each for loop in the AST generated
9654 * from test_ast_gen1.
9656 * Increment the number of calls and decrement the depth.
9657 * Check that the annotation attached to the node matches
9658 * the isl_id returned by the corresponding call to before_for.
9660 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
9661 __isl_keep isl_ast_build *build, void *user)
9663 struct isl_test_codegen_data *data = user;
9664 isl_id *id;
9665 const char *name;
9666 int valid;
9668 data->after++;
9669 data->depth--;
9671 if (data->after > data->before)
9672 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
9673 "mismatch in number of for nodes",
9674 return isl_ast_node_free(node));
9676 id = isl_ast_node_get_annotation(node);
9677 if (!id)
9678 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
9679 "missing annotation", return isl_ast_node_free(node));
9681 name = isl_id_get_name(id);
9682 valid = name && atoi(name + 1) == data->depth;
9683 isl_id_free(id);
9685 if (!valid)
9686 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
9687 "wrong annotation", return isl_ast_node_free(node));
9689 return node;
9692 /* Check that the before_each_for and after_each_for callbacks
9693 * are called for each for loop in the generated code,
9694 * that they are called in the right order and that the isl_id
9695 * returned from the before_each_for callback is attached to
9696 * the isl_ast_node passed to the corresponding after_each_for call.
9698 static int test_ast_gen1(isl_ctx *ctx)
9700 const char *str;
9701 isl_set *set;
9702 isl_union_map *schedule;
9703 isl_ast_build *build;
9704 isl_ast_node *tree;
9705 struct isl_test_codegen_data data;
9707 str = "[N] -> { : N >= 10 }";
9708 set = isl_set_read_from_str(ctx, str);
9709 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
9710 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
9711 schedule = isl_union_map_read_from_str(ctx, str);
9713 data.before = 0;
9714 data.after = 0;
9715 data.depth = 0;
9716 build = isl_ast_build_from_context(set);
9717 build = isl_ast_build_set_before_each_for(build,
9718 &before_for, &data);
9719 build = isl_ast_build_set_after_each_for(build,
9720 &after_for, &data);
9721 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9722 isl_ast_build_free(build);
9723 if (!tree)
9724 return -1;
9726 isl_ast_node_free(tree);
9728 if (data.before != 3 || data.after != 3)
9729 isl_die(ctx, isl_error_unknown,
9730 "unexpected number of for nodes", return -1);
9732 return 0;
9735 /* Check that the AST generator handles domains that are integrally disjoint
9736 * but not rationally disjoint.
9738 static int test_ast_gen2(isl_ctx *ctx)
9740 const char *str;
9741 isl_set *set;
9742 isl_union_map *schedule;
9743 isl_union_map *options;
9744 isl_ast_build *build;
9745 isl_ast_node *tree;
9747 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
9748 schedule = isl_union_map_read_from_str(ctx, str);
9749 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9750 build = isl_ast_build_from_context(set);
9752 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
9753 options = isl_union_map_read_from_str(ctx, str);
9754 build = isl_ast_build_set_options(build, options);
9755 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9756 isl_ast_build_free(build);
9757 if (!tree)
9758 return -1;
9759 isl_ast_node_free(tree);
9761 return 0;
9764 /* Increment *user on each call.
9766 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
9767 __isl_keep isl_ast_build *build, void *user)
9769 int *n = user;
9771 (*n)++;
9773 return node;
9776 /* Test that unrolling tries to minimize the number of instances.
9777 * In particular, for the schedule given below, make sure it generates
9778 * 3 nodes (rather than 101).
9780 static int test_ast_gen3(isl_ctx *ctx)
9782 const char *str;
9783 isl_set *set;
9784 isl_union_map *schedule;
9785 isl_union_map *options;
9786 isl_ast_build *build;
9787 isl_ast_node *tree;
9788 int n_domain = 0;
9790 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
9791 schedule = isl_union_map_read_from_str(ctx, str);
9792 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9794 str = "{ [i] -> unroll[0] }";
9795 options = isl_union_map_read_from_str(ctx, str);
9797 build = isl_ast_build_from_context(set);
9798 build = isl_ast_build_set_options(build, options);
9799 build = isl_ast_build_set_at_each_domain(build,
9800 &count_domains, &n_domain);
9801 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9802 isl_ast_build_free(build);
9803 if (!tree)
9804 return -1;
9806 isl_ast_node_free(tree);
9808 if (n_domain != 3)
9809 isl_die(ctx, isl_error_unknown,
9810 "unexpected number of for nodes", return -1);
9812 return 0;
9815 /* Check that if the ast_build_exploit_nested_bounds options is set,
9816 * we do not get an outer if node in the generated AST,
9817 * while we do get such an outer if node if the options is not set.
9819 static int test_ast_gen4(isl_ctx *ctx)
9821 const char *str;
9822 isl_set *set;
9823 isl_union_map *schedule;
9824 isl_ast_build *build;
9825 isl_ast_node *tree;
9826 enum isl_ast_node_type type;
9827 int enb;
9829 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
9830 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
9832 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
9834 schedule = isl_union_map_read_from_str(ctx, str);
9835 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9836 build = isl_ast_build_from_context(set);
9837 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9838 isl_ast_build_free(build);
9839 if (!tree)
9840 return -1;
9842 type = isl_ast_node_get_type(tree);
9843 isl_ast_node_free(tree);
9845 if (type == isl_ast_node_if)
9846 isl_die(ctx, isl_error_unknown,
9847 "not expecting if node", return -1);
9849 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
9851 schedule = isl_union_map_read_from_str(ctx, str);
9852 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9853 build = isl_ast_build_from_context(set);
9854 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9855 isl_ast_build_free(build);
9856 if (!tree)
9857 return -1;
9859 type = isl_ast_node_get_type(tree);
9860 isl_ast_node_free(tree);
9862 if (type != isl_ast_node_if)
9863 isl_die(ctx, isl_error_unknown,
9864 "expecting if node", return -1);
9866 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
9868 return 0;
9871 /* This function is called for each leaf in the AST generated
9872 * from test_ast_gen5.
9874 * We finalize the AST generation by extending the outer schedule
9875 * with a zero-dimensional schedule. If this results in any for loops,
9876 * then this means that we did not pass along enough information
9877 * about the outer schedule to the inner AST generation.
9879 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
9880 void *user)
9882 isl_union_map *schedule, *extra;
9883 isl_ast_node *tree;
9885 schedule = isl_ast_build_get_schedule(build);
9886 extra = isl_union_map_copy(schedule);
9887 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
9888 schedule = isl_union_map_range_product(schedule, extra);
9889 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9890 isl_ast_build_free(build);
9892 if (!tree)
9893 return NULL;
9895 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
9896 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
9897 "code should not contain any for loop",
9898 return isl_ast_node_free(tree));
9900 return tree;
9903 /* Check that we do not lose any information when going back and
9904 * forth between internal and external schedule.
9906 * In particular, we create an AST where we unroll the only
9907 * non-constant dimension in the schedule. We therefore do
9908 * not expect any for loops in the AST. However, older versions
9909 * of isl would not pass along enough information about the outer
9910 * schedule when performing an inner code generation from a create_leaf
9911 * callback, resulting in the inner code generation producing a for loop.
9913 static int test_ast_gen5(isl_ctx *ctx)
9915 const char *str;
9916 isl_set *set;
9917 isl_union_map *schedule, *options;
9918 isl_ast_build *build;
9919 isl_ast_node *tree;
9921 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
9922 schedule = isl_union_map_read_from_str(ctx, str);
9924 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
9925 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
9926 options = isl_union_map_read_from_str(ctx, str);
9928 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9929 build = isl_ast_build_from_context(set);
9930 build = isl_ast_build_set_options(build, options);
9931 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
9932 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9933 isl_ast_build_free(build);
9934 isl_ast_node_free(tree);
9935 if (!tree)
9936 return -1;
9938 return 0;
9941 /* Check that the expression
9943 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
9945 * is not combined into
9947 * min(n/2, 0)
9949 * as this would result in n/2 being evaluated in parts of
9950 * the definition domain where n is not a multiple of 2.
9952 static int test_ast_expr(isl_ctx *ctx)
9954 const char *str;
9955 isl_pw_aff *pa;
9956 isl_ast_build *build;
9957 isl_ast_expr *expr;
9958 int min_max;
9959 int is_min;
9961 min_max = isl_options_get_ast_build_detect_min_max(ctx);
9962 isl_options_set_ast_build_detect_min_max(ctx, 1);
9964 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
9965 pa = isl_pw_aff_read_from_str(ctx, str);
9966 build = isl_ast_build_alloc(ctx);
9967 expr = isl_ast_build_expr_from_pw_aff(build, pa);
9968 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
9969 isl_ast_expr_get_op_type(expr) == isl_ast_expr_op_min;
9970 isl_ast_build_free(build);
9971 isl_ast_expr_free(expr);
9973 isl_options_set_ast_build_detect_min_max(ctx, min_max);
9975 if (!expr)
9976 return -1;
9977 if (is_min)
9978 isl_die(ctx, isl_error_unknown,
9979 "expressions should not be combined", return -1);
9981 return 0;
9984 static int test_ast_gen(isl_ctx *ctx)
9986 if (test_ast_gen1(ctx) < 0)
9987 return -1;
9988 if (test_ast_gen2(ctx) < 0)
9989 return -1;
9990 if (test_ast_gen3(ctx) < 0)
9991 return -1;
9992 if (test_ast_gen4(ctx) < 0)
9993 return -1;
9994 if (test_ast_gen5(ctx) < 0)
9995 return -1;
9996 if (test_ast_expr(ctx) < 0)
9997 return -1;
9998 return 0;
10001 /* Check if dropping output dimensions from an isl_pw_multi_aff
10002 * works properly.
10004 static int test_pw_multi_aff(isl_ctx *ctx)
10006 const char *str;
10007 isl_pw_multi_aff *pma1, *pma2;
10008 int equal;
10010 str = "{ [i,j] -> [i+j, 4i-j] }";
10011 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
10012 str = "{ [i,j] -> [4i-j] }";
10013 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
10015 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
10017 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
10019 isl_pw_multi_aff_free(pma1);
10020 isl_pw_multi_aff_free(pma2);
10021 if (equal < 0)
10022 return -1;
10023 if (!equal)
10024 isl_die(ctx, isl_error_unknown,
10025 "expressions not equal", return -1);
10027 return 0;
10030 /* Check that we can properly parse multi piecewise affine expressions
10031 * where the piecewise affine expressions have different domains.
10033 static int test_multi_pw_aff_1(isl_ctx *ctx)
10035 const char *str;
10036 isl_set *dom, *dom2;
10037 isl_multi_pw_aff *mpa1, *mpa2;
10038 isl_pw_aff *pa;
10039 int equal;
10040 int equal_domain;
10042 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
10043 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
10044 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
10045 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
10046 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
10047 str = "{ [i] -> [(i : i > 0), 2i] }";
10048 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
10050 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
10052 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
10053 dom = isl_pw_aff_domain(pa);
10054 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
10055 dom2 = isl_pw_aff_domain(pa);
10056 equal_domain = isl_set_is_equal(dom, dom2);
10058 isl_set_free(dom);
10059 isl_set_free(dom2);
10060 isl_multi_pw_aff_free(mpa1);
10061 isl_multi_pw_aff_free(mpa2);
10063 if (equal < 0)
10064 return -1;
10065 if (!equal)
10066 isl_die(ctx, isl_error_unknown,
10067 "expressions not equal", return -1);
10069 if (equal_domain < 0)
10070 return -1;
10071 if (equal_domain)
10072 isl_die(ctx, isl_error_unknown,
10073 "domains unexpectedly equal", return -1);
10075 return 0;
10078 /* Check that the dimensions in the explicit domain
10079 * of a multi piecewise affine expression are properly
10080 * taken into account.
10082 static int test_multi_pw_aff_2(isl_ctx *ctx)
10084 const char *str;
10085 isl_bool involves1, involves2, involves3, equal;
10086 isl_multi_pw_aff *mpa, *mpa1, *mpa2;
10088 str = "{ A[x,y] -> B[] : x >= y }";
10089 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
10090 involves1 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 2);
10091 mpa1 = isl_multi_pw_aff_copy(mpa);
10093 mpa = isl_multi_pw_aff_insert_dims(mpa, isl_dim_in, 0, 1);
10094 involves2 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 1);
10095 involves3 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 1, 2);
10096 str = "{ [a,x,y] -> B[] : x >= y }";
10097 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
10098 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
10099 isl_multi_pw_aff_free(mpa2);
10101 mpa = isl_multi_pw_aff_drop_dims(mpa, isl_dim_in, 0, 1);
10102 mpa = isl_multi_pw_aff_set_tuple_name(mpa, isl_dim_in, "A");
10103 if (equal >= 0 && equal)
10104 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa1);
10105 isl_multi_pw_aff_free(mpa1);
10106 isl_multi_pw_aff_free(mpa);
10108 if (involves1 < 0 || involves2 < 0 || involves3 < 0 || equal < 0)
10109 return -1;
10110 if (!equal)
10111 isl_die(ctx, isl_error_unknown,
10112 "incorrect result of dimension insertion/removal",
10113 return isl_stat_error);
10114 if (!involves1 || involves2 || !involves3)
10115 isl_die(ctx, isl_error_unknown,
10116 "incorrect characterization of involved dimensions",
10117 return isl_stat_error);
10119 return 0;
10122 /* Check that isl_multi_union_pw_aff_multi_val_on_domain
10123 * sets the explicit domain of a zero-dimensional result,
10124 * such that it can be converted to an isl_union_map.
10126 static isl_stat test_multi_pw_aff_3(isl_ctx *ctx)
10128 isl_space *space;
10129 isl_union_set *dom;
10130 isl_multi_val *mv;
10131 isl_multi_union_pw_aff *mupa;
10132 isl_union_map *umap;
10134 dom = isl_union_set_read_from_str(ctx, "{ A[]; B[] }");
10135 space = isl_union_set_get_space(dom);
10136 mv = isl_multi_val_zero(isl_space_set_from_params(space));
10137 mupa = isl_multi_union_pw_aff_multi_val_on_domain(dom, mv);
10138 umap = isl_union_map_from_multi_union_pw_aff(mupa);
10139 isl_union_map_free(umap);
10140 if (!umap)
10141 return isl_stat_error;
10143 return isl_stat_ok;
10146 /* String descriptions of boxes that
10147 * are used for reconstructing box maps from their lower and upper bounds.
10149 static const char *multi_pw_aff_box_tests[] = {
10150 "{ A[x, y] -> [] : x + y >= 0 }",
10151 "[N] -> { A[x, y] -> [x] : x + y <= N }",
10152 "[N] -> { A[x, y] -> [x : y] : x + y <= N }",
10155 /* Check that map representations of boxes can be reconstructed
10156 * from their lower and upper bounds.
10158 static isl_stat test_multi_pw_aff_box(isl_ctx *ctx)
10160 int i;
10162 for (i = 0; i < ARRAY_SIZE(multi_pw_aff_box_tests); ++i) {
10163 const char *str;
10164 isl_bool equal;
10165 isl_map *map, *box;
10166 isl_multi_pw_aff *min, *max;
10168 str = multi_pw_aff_box_tests[i];
10169 map = isl_map_read_from_str(ctx, str);
10170 min = isl_map_min_multi_pw_aff(isl_map_copy(map));
10171 max = isl_map_max_multi_pw_aff(isl_map_copy(map));
10172 box = isl_map_universe(isl_map_get_space(map));
10173 box = isl_map_lower_bound_multi_pw_aff(box, min);
10174 box = isl_map_upper_bound_multi_pw_aff(box, max);
10175 equal = isl_map_is_equal(map, box);
10176 isl_map_free(map);
10177 isl_map_free(box);
10178 if (equal < 0)
10179 return isl_stat_error;
10180 if (!equal)
10181 isl_die(ctx, isl_error_unknown,
10182 "unexpected result", return isl_stat_error);
10185 return isl_stat_ok;
10188 /* Perform some tests on multi piecewise affine expressions.
10190 static int test_multi_pw_aff(isl_ctx *ctx)
10192 if (test_multi_pw_aff_1(ctx) < 0)
10193 return -1;
10194 if (test_multi_pw_aff_2(ctx) < 0)
10195 return -1;
10196 if (test_multi_pw_aff_3(ctx) < 0)
10197 return -1;
10198 if (test_multi_pw_aff_box(ctx) < 0)
10199 return -1;
10200 return 0;
10203 /* This is a regression test for a bug where isl_basic_map_simplify
10204 * would end up in an infinite loop. In particular, we construct
10205 * an empty basic set that is not obviously empty.
10206 * isl_basic_set_is_empty marks the basic set as empty.
10207 * After projecting out i3, the variable can be dropped completely,
10208 * but isl_basic_map_simplify refrains from doing so if the basic set
10209 * is empty and would end up in an infinite loop if it didn't test
10210 * explicitly for empty basic maps in the outer loop.
10212 static int test_simplify_1(isl_ctx *ctx)
10214 const char *str;
10215 isl_basic_set *bset;
10216 int empty;
10218 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
10219 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
10220 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
10221 "i3 >= i2 }";
10222 bset = isl_basic_set_read_from_str(ctx, str);
10223 empty = isl_basic_set_is_empty(bset);
10224 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
10225 isl_basic_set_free(bset);
10226 if (!bset)
10227 return -1;
10228 if (!empty)
10229 isl_die(ctx, isl_error_unknown,
10230 "basic set should be empty", return -1);
10232 return 0;
10235 /* Check that the equality in the set description below
10236 * is simplified away.
10238 static int test_simplify_2(isl_ctx *ctx)
10240 const char *str;
10241 isl_basic_set *bset;
10242 isl_bool universe;
10244 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
10245 bset = isl_basic_set_read_from_str(ctx, str);
10246 universe = isl_basic_set_plain_is_universe(bset);
10247 isl_basic_set_free(bset);
10249 if (universe < 0)
10250 return -1;
10251 if (!universe)
10252 isl_die(ctx, isl_error_unknown,
10253 "equality not simplified away", return -1);
10254 return 0;
10257 /* Some simplification tests.
10259 static int test_simplify(isl_ctx *ctx)
10261 if (test_simplify_1(ctx) < 0)
10262 return -1;
10263 if (test_simplify_2(ctx) < 0)
10264 return -1;
10265 return 0;
10268 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
10269 * with gbr context would fail to disable the use of the shifted tableau
10270 * when transferring equalities for the input to the context, resulting
10271 * in invalid sample values.
10273 static int test_partial_lexmin(isl_ctx *ctx)
10275 const char *str;
10276 isl_basic_set *bset;
10277 isl_basic_map *bmap;
10278 isl_map *map;
10280 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
10281 bmap = isl_basic_map_read_from_str(ctx, str);
10282 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
10283 bset = isl_basic_set_read_from_str(ctx, str);
10284 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
10285 isl_map_free(map);
10287 if (!map)
10288 return -1;
10290 return 0;
10293 /* Check that the variable compression performed on the existentially
10294 * quantified variables inside isl_basic_set_compute_divs is not confused
10295 * by the implicit equalities among the parameters.
10297 static int test_compute_divs(isl_ctx *ctx)
10299 const char *str;
10300 isl_basic_set *bset;
10301 isl_set *set;
10303 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
10304 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
10305 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
10306 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
10307 bset = isl_basic_set_read_from_str(ctx, str);
10308 set = isl_basic_set_compute_divs(bset);
10309 isl_set_free(set);
10310 if (!set)
10311 return -1;
10313 return 0;
10316 /* Check that isl_schedule_get_map is not confused by a schedule tree
10317 * with divergent filter node parameters, as can result from a call
10318 * to isl_schedule_intersect_domain.
10320 static int test_schedule_tree(isl_ctx *ctx)
10322 const char *str;
10323 isl_union_set *uset;
10324 isl_schedule *sched1, *sched2;
10325 isl_union_map *umap;
10327 uset = isl_union_set_read_from_str(ctx, "{ A[i] }");
10328 sched1 = isl_schedule_from_domain(uset);
10329 uset = isl_union_set_read_from_str(ctx, "{ B[] }");
10330 sched2 = isl_schedule_from_domain(uset);
10332 sched1 = isl_schedule_sequence(sched1, sched2);
10333 str = "[n] -> { A[i] : 0 <= i < n; B[] }";
10334 uset = isl_union_set_read_from_str(ctx, str);
10335 sched1 = isl_schedule_intersect_domain(sched1, uset);
10336 umap = isl_schedule_get_map(sched1);
10337 isl_schedule_free(sched1);
10338 isl_union_map_free(umap);
10339 if (!umap)
10340 return -1;
10342 return 0;
10345 /* Check that a zero-dimensional prefix schedule keeps track
10346 * of the domain and outer filters.
10348 static int test_schedule_tree_prefix(isl_ctx *ctx)
10350 const char *str;
10351 isl_bool equal;
10352 isl_union_set *uset;
10353 isl_union_set_list *filters;
10354 isl_multi_union_pw_aff *mupa, *mupa2;
10355 isl_schedule_node *node;
10357 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
10358 uset = isl_union_set_read_from_str(ctx, str);
10359 node = isl_schedule_node_from_domain(uset);
10360 node = isl_schedule_node_child(node, 0);
10362 str = "{ S1[i,j] : i > j }";
10363 uset = isl_union_set_read_from_str(ctx, str);
10364 filters = isl_union_set_list_from_union_set(uset);
10365 str = "{ S1[i,j] : i <= j; S2[i,j] }";
10366 uset = isl_union_set_read_from_str(ctx, str);
10367 filters = isl_union_set_list_add(filters, uset);
10368 node = isl_schedule_node_insert_sequence(node, filters);
10370 node = isl_schedule_node_grandchild(node, 0, 0);
10371 mupa = isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
10372 str = "([] : { S1[i,j] : i > j })";
10373 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx, str);
10374 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
10375 isl_multi_union_pw_aff_free(mupa2);
10376 isl_multi_union_pw_aff_free(mupa);
10377 isl_schedule_node_free(node);
10379 if (equal < 0)
10380 return -1;
10381 if (!equal)
10382 isl_die(ctx, isl_error_unknown, "unexpected prefix schedule",
10383 return -1);
10385 return 0;
10388 /* Check that the reaching domain elements and the prefix schedule
10389 * at a leaf node are the same before and after grouping.
10391 static int test_schedule_tree_group_1(isl_ctx *ctx)
10393 int equal;
10394 const char *str;
10395 isl_id *id;
10396 isl_union_set *uset;
10397 isl_multi_union_pw_aff *mupa;
10398 isl_union_pw_multi_aff *upma1, *upma2;
10399 isl_union_set *domain1, *domain2;
10400 isl_union_map *umap1, *umap2;
10401 isl_schedule_node *node;
10403 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
10404 uset = isl_union_set_read_from_str(ctx, str);
10405 node = isl_schedule_node_from_domain(uset);
10406 node = isl_schedule_node_child(node, 0);
10407 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
10408 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10409 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10410 node = isl_schedule_node_child(node, 0);
10411 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
10412 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10413 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10414 node = isl_schedule_node_child(node, 0);
10415 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
10416 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
10417 domain1 = isl_schedule_node_get_domain(node);
10418 id = isl_id_alloc(ctx, "group", NULL);
10419 node = isl_schedule_node_parent(node);
10420 node = isl_schedule_node_group(node, id);
10421 node = isl_schedule_node_child(node, 0);
10422 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
10423 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
10424 domain2 = isl_schedule_node_get_domain(node);
10425 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
10426 if (equal >= 0 && equal)
10427 equal = isl_union_set_is_equal(domain1, domain2);
10428 if (equal >= 0 && equal)
10429 equal = isl_union_map_is_equal(umap1, umap2);
10430 isl_union_map_free(umap1);
10431 isl_union_map_free(umap2);
10432 isl_union_set_free(domain1);
10433 isl_union_set_free(domain2);
10434 isl_union_pw_multi_aff_free(upma1);
10435 isl_union_pw_multi_aff_free(upma2);
10436 isl_schedule_node_free(node);
10438 if (equal < 0)
10439 return -1;
10440 if (!equal)
10441 isl_die(ctx, isl_error_unknown,
10442 "expressions not equal", return -1);
10444 return 0;
10447 /* Check that we can have nested groupings and that the union map
10448 * schedule representation is the same before and after the grouping.
10449 * Note that after the grouping, the union map representation contains
10450 * the domain constraints from the ranges of the expansion nodes,
10451 * while they are missing from the union map representation of
10452 * the tree without expansion nodes.
10454 * Also check that the global expansion is as expected.
10456 static int test_schedule_tree_group_2(isl_ctx *ctx)
10458 int equal, equal_expansion;
10459 const char *str;
10460 isl_id *id;
10461 isl_union_set *uset;
10462 isl_union_map *umap1, *umap2;
10463 isl_union_map *expansion1, *expansion2;
10464 isl_union_set_list *filters;
10465 isl_multi_union_pw_aff *mupa;
10466 isl_schedule *schedule;
10467 isl_schedule_node *node;
10469 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
10470 "S3[i,j] : 0 <= i,j < 10 }";
10471 uset = isl_union_set_read_from_str(ctx, str);
10472 node = isl_schedule_node_from_domain(uset);
10473 node = isl_schedule_node_child(node, 0);
10474 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
10475 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10476 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10477 node = isl_schedule_node_child(node, 0);
10478 str = "{ S1[i,j] }";
10479 uset = isl_union_set_read_from_str(ctx, str);
10480 filters = isl_union_set_list_from_union_set(uset);
10481 str = "{ S2[i,j]; S3[i,j] }";
10482 uset = isl_union_set_read_from_str(ctx, str);
10483 filters = isl_union_set_list_add(filters, uset);
10484 node = isl_schedule_node_insert_sequence(node, filters);
10485 node = isl_schedule_node_grandchild(node, 1, 0);
10486 str = "{ S2[i,j] }";
10487 uset = isl_union_set_read_from_str(ctx, str);
10488 filters = isl_union_set_list_from_union_set(uset);
10489 str = "{ S3[i,j] }";
10490 uset = isl_union_set_read_from_str(ctx, str);
10491 filters = isl_union_set_list_add(filters, uset);
10492 node = isl_schedule_node_insert_sequence(node, filters);
10494 schedule = isl_schedule_node_get_schedule(node);
10495 umap1 = isl_schedule_get_map(schedule);
10496 uset = isl_schedule_get_domain(schedule);
10497 umap1 = isl_union_map_intersect_domain(umap1, uset);
10498 isl_schedule_free(schedule);
10500 node = isl_schedule_node_grandparent(node);
10501 id = isl_id_alloc(ctx, "group1", NULL);
10502 node = isl_schedule_node_group(node, id);
10503 node = isl_schedule_node_grandchild(node, 1, 0);
10504 id = isl_id_alloc(ctx, "group2", NULL);
10505 node = isl_schedule_node_group(node, id);
10507 schedule = isl_schedule_node_get_schedule(node);
10508 umap2 = isl_schedule_get_map(schedule);
10509 isl_schedule_free(schedule);
10511 node = isl_schedule_node_root(node);
10512 node = isl_schedule_node_child(node, 0);
10513 expansion1 = isl_schedule_node_get_subtree_expansion(node);
10514 isl_schedule_node_free(node);
10516 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
10517 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
10518 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
10520 expansion2 = isl_union_map_read_from_str(ctx, str);
10522 equal = isl_union_map_is_equal(umap1, umap2);
10523 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
10525 isl_union_map_free(umap1);
10526 isl_union_map_free(umap2);
10527 isl_union_map_free(expansion1);
10528 isl_union_map_free(expansion2);
10530 if (equal < 0 || equal_expansion < 0)
10531 return -1;
10532 if (!equal)
10533 isl_die(ctx, isl_error_unknown,
10534 "expressions not equal", return -1);
10535 if (!equal_expansion)
10536 isl_die(ctx, isl_error_unknown,
10537 "unexpected expansion", return -1);
10539 return 0;
10542 /* Some tests for the isl_schedule_node_group function.
10544 static int test_schedule_tree_group(isl_ctx *ctx)
10546 if (test_schedule_tree_group_1(ctx) < 0)
10547 return -1;
10548 if (test_schedule_tree_group_2(ctx) < 0)
10549 return -1;
10550 return 0;
10553 struct {
10554 const char *set;
10555 const char *dual;
10556 } coef_tests[] = {
10557 { "{ rat: [i] : 0 <= i <= 10 }",
10558 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
10559 { "{ rat: [i] : FALSE }",
10560 "{ rat: coefficients[[cst] -> [a]] }" },
10561 { "{ rat: [i] : }",
10562 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
10563 { "{ [0:,1,2:3] }",
10564 "{ rat: coefficients[[c_cst] -> [a, b, c]] : "
10565 "a >= 0 and 2c >= -c_cst - b and 3c >= -c_cst - b }" },
10566 { "[M, N] -> { [x = (1 - N):-1, -4x:(M - 4x)] }",
10567 "{ rat: coefficients[[c_cst, c_M = 0:, c_N = 0:] -> [a, b = -c_M:]] :"
10568 "4b >= -c_N + a and 4b >= -c_cst - 2c_N + a }" },
10569 { "{ rat : [x, y] : 1 <= 2x <= 9 and 2 <= 3y <= 16 }",
10570 "{ rat: coefficients[[c_cst] -> [c_x, c_y]] : "
10571 "4c_y >= -6c_cst - 3c_x and 4c_y >= -6c_cst - 27c_x and "
10572 "32c_y >= -6c_cst - 3c_x and 32c_y >= -6c_cst - 27c_x }" },
10573 { "{ [x, y, z] : 3y <= 2x - 2 and y >= -2 + 2x and 2y >= 2 - x }",
10574 "{ rat: coefficients[[cst] -> [a, b, c]] }" },
10577 struct {
10578 const char *set;
10579 const char *dual;
10580 } sol_tests[] = {
10581 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
10582 "{ rat: [i] : 0 <= i <= 10 }" },
10583 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
10584 "{ rat: [i] }" },
10585 { "{ rat: coefficients[[cst] -> [a]] }",
10586 "{ rat: [i] : FALSE }" },
10589 /* Test the basic functionality of isl_basic_set_coefficients and
10590 * isl_basic_set_solutions.
10592 static int test_dual(isl_ctx *ctx)
10594 int i;
10596 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
10597 int equal;
10598 isl_basic_set *bset1, *bset2;
10600 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
10601 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
10602 bset1 = isl_basic_set_coefficients(bset1);
10603 equal = isl_basic_set_is_equal(bset1, bset2);
10604 isl_basic_set_free(bset1);
10605 isl_basic_set_free(bset2);
10606 if (equal < 0)
10607 return -1;
10608 if (!equal)
10609 isl_die(ctx, isl_error_unknown,
10610 "incorrect dual", return -1);
10613 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
10614 int equal;
10615 isl_basic_set *bset1, *bset2;
10617 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
10618 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
10619 bset1 = isl_basic_set_solutions(bset1);
10620 equal = isl_basic_set_is_equal(bset1, bset2);
10621 isl_basic_set_free(bset1);
10622 isl_basic_set_free(bset2);
10623 if (equal < 0)
10624 return -1;
10625 if (!equal)
10626 isl_die(ctx, isl_error_unknown,
10627 "incorrect dual", return -1);
10630 return 0;
10633 struct {
10634 int scale_tile;
10635 int shift_point;
10636 const char *domain;
10637 const char *schedule;
10638 const char *sizes;
10639 const char *tile;
10640 const char *point;
10641 } tile_tests[] = {
10642 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
10643 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10644 "{ [32,32] }",
10645 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
10646 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10648 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
10649 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10650 "{ [32,32] }",
10651 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
10652 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10654 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
10655 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10656 "{ [32,32] }",
10657 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
10658 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
10660 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
10661 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10662 "{ [32,32] }",
10663 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
10664 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
10668 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
10669 * tile the band and then check if the tile and point bands have the
10670 * expected partial schedule.
10672 static int test_tile(isl_ctx *ctx)
10674 int i;
10675 int scale;
10676 int shift;
10678 scale = isl_options_get_tile_scale_tile_loops(ctx);
10679 shift = isl_options_get_tile_shift_point_loops(ctx);
10681 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
10682 int opt;
10683 int equal;
10684 const char *str;
10685 isl_union_set *domain;
10686 isl_multi_union_pw_aff *mupa, *mupa2;
10687 isl_schedule_node *node;
10688 isl_multi_val *sizes;
10690 opt = tile_tests[i].scale_tile;
10691 isl_options_set_tile_scale_tile_loops(ctx, opt);
10692 opt = tile_tests[i].shift_point;
10693 isl_options_set_tile_shift_point_loops(ctx, opt);
10695 str = tile_tests[i].domain;
10696 domain = isl_union_set_read_from_str(ctx, str);
10697 node = isl_schedule_node_from_domain(domain);
10698 node = isl_schedule_node_child(node, 0);
10699 str = tile_tests[i].schedule;
10700 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10701 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10702 str = tile_tests[i].sizes;
10703 sizes = isl_multi_val_read_from_str(ctx, str);
10704 node = isl_schedule_node_band_tile(node, sizes);
10706 str = tile_tests[i].tile;
10707 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10708 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
10709 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
10710 isl_multi_union_pw_aff_free(mupa);
10711 isl_multi_union_pw_aff_free(mupa2);
10713 node = isl_schedule_node_child(node, 0);
10715 str = tile_tests[i].point;
10716 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10717 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
10718 if (equal >= 0 && equal)
10719 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
10720 mupa2);
10721 isl_multi_union_pw_aff_free(mupa);
10722 isl_multi_union_pw_aff_free(mupa2);
10724 isl_schedule_node_free(node);
10726 if (equal < 0)
10727 return -1;
10728 if (!equal)
10729 isl_die(ctx, isl_error_unknown,
10730 "unexpected result", return -1);
10733 isl_options_set_tile_scale_tile_loops(ctx, scale);
10734 isl_options_set_tile_shift_point_loops(ctx, shift);
10736 return 0;
10739 /* Check that the domain hash of a space is equal to the hash
10740 * of the domain of the space, both ignoring parameters.
10742 static int test_domain_hash(isl_ctx *ctx)
10744 isl_map *map;
10745 isl_space *space;
10746 uint32_t hash1, hash2;
10748 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
10749 space = isl_map_get_space(map);
10750 isl_map_free(map);
10751 hash1 = isl_space_get_tuple_domain_hash(space);
10752 space = isl_space_domain(space);
10753 hash2 = isl_space_get_tuple_hash(space);
10754 isl_space_free(space);
10756 if (!space)
10757 return -1;
10758 if (hash1 != hash2)
10759 isl_die(ctx, isl_error_unknown,
10760 "domain hash not equal to hash of domain", return -1);
10762 return 0;
10765 /* Check that a universe basic set that is not obviously equal to the universe
10766 * is still recognized as being equal to the universe.
10768 static int test_universe(isl_ctx *ctx)
10770 const char *s;
10771 isl_basic_set *bset;
10772 isl_bool is_univ;
10774 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
10775 bset = isl_basic_set_read_from_str(ctx, s);
10776 is_univ = isl_basic_set_is_universe(bset);
10777 isl_basic_set_free(bset);
10779 if (is_univ < 0)
10780 return -1;
10781 if (!is_univ)
10782 isl_die(ctx, isl_error_unknown,
10783 "not recognized as universe set", return -1);
10785 return 0;
10788 /* Sets for which chambers are computed and checked.
10790 const char *chambers_tests[] = {
10791 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
10792 "z >= 0 and z <= C - y and z <= B - x - y }",
10795 /* Add the domain of "cell" to "cells".
10797 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
10799 isl_basic_set_list **cells = user;
10800 isl_basic_set *dom;
10802 dom = isl_cell_get_domain(cell);
10803 isl_cell_free(cell);
10804 *cells = isl_basic_set_list_add(*cells, dom);
10806 return *cells ? isl_stat_ok : isl_stat_error;
10809 /* Check that the elements of "list" are pairwise disjoint.
10811 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
10813 int i, j;
10814 isl_size n;
10816 n = isl_basic_set_list_n_basic_set(list);
10817 if (n < 0)
10818 return isl_stat_error;
10820 for (i = 0; i < n; ++i) {
10821 isl_basic_set *bset_i;
10823 bset_i = isl_basic_set_list_get_basic_set(list, i);
10824 for (j = i + 1; j < n; ++j) {
10825 isl_basic_set *bset_j;
10826 isl_bool disjoint;
10828 bset_j = isl_basic_set_list_get_basic_set(list, j);
10829 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
10830 isl_basic_set_free(bset_j);
10831 if (!disjoint)
10832 isl_die(isl_basic_set_list_get_ctx(list),
10833 isl_error_unknown, "not disjoint",
10834 break);
10835 if (disjoint < 0 || !disjoint)
10836 break;
10838 isl_basic_set_free(bset_i);
10839 if (j < n)
10840 return isl_stat_error;
10843 return isl_stat_ok;
10846 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
10847 * are pairwise disjoint.
10849 static int test_chambers(isl_ctx *ctx)
10851 int i;
10853 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
10854 isl_basic_set *bset;
10855 isl_vertices *vertices;
10856 isl_basic_set_list *cells;
10857 isl_stat ok;
10859 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
10860 vertices = isl_basic_set_compute_vertices(bset);
10861 cells = isl_basic_set_list_alloc(ctx, 0);
10862 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
10863 &cells) < 0)
10864 cells = isl_basic_set_list_free(cells);
10865 ok = check_pairwise_disjoint(cells);
10866 isl_basic_set_list_free(cells);
10867 isl_vertices_free(vertices);
10868 isl_basic_set_free(bset);
10870 if (ok < 0)
10871 return -1;
10874 return 0;
10877 struct {
10878 const char *name;
10879 int (*fn)(isl_ctx *ctx);
10880 } tests [] = {
10881 { "universe", &test_universe },
10882 { "domain hash", &test_domain_hash },
10883 { "dual", &test_dual },
10884 { "dependence analysis", &test_flow },
10885 { "val", &test_val },
10886 { "compute divs", &test_compute_divs },
10887 { "partial lexmin", &test_partial_lexmin },
10888 { "simplify", &test_simplify },
10889 { "curry", &test_curry },
10890 { "piecewise multi affine expressions", &test_pw_multi_aff },
10891 { "multi piecewise affine expressions", &test_multi_pw_aff },
10892 { "conversion", &test_conversion },
10893 { "list", &test_list },
10894 { "align parameters", &test_align_parameters },
10895 { "drop unused parameters", &test_drop_unused_parameters },
10896 { "pullback", &test_pullback },
10897 { "AST", &test_ast },
10898 { "AST build", &test_ast_build },
10899 { "AST generation", &test_ast_gen },
10900 { "eliminate", &test_eliminate },
10901 { "deltas_map", &test_deltas_map },
10902 { "residue class", &test_residue_class },
10903 { "div", &test_div },
10904 { "slice", &test_slice },
10905 { "fixed power", &test_fixed_power },
10906 { "sample", &test_sample },
10907 { "empty projection", &test_empty_projection },
10908 { "output", &test_output },
10909 { "vertices", &test_vertices },
10910 { "chambers", &test_chambers },
10911 { "fixed", &test_fixed },
10912 { "equal", &test_equal },
10913 { "disjoint", &test_disjoint },
10914 { "product", &test_product },
10915 { "dim_max", &test_dim_max },
10916 { "affine", &test_aff },
10917 { "injective", &test_injective },
10918 { "schedule (whole component)", &test_schedule_whole },
10919 { "schedule (incremental)", &test_schedule_incremental },
10920 { "schedule tree", &test_schedule_tree },
10921 { "schedule tree prefix", &test_schedule_tree_prefix },
10922 { "schedule tree grouping", &test_schedule_tree_group },
10923 { "tile", &test_tile },
10924 { "union map", &test_union_map },
10925 { "union_pw", &test_union_pw },
10926 { "locus", &test_locus },
10927 { "eval", &test_eval },
10928 { "parse", &test_parse },
10929 { "single-valued", &test_sv },
10930 { "recession cone", &test_recession_cone },
10931 { "affine hull", &test_affine_hull },
10932 { "simple_hull", &test_simple_hull },
10933 { "box hull", &test_box_hull },
10934 { "coalesce", &test_coalesce },
10935 { "factorize", &test_factorize },
10936 { "subset", &test_subset },
10937 { "subtract", &test_subtract },
10938 { "intersect", &test_intersect },
10939 { "lexmin", &test_lexmin },
10940 { "min", &test_min },
10941 { "set lower bounds", &test_min_mpa },
10942 { "gist", &test_gist },
10943 { "piecewise quasi-polynomials", &test_pwqp },
10944 { "lift", &test_lift },
10945 { "bind parameters", &test_bind },
10946 { "unbind parameters", &test_unbind },
10947 { "bound", &test_bound },
10948 { "get lists", &test_get_list },
10949 { "union", &test_union },
10950 { "split periods", &test_split_periods },
10951 { "lexicographic order", &test_lex },
10952 { "bijectivity", &test_bijective },
10953 { "dataflow analysis", &test_dep },
10954 { "reading", &test_read },
10955 { "bounded", &test_bounded },
10956 { "construction", &test_construction },
10957 { "dimension manipulation", &test_dim },
10958 { "map application", &test_application },
10959 { "convex hull", &test_convex_hull },
10960 { "transitive closure", &test_closure },
10961 { "isl_bool", &test_isl_bool},
10964 int main(int argc, char **argv)
10966 int i;
10967 struct isl_ctx *ctx;
10968 struct isl_options *options;
10970 options = isl_options_new_with_defaults();
10971 assert(options);
10972 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
10974 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
10975 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
10976 printf("%s\n", tests[i].name);
10977 if (tests[i].fn(ctx) < 0)
10978 goto error;
10980 isl_ctx_free(ctx);
10981 return 0;
10982 error:
10983 isl_ctx_free(ctx);
10984 return -1;