~/bend-docscommunity

float64.bend checks

raw source on the hub · import bend-float64@0.1.8.0/float64.bend as Float64

bend-float64: IEEE 754 binary64 ("double", NumPy's float64) in pure Bend, as two U32 words and integer arithmetic. Every operation is correctly rounded (to nearest, ties to even; the _m versions take any of IEEE 754's five rounding modes) and gives the bits x86-64 gives, NaNs included: a NaN operand passes through quieted (for two, the first; fma takes b, then a, then c), and an invalid operation (inf - inf, 0 * inf, 0 / 0, inf / inf, the square root of a negative number) gives the default NaN 0xFFF8000000000000. minimum and maximum follow NumPy's np.minimum and np.maximum; the conversions to integers follow NumPy's astype on x86-64.

2 imports
import Base
import ./wide.bend as W

Types

type F64 source · line 11 · raw

Data

type Cls source · line 107 · raw

Data

a finite nonzero x is m * 2^(e - 1075): m < 2^53, e >= 1 (a subnormal has e = 1 and m < 2^52)

type NM source · line 127 · raw

Data

m * 2^(e - 1075) with m's top bit at bit 52

type Rnd source · line 138 · raw

Data

IEEE 754's five rounding modes: to nearest with ties to even (the default everywhere), to nearest with ties away from zero, toward zero, toward +infinity, toward -infinity

type QR source · line 358 · raw

Data

type Prod source · line 465 · raw

Data

type RMode source · line 678 · raw

Data

Definitions

def of_bits source · line 17 · raw

@+hi:U32 -> @+lo:U32 -> F64

the float64 with these words (hi holds the sign, the exponent and the top 20 bits of the fraction)

def hi_bits source · line 20 · raw

@x:F64 -> U32

def lo_bits source · line 25 · raw

@x:F64 -> U32

def sign_of source · line 30 · raw

@+h:U32 -> U32

def exp_of source · line 33 · raw

@+h:U32 -> U32

def frac_hi source · line 36 · raw

@+h:U32 -> U32

def zero source · line 39 · raw

@+s:U32 -> F64

def inf source · line 42 · raw

@+s:U32 -> F64

def dnan source · line 46 · raw

F64

x86's default NaN, the answer to an invalid operation

def quiet source · line 50 · raw

@x:F64 -> F64

a NaN with its quiet bit set

def neg source · line 55 · raw

@x:F64 -> F64

def abs source · line 60 · raw

@x:F64 -> F64

def nan_bits source · line 65 · raw

@+h:U32 -> @+l:U32 -> Bool

def is_nan source · line 68 · raw

@x:F64 -> Bool

def is_inf source · line 73 · raw

@x:F64 -> Bool

def is_finite source · line 78 · raw

@x:F64 -> Bool

def signbit source · line 84 · raw

@x:F64 -> Bool

the sign bit (True for -0.0 and negative NaNs too)

def with_sign source · line 89 · raw

@+s:U32 -> @x:F64 -> F64

def copysign source · line 95 · raw

@x:F64 -> @y:F64 -> F64

x's magnitude with y's sign bit

def s_lt source · line 101 · raw

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

a < b for exponents held as signed 32-bit words

def cls_fin source · line 113 · raw

@+s:U32 -> @+e:U32 -> @+fh:U32 -> @+lo:U32 -> Cls

def cls_parts source · line 116 · raw

@+s:U32 -> @+e:U32 -> @+fh:U32 -> @+lo:U32 -> Cls

def cls source · line 121 · raw

@x:F64 -> Cls

def norm source · line 130 · raw

@+e:U32 -> @+m:0x72aa159312099184b98e33378b057a0a/wide.W64 -> NM

def of_w source · line 145 · raw

@x:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

def rp_bits source · line 150 · raw

@+s:U32 -> @+e:U32 -> @m:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

def rnd_up source · line 155 · raw

@m:Rnd -> @+s:U32 -> @+rb:U32 -> @+odd:Bool -> Bool

whether to round the kept bits up, given the 9 bits below them (rb, the lowest one sticky), the sign, and whether the kept bits are odd

def rp_round source · line 170 · raw

@+rm:Rnd -> @+s:U32 -> @+e:U32 -> @+sig:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

S's top bit at bit 61, e >= 1: keep 53 bits, round on the 9 below as the mode says, pack; a carry out of the significand moves into the exponent, and past the largest exponent gives infinity

def overflow source · line 175 · raw

@m:Rnd -> @+s:U32 -> F64

