Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
float_sign.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: builtin_signbit_is_constexpr does relate to floating point signs"
2// Copyright 2025, Toyota Motor Corporation
3//
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5
6#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_FLOAT_SIGN_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_FLOAT_SIGN_HPP_
8
9// IWYU pragma: private, include "arene/base/math.hpp"
10// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
11
14#include "arene/base/stdlib_choice/enable_if.hpp"
15#include "arene/base/stdlib_choice/integral_constant.hpp"
16
17// parasoft-begin-suppress CERT_CPP-DCL51-f "False positive: these names are not reserved in namespace arene::base"
18// parasoft-begin-suppress AUTOSAR-M17_0_3-a "False positive: the names are added in namespace arene::base"
19// parasoft-begin-suppress AUTOSAR-A7_1_5-a "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
20
21namespace arene {
22namespace base {
23
24/// @brief Return a number with the magnitude of the first operand and the sign of the second
25/// @param magnitude The operand to take the magnitude from
26/// @param sign The operand to take the sign from
27/// @return A value with the same absolute value as @c magnitude and the same sign as @c sign
28constexpr auto copysign(float const magnitude, float const sign) noexcept -> float {
29 return __builtin_copysignf(magnitude, sign);
30}
31
32/// @brief Return a number with the magnitude of the first operand and the sign of the second
33/// @param magnitude The operand to take the magnitude from
34/// @param sign The operand to take the sign from
35/// @return A value with the same absolute value as @c magnitude and the same sign as @c sign
36constexpr auto copysign(double const magnitude, double const sign) noexcept -> double {
37 return __builtin_copysign(magnitude, sign);
38}
39
40namespace float_sign_detail {
41/// @brief Type trait to check whether or not __builtin_signbit is constexpr for the given argument type
42/// @tparam T Argument type with which __builtin_signbit will be called
43template <typename T, typename = constraints<>>
44class builtin_signbit_is_constexpr : public std::false_type {};
45
46/// @brief Type trait to check whether or not __builtin_signbit is constexpr for the given argument type
47/// @tparam T Argument type with which __builtin_signbit will be called
48template <typename T>
49class builtin_signbit_is_constexpr<T, constraints<std::enable_if_t<(static_cast<void>(__builtin_signbit(T{})), true)>>>
50 : public std::true_type {};
51
52/// @brief Type trait to check whether or not __builtin_signbit is constexpr for the given argument type
53/// @tparam T Argument type with which __builtin_signbit will be called
54template <typename T>
55constexpr bool builtin_signbit_is_constexpr_v = builtin_signbit_is_constexpr<T>::value;
56
57/// @brief Implementation of @c signbit function when @c __builtin_signbit is constexpr (all GCC versions, Clang v20.1+)
58/// @tparam T Argument type to check the sign bit of
59/// @param num Value to check the sign bit of
60/// @return @c true if @c num is *negative* (i.e. its sign bit is 1), @c false if @c num is positive
61template <typename T, constraints<std::enable_if_t<builtin_signbit_is_constexpr_v<T>>> = nullptr>
62constexpr auto signbit(T const num) noexcept -> bool {
63 return __builtin_signbit(num);
64}
65
67ARENE_IGNORE_ALL("-Wfloat-equal", "Equality operand is guaranteed to be either exactly 1.0 or exactly -1.0");
68
69/// @brief Implementation of @c signbit function when @c __builtin_signbit is *not* constexpr (Clang v19.4-)
70/// @tparam T Argument type to check the sign bit of
71/// @param num Value to check the sign bit of
72/// @return @c true if @c num is *negative* (i.e. its sign bit is 1), @c false if @c num is positive
73template <typename T, constraints<std::enable_if_t<!builtin_signbit_is_constexpr_v<T>>> = nullptr>
74constexpr auto signbit(T const num) noexcept -> bool {
75 // ISO/IEC 60559 does not guarantee that this will work when num is a NaN, but it does work in practice on the
76 // relevant platforms. Its correctness is checked in the unit tests.
77 auto sign_carrier = ::arene::base::copysign(static_cast<T>(1.0), num);
78 return sign_carrier == static_cast<T>(-1.0);
79}
80
82
83} // namespace float_sign_detail
84
85/// @brief Check the sign bit of a floating point number
86/// @param num The number to check
87/// @return @c true if @c num is *negative* (i.e. its sign bit is 1), @c false if @c num is positive
88constexpr auto signbit(float const num) noexcept -> bool { return float_sign_detail::signbit(num); }
89
90/// @brief Check the sign bit of a floating point number
91/// @param num The number to check
92/// @return @c true if @c num is *negative* (i.e. its sign bit is 1), @c false if @c num is positive
93constexpr auto signbit(double const num) noexcept -> bool { return float_sign_detail::signbit(num); }
94
95} // namespace base
96} // namespace arene
97
98// parasoft-end-suppress CERT_CPP-DCL51-f
99// parasoft-end-suppress AUTOSAR-M17_0_3-a
100// parasoft-end-suppress AUTOSAR-A7_1_5-a
101
102#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_FLOAT_SIGN_HPP_
static constexpr value_type value
Definition integral_constant.hpp:34
Provides macros used to disable and enable compiler specific diagnostics using the _Pragma operator.
#define ARENE_IGNORE_END()
Ends a region of a suppressed diagnostic.
Definition diagnostics.hpp:136
#define ARENE_IGNORE_START()
Begins a region of a suppressed diagnostic.
Definition diagnostics.hpp:131
#define ARENE_IGNORE_ALL(s, comment)
Pushes an ignore for a diagnostic into the compiler for any compiler.
Definition diagnostics.hpp:122
Definition accumulate.hpp:37
constexpr auto copysign(float const magnitude, float const sign) noexcept -> float
Return a number with the magnitude of the first operand and the sign of the second.
Definition float_sign.hpp:28
constexpr auto signbit(float const num) noexcept -> bool
Check the sign bit of a floating point number.
Definition float_sign.hpp:88
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
STL namespace.
bool_constant< false > false_type
An alias for bool_constant<false>
Definition integral_constant.hpp:79
bool_constant< true > true_type
An alias for bool_constant<true>
Definition integral_constant.hpp:76