PostgreSQL Source Code  git master
int.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <limits.h>
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "libpq/pqformat.h"
#include "utils/array.h"
#include "utils/builtins.h"
Include dependency graph for int.c:

Go to the source code of this file.

Data Structures

struct  generate_series_fctx
 

Macros

#define SAMESIGN(a, b)   (((a) < 0) == ((b) < 0))
 
#define Int2VectorSize(n)   (offsetof(int2vector, values) + (n) * sizeof(int16))
 

Functions

Datum int2in (PG_FUNCTION_ARGS)
 
Datum int2out (PG_FUNCTION_ARGS)
 
Datum int2recv (PG_FUNCTION_ARGS)
 
Datum int2send (PG_FUNCTION_ARGS)
 
int2vectorbuildint2vector (const int16 *int2s, int n)
 
Datum int2vectorin (PG_FUNCTION_ARGS)
 
Datum int2vectorout (PG_FUNCTION_ARGS)
 
Datum int2vectorrecv (PG_FUNCTION_ARGS)
 
Datum int2vectorsend (PG_FUNCTION_ARGS)
 
Datum int4in (PG_FUNCTION_ARGS)
 
Datum int4out (PG_FUNCTION_ARGS)
 
Datum int4recv (PG_FUNCTION_ARGS)
 
Datum int4send (PG_FUNCTION_ARGS)
 
Datum i2toi4 (PG_FUNCTION_ARGS)
 
Datum i4toi2 (PG_FUNCTION_ARGS)
 
Datum int4_bool (PG_FUNCTION_ARGS)
 
Datum bool_int4 (PG_FUNCTION_ARGS)
 
Datum int4eq (PG_FUNCTION_ARGS)
 
Datum int4ne (PG_FUNCTION_ARGS)
 
Datum int4lt (PG_FUNCTION_ARGS)
 
Datum int4le (PG_FUNCTION_ARGS)
 
Datum int4gt (PG_FUNCTION_ARGS)
 
Datum int4ge (PG_FUNCTION_ARGS)
 
Datum int2eq (PG_FUNCTION_ARGS)
 
Datum int2ne (PG_FUNCTION_ARGS)
 
Datum int2lt (PG_FUNCTION_ARGS)
 
Datum int2le (PG_FUNCTION_ARGS)
 
Datum int2gt (PG_FUNCTION_ARGS)
 
Datum int2ge (PG_FUNCTION_ARGS)
 
Datum int24eq (PG_FUNCTION_ARGS)
 
Datum int24ne (PG_FUNCTION_ARGS)
 
Datum int24lt (PG_FUNCTION_ARGS)
 
Datum int24le (PG_FUNCTION_ARGS)
 
Datum int24gt (PG_FUNCTION_ARGS)
 
Datum int24ge (PG_FUNCTION_ARGS)
 
Datum int42eq (PG_FUNCTION_ARGS)
 
Datum int42ne (PG_FUNCTION_ARGS)
 
Datum int42lt (PG_FUNCTION_ARGS)
 
Datum int42le (PG_FUNCTION_ARGS)
 
Datum int42gt (PG_FUNCTION_ARGS)
 
Datum int42ge (PG_FUNCTION_ARGS)
 
Datum int4um (PG_FUNCTION_ARGS)
 
Datum int4up (PG_FUNCTION_ARGS)
 
Datum int4pl (PG_FUNCTION_ARGS)
 
Datum int4mi (PG_FUNCTION_ARGS)
 
Datum int4mul (PG_FUNCTION_ARGS)
 
Datum int4div (PG_FUNCTION_ARGS)
 
Datum int4inc (PG_FUNCTION_ARGS)
 
Datum int2um (PG_FUNCTION_ARGS)
 
Datum int2up (PG_FUNCTION_ARGS)
 
Datum int2pl (PG_FUNCTION_ARGS)
 
Datum int2mi (PG_FUNCTION_ARGS)
 
Datum int2mul (PG_FUNCTION_ARGS)
 
Datum int2div (PG_FUNCTION_ARGS)
 
Datum int24pl (PG_FUNCTION_ARGS)
 
Datum int24mi (PG_FUNCTION_ARGS)
 
Datum int24mul (PG_FUNCTION_ARGS)
 
Datum int24div (PG_FUNCTION_ARGS)
 
Datum int42pl (PG_FUNCTION_ARGS)
 
Datum int42mi (PG_FUNCTION_ARGS)
 
Datum int42mul (PG_FUNCTION_ARGS)
 
Datum int42div (PG_FUNCTION_ARGS)
 
Datum int4mod (PG_FUNCTION_ARGS)
 
Datum int2mod (PG_FUNCTION_ARGS)
 
Datum int4abs (PG_FUNCTION_ARGS)
 
Datum int2abs (PG_FUNCTION_ARGS)
 
Datum int2larger (PG_FUNCTION_ARGS)
 
Datum int2smaller (PG_FUNCTION_ARGS)
 
Datum int4larger (PG_FUNCTION_ARGS)
 
Datum int4smaller (PG_FUNCTION_ARGS)
 
Datum int4and (PG_FUNCTION_ARGS)
 
Datum int4or (PG_FUNCTION_ARGS)
 
Datum int4xor (PG_FUNCTION_ARGS)
 
Datum int4shl (PG_FUNCTION_ARGS)
 
Datum int4shr (PG_FUNCTION_ARGS)
 
Datum int4not (PG_FUNCTION_ARGS)
 
Datum int2and (PG_FUNCTION_ARGS)
 
Datum int2or (PG_FUNCTION_ARGS)
 
Datum int2xor (PG_FUNCTION_ARGS)
 
Datum int2not (PG_FUNCTION_ARGS)
 
Datum int2shl (PG_FUNCTION_ARGS)
 
Datum int2shr (PG_FUNCTION_ARGS)
 
Datum generate_series_int4 (PG_FUNCTION_ARGS)
 
Datum generate_series_step_int4 (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ Int2VectorSize

#define Int2VectorSize (   n)    (offsetof(int2vector, values) + (n) * sizeof(int16))

Definition at line 43 of file int.c.

Referenced by buildint2vector(), and int2vectorin().

◆ SAMESIGN

#define SAMESIGN (   a,
 
)    (((a) < 0) == ((b) < 0))

Function Documentation

◆ bool_int4()

Datum bool_int4 ( PG_FUNCTION_ARGS  )

Definition at line 351 of file int.c.

References PG_GETARG_BOOL, and PG_RETURN_INT32.

