xref: /aosp_15_r20/external/cronet/base/numerics/wrapping_math.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2023 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef BASE_NUMERICS_WRAPPING_MATH_H_
6*6777b538SAndroid Build Coastguard Worker #define BASE_NUMERICS_WRAPPING_MATH_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <type_traits>
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker namespace base {
11*6777b538SAndroid Build Coastguard Worker 
12*6777b538SAndroid Build Coastguard Worker // Returns `a + b` with overflow defined to wrap around, i.e. modulo 2^N where N
13*6777b538SAndroid Build Coastguard Worker // is the bit width of `T`.
14*6777b538SAndroid Build Coastguard Worker template <typename T>
WrappingAdd(T a,T b)15*6777b538SAndroid Build Coastguard Worker inline constexpr T WrappingAdd(T a, T b) {
16*6777b538SAndroid Build Coastguard Worker   static_assert(std::is_integral_v<T>);
17*6777b538SAndroid Build Coastguard Worker   // Unsigned arithmetic wraps, so convert to the corresponding unsigned type.
18*6777b538SAndroid Build Coastguard Worker   // Note that, if `T` is smaller than `int`, e.g. `int16_t`, the values are
19*6777b538SAndroid Build Coastguard Worker   // promoted to `int`, which brings us back to undefined overflow. This is fine
20*6777b538SAndroid Build Coastguard Worker   // here because the sum of any two `int16_t`s fits in `int`, but `WrappingMul`
21*6777b538SAndroid Build Coastguard Worker   // will need a more complex implementation.
22*6777b538SAndroid Build Coastguard Worker   using Unsigned = std::make_unsigned_t<T>;
23*6777b538SAndroid Build Coastguard Worker   return static_cast<T>(static_cast<Unsigned>(a) + static_cast<Unsigned>(b));
24*6777b538SAndroid Build Coastguard Worker }
25*6777b538SAndroid Build Coastguard Worker 
26*6777b538SAndroid Build Coastguard Worker // Returns `a - b` with overflow defined to wrap around, i.e. modulo 2^N where N
27*6777b538SAndroid Build Coastguard Worker // is the bit width of `T`.
28*6777b538SAndroid Build Coastguard Worker template <typename T>
WrappingSub(T a,T b)29*6777b538SAndroid Build Coastguard Worker inline constexpr T WrappingSub(T a, T b) {
30*6777b538SAndroid Build Coastguard Worker   static_assert(std::is_integral_v<T>);
31*6777b538SAndroid Build Coastguard Worker   // Unsigned arithmetic wraps, so convert to the corresponding unsigned type.
32*6777b538SAndroid Build Coastguard Worker   // Note that, if `T` is smaller than `int`, e.g. `int16_t`, the values are
33*6777b538SAndroid Build Coastguard Worker   // promoted to `int`, which brings us back to undefined overflow. This is fine
34*6777b538SAndroid Build Coastguard Worker   // here because the difference of any two `int16_t`s fits in `int`, but
35*6777b538SAndroid Build Coastguard Worker   // `WrappingMul` will need a more complex implementation.
36*6777b538SAndroid Build Coastguard Worker   using Unsigned = std::make_unsigned_t<T>;
37*6777b538SAndroid Build Coastguard Worker   return static_cast<T>(static_cast<Unsigned>(a) - static_cast<Unsigned>(b));
38*6777b538SAndroid Build Coastguard Worker }
39*6777b538SAndroid Build Coastguard Worker 
40*6777b538SAndroid Build Coastguard Worker }  // namespace base
41*6777b538SAndroid Build Coastguard Worker 
42*6777b538SAndroid Build Coastguard Worker #endif  // BASE_NUMERICS_WRAPPING_MATH_H_
43