IntegerCorrectOrDefer

Lean checked

Statement

This explanation was written alongside the formalization to describe the statement and its reasoning.

Theorem

For every signed 64-bit exponent q, unsigned 64-bit mantissa w, and normal result of compute_float, either the raw pair has a negative biased exponent (deferral), or its unrestricted assembled word is the nonnegative binary64 rounding of the exact real value w10^q.

Lean statement

Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.

import Tablet.DecimalGoalVocabularyDefault
import Tablet.Assumptions
import Tablet.F64
import Tablet.ModelF64MantissaDigits
import Tablet.ModelU64BitAnd
import Tablet.ModelU64BitOr
import Tablet.ModelU64Shl
import Tablet.IntegerComputeFloatControlFlow
import Tablet.IntegerCoreRounding
import Tablet.IntegerEndpointRounding
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_BITS
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_LARGEST_POWER_OF_TEN
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_MAX_EXPONENT_ROUND_TO_EVEN
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_MIN_EXPONENT_ROUND_TO_EVEN
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SIG_TOTAL_BITS
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SMALLEST_POWER_OF_TEN
import Tablet.num_imp_dec2flt_common_BiasedFp
import Tablet.num_imp_dec2flt_common_BiasedFp_zero_pow2
import Tablet.num_imp_dec2flt_lemire_compute_float
import Tablet.num_imp_dec2flt_lemire_compute_product_approx
import Tablet.num_imp_dec2flt_lemire_full_multiplication
import Tablet.num_imp_dec2flt_lemire_power
import Tablet.num_imp_dec2flt_table_LARGEST_POWER_OF_FIVE
import Tablet.num_imp_dec2flt_table_POWER_OF_FIVE_128
import Tablet.num_imp_dec2flt_table_SMALLEST_POWER_OF_FIVE
import Tablet.num_imp_traits_Float
import Tablet.num_imp_traits_Float_EXP_BIAS_default
import Tablet.num_imp_traits_Float_EXP_BITS_default
import Tablet.num_imp_traits_Float_EXP_MIN_default
import Tablet.num_imp_traits_Float_EXP_SAT_default
import Tablet.num_imp_traits_Float_INFINITE_POWER_default
import Tablet.num_imp_traits_Float_SIG_BITS_default

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: IntegerCorrectOrDefer]
theorem IntegerCorrectOrDefer :
    ∀ (q : Std.I64) (w : Std.U64) fp,
      num.imp.dec2flt.lemire.compute_float
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 q w = ok fp ->
      fp.p_biased.val < 0 ∨ ∃ target : F64,
        DecimalGoalVocabularyDefault.assembledWord fp target ∧
        DecimalGoalVocabularyDefault.roundsMagnitude ((w.val : Real) * (10 : Real) ^ q.val) target := by
Proof

Natural-language proof

The status above describes the Lean proof. Its explanatory prose may remain a sketch after that proof closes.

Explanatory prose status

Prose retained as authored. The explanatory prose is preserved as it stood when captured.

Proof

Fix q,w, and fp, and suppose that the call displayed in the Lean statement is ok fp. Put B=2^{64} and U=2^{52}. We use integer letters for the mathematical values of fixed-width scalars. The platform equality in Assumptions makes usize a 64-bit unsigned type; it is used only for the table index. All other scalar widths below are fixed independently of that assumption.

The selected constants and the binary64 target relation.

The selected record F64 Insts Dec2flt modelNumImpTraitsFloatU64 instantiates the fields of num imp traits Float. Its BITS field is 64 by F64 Insts Dec2flt modelNumImpTraitsFloatU64 BITS. Its SIG_TOTAL_BITS field invokes ModelF64MantissaDigits and is 53 by F64 Insts Dec2flt modelNumImpTraitsFloatU64 SIG TOTAL BITS. Consequently num imp traits Float SIG BITS default performs the checked subtraction 53-1 and returns 52. Next num imp traits Float EXP BITS default computes 64-52-1=11; num imp traits Float EXP SAT default legally shifts 1 by 11 and subtracts one, giving 2047; num imp traits Float INFINITE POWER default casts that in-range value to i32; and num imp traits Float EXP BIAS default shifts it right once, giving 1023. Finally num imp traits Float EXP MIN default casts 1023 exactly, subtracts one, and negates, giving -1022. Thus every bind in this constant chain succeeds and
\texttt{SIG_BITS}=52,\qquad \texttt{INFINITE_POWER}=2047, \qquad \texttt{EXP_MIN}=-1022. \tag{1}
The remaining four constants read by the converter are, literally, -342,308,-4,23 by F64 Insts Dec2flt modelNumImpTraitsFloatU64 SMALLEST POWER OF TEN, F64 Insts Dec2flt modelNumImpTraitsFloatU64 LARGEST POWER OF TEN, F64 Insts Dec2flt modelNumImpTraitsFloatU64 MIN EXPONENT ROUND TO EVEN, and F64 Insts Dec2flt modelNumImpTraitsFloatU64 MAX EXPONENT ROUND TO EVEN.
Unfold DecimalGoalVocabularyDefault. Its positive finite word eU+m, with 0\le e\le2046 and 0\le m<U, has value
m2^{-1074}\quad(e=0),\qquad (U+m)2^{e-1075}\quad(e>0), \tag{2}
and word 2047U is positive infinity. These are all the nonnegative binary64 candidates because F64 is the complete 64-bit-word carrier. Adjacent subnormals are separated by 2^{-1074}; adjacent normal words of exponent e are separated by 2^{e-1075}, with the same value at a carry to the next exponent. Hence rounding an exact nonnegative number to the nearer adjacent words and choosing the even raw low bit at equality implies the quantified finite-competitor clause of roundsMagnitude: every other finite word is farther along this strictly increasing list. At the top, the maximum finite value is 2^{1024}-2^{971}, so its midpoint with the next spacing endpoint is
H=2^{1024}-2^{970}. \tag{3}
The vocabulary selects infinity exactly on [H,\infty). It therefore suffices below to prove that the extracted integer operations implement this nearest-even selection, including (3).

