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_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 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)
 
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 404 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 487 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 489 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 483 of file numeric.c.

Referenced by int2int4_sum(), and make_result_opt_error().

◆ dump_var

#define dump_var (   s,
 
)

Definition at line 484 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 5323 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 4547 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 395 of file numeric.c.

◆ NUMERIC_ABBREV_NAN

#define NUMERIC_ABBREV_NAN   NumericAbbrevGetDatum(PG_INT32_MIN)

Definition at line 405 of file numeric.c.

Referenced by numeric_abbrev_convert().

◆ NUMERIC_ABBREV_NINF

#define NUMERIC_ABBREV_NINF   NumericAbbrevGetDatum(PG_INT32_MAX)

Definition at line 407 of file numeric.c.

Referenced by numeric_abbrev_convert().

◆ NUMERIC_ABBREV_PINF

#define NUMERIC_ABBREV_PINF   NumericAbbrevGetDatum(-PG_INT32_MAX)

Definition at line 406 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:189
#define NUMERIC_SHORT_WEIGHT_MIN
Definition: numeric.c:223
#define NUMERIC_SHORT_DSCALE_MAX
Definition: numeric.c:218

Definition at line 501 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 497 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 243 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_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 237 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 247 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 403 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 5321 of file numeric.c.

Function Documentation

◆ accum_sum_add()

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

Definition at line 10914 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().

10915 {
10916  int32 *accum_digits;
10917  int i,
10918  val_i;
10919  int val_ndigits;
10920  NumericDigit *val_digits;
10921 
10922  /*
10923  * If we have accumulated too many values since the last carry
10924  * propagation, do it now, to avoid overflowing. (We could allow more
10925  * than NBASE - 1, if we reserved two extra digits, rather than one, for
10926  * carry propagation. But even with NBASE - 1, this needs to be done so
10927  * seldom, that the performance difference is negligible.)
10928  */
10929  if (accum->num_uncarried == NBASE - 1)
10930  accum_sum_carry(accum);
10931 
10932  /*
10933  * Adjust the weight or scale of the old value, so that it can accommodate
10934  * the new value.
10935  */
10936  accum_sum_rescale(accum, val);
10937 
10938  /* */
10939  if (val->sign == NUMERIC_POS)
10940  accum_digits = accum->pos_digits;
10941  else
10942  accum_digits = accum->neg_digits;
10943 
10944  /* copy these values into local vars for speed in loop */
10945  val_ndigits = val->ndigits;
10946  val_digits = val->digits;
10947 
10948  i = accum->weight - val->weight;
10949  for (val_i = 0; val_i < val_ndigits; val_i++)
10950  {
10951  accum_digits[i] += (int32) val_digits[val_i];
10952  i++;
10953  }
10954 
10955  accum->num_uncarried++;
10956 }
static void accum_sum_rescale(NumericSumAccum *accum, const NumericVar *val)
Definition: numeric.c:11035
#define NUMERIC_POS
Definition: numeric.c:167
int32 * neg_digits
Definition: numeric.c:379
int num_uncarried
Definition: numeric.c:376
static void accum_sum_carry(NumericSumAccum *accum)
Definition: numeric.c:10962
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:378
long val
Definition: informix.c:664

◆ accum_sum_carry()

static void accum_sum_carry ( NumericSumAccum accum)
static

Definition at line 10962 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().

10963 {
10964  int i;
10965  int ndigits;
10966  int32 *dig;
10967  int32 carry;
10968  int32 newdig = 0;
10969 
10970  /*
10971  * If no new values have been added since last carry propagation, nothing
10972  * to do.
10973  */
10974  if (accum->num_uncarried == 0)
10975  return;
10976 
10977  /*
10978  * We maintain that the weight of the accumulator is always one larger
10979  * than needed to hold the current value, before carrying, to make sure
10980  * there is enough space for the possible extra digit when carry is
10981  * propagated. We cannot expand the buffer here, unless we require
10982  * callers of accum_sum_final() to switch to the right memory context.
10983  */
10984  Assert(accum->pos_digits[0] == 0 && accum->neg_digits[0] == 0);
10985 
10986  ndigits = accum->ndigits;
10987 
10988  /* Propagate carry in the positive sum */
10989  dig = accum->pos_digits;
10990  carry = 0;
10991  for (i = ndigits - 1; i >= 0; i--)
10992  {
10993  newdig = dig[i] + carry;
10994  if (newdig >= NBASE)
10995  {
10996  carry = newdig / NBASE;
10997  newdig -= carry * NBASE;
10998  }
10999  else
11000  carry = 0;
11001  dig[i] = newdig;
11002  }
11003  /* Did we use up the digit reserved for carry propagation? */
11004  if (newdig > 0)
11005  accum->have_carry_space = false;
11006 
11007  /* And the same for the negative sum */
11008  dig = accum->neg_digits;
11009  carry = 0;
11010  for (i = ndigits - 1; i >= 0; i--)
11011  {
11012  newdig = dig[i] + carry;
11013  if (newdig >= NBASE)
11014  {
11015  carry = newdig / NBASE;
11016  newdig -= carry * NBASE;
11017  }
11018  else
11019  carry = 0;
11020  dig[i] = newdig;
11021  }
11022  if (newdig > 0)
11023  accum->have_carry_space = false;
11024 
11025  accum->num_uncarried = 0;
11026 }
int32 * neg_digits
Definition: numeric.c:379
int num_uncarried
Definition: numeric.c:376
signed int int32
Definition: c.h:429
bool have_carry_space
Definition: numeric.c:377
#define NBASE
Definition: numeric.c:97
#define Assert(condition)
Definition: c.h:804
int i
int32 * pos_digits
Definition: numeric.c:378

◆ accum_sum_combine()

static void accum_sum_combine ( NumericSumAccum accum,
NumericSumAccum accum2 
)
static

Definition at line 11192 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().

11193 {
11194  NumericVar tmp_var;
11195 
11196  init_var(&tmp_var);
11197 
11198  accum_sum_final(accum2, &tmp_var);
11199  accum_sum_add(accum, &tmp_var);
11200 
11201  free_var(&tmp_var);
11202 }
static void accum_sum_final(NumericSumAccum *accum, NumericVar *result)
Definition: numeric.c:11124
static void accum_sum_add(NumericSumAccum *accum, const NumericVar *var1)
Definition: numeric.c:10914
static void free_var(NumericVar *var)
Definition: numeric.c:6781
#define init_var(v)
Definition: numeric.c:495

◆ accum_sum_copy()

static void accum_sum_copy ( NumericSumAccum dst,
NumericSumAccum src 
)
static

Definition at line 11175 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().

11176 {
11177  dst->pos_digits = palloc(src->ndigits * sizeof(int32));
11178  dst->neg_digits = palloc(src->ndigits * sizeof(int32));
11179 
11180  memcpy(dst->pos_digits, src->pos_digits, src->ndigits * sizeof(int32));
11181  memcpy(dst->neg_digits, src->neg_digits, src->ndigits * sizeof(int32));
11182  dst->num_uncarried = src->num_uncarried;
11183  dst->ndigits = src->ndigits;
11184  dst->weight = src->weight;
11185  dst->dscale = src->dscale;
11186 }
int32 * neg_digits
Definition: numeric.c:379
int num_uncarried
Definition: numeric.c:376
signed int int32
Definition: c.h:429
void * palloc(Size size)
Definition: mcxt.c:1062
int32 * pos_digits
Definition: numeric.c:378

◆ accum_sum_final()

static void accum_sum_final ( NumericSumAccum accum,
NumericVar result 
)
static

Definition at line 11124 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().

11125 {
11126  int i;
11127  NumericVar pos_var;
11128  NumericVar neg_var;
11129 
11130  if (accum->ndigits == 0)
11131  {
11132  set_var_from_var(&const_zero, result);
11133  return;
11134  }
11135 
11136  /* Perform final carry */
11137  accum_sum_carry(accum);
11138 
11139  /* Create NumericVars representing the positive and negative sums */
11140  init_var(&pos_var);
11141  init_var(&neg_var);
11142 
11143  pos_var.ndigits = neg_var.ndigits = accum->ndigits;
11144  pos_var.weight = neg_var.weight = accum->weight;
11145  pos_var.dscale = neg_var.dscale = accum->dscale;
11146  pos_var.sign = NUMERIC_POS;
11147  neg_var.sign = NUMERIC_NEG;
11148 
11149  pos_var.buf = pos_var.digits = digitbuf_alloc(accum->ndigits);
11150  neg_var.buf = neg_var.digits = digitbuf_alloc(accum->ndigits);
11151 
11152  for (i = 0; i < accum->ndigits; i++)
11153  {
11154  Assert(accum->pos_digits[i] < NBASE);
11155  pos_var.digits[i] = (int16) accum->pos_digits[i];
11156 
11157  Assert(accum->neg_digits[i] < NBASE);
11158  neg_var.digits[i] = (int16) accum->neg_digits[i];
11159  }
11160 
11161  /* And add them together */
11162  add_var(&pos_var, &neg_var, result);
11163 
11164  /* Remove leading/trailing zeroes */
11165  strip_var(result);
11166 }
signed short int16
Definition: c.h:428
int weight
Definition: numeric.c:307
#define NUMERIC_POS
Definition: numeric.c:167
static void strip_var(NumericVar *var)
Definition: numeric.c:10857
int32 * neg_digits
Definition: numeric.c:379
#define digitbuf_alloc(ndigits)
Definition: numeric.c:487
int ndigits
Definition: numeric.c:306
static void accum_sum_carry(NumericSumAccum *accum)
Definition: numeric.c:10962
int dscale
Definition: numeric.c:309
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:308
NumericDigit * buf
Definition: numeric.c:310
#define NBASE
Definition: numeric.c:97
static const NumericVar const_zero
Definition: numeric.c:416
#define Assert(condition)
Definition: c.h:804
NumericDigit * digits
Definition: numeric.c:311
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7953
int i
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:7031
int32 * pos_digits
Definition: numeric.c:378
#define init_var(v)
Definition: numeric.c:495

