-
Notifications
You must be signed in to change notification settings - Fork 1
/
Aeu.h
executable file
·1658 lines (1452 loc) · 62.7 KB
/
Aeu.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
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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2021-2023, Alexander V. Lvov
* All rights reserved.
*
* 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
**/
/**
* @file Aeu.h
* @brief Long precision unsigned integer with arithmetic operations
*/
#ifndef AEU_MULTIPRECISION
#define AEU_MULTIPRECISION
/// @cond HIDE_INCLUDES
#include <iostream>
#include <cassert>
#ifdef __CUDACC__
#define gpu __host__ __device__
#include <cuda/std/utility>
#include <cuda/std/array>
#else
#define gpu
#include <utility>
#include <array>
#endif
/// @endcond
#if defined AESI_UNSAFE
#warning Enabled nightly mode for the library. Functions and methods input arguments are not checked for validity. Be really gentle
#endif
#ifdef AESI_CRYPTOPP_INTEGRATION
#include <cryptopp/integer.h>
#endif
#ifdef AESI_GMP_INTEGRATION
#include <gmpxx.h>
#endif
namespace {
using byte = uint8_t;
using block = uint32_t;
constexpr inline std::size_t bitsInByte = 8;
constexpr inline std::size_t blockBitLength = sizeof(block) * bitsInByte;
constexpr inline uint64_t blockBase = 1ULL << blockBitLength;
constexpr inline block blockMax = 0xff'ff'ff'ffu;
/**
* @enum Comparison
* @brief Analogue of STD::Strong_ordering since CUDA does not support <=> operator
*/
enum class Comparison { equal = 0, less = 1, greater = 2, equivalent = 3 };
}
/**
* @class Aeu
* @brief Long precision unsigned integer
* @details May be used to represent only positive integers. Number precision is set in template parameter bitness.
*/
template <std::size_t bitness = 512> requires (bitness % blockBitLength == 0)
class Aeu final {
static_assert(bitness > sizeof(uint64_t), "Use built-in types for numbers 64-bit or less.");
static constexpr std::size_t blocksNumber = bitness / blockBitLength;
#ifdef __CUDACC__
template <typename T1, typename T2>
using pair = cuda::std::pair<T1, T2>;
using blockLine = cuda::std::array<block, blocksNumber>;
#else
template<typename T1, typename T2>
using pair = std::pair<T1, T2>;
using blockLine = std::array<block, blocksNumber>;
#endif
/* -------------------------- @name Class members. ----------------------- */
/**
* @brief Block line of the number
*/
blockLine blocks;
/* ----------------------------------------------------------------------- */
/* ------------------------ @name Helper functions. ---------------------- */
/**
* @brief Makes line addition
* @param dst BlockLine
* @param src BlockLine
* @return Uint64 - carry out from addition
*/
gpu static constexpr auto addLine(blockLine& dst, const blockLine& src) noexcept -> uint64_t {
uint64_t carryOut = 0;
for (std::size_t i = 0; i < blocksNumber; ++i) {
uint64_t sum = static_cast<uint64_t>(dst[i]) + static_cast<uint64_t>(src[i]) + carryOut;
carryOut = sum / blockBase; dst[i] = static_cast<block>(sum % blockBase);
}
return carryOut;
}
/**
* @brief Makes complement block line for line
* @param line BlockLine
* @return BlockLine
*/
gpu static constexpr auto makeComplement(blockLine& line) noexcept -> uint64_t {
uint64_t carryOut = 1;
for(std::size_t i = 0; i < blocksNumber; ++i) {
const uint64_t sum = blockBase - 1ULL - static_cast<uint64_t>(line[i]) + carryOut;
carryOut = sum / blockBase; line[i] = static_cast<block>(sum % blockBase);
}
return carryOut;
}
/* ----------------------------------------------------------------------- */
public:
/* --------------------- @name Different constructors. ------------------- */
/**
* @brief Default constructor
*/
gpu constexpr Aeu() noexcept = default;
/**
* @brief Destructor
*/
gpu constexpr ~Aeu() noexcept = default;
/**
* @brief Copy constructor
*/
gpu constexpr Aeu(const Aeu& copy) noexcept = default;
/**
* @brief Copy assignment operator
* @param other Aeu
*/
gpu constexpr Aeu& operator=(const Aeu& other) = default;
/**
* @brief Integral constructor
* @param value Integral
* @details Accepts both signed and unsigned built-in integral types. When calling this constructor on negative value, final blocks would be inverted.
* @note Be aware of calling this constructor explicitly
*/
template <typename Integral> requires (std::is_unsigned_v<Integral>)
gpu constexpr Aeu(Integral value) noexcept {
if(value != 0) {
uint64_t tValue = (value < 0 ? static_cast<uint64_t>(value * -1) : static_cast<uint64_t>(value));
for (std::size_t i = 0; i < blocksNumber; ++i) {
blocks[i] = static_cast<block>(tValue % blockBase);
tValue /= blockBase;
}
if(value < 0)
makeComplement(blocks);
} else
blocks = {};
}
/**
* @brief Pointer-based character constructor
* @param data Char*
* @param size Size_t
* @details Accepts decimal strings (no prefix), binary (0b/0B), octal (0o/0O) and hexadecimal (0x/0X)
*/
template <typename Char> requires (std::is_same_v<Char, char> || std::is_same_v<Char, wchar_t>)
gpu constexpr Aeu(const Char* data, std::size_t size) noexcept : Aeu {} {
if(size == 0) return;
constexpr const Char* characters = [] {
if constexpr (std::is_same_v<char, Char>) {
return "09aAfFoObBxX";
} else {
return L"09aAfFoObBxX";
}
} ();
std::size_t position = 0;
if constexpr (std::is_same_v<Char, char>) {
for(; position < size && !std::isalnum(data[position]); ++position) ;
} else {
for(; position < size && !std::iswalnum(data[position]); ++position) ;
}
if(position == size)
return;
const auto base = [&data, &size, &position, &characters] {
if (data[position] == characters[0] && size > position + 1) {
switch (data[position + 1]) {
case characters[8]:
case characters[9]:
position += 2; return 2u;
case characters[6]:
case characters[7]:
position += 2; return 8u;
case characters[10]:
case characters[11]:
position += 2; return 16u;
default:
return 10u;
}
} return 10u;
} ();
for(; position < size; ++position) {
const auto digit = [] (Char ch) {
if(characters[0] <= ch && ch <= characters[1])
return static_cast<unsigned>(ch) - static_cast<unsigned>(characters[0]);
if(characters[2] <= ch && ch <= characters[4])
return static_cast<unsigned>(ch) - static_cast<unsigned>(characters[2]) + 10u;
if(characters[3] <= ch && ch <= characters[5])
return static_cast<unsigned>(ch) - static_cast<unsigned>(characters[3]) + 10u;
return 99u;
} (data[position]);
if(digit < base) {
*this *= base;
*this += digit;
}
}
}
/**
* @brief C-style string literal constructor
* @param literal Char[]
* @details Accepts decimal literals along with binary (starting with 0b/0B), octal (0o/0O) and hexadecimal (0x/0X)
*/
template <typename Char, std::size_t arrayLength> requires (arrayLength > 1 && (std::is_same_v<Char, char> || std::is_same_v<Char, wchar_t>))
gpu constexpr Aeu(const Char (&literal)[arrayLength]) noexcept : Aeu(literal, arrayLength) {}
/**
* @brief String or string-view based constructor
* @param stringView String/String-View
* @details Constructs object from STD::Basic_String or STD::Basic_String_View. Accepts objects based on char or wchar_t
*/
template <typename String, typename Char = typename String::value_type> requires (std::is_same_v<std::basic_string<Char>,
std::decay_t<String>> || std::is_same_v<std::basic_string_view<Char>, std::decay_t<String>>)
gpu constexpr Aeu(String&& stringView) noexcept : Aeu(stringView.data(), stringView.size()) {}
template <typename String, typename Char = typename String::value_type> requires (std::is_same_v<std::basic_string<Char>,
std::decay_t<String>> || std::is_same_v<std::basic_string_view<Char>, std::decay_t<String>>)
gpu constexpr Aeu(const String& stringView) noexcept : Aeu(stringView.data(), stringView.size()) {}
#ifdef AESI_CRYPTOPP_INTEGRATION
/**
* @brief Constructor from the CryptoPP library integer value
* @param value CryptoPP::Integer
*/
constexpr Aeu(const CryptoPP::Integer& value): Aeu {} {
const auto byteCount = value.ByteCount();
if(byteCount * 8 > bitness)
throw std::invalid_argument("Accessed overflow on construction object from CryptoPP::Integer");
for(std::size_t i = 0; i < byteCount; ++i)
setByte(i, value.GetByte(i));
}
#endif
#ifdef AESI_GMP_INTEGRATION
/**
* @brief Constructor from the GMP library integer value
* @param value mpz_class
*/
constexpr Aeu(const mpz_class& value) : Aeu {} {
const auto bitLength = mpz_sizeinbase(value.get_mpz_t(), 2);
auto tBlocksNumber = 1 + (bitLength - 1) / sizeof(block) * 8;
std::basic_string<block> buffer (tBlocksNumber, 0u);
mpz_export(buffer.data(), &tBlocksNumber, -1, sizeof(block), -1, 0, value.get_mpz_t());
for (std::size_t i = 0; i < tBlocksNumber; ++i)
setBlock(i, buffer[i]);
}
#endif
/* ----------------------------------------------------------------------- */
/* --------------------- @name Arithmetic operators. --------------------- */
/* ------------------------- @name Unary operators. -------------------------- */
/**
* @brief Unary plus operator
* @return Aeu
* @note Does basically nothing
*/
[[nodiscard]]
gpu constexpr auto operator+() const noexcept -> Aeu { return *this; }
/**
* @brief Unary minus operator
* @return Aeu
*/
[[nodiscard]]
gpu constexpr auto operator-() const noexcept -> Aeu {
Aeu result = *this; makeComplement(result.blocks); return result;
}
/**
* @brief Prefix increment
* @return Aeu&
*/
gpu constexpr auto operator++() noexcept -> Aeu& {
for(std::size_t i = 0; i < blocksNumber; ++i) {
if(blocks[i] < blockMax) {
++blocks[i];
break;
} else blocks[i] = 0u;
}
return *this;
}
/**
* @brief Postfix increment
* @return Aeu
*/
gpu constexpr auto operator++(int) & noexcept -> Aeu { Aeu old = *this; operator++(); return old; }
/**
* @brief Prefix decrement
* @return Aeu&
*/
gpu constexpr auto operator--() noexcept -> Aeu& {
for(std::size_t i = 0; i < blocksNumber; ++i) {
if(blocks[i] > 0u) {
--blocks[i]; break;
}
blocks[i] = blockMax;
}
return *this;
}
/**
* @brief Postfix decrement
* @return Aeu
*/
gpu constexpr auto operator--(int) & noexcept -> Aeu { Aeu old = *this; operator--(); return old; }
/* --------------------------------------------------------------------------- */
/* ------------------------ @name Addition operators. ------------------------ */
/**
* @brief Addition operator for built-in integral types
* @param addition Aeu
* @param addendum Unsigned
* @return Aeu
*/
template <typename Unsigned> requires (std::is_unsigned_v<Unsigned>) [[nodiscard]]
gpu constexpr friend auto operator+(const Aeu& addition, Unsigned addendum) noexcept -> Aeu {
Aeu result = addition; result += addendum; return result;
}
/**
* @brief Addition operator
* @param addition Aeu
* @param addendum Aeu
* @return Aeu
*/
[[nodiscard]]
gpu constexpr friend auto operator+(const Aeu& addition, const Aeu& addendum) noexcept -> Aeu {
Aeu result = addition; result += addendum; return result;
}
/**
* @brief Assignment addition operator for built-in integral types
* @param addition Aeu
* @param addendum Unsigned
* @return Aeu&
*/
template <typename Unsigned> requires (std::is_unsigned_v<Unsigned>)
gpu constexpr friend auto operator+=(Aeu& addition, Unsigned addendum) noexcept -> Aeu& {
for(std::size_t i = 0; i < blocksNumber; ++i) {
const auto currentSum = static_cast<uint64_t>(addition.blocks[i]) + static_cast<uint64_t>(addendum);
addendum = currentSum / blockBase; addition.blocks[i] = currentSum % blockBase;
}
return addition;
}
/**
* @brief Assignment addition operator
* @param addition Aeu
* @param addendum Aeu
* @return Aeu&
*/
gpu constexpr friend auto operator+=(Aeu& addition, const Aeu& addendum) noexcept -> Aeu& {
addLine(addition.blocks, addendum.blocks); return addition;
}
/* --------------------------------------------------------------------------- */
/* ----------------------- @name Subtraction operators. ---------------------- */
/**
* @brief Subtraction operator
* @param subtraction Aeu
* @param subtrahend Aeu
* @return Aeu
*/
[[nodiscard]]
gpu constexpr friend auto operator-(const Aeu& subtraction, const Aeu& subtrahend) noexcept -> Aeu {
Aeu result = subtraction; result -= subtrahend; return result;
}
/**
* @brief Assignment subtraction operator
* @param subtraction Aeu
* @param subtrahend Aeu
* @return Aeu&
*/
gpu constexpr friend auto operator-=(Aeu& subtraction, const Aeu& subtrahend) noexcept -> Aeu& {
return subtraction += -subtrahend;
}
/* --------------------------------------------------------------------------- */
/* --------------------- @name Multiplication operators. --------------------- */
/**
* @brief Multiplication operator for built-in integral types
* @param multiplication Aeu
* @param factor Unsigned
* @return Aeu
*/
template <typename Unsigned> requires (std::is_unsigned_v<Unsigned>) [[nodiscard]]
gpu constexpr friend auto operator*(Aeu& multiplication, Unsigned factor) noexcept -> Aeu {
Aeu result = multiplication; result *= factor; return result;
}
/**
* @brief Multiplication operator
* @param multiplication Aeu
* @param factor Aeu
* @return Aeu
*/
[[nodiscard]]
gpu constexpr friend auto operator*(const Aeu& multiplication, const Aeu& factor) noexcept -> Aeu {
Aeu result = multiplication; result *= factor; return result;
}
/**
* @brief Assignment multiplication operator for built-in integral types
* @param multiplication Aeu
* @param factor Unsigned
* @return Aeu&
* @details Works with the greatest performance with types smaller than uint64_t
*/
template <typename Unsigned> requires (std::is_unsigned_v<Unsigned>)
gpu constexpr friend auto operator*=(Aeu& multiplication, Unsigned factor) noexcept -> Aeu& {
if constexpr (std::is_same_v<Unsigned, uint64_t>) {
const auto longerLength = multiplication.filledBlocksNumber();
const auto smallerLength = (factor > blockMax ? 2UL : 1UL);
blockLine buffer {};
for(std::size_t i = 0; i < longerLength; ++i) {
const uint64_t tBlock = multiplication.blocks[i];
uint64_t carryOut = 0;
for(std::size_t j = 0; j < smallerLength && j < buffer.size() - i; ++j) {
const auto product = tBlock * (factor >> blockBitLength * j & 0x00'00'00'00'ff'ff'ff'ff) + carryOut;
const auto block = static_cast<uint64_t>(buffer[i + j]) + product % blockBase;
carryOut = product / blockBase + block / blockBase;
buffer[i + j] = block % blockBase;
}
if(smallerLength + i < buffer.size())
buffer[smallerLength + i] += carryOut;
}
multiplication.blocks = buffer;
return multiplication;
} else {
uint64_t carryOut = 0;
for (std::size_t i = 0; i < blocksNumber; ++i) {
const auto product = static_cast<uint64_t>(factor) * static_cast<uint64_t>(multiplication.blocks[i]) + carryOut;
multiplication.blocks[i] = product % blockBase; carryOut = product / blockBase;
}
return multiplication;
}
}
/**
* @brief Assignment multiplication operator
* @param multiplication Aeu
* @param factor Aeu
* @return Aeu&
*/
gpu constexpr friend auto operator*=(Aeu& multiplication, const Aeu& factor) noexcept -> Aeu& {
constexpr auto multiplyLines = [] (const blockLine& longerLine, const std::size_t longerLength,
const blockLine& smallerLine, const std::size_t smallerLength) {
blockLine buffer {};
for(std::size_t i = 0; i < longerLength; ++i) {
const uint64_t tBlock = longerLine[i];
uint64_t carryOut = 0;
for(std::size_t j = 0; j < smallerLength && j < buffer.size() - i; ++j) {
const auto product = tBlock * static_cast<uint64_t>(smallerLine[j]) + carryOut;
const auto block = static_cast<uint64_t>(buffer[i + j]) + product % blockBase;
carryOut = product / blockBase + block / blockBase;
buffer[i + j] = block % blockBase;
}
if(smallerLength < blocksNumber && smallerLength + i < buffer.size())
buffer[smallerLength + i] += carryOut;
}
return buffer;
};
const std::size_t thisLength = multiplication.filledBlocksNumber();
if(const std::size_t valueLength = factor.filledBlocksNumber(); thisLength > valueLength)
multiplication.blocks = multiplyLines(multiplication.blocks, thisLength, factor.blocks, valueLength);
else
multiplication.blocks = multiplyLines(factor.blocks, valueLength, multiplication.blocks, thisLength);
return multiplication;
}
/* --------------------------------------------------------------------------- */
/* ------------------------ @name Division operators. ------------------------ */
/**
* @brief Division operator
* @param division Aeu
* @param divisor Aeu
* @return Aeu
* @note Undefined behaviour for division by zero
*/
[[nodiscard]]
gpu constexpr friend auto operator/(const Aeu& division, const Aeu& divisor) noexcept -> Aeu {
Aeu quotient, _; divide(division, divisor, quotient, _); return quotient;
}
/**
* @brief Assignment division operator
* @param division Aeu
* @param divisor Aeu
* @return Aeu&
* @note Undefined behaviour for division by zero
*/
gpu constexpr friend auto operator/=(Aeu& division, const Aeu& divisor) noexcept -> Aeu& { return division = division / divisor; }
/* --------------------------------------------------------------------------- */
/* ------------------------- @name Modulo operators. ------------------------- */
/**
* @brief Modulo operator
* @param modulation Aeu
* @param modulo Aeu
* @return Aeu
*/
[[nodiscard]]
gpu constexpr friend auto operator%(const Aeu& modulation, const Aeu& modulo) noexcept -> Aeu {
Aeu _, remainder; divide(modulation, modulo, _, remainder); return remainder;
}
/**
* @brief Assignment modulo operator
* @param modulation Aeu
* @param modulo Aeu
* @return Aeu&
*/
gpu constexpr friend auto operator%=(Aeu& modulation, const Aeu& modulo) noexcept -> Aeu& {
return modulation = modulation % modulo;
}
/* --------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------- */
/* ----------------------- @name Bitwise operators. ---------------------- */
/**
* @brief Bitwise complement operator
* @return Aeu
*/
[[nodiscard]]
gpu constexpr auto operator~() const noexcept -> Aeu {
Aeu result;
for(std::size_t i = 0; i < blocksNumber; ++i)
result.blocks[i] = ~blocks[i];
return result;
}
/**
* @brief Bitwise XOR operator
* @param left Aeu
* @param right Aeu
* @return Aeu
*/
[[nodiscard]]
gpu constexpr friend auto operator^(const Aeu& left, const Aeu& right) noexcept -> Aeu {
Aeu result = left; result ^= right; return result;
}
/**
* @brief Assignment bitwise XOR operator
* @param left Aeu
* @param right Aeu
* @return Aeu&
*/
gpu constexpr friend auto operator^=(Aeu& left, const Aeu& right) noexcept -> Aeu& {
for(std::size_t i = 0; i < blocksNumber; ++i)
left.blocks[i] ^= right.blocks[i];
return left;
}
/**
* @brief Bitwise AND operator
* @param left Aeu
* @param right Aeu
* @return Aeu
*/
[[nodiscard]]
gpu constexpr friend auto operator&(const Aeu& left, const Aeu& right) noexcept -> Aeu {
Aeu result = left; result &= right; return result;
}
/**
* @brief Assignment bitwise AND operator
* @param left Aeu
* @param right Aeu
* @return Aeu&
*/
gpu constexpr friend auto operator&=(Aeu& left, const Aeu& right) noexcept -> Aeu& {
for(std::size_t i = 0; i < blocksNumber; ++i)
left.blocks[i] &= right.blocks[i];
return left;
}
/**
* @brief Bitwise OR operator
* @param left Aeu
* @param right Aeu
* @return Aeu
*/
[[nodiscard]]
gpu constexpr friend auto operator|(const Aeu& left, const Aeu& right) noexcept -> Aeu {
Aeu result = left; result |= right; return result;
}
/**
* @brief Assignment bitwise OR operator
* @param left Aeu
* @param right Aeu
* @return Aeu&
*/
gpu constexpr friend auto operator|=(Aeu& left, const Aeu& right) noexcept -> Aeu& {
for(std::size_t i = 0; i < blocksNumber; ++i)
left.blocks[i] |= right.blocks[i];
return left;
}
/**
* @brief Left shift operator
* @param value Aeu
* @param bitShift Unsigned
* @return Aeu
* @note Does nothing for shift greater than precision
*/
template <typename Unsigned> requires (std::is_integral_v<Unsigned> && std::is_unsigned_v<Unsigned>) [[nodiscard]]
gpu constexpr friend auto operator<<(const Aeu& value, Unsigned bitShift) noexcept -> Aeu {
Aeu result = value; result <<= bitShift; return result;
}
/**
* @brief Left shift assignment operator
* @param value Aeu
* @param bitShift Unsigned
* @return Aeu&
* @note Does nothing for shift greater than precision
*/
template <typename Unsigned> requires (std::is_integral_v<Unsigned> && std::is_unsigned_v<Unsigned>)
gpu constexpr friend auto operator<<=(Aeu& value, Unsigned bitShift) noexcept -> Aeu& {
if(bitShift >= bitness || bitShift == 0) return value;
const std::size_t quotient = bitShift / blockBitLength, remainder = bitShift % blockBitLength;
const block stamp = (1UL << (blockBitLength - remainder)) - 1;
for (long long i = blocksNumber - 1; i >= (quotient + (remainder ? 1 : 0)); --i)
value.blocks[i] = (value.blocks[i - quotient] & stamp) << remainder
| ((value.blocks[i - quotient - (remainder ? 1 : 0)] & ~stamp) >> (blockBitLength - remainder) % blockBitLength);
value.blocks[quotient] = (value.blocks[0] & stamp) << remainder;
for (std::size_t i = 0; i < quotient; ++i)
value.blocks[i] = 0;
return value;
}
/**
* @brief Right shift operator
* @param value Aeu
* @param bitShift Unsigned
* @return Aeu
* @note Does nothing for shift greater than precision
*/
template <typename Unsigned> requires (std::is_integral_v<Unsigned> && std::is_unsigned_v<Unsigned>) [[nodiscard]]
gpu constexpr friend auto operator>>(const Aeu& value, Unsigned bitShift) noexcept -> Aeu {
Aeu result = value; result >>= bitShift; return result;
}
/**
* @brief Right shift assignment operator
* @param value Aeu
* @param bitShift Unsigned
* @return Aeu&
* @note Does nothing for shift greater than precision
*/
template <typename Unsigned> requires (std::is_integral_v<Unsigned> && std::is_unsigned_v<Unsigned>)
gpu constexpr friend auto operator>>=(Aeu& value, Unsigned bitShift) noexcept -> Aeu& {
if(bitShift >= bitness || bitShift == 0) return value;
const std::size_t quotient = bitShift / blockBitLength, remainder = bitShift % blockBitLength;
const block stamp = (1UL << remainder) - 1;
for(std::size_t i = 0; i < blocksNumber - (quotient + (remainder ? 1 : 0)); ++i)
value.blocks[i] = ((value.blocks[i + quotient + (remainder ? 1 : 0)] & stamp) << (blockBitLength - remainder) % blockBitLength) | (value.blocks[i + quotient] & ~stamp) >> remainder;
value.blocks[blocksNumber - 1 - quotient] = (value.blocks[blocksNumber - 1] & ~stamp) >> remainder;
for(long long i = blocksNumber - quotient; i < blocksNumber; ++i)
value.blocks[i] = 0;
return value;
}
/* ----------------------------------------------------------------------- */
/* --------------------- @name Comparison operators. --------------------- */
/* ------------------------ @name Equality operators. ------------------------ */
/**
* @brief Equality check operator for built-in types uint8_t, uint16_t, uint32_t
* @param our Aeu
* @param other Unsigned
* @return Boolean
*/
template <typename Unsigned> requires (std::is_unsigned_v<Unsigned> && sizeof(Unsigned) < 8)
gpu constexpr friend auto operator==(const Aeu& our, Unsigned other) noexcept -> bool {
return our.filledBlocksNumber() <= 1 && static_cast<block>(other) == our.blocks[0];
}
gpu constexpr friend auto operator==(const Aeu& our, uint64_t other) noexcept -> bool {
return our.filledBlocksNumber() <= 2 && (static_cast<uint64_t>(our.blocks[1]) << blockBitLength | static_cast<uint64_t>(our.blocks[0])) == other;
}
/**
* @brief Templated Equality check operator for numbers of different precision
* @param our Aeu
* @param other Aeu
* @return Boolean
*/
template <std::size_t otherBitness>
gpu constexpr friend auto operator==(const Aeu& our, const Aeu<otherBitness>& other) noexcept -> bool {
return our.compareTo(other) == Comparison::equal;
}
/* --------------------------------------------------------------------------- */
/* ----------------------- @name Comparison operators. ----------------------- */
/**
* @brief Internal comparison operator for built-in integral types uint8_t, uint16_t, uint32_t
* @param other Unsigned
* @return Comparison
* @note Should almost never return Comparison::Equivalent
*/
template <typename Unsigned> requires (std::is_unsigned_v<Unsigned> && sizeof(Unsigned) < 8) [[nodiscard]]
gpu constexpr auto compareTo(Unsigned other) const noexcept -> Comparison {
using enum Comparison;
if(filledBlocksNumber() > 1) return greater;
const auto cmp = static_cast<block>(other);
if(blocks[0] > cmp)
return greater;
if(blocks[0] < cmp)
return less;
return equal;
}
/**
* @brief Internal comparison operator for type uint64_t
* @param other uint64_t
* @return Comparison
* @note Should almost never return Comparison::Equivalent
*/
[[nodiscard]]
gpu constexpr auto compareTo(uint64_t other) const noexcept -> Comparison {
using enum Comparison;
if(filledBlocksNumber() > 2) return greater;
const auto base = (static_cast<uint64_t>(blocks[1]) << blockBitLength) | static_cast<uint64_t>(blocks[0]);
if(base > other)
return greater; else if(base < other) return less; else return equal;
}
/**
* @brief Internal comparison operator
* @param other Aeu
* @return Comparison
* @note Should almost never return Comparison::Equivalent
*/
template <std::size_t otherBitness = bitness> /*requires (otherBitness != bitness)*/ [[nodiscard]]
gpu constexpr auto compareTo(const Aeu<otherBitness>& other) const noexcept -> Comparison {
using enum Comparison;
const auto lowerBlockBorder = (blocksNumber < other.totalBlocksNumber() ? blocksNumber : other.totalBlocksNumber());
for(long long i = lowerBlockBorder - 1; i >= 0; --i) {
const block thisBlock = blocks[i], otherBlock = other.getBlock(i);
if(thisBlock != otherBlock)
return (thisBlock > otherBlock ? greater : less);
}
if constexpr (otherBitness != blocksNumber * blockBitLength) {
using enum Comparison;
if (other.totalBlocksNumber() > blocksNumber) {
for (long long i = other.totalBlocksNumber() - 1; i > lowerBlockBorder - 1; --i)
if (other.getBlock(i) != 0)
return less;
} else if (blocksNumber > other.totalBlocksNumber()) {
for (long long i = blocksNumber - 1; i > lowerBlockBorder - 1; --i)
if (blocks[i] != 0)
return greater;
}
}
return equal;
}
/* --------------------------------------------------------------------------- */
/* ------------------------ @name Spaceship operators. ----------------------- */
#if (defined(__CUDACC__) || __cplusplus < 202002L || defined (PRE_CPP_20)) && !defined DOXYGEN_SKIP
/**
* @brief Oldstyle comparison operator(s). Used inside CUDA because it does not support <=> on preCpp20
*/
gpu constexpr auto operator!=(const Aeu& value) const noexcept -> bool { return !this->operator==(value); }
gpu constexpr auto operator<(const Aeu& value) const noexcept -> bool { return this->compareTo(value) == Comparison::less; }
gpu constexpr auto operator<=(const Aeu& value) const noexcept -> bool { return !this->operator>(value); }
gpu constexpr auto operator>(const Aeu& value) const noexcept -> bool { return this->compareTo(value) == Comparison::greater; }
gpu constexpr auto operator>=(const Aeu& value) const noexcept -> bool { return !this->operator<(value); }
#else
/**
* @brief Three-way comparison operator
* @param other Aeu
* @return Std::Strong_ordering
* @note Available from C++20 standard and further. Should almost never return Strong_ordering::Equivalent
*/
gpu constexpr auto operator<=>(const Aeu& other) const noexcept -> std::strong_ordering {
switch(this->compareTo(other)) {
using enum Comparison;
case less:
return std::strong_ordering::less;
case greater:
return std::strong_ordering::greater;
case equal:
return std::strong_ordering::equal;
default:
return std::strong_ordering::equivalent;
}
};
/**
* @brief Three-way comparison operator for numbers of different precision and built-in integral types
* @param other Integral
* @return Std::Strong_ordering
* @note Available from C++20 standard and further. Should almost never return Strong_ordering::Equivalent
*/
template <typename Unsigned>
gpu constexpr auto operator<=>(const Unsigned& other) const noexcept -> std::strong_ordering {
switch(this->compareTo(other)) {
using enum Comparison;
case less:
return std::strong_ordering::less;
case greater:
return std::strong_ordering::greater;
case equal:
return std::strong_ordering::equal;
default:
return std::strong_ordering::equivalent;
}
};
#endif
/* --------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------- */
/* ---------------------- @name Supporting methods. ---------------------- */
/**
* @brief Set a bit in number by index starting from the right
* @param index Size_t
* @param bit Boolean
* @note Does nothing for index out of range
*/
gpu constexpr auto setBit(std::size_t index, bool bit) noexcept -> void {
#ifndef AESI_UNSAFE
if(index >= bitness) return;
#endif
const std::size_t blockNumber = index / blockBitLength, bitNumber = index % blockBitLength;
assert(blockNumber < blocksNumber && bitNumber < blockBitLength);
if(bit)
blocks[blockNumber] |= (1U << bitNumber);
else
blocks[blockNumber] &= (~(1U << bitNumber));
}
/**
* @brief Get bit in number by index staring from the right
* @param index Size_t
* @return Boolean
* @note Returns zero for index out of range
*/
[[nodiscard]]
gpu constexpr auto getBit(std::size_t index) const noexcept -> bool {
#ifndef AESI_UNSAFE
if(index >= bitness) return false;
#endif
const std::size_t blockNumber = index / blockBitLength, bitNumber = index % blockBitLength;
assert(blockNumber < blocksNumber && bitNumber < blockBitLength);
return blocks[blockNumber] & (1U << bitNumber);
}
/**
* @brief Set byte in number by index starting from the right
* @param index Size_t
* @param byte Byte
* @note Does nothing for index out of range
*/
gpu constexpr auto setByte(std::size_t index, byte byte) noexcept -> void {
#ifndef AESI_UNSAFE
if(index > blocksNumber * sizeof(block)) return;
#endif
const std::size_t blockNumber = index / sizeof(block), byteInBlock = index % sizeof(block), shift = byteInBlock * bitsInByte;
assert(blockNumber < blocksNumber && byteInBlock < sizeof(block));
blocks[blockNumber] &= ~(0xffU << shift); blocks[blockNumber] |= static_cast<block>(byte) << shift;
}
/**
* @brief Get byte in number by index starting from the right
* @param index Size_t
* @return Byte
* @note Returns zero for index out of range
*/
[[nodiscard]]
gpu constexpr auto getByte(std::size_t index) const noexcept -> byte {
#ifndef AESI_UNSAFE
if(index > blocksNumber * sizeof(block)) return 0;
#endif
const std::size_t blockNumber = index / sizeof(block), byteInBlock = index % sizeof(block), shift = byteInBlock * bitsInByte;
assert(blockNumber < blocksNumber && byteInBlock < sizeof(block));
return (blocks[blockNumber] & (0xffU << shift)) >> shift;
}
/**
* @brief Set block in number by index starting from the right
* @param index Size_t
* @param block Block
* @note Does nothing for index out of range. Index check is disabled in unsafe mode.
*/
gpu constexpr auto setBlock(std::size_t index, block block) noexcept -> void {
#ifndef AESI_UNSAFE
if(index >= blocksNumber) return;
#endif
blocks[index] = block;
}
/**
* @brief Get block in number by index starting from the right
* @param index Size_t
* @return Block
* @note Returns zero for index out of range. Index check is disabled in unsafe mode.
*/
[[nodiscard]]
gpu constexpr auto getBlock(std::size_t index) const noexcept -> block {
#ifndef AESI_UNSAFE
if(index >= blocksNumber) return block();
#endif
return blocks[index];
}
/**
* @brief Get amount of non-empty bytes in number right to left
* @return Size_t
*/
[[nodiscard]]
gpu constexpr auto byteCount() const noexcept -> std::size_t {
std::size_t lastBlock = blocksNumber - 1;
for(; lastBlock > 0 && blocks[lastBlock] == 0; --lastBlock)