671
671
int32 arg2 = PG_GETARG_INT32(1);
677
* Win32 doesn't throw a catchable exception for SELECT -2147483648 *
680
if (arg2 == -1 && arg1 == INT_MIN)
682
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
683
errmsg("integer out of range")));
686
674
result = arg1 * arg2;
699
687
if (!(arg1 >= (int32) SHRT_MIN && arg1 <= (int32) SHRT_MAX &&
700
688
arg2 >= (int32) SHRT_MIN && arg2 <= (int32) SHRT_MAX) &&
702
(result / arg2 != arg1 || (arg2 == -1 && arg1 < 0 && result < 0)))
690
((arg2 == -1 && arg1 < 0 && result < 0) ||
691
result / arg2 != arg1))
704
693
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
705
694
errmsg("integer out of range")));
722
711
PG_RETURN_NULL();
728
* Win32 doesn't throw a catchable exception for SELECT -2147483648 /
715
* INT_MIN / -1 is problematic, since the result can't be represented on a
716
* two's-complement machine. Some machines produce INT_MIN, some produce
717
* zero, some throw an exception. We can dodge the problem by recognizing
718
* that division by -1 is the same as negation.
731
if (arg2 == -1 && arg1 == INT_MIN)
733
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
734
errmsg("integer out of range")));
723
/* overflow check (needed for INT_MIN) */
724
if (arg1 != 0 && SAMESIGN(result, arg1))
726
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
727
errmsg("integer out of range")));
728
PG_RETURN_INT32(result);
731
/* No overflow is possible */
737
733
result = arg1 / arg2;
740
* Overflow check. The only possible overflow case is for arg1 = INT_MIN,
741
* arg2 = -1, where the correct result is -INT_MIN, which can't be
742
* represented on a two's-complement machine. Most machines produce
743
* INT_MIN but it seems some produce zero.
745
if (arg2 == -1 && arg1 < 0 && result <= 0)
747
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
748
errmsg("integer out of range")));
749
735
PG_RETURN_INT32(result);
867
853
PG_RETURN_NULL();
870
result = arg1 / arg2;
873
* Overflow check. The only possible overflow case is for arg1 =
874
* SHRT_MIN, arg2 = -1, where the correct result is -SHRT_MIN, which can't
875
* be represented on a two's-complement machine. Most machines produce
876
* SHRT_MIN but it seems some produce zero.
857
* SHRT_MIN / -1 is problematic, since the result can't be represented on
858
* a two's-complement machine. Some machines produce SHRT_MIN, some
859
* produce zero, some throw an exception. We can dodge the problem by
860
* recognizing that division by -1 is the same as negation.
878
if (arg2 == -1 && arg1 < 0 && result <= 0)
880
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
881
errmsg("smallint out of range")));
865
/* overflow check (needed for SHRT_MIN) */
866
if (arg1 != 0 && SAMESIGN(result, arg1))
868
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
869
errmsg("smallint out of range")));
870
PG_RETURN_INT16(result);
873
/* No overflow is possible */
875
result = arg1 / arg2;
882
877
PG_RETURN_INT16(result);
1055
1050
PG_RETURN_NULL();
1058
result = arg1 / arg2;
1061
* Overflow check. The only possible overflow case is for arg1 = INT_MIN,
1062
* arg2 = -1, where the correct result is -INT_MIN, which can't be
1063
* represented on a two's-complement machine. Most machines produce
1064
* INT_MIN but it seems some produce zero.
1054
* INT_MIN / -1 is problematic, since the result can't be represented on a
1055
* two's-complement machine. Some machines produce INT_MIN, some produce
1056
* zero, some throw an exception. We can dodge the problem by recognizing
1057
* that division by -1 is the same as negation.
1066
if (arg2 == -1 && arg1 < 0 && result <= 0)
1068
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1069
errmsg("integer out of range")));
1062
/* overflow check (needed for INT_MIN) */
1063
if (arg1 != 0 && SAMESIGN(result, arg1))
1065
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1066
errmsg("integer out of range")));
1067
PG_RETURN_INT32(result);
1070
/* No overflow is possible */
1072
result = arg1 / arg2;
1070
1074
PG_RETURN_INT32(result);
1085
1089
PG_RETURN_NULL();
1088
/* SELECT ((-2147483648)::int4) % (-1); causes a floating point exception */
1089
if (arg1 == INT_MIN && arg2 == -1)
1093
* Some machines throw a floating-point exception for INT_MIN % -1, which
1094
* is a bit silly since the correct answer is perfectly well-defined,
1090
1098
PG_RETURN_INT32(0);
1092
1100
/* No overflow is possible */
1109
1117
PG_RETURN_NULL();
1121
* Some machines throw a floating-point exception for INT_MIN % -1, which
1122
* is a bit silly since the correct answer is perfectly well-defined,
1123
* namely zero. (It's not clear this ever happens when dealing with
1124
* int16, but we might as well have the test for safety.)
1112
1129
/* No overflow is possible */
1114
1131
PG_RETURN_INT16(arg1 % arg2);