PostgreSQL Source Code  git master
numeric.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include "catalog/pg_type.h"
#include "common/hashfn.h"
#include "common/int.h"
#include "funcapi.h"
#include "lib/hyperloglog.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/int8.h"
#include "utils/numeric.h"
#include "utils/pg_lsn.h"
#include "utils/sortsupport.h"
Include dependency graph for numeric.c:

Go to the source code of this file.

Data Structures

struct  NumericShort
 
struct  NumericLong
 
union  NumericChoice
 
struct  NumericData
 
struct  NumericVar
 
struct  generate_series_numeric_fctx
 
struct  NumericSortSupport
 
struct  NumericSumAccum
 
struct  NumericAggState
 
struct  Int8TransTypeData
 

Macros

#define NBASE   10000
 
#define HALF_NBASE   5000
 
#define DEC_DIGITS   4 /* decimal digits per NBASE digit */
 
#define MUL_GUARD_DIGITS   2 /* these are measured in NBASE digits */
 
#define DIV_GUARD_DIGITS   4
 
#define NUMERIC_SIGN_MASK   0xC000
 
#define NUMERIC_POS   0x0000
 
#define NUMERIC_NEG   0x4000
 
#define NUMERIC_SHORT   0x8000
 
#define NUMERIC_SPECIAL   0xC000
 
#define NUMERIC_FLAGBITS(n)   ((n)->choice.n_header & NUMERIC_SIGN_MASK)
 
#define NUMERIC_IS_SHORT(n)   (NUMERIC_FLAGBITS(n) == NUMERIC_SHORT)
 
#define NUMERIC_IS_SPECIAL(n)   (NUMERIC_FLAGBITS(n) == NUMERIC_SPECIAL)
 
#define NUMERIC_HDRSZ   (VARHDRSZ + sizeof(uint16) + sizeof(int16))
 
#define NUMERIC_HDRSZ_SHORT   (VARHDRSZ + sizeof(uint16))
 
#define NUMERIC_HEADER_IS_SHORT(n)   (((n)->choice.n_header & 0x8000) != 0)
 
#define NUMERIC_HEADER_SIZE(n)
 
#define NUMERIC_EXT_SIGN_MASK   0xF000 /* high bits plus NaN/Inf flag bits */
 
#define NUMERIC_NAN   0xC000
 
#define NUMERIC_PINF   0xD000
 
#define NUMERIC_NINF   0xF000
 
#define NUMERIC_INF_SIGN_MASK   0x2000
 
#define NUMERIC_EXT_FLAGBITS(n)   ((n)->choice.n_header & NUMERIC_EXT_SIGN_MASK)
 
#define NUMERIC_IS_NAN(n)   ((n)->choice.n_header == NUMERIC_NAN)
 
#define NUMERIC_IS_PINF(n)   ((n)->choice.n_header == NUMERIC_PINF)
 
#define NUMERIC_IS_NINF(n)   ((n)->choice.n_header == NUMERIC_NINF)
 
#define NUMERIC_IS_INF(n)   (((n)->choice.n_header & ~NUMERIC_INF_SIGN_MASK) == NUMERIC_PINF)
 
#define NUMERIC_SHORT_SIGN_MASK   0x2000
 
#define NUMERIC_SHORT_DSCALE_MASK   0x1F80
 
#define NUMERIC_SHORT_DSCALE_SHIFT   7
 
#define NUMERIC_SHORT_DSCALE_MAX   (NUMERIC_SHORT_DSCALE_MASK >> NUMERIC_SHORT_DSCALE_SHIFT)
 
#define NUMERIC_SHORT_WEIGHT_SIGN_MASK   0x0040
 
#define NUMERIC_SHORT_WEIGHT_MASK   0x003F
 
#define NUMERIC_SHORT_WEIGHT_MAX   NUMERIC_SHORT_WEIGHT_MASK
 
#define NUMERIC_SHORT_WEIGHT_MIN   (-(NUMERIC_SHORT_WEIGHT_MASK+1))
 
#define NUMERIC_DSCALE_MASK   0x3FFF
 
#define NUMERIC_DSCALE_MAX   NUMERIC_DSCALE_MASK
 
#define NUMERIC_SIGN(n)
 
#define NUMERIC_DSCALE(n)
 
#define NUMERIC_WEIGHT(n)
 
#define NUMERIC_ABBREV_BITS   (SIZEOF_DATUM * BITS_PER_BYTE)
 
#define NumericAbbrevGetDatum(X)   ((Datum) (X))
 
#define DatumGetNumericAbbrev(X)   ((int32) (X))
 
#define NUMERIC_ABBREV_NAN   NumericAbbrevGetDatum(PG_INT32_MIN)
 
#define NUMERIC_ABBREV_PINF   NumericAbbrevGetDatum(-PG_INT32_MAX)
 
#define NUMERIC_ABBREV_NINF   NumericAbbrevGetDatum(PG_INT32_MAX)
 
#define dump_numeric(s, n)
 
#define dump_var(s, v)
 
#define digitbuf_alloc(ndigits)   ((NumericDigit *) palloc((ndigits) * sizeof(NumericDigit)))
 
#define digitbuf_free(buf)
 
#define init_var(v)   memset(v, 0, sizeof(NumericVar))
 
#define NUMERIC_DIGITS(num)
 
#define NUMERIC_NDIGITS(num)   ((VARSIZE(num) - NUMERIC_HEADER_SIZE(num)) / sizeof(NumericDigit))
 
#define NUMERIC_CAN_BE_SHORT(scale, weight)
 
#define NA_TOTAL_COUNT(na)   ((na)->N + (na)->NaNcount + (na)->pInfcount + (na)->nInfcount)
 
#define makePolyNumAggState   makeNumericAggState
 
#define makePolyNumAggStateCurrentContext   makeNumericAggStateCurrentContext
 

Typedefs

typedef int16 NumericDigit
 
typedef struct NumericVar NumericVar
 
typedef struct NumericSumAccum NumericSumAccum
 
typedef struct NumericAggState NumericAggState
 
typedef NumericAggState PolyNumAggState
 
typedef struct Int8TransTypeData Int8TransTypeData
 

Functions

static void alloc_var (NumericVar *var, int ndigits)
 
static void free_var (NumericVar *var)
 
static void zero_var (NumericVar *var)
 
static const char * set_var_from_str (const char *str, const char *cp, NumericVar *dest)
 
static void set_var_from_num (Numeric value, NumericVar *dest)
 
static void init_var_from_num (Numeric num, NumericVar *dest)
 
static void set_var_from_var (const NumericVar *value, NumericVar *dest)
 
static char * get_str_from_var (const NumericVar *var)
 
static char * get_str_from_var_sci (const NumericVar *var, int rscale)
 
static void numericvar_serialize (StringInfo buf, const NumericVar *var)
 
static void numericvar_deserialize (StringInfo buf, NumericVar *var)
 
static Numeric duplicate_numeric (Numeric num)
 
static Numeric make_result (const NumericVar *var)
 
static Numeric make_result_opt_error (const NumericVar *var, bool *error)
 
static void apply_typmod (NumericVar *var, int32 typmod)
 
static void apply_typmod_special (Numeric num, int32 typmod)
 
static bool numericvar_to_int32 (const NumericVar *var, int32 *result)
 
static bool numericvar_to_int64 (const NumericVar *var, int64 *result)
 
static void int64_to_numericvar (int64 val, NumericVar *var)
 
static bool numericvar_to_uint64 (const NumericVar *var, uint64 *result)
 
static double numericvar_to_double_no_overflow (const NumericVar *var)
 
static Datum numeric_abbrev_convert (Datum original_datum, SortSupport ssup)
 
static bool numeric_abbrev_abort (int memtupcount, SortSupport ssup)
 
static int numeric_fast_cmp (Datum x, Datum y, SortSupport ssup)
 
static int numeric_cmp_abbrev (Datum x, Datum y, SortSupport ssup)
 
static Datum numeric_abbrev_convert_var (const NumericVar *var, NumericSortSupport *nss)
 
static int cmp_numerics (Numeric num1, Numeric num2)
 
static int cmp_var (const NumericVar *var1, const NumericVar *var2)
 
static int cmp_var_common (const NumericDigit *var1digits, int var1ndigits, int var1weight, int var1sign, const NumericDigit *var2digits, int var2ndigits, int var2weight, int var2sign)
 
static void add_var (const NumericVar *var1, const NumericVar *var2, NumericVar *result)
 
static void sub_var (const NumericVar *var1, const NumericVar *var2, NumericVar *result)
 
static void mul_var (const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
 
static void div_var (const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
 
static void div_var_fast (const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
 
static int select_div_scale (const NumericVar *var1, const NumericVar *var2)
 
static void mod_var (const NumericVar *var1, const NumericVar *var2, NumericVar *result)
 
static void div_mod_var (const NumericVar *var1, const NumericVar *var2, NumericVar *quot, NumericVar *rem)
 
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)
 
static void ln_var (const NumericVar *arg, NumericVar *result, int rscale)
 
static void log_var (const NumericVar *base, const NumericVar *num, NumericVar *result)
 
static void power_var (const NumericVar *base, const NumericVar *exp, NumericVar *result)
 
static void power_var_int (const NumericVar *base, int exp, NumericVar *result, int rscale)
 
static int cmp_abs (const NumericVar *var1, const NumericVar *var2)
 
static int cmp_abs_common (const NumericDigit *var1digits, int var1ndigits, int var1weight, const NumericDigit *var2digits, int var2ndigits, int var2weight)
 
static void add_abs (const NumericVar *var1, const NumericVar *var2, NumericVar *result)
 
static void sub_abs (const NumericVar *var1, const NumericVar *var2, NumericVar *result)
 
static void round_var (NumericVar *var, int rscale)
 
static void trunc_var (NumericVar *var, int rscale)
 
static void strip_var (NumericVar *var)
 
static void compute_bucket (Numeric operand, Numeric bound1, Numeric bound2, const NumericVar *count_var, bool reversed_bounds, NumericVar *result_var)
 
static void accum_sum_add (NumericSumAccum *accum, const NumericVar *var1)
 
static void accum_sum_rescale (NumericSumAccum *accum, const NumericVar *val)
 
static void accum_sum_carry (NumericSumAccum *accum)
 
static void accum_sum_reset (NumericSumAccum *accum)
 
static void accum_sum_final (NumericSumAccum *accum, NumericVar *result)
 
static void accum_sum_copy (NumericSumAccum *dst, NumericSumAccum *src)
 
static void accum_sum_combine (NumericSumAccum *accum, NumericSumAccum *accum2)
 
Datum numeric_in (PG_FUNCTION_ARGS)
 
Datum numeric_out (PG_FUNCTION_ARGS)
 
bool numeric_is_nan (Numeric num)
 
bool numeric_is_inf (Numeric num)
 
static bool numeric_is_integral (Numeric num)
 
static int32 make_numeric_typmod (int precision, int scale)
 
static bool is_valid_numeric_typmod (int32 typmod)
 
static int numeric_typmod_precision (int32 typmod)
 
static int numeric_typmod_scale (int32 typmod)
 
int32 numeric_maximum_size (int32 typmod)
 
char * numeric_out_sci (Numeric num, int scale)
 
char * numeric_normalize (Numeric num)
 
Datum numeric_recv (PG_FUNCTION_ARGS)
 
Datum numeric_send (PG_FUNCTION_ARGS)
 
Datum numeric_support (PG_FUNCTION_ARGS)
 
Datum numeric (PG_FUNCTION_ARGS)
 
Datum numerictypmodin (PG_FUNCTION_ARGS)
 
Datum numerictypmodout (PG_FUNCTION_ARGS)
 
Datum numeric_abs (PG_FUNCTION_ARGS)
 
Datum numeric_uminus (PG_FUNCTION_ARGS)
 
Datum numeric_uplus (PG_FUNCTION_ARGS)
 
static int numeric_sign_internal (Numeric num)
 
Datum numeric_sign (PG_FUNCTION_ARGS)
 
Datum numeric_round (PG_FUNCTION_ARGS)
 
Datum numeric_trunc (PG_FUNCTION_ARGS)
 
Datum numeric_ceil (PG_FUNCTION_ARGS)
 
Datum numeric_floor (PG_FUNCTION_ARGS)
 
Datum generate_series_numeric (PG_FUNCTION_ARGS)
 
Datum generate_series_step_numeric (PG_FUNCTION_ARGS)
 
Datum width_bucket_numeric (PG_FUNCTION_ARGS)
 
Datum numeric_sortsupport (PG_FUNCTION_ARGS)
 
Datum numeric_cmp (PG_FUNCTION_ARGS)
 
Datum numeric_eq (PG_FUNCTION_ARGS)
 
Datum numeric_ne (PG_FUNCTION_ARGS)
 
Datum numeric_gt (PG_FUNCTION_ARGS)
 
Datum numeric_ge (PG_FUNCTION_ARGS)
 
Datum numeric_lt (PG_FUNCTION_ARGS)
 
Datum numeric_le (PG_FUNCTION_ARGS)
 
Datum in_range_numeric_numeric (PG_FUNCTION_ARGS)
 
Datum hash_numeric (PG_FUNCTION_ARGS)
 
Datum hash_numeric_extended (PG_FUNCTION_ARGS)
 
Datum numeric_add (PG_FUNCTION_ARGS)
 
Numeric numeric_add_opt_error (Numeric num1, Numeric num2, bool *have_error)
 
Datum numeric_sub (PG_FUNCTION_ARGS)
 
Numeric numeric_sub_opt_error (Numeric num1, Numeric num2, bool *have_error)
 
Datum numeric_mul (PG_FUNCTION_ARGS)
 
Numeric numeric_mul_opt_error (Numeric num1, Numeric num2, bool *have_error)
 
Datum numeric_div (PG_FUNCTION_ARGS)
 
Numeric numeric_div_opt_error (Numeric num1, Numeric num2, bool *have_error)
 
Datum numeric_div_trunc (PG_FUNCTION_ARGS)
 
Datum numeric_mod (PG_FUNCTION_ARGS)
 
Numeric numeric_mod_opt_error (Numeric num1, Numeric num2, bool *have_error)
 
Datum numeric_inc (PG_FUNCTION_ARGS)
 
Datum numeric_smaller (PG_FUNCTION_ARGS)
 
Datum numeric_larger (PG_FUNCTION_ARGS)
 
Datum numeric_gcd (PG_FUNCTION_ARGS)
 
Datum numeric_lcm (PG_FUNCTION_ARGS)
 
Datum numeric_fac (PG_FUNCTION_ARGS)
 
Datum numeric_sqrt (PG_FUNCTION_ARGS)
 
Datum numeric_exp (PG_FUNCTION_ARGS)
 
Datum numeric_ln (PG_FUNCTION_ARGS)
 
Datum numeric_log (PG_FUNCTION_ARGS)
 
Datum numeric_power (PG_FUNCTION_ARGS)
 
Datum numeric_scale (PG_FUNCTION_ARGS)
 
static int get_min_scale (NumericVar *var)
 
Datum numeric_min_scale (PG_FUNCTION_ARGS)
 
Datum numeric_trim_scale (PG_FUNCTION_ARGS)
 
Numeric int64_to_numeric (int64 val)
 
Numeric int64_div_fast_to_numeric (int64 val1, int log10val2)
 
Datum int4_numeric (PG_FUNCTION_ARGS)
 
int32 numeric_int4_opt_error (Numeric num, bool *have_error)
 
Datum numeric_int4 (PG_FUNCTION_ARGS)
 
Datum int8_numeric (PG_FUNCTION_ARGS)
 
Datum numeric_int8 (PG_FUNCTION_ARGS)
 
Datum int2_numeric (PG_FUNCTION_ARGS)
 
Datum numeric_int2 (PG_FUNCTION_ARGS)
 
Datum float8_numeric (PG_FUNCTION_ARGS)
 
Datum numeric_float8 (PG_FUNCTION_ARGS)
 
Datum numeric_float8_no_overflow (PG_FUNCTION_ARGS)
 
Datum float4_numeric (PG_FUNCTION_ARGS)
 
Datum numeric_float4 (PG_FUNCTION_ARGS)
 
Datum numeric_pg_lsn (PG_FUNCTION_ARGS)
 
static NumericAggStatemakeNumericAggState (FunctionCallInfo fcinfo, bool calcSumX2)
 
static NumericAggStatemakeNumericAggStateCurrentContext (bool calcSumX2)
 
static void do_numeric_accum (NumericAggState *state, Numeric newval)
 
static bool do_numeric_discard (NumericAggState *state, Numeric newval)
 
Datum numeric_accum (PG_FUNCTION_ARGS)
 
Datum numeric_combine (PG_FUNCTION_ARGS)
 
Datum numeric_avg_accum (PG_FUNCTION_ARGS)
 
Datum numeric_avg_combine (PG_FUNCTION_ARGS)
 
Datum numeric_avg_serialize (PG_FUNCTION_ARGS)
 
Datum numeric_avg_deserialize (PG_FUNCTION_ARGS)
 
Datum numeric_serialize (PG_FUNCTION_ARGS)
 
Datum numeric_deserialize (PG_FUNCTION_ARGS)
 
Datum numeric_accum_inv (PG_FUNCTION_ARGS)
 
Datum int2_accum (PG_FUNCTION_ARGS)
 
Datum int4_accum (PG_FUNCTION_ARGS)
 
Datum int8_accum (PG_FUNCTION_ARGS)
 
Datum numeric_poly_combine (PG_FUNCTION_ARGS)
 
Datum numeric_poly_serialize (PG_FUNCTION_ARGS)
 
Datum numeric_poly_deserialize (PG_FUNCTION_ARGS)
 
Datum int8_avg_accum (PG_FUNCTION_ARGS)
 
Datum int8_avg_combine (PG_FUNCTION_ARGS)
 
Datum int8_avg_serialize (PG_FUNCTION_ARGS)
 
Datum int8_avg_deserialize (PG_FUNCTION_ARGS)
 
Datum int2_accum_inv (PG_FUNCTION_ARGS)
 
Datum int4_accum_inv (PG_FUNCTION_ARGS)
 
Datum int8_accum_inv (PG_FUNCTION_ARGS)
 
Datum int8_avg_accum_inv (PG_FUNCTION_ARGS)
 
Datum numeric_poly_sum (PG_FUNCTION_ARGS)
 
Datum numeric_poly_avg (PG_FUNCTION_ARGS)
 
Datum numeric_avg (PG_FUNCTION_ARGS)
 
Datum numeric_sum (PG_FUNCTION_ARGS)
 
static Numeric numeric_stddev_internal (NumericAggState *state, bool variance, bool sample, bool *is_null)
 
Datum numeric_var_samp (PG_FUNCTION_ARGS)
 
Datum numeric_stddev_samp (PG_FUNCTION_ARGS)
 
Datum numeric_var_pop (PG_FUNCTION_ARGS)
 
Datum numeric_stddev_pop (PG_FUNCTION_ARGS)
 
Datum numeric_poly_var_samp (PG_FUNCTION_ARGS)
 
Datum numeric_poly_stddev_samp (PG_FUNCTION_ARGS)
 
Datum numeric_poly_var_pop (PG_FUNCTION_ARGS)
 
Datum numeric_poly_stddev_pop (PG_FUNCTION_ARGS)
 
Datum int2_sum (PG_FUNCTION_ARGS)
 
Datum int4_sum (PG_FUNCTION_ARGS)
 
Datum int8_sum (PG_FUNCTION_ARGS)
 
Datum int2_avg_accum (PG_FUNCTION_ARGS)
 
Datum int4_avg_accum (PG_FUNCTION_ARGS)
 
Datum int4_avg_combine (PG_FUNCTION_ARGS)
 
Datum int2_avg_accum_inv (PG_FUNCTION_ARGS)
 
Datum int4_avg_accum_inv (PG_FUNCTION_ARGS)
 
Datum int8_avg (PG_FUNCTION_ARGS)
 
Datum int2int4_sum (PG_FUNCTION_ARGS)
 

Variables

static const NumericDigit const_zero_data [1] = {0}
 
static const NumericVar const_zero
 
static const NumericDigit const_one_data [1] = {1}
 
static const NumericVar const_one
 
static const NumericVar const_minus_one
 
static const NumericDigit const_two_data [1] = {2}
 
static const NumericVar const_two
 
static const NumericDigit const_ten_data [1] = {10}
 
static const NumericVar const_ten
 
static const NumericDigit const_zero_point_nine_data [1] = {9000}
 
static const NumericVar const_zero_point_nine
 
static const NumericDigit const_one_point_one_data [2] = {1, 1000}
 
static const NumericVar const_one_point_one
 
static const NumericVar const_nan
 
static const NumericVar const_pinf
 
static const NumericVar const_ninf
 
static const int round_powers [4] = {0, 1000, 100, 10}
 

Macro Definition Documentation

◆ DatumGetNumericAbbrev

#define DatumGetNumericAbbrev (   X)    ((int32) (X))

Definition at line 405 of file numeric.c.

Referenced by numeric_cmp_abbrev().

◆ DEC_DIGITS

◆ digitbuf_alloc

#define digitbuf_alloc (   ndigits)    ((NumericDigit *) palloc((ndigits) * sizeof(NumericDigit)))

Definition at line 488 of file numeric.c.

Referenced by accum_sum_final(), add_abs(), alloc_var(), set_var_from_var(), and sub_abs().

◆ digitbuf_free

#define digitbuf_free (   buf)
Value:
do { \
if ((buf) != NULL) \
pfree(buf); \
} while (0)
static char * buf
Definition: pg_test_fsync.c:68

Definition at line 490 of file numeric.c.

Referenced by add_abs(), alloc_var(), free_var(), set_var_from_var(), sub_abs(), and zero_var().

◆ DIV_GUARD_DIGITS

#define DIV_GUARD_DIGITS   4

Definition at line 101 of file numeric.c.

Referenced by div_var_fast().

◆ dump_numeric

#define dump_numeric (   s,
 
)

Definition at line 484 of file numeric.c.

Referenced by int2int4_sum(), and make_result_opt_error().

◆ dump_var

#define dump_var (   s,
 
)