The three immediate returns.

By num imp dec2flt common BiasedFp, a pair has fields m and p_{\rm biased}. Each invocation of num imp dec2flt common BiasedFp zero pow2 is unconditionally successful and returns (0,p), so the three eager values in num imp dec2flt lemire compute float are
fp_0=(0,0),\qquad fp_\infty=(0,2047),\qquad fp_{\rm err}=(0,-1). \tag{4}
If w=0, the exact real input is zero. The unrestricted assembly formula in DecimalGoalVocabularyDefault gives word zero for fp_0, and (2) shows directly that positive zero is its nearest-even rounding.
Suppose w>0. If q<-342, then q\le-343 and
0<w10^q\le(B-1)10^{-343}<2^{-1075}; \tag{5}
the last strict inequality is the integer inequality (B-1)2^{1075}<10^{343}. Thus the value is strictly below half of the smallest subnormal and fp_0 again assembles to the nearest-even word zero. If q>308, then w10^q\ge10^{309}>2^{1024}>H; the unrestricted assembly of fp_\infty is 2047U, so (3) gives the required infinity result. We may henceforth assume
w>0\quad\hbox{and}\quad -342\le q\le308. \tag{6}
The casts of both endpoint constants to i64 are exact, so these comparisons and all monadic predecessors of (6) have succeeded.

A reproducible certificate for every table entry.

We now give the finite integer certificate used below; it is stated as a recurrence rather than as an appeal to comments or visual inspection of the 651 literals. Define P_0=1 and P_{n+1}=5P_n. For n>0, let z_n be the unique integer satisfying
2^{z_n-1}<P_n<2^{z_n}. \tag{7}
Strictness holds because an odd power of five is not a power of two. Define the following normalized 128-bit integers:
D_q=\begin{cases} \left\lfloor \dfrac{\left\lfloor2^{2z_{-q}+128}/P_{-q}\right\rfloor+1} {2^{z_{-q}+1}} \right\rfloor,&-342\le q<-27,\\[6pt] \left\lfloor2^{z_{-q}+127}/P_{-q}\right\rfloor+1, &-27\le q<0,\\[6pt] P_q2^{127-(P_q.\mathit{bitLength}-1)}, &q\ge0\text{ and }P_q<2^{128},\\[4pt] \left\lfloor P_q/2^{(P_q.\mathit{bitLength}-1)-127}\right\rfloor, &q\ge0\text{ and }P_q\ge2^{128}. \end{cases} \tag{8}
Here bitLength is computed from (7), by incrementing z exactly when multiplication by five crosses the next power of two. Thus (7)--(8) use only multiplication by five, comparison, shifts, and Euclidean division of nonnegative integers.
Write the literal at array index q+342 in num imp dec2flt table POWER OF FIVE 128 as (a_q,b_q). The certificate is
D_q=a_qB+b_q\qquad(-342\le q\le308). \tag{9}
For completeness, (9) is checked uniformly as follows. Starting with P_0=1, make two passes, multiplying the current integer by five on each step, through P_1,\ldots,P_{342} and P_1,\ldots,P_{308}. At each negative index compute the first or second line of (8), and at each nonnegative index repeatedly double or halve the current P_q until it lies in [2^{127},2^{128}). Divide the result by B and take its remainder modulo B; those two integers are the displayed decimal limbs at index q+342. The alignment assertions are
\begin{array}{c|c} q& (a_q,b_q)\\ \hline -342&(17218479456385750618,1242899115359157055)\\ 0&(9223372036854775808,0)\\ 308&(10261342003245940623,6273243709394548296), \end{array}
and the two passes perform respectively 342 and 308 applications of the single recurrence P\leftarrow5P. Therefore every one of the 651 equality checks in (9) reduces directly to a pinned pair; there is no omitted floating-point logarithm or unrecorded table-generation convention.
Let
e_q=\begin{cases}P_q.\mathit{bitLength}-1,&q\ge0,\\ -z_{-q},&q<0, \end{cases} \qquad T_q=2^{127-e_q}5^q. \tag{10}
We spell out the direction of every case in (8), since it will matter at a cell boundary. If q=-n<-27, put K=2^{z_n+1} and write N=2^{z_n+127}=kP_n+r. Since P_n is odd and greater than one whereas N is a power of two, P_n\nmid N, so 1\le r\le P_n-1. The numerator inside the outer floor is
\left\lfloor NK/P_n\right\rfloor+1 =Kk+\left\lfloor Kr/P_n\right\rfloor+1.
Here (7) says 2^{z_n-1}<P_n<2^{z_n}=K/2, and therefore K/P_n>2. Positivity gives 0\le\lfloor Kr/P_n\rfloor, while r\le P_n-1 gives
\frac{Kr}{P_n}\le \frac{K(P_n-1)}{P_n} =K-\frac K{P_n}<K-2.
Thus 0\le\lfloor Kr/P_n\rfloor and \lfloor Kr/P_n\rfloor+1<K. Dividing the displayed numerator first by K consequently gives
\left\lfloor \frac{Kk+\lfloor Kr/P_n\rfloor+1}{K} \right\rfloor=k,
which proves that the first case of (8) is exactly k=\lfloor N/P_n\rfloor=\lfloor T_q\rfloor. Since T_q=N/P_n and 0<r<P_n, this also gives D_q=k<T_q<k+1=D_q+1, the q<-27 direction of (11). If -27\le q=-n<0, oddness of P_n>1 shows P_n\nmid N, so the second case is \lfloor N/P_n\rfloor+1=\lceil N/P_n\rceil. Finally, for q\ge0 the last two cases are respectively the exact integer P_q2^{127-e_q} when e_q\le127, and its floor when e_q\ge128. Consequently (7)--(9), after cross multiplication by the positive denominator, give the exact directed error intervals
\begin{array}{ll} D_q\le T_q<D_q+1,&q<-27\text{ or }q\ge0,\\ D_q-1<T_q<D_q,&-27\le q<0. \end{array} \tag{11}
The same recurrence supplies the fixed-point exponent certificate
65536(q+e_q)\le217706q<65536(q+e_q+1) \tag{12}
at each of the 651 indices. The test is again wholly integral: use (7) for e_q, multiply its two neighboring integers by 65536, and compare with 217706q. Thus arithmetic right shift gives
\left\lfloor\frac{217706q}{65536}\right\rfloor+63 =q+e_q+63. \tag{13}
This also proves the value returned by num imp dec2flt lemire power, not just an approximation to it. Indeed, its checked constant addition is 152170+65536=217706; over (6) the product lies in [-74455452,67053448], so the extracted wrapping multiplication equals mathematical multiplication; the shift count 16 is legal; and the final addition of 63 is in i32 range.

