~/bend-docscommunity

f64.bend checks

raw source on the hub · import 0x9c00036bcd9556a4ab0f442a1bdecc28/f64.bend as F64

2 imports
import Base
import element-ints@0.1.0.0/signed.bend as S

Types

type F source · line 18 · raw

Data

type Dec source · line 21 · raw

Data

type QR source · line 27 · raw

Data

type Digits source · line 30 · raw

Data

type Lit source · line 33 · raw

Data

type Ex source · line 36 · raw

Data

type Q53 source · line 559 · raw

Data

A bounded, allocation-free quotient loop for binary64 significands. Normalize both operands to 53 bits. Then a/b is in [1/2, 2), so 57 restoring steps yield floor(a * 2^56 / b). Each remainder is < 2*b < 2^54; quotient and remainder therefore fit two U32 words without lost bits. The final remainder is doubled, which preserves the only fact rounding needs: whether any discarded bit is nonzero. Keep div_qr/round_pack unchanged.

Definitions

def lo16 source · line 43 · raw

@+x:U32 -> U32

def hi16 source · line 46 · raw

@+x:U32 -> U32

def head0 source · line 49 · raw

@xs:List<&2, U32> -> U32

def tail0 source · line 56 · raw

@xs:List<&2, U32> -> List<&2, U32>

def carry_go source · line 63 · raw

@b:List<&2, U32> -> @+c:U32 -> List<&2, U32>

def add_go source · line 71 · raw

@a:List<&2, U32> -> @+b:List<&2, U32> -> @+c:U32 -> List<&2, U32>

def n_add source · line 83 · raw

@+a:List<&2, U32> -> @+b:List<&2, U32> -> List<&2, U32>

def sub_go source · line 87 · raw

@a:List<&2, U32> -> @+b:List<&2, U32> -> @+br:U32 -> List<&2, U32>

a - b for a >= b.

def drop_zeros source · line 96 · raw

@xs:List<&2, U32> -> List<&2, U32>

def norm source · line 104 · raw

@+xs:List<&2, U32> -> List<&2, U32>

Without zero limbs on the high side.

def n_sub source · line 107 · raw

@+a:List<&2, U32> -> @+b:List<&2, U32> -> List<&2, U32>

def is_zero source · line 110 · raw

@xs:List<&2, U32> -> Bool

def n_cmp source · line 120 · raw

@a:List<&2, U32> -> @b:List<&2, U32> -> U32

0: a < b, 1: a = b, 2: a > b. Compare in the stored order. A difference in the remaining (higher) limbs wins; missing limbs are zero. No normalization or reversed copies.

def n_ge source · line 132 · raw

@+a:List<&2, U32> -> @+b:List<&2, U32> -> Bool

def mul_small source · line 135 · raw

@b:List<&2, U32> -> @+x:U32 -> @+c:U32 -> List<&2, U32>

def n_mul source · line 143 · raw

@a:List<&2, U32> -> @+b:List<&2, U32> -> List<&2, U32>

def zeros_then source · line 150 · raw

@k:Nat -> @+xs:List<&2, U32> -> List<&2, U32>

def shl_bits source · line 157 · raw

@a:List<&2, U32> -> @+r:U32 -> @+c:U32 -> List<&2, U32>

def n_shl source · line 166 · raw

@+a:List<&2, U32> -> @+s:U32 -> List<&2, U32>

a * 2^s.

def shr_bits source · line 169 · raw

@a:List<&2, U32> -> @+r:U32 -> List<&2, U32>

def n_shr source · line 177 · raw

@+a:List<&2, U32> -> @+s:U32 -> List<&2, U32>

floor(a / 2^s).

def n_low source · line 181 · raw

@+a:List<&2, U32> -> @+s:U32 -> List<&2, U32>

a mod 2^s.

def bits32 source · line 187 · raw

@+x:U32 -> U32

def bitlen_go source · line 198 · raw

@a:List<&2, U32> -> @+offset:U32 -> @+seen:U32 -> U32

def bitlen source · line 206 · raw

