\begin{lemma}[Final slow-path rounding and binary64 encoding]
For every $x:\mathtt{DecimalLexeme}$, $B\in\mathbb R$,
$\mathrm{tr}:\mathtt{num.imp.dec2flt.Dec2FltAssemblyTrace}(\mathtt{F64})$,
five records $d,d_0,d_1,d_2,d_3:\mathtt{DecimalSeq}$, and four checked
signed 32-bit scalars $E_0,E_1,E_s,E_2:\mathtt{I32}$, the following
implication holds.  Here $\mathtt{DecimalSeq}$ denotes
$\mathtt{num.imp.dec2flt.decimal\_seq.DecimalSeq}$, and
$e_0,e_1,e_s,e_2\in\mathbb Z$ are the respective scalar values of the
four $E$'s.

Use the following local notation.  For a list $L$ of natural numbers, set
\[
 N(L)=\operatorname{foldl}((a,b)\mapsto10a+b,0,L).
\]
Let $F_x$ be $x.\mathtt{fractionDigits}$ when present and the empty list
otherwise.  If $x.\mathtt{exponent}$ is absent, put $a_x=0$.  If it is
present as $(u,\sigma,L)$, fold $L$ from zero, replacing the accumulator
$a$ by $10a+b$ on the next entry $b$ when $a<65536$, and leaving $a$
unchanged otherwise.  Give the resulting natural accumulator a minus sign
exactly when $\sigma=\operatorname{some}(\mathtt{true})$, to obtain the
integer $a_x$; the Boolean $u$ does not affect this value.  Put
\[
 A=\operatorname{accumulatedMagnitude}(x)
   =N(x.\mathtt{integerDigits}\mathbin{++}F_x)
       10^{a_x-|F_x|}.
\]
For any decimal record $s$, define
\[
 \begin{aligned}
 n(s)&=s.\mathtt{num\_digits}.\mathtt{val}\in\mathbb N,&
 q(s)&=s.\mathtt{decimal\_point}.\mathtt{val}\in\mathbb Z,\\
 \tau(s)&=s.\mathtt{truncated},&
 D(s)&=\operatorname{take}_{n(s)}(s.\mathtt{digits}.\mathtt{val}),\\
 L(s)&=\operatorname{map}(\mathtt{val},D(s)),&
 V(s)&=N(L(s))10^{q(s)-n(s)}\in\mathbb R.
 \end{aligned}
\]
The map retains list order and takes each unsigned byte's natural value.
Write $n_i=n(d_i)$, $q_i=q(d_i)$, $\tau_i=\tau(d_i)$,
$D_i=D(d_i)$, $L_i=L(d_i)$, and $V_i=V(d_i)$.
The notation $\operatorname{last}(L)$ means the last entry of a nonempty
list.  Decimal values and scaled magnitude inequalities use real powers
with integer exponents and embed natural coefficients in $\mathbb R$.
Word arithmetic and scalar or digit-count bounds use natural or integer
arithmetic as indicated.

