forked from EdgeCloudX/ovn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitwise.dl
272 lines (239 loc) · 10.9 KB
/
bitwise.dl
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
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Returns true if and only if 'x' is a power of 2.
*/
function is_power_of_two(x: u8): bool { x != 0 and (x & (x - 1)) == 0 }
function is_power_of_two(x: u16): bool { x != 0 and (x & (x - 1)) == 0 }
function is_power_of_two(x: u32): bool { x != 0 and (x & (x - 1)) == 0 }
function is_power_of_two(x: u64): bool { x != 0 and (x & (x - 1)) == 0 }
function is_power_of_two(x: u128): bool { x != 0 and (x & (x - 1)) == 0 }
/*
* Returns the next power of 2 greater than 'x', or None if that's bigger than the
* type's maximum value.
*/
function next_power_of_two(x: u8): Option<u8> { u8_next_power_of_two(x) }
function next_power_of_two(x: u16): Option<u16> { u16_next_power_of_two(x) }
function next_power_of_two(x: u32): Option<u32> { u32_next_power_of_two(x) }
function next_power_of_two(x: u64): Option<u64> { u64_next_power_of_two(x) }
function next_power_of_two(x: u128): Option<u128> { u128_next_power_of_two(x) }
extern function u8_next_power_of_two(x: u8): Option<u8>
extern function u16_next_power_of_two(x: u16): Option<u16>
extern function u32_next_power_of_two(x: u32): Option<u32>
extern function u64_next_power_of_two(x: u64): Option<u64>
extern function u128_next_power_of_two(x: u128): Option<u128>
/*
* Returns the next power of 2 greater than 'x', or 0 if that's bigger than the
* type's maximum value.
*/
function wrapping_next_power_of_two(x: u8): u8 { u8_wrapping_next_power_of_two(x) }
function wrapping_next_power_of_two(x: u16): u16 { u16_wrapping_next_power_of_two(x) }
function wrapping_next_power_of_two(x: u32): u32 { u32_wrapping_next_power_of_two(x) }
function wrapping_next_power_of_two(x: u64): u64 { u64_wrapping_next_power_of_two(x) }
function wrapping_next_power_of_two(x: u128): u128 { u128_wrapping_next_power_of_two(x) }
extern function u8_wrapping_next_power_of_two(x: u8): u8
extern function u16_wrapping_next_power_of_two(x: u16): u16
extern function u32_wrapping_next_power_of_two(x: u32): u32
extern function u64_wrapping_next_power_of_two(x: u64): u64
extern function u128_wrapping_next_power_of_two(x: u128): u128
/*
* Number of 1-bits in the binary representation of 'x'.
*/
function count_ones(x: u8): u32 { u8_count_ones(x) }
function count_ones(x: u16): u32 { u16_count_ones(x) }
function count_ones(x: u32): u32 { u32_count_ones(x) }
function count_ones(x: u64): u32 { u64_count_ones(x) }
function count_ones(x: u128): u32 { u128_count_ones(x) }
extern function u8_count_ones(x: u8): u32
extern function u16_count_ones(x: u16): u32
extern function u32_count_ones(x: u32): u32
extern function u64_count_ones(x: u64): u32
extern function u128_count_ones(x: u128): u32
/*
* Number of 0-bits in the binary representation of 'x'.
*/
function count_zeros(x: u8): u32 { u8_count_zeros(x) }
function count_zeros(x: u16): u32 { u16_count_zeros(x) }
function count_zeros(x: u32): u32 { u32_count_zeros(x) }
function count_zeros(x: u64): u32 { u64_count_zeros(x) }
function count_zeros(x: u128): u32 { u128_count_zeros(x) }
extern function u8_count_zeros(x: u8): u32
extern function u16_count_zeros(x: u16): u32
extern function u32_count_zeros(x: u32): u32
extern function u64_count_zeros(x: u64): u32
extern function u128_count_zeros(x: u128): u32
/*
* Number of leading 0-bits in the binary representation of 'x'.
*/
function leading_zeros(x: u8): u32 { u8_leading_zeros(x) }
function leading_zeros(x: u16): u32 { u16_leading_zeros(x) }
function leading_zeros(x: u32): u32 { u32_leading_zeros(x) }
function leading_zeros(x: u64): u32 { u64_leading_zeros(x) }
function leading_zeros(x: u128): u32 { u128_leading_zeros(x) }
extern function u8_leading_zeros(x: u8): u32
extern function u16_leading_zeros(x: u16): u32
extern function u32_leading_zeros(x: u32): u32
extern function u64_leading_zeros(x: u64): u32
extern function u128_leading_zeros(x: u128): u32
/*
* Number of leading 1-bits in the binary representation of 'x'.
*/
function leading_ones(x: u8): u32 { u8_leading_ones(x) }
function leading_ones(x: u16): u32 { u16_leading_ones(x) }
function leading_ones(x: u32): u32 { u32_leading_ones(x) }
function leading_ones(x: u64): u32 { u64_leading_ones(x) }
function leading_ones(x: u128): u32 { u128_leading_ones(x) }
extern function u8_leading_ones(x: u8): u32
extern function u16_leading_ones(x: u16): u32
extern function u32_leading_ones(x: u32): u32
extern function u64_leading_ones(x: u64): u32
extern function u128_leading_ones(x: u128): u32
/*
* Number of trailing 0-bits in the binary representation of 'x'.
*/
function trailing_zeros(x: u8): u32 { u8_trailing_zeros(x) }
function trailing_zeros(x: u16): u32 { u16_trailing_zeros(x) }
function trailing_zeros(x: u32): u32 { u32_trailing_zeros(x) }
function trailing_zeros(x: u64): u32 { u64_trailing_zeros(x) }
function trailing_zeros(x: u128): u32 { u128_trailing_zeros(x) }
extern function u8_trailing_zeros(x: u8): u32
extern function u16_trailing_zeros(x: u16): u32
extern function u32_trailing_zeros(x: u32): u32
extern function u64_trailing_zeros(x: u64): u32
extern function u128_trailing_zeros(x: u128): u32
/*
* Number of trailing 0-bits in the binary representation of 'x'.
*/
function trailing_ones(x: u8): u32 { u8_trailing_ones(x) }
function trailing_ones(x: u16): u32 { u16_trailing_ones(x) }
function trailing_ones(x: u32): u32 { u32_trailing_ones(x) }
function trailing_ones(x: u64): u32 { u64_trailing_ones(x) }
function trailing_ones(x: u128): u32 { u128_trailing_ones(x) }
extern function u8_trailing_ones(x: u8): u32
extern function u16_trailing_ones(x: u16): u32
extern function u32_trailing_ones(x: u32): u32
extern function u64_trailing_ones(x: u64): u32
extern function u128_trailing_ones(x: u128): u32
/*
* Reverses the order of bits in 'x'.
*/
function reverse_bits(x: u8): u8 { u8_reverse_bits(x) }
function reverse_bits(x: u16): u16 { u16_reverse_bits(x) }
function reverse_bits(x: u32): u32 { u32_reverse_bits(x) }
function reverse_bits(x: u64): u64 { u64_reverse_bits(x) }
function reverse_bits(x: u128): u128 { u128_reverse_bits(x) }
extern function u8_reverse_bits(x: u8): u8
extern function u16_reverse_bits(x: u16): u16
extern function u32_reverse_bits(x: u32): u32
extern function u64_reverse_bits(x: u64): u64
extern function u128_reverse_bits(x: u128): u128
/*
* Reverses the order of bytes in 'x'.
*/
function swap_bytes(x: u8): u8 { u8_swap_bytes(x) }
function swap_bytes(x: u16): u16 { u16_swap_bytes(x) }
function swap_bytes(x: u32): u32 { u32_swap_bytes(x) }
function swap_bytes(x: u64): u64 { u64_swap_bytes(x) }
function swap_bytes(x: u128): u128 { u128_swap_bytes(x) }
extern function u8_swap_bytes(x: u8): u8
extern function u16_swap_bytes(x: u16): u16
extern function u32_swap_bytes(x: u32): u32
extern function u64_swap_bytes(x: u64): u64
extern function u128_swap_bytes(x: u128): u128
/*
* Converts 'x' from big endian to the machine's native endianness.
* On a big-endian machine it is a no-op.
* On a little-end machine it is equivalent to swap_bytes().
*/
function from_be(x: u8): u8 { u8_from_be(x) }
function from_be(x: u16): u16 { u16_from_be(x) }
function from_be(x: u32): u32 { u32_from_be(x) }
function from_be(x: u64): u64 { u64_from_be(x) }
function from_be(x: u128): u128 { u128_from_be(x) }
extern function u8_from_be(x: u8): u8
extern function u16_from_be(x: u16): u16
extern function u32_from_be(x: u32): u32
extern function u64_from_be(x: u64): u64
extern function u128_from_be(x: u128): u128
/*
* Converts 'x' from the machine's native endianness to big endian.
* On a big-endian machine it is a no-op.
* On a little-endian machine it is equivalent to swap_bytes().
*/
function to_be(x: u8): u8 { u8_to_be(x) }
function to_be(x: u16): u16 { u16_to_be(x) }
function to_be(x: u32): u32 { u32_to_be(x) }
function to_be(x: u64): u64 { u64_to_be(x) }
function to_be(x: u128): u128 { u128_to_be(x) }
extern function u8_to_be(x: u8): u8
extern function u16_to_be(x: u16): u16
extern function u32_to_be(x: u32): u32
extern function u64_to_be(x: u64): u64
extern function u128_to_be(x: u128): u128
/*
* Converts 'x' from little endian to the machine's native endianness.
* On a little-endian machine it is a no-op.
* On a big-endian machine it is equivalent to swap_bytes().
*/
function from_le(x: u8): u8 { u8_from_le(x) }
function from_le(x: u16): u16 { u16_from_le(x) }
function from_le(x: u32): u32 { u32_from_le(x) }
function from_le(x: u64): u64 { u64_from_le(x) }
function from_le(x: u128): u128 { u128_from_le(x) }
extern function u8_from_le(x: u8): u8
extern function u16_from_le(x: u16): u16
extern function u32_from_le(x: u32): u32
extern function u64_from_le(x: u64): u64
extern function u128_from_le(x: u128): u128
/*
* Converts 'x' from the machine's native endianness to little endian.
* On a little-endian machine it is a no-op.
* On a big-endian machine it is equivalent to swap_bytes().
*/
function to_le(x: u8): u8 { u8_to_le(x) }
function to_le(x: u16): u16 { u16_to_le(x) }
function to_le(x: u32): u32 { u32_to_le(x) }
function to_le(x: u64): u64 { u64_to_le(x) }
function to_le(x: u128): u128 { u128_to_le(x) }
extern function u8_to_le(x: u8): u8
extern function u16_to_le(x: u16): u16
extern function u32_to_le(x: u32): u32
extern function u64_to_le(x: u64): u64
extern function u128_to_le(x: u128): u128
/*
* Rotates the bits in 'x' left by 'n' positions.
*/
function rotate_left(x: u8, n: u32): u8 { u8_rotate_left(x, n) }
function rotate_left(x: u16, n: u32): u16 { u16_rotate_left(x, n) }
function rotate_left(x: u32, n: u32): u32 { u32_rotate_left(x, n) }
function rotate_left(x: u64, n: u32): u64 { u64_rotate_left(x, n) }
function rotate_left(x: u128, n: u32): u128 { u128_rotate_left(x, n) }
extern function u8_rotate_left(x: u8, n: u32): u8
extern function u16_rotate_left(x: u16, n: u32): u16
extern function u32_rotate_left(x: u32, n: u32): u32
extern function u64_rotate_left(x: u64, n: u32): u64
extern function u128_rotate_left(x: u128, n: u32): u128
/*
* Rotates the bits in 'x' right by 'n' positions.
*/
function rotate_right(x: u8, n: u32): u8 { u8_rotate_right(x, n) }
function rotate_right(x: u16, n: u32): u16 { u16_rotate_right(x, n) }
function rotate_right(x: u32, n: u32): u32 { u32_rotate_right(x, n) }
function rotate_right(x: u64, n: u32): u64 { u64_rotate_right(x, n) }
function rotate_right(x: u128, n: u32): u128 { u128_rotate_right(x, n) }
extern function u8_rotate_right(x: u8, n: u32): u8
extern function u16_rotate_right(x: u16, n: u32): u16
extern function u32_rotate_right(x: u32, n: u32): u32
extern function u64_rotate_right(x: u64, n: u32): u64
extern function u128_rotate_right(x: u128, n: u32): u128