-
Notifications
You must be signed in to change notification settings - Fork 15
/
unsigned_integer_methods_impl.inc
791 lines (703 loc) · 27 KB
/
unsigned_integer_methods_impl.inc
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
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// IWYU pragma: private
// IWYU pragma: friend "sus/.*"
///////////////////////////////////////////////////////////////////////////
//
// Declares (and defines) methods of unsigned integer types
//
// TO USE THIS INC FILE:
//
// Include it in the top level namespace.
//
// Define `_self` to the name of the integer type.
// Define `_primitive` to the primitive it holds inside.
// Define `_signed` to the signed integer type of the same size (except when
// _pointer is true).
// Define `_pointer` to true if the type is `uptr` or false otherwise.
///////////////////////////////////////////////////////////////////////////
namespace sus::num {
#if _pointer
template <class U>
_sus_pure constexpr ::sus::result::Result<_self, ::sus::num::TryFromIntError>
_self::try_from(U* u) noexcept {
using R = ::sus::result::Result<_self, ::sus::num::TryFromIntError>;
return R(_self(reinterpret_cast<_primitive>(u)));
}
template <Unsigned U>
requires(::sus::mem::size_of<U>() == ::sus::mem::size_of<_primitive>())
_sus_pure constexpr ::sus::result::Result<_self, ::sus::num::TryFromIntError>
_self::try_from(U u) noexcept {
using R = ::sus::result::Result<_self, ::sus::num::TryFromIntError>;
if constexpr (MAX_PRIMITIVE < U::MAX_PRIMITIVE) {
if (u.primitive_value > MAX_PRIMITIVE) {
return R::with_err(::sus::num::TryFromIntError::with_out_of_bounds());
}
}
return R(_self(static_cast<_primitive>(u.primitive_value)));
}
template <UnsignedPrimitiveInteger U>
requires(::sus::mem::size_of<U>() == ::sus::mem::size_of<_primitive>())
_sus_pure constexpr ::sus::result::Result<_self, ::sus::num::TryFromIntError>
_self::try_from(U u) noexcept {
using R = ::sus::result::Result<_self, ::sus::num::TryFromIntError>;
if constexpr (MAX_PRIMITIVE < __private::max_value<U>()) {
if (u > MAX_PRIMITIVE) {
return R::with_err(::sus::num::TryFromIntError::with_out_of_bounds());
}
}
return R(_self(static_cast<_primitive>(u)));
}
#else
template <Unsigned U>
_sus_pure constexpr ::sus::result::Result<_self, ::sus::num::TryFromIntError>
_self::try_from(U u) noexcept {
using R = ::sus::result::Result<_self, ::sus::num::TryFromIntError>;
if constexpr (MAX_PRIMITIVE < U::MAX_PRIMITIVE) {
if (u.primitive_value > MAX_PRIMITIVE) {
return R::with_err(::sus::num::TryFromIntError::with_out_of_bounds());
}
}
return R(_self(static_cast<_primitive>(u.primitive_value)));
}
template <UnsignedPrimitiveInteger U>
_sus_pure constexpr ::sus::result::Result<_self, ::sus::num::TryFromIntError>
_self::try_from(U u) noexcept {
using R = ::sus::result::Result<_self, ::sus::num::TryFromIntError>;
if constexpr (MAX_PRIMITIVE < __private::max_value<U>()) {
if (u > MAX_PRIMITIVE) {
return R::with_err(::sus::num::TryFromIntError::with_out_of_bounds());
}
}
return R(_self(static_cast<_primitive>(u)));
}
template <Signed S>
_sus_pure constexpr ::sus::result::Result<_self, ::sus::num::TryFromIntError>
_self::try_from(S s) noexcept {
using R = ::sus::result::Result<_self, ::sus::num::TryFromIntError>;
if (s.primitive_value < decltype(S::primitive_value){0}) {
return R::with_err(::sus::num::TryFromIntError::with_out_of_bounds());
}
constexpr auto umax = __private::into_unsigned(S::MAX_PRIMITIVE);
if constexpr (MAX_PRIMITIVE < umax) {
if (__private::into_unsigned(s.primitive_value) > MAX_PRIMITIVE) {
return R::with_err(::sus::num::TryFromIntError::with_out_of_bounds());
}
}
return R(_self(static_cast<_primitive>(s.primitive_value)));
}
template <SignedPrimitiveInteger S>
_sus_pure constexpr ::sus::result::Result<_self, ::sus::num::TryFromIntError>
_self::try_from(S s) noexcept {
using R = ::sus::result::Result<_self, ::sus::num::TryFromIntError>;
if (s < 0) {
return R::with_err(::sus::num::TryFromIntError::with_out_of_bounds());
}
constexpr auto umax = __private::into_unsigned(__private::max_value<S>());
if constexpr (MAX_PRIMITIVE < umax) {
if (__private::into_unsigned(s) > MAX_PRIMITIVE) {
return R::with_err(::sus::num::TryFromIntError::with_out_of_bounds());
}
}
return R(_self(static_cast<_primitive>(s)));
}
template <class S>
requires(SignedPrimitiveEnum<S> || SignedPrimitiveEnumClass<S>)
_sus_pure constexpr ::sus::result::Result<_self, ::sus::num::TryFromIntError>
_self::try_from(S s) noexcept {
using D = std::underlying_type_t<S>;
using R = ::sus::result::Result<_self, ::sus::num::TryFromIntError>;
if (static_cast<D>(s) < 0) {
return R::with_err(::sus::num::TryFromIntError::with_out_of_bounds());
}
constexpr auto umax = __private::into_unsigned(__private::max_value<D>());
if constexpr (MAX_PRIMITIVE < umax) {
if (__private::into_unsigned(static_cast<D>(s)) > MAX_PRIMITIVE) {
return R::with_err(::sus::num::TryFromIntError::with_out_of_bounds());
}
}
return R(_self(static_cast<_primitive>(s)));
}
template <class U>
requires(UnsignedPrimitiveEnum<U> || UnsignedPrimitiveEnumClass<U>)
_sus_pure constexpr ::sus::result::Result<_self, ::sus::num::TryFromIntError>
_self::try_from(U u) noexcept {
using D = std::underlying_type_t<U>;
using R = ::sus::result::Result<_self, ::sus::num::TryFromIntError>;
if constexpr (MAX_PRIMITIVE < __private::max_value<D>()) {
if (static_cast<D>(u) > MAX_PRIMITIVE) {
return R::with_err(::sus::num::TryFromIntError::with_out_of_bounds());
}
}
return R(_self(static_cast<_primitive>(u)));
}
#endif
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_add(
_self rhs) const& noexcept {
const auto out =
__private::add_with_overflow(primitive_value, rhs.primitive_value);
if (!out.overflow) [[likely]]
return ::sus::option::Option<_self>(_self(out.value));
else
return ::sus::option::Option<_self>();
}
#if _pointer
template <class U>
requires((UnsignedNumeric<U> || UnsignedPrimitiveInteger<U>) &&
::sus::mem::size_of<U>() <= ::sus::mem::size_of<_primitive>())
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_add(
U rhs) const& noexcept {
return checked_add(_self(_primitive{rhs}));
}
#endif
#if !_pointer // No `_signed` for uptr.
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_add_signed(
_signed rhs) const& noexcept {
const auto out =
__private::add_with_overflow_signed(primitive_value, rhs.primitive_value);
if (!out.overflow) [[likely]]
return ::sus::option::Option<_self>(out.value);
else
return ::sus::option::Option<_self>();
}
#endif
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool> _self::overflowing_add(
_self rhs) const& noexcept {
const auto out =
__private::add_with_overflow(primitive_value, rhs.primitive_value);
return ::sus::tuple_type::Tuple<_self, bool>(_self(out.value), out.overflow);
}
#if _pointer
template <class U>
requires((UnsignedNumeric<U> || UnsignedPrimitiveInteger<U>) &&
::sus::mem::size_of<U>() <= ::sus::mem::size_of<_primitive>())
_sus_pure constexpr sus::tuple_type::Tuple<_self, bool> _self::overflowing_add(
U rhs) const& noexcept {
return overflowing_add(_self(_primitive{rhs}));
}
#endif
#if !_pointer // No `_signed` for uptr.
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool>
_self::overflowing_add_signed(_signed rhs) const& noexcept {
const auto r =
__private::add_with_overflow_signed(primitive_value, rhs.primitive_value);
return ::sus::tuple_type::Tuple<_self, bool>(r.value, r.overflow);
}
_sus_pure constexpr _self _self::saturating_add_signed(
_signed rhs) const& noexcept {
const auto r =
__private::add_with_overflow_signed(primitive_value, rhs.primitive_value);
if (!r.overflow) [[likely]]
return r.value;
else {
// TODO: Can this be done without a branch? If it's complex or uses compiler
// stuff, move into intrinsics.
if (rhs.primitive_value >= 0)
return MAX;
else
return MIN;
}
}
_sus_pure constexpr _self _self::wrapping_add_signed(
_signed rhs) const& noexcept {
return __private::add_with_overflow_signed(primitive_value,
rhs.primitive_value)
.value;
}
#endif
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_div(
_self rhs) const& noexcept {
if (rhs.primitive_value != 0u) [[likely]]
return ::sus::option::Option<_self>(
_self(__private::unchecked_div(primitive_value, rhs.primitive_value)));
else
return ::sus::option::Option<_self>();
}
#if _pointer
template <class U>
requires((UnsignedNumeric<U> || UnsignedPrimitiveInteger<U>) &&
::sus::mem::size_of<U>() <= ::sus::mem::size_of<_primitive>())
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_div(
U rhs) const& noexcept {
return checked_div(_self(_primitive{rhs}));
}
#endif
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool> _self::overflowing_div(
_self rhs) const& noexcept {
sus_check_with_message(rhs.primitive_value != _primitive{0},
"attempt to divide by zero");
return ::sus::tuple_type::Tuple<_self, bool>(
_self(__private::unchecked_div(primitive_value, rhs.primitive_value)),
false);
}
#if _pointer
template <class U>
requires((UnsignedNumeric<U> || UnsignedPrimitiveInteger<U>) &&
::sus::mem::size_of<U>() <= ::sus::mem::size_of<_primitive>())
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool> _self::overflowing_div(
U rhs) const& noexcept {
return overflowing_div(_self(_primitive{rhs}));
}
#endif
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_mul(
_self rhs) const& noexcept {
const auto out =
__private::mul_with_overflow(primitive_value, rhs.primitive_value);
if (!out.overflow) [[likely]]
return ::sus::option::Option<_self>(_self(out.value));
else
return ::sus::option::Option<_self>();
}
#if _pointer
template <class U>
requires((UnsignedNumeric<U> || UnsignedPrimitiveInteger<U>) &&
::sus::mem::size_of<U>() <= ::sus::mem::size_of<_primitive>())
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_mul(
U rhs) const& noexcept {
return checked_mul(_self(_primitive{rhs}));
}
#endif
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool> _self::overflowing_mul(
_self rhs) const& noexcept {
const auto out =
__private::mul_with_overflow(primitive_value, rhs.primitive_value);
return ::sus::tuple_type::Tuple<_self, bool>(_self(out.value), out.overflow);
}
#if _pointer
template <class U>
requires((UnsignedNumeric<U> || UnsignedPrimitiveInteger<U>) &&
::sus::mem::size_of<U>() <= ::sus::mem::size_of<_primitive>())
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool> _self::overflowing_mul(
U rhs) const& noexcept {
return overflowing_mul(_self(_primitive{rhs}));
}
#endif
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_neg()
const& noexcept {
if (primitive_value == 0u)
return ::sus::option::Option<_self>(_self(_primitive{0u}));
else
return ::sus::option::Option<_self>();
}
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool>
_self::overflowing_neg() const& noexcept {
return ::sus::tuple_type::Tuple<_self, bool>(
(~(*this)).wrapping_add(_self(_primitive{1u})), primitive_value != 0u);
}
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_rem(
_self rhs) const& noexcept {
if (rhs.primitive_value != 0u) [[likely]]
return ::sus::option::Option<_self>(
_self(__private::unchecked_rem(primitive_value, rhs.primitive_value)));
else
return ::sus::option::Option<_self>();
}
#if _pointer
template <class U>
requires((UnsignedNumeric<U> || UnsignedPrimitiveInteger<U>) &&
::sus::mem::size_of<U>() <= ::sus::mem::size_of<_primitive>())
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_rem(
U rhs) const& noexcept {
return checked_rem(_self(_primitive{rhs}));
}
#endif
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool> _self::overflowing_rem(
_self rhs) const& noexcept {
sus_check_with_message(
rhs.primitive_value != _primitive{0},
"attempt to calculate the remainder with a divisor of zero");
return ::sus::tuple_type::Tuple<_self, bool>(
_self(__private::unchecked_rem(primitive_value, rhs.primitive_value)),
false);
}
#if _pointer
template <class U>
requires((UnsignedNumeric<U> || UnsignedPrimitiveInteger<U>) &&
::sus::mem::size_of<U>() <= ::sus::mem::size_of<_primitive>())
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool> _self::overflowing_rem(
U rhs) const& noexcept {
return overflowing_rem(_self(_primitive{rhs}));
}
#endif
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_div_euclid(
_self rhs) const& noexcept {
if (rhs.primitive_value == 0u) [[unlikely]] {
return ::sus::option::Option<_self>();
} else {
return ::sus::option::Option<_self>(
_self(__private::unchecked_div(primitive_value, rhs.primitive_value)));
}
}
#if _pointer
template <class U>
requires((UnsignedNumeric<U> || UnsignedPrimitiveInteger<U>) &&
::sus::mem::size_of<U>() <= ::sus::mem::size_of<_primitive>())
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_div_euclid(
U rhs) const& noexcept {
return checked_div_euclid(_self(_primitive{rhs}));
}
#endif
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool>
_self::overflowing_div_euclid(_self rhs) const& noexcept {
sus_check_with_message(rhs.primitive_value != _primitive{0},
"attempt to divide by zero");
return ::sus::tuple_type::Tuple<_self, bool>(
_self(__private::unchecked_div(primitive_value, rhs.primitive_value)),
false);
}
#if _pointer
template <class U>
requires((UnsignedNumeric<U> || UnsignedPrimitiveInteger<U>) &&
::sus::mem::size_of<U>() <= ::sus::mem::size_of<_primitive>())
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool>
_self::overflowing_div_euclid(U rhs) const& noexcept {
return overflowing_div_euclid(_self(_primitive{rhs}));
}
#endif
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_rem_euclid(
_self rhs) const& noexcept {
if (rhs.primitive_value == 0u) [[unlikely]] {
return ::sus::option::Option<_self>();
} else {
return ::sus::option::Option<_self>(
_self(__private::unchecked_rem(primitive_value, rhs.primitive_value)));
}
}
#if _pointer
template <class U>
requires((UnsignedNumeric<U> || UnsignedPrimitiveInteger<U>) &&
::sus::mem::size_of<U>() <= ::sus::mem::size_of<_primitive>())
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_rem_euclid(
U rhs) const& noexcept {
return checked_rem_euclid(_self(_primitive{rhs}));
}
#endif
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool>
_self::overflowing_rem_euclid(_self rhs) const& noexcept {
sus_check_with_message(
rhs.primitive_value != _primitive{0},
"attempt to calculate the remainder with a divisor of zero");
return ::sus::tuple_type::Tuple<_self, bool>(
_self(__private::unchecked_rem(primitive_value, rhs.primitive_value)),
false);
}
#if _pointer
template <class U>
requires((UnsignedNumeric<U> || UnsignedPrimitiveInteger<U>) &&
::sus::mem::size_of<U>() <= ::sus::mem::size_of<_primitive>())
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool>
_self::overflowing_rem_euclid(U rhs) const& noexcept {
return overflowing_rem_euclid(_self(_primitive{rhs}));
}
#endif
constexpr inline void _self::operator<<=(u64 r) & noexcept {
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
sus_check_with_message(r < u64(__private::num_bits<_primitive>()),
"attempt to shift left with overflow");
primitive_value <<= r.primitive_value;
} else {
primitive_value = wrapping_shl(r).primitive_value;
}
}
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_shl(
u64 rhs) const& noexcept {
const auto out =
__private::shl_with_overflow(primitive_value, rhs.primitive_value);
if (!out.overflow) [[likely]]
return ::sus::option::Option<_self>(_self(out.value));
else
return ::sus::option::Option<_self>();
}
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool> _self::overflowing_shl(
u64 rhs) const& noexcept {
const auto out =
__private::shl_with_overflow(primitive_value, rhs.primitive_value);
return ::sus::tuple_type::Tuple<_self, bool>(_self(out.value), out.overflow);
}
_sus_pure constexpr _self _self::wrapping_shl(u64 rhs) const& noexcept {
return _self(
__private::shl_with_overflow(primitive_value, rhs.primitive_value).value);
}
constexpr inline void _self::operator>>=(u64 r) & noexcept {
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
sus_check_with_message(r < u64(__private::num_bits<_primitive>()),
"attempt to shift right with overflow");
primitive_value >>= r.primitive_value;
} else {
primitive_value = wrapping_shr(r).primitive_value;
}
}
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_shr(
u64 rhs) const& noexcept {
const auto out =
__private::shr_with_overflow(primitive_value, rhs.primitive_value);
if (!out.overflow) [[likely]]
return ::sus::option::Option<_self>(_self(out.value));
else
return ::sus::option::Option<_self>();
}
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool> _self::overflowing_shr(
u64 rhs) const& noexcept {
const auto out =
__private::shr_with_overflow(primitive_value, rhs.primitive_value);
return ::sus::tuple_type::Tuple<_self, bool>(_self(out.value), out.overflow);
}
_sus_pure constexpr _self _self::wrapping_shr(u64 rhs) const& noexcept {
return _self(
__private::shr_with_overflow(primitive_value, rhs.primitive_value).value);
}
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_sub(
_self rhs) const& {
const auto out =
__private::sub_with_overflow(primitive_value, rhs.primitive_value);
if (!out.overflow) [[likely]]
return ::sus::option::Option<_self>(_self(out.value));
else
return ::sus::option::Option<_self>();
}
#if _pointer
template <class U>
requires((UnsignedNumeric<U> || UnsignedPrimitiveInteger<U>) &&
::sus::mem::size_of<U>() <= ::sus::mem::size_of<_primitive>())
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_sub(
U rhs) const& noexcept {
return checked_sub(_self(_primitive{rhs}));
}
#endif
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool> _self::overflowing_sub(
_self rhs) const& noexcept {
const auto out =
__private::sub_with_overflow(primitive_value, rhs.primitive_value);
return ::sus::tuple_type::Tuple<_self, bool>(_self(out.value), out.overflow);
}
#if _pointer
template <class U>
requires((UnsignedNumeric<U> || UnsignedPrimitiveInteger<U>) &&
::sus::mem::size_of<U>() <= ::sus::mem::size_of<_primitive>())
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool> _self::overflowing_sub(
U rhs) const& noexcept {
return overflowing_sub(_self(_primitive{rhs}));
}
#endif
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_pow(
u32 rhs) const& noexcept {
const auto out =
__private::pow_with_overflow(primitive_value, rhs.primitive_value);
if (!out.overflow) [[likely]]
return ::sus::option::Option<_self>(_self(out.value));
else
return ::sus::option::Option<_self>();
}
_sus_pure constexpr ::sus::tuple_type::Tuple<_self, bool> _self::overflowing_pow(
u32 exp) const& noexcept {
const auto out =
__private::pow_with_overflow(primitive_value, exp.primitive_value);
return ::sus::tuple_type::Tuple<_self, bool>(_self(out.value), out.overflow);
}
_sus_pure constexpr _self _self::rotate_left(u64 n) const& noexcept {
return _self(__private::rotate_left(primitive_value, n.primitive_value));
}
_sus_pure constexpr _self _self::rotate_right(u64 n) const& noexcept {
return _self(__private::rotate_right(primitive_value, n.primitive_value));
}
_sus_pure constexpr ::sus::option::Option<u32> _self::checked_log2()
const& noexcept {
if (primitive_value == 0u) [[unlikely]] {
return ::sus::option::Option<u32>();
} else {
uint32_t zeros = __private::leading_zeros_nonzero(::sus::marker::unsafe_fn,
primitive_value);
return ::sus::option::Option<u32>(BITS - u32(1u) - u32(zeros));
}
}
_sus_pure constexpr u32 _self::log2() const& noexcept {
return checked_log2().expect(
"argument of integer logarithm must be positive");
}
_sus_pure constexpr ::sus::option::Option<u32> _self::checked_log10()
const& noexcept {
if (primitive_value == 0u) [[unlikely]] {
return ::sus::option::Option<u32>();
} else {
return ::sus::option::Option<u32>(
__private::int_log10::_self(primitive_value));
}
}
_sus_pure constexpr u32 _self::log10() const& noexcept {
return checked_log10().expect(
"argument of integer logarithm must be positive");
}
_sus_pure constexpr ::sus::option::Option<u32> _self::checked_log(
_self base) const& noexcept {
if (primitive_value == 0u || base.primitive_value <= 1u) [[unlikely]] {
return ::sus::option::Option<u32>();
} else {
auto n = uint32_t{0u};
auto r = primitive_value;
const auto b = base.primitive_value;
while (r >= b) {
r /= b;
n += 1u;
}
return ::sus::option::Option<u32>(n);
}
}
#if _pointer
template <class U>
requires((UnsignedNumeric<U> || UnsignedPrimitiveInteger<U>) &&
::sus::mem::size_of<U>() <= ::sus::mem::size_of<_primitive>())
_sus_pure constexpr ::sus::option::Option<u32> _self::checked_log(
U rhs) const& noexcept {
return checked_log(_self(_primitive{rhs}));
}
#endif
_sus_pure constexpr u32 _self::log(_self base) const& noexcept {
return checked_log(base).expect(
"argument of integer logarithm must be positive");
}
#if _pointer
template <class U>
requires((UnsignedNumeric<U> || UnsignedPrimitiveInteger<U>) &&
::sus::mem::size_of<U>() <= ::sus::mem::size_of<_primitive>())
_sus_pure constexpr u32 _self::log(U rhs) const& noexcept {
return log(_self(_primitive{rhs}));
}
#endif
_sus_pure constexpr ::sus::option::Option<_self>
_self::checked_next_power_of_two() const& noexcept {
const auto one_less =
__private::one_less_than_next_power_of_two(primitive_value);
return _self(one_less).checked_add(_self(_primitive{1u}));
}
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_next_multiple_of(
_self rhs) const& noexcept {
return checked_rem(rhs).and_then([s = *this, rhs](_self r) {
return r == _primitive{0}
? Option<_self>(s)
// SAFETY: rhs - r cannot overflow because `r` is smaller than
// `rhs`, as it's the remainder of `*this / rhs`.
: s.checked_add(__private::unchecked_sub(rhs.primitive_value,
r.primitive_value));
});
}
#if _pointer
template <class U>
requires((UnsignedNumeric<U> || UnsignedPrimitiveInteger<U>) &&
::sus::mem::size_of<U>() <= ::sus::mem::size_of<_primitive>())
_sus_pure constexpr ::sus::option::Option<_self> _self::checked_next_multiple_of(
U rhs) const& noexcept {
return checked_next_multiple_of(_self(_primitive{rhs}));
}
#endif
_sus_pure constexpr ::sus::collections::Array<u8,
::sus::mem::size_of<_primitive>()>
_self::to_be_bytes() const& noexcept {
return to_be().to_ne_bytes();
}
_sus_pure constexpr ::sus::collections::Array<u8,
::sus::mem::size_of<_primitive>()>
_self::to_le_bytes() const& noexcept {
return to_le().to_ne_bytes();
}
_sus_pure constexpr ::sus::collections::Array<u8,
::sus::mem::size_of<_primitive>()>
_self::to_ne_bytes() const& noexcept {
auto bytes =
::sus::collections::Array<u8, ::sus::mem::size_of<_primitive>()>();
if (std::is_constant_evaluated()) {
auto uval = primitive_value;
for (usize i; i < ::sus::mem::size_of<_primitive>(); i += 1u) {
const auto last_byte = static_cast<uint8_t>(uval & 0xff);
if constexpr (std::endian::native == std::endian::little)
bytes[i] = last_byte;
else
bytes[::sus::mem::size_of<_primitive>() - 1u - i] = last_byte;
// If _self is one byte, this shift would be UB. But it's also not needed
// since the loop will not run again.
if constexpr (::sus::mem::size_of<_primitive>() > 1u) uval >>= 8u;
}
return bytes;
} else {
::sus::ptr::copy_nonoverlapping(
::sus::marker::unsafe_fn,
reinterpret_cast<const char*>(&primitive_value),
reinterpret_cast<char*>(bytes.as_mut_ptr()),
::sus::mem::size_of<_primitive>());
return bytes;
}
}
_sus_pure constexpr _self _self::from_be_bytes(
const ::sus::collections::Array<u8, ::sus::mem::size_of<_primitive>()>&
bytes) noexcept {
return from_be(from_ne_bytes(bytes));
}
_sus_pure constexpr _self _self::from_le_bytes(
const ::sus::collections::Array<u8, ::sus::mem::size_of<_primitive>()>&
bytes) noexcept {
return from_le(from_ne_bytes(bytes));
}
_sus_pure constexpr _self _self::from_ne_bytes(
const ::sus::collections::Array<u8, ::sus::mem::size_of<_primitive>()>&
bytes) noexcept {
_self val;
if (std::is_constant_evaluated()) {
val = _primitive{0};
// size_of<_primitive>() < u32::MAX` so casting to u32 is not lossy.
for (u32 i; i < ::sus::cast<u32>(::sus::mem::size_of<_primitive>());
i += 1u) {
val |= _primitive{bytes[i]} << (i * 8u);
}
} else {
::sus::ptr::copy_nonoverlapping(
::sus::marker::unsafe_fn, reinterpret_cast<const char*>(bytes.as_ptr()),
reinterpret_cast<char*>(&val.primitive_value),
::sus::mem::size_of<_primitive>());
}
return _self(val);
}
} // namespace sus::num
// std hash support.
template <>
struct std::hash<::sus::num::_self> {
_sus_pure auto operator()(::sus::num::_self u) const noexcept {
return std::hash<_primitive>()(u.primitive_value);
}
};
template <>
struct std::equal_to<::sus::num::_self> {
_sus_pure constexpr auto operator()(::sus::num::_self l,
::sus::num::_self r) const noexcept {
return l == r;
}
};
// fmt support.
template <class Char>
struct fmt::formatter<::sus::num::_self, Char> {
template <class ParseContext>
constexpr auto parse(ParseContext& ctx) {
return underlying_.parse(ctx);
}
template <class FormatContext>
constexpr auto format(::sus::num::_self t, FormatContext& ctx) const {
return underlying_.format(t.primitive_value, ctx);
}
private:
formatter<_primitive, Char> underlying_;
};
// Stream support.
_sus_format_to_stream(sus::num, _self);
#undef _self
#undef _primitive
#undef _signed
#undef _pointer