-
Notifications
You must be signed in to change notification settings - Fork 56
/
Int.hylo
385 lines (281 loc) · 8.95 KB
/
Int.hylo
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/// A signed integer value.
public type Int {
internal var value: Builtin.word
internal memberwise init
/// Creates an instance with the same memory representation as `other`.
public init(bit_pattern other: UInt) {
&self.value = other.value
}
/// Creates an instance with the same memory representation as `address`.
public init(bit_pattern address: PointerToMutable<Never>) {
&self.value = Builtin.ptrtoint_word(address.base)
}
/// Returns the absolute value of `self`.
public fun abs() -> Int {
if self < 0 { -self } else { +self }
}
/// Returns `self` rounded up to the nearest multiple of `stride`.
public fun round_up(nearest_multiple_of stride: Int) -> Int {
if stride == 0 {
return self.copy()
}
let r = abs() % stride
if r == 0 {
return self.copy()
} else if r < 0 {
return -(abs() - r)
}
return self + stride - r
}
/// Returns the smallest power of two that is not smaller than `self`.
public fun round_up_nearest_power_two() -> Int {
if self == 1 { 1 } else { 1 << (Self.bit_width() - (self - 1).leading_zeros()) }
}
/// Returns `self`.
public fun prefix+ () -> Self {
self.copy()
}
/// Returns the bitwise inverse of `self`.
public fun prefix~ () -> Self {
self ^ -1
}
/// Returns the product of `self` and `other, wrapping the result in case of any overflow.
public fun infix &* (_ other: Self) -> Self {
self.multiplied_reporting_overflow(by: other).0
}
}
public conformance Int: ExpressibleByIntegerLiteral {}
public conformance Int: Deinitializable {}
public conformance Int: Movable {}
public conformance Int: Copyable {
public fun copy() -> Self {
Int(value: value)
}
}
public conformance Int: Equatable {
public fun infix== (_ other: Self) -> Bool {
Bool(value: Builtin.icmp_eq_word(value, other.value))
}
public fun infix!= (_ other: Self) -> Bool {
Bool(value: Builtin.icmp_ne_word(value, other.value))
}
}
public conformance Int: Regular {}
public conformance Int: Hashable {
public fun hash(into hasher: inout Hasher) {
// TODO: use conditional compilation to avoid branches
&hasher.unsafe_combine(bytes: pointer_to_bytes[of: self])
}
}
public conformance Int: Comparable {
public fun infix< (_ other: Self) -> Bool {
Bool(value: Builtin.icmp_slt_word(value, other.value))
}
public fun infix<= (_ other: Self) -> Bool {
Bool(value: Builtin.icmp_sle_word(value, other.value))
}
public fun infix> (_ other: Self) -> Bool {
Bool(value: Builtin.icmp_sgt_word(value, other.value))
}
public fun infix>= (_ other: Self) -> Bool {
Bool(value: Builtin.icmp_sge_word(value, other.value))
}
}
public conformance Int: AdditiveArithmetic {
public fun infix+ (_ other: Self) -> Self {
Int(value: Builtin.add_word(value, other.value))
}
public fun infix+= (_ other: Self) inout {
&self.value = Builtin.add_word(value, other.value)
}
public fun infix- (_ other: Self) -> Self {
Int(value: Builtin.sub_word(value, other.value))
}
public fun infix-= (_ other: Self) inout {
&self.value = Builtin.sub_word(value, other.value)
}
public static fun zero() -> Self {
Int()
}
}
public conformance Int: Numeric {
public typealias Magnitude = UInt
public fun magnitude() -> UInt {
UInt(bit_pattern: self)
}
public fun infix* (_ other: Self) -> Self {
Int(value: Builtin.mul_word(value, other.value))
}
public fun infix*= (_ other: Self) inout {
&self.value = Builtin.mul_word(value, other.value)
}
}
public conformance Int: SignedNumeric {
public fun prefix- () -> Self {
Int() - self
}
public fun negate() inout {
&self = -self
}
}
public conformance Int: BinaryInteger {
public init() {
&self.value = Builtin.zeroinitializer_word()
}
public init<T: BinaryInteger>(truncating_or_extending source: T) {
let w = source.words()
&self.value = w[w.start_position()].value
}
public fun instance_bit_width() -> Int {
Self.bit_width()
}
public fun signum() -> Int {
let positive = Int(value: Builtin.zext_i1_word((self > 0).value))
return positive | (self &>> (Self.bit_width() - 1))
}
public fun trailing_zeros() -> Int {
Int(value: Builtin.cttz_word(value))
}
public fun quotient_and_remainder(dividing_by other: Self) -> {quotient: Self, remainder: Self} {
(quotient: self / other, remainder: self % other)
}
public fun words() -> CollectionOfOne<UInt> {
CollectionOfOne(UInt(bit_pattern: self))
}
public fun infix/ (_ other: Self) -> Self {
Int(value: Builtin.sdiv_word(value, other.value))
}
public fun infix/= (_ other: Self) inout {
&self.value = Builtin.sdiv_word(value, other.value)
}
public fun infix% (_ other: Self) -> Self {
Int(value: Builtin.srem_word(value, other.value))
}
public fun infix%= (_ other: Self) inout {
&self.value = Builtin.srem_word(value, other.value)
}
public fun infix& (_ other: Self) -> Self {
Int(value: Builtin.and_word(value, other.value))
}
public fun infix&= (_ other: Self) inout {
&self.value = Builtin.and_word(value, other.value)
}
public fun infix| (_ other: Self) -> Self {
Int(value: Builtin.or_word(value, other.value))
}
public fun infix|= (_ other: Self) inout {
&self.value = Builtin.or_word(value, other.value)
}
public fun infix^ (_ other: Self) -> Self {
Int(value: Builtin.xor_word(value, other.value))
}
public fun infix^= (_ other: Self) inout {
&self.value = Builtin.xor_word(value, other.value)
}
public fun infix<< (_ n: Int) -> Self {
Int(value: Builtin.shl_word(value, n.value))
}
public fun infix<<= (_ n: Int) inout {
&self.value = Builtin.shl_word(value, n.value)
}
public fun infix>> (_ n: Int) -> Self {
Int(value: Builtin.ashr_word(value, n.value))
}
public fun infix>>= (_ n: Int) inout {
&self.value = Builtin.ashr_word(value, n.value)
}
public static fun is_signed() -> Bool {
true
}
}
public conformance Int: SignedInteger {
public fun successor() -> Self {
self + 1
}
}
public conformance Int: FixedWidthInteger {
public fun matches(_ mask: Self) -> Bool {
(self & mask) == mask
}
public fun adding_reporting_overflow(_ other: Self) -> {partial_value: Self, overflow: Bool} {
let r = Builtin.sadd_with_overflow_word(value, other.value)
return (partial_value: Int(value: r.0), overflow: Bool(value: r.1))
}
public fun subtracting_reporting_overflow(
_ other: Self
) -> {partial_value: Self, overflow: Bool} {
let r = Builtin.ssub_with_overflow_word(value, other.value)
return (partial_value: Int(value: r.0), overflow: Bool(value: r.1))
}
public fun multiplied_reporting_overflow(
by other: Self
) -> {partial_value: Self, overflow: Bool} {
let r = Builtin.smul_with_overflow_word(value, other.value)
return (partial_value: Int(value: r.0), overflow: Bool(value: r.1))
}
public fun divided_reporting_overflow(by other: Self) -> {partial_value: Self, overflow: Bool} {
if other == 0 {
return (partial_value: self.copy(), overflow: true)
}
if (self == Self.min()) && (other == -1) {
return (partial_value: self.copy(), overflow: true)
}
return (partial_value: Int(value: Builtin.sdiv_word(value, other.value)), overflow: false)
}
public fun remainder_reporting_overflow(
dividing_by other: Self
) -> {partial_value: Self, overflow: Bool} {
if other == 0 {
return (partial_value: self.copy(), overflow: true)
}
if (self == Self.min()) && (other == -1) {
return (partial_value: 0, overflow: true)
}
return (partial_value: Int(value: Builtin.srem_word(value, other.value)), overflow: false)
}
public fun nonzero_bit_count() -> Int {
Int(value: Builtin.ctpop_word(value))
}
public fun leading_zeros() -> Int {
Int(value: Builtin.ctlz_word(value))
}
public fun infix&<< (_ n: Int) -> Self {
Int(value: Builtin.shl_word(value, n.value))
}
public fun infix&<<= (_ n: Int) inout {
&self.value = Builtin.shl_word(value, n.value)
}
public fun infix&>> (_ n: Int) -> Self {
Int(value: Builtin.ashr_word(value, n.value))
}
public fun infix&>>= (_ n: Int) inout {
&self.value = Builtin.ashr_word(value, n.value)
}
public static fun bit_width() -> Int {
MemoryLayout<Builtin.word>.size() * 8
}
public static fun max() -> Self {
Int(value: Builtin.lshr_word((~0).value, (1).value))
}
public static fun min() -> Self {
Int(value: Builtin.shl_word((1).value, (bit_width() - 1).value))
}
}
public conformance Int: Strideable {
public typealias Stride = Int
public fun offset(to other: Self) -> Int {
other - self
}
public fun advance(by offset: Int) -> Self {
self + offset
}
}
public conformance Int: ForeignConvertible {
public typealias ForeignRepresentation = Builtin.word
public init(foreign_value: sink Builtin.word) {
&self.value = foreign_value
}
public fun foreign_value() -> Builtin.word {
value
}
}