]> granicus.if.org Git - postgresql/blobdiff - src/backend/utils/adt/int8.c
Remove all the special-case code for INT64_IS_BUSTED, per decision that
[postgresql] / src / backend / utils / adt / int8.c
index 3f749ad174ca216cfb742e02e6a23c5e51ba28de..6707b79e5485cc65b86ad7f29e57cebd34937b3a 100644 (file)
@@ -3,11 +3,11 @@
  * int8.c
  *       Internal 64-bit integer operations
  *
- * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/utils/adt/int8.c,v 1.67 2007/08/30 05:27:29 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/utils/adt/int8.c,v 1.77 2010/01/07 04:53:34 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -76,15 +76,12 @@ scanint8(const char *str, bool errorOK, int64 *result)
                 * Do an explicit check for INT64_MIN.  Ugly though this is, it's
                 * cleaner than trying to get the loop below to handle it portably.
                 */
-#ifndef INT64_IS_BUSTED
                if (strncmp(ptr, "9223372036854775808", 19) == 0)
                {
                        tmp = -INT64CONST(0x7fffffffffffffff) - 1;
                        ptr += 19;
                        goto gotdigits;
                }
-#endif
-
                sign = -1;
        }
        else if (*ptr == '+')
@@ -575,12 +572,9 @@ int8mul(PG_FUNCTION_ARGS)
         * Since the division is likely much more expensive than the actual
         * multiplication, we'd like to skip it where possible.  The best bang for
         * the buck seems to be to check whether both inputs are in the int32
-        * range; if so, no overflow is possible.  (But that only works if we
-        * really have a 64-bit int64 datatype...)
+        * range; if so, no overflow is possible.
         */
-#ifndef INT64_IS_BUSTED
        if (arg1 != (int64) ((int32) arg1) || arg2 != (int64) ((int32) arg2))
