Skip to content

§ 7.5. Comparisons and Conversions

The remaining operations a processor needs: comparing two floats, sorting out what kind of value a float is, and moving numbers between floats and integers and between the two formats.

Component Description Structural Behavioral
FloatCompare<FMT> Equal, less, less-or-equal, min, max Yes Yes (compiled gate list)
FloatClassify<FMT> RISC-V's ten value classes Yes Yes (compiled gate list)
FloatToInt<FMT> Float → signed or unsigned 32-bit integer, rounded Yes Yes (compiled gate list)
IntToFloat<FMT> Signed or unsigned 32-bit integer → float, rounded Yes Yes (compiled gate list)
FloatConvert<FROM,TO> FP16 → FP32 (exact) and FP32 → FP16 (rounded) Yes Yes (compiled gate list)

Catalog: float.compare.*, float.classify.*, float.to-int.*, float.from-int.* (FP16 and FP32), float.convert.fp16-to-fp32, float.convert.fp32-to-fp16.

7.5.1. FloatCompare<FMT>

IO pins

Pin Direction Width Meaning
A, B Input 16 or 32 each Operands
EQ, LT, LE Output 1 each A = B, A < B, A ≤ B (all 0 when either is a NaN)
MIN, MAX Output 16 or 32 each RISC-V FMIN/FMAX
UNORDERED Output 1 Either operand is a NaN
SIGNALLING Output 1 Either operand is a signalling NaN

Behavior

+0 and −0 are equal. MIN and MAX return the other operand when one is a NaN, the canonical NaN when both are, and treat −0 as below +0. The unit raises no flags itself; RISC-V's rules are on the outputs: invalid on SIGNALLING for FEQ, FMIN, FMAX, on UNORDERED for FLT and FLE (the CPU's execute unit, 7.7, applies them).

Implementation

One subtractor compares the magnitudes (exponent and fraction as one integer); the signs decide the rest: a negative value is below a positive one (unless both are zeros), two positives order as their magnitudes, two negatives the other way. Multiplexers pick MIN and MAX and the NaN cases. The compact form is its compiled gate list.

Part Type Inputs Outputs What it does
UNPACK_A, UNPACK_B shared (§ 7.2.5) The operands
MAGNITUDE FloatCompareMagnitude<FMT> A_MAGNITUDE, B_MAGNITUDE (E + F) BELOW, EQUAL, ABOVE One subtractor and a zero test
RELATION FloatCompareRelation the signs, ZERO, NAN, BELOW, EQUAL, ABOVE EQ, LT, LE, UNORDERED, A_FIRST The signs' rules above; A_FIRST: A is the smaller (−0 before +0)
SELECT FloatCompareSelect<FMT> A, B, A_FIRST, A_NAN, B_NAN MIN, MAX Word selects for the order and the NaN cases

SIGNALLING is one loose OR of the operands' signalling bits.

Examples

All 10 recorded rows of FloatCompareFp16ScenarioTest, 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 EQExpected output · 1 bit LEExpected output · 1 bit LTExpected output · 1 bit MAXExpected output · 16 bit MINExpected output · 16 bit SIGNALLINGExpected output · 1 bit UNORDEREDExpected output · 1 bit
#01-vs-223 0x3C00 0x4000 0 1 1 0x4000 0x3C00 0 0
#12-vs-146 0x4000 0x3C00 0 0 0 0x4000 0x3C00 0 0
#2neg2-vs-160 0xC000 0x3C00 0 1 1 0x3C00 0xC000 0 0
#3neg1-vs-neg283 0xBC00 0xC000 0 0 0 0xBC00 0xC000 0 0
#4equal107 0x3C00 0x3C00 1 1 0 0x3C00 0x3C00 0 0
#5pos0-vs-neg0123 0x0000 0x8000 1 1 0 0x0000 0x8000 0 0
#6qnan-vs-1136 0x7E00 0x3C00 0 0 0 0x3C00 0x3C00 0 1
#7snan-vs-1149 0x7C01 0x3C00 0 0 0 0x3C00 0x3C00 1 1
#8two-nans168 0x7E00 0x7E00 0 0 0 0x7E00 0x7E00 0 1
#9inf-vs-largest192 0x7C00 0x7BFF 0 0 0 0x7C00 0x7BFF 0 0

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.

Open the full visualizer FloatCompare<FP32>

7.5.2. FloatClassify<FMT>

IO pins

Pin Direction Width Meaning
A Input 16 or 32 Operand
CLASS Output 10 One bit set: 0 −∞, 1 −normal, 2 −subnormal, 3 −0, 4 +0, 5 +subnormal, 6 +normal, 7 +∞, 8 signalling NaN, 9 quiet NaN

