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)
 
static int pg_cmp_s16 (int16 a, int16 b)
 
static int pg_cmp_u16 (uint16 a, uint16 b)
 
static int pg_cmp_s32 (int32 a, int32 b)
 
static int pg_cmp_u32 (uint32 a, uint32 b)
 
static int pg_cmp_s64 (int64 a, int64 b)
 
static int pg_cmp_u64 (uint64 a, uint64 b)
 
static int pg_cmp_size (size_t a, size_t b)
 

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.

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:493
signed int int32
Definition: c.h:494
#define PG_INT16_MIN
Definition: c.h:585
#define PG_INT16_MAX
Definition: c.h:586
int b
Definition: isn.c:70
int a
Definition: isn.c:69

References a, b, PG_INT16_MAX, PG_INT16_MIN, and res.

Referenced by int2pl().

◆ 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.

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 }
#define PG_INT32_MAX
Definition: c.h:589
#define PG_INT32_MIN
Definition: c.h:588

References a, b, PG_INT32_MAX, PG_INT32_MIN, and res.

Referenced by AdjustDays(), AdjustFractDays(), AdjustFractYears(), AdjustMonths(), AdjustYears(), array_append(), array_set_element(), array_set_element_expanded(), array_set_slice(), ArrayCheckBoundsSafe(), bit_overlay(), bitsubstring(), bytea_overlay(), bytea_substring(), detoast_attr_slice(), finite_interval_pl(), generate_series_step_int4(), in_range_int2_int4(), in_range_int4_int4(), int24pl(), int42pl(), int4inc(), int4pl(), interval_div(), interval_justify_days(), interval_justify_hours(), interval_justify_interval(), interval_mul(), lpad(), make_interval(), ReadArrayDimensions(), repeat(), rpad(), text_format_parse_digits(), text_overlay(), text_substring(), timestamp_pl_interval(), timestamptz_pl_interval_internal(), translate(), and width_bucket_float8().

◆ 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.

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:592
#define PG_INT64_MIN
Definition: c.h:591

References a, b, PG_INT64_MAX, PG_INT64_MIN, and res.

Referenced by AdjustFractMicroseconds(), AdjustIntervalForTypmod(), DecodeInterval(), evalStandardFunc(), finite_interval_pl(), generate_series_step_int8(), in_range_int4_int8(), in_range_int8_int8(), in_range_time_interval(), in_range_timetz_interval(), int28pl(), int48pl(), int64_multiply_add(), int82pl(), int84pl(), int8inc(), int8pl(), interval_part_common(), itm2interval(), make_interval(), timestamp_bin(), and timestamptz_bin().

◆ pg_add_u16_overflow()

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

Definition at line 270 of file int.h.

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

References a, b, and res.

◆ pg_add_u32_overflow()

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

Definition at line 325 of file int.h.

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

References a, b, and res.

◆ pg_add_u64_overflow()

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

Definition at line 380 of file int.h.

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

References a, b, and res.

Referenced by basic_archive_file(), and numericvar_to_uint64().

◆ pg_cmp_s16()

static int pg_cmp_s16 ( int16  a,
int16  b 
)
inlinestatic

Definition at line 471 of file int.h.

472 {
473  return (int32) a - (int32) b;
474 }

References a, and b.

Referenced by _bt_delitems_cmp(), _bt_splitcmp(), AttrDefaultCmp(), cmpNodePtr(), and list_cookedconstr_attnum_cmp().

◆ pg_cmp_s32()

static int pg_cmp_s32 ( int32  a,
int32  b 
)
inlinestatic

◆ pg_cmp_s64()

static int pg_cmp_s64 ( int64  a,
int64  b 
)
inlinestatic

Definition at line 495 of file int.h.

496 {
497  return (a > b) - (a < b);
498 }

References a, and b.

◆ pg_cmp_size()

static int pg_cmp_size ( size_t  a,
size_t  b 
)
inlinestatic

Definition at line 507 of file int.h.

508 {
509  return (a > b) - (a < b);
510 }

References a, and b.

Referenced by library_name_compare().

◆ pg_cmp_u16()

static int pg_cmp_u16 ( uint16  a,
uint16  b 
)
inlinestatic

Definition at line 477 of file int.h.

478 {
479  return (int32) a - (int32) b;
480 }

References a, and b.

Referenced by cmpOffsetNumbers().

◆ pg_cmp_u32()

static int pg_cmp_u32 ( uint32  a,
uint32  b 
)
inlinestatic

Definition at line 489 of file int.h.

490 {
491  return (a > b) - (a < b);
492 }

