PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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)
 
static void check_valid_int2vector (const int2vector *int2Array)
 
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.

Function Documentation

◆ bool_int4()

Datum bool_int4 ( PG_FUNCTION_ARGS  )

Definition at line 401 of file int.c.

402{
403 if (PG_GETARG_BOOL(0) == false)
405 else
407}
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
#define PG_GETARG_BOOL(n)
Definition fmgr.h:274

References PG_GETARG_BOOL, and PG_RETURN_INT32.

Referenced by ExecEvalJsonCoercion().

◆ buildint2vector()

int2vector * buildint2vector ( const int16 int2s,
int  n 
)

Definition at line 114 of file int.c.

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}
int16_t int16
Definition c.h:541
#define Int2VectorSize(n)
Definition int.c:45
void * palloc0(Size size)
Definition mcxt.c:1417
static int fb(int x)
int ndim
Definition c.h:736
int dim1
Definition c.h:739
Oid elemtype
Definition c.h:738
int16 values[FLEXIBLE_ARRAY_MEMBER]
Definition c.h:741
int32 dataoffset
Definition c.h:737
int lbound1
Definition c.h:740
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432

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

Referenced by attnumstoint2vector(), CreateStatistics(), CreateTriggerFiringOn(), pg_get_publication_tables(), StorePartitionKey(), and UpdateIndexRelation().

◆ check_valid_int2vector()

static void check_valid_int2vector ( const int2vector int2Array)
static

Definition at line 145 of file int.c.

146{
147 /*
148 * We insist on ndim == 1 and dataoffset == 0 (that is, no nulls) because
149 * otherwise the array's layout will not be what calling code expects. We
150 * needn't be picky about the index lower bound though. Checking elemtype
151 * is just paranoia.
152 */
153 if (int2Array->ndim != 1 ||
154 int2Array->dataoffset != 0 ||
155 int2Array->elemtype != INT2OID)
158 errmsg("array is not a valid int2vector")));
159}
int errcode(int sqlerrcode)
Definition elog.c:864
int errmsg(const char *fmt,...)
Definition elog.c:1081
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150

References ereport, errcode(), errmsg(), ERROR, and fb().

Referenced by int2vectorout().

◆ generate_series_int4()

Datum generate_series_int4 ( PG_FUNCTION_ARGS  )

Definition at line 1532 of file int.c.

1533{
1534 return generate_series_step_int4(fcinfo);
1535}
Datum generate_series_step_int4(PG_FUNCTION_ARGS)
Definition int.c:1538

References generate_series_step_int4().

◆ generate_series_int4_support()

Datum generate_series_int4_support ( PG_FUNCTION_ARGS  )

Definition at line 1614 of file int.c.

1615{
1617 Node *ret = NULL;
1618
1620 {
1621 /* Try to estimate the number of rows returned */
1623
1624 if (is_funcclause(req->node)) /* be paranoid */
1625 {
1626 List *args = ((FuncExpr *) req->node)->args;
1627 Node *arg1,
1628 *arg2,
1629 *arg3;
1630
1631 /* We can use estimated argument values here */
1633 arg2 = estimate_expression_value(req->root, lsecond(args));
1634 if (list_length(args) >= 3)
1635 arg3 = estimate_expression_value(req->root, lthird(args));
1636 else
1637 arg3 = NULL;
1638
1639 /*
1640 * If any argument is constant NULL, we can safely assume that
1641 * zero rows are returned. Otherwise, if they're all non-NULL
1642 * constants, we can calculate the number of rows that will be
1643 * returned. Use double arithmetic to avoid overflow hazards.
1644 */
1645 if ((IsA(arg1, Const) &&
1646 ((Const *) arg1)->constisnull) ||
1647 (IsA(arg2, Const) &&
1648 ((Const *) arg2)->constisnull) ||
1649 (arg3 != NULL && IsA(arg3, Const) &&
1650 ((Const *) arg3)->constisnull))
1651 {
1652 req->rows = 0;
1653 ret = (Node *) req;
1654 }
1655 else if (IsA(arg1, Const) &&
1656 IsA(arg2, Const) &&
1657 (arg3 == NULL || IsA(arg3, Const)))
1658 {
1659 double start,
1660 finish,
1661 step;
1662
1664 finish = DatumGetInt32(((Const *) arg2)->constvalue);
1665 step = arg3 ? DatumGetInt32(((Const *) arg3)->constvalue) : 1;
1666
1667 /* This equation works for either sign of step */
1668 if (step != 0)
1669 {
1670 req->rows = floor((finish - start + step) / step);
1671 ret = (Node *) req;
1672 }
1673 }
1674 }
1675 }
1676
1677 PG_RETURN_POINTER(ret);
1678}
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition clauses.c:2408
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
return str start
static bool is_funcclause(const void *clause)
Definition nodeFuncs.h:69
#define IsA(nodeptr, _type_)
Definition nodes.h:164
static int list_length(const List *l)
Definition pg_list.h:152
#define lthird(l)
Definition pg_list.h:188
#define linitial(l)
Definition pg_list.h:178
#define lsecond(l)
Definition pg_list.h:183
static int32 DatumGetInt32(Datum X)
Definition postgres.h:212
Definition pg_list.h:54
Definition nodes.h:135

References DatumGetInt32(), estimate_expression_value(), fb(), is_funcclause(), IsA, linitial, list_length(), lsecond, lthird, PG_GETARG_POINTER, PG_RETURN_POINTER, and start.

◆ generate_series_step_int4()

Datum generate_series_step_int4 ( PG_FUNCTION_ARGS  )

Definition at line 1538 of file int.c.

