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