◆ accum_sum_rescale()

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

Definition at line 11035 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().

11036 {
11037  int old_weight = accum->weight;
11038  int old_ndigits = accum->ndigits;
11039  int accum_ndigits;
11040  int accum_weight;
11041  int accum_rscale;
11042  int val_rscale;
11043 
11044  accum_weight = old_weight;
11045  accum_ndigits = old_ndigits;
11046 
11047  /*
11048  * Does the new value have a larger weight? If so, enlarge the buffers,
11049  * and shift the existing value to the new weight, by adding leading
11050  * zeros.
11051  *
11052  * We enforce that the accumulator always has a weight one larger than
11053  * needed for the inputs, so that we have space for an extra digit at the
11054  * final carry-propagation phase, if necessary.
11055  */
11056  if (val->weight >= accum_weight)
11057  {
11058  accum_weight = val->weight + 1;
11059  accum_ndigits = accum_ndigits + (accum_weight - old_weight);
11060  }
11061 
11062  /*
11063  * Even though the new value is small, we might've used up the space
11064  * reserved for the carry digit in the last call to accum_sum_carry(). If
11065  * so, enlarge to make room for another one.
11066  */
11067  else if (!accum->have_carry_space)
11068  {
11069  accum_weight++;
11070  accum_ndigits++;
11071  }
11072 
11073  /* Is the new value wider on the right side? */
11074  accum_rscale = accum_ndigits - accum_weight - 1;
11075  val_rscale = val->ndigits - val->weight - 1;
11076  if (val_rscale > accum_rscale)
11077  accum_ndigits = accum_ndigits + (val_rscale - accum_rscale);
11078 
11079  if (accum_ndigits != old_ndigits ||
11080  accum_weight != old_weight)
11081  {
11082  int32 *new_pos_digits;
11083  int32 *new_neg_digits;
11084  int weightdiff;
11085 
11086  weightdiff = accum_weight - old_weight;
11087 
11088  new_pos_digits = palloc0(accum_ndigits * sizeof(int32));
11089  new_neg_digits = palloc0(accum_ndigits * sizeof(int32));
11090 
11091  if (accum->pos_digits)
11092  {
11093  memcpy(&new_pos_digits[weightdiff], accum->pos_digits,
11094  old_ndigits * sizeof(int32));
11095  pfree(accum->pos_digits);
11096 
11097  memcpy(&new_neg_digits[weightdiff], accum->neg_digits,
11098  old_ndigits * sizeof(int32));
11099  pfree(accum->neg_digits);
11100  }
11101 
11102  accum->pos_digits = new_pos_digits;
11103  accum->neg_digits = new_neg_digits;
11104 
11105  accum->weight = accum_weight;
11106  accum->ndigits = accum_ndigits;
11107 
11108  Assert(accum->pos_digits[0] == 0 && accum->neg_digits[0] == 0);
11109  accum->have_carry_space = true;
11110  }
11111 
11112  if (val->dscale > accum->dscale)
11113  accum->dscale = val->dscale;
11114 }
int weight
Definition: numeric.c:307
int32 * neg_digits
Definition: numeric.c:379
int ndigits
Definition: numeric.c:306
int dscale
Definition: numeric.c:309
signed int int32
Definition: c.h:429
void pfree(void *pointer)
Definition: mcxt.c:1169
bool have_carry_space
Definition: numeric.c:377
void * palloc0(Size size)
Definition: mcxt.c:1093
#define Assert(condition)
Definition: c.h:804
int32 * pos_digits
Definition: numeric.c:378

◆ accum_sum_reset()

static void accum_sum_reset ( NumericSumAccum accum)
static

Definition at line 10898 of file numeric.c.

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

Referenced by do_numeric_discard().

10899 {
10900  int i;
10901 
10902  accum->dscale = 0;
10903  for (i = 0; i < accum->ndigits; i++)
10904  {
10905  accum->pos_digits[i] = 0;
10906  accum->neg_digits[i] = 0;
10907  }
10908 }
int32 * neg_digits
Definition: numeric.c:379
int i
int32 * pos_digits
Definition: numeric.c:378

◆ add_abs()

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

Definition at line 10522 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().

10523 {
10524  NumericDigit *res_buf;
10525  NumericDigit *res_digits;
10526  int res_ndigits;
10527  int res_weight;
10528  int res_rscale,
10529  rscale1,
10530  rscale2;
10531  int res_dscale;
10532  int i,
10533  i1,
10534  i2;
10535  int carry = 0;
10536 
10537  /* copy these values into local vars for speed in inner loop */
10538  int var1ndigits = var1->ndigits;
10539  int var2ndigits = var2->ndigits;
10540  NumericDigit *var1digits = var1->digits;
10541  NumericDigit *var2digits = var2->digits;
10542 
10543  res_weight = Max(var1->weight, var2->weight) + 1;
10544 
10545  res_dscale = Max(var1->dscale, var2->dscale);
10546 
10547  /* Note: here we are figuring rscale in base-NBASE digits */
10548  rscale1 = var1->ndigits - var1->weight - 1;
10549  rscale2 = var2->ndigits - var2->weight - 1;
10550  res_rscale = Max(rscale1, rscale2);
10551 
10552  res_ndigits = res_rscale + res_weight + 1;
10553  if (res_ndigits <= 0)
10554  res_ndigits = 1;
10555 
10556  res_buf = digitbuf_alloc(res_ndigits + 1);
10557  res_buf[0] = 0; /* spare digit for later rounding */
10558  res_digits = res_buf + 1;
10559 
10560  i1 = res_rscale + var1->weight + 1;
10561  i2 = res_rscale + var2->weight + 1;
10562  for (i = res_ndigits - 1; i >= 0; i--)
10563  {
10564  i1--;
10565  i2--;
10566  if (i1 >= 0 && i1 < var1ndigits)
10567  carry += var1digits[i1];
10568  if (i2 >= 0 && i2 < var2ndigits)
10569  carry += var2digits[i2];
10570 
10571  if (carry >= NBASE)
10572  {
10573  res_digits[i] = carry - NBASE;
10574  carry = 1;
10575  }
10576  else
10577  {
10578  res_digits[i] = carry;
10579  carry = 0;
10580  }
10581  }
10582 
10583  Assert(carry == 0); /* else we failed to allow for carry out */
10584 
10585  digitbuf_free(result->buf);
10586  result->ndigits = res_ndigits;
10587  result->buf = res_buf;
10588  result->digits = res_digits;
10589  result->weight = res_weight;
10590  result->dscale = res_dscale;
10591 
10592  /* Remove leading/trailing zeroes */
10593  strip_var(result);
10594 }
int weight
Definition: numeric.c:307
static void strip_var(NumericVar *var)
Definition: numeric.c:10857
#define digitbuf_alloc(ndigits)
Definition: numeric.c:487
int ndigits
Definition: numeric.c:306
int dscale
Definition: numeric.c:309
#define digitbuf_free(buf)
Definition: numeric.c:489
int16 NumericDigit
Definition: numeric.c:103
NumericDigit * buf
Definition: numeric.c:310
#define NBASE
Definition: numeric.c:97
#define Assert(condition)
Definition: c.h:804
NumericDigit * digits
Definition: numeric.c:311
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 7953 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().