past the largest float64: infinity, or the largest finite number when the mode rounds toward zero on that side

def underflow source · line 190 · raw

@m:Rnd -> @+s:U32 -> F64

a nonzero value too small for any float64 (below half the smallest subnormal): zero, or the smallest subnormal when the mode rounds away from zero on that side

def zero_sum source · line 200 · raw

@m:Rnd -> U32

the sign of an exact zero sum of opposite operands: -0 rounding down, +0 otherwise

def rp_range source · line 207 · raw

@+rm:Rnd -> @over:Bool -> @tiny:Bool -> @+s:U32 -> @+e:U32 -> @+sig:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

def rp_at source · line 216 · raw

@+rm:Rnd -> @+s:U32 -> @+e:U32 -> @+sig:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

def rp_norm source · line 219 · raw

@+rm:Rnd -> @small:Bool -> @+s:U32 -> @+e:U32 -> @+sig:0x72aa159312099184b98e33378b057a0a/wide.W64 -> @+n:U32 -> F64

def round_pack_m source · line 226 · raw

@+rm:Rnd -> @+s:U32 -> @+e:U32 -> @+sig:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

def round_or_zero source · line 230 · raw

@+rm:Rnd -> @z:Bool -> @+s:U32 -> @+e:U32 -> @+sig:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

def add_sum source · line 239 · raw

@+rm:Rnd -> @same:Bool -> @+s:U32 -> @+e:U32 -> @+x:0x72aa159312099184b98e33378b057a0a/wide.W64 -> @+y:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

def add_mag source · line 247 · raw

@+rm:Rnd -> @+sx:U32 -> @+ex:U32 -> @+mx:0x72aa159312099184b98e33378b057a0a/wide.W64 -> @+sy:U32 -> @+d:U32 -> @+my:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

|x| >= |y|: x's exponent ex, the exponents' difference d

def add_ord source · line 250 · raw

@+rm:Rnd -> @a_big:Bool -> @+sa:U32 -> @+ea:U32 -> @+ma:0x72aa159312099184b98e33378b057a0a/wide.W64 -> @+sb:U32 -> @+eb:U32 -> @+mb:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

def add_fin source · line 257 · raw

@+rm:Rnd -> @+sa:U32 -> @+ea:U32 -> @+ma:0x72aa159312099184b98e33378b057a0a/wide.W64 -> @+sb:U32 -> @+eb:U32 -> @+mb:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

def add_c source · line 260 · raw

@+rm:Rnd -> @ca:Cls -> @cb:Cls -> @+a:F64 -> @+b:F64 -> F64

def add_n source · line 279 · raw

@+rm:Rnd -> @na:Bool -> @nb:Bool -> @+a:F64 -> @+b:F64 -> F64

def add_m source · line 289 · raw

@+rm:Rnd -> @+a:F64 -> @+b:F64 -> F64

a + b, rounded as m says

def sub_n source · line 292 · raw

@+rm:Rnd -> @na:Bool -> @nb:Bool -> @+a:F64 -> @+b:F64 -> F64

def sub_m source · line 302 · raw

@+rm:Rnd -> @+a:F64 -> @+b:F64 -> F64

a - b, rounded as m says (a NaN b passes through with its own sign)

def mul_hi source · line 307 · raw

@p:0x72aa159312099184b98e33378b057a0a/wide.W128 -> 0x72aa159312099184b98e33378b057a0a/wide.W64

def mul_nm source · line 312 · raw

@+rm:Rnd -> @+s:U32 -> @a:NM -> @b:NM -> F64

def mul_fin source · line 317 · raw

@+rm:Rnd -> @+s:U32 -> @+ea:U32 -> @+ma:0x72aa159312099184b98e33378b057a0a/wide.W64 -> @+eb:U32 -> @+mb:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

def mul_c source · line 320 · raw

@+rm:Rnd -> @ca:Cls -> @cb:Cls -> F64

def mul_n source · line 343 · raw

@+rm:Rnd -> @na:Bool -> @nb:Bool -> @+a:F64 -> @+b:F64 -> F64

def mul_m source · line 353 · raw

@+rm:Rnd -> @+a:F64 -> @+b:F64 -> F64

a * b, rounded as m says

def div_go source · line 362 · raw

@n:Nat -> @+rem:0x72aa159312099184b98e33378b057a0a/wide.W64 -> @+d:0x72aa159312099184b98e33378b057a0a/wide.W64 -> @+q:0x72aa159312099184b98e33378b057a0a/wide.W64 -> QR

n steps of long division: q gains one bit a step

