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, -1);
52 void test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
56 map = isl_map_read_from_str(ctx, str, -1);
57 map2 = isl_map_read_from_str(ctx, str2, -1);
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, -1);
82 str = "{ A[i] -> L[([i/3])] }";
83 map = isl_map_read_from_str(ctx, str, -1);
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, -1);
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, -1);
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, -1);
113 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
114 map2 = isl_map_read_from_str(ctx, str, -1);
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 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
132 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
135 void test_read(struct isl_ctx *ctx)
139 struct isl_basic_set *bset1, *bset2;
140 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
142 filename = get_filename(ctx, "set", "omega");
144 input = fopen(filename, "r");
147 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
148 bset2 = isl_basic_set_read_from_str(ctx, str, 0);
150 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
152 isl_basic_set_free(bset1);
153 isl_basic_set_free(bset2);
159 void test_bounded(struct isl_ctx *ctx)
164 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }", -1);
165 bounded = isl_set_is_bounded(set);
169 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }", -1);
170 bounded = isl_set_is_bounded(set);
174 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }", -1);
175 bounded = isl_set_is_bounded(set);
180 /* Construct the basic set { [i] : 5 <= i <= N } */
181 void test_construction(struct isl_ctx *ctx)
185 struct isl_basic_set *bset;
186 struct isl_constraint *c;
190 dim = isl_dim_set_alloc(ctx, 1, 1);
191 bset = isl_basic_set_universe(dim);
193 c = isl_inequality_alloc(isl_basic_set_get_dim(bset));
194 isl_int_set_si(v, -1);
195 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
196 isl_int_set_si(v, 1);
197 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
198 bset = isl_basic_set_add_constraint(bset, c);
200 c = isl_inequality_alloc(isl_basic_set_get_dim(bset));
201 isl_int_set_si(v, 1);
202 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
203 isl_int_set_si(v, -5);
204 isl_constraint_set_constant(c, v);
205 bset = isl_basic_set_add_constraint(bset, c);
207 isl_basic_set_free(bset);
212 void test_dim(struct isl_ctx *ctx)
215 isl_map *map1, *map2;
217 map1 = isl_map_read_from_str(ctx,
218 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
219 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
220 map2 = isl_map_read_from_str(ctx,
221 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
222 assert(isl_map_is_equal(map1, map2));
225 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
226 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }", -1);
227 assert(isl_map_is_equal(map1, map2));
232 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
233 map1 = isl_map_read_from_str(ctx, str, -1);
234 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
235 map2 = isl_map_read_from_str(ctx, str, -1);
236 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
237 assert(isl_map_is_equal(map1, map2));
243 void test_div(struct isl_ctx *ctx)
247 struct isl_basic_set *bset;
248 struct isl_constraint *c;
253 dim = isl_dim_set_alloc(ctx, 0, 3);
254 bset = isl_basic_set_universe(dim);
256 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
257 isl_int_set_si(v, -1);
258 isl_constraint_set_constant(c, v);
259 isl_int_set_si(v, 1);
260 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
261 isl_int_set_si(v, 3);
262 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
263 bset = isl_basic_set_add_constraint(bset, c);
265 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
266 isl_int_set_si(v, 1);
267 isl_constraint_set_constant(c, v);
268 isl_int_set_si(v, -1);
269 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
270 isl_int_set_si(v, 3);
271 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
272 bset = isl_basic_set_add_constraint(bset, c);
274 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
276 assert(bset && bset->n_div == 1);
277 isl_basic_set_free(bset);
280 dim = isl_dim_set_alloc(ctx, 0, 3);
281 bset = isl_basic_set_universe(dim);
283 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
284 isl_int_set_si(v, 1);
285 isl_constraint_set_constant(c, v);
286 isl_int_set_si(v, -1);
287 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
288 isl_int_set_si(v, 3);
289 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
290 bset = isl_basic_set_add_constraint(bset, c);
292 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
293 isl_int_set_si(v, -1);
294 isl_constraint_set_constant(c, v);
295 isl_int_set_si(v, 1);
296 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
297 isl_int_set_si(v, 3);
298 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
299 bset = isl_basic_set_add_constraint(bset, c);
301 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
303 assert(bset && bset->n_div == 1);
304 isl_basic_set_free(bset);
307 dim = isl_dim_set_alloc(ctx, 0, 3);
308 bset = isl_basic_set_universe(dim);
310 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
311 isl_int_set_si(v, 1);
312 isl_constraint_set_constant(c, v);
313 isl_int_set_si(v, -1);
314 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
315 isl_int_set_si(v, 3);
316 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
317 bset = isl_basic_set_add_constraint(bset, c);
319 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
320 isl_int_set_si(v, -3);
321 isl_constraint_set_constant(c, v);
322 isl_int_set_si(v, 1);
323 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
324 isl_int_set_si(v, 4);
325 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
326 bset = isl_basic_set_add_constraint(bset, c);
328 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
330 assert(bset && bset->n_div == 1);
331 isl_basic_set_free(bset);
334 dim = isl_dim_set_alloc(ctx, 0, 3);
335 bset = isl_basic_set_universe(dim);
337 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
338 isl_int_set_si(v, 2);
339 isl_constraint_set_constant(c, v);
340 isl_int_set_si(v, -1);
341 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
342 isl_int_set_si(v, 3);
343 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
344 bset = isl_basic_set_add_constraint(bset, c);
346 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
347 isl_int_set_si(v, -1);
348 isl_constraint_set_constant(c, v);
349 isl_int_set_si(v, 1);
350 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
351 isl_int_set_si(v, 6);
352 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
353 bset = isl_basic_set_add_constraint(bset, c);
355 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
357 assert(isl_basic_set_is_empty(bset));
358 isl_basic_set_free(bset);
361 dim = isl_dim_set_alloc(ctx, 0, 3);
362 bset = isl_basic_set_universe(dim);
364 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
365 isl_int_set_si(v, -1);
366 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
367 isl_int_set_si(v, 3);
368 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
369 bset = isl_basic_set_add_constraint(bset, c);
371 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
372 isl_int_set_si(v, 1);
373 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
374 isl_int_set_si(v, -3);
375 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
376 bset = isl_basic_set_add_constraint(bset, c);
378 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
380 assert(bset && bset->n_div == 0);
381 isl_basic_set_free(bset);
384 dim = isl_dim_set_alloc(ctx, 0, 3);
385 bset = isl_basic_set_universe(dim);
387 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
388 isl_int_set_si(v, -1);
389 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
390 isl_int_set_si(v, 6);
391 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
392 bset = isl_basic_set_add_constraint(bset, c);
394 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
395 isl_int_set_si(v, 1);
396 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
397 isl_int_set_si(v, -3);
398 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
399 bset = isl_basic_set_add_constraint(bset, c);
401 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
403 assert(bset && bset->n_div == 1);
404 isl_basic_set_free(bset);
407 /* This test is a bit tricky. We set up an equality
409 * Normalization of divs creates _two_ divs
412 * Afterwards e0 is removed again because it has coefficient -1
413 * and we end up with the original equality and div again.
414 * Perhaps we can avoid the introduction of this temporary div.
416 dim = isl_dim_set_alloc(ctx, 0, 4);
417 bset = isl_basic_set_universe(dim);
419 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
420 isl_int_set_si(v, -1);
421 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
422 isl_int_set_si(v, -3);
423 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
424 isl_int_set_si(v, -3);
425 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
426 isl_int_set_si(v, 6);
427 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
428 bset = isl_basic_set_add_constraint(bset, c);
430 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
432 /* Test disabled for now */
434 assert(bset && bset->n_div == 1);
436 isl_basic_set_free(bset);
439 dim = isl_dim_set_alloc(ctx, 0, 5);
440 bset = isl_basic_set_universe(dim);
442 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
443 isl_int_set_si(v, -1);
444 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
445 isl_int_set_si(v, -3);
446 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
447 isl_int_set_si(v, -3);
448 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
449 isl_int_set_si(v, 6);
450 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
451 bset = isl_basic_set_add_constraint(bset, c);
453 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
454 isl_int_set_si(v, -1);
455 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
456 isl_int_set_si(v, 1);
457 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
458 isl_int_set_si(v, 1);
459 isl_constraint_set_constant(c, v);
460 bset = isl_basic_set_add_constraint(bset, c);
462 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
464 /* Test disabled for now */
466 assert(bset && bset->n_div == 1);
468 isl_basic_set_free(bset);
471 dim = isl_dim_set_alloc(ctx, 0, 4);
472 bset = isl_basic_set_universe(dim);
474 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
475 isl_int_set_si(v, 1);
476 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
477 isl_int_set_si(v, -1);
478 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
479 isl_int_set_si(v, -2);
480 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
481 bset = isl_basic_set_add_constraint(bset, c);
483 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
484 isl_int_set_si(v, -1);
485 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
486 isl_int_set_si(v, 3);
487 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
488 isl_int_set_si(v, 2);
489 isl_constraint_set_constant(c, v);
490 bset = isl_basic_set_add_constraint(bset, c);
492 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
494 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
496 assert(!isl_basic_set_is_empty(bset));
498 isl_basic_set_free(bset);
501 dim = isl_dim_set_alloc(ctx, 0, 3);
502 bset = isl_basic_set_universe(dim);
504 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
505 isl_int_set_si(v, 1);
506 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
507 isl_int_set_si(v, -2);
508 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
509 bset = isl_basic_set_add_constraint(bset, c);
511 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
513 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
515 isl_basic_set_free(bset);
520 void test_application_case(struct isl_ctx *ctx, const char *name)
524 struct isl_basic_set *bset1, *bset2;
525 struct isl_basic_map *bmap;
527 filename = get_filename(ctx, name, "omega");
529 input = fopen(filename, "r");
532 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
533 bmap = isl_basic_map_read_from_file(ctx, input, 0);
535 bset1 = isl_basic_set_apply(bset1, bmap);
537 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
539 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
541 isl_basic_set_free(bset1);
542 isl_basic_set_free(bset2);
548 void test_application(struct isl_ctx *ctx)
550 test_application_case(ctx, "application");
551 test_application_case(ctx, "application2");
554 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
558 struct isl_basic_set *bset1, *bset2;
560 filename = get_filename(ctx, name, "polylib");
562 input = fopen(filename, "r");
565 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
566 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
568 bset1 = isl_basic_set_affine_hull(bset1);
570 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
572 isl_basic_set_free(bset1);
573 isl_basic_set_free(bset2);
579 void test_affine_hull(struct isl_ctx *ctx)
581 test_affine_hull_case(ctx, "affine2");
582 test_affine_hull_case(ctx, "affine");
583 test_affine_hull_case(ctx, "affine3");
586 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
590 struct isl_basic_set *bset1, *bset2;
593 filename = get_filename(ctx, name, "polylib");
595 input = fopen(filename, "r");
598 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
599 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
601 set = isl_basic_set_union(bset1, bset2);
602 bset1 = isl_set_convex_hull(set);
604 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
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_convex_hull_algo(struct isl_ctx *ctx, int convex)
617 const char *str1, *str2;
618 isl_set *set1, *set2;
619 int orig_convex = ctx->opt->convex;
620 ctx->opt->convex = convex;
622 test_convex_hull_case(ctx, "convex0");
623 test_convex_hull_case(ctx, "convex1");
624 test_convex_hull_case(ctx, "convex2");
625 test_convex_hull_case(ctx, "convex3");
626 test_convex_hull_case(ctx, "convex4");
627 test_convex_hull_case(ctx, "convex5");
628 test_convex_hull_case(ctx, "convex6");
629 test_convex_hull_case(ctx, "convex7");
630 test_convex_hull_case(ctx, "convex8");
631 test_convex_hull_case(ctx, "convex9");
632 test_convex_hull_case(ctx, "convex10");
633 test_convex_hull_case(ctx, "convex11");
634 test_convex_hull_case(ctx, "convex12");
635 test_convex_hull_case(ctx, "convex13");
636 test_convex_hull_case(ctx, "convex14");
637 test_convex_hull_case(ctx, "convex15");
639 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
640 "(i0 = 1 and i1 = 0 and i2 = 1) or "
641 "(i0 = 0 and i1 = 0 and i2 = 0) }";
642 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
643 set1 = isl_set_read_from_str(ctx, str1, -1);
644 set2 = isl_set_read_from_str(ctx, str2, -1);
645 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
646 assert(isl_set_is_equal(set1, set2));
650 ctx->opt->convex = orig_convex;
653 void test_convex_hull(struct isl_ctx *ctx)
655 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
656 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
659 void test_gist_case(struct isl_ctx *ctx, const char *name)
663 struct isl_basic_set *bset1, *bset2;
665 filename = get_filename(ctx, name, "polylib");
667 input = fopen(filename, "r");
670 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
671 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
673 bset1 = isl_basic_set_gist(bset1, bset2);
675 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
677 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
679 isl_basic_set_free(bset1);
680 isl_basic_set_free(bset2);
686 void test_gist(struct isl_ctx *ctx)
689 isl_basic_set *bset1, *bset2;
691 test_gist_case(ctx, "gist1");
693 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
694 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
695 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
696 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
697 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
698 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
699 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
700 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
701 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
702 bset1 = isl_basic_set_read_from_str(ctx, str, -1);
703 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
704 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
705 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
706 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
707 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
708 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
709 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
710 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
711 bset2 = isl_basic_set_read_from_str(ctx, str, -1);
712 bset1 = isl_basic_set_gist(bset1, bset2);
713 assert(bset1 && bset1->n_div == 0);
714 isl_basic_set_free(bset1);
717 void test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
721 set = isl_set_read_from_str(ctx, str, -1);
722 set = isl_set_coalesce(set);
723 set2 = isl_set_read_from_str(ctx, str, -1);
724 assert(isl_set_is_equal(set, set2));
726 assert(set && set->n == 1);
731 void test_coalesce(struct isl_ctx *ctx)
734 struct isl_set *set, *set2;
735 struct isl_map *map, *map2;
737 set = isl_set_read_from_str(ctx,
738 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
739 "y >= x & x >= 2 & 5 >= y }", -1);
740 set = isl_set_coalesce(set);
741 assert(set && set->n == 1);
744 set = isl_set_read_from_str(ctx,
745 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
746 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}", -1);
747 set = isl_set_coalesce(set);
748 assert(set && set->n == 1);
751 set = isl_set_read_from_str(ctx,
752 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
753 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}", -1);
754 set = isl_set_coalesce(set);
755 assert(set && set->n == 2);
758 set = isl_set_read_from_str(ctx,
759 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
760 "y >= 0 & x >= 6 & x <= 10 & y <= x}", -1);
761 set = isl_set_coalesce(set);
762 assert(set && set->n == 1);
765 set = isl_set_read_from_str(ctx,
766 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
767 "y >= 0 & x >= 7 & x <= 10 & y <= x}", -1);
768 set = isl_set_coalesce(set);
769 assert(set && set->n == 2);
772 set = isl_set_read_from_str(ctx,
773 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
774 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}", -1);
775 set = isl_set_coalesce(set);
776 assert(set && set->n == 2);
779 set = isl_set_read_from_str(ctx,
780 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
781 "y >= 0 & x = 6 & y <= 6}", -1);
782 set = isl_set_coalesce(set);
783 assert(set && set->n == 1);
786 set = isl_set_read_from_str(ctx,
787 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
788 "y >= 0 & x = 7 & y <= 6}", -1);
789 set = isl_set_coalesce(set);
790 assert(set && set->n == 2);
793 set = isl_set_read_from_str(ctx,
794 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
795 "y >= 0 & x = 6 & y <= 5}", -1);
796 set = isl_set_coalesce(set);
797 assert(set && set->n == 1);
798 set2 = isl_set_read_from_str(ctx,
799 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
800 "y >= 0 & x = 6 & y <= 5}", -1);
801 assert(isl_set_is_equal(set, set2));
805 set = isl_set_read_from_str(ctx,
806 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
807 "y >= 0 & x = 6 & y <= 7}", -1);
808 set = isl_set_coalesce(set);
809 assert(set && set->n == 2);
812 set = isl_set_read_from_str(ctx,
813 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
814 set = isl_set_coalesce(set);
815 assert(set && set->n == 1);
816 set2 = isl_set_read_from_str(ctx,
817 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
818 assert(isl_set_is_equal(set, set2));
822 set = isl_set_read_from_str(ctx,
823 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
824 set = isl_set_coalesce(set);
825 set2 = isl_set_read_from_str(ctx,
826 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
827 assert(isl_set_is_equal(set, set2));
831 set = isl_set_read_from_str(ctx,
832 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
833 "2 <= i and i <= n }", -1);
834 set = isl_set_coalesce(set);
835 assert(set && set->n == 1);
836 set2 = isl_set_read_from_str(ctx,
837 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
838 "2 <= i and i <= n }", -1);
839 assert(isl_set_is_equal(set, set2));
843 map = isl_map_read_from_str(ctx,
844 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
845 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
846 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
847 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
848 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
849 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
850 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
851 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
852 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
853 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
854 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
855 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
856 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
857 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
858 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
859 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
860 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
861 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
862 map = isl_map_coalesce(map);
863 map2 = isl_map_read_from_str(ctx,
864 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
865 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
866 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
867 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
868 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
869 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
870 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
871 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
872 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
873 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
874 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
875 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
876 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
877 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
878 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
879 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
880 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
881 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
882 assert(isl_map_is_equal(map, map2));
886 str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
887 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
888 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
889 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
890 map = isl_map_read_from_str(ctx, str, -1);
891 map = isl_map_coalesce(map);
892 map2 = isl_map_read_from_str(ctx, str, -1);
893 assert(isl_map_is_equal(map, map2));
897 str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
898 "[o0, o1, o2, o3, o4, o5, o6] : "
899 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
900 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
901 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
902 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
903 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
904 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
905 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
906 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
907 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
908 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
909 "o6 >= i3 + i6 - o3 and M >= 0 and "
910 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
911 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
912 map = isl_map_read_from_str(ctx, str, -1);
913 map = isl_map_coalesce(map);
914 map2 = isl_map_read_from_str(ctx, str, -1);
915 assert(isl_map_is_equal(map, map2));
919 str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
920 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
921 "(o0 = 0 and M >= 2 and N >= 3) or "
922 "(M = 0 and o0 = 0 and N >= 3) }";
923 map = isl_map_read_from_str(ctx, str, -1);
924 map = isl_map_coalesce(map);
925 map2 = isl_map_read_from_str(ctx, str, -1);
926 assert(isl_map_is_equal(map, map2));
930 str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
931 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
932 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
933 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
934 map = isl_map_read_from_str(ctx, str, -1);
935 map = isl_map_coalesce(map);
936 map2 = isl_map_read_from_str(ctx, str, -1);
937 assert(isl_map_is_equal(map, map2));
941 test_coalesce_set(ctx,
942 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
943 "(i1 = M and M >= 1) }", 0);
944 test_coalesce_set(ctx,
945 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
946 test_coalesce_set(ctx,
947 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
948 "(y = 3 and x = 1) }", 1);
949 test_coalesce_set(ctx,
950 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
951 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
952 "i1 <= M and i3 <= M and i4 <= M) or "
953 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
954 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
955 "i4 <= -1 + M) }", 1);
956 test_coalesce_set(ctx,
957 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
958 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
959 test_coalesce_set(ctx,
960 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
961 "-x - y + 1 >= 0 and -3 <= z <= 3;"
962 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
963 "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
964 test_coalesce_set(ctx,
965 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1);
966 test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0);
967 test_coalesce_set(ctx,
968 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1);
969 test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1);
970 test_coalesce_set(ctx, "{ [0,0]; [i,i] : 1 <= i <= 10 }", 1);
971 test_coalesce_set(ctx, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }", 0);
972 test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 1 <= i <= 10 }", 1);
973 test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 2 <= i <= 10 }", 0);
974 test_coalesce_set(ctx, "{ [1,0]; [i,2i] : 1 <= i <= 10 }", 0);
975 test_coalesce_set(ctx, "{ [0,1]; [i,2i] : 1 <= i <= 10 }", 0);
978 void test_closure(struct isl_ctx *ctx)
986 /* COCOA example 1 */
987 map = isl_map_read_from_str(ctx,
988 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
989 "1 <= i and i < n and 1 <= j and j < n or "
990 "i2 = i + 1 and j2 = j - 1 and "
991 "1 <= i and i < n and 2 <= j and j <= n }", -1);
992 map = isl_map_power(map, &exact);
996 /* COCOA example 1 */
997 map = isl_map_read_from_str(ctx,
998 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
999 "1 <= i and i < n and 1 <= j and j < n or "
1000 "i2 = i + 1 and j2 = j - 1 and "
1001 "1 <= i and i < n and 2 <= j and j <= n }", -1);
1002 map = isl_map_transitive_closure(map, &exact);
1004 map2 = isl_map_read_from_str(ctx,
1005 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1006 "1 <= i and i < n and 1 <= j and j <= n and "
1007 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1008 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1009 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}", -1);
1010 assert(isl_map_is_equal(map, map2));
1014 map = isl_map_read_from_str(ctx,
1015 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1016 " 0 <= y and y <= n }", -1);
1017 map = isl_map_transitive_closure(map, &exact);
1018 map2 = isl_map_read_from_str(ctx,
1019 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1020 " 0 <= y and y <= n }", -1);
1021 assert(isl_map_is_equal(map, map2));
1025 /* COCOA example 2 */
1026 map = isl_map_read_from_str(ctx,
1027 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1028 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1029 "i2 = i + 2 and j2 = j - 2 and "
1030 "1 <= i and i < n - 1 and 3 <= j and j <= n }", -1);
1031 map = isl_map_transitive_closure(map, &exact);
1033 map2 = isl_map_read_from_str(ctx,
1034 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1035 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1036 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1037 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1038 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }", -1);
1039 assert(isl_map_is_equal(map, map2));
1043 /* COCOA Fig.2 left */
1044 map = isl_map_read_from_str(ctx,
1045 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1046 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1048 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1049 "j <= 2 i - 3 and j <= n - 2 or "
1050 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1051 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
1052 map = isl_map_transitive_closure(map, &exact);
1056 /* COCOA Fig.2 right */
1057 map = isl_map_read_from_str(ctx,
1058 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1059 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1061 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1062 "j <= 2 i - 4 and j <= n - 3 or "
1063 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1064 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
1065 map = isl_map_power(map, &exact);
1069 /* COCOA Fig.2 right */
1070 map = isl_map_read_from_str(ctx,
1071 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1072 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1074 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1075 "j <= 2 i - 4 and j <= n - 3 or "
1076 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1077 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
1078 map = isl_map_transitive_closure(map, &exact);
1080 map2 = isl_map_read_from_str(ctx,
1081 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1082 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1083 "j <= n and 3 + i + 2 j <= 3 n and "
1084 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1085 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1086 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1087 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1088 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }", -1);
1089 assert(isl_map_is_equal(map, map2));
1093 /* COCOA Fig.1 right */
1094 dom = isl_set_read_from_str(ctx,
1095 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1096 "2 x - 3 y + 3 >= 0 }", -1);
1097 right = isl_map_read_from_str(ctx,
1098 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }", -1);
1099 up = isl_map_read_from_str(ctx,
1100 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }", -1);
1101 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1102 right = isl_map_intersect_range(right, isl_set_copy(dom));
1103 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1104 up = isl_map_intersect_range(up, dom);
1105 map = isl_map_union(up, right);
1106 map = isl_map_transitive_closure(map, &exact);
1108 map2 = isl_map_read_from_str(ctx,
1109 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1110 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }", -1);
1111 assert(isl_map_is_equal(map, map2));
1115 /* COCOA Theorem 1 counter example */
1116 map = isl_map_read_from_str(ctx,
1117 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1118 "i2 = 1 and j2 = j or "
1119 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }", -1);
1120 map = isl_map_transitive_closure(map, &exact);
1124 map = isl_map_read_from_str(ctx,
1125 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1126 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1127 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1128 "1 <= i,i2 <= n and 1 <= j,j2 <= m }", -1);
1129 map = isl_map_transitive_closure(map, &exact);
1133 /* Kelly et al 1996, fig 12 */
1134 map = isl_map_read_from_str(ctx,
1135 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1136 "1 <= i,j,j+1 <= n or "
1137 "j = n and j2 = 1 and i2 = i + 1 and "
1138 "1 <= i,i+1 <= n }", -1);
1139 map = isl_map_transitive_closure(map, &exact);
1141 map2 = isl_map_read_from_str(ctx,
1142 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1143 "1 <= i <= n and i = i2 or "
1144 "1 <= i < i2 <= n and 1 <= j <= n and "
1145 "1 <= j2 <= n }", -1);
1146 assert(isl_map_is_equal(map, map2));
1150 /* Omega's closure4 */
1151 map = isl_map_read_from_str(ctx,
1152 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1153 "1 <= x,y <= 10 or "
1154 "x2 = x + 1 and y2 = y and "
1155 "1 <= x <= 20 && 5 <= y <= 15 }", -1);
1156 map = isl_map_transitive_closure(map, &exact);
1160 map = isl_map_read_from_str(ctx,
1161 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }", -1);
1162 map = isl_map_transitive_closure(map, &exact);
1164 map2 = isl_map_read_from_str(ctx,
1165 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }", -1);
1166 assert(isl_map_is_equal(map, map2));
1170 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1171 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1172 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1173 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1174 map = isl_map_read_from_str(ctx, str, -1);
1175 map = isl_map_transitive_closure(map, &exact);
1177 map2 = isl_map_read_from_str(ctx, str, -1);
1178 assert(isl_map_is_equal(map, map2));
1182 str = "{[0] -> [1]; [2] -> [3]}";
1183 map = isl_map_read_from_str(ctx, str, -1);
1184 map = isl_map_transitive_closure(map, &exact);
1186 map2 = isl_map_read_from_str(ctx, str, -1);
1187 assert(isl_map_is_equal(map, map2));
1191 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1192 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1193 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1194 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1195 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1196 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1197 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1198 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1199 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1200 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1201 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1202 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1203 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1204 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1205 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1206 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1207 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1208 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1209 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1210 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1211 map = isl_map_read_from_str(ctx, str, -1);
1212 map = isl_map_transitive_closure(map, NULL);
1217 void test_lex(struct isl_ctx *ctx)
1222 dim = isl_dim_alloc(ctx, 0, 0, 0);
1223 map = isl_map_lex_le(dim);
1224 assert(!isl_map_is_empty(map));
1228 static int consume_lexmin(__isl_take isl_basic_set *dom,
1229 __isl_take isl_aff_list *list, void *user)
1232 isl_basic_map *bmap;
1233 isl_map **map = user;
1235 dim = isl_basic_set_get_dim(dom);
1236 bmap = isl_basic_map_from_aff_list(dim, list);
1237 bmap = isl_basic_map_intersect_domain(bmap, dom);
1239 *map = isl_map_union(*map, isl_map_from_basic_map(bmap));
1244 void test_lexmin(struct isl_ctx *ctx)
1247 isl_basic_map *bmap;
1248 isl_map *map, *map2;
1252 str = "[p0, p1] -> { [] -> [] : "
1253 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1254 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1255 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1256 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1257 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1258 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1259 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1260 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1261 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1262 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1263 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1264 map = isl_map_read_from_str(ctx, str, -1);
1265 map = isl_map_lexmin(map);
1268 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1269 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1270 set = isl_set_read_from_str(ctx, str, -1);
1271 set = isl_set_lexmax(set);
1272 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1273 set2 = isl_set_read_from_str(ctx, str, -1);
1274 set = isl_set_intersect(set, set2);
1275 assert(!isl_set_is_empty(set));
1278 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1279 map = isl_map_read_from_str(ctx, str, -1);
1280 map = isl_map_lexmin(map);
1281 str = "{ [x] -> [5] : 6 <= x <= 8; "
1282 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1283 map2 = isl_map_read_from_str(ctx, str, -1);
1284 assert(isl_map_is_equal(map, map2));
1288 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1289 map = isl_map_read_from_str(ctx, str, -1);
1290 map2 = isl_map_copy(map);
1291 map = isl_map_lexmin(map);
1292 assert(isl_map_is_equal(map, map2));
1296 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1297 map = isl_map_read_from_str(ctx, str, -1);
1298 map = isl_map_lexmin(map);
1299 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1300 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1301 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1302 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1303 map2 = isl_map_read_from_str(ctx, str, -1);
1304 assert(isl_map_is_equal(map, map2));
1308 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1309 " 8i' <= i and 8i' >= -7 + i }";
1310 bmap = isl_basic_map_read_from_str(ctx, str, -1);
1311 map2 = isl_map_empty(isl_basic_map_get_dim(bmap));
1312 isl_basic_map_foreach_lexmin(bmap, &consume_lexmin, &map2);
1313 map = isl_map_from_basic_map(bmap);
1314 assert(isl_map_is_equal(map, map2));
1324 static int collect_must_may(__isl_take isl_map *dep, int must,
1325 void *dep_user, void *user)
1327 struct must_may *mm = (struct must_may *)user;
1330 mm->must = isl_map_union(mm->must, dep);
1332 mm->may = isl_map_union(mm->may, dep);
1337 static int common_space(void *first, void *second)
1339 int depth = *(int *)first;
1343 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1351 map2 = isl_map_read_from_str(map->ctx, str, -1);
1352 equal = isl_map_is_equal(map, map2);
1358 void test_dep(struct isl_ctx *ctx)
1363 isl_access_info *ai;
1370 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1371 map = isl_map_read_from_str(ctx, str, -1);
1372 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1374 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1375 map = isl_map_read_from_str(ctx, str, -1);
1376 ai = isl_access_info_add_source(ai, map, 1, &depth);
1378 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1379 map = isl_map_read_from_str(ctx, str, -1);
1380 ai = isl_access_info_add_source(ai, map, 1, &depth);
1382 flow = isl_access_info_compute_flow(ai);
1383 dim = isl_dim_alloc(ctx, 0, 3, 3);
1384 mm.must = isl_map_empty(isl_dim_copy(dim));
1385 mm.may = isl_map_empty(dim);
1387 isl_flow_foreach(flow, collect_must_may, &mm);
1389 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1390 " [1,10,0] -> [2,5,0] }";
1391 assert(map_is_equal(mm.must, str));
1392 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1393 assert(map_is_equal(mm.may, str));
1395 isl_map_free(mm.must);
1396 isl_map_free(mm.may);
1397 isl_flow_free(flow);
1400 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1401 map = isl_map_read_from_str(ctx, str, -1);
1402 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1404 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1405 map = isl_map_read_from_str(ctx, str, -1);
1406 ai = isl_access_info_add_source(ai, map, 1, &depth);
1408 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1409 map = isl_map_read_from_str(ctx, str, -1);
1410 ai = isl_access_info_add_source(ai, map, 0, &depth);
1412 flow = isl_access_info_compute_flow(ai);
1413 dim = isl_dim_alloc(ctx, 0, 3, 3);
1414 mm.must = isl_map_empty(isl_dim_copy(dim));
1415 mm.may = isl_map_empty(dim);
1417 isl_flow_foreach(flow, collect_must_may, &mm);
1419 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1420 assert(map_is_equal(mm.must, str));
1421 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1422 assert(map_is_equal(mm.may, str));
1424 isl_map_free(mm.must);
1425 isl_map_free(mm.may);
1426 isl_flow_free(flow);
1429 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1430 map = isl_map_read_from_str(ctx, str, -1);
1431 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1433 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1434 map = isl_map_read_from_str(ctx, str, -1);
1435 ai = isl_access_info_add_source(ai, map, 0, &depth);
1437 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1438 map = isl_map_read_from_str(ctx, str, -1);
1439 ai = isl_access_info_add_source(ai, map, 0, &depth);
1441 flow = isl_access_info_compute_flow(ai);
1442 dim = isl_dim_alloc(ctx, 0, 3, 3);
1443 mm.must = isl_map_empty(isl_dim_copy(dim));
1444 mm.may = isl_map_empty(dim);
1446 isl_flow_foreach(flow, collect_must_may, &mm);
1448 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1449 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1450 assert(map_is_equal(mm.may, str));
1451 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1452 assert(map_is_equal(mm.must, str));
1454 isl_map_free(mm.must);
1455 isl_map_free(mm.may);
1456 isl_flow_free(flow);
1459 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1460 map = isl_map_read_from_str(ctx, str, -1);
1461 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1463 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1464 map = isl_map_read_from_str(ctx, str, -1);
1465 ai = isl_access_info_add_source(ai, map, 0, &depth);
1467 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1468 map = isl_map_read_from_str(ctx, str, -1);
1469 ai = isl_access_info_add_source(ai, map, 0, &depth);
1471 flow = isl_access_info_compute_flow(ai);
1472 dim = isl_dim_alloc(ctx, 0, 3, 3);
1473 mm.must = isl_map_empty(isl_dim_copy(dim));
1474 mm.may = isl_map_empty(dim);
1476 isl_flow_foreach(flow, collect_must_may, &mm);
1478 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1479 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1480 assert(map_is_equal(mm.may, str));
1481 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1482 assert(map_is_equal(mm.must, str));
1484 isl_map_free(mm.must);
1485 isl_map_free(mm.may);
1486 isl_flow_free(flow);
1489 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1490 map = isl_map_read_from_str(ctx, str, -1);
1491 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1493 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1494 map = isl_map_read_from_str(ctx, str, -1);
1495 ai = isl_access_info_add_source(ai, map, 0, &depth);
1497 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1498 map = isl_map_read_from_str(ctx, str, -1);
1499 ai = isl_access_info_add_source(ai, map, 0, &depth);
1501 flow = isl_access_info_compute_flow(ai);
1502 dim = isl_dim_alloc(ctx, 0, 3, 3);
1503 mm.must = isl_map_empty(isl_dim_copy(dim));
1504 mm.may = isl_map_empty(dim);
1506 isl_flow_foreach(flow, collect_must_may, &mm);
1508 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1509 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1510 assert(map_is_equal(mm.may, str));
1511 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1512 assert(map_is_equal(mm.must, str));
1514 isl_map_free(mm.must);
1515 isl_map_free(mm.may);
1516 isl_flow_free(flow);
1521 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1522 map = isl_map_read_from_str(ctx, str, -1);
1523 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1525 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1526 map = isl_map_read_from_str(ctx, str, -1);
1527 ai = isl_access_info_add_source(ai, map, 1, &depth);
1529 flow = isl_access_info_compute_flow(ai);
1530 dim = isl_dim_alloc(ctx, 0, 5, 5);
1531 mm.must = isl_map_empty(isl_dim_copy(dim));
1532 mm.may = isl_map_empty(dim);
1534 isl_flow_foreach(flow, collect_must_may, &mm);
1536 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1537 assert(map_is_equal(mm.must, str));
1538 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1539 assert(map_is_equal(mm.may, str));
1541 isl_map_free(mm.must);
1542 isl_map_free(mm.may);
1543 isl_flow_free(flow);
1546 int test_sv(isl_ctx *ctx)
1550 isl_union_map *umap;
1553 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1554 map = isl_map_read_from_str(ctx, str, -1);
1555 sv = isl_map_is_single_valued(map);
1560 isl_die(ctx, isl_error_internal,
1561 "map not detected as single valued", return -1);
1563 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1564 map = isl_map_read_from_str(ctx, str, -1);
1565 sv = isl_map_is_single_valued(map);
1570 isl_die(ctx, isl_error_internal,
1571 "map detected as single valued", return -1);
1573 str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
1574 umap = isl_union_map_read_from_str(ctx, str);
1575 sv = isl_union_map_is_single_valued(umap);
1576 isl_union_map_free(umap);
1580 isl_die(ctx, isl_error_internal,
1581 "map not detected as single valued", return -1);
1583 str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
1584 umap = isl_union_map_read_from_str(ctx, str);
1585 sv = isl_union_map_is_single_valued(umap);
1586 isl_union_map_free(umap);
1590 isl_die(ctx, isl_error_internal,
1591 "map detected as single valued", return -1);
1596 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1600 map = isl_map_read_from_str(ctx, str, -1);
1602 assert(isl_map_is_bijective(map));
1604 assert(!isl_map_is_bijective(map));
1608 void test_bijective(struct isl_ctx *ctx)
1610 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1611 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1612 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1613 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1614 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1615 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1616 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1617 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1618 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1619 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1620 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1621 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1622 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1625 void test_pwqp(struct isl_ctx *ctx)
1629 isl_pw_qpolynomial *pwqp1, *pwqp2;
1631 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1632 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1634 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1637 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1638 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1640 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1642 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1644 isl_pw_qpolynomial_free(pwqp1);
1646 str = "{ [i] -> i }";
1647 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1648 str = "{ [k] : exists a : k = 2a }";
1649 set = isl_set_read_from_str(ctx, str, 0);
1650 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1651 str = "{ [i] -> i }";
1652 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1654 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1656 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1658 isl_pw_qpolynomial_free(pwqp1);
1660 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1661 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1663 set = isl_set_read_from_str(ctx, str, 0);
1664 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1665 str = "{ [i] -> 16 }";
1666 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1668 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1670 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1672 isl_pw_qpolynomial_free(pwqp1);
1674 str = "{ [i] -> ([(i)/2]) }";
1675 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1676 str = "{ [k] : exists a : k = 2a+1 }";
1677 set = isl_set_read_from_str(ctx, str, 0);
1678 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1679 str = "{ [i] -> -1/2 + 1/2 * i }";
1680 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1682 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1684 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1686 isl_pw_qpolynomial_free(pwqp1);
1688 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1689 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1690 str = "{ [i] -> ([(2 * [i/2])/5]) }";
1691 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1693 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1695 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1697 isl_pw_qpolynomial_free(pwqp1);
1699 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1700 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1701 str = "{ [x] -> x }";
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]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
1711 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1712 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1713 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
1714 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1715 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1716 isl_pw_qpolynomial_free(pwqp1);
1719 void test_split_periods(isl_ctx *ctx)
1722 isl_pw_qpolynomial *pwqp;
1724 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1725 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
1726 "U >= 0; [U,V] -> U^2 : U >= 100 }";
1727 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1729 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1732 isl_pw_qpolynomial_free(pwqp);
1735 void test_union(isl_ctx *ctx)
1738 isl_union_set *uset1, *uset2;
1739 isl_union_map *umap1, *umap2;
1741 str = "{ [i] : 0 <= i <= 1 }";
1742 uset1 = isl_union_set_read_from_str(ctx, str);
1743 str = "{ [1] -> [0] }";
1744 umap1 = isl_union_map_read_from_str(ctx, str);
1746 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1747 assert(isl_union_map_is_equal(umap1, umap2));
1749 isl_union_map_free(umap1);
1750 isl_union_map_free(umap2);
1752 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1753 umap1 = isl_union_map_read_from_str(ctx, str);
1754 str = "{ A[i]; B[i] }";
1755 uset1 = isl_union_set_read_from_str(ctx, str);
1757 uset2 = isl_union_map_domain(umap1);
1759 assert(isl_union_set_is_equal(uset1, uset2));
1761 isl_union_set_free(uset1);
1762 isl_union_set_free(uset2);
1765 void test_bound(isl_ctx *ctx)
1768 isl_pw_qpolynomial *pwqp;
1769 isl_pw_qpolynomial_fold *pwf;
1771 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1772 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1773 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1774 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_set) == 4);
1775 isl_pw_qpolynomial_fold_free(pwf);
1777 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
1778 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1779 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1780 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_set) == 1);
1781 isl_pw_qpolynomial_fold_free(pwf);
1784 void test_lift(isl_ctx *ctx)
1787 isl_basic_map *bmap;
1788 isl_basic_set *bset;
1790 str = "{ [i0] : exists e0 : i0 = 4e0 }";
1791 bset = isl_basic_set_read_from_str(ctx, str, 0);
1792 bset = isl_basic_set_lift(bset);
1793 bmap = isl_basic_map_from_range(bset);
1794 bset = isl_basic_map_domain(bmap);
1795 isl_basic_set_free(bset);
1798 void test_subset(isl_ctx *ctx)
1801 isl_set *set1, *set2;
1803 str = "{ [112, 0] }";
1804 set1 = isl_set_read_from_str(ctx, str, 0);
1805 str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1806 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1807 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
1808 set2 = isl_set_read_from_str(ctx, str, 0);
1809 assert(isl_set_is_subset(set1, set2));
1814 void test_factorize(isl_ctx *ctx)
1817 isl_basic_set *bset;
1820 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
1821 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
1822 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
1823 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
1824 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
1825 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
1826 "3i5 >= -2i0 - i2 + 3i4 }";
1827 bset = isl_basic_set_read_from_str(ctx, str, 0);
1828 f = isl_basic_set_factorizer(bset);
1830 isl_basic_set_free(bset);
1831 isl_factorizer_free(f);
1833 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1834 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
1835 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
1836 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
1837 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
1838 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
1839 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
1840 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
1841 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
1842 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
1843 bset = isl_basic_set_read_from_str(ctx, str, 0);
1844 f = isl_basic_set_factorizer(bset);
1846 isl_basic_set_free(bset);
1847 isl_factorizer_free(f);
1850 static int check_injective(__isl_take isl_map *map, void *user)
1852 int *injective = user;
1854 *injective = isl_map_is_injective(map);
1857 if (*injective < 0 || !*injective)
1863 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
1864 const char *r, const char *s, int tilable, int parallel)
1868 isl_union_map *W, *R, *S;
1869 isl_union_map *empty;
1870 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
1871 isl_union_map *validity, *proximity;
1872 isl_union_map *schedule;
1873 isl_union_map *test;
1874 isl_union_set *delta;
1875 isl_union_set *domain;
1879 isl_schedule *sched;
1880 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
1882 D = isl_union_set_read_from_str(ctx, d);
1883 W = isl_union_map_read_from_str(ctx, w);
1884 R = isl_union_map_read_from_str(ctx, r);
1885 S = isl_union_map_read_from_str(ctx, s);
1887 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
1888 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
1890 empty = isl_union_map_empty(isl_union_map_get_dim(S));
1891 isl_union_map_compute_flow(isl_union_map_copy(R),
1892 isl_union_map_copy(W), empty,
1893 isl_union_map_copy(S),
1894 &dep_raw, NULL, NULL, NULL);
1895 isl_union_map_compute_flow(isl_union_map_copy(W),
1896 isl_union_map_copy(W),
1897 isl_union_map_copy(R),
1898 isl_union_map_copy(S),
1899 &dep_waw, &dep_war, NULL, NULL);
1901 dep = isl_union_map_union(dep_waw, dep_war);
1902 dep = isl_union_map_union(dep, dep_raw);
1903 validity = isl_union_map_copy(dep);
1904 proximity = isl_union_map_copy(dep);
1906 sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
1907 validity, proximity);
1908 schedule = isl_schedule_get_map(sched);
1909 isl_schedule_free(sched);
1910 isl_union_map_free(W);
1911 isl_union_map_free(R);
1912 isl_union_map_free(S);
1915 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
1917 domain = isl_union_map_domain(isl_union_map_copy(schedule));
1918 is_complete = isl_union_set_is_subset(D, domain);
1919 isl_union_set_free(D);
1920 isl_union_set_free(domain);
1922 test = isl_union_map_reverse(isl_union_map_copy(schedule));
1923 test = isl_union_map_apply_range(test, dep);
1924 test = isl_union_map_apply_range(test, schedule);
1926 delta = isl_union_map_deltas(test);
1927 if (isl_union_set_n_set(delta) == 0) {
1931 isl_union_set_free(delta);
1933 delta_set = isl_set_from_union_set(delta);
1935 slice = isl_set_universe(isl_set_get_dim(delta_set));
1936 for (i = 0; i < tilable; ++i)
1937 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
1938 is_tilable = isl_set_is_subset(delta_set, slice);
1939 isl_set_free(slice);
1941 slice = isl_set_universe(isl_set_get_dim(delta_set));
1942 for (i = 0; i < parallel; ++i)
1943 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
1944 is_parallel = isl_set_is_subset(delta_set, slice);
1945 isl_set_free(slice);
1947 origin = isl_set_universe(isl_set_get_dim(delta_set));
1948 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
1949 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
1951 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
1952 delta_set = isl_set_lexmin(delta_set);
1954 is_nonneg = isl_set_is_equal(delta_set, origin);
1956 isl_set_free(origin);
1957 isl_set_free(delta_set);
1960 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
1961 is_injection < 0 || is_complete < 0)
1964 isl_die(ctx, isl_error_unknown,
1965 "generated schedule incomplete", return -1);
1967 isl_die(ctx, isl_error_unknown,
1968 "generated schedule not injective on each statement",
1971 isl_die(ctx, isl_error_unknown,
1972 "negative dependences in generated schedule",
1975 isl_die(ctx, isl_error_unknown,
1976 "generated schedule not as tilable as expected",
1979 isl_die(ctx, isl_error_unknown,
1980 "generated schedule not as parallel as expected",
1986 int test_special_schedule(isl_ctx *ctx)
1990 isl_union_map *empty;
1992 isl_union_map *sched1, *sched2;
1993 isl_schedule *schedule;
1996 str = "{ S[i,j] : 0 <= i <= 10 }";
1997 dom = isl_union_set_read_from_str(ctx, str);
1998 str = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
1999 dep = isl_union_map_read_from_str(ctx, str);
2000 empty = isl_union_map_read_from_str(ctx, "{}");
2001 schedule = isl_union_set_compute_schedule(dom, empty, dep);
2002 sched1 = isl_schedule_get_map(schedule);
2003 isl_schedule_free(schedule);
2005 str = "{ S[i, j] -> [j, i] }";
2006 sched2 = isl_union_map_read_from_str(ctx, str);
2008 equal = isl_union_map_is_equal(sched1, sched2);
2009 isl_union_map_free(sched1);
2010 isl_union_map_free(sched2);
2015 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2021 int test_schedule(isl_ctx *ctx)
2023 const char *D, *W, *R, *S;
2026 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2027 W = "{ S1[t,i] -> a[t,i] }";
2028 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2029 "S1[t,i] -> a[t-1,i+1] }";
2030 S = "{ S1[t,i] -> [t,i] }";
2031 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2034 /* Fig. 5 of CC2008 */
2035 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2037 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2038 "j >= 2 and j <= -1 + N }";
2039 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2040 "j >= 2 and j <= -1 + N; "
2041 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2042 "j >= 2 and j <= -1 + N }";
2043 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2044 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2047 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2048 W = "{ S1[i] -> a[i] }";
2049 R = "{ S2[i] -> a[i+1] }";
2050 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2051 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2054 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2055 W = "{ S1[i] -> a[i] }";
2056 R = "{ S2[i] -> a[9-i] }";
2057 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2058 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2061 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2062 W = "{ S1[i] -> a[i] }";
2063 R = "[N] -> { S2[i] -> a[N-1-i] }";
2064 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2065 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2068 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2069 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2070 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2071 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2072 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2075 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2076 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2077 R = "{ S2[i,j] -> a[i-1,j] }";
2078 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2079 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2082 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2083 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2084 R = "{ S2[i,j] -> a[i,j-1] }";
2085 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2086 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2089 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2090 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2091 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2092 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2093 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2094 "S_0[] -> [0, 0, 0] }";
2095 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2097 ctx->opt->schedule_parametric = 0;
2098 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2100 ctx->opt->schedule_parametric = 1;
2102 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2103 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2104 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2105 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2106 "S4[i] -> a[i,N] }";
2107 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2108 "S4[i] -> [4,i,0] }";
2109 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2112 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2113 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2115 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2117 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2119 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2120 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2123 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2124 " S_2[t] : t >= 0 and t <= -1 + N; "
2125 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2127 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2128 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2129 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2130 "i >= 0 and i <= -1 + N }";
2131 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2132 "i >= 0 and i <= -1 + N; "
2133 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2134 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2135 " S_0[t] -> [0, t, 0] }";
2137 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2139 ctx->opt->schedule_parametric = 0;
2140 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2142 ctx->opt->schedule_parametric = 1;
2144 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2145 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2146 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2149 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2150 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2151 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2152 "j >= 0 and j <= -1 + N; "
2153 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2154 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2155 "j >= 0 and j <= -1 + N; "
2156 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2157 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2158 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2161 D = "{ S_0[i] : i >= 0 }";
2162 W = "{ S_0[i] -> a[i] : i >= 0 }";
2163 R = "{ S_0[i] -> a[0] : i >= 0 }";
2164 S = "{ S_0[i] -> [0, i, 0] }";
2165 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2168 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2169 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2170 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2171 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2172 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2175 return test_special_schedule(ctx);
2178 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2180 isl_union_map *umap;
2183 umap = isl_union_map_read_from_str(ctx, str);
2184 test = isl_union_map_plain_is_injective(umap);
2185 isl_union_map_free(umap);
2188 if (test == injective)
2191 isl_die(ctx, isl_error_unknown,
2192 "map not detected as injective", return -1);
2194 isl_die(ctx, isl_error_unknown,
2195 "map detected as injective", return -1);
2198 int test_injective(isl_ctx *ctx)
2202 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2204 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2206 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2208 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2210 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2212 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2214 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2216 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2219 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2220 if (test_plain_injective(ctx, str, 1))
2222 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2223 if (test_plain_injective(ctx, str, 0))
2229 int test_aff(isl_ctx *ctx)
2234 isl_local_space *ls;
2238 dim = isl_dim_set_alloc(ctx, 0, 1);
2239 ls = isl_local_space_from_dim(dim);
2240 aff = isl_aff_zero(ls);
2242 aff = isl_aff_add_coefficient_si(aff, isl_dim_set, 0, 1);
2243 aff = isl_aff_scale_down_ui(aff, 3);
2244 aff = isl_aff_floor(aff);
2245 aff = isl_aff_add_coefficient_si(aff, isl_dim_set, 0, 1);
2246 aff = isl_aff_scale_down_ui(aff, 2);
2247 aff = isl_aff_floor(aff);
2248 aff = isl_aff_add_coefficient_si(aff, isl_dim_set, 0, 1);
2251 set = isl_set_read_from_str(ctx, str, 0);
2252 aff = isl_aff_gist(aff, set);
2254 aff = isl_aff_add_constant_si(aff, -16);
2255 zero = isl_aff_plain_is_zero(aff);
2261 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2266 int test_dim_max(isl_ctx *ctx)
2270 isl_map *map, *map2;
2274 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
2275 set = isl_set_read_from_str(ctx, str, -1);
2276 pwaff = isl_set_dim_max(set, 0);
2277 map = isl_map_from_pw_aff(pwaff);
2278 str = "[N] -> { [] -> [10] : N >= 10; [] -> [N] : N <= 9 and N >= 0 }";
2279 map2 = isl_map_read_from_str(ctx, str, -1);
2280 equal = isl_map_is_equal(map, map2);
2286 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2288 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
2289 set = isl_set_read_from_str(ctx, str, -1);
2290 pwaff = isl_set_dim_max(set, 0);
2291 map = isl_map_from_pw_aff(pwaff);
2292 str = "[N] -> { [] -> [6 + N] : -6 <= N <= 5; [] -> [2N] : N >= 6 }";
2293 map2 = isl_map_read_from_str(ctx, str, -1);
2294 equal = isl_map_is_equal(map, map2);
2300 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2302 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
2303 set = isl_set_read_from_str(ctx, str, -1);
2304 pwaff = isl_set_dim_max(set, 0);
2305 map = isl_map_from_pw_aff(pwaff);
2306 str = "[N] -> { [] -> [6 + N] : -6 <= N <= 5; [] -> [2N] : N >= 6 }";
2307 map2 = isl_map_read_from_str(ctx, str, -1);
2308 equal = isl_map_is_equal(map, map2);
2314 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2316 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
2317 "0 <= i < N and 0 <= j < M }";
2318 map = isl_map_read_from_str(ctx, str, -1);
2319 set = isl_map_range(map);
2321 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
2322 map = isl_map_from_pw_aff(pwaff);
2323 str = "[N,M] -> { [] -> [([(N-1)/16])] : M,N > 0 }";
2324 map2 = isl_map_read_from_str(ctx, str, -1);
2325 equal = isl_map_is_equal(map, map2);
2329 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
2330 map = isl_map_from_pw_aff(pwaff);
2331 str = "[N,M] -> { [] -> [t] : t = min(M-1,15) and M,N > 0 }";
2332 map2 = isl_map_read_from_str(ctx, str, -1);
2333 if (equal >= 0 && equal)
2334 equal = isl_map_is_equal(map, map2);
2343 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2350 struct isl_ctx *ctx;
2352 srcdir = getenv("srcdir");
2355 ctx = isl_ctx_alloc();
2356 if (test_dim_max(ctx) < 0)
2358 if (test_aff(ctx) < 0)
2360 if (test_injective(ctx) < 0)
2362 if (test_schedule(ctx) < 0)
2364 test_factorize(ctx);
2369 test_split_periods(ctx);
2373 if (test_sv(ctx) < 0)
2375 test_bijective(ctx);
2379 test_construction(ctx);
2382 test_application(ctx);
2383 test_affine_hull(ctx);
2384 test_convex_hull(ctx);