Normalization and the two-limb product.

Let l be the leading-zero count of w. Since w>0, the definition of leading_zeros gives 0\le l\le63 and
W=w2^l,\qquad 2^{63}\le W<B. \tag{14}
The left shift therefore has an in-range count, discards no set high bit, and returns exactly W; this is the bounded-shift meaning recorded by ModelU64Shl. The cast of 52 to 64-bit usize is exact, and its checked addition by three returns the precision 55. The endpoint nodes num imp dec2flt table SMALLEST POWER OF FIVE and num imp dec2flt table LARGEST POWER OF FIVE are the same -342 and 308 as (6). Hence all three assertions in num imp dec2flt lemire compute product approx hold; the checked difference q-(-342) is in [0,650], its cast to 64-bit usize is exact, and the access to the length-651 array is in bounds.
At precision 55, the mask is the legal logical shift (B-1)\mathbin{\mathtt{>>>}}55=2^9-1=511 by the unfolded unsigned-scalar right-shift operation. In num imp dec2flt lemire full multiplication, widening two words to 128 bits is exact, their product is below 2^{128}, the narrowing result is the low limb modulo B, shifting the 128-bit product by 64 is legal, and the final cast is its high limb. Consequently, if D_q=a_qB+b_q, the first multiplication returns the limbs of Wa_q. If its high limb conjoined with 511 is not 511, the product routine returns those limbs. Conjunction is total and bit-exact by ModelU64BitAnd. Otherwise the second multiplication obtains
c_q=\left\lfloor Wb_q/B\right\rfloor,
the wrapping addition forms the low limb of Wa_q+c_q, and the comparison detects exactly its carry. The checked high-limb increment cannot overflow: the pre-increment high limb is at most a_q-1<B-1. Thus every operation in the product routine succeeds. In the refining branch its returned limbs (lo,hi) satisfy
hiB+lo=Wa_q+\left\lfloor Wb_q/B\right\rfloor =\left\lfloor WD_q/B\right\rfloor. \tag{15}
In the nonrefining branch they satisfy hiB+lo=Wa_q. This proves the exact meaning of the returned pair directly from num imp dec2flt lemire compute product approx and num imp dec2flt lemire full multiplication; no source comment is being used as a lemma.
For reference, put
R=WT_q/B,\qquad u=\lfloor R/2^{127}\rfloor\in\{0,1\}, \qquad s=73+u. \tag{16}
The bounds in (10), (14) give 2^{126}\le R<2^{128}, which proves the stated range of u. Combining (11) and (15) puts the exact R in an interval of width less than two around the refined integer.
We first treat the nonrefining branch without assuming the sign of the table error. Write its returned integer as Y=Wa_q=hiB+lo, with 0\le lo<B. On a floor entry, write T_q=D_q+\varepsilon with 0\le\varepsilon<1. Since D_q=a_qB+b_q and 0\le b_q<B,
R-Y=\frac{W(b_q+\varepsilon)}B, \qquad 0\le R-Y<B. \tag{16a}
Indeed b_q+\varepsilon<B and W<B.
For a ceiling entry, necessarily q=-n with 1\le n\le27. Write
D_q=a_qB+b_q,\qquad N=2^{z_n+127}=kP_n+r, \qquad \delta=D_qP_n-N=P_n-r,
so that D_q=k+1 and 0<\delta<P_n. Moreover P_n\le P_{27}=7450580596923828125<B. We claim b_q>0. Otherwise B\mid D_q, hence B\mid D_qP_n; also B\mid N because (7) gives z_n\ge3 and hence z_n+127\ge130>64. It would follow that B\mid\delta=D_qP_n-N, contradicting 0<\delta<B. Therefore 1\le b_q<B, and the exact ceiling-entry correction is
R-Y=\frac{WN}{BP_n}-Wa_q =\frac{W(b_q-\delta/P_n)}B. \tag{16b}
Since 0<\delta/P_n<1\le b_q<B, the parenthesized factor lies strictly between 0 and B. Together with 0<W<B, this proves 0<R-Y<W<B. Thus (16a)--(16b) give 0\le R-Y<B for every nonrefining entry, with strict positivity on every ceiling entry.
Put d=9+u\in\{9,10\}, so 2^s=2^dB. The nonrefining mask condition says that the low nine bits of hi are not all ones. It follows for either value of d that hi\bmod2^d\le2^d-2. Hence
Y\bmod2^s=(hi\bmod2^d)B+lo \le (2^d-2)B+(B-1)<2^s-B.
Adding the correction R-Y<B can cause at most one carry from the low limb into hi, and the displayed missing high-limb unit shows that this carry cannot cross a 2^s boundary. Consequently Y and R lie in the same half-open 2^s cell and
\left\lfloor R/2^s\right\rfloor =\left\lfloor Y/2^s\right\rfloor =hi\mathbin{\mathtt{>>>}}d. \tag{16c}
Since 2^{127} is itself a 2^s boundary, the same-cell conclusion also gives u=\lfloor R/2^{127}\rfloor=hi\mathbin{\mathtt{>>>}}63. Thus (16c) is exactly both equalities in (17) for the nonrefining branch.
For the refining branch, the direction in (11) must also be used. On a floor entry (D_q\le T_q), R can cross upward from the computed integer's cell only when that integer is one below a multiple of 2^s; because the lower 64 bits are part of those s\ge73 bits, this requires lo=B-1. On a ceiling entry (T_q<D_q), a possible crossing would instead be downward from a computed multiple of 2^s. We now exclude it uniformly in W. Write q=-n, so 1\le n\le27, put
P=P_n,\quad N=2^{z_n+127}=kP+r,\quad D=D_q=k+1, \quad\delta=DP-N=P-r, \tag{17a}
where 0<r<P and 0<\delta<P. For C=\lfloor WD/B\rfloor write the Euclidean division
WD=CB+\rho,\qquad0\le\rho<B. \tag{17b}
The exact difference from the lower boundary is
BP(R-C)=WN-CBP=\rho P-W\delta. \tag{17c}
Suppose now, exactly as required for a downward cell crossing, that 2^s\mid C. The two terms WN and CBP in (17c) are divisible by 2^{z_n+127} and 2^{s+64} respectively. Hence their difference is divisible by
2^{\min(z_n+127,s+64)}\ge2^{130},
because z_n\ge3 and s\ge73. On the other hand n\le27 gives P=5^n<2^{z_n}\le2^{63}, and (17b) gives the strict bound
|\rho P-W\delta|<BP<2^{64+z_n}\le2^{127}.
Thus the integer in (17c), being a multiple of 2^{130} of absolute value less than 2^{127}, is zero. In particular \rho P=W\delta and R=C; this proves the required inequality with equality and rules out every downward crossing for every normalized W. Combining this refined-branch conclusion with the nonrefining same-cell calculation (16c), Euclidean division by 2^s gives, except for the upward sentinel configuration,
u=hi\mathbin{\mathtt{>>>}}63,\qquad G=hi\mathbin{\mathtt{>>>}}(9+u) =\left\lfloor R/2^s\right\rfloor. \tag{17}
For the nonrefining branch, (16a)--(16c) prove directly that its correction cannot leave the current cell. For the refining branch, the error from (11) is less than one before multiplication and less than one after division by B, and (17a)--(17c) handle the opposite directed boundary.
The remaining upward boundary is exactly why the extracted code tests lo=B-1. For q\notin[-27,55] it returns fp_{\rm err}, whose exponent is -1 by (4), and the theorem's first disjunct is proved. For -27\le q<0, D_q is a ceiling entry, so (17a)--(17c) have already excluded an incorrect downward cell and there is no upward table error. For 0\le q\le55, (8) gives D_q=T_q exactly because 5^{55}<2^{128}. Thus (17) also holds on every continuing sentinel branch, with exact half-way equality retained rather than guessed. This proves both why all other sentinel exponents defer and why no value is silently rounded on an unresolved interval.
We record the exact half-way certificate used later. Write uniquely W=2^vh with h odd; (14) gives 0\le v\le63. Such an equality is a rounding midpoint only when G is odd: if G is even, G/2 is already an integer grid significand.
First let q=-n<0. From (10) and (16),
R=\frac{W2^{z_n+63}}{P_n}.
If R=G2^s and G is odd, unique factorization into its power of two and odd part gives, in both directions,
v+z_n+63=s,\qquad h=GP_n. \tag{18a}
Thus v=10+u-z_n\ge0, so z_n\le11. The boundary computations
2^9<5^4=625<2^{10},\qquad 2^{11}<5^5=3125<2^{12}
and monotonicity of 5^n show that this is possible only for n\le4.
Now let q\ge0 and put e=e_q. Here R=WP_q2^{63-e}. Equality with odd G cancels to
v+63-e=s,\qquad hP_q=G, \tag{18b}
where the first equality is an equality of dyadic valuations and remains valid when 63-e<0. Hence v=e+10+u\le63, so e\le53. The boundary computations
2^{53}<5^{23}=11920928955078125<2^{54},\qquad 2^{55}<5^{24}=59604644775390625<2^{56}
and monotonicity give q\le23. Equations (18a)--(18b), including the boundary cases n=5 and q=24, prove
R=G2^s\ \land\ G\text{ odd} \quad\Longrightarrow\quad -4\le q\le23. \tag{18}
We next prove that the extracted predicates recognize exactly the midpoint case in which the lower candidate is even. Let Y=hiB+lo be the integer represented by the pair actually returned by the product routine. In the refining branch Y=C by (15); in the nonrefining branch Y=Wa_q. We claim the exact equivalence
\begin{split} R=G2^s\ \land\ G\bmod4=1 \quad\Longleftrightarrow\quad{}&-4\le q\le23\ \land\ G\bmod4=1\\ &{}\land\ lo\le1\ \land\ (G\ll(9+u))=hi. \tag{19} \end{split}
The shifts are by 9 or 10 and are therefore legal. For the forward direction, (18) supplies the range. If 0\le q\le23, then e_q\le53 and (8) gives
D_q=T_q=P_q2^{127-e_q}=a_qB,\qquad a_q=P_q2^{63-e_q},\quad b_q=0. \tag{19a}
The first multiplication therefore returns the two limbs of Wa_q=R=G2^s, and refinement adds zero. Hence lo=0 and hi=G2^{s-64}=G2^{9+u}.
If q=-n with 1\le n\le4, then D=(N+\delta)/P and exact equality gives
WD/B=R+W\delta/(BP),\qquad0<W\delta/(BP)<1.
Thus C=R=G2^s. Put c=\lfloor Wb_q/B\rfloor, so C=Wa_q+c. If c=0, the initial product already has the limbs of C. If c>0, its low limb is B-c and its high limb is C/B-1; since C/B=G2^{9+u} is divisible by 512, that initial high limb has its low nine bits all one. The routine therefore takes the refining branch and again returns the limbs of C. This proves lo=0\le1 and the reconstruction equality for every forward case.
Conversely, assume the right side of (19), put j=lo\in\{0,1\}, and set L=G2^s. The reconstruction equality says exactly that
Y=L+j. \tag{19b}
For 0\le q\le23, (19a) says Y=R=Wa_q in either product branch. Moreover a_q is divisible by 2^{63-e_q}, which is at least 2^{10}. Thus Y is even, so (19b) forces j=0 and R=L.
It remains to justify this converse for the four negative indices. The following compact certificate comes directly from N=2^{z_n+127}=kP_n+r, D=k+1=aB+b, and ordinary Euclidean division:
\begin{array}{c|r|r|r|r|r|r|r} n&P_n&z_n&r&\delta&a_q&b_q&\nu_2(a_q)\\ \hline 1&5&3&4&1&14757395258967641292&14757395258967641293&2\\ 2&25&5&21&4&11805916207174113034&4427218577690292388&1\\ 3&125&7&59&66&9444732965739290427&7231123676894144234&0\\ 4&625&10&472&153&15111572745182864683&15259146697772541097&0 \end{array} \tag{19c}
The checkable identities for the columns are N\bmod P_n=r, \delta=P_n-r, and D=\lfloor N/P_n\rfloor+1=a_qB+b_q. In a refining branch, Y=C. Let X=WN-CBP=\rho P_n-W\delta as in (17c). From (19b),
Z:=WN-LBP_n=X+jBP_n.
Both WN and LBP_n make 2^{130}\mid Z, while -BP_n<Z<2BP_n<2^{75}. Hence Z=0; strictness of X>-BP_n excludes j=1, and j=0 gives R=L.
In a nonrefining branch, (19b) is Wa_q=L+j. If j=0, oddness of G would give \nu_2(W)+\nu_2(a_q)=s, impossible because (19c) bounds the left side by 63+2<73. If j=1, both factors must be odd, so only n=3,4 remain. Since G\bmod4=1, reduction modulo 2^{s+2} forces
W\equiv x_{n,u}:=a_q^{-1}(2^s+1)\pmod {2^{s+2}}.
Here are the least nonnegative residues and exact quotient certificates:
\begin{array}{c|c|r|r} n&u&x_{n,u}&t\text{ in }a_qx_{n,u}=2^s+1+t2^{s+2}\\ \hline 3&0&36501965416058737238515&9125491354014684309\\ 3&1&27057232450319446811123&3382154056289930851\\ 4&0&35111753888106220892547&14044701555242488356\\ 4&1&63445952785324092174723&12689190557064818434 \end{array} \tag{19d}
Every displayed residue is greater than B=18446744073709551616 and less than its modulus 2^{s+2}. Since 0<W<B, none can be W. This eliminates the last nonrefining cases and completes both directions of (19) for arbitrary normalized W.
Finally, if G=4k+1, the lower candidate (G-1)/2=2k is even and the upper candidate 2k+1 is odd. If G=4k+3, the lower candidate 2k+1 is odd and the upper candidate (G+1)/2=2k+2 is even. Thus G\bmod4=1 is precisely the exceptional lower-even tie, while at a G\bmod4=3 tie the ordinary increment already chooses the even upper candidate.
Finally, the exact unscaled input is related to (16) by
w10^q=R\,2^{q+e_q-l-63}. \tag{20}
Equations (13), (16), and (20) also verify every arithmetic operation used to form the extracted exponent. The cast of q to i32 is exact by (6); the cast of l is exact by (14); u+64-52-3 is 9+u; and
p=(q+e_q+63)+u-l-(-1022)+1 =q+e_q+u-l+1086 \tag{21}
is the value called power2. We check its operations in their extracted order. Over (6), multiplication by the positive constant gives -74455452\le217706q\le67053448. Arithmetic right shift by 16 therefore lies in [-1137,1023], since the endpoint floors are \lfloor-74455452/65536\rfloor=-1137 and \lfloor67053448/65536\rfloor=1023. The checked addition of 63 inside power consequently produces
i14=q+e_q+63\in[-1074,1086].
Now u\in\{0,1\} by (16), so the next checked addition produces i15=i14+u\in[-1074,1087]. The exact cast of l, together with 0\le l\le63 from (14), makes the following checked subtraction produce i17=i15-l\in[-1137,1087]. By (1), the next subtraction gives i19=i17-(-1022)\in[-115,2109], and the final checked addition gives p=i19+1\in[-114,2110]. Each displayed interval is contained in the signed 32-bit range at the point of its corresponding checked addition or subtraction, so every operation succeeds. These intervals cannot be replaced by the final interval for all intermediates. Indeed, at q=-342,w=1, (12) gives q+e_q=-1137 and hence e_q=-795, while (14) gives l=63 and W=2^{63}. Equations (7), (10), and (16) then give R=2^{921}/5^{342}<2^{127}, so u=0. The sequential values are therefore -1074,-1074,-1137,-115,-114 for i14,i15,i17,i19,p, respectively. The shifts by 63, 9, and 10 are all below 64, so the unfolded unsigned-scalar right-shift operations return their exact logical shifts. Thus the extracted u,G,p are precisely (16)--(21), not merely mathematical stand-ins for possibly failing binds.

