mirror of
https://github.com/opnsense/src.git
synced 2026-02-03 20:49:35 -05:00
This adds macros for checked addition, subtraction, and multiplication with semantics similar to the builtins gcc and clang have had for years. Reviewed by: kib, emaste Differential Revision: https://reviews.freebsd.org/D41734 (cherry picked from commit e6615b10347caf67f5bc12c9a8e30b8ddd9860ae) include: Add tests for N2867. Reviewed by: imp Differential Revision: https://reviews.freebsd.org/D41735 (cherry picked from commit 4fbb9c43aa44d9145151bb5f77d302ba01fb7551) less: We have <stdckdint.h> now. Reviewed by: delphij Differential Revision: https://reviews.freebsd.org/D41736 (cherry picked from commit cb8dd292c7ec53391dfa25847858dd7ef895f94e) Approved by: re (gjb)
40 lines
1 KiB
C
40 lines
1 KiB
C
/*-
|
|
* Copyright (c) 2023 Dag-Erling Smørgrav
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#ifndef __STDC_VERSION_STDCKDINT_H__
|
|
#define __STDC_VERSION_STDCKDINT_H__ 202311L
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#if __BSD_VISIBLE || __ISO_C_VISIBLE >= 2023
|
|
|
|
#if __GNUC_PREREQ__(5, 1) || __has_builtin(__builtin_add_overflow)
|
|
#define ckd_add(result, a, b) \
|
|
(_Bool)__builtin_add_overflow((a), (b), (result))
|
|
#else
|
|
#define ckd_add(result, a, b) \
|
|
_Static_assert(0, "checked addition not supported")
|
|
#endif
|
|
|
|
#if __GNUC_PREREQ__(5, 1) || __has_builtin(__builtin_sub_overflow)
|
|
#define ckd_sub(result, a, b) \
|
|
(_Bool)__builtin_sub_overflow((a), (b), (result))
|
|
#else
|
|
#define ckd_sub(result, a, b) \
|
|
_Static_assert(0, "checked subtraction not supported")
|
|
#endif
|
|
|
|
#if __GNUC_PREREQ__(5, 1) || __has_builtin(__builtin_mul_overflow)
|
|
#define ckd_mul(result, a, b) \
|
|
(_Bool)__builtin_mul_overflow((a), (b), (result))
|
|
#else
|
|
#define ckd_mul(result, a, b) \
|
|
_Static_assert(0, "checked multiplication not supported")
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|