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_floating_point.h>
16#include <__type_traits/is_integral.h>
17#include <__type_traits/is_signed.h>
18#include <__type_traits/promote.h>
19#include <limits>
20
21#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22# pragma GCC system_header
23#endif
24
25_LIBCPP_BEGIN_NAMESPACE_STD
26
27namespace __math {
28
29// signbit
30
31template <class _A1, __enable_if_t<is_floating_point<_A1>::value, int> = 0>
32_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI bool signbit(_A1 __x) _NOEXCEPT {
33 return __builtin_signbit(__x);
34}
35
36template <class _A1, __enable_if_t<is_integral<_A1>::value && is_signed<_A1>::value, int> = 0>
37_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI bool signbit(_A1 __x) _NOEXCEPT {
38 return __x < 0;
39}
40
41template <class _A1, __enable_if_t<is_integral<_A1>::value && !is_signed<_A1>::value, int> = 0>
42_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI bool signbit(_A1) _NOEXCEPT {
43 return false;
44}
45
46// isfinite
47
48template <class _A1, __enable_if_t<is_arithmetic<_A1>::value && numeric_limits<_A1>::has_infinity, int> = 0>
49_LIBCPP_NODISCARD _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isfinite(_A1 __x) _NOEXCEPT {
50 return __builtin_isfinite((typename __promote<_A1>::type)__x);
51}
52
53template <class _A1, __enable_if_t<is_arithmetic<_A1>::value && !numeric_limits<_A1>::has_infinity, int> = 0>
54_LIBCPP_NODISCARD _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isfinite(_A1) _NOEXCEPT {
55 return true;
56}
57
58_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isfinite(float __x) _NOEXCEPT {
59 return __builtin_isfinite(__x);
60}
61
62_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isfinite(double __x) _NOEXCEPT {
63 return __builtin_isfinite(__x);
64}
65
66_LIBCPP_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_arithmetic<_A1>::value && numeric_limits<_A1>::has_infinity, int> = 0>
73_LIBCPP_NODISCARD _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isinf(_A1 __x) _NOEXCEPT {
74 return __builtin_isinf((typename __promote<_A1>::type)__x);
75}
76
77template <class _A1, __enable_if_t<is_arithmetic<_A1>::value && !numeric_limits<_A1>::has_infinity, int> = 0>
78_LIBCPP_NODISCARD _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isinf(_A1) _NOEXCEPT {
79 return false;
80}
81
82#ifdef _LIBCPP_PREFERRED_OVERLOAD
83_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isinf(float __x) _NOEXCEPT {
84 return __builtin_isinf(__x);
85}
86
87_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI _LIBCPP_PREFERRED_OVERLOAD bool
88isinf(double __x) _NOEXCEPT {
89 return __builtin_isinf(__x);
90}
91
92_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isinf(long double __x) _NOEXCEPT {
93 return __builtin_isinf(__x);
94}
95#endif
96
97// isnan
98
99template <class _A1, __enable_if_t<is_floating_point<_A1>::value, int> = 0>
100_LIBCPP_NODISCARD _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnan(_A1 __x) _NOEXCEPT {
101 return __builtin_isnan(__x);
102}
103
104template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
105_LIBCPP_NODISCARD _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnan(_A1) _NOEXCEPT {
106 return false;
107}
108
109#ifdef _LIBCPP_PREFERRED_OVERLOAD
110_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnan(float __x) _NOEXCEPT {
111 return __builtin_isnan(__x);
112}
113
114_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI _LIBCPP_PREFERRED_OVERLOAD bool
115isnan(double __x) _NOEXCEPT {
116 return __builtin_isnan(__x);
117}
118
119_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnan(long double __x) _NOEXCEPT {
120 return __builtin_isnan(__x);
121}
122#endif
123
124// isnormal
125
126template <class _A1, __enable_if_t<is_floating_point<_A1>::value, int> = 0>
127_LIBCPP_NODISCARD _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnormal(_A1 __x) _NOEXCEPT {
128 return __builtin_isnormal(__x);
129}
130
131template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
132_LIBCPP_NODISCARD _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnormal(_A1 __x) _NOEXCEPT {
133 return __x != 0;
134}
135
136// isgreater
137
138template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
139_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI bool isgreater(_A1 __x, _A2 __y) _NOEXCEPT {
140 using type = typename __promote<_A1, _A2>::type;
141 return __builtin_isgreater((type)__x, (type)__y);
142}
143
144// isgreaterequal
145
146template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
147_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI bool isgreaterequal(_A1 __x, _A2 __y) _NOEXCEPT {
148 using type = typename __promote<_A1, _A2>::type;
149 return __builtin_isgreaterequal((type)__x, (type)__y);
150}
151
152// isless
153
154template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
155_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI bool isless(_A1 __x, _A2 __y) _NOEXCEPT {
156 using type = typename __promote<_A1, _A2>::type;
157 return __builtin_isless((type)__x, (type)__y);
158}
159
160// islessequal
161
162template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
163_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI bool islessequal(_A1 __x, _A2 __y) _NOEXCEPT {
164 using type = typename __promote<_A1, _A2>::type;
165 return __builtin_islessequal((type)__x, (type)__y);
166}
167
168// islessgreater
169
170template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
171_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI bool islessgreater(_A1 __x, _A2 __y) _NOEXCEPT {
172 using type = typename __promote<_A1, _A2>::type;
173 return __builtin_islessgreater((type)__x, (type)__y);
174}
175
176// isunordered
177
178template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
179_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI bool isunordered(_A1 __x, _A2 __y) _NOEXCEPT {
180 using type = typename __promote<_A1, _A2>::type;
181 return __builtin_isunordered((type)__x, (type)__y);
182}
183
184} // namespace __math
185
186_LIBCPP_END_NAMESPACE_STD
187
188#endif // _LIBCPP___MATH_TRAITS_H
189