@a:List<&2, U32> -> U32

def odd source · line 209 · raw

@xs:List<&2, U32> -> Bool

def n_of source · line 212 · raw

@+x:U32 -> List<&2, U32>

def one source · line 215 · raw

List<&2, U32>

def subtract_if source · line 219 · raw

@fits:Bool -> @a:List<&2, U32> -> @b:List<&2, U32> -> List<&2, U32>

Long division by shift and subtract: i from the top bit down.

def div_go source · line 226 · raw

@k:Nat -> @+q:List<&2, U32> -> @+r:List<&2, U32> -> @+bs:List<&2, U32> -> QR

def n_divmod source · line 236 · raw

@+a:List<&2, U32> -> @+b:List<&2, U32> -> QR

def pow10 source · line 242 · raw

@k:Nat -> List<&2, U32>

def smax source · line 251 · raw

@+a:U32 -> @+b:U32 -> U32

def smin source · line 254 · raw

@+a:U32 -> @+b:U32 -> U32

def spos source · line 257 · raw

@+a:U32 -> Bool

def nth16 source · line 262 · raw

@xs:List<&2, U32> -> @n:Nat -> U32

def pack source · line 265 · raw

@+neg:Bool -> @+biased:U32 -> @+frac:List<&2, U32> -> F

def inf source · line 269 · raw

@+neg:Bool -> F

def nan source · line 272 · raw

F

def zero source · line 275 · raw

@+neg:Bool -> F

def pack_rounded source · line 279 · raw

@+neg:Bool -> @+m:List<&2, U32> -> @+lsb:U32 -> F

m * 2^lsb with m < 2^53 (after rounding): a subnormal when m < 2^52.

def round_shr source · line 287 · raw

@+m:List<&2, U32> -> @+s:U32 -> List<&2, U32>

m >> s, rounded to nearest, ties to even (s > 0).

def shift_to source · line 292 · raw

@right:Bool -> @+m:List<&2, U32> -> @+sh:U32 -> List<&2, U32>

def carry_fix source · line 299 · raw

@over:Bool -> @+neg:Bool -> @+q:List<&2, U32> -> @+lsb:U32 -> F

def round_pack source · line 307 · raw

@+neg:Bool -> @+m:List<&2, U32> -> @+e:U32 -> F

The nearest binary64 to (-1)^neg * m * 2^e, m > 0.

def pack_zero source · line 314 · raw

@z:Bool -> @+neg:Bool -> @+m:List<&2, U32> -> @+e:U32 -> F

def exact source · line 321 · raw

@+neg:Bool -> @+m:List<&2, U32> -> @+e:U32 -> F

def frac_of source · line 326 · raw

@+hi:U32 -> @+lo:U32 -> List<&2, U32>

def dec_finite source · line 329 · raw

@normal:Bool -> @+neg:Bool -> @+hi:U32 -> @+lo:U32 -> @biased:U32 -> Dec

def dec_kind source · line 338 · raw

@special:Bool -> @+neg:Bool -> @+hi:U32 -> @+lo:U32 -> @+biased:U32 -> Dec

def dec source · line 345 · raw

@f:F -> Dec

def negate source · line 352 · raw

@f:F -> F

def is_nan source · line 359 · raw

@f:F -> Bool

def diff_go source · line 364 · raw

@c:Nat -> @+na:Bool -> @+nb:Bool -> @+a:List<&2, U32> -> @+b:List<&2, U32> -> @+e:U32 -> F

def diff source · line 373 · raw

@+c:U32 -> @+na:Bool -> @+nb:Bool -> @+a:List<&2, U32> -> @+b:List<&2, U32> -> @+e:U32 -> F

def add_signs source · line 376 · raw

@same:Bool -> @+na:Bool -> @+nb:Bool -> @+a:List<&2, U32> -> @+b:List<&2, U32> -> @+e:U32 -> F

def add_fin source · line 384 · raw

@+na:Bool -> @+ma:List<&2, U32> -> @+ea:U32 -> @+nb:Bool -> @+mb:List<&2, U32> -> @+eb:U32 -> F

