isl 0.27
[isl.git] / isl_multi_arith_templ.c
blob4e61051fd445fd0fe686fd311fbaee9aadc51aec
1 /*
2 * Copyright 2011 Sven Verdoolaege
3 * Copyright 2012-2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
9 */
11 #include <isl/space.h>
12 #include <isl_val_private.h>
14 #include <isl_multi_macro.h>
16 /* Add "multi2" to "multi1" and return the result.
18 __isl_give MULTI(BASE) *FN(MULTI(BASE),add)(__isl_take MULTI(BASE) *multi1,
19 __isl_take MULTI(BASE) *multi2)
21 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,add));
24 /* Subtract "multi2" from "multi1" and return the result.
26 __isl_give MULTI(BASE) *FN(MULTI(BASE),sub)(__isl_take MULTI(BASE) *multi1,
27 __isl_take MULTI(BASE) *multi2)
29 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,sub));
32 /* Depending on "fn", multiply or divide the elements of "multi" by "v" and
33 * return the result.
35 static __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_val_fn)(
36 __isl_take MULTI(BASE) *multi, __isl_take isl_val *v,
37 __isl_give EL *(*fn)(__isl_take EL *el, __isl_take isl_val *v))
39 if (!multi || !v)
40 goto error;
42 if (isl_val_is_one(v)) {
43 isl_val_free(v);
44 return multi;
47 if (!isl_val_is_rat(v))
48 isl_die(isl_val_get_ctx(v), isl_error_invalid,
49 "expecting rational factor", goto error);
51 return FN(MULTI(BASE),fn_val)(multi, fn, v);
52 error:
53 isl_val_free(v);
54 return FN(MULTI(BASE),free)(multi);
57 /* Multiply the elements of "multi" by "v" and return the result.
59 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_val)(__isl_take MULTI(BASE) *multi,
60 __isl_take isl_val *v)
62 return FN(MULTI(BASE),scale_val_fn)(multi, v, &FN(EL,scale_val));
65 /* Divide the elements of "multi" by "v" and return the result.
67 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_val)(
68 __isl_take MULTI(BASE) *multi, __isl_take isl_val *v)
70 if (!v)
71 goto error;
72 if (isl_val_is_zero(v))
73 isl_die(isl_val_get_ctx(v), isl_error_invalid,
74 "cannot scale down by zero", goto error);
75 return FN(MULTI(BASE),scale_val_fn)(multi, v, &FN(EL,scale_down_val));
76 error:
77 isl_val_free(v);
78 return FN(MULTI(BASE),free)(multi);
81 /* Multiply the elements of "multi" by the corresponding element of "mv"
82 * and return the result.
84 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_multi_val)(
85 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
87 return FN(MULTI(BASE),fn_multi_val)(multi, &FN(EL,scale_val), mv);
90 /* Divide the elements of "multi" by the corresponding element of "mv"
91 * and return the result.
93 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_multi_val)(
94 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
96 return FN(MULTI(BASE),fn_multi_val)(multi, &FN(EL,scale_down_val), mv);
99 /* Compute the residues of the elements of "multi" modulo
100 * the corresponding element of "mv" and return the result.
102 __isl_give MULTI(BASE) *FN(MULTI(BASE),mod_multi_val)(
103 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
105 return FN(MULTI(BASE),fn_multi_val)(multi, &FN(EL,mod_val), mv);
108 /* Return the opposite of "multi".
110 __isl_give MULTI(BASE) *FN(MULTI(BASE),neg)(__isl_take MULTI(BASE) *multi)
112 S(MULTI(BASE),un_op_control) control = { .fn_el = &FN(EL,neg) };
113 return FN(MULTI(BASE),un_op)(multi, &control);