Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
abs.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_ABS_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_ABS_HPP_
7
8// IWYU pragma: private, include "arene/base/math.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11#include "arene/base/constraints/constraints.hpp"
12#include "arene/base/contracts/contract.hpp"
13#include "arene/base/stdlib_choice/enable_if.hpp"
14#include "arene/base/stdlib_choice/is_floating_point.hpp"
15#include "arene/base/stdlib_choice/is_integral.hpp"
16#include "arene/base/stdlib_choice/is_signed.hpp"
17#include "arene/base/stdlib_choice/numeric_limits.hpp"
18
19// parasoft-begin-suppress AUTOSAR-M17_0_3-a-2 "False positive: names are in a different namespace"
20// parasoft-begin-suppress CERT_CPP-DCL51-f-3 "False positive: no reserved names are used"
21
22// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
23#include "arene/base/compiler_support/platform_queries.hpp"
24#include "arene/base/compiler_support/preprocessor.hpp"
25// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
26
27#if ARENE_IS_ON(ARENE_HAS_BUILTIN_FABS) && ARENE_IS_ON(ARENE_HAS_BUILTIN_FABSF)
28#include "arene/base/math/detail/builtin_abs_float.hpp"
29#else
30#include "arene/base/math/detail/custom_abs_float.hpp"
31#endif
32
33namespace arene {
34namespace base {
35
36/// @brief Calculate the absolute value of a number.
37/// @tparam Number The type of the number.
38/// @param value The value to take the absolute value of.
39/// @pre If @c Number is an Integral type, @c value is not the smallest negative number the type can represent.
40/// @return The absolute value of @c value, which is equivalent to <c>return value < 0 ? -value : value;</c>.
41// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
42template <
43 typename Number,
45 nullptr>
46constexpr auto abs(Number value) noexcept -> Number {
47 // Getting the absolute value of the smallest negative number is undefined behavior.
48 constexpr Number zero{static_cast<Number>(0)};
49 if (value < zero) {
51 return static_cast<Number>(-value);
52 }
53 return value;
54}
55// parasoft-end-suppress AUTOSAR-A2_7_3
56
57/// @brief Calculate the absolute value of a number.
58/// @tparam Number The type of the number.
59/// @param value The value to take the absolute value of.
60/// @pre @c value is not the smallest negative number the type can represent.
61/// @return The absolute value of @c value, which is equivalent to <c>return value < 0 ? -value : value;</c>.
62///
63/// @note Care should be taken when calling @c abs with unsigned values. This overload for unsigned values does not
64/// prevent potential logic errors in expressions like: <tt>arene::base::abs(a - b)</tt>, where unsigned
65/// wraparound can lead to unexpected behavior if <tt>a < b</tt>. Ensure that you understand the implications of using
66/// @c abs() with unsigned types and consider using <tt>arene::base::abs_diff(a, b)</tt> instead.
67// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
68template <
69 typename Number,
71 nullptr>
72constexpr auto abs(Number value) noexcept -> Number {
73 return value;
74}
75// parasoft-end-suppress AUTOSAR-A2_7_3
76/// @brief Calculate the absolute value of a number.
77/// @tparam Number The type of the number.
78/// @param value The value to take the absolute value of.
79/// @return The absolute value of @c value, which is equivalent to <c>return value < 0 ? -value : value;</c>.
80// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
81template <typename Number, constraints<std::enable_if_t<(std::is_floating_point<Number>::value)> > = nullptr>
82constexpr auto abs(Number value) noexcept -> Number {
83 return math::detail::abs(value);
84}
85// parasoft-end-suppress AUTOSAR-A2_7_3
86
87} // namespace base
88} // namespace arene
89
90#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_ABS_HPP_
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10