1539{
1542 int32 result;
1543 MemoryContext oldcontext;
1544
1545 /* stuff done only on the first call of the function */
1546 if (SRF_IS_FIRSTCALL())
1547 {
1549 int32 finish = PG_GETARG_INT32(1);
1550 int32 step = 1;
1551
1552 /* see if we were given an explicit step size */
1553 if (PG_NARGS() == 3)
1554 step = PG_GETARG_INT32(2);
1555 if (step == 0)
1556 ereport(ERROR,
1558 errmsg("step size cannot equal zero")));
1559
1560 /* create a function context for cross-call persistence */
1562
1563 /*
1564 * switch to memory context appropriate for multiple function calls
1565 */
1566 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1567
1568 /* allocate memory for user context */
1570
1571 /*
1572 * Use fctx to keep state from call to call. Seed current with the
1573 * original start value
1574 */
1575 fctx->current = start;
1576 fctx->finish = finish;
1577 fctx->step = step;
1578
1579 funcctx->user_fctx = fctx;
1580 MemoryContextSwitchTo(oldcontext);
1581 }
1582
1583 /* stuff done on every call of the function */
1585
1586 /*
1587 * get the saved state and use current as the result for this iteration
1588 */
1589 fctx = funcctx->user_fctx;
1590 result = fctx->current;
1591
1592 if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1593 (fctx->step < 0 && fctx->current >= fctx->finish))
1594 {
1595 /*
1596 * Increment current in preparation for next iteration. If next-value
1597 * computation overflows, this is the final result.
1598 */
1599 if (pg_add_s32_overflow(fctx->current, fctx->step, &fctx->current))
1600 fctx->step = 0;
1601
1602 /* do when there is more left to send */
1604 }
1605 else
1606 /* do when there is no more left */
1608}
int32_t int32
Definition c.h:542
#define palloc_object(type)
Definition fe_memutils.h:74
#define PG_NARGS()
Definition fmgr.h:203
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
#define SRF_IS_FIRSTCALL()
Definition funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition funcapi.h:308
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition funcapi.h:306
#define SRF_RETURN_DONE(_funcctx)
Definition funcapi.h:328
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition int.h:151
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
static Datum Int32GetDatum(int32 X)
Definition postgres.h:222

References ereport, errcode(), errmsg(), ERROR, fb(), Int32GetDatum(), MemoryContextSwitchTo(), palloc_object, pg_add_s32_overflow(), PG_GETARG_INT32, PG_NARGS, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, and start.

Referenced by generate_series_int4().

◆ i2toi4()

Datum i2toi4 ( PG_FUNCTION_ARGS  )

Definition at line 369 of file int.c.

370{
372
374}
#define PG_GETARG_INT16(n)
Definition fmgr.h:271

References fb(), PG_GETARG_INT16, and PG_RETURN_INT32.

◆ i4toi2()

Datum i4toi2 ( PG_FUNCTION_ARGS  )

Definition at line 377 of file int.c.

378{
380
384 errmsg("smallint out of range")));
385
387}
#define unlikely(x)
Definition c.h:412
#define PG_RETURN_INT16(x)
Definition fmgr.h:357

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

◆ in_range_int2_int2()

Datum in_range_int2_int2 ( PG_FUNCTION_ARGS  )

Definition at line 768 of file int.c.

769{
770 /* Doesn't seem worth duplicating code for, so just invoke int2_int4 */
776 PG_GETARG_DATUM(4));
777}
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
#define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5)
Definition fmgr.h:692
Datum in_range_int2_int4(PG_FUNCTION_ARGS)
Definition int.c:733

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

◆ in_range_int2_int4()

Datum in_range_int2_int4 ( PG_FUNCTION_ARGS  )

Definition at line 733 of file int.c.

734{
735 /* We must do all the math in int32 */
737 int32 base = (int32) PG_GETARG_INT16(1);
738 int32 offset = PG_GETARG_INT32(2);
739 bool sub = PG_GETARG_BOOL(3);
740 bool less = PG_GETARG_BOOL(4);
741 int32 sum;
742
743 if (offset < 0)
746 errmsg("invalid preceding or following size in window function")));
747
748 if (sub)
749 offset = -offset; /* cannot overflow */
750
751 if (unlikely(pg_add_s32_overflow(base, offset, &sum)))
752 {
753 /*
754 * If sub is false, the true sum is surely more than val, so correct
755 * answer is the same as "less". If sub is true, the true sum is
756 * surely less than val, so the answer is "!less".
757 */
758 PG_RETURN_BOOL(sub ? !less : less);
759 }
760
761 if (less)
762 PG_RETURN_BOOL(val <= sum);
763 else
764 PG_RETURN_BOOL(val >= sum);
765}
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
long val
Definition informix.c:689

References ereport, errcode(), errmsg(), ERROR, fb(), 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().

◆ in_range_int2_int8()

Datum in_range_int2_int8 ( PG_FUNCTION_ARGS  )

Definition at line 780 of file int.c.

781{
782 /* Doesn't seem worth duplicating code for, so just invoke int4_int8 */
788 PG_GETARG_DATUM(4));
789}
Datum in_range_int4_int8(PG_FUNCTION_ARGS)
Definition int.c:698

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

◆ in_range_int4_int2()

Datum in_range_int4_int2 ( PG_FUNCTION_ARGS  )

Definition at line 686 of file int.c.

687{
688 /* Doesn't seem worth duplicating code for, so just invoke int4_int4 */
694 PG_GETARG_DATUM(4));
695}
Datum in_range_int4_int4(PG_FUNCTION_ARGS)
Definition int.c:652

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

◆ in_range_int4_int4()

Datum in_range_int4_int4 ( PG_FUNCTION_ARGS  )

Definition at line 652 of file int.c.

