int_limits.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Minimal replacement for numeric_limits of integers. -*- C++ -*-
  2. // Copyright (C) 2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/int_limits.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{limits}
  23. */
  24. #ifndef _GLIBCXX_INT_LIMITS_H
  25. #define _GLIBCXX_INT_LIMITS_H 1
  26. #pragma GCC system_header
  27. #if __cplusplus >= 201103L
  28. #include <bits/c++config.h>
  29. namespace std _GLIBCXX_VISIBILITY(default)
  30. {
  31. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  32. namespace __detail
  33. {
  34. // This template is used for arbitrary signed and unsigned integer types
  35. // (by headers <bit> and <charconv>) and for specific integer types
  36. // (by <memory_resource> and <string_view>) but also for char (<charconv>).
  37. // For simplicity's sake, all integral types except bool are supported.
  38. // Lightweight alternative to numeric_limits<signed integer type>.
  39. template<typename _Tp, bool = is_signed<_Tp>::value>
  40. struct __int_limits
  41. {
  42. static_assert(is_integral<_Tp>::value, "unsupported specialization");
  43. using _Up = typename make_unsigned<_Tp>::type;
  44. static constexpr int digits = sizeof(_Tp) * __CHAR_BIT__ - 1;
  45. static constexpr _Tp min() noexcept { return _Tp(_Up(1) << digits); }
  46. static constexpr _Tp max() noexcept { return _Tp(_Up(~_Up(0)) >> 1); }
  47. };
  48. // Lightweight alternative to numeric_limits<unsigned integer type>.
  49. template<typename _Tp>
  50. struct __int_limits<_Tp, false>
  51. {
  52. static_assert(is_integral<_Tp>::value, "unsupported specialization");
  53. static constexpr int digits = sizeof(_Tp) * __CHAR_BIT__;
  54. static constexpr _Tp min() noexcept { return 0; }
  55. static constexpr _Tp max() noexcept { return _Tp(-1); }
  56. };
  57. template<> struct __int_limits<bool>; // not defined
  58. }
  59. _GLIBCXX_END_NAMESPACE_VERSION
  60. } // namespace
  61. #endif // C++11
  62. #endif // _GLIBCXX_INT_LIMITS_H