PostgreSQL Source Code  git master
int.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

static bool pg_add_s16_overflow (int16 a, int16 b, int16 *result)
 
static bool pg_sub_s16_overflow (int16 a, int16 b, int16 *result)
 
static bool pg_mul_s16_overflow (int16 a, int16 b, int16 *result)
 
static bool pg_add_s32_overflow (int32 a, int32 b, int32 *result)
 
static bool pg_sub_s32_overflow (int32 a, int32 b, int32 *result)
 
static bool pg_mul_s32_overflow (int32 a, int32 b, int32 *result)
 
static bool pg_add_s64_overflow (int64 a, int64 b, int64 *result)
 
static bool pg_sub_s64_overflow (int64 a, int64 b, int64 *result)
 
static bool pg_mul_s64_overflow (int64 a, int64 b, int64 *result)
 
static bool pg_add_u16_overflow (uint16 a, uint16 b, uint16 *result)
 
static bool pg_sub_u16_overflow (uint16 a, uint16 b, uint16 *result)
 
static bool pg_mul_u16_overflow (uint16 a, uint16 b, uint16 *result)
 
static bool pg_add_u32_overflow (uint32 a, uint32 b, uint32 *result)
 
static bool pg_sub_u32_overflow (uint32 a, uint32 b, uint32 *result)
 
static bool pg_mul_u32_overflow (uint32 a, uint32 b, uint32 *result)
 
static bool pg_add_u64_overflow (uint64 a, uint64 b, uint64 *result)
 
static bool pg_sub_u64_overflow (uint64 a, uint64 b, uint64 *result)
 
static bool pg_mul_u64_overflow (uint64 a, uint64 b, uint64 *result)
 

Function Documentation

◆ pg_add_s16_overflow()

static bool pg_add_s16_overflow ( int16  a,
int16  b,
int16 result 
)
inlinestatic

Definition at line 47 of file int.h.

References PG_INT16_MAX, and PG_INT16_MIN.

Referenced by int2pl().

48 {
49 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
50  return __builtin_add_overflow(a, b, result);
51 #else
52  int32 res = (int32) a + (int32) b;
53 
54  if (res > PG_INT16_MAX || res < PG_INT16_MIN)
55  {
56  *result = 0x5EED; /* to avoid spurious warnings */
57  return true;
58  }
59  *result = (int16) res;
60  return false;
61 #endif
62 }
signed short int16
Definition: c.h:346
signed int int32
Definition: c.h:347
#define PG_INT16_MIN
Definition: c.h:438
#define PG_INT16_MAX
Definition: c.h:439

◆ pg_add_s32_overflow()

static bool pg_add_s32_overflow ( int32  a,
int32  b,
int32 result 
)
inlinestatic

Definition at line 104 of file int.h.

References PG_INT32_MAX, and PG_INT32_MIN.

Referenced by array_append(), bit_overlay(), bytea_overlay(), generate_series_step_int4(), in_range_int2_int4(), in_range_int4_int4(), int24pl(), int42pl(), int4inc(), int4pl(), repeat(), text_format_parse_digits(), text_overlay(), and width_bucket_float8().

105 {
106 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
107  return __builtin_add_overflow(a, b, result);
108 #else
109  int64 res = (int64) a + (int64) b;
110 
111  if (res > PG_INT32_MAX || res < PG_INT32_MIN)
112  {
113  *result = 0x5EED; /* to avoid spurious warnings */
114  return true;
115  }
116  *result = (int32) res;
117  return false;
118 #endif
119 }
signed int int32
Definition: c.h:347
#define PG_INT32_MIN
Definition: c.h:441
#define PG_INT32_MAX
Definition: c.h:442

◆ pg_add_s64_overflow()

static bool pg_add_s64_overflow ( int64  a,
int64  b,
int64 *  result 
)
inlinestatic

Definition at line 161 of file int.h.

References PG_INT64_MAX, and PG_INT64_MIN.

Referenced by evalStandardFunc(), generate_series_step_int8(), in_range_int4_int8(), in_range_int8_int8(), int28pl(), int48pl(), int82pl(), int84pl(), int8inc(), and int8pl().