The subnormal branch.

Suppose first that p\le0 and put d=1-p. The final bound following (21) now gives -114\le p\le0. Hence the checked negation produces -p\in[0,114], and each of the two extracted checked additions by one returns d=(-p)+1\in[1,115]; all three results are in signed 32-bit range. If d\ge64, the interval certificate (17), scaled by (20), gives
0<w10^q<2^{-1075}. \tag{22}
Equality cannot occur: this branch has a negative decimal exponent whose uncancelled factor of five is nontrivial, whereas 2^{-1075} is dyadic; the same fact is visible in the strict side of (11). Hence the extracted return fp_0 is exactly the zero choice required by (2).
If d<64, then 1\le d\le63, so both right shifts by d and by one are legal. The code computes
J=G\mathbin{\mathtt{>>>}}d,\qquad M=(J+(J\mathbin{\mathtt{\&}}1))\mathbin{\mathtt{>>>}}1. \tag{23}
The conjunction is the exact low bit by ModelU64BitAnd, and the addition cannot overflow because J<2^{54}. By (20)--(21), one unit of J is exactly half a subnormal ulp, 2^{-1075}. Thus (23) rounds to the nearer multiple M2^{-1074}. An exact tie is again impossible. Indeed, if q\ge-27, then e_q\ge-63 and (21), using l\le63 and u\ge0, gives p\ge-27-63-63+1086=933>0; hence a subnormal input forces q<-27. A 64-bit w contains at most 27 factors of five (5^{28}>B), leaving an odd factor five in the denominator. Therefore the unconditional upward choice when the guard bit is one is nearest-even as well as nearest.
The legal shift 1\mathbin{\mathtt{<<}}52=U is justified by ModelU64Shl. The rounded M is at most U; the Boolean cast sets the returned exponent to zero for M<U and to one exactly for M=U. In the first case unrestricted assembly is word M and (2) gives M2^{-1074}. In the second case both the mantissa and exponent have bit 52 set, so bitwise OR is still U (by ModelU64BitOr), the first normal word, whose value is also U2^{-1074}. This treats the subnormal carry without assuming that the returned mantissa is strictly below U.

