ARITHMETIC(3) Library Functions Manual ARITHMETIC(3)

KS_i32_add_overflow, KS_i32_sub_overflow, KS_i32_mul_overflow, KS_i64_add_overflow, KS_i64_sub_overflow, KS_i64_mul_overflow, KS_u32_add_overflow, KS_u32_sub_overflow, KS_u32_mul_overflow, KS_u64_add_overflow, KS_u64_sub_overflow, KS_u64_mul_overflow, KS_size_add_overflow, KS_size_sub_overflow, KS_size_mul_overflowoverflow safe arithmetic functions

#include <libks/arithmetic.h>

int
KS_i32_add_overflow(int32_t a, int32_t b, int32_t *c);

int
KS_i32_sub_overflow(int32_t a, int32_t b, int32_t *c);

int
KS_i32_mul_overflow(int32_t a, int32_t b, int32_t *c);

int
KS_i64_add_overflow(int64_t a, int64_t b, int64_t *c);

int
KS_i64_sub_overflow(int64_t a, int64_t b, int64_t *c);

int
KS_i64_mul_overflow(int64_t a, int64_t b, int64_t *c);

int
KS_u32_add_overflow(uint32_t a, uint32_t b, uint32_t *c);

int
KS_u32_sub_overflow(uint32_t a, uint32_t b, uint32_t *c);

int
KS_u32_mul_overflow(uint32_t a, uint32_t b, uint32_t *c);

int
KS_u64_add_overflow(uint64_t a, uint64_t b, uint64_t *c);

int
KS_u64_sub_overflow(uint64_t a, uint64_t b, uint64_t *c);

int
KS_u64_mul_overflow(uint64_t a, uint64_t b, uint64_t *c);

int
KS_size_add_overflow(size_t a, size_t b, size_t *c);

int
KS_size_sub_overflow(size_t a, size_t b, size_t *c);

int
KS_size_mul_overflow(size_t a, size_t b, size_t *c);

The arithmetic functions allow performing basic arithmetic operations and detect whether such operations would overflow.

The (), (), (), (), and () functions adds b to a and stores the resulting sum in c.

The (), (), (), () and () functions subtracts b from a and stores the resulting sum in c.

The (), (), (), () and () functions multiplies a and b and stores the resulting product in c.

All functions returns non-zero if the corresponding arithmetic operation would overflow and zero otherwise.

Anton Lindqvist <anton@basename.se>

OpenBSD 7.8 February 13, 2023 ARITHMETIC(3)