Skip to content

Commit

Permalink
Fix FScale'ing zero that should return zero
Browse files Browse the repository at this point in the history
Added tests will fail without the current patch.
This will properly check if we return the correct sign of zero.
  • Loading branch information
pmatos committed Sep 26, 2024
1 parent f49d82d commit e1e0d9e
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 2 deletions.
4 changes: 4 additions & 0 deletions FEXCore/Source/Common/SoftFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ struct FEX_PACKED X80SoftFloat {

return Result;
#else
extFloat80_t Zero {0, 0};
if (extF80_eq(state, lhs, Zero)) {
return lhs;
}
X80SoftFloat Int = FRNDINT(state, rhs, softfloat_round_minMag);
LIBRARY_PRECISION Src2_d = Int.ToFMax(state);
Src2_d = exp2l(Src2_d);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,11 @@ struct OpHandlers<IR::OP_F64FYL2X> {
template<>
struct OpHandlers<IR::OP_F64SCALE> {
static double handle(uint16_t FCW, double src1, double src2) {
double trunc = (double)(int64_t)(src2); // truncate
return src1 * exp2(trunc);
if (src1 == 0.0) {
return std::signbit(src1) ? -0.0 : 0.0;
}
double trun = trunc(src2);
return src1 * exp2(trun);
}
};

Expand Down
85 changes: 85 additions & 0 deletions unittests/ASM/X87/FScale-Zero.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
%ifdef CONFIG
{
"RegData": {
"R8": "0",
"R9": "0",
"R10": "0",
"R11": "0",
"R12": "0",
"R13": "0x8000000000000000"
}
}
%endif

section .data
neg_zero dq 0x8000000000000000 ; -0.0

section .bss
align 8
intstor resq 1

section .text
global _start

_start:
; scale by zero (st1 == 0)
mov rax, 0
mov qword [rel intstor], rax
finit
fild qword [rel intstor]
fldz
fscale
fst qword [rel intstor]
mov r8, [rel intstor]

; scale by zero (st1 == 1)
mov rax, 1
mov qword [rel intstor], rax
finit
fild qword [rel intstor]
fldz
fscale
fst qword [rel intstor]
mov r9, [rel intstor]

; scale by zero (st1 == 100)
mov rax, 100
mov qword [rel intstor], rax
finit
fild qword [rel intstor]
fldz
fscale
fst qword [rel intstor]
mov r10, [rel intstor]

; scale by zero (st1 == 1024)
mov rax, 1024
mov qword [rel intstor], rax
finit
fild qword [rel intstor]
fldz
fscale
fst qword [rel intstor]
mov r11, [rel intstor]

; scale by zero (st1 == 1048576)
mov rax, 1048576
mov qword [rel intstor], rax
finit
fild qword [rel intstor]
fldz
fscale
fst qword [rel intstor]
mov r12, [rel intstor]

; tests scaling negative zero
mov rax, 1048576
mov qword [rel intstor], rax
finit
fild qword [rel intstor]
fld qword [rel neg_zero]
fscale
fst qword [rel intstor]
mov r13, [rel intstor]

hlt
81 changes: 81 additions & 0 deletions unittests/ASM/X87_F64/FScale-Zero_F64.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
%ifdef CONFIG
{
"RegData": {
"R8": "0",
"R9": "0",
"R10": "0",
"R11": "0",
"R12": "0"
},
"Env": { "FEX_X87REDUCEDPRECISION" : "1" }
}
%endif

section .data
neg_zero dq 0x8000000000000000 ; -0.0

section .bss
intstor: resq 1

section .text
; scale by zero (st1 == 0)
mov rax, 0
mov qword [rel intstor], rax
finit
fild qword [rel intstor]
fldz
fscale
fst qword [rel intstor]
mov r8, [rel intstor]

; scale by zero (st1 == 1)
mov rax, 1
mov qword [rel intstor], rax
finit
fild qword [rel intstor]
fldz
fscale
fst qword [rel intstor]
mov r9, [rel intstor]

; scale by zero (st1 == 100)
mov rax, 100
mov qword [rel intstor], rax
finit
fild qword [rel intstor]
fldz
fscale
fst qword [rel intstor]
mov r10, [rel intstor]

; scale by zero (st1 == 1024)
mov rax, 1024
mov qword [rel intstor], rax
finit
fild qword [rel intstor]
fldz
fscale
fst qword [rel intstor]
mov r11, [rel intstor]

; scale by zero (st1 == 1048576)
mov rax, 1048576
mov qword [rel intstor], rax
finit
fild qword [rel intstor]
fldz
fscale
fst qword [rel intstor]
mov r12, [rel intstor]

; tests scaling negative zero
mov rax, 1048576
mov qword [rel intstor], rax
finit
fild qword [rel intstor]
fld qword [rel neg_zero]
fscale
fst qword [rel intstor]
mov r13, [rel intstor]

hlt

0 comments on commit e1e0d9e

Please sign in to comment.