Skip to content

Commit

Permalink
Add support for constant float compare
Browse files Browse the repository at this point in the history
  • Loading branch information
hermantb committed May 22, 2023
1 parent 0ec3a40 commit a46372e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
24 changes: 23 additions & 1 deletion tccgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -2461,7 +2461,7 @@ void gen_negf(int op)
/* generate a floating point operation with constant propagation */
static void gen_opif(int op)
{
int c1, c2;
int c1, c2, cast_int = 0;
SValue *v1, *v2;
#if defined _MSC_VER && defined __x86_64__
/* avoid bad optimization with f1 -= f2 for f1:-0.0, f2:0.0 */
Expand Down Expand Up @@ -2519,6 +2519,26 @@ static void gen_opif(int op)
case TOK_NEG:
f1 = -f1;
goto unary_result;
case TOK_EQ:
f1 = f1 == f2;
make_int:
cast_int = 1;
break;
case TOK_NE:
f1 = f1 != f2;
goto make_int;
case TOK_LT:
f1 = f1 < f2;
goto make_int;
case TOK_GE:
f1 = f1 >= f2;
goto make_int;
case TOK_LE:
f1 = f1 <= f2;
goto make_int;
case TOK_GT:
f1 = f1 > f2;
goto make_int;
/* XXX: also handles tests ? */
default:
goto general_case;
Expand All @@ -2533,6 +2553,8 @@ static void gen_opif(int op)
} else {
v1->c.ld = f1;
}
if (cast_int)
gen_cast_s(VT_INT);
} else {
general_case:
if (op == TOK_NEG) {
Expand Down
7 changes: 7 additions & 0 deletions tests/tests2/22_floating_point.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ test()

int main()
{
static int e1 = -1.0 == 0.0;
static int e2 = -1.0 != 0.0;
static int e3 = -1.0 < 0.0;
static int e4 = -1.0 >= 0.0;
static int e5 = -1.0 <= 0.0;
static int e6 = -1.0 > 0.0;
// variables
float a = 12.34 + 56.78;
printf("%f\n", a);
Expand All @@ -30,6 +36,7 @@ int main()
printf("%d %d %d %d %d %d\n", 12.34 < 56.78, 12.34 <= 56.78, 12.34 == 56.78, 12.34 >= 56.78, 12.34 > 56.78, 12.34 != 56.78);
printf("%d %d %d %d %d %d\n", 12.34 < 12.34, 12.34 <= 12.34, 12.34 == 12.34, 12.34 >= 12.34, 12.34 > 12.34, 12.34 != 12.34);
printf("%d %d %d %d %d %d\n", 56.78 < 12.34, 56.78 <= 12.34, 56.78 == 12.34, 56.78 >= 12.34, 56.78 > 12.34, 56.78 != 12.34);
printf("%d %d %d %d %d %d\n", e1, e2, e3, e4, e5, e6);

// assignment operators
a = 12.34;
Expand Down
1 change: 1 addition & 0 deletions tests/tests2/22_floating_point.expect
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
1 1 0 0 0 1
0 1 1 1 0 0
0 0 0 1 1 1
0 1 1 0 1 0
69.120003
-44.439999
700.665222
Expand Down

0 comments on commit a46372e

Please sign in to comment.