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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of libcxx/test/std/numerics/c.math/signbit.pass.cpp