tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

52 lignes · 1994 octetsBrut · Historique
1// U64 (ADR 0035): unsigned 64-bit integers that wrap.
2
3import std/io
4import std/process.{type proc}
5import std/u64.{type U64}
6
7/// The constant `hi * 2^32 + lo`: U64 has no literals, and Int literals
8/// stop at 2^62.
9fn word :: hi: Int lo: Int -> U64
10{
11 u64.or (u64.shift_left (u64.from_int hi) 32) (u64.from_int lo)
12}
13
14/// One step of SplitMix64: the next state and its output.
15fn splitmix :: state: U64 -> (U64, U64)
16{
17 let s = state + word 0x9E3779B9 0x7F4A7C15
18 let z = u64.xor s (u64.shift_right s 30) * word 0xBF58476D 0x1CE4E5B9
19 let z = u64.xor z (u64.shift_right z 27) * word 0x94D049BB 0x133111EB
20 (s, u64.xor z (u64.shift_right z 31))
21}
22
23pub fn main :: -> () / <io, proc>
24{
25 let max = u64.max ()
26 let one = u64.from_int 1
27 io.println
28 "{max} {max + one} {u64.zero () - one} {u64.from_int (-1) == max} {-one}"
29 io.println
30 "{max * u64.from_int 2} {u64.to_int max} {max / u64.from_int 2} {max % u64.from_int 10}"
31 io.println "{max > one} {u64.from_int 0 < max} {max == max}"
32 io.println
33 "{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}"
34 io.println
35 "{u64.and max (u64.from_int 255)} {u64.xor max max} {u64.not (u64.zero ())} {max:?}"
36 // SplitMix64 from seed 0 (the reference implementation's first outputs).
37 let (s1, a) = splitmix (u64.zero ())
38 let (s2, b) = splitmix s1
39 let (_, c) = splitmix s2
40 io.println "{a} {b} {c}"
41 // A U64 is a value like any other: it can be sent.
42 let inbox = process.subject ()
43 let _ = process.spawn fn { process.send inbox (max - one) }
44 io.println "{process.receive_forever inbox}"
45}
46//> 18446744073709551615 0 18446744073709551615 True 18446744073709551615
47//> 18446744073709551614 18446744073709551615 9223372036854775807 5
48//> True True True
49//> 9223372036854775808 0 15 2 9223372036854775808
50//> 255 0 18446744073709551615 18446744073709551615
51//> 16294208416658607535 7960286522194355700 487617019471545679
52//> 18446744073709551614