653{
655 int32 base = PG_GETARG_INT32(1);
656 int32 offset = PG_GETARG_INT32(2);
657 bool sub = PG_GETARG_BOOL(3);
658 bool less = PG_GETARG_BOOL(4);
659 int32 sum;
660
661 if (offset < 0)
664 errmsg("invalid preceding or following size in window function")));
665
666 if (sub)
667 offset = -offset; /* cannot overflow */
668
669 if (unlikely(pg_add_s32_overflow(base, offset, &sum)))
670 {
671 /*
672 * If sub is false, the true sum is surely more than val, so correct
673 * answer is the same as "less". If sub is true, the true sum is
674 * surely less than val, so the answer is "!less".
675 */
676 PG_RETURN_BOOL(sub ? !less : less);
677 }
678
679 if (less)
680 PG_RETURN_BOOL(val <= sum);
681 else
682 PG_RETURN_BOOL(val >= sum);
683}

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

Referenced by in_range_int4_int2().

◆ in_range_int4_int8()

Datum in_range_int4_int8 ( PG_FUNCTION_ARGS  )

Definition at line 698 of file int.c.

699{
700 /* We must do all the math in int64 */
702 int64 base = (int64) PG_GETARG_INT32(1);
703 int64 offset = PG_GETARG_INT64(2);
704 bool sub = PG_GETARG_BOOL(3);
705 bool less = PG_GETARG_BOOL(4);
706 int64 sum;
707
708 if (offset < 0)
711 errmsg("invalid preceding or following size in window function")));
712
713 if (sub)
714 offset = -offset; /* cannot overflow */
715
716 if (unlikely(pg_add_s64_overflow(base, offset, &sum)))
717 {
718 /*
719 * If sub is false, the true sum is surely more than val, so correct
720 * answer is the same as "less". If sub is true, the true sum is
721 * surely less than val, so the answer is "!less".
722 */
723 PG_RETURN_BOOL(sub ? !less : less);
724 }
725
726 if (less)
727 PG_RETURN_BOOL(val <= sum);
728 else
729 PG_RETURN_BOOL(val >= sum);
730}
int64_t int64
Definition c.h:543
#define PG_GETARG_INT64(n)
Definition fmgr.h:284
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:235

References ereport, errcode(), errmsg(), ERROR, fb(), 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().

◆ int24div()

Datum int24div ( PG_FUNCTION_ARGS  )

Definition at line 1059 of file int.c.

1060{
1063
1064 if (unlikely(arg2 == 0))
1065 {
1066 ereport(ERROR,
1068 errmsg("division by zero")));
1069 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1071 }
1072
1073 /* No overflow is possible */
1075}
#define PG_RETURN_NULL()
Definition fmgr.h:346

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

◆ int24eq()

Datum int24eq ( PG_FUNCTION_ARGS  )

Definition at line 533 of file int.c.

534{
537
539}

References fb(), PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int24ge()

Datum int24ge ( PG_FUNCTION_ARGS  )

Definition at line 578 of file int.c.

579{
582
584}

References fb(), PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int24gt()

Datum int24gt ( PG_FUNCTION_ARGS  )

Definition at line 569 of file int.c.

570{
573
575}

References fb(), PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int24le()

Datum int24le ( PG_FUNCTION_ARGS  )

Definition at line 560 of file int.c.

561{
564
566}

References fb(), PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int24lt()

Datum int24lt ( PG_FUNCTION_ARGS  )

Definition at line 551 of file int.c.

552{
555
557}

References fb(), PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int24mi()

Datum int24mi ( PG_FUNCTION_ARGS  )

Definition at line 1031 of file int.c.

1032{
1035 int32 result;
1036
1037 if (unlikely(pg_sub_s32_overflow((int32) arg1, arg2, &result)))
1038 ereport(ERROR,
1040 errmsg("integer out of range")));
1041 PG_RETURN_INT32(result);
1042}
static bool pg_sub_s32_overflow(int32 a, int32 b, int32 *result)
Definition int.h:169

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

◆ int24mul()

Datum int24mul ( PG_FUNCTION_ARGS  )

Definition at line 1045 of file int.c.

1046{
1049 int32 result;
1050
1051 if (unlikely(pg_mul_s32_overflow((int32) arg1, arg2, &result)))
1052 ereport(ERROR,
1054 errmsg("integer out of range")));
1055 PG_RETURN_INT32(result);
1056}
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition int.h:187

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

◆ int24ne()

Datum int24ne ( PG_FUNCTION_ARGS  )

Definition at line 542 of file int.c.

543{
546
548}

References fb(), PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int24pl()

Datum int24pl ( PG_FUNCTION_ARGS  )

Definition at line 1017 of file int.c.

1018{
1021 int32 result;
1022
1023 if (unlikely(pg_add_s32_overflow((int32) arg1, arg2, &result)))
1024 ereport(ERROR,
1026 errmsg("integer out of range")));
1027 PG_RETURN_INT32(result);
1028}

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

◆ int2abs()

Datum int2abs ( PG_FUNCTION_ARGS  )

Definition at line 1234 of file int.c.

1235{
1237 int16 result;
1238
1239 if (unlikely(arg1 == PG_INT16_MIN))
1240 ereport(ERROR,
1242 errmsg("smallint out of range")));
1243 result = (arg1 < 0) ? -arg1 : arg1;
1244 PG_RETURN_INT16(result);
1245}
#define PG_INT16_MIN
Definition c.h:599

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

◆ int2and()

Datum int2and ( PG_FUNCTION_ARGS  )

Definition at line 1475 of file int.c.

1476{
1479
1481}

References fb(), PG_GETARG_INT16, and PG_RETURN_INT16.

◆ int2div()

Datum int2div ( PG_FUNCTION_ARGS  )

Definition at line 978 of file int.c.

