PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
int8.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include "funcapi.h"
#include "libpq/pqformat.h"
#include "utils/int8.h"
#include "utils/builtins.h"
Include dependency graph for int8.c:

Go to the source code of this file.

Data Structures

struct  generate_series_fctx
 

Macros

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

Functions

bool scanint8 (const char *str, bool errorOK, int64 *result)
 
Datum int8in (PG_FUNCTION_ARGS)
 
Datum int8out (PG_FUNCTION_ARGS)
 
Datum int8recv (PG_FUNCTION_ARGS)
 
Datum int8send (PG_FUNCTION_ARGS)
 
Datum int8eq (PG_FUNCTION_ARGS)
 
Datum int8ne (PG_FUNCTION_ARGS)
 
Datum int8lt (PG_FUNCTION_ARGS)
 
Datum int8gt (PG_FUNCTION_ARGS)
 
Datum int8le (PG_FUNCTION_ARGS)
 
Datum int8ge (PG_FUNCTION_ARGS)
 
Datum int84eq (PG_FUNCTION_ARGS)
 
Datum int84ne (PG_FUNCTION_ARGS)
 
Datum int84lt (PG_FUNCTION_ARGS)
 
Datum int84gt (PG_FUNCTION_ARGS)
 
Datum int84le (PG_FUNCTION_ARGS)
 
Datum int84ge (PG_FUNCTION_ARGS)
 
Datum int48eq (PG_FUNCTION_ARGS)
 
Datum int48ne (PG_FUNCTION_ARGS)
 
Datum int48lt (PG_FUNCTION_ARGS)
 
Datum int48gt (PG_FUNCTION_ARGS)
 
Datum int48le (PG_FUNCTION_ARGS)
 
Datum int48ge (PG_FUNCTION_ARGS)
 
Datum int82eq (PG_FUNCTION_ARGS)
 
Datum int82ne (PG_FUNCTION_ARGS)
 
Datum int82lt (PG_FUNCTION_ARGS)
 
Datum int82gt (PG_FUNCTION_ARGS)
 
Datum int82le (PG_FUNCTION_ARGS)
 
Datum int82ge (PG_FUNCTION_ARGS)
 
Datum int28eq (PG_FUNCTION_ARGS)
 
Datum int28ne (PG_FUNCTION_ARGS)
 
Datum int28lt (PG_FUNCTION_ARGS)
 
Datum int28gt (PG_FUNCTION_ARGS)
 
Datum int28le (PG_FUNCTION_ARGS)
 
Datum int28ge (PG_FUNCTION_ARGS)
 
Datum int8um (PG_FUNCTION_ARGS)
 
Datum int8up (PG_FUNCTION_ARGS)
 
Datum int8pl (PG_FUNCTION_ARGS)
 
Datum int8mi (PG_FUNCTION_ARGS)
 
Datum int8mul (PG_FUNCTION_ARGS)
 
Datum int8div (PG_FUNCTION_ARGS)
 
Datum int8abs (PG_FUNCTION_ARGS)
 
Datum int8mod (PG_FUNCTION_ARGS)
 
Datum int8inc (PG_FUNCTION_ARGS)
 
Datum int8dec (PG_FUNCTION_ARGS)
 
Datum int8inc_any (PG_FUNCTION_ARGS)
 
Datum int8inc_float8_float8 (PG_FUNCTION_ARGS)
 
Datum int8dec_any (PG_FUNCTION_ARGS)
 
Datum int8larger (PG_FUNCTION_ARGS)
 
Datum int8smaller (PG_FUNCTION_ARGS)
 
Datum int84pl (PG_FUNCTION_ARGS)
 
Datum int84mi (PG_FUNCTION_ARGS)
 
Datum int84mul (PG_FUNCTION_ARGS)
 
Datum int84div (PG_FUNCTION_ARGS)
 
Datum int48pl (PG_FUNCTION_ARGS)
 
Datum int48mi (PG_FUNCTION_ARGS)
 
Datum int48mul (PG_FUNCTION_ARGS)
 
Datum int48div (PG_FUNCTION_ARGS)
 
Datum int82pl (PG_FUNCTION_ARGS)
 
Datum int82mi (PG_FUNCTION_ARGS)
 
Datum int82mul (PG_FUNCTION_ARGS)
 
Datum int82div (PG_FUNCTION_ARGS)
 
Datum int28pl (PG_FUNCTION_ARGS)
 
Datum int28mi (PG_FUNCTION_ARGS)
 
Datum int28mul (PG_FUNCTION_ARGS)
 
Datum int28div (PG_FUNCTION_ARGS)
 
Datum int8and (PG_FUNCTION_ARGS)
 
Datum int8or (PG_FUNCTION_ARGS)
 
Datum int8xor (PG_FUNCTION_ARGS)
 
Datum int8not (PG_FUNCTION_ARGS)
 
Datum int8shl (PG_FUNCTION_ARGS)
 
Datum int8shr (PG_FUNCTION_ARGS)
 
Datum int48 (PG_FUNCTION_ARGS)
 
Datum int84 (PG_FUNCTION_ARGS)
 
Datum int28 (PG_FUNCTION_ARGS)
 
Datum int82 (PG_FUNCTION_ARGS)
 
Datum i8tod (PG_FUNCTION_ARGS)
 
Datum dtoi8 (PG_FUNCTION_ARGS)
 
Datum i8tof (PG_FUNCTION_ARGS)
 
Datum ftoi8 (PG_FUNCTION_ARGS)
 
Datum i8tooid (PG_FUNCTION_ARGS)
 
Datum oidtoi8 (PG_FUNCTION_ARGS)
 
Datum generate_series_int8 (PG_FUNCTION_ARGS)
 
