-
Notifications
You must be signed in to change notification settings - Fork 1
/
Aesi.h
executable file
·1296 lines (1175 loc) · 46.7 KB
/
Aesi.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 Aesi.h
* @brief Long precision signed integer with arithmetic operations
*/
#ifndef AESI_MULTIPRECISION
#define AESI_MULTIPRECISION
/// @cond HIDE_INCLUDES
#include "Aeu.h"
/// @endcond
namespace {
enum class Sign { Zero = 0, Positive = 1, Negative = -1 };
template <typename Char> requires (std::is_same_v<Char, char> || std::is_same_v<Char, wchar_t>)
Sign traverseDashes(const Char* ptr, std::size_t);
template <>
Sign traverseDashes(const char* ptr, std::size_t size) {
std::byte positive { 1 };
for(std::size_t i = 0; i < size; ++i)
if(ptr[i] == '-') positive ^= std::byte {1};
return positive == std::byte {1} ? Sign::Positive : Sign::Negative;
}
template <>
Sign traverseDashes(const wchar_t* ptr, std::size_t size) {
std::byte positive { 1 };
for(std::size_t i = 0; i < size; ++i)
if(ptr[i] == L'-') positive ^= std::byte {1};
return positive == std::byte {1} ? Sign::Positive : Sign::Negative;
}
}
/**
* @class Aesi
* @brief Long precision signed integer
* @details May be used to represent positive and negative integers. Number precision is set in template parameter bitness.
*/
template <std::size_t bitness = 512> requires (bitness % blockBitLength == 0)
class Aesi final {
/* -------------------------- @name Class members. ----------------------- */
Sign sign;
using Base = Aeu<bitness>;
Base base;
/* ----------------------------------------------------------------------- */
gpu constexpr Aesi(Sign withSign, Base withBase): sign { withSign }, base { withBase } {}
public:
/* --------------------- @name Different constructors. ------------------- */
/**
* @brief Default constructor
*/
gpu constexpr Aesi() noexcept = default;
/**
* @brief Destructor
*/
gpu constexpr ~Aesi() noexcept = default;
/**
* @brief Copy constructor
* @param copy Aesi&
*/
gpu constexpr Aesi(const Aesi& copy) noexcept = default;
/**
* @brief Integral constructor
* @param value Integral
*/
template <typename Integral> requires (std::is_integral_v<Integral>)
gpu constexpr Aesi(Integral value) noexcept {
using enum Sign;
if(value < 0) {
value *= -1;
base = Base(static_cast<unsigned long long>(value));
sign = Negative;
} else if(value > 0) {
base = Base(static_cast<unsigned long long>(value));
sign = Positive;
} else
sign = Zero;
}
/**
* @brief Pointer-based character constructor
* @param ptr Char*
* @param size Size_t
* @details Accepts decimal strings (no prefix), binary (0b/0B), octal (0o/0O) and hexadecimal (0x/0X)
* @note An odd number of dashes makes the number negative
*/
template <typename Char> requires (std::is_same_v<Char, char> || std::is_same_v<Char, wchar_t>)
gpu constexpr Aesi(const Char* ptr, std::size_t size) noexcept : base(ptr, size) {
if(!base.isZero())
sign = traverseDashes(ptr, size);
else sign = Sign::Zero;
}
/**
* @brief C-style string literal constructor
* @param literal Char[]
*/
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 Aesi(const Char (&literal)[arrayLength]) noexcept : Aesi(literal, arrayLength) {}
/**
* @brief String / String-view constructor
* @param stringView String
*/
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 Aesi(String&& stringView) noexcept : Aesi(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 Aesi(const String& stringView) noexcept : Aesi(stringView.data(), stringView.size()) {}
/**
* @brief Unsigned integer conversion
* @param value Aeu&
*/
explicit gpu constexpr Aesi(const Aeu<bitness>& value) : sign(Sign::Positive), base(value) {}
#ifdef AESI_CRYPTOPP_INTEGRATION
/**
* @brief Crypto++ library integer constructor
* @param number CryptoPP::Integer&
*/
constexpr Aesi(const CryptoPP::Integer& number) {
using enum Sign;
if(number.IsZero())
sign = Zero;
else {
base = number;
if(number.IsNegative())
sign = Negative;
else sign = Positive;
}
}
#endif
#ifdef AESI_GMP_INTEGRATION
/**
* @brief GMP library integer constructor
* @param number mpz_class&
*/
constexpr Aesi(const mpz_class& number) {
using enum Sign;
if(number == 0)
sign = Zero;
else {
base = number;
if(number < 0)
sign = Negative;
else sign = Positive;
}
}
#endif
/**
* @brief Integral assignment operator
* @param value Integral
*/
template <typename Integral> requires (std::is_signed_v<Integral>)
gpu constexpr Aesi& operator=(Integral value) noexcept {
using enum Sign;
if(value != 0) {
if(value < 0) {
sign = Negative;
value *= -1;
} else sign = Positive;
base = static_cast<unsigned long long>(value);
} else sign = Zero;
return *this;
}
/**
* @brief Copy assignment operator
* @param other Aesi&
*/
gpu constexpr Aesi& operator=(const Aesi& other) noexcept = default;
/* ----------------------------------------------------------------------- */
/* --------------------- @name Arithmetic operators. --------------------- */
/* ------------------------- @name Unary operators. -------------------------- */
/**
* @brief Unary plus operator
* @details Does basically nothing
* @return Aesi
*/
gpu constexpr auto operator+() const noexcept -> Aesi { return *this; }
/**
* @brief Unary minus operator
* @return Aesi
*/
[[nodiscard]]
gpu constexpr auto operator-() const noexcept -> Aesi { Aesi copy = *this; copy.inverse(); return copy; }
/**
* @brief Prefix increment operator
* @return Aesi&
*/
gpu constexpr auto operator++() noexcept -> Aesi& {
using enum Sign;
if(sign == Negative) {
--base; if(base.isZero()) sign = Zero;
} else if(sign == Positive) {
++base;
} else { base = 1u; sign = Positive; }
return *this;
}
/**
* @brief Postfix increment operator
* @return Aesi
*/
gpu constexpr auto operator++(int) & noexcept -> Aesi { Aesi old = *this; operator++(); return old; }
/**
* @brief Prefix decrement operator
* @return Aesi&
*/
gpu constexpr auto operator--() noexcept -> Aesi& {
using enum Sign;
if(sign == Negative) {
++base;
} else if(sign == Positive) {
--base; if(base.isZero()) sign = Zero;
} else { base = 1u; sign = Negative; }
return *this;
}
/**
* @brief Postfix decrement operator
* @return Aesi
*/
gpu constexpr auto operator--(int) & noexcept -> Aesi { Aesi old = *this; operator--(); return old; }
/* --------------------------------------------------------------------------- */
/* ------------------------ @name Addition operators. ------------------------ */
/**
* @brief Addition operator
* @param addition Aesi
* @param addendum Aesi&
* @return Aesi
*/
[[nodiscard]]
gpu constexpr friend auto operator+(const Aesi& addition, const Aesi& addendum) noexcept -> Aesi {
Aesi result = addition; result += addendum; return result;
}
/**
* @brief Addition assignment operator
* @param addition Aesi
* @param addendum Aesi&
* @return Aesi&
*/
gpu constexpr friend auto operator+=(Aesi& addition, const Aesi& addendum) noexcept -> Aesi& {
using enum Sign;
Sign& lSign = addition.sign; const Sign& rSign = addendum.sign;
Base& lBase = addition.base; const Base& rBase = addendum.base;
if(rSign == Zero)
return addition;
else if(lSign == Zero)
return addition = addendum;
else if(lSign == rSign) {
lBase += rBase;
return addition;
}
if(lSign == Positive) {
switch(lBase.compareTo(rBase)) {
using enum Comparison;
case greater: {
lBase -= rBase;
return addition;
}
case less: {
lBase = rBase - lBase;
lSign = Negative;
return addition;
}
default: {
lSign = Zero;
return addition;
}
}
} else {
switch(const auto ratio = lBase.compareTo(rBase)) {
using enum Comparison;
case greater: {
lBase -= rBase;
return addition;
}
case less: {
lBase = rBase - lBase;
lSign = Positive;
return addition;
}
default: {
lSign = Zero;
return addition;
}
}
}
}
/* --------------------------------------------------------------------------- */
/* ----------------------- @name Subtraction operators. ---------------------- */
/**
* @brief Subtraction operator
* @param subtraction Aesi
* @param subtrahend Aesi&
* @return Aesi
*/
[[nodiscard]]
gpu constexpr friend auto operator-(const Aesi& subtraction, const Aesi& subtrahend) noexcept -> Aesi {
Aesi result = subtraction; result -= subtrahend; return result;
}
/**
* @brief Subtraction assignment operator
* @param subtraction Aesi
* @param subtrahend Aesi&
* @return Aesi&
*/
gpu constexpr friend auto operator-=(Aesi& subtraction, const Aesi& subtrahend) noexcept -> Aesi& {
using enum Sign;
Sign& lSign = subtraction.sign; const Sign& rSign = subtrahend.sign;
Base& lBase = subtraction.base; const Base& rBase = subtrahend.base;
if(rSign == Zero)
return subtraction;
if(lSign == Zero) {
subtraction = subtrahend;
subtraction.inverse();
return subtraction;
}
if(lSign == Positive) {
if(rSign == Positive) {
switch(lBase.compareTo(rBase)) {
using enum Comparison;
case greater: {
lBase -= rBase;
return subtraction;
}
case less: {
lBase = rBase - lBase;
lSign = Negative;
return subtraction;
}
default: {
lSign = Zero;
return subtraction;
}
}
} else {
lBase += rBase;
return subtraction;
}
} else {
if(rSign == Negative) {
switch(lBase.compareTo(rBase)) {
using enum Comparison;
case greater: {
lBase -= rBase;
return subtraction;
}
case less: {
lBase = rBase - lBase;
lSign = Positive;
return subtraction;
}
default: {
lSign = Zero;
return subtraction;
}
}
} else {
lBase += rBase;
return subtraction;
}
}
}
/* --------------------------------------------------------------------------- */
/* --------------------- @name Multiplication operators. --------------------- */
/**
* @brief Multiplication operator for built-in types
* @param multiplication Aesi
* @param factor Integral
* @return Aesi
*/
template <typename Integral> requires (std::is_integral_v<Integral>) [[nodiscard]]
gpu constexpr friend auto operator*(const Aesi& multiplication, Integral factor) noexcept -> Aesi {
Aesi result = multiplication; result *= factor; return result;
}
/**
* @brief Multiplication operator
* @param multiplication Aesi
* @param factor Aesi
* @return Aesi
*/
[[nodiscard]]
gpu constexpr friend auto operator*(const Aesi& multiplication, const Aesi& factor) noexcept -> Aesi {
Aesi result = multiplication; result *= factor; return result;
}
/**
* @brief Multiplication assignment operator for built-in types
* @param multiplication Aesi
* @param factor Integral
* @return Aesi&
*/
template <typename Integral> requires (std::is_integral_v<Integral>)
gpu constexpr friend auto operator*=(Aesi& multiplication, Integral factor) noexcept -> Aesi& {
using enum Sign;
if(factor == 0) {
multiplication.sign = Zero;
} else {
if(factor < 0) {
multiplication.inverse();
factor *= -1;
}
multiplication.base *= static_cast<unsigned long long>(factor);
}
return multiplication;
}
/**
* @brief Multiplication assignment operator
* @param multiplication Aesi
* @param factor Aesi
* @return Aesi&
*/
gpu constexpr friend auto operator*=(Aesi& multiplication, const Aesi& factor) noexcept -> Aesi& {
using enum Sign;
if(factor.isZero()) {
multiplication.sign = Zero;
} else {
if(factor.isNegative())
multiplication.inverse();
multiplication.base *= factor.base;
}
return multiplication;
}
/* --------------------------------------------------------------------------- */
/* ------------------------ @name Division operators. ------------------------ */
/**
* @brief Division operator for built-in integral types
* @param division Aesi
* @param divisor Integral
* @return Aesi
* @note Undefined behaviour for division by zero
*/
template <typename Integral> requires (std::is_integral_v<Integral>) [[nodiscard]]
gpu constexpr friend auto operator/(const Aesi& division, Integral divisor) noexcept -> Aesi {
Aesi result = division; result /= divisor; return result;
}
/**
* @brief Division operator
* @param division Aesi
* @param divisor Aesi
* @return Aesi
* @note Undefined behaviour for division by zero
*/
[[nodiscard]]
gpu constexpr friend auto operator/(const Aesi& division, const Aesi& divisor) noexcept -> Aesi {
Aesi result = division; result /= divisor; return result;
}
/**
* @brief Assignment division operator for built-in integral types
* @param division Aesi
* @param divisor Integral
* @return Aesi&
* @note Undefined behaviour for division by zero
*/
template <typename Integral> requires (std::is_integral_v<Integral>)
gpu constexpr friend auto operator/=(Aesi& division, Integral divisor) noexcept -> Aesi& {
using enum Sign;
if(divisor == 0) {
division.sign = Zero;
} else {
if(divisor < 0) {
division.inverse();
divisor *= -1;
}
division.base /= static_cast<unsigned long long>(divisor);
if(division.base.isZero()) division.sign = Zero;
}
return division;
}
/**
* @brief Assignment division operator
* @param division Aesi
* @param divisor Aesi
* @return Aesi&
* @note Undefined behaviour for division by zero
*/
gpu constexpr friend auto operator/=(Aesi& division, const Aesi& divisor) noexcept -> Aesi& {
using enum Sign;
if(divisor.isZero()) {
division.sign = Zero;
} else {
if(divisor.isNegative())
division.inverse();
division.base /= divisor.base;
if(division.base.isZero()) division.sign = Zero;
}
return division;
}
/* --------------------------------------------------------------------------- */
/* ------------------------- @name Modulo operators. ------------------------- */
/**
* @brief Modulo operator for built-in types
* @param modulation Aesi
* @param modulo Integral
* @return Aesi
* @note Returns zero for the modulo of zero
*/
template <typename Integral> requires (std::is_integral_v<Integral>) [[nodiscard]]
gpu constexpr friend auto operator%(const Aesi& modulation, Integral modulo) noexcept -> Aesi {
Aesi result = modulation; result %= modulo; return result;
}
/**
* @brief Modulo operator
* @param modulation Aesi
* @param modulo Aesi
* @return Aesi
* @details DETAILS
* @note Returns zero for the modulo of zero
*/
[[nodiscard]]
gpu constexpr friend auto operator%(const Aesi& modulation, const Aesi& modulo) noexcept -> Aesi {
Aesi result = modulation; result %= modulo; return result;
}
/**
* @brief Modulo assignment operator for built-in types
* @param modulation Aesi
* @param modulo Integral
* @return Aesi&
* @note Returns zero for the modulo of zero
*/
template <typename Integral> requires (std::is_integral_v<Integral>)
gpu constexpr friend auto operator%=(Aesi& modulation, Integral modulo) noexcept -> Aesi& {
using enum Sign;
if(modulo == 0) {
modulation.sign = Zero;
} else {
if(modulo < 0) {
modulation.inverse();
modulo *= -1;
}
modulation.base %= static_cast<unsigned long long>(modulo);
}
return modulation;
}
/**
* @brief Modulo assignment operator
* @param modulation Aesi
* @param modulo Aesi
* @return Aesi&
* @details DETAILS
* @note Returns zero for the modulo of zero
*/
gpu constexpr friend auto operator%=(Aesi& modulation, const Aesi& modulo) noexcept -> Aesi& {
if(modulo.isZero())
return modulation;
if(modulo.isNegative())
modulation.inverse();
modulation.base %= modulo.base;
if(modulation.base.isZero())
modulation.sign = Sign::Zero;
return modulation;
}
/* --------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------- */
/* --------------------- @name Comparison operators. --------------------- */
/* ------------------------ @name Equality operators. ------------------------ */
/**
* @brief Equality operator for built-in types
* @param our Aesi
* @param integral Integral
* @return bool
*/
template <typename Integral> requires (std::is_integral_v<Integral>)
gpu constexpr friend auto operator==(const Aesi& our, Integral integral) noexcept -> bool {
return our.compareTo(integral) == Comparison::equal;
}
/**
* @brief Different precision equlity operator
* @param our Aesi
* @param other Aesi
* @return bool
*/
template <std::size_t otherBitness>
gpu constexpr friend auto operator==(const Aesi& our, const Aesi<otherBitness>& other) noexcept -> bool {
if constexpr (bitness == otherBitness) {
return our.compareTo(other) == Comparison::equal;
} else {
return our.precisionCast<otherBitness>() == other;
}
}
/* --------------------------------------------------------------------------- */
/* ----------------------- @name Comparison operators. ----------------------- */
/**
* @brief Comparison operator for built-in types
* @param integral Integral
* @return Comparison
*/
template <typename Integral> requires (std::is_integral_v<Integral>)
gpu constexpr auto compareTo(Integral integral) const noexcept -> Comparison {
using enum Sign; using enum Comparison;
if(integral == 0) {
switch(sign) {
case Positive:
return greater;
case Negative:
return less;
default:
return equal;
}
} else if(integral < 0) {
if(sign == Negative)
switch(base.compareTo(static_cast<unsigned long long>(integral * -1))) {
using enum Comparison;
case greater:
return less;
case less:
return greater;
default:
return equal;
}
else return greater;
} else {
if(sign == Positive)
return base.compareTo(static_cast<unsigned long long>(integral));
return less;
}
}
/**
* @brief Different precision comparison operator
* @param value Aesi&
* @return Comparison
*/
template <std::size_t otherBitness = bitness> [[nodiscard]]
gpu constexpr auto compareTo(const Aesi<otherBitness>& value) const noexcept -> Comparison {
return precisionCast<otherBitness>().compareTo(value);
}
/**
* @brief Comparison operator
* @param value Aesi&
* @return Comparison
*/
[[nodiscard]]
gpu constexpr auto compareTo(const Aesi& value) const noexcept -> Comparison {
using enum Sign; using enum Comparison;
if(value.isZero()) {
switch(sign) {
case Positive:
return greater;
case Negative:
return less;
default:
return equal;
}
}
if(value.isNegative()) {
if(sign == Negative)
switch(base.compareTo(value.base)) {
case greater:
return less;
case less:
return greater;
default:
return equal;
}
return greater;
}
if(sign == Positive)
return base.compareTo(value.base);
return less;
}
/* --------------------------------------------------------------------------- */
/* ------------------------ @name Spaceship operators. ----------------------- */
#if (defined(__CUDACC__) || __cplusplus < 202002L || defined (PRE_CPP_20)) && !defined DOXYGEN_SKIP
/**
* @brief Oldstyle comparison operator(s). Used inside CUDA cause it does not support <=> operator.
*/
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 Aesi
* @return Std::Strong_ordering
* @note Available from C++20 standard and further. Should almost never return Strong_ordering::Equivalent
*/
gpu constexpr auto operator<=>(const Aesi& 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 Unsigned
* @return Std::Strong_ordering
* @note Available from C++20 standard and further. Should almost never return Strong_ordering::Equivalent
*/
template <typename Object>
gpu constexpr auto operator<=>(const Object& 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 bit in number by index starting from the right
* @param index Size_t
* @param bit Boolean
* @note Does nothing for index out of range. Does not affect sign
*/
gpu constexpr auto setBit(std::size_t index, bool bit) noexcept -> void { return base.setBit(index, bit); }
/**
* @brief Get bit in number by index starting from the right
* @param index Size_t
* @return Boolean
* @note Returns zero for index out of range. Does not affect the sign
*/
[[nodiscard]]
gpu constexpr auto getBit(std::size_t index) const noexcept -> bool { return base.getBit(index); }
/**
* @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. Does not affect the sign
*/
gpu constexpr auto setByte(std::size_t index, byte byte) noexcept -> void { return base.setByte(index, byte); }
/**
* @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. Does not affect the sign
*/
[[nodiscard]]
gpu constexpr auto getByte(std::size_t index) const noexcept -> byte { return base.getByte(index); }
/**
* @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. Does not affect the sign
*/
gpu constexpr auto setBlock(std::size_t index, block block) noexcept -> void { return base.setBlock(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. Does not affect the sign
*/
[[nodiscard]]
gpu constexpr auto getBlock(std::size_t index) const noexcept -> block { return base.getBlock(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 { return base.byteCount(); }
/**
* @brief Get amount of non-empty bits in number right to left
* @return Size_t
*/
[[nodiscard]]
gpu constexpr auto bitCount() const noexcept -> std::size_t { return base.bitCount(); }
/**
* @brief Check whether number is odd
* @return Boolean: true if the number is odd and false otherwise
*/
[[nodiscard]]
gpu constexpr auto isOdd() const noexcept -> bool { return base.isOdd(); }
/**
* @brief Check whether number is even
* @return Boolean: true if the number is even and false otherwise
*/
[[nodiscard]]
gpu constexpr auto isEven() const noexcept -> bool { return base.isEven(); }
/**
* @brief Check whether number is zero
* @return Boolean: true if the number is zero and false otherwise
*/
[[nodiscard]]
gpu constexpr auto isZero() const noexcept -> bool { return sign == Sign::Zero; }
/**
* @brief Check whether number is positive
* @return Boolean: true if the number is positive and false otherwise
*/
[[nodiscard]]
gpu constexpr auto isPositive() const noexcept -> bool { return sign == Sign::Positive; }
/**
* @brief Check whether number is negative
* @return Boolean: true if the number is negative and false otherwise
*/
[[nodiscard]]
gpu constexpr auto isNegative() const noexcept -> bool { return sign == Sign::Negative; }
/**
* @brief Get number of non-empty blocks inside object starting from the right
* @return Size_t
*/
[[nodiscard]]
gpu constexpr auto filledBlocksNumber() const noexcept -> std::size_t { return base.filledBlocksNumber(); }
/**
* @brief Get number's precision
* @return Size_t
*/
[[nodiscard]]
gpu static constexpr auto getBitness() noexcept -> std::size_t { return bitness; }
/**
* @brief Get the number of blocks (length of array of uint32_t integers) inside object
* @return Size_t
*/
[[nodiscard]]
gpu static constexpr auto totalBlocksNumber() noexcept -> std::size_t { return Aeu<bitness>::totalBlocksNumber(); }
/**
* @brief Make swap between two objects
* @param other Aesi
*/
gpu constexpr auto swap(Aesi& other) noexcept -> void {
Sign tSign = sign; sign = other.sign; other.sign = tSign;
base.swap(other.base);
}
/**
* @brief Invertes number's bitness
* @details Turns negative to positive and otherwise. Leaves zero unchanges
*/
gpu constexpr auto inverse() noexcept -> void {
using enum Sign;
sign = (sign == Zero ? Zero : (sign == Negative ? Positive : Negative));
}
/* ----------------------------------------------------------------------- */
/* -------------- @name Public arithmetic and number theory. ------------- */
/**
* @brief Integral division
* @param number Aesi&
* @param divisor Aesi&
* @param quotient Aesi& OUT
* @param remainder Aesi& OUT
* @details Returns values by references
*/
gpu static constexpr auto divide(const Aesi& number, const Aesi& divisor, Aesi& quotient, Aesi& remainder) noexcept -> void {
using enum Sign;
if(number.sign == Zero || divisor.sign == Zero) {
quotient.sign = Zero;
remainder.sign = Zero;
return;
}
Base::divide(number.base, divisor.base, quotient.base, remainder.base);
if(number.sign == Positive) {
if(divisor.sign == Positive) {
quotient.sign = Positive;
remainder.sign = Positive;
} else {
quotient.sign = Negative;
remainder.sign = Positive;
}
} else {
if(divisor.sign == Positive) {
quotient.sign = Negative;
remainder.sign = Negative;
} else {
quotient.sign = Positive;
remainder.sign = Negative;
}
}
if(quotient.base.isZero())
quotient.sign = Zero;
if(remainder.base.isZero())
remainder.sign = Zero;
}
/**
* @brief Square root
* @return Aesi
* @details Returns zero for negative
*/
[[nodiscard]]
gpu constexpr auto squareRoot() const noexcept -> Aesi {
using enum Sign;
if(sign == Positive)
return Aesi { Positive, base.squareRoot() };
Aesi result; result.sign = Zero; return result;
}
/**
* @brief Fast exponentiation of 2
* @return Aesi