/Users/deen/code/yugabyte-db/src/postgres/src/include/common/int.h
Line | Count | Source (jump to first uncovered line) |
1 | | /*------------------------------------------------------------------------- |
2 | | * |
3 | | * int.h |
4 | | * Routines to perform integer math, while checking for overflows. |
5 | | * |
6 | | * The routines in this file are intended to be well defined C, without |
7 | | * relying on compiler flags like -fwrapv. |
8 | | * |
9 | | * To reduce the overhead of these routines try to use compiler intrinsics |
10 | | * where available. That's not that important for the 16, 32 bit cases, but |
11 | | * the 64 bit cases can be considerably faster with intrinsics. In case no |
12 | | * intrinsics are available 128 bit math is used where available. |
13 | | * |
14 | | * Copyright (c) 2017-2018, PostgreSQL Global Development Group |
15 | | * |
16 | | * src/include/common/int.h |
17 | | * |
18 | | *------------------------------------------------------------------------- |
19 | | */ |
20 | | #ifndef COMMON_INT_H |
21 | | #define COMMON_INT_H |
22 | | |
23 | | /* |
24 | | * If a + b overflows, return true, otherwise store the result of a + b into |
25 | | * *result. The content of *result is implementation defined in case of |
26 | | * overflow. |
27 | | */ |
28 | | static inline bool |
29 | | pg_add_s16_overflow(int16 a, int16 b, int16 *result) |
30 | 0 | { |
31 | 0 | #if defined(HAVE__BUILTIN_OP_OVERFLOW) |
32 | 0 | return __builtin_add_overflow(a, b, result); |
33 | | #else |
34 | | int32 res = (int32) a + (int32) b; |
35 | | |
36 | | if (res > PG_INT16_MAX || res < PG_INT16_MIN) |
37 | | { |
38 | | *result = 0x5EED; /* to avoid spurious warnings */ |
39 | | return true; |
40 | | } |
41 | | *result = (int16) res; |
42 | | return false; |
43 | | #endif |
44 | 0 | } Unexecuted instantiation: array_userfuncs.c:pg_add_s16_overflow Unexecuted instantiation: arrayutils.c:pg_add_s16_overflow Unexecuted instantiation: cash.c:pg_add_s16_overflow Unexecuted instantiation: float.c:pg_add_s16_overflow Unexecuted instantiation: int.c:pg_add_s16_overflow Unexecuted instantiation: int8.c:pg_add_s16_overflow Unexecuted instantiation: numeric.c:pg_add_s16_overflow Unexecuted instantiation: oracle_compat.c:pg_add_s16_overflow Unexecuted instantiation: varbit.c:pg_add_s16_overflow Unexecuted instantiation: varlena.c:pg_add_s16_overflow Unexecuted instantiation: ybgate_api.c:pg_add_s16_overflow |
45 | | |
46 | | /* |
47 | | * If a - b overflows, return true, otherwise store the result of a - b into |
48 | | * *result. The content of *result is implementation defined in case of |
49 | | * overflow. |
50 | | */ |
51 | | static inline bool |
52 | | pg_sub_s16_overflow(int16 a, int16 b, int16 *result) |
53 | 0 | { |
54 | 0 | #if defined(HAVE__BUILTIN_OP_OVERFLOW) |
55 | 0 | return __builtin_sub_overflow(a, b, result); |
56 | | #else |
57 | | int32 res = (int32) a - (int32) b; |
58 | | |
59 | | if (res > PG_INT16_MAX || res < PG_INT16_MIN) |
60 | | { |
61 | | *result = 0x5EED; /* to avoid spurious warnings */ |
62 | | return true; |
63 | | } |
64 | | *result = (int16) res; |
65 | | return false; |
66 | | #endif |
67 | 0 | } Unexecuted instantiation: array_userfuncs.c:pg_sub_s16_overflow Unexecuted instantiation: arrayutils.c:pg_sub_s16_overflow Unexecuted instantiation: cash.c:pg_sub_s16_overflow Unexecuted instantiation: float.c:pg_sub_s16_overflow Unexecuted instantiation: int.c:pg_sub_s16_overflow Unexecuted instantiation: int8.c:pg_sub_s16_overflow Unexecuted instantiation: numeric.c:pg_sub_s16_overflow Unexecuted instantiation: oracle_compat.c:pg_sub_s16_overflow Unexecuted instantiation: varbit.c:pg_sub_s16_overflow Unexecuted instantiation: varlena.c:pg_sub_s16_overflow Unexecuted instantiation: ybgate_api.c:pg_sub_s16_overflow |
68 | | |
69 | | /* |
70 | | * If a * b overflows, return true, otherwise store the result of a * b into |
71 | | * *result. The content of *result is implementation defined in case of |
72 | | * overflow. |
73 | | */ |
74 | | static inline bool |
75 | | pg_mul_s16_overflow(int16 a, int16 b, int16 *result) |
76 | 1 | { |
77 | 1 | #if defined(HAVE__BUILTIN_OP_OVERFLOW) |
78 | 1 | return __builtin_mul_overflow(a, b, result); |
79 | | #else |
80 | | int32 res = (int32) a * (int32) b; |
81 | | |
82 | | if (res > PG_INT16_MAX || res < PG_INT16_MIN) |
83 | | { |
84 | | *result = 0x5EED; /* to avoid spurious warnings */ |
85 | | return true; |
86 | | } |
87 | | *result = (int16) res; |
88 | | return false; |
89 | | #endif |
90 | 1 | } Unexecuted instantiation: array_userfuncs.c:pg_mul_s16_overflow Unexecuted instantiation: arrayutils.c:pg_mul_s16_overflow Unexecuted instantiation: cash.c:pg_mul_s16_overflow Unexecuted instantiation: float.c:pg_mul_s16_overflow int.c:pg_mul_s16_overflow Line | Count | Source | 76 | 1 | { | 77 | 1 | #if defined(HAVE__BUILTIN_OP_OVERFLOW) | 78 | 1 | return __builtin_mul_overflow(a, b, result); | 79 | | #else | 80 | | int32 res = (int32) a * (int32) b; | 81 | | | 82 | | if (res > PG_INT16_MAX || res < PG_INT16_MIN) | 83 | | { | 84 | | *result = 0x5EED; /* to avoid spurious warnings */ | 85 | | return true; | 86 | | } | 87 | | *result = (int16) res; | 88 | | return false; | 89 | | #endif | 90 | 1 | } |
Unexecuted instantiation: int8.c:pg_mul_s16_overflow Unexecuted instantiation: numeric.c:pg_mul_s16_overflow Unexecuted instantiation: oracle_compat.c:pg_mul_s16_overflow Unexecuted instantiation: varbit.c:pg_mul_s16_overflow Unexecuted instantiation: varlena.c:pg_mul_s16_overflow Unexecuted instantiation: ybgate_api.c:pg_mul_s16_overflow |
91 | | |
92 | | /* |
93 | | * If a + b overflows, return true, otherwise store the result of a + b into |
94 | | * *result. The content of *result is implementation defined in case of |
95 | | * overflow. |
96 | | */ |
97 | | static inline bool |
98 | | pg_add_s32_overflow(int32 a, int32 b, int32 *result) |
99 | 1.85M | { |
100 | 1.85M | #if defined(HAVE__BUILTIN_OP_OVERFLOW) |
101 | 1.85M | return __builtin_add_overflow(a, b, result); |
102 | | #else |
103 | | int64 res = (int64) a + (int64) b; |
104 | | |
105 | | if (res > PG_INT32_MAX || res < PG_INT32_MIN) |
106 | | { |
107 | | *result = 0x5EED; /* to avoid spurious warnings */ |
108 | | return true; |
109 | | } |
110 | | *result = (int32) res; |
111 | | return false; |
112 | | #endif |
113 | 1.85M | } array_userfuncs.c:pg_add_s32_overflow Line | Count | Source | 99 | 52 | { | 100 | 52 | #if defined(HAVE__BUILTIN_OP_OVERFLOW) | 101 | 52 | return __builtin_add_overflow(a, b, result); | 102 | | #else | 103 | | int64 res = (int64) a + (int64) b; | 104 | | | 105 | | if (res > PG_INT32_MAX || res < PG_INT32_MIN) | 106 | | { | 107 | | *result = 0x5EED; /* to avoid spurious warnings */ | 108 | | return true; | 109 | | } | 110 | | *result = (int32) res; | 111 | | return false; | 112 | | #endif | 113 | 52 | } |
arrayutils.c:pg_add_s32_overflow Line | Count | Source | 99 | 51.6k | { | 100 | 51.6k | #if defined(HAVE__BUILTIN_OP_OVERFLOW) | 101 | 51.6k | return __builtin_add_overflow(a, b, result); | 102 | | #else | 103 | | int64 res = (int64) a + (int64) b; | 104 | | | 105 | | if (res > PG_INT32_MAX || res < PG_INT32_MIN) | 106 | | { | 107 | | *result = 0x5EED; /* to avoid spurious warnings */ | 108 | | return true; | 109 | | } | 110 | | *result = (int32) res; | 111 | | return false; | 112 | | #endif | 113 | 51.6k | } |
Unexecuted instantiation: cash.c:pg_add_s32_overflow Unexecuted instantiation: float.c:pg_add_s32_overflow int.c:pg_add_s32_overflow Line | Count | Source | 99 | 1.79M | { | 100 | 1.79M | #if defined(HAVE__BUILTIN_OP_OVERFLOW) | 101 | 1.79M | return __builtin_add_overflow(a, b, result); | 102 | | #else | 103 | | int64 res = (int64) a + (int64) b; | 104 | | | 105 | | if (res > PG_INT32_MAX || res < PG_INT32_MIN) | 106 | | { | 107 | | *result = 0x5EED; /* to avoid spurious warnings */ | 108 | | return true; | 109 | | } | 110 | | *result = (int32) res; | 111 | | return false; | 112 | | #endif | 113 | 1.79M | } |
Unexecuted instantiation: int8.c:pg_add_s32_overflow Unexecuted instantiation: numeric.c:pg_add_s32_overflow oracle_compat.c:pg_add_s32_overflow Line | Count | Source | 99 | 2 | { | 100 | 2 | #if defined(HAVE__BUILTIN_OP_OVERFLOW) | 101 | 2 | return __builtin_add_overflow(a, b, result); | 102 | | #else | 103 | | int64 res = (int64) a + (int64) b; | 104 | | | 105 | | if (res > PG_INT32_MAX || res < PG_INT32_MIN) | 106 | | { | 107 | | *result = 0x5EED; /* to avoid spurious warnings */ | 108 | | return true; | 109 | | } | 110 | | *result = (int32) res; | 111 | | return false; | 112 | | #endif | 113 | 2 | } |
Unexecuted instantiation: varbit.c:pg_add_s32_overflow Unexecuted instantiation: varlena.c:pg_add_s32_overflow Unexecuted instantiation: ybgate_api.c:pg_add_s32_overflow |
114 | | |
115 | | /* |
116 | | * If a - b overflows, return true, otherwise store the result of a - b into |
117 | | * *result. The content of *result is implementation defined in case of |
118 | | * overflow. |
119 | | */ |
120 | | static inline bool |
121 | | pg_sub_s32_overflow(int32 a, int32 b, int32 *result) |
122 | 20.1k | { |
123 | 20.1k | #if defined(HAVE__BUILTIN_OP_OVERFLOW) |
124 | 20.1k | return __builtin_sub_overflow(a, b, result); |
125 | | #else |
126 | | int64 res = (int64) a - (int64) b; |
127 | | |
128 | | if (res > PG_INT32_MAX || res < PG_INT32_MIN) |
129 | | { |
130 | | *result = 0x5EED; /* to avoid spurious warnings */ |
131 | | return true; |
132 | | } |
133 | | *result = (int32) res; |
134 | | return false; |
135 | | #endif |
136 | 20.1k | } array_userfuncs.c:pg_sub_s32_overflow Line | Count | Source | 122 | 3 | { | 123 | 3 | #if defined(HAVE__BUILTIN_OP_OVERFLOW) | 124 | 3 | return __builtin_sub_overflow(a, b, result); | 125 | | #else | 126 | | int64 res = (int64) a - (int64) b; | 127 | | | 128 | | if (res > PG_INT32_MAX || res < PG_INT32_MIN) | 129 | | { | 130 | | *result = 0x5EED; /* to avoid spurious warnings */ | 131 | | return true; | 132 | | } | 133 | | *result = (int32) res; | 134 | | return false; | 135 | | #endif | 136 | 3 | } |
Unexecuted instantiation: arrayutils.c:pg_sub_s32_overflow Unexecuted instantiation: cash.c:pg_sub_s32_overflow Unexecuted instantiation: float.c:pg_sub_s32_overflow int.c:pg_sub_s32_overflow Line | Count | Source | 122 | 20.1k | { | 123 | 20.1k | #if defined(HAVE__BUILTIN_OP_OVERFLOW) | 124 | 20.1k | return __builtin_sub_overflow(a, b, result); | 125 | | #else | 126 | | int64 res = (int64) a - (int64) b; | 127 | | | 128 | | if (res > PG_INT32_MAX || res < PG_INT32_MIN) | 129 | | { | 130 | | *result = 0x5EED; /* to avoid spurious warnings */ | 131 | | return true; | 132 | | } | 133 | | *result = (int32) res; | 134 | | return false; | 135 | | #endif | 136 | 20.1k | } |
Unexecuted instantiation: int8.c:pg_sub_s32_overflow Unexecuted instantiation: numeric.c:pg_sub_s32_overflow Unexecuted instantiation: oracle_compat.c:pg_sub_s32_overflow Unexecuted instantiation: varbit.c:pg_sub_s32_overflow Unexecuted instantiation: varlena.c:pg_sub_s32_overflow Unexecuted instantiation: ybgate_api.c:pg_sub_s32_overflow |
137 | | |
138 | | /* |
139 | | * If a * b overflows, return true, otherwise store the result of a * b into |
140 | | * *result. The content of *result is implementation defined in case of |
141 | | * overflow. |
142 | | */ |
143 | | static inline bool |
144 | | pg_mul_s32_overflow(int32 a, int32 b, int32 *result) |
145 | 167k | { |
146 | 167k | #if defined(HAVE__BUILTIN_OP_OVERFLOW) |
147 | 167k | return __builtin_mul_overflow(a, b, result); |
148 | | #else |
149 | | int64 res = (int64) a * (int64) b; |
150 | | |
151 | | if (res > PG_INT32_MAX || res < PG_INT32_MIN) |
152 | | { |
153 | | *result = 0x5EED; /* to avoid spurious warnings */ |
154 | | return true; |
155 | | } |
156 | | *result = (int32) res; |
157 | | return false; |
158 | | #endif |
159 | 167k | } Unexecuted instantiation: array_userfuncs.c:pg_mul_s32_overflow Unexecuted instantiation: arrayutils.c:pg_mul_s32_overflow Unexecuted instantiation: cash.c:pg_mul_s32_overflow Unexecuted instantiation: float.c:pg_mul_s32_overflow int.c:pg_mul_s32_overflow Line | Count | Source | 145 | 167k | { | 146 | 167k | #if defined(HAVE__BUILTIN_OP_OVERFLOW) | 147 | 167k | return __builtin_mul_overflow(a, b, result); | 148 | | #else | 149 | | int64 res = (int64) a * (int64) b; | 150 | | | 151 | | if (res > PG_INT32_MAX || res < PG_INT32_MIN) | 152 | | { | 153 | | *result = 0x5EED; /* to avoid spurious warnings */ | 154 | | return true; | 155 | | } | 156 | | *result = (int32) res; | 157 | | return false; | 158 | | #endif | 159 | 167k | } |
Unexecuted instantiation: int8.c:pg_mul_s32_overflow Unexecuted instantiation: numeric.c:pg_mul_s32_overflow oracle_compat.c:pg_mul_s32_overflow Line | Count | Source | 145 | 2 | { | 146 | 2 | #if defined(HAVE__BUILTIN_OP_OVERFLOW) | 147 | 2 | return __builtin_mul_overflow(a, b, result); | 148 | | #else | 149 | | int64 res = (int64) a * (int64) b; | 150 | | | 151 | | if (res > PG_INT32_MAX || res < PG_INT32_MIN) | 152 | | { | 153 | | *result = 0x5EED; /* to avoid spurious warnings */ | 154 | | return true; | 155 | | } | 156 | | *result = (int32) res; | 157 | | return false; | 158 | | #endif | 159 | 2 | } |
Unexecuted instantiation: varbit.c:pg_mul_s32_overflow Unexecuted instantiation: varlena.c:pg_mul_s32_overflow Unexecuted instantiation: ybgate_api.c:pg_mul_s32_overflow |
160 | | |
161 | | /* |
162 | | * If a + b overflows, return true, otherwise store the result of a + b into |
163 | | * *result. The content of *result is implementation defined in case of |
164 | | * overflow. |
165 | | */ |
166 | | static inline bool |
167 | | pg_add_s64_overflow(int64 a, int64 b, int64 *result) |
168 | 379k | { |
169 | 379k | #if defined(HAVE__BUILTIN_OP_OVERFLOW) |
170 | 379k | return __builtin_add_overflow(a, b, result); |
171 | | #elif defined(HAVE_INT128) |
172 | | int128 res = (int128) a + (int128) b; |
173 | | |
174 | | if (res > PG_INT64_MAX || res < PG_INT64_MIN) |
175 | | { |
176 | | *result = 0x5EED; /* to avoid spurious warnings */ |
177 | | return true; |
178 | | } |
179 | | *result = (int64) res; |
180 | | return false; |
181 | | #else |
182 | | if ((a > 0 && b > 0 && a > PG_INT64_MAX - b) || |
183 | | (a < 0 && b < 0 && a < PG_INT64_MIN - b)) |
184 | | { |
185 | | *result = 0x5EED; /* to avoid spurious warnings */ |
186 | | return true; |
187 | | } |
188 | | *result = a + b; |
189 | | return false; |
190 | | #endif |
191 | 379k | } Unexecuted instantiation: array_userfuncs.c:pg_add_s64_overflow Unexecuted instantiation: arrayutils.c:pg_add_s64_overflow Unexecuted instantiation: cash.c:pg_add_s64_overflow Unexecuted instantiation: float.c:pg_add_s64_overflow Unexecuted instantiation: int.c:pg_add_s64_overflow int8.c:pg_add_s64_overflow Line | Count | Source | 168 | 379k | { | 169 | 379k | #if defined(HAVE__BUILTIN_OP_OVERFLOW) | 170 | 379k | return __builtin_add_overflow(a, b, result); | 171 | | #elif defined(HAVE_INT128) | 172 | | int128 res = (int128) a + (int128) b; | 173 | | | 174 | | if (res > PG_INT64_MAX || res < PG_INT64_MIN) | 175 | | { | 176 | | *result = 0x5EED; /* to avoid spurious warnings */ | 177 | | return true; | 178 | | } | 179 | | *result = (int64) res; | 180 | | return false; | 181 | | #else | 182 | | if ((a > 0 && b > 0 && a > PG_INT64_MAX - b) || | 183 | | (a < 0 && b < 0 && a < PG_INT64_MIN - b)) | 184 | | { | 185 | | *result = 0x5EED; /* to avoid spurious warnings */ | 186 | | return true; | 187 | | } | 188 | | *result = a + b; | 189 | | return false; | 190 | | #endif | 191 | 379k | } |
Unexecuted instantiation: numeric.c:pg_add_s64_overflow Unexecuted instantiation: oracle_compat.c:pg_add_s64_overflow Unexecuted instantiation: varbit.c:pg_add_s64_overflow Unexecuted instantiation: varlena.c:pg_add_s64_overflow Unexecuted instantiation: ybgate_api.c:pg_add_s64_overflow |
192 | | |
193 | | /* |
194 | | * If a - b overflows, return true, otherwise store the result of a - b into |
195 | | * *result. The content of *result is implementation defined in case of |
196 | | * overflow. |
197 | | */ |
198 | | static inline bool |
199 | | pg_sub_s64_overflow(int64 a, int64 b, int64 *result) |
200 | 97.9k | { |
201 | 97.9k | #if defined(HAVE__BUILTIN_OP_OVERFLOW) |
202 | 97.9k | return __builtin_sub_overflow(a, b, result); |
203 | | #elif defined(HAVE_INT128) |
204 | | int128 res = (int128) a - (int128) b; |
205 | | |
206 | | if (res > PG_INT64_MAX || res < PG_INT64_MIN) |
207 | | { |
208 | | *result = 0x5EED; /* to avoid spurious warnings */ |
209 | | return true; |
210 | | } |
211 | | *result = (int64) res; |
212 | | return false; |
213 | | #else |
214 | | if ((a < 0 && b > 0 && a < PG_INT64_MIN + b) || |
215 | | (a > 0 && b < 0 && a > PG_INT64_MAX + b)) |
216 | | { |
217 | | *result = 0x5EED; /* to avoid spurious warnings */ |
218 | | return true; |
219 | | } |
220 | | *result = a - b; |
221 | | return false; |
222 | | #endif |
223 | 97.9k | } Unexecuted instantiation: array_userfuncs.c:pg_sub_s64_overflow Unexecuted instantiation: arrayutils.c:pg_sub_s64_overflow cash.c:pg_sub_s64_overflow Line | Count | Source | 200 | 58 | { | 201 | 58 | #if defined(HAVE__BUILTIN_OP_OVERFLOW) | 202 | 58 | return __builtin_sub_overflow(a, b, result); | 203 | | #elif defined(HAVE_INT128) | 204 | | int128 res = (int128) a - (int128) b; | 205 | | | 206 | | if (res > PG_INT64_MAX || res < PG_INT64_MIN) | 207 | | { | 208 | | *result = 0x5EED; /* to avoid spurious warnings */ | 209 | | return true; | 210 | | } | 211 | | *result = (int64) res; | 212 | | return false; | 213 | | #else | 214 | | if ((a < 0 && b > 0 && a < PG_INT64_MIN + b) || | 215 | | (a > 0 && b < 0 && a > PG_INT64_MAX + b)) | 216 | | { | 217 | | *result = 0x5EED; /* to avoid spurious warnings */ | 218 | | return true; | 219 | | } | 220 | | *result = a - b; | 221 | | return false; | 222 | | #endif | 223 | 58 | } |
Unexecuted instantiation: float.c:pg_sub_s64_overflow Unexecuted instantiation: int.c:pg_sub_s64_overflow int8.c:pg_sub_s64_overflow Line | Count | Source | 200 | 97.8k | { | 201 | 97.8k | #if defined(HAVE__BUILTIN_OP_OVERFLOW) | 202 | 97.8k | return __builtin_sub_overflow(a, b, result); | 203 | | #elif defined(HAVE_INT128) | 204 | | int128 res = (int128) a - (int128) b; | 205 | | | 206 | | if (res > PG_INT64_MAX || res < PG_INT64_MIN) | 207 | | { | 208 | | *result = 0x5EED; /* to avoid spurious warnings */ | 209 | | return true; | 210 | | } | 211 | | *result = (int64) res; | 212 | | return false; | 213 | | #else | 214 | | if ((a < 0 && b > 0 && a < PG_INT64_MIN + b) || | 215 | | (a > 0 && b < 0 && a > PG_INT64_MAX + b)) | 216 | | { | 217 | | *result = 0x5EED; /* to avoid spurious warnings */ | 218 | | return true; | 219 | | } | 220 | | *result = a - b; | 221 | | return false; | 222 | | #endif | 223 | 97.8k | } |
numeric.c:pg_sub_s64_overflow Line | Count | Source | 200 | 1 | { | 201 | 1 | #if defined(HAVE__BUILTIN_OP_OVERFLOW) | 202 | 1 | return __builtin_sub_overflow(a, b, result); | 203 | | #elif defined(HAVE_INT128) | 204 | | int128 res = (int128) a - (int128) b; | 205 | | | 206 | | if (res > PG_INT64_MAX || res < PG_INT64_MIN) | 207 | | { | 208 | | *result = 0x5EED; /* to avoid spurious warnings */ | 209 | | return true; | 210 | | } | 211 | | *result = (int64) res; | 212 | | return false; | 213 | | #else | 214 | | if ((a < 0 && b > 0 && a < PG_INT64_MIN + b) || | 215 | | (a > 0 && b < 0 && a > PG_INT64_MAX + b)) | 216 | | { | 217 | | *result = 0x5EED; /* to avoid spurious warnings */ | 218 | | return true; | 219 | | } | 220 | | *result = a - b; | 221 | | return false; | 222 | | #endif | 223 | 1 | } |
Unexecuted instantiation: oracle_compat.c:pg_sub_s64_overflow Unexecuted instantiation: varbit.c:pg_sub_s64_overflow Unexecuted instantiation: varlena.c:pg_sub_s64_overflow Unexecuted instantiation: ybgate_api.c:pg_sub_s64_overflow |
224 | | |
225 | | /* |
226 | | * If a * b overflows, return true, otherwise store the result of a * b into |
227 | | * *result. The content of *result is implementation defined in case of |
228 | | * overflow. |
229 | | */ |
230 | | static inline bool |
231 | | pg_mul_s64_overflow(int64 a, int64 b, int64 *result) |
232 | 98.0k | { |
233 | 98.0k | #if defined(HAVE__BUILTIN_OP_OVERFLOW) |
234 | 98.0k | return __builtin_mul_overflow(a, b, result); |
235 | | #elif defined(HAVE_INT128) |
236 | | int128 res = (int128) a * (int128) b; |
237 | | |
238 | | if (res > PG_INT64_MAX || res < PG_INT64_MIN) |
239 | | { |
240 | | *result = 0x5EED; /* to avoid spurious warnings */ |
241 | | return true; |
242 | | } |
243 | | *result = (int64) res; |
244 | | return false; |
245 | | #else |
246 | | /* |
247 | | * Overflow can only happen if at least one value is outside the range |
248 | | * sqrt(min)..sqrt(max) so check that first as the division can be quite a |
249 | | * bit more expensive than the multiplication. |
250 | | * |
251 | | * Multiplying by 0 or 1 can't overflow of course and checking for 0 |
252 | | * separately avoids any risk of dividing by 0. Be careful about dividing |
253 | | * INT_MIN by -1 also, note reversing the a and b to ensure we're always |
254 | | * dividing it by a positive value. |
255 | | * |
256 | | */ |
257 | | if ((a > PG_INT32_MAX || a < PG_INT32_MIN || |
258 | | b > PG_INT32_MAX || b < PG_INT32_MIN) && |
259 | | a != 0 && a != 1 && b != 0 && b != 1 && |
260 | | ((a > 0 && b > 0 && a > PG_INT64_MAX / b) || |
261 | | (a > 0 && b < 0 && b < PG_INT64_MIN / a) || |
262 | | (a < 0 && b > 0 && a < PG_INT64_MIN / b) || |
263 | | (a < 0 && b < 0 && a < PG_INT64_MAX / b))) |
264 | | { |
265 | | *result = 0x5EED; /* to avoid spurious warnings */ |
266 | | return true; |
267 | | } |
268 | | *result = a * b; |
269 | | return false; |
270 | | #endif |
271 | 98.0k | } Unexecuted instantiation: array_userfuncs.c:pg_mul_s64_overflow Unexecuted instantiation: arrayutils.c:pg_mul_s64_overflow cash.c:pg_mul_s64_overflow Line | Count | Source | 232 | 60 | { | 233 | 60 | #if defined(HAVE__BUILTIN_OP_OVERFLOW) | 234 | 60 | return __builtin_mul_overflow(a, b, result); | 235 | | #elif defined(HAVE_INT128) | 236 | | int128 res = (int128) a * (int128) b; | 237 | | | 238 | | if (res > PG_INT64_MAX || res < PG_INT64_MIN) | 239 | | { | 240 | | *result = 0x5EED; /* to avoid spurious warnings */ | 241 | | return true; | 242 | | } | 243 | | *result = (int64) res; | 244 | | return false; | 245 | | #else | 246 | | /* | 247 | | * Overflow can only happen if at least one value is outside the range | 248 | | * sqrt(min)..sqrt(max) so check that first as the division can be quite a | 249 | | * bit more expensive than the multiplication. | 250 | | * | 251 | | * Multiplying by 0 or 1 can't overflow of course and checking for 0 | 252 | | * separately avoids any risk of dividing by 0. Be careful about dividing | 253 | | * INT_MIN by -1 also, note reversing the a and b to ensure we're always | 254 | | * dividing it by a positive value. | 255 | | * | 256 | | */ | 257 | | if ((a > PG_INT32_MAX || a < PG_INT32_MIN || | 258 | | b > PG_INT32_MAX || b < PG_INT32_MIN) && | 259 | | a != 0 && a != 1 && b != 0 && b != 1 && | 260 | | ((a > 0 && b > 0 && a > PG_INT64_MAX / b) || | 261 | | (a > 0 && b < 0 && b < PG_INT64_MIN / a) || | 262 | | (a < 0 && b > 0 && a < PG_INT64_MIN / b) || | 263 | | (a < 0 && b < 0 && a < PG_INT64_MAX / b))) | 264 | | { | 265 | | *result = 0x5EED; /* to avoid spurious warnings */ | 266 | | return true; | 267 | | } | 268 | | *result = a * b; | 269 | | return false; | 270 | | #endif | 271 | 60 | } |
Unexecuted instantiation: float.c:pg_mul_s64_overflow Unexecuted instantiation: int.c:pg_mul_s64_overflow int8.c:pg_mul_s64_overflow Line | Count | Source | 232 | 97.9k | { | 233 | 97.9k | #if defined(HAVE__BUILTIN_OP_OVERFLOW) | 234 | 97.9k | return __builtin_mul_overflow(a, b, result); | 235 | | #elif defined(HAVE_INT128) | 236 | | int128 res = (int128) a * (int128) b; | 237 | | | 238 | | if (res > PG_INT64_MAX || res < PG_INT64_MIN) | 239 | | { | 240 | | *result = 0x5EED; /* to avoid spurious warnings */ | 241 | | return true; | 242 | | } | 243 | | *result = (int64) res; | 244 | | return false; | 245 | | #else | 246 | | /* | 247 | | * Overflow can only happen if at least one value is outside the range | 248 | | * sqrt(min)..sqrt(max) so check that first as the division can be quite a | 249 | | * bit more expensive than the multiplication. | 250 | | * | 251 | | * Multiplying by 0 or 1 can't overflow of course and checking for 0 | 252 | | * separately avoids any risk of dividing by 0. Be careful about dividing | 253 | | * INT_MIN by -1 also, note reversing the a and b to ensure we're always | 254 | | * dividing it by a positive value. | 255 | | * | 256 | | */ | 257 | | if ((a > PG_INT32_MAX || a < PG_INT32_MIN || | 258 | | b > PG_INT32_MAX || b < PG_INT32_MIN) && | 259 | | a != 0 && a != 1 && b != 0 && b != 1 && | 260 | | ((a > 0 && b > 0 && a > PG_INT64_MAX / b) || | 261 | | (a > 0 && b < 0 && b < PG_INT64_MIN / a) || | 262 | | (a < 0 && b > 0 && a < PG_INT64_MIN / b) || | 263 | | (a < 0 && b < 0 && a < PG_INT64_MAX / b))) | 264 | | { | 265 | | *result = 0x5EED; /* to avoid spurious warnings */ | 266 | | return true; | 267 | | } | 268 | | *result = a * b; | 269 | | return false; | 270 | | #endif | 271 | 97.9k | } |
numeric.c:pg_mul_s64_overflow Line | Count | Source | 232 | 1 | { | 233 | 1 | #if defined(HAVE__BUILTIN_OP_OVERFLOW) | 234 | 1 | return __builtin_mul_overflow(a, b, result); | 235 | | #elif defined(HAVE_INT128) | 236 | | int128 res = (int128) a * (int128) b; | 237 | | | 238 | | if (res > PG_INT64_MAX || res < PG_INT64_MIN) | 239 | | { | 240 | | *result = 0x5EED; /* to avoid spurious warnings */ | 241 | | return true; | 242 | | } | 243 | | *result = (int64) res; | 244 | | return false; | 245 | | #else | 246 | | /* | 247 | | * Overflow can only happen if at least one value is outside the range | 248 | | * sqrt(min)..sqrt(max) so check that first as the division can be quite a | 249 | | * bit more expensive than the multiplication. | 250 | | * | 251 | | * Multiplying by 0 or 1 can't overflow of course and checking for 0 | 252 | | * separately avoids any risk of dividing by 0. Be careful about dividing | 253 | | * INT_MIN by -1 also, note reversing the a and b to ensure we're always | 254 | | * dividing it by a positive value. | 255 | | * | 256 | | */ | 257 | | if ((a > PG_INT32_MAX || a < PG_INT32_MIN || | 258 | | b > PG_INT32_MAX || b < PG_INT32_MIN) && | 259 | | a != 0 && a != 1 && b != 0 && b != 1 && | 260 | | ((a > 0 && b > 0 && a > PG_INT64_MAX / b) || | 261 | | (a > 0 && b < 0 && b < PG_INT64_MIN / a) || | 262 | | (a < 0 && b > 0 && a < PG_INT64_MIN / b) || | 263 | | (a < 0 && b < 0 && a < PG_INT64_MAX / b))) | 264 | | { | 265 | | *result = 0x5EED; /* to avoid spurious warnings */ | 266 | | return true; | 267 | | } | 268 | | *result = a * b; | 269 | | return false; | 270 | | #endif | 271 | 1 | } |
Unexecuted instantiation: oracle_compat.c:pg_mul_s64_overflow Unexecuted instantiation: varbit.c:pg_mul_s64_overflow Unexecuted instantiation: varlena.c:pg_mul_s64_overflow Unexecuted instantiation: ybgate_api.c:pg_mul_s64_overflow |
272 | | |
273 | | #endif /* COMMON_INT_H */ |