diff options
author | EuAndreh <eu@euandre.org> | 2024-05-24 11:35:20 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-05-24 13:03:56 -0300 |
commit | fef0a487a119a5cbcd2fd95c6a35e0a9c4ab8ac1 (patch) | |
tree | 542e59d44cf82c988e90cc05217cb4b35b22a76d /src | |
parent | Add some version of vector.c (diff) | |
download | pindaiba-fef0a487a119a5cbcd2fd95c6a35e0a9c4ab8ac1.tar.gz pindaiba-fef0a487a119a5cbcd2fd95c6a35e0a9c4ab8ac1.tar.xz |
src/math.c: Add mul_size()
Diffstat (limited to 'src')
-rw-r--r-- | src/math.c | 27 | ||||
-rw-r--r-- | src/math.h | 2 |
2 files changed, 29 insertions, 0 deletions
diff --git a/src/math.c b/src/math.c new file mode 100644 index 0000000..366bd5e --- /dev/null +++ b/src/math.c @@ -0,0 +1,27 @@ +#include "config.h" + +#include <assert.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + +#include "math.h" + + +int +mul_size(const size_t x, const size_t y, size_t *const out) { + int rc = -1; + + assert(x != 0U); + assert(y != 0U); + + const bool overflows = x > (SIZE_MAX / y); + if (overflows) { + goto out; + } + + *out = x * y; + rc = 0; +out: + return rc; +} diff --git a/src/math.h b/src/math.h new file mode 100644 index 0000000..13f1795 --- /dev/null +++ b/src/math.h @@ -0,0 +1,2 @@ +int +mul_size(const size_t x, const size_t y, size_t *const out); |