7954 {
7955  /*
7956  * Decide on the signs of the two variables what to do
7957  */
7958  if (var1->sign == NUMERIC_POS)
7959  {
7960  if (var2->sign == NUMERIC_POS)
7961  {
7962  /*
7963  * Both are positive result = +(ABS(var1) + ABS(var2))
7964  */
7965  add_abs(var1, var2, result);
7966  result->sign = NUMERIC_POS;
7967  }
7968  else
7969  {
7970  /*
7971  * var1 is positive, var2 is negative Must compare absolute values
7972  */
7973  switch (cmp_abs(var1, var2))
7974  {
7975  case 0:
7976  /* ----------
7977  * ABS(var1) == ABS(var2)
7978  * result = ZERO
7979  * ----------
7980  */
7981  zero_var(result);
7982  result->dscale = Max(var1->dscale, var2->dscale);
7983  break;
7984 
7985  case 1:
7986  /* ----------
7987  * ABS(var1) > ABS(var2)
7988  * result = +(ABS(var1) - ABS(var2))
7989  * ----------
7990  */
7991  sub_abs(var1, var2, result);
7992  result->sign = NUMERIC_POS;
7993  break;
7994 
7995  case -1:
7996  /* ----------
7997  * ABS(var1) < ABS(var2)
7998  * result = -(ABS(var2) - ABS(var1))
7999  * ----------
8000  */
8001  sub_abs(var2, var1, result);
8002  result->sign = NUMERIC_NEG;
8003  break;
8004  }
8005  }
8006  }
8007  else
8008  {
8009  if (var2->sign == NUMERIC_POS)
8010  {
8011  /* ----------
8012  * var1 is negative, var2 is positive
8013  * Must compare absolute values
8014  * ----------
8015  */
8016  switch (cmp_abs(var1, var2))
8017  {
8018  case 0:
8019  /* ----------
8020  * ABS(var1) == ABS(var2)
8021  * result = ZERO
8022  * ----------
8023  */
8024  zero_var(result);
8025  result->dscale = Max(var1->dscale, var2->dscale);
8026  break;
8027 
8028  case 1:
8029  /* ----------
8030  * ABS(var1) > ABS(var2)
8031  * result = -(ABS(var1) - ABS(var2))
8032  * ----------
8033  */
8034  sub_abs(var1, var2, result);
8035  result->sign = NUMERIC_NEG;
8036  break;
8037 
8038  case -1:
8039  /* ----------
8040  * ABS(var1) < ABS(var2)
8041  * result = +(ABS(var2) - ABS(var1))
8042  * ----------
8043  */
8044  sub_abs(var2, var1, result);
8045  result->sign = NUMERIC_POS;
8046  break;
8047  }
8048  }
8049  else
8050  {
8051  /* ----------
8052  * Both are negative
8053  * result = -(ABS(var1) + ABS(var2))
8054  * ----------
8055  */
8056  add_abs(var1, var2, result);
8057  result->sign = NUMERIC_NEG;
8058  }
8059  }
8060 }
static void sub_abs(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:10607
static void add_abs(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:10522
#define NUMERIC_POS
Definition: numeric.c:167
int dscale
Definition: numeric.c:309
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:308
static void zero_var(NumericVar *var)
Definition: numeric.c:6797
static int cmp_abs(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:10444
#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_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:487
int ndigits
Definition: numeric.c:306
#define digitbuf_free(buf)
Definition: numeric.c:489
NumericDigit * buf
Definition: numeric.c:310
NumericDigit * digits
Definition: numeric.c:311

◆ apply_typmod()

static void apply_typmod ( NumericVar var,
int32  typmod 
)
static

Definition at line 7436 of file numeric.c.

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

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

7437 {
7438  int precision;
7439  int scale;
7440  int maxdigits;
7441  int ddigits;
7442  int i;
7443 
7444  /* Do nothing if we have a default typmod (-1) */
7445  if (typmod < (int32) (VARHDRSZ))
7446  return;
7447 
7448  typmod -= VARHDRSZ;
7449  precision = (typmod >> 16) & 0xffff;
7450  scale = typmod & 0xffff;
7451  maxdigits = precision - scale;
7452 
7453  /* Round to target scale (and set var->dscale) */
7454  round_var(var, scale);
7455 
7456  /*
7457  * Check for overflow - note we can't do this before rounding, because
7458  * rounding could raise the weight. Also note that the var's weight could
7459  * be inflated by leading zeroes, which will be stripped before storage
7460  * but perhaps might not have been yet. In any case, we must recognize a
7461  * true zero, whose weight doesn't mean anything.
7462  */
7463  ddigits = (var->weight + 1) * DEC_DIGITS;
7464  if (ddigits > maxdigits)
7465  {
7466  /* Determine true weight; and check for all-zero result */
7467  for (i = 0; i < var->ndigits; i++)
7468  {
7469  NumericDigit dig = var->digits[i];
7470 
7471  if (dig)
7472  {
7473  /* Adjust for any high-order decimal zero digits */
7474 #if DEC_DIGITS == 4
7475  if (dig < 10)
7476  ddigits -= 3;
7477  else if (dig < 100)
7478  ddigits -= 2;
7479  else if (dig < 1000)
7480  ddigits -= 1;
7481 #elif DEC_DIGITS == 2
7482  if (dig < 10)
7483  ddigits -= 1;
7484 #elif DEC_DIGITS == 1
7485  /* no adjustment */
7486 #else
7487 #error unsupported NBASE
7488 #endif
7489  if (ddigits > maxdigits)
7490  ereport(ERROR,
7491  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
7492  errmsg("numeric field overflow"),
7493  errdetail("A field with precision %d, scale %d must round to an absolute value less than %s%d.",
7494  precision, scale,
7495  /* Display 10^0 as 1 */
7496  maxdigits ? "10^" : "",
7497  maxdigits ? maxdigits : 1
7498  )));
7499  break;
7500  }
7501  ddigits -= DEC_DIGITS;
7502  }
7503  }
7504 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:10689
int weight
Definition: numeric.c:307
#define VARHDRSZ
Definition: c.h:627
int errcode(int sqlerrcode)
Definition: elog.c:698
int scale
Definition: pgbench.c:189
int ndigits
Definition: numeric.c:306
signed int int32
Definition: c.h:429
#define ERROR
Definition: elog.h:46
int16 NumericDigit
Definition: numeric.c:103
int errdetail(const char *fmt,...)
Definition: elog.c:1042
#define ereport(elevel,...)
Definition: elog.h:157
int maxdigits
Definition: informix.c:665
NumericDigit * digits
Definition: numeric.c:311
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i
#define DEC_DIGITS
Definition: numeric.c:99

◆ apply_typmod_special()

static void apply_typmod_special ( Numeric  num,
int32  typmod 
)
static

Definition at line 7513 of file numeric.c.

References Assert, ereport, errcode(), errdetail(), errmsg(), ERROR, NUMERIC_IS_NAN, NUMERIC_IS_SPECIAL, scale, and VARHDRSZ.

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

7514 {
7515  int precision;
7516  int scale;
7517 
7518  Assert(NUMERIC_IS_SPECIAL(num)); /* caller error if not */
7519 
7520  /*
7521  * NaN is allowed regardless of the typmod; that's rather dubious perhaps,
7522  * but it's a longstanding behavior. Inf is rejected if we have any
7523  * typmod restriction, since an infinity shouldn't be claimed to fit in
7524  * any finite number of digits.
7525  */
7526  if (NUMERIC_IS_NAN(num))
7527  return;
7528 
7529  /* Do nothing if we have a default typmod (-1) */
7530  if (typmod < (int32) (VARHDRSZ))
7531  return;
7532 
7533  typmod -= VARHDRSZ;
7534  precision = (typmod >> 16) & 0xffff;
7535  scale = typmod & 0xffff;
7536 
7537  ereport(ERROR,
7538  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
7539  errmsg("numeric field overflow"),
7540  errdetail("A field with precision %d, scale %d cannot hold an infinite value.",
7541  precision, scale)));
7542 }
#define VARHDRSZ
Definition: c.h:627
int errcode(int sqlerrcode)
Definition: elog.c:698
int scale
Definition: pgbench.c:189
signed int int32
Definition: c.h:429
#define NUMERIC_IS_SPECIAL(n)
Definition: numeric.c:174
#define ERROR
Definition: elog.h:46
int errdetail(const char *fmt,...)
Definition: elog.c:1042
#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

◆ ceil_var()

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

Definition at line 9158 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().

9159 {
9160  NumericVar tmp;
9161 
9162  init_var(&tmp);
9163  set_var_from_var(var, &tmp);
9164 
9165  trunc_var(&tmp, 0);
9166 
9167  if (var->sign == NUMERIC_POS && cmp_var(var, &tmp) != 0)
9168  add_var(&tmp, &const_one, &tmp);
9169 
9170  set_var_from_var(&tmp, result);
9171  free_var(&tmp);
9172 }
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:10795
static const NumericVar const_one
Definition: numeric.c:420
#define NUMERIC_POS
Definition: numeric.c:167
int sign
Definition: numeric.c:308
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:7895
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:7953
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:7031
#define init_var(v)
Definition: numeric.c:495

◆ cmp_abs()

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

Definition at line 10444 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().

10445 {
10446  return cmp_abs_common(var1->digits, var1->ndigits, var1->weight,
10447  var2->digits, var2->ndigits, var2->weight);
10448 }
int weight
Definition: numeric.c:307
int ndigits
Definition: numeric.c:306
NumericDigit * digits
Definition: numeric.c:311
static int cmp_abs_common(const NumericDigit *var1digits, int var1ndigits, int var1weight, const NumericDigit *var2digits, int var2ndigits, int var2weight)
Definition: numeric.c:10458

◆ 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 10458 of file numeric.c.

Referenced by cmp_abs(), and cmp_var_common().

10460 {
10461  int i1 = 0;
10462  int i2 = 0;
10463 
10464  /* Check any digits before the first common digit */
10465 
10466  while (var1weight > var2weight && i1 < var1ndigits)
10467  {
10468  if (var1digits[i1++] != 0)
10469  return 1;
10470  var1weight--;
10471  }
10472  while (var2weight > var1weight && i2 < var2ndigits)
10473  {
10474  if (var2digits[i2++] != 0)
10475  return -1;
10476  var2weight--;
10477  }
10478 
10479  /* At this point, either w1 == w2 or we've run out of digits */
10480 
10481  if (var1weight == var2weight)
10482  {
10483  while (i1 < var1ndigits && i2 < var2ndigits)
10484  {
10485  int stat = var1digits[i1++] - var2digits[i2++];
10486 
10487  if (stat)
10488  {
10489  if (stat > 0)
10490  return 1;
10491  return -1;
10492  }
10493  }
10494  }
10495 
10496  /*
10497  * At this point, we've run out of digits on one side or the other; so any
10498  * remaining nonzero digits imply that side is larger
10499  */
10500  while (i1 < var1ndigits)
10501  {
10502  if (var1digits[i1++] != 0)
10503  return 1;
10504  }
10505  while (i2 < var2ndigits)
10506  {
10507  if (var2digits[i2++] != 0)
10508  return -1;
10509  }
10510 
10511  return 0;
10512 }

◆ cmp_numerics()

static int cmp_numerics ( Numeric  num1,
Numeric  num2 
)
static

Definition at line 2368 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().

2369 {
2370  int result;
2371 
2372  /*
2373  * We consider all NANs to be equal and larger than any non-NAN (including
2374  * Infinity). This is somewhat arbitrary; the important thing is to have
2375  * a consistent sort order.
2376  */
2377  if (NUMERIC_IS_SPECIAL(num1))
2378  {
2379  if (NUMERIC_IS_NAN(num1))
2380  {
2381  if (NUMERIC_IS_NAN(num2))
2382  result = 0; /* NAN = NAN */
2383  else
2384  result = 1; /* NAN > non-NAN */
2385  }
2386  else if (NUMERIC_IS_PINF(num1))
2387  {
2388  if (NUMERIC_IS_NAN(num2))
2389  result = -1; /* PINF < NAN */
2390  else if (NUMERIC_IS_PINF(num2))
2391  result = 0; /* PINF = PINF */
2392  else
2393  result = 1; /* PINF > anything else */
2394  }
2395  else /* num1 must be NINF */
2396  {
2397  if (NUMERIC_IS_NINF(num2))
2398  result = 0; /* NINF = NINF */
2399  else
2400  result = -1; /* NINF < anything else */
2401  }
2402  }
2403  else if (NUMERIC_IS_SPECIAL(num2))
2404  {
2405  if (NUMERIC_IS_NINF(num2))
2406  result = 1; /* normal > NINF */
2407  else
2408  result = -1; /* normal < NAN or PINF */
2409  }
2410  else
2411  {
2412  result = cmp_var_common(NUMERIC_DIGITS(num1), NUMERIC_NDIGITS(num1),
2413  NUMERIC_WEIGHT(num1), NUMERIC_SIGN(num1),
2414  NUMERIC_DIGITS(num2), NUMERIC_NDIGITS(num2),
2415  NUMERIC_WEIGHT(num2), NUMERIC_SIGN(num2));
2416  }
2417 
2418  return result;
2419 }
#define NUMERIC_IS_SPECIAL(n)
Definition: numeric.c:174
#define NUMERIC_DIGITS(num)
Definition: numeric.c:497
#define NUMERIC_IS_PINF(n)
Definition: numeric.c:206
#define NUMERIC_SIGN(n)
Definition: numeric.c:237
#define NUMERIC_NDIGITS(num)
Definition: numeric.c:499
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:7910
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:247
#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 7895 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().

7896 {
7897  return cmp_var_common(var1->digits, var1->ndigits,
7898  var1->weight, var1->sign,
7899  var2->digits, var2->ndigits,
7900  var2->weight, var2->sign);
7901 }
int weight
Definition: numeric.c:307
int ndigits
Definition: numeric.c:306
int sign
Definition: numeric.c:308
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:7910
NumericDigit * digits
Definition: numeric.c:311

◆ 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 7910 of file numeric.c.

References cmp_abs_common(), NUMERIC_NEG, and NUMERIC_POS.

Referenced by cmp_numerics(), and cmp_var().

7914 {
7915  if (var1ndigits == 0)
7916  {
7917  if (var2ndigits == 0)
7918  return 0;
7919  if (var2sign == NUMERIC_NEG)
7920  return 1;
7921  return -1;
7922  }
7923  if (var2ndigits == 0)
7924  {
7925  if (var1sign == NUMERIC_POS)
7926  return 1;
7927  return -1;
7928  }
7929 
7930  if (var1sign == NUMERIC_POS)
7931  {
7932  if (var2sign == NUMERIC_NEG)
7933  return 1;
7934  return cmp_abs_common(var1digits, var1ndigits, var1weight,
7935  var2digits, var2ndigits, var2weight);
7936  }
7937 
7938  if (var2sign == NUMERIC_POS)
7939  return -1;
7940 
7941  return cmp_abs_common(var2digits, var2ndigits, var2weight,
7942  var1digits, var1ndigits, var1weight);
7943 }
#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:10458

◆ 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 1791 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().

1794 {
1795  NumericVar bound1_var;
1796  NumericVar bound2_var;
1797  NumericVar operand_var;
1798 
1799  init_var_from_num(bound1, &bound1_var);
1800  init_var_from_num(bound2, &bound2_var);
1801  init_var_from_num(operand, &operand_var);
1802 
1803  if (!reversed_bounds)
1804  {
1805  sub_var(&operand_var, &bound1_var, &operand_var);
1806  sub_var(&bound2_var, &bound1_var, &bound2_var);
1807  }
1808  else
1809  {
1810  sub_var(&bound1_var, &operand_var, &operand_var);
1811  sub_var(&bound1_var, &bound2_var, &bound2_var);
1812  }
1813 
1814  mul_var(&operand_var, count_var, &operand_var,
1815  operand_var.dscale + count_var->dscale);
1816  div_var(&operand_var, &bound2_var, result_var,
1817  select_div_scale(&operand_var, &bound2_var), true);
1818  add_var(result_var, &const_one, result_var);
1819  floor_var(result_var, result_var);
1820 
1821  free_var(&bound1_var);
1822  free_var(&bound2_var);
1823  free_var(&operand_var);
1824 }
static const NumericVar const_one
Definition: numeric.c:420
static void div_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:8399
int dscale
Definition: numeric.c:309
static int select_div_scale(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:8990
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:8191
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7953
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8070
static void floor_var(const NumericVar *var, NumericVar *result)
Definition: numeric.c:9182

◆ div_mod_var()

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

Definition at line 9088 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().

9090 {
9091  NumericVar q;
9092  NumericVar r;
9093 
9094  init_var(&q);
9095  init_var(&r);
9096 
9097  /*
9098  * Use div_var_fast() to get an initial estimate for the integer quotient.
9099  * This might be inaccurate (per the warning in div_var_fast's comments),
9100  * but we can correct it below.
9101  */
9102  div_var_fast(var1, var2, &q, 0, false);
9103 
9104  /* Compute initial estimate of remainder using the quotient estimate. */
9105  mul_var(var2, &q, &r, var2->dscale);
9106  sub_var(var1, &r, &r);
9107 
9108  /*
9109  * Adjust the results if necessary --- the remainder should have the same
9110  * sign as var1, and its absolute value should be less than the absolute
9111  * value of var2.
9112  */
9113  while (r.ndigits != 0 && r.sign != var1->sign)
9114  {
9115  /* The absolute value of the quotient is too large */
9116  if (var1->sign == var2->sign)
9117  {
9118  sub_var(&q, &const_one, &q);
9119  add_var(&r, var2, &r);
9120  }
9121  else
9122  {
9123  add_var(&q, &const_one, &q);
9124  sub_var(&r, var2, &r);
9125  }
9126  }
9127 
9128  while (cmp_abs(&r, var2) >= 0)
9129  {
9130  /* The absolute value of the quotient is too small */
9131  if (var1->sign == var2->sign)
9132  {
9133  add_var(&q, &const_one, &q);
9134  sub_var(&r, var2, &r);
9135  }
9136  else
9137  {
9138  sub_var(&q, &const_one, &q);
9139  add_var(&r, var2, &r);
9140  }
9141  }
9142 
9143  set_var_from_var(&q, quot);
9144  set_var_from_var(&r, rem);
9145 
9146  free_var(&q);
9147  free_var(&r);
9148 }
static const NumericVar const_one
Definition: numeric.c:420
int ndigits
Definition: numeric.c:306
int dscale
Definition: numeric.c:309
int sign
Definition: numeric.c:308
static void div_var_fast(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:8684
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:8191
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7953
static int cmp_abs(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:10444
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8070
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:7031
#define init_var(v)
Definition: numeric.c:495

◆ div_var()

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

Definition at line 8399 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().

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

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

4596 {
4597  NumericVar X;
4598  NumericVar X2;
4599  MemoryContext old_context;
4600 
4601  /* Count NaN/infinity inputs separately from all else */
4602  if (NUMERIC_IS_SPECIAL(newval))
4603  {
4604  if (NUMERIC_IS_PINF(newval))
4605  state->pInfcount++;
4606  else if (NUMERIC_IS_NINF(newval))
4607  state->nInfcount++;
4608  else
4609  state->NaNcount++;
4610  return;
4611  }
4612 
4613  /* load processed number in short-lived context */
4614  init_var_from_num(newval, &X);
4615 
4616  /*
4617  * Track the highest input dscale that we've seen, to support inverse
4618  * transitions (see do_numeric_discard).
4619  */
4620  if (X.dscale > state->maxScale)
4621  {
4622  state->maxScale = X.dscale;
4623  state->maxScaleCount = 1;
4624  }
4625  else if (X.dscale == state->maxScale)
4626  state->maxScaleCount++;
4627 
4628  /* if we need X^2, calculate that in short-lived context */
4629  if (state->calcSumX2)
4630  {
4631  init_var(&X2);
4632  mul_var(&X, &X, &X2, X.dscale * 2);
4633  }
4634 
4635  /* The rest of this needs to work in the aggregate context */
4636  old_context = MemoryContextSwitchTo(state->agg_context);
4637 
4638  state->N++;
4639 
4640  /* Accumulate sums */
4641  accum_sum_add(&(state->sumX), &X);
4642 
4643  if (state->calcSumX2)
4644  accum_sum_add(&(state->sumX2), &X2);
4645 
4646  MemoryContextSwitchTo(old_context);
4647 }
int64 nInfcount
Definition: numeric.c:4544
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext agg_context
Definition: numeric.c:4535
int dscale
Definition: numeric.c:309
#define NUMERIC_IS_SPECIAL(n)
Definition: numeric.c:174
static void accum_sum_add(NumericSumAccum *accum, const NumericVar *var1)
Definition: numeric.c:10914
#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:8191
#define NUMERIC_IS_NINF(n)
Definition: numeric.c:207
NumericSumAccum sumX2
Definition: numeric.c:4538
int64 pInfcount
Definition: numeric.c:4543
int64 NaNcount
Definition: numeric.c:4542
int64 maxScaleCount
Definition: numeric.c:4540
NumericSumAccum sumX
Definition: numeric.c:4537
#define init_var(v)
Definition: numeric.c:495

◆ do_numeric_discard()

static bool do_numeric_discard ( NumericAggState state,
Numeric  newval 
)
static

Definition at line 4665 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().

4666 {
4667  NumericVar X;
4668  NumericVar X2;
4669  MemoryContext old_context;
4670 
4671  /* Count NaN/infinity inputs separately from all else */
4672  if (NUMERIC_IS_SPECIAL(newval))
4673  {
4674  if (NUMERIC_IS_PINF(newval))
4675  state->pInfcount--;
4676  else if (NUMERIC_IS_NINF(newval))
4677  state->nInfcount--;
4678  else
4679  state->NaNcount--;
4680  return true;
4681  }
4682 
4683  /* load processed number in short-lived context */
4684  init_var_from_num(newval, &X);
4685 
4686  /*
4687  * state->sumX's dscale is the maximum dscale of any of the inputs.
4688  * Removing the last input with that dscale would require us to recompute
4689  * the maximum dscale of the *remaining* inputs, which we cannot do unless
4690  * no more non-NaN inputs remain at all. So we report a failure instead,
4691  * and force the aggregation to be redone from scratch.
4692  */
4693  if (X.dscale == state->maxScale)
4694  {
4695  if (state->maxScaleCount > 1 || state->maxScale == 0)
4696  {
4697  /*
4698  * Some remaining inputs have same dscale, or dscale hasn't gotten
4699  * above zero anyway
4700  */
4701  state->maxScaleCount--;
4702  }
4703  else if (state->N == 1)
4704  {
4705  /* No remaining non-NaN inputs at all, so reset maxScale */
4706  state->maxScale = 0;
4707  state->maxScaleCount = 0;
4708  }
4709  else
4710  {
4711  /* Correct new maxScale is uncertain, must fail */
4712  return false;
4713  }
4714  }
4715 
4716  /* if we need X^2, calculate that in short-lived context */
4717  if (state->calcSumX2)
4718  {
4719  init_var(&X2);
4720  mul_var(&X, &X, &X2, X.dscale * 2);
4721  }
4722 
4723  /* The rest of this needs to work in the aggregate context */
4724  old_context = MemoryContextSwitchTo(state->agg_context);
4725 
4726  if (state->N-- > 1)
4727  {
4728  /* Negate X, to subtract it from the sum */
4729  X.sign = (X.sign == NUMERIC_POS ? NUMERIC_NEG : NUMERIC_POS);
4730  accum_sum_add(&(state->sumX), &X);
4731 
4732  if (state->calcSumX2)
4733  {
4734  /* Negate X^2. X^2 is always positive */
4735  X2.sign = NUMERIC_NEG;
4736  accum_sum_add(&(state->sumX2), &X2);
4737  }
4738  }
4739  else
4740  {
4741  /* Zero the sums */
4742  Assert(state->N == 0);
4743 
4744  accum_sum_reset(&state->sumX);
4745  if (state->calcSumX2)
4746  accum_sum_reset(&state->sumX2);
4747  }
4748 
4749  MemoryContextSwitchTo(old_context);
4750 
4751  return true;
4752 }
int64 nInfcount
Definition: numeric.c:4544
#define NUMERIC_POS
Definition: numeric.c:167
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext agg_context
Definition: numeric.c:4535
int dscale
Definition: numeric.c:309
#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:10914
int sign
Definition: numeric.c:308
#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:8191
#define NUMERIC_IS_NINF(n)
Definition: numeric.c:207
NumericSumAccum sumX2
Definition: numeric.c:4538
static void accum_sum_reset(NumericSumAccum *accum)
Definition: numeric.c:10898
int64 pInfcount
Definition: numeric.c:4543
int64 NaNcount
Definition: numeric.c:4542
int64 maxScaleCount
Definition: numeric.c:4540
NumericSumAccum sumX
Definition: numeric.c:4537
#define init_var(v)
Definition: numeric.c:495

◆ duplicate_numeric()

static Numeric duplicate_numeric ( Numeric  num)
static

Definition at line 7295 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().

7296 {
7297  Numeric res;
7298 
7299  res = (Numeric) palloc(VARSIZE(num));
7300  memcpy(res, num, VARSIZE(num));
7301  return res;
7302 }
#define VARSIZE(PTR)
Definition: postgres.h:316
struct NumericData * Numeric
Definition: numeric.h:43
void * palloc(Size size)
Definition: mcxt.c:1062

◆ estimate_ln_dweight()

static int estimate_ln_dweight ( const NumericVar var)
static

Definition at line 9881 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().

9882 {
9883  int ln_dweight;
9884 
9885  if (cmp_var(var, &const_zero_point_nine) >= 0 &&
9886  cmp_var(var, &const_one_point_one) <= 0)
9887  {
9888  /*
9889  * 0.9 <= var <= 1.1
9890  *
9891  * ln(var) has a negative weight (possibly very large). To get a
9892  * reasonably accurate result, estimate it using ln(1+x) ~= x.
9893  */
9894  NumericVar x;
9895 
9896  init_var(&x);
9897  sub_var(var, &const_one, &x);
9898 
9899  if (x.ndigits > 0)
9900  {
9901  /* Use weight of most significant decimal digit of x */
9902  ln_dweight = x.weight * DEC_DIGITS + (int) log10(x.digits[0]);
9903  }
9904  else
9905  {
9906  /* x = 0. Since ln(1) = 0 exactly, we don't need extra digits */
9907  ln_dweight = 0;
9908  }
9909 
9910  free_var(&x);
9911  }
9912  else
9913  {
9914  /*
9915  * Estimate the logarithm using the first couple of digits from the
9916  * input number. This will give an accurate result whenever the input
9917  * is not too close to 1.
9918  */
9919  if (var->ndigits > 0)
9920  {
9921  int digits;
9922  int dweight;
9923  double ln_var;
9924 
9925  digits = var->digits[0];
9926  dweight = var->weight * DEC_DIGITS;
9927 
9928  if (var->ndigits > 1)
9929  {
9930  digits = digits * NBASE + var->digits[1];
9931  dweight -= DEC_DIGITS;
9932  }
9933 
9934  /*----------
9935  * We have var ~= digits * 10^dweight
9936  * so ln(var) ~= ln(digits) + dweight * ln(10)
9937  *----------
9938  */
9939  ln_var = log((double) digits) + dweight * 2.302585092994046;
9940  ln_dweight = (int) log10(Abs(ln_var));
9941  }
9942  else
9943  {
9944  /* Caller should fail on ln(0), but for the moment return zero */
9945  ln_dweight = 0;
9946  }
9947  }
9948 
9949  return ln_dweight;
9950 }
int weight
Definition: numeric.c:307
static const NumericVar const_zero_point_nine
Definition: numeric.c:447
static const NumericVar const_one
Definition: numeric.c:420
static void ln_var(const NumericVar *arg, NumericVar *result, int rscale)
Definition: numeric.c:9959
int ndigits
Definition: numeric.c:306
#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:7895
static void free_var(NumericVar *var)
Definition: numeric.c:6781
NumericDigit * digits
Definition: numeric.c:311
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8070
#define DEC_DIGITS
Definition: numeric.c:99
static const NumericVar const_one_point_one
Definition: numeric.c:457
#define init_var(v)
Definition: numeric.c:495
int digits
Definition: informix.c:666

◆ exp_var()

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

Definition at line 9755 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, and NumericVar::weight.

Referenced by numeric_exp(), and power_var().

9756 {
9757  NumericVar x;
9758  NumericVar elem;
9759  NumericVar ni;
9760  double val;
9761  int dweight;
9762  int ndiv2;
9763  int sig_digits;
9764  int local_rscale;
9765 
9766  init_var(&x);
9767  init_var(&elem);
9768  init_var(&ni);
9769 
9770  set_var_from_var(arg, &x);
9771 
9772  /*
9773  * Estimate the dweight of the result using floating point arithmetic, so
9774  * that we can choose an appropriate local rscale for the calculation.
9775  */
9777 
9778  /* Guard against overflow */
9779  /* If you change this limit, see also power_var()'s limit */
9780  if (Abs(val) >= NUMERIC_MAX_RESULT_SCALE * 3)
9781  ereport(ERROR,
9782  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
9783  errmsg("value overflows numeric format")));
9784 
9785  /* decimal weight = log10(e^x) = x * log10(e) */
9786  dweight = (int) (val * 0.434294481903252);
9787 
9788  /*
9789  * Reduce x to the range -0.01 <= x <= 0.01 (approximately) by dividing by
9790  * 2^n, to improve the convergence rate of the Taylor series.
9791  */
9792  if (Abs(val) > 0.01)
9793  {
9794  NumericVar tmp;
9795 
9796  init_var(&tmp);
9797  set_var_from_var(&const_two, &tmp);
9798 
9799  ndiv2 = 1;
9800  val /= 2;
9801 
9802  while (Abs(val) > 0.01)
9803  {
9804  ndiv2++;
9805  val /= 2;
9806  add_var(&tmp, &tmp, &tmp);
9807  }
9808 
9809  local_rscale = x.dscale + ndiv2;
9810  div_var_fast(&x, &tmp, &x, local_rscale, true);
9811 
9812  free_var(&tmp);
9813  }
9814  else
9815  ndiv2 = 0;
9816 
9817  /*
9818  * Set the scale for the Taylor series expansion. The final result has
9819  * (dweight + rscale + 1) significant digits. In addition, we have to
9820  * raise the Taylor series result to the power 2^ndiv2, which introduces
9821  * an error of up to around log10(2^ndiv2) digits, so work with this many
9822  * extra digits of precision (plus a few more for good measure).
9823  */
9824  sig_digits = 1 + dweight + rscale + (int) (ndiv2 * 0.301029995663981);
9825  sig_digits = Max(sig_digits, 0) + 8;
9826 
9827  local_rscale = sig_digits - 1;
9828 
9829  /*
9830  * Use the Taylor series
9831  *
9832  * exp(x) = 1 + x + x^2/2! + x^3/3! + ...
9833  *
9834  * Given the limited range of x, this should converge reasonably quickly.
9835  * We run the series until the terms fall below the local_rscale limit.
9836  */
9837  add_var(&const_one, &x, result);
9838 
9839  mul_var(&x, &x, &elem, local_rscale);
9840  set_var_from_var(&const_two, &ni);
9841  div_var_fast(&elem, &ni, &elem, local_rscale, true);
9842 
9843  while (elem.ndigits != 0)
9844  {
9845  add_var(result, &elem, result);
9846 
9847  mul_var(&elem, &x, &elem, local_rscale);
9848  add_var(&ni, &const_one, &ni);
9849  div_var_fast(&elem, &ni, &elem, local_rscale, true);
9850  }
9851 
9852  /*
9853  * Compensate for the argument range reduction. Since the weight of the
9854  * result doubles with each multiplication, we can reduce the local rscale
9855  * as we proceed.
9856  */
9857  while (ndiv2-- > 0)
9858  {
9859  local_rscale = sig_digits - result->weight * 2 * DEC_DIGITS;
9860  local_rscale = Max(local_rscale, NUMERIC_MIN_DISPLAY_SCALE);
9861  mul_var(result, result, result, local_rscale);
9862  }
9863 
9864  /* Round to requested rscale */
9865  round_var(result, rscale);
9866 
9867  free_var(&x);
9868  free_var(&elem);
9869  free_var(&ni);
9870 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:10689
int weight
Definition: numeric.c:307
static const NumericVar const_one
Definition: numeric.c:420
int errcode(int sqlerrcode)
Definition: elog.c:698
int ndigits
Definition: numeric.c:306
int dscale
Definition: numeric.c:309
#define Abs(x)
Definition: c.h:992
#define ERROR
Definition: elog.h:46
static double numericvar_to_double_no_overflow(const NumericVar *var)
Definition: numeric.c:7863
#define NUMERIC_MIN_DISPLAY_SCALE
Definition: numeric.h:30
#define NUMERIC_MAX_RESULT_SCALE
Definition: numeric.h:32
static void div_var_fast(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:8684
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:427
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:8191
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7953
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:495

◆ float4_numeric()

Datum float4_numeric ( PG_FUNCTION_ARGS  )

Definition at line 4426 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.

4427 {
4429  Numeric res;
4430  NumericVar result;
4431  char buf[FLT_DIG + 100];
4432 
4433  if (isnan(val))
4435 
4436  if (isinf(val))
4437  {
4438  if (val < 0)
4440  else
4442  }
4443 
4444  snprintf(buf, sizeof(buf), "%.*g", FLT_DIG, val);
4445 
4446  init_var(&result);
4447 
4448  /* Assume we need not worry about leading/trailing spaces */
4449  (void) set_var_from_str(buf, buf, &result);
4450 
4451  res = make_result(&result);
4452 
4453  free_var(&result);
4454 
4455  PG_RETURN_NUMERIC(res);
4456 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
static char * buf
Definition: pg_test_fsync.c:68
static const NumericVar const_ninf
Definition: numeric.c:466
#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:463
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:460
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:7423
#define snprintf
Definition: port.h:216
long val
Definition: informix.c:664
#define init_var(v)
Definition: numeric.c:495

◆ float8_numeric()

Datum float8_numeric ( PG_FUNCTION_ARGS  )

Definition at line 4333 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().

4334 {
4336  Numeric res;
4337  NumericVar result;
4338  char buf[DBL_DIG + 100];
4339 
4340  if (isnan(val))
4342 
4343  if (isinf(val))
4344  {
4345  if (val < 0)
4347  else
4349  }
4350 
4351  snprintf(buf, sizeof(buf), "%.*g", DBL_DIG, val);
4352 
4353  init_var(&result);
4354 
4355  /* Assume we need not worry about leading/trailing spaces */
4356  (void) set_var_from_str(buf, buf, &result);
4357 
4358  res = make_result(&result);
4359 
4360  free_var(&result);
4361 
4362  PG_RETURN_NUMERIC(res);
4363 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
#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:466
static void free_var(NumericVar *var)
Definition: numeric.c:6781
static const NumericVar const_pinf
Definition: numeric.c:463
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:460
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:7423
#define snprintf
Definition: port.h:216
long val
Definition: informix.c:664
#define init_var(v)
Definition: numeric.c:495

◆ floor_var()

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

Definition at line 9182 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().

9183 {
9184  NumericVar tmp;
9185 
9186  init_var(&tmp);
9187  set_var_from_var(var, &tmp);
9188 
9189  trunc_var(&tmp, 0);
9190 
9191  if (var->sign == NUMERIC_NEG && cmp_var(var, &tmp) != 0)
9192  sub_var(&tmp, &const_one, &tmp);
9193 
9194  set_var_from_var(&tmp, result);
9195  free_var(&tmp);
9196 }
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:10795
static const NumericVar const_one
Definition: numeric.c:420
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:308
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:7895
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:8070
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:7031
#define init_var(v)
Definition: numeric.c:495

◆ 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_serialize(), ln_var(), log_var(), mod_var(), numeric(), numeric_add_opt_error(), numeric_avg(), numeric_avg_serialize(), numeric_ceil(), 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_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:308
#define digitbuf_free(buf)
Definition: numeric.c:489
#define NUMERIC_NAN
Definition: numeric.c:199
NumericDigit * buf
Definition: numeric.c:310
NumericDigit * digits
Definition: numeric.c:311

◆ gcd_var()

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

Definition at line 9205 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().

9206 {
9207  int res_dscale;
9208  int cmp;
9209  NumericVar tmp_arg;
9210  NumericVar mod;
9211 
9212  res_dscale = Max(var1->dscale, var2->dscale);
9213 
9214  /*
9215  * Arrange for var1 to be the number with the greater absolute value.
9216  *
9217  * This would happen automatically in the loop below, but avoids an
9218  * expensive modulo operation.
9219  */
9220  cmp = cmp_abs(var1, var2);
9221  if (cmp < 0)
9222  {
9223  const NumericVar *tmp = var1;
9224 
9225  var1 = var2;
9226  var2 = tmp;
9227  }
9228 
9229  /*
9230  * Also avoid the taking the modulo if the inputs have the same absolute
9231  * value, or if the smaller input is zero.
9232  */
9233  if (cmp == 0 || var2->ndigits == 0)
9234  {
9235  set_var_from_var(var1, result);
9236  result->sign = NUMERIC_POS;
9237  result->dscale = res_dscale;
9238  return;
9239  }
9240 
9241  init_var(&tmp_arg);
9242  init_var(&mod);
9243 
9244  /* Use the Euclidean algorithm to find the GCD */
9245  set_var_from_var(var1, &tmp_arg);
9246  set_var_from_var(var2, result);
9247 
9248  for (;;)
9249  {
9250  /* this loop can take a while, so allow it to be interrupted */
9252 
9253  mod_var(&tmp_arg, result, &mod);
9254  if (mod.ndigits == 0)
9255  break;
9256  set_var_from_var(result, &tmp_arg);
9257  set_var_from_var(&mod, result);
9258  }
9259  result->sign = NUMERIC_POS;
9260  result->dscale = res_dscale;
9261 
9262  free_var(&tmp_arg);
9263  free_var(&mod);
9264 }
#define NUMERIC_POS
Definition: numeric.c:167
int ndigits
Definition: numeric.c:306
int dscale
Definition: numeric.c:309
int sign
Definition: numeric.c:308
static void mod_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:9059
static void free_var(NumericVar *var)
Definition: numeric.c:6781
static int cmp_abs(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:10444
#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:495
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 1560 of file numeric.c.

References generate_series_step_numeric().

1561 {
1562  return generate_series_step_numeric(fcinfo);
1563 }
Datum generate_series_step_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:1566

◆ generate_series_step_numeric()

Datum generate_series_step_numeric ( PG_FUNCTION_ARGS  )

Definition at line 1566 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().

1567 {
1569  FuncCallContext *funcctx;
1570  MemoryContext oldcontext;
1571 
1572  if (SRF_IS_FIRSTCALL())
1573  {
1574  Numeric start_num = PG_GETARG_NUMERIC(0);
1575  Numeric stop_num = PG_GETARG_NUMERIC(1);
1576  NumericVar steploc = const_one;
1577 
1578  /* Reject NaN and infinities in start and stop values */
1579  if (NUMERIC_IS_SPECIAL(start_num))
1580  {
1581  if (NUMERIC_IS_NAN(start_num))
1582  ereport(ERROR,
1583  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1584  errmsg("start value cannot be NaN")));
1585  else
1586  ereport(ERROR,
1587  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1588  errmsg("start value cannot be infinity")));
1589  }
1590  if (NUMERIC_IS_SPECIAL(stop_num))
1591  {
1592  if (NUMERIC_IS_NAN(stop_num))
1593  ereport(ERROR,
1594  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1595  errmsg("stop value cannot be NaN")));
1596  else
1597  ereport(ERROR,
1598  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1599  errmsg("stop value cannot be infinity")));
1600  }
1601 
1602  /* see if we were given an explicit step size */
1603  if (PG_NARGS() == 3)
1604  {
1605  Numeric step_num = PG_GETARG_NUMERIC(2);
1606 
1607  if (NUMERIC_IS_SPECIAL(step_num))
1608  {
1609  if (NUMERIC_IS_NAN(step_num))
1610  ereport(ERROR,
1611  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1612  errmsg("step size cannot be NaN")));
1613  else
1614  ereport(ERROR,
1615  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1616  errmsg("step size cannot be infinity")));
1617  }
1618 
1619  init_var_from_num(step_num, &steploc);
1620 
1621  if (cmp_var(&steploc, &const_zero) == 0)
1622  ereport(ERROR,
1623  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1624  errmsg("step size cannot equal zero")));
1625  }
1626 
1627  /* create a function context for cross-call persistence */
1628  funcctx = SRF_FIRSTCALL_INIT();
1629 
1630  /*
1631  * Switch to memory context appropriate for multiple function calls.
1632  */
1633  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1634 
1635  /* allocate memory for user context */
1636  fctx = (generate_series_numeric_fctx *)
1638 
1639  /*
1640  * Use fctx to keep state from call to call. Seed current with the
1641  * original start value. We must copy the start_num and stop_num
1642  * values rather than pointing to them, since we may have detoasted
1643  * them in the per-call context.
1644  */
1645  init_var(&fctx->current);
1646  init_var(&fctx->stop);
1647  init_var(&fctx->step);
1648 
1649  set_var_from_num(start_num, &fctx->current);
1650  set_var_from_num(stop_num, &fctx->stop);
1651  set_var_from_var(&steploc, &fctx->step);
1652 
1653  funcctx->user_fctx = fctx;
1654  MemoryContextSwitchTo(oldcontext);
1655  }
1656 
1657  /* stuff done on every call of the function */
1658  funcctx = SRF_PERCALL_SETUP();
1659 
1660  /*
1661  * Get the saved state and use current state as the result of this
1662  * iteration.
1663  */
1664  fctx = funcctx->user_fctx;
1665 
1666  if ((fctx->step.sign == NUMERIC_POS &&
1667  cmp_var(&fctx->current, &fctx->stop) <= 0) ||
1668  (fctx->step.sign == NUMERIC_NEG &&
1669  cmp_var(&fctx->current, &fctx->stop) >= 0))
1670  {
1671  Numeric result = make_result(&fctx->current);
1672 
1673  /* switch to memory context appropriate for iteration calculation */
1674  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1675 
1676  /* increment current in preparation for next iteration */
1677  add_var(&fctx->current, &fctx->step, &fctx->current);
1678  MemoryContextSwitchTo(oldcontext);
1679 
1680  /* do when there is more left to send */
1681  SRF_RETURN_NEXT(funcctx, NumericGetDatum(result));
1682  }
1683  else
1684  /* do when there is no more left */
1685  SRF_RETURN_DONE(funcctx);
1686 }
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:294
static const NumericVar const_one
Definition: numeric.c:420
#define NumericGetDatum(X)
Definition: numeric.h:51
#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:298
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:300
#define NUMERIC_IS_SPECIAL(n)
Definition: numeric.c:174
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:308
#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:416
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:7895
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:52
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:7953
#define PG_NARGS()
Definition: fmgr.h:203
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:7423
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:495
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:318
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:296

◆ get_min_scale()

static int get_min_scale ( NumericVar var)
static

Definition at line 3982 of file numeric.c.

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

Referenced by numeric_min_scale(), and numeric_trim_scale().

3983 {
3984  int min_scale;
3985  int last_digit_pos;
3986 
3987  /*
3988  * Ordinarily, the input value will be "stripped" so that the last
3989  * NumericDigit is nonzero. But we don't want to get into an infinite
3990  * loop if it isn't, so explicitly find the last nonzero digit.
3991  */
3992  last_digit_pos = var->ndigits - 1;
3993  while (last_digit_pos >= 0 &&
3994  var->digits[last_digit_pos] == 0)
3995  last_digit_pos--;
3996 
3997  if (last_digit_pos >= 0)
3998  {
3999  /* compute min_scale assuming that last ndigit has no zeroes */
4000  min_scale = (last_digit_pos - var->weight) * DEC_DIGITS;
4001 
4002  /*
4003  * We could get a negative result if there are no digits after the
4004  * decimal point. In this case the min_scale must be zero.
4005  */
4006  if (min_scale > 0)
4007  {
4008  /*
4009  * Reduce min_scale if trailing digit(s) in last NumericDigit are
4010  * zero.
4011  */
4012  NumericDigit last_digit = var->digits[last_digit_pos];
4013 
4014  while (last_digit % 10 == 0)
4015  {
4016  min_scale--;
4017  last_digit /= 10;
4018  }
4019  }
4020  else
4021  min_scale = 0;
4022  }
4023  else
4024  min_scale = 0; /* result if input is zero */
4025 
4026  return min_scale;
4027 }
int weight
Definition: numeric.c:307
int ndigits
Definition: numeric.c:306
int16 NumericDigit
Definition: numeric.c:103
NumericDigit * digits
Definition: numeric.c:311
#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:307
static void error(void)
Definition: sql-dyntest.c:147
int ndigits
Definition: numeric.c:306
int dscale
Definition: numeric.c:309
#define NUMERIC_NEG
Definition: numeric.c:168
int sign
Definition: numeric.c:308
int16 NumericDigit
Definition: numeric.c:103
#define NBASE
Definition: numeric.c:97
NumericDigit * digits
Definition: numeric.c:311
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:307
int ndigits
Definition: numeric.c:306
static void div_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale, bool round)
Definition: numeric.c:8399
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:10254
static const NumericVar const_ten
Definition: numeric.c:432
static void free_var(NumericVar *var)
Definition: numeric.c:6781
NumericDigit * digits
Definition: numeric.c:311
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:495

◆ hash_numeric()

Datum hash_numeric ( PG_FUNCTION_ARGS  )

Definition at line 2560 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().

2561 {
2563  Datum digit_hash;
2564  Datum result;
2565  int weight;
2566  int start_offset;
2567  int end_offset;
2568  int i;
2569  int hash_len;
2571 
2572  /* If it's NaN or infinity, don't try to hash the rest of the fields */
2573  if (NUMERIC_IS_SPECIAL(key))
2574  PG_RETURN_UINT32(0);
2575 
2576  weight = NUMERIC_WEIGHT(key);
2577  start_offset = 0;
2578  end_offset = 0;
2579 
2580  /*
2581  * Omit any leading or trailing zeros from the input to the hash. The
2582  * numeric implementation *should* guarantee that leading and trailing
2583  * zeros are suppressed, but we're paranoid. Note that we measure the
2584  * starting and ending offsets in units of NumericDigits, not bytes.
2585  */
2586  digits = NUMERIC_DIGITS(key);
2587  for (i = 0; i < NUMERIC_NDIGITS(key); i++)
2588  {
2589  if (digits[i] != (NumericDigit) 0)
2590  break;
2591 
2592  start_offset++;
2593 
2594  /*
2595  * The weight is effectively the # of digits before the decimal point,
2596  * so decrement it for each leading zero we skip.
2597  */
2598  weight--;
2599  }
2600 
2601  /*
2602  * If there are no non-zero digits, then the value of the number is zero,
2603  * regardless of any other fields.
2604  */
2605  if (NUMERIC_NDIGITS(key) == start_offset)
2606  PG_RETURN_UINT32(-1);
2607 
2608  for (i = NUMERIC_NDIGITS(key) - 1; i >= 0; i--)
2609  {
2610  if (digits[i] != (NumericDigit) 0)
2611  break;
2612 
2613  end_offset++;
2614  }
2615 
2616  /* If we get here, there should be at least one non-zero digit */
2617  Assert(start_offset + end_offset < NUMERIC_NDIGITS(key));
2618 
2619  /*
2620  * Note that we don't hash on the Numeric's scale, since two numerics can
2621  * compare equal but have different scales. We also don't hash on the
2622  * sign, although we could: since a sign difference implies inequality,
2623  * this shouldn't affect correctness.
2624  */
2625  hash_len = NUMERIC_NDIGITS(key) - start_offset - end_offset;
2626  digit_hash = hash_any((unsigned char *) (NUMERIC_DIGITS(key) + start_offset),
2627  hash_len * sizeof(NumericDigit));
2628 
2629  /* Mix in the weight, via XOR */
2630  result = digit_hash ^ weight;
2631 
2632  PG_RETURN_DATUM(result);
2633 }
#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:497
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:499
uintptr_t Datum
Definition: postgres.h:411
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:247
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:52
#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 2640 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().

2641 {
2643  uint64 seed = PG_GETARG_INT64(1);
2644  Datum digit_hash;
2645  Datum result;
2646  int weight;
2647  int start_offset;
2648  int end_offset;
2649  int i;
2650  int hash_len;
2652 
2653  /* If it's NaN or infinity, don't try to hash the rest of the fields */
2654  if (NUMERIC_IS_SPECIAL(key))
2655  PG_RETURN_UINT64(seed);
2656 
2657  weight = NUMERIC_WEIGHT(key);
2658  start_offset = 0;
2659  end_offset = 0;
2660 
2661  digits = NUMERIC_DIGITS(key);
2662  for (i = 0; i < NUMERIC_NDIGITS(key); i++)
2663  {
2664  if (digits[i] != (NumericDigit) 0)
2665  break;
2666 
2667  start_offset++;
2668 
2669  weight--;
2670  }
2671 
2672  if (NUMERIC_NDIGITS(key) == start_offset)
2673  PG_RETURN_UINT64(seed - 1);
2674 
2675  for (i = NUMERIC_NDIGITS(key) - 1; i >= 0; i--)
2676  {
2677  if (digits[i] != (NumericDigit) 0)
2678  break;
2679 
2680  end_offset++;
2681  }
2682 
2683  Assert(start_offset + end_offset < NUMERIC_NDIGITS(key));
2684 
2685  hash_len = NUMERIC_NDIGITS(key) - start_offset - end_offset;
2686  digit_hash = hash_any_extended((unsigned char *) (NUMERIC_DIGITS(key)
2687  + start_offset),
2688  hash_len * sizeof(NumericDigit),
2689  seed);
2690 
2691  result = UInt64GetDatum(DatumGetUInt64(digit_hash) ^ weight);
2692 
2693  PG_RETURN_DATUM(result);
2694 }
#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:497
int16 NumericDigit
Definition: numeric.c:103
#define NUMERIC_NDIGITS(num)
Definition: numeric.c:499
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:247
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:52
#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 2425 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.

2426 {
2428  Numeric base = PG_GETARG_NUMERIC(1);
2429  Numeric offset = PG_GETARG_NUMERIC(2);
2430  bool sub = PG_GETARG_BOOL(3);
2431  bool less = PG_GETARG_BOOL(4);
2432  bool result;
2433 
2434  /*
2435  * Reject negative (including -Inf) or NaN offset. Negative is per spec,
2436  * and NaN is because appropriate semantics for that seem non-obvious.
2437  */
2438  if (NUMERIC_IS_NAN(offset) ||
2439  NUMERIC_IS_NINF(offset) ||
2440  NUMERIC_SIGN(offset) == NUMERIC_NEG)
2441  ereport(ERROR,
2442  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2443  errmsg("invalid preceding or following size in window function")));
2444 
2445  /*
2446  * Deal with cases where val and/or base is NaN, following the rule that
2447  * NaN sorts after non-NaN (cf cmp_numerics). The offset cannot affect
2448  * the conclusion.
2449  */
2450  if (NUMERIC_IS_NAN(val))
2451  {
2452  if (NUMERIC_IS_NAN(base))
2453  result = true; /* NAN = NAN */
2454  else
2455  result = !less; /* NAN > non-NAN */
2456  }
2457  else if (NUMERIC_IS_NAN(base))
2458  {
2459  result = less; /* non-NAN < NAN */
2460  }
2461 
2462  /*
2463  * Deal with infinite offset (necessarily +Inf, at this point).
2464  */
2465  else if (NUMERIC_IS_SPECIAL(offset))
2466  {
2467  Assert(NUMERIC_IS_PINF(offset));
2468  if (sub ? NUMERIC_IS_PINF(base) : NUMERIC_IS_NINF(base))
2469  {
2470  /*
2471  * base +/- offset would produce NaN, so return true for any val
2472  * (see in_range_float8_float8() for reasoning).
2473  */
2474  result = true;
2475  }
2476  else if (sub)
2477  {
2478  /* base - offset must be -inf */
2479  if (less)
2480  result = NUMERIC_IS_NINF(val); /* only -inf is <= sum */
2481  else
2482  result = true; /* any val is >= sum */
2483  }
2484  else
2485  {
2486  /* base + offset must be +inf */
2487  if (less)
2488  result = true; /* any val is <= sum */
2489  else
2490  result = NUMERIC_IS_PINF(val); /* only +inf is >= sum */
2491  }
2492  }
2493 
2494  /*
2495  * Deal with cases where val and/or base is infinite. The offset, being
2496  * now known finite, cannot affect the conclusion.
2497  */
2498  else if (NUMERIC_IS_SPECIAL(val))
2499  {
2500  if (NUMERIC_IS_PINF(val))
2501  {
2502  if (NUMERIC_IS_PINF(base))
2503  result = true; /* PINF = PINF */
2504  else
2505  result = !less; /* PINF > any other non-NAN */
2506  }
2507  else /* val must be NINF */
2508  {
2509  if (NUMERIC_IS_NINF(base))
2510  result = true; /* NINF = NINF */
2511  else
2512  result = less; /* NINF < anything else */
2513  }
2514  }
2515  else if (NUMERIC_IS_SPECIAL(base))
2516  {
2517  if (NUMERIC_IS_NINF(base))
2518  result = !less; /* normal > NINF */
2519  else
2520  result = less; /* normal < PINF */
2521  }
2522  else
2523  {
2524  /*
2525  * Otherwise go ahead and compute base +/- offset. While it's
2526  * possible for this to overflow the numeric format, it's unlikely
2527  * enough that we don't take measures to prevent it.
2528  */
2529  NumericVar valv;
2530  NumericVar basev;
2531  NumericVar offsetv;
2532  NumericVar sum;
2533 
2534  init_var_from_num(val, &valv);
2535  init_var_from_num(base, &basev);
2536  init_var_from_num(offset, &offsetv);
2537  init_var(&sum);
2538 
2539  if (sub)
2540  sub_var(&basev, &offsetv, &sum);
2541  else
2542  add_var(&basev, &offsetv, &sum);
2543 
2544  if (less)
2545  result = (cmp_var(&valv, &sum) <= 0);
2546  else
2547  result = (cmp_var(&valv, &sum) >= 0);
2548 
2549  free_var(&sum);
2550  }
2551 
2552  PG_FREE_IF_COPY(val, 0);
2553  PG_FREE_IF_COPY(base, 1);
2554  PG_FREE_IF_COPY(offset, 2);
2555 
2556  PG_RETURN_BOOL(result);
2557 }
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:237
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:7895
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:52
#define Assert(condition)
Definition: c.h:804
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7953
#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:8070
long val
Definition: informix.c:664
#define init_var(v)
Definition: numeric.c:495

