72a82e1f2ac80e64c0b51da947127cb62fb2d604
[m6w6/ext-psi] / src / calc / unary.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_minus(token_t t1, impl_val *v1, token_t t2, impl_val *v2, impl_val *res)
31 {
32 (void) t2;
33 (void) v2;
34
35 switch (t1) {
36 case PSI_T_INT8:
37 res->i8 = -v1->i8;
38 break;
39 case PSI_T_UINT8:
40 res->u8 = -v1->u8;
41 break;
42 case PSI_T_INT16:
43 res->i16 = -v1->i16;
44 break;
45 case PSI_T_UINT16:
46 res->u16 = -v1->u16;
47 break;
48 case PSI_T_INT32:
49 res->i32 = -v1->i32;
50 break;
51 case PSI_T_UINT32:
52 res->u32 = -v1->u32;
53 break;
54 case PSI_T_INT64:
55 res->i64 = -v1->i64;
56 break;
57 case PSI_T_UINT64:
58 res->u64 = -v1->u64;
59 break;
60 case PSI_T_INT128:
61 res->i128 = -v1->i128;
62 break;
63 case PSI_T_UINT128:
64 res->u128 = -v1->u128;
65 break;
66 case PSI_T_FLOAT:
67 res->fval = -v1->fval;
68 break;
69 case PSI_T_DOUBLE:
70 res->dval = -v1->dval;
71 break;
72 #if HAVE_LONG_DOUBLE
73 case PSI_T_LONG_DOUBLE:
74 res->ldval = -v1->ldval;
75 break;
76 #endif
77
78 default:
79 assert(0);
80 break;
81 }
82 return t1;
83 }
84
85
86 static inline token_t psi_calc_bool_not(token_t t1, impl_val *v1, token_t t2, impl_val *v2, impl_val *res)
87 {
88 (void) t2;
89 (void) v2;
90
91 switch (t1) {
92 case PSI_T_INT8:
93 res->u8 = !v1->i8;
94 break;
95 case PSI_T_UINT8:
96 res->u8 = !v1->u8;
97 break;
98 case PSI_T_INT16:
99 res->u8 = !v1->i16;
100 break;
101 case PSI_T_UINT16:
102 res->u8 = !v1->u16;
103 break;
104 case PSI_T_INT32:
105 res->u8 = !v1->i32;
106 break;
107 case PSI_T_UINT32:
108 res->u8 = !v1->u32;
109 break;
110 case PSI_T_INT64:
111 res->u8 = !v1->i64;
112 break;
113 case PSI_T_UINT64:
114 res->u8 = !v1->u64;
115 break;
116 case PSI_T_INT128:
117 res->u8 = !v1->i128;
118 break;
119 case PSI_T_UINT128:
120 res->u8 = !v1->u128;
121 break;
122 case PSI_T_FLOAT:
123 res->u8 = !v1->fval;
124 break;
125 case PSI_T_DOUBLE:
126 res->u8 = !v1->dval;
127 break;
128 #if HAVE_LONG_DOUBLE
129 case PSI_T_LONG_DOUBLE:
130 res->u8 = !v1->ldval;
131 break;
132 #endif
133
134 default:
135 assert(0);
136 break;
137 }
138 return PSI_T_UINT8;
139 }
140
141
142 static inline token_t psi_calc_bin_not(token_t t1, impl_val *v1, token_t t2, impl_val *v2, impl_val *res)
143 {
144 impl_val i1;
145
146 (void) t2;
147 (void) v2;
148
149 switch (t1) {
150 case PSI_T_INT8:
151 i1.u64 = v1->i8;
152 break;
153
154 case PSI_T_UINT8:
155 i1.u64 = v1->u8;
156 break;
157
158 case PSI_T_INT16:
159 i1.u64 = v1->i16;
160 break;
161
162 case PSI_T_UINT16:
163 i1.u64 = v1->u16;
164 break;
165
166 case PSI_T_INT32:
167 i1.u64 = v1->i32;
168 break;
169
170 case PSI_T_UINT32:
171 i1.u64 = v1->u32;
172 break;
173
174 case PSI_T_INT64:
175 i1.u64 = v1->i64;
176 break;
177
178 case PSI_T_UINT64:
179 i1.u64 = v1->u64;
180 break;
181
182 case PSI_T_INT128:
183 i1.u64 = v1->i128;
184 break;
185
186 case PSI_T_UINT128:
187 i1.u64 = v1->u128;
188 break;
189
190 case PSI_T_FLOAT:
191 i1.u64 = v1->fval;
192 break;
193
194 case PSI_T_DOUBLE:
195 i1.u64 = v1->dval;
196 break;
197
198 #if HAVE_LONG_DOUBLE
199 case PSI_T_LONG_DOUBLE:
200 i1.u64 = v1->ldval;
201 break;
202
203 #endif
204
205 default:
206 assert(0);
207 break;
208 }
209
210 res->u64 = ~i1.u64;
211 return PSI_T_UINT64;
212 }