Graphviz 14.1.4~dev.20260320.0055
Loading...
Searching...
No Matches
overflow.h
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <assert.h>
10#include <limits.h>
11#include <stdbool.h>
12#include <stddef.h>
13#include <stdint.h>
14
22static inline bool sadd_overflow(int a, int b, int *res) {
23 assert(res != NULL);
24
25 // delegate to hardware optimized implementations where possible
26#if defined(__clang__)
27 return __builtin_sadd_overflow(a, b, res);
28#elif defined(__GNUC__) && __GNUC__ > 4 // GCC ≥ 5
29 return __builtin_sadd_overflow(a, b, res);
30#endif
31
32 if (a > 0 && INT_MAX - a < b) {
33 return true;
34 }
35 if (a < 0 && INT_MIN - a > b) {
36 return true;
37 }
38
39 *res = a + b;
40 return false;
41}
42
49static inline bool size_overflow(size_t a, size_t b, size_t *res) {
50 assert(res != NULL);
51
52 // delegate to hardware optimized implementations where possible
53#if defined(__clang__)
54 return __builtin_add_overflow(a, b, res);
55#elif defined(__GNUC__) && __GNUC__ > 4 // GCC ≥ 5
56 return __builtin_add_overflow(a, b, res);
57#endif
58
59 if (SIZE_MAX - a < b) {
60 return true;
61 }
62
63 *res = a + b;
64 return false;
65}
66
73static inline bool u64add_overflow(uint64_t a, uint64_t b, uint64_t *res) {
74 assert(res != NULL);
75
76 // delegate to hardware optimized implementations where possible
77#if defined(__clang__)
78 return __builtin_add_overflow(a, b, res);
79#elif defined(__GNUC__) && __GNUC__ > 4 // GCC ≥ 5
80 return __builtin_add_overflow(a, b, res);
81#endif
82
83 if (UINT64_MAX - a < b) {
84 return true;
85 }
86
87 *res = a + b;
88 return false;
89}
90
97static inline bool u64mul_overflow(uint64_t a, uint64_t b, uint64_t *res) {
98 assert(res != NULL);
99
100 // delegate to hardware optimized implementations where possible
101#if defined(__clang__)
102 return __builtin_mul_overflow(a, b, res);
103#elif defined(__GNUC__) && __GNUC__ > 4 // GCC ≥ 5
104 return __builtin_mul_overflow(a, b, res);
105#endif
106
107 if (a > 0 && UINT64_MAX / a < b) {
108 return true;
109 }
110
111 *res = a * b;
112 return false;
113}
#define SIZE_MAX
Definition gmlscan.c:347
node NULL
Definition grammar.y:181
static bool size_overflow(size_t a, size_t b, size_t *res)
Definition overflow.h:49
static bool sadd_overflow(int a, int b, int *res)
Definition overflow.h:22
static bool u64mul_overflow(uint64_t a, uint64_t b, uint64_t *res)
Definition overflow.h:97
static bool u64add_overflow(uint64_t a, uint64_t b, uint64_t *res)
Definition overflow.h:73