diff --git a/src/asm/lexer.c b/src/asm/lexer.c index 54a0d5624..7820c9855 100644 --- a/src/asm/lexer.c +++ b/src/asm/lexer.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -1130,7 +1131,7 @@ static void readFractionalPart(void) /* Cast to unsigned avoids UB if shifting discards bits */ yylval.nConstValue = (uint32_t)yylval.nConstValue << 16; /* Cast to unsigned avoids undefined overflow behavior */ - uint16_t fractional = value * 65536 / divisor; + uint16_t fractional = (uint16_t)round(value * 65536.0 / divisor); yylval.nConstValue |= fractional * (yylval.nConstValue >= 0 ? 1 : -1); } diff --git a/src/asm/math.c b/src/asm/math.c index df122748c..73f6e9f0d 100644 --- a/src/asm/math.c +++ b/src/asm/math.c @@ -20,7 +20,7 @@ #include "asm/warning.h" #define fx2double(i) ((double)((i) / 65536.0)) -#define double2fx(d) ((int32_t)((d) * 65536.0)) +#define double2fx(d) ((int32_t)round((d) * 65536.0)) #ifndef M_PI #define M_PI 3.14159265358979323846 diff --git a/test/asm/fixed-point-precision.asm b/test/asm/fixed-point-precision.asm new file mode 100644 index 000000000..9ae79d835 --- /dev/null +++ b/test/asm/fixed-point-precision.asm @@ -0,0 +1,11 @@ +f1 = 3.1 +f2 = 5.2 +pm = MUL(f1, f2) +pr = 16.12 + println "`3.1`: {9.6f:f1} -> ${08x:f1}" + println "`5.2`: {9.6f:f2} -> ${08x:f2}" + println "`MUL`: {9.6f:pm} -> ${08x:pm}" + println "`16.12`: {9.6f:pr} -> ${08x:pr}" + +fl = 6.283185 + println "`6.283185`: {.6f:fl} -> ${08x:fl}" diff --git a/test/asm/fixed-point-precision.err b/test/asm/fixed-point-precision.err new file mode 100644 index 000000000..e69de29bb diff --git a/test/asm/fixed-point-precision.out b/test/asm/fixed-point-precision.out new file mode 100644 index 000000000..ed6a33619 --- /dev/null +++ b/test/asm/fixed-point-precision.out @@ -0,0 +1,5 @@ +`3.1`: 3.100007 -> $0003199a +`5.2`: 5.199997 -> $00053333 +`MUL`: 16.120026 -> $00101eba +`16.12`: 16.119996 -> $00101eb8 +`6.283185`: 6.283188 -> $0006487f diff --git a/test/asm/underscore-in-numeric-literal.out.bin b/test/asm/underscore-in-numeric-literal.out.bin index c4f16cddf..b92b525f7 100644 Binary files a/test/asm/underscore-in-numeric-literal.out.bin and b/test/asm/underscore-in-numeric-literal.out.bin differ