1 /*-------------------------------------------------------------------------
4 * Internal 64-bit integer operations
6 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/backend/utils/adt/int8.c,v 1.77 2010/01/07 04:53:34 tgl Exp $
12 *-------------------------------------------------------------------------
21 #include "libpq/pqformat.h"
22 #include "nodes/nodes.h"
23 #include "utils/int8.h"
28 #define SAMESIGN(a,b) (((a) < 0) == ((b) < 0))
35 } generate_series_fctx;
38 /***********************************************************************
40 ** Routines for 64-bit integers.
42 ***********************************************************************/
44 /*----------------------------------------------------------
45 * Formatting and conversion routines.
46 *---------------------------------------------------------*/
49 * scanint8 --- try to parse a string into an int8.
51 * If errorOK is false, ereport a useful error message if the string is bad.
52 * If errorOK is true, just return "false" for bad input.
55 scanint8(const char *str, bool errorOK, int64 *result)
57 const char *ptr = str;
62 * Do our own scan, rather than relying on sscanf which might be broken
66 /* skip leading spaces */
67 while (*ptr && isspace((unsigned char) *ptr))
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.
79 if (strncmp(ptr, "9223372036854775808", 19) == 0)
81 tmp = -INT64CONST(0x7fffffffffffffff) - 1;
90 /* require at least one digit */
91 if (!isdigit((unsigned char) *ptr))
97 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
98 errmsg("invalid input syntax for integer: \"%s\"",
103 while (*ptr && isdigit((unsigned char) *ptr))
105 int64 newtmp = tmp * 10 + (*ptr++ - '0');
107 if ((newtmp / 10) != tmp) /* overflow? */
113 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
114 errmsg("value \"%s\" is out of range for type bigint",
122 /* allow trailing whitespace, but not other trailing chars */
123 while (*ptr != '\0' && isspace((unsigned char) *ptr))
132 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
133 errmsg("invalid input syntax for integer: \"%s\"",
137 *result = (sign < 0) ? -tmp : tmp;
145 int8in(PG_FUNCTION_ARGS)
147 char *str = PG_GETARG_CSTRING(0);
150 (void) scanint8(str, false, &result);
151 PG_RETURN_INT64(result);
158 int8out(PG_FUNCTION_ARGS)
160 int64 val = PG_GETARG_INT64(0);
163 char buf[MAXINT8LEN + 1];
165 if ((len = snprintf(buf, MAXINT8LEN, INT64_FORMAT, val)) < 0)
166 elog(ERROR, "could not format int8");
168 result = pstrdup(buf);
169 PG_RETURN_CSTRING(result);
173 * int8recv - converts external binary format to int8
176 int8recv(PG_FUNCTION_ARGS)
178 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
180 PG_RETURN_INT64(pq_getmsgint64(buf));
184 * int8send - converts int8 to binary format
187 int8send(PG_FUNCTION_ARGS)
189 int64 arg1 = PG_GETARG_INT64(0);
192 pq_begintypsend(&buf);
193 pq_sendint64(&buf, arg1);
194 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
198 /*----------------------------------------------------------
199 * Relational operators for int8s, including cross-data-type comparisons.
200 *---------------------------------------------------------*/
203 * Is val1 relop val2?
206 int8eq(PG_FUNCTION_ARGS)
208 int64 val1 = PG_GETARG_INT64(0);
209 int64 val2 = PG_GETARG_INT64(1);
211 PG_RETURN_BOOL(val1 == val2);
215 int8ne(PG_FUNCTION_ARGS)
217 int64 val1 = PG_GETARG_INT64(0);
218 int64 val2 = PG_GETARG_INT64(1);
220 PG_RETURN_BOOL(val1 != val2);
224 int8lt(PG_FUNCTION_ARGS)
226 int64 val1 = PG_GETARG_INT64(0);
227 int64 val2 = PG_GETARG_INT64(1);
229 PG_RETURN_BOOL(val1 < val2);
233 int8gt(PG_FUNCTION_ARGS)
235 int64 val1 = PG_GETARG_INT64(0);
236 int64 val2 = PG_GETARG_INT64(1);
238 PG_RETURN_BOOL(val1 > val2);
242 int8le(PG_FUNCTION_ARGS)
244 int64 val1 = PG_GETARG_INT64(0);
245 int64 val2 = PG_GETARG_INT64(1);
247 PG_RETURN_BOOL(val1 <= val2);
251 int8ge(PG_FUNCTION_ARGS)
253 int64 val1 = PG_GETARG_INT64(0);
254 int64 val2 = PG_GETARG_INT64(1);
256 PG_RETURN_BOOL(val1 >= val2);
260 * Is 64-bit val1 relop 32-bit val2?
263 int84eq(PG_FUNCTION_ARGS)
265 int64 val1 = PG_GETARG_INT64(0);
266 int32 val2 = PG_GETARG_INT32(1);
268 PG_RETURN_BOOL(val1 == val2);
272 int84ne(PG_FUNCTION_ARGS)
274 int64 val1 = PG_GETARG_INT64(0);
275 int32 val2 = PG_GETARG_INT32(1);
277 PG_RETURN_BOOL(val1 != val2);
281 int84lt(PG_FUNCTION_ARGS)
283 int64 val1 = PG_GETARG_INT64(0);
284 int32 val2 = PG_GETARG_INT32(1);
286 PG_RETURN_BOOL(val1 < val2);
290 int84gt(PG_FUNCTION_ARGS)
292 int64 val1 = PG_GETARG_INT64(0);
293 int32 val2 = PG_GETARG_INT32(1);
295 PG_RETURN_BOOL(val1 > val2);
299 int84le(PG_FUNCTION_ARGS)
301 int64 val1 = PG_GETARG_INT64(0);
302 int32 val2 = PG_GETARG_INT32(1);
304 PG_RETURN_BOOL(val1 <= val2);
308 int84ge(PG_FUNCTION_ARGS)
310 int64 val1 = PG_GETARG_INT64(0);
311 int32 val2 = PG_GETARG_INT32(1);
313 PG_RETURN_BOOL(val1 >= val2);
317 * Is 32-bit val1 relop 64-bit val2?
320 int48eq(PG_FUNCTION_ARGS)
322 int32 val1 = PG_GETARG_INT32(0);
323 int64 val2 = PG_GETARG_INT64(1);
325 PG_RETURN_BOOL(val1 == val2);
329 int48ne(PG_FUNCTION_ARGS)
331 int32 val1 = PG_GETARG_INT32(0);
332 int64 val2 = PG_GETARG_INT64(1);
334 PG_RETURN_BOOL(val1 != val2);
338 int48lt(PG_FUNCTION_ARGS)
340 int32 val1 = PG_GETARG_INT32(0);
341 int64 val2 = PG_GETARG_INT64(1);
343 PG_RETURN_BOOL(val1 < val2);
347 int48gt(PG_FUNCTION_ARGS)
349 int32 val1 = PG_GETARG_INT32(0);
350 int64 val2 = PG_GETARG_INT64(1);
352 PG_RETURN_BOOL(val1 > val2);
356 int48le(PG_FUNCTION_ARGS)
358 int32 val1 = PG_GETARG_INT32(0);
359 int64 val2 = PG_GETARG_INT64(1);
361 PG_RETURN_BOOL(val1 <= val2);
365 int48ge(PG_FUNCTION_ARGS)
367 int32 val1 = PG_GETARG_INT32(0);
368 int64 val2 = PG_GETARG_INT64(1);
370 PG_RETURN_BOOL(val1 >= val2);
374 * Is 64-bit val1 relop 16-bit val2?
377 int82eq(PG_FUNCTION_ARGS)
379 int64 val1 = PG_GETARG_INT64(0);
380 int16 val2 = PG_GETARG_INT16(1);
382 PG_RETURN_BOOL(val1 == val2);
386 int82ne(PG_FUNCTION_ARGS)
388 int64 val1 = PG_GETARG_INT64(0);
389 int16 val2 = PG_GETARG_INT16(1);
391 PG_RETURN_BOOL(val1 != val2);
395 int82lt(PG_FUNCTION_ARGS)
397 int64 val1 = PG_GETARG_INT64(0);
398 int16 val2 = PG_GETARG_INT16(1);
400 PG_RETURN_BOOL(val1 < val2);
404 int82gt(PG_FUNCTION_ARGS)
406 int64 val1 = PG_GETARG_INT64(0);
407 int16 val2 = PG_GETARG_INT16(1);
409 PG_RETURN_BOOL(val1 > val2);
413 int82le(PG_FUNCTION_ARGS)
415 int64 val1 = PG_GETARG_INT64(0);
416 int16 val2 = PG_GETARG_INT16(1);
418 PG_RETURN_BOOL(val1 <= val2);
422 int82ge(PG_FUNCTION_ARGS)
424 int64 val1 = PG_GETARG_INT64(0);
425 int16 val2 = PG_GETARG_INT16(1);
427 PG_RETURN_BOOL(val1 >= val2);
431 * Is 16-bit val1 relop 64-bit val2?
434 int28eq(PG_FUNCTION_ARGS)
436 int16 val1 = PG_GETARG_INT16(0);
437 int64 val2 = PG_GETARG_INT64(1);
439 PG_RETURN_BOOL(val1 == val2);
443 int28ne(PG_FUNCTION_ARGS)
445 int16 val1 = PG_GETARG_INT16(0);
446 int64 val2 = PG_GETARG_INT64(1);
448 PG_RETURN_BOOL(val1 != val2);
452 int28lt(PG_FUNCTION_ARGS)
454 int16 val1 = PG_GETARG_INT16(0);
455 int64 val2 = PG_GETARG_INT64(1);
457 PG_RETURN_BOOL(val1 < val2);
461 int28gt(PG_FUNCTION_ARGS)
463 int16 val1 = PG_GETARG_INT16(0);
464 int64 val2 = PG_GETARG_INT64(1);
466 PG_RETURN_BOOL(val1 > val2);
470 int28le(PG_FUNCTION_ARGS)
472 int16 val1 = PG_GETARG_INT16(0);
473 int64 val2 = PG_GETARG_INT64(1);
475 PG_RETURN_BOOL(val1 <= val2);
479 int28ge(PG_FUNCTION_ARGS)
481 int16 val1 = PG_GETARG_INT16(0);
482 int64 val2 = PG_GETARG_INT64(1);
484 PG_RETURN_BOOL(val1 >= val2);
488 /*----------------------------------------------------------
489 * Arithmetic operators on 64-bit integers.
490 *---------------------------------------------------------*/
493 int8um(PG_FUNCTION_ARGS)
495 int64 arg = PG_GETARG_INT64(0);
499 /* overflow check (needed for INT64_MIN) */
500 if (arg != 0 && SAMESIGN(result, arg))
502 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
503 errmsg("bigint out of range")));
504 PG_RETURN_INT64(result);
508 int8up(PG_FUNCTION_ARGS)
510 int64 arg = PG_GETARG_INT64(0);
512 PG_RETURN_INT64(arg);
516 int8pl(PG_FUNCTION_ARGS)
518 int64 arg1 = PG_GETARG_INT64(0);
519 int64 arg2 = PG_GETARG_INT64(1);
522 result = arg1 + arg2;
525 * Overflow check. If the inputs are of different signs then their sum
526 * cannot overflow. If the inputs are of the same sign, their sum had
527 * better be that sign too.
529 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
531 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
532 errmsg("bigint out of range")));
533 PG_RETURN_INT64(result);
537 int8mi(PG_FUNCTION_ARGS)
539 int64 arg1 = PG_GETARG_INT64(0);
540 int64 arg2 = PG_GETARG_INT64(1);
543 result = arg1 - arg2;
546 * Overflow check. If the inputs are of the same sign then their
547 * difference cannot overflow. If they are of different signs then the
548 * result should be of the same sign as the first input.
550 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
552 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
553 errmsg("bigint out of range")));
554 PG_RETURN_INT64(result);
558 int8mul(PG_FUNCTION_ARGS)
560 int64 arg1 = PG_GETARG_INT64(0);
561 int64 arg2 = PG_GETARG_INT64(1);
564 result = arg1 * arg2;
567 * Overflow check. We basically check to see if result / arg2 gives arg1
568 * again. There are two cases where this fails: arg2 = 0 (which cannot
569 * overflow) and arg1 = INT64_MIN, arg2 = -1 (where the division itself
570 * will overflow and thus incorrectly match).
572 * Since the division is likely much more expensive than the actual
573 * multiplication, we'd like to skip it where possible. The best bang for
574 * the buck seems to be to check whether both inputs are in the int32
575 * range; if so, no overflow is possible.
577 if (arg1 != (int64) ((int32) arg1) || arg2 != (int64) ((int32) arg2))
580 (result / arg2 != arg1 || (arg2 == -1 && arg1 < 0 && result < 0)))
582 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
583 errmsg("bigint out of range")));
585 PG_RETURN_INT64(result);
589 int8div(PG_FUNCTION_ARGS)
591 int64 arg1 = PG_GETARG_INT64(0);
592 int64 arg2 = PG_GETARG_INT64(1);
597 (errcode(ERRCODE_DIVISION_BY_ZERO),
598 errmsg("division by zero")));
600 result = arg1 / arg2;
603 * Overflow check. The only possible overflow case is for arg1 =
604 * INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
605 * can't be represented on a two's-complement machine. Most machines
606 * produce INT64_MIN but it seems some produce zero.
608 if (arg2 == -1 && arg1 < 0 && result <= 0)
610 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
611 errmsg("bigint out of range")));
612 PG_RETURN_INT64(result);
619 int8abs(PG_FUNCTION_ARGS)
621 int64 arg1 = PG_GETARG_INT64(0);
624 result = (arg1 < 0) ? -arg1 : arg1;
625 /* overflow check (needed for INT64_MIN) */
628 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
629 errmsg("bigint out of range")));
630 PG_RETURN_INT64(result);
637 int8mod(PG_FUNCTION_ARGS)
639 int64 arg1 = PG_GETARG_INT64(0);
640 int64 arg2 = PG_GETARG_INT64(1);
644 (errcode(ERRCODE_DIVISION_BY_ZERO),
645 errmsg("division by zero")));
646 /* No overflow is possible */
648 PG_RETURN_INT64(arg1 % arg2);
653 int8inc(PG_FUNCTION_ARGS)
656 * When int8 is pass-by-reference, we provide this special case to avoid
657 * palloc overhead for COUNT(): when called from nodeAgg, we know that the
658 * argument is modifiable local storage, so just update it in-place. (If
659 * int8 is pass-by-value, then of course this is useless as well as
660 * incorrect, so just ifdef it out.)
662 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
663 if (fcinfo->context &&
664 (IsA(fcinfo->context, AggState) ||
665 IsA(fcinfo->context, WindowAggState)))
667 int64 *arg = (int64 *) PG_GETARG_POINTER(0);
672 if (result < 0 && *arg > 0)
674 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
675 errmsg("bigint out of range")));
678 PG_RETURN_POINTER(arg);
683 /* Not called by nodeAgg, so just do it the dumb way */
684 int64 arg = PG_GETARG_INT64(0);
689 if (result < 0 && arg > 0)
691 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
692 errmsg("bigint out of range")));
694 PG_RETURN_INT64(result);
699 * These functions are exactly like int8inc but are used for aggregates that
700 * count only non-null values. Since the functions are declared strict,
701 * the null checks happen before we ever get here, and all we need do is
702 * increment the state value. We could actually make these pg_proc entries
703 * point right at int8inc, but then the opr_sanity regression test would
704 * complain about mismatched entries for a built-in function.
708 int8inc_any(PG_FUNCTION_ARGS)
710 return int8inc(fcinfo);
714 int8inc_float8_float8(PG_FUNCTION_ARGS)
716 return int8inc(fcinfo);
721 int8larger(PG_FUNCTION_ARGS)
723 int64 arg1 = PG_GETARG_INT64(0);
724 int64 arg2 = PG_GETARG_INT64(1);
727 result = ((arg1 > arg2) ? arg1 : arg2);
729 PG_RETURN_INT64(result);
733 int8smaller(PG_FUNCTION_ARGS)
735 int64 arg1 = PG_GETARG_INT64(0);
736 int64 arg2 = PG_GETARG_INT64(1);
739 result = ((arg1 < arg2) ? arg1 : arg2);
741 PG_RETURN_INT64(result);
745 int84pl(PG_FUNCTION_ARGS)
747 int64 arg1 = PG_GETARG_INT64(0);
748 int32 arg2 = PG_GETARG_INT32(1);
751 result = arg1 + arg2;
754 * Overflow check. If the inputs are of different signs then their sum
755 * cannot overflow. If the inputs are of the same sign, their sum had
756 * better be that sign too.
758 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
760 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
761 errmsg("bigint out of range")));
762 PG_RETURN_INT64(result);
766 int84mi(PG_FUNCTION_ARGS)
768 int64 arg1 = PG_GETARG_INT64(0);
769 int32 arg2 = PG_GETARG_INT32(1);
772 result = arg1 - arg2;
775 * Overflow check. If the inputs are of the same sign then their
776 * difference cannot overflow. If they are of different signs then the
777 * result should be of the same sign as the first input.
779 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
781 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
782 errmsg("bigint out of range")));
783 PG_RETURN_INT64(result);
787 int84mul(PG_FUNCTION_ARGS)
789 int64 arg1 = PG_GETARG_INT64(0);
790 int32 arg2 = PG_GETARG_INT32(1);
793 result = arg1 * arg2;
796 * Overflow check. We basically check to see if result / arg1 gives arg2
797 * again. There is one case where this fails: arg1 = 0 (which cannot
800 * Since the division is likely much more expensive than the actual
801 * multiplication, we'd like to skip it where possible. The best bang for
802 * the buck seems to be to check whether both inputs are in the int32
803 * range; if so, no overflow is possible.
805 if (arg1 != (int64) ((int32) arg1) &&
806 result / arg1 != arg2)
808 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
809 errmsg("bigint out of range")));
810 PG_RETURN_INT64(result);
814 int84div(PG_FUNCTION_ARGS)
816 int64 arg1 = PG_GETARG_INT64(0);
817 int32 arg2 = PG_GETARG_INT32(1);
822 (errcode(ERRCODE_DIVISION_BY_ZERO),
823 errmsg("division by zero")));
825 result = arg1 / arg2;
828 * Overflow check. The only possible overflow case is for arg1 =
829 * INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
830 * can't be represented on a two's-complement machine. Most machines
831 * produce INT64_MIN but it seems some produce zero.
833 if (arg2 == -1 && arg1 < 0 && result <= 0)
835 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
836 errmsg("bigint out of range")));
837 PG_RETURN_INT64(result);
841 int48pl(PG_FUNCTION_ARGS)
843 int32 arg1 = PG_GETARG_INT32(0);
844 int64 arg2 = PG_GETARG_INT64(1);
847 result = arg1 + arg2;
850 * Overflow check. If the inputs are of different signs then their sum
851 * cannot overflow. If the inputs are of the same sign, their sum had
852 * better be that sign too.
854 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
856 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
857 errmsg("bigint out of range")));
858 PG_RETURN_INT64(result);
862 int48mi(PG_FUNCTION_ARGS)
864 int32 arg1 = PG_GETARG_INT32(0);
865 int64 arg2 = PG_GETARG_INT64(1);
868 result = arg1 - arg2;
871 * Overflow check. If the inputs are of the same sign then their
872 * difference cannot overflow. If they are of different signs then the
873 * result should be of the same sign as the first input.
875 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
877 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
878 errmsg("bigint out of range")));
879 PG_RETURN_INT64(result);
883 int48mul(PG_FUNCTION_ARGS)
885 int32 arg1 = PG_GETARG_INT32(0);
886 int64 arg2 = PG_GETARG_INT64(1);
889 result = arg1 * arg2;
892 * Overflow check. We basically check to see if result / arg2 gives arg1
893 * again. There is one case where this fails: arg2 = 0 (which cannot
896 * Since the division is likely much more expensive than the actual
897 * multiplication, we'd like to skip it where possible. The best bang for
898 * the buck seems to be to check whether both inputs are in the int32
899 * range; if so, no overflow is possible.
901 if (arg2 != (int64) ((int32) arg2) &&
902 result / arg2 != arg1)
904 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
905 errmsg("bigint out of range")));
906 PG_RETURN_INT64(result);
910 int48div(PG_FUNCTION_ARGS)
912 int32 arg1 = PG_GETARG_INT32(0);
913 int64 arg2 = PG_GETARG_INT64(1);
918 (errcode(ERRCODE_DIVISION_BY_ZERO),
919 errmsg("division by zero")));
920 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
924 /* No overflow is possible */
925 PG_RETURN_INT64((int64) arg1 / arg2);
929 int82pl(PG_FUNCTION_ARGS)
931 int64 arg1 = PG_GETARG_INT64(0);
932 int16 arg2 = PG_GETARG_INT16(1);
935 result = arg1 + arg2;
938 * Overflow check. If the inputs are of different signs then their sum
939 * cannot overflow. If the inputs are of the same sign, their sum had
940 * better be that sign too.
942 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
944 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
945 errmsg("bigint out of range")));
946 PG_RETURN_INT64(result);
950 int82mi(PG_FUNCTION_ARGS)
952 int64 arg1 = PG_GETARG_INT64(0);
953 int16 arg2 = PG_GETARG_INT16(1);
956 result = arg1 - arg2;
959 * Overflow check. If the inputs are of the same sign then their
960 * difference cannot overflow. If they are of different signs then the
961 * result should be of the same sign as the first input.
963 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
965 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
966 errmsg("bigint out of range")));
967 PG_RETURN_INT64(result);
971 int82mul(PG_FUNCTION_ARGS)
973 int64 arg1 = PG_GETARG_INT64(0);
974 int16 arg2 = PG_GETARG_INT16(1);
977 result = arg1 * arg2;
980 * Overflow check. We basically check to see if result / arg1 gives arg2
981 * again. There is one case where this fails: arg1 = 0 (which cannot
984 * Since the division is likely much more expensive than the actual
985 * multiplication, we'd like to skip it where possible. The best bang for
986 * the buck seems to be to check whether both inputs are in the int32
987 * range; if so, no overflow is possible.
989 if (arg1 != (int64) ((int32) arg1) &&
990 result / arg1 != arg2)
992 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
993 errmsg("bigint out of range")));
994 PG_RETURN_INT64(result);
998 int82div(PG_FUNCTION_ARGS)
1000 int64 arg1 = PG_GETARG_INT64(0);
1001 int16 arg2 = PG_GETARG_INT16(1);
1006 (errcode(ERRCODE_DIVISION_BY_ZERO),
1007 errmsg("division by zero")));
1009 result = arg1 / arg2;
1012 * Overflow check. The only possible overflow case is for arg1 =
1013 * INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
1014 * can't be represented on a two's-complement machine. Most machines
1015 * produce INT64_MIN but it seems some produce zero.
1017 if (arg2 == -1 && arg1 < 0 && result <= 0)
1019 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1020 errmsg("bigint out of range")));
1021 PG_RETURN_INT64(result);
1025 int28pl(PG_FUNCTION_ARGS)
1027 int16 arg1 = PG_GETARG_INT16(0);
1028 int64 arg2 = PG_GETARG_INT64(1);
1031 result = arg1 + arg2;
1034 * Overflow check. If the inputs are of different signs then their sum
1035 * cannot overflow. If the inputs are of the same sign, their sum had
1036 * better be that sign too.
1038 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1040 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1041 errmsg("bigint out of range")));
1042 PG_RETURN_INT64(result);
1046 int28mi(PG_FUNCTION_ARGS)
1048 int16 arg1 = PG_GETARG_INT16(0);
1049 int64 arg2 = PG_GETARG_INT64(1);
1052 result = arg1 - arg2;
1055 * Overflow check. If the inputs are of the same sign then their
1056 * difference cannot overflow. If they are of different signs then the
1057 * result should be of the same sign as the first input.
1059 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1061 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1062 errmsg("bigint out of range")));
1063 PG_RETURN_INT64(result);
1067 int28mul(PG_FUNCTION_ARGS)
1069 int16 arg1 = PG_GETARG_INT16(0);
1070 int64 arg2 = PG_GETARG_INT64(1);
1073 result = arg1 * arg2;
1076 * Overflow check. We basically check to see if result / arg2 gives arg1
1077 * again. There is one case where this fails: arg2 = 0 (which cannot
1080 * Since the division is likely much more expensive than the actual
1081 * multiplication, we'd like to skip it where possible. The best bang for
1082 * the buck seems to be to check whether both inputs are in the int32
1083 * range; if so, no overflow is possible.
1085 if (arg2 != (int64) ((int32) arg2) &&
1086 result / arg2 != arg1)
1088 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1089 errmsg("bigint out of range")));
1090 PG_RETURN_INT64(result);
1094 int28div(PG_FUNCTION_ARGS)
1096 int16 arg1 = PG_GETARG_INT16(0);
1097 int64 arg2 = PG_GETARG_INT64(1);
1102 (errcode(ERRCODE_DIVISION_BY_ZERO),
1103 errmsg("division by zero")));
1104 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1108 /* No overflow is possible */
1109 PG_RETURN_INT64((int64) arg1 / arg2);
1112 /* Binary arithmetics
1114 * int8and - returns arg1 & arg2
1115 * int8or - returns arg1 | arg2
1116 * int8xor - returns arg1 # arg2
1117 * int8not - returns ~arg1
1118 * int8shl - returns arg1 << arg2
1119 * int8shr - returns arg1 >> arg2
1123 int8and(PG_FUNCTION_ARGS)
1125 int64 arg1 = PG_GETARG_INT64(0);
1126 int64 arg2 = PG_GETARG_INT64(1);
1128 PG_RETURN_INT64(arg1 & arg2);
1132 int8or(PG_FUNCTION_ARGS)
1134 int64 arg1 = PG_GETARG_INT64(0);
1135 int64 arg2 = PG_GETARG_INT64(1);
1137 PG_RETURN_INT64(arg1 | arg2);
1141 int8xor(PG_FUNCTION_ARGS)
1143 int64 arg1 = PG_GETARG_INT64(0);
1144 int64 arg2 = PG_GETARG_INT64(1);
1146 PG_RETURN_INT64(arg1 ^ arg2);
1150 int8not(PG_FUNCTION_ARGS)
1152 int64 arg1 = PG_GETARG_INT64(0);
1154 PG_RETURN_INT64(~arg1);
1158 int8shl(PG_FUNCTION_ARGS)
1160 int64 arg1 = PG_GETARG_INT64(0);
1161 int32 arg2 = PG_GETARG_INT32(1);
1163 PG_RETURN_INT64(arg1 << arg2);
1167 int8shr(PG_FUNCTION_ARGS)
1169 int64 arg1 = PG_GETARG_INT64(0);
1170 int32 arg2 = PG_GETARG_INT32(1);
1172 PG_RETURN_INT64(arg1 >> arg2);
1175 /*----------------------------------------------------------
1176 * Conversion operators.
1177 *---------------------------------------------------------*/
1180 int48(PG_FUNCTION_ARGS)
1182 int32 arg = PG_GETARG_INT32(0);
1184 PG_RETURN_INT64((int64) arg);
1188 int84(PG_FUNCTION_ARGS)
1190 int64 arg = PG_GETARG_INT64(0);
1193 result = (int32) arg;
1195 /* Test for overflow by reverse-conversion. */
1196 if ((int64) result != arg)
1198 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1199 errmsg("integer out of range")));
1201 PG_RETURN_INT32(result);
1205 int28(PG_FUNCTION_ARGS)
1207 int16 arg = PG_GETARG_INT16(0);
1209 PG_RETURN_INT64((int64) arg);
1213 int82(PG_FUNCTION_ARGS)
1215 int64 arg = PG_GETARG_INT64(0);
1218 result = (int16) arg;
1220 /* Test for overflow by reverse-conversion. */
1221 if ((int64) result != arg)
1223 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1224 errmsg("smallint out of range")));
1226 PG_RETURN_INT16(result);
1230 i8tod(PG_FUNCTION_ARGS)
1232 int64 arg = PG_GETARG_INT64(0);
1237 PG_RETURN_FLOAT8(result);
1241 * Convert float8 to 8-byte integer.
1244 dtoi8(PG_FUNCTION_ARGS)
1246 float8 arg = PG_GETARG_FLOAT8(0);
1249 /* Round arg to nearest integer (but it's still in float form) */
1253 * Does it fit in an int64? Avoid assuming that we have handy constants
1254 * defined for the range boundaries, instead test for overflow by
1255 * reverse-conversion.
1257 result = (int64) arg;
1259 if ((float8) result != arg)
1261 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1262 errmsg("bigint out of range")));
1264 PG_RETURN_INT64(result);
1268 i8tof(PG_FUNCTION_ARGS)
1270 int64 arg = PG_GETARG_INT64(0);
1275 PG_RETURN_FLOAT4(result);
1279 * Convert float4 to 8-byte integer.
1282 ftoi8(PG_FUNCTION_ARGS)
1284 float4 arg = PG_GETARG_FLOAT4(0);
1288 /* Round arg to nearest integer (but it's still in float form) */
1292 * Does it fit in an int64? Avoid assuming that we have handy constants
1293 * defined for the range boundaries, instead test for overflow by
1294 * reverse-conversion.
1296 result = (int64) darg;
1298 if ((float8) result != darg)
1300 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1301 errmsg("bigint out of range")));
1303 PG_RETURN_INT64(result);
1307 i8tooid(PG_FUNCTION_ARGS)
1309 int64 arg = PG_GETARG_INT64(0);
1314 /* Test for overflow by reverse-conversion. */
1315 if ((int64) result != arg)
1317 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1318 errmsg("OID out of range")));
1320 PG_RETURN_OID(result);
1324 oidtoi8(PG_FUNCTION_ARGS)
1326 Oid arg = PG_GETARG_OID(0);
1328 PG_RETURN_INT64((int64) arg);
1332 * non-persistent numeric series generator
1335 generate_series_int8(PG_FUNCTION_ARGS)
1337 return generate_series_step_int8(fcinfo);
1341 generate_series_step_int8(PG_FUNCTION_ARGS)
1343 FuncCallContext *funcctx;
1344 generate_series_fctx *fctx;
1346 MemoryContext oldcontext;
1348 /* stuff done only on the first call of the function */
1349 if (SRF_IS_FIRSTCALL())
1351 int64 start = PG_GETARG_INT64(0);
1352 int64 finish = PG_GETARG_INT64(1);
1355 /* see if we were given an explicit step size */
1356 if (PG_NARGS() == 3)
1357 step = PG_GETARG_INT64(2);
1360 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1361 errmsg("step size cannot equal zero")));
1363 /* create a function context for cross-call persistence */
1364 funcctx = SRF_FIRSTCALL_INIT();
1367 * switch to memory context appropriate for multiple function calls
1369 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1371 /* allocate memory for user context */
1372 fctx = (generate_series_fctx *) palloc(sizeof(generate_series_fctx));
1375 * Use fctx to keep state from call to call. Seed current with the
1376 * original start value
1378 fctx->current = start;
1379 fctx->finish = finish;
1382 funcctx->user_fctx = fctx;
1383 MemoryContextSwitchTo(oldcontext);
1386 /* stuff done on every call of the function */
1387 funcctx = SRF_PERCALL_SETUP();
1390 * get the saved state and use current as the result for this iteration
1392 fctx = funcctx->user_fctx;
1393 result = fctx->current;
1395 if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1396 (fctx->step < 0 && fctx->current >= fctx->finish))
1398 /* increment current in preparation for next iteration */
1399 fctx->current += fctx->step;
1401 /* do when there is more left to send */
1402 SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
1405 /* do when there is no more left */
1406 SRF_RETURN_DONE(funcctx);