std_abs.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // -*- C++ -*- C library enhancements header.
  2. // Copyright (C) 2016-2023 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 include/bits/std_abs.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{cmath, cstdlib}
  23. */
  24. #ifndef _GLIBCXX_BITS_STD_ABS_H
  25. #define _GLIBCXX_BITS_STD_ABS_H
  26. #pragma GCC system_header
  27. #include <bits/c++config.h>
  28. #define _GLIBCXX_INCLUDE_NEXT_C_HEADERS
  29. #include_next <stdlib.h>
  30. #ifdef __CORRECT_ISO_CPP_MATH_H_PROTO
  31. # include_next <math.h>
  32. #endif
  33. #undef _GLIBCXX_INCLUDE_NEXT_C_HEADERS
  34. #undef abs
  35. extern "C++"
  36. {
  37. namespace std _GLIBCXX_VISIBILITY(default)
  38. {
  39. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  40. using ::abs;
  41. #ifndef __CORRECT_ISO_CPP_STDLIB_H_PROTO
  42. inline long
  43. abs(long __i) { return __builtin_labs(__i); }
  44. #endif
  45. #ifdef _GLIBCXX_USE_LONG_LONG
  46. inline long long
  47. abs(long long __x) { return __builtin_llabs (__x); }
  48. #endif
  49. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  50. // 2192. Validity and return type of std::abs(0u) is unclear
  51. // 2294. <cstdlib> should declare abs(double)
  52. // 2735. std::abs(short), std::abs(signed char) and others should return int
  53. #ifndef __CORRECT_ISO_CPP_MATH_H_PROTO
  54. inline _GLIBCXX_CONSTEXPR double
  55. abs(double __x)
  56. { return __builtin_fabs(__x); }
  57. inline _GLIBCXX_CONSTEXPR float
  58. abs(float __x)
  59. { return __builtin_fabsf(__x); }
  60. inline _GLIBCXX_CONSTEXPR long double
  61. abs(long double __x)
  62. { return __builtin_fabsl(__x); }
  63. #endif
  64. #if defined(__GLIBCXX_TYPE_INT_N_0)
  65. __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_0
  66. abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
  67. #endif
  68. #if defined(__GLIBCXX_TYPE_INT_N_1)
  69. __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_1
  70. abs(__GLIBCXX_TYPE_INT_N_1 __x) { return __x >= 0 ? __x : -__x; }
  71. #endif
  72. #if defined(__GLIBCXX_TYPE_INT_N_2)
  73. __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_2
  74. abs(__GLIBCXX_TYPE_INT_N_2 __x) { return __x >= 0 ? __x : -__x; }
  75. #endif
  76. #if defined(__GLIBCXX_TYPE_INT_N_3)
  77. __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_3
  78. abs(__GLIBCXX_TYPE_INT_N_3 __x) { return __x >= 0 ? __x : -__x; }
  79. #endif
  80. #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
  81. constexpr _Float16
  82. abs(_Float16 __x)
  83. { return _Float16(__builtin_fabsf(__x)); }
  84. #endif
  85. #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
  86. constexpr _Float32
  87. abs(_Float32 __x)
  88. { return __builtin_fabsf(__x); }
  89. #endif
  90. #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
  91. constexpr _Float64
  92. abs(_Float64 __x)
  93. { return __builtin_fabs(__x); }
  94. #endif
  95. #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
  96. constexpr _Float128
  97. abs(_Float128 __x)
  98. { return __builtin_fabsl(__x); }
  99. #elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
  100. constexpr _Float128
  101. abs(_Float128 __x)
  102. { return __builtin_fabsf128(__x); }
  103. #endif
  104. #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
  105. constexpr __gnu_cxx::__bfloat16_t
  106. abs(__gnu_cxx::__bfloat16_t __x)
  107. { return __gnu_cxx::__bfloat16_t(__builtin_fabsf(__x)); }
  108. #endif
  109. #if !defined(__STRICT_ANSI__) && defined(_GLIBCXX_USE_FLOAT128)
  110. __extension__ inline _GLIBCXX_CONSTEXPR
  111. __float128
  112. abs(__float128 __x)
  113. {
  114. #if defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
  115. return __builtin_fabsl(__x);
  116. #elif defined(_GLIBCXX_HAVE_FLOAT128_MATH)
  117. return __builtin_fabsf128(__x);
  118. #else
  119. // Assume that __builtin_signbit works for __float128.
  120. return __builtin_signbit(__x) ? -__x : __x;
  121. #endif
  122. }
  123. #endif
  124. _GLIBCXX_END_NAMESPACE_VERSION
  125. } // namespace
  126. } // extern "C++"
  127. #endif // _GLIBCXX_BITS_STD_ABS_H