Floating point arithmetic
The processor's floating point unit (COP1) works with numbers of 32 bits (S) and 64 bits (D). It also converts
between them and whole numbers of 32 bits (W) and 64 bits (L). crates/n64-fpu has its arithmetic: what an operation
gives, and which exceptions it raises. It has no state and no time. The registers, traps and cycles are
the processor's.
Section, table and page numbers are those of NEC's VR4300 User's Manual (U10504EJ7V0UM00). Where it
says nothing else, the rules are IEEE 754's. Nine rules that the manual leaves open were measured on a console (below).
What is still open is in what is a guess.
An operation, step by step
The processor gives the operands' bits. An operation that can round also needs the rounding mode, and one that can have
a denormal result needs FS and the enabled exceptions too. For ADD.S:
- The operands are looked at first. A denormal number or a quiet NaN is left to software: the result is
the unimplemented operation exception (E), and nothing else is raised. A signalling NaN is an invalid
operation (V). Infinities and zeros have an exact result, or none, such as plus infinity added to minus infinity.
- Two normal numbers are added exactly, with whole numbers. Everything below the bits that are kept becomes one
bit, which says that something was there.
- The sum is rounded once, to 24 bits for S, and to 53 for D, as if the exponent had no limits. If it changed,
it is inexact (I).
- Then its size is checked. A result that is too large is an overflow, and one that is too small an
underflow.
The processor then has the result's bits, and the flags for its Cause field. If one of them is enabled, or it is E, it traps:
it writes no result and changes no Flag bit. Otherwise it writes the result, and adds the flags to the Flag
field (7.2.4). After a comparison, the result is the C bit. E always comes alone (B.1.5).
MOV.S and MOV.D are no arithmetic. They copy any bits, and change neither Cause nor Flag (7.2.4, and the page of MOV.fmt).
NaNs
A NaN with the highest bit of its fraction set is a signalling one, and any other is quiet (7.3). IEEE 754-2008, and with
it most machines today, have this the other way round.
| Operand | Arithmetic, ABS, NEG, and between S and D | To a whole number | Comparison |
| Quiet NaN | E | E | cannot be compared |
| Signalling NaN | V | E | cannot be compared, and V |
| Denormal number | E | E | a number like any other |
They are from table 8-2, 8.2.3 and 8.2.7. FS changes nothing for operands: it is about results (7.2.4). An
invalid operation gives a quiet NaN (table 8-1). Here it is 7FBFFFFF in S, and 7FF7FFFFFFFFFFFF in D.
Results that are too large
They always raise overflow and inexact together (table 8-2, note 1). The result is infinity or the largest
number, whichever the rounding mode goes to (table 8-1). What counts is the rounded result (8.2.5).
A division by zero is raised only for a finite number that is not zero. Its result is infinity (8.2.4). Infinity divided by
zero is infinity, and raises nothing. Zero divided by zero is invalid.
Results that are too small
The unit never gives a denormal number. A result is too small when it is not zero and is below the smallest normal
number after rounding (8.2.6). One that rounds up to that number is only inexact. Otherwise:
-
With FS set, and neither underflow nor inexact enabled, it is flushed. It raises both, also when a
denormal number could hold it exactly. Table 7-2 has the result:
| Nearest | To zero | Up | Down |
| Positive | +0 | +0 | the smallest normal number | +0 |
| Negative | -0 | -0 | -0 | minus the smallest normal number |
-
Otherwise it is E (8.2.6, and note 2 of table 8-2).
7.2.4 and 8.2.2 say "illegal" for the second exception. 8.2.6 and 8.2.7 say "inexact", and so does the second note of table 8-2. It
stands at the row for invalid operations, but is about underflow.
Conversions
To whole numbers: CVT has the control register's rounding mode. ROUND (to the nearest), TRUNC, CEIL and FLOOR have
their own. A W result has to fit into 32 bits after rounding. An L result has to be from -(2^53 - 1) to 2^53 - 1: -2^53 gives E, which was measured (below). Any other
result is E, and so is each NaN, infinity and denormal number (8.2.7). The instructions' pages also say that these are invalid
and give the largest whole number. Table 8-2 shows that as IEEE 754's way, and E as this chip's.
From whole numbers: an L operand has to have bits 63 to 55 all the same, so it is from -2^55 to 2^55 - 1. Any
other one is E (7.5.2 and B.3.3). Each W operand can be converted.
Between S and D: from S to D is always exact. From D to S is rounded, and can be too large or too small.
Comparisons
The lowest four bits of the instruction say what is asked (table 17-2):
| Bit | Meaning |
| 0 | true if they cannot be compared, as one is a NaN |
| 1 | true if they are equal |
| 2 | true if the first one is less |
| 3 | V if they cannot be compared |
+0 and -0 are equal. A signalling NaN is V without bit 3 too.
What was measured
On one console, the owner's (a US console, NTSC: record m2-2026-12-01, from our program 2,
tools/console/m2.c). Each of these was a guess here before, and only -2^53 was wrong. n64emu gives the same as the console for all of
them now. The manual says nothing about these, or contradicts itself:
| What | On the console | Why it was a guess |
| The bits of the NaN that an invalid operation gives | 7FBFFFFF and 7FF7FFFFFFFFFFFF (0/0 in S and D, and the square root of -1) | The manual only says that it is quiet, which leaves many values. The guess came from what an agent knew from its training |
| An infinity converted between S and D | It stays one, and raises nothing | Table 8-2 does not name it in its nine causes. 8.2.7, appendix D and the pages of CVT.S and CVT.D list an infinite operand as E |
| A signalling NaN converted between S and D | V, and the default NaN | Table 8-2 and 8.2.3. The same list has NaNs as E |
| A signalling NaN converted to a whole number | E | That list. Table 8-2 has V for each signalling NaN |
| A quiet NaN in ABS or NEG | E | Table 8-2 and 8.2.7. Their own pages say that a NaN is invalid |
| A signalling NaN in a comparison without bit 3 (C.EQ.S) | V | 8.2.3, table 8-2 and IEEE 754. The page of C.cond and table 17-2 have only bit 3 |
| A denormal number divided by zero | E alone | B.1.5 says that E comes alone. No page says what is looked at first |
| -2^53 converted to L | E. The guess was that it is converted, and it is changed | The pages say "any of bits 53 to 62 of the result", which each negative number has. The same sentence about bits 55 to 62 is explained in 7.5.2 as "all zeroes or ones". It means the size |
| A result that a denormal number could hold exactly, with FS (2^-126 times 0.5) | It is flushed to 0, and raises U and I | 7.2.4 and 8.2.7 speak of any denormal result. No page has this case by itself |
What is a guess
| What | Here | Why |
| An L operand converted to S or D | It has to be from -2^55 to 2^55 - 1 | 7.5.2 and B.3.3 say "all zeroes or ones" for bits 63 to 55. That -2^53 gives E as a result (above) does not say what -2^55 does as an operand |
| The other cases of an operand for E together with a reason for V or a division by zero | E alone | Only a denormal number divided by zero was measured |
What is not emulated
The multiplication bug of early consoles. n64brew's page about the VR4300 says that a multiplication can go wrong when one shortly before
it had a NaN, a zero or an infinity as an operand. It names no source, and does not say what the wrong result is.
It would also need to know the instruction before, which this crate does not.
How it is checked
- By hand:
tests/rules.rs has what the manual says, and names the tests of guesses. It agrees with how the manual is read
here, which is no proof that the chip does the same.
- Against exact arithmetic:
tests/exact.rs finds the two numbers on either side of the exact result, with whole numbers
of any size. It computes no result like the crate does. It has add, subtract, multiply, divide, square root, D to S and from whole numbers, in each
rounding mode, with and without FS, and with different exceptions enabled. It checks that ties, overflows
and underflows were among them. A longer run is only done when asked for:
cargo test --release --locked -p n64-fpu --test exact -- --ignored.
- Against the computer's own arithmetic:
tests/host.rs does this where the rules are the same. Conversions to whole numbers are
checked there in each rounding mode.
Speed
All results come from whole numbers. The computer's floating point unit is only used in tests, so the bits and the flags are
the same on each machine. cargo run --release --locked -p n64-fpu --example speed shows how long each operation takes.
These are from two runs on a Ryzen 9 5950X that other programs were using too (a load of about 3). The operands were normal numbers of similar
sizes, and the results were rounded to the nearest:
| S | D |
| Add | 6.4 ns | 6.5 ns |
| Multiply | 4.6 ns | 4.7 ns |
| Divide | 8.1 to 8.2 ns | 8.0 to 8.1 ns |
| Square root | 23.0 ns | 23.1 to 23.2 ns |
Shortcuts with the computer's own arithmetic were tried in a trial, and left out. In both of its runs, they gained nothing
in add.s and mul.s, about 1 ns in add.d, and 2.5 ns in div.s. The console needs at least
32 ns for an addition that is not a special case, and 309 ns for such a division in S (table 7-14, at 93.75 MHz). No game was measured.
Rounding without branches was tried too. It was no faster, and conversions with whole numbers took longer.