Implementation

The shared UNPACK step (§ 7.2.5) and ten AND gates on its outputs. Compact: its gate list.

Examples

All 10 recorded rows of FloatClassifyFp16ScenarioTest, 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 CLASSExpected output · 10 bit
#0class-07 0xFC00 0x001
#1class-112 0xBC00 0x002
#2class-222 0x8001 0x004
#3class-332 0x8000 0x008
#4class-435 0x0000 0x010
#5class-545 0x0001 0x020
#6class-652 0x3C00 0x040
#7class-757 0x7C00 0x080
#8class-865 0x7C01 0x100
#9class-970 0x7E00 0x200

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.

Open FloatClassify<FP16> FloatClassify<FP32>

7.5.3. FloatToInt<FMT>

IO pins

Pin Direction Width Meaning
A Input 16 or 32 Operand
RM Input 3 Rounding mode
SIGNED Input 1 1: to int32 (FCVT.W.S), 0: to uint32 (FCVT.WU.S)
Y Output 32 The integer
FLAGS Output 5 NX when rounded; NV when out of range or NaN

Behavior

Rounded to an integer by RM. Out of range (and NaN) gives the largest value (NaN and positive) or the smallest (negative) with invalid only. For unsigned results, a negative value that rounds to 0 gives 0 with inexact, one that rounds to −1 or below is invalid.

Implementation

The significand is placed 34 places up a window and shifted right by F + 33 − (e − bias): bit 0 is then the half place (GUARD), bits 1–33 the integer, the shifted-out bits the sticky. A Rounder<33> rounds, a few gates check the range (signed: below 2^31, or exactly 2^31 when negative; unsigned: below 2^32 when positive, 0 when negative), and an adder negates a negative result. A shift below zero means 2^34 or more: out of range.

Part Type Inputs Outputs What it does
UNPACK shared (§ 7.2.5) The operand
WINDOW FloatToIntWindow<FMT> SIGNIFICAND (P), EXPONENT (E) SHIFTED (P + 34), STICKY, TOO_LARGE, ABOVE The window, its shift (an AlignShifter<P+34>), a negative shift and bits left above the 33 kept
ROUND Rounder<33> Rounds the 33-bit integer part (7.2.4)
RANGE FloatToIntRange MAGNITUDE (34), SIGN, SIGNED, NAN, INFINITY, TOO_LARGE, ABOVE VALID The range check above
VALUE FloatToIntValue MAGNITUDE (32), SIGN, NAN, SIGNED, VALID Y (32) The negation and the saturated limit when not valid

FLAGS comes from two loose gates: NX = valid and inexact, NV = not valid.

Examples

All 11 recorded rows of FloatToIntFp32ScenarioTest, 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 RMInput · 3 bit SIGNEDInput · 1 bit FLAGSExpected output · 5 bit YExpected output · 32 bit
#02.5-rne35 0x40200000 0x0 1 0x01 0x00000002
#12.5-rmm47 0x40200000 0x4 1 0x01 0x00000003
#2neg2.5-rtz61 0xC0200000 0x1 1 0x01 0xFFFFFFFE
#3neg2.5-rdn76 0xC0200000 0x2 1 0x01 0xFFFFFFFD
#40.75-rup111 0x3F400000 0x3 1 0x01 0x00000001
#5neg0.25-unsigned-rtz138 0xBE800000 0x1 0 0x01 0x00000000
#6neg1-unsigned172 0xBF800000 0x0 0 0x10 0x00000000
#7inf-signed198 0x7F800000 0x0 1 0x10 0x7FFFFFFF
#8nan-unsigned214 0x7FC00000 0x0 0 0x10 0xFFFFFFFF
#9big-signed253 0x4F32D05E 0x0 1 0x10 0x7FFFFFFF
#10big-unsigned262 0x4F32D05E 0x0 0 0x00 0xB2D05E00

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.

Open the full visualizer FloatToInt<FP16>

7.5.4. IntToFloat<FMT>

IO pins

Pin Direction Width Meaning
A Input 32 The integer
RM Input 3 Rounding mode
SIGNED Input 1 Read A as int32 (FCVT.S.W) or uint32 (FCVT.S.WU)
Y Output 16 or 32 The float
FLAGS Output 5 NX when rounded; OF (with NX) when too large for FP16

Implementation

|A| (an adder negates a negative signed value), then the multiplier's normalize-and-round path: its top bit has exponent 31, the leading zeros shift it up, the rounder keeps P bits. Every int32 fits FP32's range; FP16's largest finite value is 65,504, so larger integers overflow there.