For call notation let $\mathcal P$ stand for the name prefix
\texttt{num.imp.dec2flt}, and fix the binary64 trait instance
\texttt{F64.Insts.Dec2flt\_modelNumImpTraitsFloatU64}.
The symbols $\mathsf{parse}$, $\mathsf{slow}$, and $\mathsf{Loop}_i$
denote respectively the model operations
$\mathcal P.\mathtt{decimal\_seq.parse\_decimal\_seq}$,
$\mathcal P.\mathtt{slow.parse\_long\_mantissa}$, and
$\mathcal P.\mathtt{slow.parse\_long\_mantissa\_loop}i$
for $i=0,1,2$.  Each $\mathsf{slow}$ or $\mathsf{Loop}_i$ call includes
the fixed trait instance as its first argument.  Write $\mathsf{left}$
and $\mathsf{right}$ for
$\mathcal P.\mathtt{decimal\_seq.DecimalSeq.left\_shift}$ and
$\mathcal P.\mathtt{decimal\_seq.DecimalSeq.right\_shift}$.
In calls, $Q(s)$ abbreviates the four consecutive arguments
\[
 s.\mathtt{num\_digits},\quad s.\mathtt{decimal\_point},\quad
 s.\mathtt{truncated},\quad s.\mathtt{digits};
\]
in a returned tuple, $(Q(s),E,\mathtt{none})$ abbreviates the six-tuple
of those four fields followed by $E$ and $\mathtt{none}$.
The notation $k_T$ denotes the scalar constant $k$ of type $T$, and
$\operatorname{ok}(v)$ denotes successful return of exactly $v$.
Set the two biased-pair constants
\[
 Z=\{\mathtt{m}=0_{\mathtt{U64}},
       \mathtt{p\_biased}=0_{\mathtt{I32}}\},\qquad
 J=\{\mathtt{m}=0_{\mathtt{U64}},
       \mathtt{p\_biased}=2047_{\mathtt{I32}}\}.
\]
Define the natural unit $U=2^{52}$, the infinity word $T=2047U$, and
the real threshold $H=2^{1024}-2^{970}$.  With
$m=\mathrm{tr}.\mathtt{selected\_pair}.\mathtt{m}.\mathtt{val}\in\mathbb N$
and
$b=\mathrm{tr}.\mathtt{selected\_pair}.\mathtt{p\_biased}.\mathtt{val}\in\mathbb Z$,
define the natural raw word
\[
 r=m\mathbin{\operatorname{OR}}
       \bigl((\operatorname{emod}_{2^{64}}(b)\,U)\bmod2^{64}\bigr).
\]
Here $\operatorname{OR}$ is bitwise OR on natural numbers, and
$\operatorname{emod}_{2^{64}}(b)$ is the unique integer in
$[0,2^{64})$ congruent to $b$ modulo $2^{64}$, viewed as a natural
number.  Thus the exponent-field shift is reduced modulo $2^{64}$.

Assume the exact successful calls and numerical premises
\[
 \begin{gathered}
 \mathsf{parse}(\mathrm{tr}.\mathtt{body})=\operatorname{ok}(d),\qquad
 n(d)\ne0,\qquad -324\leq q(d)<310,\\
 \mathsf{slow}(\mathrm{tr}.\mathtt{body})
       =\operatorname{ok}(\mathrm{tr}.\mathtt{selected\_pair}),\qquad
 \mathrm{tr}.\mathtt{assembly\_result}.\mathtt{val}=r,\\
 \mathsf{Loop}_0(Z,Q(d),0_{\mathtt{I32}},\mathtt{none})
       =\operatorname{ok}((Q(d_0),E_0,\mathtt{none})),\\
 \mathsf{Loop}_1(J,Q(d_0),E_0,\mathtt{none})
       =\operatorname{ok}((Q(d_1),E_1,\mathtt{none})),\\
 \mathsf{Loop}_2(d_1,E_s)=\operatorname{ok}((d_2,E_2)),\\
 \operatorname{sub}_{\mathtt{I32}}(E_1,1_{\mathtt{I32}})
       =\operatorname{ok}(E_s),\qquad e_s=e_1-1,\\
 q_1=0,\qquad n_1\ne0,\qquad
       5\leq d_1.\mathtt{digits}.\mathtt{val}[0].\mathtt{val},\\
 -1079\leq e_1,\qquad B/2^{e_1}\leq1,\qquad
 e_2=\max(e_s,-1022),\qquad \max(e_2-e_s,0)\leq58.
 \end{gathered}
\]
Here $\operatorname{sub}_{\mathtt{I32}}$ is checked signed 32-bit
subtraction, so its equation includes successful evaluation without
overflow; the last bound uses the nonnegative part of the integer difference.

Assume the following $d_2$ certificate:
\[
 \begin{gathered}
 n_2\leq768,\qquad \forall z\in D_2,\quad z.\mathtt{val}<10,\\
 \exists z\in\mathbb N\ \exists L\in\operatorname{List}(\mathbb N),\quad
 L_2=z::L\ \land\ 1\leq z<10\ \land\ |L_2|=n_2,\\
 \exists\eta_2,u_2\in\mathbb R:\quad
 \begin{aligned}
 &A/2^{e_2+1}=V_2+\eta_2\ \land\ B/2^{e_2+1}=V_2+u_2\\
 &{}\land\ 0\leq\eta_2<u_2
       \ \land\ (\tau_2=\mathtt{true}\ \Longleftrightarrow\ 0<\eta_2).
 \end{aligned}
 \end{gathered}
\]
This requires a positive leading digit, but imposes no nonzero-last-digit
condition on $d_2$.