352 {
353  if (PG_GETARG_BOOL(0) == false)
354  PG_RETURN_INT32(0);
355  else
356  PG_RETURN_INT32(1);
357 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239

◆ buildint2vector()

int2vector* buildint2vector ( const int16 int2s,
int  n 
)

Definition at line 112 of file int.c.

References int2vector::dataoffset, int2vector::dim1, int2vector::elemtype, INT2OID, Int2VectorSize, int2vector::lbound1, int2vector::ndim, palloc0(), SET_VARSIZE, and int2vector::values.

Referenced by CreateStatistics(), CreateTrigger(), StorePartitionKey(), and UpdateIndexRelation().

113 {
114  int2vector *result;
115 
116  result = (int2vector *) palloc0(Int2VectorSize(n));
117 
118  if (n > 0 && int2s)
119  memcpy(result->values, int2s, n * sizeof(int16));
120 
121  /*
122  * Attach standard array header. For historical reasons, we set the index
123  * lower bound to 0 not 1.
124  */
125  SET_VARSIZE(result, Int2VectorSize(n));
126  result->ndim = 1;
127  result->dataoffset = 0; /* never any nulls */
128  result->elemtype = INT2OID;
129  result->dim1 = n;
130  result->lbound1 = 0;
131 
132  return result;
133 }
signed short int16
Definition: c.h:283
int lbound1
Definition: c.h:522
Oid elemtype
Definition: c.h:520
int32 dataoffset
Definition: c.h:519
#define INT2OID
Definition: pg_type.h:308
int ndim
Definition: c.h:518
Definition: c.h:515
void * palloc0(Size size)
Definition: mcxt.c:877
int dim1
Definition: c.h:521
int16 values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:523
#define Int2VectorSize(n)
Definition: int.c:43
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328

◆ generate_series_int4()

Datum generate_series_int4 ( PG_FUNCTION_ARGS  )

Definition at line 1321 of file int.c.

References generate_series_step_int4().

1322 {
1323  return generate_series_step_int4(fcinfo);
1324 }
Datum generate_series_step_int4(PG_FUNCTION_ARGS)
Definition: int.c:1327

◆ generate_series_step_int4()

Datum generate_series_step_int4 ( PG_FUNCTION_ARGS  )

Definition at line 1327 of file int.c.

References generate_series_fctx::current, ereport, errcode(), errmsg(), ERROR, generate_series_fctx::finish, Int32GetDatum, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, palloc(), PG_GETARG_INT32, PG_NARGS, SAMESIGN, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, generate_series_fctx::step, and FuncCallContext::user_fctx.

Referenced by generate_series_int4().

1328 {
1329  FuncCallContext *funcctx;
1330  generate_series_fctx *fctx;
1331  int32 result;
1332  MemoryContext oldcontext;
1333 
1334  /* stuff done only on the first call of the function */
1335  if (SRF_IS_FIRSTCALL())
1336  {
1337  int32 start = PG_GETARG_INT32(0);
1338  int32 finish = PG_GETARG_INT32(1);
1339  int32 step = 1;
1340 
1341  /* see if we were given an explicit step size */
1342  if (PG_NARGS() == 3)
1343  step = PG_GETARG_INT32(2);
1344  if (step == 0)
1345  ereport(ERROR,
1346  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1347  errmsg("step size cannot equal zero")));
1348 
1349  /* create a function context for cross-call persistence */
1350  funcctx = SRF_FIRSTCALL_INIT();
1351 
1352  /*
1353  * switch to memory context appropriate for multiple function calls
1354  */
1355  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1356 
1357  /* allocate memory for user context */
1358  fctx = (generate_series_fctx *) palloc(sizeof(generate_series_fctx));
1359 
1360  /*
1361  * Use fctx to keep state from call to call. Seed current with the
1362  * original start value
1363  */
1364  fctx->current = start;
1365  fctx->finish = finish;
1366  fctx->step = step;
1367 
1368  funcctx->user_fctx = fctx;
1369  MemoryContextSwitchTo(oldcontext);
1370  }
1371 
1372  /* stuff done on every call of the function */
1373  funcctx = SRF_PERCALL_SETUP();
1374 
1375  /*
1376  * get the saved state and use current as the result for this iteration
1377  */
1378  fctx = funcctx->user_fctx;
1379  result = fctx->current;
1380 
1381  if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1382  (fctx->step < 0 && fctx->current >= fctx->finish))
1383  {
1384  /* increment current in preparation for next iteration */
1385  fctx->current += fctx->step;
1386 
1387  /* if next-value computation overflows, this is the final result */
1388  if (SAMESIGN(result, fctx->step) && !SAMESIGN(result, fctx->current))
1389  fctx->step = 0;
1390 
1391  /* do when there is more left to send */
1392  SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
1393  }
1394  else
1395  /* do when there is no more left */
1396  SRF_RETURN_DONE(funcctx);
1397 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
#define SAMESIGN(a, b)
Definition: int.c:41
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
signed int int32
Definition: c.h:284
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int32 current
Definition: int.c:47
int32 finish
Definition: int.c:48
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:110
#define PG_NARGS()
Definition: fmgr.h:168
#define Int32GetDatum(X)
Definition: postgres.h:485
void * user_fctx
Definition: funcapi.h:91
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295

◆ i2toi4()

Datum i2toi4 ( PG_FUNCTION_ARGS  )

Definition at line 319 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT32.

320 {
321  int16 arg1 = PG_GETARG_INT16(0);
322 
323  PG_RETURN_INT32((int32) arg1);
324 }
signed short int16
Definition: c.h:283
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
signed int int32
Definition: c.h:284
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ i4toi2()

Datum i4toi2 ( PG_FUNCTION_ARGS  )

Definition at line 327 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, and PG_RETURN_INT16.

328 {
329  int32 arg1 = PG_GETARG_INT32(0);
330 
331  if (arg1 < SHRT_MIN || arg1 > SHRT_MAX)
332  ereport(ERROR,
333  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
334  errmsg("smallint out of range")));
335 
336  PG_RETURN_INT16((int16) arg1);
337 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ int24div()

Datum int24div ( PG_FUNCTION_ARGS  )

Definition at line 945 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_GETARG_INT32, PG_RETURN_INT32, and PG_RETURN_NULL.

