diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c new file mode 100644 index 5510a20..9e50ea7 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -551,6 +551,8 @@ static void sub_var(const NumericVar *va static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale); +static void mul_var_int(const NumericVar *var, int ival, int ival_weight, + NumericVar *result, int rscale); static void div_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round); @@ -8707,7 +8709,7 @@ mul_var(const NumericVar *var1, const Nu var1digits = var1->digits; var2digits = var2->digits; - if (var1ndigits == 0 || var2ndigits == 0) + if (var1ndigits == 0) { /* one or both inputs is zero; so is result */ zero_var(result); @@ -8715,6 +8717,31 @@ mul_var(const NumericVar *var1, const Nu return; } + /* + * If var1 has just one or two digits, delegate to mul_var_int(), which + * uses a faster direct multiplication algorithm. + * + * TODO: Similarly, on platforms with 128-bit integers ... + */ + if (var1ndigits <= 2) + { + int ifactor; + int ifactor_weight; + + ifactor = var1->digits[0]; + ifactor_weight = var1->weight; + if (var1ndigits == 2) + { + ifactor = ifactor * NBASE + var1->digits[1]; + ifactor_weight--; + } + if (var1->sign == NUMERIC_NEG) + ifactor = -ifactor; + + mul_var_int(var2, ifactor, ifactor_weight, result, rscale); + return; + } + /* Determine result sign and (maximum possible) weight */ if (var1->sign == var2->sign) res_sign = NUMERIC_POS; @@ -8857,6 +8884,123 @@ mul_var(const NumericVar *var1, const Nu result->weight = res_weight; result->sign = res_sign; + /* Round to target rscale (and set result->dscale) */ + round_var(result, rscale); + + /* Strip leading and trailing zeroes */ + strip_var(result); +} + + +/* + * mul_var_int() - + * + * Multiply a numeric variable by a 32-bit integer with the specified weight. + * The product var * ival * NBASE^ival_weight is stored in result. + */ +static void +mul_var_int(const NumericVar *var, int ival, int ival_weight, + NumericVar *result, int rscale) +{ + NumericDigit *var_digits = var->digits; + int var_ndigits = var->ndigits; + int res_sign; + int res_weight; + int res_ndigits; + int maxdigits; + NumericDigit *res_buf; + NumericDigit *res_digits; + uint32 factor; + uint32 carry; + + if (ival == 0 || var_ndigits == 0) + { + zero_var(result); + result->dscale = rscale; + return; + } + + /* + * Determine the result sign, (maximum possible) weight and number of + * digits to calculate. The weight figured here is correct if the emitted + * product has no leading zero digits; otherwise strip_var() will fix + * things up. + */ + if (var->sign == NUMERIC_POS) + res_sign = ival > 0 ? NUMERIC_POS : NUMERIC_NEG; + else + res_sign = ival > 0 ? NUMERIC_NEG : NUMERIC_POS; + res_weight = var->weight + ival_weight + 3; + /* The number of accurate result digits we need to produce: */ + res_ndigits = var_ndigits + 3; + maxdigits = res_weight + 1 + (rscale + DEC_DIGITS - 1) / DEC_DIGITS + + MUL_GUARD_DIGITS; + res_ndigits = Min(res_ndigits, maxdigits); + + if (res_ndigits < 3) + { + /* All input digits will be ignored; so result is zero */ + zero_var(result); + result->dscale = rscale; + return; + } + + res_buf = digitbuf_alloc(res_ndigits + 1); + res_buf[0] = 0; /* spare digit for later rounding */ + res_digits = res_buf + 1; + + /* + * Now compute the product digits by procssing the input digits in reverse + * and propagating the carry up as we go. + * + * In this algorithm, the carry from one digit to the next is at most + * factor - 1, and product is at most factor * NBASE - 1, and so it needs + * to be a 64-bit integer if this exceeds UINT_MAX. + */ + factor = abs(ival); + carry = 0; + + if (factor <= UINT_MAX / NBASE) + { + /* product cannot overflow 32 bits */ + uint32 product; + + for (int i = res_ndigits - 4; i >= 0; i--) + { + product = factor * var_digits[i] + carry; + res_digits[i + 3] = (NumericDigit) (product % NBASE); + carry = product / NBASE; + } + res_digits[2] = (NumericDigit) (carry % NBASE); + carry = carry / NBASE; + res_digits[1] = (NumericDigit) (carry % NBASE); + res_digits[0] = (NumericDigit) (carry / NBASE); + } + else + { + /* product may exceed 32 bits */ + uint64 product; + + for (int i = res_ndigits - 4; i >= 0; i--) + { + product = (uint64) factor * var_digits[i] + carry; + res_digits[i + 3] = (NumericDigit) (product % NBASE); + carry = (uint32) (product / NBASE); + } + res_digits[2] = (NumericDigit) (carry % NBASE); + carry = carry / NBASE; + res_digits[1] = (NumericDigit) (carry % NBASE); + res_digits[0] = (NumericDigit) (carry / NBASE); + } + + /* Store the product in result */ + digitbuf_free(result->buf); + result->ndigits = res_ndigits; + result->buf = res_buf; + result->digits = res_digits; + result->weight = res_weight; + result->sign = res_sign; + /* Round to target rscale (and set result->dscale) */ round_var(result, rscale);