| Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #ifndef ART_RUNTIME_INTERPRETER_SAFE_MATH_H_ |
| 18 | #define ART_RUNTIME_INTERPRETER_SAFE_MATH_H_ |
| 19 | |
| 20 | #include <functional> |
| 21 | #include <type_traits> |
| 22 | |
| 23 | namespace art { |
| 24 | namespace interpreter { |
| 25 | |
| 26 | // Declares a type which is the larger in bit size of the two template parameters. |
| 27 | template <typename T1, typename T2> |
| 28 | struct select_bigger { |
| Vladimir Marko | 4f99071 | 2021-07-14 12:45:13 +0100 | [diff] [blame] | 29 | using type = std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>; |
| Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 30 | }; |
| Vladimir Marko | 4f99071 | 2021-07-14 12:45:13 +0100 | [diff] [blame] | 31 | template <typename T1, typename T2> |
| 32 | using select_bigger_t = typename select_bigger<T1, T2>::type; |
| Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 33 | |
| 34 | // Perform signed arithmetic Op on 'a' and 'b' with defined wrapping behavior. |
| 35 | template<template <typename OpT> class Op, typename T1, typename T2> |
| Vladimir Marko | 4f99071 | 2021-07-14 12:45:13 +0100 | [diff] [blame] | 36 | static inline select_bigger_t<T1, T2> SafeMath(T1 a, T2 b) { |
| 37 | using biggest_T = select_bigger_t<T1, T2>; |
| 38 | using unsigned_biggest_T = std::make_unsigned_t<biggest_T>; |
| 39 | static_assert(std::is_signed_v<T1>, "Expected T1 to be signed"); |
| 40 | static_assert(std::is_signed_v<T2>, "Expected T2 to be signed"); |
| Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 41 | unsigned_biggest_T val1 = static_cast<unsigned_biggest_T>(static_cast<biggest_T>(a)); |
| 42 | unsigned_biggest_T val2 = static_cast<unsigned_biggest_T>(b); |
| 43 | return static_cast<biggest_T>(Op<unsigned_biggest_T>()(val1, val2)); |
| 44 | } |
| 45 | |
| Hans Boehm | 9076fbd | 2018-03-29 10:30:42 -0700 | [diff] [blame] | 46 | // Perform a signed add on 'a' and 'b' with defined wrapping behavior. |
| Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 47 | template<typename T1, typename T2> |
| Vladimir Marko | 4f99071 | 2021-07-14 12:45:13 +0100 | [diff] [blame] | 48 | static inline select_bigger_t<T1, T2> SafeAdd(T1 a, T2 b) { |
| Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 49 | return SafeMath<std::plus>(a, b); |
| 50 | } |
| 51 | |
| Hans Boehm | 9076fbd | 2018-03-29 10:30:42 -0700 | [diff] [blame] | 52 | // Perform a signed substract on 'a' and 'b' with defined wrapping behavior. |
| Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 53 | template<typename T1, typename T2> |
| Vladimir Marko | 4f99071 | 2021-07-14 12:45:13 +0100 | [diff] [blame] | 54 | static inline select_bigger_t<T1, T2> SafeSub(T1 a, T2 b) { |
| Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 55 | return SafeMath<std::minus>(a, b); |
| 56 | } |
| 57 | |
| Hans Boehm | 9076fbd | 2018-03-29 10:30:42 -0700 | [diff] [blame] | 58 | // Perform a signed multiply on 'a' and 'b' with defined wrapping behavior. |
| Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 59 | template<typename T1, typename T2> |
| Vladimir Marko | 4f99071 | 2021-07-14 12:45:13 +0100 | [diff] [blame] | 60 | static inline select_bigger_t<T1, T2> SafeMul(T1 a, T2 b) { |
| Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 61 | return SafeMath<std::multiplies>(a, b); |
| 62 | } |
| 63 | |
| 64 | } // namespace interpreter |
| 65 | } // namespace art |
| 66 | |
| 67 | #endif // ART_RUNTIME_INTERPRETER_SAFE_MATH_H_ |