Assume $\mathsf{left}(d_2,53_{\mathtt{Usize}})=\operatorname{ok}(d_3)$
and all of the following $d_3$ certificate:
\[
 \begin{gathered}
 n_3\leq768,\qquad \forall z\in D_3,\quad z.\mathtt{val}<10,\\
 \exists z\in\mathbb N\ \exists L\in\operatorname{List}(\mathbb N),\quad
 L_3=z::L\ \land\ 1\leq z<10\ \land\ |L_3|=n_3
       \ \land\ \operatorname{last}(L_3)\ne0,\\
 \exists\eta_3,u_3\in\mathbb R:\quad
 \begin{aligned}
 &A/2^{e_2-52}=V_3+\eta_3\ \land\ B/2^{e_2-52}=V_3+u_3\\
 &{}\land\ 0\leq\eta_3<u_3
       \ \land\ (\tau_3=\mathtt{true}\ \Longleftrightarrow\ 0<\eta_3),
 \end{aligned}\\
 q_3<19,\qquad 0\leq A/2^{e_2-52},\qquad
 A/2^{e_2-52}<2^{64}-\tfrac12,\\
 \forall n\in\mathbb N,\quad
 n+\tfrac12\leq V_3\ \lor\ B/2^{e_2-52}\leq n+\tfrac12.
 \end{gathered}
\]

Finally assume that for every record $d_4:\mathtt{DecimalSeq}$, if
\[
 \mathsf{right}(d_3,1_{\mathtt{Usize}})=\operatorname{ok}(d_4)
       \quad\hbox{and}\quad n(d_4)\ne0,
\]
then, using $n_4,q_4,\tau_4,D_4,L_4,V_4$ as defined above for $d_4$,
all of the following hold:
\[
 \begin{gathered}
 n_4\leq768,\qquad \forall z\in D_4,\quad z.\mathtt{val}<10,\\
 \exists z\in\mathbb N\ \exists L\in\operatorname{List}(\mathbb N),\quad
 L_4=z::L\ \land\ 1\leq z<10\ \land\ |L_4|=n_4
       \ \land\ \operatorname{last}(L_4)\ne0,\\
 \exists\eta_4,u_4\in\mathbb R:\quad
 \begin{aligned}
 &A/2^{e_2-51}=V_4+\eta_4\ \land\ B/2^{e_2-51}=V_4+u_4\\
 &{}\land\ 0\leq\eta_4<u_4
       \ \land\ (\tau_4=\mathtt{true}\ \Longleftrightarrow\ 0<\eta_4),
 \end{aligned}\\
 q_4<19,\qquad 0\leq A/2^{e_2-51},\qquad
 A/2^{e_2-51}<2^{64}-\tfrac12,\\
 \forall n\in\mathbb N,\quad
 n+\tfrac12\leq V_4\ \lor\ B/2^{e_2-51}\leq n+\tfrac12.
 \end{gathered}
\]
This is conditional on both successful shifting and a nonempty result; it
does not assume that such a $d_4$ exists.

