isl_basic_map_add_div_constraints: use isl_basic_map_var_offset
[isl.git] / isl_pw_eval.c
blobf59b78726a81d41f215de1199ecb464f137581cd
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 /* Evaluate the piecewise function "pw" in "pnt".
34 * If the point is void, then return NaN.
35 * If the point lies outside the domain of "pw", then return 0 or NaN
36 * depending on whether 0 is the default value for this type of function.
38 __isl_give isl_val *FN(PW,eval)(__isl_take PW *pw, __isl_take isl_point *pnt)
40 int i;
41 isl_bool is_void;
42 isl_bool found;
43 isl_ctx *ctx;
44 isl_bool ok;
45 isl_space *pnt_space, *pw_space;
46 isl_val *v;
48 pnt_space = isl_point_peek_space(pnt);
49 pw_space = FN(PW,peek_space)(pw);
50 ok = isl_space_is_domain_internal(pnt_space, pw_space);
51 if (ok < 0)
52 goto error;
53 ctx = isl_point_get_ctx(pnt);
54 if (!ok)
55 isl_die(ctx, isl_error_invalid,
56 "incompatible spaces", goto error);
57 is_void = isl_point_is_void(pnt);
58 if (is_void < 0)
59 goto error;
60 if (is_void)
61 return FN(PW,eval_void)(pw, pnt);
63 found = isl_bool_false;
64 for (i = 0; i < pw->n; ++i) {
65 found = isl_set_contains_point(pw->p[i].set, pnt);
66 if (found < 0)
67 goto error;
68 if (found)
69 break;
71 if (found) {
72 v = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
73 isl_point_copy(pnt));
74 } else if (DEFAULT_IS_ZERO) {
75 v = isl_val_zero(ctx);
76 } else {
77 v = isl_val_nan(ctx);
79 FN(PW,free)(pw);
80 isl_point_free(pnt);
81 return v;
82 error:
83 FN(PW,free)(pw);
84 isl_point_free(pnt);
85 return NULL;