xref: /llvm-project/libcxx/test/std/numerics/c.math/signbit.pass.cpp (revision 7f845cba2ccc2ab637b8e40fbafb9f83a2d67c70)
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 // bool signbit(floating-point-type x); // constexpr since C++23
10 
11 // We don't control the implementation on windows
12 // UNSUPPORTED: windows
13 
14 // These compilers don't support constexpr `__builtin_signbit` yet.
15 // UNSUPPORTED: clang-18, clang-19, apple-clang-15, apple-clang-16
16 
17 // XFAIL: FROZEN-CXX03-HEADERS-FIXME
18 
19 #include <cassert>
20 #include <cmath>
21 #include <limits>
22 
23 #include "test_macros.h"
24 #include "type_algorithms.h"
25 
26 struct TestFloat {
27   template <class T>
28   static TEST_CONSTEXPR_CXX23 bool test() {
29     assert(!std::signbit(T(0)));
30     assert(!std::signbit(std::numeric_limits<T>::min()));
31     assert(!std::signbit(std::numeric_limits<T>::denorm_min()));
32     assert(!std::signbit(std::numeric_limits<T>::max()));
33     assert(!std::signbit(std::numeric_limits<T>::infinity()));
34     assert(!std::signbit(std::numeric_limits<T>::quiet_NaN()));
35     assert(!std::signbit(std::numeric_limits<T>::signaling_NaN()));
36     assert(std::signbit(-T(0)));
37     assert(std::signbit(-std::numeric_limits<T>::infinity()));
38     assert(std::signbit(std::numeric_limits<T>::lowest()));
39 
40     return true;
41   }
42 
43   template <class T>
44   TEST_CONSTEXPR_CXX23 void operator()() {
45     test<T>();
46 #if TEST_STD_VER >= 23
47     static_assert(test<T>());
48 #endif
49   }
50 };
51 
52 struct TestInt {
53   template <class T>
54   static TEST_CONSTEXPR_CXX23 bool test() {
55     assert(!std::signbit(std::numeric_limits<T>::max()));
56     assert(!std::signbit(T(0)));
57     if (std::is_unsigned<T>::value) {
58       assert(!std::signbit(std::numeric_limits<T>::lowest()));
59     } else {
60       assert(std::signbit(std::numeric_limits<T>::lowest()));
61     }
62 
63     return true;
64   }
65 
66   template <class T>
67   TEST_CONSTEXPR_CXX23 void operator()() {
68     test<T>();
69 #if TEST_STD_VER >= 23
70     static_assert(test<T>());
71 #endif
72   }
73 };
74 
75 template <typename T>
76 struct ConvertibleTo {
77   operator T() const { return T(); }
78 };
79 
80 int main(int, char**) {
81   types::for_each(types::floating_point_types(), TestFloat());
82   types::for_each(types::integral_types(), TestInt());
83 
84   // Make sure we can call `std::signbit` with convertible types. This checks
85   // whether overloads for all cv-unqualified floating-point types are working
86   // as expected.
87   {
88     assert(!std::signbit(ConvertibleTo<float>()));
89     assert(!std::signbit(ConvertibleTo<double>()));
90     assert(!std::signbit(ConvertibleTo<long double>()));
91   }
92   return 0;
93 }
94