~/bend-docscommunity

float64.bend checks

raw source on the hub · import bend-float64@0.1.2.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) 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 10 · raw

Data

type Cls source · line 106 · 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 126 · raw

Data

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

type QR source · line 306 · raw

Data

type Prod source · line 413 · raw

Data

type RMode source · line 596 · raw

Data

Definitions

def of_bits source · line 16 · 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 19 · raw

@x:F64 -> U32

def lo_bits source · line 24 · raw

@x:F64 -> U32

def sign_of source · line 29 · raw

@+h:U32 -> U32

def exp_of source · line 32 · raw

@+h:U32 -> U32

def frac_hi source · line 35 · raw

@+h:U32 -> U32

def zero source · line 38 · raw

@+s:U32 -> F64

def inf source · line 41 · raw

@+s:U32 -> F64

def dnan source · line 45 · raw

F64

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

def quiet source · line 49 · raw

@x:F64 -> F64

a NaN with its quiet bit set

def neg source · line 54 · raw

@x:F64 -> F64

def abs source · line 59 · raw

@x:F64 -> F64

def nan_bits source · line 64 · raw

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

def is_nan source · line 67 · raw

@x:F64 -> Bool

def is_inf source · line 72 · raw

@x:F64 -> Bool

def is_finite source · line 77 · raw

@x:F64 -> Bool

def signbit source · line 83 · raw

@x:F64 -> Bool

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

def with_sign source · line 88 · raw

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

def copysign source · line 94 · raw

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

x's magnitude with y's sign bit

def s_lt source · line 100 · raw

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

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

def cls_fin source · line 112 · raw

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

def cls_parts source · line 115 · raw

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

def cls source · line 120 · raw

@x:F64 -> Cls

def norm source · line 129 · raw

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

def of_w source · line 135 · raw

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

def rp_bits source · line 140 · raw

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

def rp_tie source · line 143 · raw

@tie:Bool -> @m:0x3623a86835ccb58d523b8156ac4abb92/wide.W64 -> 0x3623a86835ccb58d523b8156ac4abb92/wide.W64

def rp_round source · line 152 · raw

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

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

def rp_range source · line 155 · raw

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

def rp_at source · line 164 · raw

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

def rp_norm source · line 167 · raw

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

def round_pack source · line 174 · raw

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

def round_or_zero source · line 178 · raw

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

def add_sum source · line 187 · raw

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

def add_mag source · line 195 · raw

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

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

def add_ord source · line 198 · raw

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

def add_fin source · line 205 · raw

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

def add_c source · line 208 · raw

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

def add_n source · line 227 · raw

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

def add source · line 237 · raw

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

a + b

def sub_n source · line 240 · raw

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

def sub source · line 250 · raw

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

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

def mul_hi source · line 255 · raw

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

def mul_nm source · line 260 · raw

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

def mul_fin source · line 265 · raw

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

def mul_c source · line 268 · raw

@ca:Cls -> @cb:Cls -> F64

def mul_n source · line 291 · raw

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

def mul source · line 301 · raw

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

a * b

def div_go source · line 310 · raw

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

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

def qr_sticky source · line 318 · raw

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

def div_nm source · line 324 · raw

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

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

def div_fin source · line 329 · raw

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

def div_c source · line 332 · raw

@ca:Cls -> @cb:Cls -> F64

def div_n source · line 355 · raw

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

def div source · line 365 · raw

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

a / b

def sqrt_go source · line 371 · raw

@n:Nat -> @+x:0x3623a86835ccb58d523b8156ac4abb92/wide.W64 -> @+rem:0x3623a86835ccb58d523b8156ac4abb92/wide.W64 -> @+q:0x3623a86835ccb58d523b8156ac4abb92/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 382 · raw

@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 389 · raw

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

def sqrt_n source · line 400 · raw

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

def sqrt source · line 408 · raw

@+a:F64 -> F64

the square root (of -0.0 is -0.0)

def prod_of source · line 419 · raw

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

def fma_pack source · line 443 · raw

@+s:U32 -> @+sc:U32 -> @+r:0x3623a86835ccb58d523b8156ac4abb92/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 449 · raw

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

def fma_diff source · line 456 · raw

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

def fma_sum source · line 464 · raw

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

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

def fma_ord source · line 471 · raw

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

def fma_nm source · line 479 · raw

@+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 486 · raw

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

def fma_n source · line 507 · raw

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

def fma source · line 519 · raw

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

a * b + c with one rounding

def both_zero source · line 524 · raw

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

def eq_bits source · line 527 · raw

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

def lt_sign source · line 532 · raw

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

def lt_bits source · line 543 · raw

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

def ordered source · line 548 · raw

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

def eq source · line 556 · raw

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

a == b

def ne source · line 560 · raw

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

a != b (True when either is NaN)

def lt source · line 564 · raw

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

a < b

def le source · line 568 · raw

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

a <= b

def gt source · line 571 · raw

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

def ge source · line 574 · raw

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

def pick_nan source · line 577 · raw

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

def minimum source · line 587 · 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 591 · 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 602 · raw

U32

def ri_small source · line 606 · raw

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

|x| < 1, x not zero

def ri_up source · line 617 · raw

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

def ri_mid source · line 629 · raw

@m:RMode -> @+s:U32 -> @+u:U32 -> @+bits:0x3623a86835ccb58d523b8156ac4abb92/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 636 · raw

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

x not NaN

def ri_bits source · line 641 · raw

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

def ri_n source · line 646 · raw

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

def round_int source · line 654 · raw

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

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

def trunc source · line 658 · raw

@x:F64 -> F64

toward zero (np.trunc)

def floor source · line 662 · raw

@x:F64 -> F64

toward minus infinity (np.floor)

def ceil source · line 666 · raw

@x:F64 -> F64

toward plus infinity (np.ceil)

def rint source · line 670 · raw

@x:F64 -> F64

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

def from_mag source · line 675 · raw

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

def from_i32 source · line 679 · raw

@+x:U32 -> F64

a signed 32-bit integer, exactly

def from_u32 source · line 684 · raw

@+x:U32 -> F64

an unsigned 32-bit integer, exactly

def apply_sign source · line 687 · raw

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

def sig_of source · line 690 · raw

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

def to_i32 source · line 694 · 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 703 · 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 710 · raw

@x:F32 -> U32

def bits_f32 source · line 715 · raw

@x:U32 -> F32

def from_f32_bits source · line 721 · 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 731 · raw

@x:F32 -> F64

a float32, exactly

def r32_round source · line 735 · raw

@+s:U32 -> @+e32:U32 -> @sig:0x3623a86835ccb58d523b8156ac4abb92/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

def r32_range source · line 743 · raw

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

def to_f32_nm source · line 752 · raw

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

def to_f32_c source · line 758 · raw

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

def to_f32_bits source · line 770 · raw

@x:F64 -> U32

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

def to_f32 source · line 776 · raw

@x:F64 -> F32

the float32 nearest x