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_TRAITS_H
10#define _LIBCPP___MATH_TRAITS_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/promote.h>
17
18#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19# pragma GCC system_header
20#endif
21
22_LIBCPP_BEGIN_NAMESPACE_STD
23
24namespace __math {
25
26// signbit
27
28// The universal C runtime (UCRT) in the WinSDK provides floating point overloads
29// for std::signbit(). By defining our overloads as templates, we can work around
30// this issue as templates are less preferred than non-template functions.
31template <class = void>
32[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool signbit(float __x) _NOEXCEPT {
33 return __builtin_signbit(__x);
34}
35
36template <class = void>
37[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool signbit(double __x) _NOEXCEPT {
38 return __builtin_signbit(__x);
39}
40
41template <class = void>
42[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool signbit(long double __x) _NOEXCEPT {
43 return __builtin_signbit(__x);
44}
45
46template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
47[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool signbit(_A1 __x) _NOEXCEPT {
48 return __x < 0;
49}
50
51// isfinite
52
53template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
54[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isfinite(_A1) _NOEXCEPT {
55 return true;
56}
57
58[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isfinite(float __x) _NOEXCEPT {
59 return __builtin_isfinite(__x);
60}
61
62[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isfinite(double __x) _NOEXCEPT {
63 return __builtin_isfinite(__x);
64}
65
66[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isfinite(long double __x) _NOEXCEPT {
67 return __builtin_isfinite(__x);
68}
69
70// isinf
71
72template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
73[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isinf(_A1) _NOEXCEPT {
74 return false;
75}
76
77[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isinf(float __x) _NOEXCEPT {
78 return __builtin_isinf(__x);
79}
80
81[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI
82#ifdef _LIBCPP_PREFERRED_OVERLOAD
83_LIBCPP_PREFERRED_OVERLOAD
84#endif
85 bool
86 isinf(double __x) _NOEXCEPT {
87 return __builtin_isinf(__x);
88}
89
90[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isinf(long double __x) _NOEXCEPT {
91 return __builtin_isinf(__x);
92}
93
94// isnan
95
96template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
97[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnan(_A1) _NOEXCEPT {
98 return false;
99}
100
101[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnan(float __x) _NOEXCEPT {
102 return __builtin_isnan(__x);
103}
104
105[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI
106#ifdef _LIBCPP_PREFERRED_OVERLOAD
107_LIBCPP_PREFERRED_OVERLOAD
108#endif
109 bool
110 isnan(double __x) _NOEXCEPT {
111 return __builtin_isnan(__x);
112}
113
114[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnan(long double __x) _NOEXCEPT {
115 return __builtin_isnan(__x);
116}
117
118// isnormal
119
120template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
121[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnormal(_A1 __x) _NOEXCEPT {
122 return __x != 0;
123}
124
125[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnormal(float __x) _NOEXCEPT {
126 return __builtin_isnormal(__x);
127}
128
129[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnormal(double __x) _NOEXCEPT {
130 return __builtin_isnormal(__x);
131}
132
133[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnormal(long double __x) _NOEXCEPT {
134 return __builtin_isnormal(__x);
135}
136
137// isgreater
138
139template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
140[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool
141isgreater(_A1 __x, _A2 __y) _NOEXCEPT {
142 using type = __promote_t<_A1, _A2>;
143 return __builtin_isgreater((type)__x, (type)__y);
144}
145
146// isgreaterequal
147
148template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
149[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool
150isgreaterequal(_A1 __x, _A2 __y) _NOEXCEPT {
151 using type = __promote_t<_A1, _A2>;
152 return __builtin_isgreaterequal((type)__x, (type)__y);
153}
154
155// isless
156
157template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
158[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isless(_A1 __x, _A2 __y) _NOEXCEPT {
159 using type = __promote_t<_A1, _A2>;
160 return __builtin_isless((type)__x, (type)__y);
161}
162
163// islessequal
164
165template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
166[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool
167islessequal(_A1 __x, _A2 __y) _NOEXCEPT {
168 using type = __promote_t<_A1, _A2>;
169 return __builtin_islessequal((type)__x, (type)__y);
170}
171
172// islessgreater
173
174template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
175[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool
176islessgreater(_A1 __x, _A2 __y) _NOEXCEPT {
177 using type = __promote_t<_A1, _A2>;
178 return __builtin_islessgreater((type)__x, (type)__y);
179}
180
181// isunordered
182
183template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
184[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool
185isunordered(_A1 __x, _A2 __y) _NOEXCEPT {
186 using type = __promote_t<_A1, _A2>;
187 return __builtin_isunordered((type)__x, (type)__y);
188}
189
190// MS UCRT incorrectly defines some functions in a way not working with integer types. Until C++20, this was worked
191// around by -fdelayed-template-parsing. Since C++20, we can use standard feature "requires" instead.
192
193// TODO: Remove the workaround once UCRT fixes these functions. Note that this doesn't seem planned as of 2025-07 per
194// https://developercommunity.visualstudio.com/t/10294165.
195
196#if defined(_LIBCPP_MSVCRT) && _LIBCPP_STD_VER >= 20
197namespace __ucrt {
198template <class _A1>
199 requires is_integral_v<_A1>
200[[nodiscard]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isfinite(_A1) noexcept {
201 return true;
202}
203
204template <class _A1>
205 requires is_integral_v<_A1>
206[[nodiscard]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isinf(_A1) noexcept {
207 return false;
208}
209
210template <class _A1>
211 requires is_integral_v<_A1>
212[[nodiscard]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnan(_A1) noexcept {
213 return false;
214}
215
216template <class _A1>
217 requires is_integral_v<_A1>
218[[nodiscard]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnormal(_A1 __x) noexcept {
219 return __x != 0;
220}
221
222template <class _A1, class _A2>
223 requires is_arithmetic_v<_A1> && is_arithmetic_v<_A2>
224[[nodiscard]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isgreater(_A1 __x, _A2 __y) noexcept {
225 using type = __promote_t<_A1, _A2>;
226 return __builtin_isgreater((type)__x, (type)__y);
227}
228
229template <class _A1, class _A2>
230 requires is_arithmetic_v<_A1> && is_arithmetic_v<_A2>
231[[nodiscard]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool
232isgreaterequal(_A1 __x, _A2 __y) noexcept {
233 using type = __promote_t<_A1, _A2>;
234 return __builtin_isgreaterequal((type)__x, (type)__y);
235}
236
237template <class _A1, class _A2>
238 requires is_arithmetic_v<_A1> && is_arithmetic_v<_A2>
239[[nodiscard]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isless(_A1 __x, _A2 __y) noexcept {
240 using type = __promote_t<_A1, _A2>;
241 return __builtin_isless((type)__x, (type)__y);
242}
243
244template <class _A1, class _A2>
245 requires is_arithmetic_v<_A1> && is_arithmetic_v<_A2>
246[[nodiscard]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool islessequal(_A1 __x, _A2 __y) noexcept {
247 using type = __promote_t<_A1, _A2>;
248 return __builtin_islessequal((type)__x, (type)__y);
249}
250
251template <class _A1, class _A2>
252 requires is_arithmetic_v<_A1> && is_arithmetic_v<_A2>
253[[nodiscard]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool islessgreater(_A1 __x, _A2 __y) noexcept {
254 using type = __promote_t<_A1, _A2>;
255 return __builtin_islessgreater((type)__x, (type)__y);
256}
257
258template <class _A1, class _A2>
259 requires is_arithmetic_v<_A1> && is_arithmetic_v<_A2>
260[[nodiscard]] inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isunordered(_A1 __x, _A2 __y) noexcept {
261 using type = __promote_t<_A1, _A2>;
262 return __builtin_isunordered((type)__x, (type)__y);
263}
264} // namespace __ucrt
265#endif
266
267} // namespace __math
268
269_LIBCPP_END_NAMESPACE_STD
270
271#endif // _LIBCPP___MATH_TRAITS_H
272