◆ 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(), int8_avg_deserialize(), numeric_abbrev_convert(), numeric_add_opt_error(), numeric_avg_deserialize(), numeric_ceil(), numeric_deserialize(), 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_poly_deserialize(), 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:243
int weight
Definition: numeric.c:307
int ndigits
Definition: numeric.c:306
int dscale
Definition: numeric.c:309
int sign
Definition: numeric.c:308
#define NUMERIC_DIGITS(num)
Definition: numeric.c:497
#define NUMERIC_SIGN(n)
Definition: numeric.c:237
NumericDigit * buf
Definition: numeric.c:310
#define NUMERIC_NDIGITS(num)
Definition: numeric.c:499
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:247
NumericDigit * digits
Definition: numeric.c:311

◆ int2_accum()

Datum int2_accum ( PG_FUNCTION_ARGS  )

Definition at line 5327 of file numeric.c.

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

5328 {
5330 
5331  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
5332 
5333  /* Create the state data on the first call */
5334  if (state == NULL)
5335  state = makePolyNumAggState(fcinfo, true);
5336 
5337  if (!PG_ARGISNULL(1))
5338  {
5339 #ifdef HAVE_INT128
5340  do_int128_accum(state, (int128) PG_GETARG_INT16(1));
5341 #else
5343 #endif
5344  }
5345 
5346  PG_RETURN_POINTER(state);
5347 }
#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:4595
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4079
#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:5322

