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 353 of file int.c.

References PG_GETARG_BOOL, and PG_RETURN_INT32.

354 {
355  if (PG_GETARG_BOOL(0) == false)
356  PG_RETURN_INT32(0);
357  else
358  PG_RETURN_INT32(1);
359 }
#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:354
int lbound1
Definition: c.h:590
Oid elemtype
Definition: c.h:588
int32 dataoffset
Definition: c.h:587
int ndim
Definition: c.h:586
Definition: c.h:583
void * palloc0(Size size)
Definition: mcxt.c:980
int dim1
Definition: c.h:589
int16 values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:591
#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 1484 of file int.c.

References generate_series_step_int4().

1485 {
1486  return generate_series_step_int4(fcinfo);
1487 }
Datum generate_series_step_int4(PG_FUNCTION_ARGS)
Definition: int.c:1490

◆ generate_series_int4_support()

Datum generate_series_int4_support ( PG_FUNCTION_ARGS  )

Definition at line 1566 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.

1567 {
1568  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
1569  Node *ret = NULL;
1570 
1571  if (IsA(rawreq, SupportRequestRows))
1572  {
1573  /* Try to estimate the number of rows returned */
1574  SupportRequestRows *req = (SupportRequestRows *) rawreq;
1575 
1576  if (is_funcclause(req->node)) /* be paranoid */
1577  {
1578  List *args = ((FuncExpr *) req->node)->args;
1579  Node *arg1,
1580  *arg2,
1581  *arg3;
1582 
1583  /* We can use estimated argument values here */
1584  arg1 = estimate_expression_value(req->root, linitial(args));
1585  arg2 = estimate_expression_value(req->root, lsecond(args));
1586  if (list_length(args) >= 3)
1587  arg3 = estimate_expression_value(req->root, lthird(args));
1588  else
1589  arg3 = NULL;
1590 
1591  /*
1592  * If any argument is constant NULL, we can safely assume that
1593  * zero rows are returned. Otherwise, if they're all non-NULL
1594  * constants, we can calculate the number of rows that will be
1595  * returned. Use double arithmetic to avoid overflow hazards.
1596  */
1597  if ((IsA(arg1, Const) &&
1598  ((Const *) arg1)->constisnull) ||
1599  (IsA(arg2, Const) &&
1600  ((Const *) arg2)->constisnull) ||
1601  (arg3 != NULL && IsA(arg3, Const) &&
1602  ((Const *) arg3)->constisnull))
1603  {
1604  req->rows = 0;
1605  ret = (Node *) req;
1606  }
1607  else if (IsA(arg1, Const) &&
1608  IsA(arg2, Const) &&
1609  (arg3 == NULL || IsA(arg3, Const)))
1610  {
1611  double start,
1612  finish,
1613  step;
1614 
1615  start = DatumGetInt32(((Const *) arg1)->constvalue);
1616  finish = DatumGetInt32(((Const *) arg2)->constvalue);
1617  step = arg3 ? DatumGetInt32(((Const *) arg3)->constvalue) : 1;
1618 
1619  /* This equation works for either sign of step */
1620  if (step != 0)
1621  {
1622  req->rows = floor((finish - start + step) / step);
1623  ret = (Node *) req;
1624  }
1625  }
1626  }
1627  }
1628 
1629  PG_RETURN_POINTER(ret);
1630 }
#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 1490 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().

1491 {
1492  FuncCallContext *funcctx;
1493  generate_series_fctx *fctx;
1494  int32 result;
1495  MemoryContext oldcontext;
1496 
1497  /* stuff done only on the first call of the function */
1498  if (SRF_IS_FIRSTCALL())
1499  {
1500  int32 start = PG_GETARG_INT32(0);
1501  int32 finish = PG_GETARG_INT32(1);
1502  int32 step = 1;
1503 
1504  /* see if we were given an explicit step size */
1505  if (PG_NARGS() == 3)
1506  step = PG_GETARG_INT32(2);
1507  if (step == 0)
1508  ereport(ERROR,
1509  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1510  errmsg("step size cannot equal zero")));
1511 
1512  /* create a function context for cross-call persistence */
1513  funcctx = SRF_FIRSTCALL_INIT();
1514 
1515  /*
1516  * switch to memory context appropriate for multiple function calls
1517  */
1518  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1519 
1520  /* allocate memory for user context */
1521  fctx = (generate_series_fctx *) palloc(sizeof(generate_series_fctx));
1522 
1523  /*
1524  * Use fctx to keep state from call to call. Seed current with the
1525  * original start value
1526  */
1527  fctx->current = start;
1528  fctx->finish = finish;
1529  fctx->step = step;
1530 
1531  funcctx->user_fctx = fctx;
1532  MemoryContextSwitchTo(oldcontext);
1533  }
1534 
1535  /* stuff done on every call of the function */
1536  funcctx = SRF_PERCALL_SETUP();
1537 
1538  /*
1539  * get the saved state and use current as the result for this iteration
1540  */
1541  fctx = funcctx->user_fctx;
1542  result = fctx->current;
1543 
1544  if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1545  (fctx->step < 0 && fctx->current >= fctx->finish))
1546  {
1547  /*
1548  * Increment current in preparation for next iteration. If next-value
1549  * computation overflows, this is the final result.
1550  */
1551  if (pg_add_s32_overflow(fctx->current, fctx->step, &fctx->current))
1552  fctx->step = 0;
1553 
1554  /* do when there is more left to send */
1555  SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
1556  }
1557  else
1558  /* do when there is no more left */
1559  SRF_RETURN_DONE(funcctx);
1560 }
#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:355
#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 321 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT32.

