aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/int.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/utils/adt/int.c')
-rw-r--r--src/backend/utils/adt/int.c148
1 files changed, 80 insertions, 68 deletions
diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c
index d35af1c913a..e41e584ffea 100644
--- a/src/backend/utils/adt/int.c
+++ b/src/backend/utils/adt/int.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/int.c,v 1.67 2005/07/10 21:36:21 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/int.c,v 1.68 2005/10/15 02:49:28 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -120,7 +120,7 @@ int2send(PG_FUNCTION_ARGS)
int2vector *
buildint2vector(const int2 *int2s, int n)
{
- int2vector *result;
+ int2vector *result;
result = (int2vector *) palloc0(Int2VectorSize(n));
@@ -128,8 +128,8 @@ buildint2vector(const int2 *int2s, int n)
memcpy(result->values, int2s, n * sizeof(int2));
/*
- * Attach standard array header. For historical reasons, we set the
- * index lower bound to 0 not 1.
+ * Attach standard array header. For historical reasons, we set the index
+ * lower bound to 0 not 1.
*/
result->size = Int2VectorSize(n);
result->ndim = 1;
@@ -212,7 +212,7 @@ Datum
int2vectorrecv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
- int2vector *result;
+ int2vector *result;
result = (int2vector *)
DatumGetPointer(DirectFunctionCall3(array_recv,
@@ -686,10 +686,11 @@ int4pl(PG_FUNCTION_ARGS)
int32 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.
+ * 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,
@@ -706,10 +707,11 @@ int4mi(PG_FUNCTION_ARGS)
int32 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.
+ * 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,
@@ -726,21 +728,22 @@ int4mul(PG_FUNCTION_ARGS)
int32 result;
result = arg1 * arg2;
+
/*
- * Overflow check. We basically check to see if result / arg2 gives
- * arg1 again. There are two cases where this fails: arg2 = 0 (which
- * cannot overflow) and arg1 = INT_MIN, arg2 = -1 (where the division
- * itself will overflow and thus incorrectly match).
+ * Overflow check. We basically check to see if result / arg2 gives arg1
+ * again. There are two cases where this fails: arg2 = 0 (which cannot
+ * overflow) and arg1 = INT_MIN, arg2 = -1 (where the division itself will
+ * overflow and thus incorrectly match).
*
* 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 int16 range; if so, no overflow is possible.
+ * 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 int16
+ * range; if so, no overflow is possible.
*/
if (!(arg1 >= (int32) SHRT_MIN && arg1 <= (int32) SHRT_MAX &&
arg2 >= (int32) SHRT_MIN && arg2 <= (int32) SHRT_MAX) &&
arg2 != 0 &&
- (result/arg2 != arg1 || (arg2 == -1 && arg1 < 0 && result < 0)))
+ (result / arg2 != arg1 || (arg2 == -1 && arg1 < 0 && result < 0)))
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("integer out of range")));
@@ -760,10 +763,11 @@ int4div(PG_FUNCTION_ARGS)
errmsg("division by zero")));
result = arg1 / arg2;
+
/*
- * Overflow check. The only possible overflow case is for
- * arg1 = INT_MIN, arg2 = -1, where the correct result is -INT_MIN,
- * which can't be represented on a two's-complement machine.
+ * Overflow check. The only possible overflow case is for arg1 = INT_MIN,
+ * arg2 = -1, where the correct result is -INT_MIN, which can't be
+ * represented on a two's-complement machine.
*/
if (arg2 == -1 && arg1 < 0 && result < 0)
ereport(ERROR,
@@ -819,10 +823,11 @@ int2pl(PG_FUNCTION_ARGS)
int16 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.
+ * 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,
@@ -839,10 +844,11 @@ int2mi(PG_FUNCTION_ARGS)
int16 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.
+ * 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,
@@ -859,11 +865,11 @@ int2mul(PG_FUNCTION_ARGS)
int32 result32;
/*
- * The most practical way to detect overflow is to do the arithmetic
- * in int32 (so that the result can't overflow) and then do a range
- * check.
+ * The most practical way to detect overflow is to do the arithmetic in
+ * int32 (so that the result can't overflow) and then do a range check.
*/
- result32 = (int32) arg1 * (int32) arg2;
+ result32 = (int32) arg1 *(int32) arg2;
+
if (result32 < SHRT_MIN || result32 > SHRT_MAX)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
@@ -885,10 +891,11 @@ int2div(PG_FUNCTION_ARGS)
errmsg("division by zero")));
result = arg1 / arg2;
+
/*
- * Overflow check. The only possible overflow case is for
- * arg1 = SHRT_MIN, arg2 = -1, where the correct result is -SHRT_MIN,
- * which can't be represented on a two's-complement machine.
+ * Overflow check. The only possible overflow case is for arg1 =
+ * SHRT_MIN, arg2 = -1, where the correct result is -SHRT_MIN, which can't
+ * be represented on a two's-complement machine.
*/
if (arg2 == -1 && arg1 < 0 && result < 0)
ereport(ERROR,
@@ -905,10 +912,11 @@ int24pl(PG_FUNCTION_ARGS)
int32 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.
+ * 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,
@@ -925,10 +933,11 @@ int24mi(PG_FUNCTION_ARGS)
int32 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.
+ * 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,
@@ -945,18 +954,19 @@ int24mul(PG_FUNCTION_ARGS)
int32 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).
+ * 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 int16 range; if so, no overflow is possible.
+ * 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 int16
+ * range; if so, no overflow is possible.
*/
if (!(arg2 >= (int32) SHRT_MIN && arg2 <= (int32) SHRT_MAX) &&
- result/arg2 != arg1)
+ result / arg2 != arg1)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("integer out of range")));
@@ -985,10 +995,11 @@ int42pl(PG_FUNCTION_ARGS)
int32 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.
+ * 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,
@@ -1005,10 +1016,11 @@ int42mi(PG_FUNCTION_ARGS)
int32 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.
+ * 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,
@@ -1025,18 +1037,19 @@ int42mul(PG_FUNCTION_ARGS)
int32 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).
+ * 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 int16 range; if so, no overflow is possible.
+ * 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 int16
+ * range; if so, no overflow is possible.
*/
if (!(arg1 >= (int32) SHRT_MIN && arg1 <= (int32) SHRT_MAX) &&
- result/arg1 != arg2)
+ result / arg1 != arg2)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("integer out of range")));
@@ -1056,10 +1069,11 @@ int42div(PG_FUNCTION_ARGS)
errmsg("division by zero")));
result = arg1 / arg2;
+
/*
- * Overflow check. The only possible overflow case is for
- * arg1 = INT_MIN, arg2 = -1, where the correct result is -INT_MIN,
- * which can't be represented on a two's-complement machine.
+ * Overflow check. The only possible overflow case is for arg1 = INT_MIN,
+ * arg2 = -1, where the correct result is -INT_MIN, which can't be
+ * represented on a two's-complement machine.
*/
if (arg2 == -1 && arg1 < 0 && result < 0)
ereport(ERROR,
@@ -1352,8 +1366,7 @@ generate_series_step_int4(PG_FUNCTION_ARGS)
funcctx = SRF_FIRSTCALL_INIT();
/*
- * switch to memory context appropriate for multiple function
- * calls
+ * switch to memory context appropriate for multiple function calls
*/
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
@@ -1376,8 +1389,7 @@ generate_series_step_int4(PG_FUNCTION_ARGS)
funcctx = SRF_PERCALL_SETUP();
/*
- * get the saved state and use current as the result for this
- * iteration
+ * get the saved state and use current as the result for this iteration
*/
fctx = funcctx->user_fctx;
result = fctx->current;