◆ 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:4665
#define ERROR
Definition: elog.h:46
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4079
#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:4588
#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:4588
#define elog(elevel,...)
Definition: elog.h:232

◆ int2_numeric()

Datum int2_numeric ( PG_FUNCTION_ARGS  )

Definition at line 4283 of file numeric.c.

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

4284 {
4285  int16 val = PG_GETARG_INT16(0);
4286 
4288 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
signed short int16
Definition: c.h:428
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4079
#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_POINTER(x)
Definition: fmgr.h:361
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4588
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ int2int4_sum()

Datum int2int4_sum ( PG_FUNCTION_ARGS  )

Definition at line 6639 of file numeric.c.

References ARR_DATA_PTR, ARR_HASNULL, ARR_OVERHEAD_NONULLS, ARR_SIZE, Int8TransTypeData::count, DEC_DIGITS, NumericVar::digits, digits, NumericVar::dscale, dump_numeric, dump_var, elog, ERROR, i, Int64GetDatumFast, NumericVar::ndigits, NUMERIC_DIGITS, NUMERIC_DSCALE, NUMERIC_NAN, NUMERIC_NDIGITS, NUMERIC_NEG, NUMERIC_NINF, NUMERIC_PINF, NUMERIC_POS, NUMERIC_SIGN, NUMERIC_WEIGHT, PG_GETARG_ARRAYTYPE_P, PG_RETURN_DATUM, PG_RETURN_NULL, printf, NumericVar::sign, generate_unaccent_rules::str, Int8TransTypeData::sum, and NumericVar::weight.

6640 {
6641  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
6642  Int8TransTypeData *transdata;
6643 
6644  if (ARR_HASNULL(transarray) ||
6645  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
6646  elog(ERROR, "expected 2-element int8 array");
6647  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
6648 
6649  /* SQL defines SUM of no values to be NULL */
6650  if (transdata->count == 0)
6651  PG_RETURN_NULL();
6652 
6653  PG_RETURN_DATUM(Int64GetDatumFast(transdata->sum));
6654 }
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:303