checked-integer-arithmetic.h 840 Bytes
Newer Older
1
/* SPDX-License-Identifier: Zlib */
2 3 4 5 6 7
#ifndef ZATHURA_CHECKED_INTEGER_ARITHMETIC_H
#define ZATHURA_CHECKED_INTEGER_ARITHMETIC_H

#include "macros.h"
#include <stdbool.h>

Sebastian Ramacher's avatar
Sebastian Ramacher committed
8
#if __GNUC__ >= 5 || __has_builtin(__builtin_add_overflow)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#define HAVE_BUILTIN
#endif

#ifdef __cplusplus
extern "C" {
#endif

#ifdef HAVE_BUILTIN
#define checked_umul(lhs, rhs, res) __builtin_umul_overflow((lhs), (rhs), (res))
#else
/**
 * Helper function for multiplication with overflow detection. This function has
 * the same semantics as the __builtin_*mul_overflow functions.
 *
 * @param[in] lhs first operand
 * @param[in] rhs second operand
 * @param[out] res result
 * @return true if an overflow occurred, false otherwise
 */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
28
GIRARA_HIDDEN bool checked_umul(unsigned int lhs, unsigned int rhs, unsigned int* res);
29 30 31 32 33 34 35
#endif

#ifdef __cplusplus
}
#endif

#endif