322 {
323  int16 arg1 = PG_GETARG_INT16(0);
324 
325  PG_RETURN_INT32((int32) arg1);
326 }
signed short int16
Definition: c.h:354
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
signed int int32
Definition: c.h:355
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

◆ i4toi2()

Datum i4toi2 ( PG_FUNCTION_ARGS  )

Definition at line 329 of file int.c.

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

330 {
331  int32 arg1 = PG_GETARG_INT32(0);
332 
333  if (unlikely(arg1 < SHRT_MIN) || unlikely(arg1 > SHRT_MAX))
334  ereport(ERROR,
335  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
336  errmsg("smallint out of range")));
337 
338  PG_RETURN_INT16((int16) arg1);
339 }
signed short int16
Definition: c.h:354
#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:355
#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 720 of file int.c.

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

721 {
722  /* Doesn't seem worth duplicating code for, so just invoke int2_int4 */
724  PG_GETARG_DATUM(0),
725  PG_GETARG_DATUM(1),
727  PG_GETARG_DATUM(3),
728  PG_GETARG_DATUM(4));
729 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
signed int int32
Definition: c.h:355
Datum in_range_int2_int4(PG_FUNCTION_ARGS)
Definition: int.c:685
#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 685 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().

686 {
687  /* We must do all the math in int32 */
689  int32 base = (int32) PG_GETARG_INT16(1);
690  int32 offset = PG_GETARG_INT32(2);
691  bool sub = PG_GETARG_BOOL(3);
692  bool less = PG_GETARG_BOOL(4);
693  int32 sum;
694 
695  if (offset < 0)
696  ereport(ERROR,
697  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
698  errmsg("invalid preceding or following size in window function")));
699 
700  if (sub)
701  offset = -offset; /* cannot overflow */
702 
703  if (unlikely(pg_add_s32_overflow(base, offset, &sum)))
704  {
705  /*
706  * If sub is false, the true sum is surely more than val, so correct
707  * answer is the same as "less". If sub is true, the true sum is
708  * surely less than val, so the answer is "!less".
709  */
710  PG_RETURN_BOOL(sub ? !less : less);
711  }
712 
713  if (less)
714  PG_RETURN_BOOL(val <= sum);
715  else
716  PG_RETURN_BOOL(val >= sum);
717 }
#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:355
#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 732 of file int.c.

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

733 {
734  /* Doesn't seem worth duplicating code for, so just invoke int4_int8 */
738  PG_GETARG_DATUM(2),
739  PG_GETARG_DATUM(3),
740  PG_GETARG_DATUM(4));
741 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum in_range_int4_int8(PG_FUNCTION_ARGS)
Definition: int.c:650
signed int int32
Definition: c.h:355
#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 638 of file int.c.

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

639 {
640  /* Doesn't seem worth duplicating code for, so just invoke int4_int4 */
642  PG_GETARG_DATUM(0),
643  PG_GETARG_DATUM(1),
645  PG_GETARG_DATUM(3),
646  PG_GETARG_DATUM(4));
647 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
signed int int32
Definition: c.h:355
Datum in_range_int4_int4(PG_FUNCTION_ARGS)
Definition: int.c:604
#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 604 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().

605 {
607  int32 base = PG_GETARG_INT32(1);
608  int32 offset = PG_GETARG_INT32(2);
609  bool sub = PG_GETARG_BOOL(3);
610  bool less = PG_GETARG_BOOL(4);
611  int32 sum;
612 
613  if (offset < 0)
614  ereport(ERROR,
615  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
616  errmsg("invalid preceding or following size in window function")));
617 
618  if (sub)
619  offset = -offset; /* cannot overflow */
620 
621  if (unlikely(pg_add_s32_overflow(base, offset, &sum)))
622  {
623  /*
624  * If sub is false, the true sum is surely more than val, so correct
625  * answer is the same as "less". If sub is true, the true sum is
626  * surely less than val, so the answer is "!less".
627  */
628  PG_RETURN_BOOL(sub ? !less : less);
629  }
630 
631  if (less)
632  PG_RETURN_BOOL(val <= sum);
633  else
634  PG_RETURN_BOOL(val >= sum);
635 }
#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:355
#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 650 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().

651 {
652  /* We must do all the math in int64 */
653  int64 val = (int64) PG_GETARG_INT32(0);
654  int64 base = (int64) PG_GETARG_INT32(1);
655  int64 offset = PG_GETARG_INT64(2);
656  bool sub = PG_GETARG_BOOL(3);
657  bool less = PG_GETARG_BOOL(4);
658  int64 sum;
659 
660  if (offset < 0)
661  ereport(ERROR,
662  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
663  errmsg("invalid preceding or following size in window function")));
664 
665  if (sub)
666  offset = -offset; /* cannot overflow */
667 
668  if (unlikely(pg_add_s64_overflow(base, offset, &sum)))
669  {
670  /*
671  * If sub is false, the true sum is surely more than val, so correct
672  * answer is the same as "less". If sub is true, the true sum is
673  * surely less than val, so the answer is "!less".
674  */
675  PG_RETURN_BOOL(sub ? !less : less);
676  }
677 
678  if (less)
679  PG_RETURN_BOOL(val <= sum);
680  else
681  PG_RETURN_BOOL(val >= sum);
682 }
#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 1011 of file int.c.

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

