PostgreSQL Source Code  git master
int.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include "catalog/pg_type.h"
#include "common/int.h"
#include "funcapi.h"
#include "libpq/pqformat.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h"
#include "optimizer/optimizer.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 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 in_range_int4_int4 (PG_FUNCTION_ARGS)
 
Datum in_range_int4_int2 (PG_FUNCTION_ARGS)
 
Datum in_range_int4_int8 (PG_FUNCTION_ARGS)
 
Datum in_range_int2_int4 (PG_FUNCTION_ARGS)
 
Datum in_range_int2_int2 (PG_FUNCTION_ARGS)
 
Datum in_range_int2_int8 (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)
 
static int32 int4gcd_internal (int32 arg1, int32 arg2)
 
Datum int4gcd (PG_FUNCTION_ARGS)
 
Datum int4lcm (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)
 
Datum generate_series_int4_support (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ Int2VectorSize

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

Definition at line 45 of file int.c.

Referenced by buildint2vector(), and int2vectorin().

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:353
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274

◆ buildint2vector()

int2vector* buildint2vector ( const int16 int2s,
int  n 
)

Definition at line 114 of file int.c.

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

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

115 {
116  int2vector *result;
117 
118  result = (int2vector *) palloc0(Int2VectorSize(n));
119 
120  if (n > 0 && int2s)
121  memcpy(result->values, int2s, n * sizeof(int16));
122 
123  /*
124  * Attach standard array header. For historical reasons, we set the index
125  * lower bound to 0 not 1.
126  */
127  SET_VARSIZE(result, Int2VectorSize(n));
128  result->ndim = 1;
129  result->dataoffset = 0; /* never any nulls */
130  result->elemtype = INT2OID;
131  result->dim1 = n;
132  result->lbound1 = 0;
133 
134  return result;
135 }
signed short int16
Definition: c.h:361
int lbound1
Definition: c.h:597
Oid elemtype
Definition: c.h:595
int32 dataoffset
Definition: c.h:594
int ndim
Definition: c.h:593
Definition: c.h:590
void * palloc0(Size size)
Definition: mcxt.c:980
int dim1
Definition: c.h:596
int16 values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:598
#define Int2VectorSize(n)
Definition: int.c:45
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ generate_series_int4()

Datum generate_series_int4 ( PG_FUNCTION_ARGS  )

Definition at line 1482 of file int.c.

References generate_series_step_int4().

1483 {
1484  return generate_series_step_int4(fcinfo);
1485 }
Datum generate_series_step_int4(PG_FUNCTION_ARGS)
Definition: int.c:1488

◆ generate_series_int4_support()

Datum generate_series_int4_support ( PG_FUNCTION_ARGS  )

Definition at line 1564 of file int.c.

References generate_unaccent_rules::args, DatumGetInt32, estimate_expression_value(), is_funcclause(), IsA, linitial, list_length(), lsecond, lthird, SupportRequestRows::node, PG_GETARG_POINTER, PG_RETURN_POINTER, SupportRequestRows::root, and SupportRequestRows::rows.

1565 {
1566  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
1567  Node *ret = NULL;
1568 
1569  if (IsA(rawreq, SupportRequestRows))
1570  {
1571  /* Try to estimate the number of rows returned */
1572  SupportRequestRows *req = (SupportRequestRows *) rawreq;
1573 
1574  if (is_funcclause(req->node)) /* be paranoid */
1575  {
1576  List *args = ((FuncExpr *) req->node)->args;
1577  Node *arg1,
1578  *arg2,
1579  *arg3;
1580 
1581  /* We can use estimated argument values here */
1582  arg1 = estimate_expression_value(req->root, linitial(args));
1583  arg2 = estimate_expression_value(req->root, lsecond(args));
1584  if (list_length(args) >= 3)
1585  arg3 = estimate_expression_value(req->root, lthird(args));
1586  else
1587  arg3 = NULL;
1588 
1589  /*
1590  * If any argument is constant NULL, we can safely assume that
1591  * zero rows are returned. Otherwise, if they're all non-NULL
1592  * constants, we can calculate the number of rows that will be
1593  * returned. Use double arithmetic to avoid overflow hazards.
1594  */
1595  if ((IsA(arg1, Const) &&
1596  ((Const *) arg1)->constisnull) ||
1597  (IsA(arg2, Const) &&
1598  ((Const *) arg2)->constisnull) ||
1599  (arg3 != NULL && IsA(arg3, Const) &&
1600  ((Const *) arg3)->constisnull))
1601  {
1602  req->rows = 0;
1603  ret = (Node *) req;
1604  }
1605  else if (IsA(arg1, Const) &&
1606  IsA(arg2, Const) &&
1607  (arg3 == NULL || IsA(arg3, Const)))
1608  {
1609  double start,
1610  finish,
1611  step;
1612 
1613  start = DatumGetInt32(((Const *) arg1)->constvalue);
1614  finish = DatumGetInt32(((Const *) arg2)->constvalue);
1615  step = arg3 ? DatumGetInt32(((Const *) arg3)->constvalue) : 1;
1616 
1617  /* This equation works for either sign of step */
1618  if (step != 0)
1619  {
1620  req->rows = floor((finish - start + step) / step);
1621  ret = (Node *) req;
1622  }
1623  }
1624  }
1625  }
1626 
1627  PG_RETURN_POINTER(ret);
1628 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
#define DatumGetInt32(X)
Definition: postgres.h:472
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2288
Definition: nodes.h:529
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define lsecond(l)
Definition: pg_list.h:200
static bool is_funcclause(const void *clause)
Definition: nodeFuncs.h:56
struct PlannerInfo * root
Definition: supportnodes.h:163
#define linitial(l)
Definition: pg_list.h:195
static int list_length(const List *l)
Definition: pg_list.h:169
#define lthird(l)
Definition: pg_list.h:205
Definition: pg_list.h:50

◆ generate_series_step_int4()

Datum generate_series_step_int4 ( PG_FUNCTION_ARGS  )

Definition at line 1488 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_add_s32_overflow(), PG_GETARG_INT32, PG_NARGS, 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().

1489 {
1490  FuncCallContext *funcctx;
1491  generate_series_fctx *fctx;
1492  int32 result;
1493  MemoryContext oldcontext;
1494 
1495  /* stuff done only on the first call of the function */
1496  if (SRF_IS_FIRSTCALL())
1497  {
1498  int32 start = PG_GETARG_INT32(0);
1499  int32 finish = PG_GETARG_INT32(1);
1500  int32 step = 1;
1501 
1502  /* see if we were given an explicit step size */
1503  if (PG_NARGS() == 3)
1504  step = PG_GETARG_INT32(2);
1505  if (step == 0)
1506  ereport(ERROR,
1507  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1508  errmsg("step size cannot equal zero")));
1509 
1510  /* create a function context for cross-call persistence */
1511  funcctx = SRF_FIRSTCALL_INIT();
1512 
1513  /*
1514  * switch to memory context appropriate for multiple function calls
1515  */
1516  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1517 
1518  /* allocate memory for user context */
1519  fctx = (generate_series_fctx *) palloc(sizeof(generate_series_fctx));
1520 
1521  /*
1522  * Use fctx to keep state from call to call. Seed current with the
1523  * original start value
1524  */
1525  fctx->current = start;
1526  fctx->finish = finish;
1527  fctx->step = step;
1528 
1529  funcctx->user_fctx = fctx;
1530  MemoryContextSwitchTo(oldcontext);
1531  }
1532 
1533  /* stuff done on every call of the function */
1534  funcctx = SRF_PERCALL_SETUP();
1535 
1536  /*
1537  * get the saved state and use current as the result for this iteration
1538  */
1539  fctx = funcctx->user_fctx;
1540  result = fctx->current;
1541 
1542  if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1543  (fctx->step < 0 && fctx->current >= fctx->finish))
1544  {
1545  /*
1546  * Increment current in preparation for next iteration. If next-value
1547  * computation overflows, this is the final result.
1548  */
1549  if (pg_add_s32_overflow(fctx->current, fctx->step, &fctx->current))
1550  fctx->step = 0;
1551 
1552  /* do when there is more left to send */
1553  SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
1554  }
1555  else
1556  /* do when there is no more left */
1557  SRF_RETURN_DONE(funcctx);
1558 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:610
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
signed int int32
Definition: c.h:362
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
int32 current
Definition: int.c:49
int32 finish
Definition: int.c:50
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
#define PG_NARGS()
Definition: fmgr.h:203
#define Int32GetDatum(X)
Definition: postgres.h:479
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
#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:361
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
signed int int32
Definition: c.h:362
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ i4toi2()

Datum i4toi2 ( PG_FUNCTION_ARGS  )

Definition at line 327 of file int.c.

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

328 {
329  int32 arg1 = PG_GETARG_INT32(0);
330 
331  if (unlikely(arg1 < SHRT_MIN) || unlikely(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:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206

◆ in_range_int2_int2()

Datum in_range_int2_int2 ( PG_FUNCTION_ARGS  )

Definition at line 718 of file int.c.

References DirectFunctionCall5, in_range_int2_int4(), Int32GetDatum, PG_GETARG_DATUM, and PG_GETARG_INT16.

719 {
720  /* Doesn't seem worth duplicating code for, so just invoke int2_int4 */
722  PG_GETARG_DATUM(0),
723  PG_GETARG_DATUM(1),
725  PG_GETARG_DATUM(3),
726  PG_GETARG_DATUM(4));
727 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
signed int int32
Definition: c.h:362
Datum in_range_int2_int4(PG_FUNCTION_ARGS)
Definition: int.c:683
#define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5)
Definition: fmgr.h:632
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define Int32GetDatum(X)
Definition: postgres.h:479

◆ in_range_int2_int4()

Datum in_range_int2_int4 ( PG_FUNCTION_ARGS  )

Definition at line 683 of file int.c.

References ereport, errcode(), errmsg(), ERROR, pg_add_s32_overflow(), PG_GETARG_BOOL, PG_GETARG_INT16, PG_GETARG_INT32, PG_RETURN_BOOL, unlikely, and val.

Referenced by in_range_int2_int2().

684 {
685  /* We must do all the math in int32 */
687  int32 base = (int32) PG_GETARG_INT16(1);
688  int32 offset = PG_GETARG_INT32(2);
689  bool sub = PG_GETARG_BOOL(3);
690  bool less = PG_GETARG_BOOL(4);
691  int32 sum;
692 
693  if (offset < 0)
694  ereport(ERROR,
695  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
696  errmsg("invalid preceding or following size in window function")));
697 
698  if (sub)
699  offset = -offset; /* cannot overflow */
700 
701  if (unlikely(pg_add_s32_overflow(base, offset, &sum)))
702  {
703  /*
704  * If sub is false, the true sum is surely more than val, so correct
705  * answer is the same as "less". If sub is true, the true sum is
706  * surely less than val, so the answer is "!less".
707  */
708  PG_RETURN_BOOL(sub ? !less : less);
709  }
710 
711  if (less)
712  PG_RETURN_BOOL(val <= sum);
713  else
714  PG_RETURN_BOOL(val >= sum);
715 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define ereport(elevel,...)
Definition: elog.h:144
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206
long val
Definition: informix.c:664

◆ in_range_int2_int8()

Datum in_range_int2_int8 ( PG_FUNCTION_ARGS  )

Definition at line 730 of file int.c.

References DirectFunctionCall5, in_range_int4_int8(), Int32GetDatum, PG_GETARG_DATUM, and PG_GETARG_INT16.

731 {
732  /* Doesn't seem worth duplicating code for, so just invoke int4_int8 */
736  PG_GETARG_DATUM(2),
737  PG_GETARG_DATUM(3),
738  PG_GETARG_DATUM(4));
739 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum in_range_int4_int8(PG_FUNCTION_ARGS)
Definition: int.c:648
signed int int32
Definition: c.h:362
#define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5)
Definition: fmgr.h:632
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define Int32GetDatum(X)
Definition: postgres.h:479

◆ in_range_int4_int2()

Datum in_range_int4_int2 ( PG_FUNCTION_ARGS  )

Definition at line 636 of file int.c.

References DirectFunctionCall5, in_range_int4_int4(), Int32GetDatum, PG_GETARG_DATUM, and PG_GETARG_INT16.

637 {
638  /* Doesn't seem worth duplicating code for, so just invoke int4_int4 */
640  PG_GETARG_DATUM(0),
641  PG_GETARG_DATUM(1),
643  PG_GETARG_DATUM(3),
644  PG_GETARG_DATUM(4));
645 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
signed int int32
Definition: c.h:362
Datum in_range_int4_int4(PG_FUNCTION_ARGS)
Definition: int.c:602
#define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5)
Definition: fmgr.h:632
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define Int32GetDatum(X)
Definition: postgres.h:479

◆ in_range_int4_int4()

Datum in_range_int4_int4 ( PG_FUNCTION_ARGS  )

Definition at line 602 of file int.c.

References ereport, errcode(), errmsg(), ERROR, pg_add_s32_overflow(), PG_GETARG_BOOL, PG_GETARG_INT32, PG_RETURN_BOOL, unlikely, and val.

Referenced by in_range_int4_int2().

603 {
605  int32 base = PG_GETARG_INT32(1);
606  int32 offset = PG_GETARG_INT32(2);
607  bool sub = PG_GETARG_BOOL(3);
608  bool less = PG_GETARG_BOOL(4);
609  int32 sum;
610 
611  if (offset < 0)
612  ereport(ERROR,
613  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
614  errmsg("invalid preceding or following size in window function")));
615 
616  if (sub)
617  offset = -offset; /* cannot overflow */
618 
619  if (unlikely(pg_add_s32_overflow(base, offset, &sum)))
620  {
621  /*
622  * If sub is false, the true sum is surely more than val, so correct
623  * answer is the same as "less". If sub is true, the true sum is
624  * surely less than val, so the answer is "!less".
625  */
626  PG_RETURN_BOOL(sub ? !less : less);
627  }
628 
629  if (less)
630  PG_RETURN_BOOL(val <= sum);
631  else
632  PG_RETURN_BOOL(val >= sum);
633 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define ereport(elevel,...)
Definition: elog.h:144
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206
long val
Definition: informix.c:664

◆ in_range_int4_int8()

Datum in_range_int4_int8 ( PG_FUNCTION_ARGS  )

Definition at line 648 of file int.c.

References ereport, errcode(), errmsg(), ERROR, pg_add_s64_overflow(), PG_GETARG_BOOL, PG_GETARG_INT32, PG_GETARG_INT64, PG_RETURN_BOOL, unlikely, and val.

Referenced by in_range_int2_int8().

649 {
650  /* We must do all the math in int64 */
651  int64 val = (int64) PG_GETARG_INT32(0);
652  int64 base = (int64) PG_GETARG_INT32(1);
653  int64 offset = PG_GETARG_INT64(2);
654  bool sub = PG_GETARG_BOOL(3);
655  bool less = PG_GETARG_BOOL(4);
656  int64 sum;
657 
658  if (offset < 0)
659  ereport(ERROR,
660  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
661  errmsg("invalid preceding or following size in window function")));
662 
663  if (sub)
664  offset = -offset; /* cannot overflow */
665 
666  if (unlikely(pg_add_s64_overflow(base, offset, &sum)))
667  {
668  /*
669  * If sub is false, the true sum is surely more than val, so correct
670  * answer is the same as "less". If sub is true, the true sum is
671  * surely less than val, so the answer is "!less".
672  */
673  PG_RETURN_BOOL(sub ? !less : less);
674  }
675 
676  if (less)
677  PG_RETURN_BOOL(val <= sum);
678  else
679  PG_RETURN_BOOL(val >= sum);
680 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define ERROR
Definition: elog.h:43
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:161
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206
#define PG_GETARG_INT64(n)
Definition: fmgr.h:282
long val
Definition: informix.c:664

◆ int24div()

Datum int24div ( PG_FUNCTION_ARGS  )

Definition at line 1009 of file int.c.

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

1010 {
1011  int16 arg1 = PG_GETARG_INT16(0);
1012  int32 arg2 = PG_GETARG_INT32(1);
1013 
1014  if (unlikely(arg2 == 0))
1015  {
1016  ereport(ERROR,
1017  (errcode(ERRCODE_DIVISION_BY_ZERO),
1018  errmsg("division by zero")));
1019  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1020  PG_RETURN_NULL();
1021  }
1022 
1023  /* No overflow is possible */
1024  PG_RETURN_INT32((int32) arg1 / arg2);
1025 }
signed short int16
Definition: c.h:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ 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:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ 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:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ 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:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ 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:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ 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:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ int24mi()

Datum int24mi ( PG_FUNCTION_ARGS  )

Definition at line 981 of file int.c.

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

982 {
983  int16 arg1 = PG_GETARG_INT16(0);
984  int32 arg2 = PG_GETARG_INT32(1);
985  int32 result;
986 
987  if (unlikely(pg_sub_s32_overflow((int32) arg1, arg2, &result)))
988  ereport(ERROR,
989  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
990  errmsg("integer out of range")));
991  PG_RETURN_INT32(result);
992 }
signed short int16
Definition: c.h:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
static bool pg_sub_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206

◆ int24mul()

Datum int24mul ( PG_FUNCTION_ARGS  )

Definition at line 995 of file int.c.

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

996 {
997  int16 arg1 = PG_GETARG_INT16(0);
998  int32 arg2 = PG_GETARG_INT32(1);
999  int32 result;
1000 
1001  if (unlikely(pg_mul_s32_overflow((int32) arg1, arg2, &result)))
1002  ereport(ERROR,
1003  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1004  errmsg("integer out of range")));
1005  PG_RETURN_INT32(result);
1006 }
signed short int16
Definition: c.h:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:140
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206

◆ 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:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ int24pl()

Datum int24pl ( PG_FUNCTION_ARGS  )

Definition at line 967 of file int.c.

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

968 {
969  int16 arg1 = PG_GETARG_INT16(0);
970  int32 arg2 = PG_GETARG_INT32(1);
971  int32 result;
972 
973  if (unlikely(pg_add_s32_overflow((int32) arg1, arg2, &result)))
974  ereport(ERROR,
975  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
976  errmsg("integer out of range")));
977  PG_RETURN_INT32(result);
978 }
signed short int16
Definition: c.h:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define ereport(elevel,...)
Definition: elog.h:144
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206

◆ int2abs()

Datum int2abs ( PG_FUNCTION_ARGS  )

Definition at line 1184 of file int.c.

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

1185 {
1186  int16 arg1 = PG_GETARG_INT16(0);
1187  int16 result;
1188 
1189  if (unlikely(arg1 == PG_INT16_MIN))
1190  ereport(ERROR,
1191  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1192  errmsg("smallint out of range")));
1193  result = (arg1 < 0) ? -arg1 : arg1;
1194  PG_RETURN_INT16(result);
1195 }
signed short int16
Definition: c.h:361
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
#define PG_INT16_MIN
Definition: c.h:453
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206

