History

Another hex float fix

Hex floating point number did fail when a lot of digits are present. See testcase 70.

herman ten brugge committed 1 year ago

Commit 59aecdb1 parent c6792da3 files · +17 −9Browse files

Unified · Split

3 files
  1. tccpp.c +12 −8
  2. tests/tests2/70_floating_point_literals.c +3 −1
  3. tests/tests2/70_floating_point_literals.expect +2 −0
tccpp.c+12−8
@@ -2240,15 +2240,18 @@ static void parse_string(const char *s, int len) · 2239 unmodified lines
2240#endif2240#endif
2241 2241
2242/* bn = (bn << shift) | or_val */2242/* bn = (bn << shift) | or_val */
2243static void bn_lshift(unsigned int *bn, int shift, int or_val)2243static int bn_lshift(unsigned int *bn, int shift, int or_val)
2244{2244{
2245 int i;2245 int i;
2246 unsigned int v;2246 unsigned int v;
2247 if (bn[BN_SIZE - 1] >> (32 - shift))
2248 return shift;
2247 for(i=0;i<BN_SIZE;i++) {2249 for(i=0;i<BN_SIZE;i++) {
2248 v = bn[i];2250 v = bn[i];
2249 bn[i] = (v << shift) | or_val;2251 bn[i] = (v << shift) | or_val;
2250 or_val = v >> (32 - shift);2252 or_val = v >> (32 - shift);
2251 }2253 }
2254 return 0;
2252}2255}
2253 2256
2254static void bn_zero(unsigned int *bn)2257static void bn_zero(unsigned int *bn)
@@ -2321,6 +2324,7 @@ static void parse_number(const char *p) · 66 unmodified lines
2321 it by hand */2324 it by hand */
2322 /* hexadecimal or binary floats */2325 /* hexadecimal or binary floats */
2323 /* XXX: handle overflows */2326 /* XXX: handle overflows */
2327 frac_bits = 0;
2324 *q = '\0';2328 *q = '\0';
2325 if (b == 16)2329 if (b == 16)
2326 shift = 4;2330 shift = 4;
@@ -2339,9 +2343,8 @@ static void parse_number(const char *p) · 12 unmodified lines
2339 } else {2343 } else {
2340 t = t - '0';2344 t = t - '0';
2341 }2345 }
2342 bn_lshift(bn, shift, t);2346 frac_bits -= bn_lshift(bn, shift, t);
2343 }2347 }
2344 frac_bits = 0;
2345 if (ch == '.') {2348 if (ch == '.') {
2346 ch = *p++;2349 ch = *p++;
2347 while (1) {2350 while (1) {
@@ -2357,7 +2360,7 @@ static void parse_number(const char *p) · 9 unmodified lines
2357 }2360 }
2358 if (t >= b)2361 if (t >= b)
2359 tcc_error("invalid digit");2362 tcc_error("invalid digit");
2360 bn_lshift(bn, shift, t);2363 frac_bits -= bn_lshift(bn, shift, t);
2361 frac_bits += shift;2364 frac_bits += shift;
2362 ch = *p++;2365 ch = *p++;
2363 }2366 }
@@ -2376,7 +2379,9 @@ static void parse_number(const char *p) · 12 unmodified lines
2376 if (ch < '0' || ch > '9')2379 if (ch < '0' || ch > '9')
2377 expect("exponent digits");2380 expect("exponent digits");
2378 while (ch >= '0' && ch <= '9') {2381 while (ch >= '0' && ch <= '9') {
2379 exp_val = exp_val * 10 + ch - '0';2382 /* If exp_val is this large ldexp will return HUGE_VAL */
2383 if (exp_val < 100000000)
2384 exp_val = exp_val * 10 + ch - '0';
2380 ch = *p++;2385 ch = *p++;
2381 }2386 }
2382 exp_val = exp_val * s;2387 exp_val = exp_val * s;
@@ -2405,12 +2410,11 @@ static void parse_number(const char *p) · 22 unmodified lines
2405#ifdef TCC_USING_DOUBLE_FOR_LDOUBLE2410#ifdef TCC_USING_DOUBLE_FOR_LDOUBLE
2406 tokc.d = d;2411 tokc.d = d;
2407#else2412#else
2408 /* XXX: not large enough */2413 tokc.ld = d;
2409 tokc.ld = (long double)d;
2410#endif2414#endif
2411 } else {2415 } else {
2412 tok = TOK_CDOUBLE;2416 tok = TOK_CDOUBLE;
2413 tokc.d = d;2417 tokc.d = (double)d;
2414 }2418 }
2415 } else {2419 } else {
2416 /* decimal floats */2420 /* decimal floats */
tests/tests2/70_floating_point_literals.c+3−1
@@ -31,7 +31,9 @@ int main() · 30 unmodified lines
31 double da0 = 0X.1ACP12;31 double da0 = 0X.1ACP12;
32 double da1 = 0x.1acp-12;32 double da1 = 0x.1acp-12;
33 double da2 = 0x.1acp+12;33 double da2 = 0x.1acp+12;
34 printf("%f\n%f\n%f\n\n", da0, da1, da2);34 double da3 = 0x1.00000000000000000000000000000000000000000000p+12;
35 double da4 = 0x1000000000000000000000000000000000000000000000p-140;
36 printf("%f\n%f\n%f\n%f\n%f\n\n", da0, da1, da2, da3, da4);
35 37
36 double db0 = 0X1AC.BDP12;38 double db0 = 0X1AC.BDP12;
37 double db1 = 0x1ac.bdp-12;39 double db1 = 0x1ac.bdp-12;
tests/tests2/70_floating_point_literals.expect+2−0
@@ -21,6 +21,8 @@ · 20 unmodified lines
21428.00000021428.000000
220.000026220.000026
23428.00000023428.000000
244096.000000
251099511627776.000000
24 26
251756112.000000271756112.000000
260.104672280.104672

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t