Skip to content

Commit

Permalink
Fixed some rounding issues with x87 interpretor (thx @pale)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitSeb committed Sep 19, 2020
1 parent 2c6e916 commit b08960d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/emu/rund9.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
emu->sw.f.F87_C3 = (ll&2)?1:0;
emu->sw.f.F87_C0 = (ll&4)?1:0;
} else {
ll = (int64_t)(floor((ST0.d/ST1.d))/exp2(tmp32s - 32));
ll = (int64_t)(trunc((ST0.d/ST1.d))/exp2(tmp32s - 32));
ST0.d = ST0.d - ST1.d*ll*exp2(tmp32s - 32);
emu->sw.f.F87_C2 = 1;
}
Expand Down
32 changes: 2 additions & 30 deletions src/emu/rundb.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,7 @@
if(isgreater(ST0.d, (double)(int32_t)0x7fffffff) || isless(ST0.d, -(double)(int32_t)0x7fffffff))
ED->sdword[0] = 0x80000000;
else {
volatile int32_t tmp; // tmp to avoid BUS ERROR
switch(emu->round) {
case ROUND_Nearest:
ED->sdword[0] = floor(ST0.d+0.5);
break;
case ROUND_Down:
ED->sdword[0] = floor(ST0.d);
break;
case ROUND_Up:
ED->sdword[0] = ceil(ST0.d);
break;
case ROUND_Chop:
ED->sdword[0] = ST0.d;
break;
}
volatile int32_t tmp = fpu_round(emu, ST0.d); // tmp to avoid BUS ERROR
ED->sdword[0] = tmp;
}
break;
Expand All @@ -136,21 +122,7 @@
if(isgreater(ST0.d, (double)(int32_t)0x7fffffff) || isless(ST0.d, -(double)(int32_t)0x7fffffff))
ED->sdword[0] = 0x80000000;
else {
volatile int32_t tmp; // tmp to avoid BUS ERROR
switch(emu->round) {
case ROUND_Nearest:
tmp = floor(ST0.d+0.5);
break;
case ROUND_Down:
tmp = floor(ST0.d);
break;
case ROUND_Up:
tmp = ceil(ST0.d);
break;
case ROUND_Chop:
tmp = ST0.d;
break;
}
volatile int32_t tmp = fpu_round(emu, ST0.d); // tmp to avoid BUS ERROR
ED->sdword[0] = tmp;
}
fpu_do_pop(emu);
Expand Down
35 changes: 3 additions & 32 deletions src/emu/rundf.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,8 @@
GET_EW;
if(isgreater(ST0.d, (double)(int32_t)0x7fff) || isless(ST0.d, -(double)(int32_t)0x7fff))
EW->sword[0] = 0x8000;
else {
switch(emu->round) {
case ROUND_Nearest:
EW->sword[0] = floor(ST0.d+0.5);
break;
case ROUND_Down:
EW->sword[0] = floor(ST0.d);
break;
case ROUND_Up:
EW->sword[0] = ceil(ST0.d);
break;
case ROUND_Chop:
EW->sword[0] = ST0.d;
break;
}
}
else
EW->sword[0] = fpu_round(emu, ST0.d);
fpu_do_pop(emu);
break;
case 4: /* FBLD ST0, tbytes */
Expand Down Expand Up @@ -161,22 +147,7 @@
if(isgreater(ST0.d, (double)(int64_t)0x7fffffffffffffffLL) || isless(ST0.d, -(double)(int64_t)0x7fffffffffffffffLL))
*(int64_t*)ED = 0x8000000000000000LL;
else {
int64_t i64;
switch(emu->round) {
case ROUND_Nearest:
i64 = floor(ST0.d+0.5);
break;
case ROUND_Down:
i64 = floor(ST0.d);
break;
case ROUND_Up:
i64 = ceil(ST0.d);
break;
case ROUND_Chop:
default:
i64 = ST0.d;
break;
}
int64_t i64 = fpu_round(emu, ST0.d);
*(int64_t*)ED = i64;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/emu/x87emu_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ static inline double fpu_round(x86emu_t* emu, double d) {
return d;
switch(emu->round) {
case ROUND_Nearest:
return floor(d+0.5);
return nearbyint(d);
case ROUND_Down:
return floor(d);
case ROUND_Up:
return ceil(d);
case ROUND_Chop:
default:
return round(d);
return trunc(d);
}
}

Expand Down

0 comments on commit b08960d

Please # to comment.