1 /*-------------------------------------------------------------------------
4 * Internal 64-bit integer operations
6 * Portions Copyright (c) 1996-2014, 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 ((arg2 == -1 && arg1 < 0 && result < 0) ||
578 result / arg2 != arg1))
580 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
581 errmsg("bigint out of range")));
583 PG_RETURN_INT64(result);
587 int8div(PG_FUNCTION_ARGS)
589 int64 arg1 = PG_GETARG_INT64(0);
590 int64 arg2 = PG_GETARG_INT64(1);
596 (errcode(ERRCODE_DIVISION_BY_ZERO),
597 errmsg("division by zero")));
598 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
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.
611 /* overflow check (needed for INT64_MIN) */
612 if (arg1 != 0 && SAMESIGN(result, arg1))
614 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
615 errmsg("bigint out of range")));
616 PG_RETURN_INT64(result);
619 /* No overflow is possible */
621 result = arg1 / arg2;
623 PG_RETURN_INT64(result);
630 int8abs(PG_FUNCTION_ARGS)
632 int64 arg1 = PG_GETARG_INT64(0);
635 result = (arg1 < 0) ? -arg1 : arg1;
636 /* overflow check (needed for INT64_MIN) */
639 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
640 errmsg("bigint out of range")));
641 PG_RETURN_INT64(result);
648 int8mod(PG_FUNCTION_ARGS)
650 int64 arg1 = PG_GETARG_INT64(0);
651 int64 arg2 = PG_GETARG_INT64(1);
656 (errcode(ERRCODE_DIVISION_BY_ZERO),
657 errmsg("division by zero")));
658 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
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.
670 /* No overflow is possible */
672 PG_RETURN_INT64(arg1 % arg2);
677 int8inc(PG_FUNCTION_ARGS)
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.)
686 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
687 if (AggCheckCallContext(fcinfo, NULL))
689 int64 *arg = (int64 *) PG_GETARG_POINTER(0);
694 if (result < 0 && *arg > 0)
696 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
697 errmsg("bigint out of range")));
700 PG_RETURN_POINTER(arg);
705 /* Not called as an aggregate, so just do it the dumb way */
706 int64 arg = PG_GETARG_INT64(0);
711 if (result < 0 && arg > 0)
713 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
714 errmsg("bigint out of range")));
716 PG_RETURN_INT64(result);
721 * These functions are exactly like int8inc but are used for aggregates that
722 * count only non-null values. Since the functions are declared strict,
723 * the null checks happen before we ever get here, and all we need do is
724 * increment the state value. We could actually make these pg_proc entries
725 * point right at int8inc, but then the opr_sanity regression test would
726 * complain about mismatched entries for a built-in function.
730 int8inc_any(PG_FUNCTION_ARGS)
732 return int8inc(fcinfo);
736 int8inc_float8_float8(PG_FUNCTION_ARGS)
738 return int8inc(fcinfo);
743 int8larger(PG_FUNCTION_ARGS)
745 int64 arg1 = PG_GETARG_INT64(0);
746 int64 arg2 = PG_GETARG_INT64(1);
749 result = ((arg1 > arg2) ? arg1 : arg2);
751 PG_RETURN_INT64(result);
755 int8smaller(PG_FUNCTION_ARGS)
757 int64 arg1 = PG_GETARG_INT64(0);
758 int64 arg2 = PG_GETARG_INT64(1);
761 result = ((arg1 < arg2) ? arg1 : arg2);
763 PG_RETURN_INT64(result);
767 int84pl(PG_FUNCTION_ARGS)
769 int64 arg1 = PG_GETARG_INT64(0);
770 int32 arg2 = PG_GETARG_INT32(1);
773 result = arg1 + arg2;
776 * Overflow check. If the inputs are of different signs then their sum
777 * cannot overflow. If the inputs are of the same sign, their sum had
778 * better be that sign too.
780 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
782 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
783 errmsg("bigint out of range")));
784 PG_RETURN_INT64(result);
788 int84mi(PG_FUNCTION_ARGS)
790 int64 arg1 = PG_GETARG_INT64(0);
791 int32 arg2 = PG_GETARG_INT32(1);
794 result = arg1 - arg2;
797 * Overflow check. If the inputs are of the same sign then their
798 * difference cannot overflow. If they are of different signs then the
799 * result should be of the same sign as the first input.
801 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
803 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
804 errmsg("bigint out of range")));
805 PG_RETURN_INT64(result);
809 int84mul(PG_FUNCTION_ARGS)
811 int64 arg1 = PG_GETARG_INT64(0);
812 int32 arg2 = PG_GETARG_INT32(1);
815 result = arg1 * arg2;
818 * Overflow check. We basically check to see if result / arg1 gives arg2
819 * again. There is one case where this fails: arg1 = 0 (which cannot
822 * Since the division is likely much more expensive than the actual
823 * multiplication, we'd like to skip it where possible. The best bang for
824 * the buck seems to be to check whether both inputs are in the int32
825 * range; if so, no overflow is possible.
827 if (arg1 != (int64) ((int32) arg1) &&
828 result / arg1 != arg2)
830 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
831 errmsg("bigint out of range")));
832 PG_RETURN_INT64(result);
836 int84div(PG_FUNCTION_ARGS)
838 int64 arg1 = PG_GETARG_INT64(0);
839 int32 arg2 = PG_GETARG_INT32(1);
845 (errcode(ERRCODE_DIVISION_BY_ZERO),
846 errmsg("division by zero")));
847 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
852 * INT64_MIN / -1 is problematic, since the result can't be represented on
853 * a two's-complement machine. Some machines produce INT64_MIN, some
854 * produce zero, some throw an exception. We can dodge the problem by
855 * recognizing that division by -1 is the same as negation.
860 /* overflow check (needed for INT64_MIN) */
861 if (arg1 != 0 && SAMESIGN(result, arg1))
863 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
864 errmsg("bigint out of range")));
865 PG_RETURN_INT64(result);
868 /* No overflow is possible */
870 result = arg1 / arg2;
872 PG_RETURN_INT64(result);
876 int48pl(PG_FUNCTION_ARGS)
878 int32 arg1 = PG_GETARG_INT32(0);
879 int64 arg2 = PG_GETARG_INT64(1);
882 result = arg1 + arg2;
885 * Overflow check. If the inputs are of different signs then their sum
886 * cannot overflow. If the inputs are of the same sign, their sum had
887 * better be that sign too.
889 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
891 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
892 errmsg("bigint out of range")));
893 PG_RETURN_INT64(result);
897 int48mi(PG_FUNCTION_ARGS)
899 int32 arg1 = PG_GETARG_INT32(0);
900 int64 arg2 = PG_GETARG_INT64(1);
903 result = arg1 - arg2;
906 * Overflow check. If the inputs are of the same sign then their
907 * difference cannot overflow. If they are of different signs then the
908 * result should be of the same sign as the first input.
910 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
912 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
913 errmsg("bigint out of range")));
914 PG_RETURN_INT64(result);
918 int48mul(PG_FUNCTION_ARGS)
920 int32 arg1 = PG_GETARG_INT32(0);
921 int64 arg2 = PG_GETARG_INT64(1);
924 result = arg1 * arg2;
927 * Overflow check. We basically check to see if result / arg2 gives arg1
928 * again. There is one case where this fails: arg2 = 0 (which cannot
931 * Since the division is likely much more expensive than the actual
932 * multiplication, we'd like to skip it where possible. The best bang for
933 * the buck seems to be to check whether both inputs are in the int32
934 * range; if so, no overflow is possible.
936 if (arg2 != (int64) ((int32) arg2) &&
937 result / arg2 != arg1)
939 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
940 errmsg("bigint out of range")));
941 PG_RETURN_INT64(result);
945 int48div(PG_FUNCTION_ARGS)
947 int32 arg1 = PG_GETARG_INT32(0);
948 int64 arg2 = PG_GETARG_INT64(1);
953 (errcode(ERRCODE_DIVISION_BY_ZERO),
954 errmsg("division by zero")));
955 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
959 /* No overflow is possible */
960 PG_RETURN_INT64((int64) arg1 / arg2);
964 int82pl(PG_FUNCTION_ARGS)
966 int64 arg1 = PG_GETARG_INT64(0);
967 int16 arg2 = PG_GETARG_INT16(1);
970 result = arg1 + arg2;
973 * Overflow check. If the inputs are of different signs then their sum
974 * cannot overflow. If the inputs are of the same sign, their sum had
975 * better be that sign too.
977 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
979 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
980 errmsg("bigint out of range")));
981 PG_RETURN_INT64(result);
985 int82mi(PG_FUNCTION_ARGS)
987 int64 arg1 = PG_GETARG_INT64(0);
988 int16 arg2 = PG_GETARG_INT16(1);
991 result = arg1 - arg2;
994 * Overflow check. If the inputs are of the same sign then their
995 * difference cannot overflow. If they are of different signs then the
996 * result should be of the same sign as the first input.
998 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1000 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1001 errmsg("bigint out of range")));
1002 PG_RETURN_INT64(result);
1006 int82mul(PG_FUNCTION_ARGS)
1008 int64 arg1 = PG_GETARG_INT64(0);
1009 int16 arg2 = PG_GETARG_INT16(1);
1012 result = arg1 * arg2;
1015 * Overflow check. We basically check to see if result / arg1 gives arg2
1016 * again. There is one case where this fails: arg1 = 0 (which cannot
1019 * Since the division is likely much more expensive than the actual
1020 * multiplication, we'd like to skip it where possible. The best bang for
1021 * the buck seems to be to check whether both inputs are in the int32
1022 * range; if so, no overflow is possible.
1024 if (arg1 != (int64) ((int32) arg1) &&
1025 result / arg1 != arg2)
1027 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1028 errmsg("bigint out of range")));
1029 PG_RETURN_INT64(result);
1033 int82div(PG_FUNCTION_ARGS)
1035 int64 arg1 = PG_GETARG_INT64(0);
1036 int16 arg2 = PG_GETARG_INT16(1);
1042 (errcode(ERRCODE_DIVISION_BY_ZERO),
1043 errmsg("division by zero")));
1044 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1049 * INT64_MIN / -1 is problematic, since the result can't be represented on
1050 * a two's-complement machine. Some machines produce INT64_MIN, some
1051 * produce zero, some throw an exception. We can dodge the problem by
1052 * recognizing that division by -1 is the same as negation.
1057 /* overflow check (needed for INT64_MIN) */
1058 if (arg1 != 0 && SAMESIGN(result, arg1))
1060 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1061 errmsg("bigint out of range")));
1062 PG_RETURN_INT64(result);
1065 /* No overflow is possible */
1067 result = arg1 / arg2;
1069 PG_RETURN_INT64(result);
1073 int28pl(PG_FUNCTION_ARGS)
1075 int16 arg1 = PG_GETARG_INT16(0);
1076 int64 arg2 = PG_GETARG_INT64(1);
1079 result = arg1 + arg2;
1082 * Overflow check. If the inputs are of different signs then their sum
1083 * cannot overflow. If the inputs are of the same sign, their sum had
1084 * better be that sign too.
1086 if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1088 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1089 errmsg("bigint out of range")));
1090 PG_RETURN_INT64(result);
1094 int28mi(PG_FUNCTION_ARGS)
1096 int16 arg1 = PG_GETARG_INT16(0);
1097 int64 arg2 = PG_GETARG_INT64(1);
1100 result = arg1 - arg2;
1103 * Overflow check. If the inputs are of the same sign then their
1104 * difference cannot overflow. If they are of different signs then the
1105 * result should be of the same sign as the first input.
1107 if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
1109 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1110 errmsg("bigint out of range")));
1111 PG_RETURN_INT64(result);
1115 int28mul(PG_FUNCTION_ARGS)
1117 int16 arg1 = PG_GETARG_INT16(0);
1118 int64 arg2 = PG_GETARG_INT64(1);
1121 result = arg1 * arg2;
1124 * Overflow check. We basically check to see if result / arg2 gives arg1
1125 * again. There is one case where this fails: arg2 = 0 (which cannot
1128 * Since the division is likely much more expensive than the actual
1129 * multiplication, we'd like to skip it where possible. The best bang for
1130 * the buck seems to be to check whether both inputs are in the int32
1131 * range; if so, no overflow is possible.
1133 if (arg2 != (int64) ((int32) arg2) &&
1134 result / arg2 != arg1)
1136 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1137 errmsg("bigint out of range")));
1138 PG_RETURN_INT64(result);
1142 int28div(PG_FUNCTION_ARGS)
1144 int16 arg1 = PG_GETARG_INT16(0);
1145 int64 arg2 = PG_GETARG_INT64(1);
1150 (errcode(ERRCODE_DIVISION_BY_ZERO),
1151 errmsg("division by zero")));
1152 /* ensure compiler realizes we mustn't reach the division (gcc bug) */
1156 /* No overflow is possible */
1157 PG_RETURN_INT64((int64) arg1 / arg2);
1160 /* Binary arithmetics
1162 * int8and - returns arg1 & arg2
1163 * int8or - returns arg1 | arg2
1164 * int8xor - returns arg1 # arg2
1165 * int8not - returns ~arg1
1166 * int8shl - returns arg1 << arg2
1167 * int8shr - returns arg1 >> arg2
1171 int8and(PG_FUNCTION_ARGS)
1173 int64 arg1 = PG_GETARG_INT64(0);
1174 int64 arg2 = PG_GETARG_INT64(1);
1176 PG_RETURN_INT64(arg1 & arg2);
1180 int8or(PG_FUNCTION_ARGS)
1182 int64 arg1 = PG_GETARG_INT64(0);
1183 int64 arg2 = PG_GETARG_INT64(1);
1185 PG_RETURN_INT64(arg1 | arg2);
1189 int8xor(PG_FUNCTION_ARGS)
1191 int64 arg1 = PG_GETARG_INT64(0);
1192 int64 arg2 = PG_GETARG_INT64(1);
1194 PG_RETURN_INT64(arg1 ^ arg2);
1198 int8not(PG_FUNCTION_ARGS)
1200 int64 arg1 = PG_GETARG_INT64(0);
1202 PG_RETURN_INT64(~arg1);
1206 int8shl(PG_FUNCTION_ARGS)
1208 int64 arg1 = PG_GETARG_INT64(0);
1209 int32 arg2 = PG_GETARG_INT32(1);
1211 PG_RETURN_INT64(arg1 << arg2);
1215 int8shr(PG_FUNCTION_ARGS)
1217 int64 arg1 = PG_GETARG_INT64(0);
1218 int32 arg2 = PG_GETARG_INT32(1);
1220 PG_RETURN_INT64(arg1 >> arg2);
1223 /*----------------------------------------------------------
1224 * Conversion operators.
1225 *---------------------------------------------------------*/
1228 int48(PG_FUNCTION_ARGS)
1230 int32 arg = PG_GETARG_INT32(0);
1232 PG_RETURN_INT64((int64) arg);
1236 int84(PG_FUNCTION_ARGS)
1238 int64 arg = PG_GETARG_INT64(0);
1241 result = (int32) arg;
1243 /* Test for overflow by reverse-conversion. */
1244 if ((int64) result != arg)
1246 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1247 errmsg("integer out of range")));
1249 PG_RETURN_INT32(result);
1253 int28(PG_FUNCTION_ARGS)
1255 int16 arg = PG_GETARG_INT16(0);
1257 PG_RETURN_INT64((int64) arg);
1261 int82(PG_FUNCTION_ARGS)
1263 int64 arg = PG_GETARG_INT64(0);
1266 result = (int16) arg;
1268 /* Test for overflow by reverse-conversion. */
1269 if ((int64) result != arg)
1271 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1272 errmsg("smallint out of range")));
1274 PG_RETURN_INT16(result);
1278 i8tod(PG_FUNCTION_ARGS)
1280 int64 arg = PG_GETARG_INT64(0);
1285 PG_RETURN_FLOAT8(result);
1289 * Convert float8 to 8-byte integer.
1292 dtoi8(PG_FUNCTION_ARGS)
1294 float8 arg = PG_GETARG_FLOAT8(0);
1297 /* Round arg to nearest integer (but it's still in float form) */
1301 * Does it fit in an int64? Avoid assuming that we have handy constants
1302 * defined for the range boundaries, instead test for overflow by
1303 * reverse-conversion.
1305 result = (int64) arg;
1307 if ((float8) result != arg)
1309 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1310 errmsg("bigint out of range")));
1312 PG_RETURN_INT64(result);
1316 i8tof(PG_FUNCTION_ARGS)
1318 int64 arg = PG_GETARG_INT64(0);
1323 PG_RETURN_FLOAT4(result);
1327 * Convert float4 to 8-byte integer.
1330 ftoi8(PG_FUNCTION_ARGS)
1332 float4 arg = PG_GETARG_FLOAT4(0);
1336 /* Round arg to nearest integer (but it's still in float form) */
1340 * Does it fit in an int64? Avoid assuming that we have handy constants
1341 * defined for the range boundaries, instead test for overflow by
1342 * reverse-conversion.
1344 result = (int64) darg;
1346 if ((float8) result != darg)
1348 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1349 errmsg("bigint out of range")));
1351 PG_RETURN_INT64(result);
1355 i8tooid(PG_FUNCTION_ARGS)
1357 int64 arg = PG_GETARG_INT64(0);
1362 /* Test for overflow by reverse-conversion. */
1363 if ((int64) result != arg)
1365 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1366 errmsg("OID out of range")));
1368 PG_RETURN_OID(result);
1372 oidtoi8(PG_FUNCTION_ARGS)
1374 Oid arg = PG_GETARG_OID(0);
1376 PG_RETURN_INT64((int64) arg);
1380 * non-persistent numeric series generator
1383 generate_series_int8(PG_FUNCTION_ARGS)
1385 return generate_series_step_int8(fcinfo);
1389 generate_series_step_int8(PG_FUNCTION_ARGS)
1391 FuncCallContext *funcctx;
1392 generate_series_fctx *fctx;
1394 MemoryContext oldcontext;
1396 /* stuff done only on the first call of the function */
1397 if (SRF_IS_FIRSTCALL())
1399 int64 start = PG_GETARG_INT64(0);
1400 int64 finish = PG_GETARG_INT64(1);
1403 /* see if we were given an explicit step size */
1404 if (PG_NARGS() == 3)
1405 step = PG_GETARG_INT64(2);
1408 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1409 errmsg("step size cannot equal zero")));
1411 /* create a function context for cross-call persistence */
1412 funcctx = SRF_FIRSTCALL_INIT();
1415 * switch to memory context appropriate for multiple function calls
1417 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1419 /* allocate memory for user context */
1420 fctx = (generate_series_fctx *) palloc(sizeof(generate_series_fctx));
1423 * Use fctx to keep state from call to call. Seed current with the
1424 * original start value
1426 fctx->current = start;
1427 fctx->finish = finish;
1430 funcctx->user_fctx = fctx;
1431 MemoryContextSwitchTo(oldcontext);
1434 /* stuff done on every call of the function */
1435 funcctx = SRF_PERCALL_SETUP();
1438 * get the saved state and use current as the result for this iteration
1440 fctx = funcctx->user_fctx;
1441 result = fctx->current;
1443 if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1444 (fctx->step < 0 && fctx->current >= fctx->finish))
1446 /* increment current in preparation for next iteration */
1447 fctx->current += fctx->step;
1449 /* if next-value computation overflows, this is the final result */
1450 if (SAMESIGN(result, fctx->step) && !SAMESIGN(result, fctx->current))
1453 /* do when there is more left to send */
1454 SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
1457 /* do when there is no more left */
1458 SRF_RETURN_DONE(funcctx);