162 {
163 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
164  return __builtin_add_overflow(a, b, result);
165 #elif defined(HAVE_INT128)
166  int128 res = (int128) a + (int128) b;
167 
168  if (res > PG_INT64_MAX || res < PG_INT64_MIN)
169  {
170  *result = 0x5EED; /* to avoid spurious warnings */
171  return true;
172  }
173  *result = (int64) res;
174  return false;
175 #else
176  if ((a > 0 && b > 0 && a > PG_INT64_MAX - b) ||
177  (a < 0 && b < 0 && a < PG_INT64_MIN - b))
178  {
179  *result = 0x5EED; /* to avoid spurious warnings */
180  return true;
181  }
182  *result = a + b;
183  return false;
184 #endif
185 }
#define PG_INT64_MAX
Definition: c.h:445
#define PG_INT64_MIN
Definition: c.h:444

◆ pg_add_u16_overflow()

static bool pg_add_u16_overflow ( uint16  a,
uint16  b,
uint16 result 
)
inlinestatic

Definition at line 266 of file int.h.

267 {
268 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
269  return __builtin_add_overflow(a, b, result);
270 #else
271  uint16 res = a + b;
272 
273  if (res < a)
274  {
275  *result = 0x5EED; /* to avoid spurious warnings */
276  return true;
277  }
278  *result = res;
279  return false;
280 #endif
281 }
unsigned short uint16
Definition: c.h:358

◆ pg_add_u32_overflow()

static bool pg_add_u32_overflow ( uint32  a,
uint32  b,
uint32 result 
)
inlinestatic

Definition at line 321 of file int.h.

322 {
323 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
324  return __builtin_add_overflow(a, b, result);
325 #else
326  uint32 res = a + b;
327 
328  if (res < a)
329  {
330  *result = 0x5EED; /* to avoid spurious warnings */
331  return true;
332  }
333  *result = res;
334  return false;
335 #endif
336 }
unsigned int uint32
Definition: c.h:359

◆ pg_add_u64_overflow()

static bool pg_add_u64_overflow ( uint64  a,
uint64  b,
uint64 *  result 
)
inlinestatic

Definition at line 376 of file int.h.

377 {
378 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
379  return __builtin_add_overflow(a, b, result);
380 #else
381  uint64 res = a + b;
382 
383  if (res < a)
384  {
385  *result = 0x5EED; /* to avoid spurious warnings */
386  return true;
387  }
388  *result = res;
389  return false;
390 #endif
391 }

◆ pg_mul_s16_overflow()

static bool pg_mul_s16_overflow ( int16  a,
int16  b,
int16 result 
)
inlinestatic

Definition at line 83 of file int.h.

References PG_INT16_MAX, and PG_INT16_MIN.

Referenced by int2mul(), and pg_strtoint16().

84 {
85 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
86  return __builtin_mul_overflow(a, b, result);
87 #else
88  int32 res = (int32) a * (int32) b;
89 
90  if (res > PG_INT16_MAX || res < PG_INT16_MIN)
91  {
92  *result = 0x5EED; /* to avoid spurious warnings */
93  return true;
94  }
95  *result = (int16) res;
96  return false;
97 #endif
98 }
signed short int16
Definition: c.h:346
signed int int32
Definition: c.h:347
#define PG_INT16_MIN
Definition: c.h:438
#define PG_INT16_MAX
Definition: c.h:439

◆ pg_mul_s32_overflow()

static bool pg_mul_s32_overflow ( int32  a,
int32  b,
int32 result 
)
inlinestatic

Definition at line 140 of file int.h.

References PG_INT32_MAX, and PG_INT32_MIN.

Referenced by int24mul(), int42mul(), int4mul(), pg_strtoint32(), repeat(), and text_format_parse_digits().

141 {
142 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
143  return __builtin_mul_overflow(a, b, result);
144 #else
145  int64 res = (int64) a * (int64) b;
146 
147  if (res > PG_INT32_MAX || res < PG_INT32_MIN)
148  {
149  *result = 0x5EED; /* to avoid spurious warnings */
150  return true;
151  }
152  *result = (int32) res;
153  return false;
154 #endif
155 }
signed int int32
Definition: c.h:347
#define PG_INT32_MIN
Definition: c.h:441
#define PG_INT32_MAX
Definition: c.h:442

◆ pg_mul_s64_overflow()

static bool pg_mul_s64_overflow ( int64  a,
int64  b,
int64 *  result 
)
inlinestatic

Definition at line 215 of file int.h.

References PG_INT32_MAX, PG_INT32_MIN, PG_INT64_MAX, and PG_INT64_MIN.

Referenced by cash_in(), evalStandardFunc(), int28mul(), int48mul(), int82mul(), int84mul(), int8mul(), numericvar_to_int64(), scanint8(), and strtoint64().