Both finite: x + y exactly, then rounded.

def add_dec source · line 390 · raw

@x:Dec -> @y:Dec -> @+fx:F -> @+fy:F -> F

def add source · line 425 · raw

@+a:F -> @+b:F -> F

def sub source · line 428 · raw

@+a:F -> @+b:F -> F

def xor_b source · line 431 · raw

@+a:Bool -> @+b:Bool -> Bool

def mul53_words source · line 439 · raw

@+a:List<&2, U32> -> @+b:List<&2, U32> -> List<&2, U32>

Exact 53x53-bit significand product, least-significant 16-bit limb first. Called only from mul_dec(dec(a), dec(b)); the top input limbs are <=31. Each partial product fits U32 and each split column sum is <2^19. Preserve all 106 bits for the existing round_pack; general n_mul stays available to wide power/decimal arithmetic. No intermediate row lists.

def mul_dec source · line 510 · raw

@x:Dec -> @y:Dec -> F

def mul source · line 545 · raw

@+a:F -> @+b:F -> F

def div_qr source · line 548 · raw

@qr:QR -> @+neg:Bool -> @+e:U32 -> F

def div53_loop source · line 562 · raw

@n:Nat -> @+qh:U32 -> @+ql:U32 -> @+rh:U32 -> @+rl:U32 -> @+bh:U32 -> @+bl:U32 -> Q53

def div53_words source · line 576 · raw

@+a:List<&2, U32> -> @+b:List<&2, U32> -> Q53

def div53_finish source · line 583 · raw

@q:Q53 -> @+neg:Bool -> @+e:U32 -> F

def div_widths source · line 589 · raw

@small:Bool -> @+neg:Bool -> @+ma:List<&2, U32> -> @+mb:List<&2, U32> -> @+la:U32 -> @+lb:U32 -> @+k:U32 -> @+e:U32 -> F

def div_fin source · line 599 · raw

@+neg:Bool -> @+ma:List<&2, U32> -> @+ea:U32 -> @+mb:List<&2, U32> -> @+eb:U32 -> F

m_a / m_b with a quotient of 56 bits or more and a sticky bit below it.

def div_dec source · line 606 · raw

@x:Dec -> @y:Dec -> F

def div source · line 641 · raw

@+a:F -> @+b:F -> F

def rem_qr source · line 644 · raw

@qr:QR -> @+neg:Bool -> @+e:U32 -> F

def rem_fin source · line 650 · raw

@+na:Bool -> @+ma:List<&2, U32> -> @+ea:U32 -> @+mb:List<&2, U32> -> @+eb:U32 -> F

JavaScript %: the remainder of truncated division, exact, with a's sign.

def rem_dec source · line 654 · raw

@x:Dec -> @y:Dec -> @+fx:F -> F

def rem source · line 681 · raw

@+a:F -> @+b:F -> F

def n_pow source · line 686 · raw

@k:Nat -> @+m:List<&2, U32> -> @+acc:List<&2, U32> -> List<&2, U32>

def pow_fin source · line 696 · raw

@+neg:Bool -> @+m:List<&2, U32> -> @+e:U32 -> @+n:U32 -> F

x ** n for an integer n, exact then rounded once: (m 2^e)^n = m^n 2^(e n); a negative n is 1 / x^|n| (a quotient with a sticky bit). |n| is capped at 2100 (beyond it every finite x != 1 over- or underflows anyway).

def from_i32 source · line 705 · raw

@+n:U32 -> F

def pow_dec source · line 710 · raw

@d:Dec -> @+f:F -> @+n:U32 -> F

x ** n for an int32 n. n = 0 is 1 whatever x is, NaN too (ES 6.1.6.1.3, step 1: NaN ** 0 was NaN, m4c-pow-special).

def pow_int source · line 721 · raw

@+f:F -> @+n:U32 -> F

def isqrt_go source · line 728 · raw

@k:Nat -> @+a:List<&2, U32> -> @+r:List<&2, U32> -> @+rem:List<&2, U32> -> @+shift:U32 -> QR

