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 
9 #ifndef _LIBCPP___MATH_ROUNDING_FUNCTIONS_H
10 #define _LIBCPP___MATH_ROUNDING_FUNCTIONS_H
11 
12 #include <__config>
13 #include <__type_traits/enable_if.h>
14 #include <__type_traits/is_arithmetic.h>
15 #include <__type_traits/is_integral.h>
16 #include <__type_traits/is_same.h>
17 #include <__type_traits/promote.h>
18 
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #  pragma GCC system_header
21 #endif
22 
23 _LIBCPP_BEGIN_NAMESPACE_STD
24 
25 namespace __math {
26 
27 // ceil
28 
ceil(float __x)29 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI float ceil(float __x) _NOEXCEPT { return __builtin_ceilf(__x); }
30 
31 template <class = int>
ceil(double __x)32 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI double ceil(double __x) _NOEXCEPT {
33   return __builtin_ceil(__x);
34 }
35 
ceil(long double __x)36 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI long double ceil(long double __x) _NOEXCEPT {
37   return __builtin_ceill(__x);
38 }
39 
40 template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
ceil(_A1 __x)41 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI double ceil(_A1 __x) _NOEXCEPT {
42   return __builtin_ceil((double)__x);
43 }
44 
45 // floor
46 
floor(float __x)47 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI float floor(float __x) _NOEXCEPT { return __builtin_floorf(__x); }
48 
49 template <class = int>
floor(double __x)50 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI double floor(double __x) _NOEXCEPT {
51   return __builtin_floor(__x);
52 }
53 
floor(long double __x)54 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI long double floor(long double __x) _NOEXCEPT {
55   return __builtin_floorl(__x);
56 }
57 
58 template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
floor(_A1 __x)59 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI double floor(_A1 __x) _NOEXCEPT {
60   return __builtin_floor((double)__x);
61 }
62 
63 // llrint
64 
llrint(float __x)65 inline _LIBCPP_HIDE_FROM_ABI long long llrint(float __x) _NOEXCEPT { return __builtin_llrintf(__x); }
66 
67 template <class = int>
llrint(double __x)68 _LIBCPP_HIDE_FROM_ABI long long llrint(double __x) _NOEXCEPT {
69   return __builtin_llrint(__x);
70 }
71 
llrint(long double __x)72 inline _LIBCPP_HIDE_FROM_ABI long long llrint(long double __x) _NOEXCEPT { return __builtin_llrintl(__x); }
73 
74 template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
llrint(_A1 __x)75 inline _LIBCPP_HIDE_FROM_ABI long long llrint(_A1 __x) _NOEXCEPT {
76   return __builtin_llrint((double)__x);
77 }
78 
79 // llround
80 
llround(float __x)81 inline _LIBCPP_HIDE_FROM_ABI long long llround(float __x) _NOEXCEPT { return __builtin_llroundf(__x); }
82 
83 template <class = int>
llround(double __x)84 _LIBCPP_HIDE_FROM_ABI long long llround(double __x) _NOEXCEPT {
85   return __builtin_llround(__x);
86 }
87 
llround(long double __x)88 inline _LIBCPP_HIDE_FROM_ABI long long llround(long double __x) _NOEXCEPT { return __builtin_llroundl(__x); }
89 
90 template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
llround(_A1 __x)91 inline _LIBCPP_HIDE_FROM_ABI long long llround(_A1 __x) _NOEXCEPT {
92   return __builtin_llround((double)__x);
93 }
94 
95 // lrint
96 
lrint(float __x)97 inline _LIBCPP_HIDE_FROM_ABI long lrint(float __x) _NOEXCEPT { return __builtin_lrintf(__x); }
98 
99 template <class = int>
lrint(double __x)100 _LIBCPP_HIDE_FROM_ABI long lrint(double __x) _NOEXCEPT {
101   return __builtin_lrint(__x);
102 }
103 
lrint(long double __x)104 inline _LIBCPP_HIDE_FROM_ABI long lrint(long double __x) _NOEXCEPT { return __builtin_lrintl(__x); }
105 
106 template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
lrint(_A1 __x)107 inline _LIBCPP_HIDE_FROM_ABI long lrint(_A1 __x) _NOEXCEPT {
108   return __builtin_lrint((double)__x);
109 }
110 
111 // lround
112 
lround(float __x)113 inline _LIBCPP_HIDE_FROM_ABI long lround(float __x) _NOEXCEPT { return __builtin_lroundf(__x); }
114 
115 template <class = int>
lround(double __x)116 _LIBCPP_HIDE_FROM_ABI long lround(double __x) _NOEXCEPT {
117   return __builtin_lround(__x);
118 }
119 
lround(long double __x)120 inline _LIBCPP_HIDE_FROM_ABI long lround(long double __x) _NOEXCEPT { return __builtin_lroundl(__x); }
121 
122 template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
lround(_A1 __x)123 inline _LIBCPP_HIDE_FROM_ABI long lround(_A1 __x) _NOEXCEPT {
124   return __builtin_lround((double)__x);
125 }
126 
127 // nearbyint
128 
nearbyint(float __x)129 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI float nearbyint(float __x) _NOEXCEPT {
130   return __builtin_nearbyintf(__x);
131 }
132 
133 template <class = int>
nearbyint(double __x)134 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI double nearbyint(double __x) _NOEXCEPT {
135   return __builtin_nearbyint(__x);
136 }
137 
nearbyint(long double __x)138 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI long double nearbyint(long double __x) _NOEXCEPT {
139   return __builtin_nearbyintl(__x);
140 }
141 
142 template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
nearbyint(_A1 __x)143 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI double nearbyint(_A1 __x) _NOEXCEPT {
144   return __builtin_nearbyint((double)__x);
145 }
146 
147 // nextafter
148 
nextafter(float __x,float __y)149 inline _LIBCPP_HIDE_FROM_ABI float nextafter(float __x, float __y) _NOEXCEPT { return __builtin_nextafterf(__x, __y); }
150 
151 template <class = int>
nextafter(double __x,double __y)152 _LIBCPP_HIDE_FROM_ABI double nextafter(double __x, double __y) _NOEXCEPT {
153   return __builtin_nextafter(__x, __y);
154 }
155 
nextafter(long double __x,long double __y)156 inline _LIBCPP_HIDE_FROM_ABI long double nextafter(long double __x, long double __y) _NOEXCEPT {
157   return __builtin_nextafterl(__x, __y);
158 }
159 
160 template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
nextafter(_A1 __x,_A2 __y)161 inline _LIBCPP_HIDE_FROM_ABI typename __promote<_A1, _A2>::type nextafter(_A1 __x, _A2 __y) _NOEXCEPT {
162   using __result_type = typename __promote<_A1, _A2>::type;
163   static_assert((!(_IsSame<_A1, __result_type>::value && _IsSame<_A2, __result_type>::value)), "");
164   return __math::nextafter((__result_type)__x, (__result_type)__y);
165 }
166 
167 // nexttoward
168 
nexttoward(float __x,long double __y)169 inline _LIBCPP_HIDE_FROM_ABI float nexttoward(float __x, long double __y) _NOEXCEPT {
170   return __builtin_nexttowardf(__x, __y);
171 }
172 
173 template <class = int>
nexttoward(double __x,long double __y)174 _LIBCPP_HIDE_FROM_ABI double nexttoward(double __x, long double __y) _NOEXCEPT {
175   return __builtin_nexttoward(__x, __y);
176 }
177 
nexttoward(long double __x,long double __y)178 inline _LIBCPP_HIDE_FROM_ABI long double nexttoward(long double __x, long double __y) _NOEXCEPT {
179   return __builtin_nexttowardl(__x, __y);
180 }
181 
182 template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
nexttoward(_A1 __x,long double __y)183 inline _LIBCPP_HIDE_FROM_ABI double nexttoward(_A1 __x, long double __y) _NOEXCEPT {
184   return __builtin_nexttoward((double)__x, __y);
185 }
186 
187 // rint
188 
rint(float __x)189 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI float rint(float __x) _NOEXCEPT { return __builtin_rintf(__x); }
190 
191 template <class = int>
rint(double __x)192 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI double rint(double __x) _NOEXCEPT {
193   return __builtin_rint(__x);
194 }
195 
rint(long double __x)196 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI long double rint(long double __x) _NOEXCEPT {
197   return __builtin_rintl(__x);
198 }
199 
200 template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
rint(_A1 __x)201 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI double rint(_A1 __x) _NOEXCEPT {
202   return __builtin_rint((double)__x);
203 }
204 
205 // round
206 
round(float __x)207 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI float round(float __x) _NOEXCEPT { return __builtin_round(__x); }
208 
209 template <class = int>
round(double __x)210 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI double round(double __x) _NOEXCEPT {
211   return __builtin_round(__x);
212 }
213 
round(long double __x)214 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI long double round(long double __x) _NOEXCEPT {
215   return __builtin_roundl(__x);
216 }
217 
218 template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
round(_A1 __x)219 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI double round(_A1 __x) _NOEXCEPT {
220   return __builtin_round((double)__x);
221 }
222 
223 // trunc
224 
trunc(float __x)225 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI float trunc(float __x) _NOEXCEPT { return __builtin_trunc(__x); }
226 
227 template <class = int>
trunc(double __x)228 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI double trunc(double __x) _NOEXCEPT {
229   return __builtin_trunc(__x);
230 }
231 
trunc(long double __x)232 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI long double trunc(long double __x) _NOEXCEPT {
233   return __builtin_truncl(__x);
234 }
235 
236 template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
trunc(_A1 __x)237 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI double trunc(_A1 __x) _NOEXCEPT {
238   return __builtin_trunc((double)__x);
239 }
240 
241 } // namespace __math
242 
243 _LIBCPP_END_NAMESPACE_STD
244 
245 #endif // _LIBCPP___MATH_ROUNDING_FUNCTIONS_H
246