979{
982 int16 result;
983
984 if (arg2 == 0)
985 {
988 errmsg("division by zero")));
989 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
991 }
992
993 /*
994 * SHRT_MIN / -1 is problematic, since the result can't be represented on
995 * a two's-complement machine. Some machines produce SHRT_MIN, some
996 * produce zero, some throw an exception. We can dodge the problem by
997 * recognizing that division by -1 is the same as negation.
998 */
999 if (arg2 == -1)
1000 {
1001 if (unlikely(arg1 == PG_INT16_MIN))
1002 ereport(ERROR,
1004 errmsg("smallint out of range")));
1005 result = -arg1;
1006 PG_RETURN_INT16(result);
1007 }
1008
1009 /* No overflow is possible */
1010
1011 result = arg1 / arg2;
1012
1013 PG_RETURN_INT16(result);
1014}

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

◆ int2eq()

Datum int2eq ( PG_FUNCTION_ARGS  )

Definition at line 479 of file int.c.

480{
483
485}

References fb(), PG_GETARG_INT16, and PG_RETURN_BOOL.

◆ int2ge()

Datum int2ge ( PG_FUNCTION_ARGS  )

Definition at line 524 of file int.c.

525{
528
530}

References fb(), PG_GETARG_INT16, and PG_RETURN_BOOL.

◆ int2gt()

Datum int2gt ( PG_FUNCTION_ARGS  )

Definition at line 515 of file int.c.

516{
519
521}

References fb(), PG_GETARG_INT16, and PG_RETURN_BOOL.

◆ int2in()

Datum int2in ( PG_FUNCTION_ARGS  )

Definition at line 63 of file int.c.

64{
65 char *num = PG_GETARG_CSTRING(0);
66
67 PG_RETURN_INT16(pg_strtoint16_safe(num, fcinfo->context));
68}
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278
int16 pg_strtoint16_safe(const char *s, Node *escontext)
Definition numutils.c:127

References PG_GETARG_CSTRING, PG_RETURN_INT16, and pg_strtoint16_safe().

◆ int2larger()

Datum int2larger ( PG_FUNCTION_ARGS  )

Definition at line 1375 of file int.c.

1376{
1379
1381}

References fb(), PG_GETARG_INT16, and PG_RETURN_INT16.

◆ int2le()

Datum int2le ( PG_FUNCTION_ARGS  )

Definition at line 506 of file int.c.

507{
510
512}

References fb(), PG_GETARG_INT16, and PG_RETURN_BOOL.

◆ int2lt()

Datum int2lt ( PG_FUNCTION_ARGS  )

Definition at line 497 of file int.c.

498{
501
503}

References fb(), PG_GETARG_INT16, and PG_RETURN_BOOL.

◆ int2mi()

Datum int2mi ( PG_FUNCTION_ARGS  )

Definition at line 949 of file int.c.

950{
953 int16 result;
954
955 if (unlikely(pg_sub_s16_overflow(arg1, arg2, &result)))
958 errmsg("smallint out of range")));
959 PG_RETURN_INT16(result);
960}
static bool pg_sub_s16_overflow(int16 a, int16 b, int16 *result)
Definition int.h:85

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

◆ int2mod()

Datum int2mod ( PG_FUNCTION_ARGS  )

Definition at line 1187 of file int.c.

1188{
1191
1192 if (unlikely(arg2 == 0))
1193 {
1194 ereport(ERROR,
1196 errmsg("division by zero")));
1197 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1199 }
1200
1201 /*
1202 * Some machines throw a floating-point exception for INT_MIN % -1, which
1203 * is a bit silly since the correct answer is perfectly well-defined,
1204 * namely zero. (It's not clear this ever happens when dealing with
1205 * int16, but we might as well have the test for safety.)
1206 */
1207 if (arg2 == -1)
1208 PG_RETURN_INT16(0);
1209
1210 /* No overflow is possible */
1211
1213}

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

◆ int2mul()

Datum int2mul ( PG_FUNCTION_ARGS  )

Definition at line 963 of file int.c.

964{
967 int16 result;
968
969 if (unlikely(pg_mul_s16_overflow(arg1, arg2, &result)))
972 errmsg("smallint out of range")));
973
974 PG_RETURN_INT16(result);
975}
static bool pg_mul_s16_overflow(int16 a, int16 b, int16 *result)
Definition int.h:103

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

◆ int2ne()

Datum int2ne ( PG_FUNCTION_ARGS  )

Definition at line 488 of file int.c.

489{
492
494}

References fb(), PG_GETARG_INT16, and PG_RETURN_BOOL.

◆ int2not()

Datum int2not ( PG_FUNCTION_ARGS  )

Definition at line 1502 of file int.c.

1503{
1505
1507}

References fb(), PG_GETARG_INT16, and PG_RETURN_INT16.

◆ int2or()

Datum int2or ( PG_FUNCTION_ARGS  )

Definition at line 1484 of file int.c.

1485{
1488
1490}

References fb(), PG_GETARG_INT16, and PG_RETURN_INT16.

◆ int2out()

Datum int2out ( PG_FUNCTION_ARGS  )

Definition at line 74 of file int.c.

75{
77 char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */
78
79 pg_itoa(arg1, result);
80 PG_RETURN_CSTRING(result);
81}
#define PG_RETURN_CSTRING(x)
Definition fmgr.h:364
void * palloc(Size size)
Definition mcxt.c:1387
int pg_itoa(int16 i, char *a)
Definition numutils.c:1041

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

◆ int2pl()

Datum int2pl ( PG_FUNCTION_ARGS  )

Definition at line 935 of file int.c.

936{
939 int16 result;
940
941 if (unlikely(pg_add_s16_overflow(arg1, arg2, &result)))
944 errmsg("smallint out of range")));
945 PG_RETURN_INT16(result);
946}
static bool pg_add_s16_overflow(int16 a, int16 b, int16 *result)
Definition int.h:67

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