floor(sqrt(a)) by the digit-by-digit method, two bits at a time from the top; the remainder says whether it was exact.

def isqrt source · line 738 · raw

@+a:List<&2, U32> -> QR

def S_half source · line 742 · raw

@+e:U32 -> U32

def sqrt_qr source · line 745 · raw

@qr:QR -> @+e:U32 -> F

def sqrt_fin source · line 752 · raw

@+m:List<&2, U32> -> @+e:U32 -> F

sqrt(m 2^e): e made even, m widened to 112 bits or more, the integer root taken with a sticky bit from its remainder, then rounded once.

def sqrt_dec source · line 761 · raw

@d:Dec -> @+f:F -> F

def sqrt source · line 773 · raw

@+f:F -> F

Math.sqrt, correctly rounded.

def mag_cmp source · line 779 · raw

@+ma:List<&2, U32> -> @+ea:U32 -> @+mb:List<&2, U32> -> @+eb:U32 -> U32

0: a < b, 1: a = b, 2: a > b, 3: unordered (a NaN). -0 equals +0.

def flip source · line 783 · raw

@+c:U32 -> U32

def signed_cmp source · line 786 · raw

@+neg:Bool -> @+c:U32 -> U32

def fin_cmp source · line 789 · raw

@same:Bool -> @+nx:Bool -> @+mx:List<&2, U32> -> @+ex:U32 -> @+my:List<&2, U32> -> @+ey:U32 -> U32

def cmp_dec source · line 796 · raw

@x:Dec -> @y:Dec -> U32

def cmp source · line 831 · raw

@+a:F -> @+b:F -> U32

def from_u32 source · line 834 · raw

@+n:U32 -> F

def limbs32 source · line 837 · raw

@+xs:List<&2, U32> -> U32

def whole_part source · line 844 · raw

@neg_e:Bool -> @+m:List<&2, U32> -> @+e:U32 -> List<&2, U32>

The integer part toward zero, modulo 2^32 (ES ToInt32 / ToUint32 before the sign is read). m * 2^e toward zero (a right shift for e < 0, a left shift otherwise), with the left shift stopped at 64 bits (only the low 32 are read).

def int_bits source · line 851 · raw

@d:Dec -> U32

def to_int32 source · line 863 · raw

@+f:F -> U32

def frac_zero source · line 867 · raw

@neg_e:Bool -> @+m:List<&2, U32> -> @+e:U32 -> Bool

The value as an exact int32, when it is one (-0 is not: 1/-0 is -Infinity).

def exact_i32 source · line 874 · raw

@d:Dec -> Maybe<&2, U32>

def as_i32 source · line 887 · raw

@+f:F -> Maybe<&2, U32>

def log10_2 source · line 893 · raw

@+t:U32 -> U32

floor(t * log10(2)) for |t| < 4096 (78913 / 2^18 ~ 0.30103).

def round_div source · line 896 · raw

@qr:QR -> @+den:List<&2, U32> -> List<&2, U32>

def scaled source · line 903 · raw

@+m:List<&2, U32> -> @+e:U32 -> @+s:U32 -> List<&2, U32>

m * 2^e / 10^s, rounded to nearest (ties to even).

def q_of source · line 909 · raw

@qr:QR -> List<&2, U32>

Digits of a natural, most significant first.

def r_of source · line 914 · raw

@qr:QR -> List<&2, U32>

def digits_go source · line 920 · raw

@k:Nat -> @+x:List<&2, U32> -> @stop:Bool -> @+acc:List<&2, Char> -> List<&2, Char>

Decimal digits of x, most significant first; stop says x is zero.

def n_text source · line 932 · raw

@+x:List<&2, U32> -> String

def fit_code source · line 939 · raw

@+d:List<&2, U32> -> @+p:U32 -> Nat

0: d has p digits; 1: one too many (k too small); 2: too few.

def digits_try source · line 942 · raw

@fuel:Nat -> @code:Nat -> @+m:List<&2, U32> -> @+e:U32 -> @+p:U32 -> @+k:U32 -> @+d:List<&2, U32> -> Digits

