99f5f0a352315fce0885aacfa3c34983816ab0d4
[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
31 static inline token_t psi_calc_bool_not(token_t t1, impl_val *v1, token_t t2, impl_val *v2, impl_val *res)
32 {
33 (void) t2;
34 (void) v2;
35
36 switch (t1) {
37 case PSI_T_INT8:
38 res->u8 = !v1->i8;
39 break;
40 case PSI_T_UINT8:
41 res->u8 = !v1->u8;
42 break;
43 case PSI_T_INT16:
44 res->u8 = !v1->i16;
45 break;
46 case PSI_T_UINT16:
47 res->u8 = !v1->u16;
48 break;
49 case PSI_T_INT32:
50 res->u8 = !v1->i32;
51 break;
52 case PSI_T_UINT32:
53 res->u8 = !v1->u32;
54 break;
55 case PSI_T_INT64:
56 res->u8 = !v1->i64;
57 break;
58 case PSI_T_UINT64:
59 res->u8 = !v1->u64;
60 break;
61 case PSI_T_FLOAT:
62 res->u8 = !v1->fval;
63 break;
64 case PSI_T_DOUBLE:
65 res->u8 = !v1->dval;
66 break;
67 #if HAVE_LONG_DOUBLE
68 case PSI_T_LONG_DOUBLE:
69 res->u8 = !v1->ldval;
70 break;
71 #endif
72
73 default:
74 assert(0);
75 break;
76 }
77 return PSI_T_UINT8;
78 }
79
80 static inline token_t psi_calc_bool_or(token_t t1, impl_val *v1, token_t t2, impl_val *v2, impl_val *res)
81 {
82 switch (t1) {
83 case PSI_T_INT8:
84 if (v1->i8)
85 goto return_true;
86 break;
87 case PSI_T_UINT8:
88 if (v1->u8)
89 goto return_true;
90 break;
91 case PSI_T_INT16:
92 if (v1->i16)
93 goto return_true;
94 break;
95 case PSI_T_UINT16:
96 if (v1->u16)
97 goto return_true;
98 break;
99 case PSI_T_INT32:
100 if (v1->i32)
101 goto return_true;
102 break;
103 case PSI_T_UINT32:
104 if (v1->u32)
105 goto return_true;
106 break;
107 case PSI_T_INT64:
108 if (v1->i64)
109 goto return_true;
110 break;
111 case PSI_T_UINT64:
112 if (v1->u64)
113 goto return_true;
114 break;
115 case PSI_T_FLOAT:
116 if (v1->fval)
117 goto return_true;
118 break;
119 case PSI_T_DOUBLE:
120 if (v1->dval)
121 goto return_true;
122 break;
123 #if HAVE_LONG_DOUBLE
124 case PSI_T_LONG_DOUBLE:
125 if (v1->ldval)
126 goto return_true;
127 break;
128 #endif
129
130 default:
131 assert(0);
132 break;
133 }
134
135 switch (t2) {
136 case PSI_T_INT8:
137 if (v2->i8)
138 goto return_true;
139 break;
140 case PSI_T_UINT8:
141 if (v2->u8)
142 goto return_true;
143 break;
144 case PSI_T_INT16:
145 if (v2->i16)
146 goto return_true;
147 break;
148 case PSI_T_UINT16:
149 if (v2->u16)
150 goto return_true;
151 break;
152 case PSI_T_INT32:
153 if (v2->i32)
154 goto return_true;
155 break;
156 case PSI_T_UINT32:
157 if (v2->u32)
158 goto return_true;
159 break;
160 case PSI_T_INT64:
161 if (v2->i64)
162 goto return_true;
163 break;
164 case PSI_T_UINT64:
165 if (v2->u64)
166 goto return_true;
167 break;
168 case PSI_T_FLOAT:
169 if (v2->fval)
170 goto return_true;
171 break;
172 case PSI_T_DOUBLE:
173 if (v2->dval)
174 goto return_true;
175 break;
176 #if HAVE_LONG_DOUBLE
177 case PSI_T_LONG_DOUBLE:
178 if (v2->ldval)
179 goto return_true;
180 break;
181 #endif
182
183 default:
184 assert(0);
185 break;
186 }
187
188 res->u8 = 0;
189 return PSI_T_UINT8;
190
191 return_true:
192 res->u8 = 1;
193 return PSI_T_UINT8;
194 }
195
196 static inline token_t psi_calc_bool_and(token_t t1, impl_val *v1, token_t t2, impl_val *v2, impl_val *res)
197 {
198 switch (t1) {
199 case PSI_T_INT8:
200 if (!v1->i8)
201 goto return_false;
202 break;
203 case PSI_T_UINT8:
204 if (!v1->u8)
205 goto return_false;
206 break;
207 case PSI_T_INT16:
208 if (!v1->i16)
209 goto return_false;
210 break;
211 case PSI_T_UINT16:
212 if (!v1->u16)
213 goto return_false;
214 break;
215 case PSI_T_INT32:
216 if (!v1->i32)
217 goto return_false;
218 break;
219 case PSI_T_UINT32:
220 if (!v1->u32)
221 goto return_false;
222 break;
223 case PSI_T_INT64:
224 if (!v1->i64)
225 goto return_false;
226 break;
227 case PSI_T_UINT64:
228 if (!v1->u64)
229 goto return_false;
230 break;
231 case PSI_T_FLOAT:
232 if (!v1->fval)
233 goto return_false;
234 break;
235 case PSI_T_DOUBLE:
236 if (!v1->dval)
237 goto return_false;
238 break;
239 #if HAVE_LONG_DOUBLE
240 case PSI_T_LONG_DOUBLE:
241 if (!v1->ldval)
242 goto return_false;
243 break;
244 #endif
245
246 default:
247 assert(0);
248 break;
249 }
250
251 switch (t2) {
252 case PSI_T_INT8:
253 if (!v2->i8)
254 goto return_false;
255 break;
256 case PSI_T_UINT8:
257 if (!v2->u8)
258 goto return_false;
259 break;
260 case PSI_T_INT16:
261 if (!v2->i16)
262 goto return_false;
263 break;
264 case PSI_T_UINT16:
265 if (!v2->u16)
266 goto return_false;
267 break;
268 case PSI_T_INT32:
269 if (!v2->i32)
270 goto return_false;
271 break;
272 case PSI_T_UINT32:
273 if (!v2->u32)
274 goto return_false;
275 break;
276 case PSI_T_INT64:
277 if (!v2->i64)
278 goto return_false;
279 break;
280 case PSI_T_UINT64:
281 if (!v2->u64)
282 goto return_false;
283 break;
284 case PSI_T_FLOAT:
285 if (!v2->fval)
286 goto return_false;
287 break;
288 case PSI_T_DOUBLE:
289 if (!v2->dval)
290 goto return_false;
291 break;
292 #if HAVE_LONG_DOUBLE
293 case PSI_T_LONG_DOUBLE:
294 if (!v2->ldval)
295 goto return_false;
296 break;
297 #endif
298
299 default:
300 assert(0);
301 break;
302 }
303
304 res->u8 = 1;
305 return PSI_T_UINT8;
306
307 return_false:
308 res->u8 = 0;
309 return PSI_T_UINT8;
310 }