xref: /freebsd/contrib/llvm-project/libcxx/include/__math/rounding_functions.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
15f757f3fSDimitry Andric //===----------------------------------------------------------------------===//
25f757f3fSDimitry Andric //
35f757f3fSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f757f3fSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f757f3fSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65f757f3fSDimitry Andric //
75f757f3fSDimitry Andric //===----------------------------------------------------------------------===//
85f757f3fSDimitry Andric 
95f757f3fSDimitry Andric #ifndef _LIBCPP___MATH_ROUNDING_FUNCTIONS_H
105f757f3fSDimitry Andric #define _LIBCPP___MATH_ROUNDING_FUNCTIONS_H
115f757f3fSDimitry Andric 
125f757f3fSDimitry Andric #include <__config>
135f757f3fSDimitry Andric #include <__type_traits/enable_if.h>
145f757f3fSDimitry Andric #include <__type_traits/is_arithmetic.h>
155f757f3fSDimitry Andric #include <__type_traits/is_integral.h>
165f757f3fSDimitry Andric #include <__type_traits/is_same.h>
175f757f3fSDimitry Andric #include <__type_traits/promote.h>
185f757f3fSDimitry Andric 
195f757f3fSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
205f757f3fSDimitry Andric #  pragma GCC system_header
215f757f3fSDimitry Andric #endif
225f757f3fSDimitry Andric 
235f757f3fSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
245f757f3fSDimitry Andric 
255f757f3fSDimitry Andric namespace __math {
265f757f3fSDimitry Andric 
275f757f3fSDimitry Andric // ceil
285f757f3fSDimitry Andric 
ceil(float __x)29*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI float ceil(float __x) _NOEXCEPT { return __builtin_ceilf(__x); }
305f757f3fSDimitry Andric 
315f757f3fSDimitry Andric template <class = int>
ceil(double __x)32*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI double ceil(double __x) _NOEXCEPT {
335f757f3fSDimitry Andric   return __builtin_ceil(__x);
345f757f3fSDimitry Andric }
355f757f3fSDimitry Andric 
ceil(long double __x)36*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI long double ceil(long double __x) _NOEXCEPT {
375f757f3fSDimitry Andric   return __builtin_ceill(__x);
385f757f3fSDimitry Andric }
395f757f3fSDimitry Andric 
405f757f3fSDimitry Andric template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
ceil(_A1 __x)41*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI double ceil(_A1 __x) _NOEXCEPT {
425f757f3fSDimitry Andric   return __builtin_ceil((double)__x);
435f757f3fSDimitry Andric }
445f757f3fSDimitry Andric 
455f757f3fSDimitry Andric // floor
465f757f3fSDimitry Andric 
floor(float __x)47*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI float floor(float __x) _NOEXCEPT { return __builtin_floorf(__x); }
485f757f3fSDimitry Andric 
495f757f3fSDimitry Andric template <class = int>
floor(double __x)50*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI double floor(double __x) _NOEXCEPT {
515f757f3fSDimitry Andric   return __builtin_floor(__x);
525f757f3fSDimitry Andric }
535f757f3fSDimitry Andric 
floor(long double __x)54*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI long double floor(long double __x) _NOEXCEPT {
555f757f3fSDimitry Andric   return __builtin_floorl(__x);
565f757f3fSDimitry Andric }
575f757f3fSDimitry Andric 
585f757f3fSDimitry Andric template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
floor(_A1 __x)59*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI double floor(_A1 __x) _NOEXCEPT {
605f757f3fSDimitry Andric   return __builtin_floor((double)__x);
615f757f3fSDimitry Andric }
625f757f3fSDimitry Andric 
635f757f3fSDimitry Andric // llrint
645f757f3fSDimitry Andric 
llrint(float __x)655f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long long llrint(float __x) _NOEXCEPT { return __builtin_llrintf(__x); }
665f757f3fSDimitry Andric 
675f757f3fSDimitry Andric template <class = int>
llrint(double __x)685f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI long long llrint(double __x) _NOEXCEPT {
695f757f3fSDimitry Andric   return __builtin_llrint(__x);
705f757f3fSDimitry Andric }
715f757f3fSDimitry Andric 
llrint(long double __x)725f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long long llrint(long double __x) _NOEXCEPT { return __builtin_llrintl(__x); }
735f757f3fSDimitry Andric 
745f757f3fSDimitry Andric template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
llrint(_A1 __x)755f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long long llrint(_A1 __x) _NOEXCEPT {
765f757f3fSDimitry Andric   return __builtin_llrint((double)__x);
775f757f3fSDimitry Andric }
785f757f3fSDimitry Andric 
795f757f3fSDimitry Andric // llround
805f757f3fSDimitry Andric 
llround(float __x)815f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long long llround(float __x) _NOEXCEPT { return __builtin_llroundf(__x); }
825f757f3fSDimitry Andric 
835f757f3fSDimitry Andric template <class = int>
llround(double __x)845f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI long long llround(double __x) _NOEXCEPT {
855f757f3fSDimitry Andric   return __builtin_llround(__x);
865f757f3fSDimitry Andric }
875f757f3fSDimitry Andric 
llround(long double __x)885f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long long llround(long double __x) _NOEXCEPT { return __builtin_llroundl(__x); }
895f757f3fSDimitry Andric 
905f757f3fSDimitry Andric template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
llround(_A1 __x)915f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long long llround(_A1 __x) _NOEXCEPT {
925f757f3fSDimitry Andric   return __builtin_llround((double)__x);
935f757f3fSDimitry Andric }
945f757f3fSDimitry Andric 
955f757f3fSDimitry Andric // lrint
965f757f3fSDimitry Andric 
lrint(float __x)975f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long lrint(float __x) _NOEXCEPT { return __builtin_lrintf(__x); }
985f757f3fSDimitry Andric 
995f757f3fSDimitry Andric template <class = int>
lrint(double __x)1005f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI long lrint(double __x) _NOEXCEPT {
1015f757f3fSDimitry Andric   return __builtin_lrint(__x);
1025f757f3fSDimitry Andric }
1035f757f3fSDimitry Andric 
lrint(long double __x)1045f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long lrint(long double __x) _NOEXCEPT { return __builtin_lrintl(__x); }
1055f757f3fSDimitry Andric 
1065f757f3fSDimitry Andric template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
lrint(_A1 __x)1075f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long lrint(_A1 __x) _NOEXCEPT {
1085f757f3fSDimitry Andric   return __builtin_lrint((double)__x);
1095f757f3fSDimitry Andric }
1105f757f3fSDimitry Andric 
1115f757f3fSDimitry Andric // lround
1125f757f3fSDimitry Andric 
lround(float __x)1135f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long lround(float __x) _NOEXCEPT { return __builtin_lroundf(__x); }
1145f757f3fSDimitry Andric 
1155f757f3fSDimitry Andric template <class = int>
lround(double __x)1165f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI long lround(double __x) _NOEXCEPT {
1175f757f3fSDimitry Andric   return __builtin_lround(__x);
1185f757f3fSDimitry Andric }
1195f757f3fSDimitry Andric 
lround(long double __x)1205f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long lround(long double __x) _NOEXCEPT { return __builtin_lroundl(__x); }
1215f757f3fSDimitry Andric 
1225f757f3fSDimitry Andric template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
lround(_A1 __x)1235f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long lround(_A1 __x) _NOEXCEPT {
1245f757f3fSDimitry Andric   return __builtin_lround((double)__x);
1255f757f3fSDimitry Andric }
1265f757f3fSDimitry Andric 
1275f757f3fSDimitry Andric // nearbyint
1285f757f3fSDimitry Andric 
nearbyint(float __x)129*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI float nearbyint(float __x) _NOEXCEPT {
1305f757f3fSDimitry Andric   return __builtin_nearbyintf(__x);
1315f757f3fSDimitry Andric }
1325f757f3fSDimitry Andric 
1335f757f3fSDimitry Andric template <class = int>
nearbyint(double __x)134*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI double nearbyint(double __x) _NOEXCEPT {
1355f757f3fSDimitry Andric   return __builtin_nearbyint(__x);
1365f757f3fSDimitry Andric }
1375f757f3fSDimitry Andric 
nearbyint(long double __x)138*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI long double nearbyint(long double __x) _NOEXCEPT {
1395f757f3fSDimitry Andric   return __builtin_nearbyintl(__x);
1405f757f3fSDimitry Andric }
1415f757f3fSDimitry Andric 
1425f757f3fSDimitry Andric template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
nearbyint(_A1 __x)143*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI double nearbyint(_A1 __x) _NOEXCEPT {
1445f757f3fSDimitry Andric   return __builtin_nearbyint((double)__x);
1455f757f3fSDimitry Andric }
1465f757f3fSDimitry Andric 
1475f757f3fSDimitry Andric // nextafter
1485f757f3fSDimitry Andric 
nextafter(float __x,float __y)1495f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI float nextafter(float __x, float __y) _NOEXCEPT { return __builtin_nextafterf(__x, __y); }
1505f757f3fSDimitry Andric 
1515f757f3fSDimitry Andric template <class = int>
nextafter(double __x,double __y)1525f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI double nextafter(double __x, double __y) _NOEXCEPT {
1535f757f3fSDimitry Andric   return __builtin_nextafter(__x, __y);
1545f757f3fSDimitry Andric }
1555f757f3fSDimitry Andric 
nextafter(long double __x,long double __y)1565f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long double nextafter(long double __x, long double __y) _NOEXCEPT {
1575f757f3fSDimitry Andric   return __builtin_nextafterl(__x, __y);
1585f757f3fSDimitry Andric }
1595f757f3fSDimitry Andric 
1605f757f3fSDimitry Andric template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
nextafter(_A1 __x,_A2 __y)1615f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI typename __promote<_A1, _A2>::type nextafter(_A1 __x, _A2 __y) _NOEXCEPT {
1625f757f3fSDimitry Andric   using __result_type = typename __promote<_A1, _A2>::type;
163*0fca6ea1SDimitry Andric   static_assert(!(_IsSame<_A1, __result_type>::value && _IsSame<_A2, __result_type>::value), "");
1645f757f3fSDimitry Andric   return __math::nextafter((__result_type)__x, (__result_type)__y);
1655f757f3fSDimitry Andric }
1665f757f3fSDimitry Andric 
1675f757f3fSDimitry Andric // nexttoward
1685f757f3fSDimitry Andric 
nexttoward(float __x,long double __y)1695f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI float nexttoward(float __x, long double __y) _NOEXCEPT {
1705f757f3fSDimitry Andric   return __builtin_nexttowardf(__x, __y);
1715f757f3fSDimitry Andric }
1725f757f3fSDimitry Andric 
1735f757f3fSDimitry Andric template <class = int>
nexttoward(double __x,long double __y)1745f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI double nexttoward(double __x, long double __y) _NOEXCEPT {
1755f757f3fSDimitry Andric   return __builtin_nexttoward(__x, __y);
1765f757f3fSDimitry Andric }
1775f757f3fSDimitry Andric 
nexttoward(long double __x,long double __y)1785f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long double nexttoward(long double __x, long double __y) _NOEXCEPT {
1795f757f3fSDimitry Andric   return __builtin_nexttowardl(__x, __y);
1805f757f3fSDimitry Andric }
1815f757f3fSDimitry Andric 
1825f757f3fSDimitry Andric template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
nexttoward(_A1 __x,long double __y)1835f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI double nexttoward(_A1 __x, long double __y) _NOEXCEPT {
1845f757f3fSDimitry Andric   return __builtin_nexttoward((double)__x, __y);
1855f757f3fSDimitry Andric }
1865f757f3fSDimitry Andric 
1875f757f3fSDimitry Andric // rint
1885f757f3fSDimitry Andric 
rint(float __x)189*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI float rint(float __x) _NOEXCEPT { return __builtin_rintf(__x); }
1905f757f3fSDimitry Andric 
1915f757f3fSDimitry Andric template <class = int>
rint(double __x)192*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI double rint(double __x) _NOEXCEPT {
1935f757f3fSDimitry Andric   return __builtin_rint(__x);
1945f757f3fSDimitry Andric }
1955f757f3fSDimitry Andric 
rint(long double __x)196*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI long double rint(long double __x) _NOEXCEPT {
1975f757f3fSDimitry Andric   return __builtin_rintl(__x);
1985f757f3fSDimitry Andric }
1995f757f3fSDimitry Andric 
2005f757f3fSDimitry Andric template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
rint(_A1 __x)201*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI double rint(_A1 __x) _NOEXCEPT {
2025f757f3fSDimitry Andric   return __builtin_rint((double)__x);
2035f757f3fSDimitry Andric }
2045f757f3fSDimitry Andric 
2055f757f3fSDimitry Andric // round
2065f757f3fSDimitry Andric 
round(float __x)207*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI float round(float __x) _NOEXCEPT { return __builtin_round(__x); }
2085f757f3fSDimitry Andric 
2095f757f3fSDimitry Andric template <class = int>
round(double __x)210*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI double round(double __x) _NOEXCEPT {
2115f757f3fSDimitry Andric   return __builtin_round(__x);
2125f757f3fSDimitry Andric }
2135f757f3fSDimitry Andric 
round(long double __x)214*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI long double round(long double __x) _NOEXCEPT {
2155f757f3fSDimitry Andric   return __builtin_roundl(__x);
2165f757f3fSDimitry Andric }
2175f757f3fSDimitry Andric 
2185f757f3fSDimitry Andric template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
round(_A1 __x)219*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI double round(_A1 __x) _NOEXCEPT {
2205f757f3fSDimitry Andric   return __builtin_round((double)__x);
2215f757f3fSDimitry Andric }
2225f757f3fSDimitry Andric 
2235f757f3fSDimitry Andric // trunc
2245f757f3fSDimitry Andric 
trunc(float __x)225*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI float trunc(float __x) _NOEXCEPT { return __builtin_trunc(__x); }
2265f757f3fSDimitry Andric 
2275f757f3fSDimitry Andric template <class = int>
trunc(double __x)228*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI double trunc(double __x) _NOEXCEPT {
2295f757f3fSDimitry Andric   return __builtin_trunc(__x);
2305f757f3fSDimitry Andric }
2315f757f3fSDimitry Andric 
trunc(long double __x)232*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI long double trunc(long double __x) _NOEXCEPT {
2335f757f3fSDimitry Andric   return __builtin_truncl(__x);
2345f757f3fSDimitry Andric }
2355f757f3fSDimitry Andric 
2365f757f3fSDimitry Andric template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
trunc(_A1 __x)237*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI double trunc(_A1 __x) _NOEXCEPT {
2385f757f3fSDimitry Andric   return __builtin_trunc((double)__x);
2395f757f3fSDimitry Andric }
2405f757f3fSDimitry Andric 
2415f757f3fSDimitry Andric } // namespace __math
2425f757f3fSDimitry Andric 
2435f757f3fSDimitry Andric _LIBCPP_END_NAMESPACE_STD
2445f757f3fSDimitry Andric 
2455f757f3fSDimitry Andric #endif // _LIBCPP___MATH_ROUNDING_FUNCTIONS_H
246