Then at least one of the following three alternatives holds.
\begin{enumerate}
\item Infinity: $r=T$ and $H\leq A$.
\item Low grid: there exists $j\in\mathbb N$ such that
\[
 \begin{gathered}
 j\leq2^{53},\qquad r=j,\qquad A\leq2^{53}2^{-1074},\\
 A\leq j2^{-1074}+2^{-1075},\qquad
 j2^{-1074}\leq A+2^{-1075},\\
 \bigl(A=(j+\tfrac12)2^{-1074}\ \lor
       A=(j-\tfrac12)2^{-1074}\bigr)
       \ \Longrightarrow\ j\bmod2=0.
 \end{gathered}
\]
\item Normal grid: there exist $p,j\in\mathbb N$ such that, with the real
spacing $\Delta=2^{p-1075}$ (where $p-1075$ is an integer difference),
\[
 \begin{gathered}
 2\leq p\leq2046,\qquad U\leq j\leq2^{53},\qquad
       p<2046\ \lor\ j<2^{53},\\
 r=pU+(j-U),\qquad U\Delta\leq A\leq2^{53}\Delta,\qquad A<H,\\
 A\leq j\Delta+\Delta/2,\qquad j\Delta\leq A+\Delta/2,\\
 \bigl(A=(j+\tfrac12)\Delta\ \lor\ A=(j-\tfrac12)\Delta\bigr)
       \ \Longrightarrow\ j\bmod2=0.
 \end{gathered}
\]
The difference $j-U$ in the raw word is natural subtraction; the stated
lower bound on $j$ makes it the ordinary nonnegative difference.
\end{enumerate}
\end{lemma}
\begin{proof}
Unfold \noderef{num_imp_dec2flt_slow_parse_long_mantissa} and rewrite its
three loops by the exact call hypotheses.  The checked decrement gives
$e_s=e_1-1$, and the normalization equation gives
$e_2=\max(e_s,-1022)$.  Hence
\[
 -1022\leq e_2,\qquad e_1\leq e_2+1.                    \tag{1}
\]
Write $V_2$ for the positive decimal value represented by $d_2$.  Positivity
follows from its nonempty canonical leading digit and
\noderef{P804_T1_DecimalValueBounds}.  The two endpoint equations for $d_2$
have errors $0\leq\eta_2<u_2$, so
\[
 \frac{A}{2^{e_2+1}}=V_2+\eta_2
 <V_2+u_2=\frac{B}{2^{e_2+1}}.                          \tag{2}
\]
In particular $A<B$ and $B>0$.  By (1), monotonicity of powers of two, and
the explicit hypothesis $B/2^{e_1}\leq1$,
\[
 \frac{B}{2^{e_2+1}}\leq\frac{B}{2^{e_1}}\leq1,
 \qquad \frac{A}{2^{e_2+1}}<1.                         \tag{3}
\]
This is the normalization bound needed below; it is not inferred merely from
the leading digit.

Put $X=A/2^{e_2-52}$ and $B_3=B/2^{e_2-52}$.  The supplied
$d_3$ certificate, whose construction is stated by
\noderef{ParserSlowFirstRoundSeparation}, gives precisely the shape, endpoint,
point, U64-range, and half-integer-separation premises of
\noderef{DecimalSeqRoundArbitraryEndpoint}.  Applying that theorem produces
the same U64 $M$ as the extracted first round and gives
\[
 X\leq M+\tfrac12,\qquad M\leq X+\tfrac12,              \tag{4}
\]
with $M$ even if either inequality is an equality at a midpoint.  Rewriting
$X=(A/2^{e_2+1})2^{53}$ in (3) gives $X<2^{53}$; since $M$ is integral,
(4) gives $M\leq2^{53}$.

The constants supplied by \noderef{ParserExponentConstants} reduce the first
saturation test in \noderef{num_imp_dec2flt_slow_parse_long_mantissa} to
$e_2+1023\geq2047$.  If it succeeds, then $e_2\geq1024$.  In this case
$e_2>-1022$, so loop 2 performs no shift: its one-step body is already done,
$d_2=d_1$, and $e_2=e_s$.  The point-zero and leading-digit hypotheses and
\noderef{P804_T1_DecimalValueBounds} give $V_2\geq1/2$, whence
$A/2^{e_2+1}\geq1/2$.  Therefore $A\geq2^{1024}$, which is at least the
required threshold $2^{1024}-2^{970}$.  The extracted result is exactly the
zero-mantissa, biased-exponent-2047 record, whose raw word is
$2047\,2^{52}$.

Suppose instead that the first test does not saturate.  The checked cast and
addition produce the shift count 53, the stipulated left-shift call identifies
its result with $d_3$, the stipulated round call identifies its result with
$M$, and the checked U32 addition and U64 shift produce the comparison word
$2^{53}$.  Thus the extracted carry branch is taken exactly when
$M\geq2^{53}$.

