-
Notifications
You must be signed in to change notification settings - Fork 0
/
testnum.h
500 lines (407 loc) · 12.5 KB
/
testnum.h
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
* File: testnum.h
* Author: Daniel Hannon
*
* Copyright: 2023 Daniel Hannon
*/
#ifndef TESTNUM_H_A9029654B9334CB6AF4DC1861248C6DD
#define TESTNUM_H_A9029654B9334CB6AF4DC1861248C6DD 1
#include <algorithm>
#include <compare>
#include <cstddef>
#include <iostream>
#include <array>
#include <cstdint>
#include <ostream>
#include <utility>
#include "helpers.h"
// TestNum is a proof-of-concept for large integer types
// It replicates a uint32 using only characters to test algorithms
// for numbers that are greater than the largest integer type
// (which this very much is not)
struct TestNum {
public:
TestNum(): m_data{0,0,0,0}
{}
TestNum(std::uint32_t val): m_data{0,0,0,0}
{
m_data[0] = val;
m_data[1] = val >> 8;
m_data[2] = val >> 16;
m_data[3] = val >> 24;
}
TestNum(TestNum const& t) : m_data{t.m_data}
{}
TestNum(TestNum&& t) : m_data{std::move(t.m_data)}
{}
// Output the number as a uint32 because it's equivalent to that anyway
std::uint32_t to_uint32() const {
std::uint32_t outp = 0;
outp ^= m_data[0] & 0x000000FF;
outp ^= (m_data[1] << 8) & 0x0000FF00;
outp ^= (m_data[2] << 16) & 0x00FF0000;
outp ^= (m_data[3] << 24) & 0xFF000000;
return outp;
}
// Comparators
friend bool operator==(TestNum const& lhs, TestNum const& rhs);
friend bool operator!=(TestNum const& lhs, TestNum const& rhs);
friend std::weak_ordering operator<=>(TestNum const& lhs, TestNum const& rhs);
// Assignment Operator
TestNum& operator=(TestNum const& t) {
if(this == &t) {
return *this;
}
m_data = t.m_data;
return *this;
}
TestNum& operator=(std::uint32_t const& val) {
m_data[0] = val;
m_data[1] = val >> 8;
m_data[2] = val >> 16;
m_data[3] = val >> 24;
return *this;
}
// Arithmetic Operators
TestNum operator+(TestNum const& val) const {
TestNum res{*this};
res += val;
return res;
}
TestNum& operator++() {
*this += 1;
return *this;
}
TestNum& operator+=(TestNum const& t) {
std::uint16_t buff = 0;
for(std::size_t i = 0; i < 4; i++) {
buff += (m_data[i]&0x00FF) + (t.m_data[i]&0x00FF);
m_data[i] = (buff & 0x00FF);
buff = (buff >> 8) & 0x00FF;
}
return *this;
}
TestNum operator-(TestNum const& val) const {
TestNum res{*this};
res -= val;
return res;
}
TestNum& operator--() {
auto tmp = *this - 1;
m_data.swap(tmp.m_data);
return *this;
}
TestNum& operator-=(TestNum const& t) {
std::uint16_t tmp = 0;
std::uint16_t buff = 0;
for(std::size_t i = 0; i < 4; i++) {
tmp = m_data[i] & 0x00FF;
buff = tmp - (t.m_data[i]&0x00FF) - buff;
m_data[i] = buff & 0x00FF;
// Underflow can occur.
buff = (buff > tmp);
}
return *this;
}
TestNum operator*(TestNum const& t) const {
if(t == TestNum{0}) {
return 0;
}
TestNum outp{0};
std::uint16_t buff = 0;
std::uint16_t tmp = 0;
// This is n^2 because I'm not bothered making this more efficient yet
for(std::size_t i = 0; i < 4; i++) {
// avoid pointless operations
if(t.m_data[i] == 0) continue;
for(std::size_t j = 0; j + i < 4; j++) {
tmp = (m_data[j] & 0xFF);
tmp *= (t.m_data[i] & 0xFF);
buff += tmp;
buff += (outp.m_data[j + i] & 0xFF);
outp.m_data[j + i] = (buff& 0xFF);
buff = buff >> 8;
}
buff = 0;
}
return outp;
}
TestNum& operator*=(TestNum const& t) {
auto tmp = TestNum{*this};
tmp = tmp * t;
m_data.swap(tmp.m_data);
return *this;
}
TestNum operator/(TestNum const& t) const {
auto tmp = this->simple_divide(t);
return tmp.first;
}
TestNum& operator/=(TestNum const& t) {
auto tmp = this->operator/(t);
m_data.swap(tmp.m_data);
return *this;
}
TestNum operator%(TestNum const& t) const {
auto tmp = this->simple_divide(t);
return tmp.second;
}
TestNum& operator%=(TestNum const& t) {
auto tmp = *this % t;
m_data.swap(tmp.m_data);
return *this;
}
// Bitwise operators
// Shift left operator for the TestNum type, it's proof-of-concept so idk has to be done and whatever
TestNum operator<<(std::size_t const& val) const {
TestNum temp{0};
auto byte_offset = val >> 3;
auto bit_offset = val & 0x7;
std::uint16_t buff = 0;
if(byte_offset >= 4) {
return 0;
}
for(auto i = 0; (i + byte_offset) < 4; i++) {
buff = buff >> 8;
buff ^= ((m_data[i] & 0xFF) << bit_offset);
temp.m_data[byte_offset + i] = (buff & 0xFF);
}
return temp;
}
// Shift right operator does what you'd expect :D
TestNum operator>>(std::size_t const& val) const {
TestNum temp{0};
auto byte_offset = val >> 3;
auto bit_offset = val & 0x7;
std::uint16_t buff = 0;
if(byte_offset >= 4) {
return 0;
}
for(auto i = 0; (i + byte_offset) < 4; i++) {
buff = buff << (8 - bit_offset);
buff ^= ((m_data[3 - i] & 0xFF) >> bit_offset);
temp.m_data[3 - (byte_offset + i)] = buff;
buff = m_data[3-i] & 0xFF;
}
return temp;
}
// Bitwise Operators
TestNum operator&(TestNum const& test) const {
TestNum val{*this};
for(std::size_t idx = 0; idx < 4; idx++) {
val.m_data[idx] &= test.m_data[idx];
}
return val;
}
TestNum& operator&=(TestNum const& test) {
auto val = operator&(test);
m_data.swap(val.m_data);
return *this;
}
TestNum operator^(TestNum const& mask) const {
TestNum val{*this};
for(std::size_t idx = 0; idx < 4; idx++) {
val.m_data[idx] ^= mask.m_data[idx];
}
return val;
}
TestNum& operator^=(TestNum const& mask) {
auto val = operator^(mask);
m_data.swap(val.m_data);
return *this;
}
TestNum operator|(TestNum const& cmp) const {
TestNum val{*this};
for(std::size_t idx = 0; idx < 4; idx++) {
val.m_data[idx] |= cmp.m_data[idx];
}
return val;
}
TestNum& operator|=(TestNum const& cmp) {
auto val = operator|(cmp);
m_data.swap(val.m_data);
return *this;
}
TestNum operator~() const {
TestNum val{*this};
for(auto & c : val.m_data) {
c = ~c;
}
return val;
}
// Ostream
friend std::ostream & operator<<(std::ostream & os, TestNum const& v);
// Test Code
std::size_t get_most_populated() const;
std::pair<TestNum,TestNum> split_at(std::size_t idx) const;
static TestNum karatsuba_multiplication(TestNum const& v1, TestNum const& v2);
private:
// Internal divide maybe make this public?
std::pair<TestNum, TestNum> divide_by(TestNum const& val);
std::pair<TestNum, TestNum> simple_divide(TestNum const& val) const;
std::array<std::uint8_t,4> m_data; // The data stored within
};
//Equivalance operator
inline bool operator==(TestNum const& lhs, TestNum const& rhs) {
return std::is_eq(lhs <=> rhs);
}
inline bool operator!=(TestNum const& lhs, TestNum const& rhs) {
return !(lhs==rhs);
}
inline std::weak_ordering operator<=>(TestNum const& lhs, TestNum const& rhs) {
if(&lhs == &rhs) return std::weak_ordering::equivalent;
for(auto idx = 4; idx > 0; idx--) {
if(auto cmp = lhs.m_data[idx - 1] <=> rhs.m_data[idx - 1]; cmp != 0) {
return cmp;
}
}
return std::weak_ordering::equivalent;
}
inline std::ostream& operator<<(std::ostream & os, TestNum const& v) {
os << v.to_uint32();
return os;
}
inline std::size_t TestNum::get_most_populated() const {
if(m_data[3] != 0) return 3;
if(m_data[2] != 0) return 2;
if(m_data[1] != 0) return 1;
return 0;
}
inline std::pair<TestNum,TestNum> TestNum::split_at(std::size_t idx) const {
std::cout << "Splitting at idx: " << idx << std::endl;
TestNum left = 0;
TestNum right = 0;
int j = 0;
for(int i = idx; i <= 3; i++) {
left.m_data[j] = m_data[i];
j++;
}
for(int i = 0; i < idx; i++) {
right.m_data[i] = m_data[i];
}
std::cout << "Left: " << left << ", Right: " << right << std::endl;
return {left, right};
}
// Multiplication using the Karatsuba Multiplication formula, this has an O(n^1.56) as opposed to
// standard multiplication which is O(n^2). It's not particularly useful for this class but,
// it's a good testbench for a larger number as that is algorithm is very much portable
inline TestNum TestNum::karatsuba_multiplication(TestNum const& v1,TestNum const& v2) {
if((v1.get_most_populated() == 0) || (v2.get_most_populated() == 0)) {
return v1 * v2;
}
std::size_t split_point = std::max(v1.get_most_populated(),v2.get_most_populated());
split_point = (split_point >> 1) + (split_point != 0);
if(split_point == 0) {
return v1 * v2;
}
auto p1 = v1.split_at(split_point);
auto p2 = v2.split_at(split_point);
auto r0 = TestNum::karatsuba_multiplication(p1.second, p2.second);
auto r1 = TestNum::karatsuba_multiplication((p1.first + p1.second), (p2.first + p2.second));
auto r2 = TestNum::karatsuba_multiplication(p1.first, p2.first);
std::cout << "Split Point: " << split_point << ", r0: " << r0 << ", r1: " << r1 << ", r2: " << r2 << std::endl;
// The 16 and 8 represent byte offsets as the base here is 256
// and I can just bitshift instead of doing a multiplication which makes this cleaner
auto comp1 = (r2 << (split_point * 16));
auto comp2 = ((r1 - r2) - r0) << (split_point * 8);
std::cout << "Sum is: " << comp1 << " + " << comp2 << " + " << r0 << std::endl;
return comp1 + comp2 + r0;
}
// This is going to utilize Knuths Algorithm D
// TODO:Complete
inline std::pair<TestNum, TestNum> TestNum::divide_by(TestNum const& divisor) {
static constexpr std::uint16_t base = 256;
if(divisor == TestNum{0} || *this == TestNum{0}) {
return {0,0};
}
if(*this == divisor) {
return {1,0};
}
/*
* Algorithm D overview
*
* D0:
* Let U be the dividend of m+n digits stored in an array of m+n+1 digits with LSD at U[0] and Most Significant at U[m+n-1]
* Let V be the divisor of n digits stored in a second array of n elements with n > 1 most Significant non zero digit at V[n-1] and least Significant digit at V[0]
* Let B be the base of the "digits" in this case we are using base 256
* D1:
* Normalize by multiplying U and V by some D (since this is a base2 computer we can just do bitshifts for this :D)
*/
// Get number of bitshifts :D
std::size_t m = 0;
std::size_t n = 0;
for(n = 3 ;n != 0;n--) {
if(divisor.m_data[n] != 0) break;
}
for(m = 3; m != 0; m--) {
if (m_data[m] != 0) break;
}
if(m < n) {
return {0,*this};
}
// Algorithm D requires that the divisor be at least two digits in length
// Otherwise we just do schoolboy division which is fine.
if(n == 0) {
std::cout << "Divisor is less than 256 using Schoolboy division" << std::endl;
// Schoolboy division
std::uint32_t dividend = 0;
std::uint32_t remainder = 0;
std::uint16_t buffer = 0;
for(int idx = 3; idx >= 0; idx--) {
buffer = buffer << 8;
dividend = dividend << 8;
buffer ^= (m_data[idx] & 0xFF);
dividend ^= ((buffer / divisor.m_data[0]) & 0xFF);
buffer = buffer % divisor.m_data[0];
}
remainder = buffer & 0x000000FF;
return {dividend,remainder};
}
// Normalize data
auto v = divisor << clz_uint8(divisor.m_data[n]);
auto u = *this << clz_uint8(divisor.m_data[n]);
m -= n;
n++;
m++;
for(auto j = m; j >= 0; j--) {
std::uint16_t q_p = (u.m_data[n + j] << 16) ^ (u.m_data[n + j - 1] & 0x00FF) / v.m_data[n - 1];
std::uint16_t r_p = (u.m_data[n + j] << 16) ^ (u.m_data[n + j - 1] & 0x00FF) % v.m_data[n - 1];
do {
if ((q_p == base) ||
((q_p * u.m_data[n-2]) > ((r_p << 16 ) ^ (u.m_data[n - 2 + j] & 0x00FF)))) {
q_p--;
r_p += v.m_data[n-1];
}
} while(r_p < base);
}
return {std::move(u),0};
}
// this just does standard long division whatever
inline std::pair<TestNum, TestNum> TestNum::simple_divide(TestNum const& t) const {
// Division by zero is undefined in the C++ standard, so
// I think short circuiting the division is the best approach.
if(t == 0) return {0,0};
TestNum remainder{*this};
TestNum qty{0};
std::size_t len_divisor = t.get_most_populated();
std::size_t len_dividend = get_most_populated();
// divisor bigger so remainder is just the number
if(len_divisor > len_dividend) return {qty, remainder};
int dividend_pop_bits = (8 - clz_uint8(m_data[len_dividend])) + (len_dividend * 8);
int divisor_pop_bits = (8 - clz_uint8(t.m_data[len_divisor])) + (len_divisor * 8);
int operations = dividend_pop_bits - divisor_pop_bits;
//int operations = (((3 - len_divisor) * 8) + clz_uint8(t.m_data[len_divisor])) - (((3 - len_dividend) * 8) + clz_uint8(t.m_data[len_dividend]));
if(operations < 0) return {0, remainder};
// This may produce undefined behavior but it won't matter because the result will be discarded
TestNum divisor = t << operations;
while((divisor >= t) && (operations >= 0)) {
qty = qty << 1;
if(remainder >= divisor) {
remainder -= divisor;
qty += 1;
}
divisor = divisor >> 1;
operations--;
}
return {qty, remainder};
}
#endif // TESTNUM_H_A9029654B9334CB6AF4DC1861248C6DD