§ 7.6. Divide and Square Root¶
RISC-V's F extension has FDIV.S and FSQRT.S. They are rare in most
programs and expensive in gates, so this unit spends time instead: one bit
of the quotient or root per clock edge, as the integer divider of
§ 5.1.1 does. (The GPU of the next chapter
has no such unit: Volta computes divisions and roots in software from an
approximate reciprocal.)
| Component | Description | Structural | Behavioral |
|---|---|---|---|
FloatDivSqrt<FMT> |
A / B or √A, one bit per edge, rounded once | Yes | Yes (compiled gate list) |
Catalog: float.div-sqrt.fp16, float.div-sqrt.fp32.
IO pins¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
CLK, RST |
Input | 1 each | Clock; reset clears the unit |
START |
Input | 1 | Begin an operation (taken when the unit is not busy) |
OP |
Input | 1 | 0: A / B, 1: √A |
A, B |
Input | 16 or 32 each | Operands, taken at START |
RM |
Input | 3 | Rounding mode, taken at START |
TAKE |
Input | 1 | The result was used: clears DONE |
BUSY |
Output | 1 | An operation is running |
DONE |
Output | 1 | Y and FLAGS hold the result, until TAKE |
Y |
Output | 16 or 32 | Result |
FLAGS |
Output | 5 | NX, UF, OF, DZ, NV |
Behavior¶
DONE rises at the P + 3rd rising edge counting the one that takes
START: 14 edges for FP16, 27 for FP32. The result is the reference's
correctly rounded divide or square root. x / 0 for finite non-zero x is ±∞
with divide-by-zero; 0 / 0 and ∞ / ∞ are invalid; the square root of a
negative number (other than −0) is invalid; √−0 = −0.
Implementation¶
At START both significands are normalized (a subnormal by its leading
zeros), the result's exponent is computed and the special cases are
decided; all of this is held in registers. Then a restoring recurrence
runs P + 2 steps, one per edge, on a remainder R and the result Q:
| One step | |
|---|---|
| Divide | q = (R ≥ D); R = (q ? R − D : R) shifted left by one |
| Square root | R = R shifted left by two, with the next two bits of the radicand; T = Q shifted left by two, plus 1; q = (R ≥ T); R = q ? R − T : R |
| Both | Q = Q shifted left by one, with q |
The radicand is the significand, doubled when the exponent is odd, times
2^(P+3), so the root has P + 2 bits with its leading 1 on top. After the
last step Q holds P + 2 result bits and a non-zero remainder is the sticky
bit; the shared normalizer, rounder and finishing logic of the other units
produce Y and FLAGS (a quotient below 1 shifts left by one; a quotient
below the smallest normal shifts right with sticky).
The registers (BUSY, DONE, the held operands and modes HELD_…, and
REMAINDER, QUOTIENT, RADICAND, COUNT) sit in the unit's top level,
between its steps:
| Part | Type | Inputs | Outputs | What it does |
|---|---|---|---|---|
UNPACK_A, UNPACK_B |
shared (§ 7.2.5) | The operands | ||
START |
FloatDivSqrtStart<FMT> |
OP, the significands, exponents and flags of A and B |
A_NORMALIZED, B_NORMALIZED (P), T (E + 3), RADICAND (2P + 4), SPECIALS (7) |
What is decided at START: two LeadingZeroCounters and NormalizeShifters, the result's T for divide or root, the radicand, and the special cases (NaN, invalid, ∞, zero, divide by zero, the result's sign and a zero's sign) |
RECURRENCE |
FloatDivSqrtStep<FMT> |
OP, R, Q, M, DIVISOR |
R_NEXT, Q_NEXT, M_NEXT |
One step of the table above, both operations side by side, OP choosing |
LOAD |
FloatDivSqrtLoad<FMT> |
ACCEPT, OP, the step's outputs, A_NORMALIZED, RADICAND, COUNT |
the registers' next values | At START the loads (R = A's significand or 0, Q = 0, M = the radicand, the count 0), otherwise the step and the count plus one |
CONTROL |
FloatDivSqrtControl<FMT> |
COUNT, BUSY, DONE, ACCEPT, TAKE |
BUSY_D, DONE_D |
Busy for P + 2 steps; DONE until TAKE |
MODE, REMAINDER_ZERO, NORMALIZE, ROUND_BITS, RESULT |
shared | The result from the held state (a non-zero remainder is lost bits) |
Three loose gates take a START while idle (ACCEPT) and enable the
registers while loading or stepping.
The compact form is the unit's compiled gate list.
Examples¶
1/3 in two rounding modes, √2, 1/0 (divide by zero) and √−1 (invalid);
the steps between START and DONE are run but not shown.
All 10 recorded rows of FloatDivSqrtFp16ScenarioTest, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.
Default scenario profile: fidelity-gate-v1 / structural (ed03f70afe360dba). Changing fidelity or the circuit can change these times.
| Checkpointzero-based number · timeline label | Timesimulator units | AInput · 16 bit |
BInput · 16 bit |
CLKInput · 1 bit |
OPInput · 1 bit |
RMInput · 3 bit |
RSTInput · 1 bit |
STARTInput · 1 bit |
TAKEInput · 1 bit |
BUSYExpected output · 1 bit |
DONEExpected output · 1 bit |
FLAGSExpected output · 5 bit |
YExpected output · 16 bit |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
#01-div-3-start | 112 | 0x3C00 |
0x4200 |
1 |
0 |
0x0 |
0 |
1 |
0 |
1 |
0 |
not checked | not checked |
#11-div-3-done | 723 | 0x3C00 |
0x4200 |
1 |
0 |
0x0 |
0 |
0 |
0 |
0 |
1 |
0x01 |
0x3555 |
#21-div-3-rup-start | 791 | 0x3C00 |
0x4200 |
1 |
0 |
0x3 |
0 |
1 |
0 |
1 |
0 |
not checked | not checked |
#31-div-3-rup-done | 1403 | 0x3C00 |
0x4200 |
1 |
0 |
0x3 |
0 |
0 |
0 |
0 |
1 |
0x01 |
0x3556 |
#4sqrt-2-start | 1495 | 0x4000 |
0x0000 |
1 |
1 |
0x0 |
0 |
1 |
0 |
1 |
0 |
not checked | not checked |
#5sqrt-2-done | 2108 | 0x4000 |
0x0000 |
1 |
1 |
0x0 |
0 |
0 |
0 |
0 |
1 |
0x01 |
0x3DA8 |
#61-div-0-start | 2202 | 0x3C00 |
0x0000 |
1 |
0 |
0x0 |
0 |
1 |
0 |
1 |
0 |
not checked | not checked |
#71-div-0-done | 2858 | 0x3C00 |
0x0000 |
1 |
0 |
0x0 |
0 |
0 |
0 |
0 |
1 |
0x08 |
0x7C00 |
#8sqrt-neg1-start | 2931 | 0xBC00 |
0x0000 |
1 |
1 |
0x0 |
0 |
1 |
0 |
1 |
0 |
not checked | not checked |
#9sqrt-neg1-done | 3531 | 0xBC00 |
0x0000 |
1 |
1 |
0x0 |
0 |
0 |
0 |
0 |
1 |
0x10 |
0x7E00 |
Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.
All 10 recorded rows of FloatDivSqrtFp32ScenarioTest, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.
Default scenario profile: fidelity-gate-v1 / structural (ed03f70afe360dba). Changing fidelity or the circuit can change these times.
| Checkpointzero-based number · timeline label | Timesimulator units | AInput · 32 bit |
BInput · 32 bit |
CLKInput · 1 bit |
OPInput · 1 bit |
RMInput · 3 bit |
RSTInput · 1 bit |
STARTInput · 1 bit |
TAKEInput · 1 bit |
BUSYExpected output · 1 bit |
DONEExpected output · 1 bit |
FLAGSExpected output · 5 bit |
YExpected output · 32 bit |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
#01-div-3-start | 123 | 0x3F800000 |
0x40400000 |
1 |
0 |
0x0 |
0 |
1 |
0 |
1 |
0 |
not checked | not checked |
#11-div-3-done | 1436 | 0x3F800000 |
0x40400000 |
1 |
0 |
0x0 |
0 |
0 |
0 |
0 |
1 |
0x01 |
0x3EAAAAAB |
#21-div-3-rup-start | 1506 | 0x3F800000 |
0x40400000 |
1 |
0 |
0x3 |
0 |
1 |
0 |
1 |
0 |
not checked | not checked |
#31-div-3-rup-done | 2822 | 0x3F800000 |
0x40400000 |
1 |
0 |
0x3 |
0 |
0 |
0 |
0 |
1 |
0x01 |
0x3EAAAAAB |
#4sqrt-2-start | 2915 | 0x40000000 |
0x00000000 |
1 |
1 |
0x0 |
0 |
1 |
0 |
1 |
0 |
not checked | not checked |
#5sqrt-2-done | 4246 | 0x40000000 |
0x00000000 |
1 |
1 |
0x0 |
0 |
0 |
0 |
0 |
1 |
0x01 |
0x3FB504F3 |
#61-div-0-start | 4335 | 0x3F800000 |
0x00000000 |
1 |
0 |
0x0 |
0 |
1 |
0 |
1 |
0 |
not checked | not checked |
#71-div-0-done | 5791 | 0x3F800000 |
0x00000000 |
1 |
0 |
0x0 |
0 |
0 |
0 |
0 |
1 |
0x08 |
0x7F800000 |
#8sqrt-neg1-start | 5866 | 0xBF800000 |
0x00000000 |
1 |
1 |
0x0 |
0 |
1 |
0 |
1 |
0 |
not checked | not checked |
#9sqrt-neg1-done | 7169 | 0xBF800000 |
0x00000000 |
1 |
1 |
0x0 |
0 |
0 |
0 |
0 |
1 |
0x10 |
0x7FC00000 |
Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.
Source and tests¶
| Test | Coverage |
|---|---|
FloatDivSqrtTest |
FP16 and FP32, gates and compact form: 120 operations each (every special value, then random operands; a third of them square roots), each checked for DONE at exactly P + 3 edges, Y and FLAGS against the reference, and TAKE clearing DONE |
FloatDivSqrt…ScenarioTest |
The recorded rows above |