1012 {
1013  int16 arg1 = PG_GETARG_INT16(0);
1014  int32 arg2 = PG_GETARG_INT32(1);
1015 
1016  if (unlikely(arg2 == 0))
1017  {
1018  ereport(ERROR,
1019  (errcode(ERRCODE_DIVISION_BY_ZERO),
1020  errmsg("division by zero")));
1021  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1022  PG_RETURN_NULL();
1023  }
1024 
1025  /* No overflow is possible */
1026  PG_RETURN_INT32((int32) arg1 / arg2);
1027 }
signed short int16
Definition: c.h:354
#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:355
#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 485 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

486 {
487  int16 arg1 = PG_GETARG_INT16(0);
488  int32 arg2 = PG_GETARG_INT32(1);
489 
490  PG_RETURN_BOOL(arg1 == arg2);
491 }
signed short int16
Definition: c.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#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 530 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

531 {
532  int16 arg1 = PG_GETARG_INT16(0);
533  int32 arg2 = PG_GETARG_INT32(1);
534 
535  PG_RETURN_BOOL(arg1 >= arg2);
536 }
signed short int16
Definition: c.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#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 521 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

522 {
523  int16 arg1 = PG_GETARG_INT16(0);
524  int32 arg2 = PG_GETARG_INT32(1);
525 
526  PG_RETURN_BOOL(arg1 > arg2);
527 }
signed short int16
Definition: c.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#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 512 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

513 {
514  int16 arg1 = PG_GETARG_INT16(0);
515  int32 arg2 = PG_GETARG_INT32(1);
516 
517  PG_RETURN_BOOL(arg1 <= arg2);
518 }
signed short int16
Definition: c.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#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 503 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

504 {
505  int16 arg1 = PG_GETARG_INT16(0);
506  int32 arg2 = PG_GETARG_INT32(1);
507 
508  PG_RETURN_BOOL(arg1 < arg2);
509 }
signed short int16
Definition: c.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#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 983 of file int.c.

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

984 {
985  int16 arg1 = PG_GETARG_INT16(0);
986  int32 arg2 = PG_GETARG_INT32(1);
987  int32 result;
988 
989  if (unlikely(pg_sub_s32_overflow((int32) arg1, arg2, &result)))
990  ereport(ERROR,
991  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
992  errmsg("integer out of range")));
993  PG_RETURN_INT32(result);
994 }
signed short int16
Definition: c.h:354
#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:355
#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 997 of file int.c.

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

998 {
999  int16 arg1 = PG_GETARG_INT16(0);
1000  int32 arg2 = PG_GETARG_INT32(1);
1001  int32 result;
1002 
1003  if (unlikely(pg_mul_s32_overflow((int32) arg1, arg2, &result)))
1004  ereport(ERROR,
1005  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1006  errmsg("integer out of range")));
1007  PG_RETURN_INT32(result);
1008 }
signed short int16
Definition: c.h:354
#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:355
#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 494 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

495 {
496  int16 arg1 = PG_GETARG_INT16(0);
497  int32 arg2 = PG_GETARG_INT32(1);
498 
499  PG_RETURN_BOOL(arg1 != arg2);
500 }
signed short int16
Definition: c.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#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 969 of file int.c.

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

970 {
971  int16 arg1 = PG_GETARG_INT16(0);
972  int32 arg2 = PG_GETARG_INT32(1);
973  int32 result;
974 
975  if (unlikely(pg_add_s32_overflow((int32) arg1, arg2, &result)))
976  ereport(ERROR,
977  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
978  errmsg("integer out of range")));
979  PG_RETURN_INT32(result);
980 }
signed short int16
Definition: c.h:354
#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:355
#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 1186 of file int.c.

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

1187 {
1188  int16 arg1 = PG_GETARG_INT16(0);
1189  int16 result;
1190 
1191  if (unlikely(arg1 == PG_INT16_MIN))
1192  ereport(ERROR,
1193  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1194  errmsg("smallint out of range")));
1195  result = (arg1 < 0) ? -arg1 : arg1;
1196  PG_RETURN_INT16(result);
1197 }
signed short int16
Definition: c.h:354
#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:446
#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 1427 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1428 {
1429  int16 arg1 = PG_GETARG_INT16(0);
1430  int16 arg2 = PG_GETARG_INT16(1);
1431 
1432  PG_RETURN_INT16(arg1 & arg2);
1433 }
signed short int16
Definition: c.h:354
#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 930 of file int.c.

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

