1*5971e316Smrg /*
2*5971e316Smrg * Copyright 2010 INRIA Saclay
3*5971e316Smrg * Copyright 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, INRIA Saclay - Ile-de-France,
8*5971e316Smrg * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9*5971e316Smrg * 91893 Orsay, France
10*5971e316Smrg * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
11*5971e316Smrg */
12*5971e316Smrg
13*5971e316Smrg #include <isl/val.h>
14*5971e316Smrg #include <isl_space_private.h>
15*5971e316Smrg #include <isl_point_private.h>
16*5971e316Smrg
17*5971e316Smrg #include <isl_pw_macro.h>
18*5971e316Smrg
19*5971e316Smrg #undef SUFFIX
20*5971e316Smrg #define SUFFIX point
21*5971e316Smrg #undef ARG1
22*5971e316Smrg #define ARG1 PW
23*5971e316Smrg #undef ARG2
24*5971e316Smrg #define ARG2 isl_point
25*5971e316Smrg
26*5971e316Smrg static
27*5971e316Smrg #include "isl_align_params_templ.c"
28*5971e316Smrg
29*5971e316Smrg /* Evaluate "pw" in the void point "pnt".
30*5971e316Smrg * In particular, return the value NaN.
31*5971e316Smrg */
FN(PW,eval_void)32*5971e316Smrg static __isl_give isl_val *FN(PW,eval_void)(__isl_take PW *pw,
33*5971e316Smrg __isl_take isl_point *pnt)
34*5971e316Smrg {
35*5971e316Smrg isl_ctx *ctx;
36*5971e316Smrg
37*5971e316Smrg ctx = isl_point_get_ctx(pnt);
38*5971e316Smrg FN(PW,free)(pw);
39*5971e316Smrg isl_point_free(pnt);
40*5971e316Smrg return isl_val_nan(ctx);
41*5971e316Smrg }
42*5971e316Smrg
43*5971e316Smrg /* Evaluate the piecewise function "pw" in "pnt".
44*5971e316Smrg * If the point is void, then return NaN.
45*5971e316Smrg * If the point lies outside the domain of "pw", then return 0 or NaN
46*5971e316Smrg * depending on whether 0 is the default value for this type of function.
47*5971e316Smrg *
48*5971e316Smrg * Align the parameters if needed, but "pnt" should specify a value
49*5971e316Smrg * for all parameters in "pw".
50*5971e316Smrg */
FN(PW,eval)51*5971e316Smrg __isl_give isl_val *FN(PW,eval)(__isl_take PW *pw, __isl_take isl_point *pnt)
52*5971e316Smrg {
53*5971e316Smrg int i;
54*5971e316Smrg isl_bool is_void;
55*5971e316Smrg isl_bool found;
56*5971e316Smrg isl_ctx *ctx;
57*5971e316Smrg isl_bool ok;
58*5971e316Smrg isl_space *pnt_space, *pw_space;
59*5971e316Smrg isl_val *v;
60*5971e316Smrg
61*5971e316Smrg FN(PW,align_params_point)(&pw, &pnt);
62*5971e316Smrg
63*5971e316Smrg pnt_space = isl_point_peek_space(pnt);
64*5971e316Smrg pw_space = FN(PW,peek_space)(pw);
65*5971e316Smrg ok = isl_space_is_domain_internal(pnt_space, pw_space);
66*5971e316Smrg if (ok < 0)
67*5971e316Smrg goto error;
68*5971e316Smrg ctx = isl_point_get_ctx(pnt);
69*5971e316Smrg if (!ok)
70*5971e316Smrg isl_die(ctx, isl_error_invalid,
71*5971e316Smrg "incompatible spaces", goto error);
72*5971e316Smrg is_void = isl_point_is_void(pnt);
73*5971e316Smrg if (is_void < 0)
74*5971e316Smrg goto error;
75*5971e316Smrg if (is_void)
76*5971e316Smrg return FN(PW,eval_void)(pw, pnt);
77*5971e316Smrg
78*5971e316Smrg found = isl_bool_false;
79*5971e316Smrg for (i = 0; i < pw->n; ++i) {
80*5971e316Smrg found = isl_set_contains_point(pw->p[i].set, pnt);
81*5971e316Smrg if (found < 0)
82*5971e316Smrg goto error;
83*5971e316Smrg if (found)
84*5971e316Smrg break;
85*5971e316Smrg }
86*5971e316Smrg if (found) {
87*5971e316Smrg v = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
88*5971e316Smrg isl_point_copy(pnt));
89*5971e316Smrg } else if (DEFAULT_IS_ZERO) {
90*5971e316Smrg v = isl_val_zero(ctx);
91*5971e316Smrg } else {
92*5971e316Smrg v = isl_val_nan(ctx);
93*5971e316Smrg }
94*5971e316Smrg FN(PW,free)(pw);
95*5971e316Smrg isl_point_free(pnt);
96*5971e316Smrg return v;
97*5971e316Smrg error:
98*5971e316Smrg FN(PW,free)(pw);
99*5971e316Smrg isl_point_free(pnt);
100*5971e316Smrg return NULL;
101*5971e316Smrg }
102