the rules for constant expressions in static initializers are more relaxed than for integer constant expressions. We need to accept 0.0/0.0 in static initializers (in non-static initializers the potential exceptions need to be raised though, so no translation-time calculation then).
Unified · Split
| @@ -2049,9 +2049,10 @@ static void gen_opif(int op) · 2048 unmodified lines | |||
| 2049 | case '*': f1 *= f2; break; | 2049 | case '*': f1 *= f2; break; |
| 2050 | case '/': | 2050 | case '/': |
| 2051 | if (f2 == 0.0) { | 2051 | if (f2 == 0.0) { |
| 2052 | if (const_wanted) | 2052 | /* If not in initializer we need to potentially generate |
| 2053 | tcc_error("division by zero in constant"); | 2053 | FP exceptions at runtime, otherwise we want to fold. */ |
| 2054 | goto general_case; | 2054 | if (!const_wanted) |
| 2055 | goto general_case; | ||
| 2055 | } | 2056 | } |
| 2056 | f1 /= f2; | 2057 | f1 /= f2; |
| 2057 | break; | 2058 | break; |
| @@ -2234,6 +2234,9 @@ void float_test(void) · 2233 unmodified lines | |||
| 2234 | double da, db; | 2234 | double da, db; |
| 2235 | int a; | 2235 | int a; |
| 2236 | unsigned int b; | 2236 | unsigned int b; |
| 2237 | static double nan2 = 0.0/0.0; | ||
| 2238 | static double inf1 = 1.0/0.0; | ||
| 2239 | static double inf2 = 1e5000; | ||
| 2237 | 2240 | ||
| 2238 | printf("float_test:\n"); | 2241 | printf("float_test:\n"); |
| 2239 | printf("sizeof(float) = %d\n", sizeof(float)); | 2242 | printf("sizeof(float) = %d\n", sizeof(float)); |
| @@ -2255,6 +2258,7 @@ void float_test(void) · 15 unmodified lines | |||
| 2255 | b = 4000000000; | 2258 | b = 4000000000; |
| 2256 | db = b; | 2259 | db = b; |
| 2257 | printf("db = %f\n", db); | 2260 | printf("db = %f\n", db); |
| 2261 | printf("nan != nan = %d, inf1 = %f, inf2 = %f\n", nan2 != nan2, inf1, inf2); | ||
| 2258 | #endif | 2262 | #endif |
| 2259 | } | 2263 | } |
| 2260 | 2264 | ||