// U64 (ADR 0035): unsigned 64-bit integers that wrap. import std/io import std/process.{type proc} import std/u64.{type U64} /// The constant `hi * 2^32 + lo`: U64 has no literals, and Int literals /// stop at 2^62. fn word :: hi: Int lo: Int -> U64 { u64.or (u64.shift_left (u64.from_int hi) 32) (u64.from_int lo) } /// One step of SplitMix64: the next state and its output. fn splitmix :: state: U64 -> (U64, U64) { let s = state + word 0x9E3779B9 0x7F4A7C15 let z = u64.xor s (u64.shift_right s 30) * word 0xBF58476D 0x1CE4E5B9 let z = u64.xor z (u64.shift_right z 27) * word 0x94D049BB 0x133111EB (s, u64.xor z (u64.shift_right z 31)) } pub fn main :: -> () / { let max = u64.max () let one = u64.from_int 1 io.println "{max} {max + one} {u64.zero () - one} {u64.from_int (-1) == max} {-one}" io.println "{max * u64.from_int 2} {u64.to_int max} {max / u64.from_int 2} {max % u64.from_int 10}" io.println "{max > one} {u64.from_int 0 < max} {max == max}" io.println "{u64.shift_left one 63} {u64.shift_left one 64} {u64.shift_right max 60} {u64.rotate_left one 65} {u64.rotate_right one 1}" io.println "{u64.and max (u64.from_int 255)} {u64.xor max max} {u64.not (u64.zero ())} {max:?}" // SplitMix64 from seed 0 (the reference implementation's first outputs). let (s1, a) = splitmix (u64.zero ()) let (s2, b) = splitmix s1 let (_, c) = splitmix s2 io.println "{a} {b} {c}" // A U64 is a value like any other: it can be sent. let inbox = process.subject () let _ = process.spawn fn { process.send inbox (max - one) } io.println "{process.receive_forever inbox}" } //> 18446744073709551615 0 18446744073709551615 True 18446744073709551615 //> 18446744073709551614 18446744073709551615 9223372036854775807 5 //> True True True //> 9223372036854775808 0 15 2 9223372036854775808 //> 255 0 18446744073709551615 18446744073709551615 //> 16294208416658607535 7960286522194355700 487617019471545679 //> 18446744073709551614