diff options
| author | Ben Noordhuis <info@bnoordhuis.nl> | 2018-06-20 21:51:18 +0200 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-06-20 17:37:38 -0400 |
| commit | eb6a8e6a3bba061c4a7fb18f53976e1fb683c3d4 (patch) | |
| tree | 137d90a657f1b6fe178f4eba4436c7ebfa2638b0 /src/ir.cpp | |
| parent | 4eca75c53b4679e7e31df1505d22a5b618a2d797 (diff) | |
| download | zig-eb6a8e6a3bba061c4a7fb18f53976e1fb683c3d4.tar.gz zig-eb6a8e6a3bba061c4a7fb18f53976e1fb683c3d4.zip | |
fix f128 remainder division bug
The modulo operation computed rem(b+rem(a,b), b) which produces -1
for a=1 and b=2.
Switch to a - b * trunc(a/b) which produces the expected result, 1.
closes #1137
Diffstat (limited to 'src/ir.cpp')
| -rw-r--r-- | src/ir.cpp | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/ir.cpp b/src/ir.cpp index ba2a9b3fe2..950d051492 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -7710,6 +7710,14 @@ static void float_rem(ConstExprValue *out_val, ConstExprValue *op1, ConstExprVal } } +// c = a - b * trunc(a / b) +static void zig_f128M_mod(const float128_t* a, const float128_t* b, float128_t* c) { + f128M_div(a, b, c); + f128M_roundToInt(c, softfloat_round_min, true, c); + f128M_mul(b, c, c); + f128M_sub(a, c, c); +} + static void float_mod(ConstExprValue *out_val, ConstExprValue *op1, ConstExprValue *op2) { assert(op1->type == op2->type); out_val->type = op1->type; @@ -7724,9 +7732,7 @@ static void float_mod(ConstExprValue *out_val, ConstExprValue *op1, ConstExprVal out_val->data.x_f64 = fmod(fmod(op1->data.x_f64, op2->data.x_f64) + op2->data.x_f64, op2->data.x_f64); return; case 128: - f128M_rem(&op1->data.x_f128, &op2->data.x_f128, &out_val->data.x_f128); - f128M_add(&out_val->data.x_f128, &op2->data.x_f128, &out_val->data.x_f128); - f128M_rem(&out_val->data.x_f128, &op2->data.x_f128, &out_val->data.x_f128); + zig_f128M_mod(&op1->data.x_f128, &op2->data.x_f128, &out_val->data.x_f128); return; default: zig_unreachable(); |