931 {
932  int16 arg1 = PG_GETARG_INT16(0);
933  int16 arg2 = PG_GETARG_INT16(1);
934  int16 result;
935 
936  if (arg2 == 0)
937  {
938  ereport(ERROR,
939  (errcode(ERRCODE_DIVISION_BY_ZERO),
940  errmsg("division by zero")));
941  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
942  PG_RETURN_NULL();
943  }
944 
945  /*
946  * SHRT_MIN / -1 is problematic, since the result can't be represented on
947  * a two's-complement machine. Some machines produce SHRT_MIN, some
948  * produce zero, some throw an exception. We can dodge the problem by
949  * recognizing that division by -1 is the same as negation.
950  */
951  if (arg2 == -1)
952  {
953  if (unlikely(arg1 == PG_INT16_MIN))
954  ereport(ERROR,
955  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
956  errmsg("smallint out of range")));
957  result = -arg1;
958  PG_RETURN_INT16(result);
959  }
960 
961  /* No overflow is possible */
962 
963  result = arg1 / arg2;
964 
965  PG_RETURN_INT16(result);
966 }
signed short int16
Definition: c.h:354
#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:446
#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 431 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_BOOL.

432 {
433  int16 arg1 = PG_GETARG_INT16(0);
434  int16 arg2 = PG_GETARG_INT16(1);
435 
436  PG_RETURN_BOOL(arg1 == arg2);
437 }
signed short int16
Definition: c.h:354
#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 476 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_BOOL.

477 {
478  int16 arg1 = PG_GETARG_INT16(0);
479  int16 arg2 = PG_GETARG_INT16(1);
480 
481  PG_RETURN_BOOL(arg1 >= arg2);
482 }
signed short int16
Definition: c.h:354
#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 467 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_BOOL.

468 {
469  int16 arg1 = PG_GETARG_INT16(0);
470  int16 arg2 = PG_GETARG_INT16(1);
471 
472  PG_RETURN_BOOL(arg1 > arg2);
473 }
signed short int16
Definition: c.h:354
#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 1327 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1328 {
1329  int16 arg1 = PG_GETARG_INT16(0);
1330  int16 arg2 = PG_GETARG_INT16(1);
1331 
1332  PG_RETURN_INT16((arg1 > arg2) ? arg1 : arg2);
1333 }
signed short int16
Definition: c.h:354
#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 458 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_BOOL.

459 {
460  int16 arg1 = PG_GETARG_INT16(0);
461  int16 arg2 = PG_GETARG_INT16(1);
462 
463  PG_RETURN_BOOL(arg1 <= arg2);
464 }
signed short int16
Definition: c.h:354
#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 449 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_BOOL.

450 {
451  int16 arg1 = PG_GETARG_INT16(0);
452  int16 arg2 = PG_GETARG_INT16(1);
453 
454  PG_RETURN_BOOL(arg1 < arg2);
455 }
signed short int16
Definition: c.h:354
#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 901 of file int.c.

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

902 {
903  int16 arg1 = PG_GETARG_INT16(0);
904  int16 arg2 = PG_GETARG_INT16(1);
905  int16 result;
906 
907  if (unlikely(pg_sub_s16_overflow(arg1, arg2, &result)))
908  ereport(ERROR,
909  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
910  errmsg("smallint out of range")));
911  PG_RETURN_INT16(result);
912 }
signed short int16
Definition: c.h:354
#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 1139 of file int.c.

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

1140 {
1141  int16 arg1 = PG_GETARG_INT16(0);
1142  int16 arg2 = PG_GETARG_INT16(1);
1143 
1144  if (unlikely(arg2 == 0))
1145  {
1146  ereport(ERROR,
1147  (errcode(ERRCODE_DIVISION_BY_ZERO),
1148  errmsg("division by zero")));
1149  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1150  PG_RETURN_NULL();
1151  }
1152 
1153  /*
1154  * Some machines throw a floating-point exception for INT_MIN % -1, which
1155  * is a bit silly since the correct answer is perfectly well-defined,
1156  * namely zero. (It's not clear this ever happens when dealing with
1157  * int16, but we might as well have the test for safety.)
1158  */
1159  if (arg2 == -1)
1160  PG_RETURN_INT16(0);
1161 
1162  /* No overflow is possible */
1163 
1164  PG_RETURN_INT16(arg1 % arg2);
1165 }
signed short int16
Definition: c.h:354
#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 915 of file int.c.

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

916 {
917  int16 arg1 = PG_GETARG_INT16(0);
918  int16 arg2 = PG_GETARG_INT16(1);
919  int16 result;
920 
921  if (unlikely(pg_mul_s16_overflow(arg1, arg2, &result)))
922  ereport(ERROR,
923  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
924  errmsg("smallint out of range")));
925 
926  PG_RETURN_INT16(result);
927 }
signed short int16
Definition: c.h:354
#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 440 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_BOOL.

441 {
442  int16 arg1 = PG_GETARG_INT16(0);
443  int16 arg2 = PG_GETARG_INT16(1);
444 
445  PG_RETURN_BOOL(arg1 != arg2);
446 }
signed short int16
Definition: c.h:354
#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 1454 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1455 {
1456  int16 arg1 = PG_GETARG_INT16(0);
1457 
1458  PG_RETURN_INT16(~arg1);
1459 }
signed short int16
Definition: c.h:354
#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 1436 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1437 {
1438  int16 arg1 = PG_GETARG_INT16(0);
1439  int16 arg2 = PG_GETARG_INT16(1);
1440 
1441  PG_RETURN_INT16(arg1 | arg2);
1442 }
signed short int16
Definition: c.h:354
#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:354
void pg_itoa(int16 i, char *a)
Definition: numutils.c:337
#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