◆ int2recv()

Datum int2recv ( PG_FUNCTION_ARGS  )

Definition at line 87 of file int.c.

88{
90
92}
static char buf[DEFAULT_XLOG_SEG_SIZE]
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition pqformat.c:414
struct StringInfoData * StringInfo
Definition string.h:15

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

◆ int2send()

Datum int2send ( PG_FUNCTION_ARGS  )

Definition at line 98 of file int.c.

99{
102
106}
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373
void pq_begintypsend(StringInfo buf)
Definition pqformat.c:325
bytea * pq_endtypsend(StringInfo buf)
Definition pqformat.c:345
static void pq_sendint16(StringInfo buf, uint16 i)
Definition pqformat.h:136

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

Referenced by int2_bytea().

◆ int2shl()

Datum int2shl ( PG_FUNCTION_ARGS  )

Definition at line 1511 of file int.c.

1512{
1515
1517}

References fb(), PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_INT16.

◆ int2shr()

Datum int2shr ( PG_FUNCTION_ARGS  )

Definition at line 1520 of file int.c.

1521{
1524
1526}

References fb(), PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_INT16.

◆ int2smaller()

Datum int2smaller ( PG_FUNCTION_ARGS  )

Definition at line 1384 of file int.c.

1385{
1388
1390}

References fb(), PG_GETARG_INT16, and PG_RETURN_INT16.

◆ int2um()

Datum int2um ( PG_FUNCTION_ARGS  )

Definition at line 915 of file int.c.

916{
918
919 if (unlikely(arg == PG_INT16_MIN))
922 errmsg("smallint out of range")));
924}
void * arg

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

◆ int2up()

Datum int2up ( PG_FUNCTION_ARGS  )

Definition at line 927 of file int.c.

928{
930
932}

References arg, PG_GETARG_INT16, and PG_RETURN_INT16.

◆ int2vectorin()

Datum int2vectorin ( PG_FUNCTION_ARGS  )

Definition at line 165 of file int.c.

166{
167 char *intString = PG_GETARG_CSTRING(0);
168 Node *escontext = fcinfo->context;
169 int2vector *result;
170 int nalloc;
171 int n;
172
173 nalloc = 32; /* arbitrary initial size guess */
175
176 for (n = 0;; n++)
177 {
178 long l;
179 char *endp;
180
181 while (*intString && isspace((unsigned char) *intString))
182 intString++;
183 if (*intString == '\0')
184 break;
185
186 if (n >= nalloc)
187 {
188 nalloc *= 2;
189 result = (int2vector *) repalloc(result, Int2VectorSize(nalloc));
190 }
191
192 errno = 0;
193 l = strtol(intString, &endp, 10);
194
195 if (intString == endp)
196 ereturn(escontext, (Datum) 0,
198 errmsg("invalid input syntax for type %s: \"%s\"",
199 "smallint", intString)));
200
202 ereturn(escontext, (Datum) 0,
204 errmsg("value \"%s\" is out of range for type %s", intString,
205 "smallint")));
206
207 if (*endp && *endp != ' ')
208 ereturn(escontext, (Datum) 0,
210 errmsg("invalid input syntax for type %s: \"%s\"",
211 "smallint", intString)));
212
213 result->values[n] = l;
214 intString = endp;
215 }
216
217 SET_VARSIZE(result, Int2VectorSize(n));
218 result->ndim = 1;
219 result->dataoffset = 0; /* never any nulls */
220 result->elemtype = INT2OID;
221 result->dim1 = n;
222 result->lbound1 = 0;
223
224 PG_RETURN_POINTER(result);
225}
#define ereturn(context, dummy_value,...)
Definition elog.h:278
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1632
uint64_t Datum
Definition postgres.h:70

References int2vector::dataoffset, int2vector::dim1, int2vector::elemtype, ereturn, errcode(), errmsg(), fb(), Int2VectorSize, int2vector::lbound1, int2vector::ndim, palloc0(), PG_GETARG_CSTRING, PG_RETURN_POINTER, repalloc(), SET_VARSIZE(), and int2vector::values.

◆ int2vectorout()

Datum int2vectorout ( PG_FUNCTION_ARGS  )

Definition at line 231 of file int.c.

232{
234 int num,
235 nnums;
236 char *rp;
237 char *result;
238
239 /* validate input before fetching dim1 */
241 nnums = int2Array->dim1;
242
243 /* assumes sign, 5 digits, ' ' */
244 rp = result = (char *) palloc(nnums * 7 + 1);
245 for (num = 0; num < nnums; num++)
246 {
247 if (num != 0)
248 *rp++ = ' ';
249 rp += pg_itoa(int2Array->values[num], rp);
250 }
251 *rp = '\0';
252 PG_RETURN_CSTRING(result);
253}
static void check_valid_int2vector(const int2vector *int2Array)
Definition int.c:145

References check_valid_int2vector(), fb(), palloc(), PG_GETARG_POINTER, pg_itoa(), and PG_RETURN_CSTRING.

◆ int2vectorrecv()

Datum int2vectorrecv ( PG_FUNCTION_ARGS  )

Definition at line 259 of file int.c.

