N64 icon indicating copy to clipboard operation
N64 copied to clipboard

FPU Rounding Control

Open bryanperris opened this issue 5 years ago • 5 comments

The current set of CP1 test roms all test against the FPU while using the default rounding mode: nearest. Having tests using the other 3 modes would be helpful: toward zero, toward positive infinity, toward negative infinity.

http://www.cplusplus.com/reference/cfenv/fesetround/

bryanperris avatar Mar 03 '19 02:03 bryanperris

Some more info: According to mips documentation bits [0:1] of CP1.FCSR register ($31) set the rounding mode (modes in order being nearest, zero, +inf, -inf). So for example something like this should set the rounding mode to zero (quick and dirty):

li t0, 1
ctc1 t0, 31

Though a quick test on cen64 yielded no difference in the results, as far as I can tell only the forced rounding modes are implemented there (ceil/floor) and it always uses nearest mode otherwise.

Zapeth avatar Dec 19 '19 14:12 Zapeth

I thought the explicit rounding opcodes weren't affected by the rounding bit. The last time I looked at the FPU pipeline, rounding is applied before writeback so I think that stage is affected by the rounding control bit (except for those rounding instructions). This make senses in general when an application requires control over the rounding mode for all FPU operations globally so it doesn't have to do it explicitly. For PJ64, it updates the x87 FPU rounding mode according to the register bit, so that means its applying the rounding mode to all operations done in the native FPU pipeline.

bryanperris avatar Dec 19 '19 16:12 bryanperris

I thought the explicit rounding opcodes weren't affected by the rounding bit.

Yes, thats what I meant with those being implemented on cen64 (its programmed to switch to the according rounding mode for those opcodes). However apart from those cases it seems that cen64 is only operating in nearest mode (in other words, writes to the rounding control bits have no effect).

For PJ64, it updates the x87 FPU rounding mode according to the register bit, so that means its applying the rounding mode to all operations done in the native FPU pipeline.

A quick test with a modified CP1DIV test rom on PJ64 using the two instructions above reveals at least different results, which indicates that the rounding control bits make a difference there (haven't checked if the results are correct).

The same modified test rom makes no difference on cen64, mupen64plus wouldn't boot it (maybe I'll look into that at some point).

Zapeth avatar Dec 19 '19 17:12 Zapeth

Small update: I got the test rom to boot on mupen64plus as well (https://github.com/mupen64plus/mupen64plus-core/pull/692), actually it just didn't display anything, ~~though there is still an issue where the emulator isn't responding to key presses after loading the rom~~ (turns out this is caused by https://github.com/mupen64plus/mupen64plus-core/commit/9aae471aa25909ec7da877337790b1a4b8a468a3)

With the modified rom it produces the same results as PJ64.

Zapeth avatar Dec 22 '19 13:12 Zapeth

Thanks for all the info & testing Zapeth & bryanperris, I'll put this on my todo list to check out & make tests =D

PeterLemon avatar Dec 22 '19 21:12 PeterLemon