◆ int2pl()

Datum int2pl ( PG_FUNCTION_ARGS  )

Definition at line 887 of file int.c.

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

888 {
889  int16 arg1 = PG_GETARG_INT16(0);
890  int16 arg2 = PG_GETARG_INT16(1);
891  int16 result;
892 
893  if (unlikely(pg_add_s16_overflow(arg1, arg2, &result)))
894  ereport(ERROR,
895  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
896  errmsg("smallint out of range")));
897  PG_RETURN_INT16(result);
898 }
signed short int16
Definition: c.h:354
#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:354
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:354
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 1463 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_INT16.

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

◆ int2shr()

Datum int2shr ( PG_FUNCTION_ARGS  )

Definition at line 1472 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_INT16.

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

◆ int2smaller()

Datum int2smaller ( PG_FUNCTION_ARGS  )

Definition at line 1336 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1337 {
1338  int16 arg1 = PG_GETARG_INT16(0);
1339  int16 arg2 = PG_GETARG_INT16(1);
1340 
1341  PG_RETURN_INT16((arg1 < arg2) ? arg1 : arg2);
1342 }
signed short int16
Definition: c.h:354
#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 867 of file int.c.

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

868 {
870 
871  if (unlikely(arg == PG_INT16_MIN))
872  ereport(ERROR,
873  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
874  errmsg("smallint out of range")));
875  PG_RETURN_INT16(-arg);
876 }
signed short int16
Definition: c.h:354
#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:446
#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 879 of file int.c.

References arg, PG_GETARG_INT16, and PG_RETURN_INT16.

880 {
882 
883  PG_RETURN_INT16(arg);
884 }
signed short int16
Definition: c.h:354
#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:354
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
int lbound1
Definition: c.h:590
int errcode(int sqlerrcode)
Definition: elog.c:610
Oid elemtype
Definition: c.h:588
#define FUNC_MAX_ARGS
int32 dataoffset
Definition: c.h:587
#define ERROR
Definition: elog.h:43
int ndim
Definition: c.h:586
Definition: c.h:583
void * palloc0(Size size)
Definition: mcxt.c:980
int dim1
Definition: c.h:589
#define ereport(elevel,...)
Definition: elog.h:144
int16 values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:591
#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  pg_itoa(int2Array->values[num], rp);
195  while (*++rp != '\0')
196  ;
197  }
198  *rp = '\0';
199  PG_RETURN_CSTRING(result);
200 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
void pg_itoa(int16 i, char *a)
Definition: numutils.c:337
Definition: c.h:583
int dim1
Definition: c.h:589
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:361
int16 values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:591
void * palloc(Size size)
Definition: mcxt.c:949

◆ int2vectorrecv()

Datum int2vectorrecv ( PG_FUNCTION_ARGS  )

Definition at line 206 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.

207 {
208  LOCAL_FCINFO(locfcinfo, 3);
210  int2vector *result;
211 
212  /*
213  * Normally one would call array_recv() using DirectFunctionCall3, but
214  * that does not work since array_recv wants to cache some data using
215  * fcinfo->flinfo->fn_extra. So we need to pass it our own flinfo
216  * parameter.
217  */
218  InitFunctionCallInfoData(*locfcinfo, fcinfo->flinfo, 3,
219  InvalidOid, NULL, NULL);
220 
221  locfcinfo->args[0].value = PointerGetDatum(buf);
222  locfcinfo->args[0].isnull = false;
223  locfcinfo->args[1].value = ObjectIdGetDatum(INT2OID);
224  locfcinfo->args[1].isnull = false;
225  locfcinfo->args[2].value = Int32GetDatum(-1);
226  locfcinfo->args[2].isnull = false;
227 
228  result = (int2vector *) DatumGetPointer(array_recv(locfcinfo));
229 
230  Assert(!locfcinfo->isnull);
231 
232  /* sanity checks: int2vector must be 1-D, 0-based, no nulls */
233  if (ARR_NDIM(result) != 1 ||
234  ARR_HASNULL(result) ||
235  ARR_ELEMTYPE(result) != INT2OID ||
236  ARR_LBOUND(result)[0] != 0)
237  ereport(ERROR,
238  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
239  errmsg("invalid int2vector data")));
240 
241  /* check length for consistency with int2vectorin() */
242  if (ARR_DIMS(result)[0] > FUNC_MAX_ARGS)
243  ereport(ERROR,
244  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
245  errmsg("oidvector has too many elements")));
246 
247  PG_RETURN_POINTER(result);
248 }
#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:583
#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:738
#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 254 of file int.c.

References array_send().

