diff options
author | Dean Rasheed <dean.a.rasheed@gmail.com> | 2020-01-25 14:00:59 +0000 |
---|---|---|
committer | Dean Rasheed <dean.a.rasheed@gmail.com> | 2020-01-25 14:00:59 +0000 |
commit | 13661ddd7eaec7e2809ff5c29fc14653b6161036 (patch) | |
tree | 478835d5b14b5b5c304face1c351079495cdbb38 /src/backend | |
parent | 530609aa4263bee5b5ca205d83f0dbad098d0465 (diff) | |
download | postgresql-13661ddd7eaec7e2809ff5c29fc14653b6161036.tar.gz postgresql-13661ddd7eaec7e2809ff5c29fc14653b6161036.zip |
Add functions gcd() and lcm() for integer and numeric types.
These compute the greatest common divisor and least common multiple of
a pair of numbers using the Euclidean algorithm.
Vik Fearing, reviewed by Fabien Coelho.
Discussion: https://postgr.es/m/adbd3e0b-e3f1-5bbc-21db-03caf1cef0f7@2ndquadrant.com
Diffstat (limited to 'src/backend')
-rw-r--r-- | src/backend/utils/adt/int.c | 126 | ||||
-rw-r--r-- | src/backend/utils/adt/int8.c | 126 | ||||
-rw-r--r-- | src/backend/utils/adt/numeric.c | 171 |
3 files changed, 423 insertions, 0 deletions
diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index 583ce71e664..4acbc27d426 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -1196,6 +1196,132 @@ int2abs(PG_FUNCTION_ARGS) PG_RETURN_INT16(result); } +/* + * Greatest Common Divisor + * + * Returns the largest positive integer that exactly divides both inputs. + * Special cases: + * - gcd(x, 0) = gcd(0, x) = abs(x) + * because 0 is divisible by anything + * - gcd(0, 0) = 0 + * complies with the previous definition and is a common convention + * + * Special care must be taken if either input is INT_MIN --- gcd(0, INT_MIN), + * gcd(INT_MIN, 0) and gcd(INT_MIN, INT_MIN) are all equal to abs(INT_MIN), + * which cannot be represented as a 32-bit signed integer. + */ +static int32 +int4gcd_internal(int32 arg1, int32 arg2) +{ + int32 swap; + int32 a1, a2; + + /* + * Put the greater absolute value in arg1. + * + * This would happen automatically in the loop below, but avoids an + * expensive modulo operation, and simplifies the special-case handling + * for INT_MIN below. + * + * We do this in negative space in order to handle INT_MIN. + */ + a1 = (arg1 < 0) ? arg1 : -arg1; + a2 = (arg2 < 0) ? arg2 : -arg2; + if (a1 > a2) + { + swap = arg1; + arg1 = arg2; + arg2 = swap; + } + + /* Special care needs to be taken with INT_MIN. See comments above. */ + if (arg1 == PG_INT32_MIN) + { + if (arg2 == 0 || arg2 == PG_INT32_MIN) + ereport(ERROR, + (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), + errmsg("integer out of range"))); + + /* + * Some machines throw a floating-point exception for INT_MIN % -1, + * which is a bit silly since the correct answer is perfectly + * well-defined, namely zero. Guard against this and just return the + * result, gcd(INT_MIN, -1) = 1. + */ + if (arg2 == -1) + return 1; + } + + /* Use the Euclidean algorithm to find the GCD */ + while (arg2 != 0) + { + swap = arg2; + arg2 = arg1 % arg2; + arg1 = swap; + } + + /* + * Make sure the result is positive. (We know we don't have INT_MIN + * anymore). + */ + if (arg1 < 0) + arg1 = -arg1; + + return arg1; +} + +Datum +int4gcd(PG_FUNCTION_ARGS) +{ + int32 arg1 = PG_GETARG_INT32(0); + int32 arg2 = PG_GETARG_INT32(1); + int32 result; + + result = int4gcd_internal(arg1, arg2); + + PG_RETURN_INT32(result); +} + +/* + * Least Common Multiple + */ +Datum +int4lcm(PG_FUNCTION_ARGS) +{ + int32 arg1 = PG_GETARG_INT32(0); + int32 arg2 = PG_GETARG_INT32(1); + int32 gcd; + int32 result; + + /* + * Handle lcm(x, 0) = lcm(0, x) = 0 as a special case. This prevents a + * division-by-zero error below when x is zero, and an overflow error from + * the GCD computation when x = INT_MIN. + */ + if (arg1 == 0 || arg2 == 0) + PG_RETURN_INT32(0); + + /* lcm(x, y) = abs(x / gcd(x, y) * y) */ + gcd = int4gcd_internal(arg1, arg2); + arg1 = arg1 / gcd; + + if (unlikely(pg_mul_s32_overflow(arg1, arg2, &result))) + ereport(ERROR, + (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), + errmsg("integer out of range"))); + + /* If the result is INT_MIN, it cannot be represented. */ + if (unlikely(result == PG_INT32_MIN)) + ereport(ERROR, + (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), + errmsg("integer out of range"))); + + if (result < 0) + result = -result; + + PG_RETURN_INT32(result); +} + Datum int2larger(PG_FUNCTION_ARGS) { diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index fcdf77331e7..494768c1901 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -667,6 +667,132 @@ int8mod(PG_FUNCTION_ARGS) PG_RETURN_INT64(arg1 % arg2); } +/* + * Greatest Common Divisor + * + * Returns the largest positive integer that exactly divides both inputs. + * Special cases: + * - gcd(x, 0) = gcd(0, x) = abs(x) + * because 0 is divisible by anything + * - gcd(0, 0) = 0 + * complies with the previous definition and is a common convention + * + * Special care must be taken if either input is INT64_MIN --- + * gcd(0, INT64_MIN), gcd(INT64_MIN, 0) and gcd(INT64_MIN, INT64_MIN) are + * all equal to abs(INT64_MIN), which cannot be represented as a 64-bit signed + * integer. + */ +static int64 +int8gcd_internal(int64 arg1, int64 arg2) +{ + int64 swap; + int64 a1, a2; + + /* + * Put the greater absolute value in arg1. + * + * This would happen automatically in the loop below, but avoids an + * expensive modulo operation, and simplifies the special-case handling + * for INT64_MIN below. + * + * We do this in negative space in order to handle INT64_MIN. + */ + a1 = (arg1 < 0) ? arg1 : -arg1; + a2 = (arg2 < 0) ? arg2 : -arg2; + if (a1 > a2) + { + swap = arg1; + arg1 = arg2; + arg2 = swap; + } + + /* Special care needs to be taken with INT64_MIN. See comments above. */ + if (arg1 == PG_INT64_MIN) + { + if (arg2 == 0 || arg2 == PG_INT64_MIN) + ereport(ERROR, + (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), + errmsg("bigint out of range"))); + + /* + * Some machines throw a floating-point exception for INT64_MIN % -1, + * which is a bit silly since the correct answer is perfectly + * well-defined, namely zero. Guard against this and just return the + * result, gcd(INT64_MIN, -1) = 1. + */ + if (arg2 == -1) + return 1; + } + + /* Use the Euclidean algorithm to find the GCD */ + while (arg2 != 0) + { + swap = arg2; + arg2 = arg1 % arg2; + arg1 = swap; + } + + /* + * Make sure the result is positive. (We know we don't have INT64_MIN + * anymore). + */ + if (arg1 < 0) + arg1 = -arg1; + + return arg1; +} + +Datum +int8gcd(PG_FUNCTION_ARGS) +{ + int64 arg1 = PG_GETARG_INT64(0); + int64 arg2 = PG_GETARG_INT64(1); + int64 result; + + result = int8gcd_internal(arg1, arg2); + + PG_RETURN_INT64(result); +} + +/* + * Least Common Multiple + */ +Datum +int8lcm(PG_FUNCTION_ARGS) +{ + int64 arg1 = PG_GETARG_INT64(0); + int64 arg2 = PG_GETARG_INT64(1); + int64 gcd; + int64 result; + + /* + * Handle lcm(x, 0) = lcm(0, x) = 0 as a special case. This prevents a + * division-by-zero error below when x is zero, and an overflow error from + * the GCD computation when x = INT64_MIN. + */ + if (arg1 == 0 || arg2 == 0) + PG_RETURN_INT64(0); + + /* lcm(x, y) = abs(x / gcd(x, y) * y) */ + gcd = int8gcd_internal(arg1, arg2); + arg1 = arg1 / gcd; + + if (unlikely(pg_mul_s64_overflow(arg1, arg2, &result))) + ereport(ERROR, + (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), + errmsg("bigint out of range"))); + + /* If the result is INT64_MIN, it cannot be represented. */ + if (unlikely(result == PG_INT64_MIN)) + ereport(ERROR, + (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), + errmsg("bigint out of range"))); + + if (result < 0) + result = -result; + + PG_RETURN_INT64(result); +} Datum int8inc(PG_FUNCTION_ARGS) diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index 76a597e56fa..c92ad5a4fe0 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -521,6 +521,8 @@ static void mod_var(const NumericVar *var1, const NumericVar *var2, static void ceil_var(const NumericVar *var, NumericVar *result); static void floor_var(const NumericVar *var, NumericVar *result); +static void gcd_var(const NumericVar *var1, const NumericVar *var2, + NumericVar *result); static void sqrt_var(const NumericVar *arg, NumericVar *result, int rscale); static void exp_var(const NumericVar *arg, NumericVar *result, int rscale); static int estimate_ln_dweight(const NumericVar *var); @@ -2839,6 +2841,107 @@ numeric_larger(PG_FUNCTION_ARGS) */ /* + * numeric_gcd() - + * + * Calculate the greatest common divisor of two numerics + */ +Datum +numeric_gcd(PG_FUNCTION_ARGS) +{ + Numeric num1 = PG_GETARG_NUMERIC(0); + Numeric num2 = PG_GETARG_NUMERIC(1); + NumericVar arg1; + NumericVar arg2; + NumericVar result; + Numeric res; + + /* + * Handle NaN + */ + if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2)) + PG_RETURN_NUMERIC(make_result(&const_nan)); + + /* + * Unpack the arguments + */ + init_var_from_num(num1, &arg1); + init_var_from_num(num2, &arg2); + + init_var(&result); + + /* + * Find the GCD and return the result + */ + gcd_var(&arg1, &arg2, &result); + + res = make_result(&result); + + free_var(&result); + + PG_RETURN_NUMERIC(res); +} + + +/* + * numeric_lcm() - + * + * Calculate the least common multiple of two numerics + */ +Datum +numeric_lcm(PG_FUNCTION_ARGS) +{ + Numeric num1 = PG_GETARG_NUMERIC(0); + Numeric num2 = PG_GETARG_NUMERIC(1); + NumericVar arg1; + NumericVar arg2; + NumericVar result; + Numeric res; + + /* + * Handle NaN + */ + if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2)) + PG_RETURN_NUMERIC(make_result(&const_nan)); + + /* + * Unpack the arguments + */ + init_var_from_num(num1, &arg1); + init_var_from_num(num2, &arg2); + + init_var(&result); + + /* + * Compute the result using lcm(x, y) = abs(x / gcd(x, y) * y), returning + * zero if either input is zero. + * + * Note that the division is guaranteed to be exact, returning an integer + * result, so the LCM is an integral multiple of both x and y. A display + * scale of Min(x.dscale, y.dscale) would be sufficient to represent it, + * but as with other numeric functions, we choose to return a result whose + * display scale is no smaller than either input. + */ + if (arg1.ndigits == 0 || arg2.ndigits == 0) + set_var_from_var(&const_zero, &result); + else + { + gcd_var(&arg1, &arg2, &result); + div_var(&arg1, &result, &result, 0, false); + mul_var(&arg2, &result, &result, arg2.dscale); + result.sign = NUMERIC_POS; + } + + result.dscale = Max(arg1.dscale, arg2.dscale); + + res = make_result(&result); + + free_var(&result); + + PG_RETURN_NUMERIC(res); +} + + +/* * numeric_fac() * * Compute factorial @@ -8040,6 +8143,74 @@ floor_var(const NumericVar *var, NumericVar *result) /* + * gcd_var() - + * + * Calculate the greatest common divisor of two numerics at variable level + */ +static void +gcd_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result) +{ + int res_dscale; + int cmp; + NumericVar tmp_arg; + NumericVar mod; + + res_dscale = Max(var1->dscale, var2->dscale); + + /* + * Arrange for var1 to be the number with the greater absolute value. + * + * This would happen automatically in the loop below, but avoids an + * expensive modulo operation. + */ + cmp = cmp_abs(var1, var2); + if (cmp < 0) + { + const NumericVar *tmp = var1; + + var1 = var2; + var2 = tmp; + } + + /* + * Also avoid the taking the modulo if the inputs have the same absolute + * value, or if the smaller input is zero. + */ + if (cmp == 0 || var2->ndigits == 0) + { + set_var_from_var(var1, result); + result->sign = NUMERIC_POS; + result->dscale = res_dscale; + return; + } + + init_var(&tmp_arg); + init_var(&mod); + + /* Use the Euclidean algorithm to find the GCD */ + set_var_from_var(var1, &tmp_arg); + set_var_from_var(var2, result); + + for (;;) + { + /* this loop can take a while, so allow it to be interrupted */ + CHECK_FOR_INTERRUPTS(); + + mod_var(&tmp_arg, result, &mod); + if (mod.ndigits == 0) + break; + set_var_from_var(result, &tmp_arg); + set_var_from_var(&mod, result); + } + result->sign = NUMERIC_POS; + result->dscale = res_dscale; + + free_var(&tmp_arg); + free_var(&mod); +} + + +/* * sqrt_var() - * * Compute the square root of x using Newton's algorithm |