260{
263 int2vector *result;
264
265 /*
266 * Normally one would call array_recv() using DirectFunctionCall3, but
267 * that does not work since array_recv wants to cache some data using
268 * fcinfo->flinfo->fn_extra. So we need to pass it our own flinfo
269 * parameter.
270 */
271 InitFunctionCallInfoData(*locfcinfo, fcinfo->flinfo, 3,
273
274 locfcinfo->args[0].value = PointerGetDatum(buf);
275 locfcinfo->args[0].isnull = false;
276 locfcinfo->args[1].value = ObjectIdGetDatum(INT2OID);
277 locfcinfo->args[1].isnull = false;
278 locfcinfo->args[2].value = Int32GetDatum(-1);
279 locfcinfo->args[2].isnull = false;
280
282
283 Assert(!locfcinfo->isnull);
284
285 /* sanity checks: int2vector must be 1-D, 0-based, no nulls */
286 if (ARR_NDIM(result) != 1 ||
287 ARR_HASNULL(result) ||
288 ARR_ELEMTYPE(result) != INT2OID ||
289 ARR_LBOUND(result)[0] != 0)
292 errmsg("invalid int2vector data")));
293
294 PG_RETURN_POINTER(result);
295}
#define ARR_NDIM(a)
Definition array.h:290
#define ARR_ELEMTYPE(a)
Definition array.h:292
#define ARR_HASNULL(a)
Definition array.h:291
#define ARR_LBOUND(a)
Definition array.h:296
Datum array_recv(PG_FUNCTION_ARGS)
#define Assert(condition)
Definition c.h:873
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition fmgr.h:150
#define LOCAL_FCINFO(name, nargs)
Definition fmgr.h:110
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
#define InvalidOid

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

◆ int2vectorsend()

Datum int2vectorsend ( PG_FUNCTION_ARGS  )

Definition at line 301 of file int.c.

302{
303 /* We don't do check_valid_int2vector, since array_send won't care */
304 return array_send(fcinfo);
305}
Datum array_send(PG_FUNCTION_ARGS)

References array_send().

◆ int2xor()

Datum int2xor ( PG_FUNCTION_ARGS  )

Definition at line 1493 of file int.c.

1494{
1497
1499}

References fb(), PG_GETARG_INT16, and PG_RETURN_INT16.

◆ int42div()

Datum int42div ( PG_FUNCTION_ARGS  )

Definition at line 1120 of file int.c.

1121{
1124 int32 result;
1125
1126 if (unlikely(arg2 == 0))
1127 {
1128 ereport(ERROR,
1130 errmsg("division by zero")));
1131 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1133 }
1134
1135 /*
1136 * INT_MIN / -1 is problematic, since the result can't be represented on a
1137 * two's-complement machine. Some machines produce INT_MIN, some produce
1138 * zero, some throw an exception. We can dodge the problem by recognizing
1139 * that division by -1 is the same as negation.
1140 */
1141 if (arg2 == -1)
1142 {
1143 if (unlikely(arg1 == PG_INT32_MIN))
1144 ereport(ERROR,
1146 errmsg("integer out of range")));
1147 result = -arg1;
1148 PG_RETURN_INT32(result);
1149 }
1150
1151 /* No overflow is possible */
1152
1153 result = arg1 / arg2;
1154
1155 PG_RETURN_INT32(result);
1156}
#define PG_INT32_MIN
Definition c.h:602

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

◆ int42eq()

Datum int42eq ( PG_FUNCTION_ARGS  )

Definition at line 587 of file int.c.

588{
591
593}

References fb(), PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int42ge()

Datum int42ge ( PG_FUNCTION_ARGS  )

Definition at line 632 of file int.c.

633{
636
638}

References fb(), PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int42gt()

Datum int42gt ( PG_FUNCTION_ARGS  )

Definition at line 623 of file int.c.

624{
627
629}

References fb(), PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int42le()

Datum int42le ( PG_FUNCTION_ARGS  )

Definition at line 614 of file int.c.

615{
618
620}

References fb(), PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int42lt()

Datum int42lt ( PG_FUNCTION_ARGS  )

Definition at line 605 of file int.c.

606{
609
611}

References fb(), PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int42mi()

Datum int42mi ( PG_FUNCTION_ARGS  )

Definition at line 1092 of file int.c.

1093{
1096 int32 result;
1097
1098 if (unlikely(pg_sub_s32_overflow(arg1, (int32) arg2, &result)))
1099 ereport(ERROR,
1101 errmsg("integer out of range")));
1102 PG_RETURN_INT32(result);
1103}

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

◆ int42mul()

Datum int42mul ( PG_FUNCTION_ARGS  )

Definition at line 1106 of file int.c.

1107{
1110 int32 result;
1111
1112 if (unlikely(pg_mul_s32_overflow(arg1, (int32) arg2, &result)))
1113 ereport(ERROR,
1115 errmsg("integer out of range")));
1116 PG_RETURN_INT32(result);
1117}

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

◆ int42ne()

Datum int42ne ( PG_FUNCTION_ARGS  )

Definition at line 596 of file int.c.

597{
600
602}

References fb(), PG_GETARG_INT16, PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int42pl()

Datum int42pl ( PG_FUNCTION_ARGS  )

Definition at line 1078 of file int.c.

1079{
1082 int32 result;
1083
1084 if (unlikely(pg_add_s32_overflow(arg1, (int32) arg2, &result)))
1085 ereport(ERROR,
1087 errmsg("integer out of range")));
1088 PG_RETURN_INT32(result);
1089}

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

◆ int4_bool()

Datum int4_bool ( PG_FUNCTION_ARGS  )

Definition at line 391 of file int.c.

392{
393 if (PG_GETARG_INT32(0) == 0)
394 PG_RETURN_BOOL(false);
395 else
396 PG_RETURN_BOOL(true);
397}

References PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int4abs()

Datum int4abs ( PG_FUNCTION_ARGS  )

Definition at line 1220 of file int.c.

1221{
1223 int32 result;
1224
1225 if (unlikely(arg1 == PG_INT32_MIN))
1226 ereport(ERROR,
1228 errmsg("integer out of range")));
1229 result = (arg1 < 0) ? -arg1 : arg1;
1230 PG_RETURN_INT32(result);
1231}

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

