|
| 1 | +// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- |
| 2 | +// |
| 3 | +// safe_math.h: Rcpp R/C++ interface class library -- |
| 4 | +// |
| 5 | +// Copyright (C) 2026 Iñaki Ucar |
| 6 | +// |
| 7 | +// This file is part of Rcpp. |
| 8 | +// |
| 9 | +// Rcpp is free software: you can redistribute it and/or modify it |
| 10 | +// under the terms of the GNU General Public License as published by |
| 11 | +// the Free Software Foundation, either version 2 of the License, or |
| 12 | +// (at your option) any later version. |
| 13 | +// |
| 14 | +// Rcpp is distributed in the hope that it will be useful, but |
| 15 | +// WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +// GNU General Public License for more details. |
| 18 | +// |
| 19 | +// You should have received a copy of the GNU General Public License |
| 20 | +// along with Rcpp. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + |
| 22 | +#ifndef Rcpp__sugar__tools_safe_math_h |
| 23 | +#define Rcpp__sugar__tools_safe_math_h |
| 24 | + |
| 25 | +#ifndef safe_math__has_builtin |
| 26 | +# ifdef __has_builtin |
| 27 | +# define safe_math__has_builtin(x) __has_builtin(x) |
| 28 | +# else |
| 29 | +# define safe_math__has_builtin(x) 0 |
| 30 | +# endif |
| 31 | +#endif |
| 32 | + |
| 33 | +#define RCPP_SAFE_ADD(a, b) Rcpp::sugar::safe_add(a, b, __func__) |
| 34 | +#define RCPP_SAFE_SUB(a, b) Rcpp::sugar::safe_sub(a, b, __func__) |
| 35 | +#define RCPP_SAFE_MUL(a, b) Rcpp::sugar::safe_mul(a, b, __func__) |
| 36 | + |
| 37 | +namespace Rcpp { |
| 38 | +namespace sugar { |
| 39 | + |
| 40 | + inline void stop_overflow(const char* caller) { |
| 41 | + if (caller) |
| 42 | + Rcpp::stop("[%s] Integer overflow!", caller); |
| 43 | + Rcpp::stop("Integer overflow!"); |
| 44 | + } |
| 45 | + |
| 46 | + // Addition |
| 47 | + template <typename T> |
| 48 | + inline typename std::enable_if<std::is_integral<T>::value, T>::type |
| 49 | + safe_add(T a, T b, const char* caller = nullptr) { |
| 50 | + #if safe_math__has_builtin(__builtin_add_overflow) |
| 51 | + T result; |
| 52 | + if (__builtin_add_overflow(a, b, &result)) |
| 53 | + stop_overflow(caller); |
| 54 | + return result; |
| 55 | + #else // fallback |
| 56 | + if (std::is_signed<T>::value) { |
| 57 | + if ((b > 0 && a > std::numeric_limits<T>::max() - b) || |
| 58 | + (b < 0 && a < std::numeric_limits<T>::min() - b)) |
| 59 | + stop_overflow(caller); |
| 60 | + } else { |
| 61 | + if (a > std::numeric_limits<T>::max() - b) |
| 62 | + stop_overflow(caller); |
| 63 | + } |
| 64 | + return a + b; |
| 65 | + #endif |
| 66 | + } |
| 67 | + |
| 68 | + template <typename T> |
| 69 | + inline typename std::enable_if<!std::is_integral<T>::value, T>::type |
| 70 | + safe_add(T a, T b, const char* caller = nullptr) { return a + b; } |
| 71 | + |
| 72 | + // Subtraction |
| 73 | + template <typename T> |
| 74 | + inline typename std::enable_if<std::is_integral<T>::value, T>::type |
| 75 | + safe_sub(T a, T b, const char* caller = nullptr) { |
| 76 | + #if safe_math__has_builtin(__builtin_sub_overflow) |
| 77 | + T result; |
| 78 | + if (__builtin_sub_overflow(a, b, &result)) |
| 79 | + stop_overflow(caller); |
| 80 | + return result; |
| 81 | + #else // fallback |
| 82 | + if (std::is_signed<T>::value) { |
| 83 | + if ((b < 0 && a > std::numeric_limits<T>::max() + b) || |
| 84 | + (b > 0 && a < std::numeric_limits<T>::min() + b)) |
| 85 | + stop_overflow(caller); |
| 86 | + } else { |
| 87 | + if (a < b) |
| 88 | + stop_overflow(caller); |
| 89 | + } |
| 90 | + return a - b; |
| 91 | + #endif |
| 92 | + } |
| 93 | + |
| 94 | + template <typename T> |
| 95 | + inline typename std::enable_if<!std::is_integral<T>::value, T>::type |
| 96 | + safe_sub(T a, T b, const char* caller = nullptr) { return a - b; } |
| 97 | + |
| 98 | + // Multiplication |
| 99 | + template <typename T> |
| 100 | + inline typename std::enable_if<std::is_integral<T>::value, T>::type |
| 101 | + safe_mul(T a, T b, const char* caller = nullptr) { |
| 102 | + #if safe_math__has_builtin(__builtin_mul_overflow) |
| 103 | + T result; |
| 104 | + if (__builtin_mul_overflow(a, b, &result)) |
| 105 | + stop_overflow(caller); |
| 106 | + return result; |
| 107 | + #else // fallback |
| 108 | + if (a == 0 || b == 0) return 0; |
| 109 | + if (std::is_signed<T>::value) { |
| 110 | + if ((a > 0 && b > 0 && a > std::numeric_limits<T>::max() / b) || |
| 111 | + (a > 0 && b < 0 && b < std::numeric_limits<T>::min() / a) || |
| 112 | + (a < 0 && b > 0 && a < std::numeric_limits<T>::min() / b) || |
| 113 | + (a < 0 && b < 0 && a < std::numeric_limits<T>::max() / b)) |
| 114 | + stop_overflow(caller); |
| 115 | + } else { |
| 116 | + if (b > 0 && a > std::numeric_limits<T>::max() / b) |
| 117 | + stop_overflow(caller); |
| 118 | + } |
| 119 | + return a * b; |
| 120 | + #endif |
| 121 | + } |
| 122 | + |
| 123 | + template <typename T> |
| 124 | + inline typename std::enable_if<!std::is_integral<T>::value, T>::type |
| 125 | + safe_mul(T a, T b, const char* caller = nullptr) { return a * b; } |
| 126 | + |
| 127 | +} // namespace sugar |
| 128 | +} // namespace Rcpp |
| 129 | + |
| 130 | +#undef safe_math__has_builtin |
| 131 | + |
| 132 | +#endif |
0 commit comments