def digits_at source · line 957 · raw

@+m:List<&2, U32> -> @+e:U32 -> @+p:U32 -> @+k:U32 -> Digits

def from_dec_go source · line 961 · raw

@frac:Bool -> @+d:List<&2, U32> -> @+s:U32 -> F

def from_dec source · line 970 · raw

@+d:List<&2, U32> -> @+s:U32 -> F

def same_bits source · line 973 · raw

@a:F -> @b:F -> Bool

def k_of source · line 981 · raw

@+m:List<&2, U32> -> @+e:U32 -> U32

The fewest digits (1..17) that read back to |f|.

def digits_back source · line 984 · raw

@dg:Digits -> @+p:U32 -> @+target:F -> Bool

def shortest source · line 991 · raw

@fuel:Nat -> @ok:Bool -> @+dg:Digits -> @+m:List<&2, U32> -> @+e:U32 -> @+p:U32 -> @+target:F -> Digits

The fewest digits (1..17) that read back to |f|: dg holds p digits and ok whether they read back.

def zeros_text source · line 1003 · raw

@k:Nat -> String

def drop_c0 source · line 1006 · raw

@s:String -> String

def strip0 source · line 1013 · raw

@s:String -> String

def layout_go source · line 1016 · raw

@code:Nat -> @+ds:String -> @+n:U32 -> @+p:U32 -> String

def layout source · line 1029 · raw

@+ds:String -> @+n:U32 -> String

ES 9.8.1 Number::toString with digits ds and n = k + 1.

def digits_text source · line 1034 · raw

@dg:Digits -> String

def abs source · line 1039 · raw

@f:F -> F

def f_hi source · line 1044 · raw

@f:F -> U32

def f_lo source · line 1049 · raw

@f:F -> U32

def show_dec source · line 1054 · raw

@d:Dec -> @+f:F -> String

def show source · line 1067 · raw

@+f:F -> String

def show_signed source · line 1071 · raw

@+f:F -> String

As show, but -0 is "-0" (CDP's unserializableValue).

def dig source · line 1079 · raw

@+c:Char -> U32

def lit_class source · line 1085 · raw

@+c:Char -> @+frac:Bool -> @+any:Bool -> Nat

Integer digits, then fraction digits (each lowers the exponent), then the exponent part. 0 digit, 1 point, 2 exponent mark, 3 anything else.

def exp_digits source · line 1089 · raw

@s:String -> @+acc:U32 -> @+any:Bool -> Ex

def neg_ex source · line 1096 · raw

@x:Ex -> @+minus:Bool -> Ex

def read_exp source · line 1101 · raw

@+s:String -> Ex

def with_exp source · line 1106 · raw

@x:Ex -> @+acc:List<&2, U32> -> @+sc:U32 -> Lit

def head_class source · line 1111 · raw

@s:String -> @+frac:Bool -> @+any:Bool -> Nat

def read_int source · line 1119 · raw

@s:String -> @code:Nat -> @+acc:List<&2, U32> -> @+sc:U32 -> @+frac:Bool -> @+any:Bool -> Lit

code is the class of s's first character (lit_class).

def hex_val source · line 1134 · raw

@+c:Char -> U32

def read_hex source · line 1140 · raw

@s:String -> @+base:U32 -> @+acc:List<&2, U32> -> @+any:Bool -> Lit

The digits of an integer in base (16 for 0x, 8 for 0o, 2 for 0b).

def apply_sign source · line 1147 · raw

@+neg:Bool -> @+f:F -> F

def lit_value source · line 1150 · raw

@l:Lit -> @+neg:Bool -> @+hex:Bool -> Maybe<&2, F>

def read source · line 1162 · raw

@+text:String -> Maybe<&2, F>

The numeric value of a decimal, hexadecimal, octal or binary literal ("12", "1.5e-3", ".5", "0x1F", "0o17", "0b101", "Infinity"), signed when signed; None when it is not one.

def bits_text source · line 1171 · raw

@f:F -> String