rounding_functions.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___MATH_ROUNDING_FUNCTIONS_H
  9. #define _LIBCPP___MATH_ROUNDING_FUNCTIONS_H
  10. #include <__config>
  11. #include <__type_traits/enable_if.h>
  12. #include <__type_traits/is_arithmetic.h>
  13. #include <__type_traits/is_integral.h>
  14. #include <__type_traits/is_same.h>
  15. #include <__type_traits/promote.h>
  16. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  17. # pragma GCC system_header
  18. #endif
  19. _LIBCPP_BEGIN_NAMESPACE_STD
  20. namespace __math {
  21. // ceil
  22. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI float ceil(float __x) _NOEXCEPT { return __builtin_ceilf(__x); }
  23. template <class = int>
  24. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI double ceil(double __x) _NOEXCEPT {
  25. return __builtin_ceil(__x);
  26. }
  27. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI long double ceil(long double __x) _NOEXCEPT {
  28. return __builtin_ceill(__x);
  29. }
  30. template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
  31. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI double ceil(_A1 __x) _NOEXCEPT {
  32. return __builtin_ceil((double)__x);
  33. }
  34. // floor
  35. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI float floor(float __x) _NOEXCEPT { return __builtin_floorf(__x); }
  36. template <class = int>
  37. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI double floor(double __x) _NOEXCEPT {
  38. return __builtin_floor(__x);
  39. }
  40. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI long double floor(long double __x) _NOEXCEPT {
  41. return __builtin_floorl(__x);
  42. }
  43. template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
  44. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI double floor(_A1 __x) _NOEXCEPT {
  45. return __builtin_floor((double)__x);
  46. }
  47. // llrint
  48. inline _LIBCPP_HIDE_FROM_ABI long long llrint(float __x) _NOEXCEPT { return __builtin_llrintf(__x); }
  49. template <class = int>
  50. _LIBCPP_HIDE_FROM_ABI long long llrint(double __x) _NOEXCEPT {
  51. return __builtin_llrint(__x);
  52. }
  53. inline _LIBCPP_HIDE_FROM_ABI long long llrint(long double __x) _NOEXCEPT { return __builtin_llrintl(__x); }
  54. template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
  55. inline _LIBCPP_HIDE_FROM_ABI long long llrint(_A1 __x) _NOEXCEPT {
  56. return __builtin_llrint((double)__x);
  57. }
  58. // llround
  59. inline _LIBCPP_HIDE_FROM_ABI long long llround(float __x) _NOEXCEPT { return __builtin_llroundf(__x); }
  60. template <class = int>
  61. _LIBCPP_HIDE_FROM_ABI long long llround(double __x) _NOEXCEPT {
  62. return __builtin_llround(__x);
  63. }
  64. inline _LIBCPP_HIDE_FROM_ABI long long llround(long double __x) _NOEXCEPT { return __builtin_llroundl(__x); }
  65. template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
  66. inline _LIBCPP_HIDE_FROM_ABI long long llround(_A1 __x) _NOEXCEPT {
  67. return __builtin_llround((double)__x);
  68. }
  69. // lrint
  70. inline _LIBCPP_HIDE_FROM_ABI long lrint(float __x) _NOEXCEPT { return __builtin_lrintf(__x); }
  71. template <class = int>
  72. _LIBCPP_HIDE_FROM_ABI long lrint(double __x) _NOEXCEPT {
  73. return __builtin_lrint(__x);
  74. }
  75. inline _LIBCPP_HIDE_FROM_ABI long lrint(long double __x) _NOEXCEPT { return __builtin_lrintl(__x); }
  76. template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
  77. inline _LIBCPP_HIDE_FROM_ABI long lrint(_A1 __x) _NOEXCEPT {
  78. return __builtin_lrint((double)__x);
  79. }
  80. // lround
  81. inline _LIBCPP_HIDE_FROM_ABI long lround(float __x) _NOEXCEPT { return __builtin_lroundf(__x); }
  82. template <class = int>
  83. _LIBCPP_HIDE_FROM_ABI long lround(double __x) _NOEXCEPT {
  84. return __builtin_lround(__x);
  85. }
  86. inline _LIBCPP_HIDE_FROM_ABI long lround(long double __x) _NOEXCEPT { return __builtin_lroundl(__x); }
  87. template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
  88. inline _LIBCPP_HIDE_FROM_ABI long lround(_A1 __x) _NOEXCEPT {
  89. return __builtin_lround((double)__x);
  90. }
  91. // nearbyint
  92. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI float nearbyint(float __x) _NOEXCEPT {
  93. return __builtin_nearbyintf(__x);
  94. }
  95. template <class = int>
  96. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI double nearbyint(double __x) _NOEXCEPT {
  97. return __builtin_nearbyint(__x);
  98. }
  99. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI long double nearbyint(long double __x) _NOEXCEPT {
  100. return __builtin_nearbyintl(__x);
  101. }
  102. template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
  103. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI double nearbyint(_A1 __x) _NOEXCEPT {
  104. return __builtin_nearbyint((double)__x);
  105. }
  106. // nextafter
  107. inline _LIBCPP_HIDE_FROM_ABI float nextafter(float __x, float __y) _NOEXCEPT { return __builtin_nextafterf(__x, __y); }
  108. template <class = int>
  109. _LIBCPP_HIDE_FROM_ABI double nextafter(double __x, double __y) _NOEXCEPT {
  110. return __builtin_nextafter(__x, __y);
  111. }
  112. inline _LIBCPP_HIDE_FROM_ABI long double nextafter(long double __x, long double __y) _NOEXCEPT {
  113. return __builtin_nextafterl(__x, __y);
  114. }
  115. template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
  116. inline _LIBCPP_HIDE_FROM_ABI typename __promote<_A1, _A2>::type nextafter(_A1 __x, _A2 __y) _NOEXCEPT {
  117. using __result_type = typename __promote<_A1, _A2>::type;
  118. static_assert((!(_IsSame<_A1, __result_type>::value && _IsSame<_A2, __result_type>::value)), "");
  119. return __math::nextafter((__result_type)__x, (__result_type)__y);
  120. }
  121. // nexttoward
  122. inline _LIBCPP_HIDE_FROM_ABI float nexttoward(float __x, long double __y) _NOEXCEPT {
  123. return __builtin_nexttowardf(__x, __y);
  124. }
  125. template <class = int>
  126. _LIBCPP_HIDE_FROM_ABI double nexttoward(double __x, long double __y) _NOEXCEPT {
  127. return __builtin_nexttoward(__x, __y);
  128. }
  129. inline _LIBCPP_HIDE_FROM_ABI long double nexttoward(long double __x, long double __y) _NOEXCEPT {
  130. return __builtin_nexttowardl(__x, __y);
  131. }
  132. template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
  133. inline _LIBCPP_HIDE_FROM_ABI double nexttoward(_A1 __x, long double __y) _NOEXCEPT {
  134. return __builtin_nexttoward((double)__x, __y);
  135. }
  136. // rint
  137. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI float rint(float __x) _NOEXCEPT { return __builtin_rintf(__x); }
  138. template <class = int>
  139. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI double rint(double __x) _NOEXCEPT {
  140. return __builtin_rint(__x);
  141. }
  142. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI long double rint(long double __x) _NOEXCEPT {
  143. return __builtin_rintl(__x);
  144. }
  145. template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
  146. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI double rint(_A1 __x) _NOEXCEPT {
  147. return __builtin_rint((double)__x);
  148. }
  149. // round
  150. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI float round(float __x) _NOEXCEPT { return __builtin_roundf(__x); }
  151. template <class = int>
  152. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI double round(double __x) _NOEXCEPT {
  153. return __builtin_round(__x);
  154. }
  155. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI long double round(long double __x) _NOEXCEPT {
  156. return __builtin_roundl(__x);
  157. }
  158. template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
  159. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI double round(_A1 __x) _NOEXCEPT {
  160. return __builtin_round((double)__x);
  161. }
  162. // trunc
  163. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI float trunc(float __x) _NOEXCEPT { return __builtin_truncf(__x); }
  164. template <class = int>
  165. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI double trunc(double __x) _NOEXCEPT {
  166. return __builtin_trunc(__x);
  167. }
  168. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI long double trunc(long double __x) _NOEXCEPT {
  169. return __builtin_truncl(__x);
  170. }
  171. template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
  172. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI double trunc(_A1 __x) _NOEXCEPT {
  173. return __builtin_trunc((double)__x);
  174. }
  175. } // namespace __math
  176. _LIBCPP_END_NAMESPACE_STD
  177. #endif // _LIBCPP___MATH_ROUNDING_FUNCTIONS_H