1 /*
2 * Copyright 2019 Cerebras Systems
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Sven Verdoolaege,
7 * Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
8 */
9
10 #include <isl_multi_macro.h>
11
12 /* Add "v" to the constant terms of all the base expressions of "multi".
13 */
MULTI(BASE)14 __isl_give MULTI(BASE) *FN(MULTI(BASE),add_constant_val)(
15 __isl_take MULTI(BASE) *multi, __isl_take isl_val *v)
16 {
17 isl_bool zero;
18 isl_size n;
19 int i;
20
21 zero = isl_val_is_zero(v);
22 n = FN(MULTI(BASE),size)(multi);
23 if (zero < 0 || n < 0)
24 goto error;
25 if (zero || n == 0) {
26 isl_val_free(v);
27 return multi;
28 }
29
30 multi = FN(MULTI(BASE),cow)(multi);
31 if (!multi)
32 goto error;
33
34 for (i = 0; i < n; ++i) {
35 multi->u.p[i] = FN(EL,add_constant_val)(multi->u.p[i],
36 isl_val_copy(v));
37 if (!multi->u.p[i])
38 goto error;
39 }
40
41 isl_val_free(v);
42 return multi;
43 error:
44 FN(MULTI(BASE),free)(multi);
45 isl_val_free(v);
46 return NULL;
47 }
48
49 /* Add the elements of "mv" to the constant terms of
50 * the corresponding base expressions of "multi".
51 */
MULTI(BASE)52 __isl_give MULTI(BASE) *FN(MULTI(BASE),add_constant_multi_val)(
53 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
54 {
55 isl_space *multi_space, *mv_space;
56 isl_bool zero, equal;
57 isl_size n;
58 int i;
59
60 zero = isl_multi_val_is_zero(mv);
61 n = FN(MULTI(BASE),size)(multi);
62 multi_space = FN(MULTI(BASE),peek_space)(multi);
63 mv_space = isl_multi_val_peek_space(mv);
64 equal = isl_space_tuple_is_equal(multi_space, isl_dim_out,
65 mv_space, isl_dim_out);
66 if (zero < 0 || n < 0 || equal < 0)
67 goto error;
68 if (!equal)
69 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
70 "spaces don't match", goto error);
71 if (zero || n == 0) {
72 isl_multi_val_free(mv);
73 return multi;
74 }
75
76 multi = FN(MULTI(BASE),cow)(multi);
77 if (!multi)
78 goto error;
79
80 for (i = 0; i < n; ++i) {
81 isl_val *v = isl_multi_val_get_at(mv, i);
82 multi->u.p[i] = FN(EL,add_constant_val)(multi->u.p[i], v);
83 if (!multi->u.p[i])
84 goto error;
85 }
86
87 isl_multi_val_free(mv);
88 return multi;
89 error:
90 FN(MULTI(BASE),free)(multi);
91 isl_multi_val_free(mv);
92 return NULL;
93 }
94