In the carry branch $M=2^{53}$.  It is also necessary to justify, rather than
assume, the nonempty successor required by the carry certificate.  Since this
round result is nonzero, unfolding the round guard rules out a negative
decimal point for $d_3$.  Its canonical positive-leading shape and point
bound then satisfy \noderef{DecimalRightShiftLoop0Execution} with shift one.
That theorem constructs a successful right shift with nonzero digit count;
determinism of the result identifies it with the successor in the extracted
call.  The universal carry hypothesis, whose certificate is supplied by
\noderef{ParserSlowCarryRoundSeparation}, now applies.  A second application
of \noderef{DecimalSeqRoundArbitraryEndpoint} gives the rerounded integer
$M_4$.  From (4), (3), and $M=2^{53}$,
\[
 2^{52}-\tfrac14\leq A/2^{e_2-51}<2^{52}.
\]
The two half-step inequalities for $M_4$ force $M_4=2^{52}$.

The checked exponent update in this branch is $e_{24}=e_2+1$ and its second
biased test is $e_2+1024\geq2047$.  If that test succeeds, the preceding
nonsaturation forces $e_2=1023$.  The lower inequality in (4) then gives
\[
 (2^{53}-\tfrac12)2^{971}\leq A.
\]
Direct power arithmetic gives
\[
 (2^{53}-\tfrac12)2^{971}=2^{1024}-2^{970}.             \tag{5}
\]
Thus this extracted infinity result satisfies the exact threshold clause.  If
the second test does not succeed, $e_2\leq1022$.  The reround is $2^{52}$,
its mask is zero, and the raw word is $(e_2+1024)2^{52}$.  For
$e_2=-1022$ this is the low-grid witness $j=2^{53}$.  For $e_2>-1022$,
choose the equivalent endpoint witness $p=e_2+1023$, $j=2^{53}$; then
$(e_2+1024)2^{52}=p2^{52}+(j-2^{52})$.  The inequalities and parity clause
are exactly (4) scaled by $2^{e_2-52}$.

It remains to treat the noncarry branch $M<2^{53}$.  Its checked arithmetic
first forms $p_0=e_2+1023$, subtracts one precisely when $M<2^{52}$, and
masks the mantissa by $2^{52}-1$.  The mask identity is
\[
 (M\mathbin{\&}(2^{52}-1))=M\bmod2^{52}.               \tag{6}
\]
The masked value is below $2^{52}$, while the exponent field is a multiple
of $2^{52}$; hence their bitwise OR is their sum.  These are exactly the
operations denoted by \noderef{ModelU64Shl}, \noderef{ModelU64BitAnd}, and
\noderef{ModelU64BitOr}.  If $e_2=-1022$, the two cases
$M<2^{52}$ and $M\geq2^{52}$ reduce (6) respectively to exponent fields zero
and one, and both give raw word $M$.  Thus $j=M$ is the low-grid witness.

If $e_2>-1022$, the no-shift argument used above gives
$A/2^{e_2+1}\geq1/2$, hence $X\geq2^{52}$.  The first inequality in (4) and
integrality force $M\geq2^{52}$, so no exponent decrement occurs.  With
$p=e_2+1023$ and $j=M$, (6) gives
\[
 2\leq p\leq2046,\quad 2^{52}\leq j<2^{53},\quad
 r=p2^{52}+(j-2^{52}).                                  \tag{7}
\]
The lower and upper magnitude bounds follow from
$2^{52}\leq X<2^{53}$ after multiplying by
$\Delta=2^{p-1075}=2^{e_2-52}$, and (4) gives both half-step inequalities
and the even-tie implication.

For every finite case with $e_2\leq1022$, (3) gives
$A<2^{e_2+1}\leq2^{1023}<2^{1024}-2^{970}$.  In the sole remaining case
$e_2=1023$, noncarry gives $M\leq2^{53}-1$, and (4) gives
$X\leq2^{53}-1/2$.  Equality would force
$M=2^{53}-1$, an odd midpoint result, contradicting the even-tie clause.
Thus the inequality is strict; multiplying by $2^{971}$ and using (5) gives
$A<2^{1024}-2^{970}$.  The carry and noncarry cases are exhaustive, and the
displayed calculations establish every raw-word, range, nearest, parity, and
threshold conjunct in the three alternatives.
\end{proof}