◆ int2and()

Datum int2and ( PG_FUNCTION_ARGS  )

Definition at line 1425 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1426 {
1427  int16 arg1 = PG_GETARG_INT16(0);
1428  int16 arg2 = PG_GETARG_INT16(1);
1429 
1430  PG_RETURN_INT16(arg1 & arg2);
1431 }
signed short int16
Definition: c.h:361
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ int2div()

Datum int2div ( PG_FUNCTION_ARGS  )

Definition at line 928 of file int.c.

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

929 {
930  int16 arg1 = PG_GETARG_INT16(0);
931  int16 arg2 = PG_GETARG_INT16(1);
932  int16 result;
933 
934  if (arg2 == 0)
935  {
936  ereport(ERROR,
937  (errcode(ERRCODE_DIVISION_BY_ZERO),
938  errmsg("division by zero")));
939  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
940  PG_RETURN_NULL();
941  }
942 
943  /*
944  * SHRT_MIN / -1 is problematic, since the result can't be represented on
945  * a two's-complement machine. Some machines produce SHRT_MIN, some
946  * produce zero, some throw an exception. We can dodge the problem by
947  * recognizing that division by -1 is the same as negation.
948  */
949  if (arg2 == -1)
950  {
951  if (unlikely(arg1 == PG_INT16_MIN))
952  ereport(ERROR,
953  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
954  errmsg("smallint out of range")));
955  result = -arg1;
956  PG_RETURN_INT16(result);
957  }
958 
959  /* No overflow is possible */
960 
961  result = arg1 / arg2;
962 
963  PG_RETURN_INT16(result);
964 }
signed short int16
Definition: c.h:361
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
#define PG_INT16_MIN
Definition: c.h:453
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ 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:361
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ 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:361
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ 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:361
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ int2in()