Definition at line 485 of file numeric.c.

Referenced by int2int4_sum().

◆ HALF_NBASE

#define HALF_NBASE   5000

Definition at line 98 of file numeric.c.

Referenced by div_var(), and round_var().

◆ init_var

◆ makePolyNumAggState

#define makePolyNumAggState   makeNumericAggState

◆ makePolyNumAggStateCurrentContext

#define makePolyNumAggStateCurrentContext   makeNumericAggStateCurrentContext

Definition at line 5354 of file numeric.c.

Referenced by int8_avg_deserialize(), and numeric_poly_deserialize().

◆ MUL_GUARD_DIGITS

#define MUL_GUARD_DIGITS   2 /* these are measured in NBASE digits */

Definition at line 100 of file numeric.c.

Referenced by mul_var().

◆ NA_TOTAL_COUNT

#define NA_TOTAL_COUNT (   na)    ((na)->N + (na)->NaNcount + (na)->pInfcount + (na)->nInfcount)

Definition at line 4613 of file numeric.c.

Referenced by numeric_avg(), numeric_stddev_internal(), and numeric_sum().

◆ NBASE

◆ NUMERIC_ABBREV_BITS

#define NUMERIC_ABBREV_BITS   (SIZEOF_DATUM * BITS_PER_BYTE)

Definition at line 396 of file numeric.c.

◆ NUMERIC_ABBREV_NAN

#define NUMERIC_ABBREV_NAN   NumericAbbrevGetDatum(PG_INT32_MIN)

Definition at line 406 of file numeric.c.

Referenced by numeric_abbrev_convert().

◆ NUMERIC_ABBREV_NINF

#define NUMERIC_ABBREV_NINF   NumericAbbrevGetDatum(PG_INT32_MAX)

Definition at line 408 of file numeric.c.

Referenced by numeric_abbrev_convert().

◆ NUMERIC_ABBREV_PINF

#define NUMERIC_ABBREV_PINF   NumericAbbrevGetDatum(-PG_INT32_MAX)

Definition at line 407 of file numeric.c.

Referenced by numeric_abbrev_convert().

◆ NUMERIC_CAN_BE_SHORT

#define NUMERIC_CAN_BE_SHORT (   scale,
  weight 
)
Value:
(weight) <= NUMERIC_SHORT_WEIGHT_MAX && \
#define NUMERIC_SHORT_WEIGHT_MAX
Definition: numeric.c:222
int scale
Definition: pgbench.c:191
#define NUMERIC_SHORT_WEIGHT_MIN
Definition: numeric.c:223
#define NUMERIC_SHORT_DSCALE_MAX
Definition: numeric.c:218

Definition at line 502 of file numeric.c.

Referenced by make_result_opt_error(), and numeric().

◆ NUMERIC_DIGITS

#define NUMERIC_DIGITS (   num)
Value:
(num)->choice.n_short.n_data : (num)->choice.n_long.n_data)
#define NUMERIC_HEADER_IS_SHORT(n)
Definition: numeric.c:184

Definition at line 498 of file numeric.c.

Referenced by cmp_numerics(), hash_numeric(), hash_numeric_extended(), init_var_from_num(), int2int4_sum(), make_result_opt_error(), and set_var_from_num().

◆ NUMERIC_DSCALE

#define NUMERIC_DSCALE (   n)
Value:
((n)->choice.n_short.n_header & NUMERIC_SHORT_DSCALE_MASK) \
: ((n)->choice.n_long.n_sign_dscale & NUMERIC_DSCALE_MASK))
#define NUMERIC_HEADER_IS_SHORT(n)
Definition: numeric.c:184
#define NUMERIC_DSCALE_MASK
Definition: numeric.c:235
#define NUMERIC_SHORT_DSCALE_MASK
Definition: numeric.c:216
#define NUMERIC_SHORT_DSCALE_SHIFT
Definition: numeric.c:217

Definition at line 244 of file numeric.c.

Referenced by init_var_from_num(), int2int4_sum(), make_result_opt_error(), numeric(), numeric_abs(), numeric_scale(), numeric_uminus(), and set_var_from_num().

◆ NUMERIC_DSCALE_MASK

#define NUMERIC_DSCALE_MASK   0x3FFF

Definition at line 235 of file numeric.c.

Referenced by make_result_opt_error(), numeric(), and numeric_recv().

◆ NUMERIC_DSCALE_MAX

#define NUMERIC_DSCALE_MAX   NUMERIC_DSCALE_MASK

Definition at line 236 of file numeric.c.

Referenced by numeric_mul_opt_error().

◆ NUMERIC_EXT_FLAGBITS

#define NUMERIC_EXT_FLAGBITS (   n)    ((n)->choice.n_header & NUMERIC_EXT_SIGN_MASK)

Definition at line 204 of file numeric.c.

◆ NUMERIC_EXT_SIGN_MASK

#define NUMERIC_EXT_SIGN_MASK   0xF000 /* high bits plus NaN/Inf flag bits */

Definition at line 198 of file numeric.c.

◆ NUMERIC_FLAGBITS

#define NUMERIC_FLAGBITS (   n)    ((n)->choice.n_header & NUMERIC_SIGN_MASK)

Definition at line 172 of file numeric.c.

◆ NUMERIC_HDRSZ

#define NUMERIC_HDRSZ   (VARHDRSZ + sizeof(uint16) + sizeof(int16))

Definition at line 176 of file numeric.c.

Referenced by make_result_opt_error(), and numeric_maximum_size().

◆ NUMERIC_HDRSZ_SHORT

#define NUMERIC_HDRSZ_SHORT   (VARHDRSZ + sizeof(uint16))

Definition at line 177 of file numeric.c.

Referenced by make_result_opt_error().

◆ NUMERIC_HEADER_IS_SHORT

#define NUMERIC_HEADER_IS_SHORT (   n)    (((n)->choice.n_header & 0x8000) != 0)

Definition at line 184 of file numeric.c.

◆ NUMERIC_HEADER_SIZE

#define NUMERIC_HEADER_SIZE (   n)
Value:
(VARHDRSZ + sizeof(uint16) + \
(NUMERIC_HEADER_IS_SHORT(n) ? 0 : sizeof(int16)))
signed short int16
Definition: c.h:428
#define NUMERIC_HEADER_IS_SHORT(n)
Definition: numeric.c:184
#define VARHDRSZ
Definition: c.h:627
unsigned short uint16
Definition: c.h:440

Definition at line 185 of file numeric.c.

◆ NUMERIC_INF_SIGN_MASK

#define NUMERIC_INF_SIGN_MASK   0x2000

Definition at line 202 of file numeric.c.

Referenced by numeric_abs(), and numeric_uminus().

◆ NUMERIC_IS_INF

#define NUMERIC_IS_INF (   n)    (((n)->choice.n_header & ~NUMERIC_INF_SIGN_MASK) == NUMERIC_PINF)

◆ NUMERIC_IS_NAN

◆ NUMERIC_IS_NINF

◆ NUMERIC_IS_PINF

◆ NUMERIC_IS_SHORT

#define NUMERIC_IS_SHORT (   n)    (NUMERIC_FLAGBITS(n) == NUMERIC_SHORT)

Definition at line 173 of file numeric.c.

Referenced by numeric(), numeric_abs(), and numeric_uminus().

◆ NUMERIC_IS_SPECIAL

◆ NUMERIC_NAN

#define NUMERIC_NAN   0xC000

◆ NUMERIC_NDIGITS

◆ NUMERIC_NEG

◆ NUMERIC_NINF

#define NUMERIC_NINF   0xF000

Definition at line 201 of file numeric.c.

Referenced by int2int4_sum(), make_result_opt_error(), and numeric_recv().

◆ NUMERIC_PINF

#define NUMERIC_PINF   0xD000

Definition at line 200 of file numeric.c.

Referenced by int2int4_sum(), make_result_opt_error(), and numeric_recv().

◆ NUMERIC_POS

◆ NUMERIC_SHORT

#define NUMERIC_SHORT   0x8000

Definition at line 169 of file numeric.c.

Referenced by make_result_opt_error().

◆ NUMERIC_SHORT_DSCALE_MASK

#define NUMERIC_SHORT_DSCALE_MASK   0x1F80

Definition at line 216 of file numeric.c.

Referenced by numeric().

◆ NUMERIC_SHORT_DSCALE_MAX

#define NUMERIC_SHORT_DSCALE_MAX   (NUMERIC_SHORT_DSCALE_MASK >> NUMERIC_SHORT_DSCALE_SHIFT)

Definition at line 218 of file numeric.c.

◆ NUMERIC_SHORT_DSCALE_SHIFT

#define NUMERIC_SHORT_DSCALE_SHIFT   7

Definition at line 217 of file numeric.c.

Referenced by make_result_opt_error(), and numeric().

◆ NUMERIC_SHORT_SIGN_MASK

#define NUMERIC_SHORT_SIGN_MASK   0x2000

Definition at line 215 of file numeric.c.

Referenced by make_result_opt_error(), numeric_abs(), and numeric_uminus().

◆ NUMERIC_SHORT_WEIGHT_MASK

#define NUMERIC_SHORT_WEIGHT_MASK   0x003F

Definition at line 221 of file numeric.c.

Referenced by make_result_opt_error().

◆ NUMERIC_SHORT_WEIGHT_MAX

#define NUMERIC_SHORT_WEIGHT_MAX   NUMERIC_SHORT_WEIGHT_MASK

Definition at line 222 of file numeric.c.

◆ NUMERIC_SHORT_WEIGHT_MIN

#define NUMERIC_SHORT_WEIGHT_MIN   (-(NUMERIC_SHORT_WEIGHT_MASK+1))

Definition at line 223 of file numeric.c.

◆ NUMERIC_SHORT_WEIGHT_SIGN_MASK

#define NUMERIC_SHORT_WEIGHT_SIGN_MASK   0x0040

Definition at line 220 of file numeric.c.

Referenced by make_result_opt_error().

◆ NUMERIC_SIGN

#define NUMERIC_SIGN (   n)
Value:
(((n)->choice.n_short.n_header & NUMERIC_SHORT_SIGN_MASK) ? \
NUMERIC_NEG : NUMERIC_POS) : \
NUMERIC_EXT_FLAGBITS(n) : NUMERIC_FLAGBITS(n)))
#define NUMERIC_POS
Definition: numeric.c:167
#define NUMERIC_FLAGBITS(n)
Definition: numeric.c:172
#define NUMERIC_IS_SPECIAL(n)
Definition: numeric.c:174
#define NUMERIC_SHORT_SIGN_MASK
Definition: numeric.c:215
#define NUMERIC_IS_SHORT(n)
Definition: numeric.c:173

Definition at line 238 of file numeric.c.

Referenced by cmp_numerics(), in_range_numeric_numeric(), init_var_from_num(), int2int4_sum(), numeric(), numeric_sign_internal(), numeric_uminus(), and set_var_from_num().

◆ NUMERIC_SIGN_MASK

#define NUMERIC_SIGN_MASK   0xC000

Definition at line 166 of file numeric.c.

Referenced by make_result_opt_error().

◆ NUMERIC_SPECIAL

#define NUMERIC_SPECIAL   0xC000

Definition at line 170 of file numeric.c.

Referenced by make_result_opt_error().

◆ NUMERIC_WEIGHT