255 {
256  return array_send(fcinfo);
257 }
Datum array_send(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1548

◆ int2xor()

Datum int2xor ( PG_FUNCTION_ARGS  )

Definition at line 1445 of file int.c.

References PG_GETARG_INT16, and PG_RETURN_INT16.

1446 {
1447  int16 arg1 = PG_GETARG_INT16(0);
1448  int16 arg2 = PG_GETARG_INT16(1);
1449 
1450  PG_RETURN_INT16(arg1 ^ arg2);
1451 }
signed short int16
Definition: c.h:354
#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 1072 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.

1073 {
1074  int32 arg1 = PG_GETARG_INT32(0);
1075  int16 arg2 = PG_GETARG_INT16(1);
1076  int32 result;
1077 
1078  if (unlikely(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  * INT_MIN / -1 is problematic, since the result can't be represented on a
1089  * two's-complement machine. Some machines produce INT_MIN, some produce
1090  * zero, some throw an exception. We can dodge the problem by recognizing
1091  * that division by -1 is the same as negation.
1092  */
1093  if (arg2 == -1)
1094  {
1095  if (unlikely(arg1 == PG_INT32_MIN))
1096  ereport(ERROR,
1097  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1098  errmsg("integer out of range")));
1099  result = -arg1;
1100  PG_RETURN_INT32(result);
1101  }
1102 
1103  /* No overflow is possible */
1104 
1105  result = arg1 / arg2;
1106 
1107  PG_RETURN_INT32(result);
1108 }
signed short int16
Definition: c.h:354
#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:355
#define ERROR
Definition: elog.h:43
#define PG_INT32_MIN
Definition: c.h:449
#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 539 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

540 {
541  int32 arg1 = PG_GETARG_INT32(0);
542  int16 arg2 = PG_GETARG_INT16(1);
543 
544  PG_RETURN_BOOL(arg1 == arg2);
545 }
signed short int16
Definition: c.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#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 584 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

585 {
586  int32 arg1 = PG_GETARG_INT32(0);
587  int16 arg2 = PG_GETARG_INT16(1);
588 
589  PG_RETURN_BOOL(arg1 >= arg2);
590 }
signed short int16
Definition: c.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#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 575 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

576 {
577  int32 arg1 = PG_GETARG_INT32(0);
578  int16 arg2 = PG_GETARG_INT16(1);
579 
580  PG_RETURN_BOOL(arg1 > arg2);
581 }
signed short int16
Definition: c.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#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 566 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

567 {
568  int32 arg1 = PG_GETARG_INT32(0);
569  int16 arg2 = PG_GETARG_INT16(1);
570 
571  PG_RETURN_BOOL(arg1 <= arg2);
572 }
signed short int16
Definition: c.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#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 557 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

558 {
559  int32 arg1 = PG_GETARG_INT32(0);
560  int16 arg2 = PG_GETARG_INT16(1);
561 
562  PG_RETURN_BOOL(arg1 < arg2);
563 }
signed short int16
Definition: c.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#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 1044 of file int.c.

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

1045 {
1046  int32 arg1 = PG_GETARG_INT32(0);
1047  int16 arg2 = PG_GETARG_INT16(1);
1048  int32 result;
1049 
1050  if (unlikely(pg_sub_s32_overflow(arg1, (int32) arg2, &result)))
1051  ereport(ERROR,
1052  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1053  errmsg("integer out of range")));
1054  PG_RETURN_INT32(result);
1055 }
signed short int16
Definition: c.h:354
#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:355
#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 1058 of file int.c.

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

1059 {
1060  int32 arg1 = PG_GETARG_INT32(0);
1061  int16 arg2 = PG_GETARG_INT16(1);
1062  int32 result;
1063 
1064  if (unlikely(pg_mul_s32_overflow(arg1, (int32) arg2, &result)))
1065  ereport(ERROR,
1066  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1067  errmsg("integer out of range")));
1068  PG_RETURN_INT32(result);
1069 }
signed short int16
Definition: c.h:354
#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:355
#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 548 of file int.c.

References PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

549 {
550  int32 arg1 = PG_GETARG_INT32(0);
551  int16 arg2 = PG_GETARG_INT16(1);
552 
553  PG_RETURN_BOOL(arg1 != arg2);
554 }
signed short int16
Definition: c.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#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 1030 of file int.c.

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

1031 {
1032  int32 arg1 = PG_GETARG_INT32(0);
1033  int16 arg2 = PG_GETARG_INT16(1);
1034  int32 result;
1035 
1036  if (unlikely(pg_add_s32_overflow(arg1, (int32) arg2, &result)))
1037  ereport(ERROR,
1038  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1039  errmsg("integer out of range")));
1040  PG_RETURN_INT32(result);
1041 }
signed short int16
Definition: c.h:354
#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:355
#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 343 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_BOOL.

344 {
345  if (PG_GETARG_INT32(0) == 0)
346  PG_RETURN_BOOL(false);
347  else
348  PG_RETURN_BOOL(true);
349 }
#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 1172 of file int.c.

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

1173 {
1174  int32 arg1 = PG_GETARG_INT32(0);
1175  int32 result;
1176 
1177  if (unlikely(arg1 == PG_INT32_MIN))
1178  ereport(ERROR,
1179  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1180  errmsg("integer out of range")));
1181  result = (arg1 < 0) ? -arg1 : arg1;
1182  PG_RETURN_INT32(result);
1183 }
#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:355
#define ERROR
Definition: elog.h:43
#define PG_INT32_MIN
Definition: c.h:449
#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 1374 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

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