Datum generate_series_step_int8 (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

#define MAXINT8LEN   25

Definition at line 26 of file int8.c.

Referenced by int8out().

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

Function Documentation

Datum dtoi8 ( PG_FUNCTION_ARGS  )

Definition at line 1343 of file int8.c.

References arg, ereport, errcode(), errmsg(), ERROR, PG_GETARG_FLOAT8, PG_RETURN_INT64, result, and rint().

Referenced by int8_to_char().

1344 {
1346  int64 result;
1347 
1348  /* Round arg to nearest integer (but it's still in float form) */
1349  arg = rint(arg);
1350 
1351  /*
1352  * Does it fit in an int64? Avoid assuming that we have handy constants
1353  * defined for the range boundaries, instead test for overflow by
1354  * reverse-conversion.
1355  */
1356  result = (int64) arg;
1357 
1358  if ((float8) result != arg)
1359  ereport(ERROR,
1360  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1361  errmsg("bigint out of range")));
1362 
1363  PG_RETURN_INT64(result);
1364 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:238
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:381
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * arg
Datum ftoi8 ( PG_FUNCTION_ARGS  )

Definition at line 1381 of file int8.c.

References arg, ereport, errcode(), errmsg(), ERROR, PG_GETARG_FLOAT4, PG_RETURN_INT64, result, and rint().

1382 {
1384  int64 result;
1385  float8 darg;
1386 
1387  /* Round arg to nearest integer (but it's still in float form) */
1388  darg = rint(arg);
1389 
1390  /*
1391  * Does it fit in an int64? Avoid assuming that we have handy constants
1392  * defined for the range boundaries, instead test for overflow by
1393  * reverse-conversion.
1394  */
1395  result = (int64) darg;
1396 
1397  if ((float8) result != darg)
1398  ereport(ERROR,
1399  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1400  errmsg("bigint out of range")));
1401 
1402  PG_RETURN_INT64(result);
1403 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:381
double rint(double x)
Definition: rint.c:22
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:237
#define ereport(elevel, rest)
Definition: elog.h:122
float float4
Definition: c.h:380
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * arg
Datum generate_series_int8 ( PG_FUNCTION_ARGS  )

Definition at line 1434 of file int8.c.

References generate_series_step_int8().

1435 {
1436  return generate_series_step_int8(fcinfo);
1437 }
Datum generate_series_step_int8(PG_FUNCTION_ARGS)
Definition: int8.c:1440
Datum generate_series_step_int8 ( PG_FUNCTION_ARGS  )

Definition at line 1440 of file int8.c.

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

Referenced by generate_series_int8().

1441 {
1442  FuncCallContext *funcctx;
1443  generate_series_fctx *fctx;
1444  int64 result;
1445  MemoryContext oldcontext;
1446 
1447  /* stuff done only on the first call of the function */
1448  if (SRF_IS_FIRSTCALL())
1449  {
1450  int64 start = PG_GETARG_INT64(0);
1451  int64 finish = PG_GETARG_INT64(1);
1452  int64 step = 1;
1453 
1454  /* see if we were given an explicit step size */
1455  if (PG_NARGS() == 3)
1456  step = PG_GETARG_INT64(2);
1457  if (step == 0)
1458  ereport(ERROR,
1459  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1460  errmsg("step size cannot equal zero")));
1461 
1462  /* create a function context for cross-call persistence */
1463  funcctx = SRF_FIRSTCALL_INIT();
1464 
1465  /*
1466  * switch to memory context appropriate for multiple function calls
1467  */
1468  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1469 
1470  /* allocate memory for user context */
1471  fctx = (generate_series_fctx *) palloc(sizeof(generate_series_fctx));
1472 
1473  /*
1474  * Use fctx to keep state from call to call. Seed current with the
1475  * original start value
1476  */
1477  fctx->current = start;
1478  fctx->finish = finish;
1479  fctx->step = step;
1480 
1481  funcctx->user_fctx = fctx;
1482  MemoryContextSwitchTo(oldcontext);
1483  }
1484 
1485  /* stuff done on every call of the function */
1486  funcctx = SRF_PERCALL_SETUP();
1487 
1488  /*
1489  * get the saved state and use current as the result for this iteration
1490  */
1491  fctx = funcctx->user_fctx;
1492  result = fctx->current;
1493 
1494  if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1495  (fctx->step < 0 && fctx->current >= fctx->finish))
1496  {
1497  /* increment current in preparation for next iteration */
1498  fctx->current += fctx->step;
1499 
1500  /* if next-value computation overflows, this is the final result */
1501  if (SAMESIGN(result, fctx->step) && !SAMESIGN(result, fctx->current))
1502  fctx->step = 0;
1503 
1504  /* do when there is more left to send */
1505  SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
1506  }
1507  else
1508  /* do when there is no more left */
1509  SRF_RETURN_DONE(funcctx);
1510 }
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
#define ERROR
Definition: elog.h:43
#define SAMESIGN(a, b)
Definition: int8.c:28
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:2152
#define ereport(elevel, rest)
Definition: elog.h:122
int32 current
Definition: int.c:47
int32 finish
Definition: int.c:48
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
#define PG_NARGS()
Definition: fmgr.h:160
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
Datum i8tod ( PG_FUNCTION_ARGS  )

Definition at line 1329 of file int8.c.

References arg, PG_GETARG_INT64, PG_RETURN_FLOAT8, and result.

1330 {
1331  int64 arg = PG_GETARG_INT64(0);
1332  float8 result;
1333 
1334  result = arg;
1335 
1336  PG_RETURN_FLOAT8(result);
1337 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:318
return result
Definition: formatting.c:1618
double float8
Definition: c.h:381
void * arg
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum i8tof ( PG_FUNCTION_ARGS  )

Definition at line 1367 of file int8.c.

References arg, PG_GETARG_INT64, PG_RETURN_FLOAT4, and result.

1368 {
1369  int64 arg = PG_GETARG_INT64(0);
1370  float4 result;
1371 
1372  result = arg;
1373 
1374  PG_RETURN_FLOAT4(result);
1375 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:317
return result
Definition: formatting.c:1618
float float4
Definition: c.h:380
void * arg
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum i8tooid ( PG_FUNCTION_ARGS  )

Definition at line 1406 of file int8.c.

References arg, ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT64, PG_RETURN_OID, and result.

1407 {
1408  int64 arg = PG_GETARG_INT64(0);
1409  Oid result;
1410 
1411  result = (Oid) arg;
1412 
1413  /* Test for overflow by reverse-conversion. */
1414  if ((int64) result != arg)
1415  ereport(ERROR,
1416  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1417  errmsg("OID out of range")));
1418 
1419  PG_RETURN_OID(result);
1420 }
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * arg
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
#define PG_RETURN_OID(x)
Definition: fmgr.h:312
Datum int28 ( PG_FUNCTION_ARGS  )

Definition at line 1304 of file int8.c.

References arg, PG_GETARG_INT16, and PG_RETURN_INT64.

1305 {
1306  int16 arg = PG_GETARG_INT16(0);
1307 
1308  PG_RETURN_INT64((int64) arg);
1309 }
signed short int16
Definition: c.h:255
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
void * arg
Datum int28div ( PG_FUNCTION_ARGS  )

Definition at line 1193 of file int8.c.

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

1194 {
1195  int16 arg1 = PG_GETARG_INT16(0);
1196  int64 arg2 = PG_GETARG_INT64(1);
1197 
1198  if (arg2 == 0)
1199  {
1200  ereport(ERROR,
1201  (errcode(ERRCODE_DIVISION_BY_ZERO),
1202  errmsg("division by zero")));
1203  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1204  PG_RETURN_NULL();
1205  }
1206 
1207  /* No overflow is possible */
1208  PG_RETURN_INT64((int64) arg1 / arg2);
1209 }
signed short int16
Definition: c.h:255
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
#define PG_RETURN_NULL()
Definition: fmgr.h:297
Datum int28eq ( PG_FUNCTION_ARGS  )

Definition at line 431 of file int8.c.

References PG_GETARG_INT16, PG_GETARG_INT64, and PG_RETURN_BOOL.

432 {
433  int16 val1 = PG_GETARG_INT16(0);
434  int64 val2 = PG_GETARG_INT64(1);
435 
436  PG_RETURN_BOOL(val1 == val2);
437 }
signed short int16
Definition: c.h:255
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int28ge ( PG_FUNCTION_ARGS  )

Definition at line 476 of file int8.c.

References PG_GETARG_INT16, PG_GETARG_INT64, and PG_RETURN_BOOL.

477 {
478  int16 val1 = PG_GETARG_INT16(0);
479  int64 val2 = PG_GETARG_INT64(1);
480 
481  PG_RETURN_BOOL(val1 >= val2);
482 }
signed short int16
Definition: c.h:255
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int28gt ( PG_FUNCTION_ARGS  )

Definition at line 458 of file int8.c.

References PG_GETARG_INT16, PG_GETARG_INT64, and PG_RETURN_BOOL.

459 {
460  int16 val1 = PG_GETARG_INT16(0);
461  int64 val2 = PG_GETARG_INT64(1);
462 
463  PG_RETURN_BOOL(val1 > val2);
464 }
signed short int16
Definition: c.h:255
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int28le ( PG_FUNCTION_ARGS  )

Definition at line 467 of file int8.c.

References PG_GETARG_INT16, PG_GETARG_INT64, and PG_RETURN_BOOL.

468 {
469  int16 val1 = PG_GETARG_INT16(0);
470  int64 val2 = PG_GETARG_INT64(1);
471 
472  PG_RETURN_BOOL(val1 <= val2);
473 }
signed short int16
Definition: c.h:255
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int28lt ( PG_FUNCTION_ARGS  )

Definition at line 449 of file int8.c.

References PG_GETARG_INT16, PG_GETARG_INT64, and PG_RETURN_BOOL.

450 {
451  int16 val1 = PG_GETARG_INT16(0);
452  int64 val2 = PG_GETARG_INT64(1);
453 
454  PG_RETURN_BOOL(val1 < val2);
455 }
signed short int16
Definition: c.h:255
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int28mi ( PG_FUNCTION_ARGS  )

Definition at line 1145 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_GETARG_INT64, PG_RETURN_INT64, result, and SAMESIGN.

1146 {
1147  int16 arg1 = PG_GETARG_INT16(0);
1148  int64 arg2 = PG_GETARG_INT64(1);
1149  int64 result;
1150 
1151  result = arg1 - arg2;
1152 
1153  /*
1154  * Overflow check. If the inputs are of the same sign then their
1155  * difference cannot overflow. If they are of different signs then the
1156  * result should be of the same sign as the first input.
1157  */
1158  if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1159  ereport(ERROR,
1160  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1161  errmsg("bigint out of range")));
1162  PG_RETURN_INT64(result);
1163 }
signed short int16
Definition: c.h:255
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
#define SAMESIGN(a, b)
Definition: int8.c:28
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int28mul ( PG_FUNCTION_ARGS  )