216 {
217 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
218  return __builtin_mul_overflow(a, b, result);
219 #elif defined(HAVE_INT128)
220  int128 res = (int128) a * (int128) b;
221 
222  if (res > PG_INT64_MAX || res < PG_INT64_MIN)
223  {
224  *result = 0x5EED; /* to avoid spurious warnings */
225  return true;
226  }
227  *result = (int64) res;
228  return false;
229 #else
230  /*
231  * Overflow can only happen if at least one value is outside the range
232  * sqrt(min)..sqrt(max) so check that first as the division can be quite a
233  * bit more expensive than the multiplication.
234  *
235  * Multiplying by 0 or 1 can't overflow of course and checking for 0
236  * separately avoids any risk of dividing by 0. Be careful about dividing
237  * INT_MIN by -1 also, note reversing the a and b to ensure we're always
238  * dividing it by a positive value.
239  *
240  */
241  if ((a > PG_INT32_MAX || a < PG_INT32_MIN ||
242  b > PG_INT32_MAX || b < PG_INT32_MIN) &&
243  a != 0 && a != 1 && b != 0 && b != 1 &&
244  ((a > 0 && b > 0 && a > PG_INT64_MAX / b) ||
245  (a > 0 && b < 0 && b < PG_INT64_MIN / a) ||
246  (a < 0 && b > 0 && a < PG_INT64_MIN / b) ||
247  (a < 0 && b < 0 && a < PG_INT64_MAX / b)))
248  {
249  *result = 0x5EED; /* to avoid spurious warnings */
250  return true;
251  }
252  *result = a * b;
253  return false;
254 #endif
255 }
#define PG_INT64_MAX
Definition: c.h:445
#define PG_INT64_MIN
Definition: c.h:444
#define PG_INT32_MIN
Definition: c.h:441
#define PG_INT32_MAX
Definition: c.h:442

◆ pg_mul_u16_overflow()

static bool pg_mul_u16_overflow ( uint16  a,
uint16  b,
uint16 result 
)
inlinestatic

Definition at line 300 of file int.h.

References PG_UINT16_MAX.

301 {
302 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
303  return __builtin_mul_overflow(a, b, result);
304 #else
305  uint32 res = (uint32) a * (uint32) b;
306 
307  if (res > PG_UINT16_MAX)
308  {
309  *result = 0x5EED; /* to avoid spurious warnings */
310  return true;
311  }
312  *result = (uint16) res;
313  return false;
314 #endif
315 }
unsigned short uint16
Definition: c.h:358
#define PG_UINT16_MAX
Definition: c.h:440
unsigned int uint32
Definition: c.h:359

◆ pg_mul_u32_overflow()

static bool pg_mul_u32_overflow ( uint32  a,
uint32  b,
uint32 result 
)
inlinestatic

Definition at line 355 of file int.h.

References PG_UINT32_MAX.

356 {
357 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
358  return __builtin_mul_overflow(a, b, result);
359 #else
360  uint64 res = (uint64) a * (uint64) b;
361 
362  if (res > PG_UINT32_MAX)
363  {
364  *result = 0x5EED; /* to avoid spurious warnings */
365  return true;
366  }
367  *result = (uint32) res;
368  return false;
369 #endif
370 }
#define PG_UINT32_MAX
Definition: c.h:443
unsigned int uint32
Definition: c.h:359

◆ pg_mul_u64_overflow()

static bool pg_mul_u64_overflow ( uint64  a,
uint64  b,
uint64 *  result 
)
inlinestatic

Definition at line 410 of file int.h.

References PG_UINT64_MAX.

411 {
412 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
413  return __builtin_mul_overflow(a, b, result);
414 #elif defined(HAVE_INT128)
415  uint128 res = (uint128) a * (uint128) b;
416 
417  if (res > PG_UINT64_MAX)
418  {
419  *result = 0x5EED; /* to avoid spurious warnings */
420  return true;
421  }
422  *result = (uint64) res;
423  return false;
424 #else
425  uint64 res = a * b;
426 
427  if (a != 0 && b != res / a)
428  {
429  *result = 0x5EED; /* to avoid spurious warnings */
430  return true;
431  }
432  *result = res;
433  return false;
434 #endif
435 }
#define PG_UINT64_MAX
Definition: c.h:446

◆ pg_sub_s16_overflow()

static bool pg_sub_s16_overflow ( int16  a,
int16  b,
int16 result 
)
inlinestatic

Definition at line 65 of file int.h.