◆ int4div()

Datum int4div ( PG_FUNCTION_ARGS  )

Definition at line 814 of file int.c.

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

815 {
816  int32 arg1 = PG_GETARG_INT32(0);
817  int32 arg2 = PG_GETARG_INT32(1);
818  int32 result;
819 
820  if (arg2 == 0)
821  {
822  ereport(ERROR,
823  (errcode(ERRCODE_DIVISION_BY_ZERO),
824  errmsg("division by zero")));
825  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
826  PG_RETURN_NULL();
827  }
828 
829  /*
830  * INT_MIN / -1 is problematic, since the result can't be represented on a
831  * two's-complement machine. Some machines produce INT_MIN, some produce
832  * zero, some throw an exception. We can dodge the problem by recognizing
833  * that division by -1 is the same as negation.
834  */
835  if (arg2 == -1)
836  {
837  if (unlikely(arg1 == PG_INT32_MIN))
838  ereport(ERROR,
839  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
840  errmsg("integer out of range")));
841  result = -arg1;
842  PG_RETURN_INT32(result);
843  }
844 
845  /* No overflow is possible */
846 
847  result = arg1 / arg2;
848 
849  PG_RETURN_INT32(result);
850 }
#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:355
#define ERROR
Definition: elog.h:43
#define PG_INT32_MIN
Definition: c.h:449
#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 377 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_BOOL.

378 {
379  int32 arg1 = PG_GETARG_INT32(0);
380  int32 arg2 = PG_GETARG_INT32(1);
381 
382  PG_RETURN_BOOL(arg1 == arg2);
383 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358

◆ int4gcd()

Datum int4gcd ( PG_FUNCTION_ARGS  )

Definition at line 1275 of file int.c.

References int4gcd_internal(), PG_GETARG_INT32, and PG_RETURN_INT32.

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

◆ int4gcd_internal()

static int32 int4gcd_internal ( int32  arg1,
int32  arg2 
)
static

Definition at line 1214 of file int.c.

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

Referenced by int4gcd(), and int4lcm().

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

References PG_GETARG_INT32, and PG_RETURN_BOOL.

423 {
424  int32 arg1 = PG_GETARG_INT32(0);
425  int32 arg2 = PG_GETARG_INT32(1);
426 
427  PG_RETURN_BOOL(arg1 >= arg2);
428 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358

◆ int4gt()

Datum int4gt ( PG_FUNCTION_ARGS  )

Definition at line 413 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_BOOL.

414 {
415  int32 arg1 = PG_GETARG_INT32(0);
416  int32 arg2 = PG_GETARG_INT32(1);
417 
418  PG_RETURN_BOOL(arg1 > arg2);
419 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358

◆ int4in()

Datum int4in ( PG_FUNCTION_ARGS  )

Definition at line 268 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().

269 {
270  char *num = PG_GETARG_CSTRING(0);
271 
273 }
#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 853 of file int.c.

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

854 {
856  int32 result;
857 
858  if (unlikely(pg_add_s32_overflow(arg, 1, &result)))
859  ereport(ERROR,
860  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
861  errmsg("integer out of range")));
862 
863  PG_RETURN_INT32(result);
864 }
#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:355
#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 1345 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

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

◆ int4lcm()

Datum int4lcm ( PG_FUNCTION_ARGS  )

Definition at line 1290 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.

1291 {
1292  int32 arg1 = PG_GETARG_INT32(0);
1293  int32 arg2 = PG_GETARG_INT32(1);
1294  int32 gcd;
1295  int32 result;
1296 
1297  /*
1298  * Handle lcm(x, 0) = lcm(0, x) = 0 as a special case. This prevents a
1299  * division-by-zero error below when x is zero, and an overflow error from
1300  * the GCD computation when x = INT_MIN.
1301  */
1302  if (arg1 == 0 || arg2 == 0)
1303  PG_RETURN_INT32(0);
1304 
1305  /* lcm(x, y) = abs(x / gcd(x, y) * y) */
1306  gcd = int4gcd_internal(arg1, arg2);
1307  arg1 = arg1 / gcd;
1308 
1309  if (unlikely(pg_mul_s32_overflow(arg1, arg2, &result)))
1310  ereport(ERROR,
1311  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1312  errmsg("integer out of range")));
1313 
1314  /* If the result is INT_MIN, it cannot be represented. */
1315  if (unlikely(result == PG_INT32_MIN))
1316  ereport(ERROR,
1317  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1318  errmsg("integer out of range")));
1319 
1320  if (result < 0)
1321  result = -result;
1322 
1323  PG_RETURN_INT32(result);
1324 }
#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:355
#define ERROR
Definition: elog.h:43
#define PG_INT32_MIN
Definition: c.h:449
#define ereport(elevel,...)
Definition: elog.h:144
static int32 int4gcd_internal(int32 arg1, int32 arg2)
Definition: int.c:1214
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 404 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_BOOL.