Datum int2in ( PG_FUNCTION_ARGS  )

Definition at line 63 of file int.c.

References PG_GETARG_CSTRING, PG_RETURN_INT16, and pg_strtoint16().

64 {
65  char *num = PG_GETARG_CSTRING(0);
66 
68 }
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
int16 pg_strtoint16(const char *s)
Definition: numutils.c:187
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277

◆ int2larger()

Datum int2larger ( PG_FUNCTION_ARGS  )

Definition at line 1325 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1326 {
1327  int16 arg1 = PG_GETARG_INT16(0);
1328  int16 arg2 = PG_GETARG_INT16(1);
1329 
1330  PG_RETURN_INT16((arg1 > arg2) ? arg1 : arg2);
1331 }
signed short int16
Definition: c.h:361
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ 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:361
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ 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:361
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ int2mi()

Datum int2mi ( PG_FUNCTION_ARGS  )

Definition at line 899 of file int.c.

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

900 {
901  int16 arg1 = PG_GETARG_INT16(0);
902  int16 arg2 = PG_GETARG_INT16(1);
903  int16 result;
904 
905  if (unlikely(pg_sub_s16_overflow(arg1, arg2, &result)))
906  ereport(ERROR,
907  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
908  errmsg("smallint out of range")));
909  PG_RETURN_INT16(result);
910 }
signed short int16
Definition: c.h:361
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define ereport(elevel,...)
Definition: elog.h:144
static bool pg_sub_s16_overflow(int16 a, int16 b, int16 *result)
Definition: int.h:65
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206

