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 void test_parse(struct isl_ctx *ctx)
75 const char *str, *str2;
77 str = "{ [i] -> [-i] }";
78 map = isl_map_read_from_str(ctx, str);
82 str = "{ A[i] -> L[([i/3])] }";
83 map = isl_map_read_from_str(ctx, str);
87 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
88 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
89 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
91 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
92 str2 = "{ [x, y] : 2y >= 6 - x }";
93 test_parse_map_equal(ctx, str, str2);
95 test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
96 "{ [x,y] : x <= y, 2*y + 3 }");
97 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
98 test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }", str);
100 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
101 map = isl_map_read_from_str(ctx, str);
102 str = "{ [new, old] -> [o0, o1] : "
103 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
104 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
105 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
106 map2 = isl_map_read_from_str(ctx, str);
107 assert(isl_map_is_equal(map, map2));
111 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
112 map = isl_map_read_from_str(ctx, str);
113 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
114 map2 = isl_map_read_from_str(ctx, str);
115 assert(isl_map_is_equal(map, map2));
119 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
120 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
121 test_parse_map_equal(ctx, str, str2);
123 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
124 str2 = "{ [i,j] -> [min(i,j)] }";
125 test_parse_map_equal(ctx, str, str2);
127 str = "{ [i,j] : i != j }";
128 str2 = "{ [i,j] : i < j or i > j }";
129 test_parse_map_equal(ctx, str, str2);
131 str = "{ [i,j] : (i+1)*2 >= j }";
132 str2 = "{ [i, j] : j <= 2 + 2i }";
133 test_parse_map_equal(ctx, str, str2);
135 str = "{ [i] -> [i > 0 ? 4 : 5] }";
136 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
137 test_parse_map_equal(ctx, str, str2);
139 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
140 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
141 test_parse_map_equal(ctx, str, str2);
143 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
144 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
147 void test_read(struct isl_ctx *ctx)
151 struct isl_basic_set *bset1, *bset2;
152 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
154 filename = get_filename(ctx, "set", "omega");
156 input = fopen(filename, "r");
159 bset1 = isl_basic_set_read_from_file(ctx, input);
160 bset2 = isl_basic_set_read_from_str(ctx, str);
162 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
164 isl_basic_set_free(bset1);
165 isl_basic_set_free(bset2);
171 void test_bounded(struct isl_ctx *ctx)
176 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
177 bounded = isl_set_is_bounded(set);
181 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
182 bounded = isl_set_is_bounded(set);
186 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
187 bounded = isl_set_is_bounded(set);
192 /* Construct the basic set { [i] : 5 <= i <= N } */
193 void test_construction(struct isl_ctx *ctx)
198 struct isl_basic_set *bset;
199 struct isl_constraint *c;
203 dim = isl_space_set_alloc(ctx, 1, 1);
204 bset = isl_basic_set_universe(isl_space_copy(dim));
205 ls = isl_local_space_from_space(dim);
207 c = isl_inequality_alloc(isl_local_space_copy(ls));
208 isl_int_set_si(v, -1);
209 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
210 isl_int_set_si(v, 1);
211 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
212 bset = isl_basic_set_add_constraint(bset, c);
214 c = isl_inequality_alloc(isl_local_space_copy(ls));
215 isl_int_set_si(v, 1);
216 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
217 isl_int_set_si(v, -5);
218 isl_constraint_set_constant(c, v);
219 bset = isl_basic_set_add_constraint(bset, c);
221 isl_local_space_free(ls);
222 isl_basic_set_free(bset);
227 void test_dim(struct isl_ctx *ctx)
230 isl_map *map1, *map2;
232 map1 = isl_map_read_from_str(ctx,
233 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
234 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
235 map2 = isl_map_read_from_str(ctx,
236 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
237 assert(isl_map_is_equal(map1, map2));
240 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
241 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
242 assert(isl_map_is_equal(map1, map2));
247 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
248 map1 = isl_map_read_from_str(ctx, str);
249 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
250 map2 = isl_map_read_from_str(ctx, str);
251 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
252 assert(isl_map_is_equal(map1, map2));
258 void test_div(struct isl_ctx *ctx)
263 struct isl_basic_set *bset;
264 struct isl_constraint *c;
269 dim = isl_space_set_alloc(ctx, 0, 3);
270 bset = isl_basic_set_universe(isl_space_copy(dim));
271 ls = isl_local_space_from_space(dim);
273 c = isl_equality_alloc(isl_local_space_copy(ls));
274 isl_int_set_si(v, -1);
275 isl_constraint_set_constant(c, v);
276 isl_int_set_si(v, 1);
277 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
278 isl_int_set_si(v, 3);
279 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
280 bset = isl_basic_set_add_constraint(bset, c);
282 c = isl_equality_alloc(isl_local_space_copy(ls));
283 isl_int_set_si(v, 1);
284 isl_constraint_set_constant(c, v);
285 isl_int_set_si(v, -1);
286 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
287 isl_int_set_si(v, 3);
288 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
289 bset = isl_basic_set_add_constraint(bset, c);
291 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
293 assert(bset && bset->n_div == 1);
294 isl_local_space_free(ls);
295 isl_basic_set_free(bset);
298 dim = isl_space_set_alloc(ctx, 0, 3);
299 bset = isl_basic_set_universe(isl_space_copy(dim));
300 ls = isl_local_space_from_space(dim);
302 c = isl_equality_alloc(isl_local_space_copy(ls));
303 isl_int_set_si(v, 1);
304 isl_constraint_set_constant(c, v);
305 isl_int_set_si(v, -1);
306 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
307 isl_int_set_si(v, 3);
308 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
309 bset = isl_basic_set_add_constraint(bset, c);
311 c = isl_equality_alloc(isl_local_space_copy(ls));
312 isl_int_set_si(v, -1);
313 isl_constraint_set_constant(c, v);
314 isl_int_set_si(v, 1);
315 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
316 isl_int_set_si(v, 3);
317 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
318 bset = isl_basic_set_add_constraint(bset, c);
320 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
322 assert(bset && bset->n_div == 1);
323 isl_local_space_free(ls);
324 isl_basic_set_free(bset);
327 dim = isl_space_set_alloc(ctx, 0, 3);
328 bset = isl_basic_set_universe(isl_space_copy(dim));
329 ls = isl_local_space_from_space(dim);
331 c = isl_equality_alloc(isl_local_space_copy(ls));
332 isl_int_set_si(v, 1);
333 isl_constraint_set_constant(c, v);
334 isl_int_set_si(v, -1);
335 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
336 isl_int_set_si(v, 3);
337 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
338 bset = isl_basic_set_add_constraint(bset, c);
340 c = isl_equality_alloc(isl_local_space_copy(ls));
341 isl_int_set_si(v, -3);
342 isl_constraint_set_constant(c, v);
343 isl_int_set_si(v, 1);
344 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
345 isl_int_set_si(v, 4);
346 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
347 bset = isl_basic_set_add_constraint(bset, c);
349 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
351 assert(bset && bset->n_div == 1);
352 isl_local_space_free(ls);
353 isl_basic_set_free(bset);
356 dim = isl_space_set_alloc(ctx, 0, 3);
357 bset = isl_basic_set_universe(isl_space_copy(dim));
358 ls = isl_local_space_from_space(dim);
360 c = isl_equality_alloc(isl_local_space_copy(ls));
361 isl_int_set_si(v, 2);
362 isl_constraint_set_constant(c, v);
363 isl_int_set_si(v, -1);
364 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
365 isl_int_set_si(v, 3);
366 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
367 bset = isl_basic_set_add_constraint(bset, c);
369 c = isl_equality_alloc(isl_local_space_copy(ls));
370 isl_int_set_si(v, -1);
371 isl_constraint_set_constant(c, v);
372 isl_int_set_si(v, 1);
373 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
374 isl_int_set_si(v, 6);
375 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
376 bset = isl_basic_set_add_constraint(bset, c);
378 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
380 assert(isl_basic_set_is_empty(bset));
381 isl_local_space_free(ls);
382 isl_basic_set_free(bset);
385 dim = isl_space_set_alloc(ctx, 0, 3);
386 bset = isl_basic_set_universe(isl_space_copy(dim));
387 ls = isl_local_space_from_space(dim);
389 c = isl_equality_alloc(isl_local_space_copy(ls));
390 isl_int_set_si(v, -1);
391 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
392 isl_int_set_si(v, 3);
393 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
394 bset = isl_basic_set_add_constraint(bset, c);
396 c = isl_equality_alloc(isl_local_space_copy(ls));
397 isl_int_set_si(v, 1);
398 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
399 isl_int_set_si(v, -3);
400 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
401 bset = isl_basic_set_add_constraint(bset, c);
403 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
405 assert(bset && bset->n_div == 0);
406 isl_basic_set_free(bset);
407 isl_local_space_free(ls);
410 dim = isl_space_set_alloc(ctx, 0, 3);
411 bset = isl_basic_set_universe(isl_space_copy(dim));
412 ls = isl_local_space_from_space(dim);
414 c = isl_equality_alloc(isl_local_space_copy(ls));
415 isl_int_set_si(v, -1);
416 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
417 isl_int_set_si(v, 6);
418 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
419 bset = isl_basic_set_add_constraint(bset, c);
421 c = isl_equality_alloc(isl_local_space_copy(ls));
422 isl_int_set_si(v, 1);
423 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
424 isl_int_set_si(v, -3);
425 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
426 bset = isl_basic_set_add_constraint(bset, c);
428 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
430 assert(bset && bset->n_div == 1);
431 isl_basic_set_free(bset);
432 isl_local_space_free(ls);
435 /* This test is a bit tricky. We set up an equality
437 * Normalization of divs creates _two_ divs
440 * Afterwards e0 is removed again because it has coefficient -1
441 * and we end up with the original equality and div again.
442 * Perhaps we can avoid the introduction of this temporary div.
444 dim = isl_space_set_alloc(ctx, 0, 4);
445 bset = isl_basic_set_universe(isl_space_copy(dim));
446 ls = isl_local_space_from_space(dim);
448 c = isl_equality_alloc(isl_local_space_copy(ls));
449 isl_int_set_si(v, -1);
450 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
451 isl_int_set_si(v, -3);
452 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
453 isl_int_set_si(v, -3);
454 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
455 isl_int_set_si(v, 6);
456 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
457 bset = isl_basic_set_add_constraint(bset, c);
459 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
461 /* Test disabled for now */
463 assert(bset && bset->n_div == 1);
465 isl_local_space_free(ls);
466 isl_basic_set_free(bset);
469 dim = isl_space_set_alloc(ctx, 0, 5);
470 bset = isl_basic_set_universe(isl_space_copy(dim));
471 ls = isl_local_space_from_space(dim);
473 c = isl_equality_alloc(isl_local_space_copy(ls));
474 isl_int_set_si(v, -1);
475 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
476 isl_int_set_si(v, -3);
477 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
478 isl_int_set_si(v, -3);
479 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
480 isl_int_set_si(v, 6);
481 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
482 bset = isl_basic_set_add_constraint(bset, c);
484 c = isl_equality_alloc(isl_local_space_copy(ls));
485 isl_int_set_si(v, -1);
486 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
487 isl_int_set_si(v, 1);
488 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
489 isl_int_set_si(v, 1);
490 isl_constraint_set_constant(c, v);
491 bset = isl_basic_set_add_constraint(bset, c);
493 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
495 /* Test disabled for now */
497 assert(bset && bset->n_div == 1);
499 isl_local_space_free(ls);
500 isl_basic_set_free(bset);
503 dim = isl_space_set_alloc(ctx, 0, 4);
504 bset = isl_basic_set_universe(isl_space_copy(dim));
505 ls = isl_local_space_from_space(dim);
507 c = isl_equality_alloc(isl_local_space_copy(ls));
508 isl_int_set_si(v, 1);
509 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
510 isl_int_set_si(v, -1);
511 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
512 isl_int_set_si(v, -2);
513 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
514 bset = isl_basic_set_add_constraint(bset, c);
516 c = isl_equality_alloc(isl_local_space_copy(ls));
517 isl_int_set_si(v, -1);
518 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
519 isl_int_set_si(v, 3);
520 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
521 isl_int_set_si(v, 2);
522 isl_constraint_set_constant(c, v);
523 bset = isl_basic_set_add_constraint(bset, c);
525 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
527 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
529 assert(!isl_basic_set_is_empty(bset));
531 isl_local_space_free(ls);
532 isl_basic_set_free(bset);
535 dim = isl_space_set_alloc(ctx, 0, 3);
536 bset = isl_basic_set_universe(isl_space_copy(dim));
537 ls = isl_local_space_from_space(dim);
539 c = isl_equality_alloc(isl_local_space_copy(ls));
540 isl_int_set_si(v, 1);
541 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
542 isl_int_set_si(v, -2);
543 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
544 bset = isl_basic_set_add_constraint(bset, c);
546 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
548 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
550 isl_local_space_free(ls);
551 isl_basic_set_free(bset);
556 void test_application_case(struct isl_ctx *ctx, const char *name)
560 struct isl_basic_set *bset1, *bset2;
561 struct isl_basic_map *bmap;
563 filename = get_filename(ctx, name, "omega");
565 input = fopen(filename, "r");
568 bset1 = isl_basic_set_read_from_file(ctx, input);
569 bmap = isl_basic_map_read_from_file(ctx, input);
571 bset1 = isl_basic_set_apply(bset1, bmap);
573 bset2 = isl_basic_set_read_from_file(ctx, input);
575 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
577 isl_basic_set_free(bset1);
578 isl_basic_set_free(bset2);
584 void test_application(struct isl_ctx *ctx)
586 test_application_case(ctx, "application");
587 test_application_case(ctx, "application2");
590 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
594 struct isl_basic_set *bset1, *bset2;
596 filename = get_filename(ctx, name, "polylib");
598 input = fopen(filename, "r");
601 bset1 = isl_basic_set_read_from_file(ctx, input);
602 bset2 = isl_basic_set_read_from_file(ctx, input);
604 bset1 = isl_basic_set_affine_hull(bset1);
606 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
608 isl_basic_set_free(bset1);
609 isl_basic_set_free(bset2);
615 void test_affine_hull(struct isl_ctx *ctx)
617 test_affine_hull_case(ctx, "affine2");
618 test_affine_hull_case(ctx, "affine");
619 test_affine_hull_case(ctx, "affine3");
622 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
626 struct isl_basic_set *bset1, *bset2;
629 filename = get_filename(ctx, name, "polylib");
631 input = fopen(filename, "r");
634 bset1 = isl_basic_set_read_from_file(ctx, input);
635 bset2 = isl_basic_set_read_from_file(ctx, input);
637 set = isl_basic_set_union(bset1, bset2);
638 bset1 = isl_set_convex_hull(set);
640 bset2 = isl_basic_set_read_from_file(ctx, input);
642 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
644 isl_basic_set_free(bset1);
645 isl_basic_set_free(bset2);
651 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
653 const char *str1, *str2;
654 isl_set *set1, *set2;
655 int orig_convex = ctx->opt->convex;
656 ctx->opt->convex = convex;
658 test_convex_hull_case(ctx, "convex0");
659 test_convex_hull_case(ctx, "convex1");
660 test_convex_hull_case(ctx, "convex2");
661 test_convex_hull_case(ctx, "convex3");
662 test_convex_hull_case(ctx, "convex4");
663 test_convex_hull_case(ctx, "convex5");
664 test_convex_hull_case(ctx, "convex6");
665 test_convex_hull_case(ctx, "convex7");
666 test_convex_hull_case(ctx, "convex8");
667 test_convex_hull_case(ctx, "convex9");
668 test_convex_hull_case(ctx, "convex10");
669 test_convex_hull_case(ctx, "convex11");
670 test_convex_hull_case(ctx, "convex12");
671 test_convex_hull_case(ctx, "convex13");
672 test_convex_hull_case(ctx, "convex14");
673 test_convex_hull_case(ctx, "convex15");
675 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
676 "(i0 = 1 and i1 = 0 and i2 = 1) or "
677 "(i0 = 0 and i1 = 0 and i2 = 0) }";
678 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
679 set1 = isl_set_read_from_str(ctx, str1);
680 set2 = isl_set_read_from_str(ctx, str2);
681 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
682 assert(isl_set_is_equal(set1, set2));
686 ctx->opt->convex = orig_convex;
689 void test_convex_hull(struct isl_ctx *ctx)
691 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
692 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
695 void test_gist_case(struct isl_ctx *ctx, const char *name)
699 struct isl_basic_set *bset1, *bset2;
701 filename = get_filename(ctx, name, "polylib");
703 input = fopen(filename, "r");
706 bset1 = isl_basic_set_read_from_file(ctx, input);
707 bset2 = isl_basic_set_read_from_file(ctx, input);
709 bset1 = isl_basic_set_gist(bset1, bset2);
711 bset2 = isl_basic_set_read_from_file(ctx, input);
713 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
715 isl_basic_set_free(bset1);
716 isl_basic_set_free(bset2);
722 void test_gist(struct isl_ctx *ctx)
725 isl_basic_set *bset1, *bset2;
727 test_gist_case(ctx, "gist1");
729 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
730 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
731 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
732 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
733 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
734 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
735 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
736 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
737 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
738 bset1 = isl_basic_set_read_from_str(ctx, str);
739 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
740 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
741 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
742 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
743 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
744 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
745 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
746 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
747 bset2 = isl_basic_set_read_from_str(ctx, str);
748 bset1 = isl_basic_set_gist(bset1, bset2);
749 assert(bset1 && bset1->n_div == 0);
750 isl_basic_set_free(bset1);
753 void test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
757 set = isl_set_read_from_str(ctx, str);
758 set = isl_set_coalesce(set);
759 set2 = isl_set_read_from_str(ctx, str);
760 assert(isl_set_is_equal(set, set2));
762 assert(set && set->n == 1);
767 void test_coalesce(struct isl_ctx *ctx)
770 struct isl_set *set, *set2;
771 struct isl_map *map, *map2;
773 set = isl_set_read_from_str(ctx,
774 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
775 "y >= x & x >= 2 & 5 >= y }");
776 set = isl_set_coalesce(set);
777 assert(set && set->n == 1);
780 set = isl_set_read_from_str(ctx,
781 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
782 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}");
783 set = isl_set_coalesce(set);
784 assert(set && set->n == 1);
787 set = isl_set_read_from_str(ctx,
788 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
789 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}");
790 set = isl_set_coalesce(set);
791 assert(set && set->n == 2);
794 set = isl_set_read_from_str(ctx,
795 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
796 "y >= 0 & x >= 6 & x <= 10 & y <= x}");
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 & x <= 5 & y <= x or "
803 "y >= 0 & x >= 7 & x <= 10 & y <= x}");
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 + 1 <= x}");
811 set = isl_set_coalesce(set);
812 assert(set && set->n == 2);
815 set = isl_set_read_from_str(ctx,
816 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
817 "y >= 0 & x = 6 & y <= 6}");
818 set = isl_set_coalesce(set);
819 assert(set && set->n == 1);
822 set = isl_set_read_from_str(ctx,
823 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
824 "y >= 0 & x = 7 & y <= 6}");
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 <= 5}");
832 set = isl_set_coalesce(set);
833 assert(set && set->n == 1);
834 set2 = isl_set_read_from_str(ctx,
835 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
836 "y >= 0 & x = 6 & y <= 5}");
837 assert(isl_set_is_equal(set, set2));
841 set = isl_set_read_from_str(ctx,
842 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
843 "y >= 0 & x = 6 & y <= 7}");
844 set = isl_set_coalesce(set);
845 assert(set && set->n == 2);
848 set = isl_set_read_from_str(ctx,
849 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
850 set = isl_set_coalesce(set);
851 assert(set && set->n == 1);
852 set2 = isl_set_read_from_str(ctx,
853 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
854 assert(isl_set_is_equal(set, set2));
858 set = isl_set_read_from_str(ctx,
859 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
860 set = isl_set_coalesce(set);
861 set2 = isl_set_read_from_str(ctx,
862 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
863 assert(isl_set_is_equal(set, set2));
867 set = isl_set_read_from_str(ctx,
868 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
869 "2 <= i and i <= n }");
870 set = isl_set_coalesce(set);
871 assert(set && set->n == 1);
872 set2 = isl_set_read_from_str(ctx,
873 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
874 "2 <= i and i <= n }");
875 assert(isl_set_is_equal(set, set2));
879 map = isl_map_read_from_str(ctx,
880 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
881 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
882 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
883 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
884 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
885 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
886 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
887 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
888 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
889 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
890 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
891 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
892 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
893 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
894 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
895 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
896 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
897 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
898 map = isl_map_coalesce(map);
899 map2 = isl_map_read_from_str(ctx,
900 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
901 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
902 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
903 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
904 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
905 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
906 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
907 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
908 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
909 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
910 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
911 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
912 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
913 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
914 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
915 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
916 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
917 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
918 assert(isl_map_is_equal(map, map2));
922 str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
923 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
924 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
925 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
926 map = isl_map_read_from_str(ctx, str);
927 map = isl_map_coalesce(map);
928 map2 = isl_map_read_from_str(ctx, str);
929 assert(isl_map_is_equal(map, map2));
933 str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
934 "[o0, o1, o2, o3, o4, o5, o6] : "
935 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
936 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
937 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
938 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
939 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
940 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
941 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
942 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
943 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
944 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
945 "o6 >= i3 + i6 - o3 and M >= 0 and "
946 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
947 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
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] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
956 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
957 "(o0 = 0 and M >= 2 and N >= 3) or "
958 "(M = 0 and o0 = 0 and N >= 3) }";
959 map = isl_map_read_from_str(ctx, str);
960 map = isl_map_coalesce(map);
961 map2 = isl_map_read_from_str(ctx, str);
962 assert(isl_map_is_equal(map, map2));
966 str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
967 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
968 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
969 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
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 test_coalesce_set(ctx,
978 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
979 "(i1 = M and M >= 1) }", 0);
980 test_coalesce_set(ctx,
981 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
982 test_coalesce_set(ctx,
983 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
984 "(y = 3 and x = 1) }", 1);
985 test_coalesce_set(ctx,
986 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
987 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
988 "i1 <= M and i3 <= M and i4 <= M) or "
989 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
990 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
991 "i4 <= -1 + M) }", 1);
992 test_coalesce_set(ctx,
993 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
994 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
995 test_coalesce_set(ctx,
996 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
997 "-x - y + 1 >= 0 and -3 <= z <= 3;"
998 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
999 "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
1000 test_coalesce_set(ctx,
1001 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1);
1002 test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0);
1003 test_coalesce_set(ctx,
1004 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1);
1005 test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1);
1006 test_coalesce_set(ctx, "{ [0,0]; [i,i] : 1 <= i <= 10 }", 1);
1007 test_coalesce_set(ctx, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }", 0);
1008 test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 1 <= i <= 10 }", 1);
1009 test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 2 <= i <= 10 }", 0);
1010 test_coalesce_set(ctx, "{ [1,0]; [i,2i] : 1 <= i <= 10 }", 0);
1011 test_coalesce_set(ctx, "{ [0,1]; [i,2i] : 1 <= i <= 10 }", 0);
1014 void test_closure(struct isl_ctx *ctx)
1018 isl_map *up, *right;
1019 isl_map *map, *map2;
1022 /* COCOA example 1 */
1023 map = isl_map_read_from_str(ctx,
1024 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1025 "1 <= i and i < n and 1 <= j and j < n or "
1026 "i2 = i + 1 and j2 = j - 1 and "
1027 "1 <= i and i < n and 2 <= j and j <= n }");
1028 map = isl_map_power(map, &exact);
1032 /* COCOA example 1 */
1033 map = isl_map_read_from_str(ctx,
1034 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1035 "1 <= i and i < n and 1 <= j and j < n or "
1036 "i2 = i + 1 and j2 = j - 1 and "
1037 "1 <= i and i < n and 2 <= j and j <= n }");
1038 map = isl_map_transitive_closure(map, &exact);
1040 map2 = isl_map_read_from_str(ctx,
1041 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1042 "1 <= i and i < n and 1 <= j and j <= n and "
1043 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1044 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1045 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1046 assert(isl_map_is_equal(map, map2));
1050 map = isl_map_read_from_str(ctx,
1051 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1052 " 0 <= y and y <= n }");
1053 map = isl_map_transitive_closure(map, &exact);
1054 map2 = isl_map_read_from_str(ctx,
1055 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1056 " 0 <= y and y <= n }");
1057 assert(isl_map_is_equal(map, map2));
1061 /* COCOA example 2 */
1062 map = isl_map_read_from_str(ctx,
1063 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1064 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1065 "i2 = i + 2 and j2 = j - 2 and "
1066 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1067 map = isl_map_transitive_closure(map, &exact);
1069 map2 = isl_map_read_from_str(ctx,
1070 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1071 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1072 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1073 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1074 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1075 assert(isl_map_is_equal(map, map2));
1079 /* COCOA Fig.2 left */
1080 map = isl_map_read_from_str(ctx,
1081 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1082 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1084 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1085 "j <= 2 i - 3 and j <= n - 2 or "
1086 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1087 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1088 map = isl_map_transitive_closure(map, &exact);
1092 /* COCOA Fig.2 right */
1093 map = isl_map_read_from_str(ctx,
1094 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1095 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1097 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1098 "j <= 2 i - 4 and j <= n - 3 or "
1099 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1100 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1101 map = isl_map_power(map, &exact);
1105 /* COCOA Fig.2 right */
1106 map = isl_map_read_from_str(ctx,
1107 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1108 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1110 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1111 "j <= 2 i - 4 and j <= n - 3 or "
1112 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1113 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1114 map = isl_map_transitive_closure(map, &exact);
1116 map2 = isl_map_read_from_str(ctx,
1117 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1118 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1119 "j <= n and 3 + i + 2 j <= 3 n and "
1120 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1121 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1122 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1123 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1124 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1125 assert(isl_map_is_equal(map, map2));
1129 /* COCOA Fig.1 right */
1130 dom = isl_set_read_from_str(ctx,
1131 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1132 "2 x - 3 y + 3 >= 0 }");
1133 right = isl_map_read_from_str(ctx,
1134 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1135 up = isl_map_read_from_str(ctx,
1136 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1137 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1138 right = isl_map_intersect_range(right, isl_set_copy(dom));
1139 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1140 up = isl_map_intersect_range(up, dom);
1141 map = isl_map_union(up, right);
1142 map = isl_map_transitive_closure(map, &exact);
1144 map2 = isl_map_read_from_str(ctx,
1145 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1146 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1147 assert(isl_map_is_equal(map, map2));
1151 /* COCOA Theorem 1 counter example */
1152 map = isl_map_read_from_str(ctx,
1153 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1154 "i2 = 1 and j2 = j or "
1155 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1156 map = isl_map_transitive_closure(map, &exact);
1160 map = isl_map_read_from_str(ctx,
1161 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1162 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1163 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1164 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1165 map = isl_map_transitive_closure(map, &exact);
1169 /* Kelly et al 1996, fig 12 */
1170 map = isl_map_read_from_str(ctx,
1171 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1172 "1 <= i,j,j+1 <= n or "
1173 "j = n and j2 = 1 and i2 = i + 1 and "
1174 "1 <= i,i+1 <= n }");
1175 map = isl_map_transitive_closure(map, &exact);
1177 map2 = isl_map_read_from_str(ctx,
1178 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1179 "1 <= i <= n and i = i2 or "
1180 "1 <= i < i2 <= n and 1 <= j <= n and "
1182 assert(isl_map_is_equal(map, map2));
1186 /* Omega's closure4 */
1187 map = isl_map_read_from_str(ctx,
1188 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1189 "1 <= x,y <= 10 or "
1190 "x2 = x + 1 and y2 = y and "
1191 "1 <= x <= 20 && 5 <= y <= 15 }");
1192 map = isl_map_transitive_closure(map, &exact);
1196 map = isl_map_read_from_str(ctx,
1197 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1198 map = isl_map_transitive_closure(map, &exact);
1200 map2 = isl_map_read_from_str(ctx,
1201 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1202 assert(isl_map_is_equal(map, map2));
1206 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1207 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1208 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1209 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1210 map = isl_map_read_from_str(ctx, str);
1211 map = isl_map_transitive_closure(map, &exact);
1213 map2 = isl_map_read_from_str(ctx, str);
1214 assert(isl_map_is_equal(map, map2));
1218 str = "{[0] -> [1]; [2] -> [3]}";
1219 map = isl_map_read_from_str(ctx, str);
1220 map = isl_map_transitive_closure(map, &exact);
1222 map2 = isl_map_read_from_str(ctx, str);
1223 assert(isl_map_is_equal(map, map2));
1227 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1228 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1229 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1230 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1231 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1232 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1233 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1234 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1235 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1236 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1237 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1238 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1239 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1240 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1241 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1242 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1243 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1244 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1245 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1246 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1247 map = isl_map_read_from_str(ctx, str);
1248 map = isl_map_transitive_closure(map, NULL);
1253 void test_lex(struct isl_ctx *ctx)
1258 dim = isl_space_set_alloc(ctx, 0, 0);
1259 map = isl_map_lex_le(dim);
1260 assert(!isl_map_is_empty(map));
1264 static int consume_lexmin(__isl_take isl_basic_set *dom,
1265 __isl_take isl_aff_list *list, void *user)
1268 isl_basic_map *bmap;
1269 isl_map **map = user;
1271 dim = isl_basic_set_get_space(dom);
1272 bmap = isl_basic_map_from_aff_list(dim, list);
1273 bmap = isl_basic_map_intersect_domain(bmap, dom);
1275 *map = isl_map_union(*map, isl_map_from_basic_map(bmap));
1280 void test_lexmin(struct isl_ctx *ctx)
1283 isl_basic_map *bmap;
1284 isl_map *map, *map2;
1288 str = "[p0, p1] -> { [] -> [] : "
1289 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1290 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1291 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1292 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1293 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1294 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1295 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1296 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1297 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1298 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1299 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1300 map = isl_map_read_from_str(ctx, str);
1301 map = isl_map_lexmin(map);
1304 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1305 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1306 set = isl_set_read_from_str(ctx, str);
1307 set = isl_set_lexmax(set);
1308 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1309 set2 = isl_set_read_from_str(ctx, str);
1310 set = isl_set_intersect(set, set2);
1311 assert(!isl_set_is_empty(set));
1314 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1315 map = isl_map_read_from_str(ctx, str);
1316 map = isl_map_lexmin(map);
1317 str = "{ [x] -> [5] : 6 <= x <= 8; "
1318 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1319 map2 = isl_map_read_from_str(ctx, str);
1320 assert(isl_map_is_equal(map, map2));
1324 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1325 map = isl_map_read_from_str(ctx, str);
1326 map2 = isl_map_copy(map);
1327 map = isl_map_lexmin(map);
1328 assert(isl_map_is_equal(map, map2));
1332 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1333 map = isl_map_read_from_str(ctx, str);
1334 map = isl_map_lexmin(map);
1335 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1336 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1337 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1338 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1339 map2 = isl_map_read_from_str(ctx, str);
1340 assert(isl_map_is_equal(map, map2));
1344 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1345 " 8i' <= i and 8i' >= -7 + i }";
1346 bmap = isl_basic_map_read_from_str(ctx, str);
1347 map2 = isl_map_empty(isl_basic_map_get_space(bmap));
1348 isl_basic_map_foreach_lexmin(bmap, &consume_lexmin, &map2);
1349 map = isl_map_from_basic_map(bmap);
1350 assert(isl_map_is_equal(map, map2));
1360 static int collect_must_may(__isl_take isl_map *dep, int must,
1361 void *dep_user, void *user)
1363 struct must_may *mm = (struct must_may *)user;
1366 mm->must = isl_map_union(mm->must, dep);
1368 mm->may = isl_map_union(mm->may, dep);
1373 static int common_space(void *first, void *second)
1375 int depth = *(int *)first;
1379 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1387 map2 = isl_map_read_from_str(map->ctx, str);
1388 equal = isl_map_is_equal(map, map2);
1394 void test_dep(struct isl_ctx *ctx)
1399 isl_access_info *ai;
1406 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1407 map = isl_map_read_from_str(ctx, str);
1408 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1410 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1411 map = isl_map_read_from_str(ctx, str);
1412 ai = isl_access_info_add_source(ai, map, 1, &depth);
1414 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1415 map = isl_map_read_from_str(ctx, str);
1416 ai = isl_access_info_add_source(ai, map, 1, &depth);
1418 flow = isl_access_info_compute_flow(ai);
1419 dim = isl_space_alloc(ctx, 0, 3, 3);
1420 mm.must = isl_map_empty(isl_space_copy(dim));
1421 mm.may = isl_map_empty(dim);
1423 isl_flow_foreach(flow, collect_must_may, &mm);
1425 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1426 " [1,10,0] -> [2,5,0] }";
1427 assert(map_is_equal(mm.must, str));
1428 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1429 assert(map_is_equal(mm.may, str));
1431 isl_map_free(mm.must);
1432 isl_map_free(mm.may);
1433 isl_flow_free(flow);
1436 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1437 map = isl_map_read_from_str(ctx, str);
1438 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1440 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1441 map = isl_map_read_from_str(ctx, str);
1442 ai = isl_access_info_add_source(ai, map, 1, &depth);
1444 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1445 map = isl_map_read_from_str(ctx, str);
1446 ai = isl_access_info_add_source(ai, map, 0, &depth);
1448 flow = isl_access_info_compute_flow(ai);
1449 dim = isl_space_alloc(ctx, 0, 3, 3);
1450 mm.must = isl_map_empty(isl_space_copy(dim));
1451 mm.may = isl_map_empty(dim);
1453 isl_flow_foreach(flow, collect_must_may, &mm);
1455 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1456 assert(map_is_equal(mm.must, str));
1457 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1458 assert(map_is_equal(mm.may, str));
1460 isl_map_free(mm.must);
1461 isl_map_free(mm.may);
1462 isl_flow_free(flow);
1465 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1466 map = isl_map_read_from_str(ctx, str);
1467 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1469 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1470 map = isl_map_read_from_str(ctx, str);
1471 ai = isl_access_info_add_source(ai, map, 0, &depth);
1473 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1474 map = isl_map_read_from_str(ctx, str);
1475 ai = isl_access_info_add_source(ai, map, 0, &depth);
1477 flow = isl_access_info_compute_flow(ai);
1478 dim = isl_space_alloc(ctx, 0, 3, 3);
1479 mm.must = isl_map_empty(isl_space_copy(dim));
1480 mm.may = isl_map_empty(dim);
1482 isl_flow_foreach(flow, collect_must_may, &mm);
1484 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1485 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1486 assert(map_is_equal(mm.may, str));
1487 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1488 assert(map_is_equal(mm.must, str));
1490 isl_map_free(mm.must);
1491 isl_map_free(mm.may);
1492 isl_flow_free(flow);
1495 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1496 map = isl_map_read_from_str(ctx, str);
1497 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1499 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1500 map = isl_map_read_from_str(ctx, str);
1501 ai = isl_access_info_add_source(ai, map, 0, &depth);
1503 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1504 map = isl_map_read_from_str(ctx, str);
1505 ai = isl_access_info_add_source(ai, map, 0, &depth);
1507 flow = isl_access_info_compute_flow(ai);
1508 dim = isl_space_alloc(ctx, 0, 3, 3);
1509 mm.must = isl_map_empty(isl_space_copy(dim));
1510 mm.may = isl_map_empty(dim);
1512 isl_flow_foreach(flow, collect_must_may, &mm);
1514 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1515 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1516 assert(map_is_equal(mm.may, str));
1517 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1518 assert(map_is_equal(mm.must, str));
1520 isl_map_free(mm.must);
1521 isl_map_free(mm.may);
1522 isl_flow_free(flow);
1525 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1526 map = isl_map_read_from_str(ctx, str);
1527 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1529 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1530 map = isl_map_read_from_str(ctx, str);
1531 ai = isl_access_info_add_source(ai, map, 0, &depth);
1533 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1534 map = isl_map_read_from_str(ctx, str);
1535 ai = isl_access_info_add_source(ai, map, 0, &depth);
1537 flow = isl_access_info_compute_flow(ai);
1538 dim = isl_space_alloc(ctx, 0, 3, 3);
1539 mm.must = isl_map_empty(isl_space_copy(dim));
1540 mm.may = isl_map_empty(dim);
1542 isl_flow_foreach(flow, collect_must_may, &mm);
1544 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1545 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1546 assert(map_is_equal(mm.may, str));
1547 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1548 assert(map_is_equal(mm.must, str));
1550 isl_map_free(mm.must);
1551 isl_map_free(mm.may);
1552 isl_flow_free(flow);
1557 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1558 map = isl_map_read_from_str(ctx, str);
1559 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1561 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1562 map = isl_map_read_from_str(ctx, str);
1563 ai = isl_access_info_add_source(ai, map, 1, &depth);
1565 flow = isl_access_info_compute_flow(ai);
1566 dim = isl_space_alloc(ctx, 0, 5, 5);
1567 mm.must = isl_map_empty(isl_space_copy(dim));
1568 mm.may = isl_map_empty(dim);
1570 isl_flow_foreach(flow, collect_must_may, &mm);
1572 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1573 assert(map_is_equal(mm.must, str));
1574 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1575 assert(map_is_equal(mm.may, str));
1577 isl_map_free(mm.must);
1578 isl_map_free(mm.may);
1579 isl_flow_free(flow);
1582 int test_sv(isl_ctx *ctx)
1586 isl_union_map *umap;
1589 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1590 map = isl_map_read_from_str(ctx, str);
1591 sv = isl_map_is_single_valued(map);
1596 isl_die(ctx, isl_error_internal,
1597 "map not detected as single valued", return -1);
1599 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1600 map = isl_map_read_from_str(ctx, str);
1601 sv = isl_map_is_single_valued(map);
1606 isl_die(ctx, isl_error_internal,
1607 "map detected as single valued", return -1);
1609 str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
1610 umap = isl_union_map_read_from_str(ctx, str);
1611 sv = isl_union_map_is_single_valued(umap);
1612 isl_union_map_free(umap);
1616 isl_die(ctx, isl_error_internal,
1617 "map not detected as single valued", return -1);
1619 str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
1620 umap = isl_union_map_read_from_str(ctx, str);
1621 sv = isl_union_map_is_single_valued(umap);
1622 isl_union_map_free(umap);
1626 isl_die(ctx, isl_error_internal,
1627 "map detected as single valued", return -1);
1632 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1636 map = isl_map_read_from_str(ctx, str);
1638 assert(isl_map_is_bijective(map));
1640 assert(!isl_map_is_bijective(map));
1644 void test_bijective(struct isl_ctx *ctx)
1646 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1647 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1648 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1649 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1650 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1651 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1652 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1653 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1654 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1655 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1656 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1657 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1658 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1661 void test_pwqp(struct isl_ctx *ctx)
1665 isl_pw_qpolynomial *pwqp1, *pwqp2;
1667 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1668 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1670 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1673 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1674 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1676 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1678 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1680 isl_pw_qpolynomial_free(pwqp1);
1682 str = "{ [i] -> i }";
1683 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1684 str = "{ [k] : exists a : k = 2a }";
1685 set = isl_set_read_from_str(ctx, str);
1686 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1687 str = "{ [i] -> i }";
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 + [ (i + [i/3])/2 ] }";
1697 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1699 set = isl_set_read_from_str(ctx, str);
1700 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1701 str = "{ [i] -> 16 }";
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)/2]) }";
1711 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1712 str = "{ [k] : exists a : k = 2a+1 }";
1713 set = isl_set_read_from_str(ctx, str);
1714 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1715 str = "{ [i] -> -1/2 + 1/2 * i }";
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] + [i/2])/5]) }";
1725 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1726 str = "{ [i] -> ([(2 * [i/2])/5]) }";
1727 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1729 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1731 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1733 isl_pw_qpolynomial_free(pwqp1);
1735 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1736 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1737 str = "{ [x] -> x }";
1738 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1740 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1742 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1744 isl_pw_qpolynomial_free(pwqp1);
1746 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
1747 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1748 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1749 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
1750 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1751 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1752 isl_pw_qpolynomial_free(pwqp1);
1755 void test_split_periods(isl_ctx *ctx)
1758 isl_pw_qpolynomial *pwqp;
1760 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1761 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
1762 "U >= 0; [U,V] -> U^2 : U >= 100 }";
1763 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1765 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1768 isl_pw_qpolynomial_free(pwqp);
1771 void test_union(isl_ctx *ctx)
1774 isl_union_set *uset1, *uset2;
1775 isl_union_map *umap1, *umap2;
1777 str = "{ [i] : 0 <= i <= 1 }";
1778 uset1 = isl_union_set_read_from_str(ctx, str);
1779 str = "{ [1] -> [0] }";
1780 umap1 = isl_union_map_read_from_str(ctx, str);
1782 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1783 assert(isl_union_map_is_equal(umap1, umap2));
1785 isl_union_map_free(umap1);
1786 isl_union_map_free(umap2);
1788 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1789 umap1 = isl_union_map_read_from_str(ctx, str);
1790 str = "{ A[i]; B[i] }";
1791 uset1 = isl_union_set_read_from_str(ctx, str);
1793 uset2 = isl_union_map_domain(umap1);
1795 assert(isl_union_set_is_equal(uset1, uset2));
1797 isl_union_set_free(uset1);
1798 isl_union_set_free(uset2);
1801 void test_bound(isl_ctx *ctx)
1804 isl_pw_qpolynomial *pwqp;
1805 isl_pw_qpolynomial_fold *pwf;
1807 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1808 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1809 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1810 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
1811 isl_pw_qpolynomial_fold_free(pwf);
1813 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
1814 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1815 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1816 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
1817 isl_pw_qpolynomial_fold_free(pwf);
1820 void test_lift(isl_ctx *ctx)
1823 isl_basic_map *bmap;
1824 isl_basic_set *bset;
1826 str = "{ [i0] : exists e0 : i0 = 4e0 }";
1827 bset = isl_basic_set_read_from_str(ctx, str);
1828 bset = isl_basic_set_lift(bset);
1829 bmap = isl_basic_map_from_range(bset);
1830 bset = isl_basic_map_domain(bmap);
1831 isl_basic_set_free(bset);
1834 void test_subset(isl_ctx *ctx)
1837 isl_set *set1, *set2;
1839 str = "{ [112, 0] }";
1840 set1 = isl_set_read_from_str(ctx, str);
1841 str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1842 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1843 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
1844 set2 = isl_set_read_from_str(ctx, str);
1845 assert(isl_set_is_subset(set1, set2));
1850 void test_factorize(isl_ctx *ctx)
1853 isl_basic_set *bset;
1856 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
1857 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
1858 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
1859 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
1860 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
1861 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
1862 "3i5 >= -2i0 - i2 + 3i4 }";
1863 bset = isl_basic_set_read_from_str(ctx, str);
1864 f = isl_basic_set_factorizer(bset);
1866 isl_basic_set_free(bset);
1867 isl_factorizer_free(f);
1869 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1870 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
1871 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
1872 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
1873 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
1874 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
1875 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
1876 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
1877 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
1878 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
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);
1886 static int check_injective(__isl_take isl_map *map, void *user)
1888 int *injective = user;
1890 *injective = isl_map_is_injective(map);
1893 if (*injective < 0 || !*injective)
1899 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
1900 const char *r, const char *s, int tilable, int parallel)
1904 isl_union_map *W, *R, *S;
1905 isl_union_map *empty;
1906 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
1907 isl_union_map *validity, *proximity;
1908 isl_union_map *schedule;
1909 isl_union_map *test;
1910 isl_union_set *delta;
1911 isl_union_set *domain;
1915 isl_schedule *sched;
1916 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
1918 D = isl_union_set_read_from_str(ctx, d);
1919 W = isl_union_map_read_from_str(ctx, w);
1920 R = isl_union_map_read_from_str(ctx, r);
1921 S = isl_union_map_read_from_str(ctx, s);
1923 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
1924 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
1926 empty = isl_union_map_empty(isl_union_map_get_space(S));
1927 isl_union_map_compute_flow(isl_union_map_copy(R),
1928 isl_union_map_copy(W), empty,
1929 isl_union_map_copy(S),
1930 &dep_raw, NULL, NULL, NULL);
1931 isl_union_map_compute_flow(isl_union_map_copy(W),
1932 isl_union_map_copy(W),
1933 isl_union_map_copy(R),
1934 isl_union_map_copy(S),
1935 &dep_waw, &dep_war, NULL, NULL);
1937 dep = isl_union_map_union(dep_waw, dep_war);
1938 dep = isl_union_map_union(dep, dep_raw);
1939 validity = isl_union_map_copy(dep);
1940 proximity = isl_union_map_copy(dep);
1942 sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
1943 validity, proximity);
1944 schedule = isl_schedule_get_map(sched);
1945 isl_schedule_free(sched);
1946 isl_union_map_free(W);
1947 isl_union_map_free(R);
1948 isl_union_map_free(S);
1951 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
1953 domain = isl_union_map_domain(isl_union_map_copy(schedule));
1954 is_complete = isl_union_set_is_subset(D, domain);
1955 isl_union_set_free(D);
1956 isl_union_set_free(domain);
1958 test = isl_union_map_reverse(isl_union_map_copy(schedule));
1959 test = isl_union_map_apply_range(test, dep);
1960 test = isl_union_map_apply_range(test, schedule);
1962 delta = isl_union_map_deltas(test);
1963 if (isl_union_set_n_set(delta) == 0) {
1967 isl_union_set_free(delta);
1969 delta_set = isl_set_from_union_set(delta);
1971 slice = isl_set_universe(isl_set_get_space(delta_set));
1972 for (i = 0; i < tilable; ++i)
1973 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
1974 is_tilable = isl_set_is_subset(delta_set, slice);
1975 isl_set_free(slice);
1977 slice = isl_set_universe(isl_set_get_space(delta_set));
1978 for (i = 0; i < parallel; ++i)
1979 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
1980 is_parallel = isl_set_is_subset(delta_set, slice);
1981 isl_set_free(slice);
1983 origin = isl_set_universe(isl_set_get_space(delta_set));
1984 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
1985 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
1987 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
1988 delta_set = isl_set_lexmin(delta_set);
1990 is_nonneg = isl_set_is_equal(delta_set, origin);
1992 isl_set_free(origin);
1993 isl_set_free(delta_set);
1996 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
1997 is_injection < 0 || is_complete < 0)
2000 isl_die(ctx, isl_error_unknown,
2001 "generated schedule incomplete", return -1);
2003 isl_die(ctx, isl_error_unknown,
2004 "generated schedule not injective on each statement",
2007 isl_die(ctx, isl_error_unknown,
2008 "negative dependences in generated schedule",
2011 isl_die(ctx, isl_error_unknown,
2012 "generated schedule not as tilable as expected",
2015 isl_die(ctx, isl_error_unknown,
2016 "generated schedule not as parallel as expected",
2022 int test_special_schedule(isl_ctx *ctx)
2026 isl_union_map *empty;
2028 isl_union_map *sched1, *sched2;
2029 isl_schedule *schedule;
2032 str = "{ S[i,j] : 0 <= i <= 10 }";
2033 dom = isl_union_set_read_from_str(ctx, str);
2034 str = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2035 dep = isl_union_map_read_from_str(ctx, str);
2036 empty = isl_union_map_read_from_str(ctx, "{}");
2037 schedule = isl_union_set_compute_schedule(dom, empty, dep);
2038 sched1 = isl_schedule_get_map(schedule);
2039 isl_schedule_free(schedule);
2041 str = "{ S[i, j] -> [j, i] }";
2042 sched2 = isl_union_map_read_from_str(ctx, str);
2044 equal = isl_union_map_is_equal(sched1, sched2);
2045 isl_union_map_free(sched1);
2046 isl_union_map_free(sched2);
2051 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2057 int test_schedule(isl_ctx *ctx)
2059 const char *D, *W, *R, *S;
2062 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2063 W = "{ S1[t,i] -> a[t,i] }";
2064 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2065 "S1[t,i] -> a[t-1,i+1] }";
2066 S = "{ S1[t,i] -> [t,i] }";
2067 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2070 /* Fig. 5 of CC2008 */
2071 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2073 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2074 "j >= 2 and j <= -1 + N }";
2075 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2076 "j >= 2 and j <= -1 + N; "
2077 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2078 "j >= 2 and j <= -1 + N }";
2079 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2080 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2083 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2084 W = "{ S1[i] -> a[i] }";
2085 R = "{ S2[i] -> a[i+1] }";
2086 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2087 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2090 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2091 W = "{ S1[i] -> a[i] }";
2092 R = "{ S2[i] -> a[9-i] }";
2093 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2094 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2097 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2098 W = "{ S1[i] -> a[i] }";
2099 R = "[N] -> { S2[i] -> a[N-1-i] }";
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]; S2[i] -> b[i] }";
2106 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2107 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2108 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2111 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2112 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2113 R = "{ S2[i,j] -> a[i-1,j] }";
2114 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2115 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2118 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2119 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2120 R = "{ S2[i,j] -> a[i,j-1] }";
2121 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2122 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2125 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2126 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2127 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2128 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2129 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2130 "S_0[] -> [0, 0, 0] }";
2131 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2133 ctx->opt->schedule_parametric = 0;
2134 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2136 ctx->opt->schedule_parametric = 1;
2138 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2139 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2140 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2141 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2142 "S4[i] -> a[i,N] }";
2143 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2144 "S4[i] -> [4,i,0] }";
2145 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2148 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2149 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2151 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2153 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2155 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2156 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2159 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2160 " S_2[t] : t >= 0 and t <= -1 + N; "
2161 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2163 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2164 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2165 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2166 "i >= 0 and i <= -1 + N }";
2167 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2168 "i >= 0 and i <= -1 + N; "
2169 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2170 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2171 " S_0[t] -> [0, t, 0] }";
2173 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2175 ctx->opt->schedule_parametric = 0;
2176 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2178 ctx->opt->schedule_parametric = 1;
2180 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2181 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2182 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2185 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2186 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2187 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2188 "j >= 0 and j <= -1 + N; "
2189 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2190 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2191 "j >= 0 and j <= -1 + N; "
2192 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2193 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2194 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2197 D = "{ S_0[i] : i >= 0 }";
2198 W = "{ S_0[i] -> a[i] : i >= 0 }";
2199 R = "{ S_0[i] -> a[0] : i >= 0 }";
2200 S = "{ S_0[i] -> [0, i, 0] }";
2201 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2204 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2205 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2206 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2207 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2208 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2211 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2212 "k <= -1 + n and k >= 0 }";
2213 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
2214 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2215 "k <= -1 + n and k >= 0; "
2216 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2217 "k <= -1 + n and k >= 0; "
2218 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2219 "k <= -1 + n and k >= 0 }";
2220 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2221 ctx->opt->schedule_outer_zero_distance = 1;
2222 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2224 ctx->opt->schedule_outer_zero_distance = 0;
2226 return test_special_schedule(ctx);
2229 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2231 isl_union_map *umap;
2234 umap = isl_union_map_read_from_str(ctx, str);
2235 test = isl_union_map_plain_is_injective(umap);
2236 isl_union_map_free(umap);
2239 if (test == injective)
2242 isl_die(ctx, isl_error_unknown,
2243 "map not detected as injective", return -1);
2245 isl_die(ctx, isl_error_unknown,
2246 "map detected as injective", return -1);
2249 int test_injective(isl_ctx *ctx)
2253 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2255 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2257 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2259 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2261 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2263 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2265 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2267 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2270 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2271 if (test_plain_injective(ctx, str, 1))
2273 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2274 if (test_plain_injective(ctx, str, 0))
2280 int test_aff(isl_ctx *ctx)
2285 isl_local_space *ls;
2289 dim = isl_space_set_alloc(ctx, 0, 1);
2290 ls = isl_local_space_from_space(dim);
2291 aff = isl_aff_zero_on_domain(ls);
2293 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2294 aff = isl_aff_scale_down_ui(aff, 3);
2295 aff = isl_aff_floor(aff);
2296 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2297 aff = isl_aff_scale_down_ui(aff, 2);
2298 aff = isl_aff_floor(aff);
2299 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2302 set = isl_set_read_from_str(ctx, str);
2303 aff = isl_aff_gist(aff, set);
2305 aff = isl_aff_add_constant_si(aff, -16);
2306 zero = isl_aff_plain_is_zero(aff);
2312 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2317 int test_dim_max(isl_ctx *ctx)
2321 isl_set *set1, *set2;
2326 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
2327 set = isl_set_read_from_str(ctx, str);
2328 pwaff = isl_set_dim_max(set, 0);
2329 set1 = isl_set_from_pw_aff(pwaff);
2330 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
2331 set2 = isl_set_read_from_str(ctx, str);
2332 equal = isl_set_is_equal(set1, set2);
2338 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2340 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
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] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
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 <= 2N or 0 <= i <= 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,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
2369 "0 <= i < N and 0 <= j < M }";
2370 map = isl_map_read_from_str(ctx, str);
2371 set = isl_map_range(map);
2373 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
2374 set1 = isl_set_from_pw_aff(pwaff);
2375 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
2376 set2 = isl_set_read_from_str(ctx, str);
2377 equal = isl_set_is_equal(set1, set2);
2381 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
2382 set1 = isl_set_from_pw_aff(pwaff);
2383 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
2384 set2 = isl_set_read_from_str(ctx, str);
2385 if (equal >= 0 && equal)
2386 equal = isl_set_is_equal(set1, set2);
2395 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2400 int test_product(isl_ctx *ctx)
2407 set = isl_set_read_from_str(ctx, str);
2408 set = isl_set_product(set, isl_set_copy(set));
2409 ok = isl_set_is_wrapping(set);
2414 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2419 int test_equal(isl_ctx *ctx)
2422 isl_set *set, *set2;
2426 set = isl_set_read_from_str(ctx, str);
2428 set2 = isl_set_read_from_str(ctx, str);
2429 equal = isl_set_is_equal(set, set2);
2435 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2440 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
2441 enum isl_dim_type type, unsigned pos, int fixed)
2445 test = isl_map_plain_is_fixed(map, type, pos, NULL);
2452 isl_die(ctx, isl_error_unknown,
2453 "map not detected as fixed", return -1);
2455 isl_die(ctx, isl_error_unknown,
2456 "map detected as fixed", return -1);
2459 int test_fixed(isl_ctx *ctx)
2464 str = "{ [i] -> [i] }";
2465 map = isl_map_read_from_str(ctx, str);
2466 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
2468 str = "{ [i] -> [1] }";
2469 map = isl_map_read_from_str(ctx, str);
2470 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2472 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
2473 map = isl_map_read_from_str(ctx, str);
2474 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2476 map = isl_map_read_from_str(ctx, str);
2477 map = isl_map_neg(map);
2478 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2486 struct isl_ctx *ctx;
2488 srcdir = getenv("srcdir");
2491 ctx = isl_ctx_alloc();
2492 if (test_fixed(ctx) < 0)
2494 if (test_equal(ctx) < 0)
2496 if (test_product(ctx) < 0)
2498 if (test_dim_max(ctx) < 0)
2500 if (test_aff(ctx) < 0)
2502 if (test_injective(ctx) < 0)
2504 if (test_schedule(ctx) < 0)
2506 test_factorize(ctx);
2511 test_split_periods(ctx);
2515 if (test_sv(ctx) < 0)
2517 test_bijective(ctx);
2521 test_construction(ctx);
2524 test_application(ctx);
2525 test_affine_hull(ctx);
2526 test_convex_hull(ctx);