405 {
406  int32 arg1 = PG_GETARG_INT32(0);
407  int32 arg2 = PG_GETARG_INT32(1);
408 
409  PG_RETURN_BOOL(arg1 <= arg2);
410 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358

◆ int4lt()

Datum int4lt ( PG_FUNCTION_ARGS  )

Definition at line 395 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_BOOL.

396 {
397  int32 arg1 = PG_GETARG_INT32(0);
398  int32 arg2 = PG_GETARG_INT32(1);
399 
400  PG_RETURN_BOOL(arg1 < arg2);
401 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358

◆ int4mi()

Datum int4mi ( PG_FUNCTION_ARGS  )

Definition at line 786 of file int.c.

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

787 {
788  int32 arg1 = PG_GETARG_INT32(0);
789  int32 arg2 = PG_GETARG_INT32(1);
790  int32 result;
791 
792  if (unlikely(pg_sub_s32_overflow(arg1, arg2, &result)))
793  ereport(ERROR,
794  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
795  errmsg("integer out of range")));
796  PG_RETURN_INT32(result);
797 }
#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:355
#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 1111 of file int.c.

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

1112 {
1113  int32 arg1 = PG_GETARG_INT32(0);
1114  int32 arg2 = PG_GETARG_INT32(1);
1115 
1116  if (unlikely(arg2 == 0))
1117  {
1118  ereport(ERROR,
1119  (errcode(ERRCODE_DIVISION_BY_ZERO),
1120  errmsg("division by zero")));
1121  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1122  PG_RETURN_NULL();
1123  }
1124 
1125  /*
1126  * Some machines throw a floating-point exception for INT_MIN % -1, which
1127  * is a bit silly since the correct answer is perfectly well-defined,
1128  * namely zero.
1129  */
1130  if (arg2 == -1)
1131  PG_RETURN_INT32(0);
1132 
1133  /* No overflow is possible */
1134 
1135  PG_RETURN_INT32(arg1 % arg2);
1136 }
#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:355
#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 800 of file int.c.

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

801 {
802  int32 arg1 = PG_GETARG_INT32(0);
803  int32 arg2 = PG_GETARG_INT32(1);
804  int32 result;
805 
806  if (unlikely(pg_mul_s32_overflow(arg1, arg2, &result)))
807  ereport(ERROR,
808  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
809  errmsg("integer out of range")));
810  PG_RETURN_INT32(result);
811 }
#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:355
#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 386 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_BOOL.

387 {
388  int32 arg1 = PG_GETARG_INT32(0);
389  int32 arg2 = PG_GETARG_INT32(1);
390 
391  PG_RETURN_BOOL(arg1 != arg2);
392 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358

◆ int4not()

Datum int4not ( PG_FUNCTION_ARGS  )

Definition at line 1419 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

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

◆ int4or()

Datum int4or ( PG_FUNCTION_ARGS  )

Definition at line 1383 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

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

◆ int4out()

Datum int4out ( PG_FUNCTION_ARGS  )

Definition at line 279 of file int.c.

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

Referenced by int4_to_char().

280 {
281  int32 arg1 = PG_GETARG_INT32(0);
282  char *result = (char *) palloc(12); /* sign, 10 digits, '\0' */
283 
284  pg_ltoa(arg1, result);
285  PG_RETURN_CSTRING(result);
286 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
signed int int32
Definition: c.h:355
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:361
void * palloc(Size size)
Definition: mcxt.c:949
void pg_ltoa(int32 value, char *a)
Definition: numutils.c:414

◆ int4pl()

Datum int4pl ( PG_FUNCTION_ARGS  )

Definition at line 772 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().

773 {
774  int32 arg1 = PG_GETARG_INT32(0);
775  int32 arg2 = PG_GETARG_INT32(1);
776  int32 result;
777 
778  if (unlikely(pg_add_s32_overflow(arg1, arg2, &result)))
779  ereport(ERROR,
780  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
781  errmsg("integer out of range")));
782  PG_RETURN_INT32(result);
783 }
#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:355
#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 292 of file int.c.

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

293 {
295 
296  PG_RETURN_INT32((int32) pq_getmsgint(buf, sizeof(int32)));
297 }
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:355
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 303 of file int.c.

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

304 {
305  int32 arg1 = PG_GETARG_INT32(0);
307 
308  pq_begintypsend(&buf);
309  pq_sendint32(&buf, arg1);
311 }
#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:355
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 1401 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

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

◆ int4shr()

Datum int4shr ( PG_FUNCTION_ARGS  )

Definition at line 1410 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

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

◆ int4smaller()

Datum int4smaller ( PG_FUNCTION_ARGS  )

Definition at line 1354 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

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

◆ int4um()

Datum int4um ( PG_FUNCTION_ARGS  )

Definition at line 752 of file int.c.

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

753 {
755 
756  if (unlikely(arg == PG_INT32_MIN))
757  ereport(ERROR,
758  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
759  errmsg("integer out of range")));
760  PG_RETURN_INT32(-arg);
761 }
#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:355
#define ERROR
Definition: elog.h:43
#define PG_INT32_MIN
Definition: c.h:449
#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 764 of file int.c.

References arg, PG_GETARG_INT32, and PG_RETURN_INT32.

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

◆ int4xor()

Datum int4xor ( PG_FUNCTION_ARGS  )

Definition at line 1392 of file int.c.

References PG_GETARG_INT32, and PG_RETURN_INT32.

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