Normal rounding and exact ties.

Now suppose p>0. Before rounding, (17), (20), and (21) say that G is the 53 retained significand bits together with one guard bit for the normal grid of spacing
\Delta=2^{p-1075}; \tag{24}
one unit of G represents \Delta/2. By (17), write R/2^s=G+\theta with 0\le\theta<1. If G=2k then (G+\theta)/2\in[k,k+1/2), so shifting right selects the strictly nearer lower significand. If G=2k+1 and \theta>0, then (G+\theta)/2\in(k+1/2,k+1), so adding one before shifting selects the strictly nearer upper significand. The only parity decision is therefore G odd and \theta=0, equivalently R=G2^s.
Equations (18)--(19) say exactly when that decision is lower-even. The extracted nested tests compare q with the proved bounds -4 and 23, compute G\mathbin{\mathtt{\&}}3, test lo\le1, and compare the legal reconstruction shift with hi. By both directions of (19), all these tests succeed with G\bmod4=1 if and only if this is an exact lower-even tie. Complementing one gives the word B-2, and conjunction therefore clears the guard bit of G. Bitwise complement and conjunction are total 64-bit operations, and their per-bit meaning is the one in ModelU64BitAnd. The subsequent addition and shift then select the even lower significand. At a G\bmod4=3 tie, the final paragraph after (19) proves that the ordinary addition selects the even upper significand. If any extracted condition is false, the reverse implication of (19) rules out a lower-even tie; the strict cases and the upper-even tie are therefore handled by the ordinary direction. Hence in all cases
S=(G' +(G'\mathbin{\mathtt{\&}}1)) \mathbin{\mathtt{>>>}}1 \tag{25}
is the nearest significand, with even low bit at equality. Here G' denotes the possibly corrected G. The addend is zero or one, G'<2^{54}, so the checked addition in (25) succeeds; its shift count is one.
If S<2U, the extracted pair keeps exponent p and mantissa S before masking. If S\ge2U, the preceding bounds force S=2U; the legal shifts by 52 construct U and 2U, and the checked exponent increment produces p+1. This is the usual significand carry because 2U\,2^{p-1075}=U2^{p+1-1075}. In either case conjunction with the complement of U clears exactly the hidden bit and leaves a fraction in [0,U). Consequently, whenever the final exponent p_f<2047, unrestricted assembly is the word
p_fU+(S_f-U), \tag{26}
where S_f\in[U,2U) is the carried or uncarried 53-bit significand. There is no overlap between the exponent bits and the fraction bits in (26), so the OR in DecimalGoalVocabularyDefault is this sum. Formula (2), together with (24)--(25), proves that (26) is exactly the nearest-even binary64 word for w10^q.
It remains to check the final infinity test. If p_f\ge2047, the extracted code returns fp_\infty. By (21), (24), and the carry analysis, this can happen exactly when rounding at the top binade reaches the endpoint following the maximum finite word. Its boundary is the midpoint H in (3). Below H, (25) selects the maximum finite significand 2U-1 with exponent 2046; at H the competitors are the odd word 2047U-1 and the even word 2047U, so nearest-even selects infinity; above H infinity remains the stipulated target. Thus the exponent-at-least-2047 branch agrees exactly with the finite-to-infinity clause, including equality.