◆ int2mod()

Datum int2mod ( PG_FUNCTION_ARGS  )

Definition at line 1137 of file int.c.

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

1138 {
1139  int16 arg1 = PG_GETARG_INT16(0);
1140  int16 arg2 = PG_GETARG_INT16(1);
1141 
1142  if (unlikely(arg2 == 0))
1143  {
1144  ereport(ERROR,
1145  (errcode(ERRCODE_DIVISION_BY_ZERO),
1146  errmsg("division by zero")));
1147  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1148  PG_RETURN_NULL();
1149  }
1150 
1151  /*
1152  * Some machines throw a floating-point exception for INT_MIN % -1, which
1153  * is a bit silly since the correct answer is perfectly well-defined,
1154  * namely zero. (It's not clear this ever happens when dealing with
1155  * int16, but we might as well have the test for safety.)
1156  */
1157  if (arg2 == -1)
1158  PG_RETURN_INT16(0);
1159 
1160  /* No overflow is possible */
1161 
1162  PG_RETURN_INT16(arg1 % arg2);
1163 }
signed short int16
Definition: c.h:361
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ int2mul()

Datum int2mul ( PG_FUNCTION_ARGS  )

Definition at line 913 of file int.c.

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

914 {
915  int16 arg1 = PG_GETARG_INT16(0);
916  int16 arg2 = PG_GETARG_INT16(1);
917  int16 result;
918 
919  if (unlikely(pg_mul_s16_overflow(arg1, arg2, &result)))
920  ereport(ERROR,
921  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
922  errmsg("smallint out of range")));
923 
924  PG_RETURN_INT16(result);
925 }
signed short int16
Definition: c.h:361
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
int errcode(int sqlerrcode)
Definition: elog.c:610
static bool pg_mul_s16_overflow(int16 a, int16 b, int16 *result)
Definition: int.h:83
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206

◆ 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:361
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ int2not()

Datum int2not ( PG_FUNCTION_ARGS  )

Definition at line 1452 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1453 {
1454  int16 arg1 = PG_GETARG_INT16(0);
1455 
1456  PG_RETURN_INT16(~arg1);
1457 }
signed short int16
Definition: c.h:361
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ int2or()

Datum int2or ( PG_FUNCTION_ARGS  )

Definition at line 1434 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1435 {
1436  int16 arg1 = PG_GETARG_INT16(0);
1437  int16 arg2 = PG_GETARG_INT16(1);
1438 
1439  PG_RETURN_INT16(arg1 | arg2);
1440 }
signed short int16
Definition: c.h:361
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ int2out()

Datum int2out ( PG_FUNCTION_ARGS  )

Definition at line 74 of file int.c.

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

75 {
76  int16 arg1 = PG_GETARG_INT16(0);
77  char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */
78 
79  pg_itoa(arg1, result);
80  PG_RETURN_CSTRING(result);
81 }
signed short int16
Definition: c.h:361
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:361
void * palloc(Size size)
Definition: mcxt.c:949
int pg_itoa(int16 i, char *a)
Definition: numutils.c:338

◆ int2pl()

Datum int2pl ( PG_FUNCTION_ARGS  )

Definition at line 885 of file int.c.

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