◆ int4and()

Datum int4and ( PG_FUNCTION_ARGS  )

Definition at line 1422 of file int.c.

1423{
1426
1428}

References fb(), PG_GETARG_INT32, and PG_RETURN_INT32.

◆ int4div()

Datum int4div ( PG_FUNCTION_ARGS  )

Definition at line 862 of file int.c.

863{
866 int32 result;
867
868 if (arg2 == 0)
869 {
872 errmsg("division by zero")));
873 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
875 }
876
877 /*
878 * INT_MIN / -1 is problematic, since the result can't be represented on a
879 * two's-complement machine. Some machines produce INT_MIN, some produce
880 * zero, some throw an exception. We can dodge the problem by recognizing
881 * that division by -1 is the same as negation.
882 */
883 if (arg2 == -1)
884 {
885 if (unlikely(arg1 == PG_INT32_MIN))
888 errmsg("integer out of range")));
889 result = -arg1;
890 PG_RETURN_INT32(result);
891 }
892
893 /* No overflow is possible */
894
895 result = arg1 / arg2;
896
897 PG_RETURN_INT32(result);
898}

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

◆ int4eq()

Datum int4eq ( PG_FUNCTION_ARGS  )

Definition at line 425 of file int.c.

426{
429
431}

References fb(), PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int4gcd()

Datum int4gcd ( PG_FUNCTION_ARGS  )

Definition at line 1323 of file int.c.

1324{
1327 int32 result;
1328
1329 result = int4gcd_internal(arg1, arg2);
1330
1331 PG_RETURN_INT32(result);
1332}
static int32 int4gcd_internal(int32 arg1, int32 arg2)
Definition int.c:1262

References fb(), int4gcd_internal(), PG_GETARG_INT32, and PG_RETURN_INT32.

◆ int4gcd_internal()

static int32 int4gcd_internal ( int32  arg1,
int32  arg2 
)
static

Definition at line 1262 of file int.c.

1263{
1264 int32 swap;
1265 int32 a1,
1266 a2;
1267
1268 /*
1269 * Put the greater absolute value in arg1.
1270 *
1271 * This would happen automatically in the loop below, but avoids an
1272 * expensive modulo operation, and simplifies the special-case handling
1273 * for INT_MIN below.
1274 *
1275 * We do this in negative space in order to handle INT_MIN.
1276 */
1277 a1 = (arg1 < 0) ? arg1 : -arg1;
1278 a2 = (arg2 < 0) ? arg2 : -arg2;
1279 if (a1 > a2)
1280 {
1281 swap = arg1;
1282 arg1 = arg2;
1283 arg2 = swap;
1284 }
1285
1286 /* Special care needs to be taken with INT_MIN. See comments above. */
1287 if (arg1 == PG_INT32_MIN)
1288 {
1289 if (arg2 == 0 || arg2 == PG_INT32_MIN)
1290 ereport(ERROR,
1292 errmsg("integer out of range")));
1293
1294 /*
1295 * Some machines throw a floating-point exception for INT_MIN % -1,
1296 * which is a bit silly since the correct answer is perfectly
1297 * well-defined, namely zero. Guard against this and just return the
1298 * result, gcd(INT_MIN, -1) = 1.
1299 */
1300 if (arg2 == -1)
1301 return 1;
1302 }
1303
1304 /* Use the Euclidean algorithm to find the GCD */
1305 while (arg2 != 0)
1306 {
1307 swap = arg2;
1308 arg2 = arg1 % arg2;
1309 arg1 = swap;
1310 }
1311
1312 /*
1313 * Make sure the result is positive. (We know we don't have INT_MIN
1314 * anymore).
1315 */
1316 if (arg1 < 0)
1317 arg1 = -arg1;
1318
1319 return arg1;
1320}
static const FormData_pg_attribute a1
Definition heap.c:144
static const FormData_pg_attribute a2
Definition heap.c:157

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

Referenced by int4gcd(), and int4lcm().

◆ int4ge()

Datum int4ge ( PG_FUNCTION_ARGS  )

Definition at line 470 of file int.c.

471{
474
476}

References fb(), PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int4gt()

Datum int4gt ( PG_FUNCTION_ARGS  )

Definition at line 461 of file int.c.

462{
465
467}

References fb(), PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int4in()

Datum int4in ( PG_FUNCTION_ARGS  )

Definition at line 316 of file int.c.

317{
318 char *num = PG_GETARG_CSTRING(0);
319
320 PG_RETURN_INT32(pg_strtoint32_safe(num, fcinfo->context));
321}
int32 pg_strtoint32_safe(const char *s, Node *escontext)
Definition numutils.c:388

References PG_GETARG_CSTRING, PG_RETURN_INT32, and pg_strtoint32_safe().

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

◆ int4inc()

Datum int4inc ( PG_FUNCTION_ARGS  )

Definition at line 901 of file int.c.

902{
904 int32 result;
905
906 if (unlikely(pg_add_s32_overflow(arg, 1, &result)))
909 errmsg("integer out of range")));
910
911 PG_RETURN_INT32(result);
912}

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

◆ int4larger()

Datum int4larger ( PG_FUNCTION_ARGS  )

Definition at line 1393 of file int.c.

1394{
1397
1399}

References fb(), PG_GETARG_INT32, and PG_RETURN_INT32.

◆ int4lcm()

Datum int4lcm ( PG_FUNCTION_ARGS  )

Definition at line 1338 of file int.c.

