2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
18 #include <isl/constraint.h>
19 #include <isl/polynomial.h>
20 #include <isl/union_map.h>
21 #include <isl_factorization.h>
22 #include <isl/schedule.h>
26 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
29 char *pattern = "%s/test_inputs/%s.%s";
31 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
33 filename = isl_alloc_array(ctx, char, length);
38 sprintf(filename, pattern, srcdir, name, suffix);
43 void test_parse_map(isl_ctx *ctx, const char *str)
47 map = isl_map_read_from_str(ctx, str);
52 void test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
56 map = isl_map_read_from_str(ctx, str);
57 map2 = isl_map_read_from_str(ctx, str2);
58 assert(map && map2 && isl_map_is_equal(map, map2));
63 void test_parse_pwqp(isl_ctx *ctx, const char *str)
65 isl_pw_qpolynomial *pwqp;
67 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
69 isl_pw_qpolynomial_free(pwqp);
72 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
76 pwaff = isl_pw_aff_read_from_str(ctx, str);
78 isl_pw_aff_free(pwaff);
81 void test_parse(struct isl_ctx *ctx)
84 const char *str, *str2;
86 str = "{ [i] -> [-i] }";
87 map = isl_map_read_from_str(ctx, str);
91 str = "{ A[i] -> L[([i/3])] }";
92 map = isl_map_read_from_str(ctx, str);
96 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
97 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
98 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
100 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
101 str2 = "{ [x, y] : 2y >= 6 - x }";
102 test_parse_map_equal(ctx, str, str2);
104 test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
105 "{ [x,y] : x <= y, 2*y + 3 }");
106 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
107 test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }", str);
109 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
110 map = isl_map_read_from_str(ctx, str);
111 str = "{ [new, old] -> [o0, o1] : "
112 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
113 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
114 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
115 map2 = isl_map_read_from_str(ctx, str);
116 assert(isl_map_is_equal(map, map2));
120 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
121 map = isl_map_read_from_str(ctx, str);
122 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
123 map2 = isl_map_read_from_str(ctx, str);
124 assert(isl_map_is_equal(map, map2));
128 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
129 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
130 test_parse_map_equal(ctx, str, str2);
132 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
133 str2 = "{ [i,j] -> [min(i,j)] }";
134 test_parse_map_equal(ctx, str, str2);
136 str = "{ [i,j] : i != j }";
137 str2 = "{ [i,j] : i < j or i > j }";
138 test_parse_map_equal(ctx, str, str2);
140 str = "{ [i,j] : (i+1)*2 >= j }";
141 str2 = "{ [i, j] : j <= 2 + 2i }";
142 test_parse_map_equal(ctx, str, str2);
144 str = "{ [i] -> [i > 0 ? 4 : 5] }";
145 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
146 test_parse_map_equal(ctx, str, str2);
148 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
149 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
150 test_parse_map_equal(ctx, str, str2);
152 str = "{ [x] : x >= 0 }";
153 str2 = "{ [x] : x-0 >= 0 }";
154 test_parse_map_equal(ctx, str, str2);
156 str = "{ [i] : ((i > 10)) }";
157 str2 = "{ [i] : i >= 11 }";
158 test_parse_map_equal(ctx, str, str2);
160 str = "{ [i] -> [0] }";
161 str2 = "{ [i] -> [0 * i] }";
162 test_parse_map_equal(ctx, str, str2);
164 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
165 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
166 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
169 void test_read(struct isl_ctx *ctx)
173 struct isl_basic_set *bset1, *bset2;
174 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
176 filename = get_filename(ctx, "set", "omega");
178 input = fopen(filename, "r");
181 bset1 = isl_basic_set_read_from_file(ctx, input);
182 bset2 = isl_basic_set_read_from_str(ctx, str);
184 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
186 isl_basic_set_free(bset1);
187 isl_basic_set_free(bset2);
193 void test_bounded(struct isl_ctx *ctx)
198 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
199 bounded = isl_set_is_bounded(set);
203 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
204 bounded = isl_set_is_bounded(set);
208 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
209 bounded = isl_set_is_bounded(set);
214 /* Construct the basic set { [i] : 5 <= i <= N } */
215 void test_construction(struct isl_ctx *ctx)
220 struct isl_basic_set *bset;
221 struct isl_constraint *c;
225 dim = isl_space_set_alloc(ctx, 1, 1);
226 bset = isl_basic_set_universe(isl_space_copy(dim));
227 ls = isl_local_space_from_space(dim);
229 c = isl_inequality_alloc(isl_local_space_copy(ls));
230 isl_int_set_si(v, -1);
231 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
232 isl_int_set_si(v, 1);
233 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
234 bset = isl_basic_set_add_constraint(bset, c);
236 c = isl_inequality_alloc(isl_local_space_copy(ls));
237 isl_int_set_si(v, 1);
238 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
239 isl_int_set_si(v, -5);
240 isl_constraint_set_constant(c, v);
241 bset = isl_basic_set_add_constraint(bset, c);
243 isl_local_space_free(ls);
244 isl_basic_set_free(bset);
249 void test_dim(struct isl_ctx *ctx)
252 isl_map *map1, *map2;
254 map1 = isl_map_read_from_str(ctx,
255 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
256 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
257 map2 = isl_map_read_from_str(ctx,
258 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
259 assert(isl_map_is_equal(map1, map2));
262 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
263 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
264 assert(isl_map_is_equal(map1, map2));
269 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
270 map1 = isl_map_read_from_str(ctx, str);
271 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
272 map2 = isl_map_read_from_str(ctx, str);
273 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
274 assert(isl_map_is_equal(map1, map2));
280 void test_div(struct isl_ctx *ctx)
285 struct isl_basic_set *bset;
286 struct isl_constraint *c;
291 dim = isl_space_set_alloc(ctx, 0, 3);
292 bset = isl_basic_set_universe(isl_space_copy(dim));
293 ls = isl_local_space_from_space(dim);
295 c = isl_equality_alloc(isl_local_space_copy(ls));
296 isl_int_set_si(v, -1);
297 isl_constraint_set_constant(c, v);
298 isl_int_set_si(v, 1);
299 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
300 isl_int_set_si(v, 3);
301 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
302 bset = isl_basic_set_add_constraint(bset, c);
304 c = isl_equality_alloc(isl_local_space_copy(ls));
305 isl_int_set_si(v, 1);
306 isl_constraint_set_constant(c, v);
307 isl_int_set_si(v, -1);
308 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
309 isl_int_set_si(v, 3);
310 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
311 bset = isl_basic_set_add_constraint(bset, c);
313 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
315 assert(bset && bset->n_div == 1);
316 isl_local_space_free(ls);
317 isl_basic_set_free(bset);
320 dim = isl_space_set_alloc(ctx, 0, 3);
321 bset = isl_basic_set_universe(isl_space_copy(dim));
322 ls = isl_local_space_from_space(dim);
324 c = isl_equality_alloc(isl_local_space_copy(ls));
325 isl_int_set_si(v, 1);
326 isl_constraint_set_constant(c, v);
327 isl_int_set_si(v, -1);
328 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
329 isl_int_set_si(v, 3);
330 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
331 bset = isl_basic_set_add_constraint(bset, c);
333 c = isl_equality_alloc(isl_local_space_copy(ls));
334 isl_int_set_si(v, -1);
335 isl_constraint_set_constant(c, v);
336 isl_int_set_si(v, 1);
337 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
338 isl_int_set_si(v, 3);
339 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
340 bset = isl_basic_set_add_constraint(bset, c);
342 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
344 assert(bset && bset->n_div == 1);
345 isl_local_space_free(ls);
346 isl_basic_set_free(bset);
349 dim = isl_space_set_alloc(ctx, 0, 3);
350 bset = isl_basic_set_universe(isl_space_copy(dim));
351 ls = isl_local_space_from_space(dim);
353 c = isl_equality_alloc(isl_local_space_copy(ls));
354 isl_int_set_si(v, 1);
355 isl_constraint_set_constant(c, v);
356 isl_int_set_si(v, -1);
357 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
358 isl_int_set_si(v, 3);
359 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
360 bset = isl_basic_set_add_constraint(bset, c);
362 c = isl_equality_alloc(isl_local_space_copy(ls));
363 isl_int_set_si(v, -3);
364 isl_constraint_set_constant(c, v);
365 isl_int_set_si(v, 1);
366 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
367 isl_int_set_si(v, 4);
368 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
369 bset = isl_basic_set_add_constraint(bset, c);
371 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
373 assert(bset && bset->n_div == 1);
374 isl_local_space_free(ls);
375 isl_basic_set_free(bset);
378 dim = isl_space_set_alloc(ctx, 0, 3);
379 bset = isl_basic_set_universe(isl_space_copy(dim));
380 ls = isl_local_space_from_space(dim);
382 c = isl_equality_alloc(isl_local_space_copy(ls));
383 isl_int_set_si(v, 2);
384 isl_constraint_set_constant(c, v);
385 isl_int_set_si(v, -1);
386 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
387 isl_int_set_si(v, 3);
388 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
389 bset = isl_basic_set_add_constraint(bset, c);
391 c = isl_equality_alloc(isl_local_space_copy(ls));
392 isl_int_set_si(v, -1);
393 isl_constraint_set_constant(c, v);
394 isl_int_set_si(v, 1);
395 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
396 isl_int_set_si(v, 6);
397 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
398 bset = isl_basic_set_add_constraint(bset, c);
400 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
402 assert(isl_basic_set_is_empty(bset));
403 isl_local_space_free(ls);
404 isl_basic_set_free(bset);
407 dim = isl_space_set_alloc(ctx, 0, 3);
408 bset = isl_basic_set_universe(isl_space_copy(dim));
409 ls = isl_local_space_from_space(dim);
411 c = isl_equality_alloc(isl_local_space_copy(ls));
412 isl_int_set_si(v, -1);
413 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
414 isl_int_set_si(v, 3);
415 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
416 bset = isl_basic_set_add_constraint(bset, c);
418 c = isl_equality_alloc(isl_local_space_copy(ls));
419 isl_int_set_si(v, 1);
420 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
421 isl_int_set_si(v, -3);
422 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
423 bset = isl_basic_set_add_constraint(bset, c);
425 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
427 assert(bset && bset->n_div == 0);
428 isl_basic_set_free(bset);
429 isl_local_space_free(ls);
432 dim = isl_space_set_alloc(ctx, 0, 3);
433 bset = isl_basic_set_universe(isl_space_copy(dim));
434 ls = isl_local_space_from_space(dim);
436 c = isl_equality_alloc(isl_local_space_copy(ls));
437 isl_int_set_si(v, -1);
438 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
439 isl_int_set_si(v, 6);
440 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
441 bset = isl_basic_set_add_constraint(bset, c);
443 c = isl_equality_alloc(isl_local_space_copy(ls));
444 isl_int_set_si(v, 1);
445 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
446 isl_int_set_si(v, -3);
447 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
448 bset = isl_basic_set_add_constraint(bset, c);
450 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
452 assert(bset && bset->n_div == 1);
453 isl_basic_set_free(bset);
454 isl_local_space_free(ls);
457 /* This test is a bit tricky. We set up an equality
459 * Normalization of divs creates _two_ divs
462 * Afterwards e0 is removed again because it has coefficient -1
463 * and we end up with the original equality and div again.
464 * Perhaps we can avoid the introduction of this temporary div.
466 dim = isl_space_set_alloc(ctx, 0, 4);
467 bset = isl_basic_set_universe(isl_space_copy(dim));
468 ls = isl_local_space_from_space(dim);
470 c = isl_equality_alloc(isl_local_space_copy(ls));
471 isl_int_set_si(v, -1);
472 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
473 isl_int_set_si(v, -3);
474 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
475 isl_int_set_si(v, -3);
476 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
477 isl_int_set_si(v, 6);
478 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
479 bset = isl_basic_set_add_constraint(bset, c);
481 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
483 /* Test disabled for now */
485 assert(bset && bset->n_div == 1);
487 isl_local_space_free(ls);
488 isl_basic_set_free(bset);
491 dim = isl_space_set_alloc(ctx, 0, 5);
492 bset = isl_basic_set_universe(isl_space_copy(dim));
493 ls = isl_local_space_from_space(dim);
495 c = isl_equality_alloc(isl_local_space_copy(ls));
496 isl_int_set_si(v, -1);
497 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
498 isl_int_set_si(v, -3);
499 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
500 isl_int_set_si(v, -3);
501 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
502 isl_int_set_si(v, 6);
503 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
504 bset = isl_basic_set_add_constraint(bset, c);
506 c = isl_equality_alloc(isl_local_space_copy(ls));
507 isl_int_set_si(v, -1);
508 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
509 isl_int_set_si(v, 1);
510 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
511 isl_int_set_si(v, 1);
512 isl_constraint_set_constant(c, v);
513 bset = isl_basic_set_add_constraint(bset, c);
515 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
517 /* Test disabled for now */
519 assert(bset && bset->n_div == 1);
521 isl_local_space_free(ls);
522 isl_basic_set_free(bset);
525 dim = isl_space_set_alloc(ctx, 0, 4);
526 bset = isl_basic_set_universe(isl_space_copy(dim));
527 ls = isl_local_space_from_space(dim);
529 c = isl_equality_alloc(isl_local_space_copy(ls));
530 isl_int_set_si(v, 1);
531 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
532 isl_int_set_si(v, -1);
533 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
534 isl_int_set_si(v, -2);
535 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
536 bset = isl_basic_set_add_constraint(bset, c);
538 c = isl_equality_alloc(isl_local_space_copy(ls));
539 isl_int_set_si(v, -1);
540 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
541 isl_int_set_si(v, 3);
542 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
543 isl_int_set_si(v, 2);
544 isl_constraint_set_constant(c, v);
545 bset = isl_basic_set_add_constraint(bset, c);
547 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
549 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
551 assert(!isl_basic_set_is_empty(bset));
553 isl_local_space_free(ls);
554 isl_basic_set_free(bset);
557 dim = isl_space_set_alloc(ctx, 0, 3);
558 bset = isl_basic_set_universe(isl_space_copy(dim));
559 ls = isl_local_space_from_space(dim);
561 c = isl_equality_alloc(isl_local_space_copy(ls));
562 isl_int_set_si(v, 1);
563 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
564 isl_int_set_si(v, -2);
565 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
566 bset = isl_basic_set_add_constraint(bset, c);
568 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
570 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
572 isl_local_space_free(ls);
573 isl_basic_set_free(bset);
578 void test_application_case(struct isl_ctx *ctx, const char *name)
582 struct isl_basic_set *bset1, *bset2;
583 struct isl_basic_map *bmap;
585 filename = get_filename(ctx, name, "omega");
587 input = fopen(filename, "r");
590 bset1 = isl_basic_set_read_from_file(ctx, input);
591 bmap = isl_basic_map_read_from_file(ctx, input);
593 bset1 = isl_basic_set_apply(bset1, bmap);
595 bset2 = isl_basic_set_read_from_file(ctx, input);
597 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
599 isl_basic_set_free(bset1);
600 isl_basic_set_free(bset2);
606 void test_application(struct isl_ctx *ctx)
608 test_application_case(ctx, "application");
609 test_application_case(ctx, "application2");
612 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
616 struct isl_basic_set *bset1, *bset2;
618 filename = get_filename(ctx, name, "polylib");
620 input = fopen(filename, "r");
623 bset1 = isl_basic_set_read_from_file(ctx, input);
624 bset2 = isl_basic_set_read_from_file(ctx, input);
626 bset1 = isl_basic_set_affine_hull(bset1);
628 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
630 isl_basic_set_free(bset1);
631 isl_basic_set_free(bset2);
637 void test_affine_hull(struct isl_ctx *ctx)
639 test_affine_hull_case(ctx, "affine2");
640 test_affine_hull_case(ctx, "affine");
641 test_affine_hull_case(ctx, "affine3");
644 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
648 struct isl_basic_set *bset1, *bset2;
651 filename = get_filename(ctx, name, "polylib");
653 input = fopen(filename, "r");
656 bset1 = isl_basic_set_read_from_file(ctx, input);
657 bset2 = isl_basic_set_read_from_file(ctx, input);
659 set = isl_basic_set_union(bset1, bset2);
660 bset1 = isl_set_convex_hull(set);
662 bset2 = isl_basic_set_read_from_file(ctx, input);
664 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
666 isl_basic_set_free(bset1);
667 isl_basic_set_free(bset2);
673 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
675 const char *str1, *str2;
676 isl_set *set1, *set2;
677 int orig_convex = ctx->opt->convex;
678 ctx->opt->convex = convex;
680 test_convex_hull_case(ctx, "convex0");
681 test_convex_hull_case(ctx, "convex1");
682 test_convex_hull_case(ctx, "convex2");
683 test_convex_hull_case(ctx, "convex3");
684 test_convex_hull_case(ctx, "convex4");
685 test_convex_hull_case(ctx, "convex5");
686 test_convex_hull_case(ctx, "convex6");
687 test_convex_hull_case(ctx, "convex7");
688 test_convex_hull_case(ctx, "convex8");
689 test_convex_hull_case(ctx, "convex9");
690 test_convex_hull_case(ctx, "convex10");
691 test_convex_hull_case(ctx, "convex11");
692 test_convex_hull_case(ctx, "convex12");
693 test_convex_hull_case(ctx, "convex13");
694 test_convex_hull_case(ctx, "convex14");
695 test_convex_hull_case(ctx, "convex15");
697 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
698 "(i0 = 1 and i1 = 0 and i2 = 1) or "
699 "(i0 = 0 and i1 = 0 and i2 = 0) }";
700 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
701 set1 = isl_set_read_from_str(ctx, str1);
702 set2 = isl_set_read_from_str(ctx, str2);
703 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
704 assert(isl_set_is_equal(set1, set2));
708 ctx->opt->convex = orig_convex;
711 void test_convex_hull(struct isl_ctx *ctx)
713 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
714 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
717 void test_gist_case(struct isl_ctx *ctx, const char *name)
721 struct isl_basic_set *bset1, *bset2;
723 filename = get_filename(ctx, name, "polylib");
725 input = fopen(filename, "r");
728 bset1 = isl_basic_set_read_from_file(ctx, input);
729 bset2 = isl_basic_set_read_from_file(ctx, input);
731 bset1 = isl_basic_set_gist(bset1, bset2);
733 bset2 = isl_basic_set_read_from_file(ctx, input);
735 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
737 isl_basic_set_free(bset1);
738 isl_basic_set_free(bset2);
744 void test_gist(struct isl_ctx *ctx)
747 isl_basic_set *bset1, *bset2;
749 test_gist_case(ctx, "gist1");
751 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
752 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
753 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
754 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
755 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
756 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
757 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
758 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
759 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
760 bset1 = isl_basic_set_read_from_str(ctx, str);
761 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
762 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
763 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
764 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
765 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
766 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
767 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
768 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
769 bset2 = isl_basic_set_read_from_str(ctx, str);
770 bset1 = isl_basic_set_gist(bset1, bset2);
771 assert(bset1 && bset1->n_div == 0);
772 isl_basic_set_free(bset1);
775 void test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
779 set = isl_set_read_from_str(ctx, str);
780 set = isl_set_coalesce(set);
781 set2 = isl_set_read_from_str(ctx, str);
782 assert(isl_set_is_equal(set, set2));
784 assert(set && set->n == 1);
789 void test_coalesce(struct isl_ctx *ctx)
792 struct isl_set *set, *set2;
793 struct isl_map *map, *map2;
795 set = isl_set_read_from_str(ctx,
796 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
797 "y >= x & x >= 2 & 5 >= y }");
798 set = isl_set_coalesce(set);
799 assert(set && set->n == 1);
802 set = isl_set_read_from_str(ctx,
803 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
804 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}");
805 set = isl_set_coalesce(set);
806 assert(set && set->n == 1);
809 set = isl_set_read_from_str(ctx,
810 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
811 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}");
812 set = isl_set_coalesce(set);
813 assert(set && set->n == 2);
816 set = isl_set_read_from_str(ctx,
817 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
818 "y >= 0 & x >= 6 & x <= 10 & y <= x}");
819 set = isl_set_coalesce(set);
820 assert(set && set->n == 1);
823 set = isl_set_read_from_str(ctx,
824 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
825 "y >= 0 & x >= 7 & x <= 10 & y <= x}");
826 set = isl_set_coalesce(set);
827 assert(set && set->n == 2);
830 set = isl_set_read_from_str(ctx,
831 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
832 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}");
833 set = isl_set_coalesce(set);
834 assert(set && set->n == 2);
837 set = isl_set_read_from_str(ctx,
838 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
839 "y >= 0 & x = 6 & y <= 6}");
840 set = isl_set_coalesce(set);
841 assert(set && set->n == 1);
844 set = isl_set_read_from_str(ctx,
845 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
846 "y >= 0 & x = 7 & y <= 6}");
847 set = isl_set_coalesce(set);
848 assert(set && set->n == 2);
851 set = isl_set_read_from_str(ctx,
852 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
853 "y >= 0 & x = 6 & y <= 5}");
854 set = isl_set_coalesce(set);
855 assert(set && set->n == 1);
856 set2 = isl_set_read_from_str(ctx,
857 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
858 "y >= 0 & x = 6 & y <= 5}");
859 assert(isl_set_is_equal(set, set2));
863 set = isl_set_read_from_str(ctx,
864 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
865 "y >= 0 & x = 6 & y <= 7}");
866 set = isl_set_coalesce(set);
867 assert(set && set->n == 2);
870 set = isl_set_read_from_str(ctx,
871 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
872 set = isl_set_coalesce(set);
873 assert(set && set->n == 1);
874 set2 = isl_set_read_from_str(ctx,
875 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
876 assert(isl_set_is_equal(set, set2));
880 set = isl_set_read_from_str(ctx,
881 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
882 set = isl_set_coalesce(set);
883 set2 = isl_set_read_from_str(ctx,
884 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
885 assert(isl_set_is_equal(set, set2));
889 set = isl_set_read_from_str(ctx,
890 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
891 "2 <= i and i <= n }");
892 set = isl_set_coalesce(set);
893 assert(set && set->n == 1);
894 set2 = isl_set_read_from_str(ctx,
895 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
896 "2 <= i and i <= n }");
897 assert(isl_set_is_equal(set, set2));
901 map = isl_map_read_from_str(ctx,
902 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
903 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
904 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
905 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
906 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
907 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
908 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
909 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
910 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
911 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
912 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
913 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
914 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
915 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
916 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
917 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
918 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
919 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
920 map = isl_map_coalesce(map);
921 map2 = isl_map_read_from_str(ctx,
922 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
923 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
924 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
925 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
926 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
927 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
928 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
929 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
930 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
931 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
932 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
933 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
934 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
935 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
936 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
937 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
938 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
939 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
940 assert(isl_map_is_equal(map, map2));
944 str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
945 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
946 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
947 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
948 map = isl_map_read_from_str(ctx, str);
949 map = isl_map_coalesce(map);
950 map2 = isl_map_read_from_str(ctx, str);
951 assert(isl_map_is_equal(map, map2));
955 str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
956 "[o0, o1, o2, o3, o4, o5, o6] : "
957 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
958 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
959 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
960 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
961 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
962 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
963 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
964 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
965 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
966 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
967 "o6 >= i3 + i6 - o3 and M >= 0 and "
968 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
969 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
970 map = isl_map_read_from_str(ctx, str);
971 map = isl_map_coalesce(map);
972 map2 = isl_map_read_from_str(ctx, str);
973 assert(isl_map_is_equal(map, map2));
977 str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
978 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
979 "(o0 = 0 and M >= 2 and N >= 3) or "
980 "(M = 0 and o0 = 0 and N >= 3) }";
981 map = isl_map_read_from_str(ctx, str);
982 map = isl_map_coalesce(map);
983 map2 = isl_map_read_from_str(ctx, str);
984 assert(isl_map_is_equal(map, map2));
988 str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
989 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
990 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
991 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
992 map = isl_map_read_from_str(ctx, str);
993 map = isl_map_coalesce(map);
994 map2 = isl_map_read_from_str(ctx, str);
995 assert(isl_map_is_equal(map, map2));
999 test_coalesce_set(ctx,
1000 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
1001 "(i1 = M and M >= 1) }", 0);
1002 test_coalesce_set(ctx,
1003 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
1004 test_coalesce_set(ctx,
1005 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1006 "(y = 3 and x = 1) }", 1);
1007 test_coalesce_set(ctx,
1008 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1009 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1010 "i1 <= M and i3 <= M and i4 <= M) or "
1011 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1012 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1013 "i4 <= -1 + M) }", 1);
1014 test_coalesce_set(ctx,
1015 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1016 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
1017 test_coalesce_set(ctx,
1018 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1019 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1020 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1021 "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
1022 test_coalesce_set(ctx,
1023 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1);
1024 test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0);
1025 test_coalesce_set(ctx,
1026 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1);
1027 test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1);
1028 test_coalesce_set(ctx, "{ [0,0]; [i,i] : 1 <= i <= 10 }", 1);
1029 test_coalesce_set(ctx, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }", 0);
1030 test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 1 <= i <= 10 }", 1);
1031 test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 2 <= i <= 10 }", 0);
1032 test_coalesce_set(ctx, "{ [1,0]; [i,2i] : 1 <= i <= 10 }", 0);
1033 test_coalesce_set(ctx, "{ [0,1]; [i,2i] : 1 <= i <= 10 }", 0);
1036 void test_closure(struct isl_ctx *ctx)
1040 isl_map *up, *right;
1041 isl_map *map, *map2;
1044 /* COCOA example 1 */
1045 map = isl_map_read_from_str(ctx,
1046 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1047 "1 <= i and i < n and 1 <= j and j < n or "
1048 "i2 = i + 1 and j2 = j - 1 and "
1049 "1 <= i and i < n and 2 <= j and j <= n }");
1050 map = isl_map_power(map, &exact);
1054 /* COCOA example 1 */
1055 map = isl_map_read_from_str(ctx,
1056 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1057 "1 <= i and i < n and 1 <= j and j < n or "
1058 "i2 = i + 1 and j2 = j - 1 and "
1059 "1 <= i and i < n and 2 <= j and j <= n }");
1060 map = isl_map_transitive_closure(map, &exact);
1062 map2 = isl_map_read_from_str(ctx,
1063 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1064 "1 <= i and i < n and 1 <= j and j <= n and "
1065 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1066 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1067 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1068 assert(isl_map_is_equal(map, map2));
1072 map = isl_map_read_from_str(ctx,
1073 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1074 " 0 <= y and y <= n }");
1075 map = isl_map_transitive_closure(map, &exact);
1076 map2 = isl_map_read_from_str(ctx,
1077 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1078 " 0 <= y and y <= n }");
1079 assert(isl_map_is_equal(map, map2));
1083 /* COCOA example 2 */
1084 map = isl_map_read_from_str(ctx,
1085 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1086 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1087 "i2 = i + 2 and j2 = j - 2 and "
1088 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1089 map = isl_map_transitive_closure(map, &exact);
1091 map2 = isl_map_read_from_str(ctx,
1092 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1093 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1094 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1095 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1096 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1097 assert(isl_map_is_equal(map, map2));
1101 /* COCOA Fig.2 left */
1102 map = isl_map_read_from_str(ctx,
1103 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1104 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1106 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1107 "j <= 2 i - 3 and j <= n - 2 or "
1108 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1109 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1110 map = isl_map_transitive_closure(map, &exact);
1114 /* COCOA Fig.2 right */
1115 map = isl_map_read_from_str(ctx,
1116 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1117 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1119 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1120 "j <= 2 i - 4 and j <= n - 3 or "
1121 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1122 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1123 map = isl_map_power(map, &exact);
1127 /* COCOA Fig.2 right */
1128 map = isl_map_read_from_str(ctx,
1129 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1130 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1132 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1133 "j <= 2 i - 4 and j <= n - 3 or "
1134 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1135 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1136 map = isl_map_transitive_closure(map, &exact);
1138 map2 = isl_map_read_from_str(ctx,
1139 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1140 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1141 "j <= n and 3 + i + 2 j <= 3 n and "
1142 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1143 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1144 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1145 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1146 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1147 assert(isl_map_is_equal(map, map2));
1151 /* COCOA Fig.1 right */
1152 dom = isl_set_read_from_str(ctx,
1153 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1154 "2 x - 3 y + 3 >= 0 }");
1155 right = isl_map_read_from_str(ctx,
1156 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1157 up = isl_map_read_from_str(ctx,
1158 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1159 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1160 right = isl_map_intersect_range(right, isl_set_copy(dom));
1161 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1162 up = isl_map_intersect_range(up, dom);
1163 map = isl_map_union(up, right);
1164 map = isl_map_transitive_closure(map, &exact);
1166 map2 = isl_map_read_from_str(ctx,
1167 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1168 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1169 assert(isl_map_is_equal(map, map2));
1173 /* COCOA Theorem 1 counter example */
1174 map = isl_map_read_from_str(ctx,
1175 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1176 "i2 = 1 and j2 = j or "
1177 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1178 map = isl_map_transitive_closure(map, &exact);
1182 map = isl_map_read_from_str(ctx,
1183 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1184 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1185 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1186 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1187 map = isl_map_transitive_closure(map, &exact);
1191 /* Kelly et al 1996, fig 12 */
1192 map = isl_map_read_from_str(ctx,
1193 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1194 "1 <= i,j,j+1 <= n or "
1195 "j = n and j2 = 1 and i2 = i + 1 and "
1196 "1 <= i,i+1 <= n }");
1197 map = isl_map_transitive_closure(map, &exact);
1199 map2 = isl_map_read_from_str(ctx,
1200 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1201 "1 <= i <= n and i = i2 or "
1202 "1 <= i < i2 <= n and 1 <= j <= n and "
1204 assert(isl_map_is_equal(map, map2));
1208 /* Omega's closure4 */
1209 map = isl_map_read_from_str(ctx,
1210 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1211 "1 <= x,y <= 10 or "
1212 "x2 = x + 1 and y2 = y and "
1213 "1 <= x <= 20 && 5 <= y <= 15 }");
1214 map = isl_map_transitive_closure(map, &exact);
1218 map = isl_map_read_from_str(ctx,
1219 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1220 map = isl_map_transitive_closure(map, &exact);
1222 map2 = isl_map_read_from_str(ctx,
1223 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1224 assert(isl_map_is_equal(map, map2));
1228 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1229 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1230 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1231 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1232 map = isl_map_read_from_str(ctx, str);
1233 map = isl_map_transitive_closure(map, &exact);
1235 map2 = isl_map_read_from_str(ctx, str);
1236 assert(isl_map_is_equal(map, map2));
1240 str = "{[0] -> [1]; [2] -> [3]}";
1241 map = isl_map_read_from_str(ctx, str);
1242 map = isl_map_transitive_closure(map, &exact);
1244 map2 = isl_map_read_from_str(ctx, str);
1245 assert(isl_map_is_equal(map, map2));
1249 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1250 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1251 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1252 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1253 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1254 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1255 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1256 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1257 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1258 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1259 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1260 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1261 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1262 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1263 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1264 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1265 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1266 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1267 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1268 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1269 map = isl_map_read_from_str(ctx, str);
1270 map = isl_map_transitive_closure(map, NULL);
1275 void test_lex(struct isl_ctx *ctx)
1280 dim = isl_space_set_alloc(ctx, 0, 0);
1281 map = isl_map_lex_le(dim);
1282 assert(!isl_map_is_empty(map));
1286 void test_lexmin(struct isl_ctx *ctx)
1289 isl_basic_map *bmap;
1290 isl_map *map, *map2;
1293 isl_pw_multi_aff *pma;
1295 str = "[p0, p1] -> { [] -> [] : "
1296 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1297 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1298 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1299 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1300 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1301 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1302 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1303 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1304 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1305 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1306 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1307 map = isl_map_read_from_str(ctx, str);
1308 map = isl_map_lexmin(map);
1311 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1312 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1313 set = isl_set_read_from_str(ctx, str);
1314 set = isl_set_lexmax(set);
1315 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1316 set2 = isl_set_read_from_str(ctx, str);
1317 set = isl_set_intersect(set, set2);
1318 assert(!isl_set_is_empty(set));
1321 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1322 map = isl_map_read_from_str(ctx, str);
1323 map = isl_map_lexmin(map);
1324 str = "{ [x] -> [5] : 6 <= x <= 8; "
1325 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1326 map2 = isl_map_read_from_str(ctx, str);
1327 assert(isl_map_is_equal(map, map2));
1331 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1332 map = isl_map_read_from_str(ctx, str);
1333 map2 = isl_map_copy(map);
1334 map = isl_map_lexmin(map);
1335 assert(isl_map_is_equal(map, map2));
1339 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1340 map = isl_map_read_from_str(ctx, str);
1341 map = isl_map_lexmin(map);
1342 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1343 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1344 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1345 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1346 map2 = isl_map_read_from_str(ctx, str);
1347 assert(isl_map_is_equal(map, map2));
1351 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1352 " 8i' <= i and 8i' >= -7 + i }";
1353 bmap = isl_basic_map_read_from_str(ctx, str);
1354 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1355 map2 = isl_map_from_pw_multi_aff(pma);
1356 map = isl_map_from_basic_map(bmap);
1357 assert(isl_map_is_equal(map, map2));
1361 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1362 map = isl_map_read_from_str(ctx, str);
1363 map = isl_map_lexmin(map);
1364 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1365 map2 = isl_map_read_from_str(ctx, str);
1366 assert(isl_map_is_equal(map, map2));
1376 static int collect_must_may(__isl_take isl_map *dep, int must,
1377 void *dep_user, void *user)
1379 struct must_may *mm = (struct must_may *)user;
1382 mm->must = isl_map_union(mm->must, dep);
1384 mm->may = isl_map_union(mm->may, dep);
1389 static int common_space(void *first, void *second)
1391 int depth = *(int *)first;
1395 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1403 map2 = isl_map_read_from_str(map->ctx, str);
1404 equal = isl_map_is_equal(map, map2);
1410 void test_dep(struct isl_ctx *ctx)
1415 isl_access_info *ai;
1422 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1423 map = isl_map_read_from_str(ctx, str);
1424 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1426 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1427 map = isl_map_read_from_str(ctx, str);
1428 ai = isl_access_info_add_source(ai, map, 1, &depth);
1430 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1431 map = isl_map_read_from_str(ctx, str);
1432 ai = isl_access_info_add_source(ai, map, 1, &depth);
1434 flow = isl_access_info_compute_flow(ai);
1435 dim = isl_space_alloc(ctx, 0, 3, 3);
1436 mm.must = isl_map_empty(isl_space_copy(dim));
1437 mm.may = isl_map_empty(dim);
1439 isl_flow_foreach(flow, collect_must_may, &mm);
1441 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1442 " [1,10,0] -> [2,5,0] }";
1443 assert(map_is_equal(mm.must, str));
1444 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1445 assert(map_is_equal(mm.may, str));
1447 isl_map_free(mm.must);
1448 isl_map_free(mm.may);
1449 isl_flow_free(flow);
1452 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1453 map = isl_map_read_from_str(ctx, str);
1454 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1456 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1457 map = isl_map_read_from_str(ctx, str);
1458 ai = isl_access_info_add_source(ai, map, 1, &depth);
1460 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1461 map = isl_map_read_from_str(ctx, str);
1462 ai = isl_access_info_add_source(ai, map, 0, &depth);
1464 flow = isl_access_info_compute_flow(ai);
1465 dim = isl_space_alloc(ctx, 0, 3, 3);
1466 mm.must = isl_map_empty(isl_space_copy(dim));
1467 mm.may = isl_map_empty(dim);
1469 isl_flow_foreach(flow, collect_must_may, &mm);
1471 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1472 assert(map_is_equal(mm.must, str));
1473 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1474 assert(map_is_equal(mm.may, str));
1476 isl_map_free(mm.must);
1477 isl_map_free(mm.may);
1478 isl_flow_free(flow);
1481 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1482 map = isl_map_read_from_str(ctx, str);
1483 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1485 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1486 map = isl_map_read_from_str(ctx, str);
1487 ai = isl_access_info_add_source(ai, map, 0, &depth);
1489 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1490 map = isl_map_read_from_str(ctx, str);
1491 ai = isl_access_info_add_source(ai, map, 0, &depth);
1493 flow = isl_access_info_compute_flow(ai);
1494 dim = isl_space_alloc(ctx, 0, 3, 3);
1495 mm.must = isl_map_empty(isl_space_copy(dim));
1496 mm.may = isl_map_empty(dim);
1498 isl_flow_foreach(flow, collect_must_may, &mm);
1500 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1501 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1502 assert(map_is_equal(mm.may, str));
1503 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1504 assert(map_is_equal(mm.must, str));
1506 isl_map_free(mm.must);
1507 isl_map_free(mm.may);
1508 isl_flow_free(flow);
1511 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1512 map = isl_map_read_from_str(ctx, str);
1513 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1515 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1516 map = isl_map_read_from_str(ctx, str);
1517 ai = isl_access_info_add_source(ai, map, 0, &depth);
1519 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1520 map = isl_map_read_from_str(ctx, str);
1521 ai = isl_access_info_add_source(ai, map, 0, &depth);
1523 flow = isl_access_info_compute_flow(ai);
1524 dim = isl_space_alloc(ctx, 0, 3, 3);
1525 mm.must = isl_map_empty(isl_space_copy(dim));
1526 mm.may = isl_map_empty(dim);
1528 isl_flow_foreach(flow, collect_must_may, &mm);
1530 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1531 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1532 assert(map_is_equal(mm.may, str));
1533 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1534 assert(map_is_equal(mm.must, str));
1536 isl_map_free(mm.must);
1537 isl_map_free(mm.may);
1538 isl_flow_free(flow);
1541 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1542 map = isl_map_read_from_str(ctx, str);
1543 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1545 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1546 map = isl_map_read_from_str(ctx, str);
1547 ai = isl_access_info_add_source(ai, map, 0, &depth);
1549 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1550 map = isl_map_read_from_str(ctx, str);
1551 ai = isl_access_info_add_source(ai, map, 0, &depth);
1553 flow = isl_access_info_compute_flow(ai);
1554 dim = isl_space_alloc(ctx, 0, 3, 3);
1555 mm.must = isl_map_empty(isl_space_copy(dim));
1556 mm.may = isl_map_empty(dim);
1558 isl_flow_foreach(flow, collect_must_may, &mm);
1560 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1561 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1562 assert(map_is_equal(mm.may, str));
1563 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1564 assert(map_is_equal(mm.must, str));
1566 isl_map_free(mm.must);
1567 isl_map_free(mm.may);
1568 isl_flow_free(flow);
1573 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1574 map = isl_map_read_from_str(ctx, str);
1575 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1577 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1578 map = isl_map_read_from_str(ctx, str);
1579 ai = isl_access_info_add_source(ai, map, 1, &depth);
1581 flow = isl_access_info_compute_flow(ai);
1582 dim = isl_space_alloc(ctx, 0, 5, 5);
1583 mm.must = isl_map_empty(isl_space_copy(dim));
1584 mm.may = isl_map_empty(dim);
1586 isl_flow_foreach(flow, collect_must_may, &mm);
1588 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1589 assert(map_is_equal(mm.must, str));
1590 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1591 assert(map_is_equal(mm.may, str));
1593 isl_map_free(mm.must);
1594 isl_map_free(mm.may);
1595 isl_flow_free(flow);
1598 int test_sv(isl_ctx *ctx)
1602 isl_union_map *umap;
1605 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1606 map = isl_map_read_from_str(ctx, str);
1607 sv = isl_map_is_single_valued(map);
1612 isl_die(ctx, isl_error_internal,
1613 "map not detected as single valued", return -1);
1615 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1616 map = isl_map_read_from_str(ctx, str);
1617 sv = isl_map_is_single_valued(map);
1622 isl_die(ctx, isl_error_internal,
1623 "map detected as single valued", return -1);
1625 str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
1626 umap = isl_union_map_read_from_str(ctx, str);
1627 sv = isl_union_map_is_single_valued(umap);
1628 isl_union_map_free(umap);
1632 isl_die(ctx, isl_error_internal,
1633 "map not detected as single valued", return -1);
1635 str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
1636 umap = isl_union_map_read_from_str(ctx, str);
1637 sv = isl_union_map_is_single_valued(umap);
1638 isl_union_map_free(umap);
1642 isl_die(ctx, isl_error_internal,
1643 "map detected as single valued", return -1);
1648 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1652 map = isl_map_read_from_str(ctx, str);
1654 assert(isl_map_is_bijective(map));
1656 assert(!isl_map_is_bijective(map));
1660 void test_bijective(struct isl_ctx *ctx)
1662 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1663 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1664 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1665 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1666 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1667 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1668 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1669 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1670 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1671 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1672 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1673 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1674 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1677 void test_pwqp(struct isl_ctx *ctx)
1681 isl_pw_qpolynomial *pwqp1, *pwqp2;
1683 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1684 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1686 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1689 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1690 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1692 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1694 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1696 isl_pw_qpolynomial_free(pwqp1);
1698 str = "{ [i] -> i }";
1699 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1700 str = "{ [k] : exists a : k = 2a }";
1701 set = isl_set_read_from_str(ctx, str);
1702 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1703 str = "{ [i] -> i }";
1704 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1706 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1708 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1710 isl_pw_qpolynomial_free(pwqp1);
1712 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1713 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1715 set = isl_set_read_from_str(ctx, str);
1716 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1717 str = "{ [i] -> 16 }";
1718 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1720 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1722 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1724 isl_pw_qpolynomial_free(pwqp1);
1726 str = "{ [i] -> ([(i)/2]) }";
1727 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1728 str = "{ [k] : exists a : k = 2a+1 }";
1729 set = isl_set_read_from_str(ctx, str);
1730 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1731 str = "{ [i] -> -1/2 + 1/2 * i }";
1732 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1734 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1736 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1738 isl_pw_qpolynomial_free(pwqp1);
1740 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1741 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1742 str = "{ [i] -> ([(2 * [i/2])/5]) }";
1743 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1745 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1747 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1749 isl_pw_qpolynomial_free(pwqp1);
1751 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1752 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1753 str = "{ [x] -> x }";
1754 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1756 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1758 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1760 isl_pw_qpolynomial_free(pwqp1);
1762 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
1763 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1764 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1765 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
1766 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1767 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1768 isl_pw_qpolynomial_free(pwqp1);
1771 void test_split_periods(isl_ctx *ctx)
1774 isl_pw_qpolynomial *pwqp;
1776 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1777 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
1778 "U >= 0; [U,V] -> U^2 : U >= 100 }";
1779 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1781 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1784 isl_pw_qpolynomial_free(pwqp);
1787 void test_union(isl_ctx *ctx)
1790 isl_union_set *uset1, *uset2;
1791 isl_union_map *umap1, *umap2;
1793 str = "{ [i] : 0 <= i <= 1 }";
1794 uset1 = isl_union_set_read_from_str(ctx, str);
1795 str = "{ [1] -> [0] }";
1796 umap1 = isl_union_map_read_from_str(ctx, str);
1798 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1799 assert(isl_union_map_is_equal(umap1, umap2));
1801 isl_union_map_free(umap1);
1802 isl_union_map_free(umap2);
1804 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1805 umap1 = isl_union_map_read_from_str(ctx, str);
1806 str = "{ A[i]; B[i] }";
1807 uset1 = isl_union_set_read_from_str(ctx, str);
1809 uset2 = isl_union_map_domain(umap1);
1811 assert(isl_union_set_is_equal(uset1, uset2));
1813 isl_union_set_free(uset1);
1814 isl_union_set_free(uset2);
1817 void test_bound(isl_ctx *ctx)
1820 isl_pw_qpolynomial *pwqp;
1821 isl_pw_qpolynomial_fold *pwf;
1823 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1824 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1825 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1826 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
1827 isl_pw_qpolynomial_fold_free(pwf);
1829 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
1830 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1831 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1832 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
1833 isl_pw_qpolynomial_fold_free(pwf);
1836 void test_lift(isl_ctx *ctx)
1839 isl_basic_map *bmap;
1840 isl_basic_set *bset;
1842 str = "{ [i0] : exists e0 : i0 = 4e0 }";
1843 bset = isl_basic_set_read_from_str(ctx, str);
1844 bset = isl_basic_set_lift(bset);
1845 bmap = isl_basic_map_from_range(bset);
1846 bset = isl_basic_map_domain(bmap);
1847 isl_basic_set_free(bset);
1850 void test_subset(isl_ctx *ctx)
1853 isl_set *set1, *set2;
1855 str = "{ [112, 0] }";
1856 set1 = isl_set_read_from_str(ctx, str);
1857 str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1858 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1859 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
1860 set2 = isl_set_read_from_str(ctx, str);
1861 assert(isl_set_is_subset(set1, set2));
1866 void test_factorize(isl_ctx *ctx)
1869 isl_basic_set *bset;
1872 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
1873 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
1874 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
1875 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
1876 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
1877 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
1878 "3i5 >= -2i0 - i2 + 3i4 }";
1879 bset = isl_basic_set_read_from_str(ctx, str);
1880 f = isl_basic_set_factorizer(bset);
1882 isl_basic_set_free(bset);
1883 isl_factorizer_free(f);
1885 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1886 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
1887 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
1888 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
1889 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
1890 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
1891 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
1892 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
1893 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
1894 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
1895 bset = isl_basic_set_read_from_str(ctx, str);
1896 f = isl_basic_set_factorizer(bset);
1898 isl_basic_set_free(bset);
1899 isl_factorizer_free(f);
1902 static int check_injective(__isl_take isl_map *map, void *user)
1904 int *injective = user;
1906 *injective = isl_map_is_injective(map);
1909 if (*injective < 0 || !*injective)
1915 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
1916 const char *r, const char *s, int tilable, int parallel)
1920 isl_union_map *W, *R, *S;
1921 isl_union_map *empty;
1922 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
1923 isl_union_map *validity, *proximity;
1924 isl_union_map *schedule;
1925 isl_union_map *test;
1926 isl_union_set *delta;
1927 isl_union_set *domain;
1931 isl_schedule *sched;
1932 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
1934 D = isl_union_set_read_from_str(ctx, d);
1935 W = isl_union_map_read_from_str(ctx, w);
1936 R = isl_union_map_read_from_str(ctx, r);
1937 S = isl_union_map_read_from_str(ctx, s);
1939 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
1940 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
1942 empty = isl_union_map_empty(isl_union_map_get_space(S));
1943 isl_union_map_compute_flow(isl_union_map_copy(R),
1944 isl_union_map_copy(W), empty,
1945 isl_union_map_copy(S),
1946 &dep_raw, NULL, NULL, NULL);
1947 isl_union_map_compute_flow(isl_union_map_copy(W),
1948 isl_union_map_copy(W),
1949 isl_union_map_copy(R),
1950 isl_union_map_copy(S),
1951 &dep_waw, &dep_war, NULL, NULL);
1953 dep = isl_union_map_union(dep_waw, dep_war);
1954 dep = isl_union_map_union(dep, dep_raw);
1955 validity = isl_union_map_copy(dep);
1956 proximity = isl_union_map_copy(dep);
1958 sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
1959 validity, proximity);
1960 schedule = isl_schedule_get_map(sched);
1961 isl_schedule_free(sched);
1962 isl_union_map_free(W);
1963 isl_union_map_free(R);
1964 isl_union_map_free(S);
1967 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
1969 domain = isl_union_map_domain(isl_union_map_copy(schedule));
1970 is_complete = isl_union_set_is_subset(D, domain);
1971 isl_union_set_free(D);
1972 isl_union_set_free(domain);
1974 test = isl_union_map_reverse(isl_union_map_copy(schedule));
1975 test = isl_union_map_apply_range(test, dep);
1976 test = isl_union_map_apply_range(test, schedule);
1978 delta = isl_union_map_deltas(test);
1979 if (isl_union_set_n_set(delta) == 0) {
1983 isl_union_set_free(delta);
1985 delta_set = isl_set_from_union_set(delta);
1987 slice = isl_set_universe(isl_set_get_space(delta_set));
1988 for (i = 0; i < tilable; ++i)
1989 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
1990 is_tilable = isl_set_is_subset(delta_set, slice);
1991 isl_set_free(slice);
1993 slice = isl_set_universe(isl_set_get_space(delta_set));
1994 for (i = 0; i < parallel; ++i)
1995 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
1996 is_parallel = isl_set_is_subset(delta_set, slice);
1997 isl_set_free(slice);
1999 origin = isl_set_universe(isl_set_get_space(delta_set));
2000 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2001 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2003 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2004 delta_set = isl_set_lexmin(delta_set);
2006 is_nonneg = isl_set_is_equal(delta_set, origin);
2008 isl_set_free(origin);
2009 isl_set_free(delta_set);
2012 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2013 is_injection < 0 || is_complete < 0)
2016 isl_die(ctx, isl_error_unknown,
2017 "generated schedule incomplete", return -1);
2019 isl_die(ctx, isl_error_unknown,
2020 "generated schedule not injective on each statement",
2023 isl_die(ctx, isl_error_unknown,
2024 "negative dependences in generated schedule",
2027 isl_die(ctx, isl_error_unknown,
2028 "generated schedule not as tilable as expected",
2031 isl_die(ctx, isl_error_unknown,
2032 "generated schedule not as parallel as expected",
2038 int test_special_schedule(isl_ctx *ctx)
2042 isl_union_map *empty;
2044 isl_union_map *sched1, *sched2;
2045 isl_schedule *schedule;
2048 str = "{ S[i,j] : 0 <= i <= 10 }";
2049 dom = isl_union_set_read_from_str(ctx, str);
2050 str = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2051 dep = isl_union_map_read_from_str(ctx, str);
2052 empty = isl_union_map_read_from_str(ctx, "{}");
2053 schedule = isl_union_set_compute_schedule(dom, empty, dep);
2054 sched1 = isl_schedule_get_map(schedule);
2055 isl_schedule_free(schedule);
2057 str = "{ S[i, j] -> [j, i] }";
2058 sched2 = isl_union_map_read_from_str(ctx, str);
2060 equal = isl_union_map_is_equal(sched1, sched2);
2061 isl_union_map_free(sched1);
2062 isl_union_map_free(sched2);
2067 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2073 int test_schedule(isl_ctx *ctx)
2075 const char *D, *W, *R, *S;
2078 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2079 W = "{ S1[t,i] -> a[t,i] }";
2080 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2081 "S1[t,i] -> a[t-1,i+1] }";
2082 S = "{ S1[t,i] -> [t,i] }";
2083 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2086 /* Fig. 5 of CC2008 */
2087 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2089 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2090 "j >= 2 and j <= -1 + N }";
2091 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2092 "j >= 2 and j <= -1 + N; "
2093 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2094 "j >= 2 and j <= -1 + N }";
2095 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2096 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2099 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2100 W = "{ S1[i] -> a[i] }";
2101 R = "{ S2[i] -> a[i+1] }";
2102 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2103 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2106 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2107 W = "{ S1[i] -> a[i] }";
2108 R = "{ S2[i] -> a[9-i] }";
2109 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2110 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2113 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2114 W = "{ S1[i] -> a[i] }";
2115 R = "[N] -> { S2[i] -> a[N-1-i] }";
2116 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2117 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2120 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2121 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2122 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2123 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2124 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2127 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2128 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2129 R = "{ S2[i,j] -> a[i-1,j] }";
2130 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2131 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2134 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2135 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2136 R = "{ S2[i,j] -> a[i,j-1] }";
2137 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2138 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2141 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2142 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2143 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2144 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2145 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2146 "S_0[] -> [0, 0, 0] }";
2147 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2149 ctx->opt->schedule_parametric = 0;
2150 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2152 ctx->opt->schedule_parametric = 1;
2154 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2155 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2156 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2157 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2158 "S4[i] -> a[i,N] }";
2159 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2160 "S4[i] -> [4,i,0] }";
2161 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2164 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2165 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2167 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2169 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2171 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2172 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2175 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2176 " S_2[t] : t >= 0 and t <= -1 + N; "
2177 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2179 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2180 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2181 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2182 "i >= 0 and i <= -1 + N }";
2183 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2184 "i >= 0 and i <= -1 + N; "
2185 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2186 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2187 " S_0[t] -> [0, t, 0] }";
2189 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2191 ctx->opt->schedule_parametric = 0;
2192 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2194 ctx->opt->schedule_parametric = 1;
2196 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2197 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2198 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2201 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2202 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2203 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2204 "j >= 0 and j <= -1 + N; "
2205 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2206 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2207 "j >= 0 and j <= -1 + N; "
2208 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2209 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2210 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2213 D = "{ S_0[i] : i >= 0 }";
2214 W = "{ S_0[i] -> a[i] : i >= 0 }";
2215 R = "{ S_0[i] -> a[0] : i >= 0 }";
2216 S = "{ S_0[i] -> [0, i, 0] }";
2217 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2220 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2221 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2222 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2223 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2224 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2227 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2228 "k <= -1 + n and k >= 0 }";
2229 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
2230 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2231 "k <= -1 + n and k >= 0; "
2232 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2233 "k <= -1 + n and k >= 0; "
2234 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2235 "k <= -1 + n and k >= 0 }";
2236 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2237 ctx->opt->schedule_outer_zero_distance = 1;
2238 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2240 ctx->opt->schedule_outer_zero_distance = 0;
2242 return test_special_schedule(ctx);
2245 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2247 isl_union_map *umap;
2250 umap = isl_union_map_read_from_str(ctx, str);
2251 test = isl_union_map_plain_is_injective(umap);
2252 isl_union_map_free(umap);
2255 if (test == injective)
2258 isl_die(ctx, isl_error_unknown,
2259 "map not detected as injective", return -1);
2261 isl_die(ctx, isl_error_unknown,
2262 "map detected as injective", return -1);
2265 int test_injective(isl_ctx *ctx)
2269 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2271 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2273 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2275 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2277 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2279 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2281 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2283 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2286 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2287 if (test_plain_injective(ctx, str, 1))
2289 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2290 if (test_plain_injective(ctx, str, 0))
2296 int test_aff(isl_ctx *ctx)
2301 isl_local_space *ls;
2305 dim = isl_space_set_alloc(ctx, 0, 1);
2306 ls = isl_local_space_from_space(dim);
2307 aff = isl_aff_zero_on_domain(ls);
2309 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2310 aff = isl_aff_scale_down_ui(aff, 3);
2311 aff = isl_aff_floor(aff);
2312 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2313 aff = isl_aff_scale_down_ui(aff, 2);
2314 aff = isl_aff_floor(aff);
2315 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2318 set = isl_set_read_from_str(ctx, str);
2319 aff = isl_aff_gist(aff, set);
2321 aff = isl_aff_add_constant_si(aff, -16);
2322 zero = isl_aff_plain_is_zero(aff);
2328 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2333 int test_dim_max(isl_ctx *ctx)
2337 isl_set *set1, *set2;
2342 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
2343 set = isl_set_read_from_str(ctx, str);
2344 pwaff = isl_set_dim_max(set, 0);
2345 set1 = isl_set_from_pw_aff(pwaff);
2346 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
2347 set2 = isl_set_read_from_str(ctx, str);
2348 equal = isl_set_is_equal(set1, set2);
2354 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2356 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
2357 set = isl_set_read_from_str(ctx, str);
2358 pwaff = isl_set_dim_max(set, 0);
2359 set1 = isl_set_from_pw_aff(pwaff);
2360 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2361 set2 = isl_set_read_from_str(ctx, str);
2362 equal = isl_set_is_equal(set1, set2);
2368 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2370 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
2371 set = isl_set_read_from_str(ctx, str);
2372 pwaff = isl_set_dim_max(set, 0);
2373 set1 = isl_set_from_pw_aff(pwaff);
2374 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2375 set2 = isl_set_read_from_str(ctx, str);
2376 equal = isl_set_is_equal(set1, set2);
2382 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2384 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
2385 "0 <= i < N and 0 <= j < M }";
2386 map = isl_map_read_from_str(ctx, str);
2387 set = isl_map_range(map);
2389 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
2390 set1 = isl_set_from_pw_aff(pwaff);
2391 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
2392 set2 = isl_set_read_from_str(ctx, str);
2393 equal = isl_set_is_equal(set1, set2);
2397 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
2398 set1 = isl_set_from_pw_aff(pwaff);
2399 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
2400 set2 = isl_set_read_from_str(ctx, str);
2401 if (equal >= 0 && equal)
2402 equal = isl_set_is_equal(set1, set2);
2411 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2416 int test_product(isl_ctx *ctx)
2423 set = isl_set_read_from_str(ctx, str);
2424 set = isl_set_product(set, isl_set_copy(set));
2425 ok = isl_set_is_wrapping(set);
2430 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2435 int test_equal(isl_ctx *ctx)
2438 isl_set *set, *set2;
2442 set = isl_set_read_from_str(ctx, str);
2444 set2 = isl_set_read_from_str(ctx, str);
2445 equal = isl_set_is_equal(set, set2);
2451 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2456 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
2457 enum isl_dim_type type, unsigned pos, int fixed)
2461 test = isl_map_plain_is_fixed(map, type, pos, NULL);
2468 isl_die(ctx, isl_error_unknown,
2469 "map not detected as fixed", return -1);
2471 isl_die(ctx, isl_error_unknown,
2472 "map detected as fixed", return -1);
2475 int test_fixed(isl_ctx *ctx)
2480 str = "{ [i] -> [i] }";
2481 map = isl_map_read_from_str(ctx, str);
2482 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
2484 str = "{ [i] -> [1] }";
2485 map = isl_map_read_from_str(ctx, str);
2486 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2488 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
2489 map = isl_map_read_from_str(ctx, str);
2490 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2492 map = isl_map_read_from_str(ctx, str);
2493 map = isl_map_neg(map);
2494 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2500 int test_union_pw(isl_ctx *ctx)
2504 isl_union_set *uset;
2505 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
2507 str = "{ [x] -> x^2 }";
2508 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
2509 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
2510 uset = isl_union_pw_qpolynomial_domain(upwqp1);
2511 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
2512 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
2513 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
2514 isl_union_pw_qpolynomial_free(upwqp1);
2515 isl_union_pw_qpolynomial_free(upwqp2);
2519 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2526 struct isl_ctx *ctx;
2528 srcdir = getenv("srcdir");
2531 ctx = isl_ctx_alloc();
2532 if (test_fixed(ctx) < 0)
2534 if (test_equal(ctx) < 0)
2536 if (test_product(ctx) < 0)
2538 if (test_dim_max(ctx) < 0)
2540 if (test_aff(ctx) < 0)
2542 if (test_injective(ctx) < 0)
2544 if (test_schedule(ctx) < 0)
2546 if (test_union_pw(ctx) < 0)
2548 test_factorize(ctx);
2553 test_split_periods(ctx);
2557 if (test_sv(ctx) < 0)
2559 test_bijective(ctx);
2563 test_construction(ctx);
2566 test_application(ctx);
2567 test_affine_hull(ctx);
2568 test_convex_hull(ctx);