Repository navigation
Expand file tree
/
Copy pathmath.mach
More file actions
255 lines (233 loc) · 5.8 KB
/
Copy pathmath.mach
File metadata and controls
255 lines (233 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# integer math utilities
use std.types.bool.bool;
use std.types.bool.false;
val I64_MIN: i64 = -9223372036854775808;
val I64_MAX: i64 = 9223372036854775807;
# return the smaller of two signed integers
# ---
# a: first value
# b: second value
# ret: the smaller of a and b
pub fun min(a: i64, b: i64) i64 {
if (a < b) { ret a; }
ret b;
}
# return the larger of two signed integers
# ---
# a: first value
# b: second value
# ret: the larger of a and b
pub fun max(a: i64, b: i64) i64 {
if (a > b) { ret a; }
ret b;
}
# clamp a signed integer to the range [lo, hi]
# ---
# v: value to clamp
# lo: lower bound
# hi: upper bound
# ret: v clamped to [lo, hi]
pub fun clamp(v: i64, lo: i64, hi: i64) i64 {
if (v < lo) { ret lo; }
if (v > hi) { ret hi; }
ret v;
}
# return the absolute value of a signed integer
#
# saturates at I64_MAX for I64_MIN input.
# ---
# v: value
# ret: absolute value of v
pub fun abs(v: i64) i64 {
if (v == I64_MIN) { ret I64_MAX; }
if (v < 0) { ret 0 - v; }
ret v;
}
# return the smaller of two unsigned integers
# ---
# a: first value
# b: second value
# ret: the smaller of a and b
pub fun umin(a: u64, b: u64) u64 {
if (a < b) { ret a; }
ret b;
}
# return the larger of two unsigned integers
# ---
# a: first value
# b: second value
# ret: the larger of a and b
pub fun umax(a: u64, b: u64) u64 {
if (a > b) { ret a; }
ret b;
}
# clamp an unsigned integer to the range [lo, hi]
# ---
# v: value to clamp
# lo: lower bound
# hi: upper bound
# ret: v clamped to [lo, hi]
pub fun uclamp(v: u64, lo: u64, hi: u64) u64 {
if (v < lo) { ret lo; }
if (v > hi) { ret hi; }
ret v;
}
# check whether a value is a power of two
# ---
# v: value to check
# ret: true if v is a non-zero power of two
pub fun is_pow2(v: u64) bool {
if (v == 0) { ret false; }
ret (v & (v - 1)) == 0;
}
# round up to the next power of two
#
# returns v if already a power of two, 1 if v is 0.
# ---
# v: value to round up
# ret: next power of two >= v
pub fun next_pow2(v: u64) u64 {
if (v == 0) { ret 1; }
var n: u64 = v - 1;
n = n | (n >> 1);
n = n | (n >> 2);
n = n | (n >> 4);
n = n | (n >> 8);
n = n | (n >> 16);
n = n | (n >> 32);
ret n + 1;
}
# compute the floor of log base 2
#
# returns 0 for v == 0 (undefined).
# ---
# v: value
# ret: floor(log2(v))
pub fun log2(v: u64) u64 {
if (v == 0) { ret 0; }
var n: u64 = 0;
var x: u64 = v;
if (x >= 4294967296) {
n = n + 32;
x = x >> 32;
}
if (x >= 65536) {
n = n + 16;
x = x >> 16;
}
if (x >= 256) {
n = n + 8;
x = x >> 8;
}
if (x >= 16) {
n = n + 4;
x = x >> 4;
}
if (x >= 4) {
n = n + 2;
x = x >> 2;
}
if (x >= 2) { n = n + 1; }
ret n;
}
# ceiling division of two unsigned integers
# ---
# a: dividend
# b: divisor (must not be 0)
# ret: ceil(a / b)
pub fun ceil_div(a: u64, b: u64) u64 {
ret (a + b - 1) / b;
}
# saturating signed addition
#
# clamps the result to [I64_MIN, I64_MAX] on overflow.
# ---
# a: first operand
# b: second operand
# ret: a + b, saturated
pub fun sat_add(a: i64, b: i64) i64 {
val result: i64 = a + b;
if (b > 0 && result < a) { ret I64_MAX; }
if (b < 0 && result > a) { ret I64_MIN; }
ret result;
}
# saturating signed subtraction
#
# clamps the result to [I64_MIN, I64_MAX] on overflow.
# ---
# a: first operand
# b: second operand
# ret: a - b, saturated
pub fun sat_sub(a: i64, b: i64) i64 {
val result: i64 = a - b;
if (b < 0 && result < a) { ret I64_MAX; }
if (b > 0 && result > a) { ret I64_MIN; }
ret result;
}
# saturating signed multiplication
#
# clamps the result to [I64_MIN, I64_MAX] on overflow.
# ---
# a: first operand
# b: second operand
# ret: a * b, saturated
pub fun sat_mul(a: i64, b: i64) i64 {
if (a == 0 || b == 0) { ret 0; }
if (a == I64_MIN) {
if (b > 0) { ret I64_MIN; }
ret I64_MAX;
}
if (b == I64_MIN) {
if (a > 0) { ret I64_MIN; }
ret I64_MAX;
}
val result: i64 = a * b;
if (result / a != b) {
if ((a > 0 && b > 0) || (a < 0 && b < 0)) { ret I64_MAX; }
ret I64_MIN;
}
ret result;
}
test abs__i64_min_saturates {
if (abs(-42) != 42) { ret 1; }
if (abs(I64_MIN) != I64_MAX) { ret 2; }
ret 0;
}
test next_pow2__rounds_up_to_a_power {
if (next_pow2(0) != 1) { ret 1; }
if (next_pow2(16) != 16) { ret 2; }
if (next_pow2(1000) != 1024) { ret 3; }
if (next_pow2(4294967297) != 8589934592) { ret 4; }
ret 0;
}
test log2__floors_across_every_shift {
if (log2(0) != 0) { ret 1; }
if (log2(1) != 0) { ret 2; }
if (log2(3) != 1) { ret 3; }
if (log2(100) != 6) { ret 4; }
if (log2(65535) != 15) { ret 5; }
if (log2(4294967296) != 32) { ret 6; }
if (log2(0xffffffffffffffff) != 63) { ret 7; }
ret 0;
}
test sat_add__saturates_both_ways {
if (sat_add(-3, 5) != 2) { ret 1; }
if (sat_add(I64_MAX, 1) != I64_MAX) { ret 2; }
if (sat_add(I64_MIN, I64_MIN) != I64_MIN) { ret 3; }
ret 0;
}
test sat_sub__saturates_both_ways {
if (sat_sub(-3, -5) != 2) { ret 1; }
if (sat_sub(I64_MAX, -1) != I64_MAX) { ret 2; }
if (sat_sub(I64_MIN, 1) != I64_MIN) { ret 3; }
ret 0;
}
test sat_mul__saturates_by_sign {
if (sat_mul(0, I64_MIN) != 0) { ret 1; }
if (sat_mul(-3, 5) != -15) { ret 2; }
if (sat_mul(I64_MAX, 2) != I64_MAX) { ret 3; }
if (sat_mul(-2, I64_MAX) != I64_MIN) { ret 4; }
if (sat_mul(I64_MIN, 2) != I64_MIN) { ret 5; }
if (sat_mul(-1, I64_MIN) != I64_MAX) { ret 6; }
ret 0;
}