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 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
157 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
158 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
161 void test_read(struct isl_ctx *ctx)
165 struct isl_basic_set *bset1, *bset2;
166 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
168 filename = get_filename(ctx, "set", "omega");
170 input = fopen(filename, "r");
173 bset1 = isl_basic_set_read_from_file(ctx, input);
174 bset2 = isl_basic_set_read_from_str(ctx, str);
176 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
178 isl_basic_set_free(bset1);
179 isl_basic_set_free(bset2);
185 void test_bounded(struct isl_ctx *ctx)
190 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
191 bounded = isl_set_is_bounded(set);
195 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
196 bounded = isl_set_is_bounded(set);
200 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
201 bounded = isl_set_is_bounded(set);
206 /* Construct the basic set { [i] : 5 <= i <= N } */
207 void test_construction(struct isl_ctx *ctx)
212 struct isl_basic_set *bset;
213 struct isl_constraint *c;
217 dim = isl_space_set_alloc(ctx, 1, 1);
218 bset = isl_basic_set_universe(isl_space_copy(dim));
219 ls = isl_local_space_from_space(dim);
221 c = isl_inequality_alloc(isl_local_space_copy(ls));
222 isl_int_set_si(v, -1);
223 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
224 isl_int_set_si(v, 1);
225 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
226 bset = isl_basic_set_add_constraint(bset, c);
228 c = isl_inequality_alloc(isl_local_space_copy(ls));
229 isl_int_set_si(v, 1);
230 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
231 isl_int_set_si(v, -5);
232 isl_constraint_set_constant(c, v);
233 bset = isl_basic_set_add_constraint(bset, c);
235 isl_local_space_free(ls);
236 isl_basic_set_free(bset);
241 void test_dim(struct isl_ctx *ctx)
244 isl_map *map1, *map2;
246 map1 = isl_map_read_from_str(ctx,
247 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
248 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
249 map2 = isl_map_read_from_str(ctx,
250 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
251 assert(isl_map_is_equal(map1, map2));
254 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
255 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
256 assert(isl_map_is_equal(map1, map2));
261 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
262 map1 = isl_map_read_from_str(ctx, str);
263 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
264 map2 = isl_map_read_from_str(ctx, str);
265 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
266 assert(isl_map_is_equal(map1, map2));
272 void test_div(struct isl_ctx *ctx)
277 struct isl_basic_set *bset;
278 struct isl_constraint *c;
283 dim = isl_space_set_alloc(ctx, 0, 3);
284 bset = isl_basic_set_universe(isl_space_copy(dim));
285 ls = isl_local_space_from_space(dim);
287 c = isl_equality_alloc(isl_local_space_copy(ls));
288 isl_int_set_si(v, -1);
289 isl_constraint_set_constant(c, v);
290 isl_int_set_si(v, 1);
291 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
292 isl_int_set_si(v, 3);
293 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
294 bset = isl_basic_set_add_constraint(bset, c);
296 c = isl_equality_alloc(isl_local_space_copy(ls));
297 isl_int_set_si(v, 1);
298 isl_constraint_set_constant(c, v);
299 isl_int_set_si(v, -1);
300 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
301 isl_int_set_si(v, 3);
302 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
303 bset = isl_basic_set_add_constraint(bset, c);
305 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
307 assert(bset && bset->n_div == 1);
308 isl_local_space_free(ls);
309 isl_basic_set_free(bset);
312 dim = isl_space_set_alloc(ctx, 0, 3);
313 bset = isl_basic_set_universe(isl_space_copy(dim));
314 ls = isl_local_space_from_space(dim);
316 c = isl_equality_alloc(isl_local_space_copy(ls));
317 isl_int_set_si(v, 1);
318 isl_constraint_set_constant(c, v);
319 isl_int_set_si(v, -1);
320 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
321 isl_int_set_si(v, 3);
322 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
323 bset = isl_basic_set_add_constraint(bset, c);
325 c = isl_equality_alloc(isl_local_space_copy(ls));
326 isl_int_set_si(v, -1);
327 isl_constraint_set_constant(c, v);
328 isl_int_set_si(v, 1);
329 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
330 isl_int_set_si(v, 3);
331 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
332 bset = isl_basic_set_add_constraint(bset, c);
334 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
336 assert(bset && bset->n_div == 1);
337 isl_local_space_free(ls);
338 isl_basic_set_free(bset);
341 dim = isl_space_set_alloc(ctx, 0, 3);
342 bset = isl_basic_set_universe(isl_space_copy(dim));
343 ls = isl_local_space_from_space(dim);
345 c = isl_equality_alloc(isl_local_space_copy(ls));
346 isl_int_set_si(v, 1);
347 isl_constraint_set_constant(c, v);
348 isl_int_set_si(v, -1);
349 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
350 isl_int_set_si(v, 3);
351 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
352 bset = isl_basic_set_add_constraint(bset, c);
354 c = isl_equality_alloc(isl_local_space_copy(ls));
355 isl_int_set_si(v, -3);
356 isl_constraint_set_constant(c, v);
357 isl_int_set_si(v, 1);
358 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
359 isl_int_set_si(v, 4);
360 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
361 bset = isl_basic_set_add_constraint(bset, c);
363 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
365 assert(bset && bset->n_div == 1);
366 isl_local_space_free(ls);
367 isl_basic_set_free(bset);
370 dim = isl_space_set_alloc(ctx, 0, 3);
371 bset = isl_basic_set_universe(isl_space_copy(dim));
372 ls = isl_local_space_from_space(dim);
374 c = isl_equality_alloc(isl_local_space_copy(ls));
375 isl_int_set_si(v, 2);
376 isl_constraint_set_constant(c, v);
377 isl_int_set_si(v, -1);
378 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
379 isl_int_set_si(v, 3);
380 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
381 bset = isl_basic_set_add_constraint(bset, c);
383 c = isl_equality_alloc(isl_local_space_copy(ls));
384 isl_int_set_si(v, -1);
385 isl_constraint_set_constant(c, v);
386 isl_int_set_si(v, 1);
387 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
388 isl_int_set_si(v, 6);
389 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
390 bset = isl_basic_set_add_constraint(bset, c);
392 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
394 assert(isl_basic_set_is_empty(bset));
395 isl_local_space_free(ls);
396 isl_basic_set_free(bset);
399 dim = isl_space_set_alloc(ctx, 0, 3);
400 bset = isl_basic_set_universe(isl_space_copy(dim));
401 ls = isl_local_space_from_space(dim);
403 c = isl_equality_alloc(isl_local_space_copy(ls));
404 isl_int_set_si(v, -1);
405 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
406 isl_int_set_si(v, 3);
407 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
408 bset = isl_basic_set_add_constraint(bset, c);
410 c = isl_equality_alloc(isl_local_space_copy(ls));
411 isl_int_set_si(v, 1);
412 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
413 isl_int_set_si(v, -3);
414 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
415 bset = isl_basic_set_add_constraint(bset, c);
417 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
419 assert(bset && bset->n_div == 0);
420 isl_basic_set_free(bset);
421 isl_local_space_free(ls);
424 dim = isl_space_set_alloc(ctx, 0, 3);
425 bset = isl_basic_set_universe(isl_space_copy(dim));
426 ls = isl_local_space_from_space(dim);
428 c = isl_equality_alloc(isl_local_space_copy(ls));
429 isl_int_set_si(v, -1);
430 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
431 isl_int_set_si(v, 6);
432 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
433 bset = isl_basic_set_add_constraint(bset, c);
435 c = isl_equality_alloc(isl_local_space_copy(ls));
436 isl_int_set_si(v, 1);
437 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
438 isl_int_set_si(v, -3);
439 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
440 bset = isl_basic_set_add_constraint(bset, c);
442 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
444 assert(bset && bset->n_div == 1);
445 isl_basic_set_free(bset);
446 isl_local_space_free(ls);
449 /* This test is a bit tricky. We set up an equality
451 * Normalization of divs creates _two_ divs
454 * Afterwards e0 is removed again because it has coefficient -1
455 * and we end up with the original equality and div again.
456 * Perhaps we can avoid the introduction of this temporary div.
458 dim = isl_space_set_alloc(ctx, 0, 4);
459 bset = isl_basic_set_universe(isl_space_copy(dim));
460 ls = isl_local_space_from_space(dim);
462 c = isl_equality_alloc(isl_local_space_copy(ls));
463 isl_int_set_si(v, -1);
464 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
465 isl_int_set_si(v, -3);
466 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
467 isl_int_set_si(v, -3);
468 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
469 isl_int_set_si(v, 6);
470 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
471 bset = isl_basic_set_add_constraint(bset, c);
473 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
475 /* Test disabled for now */
477 assert(bset && bset->n_div == 1);
479 isl_local_space_free(ls);
480 isl_basic_set_free(bset);
483 dim = isl_space_set_alloc(ctx, 0, 5);
484 bset = isl_basic_set_universe(isl_space_copy(dim));
485 ls = isl_local_space_from_space(dim);
487 c = isl_equality_alloc(isl_local_space_copy(ls));
488 isl_int_set_si(v, -1);
489 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
490 isl_int_set_si(v, -3);
491 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
492 isl_int_set_si(v, -3);
493 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
494 isl_int_set_si(v, 6);
495 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
496 bset = isl_basic_set_add_constraint(bset, c);
498 c = isl_equality_alloc(isl_local_space_copy(ls));
499 isl_int_set_si(v, -1);
500 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
501 isl_int_set_si(v, 1);
502 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
503 isl_int_set_si(v, 1);
504 isl_constraint_set_constant(c, v);
505 bset = isl_basic_set_add_constraint(bset, c);
507 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
509 /* Test disabled for now */
511 assert(bset && bset->n_div == 1);
513 isl_local_space_free(ls);
514 isl_basic_set_free(bset);
517 dim = isl_space_set_alloc(ctx, 0, 4);
518 bset = isl_basic_set_universe(isl_space_copy(dim));
519 ls = isl_local_space_from_space(dim);
521 c = isl_equality_alloc(isl_local_space_copy(ls));
522 isl_int_set_si(v, 1);
523 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
524 isl_int_set_si(v, -1);
525 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
526 isl_int_set_si(v, -2);
527 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
528 bset = isl_basic_set_add_constraint(bset, c);
530 c = isl_equality_alloc(isl_local_space_copy(ls));
531 isl_int_set_si(v, -1);
532 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
533 isl_int_set_si(v, 3);
534 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
535 isl_int_set_si(v, 2);
536 isl_constraint_set_constant(c, v);
537 bset = isl_basic_set_add_constraint(bset, c);
539 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
541 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
543 assert(!isl_basic_set_is_empty(bset));
545 isl_local_space_free(ls);
546 isl_basic_set_free(bset);
549 dim = isl_space_set_alloc(ctx, 0, 3);
550 bset = isl_basic_set_universe(isl_space_copy(dim));
551 ls = isl_local_space_from_space(dim);
553 c = isl_equality_alloc(isl_local_space_copy(ls));
554 isl_int_set_si(v, 1);
555 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
556 isl_int_set_si(v, -2);
557 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
558 bset = isl_basic_set_add_constraint(bset, c);
560 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
562 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
564 isl_local_space_free(ls);
565 isl_basic_set_free(bset);
570 void test_application_case(struct isl_ctx *ctx, const char *name)
574 struct isl_basic_set *bset1, *bset2;
575 struct isl_basic_map *bmap;
577 filename = get_filename(ctx, name, "omega");
579 input = fopen(filename, "r");
582 bset1 = isl_basic_set_read_from_file(ctx, input);
583 bmap = isl_basic_map_read_from_file(ctx, input);
585 bset1 = isl_basic_set_apply(bset1, bmap);
587 bset2 = isl_basic_set_read_from_file(ctx, input);
589 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
591 isl_basic_set_free(bset1);
592 isl_basic_set_free(bset2);
598 void test_application(struct isl_ctx *ctx)
600 test_application_case(ctx, "application");
601 test_application_case(ctx, "application2");
604 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
608 struct isl_basic_set *bset1, *bset2;
610 filename = get_filename(ctx, name, "polylib");
612 input = fopen(filename, "r");
615 bset1 = isl_basic_set_read_from_file(ctx, input);
616 bset2 = isl_basic_set_read_from_file(ctx, input);
618 bset1 = isl_basic_set_affine_hull(bset1);
620 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
622 isl_basic_set_free(bset1);
623 isl_basic_set_free(bset2);
629 void test_affine_hull(struct isl_ctx *ctx)
631 test_affine_hull_case(ctx, "affine2");
632 test_affine_hull_case(ctx, "affine");
633 test_affine_hull_case(ctx, "affine3");
636 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
640 struct isl_basic_set *bset1, *bset2;
643 filename = get_filename(ctx, name, "polylib");
645 input = fopen(filename, "r");
648 bset1 = isl_basic_set_read_from_file(ctx, input);
649 bset2 = isl_basic_set_read_from_file(ctx, input);
651 set = isl_basic_set_union(bset1, bset2);
652 bset1 = isl_set_convex_hull(set);
654 bset2 = isl_basic_set_read_from_file(ctx, input);
656 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
658 isl_basic_set_free(bset1);
659 isl_basic_set_free(bset2);
665 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
667 const char *str1, *str2;
668 isl_set *set1, *set2;
669 int orig_convex = ctx->opt->convex;
670 ctx->opt->convex = convex;
672 test_convex_hull_case(ctx, "convex0");
673 test_convex_hull_case(ctx, "convex1");
674 test_convex_hull_case(ctx, "convex2");
675 test_convex_hull_case(ctx, "convex3");
676 test_convex_hull_case(ctx, "convex4");
677 test_convex_hull_case(ctx, "convex5");
678 test_convex_hull_case(ctx, "convex6");
679 test_convex_hull_case(ctx, "convex7");
680 test_convex_hull_case(ctx, "convex8");
681 test_convex_hull_case(ctx, "convex9");
682 test_convex_hull_case(ctx, "convex10");
683 test_convex_hull_case(ctx, "convex11");
684 test_convex_hull_case(ctx, "convex12");
685 test_convex_hull_case(ctx, "convex13");
686 test_convex_hull_case(ctx, "convex14");
687 test_convex_hull_case(ctx, "convex15");
689 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
690 "(i0 = 1 and i1 = 0 and i2 = 1) or "
691 "(i0 = 0 and i1 = 0 and i2 = 0) }";
692 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
693 set1 = isl_set_read_from_str(ctx, str1);
694 set2 = isl_set_read_from_str(ctx, str2);
695 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
696 assert(isl_set_is_equal(set1, set2));
700 ctx->opt->convex = orig_convex;
703 void test_convex_hull(struct isl_ctx *ctx)
705 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
706 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
709 void test_gist_case(struct isl_ctx *ctx, const char *name)
713 struct isl_basic_set *bset1, *bset2;
715 filename = get_filename(ctx, name, "polylib");
717 input = fopen(filename, "r");
720 bset1 = isl_basic_set_read_from_file(ctx, input);
721 bset2 = isl_basic_set_read_from_file(ctx, input);
723 bset1 = isl_basic_set_gist(bset1, bset2);
725 bset2 = isl_basic_set_read_from_file(ctx, input);
727 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
729 isl_basic_set_free(bset1);
730 isl_basic_set_free(bset2);
736 void test_gist(struct isl_ctx *ctx)
739 isl_basic_set *bset1, *bset2;
741 test_gist_case(ctx, "gist1");
743 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
744 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
745 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
746 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
747 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
748 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
749 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
750 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
751 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
752 bset1 = isl_basic_set_read_from_str(ctx, str);
753 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
754 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
755 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
756 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
757 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
758 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
759 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
760 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
761 bset2 = isl_basic_set_read_from_str(ctx, str);
762 bset1 = isl_basic_set_gist(bset1, bset2);
763 assert(bset1 && bset1->n_div == 0);
764 isl_basic_set_free(bset1);
767 void test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
771 set = isl_set_read_from_str(ctx, str);
772 set = isl_set_coalesce(set);
773 set2 = isl_set_read_from_str(ctx, str);
774 assert(isl_set_is_equal(set, set2));
776 assert(set && set->n == 1);
781 void test_coalesce(struct isl_ctx *ctx)
784 struct isl_set *set, *set2;
785 struct isl_map *map, *map2;
787 set = isl_set_read_from_str(ctx,
788 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
789 "y >= x & x >= 2 & 5 >= y }");
790 set = isl_set_coalesce(set);
791 assert(set && set->n == 1);
794 set = isl_set_read_from_str(ctx,
795 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
796 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}");
797 set = isl_set_coalesce(set);
798 assert(set && set->n == 1);
801 set = isl_set_read_from_str(ctx,
802 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
803 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}");
804 set = isl_set_coalesce(set);
805 assert(set && set->n == 2);
808 set = isl_set_read_from_str(ctx,
809 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
810 "y >= 0 & x >= 6 & x <= 10 & y <= x}");
811 set = isl_set_coalesce(set);
812 assert(set && set->n == 1);
815 set = isl_set_read_from_str(ctx,
816 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
817 "y >= 0 & x >= 7 & x <= 10 & y <= x}");
818 set = isl_set_coalesce(set);
819 assert(set && set->n == 2);
822 set = isl_set_read_from_str(ctx,
823 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
824 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}");
825 set = isl_set_coalesce(set);
826 assert(set && set->n == 2);
829 set = isl_set_read_from_str(ctx,
830 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
831 "y >= 0 & x = 6 & y <= 6}");
832 set = isl_set_coalesce(set);
833 assert(set && set->n == 1);
836 set = isl_set_read_from_str(ctx,
837 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
838 "y >= 0 & x = 7 & y <= 6}");
839 set = isl_set_coalesce(set);
840 assert(set && set->n == 2);
843 set = isl_set_read_from_str(ctx,
844 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
845 "y >= 0 & x = 6 & y <= 5}");
846 set = isl_set_coalesce(set);
847 assert(set && set->n == 1);
848 set2 = isl_set_read_from_str(ctx,
849 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
850 "y >= 0 & x = 6 & y <= 5}");
851 assert(isl_set_is_equal(set, set2));
855 set = isl_set_read_from_str(ctx,
856 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
857 "y >= 0 & x = 6 & y <= 7}");
858 set = isl_set_coalesce(set);
859 assert(set && set->n == 2);
862 set = isl_set_read_from_str(ctx,
863 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
864 set = isl_set_coalesce(set);
865 assert(set && set->n == 1);
866 set2 = isl_set_read_from_str(ctx,
867 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
868 assert(isl_set_is_equal(set, set2));
872 set = isl_set_read_from_str(ctx,
873 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
874 set = isl_set_coalesce(set);
875 set2 = isl_set_read_from_str(ctx,
876 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
877 assert(isl_set_is_equal(set, set2));
881 set = isl_set_read_from_str(ctx,
882 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
883 "2 <= i and i <= n }");
884 set = isl_set_coalesce(set);
885 assert(set && set->n == 1);
886 set2 = isl_set_read_from_str(ctx,
887 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
888 "2 <= i and i <= n }");
889 assert(isl_set_is_equal(set, set2));
893 map = isl_map_read_from_str(ctx,
894 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
895 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
896 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
897 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
898 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
899 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
900 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
901 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
902 "[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 2e0 >= 3 + n and e0 <= -4 + n and "
906 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
907 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
908 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
909 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
910 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
911 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
912 map = isl_map_coalesce(map);
913 map2 = isl_map_read_from_str(ctx,
914 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
915 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
916 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
917 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
918 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
919 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
920 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
921 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
922 "[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 2e0 >= 3 + n and e0 <= -4 + n and "
926 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
927 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
928 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
929 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
930 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
931 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
932 assert(isl_map_is_equal(map, map2));
936 str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
937 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
938 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
939 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
940 map = isl_map_read_from_str(ctx, str);
941 map = isl_map_coalesce(map);
942 map2 = isl_map_read_from_str(ctx, str);
943 assert(isl_map_is_equal(map, map2));
947 str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
948 "[o0, o1, o2, o3, o4, o5, o6] : "
949 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
950 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
951 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
952 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
953 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
954 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
955 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
956 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
957 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
958 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
959 "o6 >= i3 + i6 - o3 and M >= 0 and "
960 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
961 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
962 map = isl_map_read_from_str(ctx, str);
963 map = isl_map_coalesce(map);
964 map2 = isl_map_read_from_str(ctx, str);
965 assert(isl_map_is_equal(map, map2));
969 str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
970 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
971 "(o0 = 0 and M >= 2 and N >= 3) or "
972 "(M = 0 and o0 = 0 and N >= 3) }";
973 map = isl_map_read_from_str(ctx, str);
974 map = isl_map_coalesce(map);
975 map2 = isl_map_read_from_str(ctx, str);
976 assert(isl_map_is_equal(map, map2));
980 str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
981 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
982 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
983 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
984 map = isl_map_read_from_str(ctx, str);
985 map = isl_map_coalesce(map);
986 map2 = isl_map_read_from_str(ctx, str);
987 assert(isl_map_is_equal(map, map2));
991 test_coalesce_set(ctx,
992 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
993 "(i1 = M and M >= 1) }", 0);
994 test_coalesce_set(ctx,
995 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
996 test_coalesce_set(ctx,
997 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
998 "(y = 3 and x = 1) }", 1);
999 test_coalesce_set(ctx,
1000 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1001 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1002 "i1 <= M and i3 <= M and i4 <= M) or "
1003 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1004 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1005 "i4 <= -1 + M) }", 1);
1006 test_coalesce_set(ctx,
1007 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1008 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
1009 test_coalesce_set(ctx,
1010 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1011 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1012 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1013 "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
1014 test_coalesce_set(ctx,
1015 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1);
1016 test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0);
1017 test_coalesce_set(ctx,
1018 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1);
1019 test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1);
1020 test_coalesce_set(ctx, "{ [0,0]; [i,i] : 1 <= i <= 10 }", 1);
1021 test_coalesce_set(ctx, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }", 0);
1022 test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 1 <= i <= 10 }", 1);
1023 test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 2 <= i <= 10 }", 0);
1024 test_coalesce_set(ctx, "{ [1,0]; [i,2i] : 1 <= i <= 10 }", 0);
1025 test_coalesce_set(ctx, "{ [0,1]; [i,2i] : 1 <= i <= 10 }", 0);
1028 void test_closure(struct isl_ctx *ctx)
1032 isl_map *up, *right;
1033 isl_map *map, *map2;
1036 /* COCOA example 1 */
1037 map = isl_map_read_from_str(ctx,
1038 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1039 "1 <= i and i < n and 1 <= j and j < n or "
1040 "i2 = i + 1 and j2 = j - 1 and "
1041 "1 <= i and i < n and 2 <= j and j <= n }");
1042 map = isl_map_power(map, &exact);
1046 /* COCOA example 1 */
1047 map = isl_map_read_from_str(ctx,
1048 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1049 "1 <= i and i < n and 1 <= j and j < n or "
1050 "i2 = i + 1 and j2 = j - 1 and "
1051 "1 <= i and i < n and 2 <= j and j <= n }");
1052 map = isl_map_transitive_closure(map, &exact);
1054 map2 = isl_map_read_from_str(ctx,
1055 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1056 "1 <= i and i < n and 1 <= j and j <= n and "
1057 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1058 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1059 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1060 assert(isl_map_is_equal(map, map2));
1064 map = isl_map_read_from_str(ctx,
1065 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1066 " 0 <= y and y <= n }");
1067 map = isl_map_transitive_closure(map, &exact);
1068 map2 = isl_map_read_from_str(ctx,
1069 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1070 " 0 <= y and y <= n }");
1071 assert(isl_map_is_equal(map, map2));
1075 /* COCOA example 2 */
1076 map = isl_map_read_from_str(ctx,
1077 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1078 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1079 "i2 = i + 2 and j2 = j - 2 and "
1080 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1081 map = isl_map_transitive_closure(map, &exact);
1083 map2 = isl_map_read_from_str(ctx,
1084 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1085 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1086 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1087 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1088 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1089 assert(isl_map_is_equal(map, map2));
1093 /* COCOA Fig.2 left */
1094 map = isl_map_read_from_str(ctx,
1095 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1096 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1098 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1099 "j <= 2 i - 3 and j <= n - 2 or "
1100 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1101 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1102 map = isl_map_transitive_closure(map, &exact);
1106 /* COCOA Fig.2 right */
1107 map = isl_map_read_from_str(ctx,
1108 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1109 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1111 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1112 "j <= 2 i - 4 and j <= n - 3 or "
1113 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1114 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1115 map = isl_map_power(map, &exact);
1119 /* COCOA Fig.2 right */
1120 map = isl_map_read_from_str(ctx,
1121 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1122 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1124 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1125 "j <= 2 i - 4 and j <= n - 3 or "
1126 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1127 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1128 map = isl_map_transitive_closure(map, &exact);
1130 map2 = isl_map_read_from_str(ctx,
1131 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1132 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1133 "j <= n and 3 + i + 2 j <= 3 n and "
1134 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1135 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1136 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1137 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1138 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1139 assert(isl_map_is_equal(map, map2));
1143 /* COCOA Fig.1 right */
1144 dom = isl_set_read_from_str(ctx,
1145 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1146 "2 x - 3 y + 3 >= 0 }");
1147 right = isl_map_read_from_str(ctx,
1148 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1149 up = isl_map_read_from_str(ctx,
1150 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1151 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1152 right = isl_map_intersect_range(right, isl_set_copy(dom));
1153 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1154 up = isl_map_intersect_range(up, dom);
1155 map = isl_map_union(up, right);
1156 map = isl_map_transitive_closure(map, &exact);
1158 map2 = isl_map_read_from_str(ctx,
1159 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1160 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1161 assert(isl_map_is_equal(map, map2));
1165 /* COCOA Theorem 1 counter example */
1166 map = isl_map_read_from_str(ctx,
1167 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1168 "i2 = 1 and j2 = j or "
1169 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1170 map = isl_map_transitive_closure(map, &exact);
1174 map = isl_map_read_from_str(ctx,
1175 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1176 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1177 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1178 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1179 map = isl_map_transitive_closure(map, &exact);
1183 /* Kelly et al 1996, fig 12 */
1184 map = isl_map_read_from_str(ctx,
1185 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1186 "1 <= i,j,j+1 <= n or "
1187 "j = n and j2 = 1 and i2 = i + 1 and "
1188 "1 <= i,i+1 <= n }");
1189 map = isl_map_transitive_closure(map, &exact);
1191 map2 = isl_map_read_from_str(ctx,
1192 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1193 "1 <= i <= n and i = i2 or "
1194 "1 <= i < i2 <= n and 1 <= j <= n and "
1196 assert(isl_map_is_equal(map, map2));
1200 /* Omega's closure4 */
1201 map = isl_map_read_from_str(ctx,
1202 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1203 "1 <= x,y <= 10 or "
1204 "x2 = x + 1 and y2 = y and "
1205 "1 <= x <= 20 && 5 <= y <= 15 }");
1206 map = isl_map_transitive_closure(map, &exact);
1210 map = isl_map_read_from_str(ctx,
1211 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1212 map = isl_map_transitive_closure(map, &exact);
1214 map2 = isl_map_read_from_str(ctx,
1215 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1216 assert(isl_map_is_equal(map, map2));
1220 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1221 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1222 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1223 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1224 map = isl_map_read_from_str(ctx, str);
1225 map = isl_map_transitive_closure(map, &exact);
1227 map2 = isl_map_read_from_str(ctx, str);
1228 assert(isl_map_is_equal(map, map2));
1232 str = "{[0] -> [1]; [2] -> [3]}";
1233 map = isl_map_read_from_str(ctx, str);
1234 map = isl_map_transitive_closure(map, &exact);
1236 map2 = isl_map_read_from_str(ctx, str);
1237 assert(isl_map_is_equal(map, map2));
1241 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1242 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1243 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1244 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1245 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1246 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1247 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1248 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1249 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1250 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1251 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1252 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1253 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1254 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1255 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1256 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1257 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1258 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1259 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1260 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1261 map = isl_map_read_from_str(ctx, str);
1262 map = isl_map_transitive_closure(map, NULL);
1267 void test_lex(struct isl_ctx *ctx)
1272 dim = isl_space_set_alloc(ctx, 0, 0);
1273 map = isl_map_lex_le(dim);
1274 assert(!isl_map_is_empty(map));
1278 static int consume_lexmin(__isl_take isl_basic_set *dom,
1279 __isl_take isl_aff_list *list, void *user)
1282 isl_basic_map *bmap;
1283 isl_map **map = user;
1285 dim = isl_basic_set_get_space(dom);
1286 bmap = isl_basic_map_from_aff_list(dim, list);
1287 bmap = isl_basic_map_intersect_domain(bmap, dom);
1289 *map = isl_map_union(*map, isl_map_from_basic_map(bmap));
1294 void test_lexmin(struct isl_ctx *ctx)
1297 isl_basic_map *bmap;
1298 isl_map *map, *map2;
1302 str = "[p0, p1] -> { [] -> [] : "
1303 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1304 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1305 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1306 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1307 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1308 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1309 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1310 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1311 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1312 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1313 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1314 map = isl_map_read_from_str(ctx, str);
1315 map = isl_map_lexmin(map);
1318 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1319 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1320 set = isl_set_read_from_str(ctx, str);
1321 set = isl_set_lexmax(set);
1322 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1323 set2 = isl_set_read_from_str(ctx, str);
1324 set = isl_set_intersect(set, set2);
1325 assert(!isl_set_is_empty(set));
1328 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1329 map = isl_map_read_from_str(ctx, str);
1330 map = isl_map_lexmin(map);
1331 str = "{ [x] -> [5] : 6 <= x <= 8; "
1332 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1333 map2 = isl_map_read_from_str(ctx, str);
1334 assert(isl_map_is_equal(map, map2));
1338 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1339 map = isl_map_read_from_str(ctx, str);
1340 map2 = isl_map_copy(map);
1341 map = isl_map_lexmin(map);
1342 assert(isl_map_is_equal(map, map2));
1346 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1347 map = isl_map_read_from_str(ctx, str);
1348 map = isl_map_lexmin(map);
1349 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1350 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1351 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1352 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1353 map2 = isl_map_read_from_str(ctx, str);
1354 assert(isl_map_is_equal(map, map2));
1358 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1359 " 8i' <= i and 8i' >= -7 + i }";
1360 bmap = isl_basic_map_read_from_str(ctx, str);
1361 map2 = isl_map_empty(isl_basic_map_get_space(bmap));
1362 isl_basic_map_foreach_lexmin(bmap, &consume_lexmin, &map2);
1363 map = isl_map_from_basic_map(bmap);
1364 assert(isl_map_is_equal(map, map2));
1374 static int collect_must_may(__isl_take isl_map *dep, int must,
1375 void *dep_user, void *user)
1377 struct must_may *mm = (struct must_may *)user;
1380 mm->must = isl_map_union(mm->must, dep);
1382 mm->may = isl_map_union(mm->may, dep);
1387 static int common_space(void *first, void *second)
1389 int depth = *(int *)first;
1393 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1401 map2 = isl_map_read_from_str(map->ctx, str);
1402 equal = isl_map_is_equal(map, map2);
1408 void test_dep(struct isl_ctx *ctx)
1413 isl_access_info *ai;
1420 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1421 map = isl_map_read_from_str(ctx, str);
1422 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1424 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1425 map = isl_map_read_from_str(ctx, str);
1426 ai = isl_access_info_add_source(ai, map, 1, &depth);
1428 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1429 map = isl_map_read_from_str(ctx, str);
1430 ai = isl_access_info_add_source(ai, map, 1, &depth);
1432 flow = isl_access_info_compute_flow(ai);
1433 dim = isl_space_alloc(ctx, 0, 3, 3);
1434 mm.must = isl_map_empty(isl_space_copy(dim));
1435 mm.may = isl_map_empty(dim);
1437 isl_flow_foreach(flow, collect_must_may, &mm);
1439 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1440 " [1,10,0] -> [2,5,0] }";
1441 assert(map_is_equal(mm.must, str));
1442 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1443 assert(map_is_equal(mm.may, str));
1445 isl_map_free(mm.must);
1446 isl_map_free(mm.may);
1447 isl_flow_free(flow);
1450 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1451 map = isl_map_read_from_str(ctx, str);
1452 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1454 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1455 map = isl_map_read_from_str(ctx, str);
1456 ai = isl_access_info_add_source(ai, map, 1, &depth);
1458 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1459 map = isl_map_read_from_str(ctx, str);
1460 ai = isl_access_info_add_source(ai, map, 0, &depth);
1462 flow = isl_access_info_compute_flow(ai);
1463 dim = isl_space_alloc(ctx, 0, 3, 3);
1464 mm.must = isl_map_empty(isl_space_copy(dim));
1465 mm.may = isl_map_empty(dim);
1467 isl_flow_foreach(flow, collect_must_may, &mm);
1469 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1470 assert(map_is_equal(mm.must, str));
1471 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1472 assert(map_is_equal(mm.may, str));
1474 isl_map_free(mm.must);
1475 isl_map_free(mm.may);
1476 isl_flow_free(flow);
1479 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1480 map = isl_map_read_from_str(ctx, str);
1481 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1483 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1484 map = isl_map_read_from_str(ctx, str);
1485 ai = isl_access_info_add_source(ai, map, 0, &depth);
1487 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1488 map = isl_map_read_from_str(ctx, str);
1489 ai = isl_access_info_add_source(ai, map, 0, &depth);
1491 flow = isl_access_info_compute_flow(ai);
1492 dim = isl_space_alloc(ctx, 0, 3, 3);
1493 mm.must = isl_map_empty(isl_space_copy(dim));
1494 mm.may = isl_map_empty(dim);
1496 isl_flow_foreach(flow, collect_must_may, &mm);
1498 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1499 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1500 assert(map_is_equal(mm.may, str));
1501 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1502 assert(map_is_equal(mm.must, str));
1504 isl_map_free(mm.must);
1505 isl_map_free(mm.may);
1506 isl_flow_free(flow);
1509 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1510 map = isl_map_read_from_str(ctx, str);
1511 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1513 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1514 map = isl_map_read_from_str(ctx, str);
1515 ai = isl_access_info_add_source(ai, map, 0, &depth);
1517 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1518 map = isl_map_read_from_str(ctx, str);
1519 ai = isl_access_info_add_source(ai, map, 0, &depth);
1521 flow = isl_access_info_compute_flow(ai);
1522 dim = isl_space_alloc(ctx, 0, 3, 3);
1523 mm.must = isl_map_empty(isl_space_copy(dim));
1524 mm.may = isl_map_empty(dim);
1526 isl_flow_foreach(flow, collect_must_may, &mm);
1528 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1529 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1530 assert(map_is_equal(mm.may, str));
1531 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1532 assert(map_is_equal(mm.must, str));
1534 isl_map_free(mm.must);
1535 isl_map_free(mm.may);
1536 isl_flow_free(flow);
1539 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1540 map = isl_map_read_from_str(ctx, str);
1541 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1543 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1544 map = isl_map_read_from_str(ctx, str);
1545 ai = isl_access_info_add_source(ai, map, 0, &depth);
1547 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1548 map = isl_map_read_from_str(ctx, str);
1549 ai = isl_access_info_add_source(ai, map, 0, &depth);
1551 flow = isl_access_info_compute_flow(ai);
1552 dim = isl_space_alloc(ctx, 0, 3, 3);
1553 mm.must = isl_map_empty(isl_space_copy(dim));
1554 mm.may = isl_map_empty(dim);
1556 isl_flow_foreach(flow, collect_must_may, &mm);
1558 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1559 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1560 assert(map_is_equal(mm.may, str));
1561 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1562 assert(map_is_equal(mm.must, str));
1564 isl_map_free(mm.must);
1565 isl_map_free(mm.may);
1566 isl_flow_free(flow);
1571 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1572 map = isl_map_read_from_str(ctx, str);
1573 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1575 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1576 map = isl_map_read_from_str(ctx, str);
1577 ai = isl_access_info_add_source(ai, map, 1, &depth);
1579 flow = isl_access_info_compute_flow(ai);
1580 dim = isl_space_alloc(ctx, 0, 5, 5);
1581 mm.must = isl_map_empty(isl_space_copy(dim));
1582 mm.may = isl_map_empty(dim);
1584 isl_flow_foreach(flow, collect_must_may, &mm);
1586 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1587 assert(map_is_equal(mm.must, str));
1588 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1589 assert(map_is_equal(mm.may, str));
1591 isl_map_free(mm.must);
1592 isl_map_free(mm.may);
1593 isl_flow_free(flow);
1596 int test_sv(isl_ctx *ctx)
1600 isl_union_map *umap;
1603 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1604 map = isl_map_read_from_str(ctx, str);
1605 sv = isl_map_is_single_valued(map);
1610 isl_die(ctx, isl_error_internal,
1611 "map not detected as single valued", return -1);
1613 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1614 map = isl_map_read_from_str(ctx, str);
1615 sv = isl_map_is_single_valued(map);
1620 isl_die(ctx, isl_error_internal,
1621 "map detected as single valued", return -1);
1623 str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
1624 umap = isl_union_map_read_from_str(ctx, str);
1625 sv = isl_union_map_is_single_valued(umap);
1626 isl_union_map_free(umap);
1630 isl_die(ctx, isl_error_internal,
1631 "map not detected as single valued", return -1);
1633 str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
1634 umap = isl_union_map_read_from_str(ctx, str);
1635 sv = isl_union_map_is_single_valued(umap);
1636 isl_union_map_free(umap);
1640 isl_die(ctx, isl_error_internal,
1641 "map detected as single valued", return -1);
1646 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1650 map = isl_map_read_from_str(ctx, str);
1652 assert(isl_map_is_bijective(map));
1654 assert(!isl_map_is_bijective(map));
1658 void test_bijective(struct isl_ctx *ctx)
1660 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1661 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1662 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1663 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1664 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1665 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1666 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1667 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1668 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1669 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1670 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1671 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1672 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1675 void test_pwqp(struct isl_ctx *ctx)
1679 isl_pw_qpolynomial *pwqp1, *pwqp2;
1681 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1682 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1684 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1687 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1688 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1690 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1692 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1694 isl_pw_qpolynomial_free(pwqp1);
1696 str = "{ [i] -> i }";
1697 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1698 str = "{ [k] : exists a : k = 2a }";
1699 set = isl_set_read_from_str(ctx, str);
1700 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1701 str = "{ [i] -> i }";
1702 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1704 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1706 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1708 isl_pw_qpolynomial_free(pwqp1);
1710 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1711 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1713 set = isl_set_read_from_str(ctx, str);
1714 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1715 str = "{ [i] -> 16 }";
1716 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1718 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1720 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1722 isl_pw_qpolynomial_free(pwqp1);
1724 str = "{ [i] -> ([(i)/2]) }";
1725 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1726 str = "{ [k] : exists a : k = 2a+1 }";
1727 set = isl_set_read_from_str(ctx, str);
1728 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1729 str = "{ [i] -> -1/2 + 1/2 * i }";
1730 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1732 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1734 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1736 isl_pw_qpolynomial_free(pwqp1);
1738 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1739 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1740 str = "{ [i] -> ([(2 * [i/2])/5]) }";
1741 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1743 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1745 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1747 isl_pw_qpolynomial_free(pwqp1);
1749 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1750 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1751 str = "{ [x] -> x }";
1752 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1754 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1756 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1758 isl_pw_qpolynomial_free(pwqp1);
1760 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
1761 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1762 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1763 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
1764 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1765 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1766 isl_pw_qpolynomial_free(pwqp1);
1769 void test_split_periods(isl_ctx *ctx)
1772 isl_pw_qpolynomial *pwqp;
1774 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1775 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
1776 "U >= 0; [U,V] -> U^2 : U >= 100 }";
1777 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1779 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1782 isl_pw_qpolynomial_free(pwqp);
1785 void test_union(isl_ctx *ctx)
1788 isl_union_set *uset1, *uset2;
1789 isl_union_map *umap1, *umap2;
1791 str = "{ [i] : 0 <= i <= 1 }";
1792 uset1 = isl_union_set_read_from_str(ctx, str);
1793 str = "{ [1] -> [0] }";
1794 umap1 = isl_union_map_read_from_str(ctx, str);
1796 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1797 assert(isl_union_map_is_equal(umap1, umap2));
1799 isl_union_map_free(umap1);
1800 isl_union_map_free(umap2);
1802 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1803 umap1 = isl_union_map_read_from_str(ctx, str);
1804 str = "{ A[i]; B[i] }";
1805 uset1 = isl_union_set_read_from_str(ctx, str);
1807 uset2 = isl_union_map_domain(umap1);
1809 assert(isl_union_set_is_equal(uset1, uset2));
1811 isl_union_set_free(uset1);
1812 isl_union_set_free(uset2);
1815 void test_bound(isl_ctx *ctx)
1818 isl_pw_qpolynomial *pwqp;
1819 isl_pw_qpolynomial_fold *pwf;
1821 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1822 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1823 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1824 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
1825 isl_pw_qpolynomial_fold_free(pwf);
1827 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
1828 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1829 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1830 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
1831 isl_pw_qpolynomial_fold_free(pwf);
1834 void test_lift(isl_ctx *ctx)
1837 isl_basic_map *bmap;
1838 isl_basic_set *bset;
1840 str = "{ [i0] : exists e0 : i0 = 4e0 }";
1841 bset = isl_basic_set_read_from_str(ctx, str);
1842 bset = isl_basic_set_lift(bset);
1843 bmap = isl_basic_map_from_range(bset);
1844 bset = isl_basic_map_domain(bmap);
1845 isl_basic_set_free(bset);
1848 void test_subset(isl_ctx *ctx)
1851 isl_set *set1, *set2;
1853 str = "{ [112, 0] }";
1854 set1 = isl_set_read_from_str(ctx, str);
1855 str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1856 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1857 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
1858 set2 = isl_set_read_from_str(ctx, str);
1859 assert(isl_set_is_subset(set1, set2));
1864 void test_factorize(isl_ctx *ctx)
1867 isl_basic_set *bset;
1870 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
1871 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
1872 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
1873 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
1874 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
1875 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
1876 "3i5 >= -2i0 - i2 + 3i4 }";
1877 bset = isl_basic_set_read_from_str(ctx, str);
1878 f = isl_basic_set_factorizer(bset);
1880 isl_basic_set_free(bset);
1881 isl_factorizer_free(f);
1883 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1884 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
1885 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
1886 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
1887 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
1888 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
1889 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
1890 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
1891 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
1892 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
1893 bset = isl_basic_set_read_from_str(ctx, str);
1894 f = isl_basic_set_factorizer(bset);
1896 isl_basic_set_free(bset);
1897 isl_factorizer_free(f);
1900 static int check_injective(__isl_take isl_map *map, void *user)
1902 int *injective = user;
1904 *injective = isl_map_is_injective(map);
1907 if (*injective < 0 || !*injective)
1913 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
1914 const char *r, const char *s, int tilable, int parallel)
1918 isl_union_map *W, *R, *S;
1919 isl_union_map *empty;
1920 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
1921 isl_union_map *validity, *proximity;
1922 isl_union_map *schedule;
1923 isl_union_map *test;
1924 isl_union_set *delta;
1925 isl_union_set *domain;
1929 isl_schedule *sched;
1930 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
1932 D = isl_union_set_read_from_str(ctx, d);
1933 W = isl_union_map_read_from_str(ctx, w);
1934 R = isl_union_map_read_from_str(ctx, r);
1935 S = isl_union_map_read_from_str(ctx, s);
1937 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
1938 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
1940 empty = isl_union_map_empty(isl_union_map_get_space(S));
1941 isl_union_map_compute_flow(isl_union_map_copy(R),
1942 isl_union_map_copy(W), empty,
1943 isl_union_map_copy(S),
1944 &dep_raw, NULL, NULL, NULL);
1945 isl_union_map_compute_flow(isl_union_map_copy(W),
1946 isl_union_map_copy(W),
1947 isl_union_map_copy(R),
1948 isl_union_map_copy(S),
1949 &dep_waw, &dep_war, NULL, NULL);
1951 dep = isl_union_map_union(dep_waw, dep_war);
1952 dep = isl_union_map_union(dep, dep_raw);
1953 validity = isl_union_map_copy(dep);
1954 proximity = isl_union_map_copy(dep);
1956 sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
1957 validity, proximity);
1958 schedule = isl_schedule_get_map(sched);
1959 isl_schedule_free(sched);
1960 isl_union_map_free(W);
1961 isl_union_map_free(R);
1962 isl_union_map_free(S);
1965 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
1967 domain = isl_union_map_domain(isl_union_map_copy(schedule));
1968 is_complete = isl_union_set_is_subset(D, domain);
1969 isl_union_set_free(D);
1970 isl_union_set_free(domain);
1972 test = isl_union_map_reverse(isl_union_map_copy(schedule));
1973 test = isl_union_map_apply_range(test, dep);
1974 test = isl_union_map_apply_range(test, schedule);
1976 delta = isl_union_map_deltas(test);
1977 if (isl_union_set_n_set(delta) == 0) {
1981 isl_union_set_free(delta);
1983 delta_set = isl_set_from_union_set(delta);
1985 slice = isl_set_universe(isl_set_get_space(delta_set));
1986 for (i = 0; i < tilable; ++i)
1987 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
1988 is_tilable = isl_set_is_subset(delta_set, slice);
1989 isl_set_free(slice);
1991 slice = isl_set_universe(isl_set_get_space(delta_set));
1992 for (i = 0; i < parallel; ++i)
1993 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
1994 is_parallel = isl_set_is_subset(delta_set, slice);
1995 isl_set_free(slice);
1997 origin = isl_set_universe(isl_set_get_space(delta_set));
1998 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
1999 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2001 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2002 delta_set = isl_set_lexmin(delta_set);
2004 is_nonneg = isl_set_is_equal(delta_set, origin);
2006 isl_set_free(origin);
2007 isl_set_free(delta_set);
2010 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2011 is_injection < 0 || is_complete < 0)
2014 isl_die(ctx, isl_error_unknown,
2015 "generated schedule incomplete", return -1);
2017 isl_die(ctx, isl_error_unknown,
2018 "generated schedule not injective on each statement",
2021 isl_die(ctx, isl_error_unknown,
2022 "negative dependences in generated schedule",
2025 isl_die(ctx, isl_error_unknown,
2026 "generated schedule not as tilable as expected",
2029 isl_die(ctx, isl_error_unknown,
2030 "generated schedule not as parallel as expected",
2036 int test_special_schedule(isl_ctx *ctx)
2040 isl_union_map *empty;
2042 isl_union_map *sched1, *sched2;
2043 isl_schedule *schedule;
2046 str = "{ S[i,j] : 0 <= i <= 10 }";
2047 dom = isl_union_set_read_from_str(ctx, str);
2048 str = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2049 dep = isl_union_map_read_from_str(ctx, str);
2050 empty = isl_union_map_read_from_str(ctx, "{}");
2051 schedule = isl_union_set_compute_schedule(dom, empty, dep);
2052 sched1 = isl_schedule_get_map(schedule);
2053 isl_schedule_free(schedule);
2055 str = "{ S[i, j] -> [j, i] }";
2056 sched2 = isl_union_map_read_from_str(ctx, str);
2058 equal = isl_union_map_is_equal(sched1, sched2);
2059 isl_union_map_free(sched1);
2060 isl_union_map_free(sched2);
2065 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2071 int test_schedule(isl_ctx *ctx)
2073 const char *D, *W, *R, *S;
2076 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2077 W = "{ S1[t,i] -> a[t,i] }";
2078 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2079 "S1[t,i] -> a[t-1,i+1] }";
2080 S = "{ S1[t,i] -> [t,i] }";
2081 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2084 /* Fig. 5 of CC2008 */
2085 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2087 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2088 "j >= 2 and j <= -1 + N }";
2089 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2090 "j >= 2 and j <= -1 + N; "
2091 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2092 "j >= 2 and j <= -1 + N }";
2093 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2094 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2097 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2098 W = "{ S1[i] -> a[i] }";
2099 R = "{ S2[i] -> a[i+1] }";
2100 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2101 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2104 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2105 W = "{ S1[i] -> a[i] }";
2106 R = "{ S2[i] -> a[9-i] }";
2107 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2108 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2111 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2112 W = "{ S1[i] -> a[i] }";
2113 R = "[N] -> { S2[i] -> a[N-1-i] }";
2114 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2115 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2118 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2119 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2120 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2121 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2122 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2125 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2126 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2127 R = "{ S2[i,j] -> a[i-1,j] }";
2128 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2129 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2132 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2133 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2134 R = "{ S2[i,j] -> a[i,j-1] }";
2135 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2136 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2139 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2140 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2141 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2142 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2143 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2144 "S_0[] -> [0, 0, 0] }";
2145 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2147 ctx->opt->schedule_parametric = 0;
2148 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2150 ctx->opt->schedule_parametric = 1;
2152 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2153 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2154 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2155 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2156 "S4[i] -> a[i,N] }";
2157 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2158 "S4[i] -> [4,i,0] }";
2159 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2162 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2163 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2165 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2167 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2169 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2170 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2173 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2174 " S_2[t] : t >= 0 and t <= -1 + N; "
2175 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2177 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2178 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2179 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2180 "i >= 0 and i <= -1 + N }";
2181 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2182 "i >= 0 and i <= -1 + N; "
2183 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2184 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2185 " S_0[t] -> [0, t, 0] }";
2187 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2189 ctx->opt->schedule_parametric = 0;
2190 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2192 ctx->opt->schedule_parametric = 1;
2194 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2195 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2196 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2199 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2200 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2201 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2202 "j >= 0 and j <= -1 + N; "
2203 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2204 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2205 "j >= 0 and j <= -1 + N; "
2206 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2207 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2208 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2211 D = "{ S_0[i] : i >= 0 }";
2212 W = "{ S_0[i] -> a[i] : i >= 0 }";
2213 R = "{ S_0[i] -> a[0] : i >= 0 }";
2214 S = "{ S_0[i] -> [0, i, 0] }";
2215 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2218 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2219 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2220 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2221 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2222 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2225 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2226 "k <= -1 + n and k >= 0 }";
2227 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
2228 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2229 "k <= -1 + n and k >= 0; "
2230 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2231 "k <= -1 + n and k >= 0; "
2232 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2233 "k <= -1 + n and k >= 0 }";
2234 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2235 ctx->opt->schedule_outer_zero_distance = 1;
2236 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2238 ctx->opt->schedule_outer_zero_distance = 0;
2240 return test_special_schedule(ctx);
2243 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2245 isl_union_map *umap;
2248 umap = isl_union_map_read_from_str(ctx, str);
2249 test = isl_union_map_plain_is_injective(umap);
2250 isl_union_map_free(umap);
2253 if (test == injective)
2256 isl_die(ctx, isl_error_unknown,
2257 "map not detected as injective", return -1);
2259 isl_die(ctx, isl_error_unknown,
2260 "map detected as injective", return -1);
2263 int test_injective(isl_ctx *ctx)
2267 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2269 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2271 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2273 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2275 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2277 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2279 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2281 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2284 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2285 if (test_plain_injective(ctx, str, 1))
2287 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2288 if (test_plain_injective(ctx, str, 0))
2294 int test_aff(isl_ctx *ctx)
2299 isl_local_space *ls;
2303 dim = isl_space_set_alloc(ctx, 0, 1);
2304 ls = isl_local_space_from_space(dim);
2305 aff = isl_aff_zero_on_domain(ls);
2307 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2308 aff = isl_aff_scale_down_ui(aff, 3);
2309 aff = isl_aff_floor(aff);
2310 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2311 aff = isl_aff_scale_down_ui(aff, 2);
2312 aff = isl_aff_floor(aff);
2313 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2316 set = isl_set_read_from_str(ctx, str);
2317 aff = isl_aff_gist(aff, set);
2319 aff = isl_aff_add_constant_si(aff, -16);
2320 zero = isl_aff_plain_is_zero(aff);
2326 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2331 int test_dim_max(isl_ctx *ctx)
2335 isl_set *set1, *set2;
2340 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
2341 set = isl_set_read_from_str(ctx, str);
2342 pwaff = isl_set_dim_max(set, 0);
2343 set1 = isl_set_from_pw_aff(pwaff);
2344 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
2345 set2 = isl_set_read_from_str(ctx, str);
2346 equal = isl_set_is_equal(set1, set2);
2352 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2354 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
2355 set = isl_set_read_from_str(ctx, str);
2356 pwaff = isl_set_dim_max(set, 0);
2357 set1 = isl_set_from_pw_aff(pwaff);
2358 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2359 set2 = isl_set_read_from_str(ctx, str);
2360 equal = isl_set_is_equal(set1, set2);
2366 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2368 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
2369 set = isl_set_read_from_str(ctx, str);
2370 pwaff = isl_set_dim_max(set, 0);
2371 set1 = isl_set_from_pw_aff(pwaff);
2372 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2373 set2 = isl_set_read_from_str(ctx, str);
2374 equal = isl_set_is_equal(set1, set2);
2380 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2382 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
2383 "0 <= i < N and 0 <= j < M }";
2384 map = isl_map_read_from_str(ctx, str);
2385 set = isl_map_range(map);
2387 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
2388 set1 = isl_set_from_pw_aff(pwaff);
2389 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
2390 set2 = isl_set_read_from_str(ctx, str);
2391 equal = isl_set_is_equal(set1, set2);
2395 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
2396 set1 = isl_set_from_pw_aff(pwaff);
2397 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
2398 set2 = isl_set_read_from_str(ctx, str);
2399 if (equal >= 0 && equal)
2400 equal = isl_set_is_equal(set1, set2);
2409 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2414 int test_product(isl_ctx *ctx)
2421 set = isl_set_read_from_str(ctx, str);
2422 set = isl_set_product(set, isl_set_copy(set));
2423 ok = isl_set_is_wrapping(set);
2428 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2433 int test_equal(isl_ctx *ctx)
2436 isl_set *set, *set2;
2440 set = isl_set_read_from_str(ctx, str);
2442 set2 = isl_set_read_from_str(ctx, str);
2443 equal = isl_set_is_equal(set, set2);
2449 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2454 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
2455 enum isl_dim_type type, unsigned pos, int fixed)
2459 test = isl_map_plain_is_fixed(map, type, pos, NULL);
2466 isl_die(ctx, isl_error_unknown,
2467 "map not detected as fixed", return -1);
2469 isl_die(ctx, isl_error_unknown,
2470 "map detected as fixed", return -1);
2473 int test_fixed(isl_ctx *ctx)
2478 str = "{ [i] -> [i] }";
2479 map = isl_map_read_from_str(ctx, str);
2480 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
2482 str = "{ [i] -> [1] }";
2483 map = isl_map_read_from_str(ctx, str);
2484 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2486 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
2487 map = isl_map_read_from_str(ctx, str);
2488 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2490 map = isl_map_read_from_str(ctx, str);
2491 map = isl_map_neg(map);
2492 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2500 struct isl_ctx *ctx;
2502 srcdir = getenv("srcdir");
2505 ctx = isl_ctx_alloc();
2506 if (test_fixed(ctx) < 0)
2508 if (test_equal(ctx) < 0)
2510 if (test_product(ctx) < 0)
2512 if (test_dim_max(ctx) < 0)
2514 if (test_aff(ctx) < 0)
2516 if (test_injective(ctx) < 0)
2518 if (test_schedule(ctx) < 0)
2520 test_factorize(ctx);
2525 test_split_periods(ctx);
2529 if (test_sv(ctx) < 0)
2531 test_bijective(ctx);
2535 test_construction(ctx);
2538 test_application(ctx);
2539 test_affine_hull(ctx);
2540 test_convex_hull(ctx);