Make {add,sub,mul}_will_overflow more compiler-independent
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2022-12-06 15:53:06 +01:00
parent 26b44e651d
commit 1fa99f4f64
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -1,21 +1,6 @@
#pragma once
#include <luna/Result.h>
template <typename T> constexpr bool add_will_overflow(T a, T b)
{
return __builtin_add_overflow_p(a, b, (T)0);
}
template <typename T> constexpr bool sub_will_overflow(T a, T b)
{
return __builtin_sub_overflow_p(a, b, (T)0);
}
template <typename T> constexpr bool mul_will_overflow(T a, T b)
{
return __builtin_mul_overflow_p(a, b, (T)0);
}
template <typename T> Result<T> safe_add(T a, T b)
{
T result;
@ -41,4 +26,31 @@ template <typename T> Result<T> safe_mul(T a, T b)
if (__builtin_mul_overflow(a, b, &result)) return err(EOVERFLOW);
return result;
}
template <typename T> bool add_will_overflow(T a, T b)
{
#ifdef __GNUC__
return __builtin_add_overflow_p(a, b, (T)0);
#else
return safe_add(a, b).has_error();
#endif
}
template <typename T> bool sub_will_overflow(T a, T b)
{
#ifdef __GNUC__
return __builtin_sub_overflow_p(a, b, (T)0);
#else
return safe_sub(a, b).has_error();
#endif
}
template <typename T> bool mul_will_overflow(T a, T b)
{
#ifdef __GNUC__
return __builtin_mul_overflow_p(a, b, (T)0);
#else
return safe_mul(a, b).has_error();
#endif
}