detect more forms of modulo expressions when extracting a function
[isl.git] / isl_test2.cc
blob62ffa5a6a5ed7a511d583ff2d5fa7895fb03a08b
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2021-2022 Cerebras Systems
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
17 * and Cerebras Systems, 1237 E Arques Ave, Sunnyvale, CA, USA
20 #include <assert.h>
21 #include <stdlib.h>
23 #include <functional>
24 #include <ios>
25 #include <iostream>
26 #include <sstream>
27 #include <string>
28 #include <type_traits>
29 #include <utility>
30 #include <vector>
32 #include <isl/cpp.h>
34 /* A binary isl function that appears in the C++ bindings
35 * as a unary method in a class T, taking an extra argument
36 * of type A1 and returning an object of type R.
38 template <typename A1, typename R, typename T>
39 using binary_fn = R (T::*)(A1) const;
41 /* A function for selecting an overload of a pointer to a unary C++ method
42 * based on the single argument type.
43 * The object type and the return type are meant to be deduced.
45 template <typename A1, typename R, typename T>
46 static binary_fn<A1, R, T> const arg(const binary_fn<A1, R, T> &fn)
48 return fn;
51 /* A ternary isl function that appears in the C++ bindings
52 * as a binary method in a class T, taking extra arguments
53 * of type A1 and A2 and returning an object of type R.
55 template <typename A1, typename A2, typename R, typename T>
56 using ternary_fn = R (T::*)(A1, A2) const;
58 /* A function for selecting an overload of a pointer to a binary C++ method
59 * based on the (first) argument type(s).
60 * The object type and the return type are meant to be deduced.
62 template <typename A1, typename A2, typename R, typename T>
63 static ternary_fn<A1, A2, R, T> const arg(const ternary_fn<A1, A2, R, T> &fn)
65 return fn;
68 /* A description of the input and the output of a unary property.
70 struct unary_prop {
71 const char *arg;
72 bool res;
75 /* A description of the input and the output of a unary operation.
77 struct unary {
78 const char *arg;
79 const char *res;
82 /* A description of the inputs and the output of a binary operation.
84 struct binary {
85 const char *arg1;
86 const char *arg2;
87 const char *res;
90 /* A description of the inputs and the output of a ternary operation.
92 struct ternary {
93 const char *arg1;
94 const char *arg2;
95 const char *arg3;
96 const char *res;
99 /* A template function for checking whether two objects
100 * of the same (isl) type are (obviously) equal.
101 * The spelling depends on the isl type and
102 * in particular on whether an equality method is available or
103 * whether only obvious equality can be tested.
105 template <typename T, typename std::decay<decltype(
106 std::declval<T>().is_equal(std::declval<T>()))>::type = true>
107 static bool is_equal(const T &a, const T &b)
109 return a.is_equal(b);
111 template <typename T, typename std::decay<decltype(
112 std::declval<T>().plain_is_equal(std::declval<T>()))>::type = true>
113 static bool is_equal(const T &a, const T &b)
115 return a.plain_is_equal(b);
118 /* A helper macro for throwing an isl::exception_invalid with message "msg".
120 #define THROW_INVALID(msg) \
121 isl::exception::throw_error(isl_error_invalid, msg, __FILE__, __LINE__)
123 /* Run a sequence of tests of function "fn" with stringification "name" and
124 * with input and output described by "tests",
125 * throwing an exception when an unexpected result is produced.
127 template <typename T>
128 static void test(isl::ctx ctx, bool fn(const T &), const std::string &name,
129 const std::vector<unary_prop> &tests)
131 for (const auto &test : tests) {
132 T obj(ctx, test.arg);
133 bool res = fn(obj);
134 std::ostringstream ss;
136 if (test.res == res)
137 continue;
139 ss << name << "(" << test.arg << ") = "
140 << std::boolalpha << res << "\n"
141 << "expecting: "
142 << test.res;
143 THROW_INVALID(ss.str().c_str());
147 /* Run a sequence of tests of method "fn" with stringification "name" and
148 * with input and output described by "test",
149 * throwing an exception when an unexpected result is produced.
151 template <typename R, typename T>
152 static void test(isl::ctx ctx, R (T::*fn)() const, const std::string &name,
153 const std::vector<unary> &tests)
155 for (const auto &test : tests) {
156 T obj(ctx, test.arg);
157 R expected(ctx, test.res);
158 const auto &res = (obj.*fn)();
159 std::ostringstream ss;
161 if (is_equal(expected, res))
162 continue;
164 ss << name << "(" << test.arg << ") =\n"
165 << res << "\n"
166 << "expecting:\n"
167 << expected;
168 THROW_INVALID(ss.str().c_str());
172 /* Run a sequence of tests of method "fn" with stringification "name" and
173 * with inputs and output described by "test",
174 * throwing an exception when an unexpected result is produced.
176 template <typename R, typename T, typename A1>
177 static void test(isl::ctx ctx, R (T::*fn)(A1) const, const std::string &name,
178 const std::vector<binary> &tests)
180 for (const auto &test : tests) {
181 T obj(ctx, test.arg1);
182 A1 arg1(ctx, test.arg2);
183 R expected(ctx, test.res);
184 const auto &res = (obj.*fn)(arg1);
185 std::ostringstream ss;
187 if (is_equal(expected, res))
188 continue;
190 ss << name << "(" << test.arg1 << ", " << test.arg2 << ") =\n"
191 << res << "\n"
192 << "expecting:\n"
193 << expected;
194 THROW_INVALID(ss.str().c_str());
198 /* Run a sequence of tests of function "fn" with stringification "name" and
199 * with inputs and output described by "tests",
200 * throwing an exception when an unexpected result is produced.
202 template <typename R, typename T, typename A1, typename A2, typename F>
203 static void test_ternary(isl::ctx ctx, const F &fn,
204 const std::string &name, const std::vector<ternary> &tests)
206 for (const auto &test : tests) {
207 T obj(ctx, test.arg1);
208 A1 arg1(ctx, test.arg2);
209 A2 arg2(ctx, test.arg3);
210 R expected(ctx, test.res);
211 const auto &res = fn(obj, arg1, arg2);
212 std::ostringstream ss;
214 if (is_equal(expected, res))
215 continue;
217 ss << name << "(" << test.arg1 << ", " << test.arg2 << ", "
218 << test.arg3 << ") =\n"
219 << res << "\n"
220 << "expecting:\n"
221 << expected;
222 THROW_INVALID(ss.str().c_str());
226 /* Run a sequence of tests of function "fn" with stringification "name" and
227 * with inputs and output described by "tests",
228 * throwing an exception when an unexpected result is produced.
230 * Simply call test_ternary.
232 template <typename R, typename T, typename A1, typename A2>
233 static void test(isl::ctx ctx, R fn(const T&, const A1&, const A2&),
234 const std::string &name, const std::vector<ternary> &tests)
236 test_ternary<R, T, A1, A2>(ctx, fn, name, tests);
239 /* Run a sequence of tests of method "fn" with stringification "name" and
240 * with inputs and output described by "tests",
241 * throwing an exception when an unexpected result is produced.
243 * Wrap the method pointer into a function taking an object reference and
244 * call test_ternary.
246 template <typename R, typename T, typename A1, typename A2>
247 static void test(isl::ctx ctx, R (T::*fn)(A1, A2) const,
248 const std::string &name, const std::vector<ternary> &tests)
250 const auto &wrap = [&] (const T &o, const A1 &arg1, const A2 &arg2) {
251 return (o.*fn)(arg1, arg2);
253 test_ternary<R, T, A1, A2>(ctx, wrap, name, tests);
256 /* A helper macro that calls test with as implicit initial argument "ctx" and
257 * as extra argument a stringification of "FN".
259 #define C(FN, ...) test(ctx, FN, #FN, __VA_ARGS__)
261 /* Perform some basic isl::space tests.
263 static void test_space(isl::ctx ctx)
265 C(&isl::space::domain, {
266 { "{ A[] -> B[] }", "{ A[] }" },
267 { "{ A[C[] -> D[]] -> B[E[] -> F[]] }", "{ A[C[] -> D[]] }" },
270 C(&isl::space::range, {
271 { "{ A[] -> B[] }", "{ B[] }" },
272 { "{ A[C[] -> D[]] -> B[E[] -> F[]] }", "{ B[E[] -> F[]] }" },
275 C(&isl::space::params, {
276 { "{ A[] -> B[] }", "{ : }" },
277 { "{ A[C[] -> D[]] -> B[E[] -> F[]] }", "{ : }" },
281 /* Is "fn" an expression defined over a single cell?
283 static bool has_single_cell(const isl::pw_multi_aff &fn)
285 const auto &domain = fn.domain();
286 return fn.gist(domain).isa_multi_aff();
289 /* Does the conversion of "obj" to an isl_pw_multi_aff
290 * result in an expression defined over a single cell?
292 template <typename T>
293 static bool has_single_cell_pma(const T &obj)
295 return has_single_cell(obj.as_pw_multi_aff());
298 /* Perform some basic conversion tests.
300 * In particular, check that a map with an output dimension
301 * that is equal to some integer division over a domain involving
302 * a local variable without a known integer division expression or
303 * to some linear combination of integer divisions
304 * can be converted to a function expressed in the same way.
306 * Also, check that a nested modulo expression can be extracted
307 * from a set or binary relation representation, or at least
308 * that a conversion to a function does not result in multiple cells.
310 static void test_conversion(isl::ctx ctx)
312 C(&isl::set::as_pw_multi_aff, {
313 { "[N=0:] -> { [] }",
314 "[N=0:] -> { [] }" },
317 C(&isl::multi_pw_aff::as_set, {
318 { "[n] -> { [] : n >= 0 } ",
319 "[n] -> { [] : n >= 0 } " },
322 C(&isl::map::as_pw_multi_aff, {
323 { "{ [a] -> [a//2] : "
324 "exists (e0: 8*floor((-a + e0)/8) <= -8 - a + 8e0) }",
325 "{ [a] -> [a//2] : "
326 "exists (e0: 8*floor((-a + e0)/8) <= -8 - a + 8e0) }" },
327 { "{ [a, b] -> [(2*floor((a)/8) + floor((b)/6))] }",
328 "{ [a, b] -> [(2*floor((a)/8) + floor((b)/6))] }" },
331 C(&has_single_cell_pma<isl::set>, {
332 { "[s=0:23] -> { A[(s//4)%3, s%4, s//12] }", true },
335 C(&has_single_cell_pma<isl::map>, {
336 { "{ [a] -> [a//2] : "
337 "exists (e0: 8*floor((-a + e0)/8) <= -8 - a + 8e0) }",
338 true },
339 { "{ [s=0:23, t] -> B[((s+1+2t)//4)%3, 2+(s+1+2t)%4, (s+1+2t)//12] }",
340 true },
341 { "{ [a=0:31] -> [b=0:3, c] : 4c = 28 - a + b }", true },
345 /* Perform some basic preimage tests.
347 static void test_preimage(isl::ctx ctx)
349 C(arg<isl::multi_aff>(&isl::set::preimage), {
350 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
351 "{ A[j,i] -> B[i,j] }",
352 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
353 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
354 "{ A[a,b] -> B[a/2,b/6] }",
355 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
356 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
357 "{ A[a,b] -> B[a/2,b/6] }",
358 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
359 "exists i,j : a = 2 i and b = 6 j }" },
360 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
361 "[n] -> { : 0 <= n <= 100 }" },
362 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
363 "{ A[a] -> B[2a] }",
364 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
365 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
366 "{ A[a] -> B[([a/2])] }",
367 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
368 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
369 "{ A[a] -> B[a,a,a/3] }",
370 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
371 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
372 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
375 C(arg<isl::pw_multi_aff>(&isl::set::preimage), {
376 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
377 "{ A[j,i] -> B[i,j] : false }",
378 "{ A[j,i] : false }" },
381 C(arg<isl::multi_aff>(&isl::union_map::preimage_domain), {
382 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
383 "{ A[j,i] -> B[i,j] }",
384 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
385 { "{ B[i] -> C[i]; D[i] -> E[i] }",
386 "{ A[i] -> B[i + 1] }",
387 "{ A[i] -> C[i + 1] }" },
388 { "{ B[i] -> C[i]; B[i] -> E[i] }",
389 "{ A[i] -> B[i + 1] }",
390 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
391 { "{ B[i] -> C[([i/2])] }",
392 "{ A[i] -> B[2i] }",
393 "{ A[i] -> C[i] }" },
394 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
395 "{ A[i] -> B[([i/5]), ([i/7])] }",
396 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
397 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
398 "[N] -> { A[] -> B[([N/5])] }",
399 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
400 { "{ B[i] -> C[i] : exists a : i = 5 a }",
401 "{ A[i] -> B[2i] }",
402 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
403 { "{ B[i] -> C[i] : exists a : i = 2 a; "
404 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
405 "{ A[i] -> B[2i] }",
406 "{ A[i] -> C[2i] }" },
407 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
408 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
411 C(arg<isl::multi_aff>(&isl::union_map::preimage_range), {
412 { "[M] -> { A[a] -> B[a] }", "[M] -> { C[] -> B[floor(M/2)] }",
413 "[M] -> { A[floor(M/2)] -> C[] }" },
417 /* Perform some basic fixed power tests.
419 static void test_fixed_power(isl::ctx ctx)
421 C(arg<isl::val>(&isl::map::fixed_power), {
422 { "{ [i] -> [i + 1] }", "23",
423 "{ [i] -> [i + 23] }" },
424 { "{ [a = 0:1, b = 0:15, c = 0:1, d = 0:1, 0] -> [a, b, c, d, 1]; "
425 "[a = 0:1, b = 0:15, c = 0:1, 0, 1] -> [a, b, c, 1, 0]; "
426 "[a = 0:1, b = 0:15, 0, 1, 1] -> [a, b, 1, 0, 0]; "
427 "[a = 0:1, b = 0:14, 1, 1, 1] -> [a, 1 + b, 0, 0, 0]; "
428 "[0, 15, 1, 1, 1] -> [1, 0, 0, 0, 0] }",
429 "128",
430 "{ [0, b = 0:15, c = 0:1, d = 0:1, e = 0:1] -> [1, b, c, d, e] }" },
434 /* Perform some basic intersection tests.
436 static void test_intersect(isl::ctx ctx)
438 C(arg<isl::basic_set>(&isl::basic_map::intersect_params), {
439 { "[n] -> { A[x] -> B[y] }", "[n] -> { : n >= 0 }",
440 "[n] -> { A[x] -> B[y] : n >= 0 }" },
443 C(&isl::union_map::intersect_domain_wrapped_domain, {
444 { "{ [A[x] -> B[y]] -> C[z]; [D[x] -> A[y]] -> E[z] }",
445 "{ A[0] }",
446 "{ [A[0] -> B[y]] -> C[z] }" },
447 { "{ C[z] -> [A[x] -> B[y]]; E[z] -> [D[x] -> A[y]] }",
448 "{ A[0] }",
449 "{ }" },
450 { "{ T[A[x] -> B[y]] -> C[z]; [D[x] -> A[y]] -> E[z] }",
451 "{ A[0] }",
452 "{ T[A[0] -> B[y]] -> C[z] }" },
455 C(&isl::union_map::intersect_range_wrapped_domain, {
456 { "{ [A[x] -> B[y]] -> C[z]; [D[x] -> A[y]] -> E[z] }",
457 "{ A[0] }",
458 "{ }" },
459 { "{ C[z] -> [A[x] -> B[y]]; E[z] -> [D[x] -> A[y]] }",
460 "{ A[0] }",
461 "{ C[z] -> [A[0] -> B[y]] }" },
462 { "{ C[z] -> T[A[x] -> B[y]]; E[z] -> [D[x] -> A[y]] }",
463 "{ A[0] }",
464 "{ C[z] -> T[A[0] -> B[y]] }" },
468 /* Is the expression for the lexicographic minimum of "obj"
469 * defined over a single cell?
471 template <typename T>
472 static bool lexmin_has_single_cell(const T &obj)
474 return has_single_cell(obj.lexmin_pw_multi_aff());
477 /* Perform some basic lexicographic minimization tests.
479 static void test_lexmin(isl::ctx ctx)
481 C(&lexmin_has_single_cell<isl::map>, {
482 /* The following two inputs represent the same binary relation,
483 * the second with extra redundant constraints.
484 * The lexicographic minimum of both should consist of a single cell.
486 { "{ [a=0:11] -> [b] : -1 + b <= 2*floor((a)/6) <= b }", true },
487 { "{ [a=0:11] -> [b=0:3] : -1 + b <= 2*floor((a)/6) <= b }", true },
489 { "{ [a = 0:2, b = 0:1] -> [c = 0:9, d = (-a + b) mod 3] : "
490 "10a + 5b - 3c <= 5d <= 12 + 10a + 5b - 3c }", true },
491 { "{ [a=0:71] -> [(a//3)%8] }", true },
492 { "{ [a=0:71] -> [b=0:7] : (a - 3 * b + 21) % 24 >= 21 }", true },
493 { "{ [a=0:71] -> [b=0:7] : (a - 3 * b + 21) % 24 >= 20 }", false },
494 { "{ [a=0:71] -> [b=0:7] : (a - 3 * b + 21) % 24 >= 22 }", true },
495 { "{ [a=0:71] -> [b=-7:0] : (a + 3 * b + 21) % 24 >= 21 }", true },
496 { "{ [a=0:71] -> [b=-7:0] : (a + 3 * b + 21) % 24 >= 20 }", false },
497 { "{ [a=0:71] -> [b=-7:0] : (a + 3 * b + 21) % 24 >= 22 }", true },
500 C(&isl::map::lexmin_pw_multi_aff, {
501 /* The following two inputs represent the same binary relation,
502 * the second with some redundant constraints removed.
503 * The lexicographic minimum of both should consist of a single cell.
505 { "{ [a=0:3] -> [b=a//2] : 0 <= b <= 1 }",
506 "{ [a=0:3] -> [(floor((a)/2))] }" },
507 { "{ [a] -> [b=a//2] : 0 <= b <= 1 }",
508 "{ [a=0:3] -> [(floor((a)/2))] }" },
510 { "{ [a = 0:2, b = 0:1] -> [c = 0:9, d = (-a + b) mod 3] : "
511 "10a + 5b - 3c <= 5d <= 12 + 10a + 5b - 3c }",
512 "{ [a = 0:2, b = 0:1] -> [5*(2a + b)//3, (2a + b) mod 3] }" },
513 { "{ [a=0:71] -> [(a//3)%8] }",
514 "{ [a=0:71] -> [(a//3)%8] }" },
515 { "{ [a=0:71] -> [b=0:7] : (a - 3 * b + 21) % 24 >= 21 }",
516 "{ [a=0:71] -> [(a//3)%8] }" },
517 { "{ [a=0:71] -> [b=0:7] : (a - 3 * b + 21) % 24 >= 22 }",
518 "{ [a=0:71] -> [(a//3)%8] : a % 3 > 0 }" },
519 { "{ [a=0:71] -> [b=-7:0] : (a + 3 * b + 21) % 24 >= 21 }",
520 "{ [a=0:71] -> [(-7 + (-1 - floor((a)/3)) mod 8)] }" },
523 C(&isl::set::lexmin_pw_multi_aff, {
524 { "[a] -> { [b=a//2] : 0 <= b <= 1 }",
525 "[a=0:3] -> { [(floor((a)/2))] }" },
526 { "[a=0:71] -> { [(a//3)%8] }",
527 "[a=0:71] -> { [(a//3)%8] }" },
528 { "[a=0:71] -> { [b=0:7] : (a - 3 * b + 21) % 24 >= 21 }",
529 "[a=0:71] -> { [(a//3)%8] }" },
533 /* Compute the gist of "obj" with respect to "context",
534 * with "copy" an independent copy of "obj",
535 * but also check that applying the gist operation does
536 * not modify the input set (an earlier version of isl would do that) and
537 * that the test case is consistent, i.e., that the gist has the same
538 * intersection with the context as the input set.
540 template <typename T>
541 T gist(const T &obj, const T &copy, const T &context)
543 const auto &res = obj.gist(context);
544 if (!is_equal(obj, copy)) {
545 std::ostringstream ss;
546 ss << "gist changed " << copy << " into " << obj;
547 THROW_INVALID(ss.str().c_str());
549 if (!is_equal(obj.intersect(context), res.intersect(context))) {
550 std::ostringstream ss;
551 ss << "inconsistent "
552 << obj << " % " << context << " = " << res;
553 THROW_INVALID(ss.str().c_str());
555 return res;
558 /* A helper macro for producing two instances of "x".
560 #define TWO(x) (x), (x)
562 /* Perform some basic gist tests.
564 * The gist() function is given two identical inputs so that
565 * it can check that the input to the call to the gist method
566 * is not modified.
568 static void test_gist(isl::ctx ctx)
570 C(&gist<isl::basic_set> , {
571 { TWO("{ [i=100:] }"),
572 "{ [i] : exists a, b: 2b > 2i - 5a > 8b -3 i and 3b > 2a }",
573 "{ [i=100:] }" },
574 { TWO("{ [i=0:] }"),
575 "{ [i] : exists a, b: 2b > 2i - 5a > 8b -3 i and 3b > 2a }",
576 "{ [i] }" },
577 { TWO("{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
578 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }"),
579 "{ [i] : i >= 0 }",
580 "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
581 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }" },
582 { TWO("{ [i=0:10] : exists a, b: 2b > 2i - 5a > 8b -3 i and 3b > 2a }"),
583 "{ [i=0:10] }",
584 "{ [i] : exists a, b: 2b > 2i - 5a > 8b -3 i and 3b > 2a }" },
587 C(&gist<isl::set> , {
588 { TWO("{ [1, -1, 3] }"),
589 "{ [1, b, 2 - b] : -1 <= b <= 2 }",
590 "{ [a, -1, c] }" },
591 { TWO("{ [a, b, c] : a <= 15 and a >= 1 }"),
592 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
593 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
594 "{ [a, b, c] : a <= 15 }" },
595 { TWO("{ : }"), "{ : 1 = 0 }", "{ : }" },
596 { TWO("{ : 1 = 0 }"), "{ : 1 = 0 }", "{ : }" },
597 { TWO("[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }"),
598 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
599 { TWO("{ [m, n, a, b] : a <= 2147 + n }"),
600 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
601 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
602 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
603 "b >= 0) }",
604 "{ [m, n, ku, kl] }" },
605 { TWO("{ [a, a, b] : a >= 10 }"),
606 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
607 "{ [a, a, b] : a >= 10 }" },
608 { TWO("{ [i, j] : i >= 0 and i + j >= 0 }"), "{ [i, j] : i <= 0 }",
609 "{ [0, j] : j >= 0 }" },
610 /* Check that no constraints on i6 are introduced in the gist */
611 { TWO("[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
612 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
613 "5e0 <= 381 - t1 and i4 <= 1) }"),
614 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
615 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
616 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
617 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
618 "20e0 >= 1511 - 4t1 - 5i4) }" },
619 /* Check that no constraints on i6 are introduced in the gist */
620 { TWO("[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
621 "e1 = floor((1530 - 4t1 - 5i4)/20), "
622 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
623 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
624 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
625 "2e0 <= 1 + i4 and 2e0 >= i4 and "
626 "20e1 <= 1530 - 4t1 - 5i4 and "
627 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
628 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }"),
629 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
630 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
631 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
632 "10e0 <= -91 + 5i4 + 4i6 and "
633 "10e0 >= -105 + 5i4 + 4i6) }",
634 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
635 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
636 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
637 { TWO("{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }"),
638 "{ [a, b, q, p] : b >= 1 + a }",
639 "{ [a, b, q, p] : false }" },
640 { TWO("[n] -> { [x] : x = n && x mod 32 = 0 }"),
641 "[n] -> { [x] : x mod 32 = 0 }",
642 "[n] -> { [x = n] }" },
643 { TWO("{ [x] : x mod 6 = 0 }"), "{ [x] : x mod 3 = 0 }",
644 "{ [x] : x mod 2 = 0 }" },
645 { TWO("{ [x] : x mod 3200 = 0 }"), "{ [x] : x mod 10000 = 0 }",
646 "{ [x] : x mod 128 = 0 }" },
647 { TWO("{ [x] : x mod 3200 = 0 }"), "{ [x] : x mod 10 = 0 }",
648 "{ [x] : x mod 3200 = 0 }" },
649 { TWO("{ [a, b, c] : a mod 2 = 0 and a = c }"),
650 "{ [a, b, c] : b mod 2 = 0 and b = c }",
651 "{ [a, b, c = a] }" },
652 { TWO("{ [a, b, c] : a mod 6 = 0 and a = c }"),
653 "{ [a, b, c] : b mod 2 = 0 and b = c }",
654 "{ [a, b, c = a] : a mod 3 = 0 }" },
655 { TWO("{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }"),
656 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
657 "{ [x] }" },
658 { TWO("{ [x,y] : x < 0 and 0 <= y <= 4 or "
659 "x >= -2 and -x <= y <= 10 + x }"),
660 "{ [x,y] : 1 <= y <= 3 }",
661 "{ [x,y] }" },
664 C(arg<isl::set>(&isl::pw_aff::gist), {
665 { "{ [x] -> [x] : x != 0 }", "{ [x] : x < -1 or x > 1 }",
666 "{ [x] -> [x] }" },
669 C(&isl::pw_aff::gist_params, {
670 { "[N] -> { D[x] -> [x] : N >= 0; D[x] -> [0] : N < 0 }",
671 "[N] -> { : N >= 0 }",
672 "[N] -> { D[x] -> [x] }" },
675 C(arg<isl::set>(&isl::multi_aff::gist), {
676 { "{ A[i] -> B[i, i] }", "{ A[0] }",
677 "{ A[i] -> B[0, 0] }" },
678 { "[N] -> { A[i] -> B[i, N] }", "[N] -> { A[0] : N = 5 }",
679 "[N] -> { A[i] -> B[0, 5] }" },
680 { "[N] -> { B[N + 1, N] }", "[N] -> { : N = 5 }",
681 "[N] -> { B[6, 5] }" },
682 { "[N] -> { A[i] -> B[] }", "[N] -> { A[0] : N = 5 }",
683 "[N] -> { A[i] -> B[] }" },
684 { "[N] -> { B[] }", "[N] -> { : N = 5 }",
685 "[N] -> { B[] }" },
688 C(&isl::multi_aff::gist_params, {
689 { "[N] -> { A[i] -> B[i, N] }", "[N] -> { : N = 5 }",
690 "[N] -> { A[i] -> B[i, 5] }" },
691 { "[N] -> { B[N + 1, N] }", "[N] -> { : N = 5 }",
692 "[N] -> { B[6, 5] }" },
693 { "[N] -> { A[i] -> B[] }", "[N] -> { : N = 5 }",
694 "[N] -> { A[i] -> B[] }" },
695 { "[N] -> { B[] }", "[N] -> { : N = 5 }",
696 "[N] -> { B[] }" },
699 C(arg<isl::set>(&isl::multi_pw_aff::gist), {
700 { "{ A[i] -> B[i, i] : i >= 0 }", "{ A[0] }",
701 "{ A[i] -> B[0, 0] }" },
702 { "[N] -> { A[i] -> B[i, N] : N >= 0 }", "[N] -> { A[0] : N = 5 }",
703 "[N] -> { A[i] -> B[0, 5] }" },
704 { "[N] -> { B[N + 1, N] }", "[N] -> { : N = 5 }",
705 "[N] -> { B[6, 5] }" },
706 { "[N] -> { A[i] -> B[] }", "[N] -> { A[0] : N = 5 }",
707 "[N] -> { A[i] -> B[] }" },
708 { "[N] -> { B[] }", "[N] -> { : N = 5 }",
709 "[N] -> { B[] }" },
710 { "{ A[i=0:10] -> B[i] }", "{ A[5] }",
711 "{ A[i] -> B[5] }" },
712 { "{ A[0:10] -> B[] }", "{ A[0:10] }",
713 "{ A[i] -> B[] }" },
714 { "[N] -> { A[i] -> B[] : N >= 0 }", "[N] -> { A[0] : N = 5 }",
715 "[N] -> { A[i] -> B[] }" },
716 { "[N] -> { B[] : N >= 0 }", "[N] -> { : N = 5 }",
717 "[N] -> { B[] }" },
718 { "[N] -> { B[] : N = 5 }", "[N] -> { : N >= 0 }",
719 "[N] -> { B[] : N = 5 }" },
722 C(&isl::multi_pw_aff::gist_params, {
723 { "[N] -> { A[i] -> B[i, N] : N >= 0 }", "[N] -> { : N = 5 }",
724 "[N] -> { A[i] -> B[i, 5] }" },
725 { "[N] -> { B[N + 1, N] }", "[N] -> { : N = 5 }",
726 "[N] -> { B[6, 5] }" },
727 { "[N] -> { A[i] -> B[] : N >= 0 }", "[N] -> { : N = 5 }",
728 "[N] -> { A[i] -> B[] }" },
729 { "[N] -> { B[] : N >= 0 }", "[N] -> { : N = 5 }",
730 "[N] -> { B[] }" },
731 { "[N] -> { B[] : N >= 5 }", "[N] -> { : N >= 0 }",
732 "[N] -> { B[] : N >= 5 }" },
735 C(&isl::multi_union_pw_aff::gist, {
736 { "C[{ B[i,i] -> [3i] }]", "{ B[i,i] }",
737 "C[{ B[i,j] -> [3i] }]" },
738 { "(C[] : { B[i,i] })", "{ B[i,i] }",
739 "(C[] : { B[i,j] })" },
740 { "[N] -> (C[] : { B[N,N] })", "[N] -> { B[N,N] }",
741 "[N] -> (C[] : { B[i,j] })" },
742 { "C[]", "{ B[i,i] }",
743 "C[]" },
744 { "[N] -> (C[] : { B[i,i] : N >= 0 })", "{ B[i,i] }",
745 "[N] -> (C[] : { B[i,j] : N >= 0 })" },
746 { "[N] -> (C[] : { : N >= 0 })", "{ B[i,i] }",
747 "[N] -> (C[] : { : N >= 0 })" },
748 { "[N] -> (C[] : { : N >= 0 })", "[N] -> { B[i,i] : N >= 0 }",
749 "[N] -> C[]" },
752 C(&isl::multi_union_pw_aff::gist_params, {
753 { "[N] -> C[{ B[i,i] -> [3i + N] }]", "[N] -> { : N = 1 }",
754 "[N] -> C[{ B[i,i] -> [3i + 1] }]" },
755 { "C[{ B[i,i] -> [3i] }]", "[N] -> { : N >= 0 }",
756 "[N] -> C[{ B[i,i] -> [3i] }]" },
757 { "[N] -> C[{ B[i,i] -> [3i] : N >= 0 }]", "[N] -> { : N >= 0 }",
758 "[N] -> C[{ B[i,i] -> [3i] }]" },
759 { "[N] -> C[{ B[i,i] -> [3i] : N >= 1 }]", "[N] -> { : N >= 0 }",
760 "[N] -> C[{ B[i,i] -> [3i] : N >= 1 }]" },
761 { "[N] -> (C[] : { B[i,i] : N >= 0 })", "[N] -> { : N >= 0 }",
762 "[N] -> (C[] : { B[i,i] })" },
763 { "[N] -> (C[] : { : N >= 0 })", "[N] -> { : N >= 0 }",
764 "[N] -> C[]" },
765 { "C[{ B[i,i] -> [3i] }]", "[N] -> { : N >= 0 }",
766 "[N] -> C[{ B[i,i] -> [3i] }]" },
770 /* Perform tests that project out parameters.
772 static void test_project(isl::ctx ctx)
774 C(arg<isl::id>(&isl::union_map::project_out_param), {
775 { "[N] -> { D[i] -> A[0:N-1]; D[i] -> B[i] }", "N",
776 "{ D[i] -> A[0:]; D[i] -> B[i] }" },
777 { "[N] -> { D[i] -> A[0:N-1]; D[i] -> B[i] }", "M",
778 "[N] -> { D[i] -> A[0:N-1]; D[i] -> B[i] }" },
781 C(arg<isl::id_list>(&isl::union_map::project_out_param), {
782 { "[M, N, O] -> { D[i] -> A[j] : i <= j < M, N, O }", "(M, N)",
783 "[O] -> { D[i] -> A[j] : i <= j < O }" },
787 /* Perform some basic reverse tests.
789 static void test_reverse(isl::ctx ctx)
791 C(&isl::aff::domain_reverse, {
792 { "{ T[A[] -> B[*]] -> [0] }",
793 "{ [B[*] -> A[]] -> [0] }" },
794 { "{ T[A[] -> A[]] -> [0] }",
795 "{ T[A[] -> A[]] -> [0] }" },
796 { "{ [A[x] -> B[y]] -> [5*(x // 2) + 7*(y // 3)] }",
797 "{ [B[y] -> A[x]] -> [5*(x // 2) + 7*(y // 3)] }" },
800 C(&isl::multi_aff::domain_reverse, {
801 { "{ [A[x] -> B[y]] -> [5*(x // 2) + 7*(y // 3)] }",
802 "{ [B[y] -> A[x]] -> [5*(x // 2) + 7*(y // 3)] }" },
803 { "{ [A[x] -> B[y]] -> T[5*(x // 2) + 7*(y // 3), 0] }",
804 "{ [B[y] -> A[x]] -> T[5*(x // 2) + 7*(y // 3), 0] }" },
807 C(&isl::set::wrapped_reverse, {
808 { "{ T[A[] -> B[*]] }",
809 "{ [B[*] -> A[]] }" },
810 { "{ T[A[] -> A[]] }",
811 "{ T[A[] -> A[]] }" },
812 { "{ [A[x] -> B[2x]] }",
813 "{ [B[y] -> A[x]] : y = 2x }" },
816 C(&isl::pw_aff::domain_reverse, {
817 { "{ [A[x] -> B[y]] -> [5*(x // 2) + 7*(y // 3)] }",
818 "{ [B[y] -> A[x]] -> [5*(x // 2) + 7*(y // 3)] }" },
819 { "{ [A[x] -> B[y]] -> [5*(x // 2) + 7*(y // 3)] : x > y }",
820 "{ [B[y] -> A[x]] -> [5*(x // 2) + 7*(y // 3)] : x > y }" },
821 { "{ [A[i] -> B[i + 1]] -> [i + 2] }",
822 "{ [B[i] -> A[i - 1]] -> [i + 1] }" },
825 C(&isl::pw_multi_aff::domain_reverse, {
826 { "{ [A[x] -> B[y]] -> T[5*(x // 2) + 7*(y // 3), 0] : x > y }",
827 "{ [B[y] -> A[x]] -> T[5*(x // 2) + 7*(y // 3), 0] : x > y }" },
828 { "{ [A[i] -> B[i + 1]] -> T[0, i + 2] }",
829 "{ [B[i] -> A[i - 1]] -> T[0, i + 1] }" },
832 C(&isl::multi_pw_aff::domain_reverse, {
833 { "{ [A[x] -> B[y]] -> T[5*(x // 2) + 7*(y // 3) : x > y, 0] }",
834 "{ [B[y] -> A[x]] -> T[5*(x // 2) + 7*(y // 3) : x > y, 0] }" },
837 C(&isl::map::domain_reverse, {
838 { "{ [A[] -> B[]] -> [C[] -> D[]] }",
839 "{ [B[] -> A[]] -> [C[] -> D[]] }" },
840 { "{ N[B[] -> C[]] -> A[] }",
841 "{ [C[] -> B[]] -> A[] }" },
842 { "{ N[B[x] -> B[y]] -> A[] }",
843 "{ N[B[*] -> B[*]] -> A[] }" },
846 C(&isl::union_map::domain_reverse, {
847 { "{ [A[] -> B[]] -> [C[] -> D[]] }",
848 "{ [B[] -> A[]] -> [C[] -> D[]] }" },
849 { "{ A[] -> [B[] -> C[]]; A[] -> B[]; A[0] -> N[B[1] -> B[2]] }",
850 "{ }" },
851 { "{ N[B[] -> C[]] -> A[] }",
852 "{ [C[] -> B[]] -> A[] }" },
853 { "{ N[B[x] -> B[y]] -> A[] }",
854 "{ N[B[*] -> B[*]] -> A[] }" },
857 C(&isl::union_map::range_reverse, {
858 { "{ A[] -> [B[] -> C[]]; A[] -> B[]; A[0] -> N[B[1] -> B[2]] }",
859 "{ A[] -> [C[] -> B[]]; A[0] -> N[B[2] -> B[1]] }" },
860 { "{ A[] -> N[B[] -> C[]] }",
861 "{ A[] -> [C[] -> B[]] }" },
862 { "{ A[] -> N[B[x] -> B[y]] }",
863 "{ A[] -> N[B[*] -> B[*]] }" },
867 /* Perform some basic scaling tests.
869 static void test_scale(isl::ctx ctx)
871 C(arg<isl::multi_val>(&isl::pw_multi_aff::scale), {
872 { "{ A[a] -> B[a, a + 1, a - 1] : a >= 0 }", "{ B[2, 7, 0] }",
873 "{ A[a] -> B[2a, 7a + 7, 0] : a >= 0 }" },
875 C(arg<isl::multi_val>(&isl::pw_multi_aff::scale), {
876 { "{ A[a] -> B[1, a - 1] : a >= 0 }", "{ B[1/2, 7] }",
877 "{ A[a] -> B[1/2, 7a - 7] : a >= 0 }" },
880 C(arg<isl::multi_val>(&isl::pw_multi_aff::scale_down), {
881 { "{ A[a] -> B[a, a + 1] : a >= 0 }", "{ B[2, 7] }",
882 "{ A[a] -> B[a/2, (a + 1)/7] : a >= 0 }" },
884 C(arg<isl::multi_val>(&isl::pw_multi_aff::scale_down), {
885 { "{ A[a] -> B[a, a - 1] : a >= 0 }", "{ B[2, 1/7] }",
886 "{ A[a] -> B[a/2, 7a - 7] : a >= 0 }" },
890 /* Perform some basic isl::id_to_id tests.
892 static void test_id_to_id(isl::ctx ctx)
894 C((arg<isl::id, isl::id>(&isl::id_to_id::set)), {
895 { "{ }", "a", "b",
896 "{ a: b }" },
897 { "{ a: b }", "a", "b",
898 "{ a: b }" },
899 { "{ a: c }", "a", "b",
900 "{ a: b }" },
901 { "{ a: b }", "b", "a",
902 "{ a: b, b: a }" },
903 { "{ a: b }", "b", "a",
904 "{ b: a, a: b }" },
908 /* The list of tests to perform.
910 static std::vector<std::pair<const char *, void (*)(isl::ctx)>> tests =
912 { "space", &test_space },
913 { "conversion", &test_conversion },
914 { "preimage", &test_preimage },
915 { "fixed power", &test_fixed_power },
916 { "intersect", &test_intersect },
917 { "lexmin", &test_lexmin },
918 { "gist", &test_gist },
919 { "project out parameters", &test_project },
920 { "reverse", &test_reverse },
921 { "scale", &test_scale },
922 { "id-to-id", &test_id_to_id },
925 /* Perform some basic checks by means of the C++ bindings.
927 int main(int argc, char **argv)
929 int ret = EXIT_SUCCESS;
930 struct isl_ctx *ctx;
931 struct isl_options *options;
933 options = isl_options_new_with_defaults();
934 assert(options);
935 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
936 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
938 try {
939 for (const auto &f : tests) {
940 std::cout << f.first << "\n";
941 f.second(ctx);
943 } catch (const isl::exception &e) {
944 std::cerr << e.what() << "\n";
945 ret = EXIT_FAILURE;
948 isl_ctx_free(ctx);
949 return ret;