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 namespace android::audio_utils {
18
19 // safe_sub_overflow is used ensure that subtraction occurs in the same native
20 // type with proper 2's complement overflow. Without calling this function, it
21 // is possible, for example, that optimizing compilers may elect to treat 32 bit
22 // subtraction as 64 bit subtraction when storing into a 64 bit destination as
23 // integer overflow is technically undefined.
24 template <typename T, typename U,
25 typename = std::enable_if_t<
26 std::is_same<std::decay_t<T>, std::decay_t<U>>{}>>
27 // ensure arguments are same type (ignoring volatile, which is used in cblk
28 // variables).
safe_sub_overflow(const T & a,const U & b)29 auto safe_sub_overflow(const T& a, const U& b) {
30 std::decay_t<T> result;
31 (void)__builtin_sub_overflow(a, b, &result);
32 // note if __builtin_sub_overflow returns true, an overflow occurred.
33 return result;
34 }
35
36 // similar to safe_sub_overflow but for add operator.
37 template <typename T, typename U,
38 typename = std::enable_if_t<
39 std::is_same<std::decay_t<T>, std::decay_t<U>>{}>>
40 // ensure arguments are same type (ignoring volatile, which is used in cblk
41 // variables).
safe_add_overflow(const T & a,const U & b)42 auto safe_add_overflow(const T& a, const U& b) {
43 std::decay_t<T> result;
44 (void)__builtin_add_overflow(a, b, &result);
45 // note if __builtin_add_overflow returns true, an overflow occurred.
46 return result;
47 }
48
49 } // namespace android::audio_utils
50