Assembly and conclusion.

All continuing branches just considered return exponent 0, 1, a normal field in [1,2046], or 2047, never a negative exponent. Their mantissa and exponent bits were evaluated explicitly above. Choose the F64 carrier word given by the unrestricted assembly expression in DecimalGoalVocabularyDefault. For zero it is 0; for a subnormal or its carry it is the word established after (23); for a finite normal it is (26); and for infinity it is 2047U. Hence assembledWord fp target holds by construction. Equations (2)--(3), (22)--(26), and the adjacent-word argument preceding (3) prove roundsMagnitude for the exact real number (w:\mathbb R)10^q.
The only successful branch not included in that construction is the explicit approximation-error return, already shown from (4) to have fp.p_{\rm biased}=-1<0. The immediate returns, error return, subnormal zero and nonzero paths, ordinary and exact-midpoint normal paths, significand-carry path, and infinity path exhaust both duplicated continuing blocks of num imp dec2flt lemire compute float. Every monadic bind, cast, index, checked arithmetic operation, mask, complement, and shift on an ok path has been accounted for. This proves the required disjunction.

Lean proof


  intro q w fp h
  rcases IntegerComputeFloatControlFlow q w fp h with
    hdefer | hzero | hsmall | hlarge | hcore
  · exact Or.inl hdefer
  · exact Or.inr (IntegerEndpointRounding q w fp (Or.inl hzero))
  · exact Or.inr
      (IntegerEndpointRounding q w fp (Or.inr (Or.inl hsmall)))
  · exact Or.inr
      (IntegerEndpointRounding q w fp (Or.inr (Or.inr hlarge)))
  · rcases hcore with ⟨hw, hqlo, hqhi, hp⟩
    exact Or.inr (IntegerCoreRounding q w fp hw hqlo hqhi hp h)

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalGoalVocabularyDefault
import Tablet.Assumptions
import Tablet.F64
import Tablet.ModelF64MantissaDigits
import Tablet.ModelU64BitAnd
import Tablet.ModelU64BitOr
import Tablet.ModelU64Shl
import Tablet.IntegerComputeFloatControlFlow
import Tablet.IntegerCoreRounding
import Tablet.IntegerEndpointRounding
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_BITS
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_LARGEST_POWER_OF_TEN
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_MAX_EXPONENT_ROUND_TO_EVEN
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_MIN_EXPONENT_ROUND_TO_EVEN
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SIG_TOTAL_BITS
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SMALLEST_POWER_OF_TEN
import Tablet.num_imp_dec2flt_common_BiasedFp
import Tablet.num_imp_dec2flt_common_BiasedFp_zero_pow2
import Tablet.num_imp_dec2flt_lemire_compute_float
import Tablet.num_imp_dec2flt_lemire_compute_product_approx
import Tablet.num_imp_dec2flt_lemire_full_multiplication
import Tablet.num_imp_dec2flt_lemire_power
import Tablet.num_imp_dec2flt_table_LARGEST_POWER_OF_FIVE
import Tablet.num_imp_dec2flt_table_POWER_OF_FIVE_128
import Tablet.num_imp_dec2flt_table_SMALLEST_POWER_OF_FIVE
import Tablet.num_imp_traits_Float
import Tablet.num_imp_traits_Float_EXP_BIAS_default
import Tablet.num_imp_traits_Float_EXP_BITS_default
import Tablet.num_imp_traits_Float_EXP_MIN_default
import Tablet.num_imp_traits_Float_EXP_SAT_default
import Tablet.num_imp_traits_Float_INFINITE_POWER_default
import Tablet.num_imp_traits_Float_SIG_BITS_default

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: IntegerCorrectOrDefer]
theorem IntegerCorrectOrDefer :
    ∀ (q : Std.I64) (w : Std.U64) fp,
      num.imp.dec2flt.lemire.compute_float
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 q w = ok fp ->
      fp.p_biased.val < 0 ∨ ∃ target : F64,
        DecimalGoalVocabularyDefault.assembledWord fp target ∧
        DecimalGoalVocabularyDefault.roundsMagnitude ((w.val : Real) * (10 : Real) ^ q.val) target := by
