1 /*-------------------------------------------------------------------------
4 * Internal 64-bit integer operations
6 * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/backend/utils/adt/int8.c
12 *-------------------------------------------------------------------------
21 #include "libpq/pqformat.h"
22 #include "utils/int8.h"
23 #include "utils/builtins.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);
161 char buf[MAXINT8LEN + 1];
165 result = pstrdup(buf);
166 PG_RETURN_CSTRING(result);
170 * int8recv - converts external binary format to int8
173 int8recv(PG_FUNCTION_ARGS)
175 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
177 PG_RETURN_INT64(pq_getmsgint64(buf));
181 * int8send - converts int8 to binary format
184 int8send(PG_FUNCTION_ARGS)
186 int64 arg1 = PG_GETARG_INT64(0);
189 pq_begintypsend(&buf);
190 pq_sendint64(&buf, arg1);
191 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
195 /*----------------------------------------------------------
196 * Relational operators for int8s, including cross-data-type comparisons.
197 *---------------------------------------------------------*/
200 * Is val1 relop val2?
203 int8eq(PG_FUNCTION_ARGS)
205 int64 val1 = PG_GETARG_INT64(0);
206 int64 val2 = PG_GETARG_INT64(1);
208 PG_RETURN_BOOL(val1 == val2);
212 int8ne(PG_FUNCTION_ARGS)
214 int64 val1 = PG_GETARG_INT64(0);
215 int64 val2 = PG_GETARG_INT64(1);
217 PG_RETURN_BOOL(val1 != val2);
221 int8lt(PG_FUNCTION_ARGS)
223 int64 val1 = PG_GETARG_INT64(0);
224 int64 val2 = PG_GETARG_INT64(1);
226 PG_RETURN_BOOL(val1 < val2);
230 int8gt(PG_FUNCTION_ARGS)
232 int64 val1 = PG_GETARG_INT64(0);
233 int64 val2 = PG_GETARG_INT64(1);
235 PG_RETURN_BOOL(val1 > val2);
239 int8le(PG_FUNCTION_ARGS)
241 int64 val1 = PG_GETARG_INT64(0);
242 int64 val2 = PG_GETARG_INT64(1);
244 PG_RETURN_BOOL(val1 <= val2);
248 int8ge(PG_FUNCTION_ARGS)
250 int64 val1 = PG_GETARG_INT64(0);
251 int64 val2 = PG_GETARG_INT64(1);
253 PG_RETURN_BOOL(val1 >= val2);
257 * Is 64-bit val1 relop 32-bit val2?
260 int84eq(PG_FUNCTION_ARGS)
262 int64 val1 = PG_GETARG_INT64(0);
263 int32 val2 = PG_GETARG_INT32(1);
265 PG_RETURN_BOOL(val1 == val2);
269 int84ne(PG_FUNCTION_ARGS)
271 int64 val1 = PG_GETARG_INT64(0);
272 int32 val2 = PG_GETARG_INT32(1);
274 PG_RETURN_BOOL(val1 != val2);
278 int84lt(PG_FUNCTION_ARGS)
280 int64 val1 = PG_GETARG_INT64(0);
281 int32 val2 = PG_GETARG_INT32(1);
283 PG_RETURN_BOOL(val1 < val2);
287 int84gt(PG_FUNCTION_ARGS)
289 int64 val1 = PG_GETARG_INT64(0);
290 int32 val2 = PG_GETARG_INT32(1);
292 PG_RETURN_BOOL(val1 > val2);
296 int84le(PG_FUNCTION_ARGS)
298 int64 val1 = PG_GETARG_INT64(0);
299 int32 val2 = PG_GETARG_INT32(1);
301 PG_RETURN_BOOL(val1 <= val2);
305 int84ge(PG_FUNCTION_ARGS)
307 int64 val1 = PG_GETARG_INT64(0);
308 int32 val2 = PG_GETARG_INT32(1);
310 PG_RETURN_BOOL(val1 >= val2);
314 * Is 32-bit val1 relop 64-bit val2?
317 int48eq(PG_FUNCTION_ARGS)
319 int32 val1 = PG_GETARG_INT32(0);
320 int64 val2 = PG_GETARG_INT64(1);
322 PG_RETURN_BOOL(val1 == val2);
326 int48ne(PG_FUNCTION_ARGS)
328 int32 val1 = PG_GETARG_INT32(0);
329 int64 val2 = PG_GETARG_INT64(1);
331 PG_RETURN_BOOL(val1 != val2);
335 int48lt(PG_FUNCTION_ARGS)
337 int32 val1 = PG_GETARG_INT32(0);
338 int64 val2 = PG_GETARG_INT64(1);
340 PG_RETURN_BOOL(val1 < val2);
344 int48gt(PG_FUNCTION_ARGS)
346 int32 val1 = PG_GETARG_INT32(0);
347 int64 val2 = PG_GETARG_INT64(1);
349 PG_RETURN_BOOL(val1 > val2);
353 int48le(PG_FUNCTION_ARGS)
355 int32 val1 = PG_GETARG_INT32(0);
356 int64 val2 = PG_GETARG_INT64(1);
358 PG_RETURN_BOOL(val1 <= val2);
362 int48ge(PG_FUNCTION_ARGS)
364 int32 val1 = PG_GETARG_INT32(0);
365 int64 val2 = PG_GETARG_INT64(1);
367 PG_RETURN_BOOL(val1 >= val2);
371 * Is 64-bit val1 relop 16-bit val2?
374 int82eq(PG_FUNCTION_ARGS)
376 int64 val1 = PG_GETARG_INT64(0);
377 int16 val2 = PG_GETARG_INT16(1);
379 PG_RETURN_BOOL(val1 == val2);
383 int82ne(PG_FUNCTION_ARGS)
385 int64 val1 = PG_GETARG_INT64(0);
386 int16 val2 = PG_GETARG_INT16(1);
388 PG_RETURN_BOOL(val1 != val2);
392 int82lt(PG_FUNCTION_ARGS)
394 int64 val1 = PG_GETARG_INT64(0);
395 int16 val2 = PG_GETARG_INT16(1);
397 PG_RETURN_BOOL(val1 < val2);
401 int82gt(PG_FUNCTION_ARGS)
403 int64 val1 = PG_GETARG_INT64(0);
404 int16 val2 = PG_GETARG_INT16(1);
406 PG_RETURN_BOOL(val1 > val2);
410 int82le(PG_FUNCTION_ARGS)
412 int64 val1 = PG_GETARG_INT64(0);
413 int16 val2 = PG_GETARG_INT16(1);
415 PG_RETURN_BOOL(val1 <= val2);
419 int82ge(PG_FUNCTION_ARGS)
421 int64 val1 = PG_GETARG_INT64(0);
422 int16 val2 = PG_GETARG_INT16(1);
424 PG_RETURN_BOOL(val1 >= val2);
428 * Is 16-bit val1 relop 64-bit val2?
431 int28eq(PG_FUNCTION_ARGS)
433 int16 val1 = PG_GETARG_INT16(0);
434 int64 val2 = PG_GETARG_INT64(1);
436 PG_RETURN_BOOL(val1 == val2);
440 int28ne(PG_FUNCTION_ARGS)
442 int16 val1 = PG_GETARG_INT16(0);
443 int64 val2 = PG_GETARG_INT64(1);
445 PG_RETURN_BOOL(val1 != val2);
449 int28lt(PG_FUNCTION_ARGS)
451 int16 val1 = PG_GETARG_INT16(0);
452 int64 val2 = PG_GETARG_INT64(1);
454 PG_RETURN_BOOL(val1 < val2);
458 int28gt(PG_FUNCTION_ARGS)
460 int16 val1 = PG_GETARG_INT16(0);
461 int64 val2 = PG_GETARG_INT64(1);
463 PG_RETURN_BOOL(val1 > val2);
467 int28le(PG_FUNCTION_ARGS)
469 int16 val1 = PG_GETARG_INT16(0);
470 int64 val2 = PG_GETARG_INT64(1);
472 PG_RETURN_BOOL(val1 <= val2);
476 int28ge(PG_FUNCTION_ARGS)
478 int16 val1 = PG_GETARG_INT16(0);
479 int64 val2 = PG_GETARG_INT64(1);
481 PG_RETURN_BOOL(val1 >= val2);
485 /*----------------------------------------------------------
486 * Arithmetic operators on 64-bit integers.
487 *---------------------------------------------------------*/
490 int8um(PG_FUNCTION_ARGS)
492 int64 arg = PG_GETARG_INT64(0);
496 /* overflow check (needed for INT64_MIN) */
497 if (arg != 0 && SAMESIGN(result, arg))
499 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
500 errmsg("bigint out of range")));
501 PG_RETURN_INT64(result);
505 int8up(PG_FUNCTION_ARGS)
507 int64 arg = PG_GETARG_INT64(0);
509 PG_RETURN_INT64(arg);
513 int8pl(PG_FUNCTION_ARGS)
515 int64 arg1 = PG_GETARG_INT64(0);
516 int64 arg2 = PG_GETARG_INT64(1);
519 result = arg1 + arg2;
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.
526 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
528 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
529 errmsg("bigint out of range")));
530 PG_RETURN_INT64(result);
534 int8mi(PG_FUNCTION_ARGS)
536 int64 arg1 = PG_GETARG_INT64(0);
537 int64 arg2 = PG_GETARG_INT64(1);
540 result = arg1 - arg2;
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.
547 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
549 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
550 errmsg("bigint out of range")));
551 PG_RETURN_INT64(result);
555 int8mul(PG_FUNCTION_ARGS)
557 int64 arg1 = PG_GETARG_INT64(0);
558 int64 arg2 = PG_GETARG_INT64(1);
561 result = arg1 * arg2;
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).
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.
574 if (arg1 != (int64) ((int32) arg1) || arg2 != (int64) ((int32) arg2))
577 (result / arg2 != arg1 || (arg2 == -1 && arg1 < 0 && result < 0)))
579 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
580 errmsg("bigint out of range")));
582 PG_RETURN_INT64(result);
586 int8div(PG_FUNCTION_ARGS)
588 int64 arg1 = PG_GETARG_INT64(0);
589 int64 arg2 = PG_GETARG_INT64(1);
595 (errcode(ERRCODE_DIVISION_BY_ZERO),
596 errmsg("division by zero")));
597 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
601 result = arg1 / arg2;
604 * Overflow check. The only possible overflow case is for arg1 =
605 * INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
606 * can't be represented on a two's-complement machine. Most machines
607 * produce INT64_MIN but it seems some produce zero.
609 if (arg2 == -1 && arg1 < 0 && result <= 0)
611 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
612 errmsg("bigint out of range")));
613 PG_RETURN_INT64(result);
620 int8abs(PG_FUNCTION_ARGS)
622 int64 arg1 = PG_GETARG_INT64(0);
625 result = (arg1 < 0) ? -arg1 : arg1;
626 /* overflow check (needed for INT64_MIN) */
629 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
630 errmsg("bigint out of range")));
631 PG_RETURN_INT64(result);
638 int8mod(PG_FUNCTION_ARGS)
640 int64 arg1 = PG_GETARG_INT64(0);
641 int64 arg2 = PG_GETARG_INT64(1);
646 (errcode(ERRCODE_DIVISION_BY_ZERO),
647 errmsg("division by zero")));
648 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
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 as an aggregate, we know that
664 * the 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 as
666 * incorrect, so just ifdef it out.)
668 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
669 if (AggCheckCallContext(fcinfo, NULL))
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 as an aggregate, 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);
827 (errcode(ERRCODE_DIVISION_BY_ZERO),
828 errmsg("division by zero")));
829 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
833 result = arg1 / arg2;
836 * Overflow check. The only possible overflow case is for arg1 =
837 * INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
838 * can't be represented on a two's-complement machine. Most machines
839 * produce INT64_MIN but it seems some produce zero.
841 if (arg2 == -1 && arg1 < 0 && result <= 0)
843 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
844 errmsg("bigint out of range")));
845 PG_RETURN_INT64(result);
849 int48pl(PG_FUNCTION_ARGS)
851 int32 arg1 = PG_GETARG_INT32(0);
852 int64 arg2 = PG_GETARG_INT64(1);
855 result = arg1 + arg2;
858 * Overflow check. If the inputs are of different signs then their sum
859 * cannot overflow. If the inputs are of the same sign, their sum had
860 * better be that sign too.
862 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
864 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
865 errmsg("bigint out of range")));
866 PG_RETURN_INT64(result);
870 int48mi(PG_FUNCTION_ARGS)
872 int32 arg1 = PG_GETARG_INT32(0);
873 int64 arg2 = PG_GETARG_INT64(1);
876 result = arg1 - arg2;
879 * Overflow check. If the inputs are of the same sign then their
880 * difference cannot overflow. If they are of different signs then the
881 * result should be of the same sign as the first input.
883 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
885 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
886 errmsg("bigint out of range")));
887 PG_RETURN_INT64(result);
891 int48mul(PG_FUNCTION_ARGS)
893 int32 arg1 = PG_GETARG_INT32(0);
894 int64 arg2 = PG_GETARG_INT64(1);
897 result = arg1 * arg2;
900 * Overflow check. We basically check to see if result / arg2 gives arg1
901 * again. There is one case where this fails: arg2 = 0 (which cannot
904 * Since the division is likely much more expensive than the actual
905 * multiplication, we'd like to skip it where possible. The best bang for
906 * the buck seems to be to check whether both inputs are in the int32
907 * range; if so, no overflow is possible.
909 if (arg2 != (int64) ((int32) arg2) &&
910 result / arg2 != arg1)
912 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
913 errmsg("bigint out of range")));
914 PG_RETURN_INT64(result);
918 int48div(PG_FUNCTION_ARGS)
920 int32 arg1 = PG_GETARG_INT32(0);
921 int64 arg2 = PG_GETARG_INT64(1);
926 (errcode(ERRCODE_DIVISION_BY_ZERO),
927 errmsg("division by zero")));
928 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
932 /* No overflow is possible */
933 PG_RETURN_INT64((int64) arg1 / arg2);
937 int82pl(PG_FUNCTION_ARGS)
939 int64 arg1 = PG_GETARG_INT64(0);
940 int16 arg2 = PG_GETARG_INT16(1);
943 result = arg1 + arg2;
946 * Overflow check. If the inputs are of different signs then their sum
947 * cannot overflow. If the inputs are of the same sign, their sum had
948 * better be that sign too.
950 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
952 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
953 errmsg("bigint out of range")));
954 PG_RETURN_INT64(result);
958 int82mi(PG_FUNCTION_ARGS)
960 int64 arg1 = PG_GETARG_INT64(0);
961 int16 arg2 = PG_GETARG_INT16(1);
964 result = arg1 - arg2;
967 * Overflow check. If the inputs are of the same sign then their
968 * difference cannot overflow. If they are of different signs then the
969 * result should be of the same sign as the first input.
971 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
973 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
974 errmsg("bigint out of range")));
975 PG_RETURN_INT64(result);
979 int82mul(PG_FUNCTION_ARGS)
981 int64 arg1 = PG_GETARG_INT64(0);
982 int16 arg2 = PG_GETARG_INT16(1);
985 result = arg1 * arg2;
988 * Overflow check. We basically check to see if result / arg1 gives arg2
989 * again. There is one case where this fails: arg1 = 0 (which cannot
992 * Since the division is likely much more expensive than the actual
993 * multiplication, we'd like to skip it where possible. The best bang for
994 * the buck seems to be to check whether both inputs are in the int32
995 * range; if so, no overflow is possible.
997 if (arg1 != (int64) ((int32) arg1) &&
998 result / arg1 != arg2)
1000 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1001 errmsg("bigint out of range")));
1002 PG_RETURN_INT64(result);
1006 int82div(PG_FUNCTION_ARGS)
1008 int64 arg1 = PG_GETARG_INT64(0);
1009 int16 arg2 = PG_GETARG_INT16(1);
1015 (errcode(ERRCODE_DIVISION_BY_ZERO),
1016 errmsg("division by zero")));
1017 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1021 result = arg1 / arg2;
1024 * Overflow check. The only possible overflow case is for arg1 =
1025 * INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
1026 * can't be represented on a two's-complement machine. Most machines
1027 * produce INT64_MIN but it seems some produce zero.
1029 if (arg2 == -1 && arg1 < 0 && result <= 0)
1031 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1032 errmsg("bigint out of range")));
1033 PG_RETURN_INT64(result);
1037 int28pl(PG_FUNCTION_ARGS)
1039 int16 arg1 = PG_GETARG_INT16(0);
1040 int64 arg2 = PG_GETARG_INT64(1);
1043 result = arg1 + arg2;
1046 * Overflow check. If the inputs are of different signs then their sum
1047 * cannot overflow. If the inputs are of the same sign, their sum had
1048 * better be that sign too.
1050 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1052 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1053 errmsg("bigint out of range")));
1054 PG_RETURN_INT64(result);
1058 int28mi(PG_FUNCTION_ARGS)
1060 int16 arg1 = PG_GETARG_INT16(0);
1061 int64 arg2 = PG_GETARG_INT64(1);
1064 result = arg1 - arg2;
1067 * Overflow check. If the inputs are of the same sign then their
1068 * difference cannot overflow. If they are of different signs then the
1069 * result should be of the same sign as the first input.
1071 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1073 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1074 errmsg("bigint out of range")));
1075 PG_RETURN_INT64(result);
1079 int28mul(PG_FUNCTION_ARGS)
1081 int16 arg1 = PG_GETARG_INT16(0);
1082 int64 arg2 = PG_GETARG_INT64(1);
1085 result = arg1 * arg2;
1088 * Overflow check. We basically check to see if result / arg2 gives arg1
1089 * again. There is one case where this fails: arg2 = 0 (which cannot
1092 * Since the division is likely much more expensive than the actual
1093 * multiplication, we'd like to skip it where possible. The best bang for
1094 * the buck seems to be to check whether both inputs are in the int32
1095 * range; if so, no overflow is possible.
1097 if (arg2 != (int64) ((int32) arg2) &&
1098 result / arg2 != arg1)
1100 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1101 errmsg("bigint out of range")));
1102 PG_RETURN_INT64(result);
1106 int28div(PG_FUNCTION_ARGS)
1108 int16 arg1 = PG_GETARG_INT16(0);
1109 int64 arg2 = PG_GETARG_INT64(1);
1114 (errcode(ERRCODE_DIVISION_BY_ZERO),
1115 errmsg("division by zero")));
1116 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1120 /* No overflow is possible */
1121 PG_RETURN_INT64((int64) arg1 / arg2);
1124 /* Binary arithmetics
1126 * int8and - returns arg1 & arg2
1127 * int8or - returns arg1 | arg2
1128 * int8xor - returns arg1 # arg2
1129 * int8not - returns ~arg1
1130 * int8shl - returns arg1 << arg2
1131 * int8shr - returns arg1 >> arg2
1135 int8and(PG_FUNCTION_ARGS)
1137 int64 arg1 = PG_GETARG_INT64(0);
1138 int64 arg2 = PG_GETARG_INT64(1);
1140 PG_RETURN_INT64(arg1 & arg2);
1144 int8or(PG_FUNCTION_ARGS)
1146 int64 arg1 = PG_GETARG_INT64(0);
1147 int64 arg2 = PG_GETARG_INT64(1);
1149 PG_RETURN_INT64(arg1 | arg2);
1153 int8xor(PG_FUNCTION_ARGS)
1155 int64 arg1 = PG_GETARG_INT64(0);
1156 int64 arg2 = PG_GETARG_INT64(1);
1158 PG_RETURN_INT64(arg1 ^ arg2);
1162 int8not(PG_FUNCTION_ARGS)
1164 int64 arg1 = PG_GETARG_INT64(0);
1166 PG_RETURN_INT64(~arg1);
1170 int8shl(PG_FUNCTION_ARGS)
1172 int64 arg1 = PG_GETARG_INT64(0);
1173 int32 arg2 = PG_GETARG_INT32(1);
1175 PG_RETURN_INT64(arg1 << arg2);
1179 int8shr(PG_FUNCTION_ARGS)
1181 int64 arg1 = PG_GETARG_INT64(0);
1182 int32 arg2 = PG_GETARG_INT32(1);
1184 PG_RETURN_INT64(arg1 >> arg2);
1187 /*----------------------------------------------------------
1188 * Conversion operators.
1189 *---------------------------------------------------------*/
1192 int48(PG_FUNCTION_ARGS)
1194 int32 arg = PG_GETARG_INT32(0);
1196 PG_RETURN_INT64((int64) arg);
1200 int84(PG_FUNCTION_ARGS)
1202 int64 arg = PG_GETARG_INT64(0);
1205 result = (int32) arg;
1207 /* Test for overflow by reverse-conversion. */
1208 if ((int64) result != arg)
1210 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1211 errmsg("integer out of range")));
1213 PG_RETURN_INT32(result);
1217 int28(PG_FUNCTION_ARGS)
1219 int16 arg = PG_GETARG_INT16(0);
1221 PG_RETURN_INT64((int64) arg);
1225 int82(PG_FUNCTION_ARGS)
1227 int64 arg = PG_GETARG_INT64(0);
1230 result = (int16) arg;
1232 /* Test for overflow by reverse-conversion. */
1233 if ((int64) result != arg)
1235 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1236 errmsg("smallint out of range")));
1238 PG_RETURN_INT16(result);
1242 i8tod(PG_FUNCTION_ARGS)
1244 int64 arg = PG_GETARG_INT64(0);
1249 PG_RETURN_FLOAT8(result);
1253 * Convert float8 to 8-byte integer.
1256 dtoi8(PG_FUNCTION_ARGS)
1258 float8 arg = PG_GETARG_FLOAT8(0);
1261 /* Round arg to nearest integer (but it's still in float form) */
1265 * Does it fit in an int64? Avoid assuming that we have handy constants
1266 * defined for the range boundaries, instead test for overflow by
1267 * reverse-conversion.
1269 result = (int64) arg;
1271 if ((float8) result != arg)
1273 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1274 errmsg("bigint out of range")));
1276 PG_RETURN_INT64(result);
1280 i8tof(PG_FUNCTION_ARGS)
1282 int64 arg = PG_GETARG_INT64(0);
1287 PG_RETURN_FLOAT4(result);
1291 * Convert float4 to 8-byte integer.
1294 ftoi8(PG_FUNCTION_ARGS)
1296 float4 arg = PG_GETARG_FLOAT4(0);
1300 /* Round arg to nearest integer (but it's still in float form) */
1304 * Does it fit in an int64? Avoid assuming that we have handy constants
1305 * defined for the range boundaries, instead test for overflow by
1306 * reverse-conversion.
1308 result = (int64) darg;
1310 if ((float8) result != darg)
1312 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1313 errmsg("bigint out of range")));
1315 PG_RETURN_INT64(result);
1319 i8tooid(PG_FUNCTION_ARGS)
1321 int64 arg = PG_GETARG_INT64(0);
1326 /* Test for overflow by reverse-conversion. */
1327 if ((int64) result != arg)
1329 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1330 errmsg("OID out of range")));
1332 PG_RETURN_OID(result);
1336 oidtoi8(PG_FUNCTION_ARGS)
1338 Oid arg = PG_GETARG_OID(0);
1340 PG_RETURN_INT64((int64) arg);
1344 * non-persistent numeric series generator
1347 generate_series_int8(PG_FUNCTION_ARGS)
1349 return generate_series_step_int8(fcinfo);
1353 generate_series_step_int8(PG_FUNCTION_ARGS)
1355 FuncCallContext *funcctx;
1356 generate_series_fctx *fctx;
1358 MemoryContext oldcontext;
1360 /* stuff done only on the first call of the function */
1361 if (SRF_IS_FIRSTCALL())
1363 int64 start = PG_GETARG_INT64(0);
1364 int64 finish = PG_GETARG_INT64(1);
1367 /* see if we were given an explicit step size */
1368 if (PG_NARGS() == 3)
1369 step = PG_GETARG_INT64(2);
1372 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1373 errmsg("step size cannot equal zero")));
1375 /* create a function context for cross-call persistence */
1376 funcctx = SRF_FIRSTCALL_INIT();
1379 * switch to memory context appropriate for multiple function calls
1381 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1383 /* allocate memory for user context */
1384 fctx = (generate_series_fctx *) palloc(sizeof(generate_series_fctx));
1387 * Use fctx to keep state from call to call. Seed current with the
1388 * original start value
1390 fctx->current = start;
1391 fctx->finish = finish;
1394 funcctx->user_fctx = fctx;
1395 MemoryContextSwitchTo(oldcontext);
1398 /* stuff done on every call of the function */
1399 funcctx = SRF_PERCALL_SETUP();
1402 * get the saved state and use current as the result for this iteration
1404 fctx = funcctx->user_fctx;
1405 result = fctx->current;
1407 if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1408 (fctx->step < 0 && fctx->current >= fctx->finish))
1410 /* increment current in preparation for next iteration */
1411 fctx->current += fctx->step;
1413 /* if next-value computation overflows, this is the final result */
1414 if (SAMESIGN(result, fctx->step) && !SAMESIGN(result, fctx->current))
1417 /* do when there is more left to send */
1418 SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
1421 /* do when there is no more left */
1422 SRF_RETURN_DONE(funcctx);