7 #define FOLD_STRING_UNTRANSLATE_HTSIZE 1024
8 #define FOLD_STRING_DOTRANSLATE_HTSIZE 1024
10 /* The options to use for inexact and arithmetic exceptions */
11 #define FOLD_ROUNDING SFLOAT_ROUND_NEAREST_EVEN
12 #define FOLD_TINYNESS SFLOAT_TBEFORE
15 * Comparing float values is an unsafe operation when the operands to the
16 * comparison are floating point values that are inexact. For instance 1/3 is an
17 * inexact value. The FPU is meant to raise exceptions when these sorts of things
18 * happen, including division by zero, underflows and overflows. The C standard
19 * library provides us with the <fenv.h> header to gain access to the floating-
20 * point environment and lets us set the rounding mode and check for these exceptions.
21 * The problem is the standard C library allows an implementation to leave these
22 * stubbed out and does not require they be implemented. Furthermore, depending
23 * on implementations there is no control over the FPU. This is an IEE 754
24 * conforming implementation in software to compensate.
26 typedef uint32_t sfloat_t;
39 SFLOAT_UNDERFLOW = 16,
41 } sfloat_exceptionflags_t;
45 SFLOAT_ROUND_NEAREST_EVEN,
49 } sfloat_roundingmode_t;
51 /* Underflow tininess-detection mode */
58 sfloat_roundingmode_t roundingmode;
59 sfloat_exceptionflags_t exceptionflags;
60 sfloat_tdetect_t tiny;
63 /* Counts the number of leading zero bits before the most-significand one bit. */
65 /* MSVC has an intrinsic for this */
66 static GMQCC_INLINE uint32_t sfloat_clz(uint32_t x) {
68 _BitScanForward(&r, x);
71 # define SFLOAT_CLZ(X, SUB) \
72 (sfloat_clz((X)) - (SUB))
73 #elif defined(__GNUC__) || defined(__CLANG__)
74 /* Clang and GCC have a builtin for this */
75 # define SFLOAT_CLZ(X, SUB) \
76 (__builtin_clz((X)) - (SUB))
79 static GMQCC_INLINE uint32_t sfloat_popcnt(uint32_t x) {
80 x -= ((x >> 1) & 0x55555555);
81 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
82 x = (((x >> 4) + x) & 0x0F0F0F0F);
85 return x & 0x0000003F;
87 static GMQCC_INLINE uint32_t sfloat_clz(uint32_t x) {
93 return 32 - sfloat_popcnt(x);
95 # define SFLOAT_CLZ(X, SUB) \
96 (sfloat_clz((X) - (SUB)))
99 /* The value of a NaN */
100 #define SFLOAT_NAN 0xFFFFFFFF
102 #define SFLOAT_ISNAN(A) \
103 (0xFF000000 < (uint32_t)((A) << 1))
104 /* Test if signaling NaN */
105 #define SFLOAT_ISSNAN(A) \
106 (((((A) >> 22) & 0x1FF) == 0x1FE) && ((A) & 0x003FFFFF))
107 /* Raise exception */
108 #define SFLOAT_RAISE(STATE, FLAGS) \
109 ((STATE)->exceptionflags = (sfloat_exceptionflags_t)((STATE)->exceptionflags | (FLAGS)))
111 * Shifts `A' right by the number of bits given in `COUNT'. If any non-zero bits
112 * are shifted off they are forced into the least significand bit of the result
113 * by setting it to one. As a result of this, the value of `COUNT' can be
114 * arbitrarily large; if `COUNT' is greater than 32, the result will be either
115 * zero or one, depending on whether `A' is a zero or non-zero. The result is
116 * stored into the value pointed by `Z'.
118 #define SFLOAT_SHIFT(SIZE, A, COUNT, Z) \
119 *(Z) = ((COUNT) == 0) \
121 : (((COUNT) < (SIZE)) \
122 ? ((A) >> (COUNT)) | (((A) << ((-(COUNT)) & ((SIZE) - 1))) != 0) \
125 /* Extract fractional component */
126 #define SFLOAT_EXTRACT_FRAC(X) \
127 ((uint32_t)((X) & 0x007FFFFF))
128 /* Extract exponent component */
129 #define SFLOAT_EXTRACT_EXP(X) \
130 ((int16_t)((X) >> 23) & 0xFF)
131 /* Extract sign bit */
132 #define SFLOAT_EXTRACT_SIGN(X) \
135 * Normalizes the subnormal value represented by the denormalized significand
136 * `SA'. The normalized exponent and significand are stored at the locations
137 * pointed by `Z' and `SZ' respectively.
139 #define SFLOAT_SUBNORMALIZE(SA, Z, SZ) \
140 (void)(*(SZ) = (SA) << SFLOAT_CLZ((SA), 8), *(Z) = 1 - SFLOAT_CLZ((SA), 8))
142 * Packs the sign `SIGN', exponent `EXP' and significand `SIG' into the value
145 * After the shifting into their proper positions, the fields are added together
146 * to form the result. This means any integer portion of `SIG' will be added
147 * to the exponent. Similarly, because a properly normalized significand will
148 * always have an integer portion equal to one, the exponent input `EXP' should
149 * be one less than the desired result exponent whenever the significant input
150 * `SIG' is a complete, normalized significand.
152 #define SFLOAT_PACK(SIGN, EXP, SIG) \
153 (sfloat_t)((((uint32_t)(SIGN)) << 31) + (((uint32_t)(EXP)) << 23) + (SIG))
156 * Takes two values `a' and `b', one of which is a NaN, and returns the appropriate
157 * NaN result. If either `a' or `b' is a signaling NaN than an invalid exception is
160 static sfloat_t sfloat_propagate_nan(sfloat_state_t *state, sfloat_t a, sfloat_t b) {
161 bool isnan_a = SFLOAT_ISNAN(a);
162 bool issnan_a = SFLOAT_ISSNAN(a);
163 bool isnan_b = SFLOAT_ISNAN(b);
164 bool issnan_b = SFLOAT_ISSNAN(b);
169 if (issnan_a | issnan_b)
170 SFLOAT_RAISE(state, SFLOAT_INVALID);
172 return (issnan_a & isnan_b) ? b : a;
177 * Takes an abstract value having sign `sign_z', exponent `exp_z', and significand
178 * `sig_z' and returns the appropriate value corresponding to the abstract input.
180 * The abstract value is simply rounded and packed into the format. If the abstract
181 * input cannot be represented exactly an inexact exception is raised. If the
182 * abstract input is too large, the overflow and inexact exceptions are both raised
183 * and an infinity or maximal finite value is returned. If the abstract value is
184 * too small, the value is rounded to a subnormal and the underflow and inexact
185 * exceptions are only raised if the value cannot be represented exactly with
188 * The input significand `sig_z' has it's binary point between bits 30 and 29,
189 * this is seven bits to the left of its usual location. The shifted significand
190 * must be normalized or smaller than this. If it's not normalized then the exponent
191 * `exp_z' must be zero; in that case, the result returned is a subnormal number
192 * which must not require rounding. In the more usual case where the significand
193 * is normalized, the exponent must be one less than the *true* exponent.
195 * The handling of underflow and overflow is otherwise in alignment with IEC/IEEE.
197 static sfloat_t SFLOAT_PACK_round(sfloat_state_t *state, bool sign_z, int16_t exp_z, uint32_t sig_z) {
198 sfloat_roundingmode_t mode = state->roundingmode;
199 bool even = !!(mode == SFLOAT_ROUND_NEAREST_EVEN);
200 unsigned char increment = 0x40;
201 unsigned char bits = sig_z & 0x7F;
204 if (mode == SFLOAT_ROUND_TO_ZERO)
209 if (mode == SFLOAT_ROUND_UP)
212 if (mode == SFLOAT_ROUND_DOWN)
218 if (0xFD <= (uint16_t)exp_z) {
219 if ((0xFD < exp_z) || ((exp_z == 0xFD) && ((int32_t)(sig_z + increment) < 0))) {
220 SFLOAT_RAISE(state, SFLOAT_OVERFLOW | SFLOAT_INEXACT);
221 return SFLOAT_PACK(sign_z, 0xFF, 0) - (increment == 0);
224 /* Check for underflow */
225 bool tiny = (state->tiny == SFLOAT_TBEFORE) || (exp_z < -1) || (sig_z + increment < 0x80000000);
226 SFLOAT_SHIFT(32, sig_z, -exp_z, &sig_z);
230 SFLOAT_RAISE(state, SFLOAT_UNDERFLOW);
234 SFLOAT_RAISE(state, SFLOAT_INEXACT);
235 sig_z = (sig_z + increment) >> 7;
236 sig_z &= ~(((bits ^ 0x40) == 0) & even);
239 return SFLOAT_PACK(sign_z, exp_z, sig_z);
243 * Takes an abstract value having sign `sign_z', exponent `exp_z' and significand
244 * `sig_z' and returns the appropriate value corresponding to the abstract input.
245 * This function is exactly like `PACK_round' except the significand does not have
248 * Bit 31 of the significand must be zero and the exponent must be one less than
249 * the *true* exponent.
251 static sfloat_t SFLOAT_PACK_normal(sfloat_state_t *state, bool sign_z, int16_t exp_z, uint32_t sig_z) {
252 unsigned char c = SFLOAT_CLZ(sig_z, 1);
253 return SFLOAT_PACK_round(state, sign_z, exp_z - c, sig_z << c);
257 * Returns the result of adding the absolute values of `a' and `b'. The sign
258 * `sign_z' is ignored if the result is a NaN.
260 static sfloat_t sfloat_add_impl(sfloat_state_t *state, sfloat_t a, sfloat_t b, bool sign_z) {
261 int16_t exp_a = SFLOAT_EXTRACT_EXP(a);
262 int16_t exp_b = SFLOAT_EXTRACT_EXP(b);
264 int16_t exp_d = exp_a - exp_b;
265 uint32_t sig_a = SFLOAT_EXTRACT_FRAC(a) << 6;
266 uint32_t sig_b = SFLOAT_EXTRACT_FRAC(b) << 6;
271 return sig_a ? sfloat_propagate_nan(state, a, b) : a;
276 SFLOAT_SHIFT(32, sig_b, exp_d, &sig_b);
278 } else if (exp_d < 0) {
280 return sig_b ? sfloat_propagate_nan(state, a, b) : SFLOAT_PACK(sign_z, 0xFF, 0);
285 SFLOAT_SHIFT(32, sig_a, -exp_d, &sig_a);
289 return (sig_a | sig_b) ? sfloat_propagate_nan(state, a, b) : a;
291 return SFLOAT_PACK(sign_z, 0, (sig_a + sig_b) >> 6);
292 sig_z = 0x40000000 + sig_a + sig_b;
297 sig_z = (sig_a + sig_b) << 1;
299 if ((int32_t)sig_z < 0) {
300 sig_z = sig_a + sig_b;
304 return SFLOAT_PACK_round(state, sign_z, exp_z, sig_z);
308 * Returns the result of subtracting the absolute values of `a' and `b'. If the
309 * sign `sign_z' is one, the difference is negated before being returned. The
310 * sign is ignored if the result is a NaN.
312 static sfloat_t sfloat_sub_impl(sfloat_state_t *state, sfloat_t a, sfloat_t b, bool sign_z) {
313 int16_t exp_a = SFLOAT_EXTRACT_EXP(a);
314 int16_t exp_b = SFLOAT_EXTRACT_EXP(b);
316 int16_t exp_d = exp_a - exp_b;
317 uint32_t sig_a = SFLOAT_EXTRACT_FRAC(a) << 7;
318 uint32_t sig_b = SFLOAT_EXTRACT_FRAC(b) << 7;
321 if (0 < exp_d) goto exp_greater_a;
322 if (exp_d < 0) goto exp_greater_b;
326 return sfloat_propagate_nan(state, a, b);
327 SFLOAT_RAISE(state, SFLOAT_INVALID);
334 if (sig_b < sig_a) goto greater_a;
335 if (sig_a < sig_b) goto greater_b;
337 return SFLOAT_PACK(state->roundingmode == SFLOAT_ROUND_DOWN, 0, 0);
341 return (sig_b) ? sfloat_propagate_nan(state, a, b) : SFLOAT_PACK(sign_z ^ 1, 0xFF, 0);
346 SFLOAT_SHIFT(32, sig_a, -exp_d, &sig_a);
349 sig_z = sig_b - sig_a;
356 return (sig_a) ? sfloat_propagate_nan(state, a, b) : a;
361 SFLOAT_SHIFT(32, sig_b, exp_d, &sig_b);
364 sig_z = sig_a - sig_b;
369 return SFLOAT_PACK_normal(state, sign_z, exp_z, sig_z);
372 static GMQCC_INLINE sfloat_t sfloat_add(sfloat_state_t *state, sfloat_t a, sfloat_t b) {
373 bool sign_a = SFLOAT_EXTRACT_SIGN(a);
374 bool sign_b = SFLOAT_EXTRACT_SIGN(b);
375 return (sign_a == sign_b) ? sfloat_add_impl(state, a, b, sign_a)
376 : sfloat_sub_impl(state, a, b, sign_a);
379 static GMQCC_INLINE sfloat_t sfloat_sub(sfloat_state_t *state, sfloat_t a, sfloat_t b) {
380 bool sign_a = SFLOAT_EXTRACT_SIGN(a);
381 bool sign_b = SFLOAT_EXTRACT_SIGN(b);
382 return (sign_a == sign_b) ? sfloat_sub_impl(state, a, b, sign_a)
383 : sfloat_add_impl(state, a, b, sign_a);
386 static sfloat_t sfloat_mul(sfloat_state_t *state, sfloat_t a, sfloat_t b) {
387 int16_t exp_a = SFLOAT_EXTRACT_EXP(a);
388 int16_t exp_b = SFLOAT_EXTRACT_EXP(b);
390 uint32_t sig_a = SFLOAT_EXTRACT_FRAC(a);
391 uint32_t sig_b = SFLOAT_EXTRACT_FRAC(b);
393 uint64_t sig_z64 = 0;
394 bool sign_a = SFLOAT_EXTRACT_SIGN(a);
395 bool sign_b = SFLOAT_EXTRACT_SIGN(b);
396 bool sign_z = sign_a ^ sign_b;
399 if (sig_a || ((exp_b == 0xFF) && sig_b))
400 return sfloat_propagate_nan(state, a, b);
401 if ((exp_b | sig_b) == 0) {
402 SFLOAT_RAISE(state, SFLOAT_INVALID);
405 return SFLOAT_PACK(sign_z, 0xFF, 0);
409 return sfloat_propagate_nan(state, a, b);
410 if ((exp_a | sig_a) == 0) {
411 SFLOAT_RAISE(state, SFLOAT_INVALID);
414 return SFLOAT_PACK(sign_z, 0xFF, 0);
418 return SFLOAT_PACK(sign_z, 0, 0);
419 SFLOAT_SUBNORMALIZE(sig_a, &exp_a, &sig_a);
423 return SFLOAT_PACK(sign_z, 0, 0);
424 SFLOAT_SUBNORMALIZE(sig_b, &exp_b, &sig_b);
426 exp_z = exp_a + exp_b - 0x7F;
427 sig_a = (sig_a | 0x00800000) << 7;
428 sig_b = (sig_b | 0x00800000) << 8;
429 SFLOAT_SHIFT(64, ((uint64_t)sig_a) * sig_b, 32, &sig_z64);
431 if (0 <= (int32_t)(sig_z << 1)) {
435 return SFLOAT_PACK_round(state, sign_z, exp_z, sig_z);
438 static sfloat_t sfloat_div(sfloat_state_t *state, sfloat_t a, sfloat_t b) {
439 int16_t exp_a = SFLOAT_EXTRACT_EXP(a);
440 int16_t exp_b = SFLOAT_EXTRACT_EXP(b);
442 uint32_t sig_a = SFLOAT_EXTRACT_FRAC(a);
443 uint32_t sig_b = SFLOAT_EXTRACT_FRAC(b);
445 bool sign_a = SFLOAT_EXTRACT_SIGN(a);
446 bool sign_b = SFLOAT_EXTRACT_SIGN(b);
447 bool sign_z = sign_a ^ sign_b;
451 return sfloat_propagate_nan(state, a, b);
454 return sfloat_propagate_nan(state, a, b);
455 SFLOAT_RAISE(state, SFLOAT_INVALID);
458 return SFLOAT_PACK(sign_z, 0xFF, 0);
461 return (sig_b) ? sfloat_propagate_nan(state, a, b) : SFLOAT_PACK(sign_z, 0, 0);
464 if ((exp_a | sig_a) == 0) {
465 SFLOAT_RAISE(state, SFLOAT_INVALID);
468 SFLOAT_RAISE(state, SFLOAT_DIVBYZERO);
469 return SFLOAT_PACK(sign_z, 0xFF, 0);
471 SFLOAT_SUBNORMALIZE(sig_b, &exp_b, &sig_b);
475 return SFLOAT_PACK(sign_z, 0, 0);
476 SFLOAT_SUBNORMALIZE(sig_a, &exp_a, &sig_a);
478 exp_z = exp_a - exp_b + 0x7D;
479 sig_a = (sig_a | 0x00800000) << 7;
480 sig_b = (sig_b | 0x00800000) << 8;
481 if (sig_b <= (sig_a + sig_a)) {
485 sig_z = (((uint64_t)sig_a) << 32) / sig_b;
486 if ((sig_z & 0x3F) == 0)
487 sig_z |= ((uint64_t)sig_b * sig_z != ((uint64_t)sig_a) << 32);
488 return SFLOAT_PACK_round(state, sign_z, exp_z, sig_z);
491 static sfloat_t sfloat_neg(sfloat_state_t *state, sfloat_t a) {
494 return sfloat_mul(state, a, neg.s);
497 static GMQCC_INLINE void sfloat_check(lex_ctx_t ctx, sfloat_state_t *state, const char *vec) {
498 /* Exception comes from vector component */
500 if (state->exceptionflags & SFLOAT_DIVBYZERO)
501 compile_error(ctx, "division by zero in `%s' component", vec);
502 if (state->exceptionflags & SFLOAT_INVALID)
503 compile_error(ctx, "undefined (inf) in `%s' component", vec);
504 if (state->exceptionflags & SFLOAT_OVERFLOW)
505 compile_error(ctx, "arithmetic overflow in `%s' component", vec);
506 if (state->exceptionflags & SFLOAT_UNDERFLOW)
507 compile_error(ctx, "arithmetic underflow in `%s' component", vec);
510 if (state->exceptionflags & SFLOAT_DIVBYZERO)
511 compile_error(ctx, "division by zero");
512 if (state->exceptionflags & SFLOAT_INVALID)
513 compile_error(ctx, "undefined (inf)");
514 if (state->exceptionflags & SFLOAT_OVERFLOW)
515 compile_error(ctx, "arithmetic overflow");
516 if (state->exceptionflags & SFLOAT_UNDERFLOW)
517 compile_error(ctx, "arithmetic underflow");
520 static GMQCC_INLINE void sfloat_init(sfloat_state_t *state) {
521 state->exceptionflags = SFLOAT_NOEXCEPT;
522 state->roundingmode = FOLD_ROUNDING;
523 state->tiny = FOLD_TINYNESS;
527 * There is two stages to constant folding in GMQCC: there is the parse
528 * stage constant folding, where, with the help of the AST, operator
529 * usages can be constant folded. Then there is the constant folding
530 * in the IR for things like eliding if statements, can occur.
532 * This file is thus, split into two parts.
535 #define isfloat(X) (((ast_expression*)(X))->vtype == TYPE_FLOAT)
536 #define isvector(X) (((ast_expression*)(X))->vtype == TYPE_VECTOR)
537 #define isstring(X) (((ast_expression*)(X))->vtype == TYPE_STRING)
538 #define isarray(X) (((ast_expression*)(X))->vtype == TYPE_ARRAY)
539 #define isfloats(X,Y) (isfloat (X) && isfloat (Y))
542 * Implementation of basic vector math for vec3_t, for trivial constant
545 * TODO: gcc/clang hinting for autovectorization
561 sfloat_state_t state[3];
564 static GMQCC_INLINE vec3_soft_t vec3_soft_convert(vec3_t vec) {
572 static GMQCC_INLINE bool vec3_soft_exception(vec3_soft_state_t *vstate, size_t index) {
573 sfloat_exceptionflags_t flags = vstate->state[index].exceptionflags;
574 if (flags & SFLOAT_DIVBYZERO) return true;
575 if (flags & SFLOAT_INVALID) return true;
576 if (flags & SFLOAT_OVERFLOW) return true;
577 if (flags & SFLOAT_UNDERFLOW) return true;
581 static GMQCC_INLINE void vec3_soft_eval(vec3_soft_state_t *state,
582 sfloat_t (*callback)(sfloat_state_t *, sfloat_t, sfloat_t),
586 vec3_soft_t sa = vec3_soft_convert(a);
587 vec3_soft_t sb = vec3_soft_convert(b);
588 callback(&state->state[0], sa.x.s, sb.x.s);
589 if (vec3_soft_exception(state, 0)) state->faults = (vec3_comp_t)(state->faults | VEC_COMP_X);
590 callback(&state->state[1], sa.y.s, sb.y.s);
591 if (vec3_soft_exception(state, 1)) state->faults = (vec3_comp_t)(state->faults | VEC_COMP_Y);
592 callback(&state->state[2], sa.z.s, sb.z.s);
593 if (vec3_soft_exception(state, 2)) state->faults = (vec3_comp_t)(state->faults | VEC_COMP_Z);
596 static GMQCC_INLINE void vec3_check_except(vec3_t a,
599 sfloat_t (*callback)(sfloat_state_t *, sfloat_t, sfloat_t))
601 vec3_soft_state_t state;
603 if (!OPTS_FLAG(ARITHMETIC_EXCEPTIONS))
606 sfloat_init(&state.state[0]);
607 sfloat_init(&state.state[1]);
608 sfloat_init(&state.state[2]);
610 vec3_soft_eval(&state, callback, a, b);
611 if (state.faults & VEC_COMP_X) sfloat_check(ctx, &state.state[0], "x");
612 if (state.faults & VEC_COMP_Y) sfloat_check(ctx, &state.state[1], "y");
613 if (state.faults & VEC_COMP_Z) sfloat_check(ctx, &state.state[2], "z");
616 static GMQCC_INLINE vec3_t vec3_add(lex_ctx_t ctx, vec3_t a, vec3_t b) {
618 vec3_check_except(a, b, ctx, &sfloat_add);
625 static GMQCC_INLINE vec3_t vec3_sub(lex_ctx_t ctx, vec3_t a, vec3_t b) {
627 vec3_check_except(a, b, ctx, &sfloat_sub);
634 static GMQCC_INLINE vec3_t vec3_neg(lex_ctx_t ctx, vec3_t a) {
639 if (!OPTS_FLAG(ARITHMETIC_EXCEPTIONS))
650 sfloat_neg(&s[0], v[0].s);
651 sfloat_neg(&s[1], v[1].s);
652 sfloat_neg(&s[2], v[2].s);
654 sfloat_check(ctx, &s[0], NULL);
655 sfloat_check(ctx, &s[1], NULL);
656 sfloat_check(ctx, &s[2], NULL);
665 static GMQCC_INLINE vec3_t vec3_or(vec3_t a, vec3_t b) {
667 out.x = (qcfloat_t)(((qcint_t)a.x) | ((qcint_t)b.x));
668 out.y = (qcfloat_t)(((qcint_t)a.y) | ((qcint_t)b.y));
669 out.z = (qcfloat_t)(((qcint_t)a.z) | ((qcint_t)b.z));
673 static GMQCC_INLINE vec3_t vec3_orvf(vec3_t a, qcfloat_t b) {
675 out.x = (qcfloat_t)(((qcint_t)a.x) | ((qcint_t)b));
676 out.y = (qcfloat_t)(((qcint_t)a.y) | ((qcint_t)b));
677 out.z = (qcfloat_t)(((qcint_t)a.z) | ((qcint_t)b));
681 static GMQCC_INLINE vec3_t vec3_and(vec3_t a, vec3_t b) {
683 out.x = (qcfloat_t)(((qcint_t)a.x) & ((qcint_t)b.x));
684 out.y = (qcfloat_t)(((qcint_t)a.y) & ((qcint_t)b.y));
685 out.z = (qcfloat_t)(((qcint_t)a.z) & ((qcint_t)b.z));
689 static GMQCC_INLINE vec3_t vec3_andvf(vec3_t a, qcfloat_t b) {
691 out.x = (qcfloat_t)(((qcint_t)a.x) & ((qcint_t)b));
692 out.y = (qcfloat_t)(((qcint_t)a.y) & ((qcint_t)b));
693 out.z = (qcfloat_t)(((qcint_t)a.z) & ((qcint_t)b));
697 static GMQCC_INLINE vec3_t vec3_xor(vec3_t a, vec3_t b) {
699 out.x = (qcfloat_t)(((qcint_t)a.x) ^ ((qcint_t)b.x));
700 out.y = (qcfloat_t)(((qcint_t)a.y) ^ ((qcint_t)b.y));
701 out.z = (qcfloat_t)(((qcint_t)a.z) ^ ((qcint_t)b.z));
705 static GMQCC_INLINE vec3_t vec3_xorvf(vec3_t a, qcfloat_t b) {
707 out.x = (qcfloat_t)(((qcint_t)a.x) ^ ((qcint_t)b));
708 out.y = (qcfloat_t)(((qcint_t)a.y) ^ ((qcint_t)b));
709 out.z = (qcfloat_t)(((qcint_t)a.z) ^ ((qcint_t)b));
713 static GMQCC_INLINE vec3_t vec3_not(vec3_t a) {
721 static GMQCC_INLINE qcfloat_t vec3_mulvv(lex_ctx_t ctx, vec3_t a, vec3_t b) {
727 if (!OPTS_FLAG(ARITHMETIC_EXCEPTIONS))
730 sa = vec3_soft_convert(a);
731 sb = vec3_soft_convert(b);
739 r[0] = sfloat_mul(&s[0], sa.x.s, sb.x.s);
740 r[1] = sfloat_mul(&s[1], sa.y.s, sb.y.s);
741 r[2] = sfloat_mul(&s[2], sa.z.s, sb.z.s);
742 r[3] = sfloat_add(&s[3], r[0], r[1]);
743 r[4] = sfloat_add(&s[4], r[3], r[2]);
745 sfloat_check(ctx, &s[0], NULL);
746 sfloat_check(ctx, &s[1], NULL);
747 sfloat_check(ctx, &s[2], NULL);
748 sfloat_check(ctx, &s[3], NULL);
749 sfloat_check(ctx, &s[4], NULL);
752 return (a.x * b.x + a.y * b.y + a.z * b.z);
755 static GMQCC_INLINE vec3_t vec3_mulvf(lex_ctx_t ctx, vec3_t a, qcfloat_t b) {
761 if (!OPTS_FLAG(ARITHMETIC_EXCEPTIONS))
764 sa = vec3_soft_convert(a);
770 sfloat_mul(&s[0], sa.x.s, sb.s);
771 sfloat_mul(&s[1], sa.y.s, sb.s);
772 sfloat_mul(&s[2], sa.z.s, sb.s);
774 sfloat_check(ctx, &s[0], "x");
775 sfloat_check(ctx, &s[1], "y");
776 sfloat_check(ctx, &s[2], "z");
785 static GMQCC_INLINE bool vec3_cmp(vec3_t a, vec3_t b) {
791 static GMQCC_INLINE vec3_t vec3_create(float x, float y, float z) {
799 static GMQCC_INLINE qcfloat_t vec3_notf(vec3_t a) {
800 return (!a.x && !a.y && !a.z);
803 static GMQCC_INLINE bool vec3_pbool(vec3_t a) {
804 return (a.x || a.y || a.z);
807 static GMQCC_INLINE vec3_t vec3_cross(lex_ctx_t ctx, vec3_t a, vec3_t b) {
814 if (!OPTS_FLAG(ARITHMETIC_EXCEPTIONS))
817 sa = vec3_soft_convert(a);
818 sb = vec3_soft_convert(b);
830 r[0] = sfloat_mul(&s[0], sa.y.s, sb.z.s);
831 r[1] = sfloat_mul(&s[1], sa.z.s, sb.y.s);
832 r[2] = sfloat_mul(&s[2], sa.z.s, sb.x.s);
833 r[3] = sfloat_mul(&s[3], sa.x.s, sb.z.s);
834 r[4] = sfloat_mul(&s[4], sa.x.s, sb.y.s);
835 r[5] = sfloat_mul(&s[5], sa.y.s, sb.x.s);
836 r[6] = sfloat_sub(&s[6], r[0], r[1]);
837 r[7] = sfloat_sub(&s[7], r[2], r[3]);
838 r[8] = sfloat_sub(&s[8], r[4], r[5]);
840 sfloat_check(ctx, &s[0], NULL);
841 sfloat_check(ctx, &s[1], NULL);
842 sfloat_check(ctx, &s[2], NULL);
843 sfloat_check(ctx, &s[3], NULL);
844 sfloat_check(ctx, &s[4], NULL);
845 sfloat_check(ctx, &s[5], NULL);
846 sfloat_check(ctx, &s[6], "x");
847 sfloat_check(ctx, &s[7], "y");
848 sfloat_check(ctx, &s[8], "z");
851 out.x = a.y * b.z - a.z * b.y;
852 out.y = a.z * b.x - a.x * b.z;
853 out.z = a.x * b.y - a.y * b.x;
857 static lex_ctx_t fold_ctx(fold_t *fold) {
859 if (fold->parser->lex)
860 return parser_ctx(fold->parser);
862 memset(&ctx, 0, sizeof(ctx));
866 static GMQCC_INLINE bool fold_immediate_true(fold_t *fold, ast_value *v) {
867 switch (v->expression.vtype) {
869 return !!v->constval.vfloat;
871 return !!v->constval.vint;
873 if (OPTS_FLAG(CORRECT_LOGIC))
874 return vec3_pbool(v->constval.vvec);
875 return !!(v->constval.vvec.x);
877 if (!v->constval.vstring)
879 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
881 return !!v->constval.vstring[0];
883 compile_error(fold_ctx(fold), "internal error: fold_immediate_true on invalid type");
886 return !!v->constval.vfunc;
889 /* Handy macros to determine if an ast_value can be constant folded. */
890 #define fold_can_1(X) \
891 (ast_istype(((ast_expression*)(X)), ast_value) && (X)->hasvalue && ((X)->cvq == CV_CONST) && \
892 ((ast_expression*)(X))->vtype != TYPE_FUNCTION)
894 #define fold_can_2(X, Y) (fold_can_1(X) && fold_can_1(Y))
896 #define fold_immvalue_float(E) ((E)->constval.vfloat)
897 #define fold_immvalue_vector(E) ((E)->constval.vvec)
898 #define fold_immvalue_string(E) ((E)->constval.vstring)
900 fold_t *fold_init(parser_t *parser) {
901 fold_t *fold = (fold_t*)mem_a(sizeof(fold_t));
902 fold->parser = parser;
903 fold->imm_float = NULL;
904 fold->imm_vector = NULL;
905 fold->imm_string = NULL;
906 fold->imm_string_untranslate = util_htnew(FOLD_STRING_UNTRANSLATE_HTSIZE);
907 fold->imm_string_dotranslate = util_htnew(FOLD_STRING_DOTRANSLATE_HTSIZE);
910 * prime the tables with common constant values at constant
913 (void)fold_constgen_float (fold, 0.0f, false);
914 (void)fold_constgen_float (fold, 1.0f, false);
915 (void)fold_constgen_float (fold, -1.0f, false);
916 (void)fold_constgen_float (fold, 2.0f, false);
918 (void)fold_constgen_vector(fold, vec3_create(0.0f, 0.0f, 0.0f));
919 (void)fold_constgen_vector(fold, vec3_create(-1.0f, -1.0f, -1.0f));
924 bool fold_generate(fold_t *fold, ir_builder *ir) {
925 /* generate globals for immediate folded values */
929 for (i = 0; i < vec_size(fold->imm_float); ++i)
930 if (!ast_global_codegen ((cur = fold->imm_float[i]), ir, false)) goto err;
931 for (i = 0; i < vec_size(fold->imm_vector); ++i)
932 if (!ast_global_codegen((cur = fold->imm_vector[i]), ir, false)) goto err;
933 for (i = 0; i < vec_size(fold->imm_string); ++i)
934 if (!ast_global_codegen((cur = fold->imm_string[i]), ir, false)) goto err;
939 con_out("failed to generate global %s\n", cur->name);
940 ir_builder_delete(ir);
944 void fold_cleanup(fold_t *fold) {
947 for (i = 0; i < vec_size(fold->imm_float); ++i) ast_delete(fold->imm_float[i]);
948 for (i = 0; i < vec_size(fold->imm_vector); ++i) ast_delete(fold->imm_vector[i]);
949 for (i = 0; i < vec_size(fold->imm_string); ++i) ast_delete(fold->imm_string[i]);
951 vec_free(fold->imm_float);
952 vec_free(fold->imm_vector);
953 vec_free(fold->imm_string);
955 util_htdel(fold->imm_string_untranslate);
956 util_htdel(fold->imm_string_dotranslate);
961 ast_expression *fold_constgen_float(fold_t *fold, qcfloat_t value, bool inexact) {
962 ast_value *out = NULL;
965 for (i = 0; i < vec_size(fold->imm_float); i++) {
966 if (!memcmp(&fold->imm_float[i]->constval.vfloat, &value, sizeof(qcfloat_t)))
967 return (ast_expression*)fold->imm_float[i];
970 out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_FLOAT);
972 out->hasvalue = true;
973 out->inexact = inexact;
974 out->constval.vfloat = value;
976 vec_push(fold->imm_float, out);
978 return (ast_expression*)out;
981 ast_expression *fold_constgen_vector(fold_t *fold, vec3_t value) {
985 for (i = 0; i < vec_size(fold->imm_vector); i++) {
986 if (vec3_cmp(fold->imm_vector[i]->constval.vvec, value))
987 return (ast_expression*)fold->imm_vector[i];
990 out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_VECTOR);
992 out->hasvalue = true;
993 out->constval.vvec = value;
995 vec_push(fold->imm_vector, out);
997 return (ast_expression*)out;
1000 ast_expression *fold_constgen_string(fold_t *fold, const char *str, bool translate) {
1001 hash_table_t *table = (translate) ? fold->imm_string_untranslate : fold->imm_string_dotranslate;
1002 ast_value *out = NULL;
1003 size_t hash = util_hthash(table, str);
1005 if ((out = (ast_value*)util_htgeth(table, str, hash)))
1006 return (ast_expression*)out;
1010 util_snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(fold->parser->translated++));
1011 out = ast_value_new(parser_ctx(fold->parser), name, TYPE_STRING);
1012 out->expression.flags |= AST_FLAG_INCLUDE_DEF; /* def needs to be included for translatables */
1014 out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_STRING);
1016 out->cvq = CV_CONST;
1017 out->hasvalue = true;
1019 out->constval.vstring = parser_strdup(str);
1021 vec_push(fold->imm_string, out);
1022 util_htseth(table, str, hash, out);
1024 return (ast_expression*)out;
1028 void (*callback)(void);
1029 sfloat_t (*binary)(sfloat_state_t *, sfloat_t, sfloat_t);
1030 sfloat_t (*unary)(sfloat_state_t *, sfloat_t);
1031 } float_check_callback_t;
1033 static bool fold_check_except_float_impl(void (*callback)(void),
1038 float_check_callback_t call;
1042 if (!OPTS_FLAG(ARITHMETIC_EXCEPTIONS) && !OPTS_WARN(WARN_INEXACT_COMPARES))
1045 call.callback = callback;
1047 ca.f = fold_immvalue_float(a);
1050 cb.f = fold_immvalue_float(b);
1051 call.binary(&s, ca.s, cb.s);
1053 call.unary(&s, ca.s);
1056 if (s.exceptionflags == 0)
1059 if (!OPTS_FLAG(ARITHMETIC_EXCEPTIONS))
1060 goto inexact_possible;
1062 sfloat_check(fold_ctx(fold), &s, NULL);
1065 return s.exceptionflags & SFLOAT_INEXACT;
1068 #define fold_check_except_float(CALLBACK, FOLD, A, B) \
1069 fold_check_except_float_impl(((void (*)(void))(CALLBACK)), (FOLD), (A), (B))
1071 static bool fold_check_inexact_float(fold_t *fold, ast_value *a, ast_value *b) {
1072 lex_ctx_t ctx = fold_ctx(fold);
1073 if (!OPTS_WARN(WARN_INEXACT_COMPARES))
1075 if (!a->inexact && !b->inexact)
1077 return compile_warning(ctx, WARN_INEXACT_COMPARES, "inexact value in comparison");
1080 static GMQCC_INLINE ast_expression *fold_op_mul_vec(fold_t *fold, vec3_t vec, ast_value *sel, const char *set) {
1081 qcfloat_t x = (&vec.x)[set[0]-'x'];
1082 qcfloat_t y = (&vec.x)[set[1]-'x'];
1083 qcfloat_t z = (&vec.x)[set[2]-'x'];
1085 ast_expression *out;
1086 ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
1087 out = (ast_expression*)ast_member_new(fold_ctx(fold), (ast_expression*)sel, set[0]-'x', NULL);
1088 out->node.keep = false;
1089 ((ast_member*)out)->rvalue = true;
1091 return (ast_expression*)ast_binary_new(fold_ctx(fold), INSTR_MUL_F, fold_constgen_float(fold, x, false), out);
1097 static GMQCC_INLINE ast_expression *fold_op_neg(fold_t *fold, ast_value *a) {
1099 if (fold_can_1(a)) {
1100 /* Negation can produce inexact as well */
1101 bool inexact = fold_check_except_float(&sfloat_neg, fold, a, NULL);
1102 return fold_constgen_float(fold, -fold_immvalue_float(a), inexact);
1104 } else if (isvector(a)) {
1106 return fold_constgen_vector(fold, vec3_neg(fold_ctx(fold), fold_immvalue_vector(a)));
1111 static GMQCC_INLINE ast_expression *fold_op_not(fold_t *fold, ast_value *a) {
1114 return fold_constgen_float(fold, !fold_immvalue_float(a), false);
1115 } else if (isvector(a)) {
1117 return fold_constgen_float(fold, vec3_notf(fold_immvalue_vector(a)), false);
1118 } else if (isstring(a)) {
1119 if (fold_can_1(a)) {
1120 if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
1121 return fold_constgen_float(fold, !fold_immvalue_string(a), false);
1123 return fold_constgen_float(fold, !fold_immvalue_string(a) || !*fold_immvalue_string(a), false);
1129 static GMQCC_INLINE ast_expression *fold_op_add(fold_t *fold, ast_value *a, ast_value *b) {
1131 if (fold_can_2(a, b)) {
1132 bool inexact = fold_check_except_float(&sfloat_add, fold, a, b);
1133 return fold_constgen_float(fold, fold_immvalue_float(a) + fold_immvalue_float(b), inexact);
1135 } else if (isvector(a)) {
1136 if (fold_can_2(a, b))
1137 return fold_constgen_vector(fold, vec3_add(fold_ctx(fold),
1138 fold_immvalue_vector(a),
1139 fold_immvalue_vector(b)));
1144 static GMQCC_INLINE ast_expression *fold_op_sub(fold_t *fold, ast_value *a, ast_value *b) {
1146 if (fold_can_2(a, b)) {
1147 bool inexact = fold_check_except_float(&sfloat_sub, fold, a, b);
1148 return fold_constgen_float(fold, fold_immvalue_float(a) - fold_immvalue_float(b), inexact);
1150 } else if (isvector(a)) {
1151 if (fold_can_2(a, b))
1152 return fold_constgen_vector(fold, vec3_sub(fold_ctx(fold),
1153 fold_immvalue_vector(a),
1154 fold_immvalue_vector(b)));
1159 static GMQCC_INLINE ast_expression *fold_op_mul(fold_t *fold, ast_value *a, ast_value *b) {
1162 if (fold_can_2(a, b))
1163 return fold_constgen_vector(fold, vec3_mulvf(fold_ctx(fold), fold_immvalue_vector(b), fold_immvalue_float(a)));
1165 if (fold_can_2(a, b)) {
1166 bool inexact = fold_check_except_float(&sfloat_mul, fold, a, b);
1167 return fold_constgen_float(fold, fold_immvalue_float(a) * fold_immvalue_float(b), inexact);
1170 } else if (isvector(a)) {
1172 if (fold_can_2(a, b))
1173 return fold_constgen_vector(fold, vec3_mulvf(fold_ctx(fold), fold_immvalue_vector(a), fold_immvalue_float(b)));
1175 if (fold_can_2(a, b)) {
1176 return fold_constgen_float(fold, vec3_mulvv(fold_ctx(fold), fold_immvalue_vector(a), fold_immvalue_vector(b)), false);
1177 } else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && fold_can_1(a)) {
1178 ast_expression *out;
1179 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(a), b, "xyz"))) return out;
1180 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(a), b, "yxz"))) return out;
1181 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(a), b, "zxy"))) return out;
1182 } else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && fold_can_1(b)) {
1183 ast_expression *out;
1184 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(b), a, "xyz"))) return out;
1185 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(b), a, "yxz"))) return out;
1186 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(b), a, "zxy"))) return out;
1193 static GMQCC_INLINE ast_expression *fold_op_div(fold_t *fold, ast_value *a, ast_value *b) {
1195 if (fold_can_2(a, b)) {
1196 bool inexact = fold_check_except_float(&sfloat_div, fold, a, b);
1197 return fold_constgen_float(fold, fold_immvalue_float(a) / fold_immvalue_float(b), inexact);
1198 } else if (fold_can_1(b)) {
1199 return (ast_expression*)ast_binary_new(
1203 fold_constgen_float(fold, 1.0f / fold_immvalue_float(b), false)
1206 } else if (isvector(a)) {
1207 if (fold_can_2(a, b)) {
1208 return fold_constgen_vector(fold, vec3_mulvf(fold_ctx(fold), fold_immvalue_vector(a), 1.0f / fold_immvalue_float(b)));
1210 return (ast_expression*)ast_binary_new(
1215 ? (ast_expression*)fold_constgen_float(fold, 1.0f / fold_immvalue_float(b), false)
1216 : (ast_expression*)ast_binary_new(
1219 (ast_expression*)fold->imm_float[1],
1228 static GMQCC_INLINE ast_expression *fold_op_mod(fold_t *fold, ast_value *a, ast_value *b) {
1229 return (fold_can_2(a, b))
1230 ? fold_constgen_float(fold, fmod(fold_immvalue_float(a), fold_immvalue_float(b)), false)
1234 static GMQCC_INLINE ast_expression *fold_op_bor(fold_t *fold, ast_value *a, ast_value *b) {
1236 if (fold_can_2(a, b))
1237 return fold_constgen_float(fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) | ((qcint_t)fold_immvalue_float(b))), false);
1240 if (fold_can_2(a, b))
1241 return fold_constgen_vector(fold, vec3_or(fold_immvalue_vector(a), fold_immvalue_vector(b)));
1243 if (fold_can_2(a, b))
1244 return fold_constgen_vector(fold, vec3_orvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
1250 static GMQCC_INLINE ast_expression *fold_op_band(fold_t *fold, ast_value *a, ast_value *b) {
1252 if (fold_can_2(a, b))
1253 return fold_constgen_float(fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) & ((qcint_t)fold_immvalue_float(b))), false);
1256 if (fold_can_2(a, b))
1257 return fold_constgen_vector(fold, vec3_and(fold_immvalue_vector(a), fold_immvalue_vector(b)));
1259 if (fold_can_2(a, b))
1260 return fold_constgen_vector(fold, vec3_andvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
1266 static GMQCC_INLINE ast_expression *fold_op_xor(fold_t *fold, ast_value *a, ast_value *b) {
1268 if (fold_can_2(a, b))
1269 return fold_constgen_float(fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) ^ ((qcint_t)fold_immvalue_float(b))), false);
1271 if (fold_can_2(a, b)) {
1273 return fold_constgen_vector(fold, vec3_xor(fold_immvalue_vector(a), fold_immvalue_vector(b)));
1275 return fold_constgen_vector(fold, vec3_xorvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
1281 static GMQCC_INLINE ast_expression *fold_op_lshift(fold_t *fold, ast_value *a, ast_value *b) {
1282 if (fold_can_2(a, b) && isfloats(a, b))
1283 return fold_constgen_float(fold, (qcfloat_t)floorf(fold_immvalue_float(a) * powf(2.0f, fold_immvalue_float(b))), false);
1287 static GMQCC_INLINE ast_expression *fold_op_rshift(fold_t *fold, ast_value *a, ast_value *b) {
1288 if (fold_can_2(a, b) && isfloats(a, b))
1289 return fold_constgen_float(fold, (qcfloat_t)floorf(fold_immvalue_float(a) / powf(2.0f, fold_immvalue_float(b))), false);
1293 static GMQCC_INLINE ast_expression *fold_op_andor(fold_t *fold, ast_value *a, ast_value *b, float expr) {
1294 if (fold_can_2(a, b)) {
1295 if (OPTS_FLAG(PERL_LOGIC)) {
1297 return (fold_immediate_true(fold, a)) ? (ast_expression*)a : (ast_expression*)b;
1299 return (fold_immediate_true(fold, a)) ? (ast_expression*)b : (ast_expression*)a;
1301 return fold_constgen_float (
1303 ((expr) ? (fold_immediate_true(fold, a) || fold_immediate_true(fold, b))
1304 : (fold_immediate_true(fold, a) && fold_immediate_true(fold, b)))
1314 static GMQCC_INLINE ast_expression *fold_op_tern(fold_t *fold, ast_value *a, ast_value *b, ast_value *c) {
1315 if (fold_can_1(a)) {
1316 return fold_immediate_true(fold, a)
1317 ? (ast_expression*)b
1318 : (ast_expression*)c;
1323 static GMQCC_INLINE ast_expression *fold_op_exp(fold_t *fold, ast_value *a, ast_value *b) {
1324 if (fold_can_2(a, b))
1325 return fold_constgen_float(fold, (qcfloat_t)powf(fold_immvalue_float(a), fold_immvalue_float(b)), false);
1329 static GMQCC_INLINE ast_expression *fold_op_lteqgt(fold_t *fold, ast_value *a, ast_value *b) {
1330 if (fold_can_2(a,b)) {
1331 fold_check_inexact_float(fold, a, b);
1332 if (fold_immvalue_float(a) < fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[2];
1333 if (fold_immvalue_float(a) == fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[0];
1334 if (fold_immvalue_float(a) > fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[1];
1339 static GMQCC_INLINE ast_expression *fold_op_ltgt(fold_t *fold, ast_value *a, ast_value *b, bool lt) {
1340 if (fold_can_2(a, b)) {
1341 fold_check_inexact_float(fold, a, b);
1342 return (lt) ? (ast_expression*)fold->imm_float[!!(fold_immvalue_float(a) < fold_immvalue_float(b))]
1343 : (ast_expression*)fold->imm_float[!!(fold_immvalue_float(a) > fold_immvalue_float(b))];
1348 static GMQCC_INLINE ast_expression *fold_op_cmp(fold_t *fold, ast_value *a, ast_value *b, bool ne) {
1349 if (fold_can_2(a, b)) {
1350 if (isfloat(a) && isfloat(b)) {
1351 float la = fold_immvalue_float(a);
1352 float lb = fold_immvalue_float(b);
1353 fold_check_inexact_float(fold, a, b);
1354 return (ast_expression*)fold->imm_float[!(ne ? la == lb : la != lb)];
1355 } if (isvector(a) && isvector(b)) {
1356 vec3_t la = fold_immvalue_vector(a);
1357 vec3_t lb = fold_immvalue_vector(b);
1358 return (ast_expression*)fold->imm_float[!(ne ? vec3_cmp(la, lb) : !vec3_cmp(la, lb))];
1364 static GMQCC_INLINE ast_expression *fold_op_bnot(fold_t *fold, ast_value *a) {
1367 return fold_constgen_float(fold, -1-fold_immvalue_float(a), false);
1371 return fold_constgen_vector(fold, vec3_not(fold_immvalue_vector(a)));
1377 static GMQCC_INLINE ast_expression *fold_op_cross(fold_t *fold, ast_value *a, ast_value *b) {
1378 if (fold_can_2(a, b))
1379 return fold_constgen_vector(fold, vec3_cross(fold_ctx(fold),
1380 fold_immvalue_vector(a),
1381 fold_immvalue_vector(b)));
1385 static GMQCC_INLINE ast_expression *fold_op_length(fold_t *fold, ast_value *a) {
1386 if (fold_can_1(a) && isstring(a))
1387 return fold_constgen_float(fold, strlen(fold_immvalue_string(a)), false);
1389 return fold_constgen_float(fold, vec_size(a->initlist), false);
1393 ast_expression *fold_op(fold_t *fold, const oper_info *info, ast_expression **opexprs) {
1394 ast_value *a = (ast_value*)opexprs[0];
1395 ast_value *b = (ast_value*)opexprs[1];
1396 ast_value *c = (ast_value*)opexprs[2];
1397 ast_expression *e = NULL;
1399 /* can a fold operation be applied to this operator usage? */
1403 switch(info->operands) {
1404 case 3: if(!c) return NULL;
1405 case 2: if(!b) return NULL;
1408 compile_error(fold_ctx(fold), "internal error: fold_op no operands to fold\n");
1414 * we could use a boolean and default case but ironically gcc produces
1415 * invalid broken assembly from that operation. clang/tcc get it right,
1416 * but interestingly ignore compiling this to a jump-table when I do that,
1417 * this happens to be the most efficent method, since you have per-level
1418 * granularity on the pointer check happening only for the case you check
1419 * it in. Opposed to the default method which would involve a boolean and
1420 * pointer check after wards.
1422 #define fold_op_case(ARGS, ARGS_OPID, OP, ARGS_FOLD) \
1423 case opid##ARGS ARGS_OPID: \
1424 if ((e = fold_op_##OP ARGS_FOLD)) { \
1425 ++opts_optimizationcount[OPTIM_CONST_FOLD]; \
1430 fold_op_case(2, ('-', 'P'), neg, (fold, a));
1431 fold_op_case(2, ('!', 'P'), not, (fold, a));
1432 fold_op_case(1, ('+'), add, (fold, a, b));
1433 fold_op_case(1, ('-'), sub, (fold, a, b));
1434 fold_op_case(1, ('*'), mul, (fold, a, b));
1435 fold_op_case(1, ('/'), div, (fold, a, b));
1436 fold_op_case(1, ('%'), mod, (fold, a, b));
1437 fold_op_case(1, ('|'), bor, (fold, a, b));
1438 fold_op_case(1, ('&'), band, (fold, a, b));
1439 fold_op_case(1, ('^'), xor, (fold, a, b));
1440 fold_op_case(1, ('<'), ltgt, (fold, a, b, true));
1441 fold_op_case(1, ('>'), ltgt, (fold, a, b, false));
1442 fold_op_case(2, ('<', '<'), lshift, (fold, a, b));
1443 fold_op_case(2, ('>', '>'), rshift, (fold, a, b));
1444 fold_op_case(2, ('|', '|'), andor, (fold, a, b, true));
1445 fold_op_case(2, ('&', '&'), andor, (fold, a, b, false));
1446 fold_op_case(2, ('?', ':'), tern, (fold, a, b, c));
1447 fold_op_case(2, ('*', '*'), exp, (fold, a, b));
1448 fold_op_case(3, ('<','=','>'), lteqgt, (fold, a, b));
1449 fold_op_case(2, ('!', '='), cmp, (fold, a, b, true));
1450 fold_op_case(2, ('=', '='), cmp, (fold, a, b, false));
1451 fold_op_case(2, ('~', 'P'), bnot, (fold, a));
1452 fold_op_case(2, ('>', '<'), cross, (fold, a, b));
1453 fold_op_case(3, ('l', 'e', 'n'), length, (fold, a));
1456 compile_error(fold_ctx(fold), "internal error: attempted to constant-fold for unsupported operator");
1461 * Constant folding for compiler intrinsics, similar approach to operator
1462 * folding, primarily: individual functions for each intrinsics to fold,
1463 * and a generic selection function.
1465 static GMQCC_INLINE ast_expression *fold_intrin_isfinite(fold_t *fold, ast_value *a) {
1466 return fold_constgen_float(fold, isfinite(fold_immvalue_float(a)), false);
1468 static GMQCC_INLINE ast_expression *fold_intrin_isinf(fold_t *fold, ast_value *a) {
1469 return fold_constgen_float(fold, isinf(fold_immvalue_float(a)), false);
1471 static GMQCC_INLINE ast_expression *fold_intrin_isnan(fold_t *fold, ast_value *a) {
1472 return fold_constgen_float(fold, isnan(fold_immvalue_float(a)), false);
1474 static GMQCC_INLINE ast_expression *fold_intrin_isnormal(fold_t *fold, ast_value *a) {
1475 return fold_constgen_float(fold, isnormal(fold_immvalue_float(a)), false);
1477 static GMQCC_INLINE ast_expression *fold_intrin_signbit(fold_t *fold, ast_value *a) {
1478 return fold_constgen_float(fold, signbit(fold_immvalue_float(a)), false);
1480 static GMQCC_INLINE ast_expression *fold_intirn_acosh(fold_t *fold, ast_value *a) {
1481 return fold_constgen_float(fold, acoshf(fold_immvalue_float(a)), false);
1483 static GMQCC_INLINE ast_expression *fold_intrin_asinh(fold_t *fold, ast_value *a) {
1484 return fold_constgen_float(fold, asinhf(fold_immvalue_float(a)), false);
1486 static GMQCC_INLINE ast_expression *fold_intrin_atanh(fold_t *fold, ast_value *a) {
1487 return fold_constgen_float(fold, (float)atanh(fold_immvalue_float(a)), false);
1489 static GMQCC_INLINE ast_expression *fold_intrin_exp(fold_t *fold, ast_value *a) {
1490 return fold_constgen_float(fold, expf(fold_immvalue_float(a)), false);
1492 static GMQCC_INLINE ast_expression *fold_intrin_exp2(fold_t *fold, ast_value *a) {
1493 return fold_constgen_float(fold, exp2f(fold_immvalue_float(a)), false);
1495 static GMQCC_INLINE ast_expression *fold_intrin_expm1(fold_t *fold, ast_value *a) {
1496 return fold_constgen_float(fold, expm1f(fold_immvalue_float(a)), false);
1498 static GMQCC_INLINE ast_expression *fold_intrin_mod(fold_t *fold, ast_value *lhs, ast_value *rhs) {
1499 return fold_constgen_float(fold, fmodf(fold_immvalue_float(lhs), fold_immvalue_float(rhs)), false);
1501 static GMQCC_INLINE ast_expression *fold_intrin_pow(fold_t *fold, ast_value *lhs, ast_value *rhs) {
1502 return fold_constgen_float(fold, powf(fold_immvalue_float(lhs), fold_immvalue_float(rhs)), false);
1504 static GMQCC_INLINE ast_expression *fold_intrin_fabs(fold_t *fold, ast_value *a) {
1505 return fold_constgen_float(fold, fabsf(fold_immvalue_float(a)), false);
1509 ast_expression *fold_intrin(fold_t *fold, const char *intrin, ast_expression **arg) {
1510 ast_expression *ret = NULL;
1511 ast_value *a = (ast_value*)arg[0];
1512 ast_value *b = (ast_value*)arg[1];
1514 if (!strcmp(intrin, "isfinite")) ret = fold_intrin_isfinite(fold, a);
1515 if (!strcmp(intrin, "isinf")) ret = fold_intrin_isinf(fold, a);
1516 if (!strcmp(intrin, "isnan")) ret = fold_intrin_isnan(fold, a);
1517 if (!strcmp(intrin, "isnormal")) ret = fold_intrin_isnormal(fold, a);
1518 if (!strcmp(intrin, "signbit")) ret = fold_intrin_signbit(fold, a);
1519 if (!strcmp(intrin, "acosh")) ret = fold_intirn_acosh(fold, a);
1520 if (!strcmp(intrin, "asinh")) ret = fold_intrin_asinh(fold, a);
1521 if (!strcmp(intrin, "atanh")) ret = fold_intrin_atanh(fold, a);
1522 if (!strcmp(intrin, "exp")) ret = fold_intrin_exp(fold, a);
1523 if (!strcmp(intrin, "exp2")) ret = fold_intrin_exp2(fold, a);
1524 if (!strcmp(intrin, "expm1")) ret = fold_intrin_expm1(fold, a);
1525 if (!strcmp(intrin, "mod")) ret = fold_intrin_mod(fold, a, b);
1526 if (!strcmp(intrin, "pow")) ret = fold_intrin_pow(fold, a, b);
1527 if (!strcmp(intrin, "fabs")) ret = fold_intrin_fabs(fold, a);
1530 ++opts_optimizationcount[OPTIM_CONST_FOLD];
1536 * These are all the actual constant folding methods that happen in between
1537 * the AST/IR stage of the compiler , i.e eliminating branches for const
1538 * expressions, which is the only supported thing so far. We undefine the
1539 * testing macros here because an ir_value is differant than an ast_value.
1545 #undef fold_immvalue_float
1546 #undef fold_immvalue_string
1547 #undef fold_immvalue_vector
1551 #define isfloat(X) ((X)->vtype == TYPE_FLOAT)
1552 /*#define isstring(X) ((X)->vtype == TYPE_STRING)*/
1553 /*#define isvector(X) ((X)->vtype == TYPE_VECTOR)*/
1554 #define fold_immvalue_float(X) ((X)->constval.vfloat)
1555 #define fold_immvalue_vector(X) ((X)->constval.vvec)
1556 /*#define fold_immvalue_string(X) ((X)->constval.vstring)*/
1557 #define fold_can_1(X) ((X)->hasvalue && (X)->cvq == CV_CONST)
1558 /*#define fold_can_2(X,Y) (fold_can_1(X) && fold_can_1(Y))*/
1560 static ast_expression *fold_superfluous(ast_expression *left, ast_expression *right, int op) {
1561 ast_expression *swapped = NULL; /* using this as bool */
1564 if (!ast_istype(right, ast_value) || !fold_can_1((load = (ast_value*)right))) {
1570 if (!ast_istype(right, ast_value) || !fold_can_1((load = (ast_value*)right)))
1578 if (fold_immvalue_float(load) == 1.0f) {
1579 ++opts_optimizationcount[OPTIM_PEEPHOLE];
1590 if (fold_immvalue_float(load) == 0.0f) {
1591 ++opts_optimizationcount[OPTIM_PEEPHOLE];
1598 if (vec3_cmp(fold_immvalue_vector(load), vec3_create(1, 1, 1))) {
1599 ++opts_optimizationcount[OPTIM_PEEPHOLE];
1609 if (vec3_cmp(fold_immvalue_vector(load), vec3_create(0, 0, 0))) {
1610 ++opts_optimizationcount[OPTIM_PEEPHOLE];
1620 ast_expression *fold_binary(lex_ctx_t ctx, int op, ast_expression *left, ast_expression *right) {
1621 ast_expression *ret = fold_superfluous(left, right, op);
1624 return (ast_expression*)ast_binary_new(ctx, op, left, right);
1627 static GMQCC_INLINE int fold_cond(ir_value *condval, ast_function *func, ast_ifthen *branch) {
1628 if (isfloat(condval) && fold_can_1(condval) && OPTS_OPTIMIZATION(OPTIM_CONST_FOLD_DCE)) {
1629 ast_expression_codegen *cgen;
1632 bool istrue = (fold_immvalue_float(condval) != 0.0f && branch->on_true);
1633 bool isfalse = (fold_immvalue_float(condval) == 0.0f && branch->on_false);
1634 ast_expression *path = (istrue) ? branch->on_true :
1635 (isfalse) ? branch->on_false : NULL;
1638 * no path to take implies that the evaluation is if(0) and there
1639 * is no else block. so eliminate all the code.
1641 ++opts_optimizationcount[OPTIM_CONST_FOLD_DCE];
1645 if (!(elide = ir_function_create_block(ast_ctx(branch), func->ir_func, ast_function_label(func, ((istrue) ? "ontrue" : "onfalse")))))
1647 if (!(*(cgen = path->codegen))((ast_expression*)path, func, false, &dummy))
1649 if (!ir_block_create_jump(func->curblock, ast_ctx(branch), elide))
1652 * now the branch has been eliminated and the correct block for the constant evaluation
1653 * is expanded into the current block for the function.
1655 func->curblock = elide;
1656 ++opts_optimizationcount[OPTIM_CONST_FOLD_DCE];
1659 return -1; /* nothing done */
1662 int fold_cond_ternary(ir_value *condval, ast_function *func, ast_ternary *branch) {
1663 return fold_cond(condval, func, (ast_ifthen*)branch);
1666 int fold_cond_ifthen(ir_value *condval, ast_function *func, ast_ifthen *branch) {
1667 return fold_cond(condval, func, branch);