-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbin_div.c
415 lines (331 loc) · 6.37 KB
/
mbin_div.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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*-
* Copyright (c) 2008-2010 Hans Petter Selasky
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdint.h>
#include "math_bin.h"
void
mbin_xor_common32(uint32_t *pa, uint32_t *pb)
{
uint32_t and;
and = *pa & *pb;
*pa ^= and;
*pb ^= and;
}
void
mbin_xor_common16(uint16_t *pa, uint16_t *pb)
{
uint16_t and;
and = *pa & *pb;
*pa ^= and;
*pb ^= and;
}
void
mbin_xor_common8(uint8_t *pa, uint8_t *pb)
{
uint8_t and;
and = *pa & *pb;
*pa ^= and;
*pb ^= and;
}
uint32_t
mbin_div_odd32(uint32_t r, uint32_t div)
{
uint32_t m;
div = -div + 1;
m = 1;
while (m) {
if (r & m) {
r += div;
}
m *= 2;
div *= 2;
}
return (r);
}
uint64_t
mbin_div_odd64(uint64_t r, uint64_t div)
{
uint64_t m;
div = -div + 1;
for (m = 1; m; m *= 2) {
if (r & m)
r += div;
div *= 2;
}
return (r);
}
uint64_t
mbin_div_odd64_alt2(uint64_t r, uint64_t div)
{
uint64_t temp[16];
uint8_t n;
div = -div + 1;
/* Build ramp table */
for (n = 0; n != 16; n++) {
uint64_t z = n;
if (z & 1)
z += div;
if (z & 2)
z += 2 * div;
if (z & 4)
z += 4 * div;
if (z & 8)
z += 8 * div;
temp[n] = z - (uint64_t)n;
}
for (n = 0; n != 64; n += 4)
r += (temp[(r >> n) & 15] << n);
return (r);
}
/*
* First version of Near Carry Less, NCL, binary division. This
* function runs faster in hardware than in software.
*/
uint32_t
mbin_div_odd32_alt1(uint32_t rem, uint32_t div)
{
uint32_t neg;
uint32_t m;
uint32_t t;
uint32_t a;
uint32_t b;
m = 1; /* mask */
neg = 0; /* no negative bits */
t = 0; /* temp variable */
while (m) {
if ((rem ^ neg) & m) {
t |= m;
/*
* Subtract "div" from "(rem-neg)" using near
* carry less, NCL, addition where "a" is
* remainder and "b" is carry.
*/
a = neg ^ div;
b = (neg & div);
/* remove common bits */
mbin_xor_common32(&a, &rem);
/* compute final part of NCL addition */
neg = ((2 * a) & ~a) ^ (2 * b);
rem = rem ^ ((~(2 * a)) & a);
/* remove common bits */
mbin_xor_common32(&rem, &neg);
}
m *= 2;
div *= 2;
}
return (t);
}
/*
* Optimised version of Near Carry Less, NCL, binary division. This
* function runs faster in hardware than in software.
*/
uint32_t
mbin_div_odd32_alt2(uint32_t rem, uint32_t div)
{
uint32_t neg;
uint32_t m;
uint32_t t;
uint32_t z;
uint32_t y;
m = 1; /* mask */
neg = 0; /* no negative bits */
t = 0; /* temp variable */
while (m) {
if (rem & m) {
t |= m;
/* "a XOR b" = "(~a AND b)" XOR "(a AND ~b)" */
/*
* The following equation computes
* "((rem & ~neg) - (rem & neg)) -= div;"
*/
z = neg | (div & ~rem);
y = (div ^ rem);
neg = (2 * z) & (~y);
rem = (2 * z) ^ y;
}
m *= 2;
div *= 2;
}
return (t);
}
uint32_t
mbin_div_odd32_alt3(uint32_t rem, uint32_t div)
{
uint8_t n;
div = -div + 1;
div = mbin_bitrev32(div / 2);
for (n = 1; n != 32; n++) {
rem += mbin_sumbits32(rem & (div >> (32 - n))) << n;
}
return (rem);
}
uint32_t
mbin_div_odd32_alt4(uint32_t rem, uint32_t div)
{
uint8_t n;
for (n = 1; n != 32; n++) {
if (div & (1 << n)) {
div = div + (div << n);
rem = rem + (rem << n);
}
}
return (rem);
}
uint32_t
mbin_div_odd32_alt5(uint32_t rem, uint32_t div)
{
/*
* Not as fast in hardware like the algorithm above, but same
* principle.
*/
if (!(div & 1))
return (0);
while (div != 1) {
rem = rem * div;
div = div * div;
}
return (rem);
}
uint32_t
mbin_div_odd32_alt6(uint32_t rem, uint32_t div)
{
uint32_t c;
uint8_t m;
/*
* Optimised version of alternative 5.
*/
if (!(div & 1))
return (0);
for (m = 1; m < 32; m *= 2) {
c = div & (((2 << m) - 1) << m);
div *= 1 - c;
rem *= 1 - c;
}
return (rem);
}
uint32_t
mbin_div_odd32_alt7(uint32_t rem, uint32_t div)
{
uint32_t f;
uint32_t m;
uint8_t y;
if (!(div & 1))
return (0);
for (y = 0; y != 5; y++) {
m = ((1 << (1 << y)) - 1) << (1 << y);
f = ((-(div & m)) & m) | 1;
rem *= f;
div *= f;
}
return (rem);
}
uint16_t
mbin_div_odd16(uint16_t r, uint16_t div)
{
uint16_t m;
div = -div + 1;
m = 1;
while (m) {
if (r & m) {
r += div;
}
m *= 2;
div *= 2;
}
return (r);
}
uint8_t
mbin_div_odd8(uint8_t r, uint8_t div)
{
uint8_t m;
div = -div + 1;
m = 1;
while (m) {
if (r & m) {
r += div;
}
m *= 2;
div *= 2;
}
return (r);
}
uint32_t
mbin_div_by3_32(uint32_t x)
{
uint32_t a = 0;
uint32_t n;
uint32_t r = 0;
uint32_t t = 3;
for (n = 0; n != 32; n += 2) {
/* sum and mod-3 */
t = t + (x & 3) + a;
/* get upper part */
a = (t / 4);
/* get lower part */
t = (t & 3);
/* store answer */
r |= t << n;
x >>= 2;
}
return (~r);
}
uint32_t
mbin_div_by3_32_alt1(uint32_t x)
{
uint32_t n;
uint32_t r;
uint32_t s;
r = 0;
s = 0;
for (n = 30; n != (uint32_t)-2; n -= 2) {
r |= (s << n);
s += (x >> n) & 3;
if (s >= 3) {
r += (1 << n);
s -= 3;
}
}
if (s & 1)
r -= 0x55555555;
if (s & 2)
r -= 0xAAAAAAAA;
return (r);
}
/*
* The following function divides a gray-coded value by three.
*/
uint32_t
mbin_div3_gray_32(uint32_t r)
{
uint32_t m;
for (m = 1; m != 0;) {
if (r & m) {
r ^= (2 * m) ^ (4 * m);
m *= 4;
} else {
m *= 2;
}
}
return (r);
}