Definition at line 1166 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_GETARG_INT64, PG_RETURN_INT64, and result.

1167 {
1168  int16 arg1 = PG_GETARG_INT16(0);
1169  int64 arg2 = PG_GETARG_INT64(1);
1170  int64 result;
1171 
1172  result = arg1 * arg2;
1173 
1174  /*
1175  * Overflow check. We basically check to see if result / arg2 gives arg1
1176  * again. There is one case where this fails: arg2 = 0 (which cannot
1177  * overflow).
1178  *
1179  * Since the division is likely much more expensive than the actual
1180  * multiplication, we'd like to skip it where possible. The best bang for
1181  * the buck seems to be to check whether both inputs are in the int32
1182  * range; if so, no overflow is possible.
1183  */
1184  if (arg2 != (int64) ((int32) arg2) &&
1185  result / arg2 != arg1)
1186  ereport(ERROR,
1187  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1188  errmsg("bigint out of range")));
1189  PG_RETURN_INT64(result);
1190 }
signed short int16
Definition: c.h:255
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int28ne ( PG_FUNCTION_ARGS  )

Definition at line 440 of file int8.c.

References PG_GETARG_INT16, PG_GETARG_INT64, and PG_RETURN_BOOL.

441 {
442  int16 val1 = PG_GETARG_INT16(0);
443  int64 val2 = PG_GETARG_INT64(1);
444 
445  PG_RETURN_BOOL(val1 != val2);
446 }
signed short int16
Definition: c.h:255
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int28pl ( PG_FUNCTION_ARGS  )

Definition at line 1124 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_GETARG_INT64, PG_RETURN_INT64, result, and SAMESIGN.

1125 {
1126  int16 arg1 = PG_GETARG_INT16(0);
1127  int64 arg2 = PG_GETARG_INT64(1);
1128  int64 result;
1129 
1130  result = arg1 + arg2;
1131 
1132  /*
1133  * Overflow check. If the inputs are of different signs then their sum
1134  * cannot overflow. If the inputs are of the same sign, their sum had
1135  * better be that sign too.
1136  */
1137  if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1138  ereport(ERROR,
1139  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1140  errmsg("bigint out of range")));
1141  PG_RETURN_INT64(result);
1142 }
signed short int16
Definition: c.h:255
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
#define SAMESIGN(a, b)
Definition: int8.c:28
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int48 ( PG_FUNCTION_ARGS  )

Definition at line 1279 of file int8.c.

References arg, PG_GETARG_INT32, and PG_RETURN_INT64.