886 {
887  int16 arg1 = PG_GETARG_INT16(0);
888  int16 arg2 = PG_GETARG_INT16(1);
889  int16 result;
890 
891  if (unlikely(pg_add_s16_overflow(arg1, arg2, &result)))
892  ereport(ERROR,
893  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
894  errmsg("smallint out of range")));
895  PG_RETURN_INT16(result);
896 }
signed short int16
Definition: c.h:361
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
int errcode(int sqlerrcode)
Definition: elog.c:610
static bool pg_add_s16_overflow(int16 a, int16 b, int16 *result)
Definition: int.h:47
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206

◆ int2recv()

Datum int2recv ( PG_FUNCTION_ARGS  )

Definition at line 87 of file int.c.

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

88 {
90 
91  PG_RETURN_INT16((int16) pq_getmsgint(buf, sizeof(int16)));
92 }
signed short int16
Definition: c.h:361
StringInfoData * StringInfo
Definition: stringinfo.h:44
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
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 98 of file int.c.

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

99 {
100  int16 arg1 = PG_GETARG_INT16(0);
102 
103  pq_begintypsend(&buf);
104  pq_sendint16(&buf, arg1);
106 }
signed short int16
Definition: c.h:361
static void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:137
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:369
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:271

◆ int2shl()

Datum int2shl ( PG_FUNCTION_ARGS  )

Definition at line 1461 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_INT16.

1462 {
1463  int16 arg1 = PG_GETARG_INT16(0);
1464  int32 arg2 = PG_GETARG_INT32(1);
1465 
1466  PG_RETURN_INT16(arg1 << arg2);
1467 }
signed short int16
Definition: c.h:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
signed int int32
Definition: c.h:362
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ int2shr()

Datum int2shr ( PG_FUNCTION_ARGS  )

Definition at line 1470 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_INT16.

1471 {
1472  int16 arg1 = PG_GETARG_INT16(0);
1473  int32 arg2 = PG_GETARG_INT32(1);
1474 
1475  PG_RETURN_INT16(arg1 >> arg2);
1476 }
signed short int16
Definition: c.h:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
signed int int32
Definition: c.h:362
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ int2smaller()

Datum int2smaller ( PG_FUNCTION_ARGS  )

Definition at line 1334 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1335 {
1336  int16 arg1 = PG_GETARG_INT16(0);
1337  int16 arg2 = PG_GETARG_INT16(1);
1338 
1339  PG_RETURN_INT16((arg1 < arg2) ? arg1 : arg2);
1340 }
signed short int16
Definition: c.h:361
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ int2um()

Datum int2um ( PG_FUNCTION_ARGS  )

Definition at line 865 of file int.c.

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

866 {
868 
869  if (unlikely(arg == PG_INT16_MIN))
870  ereport(ERROR,
871  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
872  errmsg("smallint out of range")));
873  PG_RETURN_INT16(-arg);
874 }
signed short int16
Definition: c.h:361
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
#define PG_INT16_MIN
Definition: c.h:453
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
void * arg
#define unlikely(x)
Definition: c.h:206

◆ int2up()

Datum int2up ( PG_FUNCTION_ARGS  )

Definition at line 877 of file int.c.

References arg, PG_GETARG_INT16, and PG_RETURN_INT16.

878 {
880 
881  PG_RETURN_INT16(arg);
882 }
signed short int16
Definition: c.h:361
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
void * arg

◆ int2vectorin()

Datum int2vectorin ( PG_FUNCTION_ARGS  )

Definition at line 141 of file int.c.

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

142 {
143  char *intString = PG_GETARG_CSTRING(0);
144  int2vector *result;
145  int n;
146 
148 
149  for (n = 0; *intString && n < FUNC_MAX_ARGS; n++)
150  {
151  while (*intString && isspace((unsigned char) *intString))
152  intString++;
153  if (*intString == '\0')
154  break;
155  result->values[n] = pg_atoi(intString, sizeof(int16), ' ');
156  while (*intString && !isspace((unsigned char) *intString))
157  intString++;
158  }
159  while (*intString && isspace((unsigned char) *intString))
160  intString++;
161  if (*intString)
162  ereport(ERROR,
163  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
164  errmsg("int2vector has too many elements")));
165 
166  SET_VARSIZE(result, Int2VectorSize(n));
167  result->ndim = 1;
168  result->dataoffset = 0; /* never any nulls */
169  result->elemtype = INT2OID;
170  result->dim1 = n;
171  result->lbound1 = 0;
172 
173  PG_RETURN_POINTER(result);
174 }
signed short int16
Definition: c.h:361
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
int lbound1
Definition: c.h:597
int errcode(int sqlerrcode)
Definition: elog.c:610
Oid elemtype
Definition: c.h:595
#define FUNC_MAX_ARGS
int32 dataoffset
Definition: c.h:594
#define ERROR
Definition: elog.h:43
int ndim
Definition: c.h:593
Definition: c.h:590
void * palloc0(Size size)
Definition: mcxt.c:980
int dim1
Definition: c.h:596
#define ereport(elevel,...)
Definition: elog.h:144
int16 values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:598
#define Int2VectorSize(n)
Definition: int.c:45
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
int32 pg_atoi(const char *s, int size, int c)
Definition: numutils.c:102

◆ int2vectorout()

Datum int2vectorout ( PG_FUNCTION_ARGS  )

Definition at line 180 of file int.c.

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

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

◆ int2vectorrecv()

Datum int2vectorrecv ( PG_FUNCTION_ARGS  )

Definition at line 204 of file int.c.

References ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_LBOUND, ARR_NDIM, array_recv(), Assert, buf, DatumGetPointer, ereport, errcode(), errmsg(), ERROR, FUNC_MAX_ARGS, InitFunctionCallInfoData, Int32GetDatum, InvalidOid, LOCAL_FCINFO, ObjectIdGetDatum, PG_GETARG_POINTER, PG_RETURN_POINTER, and PointerGetDatum.

