TheThirdOne / rars

RARS -- RISC-V Assembler and Runtime Simulator
Other
1.14k stars 217 forks source link

0.0 + 0.0 = 0x7dc00000? #187

Closed buloruabutata closed 11 months ago

buloruabutata commented 11 months ago

if I run "fadd.s f18, f16, f7" alone, the result of f18 is 0x7dc00000 instead of 0x0000000, I want to know why this is.

TheThirdOne commented 11 months ago

This happens because, before initialization, the floating point registers have a value of double wide 0.0. Trying to access such a register as a float results in getting a NaN because it was not a correctly NaN boxed float.

When I implemented NaN boxing, I didn't give much thought to what the initial values were so maybe there a better solution, but simply initializing the registers you need with values should make it work as you expect.

radrow commented 5 months ago

Why not initialize them to single precision 0.0, so they are usable (and not misleading) for 32bit users? And if they are not meant to be used uninitialized, then I would rather put canonical NaN to make this conspicuous.