-#endif
        {
                if (arg2 != 0 &&
                        (result / arg2 != arg1 || (arg2 == -1 && arg1 < 0 && result < 0)))
@@ -608,9 +602,10 @@ int8div(PG_FUNCTION_ARGS)
        /*
         * Overflow check.      The only possible overflow case is for arg1 =
         * INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
-        * can't be represented on a two's-complement machine.
+        * can't be represented on a two's-complement machine.  Most machines
+        * produce INT64_MIN but it seems some produce zero.
         */
-       if (arg2 == -1 && arg1 < 0 && result < 0)
+       if (arg2 == -1 && arg1 < 0 && result <= 0)
                ereport(ERROR,
                                (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                                 errmsg("bigint out of range")));
@@ -657,17 +652,18 @@ int8mod(PG_FUNCTION_ARGS)
 Datum
 int8inc(PG_FUNCTION_ARGS)
 {
-       if (fcinfo->context && IsA(fcinfo->context, AggState))
+       /*
+        * When int8 is pass-by-reference, we provide this special case to avoid
+        * palloc overhead for COUNT(): when called from nodeAgg, we know that the
+        * argument is modifiable local storage, so just update it in-place. (If
+        * int8 is pass-by-value, then of course this is useless as well as
+        * incorrect, so just ifdef it out.)
+        */
+#ifndef USE_FLOAT8_BYVAL               /* controls int8 too */
+       if (fcinfo->context &&
+               (IsA(fcinfo->context, AggState) ||
+                IsA(fcinfo->context, WindowAggState)))
        {
-               /*
-                * Special case to avoid palloc overhead for COUNT(): when called from
-                * nodeAgg, we know that the argument is modifiable local storage, so
-                * just update it in-place.
-                *
-                * Note: this assumes int8 is a pass-by-ref type; if we ever support
-                * pass-by-val int8, this should be ifdef'd out when int8 is
-                * pass-by-val.
-                */
                int64      *arg = (int64 *) PG_GETARG_POINTER(0);
                int64           result;
 
@@ -682,6 +678,7 @@ int8inc(PG_FUNCTION_ARGS)
                PG_RETURN_POINTER(arg);
        }
        else
+#endif
        {
                /* Not called by nodeAgg, so just do it the dumb way */
                int64           arg = PG_GETARG_INT64(0);
@@ -830,9 +827,10 @@ int84div(PG_FUNCTION_ARGS)
        /*
         * Overflow check.      The only possible overflow case is for arg1 =
         * INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
-        * can't be represented on a two's-complement machine.
+        * can't be represented on a two's-complement machine.  Most machines
+        * produce INT64_MIN but it seems some produce zero.
         */
-       if (arg2 == -1 && arg1 < 0 && result < 0)
+       if (arg2 == -1 && arg1 < 0 && result <= 0)
                ereport(ERROR,
                                (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                                 errmsg("bigint out of range")));
@@ -914,10 +912,199 @@ int48div(PG_FUNCTION_ARGS)
        int32           arg1 = PG_GETARG_INT32(0);
        int64           arg2 = PG_GETARG_INT64(1);
 
+       if (arg2 == 0)
+       {
+               ereport(ERROR,
+                               (errcode(ERRCODE_DIVISION_BY_ZERO),
+                                errmsg("division by zero")));
+               /* ensure compiler realizes we mustn't reach the division (gcc bug) */
+               PG_RETURN_NULL();
+       }
+
+       /* No overflow is possible */
+       PG_RETURN_INT64((int64) arg1 / arg2);
+}
+
+Datum
+int82pl(PG_FUNCTION_ARGS)
+{
+       int64           arg1 = PG_GETARG_INT64(0);
+       int16           arg2 = PG_GETARG_INT16(1);
+       int64           result;
+
+       result = arg1 + arg2;
+
+       /*
+        * Overflow check.      If the inputs are of different signs then their sum
+        * cannot overflow.  If the inputs are of the same sign, their sum had
+        * better be that sign too.
+        */
+       if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
+               ereport(ERROR,
+                               (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+                                errmsg("bigint out of range")));
+       PG_RETURN_INT64(result);
+}
+
+Datum
+int82mi(PG_FUNCTION_ARGS)
+{
+       int64           arg1 = PG_GETARG_INT64(0);
+       int16           arg2 = PG_GETARG_INT16(1);
+       int64           result;
+
+       result = arg1 - arg2;
+
+       /*
+        * Overflow check.      If the inputs are of the same sign then their
+        * difference cannot overflow.  If they are of different signs then the
+        * result should be of the same sign as the first input.
+        */
+       if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
+               ereport(ERROR,
+                               (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+                                errmsg("bigint out of range")));
+       PG_RETURN_INT64(result);
+}
+
+Datum
+int82mul(PG_FUNCTION_ARGS)
+{
+       int64           arg1 = PG_GETARG_INT64(0);
+       int16           arg2 = PG_GETARG_INT16(1);
+       int64           result;
+
+       result = arg1 * arg2;
+
+       /*
+        * Overflow check.      We basically check to see if result / arg1 gives arg2
+        * again.  There is one case where this fails: arg1 = 0 (which cannot
+        * overflow).
+        *
+        * Since the division is likely much more expensive than the actual
+        * multiplication, we'd like to skip it where possible.  The best bang for
+        * the buck seems to be to check whether both inputs are in the int32
+        * range; if so, no overflow is possible.
+        */
+       if (arg1 != (int64) ((int32) arg1) &&
+               result / arg1 != arg2)
+               ereport(ERROR,
+                               (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+                                errmsg("bigint out of range")));
+       PG_RETURN_INT64(result);
+}
+
+Datum
+int82div(PG_FUNCTION_ARGS)
+{
+       int64           arg1 = PG_GETARG_INT64(0);
+       int16           arg2 = PG_GETARG_INT16(1);
+       int64           result;
+
        if (arg2 == 0)
                ereport(ERROR,
                                (errcode(ERRCODE_DIVISION_BY_ZERO),
                                 errmsg("division by zero")));
+
+       result = arg1 / arg2;
+
+       /*
+        * Overflow check.      The only possible overflow case is for arg1 =
+        * INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
+        * can't be represented on a two's-complement machine.  Most machines
+        * produce INT64_MIN but it seems some produce zero.
+        */
+       if (arg2 == -1 && arg1 < 0 && result <= 0)
+               ereport(ERROR,
+                               (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+                                errmsg("bigint out of range")));
+       PG_RETURN_INT64(result);
+}
+
+Datum
+int28pl(PG_FUNCTION_ARGS)
+{
+       int16           arg1 = PG_GETARG_INT16(0);
+       int64           arg2 = PG_GETARG_INT64(1);
+       int64           result;
+
+       result = arg1 + arg2;
+
+       /*
+        * Overflow check.      If the inputs are of different signs then their sum
+        * cannot overflow.  If the inputs are of the same sign, their sum had
+        * better be that sign too.
+        */
+       if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
+               ereport(ERROR,
+                               (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+                                errmsg("bigint out of range")));
+       PG_RETURN_INT64(result);
+}
+
+Datum
+int28mi(PG_FUNCTION_ARGS)
+{
+       int16           arg1 = PG_GETARG_INT16(0);
+       int64           arg2 = PG_GETARG_INT64(1);
+       int64           result;
+
+       result = arg1 - arg2;
+
+       /*
+        * Overflow check.      If the inputs are of the same sign then their
+        * difference cannot overflow.  If they are of different signs then the
+        * result should be of the same sign as the first input.
+        */
+       if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
+               ereport(ERROR,
+                               (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+                                errmsg("bigint out of range")));
+       PG_RETURN_INT64(result);
+}
+
+Datum
+int28mul(PG_FUNCTION_ARGS)
+{
+       int16           arg1 = PG_GETARG_INT16(0);
+       int64           arg2 = PG_GETARG_INT64(1);
+       int64           result;
+
+       result = arg1 * arg2;
+
+       /*
+        * Overflow check.      We basically check to see if result / arg2 gives arg1
+        * again.  There is one case where this fails: arg2 = 0 (which cannot
+        * overflow).
+        *
+        * Since the division is likely much more expensive than the actual
+        * multiplication, we'd like to skip it where possible.  The best bang for
+        * the buck seems to be to check whether both inputs are in the int32
+        * range; if so, no overflow is possible.
+        */
+       if (arg2 != (int64) ((int32) arg2) &&
+               result / arg2 != arg1)
+               ereport(ERROR,
+                               (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+                                errmsg("bigint out of range")));
+       PG_RETURN_INT64(result);
+}
+
+Datum
+int28div(PG_FUNCTION_ARGS)
+{
+       int16           arg1 = PG_GETARG_INT16(0);
+       int64           arg2 = PG_GETARG_INT64(1);
+
+       if (arg2 == 0)
+       {
+               ereport(ERROR,
+                               (errcode(ERRCODE_DIVISION_BY_ZERO),
+                                errmsg("division by zero")));
+               /* ensure compiler realizes we mustn't reach the division (gcc bug) */
+               PG_RETURN_NULL();
+       }
+
        /* No overflow is possible */
        PG_RETURN_INT64((int64) arg1 / arg2);
 }