1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #pragma once
18
19 #include <bit>
20 #include <cmath>
21
22 namespace android::audio_utils {
23
24 /*
25 * The compilation option
26 * -ffast-math
27 *
28 * https://gcc.gnu.org/wiki/FloatingPointMath
29 *
30 * enables the following flags:
31 *
32 * -fno-trapping-math
33 * -funsafe-math-optimizations
34 * -ffinite-math-only
35 * -fno-errno-math
36 * -fno-signaling-nans
37 * -fno-rounding-math
38 * -fcx-limited-range
39 * -fno-signed-zeros.
40 *
41 * -ffinite-math-only means isnan() and isinf() detection may not work properly.
42 */
43
44 /**
45 * Returns the unsigned integer layout of a float.
46 */
float_as_unsigned(float f)47 inline constexpr unsigned float_as_unsigned(float f) {
48 // gnu++20 does not include std::bit_cast, so we use the builtin
49 // supported by gnu and clang. That being said, the latest gnu, clang, and msvc
50 // compilers all support std::bit_cast so update when language support is upgraded.
51 // return std::bit_cast<unsigned>(f);
52
53 return __builtin_bit_cast(unsigned, f);
54 }
55
56 /**
57 * Returns true if the float is nan regardless of -ffast-math compilation.
58 */
safe_isnan(float f)59 inline constexpr bool safe_isnan(float f) {
60 // float is signed-magnitude, so shift sign bit out to do nan comparison.
61 // (This works for ILP32 / LP64 as the sign bit is removed by the shift).
62 // https://en.wikipedia.org/wiki/Single-precision_floating-point_format
63 return float_as_unsigned(f) << 1 >= 0xff800001 << 1;
64 }
65
66 /**
67 * Returns true if the float is infinite (pos or neg) regardless of -ffast-math compilation.
68 */
safe_isinf(float f)69 inline constexpr bool safe_isinf(float f) {
70 // float is signed-magnitude, so shift sign bit out to do inf comparison.
71 // (This works for ILP32 / LP64 as the sign bit is removed by the shift).
72 // https://en.wikipedia.org/wiki/Single-precision_floating-point_format
73 return float_as_unsigned(f) << 1 == 0xff800000 << 1;
74 }
75
76 // safe_sub_overflow is used ensure that subtraction occurs in the same native
77 // type with proper 2's complement overflow. Without calling this function, it
78 // is possible, for example, that optimizing compilers may elect to treat 32 bit
79 // subtraction as 64 bit subtraction when storing into a 64 bit destination as
80 // integer overflow is technically undefined.
81 template <typename T, typename U,
82 typename = std::enable_if_t<
83 std::is_same<std::decay_t<T>, std::decay_t<U>>{}>>
84 // ensure arguments are same type (ignoring volatile, which is used in cblk
85 // variables).
safe_sub_overflow(const T & a,const U & b)86 auto safe_sub_overflow(const T& a, const U& b) {
87 std::decay_t<T> result;
88 (void)__builtin_sub_overflow(a, b, &result);
89 // note if __builtin_sub_overflow returns true, an overflow occurred.
90 return result;
91 }
92
93 // similar to safe_sub_overflow but for add operator.
94 template <typename T, typename U,
95 typename = std::enable_if_t<
96 std::is_same<std::decay_t<T>, std::decay_t<U>>{}>>
97 // ensure arguments are same type (ignoring volatile, which is used in cblk
98 // variables).
safe_add_overflow(const T & a,const U & b)99 auto safe_add_overflow(const T& a, const U& b) {
100 std::decay_t<T> result;
101 (void)__builtin_add_overflow(a, b, &result);
102 // note if __builtin_add_overflow returns true, an overflow occurred.
103 return result;
104 }
105
106 // safe_add_sat returns the saturated add result of two values a and b
107 // which are of the same integral type.
108 // compare with std::add_sat in C++26.
109 template <typename T>
110 requires std::is_integral_v<T>
safe_add_sat(T a,T b)111 constexpr auto safe_add_sat(T a, T b) {
112 std::decay_t<T> result;
113 if (__builtin_add_overflow(a, b, &result)) { // returns true on overflow.
114 if constexpr (std::is_signed_v<T>) {
115 // overflow only occurs if a and b have the same sign.
116 result = a >= 0 ? std::numeric_limits<T>::max() : std::numeric_limits<T>::min();
117 } else /* constexpr */ {
118 // on unsigned add, the only add overflow is max.
119 result = std::numeric_limits<T>::max();
120 }
121 }
122 return result;
123 }
124
125 } // namespace android::audio_utils
126