#pragma once
#include <cstddef>
#include <cstdint>

namespace fetchml::common {

// Overflow-safe arithmetic using compiler builtins
// Returns true on success, false on overflow

static inline bool safe_mul(size_t a, size_t b, size_t* result) {
    return !__builtin_mul_overflow(a, b, result);
}

static inline bool safe_add(size_t a, size_t b, size_t* result) {
    return !__builtin_add_overflow(a, b, result);
}

static inline bool safe_mul_u32(uint32_t a, uint32_t b, uint32_t* result) {
    return !__builtin_mul_overflow(a, b, result);
}

static inline bool safe_add_u32(uint32_t a, uint32_t b, uint32_t* result) {
    return !__builtin_add_overflow(a, b, result);
}

} // namespace fetchml::common