References PG_INT16_MAX, and PG_INT16_MIN.

Referenced by int2_dist(), int2mi(), and pg_strtoint16().

66 {
67 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
68  return __builtin_sub_overflow(a, b, result);
69 #else
70  int32 res = (int32) a - (int32) b;
71 
72  if (res > PG_INT16_MAX || res < PG_INT16_MIN)
73  {
74  *result = 0x5EED; /* to avoid spurious warnings */
75  return true;
76  }
77  *result = (int16) res;
78  return false;
79 #endif
80 }
signed short int16
Definition: c.h:346
signed int int32
Definition: c.h:347
#define PG_INT16_MIN
Definition: c.h:438
#define PG_INT16_MAX
Definition: c.h:439

◆ pg_sub_s32_overflow()

static bool pg_sub_s32_overflow ( int32  a,
int32  b,
int32 result 
)
inlinestatic

Definition at line 122 of file int.h.

References PG_INT32_MAX, and PG_INT32_MIN.

Referenced by array_prepend(), int24mi(), int42mi(), int4_dist(), int4mi(), and pg_strtoint32().

123 {
124 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
125  return __builtin_sub_overflow(a, b, result);
126 #else
127  int64 res = (int64) a - (int64) b;
128 
129  if (res > PG_INT32_MAX || res < PG_INT32_MIN)
130  {
131  *result = 0x5EED; /* to avoid spurious warnings */
132  return true;
133  }
134  *result = (int32) res;
135  return false;
136 #endif
137 }
signed int int32
Definition: c.h:347
#define PG_INT32_MIN
Definition: c.h:441
#define PG_INT32_MAX
Definition: c.h:442

◆ pg_sub_s64_overflow()

static bool pg_sub_s64_overflow ( int64  a,
int64  b,
int64 *  result 
)
inlinestatic

Definition at line 188 of file int.h.

References PG_INT64_MAX, and PG_INT64_MIN.

Referenced by cash_dist(), cash_in(), evalStandardFunc(), int28mi(), int48mi(), int82mi(), int84mi(), int8_dist(), int8dec(), int8mi(), numericvar_to_int64(), scanint8(), and strtoint64().

189 {
190 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
191  return __builtin_sub_overflow(a, b, result);
192 #elif defined(HAVE_INT128)
193  int128 res = (int128) a - (int128) b;
194 
195  if (res > PG_INT64_MAX || res < PG_INT64_MIN)
196  {
197  *result = 0x5EED; /* to avoid spurious warnings */
198  return true;
199  }
200  *result = (int64) res;
201  return false;
202 #else
203  if ((a < 0 && b > 0 && a < PG_INT64_MIN + b) ||
204  (a > 0 && b < 0 && a > PG_INT64_MAX + b))
205  {
206  *result = 0x5EED; /* to avoid spurious warnings */
207  return true;
208  }
209  *result = a - b;
210  return false;
211 #endif
212 }
#define PG_INT64_MAX
Definition: c.h:445
#define PG_INT64_MIN
Definition: c.h:444

◆ pg_sub_u16_overflow()

static bool pg_sub_u16_overflow ( uint16  a,
uint16  b,
uint16 result 
)
inlinestatic

Definition at line 284 of file int.h.

285 {
286 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
287  return __builtin_sub_overflow(a, b, result);
288 #else
289  if (b > a)
290  {
291  *result = 0x5EED; /* to avoid spurious warnings */
292  return true;
293  }
294  *result = a - b;
295  return false;
296 #endif
297 }

◆ pg_sub_u32_overflow()

static bool pg_sub_u32_overflow ( uint32  a,
uint32  b,
uint32 result 
)
inlinestatic

Definition at line 339 of file int.h.

340 {
341 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
342  return __builtin_sub_overflow(a, b, result);
343 #else
344  if (b > a)
345  {
346  *result = 0x5EED; /* to avoid spurious warnings */
347  return true;
348  }
349  *result = a - b;
350  return false;
351 #endif
352 }

◆ pg_sub_u64_overflow()

static bool pg_sub_u64_overflow ( uint64  a,
uint64  b,
uint64 *  result 
)
inlinestatic

Definition at line 394 of file int.h.

395 {
396 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
397  return __builtin_sub_overflow(a, b, result);
398 #else
399  if (b > a)
400  {
401  *result = 0x5EED; /* to avoid spurious warnings */
402  return true;
403  }
404  *result = a - b;
405  return false;
406 #endif
407 }