1339{
1342 int32 gcd;
1343 int32 result;
1344
1345 /*
1346 * Handle lcm(x, 0) = lcm(0, x) = 0 as a special case. This prevents a
1347 * division-by-zero error below when x is zero, and an overflow error from
1348 * the GCD computation when x = INT_MIN.
1349 */
1350 if (arg1 == 0 || arg2 == 0)
1351 PG_RETURN_INT32(0);
1352
1353 /* lcm(x, y) = abs(x / gcd(x, y) * y) */
1355 arg1 = arg1 / gcd;
1356
1357 if (unlikely(pg_mul_s32_overflow(arg1, arg2, &result)))
1358 ereport(ERROR,
1360 errmsg("integer out of range")));
1361
1362 /* If the result is INT_MIN, it cannot be represented. */
1363 if (unlikely(result == PG_INT32_MIN))
1364 ereport(ERROR,
1366 errmsg("integer out of range")));
1367
1368 if (result < 0)
1369 result = -result;
1370
1371 PG_RETURN_INT32(result);
1372}
static uint32 gcd(uint32 a, uint32 b)

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

◆ int4le()

Datum int4le ( PG_FUNCTION_ARGS  )

Definition at line 452 of file int.c.

453{
456
458}

References fb(), PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int4lt()

Datum int4lt ( PG_FUNCTION_ARGS  )

Definition at line 443 of file int.c.

444{
447
449}

References fb(), PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int4mi()

Datum int4mi ( PG_FUNCTION_ARGS  )

Definition at line 834 of file int.c.

835{
838 int32 result;
839
840 if (unlikely(pg_sub_s32_overflow(arg1, arg2, &result)))
843 errmsg("integer out of range")));
844 PG_RETURN_INT32(result);
845}

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

◆ int4mod()

Datum int4mod ( PG_FUNCTION_ARGS  )

Definition at line 1159 of file int.c.

1160{
1163
1164 if (unlikely(arg2 == 0))
1165 {
1166 ereport(ERROR,
1168 errmsg("division by zero")));
1169 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1171 }
1172
1173 /*
1174 * Some machines throw a floating-point exception for INT_MIN % -1, which
1175 * is a bit silly since the correct answer is perfectly well-defined,
1176 * namely zero.
1177 */
1178 if (arg2 == -1)
1179 PG_RETURN_INT32(0);
1180
1181 /* No overflow is possible */
1182
1184}

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

◆ int4mul()

Datum int4mul ( PG_FUNCTION_ARGS  )

Definition at line 848 of file int.c.

849{
852 int32 result;
853
854 if (unlikely(pg_mul_s32_overflow(arg1, arg2, &result)))
857 errmsg("integer out of range")));
858 PG_RETURN_INT32(result);
859}

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

◆ int4ne()

Datum int4ne ( PG_FUNCTION_ARGS  )

Definition at line 434 of file int.c.

435{
438
440}

References fb(), PG_GETARG_INT32, and PG_RETURN_BOOL.

◆ int4not()

Datum int4not ( PG_FUNCTION_ARGS  )

Definition at line 1467 of file int.c.

1468{
1470
1472}

References fb(), PG_GETARG_INT32, and PG_RETURN_INT32.

◆ int4or()

Datum int4or ( PG_FUNCTION_ARGS  )

Definition at line 1431 of file int.c.

1432{
1435
1437}

References fb(), PG_GETARG_INT32, and PG_RETURN_INT32.

◆ int4out()

Datum int4out ( PG_FUNCTION_ARGS  )

Definition at line 327 of file int.c.

328{
330 char *result = (char *) palloc(12); /* sign, 10 digits, '\0' */
331
332 pg_ltoa(arg1, result);
333 PG_RETURN_CSTRING(result);
334}
int pg_ltoa(int32 value, char *a)
Definition numutils.c:1119

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

Referenced by int4_to_char(), and jsonb_subscript_check_subscripts().

◆ int4pl()

Datum int4pl ( PG_FUNCTION_ARGS  )

Definition at line 820 of file int.c.

821{
824 int32 result;
825
826 if (unlikely(pg_add_s32_overflow(arg1, arg2, &result)))
829 errmsg("integer out of range")));
830 PG_RETURN_INT32(result);
831}

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

◆ int4recv()

Datum int4recv ( PG_FUNCTION_ARGS  )

Definition at line 340 of file int.c.

341{
343
345}

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

◆ int4send()

Datum int4send ( PG_FUNCTION_ARGS  )

Definition at line 351 of file int.c.

352{
355
359}
static void pq_sendint32(StringInfo buf, uint32 i)
Definition pqformat.h:144

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

Referenced by int4_bytea().

◆ int4shl()

Datum int4shl ( PG_FUNCTION_ARGS  )

Definition at line 1449 of file int.c.

1450{
1453
1455}

References fb(), PG_GETARG_INT32, and PG_RETURN_INT32.

◆ int4shr()

Datum int4shr ( PG_FUNCTION_ARGS  )

Definition at line 1458 of file int.c.

1459{
1462
1464}

References fb(), PG_GETARG_INT32, and PG_RETURN_INT32.

◆ int4smaller()

Datum int4smaller ( PG_FUNCTION_ARGS  )

Definition at line 1402 of file int.c.

1403{
1406
1408}

References fb(), PG_GETARG_INT32, and PG_RETURN_INT32.

◆ int4um()

Datum int4um ( PG_FUNCTION_ARGS  )

Definition at line 800 of file int.c.

801{
803
804 if (unlikely(arg == PG_INT32_MIN))
807 errmsg("integer out of range")));
809}

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

◆ int4up()

Datum int4up ( PG_FUNCTION_ARGS  )

Definition at line 812 of file int.c.

813{
815
817}

References arg, PG_GETARG_INT32, and PG_RETURN_INT32.

◆ int4xor()

Datum int4xor ( PG_FUNCTION_ARGS  )

Definition at line 1440 of file int.c.

1441{
1444
1446}

References fb(), PG_GETARG_INT32, and PG_RETURN_INT32.