From 5a76c5d2f39ad087b5153d458503caabb871c513 Mon Sep 17 00:00:00 2001 From: Lee Duhem Date: Mon, 15 Dec 2014 16:32:08 +0800 Subject: [PATCH] Fix parsing of binary floating point number * tccpp.c (parse_number): `shift' should be 1 while parsing binary floating point number. * tests/tests2/70_floating_point_literals.c: New test cases for floating point number parsing. --- tccpp.c | 2 +- tests/tests2/70_floating_point_literals.c | 77 ++++++++++++++++++++++++++ tests/tests2/70_floating_point_literals.expect | 53 ++++++++++++++++++ tests/tests2/Makefile | 3 +- 4 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 tests/tests2/70_floating_point_literals.c create mode 100644 tests/tests2/70_floating_point_literals.expect diff --git a/tccpp.c b/tccpp.c index 0cea7cf..4c7cf80 100644 --- a/tccpp.c +++ b/tccpp.c @@ -1874,7 +1874,7 @@ static void parse_number(const char *p) if (b == 16) shift = 4; else - shift = 2; + shift = 1; bn_zero(bn); q = token_buf; while (1) { diff --git a/tests/tests2/70_floating_point_literals.c b/tests/tests2/70_floating_point_literals.c new file mode 100644 index 0000000..012fb4f --- /dev/null +++ b/tests/tests2/70_floating_point_literals.c @@ -0,0 +1,77 @@ +#include + +int main() +{ + /* decimal floating constant */ + float fa0 = .123f; + float fa1 = .123E12F; + float fa2 = .123e-12f; + float fa3 = .123e+12f; + printf("%f\n%f\n%f\n%f\n\n", fa0, fa1, fa2, fa3); + + float fb0 = 123.123f; + float fb1 = 123.123E12F; + float fb2 = 123.123e-12f; + float fb3 = 123.123e+12f; + printf("%f\n%f\n%f\n%f\n\n", fb0, fb1, fb2, fb3); + + float fc0 = 123.f; + float fc1 = 123.E12F; + float fc2 = 123.e-12f; + float fc3 = 123.e+12f; + printf("%f\n%f\n%f\n%f\n\n", fc0, fc1, fc2, fc3); + + float fd0 = 123E12F; + float fd1 = 123e-12f; + float fd2 = 123e+12f; + printf("%f\n%f\n%f\n\n", fd0, fd1, fd2); + printf("\n"); + + /* hexadecimal floating constant */ + double da0 = 0X.1ACP12; + double da1 = 0x.1acp-12; + double da2 = 0x.1acp+12; + printf("%f\n%f\n%f\n\n", da0, da1, da2); + + double db0 = 0X1AC.BDP12; + double db1 = 0x1ac.bdp-12; + double db2 = 0x1ac.dbp+12; + printf("%f\n%f\n%f\n\n", db0, db1, db2); + + double dc0 = 0X1AC.P12; + double dc1 = 0x1ac.p-12; + double dc2 = 0x1ac.p+12; + printf("%f\n%f\n%f\n\n", dc0, dc1, dc2); + + double dd0 = 0X1ACP12; + double dd1 = 0x1acp-12; + double dd2 = 0x1acp+12; + printf("%f\n%f\n%f\n\n", dd0, dd1, dd2); + printf("\n"); + +#ifdef __TINYC__ + /* TCC extension + binary floating constant */ + long double la0 = 0B.110101100P12L; + long double la1 = 0b.110101100p-12l; + long double la2 = 0b.110101100p+12l; + printf("%Lf\n%Lf\n%Lf\n\n", la0, la1, la2); + + long double lb0 = 0B110101100.10111101P12L; + long double lb1 = 0b110101100.10111101p-12l; + long double lb2 = 0b110101100.10111101p+12l; + printf("%Lf\n%Lf\n%Lf\n\n", lb0, lb1, lb2); + + long double lc0 = 0B110101100.P12L; + long double lc1 = 0b110101100.p-12l; + long double lc2 = 0b110101100.p+12l; + printf("%Lf\n%Lf\n%Lf\n\n", lc0, lc1, lc2); + + long double ld0 = 0B110101100P12L; + long double ld1 = 0b110101100p-12l; + long double ld2 = 0b110101100p+12l; + printf("%Lf\n%Lf\n%Lf\n\n", ld0, ld1, ld2); +#endif + + return 0; +} diff --git a/tests/tests2/70_floating_point_literals.expect b/tests/tests2/70_floating_point_literals.expect new file mode 100644 index 0000000..7eb1efb --- /dev/null +++ b/tests/tests2/70_floating_point_literals.expect @@ -0,0 +1,53 @@ +0.123000 +122999996416.000000 +0.000000 +122999996416.000000 + +123.123001 +123122997002240.000000 +0.000000 +123122997002240.000000 + +123.000000 +123000003231744.000000 +0.000000 +123000003231744.000000 + +123000003231744.000000 +0.000000 +123000003231744.000000 + + +428.000000 +0.000026 +428.000000 + +1756112.000000 +0.104672 +1756592.000000 + +1753088.000000 +0.104492 +1753088.000000 + +1753088.000000 +0.104492 +1753088.000000 + + +3424.000000 +0.000204 +3424.000000 + +1756112.000000 +0.104672 +1756112.000000 + +1753088.000000 +0.104492 +1753088.000000 + +1753088.000000 +0.104492 +1753088.000000 + diff --git a/tests/tests2/Makefile b/tests/tests2/Makefile index 924f03c..8e11f2c 100644 --- a/tests/tests2/Makefile +++ b/tests/tests2/Makefile @@ -88,7 +88,8 @@ TESTS = \ 66_macro_concat_end.test \ 67_macro_concat.test \ 68_macro_param_list_err_1.test \ - 69_macro_param_list_err_2.test + 69_macro_param_list_err_2.test \ + 70_floating_point_literals.test # 34_array_assignment.test -- array assignment is not in C standard -- 1.9.3