205 {
206  LOCAL_FCINFO(locfcinfo, 3);
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->args[0].value = PointerGetDatum(buf);
220  locfcinfo->args[0].isnull = false;
221  locfcinfo->args[1].value = ObjectIdGetDatum(INT2OID);
222  locfcinfo->args[1].isnull = false;
223  locfcinfo->args[2].value = Int32GetDatum(-1);
224  locfcinfo->args[2].isnull = 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:360
#define PointerGetDatum(X)
Definition: postgres.h:556
StringInfoData * StringInfo
Definition: stringinfo.h:44
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
Datum array_recv(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1268
#define ARR_LBOUND(a)
Definition: array.h:284
#define FUNC_MAX_ARGS
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:282
static char * buf
Definition: pg_test_fsync.c:67
#define ARR_HASNULL(a)
Definition: array.h:279
Definition: c.h:590
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
#define Assert(condition)
Definition: c.h:745
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
#define ARR_NDIM(a)
Definition: array.h:278
#define DatumGetPointer(X)
Definition: postgres.h:549
#define Int32GetDatum(X)
Definition: postgres.h:479
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define ARR_ELEMTYPE(a)
Definition: array.h:280

◆ 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:1569

◆ int2xor()

Datum int2xor ( PG_FUNCTION_ARGS  )

Definition at line 1443 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1444 {
1445  int16 arg1 = PG_GETARG_INT16(0);
1446  int16 arg2 = PG_GETARG_INT16(1);
1447 
1448  PG_RETURN_INT16(arg1 ^ arg2);
1449 }
signed short int16
Definition: c.h:361
#define PG_RETURN_INT16(x)
Definition: fmgr.h:355
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ int42div()

Datum int42div ( PG_FUNCTION_ARGS  )

Definition at line 1070 of file int.c.

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

1071 {
1072  int32 arg1 = PG_GETARG_INT32(0);
1073  int16 arg2 = PG_GETARG_INT16(1);
1074  int32 result;
1075 
1076  if (unlikely(arg2 == 0))
1077  {
1078  ereport(ERROR,
1079  (errcode(ERRCODE_DIVISION_BY_ZERO),
1080  errmsg("division by zero")));
1081  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1082  PG_RETURN_NULL();
1083  }
1084 
1085  /*
1086  * INT_MIN / -1 is problematic, since the result can't be represented on a
1087  * two's-complement machine. Some machines produce INT_MIN, some produce
1088  * zero, some throw an exception. We can dodge the problem by recognizing
1089  * that division by -1 is the same as negation.
1090  */
1091  if (arg2 == -1)
1092  {
1093  if (unlikely(arg1 == PG_INT32_MIN))
1094  ereport(ERROR,
1095  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1096  errmsg("integer out of range")));
1097  result = -arg1;
1098  PG_RETURN_INT32(result);
1099  }
1100 
1101  /* No overflow is possible */
1102 
1103  result = arg1 / arg2;
1104 
1105  PG_RETURN_INT32(result);
1106 }
signed short int16
Definition: c.h:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
#define PG_INT32_MIN
Definition: c.h:456
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ 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:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ 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:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ 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:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ 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:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ 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:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ int42mi()

Datum int42mi ( PG_FUNCTION_ARGS  )

Definition at line 1042 of file int.c.

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

1043 {
1044  int32 arg1 = PG_GETARG_INT32(0);
1045  int16 arg2 = PG_GETARG_INT16(1);
1046  int32 result;
1047 
1048  if (unlikely(pg_sub_s32_overflow(arg1, (int32) arg2, &result)))
1049  ereport(ERROR,
1050  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1051  errmsg("integer out of range")));
1052  PG_RETURN_INT32(result);
1053 }
signed short int16
Definition: c.h:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
static bool pg_sub_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206

◆ int42mul()

Datum int42mul ( PG_FUNCTION_ARGS  )

Definition at line 1056 of file int.c.

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

1057 {
1058  int32 arg1 = PG_GETARG_INT32(0);
1059  int16 arg2 = PG_GETARG_INT16(1);
1060  int32 result;
1061 
1062  if (unlikely(pg_mul_s32_overflow(arg1, (int32) arg2, &result)))
1063  ereport(ERROR,
1064  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1065  errmsg("integer out of range")));
1066  PG_RETURN_INT32(result);
1067 }
signed short int16
Definition: c.h:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:140
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206

◆ 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:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ int42pl()

Datum int42pl ( PG_FUNCTION_ARGS  )

Definition at line 1028 of file int.c.

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

1029 {
1030  int32 arg1 = PG_GETARG_INT32(0);
1031  int16 arg2 = PG_GETARG_INT16(1);
1032  int32 result;
1033 
1034  if (unlikely(pg_add_s32_overflow(arg1, (int32) arg2, &result)))
1035  ereport(ERROR,
1036  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1037  errmsg("integer out of range")));
1038  PG_RETURN_INT32(result);
1039 }
signed short int16
Definition: c.h:361
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define ereport(elevel,...)
Definition: elog.h:144
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206

◆ 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:269
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358

◆ int4abs()

Datum int4abs ( PG_FUNCTION_ARGS  )

Definition at line 1170 of file int.c.

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

1171 {
1172  int32 arg1 = PG_GETARG_INT32(0);
1173  int32 result;
1174 
1175  if (unlikely(arg1 == PG_INT32_MIN))
1176  ereport(ERROR,
1177  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1178  errmsg("integer out of range")));
1179  result = (arg1 < 0) ? -arg1 : arg1;
1180  PG_RETURN_INT32(result);
1181 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
#define PG_INT32_MIN
Definition: c.h:456
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206

◆ int4and()

Datum int4and ( PG_FUNCTION_ARGS  )

Definition at line 1372 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