1280 {
1281  int32 arg = PG_GETARG_INT32(0);
1282 
1283  PG_RETURN_INT64((int64) arg);
1284 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
signed int int32
Definition: c.h:256
void * arg
Datum int48div ( PG_FUNCTION_ARGS  )

Definition at line 996 of file int8.c.

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

997 {
998  int32 arg1 = PG_GETARG_INT32(0);
999  int64 arg2 = PG_GETARG_INT64(1);
1000 
1001  if (arg2 == 0)
1002  {
1003  ereport(ERROR,
1004  (errcode(ERRCODE_DIVISION_BY_ZERO),
1005  errmsg("division by zero")));
1006  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1007  PG_RETURN_NULL();
1008  }
1009 
1010  /* No overflow is possible */
1011  PG_RETURN_INT64((int64) arg1 / arg2);
1012 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
#define PG_RETURN_NULL()
Definition: fmgr.h:297
Datum int48eq ( PG_FUNCTION_ARGS  )

Definition at line 317 of file int8.c.

References PG_GETARG_INT32, PG_GETARG_INT64, and PG_RETURN_BOOL.

318 {
319  int32 val1 = PG_GETARG_INT32(0);
320  int64 val2 = PG_GETARG_INT64(1);
321 
322  PG_RETURN_BOOL(val1 == val2);
323 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
signed int int32
Definition: c.h:256
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int48ge ( PG_FUNCTION_ARGS  )

Definition at line 362 of file int8.c.

References PG_GETARG_INT32, PG_GETARG_INT64, and PG_RETURN_BOOL.

363 {
364  int32 val1 = PG_GETARG_INT32(0);
365  int64 val2 = PG_GETARG_INT64(1);
366 
367  PG_RETURN_BOOL(val1 >= val2);
368 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
signed int int32
Definition: c.h:256
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int48gt ( PG_FUNCTION_ARGS  )

Definition at line 344 of file int8.c.

References PG_GETARG_INT32, PG_GETARG_INT64, and PG_RETURN_BOOL.

345 {
346  int32 val1 = PG_GETARG_INT32(0);
347  int64 val2 = PG_GETARG_INT64(1);
348 
349  PG_RETURN_BOOL(val1 > val2);
350 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
signed int int32
Definition: c.h:256
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int48le ( PG_FUNCTION_ARGS  )

Definition at line 353 of file int8.c.

References PG_GETARG_INT32, PG_GETARG_INT64, and PG_RETURN_BOOL.

354 {
355  int32 val1 = PG_GETARG_INT32(0);
356  int64 val2 = PG_GETARG_INT64(1);
357 
358  PG_RETURN_BOOL(val1 <= val2);
359 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
signed int int32
Definition: c.h:256
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int48lt ( PG_FUNCTION_ARGS  )

Definition at line 335 of file int8.c.

References PG_GETARG_INT32, PG_GETARG_INT64, and PG_RETURN_BOOL.

336 {
337  int32 val1 = PG_GETARG_INT32(0);
338  int64 val2 = PG_GETARG_INT64(1);
339 
340  PG_RETURN_BOOL(val1 < val2);
341 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
signed int int32
Definition: c.h:256
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int48mi ( PG_FUNCTION_ARGS  )

Definition at line 948 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, PG_GETARG_INT64, PG_RETURN_INT64, result, and SAMESIGN.

949 {
950  int32 arg1 = PG_GETARG_INT32(0);
951  int64 arg2 = PG_GETARG_INT64(1);
952  int64 result;
953 
954  result = arg1 - arg2;
955 
956  /*
957  * Overflow check. If the inputs are of the same sign then their
958  * difference cannot overflow. If they are of different signs then the
959  * result should be of the same sign as the first input.
960  */
961  if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
962  ereport(ERROR,
963  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
964  errmsg("bigint out of range")));
965  PG_RETURN_INT64(result);
966 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define SAMESIGN(a, b)
Definition: int8.c:28
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int48mul ( PG_FUNCTION_ARGS  )

Definition at line 969 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, PG_GETARG_INT64, PG_RETURN_INT64, and result.

970 {
971  int32 arg1 = PG_GETARG_INT32(0);
972  int64 arg2 = PG_GETARG_INT64(1);
973  int64 result;
974 
975  result = arg1 * arg2;
976 
977  /*
978  * Overflow check. We basically check to see if result / arg2 gives arg1
979  * again. There is one case where this fails: arg2 = 0 (which cannot
980  * overflow).
981  *
982  * Since the division is likely much more expensive than the actual
983  * multiplication, we'd like to skip it where possible. The best bang for
984  * the buck seems to be to check whether both inputs are in the int32
985  * range; if so, no overflow is possible.
986  */
987  if (arg2 != (int64) ((int32) arg2) &&
988  result / arg2 != arg1)
989  ereport(ERROR,
990  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
991  errmsg("bigint out of range")));
992  PG_RETURN_INT64(result);
993 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int48ne ( PG_FUNCTION_ARGS  )

Definition at line 326 of file int8.c.

References PG_GETARG_INT32, PG_GETARG_INT64, and PG_RETURN_BOOL.

327 {
328  int32 val1 = PG_GETARG_INT32(0);
329  int64 val2 = PG_GETARG_INT64(1);
330 
331  PG_RETURN_BOOL(val1 != val2);
332 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
signed int int32
Definition: c.h:256
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int48pl ( PG_FUNCTION_ARGS  )

Definition at line 927 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, PG_GETARG_INT64, PG_RETURN_INT64, result, and SAMESIGN.

928 {
929  int32 arg1 = PG_GETARG_INT32(0);
930  int64 arg2 = PG_GETARG_INT64(1);
931  int64 result;
932 
933  result = arg1 + arg2;
934 
935  /*
936  * Overflow check. If the inputs are of different signs then their sum
937  * cannot overflow. If the inputs are of the same sign, their sum had
938  * better be that sign too.
939  */
940  if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
941  ereport(ERROR,
942  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
943  errmsg("bigint out of range")));
944  PG_RETURN_INT64(result);
945 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define SAMESIGN(a, b)
Definition: int8.c:28
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int82 ( PG_FUNCTION_ARGS  )

Definition at line 1312 of file int8.c.

References arg, ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT64, PG_RETURN_INT16, and result.

1313 {
1314  int64 arg = PG_GETARG_INT64(0);
1315  int16 result;
1316 
1317  result = (int16) arg;
1318 
1319  /* Test for overflow by reverse-conversion. */
1320  if ((int64) result != arg)
1321  ereport(ERROR,
1322  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1323  errmsg("smallint out of range")));
1324 
1325  PG_RETURN_INT16(result);
1326 }
signed short int16
Definition: c.h:255
#define PG_RETURN_INT16(x)
Definition: fmgr.h:308
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * arg
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int82div ( PG_FUNCTION_ARGS  )

Definition at line 1084 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_GETARG_INT64, PG_RETURN_INT64, PG_RETURN_NULL, result, and SAMESIGN.

1085 {
1086  int64 arg1 = PG_GETARG_INT64(0);
1087  int16 arg2 = PG_GETARG_INT16(1);
1088  int64 result;
1089 
1090  if (arg2 == 0)
1091  {
1092  ereport(ERROR,
1093  (errcode(ERRCODE_DIVISION_BY_ZERO),
1094  errmsg("division by zero")));
1095  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1096  PG_RETURN_NULL();
1097  }
1098 
1099  /*
1100  * INT64_MIN / -1 is problematic, since the result can't be represented on
1101  * a two's-complement machine. Some machines produce INT64_MIN, some
1102  * produce zero, some throw an exception. We can dodge the problem by
1103  * recognizing that division by -1 is the same as negation.
1104  */
1105  if (arg2 == -1)
1106  {
1107  result = -arg1;
1108  /* overflow check (needed for INT64_MIN) */
1109  if (arg1 != 0 && SAMESIGN(result, arg1))
1110  ereport(ERROR,
1111  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1112  errmsg("bigint out of range")));
1113  PG_RETURN_INT64(result);
1114  }
1115 
1116  /* No overflow is possible */
1117 
1118  result = arg1 / arg2;
1119 
1120  PG_RETURN_INT64(result);
1121 }
signed short int16
Definition: c.h:255
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
#define SAMESIGN(a, b)
Definition: int8.c:28
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
#define PG_RETURN_NULL()
Definition: fmgr.h:297
Datum int82eq ( PG_FUNCTION_ARGS  )

Definition at line 374 of file int8.c.

References PG_GETARG_INT16, PG_GETARG_INT64, and PG_RETURN_BOOL.

375 {
376  int64 val1 = PG_GETARG_INT64(0);
377  int16 val2 = PG_GETARG_INT16(1);
378 
379  PG_RETURN_BOOL(val1 == val2);
380 }
signed short int16
Definition: c.h:255
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int82ge ( PG_FUNCTION_ARGS  )

Definition at line 419 of file int8.c.

References PG_GETARG_INT16, PG_GETARG_INT64, and PG_RETURN_BOOL.

420 {
421  int64 val1 = PG_GETARG_INT64(0);
422  int16 val2 = PG_GETARG_INT16(1);
423 
424  PG_RETURN_BOOL(val1 >= val2);
425 }
signed short int16
Definition: c.h:255
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int82gt ( PG_FUNCTION_ARGS  )

Definition at line 401 of file int8.c.

References PG_GETARG_INT16, PG_GETARG_INT64, and PG_RETURN_BOOL.

402 {
403  int64 val1 = PG_GETARG_INT64(0);
404  int16 val2 = PG_GETARG_INT16(1);
405 
406  PG_RETURN_BOOL(val1 > val2);
407 }
signed short int16
Definition: c.h:255
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int82le ( PG_FUNCTION_ARGS  )

Definition at line 410 of file int8.c.

References PG_GETARG_INT16, PG_GETARG_INT64, and PG_RETURN_BOOL.

411 {
412  int64 val1 = PG_GETARG_INT64(0);
413  int16 val2 = PG_GETARG_INT16(1);
414 
415  PG_RETURN_BOOL(val1 <= val2);
416 }
signed short int16
Definition: c.h:255
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int82lt ( PG_FUNCTION_ARGS  )

Definition at line 392 of file int8.c.

References PG_GETARG_INT16, PG_GETARG_INT64, and PG_RETURN_BOOL.

393 {
394  int64 val1 = PG_GETARG_INT64(0);
395  int16 val2 = PG_GETARG_INT16(1);
396 
397  PG_RETURN_BOOL(val1 < val2);
398 }
signed short int16
Definition: c.h:255
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int82mi ( PG_FUNCTION_ARGS  )

Definition at line 1036 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_GETARG_INT64, PG_RETURN_INT64, result, and SAMESIGN.

1037 {
1038  int64 arg1 = PG_GETARG_INT64(0);
1039  int16 arg2 = PG_GETARG_INT16(1);
1040  int64 result;
1041 
1042  result = arg1 - arg2;
1043 
1044  /*
1045  * Overflow check. If the inputs are of the same sign then their
1046  * difference cannot overflow. If they are of different signs then the
1047  * result should be of the same sign as the first input.
1048  */
1049  if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1050  ereport(ERROR,
1051  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1052  errmsg("bigint out of range")));
1053  PG_RETURN_INT64(result);
1054 }
signed short int16
Definition: c.h:255
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
#define SAMESIGN(a, b)
Definition: int8.c:28
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int82mul ( PG_FUNCTION_ARGS  )

Definition at line 1057 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_GETARG_INT64, PG_RETURN_INT64, and result.

1058 {
1059  int64 arg1 = PG_GETARG_INT64(0);
1060  int16 arg2 = PG_GETARG_INT16(1);
1061  int64 result;
1062 
1063  result = arg1 * arg2;
1064 
1065  /*
1066  * Overflow check. We basically check to see if result / arg1 gives arg2
1067  * again. There is one case where this fails: arg1 = 0 (which cannot
1068  * overflow).
1069  *
1070  * Since the division is likely much more expensive than the actual
1071  * multiplication, we'd like to skip it where possible. The best bang for
1072  * the buck seems to be to check whether both inputs are in the int32
1073  * range; if so, no overflow is possible.
1074  */
1075  if (arg1 != (int64) ((int32) arg1) &&
1076  result / arg1 != arg2)
1077  ereport(ERROR,
1078  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1079  errmsg("bigint out of range")));
1080  PG_RETURN_INT64(result);
1081 }
signed short int16
Definition: c.h:255
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int82ne ( PG_FUNCTION_ARGS  )

Definition at line 383 of file int8.c.

References PG_GETARG_INT16, PG_GETARG_INT64, and PG_RETURN_BOOL.

384 {
385  int64 val1 = PG_GETARG_INT64(0);
386  int16 val2 = PG_GETARG_INT16(1);
387 
388  PG_RETURN_BOOL(val1 != val2);
389 }
signed short int16
Definition: c.h:255
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int82pl ( PG_FUNCTION_ARGS  )

Definition at line 1015 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT16, PG_GETARG_INT64, PG_RETURN_INT64, result, and SAMESIGN.

1016 {
1017  int64 arg1 = PG_GETARG_INT64(0);
1018  int16 arg2 = PG_GETARG_INT16(1);
1019  int64 result;
1020 
1021  result = arg1 + arg2;
1022 
1023  /*
1024  * Overflow check. If the inputs are of different signs then their sum
1025  * cannot overflow. If the inputs are of the same sign, their sum had
1026  * better be that sign too.
1027  */
1028  if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1029  ereport(ERROR,
1030  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1031  errmsg("bigint out of range")));
1032  PG_RETURN_INT64(result);
1033 }
signed short int16
Definition: c.h:255
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
#define SAMESIGN(a, b)
Definition: int8.c:28
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:228
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int84 ( PG_FUNCTION_ARGS  )

Definition at line 1287 of file int8.c.

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

Referenced by int8_to_char().

1288 {
1289  int64 arg = PG_GETARG_INT64(0);
1290  int32 result;
1291 
1292  result = (int32) arg;
1293 
1294  /* Test for overflow by reverse-conversion. */
1295  if ((int64) result != arg)
1296  ereport(ERROR,
1297  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1298  errmsg("integer out of range")));
1299 
1300  PG_RETURN_INT32(result);
1301 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:306
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * arg
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int84div ( PG_FUNCTION_ARGS  )

Definition at line 887 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, PG_GETARG_INT64, PG_RETURN_INT64, PG_RETURN_NULL, result, and SAMESIGN.

888 {
889  int64 arg1 = PG_GETARG_INT64(0);
890  int32 arg2 = PG_GETARG_INT32(1);
891  int64 result;
892 
893  if (arg2 == 0)
894  {
895  ereport(ERROR,
896  (errcode(ERRCODE_DIVISION_BY_ZERO),
897  errmsg("division by zero")));
898  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
899  PG_RETURN_NULL();
900  }
901 
902  /*
903  * INT64_MIN / -1 is problematic, since the result can't be represented on
904  * a two's-complement machine. Some machines produce INT64_MIN, some
905  * produce zero, some throw an exception. We can dodge the problem by
906  * recognizing that division by -1 is the same as negation.
907  */
908  if (arg2 == -1)
909  {
910  result = -arg1;
911  /* overflow check (needed for INT64_MIN) */
912  if (arg1 != 0 && SAMESIGN(result, arg1))
913  ereport(ERROR,
914  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
915  errmsg("bigint out of range")));
916  PG_RETURN_INT64(result);
917  }
918 
919  /* No overflow is possible */
920 
921  result = arg1 / arg2;
922 
923  PG_RETURN_INT64(result);
924 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define SAMESIGN(a, b)
Definition: int8.c:28
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
#define PG_RETURN_NULL()
Definition: fmgr.h:297
Datum int84eq ( PG_FUNCTION_ARGS  )

Definition at line 260 of file int8.c.

References PG_GETARG_INT32, PG_GETARG_INT64, and PG_RETURN_BOOL.

261 {
262  int64 val1 = PG_GETARG_INT64(0);
263  int32 val2 = PG_GETARG_INT32(1);
264 
265  PG_RETURN_BOOL(val1 == val2);
266 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
signed int int32
Definition: c.h:256
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int84ge ( PG_FUNCTION_ARGS  )

Definition at line 305 of file int8.c.

References PG_GETARG_INT32, PG_GETARG_INT64, and PG_RETURN_BOOL.

306 {
307  int64 val1 = PG_GETARG_INT64(0);
308  int32 val2 = PG_GETARG_INT32(1);
309 
310  PG_RETURN_BOOL(val1 >= val2);
311 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
signed int int32
Definition: c.h:256
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int84gt ( PG_FUNCTION_ARGS  )

Definition at line 287 of file int8.c.

References PG_GETARG_INT32, PG_GETARG_INT64, and PG_RETURN_BOOL.

288 {
289  int64 val1 = PG_GETARG_INT64(0);
290  int32 val2 = PG_GETARG_INT32(1);
291 
292  PG_RETURN_BOOL(val1 > val2);
293 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
signed int int32
Definition: c.h:256
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int84le ( PG_FUNCTION_ARGS  )

Definition at line 296 of file int8.c.

References PG_GETARG_INT32, PG_GETARG_INT64, and PG_RETURN_BOOL.

297 {
298  int64 val1 = PG_GETARG_INT64(0);
299  int32 val2 = PG_GETARG_INT32(1);
300 
301  PG_RETURN_BOOL(val1 <= val2);
302 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
signed int int32
Definition: c.h:256
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int84lt ( PG_FUNCTION_ARGS  )

Definition at line 278 of file int8.c.

References PG_GETARG_INT32, PG_GETARG_INT64, and PG_RETURN_BOOL.

279 {
280  int64 val1 = PG_GETARG_INT64(0);
281  int32 val2 = PG_GETARG_INT32(1);
282 
283  PG_RETURN_BOOL(val1 < val2);
284 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
signed int int32
Definition: c.h:256
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int84mi ( PG_FUNCTION_ARGS  )

Definition at line 839 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, PG_GETARG_INT64, PG_RETURN_INT64, result, and SAMESIGN.

840 {
841  int64 arg1 = PG_GETARG_INT64(0);
842  int32 arg2 = PG_GETARG_INT32(1);
843  int64 result;
844 
845  result = arg1 - arg2;
846 
847  /*
848  * Overflow check. If the inputs are of the same sign then their
849  * difference cannot overflow. If they are of different signs then the
850  * result should be of the same sign as the first input.
851  */
852  if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
853  ereport(ERROR,
854  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
855  errmsg("bigint out of range")));
856  PG_RETURN_INT64(result);
857 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define SAMESIGN(a, b)
Definition: int8.c:28
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int84mul ( PG_FUNCTION_ARGS  )

Definition at line 860 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, PG_GETARG_INT64, PG_RETURN_INT64, and result.

861 {
862  int64 arg1 = PG_GETARG_INT64(0);
863  int32 arg2 = PG_GETARG_INT32(1);
864  int64 result;
865 
866  result = arg1 * arg2;
867 
868  /*
869  * Overflow check. We basically check to see if result / arg1 gives arg2
870  * again. There is one case where this fails: arg1 = 0 (which cannot
871  * overflow).
872  *
873  * Since the division is likely much more expensive than the actual
874  * multiplication, we'd like to skip it where possible. The best bang for
875  * the buck seems to be to check whether both inputs are in the int32
876  * range; if so, no overflow is possible.
877  */
878  if (arg1 != (int64) ((int32) arg1) &&
879  result / arg1 != arg2)
880  ereport(ERROR,
881  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
882  errmsg("bigint out of range")));
883  PG_RETURN_INT64(result);
884 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int84ne ( PG_FUNCTION_ARGS  )

Definition at line 269 of file int8.c.

References PG_GETARG_INT32, PG_GETARG_INT64, and PG_RETURN_BOOL.

270 {
271  int64 val1 = PG_GETARG_INT64(0);
272  int32 val2 = PG_GETARG_INT32(1);
273 
274  PG_RETURN_BOOL(val1 != val2);
275 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
signed int int32
Definition: c.h:256
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int84pl ( PG_FUNCTION_ARGS  )

Definition at line 818 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT32, PG_GETARG_INT64, PG_RETURN_INT64, result, and SAMESIGN.

819 {
820  int64 arg1 = PG_GETARG_INT64(0);
821  int32 arg2 = PG_GETARG_INT32(1);
822  int64 result;
823 
824  result = arg1 + arg2;
825 
826  /*
827  * Overflow check. If the inputs are of different signs then their sum
828  * cannot overflow. If the inputs are of the same sign, their sum had
829  * better be that sign too.
830  */
831  if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
832  ereport(ERROR,
833  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
834  errmsg("bigint out of range")));
835  PG_RETURN_INT64(result);
836 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define SAMESIGN(a, b)
Definition: int8.c:28
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8abs ( PG_FUNCTION_ARGS  )

Definition at line 630 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT64, PG_RETURN_INT64, and result.

631 {
632  int64 arg1 = PG_GETARG_INT64(0);
633  int64 result;
634 
635  result = (arg1 < 0) ? -arg1 : arg1;
636  /* overflow check (needed for INT64_MIN) */
637  if (result < 0)
638  ereport(ERROR,
639  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
640  errmsg("bigint out of range")));
641  PG_RETURN_INT64(result);
642 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8and ( PG_FUNCTION_ARGS  )

Definition at line 1222 of file int8.c.

References PG_GETARG_INT64, and PG_RETURN_INT64.

1223 {
1224  int64 arg1 = PG_GETARG_INT64(0);
1225  int64 arg2 = PG_GETARG_INT64(1);
1226 
1227  PG_RETURN_INT64(arg1 & arg2);
1228 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8dec ( PG_FUNCTION_ARGS  )

Definition at line 721 of file int8.c.

References AggCheckCallContext(), arg, ereport, errcode(), errmsg(), ERROR, NULL, PG_GETARG_INT64, PG_GETARG_POINTER, PG_RETURN_INT64, PG_RETURN_POINTER, and result.

Referenced by int8dec_any().

722 {
723  /*
724  * When int8 is pass-by-reference, we provide this special case to avoid
725  * palloc overhead for COUNT(): when called as an aggregate, we know that
726  * the argument is modifiable local storage, so just update it in-place.
727  * (If int8 is pass-by-value, then of course this is useless as well as
728  * incorrect, so just ifdef it out.)
729  */
730 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
731  if (AggCheckCallContext(fcinfo, NULL))
732  {
733  int64 *arg = (int64 *) PG_GETARG_POINTER(0);
734  int64 result;
735 
736  result = *arg - 1;
737  /* Overflow check */
738  if (result > 0 && *arg < 0)
739  ereport(ERROR,
740  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
741  errmsg("bigint out of range")));
742 
743  *arg = result;
744  PG_RETURN_POINTER(arg);
745  }
746  else
747 #endif
748  {
749  /* Not called as an aggregate, so just do it the dumb way */
750  int64 arg = PG_GETARG_INT64(0);
751  int64 result;
752 
753  result = arg - 1;
754  /* Overflow check */
755  if (result > 0 && arg < 0)
756  ereport(ERROR,
757  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
758  errmsg("bigint out of range")));
759 
760  PG_RETURN_INT64(result);
761  }
762 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:313
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:233
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3660
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * arg
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8dec_any ( PG_FUNCTION_ARGS  )

Definition at line 787 of file int8.c.

References int8dec().

788 {
789  return int8dec(fcinfo);
790 }
Datum int8dec(PG_FUNCTION_ARGS)
Definition: int8.c:721
Datum int8div ( PG_FUNCTION_ARGS  )

Definition at line 587 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT64, PG_RETURN_INT64, PG_RETURN_NULL, result, and SAMESIGN.

588 {
589  int64 arg1 = PG_GETARG_INT64(0);
590  int64 arg2 = PG_GETARG_INT64(1);
591  int64 result;
592 
593  if (arg2 == 0)
594  {
595  ereport(ERROR,
596  (errcode(ERRCODE_DIVISION_BY_ZERO),
597  errmsg("division by zero")));
598  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
599  PG_RETURN_NULL();
600  }
601 
602  /*
603  * INT64_MIN / -1 is problematic, since the result can't be represented on
604  * a two's-complement machine. Some machines produce INT64_MIN, some
605  * produce zero, some throw an exception. We can dodge the problem by
606  * recognizing that division by -1 is the same as negation.
607  */
608  if (arg2 == -1)
609  {
610  result = -arg1;
611  /* overflow check (needed for INT64_MIN) */
612  if (arg1 != 0 && SAMESIGN(result, arg1))
613  ereport(ERROR,
614  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
615  errmsg("bigint out of range")));
616  PG_RETURN_INT64(result);
617  }
618 
619  /* No overflow is possible */
620 
621  result = arg1 / arg2;
622 
623  PG_RETURN_INT64(result);
624 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
#define SAMESIGN(a, b)
Definition: int8.c:28
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
#define PG_RETURN_NULL()
Definition: fmgr.h:297
Datum int8eq ( PG_FUNCTION_ARGS  )

Definition at line 203 of file int8.c.

References PG_GETARG_INT64, and PG_RETURN_BOOL.

204 {
205  int64 val1 = PG_GETARG_INT64(0);
206  int64 val2 = PG_GETARG_INT64(1);
207 
208  PG_RETURN_BOOL(val1 == val2);
209 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8ge ( PG_FUNCTION_ARGS  )

Definition at line 248 of file int8.c.

References PG_GETARG_INT64, and PG_RETURN_BOOL.

249 {
250  int64 val1 = PG_GETARG_INT64(0);
251  int64 val2 = PG_GETARG_INT64(1);
252 
253  PG_RETURN_BOOL(val1 >= val2);
254 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8gt ( PG_FUNCTION_ARGS  )

Definition at line 230 of file int8.c.

References PG_GETARG_INT64, and PG_RETURN_BOOL.

231 {
232  int64 val1 = PG_GETARG_INT64(0);
233  int64 val2 = PG_GETARG_INT64(1);
234 
235  PG_RETURN_BOOL(val1 > val2);
236 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8in ( PG_FUNCTION_ARGS  )

Definition at line 145 of file int8.c.

References PG_GETARG_CSTRING, PG_RETURN_INT64, result, and scanint8().

Referenced by defGetInt64().

146 {
147  char *str = PG_GETARG_CSTRING(0);
148  int64 result;
149 
150  (void) scanint8(str, false, &result);
151  PG_RETURN_INT64(result);
152 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
return result
Definition: formatting.c:1618
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:234
bool scanint8(const char *str, bool errorOK, int64 *result)
Definition: int8.c:55
Datum int8inc ( PG_FUNCTION_ARGS  )

Definition at line 677 of file int8.c.

References AggCheckCallContext(), arg, ereport, errcode(), errmsg(), ERROR, NULL, PG_GETARG_INT64, PG_GETARG_POINTER, PG_RETURN_INT64, PG_RETURN_POINTER, and result.

Referenced by int8inc_any(), and int8inc_float8_float8().

678 {
679  /*
680  * When int8 is pass-by-reference, we provide this special case to avoid
681  * palloc overhead for COUNT(): when called as an aggregate, we know that
682  * the argument is modifiable local storage, so just update it in-place.
683  * (If int8 is pass-by-value, then of course this is useless as well as
684  * incorrect, so just ifdef it out.)
685  */
686 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
687  if (AggCheckCallContext(fcinfo, NULL))
688  {
689  int64 *arg = (int64 *) PG_GETARG_POINTER(0);
690  int64 result;
691 
692  result = *arg + 1;
693  /* Overflow check */
694  if (result < 0 && *arg > 0)
695  ereport(ERROR,
696  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
697  errmsg("bigint out of range")));
698 
699  *arg = result;
700  PG_RETURN_POINTER(arg);
701  }
702  else
703 #endif
704  {
705  /* Not called as an aggregate, so just do it the dumb way */
706  int64 arg = PG_GETARG_INT64(0);
707  int64 result;
708 
709  result = arg + 1;
710  /* Overflow check */
711  if (result < 0 && arg > 0)
712  ereport(ERROR,
713  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
714  errmsg("bigint out of range")));
715 
716  PG_RETURN_INT64(result);
717  }
718 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:313
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:233
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3660
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * arg
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8inc_any ( PG_FUNCTION_ARGS  )

Definition at line 775 of file int8.c.

References int8inc().

776 {
777  return int8inc(fcinfo);
778 }
Datum int8inc(PG_FUNCTION_ARGS)
Definition: int8.c:677
Datum int8inc_float8_float8 ( PG_FUNCTION_ARGS  )

Definition at line 781 of file int8.c.

References int8inc().

782 {
783  return int8inc(fcinfo);
784 }
Datum int8inc(PG_FUNCTION_ARGS)
Definition: int8.c:677
Datum int8larger ( PG_FUNCTION_ARGS  )

Definition at line 794 of file int8.c.

References PG_GETARG_INT64, PG_RETURN_INT64, and result.

795 {
796  int64 arg1 = PG_GETARG_INT64(0);
797  int64 arg2 = PG_GETARG_INT64(1);
798  int64 result;
799 
800  result = ((arg1 > arg2) ? arg1 : arg2);
801 
802  PG_RETURN_INT64(result);
803 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
return result
Definition: formatting.c:1618
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8le ( PG_FUNCTION_ARGS  )

Definition at line 239 of file int8.c.

References PG_GETARG_INT64, and PG_RETURN_BOOL.

240 {
241  int64 val1 = PG_GETARG_INT64(0);
242  int64 val2 = PG_GETARG_INT64(1);
243 
244  PG_RETURN_BOOL(val1 <= val2);
245 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8lt ( PG_FUNCTION_ARGS  )

Definition at line 221 of file int8.c.

References PG_GETARG_INT64, and PG_RETURN_BOOL.

222 {
223  int64 val1 = PG_GETARG_INT64(0);
224  int64 val2 = PG_GETARG_INT64(1);
225 
226  PG_RETURN_BOOL(val1 < val2);
227 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8mi ( PG_FUNCTION_ARGS  )

Definition at line 534 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT64, PG_RETURN_INT64, result, and SAMESIGN.

535 {
536  int64 arg1 = PG_GETARG_INT64(0);
537  int64 arg2 = PG_GETARG_INT64(1);
538  int64 result;
539 
540  result = arg1 - arg2;
541 
542  /*
543  * Overflow check. If the inputs are of the same sign then their
544  * difference cannot overflow. If they are of different signs then the
545  * result should be of the same sign as the first input.
546  */
547  if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
548  ereport(ERROR,
549  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
550  errmsg("bigint out of range")));
551  PG_RETURN_INT64(result);
552 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
#define SAMESIGN(a, b)
Definition: int8.c:28
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8mod ( PG_FUNCTION_ARGS  )

Definition at line 648 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT64, PG_RETURN_INT64, and PG_RETURN_NULL.

649 {
650  int64 arg1 = PG_GETARG_INT64(0);
651  int64 arg2 = PG_GETARG_INT64(1);
652 
653  if (arg2 == 0)
654  {
655  ereport(ERROR,
656  (errcode(ERRCODE_DIVISION_BY_ZERO),
657  errmsg("division by zero")));
658  /* ensure compiler realizes we mustn't reach the division (gcc bug) */
659  PG_RETURN_NULL();
660  }
661 
662  /*
663  * Some machines throw a floating-point exception for INT64_MIN % -1,
664  * which is a bit silly since the correct answer is perfectly
665  * well-defined, namely zero.
666  */
667  if (arg2 == -1)
668  PG_RETURN_INT64(0);
669 
670  /* No overflow is possible */
671 
672  PG_RETURN_INT64(arg1 % arg2);
673 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
#define PG_RETURN_NULL()
Definition: fmgr.h:297
Datum int8mul ( PG_FUNCTION_ARGS  )

Definition at line 555 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT64, PG_RETURN_INT64, and result.

Referenced by int4_cash(), int8_cash(), and int8_to_char().

556 {
557  int64 arg1 = PG_GETARG_INT64(0);
558  int64 arg2 = PG_GETARG_INT64(1);
559  int64 result;
560 
561  result = arg1 * arg2;
562 
563  /*
564  * Overflow check. We basically check to see if result / arg2 gives arg1
565  * again. There are two cases where this fails: arg2 = 0 (which cannot
566  * overflow) and arg1 = INT64_MIN, arg2 = -1 (where the division itself
567  * will overflow and thus incorrectly match).
568  *
569  * Since the division is likely much more expensive than the actual
570  * multiplication, we'd like to skip it where possible. The best bang for
571  * the buck seems to be to check whether both inputs are in the int32
572  * range; if so, no overflow is possible.
573  */
574  if (arg1 != (int64) ((int32) arg1) || arg2 != (int64) ((int32) arg2))
575  {
576  if (arg2 != 0 &&
577  ((arg2 == -1 && arg1 < 0 && result < 0) ||
578  result / arg2 != arg1))
579  ereport(ERROR,
580  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
581  errmsg("bigint out of range")));
582  }
583  PG_RETURN_INT64(result);
584 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8ne ( PG_FUNCTION_ARGS  )

Definition at line 212 of file int8.c.

References PG_GETARG_INT64, and PG_RETURN_BOOL.

213 {
214  int64 val1 = PG_GETARG_INT64(0);
215  int64 val2 = PG_GETARG_INT64(1);
216 
217  PG_RETURN_BOOL(val1 != val2);
218 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8not ( PG_FUNCTION_ARGS  )

Definition at line 1249 of file int8.c.

References PG_GETARG_INT64, and PG_RETURN_INT64.

1250 {
1251  int64 arg1 = PG_GETARG_INT64(0);
1252 
1253  PG_RETURN_INT64(~arg1);
1254 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8or ( PG_FUNCTION_ARGS  )

Definition at line 1231 of file int8.c.

References PG_GETARG_INT64, and PG_RETURN_INT64.

1232 {
1233  int64 arg1 = PG_GETARG_INT64(0);
1234  int64 arg2 = PG_GETARG_INT64(1);
1235 
1236  PG_RETURN_INT64(arg1 | arg2);
1237 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8out ( PG_FUNCTION_ARGS  )

Definition at line 158 of file int8.c.

References buf, MAXINT8LEN, PG_GETARG_INT64, pg_lltoa(), PG_RETURN_CSTRING, pstrdup(), result, and val.

Referenced by int8_to_char().

159 {
160  int64 val = PG_GETARG_INT64(0);
161  char buf[MAXINT8LEN + 1];
162  char *result;
163 
164  pg_lltoa(val, buf);
165  result = pstrdup(buf);
166  PG_RETURN_CSTRING(result);
167 }
#define MAXINT8LEN
Definition: int8.c:26
char * pstrdup(const char *in)
Definition: mcxt.c:1077
return result
Definition: formatting.c:1618
static char * buf
Definition: pg_test_fsync.c:65
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:314
void pg_lltoa(int64 value, char *a)
Definition: numutils.c:186
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
long val
Definition: informix.c:689
Datum int8pl ( PG_FUNCTION_ARGS  )

Definition at line 513 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT64, PG_RETURN_INT64, result, and SAMESIGN.

Referenced by int8range_canonical().

514 {
515  int64 arg1 = PG_GETARG_INT64(0);
516  int64 arg2 = PG_GETARG_INT64(1);
517  int64 result;
518 
519  result = arg1 + arg2;
520 
521  /*
522  * Overflow check. If the inputs are of different signs then their sum
523  * cannot overflow. If the inputs are of the same sign, their sum had
524  * better be that sign too.
525  */
526  if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
527  ereport(ERROR,
528  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
529  errmsg("bigint out of range")));
530  PG_RETURN_INT64(result);
531 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
#define SAMESIGN(a, b)
Definition: int8.c:28
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8recv ( PG_FUNCTION_ARGS  )

Definition at line 173 of file int8.c.

References buf, PG_GETARG_POINTER, PG_RETURN_INT64, and pq_getmsgint64().

174 {
176 
178 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
StringInfoData * StringInfo
Definition: stringinfo.h:46
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:233
static char * buf
Definition: pg_test_fsync.c:65
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:486
Datum int8send ( PG_FUNCTION_ARGS  )

Definition at line 184 of file int8.c.

References buf, PG_GETARG_INT64, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), and pq_sendint64().

185 {
186  int64 arg1 = PG_GETARG_INT64(0);
188 
189  pq_begintypsend(&buf);
190  pq_sendint64(&buf, arg1);
192 }
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:359
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:321
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:379
static char * buf
Definition: pg_test_fsync.c:65
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
void pq_sendint64(StringInfo buf, int64 i)
Definition: pqformat.c:271
Datum int8shl ( PG_FUNCTION_ARGS  )

Definition at line 1257 of file int8.c.

References PG_GETARG_INT32, PG_GETARG_INT64, and PG_RETURN_INT64.

1258 {
1259  int64 arg1 = PG_GETARG_INT64(0);
1260  int32 arg2 = PG_GETARG_INT32(1);
1261 
1262  PG_RETURN_INT64(arg1 << arg2);
1263 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
signed int int32
Definition: c.h:256
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8shr ( PG_FUNCTION_ARGS  )

Definition at line 1266 of file int8.c.

References PG_GETARG_INT32, PG_GETARG_INT64, and PG_RETURN_INT64.

1267 {
1268  int64 arg1 = PG_GETARG_INT64(0);
1269  int32 arg2 = PG_GETARG_INT32(1);
1270 
1271  PG_RETURN_INT64(arg1 >> arg2);
1272 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
signed int int32
Definition: c.h:256
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8smaller ( PG_FUNCTION_ARGS  )

Definition at line 806 of file int8.c.

References PG_GETARG_INT64, PG_RETURN_INT64, and result.

807 {
808  int64 arg1 = PG_GETARG_INT64(0);
809  int64 arg2 = PG_GETARG_INT64(1);
810  int64 result;
811 
812  result = ((arg1 < arg2) ? arg1 : arg2);
813 
814  PG_RETURN_INT64(result);
815 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
return result
Definition: formatting.c:1618
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8um ( PG_FUNCTION_ARGS  )

Definition at line 490 of file int8.c.

References arg, ereport, errcode(), errmsg(), ERROR, PG_GETARG_INT64, PG_RETURN_INT64, result, and SAMESIGN.

491 {
492  int64 arg = PG_GETARG_INT64(0);
493  int64 result;
494 
495  result = -arg;
496  /* overflow check (needed for INT64_MIN) */
497  if (arg != 0 && SAMESIGN(result, arg))
498  ereport(ERROR,
499  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
500  errmsg("bigint out of range")));
501  PG_RETURN_INT64(result);
502 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
#define SAMESIGN(a, b)
Definition: int8.c:28
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * arg
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8up ( PG_FUNCTION_ARGS  )

Definition at line 505 of file int8.c.

References arg, PG_GETARG_INT64, and PG_RETURN_INT64.

506 {
507  int64 arg = PG_GETARG_INT64(0);
508 
509  PG_RETURN_INT64(arg);
510 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
void * arg
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum int8xor ( PG_FUNCTION_ARGS  )

Definition at line 1240 of file int8.c.

References PG_GETARG_INT64, and PG_RETURN_INT64.

1241 {
1242  int64 arg1 = PG_GETARG_INT64(0);
1243  int64 arg2 = PG_GETARG_INT64(1);
1244 
1245  PG_RETURN_INT64(arg1 ^ arg2);
1246 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
#define PG_GETARG_INT64(n)
Definition: fmgr.h:239
Datum oidtoi8 ( PG_FUNCTION_ARGS  )

Definition at line 1423 of file int8.c.

References arg, PG_GETARG_OID, and PG_RETURN_INT64.

1424 {
1425  Oid arg = PG_GETARG_OID(0);
1426 
1427  PG_RETURN_INT64((int64) arg);
1428 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:319
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:232
void * arg
bool scanint8 ( const char *  str,
bool  errorOK,
int64 *  result 
)

Definition at line 55 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_INT64_MIN, and sign.

Referenced by int8in(), make_const(), and parse_output_parameters().

56 {
57  const char *ptr = str;
58  int64 tmp = 0;
59  int sign = 1;
60 
61  /*
62  * Do our own scan, rather than relying on sscanf which might be broken
63  * for long long.
64  */
65 
66  /* skip leading spaces */
67  while (*ptr && isspace((unsigned char) *ptr))
68  ptr++;
69 
70  /* handle sign */
71  if (*ptr == '-')
72  {
73  ptr++;
74 
75  /*
76  * Do an explicit check for INT64_MIN. Ugly though this is, it's
77  * cleaner than trying to get the loop below to handle it portably.
78  */
79  if (strncmp(ptr, "9223372036854775808", 19) == 0)
80  {
81  tmp = PG_INT64_MIN;
82  ptr += 19;
83  goto gotdigits;
84  }
85  sign = -1;
86  }
87  else if (*ptr == '+')
88  ptr++;
89 
90  /* require at least one digit */
91  if (!isdigit((unsigned char) *ptr))
92  {
93  if (errorOK)
94  return false;
95  else
96  ereport(ERROR,
97  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
98  errmsg("invalid input syntax for %s: \"%s\"",
99  "integer", str)));
100  }
101 
102  /* process digits */
103  while (*ptr && isdigit((unsigned char) *ptr))
104  {
105  int64 newtmp = tmp * 10 + (*ptr++ - '0');
106 
107  if ((newtmp / 10) != tmp) /* overflow? */
108  {
109  if (errorOK)
110  return false;
111  else
112  ereport(ERROR,
113  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
114  errmsg("value \"%s\" is out of range for type %s",
115  str, "bigint")));
116  }
117  tmp = newtmp;
118  }
119 
120 gotdigits:
121 
122  /* allow trailing whitespace, but not other trailing chars */
123  while (*ptr != '\0' && isspace((unsigned char) *ptr))
124  ptr++;
125 
126  if (*ptr != '\0')
127  {
128  if (errorOK)
129  return false;
130  else
131  ereport(ERROR,
132  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
133  errmsg("invalid input syntax for %s: \"%s\"",
134  "integer", str)));
135  }
136 
137  *result = (sign < 0) ? -tmp : tmp;
138 
139  return true;
140 }
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
char sign
Definition: informix.c:693
#define PG_INT64_MIN
Definition: c.h:342
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797