xref: /netbsd-src/external/mit/isl/dist/isl_multi_arith_templ.c (revision 5971e316fdea024efff6be8f03536623db06833e)
1*5971e316Smrg /*
2*5971e316Smrg  * Copyright 2011      Sven Verdoolaege
3*5971e316Smrg  * Copyright 2012-2013 Ecole Normale Superieure
4*5971e316Smrg  *
5*5971e316Smrg  * Use of this software is governed by the MIT license
6*5971e316Smrg  *
7*5971e316Smrg  * Written by Sven Verdoolaege,
8*5971e316Smrg  * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
9*5971e316Smrg  */
10*5971e316Smrg 
11*5971e316Smrg #include <isl/space.h>
12*5971e316Smrg #include <isl_val_private.h>
13*5971e316Smrg 
14*5971e316Smrg #include <isl_multi_macro.h>
15*5971e316Smrg 
16*5971e316Smrg /* Add "multi2" to "multi1" and return the result.
17*5971e316Smrg  */
MULTI(BASE)18*5971e316Smrg __isl_give MULTI(BASE) *FN(MULTI(BASE),add)(__isl_take MULTI(BASE) *multi1,
19*5971e316Smrg 	__isl_take MULTI(BASE) *multi2)
20*5971e316Smrg {
21*5971e316Smrg 	return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,add));
22*5971e316Smrg }
23*5971e316Smrg 
24*5971e316Smrg /* Subtract "multi2" from "multi1" and return the result.
25*5971e316Smrg  */
MULTI(BASE)26*5971e316Smrg __isl_give MULTI(BASE) *FN(MULTI(BASE),sub)(__isl_take MULTI(BASE) *multi1,
27*5971e316Smrg 	__isl_take MULTI(BASE) *multi2)
28*5971e316Smrg {
29*5971e316Smrg 	return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,sub));
30*5971e316Smrg }
31*5971e316Smrg 
32*5971e316Smrg /* Depending on "fn", multiply or divide the elements of "multi" by "v" and
33*5971e316Smrg  * return the result.
34*5971e316Smrg  */
MULTI(BASE)35*5971e316Smrg static __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_val_fn)(
36*5971e316Smrg 	__isl_take MULTI(BASE) *multi, __isl_take isl_val *v,
37*5971e316Smrg 	__isl_give EL *(*fn)(__isl_take EL *el, __isl_take isl_val *v))
38*5971e316Smrg {
39*5971e316Smrg 	if (!multi || !v)
40*5971e316Smrg 		goto error;
41*5971e316Smrg 
42*5971e316Smrg 	if (isl_val_is_one(v)) {
43*5971e316Smrg 		isl_val_free(v);
44*5971e316Smrg 		return multi;
45*5971e316Smrg 	}
46*5971e316Smrg 
47*5971e316Smrg 	if (!isl_val_is_rat(v))
48*5971e316Smrg 		isl_die(isl_val_get_ctx(v), isl_error_invalid,
49*5971e316Smrg 			"expecting rational factor", goto error);
50*5971e316Smrg 
51*5971e316Smrg 	return FN(MULTI(BASE),fn_val)(multi, fn, v);
52*5971e316Smrg error:
53*5971e316Smrg 	isl_val_free(v);
54*5971e316Smrg 	return FN(MULTI(BASE),free)(multi);
55*5971e316Smrg }
56*5971e316Smrg 
57*5971e316Smrg /* Multiply the elements of "multi" by "v" and return the result.
58*5971e316Smrg  */
MULTI(BASE)59*5971e316Smrg __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_val)(__isl_take MULTI(BASE) *multi,
60*5971e316Smrg 	__isl_take isl_val *v)
61*5971e316Smrg {
62*5971e316Smrg 	return FN(MULTI(BASE),scale_val_fn)(multi, v, &FN(EL,scale_val));
63*5971e316Smrg }
64*5971e316Smrg 
65*5971e316Smrg /* Divide the elements of "multi" by "v" and return the result.
66*5971e316Smrg  */
MULTI(BASE)67*5971e316Smrg __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_val)(
68*5971e316Smrg 	__isl_take MULTI(BASE) *multi, __isl_take isl_val *v)
69*5971e316Smrg {
70*5971e316Smrg 	if (!v)
71*5971e316Smrg 		goto error;
72*5971e316Smrg 	if (isl_val_is_zero(v))
73*5971e316Smrg 		isl_die(isl_val_get_ctx(v), isl_error_invalid,
74*5971e316Smrg 			"cannot scale down by zero", goto error);
75*5971e316Smrg 	return FN(MULTI(BASE),scale_val_fn)(multi, v, &FN(EL,scale_down_val));
76*5971e316Smrg error:
77*5971e316Smrg 	isl_val_free(v);
78*5971e316Smrg 	return FN(MULTI(BASE),free)(multi);
79*5971e316Smrg }
80*5971e316Smrg 
81*5971e316Smrg /* Multiply the elements of "multi" by the corresponding element of "mv"
82*5971e316Smrg  * and return the result.
83*5971e316Smrg  */
MULTI(BASE)84*5971e316Smrg __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_multi_val)(
85*5971e316Smrg 	__isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
86*5971e316Smrg {
87*5971e316Smrg 	return FN(MULTI(BASE),fn_multi_val)(multi, &FN(EL,scale_val), mv);
88*5971e316Smrg }
89*5971e316Smrg 
90*5971e316Smrg /* Divide the elements of "multi" by the corresponding element of "mv"
91*5971e316Smrg  * and return the result.
92*5971e316Smrg  */
MULTI(BASE)93*5971e316Smrg __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_multi_val)(
94*5971e316Smrg 	__isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
95*5971e316Smrg {
96*5971e316Smrg 	return FN(MULTI(BASE),fn_multi_val)(multi, &FN(EL,scale_down_val), mv);
97*5971e316Smrg }
98*5971e316Smrg 
99*5971e316Smrg /* Compute the residues of the elements of "multi" modulo
100*5971e316Smrg  * the corresponding element of "mv" and return the result.
101*5971e316Smrg  */
MULTI(BASE)102*5971e316Smrg __isl_give MULTI(BASE) *FN(MULTI(BASE),mod_multi_val)(
103*5971e316Smrg 	__isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
104*5971e316Smrg {
105*5971e316Smrg 	return FN(MULTI(BASE),fn_multi_val)(multi, &FN(EL,mod_val), mv);
106*5971e316Smrg }
107*5971e316Smrg 
108*5971e316Smrg /* Return the opposite of "multi".
109*5971e316Smrg  */
MULTI(BASE)110*5971e316Smrg __isl_give MULTI(BASE) *FN(MULTI(BASE),neg)(__isl_take MULTI(BASE) *multi)
111*5971e316Smrg {
112*5971e316Smrg 	S(MULTI(BASE),un_op_control) control = { .fn_el = &FN(EL,neg) };
113*5971e316Smrg 	return FN(MULTI(BASE),un_op)(multi, &control);
114*5971e316Smrg }
115