isl_pw_templ.c: extract out isl_pw_eval.c
[isl.git] / isl_pw_eval.c
blobd5d81a19b6aa6bc20ac5840975931adb26b53803
1 /*
2 * Copyright 2010 INRIA Saclay
3 * Copyright 2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl/val.h>
14 #include <isl_space_private.h>
15 #include <isl_point_private.h>
17 #include <isl_pw_macro.h>
19 /* Evaluate "pw" in the void point "pnt".
20 * In particular, return the value NaN.
22 static __isl_give isl_val *FN(PW,eval_void)(__isl_take PW *pw,
23 __isl_take isl_point *pnt)
25 isl_ctx *ctx;
27 ctx = isl_point_get_ctx(pnt);
28 FN(PW,free)(pw);
29 isl_point_free(pnt);
30 return isl_val_nan(ctx);
33 __isl_give isl_val *FN(PW,eval)(__isl_take PW *pw, __isl_take isl_point *pnt)
35 int i;
36 isl_bool is_void;
37 isl_bool found;
38 isl_ctx *ctx;
39 isl_bool ok;
40 isl_space *pnt_space, *pw_space;
41 isl_val *v;
43 pnt_space = isl_point_peek_space(pnt);
44 pw_space = FN(PW,peek_space)(pw);
45 ok = isl_space_is_domain_internal(pnt_space, pw_space);
46 if (ok < 0)
47 goto error;
48 ctx = isl_point_get_ctx(pnt);
49 if (!ok)
50 isl_die(ctx, isl_error_invalid,
51 "incompatible spaces", goto error);
52 is_void = isl_point_is_void(pnt);
53 if (is_void < 0)
54 goto error;
55 if (is_void)
56 return FN(PW,eval_void)(pw, pnt);
58 found = isl_bool_false;
59 for (i = 0; i < pw->n; ++i) {
60 found = isl_set_contains_point(pw->p[i].set, pnt);
61 if (found < 0)
62 goto error;
63 if (found)
64 break;
66 if (found)
67 v = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
68 isl_point_copy(pnt));
69 else
70 v = isl_val_zero(ctx);
71 FN(PW,free)(pw);
72 isl_point_free(pnt);
73 return v;
74 error:
75 FN(PW,free)(pw);
76 isl_point_free(pnt);
77 return NULL;