def qr_sticky source · line 370 · raw

@r:QR -> 0x72aa159312099184b98e33378b057a0a/wide.W64

def div_nm source · line 376 · raw

@+rm:Rnd -> @+s:U32 -> @a:NM -> @b:NM -> F64

q = floor(ma * 2^62 / mb), between 2^61 and 2^63

def div_fin source · line 381 · raw

@+rm:Rnd -> @+s:U32 -> @+ea:U32 -> @+ma:0x72aa159312099184b98e33378b057a0a/wide.W64 -> @+eb:U32 -> @+mb:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

def div_c source · line 384 · raw

@+rm:Rnd -> @ca:Cls -> @cb:Cls -> F64

def div_n source · line 407 · raw

@+rm:Rnd -> @na:Bool -> @nb:Bool -> @+a:F64 -> @+b:F64 -> F64

def div_m source · line 417 · raw

@+rm:Rnd -> @+a:F64 -> @+b:F64 -> F64

a / b, rounded as m says

def sqrt_go source · line 423 · raw

@n:Nat -> @+x:0x72aa159312099184b98e33378b057a0a/wide.W64 -> @+rem:0x72aa159312099184b98e33378b057a0a/wide.W64 -> @+q:0x72aa159312099184b98e33378b057a0a/wide.W64 -> QR

n steps of the digit-by-digit square root: two bits of x in, one bit of q out

def sqrt_nm source · line 434 · raw

@+rm:Rnd -> @a:NM -> F64

