aboutsummaryrefslogtreecommitdiff
path: root/tccgen.c
diff options
context:
space:
mode:
authorgrischka <grischka>2016-10-14 10:44:57 +0200
committergrischka <grischka>2017-02-05 14:30:20 +0100
commitf077d16c20c524d7db8cb38a9193242eeade343d (patch)
tree16fe494074710b7b177cfb9b4cc5944900d7b276 /tccgen.c
parent3b84e61eaddb068707a7a3d40a9ebd5e69975965 (diff)
downloadtinycc-f077d16c20c524d7db8cb38a9193242eeade343d.tar.gz
tinycc-f077d16c20c524d7db8cb38a9193242eeade343d.tar.bz2
tccgen: gen_cast: cast FLOAT to DOUBLE
... to avoid precision loss when casting to int, also when saving FLOATs to stack
Diffstat (limited to 'tccgen.c')
-rw-r--r--tccgen.c9
1 files changed, 9 insertions, 0 deletions
diff --git a/tccgen.c b/tccgen.c
index 70e6fb6..2804539 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -811,6 +811,10 @@ ST_FUNC void save_reg_upstack(int r, int n)
#else
type = &int_type;
#endif
+ if ((type->t & VT_BTYPE) == VT_FLOAT) {
+ /* cast to DOUBLE to avoid precision loss */
+ type->t = (type->t & ~VT_BTYPE) | VT_DOUBLE;
+ }
size = type_size(type, &align);
loc = (loc - size) & -align;
sv.type.t = type->t;
@@ -2349,6 +2353,11 @@ static void gen_cast(CType *type)
vpushi(0);
gen_op(TOK_NE);
} else {
+ if (sbt == VT_FLOAT) {
+ /* cast to DOUBLE to avoid precision loss */
+ gen_cvt_ftof(VT_DOUBLE);
+ vtop->type.t = (vtop->type.t & ~VT_BTYPE) | VT_DOUBLE;
+ }
/* we handle char/short/etc... with generic code */
if (dbt != (VT_INT | VT_UNSIGNED) &&
dbt != (VT_LLONG | VT_UNSIGNED) &&