30#ifndef _GLIBCXX_MAX_SIZE_TYPE_H
31#define _GLIBCXX_MAX_SIZE_TYPE_H 1
33#pragma GCC system_header
35#if __cplusplus > 201703L && __cpp_lib_concepts
48namespace std _GLIBCXX_VISIBILITY(default)
50_GLIBCXX_BEGIN_NAMESPACE_VERSION
53 struct numeric_limits;
62 __max_size_type() =
default;
64 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
66 __max_size_type(_Tp __i) noexcept
67 : _M_val(__i), _M_msb(__i < 0)
71 __max_size_type(
const __max_diff_type& __d)
noexcept;
73 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
75 operator _Tp() const noexcept
79 operator bool() const noexcept
80 {
return _M_val != 0 || _M_msb != 0; }
82 constexpr __max_size_type
86 constexpr __max_size_type
87 operator~() const noexcept
88 {
return __max_size_type{~_M_val, !_M_msb}; }
90 constexpr __max_size_type
92 {
return operator~() + 1; }
94 constexpr __max_size_type&
96 {
return *
this += 1; }
98 constexpr __max_size_type
99 operator++(
int)
noexcept
106 constexpr __max_size_type&
107 operator--() noexcept
108 {
return *
this -= 1; }
110 constexpr __max_size_type
111 operator--(
int)
noexcept
118 constexpr __max_size_type&
119 operator+=(
const __max_size_type& __r)
noexcept
121 const auto __sum = _M_val + __r._M_val;
122 const bool __overflow = (__sum < _M_val);
123 _M_msb = _M_msb ^ __r._M_msb ^ __overflow;
128 constexpr __max_size_type&
129 operator-=(
const __max_size_type& __r)
noexcept
130 {
return *
this += -__r; }
132 constexpr __max_size_type&
133 operator*=(__max_size_type __r)
noexcept
135 constexpr __max_size_type __threshold
136 = __rep(1) << (_S_rep_bits / 2 - 1);
137 if (_M_val < __threshold && __r < __threshold)
140 _M_val = _M_val * __r._M_val;
146 const bool __lsb = _M_val & 1;
147 const bool __rlsb = __r._M_val & 1;
150 _M_val = (2 * _M_val * __r._M_val
151 + _M_val * __rlsb + __r._M_val * __lsb);
153 *
this += __rlsb * __lsb;
159 constexpr __max_size_type&
160 operator/=(
const __max_size_type& __r)
noexcept
162 __glibcxx_assert(__r != 0);
164 if (!_M_msb && !__r._M_msb) [[likely]]
165 _M_val /= __r._M_val;
166 else if (_M_msb && __r._M_msb)
168 _M_val = (_M_val >= __r._M_val);
171 else if (!_M_msb && __r._M_msb)
173 else if (_M_msb && !__r._M_msb)
179 const auto __orig = *
this;
181 _M_val /= __r._M_val;
183 if (__orig - *
this * __r >= __r)
189 constexpr __max_size_type&
190 operator%=(
const __max_size_type& __r)
noexcept
192 if (!_M_msb && !__r._M_msb) [[likely]]
193 _M_val %= __r._M_val;
195 *
this -= (*
this / __r) * __r;
199 constexpr __max_size_type&
200 operator<<=(
const __max_size_type& __r)
noexcept
202 __glibcxx_assert(__r <= _S_rep_bits);
205 _M_msb = (_M_val >> (_S_rep_bits - __r._M_val)) & 1;
207 if (__r._M_val == _S_rep_bits) [[unlikely]]
210 _M_val <<= __r._M_val;
215 constexpr __max_size_type&
216 operator>>=(
const __max_size_type& __r)
noexcept
218 __glibcxx_assert(__r <= _S_rep_bits);
221 if (__r._M_val == _S_rep_bits) [[unlikely]]
224 _M_val >>= __r._M_val;
226 if (_M_msb) [[unlikely]]
228 _M_val |= __rep(1) << (_S_rep_bits - __r._M_val);
235 constexpr __max_size_type&
236 operator&=(
const __max_size_type& __r)
noexcept
238 _M_val &= __r._M_val;
239 _M_msb &= __r._M_msb;
243 constexpr __max_size_type&
244 operator|=(
const __max_size_type& __r)
noexcept
246 _M_val |= __r._M_val;
247 _M_msb |= __r._M_msb;
251 constexpr __max_size_type&
252 operator^=(
const __max_size_type& __r)
noexcept
254 _M_val ^= __r._M_val;
255 _M_msb ^= __r._M_msb;
259 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
260 friend constexpr _Tp&
261 operator+=(_Tp& __a,
const __max_size_type& __b)
noexcept
262 {
return (__a =
static_cast<_Tp
>(__a + __b)); }
264 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
265 friend constexpr _Tp&
266 operator-=(_Tp& __a,
const __max_size_type& __b)
noexcept
267 {
return (__a =
static_cast<_Tp
>(__a - __b)); }
269 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
270 friend constexpr _Tp&
271 operator*=(_Tp& __a,
const __max_size_type& __b)
noexcept
272 {
return (__a =
static_cast<_Tp
>(__a * __b)); }
274 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
275 friend constexpr _Tp&
276 operator/=(_Tp& __a,
const __max_size_type& __b)
noexcept
277 {
return (__a =
static_cast<_Tp
>(__a / __b)); }
279 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
280 friend constexpr _Tp&
281 operator%=(_Tp& __a,
const __max_size_type& __b)
noexcept
282 {
return (__a =
static_cast<_Tp
>(__a % __b)); }
284 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
285 friend constexpr _Tp&
286 operator&=(_Tp& __a,
const __max_size_type& __b)
noexcept
287 {
return (__a =
static_cast<_Tp
>(__a & __b)); }
289 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
290 friend constexpr _Tp&
291 operator|=(_Tp& __a,
const __max_size_type& __b)
noexcept
292 {
return (__a =
static_cast<_Tp
>(__a | __b)); }
294 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
295 friend constexpr _Tp&
296 operator^=(_Tp& __a,
const __max_size_type& __b)
noexcept
297 {
return (__a =
static_cast<_Tp
>(__a ^ __b)); }
299 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
300 friend constexpr _Tp&
301 operator<<=(_Tp& __a,
const __max_size_type& __b)
noexcept
302 {
return (__a =
static_cast<_Tp
>(__a << __b)); }
304 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
305 friend constexpr _Tp&
306 operator>>=(_Tp& __a,
const __max_size_type& __b)
noexcept
307 {
return (__a =
static_cast<_Tp
>(__a >> __b)); }
309 friend constexpr __max_size_type
310 operator+(__max_size_type __l,
const __max_size_type& __r)
noexcept
316 friend constexpr __max_size_type
317 operator-(__max_size_type __l,
const __max_size_type& __r)
noexcept
323 friend constexpr __max_size_type
324 operator*(__max_size_type __l,
const __max_size_type& __r)
noexcept
330 friend constexpr __max_size_type
331 operator/(__max_size_type __l,
const __max_size_type& __r)
noexcept
337 friend constexpr __max_size_type
338 operator%(__max_size_type __l,
const __max_size_type& __r)
noexcept
344 friend constexpr __max_size_type
345 operator<<(__max_size_type __l,
const __max_size_type& __r)
noexcept
351 friend constexpr __max_size_type
352 operator>>(__max_size_type __l,
const __max_size_type& __r)
noexcept
358 friend constexpr __max_size_type
359 operator&(__max_size_type __l,
const __max_size_type& __r)
noexcept
365 friend constexpr __max_size_type
366 operator|(__max_size_type __l,
const __max_size_type& __r)
noexcept
372 friend constexpr __max_size_type
373 operator^(__max_size_type __l,
const __max_size_type& __r)
noexcept
379 friend constexpr bool
380 operator==(
const __max_size_type& __l,
const __max_size_type& __r)
noexcept
381 {
return __l._M_val == __r._M_val && __l._M_msb == __r._M_msb; }
383#if __cpp_lib_three_way_comparison
384 friend constexpr strong_ordering
385 operator<=>(
const __max_size_type& __l,
const __max_size_type& __r)
noexcept
387 if (__l._M_msb ^ __r._M_msb)
388 return __l._M_msb ? strong_ordering::greater : strong_ordering::less;
390 return __l._M_val <=> __r._M_val;
393 friend constexpr bool
394 operator!=(
const __max_size_type& __l,
const __max_size_type& __r)
noexcept
395 {
return !(__l == __r); }
397 friend constexpr bool
398 operator<(
const __max_size_type& __l,
const __max_size_type& __r)
noexcept
400 if (__l._M_msb == __r._M_msb)
401 return __l._M_val < __r._M_val;
406 friend constexpr bool
407 operator>(
const __max_size_type& __l,
const __max_size_type& __r)
noexcept
408 {
return __r < __l; }
410 friend constexpr bool
411 operator<=(
const __max_size_type& __l,
const __max_size_type& __r)
noexcept
412 {
return !(__l > __r); }
414 friend constexpr bool
415 operator>=(
const __max_size_type& __l,
const __max_size_type& __r)
noexcept
416 {
return __r <= __l; }
420 using __rep =
unsigned __int128;
422 using __rep =
unsigned long long;
424 static constexpr size_t _S_rep_bits =
sizeof(__rep) * __CHAR_BIT__;
427 unsigned _M_msb:1 = 0;
430 __max_size_type(__rep __val,
int __msb) noexcept
431 : _M_val(__val), _M_msb(__msb)
434 friend __max_diff_type;
439 class __max_diff_type
442 __max_diff_type() =
default;
444 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
446 __max_diff_type(_Tp __i) noexcept
451 __max_diff_type(
const __max_size_type& __d) noexcept
455 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
457 operator _Tp() const noexcept
458 {
return static_cast<_Tp
>(_M_rep); }
461 operator bool() const noexcept
462 {
return _M_rep != 0; }
464 constexpr __max_diff_type
468 constexpr __max_diff_type
470 {
return __max_diff_type(-_M_rep); }
472 constexpr __max_diff_type
473 operator~() const noexcept
474 {
return __max_diff_type(~_M_rep); }
476 constexpr __max_diff_type&
477 operator++() noexcept
478 {
return *
this += 1; }
480 constexpr __max_diff_type
481 operator++(
int)
noexcept
488 constexpr __max_diff_type&
489 operator--() noexcept
490 {
return *
this -= 1; }
492 constexpr __max_diff_type
493 operator--(
int)
noexcept
500 constexpr __max_diff_type&
501 operator+=(
const __max_diff_type& __r)
noexcept
503 _M_rep += __r._M_rep;
507 constexpr __max_diff_type&
508 operator-=(
const __max_diff_type& __r)
noexcept
510 _M_rep -= __r._M_rep;
514 constexpr __max_diff_type&
515 operator*=(
const __max_diff_type& __r)
noexcept
517 _M_rep *= __r._M_rep;
521 constexpr __max_diff_type&
522 operator/=(
const __max_diff_type& __r)
noexcept
524 __glibcxx_assert (__r != 0);
525 const bool __neg = *
this < 0;
526 const bool __rneg = __r < 0;
527 if (!__neg && !__rneg)
528 _M_rep = _M_rep / __r._M_rep;
529 else if (__neg && __rneg)
530 _M_rep = -_M_rep / -__r._M_rep;
531 else if (__neg && !__rneg)
532 _M_rep = -(-_M_rep / __r._M_rep);
534 _M_rep = -(_M_rep / -__r._M_rep);
538 constexpr __max_diff_type&
539 operator%=(
const __max_diff_type& __r)
noexcept
541 __glibcxx_assert (__r != 0);
542 if (*
this >= 0 && __r > 0)
543 _M_rep %= __r._M_rep;
545 *
this -= (*
this / __r) * __r;
549 constexpr __max_diff_type&
550 operator<<=(
const __max_diff_type& __r)
noexcept
552 _M_rep.operator<<=(__r._M_rep);
556 constexpr __max_diff_type&
557 operator>>=(
const __max_diff_type& __r)
noexcept
560 const auto __msb = _M_rep._M_msb;
561 _M_rep >>= __r._M_rep;
563 _M_rep |= ~(__max_size_type(-1) >> __r._M_rep);
567 constexpr __max_diff_type&
568 operator&=(
const __max_diff_type& __r)
noexcept
570 _M_rep &= __r._M_rep;
574 constexpr __max_diff_type&
575 operator|=(
const __max_diff_type& __r)
noexcept
577 _M_rep |= __r._M_rep;
581 constexpr __max_diff_type&
582 operator^=(
const __max_diff_type& __r)
noexcept
584 _M_rep ^= __r._M_rep;
588 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
589 friend constexpr _Tp&
590 operator+=(_Tp& __a,
const __max_diff_type& __b)
noexcept
591 {
return (__a =
static_cast<_Tp
>(__a + __b)); }
593 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
594 friend constexpr _Tp&
595 operator-=(_Tp& __a,
const __max_diff_type& __b)
noexcept
596 {
return (__a =
static_cast<_Tp
>(__a - __b)); }
598 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
599 friend constexpr _Tp&
600 operator*=(_Tp& __a,
const __max_diff_type& __b)
noexcept
601 {
return (__a =
static_cast<_Tp
>(__a * __b)); }
603 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
604 friend constexpr _Tp&
605 operator/=(_Tp& __a,
const __max_diff_type& __b)
noexcept
606 {
return (__a =
static_cast<_Tp
>(__a / __b)); }
608 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
609 friend constexpr _Tp&
610 operator%=(_Tp& __a,
const __max_diff_type& __b)
noexcept
611 {
return (__a =
static_cast<_Tp
>(__a % __b)); }
613 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
614 friend constexpr _Tp&
615 operator&=(_Tp& __a,
const __max_diff_type& __b)
noexcept
616 {
return (__a =
static_cast<_Tp
>(__a & __b)); }
618 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
619 friend constexpr _Tp&
620 operator|=(_Tp& __a,
const __max_diff_type& __b)
noexcept
621 {
return (__a =
static_cast<_Tp
>(__a | __b)); }
623 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
624 friend constexpr _Tp&
625 operator^=(_Tp& __a,
const __max_diff_type& __b)
noexcept
626 {
return (__a =
static_cast<_Tp
>(__a ^ __b)); }
628 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
629 friend constexpr _Tp&
630 operator<<=(_Tp& __a,
const __max_diff_type& __b)
noexcept
631 {
return (__a =
static_cast<_Tp
>(__a << __b)); }
633 template<
typename _Tp>
requires integral<_Tp> || __is_int128<_Tp>
634 friend constexpr _Tp&
635 operator>>=(_Tp& __a,
const __max_diff_type& __b)
noexcept
636 {
return (__a =
static_cast<_Tp
>(__a >> __b)); }
638 friend constexpr __max_diff_type
639 operator+(__max_diff_type __l,
const __max_diff_type& __r)
noexcept
645 friend constexpr __max_diff_type
646 operator-(__max_diff_type __l,
const __max_diff_type& __r)
noexcept
652 friend constexpr __max_diff_type
653 operator*(__max_diff_type __l,
const __max_diff_type& __r)
noexcept
659 friend constexpr __max_diff_type
660 operator/(__max_diff_type __l,
const __max_diff_type& __r)
noexcept
666 friend constexpr __max_diff_type
667 operator%(__max_diff_type __l,
const __max_diff_type& __r)
noexcept
673 friend constexpr __max_diff_type
674 operator<<(__max_diff_type __l,
const __max_diff_type& __r)
noexcept
680 friend constexpr __max_diff_type
681 operator>>(__max_diff_type __l,
const __max_diff_type& __r)
noexcept
687 friend constexpr __max_diff_type
688 operator&(__max_diff_type __l,
const __max_diff_type& __r)
noexcept
694 friend constexpr __max_diff_type
695 operator|(__max_diff_type __l,
const __max_diff_type& __r)
noexcept
701 friend constexpr __max_diff_type
702 operator^(__max_diff_type __l,
const __max_diff_type& __r)
noexcept
708 friend constexpr bool
709 operator==(
const __max_diff_type& __l,
const __max_diff_type& __r)
noexcept
710 {
return __l._M_rep == __r._M_rep; }
712#if __cpp_lib_three_way_comparison
713 constexpr strong_ordering
714 operator<=>(
const __max_diff_type& __r)
const noexcept
716 const auto __lsign = _M_rep._M_msb;
717 const auto __rsign = __r._M_rep._M_msb;
718 if (__lsign ^ __rsign)
719 return __lsign ? strong_ordering::less : strong_ordering::greater;
721 return _M_rep <=> __r._M_rep;
724 friend constexpr bool
725 operator!=(
const __max_diff_type& __l,
const __max_diff_type& __r)
noexcept
726 {
return !(__l == __r); }
729 operator<(
const __max_diff_type& __r)
const noexcept
731 const auto __lsign = _M_rep._M_msb;
732 const auto __rsign = __r._M_rep._M_msb;
733 if (__lsign ^ __rsign)
736 return _M_rep < __r._M_rep;
739 friend constexpr bool
740 operator>(
const __max_diff_type& __l,
const __max_diff_type& __r)
noexcept
741 {
return __r < __l; }
743 friend constexpr bool
744 operator<=(
const __max_diff_type& __l,
const __max_diff_type& __r)
noexcept
745 {
return !(__r < __l); }
747 friend constexpr bool
748 operator>=(
const __max_diff_type& __l,
const __max_diff_type& __r)
noexcept
749 {
return !(__l < __r); }
753 __max_size_type _M_rep = 0;
755 friend class __max_size_type;
759 __max_size_type::__max_size_type(
const __max_diff_type& __d) noexcept
760 : __max_size_type(__d._M_rep)
767 struct numeric_limits<ranges::__detail::__max_size_type>
769 using _Sp = ranges::__detail::__max_size_type;
773 static constexpr bool is_exact =
true;
775 static_assert(same_as<_Sp::__rep, unsigned __int128>);
776 static constexpr int digits = 129;
778 static_assert(same_as<_Sp::__rep, unsigned long long>);
779 static constexpr int digits
783 =
static_cast<int>(
digits * numbers::ln2 / numbers::ln10);
791 {
return _Sp(
static_cast<_Sp::__rep
>(-1), 1); }
799 struct numeric_limits<ranges::__detail::__max_diff_type>
801 using _Dp = ranges::__detail::__max_diff_type;
802 using _Sp = ranges::__detail::__max_size_type;
806 static constexpr bool is_exact =
true;
809 =
static_cast<int>(
digits * numbers::ln2 / numbers::ln10);
813 {
return _Dp(_Sp(0, 1)); }
817 {
return _Dp(_Sp(
static_cast<_Sp::__rep
>(-1), 0)); }
824_GLIBCXX_END_NAMESPACE_VERSION
constexpr duration< __common_rep_t< _Rep2, _Rep1 >, _Period > operator*(const _Rep1 &__s, const duration< _Rep2, _Period > &__d)
constexpr duration< __common_rep_t< _Rep1, __disable_if_is_duration< _Rep2 > >, _Period > operator%(const duration< _Rep1, _Period > &__d, const _Rep2 &__s)
constexpr time_point< _Clock, typename common_type< duration< _Rep1, _Period1 >, _Dur2 >::type > operator+(const duration< _Rep1, _Period1 > &__lhs, const time_point< _Clock, _Dur2 > &__rhs)
Adjust a time point forwards by the given duration.
constexpr common_type< duration< _Rep1, _Period1 >, duration< _Rep2, _Period2 > >::type operator-(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
The difference between two durations.
constexpr duration< __common_rep_t< _Rep1, __disable_if_is_duration< _Rep2 > >, _Period > operator/(const duration< _Rep1, _Period > &__d, const _Rep2 &__s)
ISO C++ entities toplevel namespace is std.
bitset< _Nb > operator&(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
bitset< _Nb > operator^(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
static constexpr bool is_integer
static constexpr int digits
static constexpr bool is_exact
static constexpr bool is_specialized
static constexpr bool is_signed
static constexpr int digits10
Properties of fundamental types.
static constexpr _Tp max() noexcept
static constexpr _Tp lowest() noexcept
static constexpr _Tp min() noexcept