7560021d1e364dd49e6d0ecacd5cf6623cd3082b
[m6w6/ext-psi] / src / calc / bool.h
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #include "php_psi_stdinc.h"
27 #include <assert.h>
28
29 #include "token.h"
30 static inline token_t psi_calc_bool_or(token_t t1, impl_val *v1, token_t t2, impl_val *v2, impl_val *res)
31 {
32 switch (t1) {
33 case PSI_T_INT8:
34 if (v1->i8)
35 goto return_true;
36 break;
37 case PSI_T_UINT8:
38 if (v1->u8)
39 goto return_true;
40 break;
41 case PSI_T_INT16:
42 if (v1->i16)
43 goto return_true;
44 break;
45 case PSI_T_UINT16:
46 if (v1->u16)
47 goto return_true;
48 break;
49 case PSI_T_INT32:
50 if (v1->i32)
51 goto return_true;
52 break;
53 case PSI_T_UINT32:
54 if (v1->u32)
55 goto return_true;
56 break;
57 case PSI_T_INT64:
58 if (v1->i64)
59 goto return_true;
60 break;
61 case PSI_T_UINT64:
62 if (v1->u64)
63 goto return_true;
64 break;
65 #if HAVE_INT128
66 case PSI_T_INT128:
67 if (v1->i128)
68 goto return_true;
69 break;
70 #endif
71
72 #if HAVE_UINT128
73 case PSI_T_UINT128:
74 if (v1->u128)
75 goto return_true;
76 break;
77 #endif
78
79 case PSI_T_FLOAT:
80 if (v1->fval)
81 goto return_true;
82 break;
83 case PSI_T_DOUBLE:
84 if (v1->dval)
85 goto return_true;
86 break;
87 #if HAVE_LONG_DOUBLE
88 case PSI_T_LONG_DOUBLE:
89 if (v1->ldval)
90 goto return_true;
91 break;
92 #endif
93
94 default:
95 assert(0);
96 break;
97 }
98
99 switch (t2) {
100 case PSI_T_INT8:
101 if (v2->i8)
102 goto return_true;
103 break;
104 case PSI_T_UINT8:
105 if (v2->u8)
106 goto return_true;
107 break;
108 case PSI_T_INT16:
109 if (v2->i16)
110 goto return_true;
111 break;
112 case PSI_T_UINT16:
113 if (v2->u16)
114 goto return_true;
115 break;
116 case PSI_T_INT32:
117 if (v2->i32)
118 goto return_true;
119 break;
120 case PSI_T_UINT32:
121 if (v2->u32)
122 goto return_true;
123 break;
124 case PSI_T_INT64:
125 if (v2->i64)
126 goto return_true;
127 break;
128 case PSI_T_UINT64:
129 if (v2->u64)
130 goto return_true;
131 break;
132 #if HAVE_INT128
133 case PSI_T_INT128:
134 if (v2->i128)
135 goto return_true;
136 break;
137 #endif
138
139 #if HAVE_UINT128
140 case PSI_T_UINT128:
141 if (v2->u128)
142 goto return_true;
143 break;
144 #endif
145
146 case PSI_T_FLOAT:
147 if (v2->fval)
148 goto return_true;
149 break;
150 case PSI_T_DOUBLE:
151 if (v2->dval)
152 goto return_true;
153 break;
154 #if HAVE_LONG_DOUBLE
155 case PSI_T_LONG_DOUBLE:
156 if (v2->ldval)
157 goto return_true;
158 break;
159 #endif
160
161 default:
162 assert(0);
163 break;
164 }
165
166 res->u8 = 0;
167 return PSI_T_UINT8;
168
169 return_true:
170 res->u8 = 1;
171 return PSI_T_UINT8;
172 }
173
174 static inline token_t psi_calc_bool_and(token_t t1, impl_val *v1, token_t t2, impl_val *v2, impl_val *res)
175 {
176 switch (t1) {
177 case PSI_T_INT8:
178 if (!v1->i8)
179 goto return_false;
180 break;
181 case PSI_T_UINT8:
182 if (!v1->u8)
183 goto return_false;
184 break;
185 case PSI_T_INT16:
186 if (!v1->i16)
187 goto return_false;
188 break;
189 case PSI_T_UINT16:
190 if (!v1->u16)
191 goto return_false;
192 break;
193 case PSI_T_INT32:
194 if (!v1->i32)
195 goto return_false;
196 break;
197 case PSI_T_UINT32:
198 if (!v1->u32)
199 goto return_false;
200 break;
201 case PSI_T_INT64:
202 if (!v1->i64)
203 goto return_false;
204 break;
205 case PSI_T_UINT64:
206 if (!v1->u64)
207 goto return_false;
208 break;
209 #if HAVE_INT128
210 case PSI_T_INT128:
211 if (!v1->i128)
212 goto return_false;
213 break;
214 #endif
215
216 #if HAVE_UINT128
217 case PSI_T_UINT128:
218 if (!v1->u128)
219 goto return_false;
220 break;
221 #endif
222
223 case PSI_T_FLOAT:
224 if (!v1->fval)
225 goto return_false;
226 break;
227 case PSI_T_DOUBLE:
228 if (!v1->dval)
229 goto return_false;
230 break;
231 #if HAVE_LONG_DOUBLE
232 case PSI_T_LONG_DOUBLE:
233 if (!v1->ldval)
234 goto return_false;
235 break;
236 #endif
237
238 default:
239 assert(0);
240 break;
241 }
242
243 switch (t2) {
244 case PSI_T_INT8:
245 if (!v2->i8)
246 goto return_false;
247 break;
248 case PSI_T_UINT8:
249 if (!v2->u8)
250 goto return_false;
251 break;
252 case PSI_T_INT16:
253 if (!v2->i16)
254 goto return_false;
255 break;
256 case PSI_T_UINT16:
257 if (!v2->u16)
258 goto return_false;
259 break;
260 case PSI_T_INT32:
261 if (!v2->i32)
262 goto return_false;
263 break;
264 case PSI_T_UINT32:
265 if (!v2->u32)
266 goto return_false;
267 break;
268 case PSI_T_INT64:
269 if (!v2->i64)
270 goto return_false;
271 break;
272 case PSI_T_UINT64:
273 if (!v2->u64)
274 goto return_false;
275 break;
276 #if HAVE_INT128
277 case PSI_T_INT128:
278 if (!v2->i128)
279 goto return_false;
280 break;
281 #endif
282
283 #if HAVE_UINT128
284 case PSI_T_UINT128:
285 if (!v2->u128)
286 goto return_false;
287 break;
288 #endif
289
290 case PSI_T_FLOAT:
291 if (!v2->fval)
292 goto return_false;
293 break;
294 case PSI_T_DOUBLE:
295 if (!v2->dval)
296 goto return_false;
297 break;
298 #if HAVE_LONG_DOUBLE
299 case PSI_T_LONG_DOUBLE:
300 if (!v2->ldval)
301 goto return_false;
302 break;
303 #endif
304
305 default:
306 assert(0);
307 break;
308 }
309
310 res->u8 = 1;
311 return PSI_T_UINT8;
312
313 return_false:
314 res->u8 = 0;
315 return PSI_T_UINT8;
316 }