2
3
4
5
6
7
8
11#ifndef _MSTD_OVERFLOW_OPERATIONS_HPP_
12 #define _MSTD_OVERFLOW_OPERATIONS_HPP_
14 #include <mstd/config.hpp>
17_MSTD_WARNING(
"this is only available for c++17 and greater!");
20 #include <mstd/arithmetic_types.hpp>
21 #include <mstd/types.hpp>
27 template<
class N,
class Na,
class Nb, std::enable_if_t<are_arithmetic_v<N, Na, Nb>,
bool> =
true>
41 out =
static_cast<
N>(
a +
b);
48 template<
class N,
class Na,
class Nb, std::enable_if_t<are_arithmetic_v<N, Na, Nb>,
bool> =
true>
62 out =
static_cast<
N>(
a -
b);
69 template<
class N,
class Na,
class Nb, std::enable_if_t<are_arithmetic_v<N, Na, Nb>,
bool> =
true>
75 if (
a != 0 &&
b != 0 &&
a >
limits::
max() /
b) {
return true; }
78 if (
a != 0 &&
b != 0) {
79 if (
a == -1 &&
b ==
static_cast<
Nb>(
limits::
min())) {
return true; }
81 if (
a ==
limits::
min() &&
b ==
static_cast<
Nb>(-1)) {
return true; }
89 out =
static_cast<
N>(
a *
b);
96 template<
class N,
class Na,
class Nb, std::enable_if_t<are_arithmetic_v<N, Na, Nb>,
bool> =
true>
103 if (
a ==
limits::
min() &&
b == -1) {
return true; }
#define _MSTD_HAS_CXX17
Definition config.hpp:45
#define _MSTD_CONSTEXPR17
Definition config.hpp:76
#define _MSTD_HAS_CXX20
Definition config.hpp:52
#define _MSTD_INLINE17
Definition config.hpp:83
#define _MSTD_CONSTEXPR20
Definition config.hpp:84
Definition arithmetic_types.hpp:23
_MSTD_INLINE17 _MSTD_CONSTEXPR20 bool add_overflow(const Na &a, const Nb &b, N &out)
Definition overflow_operations.hpp:29
_MSTD_INLINE17 _MSTD_CONSTEXPR20 bool mul_overflow(const Na &a, const Nb &b, N &out)
Definition overflow_operations.hpp:71
_MSTD_INLINE17 _MSTD_CONSTEXPR20 bool sub_overflow(const Na &a, const Nb &b, N &out)
Definition overflow_operations.hpp:50
_MSTD_INLINE17 _MSTD_CONSTEXPR20 bool div_overflow(const Na &a, const Nb &b, N &out)
Definition overflow_operations.hpp:98