isl_tab_basic_map_partial_lexopt: copy integer divisions to context
[isl.git] / isl_test.c
blobf0fd4f83eece49db75432b81535f2f99e554f9d8
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) }",
171 "{ [a, b] -> [(2*floor((a)/8) + floor((b)/6))] }",
174 #undef BASE
175 #define BASE pw_multi_aff
177 #include "check_reparse_templ.c"
178 #include "check_reparse_test_templ.c"
180 /* Test parsing of piecewise multi affine expressions by printing
181 * the expressions and checking that parsing the output results
182 * in the same expression.
183 * Do this for an expression converted from a map with an output
184 * dimension name that is equal to an automatically generated name, and
185 * a set of expressions parsed from strings.
187 static isl_stat test_parse_pma(isl_ctx *ctx)
189 isl_map *map;
190 isl_pw_multi_aff *pma;
192 map = isl_map_read_from_str(ctx, "{ [a, a] -> [i1 = a + 1] }");
193 pma = isl_pw_multi_aff_from_map(map);
194 if (check_reparse_pw_multi_aff(ctx, pma) < 0)
195 return isl_stat_error;
197 if (check_reparse_pw_multi_aff_tests(ctx) < 0)
198 return isl_stat_error;
200 return isl_stat_ok;
203 /* String descriptions that cannot be parsed
204 * as union piecewise multi affine expressions.
206 static const char *parse_union_pw_multi_aff_fail_tests[] = {
207 "{ [a] -> [b] : b = a }",
208 "{ [a] -> [b = a] : b >= 0 }",
211 #undef BASE
212 #define BASE union_pw_multi_aff
214 #include "check_parse_fail_test_templ.c"
216 /* Test parsing of union piecewise multi affine expressions.
218 * In particular, check some cases where parsing is supposed to fail.
220 static isl_stat test_parse_upma(isl_ctx *ctx)
222 if (check_parse_union_pw_multi_aff_fail_tests(ctx) < 0)
223 return isl_stat_error;
225 return isl_stat_ok;
228 /* Test parsing of multi piecewise affine expressions by printing
229 * the expressions and checking that parsing the output results
230 * in the same expression.
231 * Do this for a couple of manually constructed expressions,
232 * an expression converted from a map with an output dimension name
233 * that is equal to an automatically generated name, and
234 * a set of expressions parsed from strings.
236 * Additionally, check some cases where parsing is supposed to fail.
238 static int test_parse_mpa(isl_ctx *ctx)
240 isl_space *space;
241 isl_set *dom;
242 isl_map *map;
243 isl_pw_multi_aff *pma;
244 isl_multi_pw_aff *mpa;
245 isl_stat r;
247 space = isl_space_set_alloc(ctx, 0, 0);
248 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
249 mpa = isl_multi_pw_aff_zero(space);
250 r = check_reparse_multi_pw_aff(ctx, mpa);
251 if (r < 0)
252 return -1;
254 space = isl_space_set_alloc(ctx, 1, 0);
255 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
256 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
257 dom = isl_set_universe(isl_space_params(isl_space_copy(space)));
258 dom = isl_set_lower_bound_si(dom, isl_dim_param, 0, 5);
259 mpa = isl_multi_pw_aff_zero(space);
260 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
261 r = check_reparse_multi_pw_aff(ctx, mpa);
262 if (r < 0)
263 return -1;
265 map = isl_map_read_from_str(ctx, "{ [a, a] -> [i1 = a + 1] }");
266 pma = isl_pw_multi_aff_from_map(map);
267 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
268 if (check_reparse_multi_pw_aff(ctx, mpa) < 0)
269 return -1;
271 if (check_reparse_multi_pw_aff_tests(ctx) < 0)
272 return -1;
273 if (check_parse_multi_pw_aff_fail_tests(ctx) < 0)
274 return -1;
276 return 0;
279 /* String descriptions of multi union piecewise affine expressions
280 * that are used for testing printing and parsing.
282 static const char *reparse_multi_union_pw_aff_tests[] = {
283 "[]",
284 "A[]",
285 "A[B[] -> C[]]",
286 "(A[] : { S[x] : x > 0; T[y] : y >= 0 })",
287 "(A[] : { })",
288 "[N] -> (A[] : { })",
289 "[N] -> (A[] : { : N >= 0 })",
290 "[N] -> (A[] : { S[x] : x > N; T[y] : y >= 0 })",
291 "(A[] : [N] -> { S[x] : x > N; T[y] : y >= 0 })",
292 "A[{ S[x] -> [x + 1]; T[x] -> [x] }]",
293 "(A[{ S[x] -> [x + 1]; T[x] -> [x] }] : "
294 "{ S[x] : x > 0; T[y] : y >= 0 })",
297 #undef BASE
298 #define BASE multi_union_pw_aff
300 #include "check_reparse_templ.c"
301 #include "check_reparse_test_templ.c"
303 /* Test parsing of multi union piecewise affine expressions by printing
304 * the expressions and checking that parsing the output results
305 * in the same expression.
306 * Do this for a couple of manually constructed expressions and
307 * a set of expressions parsed from strings.
309 static int test_parse_mupa(isl_ctx *ctx)
311 isl_space *space;
312 isl_multi_union_pw_aff *mupa;
313 isl_set *dom;
314 isl_union_set *uset;
315 isl_stat r;
317 space = isl_space_set_alloc(ctx, 0, 0);
318 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
319 mupa = isl_multi_union_pw_aff_zero(space);
320 r = check_reparse_multi_union_pw_aff(ctx, mupa);
321 if (r < 0)
322 return -1;
324 space = isl_space_set_alloc(ctx, 1, 0);
325 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
326 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
327 dom = isl_set_universe(space);
328 dom = isl_set_lower_bound_si(dom, isl_dim_param, 0, 5);
329 uset = isl_union_set_from_set(dom);
330 space = isl_space_set_alloc(ctx, 1, 0);
331 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
332 space = isl_space_set_tuple_name(space, isl_dim_set, "B");
333 mupa = isl_multi_union_pw_aff_zero(space);
334 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, uset);
335 r = check_reparse_multi_union_pw_aff(ctx, mupa);
336 if (r < 0)
337 return -1;
339 if (check_reparse_multi_union_pw_aff_tests(ctx) < 0)
340 return -1;
342 return 0;
345 /* Test parsing of multi expressions.
347 static int test_parse_multi(isl_ctx *ctx)
349 if (test_parse_mpa(ctx) < 0)
350 return -1;
351 if (test_parse_mupa(ctx) < 0)
352 return -1;
354 return 0;
357 /* Pairs of binary relation representations that should represent
358 * the same binary relations.
360 struct {
361 const char *map1;
362 const char *map2;
363 } parse_map_equal_tests[] = {
364 { "{ [x,y] : [([x/2]+y)/3] >= 1 }",
365 "{ [x, y] : 2y >= 6 - x }" },
366 { "{ [x,y] : x <= min(y, 2*y+3) }",
367 "{ [x,y] : x <= y, 2*y + 3 }" },
368 { "{ [x,y] : x >= min(y, 2*y+3) }",
369 "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }" },
370 { "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }",
371 "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }" },
372 { "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }",
373 "{ [i,j] -> [min(i,j)] }" },
374 { "{ [i,j] : i != j }",
375 "{ [i,j] : i < j or i > j }" },
376 { "{ [i,j] : (i+1)*2 >= j }",
377 "{ [i, j] : j <= 2 + 2i }" },
378 { "{ [i] -> [i > 0 ? 4 : 5] }",
379 "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }" },
380 { "[N=2,M] -> { [i=[(M+N)/4]] }",
381 "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }" },
382 { "{ [x] : x >= 0 }",
383 "{ [x] : x-0 >= 0 }" },
384 { "{ [i] : ((i > 10)) }",
385 "{ [i] : i >= 11 }" },
386 { "{ [i] -> [0] }",
387 "{ [i] -> [0 * i] }" },
388 { "{ [a] -> [b] : (not false) }",
389 "{ [a] -> [b] : true }" },
390 { "{ [i] : i/2 <= 5 }",
391 "{ [i] : i <= 10 }" },
392 { "{Sym=[n] [i] : i <= n }",
393 "[n] -> { [i] : i <= n }" },
394 { "{ [*] }",
395 "{ [a] }" },
396 { "{ [i] : 2*floor(i/2) = i }",
397 "{ [i] : exists a : i = 2 a }" },
398 { "{ [a] -> [b] : a = 5 implies b = 5 }",
399 "{ [a] -> [b] : a != 5 or b = 5 }" },
400 { "{ [a] -> [a - 1 : a > 0] }",
401 "{ [a] -> [a - 1] : a > 0 }" },
402 { "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
403 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }" },
404 { "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
405 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
406 { "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
407 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
408 { "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
409 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
410 { "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
411 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
412 { "{ [a,b] -> [i,j] : a,b << i,j }",
413 "{ [a,b] -> [i,j] : a < i or (a = i and b < j) }" },
414 { "{ [a,b] -> [i,j] : a,b <<= i,j }",
415 "{ [a,b] -> [i,j] : a < i or (a = i and b <= j) }" },
416 { "{ [a,b] -> [i,j] : a,b >> i,j }",
417 "{ [a,b] -> [i,j] : a > i or (a = i and b > j) }" },
418 { "{ [a,b] -> [i,j] : a,b >>= i,j }",
419 "{ [a,b] -> [i,j] : a > i or (a = i and b >= j) }" },
420 { "{ [n] -> [i] : exists (a, b, c: 8b <= i - 32a and "
421 "8b >= -7 + i - 32 a and b >= 0 and b <= 3 and "
422 "8c < n - 32a and i < n and c >= 0 and "
423 "c <= 3 and c >= -4a) }",
424 "{ [n] -> [i] : 0 <= i < n }" },
425 { "{ [x] -> [] : exists (a, b: 0 <= a <= 1 and 0 <= b <= 3 and "
426 "2b <= x - 8a and 2b >= -1 + x - 8a) }",
427 "{ [x] -> [] : 0 <= x <= 15 }" },
428 { "{ [x] -> [x] : }",
429 "{ [x] -> [x] }" },
430 { "{ [x=4:5] -> [x + 1] }",
431 "{ [x] -> [x + 1] : 4 <= x <= 5 }" },
432 { "{ [x=4:5] -> [x + 1 : x + 1] }",
433 "{ [x=4:5] -> [x + 1] }" },
434 { "{ [x] -> [x - 1 : x + 1] }",
435 "{ [x] -> [y] : x - 1 <= y <= x + 1 }" },
436 { "{ [x=4:] -> [x + 1] }",
437 "{ [x] -> [x + 1] : 4 <= x }" },
438 { "{ [x=:5] -> [x + 1] }",
439 "{ [x] -> [x + 1] : x <= 5 }" },
440 { "{ [x=:] -> [x + 1] }",
441 "{ [x] -> [x + 1] }" },
442 { "{ [:] -> [:] }",
443 "{ [x] -> [y] }" },
444 { "{ [x, x//4] }",
445 "{ [x, floor(x/4)] }" },
446 { "{ [10//4] }",
447 "{ [2] }" },
448 { "{ [-1//4] }",
449 "{ [-1] }" },
450 { "{ [0-1//4] }",
451 "{ [0] }" },
452 { "{ [- 1//4] }",
453 "{ [-1] }" },
454 { "{ [0 - 1//4] }",
455 "{ [0] }" },
456 { "{ [0--1//4] }",
457 "{ [1] }" },
458 { "{ [0 - -1//4] }",
459 "{ [1] }" },
460 { "{ [-2^2:2^2-1] }",
461 "{ [-4:3] }" },
462 { "{ [2*-2] }",
463 "{ [-4] }" },
464 { "{ [i,i*-2] }",
465 "{ [i,-2i] }" },
466 { "[a, b, c, d] -> { [max(a,b,c,d)] }",
467 "[a, b, c, d] -> { [a] : b < a and c < a and d < a; "
468 "[b] : b >= a and c < b and d < b; "
469 "[c] : c >= a and c >= b and d < c; "
470 "[d] : d >= a and d >= b and d >= c }" },
471 { "[a, b, c, d] -> { [min(a,b,c,d)] }",
472 "[a, b, c, d] -> { [a] : b >= a and c >= a and d >= a; "
473 "[b] : b < a and c >= b and d >= b; "
474 "[c] : c < b and c < a and d >= c; "
475 "[d] : d < c and d < b and d < a }" },
478 int test_parse(struct isl_ctx *ctx)
480 int i;
481 isl_map *map, *map2;
482 const char *str, *str2;
484 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
485 return -1;
486 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
487 return -1;
488 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
489 return -1;
490 if (test_parse_multi(ctx) < 0)
491 return -1;
492 if (test_parse_pma(ctx) < 0)
493 return -1;
494 if (test_parse_upma(ctx) < 0)
495 return -1;
497 str = "{ [i] -> [-i] }";
498 map = isl_map_read_from_str(ctx, str);
499 assert(map);
500 isl_map_free(map);
502 str = "{ A[i] -> L[([i/3])] }";
503 map = isl_map_read_from_str(ctx, str);
504 assert(map);
505 isl_map_free(map);
507 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
508 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
509 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
511 for (i = 0; i < ARRAY_SIZE(parse_map_equal_tests); ++i) {
512 str = parse_map_equal_tests[i].map1;
513 str2 = parse_map_equal_tests[i].map2;
514 if (test_parse_map_equal(ctx, str, str2) < 0)
515 return -1;
518 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
519 map = isl_map_read_from_str(ctx, str);
520 str = "{ [new, old] -> [o0, o1] : "
521 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
522 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
523 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
524 map2 = isl_map_read_from_str(ctx, str);
525 assert(isl_map_is_equal(map, map2));
526 isl_map_free(map);
527 isl_map_free(map2);
529 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
530 map = isl_map_read_from_str(ctx, str);
531 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
532 map2 = isl_map_read_from_str(ctx, str);
533 assert(isl_map_is_equal(map, map2));
534 isl_map_free(map);
535 isl_map_free(map2);
537 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
538 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
539 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
540 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
541 test_parse_pwaff(ctx, "{ [] -> [(100)] }");
543 return 0;
546 static int test_read(isl_ctx *ctx)
548 char *filename;
549 FILE *input;
550 isl_basic_set *bset1, *bset2;
551 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
552 int equal;
554 filename = get_filename(ctx, "set", "omega");
555 assert(filename);
556 input = fopen(filename, "r");
557 assert(input);
559 bset1 = isl_basic_set_read_from_file(ctx, input);
560 bset2 = isl_basic_set_read_from_str(ctx, str);
562 equal = isl_basic_set_is_equal(bset1, bset2);
564 isl_basic_set_free(bset1);
565 isl_basic_set_free(bset2);
566 free(filename);
568 fclose(input);
570 if (equal < 0)
571 return -1;
572 if (!equal)
573 isl_die(ctx, isl_error_unknown,
574 "read sets not equal", return -1);
576 return 0;
579 static int test_bounded(isl_ctx *ctx)
581 isl_set *set;
582 isl_bool bounded;
584 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
585 bounded = isl_set_is_bounded(set);
586 isl_set_free(set);
588 if (bounded < 0)
589 return -1;
590 if (!bounded)
591 isl_die(ctx, isl_error_unknown,
592 "set not considered bounded", return -1);
594 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
595 bounded = isl_set_is_bounded(set);
596 assert(!bounded);
597 isl_set_free(set);
599 if (bounded < 0)
600 return -1;
601 if (bounded)
602 isl_die(ctx, isl_error_unknown,
603 "set considered bounded", return -1);
605 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
606 bounded = isl_set_is_bounded(set);
607 isl_set_free(set);
609 if (bounded < 0)
610 return -1;
611 if (bounded)
612 isl_die(ctx, isl_error_unknown,
613 "set considered bounded", return -1);
615 return 0;
618 /* Construct the basic set { [i] : 5 <= i <= N } */
619 static int test_construction_1(isl_ctx *ctx)
621 isl_space *space;
622 isl_local_space *ls;
623 isl_basic_set *bset;
624 isl_constraint *c;
626 space = isl_space_set_alloc(ctx, 1, 1);
627 bset = isl_basic_set_universe(isl_space_copy(space));
628 ls = isl_local_space_from_space(space);
630 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
631 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
632 c = isl_constraint_set_coefficient_si(c, isl_dim_param, 0, 1);
633 bset = isl_basic_set_add_constraint(bset, c);
635 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
636 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
637 c = isl_constraint_set_constant_si(c, -5);
638 bset = isl_basic_set_add_constraint(bset, c);
640 isl_local_space_free(ls);
641 isl_basic_set_free(bset);
643 return 0;
646 /* Construct the basic set { [x] : -100 <= x <= 100 }
647 * using isl_basic_set_{lower,upper}_bound_val and
648 * check that it is equal the same basic set parsed from a string.
650 static int test_construction_2(isl_ctx *ctx)
652 isl_bool equal;
653 isl_val *v;
654 isl_space *space;
655 isl_basic_set *bset1, *bset2;
657 v = isl_val_int_from_si(ctx, 100);
658 space = isl_space_set_alloc(ctx, 0, 1);
659 bset1 = isl_basic_set_universe(space);
660 bset1 = isl_basic_set_upper_bound_val(bset1, isl_dim_set, 0,
661 isl_val_copy(v));
662 bset1 = isl_basic_set_lower_bound_val(bset1, isl_dim_set, 0,
663 isl_val_neg(v));
664 bset2 = isl_basic_set_read_from_str(ctx, "{ [x] : -100 <= x <= 100 }");
665 equal = isl_basic_set_is_equal(bset1, bset2);
666 isl_basic_set_free(bset1);
667 isl_basic_set_free(bset2);
669 if (equal < 0)
670 return -1;
671 if (!equal)
672 isl_die(ctx, isl_error_unknown,
673 "failed construction", return -1);
675 return 0;
678 /* Basic tests for constructing basic sets.
680 static int test_construction(isl_ctx *ctx)
682 if (test_construction_1(ctx) < 0)
683 return -1;
684 if (test_construction_2(ctx) < 0)
685 return -1;
686 return 0;
689 static int test_dim(isl_ctx *ctx)
691 const char *str;
692 isl_map *map1, *map2;
693 int equal;
695 map1 = isl_map_read_from_str(ctx,
696 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
697 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
698 map2 = isl_map_read_from_str(ctx,
699 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
700 equal = isl_map_is_equal(map1, map2);
701 isl_map_free(map2);
703 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
704 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
705 if (equal >= 0 && equal)
706 equal = isl_map_is_equal(map1, map2);
708 isl_map_free(map1);
709 isl_map_free(map2);
711 if (equal < 0)
712 return -1;
713 if (!equal)
714 isl_die(ctx, isl_error_unknown,
715 "unexpected result", return -1);
717 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
718 map1 = isl_map_read_from_str(ctx, str);
719 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
720 map2 = isl_map_read_from_str(ctx, str);
721 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
722 equal = isl_map_is_equal(map1, map2);
723 isl_map_free(map1);
724 isl_map_free(map2);
726 if (equal < 0)
727 return -1;
728 if (!equal)
729 isl_die(ctx, isl_error_unknown,
730 "unexpected result", return -1);
732 return 0;
735 #undef BASE
736 #define BASE multi_val
737 #include "isl_test_plain_equal_templ.c"
739 #undef BASE
740 #define BASE multi_aff
741 #include "isl_test_plain_equal_templ.c"
743 /* Check that "val" is equal to the value described by "str".
744 * If "str" is "NaN", then check for a NaN value explicitly.
746 static isl_stat val_check_equal(__isl_keep isl_val *val, const char *str)
748 isl_bool ok, is_nan;
749 isl_ctx *ctx;
750 isl_val *res;
752 if (!val)
753 return isl_stat_error;
755 ctx = isl_val_get_ctx(val);
756 res = isl_val_read_from_str(ctx, str);
757 is_nan = isl_val_is_nan(res);
758 if (is_nan < 0)
759 ok = isl_bool_error;
760 else if (is_nan)
761 ok = isl_val_is_nan(val);
762 else
763 ok = isl_val_eq(val, res);
764 isl_val_free(res);
765 if (ok < 0)
766 return isl_stat_error;
767 if (!ok)
768 isl_die(ctx, isl_error_unknown,
769 "unexpected result", return isl_stat_error);
770 return isl_stat_ok;
773 struct {
774 __isl_give isl_val *(*op)(__isl_take isl_val *v);
775 const char *arg;
776 const char *res;
777 } val_un_tests[] = {
778 { &isl_val_neg, "0", "0" },
779 { &isl_val_abs, "0", "0" },
780 { &isl_val_pow2, "0", "1" },
781 { &isl_val_floor, "0", "0" },
782 { &isl_val_ceil, "0", "0" },
783 { &isl_val_neg, "1", "-1" },
784 { &isl_val_neg, "-1", "1" },
785 { &isl_val_neg, "1/2", "-1/2" },
786 { &isl_val_neg, "-1/2", "1/2" },
787 { &isl_val_neg, "infty", "-infty" },
788 { &isl_val_neg, "-infty", "infty" },
789 { &isl_val_neg, "NaN", "NaN" },
790 { &isl_val_abs, "1", "1" },
791 { &isl_val_abs, "-1", "1" },
792 { &isl_val_abs, "1/2", "1/2" },
793 { &isl_val_abs, "-1/2", "1/2" },
794 { &isl_val_abs, "infty", "infty" },
795 { &isl_val_abs, "-infty", "infty" },
796 { &isl_val_abs, "NaN", "NaN" },
797 { &isl_val_floor, "1", "1" },
798 { &isl_val_floor, "-1", "-1" },
799 { &isl_val_floor, "1/2", "0" },
800 { &isl_val_floor, "-1/2", "-1" },
801 { &isl_val_floor, "infty", "infty" },
802 { &isl_val_floor, "-infty", "-infty" },
803 { &isl_val_floor, "NaN", "NaN" },
804 { &isl_val_ceil, "1", "1" },
805 { &isl_val_ceil, "-1", "-1" },
806 { &isl_val_ceil, "1/2", "1" },
807 { &isl_val_ceil, "-1/2", "0" },
808 { &isl_val_ceil, "infty", "infty" },
809 { &isl_val_ceil, "-infty", "-infty" },
810 { &isl_val_ceil, "NaN", "NaN" },
811 { &isl_val_pow2, "-3", "1/8" },
812 { &isl_val_pow2, "-1", "1/2" },
813 { &isl_val_pow2, "1", "2" },
814 { &isl_val_pow2, "2", "4" },
815 { &isl_val_pow2, "3", "8" },
816 { &isl_val_inv, "1", "1" },
817 { &isl_val_inv, "2", "1/2" },
818 { &isl_val_inv, "1/2", "2" },
819 { &isl_val_inv, "-2", "-1/2" },
820 { &isl_val_inv, "-1/2", "-2" },
821 { &isl_val_inv, "0", "NaN" },
822 { &isl_val_inv, "NaN", "NaN" },
823 { &isl_val_inv, "infty", "0" },
824 { &isl_val_inv, "-infty", "0" },
827 /* Perform some basic tests of unary operations on isl_val objects.
829 static int test_un_val(isl_ctx *ctx)
831 int i;
832 isl_val *v;
833 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
835 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
836 isl_stat r;
838 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
839 fn = val_un_tests[i].op;
840 v = fn(v);
841 r = val_check_equal(v, val_un_tests[i].res);
842 isl_val_free(v);
843 if (r < 0)
844 return -1;
847 return 0;
850 struct {
851 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
852 __isl_take isl_val *v2);
853 } val_bin_op[] = {
854 ['+'] = { &isl_val_add },
855 ['-'] = { &isl_val_sub },
856 ['*'] = { &isl_val_mul },
857 ['/'] = { &isl_val_div },
858 ['g'] = { &isl_val_gcd },
859 ['m'] = { &isl_val_min },
860 ['M'] = { &isl_val_max },
863 struct {
864 const char *arg1;
865 unsigned char op;
866 const char *arg2;
867 const char *res;
868 } val_bin_tests[] = {
869 { "0", '+', "0", "0" },
870 { "1", '+', "0", "1" },
871 { "1", '+', "1", "2" },
872 { "1", '-', "1", "0" },
873 { "1", '*', "1", "1" },
874 { "1", '/', "1", "1" },
875 { "2", '*', "3", "6" },
876 { "2", '*', "1/2", "1" },
877 { "2", '*', "1/3", "2/3" },
878 { "2/3", '*', "3/5", "2/5" },
879 { "2/3", '*', "7/5", "14/15" },
880 { "2", '/', "1/2", "4" },
881 { "-2", '/', "-1/2", "4" },
882 { "-2", '/', "1/2", "-4" },
883 { "2", '/', "-1/2", "-4" },
884 { "2", '/', "2", "1" },
885 { "2", '/', "3", "2/3" },
886 { "2/3", '/', "5/3", "2/5" },
887 { "2/3", '/', "5/7", "14/15" },
888 { "0", '/', "0", "NaN" },
889 { "42", '/', "0", "NaN" },
890 { "-42", '/', "0", "NaN" },
891 { "infty", '/', "0", "NaN" },
892 { "-infty", '/', "0", "NaN" },
893 { "NaN", '/', "0", "NaN" },
894 { "0", '/', "NaN", "NaN" },
895 { "42", '/', "NaN", "NaN" },
896 { "-42", '/', "NaN", "NaN" },
897 { "infty", '/', "NaN", "NaN" },
898 { "-infty", '/', "NaN", "NaN" },
899 { "NaN", '/', "NaN", "NaN" },
900 { "0", '/', "infty", "0" },
901 { "42", '/', "infty", "0" },
902 { "-42", '/', "infty", "0" },
903 { "infty", '/', "infty", "NaN" },
904 { "-infty", '/', "infty", "NaN" },
905 { "NaN", '/', "infty", "NaN" },
906 { "0", '/', "-infty", "0" },
907 { "42", '/', "-infty", "0" },
908 { "-42", '/', "-infty", "0" },
909 { "infty", '/', "-infty", "NaN" },
910 { "-infty", '/', "-infty", "NaN" },
911 { "NaN", '/', "-infty", "NaN" },
912 { "1", '-', "1/3", "2/3" },
913 { "1/3", '+', "1/2", "5/6" },
914 { "1/2", '+', "1/2", "1" },
915 { "3/4", '-', "1/4", "1/2" },
916 { "1/2", '-', "1/3", "1/6" },
917 { "infty", '+', "42", "infty" },
918 { "infty", '+', "infty", "infty" },
919 { "42", '+', "infty", "infty" },
920 { "infty", '-', "infty", "NaN" },
921 { "infty", '*', "infty", "infty" },
922 { "infty", '*', "-infty", "-infty" },
923 { "-infty", '*', "infty", "-infty" },
924 { "-infty", '*', "-infty", "infty" },
925 { "0", '*', "infty", "NaN" },
926 { "1", '*', "infty", "infty" },
927 { "infty", '*', "0", "NaN" },
928 { "infty", '*', "42", "infty" },
929 { "42", '-', "infty", "-infty" },
930 { "infty", '+', "-infty", "NaN" },
931 { "4", 'g', "6", "2" },
932 { "5", 'g', "6", "1" },
933 { "42", 'm', "3", "3" },
934 { "42", 'M', "3", "42" },
935 { "3", 'm', "42", "3" },
936 { "3", 'M', "42", "42" },
937 { "42", 'm', "infty", "42" },
938 { "42", 'M', "infty", "infty" },
939 { "42", 'm', "-infty", "-infty" },
940 { "42", 'M', "-infty", "42" },
941 { "42", 'm', "NaN", "NaN" },
942 { "42", 'M', "NaN", "NaN" },
943 { "infty", 'm', "-infty", "-infty" },
944 { "infty", 'M', "-infty", "infty" },
947 /* Perform some basic tests of binary operations on isl_val objects.
949 static int test_bin_val(isl_ctx *ctx)
951 int i;
952 isl_val *v1, *v2, *res;
953 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
954 __isl_take isl_val *v2);
955 int ok;
957 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
958 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
959 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
960 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
961 fn = val_bin_op[val_bin_tests[i].op].fn;
962 v1 = fn(v1, v2);
963 if (isl_val_is_nan(res))
964 ok = isl_val_is_nan(v1);
965 else
966 ok = isl_val_eq(v1, res);
967 isl_val_free(v1);
968 isl_val_free(res);
969 if (ok < 0)
970 return -1;
971 if (!ok)
972 isl_die(ctx, isl_error_unknown,
973 "unexpected result", return -1);
976 return 0;
979 /* Perform some basic tests on isl_val objects.
981 static int test_val(isl_ctx *ctx)
983 if (test_un_val(ctx) < 0)
984 return -1;
985 if (test_bin_val(ctx) < 0)
986 return -1;
987 return 0;
990 /* Sets described using existentially quantified variables that
991 * can also be described without.
993 static const char *elimination_tests[] = {
994 "{ [i,j] : 2 * [i/2] + 3 * [j/4] <= 10 and 2 i = j }",
995 "{ [m, w] : exists a : w - 2m - 5 <= 3a <= m - 2w }",
996 "{ [m, w] : exists a : w >= 0 and a < m and -1 + w <= a <= 2m - w }",
999 /* Check that redundant existentially quantified variables are
1000 * getting removed.
1002 static int test_elimination(isl_ctx *ctx)
1004 int i;
1005 isl_size n;
1006 isl_basic_set *bset;
1008 for (i = 0; i < ARRAY_SIZE(elimination_tests); ++i) {
1009 bset = isl_basic_set_read_from_str(ctx, elimination_tests[i]);
1010 n = isl_basic_set_dim(bset, isl_dim_div);
1011 isl_basic_set_free(bset);
1012 if (n < 0)
1013 return -1;
1014 if (n != 0)
1015 isl_die(ctx, isl_error_unknown,
1016 "expecting no existentials", return -1);
1019 return 0;
1022 static int test_div(isl_ctx *ctx)
1024 const char *str;
1025 int empty;
1026 isl_space *space;
1027 isl_set *set;
1028 isl_local_space *ls;
1029 struct isl_basic_set *bset;
1030 struct isl_constraint *c;
1032 /* test 1 */
1033 space = isl_space_set_alloc(ctx, 0, 3);
1034 bset = isl_basic_set_universe(isl_space_copy(space));
1035 ls = isl_local_space_from_space(space);
1037 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1038 c = isl_constraint_set_constant_si(c, -1);
1039 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1040 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1041 bset = isl_basic_set_add_constraint(bset, c);
1043 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1044 c = isl_constraint_set_constant_si(c, 1);
1045 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1046 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
1047 bset = isl_basic_set_add_constraint(bset, c);
1049 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1051 assert(bset && bset->n_div == 1);
1052 isl_local_space_free(ls);
1053 isl_basic_set_free(bset);
1055 /* test 2 */
1056 space = isl_space_set_alloc(ctx, 0, 3);
1057 bset = isl_basic_set_universe(isl_space_copy(space));
1058 ls = isl_local_space_from_space(space);
1060 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1061 c = isl_constraint_set_constant_si(c, 1);
1062 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1063 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1064 bset = isl_basic_set_add_constraint(bset, c);
1066 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1067 c = isl_constraint_set_constant_si(c, -1);
1068 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1069 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
1070 bset = isl_basic_set_add_constraint(bset, c);
1072 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1074 assert(bset && bset->n_div == 1);
1075 isl_local_space_free(ls);
1076 isl_basic_set_free(bset);
1078 /* test 3 */
1079 space = isl_space_set_alloc(ctx, 0, 3);
1080 bset = isl_basic_set_universe(isl_space_copy(space));
1081 ls = isl_local_space_from_space(space);
1083 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1084 c = isl_constraint_set_constant_si(c, 1);
1085 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1086 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1087 bset = isl_basic_set_add_constraint(bset, c);
1089 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1090 c = isl_constraint_set_constant_si(c, -3);
1091 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1092 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 4);
1093 bset = isl_basic_set_add_constraint(bset, c);
1095 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1097 assert(bset && bset->n_div == 1);
1098 isl_local_space_free(ls);
1099 isl_basic_set_free(bset);
1101 /* test 4 */
1102 space = isl_space_set_alloc(ctx, 0, 3);
1103 bset = isl_basic_set_universe(isl_space_copy(space));
1104 ls = isl_local_space_from_space(space);
1106 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1107 c = isl_constraint_set_constant_si(c, 2);
1108 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1109 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1110 bset = isl_basic_set_add_constraint(bset, c);
1112 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1113 c = isl_constraint_set_constant_si(c, -1);
1114 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1115 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
1116 bset = isl_basic_set_add_constraint(bset, c);
1118 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1120 assert(isl_basic_set_is_empty(bset));
1121 isl_local_space_free(ls);
1122 isl_basic_set_free(bset);
1124 /* test 5 */
1125 space = isl_space_set_alloc(ctx, 0, 3);
1126 bset = isl_basic_set_universe(isl_space_copy(space));
1127 ls = isl_local_space_from_space(space);
1129 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1130 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1131 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
1132 bset = isl_basic_set_add_constraint(bset, c);
1134 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1135 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1136 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1137 bset = isl_basic_set_add_constraint(bset, c);
1139 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1141 assert(bset && bset->n_div == 0);
1142 isl_basic_set_free(bset);
1143 isl_local_space_free(ls);
1145 /* test 6 */
1146 space = isl_space_set_alloc(ctx, 0, 3);
1147 bset = isl_basic_set_universe(isl_space_copy(space));
1148 ls = isl_local_space_from_space(space);
1150 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1151 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1152 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
1153 bset = isl_basic_set_add_constraint(bset, c);
1155 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1156 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1157 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1158 bset = isl_basic_set_add_constraint(bset, c);
1160 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1162 assert(bset && bset->n_div == 1);
1163 isl_basic_set_free(bset);
1164 isl_local_space_free(ls);
1166 /* test 7 */
1167 /* This test is a bit tricky. We set up an equality
1168 * a + 3b + 3c = 6 e0
1169 * Normalization of divs creates _two_ divs
1170 * a = 3 e0
1171 * c - b - e0 = 2 e1
1172 * Afterwards e0 is removed again because it has coefficient -1
1173 * and we end up with the original equality and div again.
1174 * Perhaps we can avoid the introduction of this temporary div.
1176 space = isl_space_set_alloc(ctx, 0, 4);
1177 bset = isl_basic_set_universe(isl_space_copy(space));
1178 ls = isl_local_space_from_space(space);
1180 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1181 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1182 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1183 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -3);
1184 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 6);
1185 bset = isl_basic_set_add_constraint(bset, c);
1187 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
1189 /* Test disabled for now */
1191 assert(bset && bset->n_div == 1);
1193 isl_local_space_free(ls);
1194 isl_basic_set_free(bset);
1196 /* test 8 */
1197 space = isl_space_set_alloc(ctx, 0, 5);
1198 bset = isl_basic_set_universe(isl_space_copy(space));
1199 ls = isl_local_space_from_space(space);
1201 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1202 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1203 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1204 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, -3);
1205 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 4, 6);
1206 bset = isl_basic_set_add_constraint(bset, c);
1208 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1209 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1210 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 1);
1211 c = isl_constraint_set_constant_si(c, 1);
1212 bset = isl_basic_set_add_constraint(bset, c);
1214 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
1216 /* Test disabled for now */
1218 assert(bset && bset->n_div == 1);
1220 isl_local_space_free(ls);
1221 isl_basic_set_free(bset);
1223 /* test 9 */
1224 space = isl_space_set_alloc(ctx, 0, 4);
1225 bset = isl_basic_set_universe(isl_space_copy(space));
1226 ls = isl_local_space_from_space(space);
1228 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1229 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1230 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -1);
1231 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1232 bset = isl_basic_set_add_constraint(bset, c);
1234 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1235 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1236 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 3);
1237 c = isl_constraint_set_constant_si(c, 2);
1238 bset = isl_basic_set_add_constraint(bset, c);
1240 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
1242 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1244 assert(!isl_basic_set_is_empty(bset));
1246 isl_local_space_free(ls);
1247 isl_basic_set_free(bset);
1249 /* test 10 */
1250 space = isl_space_set_alloc(ctx, 0, 3);
1251 bset = isl_basic_set_universe(isl_space_copy(space));
1252 ls = isl_local_space_from_space(space);
1254 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1255 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1256 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1257 bset = isl_basic_set_add_constraint(bset, c);
1259 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1261 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1263 isl_local_space_free(ls);
1264 isl_basic_set_free(bset);
1266 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1267 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
1268 set = isl_set_read_from_str(ctx, str);
1269 set = isl_set_compute_divs(set);
1270 isl_set_free(set);
1271 if (!set)
1272 return -1;
1274 if (test_elimination(ctx) < 0)
1275 return -1;
1277 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
1278 set = isl_set_read_from_str(ctx, str);
1279 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
1280 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
1281 empty = isl_set_is_empty(set);
1282 isl_set_free(set);
1283 if (empty < 0)
1284 return -1;
1285 if (!empty)
1286 isl_die(ctx, isl_error_unknown,
1287 "result not as accurate as expected", return -1);
1289 return 0;
1292 void test_application_case(struct isl_ctx *ctx, const char *name)
1294 char *filename;
1295 FILE *input;
1296 struct isl_basic_set *bset1, *bset2;
1297 struct isl_basic_map *bmap;
1299 filename = get_filename(ctx, name, "omega");
1300 assert(filename);
1301 input = fopen(filename, "r");
1302 assert(input);
1304 bset1 = isl_basic_set_read_from_file(ctx, input);
1305 bmap = isl_basic_map_read_from_file(ctx, input);
1307 bset1 = isl_basic_set_apply(bset1, bmap);
1309 bset2 = isl_basic_set_read_from_file(ctx, input);
1311 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1313 isl_basic_set_free(bset1);
1314 isl_basic_set_free(bset2);
1315 free(filename);
1317 fclose(input);
1320 static int test_application(isl_ctx *ctx)
1322 test_application_case(ctx, "application");
1323 test_application_case(ctx, "application2");
1325 return 0;
1328 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1330 char *filename;
1331 FILE *input;
1332 struct isl_basic_set *bset1, *bset2;
1334 filename = get_filename(ctx, name, "polylib");
1335 assert(filename);
1336 input = fopen(filename, "r");
1337 assert(input);
1339 bset1 = isl_basic_set_read_from_file(ctx, input);
1340 bset2 = isl_basic_set_read_from_file(ctx, input);
1342 bset1 = isl_basic_set_affine_hull(bset1);
1344 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1346 isl_basic_set_free(bset1);
1347 isl_basic_set_free(bset2);
1348 free(filename);
1350 fclose(input);
1353 /* Pairs of sets and the corresponding expected results of
1354 * isl_basic_set_recession_cone.
1356 struct {
1357 const char *set;
1358 const char *cone;
1359 } recession_cone_tests[] = {
1360 { "{ [i] : 0 <= i <= 10 }", "{ [0] }" },
1361 { "{ [i] : 0 <= i }", "{ [i] : 0 <= i }" },
1362 { "{ [i] : i <= 10 }", "{ [i] : i <= 0 }" },
1363 { "{ [i] : false }", "{ [i] : false }" },
1366 /* Perform some basic isl_basic_set_recession_cone tests.
1368 static int test_recession_cone(struct isl_ctx *ctx)
1370 int i;
1372 for (i = 0; i < ARRAY_SIZE(recession_cone_tests); ++i) {
1373 const char *str;
1374 isl_basic_set *bset;
1375 isl_basic_set *cone, *expected;
1376 isl_bool equal;
1378 str = recession_cone_tests[i].set;
1379 bset = isl_basic_set_read_from_str(ctx, str);
1380 str = recession_cone_tests[i].cone;
1381 expected = isl_basic_set_read_from_str(ctx, str);
1382 cone = isl_basic_set_recession_cone(bset);
1383 equal = isl_basic_set_is_equal(cone, expected);
1384 isl_basic_set_free(cone);
1385 isl_basic_set_free(expected);
1386 if (equal < 0)
1387 return -1;
1388 if (!equal)
1389 isl_die(ctx, isl_error_unknown, "unexpected cone",
1390 return -1);
1393 return 0;
1396 int test_affine_hull(struct isl_ctx *ctx)
1398 const char *str;
1399 isl_set *set;
1400 isl_basic_set *bset, *bset2;
1401 isl_size n;
1402 isl_bool subset;
1404 test_affine_hull_case(ctx, "affine2");
1405 test_affine_hull_case(ctx, "affine");
1406 test_affine_hull_case(ctx, "affine3");
1408 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1409 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1410 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1411 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1412 set = isl_set_read_from_str(ctx, str);
1413 bset = isl_set_affine_hull(set);
1414 n = isl_basic_set_dim(bset, isl_dim_div);
1415 isl_basic_set_free(bset);
1416 if (n < 0)
1417 return -1;
1418 if (n != 0)
1419 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1420 return -1);
1422 /* Check that isl_map_affine_hull is not confused by
1423 * the reordering of divs in isl_map_align_divs.
1425 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1426 "32e0 = b and 32e1 = c); "
1427 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1428 set = isl_set_read_from_str(ctx, str);
1429 bset = isl_set_affine_hull(set);
1430 isl_basic_set_free(bset);
1431 if (!bset)
1432 return -1;
1434 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1435 "32e2 = 31 + 31e0 }";
1436 set = isl_set_read_from_str(ctx, str);
1437 bset = isl_set_affine_hull(set);
1438 str = "{ [a] : exists e : a = 32 e }";
1439 bset2 = isl_basic_set_read_from_str(ctx, str);
1440 subset = isl_basic_set_is_subset(bset, bset2);
1441 isl_basic_set_free(bset);
1442 isl_basic_set_free(bset2);
1443 if (subset < 0)
1444 return -1;
1445 if (!subset)
1446 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1447 return -1);
1449 return 0;
1452 /* Test a special case of isl_set_plain_unshifted_simple_hull
1453 * where older versions of isl would include a redundant constraint
1454 * in the result.
1455 * Check that the result does not have any constraints.
1457 static isl_stat test_plain_unshifted_simple_hull_special(isl_ctx *ctx)
1459 const char *str;
1460 isl_bool is_universe;
1461 isl_set *set;
1462 isl_basic_set *bset;
1464 str = "{[x, y] : x = 0 or 2*((x+y)//2) <= y + 2 }";
1465 set = isl_set_read_from_str(ctx, str);
1466 bset = isl_set_plain_unshifted_simple_hull(set);
1467 is_universe = isl_basic_set_plain_is_universe(bset);
1468 isl_basic_set_free(bset);
1470 if (is_universe < 0)
1471 return isl_stat_error;
1472 if (!is_universe)
1473 isl_die(ctx, isl_error_unknown,
1474 "hull should not have any constraints",
1475 return isl_stat_error);
1477 return isl_stat_ok;
1480 /* Inputs for simple hull tests, consisting of
1481 * the specific simple hull function, the input set and the expected result.
1483 struct {
1484 __isl_give isl_basic_set *(*fn)(__isl_take isl_set *set);
1485 const char *set;
1486 const char *hull;
1487 } simple_hull_tests[] = {
1488 { &isl_set_plain_unshifted_simple_hull,
1489 "{ [i,j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1490 "{ [i,j] : i >= 1 }" },
1491 { &isl_set_plain_unshifted_simple_hull,
1492 "{ [n,i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1493 "(j mod 4 = 2 and k mod 6 = n) }",
1494 "{ [n,i,j,k] : j mod 4 = 2 }" },
1495 { &isl_set_unshifted_simple_hull,
1496 "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1497 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1498 { &isl_set_simple_hull,
1499 "{ [a, b] : b <= 0 and "
1500 "2*floor((-2*floor((b)/2))/5) >= a - floor((b)/2); "
1501 "[a, b] : a mod 2 = 0 }",
1502 "{ [a, b] }" },
1505 /* Basic tests for various simple hull functions.
1507 static int test_various_simple_hull(isl_ctx *ctx)
1509 int i;
1510 isl_set *set;
1511 isl_basic_set *hull, *expected;
1512 isl_bool equal;
1514 for (i = 0; i < ARRAY_SIZE(simple_hull_tests); ++i) {
1515 const char *str;
1516 str = simple_hull_tests[i].set;
1517 set = isl_set_read_from_str(ctx, str);
1518 str = simple_hull_tests[i].hull;
1519 expected = isl_basic_set_read_from_str(ctx, str);
1520 hull = simple_hull_tests[i].fn(set);
1521 equal = isl_basic_set_is_equal(hull, expected);
1522 isl_basic_set_free(hull);
1523 isl_basic_set_free(expected);
1524 if (equal < 0)
1525 return -1;
1526 if (!equal)
1527 isl_die(ctx, isl_error_unknown, "unexpected hull",
1528 return -1);
1531 return 0;
1534 static int test_simple_hull(struct isl_ctx *ctx)
1536 const char *str;
1537 isl_set *set;
1538 isl_basic_set *bset;
1539 isl_bool is_empty;
1541 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1542 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1543 set = isl_set_read_from_str(ctx, str);
1544 bset = isl_set_simple_hull(set);
1545 is_empty = isl_basic_set_is_empty(bset);
1546 isl_basic_set_free(bset);
1548 if (is_empty == isl_bool_error)
1549 return -1;
1551 if (is_empty == isl_bool_false)
1552 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1553 return -1);
1555 if (test_plain_unshifted_simple_hull_special(ctx) < 0)
1556 return -1;
1557 if (test_various_simple_hull(ctx) < 0)
1558 return -1;
1560 return 0;
1563 /* Inputs for isl_set_get_simple_fixed_box_hull tests.
1564 * "set" is the input set.
1565 * "offset" is the expected box offset.
1566 * "size" is the expected box size.
1568 static struct {
1569 const char *set;
1570 const char *offset;
1571 const char *size;
1572 } box_hull_tests[] = {
1573 { "{ S[x, y] : 0 <= x, y < 10 }", "{ S[0, 0] }", "{ S[10, 10] }" },
1574 { "[N] -> { S[x, y] : N <= x, y < N + 10 }",
1575 "[N] -> { S[N, N] }", "{ S[10, 10] }" },
1576 { "{ S[x, y] : 0 <= x + y, x - y < 10 }",
1577 "{ S[0, -4] }", "{ S[10, 9] }" },
1578 { "{ [i=0:10] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1579 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }",
1580 "{ [3] }", "{ [8] }" },
1581 { "[N] -> { [w = 0:17] : exists (e0: w < 2N and "
1582 "-1 + w <= e0 <= w and 2e0 >= N + w and w <= 2e0 <= 15 + w) }",
1583 "[N] -> { [N] }", "{ [9] }" },
1586 /* Perform basic isl_set_get_simple_fixed_box_hull tests.
1588 static int test_box_hull(struct isl_ctx *ctx)
1590 int i;
1592 for (i = 0; i < ARRAY_SIZE(box_hull_tests); ++i) {
1593 const char *str;
1594 isl_stat r;
1595 isl_set *set;
1596 isl_multi_aff *offset;
1597 isl_multi_val *size;
1598 isl_fixed_box *box;
1600 set = isl_set_read_from_str(ctx, box_hull_tests[i].set);
1601 box = isl_set_get_simple_fixed_box_hull(set);
1602 offset = isl_fixed_box_get_offset(box);
1603 size = isl_fixed_box_get_size(box);
1604 str = box_hull_tests[i].offset;
1605 r = multi_aff_check_plain_equal(offset, str);
1606 str = box_hull_tests[i].size;
1607 if (r >= 0)
1608 r = multi_val_check_plain_equal(size, str);
1609 isl_multi_aff_free(offset);
1610 isl_multi_val_free(size);
1611 isl_fixed_box_free(box);
1612 isl_set_free(set);
1613 if (r < 0)
1614 return -1;
1617 return 0;
1620 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1622 char *filename;
1623 FILE *input;
1624 struct isl_basic_set *bset1, *bset2;
1625 struct isl_set *set;
1627 filename = get_filename(ctx, name, "polylib");
1628 assert(filename);
1629 input = fopen(filename, "r");
1630 assert(input);
1632 bset1 = isl_basic_set_read_from_file(ctx, input);
1633 bset2 = isl_basic_set_read_from_file(ctx, input);
1635 set = isl_basic_set_union(bset1, bset2);
1636 bset1 = isl_set_convex_hull(set);
1638 bset2 = isl_basic_set_read_from_file(ctx, input);
1640 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1642 isl_basic_set_free(bset1);
1643 isl_basic_set_free(bset2);
1644 free(filename);
1646 fclose(input);
1649 struct {
1650 const char *set;
1651 const char *hull;
1652 } convex_hull_tests[] = {
1653 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1654 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1655 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1656 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1657 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1658 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1659 "i2 <= 5 and i2 >= 4; "
1660 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1661 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1662 "i2 <= 5 + i0 and i2 >= i0 }" },
1663 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1664 "{ [x, y] : 1 = 0 }" },
1665 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1666 "[x, y, 0] : x >= 0 and y < 0 }",
1667 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1668 { "{ [a, b, c] : a <= 1 and -a < b <= 1 and 0 <= c <= 2 - a - b and "
1669 "c <= a; "
1670 "[0, 2, 0]; [3, 1, 0] }",
1671 "{ [a, b, c] : b > -a and 2b >= -1 + a and 0 <= c <= a and "
1672 "5c <= 6 - a - 3b }" },
1675 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1677 int i;
1678 int orig_convex = ctx->opt->convex;
1679 ctx->opt->convex = convex;
1681 test_convex_hull_case(ctx, "convex0");
1682 test_convex_hull_case(ctx, "convex1");
1683 test_convex_hull_case(ctx, "convex2");
1684 test_convex_hull_case(ctx, "convex3");
1685 test_convex_hull_case(ctx, "convex4");
1686 test_convex_hull_case(ctx, "convex5");
1687 test_convex_hull_case(ctx, "convex6");
1688 test_convex_hull_case(ctx, "convex7");
1689 test_convex_hull_case(ctx, "convex8");
1690 test_convex_hull_case(ctx, "convex9");
1691 test_convex_hull_case(ctx, "convex10");
1692 test_convex_hull_case(ctx, "convex11");
1693 test_convex_hull_case(ctx, "convex12");
1694 test_convex_hull_case(ctx, "convex13");
1695 test_convex_hull_case(ctx, "convex14");
1696 test_convex_hull_case(ctx, "convex15");
1698 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1699 isl_set *set1, *set2;
1700 int equal;
1702 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1703 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1704 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1705 equal = isl_set_is_equal(set1, set2);
1706 isl_set_free(set1);
1707 isl_set_free(set2);
1709 if (equal < 0)
1710 return -1;
1711 if (!equal)
1712 isl_die(ctx, isl_error_unknown,
1713 "unexpected convex hull", return -1);
1716 ctx->opt->convex = orig_convex;
1718 return 0;
1721 static int test_convex_hull(isl_ctx *ctx)
1723 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1724 return -1;
1725 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1726 return -1;
1727 return 0;
1730 /* Check that computing the gist of "map" with respect to "context"
1731 * does not make any copy of "map" get marked empty.
1732 * Earlier versions of isl would end up doing that.
1734 static isl_stat test_gist_empty_pair(isl_ctx *ctx, const char *map,
1735 const char *context)
1737 isl_map *m1, *m2, *m3;
1738 isl_bool empty_before, empty_after;
1740 m1 = isl_map_read_from_str(ctx, map);
1741 m2 = isl_map_read_from_str(ctx, context);
1742 m3 = isl_map_copy(m1);
1743 empty_before = isl_map_is_empty(m3);
1744 m1 = isl_map_gist(m1, m2);
1745 empty_after = isl_map_is_empty(m3);
1746 isl_map_free(m1);
1747 isl_map_free(m3);
1749 if (empty_before < 0 || empty_after < 0)
1750 return isl_stat_error;
1751 if (empty_before)
1752 isl_die(ctx, isl_error_unknown, "map should not be empty",
1753 return isl_stat_error);
1754 if (empty_after)
1755 isl_die(ctx, isl_error_unknown, "map should still not be empty",
1756 return isl_stat_error);
1758 return isl_stat_ok;
1761 /* Check that computing a gist does not make any copy of the input
1762 * get marked empty.
1763 * Earlier versions of isl would end up doing that on some pairs of inputs.
1765 static isl_stat test_gist_empty(isl_ctx *ctx)
1767 const char *map, *context;
1769 map = "{ [] -> [a, b, c] : 2b = 1 + a }";
1770 context = "{ [] -> [a, b, c] : 2c = 2 + a }";
1771 if (test_gist_empty_pair(ctx, map, context) < 0)
1772 return isl_stat_error;
1773 map = "{ [] -> [0, 0] }";
1774 context = "{ [] -> [a, b] : a > b }";
1775 if (test_gist_empty_pair(ctx, map, context) < 0)
1776 return isl_stat_error;
1778 return isl_stat_ok;
1781 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1783 struct {
1784 const char *map;
1785 const char *context;
1786 const char *gist;
1787 } plain_gist_tests[] = {
1788 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1789 "{ [i] -> [j] : i >= 1 }",
1790 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1791 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1792 "(j mod 4 = 2 and k mod 6 = n) }",
1793 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1794 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1795 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1796 "{ [i] -> [j] : i > j }",
1797 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1800 /* Basic tests for isl_map_plain_gist_basic_map.
1802 static int test_plain_gist(isl_ctx *ctx)
1804 int i;
1806 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1807 const char *str;
1808 int equal;
1809 isl_map *map, *gist;
1810 isl_basic_map *context;
1812 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1813 str = plain_gist_tests[i].context;
1814 context = isl_basic_map_read_from_str(ctx, str);
1815 map = isl_map_plain_gist_basic_map(map, context);
1816 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1817 equal = isl_map_is_equal(map, gist);
1818 isl_map_free(map);
1819 isl_map_free(gist);
1820 if (equal < 0)
1821 return -1;
1822 if (!equal)
1823 isl_die(ctx, isl_error_unknown,
1824 "incorrect gist result", return -1);
1827 return 0;
1830 /* Inputs for isl_basic_set_gist tests that are expected to fail.
1832 struct {
1833 const char *set;
1834 const char *context;
1835 } gist_fail_tests[] = {
1836 { "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1837 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }",
1838 "{ [i] : i >= 0 }" },
1841 /* Check that isl_basic_set_gist fails (gracefully) when expected.
1842 * In particular, the user should be able to recover from the failure.
1844 static isl_stat test_gist_fail(struct isl_ctx *ctx)
1846 int i, n;
1847 int on_error;
1849 on_error = isl_options_get_on_error(ctx);
1850 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
1851 n = ARRAY_SIZE(gist_fail_tests);
1852 for (i = 0; i < n; ++i) {
1853 const char *str;
1854 isl_basic_set *bset, *context;
1856 bset = isl_basic_set_read_from_str(ctx, gist_fail_tests[i].set);
1857 str = gist_fail_tests[i].context;
1858 context = isl_basic_set_read_from_str(ctx, str);
1859 bset = isl_basic_set_gist(bset, context);
1860 isl_basic_set_free(bset);
1861 if (bset)
1862 break;
1864 isl_options_set_on_error(ctx, on_error);
1865 if (i < n)
1866 isl_die(ctx, isl_error_unknown,
1867 "operation not expected to succeed",
1868 return isl_stat_error);
1870 return isl_stat_ok;
1873 struct {
1874 const char *set;
1875 const char *context;
1876 const char *gist;
1877 } gist_tests[] = {
1878 { "{ [1, -1, 3] }",
1879 "{ [1, b, 2 - b] : -1 <= b <= 2 }",
1880 "{ [a, -1, c] }" },
1881 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1882 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1883 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1884 "{ [a, b, c] : a <= 15 }" },
1885 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1886 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1887 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1888 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1889 { "{ [m, n, a, b] : a <= 2147 + n }",
1890 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1891 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1892 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1893 "b >= 0) }",
1894 "{ [m, n, ku, kl] }" },
1895 { "{ [a, a, b] : a >= 10 }",
1896 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1897 "{ [a, a, b] : a >= 10 }" },
1898 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1899 "{ [0, j] : j >= 0 }" },
1900 /* Check that no constraints on i6 are introduced in the gist */
1901 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1902 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1903 "5e0 <= 381 - t1 and i4 <= 1) }",
1904 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1905 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1906 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1907 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1908 "20e0 >= 1511 - 4t1 - 5i4) }" },
1909 /* Check that no constraints on i6 are introduced in the gist */
1910 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1911 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1912 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1913 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1914 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1915 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1916 "20e1 <= 1530 - 4t1 - 5i4 and "
1917 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1918 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1919 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1920 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1921 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1922 "10e0 <= -91 + 5i4 + 4i6 and "
1923 "10e0 >= -105 + 5i4 + 4i6) }",
1924 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1925 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1926 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1927 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1928 "{ [a, b, q, p] : b >= 1 + a }",
1929 "{ [a, b, q, p] : false }" },
1930 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1931 "[n] -> { [x] : x mod 32 = 0 }",
1932 "[n] -> { [x = n] }" },
1933 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1934 "{ [x] : x mod 2 = 0 }" },
1935 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1936 "{ [x] : x mod 128 = 0 }" },
1937 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1938 "{ [x] : x mod 3200 = 0 }" },
1939 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1940 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1941 "{ [a, b, c = a] }" },
1942 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1943 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1944 "{ [a, b, c = a] : a mod 3 = 0 }" },
1945 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1946 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1947 "{ [x] }" },
1948 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1949 "{ [x,y] : 1 <= y <= 3 }",
1950 "{ [x,y] }" },
1953 /* Check that isl_set_gist behaves as expected.
1955 * For the test cases in gist_tests, besides checking that the result
1956 * is as expected, also check that applying the gist operation does
1957 * not modify the input set (an earlier version of isl would do that) and
1958 * that the test case is consistent, i.e., that the gist has the same
1959 * intersection with the context as the input set.
1961 static int test_gist(struct isl_ctx *ctx)
1963 int i;
1964 const char *str;
1965 isl_basic_set *bset1, *bset2;
1966 isl_map *map1, *map2;
1967 isl_bool equal;
1968 isl_size n_div;
1970 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1971 isl_bool equal_input, equal_intersection;
1972 isl_set *set1, *set2, *copy, *context;
1974 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1975 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1976 copy = isl_set_copy(set1);
1977 set1 = isl_set_gist(set1, isl_set_copy(context));
1978 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1979 equal = isl_set_is_equal(set1, set2);
1980 isl_set_free(set1);
1981 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1982 equal_input = isl_set_is_equal(set1, copy);
1983 isl_set_free(copy);
1984 set1 = isl_set_intersect(set1, isl_set_copy(context));
1985 set2 = isl_set_intersect(set2, context);
1986 equal_intersection = isl_set_is_equal(set1, set2);
1987 isl_set_free(set2);
1988 isl_set_free(set1);
1989 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1990 return -1;
1991 if (!equal)
1992 isl_die(ctx, isl_error_unknown,
1993 "incorrect gist result", return -1);
1994 if (!equal_input)
1995 isl_die(ctx, isl_error_unknown,
1996 "gist modified input", return -1);
1997 if (!equal_input)
1998 isl_die(ctx, isl_error_unknown,
1999 "inconsistent gist test case", return -1);
2002 if (test_gist_fail(ctx) < 0)
2003 return -1;
2005 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
2006 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
2007 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
2008 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
2009 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
2010 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
2011 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
2012 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
2013 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
2014 bset1 = isl_basic_set_read_from_str(ctx, str);
2015 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
2016 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
2017 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
2018 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
2019 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
2020 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
2021 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
2022 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
2023 bset2 = isl_basic_set_read_from_str(ctx, str);
2024 bset1 = isl_basic_set_gist(bset1, bset2);
2025 assert(bset1 && bset1->n_div == 0);
2026 isl_basic_set_free(bset1);
2028 /* Check that the integer divisions of the second disjunct
2029 * do not spread to the first disjunct.
2031 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
2032 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
2033 "(exists (e0 = [(-1 + t1)/16], "
2034 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
2035 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
2036 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
2037 "o0 <= 4294967295 and t1 <= -1)) }";
2038 map1 = isl_map_read_from_str(ctx, str);
2039 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
2040 map2 = isl_map_read_from_str(ctx, str);
2041 map1 = isl_map_gist(map1, map2);
2042 if (!map1)
2043 return -1;
2044 if (map1->n != 1)
2045 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
2046 isl_map_free(map1); return -1);
2047 n_div = isl_basic_map_dim(map1->p[0], isl_dim_div);
2048 isl_map_free(map1);
2049 if (n_div < 0)
2050 return -1;
2051 if (n_div != 1)
2052 isl_die(ctx, isl_error_unknown, "expecting single div",
2053 return -1);
2055 if (test_gist_empty(ctx) < 0)
2056 return -1;
2057 if (test_plain_gist(ctx) < 0)
2058 return -1;
2060 return 0;
2063 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
2065 isl_set *set, *set2;
2066 int equal;
2067 int one;
2069 set = isl_set_read_from_str(ctx, str);
2070 set = isl_set_coalesce(set);
2071 set2 = isl_set_read_from_str(ctx, str);
2072 equal = isl_set_is_equal(set, set2);
2073 one = set && set->n == 1;
2074 isl_set_free(set);
2075 isl_set_free(set2);
2077 if (equal < 0)
2078 return -1;
2079 if (!equal)
2080 isl_die(ctx, isl_error_unknown,
2081 "coalesced set not equal to input", return -1);
2082 if (check_one && !one)
2083 isl_die(ctx, isl_error_unknown,
2084 "coalesced set should not be a union", return -1);
2086 return 0;
2089 /* Inputs for coalescing tests with unbounded wrapping.
2090 * "str" is a string representation of the input set.
2091 * "single_disjunct" is set if we expect the result to consist of
2092 * a single disjunct.
2094 struct {
2095 int single_disjunct;
2096 const char *str;
2097 } coalesce_unbounded_tests[] = {
2098 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
2099 "-x - y + 1 >= 0 and -3 <= z <= 3;"
2100 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
2101 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
2102 "-10 <= y <= 0}" },
2103 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
2104 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
2105 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
2106 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
2107 { 0, "{ [x, y, z] : 0 <= x,y,z <= 100 and 0 < z <= 2 + 2x + 2y; "
2108 "[x, y, 0] : x,y <= 100 and y <= 9 + 11x and x <= 9 + 11y }" },
2109 { 1, "{ [0:1, 0:1]; [0, 2:3] }" },
2110 { 1, "{ [0:1, 0:1]; [0, 2:3]; [1, -2:-1] }" },
2111 { 1, "{ [0:3, 0:1]; [1:2, 2:5] }" },
2112 { 1, "{ [0:3, 0:1]; [0:2, 2:5] }" },
2113 { 1, "{ [0:3, 0:1]; [1:3, 2:5] }" },
2114 { 0, "{ [0:3, 0:1]; [1:4, 2:5] }" },
2115 { 0, "{ [0:3, 0:1]; [1:5, 2:5] }" },
2118 /* Test the functionality of isl_set_coalesce with the bounded wrapping
2119 * option turned off.
2121 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
2123 int i;
2124 int r = 0;
2125 int bounded;
2127 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
2128 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
2130 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
2131 const char *str = coalesce_unbounded_tests[i].str;
2132 int check_one = coalesce_unbounded_tests[i].single_disjunct;
2133 if (test_coalesce_set(ctx, str, check_one) >= 0)
2134 continue;
2135 r = -1;
2136 break;
2139 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
2141 return r;
2144 /* Inputs for coalescing tests.
2145 * "str" is a string representation of the input set.
2146 * "single_disjunct" is set if we expect the result to consist of
2147 * a single disjunct.
2149 struct {
2150 int single_disjunct;
2151 const char *str;
2152 } coalesce_tests[] = {
2153 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
2154 "y >= x & x >= 2 & 5 >= y }" },
2155 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
2156 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
2157 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
2158 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
2159 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
2160 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
2161 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
2162 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
2163 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
2164 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
2165 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
2166 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
2167 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
2168 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
2169 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
2170 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
2171 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
2172 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
2173 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
2174 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
2175 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
2176 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
2177 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
2178 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
2179 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
2180 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
2181 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
2182 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
2183 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
2184 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
2185 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
2186 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
2187 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
2188 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
2189 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
2190 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
2191 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
2192 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
2193 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
2194 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
2195 "[o0, o1, o2, o3, o4, o5, o6]] : "
2196 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
2197 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
2198 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
2199 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
2200 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
2201 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
2202 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
2203 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
2204 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
2205 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
2206 "o6 >= i3 + i6 - o3 and M >= 0 and "
2207 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
2208 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
2209 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
2210 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
2211 "(o0 = 0 and M >= 2 and N >= 3) or "
2212 "(M = 0 and o0 = 0 and N >= 3) }" },
2213 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
2214 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
2215 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
2216 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
2217 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
2218 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
2219 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
2220 "(y = 3 and x = 1) }" },
2221 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
2222 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
2223 "i1 <= M and i3 <= M and i4 <= M) or "
2224 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
2225 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
2226 "i4 <= -1 + M) }" },
2227 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
2228 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
2229 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
2230 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
2231 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
2232 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
2233 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
2234 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
2235 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
2236 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
2237 { 0, "{ [a, b] : exists e : 2e = a and "
2238 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
2239 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
2240 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
2241 "j >= 1 and j' <= i + j - i' and i >= 1; "
2242 "[1, 1, 1, 1] }" },
2243 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
2244 "[i,j] : exists a : j = 3a }" },
2245 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
2246 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
2247 "a >= 3) or "
2248 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
2249 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
2250 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
2251 "c <= 6 + 8a and a >= 3; "
2252 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
2253 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
2254 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2255 "[x,0] : 3 <= x <= 4 }" },
2256 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
2257 "[x,0] : 4 <= x <= 5 }" },
2258 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2259 "[x,0] : 3 <= x <= 5 }" },
2260 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
2261 "[x,0] : 3 <= x <= 4 }" },
2262 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
2263 "i1 <= 0; "
2264 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
2265 { 1, "{ [0,0]; [1,1] }" },
2266 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
2267 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
2268 "ii <= k;"
2269 "[k, 0, k] : k <= 6 and k >= 1 }" },
2270 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
2271 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
2272 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
2273 { 1, "[n] -> { [1] : n >= 0;"
2274 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
2275 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
2276 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
2277 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
2278 "2e0 <= x and 2e0 <= n);"
2279 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
2280 "n >= 0) }" },
2281 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
2282 "128e0 >= -134 + 127t1 and t1 >= 2 and "
2283 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
2284 "t1 = 1 }" },
2285 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
2286 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
2287 "[0, 0] }" },
2288 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
2289 "t1 >= 13 and t1 <= 16);"
2290 "[t1] : t1 <= 15 and t1 >= 12 }" },
2291 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
2292 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
2293 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
2294 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
2295 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
2296 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
2297 "i <= 4j + 2 }" },
2298 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
2299 "(exists (e0 : 3e0 = -2 + c0)) }" },
2300 { 0, "[n, b0, t0] -> "
2301 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2302 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2303 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2304 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2305 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
2306 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
2307 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
2308 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
2309 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2310 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2311 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2312 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
2313 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
2314 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
2315 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
2316 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
2317 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
2318 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
2319 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
2320 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
2321 { 0, "{ [i0, i1, i2] : "
2322 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
2323 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
2324 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
2325 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
2326 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
2327 "32e0 <= 31 + i0)) or "
2328 "i0 >= 0 }" },
2329 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
2330 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
2331 "2*floor((c)/2) = c and 0 <= a <= 192;"
2332 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
2334 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
2335 "(0 <= a <= b <= n) }" },
2336 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
2337 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
2338 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
2339 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2340 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2341 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2342 "b = 3 and 9e0 <= -19 + 2c)) }" },
2343 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2344 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2345 "(a = 4 and b = 3 and "
2346 "9*floor((-16 + 2c)/9) <= -19 + 2c) }" },
2347 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
2348 "(b = -1 + a and 0 < a <= 3 and "
2349 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2350 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2351 "b = 3 and 9e0 <= -19 + 2c)) }" },
2352 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2353 "[1, 0] }" },
2354 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2355 "[0, 1] }" },
2356 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2357 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2358 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
2359 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
2360 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
2361 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
2362 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
2363 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
2364 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
2365 { 1, "{ [a] : a <= 8 and "
2366 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
2367 { 1, "{ [x, y] : 2y = -x and x <= 0 or "
2368 "x <= -1 and 2y <= -x - 1 and 2y >= x - 1 }" },
2369 { 0, "{ [x, y] : 2y = -x and x <= 0 or "
2370 "x <= -2 and 2y <= -x - 1 and 2y >= x - 1 }" },
2371 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2372 "(a < 0 and 3*floor((a)/3) < a) }" },
2373 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2374 "(a < -1 and 3*floor((a)/3) < a) }" },
2375 { 1, "{ [a, b] : a <= 1024 and b >= 0 and "
2376 "((-31 - a + b <= 32*floor((-1 - a)/32) <= -33 + b and "
2377 "32*floor((-1 - a)/32) <= -16 + b + 16*floor((-1 - a)/16))"
2378 "or (2 <= a <= 15 and b < a)) }" },
2379 { 1, "{ [a] : a > 0 and ((16*floor((a)/16) < a and "
2380 "32*floor((a)/32) < a) or a <= 15) }" },
2381 { 1, "{ [a, b, c, d] : (-a + d) mod 64 = 0 and a <= 8 and b <= 1 and "
2382 "10 - a <= c <= 3 and d >= 5 and 9 - 64b <= d <= 70;"
2383 "[a, b = 1, c, d] : (-a + d) mod 64 = 0 and a <= 8 and c >= 4 and "
2384 "10 - a <= c <= 5 and 5 <= d <= 73 - c }" },
2385 { 1, "[n, m] -> { S_0[i] : (-n + i) mod 3 = 0 and m >= 3 + n and "
2386 "i >= n and 3*floor((2 + n + 2m)/3) <= n + 3m - i; "
2387 "S_0[n] : n <= m <= 2 + n }" },
2388 { 1, "{ [a, b] : exists (e0: 0 <= a <= 1 and b >= 0 and "
2389 "2e0 >= -5 + a + 2b and 2e0 >= -1 + a + b and "
2390 "2e0 <= a + b); "
2391 "[a, b] : exists (e0: 0 <= a <= 1 and 2e0 >= -5 + a + 2b and "
2392 "2e0 >= -1 - a + b and 2e0 <= -a + b and "
2393 "2e0 < -a + 2b) }" },
2394 { 1, "{ [i, j, i - 8j] : 8 <= i <= 63 and -7 + i <= 8j <= i; "
2395 "[i, 0, i] : 0 <= i <= 7 }" },
2396 { 1, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [1, 1] }" },
2397 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [0, 2] }" },
2398 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [-1, 3] }" },
2399 { 1, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [1, 1] }" },
2400 { 0, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [2, 1] }" },
2401 { 0, "{ [a, c] : (2 + a) mod 4 = 0 or "
2402 "(c = 4 + a and 4 * floor((a)/4) = a and a >= 0 and a <= 4) or "
2403 "(c = 3 + a and 4 * floor((-1 + a)/4) = -1 + a and "
2404 "a > 0 and a <= 5) }" },
2405 { 1, "{ [1, 0, 0]; [a, b, c] : -1 <= -a < b <= 0 and 2c > b }" },
2406 { 0, "{ [j, a, l] : a mod 2 = 0 and j <= 29 and a >= 2 and "
2407 "2a <= -5 + j and 32j + 2a + 2 <= 4l < 33j; "
2408 "[j, 0, l] : 4 <= j <= 29 and -3 + 33j <= 4l <= 33j }" },
2409 { 0, "{ [0:1, 0:1]; [0, 2:3] }" },
2410 { 1, "{ [a] : (a = 0 or ((1 + a) mod 2 = 0 and 0 < a <= 15) or "
2411 "((a) mod 2 = 0 and 0 < a <= 15)) }" },
2412 { 1, "{ rat: [0:2]; rat: [1:3] }" },
2413 { 1, "{ [a = 1:14] : a mod 4 = 0 and 4*floor((1 + a)/4) <= a; [0] }" },
2414 { 0, "{ [a = 4:6, b = 1:7, 8] : a mod 2 = 1 and b mod 2 = 1;"
2415 "[a = 1:7, 1:7, a] }" },
2416 { 1, "{ [a] : a mod 2 = 0 and (a <= 3 or a >= 12 or 4 <= a <= 11) }" },
2417 { 1, "{ [a] : 0 < a <= 341 or "
2418 "(0 < a <= 700 and 360*floor(a/360) >= -340 + a) }" },
2419 { 0, "{ [a] : 0 < a <= 341 or "
2420 "(0 < a <= 701 and 360*floor(a/360) >= -340 + a) }" },
2423 /* A specialized coalescing test case that would result
2424 * in a segmentation fault or a failed assertion in earlier versions of isl.
2426 static int test_coalesce_special(struct isl_ctx *ctx)
2428 const char *str;
2429 isl_map *map1, *map2;
2431 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2432 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
2433 "(y = 201 and o1 <= 239 and o1 >= 212) or "
2434 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
2435 "o1 <= 239 and o1 >= 212)) or "
2436 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
2437 "o1 <= 241 and o1 >= 240));"
2438 "[S_L220_OUT[] -> T7[]] -> "
2439 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
2440 "(y = 2 and o1 <= 241 and o1 >= 212) or "
2441 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
2442 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
2443 map1 = isl_map_read_from_str(ctx, str);
2444 map1 = isl_map_align_divs_internal(map1);
2445 map1 = isl_map_coalesce(map1);
2446 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2447 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
2448 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
2449 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
2450 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
2451 map2 = isl_map_read_from_str(ctx, str);
2452 map2 = isl_map_union(map2, map1);
2453 map2 = isl_map_align_divs_internal(map2);
2454 map2 = isl_map_coalesce(map2);
2455 isl_map_free(map2);
2456 if (!map2)
2457 return -1;
2459 return 0;
2462 /* Check that the union of the basic sets described by "str1" and "str2"
2463 * can be coalesced and that the result is equal to the union.
2464 * The explicit call to isl_basic_set_union prevents the implicit
2465 * equality constraints in the basic maps from being detected prior
2466 * to the call to isl_set_coalesce, at least at the point
2467 * where this function was introduced.
2469 static isl_stat test_coalesce_union(isl_ctx *ctx, const char *str1,
2470 const char *str2)
2472 isl_basic_set *bset1, *bset2;
2473 isl_set *set, *set2;
2474 isl_bool equal;
2476 bset1 = isl_basic_set_read_from_str(ctx, str1);
2477 bset2 = isl_basic_set_read_from_str(ctx, str2);
2478 set = isl_basic_set_union(bset1, bset2);
2479 set = isl_set_coalesce(set);
2481 bset1 = isl_basic_set_read_from_str(ctx, str1);
2482 bset2 = isl_basic_set_read_from_str(ctx, str2);
2483 set2 = isl_basic_set_union(bset1, bset2);
2485 equal = isl_set_is_equal(set, set2);
2486 isl_set_free(set);
2487 isl_set_free(set2);
2489 if (equal < 0)
2490 return isl_stat_error;
2491 if (!equal)
2492 isl_die(ctx, isl_error_unknown,
2493 "coalesced set not equal to input",
2494 return isl_stat_error);
2496 return isl_stat_non_null(set);
2499 /* A specialized coalescing test case that would result in an assertion
2500 * in an earlier version of isl. Use test_coalesce_union with
2501 * an explicit call to isl_basic_set_union to prevent the implicit
2502 * equality constraints in the first basic map from being detected prior
2503 * to the call to isl_set_coalesce, at least at the point
2504 * where this test case was introduced.
2506 static isl_stat test_coalesce_special2(struct isl_ctx *ctx)
2508 const char *str1;
2509 const char *str2;
2511 str1 = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
2512 str2 = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }";
2513 return test_coalesce_union(ctx, str1, str2);
2516 /* Check that calling isl_set_coalesce does not leave other sets
2517 * that may share some information with the input to isl_set_coalesce
2518 * in an inconsistent state.
2519 * In particular, older versions of isl would modify all copies
2520 * of the basic sets in the isl_set_coalesce input in a way
2521 * that could leave them in an inconsistent state.
2522 * The result of printing any other set containing one of these
2523 * basic sets would then result in an invalid set description.
2525 static int test_coalesce_special3(isl_ctx *ctx)
2527 const char *str;
2528 char *s;
2529 isl_set *set1, *set2;
2530 isl_printer *p;
2532 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
2533 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
2534 set2 = isl_set_read_from_str(ctx, str);
2535 set1 = isl_set_union(set1, isl_set_copy(set2));
2536 set1 = isl_set_coalesce(set1);
2537 isl_set_free(set1);
2539 p = isl_printer_to_str(ctx);
2540 p = isl_printer_print_set(p, set2);
2541 isl_set_free(set2);
2542 s = isl_printer_get_str(p);
2543 isl_printer_free(p);
2544 set1 = isl_set_read_from_str(ctx, s);
2545 free(s);
2546 isl_set_free(set1);
2548 if (!set1)
2549 return -1;
2551 return 0;
2554 /* Check that calling isl_set_coalesce on the intersection of
2555 * the sets described by "s1" and "s2" does not leave other sets
2556 * that may share some information with the input to isl_set_coalesce
2557 * in an inconsistent state.
2558 * In particular, when isl_set_coalesce detects equality constraints,
2559 * it does not immediately perform Gaussian elimination on them,
2560 * but then it needs to ensure that it is performed at some point.
2561 * The input set has implicit equality constraints in the first disjunct.
2562 * It is constructed as an intersection, because otherwise
2563 * those equality constraints would already be detected during parsing.
2565 static isl_stat test_coalesce_intersection(isl_ctx *ctx,
2566 const char *s1, const char *s2)
2568 isl_set *set1, *set2;
2570 set1 = isl_set_read_from_str(ctx, s1);
2571 set2 = isl_set_read_from_str(ctx, s2);
2572 set1 = isl_set_intersect(set1, set2);
2573 isl_set_free(isl_set_coalesce(isl_set_copy(set1)));
2574 set1 = isl_set_coalesce(set1);
2575 isl_set_free(set1);
2577 if (!set1)
2578 return isl_stat_error;
2580 return isl_stat_ok;
2583 /* Check that calling isl_set_coalesce does not leave other sets
2584 * that may share some information with the input to isl_set_coalesce
2585 * in an inconsistent state, for the case where one disjunct
2586 * is a subset of the other.
2588 static isl_stat test_coalesce_special4(isl_ctx *ctx)
2590 const char *s1, *s2;
2592 s1 = "{ [a, b] : b <= 0 or a <= 1 }";
2593 s2 = "{ [a, b] : -1 <= -a < b }";
2594 return test_coalesce_intersection(ctx, s1, s2);
2597 /* Check that calling isl_set_coalesce does not leave other sets
2598 * that may share some information with the input to isl_set_coalesce
2599 * in an inconsistent state, for the case where two disjuncts
2600 * can be fused.
2602 static isl_stat test_coalesce_special5(isl_ctx *ctx)
2604 const char *s1, *s2;
2606 s1 = "{ [a, b, c] : b <= 0 }";
2607 s2 = "{ [a, b, c] : -1 <= -a < b and (c >= 0 or c < 0) }";
2608 return test_coalesce_intersection(ctx, s1, s2);
2611 /* Check that calling isl_set_coalesce does not leave other sets
2612 * that may share some information with the input to isl_set_coalesce
2613 * in an inconsistent state, for the case where two disjuncts
2614 * can be fused and where both disjuncts have implicit equality constraints.
2616 static isl_stat test_coalesce_special6(isl_ctx *ctx)
2618 const char *s1, *s2;
2620 s1 = "{ [a, b, c] : c <= 0 }";
2621 s2 = "{ [a, b, c] : 0 <= a <= b <= c or (0 <= b <= c and a > 0) }";
2622 return test_coalesce_intersection(ctx, s1, s2);
2625 /* A specialized coalescing test case that would result in an assertion failure
2626 * in an earlier version of isl. Use test_coalesce_union with
2627 * an explicit call to isl_basic_set_union to prevent the implicit
2628 * equality constraints in the basic maps from being detected prior
2629 * to the call to isl_set_coalesce, at least at the point
2630 * where this test case was introduced.
2632 static isl_stat test_coalesce_special7(isl_ctx *ctx)
2634 const char *str1;
2635 const char *str2;
2637 str1 = "{ [a, b, c=0:17] : a <= 7 and 2b <= 11 - a and "
2638 "c <= -7 + 2a and 2c >= - 3 + 3a - 2b }";
2639 str2 = "{ [a, b, c] : c > -15a and c >= -7 + 2a and c < 0 and "
2640 "3c <= -5 + 5a - 3b and 2b >= 11 - a }";
2641 return test_coalesce_union(ctx, str1, str2);
2644 /* A specialized coalescing test case that would result in a disjunct
2645 * getting dropped in an earlier version of isl. Use test_coalesce_union with
2646 * an explicit call to isl_basic_set_union to prevent the implicit
2647 * equality constraints in the basic maps from being detected prior
2648 * to the call to isl_set_coalesce, at least at the point
2649 * where this test case was introduced.
2651 static isl_stat test_coalesce_special8(isl_ctx *ctx)
2653 const char *str1;
2654 const char *str2;
2656 str1 = "{ [a, b, c] : 2c <= -a and b >= -a and b <= 5 and "
2657 "6c > -7a and 11c >= -5a - b and a <= 3 }";
2658 str2 = "{ [a, b, c] : 6c > -7a and b >= -a and b <= 5 and "
2659 "11c >= -5a - b and a >= 4 and 2b <= a and 2c <= -a }";
2660 return test_coalesce_union(ctx, str1, str2);
2663 /* Test the functionality of isl_set_coalesce.
2664 * That is, check that the output is always equal to the input
2665 * and in some cases that the result consists of a single disjunct.
2667 static int test_coalesce(struct isl_ctx *ctx)
2669 int i;
2671 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2672 const char *str = coalesce_tests[i].str;
2673 int check_one = coalesce_tests[i].single_disjunct;
2674 if (test_coalesce_set(ctx, str, check_one) < 0)
2675 return -1;
2678 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2679 return -1;
2680 if (test_coalesce_special(ctx) < 0)
2681 return -1;
2682 if (test_coalesce_special2(ctx) < 0)
2683 return -1;
2684 if (test_coalesce_special3(ctx) < 0)
2685 return -1;
2686 if (test_coalesce_special4(ctx) < 0)
2687 return -1;
2688 if (test_coalesce_special5(ctx) < 0)
2689 return -1;
2690 if (test_coalesce_special6(ctx) < 0)
2691 return -1;
2692 if (test_coalesce_special7(ctx) < 0)
2693 return -1;
2694 if (test_coalesce_special8(ctx) < 0)
2695 return -1;
2697 return 0;
2700 /* Construct a representation of the graph on the right of Figure 1
2701 * in "Computing the Transitive Closure of a Union of
2702 * Affine Integer Tuple Relations".
2704 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2706 isl_set *dom;
2707 isl_map *up, *right;
2709 dom = isl_set_read_from_str(ctx,
2710 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2711 "2 x - 3 y + 3 >= 0 }");
2712 right = isl_map_read_from_str(ctx,
2713 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2714 up = isl_map_read_from_str(ctx,
2715 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2716 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2717 right = isl_map_intersect_range(right, isl_set_copy(dom));
2718 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2719 up = isl_map_intersect_range(up, dom);
2720 return isl_map_union(up, right);
2723 /* Construct a representation of the power of the graph
2724 * on the right of Figure 1 in "Computing the Transitive Closure of
2725 * a Union of Affine Integer Tuple Relations".
2727 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2729 return isl_map_read_from_str(ctx,
2730 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2731 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2732 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2735 /* Construct a representation of the transitive closure of the graph
2736 * on the right of Figure 1 in "Computing the Transitive Closure of
2737 * a Union of Affine Integer Tuple Relations".
2739 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2741 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2744 static int test_closure(isl_ctx *ctx)
2746 const char *str;
2747 isl_map *map, *map2;
2748 isl_bool exact, equal;
2750 /* COCOA example 1 */
2751 map = isl_map_read_from_str(ctx,
2752 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2753 "1 <= i and i < n and 1 <= j and j < n or "
2754 "i2 = i + 1 and j2 = j - 1 and "
2755 "1 <= i and i < n and 2 <= j and j <= n }");
2756 map = isl_map_power(map, &exact);
2757 assert(exact);
2758 isl_map_free(map);
2760 /* COCOA example 1 */
2761 map = isl_map_read_from_str(ctx,
2762 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2763 "1 <= i and i < n and 1 <= j and j < n or "
2764 "i2 = i + 1 and j2 = j - 1 and "
2765 "1 <= i and i < n and 2 <= j and j <= n }");
2766 map = isl_map_transitive_closure(map, &exact);
2767 assert(exact);
2768 map2 = isl_map_read_from_str(ctx,
2769 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2770 "1 <= i and i < n and 1 <= j and j <= n and "
2771 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2772 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2773 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2774 assert(isl_map_is_equal(map, map2));
2775 isl_map_free(map2);
2776 isl_map_free(map);
2778 map = isl_map_read_from_str(ctx,
2779 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2780 " 0 <= y and y <= n }");
2781 map = isl_map_transitive_closure(map, &exact);
2782 map2 = isl_map_read_from_str(ctx,
2783 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2784 " 0 <= y and y <= n }");
2785 assert(isl_map_is_equal(map, map2));
2786 isl_map_free(map2);
2787 isl_map_free(map);
2789 /* COCOA example 2 */
2790 map = isl_map_read_from_str(ctx,
2791 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2792 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2793 "i2 = i + 2 and j2 = j - 2 and "
2794 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2795 map = isl_map_transitive_closure(map, &exact);
2796 assert(exact);
2797 map2 = isl_map_read_from_str(ctx,
2798 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2799 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2800 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2801 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2802 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2803 assert(isl_map_is_equal(map, map2));
2804 isl_map_free(map);
2805 isl_map_free(map2);
2807 /* COCOA Fig.2 left */
2808 map = isl_map_read_from_str(ctx,
2809 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2810 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2811 "j <= n or "
2812 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2813 "j <= 2 i - 3 and j <= n - 2 or "
2814 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2815 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2816 map = isl_map_transitive_closure(map, &exact);
2817 assert(exact);
2818 isl_map_free(map);
2820 /* COCOA Fig.2 right */
2821 map = isl_map_read_from_str(ctx,
2822 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2823 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2824 "j <= n or "
2825 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2826 "j <= 2 i - 4 and j <= n - 3 or "
2827 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2828 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2829 map = isl_map_power(map, &exact);
2830 assert(exact);
2831 isl_map_free(map);
2833 /* COCOA Fig.2 right */
2834 map = isl_map_read_from_str(ctx,
2835 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2836 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2837 "j <= n or "
2838 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2839 "j <= 2 i - 4 and j <= n - 3 or "
2840 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2841 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2842 map = isl_map_transitive_closure(map, &exact);
2843 assert(exact);
2844 map2 = isl_map_read_from_str(ctx,
2845 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2846 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2847 "j <= n and 3 + i + 2 j <= 3 n and "
2848 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2849 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2850 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2851 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2852 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2853 assert(isl_map_is_equal(map, map2));
2854 isl_map_free(map2);
2855 isl_map_free(map);
2857 map = cocoa_fig_1_right_graph(ctx);
2858 map = isl_map_transitive_closure(map, &exact);
2859 assert(exact);
2860 map2 = cocoa_fig_1_right_tc(ctx);
2861 assert(isl_map_is_equal(map, map2));
2862 isl_map_free(map2);
2863 isl_map_free(map);
2865 map = cocoa_fig_1_right_graph(ctx);
2866 map = isl_map_power(map, &exact);
2867 map2 = cocoa_fig_1_right_power(ctx);
2868 equal = isl_map_is_equal(map, map2);
2869 isl_map_free(map2);
2870 isl_map_free(map);
2871 if (equal < 0)
2872 return -1;
2873 if (!exact)
2874 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2875 if (!equal)
2876 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2878 /* COCOA Theorem 1 counter example */
2879 map = isl_map_read_from_str(ctx,
2880 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2881 "i2 = 1 and j2 = j or "
2882 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2883 map = isl_map_transitive_closure(map, &exact);
2884 assert(exact);
2885 isl_map_free(map);
2887 map = isl_map_read_from_str(ctx,
2888 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2889 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2890 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2891 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2892 map = isl_map_transitive_closure(map, &exact);
2893 assert(exact);
2894 isl_map_free(map);
2896 /* Kelly et al 1996, fig 12 */
2897 map = isl_map_read_from_str(ctx,
2898 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2899 "1 <= i,j,j+1 <= n or "
2900 "j = n and j2 = 1 and i2 = i + 1 and "
2901 "1 <= i,i+1 <= n }");
2902 map = isl_map_transitive_closure(map, &exact);
2903 assert(exact);
2904 map2 = isl_map_read_from_str(ctx,
2905 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2906 "1 <= i <= n and i = i2 or "
2907 "1 <= i < i2 <= n and 1 <= j <= n and "
2908 "1 <= j2 <= n }");
2909 assert(isl_map_is_equal(map, map2));
2910 isl_map_free(map2);
2911 isl_map_free(map);
2913 /* Omega's closure4 */
2914 map = isl_map_read_from_str(ctx,
2915 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2916 "1 <= x,y <= 10 or "
2917 "x2 = x + 1 and y2 = y and "
2918 "1 <= x <= 20 && 5 <= y <= 15 }");
2919 map = isl_map_transitive_closure(map, &exact);
2920 assert(exact);
2921 isl_map_free(map);
2923 map = isl_map_read_from_str(ctx,
2924 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2925 map = isl_map_transitive_closure(map, &exact);
2926 assert(!exact);
2927 map2 = isl_map_read_from_str(ctx,
2928 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2929 assert(isl_map_is_equal(map, map2));
2930 isl_map_free(map);
2931 isl_map_free(map2);
2933 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2934 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2935 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2936 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2937 map = isl_map_read_from_str(ctx, str);
2938 map = isl_map_transitive_closure(map, &exact);
2939 assert(exact);
2940 map2 = isl_map_read_from_str(ctx, str);
2941 assert(isl_map_is_equal(map, map2));
2942 isl_map_free(map);
2943 isl_map_free(map2);
2945 str = "{[0] -> [1]; [2] -> [3]}";
2946 map = isl_map_read_from_str(ctx, str);
2947 map = isl_map_transitive_closure(map, &exact);
2948 assert(exact);
2949 map2 = isl_map_read_from_str(ctx, str);
2950 assert(isl_map_is_equal(map, map2));
2951 isl_map_free(map);
2952 isl_map_free(map2);
2954 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2955 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2956 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2957 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2958 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2959 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2960 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2961 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2962 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2963 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2964 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2965 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2966 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2967 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2968 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2969 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2970 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2971 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2972 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2973 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2974 map = isl_map_read_from_str(ctx, str);
2975 map = isl_map_transitive_closure(map, NULL);
2976 assert(map);
2977 isl_map_free(map);
2979 return 0;
2982 /* Check that the actual result of a boolean operation is equal
2983 * to the expected result.
2985 static isl_stat check_bool(isl_ctx *ctx, isl_bool actual, isl_bool expected)
2987 if (actual != expected)
2988 isl_die(ctx, isl_error_unknown,
2989 "incorrect boolean operation", return isl_stat_error);
2990 return isl_stat_ok;
2993 /* Test operations on isl_bool values.
2995 * This tests:
2997 * isl_bool_not
2998 * isl_bool_ok
3000 static int test_isl_bool(isl_ctx *ctx)
3002 if (check_bool(ctx, isl_bool_not(isl_bool_true), isl_bool_false) < 0)
3003 return -1;
3004 if (check_bool(ctx, isl_bool_not(isl_bool_false), isl_bool_true) < 0)
3005 return -1;
3006 if (check_bool(ctx, isl_bool_not(isl_bool_error), isl_bool_error) < 0)
3007 return -1;
3008 if (check_bool(ctx, isl_bool_ok(0), isl_bool_false) < 0)
3009 return -1;
3010 if (check_bool(ctx, isl_bool_ok(1), isl_bool_true) < 0)
3011 return -1;
3012 if (check_bool(ctx, isl_bool_ok(-1), isl_bool_true) < 0)
3013 return -1;
3014 if (check_bool(ctx, isl_bool_ok(2), isl_bool_true) < 0)
3015 return -1;
3016 if (check_bool(ctx, isl_bool_ok(-2), isl_bool_true) < 0)
3017 return -1;
3019 return 0;
3022 static int test_lex(struct isl_ctx *ctx)
3024 isl_space *space;
3025 isl_map *map;
3026 int empty;
3028 space = isl_space_set_alloc(ctx, 0, 0);
3029 map = isl_map_lex_le(space);
3030 empty = isl_map_is_empty(map);
3031 isl_map_free(map);
3033 if (empty < 0)
3034 return -1;
3035 if (empty)
3036 isl_die(ctx, isl_error_unknown,
3037 "expecting non-empty result", return -1);
3039 return 0;
3042 /* Inputs for isl_map_lexmin tests.
3043 * "map" is the input and "lexmin" is the expected result.
3045 struct {
3046 const char *map;
3047 const char *lexmin;
3048 } lexmin_tests [] = {
3049 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
3050 "{ [x] -> [5] : 6 <= x <= 8; "
3051 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
3052 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
3053 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
3054 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
3055 "{ [x] -> [y] : (4y = x and x >= 0) or "
3056 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
3057 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
3058 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
3059 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
3060 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
3061 /* Check that empty pieces are properly combined. */
3062 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
3063 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
3064 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
3065 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
3066 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
3067 "x >= 4 }" },
3068 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
3069 "a <= 255 and c <= 255 and d <= 255 - j and "
3070 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
3071 "247d <= 247 + k - j and 247d <= 247 + k - b and "
3072 "247d <= 247 + i and 248 - b <= 248d <= c and "
3073 "254d >= i - a + b and 254d >= -a + b and "
3074 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
3075 "{ [i, k, j] -> "
3076 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
3077 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
3078 "247j >= 62738 - i and 509j <= 129795 + i and "
3079 "742j >= 188724 - i; "
3080 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
3081 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
3082 "16*floor((8 + b)/16) <= 7 + b; "
3083 "[a] -> [1] }",
3084 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
3085 "[a] -> [b = 0] : 0 < a <= 509 }" },
3086 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
3087 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
3088 { "{ rat: [i] : 21 <= 2i <= 29 or i = 5 }", "{ rat: [5] }" },
3091 static int test_lexmin(struct isl_ctx *ctx)
3093 int i;
3094 int equal;
3095 const char *str;
3096 isl_basic_map *bmap;
3097 isl_map *map, *map2;
3098 isl_set *set;
3099 isl_set *set2;
3100 isl_pw_multi_aff *pma;
3102 str = "[p0, p1] -> { [] -> [] : "
3103 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
3104 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
3105 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
3106 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
3107 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
3108 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
3109 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
3110 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
3111 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
3112 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
3113 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
3114 map = isl_map_read_from_str(ctx, str);
3115 map = isl_map_lexmin(map);
3116 isl_map_free(map);
3117 if (!map)
3118 return -1;
3120 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
3121 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
3122 set = isl_set_read_from_str(ctx, str);
3123 set = isl_set_lexmax(set);
3124 str = "[C] -> { [obj,a,b,c] : C = 8 }";
3125 set2 = isl_set_read_from_str(ctx, str);
3126 set = isl_set_intersect(set, set2);
3127 assert(!isl_set_is_empty(set));
3128 isl_set_free(set);
3130 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
3131 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
3132 map = isl_map_lexmin(map);
3133 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
3134 equal = isl_map_is_equal(map, map2);
3135 isl_map_free(map);
3136 isl_map_free(map2);
3138 if (equal < 0)
3139 return -1;
3140 if (!equal)
3141 isl_die(ctx, isl_error_unknown,
3142 "unexpected result", return -1);
3145 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
3146 " 8i' <= i and 8i' >= -7 + i }";
3147 bmap = isl_basic_map_read_from_str(ctx, str);
3148 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
3149 map2 = isl_map_from_pw_multi_aff(pma);
3150 map = isl_map_from_basic_map(bmap);
3151 assert(isl_map_is_equal(map, map2));
3152 isl_map_free(map);
3153 isl_map_free(map2);
3155 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
3156 " 8i' <= i and 8i' >= -7 + i }";
3157 set = isl_set_read_from_str(ctx, str);
3158 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
3159 set2 = isl_set_from_pw_multi_aff(pma);
3160 equal = isl_set_is_equal(set, set2);
3161 isl_set_free(set);
3162 isl_set_free(set2);
3163 if (equal < 0)
3164 return -1;
3165 if (!equal)
3166 isl_die(ctx, isl_error_unknown,
3167 "unexpected difference between set and "
3168 "piecewise affine expression", return -1);
3170 return 0;
3173 /* Inputs for isl_pw_multi_aff_max_multi_val tests.
3174 * "pma" is the input.
3175 * "res" is the expected result.
3177 static struct {
3178 const char *pma;
3179 const char *res;
3180 } opt_pw_tests[] = {
3181 { "{ [-1] -> [-1]; [1] -> [1] }", "{ [1] }" },
3182 { "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] : "
3183 "0 <= a, b <= 100 and b mod 2 = 0}", "{ [30] }" },
3184 { "[N] -> { [i,j] -> A[i, -i, i + j] : 0 <= i,j <= N <= 10 }",
3185 "{ A[10, 0, 20] }" },
3186 { "[N] -> {A[N, -N, 2N] : 0 <= N }", "{ A[infty, 0, infty] }" },
3189 /* Perform basic isl_pw_multi_aff_max_multi_val tests.
3191 static isl_stat test_pw_max(struct isl_ctx *ctx)
3193 int i;
3194 isl_pw_multi_aff *pma;
3195 isl_multi_val *mv;
3196 isl_stat r;
3198 for (i = 0; i < ARRAY_SIZE(opt_pw_tests); ++i) {
3199 pma = isl_pw_multi_aff_read_from_str(ctx, opt_pw_tests[i].pma);
3200 mv = isl_pw_multi_aff_max_multi_val(pma);
3201 r = multi_val_check_plain_equal(mv, opt_pw_tests[i].res);
3202 isl_multi_val_free(mv);
3204 if (r < 0)
3205 return isl_stat_error;
3208 return isl_stat_ok;
3211 /* A specialized isl_set_min_val test case that would return the wrong result
3212 * in earlier versions of isl.
3213 * The explicit call to isl_basic_set_union prevents the second basic set
3214 * from being determined to be empty prior to the call to isl_set_min_val,
3215 * at least at the point where this test case was introduced.
3217 static int test_min_special(isl_ctx *ctx)
3219 const char *str;
3220 isl_basic_set *bset1, *bset2;
3221 isl_set *set;
3222 isl_aff *obj;
3223 isl_val *res;
3224 int ok;
3226 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
3227 bset1 = isl_basic_set_read_from_str(ctx, str);
3228 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
3229 bset2 = isl_basic_set_read_from_str(ctx, str);
3230 set = isl_basic_set_union(bset1, bset2);
3231 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
3233 res = isl_set_min_val(set, obj);
3234 ok = isl_val_cmp_si(res, 5) == 0;
3236 isl_aff_free(obj);
3237 isl_set_free(set);
3238 isl_val_free(res);
3240 if (!res)
3241 return -1;
3242 if (!ok)
3243 isl_die(ctx, isl_error_unknown, "unexpected minimum",
3244 return -1);
3246 return 0;
3249 /* A specialized isl_set_min_val test case that would return an error
3250 * in earlier versions of isl.
3252 static int test_min_special2(isl_ctx *ctx)
3254 const char *str;
3255 isl_basic_set *bset;
3256 isl_aff *obj;
3257 isl_val *res;
3259 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
3260 bset = isl_basic_set_read_from_str(ctx, str);
3262 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
3264 res = isl_basic_set_max_val(bset, obj);
3266 isl_basic_set_free(bset);
3267 isl_aff_free(obj);
3268 isl_val_free(res);
3270 if (!res)
3271 return -1;
3273 return 0;
3276 /* Check that the result of isl_set_min_multi_pw_aff
3277 * on the union of the sets with string descriptions "s1" and "s2"
3278 * consists of a single expression (on a single cell).
3280 static isl_stat check_single_expr_min(isl_ctx *ctx, const char *s1,
3281 const char *s2)
3283 isl_size n;
3284 isl_set *set1, *set2;
3285 isl_multi_pw_aff *mpa;
3286 isl_pw_multi_aff *pma;
3288 set1 = isl_set_read_from_str(ctx, s1);
3289 set2 = isl_set_read_from_str(ctx, s2);
3290 set1 = isl_set_union(set1, set2);
3291 mpa = isl_set_min_multi_pw_aff(set1);
3292 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
3293 n = isl_pw_multi_aff_n_piece(pma);
3294 isl_pw_multi_aff_free(pma);
3296 if (n < 0)
3297 return isl_stat_error;
3298 if (n != 1)
3299 isl_die(ctx, isl_error_unknown, "expecting single expression",
3300 return isl_stat_error);
3301 return isl_stat_ok;
3304 /* A specialized isl_set_min_multi_pw_aff test that checks
3305 * that the minimum of 2N and 3N for N >= 0 is represented
3306 * by a single expression, without splitting off the special case N = 0.
3307 * Do this for both orderings.
3309 static int test_min_mpa(isl_ctx *ctx)
3311 const char *s1, *s2;
3313 s1 = "[N=0:] -> { [1, 3N:] }";
3314 s2 = "[N=0:] -> { [10, 2N:] }";
3315 if (check_single_expr_min(ctx, s1, s2) < 0)
3316 return -1;
3317 if (check_single_expr_min(ctx, s2, s1) < 0)
3318 return -1;
3320 return 0;
3323 struct {
3324 const char *set;
3325 const char *obj;
3326 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
3327 __isl_keep isl_aff *obj);
3328 const char *res;
3329 } opt_tests[] = {
3330 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
3331 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
3332 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
3333 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
3334 &isl_set_max_val, "30" },
3338 /* Perform basic isl_set_min_val and isl_set_max_val tests.
3339 * In particular, check the results on non-convex inputs.
3341 static int test_min(struct isl_ctx *ctx)
3343 int i;
3344 isl_set *set;
3345 isl_aff *obj;
3346 isl_val *val, *res;
3347 isl_bool ok;
3349 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
3350 set = isl_set_read_from_str(ctx, opt_tests[i].set);
3351 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
3352 res = isl_val_read_from_str(ctx, opt_tests[i].res);
3353 val = opt_tests[i].fn(set, obj);
3354 ok = isl_val_eq(res, val);
3355 isl_val_free(res);
3356 isl_val_free(val);
3357 isl_aff_free(obj);
3358 isl_set_free(set);
3360 if (ok < 0)
3361 return -1;
3362 if (!ok)
3363 isl_die(ctx, isl_error_unknown,
3364 "unexpected optimum", return -1);
3367 if (test_pw_max(ctx) < 0)
3368 return -1;
3369 if (test_min_special(ctx) < 0)
3370 return -1;
3371 if (test_min_special2(ctx) < 0)
3372 return -1;
3374 return 0;
3377 struct must_may {
3378 isl_map *must;
3379 isl_map *may;
3382 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
3383 void *dep_user, void *user)
3385 struct must_may *mm = (struct must_may *)user;
3387 if (must)
3388 mm->must = isl_map_union(mm->must, dep);
3389 else
3390 mm->may = isl_map_union(mm->may, dep);
3392 return isl_stat_ok;
3395 static int common_space(void *first, void *second)
3397 int depth = *(int *)first;
3398 return 2 * depth;
3401 static int map_is_equal(__isl_keep isl_map *map, const char *str)
3403 isl_map *map2;
3404 int equal;
3406 if (!map)
3407 return -1;
3409 map2 = isl_map_read_from_str(map->ctx, str);
3410 equal = isl_map_is_equal(map, map2);
3411 isl_map_free(map2);
3413 return equal;
3416 static int map_check_equal(__isl_keep isl_map *map, const char *str)
3418 int equal;
3420 equal = map_is_equal(map, str);
3421 if (equal < 0)
3422 return -1;
3423 if (!equal)
3424 isl_die(isl_map_get_ctx(map), isl_error_unknown,
3425 "result not as expected", return -1);
3426 return 0;
3429 /* Is "set" equal to the set described by "str"?
3431 static isl_bool set_is_equal(__isl_keep isl_set *set, const char *str)
3433 isl_set *set2;
3434 isl_bool equal;
3436 if (!set)
3437 return isl_bool_error;
3439 set2 = isl_set_read_from_str(isl_set_get_ctx(set), str);
3440 equal = isl_set_is_equal(set, set2);
3441 isl_set_free(set2);
3443 return equal;
3446 /* Check that "set" is equal to the set described by "str".
3448 static isl_stat set_check_equal(__isl_keep isl_set *set, const char *str)
3450 isl_bool equal;
3452 equal = set_is_equal(set, str);
3453 if (equal < 0)
3454 return isl_stat_error;
3455 if (!equal)
3456 isl_die(isl_set_get_ctx(set), isl_error_unknown,
3457 "result not as expected", return isl_stat_error);
3458 return isl_stat_ok;
3461 /* Is "uset" equal to the union set described by "str"?
3463 static isl_bool uset_is_equal(__isl_keep isl_union_set *uset, const char *str)
3465 isl_union_set *uset2;
3466 isl_bool equal;
3468 if (!uset)
3469 return isl_bool_error;
3471 uset2 = isl_union_set_read_from_str(isl_union_set_get_ctx(uset), str);
3472 equal = isl_union_set_is_equal(uset, uset2);
3473 isl_union_set_free(uset2);
3475 return equal;
3478 /* Check that "uset" is equal to the union set described by "str".
3480 static isl_stat uset_check_equal(__isl_keep isl_union_set *uset,
3481 const char *str)
3483 isl_bool equal;
3485 equal = uset_is_equal(uset, str);
3486 if (equal < 0)
3487 return isl_stat_error;
3488 if (!equal)
3489 isl_die(isl_union_set_get_ctx(uset), isl_error_unknown,
3490 "result not as expected", return isl_stat_error);
3491 return isl_stat_ok;
3494 static int test_dep(struct isl_ctx *ctx)
3496 const char *str;
3497 isl_space *space;
3498 isl_map *map;
3499 isl_access_info *ai;
3500 isl_flow *flow;
3501 int depth;
3502 struct must_may mm;
3504 depth = 3;
3506 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3507 map = isl_map_read_from_str(ctx, str);
3508 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3510 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3511 map = isl_map_read_from_str(ctx, str);
3512 ai = isl_access_info_add_source(ai, map, 1, &depth);
3514 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3515 map = isl_map_read_from_str(ctx, str);
3516 ai = isl_access_info_add_source(ai, map, 1, &depth);
3518 flow = isl_access_info_compute_flow(ai);
3519 space = isl_space_alloc(ctx, 0, 3, 3);
3520 mm.must = isl_map_empty(isl_space_copy(space));
3521 mm.may = isl_map_empty(space);
3523 isl_flow_foreach(flow, collect_must_may, &mm);
3525 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
3526 " [1,10,0] -> [2,5,0] }";
3527 assert(map_is_equal(mm.must, str));
3528 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3529 assert(map_is_equal(mm.may, str));
3531 isl_map_free(mm.must);
3532 isl_map_free(mm.may);
3533 isl_flow_free(flow);
3536 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3537 map = isl_map_read_from_str(ctx, str);
3538 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3540 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3541 map = isl_map_read_from_str(ctx, str);
3542 ai = isl_access_info_add_source(ai, map, 1, &depth);
3544 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3545 map = isl_map_read_from_str(ctx, str);
3546 ai = isl_access_info_add_source(ai, map, 0, &depth);
3548 flow = isl_access_info_compute_flow(ai);
3549 space = isl_space_alloc(ctx, 0, 3, 3);
3550 mm.must = isl_map_empty(isl_space_copy(space));
3551 mm.may = isl_map_empty(space);
3553 isl_flow_foreach(flow, collect_must_may, &mm);
3555 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
3556 assert(map_is_equal(mm.must, str));
3557 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3558 assert(map_is_equal(mm.may, str));
3560 isl_map_free(mm.must);
3561 isl_map_free(mm.may);
3562 isl_flow_free(flow);
3565 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3566 map = isl_map_read_from_str(ctx, str);
3567 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3569 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3570 map = isl_map_read_from_str(ctx, str);
3571 ai = isl_access_info_add_source(ai, map, 0, &depth);
3573 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3574 map = isl_map_read_from_str(ctx, str);
3575 ai = isl_access_info_add_source(ai, map, 0, &depth);
3577 flow = isl_access_info_compute_flow(ai);
3578 space = isl_space_alloc(ctx, 0, 3, 3);
3579 mm.must = isl_map_empty(isl_space_copy(space));
3580 mm.may = isl_map_empty(space);
3582 isl_flow_foreach(flow, collect_must_may, &mm);
3584 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
3585 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3586 assert(map_is_equal(mm.may, str));
3587 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3588 assert(map_is_equal(mm.must, str));
3590 isl_map_free(mm.must);
3591 isl_map_free(mm.may);
3592 isl_flow_free(flow);
3595 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
3596 map = isl_map_read_from_str(ctx, str);
3597 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3599 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3600 map = isl_map_read_from_str(ctx, str);
3601 ai = isl_access_info_add_source(ai, map, 0, &depth);
3603 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
3604 map = isl_map_read_from_str(ctx, str);
3605 ai = isl_access_info_add_source(ai, map, 0, &depth);
3607 flow = isl_access_info_compute_flow(ai);
3608 space = isl_space_alloc(ctx, 0, 3, 3);
3609 mm.must = isl_map_empty(isl_space_copy(space));
3610 mm.may = isl_map_empty(space);
3612 isl_flow_foreach(flow, collect_must_may, &mm);
3614 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
3615 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
3616 assert(map_is_equal(mm.may, str));
3617 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3618 assert(map_is_equal(mm.must, str));
3620 isl_map_free(mm.must);
3621 isl_map_free(mm.may);
3622 isl_flow_free(flow);
3625 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
3626 map = isl_map_read_from_str(ctx, str);
3627 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3629 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3630 map = isl_map_read_from_str(ctx, str);
3631 ai = isl_access_info_add_source(ai, map, 0, &depth);
3633 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
3634 map = isl_map_read_from_str(ctx, str);
3635 ai = isl_access_info_add_source(ai, map, 0, &depth);
3637 flow = isl_access_info_compute_flow(ai);
3638 space = isl_space_alloc(ctx, 0, 3, 3);
3639 mm.must = isl_map_empty(isl_space_copy(space));
3640 mm.may = isl_map_empty(space);
3642 isl_flow_foreach(flow, collect_must_may, &mm);
3644 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
3645 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
3646 assert(map_is_equal(mm.may, str));
3647 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3648 assert(map_is_equal(mm.must, str));
3650 isl_map_free(mm.must);
3651 isl_map_free(mm.may);
3652 isl_flow_free(flow);
3655 depth = 5;
3657 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3658 map = isl_map_read_from_str(ctx, str);
3659 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
3661 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3662 map = isl_map_read_from_str(ctx, str);
3663 ai = isl_access_info_add_source(ai, map, 1, &depth);
3665 flow = isl_access_info_compute_flow(ai);
3666 space = isl_space_alloc(ctx, 0, 5, 5);
3667 mm.must = isl_map_empty(isl_space_copy(space));
3668 mm.may = isl_map_empty(space);
3670 isl_flow_foreach(flow, collect_must_may, &mm);
3672 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
3673 assert(map_is_equal(mm.must, str));
3674 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
3675 assert(map_is_equal(mm.may, str));
3677 isl_map_free(mm.must);
3678 isl_map_free(mm.may);
3679 isl_flow_free(flow);
3681 return 0;
3684 /* Check that the dependence analysis proceeds without errors.
3685 * Earlier versions of isl would break down during the analysis
3686 * due to the use of the wrong spaces.
3688 static int test_flow(isl_ctx *ctx)
3690 const char *str;
3691 isl_union_map *access, *schedule;
3692 isl_union_map *must_dep, *may_dep;
3693 int r;
3695 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
3696 access = isl_union_map_read_from_str(ctx, str);
3697 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
3698 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
3699 "S2[] -> [1,0,0,0]; "
3700 "S3[] -> [-1,0,0,0] }";
3701 schedule = isl_union_map_read_from_str(ctx, str);
3702 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
3703 isl_union_map_copy(access), schedule,
3704 &must_dep, &may_dep, NULL, NULL);
3705 isl_union_map_free(may_dep);
3706 isl_union_map_free(must_dep);
3708 return r;
3711 struct {
3712 const char *map;
3713 int sv;
3714 } sv_tests[] = {
3715 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
3716 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
3717 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
3718 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
3719 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
3720 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
3721 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3722 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3723 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
3726 int test_sv(isl_ctx *ctx)
3728 isl_union_map *umap;
3729 int i;
3730 int sv;
3732 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
3733 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
3734 sv = isl_union_map_is_single_valued(umap);
3735 isl_union_map_free(umap);
3736 if (sv < 0)
3737 return -1;
3738 if (sv_tests[i].sv && !sv)
3739 isl_die(ctx, isl_error_internal,
3740 "map not detected as single valued", return -1);
3741 if (!sv_tests[i].sv && sv)
3742 isl_die(ctx, isl_error_internal,
3743 "map detected as single valued", return -1);
3746 return 0;
3749 struct {
3750 const char *str;
3751 int bijective;
3752 } bijective_tests[] = {
3753 { "[N,M]->{[i,j] -> [i]}", 0 },
3754 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
3755 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
3756 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
3757 { "[N,M]->{[i,j] -> [j,i]}", 1 },
3758 { "[N,M]->{[i,j] -> [i+j]}", 0 },
3759 { "[N,M]->{[i,j] -> []}", 0 },
3760 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
3761 { "[N,M]->{[i,j] -> [2i]}", 0 },
3762 { "[N,M]->{[i,j] -> [i,i]}", 0 },
3763 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
3764 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
3765 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
3768 static int test_bijective(struct isl_ctx *ctx)
3770 isl_map *map;
3771 int i;
3772 int bijective;
3774 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
3775 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
3776 bijective = isl_map_is_bijective(map);
3777 isl_map_free(map);
3778 if (bijective < 0)
3779 return -1;
3780 if (bijective_tests[i].bijective && !bijective)
3781 isl_die(ctx, isl_error_internal,
3782 "map not detected as bijective", return -1);
3783 if (!bijective_tests[i].bijective && bijective)
3784 isl_die(ctx, isl_error_internal,
3785 "map detected as bijective", return -1);
3788 return 0;
3791 /* Inputs for isl_pw_qpolynomial_gist tests.
3792 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
3794 struct {
3795 const char *pwqp;
3796 const char *set;
3797 const char *gist;
3798 } pwqp_gist_tests[] = {
3799 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
3800 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
3801 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
3802 "{ [i] -> -1/2 + 1/2 * i }" },
3803 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
3804 { "{ [i] -> i^2 : i > 0; [i] -> i^2 : i < 0 }", "{ [i] : i != 0 }",
3805 "{ [i] -> i^2 }" },
3808 /* Perform some basic isl_pw_qpolynomial_gist tests.
3810 static isl_stat test_pwqp_gist(isl_ctx *ctx)
3812 int i;
3813 const char *str;
3814 isl_set *set;
3815 isl_pw_qpolynomial *pwqp1, *pwqp2;
3816 isl_bool equal;
3818 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
3819 str = pwqp_gist_tests[i].pwqp;
3820 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3821 str = pwqp_gist_tests[i].set;
3822 set = isl_set_read_from_str(ctx, str);
3823 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
3824 str = pwqp_gist_tests[i].gist;
3825 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3826 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3827 equal = isl_pw_qpolynomial_is_zero(pwqp1);
3828 isl_pw_qpolynomial_free(pwqp1);
3830 if (equal < 0)
3831 return isl_stat_error;
3832 if (!equal)
3833 isl_die(ctx, isl_error_unknown,
3834 "unexpected result", return isl_stat_error);
3837 return isl_stat_ok;
3840 /* Perform a basic isl_pw_qpolynomial_max test.
3842 static isl_stat test_pwqp_max(isl_ctx *ctx)
3844 const char *str;
3845 isl_pw_qpolynomial *pwqp;
3846 isl_val *v;
3847 int ok;
3849 str = "{ [x=2:9, y] -> floor((x + 1)/4)^3 - floor((2x)/3)^2 }";
3850 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3851 v = isl_pw_qpolynomial_max(pwqp);
3852 ok = isl_val_cmp_si(v, -1) == 0;
3853 isl_val_free(v);
3855 if (!v)
3856 return isl_stat_error;
3857 if (!ok)
3858 isl_die(ctx, isl_error_unknown, "unexpected maximum",
3859 return isl_stat_error);
3861 return isl_stat_ok;
3864 static int test_pwqp(struct isl_ctx *ctx)
3866 const char *str;
3867 isl_set *set;
3868 isl_pw_qpolynomial *pwqp1, *pwqp2;
3869 int equal;
3871 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3872 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3874 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
3875 isl_dim_in, 1, 1);
3877 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3878 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3880 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3882 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3884 isl_pw_qpolynomial_free(pwqp1);
3886 if (test_pwqp_gist(ctx) < 0)
3887 return -1;
3889 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
3890 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3891 str = "{ [i] -> ([(2 * [i/2])/5]) }";
3892 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3894 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3896 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3898 isl_pw_qpolynomial_free(pwqp1);
3900 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3901 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3902 str = "{ [x] -> x }";
3903 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3905 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3907 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3909 isl_pw_qpolynomial_free(pwqp1);
3911 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3912 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3913 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3914 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3915 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3916 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3917 isl_pw_qpolynomial_free(pwqp1);
3919 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3920 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3921 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3922 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3923 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3924 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3925 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3926 isl_pw_qpolynomial_free(pwqp1);
3927 isl_pw_qpolynomial_free(pwqp2);
3928 if (equal < 0)
3929 return -1;
3930 if (!equal)
3931 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3933 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3934 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3935 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3936 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3937 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3938 isl_val_one(ctx));
3939 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3940 isl_pw_qpolynomial_free(pwqp1);
3941 isl_pw_qpolynomial_free(pwqp2);
3942 if (equal < 0)
3943 return -1;
3944 if (!equal)
3945 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3947 if (test_pwqp_max(ctx) < 0)
3948 return -1;
3950 return 0;
3953 static int test_split_periods(isl_ctx *ctx)
3955 const char *str;
3956 isl_pw_qpolynomial *pwqp;
3958 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3959 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3960 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3961 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3963 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3965 isl_pw_qpolynomial_free(pwqp);
3967 if (!pwqp)
3968 return -1;
3970 return 0;
3973 static int test_union(isl_ctx *ctx)
3975 const char *str;
3976 isl_union_set *uset1, *uset2;
3977 isl_union_map *umap1, *umap2;
3978 int equal;
3980 str = "{ [i] : 0 <= i <= 1 }";
3981 uset1 = isl_union_set_read_from_str(ctx, str);
3982 str = "{ [1] -> [0] }";
3983 umap1 = isl_union_map_read_from_str(ctx, str);
3985 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3986 equal = isl_union_map_is_equal(umap1, umap2);
3988 isl_union_map_free(umap1);
3989 isl_union_map_free(umap2);
3991 if (equal < 0)
3992 return -1;
3993 if (!equal)
3994 isl_die(ctx, isl_error_unknown, "union maps not equal",
3995 return -1);
3997 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3998 umap1 = isl_union_map_read_from_str(ctx, str);
3999 str = "{ A[i]; B[i] }";
4000 uset1 = isl_union_set_read_from_str(ctx, str);
4002 uset2 = isl_union_map_domain(umap1);
4004 equal = isl_union_set_is_equal(uset1, uset2);
4006 isl_union_set_free(uset1);
4007 isl_union_set_free(uset2);
4009 if (equal < 0)
4010 return -1;
4011 if (!equal)
4012 isl_die(ctx, isl_error_unknown, "union sets not equal",
4013 return -1);
4015 return 0;
4018 /* Inputs for basic isl_pw_qpolynomial_bound tests.
4019 * "type" is the type of bound that should be computed.
4020 * "poly" is a string representation of the input.
4021 * "bound" is a string representation of the expected result.
4022 * "tight" is set if the result is expected to be tight.
4024 static struct {
4025 int tight;
4026 enum isl_fold type;
4027 const char *poly;
4028 const char *bound;
4029 } bound_tests[] = {
4030 /* Check that computing a bound of a non-zero polynomial
4031 * over an unbounded domain does not produce a rational value.
4032 * In particular, check that the upper bound is infinity.
4034 { 0, isl_fold_max, "{ [m, n] -> -m * n }", "{ max(infty) }" },
4035 { 1, isl_fold_max, "{ [[a, b, c, d] -> [e]] -> 0 }",
4036 "{ [a, b, c, d] -> max(0) }" },
4037 { 1, isl_fold_max, "{ [[x] -> [x]] -> 1 : exists a : x = 2 a }",
4038 "{ [x] -> max(1) : x mod 2 = 0 }" },
4039 { 1, isl_fold_min, "{ [x=5:10] -> (x + 2)^2 }", "{ min(49) }" },
4040 { 1, isl_fold_max, "{ [0:10] -> 1 }", "{ max(1) }" },
4041 { 1, isl_fold_max, "{ [[m] -> [0:m]] -> m^2 }",
4042 "{ [m] -> max(m^2) : m >= 0 }" },
4043 { 1, isl_fold_max, "{ [[a=0:1] -> [b=0:1]] -> (floor((a + b)/2)) }",
4044 "{ [a=0:1] -> max(a) }" },
4047 /* Check that the bound computation can handle differences
4048 * in domain dimension names of the input polynomial and its domain.
4050 static isl_stat test_bound_space(isl_ctx *ctx)
4052 const char *str;
4053 isl_set *set;
4054 isl_pw_qpolynomial *pwqp;
4055 isl_pw_qpolynomial_fold *pwf;
4057 str = "{ [[c] -> [c]] }";
4058 set = isl_set_read_from_str(ctx, str);
4059 str = "{ [[a] -> [b]] -> 1 }";
4060 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
4061 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
4062 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
4063 isl_pw_qpolynomial_fold_free(pwf);
4065 return isl_stat_non_null(pwf);
4068 /* Perform basic isl_pw_qpolynomial_bound tests.
4070 static int test_bound(isl_ctx *ctx)
4072 int i;
4074 if (test_bound_space(ctx) < 0)
4075 return -1;
4077 for (i = 0; i < ARRAY_SIZE(bound_tests); ++i) {
4078 const char *str;
4079 enum isl_fold type;
4080 isl_bool equal, tight;
4081 isl_pw_qpolynomial *pwqp;
4082 isl_pw_qpolynomial_fold *pwf1, *pwf2;
4084 str = bound_tests[i].poly;
4085 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
4086 type = bound_tests[i].type;
4087 pwf1 = isl_pw_qpolynomial_bound(pwqp, type, &tight);
4088 str = bound_tests[i].bound;
4089 pwf2 = isl_pw_qpolynomial_fold_read_from_str(ctx, str);
4090 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf1, pwf2);
4091 isl_pw_qpolynomial_fold_free(pwf2);
4092 isl_pw_qpolynomial_fold_free(pwf1);
4093 if (equal < 0)
4094 return -1;
4095 if (!equal)
4096 isl_die(ctx, isl_error_unknown,
4097 "incorrect bound result", return -1);
4098 if (bound_tests[i].tight && !tight)
4099 isl_die(ctx, isl_error_unknown,
4100 "bound unexpectedly not tight", return -1);
4103 return 0;
4106 /* isl_set is defined to isl_map internally, so the corresponding elements
4107 * are isl_basic_map objects.
4109 #undef EL_BASE
4110 #undef SET_BASE
4111 #define EL_BASE basic_map
4112 #define SET_BASE set
4113 #include "isl_test_list_templ.c"
4115 #undef EL_BASE
4116 #undef SET_BASE
4117 #define EL_BASE basic_set
4118 #define SET_BASE union_set
4119 #include "isl_test_list_templ.c"
4121 #undef EL_BASE
4122 #undef SET_BASE
4123 #define EL_BASE set
4124 #define SET_BASE union_set
4125 #include "isl_test_list_templ.c"
4127 #undef EL_BASE
4128 #undef SET_BASE
4129 #define EL_BASE basic_map
4130 #define SET_BASE map
4131 #include "isl_test_list_templ.c"
4133 #undef EL_BASE
4134 #undef SET_BASE
4135 #define EL_BASE map
4136 #define SET_BASE union_map
4137 #include "isl_test_list_templ.c"
4139 /* Check that the conversion from isl objects to lists works as expected.
4141 static int test_get_list(isl_ctx *ctx)
4143 if (test_get_list_basic_map_from_set(ctx, "{ [0]; [2]; [3] }"))
4144 return -1;
4145 if (test_get_list_basic_set_from_union_set(ctx, "{ A[0]; B[2]; B[3] }"))
4146 return -1;
4147 if (test_get_list_set_from_union_set(ctx, "{ A[0]; A[2]; B[3] }"))
4148 return -1;
4149 if (test_get_list_basic_map_from_map(ctx,
4150 "{ [0] -> [0]; [2] -> [0]; [3] -> [0] }"))
4151 return -1;
4152 if (test_get_list_map_from_union_map(ctx,
4153 "{ A[0] -> [0]; A[2] -> [0]; B[3] -> [0] }"))
4154 return -1;
4156 return 0;
4159 static int test_lift(isl_ctx *ctx)
4161 const char *str;
4162 isl_basic_map *bmap;
4163 isl_basic_set *bset;
4165 str = "{ [i0] : exists e0 : i0 = 4e0 }";
4166 bset = isl_basic_set_read_from_str(ctx, str);
4167 bset = isl_basic_set_lift(bset);
4168 bmap = isl_basic_map_from_range(bset);
4169 bset = isl_basic_map_domain(bmap);
4170 isl_basic_set_free(bset);
4172 return 0;
4175 /* Check that isl_set_is_subset is not confused by identical
4176 * integer divisions.
4177 * The call to isl_set_normalize ensures that the equality constraints
4178 * a = b = 0 are discovered, turning e0 and e1 into identical
4179 * integer divisions. Any further simplification would remove
4180 * the duplicate integer divisions.
4182 static isl_stat test_subset_duplicate_integer_divisions(isl_ctx *ctx)
4184 const char *str;
4185 isl_bool is_subset;
4186 isl_set *set1, *set2;
4188 str = "{ [a, b, c, d] : "
4189 "exists (e0 = floor((a + d)/4), e1 = floor((d)/4), "
4190 "e2 = floor((-a - d + 4 *floor((a + d)/4))/10), "
4191 "e3 = floor((-d + 4*floor((d)/4))/10): "
4192 "10e2 = -a - 2c - d + 4e0 and 10e3 = -2c - d + 4e1 and "
4193 "b >= 0 and a <= 0 and b <= a) }";
4194 set1 = isl_set_read_from_str(ctx, str);
4195 set2 = isl_set_read_from_str(ctx, str);
4196 set2 = isl_set_normalize(set2);
4198 is_subset = isl_set_is_subset(set1, set2);
4200 isl_set_free(set1);
4201 isl_set_free(set2);
4203 if (is_subset < 0)
4204 return isl_stat_error;
4205 if (!is_subset)
4206 isl_die(ctx, isl_error_unknown,
4207 "set is not considered to be a subset of itself",
4208 return isl_stat_error);
4210 return isl_stat_ok;
4213 struct {
4214 const char *set1;
4215 const char *set2;
4216 int subset;
4217 } subset_tests[] = {
4218 { "{ [112, 0] }",
4219 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
4220 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
4221 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
4222 { "{ [65] }",
4223 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
4224 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
4225 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
4226 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
4227 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
4228 "256e0 <= 255i and 256e0 >= -255 + 255i and "
4229 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
4230 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
4231 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
4232 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
4233 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
4234 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
4235 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
4236 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
4237 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
4238 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
4239 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
4240 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
4241 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
4242 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
4243 "4e0 <= -57 + i0 + i1)) or "
4244 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
4245 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
4246 "4e0 >= -61 + i0 + i1)) or "
4247 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
4248 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
4251 static int test_subset(isl_ctx *ctx)
4253 int i;
4254 isl_set *set1, *set2;
4255 int subset;
4257 if (test_subset_duplicate_integer_divisions(ctx) < 0)
4258 return -1;
4260 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
4261 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
4262 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
4263 subset = isl_set_is_subset(set1, set2);
4264 isl_set_free(set1);
4265 isl_set_free(set2);
4266 if (subset < 0)
4267 return -1;
4268 if (subset != subset_tests[i].subset)
4269 isl_die(ctx, isl_error_unknown,
4270 "incorrect subset result", return -1);
4273 return 0;
4276 /* Perform a set subtraction with a set that has a non-obviously empty disjunct.
4277 * Older versions of isl would fail on such cases.
4279 static isl_stat test_subtract_empty(isl_ctx *ctx)
4281 const char *str;
4282 isl_set *s1, *s2;
4284 s1 = isl_set_read_from_str(ctx, "{ [0] }");
4285 str = "{ [a] : (exists (e0, e1, e2: 1056e1 <= 32 + a - 33e0 and "
4286 "1089e1 >= a - 33e0 and 1089e1 <= 1 + a - 33e0 and "
4287 "33e2 >= -a + 33e0 + 1056e1 and "
4288 "33e2 < -2a + 66e0 + 2112e1)) or a = 0 }";
4289 s2 = isl_set_read_from_str(ctx, str);
4290 s1 = isl_set_subtract(s1, s2);
4291 isl_set_free(s1);
4293 return isl_stat_non_null(s1);
4296 struct {
4297 const char *minuend;
4298 const char *subtrahend;
4299 const char *difference;
4300 } subtract_domain_tests[] = {
4301 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
4302 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
4303 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
4306 static int test_subtract(isl_ctx *ctx)
4308 int i;
4309 isl_union_map *umap1, *umap2;
4310 isl_union_pw_multi_aff *upma1, *upma2;
4311 isl_union_set *uset;
4312 int equal;
4314 if (test_subtract_empty(ctx) < 0)
4315 return -1;
4317 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
4318 umap1 = isl_union_map_read_from_str(ctx,
4319 subtract_domain_tests[i].minuend);
4320 uset = isl_union_set_read_from_str(ctx,
4321 subtract_domain_tests[i].subtrahend);
4322 umap2 = isl_union_map_read_from_str(ctx,
4323 subtract_domain_tests[i].difference);
4324 umap1 = isl_union_map_subtract_domain(umap1, uset);
4325 equal = isl_union_map_is_equal(umap1, umap2);
4326 isl_union_map_free(umap1);
4327 isl_union_map_free(umap2);
4328 if (equal < 0)
4329 return -1;
4330 if (!equal)
4331 isl_die(ctx, isl_error_unknown,
4332 "incorrect subtract domain result", return -1);
4335 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
4336 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4337 subtract_domain_tests[i].minuend);
4338 uset = isl_union_set_read_from_str(ctx,
4339 subtract_domain_tests[i].subtrahend);
4340 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4341 subtract_domain_tests[i].difference);
4342 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
4343 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
4344 isl_union_pw_multi_aff_free(upma1);
4345 isl_union_pw_multi_aff_free(upma2);
4346 if (equal < 0)
4347 return -1;
4348 if (!equal)
4349 isl_die(ctx, isl_error_unknown,
4350 "incorrect subtract domain result", return -1);
4353 return 0;
4356 /* Check that intersecting the empty basic set with another basic set
4357 * does not increase the number of constraints. In particular,
4358 * the empty basic set should maintain its canonical representation.
4360 static int test_intersect_1(isl_ctx *ctx)
4362 isl_size n1, n2;
4363 isl_basic_set *bset1, *bset2;
4365 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
4366 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
4367 n1 = isl_basic_set_n_constraint(bset1);
4368 bset1 = isl_basic_set_intersect(bset1, bset2);
4369 n2 = isl_basic_set_n_constraint(bset1);
4370 isl_basic_set_free(bset1);
4371 if (n1 < 0 || n2 < 0)
4372 return -1;
4373 if (n1 != n2)
4374 isl_die(ctx, isl_error_unknown,
4375 "number of constraints of empty set changed",
4376 return -1);
4378 return 0;
4381 /* Check that intersecting a set with itself does not cause
4382 * an explosion in the number of disjuncts.
4384 static isl_stat test_intersect_2(isl_ctx *ctx)
4386 int i;
4387 isl_set *set;
4389 set = isl_set_read_from_str(ctx, "{ [x,y] : x >= 0 or y >= 0 }");
4390 for (i = 0; i < 100; ++i)
4391 set = isl_set_intersect(set, isl_set_copy(set));
4392 isl_set_free(set);
4393 if (!set)
4394 return isl_stat_error;
4395 return isl_stat_ok;
4398 /* Perform some intersection tests.
4400 static int test_intersect(isl_ctx *ctx)
4402 if (test_intersect_1(ctx) < 0)
4403 return -1;
4404 if (test_intersect_2(ctx) < 0)
4405 return -1;
4407 return 0;
4410 int test_factorize(isl_ctx *ctx)
4412 const char *str;
4413 isl_basic_set *bset;
4414 isl_factorizer *f;
4416 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
4417 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
4418 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
4419 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
4420 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
4421 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
4422 "3i5 >= -2i0 - i2 + 3i4 }";
4423 bset = isl_basic_set_read_from_str(ctx, str);
4424 f = isl_basic_set_factorizer(bset);
4425 isl_basic_set_free(bset);
4426 isl_factorizer_free(f);
4427 if (!f)
4428 isl_die(ctx, isl_error_unknown,
4429 "failed to construct factorizer", return -1);
4431 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
4432 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
4433 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
4434 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
4435 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
4436 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
4437 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
4438 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
4439 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
4440 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
4441 bset = isl_basic_set_read_from_str(ctx, str);
4442 f = isl_basic_set_factorizer(bset);
4443 isl_basic_set_free(bset);
4444 isl_factorizer_free(f);
4445 if (!f)
4446 isl_die(ctx, isl_error_unknown,
4447 "failed to construct factorizer", return -1);
4449 return 0;
4452 static isl_stat check_injective(__isl_take isl_map *map, void *user)
4454 int *injective = user;
4456 *injective = isl_map_is_injective(map);
4457 isl_map_free(map);
4459 if (*injective < 0 || !*injective)
4460 return isl_stat_error;
4462 return isl_stat_ok;
4465 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
4466 const char *r, const char *s, int tilable, int parallel)
4468 int i;
4469 isl_union_set *D;
4470 isl_union_map *W, *R, *S;
4471 isl_union_map *empty;
4472 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
4473 isl_union_map *validity, *proximity, *coincidence;
4474 isl_union_map *schedule;
4475 isl_union_map *test;
4476 isl_union_set *delta;
4477 isl_union_set *domain;
4478 isl_set *delta_set;
4479 isl_set *slice;
4480 isl_set *origin;
4481 isl_schedule_constraints *sc;
4482 isl_schedule *sched;
4483 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
4484 isl_size n;
4486 D = isl_union_set_read_from_str(ctx, d);
4487 W = isl_union_map_read_from_str(ctx, w);
4488 R = isl_union_map_read_from_str(ctx, r);
4489 S = isl_union_map_read_from_str(ctx, s);
4491 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
4492 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
4494 empty = isl_union_map_empty(isl_union_map_get_space(S));
4495 isl_union_map_compute_flow(isl_union_map_copy(R),
4496 isl_union_map_copy(W), empty,
4497 isl_union_map_copy(S),
4498 &dep_raw, NULL, NULL, NULL);
4499 isl_union_map_compute_flow(isl_union_map_copy(W),
4500 isl_union_map_copy(W),
4501 isl_union_map_copy(R),
4502 isl_union_map_copy(S),
4503 &dep_waw, &dep_war, NULL, NULL);
4505 dep = isl_union_map_union(dep_waw, dep_war);
4506 dep = isl_union_map_union(dep, dep_raw);
4507 validity = isl_union_map_copy(dep);
4508 coincidence = isl_union_map_copy(dep);
4509 proximity = isl_union_map_copy(dep);
4511 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
4512 sc = isl_schedule_constraints_set_validity(sc, validity);
4513 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
4514 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4515 sched = isl_schedule_constraints_compute_schedule(sc);
4516 schedule = isl_schedule_get_map(sched);
4517 isl_schedule_free(sched);
4518 isl_union_map_free(W);
4519 isl_union_map_free(R);
4520 isl_union_map_free(S);
4522 is_injection = 1;
4523 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
4525 domain = isl_union_map_domain(isl_union_map_copy(schedule));
4526 is_complete = isl_union_set_is_subset(D, domain);
4527 isl_union_set_free(D);
4528 isl_union_set_free(domain);
4530 test = isl_union_map_reverse(isl_union_map_copy(schedule));
4531 test = isl_union_map_apply_range(test, dep);
4532 test = isl_union_map_apply_range(test, schedule);
4534 delta = isl_union_map_deltas(test);
4535 n = isl_union_set_n_set(delta);
4536 if (n < 0) {
4537 isl_union_set_free(delta);
4538 return -1;
4540 if (n == 0) {
4541 is_tilable = 1;
4542 is_parallel = 1;
4543 is_nonneg = 1;
4544 isl_union_set_free(delta);
4545 } else {
4546 isl_size dim;
4548 delta_set = isl_set_from_union_set(delta);
4550 slice = isl_set_universe(isl_set_get_space(delta_set));
4551 for (i = 0; i < tilable; ++i)
4552 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
4553 is_tilable = isl_set_is_subset(delta_set, slice);
4554 isl_set_free(slice);
4556 slice = isl_set_universe(isl_set_get_space(delta_set));
4557 for (i = 0; i < parallel; ++i)
4558 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
4559 is_parallel = isl_set_is_subset(delta_set, slice);
4560 isl_set_free(slice);
4562 origin = isl_set_universe(isl_set_get_space(delta_set));
4563 dim = isl_set_dim(origin, isl_dim_set);
4564 if (dim < 0)
4565 origin = isl_set_free(origin);
4566 for (i = 0; i < dim; ++i)
4567 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
4569 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
4570 delta_set = isl_set_lexmin(delta_set);
4572 is_nonneg = isl_set_is_equal(delta_set, origin);
4574 isl_set_free(origin);
4575 isl_set_free(delta_set);
4578 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
4579 is_injection < 0 || is_complete < 0)
4580 return -1;
4581 if (!is_complete)
4582 isl_die(ctx, isl_error_unknown,
4583 "generated schedule incomplete", return -1);
4584 if (!is_injection)
4585 isl_die(ctx, isl_error_unknown,
4586 "generated schedule not injective on each statement",
4587 return -1);
4588 if (!is_nonneg)
4589 isl_die(ctx, isl_error_unknown,
4590 "negative dependences in generated schedule",
4591 return -1);
4592 if (!is_tilable)
4593 isl_die(ctx, isl_error_unknown,
4594 "generated schedule not as tilable as expected",
4595 return -1);
4596 if (!is_parallel)
4597 isl_die(ctx, isl_error_unknown,
4598 "generated schedule not as parallel as expected",
4599 return -1);
4601 return 0;
4604 /* Compute a schedule for the given instance set, validity constraints,
4605 * proximity constraints and context and return a corresponding union map
4606 * representation.
4608 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
4609 const char *domain, const char *validity, const char *proximity,
4610 const char *context)
4612 isl_set *con;
4613 isl_union_set *dom;
4614 isl_union_map *dep;
4615 isl_union_map *prox;
4616 isl_schedule_constraints *sc;
4617 isl_schedule *schedule;
4618 isl_union_map *sched;
4620 con = isl_set_read_from_str(ctx, context);
4621 dom = isl_union_set_read_from_str(ctx, domain);
4622 dep = isl_union_map_read_from_str(ctx, validity);
4623 prox = isl_union_map_read_from_str(ctx, proximity);
4624 sc = isl_schedule_constraints_on_domain(dom);
4625 sc = isl_schedule_constraints_set_context(sc, con);
4626 sc = isl_schedule_constraints_set_validity(sc, dep);
4627 sc = isl_schedule_constraints_set_proximity(sc, prox);
4628 schedule = isl_schedule_constraints_compute_schedule(sc);
4629 sched = isl_schedule_get_map(schedule);
4630 isl_schedule_free(schedule);
4632 return sched;
4635 /* Compute a schedule for the given instance set, validity constraints and
4636 * proximity constraints and return a corresponding union map representation.
4638 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
4639 const char *domain, const char *validity, const char *proximity)
4641 return compute_schedule_with_context(ctx, domain, validity, proximity,
4642 "{ : }");
4645 /* Check that a schedule can be constructed on the given domain
4646 * with the given validity and proximity constraints.
4648 static int test_has_schedule(isl_ctx *ctx, const char *domain,
4649 const char *validity, const char *proximity)
4651 isl_union_map *sched;
4653 sched = compute_schedule(ctx, domain, validity, proximity);
4654 if (!sched)
4655 return -1;
4657 isl_union_map_free(sched);
4658 return 0;
4661 int test_special_schedule(isl_ctx *ctx, const char *domain,
4662 const char *validity, const char *proximity, const char *expected_sched)
4664 isl_union_map *sched1, *sched2;
4665 int equal;
4667 sched1 = compute_schedule(ctx, domain, validity, proximity);
4668 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
4670 equal = isl_union_map_is_equal(sched1, sched2);
4671 isl_union_map_free(sched1);
4672 isl_union_map_free(sched2);
4674 if (equal < 0)
4675 return -1;
4676 if (!equal)
4677 isl_die(ctx, isl_error_unknown, "unexpected schedule",
4678 return -1);
4680 return 0;
4683 /* Check that the schedule map is properly padded, i.e., that the range
4684 * lives in a single space.
4686 static int test_padded_schedule(isl_ctx *ctx)
4688 const char *str;
4689 isl_union_set *D;
4690 isl_union_map *validity, *proximity;
4691 isl_schedule_constraints *sc;
4692 isl_schedule *sched;
4693 isl_union_map *umap;
4694 isl_union_set *range;
4695 isl_set *set;
4697 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
4698 D = isl_union_set_read_from_str(ctx, str);
4699 validity = isl_union_map_empty(isl_union_set_get_space(D));
4700 proximity = isl_union_map_copy(validity);
4701 sc = isl_schedule_constraints_on_domain(D);
4702 sc = isl_schedule_constraints_set_validity(sc, validity);
4703 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4704 sched = isl_schedule_constraints_compute_schedule(sc);
4705 umap = isl_schedule_get_map(sched);
4706 isl_schedule_free(sched);
4707 range = isl_union_map_range(umap);
4708 set = isl_set_from_union_set(range);
4709 isl_set_free(set);
4711 if (!set)
4712 return -1;
4714 return 0;
4717 /* Check that conditional validity constraints are also taken into
4718 * account across bands.
4719 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
4720 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
4721 * and then check that the adjacent order constraint C[2,1]->D[2,0]
4722 * is enforced by the rest of the schedule.
4724 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
4726 const char *str;
4727 isl_union_set *domain;
4728 isl_union_map *validity, *proximity, *condition;
4729 isl_union_map *sink, *source, *dep;
4730 isl_schedule_constraints *sc;
4731 isl_schedule *schedule;
4732 isl_union_access_info *access;
4733 isl_union_flow *flow;
4734 int empty;
4736 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4737 "A[k] : k >= 1 and k <= -1 + n; "
4738 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4739 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
4740 domain = isl_union_set_read_from_str(ctx, str);
4741 sc = isl_schedule_constraints_on_domain(domain);
4742 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
4743 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4744 "D[k, i] -> C[1 + k, i] : "
4745 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4746 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
4747 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
4748 validity = isl_union_map_read_from_str(ctx, str);
4749 sc = isl_schedule_constraints_set_validity(sc, validity);
4750 str = "[n] -> { C[k, i] -> D[k, i] : "
4751 "0 <= i <= -1 + k and k <= -1 + n }";
4752 proximity = isl_union_map_read_from_str(ctx, str);
4753 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4754 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
4755 "i <= -1 + k and i >= 1 and k <= -2 + n; "
4756 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
4757 "k <= -1 + n and i >= 0 and i <= -2 + k }";
4758 condition = isl_union_map_read_from_str(ctx, str);
4759 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
4760 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4761 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
4762 "i >= 0 and i <= -1 + k and k <= -1 + n and "
4763 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
4764 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
4765 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4766 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
4767 "k <= -1 + n and i >= 0 and i <= -1 + k and "
4768 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
4769 validity = isl_union_map_read_from_str(ctx, str);
4770 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4771 validity);
4772 schedule = isl_schedule_constraints_compute_schedule(sc);
4773 str = "{ D[2,0] -> [] }";
4774 sink = isl_union_map_read_from_str(ctx, str);
4775 access = isl_union_access_info_from_sink(sink);
4776 str = "{ C[2,1] -> [] }";
4777 source = isl_union_map_read_from_str(ctx, str);
4778 access = isl_union_access_info_set_must_source(access, source);
4779 access = isl_union_access_info_set_schedule(access, schedule);
4780 flow = isl_union_access_info_compute_flow(access);
4781 dep = isl_union_flow_get_must_dependence(flow);
4782 isl_union_flow_free(flow);
4783 empty = isl_union_map_is_empty(dep);
4784 isl_union_map_free(dep);
4786 if (empty < 0)
4787 return -1;
4788 if (empty)
4789 isl_die(ctx, isl_error_unknown,
4790 "conditional validity not respected", return -1);
4792 return 0;
4795 /* Check that the test for violated conditional validity constraints
4796 * is not confused by domain compression.
4797 * In particular, earlier versions of isl would apply
4798 * a schedule on the compressed domains to the original domains,
4799 * resulting in a failure to detect that the default schedule
4800 * violates the conditional validity constraints.
4802 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
4804 const char *str;
4805 isl_bool empty;
4806 isl_union_set *domain;
4807 isl_union_map *validity, *condition;
4808 isl_schedule_constraints *sc;
4809 isl_schedule *schedule;
4810 isl_union_map *umap;
4811 isl_map *map, *ge;
4813 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
4814 domain = isl_union_set_read_from_str(ctx, str);
4815 sc = isl_schedule_constraints_on_domain(domain);
4816 str = "{ B[1, i] -> A[0, i + 1] }";
4817 condition = isl_union_map_read_from_str(ctx, str);
4818 str = "{ A[0, i] -> B[1, i - 1] }";
4819 validity = isl_union_map_read_from_str(ctx, str);
4820 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4821 isl_union_map_copy(validity));
4822 schedule = isl_schedule_constraints_compute_schedule(sc);
4823 umap = isl_schedule_get_map(schedule);
4824 isl_schedule_free(schedule);
4825 validity = isl_union_map_apply_domain(validity,
4826 isl_union_map_copy(umap));
4827 validity = isl_union_map_apply_range(validity, umap);
4828 map = isl_map_from_union_map(validity);
4829 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4830 map = isl_map_intersect(map, ge);
4831 empty = isl_map_is_empty(map);
4832 isl_map_free(map);
4834 if (empty < 0)
4835 return -1;
4836 if (!empty)
4837 isl_die(ctx, isl_error_unknown,
4838 "conditional validity constraints not satisfied",
4839 return -1);
4841 return 0;
4844 /* Input for testing of schedule construction based on
4845 * conditional constraints.
4847 * domain is the iteration domain
4848 * flow are the flow dependences, which determine the validity and
4849 * proximity constraints
4850 * condition are the conditions on the conditional validity constraints
4851 * conditional_validity are the conditional validity constraints
4852 * outer_band_n is the expected number of members in the outer band
4854 struct {
4855 const char *domain;
4856 const char *flow;
4857 const char *condition;
4858 const char *conditional_validity;
4859 int outer_band_n;
4860 } live_range_tests[] = {
4861 /* Contrived example that illustrates that we need to keep
4862 * track of tagged condition dependences and
4863 * tagged conditional validity dependences
4864 * in isl_sched_edge separately.
4865 * In particular, the conditional validity constraints on A
4866 * cannot be satisfied,
4867 * but they can be ignored because there are no corresponding
4868 * condition constraints. However, we do have an additional
4869 * conditional validity constraint that maps to the same
4870 * dependence relation
4871 * as the condition constraint on B. If we did not make a distinction
4872 * between tagged condition and tagged conditional validity
4873 * dependences, then we
4874 * could end up treating this shared dependence as an condition
4875 * constraint on A, forcing a localization of the conditions,
4876 * which is impossible.
4878 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
4879 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
4880 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
4881 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4882 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4883 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
4886 /* TACO 2013 Fig. 7 */
4887 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4888 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4889 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4890 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
4891 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
4892 "0 <= i < n and 0 <= j < n - 1 }",
4893 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
4894 "0 <= i < n and 0 <= j < j' < n;"
4895 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
4896 "0 <= i < i' < n and 0 <= j,j' < n;"
4897 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
4898 "0 <= i,j,j' < n and j < j' }",
4901 /* TACO 2013 Fig. 7, without tags */
4902 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4903 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4904 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4905 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4906 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4907 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
4908 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
4909 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
4912 /* TACO 2013 Fig. 12 */
4913 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
4914 "S3[i,3] : 0 <= i <= 1 }",
4915 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
4916 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
4917 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
4918 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
4919 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4920 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
4921 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4922 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
4923 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
4924 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
4925 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
4930 /* Test schedule construction based on conditional constraints.
4931 * In particular, check the number of members in the outer band node
4932 * as an indication of whether tiling is possible or not.
4934 static int test_conditional_schedule_constraints(isl_ctx *ctx)
4936 int i;
4937 isl_union_set *domain;
4938 isl_union_map *condition;
4939 isl_union_map *flow;
4940 isl_union_map *validity;
4941 isl_schedule_constraints *sc;
4942 isl_schedule *schedule;
4943 isl_schedule_node *node;
4944 isl_size n_member;
4946 if (test_special_conditional_schedule_constraints(ctx) < 0)
4947 return -1;
4948 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
4949 return -1;
4951 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
4952 domain = isl_union_set_read_from_str(ctx,
4953 live_range_tests[i].domain);
4954 flow = isl_union_map_read_from_str(ctx,
4955 live_range_tests[i].flow);
4956 condition = isl_union_map_read_from_str(ctx,
4957 live_range_tests[i].condition);
4958 validity = isl_union_map_read_from_str(ctx,
4959 live_range_tests[i].conditional_validity);
4960 sc = isl_schedule_constraints_on_domain(domain);
4961 sc = isl_schedule_constraints_set_validity(sc,
4962 isl_union_map_copy(flow));
4963 sc = isl_schedule_constraints_set_proximity(sc, flow);
4964 sc = isl_schedule_constraints_set_conditional_validity(sc,
4965 condition, validity);
4966 schedule = isl_schedule_constraints_compute_schedule(sc);
4967 node = isl_schedule_get_root(schedule);
4968 while (node &&
4969 isl_schedule_node_get_type(node) != isl_schedule_node_band)
4970 node = isl_schedule_node_first_child(node);
4971 n_member = isl_schedule_node_band_n_member(node);
4972 isl_schedule_node_free(node);
4973 isl_schedule_free(schedule);
4975 if (!schedule || n_member < 0)
4976 return -1;
4977 if (n_member != live_range_tests[i].outer_band_n)
4978 isl_die(ctx, isl_error_unknown,
4979 "unexpected number of members in outer band",
4980 return -1);
4982 return 0;
4985 /* Check that the schedule computed for the given instance set and
4986 * dependence relation strongly satisfies the dependences.
4987 * In particular, check that no instance is scheduled before
4988 * or together with an instance on which it depends.
4989 * Earlier versions of isl would produce a schedule that
4990 * only weakly satisfies the dependences.
4992 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
4994 const char *domain, *dep;
4995 isl_union_map *D, *schedule;
4996 isl_map *map, *ge;
4997 int empty;
4999 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
5000 "A[i0] : 0 <= i0 <= 1 }";
5001 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
5002 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
5003 schedule = compute_schedule(ctx, domain, dep, dep);
5004 D = isl_union_map_read_from_str(ctx, dep);
5005 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
5006 D = isl_union_map_apply_range(D, schedule);
5007 map = isl_map_from_union_map(D);
5008 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
5009 map = isl_map_intersect(map, ge);
5010 empty = isl_map_is_empty(map);
5011 isl_map_free(map);
5013 if (empty < 0)
5014 return -1;
5015 if (!empty)
5016 isl_die(ctx, isl_error_unknown,
5017 "dependences not strongly satisfied", return -1);
5019 return 0;
5022 /* Compute a schedule for input where the instance set constraints
5023 * conflict with the context constraints.
5024 * Earlier versions of isl did not properly handle this situation.
5026 static int test_conflicting_context_schedule(isl_ctx *ctx)
5028 isl_union_map *schedule;
5029 const char *domain, *context;
5031 domain = "[n] -> { A[] : n >= 0 }";
5032 context = "[n] -> { : n < 0 }";
5033 schedule = compute_schedule_with_context(ctx,
5034 domain, "{}", "{}", context);
5035 isl_union_map_free(schedule);
5037 if (!schedule)
5038 return -1;
5040 return 0;
5043 /* Check that a set of schedule constraints that only allow for
5044 * a coalescing schedule still produces a schedule even if the user
5045 * request a non-coalescing schedule. Earlier versions of isl
5046 * would not handle this case correctly.
5048 static int test_coalescing_schedule(isl_ctx *ctx)
5050 const char *domain, *dep;
5051 isl_union_set *I;
5052 isl_union_map *D;
5053 isl_schedule_constraints *sc;
5054 isl_schedule *schedule;
5055 int treat_coalescing;
5057 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
5058 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
5059 I = isl_union_set_read_from_str(ctx, domain);
5060 D = isl_union_map_read_from_str(ctx, dep);
5061 sc = isl_schedule_constraints_on_domain(I);
5062 sc = isl_schedule_constraints_set_validity(sc, D);
5063 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
5064 isl_options_set_schedule_treat_coalescing(ctx, 1);
5065 schedule = isl_schedule_constraints_compute_schedule(sc);
5066 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
5067 isl_schedule_free(schedule);
5068 if (!schedule)
5069 return -1;
5070 return 0;
5073 /* Check that the scheduler does not perform any needless
5074 * compound skewing. Earlier versions of isl would compute
5075 * schedules in terms of transformed schedule coefficients and
5076 * would not accurately keep track of the sum of the original
5077 * schedule coefficients. It could then produce the schedule
5078 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
5079 * for the input below instead of the schedule below.
5081 static int test_skewing_schedule(isl_ctx *ctx)
5083 const char *D, *V, *P, *S;
5085 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
5086 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
5087 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
5088 "-1 <= a-i + b-j + c-k <= 1 }";
5089 P = "{ }";
5090 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
5092 return test_special_schedule(ctx, D, V, P, S);
5095 int test_schedule(isl_ctx *ctx)
5097 const char *D, *W, *R, *V, *P, *S;
5098 int max_coincidence;
5099 int treat_coalescing;
5101 /* Handle resulting schedule with zero bands. */
5102 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
5103 return -1;
5105 /* Jacobi */
5106 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
5107 W = "{ S1[t,i] -> a[t,i] }";
5108 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
5109 "S1[t,i] -> a[t-1,i+1] }";
5110 S = "{ S1[t,i] -> [t,i] }";
5111 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
5112 return -1;
5114 /* Fig. 5 of CC2008 */
5115 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
5116 "j <= -1 + N }";
5117 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
5118 "j >= 2 and j <= -1 + N }";
5119 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
5120 "j >= 2 and j <= -1 + N; "
5121 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
5122 "j >= 2 and j <= -1 + N }";
5123 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
5124 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
5125 return -1;
5127 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
5128 W = "{ S1[i] -> a[i] }";
5129 R = "{ S2[i] -> a[i+1] }";
5130 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
5131 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
5132 return -1;
5134 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
5135 W = "{ S1[i] -> a[i] }";
5136 R = "{ S2[i] -> a[9-i] }";
5137 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
5138 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
5139 return -1;
5141 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
5142 W = "{ S1[i] -> a[i] }";
5143 R = "[N] -> { S2[i] -> a[N-1-i] }";
5144 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
5145 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
5146 return -1;
5148 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
5149 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
5150 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
5151 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
5152 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5153 return -1;
5155 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
5156 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
5157 R = "{ S2[i,j] -> a[i-1,j] }";
5158 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
5159 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
5160 return -1;
5162 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
5163 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
5164 R = "{ S2[i,j] -> a[i,j-1] }";
5165 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
5166 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
5167 return -1;
5169 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
5170 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
5171 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
5172 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
5173 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
5174 "S_0[] -> [0, 0, 0] }";
5175 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
5176 return -1;
5177 ctx->opt->schedule_parametric = 0;
5178 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5179 return -1;
5180 ctx->opt->schedule_parametric = 1;
5182 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
5183 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
5184 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
5185 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
5186 "S4[i] -> a[i,N] }";
5187 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
5188 "S4[i] -> [4,i,0] }";
5189 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
5190 isl_options_set_schedule_maximize_coincidence(ctx, 0);
5191 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
5192 return -1;
5193 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
5195 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
5196 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
5197 "j <= N }";
5198 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
5199 "j <= N; "
5200 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
5201 "j <= N }";
5202 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
5203 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5204 return -1;
5206 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
5207 " S_2[t] : t >= 0 and t <= -1 + N; "
5208 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
5209 "i <= -1 + N }";
5210 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
5211 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
5212 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
5213 "i >= 0 and i <= -1 + N }";
5214 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
5215 "i >= 0 and i <= -1 + N; "
5216 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
5217 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
5218 " S_0[t] -> [0, t, 0] }";
5220 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
5221 return -1;
5222 ctx->opt->schedule_parametric = 0;
5223 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5224 return -1;
5225 ctx->opt->schedule_parametric = 1;
5227 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
5228 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
5229 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
5230 return -1;
5232 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
5233 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
5234 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
5235 "j >= 0 and j <= -1 + N; "
5236 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
5237 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
5238 "j >= 0 and j <= -1 + N; "
5239 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
5240 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
5241 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5242 return -1;
5244 D = "{ S_0[i] : i >= 0 }";
5245 W = "{ S_0[i] -> a[i] : i >= 0 }";
5246 R = "{ S_0[i] -> a[0] : i >= 0 }";
5247 S = "{ S_0[i] -> [0, i, 0] }";
5248 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5249 return -1;
5251 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
5252 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
5253 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
5254 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
5255 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5256 return -1;
5258 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
5259 "k <= -1 + n and k >= 0 }";
5260 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
5261 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
5262 "k <= -1 + n and k >= 0; "
5263 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
5264 "k <= -1 + n and k >= 0; "
5265 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
5266 "k <= -1 + n and k >= 0 }";
5267 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
5268 ctx->opt->schedule_outer_coincidence = 1;
5269 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
5270 return -1;
5271 ctx->opt->schedule_outer_coincidence = 0;
5273 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
5274 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
5275 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
5276 "Stmt_for_body24[i0, i1, 1, 0]:"
5277 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
5278 "Stmt_for_body7[i0, i1, i2]:"
5279 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
5280 "i2 <= 7 }";
5282 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
5283 "Stmt_for_body24[1, i1, i2, i3]:"
5284 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
5285 "i2 >= 1;"
5286 "Stmt_for_body24[0, i1, i2, i3] -> "
5287 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
5288 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
5289 "i3 >= 0;"
5290 "Stmt_for_body24[0, i1, i2, i3] ->"
5291 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
5292 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
5293 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
5294 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
5295 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
5296 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
5297 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
5298 "i1 <= 6 and i1 >= 0;"
5299 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
5300 "Stmt_for_body7[i0, i1, i2] -> "
5301 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
5302 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
5303 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
5304 "Stmt_for_body7[i0, i1, i2] -> "
5305 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
5306 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
5307 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
5308 P = V;
5310 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
5311 isl_options_set_schedule_treat_coalescing(ctx, 0);
5312 if (test_has_schedule(ctx, D, V, P) < 0)
5313 return -1;
5314 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
5316 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
5317 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
5318 "j >= 1 and j <= 7;"
5319 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
5320 "j >= 1 and j <= 8 }";
5321 P = "{ }";
5322 S = "{ S_0[i, j] -> [i + j, i] }";
5323 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5324 if (test_special_schedule(ctx, D, V, P, S) < 0)
5325 return -1;
5326 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5328 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
5329 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
5330 "j >= 0 and j <= -1 + i }";
5331 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
5332 "i <= -1 + N and j >= 0;"
5333 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
5334 "i <= -2 + N }";
5335 P = "{ }";
5336 S = "{ S_0[i, j] -> [i, j] }";
5337 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5338 if (test_special_schedule(ctx, D, V, P, S) < 0)
5339 return -1;
5340 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5342 /* Test both algorithms on a case with only proximity dependences. */
5343 D = "{ S[i,j] : 0 <= i <= 10 }";
5344 V = "{ }";
5345 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
5346 S = "{ S[i, j] -> [j, i] }";
5347 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5348 if (test_special_schedule(ctx, D, V, P, S) < 0)
5349 return -1;
5350 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5351 if (test_special_schedule(ctx, D, V, P, S) < 0)
5352 return -1;
5354 D = "{ A[a]; B[] }";
5355 V = "{}";
5356 P = "{ A[a] -> B[] }";
5357 if (test_has_schedule(ctx, D, V, P) < 0)
5358 return -1;
5360 if (test_padded_schedule(ctx) < 0)
5361 return -1;
5363 /* Check that check for progress is not confused by rational
5364 * solution.
5366 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
5367 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
5368 "i0 <= -2 + N; "
5369 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
5370 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
5371 P = "{}";
5372 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5373 if (test_has_schedule(ctx, D, V, P) < 0)
5374 return -1;
5375 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5377 /* Check that we allow schedule rows that are only non-trivial
5378 * on some full-dimensional domains.
5380 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
5381 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
5382 "S1[j] -> S2[1] : 0 <= j <= 1 }";
5383 P = "{}";
5384 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
5385 if (test_has_schedule(ctx, D, V, P) < 0)
5386 return -1;
5387 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
5389 if (test_conditional_schedule_constraints(ctx) < 0)
5390 return -1;
5392 if (test_strongly_satisfying_schedule(ctx) < 0)
5393 return -1;
5395 if (test_conflicting_context_schedule(ctx) < 0)
5396 return -1;
5398 if (test_coalescing_schedule(ctx) < 0)
5399 return -1;
5400 if (test_skewing_schedule(ctx) < 0)
5401 return -1;
5403 return 0;
5406 /* Perform scheduling tests using the whole component scheduler.
5408 static int test_schedule_whole(isl_ctx *ctx)
5410 int whole;
5411 int r;
5413 whole = isl_options_get_schedule_whole_component(ctx);
5414 isl_options_set_schedule_whole_component(ctx, 1);
5415 r = test_schedule(ctx);
5416 isl_options_set_schedule_whole_component(ctx, whole);
5418 return r;
5421 /* Perform scheduling tests using the incremental scheduler.
5423 static int test_schedule_incremental(isl_ctx *ctx)
5425 int whole;
5426 int r;
5428 whole = isl_options_get_schedule_whole_component(ctx);
5429 isl_options_set_schedule_whole_component(ctx, 0);
5430 r = test_schedule(ctx);
5431 isl_options_set_schedule_whole_component(ctx, whole);
5433 return r;
5436 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
5438 isl_union_map *umap;
5439 int test;
5441 umap = isl_union_map_read_from_str(ctx, str);
5442 test = isl_union_map_plain_is_injective(umap);
5443 isl_union_map_free(umap);
5444 if (test < 0)
5445 return -1;
5446 if (test == injective)
5447 return 0;
5448 if (injective)
5449 isl_die(ctx, isl_error_unknown,
5450 "map not detected as injective", return -1);
5451 else
5452 isl_die(ctx, isl_error_unknown,
5453 "map detected as injective", return -1);
5456 int test_injective(isl_ctx *ctx)
5458 const char *str;
5460 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
5461 return -1;
5462 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
5463 return -1;
5464 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
5465 return -1;
5466 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
5467 return -1;
5468 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
5469 return -1;
5470 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
5471 return -1;
5472 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
5473 return -1;
5474 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
5475 return -1;
5477 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
5478 if (test_plain_injective(ctx, str, 1))
5479 return -1;
5480 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
5481 if (test_plain_injective(ctx, str, 0))
5482 return -1;
5484 return 0;
5487 #undef BASE
5488 #define BASE aff
5489 #include "isl_test_plain_equal_templ.c"
5491 #undef BASE
5492 #define BASE pw_multi_aff
5493 #include "isl_test_plain_equal_templ.c"
5495 #undef BASE
5496 #define BASE union_pw_aff
5497 #include "isl_test_plain_equal_templ.c"
5499 /* Basic tests on isl_union_pw_aff.
5501 * In particular, check that isl_union_pw_aff_aff_on_domain
5502 * aligns the parameters of the input objects and
5503 * that isl_union_pw_aff_param_on_domain_id properly
5504 * introduces the parameter.
5506 static int test_upa(isl_ctx *ctx)
5508 const char *str;
5509 isl_id *id;
5510 isl_aff *aff;
5511 isl_union_set *domain;
5512 isl_union_pw_aff *upa;
5513 isl_stat ok;
5515 aff = isl_aff_read_from_str(ctx, "[N] -> { [N] }");
5516 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5517 domain = isl_union_set_read_from_str(ctx, str);
5518 upa = isl_union_pw_aff_aff_on_domain(domain, aff);
5519 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5520 ok = union_pw_aff_check_plain_equal(upa, str);
5521 isl_union_pw_aff_free(upa);
5522 if (ok < 0)
5523 return -1;
5525 id = isl_id_alloc(ctx, "N", NULL);
5526 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5527 domain = isl_union_set_read_from_str(ctx, str);
5528 upa = isl_union_pw_aff_param_on_domain_id(domain, id);
5529 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5530 ok = union_pw_aff_check_plain_equal(upa, str);
5531 isl_union_pw_aff_free(upa);
5532 if (ok < 0)
5533 return -1;
5535 return 0;
5538 struct {
5539 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5540 __isl_take isl_aff *aff2);
5541 } aff_bin_op[] = {
5542 ['+'] = { &isl_aff_add },
5543 ['-'] = { &isl_aff_sub },
5544 ['*'] = { &isl_aff_mul },
5545 ['/'] = { &isl_aff_div },
5548 struct {
5549 const char *arg1;
5550 unsigned char op;
5551 const char *arg2;
5552 const char *res;
5553 } aff_bin_tests[] = {
5554 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
5555 "{ [i] -> [2i] }" },
5556 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
5557 "{ [i] -> [0] }" },
5558 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
5559 "{ [i] -> [2i] }" },
5560 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
5561 "{ [i] -> [2i] }" },
5562 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
5563 "{ [i] -> [i/2] }" },
5564 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
5565 "{ [i] -> [i] }" },
5566 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
5567 "{ [i] -> [NaN] }" },
5568 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
5569 "{ [i] -> [NaN] }" },
5570 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
5571 "{ [i] -> [NaN] }" },
5572 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
5573 "{ [i] -> [NaN] }" },
5574 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
5575 "{ [i] -> [NaN] }" },
5576 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
5577 "{ [i] -> [NaN] }" },
5578 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
5579 "{ [i] -> [NaN] }" },
5580 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
5581 "{ [i] -> [NaN] }" },
5582 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
5583 "{ [i] -> [NaN] }" },
5584 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
5585 "{ [i] -> [NaN] }" },
5586 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
5587 "{ [i] -> [NaN] }" },
5588 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
5589 "{ [i] -> [NaN] }" },
5590 { "{ [i] -> [i] }", '/', "{ [i] -> [0] }",
5591 "{ [i] -> [NaN] }" },
5594 /* Perform some basic tests of binary operations on isl_aff objects.
5596 static int test_bin_aff(isl_ctx *ctx)
5598 int i;
5599 isl_aff *aff1, *aff2, *res;
5600 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5601 __isl_take isl_aff *aff2);
5602 int ok;
5604 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
5605 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
5606 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
5607 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
5608 fn = aff_bin_op[aff_bin_tests[i].op].fn;
5609 aff1 = fn(aff1, aff2);
5610 if (isl_aff_is_nan(res))
5611 ok = isl_aff_is_nan(aff1);
5612 else
5613 ok = isl_aff_plain_is_equal(aff1, res);
5614 isl_aff_free(aff1);
5615 isl_aff_free(res);
5616 if (ok < 0)
5617 return -1;
5618 if (!ok)
5619 isl_die(ctx, isl_error_unknown,
5620 "unexpected result", return -1);
5623 return 0;
5626 struct {
5627 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
5628 __isl_take isl_pw_aff *pa2);
5629 } pw_aff_bin_op[] = {
5630 ['m'] = { &isl_pw_aff_min },
5631 ['M'] = { &isl_pw_aff_max },
5634 /* Inputs for binary isl_pw_aff operation tests.
5635 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
5636 * defined by pw_aff_bin_op, and "res" is the expected result.
5638 struct {
5639 const char *arg1;
5640 unsigned char op;
5641 const char *arg2;
5642 const char *res;
5643 } pw_aff_bin_tests[] = {
5644 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
5645 "{ [i] -> [i] }" },
5646 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
5647 "{ [i] -> [i] }" },
5648 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
5649 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
5650 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
5651 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
5652 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
5653 "{ [i] -> [NaN] }" },
5654 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
5655 "{ [i] -> [NaN] }" },
5658 /* Perform some basic tests of binary operations on isl_pw_aff objects.
5660 static int test_bin_pw_aff(isl_ctx *ctx)
5662 int i;
5663 isl_bool ok;
5664 isl_pw_aff *pa1, *pa2, *res;
5666 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
5667 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
5668 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
5669 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
5670 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
5671 if (isl_pw_aff_involves_nan(res))
5672 ok = isl_pw_aff_involves_nan(pa1);
5673 else
5674 ok = isl_pw_aff_plain_is_equal(pa1, res);
5675 isl_pw_aff_free(pa1);
5676 isl_pw_aff_free(res);
5677 if (ok < 0)
5678 return -1;
5679 if (!ok)
5680 isl_die(ctx, isl_error_unknown,
5681 "unexpected result", return -1);
5684 return 0;
5687 /* Inputs for basic tests of test operations on
5688 * isl_union_pw_multi_aff objects.
5689 * "fn" is the function that is being tested.
5690 * "arg" is a string description of the input.
5691 * "res" is the expected result.
5693 static struct {
5694 isl_bool (*fn)(__isl_keep isl_union_pw_multi_aff *upma1);
5695 const char *arg;
5696 isl_bool res;
5697 } upma_test_tests[] = {
5698 { &isl_union_pw_multi_aff_involves_nan, "{ A[] -> [0]; B[0] -> [1] }",
5699 isl_bool_false },
5700 { &isl_union_pw_multi_aff_involves_nan, "{ A[] -> [NaN]; B[0] -> [1] }",
5701 isl_bool_true },
5702 { &isl_union_pw_multi_aff_involves_nan, "{ A[] -> [0]; B[0] -> [NaN] }",
5703 isl_bool_true },
5704 { &isl_union_pw_multi_aff_involves_nan,
5705 "{ A[] -> [0]; B[0] -> [1, NaN, 5] }",
5706 isl_bool_true },
5707 { &isl_union_pw_multi_aff_involves_locals,
5708 "{ A[] -> [0]; B[0] -> [1] }",
5709 isl_bool_false },
5710 { &isl_union_pw_multi_aff_involves_locals,
5711 "{ A[] -> [0]; B[x] -> [1] : x mod 2 = 0 }",
5712 isl_bool_true },
5713 { &isl_union_pw_multi_aff_involves_locals,
5714 "{ A[] -> [0]; B[x] -> [x // 2] }",
5715 isl_bool_true },
5716 { &isl_union_pw_multi_aff_involves_locals,
5717 "{ A[i] -> [i // 2]; B[0] -> [1] }",
5718 isl_bool_true },
5721 /* Perform some basic tests of test operations on
5722 * isl_union_pw_multi_aff objects.
5724 static isl_stat test_upma_test(isl_ctx *ctx)
5726 int i;
5727 isl_union_pw_multi_aff *upma;
5728 isl_bool res;
5730 for (i = 0; i < ARRAY_SIZE(upma_test_tests); ++i) {
5731 const char *str;
5733 str = upma_test_tests[i].arg;
5734 upma = isl_union_pw_multi_aff_read_from_str(ctx, str);
5735 res = upma_test_tests[i].fn(upma);
5736 isl_union_pw_multi_aff_free(upma);
5737 if (res < 0)
5738 return isl_stat_error;
5739 if (res != upma_test_tests[i].res)
5740 isl_die(ctx, isl_error_unknown,
5741 "unexpected result", return isl_stat_error);
5744 return isl_stat_ok;
5747 struct {
5748 __isl_give isl_union_pw_multi_aff *(*fn)(
5749 __isl_take isl_union_pw_multi_aff *upma1,
5750 __isl_take isl_union_pw_multi_aff *upma2);
5751 const char *arg1;
5752 const char *arg2;
5753 const char *res;
5754 } upma_bin_tests[] = {
5755 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
5756 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
5757 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
5758 "{ B[x] -> [2] : x >= 0 }",
5759 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
5760 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
5761 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
5762 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
5763 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
5764 "D[i] -> B[2] : i >= 5 }" },
5765 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5766 "{ B[x] -> C[2] : x > 0 }",
5767 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
5768 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5769 "{ B[x] -> A[2] : x >= 0 }",
5770 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
5772 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5773 "{ B[x] -> C[x + 2] }",
5774 "{ D[y] -> B[2y] }",
5775 "{ }" },
5777 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5778 "{ [A[x] -> B[x + 1]] -> C[x + 2] }",
5779 "{ D[y] -> B[2y] }",
5780 "{ }" },
5782 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5783 "{ [A[x] -> B[x + 1]] -> C[x + 2]; B[x] -> C[x + 2] }",
5784 "{ D[y] -> A[2y] }",
5785 "{ [D[y] -> B[2y + 1]] -> C[2y + 2] }" },
5787 &isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff,
5788 "{ T[A[x] -> B[x + 1]] -> C[x + 2]; B[x] -> C[x + 2] }",
5789 "{ D[y] -> A[2y] }",
5790 "{ T[D[y] -> B[2y + 1]] -> C[2y + 2] }" },
5793 /* Perform some basic tests of binary operations on
5794 * isl_union_pw_multi_aff objects.
5796 static int test_bin_upma(isl_ctx *ctx)
5798 int i;
5799 isl_union_pw_multi_aff *upma1, *upma2, *res;
5800 int ok;
5802 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
5803 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5804 upma_bin_tests[i].arg1);
5805 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5806 upma_bin_tests[i].arg2);
5807 res = isl_union_pw_multi_aff_read_from_str(ctx,
5808 upma_bin_tests[i].res);
5809 upma1 = upma_bin_tests[i].fn(upma1, upma2);
5810 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
5811 isl_union_pw_multi_aff_free(upma1);
5812 isl_union_pw_multi_aff_free(res);
5813 if (ok < 0)
5814 return -1;
5815 if (!ok)
5816 isl_die(ctx, isl_error_unknown,
5817 "unexpected result", return -1);
5820 return 0;
5823 struct {
5824 __isl_give isl_union_pw_multi_aff *(*fn)(
5825 __isl_take isl_union_pw_multi_aff *upma1,
5826 __isl_take isl_union_pw_multi_aff *upma2);
5827 const char *arg1;
5828 const char *arg2;
5829 } upma_bin_fail_tests[] = {
5830 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5831 "{ B[x] -> C[2] : x >= 0 }" },
5834 /* Perform some basic tests of binary operations on
5835 * isl_union_pw_multi_aff objects that are expected to fail.
5837 static int test_bin_upma_fail(isl_ctx *ctx)
5839 int i, n;
5840 isl_union_pw_multi_aff *upma1, *upma2;
5841 int on_error;
5843 on_error = isl_options_get_on_error(ctx);
5844 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
5845 n = ARRAY_SIZE(upma_bin_fail_tests);
5846 for (i = 0; i < n; ++i) {
5847 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5848 upma_bin_fail_tests[i].arg1);
5849 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5850 upma_bin_fail_tests[i].arg2);
5851 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
5852 isl_union_pw_multi_aff_free(upma1);
5853 if (upma1)
5854 break;
5856 isl_options_set_on_error(ctx, on_error);
5857 if (i < n)
5858 isl_die(ctx, isl_error_unknown,
5859 "operation not expected to succeed", return -1);
5861 return 0;
5864 /* Inputs for basic tests of binary operations on
5865 * pairs of isl_union_pw_multi_aff and isl_union_set objects.
5866 * "fn" is the function that is being tested.
5867 * "arg1" and "arg2" are string descriptions of the inputs.
5868 * "res" is a string description of the expected result.
5870 struct {
5871 __isl_give isl_union_pw_multi_aff *(*fn)(
5872 __isl_take isl_union_pw_multi_aff *upma,
5873 __isl_take isl_union_set *uset);
5874 const char *arg1;
5875 const char *arg2;
5876 const char *res;
5877 } upma_uset_tests[] = {
5878 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5879 "{ A[i] -> B[i] }", "{ B[0] }",
5880 "{ }" },
5881 { &isl_union_pw_multi_aff_intersect_domain_wrapped_domain,
5882 "{ [A[i] -> B[i]] -> C[i + 1] }", "{ A[1]; B[0] }",
5883 "{ [A[1] -> B[1]] -> C[2] }" },
5884 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5885 "{ [A[i] -> B[i]] -> C[i + 1] }", "{ A[1]; B[0] }",
5886 "{ [A[0] -> B[0]] -> C[1] }" },
5887 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5888 "{ [A[i] -> B[i]] -> C[i + 1] }", "[N] -> { B[N] }",
5889 "[N] -> { [A[N] -> B[N]] -> C[N + 1] }" },
5890 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5891 "[M] -> { [A[M] -> B[M]] -> C[M + 1] }", "[N] -> { B[N] }",
5892 "[N, M] -> { [A[N] -> B[N]] -> C[N + 1] : N = M }" },
5893 { &isl_union_pw_multi_aff_intersect_domain_wrapped_range,
5894 "{ [A[] -> B[]] -> C[]; N[A[] -> B[]] -> D[]; [B[] -> A[]] -> E[] }",
5895 "{ B[] }",
5896 "{ [A[] -> B[]] -> C[]; N[A[] -> B[]] -> D[] }" },
5899 /* Perform some basic tests of binary operations on
5900 * pairs of isl_union_pw_multi_aff and isl_union_set objects.
5902 static isl_stat test_upma_uset(isl_ctx *ctx)
5904 int i;
5905 isl_bool ok;
5906 isl_union_pw_multi_aff *upma, *res;
5907 isl_union_set *uset;
5909 for (i = 0; i < ARRAY_SIZE(upma_uset_tests); ++i) {
5910 upma = isl_union_pw_multi_aff_read_from_str(ctx,
5911 upma_uset_tests[i].arg1);
5912 uset = isl_union_set_read_from_str(ctx,
5913 upma_uset_tests[i].arg2);
5914 res = isl_union_pw_multi_aff_read_from_str(ctx,
5915 upma_uset_tests[i].res);
5916 upma = upma_uset_tests[i].fn(upma, uset);
5917 ok = isl_union_pw_multi_aff_plain_is_equal(upma, res);
5918 isl_union_pw_multi_aff_free(upma);
5919 isl_union_pw_multi_aff_free(res);
5920 if (ok < 0)
5921 return isl_stat_error;
5922 if (!ok)
5923 isl_die(ctx, isl_error_unknown,
5924 "unexpected result", return isl_stat_error);
5927 return isl_stat_ok;
5930 /* Inputs for basic tests of unary operations on isl_multi_pw_aff objects.
5931 * "fn" is the function that is tested.
5932 * "arg" is a string description of the input.
5933 * "res" is a string description of the expected result.
5935 struct {
5936 __isl_give isl_multi_pw_aff *(*fn)(__isl_take isl_multi_pw_aff *mpa);
5937 const char *arg;
5938 const char *res;
5939 } mpa_un_tests[] = {
5940 { &isl_multi_pw_aff_range_factor_domain,
5941 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5942 "{ A[x] -> B[(1 : x >= 5)] }" },
5943 { &isl_multi_pw_aff_range_factor_range,
5944 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5945 "{ A[y] -> C[(2 : y <= 10)] }" },
5946 { &isl_multi_pw_aff_range_factor_domain,
5947 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5948 "{ A[x] -> B[(1 : x >= 5)] }" },
5949 { &isl_multi_pw_aff_range_factor_range,
5950 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5951 "{ A[y] -> C[] }" },
5952 { &isl_multi_pw_aff_range_factor_domain,
5953 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5954 "{ A[x] -> B[] }" },
5955 { &isl_multi_pw_aff_range_factor_range,
5956 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5957 "{ A[y] -> C[(2 : y <= 10)] }" },
5958 { &isl_multi_pw_aff_range_factor_domain,
5959 "{ A[x] -> [B[] -> C[]] }",
5960 "{ A[x] -> B[] }" },
5961 { &isl_multi_pw_aff_range_factor_range,
5962 "{ A[x] -> [B[] -> C[]] }",
5963 "{ A[y] -> C[] }" },
5964 { &isl_multi_pw_aff_factor_range,
5965 "{ [B[] -> C[]] }",
5966 "{ C[] }" },
5967 { &isl_multi_pw_aff_range_factor_domain,
5968 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5969 "{ A[x] -> B[] : x >= 0 }" },
5970 { &isl_multi_pw_aff_range_factor_range,
5971 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5972 "{ A[y] -> C[] : y >= 0 }" },
5973 { &isl_multi_pw_aff_factor_range,
5974 "[N] -> { [B[] -> C[]] : N >= 0 }",
5975 "[N] -> { C[] : N >= 0 }" },
5978 /* Perform some basic tests of unary operations on isl_multi_pw_aff objects.
5980 static int test_un_mpa(isl_ctx *ctx)
5982 int i;
5983 isl_bool ok;
5984 isl_multi_pw_aff *mpa, *res;
5986 for (i = 0; i < ARRAY_SIZE(mpa_un_tests); ++i) {
5987 mpa = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].arg);
5988 res = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].res);
5989 mpa = mpa_un_tests[i].fn(mpa);
5990 ok = isl_multi_pw_aff_plain_is_equal(mpa, res);
5991 isl_multi_pw_aff_free(mpa);
5992 isl_multi_pw_aff_free(res);
5993 if (ok < 0)
5994 return -1;
5995 if (!ok)
5996 isl_die(ctx, isl_error_unknown,
5997 "unexpected result", return -1);
6000 return 0;
6003 /* Inputs for basic tests of binary operations on isl_multi_pw_aff objects.
6004 * "fn" is the function that is tested.
6005 * "arg1" and "arg2" are string descriptions of the inputs.
6006 * "res" is a string description of the expected result.
6008 struct {
6009 __isl_give isl_multi_pw_aff *(*fn)(
6010 __isl_take isl_multi_pw_aff *mpa1,
6011 __isl_take isl_multi_pw_aff *mpa2);
6012 const char *arg1;
6013 const char *arg2;
6014 const char *res;
6015 } mpa_bin_tests[] = {
6016 { &isl_multi_pw_aff_add, "{ A[] -> [1] }", "{ A[] -> [2] }",
6017 "{ A[] -> [3] }" },
6018 { &isl_multi_pw_aff_add, "{ A[x] -> [(1 : x >= 5)] }",
6019 "{ A[x] -> [(x : x <= 10)] }",
6020 "{ A[x] -> [(1 + x : 5 <= x <= 10)] }" },
6021 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
6022 "{ A[x] -> [] : x <= 10 }",
6023 "{ A[x] -> [] : 5 <= x <= 10 }" },
6024 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
6025 "[N] -> { A[x] -> [] : x <= N }",
6026 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
6027 { &isl_multi_pw_aff_add,
6028 "[N] -> { A[x] -> [] : x <= N }",
6029 "{ A[x] -> [] : x >= 5 }",
6030 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
6031 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
6032 "{ A[y] -> C[(2 : y <= 10)] }",
6033 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
6034 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
6035 "{ A[y] -> C[2] : y <= 10 }",
6036 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
6037 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
6038 "[N] -> { A[y] -> C[2] : y <= N }",
6039 "[N] -> { A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= N)]] }" },
6040 { &isl_multi_pw_aff_range_product, "[N] -> { A[x] -> B[1] : x >= N }",
6041 "{ A[y] -> C[2] : y <= 10 }",
6042 "[N] -> { A[x] -> [B[(1 : x >= N)] -> C[(2 : x <= 10)]] }" },
6043 { &isl_multi_pw_aff_range_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
6044 "{ A[] -> [B[1] -> C[2]] }" },
6045 { &isl_multi_pw_aff_range_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
6046 "{ A[] -> [B[] -> C[]] }" },
6047 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
6048 "{ A[y] -> C[] : y <= 10 }",
6049 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] : x <= 10 }" },
6050 { &isl_multi_pw_aff_range_product, "{ A[y] -> C[] : y <= 10 }",
6051 "{ A[x] -> B[(1 : x >= 5)] }",
6052 "{ A[x] -> [C[] -> B[(1 : x >= 5)]] : x <= 10 }" },
6053 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
6054 "{ A[y] -> C[(2 : y <= 10)] }",
6055 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[(2 : y <= 10)]] }" },
6056 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
6057 "{ A[y] -> C[] : y <= 10 }",
6058 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= 10 }" },
6059 { &isl_multi_pw_aff_product, "{ A[y] -> C[] : y <= 10 }",
6060 "{ A[x] -> B[(1 : x >= 5)] }",
6061 "{ [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= 10 }" },
6062 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
6063 "[N] -> { A[y] -> C[] : y <= N }",
6064 "[N] -> { [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= N }" },
6065 { &isl_multi_pw_aff_product, "[N] -> { A[y] -> C[] : y <= N }",
6066 "{ A[x] -> B[(1 : x >= 5)] }",
6067 "[N] -> { [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= N }" },
6068 { &isl_multi_pw_aff_product, "{ A[x] -> B[] : x >= 5 }",
6069 "{ A[y] -> C[] : y <= 10 }",
6070 "{ [A[x] -> A[y]] -> [B[] -> C[]] : x >= 5 and y <= 10 }" },
6071 { &isl_multi_pw_aff_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
6072 "{ [A[] -> A[]] -> [B[1] -> C[2]] }" },
6073 { &isl_multi_pw_aff_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
6074 "{ [A[] -> A[]] -> [B[] -> C[]] }" },
6075 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6076 "{ B[i,j] -> C[i + 2j] }", "{ A[a,b] -> B[b,a] }",
6077 "{ A[a,b] -> C[b + 2a] }" },
6078 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6079 "{ B[i,j] -> C[i + 2j] }",
6080 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6081 "{ A[a,b] -> C[(b + 2a : b > a)] }" },
6082 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6083 "{ B[i,j] -> C[(i + 2j : j > 4)] }",
6084 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6085 "{ A[a,b] -> C[(b + 2a : b > a > 4)] }" },
6086 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6087 "{ B[i,j] -> C[] }",
6088 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6089 "{ A[a,b] -> C[] }" },
6090 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6091 "{ B[i,j] -> C[] : i > j }",
6092 "{ A[a,b] -> B[b,a] }",
6093 "{ A[a,b] -> C[] : b > a }" },
6094 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6095 "{ B[i,j] -> C[] : j > 5 }",
6096 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6097 "{ A[a,b] -> C[] : b > a > 5 }" },
6098 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6099 "[N] -> { B[i,j] -> C[] : j > N }",
6100 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
6101 "[N] -> { A[a,b] -> C[] : b > a > N }" },
6102 { &isl_multi_pw_aff_pullback_multi_pw_aff,
6103 "[M,N] -> { B[] -> C[] : N > 5 }",
6104 "[M,N] -> { A[] -> B[] : M > N }",
6105 "[M,N] -> { A[] -> C[] : M > N > 5 }" },
6108 /* Perform some basic tests of binary operations on isl_multi_pw_aff objects.
6110 static int test_bin_mpa(isl_ctx *ctx)
6112 int i;
6113 isl_bool ok;
6114 isl_multi_pw_aff *mpa1, *mpa2, *res;
6116 for (i = 0; i < ARRAY_SIZE(mpa_bin_tests); ++i) {
6117 mpa1 = isl_multi_pw_aff_read_from_str(ctx,
6118 mpa_bin_tests[i].arg1);
6119 mpa2 = isl_multi_pw_aff_read_from_str(ctx,
6120 mpa_bin_tests[i].arg2);
6121 res = isl_multi_pw_aff_read_from_str(ctx,
6122 mpa_bin_tests[i].res);
6123 mpa1 = mpa_bin_tests[i].fn(mpa1, mpa2);
6124 ok = isl_multi_pw_aff_plain_is_equal(mpa1, res);
6125 isl_multi_pw_aff_free(mpa1);
6126 isl_multi_pw_aff_free(res);
6127 if (ok < 0)
6128 return -1;
6129 if (!ok)
6130 isl_die(ctx, isl_error_unknown,
6131 "unexpected result", return -1);
6134 return 0;
6137 /* Inputs for basic tests of unary operations on
6138 * isl_multi_union_pw_aff objects.
6139 * "fn" is the function that is tested.
6140 * "arg" is a string description of the input.
6141 * "res" is a string description of the expected result.
6143 struct {
6144 __isl_give isl_multi_union_pw_aff *(*fn)(
6145 __isl_take isl_multi_union_pw_aff *mupa);
6146 const char *arg;
6147 const char *res;
6148 } mupa_un_tests[] = {
6149 { &isl_multi_union_pw_aff_factor_range,
6150 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]",
6151 "C[{ A[] -> [2] }]" },
6152 { &isl_multi_union_pw_aff_factor_range,
6153 "[B[] -> C[{ A[] -> [2] }]]",
6154 "C[{ A[] -> [2] }]" },
6155 { &isl_multi_union_pw_aff_factor_range,
6156 "[B[{ A[] -> [1] }] -> C[]]",
6157 "C[]" },
6158 { &isl_multi_union_pw_aff_factor_range,
6159 "[B[] -> C[]]",
6160 "C[]" },
6161 { &isl_multi_union_pw_aff_factor_range,
6162 "([B[] -> C[]] : { A[x] : x >= 0 })",
6163 "(C[] : { A[x] : x >= 0 })" },
6164 { &isl_multi_union_pw_aff_factor_range,
6165 "[N] -> ([B[] -> C[]] : { A[x] : x <= N })",
6166 "[N] -> (C[] : { A[x] : x <= N })" },
6167 { &isl_multi_union_pw_aff_factor_range,
6168 "[N] -> ([B[] -> C[]] : { : N >= 0 })",
6169 "[N] -> (C[] : { : N >= 0 })" },
6172 /* Perform some basic tests of unary operations on
6173 * isl_multi_union_pw_aff objects.
6175 static int test_un_mupa(isl_ctx *ctx)
6177 int i;
6178 isl_bool ok;
6179 isl_multi_union_pw_aff *mupa, *res;
6181 for (i = 0; i < ARRAY_SIZE(mupa_un_tests); ++i) {
6182 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6183 mupa_un_tests[i].arg);
6184 res = isl_multi_union_pw_aff_read_from_str(ctx,
6185 mupa_un_tests[i].res);
6186 mupa = mupa_un_tests[i].fn(mupa);
6187 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6188 isl_multi_union_pw_aff_free(mupa);
6189 isl_multi_union_pw_aff_free(res);
6190 if (ok < 0)
6191 return -1;
6192 if (!ok)
6193 isl_die(ctx, isl_error_unknown,
6194 "unexpected result", return -1);
6197 return 0;
6200 /* Inputs for basic tests of binary operations on
6201 * isl_multi_union_pw_aff objects.
6202 * "fn" is the function that is tested.
6203 * "arg1" and "arg2" are string descriptions of the inputs.
6204 * "res" is a string description of the expected result.
6206 struct {
6207 __isl_give isl_multi_union_pw_aff *(*fn)(
6208 __isl_take isl_multi_union_pw_aff *mupa1,
6209 __isl_take isl_multi_union_pw_aff *mupa2);
6210 const char *arg1;
6211 const char *arg2;
6212 const char *res;
6213 } mupa_bin_tests[] = {
6214 { &isl_multi_union_pw_aff_add, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
6215 "[{ A[] -> [3] }]" },
6216 { &isl_multi_union_pw_aff_sub, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
6217 "[{ A[] -> [-1] }]" },
6218 { &isl_multi_union_pw_aff_add,
6219 "[{ A[] -> [1]; B[] -> [4] }]",
6220 "[{ A[] -> [2]; C[] -> [5] }]",
6221 "[{ A[] -> [3] }]" },
6222 { &isl_multi_union_pw_aff_union_add,
6223 "[{ A[] -> [1]; B[] -> [4] }]",
6224 "[{ A[] -> [2]; C[] -> [5] }]",
6225 "[{ A[] -> [3]; B[] -> [4]; C[] -> [5] }]" },
6226 { &isl_multi_union_pw_aff_add, "[{ A[x] -> [(1)] : x >= 5 }]",
6227 "[{ A[x] -> [(x)] : x <= 10 }]",
6228 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10 }]" },
6229 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
6230 "([] : { A[x] : x <= 10 })",
6231 "([] : { A[x] : 5 <= x <= 10 })" },
6232 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
6233 "[N] -> ([] : { A[x] : x <= N })",
6234 "[N] -> ([] : { A[x] : 5 <= x <= N })" },
6235 { &isl_multi_union_pw_aff_add, "[N] -> ([] : { A[x] : x >= N })",
6236 "([] : { A[x] : x <= 10 })",
6237 "[N] -> ([] : { A[x] : N <= x <= 10 })" },
6238 { &isl_multi_union_pw_aff_union_add, "[{ A[x] -> [(1)] : x >= 5 }]",
6239 "[{ A[x] -> [(x)] : x <= 10 }]",
6240 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10; "
6241 "A[x] -> [(1)] : x > 10; A[x] -> [(x)] : x < 5 }]" },
6242 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 5 })",
6243 "([] : { A[x] : x <= 10 })",
6244 "([] : { A[x] })" },
6245 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 0 })",
6246 "[N] -> ([] : { A[x] : x >= N })",
6247 "[N] -> ([] : { A[x] : x >= 0 or x >= N })" },
6248 { &isl_multi_union_pw_aff_union_add,
6249 "[N] -> ([] : { A[] : N >= 0})",
6250 "[N] -> ([] : { A[] : N <= 0})",
6251 "[N] -> ([] : { A[] })" },
6252 { &isl_multi_union_pw_aff_union_add,
6253 "[N] -> ([] : { A[] })",
6254 "[N] -> ([] : { : })",
6255 "[N] -> ([] : { : })" },
6256 { &isl_multi_union_pw_aff_union_add,
6257 "[N] -> ([] : { : })",
6258 "[N] -> ([] : { A[] })",
6259 "[N] -> ([] : { : })" },
6260 { &isl_multi_union_pw_aff_union_add,
6261 "[N] -> ([] : { : N >= 0})",
6262 "[N] -> ([] : { : N <= 0})",
6263 "[N] -> ([] : { : })" },
6264 { &isl_multi_union_pw_aff_range_product,
6265 "B[{ A[] -> [1] }]",
6266 "C[{ A[] -> [2] }]",
6267 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]" },
6268 { &isl_multi_union_pw_aff_range_product,
6269 "(B[] : { A[x] : x >= 5 })",
6270 "(C[] : { A[x] : x <= 10 })",
6271 "([B[] -> C[]] : { A[x] : 5 <= x <= 10 })" },
6272 { &isl_multi_union_pw_aff_range_product,
6273 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6274 "(C[] : { A[x] : x <= 10 })",
6275 "[B[{ A[x] -> [x + 1] : 5 <= x <= 10 }] -> C[]]" },
6276 { &isl_multi_union_pw_aff_range_product,
6277 "(C[] : { A[x] : x <= 10 })",
6278 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6279 "[C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= 10 }]]" },
6280 { &isl_multi_union_pw_aff_range_product,
6281 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6282 "[N] -> (C[] : { A[x] : x <= N })",
6283 "[N] -> [B[{ A[x] -> [x + 1] : 5 <= x <= N }] -> C[]]" },
6284 { &isl_multi_union_pw_aff_range_product,
6285 "[N] -> (C[] : { A[x] : x <= N })",
6286 "B[{ A[x] -> [x + 1] : x >= 5 }]",
6287 "[N] -> [C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= N }]]" },
6288 { &isl_multi_union_pw_aff_range_product,
6289 "B[{ A[] -> [1]; D[] -> [3] }]",
6290 "C[{ A[] -> [2] }]",
6291 "[B[{ A[] -> [1]; D[] -> [3] }] -> C[{ A[] -> [2] }]]" },
6292 { &isl_multi_union_pw_aff_range_product,
6293 "B[] }]",
6294 "(C[] : { A[x] })",
6295 "([B[] -> C[]] : { A[x] })" },
6296 { &isl_multi_union_pw_aff_range_product,
6297 "(B[] : { A[x] })",
6298 "C[] }]",
6299 "([B[] -> C[]] : { A[x] })" },
6302 /* Perform some basic tests of binary operations on
6303 * isl_multi_union_pw_aff objects.
6305 static int test_bin_mupa(isl_ctx *ctx)
6307 int i;
6308 isl_bool ok;
6309 isl_multi_union_pw_aff *mupa1, *mupa2, *res;
6311 for (i = 0; i < ARRAY_SIZE(mupa_bin_tests); ++i) {
6312 mupa1 = isl_multi_union_pw_aff_read_from_str(ctx,
6313 mupa_bin_tests[i].arg1);
6314 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx,
6315 mupa_bin_tests[i].arg2);
6316 res = isl_multi_union_pw_aff_read_from_str(ctx,
6317 mupa_bin_tests[i].res);
6318 mupa1 = mupa_bin_tests[i].fn(mupa1, mupa2);
6319 ok = isl_multi_union_pw_aff_plain_is_equal(mupa1, res);
6320 isl_multi_union_pw_aff_free(mupa1);
6321 isl_multi_union_pw_aff_free(res);
6322 if (ok < 0)
6323 return -1;
6324 if (!ok)
6325 isl_die(ctx, isl_error_unknown,
6326 "unexpected result", return -1);
6329 return 0;
6332 /* Inputs for basic tests of binary operations on
6333 * pairs of isl_multi_union_pw_aff and isl_set objects.
6334 * "fn" is the function that is tested.
6335 * "arg1" and "arg2" are string descriptions of the inputs.
6336 * "res" is a string description of the expected result.
6338 struct {
6339 __isl_give isl_multi_union_pw_aff *(*fn)(
6340 __isl_take isl_multi_union_pw_aff *mupa,
6341 __isl_take isl_set *set);
6342 const char *arg1;
6343 const char *arg2;
6344 const char *res;
6345 } mupa_set_tests[] = {
6346 { &isl_multi_union_pw_aff_intersect_range,
6347 "C[{ B[i,j] -> [i + 2j] }]", "{ C[1] }",
6348 "C[{ B[i,j] -> [i + 2j] : i + 2j = 1 }]" },
6349 { &isl_multi_union_pw_aff_intersect_range,
6350 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[N] }",
6351 "[N] -> C[{ B[i,j] -> [i + 2j] : i + 2j = N }]" },
6352 { &isl_multi_union_pw_aff_intersect_range,
6353 "[N] -> C[{ B[i,j] -> [i + 2j + N] }]", "{ C[1] }",
6354 "[N] -> C[{ B[i,j] -> [i + 2j + N] : i + 2j + N = 1 }]" },
6355 { &isl_multi_union_pw_aff_intersect_range,
6356 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[x] : N >= 0 }",
6357 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0 }]" },
6358 { &isl_multi_union_pw_aff_intersect_range,
6359 "C[]", "{ C[] }", "C[]" },
6360 { &isl_multi_union_pw_aff_intersect_range,
6361 "[N] -> (C[] : { : N >= 0 })",
6362 "{ C[] }",
6363 "[N] -> (C[] : { : N >= 0 })" },
6364 { &isl_multi_union_pw_aff_intersect_range,
6365 "(C[] : { A[a,b] })",
6366 "{ C[] }",
6367 "(C[] : { A[a,b] })" },
6368 { &isl_multi_union_pw_aff_intersect_range,
6369 "[N] -> (C[] : { A[a,b] : a,b <= N })",
6370 "{ C[] }",
6371 "[N] -> (C[] : { A[a,b] : a,b <= N })" },
6372 { &isl_multi_union_pw_aff_intersect_range,
6373 "C[]",
6374 "[N] -> { C[] : N >= 0 }",
6375 "[N] -> (C[] : { : N >= 0 })" },
6376 { &isl_multi_union_pw_aff_intersect_range,
6377 "(C[] : { A[a,b] })",
6378 "[N] -> { C[] : N >= 0 }",
6379 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
6380 { &isl_multi_union_pw_aff_intersect_range,
6381 "[N] -> (C[] : { : N >= 0 })",
6382 "[N] -> { C[] : N < 1024 }",
6383 "[N] -> (C[] : { : 0 <= N < 1024 })" },
6384 { &isl_multi_union_pw_aff_intersect_params,
6385 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { : N >= 0 }",
6386 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0}]" },
6387 { &isl_multi_union_pw_aff_intersect_params,
6388 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "[N] -> { : N >= 0 }",
6389 "[N] -> C[{ B[i,j] -> [i + 2j] : 0 <= N <= 256 }]" },
6390 { &isl_multi_union_pw_aff_intersect_params,
6391 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "{ : }",
6392 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]" },
6393 { &isl_multi_union_pw_aff_intersect_params,
6394 "C[]", "[N] -> { : N >= 0 }",
6395 "[N] -> (C[] : { : N >= 0 })" },
6396 { &isl_multi_union_pw_aff_intersect_params,
6397 "(C[] : { A[a,b] })", "[N] -> { : N >= 0 }",
6398 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
6399 { &isl_multi_union_pw_aff_intersect_params,
6400 "[N] -> (C[] : { A[a,N] })", "{ : }",
6401 "[N] -> (C[] : { A[a,N] })" },
6402 { &isl_multi_union_pw_aff_intersect_params,
6403 "[N] -> (C[] : { A[a,b] : N <= 256 })", "[N] -> { : N >= 0 }",
6404 "[N] -> (C[] : { A[a,b] : 0 <= N <= 256 })" },
6407 /* Perform some basic tests of binary operations on
6408 * pairs of isl_multi_union_pw_aff and isl_set objects.
6410 static int test_mupa_set(isl_ctx *ctx)
6412 int i;
6413 isl_bool ok;
6414 isl_multi_union_pw_aff *mupa, *res;
6415 isl_set *set;
6417 for (i = 0; i < ARRAY_SIZE(mupa_set_tests); ++i) {
6418 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6419 mupa_set_tests[i].arg1);
6420 set = isl_set_read_from_str(ctx, mupa_set_tests[i].arg2);
6421 res = isl_multi_union_pw_aff_read_from_str(ctx,
6422 mupa_set_tests[i].res);
6423 mupa = mupa_set_tests[i].fn(mupa, set);
6424 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6425 isl_multi_union_pw_aff_free(mupa);
6426 isl_multi_union_pw_aff_free(res);
6427 if (ok < 0)
6428 return -1;
6429 if (!ok)
6430 isl_die(ctx, isl_error_unknown,
6431 "unexpected result", return -1);
6434 return 0;
6437 /* Inputs for basic tests of binary operations on
6438 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
6439 * "fn" is the function that is tested.
6440 * "arg1" and "arg2" are string descriptions of the inputs.
6441 * "res" is a string description of the expected result.
6443 struct {
6444 __isl_give isl_multi_union_pw_aff *(*fn)(
6445 __isl_take isl_multi_union_pw_aff *mupa,
6446 __isl_take isl_union_set *uset);
6447 const char *arg1;
6448 const char *arg2;
6449 const char *res;
6450 } mupa_uset_tests[] = {
6451 { &isl_multi_union_pw_aff_intersect_domain,
6452 "C[{ B[i,j] -> [i + 2j] }]", "{ B[i,i] }",
6453 "C[{ B[i,i] -> [3i] }]" },
6454 { &isl_multi_union_pw_aff_intersect_domain,
6455 "(C[] : { B[i,j] })", "{ B[i,i] }",
6456 "(C[] : { B[i,i] })" },
6457 { &isl_multi_union_pw_aff_intersect_domain,
6458 "(C[] : { B[i,j] })", "[N] -> { B[N,N] }",
6459 "[N] -> (C[] : { B[N,N] })" },
6460 { &isl_multi_union_pw_aff_intersect_domain,
6461 "C[]", "{ B[i,i] }",
6462 "(C[] : { B[i,i] })" },
6463 { &isl_multi_union_pw_aff_intersect_domain,
6464 "[N] -> (C[] : { : N >= 0 })", "{ B[i,i] }",
6465 "[N] -> (C[] : { B[i,i] : N >= 0 })" },
6468 /* Perform some basic tests of binary operations on
6469 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
6471 static int test_mupa_uset(isl_ctx *ctx)
6473 int i;
6474 isl_bool ok;
6475 isl_multi_union_pw_aff *mupa, *res;
6476 isl_union_set *uset;
6478 for (i = 0; i < ARRAY_SIZE(mupa_uset_tests); ++i) {
6479 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6480 mupa_uset_tests[i].arg1);
6481 uset = isl_union_set_read_from_str(ctx,
6482 mupa_uset_tests[i].arg2);
6483 res = isl_multi_union_pw_aff_read_from_str(ctx,
6484 mupa_uset_tests[i].res);
6485 mupa = mupa_uset_tests[i].fn(mupa, uset);
6486 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6487 isl_multi_union_pw_aff_free(mupa);
6488 isl_multi_union_pw_aff_free(res);
6489 if (ok < 0)
6490 return -1;
6491 if (!ok)
6492 isl_die(ctx, isl_error_unknown,
6493 "unexpected result", return -1);
6496 return 0;
6499 /* Inputs for basic tests of binary operations on
6500 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
6501 * "fn" is the function that is tested.
6502 * "arg1" and "arg2" are string descriptions of the inputs.
6503 * "res" is a string description of the expected result.
6505 struct {
6506 __isl_give isl_multi_union_pw_aff *(*fn)(
6507 __isl_take isl_multi_union_pw_aff *mupa,
6508 __isl_take isl_multi_aff *ma);
6509 const char *arg1;
6510 const char *arg2;
6511 const char *res;
6512 } mupa_ma_tests[] = {
6513 { &isl_multi_union_pw_aff_apply_multi_aff,
6514 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6515 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6516 "{ C[a,b] -> D[b,a] }",
6517 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
6518 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
6519 { &isl_multi_union_pw_aff_apply_multi_aff,
6520 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
6521 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6522 "{ C[a,b] -> D[b,a] }",
6523 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
6524 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
6525 { &isl_multi_union_pw_aff_apply_multi_aff,
6526 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6527 "[N] -> { C[a] -> D[a + N] }",
6528 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }] " },
6529 { &isl_multi_union_pw_aff_apply_multi_aff,
6530 "C[]",
6531 "{ C[] -> D[] }",
6532 "D[]" },
6533 { &isl_multi_union_pw_aff_apply_multi_aff,
6534 "[N] -> (C[] : { : N >= 0 })",
6535 "{ C[] -> D[] }",
6536 "[N] -> (D[] : { : N >= 0 })" },
6537 { &isl_multi_union_pw_aff_apply_multi_aff,
6538 "C[]",
6539 "[N] -> { C[] -> D[N] }",
6540 "[N] -> D[{ [N] }]" },
6541 { &isl_multi_union_pw_aff_apply_multi_aff,
6542 "(C[] : { A[i,j] : i >= j })",
6543 "{ C[] -> D[] }",
6544 "(D[] : { A[i,j] : i >= j })" },
6545 { &isl_multi_union_pw_aff_apply_multi_aff,
6546 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6547 "{ C[] -> D[] }",
6548 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6549 { &isl_multi_union_pw_aff_apply_multi_aff,
6550 "(C[] : { A[i,j] : i >= j })",
6551 "[N] -> { C[] -> D[N] }",
6552 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6555 /* Perform some basic tests of binary operations on
6556 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
6558 static int test_mupa_ma(isl_ctx *ctx)
6560 int i;
6561 isl_bool ok;
6562 isl_multi_union_pw_aff *mupa, *res;
6563 isl_multi_aff *ma;
6565 for (i = 0; i < ARRAY_SIZE(mupa_ma_tests); ++i) {
6566 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6567 mupa_ma_tests[i].arg1);
6568 ma = isl_multi_aff_read_from_str(ctx, mupa_ma_tests[i].arg2);
6569 res = isl_multi_union_pw_aff_read_from_str(ctx,
6570 mupa_ma_tests[i].res);
6571 mupa = mupa_ma_tests[i].fn(mupa, ma);
6572 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6573 isl_multi_union_pw_aff_free(mupa);
6574 isl_multi_union_pw_aff_free(res);
6575 if (ok < 0)
6576 return -1;
6577 if (!ok)
6578 isl_die(ctx, isl_error_unknown,
6579 "unexpected result", return -1);
6582 return 0;
6585 /* Inputs for basic tests of binary operations on
6586 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
6587 * "fn" is the function that is tested.
6588 * "arg1" and "arg2" are string descriptions of the inputs.
6589 * "res" is a string description of the expected result.
6591 struct {
6592 __isl_give isl_union_pw_aff *(*fn)(
6593 __isl_take isl_multi_union_pw_aff *mupa,
6594 __isl_take isl_pw_aff *pa);
6595 const char *arg1;
6596 const char *arg2;
6597 const char *res;
6598 } mupa_pa_tests[] = {
6599 { &isl_multi_union_pw_aff_apply_pw_aff,
6600 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6601 "[N] -> { C[a] -> [a + N] }",
6602 "[N] -> { A[i,j] -> [i + N]; B[i,j] -> [j + N] }" },
6603 { &isl_multi_union_pw_aff_apply_pw_aff,
6604 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6605 "{ C[a] -> [a] : a >= 0; C[a] -> [-a] : a < 0 }",
6606 "{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
6607 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }" },
6608 { &isl_multi_union_pw_aff_apply_pw_aff,
6609 "C[]",
6610 "[N] -> { C[] -> [N] }",
6611 "[N] -> { [N] }" },
6612 { &isl_multi_union_pw_aff_apply_pw_aff,
6613 "C[]",
6614 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
6615 "[N] -> { [N] : N >= 0; [-N] : N < 0 }" },
6616 { &isl_multi_union_pw_aff_apply_pw_aff,
6617 "[N] -> (C[] : { : N >= 0 })",
6618 "[N] -> { C[] -> [N] }",
6619 "[N] -> { [N] : N >= 0 }" },
6620 { &isl_multi_union_pw_aff_apply_pw_aff,
6621 "[N] -> (C[] : { : N >= 0 })",
6622 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
6623 "[N] -> { [N] : N >= 0 }" },
6624 { &isl_multi_union_pw_aff_apply_pw_aff,
6625 "[N] -> (C[] : { : N >= 0 })",
6626 "{ C[] -> [0] }",
6627 "[N] -> { [0] : N >= 0 }" },
6628 { &isl_multi_union_pw_aff_apply_pw_aff,
6629 "(C[] : { A[i,j] : i >= j })",
6630 "[N] -> { C[] -> [N] }",
6631 "[N] -> { A[i,j] -> [N] : i >= j }" },
6632 { &isl_multi_union_pw_aff_apply_pw_aff,
6633 "(C[] : { A[i,j] : i >= j })",
6634 "[N] -> { C[] -> [N] : N >= 0 }",
6635 "[N] -> { A[i,j] -> [N] : i >= j and N >= 0 }" },
6638 /* Perform some basic tests of binary operations on
6639 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
6641 static int test_mupa_pa(isl_ctx *ctx)
6643 int i;
6644 isl_bool ok;
6645 isl_multi_union_pw_aff *mupa;
6646 isl_union_pw_aff *upa, *res;
6647 isl_pw_aff *pa;
6649 for (i = 0; i < ARRAY_SIZE(mupa_pa_tests); ++i) {
6650 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6651 mupa_pa_tests[i].arg1);
6652 pa = isl_pw_aff_read_from_str(ctx, mupa_pa_tests[i].arg2);
6653 res = isl_union_pw_aff_read_from_str(ctx,
6654 mupa_pa_tests[i].res);
6655 upa = mupa_pa_tests[i].fn(mupa, pa);
6656 ok = isl_union_pw_aff_plain_is_equal(upa, res);
6657 isl_union_pw_aff_free(upa);
6658 isl_union_pw_aff_free(res);
6659 if (ok < 0)
6660 return -1;
6661 if (!ok)
6662 isl_die(ctx, isl_error_unknown,
6663 "unexpected result", return -1);
6666 return 0;
6669 /* Inputs for basic tests of binary operations on
6670 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6671 * "fn" is the function that is tested.
6672 * "arg1" and "arg2" are string descriptions of the inputs.
6673 * "res" is a string description of the expected result.
6675 struct {
6676 __isl_give isl_multi_union_pw_aff *(*fn)(
6677 __isl_take isl_multi_union_pw_aff *mupa,
6678 __isl_take isl_pw_multi_aff *pma);
6679 const char *arg1;
6680 const char *arg2;
6681 const char *res;
6682 } mupa_pma_tests[] = {
6683 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6684 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6685 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6686 "{ C[a,b] -> D[b,a] }",
6687 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
6688 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
6689 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6690 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
6691 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6692 "{ C[a,b] -> D[b,a] }",
6693 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
6694 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
6695 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6696 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6697 "[N] -> { C[a] -> D[a + N] }",
6698 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }]" },
6699 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6700 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6701 "{ C[a] -> D[a] : a >= 0; C[a] -> D[-a] : a < 0 }",
6702 "D[{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
6703 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }]" },
6704 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6705 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6706 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6707 "{ C[a,b] -> D[a,b] : a >= b; C[a,b] -> D[b,a] : a < b }",
6708 "D[{ A[i,j] -> [i] : i >= j; A[i,j] -> [j] : i < j; "
6709 "B[i,j] -> [j] : i <= j; B[i,j] -> [i] : i > j }, "
6710 "{ A[i,j] -> [j] : i >= j; A[i,j] -> [i] : i < j; "
6711 "B[i,j] -> [i] : i <= j; B[i,j] -> [j] : i > j }]" },
6712 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6713 "C[]",
6714 "{ C[] -> D[] }",
6715 "D[]" },
6716 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6717 "[N] -> (C[] : { : N >= 0 })",
6718 "{ C[] -> D[] }",
6719 "[N] -> (D[] : { : N >= 0 })" },
6720 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6721 "C[]",
6722 "[N] -> { C[] -> D[N] }",
6723 "[N] -> D[{ [N] }]" },
6724 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6725 "(C[] : { A[i,j] : i >= j })",
6726 "{ C[] -> D[] }",
6727 "(D[] : { A[i,j] : i >= j })" },
6728 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6729 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6730 "{ C[] -> D[] }",
6731 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6732 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6733 "(C[] : { A[i,j] : i >= j })",
6734 "[N] -> { C[] -> D[N] }",
6735 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6736 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6737 "C[]",
6738 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6739 "[N] -> D[{ [N] : N >= 0; [-N] : N < 0 }]" },
6740 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6741 "[N] -> (C[] : { : N >= 0 })",
6742 "[N] -> { C[] -> D[N] }",
6743 "[N] -> D[{ [N] : N >= 0 }]" },
6744 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6745 "[N] -> (C[] : { : N >= 0 })",
6746 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6747 "[N] -> D[{ [N] : N >= 0 }]" },
6748 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6749 "[N] -> (C[] : { : N >= 0 })",
6750 "{ C[] -> D[0] }",
6751 "[N] -> D[{ [0] : N >= 0 }]" },
6752 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6753 "(C[] : { A[i,j] : i >= j })",
6754 "[N] -> { C[] -> D[N] : N >= 0 }",
6755 "[N] -> D[{ A[i,j] -> [N] : i >= j and N >= 0 }]" },
6758 /* Perform some basic tests of binary operations on
6759 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6761 static int test_mupa_pma(isl_ctx *ctx)
6763 int i;
6764 isl_bool ok;
6765 isl_multi_union_pw_aff *mupa, *res;
6766 isl_pw_multi_aff *pma;
6768 for (i = 0; i < ARRAY_SIZE(mupa_pma_tests); ++i) {
6769 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6770 mupa_pma_tests[i].arg1);
6771 pma = isl_pw_multi_aff_read_from_str(ctx,
6772 mupa_pma_tests[i].arg2);
6773 res = isl_multi_union_pw_aff_read_from_str(ctx,
6774 mupa_pma_tests[i].res);
6775 mupa = mupa_pma_tests[i].fn(mupa, pma);
6776 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6777 isl_multi_union_pw_aff_free(mupa);
6778 isl_multi_union_pw_aff_free(res);
6779 if (ok < 0)
6780 return -1;
6781 if (!ok)
6782 isl_die(ctx, isl_error_unknown,
6783 "unexpected result", return -1);
6786 return 0;
6789 /* Inputs for basic tests of binary operations on
6790 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6791 * "fn" is the function that is tested.
6792 * "arg1" and "arg2" are string descriptions of the inputs.
6793 * "res" is a string description of the expected result.
6795 struct {
6796 __isl_give isl_multi_union_pw_aff *(*fn)(
6797 __isl_take isl_multi_union_pw_aff *mupa,
6798 __isl_take isl_union_pw_multi_aff *upma);
6799 const char *arg1;
6800 const char *arg2;
6801 const char *res;
6802 } mupa_upma_tests[] = {
6803 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6804 "C[{ B[i,j] -> [i + 2j] }]", "{ A[a,b] -> B[b,a] }",
6805 "C[{ A[a,b] -> [b + 2a] }]" },
6806 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6807 "C[{ B[i,j] -> [i + 2j] }]",
6808 "{ A[a,b] -> B[b,a] : b > a }",
6809 "C[{ A[a,b] -> [b + 2a] : b > a }]" },
6810 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6811 "C[{ B[i,j] -> [i + 2j] : j > 4 }]",
6812 "{ A[a,b] -> B[b,a] : b > a }",
6813 "C[{ A[a,b] -> [b + 2a] : b > a > 4 }]" },
6814 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6815 "C[{ B[i,j] -> [i + 2j] }]",
6816 "{ A[a,b] -> B[b,a] : a > b; A[a,b] -> B[a,b] : a <= b }",
6817 "C[{ A[a,b] -> [b + 2a] : a > b; A[a,b] -> [a + 2b] : a <= b }]" },
6818 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6819 "(C[] : { B[a,b] })",
6820 "{ A[a,b] -> B[b,a] }",
6821 "(C[] : { A[a,b] })" },
6822 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6823 "(C[] : { B[a,b] })",
6824 "{ B[a,b] -> A[b,a] }",
6825 "(C[] : { })" },
6826 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6827 "(C[] : { B[a,b] })",
6828 "{ A[a,b] -> B[b,a] : a > b }",
6829 "(C[] : { A[a,b] : a > b })" },
6830 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6831 "(C[] : { B[a,b] : a > b })",
6832 "{ A[a,b] -> B[b,a] }",
6833 "(C[] : { A[a,b] : b > a })" },
6834 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6835 "[N] -> (C[] : { B[a,b] : a > N })",
6836 "{ A[a,b] -> B[b,a] : a > b }",
6837 "[N] -> (C[] : { A[a,b] : a > b > N })" },
6838 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6839 "(C[] : { B[a,b] : a > b })",
6840 "[N] -> { A[a,b] -> B[b,a] : a > N }",
6841 "[N] -> (C[] : { A[a,b] : b > a > N })" },
6842 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6843 "C[]",
6844 "{ A[a,b] -> B[b,a] }",
6845 "C[]" },
6846 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6847 "[N] -> (C[] : { : N >= 0 })",
6848 "{ A[a,b] -> B[b,a] }",
6849 "[N] -> (C[] : { : N >= 0 })" },
6850 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6851 "C[]",
6852 "[N] -> { A[a,b] -> B[b,a] : N >= 0 }",
6853 "[N] -> (C[] : { : N >= 0 })" },
6856 /* Perform some basic tests of binary operations on
6857 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6859 static int test_mupa_upma(isl_ctx *ctx)
6861 int i;
6862 isl_bool ok;
6863 isl_multi_union_pw_aff *mupa, *res;
6864 isl_union_pw_multi_aff *upma;
6866 for (i = 0; i < ARRAY_SIZE(mupa_upma_tests); ++i) {
6867 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6868 mupa_upma_tests[i].arg1);
6869 upma = isl_union_pw_multi_aff_read_from_str(ctx,
6870 mupa_upma_tests[i].arg2);
6871 res = isl_multi_union_pw_aff_read_from_str(ctx,
6872 mupa_upma_tests[i].res);
6873 mupa = mupa_upma_tests[i].fn(mupa, upma);
6874 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6875 isl_multi_union_pw_aff_free(mupa);
6876 isl_multi_union_pw_aff_free(res);
6877 if (ok < 0)
6878 return -1;
6879 if (!ok)
6880 isl_die(ctx, isl_error_unknown,
6881 "unexpected result", return -1);
6884 return 0;
6887 /* Check that the input tuple of an isl_aff can be set properly.
6889 static isl_stat test_aff_set_tuple_id(isl_ctx *ctx)
6891 isl_id *id;
6892 isl_aff *aff;
6893 isl_stat equal;
6895 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x + 1] }");
6896 id = isl_id_alloc(ctx, "A", NULL);
6897 aff = isl_aff_set_tuple_id(aff, isl_dim_in, id);
6898 equal = aff_check_plain_equal(aff, "{ A[x] -> [x + 1] }");
6899 isl_aff_free(aff);
6900 if (equal < 0)
6901 return isl_stat_error;
6903 return isl_stat_ok;
6906 /* Check that affine expressions get normalized on addition/subtraction.
6907 * In particular, check that (final) unused integer divisions get removed
6908 * such that an expression derived from expressions with integer divisions
6909 * is found to be obviously equal to one that is created directly.
6911 static isl_stat test_aff_normalize(isl_ctx *ctx)
6913 isl_aff *aff, *aff2;
6914 isl_stat ok;
6916 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x//2] }");
6917 aff2 = isl_aff_read_from_str(ctx, "{ [x] -> [1 + x//2] }");
6918 aff = isl_aff_sub(aff2, aff);
6919 ok = aff_check_plain_equal(aff, "{ [x] -> [1] }");
6920 isl_aff_free(aff);
6922 return ok;
6925 int test_aff(isl_ctx *ctx)
6927 const char *str;
6928 isl_set *set;
6929 isl_space *space;
6930 isl_local_space *ls;
6931 isl_aff *aff;
6932 int zero;
6933 isl_stat equal;
6935 if (test_upa(ctx) < 0)
6936 return -1;
6937 if (test_bin_aff(ctx) < 0)
6938 return -1;
6939 if (test_bin_pw_aff(ctx) < 0)
6940 return -1;
6941 if (test_upma_test(ctx) < 0)
6942 return -1;
6943 if (test_bin_upma(ctx) < 0)
6944 return -1;
6945 if (test_bin_upma_fail(ctx) < 0)
6946 return -1;
6947 if (test_upma_uset(ctx) < 0)
6948 return -1;
6949 if (test_un_mpa(ctx) < 0)
6950 return -1;
6951 if (test_bin_mpa(ctx) < 0)
6952 return -1;
6953 if (test_un_mupa(ctx) < 0)
6954 return -1;
6955 if (test_bin_mupa(ctx) < 0)
6956 return -1;
6957 if (test_mupa_set(ctx) < 0)
6958 return -1;
6959 if (test_mupa_uset(ctx) < 0)
6960 return -1;
6961 if (test_mupa_ma(ctx) < 0)
6962 return -1;
6963 if (test_mupa_pa(ctx) < 0)
6964 return -1;
6965 if (test_mupa_pma(ctx) < 0)
6966 return -1;
6967 if (test_mupa_upma(ctx) < 0)
6968 return -1;
6970 space = isl_space_set_alloc(ctx, 0, 1);
6971 ls = isl_local_space_from_space(space);
6972 aff = isl_aff_zero_on_domain(ls);
6974 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6975 aff = isl_aff_scale_down_ui(aff, 3);
6976 aff = isl_aff_floor(aff);
6977 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6978 aff = isl_aff_scale_down_ui(aff, 2);
6979 aff = isl_aff_floor(aff);
6980 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6982 str = "{ [10] }";
6983 set = isl_set_read_from_str(ctx, str);
6984 aff = isl_aff_gist(aff, set);
6986 aff = isl_aff_add_constant_si(aff, -16);
6987 zero = isl_aff_plain_is_zero(aff);
6988 isl_aff_free(aff);
6990 if (zero < 0)
6991 return -1;
6992 if (!zero)
6993 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6995 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
6996 aff = isl_aff_scale_down_ui(aff, 64);
6997 aff = isl_aff_floor(aff);
6998 equal = aff_check_plain_equal(aff, "{ [-1] }");
6999 isl_aff_free(aff);
7000 if (equal < 0)
7001 return -1;
7003 if (test_aff_set_tuple_id(ctx) < 0)
7004 return -1;
7005 if (test_aff_normalize(ctx) < 0)
7006 return -1;
7008 return 0;
7011 /* Inputs for isl_set_bind tests.
7012 * "set" is the input set.
7013 * "tuple" is the binding tuple.
7014 * "res" is the expected result.
7016 static
7017 struct {
7018 const char *set;
7019 const char *tuple;
7020 const char *res;
7021 } bind_set_tests[] = {
7022 { "{ A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
7023 "{ A[M, N] }",
7024 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
7025 { "{ B[N, M] : M mod 2 = 0 and N mod 8 = 3 }",
7026 "{ B[N, M] }",
7027 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
7028 { "[M] -> { C[N] : M mod 2 = 0 and N mod 8 = 3 }",
7029 "{ C[N] }",
7030 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }" },
7031 { "[M] -> { D[x, N] : x mod 2 = 0 and N mod 8 = 3 and M >= 0 }",
7032 "{ D[M, N] }",
7033 "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 and M >= 0 }" },
7036 /* Perform basic isl_set_bind tests.
7038 static isl_stat test_bind_set(isl_ctx *ctx)
7040 int i;
7042 for (i = 0; i < ARRAY_SIZE(bind_set_tests); ++i) {
7043 const char *str;
7044 isl_set *set;
7045 isl_multi_id *tuple;
7046 isl_stat r;
7048 set = isl_set_read_from_str(ctx, bind_set_tests[i].set);
7049 str = bind_set_tests[i].tuple;
7050 tuple = isl_multi_id_read_from_str(ctx, str);
7051 set = isl_set_bind(set, tuple);
7052 r = set_check_equal(set, bind_set_tests[i].res);
7053 isl_set_free(set);
7054 if (r < 0)
7055 return isl_stat_error;
7058 return isl_stat_ok;
7061 /* Inputs for isl_map_bind_domain tests.
7062 * "map" is the input map.
7063 * "tuple" is the binding tuple.
7064 * "res" is the expected result.
7066 struct {
7067 const char *map;
7068 const char *tuple;
7069 const char *res;
7070 } bind_map_domain_tests[] = {
7071 { "{ A[M, N] -> [M + floor(N/2)] }",
7072 "{ A[M, N] }",
7073 "[M, N] -> { [M + floor(N/2)] }" },
7074 { "{ B[N, M] -> [M + floor(N/2)] }",
7075 "{ B[N, M] }",
7076 "[N, M] -> { [M + floor(N/2)] }" },
7077 { "[M] -> { C[N] -> [M + floor(N/2)] }",
7078 "{ C[N] }",
7079 "[M, N] -> { [M + floor(N/2)] }" },
7080 { "[M] -> { C[x, N] -> [x + floor(N/2)] }",
7081 "{ C[M, N] }",
7082 "[M, N] -> { [M + floor(N/2)] }" },
7083 { "[M] -> { C[x, N] -> [M + floor(N/2)] }",
7084 "{ C[M, N] }",
7085 "[M, N] -> { [M + floor(N/2)] }" },
7086 { "[A, M] -> { C[N, x] -> [x + floor(N/2)] }",
7087 "{ C[N, M] }",
7088 "[A, N, M] -> { [M + floor(N/2)] }" },
7091 /* Perform basic isl_map_bind_domain tests.
7093 static isl_stat test_bind_map_domain(isl_ctx *ctx)
7095 int i;
7097 for (i = 0; i < ARRAY_SIZE(bind_map_domain_tests); ++i) {
7098 const char *str;
7099 isl_map *map;
7100 isl_set *set;
7101 isl_multi_id *tuple;
7102 isl_stat r;
7104 str = bind_map_domain_tests[i].map;
7105 map = isl_map_read_from_str(ctx, str);
7106 str = bind_map_domain_tests[i].tuple;
7107 tuple = isl_multi_id_read_from_str(ctx, str);
7108 set = isl_map_bind_domain(map, tuple);
7109 str = bind_map_domain_tests[i].res;
7110 r = set_check_equal(set, str);
7111 isl_set_free(set);
7112 if (r < 0)
7113 return isl_stat_error;
7116 return isl_stat_ok;
7119 /* Inputs for isl_union_map_bind_range tests.
7120 * "map" is the input union map.
7121 * "tuple" is the binding tuple.
7122 * "res" is the expected result.
7124 struct {
7125 const char *map;
7126 const char *tuple;
7127 const char *res;
7128 } bind_umap_range_tests[] = {
7129 { "{ B[N, M] -> A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
7130 "{ A[M, N] }",
7131 "[M, N] -> { B[N, M] : M mod 2 = 0 and N mod 8 = 3 }" },
7132 { "{ B[N, M] -> A[M, N] : M mod 2 = 0 and N mod 8 = 3 }",
7133 "{ B[M, N] }",
7134 "{ }" },
7135 { "{ A[] -> B[]; C[] -> D[]; E[] -> B[] }",
7136 "{ B[] }",
7137 "{ A[]; E[] }" },
7140 /* Perform basic isl_union_map_bind_range tests.
7142 static isl_stat test_bind_umap_range(isl_ctx *ctx)
7144 int i;
7146 for (i = 0; i < ARRAY_SIZE(bind_umap_range_tests); ++i) {
7147 const char *str;
7148 isl_union_map *umap;
7149 isl_union_set *uset;
7150 isl_multi_id *tuple;
7151 isl_stat r;
7153 str = bind_umap_range_tests[i].map;
7154 umap = isl_union_map_read_from_str(ctx, str);
7155 str = bind_umap_range_tests[i].tuple;
7156 tuple = isl_multi_id_read_from_str(ctx, str);
7157 uset = isl_union_map_bind_range(umap, tuple);
7158 str = bind_umap_range_tests[i].res;
7159 r = uset_check_equal(uset, str);
7160 isl_union_set_free(uset);
7161 if (r < 0)
7162 return isl_stat_error;
7165 return isl_stat_ok;
7168 /* Inputs for isl_pw_multi_aff_bind_domain tests.
7169 * "pma" is the input expression.
7170 * "tuple" is the binding tuple.
7171 * "res" is the expected result.
7173 struct {
7174 const char *pma;
7175 const char *tuple;
7176 const char *res;
7177 } bind_pma_domain_tests[] = {
7178 { "{ A[M, N] -> [M + floor(N/2)] }",
7179 "{ A[M, N] }",
7180 "[M, N] -> { [M + floor(N/2)] }" },
7181 { "{ B[N, M] -> [M + floor(N/2)] }",
7182 "{ B[N, M] }",
7183 "[N, M] -> { [M + floor(N/2)] }" },
7184 { "[M] -> { C[N] -> [M + floor(N/2)] }",
7185 "{ C[N] }",
7186 "[M, N] -> { [M + floor(N/2)] }" },
7187 { "[M] -> { C[x, N] -> [x + floor(N/2)] }",
7188 "{ C[M, N] }",
7189 "[M, N] -> { [M + floor(N/2)] }" },
7190 { "[M] -> { C[x, N] -> [M + floor(N/2)] }",
7191 "{ C[M, N] }",
7192 "[M, N] -> { [M + floor(N/2)] }" },
7193 { "[A, M] -> { C[N, x] -> [x + floor(N/2)] }",
7194 "{ C[N, M] }",
7195 "[A, N, M] -> { [M + floor(N/2)] }" },
7198 /* Perform basic isl_pw_multi_aff_bind_domain tests.
7200 static isl_stat test_bind_pma_domain(isl_ctx *ctx)
7202 int i;
7204 for (i = 0; i < ARRAY_SIZE(bind_pma_domain_tests); ++i) {
7205 const char *str;
7206 isl_pw_multi_aff *pma;
7207 isl_multi_id *tuple;
7208 isl_stat r;
7210 str = bind_pma_domain_tests[i].pma;
7211 pma = isl_pw_multi_aff_read_from_str(ctx, str);
7212 str = bind_pma_domain_tests[i].tuple;
7213 tuple = isl_multi_id_read_from_str(ctx, str);
7214 pma = isl_pw_multi_aff_bind_domain(pma, tuple);
7215 str = bind_pma_domain_tests[i].res;
7216 r = pw_multi_aff_check_plain_equal(pma, str);
7217 isl_pw_multi_aff_free(pma);
7218 if (r < 0)
7219 return isl_stat_error;
7222 return isl_stat_ok;
7225 /* Inputs for isl_pw_multi_aff_bind_domain_wrapped_domain tests.
7226 * "pma" is the input expression.
7227 * "tuple" is the binding tuple.
7228 * "res" is the expected result.
7230 struct {
7231 const char *pma;
7232 const char *tuple;
7233 const char *res;
7234 } bind_pma_domain_wrapped_tests[] = {
7235 { "{ [A[M, N] -> B[]] -> [M + floor(N/2)] }",
7236 "{ A[M, N] }",
7237 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
7238 { "{ [B[N, M] -> D[]] -> [M + floor(N/2)] }",
7239 "{ B[N, M] }",
7240 "[N, M] -> { D[] -> [M + floor(N/2)] }" },
7241 { "[M] -> { [C[N] -> B[x]] -> [x + M + floor(N/2)] }",
7242 "{ C[N] }",
7243 "[M, N] -> { B[x] -> [x + M + floor(N/2)] }" },
7244 { "[M] -> { [C[x, N] -> B[]] -> [x + floor(N/2)] }",
7245 "{ C[M, N] }",
7246 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
7247 { "[M] -> { [C[x, N] -> B[]] -> [M + floor(N/2)] }",
7248 "{ C[M, N] }",
7249 "[M, N] -> { B[] -> [M + floor(N/2)] }" },
7250 { "[A, M] -> { [C[N, x] -> B[]] -> [x + floor(N/2)] }",
7251 "{ C[N, M] }",
7252 "[A, N, M] -> { B[] -> [M + floor(N/2)] }" },
7255 /* Perform basic isl_pw_multi_aff_bind_domain_wrapped_domain tests.
7257 static isl_stat test_bind_pma_domain_wrapped(isl_ctx *ctx)
7259 int i;
7261 for (i = 0; i < ARRAY_SIZE(bind_pma_domain_wrapped_tests); ++i) {
7262 const char *str;
7263 isl_pw_multi_aff *pma;
7264 isl_multi_id *tuple;
7265 isl_stat r;
7267 str = bind_pma_domain_wrapped_tests[i].pma;
7268 pma = isl_pw_multi_aff_read_from_str(ctx, str);
7269 str = bind_pma_domain_wrapped_tests[i].tuple;
7270 tuple = isl_multi_id_read_from_str(ctx, str);
7271 pma = isl_pw_multi_aff_bind_domain_wrapped_domain(pma, tuple);
7272 str = bind_pma_domain_wrapped_tests[i].res;
7273 r = pw_multi_aff_check_plain_equal(pma, str);
7274 isl_pw_multi_aff_free(pma);
7275 if (r < 0)
7276 return isl_stat_error;
7279 return isl_stat_ok;
7282 /* Inputs for isl_aff_bind_id tests.
7283 * "aff" is the input expression.
7284 * "id" is the binding id.
7285 * "res" is the expected result.
7287 static
7288 struct {
7289 const char *aff;
7290 const char *id;
7291 const char *res;
7292 } bind_aff_tests[] = {
7293 { "{ [4] }", "M", "[M = 4] -> { : }" },
7294 { "{ B[x] -> [floor(x/2)] }", "M", "[M] -> { B[x] : M = floor(x/2) }" },
7295 { "[M] -> { [4] }", "M", "[M = 4] -> { : }" },
7296 { "[M] -> { [floor(M/2)] }", "M", "[M] -> { : floor(M/2) = M }" },
7297 { "{ [NaN] }", "M", "{ : false }" },
7298 { "{ A[x] -> [NaN] }", "M", "{ A[x] : false }" },
7301 /* Perform basic isl_aff_bind_id tests.
7303 static isl_stat test_bind_aff(isl_ctx *ctx)
7305 int i;
7307 for (i = 0; i < ARRAY_SIZE(bind_aff_tests); ++i) {
7308 isl_aff *aff;
7309 isl_set *res;
7310 isl_id *id;
7311 isl_stat r;
7313 aff = isl_aff_read_from_str(ctx, bind_aff_tests[i].aff);
7314 id = isl_id_read_from_str(ctx, bind_aff_tests[i].id);
7315 res = isl_set_from_basic_set(isl_aff_bind_id(aff, id));
7316 r = set_check_equal(res, bind_aff_tests[i].res);
7317 isl_set_free(res);
7318 if (r < 0)
7319 return isl_stat_error;
7322 return isl_stat_ok;
7325 /* Inputs for isl_pw_aff_bind_id tests.
7326 * "pa" is the input expression.
7327 * "id" is the binding id.
7328 * "res" is the expected result.
7330 static
7331 struct {
7332 const char *pa;
7333 const char *id;
7334 const char *res;
7335 } bind_pa_tests[] = {
7336 { "{ [4] }", "M", "[M = 4] -> { : }" },
7337 { "{ B[x] -> [floor(x/2)] }", "M", "[M] -> { B[x] : M = floor(x/2) }" },
7338 { "[M] -> { [4] }", "M", "[M = 4] -> { : }" },
7339 { "[M] -> { [floor(M/2)] }", "M", "[M] -> { : floor(M/2) = M }" },
7340 { "[M] -> { [M] : M >= 0; [-M] : M < 0 }", "M", "[M] -> { : M >= 0 }" },
7341 { "{ [NaN] }", "M", "{ : false }" },
7342 { "{ A[x] -> [NaN] }", "M", "{ A[x] : false }" },
7345 /* Perform basic isl_pw_aff_bind_id tests.
7347 static isl_stat test_bind_pa(isl_ctx *ctx)
7349 int i;
7351 for (i = 0; i < ARRAY_SIZE(bind_pa_tests); ++i) {
7352 isl_pw_aff *pa;
7353 isl_set *res;
7354 isl_id *id;
7355 isl_stat r;
7357 pa = isl_pw_aff_read_from_str(ctx, bind_pa_tests[i].pa);
7358 id = isl_id_read_from_str(ctx, bind_pa_tests[i].id);
7359 res = isl_pw_aff_bind_id(pa, id);
7360 r = set_check_equal(res, bind_pa_tests[i].res);
7361 isl_set_free(res);
7362 if (r < 0)
7363 return isl_stat_error;
7366 return isl_stat_ok;
7369 /* Inputs for isl_multi_union_pw_aff_bind tests.
7370 * "mupa" is the input expression.
7371 * "tuple" is the binding tuple.
7372 * "res" is the expected result.
7374 static
7375 struct {
7376 const char *mupa;
7377 const char *tuple;
7378 const char *res;
7379 } bind_mupa_tests[] = {
7380 { "A[{ [4] }, { [5] }]",
7381 "{ A[M, N] }",
7382 "[M = 4, N = 5] -> { : }" },
7383 { "A[{ B[x] -> [floor(x/2)] }, { B[y] -> [y + 5] }]",
7384 "{ A[M, N] }",
7385 "[M, N] -> { B[x] : M = floor(x/2) and N = x + 5 }" },
7386 { "[M] -> A[{ [4] }, { [M + 1] }]",
7387 "{ A[M, N] }",
7388 "[M = 4, N = 5] -> { : }" },
7391 /* Perform basic isl_multi_union_pw_aff_bind tests.
7393 static isl_stat test_bind_mupa(isl_ctx *ctx)
7395 int i;
7397 for (i = 0; i < ARRAY_SIZE(bind_mupa_tests); ++i) {
7398 const char *str;
7399 isl_multi_union_pw_aff *mupa;
7400 isl_union_set *res;
7401 isl_multi_id *tuple;
7402 isl_stat r;
7404 str = bind_mupa_tests[i].mupa;
7405 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7406 str = bind_mupa_tests[i].tuple;
7407 tuple = isl_multi_id_read_from_str(ctx, str);
7408 res = isl_multi_union_pw_aff_bind(mupa, tuple);
7409 r = uset_check_equal(res, bind_mupa_tests[i].res);
7410 isl_union_set_free(res);
7411 if (r < 0)
7412 return isl_stat_error;
7415 return isl_stat_ok;
7418 /* Perform tests that reinterpret dimensions as parameters.
7420 static int test_bind(isl_ctx *ctx)
7422 if (test_bind_set(ctx) < 0)
7423 return -1;
7424 if (test_bind_map_domain(ctx) < 0)
7425 return -1;
7426 if (test_bind_umap_range(ctx) < 0)
7427 return -1;
7428 if (test_bind_pma_domain(ctx) < 0)
7429 return -1;
7430 if (test_bind_pma_domain_wrapped(ctx) < 0)
7431 return -1;
7432 if (test_bind_aff(ctx) < 0)
7433 return -1;
7434 if (test_bind_pa(ctx) < 0)
7435 return -1;
7436 if (test_bind_mupa(ctx) < 0)
7437 return -1;
7439 return 0;
7442 /* Inputs for isl_set_unbind_params tests.
7443 * "set" is the input parameter domain.
7444 * "tuple" is the tuple of the constructed set.
7445 * "res" is the expected result.
7447 struct {
7448 const char *set;
7449 const char *tuple;
7450 const char *res;
7451 } unbind_set_tests[] = {
7452 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7453 "{ A[M, N] }",
7454 "{ A[M, N] : M mod 2 = 0 and N mod 8 = 3 }" },
7455 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7456 "{ B[N, M] }",
7457 "{ B[N, M] : M mod 2 = 0 and N mod 8 = 3 }" },
7458 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7459 "{ C[N] }",
7460 "[M] -> { C[N] : M mod 2 = 0 and N mod 8 = 3 }" },
7461 { "[M, N] -> { : M mod 2 = 0 and N mod 8 = 3 }",
7462 "{ D[T, N] }",
7463 "[M] -> { D[x, N] : M mod 2 = 0 and N mod 8 = 3 }" },
7466 /* Perform basic isl_set_unbind_params tests.
7468 static isl_stat test_unbind_set(isl_ctx *ctx)
7470 int i;
7472 for (i = 0; i < ARRAY_SIZE(unbind_set_tests); ++i) {
7473 const char *str;
7474 isl_set *set;
7475 isl_multi_id *tuple;
7476 isl_stat r;
7478 set = isl_set_read_from_str(ctx, unbind_set_tests[i].set);
7479 str = unbind_set_tests[i].tuple;
7480 tuple = isl_multi_id_read_from_str(ctx, str);
7481 set = isl_set_unbind_params(set, tuple);
7482 r = set_check_equal(set, unbind_set_tests[i].res);
7483 isl_set_free(set);
7484 if (r < 0)
7485 return isl_stat_error;
7488 return isl_stat_ok;
7491 /* Inputs for isl_aff_unbind_params_insert_domain tests.
7492 * "aff" is the input affine expression defined over a parameter domain.
7493 * "tuple" is the tuple of the domain that gets introduced.
7494 * "res" is the expected result.
7496 struct {
7497 const char *aff;
7498 const char *tuple;
7499 const char *res;
7500 } unbind_aff_tests[] = {
7501 { "[M, N] -> { [M + floor(N/2)] }",
7502 "{ A[M, N] }",
7503 "{ A[M, N] -> [M + floor(N/2)] }" },
7504 { "[M, N] -> { [M + floor(N/2)] }",
7505 "{ B[N, M] }",
7506 "{ B[N, M] -> [M + floor(N/2)] }" },
7507 { "[M, N] -> { [M + floor(N/2)] }",
7508 "{ C[N] }",
7509 "[M] -> { C[N] -> [M + floor(N/2)] }" },
7510 { "[M, N] -> { [M + floor(N/2)] }",
7511 "{ D[A, B, C, N, Z] }",
7512 "[M] -> { D[A, B, C, N, Z] -> [M + floor(N/2)] }" },
7515 /* Perform basic isl_aff_unbind_params_insert_domain tests.
7517 static isl_stat test_unbind_aff(isl_ctx *ctx)
7519 int i;
7521 for (i = 0; i < ARRAY_SIZE(unbind_aff_tests); ++i) {
7522 const char *str;
7523 isl_aff *aff;
7524 isl_multi_id *tuple;
7525 isl_stat r;
7527 aff = isl_aff_read_from_str(ctx, unbind_aff_tests[i].aff);
7528 str = unbind_aff_tests[i].tuple;
7529 tuple = isl_multi_id_read_from_str(ctx, str);
7530 aff = isl_aff_unbind_params_insert_domain(aff, tuple);
7531 r = aff_check_plain_equal(aff, unbind_aff_tests[i].res);
7532 isl_aff_free(aff);
7533 if (r < 0)
7534 return isl_stat_error;
7537 return isl_stat_ok;
7540 /* Inputs for isl_multi_aff_unbind_params_insert_domain tests.
7541 * "ma" is the input multi affine expression defined over a parameter domain.
7542 * "tuple" is the tuple of the domain that gets introduced.
7543 * "res" is the expected result.
7545 static struct {
7546 const char *ma;
7547 const char *tuple;
7548 const char *res;
7549 } unbind_multi_aff_tests[] = {
7550 { "[M, N] -> { T[M + floor(N/2)] }",
7551 "{ A[M, N] }",
7552 "{ A[M, N] -> T[M + floor(N/2)] }" },
7553 { "[M, N] -> { [M + floor(N/2)] }",
7554 "{ B[N, M] }",
7555 "{ B[N, M] -> [M + floor(N/2)] }" },
7556 { "[M, N] -> { [M + floor(N/2)] }",
7557 "{ C[N] }",
7558 "[M] -> { C[N] -> [M + floor(N/2)] }" },
7559 { "[M, N] -> { [M + floor(N/2)] }",
7560 "{ D[A, B, C, N, Z] }",
7561 "[M] -> { D[A, B, C, N, Z] -> [M + floor(N/2)] }" },
7562 { "[M, N] -> { T[M + floor(N/2), N + floor(M/3)] }",
7563 "{ A[M, N] }",
7564 "{ A[M, N] -> T[M + floor(N/2), N + floor(M/3)] }" },
7567 /* Perform basic isl_multi_aff_unbind_params_insert_domain tests.
7569 static isl_stat test_unbind_multi_aff(isl_ctx *ctx)
7571 int i;
7573 for (i = 0; i < ARRAY_SIZE(unbind_multi_aff_tests); ++i) {
7574 const char *str;
7575 isl_multi_aff *ma;
7576 isl_multi_id *tuple;
7577 isl_stat r;
7579 str = unbind_multi_aff_tests[i].ma;
7580 ma = isl_multi_aff_read_from_str(ctx, str);
7581 str = unbind_multi_aff_tests[i].tuple;
7582 tuple = isl_multi_id_read_from_str(ctx, str);
7583 ma = isl_multi_aff_unbind_params_insert_domain(ma, tuple);
7584 str = unbind_multi_aff_tests[i].res;
7585 r = multi_aff_check_plain_equal(ma, str);
7586 isl_multi_aff_free(ma);
7587 if (r < 0)
7588 return isl_stat_error;
7591 return isl_stat_ok;
7594 /* Perform tests that reinterpret parameters.
7596 static int test_unbind(isl_ctx *ctx)
7598 if (test_unbind_set(ctx) < 0)
7599 return -1;
7600 if (test_unbind_aff(ctx) < 0)
7601 return -1;
7602 if (test_unbind_multi_aff(ctx) < 0)
7603 return -1;
7605 return 0;
7608 /* Check that "pa" consists of a single expression.
7610 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
7612 isl_size n;
7614 n = isl_pw_aff_n_piece(pa);
7615 isl_pw_aff_free(pa);
7617 if (n < 0)
7618 return -1;
7619 if (n != 1)
7620 isl_die(ctx, isl_error_unknown, "expecting single expression",
7621 return -1);
7623 return 0;
7626 /* Check that the computation below results in a single expression.
7627 * One or two expressions may result depending on which constraint
7628 * ends up being considered as redundant with respect to the other
7629 * constraints after the projection that is performed internally
7630 * by isl_set_dim_min.
7632 static int test_dim_max_1(isl_ctx *ctx)
7634 const char *str;
7635 isl_set *set;
7636 isl_pw_aff *pa;
7638 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
7639 "-4a <= b <= 3 and b < n - 4a }";
7640 set = isl_set_read_from_str(ctx, str);
7641 pa = isl_set_dim_min(set, 0);
7642 return check_single_piece(ctx, pa);
7645 /* Check that the computation below results in a single expression.
7646 * The PIP problem corresponding to these constraints has a row
7647 * that causes a split of the solution domain. The solver should
7648 * first pick rows that split off empty parts such that the actual
7649 * solution domain does not get split.
7650 * Note that the description contains some redundant constraints.
7651 * If these constraints get removed first, then the row mentioned
7652 * above does not appear in the PIP problem.
7654 static int test_dim_max_2(isl_ctx *ctx)
7656 const char *str;
7657 isl_set *set;
7658 isl_pw_aff *pa;
7660 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
7661 "N > 0 and P >= 0 }";
7662 set = isl_set_read_from_str(ctx, str);
7663 pa = isl_set_dim_max(set, 0);
7664 return check_single_piece(ctx, pa);
7667 int test_dim_max(isl_ctx *ctx)
7669 int equal;
7670 const char *str;
7671 isl_set *set1, *set2;
7672 isl_set *set;
7673 isl_map *map;
7674 isl_pw_aff *pwaff;
7676 if (test_dim_max_1(ctx) < 0)
7677 return -1;
7678 if (test_dim_max_2(ctx) < 0)
7679 return -1;
7681 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
7682 set = isl_set_read_from_str(ctx, str);
7683 pwaff = isl_set_dim_max(set, 0);
7684 set1 = isl_set_from_pw_aff(pwaff);
7685 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
7686 set2 = isl_set_read_from_str(ctx, str);
7687 equal = isl_set_is_equal(set1, set2);
7688 isl_set_free(set1);
7689 isl_set_free(set2);
7690 if (equal < 0)
7691 return -1;
7692 if (!equal)
7693 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7695 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
7696 set = isl_set_read_from_str(ctx, str);
7697 pwaff = isl_set_dim_max(set, 0);
7698 set1 = isl_set_from_pw_aff(pwaff);
7699 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
7700 set2 = isl_set_read_from_str(ctx, str);
7701 equal = isl_set_is_equal(set1, set2);
7702 isl_set_free(set1);
7703 isl_set_free(set2);
7704 if (equal < 0)
7705 return -1;
7706 if (!equal)
7707 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7709 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
7710 set = isl_set_read_from_str(ctx, str);
7711 pwaff = isl_set_dim_max(set, 0);
7712 set1 = isl_set_from_pw_aff(pwaff);
7713 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
7714 set2 = isl_set_read_from_str(ctx, str);
7715 equal = isl_set_is_equal(set1, set2);
7716 isl_set_free(set1);
7717 isl_set_free(set2);
7718 if (equal < 0)
7719 return -1;
7720 if (!equal)
7721 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7723 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
7724 "0 <= i < N and 0 <= j < M }";
7725 map = isl_map_read_from_str(ctx, str);
7726 set = isl_map_range(map);
7728 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
7729 set1 = isl_set_from_pw_aff(pwaff);
7730 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
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 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
7737 set1 = isl_set_from_pw_aff(pwaff);
7738 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
7739 set2 = isl_set_read_from_str(ctx, str);
7740 if (equal >= 0 && equal)
7741 equal = isl_set_is_equal(set1, set2);
7742 isl_set_free(set1);
7743 isl_set_free(set2);
7745 isl_set_free(set);
7747 if (equal < 0)
7748 return -1;
7749 if (!equal)
7750 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7752 /* Check that solutions are properly merged. */
7753 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
7754 "c <= -1 + n - 4a - 2b and c >= -2b and "
7755 "4a >= -4 + n and c >= 0 }";
7756 set = isl_set_read_from_str(ctx, str);
7757 pwaff = isl_set_dim_min(set, 2);
7758 set1 = isl_set_from_pw_aff(pwaff);
7759 str = "[n] -> { [(0)] : n >= 1 }";
7760 set2 = isl_set_read_from_str(ctx, str);
7761 equal = isl_set_is_equal(set1, set2);
7762 isl_set_free(set1);
7763 isl_set_free(set2);
7765 if (equal < 0)
7766 return -1;
7767 if (!equal)
7768 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7770 /* Check that empty solution lie in the right space. */
7771 str = "[n] -> { [t,a] : 1 = 0 }";
7772 set = isl_set_read_from_str(ctx, str);
7773 pwaff = isl_set_dim_max(set, 0);
7774 set1 = isl_set_from_pw_aff(pwaff);
7775 str = "[n] -> { [t] : 1 = 0 }";
7776 set2 = isl_set_read_from_str(ctx, str);
7777 equal = isl_set_is_equal(set1, set2);
7778 isl_set_free(set1);
7779 isl_set_free(set2);
7781 if (equal < 0)
7782 return -1;
7783 if (!equal)
7784 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7786 return 0;
7789 /* Basic test for isl_pw_multi_aff_product.
7791 * Check that multiple pieces are properly handled.
7793 static int test_product_pma(isl_ctx *ctx)
7795 isl_stat equal;
7796 const char *str;
7797 isl_pw_multi_aff *pma1, *pma2;
7799 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
7800 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
7801 str = "{ C[] -> D[] }";
7802 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
7803 pma1 = isl_pw_multi_aff_product(pma1, pma2);
7804 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
7805 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
7806 equal = pw_multi_aff_check_plain_equal(pma1, str);
7807 isl_pw_multi_aff_free(pma1);
7808 if (equal < 0)
7809 return -1;
7811 return 0;
7814 int test_product(isl_ctx *ctx)
7816 const char *str;
7817 isl_set *set;
7818 isl_union_set *uset1, *uset2;
7819 int ok;
7821 str = "{ A[i] }";
7822 set = isl_set_read_from_str(ctx, str);
7823 set = isl_set_product(set, isl_set_copy(set));
7824 ok = isl_set_is_wrapping(set);
7825 isl_set_free(set);
7826 if (ok < 0)
7827 return -1;
7828 if (!ok)
7829 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7831 str = "{ [] }";
7832 uset1 = isl_union_set_read_from_str(ctx, str);
7833 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
7834 str = "{ [[] -> []] }";
7835 uset2 = isl_union_set_read_from_str(ctx, str);
7836 ok = isl_union_set_is_equal(uset1, uset2);
7837 isl_union_set_free(uset1);
7838 isl_union_set_free(uset2);
7839 if (ok < 0)
7840 return -1;
7841 if (!ok)
7842 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7844 if (test_product_pma(ctx) < 0)
7845 return -1;
7847 return 0;
7850 /* Check that two sets are not considered disjoint just because
7851 * they have a different set of (named) parameters.
7853 static int test_disjoint(isl_ctx *ctx)
7855 const char *str;
7856 isl_set *set, *set2;
7857 int disjoint;
7859 str = "[n] -> { [[]->[]] }";
7860 set = isl_set_read_from_str(ctx, str);
7861 str = "{ [[]->[]] }";
7862 set2 = isl_set_read_from_str(ctx, str);
7863 disjoint = isl_set_is_disjoint(set, set2);
7864 isl_set_free(set);
7865 isl_set_free(set2);
7866 if (disjoint < 0)
7867 return -1;
7868 if (disjoint)
7869 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7871 return 0;
7874 /* Inputs for isl_pw_multi_aff_is_equal tests.
7875 * "f1" and "f2" are the two function that need to be compared.
7876 * "equal" is the expected result.
7878 struct {
7879 int equal;
7880 const char *f1;
7881 const char *f2;
7882 } pma_equal_tests[] = {
7883 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
7884 "[N] -> { [0] : 0 <= N <= 1 }" },
7885 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
7886 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
7887 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
7888 "[N] -> { [0] : 0 <= N <= 1 }" },
7889 { 0, "{ [NaN] }", "{ [NaN] }" },
7892 int test_equal(isl_ctx *ctx)
7894 int i;
7895 const char *str;
7896 isl_set *set, *set2;
7897 int equal;
7899 str = "{ S_6[i] }";
7900 set = isl_set_read_from_str(ctx, str);
7901 str = "{ S_7[i] }";
7902 set2 = isl_set_read_from_str(ctx, str);
7903 equal = isl_set_is_equal(set, set2);
7904 isl_set_free(set);
7905 isl_set_free(set2);
7906 if (equal < 0)
7907 return -1;
7908 if (equal)
7909 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7911 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
7912 int expected = pma_equal_tests[i].equal;
7913 isl_pw_multi_aff *f1, *f2;
7915 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
7916 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
7917 equal = isl_pw_multi_aff_is_equal(f1, f2);
7918 isl_pw_multi_aff_free(f1);
7919 isl_pw_multi_aff_free(f2);
7920 if (equal < 0)
7921 return -1;
7922 if (equal != expected)
7923 isl_die(ctx, isl_error_unknown,
7924 "unexpected equality result", return -1);
7927 return 0;
7930 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
7931 enum isl_dim_type type, unsigned pos, int fixed)
7933 isl_bool test;
7935 test = isl_map_plain_is_fixed(map, type, pos, NULL);
7936 isl_map_free(map);
7937 if (test < 0)
7938 return -1;
7939 if (test == fixed)
7940 return 0;
7941 if (fixed)
7942 isl_die(ctx, isl_error_unknown,
7943 "map not detected as fixed", return -1);
7944 else
7945 isl_die(ctx, isl_error_unknown,
7946 "map detected as fixed", return -1);
7949 int test_fixed(isl_ctx *ctx)
7951 const char *str;
7952 isl_map *map;
7954 str = "{ [i] -> [i] }";
7955 map = isl_map_read_from_str(ctx, str);
7956 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
7957 return -1;
7958 str = "{ [i] -> [1] }";
7959 map = isl_map_read_from_str(ctx, str);
7960 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7961 return -1;
7962 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
7963 map = isl_map_read_from_str(ctx, str);
7964 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7965 return -1;
7966 map = isl_map_read_from_str(ctx, str);
7967 map = isl_map_neg(map);
7968 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
7969 return -1;
7971 return 0;
7974 struct isl_vertices_test_data {
7975 const char *set;
7976 int n;
7977 const char *vertex[6];
7978 } vertices_tests[] = {
7979 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
7980 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
7981 { "{ A[t, i] : t = 14 and i = 1 }",
7982 1, { "{ A[14, 1] }" } },
7983 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
7984 "c <= m and m <= n and m > 0 }",
7985 6, {
7986 "[n, m] -> { [n, m, m] : 0 < m <= n }",
7987 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
7988 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
7989 "[n, m] -> { [m, m, m] : 0 < m <= n }",
7990 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
7991 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
7992 } },
7993 /* An input with implicit equality constraints among the parameters. */
7994 { "[N, M] -> { [a, b] : M >= 3 and 9 + 3M <= a <= 29 + 2N + 11M and "
7995 "2b >= M + a and 5 - 2N - M + a <= 2b <= 3 + a and "
7996 "3b >= 15 + a }",
7997 2, {
7998 "[N, M] -> { [(21), (12)] : M = 3 and N >= 0 }",
7999 "[N, M] -> { [(61 + 2N), (32 + N)] : M = 3 and N >= 0 }",
8004 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
8006 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
8008 struct isl_vertices_test_data *data = user;
8009 isl_ctx *ctx;
8010 isl_multi_aff *ma;
8011 isl_basic_set *bset;
8012 isl_pw_multi_aff *pma;
8013 int i;
8014 isl_bool equal;
8016 ctx = isl_vertex_get_ctx(vertex);
8017 bset = isl_vertex_get_domain(vertex);
8018 ma = isl_vertex_get_expr(vertex);
8019 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
8021 for (i = 0; i < data->n; ++i) {
8022 isl_pw_multi_aff *pma_i;
8024 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
8025 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
8026 isl_pw_multi_aff_free(pma_i);
8028 if (equal < 0 || equal)
8029 break;
8032 isl_pw_multi_aff_free(pma);
8033 isl_vertex_free(vertex);
8035 if (equal < 0)
8036 return isl_stat_error;
8038 return equal ? isl_stat_ok : isl_stat_error;
8041 int test_vertices(isl_ctx *ctx)
8043 int i;
8045 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
8046 isl_basic_set *bset;
8047 isl_vertices *vertices;
8048 int ok = 1;
8049 isl_size n;
8051 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
8052 vertices = isl_basic_set_compute_vertices(bset);
8053 n = isl_vertices_get_n_vertices(vertices);
8054 if (vertices_tests[i].n != n)
8055 ok = 0;
8056 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
8057 &vertices_tests[i]) < 0)
8058 ok = 0;
8059 isl_vertices_free(vertices);
8060 isl_basic_set_free(bset);
8062 if (n < 0)
8063 return -1;
8064 if (!ok)
8065 isl_die(ctx, isl_error_unknown, "unexpected vertices",
8066 return -1);
8069 return 0;
8072 /* Inputs for basic tests of binary operations on isl_union_map.
8073 * "fn" is the function that is being tested.
8074 * "arg1" and "arg2" are string descriptions of the inputs.
8075 * "res" is a string description of the expected result.
8077 static struct {
8078 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap1,
8079 __isl_take isl_union_map *umap2);
8080 const char *arg1;
8081 const char *arg2;
8082 const char *res;
8083 } umap_bin_tests[] = {
8084 { &isl_union_map_intersect,
8085 "[n] -> { A[i] -> [] : 0 <= i <= n; B[] -> [] }",
8086 "[m] -> { A[i] -> [] : 0 <= i <= m; C[] -> [] }",
8087 "[m, n] -> { A[i] -> [] : 0 <= i <= n and i <= m }" },
8088 { &isl_union_map_intersect_domain_factor_domain,
8089 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8090 "[N] -> { B[i] -> C[N] }",
8091 "{ }" },
8092 { &isl_union_map_intersect_domain_factor_domain,
8093 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8094 "[N] -> { A[i] -> C[N] }",
8095 "[N] -> { [A[N - 2] -> B[N - 1]] -> C[N] }" },
8096 { &isl_union_map_intersect_domain_factor_domain,
8097 "{ T[A[i] -> B[i + 1]] -> C[i + 2] }",
8098 "[N] -> { A[i] -> C[N] }",
8099 "[N] -> { T[A[N - 2] -> B[N - 1]] -> C[N] }" },
8100 { &isl_union_map_intersect_domain_factor_range,
8101 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8102 "[N] -> { B[i] -> C[N] }",
8103 "[N] -> { [A[N - 2] -> B[N - 1]] -> C[N] }" },
8104 { &isl_union_map_intersect_domain_factor_range,
8105 "{ T[A[i] -> B[i + 1]] -> C[i + 2] }",
8106 "[N] -> { B[i] -> C[N] }",
8107 "[N] -> { T[A[N - 2] -> B[N - 1]] -> C[N] }" },
8108 { &isl_union_map_intersect_domain_factor_range,
8109 "{ [A[i] -> B[i + 1]] -> C[i + 2] }",
8110 "[N] -> { A[i] -> C[N] }",
8111 "{ }" },
8112 { &isl_union_map_intersect_range_factor_domain,
8113 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8114 "[N] -> { A[i] -> B[N] }",
8115 "[N] -> { A[N - 1] -> [B[N] -> C[N + 1]] }" },
8116 { &isl_union_map_intersect_range_factor_domain,
8117 "{ A[i] -> T[B[i + 1] -> C[i + 2]] }",
8118 "[N] -> { A[i] -> B[N] }",
8119 "[N] -> { A[N - 1] -> T[B[N] -> C[N + 1]] }" },
8120 { &isl_union_map_intersect_range_factor_domain,
8121 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8122 "[N] -> { A[i] -> C[N] }",
8123 "{ }" },
8124 { &isl_union_map_intersect_range_factor_range,
8125 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8126 "[N] -> { A[i] -> C[N] }",
8127 "[N] -> { A[N - 2] -> [B[N - 1] -> C[N]] }" },
8128 { &isl_union_map_intersect_range_factor_range,
8129 "{ A[i] -> T[B[i + 1] -> C[i + 2]] }",
8130 "[N] -> { A[i] -> C[N] }",
8131 "[N] -> { A[N - 2] -> T[B[N - 1] -> C[N]] }" },
8132 { &isl_union_map_intersect_range_factor_range,
8133 "{ A[i] -> [B[i + 1] -> C[i + 2]] }",
8134 "[N] -> { A[i] -> B[N] }",
8135 "{ }" },
8138 /* Perform basic tests of binary operations on isl_union_map.
8140 static isl_stat test_bin_union_map(isl_ctx *ctx)
8142 int i;
8144 for (i = 0; i < ARRAY_SIZE(umap_bin_tests); ++i) {
8145 const char *str;
8146 isl_union_map *umap1, *umap2, *res;
8147 isl_bool equal;
8149 str = umap_bin_tests[i].arg1;
8150 umap1 = isl_union_map_read_from_str(ctx, str);
8151 str = umap_bin_tests[i].arg2;
8152 umap2 = isl_union_map_read_from_str(ctx, str);
8153 str = umap_bin_tests[i].res;
8154 res = isl_union_map_read_from_str(ctx, str);
8155 umap1 = umap_bin_tests[i].fn(umap1, umap2);
8156 equal = isl_union_map_is_equal(umap1, res);
8157 isl_union_map_free(umap1);
8158 isl_union_map_free(res);
8159 if (equal < 0)
8160 return isl_stat_error;
8161 if (!equal)
8162 isl_die(ctx, isl_error_unknown,
8163 "unexpected result", return isl_stat_error);
8166 return isl_stat_ok;
8169 /* Check that isl_union_set_contains finds space independently
8170 * of the parameters.
8172 static isl_stat test_union_set_contains(isl_ctx *ctx)
8174 const char *str;
8175 isl_bool ok;
8176 isl_space *space;
8177 isl_id *id;
8178 isl_union_set *uset;
8180 str = "[N] -> { A[0:N]; B[*,*] }";
8181 uset = isl_union_set_read_from_str(ctx, str);
8182 space = isl_space_unit(ctx);
8183 id = isl_id_alloc(ctx, "A", NULL);
8184 space = isl_space_add_named_tuple_id_ui(space, id, 1);
8185 ok = isl_union_set_contains(uset, space);
8186 isl_space_free(space);
8187 isl_union_set_free(uset);
8189 if (ok < 0)
8190 return isl_stat_error;
8191 if (!ok)
8192 isl_die(ctx, isl_error_unknown,
8193 "unexpected result", return isl_stat_error);
8195 return isl_stat_ok;
8198 /* Perform basic tests of operations on isl_union_map or isl_union_set.
8200 static int test_union_map(isl_ctx *ctx)
8202 if (test_bin_union_map(ctx) < 0)
8203 return -1;
8204 if (test_union_set_contains(ctx) < 0)
8205 return -1;
8206 return 0;
8209 #undef BASE
8210 #define BASE union_pw_qpolynomial
8211 #include "isl_test_plain_equal_templ.c"
8213 /* Check that the result of applying "fn" to "a" and "b"
8214 * in (obviously) equal to "res".
8216 static isl_stat test_union_pw_op(isl_ctx *ctx, const char *a, const char *b,
8217 __isl_give isl_union_pw_qpolynomial *(*fn)(
8218 __isl_take isl_union_pw_qpolynomial *upwqp1,
8219 __isl_take isl_union_pw_qpolynomial *upwqp2),
8220 const char *res)
8222 isl_stat r;
8223 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
8225 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, a);
8226 upwqp2 = isl_union_pw_qpolynomial_read_from_str(ctx, b);
8227 upwqp1 = fn(upwqp1, upwqp2);
8228 r = union_pw_qpolynomial_check_plain_equal(upwqp1, res);
8229 isl_union_pw_qpolynomial_free(upwqp1);
8231 return r;
8234 int test_union_pw(isl_ctx *ctx)
8236 int equal;
8237 isl_stat r;
8238 const char *str;
8239 isl_union_set *uset;
8240 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
8241 const char *a, *b;
8243 str = "{ [x] -> x^2 }";
8244 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
8245 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
8246 uset = isl_union_pw_qpolynomial_domain(upwqp1);
8247 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
8248 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
8249 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
8250 isl_union_pw_qpolynomial_free(upwqp1);
8251 isl_union_pw_qpolynomial_free(upwqp2);
8252 if (equal < 0)
8253 return -1;
8254 if (!equal)
8255 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
8257 a = "{ A[x] -> x^2 : x >= 0; B[x] -> x }";
8258 b = "{ A[x] -> x }";
8259 str = "{ A[x] -> x^2 + x : x >= 0; A[x] -> x : x < 0; B[x] -> x }";
8260 if (test_union_pw_op(ctx, a, b, &isl_union_pw_qpolynomial_add, str) < 0)
8261 return -1;
8262 str = "{ A[x] -> x^2 - x : x >= 0; A[x] -> -x : x < 0; B[x] -> x }";
8263 if (test_union_pw_op(ctx, a, b, &isl_union_pw_qpolynomial_sub, str) < 0)
8264 return -1;
8266 str = "{ A[x] -> 0 }";
8267 a = "{ A[x] -> 1 }";
8268 b = "{ A[x] -> -1 }";
8269 if (test_union_pw_op(ctx, a, b, &isl_union_pw_qpolynomial_add, str) < 0)
8270 return -1;
8271 a = "{ A[x] -> 1 }";
8272 b = "{ A[x] -> 1 }";
8273 if (test_union_pw_op(ctx, a, b, &isl_union_pw_qpolynomial_sub, str) < 0)
8274 return -1;
8276 str = "{ [A[x] -> B[y,z]] -> x^2 + y * floor(x/4) * floor(z/2); "
8277 "C[z] -> z^3 }";
8278 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
8279 upwqp1 = isl_union_pw_qpolynomial_domain_reverse(upwqp1);
8280 str = "{ [B[y,z] -> A[x]] -> x^2 + y * floor(x/4) * floor(z/2) }";
8281 r = union_pw_qpolynomial_check_plain_equal(upwqp1, str);
8282 isl_union_pw_qpolynomial_free(upwqp1);
8283 if (r < 0)
8284 return -1;
8286 return 0;
8289 /* Inputs for basic tests of functions that select
8290 * subparts of the domain of an isl_multi_union_pw_aff.
8291 * "fn" is the function that is tested.
8292 * "arg" is a string description of the input.
8293 * "res" is a string description of the expected result.
8295 struct {
8296 __isl_give isl_union_set *(*fn)(
8297 __isl_take isl_multi_union_pw_aff *mupa);
8298 const char *arg;
8299 const char *res;
8300 } un_locus_tests[] = {
8301 { &isl_multi_union_pw_aff_zero_union_set,
8302 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
8303 "{ A[0,j]; B[0,j] }" },
8304 { &isl_multi_union_pw_aff_zero_union_set,
8305 "F[{ A[i,j] -> [i-j]; B[i,j] -> [i-j] : i >= 0 }]",
8306 "{ A[i,i]; B[i,i] : i >= 0 }" },
8307 { &isl_multi_union_pw_aff_zero_union_set,
8308 "(F[] : { A[i,j]; B[i,i] : i >= 0 })",
8309 "{ A[i,j]; B[i,i] : i >= 0 }" },
8312 /* Perform some basic tests of functions that select
8313 * subparts of the domain of an isl_multi_union_pw_aff.
8315 static int test_un_locus(isl_ctx *ctx)
8317 int i;
8318 isl_bool ok;
8319 isl_union_set *uset, *res;
8320 isl_multi_union_pw_aff *mupa;
8322 for (i = 0; i < ARRAY_SIZE(un_locus_tests); ++i) {
8323 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
8324 un_locus_tests[i].arg);
8325 res = isl_union_set_read_from_str(ctx, un_locus_tests[i].res);
8326 uset = un_locus_tests[i].fn(mupa);
8327 ok = isl_union_set_is_equal(uset, res);
8328 isl_union_set_free(uset);
8329 isl_union_set_free(res);
8330 if (ok < 0)
8331 return -1;
8332 if (!ok)
8333 isl_die(ctx, isl_error_unknown,
8334 "unexpected result", return -1);
8337 return 0;
8340 /* Inputs for basic tests of functions that select
8341 * subparts of an isl_union_map based on a relation
8342 * specified by an isl_multi_union_pw_aff.
8343 * "fn" is the function that is tested.
8344 * "arg1" and "arg2" are string descriptions of the inputs.
8345 * "res" is a string description of the expected result.
8347 struct {
8348 __isl_give isl_union_map *(*fn)(
8349 __isl_take isl_union_map *umap,
8350 __isl_take isl_multi_union_pw_aff *mupa);
8351 const char *arg1;
8352 const char *arg2;
8353 const char *res;
8354 } bin_locus_tests[] = {
8355 { &isl_union_map_eq_at_multi_union_pw_aff,
8356 "{ A[i,j] -> B[i',j'] }",
8357 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
8358 "{ A[i,j] -> B[i,j'] }" },
8359 { &isl_union_map_eq_at_multi_union_pw_aff,
8360 "{ A[i,j] -> B[i',j'] }",
8361 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8362 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8363 "{ A[i,j] -> B[i,j] }" },
8364 { &isl_union_map_eq_at_multi_union_pw_aff,
8365 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
8366 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
8367 "{ A[i,j] -> B[i,j'] }" },
8368 { &isl_union_map_eq_at_multi_union_pw_aff,
8369 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
8370 "F[{ A[i,j] -> [i]; B[i,j] -> [i]; C[i,j] -> [0] }]",
8371 "{ A[i,j] -> B[i,j']; A[0,j] -> C[i',j'] }" },
8372 { &isl_union_map_eq_at_multi_union_pw_aff,
8373 "{ A[i,j] -> B[i',j'] }",
8374 "F[{ A[i,j] -> [i] : i > j; B[i,j] -> [i] }]",
8375 "{ A[i,j] -> B[i,j'] : i > j }" },
8376 { &isl_union_map_lex_le_at_multi_union_pw_aff,
8377 "{ A[i,j] -> B[i',j'] }",
8378 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8379 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8380 "{ A[i,j] -> B[i',j'] : i,j <<= i',j' }" },
8381 { &isl_union_map_lex_lt_at_multi_union_pw_aff,
8382 "{ A[i,j] -> B[i',j'] }",
8383 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8384 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8385 "{ A[i,j] -> B[i',j'] : i,j << i',j' }" },
8386 { &isl_union_map_lex_ge_at_multi_union_pw_aff,
8387 "{ A[i,j] -> B[i',j'] }",
8388 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8389 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8390 "{ A[i,j] -> B[i',j'] : i,j >>= i',j' }" },
8391 { &isl_union_map_lex_gt_at_multi_union_pw_aff,
8392 "{ A[i,j] -> B[i',j'] }",
8393 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
8394 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
8395 "{ A[i,j] -> B[i',j'] : i,j >> i',j' }" },
8396 { &isl_union_map_eq_at_multi_union_pw_aff,
8397 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
8398 "(F[] : { A[i,j]; B[i,j] })",
8399 "{ A[i,j] -> B[i',j'] }" },
8400 { &isl_union_map_eq_at_multi_union_pw_aff,
8401 "{ A[i,j] -> B[i',j'] }",
8402 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
8403 "{ A[i,j] -> B[i',j'] : i > j and i' < j' }" },
8404 { &isl_union_map_eq_at_multi_union_pw_aff,
8405 "[N] -> { A[i,j] -> B[i',j'] : i,i' <= N }",
8406 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
8407 "[N] -> { A[i,j] -> B[i',j'] : i > j and i' < j' and i,i' <= N }" },
8408 { &isl_union_map_eq_at_multi_union_pw_aff,
8409 "{ A[i,j] -> B[i',j'] }",
8410 "[N] -> (F[] : { A[i,j] : i < N; B[i,j] : i < N })",
8411 "[N] -> { A[i,j] -> B[i',j'] : i,i' < N }" },
8412 { &isl_union_map_eq_at_multi_union_pw_aff,
8413 "{ A[i,j] -> B[i',j'] }",
8414 "[N] -> (F[] : { : N >= 0 })",
8415 "[N] -> { A[i,j] -> B[i',j'] : N >= 0 }" },
8418 /* Perform some basic tests of functions that select
8419 * subparts of an isl_union_map based on a relation
8420 * specified by an isl_multi_union_pw_aff.
8422 static int test_bin_locus(isl_ctx *ctx)
8424 int i;
8425 isl_bool ok;
8426 isl_union_map *umap, *res;
8427 isl_multi_union_pw_aff *mupa;
8429 for (i = 0; i < ARRAY_SIZE(bin_locus_tests); ++i) {
8430 umap = isl_union_map_read_from_str(ctx,
8431 bin_locus_tests[i].arg1);
8432 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
8433 bin_locus_tests[i].arg2);
8434 res = isl_union_map_read_from_str(ctx, bin_locus_tests[i].res);
8435 umap = bin_locus_tests[i].fn(umap, mupa);
8436 ok = isl_union_map_is_equal(umap, res);
8437 isl_union_map_free(umap);
8438 isl_union_map_free(res);
8439 if (ok < 0)
8440 return -1;
8441 if (!ok)
8442 isl_die(ctx, isl_error_unknown,
8443 "unexpected result", return -1);
8446 return 0;
8449 /* Inputs for basic tests of functions that determine
8450 * the part of the domain where two isl_multi_aff objects
8451 * related to each other in a specific way.
8452 * "fn" is the function that is being tested.
8453 * "arg1" and "arg2" are string descriptions of the inputs.
8454 * "res" is a string description of the expected result.
8456 static struct {
8457 __isl_give isl_set *(*fn)(__isl_take isl_multi_aff *ma1,
8458 __isl_take isl_multi_aff *ma2);
8459 const char *arg1;
8460 const char *arg2;
8461 const char *res;
8462 } bin_locus_ma_tests[] = {
8463 { &isl_multi_aff_lex_le_set, "{ [] }", "{ [] }", "{ : }" },
8464 { &isl_multi_aff_lex_lt_set, "{ [] }", "{ [] }", "{ : false }" },
8465 { &isl_multi_aff_lex_le_set,
8466 "{ A[i] -> [i] }", "{ A[i] -> [0] }",
8467 "{ A[i] : i <= 0 }" },
8468 { &isl_multi_aff_lex_lt_set,
8469 "{ A[i] -> [i] }", "{ A[i] -> [0] }",
8470 "{ A[i] : i < 0 }" },
8471 { &isl_multi_aff_lex_le_set,
8472 "{ A[i] -> [i, i] }", "{ A[i] -> [0, 0] }",
8473 "{ A[i] : i <= 0 }" },
8474 { &isl_multi_aff_lex_le_set,
8475 "{ A[i] -> [i, 0] }", "{ A[i] -> [0, 0] }",
8476 "{ A[i] : i <= 0 }" },
8477 { &isl_multi_aff_lex_le_set,
8478 "{ A[i] -> [i, 1] }", "{ A[i] -> [0, 0] }",
8479 "{ A[i] : i < 0 }" },
8482 /* Perform some basic tests of functions that determine
8483 * the part of the domain where two isl_multi_aff objects
8484 * related to each other in a specific way.
8486 static isl_stat test_bin_locus_ma(isl_ctx *ctx)
8488 int i;
8490 for (i = 0; i < ARRAY_SIZE(bin_locus_ma_tests); ++i) {
8491 const char *str;
8492 isl_bool ok;
8493 isl_multi_aff *ma1, *ma2;
8494 isl_set *set, *res;
8496 str = bin_locus_ma_tests[i].arg1;
8497 ma1 = isl_multi_aff_read_from_str(ctx, str);
8498 str = bin_locus_ma_tests[i].arg2;
8499 ma2 = isl_multi_aff_read_from_str(ctx, str);
8500 res = isl_set_read_from_str(ctx, bin_locus_ma_tests[i].res);
8501 set = bin_locus_ma_tests[i].fn(ma1, ma2);
8502 ok = isl_set_is_equal(set, res);
8503 isl_set_free(set);
8504 isl_set_free(res);
8505 if (ok < 0)
8506 return isl_stat_error;
8507 if (!ok)
8508 isl_die(ctx, isl_error_unknown,
8509 "unexpected result", return isl_stat_error);
8512 return isl_stat_ok;
8515 /* Perform basic locus tests.
8517 static int test_locus(isl_ctx *ctx)
8519 if (test_un_locus(ctx) < 0)
8520 return -1;
8521 if (test_bin_locus(ctx) < 0)
8522 return -1;
8523 if (test_bin_locus_ma(ctx) < 0)
8524 return -1;
8525 return 0;
8528 /* Test that isl_union_pw_qpolynomial_eval picks up the function
8529 * defined over the correct domain space.
8531 static int test_eval_1(isl_ctx *ctx)
8533 const char *str;
8534 isl_point *pnt;
8535 isl_set *set;
8536 isl_union_pw_qpolynomial *upwqp;
8537 isl_val *v;
8538 int cmp;
8540 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
8541 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
8542 str = "{ A[6] }";
8543 set = isl_set_read_from_str(ctx, str);
8544 pnt = isl_set_sample_point(set);
8545 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
8546 cmp = isl_val_cmp_si(v, 36);
8547 isl_val_free(v);
8549 if (!v)
8550 return -1;
8551 if (cmp != 0)
8552 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
8554 return 0;
8557 /* Check that isl_qpolynomial_eval handles getting called on a void point.
8559 static int test_eval_2(isl_ctx *ctx)
8561 const char *str;
8562 isl_point *pnt;
8563 isl_set *set;
8564 isl_qpolynomial *qp;
8565 isl_val *v;
8566 isl_bool ok;
8568 str = "{ A[x] -> [x] }";
8569 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
8570 str = "{ A[x] : false }";
8571 set = isl_set_read_from_str(ctx, str);
8572 pnt = isl_set_sample_point(set);
8573 v = isl_qpolynomial_eval(qp, pnt);
8574 ok = isl_val_is_nan(v);
8575 isl_val_free(v);
8577 if (ok < 0)
8578 return -1;
8579 if (!ok)
8580 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
8582 return 0;
8585 /* Check that a polynomial (without local variables) can be evaluated
8586 * in a rational point.
8588 static isl_stat test_eval_3(isl_ctx *ctx)
8590 isl_pw_qpolynomial *pwqp;
8591 isl_point *pnt;
8592 isl_val *v;
8593 isl_stat r;
8595 pwqp = isl_pw_qpolynomial_read_from_str(ctx, "{ [x] -> x^2 }");
8596 pnt = isl_point_zero(isl_pw_qpolynomial_get_domain_space(pwqp));
8597 v = isl_val_read_from_str(ctx, "1/2");
8598 pnt = isl_point_set_coordinate_val(pnt, isl_dim_set, 0, v);
8599 v = isl_pw_qpolynomial_eval(pwqp, pnt);
8600 r = val_check_equal(v, "1/4");
8601 isl_val_free(v);
8603 return r;
8606 /* Inputs for isl_pw_aff_eval test.
8607 * "f" is the affine function.
8608 * "p" is the point where the function should be evaluated.
8609 * "res" is the expected result.
8611 struct {
8612 const char *f;
8613 const char *p;
8614 const char *res;
8615 } aff_eval_tests[] = {
8616 { "{ [i] -> [2 * i] }", "{ [4] }", "8" },
8617 { "{ [i] -> [2 * i] }", "{ [x] : false }", "NaN" },
8618 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [0] }", "0" },
8619 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [1] }", "1" },
8620 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [2] }", "3" },
8621 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [3] }", "5" },
8622 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [4] }", "7" },
8623 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [0] }", "0" },
8624 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [1] }", "0" },
8625 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [2] }", "0" },
8626 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [3] }", "0" },
8627 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [4] }", "1" },
8628 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [6] }", "1" },
8629 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [8] }", "2" },
8630 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [4] }", "4" },
8631 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [-2] }", "2" },
8632 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [0] }", "NaN" },
8633 { "[N] -> { [2 * N] }", "[N] -> { : N = 4 }", "8" },
8634 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 1] }", "1" },
8635 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 2] }", "3/2" },
8636 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [4] }", "4" },
8637 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [3] }", "NaN" },
8638 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [x] : false }", "NaN" },
8639 { "[m, n] -> { [2m + 3n] }", "[n=1, m=10] -> { : }", "23" },
8642 /* Perform basic isl_pw_aff_eval tests.
8644 static int test_eval_aff(isl_ctx *ctx)
8646 int i;
8648 for (i = 0; i < ARRAY_SIZE(aff_eval_tests); ++i) {
8649 isl_stat r;
8650 isl_pw_aff *pa;
8651 isl_set *set;
8652 isl_point *pnt;
8653 isl_val *v;
8655 pa = isl_pw_aff_read_from_str(ctx, aff_eval_tests[i].f);
8656 set = isl_set_read_from_str(ctx, aff_eval_tests[i].p);
8657 pnt = isl_set_sample_point(set);
8658 v = isl_pw_aff_eval(pa, pnt);
8659 r = val_check_equal(v, aff_eval_tests[i].res);
8660 isl_val_free(v);
8661 if (r < 0)
8662 return -1;
8664 return 0;
8667 /* Perform basic evaluation tests.
8669 static int test_eval(isl_ctx *ctx)
8671 if (test_eval_1(ctx) < 0)
8672 return -1;
8673 if (test_eval_2(ctx) < 0)
8674 return -1;
8675 if (test_eval_3(ctx) < 0)
8676 return -1;
8677 if (test_eval_aff(ctx) < 0)
8678 return -1;
8679 return 0;
8682 /* Descriptions of sets that are tested for reparsing after printing.
8684 const char *output_tests[] = {
8685 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
8686 "{ [x] : 1 = 0 }",
8687 "{ [x] : false }",
8688 "{ [x] : x mod 2 = 0 }",
8689 "{ [x] : x mod 2 = 1 }",
8690 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
8691 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
8692 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
8693 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
8694 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
8695 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
8698 /* Check that printing a set and reparsing a set from the printed output
8699 * results in the same set.
8701 static int test_output_set(isl_ctx *ctx)
8703 int i;
8704 char *str;
8705 isl_set *set1, *set2;
8706 isl_bool equal;
8708 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
8709 set1 = isl_set_read_from_str(ctx, output_tests[i]);
8710 str = isl_set_to_str(set1);
8711 set2 = isl_set_read_from_str(ctx, str);
8712 free(str);
8713 equal = isl_set_is_equal(set1, set2);
8714 isl_set_free(set1);
8715 isl_set_free(set2);
8716 if (equal < 0)
8717 return -1;
8718 if (!equal)
8719 isl_die(ctx, isl_error_unknown,
8720 "parsed output not the same", return -1);
8723 return 0;
8726 /* Check that an isl_multi_aff is printed using a consistent space.
8728 static isl_stat test_output_ma(isl_ctx *ctx)
8730 char *str;
8731 isl_bool equal;
8732 isl_aff *aff;
8733 isl_multi_aff *ma, *ma2;
8735 ma = isl_multi_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
8736 aff = isl_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
8737 ma = isl_multi_aff_set_aff(ma, 0, aff);
8738 str = isl_multi_aff_to_str(ma);
8739 ma2 = isl_multi_aff_read_from_str(ctx, str);
8740 free(str);
8741 equal = isl_multi_aff_plain_is_equal(ma, ma2);
8742 isl_multi_aff_free(ma2);
8743 isl_multi_aff_free(ma);
8745 if (equal < 0)
8746 return isl_stat_error;
8747 if (!equal)
8748 isl_die(ctx, isl_error_unknown, "bad conversion",
8749 return isl_stat_error);
8751 return isl_stat_ok;
8754 /* Check that an isl_multi_pw_aff is printed using a consistent space.
8756 static isl_stat test_output_mpa(isl_ctx *ctx)
8758 char *str;
8759 isl_bool equal;
8760 isl_pw_aff *pa;
8761 isl_multi_pw_aff *mpa, *mpa2;
8763 mpa = isl_multi_pw_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
8764 pa = isl_pw_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
8765 mpa = isl_multi_pw_aff_set_pw_aff(mpa, 0, pa);
8766 str = isl_multi_pw_aff_to_str(mpa);
8767 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
8768 free(str);
8769 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
8770 isl_multi_pw_aff_free(mpa2);
8771 isl_multi_pw_aff_free(mpa);
8773 if (equal < 0)
8774 return isl_stat_error;
8775 if (!equal)
8776 isl_die(ctx, isl_error_unknown, "bad conversion",
8777 return isl_stat_error);
8779 return isl_stat_ok;
8782 int test_output(isl_ctx *ctx)
8784 char *s;
8785 const char *str;
8786 isl_pw_aff *pa;
8787 isl_printer *p;
8788 int equal;
8790 if (test_output_set(ctx) < 0)
8791 return -1;
8792 if (test_output_ma(ctx) < 0)
8793 return -1;
8794 if (test_output_mpa(ctx) < 0)
8795 return -1;
8797 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
8798 pa = isl_pw_aff_read_from_str(ctx, str);
8800 p = isl_printer_to_str(ctx);
8801 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
8802 p = isl_printer_print_pw_aff(p, pa);
8803 s = isl_printer_get_str(p);
8804 isl_printer_free(p);
8805 isl_pw_aff_free(pa);
8806 if (!s)
8807 equal = -1;
8808 else
8809 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
8810 free(s);
8811 if (equal < 0)
8812 return -1;
8813 if (!equal)
8814 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
8816 return 0;
8819 int test_sample(isl_ctx *ctx)
8821 const char *str;
8822 isl_basic_set *bset1, *bset2;
8823 int empty, subset;
8825 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
8826 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
8827 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
8828 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
8829 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
8830 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
8831 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
8832 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
8833 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
8834 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
8835 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
8836 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
8837 "d - 1073741821e and "
8838 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
8839 "3j >= 1 - a + b + 2e and "
8840 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
8841 "3i <= 4 - a + 4b - e and "
8842 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
8843 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
8844 "c <= -1 + a and 3i >= -2 - a + 3e and "
8845 "1073741822e <= 1073741823 - a + 1073741822b + c and "
8846 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
8847 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
8848 "1073741823e >= 1 + 1073741823b - d and "
8849 "3i >= 1073741823b + c - 1073741823e - f and "
8850 "3i >= 1 + 2b + e + 3g }";
8851 bset1 = isl_basic_set_read_from_str(ctx, str);
8852 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
8853 empty = isl_basic_set_is_empty(bset2);
8854 subset = isl_basic_set_is_subset(bset2, bset1);
8855 isl_basic_set_free(bset1);
8856 isl_basic_set_free(bset2);
8857 if (empty < 0 || subset < 0)
8858 return -1;
8859 if (empty)
8860 isl_die(ctx, isl_error_unknown, "point not found", return -1);
8861 if (!subset)
8862 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
8864 return 0;
8867 /* Perform a projection on a basic set that is known to be empty
8868 * but that has not been assigned a canonical representation.
8869 * Earlier versions of isl would run into a stack overflow
8870 * on this example.
8872 static int test_empty_projection(isl_ctx *ctx)
8874 const char *str;
8875 isl_bool empty;
8876 isl_basic_set *bset;
8878 str = "{ [a, b, c, d, e, f, g, h] : 5f = 1 + 4a - b + 5c - d - 2e and "
8879 "3h = 2 + b + c and 14c >= 9 - 3a + 25b and "
8880 "4c <= 50 - 3a + 23b and 6b <= -39 + a and "
8881 "9g >= -6 + 3a + b + c and e < a + b - 2d and "
8882 "7d >= -5 + 2a + 2b and 5g >= -14 + a - 4b + d + 2e and "
8883 "9g <= -28 - 5b - 2c + 3d + 6e }";
8884 bset = isl_basic_set_read_from_str(ctx, str);
8885 empty = isl_basic_set_is_empty(bset);
8886 bset = isl_basic_set_params(bset);
8887 isl_basic_set_free(bset);
8889 if (empty < 0)
8890 return -1;
8892 return 0;
8895 int test_slice(isl_ctx *ctx)
8897 const char *str;
8898 isl_map *map;
8899 int equal;
8901 str = "{ [i] -> [j] }";
8902 map = isl_map_read_from_str(ctx, str);
8903 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
8904 equal = map_check_equal(map, "{ [i] -> [i] }");
8905 isl_map_free(map);
8906 if (equal < 0)
8907 return -1;
8909 str = "{ [i] -> [j] }";
8910 map = isl_map_read_from_str(ctx, str);
8911 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
8912 equal = map_check_equal(map, "{ [i] -> [j] }");
8913 isl_map_free(map);
8914 if (equal < 0)
8915 return -1;
8917 str = "{ [i] -> [j] }";
8918 map = isl_map_read_from_str(ctx, str);
8919 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
8920 equal = map_check_equal(map, "{ [i] -> [-i] }");
8921 isl_map_free(map);
8922 if (equal < 0)
8923 return -1;
8925 str = "{ [i] -> [j] }";
8926 map = isl_map_read_from_str(ctx, str);
8927 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
8928 equal = map_check_equal(map, "{ [0] -> [j] }");
8929 isl_map_free(map);
8930 if (equal < 0)
8931 return -1;
8933 str = "{ [i] -> [j] }";
8934 map = isl_map_read_from_str(ctx, str);
8935 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
8936 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
8937 isl_map_free(map);
8938 if (equal < 0)
8939 return -1;
8941 str = "{ [i] -> [j] }";
8942 map = isl_map_read_from_str(ctx, str);
8943 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
8944 equal = map_check_equal(map, "{ [i] -> [j] : false }");
8945 isl_map_free(map);
8946 if (equal < 0)
8947 return -1;
8949 return 0;
8952 int test_eliminate(isl_ctx *ctx)
8954 const char *str;
8955 isl_map *map;
8956 int equal;
8958 str = "{ [i] -> [j] : i = 2j }";
8959 map = isl_map_read_from_str(ctx, str);
8960 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
8961 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
8962 isl_map_free(map);
8963 if (equal < 0)
8964 return -1;
8966 return 0;
8969 /* Check basic functionality of isl_map_deltas_map.
8971 static int test_deltas_map(isl_ctx *ctx)
8973 const char *str;
8974 isl_map *map;
8975 int equal;
8977 str = "{ A[i] -> A[i + 1] }";
8978 map = isl_map_read_from_str(ctx, str);
8979 map = isl_map_deltas_map(map);
8980 equal = map_check_equal(map, "{ [A[i] -> A[i + 1]] -> A[1] }");
8981 isl_map_free(map);
8982 if (equal < 0)
8983 return -1;
8985 return 0;
8988 /* Check that isl_set_dim_residue_class detects that the values of j
8989 * in the set below are all odd and that it does not detect any spurious
8990 * strides.
8992 static int test_residue_class(isl_ctx *ctx)
8994 const char *str;
8995 isl_set *set;
8996 isl_int m, r;
8997 isl_stat res;
8999 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
9000 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
9001 set = isl_set_read_from_str(ctx, str);
9002 isl_int_init(m);
9003 isl_int_init(r);
9004 res = isl_set_dim_residue_class(set, 1, &m, &r);
9005 if (res >= 0 &&
9006 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
9007 isl_die(ctx, isl_error_unknown, "incorrect residue class",
9008 res = isl_stat_error);
9009 isl_int_clear(r);
9010 isl_int_clear(m);
9011 isl_set_free(set);
9013 return res;
9016 static int test_align_parameters_1(isl_ctx *ctx)
9018 const char *str;
9019 isl_space *space;
9020 isl_multi_aff *ma1, *ma2;
9021 int equal;
9023 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
9024 ma1 = isl_multi_aff_read_from_str(ctx, str);
9026 space = isl_space_params_alloc(ctx, 1);
9027 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
9028 ma1 = isl_multi_aff_align_params(ma1, space);
9030 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
9031 ma2 = isl_multi_aff_read_from_str(ctx, str);
9033 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
9035 isl_multi_aff_free(ma1);
9036 isl_multi_aff_free(ma2);
9038 if (equal < 0)
9039 return -1;
9040 if (!equal)
9041 isl_die(ctx, isl_error_unknown,
9042 "result not as expected", return -1);
9044 return 0;
9047 /* Check the isl_multi_*_from_*_list operation in case inputs
9048 * have unaligned parameters.
9049 * In particular, older versions of isl would simply fail
9050 * (without printing any error message).
9052 static isl_stat test_align_parameters_2(isl_ctx *ctx)
9054 isl_space *space;
9055 isl_map *map;
9056 isl_aff *aff;
9057 isl_multi_aff *ma;
9059 map = isl_map_read_from_str(ctx, "{ A[] -> M[x] }");
9060 space = isl_map_get_space(map);
9061 isl_map_free(map);
9063 aff = isl_aff_read_from_str(ctx, "[N] -> { A[] -> [N] }");
9064 ma = isl_multi_aff_from_aff_list(space, isl_aff_list_from_aff(aff));
9065 isl_multi_aff_free(ma);
9067 if (!ma)
9068 return isl_stat_error;
9069 return isl_stat_ok;
9072 /* Perform basic parameter alignment tests.
9074 static int test_align_parameters(isl_ctx *ctx)
9076 if (test_align_parameters_1(ctx) < 0)
9077 return -1;
9078 if (test_align_parameters_2(ctx) < 0)
9079 return -1;
9081 return 0;
9084 /* Check that isl_*_drop_unused_params actually drops the unused parameters
9085 * by comparing the result using isl_*_plain_is_equal.
9086 * Note that this assumes that isl_*_plain_is_equal does not consider
9087 * objects that only differ by unused parameters to be equal.
9089 int test_drop_unused_parameters(isl_ctx *ctx)
9091 const char *str_with, *str_without;
9092 isl_basic_set *bset1, *bset2;
9093 isl_set *set1, *set2;
9094 isl_pw_aff *pwa1, *pwa2;
9095 int equal;
9097 str_with = "[n, m, o] -> { [m] }";
9098 str_without = "[m] -> { [m] }";
9100 bset1 = isl_basic_set_read_from_str(ctx, str_with);
9101 bset2 = isl_basic_set_read_from_str(ctx, str_without);
9102 bset1 = isl_basic_set_drop_unused_params(bset1);
9103 equal = isl_basic_set_plain_is_equal(bset1, bset2);
9104 isl_basic_set_free(bset1);
9105 isl_basic_set_free(bset2);
9107 if (equal < 0)
9108 return -1;
9109 if (!equal)
9110 isl_die(ctx, isl_error_unknown,
9111 "result not as expected", return -1);
9113 set1 = isl_set_read_from_str(ctx, str_with);
9114 set2 = isl_set_read_from_str(ctx, str_without);
9115 set1 = isl_set_drop_unused_params(set1);
9116 equal = isl_set_plain_is_equal(set1, set2);
9117 isl_set_free(set1);
9118 isl_set_free(set2);
9120 if (equal < 0)
9121 return -1;
9122 if (!equal)
9123 isl_die(ctx, isl_error_unknown,
9124 "result not as expected", return -1);
9126 pwa1 = isl_pw_aff_read_from_str(ctx, str_with);
9127 pwa2 = isl_pw_aff_read_from_str(ctx, str_without);
9128 pwa1 = isl_pw_aff_drop_unused_params(pwa1);
9129 equal = isl_pw_aff_plain_is_equal(pwa1, pwa2);
9130 isl_pw_aff_free(pwa1);
9131 isl_pw_aff_free(pwa2);
9133 if (equal < 0)
9134 return -1;
9135 if (!equal)
9136 isl_die(ctx, isl_error_unknown,
9137 "result not as expected", return -1);
9139 return 0;
9142 static int test_list(isl_ctx *ctx)
9144 isl_id *a, *b, *c, *d, *id;
9145 isl_id_list *list;
9146 isl_size n;
9147 int ok;
9149 a = isl_id_alloc(ctx, "a", NULL);
9150 b = isl_id_alloc(ctx, "b", NULL);
9151 c = isl_id_alloc(ctx, "c", NULL);
9152 d = isl_id_alloc(ctx, "d", NULL);
9154 list = isl_id_list_alloc(ctx, 4);
9155 list = isl_id_list_add(list, b);
9156 list = isl_id_list_insert(list, 0, a);
9157 list = isl_id_list_add(list, c);
9158 list = isl_id_list_add(list, d);
9159 list = isl_id_list_drop(list, 1, 1);
9161 n = isl_id_list_n_id(list);
9162 if (n < 0)
9163 return -1;
9164 if (n != 3) {
9165 isl_id_list_free(list);
9166 isl_die(ctx, isl_error_unknown,
9167 "unexpected number of elements in list", return -1);
9170 id = isl_id_list_get_id(list, 0);
9171 ok = id == a;
9172 isl_id_free(id);
9173 id = isl_id_list_get_id(list, 1);
9174 ok = ok && id == c;
9175 isl_id_free(id);
9176 id = isl_id_list_get_id(list, 2);
9177 ok = ok && id == d;
9178 isl_id_free(id);
9180 isl_id_list_free(list);
9182 if (!ok)
9183 isl_die(ctx, isl_error_unknown,
9184 "unexpected elements in list", return -1);
9186 return 0;
9189 /* Check the conversion from an isl_multi_aff to an isl_basic_set.
9191 static isl_stat test_ma_conversion(isl_ctx *ctx)
9193 const char *str;
9194 isl_bool equal;
9195 isl_multi_aff *ma;
9196 isl_basic_set *bset1, *bset2;
9198 str = "[N] -> { A[0, N + 1] }";
9199 ma = isl_multi_aff_read_from_str(ctx, str);
9200 bset1 = isl_basic_set_read_from_str(ctx, str);
9201 bset2 = isl_basic_set_from_multi_aff(ma);
9202 equal = isl_basic_set_is_equal(bset1, bset2);
9203 isl_basic_set_free(bset1);
9204 isl_basic_set_free(bset2);
9205 if (equal < 0)
9206 return isl_stat_error;
9207 if (!equal)
9208 isl_die(ctx, isl_error_unknown, "bad conversion",
9209 return isl_stat_error);
9210 return isl_stat_ok;
9213 const char *set_conversion_tests[] = {
9214 "[N] -> { [i] : N - 1 <= 2 i <= N }",
9215 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
9216 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
9217 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
9218 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
9219 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
9220 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
9221 "-3 + c <= d <= 28 + c) }",
9224 /* Check that converting from isl_set to isl_pw_multi_aff and back
9225 * to isl_set produces the original isl_set.
9227 static int test_set_conversion(isl_ctx *ctx)
9229 int i;
9230 const char *str;
9231 isl_set *set1, *set2;
9232 isl_pw_multi_aff *pma;
9233 int equal;
9235 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
9236 str = set_conversion_tests[i];
9237 set1 = isl_set_read_from_str(ctx, str);
9238 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
9239 set2 = isl_set_from_pw_multi_aff(pma);
9240 equal = isl_set_is_equal(set1, set2);
9241 isl_set_free(set1);
9242 isl_set_free(set2);
9244 if (equal < 0)
9245 return -1;
9246 if (!equal)
9247 isl_die(ctx, isl_error_unknown, "bad conversion",
9248 return -1);
9251 return 0;
9254 const char *conversion_tests[] = {
9255 "{ [a, b, c, d] -> s0[a, b, e, f] : "
9256 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
9257 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
9258 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
9259 "9e <= -2 - 2a) }",
9260 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
9261 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
9262 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
9265 /* Check that converting from isl_map to isl_pw_multi_aff and back
9266 * to isl_map produces the original isl_map.
9268 static int test_map_conversion(isl_ctx *ctx)
9270 int i;
9271 isl_map *map1, *map2;
9272 isl_pw_multi_aff *pma;
9273 int equal;
9275 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
9276 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
9277 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
9278 map2 = isl_map_from_pw_multi_aff(pma);
9279 equal = isl_map_is_equal(map1, map2);
9280 isl_map_free(map1);
9281 isl_map_free(map2);
9283 if (equal < 0)
9284 return -1;
9285 if (!equal)
9286 isl_die(ctx, isl_error_unknown, "bad conversion",
9287 return -1);
9290 return 0;
9293 /* Descriptions of isl_pw_multi_aff objects for testing conversion
9294 * to isl_multi_pw_aff and back.
9296 const char *mpa_conversion_tests[] = {
9297 "{ [x] -> A[x] }",
9298 "{ [x] -> A[x] : x >= 0 }",
9299 "{ [x] -> A[x] : x >= 0; [x] -> A[-x] : x < 0 }",
9300 "{ [x] -> A[x, x + 1] }",
9301 "{ [x] -> A[] }",
9302 "{ [x] -> A[] : x >= 0 }",
9305 /* Check that conversion from isl_pw_multi_aff to isl_multi_pw_aff and
9306 * back to isl_pw_multi_aff preserves the original meaning.
9308 static int test_mpa_conversion(isl_ctx *ctx)
9310 int i;
9311 isl_pw_multi_aff *pma1, *pma2;
9312 isl_multi_pw_aff *mpa;
9313 int equal;
9315 for (i = 0; i < ARRAY_SIZE(mpa_conversion_tests); ++i) {
9316 const char *str;
9317 str = mpa_conversion_tests[i];
9318 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
9319 pma2 = isl_pw_multi_aff_copy(pma1);
9320 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma1);
9321 pma1 = isl_pw_multi_aff_from_multi_pw_aff(mpa);
9322 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
9323 isl_pw_multi_aff_free(pma1);
9324 isl_pw_multi_aff_free(pma2);
9326 if (equal < 0)
9327 return -1;
9328 if (!equal)
9329 isl_die(ctx, isl_error_unknown, "bad conversion",
9330 return -1);
9333 return 0;
9336 /* Descriptions of union maps that should be convertible
9337 * to an isl_multi_union_pw_aff.
9339 const char *umap_mupa_conversion_tests[] = {
9340 "{ [a, b, c, d] -> s0[a, b, e, f] : "
9341 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
9342 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
9343 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
9344 "9e <= -2 - 2a) }",
9345 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
9346 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
9347 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
9348 "{ A[] -> B[0]; C[] -> B[1] }",
9349 "{ A[] -> B[]; C[] -> B[] }",
9352 /* Check that converting from isl_union_map to isl_multi_union_pw_aff and back
9353 * to isl_union_map produces the original isl_union_map.
9355 static int test_union_map_mupa_conversion(isl_ctx *ctx)
9357 int i;
9358 isl_union_map *umap1, *umap2;
9359 isl_multi_union_pw_aff *mupa;
9360 int equal;
9362 for (i = 0; i < ARRAY_SIZE(umap_mupa_conversion_tests); ++i) {
9363 const char *str;
9364 str = umap_mupa_conversion_tests[i];
9365 umap1 = isl_union_map_read_from_str(ctx, str);
9366 umap2 = isl_union_map_copy(umap1);
9367 mupa = isl_multi_union_pw_aff_from_union_map(umap2);
9368 umap2 = isl_union_map_from_multi_union_pw_aff(mupa);
9369 equal = isl_union_map_is_equal(umap1, umap2);
9370 isl_union_map_free(umap1);
9371 isl_union_map_free(umap2);
9373 if (equal < 0)
9374 return -1;
9375 if (!equal)
9376 isl_die(ctx, isl_error_unknown, "bad conversion",
9377 return -1);
9380 return 0;
9383 static int test_conversion(isl_ctx *ctx)
9385 if (test_ma_conversion(ctx) < 0)
9386 return -1;
9387 if (test_set_conversion(ctx) < 0)
9388 return -1;
9389 if (test_map_conversion(ctx) < 0)
9390 return -1;
9391 if (test_mpa_conversion(ctx) < 0)
9392 return -1;
9393 if (test_union_map_mupa_conversion(ctx) < 0)
9394 return -1;
9395 return 0;
9398 /* Check that isl_basic_map_curry does not modify input.
9400 static int test_curry(isl_ctx *ctx)
9402 const char *str;
9403 isl_basic_map *bmap1, *bmap2;
9404 int equal;
9406 str = "{ [A[] -> B[]] -> C[] }";
9407 bmap1 = isl_basic_map_read_from_str(ctx, str);
9408 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
9409 equal = isl_basic_map_is_equal(bmap1, bmap2);
9410 isl_basic_map_free(bmap1);
9411 isl_basic_map_free(bmap2);
9413 if (equal < 0)
9414 return -1;
9415 if (equal)
9416 isl_die(ctx, isl_error_unknown,
9417 "curried map should not be equal to original",
9418 return -1);
9420 return 0;
9423 struct {
9424 const char *ma1;
9425 const char *ma;
9426 const char *res;
9427 } pullback_tests[] = {
9428 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
9429 "{ A[a,b] -> C[b + 2a] }" },
9430 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
9431 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
9432 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
9433 "{ A[a] -> C[(a)/6] }" },
9434 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
9435 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
9436 "{ A[a] -> C[(2a)/3] }" },
9437 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
9438 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
9439 "{ A[i,j] -> C[i + j, i + j] }"},
9440 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
9441 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
9442 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
9443 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
9444 "{ [i, j] -> [floor((i)/3), j] }",
9445 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
9448 static int test_pullback(isl_ctx *ctx)
9450 int i;
9451 isl_multi_aff *ma1, *ma2;
9452 isl_multi_aff *ma;
9453 int equal;
9455 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
9456 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
9457 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
9458 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
9459 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
9460 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
9461 isl_multi_aff_free(ma1);
9462 isl_multi_aff_free(ma2);
9463 if (equal < 0)
9464 return -1;
9465 if (!equal)
9466 isl_die(ctx, isl_error_unknown, "bad pullback",
9467 return -1);
9470 return 0;
9473 /* Check that negation is printed correctly and that equal expressions
9474 * are correctly identified.
9476 static int test_ast(isl_ctx *ctx)
9478 isl_ast_expr *expr, *expr1, *expr2, *expr3;
9479 char *str;
9480 int ok, equal;
9482 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
9483 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
9484 expr = isl_ast_expr_add(expr1, expr2);
9485 expr2 = isl_ast_expr_copy(expr);
9486 expr = isl_ast_expr_neg(expr);
9487 expr2 = isl_ast_expr_neg(expr2);
9488 equal = isl_ast_expr_is_equal(expr, expr2);
9489 str = isl_ast_expr_to_C_str(expr);
9490 ok = str ? !strcmp(str, "-(A + B)") : -1;
9491 free(str);
9492 isl_ast_expr_free(expr);
9493 isl_ast_expr_free(expr2);
9495 if (ok < 0 || equal < 0)
9496 return -1;
9497 if (!equal)
9498 isl_die(ctx, isl_error_unknown,
9499 "equal expressions not considered equal", return -1);
9500 if (!ok)
9501 isl_die(ctx, isl_error_unknown,
9502 "isl_ast_expr printed incorrectly", return -1);
9504 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
9505 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
9506 expr = isl_ast_expr_add(expr1, expr2);
9507 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
9508 expr = isl_ast_expr_sub(expr3, expr);
9509 str = isl_ast_expr_to_C_str(expr);
9510 ok = str ? !strcmp(str, "C - (A + B)") : -1;
9511 free(str);
9512 isl_ast_expr_free(expr);
9514 if (ok < 0)
9515 return -1;
9516 if (!ok)
9517 isl_die(ctx, isl_error_unknown,
9518 "isl_ast_expr printed incorrectly", return -1);
9520 return 0;
9523 /* Check that isl_ast_build_expr_from_set returns a valid expression
9524 * for an empty set. Note that isl_ast_build_expr_from_set getting
9525 * called on an empty set probably indicates a bug in the caller.
9527 static int test_ast_build(isl_ctx *ctx)
9529 isl_set *set;
9530 isl_ast_build *build;
9531 isl_ast_expr *expr;
9533 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9534 build = isl_ast_build_from_context(set);
9536 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
9537 expr = isl_ast_build_expr_from_set(build, set);
9539 isl_ast_expr_free(expr);
9540 isl_ast_build_free(build);
9542 if (!expr)
9543 return -1;
9545 return 0;
9548 /* Internal data structure for before_for and after_for callbacks.
9550 * depth is the current depth
9551 * before is the number of times before_for has been called
9552 * after is the number of times after_for has been called
9554 struct isl_test_codegen_data {
9555 int depth;
9556 int before;
9557 int after;
9560 /* This function is called before each for loop in the AST generated
9561 * from test_ast_gen1.
9563 * Increment the number of calls and the depth.
9564 * Check that the space returned by isl_ast_build_get_schedule_space
9565 * matches the target space of the schedule returned by
9566 * isl_ast_build_get_schedule.
9567 * Return an isl_id that is checked by the corresponding call
9568 * to after_for.
9570 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
9571 void *user)
9573 struct isl_test_codegen_data *data = user;
9574 isl_ctx *ctx;
9575 isl_space *space;
9576 isl_union_map *schedule;
9577 isl_union_set *uset;
9578 isl_set *set;
9579 isl_bool empty;
9580 isl_size n;
9581 char name[] = "d0";
9583 ctx = isl_ast_build_get_ctx(build);
9585 if (data->before >= 3)
9586 isl_die(ctx, isl_error_unknown,
9587 "unexpected number of for nodes", return NULL);
9588 if (data->depth < 0 || data->depth >= 2)
9589 isl_die(ctx, isl_error_unknown,
9590 "unexpected depth", return NULL);
9592 snprintf(name, sizeof(name), "d%d", data->depth);
9593 data->before++;
9594 data->depth++;
9596 schedule = isl_ast_build_get_schedule(build);
9597 uset = isl_union_map_range(schedule);
9598 n = isl_union_set_n_set(uset);
9599 if (n != 1) {
9600 isl_union_set_free(uset);
9601 if (n < 0)
9602 return NULL;
9603 isl_die(ctx, isl_error_unknown,
9604 "expecting single range space", return NULL);
9607 space = isl_ast_build_get_schedule_space(build);
9608 set = isl_union_set_extract_set(uset, space);
9609 isl_union_set_free(uset);
9610 empty = isl_set_is_empty(set);
9611 isl_set_free(set);
9613 if (empty < 0)
9614 return NULL;
9615 if (empty)
9616 isl_die(ctx, isl_error_unknown,
9617 "spaces don't match", return NULL);
9619 return isl_id_alloc(ctx, name, NULL);
9622 /* This function is called after each for loop in the AST generated
9623 * from test_ast_gen1.
9625 * Increment the number of calls and decrement the depth.
9626 * Check that the annotation attached to the node matches
9627 * the isl_id returned by the corresponding call to before_for.
9629 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
9630 __isl_keep isl_ast_build *build, void *user)
9632 struct isl_test_codegen_data *data = user;
9633 isl_id *id;
9634 const char *name;
9635 int valid;
9637 data->after++;
9638 data->depth--;
9640 if (data->after > data->before)
9641 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
9642 "mismatch in number of for nodes",
9643 return isl_ast_node_free(node));
9645 id = isl_ast_node_get_annotation(node);
9646 if (!id)
9647 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
9648 "missing annotation", return isl_ast_node_free(node));
9650 name = isl_id_get_name(id);
9651 valid = name && atoi(name + 1) == data->depth;
9652 isl_id_free(id);
9654 if (!valid)
9655 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
9656 "wrong annotation", return isl_ast_node_free(node));
9658 return node;
9661 /* This function is called after node in the AST generated
9662 * from test_ast_gen1.
9664 * Increment the count in "user" if this is a for node and
9665 * return true to indicate that descendant should also be visited.
9667 static isl_bool count_for(__isl_keep isl_ast_node *node, void *user)
9669 int *count = user;
9671 if (isl_ast_node_get_type(node) == isl_ast_node_for)
9672 ++*count;
9674 return isl_bool_true;
9677 /* If "node" is a block node, then replace it by its first child.
9679 static __isl_give isl_ast_node *select_first(__isl_take isl_ast_node *node,
9680 void *user)
9682 isl_ast_node_list *children;
9683 isl_ast_node *child;
9685 if (isl_ast_node_get_type(node) != isl_ast_node_block)
9686 return node;
9688 children = isl_ast_node_block_get_children(node);
9689 child = isl_ast_node_list_get_at(children, 0);
9690 isl_ast_node_list_free(children);
9691 isl_ast_node_free(node);
9693 return child;
9696 /* Check that the before_each_for and after_each_for callbacks
9697 * are called for each for loop in the generated code,
9698 * that they are called in the right order and that the isl_id
9699 * returned from the before_each_for callback is attached to
9700 * the isl_ast_node passed to the corresponding after_each_for call.
9702 * Additionally, check the basic functionality of
9703 * isl_ast_node_foreach_descendant_top_down by counting the number
9704 * of for loops in the resulting AST,
9705 * as well as that of isl_ast_node_map_descendant_bottom_up
9706 * by replacing the block node by its first child and
9707 * counting the number of for loops again.
9709 static isl_stat test_ast_gen1(isl_ctx *ctx)
9711 int count = 0;
9712 int modified_count = 0;
9713 const char *str;
9714 isl_set *set;
9715 isl_union_map *schedule;
9716 isl_ast_build *build;
9717 isl_ast_node *tree;
9718 struct isl_test_codegen_data data;
9720 str = "[N] -> { : N >= 10 }";
9721 set = isl_set_read_from_str(ctx, str);
9722 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
9723 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
9724 schedule = isl_union_map_read_from_str(ctx, str);
9726 data.before = 0;
9727 data.after = 0;
9728 data.depth = 0;
9729 build = isl_ast_build_from_context(set);
9730 build = isl_ast_build_set_before_each_for(build,
9731 &before_for, &data);
9732 build = isl_ast_build_set_after_each_for(build,
9733 &after_for, &data);
9734 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9735 isl_ast_build_free(build);
9737 if (isl_ast_node_foreach_descendant_top_down(tree,
9738 &count_for, &count) < 0)
9739 tree = isl_ast_node_free(tree);
9741 tree = isl_ast_node_map_descendant_bottom_up(tree, &select_first, NULL);
9743 if (isl_ast_node_foreach_descendant_top_down(tree, &count_for,
9744 &modified_count) < 0)
9745 tree = isl_ast_node_free(tree);
9747 if (!tree)
9748 return isl_stat_error;
9750 isl_ast_node_free(tree);
9752 if (data.before != 3 || data.after != 3 || count != 3)
9753 isl_die(ctx, isl_error_unknown,
9754 "unexpected number of for nodes",
9755 return isl_stat_error);
9757 if (modified_count != 2)
9758 isl_die(ctx, isl_error_unknown,
9759 "unexpected number of for nodes after changes",
9760 return isl_stat_error);
9762 return isl_stat_ok;
9765 /* Check that the AST generator handles domains that are integrally disjoint
9766 * but not rationally disjoint.
9768 static int test_ast_gen2(isl_ctx *ctx)
9770 const char *str;
9771 isl_set *set;
9772 isl_union_map *schedule;
9773 isl_union_map *options;
9774 isl_ast_build *build;
9775 isl_ast_node *tree;
9777 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
9778 schedule = isl_union_map_read_from_str(ctx, str);
9779 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9780 build = isl_ast_build_from_context(set);
9782 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
9783 options = isl_union_map_read_from_str(ctx, str);
9784 build = isl_ast_build_set_options(build, options);
9785 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9786 isl_ast_build_free(build);
9787 if (!tree)
9788 return -1;
9789 isl_ast_node_free(tree);
9791 return 0;
9794 /* Increment *user on each call.
9796 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
9797 __isl_keep isl_ast_build *build, void *user)
9799 int *n = user;
9801 (*n)++;
9803 return node;
9806 /* Test that unrolling tries to minimize the number of instances.
9807 * In particular, for the schedule given below, make sure it generates
9808 * 3 nodes (rather than 101).
9810 static int test_ast_gen3(isl_ctx *ctx)
9812 const char *str;
9813 isl_set *set;
9814 isl_union_map *schedule;
9815 isl_union_map *options;
9816 isl_ast_build *build;
9817 isl_ast_node *tree;
9818 int n_domain = 0;
9820 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
9821 schedule = isl_union_map_read_from_str(ctx, str);
9822 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9824 str = "{ [i] -> unroll[0] }";
9825 options = isl_union_map_read_from_str(ctx, str);
9827 build = isl_ast_build_from_context(set);
9828 build = isl_ast_build_set_options(build, options);
9829 build = isl_ast_build_set_at_each_domain(build,
9830 &count_domains, &n_domain);
9831 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9832 isl_ast_build_free(build);
9833 if (!tree)
9834 return -1;
9836 isl_ast_node_free(tree);
9838 if (n_domain != 3)
9839 isl_die(ctx, isl_error_unknown,
9840 "unexpected number of for nodes", return -1);
9842 return 0;
9845 /* Check that if the ast_build_exploit_nested_bounds options is set,
9846 * we do not get an outer if node in the generated AST,
9847 * while we do get such an outer if node if the options is not set.
9849 static int test_ast_gen4(isl_ctx *ctx)
9851 const char *str;
9852 isl_set *set;
9853 isl_union_map *schedule;
9854 isl_ast_build *build;
9855 isl_ast_node *tree;
9856 enum isl_ast_node_type type;
9857 int enb;
9859 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
9860 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
9862 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
9864 schedule = isl_union_map_read_from_str(ctx, str);
9865 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9866 build = isl_ast_build_from_context(set);
9867 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9868 isl_ast_build_free(build);
9869 if (!tree)
9870 return -1;
9872 type = isl_ast_node_get_type(tree);
9873 isl_ast_node_free(tree);
9875 if (type == isl_ast_node_if)
9876 isl_die(ctx, isl_error_unknown,
9877 "not expecting if node", return -1);
9879 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
9881 schedule = isl_union_map_read_from_str(ctx, str);
9882 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9883 build = isl_ast_build_from_context(set);
9884 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9885 isl_ast_build_free(build);
9886 if (!tree)
9887 return -1;
9889 type = isl_ast_node_get_type(tree);
9890 isl_ast_node_free(tree);
9892 if (type != isl_ast_node_if)
9893 isl_die(ctx, isl_error_unknown,
9894 "expecting if node", return -1);
9896 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
9898 return 0;
9901 /* This function is called for each leaf in the AST generated
9902 * from test_ast_gen5.
9904 * We finalize the AST generation by extending the outer schedule
9905 * with a zero-dimensional schedule. If this results in any for loops,
9906 * then this means that we did not pass along enough information
9907 * about the outer schedule to the inner AST generation.
9909 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
9910 void *user)
9912 isl_union_map *schedule, *extra;
9913 isl_ast_node *tree;
9915 schedule = isl_ast_build_get_schedule(build);
9916 extra = isl_union_map_copy(schedule);
9917 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
9918 schedule = isl_union_map_range_product(schedule, extra);
9919 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9920 isl_ast_build_free(build);
9922 if (!tree)
9923 return NULL;
9925 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
9926 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
9927 "code should not contain any for loop",
9928 return isl_ast_node_free(tree));
9930 return tree;
9933 /* Check that we do not lose any information when going back and
9934 * forth between internal and external schedule.
9936 * In particular, we create an AST where we unroll the only
9937 * non-constant dimension in the schedule. We therefore do
9938 * not expect any for loops in the AST. However, older versions
9939 * of isl would not pass along enough information about the outer
9940 * schedule when performing an inner code generation from a create_leaf
9941 * callback, resulting in the inner code generation producing a for loop.
9943 static int test_ast_gen5(isl_ctx *ctx)
9945 const char *str;
9946 isl_set *set;
9947 isl_union_map *schedule, *options;
9948 isl_ast_build *build;
9949 isl_ast_node *tree;
9951 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
9952 schedule = isl_union_map_read_from_str(ctx, str);
9954 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
9955 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
9956 options = isl_union_map_read_from_str(ctx, str);
9958 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
9959 build = isl_ast_build_from_context(set);
9960 build = isl_ast_build_set_options(build, options);
9961 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
9962 tree = isl_ast_build_node_from_schedule_map(build, schedule);
9963 isl_ast_build_free(build);
9964 isl_ast_node_free(tree);
9965 if (!tree)
9966 return -1;
9968 return 0;
9971 /* Check that the expression
9973 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
9975 * is not combined into
9977 * min(n/2, 0)
9979 * as this would result in n/2 being evaluated in parts of
9980 * the definition domain where n is not a multiple of 2.
9982 static int test_ast_expr(isl_ctx *ctx)
9984 const char *str;
9985 isl_pw_aff *pa;
9986 isl_ast_build *build;
9987 isl_ast_expr *expr;
9988 int min_max;
9989 int is_min;
9991 min_max = isl_options_get_ast_build_detect_min_max(ctx);
9992 isl_options_set_ast_build_detect_min_max(ctx, 1);
9994 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
9995 pa = isl_pw_aff_read_from_str(ctx, str);
9996 build = isl_ast_build_alloc(ctx);
9997 expr = isl_ast_build_expr_from_pw_aff(build, pa);
9998 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
9999 isl_ast_expr_get_op_type(expr) == isl_ast_expr_op_min;
10000 isl_ast_build_free(build);
10001 isl_ast_expr_free(expr);
10003 isl_options_set_ast_build_detect_min_max(ctx, min_max);
10005 if (!expr)
10006 return -1;
10007 if (is_min)
10008 isl_die(ctx, isl_error_unknown,
10009 "expressions should not be combined", return -1);
10011 return 0;
10014 static int test_ast_gen(isl_ctx *ctx)
10016 if (test_ast_gen1(ctx) < 0)
10017 return -1;
10018 if (test_ast_gen2(ctx) < 0)
10019 return -1;
10020 if (test_ast_gen3(ctx) < 0)
10021 return -1;
10022 if (test_ast_gen4(ctx) < 0)
10023 return -1;
10024 if (test_ast_gen5(ctx) < 0)
10025 return -1;
10026 if (test_ast_expr(ctx) < 0)
10027 return -1;
10028 return 0;
10031 /* Check if dropping output dimensions from an isl_pw_multi_aff
10032 * works properly.
10034 static int test_pw_multi_aff(isl_ctx *ctx)
10036 const char *str;
10037 isl_pw_multi_aff *pma1, *pma2;
10038 int equal;
10040 str = "{ [i,j] -> [i+j, 4i-j] }";
10041 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
10042 str = "{ [i,j] -> [4i-j] }";
10043 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
10045 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
10047 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
10049 isl_pw_multi_aff_free(pma1);
10050 isl_pw_multi_aff_free(pma2);
10051 if (equal < 0)
10052 return -1;
10053 if (!equal)
10054 isl_die(ctx, isl_error_unknown,
10055 "expressions not equal", return -1);
10057 return 0;
10060 /* Check that we can properly parse multi piecewise affine expressions
10061 * where the piecewise affine expressions have different domains.
10063 static int test_multi_pw_aff_1(isl_ctx *ctx)
10065 const char *str;
10066 isl_set *dom, *dom2;
10067 isl_multi_pw_aff *mpa1, *mpa2;
10068 isl_pw_aff *pa;
10069 int equal;
10070 int equal_domain;
10072 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
10073 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
10074 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
10075 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
10076 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
10077 str = "{ [i] -> [(i : i > 0), 2i] }";
10078 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
10080 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
10082 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
10083 dom = isl_pw_aff_domain(pa);
10084 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
10085 dom2 = isl_pw_aff_domain(pa);
10086 equal_domain = isl_set_is_equal(dom, dom2);
10088 isl_set_free(dom);
10089 isl_set_free(dom2);
10090 isl_multi_pw_aff_free(mpa1);
10091 isl_multi_pw_aff_free(mpa2);
10093 if (equal < 0)
10094 return -1;
10095 if (!equal)
10096 isl_die(ctx, isl_error_unknown,
10097 "expressions not equal", return -1);
10099 if (equal_domain < 0)
10100 return -1;
10101 if (equal_domain)
10102 isl_die(ctx, isl_error_unknown,
10103 "domains unexpectedly equal", return -1);
10105 return 0;
10108 /* Check that the dimensions in the explicit domain
10109 * of a multi piecewise affine expression are properly
10110 * taken into account.
10112 static int test_multi_pw_aff_2(isl_ctx *ctx)
10114 const char *str;
10115 isl_bool involves1, involves2, involves3, equal;
10116 isl_multi_pw_aff *mpa, *mpa1, *mpa2;
10118 str = "{ A[x,y] -> B[] : x >= y }";
10119 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
10120 involves1 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 2);
10121 mpa1 = isl_multi_pw_aff_copy(mpa);
10123 mpa = isl_multi_pw_aff_insert_dims(mpa, isl_dim_in, 0, 1);
10124 involves2 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 1);
10125 involves3 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 1, 2);
10126 str = "{ [a,x,y] -> B[] : x >= y }";
10127 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
10128 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
10129 isl_multi_pw_aff_free(mpa2);
10131 mpa = isl_multi_pw_aff_drop_dims(mpa, isl_dim_in, 0, 1);
10132 mpa = isl_multi_pw_aff_set_tuple_name(mpa, isl_dim_in, "A");
10133 if (equal >= 0 && equal)
10134 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa1);
10135 isl_multi_pw_aff_free(mpa1);
10136 isl_multi_pw_aff_free(mpa);
10138 if (involves1 < 0 || involves2 < 0 || involves3 < 0 || equal < 0)
10139 return -1;
10140 if (!equal)
10141 isl_die(ctx, isl_error_unknown,
10142 "incorrect result of dimension insertion/removal",
10143 return isl_stat_error);
10144 if (!involves1 || involves2 || !involves3)
10145 isl_die(ctx, isl_error_unknown,
10146 "incorrect characterization of involved dimensions",
10147 return isl_stat_error);
10149 return 0;
10152 /* Check that isl_multi_union_pw_aff_multi_val_on_domain
10153 * sets the explicit domain of a zero-dimensional result,
10154 * such that it can be converted to an isl_union_map.
10156 static isl_stat test_multi_pw_aff_3(isl_ctx *ctx)
10158 isl_space *space;
10159 isl_union_set *dom;
10160 isl_multi_val *mv;
10161 isl_multi_union_pw_aff *mupa;
10162 isl_union_map *umap;
10164 dom = isl_union_set_read_from_str(ctx, "{ A[]; B[] }");
10165 space = isl_union_set_get_space(dom);
10166 mv = isl_multi_val_zero(isl_space_set_from_params(space));
10167 mupa = isl_multi_union_pw_aff_multi_val_on_domain(dom, mv);
10168 umap = isl_union_map_from_multi_union_pw_aff(mupa);
10169 isl_union_map_free(umap);
10170 if (!umap)
10171 return isl_stat_error;
10173 return isl_stat_ok;
10176 /* String descriptions of boxes that
10177 * are used for reconstructing box maps from their lower and upper bounds.
10179 static const char *multi_pw_aff_box_tests[] = {
10180 "{ A[x, y] -> [] : x + y >= 0 }",
10181 "[N] -> { A[x, y] -> [x] : x + y <= N }",
10182 "[N] -> { A[x, y] -> [x : y] : x + y <= N }",
10185 /* Check that map representations of boxes can be reconstructed
10186 * from their lower and upper bounds.
10188 static isl_stat test_multi_pw_aff_box(isl_ctx *ctx)
10190 int i;
10192 for (i = 0; i < ARRAY_SIZE(multi_pw_aff_box_tests); ++i) {
10193 const char *str;
10194 isl_bool equal;
10195 isl_map *map, *box;
10196 isl_multi_pw_aff *min, *max;
10198 str = multi_pw_aff_box_tests[i];
10199 map = isl_map_read_from_str(ctx, str);
10200 min = isl_map_min_multi_pw_aff(isl_map_copy(map));
10201 max = isl_map_max_multi_pw_aff(isl_map_copy(map));
10202 box = isl_map_universe(isl_map_get_space(map));
10203 box = isl_map_lower_bound_multi_pw_aff(box, min);
10204 box = isl_map_upper_bound_multi_pw_aff(box, max);
10205 equal = isl_map_is_equal(map, box);
10206 isl_map_free(map);
10207 isl_map_free(box);
10208 if (equal < 0)
10209 return isl_stat_error;
10210 if (!equal)
10211 isl_die(ctx, isl_error_unknown,
10212 "unexpected result", return isl_stat_error);
10215 return isl_stat_ok;
10218 /* Perform some tests on multi piecewise affine expressions.
10220 static int test_multi_pw_aff(isl_ctx *ctx)
10222 if (test_multi_pw_aff_1(ctx) < 0)
10223 return -1;
10224 if (test_multi_pw_aff_2(ctx) < 0)
10225 return -1;
10226 if (test_multi_pw_aff_3(ctx) < 0)
10227 return -1;
10228 if (test_multi_pw_aff_box(ctx) < 0)
10229 return -1;
10230 return 0;
10233 /* This is a regression test for a bug where isl_basic_map_simplify
10234 * would end up in an infinite loop. In particular, we construct
10235 * an empty basic set that is not obviously empty.
10236 * isl_basic_set_is_empty marks the basic set as empty.
10237 * After projecting out i3, the variable can be dropped completely,
10238 * but isl_basic_map_simplify refrains from doing so if the basic set
10239 * is empty and would end up in an infinite loop if it didn't test
10240 * explicitly for empty basic maps in the outer loop.
10242 static int test_simplify_1(isl_ctx *ctx)
10244 const char *str;
10245 isl_basic_set *bset;
10246 int empty;
10248 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
10249 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
10250 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
10251 "i3 >= i2 }";
10252 bset = isl_basic_set_read_from_str(ctx, str);
10253 empty = isl_basic_set_is_empty(bset);
10254 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
10255 isl_basic_set_free(bset);
10256 if (!bset)
10257 return -1;
10258 if (!empty)
10259 isl_die(ctx, isl_error_unknown,
10260 "basic set should be empty", return -1);
10262 return 0;
10265 /* Check that the equality in the set description below
10266 * is simplified away.
10268 static int test_simplify_2(isl_ctx *ctx)
10270 const char *str;
10271 isl_basic_set *bset;
10272 isl_bool universe;
10274 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
10275 bset = isl_basic_set_read_from_str(ctx, str);
10276 universe = isl_basic_set_plain_is_universe(bset);
10277 isl_basic_set_free(bset);
10279 if (universe < 0)
10280 return -1;
10281 if (!universe)
10282 isl_die(ctx, isl_error_unknown,
10283 "equality not simplified away", return -1);
10284 return 0;
10287 /* Some simplification tests.
10289 static int test_simplify(isl_ctx *ctx)
10291 if (test_simplify_1(ctx) < 0)
10292 return -1;
10293 if (test_simplify_2(ctx) < 0)
10294 return -1;
10295 return 0;
10298 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
10299 * with gbr context would fail to disable the use of the shifted tableau
10300 * when transferring equalities for the input to the context, resulting
10301 * in invalid sample values.
10303 static int test_partial_lexmin(isl_ctx *ctx)
10305 const char *str;
10306 isl_basic_set *bset;
10307 isl_basic_map *bmap;
10308 isl_map *map;
10310 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
10311 bmap = isl_basic_map_read_from_str(ctx, str);
10312 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
10313 bset = isl_basic_set_read_from_str(ctx, str);
10314 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
10315 isl_map_free(map);
10317 if (!map)
10318 return -1;
10320 return 0;
10323 /* Check that the variable compression performed on the existentially
10324 * quantified variables inside isl_basic_set_compute_divs is not confused
10325 * by the implicit equalities among the parameters.
10327 static int test_compute_divs(isl_ctx *ctx)
10329 const char *str;
10330 isl_basic_set *bset;
10331 isl_set *set;
10333 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
10334 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
10335 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
10336 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
10337 bset = isl_basic_set_read_from_str(ctx, str);
10338 set = isl_basic_set_compute_divs(bset);
10339 isl_set_free(set);
10340 if (!set)
10341 return -1;
10343 return 0;
10346 /* Check that isl_schedule_get_map is not confused by a schedule tree
10347 * with divergent filter node parameters, as can result from a call
10348 * to isl_schedule_intersect_domain.
10350 static int test_schedule_tree(isl_ctx *ctx)
10352 const char *str;
10353 isl_union_set *uset;
10354 isl_schedule *sched1, *sched2;
10355 isl_union_map *umap;
10357 uset = isl_union_set_read_from_str(ctx, "{ A[i] }");
10358 sched1 = isl_schedule_from_domain(uset);
10359 uset = isl_union_set_read_from_str(ctx, "{ B[] }");
10360 sched2 = isl_schedule_from_domain(uset);
10362 sched1 = isl_schedule_sequence(sched1, sched2);
10363 str = "[n] -> { A[i] : 0 <= i < n; B[] }";
10364 uset = isl_union_set_read_from_str(ctx, str);
10365 sched1 = isl_schedule_intersect_domain(sched1, uset);
10366 umap = isl_schedule_get_map(sched1);
10367 isl_schedule_free(sched1);
10368 isl_union_map_free(umap);
10369 if (!umap)
10370 return -1;
10372 return 0;
10375 /* Check that a zero-dimensional prefix schedule keeps track
10376 * of the domain and outer filters.
10378 static int test_schedule_tree_prefix(isl_ctx *ctx)
10380 const char *str;
10381 isl_bool equal;
10382 isl_union_set *uset;
10383 isl_union_set_list *filters;
10384 isl_multi_union_pw_aff *mupa, *mupa2;
10385 isl_schedule_node *node;
10387 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
10388 uset = isl_union_set_read_from_str(ctx, str);
10389 node = isl_schedule_node_from_domain(uset);
10390 node = isl_schedule_node_child(node, 0);
10392 str = "{ S1[i,j] : i > j }";
10393 uset = isl_union_set_read_from_str(ctx, str);
10394 filters = isl_union_set_list_from_union_set(uset);
10395 str = "{ S1[i,j] : i <= j; S2[i,j] }";
10396 uset = isl_union_set_read_from_str(ctx, str);
10397 filters = isl_union_set_list_add(filters, uset);
10398 node = isl_schedule_node_insert_sequence(node, filters);
10400 node = isl_schedule_node_grandchild(node, 0, 0);
10401 mupa = isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
10402 str = "([] : { S1[i,j] : i > j })";
10403 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx, str);
10404 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
10405 isl_multi_union_pw_aff_free(mupa2);
10406 isl_multi_union_pw_aff_free(mupa);
10407 isl_schedule_node_free(node);
10409 if (equal < 0)
10410 return -1;
10411 if (!equal)
10412 isl_die(ctx, isl_error_unknown, "unexpected prefix schedule",
10413 return -1);
10415 return 0;
10418 /* Check that the reaching domain elements and the prefix schedule
10419 * at a leaf node are the same before and after grouping.
10421 static int test_schedule_tree_group_1(isl_ctx *ctx)
10423 int equal;
10424 const char *str;
10425 isl_id *id;
10426 isl_union_set *uset;
10427 isl_multi_union_pw_aff *mupa;
10428 isl_union_pw_multi_aff *upma1, *upma2;
10429 isl_union_set *domain1, *domain2;
10430 isl_union_map *umap1, *umap2;
10431 isl_schedule_node *node;
10433 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
10434 uset = isl_union_set_read_from_str(ctx, str);
10435 node = isl_schedule_node_from_domain(uset);
10436 node = isl_schedule_node_child(node, 0);
10437 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
10438 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10439 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10440 node = isl_schedule_node_child(node, 0);
10441 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
10442 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10443 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10444 node = isl_schedule_node_child(node, 0);
10445 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
10446 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
10447 domain1 = isl_schedule_node_get_domain(node);
10448 id = isl_id_alloc(ctx, "group", NULL);
10449 node = isl_schedule_node_parent(node);
10450 node = isl_schedule_node_group(node, id);
10451 node = isl_schedule_node_child(node, 0);
10452 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
10453 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
10454 domain2 = isl_schedule_node_get_domain(node);
10455 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
10456 if (equal >= 0 && equal)
10457 equal = isl_union_set_is_equal(domain1, domain2);
10458 if (equal >= 0 && equal)
10459 equal = isl_union_map_is_equal(umap1, umap2);
10460 isl_union_map_free(umap1);
10461 isl_union_map_free(umap2);
10462 isl_union_set_free(domain1);
10463 isl_union_set_free(domain2);
10464 isl_union_pw_multi_aff_free(upma1);
10465 isl_union_pw_multi_aff_free(upma2);
10466 isl_schedule_node_free(node);
10468 if (equal < 0)
10469 return -1;
10470 if (!equal)
10471 isl_die(ctx, isl_error_unknown,
10472 "expressions not equal", return -1);
10474 return 0;
10477 /* Check that we can have nested groupings and that the union map
10478 * schedule representation is the same before and after the grouping.
10479 * Note that after the grouping, the union map representation contains
10480 * the domain constraints from the ranges of the expansion nodes,
10481 * while they are missing from the union map representation of
10482 * the tree without expansion nodes.
10484 * Also check that the global expansion is as expected.
10486 static int test_schedule_tree_group_2(isl_ctx *ctx)
10488 int equal, equal_expansion;
10489 const char *str;
10490 isl_id *id;
10491 isl_union_set *uset;
10492 isl_union_map *umap1, *umap2;
10493 isl_union_map *expansion1, *expansion2;
10494 isl_union_set_list *filters;
10495 isl_multi_union_pw_aff *mupa;
10496 isl_schedule *schedule;
10497 isl_schedule_node *node;
10499 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
10500 "S3[i,j] : 0 <= i,j < 10 }";
10501 uset = isl_union_set_read_from_str(ctx, str);
10502 node = isl_schedule_node_from_domain(uset);
10503 node = isl_schedule_node_child(node, 0);
10504 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
10505 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10506 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10507 node = isl_schedule_node_child(node, 0);
10508 str = "{ S1[i,j] }";
10509 uset = isl_union_set_read_from_str(ctx, str);
10510 filters = isl_union_set_list_from_union_set(uset);
10511 str = "{ S2[i,j]; S3[i,j] }";
10512 uset = isl_union_set_read_from_str(ctx, str);
10513 filters = isl_union_set_list_add(filters, uset);
10514 node = isl_schedule_node_insert_sequence(node, filters);
10515 node = isl_schedule_node_grandchild(node, 1, 0);
10516 str = "{ S2[i,j] }";
10517 uset = isl_union_set_read_from_str(ctx, str);
10518 filters = isl_union_set_list_from_union_set(uset);
10519 str = "{ S3[i,j] }";
10520 uset = isl_union_set_read_from_str(ctx, str);
10521 filters = isl_union_set_list_add(filters, uset);
10522 node = isl_schedule_node_insert_sequence(node, filters);
10524 schedule = isl_schedule_node_get_schedule(node);
10525 umap1 = isl_schedule_get_map(schedule);
10526 uset = isl_schedule_get_domain(schedule);
10527 umap1 = isl_union_map_intersect_domain(umap1, uset);
10528 isl_schedule_free(schedule);
10530 node = isl_schedule_node_grandparent(node);
10531 id = isl_id_alloc(ctx, "group1", NULL);
10532 node = isl_schedule_node_group(node, id);
10533 node = isl_schedule_node_grandchild(node, 1, 0);
10534 id = isl_id_alloc(ctx, "group2", NULL);
10535 node = isl_schedule_node_group(node, id);
10537 schedule = isl_schedule_node_get_schedule(node);
10538 umap2 = isl_schedule_get_map(schedule);
10539 isl_schedule_free(schedule);
10541 node = isl_schedule_node_root(node);
10542 node = isl_schedule_node_child(node, 0);
10543 expansion1 = isl_schedule_node_get_subtree_expansion(node);
10544 isl_schedule_node_free(node);
10546 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
10547 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
10548 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
10550 expansion2 = isl_union_map_read_from_str(ctx, str);
10552 equal = isl_union_map_is_equal(umap1, umap2);
10553 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
10555 isl_union_map_free(umap1);
10556 isl_union_map_free(umap2);
10557 isl_union_map_free(expansion1);
10558 isl_union_map_free(expansion2);
10560 if (equal < 0 || equal_expansion < 0)
10561 return -1;
10562 if (!equal)
10563 isl_die(ctx, isl_error_unknown,
10564 "expressions not equal", return -1);
10565 if (!equal_expansion)
10566 isl_die(ctx, isl_error_unknown,
10567 "unexpected expansion", return -1);
10569 return 0;
10572 /* Some tests for the isl_schedule_node_group function.
10574 static int test_schedule_tree_group(isl_ctx *ctx)
10576 if (test_schedule_tree_group_1(ctx) < 0)
10577 return -1;
10578 if (test_schedule_tree_group_2(ctx) < 0)
10579 return -1;
10580 return 0;
10583 struct {
10584 const char *set;
10585 const char *dual;
10586 } coef_tests[] = {
10587 { "{ rat: [i] : 0 <= i <= 10 }",
10588 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
10589 { "{ rat: [i] : FALSE }",
10590 "{ rat: coefficients[[cst] -> [a]] }" },
10591 { "{ rat: [i] : }",
10592 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
10593 { "{ [0:,1,2:3] }",
10594 "{ rat: coefficients[[c_cst] -> [a, b, c]] : "
10595 "a >= 0 and 2c >= -c_cst - b and 3c >= -c_cst - b }" },
10596 { "[M, N] -> { [x = (1 - N):-1, -4x:(M - 4x)] }",
10597 "{ rat: coefficients[[c_cst, c_M = 0:, c_N = 0:] -> [a, b = -c_M:]] :"
10598 "4b >= -c_N + a and 4b >= -c_cst - 2c_N + a }" },
10599 { "{ rat : [x, y] : 1 <= 2x <= 9 and 2 <= 3y <= 16 }",
10600 "{ rat: coefficients[[c_cst] -> [c_x, c_y]] : "
10601 "4c_y >= -6c_cst - 3c_x and 4c_y >= -6c_cst - 27c_x and "
10602 "32c_y >= -6c_cst - 3c_x and 32c_y >= -6c_cst - 27c_x }" },
10603 { "{ [x, y, z] : 3y <= 2x - 2 and y >= -2 + 2x and 2y >= 2 - x }",
10604 "{ rat: coefficients[[cst] -> [a, b, c]] }" },
10607 struct {
10608 const char *set;
10609 const char *dual;
10610 } sol_tests[] = {
10611 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
10612 "{ rat: [i] : 0 <= i <= 10 }" },
10613 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
10614 "{ rat: [i] }" },
10615 { "{ rat: coefficients[[cst] -> [a]] }",
10616 "{ rat: [i] : FALSE }" },
10619 /* Test the basic functionality of isl_basic_set_coefficients and
10620 * isl_basic_set_solutions.
10622 static int test_dual(isl_ctx *ctx)
10624 int i;
10626 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
10627 int equal;
10628 isl_basic_set *bset1, *bset2;
10630 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
10631 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
10632 bset1 = isl_basic_set_coefficients(bset1);
10633 equal = isl_basic_set_is_equal(bset1, bset2);
10634 isl_basic_set_free(bset1);
10635 isl_basic_set_free(bset2);
10636 if (equal < 0)
10637 return -1;
10638 if (!equal)
10639 isl_die(ctx, isl_error_unknown,
10640 "incorrect dual", return -1);
10643 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
10644 int equal;
10645 isl_basic_set *bset1, *bset2;
10647 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
10648 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
10649 bset1 = isl_basic_set_solutions(bset1);
10650 equal = isl_basic_set_is_equal(bset1, bset2);
10651 isl_basic_set_free(bset1);
10652 isl_basic_set_free(bset2);
10653 if (equal < 0)
10654 return -1;
10655 if (!equal)
10656 isl_die(ctx, isl_error_unknown,
10657 "incorrect dual", return -1);
10660 return 0;
10663 struct {
10664 int scale_tile;
10665 int shift_point;
10666 const char *domain;
10667 const char *schedule;
10668 const char *sizes;
10669 const char *tile;
10670 const char *point;
10671 } tile_tests[] = {
10672 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
10673 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10674 "{ [32,32] }",
10675 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
10676 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10678 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
10679 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10680 "{ [32,32] }",
10681 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
10682 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10684 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
10685 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10686 "{ [32,32] }",
10687 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
10688 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
10690 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
10691 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
10692 "{ [32,32] }",
10693 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
10694 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
10698 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
10699 * tile the band and then check if the tile and point bands have the
10700 * expected partial schedule.
10702 static int test_tile(isl_ctx *ctx)
10704 int i;
10705 int scale;
10706 int shift;
10708 scale = isl_options_get_tile_scale_tile_loops(ctx);
10709 shift = isl_options_get_tile_shift_point_loops(ctx);
10711 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
10712 int opt;
10713 int equal;
10714 const char *str;
10715 isl_union_set *domain;
10716 isl_multi_union_pw_aff *mupa, *mupa2;
10717 isl_schedule_node *node;
10718 isl_multi_val *sizes;
10720 opt = tile_tests[i].scale_tile;
10721 isl_options_set_tile_scale_tile_loops(ctx, opt);
10722 opt = tile_tests[i].shift_point;
10723 isl_options_set_tile_shift_point_loops(ctx, opt);
10725 str = tile_tests[i].domain;
10726 domain = isl_union_set_read_from_str(ctx, str);
10727 node = isl_schedule_node_from_domain(domain);
10728 node = isl_schedule_node_child(node, 0);
10729 str = tile_tests[i].schedule;
10730 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10731 node = isl_schedule_node_insert_partial_schedule(node, mupa);
10732 str = tile_tests[i].sizes;
10733 sizes = isl_multi_val_read_from_str(ctx, str);
10734 node = isl_schedule_node_band_tile(node, sizes);
10736 str = tile_tests[i].tile;
10737 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10738 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
10739 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
10740 isl_multi_union_pw_aff_free(mupa);
10741 isl_multi_union_pw_aff_free(mupa2);
10743 node = isl_schedule_node_child(node, 0);
10745 str = tile_tests[i].point;
10746 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
10747 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
10748 if (equal >= 0 && equal)
10749 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
10750 mupa2);
10751 isl_multi_union_pw_aff_free(mupa);
10752 isl_multi_union_pw_aff_free(mupa2);
10754 isl_schedule_node_free(node);
10756 if (equal < 0)
10757 return -1;
10758 if (!equal)
10759 isl_die(ctx, isl_error_unknown,
10760 "unexpected result", return -1);
10763 isl_options_set_tile_scale_tile_loops(ctx, scale);
10764 isl_options_set_tile_shift_point_loops(ctx, shift);
10766 return 0;
10769 /* Check that the domain hash of a space is equal to the hash
10770 * of the domain of the space, both ignoring parameters.
10772 static int test_domain_hash(isl_ctx *ctx)
10774 isl_map *map;
10775 isl_space *space;
10776 uint32_t hash1, hash2;
10778 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
10779 space = isl_map_get_space(map);
10780 isl_map_free(map);
10781 hash1 = isl_space_get_tuple_domain_hash(space);
10782 space = isl_space_domain(space);
10783 hash2 = isl_space_get_tuple_hash(space);
10784 isl_space_free(space);
10786 if (!space)
10787 return -1;
10788 if (hash1 != hash2)
10789 isl_die(ctx, isl_error_unknown,
10790 "domain hash not equal to hash of domain", return -1);
10792 return 0;
10795 /* Check that a universe basic set that is not obviously equal to the universe
10796 * is still recognized as being equal to the universe.
10798 static int test_universe(isl_ctx *ctx)
10800 const char *s;
10801 isl_basic_set *bset;
10802 isl_bool is_univ;
10804 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
10805 bset = isl_basic_set_read_from_str(ctx, s);
10806 is_univ = isl_basic_set_is_universe(bset);
10807 isl_basic_set_free(bset);
10809 if (is_univ < 0)
10810 return -1;
10811 if (!is_univ)
10812 isl_die(ctx, isl_error_unknown,
10813 "not recognized as universe set", return -1);
10815 return 0;
10818 /* Sets for which chambers are computed and checked.
10820 const char *chambers_tests[] = {
10821 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
10822 "z >= 0 and z <= C - y and z <= B - x - y }",
10825 /* Add the domain of "cell" to "cells".
10827 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
10829 isl_basic_set_list **cells = user;
10830 isl_basic_set *dom;
10832 dom = isl_cell_get_domain(cell);
10833 isl_cell_free(cell);
10834 *cells = isl_basic_set_list_add(*cells, dom);
10836 return *cells ? isl_stat_ok : isl_stat_error;
10839 /* Check that the elements of "list" are pairwise disjoint.
10841 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
10843 int i, j;
10844 isl_size n;
10846 n = isl_basic_set_list_n_basic_set(list);
10847 if (n < 0)
10848 return isl_stat_error;
10850 for (i = 0; i < n; ++i) {
10851 isl_basic_set *bset_i;
10853 bset_i = isl_basic_set_list_get_basic_set(list, i);
10854 for (j = i + 1; j < n; ++j) {
10855 isl_basic_set *bset_j;
10856 isl_bool disjoint;
10858 bset_j = isl_basic_set_list_get_basic_set(list, j);
10859 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
10860 isl_basic_set_free(bset_j);
10861 if (!disjoint)
10862 isl_die(isl_basic_set_list_get_ctx(list),
10863 isl_error_unknown, "not disjoint",
10864 break);
10865 if (disjoint < 0 || !disjoint)
10866 break;
10868 isl_basic_set_free(bset_i);
10869 if (j < n)
10870 return isl_stat_error;
10873 return isl_stat_ok;
10876 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
10877 * are pairwise disjoint.
10879 static int test_chambers(isl_ctx *ctx)
10881 int i;
10883 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
10884 isl_basic_set *bset;
10885 isl_vertices *vertices;
10886 isl_basic_set_list *cells;
10887 isl_stat ok;
10889 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
10890 vertices = isl_basic_set_compute_vertices(bset);
10891 cells = isl_basic_set_list_alloc(ctx, 0);
10892 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
10893 &cells) < 0)
10894 cells = isl_basic_set_list_free(cells);
10895 ok = check_pairwise_disjoint(cells);
10896 isl_basic_set_list_free(cells);
10897 isl_vertices_free(vertices);
10898 isl_basic_set_free(bset);
10900 if (ok < 0)
10901 return -1;
10904 return 0;
10907 struct {
10908 const char *name;
10909 int (*fn)(isl_ctx *ctx);
10910 } tests [] = {
10911 { "universe", &test_universe },
10912 { "domain hash", &test_domain_hash },
10913 { "dual", &test_dual },
10914 { "dependence analysis", &test_flow },
10915 { "val", &test_val },
10916 { "compute divs", &test_compute_divs },
10917 { "partial lexmin", &test_partial_lexmin },
10918 { "simplify", &test_simplify },
10919 { "curry", &test_curry },
10920 { "piecewise multi affine expressions", &test_pw_multi_aff },
10921 { "multi piecewise affine expressions", &test_multi_pw_aff },
10922 { "conversion", &test_conversion },
10923 { "list", &test_list },
10924 { "align parameters", &test_align_parameters },
10925 { "drop unused parameters", &test_drop_unused_parameters },
10926 { "pullback", &test_pullback },
10927 { "AST", &test_ast },
10928 { "AST build", &test_ast_build },
10929 { "AST generation", &test_ast_gen },
10930 { "eliminate", &test_eliminate },
10931 { "deltas_map", &test_deltas_map },
10932 { "residue class", &test_residue_class },
10933 { "div", &test_div },
10934 { "slice", &test_slice },
10935 { "sample", &test_sample },
10936 { "empty projection", &test_empty_projection },
10937 { "output", &test_output },
10938 { "vertices", &test_vertices },
10939 { "chambers", &test_chambers },
10940 { "fixed", &test_fixed },
10941 { "equal", &test_equal },
10942 { "disjoint", &test_disjoint },
10943 { "product", &test_product },
10944 { "dim_max", &test_dim_max },
10945 { "affine", &test_aff },
10946 { "injective", &test_injective },
10947 { "schedule (whole component)", &test_schedule_whole },
10948 { "schedule (incremental)", &test_schedule_incremental },
10949 { "schedule tree", &test_schedule_tree },
10950 { "schedule tree prefix", &test_schedule_tree_prefix },
10951 { "schedule tree grouping", &test_schedule_tree_group },
10952 { "tile", &test_tile },
10953 { "union map", &test_union_map },
10954 { "union_pw", &test_union_pw },
10955 { "locus", &test_locus },
10956 { "eval", &test_eval },
10957 { "parse", &test_parse },
10958 { "single-valued", &test_sv },
10959 { "recession cone", &test_recession_cone },
10960 { "affine hull", &test_affine_hull },
10961 { "simple_hull", &test_simple_hull },
10962 { "box hull", &test_box_hull },
10963 { "coalesce", &test_coalesce },
10964 { "factorize", &test_factorize },
10965 { "subset", &test_subset },
10966 { "subtract", &test_subtract },
10967 { "intersect", &test_intersect },
10968 { "lexmin", &test_lexmin },
10969 { "min", &test_min },
10970 { "set lower bounds", &test_min_mpa },
10971 { "gist", &test_gist },
10972 { "piecewise quasi-polynomials", &test_pwqp },
10973 { "lift", &test_lift },
10974 { "bind parameters", &test_bind },
10975 { "unbind parameters", &test_unbind },
10976 { "bound", &test_bound },
10977 { "get lists", &test_get_list },
10978 { "union", &test_union },
10979 { "split periods", &test_split_periods },
10980 { "lexicographic order", &test_lex },
10981 { "bijectivity", &test_bijective },
10982 { "dataflow analysis", &test_dep },
10983 { "reading", &test_read },
10984 { "bounded", &test_bounded },
10985 { "construction", &test_construction },
10986 { "dimension manipulation", &test_dim },
10987 { "map application", &test_application },
10988 { "convex hull", &test_convex_hull },
10989 { "transitive closure", &test_closure },
10990 { "isl_bool", &test_isl_bool},
10993 int main(int argc, char **argv)
10995 int i;
10996 struct isl_ctx *ctx;
10997 struct isl_options *options;
10999 options = isl_options_new_with_defaults();
11000 assert(options);
11001 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
11003 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
11004 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
11005 printf("%s\n", tests[i].name);
11006 if (tests[i].fn(ctx) < 0)
11007 goto error;
11009 isl_ctx_free(ctx);
11010 return 0;
11011 error:
11012 isl_ctx_free(ctx);
11013 return -1;