1373 {
1374  int32 arg1 = PG_GETARG_INT32(0);
1375  int32 arg2 = PG_GETARG_INT32(1);
1376 
1377  PG_RETURN_INT32(arg1 & arg2);
1378 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
signed int int32
Definition: c.h:362

◆ int4div()

Datum int4div ( PG_FUNCTION_ARGS  )

Definition at line 812 of file int.c.

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

813 {
814  int32 arg1 = PG_GETARG_INT32(0);
815  int32 arg2 = PG_GETARG_INT32(1);
816  int32 result;
817 
818  if (arg2 == 0)
819  {
820  ereport(ERROR,
821  (errcode(ERRCODE_DIVISION_BY_ZERO),
822  errmsg("division by zero")));
823  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
824  PG_RETURN_NULL();
825  }
826 
827  /*
828  * INT_MIN / -1 is problematic, since the result can't be represented on a
829  * two's-complement machine. Some machines produce INT_MIN, some produce
830  * zero, some throw an exception. We can dodge the problem by recognizing
831  * that division by -1 is the same as negation.
832  */
833  if (arg2 == -1)
834  {
835  if (unlikely(arg1 == PG_INT32_MIN))
836  ereport(ERROR,
837  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
838  errmsg("integer out of range")));
839  result = -arg1;
840  PG_RETURN_INT32(result);
841  }
842 
843  /* No overflow is possible */
844 
845  result = arg1 / arg2;
846 
847  PG_RETURN_INT32(result);
848 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
#define PG_INT32_MIN
Definition: c.h:456
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ 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:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358

◆ int4gcd()

Datum int4gcd ( PG_FUNCTION_ARGS  )

Definition at line 1273 of file int.c.

References int4gcd_internal(), PG_GETARG_INT32, and PG_RETURN_INT32.

1274 {
1275  int32 arg1 = PG_GETARG_INT32(0);
1276  int32 arg2 = PG_GETARG_INT32(1);
1277  int32 result;
1278 
1279  result = int4gcd_internal(arg1, arg2);
1280 
1281  PG_RETURN_INT32(result);
1282 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
signed int int32
Definition: c.h:362
static int32 int4gcd_internal(int32 arg1, int32 arg2)
Definition: int.c:1212

◆ int4gcd_internal()

static int32 int4gcd_internal ( int32  arg1,
int32  arg2 
)
static

Definition at line 1212 of file int.c.

References a1, a2, ereport, errcode(), errmsg(), ERROR, PG_INT32_MIN, and swap.

Referenced by int4gcd(), and int4lcm().

1213 {
1214  int32 swap;
1215  int32 a1,
1216  a2;
1217 
1218  /*
1219  * Put the greater absolute value in arg1.
1220  *
1221  * This would happen automatically in the loop below, but avoids an
1222  * expensive modulo operation, and simplifies the special-case handling
1223  * for INT_MIN below.
1224  *
1225  * We do this in negative space in order to handle INT_MIN.
1226  */
1227  a1 = (arg1 < 0) ? arg1 : -arg1;
1228  a2 = (arg2 < 0) ? arg2 : -arg2;
1229  if (a1 > a2)
1230  {
1231  swap = arg1;
1232  arg1 = arg2;
1233  arg2 = swap;
1234  }
1235 
1236  /* Special care needs to be taken with INT_MIN. See comments above. */
1237  if (arg1 == PG_INT32_MIN)
1238  {
1239  if (arg2 == 0 || arg2 == PG_INT32_MIN)
1240  ereport(ERROR,
1241  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1242  errmsg("integer out of range")));
1243 
1244  /*
1245  * Some machines throw a floating-point exception for INT_MIN % -1,
1246  * which is a bit silly since the correct answer is perfectly
1247  * well-defined, namely zero. Guard against this and just return the
1248  * result, gcd(INT_MIN, -1) = 1.
1249  */
1250  if (arg2 == -1)
1251  return 1;
1252  }
1253 
1254  /* Use the Euclidean algorithm to find the GCD */
1255  while (arg2 != 0)
1256  {
1257  swap = arg2;
1258  arg2 = arg1 % arg2;
1259  arg1 = swap;
1260  }
1261 
1262  /*
1263  * Make sure the result is positive. (We know we don't have INT_MIN
1264  * anymore).
1265  */
1266  if (arg1 < 0)
1267  arg1 = -arg1;
1268 
1269  return arg1;
1270 }
#define swap(a, b)
Definition: qsort.c:94
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
static const FormData_pg_attribute a2
Definition: heap.c:165
#define ERROR
Definition: elog.h:43
#define PG_INT32_MIN
Definition: c.h:456
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
static const FormData_pg_attribute a1
Definition: heap.c:151

◆ 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:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358

◆ 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:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358

◆ int4in()

Datum int4in ( PG_FUNCTION_ARGS  )

Definition at line 266 of file int.c.

References PG_GETARG_CSTRING, PG_RETURN_INT32, and pg_strtoint32().

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

267 {
268  char *num = PG_GETARG_CSTRING(0);
269 
271 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int32 pg_strtoint32(const char *s)
Definition: numutils.c:263
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277

◆ int4inc()

Datum int4inc ( PG_FUNCTION_ARGS  )

Definition at line 851 of file int.c.

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

852 {
854  int32 result;
855 
856  if (unlikely(pg_add_s32_overflow(arg, 1, &result)))
857  ereport(ERROR,
858  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
859  errmsg("integer out of range")));
860 
861  PG_RETURN_INT32(result);
862 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
int errmsg(const char *fmt,...)
Definition: elog.c:824
void * arg
#define unlikely(x)
Definition: c.h:206

◆ int4larger()

Datum int4larger ( PG_FUNCTION_ARGS  )

Definition at line 1343 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

1344 {
1345  int32 arg1 = PG_GETARG_INT32(0);
1346  int32 arg2 = PG_GETARG_INT32(1);
1347 
1348  PG_RETURN_INT32((arg1 > arg2) ? arg1 : arg2);
1349 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
signed int int32
Definition: c.h:362

◆ int4lcm()

Datum int4lcm ( PG_FUNCTION_ARGS  )

Definition at line 1288 of file int.c.

References ereport, errcode(), errmsg(), ERROR, gcd(), int4gcd_internal(), PG_GETARG_INT32, PG_INT32_MIN, pg_mul_s32_overflow(), PG_RETURN_INT32, and unlikely.

1289 {
1290  int32 arg1 = PG_GETARG_INT32(0);
1291  int32 arg2 = PG_GETARG_INT32(1);
1292  int32 gcd;
1293  int32 result;
1294 
1295  /*
1296  * Handle lcm(x, 0) = lcm(0, x) = 0 as a special case. This prevents a
1297  * division-by-zero error below when x is zero, and an overflow error from
1298  * the GCD computation when x = INT_MIN.
1299  */
1300  if (arg1 == 0 || arg2 == 0)
1301  PG_RETURN_INT32(0);
1302 
1303  /* lcm(x, y) = abs(x / gcd(x, y) * y) */
1304  gcd = int4gcd_internal(arg1, arg2);
1305  arg1 = arg1 / gcd;
1306 
1307  if (unlikely(pg_mul_s32_overflow(arg1, arg2, &result)))
1308  ereport(ERROR,
1309  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1310  errmsg("integer out of range")));
1311 
1312  /* If the result is INT_MIN, it cannot be represented. */
1313  if (unlikely(result == PG_INT32_MIN))
1314  ereport(ERROR,
1315  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1316  errmsg("integer out of range")));
1317 
1318  if (result < 0)
1319  result = -result;
1320 
1321  PG_RETURN_INT32(result);
1322 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:140
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
#define PG_INT32_MIN
Definition: c.h:456
#define ereport(elevel,...)
Definition: elog.h:144
static int32 int4gcd_internal(int32 arg1, int32 arg2)
Definition: int.c:1212
int errmsg(const char *fmt,...)
Definition: elog.c:824
static uint32 gcd(uint32 a, uint32 b)
#define unlikely(x)
Definition: c.h:206

◆ 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:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358

◆ 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:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358

◆ int4mi()

Datum int4mi ( PG_FUNCTION_ARGS  )

Definition at line 784 of file int.c.

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

785 {
786  int32 arg1 = PG_GETARG_INT32(0);
787  int32 arg2 = PG_GETARG_INT32(1);
788  int32 result;
789 
790  if (unlikely(pg_sub_s32_overflow(arg1, arg2, &result)))
791  ereport(ERROR,
792  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
793  errmsg("integer out of range")));
794  PG_RETURN_INT32(result);
795 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
static bool pg_sub_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:122
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206

◆ int4mod()

Datum int4mod ( PG_FUNCTION_ARGS  )

Definition at line 1109 of file int.c.

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

1110 {
1111  int32 arg1 = PG_GETARG_INT32(0);
1112  int32 arg2 = PG_GETARG_INT32(1);
1113 
1114  if (unlikely(arg2 == 0))
1115  {
1116  ereport(ERROR,
1117  (errcode(ERRCODE_DIVISION_BY_ZERO),
1118  errmsg("division by zero")));
1119  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1120  PG_RETURN_NULL();
1121  }
1122 
1123  /*
1124  * Some machines throw a floating-point exception for INT_MIN % -1, which
1125  * is a bit silly since the correct answer is perfectly well-defined,
1126  * namely zero.
1127  */
1128  if (arg2 == -1)
1129  PG_RETURN_INT32(0);
1130 
1131  /* No overflow is possible */
1132 
1133  PG_RETURN_INT32(arg1 % arg2);
1134 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ int4mul()

Datum int4mul ( PG_FUNCTION_ARGS  )

Definition at line 798 of file int.c.

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

799 {
800  int32 arg1 = PG_GETARG_INT32(0);
801  int32 arg2 = PG_GETARG_INT32(1);
802  int32 result;
803 
804  if (unlikely(pg_mul_s32_overflow(arg1, arg2, &result)))
805  ereport(ERROR,
806  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
807  errmsg("integer out of range")));
808  PG_RETURN_INT32(result);
809 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:140
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206

◆ 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:269
signed int int32
Definition: c.h:362
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358

◆ int4not()

Datum int4not ( PG_FUNCTION_ARGS  )

Definition at line 1417 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

1418 {
1419  int32 arg1 = PG_GETARG_INT32(0);
1420 
1421  PG_RETURN_INT32(~arg1);
1422 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
signed int int32
Definition: c.h:362

◆ int4or()

Datum int4or ( PG_FUNCTION_ARGS  )

Definition at line 1381 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

1382 {
1383  int32 arg1 = PG_GETARG_INT32(0);
1384  int32 arg2 = PG_GETARG_INT32(1);
1385 
1386  PG_RETURN_INT32(arg1 | arg2);
1387 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
signed int int32
Definition: c.h:362

◆ 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:269
signed int int32
Definition: c.h:362
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:361
int pg_ltoa(int32 value, char *a)
Definition: numutils.c:416
void * palloc(Size size)
Definition: mcxt.c:949

◆ int4pl()

Datum int4pl ( PG_FUNCTION_ARGS  )

Definition at line 770 of file int.c.

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

Referenced by int4range_canonical().

771 {
772  int32 arg1 = PG_GETARG_INT32(0);
773  int32 arg2 = PG_GETARG_INT32(1);
774  int32 result;
775 
776  if (unlikely(pg_add_s32_overflow(arg1, arg2, &result)))
777  ereport(ERROR,
778  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
779  errmsg("integer out of range")));
780  PG_RETURN_INT32(result);
781 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define unlikely(x)
Definition: c.h:206

◆ 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:44
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
signed int int32
Definition: c.h:362
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:269
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:369
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
signed int int32
Definition: c.h:362
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
static char * buf
Definition: pg_test_fsync.c:67

◆ int4shl()

Datum int4shl ( PG_FUNCTION_ARGS  )

Definition at line 1399 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

1400 {
1401  int32 arg1 = PG_GETARG_INT32(0);
1402  int32 arg2 = PG_GETARG_INT32(1);
1403 
1404  PG_RETURN_INT32(arg1 << arg2);
1405 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
signed int int32
Definition: c.h:362

◆ int4shr()

Datum int4shr ( PG_FUNCTION_ARGS  )

Definition at line 1408 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

1409 {
1410  int32 arg1 = PG_GETARG_INT32(0);
1411  int32 arg2 = PG_GETARG_INT32(1);
1412 
1413  PG_RETURN_INT32(arg1 >> arg2);
1414 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
signed int int32
Definition: c.h:362

◆ int4smaller()

Datum int4smaller ( PG_FUNCTION_ARGS  )

Definition at line 1352 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

1353 {
1354  int32 arg1 = PG_GETARG_INT32(0);
1355  int32 arg2 = PG_GETARG_INT32(1);
1356 
1357  PG_RETURN_INT32((arg1 < arg2) ? arg1 : arg2);
1358 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
signed int int32
Definition: c.h:362

◆ int4um()

Datum int4um ( PG_FUNCTION_ARGS  )

Definition at line 750 of file int.c.

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

751 {
753 
754  if (unlikely(arg == PG_INT32_MIN))
755  ereport(ERROR,
756  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
757  errmsg("integer out of range")));
758  PG_RETURN_INT32(-arg);
759 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define ERROR
Definition: elog.h:43
#define PG_INT32_MIN
Definition: c.h:456
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
void * arg
#define unlikely(x)
Definition: c.h:206

◆ int4up()

Datum int4up ( PG_FUNCTION_ARGS  )

Definition at line 762 of file int.c.

References arg, PG_GETARG_INT32, and PG_RETURN_INT32.

763 {
765 
766  PG_RETURN_INT32(arg);
767 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
signed int int32
Definition: c.h:362
void * arg

◆ int4xor()

Datum int4xor ( PG_FUNCTION_ARGS  )

Definition at line 1390 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

1391 {
1392  int32 arg1 = PG_GETARG_INT32(0);
1393  int32 arg2 = PG_GETARG_INT32(1);
1394 
1395  PG_RETURN_INT32(arg1 ^ arg2);
1396 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
signed int int32
Definition: c.h:362