References a, and b.

Referenced by _bt_blk_cmp(), compare_block_numbers(), enum_oid_cmp(), list_oid_cmp(), oid_cmp(), oid_compare(), resource_priority_cmp(), tbm_comparator(), and xidComparator().

◆ pg_cmp_u64()

static int pg_cmp_u64 ( uint64  a,
uint64  b 
)
inlinestatic

Definition at line 501 of file int.h.

502 {
503  return (a > b) - (a < b);
504 }

References a, and b.

Referenced by cmp_lsn(), file_sort_by_lsn(), ginCompareItemPointers(), key_cmp(), and ListComparatorForWalSummaryFiles().

◆ 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.

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 }

References a, b, PG_INT16_MAX, PG_INT16_MIN, and res.

Referenced by int2mul().

◆ 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.

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 }

References a, b, PG_INT32_MAX, PG_INT32_MIN, and res.

Referenced by AdjustDays(), AdjustYears(), int24mul(), int42mul(), int4lcm(), int4mul(), lpad(), make_interval(), repeat(), rpad(), text_format_parse_digits(), text_substring(), and translate().

◆ pg_mul_s64_overflow()

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

Definition at line 219 of file int.h.

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

References a, b, PG_INT32_MAX, PG_INT32_MIN, PG_INT64_MAX, PG_INT64_MIN, and res.

Referenced by cash_in(), DecodeInterval(), evalStandardFunc(), int28mul(), int48mul(), int64_div_fast_to_numeric(), int64_multiply_add(), int82mul(), int84mul(), int8lcm(), int8mul(), interval_part_common(), itm2interval(), numericvar_to_int64(), strtoint64(), timestamp_bin(), and timestamptz_bin().

◆ pg_mul_u16_overflow()

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

Definition at line 304 of file int.h.

305 {
306 #if defined(HAVE__BUILTIN_OP_OVERFLOW)
307  return __builtin_mul_overflow(a, b, result);
308 #else
309  uint32 res = (uint32) a * (uint32) b;
310 
311  if (res > PG_UINT16_MAX)
312  {
313  *result = 0x5EED; /* to avoid spurious warnings */
314  return true;
315  }
316  *result = (uint16) res;
317  return false;
318 #endif
319 }
#define PG_UINT16_MAX
Definition: c.h:587

References a, b, PG_UINT16_MAX, and res.

◆ pg_mul_u32_overflow()

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

Definition at line 359 of file int.h.

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

References a, b, PG_UINT32_MAX, and res.

◆ pg_mul_u64_overflow()

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

Definition at line 414 of file int.h.

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

References a, b, PG_UINT64_MAX, and res.

Referenced by basic_archive_file(), and numericvar_to_uint64().

◆ 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.

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 }

References a, b, PG_INT16_MAX, PG_INT16_MIN, and res.

Referenced by int2_dist(), and int2mi().

◆ 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.

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 }

References a, b, PG_INT32_MAX, PG_INT32_MIN, and res.

Referenced by array_prepend(), array_set_element(), array_set_element_expanded(), array_set_slice(), finite_interval_mi(), int24mi(), int42mi(), int4_dist(), int4mi(), interval_um_internal(), and ReadArrayDimensions().

◆ 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.

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  /*
204  * Note: overflow is also possible when a == 0 and b < 0 (specifically,
205  * when b == PG_INT64_MIN).
206  */
207  if ((a < 0 && b > 0 && a < PG_INT64_MIN + b) ||
208  (a >= 0 && b < 0 && a > PG_INT64_MAX + b))
209  {
210  *result = 0x5EED; /* to avoid spurious warnings */
211  return true;
212  }
213  *result = a - b;
214  return false;
215 #endif
216 }

References a, b, PG_INT64_MAX, PG_INT64_MIN, and res.

Referenced by AdjustIntervalForTypmod(), cash_dist(), cash_in(), evalStandardFunc(), finite_interval_mi(), int28mi(), int48mi(), int82mi(), int84mi(), int8_dist(), int8dec(), int8mi(), interval_um_internal(), numericvar_to_int64(), strtoint64(), timestamp_bin(), timestamp_mi(), TimestampDifferenceMilliseconds(), and timestamptz_bin().

◆ pg_sub_u16_overflow()

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

Definition at line 288 of file int.h.

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

References a, and b.

◆ pg_sub_u32_overflow()

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

Definition at line 343 of file int.h.

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

References a, and b.

◆ pg_sub_u64_overflow()

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

Definition at line 398 of file int.h.

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

References a, and b.