1 /*-------------------------------------------------------------------------
4 * Internal 64-bit integer operations
6 * Portions Copyright (c) 1996-2008, 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.71 2008/10/05 23:18:37 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 #ifndef INT64_IS_BUSTED
80 if (strncmp(ptr, "9223372036854775808", 19) == 0)
82 tmp = -INT64CONST(0x7fffffffffffffff) - 1;
93 /* require at least one digit */
94 if (!isdigit((unsigned char) *ptr))
100 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
101 errmsg("invalid input syntax for integer: \"%s\"",
106 while (*ptr && isdigit((unsigned char) *ptr))
108 int64 newtmp = tmp * 10 + (*ptr++ - '0');
110 if ((newtmp / 10) != tmp) /* overflow? */
116 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
117 errmsg("value \"%s\" is out of range for type bigint",
125 /* allow trailing whitespace, but not other trailing chars */
126 while (*ptr != '\0' && isspace((unsigned char) *ptr))
135 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
136 errmsg("invalid input syntax for integer: \"%s\"",
140 *result = (sign < 0) ? -tmp : tmp;
148 int8in(PG_FUNCTION_ARGS)
150 char *str = PG_GETARG_CSTRING(0);
153 (void) scanint8(str, false, &result);
154 PG_RETURN_INT64(result);
161 int8out(PG_FUNCTION_ARGS)
163 int64 val = PG_GETARG_INT64(0);
166 char buf[MAXINT8LEN + 1];
168 if ((len = snprintf(buf, MAXINT8LEN, INT64_FORMAT, val)) < 0)
169 elog(ERROR, "could not format int8");
171 result = pstrdup(buf);
172 PG_RETURN_CSTRING(result);
176 * int8recv - converts external binary format to int8
179 int8recv(PG_FUNCTION_ARGS)
181 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
183 PG_RETURN_INT64(pq_getmsgint64(buf));
187 * int8send - converts int8 to binary format
190 int8send(PG_FUNCTION_ARGS)
192 int64 arg1 = PG_GETARG_INT64(0);
195 pq_begintypsend(&buf);
196 pq_sendint64(&buf, arg1);
197 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
201 /*----------------------------------------------------------
202 * Relational operators for int8s, including cross-data-type comparisons.
203 *---------------------------------------------------------*/
206 * Is val1 relop val2?
209 int8eq(PG_FUNCTION_ARGS)
211 int64 val1 = PG_GETARG_INT64(0);
212 int64 val2 = PG_GETARG_INT64(1);
214 PG_RETURN_BOOL(val1 == val2);
218 int8ne(PG_FUNCTION_ARGS)
220 int64 val1 = PG_GETARG_INT64(0);
221 int64 val2 = PG_GETARG_INT64(1);
223 PG_RETURN_BOOL(val1 != val2);
227 int8lt(PG_FUNCTION_ARGS)
229 int64 val1 = PG_GETARG_INT64(0);
230 int64 val2 = PG_GETARG_INT64(1);
232 PG_RETURN_BOOL(val1 < val2);
236 int8gt(PG_FUNCTION_ARGS)
238 int64 val1 = PG_GETARG_INT64(0);
239 int64 val2 = PG_GETARG_INT64(1);
241 PG_RETURN_BOOL(val1 > val2);
245 int8le(PG_FUNCTION_ARGS)
247 int64 val1 = PG_GETARG_INT64(0);
248 int64 val2 = PG_GETARG_INT64(1);
250 PG_RETURN_BOOL(val1 <= val2);
254 int8ge(PG_FUNCTION_ARGS)
256 int64 val1 = PG_GETARG_INT64(0);
257 int64 val2 = PG_GETARG_INT64(1);
259 PG_RETURN_BOOL(val1 >= val2);
263 * Is 64-bit val1 relop 32-bit val2?
266 int84eq(PG_FUNCTION_ARGS)
268 int64 val1 = PG_GETARG_INT64(0);
269 int32 val2 = PG_GETARG_INT32(1);
271 PG_RETURN_BOOL(val1 == val2);
275 int84ne(PG_FUNCTION_ARGS)
277 int64 val1 = PG_GETARG_INT64(0);
278 int32 val2 = PG_GETARG_INT32(1);
280 PG_RETURN_BOOL(val1 != val2);
284 int84lt(PG_FUNCTION_ARGS)
286 int64 val1 = PG_GETARG_INT64(0);
287 int32 val2 = PG_GETARG_INT32(1);
289 PG_RETURN_BOOL(val1 < val2);
293 int84gt(PG_FUNCTION_ARGS)
295 int64 val1 = PG_GETARG_INT64(0);
296 int32 val2 = PG_GETARG_INT32(1);
298 PG_RETURN_BOOL(val1 > val2);
302 int84le(PG_FUNCTION_ARGS)
304 int64 val1 = PG_GETARG_INT64(0);
305 int32 val2 = PG_GETARG_INT32(1);
307 PG_RETURN_BOOL(val1 <= val2);
311 int84ge(PG_FUNCTION_ARGS)
313 int64 val1 = PG_GETARG_INT64(0);
314 int32 val2 = PG_GETARG_INT32(1);
316 PG_RETURN_BOOL(val1 >= val2);
320 * Is 32-bit val1 relop 64-bit val2?
323 int48eq(PG_FUNCTION_ARGS)
325 int32 val1 = PG_GETARG_INT32(0);
326 int64 val2 = PG_GETARG_INT64(1);
328 PG_RETURN_BOOL(val1 == val2);
332 int48ne(PG_FUNCTION_ARGS)
334 int32 val1 = PG_GETARG_INT32(0);
335 int64 val2 = PG_GETARG_INT64(1);
337 PG_RETURN_BOOL(val1 != val2);
341 int48lt(PG_FUNCTION_ARGS)
343 int32 val1 = PG_GETARG_INT32(0);
344 int64 val2 = PG_GETARG_INT64(1);
346 PG_RETURN_BOOL(val1 < val2);
350 int48gt(PG_FUNCTION_ARGS)
352 int32 val1 = PG_GETARG_INT32(0);
353 int64 val2 = PG_GETARG_INT64(1);
355 PG_RETURN_BOOL(val1 > val2);
359 int48le(PG_FUNCTION_ARGS)
361 int32 val1 = PG_GETARG_INT32(0);
362 int64 val2 = PG_GETARG_INT64(1);
364 PG_RETURN_BOOL(val1 <= val2);
368 int48ge(PG_FUNCTION_ARGS)
370 int32 val1 = PG_GETARG_INT32(0);
371 int64 val2 = PG_GETARG_INT64(1);
373 PG_RETURN_BOOL(val1 >= val2);
377 * Is 64-bit val1 relop 16-bit val2?
380 int82eq(PG_FUNCTION_ARGS)
382 int64 val1 = PG_GETARG_INT64(0);
383 int16 val2 = PG_GETARG_INT16(1);
385 PG_RETURN_BOOL(val1 == val2);
389 int82ne(PG_FUNCTION_ARGS)
391 int64 val1 = PG_GETARG_INT64(0);
392 int16 val2 = PG_GETARG_INT16(1);
394 PG_RETURN_BOOL(val1 != val2);
398 int82lt(PG_FUNCTION_ARGS)
400 int64 val1 = PG_GETARG_INT64(0);
401 int16 val2 = PG_GETARG_INT16(1);
403 PG_RETURN_BOOL(val1 < val2);
407 int82gt(PG_FUNCTION_ARGS)
409 int64 val1 = PG_GETARG_INT64(0);
410 int16 val2 = PG_GETARG_INT16(1);
412 PG_RETURN_BOOL(val1 > val2);
416 int82le(PG_FUNCTION_ARGS)
418 int64 val1 = PG_GETARG_INT64(0);
419 int16 val2 = PG_GETARG_INT16(1);
421 PG_RETURN_BOOL(val1 <= val2);
425 int82ge(PG_FUNCTION_ARGS)
427 int64 val1 = PG_GETARG_INT64(0);
428 int16 val2 = PG_GETARG_INT16(1);
430 PG_RETURN_BOOL(val1 >= val2);
434 * Is 16-bit val1 relop 64-bit val2?
437 int28eq(PG_FUNCTION_ARGS)
439 int16 val1 = PG_GETARG_INT16(0);
440 int64 val2 = PG_GETARG_INT64(1);
442 PG_RETURN_BOOL(val1 == val2);
446 int28ne(PG_FUNCTION_ARGS)
448 int16 val1 = PG_GETARG_INT16(0);
449 int64 val2 = PG_GETARG_INT64(1);
451 PG_RETURN_BOOL(val1 != val2);
455 int28lt(PG_FUNCTION_ARGS)
457 int16 val1 = PG_GETARG_INT16(0);
458 int64 val2 = PG_GETARG_INT64(1);
460 PG_RETURN_BOOL(val1 < val2);
464 int28gt(PG_FUNCTION_ARGS)
466 int16 val1 = PG_GETARG_INT16(0);
467 int64 val2 = PG_GETARG_INT64(1);
469 PG_RETURN_BOOL(val1 > val2);
473 int28le(PG_FUNCTION_ARGS)
475 int16 val1 = PG_GETARG_INT16(0);
476 int64 val2 = PG_GETARG_INT64(1);
478 PG_RETURN_BOOL(val1 <= val2);
482 int28ge(PG_FUNCTION_ARGS)
484 int16 val1 = PG_GETARG_INT16(0);
485 int64 val2 = PG_GETARG_INT64(1);
487 PG_RETURN_BOOL(val1 >= val2);
491 /*----------------------------------------------------------
492 * Arithmetic operators on 64-bit integers.
493 *---------------------------------------------------------*/
496 int8um(PG_FUNCTION_ARGS)
498 int64 arg = PG_GETARG_INT64(0);
502 /* overflow check (needed for INT64_MIN) */
503 if (arg != 0 && SAMESIGN(result, arg))
505 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
506 errmsg("bigint out of range")));
507 PG_RETURN_INT64(result);
511 int8up(PG_FUNCTION_ARGS)
513 int64 arg = PG_GETARG_INT64(0);
515 PG_RETURN_INT64(arg);
519 int8pl(PG_FUNCTION_ARGS)
521 int64 arg1 = PG_GETARG_INT64(0);
522 int64 arg2 = PG_GETARG_INT64(1);
525 result = arg1 + arg2;
528 * Overflow check. If the inputs are of different signs then their sum
529 * cannot overflow. If the inputs are of the same sign, their sum had
530 * better be that sign too.
532 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
534 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
535 errmsg("bigint out of range")));
536 PG_RETURN_INT64(result);
540 int8mi(PG_FUNCTION_ARGS)
542 int64 arg1 = PG_GETARG_INT64(0);
543 int64 arg2 = PG_GETARG_INT64(1);
546 result = arg1 - arg2;
549 * Overflow check. If the inputs are of the same sign then their
550 * difference cannot overflow. If they are of different signs then the
551 * result should be of the same sign as the first input.
553 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
555 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
556 errmsg("bigint out of range")));
557 PG_RETURN_INT64(result);
561 int8mul(PG_FUNCTION_ARGS)
563 int64 arg1 = PG_GETARG_INT64(0);
564 int64 arg2 = PG_GETARG_INT64(1);
567 result = arg1 * arg2;
570 * Overflow check. We basically check to see if result / arg2 gives arg1
571 * again. There are two cases where this fails: arg2 = 0 (which cannot
572 * overflow) and arg1 = INT64_MIN, arg2 = -1 (where the division itself
573 * will overflow and thus incorrectly match).
575 * Since the division is likely much more expensive than the actual
576 * multiplication, we'd like to skip it where possible. The best bang for
577 * the buck seems to be to check whether both inputs are in the int32
578 * range; if so, no overflow is possible. (But that only works if we
579 * really have a 64-bit int64 datatype...)
581 #ifndef INT64_IS_BUSTED
582 if (arg1 != (int64) ((int32) arg1) || arg2 != (int64) ((int32) arg2))
586 (result / arg2 != arg1 || (arg2 == -1 && arg1 < 0 && result < 0)))
588 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
589 errmsg("bigint out of range")));
591 PG_RETURN_INT64(result);
595 int8div(PG_FUNCTION_ARGS)
597 int64 arg1 = PG_GETARG_INT64(0);
598 int64 arg2 = PG_GETARG_INT64(1);
603 (errcode(ERRCODE_DIVISION_BY_ZERO),
604 errmsg("division by zero")));
606 result = arg1 / arg2;
609 * Overflow check. The only possible overflow case is for arg1 =
610 * INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
611 * can't be represented on a two's-complement machine. Most machines
612 * produce INT64_MIN but it seems some produce zero.
614 if (arg2 == -1 && arg1 < 0 && result <= 0)
616 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
617 errmsg("bigint out of range")));
618 PG_RETURN_INT64(result);
625 int8abs(PG_FUNCTION_ARGS)
627 int64 arg1 = PG_GETARG_INT64(0);
630 result = (arg1 < 0) ? -arg1 : arg1;
631 /* overflow check (needed for INT64_MIN) */
634 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
635 errmsg("bigint out of range")));
636 PG_RETURN_INT64(result);
643 int8mod(PG_FUNCTION_ARGS)
645 int64 arg1 = PG_GETARG_INT64(0);
646 int64 arg2 = PG_GETARG_INT64(1);
650 (errcode(ERRCODE_DIVISION_BY_ZERO),
651 errmsg("division by zero")));
652 /* No overflow is possible */
654 PG_RETURN_INT64(arg1 % arg2);
659 int8inc(PG_FUNCTION_ARGS)
662 * When int8 is pass-by-reference, we provide this special case to avoid
663 * palloc overhead for COUNT(): when called from nodeAgg, we know that the
664 * argument is modifiable local storage, so just update it in-place.
665 * (If int8 is pass-by-value, then of course this is useless as well
666 * as incorrect, so just ifdef it out.)
668 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
669 if (fcinfo->context && IsA(fcinfo->context, AggState))
671 int64 *arg = (int64 *) PG_GETARG_POINTER(0);
676 if (result < 0 && *arg > 0)
678 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
679 errmsg("bigint out of range")));
682 PG_RETURN_POINTER(arg);
687 /* Not called by nodeAgg, so just do it the dumb way */
688 int64 arg = PG_GETARG_INT64(0);
693 if (result < 0 && arg > 0)
695 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
696 errmsg("bigint out of range")));
698 PG_RETURN_INT64(result);
703 * These functions are exactly like int8inc but are used for aggregates that
704 * count only non-null values. Since the functions are declared strict,
705 * the null checks happen before we ever get here, and all we need do is
706 * increment the state value. We could actually make these pg_proc entries
707 * point right at int8inc, but then the opr_sanity regression test would
708 * complain about mismatched entries for a built-in function.
712 int8inc_any(PG_FUNCTION_ARGS)
714 return int8inc(fcinfo);
718 int8inc_float8_float8(PG_FUNCTION_ARGS)
720 return int8inc(fcinfo);
725 int8larger(PG_FUNCTION_ARGS)
727 int64 arg1 = PG_GETARG_INT64(0);
728 int64 arg2 = PG_GETARG_INT64(1);
731 result = ((arg1 > arg2) ? arg1 : arg2);
733 PG_RETURN_INT64(result);
737 int8smaller(PG_FUNCTION_ARGS)
739 int64 arg1 = PG_GETARG_INT64(0);
740 int64 arg2 = PG_GETARG_INT64(1);
743 result = ((arg1 < arg2) ? arg1 : arg2);
745 PG_RETURN_INT64(result);
749 int84pl(PG_FUNCTION_ARGS)
751 int64 arg1 = PG_GETARG_INT64(0);
752 int32 arg2 = PG_GETARG_INT32(1);
755 result = arg1 + arg2;
758 * Overflow check. If the inputs are of different signs then their sum
759 * cannot overflow. If the inputs are of the same sign, their sum had
760 * better be that sign too.
762 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
764 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
765 errmsg("bigint out of range")));
766 PG_RETURN_INT64(result);
770 int84mi(PG_FUNCTION_ARGS)
772 int64 arg1 = PG_GETARG_INT64(0);
773 int32 arg2 = PG_GETARG_INT32(1);
776 result = arg1 - arg2;
779 * Overflow check. If the inputs are of the same sign then their
780 * difference cannot overflow. If they are of different signs then the
781 * result should be of the same sign as the first input.
783 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
785 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
786 errmsg("bigint out of range")));
787 PG_RETURN_INT64(result);
791 int84mul(PG_FUNCTION_ARGS)
793 int64 arg1 = PG_GETARG_INT64(0);
794 int32 arg2 = PG_GETARG_INT32(1);
797 result = arg1 * arg2;
800 * Overflow check. We basically check to see if result / arg1 gives arg2
801 * again. There is one case where this fails: arg1 = 0 (which cannot
804 * Since the division is likely much more expensive than the actual
805 * multiplication, we'd like to skip it where possible. The best bang for
806 * the buck seems to be to check whether both inputs are in the int32
807 * range; if so, no overflow is possible.
809 if (arg1 != (int64) ((int32) arg1) &&
810 result / arg1 != arg2)
812 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
813 errmsg("bigint out of range")));
814 PG_RETURN_INT64(result);
818 int84div(PG_FUNCTION_ARGS)
820 int64 arg1 = PG_GETARG_INT64(0);
821 int32 arg2 = PG_GETARG_INT32(1);
826 (errcode(ERRCODE_DIVISION_BY_ZERO),
827 errmsg("division by zero")));
829 result = arg1 / arg2;
832 * Overflow check. The only possible overflow case is for arg1 =
833 * INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
834 * can't be represented on a two's-complement machine. Most machines
835 * produce INT64_MIN but it seems some produce zero.
837 if (arg2 == -1 && arg1 < 0 && result <= 0)
839 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
840 errmsg("bigint out of range")));
841 PG_RETURN_INT64(result);
845 int48pl(PG_FUNCTION_ARGS)
847 int32 arg1 = PG_GETARG_INT32(0);
848 int64 arg2 = PG_GETARG_INT64(1);
851 result = arg1 + arg2;
854 * Overflow check. If the inputs are of different signs then their sum
855 * cannot overflow. If the inputs are of the same sign, their sum had
856 * better be that sign too.
858 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
860 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
861 errmsg("bigint out of range")));
862 PG_RETURN_INT64(result);
866 int48mi(PG_FUNCTION_ARGS)
868 int32 arg1 = PG_GETARG_INT32(0);
869 int64 arg2 = PG_GETARG_INT64(1);
872 result = arg1 - arg2;
875 * Overflow check. If the inputs are of the same sign then their
876 * difference cannot overflow. If they are of different signs then the
877 * result should be of the same sign as the first input.
879 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
881 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
882 errmsg("bigint out of range")));
883 PG_RETURN_INT64(result);
887 int48mul(PG_FUNCTION_ARGS)
889 int32 arg1 = PG_GETARG_INT32(0);
890 int64 arg2 = PG_GETARG_INT64(1);
893 result = arg1 * arg2;
896 * Overflow check. We basically check to see if result / arg2 gives arg1
897 * again. There is one case where this fails: arg2 = 0 (which cannot
900 * Since the division is likely much more expensive than the actual
901 * multiplication, we'd like to skip it where possible. The best bang for
902 * the buck seems to be to check whether both inputs are in the int32
903 * range; if so, no overflow is possible.
905 if (arg2 != (int64) ((int32) arg2) &&
906 result / arg2 != arg1)
908 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
909 errmsg("bigint out of range")));
910 PG_RETURN_INT64(result);
914 int48div(PG_FUNCTION_ARGS)
916 int32 arg1 = PG_GETARG_INT32(0);
917 int64 arg2 = PG_GETARG_INT64(1);
921 (errcode(ERRCODE_DIVISION_BY_ZERO),
922 errmsg("division by zero")));
923 /* No overflow is possible */
924 PG_RETURN_INT64((int64) arg1 / arg2);
928 int82pl(PG_FUNCTION_ARGS)
930 int64 arg1 = PG_GETARG_INT64(0);
931 int16 arg2 = PG_GETARG_INT16(1);
934 result = arg1 + arg2;
937 * Overflow check. If the inputs are of different signs then their sum
938 * cannot overflow. If the inputs are of the same sign, their sum had
939 * better be that sign too.
941 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
943 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
944 errmsg("bigint out of range")));
945 PG_RETURN_INT64(result);
949 int82mi(PG_FUNCTION_ARGS)
951 int64 arg1 = PG_GETARG_INT64(0);
952 int16 arg2 = PG_GETARG_INT16(1);
955 result = arg1 - arg2;
958 * Overflow check. If the inputs are of the same sign then their
959 * difference cannot overflow. If they are of different signs then the
960 * result should be of the same sign as the first input.
962 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
964 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
965 errmsg("bigint out of range")));
966 PG_RETURN_INT64(result);
970 int82mul(PG_FUNCTION_ARGS)
972 int64 arg1 = PG_GETARG_INT64(0);
973 int16 arg2 = PG_GETARG_INT16(1);
976 result = arg1 * arg2;
979 * Overflow check. We basically check to see if result / arg1 gives arg2
980 * again. There is one case where this fails: arg1 = 0 (which cannot
983 * Since the division is likely much more expensive than the actual
984 * multiplication, we'd like to skip it where possible. The best bang for
985 * the buck seems to be to check whether both inputs are in the int32
986 * range; if so, no overflow is possible.
988 if (arg1 != (int64) ((int32) arg1) &&
989 result / arg1 != arg2)
991 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
992 errmsg("bigint out of range")));
993 PG_RETURN_INT64(result);
997 int82div(PG_FUNCTION_ARGS)
999 int64 arg1 = PG_GETARG_INT64(0);
1000 int16 arg2 = PG_GETARG_INT16(1);
1005 (errcode(ERRCODE_DIVISION_BY_ZERO),
1006 errmsg("division by zero")));
1008 result = arg1 / arg2;
1011 * Overflow check. The only possible overflow case is for arg1 =
1012 * INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
1013 * can't be represented on a two's-complement machine. Most machines
1014 * produce INT64_MIN but it seems some produce zero.
1016 if (arg2 == -1 && arg1 < 0 && result <= 0)
1018 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1019 errmsg("bigint out of range")));
1020 PG_RETURN_INT64(result);
1024 int28pl(PG_FUNCTION_ARGS)
1026 int16 arg1 = PG_GETARG_INT16(0);
1027 int64 arg2 = PG_GETARG_INT64(1);
1030 result = arg1 + arg2;
1033 * Overflow check. If the inputs are of different signs then their sum
1034 * cannot overflow. If the inputs are of the same sign, their sum had
1035 * better be that sign too.
1037 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1039 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1040 errmsg("bigint out of range")));
1041 PG_RETURN_INT64(result);
1045 int28mi(PG_FUNCTION_ARGS)
1047 int16 arg1 = PG_GETARG_INT16(0);
1048 int64 arg2 = PG_GETARG_INT64(1);
1051 result = arg1 - arg2;
1054 * Overflow check. If the inputs are of the same sign then their
1055 * difference cannot overflow. If they are of different signs then the
1056 * result should be of the same sign as the first input.
1058 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1060 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1061 errmsg("bigint out of range")));
1062 PG_RETURN_INT64(result);
1066 int28mul(PG_FUNCTION_ARGS)
1068 int16 arg1 = PG_GETARG_INT16(0);
1069 int64 arg2 = PG_GETARG_INT64(1);
1072 result = arg1 * arg2;
1075 * Overflow check. We basically check to see if result / arg2 gives arg1
1076 * again. There is one case where this fails: arg2 = 0 (which cannot
1079 * Since the division is likely much more expensive than the actual
1080 * multiplication, we'd like to skip it where possible. The best bang for
1081 * the buck seems to be to check whether both inputs are in the int32
1082 * range; if so, no overflow is possible.
1084 if (arg2 != (int64) ((int32) arg2) &&
1085 result / arg2 != arg1)
1087 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1088 errmsg("bigint out of range")));
1089 PG_RETURN_INT64(result);
1093 int28div(PG_FUNCTION_ARGS)
1095 int16 arg1 = PG_GETARG_INT16(0);
1096 int64 arg2 = PG_GETARG_INT64(1);
1100 (errcode(ERRCODE_DIVISION_BY_ZERO),
1101 errmsg("division by zero")));
1102 /* No overflow is possible */
1103 PG_RETURN_INT64((int64) arg1 / arg2);
1106 /* Binary arithmetics
1108 * int8and - returns arg1 & arg2
1109 * int8or - returns arg1 | arg2
1110 * int8xor - returns arg1 # arg2
1111 * int8not - returns ~arg1
1112 * int8shl - returns arg1 << arg2
1113 * int8shr - returns arg1 >> arg2
1117 int8and(PG_FUNCTION_ARGS)
1119 int64 arg1 = PG_GETARG_INT64(0);
1120 int64 arg2 = PG_GETARG_INT64(1);
1122 PG_RETURN_INT64(arg1 & arg2);
1126 int8or(PG_FUNCTION_ARGS)
1128 int64 arg1 = PG_GETARG_INT64(0);
1129 int64 arg2 = PG_GETARG_INT64(1);
1131 PG_RETURN_INT64(arg1 | arg2);
1135 int8xor(PG_FUNCTION_ARGS)
1137 int64 arg1 = PG_GETARG_INT64(0);
1138 int64 arg2 = PG_GETARG_INT64(1);
1140 PG_RETURN_INT64(arg1 ^ arg2);
1144 int8not(PG_FUNCTION_ARGS)
1146 int64 arg1 = PG_GETARG_INT64(0);
1148 PG_RETURN_INT64(~arg1);
1152 int8shl(PG_FUNCTION_ARGS)
1154 int64 arg1 = PG_GETARG_INT64(0);
1155 int32 arg2 = PG_GETARG_INT32(1);
1157 PG_RETURN_INT64(arg1 << arg2);
1161 int8shr(PG_FUNCTION_ARGS)
1163 int64 arg1 = PG_GETARG_INT64(0);
1164 int32 arg2 = PG_GETARG_INT32(1);
1166 PG_RETURN_INT64(arg1 >> arg2);
1169 /*----------------------------------------------------------
1170 * Conversion operators.
1171 *---------------------------------------------------------*/
1174 int48(PG_FUNCTION_ARGS)
1176 int32 arg = PG_GETARG_INT32(0);
1178 PG_RETURN_INT64((int64) arg);
1182 int84(PG_FUNCTION_ARGS)
1184 int64 arg = PG_GETARG_INT64(0);
1187 result = (int32) arg;
1189 /* Test for overflow by reverse-conversion. */
1190 if ((int64) result != arg)
1192 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1193 errmsg("integer out of range")));
1195 PG_RETURN_INT32(result);
1199 int28(PG_FUNCTION_ARGS)
1201 int16 arg = PG_GETARG_INT16(0);
1203 PG_RETURN_INT64((int64) arg);
1207 int82(PG_FUNCTION_ARGS)
1209 int64 arg = PG_GETARG_INT64(0);
1212 result = (int16) arg;
1214 /* Test for overflow by reverse-conversion. */
1215 if ((int64) result != arg)
1217 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1218 errmsg("smallint out of range")));
1220 PG_RETURN_INT16(result);
1224 i8tod(PG_FUNCTION_ARGS)
1226 int64 arg = PG_GETARG_INT64(0);
1231 PG_RETURN_FLOAT8(result);
1235 * Convert float8 to 8-byte integer.
1238 dtoi8(PG_FUNCTION_ARGS)
1240 float8 arg = PG_GETARG_FLOAT8(0);
1243 /* Round arg to nearest integer (but it's still in float form) */
1247 * Does it fit in an int64? Avoid assuming that we have handy constants
1248 * defined for the range boundaries, instead test for overflow by
1249 * reverse-conversion.
1251 result = (int64) arg;
1253 if ((float8) result != arg)
1255 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1256 errmsg("bigint out of range")));
1258 PG_RETURN_INT64(result);
1262 i8tof(PG_FUNCTION_ARGS)
1264 int64 arg = PG_GETARG_INT64(0);
1269 PG_RETURN_FLOAT4(result);
1273 * Convert float4 to 8-byte integer.
1276 ftoi8(PG_FUNCTION_ARGS)
1278 float4 arg = PG_GETARG_FLOAT4(0);
1282 /* Round arg to nearest integer (but it's still in float form) */
1286 * Does it fit in an int64? Avoid assuming that we have handy constants
1287 * defined for the range boundaries, instead test for overflow by
1288 * reverse-conversion.
1290 result = (int64) darg;
1292 if ((float8) result != darg)
1294 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1295 errmsg("bigint out of range")));
1297 PG_RETURN_INT64(result);
1301 i8tooid(PG_FUNCTION_ARGS)
1303 int64 arg = PG_GETARG_INT64(0);
1308 /* Test for overflow by reverse-conversion. */
1309 if ((int64) result != arg)
1311 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1312 errmsg("OID out of range")));
1314 PG_RETURN_OID(result);
1318 oidtoi8(PG_FUNCTION_ARGS)
1320 Oid arg = PG_GETARG_OID(0);
1322 PG_RETURN_INT64((int64) arg);
1326 * non-persistent numeric series generator
1329 generate_series_int8(PG_FUNCTION_ARGS)
1331 return generate_series_step_int8(fcinfo);
1335 generate_series_step_int8(PG_FUNCTION_ARGS)
1337 FuncCallContext *funcctx;
1338 generate_series_fctx *fctx;
1340 MemoryContext oldcontext;
1342 /* stuff done only on the first call of the function */
1343 if (SRF_IS_FIRSTCALL())
1345 int64 start = PG_GETARG_INT64(0);
1346 int64 finish = PG_GETARG_INT64(1);
1349 /* see if we were given an explicit step size */
1350 if (PG_NARGS() == 3)
1351 step = PG_GETARG_INT64(2);
1354 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1355 errmsg("step size cannot equal zero")));
1357 /* create a function context for cross-call persistence */
1358 funcctx = SRF_FIRSTCALL_INIT();
1361 * switch to memory context appropriate for multiple function calls
1363 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1365 /* allocate memory for user context */
1366 fctx = (generate_series_fctx *) palloc(sizeof(generate_series_fctx));
1369 * Use fctx to keep state from call to call. Seed current with the
1370 * original start value
1372 fctx->current = start;
1373 fctx->finish = finish;
1376 funcctx->user_fctx = fctx;
1377 MemoryContextSwitchTo(oldcontext);
1380 /* stuff done on every call of the function */
1381 funcctx = SRF_PERCALL_SETUP();
1384 * get the saved state and use current as the result for this iteration
1386 fctx = funcctx->user_fctx;
1387 result = fctx->current;
1389 if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1390 (fctx->step < 0 && fctx->current >= fctx->finish))
1392 /* increment current in preparation for next iteration */
1393 fctx->current += fctx->step;
1395 /* do when there is more left to send */
1396 SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
1399 /* do when there is no more left */
1400 SRF_RETURN_DONE(funcctx);