~/bend-docscommunity

LAWS.bend source

LAWS.bend on the hub · documented module

# The laws of bend-float64. Where a NaN goes is proved for every input (the decision: x86's bits exactly). Correct# rounding is pinned at literal inputs that walk each rounding path (ties, overflow, subnormals, cancellation,# signed zeros), every expected value taken from this machine's hardware; test/check.py compares millions more.# Gate: ./bend-check PROOF.bend --verdictimport Baseimport ./float64.bend as F# ---- NaNs, for every input ----# Guarantees: when a is NaN, a + b, a - b, a * b and a / b are all a with its quiet bit set, whatever b is, in every#   rounding mode.# Why it matters: x86 (and NumPy's scalar path) answer this way; a NaN that changed payload or lost its place to the#   second operand would make bend-float64's NaN bits differ from the hardware's, breaking bit-exact comparison.law nan_first:  for +m: F.Rnd  for +a: F.F64  for +b: F.F64  for +h: {F.is_nan(a) == True{} : Bool}  {F.add_m(m, a, b) == F.quiet(a) : F.F64} & ({F.sub_m(m, a, b) == F.quiet(a) : F.F64} & ({F.mul_m(m, a, b) == F.quiet(a) : F.F64} & {F.div_m(m, a, b) == F.quiet(a) : F.F64}))# Guarantees: when a is not NaN and b is, a + b, a - b, a * b and a / b are all b with its quiet bit set (for a - b,#   b keeps its own sign).# Why it matters: the same x86 rule for the second operand; a subtraction that flipped the NaN's sign, as negating b#   first would, gives different bits from the hardware.law nan_second:  for +m: F.Rnd  for +a: F.F64  for +b: F.F64  for +ha: {F.is_nan(a) == False{} : Bool}  for +hb: {F.is_nan(b) == True{} : Bool}  {F.add_m(m, a, b) == F.quiet(b) : F.F64} & ({F.sub_m(m, a, b) == F.quiet(b) : F.F64} & ({F.mul_m(m, a, b) == F.quiet(b) : F.F64} & {F.div_m(m, a, b) == F.quiet(b) : F.F64}))# Guarantees: fma(a, b, c) with a NaN among its operands is the first NaN of b, a, c, quieted (even for 0 * inf + NaN,#   which is not the default NaN).# Why it matters: this is the order glibc's fma gives on x86-64; another order changes the NaN's bits.law fma_nan_b:  for +m: F.Rnd  for +a: F.F64  for +b: F.F64  for +c: F.F64  for +hb: {F.is_nan(b) == True{} : Bool}  {F.fma_m(m, a, b, c) == F.quiet(b) : F.F64}# Guarantees: (fma_nan_a) when b is not NaN and a is, the result is a quieted.# Why it matters: the second step of glibc's order on x86-64.law fma_nan_a:  for +m: F.Rnd  for +a: F.F64  for +b: F.F64  for +c: F.F64  for +hb: {F.is_nan(b) == False{} : Bool}  for +ha: {F.is_nan(a) == True{} : Bool}  {F.fma_m(m, a, b, c) == F.quiet(a) : F.F64}# Guarantees: (fma_nan_c) when neither a nor b is NaN and c is, the result is c quieted, even when a * b is 0 * inf.# Why it matters: the last step of glibc's order; x86 does not raise the default NaN here.law fma_nan_c:  for +m: F.Rnd  for +a: F.F64  for +b: F.F64  for +c: F.F64  for +hb: {F.is_nan(b) == False{} : Bool}  for +ha: {F.is_nan(a) == False{} : Bool}  for +hc: {F.is_nan(c) == True{} : Bool}  {F.fma_m(m, a, b, c) == F.quiet(c) : F.F64}# Guarantees: the square root and the four roundings to an integer of a NaN are that NaN, quieted.# Why it matters: as x86's sqrtsd and roundsd; a NaN must never turn into a number.law unary_nan:  for +m: F.Rnd  for +a: F.F64  for +h: {F.is_nan(a) == True{} : Bool}  {F.sqrt_m(m, a) == F.quiet(a) : F.F64} & ({F.trunc(a) == F.quiet(a) : F.F64} & ({F.floor(a) == F.quiet(a) : F.F64} & ({F.ceil(a) == F.quiet(a) : F.F64} & {F.rint(a) == F.quiet(a) : F.F64})))# Guarantees: when a is NaN, ==, <, <=, > and >= are all False and != is True.# Why it matters: IEEE comparisons are unordered on NaN; a sort or a mask built on them would otherwise put NaNs#   among the numbers.law compare_nan_a:  for +a: F.F64  for +b: F.F64  for +h: {F.is_nan(a) == True{} : Bool}  {F.eq(a, b) == False{} : Bool} & ({F.lt(a, b) == False{} : Bool} & ({F.le(a, b) == False{} : Bool} & ({F.gt(a, b) == False{} : Bool} & ({F.ge(a, b) == False{} : Bool} & {F.ne(a, b) == True{} : Bool}))))# Guarantees: when b is NaN, ==, <, <=, > and >= are all False and != is True.# Why it matters: the same for the second operand, so no NaN compares as a number in either place.law compare_nan_b:  for +a: F.F64  for +b: F.F64  for +h: {F.is_nan(b) == True{} : Bool}  {F.eq(a, b) == False{} : Bool} & ({F.lt(a, b) == False{} : Bool} & ({F.le(a, b) == False{} : Bool} & ({F.gt(a, b) == False{} : Bool} & ({F.ge(a, b) == False{} : Bool} & {F.ne(a, b) == True{} : Bool}))))# Guarantees: minimum and maximum of a NaN a are a itself, not quieted.# Why it matters: np.minimum and np.maximum propagate NaN this way, bits unchanged; a min that skipped the NaN would#   be np.fmin, another function.law minmax_nan_first:  for +a: F.F64  for +b: F.F64  for +h: {F.is_nan(a) == True{} : Bool}  {F.minimum(a, b) == a : F.F64} & {F.maximum(a, b) == a : F.F64}# Guarantees: minimum and maximum of a non-NaN a and a NaN b are b itself, not quieted.# Why it matters: the same NumPy rule for the second operand.law minmax_nan_second:  for +a: F.F64  for +b: F.F64  for +ha: {F.is_nan(a) == False{} : Bool}  for +hb: {F.is_nan(b) == True{} : Bool}  {F.minimum(a, b) == b : F.F64} & {F.maximum(a, b) == b : F.F64}# Guarantees: quieting sets only the quiet bit (0x7FF0000000000123 -> 0x7FF8000000000123, 0xFFF0000000000001 ->#   0xFFF8000000000001, an already quiet NaN unchanged), and a signalling NaN through an addition comes out so.# Why it matters: the NaN laws above are stated with quiet; this pins quiet itself to the hardware's bits.law quiet_bits:  {[F.quiet(F.of_bits(2146435072, 291)), F.quiet(F.of_bits(4293918720, 1)), F.quiet(F.of_bits(2146959360, 7)), F.add(F.of_bits(2146435072, 291), F.of_bits(1072693248, 0))]    == [F.F64{2146959360, 291}, F.F64{4294443008, 1}, F.F64{2146959360, 7}, F.F64{2146959360, 291}] : List<&2, F.F64>}# ---- rounding, at literal inputs (expected bits from the hardware) ----# Guarantees: 0.1 + 0.2 = 0.30000000000000004; 1 + 2^-53 ties down to 1 and (1 + 2^-52) + 2^-53 ties up to 1 + 2^-51#   (ties to even); max + max and max + half an ulp overflow to infinity, max + a quarter ulp stays max; 0.3 - 0.3#   and -0.3 + 0.3 are +0, -0 + -0 is -0, -0 + +0 is +0; two smallest subnormals add exactly; the smallest normal minus the smallest#   subnormal is the largest subnormal; inf - inf is the default NaN.# Why it matters: these are the cases where an addition is most often wrong: the tie rule, the overflow threshold,#   the sign of an exact zero, and gradual underflow.law add_rounding:  {[F.add(F.of_bits(1069128089, 2576980378), F.of_bits(1070176665, 2576980378)), F.add(F.of_bits(1072693248, 0), F.of_bits(1017118720, 0)),    F.add(F.of_bits(1072693248, 1), F.of_bits(1017118720, 0)), F.add(F.of_bits(2146435071, 4294967295), F.of_bits(2146435071, 4294967295)),    F.add(F.of_bits(2146435071, 4294967295), F.of_bits(2089811968, 0)), F.add(F.of_bits(2146435071, 4294967295), F.of_bits(2088763392, 0)),    F.add(F.of_bits(1070805811, 858993459), F.of_bits(3218289459, 858993459)), F.add(F.of_bits(2147483648, 0), F.of_bits(2147483648, 0)),    F.add(F.of_bits(2147483648, 0), F.of_bits(0, 0)), F.add(F.of_bits(0, 1), F.of_bits(0, 1)),    F.add(F.of_bits(1048576, 0), F.of_bits(2147483648, 1)), F.add(F.of_bits(2146435072, 0), F.of_bits(4293918720, 0)),    F.add(F.of_bits(3218289459, 858993459), F.of_bits(1070805811, 858993459))]    == [F.F64{1070805811, 858993460}, F.F64{1072693248, 0}, F.F64{1072693248, 2}, F.F64{2146435072, 0}, F.F64{2146435072, 0},        F.F64{2146435071, 4294967295}, F.F64{0, 0}, F.F64{2147483648, 0}, F.F64{0, 0}, F.F64{0, 2}, F.F64{1048575, 4294967295},        F.F64{4294443008, 0}, F.F64{0, 0}] : List<&2, F.F64>}# Guarantees: 0.1 * 3 = 0.30000000000000004; the smallest normal halves exactly into a subnormal; the smallest#   subnormal halved ties down to 0, three of them halved tie up to two (even), and times 0.75 rounds up to one;#   max * 2 overflows; -0 * 5 is -0; inf * 0 is the default NaN; (1 + 2^-52)(1 - 2^-53) rounds to 1;#   0.1 * 0.1, (1/3)(1/7), 0.7 * 0.9 and 1.2345678901234567 * 9.87654321098765 round to nearest.# Why it matters: products that leave the normal range must round once, at the subnormal's own precision.law mul_rounding:  {[F.mul(F.of_bits(1069128089, 2576980378), F.of_bits(1074266112, 0)), F.mul(F.of_bits(1048576, 0), F.of_bits(1071644672, 0)),    F.mul(F.of_bits(0, 1), F.of_bits(1071644672, 0)), F.mul(F.of_bits(0, 3), F.of_bits(1071644672, 0)),    F.mul(F.of_bits(0, 1), F.of_bits(1072168960, 0)), F.mul(F.of_bits(2146435071, 4294967295), F.of_bits(1073741824, 0)),    F.mul(F.of_bits(2147483648, 0), F.of_bits(1075052544, 0)), F.mul(F.of_bits(2146435072, 0), F.of_bits(0, 0)),    F.mul(F.of_bits(1072693248, 1), F.of_bits(1072693247, 4294967295)), F.mul(F.of_bits(1069128089, 2576980378), F.of_bits(1069128089, 2576980378)),    F.mul(F.of_bits(1070945621, 1431655765), F.of_bits(1069697316, 2454267026)), F.mul(F.of_bits(1072064102, 1717986918), F.of_bits(1072483532, 3435973837)),    F.mul(F.of_bits(1072939210, 1116494331), F.of_bits(1076084938, 1167159825))]    == [F.F64{1070805811, 858993460}, F.F64{524288, 0}, F.F64{0, 0}, F.F64{0, 2}, F.F64{0, 1}, F.F64{2146435072, 0},        F.F64{2147483648, 0}, F.F64{4294443008, 0}, F.F64{1072693248, 0}, F.F64{1065646817, 1202590844},        F.F64{1067999622, 409044504}, F.F64{1071917301, 3264175145}, F.F64{1076388595, 1644281720}] : List<&2, F.F64>}# Guarantees: 1 / 3 and 1 / 10 round to nearest; 1 / 0 = inf, -1 / 0 = -inf, 0 / 0 = default NaN, 1 / inf = 0;#   the smallest normal / 4 is an exact subnormal; max / 0.5 overflows.# Why it matters: division is the long loop of the core; its remainder decides the rounding.law div_rounding:  {[F.div(F.of_bits(1072693248, 0), F.of_bits(1074266112, 0)), F.div(F.of_bits(1072693248, 0), F.of_bits(0, 0)),    F.div(F.of_bits(3220176896, 0), F.of_bits(0, 0)), F.div(F.of_bits(0, 0), F.of_bits(0, 0)),    F.div(F.of_bits(1072693248, 0), F.of_bits(2146435072, 0)), F.div(F.of_bits(1048576, 0), F.of_bits(1074790400, 0)),    F.div(F.of_bits(2146435071, 4294967295), F.of_bits(1071644672, 0)), F.div(F.of_bits(1072693248, 0), F.of_bits(1076101120, 0))]    == [F.F64{1070945621, 1431655765}, F.F64{2146435072, 0}, F.F64{4293918720, 0}, F.F64{4294443008, 0}, F.F64{0, 0},        F.F64{262144, 0}, F.F64{2146435072, 0}, F.F64{1069128089, 2576980378}] : List<&2, F.F64>}# Guarantees: sqrt(2) and sqrt(0.5) round to nearest, sqrt(4) = 2 exactly, sqrt(-0) = -0, sqrt(-1) = default NaN,#   sqrt of the smallest subnormal is 2^-537 exactly, sqrt(max) rounds to nearest.# Why it matters: an odd exponent (2, 0.5) and a subnormal input take the core's two alignment paths.law sqrt_rounding:  {[F.sqrt(F.of_bits(1073741824, 0)), F.sqrt(F.of_bits(1074790400, 0)), F.sqrt(F.of_bits(2147483648, 0)), F.sqrt(F.of_bits(3220176896, 0)),    F.sqrt(F.of_bits(0, 1)), F.sqrt(F.of_bits(2146435071, 4294967295)), F.sqrt(F.of_bits(1071644672, 0))]    == [F.F64{1073127582, 1719614413}, F.F64{1073741824, 0}, F.F64{2147483648, 0}, F.F64{4294443008, 0}, F.F64{509607936, 0},        F.F64{1609564159, 4294967295}, F.F64{1072079006, 1719614413}] : List<&2, F.F64>}# Guarantees: fma(0.1, 10, -1) is 2^-54 (the exact error of 0.1 * 10, where a multiply then an add gives 0);#   fma(1e-300, -1e-300, 0) is -0; (1 + 2^-52)(1 - 2^-52) - 1 is -2^-104 exactly; max * 2 - max is max (no overflow#   in the middle); a subnormal product ties to even with and without an addend.# Why it matters: fma's whole point is one rounding; a core that rounded the product first passes none of these.law fma_rounding:  {[F.fma(F.of_bits(1069128089, 2576980378), F.of_bits(1076101120, 0), F.of_bits(3220176896, 0)),    F.fma(F.of_bits(27618847, 3271095129), F.of_bits(2175102495, 3271095129), F.of_bits(0, 0)),    F.fma(F.of_bits(1072693248, 1), F.of_bits(1072693247, 4294967294), F.of_bits(3220176896, 0)),    F.fma(F.of_bits(2146435071, 4294967295), F.of_bits(1073741824, 0), F.of_bits(4293918719, 4294967295)),    F.fma(F.of_bits(0, 1), F.of_bits(1071644672, 0), F.of_bits(0, 0)), F.fma(F.of_bits(0, 1), F.of_bits(1071644672, 0), F.of_bits(0, 1))]    == [F.F64{1016070144, 0}, F.F64{2147483648, 0}, F.F64{3111124992, 0}, F.F64{2146435071, 4294967295}, F.F64{0, 0},        F.F64{0, 2}] : List<&2, F.F64>}# Guarantees: rint ties to even (0.5 -> 0, 1.5 -> 2, 2.5 -> 2, -0.5 -> -0, -2.5 -> -2, 2^52 - 0.5 -> 2^52) and rounds#   0.75 to 1; floor(-0.5) = -1, ceil(-0.5) = -0, ceil(0.25) = 1, trunc(-1.75) = -1,#   floor(-1.5) = -2, ceil(1.25) = 2; an integer beyond 2^52 is kept.# Why it matters: np.round is rint; rounding halves away from zero, the schoolbook rule, gives different data.law to_integer:  {[F.rint(F.of_bits(1071644672, 0)), F.rint(F.of_bits(1073217536, 0)), F.rint(F.of_bits(1074003968, 0)), F.rint(F.of_bits(3219128320, 0)),    F.rint(F.of_bits(3221487616, 0)), F.rint(F.of_bits(1072168960, 0)), F.rint(F.of_bits(1127219199, 4294967295)),    F.floor(F.of_bits(3219128320, 0)), F.ceil(F.of_bits(3219128320, 0)), F.ceil(F.of_bits(1070596096, 0)),    F.trunc(F.of_bits(3220963328, 0)), F.floor(F.of_bits(3274702848, 1)), F.floor(F.of_bits(3220701184, 0)), F.ceil(F.of_bits(1072955392, 0))]    == [F.F64{0, 0}, F.F64{1073741824, 0}, F.F64{1073741824, 0}, F.F64{2147483648, 0}, F.F64{3221225472, 0},        F.F64{1072693248, 0}, F.F64{1127219200, 0}, F.F64{3220176896, 0}, F.F64{2147483648, 0}, F.F64{1072693248, 0},        F.F64{3220176896, 0}, F.F64{3274702848, 1}, F.F64{3221225472, 0}, F.F64{1073741824, 0}] : List<&2, F.F64>}# Guarantees: to_i32 truncates (-2.5 -> -2, 2147483647.9 -> 2147483647) and gives -2^31 for 2^31, 3e9 and NaN; to_u32#   gives -1.0 -> 4294967295, 4294967295.5 -> 4294967295, 2^32 -> 0, 1e20 -> 0 (NumPy's astype on x86-64);#   to_f32 ties to even (1 + 2^-24 -> 1, 1 + 3 * 2^-24 -> 1 + 2^-22), overflows to infinity past float32's#   rounding threshold, flushes 1e-46 to 0 and rounds just over half the smallest subnormal up to it, keeps -0, and#   keeps a NaN's top payload bits.# Why it matters: conversions are where values silently wrap or saturate; these pin the choice to the hardware's.law conversions:  {[F.to_i32(F.of_bits(3221487616, 0)), F.to_i32(F.of_bits(1105199103, 4294547866)), F.to_i32(F.of_bits(1105199104, 0)),    F.to_i32(F.of_bits(2146959360, 0)), F.to_i32(F.of_bits(1105615371, 3221225472)), F.to_u32(F.of_bits(3220176896, 0)), F.to_u32(F.of_bits(1106247679, 4293918720)),    F.to_u32(F.of_bits(1106247680, 0)), F.to_u32(F.of_bits(1142271773, 2025163840)),    F.to_f32_bits(F.of_bits(1072693248, 268435456)), F.to_f32_bits(F.of_bits(1072693248, 805306368)),    F.to_f32_bits(F.of_bits(1206910975, 4026531840)), F.to_f32_bits(F.of_bits(912409806, 606885217)),    F.to_f32_bits(F.of_bits(915406848, 1)), F.to_f32_bits(F.of_bits(2147483648, 0)), F.to_f32_bits(F.of_bits(4293918720, 536870913))]    == [4294967294, 2147483647, 2147483648, 2147483648, 2147483648, 4294967295, 4294967295, 0, 0,        1065353216, 1065353218, 2139095040, 0, 1, 2147483648, 4290772993] : List<&2, U32>}# Guarantees: from_i32(-2^31) is -2147483648.0 exactly; from_f32 of a signalling NaN is quiet with the payload moved#   up; from_f32 of the smallest float32 subnormal is 2^-149 exactly.# Why it matters: widening must never change a value; only a NaN's quiet bit changes, as on the hardware.law widening:  {[F.from_i32(2147483648), F.from_f32_bits(2139095041), F.from_f32_bits(1)]    == [F.F64{3252682752, 0}, F.F64{2146959360, 536870912}, F.F64{916455424, 0}] : List<&2, F.F64>}# Guarantees: -0 == +0, -0 < +0 is False, -0 <= +0; minimum(+0, -0) is -0 and minimum(-0, +0) is +0 (the second of#   two equal values), and the same for maximum.# Why it matters: these are NumPy's answers on signed zeros; a minimum that compared bits would order -0 below +0.law signed_zeros:  {[F.eq(F.of_bits(2147483648, 0), F.of_bits(0, 0)), F.lt(F.of_bits(2147483648, 0), F.of_bits(0, 0)), F.le(F.of_bits(2147483648, 0), F.of_bits(0, 0))]    == [True{}, False{}, True{}] : List<&2, Bool>}  & {[F.minimum(F.of_bits(0, 0), F.of_bits(2147483648, 0)), F.minimum(F.of_bits(2147483648, 0), F.of_bits(0, 0)),      F.maximum(F.of_bits(0, 0), F.of_bits(2147483648, 0)), F.maximum(F.of_bits(2147483648, 0), F.of_bits(0, 0))]    == [F.F64{2147483648, 0}, F.F64{0, 0}, F.F64{2147483648, 0}, F.F64{0, 0}] : List<&2, F.F64>}