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.

168 lignes · 2899 octetsBrut · Historique
1/// `U64`: unsigned 64-bit integers that wrap (ADR 0035).
2///
3/// let x = u64.from_int(-1) // 18446744073709551615
4/// x + u64.from_int(1) // 0: arithmetic is modulo 2^64
5/// u64.rotate_left(u64.from_int(1), 65) // 2
6///
7/// For algorithms on 64-bit words (hashes, random generators such as
8/// SplitMix64 in `std/prng`), where an `Int` would grow into a big
9/// integer. `+`, `-` and `*` wrap; `/` and `%` are unsigned and panic on
10/// zero. There are no `U64` literals: use `from_int`. A `U64` is boxed, as
11/// a `Float` is: every result allocates.
12
13/// An unsigned 64-bit integer.
14@builtin
15pub type U64
16
17/// `n` modulo 2^64 (two's complement: `from_int(-1)` is the largest).
18@extern c "tl_u64_from_int"
19pub fn from_int :: n: Int -> U64
20
21/// The value, in `[0, 2^64)` (a big integer above 2^62).
22@extern c "tl_u64_to_int"
23pub fn to_int :: x: U64 -> Int
24
25/// 0
26pub fn zero :: -> U64
27{
28 from_int 0
29}
30
31/// 2^64 - 1
32pub fn max :: -> U64
33{
34 from_int (-1)
35}
36
37@extern c "tl_u64_and"
38pub fn and :: a: U64 b: U64 -> U64
39
40@extern c "tl_u64_or"
41pub fn or :: a: U64 b: U64 -> U64
42
43@extern c "tl_u64_xor"
44pub fn xor :: a: U64 b: U64 -> U64
45
46@extern c "tl_u64_not"
47pub fn not :: x: U64 -> U64
48
49/// Shifted left by `k` bits; 0 when `k` is 64 or more. Panics if `k` is
50/// negative.
51@extern c "tl_u64_shift_left"
52pub fn shift_left :: x: U64 k: Int -> U64
53
54/// Shifted right by `k` bits, filling with zeros; 0 when `k` is 64 or more.
55/// Panics if `k` is negative.
56@extern c "tl_u64_shift_right"
57pub fn shift_right :: x: U64 k: Int -> U64
58
59/// Rotated left by `k` bits, modulo 64.
60@extern c "tl_u64_rotate_left"
61pub fn rotate_left :: x: U64 k: Int -> U64
62
63/// Rotated right by `k` bits, modulo 64.
64@extern c "tl_u64_rotate_right"
65pub fn rotate_right :: x: U64 k: Int -> U64
66
67@extern c "tl_u64_add"
68fn add :: a: U64 b: U64 -> U64
69
70@extern c "tl_u64_sub"
71fn sub :: a: U64 b: U64 -> U64
72
73@extern c "tl_u64_mul"
74fn mul :: a: U64 b: U64 -> U64
75
76@extern c "tl_u64_div"
77fn div :: a: U64 b: U64 -> U64
78
79@extern c "tl_u64_rem"
80fn rem :: a: U64 b: U64 -> U64
81
82@extern c "tl_u64_eq"
83fn equal :: a: U64 b: U64 -> Bool
84
85@extern c "tl_u64_compare"
86fn order :: a: U64 b: U64 -> Order
87
88@extern c "tl_u64_show"
89fn decimal :: x: U64 -> String
90
91@extern c "tl_hash_u64"
92fn hash_u64 :: x: U64 -> Int
93
94impl Num<U64>
95{
96 fn add :: x y
97 {
98 add x y
99 }
100 fn sub :: x y
101 {
102 sub x y
103 }
104 fn mul :: x y
105 {
106 mul x y
107 }
108 fn div :: x y
109 {
110 div x y
111 }
112 fn rem :: x y
113 {
114 rem x y
115 }
116 fn negate :: x
117 {
118 sub (zero ()) x
119 }
120}
121
122impl Eq<U64>
123{
124 fn eq :: x y
125 {
126 equal x y
127 }
128}
129
130impl Ord<U64>
131{
132 fn compare :: x y
133 {
134 order x y
135 }
136}
137
138impl Show<U64>
139{
140 fn show :: x
141 {
142 decimal x
143 }
144}
145
146impl Debug<U64>
147{
148 fn debug :: x
149 {
150 decimal x
151 }
152}
153
154impl Hash<U64>
155{
156 fn hash :: x
157 {
158 hash_u64 x
159 }
160}
161
162impl Default<U64>
163{
164 fn default ::
165 {
166 zero ()
167 }
168}