forked from devatrun/slimcpplib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlong_math_long.h
More file actions
703 lines (507 loc) · 21.4 KB
/
Copy pathlong_math_long.h
File metadata and controls
703 lines (507 loc) · 21.4 KB
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Simple Long Integer Math for C++
// version 1.3
//
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
//
// Copyright (c) 2020-2021 Yury Kalmykov <y_kalmykov@mail.ru>.
//
// 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.
//
////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "long_math.h"
#if __has_include("long_math_gcc.h")
#include "long_math_gcc.h"
#endif // __has_include("long_math_gcc.h")
#if __has_include("long_math_msvc.h")
#include "long_math_msvc.h"
#endif // __has_include("long_math_msvc.h")
namespace slim
{
////////////////////////////////////////////////////////////////////////////////////////////////////
// type definitions
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, uint_t size>
class long_uint_t;
template<typename type_t, uint_t size>
class long_int_t;
} // namespace slim
namespace std
{
////////////////////////////////////////////////////////////////////////////////////////////////////
// class numeric_limits<slim::long_uint_t<type_t, size>>
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, size_t size>
struct numeric_limits<slim::long_uint_t<type_t, size>> {
public:
using long_uint_t = slim::long_uint_t<type_t, size>;
static const bool is_specialized = true;
static const bool is_signed = false;
static const bool is_integer = true;
static const bool is_exact = true;
static const bool has_infinity = false;
static const bool has_quiet_NaN = false;
static const bool has_signaling_NaN = false;
static const float_denorm_style has_denorm = denorm_absent;
static const bool has_denorm_loss = false;
static const float_round_style round_style = round_toward_zero;
static const bool is_iec559 = false;
static const bool is_bounded = false;
static const bool is_modulo = false;
static const int digits = 0;
static const int digits10 = 0;
static const int radix = 2;
static const int min_exponent = 0;
static const int min_exponent10 = 0;
static const int max_exponent = 0;
static const int max_exponent10 = 0;
static const bool traps = false;
static const bool tinyness_before = false;
static long_uint_t min() noexcept
{
return long_uint_t(0);
}
static constexpr long_uint_t lowest() noexcept
{
return long_uint_t(0);
}
static long_uint_t max() noexcept
{
return long_uint_t(-1);
}
static long_uint_t epsilon() noexcept
{
return long_uint_t(0);
}
static long_uint_t round_error() noexcept
{
return long_uint_t(0);
}
static long_uint_t infinity() noexcept
{
return long_uint_t(0);
}
static long_uint_t quiet_NaN() noexcept
{
return long_uint_t(0);
}
static long_uint_t signaling_NaN() noexcept
{
return long_uint_t(0);
}
static long_uint_t denorm_min() noexcept
{
return long_uint_t(0);
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// class numeric_limits<slim::long_int_t<type_t, size>>
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, size_t size>
struct numeric_limits<slim::long_int_t<type_t, size>> {
public:
using long_int_t = slim::long_int_t<type_t, size>;
static const bool is_specialized = true;
static const bool is_signed = true;
static const bool is_integer = true;
static const bool is_exact = true;
static const bool has_infinity = false;
static const bool has_quiet_NaN = false;
static const bool has_signaling_NaN = false;
static const float_denorm_style has_denorm = denorm_absent;
static const bool has_denorm_loss = false;
static const float_round_style round_style = round_toward_zero;
static const bool is_iec559 = false;
static const bool is_bounded = false;
static const bool is_modulo = false;
static const int digits = 0;
static const int digits10 = 0;
static const int radix = 2;
static const int min_exponent = 0;
static const int min_exponent10 = 0;
static const int max_exponent = 0;
static const int max_exponent10 = 0;
static const bool traps = false;
static const bool tinyness_before = false;
static long_int_t min() noexcept
{
return slim::long_uint_t<type_t, size>(1) << (slim::bit_count_v<long_int_t> - 1);
}
static constexpr long_int_t lowest() noexcept
{
return min();
}
static long_int_t max() noexcept
{
return ~slim::long_uint_t<type_t, size>(min());
}
static long_int_t epsilon() noexcept
{
return long_int_t(0);
}
static long_int_t round_error() noexcept
{
return long_int_t(0);
}
static long_int_t infinity() noexcept
{
return long_int_t(0);
}
static long_int_t quiet_NaN() noexcept
{
return long_int_t(0);
}
static long_int_t signaling_NaN() noexcept
{
return long_int_t(0);
}
static long_int_t denorm_min() noexcept
{
return long_int_t(0);
}
};
} // namespace std
namespace slim
{
////////////////////////////////////////////////////////////////////////////////////////////////////
// make_unsigned_t
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename native_t, uint_t size>
struct make_unsigned<long_uint_t<native_t, size>> {
using type = long_uint_t<native_t, size>;
};
template<typename native_t, uint_t size>
struct make_unsigned<long_int_t<native_t, size>> {
using type = long_uint_t<native_t, size>;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// make_signed_t
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename native_t, uint_t size>
struct make_signed<long_uint_t<native_t, size>> {
using type = long_int_t<native_t, size>;
};
template<typename native_t, uint_t size>
struct make_signed<long_int_t<native_t, size>> {
using type = long_int_t<native_t, size>;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// half_t
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, uint_t size>
struct half_type<long_uint_t<type_t, size>> {
using type = long_uint_t<type_t, size / 2>;
};
template<typename type_t>
struct half_type<long_uint_t<type_t, 2>> {
using type = type_t;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// standalone routines
////////////////////////////////////////////////////////////////////////////////////////////////////
// extract low half of unsigned integer
template<typename type_t, uint_t size>
constexpr long_uint_t<type_t, size / 2> half_lo(const long_uint_t<type_t, size>& value) noexcept;
template<typename type_t>
constexpr type_t half_lo(const long_uint_t<type_t, 2>& value) noexcept;
// extract high half of unsigned integer
template<typename type_t, uint_t size>
constexpr long_uint_t<type_t, size / 2> half_hi(const long_uint_t<type_t, size>& value) noexcept;
template<typename type_t>
constexpr type_t half_hi(const long_uint_t<type_t, 2>& value) noexcept;
// make unsigned integer from low and high
template<typename type_t, uint_t size>
constexpr long_uint_t<type_t, size> half_make(const long_uint_t<type_t, size / 2>& high, const long_uint_t<type_t, size / 2>& low) noexcept;
template<typename type_t>
constexpr long_uint_t<type_t, 2> half_make(const type_t& high, const type_t& low) noexcept;
// move low half to high
template<typename type_t, uint_t size>
constexpr long_uint_t<type_t, size> half_make_hi(const long_uint_t<type_t, size>& value) noexcept;
// return most significant bit
template<typename type_t, uint_t size>
constexpr bool sign(const long_uint_t<type_t, size>& value) noexcept;
template<typename type_t, uint_t size>
constexpr bool sign(const long_int_t<type_t, size>& value) noexcept;
// calculate leading zero bits
template<typename type_t, uint_t size>
constexpr uint_t nlz(const long_uint_t<type_t, size>& value) noexcept;
// multiply with carry
template<typename type_t, uint_t size>
constexpr long_uint_t<type_t, size> mul(long_uint_t<type_t, size>& value1, const long_uint_t<type_t, size>& value2) noexcept;
template<typename type_t, uint_t size>
constexpr long_uint_t<type_t, size> mulc(long_uint_t<type_t, size>& value1, const long_uint_t<type_t, size>& value2, const long_uint_t<type_t, size>& carry) noexcept;
// divide with remainder
template<typename type_t, uint_t size>
constexpr long_uint_t<type_t, size> divr(const long_uint_t<type_t, size>& value1, const long_uint_t<type_t, size>& value2, std::optional<long_uint_t<type_t, size>>& remainder) noexcept;
// negate vector
template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, int> = 0>
constexpr void negate(type_t& value) noexcept;
// add two vectors
template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, int> = 0>
constexpr void add(type_t& value1, const type_t& value2) noexcept;
// subtract two vectors
template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, int> = 0>
constexpr void sub(type_t& value1, const type_t& value2) noexcept;
// multiply two vectors
template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, int> = 0>
constexpr void mul(type_t& value1, const type_t& value2) noexcept;
////////////////////////////////////////////////////////////////////////////////////////////////////
// standalone routines
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, uint_t size>
constexpr long_uint_t<type_t, size / 2> half_lo(const long_uint_t<type_t, size>& value) noexcept
{
constexpr uint_t half_size = size / 2;
long_uint_t<type_t, half_size> result;
for (uint_t n = 0; n < half_size; n++)
result.digits[n] = value.digits[n];
return result;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t>
constexpr type_t half_lo(const long_uint_t<type_t, 2>& value) noexcept
{
return value.digits[0];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, uint_t size>
constexpr long_uint_t<type_t, size / 2> half_hi(const long_uint_t<type_t, size>& value) noexcept
{
constexpr uint_t half_size = size / 2;
long_uint_t<type_t, half_size> result;
for (uint_t n = 0; n < half_size; n++)
result.digits[n] = value.digits[n + half_size];
return result;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t>
constexpr type_t half_hi(const long_uint_t<type_t, 2>& value) noexcept
{
return value.digits[1];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, uint_t size>
constexpr long_uint_t<type_t, size> half_make(const long_uint_t<type_t, size / 2>& value_hi, const long_uint_t<type_t, size / 2>& value_lo) noexcept
{
constexpr uint_t half_size = size / 2;
long_uint_t<type_t, half_size> result;
for (uint_t n = 0; n < half_size; n++)
result.digits[n] = value_lo.digits[n];
for (uint_t n = half_size; n < size; n++)
result.digits[n] = value_hi.digits[n - half_size];
return result;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t>
constexpr long_uint_t<type_t, 2> half_make(const type_t& value_hi, const type_t& value_lo) noexcept
{
return long_uint_t<type_t, 2>({ value_lo, value_hi });
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, uint_t size>
constexpr long_uint_t<type_t, size> half_make_hi(const long_uint_t<type_t, size>& value) noexcept
{
using long_uint_t = long_uint_t<type_t, size>;
constexpr uint_t half_size = size / 2;
long_uint_t result;
for (uint_t n = 0; n < half_size; n++)
result.digits[n] = 0;
for (uint_t n = half_size; n < size; n++)
result.digits[n] = value.digits[n - half_size];
return result;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, uint_t size>
constexpr bool sign(const long_uint_t<type_t, size>& value) noexcept
{
using long_uint_t = long_uint_t<type_t, size>;
return make_signed_t<long_uint_t>(value.digits[long_uint_t::hi]) < 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, uint_t size>
constexpr bool sign(const long_int_t<type_t, size>& value) noexcept
{
return value.sign();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, uint_t size>
constexpr uint_t nlz(const long_uint_t<type_t, size>& value) noexcept
{
using long_uint_t = long_uint_t<type_t, size>;
uint_t count = 0;
for (uint_t n = std::size(value.digits); n-- > 0;) {
const uint_t scount = nlz(value.digits[n]);
count += scount;
if (scount < bit_count_v<type_t>)
break;
}
return count;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, uint_t size>
constexpr long_uint_t<type_t, size> mul(long_uint_t<type_t, size>& value1, const long_uint_t<type_t, size>& value2) noexcept
{
using long_uint_t = long_uint_t<type_t, size>;
using half_uint_t = half_t<long_uint_t>;
half_uint_t value1_lo = half_lo(value1);
half_uint_t value1_hi = half_hi(value1);
const half_uint_t value2_lo = half_lo(value2);
const half_uint_t value2_hi = half_hi(value2);
half_uint_t t0_lo = value1_lo;
half_uint_t t1_lo = value1_hi;
half_uint_t t2_lo = value1_lo;
half_uint_t t3_lo = value1_hi;
const half_uint_t t0_hi = mul(t0_lo, value2_lo);
const half_uint_t t1_hi = mulc(t1_lo, value2_lo, t0_hi);
const half_uint_t t2_hi = mulc(t2_lo, value2_hi, t1_lo);
const half_uint_t t3_hi = mulc(t3_lo, value2_hi, t2_hi);
value1_lo = t0_lo;
value1_hi = t2_lo;
half_uint_t carry_lo = t3_lo;
const half_uint_t carry_hi = t3_hi + add(carry_lo, t1_hi);
value1 = half_make(value1_hi, value1_lo);
return half_make(carry_hi, carry_lo);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, uint_t size>
constexpr long_uint_t<type_t, size> mulc(long_uint_t<type_t, size>& value1, const long_uint_t<type_t, size>& value2, const long_uint_t<type_t, size>& carry) noexcept
{
using long_uint_t = long_uint_t<type_t, size>;
using half_uint_t = half_t<long_uint_t>;
half_uint_t value1_lo = half_lo(value1);
half_uint_t value1_hi = half_hi(value1);
const half_uint_t value2_lo = half_lo(value2);
const half_uint_t value2_hi = half_hi(value2);
half_uint_t t0_lo = value1_lo;
half_uint_t t1_lo = value1_hi;
half_uint_t t2_lo = value1_lo;
half_uint_t t3_lo = value1_hi;
const half_uint_t t0_hi = mul(t0_lo, value2_lo);
const half_uint_t t1_hi = mulc(t1_lo, value2_lo, t0_hi);
const half_uint_t t2_hi = mulc(t2_lo, value2_hi, t1_lo);
const half_uint_t t3_hi = mulc(t3_lo, value2_hi, t2_hi);
value1_lo = t0_lo;
value1_hi = t2_lo;
half_uint_t carry_lo = t3_lo;
half_uint_t carry_hi = t3_hi + add(carry_lo, t1_hi);
bool add_carry = false;
if (carry != 0) {
add_carry = add(value1_lo, half_lo(carry));
add_carry = addc(value1_hi, half_hi(carry), add_carry);
}
carry_hi += add(carry_lo, half_uint_t(add_carry));
value1 = half_make(value1_hi, value1_lo);
return half_make(carry_hi, carry_lo);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, uint_t size>
constexpr long_uint_t<type_t, size> divr(const long_uint_t<type_t, size>& value1, const long_uint_t<type_t, size>& value2, std::optional<long_uint_t<type_t, size>>& remainder) noexcept
{
using long_uint_t = long_uint_t<type_t, size>;
using half_uint_t = half_t<long_uint_t>;
const half_uint_t dividend_lo = half_lo(value1);
const half_uint_t dividend_hi = half_hi(value1);
const half_uint_t divider_lo = half_lo(value2);
const half_uint_t divider_hi = half_hi(value2);
long_uint_t quotient;
if (divider_hi == 0) {
half_uint_t quotient_lo;
half_uint_t quotient_hi;
std::optional<half_uint_t> remainder_lo = remainder ? half_uint_t() : std::optional<half_uint_t>();
if (divider_lo > dividend_hi) {
if (dividend_hi == 0)
quotient_lo = divr<half_uint_t>(dividend_lo, divider_lo, remainder_lo);
else
quotient_lo = divr2<half_uint_t>(dividend_hi, dividend_lo, divider_lo, remainder_lo);
quotient_hi = 0;
} else {
quotient_lo = divr2<half_uint_t>(dividend_hi % divider_lo, dividend_lo, divider_lo, remainder_lo);
quotient_hi = dividend_hi / divider_lo;
}
quotient = half_make(quotient_hi, quotient_lo);
if (remainder)
remainder = half_make(half_uint_t(0), *remainder_lo);
} else {
quotient = divr2<long_uint_t>(0, value1, value2, remainder);
}
return quotient;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, int>>
constexpr void negate(type_t& value) noexcept
{
using value_t = typename type_t::value_type;
bool borrow = true;
for (uint_t n = 0; n < std::size(value); ++n) {
borrow = subb(value[n], value_t(0), borrow);
value[n] = ~value[n];
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, int>>
constexpr void add(type_t& value1, const type_t& value2) noexcept
{
bool carry = add(value1[0], value2[0]);
for (uint_t n = 1; n < std::size(value1); ++n)
carry = addc(value1[n], value2[n], carry);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, int>>
constexpr void sub(type_t& value1, const type_t& value2) noexcept
{
bool borrow = sub(value1[0], value2[0]);
for (uint_t n = 1; n < std::size(value1); ++n)
borrow = subb(value1[n], value2[n], borrow);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, int>>
constexpr void mul(type_t& value1, const type_t& value2) noexcept
{
using value_t = typename type_t::value_type;
type_t result;
result[0] = value1[0];
value_t carry = mul(result[0], value2[0]);
for (uint_t n = 1; n < std::size(value1); ++n) {
result[n] = value1[n];
carry = mulc(result[n], value2[0], carry);
}
for (uint_t n = 1; n < std::size(value1); ++n) {
type_t tmp;
tmp[0] = 0;
for (uint_t k = 1; k < n; ++k)
tmp[k] = 0;
tmp[n] = value1[0];
carry = mul(tmp[n], value2[n]);
for (uint_t k = 1; k < std::size(value1) - n; ++k) {
tmp[k + n] = value1[k];
carry = mulc(tmp[k + n], value2[n], carry);
}
add(result, tmp);
}
value1 = result;
}
} // namespace slim
////////////////////////////////////////////////////////////////////////////////////////////////////
// End of long_math_long.h
////////////////////////////////////////////////////////////////////////////////////////////////////