946 {
947  int16 arg1 = PG_GETARG_INT16(0);
948  int32 arg2 = PG_GETARG_INT32(1);
949 
950  if (arg2 == 0)
951  {
952  ereport(ERROR,
953  (errcode(ERRCODE_DIVISION_BY_ZERO),
954  errmsg("division by zero")));
955  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
956  PG_RETURN_NULL();
957  }
958 
959  /* No overflow is possible */
960  PG_RETURN_INT32((int32) arg1 / arg2);
961 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_RETURN_NULL()
Definition: fmgr.h:305

◆ int24eq()

Datum int24eq ( PG_FUNCTION_ARGS  )

Definition at line 483 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

484 {
485  int16 arg1 = PG_GETARG_INT16(0);
486  int32 arg2 = PG_GETARG_INT32(1);
487 
488  PG_RETURN_BOOL(arg1 == arg2);
489 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int24ge()

Datum int24ge ( PG_FUNCTION_ARGS  )

Definition at line 528 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

529 {
530  int16 arg1 = PG_GETARG_INT16(0);
531  int32 arg2 = PG_GETARG_INT32(1);
532 
533  PG_RETURN_BOOL(arg1 >= arg2);
534 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int24gt()

Datum int24gt ( PG_FUNCTION_ARGS  )

Definition at line 519 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

520 {
521  int16 arg1 = PG_GETARG_INT16(0);
522  int32 arg2 = PG_GETARG_INT32(1);
523 
524  PG_RETURN_BOOL(arg1 > arg2);
525 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int24le()

Datum int24le ( PG_FUNCTION_ARGS  )

Definition at line 510 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

511 {
512  int16 arg1 = PG_GETARG_INT16(0);
513  int32 arg2 = PG_GETARG_INT32(1);
514 
515  PG_RETURN_BOOL(arg1 <= arg2);
516 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int24lt()

Datum int24lt ( PG_FUNCTION_ARGS  )

Definition at line 501 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

502 {
503  int16 arg1 = PG_GETARG_INT16(0);
504  int32 arg2 = PG_GETARG_INT32(1);
505 
506  PG_RETURN_BOOL(arg1 < arg2);
507 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int24mi()

Datum int24mi ( PG_FUNCTION_ARGS  )

Definition at line 897 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_GETARG_INT32, PG_RETURN_INT32, and SAMESIGN.

898 {
899  int16 arg1 = PG_GETARG_INT16(0);
900  int32 arg2 = PG_GETARG_INT32(1);
901  int32 result;
902 
903  result = arg1 - arg2;
904 
905  /*
906  * Overflow check. If the inputs are of the same sign then their
907  * difference cannot overflow. If they are of different signs then the
908  * result should be of the same sign as the first input.
909  */
910  if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
911  ereport(ERROR,
912  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
913  errmsg("integer out of range")));
914  PG_RETURN_INT32(result);
915 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define SAMESIGN(a, b)
Definition: int.c:41
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ int24mul()

Datum int24mul ( PG_FUNCTION_ARGS  )

Definition at line 918 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_INT32.

919 {
920  int16 arg1 = PG_GETARG_INT16(0);
921  int32 arg2 = PG_GETARG_INT32(1);
922  int32 result;
923 
924  result = arg1 * arg2;
925 
926  /*
927  * Overflow check. We basically check to see if result / arg2 gives arg1
928  * again. There is one case where this fails: arg2 = 0 (which cannot
929  * overflow).
930  *
931  * Since the division is likely much more expensive than the actual
932  * multiplication, we'd like to skip it where possible. The best bang for
933  * the buck seems to be to check whether both inputs are in the int16
934  * range; if so, no overflow is possible.
935  */
936  if (!(arg2 >= (int32) SHRT_MIN && arg2 <= (int32) SHRT_MAX) &&
937  result / arg2 != arg1)
938  ereport(ERROR,
939  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
940  errmsg("integer out of range")));
941  PG_RETURN_INT32(result);
942 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ int24ne()

Datum int24ne ( PG_FUNCTION_ARGS  )

Definition at line 492 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

493 {
494  int16 arg1 = PG_GETARG_INT16(0);
495  int32 arg2 = PG_GETARG_INT32(1);
496 
497  PG_RETURN_BOOL(arg1 != arg2);
498 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int24pl()

Datum int24pl ( PG_FUNCTION_ARGS  )

Definition at line 876 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_GETARG_INT32, PG_RETURN_INT32, and SAMESIGN.

877 {
878  int16 arg1 = PG_GETARG_INT16(0);
879  int32 arg2 = PG_GETARG_INT32(1);
880  int32 result;
881 
882  result = arg1 + arg2;
883 
884  /*
885  * Overflow check. If the inputs are of different signs then their sum
886  * cannot overflow. If the inputs are of the same sign, their sum had
887  * better be that sign too.
888  */
889  if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
890  ereport(ERROR,
891  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
892  errmsg("integer out of range")));
893  PG_RETURN_INT32(result);
894 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define SAMESIGN(a, b)
Definition: int.c:41
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ int2abs()

Datum int2abs ( PG_FUNCTION_ARGS  )

Definition at line 1149 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, and PG_RETURN_INT16.

1150 {
1151  int16 arg1 = PG_GETARG_INT16(0);
1152  int16 result;
1153 
1154  result = (arg1 < 0) ? -arg1 : arg1;
1155  /* overflow check (needed for SHRT_MIN) */
1156  if (result < 0)
1157  ereport(ERROR,
1158  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1159  errmsg("smallint out of range")));
1160  PG_RETURN_INT16(result);
1161 }
signed short int16
Definition: c.h:283
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ int2and()

Datum int2and ( PG_FUNCTION_ARGS  )

Definition at line 1264 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1265 {
1266  int16 arg1 = PG_GETARG_INT16(0);
1267  int16 arg2 = PG_GETARG_INT16(1);
1268 
1269  PG_RETURN_INT16(arg1 & arg2);
1270 }
signed short int16
Definition: c.h:283
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int2div()

Datum int2div ( PG_FUNCTION_ARGS  )

Definition at line 836 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_RETURN_INT16, PG_RETURN_NULL, and SAMESIGN.

837 {
838  int16 arg1 = PG_GETARG_INT16(0);
839  int16 arg2 = PG_GETARG_INT16(1);
840  int16 result;
841 
842  if (arg2 == 0)
843  {
844  ereport(ERROR,
845  (errcode(ERRCODE_DIVISION_BY_ZERO),
846  errmsg("division by zero")));
847  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
848  PG_RETURN_NULL();
849  }
850 
851  /*
852  * SHRT_MIN / -1 is problematic, since the result can't be represented on
853  * a two's-complement machine. Some machines produce SHRT_MIN, some
854  * produce zero, some throw an exception. We can dodge the problem by
855  * recognizing that division by -1 is the same as negation.
856  */
857  if (arg2 == -1)
858  {
859  result = -arg1;
860  /* overflow check (needed for SHRT_MIN) */
861  if (arg1 != 0 && SAMESIGN(result, arg1))
862  ereport(ERROR,
863  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
864  errmsg("smallint out of range")));
865  PG_RETURN_INT16(result);
866  }
867 
868  /* No overflow is possible */
869 
870  result = arg1 / arg2;
871 
872  PG_RETURN_INT16(result);
873 }
signed short int16
Definition: c.h:283
#define SAMESIGN(a, b)
Definition: int.c:41
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_RETURN_NULL()
Definition: fmgr.h:305

◆ int2eq()

Datum int2eq ( PG_FUNCTION_ARGS  )

Definition at line 429 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_BOOL.

430 {
431  int16 arg1 = PG_GETARG_INT16(0);
432  int16 arg2 = PG_GETARG_INT16(1);
433 
434  PG_RETURN_BOOL(arg1 == arg2);
435 }
signed short int16
Definition: c.h:283
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int2ge()

Datum int2ge ( PG_FUNCTION_ARGS  )

Definition at line 474 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_BOOL.

475 {
476  int16 arg1 = PG_GETARG_INT16(0);
477  int16 arg2 = PG_GETARG_INT16(1);
478 
479  PG_RETURN_BOOL(arg1 >= arg2);
480 }
signed short int16
Definition: c.h:283
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int2gt()

Datum int2gt ( PG_FUNCTION_ARGS  )

Definition at line 465 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_BOOL.

466 {
467  int16 arg1 = PG_GETARG_INT16(0);
468  int16 arg2 = PG_GETARG_INT16(1);
469 
470  PG_RETURN_BOOL(arg1 > arg2);
471 }
signed short int16
Definition: c.h:283
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int2in()

Datum int2in ( PG_FUNCTION_ARGS  )

Definition at line 61 of file int.c.

References pg_atoi(), PG_GETARG_CSTRING, and PG_RETURN_INT16.

62 {
63  char *num = PG_GETARG_CSTRING(0);
64 
65  PG_RETURN_INT16(pg_atoi(num, sizeof(int16), '\0'));
66 }
signed short int16
Definition: c.h:283
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
int32 pg_atoi(const char *s, int size, int c)
Definition: numutils.c:37

◆ int2larger()

Datum int2larger ( PG_FUNCTION_ARGS  )

Definition at line 1164 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1165 {
1166  int16 arg1 = PG_GETARG_INT16(0);
1167  int16 arg2 = PG_GETARG_INT16(1);
1168 
1169  PG_RETURN_INT16((arg1 > arg2) ? arg1 : arg2);
1170 }
signed short int16
Definition: c.h:283
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int2le()

Datum int2le ( PG_FUNCTION_ARGS  )

Definition at line 456 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_BOOL.

457 {
458  int16 arg1 = PG_GETARG_INT16(0);
459  int16 arg2 = PG_GETARG_INT16(1);
460 
461  PG_RETURN_BOOL(arg1 <= arg2);
462 }
signed short int16
Definition: c.h:283
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int2lt()

Datum int2lt ( PG_FUNCTION_ARGS  )

Definition at line 447 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_BOOL.

448 {
449  int16 arg1 = PG_GETARG_INT16(0);
450  int16 arg2 = PG_GETARG_INT16(1);
451 
452  PG_RETURN_BOOL(arg1 < arg2);
453 }
signed short int16
Definition: c.h:283
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int2mi()

Datum int2mi ( PG_FUNCTION_ARGS  )

Definition at line 794 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_RETURN_INT16, and SAMESIGN.

795 {
796  int16 arg1 = PG_GETARG_INT16(0);
797  int16 arg2 = PG_GETARG_INT16(1);
798  int16 result;
799 
800  result = arg1 - arg2;
801 
802  /*
803  * Overflow check. If the inputs are of the same sign then their
804  * difference cannot overflow. If they are of different signs then the
805  * result should be of the same sign as the first input.
806  */
807  if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
808  ereport(ERROR,
809  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
810  errmsg("smallint out of range")));
811  PG_RETURN_INT16(result);
812 }
signed short int16
Definition: c.h:283
#define SAMESIGN(a, b)
Definition: int.c:41
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ int2mod()

Datum int2mod ( PG_FUNCTION_ARGS  )

Definition at line 1101 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_RETURN_INT16, and PG_RETURN_NULL.

1102 {
1103  int16 arg1 = PG_GETARG_INT16(0);
1104  int16 arg2 = PG_GETARG_INT16(1);
1105 
1106  if (arg2 == 0)
1107  {
1108  ereport(ERROR,
1109  (errcode(ERRCODE_DIVISION_BY_ZERO),
1110  errmsg("division by zero")));
1111  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1112  PG_RETURN_NULL();
1113  }
1114 
1115  /*
1116  * Some machines throw a floating-point exception for INT_MIN % -1, which
1117  * is a bit silly since the correct answer is perfectly well-defined,
1118  * namely zero. (It's not clear this ever happens when dealing with
1119  * int16, but we might as well have the test for safety.)
1120  */
1121  if (arg2 == -1)
1122  PG_RETURN_INT16(0);
1123 
1124  /* No overflow is possible */
1125 
1126  PG_RETURN_INT16(arg1 % arg2);
1127 }
signed short int16
Definition: c.h:283
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_RETURN_NULL()
Definition: fmgr.h:305

◆ int2mul()

Datum int2mul ( PG_FUNCTION_ARGS  )

Definition at line 815 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, and PG_RETURN_INT16.

816 {
817  int16 arg1 = PG_GETARG_INT16(0);
818  int16 arg2 = PG_GETARG_INT16(1);
819  int32 result32;
820 
821  /*
822  * The most practical way to detect overflow is to do the arithmetic in
823  * int32 (so that the result can't overflow) and then do a range check.
824  */
825  result32 = (int32) arg1 * (int32) arg2;
826 
827  if (result32 < SHRT_MIN || result32 > SHRT_MAX)
828  ereport(ERROR,
829  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
830  errmsg("smallint out of range")));
831 
832  PG_RETURN_INT16((int16) result32);
833 }
signed short int16
Definition: c.h:283
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ int2ne()

Datum int2ne ( PG_FUNCTION_ARGS  )

Definition at line 438 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_BOOL.

439 {
440  int16 arg1 = PG_GETARG_INT16(0);
441  int16 arg2 = PG_GETARG_INT16(1);
442 
443  PG_RETURN_BOOL(arg1 != arg2);
444 }
signed short int16
Definition: c.h:283
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int2not()

Datum int2not ( PG_FUNCTION_ARGS  )

Definition at line 1291 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1292 {
1293  int16 arg1 = PG_GETARG_INT16(0);
1294 
1295  PG_RETURN_INT16(~arg1);
1296 }
signed short int16
Definition: c.h:283
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int2or()

Datum int2or ( PG_FUNCTION_ARGS  )

Definition at line 1273 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1274 {
1275  int16 arg1 = PG_GETARG_INT16(0);
1276  int16 arg2 = PG_GETARG_INT16(1);
1277 
1278  PG_RETURN_INT16(arg1 | arg2);
1279 }
signed short int16
Definition: c.h:283
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int2out()

Datum int2out ( PG_FUNCTION_ARGS  )

Definition at line 72 of file int.c.

References palloc(), PG_GETARG_INT16, pg_itoa(), and PG_RETURN_CSTRING.

73 {
74  int16 arg1 = PG_GETARG_INT16(0);
75  char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */
76 
77  pg_itoa(arg1, result);
78  PG_RETURN_CSTRING(result);
79 }
signed short int16
Definition: c.h:283
void pg_itoa(int16 i, char *a)
Definition: numutils.c:120
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
void * palloc(Size size)
Definition: mcxt.c:848

◆ int2pl()

Datum int2pl ( PG_FUNCTION_ARGS  )

Definition at line 773 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_RETURN_INT16, and SAMESIGN.

774 {
775  int16 arg1 = PG_GETARG_INT16(0);
776  int16 arg2 = PG_GETARG_INT16(1);
777  int16 result;
778 
779  result = arg1 + arg2;
780 
781  /*
782  * Overflow check. If the inputs are of different signs then their sum
783  * cannot overflow. If the inputs are of the same sign, their sum had
784  * better be that sign too.
785  */
786  if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
787  ereport(ERROR,
788  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
789  errmsg("smallint out of range")));
790  PG_RETURN_INT16(result);
791 }
signed short int16
Definition: c.h:283
#define SAMESIGN(a, b)
Definition: int.c:41
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ int2recv()

Datum int2recv ( PG_FUNCTION_ARGS  )

Definition at line 85 of file int.c.

References buf, PG_GETARG_POINTER, PG_RETURN_INT16, and pq_getmsgint().

86 {
88 
89  PG_RETURN_INT16((int16) pq_getmsgint(buf, sizeof(int16)));
90 }
signed short int16
Definition: c.h:283
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
static char * buf
Definition: pg_test_fsync.c:67
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417

◆ int2send()

Datum int2send ( PG_FUNCTION_ARGS  )

Definition at line 96 of file int.c.

References buf, PG_GETARG_INT16, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), and pq_sendint16().

97 {
98  int16 arg1 = PG_GETARG_INT16(0);
100 
101  pq_begintypsend(&buf);
102  pq_sendint16(&buf, arg1);
104 }
signed short int16
Definition: c.h:283
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
static char * buf
Definition: pg_test_fsync.c:67
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
static void pq_sendint16(StringInfo buf, int16 i)
Definition: pqformat.h:140

◆ int2shl()

Datum int2shl ( PG_FUNCTION_ARGS  )

Definition at line 1300 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_INT16.

1301 {
1302  int16 arg1 = PG_GETARG_INT16(0);
1303  int32 arg2 = PG_GETARG_INT32(1);
1304 
1305  PG_RETURN_INT16(arg1 << arg2);
1306 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
signed int int32
Definition: c.h:284
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int2shr()

Datum int2shr ( PG_FUNCTION_ARGS  )

Definition at line 1309 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_INT16.

1310 {
1311  int16 arg1 = PG_GETARG_INT16(0);
1312  int32 arg2 = PG_GETARG_INT32(1);
1313 
1314  PG_RETURN_INT16(arg1 >> arg2);
1315 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
signed int int32
Definition: c.h:284
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int2smaller()

Datum int2smaller ( PG_FUNCTION_ARGS  )

Definition at line 1173 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1174 {
1175  int16 arg1 = PG_GETARG_INT16(0);
1176  int16 arg2 = PG_GETARG_INT16(1);
1177 
1178  PG_RETURN_INT16((arg1 < arg2) ? arg1 : arg2);
1179 }
signed short int16
Definition: c.h:283
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int2um()

Datum int2um ( PG_FUNCTION_ARGS  )

Definition at line 750 of file int.c.

References arg, ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_RETURN_INT16, and SAMESIGN.

751 {
753  int16 result;
754 
755  result = -arg;
756  /* overflow check (needed for SHRT_MIN) */
757  if (arg != 0 && SAMESIGN(result, arg))
758  ereport(ERROR,
759  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
760  errmsg("smallint out of range")));
761  PG_RETURN_INT16(result);
762 }
signed short int16
Definition: c.h:283
#define SAMESIGN(a, b)
Definition: int.c:41
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * arg

◆ int2up()

Datum int2up ( PG_FUNCTION_ARGS  )

Definition at line 765 of file int.c.

References arg, PG_GETARG_INT16, and PG_RETURN_INT16.

766 {
768 
769  PG_RETURN_INT16(arg);
770 }
signed short int16
Definition: c.h:283
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
void * arg

◆ int2vectorin()

Datum int2vectorin ( PG_FUNCTION_ARGS  )

Definition at line 139 of file int.c.

References int2vector::dataoffset, int2vector::dim1, int2vector::elemtype, ereport, errcode(), errmsg(), ERROR, FUNC_MAX_ARGS, INT2OID, Int2VectorSize, int2vector::lbound1, int2vector::ndim, palloc0(), pg_atoi(), PG_GETARG_CSTRING, PG_RETURN_POINTER, SET_VARSIZE, and int2vector::values.

140 {
141  char *intString = PG_GETARG_CSTRING(0);
142  int2vector *result;
143  int n;
144 
146 
147  for (n = 0; *intString && n < FUNC_MAX_ARGS; n++)
148  {
149  while (*intString && isspace((unsigned char) *intString))
150  intString++;
151  if (*intString == '\0')
152  break;
153  result->values[n] = pg_atoi(intString, sizeof(int16), ' ');
154  while (*intString && !isspace((unsigned char) *intString))
155  intString++;
156  }
157  while (*intString && isspace((unsigned char) *intString))
158  intString++;
159  if (*intString)
160  ereport(ERROR,
161  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
162  errmsg("int2vector has too many elements")));
163 
164  SET_VARSIZE(result, Int2VectorSize(n));
165  result->ndim = 1;
166  result->dataoffset = 0; /* never any nulls */
167  result->elemtype = INT2OID;
168  result->dim1 = n;
169  result->lbound1 = 0;
170 
171  PG_RETURN_POINTER(result);
172 }
signed short int16
Definition: c.h:283
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
int lbound1
Definition: c.h:522
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid elemtype
Definition: c.h:520
#define FUNC_MAX_ARGS
int32 dataoffset
Definition: c.h:519
#define ERROR
Definition: elog.h:43
#define INT2OID
Definition: pg_type.h:308
int ndim
Definition: c.h:518
#define ereport(elevel, rest)
Definition: elog.h:122
Definition: c.h:515
void * palloc0(Size size)
Definition: mcxt.c:877
int dim1
Definition: c.h:521
int16 values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:523
#define Int2VectorSize(n)
Definition: int.c:43
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
int32 pg_atoi(const char *s, int size, int c)
Definition: numutils.c:37

◆ int2vectorout()

Datum int2vectorout ( PG_FUNCTION_ARGS  )

Definition at line 178 of file int.c.

References int2vector::dim1, palloc(), PG_GETARG_POINTER, pg_itoa(), PG_RETURN_CSTRING, and int2vector::values.

179 {
180  int2vector *int2Array = (int2vector *) PG_GETARG_POINTER(0);
181  int num,
182  nnums = int2Array->dim1;
183  char *rp;
184  char *result;
185 
186  /* assumes sign, 5 digits, ' ' */
187  rp = result = (char *) palloc(nnums * 7 + 1);
188  for (num = 0; num < nnums; num++)
189  {
190  if (num != 0)
191  *rp++ = ' ';
192  pg_itoa(int2Array->values[num], rp);
193  while (*++rp != '\0')
194  ;
195  }
196  *rp = '\0';
197  PG_RETURN_CSTRING(result);
198 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
void pg_itoa(int16 i, char *a)
Definition: numutils.c:120
Definition: c.h:515
int dim1
Definition: c.h:521
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
int16 values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:523
void * palloc(Size size)
Definition: mcxt.c:848

◆ int2vectorrecv()

Datum int2vectorrecv ( PG_FUNCTION_ARGS  )

Definition at line 204 of file int.c.

References FunctionCallInfoData::arg, FunctionCallInfoData::argnull, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_LBOUND, ARR_NDIM, array_recv(), Assert, buf, DatumGetPointer, ereport, errcode(), errmsg(), ERROR, FunctionCallInfoData::flinfo, FUNC_MAX_ARGS, InitFunctionCallInfoData, INT2OID, Int32GetDatum, InvalidOid, FunctionCallInfoData::isnull, ObjectIdGetDatum, PG_GETARG_POINTER, PG_RETURN_POINTER, and PointerGetDatum.

205 {
207  FunctionCallInfoData locfcinfo;
208  int2vector *result;
209 
210  /*
211  * Normally one would call array_recv() using DirectFunctionCall3, but
212  * that does not work since array_recv wants to cache some data using
213  * fcinfo->flinfo->fn_extra. So we need to pass it our own flinfo
214  * parameter.
215  */
216  InitFunctionCallInfoData(locfcinfo, fcinfo->flinfo, 3,
217  InvalidOid, NULL, NULL);
218 
219  locfcinfo.arg[0] = PointerGetDatum(buf);
220  locfcinfo.arg[1] = ObjectIdGetDatum(INT2OID);
221  locfcinfo.arg[2] = Int32GetDatum(-1);
222  locfcinfo.argnull[0] = false;
223  locfcinfo.argnull[1] = false;
224  locfcinfo.argnull[2] = false;
225 
226  result = (int2vector *) DatumGetPointer(array_recv(&locfcinfo));
227 
228  Assert(!locfcinfo.isnull);
229 
230  /* sanity checks: int2vector must be 1-D, 0-based, no nulls */
231  if (ARR_NDIM(result) != 1 ||
232  ARR_HASNULL(result) ||
233  ARR_ELEMTYPE(result) != INT2OID ||
234  ARR_LBOUND(result)[0] != 0)
235  ereport(ERROR,
236  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
237  errmsg("invalid int2vector data")));
238 
239  /* check length for consistency with int2vectorin() */
240  if (ARR_DIMS(result)[0] > FUNC_MAX_ARGS)
241  ereport(ERROR,
242  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
243  errmsg("oidvector has too many elements")));
244 
245  PG_RETURN_POINTER(result);
246 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PointerGetDatum(X)
Definition: postgres.h:562
StringInfoData * StringInfo
Definition: stringinfo.h:43
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
Datum array_recv(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1255
#define ARR_LBOUND(a)
Definition: array.h:281
#define FUNC_MAX_ARGS
FmgrInfo * flinfo
Definition: fmgr.h:79
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:279
#define INT2OID
Definition: pg_type.h:308
static char * buf
Definition: pg_test_fsync.c:67
#define ARR_HASNULL(a)
Definition: array.h:276
bool argnull[FUNC_MAX_ARGS]
Definition: fmgr.h:86
#define ereport(elevel, rest)
Definition: elog.h:122
Definition: c.h:515
#define InvalidOid
Definition: postgres_ext.h:36
Datum arg[FUNC_MAX_ARGS]
Definition: fmgr.h:85
#define Assert(condition)
Definition: c.h:670
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:120
#define ARR_NDIM(a)
Definition: array.h:275
#define DatumGetPointer(X)
Definition: postgres.h:555
#define Int32GetDatum(X)
Definition: postgres.h:485
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define ARR_ELEMTYPE(a)
Definition: array.h:277

◆ int2vectorsend()

Datum int2vectorsend ( PG_FUNCTION_ARGS  )

Definition at line 252 of file int.c.

References array_send().

253 {
254  return array_send(fcinfo);
255 }
Datum array_send(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1535

◆ int2xor()

Datum int2xor ( PG_FUNCTION_ARGS  )

Definition at line 1282 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1283 {
1284  int16 arg1 = PG_GETARG_INT16(0);
1285  int16 arg2 = PG_GETARG_INT16(1);
1286 
1287  PG_RETURN_INT16(arg1 ^ arg2);
1288 }
signed short int16
Definition: c.h:283
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int42div()

Datum int42div ( PG_FUNCTION_ARGS  )

Definition at line 1033 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_GETARG_INT32, PG_RETURN_INT32, PG_RETURN_NULL, and SAMESIGN.

1034 {
1035  int32 arg1 = PG_GETARG_INT32(0);
1036  int16 arg2 = PG_GETARG_INT16(1);
1037  int32 result;
1038 
1039  if (arg2 == 0)
1040  {
1041  ereport(ERROR,
1042  (errcode(ERRCODE_DIVISION_BY_ZERO),
1043  errmsg("division by zero")));
1044  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1045  PG_RETURN_NULL();
1046  }
1047 
1048  /*
1049  * INT_MIN / -1 is problematic, since the result can't be represented on a
1050  * two's-complement machine. Some machines produce INT_MIN, some produce
1051  * zero, some throw an exception. We can dodge the problem by recognizing
1052  * that division by -1 is the same as negation.
1053  */
1054  if (arg2 == -1)
1055  {
1056  result = -arg1;
1057  /* overflow check (needed for INT_MIN) */
1058  if (arg1 != 0 && SAMESIGN(result, arg1))
1059  ereport(ERROR,
1060  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1061  errmsg("integer out of range")));
1062  PG_RETURN_INT32(result);
1063  }
1064 
1065  /* No overflow is possible */
1066 
1067  result = arg1 / arg2;
1068 
1069  PG_RETURN_INT32(result);
1070 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define SAMESIGN(a, b)
Definition: int.c:41
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_RETURN_NULL()
Definition: fmgr.h:305

◆ int42eq()

Datum int42eq ( PG_FUNCTION_ARGS  )

Definition at line 537 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

538 {
539  int32 arg1 = PG_GETARG_INT32(0);
540  int16 arg2 = PG_GETARG_INT16(1);
541 
542  PG_RETURN_BOOL(arg1 == arg2);
543 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int42ge()

Datum int42ge ( PG_FUNCTION_ARGS  )

Definition at line 582 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

583 {
584  int32 arg1 = PG_GETARG_INT32(0);
585  int16 arg2 = PG_GETARG_INT16(1);
586 
587  PG_RETURN_BOOL(arg1 >= arg2);
588 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int42gt()

Datum int42gt ( PG_FUNCTION_ARGS  )

Definition at line 573 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

574 {
575  int32 arg1 = PG_GETARG_INT32(0);
576  int16 arg2 = PG_GETARG_INT16(1);
577 
578  PG_RETURN_BOOL(arg1 > arg2);
579 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int42le()

Datum int42le ( PG_FUNCTION_ARGS  )

Definition at line 564 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

565 {
566  int32 arg1 = PG_GETARG_INT32(0);
567  int16 arg2 = PG_GETARG_INT16(1);
568 
569  PG_RETURN_BOOL(arg1 <= arg2);
570 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int42lt()

Datum int42lt ( PG_FUNCTION_ARGS  )

Definition at line 555 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

556 {
557  int32 arg1 = PG_GETARG_INT32(0);
558  int16 arg2 = PG_GETARG_INT16(1);
559 
560  PG_RETURN_BOOL(arg1 < arg2);
561 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int42mi()

Datum int42mi ( PG_FUNCTION_ARGS  )

Definition at line 985 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_GETARG_INT32, PG_RETURN_INT32, and SAMESIGN.

986 {
987  int32 arg1 = PG_GETARG_INT32(0);
988  int16 arg2 = PG_GETARG_INT16(1);
989  int32 result;
990 
991  result = arg1 - arg2;
992 
993  /*
994  * Overflow check. If the inputs are of the same sign then their
995  * difference cannot overflow. If they are of different signs then the
996  * result should be of the same sign as the first input.
997  */
998  if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
999  ereport(ERROR,
1000  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1001  errmsg("integer out of range")));
1002  PG_RETURN_INT32(result);
1003 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define SAMESIGN(a, b)
Definition: int.c:41
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ int42mul()

Datum int42mul ( PG_FUNCTION_ARGS  )

Definition at line 1006 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_INT32.

1007 {
1008  int32 arg1 = PG_GETARG_INT32(0);
1009  int16 arg2 = PG_GETARG_INT16(1);
1010  int32 result;
1011 
1012  result = arg1 * arg2;
1013 
1014  /*
1015  * Overflow check. We basically check to see if result / arg1 gives arg2
1016  * again. There is one case where this fails: arg1 = 0 (which cannot
1017  * overflow).
1018  *
1019  * Since the division is likely much more expensive than the actual
1020  * multiplication, we'd like to skip it where possible. The best bang for
1021  * the buck seems to be to check whether both inputs are in the int16
1022  * range; if so, no overflow is possible.
1023  */
1024  if (!(arg1 >= (int32) SHRT_MIN && arg1 <= (int32) SHRT_MAX) &&
1025  result / arg1 != arg2)
1026  ereport(ERROR,
1027  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1028  errmsg("integer out of range")));
1029  PG_RETURN_INT32(result);
1030 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ int42ne()

Datum int42ne ( PG_FUNCTION_ARGS  )

Definition at line 546 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

547 {
548  int32 arg1 = PG_GETARG_INT32(0);
549  int16 arg2 = PG_GETARG_INT16(1);
550 
551  PG_RETURN_BOOL(arg1 != arg2);
552 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236

◆ int42pl()

Datum int42pl ( PG_FUNCTION_ARGS  )

Definition at line 964 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_GETARG_INT32, PG_RETURN_INT32, and SAMESIGN.

965 {
966  int32 arg1 = PG_GETARG_INT32(0);
967  int16 arg2 = PG_GETARG_INT16(1);
968  int32 result;
969 
970  result = arg1 + arg2;
971 
972  /*
973  * Overflow check. If the inputs are of different signs then their sum
974  * cannot overflow. If the inputs are of the same sign, their sum had
975  * better be that sign too.
976  */
977  if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
978  ereport(ERROR,
979  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
980  errmsg("integer out of range")));
981  PG_RETURN_INT32(result);
982 }
signed short int16
Definition: c.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define SAMESIGN(a, b)
Definition: int.c:41
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ int4_bool()

Datum int4_bool ( PG_FUNCTION_ARGS  )

Definition at line 341 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_BOOL.

342 {
343  if (PG_GETARG_INT32(0) == 0)
344  PG_RETURN_BOOL(false);
345  else
346  PG_RETURN_BOOL(true);
347 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319

◆ int4abs()

Datum int4abs ( PG_FUNCTION_ARGS  )

Definition at line 1134 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, and PG_RETURN_INT32.

1135 {
1136  int32 arg1 = PG_GETARG_INT32(0);
1137  int32 result;
1138 
1139  result = (arg1 < 0) ? -arg1 : arg1;
1140  /* overflow check (needed for INT_MIN) */
1141  if (result < 0)
1142  ereport(ERROR,
1143  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1144  errmsg("integer out of range")));
1145  PG_RETURN_INT32(result);
1146 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ int4and()

Datum int4and ( PG_FUNCTION_ARGS  )

Definition at line 1211 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

1212 {
1213  int32 arg1 = PG_GETARG_INT32(0);
1214  int32 arg2 = PG_GETARG_INT32(1);
1215 
1216  PG_RETURN_INT32(arg1 & arg2);
1217 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
signed int int32
Definition: c.h:284

◆ int4div()

Datum int4div ( PG_FUNCTION_ARGS  )

Definition at line 694 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, PG_RETURN_INT32, PG_RETURN_NULL, and SAMESIGN.

695 {
696  int32 arg1 = PG_GETARG_INT32(0);
697  int32 arg2 = PG_GETARG_INT32(1);
698  int32 result;
699 
700  if (arg2 == 0)
701  {
702  ereport(ERROR,
703  (errcode(ERRCODE_DIVISION_BY_ZERO),
704  errmsg("division by zero")));
705  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
706  PG_RETURN_NULL();
707  }
708 
709  /*
710  * INT_MIN / -1 is problematic, since the result can't be represented on a
711  * two's-complement machine. Some machines produce INT_MIN, some produce
712  * zero, some throw an exception. We can dodge the problem by recognizing
713  * that division by -1 is the same as negation.
714  */
715  if (arg2 == -1)
716  {
717  result = -arg1;
718  /* overflow check (needed for INT_MIN) */
719  if (arg1 != 0 && SAMESIGN(result, arg1))
720  ereport(ERROR,
721  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
722  errmsg("integer out of range")));
723  PG_RETURN_INT32(result);
724  }
725 
726  /* No overflow is possible */
727 
728  result = arg1 / arg2;
729 
730  PG_RETURN_INT32(result);
731 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define SAMESIGN(a, b)
Definition: int.c:41
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_RETURN_NULL()
Definition: fmgr.h:305

◆ int4eq()

Datum int4eq ( PG_FUNCTION_ARGS  )

Definition at line 375 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_BOOL.

376 {
377  int32 arg1 = PG_GETARG_INT32(0);
378  int32 arg2 = PG_GETARG_INT32(1);
379 
380  PG_RETURN_BOOL(arg1 == arg2);
381 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319

◆ int4ge()

Datum int4ge ( PG_FUNCTION_ARGS  )

Definition at line 420 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_BOOL.

421 {
422  int32 arg1 = PG_GETARG_INT32(0);
423  int32 arg2 = PG_GETARG_INT32(1);
424 
425  PG_RETURN_BOOL(arg1 >= arg2);
426 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319

◆ int4gt()

Datum int4gt ( PG_FUNCTION_ARGS  )

Definition at line 411 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_BOOL.

412 {
413  int32 arg1 = PG_GETARG_INT32(0);
414  int32 arg2 = PG_GETARG_INT32(1);
415 
416  PG_RETURN_BOOL(arg1 > arg2);
417 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319

◆ int4in()

Datum int4in ( PG_FUNCTION_ARGS  )

Definition at line 266 of file int.c.

References pg_atoi(), PG_GETARG_CSTRING, and PG_RETURN_INT32.

Referenced by funny_dup17(), inet_client_port(), inet_server_port(), and pg_stat_get_backend_client_port().

267 {
268  char *num = PG_GETARG_CSTRING(0);
269 
270  PG_RETURN_INT32(pg_atoi(num, sizeof(int32), '\0'));
271 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
signed int int32
Definition: c.h:284
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
int32 pg_atoi(const char *s, int size, int c)
Definition: numutils.c:37

◆ int4inc()

Datum int4inc ( PG_FUNCTION_ARGS  )

Definition at line 734 of file int.c.

References arg, ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, and PG_RETURN_INT32.

735 {
737  int32 result;
738 
739  result = arg + 1;
740  /* Overflow check */
741  if (arg > 0 && result < 0)
742  ereport(ERROR,
743  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
744  errmsg("integer out of range")));
745 
746  PG_RETURN_INT32(result);
747 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * arg

◆ int4larger()

Datum int4larger ( PG_FUNCTION_ARGS  )

Definition at line 1182 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

1183 {
1184  int32 arg1 = PG_GETARG_INT32(0);
1185  int32 arg2 = PG_GETARG_INT32(1);
1186 
1187  PG_RETURN_INT32((arg1 > arg2) ? arg1 : arg2);
1188 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
signed int int32
Definition: c.h:284

◆ int4le()

Datum int4le ( PG_FUNCTION_ARGS  )

Definition at line 402 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_BOOL.

403 {
404  int32 arg1 = PG_GETARG_INT32(0);
405  int32 arg2 = PG_GETARG_INT32(1);
406 
407  PG_RETURN_BOOL(arg1 <= arg2);
408 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319

◆ int4lt()

Datum int4lt ( PG_FUNCTION_ARGS  )

Definition at line 393 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_BOOL.

394 {
395  int32 arg1 = PG_GETARG_INT32(0);
396  int32 arg2 = PG_GETARG_INT32(1);
397 
398  PG_RETURN_BOOL(arg1 < arg2);
399 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319

◆ int4mi()

Datum int4mi ( PG_FUNCTION_ARGS  )

Definition at line 642 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, PG_RETURN_INT32, and SAMESIGN.

643 {
644  int32 arg1 = PG_GETARG_INT32(0);
645  int32 arg2 = PG_GETARG_INT32(1);
646  int32 result;
647 
648  result = arg1 - arg2;
649 
650  /*
651  * Overflow check. If the inputs are of the same sign then their
652  * difference cannot overflow. If they are of different signs then the
653  * result should be of the same sign as the first input.
654  */
655  if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
656  ereport(ERROR,
657  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
658  errmsg("integer out of range")));
659  PG_RETURN_INT32(result);
660 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define SAMESIGN(a, b)
Definition: int.c:41
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ int4mod()

Datum int4mod ( PG_FUNCTION_ARGS  )

Definition at line 1073 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, PG_RETURN_INT32, and PG_RETURN_NULL.

1074 {
1075  int32 arg1 = PG_GETARG_INT32(0);
1076  int32 arg2 = PG_GETARG_INT32(1);
1077 
1078  if (arg2 == 0)
1079  {
1080  ereport(ERROR,
1081  (errcode(ERRCODE_DIVISION_BY_ZERO),
1082  errmsg("division by zero")));
1083  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1084  PG_RETURN_NULL();
1085  }
1086 
1087  /*
1088  * Some machines throw a floating-point exception for INT_MIN % -1, which
1089  * is a bit silly since the correct answer is perfectly well-defined,
1090  * namely zero.
1091  */
1092  if (arg2 == -1)
1093  PG_RETURN_INT32(0);
1094 
1095  /* No overflow is possible */
1096 
1097  PG_RETURN_INT32(arg1 % arg2);
1098 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_RETURN_NULL()
Definition: fmgr.h:305

◆ int4mul()

Datum int4mul ( PG_FUNCTION_ARGS  )

Definition at line 663 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, and PG_RETURN_INT32.

664 {
665  int32 arg1 = PG_GETARG_INT32(0);
666  int32 arg2 = PG_GETARG_INT32(1);
667  int32 result;
668 
669  result = arg1 * arg2;
670 
671  /*
672  * Overflow check. We basically check to see if result / arg2 gives arg1
673  * again. There are two cases where this fails: arg2 = 0 (which cannot
674  * overflow) and arg1 = INT_MIN, arg2 = -1 (where the division itself will
675  * overflow and thus incorrectly match).
676  *
677  * Since the division is likely much more expensive than the actual
678  * multiplication, we'd like to skip it where possible. The best bang for
679  * the buck seems to be to check whether both inputs are in the int16
680  * range; if so, no overflow is possible.
681  */
682  if (!(arg1 >= (int32) SHRT_MIN && arg1 <= (int32) SHRT_MAX &&
683  arg2 >= (int32) SHRT_MIN && arg2 <= (int32) SHRT_MAX) &&
684  arg2 != 0 &&
685  ((arg2 == -1 && arg1 < 0 && result < 0) ||
686  result / arg2 != arg1))
687  ereport(ERROR,
688  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
689  errmsg("integer out of range")));
690  PG_RETURN_INT32(result);
691 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ int4ne()

Datum int4ne ( PG_FUNCTION_ARGS  )

Definition at line 384 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_BOOL.

385 {
386  int32 arg1 = PG_GETARG_INT32(0);
387  int32 arg2 = PG_GETARG_INT32(1);
388 
389  PG_RETURN_BOOL(arg1 != arg2);
390 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319

◆ int4not()

Datum int4not ( PG_FUNCTION_ARGS  )

Definition at line 1256 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

1257 {
1258  int32 arg1 = PG_GETARG_INT32(0);
1259 
1260  PG_RETURN_INT32(~arg1);
1261 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
signed int int32
Definition: c.h:284

◆ int4or()

Datum int4or ( PG_FUNCTION_ARGS  )

Definition at line 1220 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

1221 {
1222  int32 arg1 = PG_GETARG_INT32(0);
1223  int32 arg2 = PG_GETARG_INT32(1);
1224 
1225  PG_RETURN_INT32(arg1 | arg2);
1226 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
signed int int32
Definition: c.h:284

◆ int4out()

Datum int4out ( PG_FUNCTION_ARGS  )

Definition at line 277 of file int.c.

References palloc(), PG_GETARG_INT32, pg_ltoa(), and PG_RETURN_CSTRING.

Referenced by int4_to_char().

278 {
279  int32 arg1 = PG_GETARG_INT32(0);
280  char *result = (char *) palloc(12); /* sign, 10 digits, '\0' */
281 
282  pg_ltoa(arg1, result);
283  PG_RETURN_CSTRING(result);
284 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:284
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
void * palloc(Size size)
Definition: mcxt.c:848
void pg_ltoa(int32 value, char *a)
Definition: numutils.c:132

◆ int4pl()

Datum int4pl ( PG_FUNCTION_ARGS  )

Definition at line 621 of file int.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, PG_RETURN_INT32, and SAMESIGN.

Referenced by int4range_canonical().

622 {
623  int32 arg1 = PG_GETARG_INT32(0);
624  int32 arg2 = PG_GETARG_INT32(1);
625  int32 result;
626 
627  result = arg1 + arg2;
628 
629  /*
630  * Overflow check. If the inputs are of different signs then their sum
631  * cannot overflow. If the inputs are of the same sign, their sum had
632  * better be that sign too.
633  */
634  if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
635  ereport(ERROR,
636  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
637  errmsg("integer out of range")));
638  PG_RETURN_INT32(result);
639 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define SAMESIGN(a, b)
Definition: int.c:41
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ int4recv()

Datum int4recv ( PG_FUNCTION_ARGS  )

Definition at line 290 of file int.c.

References buf, PG_GETARG_POINTER, PG_RETURN_INT32, and pq_getmsgint().

291 {
293 
294  PG_RETURN_INT32((int32) pq_getmsgint(buf, sizeof(int32)));
295 }
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
signed int int32
Definition: c.h:284
static char * buf
Definition: pg_test_fsync.c:67
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417

◆ int4send()

Datum int4send ( PG_FUNCTION_ARGS  )

Definition at line 301 of file int.c.

References buf, PG_GETARG_INT32, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), and pq_sendint32().

302 {
303  int32 arg1 = PG_GETARG_INT32(0);
305 
306  pq_begintypsend(&buf);
307  pq_sendint32(&buf, arg1);
309 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
static void pq_sendint32(StringInfo buf, int32 i)
Definition: pqformat.h:148
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
signed int int32
Definition: c.h:284
static char * buf
Definition: pg_test_fsync.c:67

◆ int4shl()

Datum int4shl ( PG_FUNCTION_ARGS  )

Definition at line 1238 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

1239 {
1240  int32 arg1 = PG_GETARG_INT32(0);
1241  int32 arg2 = PG_GETARG_INT32(1);
1242 
1243  PG_RETURN_INT32(arg1 << arg2);
1244 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
signed int int32
Definition: c.h:284

◆ int4shr()

Datum int4shr ( PG_FUNCTION_ARGS  )

Definition at line 1247 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

1248 {
1249  int32 arg1 = PG_GETARG_INT32(0);
1250  int32 arg2 = PG_GETARG_INT32(1);
1251 
1252  PG_RETURN_INT32(arg1 >> arg2);
1253 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
signed int int32
Definition: c.h:284

◆ int4smaller()

Datum int4smaller ( PG_FUNCTION_ARGS  )

Definition at line 1191 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

1192 {
1193  int32 arg1 = PG_GETARG_INT32(0);
1194  int32 arg2 = PG_GETARG_INT32(1);
1195 
1196  PG_RETURN_INT32((arg1 < arg2) ? arg1 : arg2);
1197 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
signed int int32
Definition: c.h:284

◆ int4um()

Datum int4um ( PG_FUNCTION_ARGS  )

Definition at line 598 of file int.c.

References arg, ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, PG_RETURN_INT32, and SAMESIGN.

599 {
601  int32 result;
602 
603  result = -arg;
604  /* overflow check (needed for INT_MIN) */
605  if (arg != 0 && SAMESIGN(result, arg))
606  ereport(ERROR,
607  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
608  errmsg("integer out of range")));
609  PG_RETURN_INT32(result);
610 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define SAMESIGN(a, b)
Definition: int.c:41
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * arg

◆ int4up()

Datum int4up ( PG_FUNCTION_ARGS  )

Definition at line 613 of file int.c.

References arg, PG_GETARG_INT32, and PG_RETURN_INT32.

614 {
616 
617  PG_RETURN_INT32(arg);
618 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
signed int int32
Definition: c.h:284
void * arg

◆ int4xor()

Datum int4xor ( PG_FUNCTION_ARGS  )

Definition at line 1229 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

1230 {
1231  int32 arg1 = PG_GETARG_INT32(0);
1232  int32 arg2 = PG_GETARG_INT32(1);
1233 
1234  PG_RETURN_INT32(arg1 ^ arg2);
1235 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
signed int int32
Definition: c.h:284