Float representation design

For binary64, bit 63 is sign, bits 62–52 exponent and bits 51–0 fraction; bit 51 is the quiet bit. Binary32 uses sign bit 31, exponent bits 30–23, fraction bits 22–0 and quiet bit 22. An all-one exponent means infinity when the fraction is zero and NaN otherwise. An all-zero exponent means zero when the fraction is zero and subnormal otherwise; all remaining encodings are normal.

Raw operations use fixed-width integer masks. Classification cases are exhaustive and disjoint by the exponent/fraction partition. Sign operations preserve the magnitude mask by construction. Payload guards exclude fraction overflow and the signaling-zero infinity encoding. Every operation takes O(1) time and space with no loops or numeric rounding; termination follows from finite expressions.

A raw integer encoding preserves NaN sign, signaling state and payload exactly. A numeric value does not preserve that information portably, especially on JavaScript. Numeric NaN is detected by x != x; portable_classify returns QuietNaN without inspecting its sign or payload. portable_is_signaling is always false, while binary interchange is_canonical_bits and portable_is_canonical are always true. Keep signaling NaNs as integers when their representation matters.

IEEE 754-2019 §§5.7.2, 5.5.1 and 9.7 motivate classification, sign and payload operations; §6.2 does not require identical NaN payload propagation across hosts. This package makes its canonical encoding policy explicit. It does not raise IEEE exception flags or preserve numeric signaling NaNs. Current MoonBit core 0.10.14+7d59c7ec9 and official language documentation provide implementation evidence, not a formal verification claim.

Round-to-integral operations implement IEEE 754-2019 §5.9 from the same integer fields. For a finite non-integral value, discarded fraction bits determine the nearest tie rule or directed step; values below one use the signed zero encoding or the adjacent signed one encoding for directed rounding. NaNs are quieted to positive canonical NaN, dropping the input sign and payload. The API does not report the invalid exception for sNaN input or inexact for non-integral input; both are deferred to the _exact variant and status-flag work in #29. Infinities pass through unchanged.

Integer conversions first apply round_to_integral and then compare the rounded Double with the half-open interval of the target before calling a core primitive, since core’s to_int family is backend-specific outside its range. Comparing after rounding is the contract: -0.5 truncates into the UInt range. UInt64 values at or above 2^63 are converted by subtracting 2^63, converting the remainder as Int64 and setting the top bit, because no single core primitive covers that range on every backend. int64_to_float and uint64_to_float take the magnitude, keep its top 53 bits and fold all lower bits into the last kept bit (round to odd), and then narrow that exactly representable intermediate to binary32 with a single ties-to-even rounding. Because a binary64 significand has at least two more bits than binary32, the round-to-odd intermediate preserves every rounding decision, so the result equals a direct rounding of the integer.

IEEE 754-2019 §9.6 extrema first handle NaN, then compare ordinary values, with explicit bit selection for two zeros: minimum combines the sign bits with OR to choose -0, and maximum combines them with AND to choose +0. A NaN result uses this package’s positive canonical quiet encoding; exception flags are not exposed here.

IEEE 754-2019 §5.10 totalOrder is an integer ordering over the encoding, not ordinary floating comparison. For a width of w bits and sign mask S, the signed key is bits XOR (S - 1) for negative encodings and bits otherwise. Comparing these keys as signed integers reverses all lower bits for negative values and orders positive values directly. The raw-bit API applies this key without materializing a float, so it retains negative NaN, sNaN and payload ordering on every backend. The portable numeric API first canonicalizes NaNs; it therefore provides deterministic ordering for numeric values but cannot reconstruct encoding details lost by host storage.

Sorting uses the same key comparator through core’s in-place sort_by, for both arrays and mutable views. The comparator returns -1, 0 or 1 from signed key comparisons instead of subtracting keys, avoiding integer overflow.

The comparison predicates of IEEE 754-2019 §5.6.1 and §5.11 are one table: a predicate is a set of the four relations plus a flag saying whether quiet NaN operands also signal. Evaluating a predicate is therefore a mask test on the relation, which keeps the 22 names from becoming 22 hand-written functions that could disagree. The relation of two encodings uses the totalOrder key except that two zeros are equal, which gives -0 = +0, equal same-sign infinities and unordered NaNs without floating-point arithmetic. The result is a pair (value, invalid) because the package has no flags yet; once exception flags exist (issue #29) the second component becomes a flag and the pair stays as a convenience.