-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathref.c
264 lines (230 loc) · 6.08 KB
/
ref.c
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
/*******************************************************************************
*
* ref.c - Reference implementations
*
* Copyright (c) 2023 Basil Hussain
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
******************************************************************************/
#include <stddef.h>
#include <stdint.h>
#include <limits.h>
#include "ref.h"
uint8_t swap_ref(const uint8_t value) {
return ((value & 0xF0) >> 4) | ((value & 0x0F) << 4);
}
uint16_t bswap_16_ref(const uint16_t value) {
return ((value & 0xFF00) >> 8) | ((value & 0x00FF) << 8);
}
uint32_t bswap_32_ref(const uint32_t value) {
return ((value & 0x000000FFUL) << 24) | ((value & 0x0000FF00UL) << 8) | ((value & 0x00FF0000UL) >> 8) | ((value & 0xFF000000UL) >> 24);
}
uint8_t reflect_8_ref(uint8_t value) {
uint8_t result = value;
uint8_t shifts = (sizeof(value) * CHAR_BIT) - 1;
for(value >>= 1; value; value >>= 1) {
result <<= 1;
result |= value & 1;
shifts--;
}
result <<= shifts;
return result;
}
uint16_t reflect_16_ref(uint16_t value) {
uint16_t result = value;
uint8_t shifts = (sizeof(value) * CHAR_BIT) - 1;
for(value >>= 1; value; value >>= 1) {
result <<= 1;
result |= value & 1;
shifts--;
}
result <<= shifts;
return result;
}
uint32_t reflect_32_ref(uint32_t value) {
uint32_t result = value;
uint8_t shifts = (sizeof(value) * CHAR_BIT) - 1;
for(value >>= 1; value; value >>= 1) {
result <<= 1;
result |= value & 1;
shifts--;
}
result <<= shifts;
return result;
}
uint8_t ctz_8_ref(uint8_t value) {
uint8_t c = 0;
if(value) {
// Set value's trailing 0s to 1s and zero the rest.
value = (value ^ (value - 1)) >> 1;
while(value) {
value >>= 1;
c++;
}
} else {
c = CHAR_BIT * sizeof(value);
}
return c;
}
uint8_t ctz_16_ref(uint16_t value) {
uint8_t c = 0;
if(value) {
// Set value's trailing 0s to 1s and zero the rest.
value = (value ^ (value - 1)) >> 1;
while(value) {
value >>= 1;
c++;
}
} else {
c = CHAR_BIT * sizeof(value);
}
return c;
}
uint8_t ctz_32_ref(uint32_t value) {
uint8_t c = 0;
if(value) {
// Set value's trailing 0s to 1s and zero the rest.
value = (value ^ (value - 1)) >> 1;
while(value) {
value >>= 1;
c++;
}
} else {
c = CHAR_BIT * sizeof(value);
}
return c;
}
uint8_t clz_8_ref(uint8_t value) {
uint8_t c = 0;
if(value) {
uint8_t mask = 1U << ((CHAR_BIT * sizeof(value)) - 1);
while(!(value & mask)) {
mask >>= 1;
c++;
}
} else {
c = CHAR_BIT * sizeof(value);
}
return c;
}
uint8_t clz_16_ref(uint16_t value) {
uint8_t c = 0;
if(value) {
uint16_t mask = 1U << ((CHAR_BIT * sizeof(value)) - 1);
while(!(value & mask)) {
mask >>= 1;
c++;
}
} else {
c = CHAR_BIT * sizeof(value);
}
return c;
}
uint8_t clz_32_ref(uint32_t value) {
uint8_t c = 0;
if(value) {
uint32_t mask = 1UL << ((CHAR_BIT * sizeof(value)) - 1);
while(!(value & mask)) {
mask >>= 1;
c++;
}
} else {
c = CHAR_BIT * sizeof(value);
}
return c;
}
uint8_t ffs_8_ref(uint8_t value) {
return (value ? ctz_8_ref(value) + 1 : 0);
}
uint8_t ffs_16_ref(uint16_t value) {
return (value ? ctz_16_ref(value) + 1 : 0);
}
uint8_t ffs_32_ref(uint32_t value) {
return (value ? ctz_32_ref(value) + 1 : 0);
}
uint8_t pop_count_8_ref(uint8_t value) {
uint8_t c = 0;
while(value) {
// Clear the least significant bit set.
value &= value - 1;
c++;
}
return c;
}
uint8_t pop_count_16_ref(uint16_t value) {
uint8_t c = 0;
while(value) {
// Clear the least significant bit set.
value &= value - 1;
c++;
}
return c;
}
uint8_t pop_count_32_ref(uint32_t value) {
uint8_t c = 0;
while(value) {
// Clear the least significant bit set.
value &= value - 1;
c++;
}
return c;
}
uint8_t rotate_left_8_ref(uint8_t value, uint8_t count) {
const uint8_t mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value << count) | (value >> (-count & mask));
}
uint8_t rotate_right_8_ref(uint8_t value, uint8_t count) {
const uint8_t mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value >> count) | (value << (-count & mask));
}
uint16_t rotate_left_16_ref(uint16_t value, uint8_t count) {
const uint8_t mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value << count) | (value >> (-count & mask));
}
uint16_t rotate_right_16_ref(uint16_t value, uint8_t count) {
const uint8_t mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value >> count) | (value << (-count & mask));
}
uint32_t rotate_left_32_ref(uint32_t value, uint8_t count) {
const uint8_t mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value << count) | (value >> (-count & mask));
}
uint32_t rotate_right_32_ref(uint32_t value, uint8_t count) {
const uint8_t mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value >> count) | (value << (-count & mask));
}
void div_s16_ref(int16_t x, int16_t y, div_s16_t *result) {
result->quot = x / y;
result->rem = x % y;
}
void div_u16_ref(uint16_t x, uint16_t y, div_u16_t *result) {
result->quot = x / y;
result->rem = x % y;
}
void div_u32_ref(uint32_t x, uint32_t y, div_u32_t *result) {
result->quot = x / y;
result->rem = x % y;
}