1 /*
2  * Copyright 2012-2013 Ecole Normale Superieure
3  *
4  * Use of this software is governed by the MIT license
5  *
6  * Written by Sven Verdoolaege,
7  * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
8  */
9 
10 #include <isl/space.h>
11 #include <isl/val.h>
12 #include <isl/aff.h>
13 #include <isl/set.h>
14 
15 #include "util.h"
16 
17 /* Construct an isl_multi_val living in "space" with all values equal to "val".
18  */
ppcg_multi_val_from_int(__isl_take isl_space * space,int val)19 __isl_give isl_multi_val *ppcg_multi_val_from_int(__isl_take isl_space *space,
20 	int val)
21 {
22 	int i, n;
23 	isl_ctx *ctx;
24 	isl_val *v;
25 	isl_multi_val *mv;
26 
27 	if (!space)
28 		return NULL;
29 
30 	ctx = isl_space_get_ctx(space);
31 	n = isl_space_dim(space, isl_dim_set);
32 	mv = isl_multi_val_zero(space);
33 	v = isl_val_int_from_si(ctx, val);
34 	for (i = 0; i < n; ++i)
35 		mv = isl_multi_val_set_val(mv, i, isl_val_copy(v));
36 	isl_val_free(v);
37 
38 	return mv;
39 }
40 
41 /* Construct an isl_multi_val living in "space" with values specified
42  * by "list".  "list" is assumed to have at least as many entries
43  * as the set dimension of "space".
44  */
ppcg_multi_val_from_int_list(__isl_take isl_space * space,int * list)45 __isl_give isl_multi_val *ppcg_multi_val_from_int_list(
46 	__isl_take isl_space *space, int *list)
47 {
48 	int i, n;
49 	isl_ctx *ctx;
50 	isl_multi_val *mv;
51 
52 	if (!space)
53 		return NULL;
54 
55 	ctx = isl_space_get_ctx(space);
56 	n = isl_space_dim(space, isl_dim_set);
57 	mv = isl_multi_val_zero(space);
58 	for (i = 0; i < n; ++i) {
59 		isl_val *v;
60 
61 		v = isl_val_int_from_si(ctx, list[i]);
62 		mv = isl_multi_val_set_val(mv, i, v);
63 	}
64 
65 	return mv;
66 }
67 
68 /* Compute the size of a bounding box around the origin and "set",
69  * where "set" is assumed to contain only non-negative elements.
70  * In particular, compute the maximal value of "set" in each direction
71  * and add one.
72  */
ppcg_size_from_extent(__isl_take isl_set * set)73 __isl_give isl_multi_pw_aff *ppcg_size_from_extent(__isl_take isl_set *set)
74 {
75 	int i, n;
76 	isl_multi_pw_aff *mpa;
77 
78 	n = isl_set_dim(set, isl_dim_set);
79 	mpa = isl_multi_pw_aff_zero(isl_set_get_space(set));
80 	for (i = 0; i < n; ++i) {
81 		isl_space *space;
82 		isl_aff *one;
83 		isl_pw_aff *bound;
84 
85 		if (!isl_set_dim_has_upper_bound(set, isl_dim_set, i)) {
86 			const char *name;
87 			name = isl_set_get_tuple_name(set);
88 			if (!name)
89 				name = "";
90 			fprintf(stderr, "unable to determine extent of '%s' "
91 				"in dimension %d\n", name, i);
92 			set = isl_set_free(set);
93 		}
94 		bound = isl_set_dim_max(isl_set_copy(set), i);
95 
96 		space = isl_pw_aff_get_domain_space(bound);
97 		one = isl_aff_zero_on_domain(isl_local_space_from_space(space));
98 		one = isl_aff_add_constant_si(one, 1);
99 		bound = isl_pw_aff_add(bound, isl_pw_aff_from_aff(one));
100 		mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, bound);
101 	}
102 	isl_set_free(set);
103 
104 	return mpa;
105 }
106