safe_math.h#
-
namespace onnx_light
Alias that makes onnx-light headers compatible with code that references
ONNX_LIGHT_NAMESPACE(the macro used in the standard onnx package).Set to
ONNX_LIGHT_NAMESPACEso both names resolve to the same namespace.Symbol-visibility attribute for the public onnx-light C++ API.
Maps the upstream compatibility macro to onnx-light’s explicit proto ABI annotation. This keeps declarations from vendored ONNX headers visible when
lib_onnx_protouses hidden visibility by default.Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal
onnxnamespace — rather than theONNX_NAMESPACEmacro — resolves to the onnx-light namespace. The standard onnx package lives innamespace onnx; onnx-light usesonnx_light(via ONNX_LIGHT_NAMESPACE), so this alias keeps onnx-light a true drop-in. It is only introduced when the onnx-light namespace differs fromonnx.Functions
-
inline bool checked_mul_overflow(int64_t a, int64_t b, int64_t *result)#
Multiplies two signed int64 values, reporting overflow.
Uses
__builtin_mul_overflowon GCC/Clang. The MSVC fallback compares against the int64 bounds per sign combination (the classic CERT INT32-C style check, generalized to 64 bits) before multiplying, so it never computes a product that doesn’t fit and never dividesINT64_MINby -1.- Parameters:
a – First operand.
b – Second operand.
result – Receives the product when no overflow occurs.
- Returns:
True on overflow, false otherwise.
-
inline bool checked_add_overflow(int64_t a, int64_t b, int64_t *result)#
Adds two signed int64 values, reporting overflow.
Uses
__builtin_add_overflowon GCC/Clang. The MSVC fallback uses unsigned addition (no UB) and detects overflow via a sign-bit XOR. The cast back toint64_tis implementation-defined in C++17 but gives two’s-complement on every MSVC target; it is mandated by the standard from C++20. Overflow occurs iffaandbhave the same sign but the result has the opposite sign.- Parameters:
a – First operand.
b – Second operand.
result – Receives the sum when no overflow occurs.
- Returns:
True on overflow, false otherwise.
-
inline bool checked_sub_overflow(int64_t a, int64_t b, int64_t *result)#
Subtracts two signed int64 values, reporting overflow.
Uses
__builtin_sub_overflowon GCC/Clang. The MSVC fallback uses unsigned subtraction (no UB) and detects overflow via a sign-bit XOR. Overflow occurs iffaandbhave different signs and the result has a different sign froma.- Parameters:
a – First operand.
b – Second operand.
result – Receives the difference when no overflow occurs.
- Returns:
True on overflow, false otherwise.
-
template<typename Iter, typename ErrorHandler>
inline int64_t safe_dim_product(Iter begin, Iter end, ErrorHandler on_error)# Computes the product of dims over an iterator range with overflow checks.
Calls
on_errorwith aconstchar*message on a negative dim or on overflow.on_errormust not return (i.e. must throw or abort).- Parameters:
begin – Iterator to the first dimension.
end – Iterator past the last dimension.
on_error – Handler invoked on a negative dimension or overflow.
- Returns:
The product of the dimensions in the range.
-
template<typename DimsContainer, typename ErrorHandler>
inline int64_t safe_dim_product(const DimsContainer &dims, ErrorHandler on_error)# Computes the product of dims in a container with overflow checks.
Delegates to the iterator-pair overload of safe_dim_product().
- Parameters:
dims – Container of dimensions.
on_error – Handler invoked on a negative dimension or overflow.
- Returns:
The product of the dimensions in the container.
-
template<typename ErrorHandler>
inline size_t safe_cast_to_size(int64_t value, ErrorHandler on_error)# Casts an int64_t to size_t, reporting values out of range.
Calls
on_errorif the value exceeds the size_t range (relevant for 32-bit platforms where size_t is 32 bits).valuemust be non-negative (callers ensure this via prior overflow checks).- Parameters:
value – Non-negative value to cast.
on_error – Handler invoked when
valueis too large for size_t.
- Returns:
The value cast to size_t.
-
inline bool checked_mul_overflow(int64_t a, int64_t b, int64_t *result)#