Parts: MODE, then MAGNITUDE (IntToFloatMagnitude: A, SIGNED → MAGNITUDE (32), NEGATIVE, ZERO: the conditional negation and a zero test), then the shared NORMALIZE (with T = bias + 30), ROUND_BITS and RESULT (§ 7.2.5).

Examples

All 9 recorded rows of IntToFloatFp32ScenarioTest, 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 RMInput · 3 bit SIGNEDInput · 1 bit FLAGSExpected output · 5 bit YExpected output · 32 bit
#0754 0x00000007 0x0 1 0x00 0x40E00000
#1neg7104 0xFFFFFFF9 0x0 1 0x00 0xC0E00000
#2neg7-unsigned155 0xFFFFFFF9 0x0 0 0x01 0x4F800000
#316777217-rne203 0x01000001 0x0 1 0x01 0x4B800000
#416777217-rup219 0x01000001 0x3 1 0x01 0x4B800001
#52049-rne267 0x00000801 0x0 1 0x00 0x45001000
#665520-rne308 0x0000FFF0 0x0 1 0x00 0x477FF000
#765520-rtz315 0x0000FFF0 0x1 1 0x00 0x477FF000
#80342 0x00000000 0x0 1 0x00 0x00000000

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.

Open IntToFloat<FP32> IntToFloat<FP16>

7.5.5. FloatConvert<FROM,TO>

IO pins

Pin Direction Width Meaning
A Input FROM's width Operand
RM Input 3 Rounding mode (FP32 → FP16)
Y Output TO's width Result
FLAGS Output 5 FP32 → FP16: NX, UF, OF; NV for a signalling NaN

Behavior

FP16 → FP32 is always exact (every FP16 value, subnormals included, is a normal FP32 value). FP32 → FP16 rounds, overflows past 65,504, and underflows below 2^−14 (tininess after rounding).

Implementation

The source significand at the top of a window at least P + 2 bits wide, then the shared normalize-and-round path with the target's exponent range: FP16's subnormals normalize up in FP32; values far below FP16's range shift right with a sticky bit.

Parts: MODE, UNPACK, then EXPONENT (FloatConvertExponent<FROM,TO>: EXPONENT → T, the source exponent in the target's bias, in the wider format's range so an exponent beyond the target's still reads as an overflow), then the shared NORMALIZE, ROUND_BITS and RESULT (§ 7.2.5); ∞ is one loose AND with "not NaN".

Examples

All 11 recorded rows of FloatConvertNarrowScenarioTest, 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 RMInput · 3 bit FLAGSExpected output · 5 bit YExpected output · 16 bit
#01.554 0x3FC00000 0x0 0x00 0x3E00
#10.1-rne108 0x3DCCCCCD 0x0 0x01 0x2E66
#20.1-rtz116 0x3DCCCCCD 0x1 0x01 0x2E66
#365519-rne173 0x477FEF00 0x0 0x01 0x7BFF
#465520-rne204 0x477FF000 0x0 0x05 0x7C00
#565520-rtz231 0x477FF000 0x1 0x01 0x7BFF
#6to-subnormal272 0x35800000 0x0 0x00 0x0010
#7underflow309 0x322BCC77 0x0 0x03 0x0000
#8tiny-rup334 0x0DA24260 0x3 0x03 0x0001
#9inf397 0x7F800000 0x0 0x00 0x7C00
#10snan417 0x7F800001 0x0 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.

Open FloatConvert<FP32,FP16>

All 7 recorded rows of FloatConvertWidenScenarioTest, 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 RMInput · 3 bit FLAGSExpected output · 5 bit YExpected output · 32 bit
#01.559 0x3E00 0x0 0x00 0x3FC00000
#1largest113 0x7BFF 0x0 0x00 0x477FE000
#2smallest-subnormal173 0x0001 0x0 0x00 0x33800000
#3subnormal214 0x0030 0x0 0x00 0x36400000
#4neg0239 0x8000 0x0 0x00 0x80000000
#5inf291 0x7C00 0x0 0x00 0x7F800000
#6snan313 0x7C01 0x0 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.

Open FloatConvert<FP16,FP32>

Source and tests

Units, tests.

Test Coverage
FloatCompareTest FP16 and FP32: every pair of special values, random pairs and near-equal pairs; all seven outputs
FloatClassifyTest Special and random values
FloatToIntTest Special values in five modes, signed and unsigned; random values around 2^−7 … 2^32 in all eight RM codes; the limits ±2^31, 2^32 and their neighbours
IntToFloatTest Edge integers (0, ±1, the extremes, FP16's overflow boundary, ties) in five modes; random integers of every size
FloatConvertTest FP16 → FP32: every subnormal, special and the low values, every seventh other value; FP32 → FP16: special values in five modes and random values over FP16's range and its edges
…ScenarioTest The recorded rows above