# The laws of bend-ndarray's float64 dtype, at literal inputs, in a gate of its own (with LAWS.bend's they came too # close to the checker's 10-second limit): NumPy's floor_divide and remainder, and each element's two cells kept # together through the operations that move elements. Values are tested against NumPy by test/f64_check.py. # Gate: ./bend-check F64_PROOF.bend --verdict import Base import ./shape.bend as S import ./ndarray.bend as NP import ./tree.bend as T import ./cells64.bend as C64 import bend-float64@0.1.8.0/float64.bend as F def lit(+dt: S.DType, +s: List<&2, Nat>, +xs: List<&2, U32>) -> NP.ND: NP.ND{dt, s, T.from_rows(NP.ps(dt, s), xs)} def cells_r(r: Result<&1, &1, S.NdErr, NP.ND>) -> Maybe<&2, List<&2, U32>>: match r: case Done{a}: Some{NP.to_list(a)} case Fail{e}: None{} # a float64 (3, 2) array: 1, 2, 3, 4, 5, 6 def a32() -> NP.ND: lit(NP.f64(), [3n, 2n], [1072693248, 0, 1073741824, 0, 1074266112, 0, 1074790400, 0, 1075052544, 0, 1075314688, 0]) # Guarantees: float64 floor_divide and remainder are NumPy's (Python's // and %): 7.5 // -2 = -4 and 7.5 % -2 = -0.5, # -7.5 // 2 = -4 and -7.5 % 2 = 0.5, -0.0 // 3 = -0.0 and -0.0 % 3 = +0.0, 5 // 0 = inf and 5 % 0 = NaN (x86's # default NaN), 0.1 // 0.01 = 9 and 0.1 % 0.01 as fmod gives it exactly; of two NaNs, the remainder is the one glibc's # fmod picks: the larger significand (payloads 1 and 2: 2, whichever comes first and whatever its sign), then of two # equal ones the positive. # Why it matters: the sign conventions of // and % are where float code and NumPy most often disagree; a remainder # with the dividend's sign, or a quotient off by one, changes results silently. law f64_divmod: {[C64.dm_div(C64.divmod(F.F64{1075707904, 0}, F.F64{3221225472, 0})), C64.dm_div(C64.divmod(F.F64{3223191552, 0}, F.F64{1073741824, 0})), C64.dm_div(C64.divmod(F.F64{2147483648, 0}, F.F64{1074266112, 0})), C64.dm_div(C64.divmod(F.F64{1075052544, 0}, F.F64{0, 0})), C64.dm_div(C64.divmod(F.F64{1069128089, 2576980378}, F.F64{1065646817, 1202590843}))] == [F.F64{3222274048, 0}, F.F64{3222274048, 0}, F.F64{2147483648, 0}, F.F64{2146435072, 0}, F.F64{1076101120, 0}] : List<&2, F.F64>} & {[C64.dm_mod(C64.divmod(F.F64{1075707904, 0}, F.F64{3221225472, 0})), C64.dm_mod(C64.divmod(F.F64{3223191552, 0}, F.F64{1073741824, 0})), C64.dm_mod(C64.divmod(F.F64{2147483648, 0}, F.F64{1074266112, 0})), C64.dm_mod(C64.divmod(F.F64{1075052544, 0}, F.F64{0, 0})), C64.dm_mod(C64.divmod(F.F64{1069128089, 2576980378}, F.F64{1065646817, 1202590843})), C64.fmod(F.F64{4294443008, 0}, F.F64{2146959360, 0}), C64.fmod(F.F64{2146959360, 1}, F.F64{2146959360, 2}), C64.fmod(F.F64{2146959360, 2}, F.F64{4294443008, 1})] == [F.F64{3219128320, 0}, F.F64{1071644672, 0}, F.F64{0, 0}, F.F64{4294443008, 0}, F.F64{1011875840, 0}, F.F64{2146959360, 0}, F.F64{2146959360, 2}, F.F64{2146959360, 2}] : List<&2, F.F64>} # Guarantees: a float64 (3, 2) array of 1 .. 6 keeps each element's two cells together through a reshape to (2, 3) # (no padding then, so it is laid out again), a transpose, a slice of rows 1 to 2, a slice of column 0 and one of # all three columns of the transpose (both leaving padding), a sum along axis 0 (9, 12), a # comparison with 3.5 (bool cells), and astype to int32 and back. # Why it matters: a float64 element is two cells under one node; an operation that treated the low word as an element # of its own would mix halves of different numbers, and every value after it would be garbage. law f64_layout: {[cells_r(NP.reshape(a32(), [2n, 3n])), Some{NP.to_list(NP.t(a32()))}, cells_r(NP.slice(a32(), 0n, 1n, 3n, 1n)), cells_r(NP.slice(a32(), 1n, 0n, 1n, 1n)), cells_r(NP.slice(NP.t(a32()), 1n, 0n, 3n, 1n)), cells_r(NP.sum(a32(), 0n, False{})), cells_r(NP.less(a32(), NP.full(NP.f64(), [], 1074528256))), Some{NP.to_list(NP.astype(a32(), NP.i32()))}, Some{NP.to_list(NP.astype(NP.astype(a32(), NP.i32()), NP.f64()))}] == [Some{[1072693248, 0, 1073741824, 0, 1074266112, 0, 1074790400, 0, 1075052544, 0, 1075314688, 0]}, Some{[1072693248, 0, 1074266112, 0, 1075052544, 0, 1073741824, 0, 1074790400, 0, 1075314688, 0]}, Some{[1074266112, 0, 1074790400, 0, 1075052544, 0, 1075314688, 0]}, Some{[1072693248, 0, 1074266112, 0, 1075052544, 0]}, Some{[1072693248, 0, 1074266112, 0, 1075052544, 0, 1073741824, 0, 1074790400, 0, 1075314688, 0]}, Some{[1075970048, 0, 1076363264, 0]}, Some{[1, 1, 1, 0, 0, 0]}, Some{[1, 2, 3, 4, 5, 6]}, Some{[1072693248, 0, 1073741824, 0, 1074266112, 0, 1074790400, 0, 1075052544, 0, 1075314688, 0]}] : List<&2, Maybe<&2, List<&2, U32>>>} # Guarantees: astype from float64 as NumPy's on x86-64: 16777217.0 to int32 is 16777217 (exact, not through float32), # -1.0 to uint32 is 4294967295 (through int64), 300.0 to uint8 is 44 (the low byte), 3e9 to int32 is -2^31 (out of # range), -2.5 to int32 is -2 (toward zero), NaN to bool is True and -0.0 to bool is False; and to float64 exactly: # int32 -5 and uint32 4294967295. # Why it matters: these are the conversions where an implementation going through float32, or through the wrong # integer width, gives a different integer with no error. law f64_convert: {[C64.to_cell(S.DI32{}, F.F64{1097859072, 268435456}), C64.to_cell(S.DU32{}, F.F64{3220176896, 0}), C64.to_cell(S.DU8{}, F.F64{1081262080, 0}), C64.to_cell(S.DI32{}, F.F64{1105615371, 3221225472}), C64.to_cell(S.DI32{}, F.F64{3221487616, 0}), C64.to_cell(S.DBool{}, F.F64{2146959360, 0}), C64.to_cell(S.DBool{}, F.F64{2147483648, 0})] == [16777217, 4294967295, 44, 2147483648, 4294967294, 1, 0] : List<&2, U32>} & {[C64.of_cell(S.DI32{}, 4294967291), C64.of_cell(S.DU32{}, 4294967295)] == [F.F64{3222536192, 0}, F.F64{1106247679, 4292870144}] : List<&2, F.F64>} # every cell of a tree, padding included def leaves(t: Array) -> U32: match t: case ALeaf{x}: 1 case ANode{l, r}: (leaves(l) + leaves(r) : U32) def tree_cells(r: Result<&1, &1, S.NdErr, NP.ND>) -> U32: match r: case Done{NP.ND{dt, s, d}}: leaves(d) case Fail{e}: 0 # Guarantees: when a slice or a join leaves padding, it is whole float64 elements: the (3, 2) array's three rows # (padded to four rows of two) hold 16 cells, its transpose cut to 3 columns (two rows of four) holds 16, and a join # of 3 and 2 elements (padded to 8) holds 16. # Why it matters: the runtime keeps an array as a balanced tree and stops on one that is not; padding one level short # (a cell where an element's two belong) reads back as the right values in the checker, yet stops the compiled # program. law f64_padding: {[tree_cells(NP.slice(a32(), 0n, 0n, 3n, 1n)), tree_cells(NP.slice(NP.t(a32()), 1n, 0n, 3n, 1n)), tree_cells(NP.concatenate(0n, NP.astype(NP.arange(NP.i32(), 3n), NP.f64()), NP.astype(NP.arange(NP.i32(), 2n), NP.f64())))] == [16, 16, 16] : List<&2, U32>}