m * 2^(e - 1075) with an even power of two: q = floor(sqrt(m' * 2^70)), between 2^61 and 2^62

def sqrt_c source · line 441 · raw

@+rm:Rnd -> @c:Cls -> @+a:F64 -> F64

def sqrt_n source · line 452 · raw

@+rm:Rnd -> @na:Bool -> @+a:F64 -> F64

def sqrt_m source · line 460 · raw

@+rm:Rnd -> @+a:F64 -> F64

the square root, rounded as m says (of -0.0 is -0.0)

def prod_of source · line 471 · raw

@ca:Cls -> @cb:Cls -> Prod

def fma_pack source · line 495 · raw

@+rm:Rnd -> @+s:U32 -> @+sc:U32 -> @+r:0x72aa159312099184b98e33378b057a0a/wide.W128 -> F64

R nonzero: R * 2^sc as s * S * 2^(e - 1084), S of at most 62 bits with the bits below jammed

def fma_zero source · line 501 · raw

@+rm:Rnd -> @z:Bool -> @+s:U32 -> @+sc:U32 -> @+r:0x72aa159312099184b98e33378b057a0a/wide.W128 -> F64

def fma_diff source · line 508 · raw

@+rm:Rnd -> @x_small:Bool -> @+sx:U32 -> @+sy:U32 -> @+sc:U32 -> @+x:0x72aa159312099184b98e33378b057a0a/wide.W128 -> @+y:0x72aa159312099184b98e33378b057a0a/wide.W128 -> F64

def fma_sum source · line 516 · raw

@+rm:Rnd -> @same:Bool -> @+sx:U32 -> @+sy:U32 -> @+sc:U32 -> @+x:0x72aa159312099184b98e33378b057a0a/wide.W128 -> @+y:0x72aa159312099184b98e33378b057a0a/wide.W128 -> F64

x the operand at the larger scale sc, y aligned to it

def fma_ord source · line 523 · raw

@+rm:Rnd -> @p_big:Bool -> @+sp:U32 -> @+scp:U32 -> @+p:0x72aa159312099184b98e33378b057a0a/wide.W128 -> @+sc:U32 -> @+scc:U32 -> @+c:0x72aa159312099184b98e33378b057a0a/wide.W128 -> F64

def fma_nm source · line 531 · raw

@+rm:Rnd -> @+sp:U32 -> @a:NM -> @b:NM -> @+sc:U32 -> @c:NM -> F64

the product P = ma * mb (below 2^106) as P * 2^20 at scale ea + eb - 2170, c's mc * 2^72 at scale ec - 1147

def fma_c source · line 538 · raw

@+rm:Rnd -> @p:Prod -> @cc:Cls -> @+c:F64 -> F64

def fma_n source · line 559 · raw

@+rm:Rnd -> @nb:Bool -> @na:Bool -> @nc:Bool -> @+a:F64 -> @+b:F64 -> @+c:F64 -> F64

def fma_m source · line 571 · raw

@+rm:Rnd -> @+a:F64 -> @+b:F64 -> @+c:F64 -> F64

a * b + c with one rounding, as m says

def round_pack source · line 577 · raw

@+s:U32 -> @+e:U32 -> @+sig:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

def add source · line 581 · raw

@+a:F64 -> @+b:F64 -> F64

a + b

def sub source · line 585 · raw

@+a:F64 -> @+b:F64 -> F64

a - b (a NaN b passes through with its own sign)

def mul source · line 589 · raw

@+a:F64 -> @+b:F64 -> F64

a * b

def div source · line 593 · raw

@+a:F64 -> @+b:F64 -> F64

a / b

def sqrt source · line 597 · raw

@+a:F64 -> F64

the square root (of -0.0 is -0.0)

def fma source · line 601 · raw

@+a:F64 -> @+b:F64 -> @+c:F64 -> F64

a * b + c with one rounding

def both_zero source · line 606 · raw

@+ah:U32 -> @+al:U32 -> @+bh:U32 -> @+bl:U32 -> Bool

def eq_bits source · line 609 · raw

@a:F64 -> @b:F64 -> Bool

def lt_sign source · line 614 · raw

@na:Bool -> @nb:Bool -> @+ah:U32 -> @+al:U32 -> @+bh:U32 -> @+bl:U32 -> Bool

def lt_bits source · line 625 · raw

@a:F64 -> @b:F64 -> Bool

def ordered source · line 630 · raw

@na:Bool -> @nb:Bool -> @r:Bool -> Bool

def eq source · line 638 · raw

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

a == b

def ne source · line 642 · raw

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

a != b (True when either is NaN)

def lt source · line 646 · raw

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

a < b

def le source · line 650 · raw

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

a <= b

def gt source · line 653 · raw

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

def ge source · line 656 · raw

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

def pick_nan source · line 659 · raw

@na:Bool -> @nb:Bool -> @+a:F64 -> @+b:F64 -> @r:F64 -> F64

def minimum source · line 669 · raw

@+a:F64 -> @+b:F64 -> F64

NumPy's np.minimum: a NaN (the first if both) passes through as it is; otherwise a if a < b, else b

def maximum source · line 673 · raw

@+a:F64 -> @+b:F64 -> F64

NumPy's np.maximum: a NaN (the first if both) passes through as it is; otherwise a if a > b, else b

def one_hi source · line 684 · raw

U32

def ri_small source · line 688 · raw

@m:RMode -> @+s:U32 -> @+e:U32 -> @+f:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

|x| < 1, x not zero

def ri_up source · line 699 · raw

@m:RMode -> @+s:U32 -> @+fr:0x72aa159312099184b98e33378b057a0a/wide.W64 -> @+half:0x72aa159312099184b98e33378b057a0a/wide.W64 -> @+odd:Bool -> Bool

def ri_mid source · line 711 · raw

@m:RMode -> @+s:U32 -> @+u:U32 -> @+bits:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

1 <= |x| < 2^52: the low u = 1075 - e bits of x's bits are the fraction

def ri_parts source · line 718 · raw

@+m:RMode -> @+s:U32 -> @+e:U32 -> @+h:U32 -> @+l:U32 -> F64

x not NaN

def ri_bits source · line 723 · raw

@m:RMode -> @x:F64 -> F64

def ri_n source · line 728 · raw

@na:Bool -> @m:RMode -> @+x:F64 -> F64

def round_int source · line 736 · raw

@m:RMode -> @+x:F64 -> F64

x rounded to an integer in mode m (a NaN passes through quieted)

def trunc source · line 740 · raw

@x:F64 -> F64

toward zero (np.trunc)

def floor source · line 744 · raw

@x:F64 -> F64

toward minus infinity (np.floor)

def ceil source · line 748 · raw

@x:F64 -> F64

toward plus infinity (np.ceil)

def rint source · line 752 · raw

@x:F64 -> F64

to the nearest integer, ties to even (np.rint, np.round)

def from_mag source · line 757 · raw

@+s:U32 -> @+x:U32 -> F64

def from_i32 source · line 761 · raw

@+x:U32 -> F64

a signed 32-bit integer, exactly

def from_u32 source · line 766 · raw

@+x:U32 -> F64

an unsigned 32-bit integer, exactly

def apply_sign source · line 769 · raw

@+s:U32 -> @+x:U32 -> U32

def sig_of source · line 772 · raw

@+h:U32 -> @+l:U32 -> 0x72aa159312099184b98e33378b057a0a/wide.W64

def to_i32 source · line 776 · raw

@x:F64 -> U32

toward zero, as a signed 32-bit integer; NaN and values out of range give -2^31 (x86's "integer indefinite")

def to_u32 source · line 785 · raw

@x:F64 -> U32

toward zero, as NumPy's astype(uint32) on x86-64 does it: through a signed 64-bit integer, keeping its low 32 bits (so -1.0 gives 4294967295), and 0 for NaN and values outside the 64-bit range

def f32_bits source · line 792 · raw

@x:F32 -> U32

def bits_f32 source · line 797 · raw

@x:U32 -> F32

def from_f32_bits source · line 803 · raw

@+u:U32 -> F64

a float32's bits to the float64 of the same value, exactly (a NaN keeps its payload, quieted)

def from_f32 source · line 813 · raw

@x:F32 -> F64

a float32, exactly

def rnd_up_g source · line 818 · raw

@m:Rnd -> @+s:U32 -> @+above:Bool -> @+half:Bool -> @+any:Bool -> @+odd:Bool -> Bool

whether to round up, from how the bits below compare with half (above, exactly half), whether any is set, the sign and whether the kept bits are odd

def r32_round source · line 832 · raw

@+rm:Rnd -> @+s:U32 -> @+e32:U32 -> @sig:0x72aa159312099184b98e33378b057a0a/wide.W64 -> U32

S's top bit at bit 61, the value S * 2^(e32 - 188), e32 >= 1: round to 24 bits on the 38 below, as rm says

def overflow32 source · line 842 · raw

@m:Rnd -> @+s:U32 -> U32

past the largest float32: infinity, or the largest finite float32 when the mode rounds toward zero on that side

def r32_range source · line 855 · raw

@+rm:Rnd -> @over:Bool -> @tiny:Bool -> @+s:U32 -> @+e32:U32 -> @+sig:0x72aa159312099184b98e33378b057a0a/wide.W64 -> U32

def to_f32_nm source · line 864 · raw

@+rm:Rnd -> @+s:U32 -> @n:NM -> U32

def to_f32_c source · line 870 · raw

@+rm:Rnd -> @c:Cls -> @+h:U32 -> @+l:U32 -> U32

def to_f32_bits_m source · line 882 · raw

@+rm:Rnd -> @x:F64 -> U32

the float32 bits of x rounded as rm says (a NaN keeps the top of its payload, quieted)

def to_f32_bits source · line 888 · raw

@x:F64 -> U32

the float32 bits nearest x (ties to even; a NaN keeps the top of its payload, quieted)

def to_f32_m source · line 892 · raw

@+rm:Rnd -> @x:F64 -> F32

x rounded to a float32 as rm says

def to_f32 source · line 896 · raw

@x:F64 -> F32

the float32 nearest x

def w_neg source · line 901 · raw

@+x:0x72aa159312099184b98e33378b057a0a/wide.W64 -> 0x72aa159312099184b98e33378b057a0a/wide.W64

def from_u64_m source · line 905 · raw

@+rm:Rnd -> @+x:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

an unsigned 64-bit integer, rounded as rm says

def from_i64_m source · line 909 · raw

@+rm:Rnd -> @+x:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

a signed 64-bit integer, rounded as rm says

def from_u64 source · line 914 · raw

@+x:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

an unsigned 64-bit integer, rounded to nearest (ties to even)

def from_i64 source · line 918 · raw

@+x:0x72aa159312099184b98e33378b057a0a/wide.W64 -> F64

a signed 64-bit integer, rounded to nearest (ties to even)

def i64_mag source · line 921 · raw

@+h:U32 -> @+l:U32 -> @+e:U32 -> 0x72aa159312099184b98e33378b057a0a/wide.W64

def to_i64 source · line 925 · raw

@x:F64 -> 0x72aa159312099184b98e33378b057a0a/wide.W64

toward zero, as a signed 64-bit integer; NaN and values outside [-2^63, 2^63) give -2^63 (x86's "integer indefinite")

def flip_top source · line 933 · raw

@x:0x72aa159312099184b98e33378b057a0a/wide.W64 -> 0x72aa159312099184b98e33378b057a0a/wide.W64

def to_u64 source · line 941 · raw

@+x:F64 -> 0x72aa159312099184b98e33378b057a0a/wide.W64

toward zero, as NumPy's astype(uint64) on x86-64 does it: below 2^63 through a signed 64-bit integer (so -1.0 gives 2^64 - 1, and NaN and values below -2^63 give 2^63); from 2^63 as x - 2^63 with the top bit set (so values from 2^64 up, and inf, give 0)