-- BODY
  intro q w fp h
  rcases IntegerComputeFloatControlFlow q w fp h with
    hdefer | hzero | hsmall | hlarge | hcore
  · exact Or.inl hdefer
  · exact Or.inr (IntegerEndpointRounding q w fp (Or.inl hzero))
  · exact Or.inr
      (IntegerEndpointRounding q w fp (Or.inr (Or.inl hsmall)))
  · exact Or.inr
      (IntegerEndpointRounding q w fp (Or.inr (Or.inr hlarge)))
  · rcases hcore with ⟨hw, hqlo, hqhi, hp⟩
    exact Or.inr (IntegerCoreRounding q w fp hw hqlo hqhi hp h)

end dec2flt_model

Proof structure

Follow the declared dependencies to read the supporting statements and proofs.

Explore this dependency tree

Inspect related source (87)
More program dependencies (45)
Standard-library dependencies (39)

Related source and models come from the extraction records and declared dependencies.

Proof assumptions and review

Lean checks the formal proof. Trellis also reviews how the statement corresponds to the intended requirement.

Transitive assumptions

This list combines the assumptions recorded for this declaration and its dependencies. It may include assumptions used by auxiliary declarations in those modules.

Helper theorems included
All 97 entries
Technical checking records
Local closure summary
{
  "node": "IntegerCorrectOrDefer",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "8dd4bedcc065d4a6bef01c5134b432649f4b9ac8fd8a0e8a66ed93d5ae93b481",
  "active_statement_hash": "3457d6d8e2295bd0e6351a4ad85202a9d676de783c99d5fd700150f6dc5ba083",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "ab64d13f256386ee0e494a5abcc6a5db49512cf3d90bbdfcabd6327793a31f95",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-146",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "IntegerComputeFloatControlFlow": "7fc96f97b62006f9",
    "IntegerCoreRounding": "c7e23c1b9dbd5945",
    "IntegerEndpointRounding": "849ab7aea56b6198"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "f76c8174a4d58eb5",
    "DecimalGoalVocabularyDefault": "512f8d0aea894250",
    "F64": "ec4e25fbaf4e11d4",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64": "94095c9bf79da164",
    "num_imp_dec2flt_common_BiasedFp": "c43614ff2149ecf2",
    "num_imp_dec2flt_lemire_compute_float": "39d89e3f0a8a9d4c"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:6177a47f1f81de418be2eced984d07359fd0482bd01f65cfea864b951bfce875",
  "current_fingerprint": "sound-v2:6177a47f1f81de418be2eced984d07359fd0482bd01f65cfea864b951bfce875",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}