#define NUMERIC_WEIGHT (   n)
Value:
(((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_SIGN_MASK ? \
| ((n)->choice.n_short.n_header & NUMERIC_SHORT_WEIGHT_MASK)) \
: ((n)->choice.n_long.n_weight))
#define NUMERIC_HEADER_IS_SHORT(n)
Definition: numeric.c:184
#define NUMERIC_SHORT_WEIGHT_SIGN_MASK
Definition: numeric.c:220
#define NUMERIC_SHORT_WEIGHT_MASK
Definition: numeric.c:221

Definition at line 248 of file numeric.c.

Referenced by cmp_numerics(), hash_numeric(), hash_numeric_extended(), init_var_from_num(), int2int4_sum(), make_result_opt_error(), numeric(), and set_var_from_num().

◆ NumericAbbrevGetDatum

#define NumericAbbrevGetDatum (   X)    ((Datum) (X))

Definition at line 404 of file numeric.c.

Referenced by numeric_cmp_abbrev().

Typedef Documentation

◆ Int8TransTypeData

◆ NumericAggState

◆ NumericDigit

Definition at line 103 of file numeric.c.

◆ NumericSumAccum

◆ NumericVar

typedef struct NumericVar NumericVar

◆ PolyNumAggState

Definition at line 5352 of file numeric.c.

Function Documentation

◆ accum_sum_add()

static void accum_sum_add ( NumericSumAccum accum,
const NumericVar var1 
)
static

Definition at line 11012 of file numeric.c.

References accum_sum_carry(), accum_sum_rescale(), NumericVar::digits, i, NBASE, NumericVar::ndigits, NumericSumAccum::neg_digits, NumericSumAccum::num_uncarried, NUMERIC_POS, NumericSumAccum::pos_digits, NumericVar::sign, NumericVar::weight, and NumericSumAccum::weight.

Referenced by accum_sum_combine(), do_numeric_accum(), do_numeric_discard(), int8_avg_deserialize(), numeric_avg_deserialize(), numeric_deserialize(), numeric_poly_deserialize(), and numeric_stddev_pop().

11013 {
11014  int32 *accum_digits;
11015  int i,
11016  val_i;
11017  int val_ndigits;
11018  NumericDigit *val_digits;
11019 
11020  /*
11021  * If we have accumulated too many values since the last carry
11022  * propagation, do it now, to avoid overflowing. (We could allow more
11023  * than NBASE - 1, if we reserved two extra digits, rather than one, for
11024  * carry propagation. But even with NBASE - 1, this needs to be done so
11025  * seldom, that the performance difference is negligible.)
11026  */
11027  if (accum->num_uncarried == NBASE - 1)
11028  accum_sum_carry(accum);
11029 
11030  /*
11031  * Adjust the weight or scale of the old value, so that it can accommodate
11032  * the new value.
11033  */
11034  accum_sum_rescale(accum, val);
11035 
11036  /* */
11037  if (val->sign == NUMERIC_POS)
11038  accum_digits = accum->pos_digits;
11039  else
11040  accum_digits = accum->neg_digits;
11041 
11042  /* copy these values into local vars for speed in loop */
11043  val_ndigits = val->ndigits;
11044  val_digits = val->digits;
11045 
11046  i = accum->weight - val->weight;
11047  for (val_i = 0; val_i < val_ndigits; val_i++)
11048  {
11049  accum_digits[i] += (int32) val_digits[val_i];
11050  i++;
11051  }
11052 
11053  accum->num_uncarried++;
11054 }
static void accum_sum_rescale(NumericSumAccum *accum, const NumericVar *val)
Definition: numeric.c:11133
#define NUMERIC_POS
Definition: numeric.c:167
int32 * neg_digits
Definition: numeric.c:380
int num_uncarried
Definition: numeric.c:377
static void accum_sum_carry(NumericSumAccum *accum)
Definition: numeric.c:11060
signed int int32
Definition: c.h:429
int16 NumericDigit
Definition: numeric.c:103
#define NBASE
Definition: numeric.c:97
int i
int32 * pos_digits
Definition: numeric.c:379
long val
Definition: informix.c:664

◆ accum_sum_carry()

static void accum_sum_carry ( NumericSumAccum accum)
static

Definition at line 11060 of file numeric.c.

References Assert, NumericSumAccum::have_carry_space, i, NBASE, NumericSumAccum::ndigits, NumericSumAccum::neg_digits, NumericSumAccum::num_uncarried, and NumericSumAccum::pos_digits.

Referenced by accum_sum_add(), and accum_sum_final().

11061 {
11062  int i;
11063  int ndigits;
11064  int32 *dig;
11065  int32 carry;
11066  int32 newdig = 0;
11067 
11068  /*
11069  * If no new values have been added since last carry propagation, nothing
11070  * to do.
11071  */
11072  if (accum->num_uncarried == 0)
11073  return;
11074 
11075  /*
11076  * We maintain that the weight of the accumulator is always one larger
11077  * than needed to hold the current value, before carrying, to make sure
11078  * there is enough space for the possible extra digit when carry is
11079  * propagated. We cannot expand the buffer here, unless we require
11080  * callers of accum_sum_final() to switch to the right memory context.
11081  */
11082  Assert(accum->pos_digits[0] == 0 && accum->neg_digits[0] == 0);
11083 
11084  ndigits = accum->ndigits;
11085 
11086  /* Propagate carry in the positive sum */
11087  dig = accum->pos_digits;
11088  carry = 0;
11089  for (i = ndigits - 1; i >= 0; i--)
11090  {
11091  newdig = dig[i] + carry;
11092  if (newdig >= NBASE)
11093  {
11094  carry = newdig / NBASE;
11095  newdig -= carry * NBASE;
11096  }
11097  else
11098  carry = 0;
11099  dig[i] = newdig;
11100  }
11101  /* Did we use up the digit reserved for carry propagation? */
11102  if (newdig > 0)
11103  accum->have_carry_space = false;
11104 
11105  /* And the same for the negative sum */
11106  dig = accum->neg_digits;
11107  carry = 0;
11108  for (i = ndigits - 1; i >= 0; i--)
11109  {
11110  newdig = dig[i] + carry;
11111  if (newdig >= NBASE)
11112  {
11113  carry = newdig / NBASE;
11114  newdig -= carry * NBASE;
11115  }
11116  else
11117  carry = 0;
11118  dig[i] = newdig;
11119  }
11120  if (newdig > 0)
11121  accum->have_carry_space = false;
11122 
11123  accum->num_uncarried = 0;
11124 }
int32 * neg_digits
Definition: numeric.c:380
int num_uncarried
Definition: numeric.c:377
signed int int32
Definition: c.h:429
bool have_carry_space
Definition: numeric.c:378
#define NBASE
Definition: numeric.c:97
#define Assert(condition)
Definition: c.h:804
int i
int32 * pos_digits
Definition: numeric.c:379

◆ accum_sum_combine()

static void accum_sum_combine ( NumericSumAccum accum,
NumericSumAccum accum2 
)
static

Definition at line 11290 of file numeric.c.

References accum_sum_add(), accum_sum_final(), free_var(), and init_var.

Referenced by int8_avg_combine(), numeric_avg_combine(), numeric_combine(), and numeric_poly_combine().

11291 {
11292  NumericVar tmp_var;
11293 
11294  init_var(&tmp_var);
11295 
11296  accum_sum_final(accum2, &tmp_var);
11297  accum_sum_add(accum, &tmp_var);
11298 
11299  free_var(&tmp_var);
11300 }
static void accum_sum_final(NumericSumAccum *accum, NumericVar *result)
Definition: numeric.c:11222
static void accum_sum_add(NumericSumAccum *accum, const NumericVar *var1)
Definition: numeric.c:11012
static void free_var(NumericVar *var)
Definition: numeric.c:6781
#define init_var(v)
Definition: numeric.c:496

◆ accum_sum_copy()

static void accum_sum_copy ( NumericSumAccum dst,
NumericSumAccum src 
)
static

Definition at line 11273 of file numeric.c.

References NumericSumAccum::dscale, NumericSumAccum::ndigits, NumericSumAccum::neg_digits, NumericSumAccum::num_uncarried, palloc(), NumericSumAccum::pos_digits, and NumericSumAccum::weight.

Referenced by int8_avg_combine(), numeric_avg_combine(), numeric_combine(), and numeric_poly_combine().

11274 {
11275  dst->pos_digits = palloc(src->ndigits * sizeof(int32));
11276  dst->neg_digits = palloc(src->ndigits * sizeof(int32));
11277 
11278  memcpy(dst->pos_digits, src->pos_digits, src->ndigits * sizeof(int32));
11279  memcpy(dst->neg_digits, src->neg_digits, src->ndigits * sizeof(int32));
11280  dst->num_uncarried = src->num_uncarried;
11281  dst->ndigits = src->ndigits;
11282  dst->weight = src->weight;
11283  dst->dscale = src->dscale;
11284 }
int32 * neg_digits
Definition: numeric.c:380
int num_uncarried
Definition: numeric.c:377
signed int int32
Definition: c.h:429
void * palloc(Size size)
Definition: mcxt.c:1062
int32 * pos_digits
Definition: numeric.c:379

◆ accum_sum_final()

static void accum_sum_final ( NumericSumAccum accum,
NumericVar result 
)
static

Definition at line 11222 of file numeric.c.

References accum_sum_carry(), add_var(), Assert, NumericVar::buf, digitbuf_alloc, NumericVar::digits, NumericVar::dscale, NumericSumAccum::dscale, i, init_var, NBASE, NumericVar::ndigits, NumericSumAccum::ndigits, NumericSumAccum::neg_digits, NUMERIC_NEG, NUMERIC_POS, NumericSumAccum::pos_digits, set_var_from_var(), NumericVar::sign, strip_var(), NumericVar::weight, and NumericSumAccum::weight.

Referenced by accum_sum_combine(), int8_avg_serialize(), numeric_avg(), numeric_avg_serialize(), numeric_poly_serialize(), numeric_serialize(), numeric_stddev_internal(), and numeric_sum().

11223 {
11224  int i;
11225  NumericVar pos_var;
11226  NumericVar neg_var;
11227 
11228  if (accum->ndigits == 0)
11229  {
11230  set_var_from_var(&const_zero, result);
11231  return;
11232  }
11233 
11234  /* Perform final carry */
11235  accum_sum_carry(accum);
11236 
11237  /* Create NumericVars representing the positive and negative sums */
11238  init_var(&pos_var);
11239  init_var(&neg_var);
11240 
11241  pos_var.ndigits = neg_var.ndigits = accum->ndigits;
11242  pos_var.weight = neg_var.weight = accum->weight;
11243  pos_var.dscale = neg_var.dscale = accum->dscale;
11244  pos_var.sign = NUMERIC_POS;
11245  neg_var.sign = NUMERIC_NEG;
11246 
11247  pos_var.buf = pos_var.digits = digitbuf_alloc(accum->ndigits);
11248  neg_var.buf = neg_var.digits = digitbuf_alloc(accum->ndigits);
11249 
11250  for (i = 0; i < accum->ndigits; i++)
11251  {
11252  Assert(accum->pos_digits[i] < NBASE);
11253  pos_var.digits[i] = (int16) accum->pos_digits[i];
11254 
11255  Assert(accum->neg_digits[i] < NBASE);
11256  neg_var.digits[i] = (int16) accum->neg_digits[i];
11257  }
11258 
11259  /* And add them together */
11260  add_var(&pos_var, &neg_var, result);
11261 
11262  /* Remove leading/trailing zeroes */
11263  strip_var(result);
11264 }
signed short int16
Definition: c.h:428
int weight
Definition: numeric.c:308
#define NUMERIC_POS
Definition: numeric.c:167
static void strip_var(NumericVar *var)
Definition: numeric.c:10955
int32 * neg_digits
Definition: numeric.c:380
#define digitbuf_alloc(ndigits)
Definition: numeric.c:488
int ndigits
Definition: numeric.c:307
static void accum_sum_carry(NumericSumAccum *accum)
Definition: numeric.c:11060
int dscale
Definition: numeric.c:310
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:309
NumericDigit * buf
Definition: numeric.c:311
#define NBASE
Definition: numeric.c:97
static const NumericVar const_zero
Definition: numeric.c:417
#define Assert(condition)
Definition: c.h:804
NumericDigit * digits
Definition: numeric.c:312
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7997
int i
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:7031
int32 * pos_digits
Definition: numeric.c:379
#define init_var(v)
Definition: numeric.c:496

◆ accum_sum_rescale()

static void accum_sum_rescale ( NumericSumAccum accum,
const NumericVar val 
)
static

Definition at line 11133 of file numeric.c.

References Assert, NumericVar::dscale, NumericSumAccum::dscale, NumericSumAccum::have_carry_space, NumericVar::ndigits, NumericSumAccum::ndigits, NumericSumAccum::neg_digits, palloc0(), pfree(), NumericSumAccum::pos_digits, NumericVar::weight, and NumericSumAccum::weight.

Referenced by accum_sum_add().

11134 {
11135  int old_weight = accum->weight;
11136  int old_ndigits = accum->ndigits;
11137  int accum_ndigits;
11138  int accum_weight;
11139  int accum_rscale;
11140  int val_rscale;
11141 
11142  accum_weight = old_weight;
11143  accum_ndigits = old_ndigits;
11144 
11145  /*
11146  * Does the new value have a larger weight? If so, enlarge the buffers,
11147  * and shift the existing value to the new weight, by adding leading
11148  * zeros.
11149  *
11150  * We enforce that the accumulator always has a weight one larger than
11151  * needed for the inputs, so that we have space for an extra digit at the
11152  * final carry-propagation phase, if necessary.
11153  */
11154  if (val->weight >= accum_weight)
11155  {
11156  accum_weight = val->weight + 1;
11157  accum_ndigits = accum_ndigits + (accum_weight - old_weight);
11158  }
11159 
11160  /*
11161  * Even though the new value is small, we might've used up the space
11162  * reserved for the carry digit in the last call to accum_sum_carry(). If
11163  * so, enlarge to make room for another one.
11164  */
11165  else if (!accum->have_carry_space)
11166  {
11167  accum_weight++;
11168  accum_ndigits++;
11169  }
11170 
11171  /* Is the new value wider on the right side? */
11172  accum_rscale = accum_ndigits - accum_weight - 1;
11173  val_rscale = val->ndigits - val->weight - 1;
11174  if (val_rscale > accum_rscale)
11175  accum_ndigits = accum_ndigits + (val_rscale - accum_rscale);
11176 
11177  if (accum_ndigits != old_ndigits ||
11178  accum_weight != old_weight)
11179  {
11180  int32 *new_pos_digits;
11181  int32 *new_neg_digits;
11182  int weightdiff;
11183 
11184  weightdiff = accum_weight - old_weight;
11185 
11186  new_pos_digits = palloc0(accum_ndigits * sizeof(int32));
11187  new_neg_digits = palloc0(accum_ndigits * sizeof(int32));
11188 
11189  if (accum->pos_digits)
11190  {
11191  memcpy(&new_pos_digits[weightdiff], accum->pos_digits,
11192  old_ndigits * sizeof(int32));
11193  pfree(accum->pos_digits);
11194 
11195  memcpy(&new_neg_digits[weightdiff], accum->neg_digits,
11196  old_ndigits * sizeof(int32));
11197  pfree(accum->neg_digits);
11198  }
11199 
11200  accum->pos_digits = new_pos_digits;
11201  accum->neg_digits = new_neg_digits;
11202 
11203  accum->weight = accum_weight;
11204  accum->ndigits = accum_ndigits;
11205 
11206  Assert(accum->pos_digits[0] == 0 && accum->neg_digits[0] == 0);
11207  accum->have_carry_space = true;
11208  }
11209 
11210  if (val->dscale > accum->dscale)
11211  accum->dscale = val->dscale;
11212 }
int weight
Definition: numeric.c:308
int32 * neg_digits
Definition: numeric.c:380
int ndigits
Definition: numeric.c:307
int dscale
Definition: numeric.c:310
signed int int32
Definition: c.h:429
void pfree(void *pointer)
Definition: mcxt.c:1169
bool have_carry_space
Definition: numeric.c:378
void * palloc0(Size size)
Definition: mcxt.c:1093
#define Assert(condition)
Definition: c.h:804
int32 * pos_digits
Definition: numeric.c:379

◆ accum_sum_reset()

static void accum_sum_reset ( NumericSumAccum accum)
static

Definition at line 10996 of file numeric.c.

References NumericSumAccum::dscale, i, NumericSumAccum::ndigits, NumericSumAccum::neg_digits, and NumericSumAccum::pos_digits.

Referenced by do_numeric_discard().

10997 {
10998  int i;
10999 
11000  accum->dscale = 0;
11001  for (i = 0; i < accum->ndigits; i++)
11002  {
11003  accum->pos_digits[i] = 0;
11004  accum->neg_digits[i] = 0;
11005  }
11006 }
int32 * neg_digits
Definition: numeric.c:380
int i
int32 * pos_digits
Definition: numeric.c:379

◆ add_abs()

static void add_abs ( const NumericVar var1,
const NumericVar var2,
NumericVar result 
)
static

Definition at line 10620 of file numeric.c.

References Assert, NumericVar::buf, digitbuf_alloc, digitbuf_free, NumericVar::digits, NumericVar::dscale, i, Max, NBASE, NumericVar::ndigits, strip_var(), and NumericVar::weight.

Referenced by add_var(), and sub_var().

10621 {
10622  NumericDigit *res_buf;
10623  NumericDigit *res_digits;
10624  int res_ndigits;
10625  int res_weight;
10626  int res_rscale,
10627  rscale1,
10628  rscale2;
10629  int res_dscale;
10630  int i,
10631  i1,
10632  i2;
10633  int carry = 0;
10634 
10635  /* copy these values into local vars for speed in inner loop */
10636  int var1ndigits = var1->ndigits;
10637  int var2ndigits = var2->ndigits;
10638  NumericDigit *var1digits = var1->digits;
10639  NumericDigit *var2digits = var2->digits;
10640 
10641  res_weight = Max(var1->weight, var2->weight) + 1;
10642 
10643  res_dscale = Max(var1->dscale, var2->dscale);
10644 
10645  /* Note: here we are figuring rscale in base-NBASE digits */
10646  rscale1 = var1->ndigits - var1->weight - 1;
10647  rscale2 = var2->ndigits - var2->weight - 1;
10648  res_rscale = Max(rscale1, rscale2);
10649 
10650  res_ndigits = res_rscale + res_weight + 1;
10651  if (res_ndigits <= 0)
10652  res_ndigits = 1;
10653 
10654  res_buf = digitbuf_alloc(res_ndigits + 1);
10655  res_buf[0] = 0; /* spare digit for later rounding */
10656  res_digits = res_buf + 1;
10657 
10658  i1 = res_rscale + var1->weight + 1;
10659  i2 = res_rscale + var2->weight + 1;
10660  for (i = res_ndigits - 1; i >= 0; i--)
10661  {
10662  i1--;
10663  i2--;
10664  if (i1 >= 0 && i1 < var1ndigits)
10665  carry += var1digits[i1];
10666  if (i2 >= 0 && i2 < var2ndigits)
10667  carry += var2digits[i2];
10668 
10669  if (carry >= NBASE)
10670  {
10671  res_digits[i] = carry - NBASE;
10672  carry = 1;
10673  }
10674  else
10675  {
10676  res_digits[i] = carry;
10677  carry = 0;
10678  }
10679  }
10680 
10681  Assert(carry == 0); /* else we failed to allow for carry out */
10682 
10683  digitbuf_free(result->buf);
10684  result->ndigits = res_ndigits;
10685  result->buf = res_buf;
10686  result->digits = res_digits;
10687  result->weight = res_weight;
10688  result->dscale = res_dscale;
10689 
10690  /* Remove leading/trailing zeroes */
10691  strip_var(result);
10692 }
int weight
Definition: numeric.c:308
static void strip_var(NumericVar *var)
Definition: numeric.c:10955
#define digitbuf_alloc(ndigits)
Definition: numeric.c:488
int ndigits
Definition: numeric.c:307
int dscale
Definition: numeric.c:310
#define digitbuf_free(buf)
Definition: numeric.c:490
int16 NumericDigit
Definition: numeric.c:103
NumericDigit * buf
Definition: numeric.c:311
#define NBASE
Definition: numeric.c:97
#define Assert(condition)
Definition: c.h:804
NumericDigit * digits
Definition: numeric.c:312
int i
#define Max(x, y)
Definition: numeric.c:13

◆ add_var()

static void add_var ( const NumericVar var1,
const NumericVar var2,
NumericVar result 
)
static

Definition at line 7997 of file numeric.c.

References add_abs(), cmp_abs(), NumericVar::dscale, Max, NUMERIC_NEG, NUMERIC_POS, NumericVar::sign, sub_abs(), and zero_var().

Referenced by accum_sum_final(), ceil_var(), compute_bucket(), div_mod_var(), exp_var(), generate_series_step_numeric(), in_range_numeric_numeric(), ln_var(), numeric_add_opt_error(), numeric_inc(), sqrt_var(), and width_bucket_numeric().

7998 {
7999  /*
8000  * Decide on the signs of the two variables what to do
8001  */
8002  if (var1->sign == NUMERIC_POS)
8003  {
8004  if (var2->sign == NUMERIC_POS)
8005  {
8006  /*
8007  * Both are positive result = +(ABS(var1) + ABS(var2))
8008  */
8009  add_abs(var1, var2, result);
8010  result->sign = NUMERIC_POS;
8011  }
8012  else
8013  {
8014  /*
8015  * var1 is positive, var2 is negative Must compare absolute values
8016  */
8017  switch (cmp_abs(var1, var2))
8018  {
8019  case 0:
8020  /* ----------
8021  * ABS(var1) == ABS(var2)
8022  * result = ZERO
8023  * ----------
8024  */
8025  zero_var(result);
8026  result->dscale = Max(var1->dscale, var2->dscale);
8027  break;
8028 
8029  case 1:
8030  /* ----------
8031  * ABS(var1) > ABS(var2)
8032  * result = +(ABS(var1) - ABS(var2))
8033  * ----------
8034  */
8035  sub_abs(var1, var2, result);
8036  result->sign = NUMERIC_POS;
8037  break;
8038 
8039  case -1:
8040  /* ----------
8041  * ABS(var1) < ABS(var2)
8042  * result = -(ABS(var2) - ABS(var1))
8043  * ----------
8044  */
8045  sub_abs(var2, var1, result);
8046  result->sign = NUMERIC_NEG;
8047  break;
8048  }
8049  }
8050  }
8051  else
8052  {
8053  if (var2->sign == NUMERIC_POS)
8054  {
8055  /* ----------
8056  * var1 is negative, var2 is positive
8057  * Must compare absolute values
8058  * ----------
8059  */
8060  switch (cmp_abs(var1, var2))
8061  {
8062  case 0:
8063  /* ----------
8064  * ABS(var1) == ABS(var2)
8065  * result = ZERO
8066  * ----------
8067  */
8068  zero_var(result);
8069  result->dscale = Max(var1->dscale, var2->dscale);
8070  break;
8071 
8072  case 1:
8073  /* ----------
8074  * ABS(var1) > ABS(var2)
8075  * result = -(ABS(var1) - ABS(var2))
8076  * ----------
8077  */
8078  sub_abs(var1, var2, result);
8079  result->sign = NUMERIC_NEG;
8080  break;
8081 
8082  case -1:
8083  /* ----------
8084  * ABS(var1) < ABS(var2)
8085  * result = +(ABS(var2) - ABS(var1))
8086  * ----------
8087  */
8088  sub_abs(var2, var1, result);
8089  result->sign = NUMERIC_POS;
8090  break;
8091  }
8092  }
8093  else
8094  {
8095  /* ----------
8096  * Both are negative
8097  * result = -(ABS(var1) + ABS(var2))
8098  * ----------
8099  */
8100  add_abs(var1, var2, result);
8101  result->sign = NUMERIC_NEG;
8102  }
8103  }
8104 }
static void sub_abs(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:10705
static void add_abs(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:10620
#define NUMERIC_POS
Definition: numeric.c:167
int dscale
Definition: numeric.c:310
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:309
static void zero_var(NumericVar *var)
Definition: numeric.c:6797
static int cmp_abs(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:10542
#define Max(x, y)
Definition: numeric.c:13

◆ alloc_var()

static void alloc_var ( NumericVar var,
int  ndigits 
)
static

Definition at line 6765 of file numeric.c.

References NumericVar::buf, digitbuf_alloc, digitbuf_free, NumericVar::digits, and NumericVar::ndigits.

Referenced by div_var(), div_var_fast(), int64_to_numericvar(), mul_var(), numeric_recv(), numericvar_deserialize(), numericvar_to_uint64(), set_var_from_num(), set_var_from_str(), and sqrt_var().

6766 {
6767  digitbuf_free(var->buf);
6768  var->buf = digitbuf_alloc(ndigits + 1);
6769  var->buf[0] = 0; /* spare digit for rounding */
6770  var->digits = var->buf + 1;
6771  var->ndigits = ndigits;
6772 }
#define digitbuf_alloc(ndigits)
Definition: numeric.c:488
int ndigits
Definition: numeric.c:307
#define digitbuf_free(buf)
Definition: numeric.c:490
NumericDigit * buf
Definition: numeric.c:311
NumericDigit * digits
Definition: numeric.c:312

◆ apply_typmod()

static void apply_typmod ( NumericVar var,
int32  typmod 
)
static

Definition at line 7478 of file numeric.c.

References DEC_DIGITS, NumericVar::digits, NumericVar::dscale, ereport, errcode(), errdetail(), errmsg(), ERROR, i, is_valid_numeric_typmod(), maxdigits, NumericVar::ndigits, numeric_typmod_precision(), numeric_typmod_scale(), round_var(), scale, and NumericVar::weight.

Referenced by numeric(), numeric_in(), and numeric_recv().

7479 {
7480  int precision;
7481  int scale;
7482  int maxdigits;
7483  int ddigits;
7484  int i;
7485 
7486  /* Do nothing if we have an invalid typmod */
7487  if (!is_valid_numeric_typmod(typmod))
7488  return;
7489 
7490  precision = numeric_typmod_precision(typmod);
7491  scale = numeric_typmod_scale(typmod);
7492  maxdigits = precision - scale;
7493 
7494  /* Round to target scale (and set var->dscale) */
7495  round_var(var, scale);
7496 
7497  /* but don't allow var->dscale to be negative */
7498  if (var->dscale < 0)
7499  var->dscale = 0;
7500 
7501  /*
7502  * Check for overflow - note we can't do this before rounding, because
7503  * rounding could raise the weight. Also note that the var's weight could
7504  * be inflated by leading zeroes, which will be stripped before storage
7505  * but perhaps might not have been yet. In any case, we must recognize a
7506  * true zero, whose weight doesn't mean anything.
7507  */
7508  ddigits = (var->weight + 1) * DEC_DIGITS;
7509  if (ddigits > maxdigits)
7510  {
7511  /* Determine true weight; and check for all-zero result */
7512  for (i = 0; i < var->ndigits; i++)
7513  {
7514  NumericDigit dig = var->digits[i];
7515 
7516  if (dig)
7517  {
7518  /* Adjust for any high-order decimal zero digits */
7519 #if DEC_DIGITS == 4
7520  if (dig < 10)
7521  ddigits -= 3;
7522  else if (dig < 100)
7523  ddigits -= 2;
7524  else if (dig < 1000)
7525  ddigits -= 1;
7526 #elif DEC_DIGITS == 2
7527  if (dig < 10)
7528  ddigits -= 1;
7529 #elif DEC_DIGITS == 1
7530  /* no adjustment */
7531 #else
7532 #error unsupported NBASE
7533 #endif
7534  if (ddigits > maxdigits)
7535  ereport(ERROR,
7536  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
7537  errmsg("numeric field overflow"),
7538  errdetail("A field with precision %d, scale %d must round to an absolute value less than %s%d.",
7539  precision, scale,
7540  /* Display 10^0 as 1 */
7541  maxdigits ? "10^" : "",
7542  maxdigits ? maxdigits : 1
7543  )));
7544  break;
7545  }
7546  ddigits -= DEC_DIGITS;
7547  }
7548  }
7549 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:10787
int weight
Definition: numeric.c:308
int errcode(int sqlerrcode)
Definition: elog.c:698
int scale
Definition: pgbench.c:191
int ndigits
Definition: numeric.c:307
int dscale
Definition: numeric.c:310
static bool is_valid_numeric_typmod(int32 typmod)
Definition: numeric.c:843
#define ERROR
Definition: elog.h:46
int16 NumericDigit
Definition: numeric.c:103
int errdetail(const char *fmt,...)
Definition: elog.c:1042
static int numeric_typmod_scale(int32 typmod)
Definition: numeric.c:869
#define ereport(elevel,...)
Definition: elog.h:157
int maxdigits
Definition: informix.c:665
NumericDigit * digits
Definition: numeric.c:312
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i
#define DEC_DIGITS
Definition: numeric.c:99
static int numeric_typmod_precision(int32 typmod)
Definition: numeric.c:854

◆ apply_typmod_special()

static void apply_typmod_special ( Numeric  num,
int32  typmod 
)
static

Definition at line 7558 of file numeric.c.

References Assert, ereport, errcode(), errdetail(), errmsg(), ERROR, is_valid_numeric_typmod(), NUMERIC_IS_NAN, NUMERIC_IS_SPECIAL, numeric_typmod_precision(), numeric_typmod_scale(), and scale.

Referenced by numeric(), numeric_in(), and numeric_recv().

7559 {
7560  int precision;
7561  int scale;
7562 
7563  Assert(NUMERIC_IS_SPECIAL(num)); /* caller error if not */
7564 
7565  /*
7566  * NaN is allowed regardless of the typmod; that's rather dubious perhaps,
7567  * but it's a longstanding behavior. Inf is rejected if we have any
7568  * typmod restriction, since an infinity shouldn't be claimed to fit in
7569  * any finite number of digits.
7570  */
7571  if (NUMERIC_IS_NAN(num))
7572  return;
7573 
7574  /* Do nothing if we have a default typmod (-1) */
7575  if (!is_valid_numeric_typmod(typmod))
7576  return;
7577 
7578  precision = numeric_typmod_precision(typmod);
7579  scale = numeric_typmod_scale(typmod);
7580 
7581  ereport(ERROR,
7582  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
7583  errmsg("numeric field overflow"),
7584  errdetail("A field with precision %d, scale %d cannot hold an infinite value.",
7585  precision, scale)));
7586 }
int errcode(int sqlerrcode)
Definition: elog.c:698
int scale
Definition: pgbench.c:191
static bool is_valid_numeric_typmod(int32 typmod)
Definition: numeric.c:843
#define NUMERIC_IS_SPECIAL(n)
Definition: numeric.c:174
#define ERROR
Definition: elog.h:46
int errdetail(const char *fmt,...)
Definition: elog.c:1042
static int numeric_typmod_scale(int32 typmod)
Definition: numeric.c:869
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:205
static int numeric_typmod_precision(int32 typmod)
Definition: numeric.c:854

◆ ceil_var()

static void ceil_var ( const NumericVar var,
NumericVar result 
)
static

Definition at line 9202 of file numeric.c.

References add_var(), cmp_var(), free_var(), init_var, NUMERIC_POS, set_var_from_var(), NumericVar::sign, and trunc_var().

Referenced by numeric_ceil().

9203 {
9204  NumericVar tmp;
9205 
9206  init_var(&tmp);
9207  set_var_from_var(var, &tmp);
9208 
9209  trunc_var(&tmp, 0);
9210 
9211  if (var->sign == NUMERIC_POS && cmp_var(var, &tmp) != 0)
9212  add_var(&tmp, &const_one, &tmp);
9213 
9214  set_var_from_var(&tmp, result);
9215  free_var(&tmp);
9216 }
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:10893
static const NumericVar const_one
Definition: numeric.c:421
#define NUMERIC_POS
Definition: numeric.c:167
int sign
Definition: numeric.c:309
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:7939
static void free_var(NumericVar *var)
Definition: numeric.c:6781
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7997
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:7031
#define init_var(v)
Definition: numeric.c:496

◆ cmp_abs()

static int cmp_abs ( const NumericVar var1,
const NumericVar var2 
)
static

Definition at line 10542 of file numeric.c.

References cmp_abs_common(), NumericVar::digits, NumericVar::ndigits, and NumericVar::weight.

Referenced by add_var(), div_mod_var(), gcd_var(), and sub_var().

10543 {
10544  return cmp_abs_common(var1->digits, var1->ndigits, var1->weight,
10545  var2->digits, var2->ndigits, var2->weight);
10546 }
int weight
Definition: numeric.c:308
int ndigits
Definition: numeric.c:307
NumericDigit * digits
Definition: numeric.c:312
static int cmp_abs_common(const NumericDigit *var1digits, int var1ndigits, int var1weight, const NumericDigit *var2digits, int var2ndigits, int var2weight)
Definition: numeric.c:10556

◆ cmp_abs_common()

static int cmp_abs_common ( const NumericDigit var1digits,
int  var1ndigits,
int  var1weight,
const NumericDigit var2digits,
int  var2ndigits,
int  var2weight 
)
static

Definition at line 10556 of file numeric.c.

Referenced by cmp_abs(), and cmp_var_common().

10558 {
10559  int i1 = 0;
10560  int i2 = 0;
10561 
10562  /* Check any digits before the first common digit */
10563 
10564  while (var1weight > var2weight && i1 < var1ndigits)
10565  {
10566  if (var1digits[i1++] != 0)
10567  return 1;
10568  var1weight--;
10569  }
10570  while (var2weight > var1weight && i2 < var2ndigits)
10571  {
10572  if (var2digits[i2++] != 0)
10573  return -1;
10574  var2weight--;
10575  }
10576 
10577  /* At this point, either w1 == w2 or we've run out of digits */
10578 
10579  if (var1weight == var2weight)
10580  {
10581  while (i1 < var1ndigits && i2 < var2ndigits)
10582  {
10583  int stat = var1digits[i1++] - var2digits[i2++];
10584 
10585  if (stat)
10586  {
10587  if (stat > 0)
10588  return 1;
10589  return -1;
10590  }
10591  }
10592  }
10593 
10594  /*
10595  * At this point, we've run out of digits on one side or the other; so any
10596  * remaining nonzero digits imply that side is larger
10597  */
10598  while (i1 < var1ndigits)
10599  {
10600  if (var1digits[i1++] != 0)
10601  return 1;
10602  }
10603  while (i2 < var2ndigits)
10604  {
10605  if (var2digits[i2++] != 0)
10606  return -1;
10607  }
10608 
10609  return 0;
10610 }

◆ cmp_numerics()

static int cmp_numerics ( Numeric  num1,
Numeric  num2 
)
static

Definition at line 2430 of file numeric.c.

References cmp_var_common(), NUMERIC_DIGITS, NUMERIC_IS_NAN, NUMERIC_IS_NINF, NUMERIC_IS_PINF, NUMERIC_IS_SPECIAL, NUMERIC_NDIGITS, NUMERIC_SIGN, and NUMERIC_WEIGHT.

Referenced by numeric_cmp(), numeric_eq(), numeric_fast_cmp(), numeric_ge(), numeric_gt(), numeric_larger(), numeric_le(), numeric_lt(), numeric_ne(), numeric_smaller(), and width_bucket_numeric().

2431 {
2432  int result;
2433 
2434  /*
2435  * We consider all NANs to be equal and larger than any non-NAN (including
2436  * Infinity). This is somewhat arbitrary; the important thing is to have
2437  * a consistent sort order.
2438  */
2439  if (NUMERIC_IS_SPECIAL(num1))
2440  {
2441  if (NUMERIC_IS_NAN(num1))
2442  {
2443  if (NUMERIC_IS_NAN(num2))
2444  result = 0; /* NAN = NAN */
2445  else
2446  result = 1; /* NAN > non-NAN */
2447  }
2448  else if (NUMERIC_IS_PINF(num1))
2449  {
2450  if (NUMERIC_IS_NAN(num2))
2451  result = -1; /* PINF < NAN */
2452  else if (NUMERIC_IS_PINF(num2))
2453  result = 0; /* PINF = PINF */
2454  else
2455  result = 1; /* PINF > anything else */
2456  }
2457  else /* num1 must be NINF */
2458  {
2459  if (NUMERIC_IS_NINF(num2))
2460  result = 0; /* NINF = NINF */
2461  else
2462  result = -1; /* NINF < anything else */
2463  }
2464  }
2465  else if (NUMERIC_IS_SPECIAL(num2))
2466  {
2467  if (NUMERIC_IS_NINF(num2))
2468  result = 1; /* normal > NINF */
2469  else
2470  result = -1; /* normal < NAN or PINF */
2471  }
2472  else
2473  {
2474  result = cmp_var_common(NUMERIC_DIGITS(num1), NUMERIC_NDIGITS(num1),
2475  NUMERIC_WEIGHT(num1), NUMERIC_SIGN(num1),
2476  NUMERIC_DIGITS(num2), NUMERIC_NDIGITS(num2),
2477  NUMERIC_WEIGHT(num2), NUMERIC_SIGN(num2));
2478  }
2479 
2480  return result;
2481 }
#define NUMERIC_IS_SPECIAL(n)
Definition: numeric.c:174
#define NUMERIC_DIGITS(num)
Definition: numeric.c:498
#define NUMERIC_IS_PINF(n)
Definition: numeric.c:206
#define NUMERIC_SIGN(n)
Definition: numeric.c:238
#define NUMERIC_NDIGITS(num)
Definition: numeric.c:500
static int cmp_var_common(const NumericDigit *var1digits, int var1ndigits, int var1weight, int var1sign, const NumericDigit *var2digits, int var2ndigits, int var2weight, int var2sign)
Definition: numeric.c:7954
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:248
#define NUMERIC_IS_NINF(n)
Definition: numeric.c:207
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:205

◆ cmp_var()

static int cmp_var ( const NumericVar var1,
const NumericVar var2 
)
static

Definition at line 7939 of file numeric.c.

References cmp_var_common(), NumericVar::digits, NumericVar::ndigits, NumericVar::sign, and NumericVar::weight.

Referenced by ceil_var(), estimate_ln_dweight(), floor_var(), generate_series_step_numeric(), in_range_numeric_numeric(), ln_var(), numeric_power(), numeric_stddev_internal(), power_var(), and sqrt_var().

7940 {
7941  return cmp_var_common(var1->digits, var1->ndigits,
7942  var1->weight, var1->sign,
7943  var2->digits, var2->ndigits,
7944  var2->weight, var2->sign);
7945 }
int weight
Definition: numeric.c:308
int ndigits
Definition: numeric.c:307
int sign
Definition: numeric.c:309
static int cmp_var_common(const NumericDigit *var1digits, int var1ndigits, int var1weight, int var1sign, const NumericDigit *var2digits, int var2ndigits, int var2weight, int var2sign)
Definition: numeric.c:7954
NumericDigit * digits
Definition: numeric.c:312

◆ cmp_var_common()

static int cmp_var_common ( const NumericDigit var1digits,
int  var1ndigits,
int  var1weight,
int  var1sign,
const NumericDigit var2digits,
int  var2ndigits,
int  var2weight,
int  var2sign 
)
static

Definition at line 7954 of file numeric.c.

References cmp_abs_common(), NUMERIC_NEG, and NUMERIC_POS.

Referenced by cmp_numerics(), and cmp_var().

7958 {
7959  if (var1ndigits == 0)
7960  {
7961  if (var2ndigits == 0)
7962  return 0;
7963  if (var2sign == NUMERIC_NEG)
7964  return 1;
7965  return -1;
7966  }
7967  if (var2ndigits == 0)
7968  {
7969  if (var1sign == NUMERIC_POS)
7970  return 1;
7971  return -1;
7972  }
7973 
7974  if (var1sign == NUMERIC_POS)
7975  {
7976  if (var2sign == NUMERIC_NEG)
7977  return 1;
7978  return cmp_abs_common(var1digits, var1ndigits, var1weight,
7979  var2digits, var2ndigits, var2weight);
7980  }
7981 
7982  if (var2sign == NUMERIC_POS)
7983  return -1;
7984 
7985  return cmp_abs_common(var2digits, var2ndigits, var2weight,
7986  var1digits, var1ndigits, var1weight);
7987 }
#define NUMERIC_POS
Definition: numeric.c:167
#define NUMERIC_NEG
Definition: numeric.c:168
static int cmp_abs_common(const NumericDigit *var1digits, int var1ndigits, int var1weight, const NumericDigit *var2digits, int var2ndigits, int var2weight)
Definition: numeric.c:10556

◆ compute_bucket()

static void compute_bucket ( Numeric  operand,
Numeric  bound1,
Numeric  bound2,
const NumericVar count_var,
bool  reversed_bounds,
NumericVar result_var 
)
static

Definition at line 1853 of file numeric.c.

References add_var(), div_var(), NumericVar::dscale, floor_var(), free_var(), init_var_from_num(), mul_var(), select_div_scale(), and sub_var().

Referenced by width_bucket_numeric().

1856 {
1857  NumericVar bound1_var;
1858  NumericVar bound2_var;
1859  NumericVar operand_var;
1860 
1861  init_var_from_num(bound1, &bound1_var);
1862  init_var_from_num(bound2, &bound2_var);
1863  init_var_from_num(operand, &operand_var);
1864 
1865  if (!reversed_bounds)
1866  {
1867  sub_var(&operand_var, &bound1_var, &operand_var);
1868  sub_var(&bound2_var, &bound1_var, &bound2_var);
1869  }
1870  else
1871  {
1872  sub_var(&bound1_var, &operand_var, &operand_var);
1873  sub_var(&bound1_var, &bound2_var, &bound2_var);
1874  }
1875 
1876  mul_var(&operand_var, count_var, &operand_var,
1877  operand_var.dscale + count_var->dscale);
1878  div_var(&operand_var, &bound2_var, result_var,
1879  select_div_scale(&operand_var, &bound2_var), true);
1880  add_var(result_var, &const_one, result_var);
1881  floor_var(result_var, result_var);
1882 
1883  free_var(&bound1_var);
1884  free_var(&bound2_var);
1885  free_var(&operand_var);
1886 }
static const NumericVar const_one
Definition: numeric.c:421
static void div_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:8443
int dscale
Definition: numeric.c:310
static int select_div_scale(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:9034
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:7014
static void free_var(NumericVar *var)
Definition: numeric.c:6781
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:8235
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7997
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8114
static void floor_var(const NumericVar *var, NumericVar *result)
Definition: numeric.c:9226

◆ div_mod_var()

static void div_mod_var ( const NumericVar var1,
const NumericVar var2,
NumericVar quot,
NumericVar rem 
)
static

Definition at line 9132 of file numeric.c.

References add_var(), cmp_abs(), div_var_fast(), NumericVar::dscale, free_var(), init_var, mul_var(), NumericVar::ndigits, set_var_from_var(), NumericVar::sign, and sub_var().

Referenced by sqrt_var().

9134 {
9135  NumericVar q;
9136  NumericVar r;
9137 
9138  init_var(&q);
9139  init_var(&r);
9140 
9141  /*
9142  * Use div_var_fast() to get an initial estimate for the integer quotient.
9143  * This might be inaccurate (per the warning in div_var_fast's comments),
9144  * but we can correct it below.
9145  */
9146  div_var_fast(var1, var2, &q, 0, false);
9147 
9148  /* Compute initial estimate of remainder using the quotient estimate. */
9149  mul_var(var2, &q, &r, var2->dscale);
9150  sub_var(var1, &r, &r);
9151 
9152  /*
9153  * Adjust the results if necessary --- the remainder should have the same
9154  * sign as var1, and its absolute value should be less than the absolute
9155  * value of var2.
9156  */
9157  while (r.ndigits != 0 && r.sign != var1->sign)
9158  {
9159  /* The absolute value of the quotient is too large */
9160  if (var1->sign == var2->sign)
9161  {
9162  sub_var(&q, &const_one, &q);
9163  add_var(&r, var2, &r);
9164  }
9165  else
9166  {
9167  add_var(&q, &const_one, &q);
9168  sub_var(&r, var2, &r);
9169  }
9170  }
9171 
9172  while (cmp_abs(&r, var2) >= 0)
9173  {
9174  /* The absolute value of the quotient is too small */
9175  if (var1->sign == var2->sign)
9176  {
9177  add_var(&q, &const_one, &q);
9178  sub_var(&r, var2, &r);
9179  }
9180  else
9181  {
9182  sub_var(&q, &const_one, &q);
9183  add_var(&r, var2, &r);
9184  }
9185  }
9186 
9187  set_var_from_var(&q, quot);
9188  set_var_from_var(&r, rem);
9189 
9190  free_var(&q);
9191  free_var(&r);
9192 }
static const NumericVar const_one
Definition: numeric.c:421
int ndigits
Definition: numeric.c:307
int dscale
Definition: numeric.c:310
int sign
Definition: numeric.c:309
static void div_var_fast(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:8728
static void free_var(NumericVar *var)
Definition: numeric.c:6781
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:8235
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7997
static int cmp_abs(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:10542
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8114
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:7031
#define init_var(v)
Definition: numeric.c:496

◆ div_var()

static void div_var ( const NumericVar var1,
const NumericVar var2,
NumericVar result,
int  rscale,
bool  round 
)
static

Definition at line 8443 of file numeric.c.

References alloc_var(), Assert, DEC_DIGITS, NumericVar::digits, NumericVar::dscale, ereport, errcode(), errmsg(), ERROR, HALF_NBASE, i, Max, NBASE, NumericVar::ndigits, NUMERIC_NEG, NUMERIC_POS, palloc0(), pfree(), round_var(), NumericVar::sign, strip_var(), trunc_var(), NumericVar::weight, and zero_var().

Referenced by compute_bucket(), get_str_from_var_sci(), mod_var(), numeric_div_opt_error(), numeric_div_trunc(), numeric_lcm(), numeric_stddev_internal(), and power_var_int().

8445 {
8446  int div_ndigits;
8447  int res_ndigits;
8448  int res_sign;
8449  int res_weight;
8450  int carry;
8451  int borrow;
8452  int divisor1;
8453  int divisor2;
8454  NumericDigit *dividend;
8455  NumericDigit *divisor;
8456  NumericDigit *res_digits;
8457  int i;
8458  int j;
8459 
8460  /* copy these values into local vars for speed in inner loop */
8461  int var1ndigits = var1->ndigits;
8462  int var2ndigits = var2->ndigits;
8463 
8464  /*
8465  * First of all division by zero check; we must not be handed an
8466  * unnormalized divisor.
8467  */
8468  if (var2ndigits == 0 || var2->digits[0] == 0)
8469  ereport(ERROR,
8470  (errcode(ERRCODE_DIVISION_BY_ZERO),
8471  errmsg("division by zero")));
8472 
8473  /*
8474  * Now result zero check
8475  */
8476  if (var1ndigits == 0)
8477  {
8478  zero_var(result);
8479  result->dscale = rscale;
8480  return;
8481  }
8482 
8483  /*
8484  * Determine the result sign, weight and number of digits to calculate.
8485  * The weight figured here is correct if the emitted quotient has no
8486  * leading zero digits; otherwise strip_var() will fix things up.
8487  */
8488  if (var1->sign == var2->sign)
8489  res_sign = NUMERIC_POS;
8490  else
8491  res_sign = NUMERIC_NEG;
8492  res_weight = var1->weight - var2->weight;
8493  /* The number of accurate result digits we need to produce: */
8494  res_ndigits = res_weight + 1 + (rscale + DEC_DIGITS - 1) / DEC_DIGITS;
8495  /* ... but always at least 1 */
8496  res_ndigits = Max(res_ndigits, 1);
8497  /* If rounding needed, figure one more digit to ensure correct result */
8498  if (round)
8499  res_ndigits++;
8500 
8501  /*
8502  * The working dividend normally requires res_ndigits + var2ndigits
8503  * digits, but make it at least var1ndigits so we can load all of var1
8504  * into it. (There will be an additional digit dividend[0] in the
8505  * dividend space, but for consistency with Knuth's notation we don't
8506  * count that in div_ndigits.)
8507  */
8508  div_ndigits = res_ndigits + var2ndigits;
8509  div_ndigits = Max(div_ndigits, var1ndigits);
8510 
8511  /*
8512  * We need a workspace with room for the working dividend (div_ndigits+1
8513  * digits) plus room for the possibly-normalized divisor (var2ndigits
8514  * digits). It is convenient also to have a zero at divisor[0] with the
8515  * actual divisor data in divisor[1 .. var2ndigits]. Transferring the
8516  * digits into the workspace also allows us to realloc the result (which
8517  * might be the same as either input var) before we begin the main loop.
8518  * Note that we use palloc0 to ensure that divisor[0], dividend[0], and
8519  * any additional dividend positions beyond var1ndigits, start out 0.
8520  */
8521  dividend = (NumericDigit *)
8522  palloc0((div_ndigits + var2ndigits + 2) * sizeof(NumericDigit));
8523  divisor = dividend + (div_ndigits + 1);
8524  memcpy(dividend + 1, var1->digits, var1ndigits * sizeof(NumericDigit));
8525  memcpy(divisor + 1, var2->digits, var2ndigits * sizeof(NumericDigit));
8526 
8527  /*
8528  * Now we can realloc the result to hold the generated quotient digits.
8529  */
8530  alloc_var(result, res_ndigits);
8531  res_digits = result->digits;
8532 
8533  if (var2ndigits == 1)
8534  {
8535  /*
8536  * If there's only a single divisor digit, we can use a fast path (cf.
8537  * Knuth section 4.3.1 exercise 16).
8538  */
8539  divisor1 = divisor[1];
8540  carry = 0;
8541  for (i = 0; i < res_ndigits; i++)
8542  {
8543  carry = carry * NBASE + dividend[i + 1];
8544  res_digits[i] = carry / divisor1;
8545  carry = carry % divisor1;
8546  }
8547  }
8548  else
8549  {
8550  /*
8551  * The full multiple-place algorithm is taken from Knuth volume 2,
8552  * Algorithm 4.3.1D.
8553  *
8554  * We need the first divisor digit to be >= NBASE/2. If it isn't,
8555  * make it so by scaling up both the divisor and dividend by the
8556  * factor "d". (The reason for allocating dividend[0] above is to
8557  * leave room for possible carry here.)
8558  */
8559  if (divisor[1] < HALF_NBASE)
8560  {
8561  int d = NBASE / (divisor[1] + 1);
8562 
8563  carry = 0;
8564  for (i = var2ndigits; i > 0; i--)
8565  {
8566  carry += divisor[i] * d;
8567  divisor[i] = carry % NBASE;
8568  carry = carry / NBASE;
8569  }
8570  Assert(carry == 0);
8571  carry = 0;
8572  /* at this point only var1ndigits of dividend can be nonzero */
8573  for (i = var1ndigits; i >= 0; i--)
8574  {
8575  carry += dividend[i] * d;
8576  dividend[i] = carry % NBASE;
8577  carry = carry / NBASE;
8578  }
8579  Assert(carry == 0);
8580  Assert(divisor[1] >= HALF_NBASE);
8581  }
8582  /* First 2 divisor digits are used repeatedly in main loop */
8583  divisor1 = divisor[1];
8584  divisor2 = divisor[2];
8585 
8586  /*
8587  * Begin the main loop. Each iteration of this loop produces the j'th
8588  * quotient digit by dividing dividend[j .. j + var2ndigits] by the
8589  * divisor; this is essentially the same as the common manual
8590  * procedure for long division.
8591  */
8592  for (j = 0; j < res_ndigits; j++)
8593  {
8594  /* Estimate quotient digit from the first two dividend digits */
8595  int next2digits = dividend[j] * NBASE + dividend[j + 1];
8596  int qhat;
8597 
8598  /*
8599  * If next2digits are 0, then quotient digit must be 0 and there's
8600  * no need to adjust the working dividend. It's worth testing
8601  * here to fall out ASAP when processing trailing zeroes in a
8602  * dividend.
8603  */
8604  if (next2digits == 0)
8605  {
8606  res_digits[j] = 0;
8607  continue;
8608  }
8609 
8610  if (dividend[j] == divisor1)
8611  qhat = NBASE - 1;
8612  else
8613  qhat = next2digits / divisor1;
8614 
8615  /*
8616  * Adjust quotient digit if it's too large. Knuth proves that
8617  * after this step, the quotient digit will be either correct or
8618  * just one too large. (Note: it's OK to use dividend[j+2] here
8619  * because we know the divisor length is at least 2.)
8620  */
8621  while (divisor2 * qhat >
8622  (next2digits - qhat * divisor1) * NBASE + dividend[j + 2])
8623  qhat--;
8624 
8625  /* As above, need do nothing more when quotient digit is 0 */
8626  if (qhat > 0)
8627  {
8628  /*
8629  * Multiply the divisor by qhat, and subtract that from the
8630  * working dividend. "carry" tracks the multiplication,
8631  * "borrow" the subtraction (could we fold these together?)
8632  */
8633  carry = 0;
8634  borrow = 0;
8635  for (i = var2ndigits; i >= 0; i--)
8636  {
8637  carry += divisor[i] * qhat;
8638  borrow -= carry % NBASE;
8639  carry = carry / NBASE;
8640  borrow += dividend[j + i];
8641  if (borrow < 0)
8642  {
8643  dividend[j + i] = borrow + NBASE;
8644  borrow = -1;
8645  }
8646  else
8647  {
8648  dividend[j + i] = borrow;
8649  borrow = 0;
8650  }
8651  }
8652  Assert(carry == 0);
8653 
8654  /*
8655  * If we got a borrow out of the top dividend digit, then
8656  * indeed qhat was one too large. Fix it, and add back the
8657  * divisor to correct the working dividend. (Knuth proves
8658  * that this will occur only about 3/NBASE of the time; hence,
8659  * it's a good idea to test this code with small NBASE to be
8660  * sure this section gets exercised.)
8661  */
8662  if (borrow)
8663  {
8664  qhat--;
8665  carry = 0;
8666  for (i = var2ndigits; i >= 0; i--)
8667  {
8668  carry += dividend[j + i] + divisor[i];
8669  if (carry >= NBASE)
8670  {
8671  dividend[j + i] = carry - NBASE;
8672  carry = 1;
8673  }
8674  else
8675  {
8676  dividend[j + i] = carry;
8677  carry = 0;
8678  }
8679  }
8680  /* A carry should occur here to cancel the borrow above */
8681  Assert(carry == 1);
8682  }
8683  }
8684 
8685  /* And we're done with this quotient digit */
8686  res_digits[j] = qhat;
8687  }
8688  }
8689 
8690  pfree(dividend);
8691 
8692  /*
8693  * Finally, round or truncate the result to the requested precision.
8694  */
8695  result->weight = res_weight;
8696  result->sign = res_sign;
8697 
8698  /* Round or truncate to target rscale (and set result->dscale) */
8699  if (round)
8700  round_var(result, rscale);
8701  else
8702  trunc_var(result, rscale);
8703 
8704  /* Strip leading and trailing zeroes */
8705  strip_var(result);
8706 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:10787
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:10893
int weight
Definition: numeric.c:308
#define NUMERIC_POS
Definition: numeric.c:167
static void strip_var(NumericVar *var)
Definition: numeric.c:10955
int errcode(int sqlerrcode)
Definition: elog.c:698
int ndigits
Definition: numeric.c:307
int dscale
Definition: numeric.c:310
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:309
static void zero_var(NumericVar *var)
Definition: numeric.c:6797
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
int16 NumericDigit
Definition: numeric.c:103
#define HALF_NBASE
Definition: numeric.c:98
#define NBASE
Definition: numeric.c:97
void * palloc0(Size size)
Definition: mcxt.c:1093
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
NumericDigit * digits
Definition: numeric.c:312
int errmsg(const char *fmt,...)
Definition: elog.c:909
static void alloc_var(NumericVar *var, int ndigits)
Definition: numeric.c:6765
int i
#define Max(x, y)
Definition: numeric.c:13
#define DEC_DIGITS
Definition: numeric.c:99

◆ div_var_fast()

static void div_var_fast ( const NumericVar var1,
const NumericVar var2,
NumericVar result,
int  rscale,
bool  round 
)
static

Definition at line 8728 of file numeric.c.

References Abs, alloc_var(), Assert, DEC_DIGITS, NumericVar::digits, DIV_GUARD_DIGITS, NumericVar::dscale, ereport, errcode(), errmsg(), ERROR, i, Min, NBASE, NumericVar::ndigits, NUMERIC_NEG, NUMERIC_POS, palloc0(), pfree(), round_var(), NumericVar::sign, strip_var(), trunc_var(), NumericVar::weight, and zero_var().

Referenced by div_mod_var(), exp_var(), ln_var(), log_var(), and power_var_int().

8730 {
8731  int div_ndigits;
8732  int load_ndigits;
8733  int res_sign;
8734  int res_weight;
8735  int *div;
8736  int qdigit;
8737  int carry;
8738  int maxdiv;
8739  int newdig;
8740  NumericDigit *res_digits;
8741  double fdividend,
8742  fdivisor,
8743  fdivisorinverse,
8744  fquotient;
8745  int qi;
8746  int i;
8747 
8748  /* copy these values into local vars for speed in inner loop */
8749  int var1ndigits = var1->ndigits;
8750  int var2ndigits = var2->ndigits;
8751  NumericDigit *var1digits = var1->digits;
8752  NumericDigit *var2digits = var2->digits;
8753 
8754  /*
8755  * First of all division by zero check; we must not be handed an
8756  * unnormalized divisor.
8757  */
8758  if (var2ndigits == 0 || var2digits[0] == 0)
8759  ereport(ERROR,
8760  (errcode(ERRCODE_DIVISION_BY_ZERO),
8761  errmsg("division by zero")));
8762 
8763  /*
8764  * Now result zero check
8765  */
8766  if (var1ndigits == 0)
8767  {
8768  zero_var(result);
8769  result->dscale = rscale;
8770  return;
8771  }
8772 
8773  /*
8774  * Determine the result sign, weight and number of digits to calculate
8775  */
8776  if (var1->sign == var2->sign)
8777  res_sign = NUMERIC_POS;
8778  else
8779  res_sign = NUMERIC_NEG;
8780  res_weight = var1->weight - var2->weight + 1;
8781  /* The number of accurate result digits we need to produce: */
8782  div_ndigits = res_weight + 1 + (rscale + DEC_DIGITS - 1) / DEC_DIGITS;
8783  /* Add guard digits for roundoff error */
8784  div_ndigits += DIV_GUARD_DIGITS;
8785  if (div_ndigits < DIV_GUARD_DIGITS)
8786  div_ndigits = DIV_GUARD_DIGITS;
8787 
8788  /*
8789  * We do the arithmetic in an array "div[]" of signed int's. Since
8790  * INT_MAX is noticeably larger than NBASE*NBASE, this gives us headroom
8791  * to avoid normalizing carries immediately.
8792  *
8793  * We start with div[] containing one zero digit followed by the
8794  * dividend's digits (plus appended zeroes to reach the desired precision
8795  * including guard digits). Each step of the main loop computes an
8796  * (approximate) quotient digit and stores it into div[], removing one
8797  * position of dividend space. A final pass of carry propagation takes
8798  * care of any mistaken quotient digits.
8799  *
8800  * Note that div[] doesn't necessarily contain all of the digits from the
8801  * dividend --- the desired precision plus guard digits might be less than
8802  * the dividend's precision. This happens, for example, in the square
8803  * root algorithm, where we typically divide a 2N-digit number by an
8804  * N-digit number, and only require a result with N digits of precision.
8805  */
8806  div = (int *) palloc0((div_ndigits + 1) * sizeof(int));
8807  load_ndigits = Min(div_ndigits, var1ndigits);
8808  for (i = 0; i < load_ndigits; i++)
8809  div[i + 1] = var1digits[i];
8810 
8811  /*
8812  * We estimate each quotient digit using floating-point arithmetic, taking
8813  * the first four digits of the (current) dividend and divisor. This must
8814  * be float to avoid overflow. The quotient digits will generally be off
8815  * by no more than one from the exact answer.
8816  */
8817  fdivisor = (double) var2digits[0];
8818  for (i = 1; i < 4; i++)
8819  {
8820  fdivisor *= NBASE;
8821  if (i < var2ndigits)
8822  fdivisor += (double) var2digits[i];
8823  }
8824  fdivisorinverse = 1.0 / fdivisor;
8825 
8826  /*
8827  * maxdiv tracks the maximum possible absolute value of any div[] entry;
8828  * when this threatens to exceed INT_MAX, we take the time to propagate
8829  * carries. Furthermore, we need to ensure that overflow doesn't occur
8830  * during the carry propagation passes either. The carry values may have
8831  * an absolute value as high as INT_MAX/NBASE + 1, so really we must
8832  * normalize when digits threaten to exceed INT_MAX - INT_MAX/NBASE - 1.
8833  *
8834  * To avoid overflow in maxdiv itself, it represents the max absolute
8835  * value divided by NBASE-1, ie, at the top of the loop it is known that
8836  * no div[] entry has an absolute value exceeding maxdiv * (NBASE-1).
8837  *
8838  * Actually, though, that holds good only for div[] entries after div[qi];
8839  * the adjustment done at the bottom of the loop may cause div[qi + 1] to
8840  * exceed the maxdiv limit, so that div[qi] in the next iteration is
8841  * beyond the limit. This does not cause problems, as explained below.
8842  */
8843  maxdiv = 1;
8844 
8845  /*
8846  * Outer loop computes next quotient digit, which will go into div[qi]
8847  */
8848  for (qi = 0; qi < div_ndigits; qi++)
8849  {
8850  /* Approximate the current dividend value */
8851  fdividend = (double) div[qi];
8852  for (i = 1; i < 4; i++)
8853  {
8854  fdividend *= NBASE;
8855  if (qi + i <= div_ndigits)
8856  fdividend += (double) div[qi + i];
8857  }
8858  /* Compute the (approximate) quotient digit */
8859  fquotient = fdividend * fdivisorinverse;
8860  qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
8861  (((int) fquotient) - 1); /* truncate towards -infinity */
8862 
8863  if (qdigit != 0)
8864  {
8865  /* Do we need to normalize now? */
8866  maxdiv += Abs(qdigit);
8867  if (maxdiv > (INT_MAX - INT_MAX / NBASE - 1) / (NBASE - 1))
8868  {
8869  /*
8870  * Yes, do it. Note that if var2ndigits is much smaller than
8871  * div_ndigits, we can save a significant amount of effort
8872  * here by noting that we only need to normalise those div[]
8873  * entries touched where prior iterations subtracted multiples
8874  * of the divisor.
8875  */
8876  carry = 0;
8877  for (i = Min(qi + var2ndigits - 2, div_ndigits); i > qi; i--)
8878  {
8879  newdig = div[i] + carry;
8880  if (newdig < 0)
8881  {
8882  carry = -((-newdig - 1) / NBASE) - 1;
8883  newdig -= carry * NBASE;
8884  }
8885  else if (newdig >= NBASE)
8886  {
8887  carry = newdig / NBASE;
8888  newdig -= carry * NBASE;
8889  }
8890  else
8891  carry = 0;
8892  div[i] = newdig;
8893  }
8894  newdig = div[qi] + carry;
8895  div[qi] = newdig;
8896 
8897  /*
8898  * All the div[] digits except possibly div[qi] are now in the
8899  * range 0..NBASE-1. We do not need to consider div[qi] in
8900  * the maxdiv value anymore, so we can reset maxdiv to 1.
8901  */
8902  maxdiv = 1;
8903 
8904  /*
8905  * Recompute the quotient digit since new info may have
8906  * propagated into the top four dividend digits
8907  */
8908  fdividend = (double) div[qi];
8909  for (i = 1; i < 4; i++)
8910  {
8911  fdividend *= NBASE;
8912  if (qi + i <= div_ndigits)
8913  fdividend += (double) div[qi + i];
8914  }
8915  /* Compute the (approximate) quotient digit */
8916  fquotient = fdividend * fdivisorinverse;
8917  qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
8918  (((int) fquotient) - 1); /* truncate towards -infinity */
8919  maxdiv += Abs(qdigit);
8920  }
8921 
8922  /*
8923  * Subtract off the appropriate multiple of the divisor.
8924  *
8925  * The digits beyond div[qi] cannot overflow, because we know they
8926  * will fall within the maxdiv limit. As for div[qi] itself, note
8927  * that qdigit is approximately trunc(div[qi] / vardigits[0]),
8928  * which would make the new value simply div[qi] mod vardigits[0].
8929  * The lower-order terms in qdigit can change this result by not
8930  * more than about twice INT_MAX/NBASE, so overflow is impossible.
8931  */
8932  if (qdigit != 0)
8933  {
8934  int istop = Min(var2ndigits, div_ndigits - qi + 1);
8935 
8936  for (i = 0; i < istop; i++)
8937  div[qi + i] -= qdigit * var2digits[i];
8938  }
8939  }
8940 
8941  /*
8942  * The dividend digit we are about to replace might still be nonzero.
8943  * Fold it into the next digit position.
8944  *
8945  * There is no risk of overflow here, although proving that requires
8946  * some care. Much as with the argument for div[qi] not overflowing,
8947  * if we consider the first two terms in the numerator and denominator
8948  * of qdigit, we can see that the final value of div[qi + 1] will be
8949  * approximately a remainder mod (vardigits[0]*NBASE + vardigits[1]).
8950  * Accounting for the lower-order terms is a bit complicated but ends
8951  * up adding not much more than INT_MAX/NBASE to the possible range.
8952  * Thus, div[qi + 1] cannot overflow here, and in its role as div[qi]
8953  * in the next loop iteration, it can't be large enough to cause
8954  * overflow in the carry propagation step (if any), either.
8955  *
8956  * But having said that: div[qi] can be more than INT_MAX/NBASE, as
8957  * noted above, which means that the product div[qi] * NBASE *can*
8958  * overflow. When that happens, adding it to div[qi + 1] will always
8959  * cause a canceling overflow so that the end result is correct. We
8960  * could avoid the intermediate overflow by doing the multiplication
8961  * and addition in int64 arithmetic, but so far there appears no need.
8962  */
8963  div[qi + 1] += div[qi] * NBASE;
8964 
8965  div[qi] = qdigit;
8966  }
8967 
8968  /*
8969  * Approximate and store the last quotient digit (div[div_ndigits])
8970  */
8971  fdividend = (double) div[qi];
8972  for (i = 1; i < 4; i++)
8973  fdividend *= NBASE;
8974  fquotient = fdividend * fdivisorinverse;
8975  qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
8976  (((int) fquotient) - 1); /* truncate towards -infinity */
8977  div[qi] = qdigit;
8978 
8979  /*
8980  * Because the quotient digits might be off by one, some of them might be
8981  * -1 or NBASE at this point. The represented value is correct in a
8982  * mathematical sense, but it doesn't look right. We do a final carry
8983  * propagation pass to normalize the digits, which we combine with storing
8984  * the result digits into the output. Note that this is still done at
8985  * full precision w/guard digits.
8986  */
8987  alloc_var(result, div_ndigits + 1);
8988  res_digits = result->digits;
8989  carry = 0;
8990  for (i = div_ndigits; i >= 0; i--)
8991  {
8992  newdig = div[i] + carry;
8993  if (newdig < 0)
8994  {
8995  carry = -((-newdig - 1) / NBASE) - 1;
8996  newdig -= carry * NBASE;
8997  }
8998  else if (newdig >= NBASE)
8999  {
9000  carry = newdig / NBASE;
9001  newdig -= carry * NBASE;
9002  }
9003  else
9004  carry = 0;
9005  res_digits[i] = newdig;
9006  }
9007  Assert(carry == 0);
9008 
9009  pfree(div);
9010 
9011  /*
9012  * Finally, round the result to the requested precision.
9013  */
9014  result->weight = res_weight;
9015  result->sign = res_sign;
9016 
9017  /* Round to target rscale (and set result->dscale) */
9018  if (round)
9019  round_var(result, rscale);
9020  else
9021  trunc_var(result, rscale);
9022 
9023  /* Strip leading and trailing zeroes */
9024  strip_var(result);
9025 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:10787
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:10893
int weight
Definition: numeric.c:308
#define NUMERIC_POS
Definition: numeric.c:167
static void strip_var(NumericVar *var)
Definition: numeric.c:10955
int errcode(int sqlerrcode)
Definition: elog.c:698
int ndigits
Definition: numeric.c:307
int dscale
Definition: numeric.c:310
#define Min(x, y)
Definition: numeric.c:14
#define Abs(x)
Definition: c.h:992
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:309
static void zero_var(NumericVar *var)
Definition: numeric.c:6797
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
int16 NumericDigit
Definition: numeric.c:103
#define NBASE
Definition: numeric.c:97
void * palloc0(Size size)
Definition: mcxt.c:1093
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
NumericDigit * digits
Definition: numeric.c:312
#define DIV_GUARD_DIGITS
Definition: numeric.c:101
int errmsg(const char *fmt,...)
Definition: elog.c:909
static void alloc_var(NumericVar *var, int ndigits)
Definition: numeric.c:6765
int i
#define DEC_DIGITS
Definition: numeric.c:99

◆ do_numeric_accum()

static void do_numeric_accum ( NumericAggState state,
Numeric  newval 
)
static

Definition at line 4661 of file numeric.c.

References accum_sum_add(), NumericAggState::agg_context, NumericAggState::calcSumX2, NumericVar::dscale, init_var, init_var_from_num(), NumericAggState::maxScale, NumericAggState::maxScaleCount, MemoryContextSwitchTo(), mul_var(), NumericAggState::N, NumericAggState::NaNcount, NumericAggState::nInfcount, NUMERIC_IS_NINF, NUMERIC_IS_PINF, NUMERIC_IS_SPECIAL, NumericAggState::pInfcount, NumericAggState::sumX, and NumericAggState::sumX2.

Referenced by int2_accum(), int4_accum(), int8_accum(), int8_avg_accum(), numeric_accum(), and numeric_avg_accum().

4662 {
4663  NumericVar X;
4664  NumericVar X2;
4665  MemoryContext old_context;
4666 
4667  /* Count NaN/infinity inputs separately from all else */
4668  if (NUMERIC_IS_SPECIAL(newval))
4669  {
4670  if (NUMERIC_IS_PINF(newval))
4671  state->pInfcount++;
4672  else if (NUMERIC_IS_NINF(newval))
4673  state->nInfcount++;
4674  else
4675  state->NaNcount++;
4676  return;
4677  }
4678 
4679  /* load processed number in short-lived context */
4680  init_var_from_num(newval, &X);
4681 
4682  /*
4683  * Track the highest input dscale that we've seen, to support inverse
4684  * transitions (see do_numeric_discard).
4685  */
4686  if (X.dscale > state->maxScale)
4687  {
4688  state->maxScale = X.dscale;
4689  state->maxScaleCount = 1;
4690  }
4691  else if (X.dscale == state->maxScale)
4692  state->maxScaleCount++;
4693 
4694  /* if we need X^2, calculate that in short-lived context */
4695  if (state->calcSumX2)
4696  {
4697  init_var(&X2);
4698  mul_var(&X, &X, &X2, X.dscale * 2);
4699  }
4700 
4701  /* The rest of this needs to work in the aggregate context */
4702  old_context = MemoryContextSwitchTo(state->agg_context);
4703 
4704  state->N++;
4705 
4706  /* Accumulate sums */
4707  accum_sum_add(&(state->sumX), &X);
4708 
4709  if (state->calcSumX2)
4710  accum_sum_add(&(state->sumX2), &X2);
4711 
4712  MemoryContextSwitchTo(old_context);
4713 }
int64 nInfcount
Definition: numeric.c:4610
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext agg_context
Definition: numeric.c:4601
int dscale
Definition: numeric.c:310
#define NUMERIC_IS_SPECIAL(n)
Definition: numeric.c:174
static void accum_sum_add(NumericSumAccum *accum, const NumericVar *var1)
Definition: numeric.c:11012
#define NUMERIC_IS_PINF(n)
Definition: numeric.c:206
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:7014
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:8235
#define NUMERIC_IS_NINF(n)
Definition: numeric.c:207
NumericSumAccum sumX2
Definition: numeric.c:4604
int64 pInfcount
Definition: numeric.c:4609
int64 NaNcount
Definition: numeric.c:4608
int64 maxScaleCount
Definition: numeric.c:4606
NumericSumAccum sumX
Definition: numeric.c:4603
#define init_var(v)
Definition: numeric.c:496

◆ do_numeric_discard()

static bool do_numeric_discard ( NumericAggState state,
Numeric  newval 
)
static

Definition at line 4731 of file numeric.c.

References accum_sum_add(), accum_sum_reset(), NumericAggState::agg_context, Assert, NumericAggState::calcSumX2, NumericVar::dscale, init_var, init_var_from_num(), NumericAggState::maxScale, NumericAggState::maxScaleCount, MemoryContextSwitchTo(), mul_var(), NumericAggState::N, NumericAggState::NaNcount, NumericAggState::nInfcount, NUMERIC_IS_NINF, NUMERIC_IS_PINF, NUMERIC_IS_SPECIAL, NUMERIC_NEG, NUMERIC_POS, NumericAggState::pInfcount, NumericVar::sign, NumericAggState::sumX, and NumericAggState::sumX2.

Referenced by int2_accum_inv(), int4_accum_inv(), int8_accum_inv(), int8_avg_accum_inv(), and numeric_accum_inv().

4732 {
4733  NumericVar X;
4734  NumericVar X2;
4735  MemoryContext old_context;
4736 
4737  /* Count NaN/infinity inputs separately from all else */
4738  if (NUMERIC_IS_SPECIAL(newval))
4739  {
4740  if (NUMERIC_IS_PINF(newval))
4741  state->pInfcount--;
4742  else if (NUMERIC_IS_NINF(newval))
4743  state->nInfcount--;
4744  else
4745  state->NaNcount--;
4746  return true;
4747  }
4748 
4749  /* load processed number in short-lived context */
4750  init_var_from_num(newval, &X);
4751 
4752  /*
4753  * state->sumX's dscale is the maximum dscale of any of the inputs.
4754  * Removing the last input with that dscale would require us to recompute
4755  * the maximum dscale of the *remaining* inputs, which we cannot do unless
4756  * no more non-NaN inputs remain at all. So we report a failure instead,
4757  * and force the aggregation to be redone from scratch.
4758  */
4759  if (X.dscale == state->maxScale)
4760  {
4761  if (state->maxScaleCount > 1 || state->maxScale == 0)
4762  {
4763  /*
4764  * Some remaining inputs have same dscale, or dscale hasn't gotten
4765  * above zero anyway
4766  */
4767  state->maxScaleCount--;
4768  }
4769  else if (state->N == 1)
4770  {
4771  /* No remaining non-NaN inputs at all, so reset maxScale */
4772  state->maxScale = 0;
4773  state->maxScaleCount = 0;
4774  }
4775  else
4776  {
4777  /* Correct new maxScale is uncertain, must fail */
4778  return false;
4779  }
4780  }
4781 
4782  /* if we need X^2, calculate that in short-lived context */
4783  if (state->calcSumX2)
4784  {
4785  init_var(&X2);
4786  mul_var(&X, &X, &X2, X.dscale * 2);
4787  }
4788 
4789  /* The rest of this needs to work in the aggregate context */
4790  old_context = MemoryContextSwitchTo(state->agg_context);
4791 
4792  if (state->N-- > 1)
4793  {
4794  /* Negate X, to subtract it from the sum */
4795  X.sign = (X.sign == NUMERIC_POS ? NUMERIC_NEG : NUMERIC_POS);
4796  accum_sum_add(&(state->sumX), &X);
4797 
4798  if (state->calcSumX2)
4799  {
4800  /* Negate X^2. X^2 is always positive */
4801  X2.sign = NUMERIC_NEG;
4802  accum_sum_add(&(state->sumX2), &X2);
4803  }
4804  }
4805  else
4806  {
4807  /* Zero the sums */
4808  Assert(state->N == 0);
4809 
4810  accum_sum_reset(&state->sumX);
4811  if (state->calcSumX2)
4812  accum_sum_reset(&state->sumX2);
4813  }
4814 
4815  MemoryContextSwitchTo(old_context);
4816 
4817  return true;
4818 }
int64 nInfcount
Definition: numeric.c:4610
#define NUMERIC_POS
Definition: numeric.c:167
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext agg_context
Definition: numeric.c:4601
int dscale
Definition: numeric.c:310
#define NUMERIC_IS_SPECIAL(n)
Definition: numeric.c:174
#define NUMERIC_NEG
Definition: numeric.c:168
static void accum_sum_add(NumericSumAccum *accum, const NumericVar *var1)
Definition: numeric.c:11012
int sign
Definition: numeric.c:309
#define NUMERIC_IS_PINF(n)
Definition: numeric.c:206
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:7014
#define Assert(condition)
Definition: c.h:804
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:8235
#define NUMERIC_IS_NINF(n)
Definition: numeric.c:207
NumericSumAccum sumX2
Definition: numeric.c:4604
static void accum_sum_reset(NumericSumAccum *accum)
Definition: numeric.c:10996
int64 pInfcount
Definition: numeric.c:4609
int64 NaNcount
Definition: numeric.c:4608
int64 maxScaleCount
Definition: numeric.c:4606
NumericSumAccum sumX
Definition: numeric.c:4603
#define init_var(v)
Definition: numeric.c:496

◆ duplicate_numeric()

static Numeric duplicate_numeric ( Numeric  num)
static

Definition at line 7337 of file numeric.c.

References palloc(), and VARSIZE.

Referenced by numeric(), numeric_abs(), numeric_ceil(), numeric_exp(), numeric_floor(), numeric_inc(), numeric_ln(), numeric_mod_opt_error(), numeric_round(), numeric_sqrt(), numeric_trim_scale(), numeric_trunc(), numeric_uminus(), and numeric_uplus().

7338 {
7339  Numeric res;
7340 
7341  res = (Numeric) palloc(VARSIZE(num));
7342  memcpy(res, num, VARSIZE(num));
7343  return res;
7344 }
#define VARSIZE(PTR)
Definition: postgres.h:316
struct NumericData * Numeric
Definition: numeric.h:53
void * palloc(Size size)
Definition: mcxt.c:1062

◆ estimate_ln_dweight()

static int estimate_ln_dweight ( const NumericVar var)
static

Definition at line 9931 of file numeric.c.

References Abs, cmp_var(), DEC_DIGITS, NumericVar::digits, digits, free_var(), init_var, ln_var(), NBASE, NumericVar::ndigits, sub_var(), and NumericVar::weight.

Referenced by log_var(), numeric_ln(), and power_var().

9932 {
9933  int ln_dweight;
9934 
9935  if (cmp_var(var, &const_zero_point_nine) >= 0 &&
9936  cmp_var(var, &const_one_point_one) <= 0)
9937  {
9938  /*
9939  * 0.9 <= var <= 1.1
9940  *
9941  * ln(var) has a negative weight (possibly very large). To get a
9942  * reasonably accurate result, estimate it using ln(1+x) ~= x.
9943  */
9944  NumericVar x;
9945 
9946  init_var(&x);
9947  sub_var(var, &const_one, &x);
9948 
9949  if (x.ndigits > 0)
9950  {
9951  /* Use weight of most significant decimal digit of x */
9952  ln_dweight = x.weight * DEC_DIGITS + (int) log10(x.digits[0]);
9953  }
9954  else
9955  {
9956  /* x = 0. Since ln(1) = 0 exactly, we don't need extra digits */
9957  ln_dweight = 0;
9958  }
9959 
9960  free_var(&x);
9961  }
9962  else
9963  {
9964  /*
9965  * Estimate the logarithm using the first couple of digits from the
9966  * input number. This will give an accurate result whenever the input
9967  * is not too close to 1.
9968  */
9969  if (var->ndigits > 0)
9970  {
9971  int digits;
9972  int dweight;
9973  double ln_var;
9974 
9975  digits = var->digits[0];
9976  dweight = var->weight * DEC_DIGITS;
9977 
9978  if (var->ndigits > 1)
9979  {
9980  digits = digits * NBASE + var->digits[1];
9981  dweight -= DEC_DIGITS;
9982  }
9983 
9984  /*----------
9985  * We have var ~= digits * 10^dweight
9986  * so ln(var) ~= ln(digits) + dweight * ln(10)
9987  *----------
9988  */
9989  ln_var = log((double) digits) + dweight * 2.302585092994046;
9990  ln_dweight = (int) log10(Abs(ln_var));
9991  }
9992  else
9993  {
9994  /* Caller should fail on ln(0), but for the moment return zero */
9995  ln_dweight = 0;
9996  }
9997  }
9998 
9999  return ln_dweight;
10000 }
int weight
Definition: numeric.c:308
static const NumericVar const_zero_point_nine
Definition: numeric.c:448
static const NumericVar const_one
Definition: numeric.c:421
static void ln_var(const NumericVar *arg, NumericVar *result, int rscale)
Definition: numeric.c:10009
int ndigits
Definition: numeric.c:307
#define Abs(x)
Definition: c.h:992
#define NBASE
Definition: numeric.c:97
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:7939
static void free_var(NumericVar *var)
Definition: numeric.c:6781
NumericDigit * digits
Definition: numeric.c:312
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8114
#define DEC_DIGITS
Definition: numeric.c:99
static const NumericVar const_one_point_one
Definition: numeric.c:458
#define init_var(v)
Definition: numeric.c:496
int digits
Definition: informix.c:666

◆ exp_var()

static void exp_var ( const NumericVar arg,
NumericVar result,
int  rscale 
)
static

Definition at line 9799 of file numeric.c.

References Abs, add_var(), DEC_DIGITS, div_var_fast(), NumericVar::dscale, ereport, errcode(), errmsg(), ERROR, free_var(), init_var, Max, mul_var(), NumericVar::ndigits, NUMERIC_MAX_RESULT_SCALE, NUMERIC_MIN_DISPLAY_SCALE, numericvar_to_double_no_overflow(), round_var(), set_var_from_var(), val, NumericVar::weight, and zero_var().

Referenced by numeric_exp(), and power_var().

9800 {
9801  NumericVar x;
9802  NumericVar elem;
9803  NumericVar ni;
9804  double val;
9805  int dweight;
9806  int ndiv2;
9807  int sig_digits;
9808  int local_rscale;
9809 
9810  init_var(&x);
9811  init_var(&elem);
9812  init_var(&ni);
9813 
9814  set_var_from_var(arg, &x);
9815 
9816  /*
9817  * Estimate the dweight of the result using floating point arithmetic, so
9818  * that we can choose an appropriate local rscale for the calculation.
9819  */
9821 
9822  /* Guard against overflow/underflow */
9823  /* If you change this limit, see also power_var()'s limit */
9824  if (Abs(val) >= NUMERIC_MAX_RESULT_SCALE * 3)
9825  {
9826  if (val > 0)
9827  ereport(ERROR,
9828  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
9829  errmsg("value overflows numeric format")));
9830  zero_var(result);
9831  result->dscale = rscale;
9832  return;
9833  }
9834 
9835  /* decimal weight = log10(e^x) = x * log10(e) */
9836  dweight = (int) (val * 0.434294481903252);
9837 
9838  /*
9839  * Reduce x to the range -0.01 <= x <= 0.01 (approximately) by dividing by
9840  * 2^n, to improve the convergence rate of the Taylor series.
9841  */
9842  if (Abs(val) > 0.01)
9843  {
9844  NumericVar tmp;
9845 
9846  init_var(&tmp);
9847  set_var_from_var(&const_two, &tmp);
9848 
9849  ndiv2 = 1;
9850  val /= 2;
9851 
9852  while (Abs(val) > 0.01)
9853  {
9854  ndiv2++;
9855  val /= 2;
9856  add_var(&tmp, &tmp, &tmp);
9857  }
9858 
9859  local_rscale = x.dscale + ndiv2;
9860  div_var_fast(&x, &tmp, &x, local_rscale, true);
9861 
9862  free_var(&tmp);
9863  }
9864  else
9865  ndiv2 = 0;
9866 
9867  /*
9868  * Set the scale for the Taylor series expansion. The final result has
9869  * (dweight + rscale + 1) significant digits. In addition, we have to
9870  * raise the Taylor series result to the power 2^ndiv2, which introduces
9871  * an error of up to around log10(2^ndiv2) digits, so work with this many
9872  * extra digits of precision (plus a few more for good measure).
9873  */
9874  sig_digits = 1 + dweight + rscale + (int) (ndiv2 * 0.301029995663981);
9875  sig_digits = Max(sig_digits, 0) + 8;
9876 
9877  local_rscale = sig_digits - 1;
9878 
9879  /*
9880  * Use the Taylor series
9881  *
9882  * exp(x) = 1 + x + x^2/2! + x^3/3! + ...
9883  *
9884  * Given the limited range of x, this should converge reasonably quickly.
9885  * We run the series until the terms fall below the local_rscale limit.
9886  */
9887  add_var(&const_one, &x, result);
9888 
9889  mul_var(&x, &x, &elem, local_rscale);
9890  set_var_from_var(&const_two, &ni);
9891  div_var_fast(&elem, &ni, &elem, local_rscale, true);
9892 
9893  while (elem.ndigits != 0)
9894  {
9895  add_var(result, &elem, result);
9896 
9897  mul_var(&elem, &x, &elem, local_rscale);
9898  add_var(&ni, &const_one, &ni);
9899  div_var_fast(&elem, &ni, &elem, local_rscale, true);
9900  }
9901 
9902  /*
9903  * Compensate for the argument range reduction. Since the weight of the
9904  * result doubles with each multiplication, we can reduce the local rscale
9905  * as we proceed.
9906  */
9907  while (ndiv2-- > 0)
9908  {
9909  local_rscale = sig_digits - result->weight * 2 * DEC_DIGITS;
9910  local_rscale = Max(local_rscale, NUMERIC_MIN_DISPLAY_SCALE);
9911  mul_var(result, result, result, local_rscale);
9912  }
9913 
9914  /* Round to requested rscale */
9915  round_var(result, rscale);
9916 
9917  free_var(&x);
9918  free_var(&elem);
9919  free_var(&ni);
9920 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:10787
int weight
Definition: numeric.c:308
static const NumericVar const_one
Definition: numeric.c:421
int errcode(int sqlerrcode)
Definition: elog.c:698
int ndigits
Definition: numeric.c:307
int dscale
Definition: numeric.c:310
#define Abs(x)
Definition: c.h:992
static void zero_var(NumericVar *var)
Definition: numeric.c:6797
#define ERROR
Definition: elog.h:46
static double numericvar_to_double_no_overflow(const NumericVar *var)
Definition: numeric.c:7907
#define NUMERIC_MIN_DISPLAY_SCALE
Definition: numeric.h:40
#define NUMERIC_MAX_RESULT_SCALE
Definition: numeric.h:42
static void div_var_fast(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:8728
static void free_var(NumericVar *var)
Definition: numeric.c:6781
#define ereport(elevel,...)
Definition: elog.h:157
static const NumericVar const_two
Definition: numeric.c:428
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:8235
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7997
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define Max(x, y)
Definition: numeric.c:13
#define DEC_DIGITS
Definition: numeric.c:99
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:7031
long val
Definition: informix.c:664
#define init_var(v)
Definition: numeric.c:496

◆ float4_numeric()

Datum float4_numeric ( PG_FUNCTION_ARGS  )

Definition at line 4492 of file numeric.c.

References buf, free_var(), init_var, make_result(), PG_GETARG_FLOAT4, PG_RETURN_NUMERIC, set_var_from_str(), snprintf, and val.

4493 {
4495  Numeric res;
4496  NumericVar result;
4497  char buf[FLT_DIG + 100];
4498 
4499  if (isnan(val))
4501 
4502  if (isinf(val))
4503  {
4504  if (val < 0)
4506  else
4508  }
4509 
4510  snprintf(buf, sizeof(buf), "%.*g", FLT_DIG, val);
4511 
4512  init_var(&result);
4513 
4514  /* Assume we need not worry about leading/trailing spaces */
4515  (void) set_var_from_str(buf, buf, &result);
4516 
4517  res = make_result(&result);
4518 
4519  free_var(&result);
4520 
4521  PG_RETURN_NUMERIC(res);
4522 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:64
static char * buf
Definition: pg_test_fsync.c:68
static const NumericVar const_ninf
Definition: numeric.c:467
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:281
float float4
Definition: c.h:564
static void free_var(NumericVar *var)
Definition: numeric.c:6781
static const NumericVar const_pinf
Definition: numeric.c:464
static const char * set_var_from_str(const char *str, const char *cp, NumericVar *dest)
Definition: numeric.c:6821
static const NumericVar const_nan
Definition: numeric.c:461
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:7465
#define snprintf
Definition: port.h:216
long val
Definition: informix.c:664
#define init_var(v)
Definition: numeric.c:496

◆ float8_numeric()

Datum float8_numeric ( PG_FUNCTION_ARGS  )

Definition at line 4399 of file numeric.c.

References buf, free_var(), init_var, make_result(), PG_GETARG_FLOAT8, PG_RETURN_NUMERIC, set_var_from_str(), snprintf, and val.

Referenced by executeItemOptUnwrapTarget(), and SV_to_JsonbValue().

4400 {
4402  Numeric res;
4403  NumericVar result;
4404  char buf[DBL_DIG + 100];
4405 
4406  if (isnan(val))
4408 
4409  if (isinf(val))
4410  {
4411  if (val < 0)
4413  else
4415  }
4416 
4417  snprintf(buf, sizeof(buf), "%.*g", DBL_DIG, val);
4418 
4419  init_var(&result);
4420 
4421  /* Assume we need not worry about leading/trailing spaces */
4422  (void) set_var_from_str(buf, buf, &result);
4423 
4424  res = make_result(&result);
4425 
4426  free_var(&result);
4427 
4428  PG_RETURN_NUMERIC(res);
4429 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:64
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
double float8
Definition: c.h:565
static char * buf
Definition: pg_test_fsync.c:68
static const NumericVar const_ninf
Definition: numeric.c:467
static void free_var(NumericVar *var)
Definition: numeric.c:6781
static const NumericVar const_pinf
Definition: numeric.c:464
static const char * set_var_from_str(const char *str, const char *cp, NumericVar *dest)
Definition: numeric.c:6821
static const NumericVar const_nan
Definition: numeric.c:461
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:7465
#define snprintf
Definition: port.h:216
long val
Definition: informix.c:664
#define init_var(v)
Definition: numeric.c:496

◆ floor_var()

static void floor_var ( const NumericVar var,
NumericVar result 
)
static

Definition at line 9226 of file numeric.c.

References cmp_var(), free_var(), init_var, NUMERIC_NEG, set_var_from_var(), NumericVar::sign, sub_var(), and trunc_var().

Referenced by compute_bucket(), and numeric_floor().

9227 {
9228  NumericVar tmp;
9229 
9230  init_var(&tmp);
9231  set_var_from_var(var, &tmp);
9232 
9233  trunc_var(&tmp, 0);
9234 
9235  if (var->sign == NUMERIC_NEG && cmp_var(var, &tmp) != 0)
9236  sub_var(&tmp, &const_one, &tmp);
9237 
9238  set_var_from_var(&tmp, result);
9239  free_var(&tmp);
9240 }
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:10893
static const NumericVar const_one
Definition: numeric.c:421
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:309
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:7939
static void free_var(NumericVar *var)
Definition: numeric.c:6781
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8114
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:7031
#define init_var(v)
Definition: numeric.c:496

◆ free_var()

static void free_var ( NumericVar var)
static

Definition at line 6781 of file numeric.c.

References NumericVar::buf, digitbuf_free, NumericVar::digits, NUMERIC_NAN, and NumericVar::sign.

Referenced by accum_sum_combine(), ceil_var(), compute_bucket(), div_mod_var(), estimate_ln_dweight(), exp_var(), float4_numeric(), float8_numeric(), floor_var(), gcd_var(), get_str_from_var_sci(), in_range_numeric_numeric(), int64_div_fast_to_numeric(), int64_to_numeric(), int8_avg_deserialize(), int8_avg_serialize(), ln_var(), log_var(), mod_var(), numeric(), numeric_add_opt_error(), numeric_avg(), numeric_avg_deserialize(), numeric_avg_serialize(), numeric_ceil(), numeric_deserialize(), numeric_div_opt_error(), numeric_div_trunc(), numeric_exp(), numeric_fac(), numeric_floor(), numeric_gcd(), numeric_in(), numeric_inc(), numeric_lcm(), numeric_ln(), numeric_log(), numeric_min_scale(), numeric_mod_opt_error(), numeric_mul_opt_error(), numeric_poly_avg(), numeric_poly_deserialize(), numeric_poly_serialize(), numeric_poly_sum(), numeric_power(), numeric_recv(), numeric_round(), numeric_serialize(), numeric_sqrt(), numeric_stddev_internal(), numeric_stddev_pop(), numeric_sub_opt_error(), numeric_sum(), numeric_trim_scale(), numeric_trunc(), numericvar_to_int64(), numericvar_to_uint64(), power_var(), power_var_int(), sqrt_var(), and width_bucket_numeric().

6782 {
6783  digitbuf_free(var->buf);
6784  var->buf = NULL;
6785  var->digits = NULL;
6786  var->sign = NUMERIC_NAN;
6787 }
int sign
Definition: numeric.c:309
#define digitbuf_free(buf)
Definition: numeric.c:490
#define NUMERIC_NAN
Definition: numeric.c:199
NumericDigit * buf
Definition: numeric.c:311
NumericDigit * digits
Definition: numeric.c:312

◆ gcd_var()

static void gcd_var ( const NumericVar var1,
const NumericVar var2,
NumericVar result 
)
static

Definition at line 9249 of file numeric.c.

References CHECK_FOR_INTERRUPTS, cmp(), cmp_abs(), NumericVar::dscale, free_var(), init_var, Max, mod_var(), NumericVar::ndigits, NUMERIC_POS, set_var_from_var(), and NumericVar::sign.

Referenced by numeric_gcd(), and numeric_lcm().

9250 {
9251  int res_dscale;
9252  int cmp;
9253  NumericVar tmp_arg;
9254  NumericVar mod;
9255 
9256  res_dscale = Max(var1->dscale, var2->dscale);
9257 
9258  /*
9259  * Arrange for var1 to be the number with the greater absolute value.
9260  *
9261  * This would happen automatically in the loop below, but avoids an
9262  * expensive modulo operation.
9263  */
9264  cmp = cmp_abs(var1, var2);
9265  if (cmp < 0)
9266  {
9267  const NumericVar *tmp = var1;
9268 
9269  var1 = var2;
9270  var2 = tmp;
9271  }
9272 
9273  /*
9274  * Also avoid the taking the modulo if the inputs have the same absolute
9275  * value, or if the smaller input is zero.
9276  */
9277  if (cmp == 0 || var2->ndigits == 0)
9278  {
9279  set_var_from_var(var1, result);
9280  result->sign = NUMERIC_POS;
9281  result->dscale = res_dscale;
9282  return;
9283  }
9284 
9285  init_var(&tmp_arg);
9286  init_var(&mod);
9287 
9288  /* Use the Euclidean algorithm to find the GCD */
9289  set_var_from_var(var1, &tmp_arg);
9290  set_var_from_var(var2, result);
9291 
9292  for (;;)
9293  {
9294  /* this loop can take a while, so allow it to be interrupted */
9296 
9297  mod_var(&tmp_arg, result, &mod);
9298  if (mod.ndigits == 0)
9299  break;
9300  set_var_from_var(result, &tmp_arg);
9301  set_var_from_var(&mod, result);
9302  }
9303  result->sign = NUMERIC_POS;
9304  result->dscale = res_dscale;
9305 
9306  free_var(&tmp_arg);
9307  free_var(&mod);
9308 }
#define NUMERIC_POS
Definition: numeric.c:167
int ndigits
Definition: numeric.c:307
int dscale
Definition: numeric.c:310
int sign
Definition: numeric.c:309
static void mod_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:9103
static void free_var(NumericVar *var)
Definition: numeric.c:6781
static int cmp_abs(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:10542
#define Max(x, y)
Definition: numeric.c:13
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:7031
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
#define init_var(v)
Definition: numeric.c:496
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:747

◆ generate_series_numeric()

Datum generate_series_numeric ( PG_FUNCTION_ARGS  )

Definition at line 1622 of file numeric.c.

References generate_series_step_numeric().

1623 {
1624  return generate_series_step_numeric(fcinfo);
1625 }
Datum generate_series_step_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:1628

◆ generate_series_step_numeric()

Datum generate_series_step_numeric ( PG_FUNCTION_ARGS  )

Definition at line 1628 of file numeric.c.

References add_var(), cmp_var(), const_one, generate_series_numeric_fctx::current, ereport, errcode(), errmsg(), ERROR, init_var, init_var_from_num(), make_result(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, NUMERIC_IS_NAN, NUMERIC_IS_SPECIAL, NUMERIC_NEG, NUMERIC_POS, NumericGetDatum, palloc(), PG_GETARG_NUMERIC, PG_NARGS, set_var_from_num(), set_var_from_var(), NumericVar::sign, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, generate_series_numeric_fctx::step, generate_series_numeric_fctx::stop, and FuncCallContext::user_fctx.

Referenced by generate_series_numeric().

1629 {
1631  FuncCallContext *funcctx;
1632  MemoryContext oldcontext;
1633 
1634  if (SRF_IS_FIRSTCALL())
1635  {
1636  Numeric start_num = PG_GETARG_NUMERIC(0);
1637  Numeric stop_num = PG_GETARG_NUMERIC(1);
1638  NumericVar steploc = const_one;
1639 
1640  /* Reject NaN and infinities in start and stop values */
1641  if (NUMERIC_IS_SPECIAL(start_num))
1642  {
1643  if (NUMERIC_IS_NAN(start_num))
1644  ereport(ERROR,
1645  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1646  errmsg("start value cannot be NaN")));
1647  else
1648  ereport(ERROR,
1649  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1650  errmsg("start value cannot be infinity")));
1651  }
1652  if (NUMERIC_IS_SPECIAL(stop_num))
1653  {
1654  if (NUMERIC_IS_NAN(stop_num))
1655  ereport(ERROR,
1656  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1657  errmsg("stop value cannot be NaN")));
1658  else
1659  ereport(ERROR,
1660  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1661  errmsg("stop value cannot be infinity")));
1662  }
1663 
1664  /* see if we were given an explicit step size */
1665  if (PG_NARGS() == 3)
1666  {
1667  Numeric step_num = PG_GETARG_NUMERIC(2);
1668 
1669  if (NUMERIC_IS_SPECIAL(step_num))
1670  {
1671  if (NUMERIC_IS_NAN(step_num))
1672  ereport(ERROR,
1673  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1674  errmsg("step size cannot be NaN")));
1675  else
1676  ereport(ERROR,
1677  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1678  errmsg("step size cannot be infinity")));
1679  }
1680 
1681  init_var_from_num(step_num, &steploc);
1682 
1683  if (cmp_var(&steploc, &const_zero) == 0)
1684  ereport(ERROR,
1685  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1686  errmsg("step size cannot equal zero")));
1687  }
1688 
1689  /* create a function context for cross-call persistence */
1690  funcctx = SRF_FIRSTCALL_INIT();
1691 
1692  /*
1693  * Switch to memory context appropriate for multiple function calls.
1694  */
1695  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1696 
1697  /* allocate memory for user context */
1698  fctx = (generate_series_numeric_fctx *)
1700 
1701  /*
1702  * Use fctx to keep state from call to call. Seed current with the
1703  * original start value. We must copy the start_num and stop_num
1704  * values rather than pointing to them, since we may have detoasted
1705  * them in the per-call context.
1706  */
1707  init_var(&fctx->current);
1708  init_var(&fctx->stop);
1709  init_var(&fctx->step);
1710 
1711  set_var_from_num(start_num, &fctx->current);
1712  set_var_from_num(stop_num, &fctx->stop);
1713  set_var_from_var(&steploc, &fctx->step);
1714 
1715  funcctx->user_fctx = fctx;
1716  MemoryContextSwitchTo(oldcontext);
1717  }
1718 
1719  /* stuff done on every call of the function */
1720  funcctx = SRF_PERCALL_SETUP();
1721 
1722  /*
1723  * Get the saved state and use current state as the result of this
1724  * iteration.
1725  */
1726  fctx = funcctx->user_fctx;
1727 
1728  if ((fctx->step.sign == NUMERIC_POS &&
1729  cmp_var(&fctx->current, &fctx->stop) <= 0) ||
1730  (fctx->step.sign == NUMERIC_NEG &&
1731  cmp_var(&fctx->current, &fctx->stop) >= 0))
1732  {
1733  Numeric result = make_result(&fctx->current);
1734 
1735  /* switch to memory context appropriate for iteration calculation */
1736  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1737 
1738  /* increment current in preparation for next iteration */
1739  add_var(&fctx->current, &fctx->step, &fctx->current);
1740  MemoryContextSwitchTo(oldcontext);
1741 
1742  /* do when there is more left to send */
1743  SRF_RETURN_NEXT(funcctx, NumericGetDatum(result));
1744  }
1745  else
1746  /* do when there is no more left */
1747  SRF_RETURN_DONE(funcctx);
1748 }
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
static const NumericVar const_one
Definition: numeric.c:421
#define NumericGetDatum(X)
Definition: numeric.h:61
#define NUMERIC_POS
Definition: numeric.c:167
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
#define NUMERIC_IS_SPECIAL(n)
Definition: numeric.c:174
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:309
#define ERROR
Definition: elog.h:46
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:7014
static const NumericVar const_zero
Definition: numeric.c:417
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:7939
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:62
static void set_var_from_num(Numeric value, NumericVar *dest)
Definition: numeric.c:6983
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7997
#define PG_NARGS()
Definition: fmgr.h:203
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:7465
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:205
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:7031
#define init_var(v)
Definition: numeric.c:496
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295

◆ get_min_scale()

static int get_min_scale ( NumericVar var)
static

Definition at line 4048 of file numeric.c.

References DEC_DIGITS, NumericVar::digits, NumericVar::ndigits, and NumericVar::weight.

Referenced by numeric_min_scale(), and numeric_trim_scale().

4049 {
4050  int min_scale;
4051  int last_digit_pos;
4052 
4053  /*
4054  * Ordinarily, the input value will be "stripped" so that the last
4055  * NumericDigit is nonzero. But we don't want to get into an infinite
4056  * loop if it isn't, so explicitly find the last nonzero digit.
4057  */
4058  last_digit_pos = var->ndigits - 1;
4059  while (last_digit_pos >= 0 &&
4060  var->digits[last_digit_pos] == 0)
4061  last_digit_pos--;
4062 
4063  if (last_digit_pos >= 0)
4064  {
4065  /* compute min_scale assuming that last ndigit has no zeroes */
4066  min_scale = (last_digit_pos - var->weight) * DEC_DIGITS;
4067 
4068  /*
4069  * We could get a negative result if there are no digits after the
4070  * decimal point. In this case the min_scale must be zero.
4071  */
4072  if (min_scale > 0)
4073  {
4074  /*
4075  * Reduce min_scale if trailing digit(s) in last NumericDigit are
4076  * zero.
4077  */
4078  NumericDigit last_digit = var->digits[last_digit_pos];
4079 
4080  while (last_digit % 10 == 0)
4081  {
4082  min_scale--;
4083  last_digit /= 10;
4084  }
4085  }
4086  else
4087  min_scale = 0;
4088  }
4089  else
4090  min_scale = 0; /* result if input is zero */
4091 
4092  return min_scale;
4093 }
int weight
Definition: numeric.c:308
int ndigits
Definition: numeric.c:307
int16 NumericDigit
Definition: numeric.c:103
NumericDigit * digits
Definition: numeric.c:312
#define DEC_DIGITS
Definition: numeric.c:99

◆ get_str_from_var()

static char * get_str_from_var ( const NumericVar var)
static

Definition at line 7057 of file numeric.c.

References DEC_DIGITS, NumericVar::digits, NumericVar::dscale, error(), i, NBASE, NumericVar::ndigits, NUMERIC_NEG, palloc(), NumericVar::sign, generate_unaccent_rules::str, and NumericVar::weight.

Referenced by get_str_from_var_sci(), numeric_normalize(), numeric_out(), and numericvar_to_double_no_overflow().

7058 {
7059  int dscale;
7060  char *str;
7061  char *cp;
7062  char *endcp;
7063  int i;
7064  int d;
7065  NumericDigit dig;
7066 
7067 #if DEC_DIGITS > 1
7068  NumericDigit d1;
7069 #endif
7070 
7071  dscale = var->dscale;
7072 
7073  /*
7074  * Allocate space for the result.
7075  *
7076  * i is set to the # of decimal digits before decimal point. dscale is the
7077  * # of decimal digits we will print after decimal point. We may generate
7078  * as many as DEC_DIGITS-1 excess digits at the end, and in addition we
7079  * need room for sign, decimal point, null terminator.
7080  */
7081  i = (var->weight + 1) * DEC_DIGITS;
7082  if (i <= 0)
7083  i = 1;
7084 
7085  str = palloc(i + dscale + DEC_DIGITS + 2);
7086  cp = str;
7087 
7088  /*
7089  * Output a dash for negative values
7090  */
7091  if (var->sign == NUMERIC_NEG)
7092  *cp++ = '-';
7093 
7094  /*
7095  * Output all digits before the decimal point
7096  */
7097  if (var->weight < 0)
7098  {
7099  d = var->weight + 1;
7100  *cp++ = '0';
7101  }
7102  else
7103  {
7104  for (d = 0; d <= var->weight; d++)
7105  {
7106  dig = (d < var->ndigits) ? var->digits[d] : 0;
7107  /* In the first digit, suppress extra leading decimal zeroes */
7108 #if DEC_DIGITS == 4
7109  {
7110  bool putit = (d > 0);
7111 
7112  d1 = dig / 1000;
7113  dig -= d1 * 1000;
7114  putit |= (d1 > 0);
7115  if (putit)
7116  *cp++ = d1 + '0';
7117  d1 = dig / 100;
7118  dig -= d1 * 100;
7119  putit |= (d1 > 0);
7120  if (putit)
7121  *cp++ = d1 + '0';
7122  d1 = dig / 10;
7123  dig -= d1 * 10;
7124  putit |= (d1 > 0);
7125  if (putit)
7126  *cp++ = d1 + '0';
7127  *cp++ = dig + '0';
7128  }
7129 #elif DEC_DIGITS == 2
7130  d1 = dig / 10;
7131  dig -= d1 * 10;
7132  if (d1 > 0 || d > 0)
7133  *cp++ = d1 + '0';
7134  *cp++ = dig + '0';
7135 #elif DEC_DIGITS == 1
7136  *cp++ = dig + '0';
7137 #else
7138 #error unsupported NBASE
7139 #endif
7140  }
7141  }
7142 
7143  /*
7144  * If requested, output a decimal point and all the digits that follow it.
7145  * We initially put out a multiple of DEC_DIGITS digits, then truncate if
7146  * needed.
7147  */
7148  if (dscale > 0)
7149  {
7150  *cp++ = '.';
7151  endcp = cp + dscale;
7152  for (i = 0; i < dscale; d++, i += DEC_DIGITS)
7153  {
7154  dig = (d >= 0 && d < var->ndigits) ? var->digits[d] : 0;
7155 #if DEC_DIGITS == 4
7156  d1 = dig / 1000;
7157  dig -= d1 * 1000;
7158  *cp++ = d1 + '0';
7159  d1 = dig / 100;
7160  dig -= d1 * 100;
7161  *cp++ = d1 + '0';
7162  d1 = dig / 10;
7163  dig -= d1 * 10;
7164  *cp++ = d1 + '0';
7165  *cp++ = dig + '0';
7166 #elif DEC_DIGITS == 2
7167  d1 = dig / 10;
7168  dig -= d1 * 10;
7169  *cp++ = d1 + '0';
7170  *cp++ = dig + '0';
7171 #elif DEC_DIGITS == 1
7172  *cp++ = dig + '0';
7173 #else
7174 #error unsupported NBASE
7175 #endif
7176  }
7177  cp = endcp;
7178  }
7179 
7180  /*
7181  * terminate the string and return it
7182  */
7183  *cp = '\0';
7184  return str;
7185 }
int weight
Definition: numeric.c:308
static void error(void)
Definition: sql-dyntest.c:147
int ndigits
Definition: numeric.c:307
int dscale
Definition: numeric.c:310
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:309
int16 NumericDigit
Definition: numeric.c:103
#define NBASE
Definition: numeric.c:97
NumericDigit * digits
Definition: numeric.c:312
void * palloc(Size size)
Definition: mcxt.c:1062
int i
#define DEC_DIGITS
Definition: numeric.c:99

◆ get_str_from_var_sci()

static char * get_str_from_var_sci ( const NumericVar var,
int  rscale 
)
static

Definition at line 7210 of file numeric.c.

References DEC_DIGITS, NumericVar::digits, div_var(), free_var(), get_str_from_var(), init_var, NumericVar::ndigits, palloc(), pfree(), power_var_int(), snprintf, generate_unaccent_rules::str, and NumericVar::weight.

Referenced by numeric_out_sci().

7211 {
7212  int32 exponent;
7213  NumericVar denominator;
7214  NumericVar significand;
7215  int denom_scale;
7216  size_t len;
7217  char *str;
7218  char *sig_out;
7219 
7220  if (rscale < 0)
7221  rscale = 0;
7222 
7223  /*
7224  * Determine the exponent of this number in normalised form.
7225  *
7226  * This is the exponent required to represent the number with only one
7227  * significant digit before the decimal place.
7228  */
7229  if (var->ndigits > 0)
7230  {
7231  exponent = (var->weight + 1) * DEC_DIGITS;
7232 
7233  /*
7234  * Compensate for leading decimal zeroes in the first numeric digit by
7235  * decrementing the exponent.
7236  */
7237  exponent -= DEC_DIGITS - (int) log10(var->digits[0]);
7238  }
7239  else
7240  {
7241  /*
7242  * If var has no digits, then it must be zero.
7243  *
7244  * Zero doesn't technically have a meaningful exponent in normalised
7245  * notation, but we just display the exponent as zero for consistency
7246  * of output.
7247  */
7248  exponent = 0;
7249  }
7250 
7251  /*
7252  * The denominator is set to 10 raised to the power of the exponent.
7253  *
7254  * We then divide var by the denominator to get the significand, rounding
7255  * to rscale decimal digits in the process.
7256  */
7257  if (exponent < 0)
7258  denom_scale = -exponent;
7259  else
7260  denom_scale = 0;
7261 
7262  init_var(&denominator);
7263  init_var(&significand);
7264 
7265  power_var_int(&const_ten, exponent, &denominator, denom_scale);
7266  div_var(var, &denominator, &significand, rscale, true);
7267  sig_out = get_str_from_var(&significand);
7268 
7269  free_var(&denominator);
7270  free_var(&significand);
7271 
7272  /*
7273  * Allocate space for the result.
7274  *
7275  * In addition to the significand, we need room for the exponent
7276  * decoration ("e"), the sign of the exponent, up to 10 digits for the
7277  * exponent itself, and of course the null terminator.
7278  */
7279  len = strlen(sig_out) + 13;
7280  str = palloc(len);
7281  snprintf(str, len, "%se%+03d", sig_out, exponent);
7282 
7283  pfree(sig_out);
7284 
7285  return str;
7286 }
int weight
Definition: numeric.c:308
int ndigits
Definition: numeric.c:307
static void div_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:8443
signed int int32
Definition: c.h:429
void pfree(void *pointer)
Definition: mcxt.c:1169
static void power_var_int(const NumericVar *base, int exp, NumericVar *result, int rscale)
Definition: numeric.c:10352
static const NumericVar const_ten
Definition: numeric.c:433
static void free_var(NumericVar *var)
Definition: numeric.c:6781
NumericDigit * digits
Definition: numeric.c:312
void * palloc(Size size)
Definition: mcxt.c:1062
static char * get_str_from_var(const NumericVar *var)
Definition: numeric.c:7057
#define DEC_DIGITS
Definition: numeric.c:99
#define snprintf
Definition: port.h:216
#define init_var(v)
Definition: numeric.c:496

◆ hash_numeric()

Datum hash_numeric ( PG_FUNCTION_ARGS  )

Definition at line 2622 of file numeric.c.

References Assert, digits, hash_any(), i, sort-test::key, NUMERIC_DIGITS, NUMERIC_IS_SPECIAL, NUMERIC_NDIGITS, NUMERIC_WEIGHT, PG_GETARG_NUMERIC, PG_RETURN_DATUM, and PG_RETURN_UINT32.

Referenced by JsonbHashScalarValue().

2623 {
2625  Datum digit_hash;
2626  Datum result;
2627  int weight;
2628  int start_offset;
2629  int end_offset;
2630  int i;
2631  int hash_len;
2633 
2634  /* If it's NaN or infinity, don't try to hash the rest of the fields */
2635  if (NUMERIC_IS_SPECIAL(key))
2636  PG_RETURN_UINT32(0);
2637 
2638  weight = NUMERIC_WEIGHT(key);
2639  start_offset = 0;
2640  end_offset = 0;
2641 
2642  /*
2643  * Omit any leading or trailing zeros from the input to the hash. The
2644  * numeric implementation *should* guarantee that leading and trailing
2645  * zeros are suppressed, but we're paranoid. Note that we measure the
2646  * starting and ending offsets in units of NumericDigits, not bytes.
2647  */
2648  digits = NUMERIC_DIGITS(key);
2649  for (i = 0; i < NUMERIC_NDIGITS(key); i++)
2650  {
2651  if (digits[i] != (NumericDigit) 0)
2652  break;
2653 
2654  start_offset++;
2655 
2656  /*
2657  * The weight is effectively the # of digits before the decimal point,
2658  * so decrement it for each leading zero we skip.
2659  */
2660  weight--;
2661  }
2662 
2663  /*
2664  * If there are no non-zero digits, then the value of the number is zero,
2665  * regardless of any other fields.
2666  */
2667  if (NUMERIC_NDIGITS(key) == start_offset)
2668  PG_RETURN_UINT32(-1);
2669 
2670  for (i = NUMERIC_NDIGITS(key) - 1; i >= 0; i--)
2671  {
2672  if (digits[i] != (NumericDigit) 0)
2673  break;
2674 
2675  end_offset++;
2676  }
2677 
2678  /* If we get here, there should be at least one non-zero digit */
2679  Assert(start_offset + end_offset < NUMERIC_NDIGITS(key));
2680 
2681  /*
2682  * Note that we don't hash on the Numeric's scale, since two numerics can
2683  * compare equal but have different scales. We also don't hash on the
2684  * sign, although we could: since a sign difference implies inequality,
2685  * this shouldn't affect correctness.
2686  */
2687  hash_len = NUMERIC_NDIGITS(key) - start_offset - end_offset;
2688  digit_hash = hash_any((unsigned char *) (NUMERIC_DIGITS(key) + start_offset),
2689  hash_len * sizeof(NumericDigit));
2690 
2691  /* Mix in the weight, via XOR */
2692  result = digit_hash ^ weight;
2693 
2694  PG_RETURN_DATUM(result);
2695 }
#define PG_RETURN_UINT32(x)
Definition: fmgr.h:355
#define NUMERIC_IS_SPECIAL(n)
Definition: numeric.c:174
#define NUMERIC_DIGITS(num)
Definition: numeric.c:498
int16 NumericDigit
Definition: numeric.c:103
static Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.h:31
#define NUMERIC_NDIGITS(num)
Definition: numeric.c:500
uintptr_t Datum
Definition: postgres.h:411
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:248
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:62
#define Assert(condition)
Definition: c.h:804
int i
int digits
Definition: informix.c:666

◆ hash_numeric_extended()

Datum hash_numeric_extended ( PG_FUNCTION_ARGS  )

Definition at line 2702 of file numeric.c.

References Assert, DatumGetUInt64, digits, hash_any_extended(), i, sort-test::key, NUMERIC_DIGITS, NUMERIC_IS_SPECIAL, NUMERIC_NDIGITS, NUMERIC_WEIGHT, PG_GETARG_INT64, PG_GETARG_NUMERIC, PG_RETURN_DATUM, PG_RETURN_UINT64, and UInt64GetDatum.

Referenced by JsonbHashScalarValueExtended().

2703 {
2705  uint64 seed = PG_GETARG_INT64(1);
2706  Datum digit_hash;
2707  Datum result;
2708  int weight;
2709  int start_offset;
2710  int end_offset;
2711  int i;
2712  int hash_len;
2714 
2715  /* If it's NaN or infinity, don't try to hash the rest of the fields */
2716  if (NUMERIC_IS_SPECIAL(key))
2717  PG_RETURN_UINT64(seed);
2718 
2719  weight = NUMERIC_WEIGHT(key);
2720  start_offset = 0;
2721  end_offset = 0;
2722 
2723  digits = NUMERIC_DIGITS(key);
2724  for (i = 0; i < NUMERIC_NDIGITS(key); i++)
2725  {
2726  if (digits[i] != (NumericDigit) 0)
2727  break;
2728 
2729  start_offset++;
2730 
2731  weight--;
2732  }
2733 
2734  if (NUMERIC_NDIGITS(key) == start_offset)
2735  PG_RETURN_UINT64(seed - 1);
2736 
2737  for (i = NUMERIC_NDIGITS(key) - 1; i >= 0; i--)
2738  {
2739  if (digits[i] != (NumericDigit) 0)
2740  break;
2741 
2742  end_offset++;
2743  }
2744 
2745  Assert(start_offset + end_offset < NUMERIC_NDIGITS(key));
2746 
2747  hash_len = NUMERIC_NDIGITS(key) - start_offset - end_offset;
2748  digit_hash = hash_any_extended((unsigned char *) (NUMERIC_DIGITS(key)
2749  + start_offset),
2750  hash_len * sizeof(NumericDigit),
2751  seed);
2752 
2753  result = UInt64GetDatum(DatumGetUInt64(digit_hash) ^ weight);
2754 
2755  PG_RETURN_DATUM(result);
2756 }
#define UInt64GetDatum(X)
Definition: postgres.h:692
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.h:37
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:369
#define NUMERIC_IS_SPECIAL(n)
Definition: numeric.c:174
#define NUMERIC_DIGITS(num)
Definition: numeric.c:498
int16 NumericDigit
Definition: numeric.c:103
#define NUMERIC_NDIGITS(num)
Definition: numeric.c:500
uintptr_t Datum
Definition: postgres.h:411
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define DatumGetUInt64(X)
Definition: postgres.h:678
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:248
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:62
#define Assert(condition)
Definition: c.h:804
int i
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
int digits
Definition: informix.c:666

◆ in_range_numeric_numeric()

Datum in_range_numeric_numeric ( PG_FUNCTION_ARGS  )

Definition at line 2487 of file numeric.c.

References add_var(), Assert, cmp_var(), ereport, errcode(), errmsg(), ERROR, free_var(), init_var, init_var_from_num(), NUMERIC_IS_NAN, NUMERIC_IS_NINF, NUMERIC_IS_PINF, NUMERIC_IS_SPECIAL, NUMERIC_NEG, NUMERIC_SIGN, PG_FREE_IF_COPY, PG_GETARG_BOOL, PG_GETARG_NUMERIC, PG_RETURN_BOOL, sub_var(), and val.

2488 {
2490  Numeric base = PG_GETARG_NUMERIC(1);
2491  Numeric offset = PG_GETARG_NUMERIC(2);
2492  bool sub = PG_GETARG_BOOL(3);
2493  bool less = PG_GETARG_BOOL(4);
2494  bool result;
2495 
2496  /*
2497  * Reject negative (including -Inf) or NaN offset. Negative is per spec,
2498  * and NaN is because appropriate semantics for that seem non-obvious.
2499  */
2500  if (NUMERIC_IS_NAN(offset) ||
2501  NUMERIC_IS_NINF(offset) ||
2502  NUMERIC_SIGN(offset) == NUMERIC_NEG)
2503  ereport(ERROR,
2504  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2505  errmsg("invalid preceding or following size in window function")));
2506 
2507  /*
2508  * Deal with cases where val and/or base is NaN, following the rule that
2509  * NaN sorts after non-NaN (cf cmp_numerics). The offset cannot affect
2510  * the conclusion.
2511  */
2512  if (NUMERIC_IS_NAN(val))
2513  {
2514  if (NUMERIC_IS_NAN(base))
2515  result = true; /* NAN = NAN */
2516  else
2517  result = !less; /* NAN > non-NAN */
2518  }
2519  else if (NUMERIC_IS_NAN(base))
2520  {
2521  result = less; /* non-NAN < NAN */
2522  }
2523 
2524  /*
2525  * Deal with infinite offset (necessarily +Inf, at this point).
2526  */
2527  else if (NUMERIC_IS_SPECIAL(offset))
2528  {
2529  Assert(NUMERIC_IS_PINF(offset));
2530  if (sub ? NUMERIC_IS_PINF(base) : NUMERIC_IS_NINF(base))
2531  {
2532  /*
2533  * base +/- offset would produce NaN, so return true for any val
2534  * (see in_range_float8_float8() for reasoning).
2535  */
2536  result = true;
2537  }
2538  else if (sub)
2539  {
2540  /* base - offset must be -inf */
2541  if (less)
2542  result = NUMERIC_IS_NINF(val); /* only -inf is <= sum */
2543  else
2544  result = true; /* any val is >= sum */
2545  }
2546  else
2547  {
2548  /* base + offset must be +inf */
2549  if (less)
2550  result = true; /* any val is <= sum */
2551  else
2552  result = NUMERIC_IS_PINF(val); /* only +inf is >= sum */
2553  }
2554  }
2555 
2556  /*
2557  * Deal with cases where val and/or base is infinite. The offset, being
2558  * now known finite, cannot affect the conclusion.
2559  */
2560  else if (NUMERIC_IS_SPECIAL(val))
2561  {
2562  if (NUMERIC_IS_PINF(val))
2563  {
2564  if (NUMERIC_IS_PINF(base))
2565  result = true; /* PINF = PINF */
2566  else
2567  result = !less; /* PINF > any other non-NAN */
2568  }
2569  else /* val must be NINF */
2570  {
2571  if (NUMERIC_IS_NINF(base))
2572  result = true; /* NINF = NINF */
2573  else
2574  result = less; /* NINF < anything else */
2575  }
2576  }
2577  else if (NUMERIC_IS_SPECIAL(base))
2578  {
2579  if (NUMERIC_IS_NINF(base))
2580  result = !less; /* normal > NINF */
2581  else
2582  result = less; /* normal < PINF */
2583  }
2584  else
2585  {
2586  /*
2587  * Otherwise go ahead and compute base +/- offset. While it's
2588  * possible for this to overflow the numeric format, it's unlikely
2589  * enough that we don't take measures to prevent it.
2590  */
2591  NumericVar valv;
2592  NumericVar basev;
2593  NumericVar offsetv;
2594  NumericVar sum;
2595 
2596  init_var_from_num(val, &valv);
2597  init_var_from_num(base, &basev);
2598  init_var_from_num(offset, &offsetv);
2599  init_var(&sum);
2600 
2601  if (sub)
2602  sub_var(&basev, &offsetv, &sum);
2603  else
2604  add_var(&basev, &offsetv, &sum);
2605 
2606  if (less)
2607  result = (cmp_var(&valv, &sum) <= 0);
2608  else
2609  result = (cmp_var(&valv, &sum) >= 0);
2610 
2611  free_var(&sum);
2612  }
2613 
2614  PG_FREE_IF_COPY(val, 0);
2615  PG_FREE_IF_COPY(base, 1);
2616  PG_FREE_IF_COPY(offset, 2);
2617 
2618  PG_RETURN_BOOL(result);
2619 }
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define NUMERIC_IS_SPECIAL(n)
Definition: numeric.c:174
#define NUMERIC_NEG
Definition: numeric.c:168
#define ERROR
Definition: elog.h:46
#define NUMERIC_IS_PINF(n)
Definition: numeric.c:206
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:7014
#define NUMERIC_SIGN(n)
Definition: numeric.c:238
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:7939
static void free_var(NumericVar *var)
Definition: numeric.c:6781
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:62
#define Assert(condition)
Definition: c.h:804
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7997
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define NUMERIC_IS_NINF(n)
Definition: numeric.c:207
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:205
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8114
long val
Definition: informix.c:664
#define init_var(v)
Definition: numeric.c:496

◆ init_var_from_num()

static void init_var_from_num ( Numeric  num,
NumericVar dest 
)
static

Definition at line 7014 of file numeric.c.

References NumericVar::buf, NumericVar::digits, NumericVar::dscale, NumericVar::ndigits, NUMERIC_DIGITS, NUMERIC_DSCALE, NUMERIC_NDIGITS, NUMERIC_SIGN, NUMERIC_WEIGHT, NumericVar::sign, and NumericVar::weight.

Referenced by compute_bucket(), do_numeric_accum(), do_numeric_discard(), generate_series_step_numeric(), in_range_numeric_numeric(), numeric_abbrev_convert(), numeric_add_opt_error(), numeric_ceil(), numeric_div_opt_error(), numeric_div_trunc(), numeric_exp(), numeric_float8_no_overflow(), numeric_floor(), numeric_gcd(), numeric_inc(), numeric_int2(), numeric_int4_opt_error(), numeric_int8(), numeric_is_integral(), numeric_lcm(), numeric_ln(), numeric_log(), numeric_min_scale(), numeric_mod_opt_error(), numeric_mul_opt_error(), numeric_normalize(), numeric_out(), numeric_out_sci(), numeric_pg_lsn(), numeric_power(), numeric_send(), numeric_sqrt(), numeric_sub_opt_error(), and numeric_trim_scale().

7015 {
7016  dest->ndigits = NUMERIC_NDIGITS(num);
7017  dest->weight = NUMERIC_WEIGHT(num);
7018  dest->sign = NUMERIC_SIGN(num);
7019  dest->dscale = NUMERIC_DSCALE(num);
7020  dest->digits = NUMERIC_DIGITS(num);
7021  dest->buf = NULL; /* digits array is not palloc'd */
7022 }
#define NUMERIC_DSCALE(n)
Definition: numeric.c:244
int weight
Definition: numeric.c:308
int ndigits
Definition: numeric.c:307
int dscale
Definition: numeric.c:310
int sign
Definition: numeric.c:309
#define NUMERIC_DIGITS(num)
Definition: numeric.c:498
#define NUMERIC_SIGN(n)
Definition: numeric.c:238
NumericDigit * buf
Definition: numeric.c:311
#define NUMERIC_NDIGITS(num)
Definition: numeric.c:500
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:248
NumericDigit * digits
Definition: numeric.c:312

◆ int2_accum()

Datum int2_accum ( PG_FUNCTION_ARGS  )

Definition at line 5358 of file numeric.c.

References do_numeric_accum(), int64_to_numeric(), makePolyNumAggState, PG_ARGISNULL, PG_GETARG_INT16, PG_GETARG_POINTER, and PG_RETURN_POINTER.

5359 {
5361 
5362  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
5363 
5364  /* Create the state data on the first call */
5365  if (state == NULL)
5366  state = makePolyNumAggState(fcinfo, true);
5367 
5368  if (!PG_ARGISNULL(1))
5369  {
5370 #ifdef HAVE_INT128
5371  do_int128_accum(state, (int128) PG_GETARG_INT16(1));
5372 #else
5374 #endif
5375  }
5376 
5377  PG_RETURN_POINTER(state);
5378 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
static void do_numeric_accum(NumericAggState *state, Numeric newval)
Definition: numeric.c:4661
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4145
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
Definition: regguts.h:317
#define makePolyNumAggState
Definition: numeric.c:5353

◆ int2_accum_inv()

Datum int2_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 5786 of file numeric.c.

References do_numeric_discard(), elog, ERROR, int64_to_numeric(), PG_ARGISNULL, PG_GETARG_INT16, PG_GETARG_POINTER, and PG_RETURN_POINTER.

5787 {
5789 
5790  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
5791 
5792  /* Should not get here with no state */
5793  if (state == NULL)
5794  elog(ERROR, "int2_accum_inv called with NULL state");
5795 
5796  if (!PG_ARGISNULL(1))
5797  {
5798 #ifdef HAVE_INT128
5799  do_int128_discard(state, (int128) PG_GETARG_INT16(1));
5800 #else
5801  /* Should never fail, all inputs have dscale 0 */
5803  elog(ERROR, "do_numeric_discard failed unexpectedly");
5804 #endif
5805  }
5806 
5807  PG_RETURN_POINTER(state);
5808 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
static bool do_numeric_discard(NumericAggState *state, Numeric newval)
Definition: numeric.c:4731
#define ERROR
Definition: elog.h:46
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4145
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
Definition: regguts.h:317
#define elog(elevel,...)
Definition: elog.h:232

◆ int2_avg_accum()

Datum int2_avg_accum ( PG_FUNCTION_ARGS  )

Definition at line 6469 of file numeric.c.

References AggCheckCallContext(), ARR_DATA_PTR, ARR_HASNULL, ARR_OVERHEAD_NONULLS, ARR_SIZE, Int8TransTypeData::count, elog, ERROR, newval, PG_GETARG_ARRAYTYPE_P, PG_GETARG_ARRAYTYPE_P_COPY, PG_GETARG_INT16, PG_RETURN_ARRAYTYPE_P, and Int8TransTypeData::sum.

6470 {
6471  ArrayType *transarray;
6473  Int8TransTypeData *transdata;
6474 
6475  /*
6476  * If we're invoked as an aggregate, we can cheat and modify our first
6477  * parameter in-place to reduce palloc overhead. Otherwise we need to make
6478  * a copy of it before scribbling on it.
6479  */
6480  if (AggCheckCallContext(fcinfo, NULL))
6481  transarray = PG_GETARG_ARRAYTYPE_P(0);
6482  else
6483  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
6484 
6485  if (ARR_HASNULL(transarray) ||
6486  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
6487  elog(ERROR, "expected 2-element int8 array");
6488 
6489  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
6490  transdata->count++;
6491  transdata->sum += newval;
6492 
6493  PG_RETURN_ARRAYTYPE_P(transarray);
6494 }
signed short int16
Definition: c.h:428
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:303
#define ARR_SIZE(a)
Definition: array.h:282
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition: array.h:257
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
#define ERROR
Definition: elog.h:46
#define ARR_DATA_PTR(a)
Definition: array.h:315
#define ARR_HASNULL(a)
Definition: array.h:284
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:258
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4584
#define elog(elevel,...)
Definition: elog.h:232

◆ int2_avg_accum_inv()

Datum int2_avg_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 6556 of file numeric.c.

References AggCheckCallContext(), ARR_DATA_PTR, ARR_HASNULL, ARR_OVERHEAD_NONULLS, ARR_SIZE, Int8TransTypeData::count, elog, ERROR, newval, PG_GETARG_ARRAYTYPE_P, PG_GETARG_ARRAYTYPE_P_COPY, PG_GETARG_INT16, PG_RETURN_ARRAYTYPE_P, and Int8TransTypeData::sum.

6557 {
6558  ArrayType *transarray;
6560  Int8TransTypeData *transdata;
6561 
6562  /*
6563  * If we're invoked as an aggregate, we can cheat and modify our first
6564  * parameter in-place to reduce palloc overhead. Otherwise we need to make
6565  * a copy of it before scribbling on it.
6566  */
6567  if (AggCheckCallContext(fcinfo, NULL))
6568  transarray = PG_GETARG_ARRAYTYPE_P(0);
6569  else
6570  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
6571 
6572  if (ARR_HASNULL(transarray) ||
6573  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
6574  elog(ERROR, "expected 2-element int8 array");
6575 
6576  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
6577  transdata->count--;
6578  transdata->sum -= newval;
6579 
6580  PG_RETURN_ARRAYTYPE_P(transarray);
6581 }
signed short int16
Definition: c.h:428
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:303
#define ARR_SIZE(a)
Definition: array.h:282
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition: array.h:257
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
#define ERROR
Definition: elog.h:46
#define ARR_DATA_PTR(a)
Definition: array.h:315
#define ARR_HASNULL(a)
Definition: array.h:284
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:258
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4584
#define elog(elevel,...)
Definition: elog.h:232

◆ int2_numeric()

Datum int2_numeric ( PG_FUNCTION_ARGS  )

Definition at line 4349 of file numeric.c.

References int64_to_numeric(), PG_GETARG_INT16, PG_RETURN_NUMERIC, and val.

4350 {
4351  int16 val = PG_GETARG_INT16(0);
4352 
4354 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:64
signed short int16
Definition: c.h:428
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4145
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
long val
Definition: informix.c:664

◆ int2_sum()

Datum int2_sum ( PG_FUNCTION_ARGS  )

Definition at line 6320 of file numeric.c.

References AggCheckCallContext(), newval, PG_ARGISNULL, PG_GETARG_INT16, PG_GETARG_INT64, PG_GETARG_POINTER, PG_RETURN_INT64, PG_RETURN_NULL, and PG_RETURN_POINTER.

6321 {
6322  int64 newval;
6323 
6324  if (PG_ARGISNULL(0))
6325  {
6326  /* No non-null input seen so far... */
6327  if (PG_ARGISNULL(1))
6328  PG_RETURN_NULL(); /* still no non-null */
6329  /* This is the first non-null input. */
6330  newval = (int64) PG_GETARG_INT16(1);
6331  PG_RETURN_INT64(newval);
6332  }
6333 
6334  /*
6335  * If we're invoked as an aggregate, we can cheat and modify our first
6336  * parameter in-place to avoid palloc overhead. If not, we need to return
6337  * the new value of the transition variable. (If int8 is pass-by-value,
6338  * then of course this is useless as well as incorrect, so just ifdef it
6339  * out.)
6340  */
6341 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
6342  if (AggCheckCallContext(fcinfo, NULL))
6343  {
6344  int64 *oldsum = (int64 *) PG_GETARG_POINTER(0);
6345 
6346  /* Leave the running sum unchanged in the new input is null */
6347  if (!PG_ARGISNULL(1))
6348  *oldsum = *oldsum + (int64) PG_GETARG_INT16(1);
6349 
6350  PG_RETURN_POINTER(oldsum);
6351  }
6352  else
6353 #endif
6354  {
6355  int64 oldsum = PG_GETARG_INT64(0);
6356 
6357  /* Leave sum unchanged if new input is null. */
6358  if (PG_ARGISNULL(1))
6359  PG_RETURN_INT64(oldsum);
6360 
6361  /* OK to do the addition. */
6362  newval = oldsum + (int64) PG_GETARG_INT16(1);
6363 
6364  PG_RETURN_INT64(newval);
6365  }
6366 }
#define PG_RETURN_POINTE