blob: 25a9353bc088f33e0c9e228daae3aa01cce1e8f3 [file] [log] [blame]
Ian Rogersf72a11d2014-10-30 15:41:08 -07001/*
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
23namespace art {
24namespace interpreter {
25
26// Declares a type which is the larger in bit size of the two template parameters.
27template <typename T1, typename T2>
28struct select_bigger {
Vladimir Marko4f990712021-07-14 12:45:13 +010029 using type = std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>;
Ian Rogersf72a11d2014-10-30 15:41:08 -070030};
Vladimir Marko4f990712021-07-14 12:45:13 +010031template <typename T1, typename T2>
32using select_bigger_t = typename select_bigger<T1, T2>::type;
Ian Rogersf72a11d2014-10-30 15:41:08 -070033
34// Perform signed arithmetic Op on 'a' and 'b' with defined wrapping behavior.
35template<template <typename OpT> class Op, typename T1, typename T2>
Vladimir Marko4f990712021-07-14 12:45:13 +010036static 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 Rogersf72a11d2014-10-30 15:41:08 -070041 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 Boehm9076fbd2018-03-29 10:30:42 -070046// Perform a signed add on 'a' and 'b' with defined wrapping behavior.
Ian Rogersf72a11d2014-10-30 15:41:08 -070047template<typename T1, typename T2>
Vladimir Marko4f990712021-07-14 12:45:13 +010048static inline select_bigger_t<T1, T2> SafeAdd(T1 a, T2 b) {
Ian Rogersf72a11d2014-10-30 15:41:08 -070049 return SafeMath<std::plus>(a, b);
50}
51
Hans Boehm9076fbd2018-03-29 10:30:42 -070052// Perform a signed substract on 'a' and 'b' with defined wrapping behavior.
Ian Rogersf72a11d2014-10-30 15:41:08 -070053template<typename T1, typename T2>
Vladimir Marko4f990712021-07-14 12:45:13 +010054static inline select_bigger_t<T1, T2> SafeSub(T1 a, T2 b) {
Ian Rogersf72a11d2014-10-30 15:41:08 -070055 return SafeMath<std::minus>(a, b);
56}
57
Hans Boehm9076fbd2018-03-29 10:30:42 -070058// Perform a signed multiply on 'a' and 'b' with defined wrapping behavior.
Ian Rogersf72a11d2014-10-30 15:41:08 -070059template<typename T1, typename T2>
Vladimir Marko4f990712021-07-14 12:45:13 +010060static inline select_bigger_t<T1, T2> SafeMul(T1 a, T2 b) {
Ian Rogersf72a11d2014-10-30 15:41:08 -070061 return SafeMath<std::multiplies>(a, b);
62}
63
64} // namespace interpreter
65} // namespace art
66
67#endif // ART_RUNTIME_INTERPRETER_SAFE_MATH_H_