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)
 
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 5262 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 4486 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:154
#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:416
#define NUMERIC_HEADER_IS_SHORT(n)
Definition: numeric.c:184
#define VARHDRSZ
Definition: c.h:623
unsigned short uint16
Definition: c.h:428

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

Function Documentation

◆ accum_sum_add()

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

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

10854 {
10855  int32 *accum_digits;
10856  int i,
10857  val_i;
10858  int val_ndigits;
10859  NumericDigit *val_digits;
10860 
10861  /*
10862  * If we have accumulated too many values since the last carry
10863  * propagation, do it now, to avoid overflowing. (We could allow more
10864  * than NBASE - 1, if we reserved two extra digits, rather than one, for
10865  * carry propagation. But even with NBASE - 1, this needs to be done so
10866  * seldom, that the performance difference is negligible.)
10867  */
10868  if (accum->num_uncarried == NBASE - 1)
10869  accum_sum_carry(accum);
10870 
10871  /*
10872  * Adjust the weight or scale of the old value, so that it can accommodate
10873  * the new value.
10874  */
10875  accum_sum_rescale(accum, val);
10876 
10877  /* */
10878  if (val->sign == NUMERIC_POS)
10879  accum_digits = accum->pos_digits;
10880  else
10881  accum_digits = accum->neg_digits;
10882 
10883  /* copy these values into local vars for speed in loop */
10884  val_ndigits = val->ndigits;
10885  val_digits = val->digits;
10886 
10887  i = accum->weight - val->weight;
10888  for (val_i = 0; val_i < val_ndigits; val_i++)
10889  {
10890  accum_digits[i] += (int32) val_digits[val_i];
10891  i++;
10892  }
10893 
10894  accum->num_uncarried++;
10895 }
static void accum_sum_rescale(NumericSumAccum *accum, const NumericVar *val)
Definition: numeric.c:10974
#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:10901
signed int int32
Definition: c.h:417
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 10901 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().

10902 {
10903  int i;
10904  int ndigits;
10905  int32 *dig;
10906  int32 carry;
10907  int32 newdig = 0;
10908 
10909  /*
10910  * If no new values have been added since last carry propagation, nothing
10911  * to do.
10912  */
10913  if (accum->num_uncarried == 0)
10914  return;
10915 
10916  /*
10917  * We maintain that the weight of the accumulator is always one larger
10918  * than needed to hold the current value, before carrying, to make sure
10919  * there is enough space for the possible extra digit when carry is
10920  * propagated. We cannot expand the buffer here, unless we require
10921  * callers of accum_sum_final() to switch to the right memory context.
10922  */
10923  Assert(accum->pos_digits[0] == 0 && accum->neg_digits[0] == 0);
10924 
10925  ndigits = accum->ndigits;
10926 
10927  /* Propagate carry in the positive sum */
10928  dig = accum->pos_digits;
10929  carry = 0;
10930  for (i = ndigits - 1; i >= 0; i--)
10931  {
10932  newdig = dig[i] + carry;
10933  if (newdig >= NBASE)
10934  {
10935  carry = newdig / NBASE;
10936  newdig -= carry * NBASE;
10937  }
10938  else
10939  carry = 0;
10940  dig[i] = newdig;
10941  }
10942  /* Did we use up the digit reserved for carry propagation? */
10943  if (newdig > 0)
10944  accum->have_carry_space = false;
10945 
10946  /* And the same for the negative sum */
10947  dig = accum->neg_digits;
10948  carry = 0;
10949  for (i = ndigits - 1; i >= 0; i--)
10950  {
10951  newdig = dig[i] + carry;
10952  if (newdig >= NBASE)
10953  {
10954  carry = newdig / NBASE;
10955  newdig -= carry * NBASE;
10956  }
10957  else
10958  carry = 0;
10959  dig[i] = newdig;
10960  }
10961  if (newdig > 0)
10962  accum->have_carry_space = false;
10963 
10964  accum->num_uncarried = 0;
10965 }
int32 * neg_digits
Definition: numeric.c:379
int num_uncarried
Definition: numeric.c:376
signed int int32
Definition: c.h:417
bool have_carry_space
Definition: numeric.c:377
#define NBASE
Definition: numeric.c:97
#define Assert(condition)
Definition: c.h:800
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 11131 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().

11132 {
11133  NumericVar tmp_var;
11134 
11135  init_var(&tmp_var);
11136 
11137  accum_sum_final(accum2, &tmp_var);
11138  accum_sum_add(accum, &tmp_var);
11139 
11140  free_var(&tmp_var);
11141 }
static void accum_sum_final(NumericSumAccum *accum, NumericVar *result)
Definition: numeric.c:11063
static void accum_sum_add(NumericSumAccum *accum, const NumericVar *var1)
Definition: numeric.c:10853
static void free_var(NumericVar *var)
Definition: numeric.c:6720
#define init_var(v)
Definition: numeric.c:495

◆ accum_sum_copy()

static void accum_sum_copy ( NumericSumAccum dst,
NumericSumAccum src 
)
static

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

11115 {
11116  dst->pos_digits = palloc(src->ndigits * sizeof(int32));
11117  dst->neg_digits = palloc(src->ndigits * sizeof(int32));
11118 
11119  memcpy(dst->pos_digits, src->pos_digits, src->ndigits * sizeof(int32));
11120  memcpy(dst->neg_digits, src->neg_digits, src->ndigits * sizeof(int32));
11121  dst->num_uncarried = src->num_uncarried;
11122  dst->ndigits = src->ndigits;
11123  dst->weight = src->weight;
11124  dst->dscale = src->dscale;
11125 }
int32 * neg_digits
Definition: numeric.c:379
int num_uncarried
Definition: numeric.c:376
signed int int32
Definition: c.h:417
void * palloc(Size size)
Definition: mcxt.c:950
int32 * pos_digits
Definition: numeric.c:378

◆ accum_sum_final()

static void accum_sum_final ( NumericSumAccum accum,
NumericVar result 
)
static

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

11064 {
11065  int i;
11066  NumericVar pos_var;
11067  NumericVar neg_var;
11068 
11069  if (accum->ndigits == 0)
11070  {
11071  set_var_from_var(&const_zero, result);
11072  return;
11073  }
11074 
11075  /* Perform final carry */
11076  accum_sum_carry(accum);
11077 
11078  /* Create NumericVars representing the positive and negative sums */
11079  init_var(&pos_var);
11080  init_var(&neg_var);
11081 
11082  pos_var.ndigits = neg_var.ndigits = accum->ndigits;
11083  pos_var.weight = neg_var.weight = accum->weight;
11084  pos_var.dscale = neg_var.dscale = accum->dscale;
11085  pos_var.sign = NUMERIC_POS;
11086  neg_var.sign = NUMERIC_NEG;
11087 
11088  pos_var.buf = pos_var.digits = digitbuf_alloc(accum->ndigits);
11089  neg_var.buf = neg_var.digits = digitbuf_alloc(accum->ndigits);
11090 
11091  for (i = 0; i < accum->ndigits; i++)
11092  {
11093  Assert(accum->pos_digits[i] < NBASE);
11094  pos_var.digits[i] = (int16) accum->pos_digits[i];
11095 
11096  Assert(accum->neg_digits[i] < NBASE);
11097  neg_var.digits[i] = (int16) accum->neg_digits[i];
11098  }
11099 
11100  /* And add them together */
11101  add_var(&pos_var, &neg_var, result);
11102 
11103  /* Remove leading/trailing zeroes */
11104  strip_var(result);
11105 }
signed short int16
Definition: c.h:416
int weight
Definition: numeric.c:307
#define NUMERIC_POS
Definition: numeric.c:167
static void strip_var(NumericVar *var)
Definition: numeric.c:10796
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:10901
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:800
NumericDigit * digits
Definition: numeric.c:311
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7892
int i
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:6970
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 10974 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().

10975 {
10976  int old_weight = accum->weight;
10977  int old_ndigits = accum->ndigits;
10978  int accum_ndigits;
10979  int accum_weight;
10980  int accum_rscale;
10981  int val_rscale;
10982 
10983  accum_weight = old_weight;
10984  accum_ndigits = old_ndigits;
10985 
10986  /*
10987  * Does the new value have a larger weight? If so, enlarge the buffers,
10988  * and shift the existing value to the new weight, by adding leading
10989  * zeros.
10990  *
10991  * We enforce that the accumulator always has a weight one larger than
10992  * needed for the inputs, so that we have space for an extra digit at the
10993  * final carry-propagation phase, if necessary.
10994  */
10995  if (val->weight >= accum_weight)
10996  {
10997  accum_weight = val->weight + 1;
10998  accum_ndigits = accum_ndigits + (accum_weight - old_weight);
10999  }
11000 
11001  /*
11002  * Even though the new value is small, we might've used up the space
11003  * reserved for the carry digit in the last call to accum_sum_carry(). If
11004  * so, enlarge to make room for another one.
11005  */
11006  else if (!accum->have_carry_space)
11007  {
11008  accum_weight++;
11009  accum_ndigits++;
11010  }
11011 
11012  /* Is the new value wider on the right side? */
11013  accum_rscale = accum_ndigits - accum_weight - 1;
11014  val_rscale = val->ndigits - val->weight - 1;
11015  if (val_rscale > accum_rscale)
11016  accum_ndigits = accum_ndigits + (val_rscale - accum_rscale);
11017 
11018  if (accum_ndigits != old_ndigits ||
11019  accum_weight != old_weight)
11020  {
11021  int32 *new_pos_digits;
11022  int32 *new_neg_digits;
11023  int weightdiff;
11024 
11025  weightdiff = accum_weight - old_weight;
11026 
11027  new_pos_digits = palloc0(accum_ndigits * sizeof(int32));
11028  new_neg_digits = palloc0(accum_ndigits * sizeof(int32));
11029 
11030  if (accum->pos_digits)
11031  {
11032  memcpy(&new_pos_digits[weightdiff], accum->pos_digits,
11033  old_ndigits * sizeof(int32));
11034  pfree(accum->pos_digits);
11035 
11036  memcpy(&new_neg_digits[weightdiff], accum->neg_digits,
11037  old_ndigits * sizeof(int32));
11038  pfree(accum->neg_digits);
11039  }
11040 
11041  accum->pos_digits = new_pos_digits;
11042  accum->neg_digits = new_neg_digits;
11043 
11044  accum->weight = accum_weight;
11045  accum->ndigits = accum_ndigits;
11046 
11047  Assert(accum->pos_digits[0] == 0 && accum->neg_digits[0] == 0);
11048  accum->have_carry_space = true;
11049  }
11050 
11051  if (val->dscale > accum->dscale)
11052  accum->dscale = val->dscale;
11053 }
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:417
void pfree(void *pointer)
Definition: mcxt.c:1057
bool have_carry_space
Definition: numeric.c:377
void * palloc0(Size size)
Definition: mcxt.c:981
#define Assert(condition)
Definition: c.h:800
int32 * pos_digits
Definition: numeric.c:378

◆ accum_sum_reset()

static void accum_sum_reset ( NumericSumAccum accum)
static

Definition at line 10837 of file numeric.c.

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

Referenced by do_numeric_discard().

10838 {
10839  int i;
10840 
10841  accum->dscale = 0;
10842  for (i = 0; i < accum->ndigits; i++)
10843  {
10844  accum->pos_digits[i] = 0;
10845  accum->neg_digits[i] = 0;
10846  }
10847 }
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 10461 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().

10462 {
10463  NumericDigit *res_buf;
10464  NumericDigit *res_digits;
10465  int res_ndigits;
10466  int res_weight;
10467  int res_rscale,
10468  rscale1,
10469  rscale2;
10470  int res_dscale;
10471  int i,
10472  i1,
10473  i2;
10474  int carry = 0;
10475 
10476  /* copy these values into local vars for speed in inner loop */
10477  int var1ndigits = var1->ndigits;
10478  int var2ndigits = var2->ndigits;
10479  NumericDigit *var1digits = var1->digits;
10480  NumericDigit *var2digits = var2->digits;
10481 
10482  res_weight = Max(var1->weight, var2->weight) + 1;
10483 
10484  res_dscale = Max(var1->dscale, var2->dscale);
10485 
10486  /* Note: here we are figuring rscale in base-NBASE digits */
10487  rscale1 = var1->ndigits - var1->weight - 1;
10488  rscale2 = var2->ndigits - var2->weight - 1;
10489  res_rscale = Max(rscale1, rscale2);
10490 
10491  res_ndigits = res_rscale + res_weight + 1;
10492  if (res_ndigits <= 0)
10493  res_ndigits = 1;
10494 
10495  res_buf = digitbuf_alloc(res_ndigits + 1);
10496  res_buf[0] = 0; /* spare digit for later rounding */
10497  res_digits = res_buf + 1;
10498 
10499  i1 = res_rscale + var1->weight + 1;
10500  i2 = res_rscale + var2->weight + 1;
10501  for (i = res_ndigits - 1; i >= 0; i--)
10502  {
10503  i1--;
10504  i2--;
10505  if (i1 >= 0 && i1 < var1ndigits)
10506  carry += var1digits[i1];
10507  if (i2 >= 0 && i2 < var2ndigits)
10508  carry += var2digits[i2];
10509 
10510  if (carry >= NBASE)
10511  {
10512  res_digits[i] = carry - NBASE;
10513  carry = 1;
10514  }
10515  else
10516  {
10517  res_digits[i] = carry;
10518  carry = 0;
10519  }
10520  }
10521 
10522  Assert(carry == 0); /* else we failed to allow for carry out */
10523 
10524  digitbuf_free(result->buf);
10525  result->ndigits = res_ndigits;
10526  result->buf = res_buf;
10527  result->digits = res_digits;
10528  result->weight = res_weight;
10529  result->dscale = res_dscale;
10530 
10531  /* Remove leading/trailing zeroes */
10532  strip_var(result);
10533 }
int weight
Definition: numeric.c:307
static void strip_var(NumericVar *var)
Definition: numeric.c:10796
#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:800
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 7892 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().

7893 {
7894  /*
7895  * Decide on the signs of the two variables what to do
7896  */
7897  if (var1->sign == NUMERIC_POS)
7898  {
7899  if (var2->sign == NUMERIC_POS)
7900  {
7901  /*
7902  * Both are positive result = +(ABS(var1) + ABS(var2))
7903  */
7904  add_abs(var1, var2, result);
7905  result->sign = NUMERIC_POS;
7906  }
7907  else
7908  {
7909  /*
7910  * var1 is positive, var2 is negative Must compare absolute values
7911  */
7912  switch (cmp_abs(var1, var2))
7913  {
7914  case 0:
7915  /* ----------
7916  * ABS(var1) == ABS(var2)
7917  * result = ZERO
7918  * ----------
7919  */
7920  zero_var(result);
7921  result->dscale = Max(var1->dscale, var2->dscale);
7922  break;
7923 
7924  case 1:
7925  /* ----------
7926  * ABS(var1) > ABS(var2)
7927  * result = +(ABS(var1) - ABS(var2))
7928  * ----------
7929  */
7930  sub_abs(var1, var2, result);
7931  result->sign = NUMERIC_POS;
7932  break;
7933 
7934  case -1:
7935  /* ----------
7936  * ABS(var1) < ABS(var2)
7937  * result = -(ABS(var2) - ABS(var1))
7938  * ----------
7939  */
7940  sub_abs(var2, var1, result);
7941  result->sign = NUMERIC_NEG;
7942  break;
7943  }
7944  }
7945  }
7946  else
7947  {
7948  if (var2->sign == NUMERIC_POS)
7949  {
7950  /* ----------
7951  * var1 is negative, var2 is positive
7952  * Must compare absolute values
7953  * ----------
7954  */
7955  switch (cmp_abs(var1, var2))
7956  {
7957  case 0:
7958  /* ----------
7959  * ABS(var1) == ABS(var2)
7960  * result = ZERO
7961  * ----------
7962  */
7963  zero_var(result);
7964  result->dscale = Max(var1->dscale, var2->dscale);
7965  break;
7966 
7967  case 1:
7968  /* ----------
7969  * ABS(var1) > ABS(var2)
7970  * result = -(ABS(var1) - ABS(var2))
7971  * ----------
7972  */
7973  sub_abs(var1, var2, result);
7974  result->sign = NUMERIC_NEG;
7975  break;
7976 
7977  case -1:
7978  /* ----------
7979  * ABS(var1) < ABS(var2)
7980  * result = +(ABS(var2) - ABS(var1))
7981  * ----------
7982  */
7983  sub_abs(var2, var1, result);
7984  result->sign = NUMERIC_POS;
7985  break;
7986  }
7987  }
7988  else
7989  {
7990  /* ----------
7991  * Both are negative
7992  * result = -(ABS(var1) + ABS(var2))
7993  * ----------
7994  */
7995  add_abs(var1, var2, result);
7996  result->sign = NUMERIC_NEG;
7997  }
7998  }
7999 }
static void sub_abs(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:10546
static void add_abs(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:10461
#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:6736
static int cmp_abs(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:10383
#define Max(x, y)
Definition: numeric.c:13

◆ alloc_var()

static void alloc_var ( NumericVar var,
int  ndigits 
)
static

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

6705 {
6706  digitbuf_free(var->buf);
6707  var->buf = digitbuf_alloc(ndigits + 1);
6708  var->buf[0] = 0; /* spare digit for rounding */
6709  var->digits = var->buf + 1;
6710  var->ndigits = ndigits;
6711 }
#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 7375 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().

7376 {
7377  int precision;
7378  int scale;
7379  int maxdigits;
7380  int ddigits;
7381  int i;
7382 
7383  /* Do nothing if we have a default typmod (-1) */
7384  if (typmod < (int32) (VARHDRSZ))
7385  return;
7386 
7387  typmod -= VARHDRSZ;
7388  precision = (typmod >> 16) & 0xffff;
7389  scale = typmod & 0xffff;
7390  maxdigits = precision - scale;
7391 
7392  /* Round to target scale (and set var->dscale) */
7393  round_var(var, scale);
7394 
7395  /*
7396  * Check for overflow - note we can't do this before rounding, because
7397  * rounding could raise the weight. Also note that the var's weight could
7398  * be inflated by leading zeroes, which will be stripped before storage
7399  * but perhaps might not have been yet. In any case, we must recognize a
7400  * true zero, whose weight doesn't mean anything.
7401  */
7402  ddigits = (var->weight + 1) * DEC_DIGITS;
7403  if (ddigits > maxdigits)
7404  {
7405  /* Determine true weight; and check for all-zero result */
7406  for (i = 0; i < var->ndigits; i++)
7407  {
7408  NumericDigit dig = var->digits[i];
7409 
7410  if (dig)
7411  {
7412  /* Adjust for any high-order decimal zero digits */
7413 #if DEC_DIGITS == 4
7414  if (dig < 10)
7415  ddigits -= 3;
7416  else if (dig < 100)
7417  ddigits -= 2;
7418  else if (dig < 1000)
7419  ddigits -= 1;
7420 #elif DEC_DIGITS == 2
7421  if (dig < 10)
7422  ddigits -= 1;
7423 #elif DEC_DIGITS == 1
7424  /* no adjustment */
7425 #else
7426 #error unsupported NBASE
7427 #endif
7428  if (ddigits > maxdigits)
7429  ereport(ERROR,
7430  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
7431  errmsg("numeric field overflow"),
7432  errdetail("A field with precision %d, scale %d must round to an absolute value less than %s%d.",
7433  precision, scale,
7434  /* Display 10^0 as 1 */
7435  maxdigits ? "10^" : "",
7436  maxdigits ? maxdigits : 1
7437  )));
7438  break;
7439  }
7440  ddigits -= DEC_DIGITS;
7441  }
7442  }
7443 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:10628
int weight
Definition: numeric.c:307
#define VARHDRSZ
Definition: c.h:623
int errcode(int sqlerrcode)
Definition: elog.c:691
int scale
Definition: pgbench.c:154
int ndigits
Definition: numeric.c:306
signed int int32
Definition: c.h:417
#define ERROR
Definition: elog.h:43
int16 NumericDigit
Definition: numeric.c:103
int errdetail(const char *fmt,...)
Definition: elog.c:1035
#define ereport(elevel,...)
Definition: elog.h:155
int maxdigits
Definition: informix.c:665
NumericDigit * digits
Definition: numeric.c:311
int errmsg(const char *fmt,...)
Definition: elog.c:902
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 7452 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().

7453 {
7454  int precision;
7455  int scale;
7456 
7457  Assert(NUMERIC_IS_SPECIAL(num)); /* caller error if not */
7458 
7459  /*
7460  * NaN is allowed regardless of the typmod; that's rather dubious perhaps,
7461  * but it's a longstanding behavior. Inf is rejected if we have any
7462  * typmod restriction, since an infinity shouldn't be claimed to fit in
7463  * any finite number of digits.
7464  */
7465  if (NUMERIC_IS_NAN(num))
7466  return;
7467 
7468  /* Do nothing if we have a default typmod (-1) */
7469  if (typmod < (int32) (VARHDRSZ))
7470  return;
7471 
7472  typmod -= VARHDRSZ;
7473  precision = (typmod >> 16) & 0xffff;
7474  scale = typmod & 0xffff;
7475 
7476  ereport(ERROR,
7477  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
7478  errmsg("numeric field overflow"),
7479  errdetail("A field with precision %d, scale %d cannot hold an infinite value.",
7480  precision, scale)));
7481 }
#define VARHDRSZ
Definition: c.h:623
int errcode(int sqlerrcode)
Definition: elog.c:691
int scale
Definition: pgbench.c:154
signed int int32
Definition: c.h:417
#define NUMERIC_IS_SPECIAL(n)
Definition: numeric.c:174
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:1035
#define ereport(elevel,...)
Definition: elog.h:155
#define Assert(condition)
Definition: c.h:800
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:205

◆ ceil_var()

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

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

9098 {
9099  NumericVar tmp;
9100 
9101  init_var(&tmp);
9102  set_var_from_var(var, &tmp);
9103 
9104  trunc_var(&tmp, 0);
9105 
9106  if (var->sign == NUMERIC_POS && cmp_var(var, &tmp) != 0)
9107  add_var(&tmp, &const_one, &tmp);
9108 
9109  set_var_from_var(&tmp, result);
9110  free_var(&tmp);
9111 }
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:10734
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:7834
static void free_var(NumericVar *var)
Definition: numeric.c:6720
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7892
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:6970
#define init_var(v)
Definition: numeric.c:495

◆ cmp_abs()

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

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

10384 {
10385  return cmp_abs_common(var1->digits, var1->ndigits, var1->weight,
10386  var2->digits, var2->ndigits, var2->weight);
10387 }
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:10397

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

Referenced by cmp_abs(), and cmp_var_common().

10399 {
10400  int i1 = 0;
10401  int i2 = 0;
10402 
10403  /* Check any digits before the first common digit */
10404 
10405  while (var1weight > var2weight && i1 < var1ndigits)
10406  {
10407  if (var1digits[i1++] != 0)
10408  return 1;
10409  var1weight--;
10410  }
10411  while (var2weight > var1weight && i2 < var2ndigits)
10412  {
10413  if (var2digits[i2++] != 0)
10414  return -1;
10415  var2weight--;
10416  }
10417 
10418  /* At this point, either w1 == w2 or we've run out of digits */
10419 
10420  if (var1weight == var2weight)
10421  {
10422  while (i1 < var1ndigits && i2 < var2ndigits)
10423  {
10424  int stat = var1digits[i1++] - var2digits[i2++];
10425 
10426  if (stat)
10427  {
10428  if (stat > 0)
10429  return 1;
10430  return -1;
10431  }
10432  }
10433  }
10434 
10435  /*
10436  * At this point, we've run out of digits on one side or the other; so any
10437  * remaining nonzero digits imply that side is larger
10438  */
10439  while (i1 < var1ndigits)
10440  {
10441  if (var1digits[i1++] != 0)
10442  return 1;
10443  }
10444  while (i2 < var2ndigits)
10445  {
10446  if (var2digits[i2++] != 0)
10447  return -1;
10448  }
10449 
10450  return 0;
10451 }

◆ 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:7849
#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 7834 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().

7835 {
7836  return cmp_var_common(var1->digits, var1->ndigits,
7837  var1->weight, var1->sign,
7838  var2->digits, var2->ndigits,
7839  var2->weight, var2->sign);
7840 }
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:7849
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 7849 of file numeric.c.

References cmp_abs_common(), NUMERIC_NEG, and NUMERIC_POS.

Referenced by cmp_numerics(), and cmp_var().

7853 {
7854  if (var1ndigits == 0)
7855  {
7856  if (var2ndigits == 0)
7857  return 0;
7858  if (var2sign == NUMERIC_NEG)
7859  return 1;
7860  return -1;
7861  }
7862  if (var2ndigits == 0)
7863  {
7864  if (var1sign == NUMERIC_POS)
7865  return 1;
7866  return -1;
7867  }
7868 
7869  if (var1sign == NUMERIC_POS)
7870  {
7871  if (var2sign == NUMERIC_NEG)
7872  return 1;
7873  return cmp_abs_common(var1digits, var1ndigits, var1weight,
7874  var2digits, var2ndigits, var2weight);
7875  }
7876 
7877  if (var2sign == NUMERIC_POS)
7878  return -1;
7879 
7880  return cmp_abs_common(var2digits, var2ndigits, var2weight,
7881  var1digits, var1ndigits, var1weight);
7882 }
#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:10397

◆ 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:8338
int dscale
Definition: numeric.c:309
static int select_div_scale(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:8929
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:6953
static void free_var(NumericVar *var)
Definition: numeric.c:6720
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:8130
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7892
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8009
static void floor_var(const NumericVar *var, NumericVar *result)
Definition: numeric.c:9121

◆ div_mod_var()

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

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

9029 {
9030  NumericVar q;
9031  NumericVar r;
9032 
9033  init_var(&q);
9034  init_var(&r);
9035 
9036  /*
9037  * Use div_var_fast() to get an initial estimate for the integer quotient.
9038  * This might be inaccurate (per the warning in div_var_fast's comments),
9039  * but we can correct it below.
9040  */
9041  div_var_fast(var1, var2, &q, 0, false);
9042 
9043  /* Compute initial estimate of remainder using the quotient estimate. */
9044  mul_var(var2, &q, &r, var2->dscale);
9045  sub_var(var1, &r, &r);
9046 
9047  /*
9048  * Adjust the results if necessary --- the remainder should have the same
9049  * sign as var1, and its absolute value should be less than the absolute
9050  * value of var2.
9051  */
9052  while (r.ndigits != 0 && r.sign != var1->sign)
9053  {
9054  /* The absolute value of the quotient is too large */
9055  if (var1->sign == var2->sign)
9056  {
9057  sub_var(&q, &const_one, &q);
9058  add_var(&r, var2, &r);
9059  }
9060  else
9061  {
9062  add_var(&q, &const_one, &q);
9063  sub_var(&r, var2, &r);
9064  }
9065  }
9066 
9067  while (cmp_abs(&r, var2) >= 0)
9068  {
9069  /* The absolute value of the quotient is too small */
9070  if (var1->sign == var2->sign)
9071  {
9072  add_var(&q, &const_one, &q);
9073  sub_var(&r, var2, &r);
9074  }
9075  else
9076  {
9077  sub_var(&q, &const_one, &q);
9078  add_var(&r, var2, &r);
9079  }
9080  }
9081 
9082  set_var_from_var(&q, quot);
9083  set_var_from_var(&r, rem);
9084 
9085  free_var(&q);
9086  free_var(&r);
9087 }
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:8623
static void free_var(NumericVar *var)
Definition: numeric.c:6720
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:8130
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7892
static int cmp_abs(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:10383
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8009
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:6970
#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 8338 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().

8340 {
8341  int div_ndigits;
8342  int res_ndigits;
8343  int res_sign;
8344  int res_weight;
8345  int carry;
8346  int borrow;
8347  int divisor1;
8348  int divisor2;
8349  NumericDigit *dividend;
8350  NumericDigit *divisor;
8351  NumericDigit *res_digits;
8352  int i;
8353  int j;
8354 
8355  /* copy these values into local vars for speed in inner loop */
8356  int var1ndigits = var1->ndigits;
8357  int var2ndigits = var2->ndigits;
8358 
8359  /*
8360  * First of all division by zero check; we must not be handed an
8361  * unnormalized divisor.
8362  */
8363  if (var2ndigits == 0 || var2->digits[0] == 0)
8364  ereport(ERROR,
8365  (errcode(ERRCODE_DIVISION_BY_ZERO),
8366  errmsg("division by zero")));
8367 
8368  /*
8369  * Now result zero check
8370  */
8371  if (var1ndigits == 0)
8372  {
8373  zero_var(result);
8374  result->dscale = rscale;
8375  return;
8376  }
8377 
8378  /*
8379  * Determine the result sign, weight and number of digits to calculate.
8380  * The weight figured here is correct if the emitted quotient has no
8381  * leading zero digits; otherwise strip_var() will fix things up.
8382  */
8383  if (var1->sign == var2->sign)
8384  res_sign = NUMERIC_POS;
8385  else
8386  res_sign = NUMERIC_NEG;
8387  res_weight = var1->weight - var2->weight;
8388  /* The number of accurate result digits we need to produce: */
8389  res_ndigits = res_weight + 1 + (rscale + DEC_DIGITS - 1) / DEC_DIGITS;
8390  /* ... but always at least 1 */
8391  res_ndigits = Max(res_ndigits, 1);
8392  /* If rounding needed, figure one more digit to ensure correct result */
8393  if (round)
8394  res_ndigits++;
8395 
8396  /*
8397  * The working dividend normally requires res_ndigits + var2ndigits
8398  * digits, but make it at least var1ndigits so we can load all of var1
8399  * into it. (There will be an additional digit dividend[0] in the
8400  * dividend space, but for consistency with Knuth's notation we don't
8401  * count that in div_ndigits.)
8402  */
8403  div_ndigits = res_ndigits + var2ndigits;
8404  div_ndigits = Max(div_ndigits, var1ndigits);
8405 
8406  /*
8407  * We need a workspace with room for the working dividend (div_ndigits+1
8408  * digits) plus room for the possibly-normalized divisor (var2ndigits
8409  * digits). It is convenient also to have a zero at divisor[0] with the
8410  * actual divisor data in divisor[1 .. var2ndigits]. Transferring the
8411  * digits into the workspace also allows us to realloc the result (which
8412  * might be the same as either input var) before we begin the main loop.
8413  * Note that we use palloc0 to ensure that divisor[0], dividend[0], and
8414  * any additional dividend positions beyond var1ndigits, start out 0.
8415  */
8416  dividend = (NumericDigit *)
8417  palloc0((div_ndigits + var2ndigits + 2) * sizeof(NumericDigit));
8418  divisor = dividend + (div_ndigits + 1);
8419  memcpy(dividend + 1, var1->digits, var1ndigits * sizeof(NumericDigit));
8420  memcpy(divisor + 1, var2->digits, var2ndigits * sizeof(NumericDigit));
8421 
8422  /*
8423  * Now we can realloc the result to hold the generated quotient digits.
8424  */
8425  alloc_var(result, res_ndigits);
8426  res_digits = result->digits;
8427 
8428  if (var2ndigits == 1)
8429  {
8430  /*
8431  * If there's only a single divisor digit, we can use a fast path (cf.
8432  * Knuth section 4.3.1 exercise 16).
8433  */
8434  divisor1 = divisor[1];
8435  carry = 0;
8436  for (i = 0; i < res_ndigits; i++)
8437  {
8438  carry = carry * NBASE + dividend[i + 1];
8439  res_digits[i] = carry / divisor1;
8440  carry = carry % divisor1;
8441  }
8442  }
8443  else
8444  {
8445  /*
8446  * The full multiple-place algorithm is taken from Knuth volume 2,
8447  * Algorithm 4.3.1D.
8448  *
8449  * We need the first divisor digit to be >= NBASE/2. If it isn't,
8450  * make it so by scaling up both the divisor and dividend by the
8451  * factor "d". (The reason for allocating dividend[0] above is to
8452  * leave room for possible carry here.)
8453  */
8454  if (divisor[1] < HALF_NBASE)
8455  {
8456  int d = NBASE / (divisor[1] + 1);
8457 
8458  carry = 0;
8459  for (i = var2ndigits; i > 0; i--)
8460  {
8461  carry += divisor[i] * d;
8462  divisor[i] = carry % NBASE;
8463  carry = carry / NBASE;
8464  }
8465  Assert(carry == 0);
8466  carry = 0;
8467  /* at this point only var1ndigits of dividend can be nonzero */
8468  for (i = var1ndigits; i >= 0; i--)
8469  {
8470  carry += dividend[i] * d;
8471  dividend[i] = carry % NBASE;
8472  carry = carry / NBASE;
8473  }
8474  Assert(carry == 0);
8475  Assert(divisor[1] >= HALF_NBASE);
8476  }
8477  /* First 2 divisor digits are used repeatedly in main loop */
8478  divisor1 = divisor[1];
8479  divisor2 = divisor[2];
8480 
8481  /*
8482  * Begin the main loop. Each iteration of this loop produces the j'th
8483  * quotient digit by dividing dividend[j .. j + var2ndigits] by the
8484  * divisor; this is essentially the same as the common manual
8485  * procedure for long division.
8486  */
8487  for (j = 0; j < res_ndigits; j++)
8488  {
8489  /* Estimate quotient digit from the first two dividend digits */
8490  int next2digits = dividend[j] * NBASE + dividend[j + 1];
8491  int qhat;
8492 
8493  /*
8494  * If next2digits are 0, then quotient digit must be 0 and there's
8495  * no need to adjust the working dividend. It's worth testing
8496  * here to fall out ASAP when processing trailing zeroes in a
8497  * dividend.
8498  */
8499  if (next2digits == 0)
8500  {
8501  res_digits[j] = 0;
8502  continue;
8503  }
8504 
8505  if (dividend[j] == divisor1)
8506  qhat = NBASE - 1;
8507  else
8508  qhat = next2digits / divisor1;
8509 
8510  /*
8511  * Adjust quotient digit if it's too large. Knuth proves that
8512  * after this step, the quotient digit will be either correct or
8513  * just one too large. (Note: it's OK to use dividend[j+2] here
8514  * because we know the divisor length is at least 2.)
8515  */
8516  while (divisor2 * qhat >
8517  (next2digits - qhat * divisor1) * NBASE + dividend[j + 2])
8518  qhat--;
8519 
8520  /* As above, need do nothing more when quotient digit is 0 */
8521  if (qhat > 0)
8522  {
8523  /*
8524  * Multiply the divisor by qhat, and subtract that from the
8525  * working dividend. "carry" tracks the multiplication,
8526  * "borrow" the subtraction (could we fold these together?)
8527  */
8528  carry = 0;
8529  borrow = 0;
8530  for (i = var2ndigits; i >= 0; i--)
8531  {
8532  carry += divisor[i] * qhat;
8533  borrow -= carry % NBASE;
8534  carry = carry / NBASE;
8535  borrow += dividend[j + i];
8536  if (borrow < 0)
8537  {
8538  dividend[j + i] = borrow + NBASE;
8539  borrow = -1;
8540  }
8541  else
8542  {
8543  dividend[j + i] = borrow;
8544  borrow = 0;
8545  }
8546  }
8547  Assert(carry == 0);
8548 
8549  /*
8550  * If we got a borrow out of the top dividend digit, then
8551  * indeed qhat was one too large. Fix it, and add back the
8552  * divisor to correct the working dividend. (Knuth proves
8553  * that this will occur only about 3/NBASE of the time; hence,
8554  * it's a good idea to test this code with small NBASE to be
8555  * sure this section gets exercised.)
8556  */
8557  if (borrow)
8558  {
8559  qhat--;
8560  carry = 0;
8561  for (i = var2ndigits; i >= 0; i--)
8562  {
8563  carry += dividend[j + i] + divisor[i];
8564  if (carry >= NBASE)
8565  {
8566  dividend[j + i] = carry - NBASE;
8567  carry = 1;
8568  }
8569  else
8570  {
8571  dividend[j + i] = carry;
8572  carry = 0;
8573  }
8574  }
8575  /* A carry should occur here to cancel the borrow above */
8576  Assert(carry == 1);
8577  }
8578  }
8579 
8580  /* And we're done with this quotient digit */
8581  res_digits[j] = qhat;
8582  }
8583  }
8584 
8585  pfree(dividend);
8586 
8587  /*
8588  * Finally, round or truncate the result to the requested precision.
8589  */
8590  result->weight = res_weight;
8591  result->sign = res_sign;
8592 
8593  /* Round or truncate to target rscale (and set result->dscale) */
8594  if (round)
8595  round_var(result, rscale);
8596  else
8597  trunc_var(result, rscale);
8598 
8599  /* Strip leading and trailing zeroes */
8600  strip_var(result);
8601 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:10628
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:10734
int weight
Definition: numeric.c:307
#define NUMERIC_POS
Definition: numeric.c:167
static void strip_var(NumericVar *var)
Definition: numeric.c:10796
int errcode(int sqlerrcode)
Definition: elog.c:691
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:6736
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
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:981
#define ereport(elevel,...)
Definition: elog.h:155
#define Assert(condition)
Definition: c.h:800
NumericDigit * digits
Definition: numeric.c:311
int errmsg(const char *fmt,...)
Definition: elog.c:902
static void alloc_var(NumericVar *var, int ndigits)
Definition: numeric.c:6704
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 8623 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().

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

4535 {
4536  NumericVar X;
4537  NumericVar X2;
4538  MemoryContext old_context;
4539 
4540  /* Count NaN/infinity inputs separately from all else */
4541  if (NUMERIC_IS_SPECIAL(newval))
4542  {
4543  if (NUMERIC_IS_PINF(newval))
4544  state->pInfcount++;
4545  else if (NUMERIC_IS_NINF(newval))
4546  state->nInfcount++;
4547  else
4548  state->NaNcount++;
4549  return;
4550  }
4551 
4552  /* load processed number in short-lived context */
4553  init_var_from_num(newval, &X);
4554 
4555  /*
4556  * Track the highest input dscale that we've seen, to support inverse
4557  * transitions (see do_numeric_discard).
4558  */
4559  if (X.dscale > state->maxScale)
4560  {
4561  state->maxScale = X.dscale;
4562  state->maxScaleCount = 1;
4563  }
4564  else if (X.dscale == state->maxScale)
4565  state->maxScaleCount++;
4566 
4567  /* if we need X^2, calculate that in short-lived context */
4568  if (state->calcSumX2)
4569  {
4570  init_var(&X2);
4571  mul_var(&X, &X, &X2, X.dscale * 2);
4572  }
4573 
4574  /* The rest of this needs to work in the aggregate context */
4575  old_context = MemoryContextSwitchTo(state->agg_context);
4576 
4577  state->N++;
4578 
4579  /* Accumulate sums */
4580  accum_sum_add(&(state->sumX), &X);
4581 
4582  if (state->calcSumX2)
4583  accum_sum_add(&(state->sumX2), &X2);
4584 
4585  MemoryContextSwitchTo(old_context);
4586 }
int64 nInfcount
Definition: numeric.c:4483
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext agg_context
Definition: numeric.c:4474
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:10853
#define NUMERIC_IS_PINF(n)
Definition: numeric.c:206
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:6953
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:8130
#define NUMERIC_IS_NINF(n)
Definition: numeric.c:207
NumericSumAccum sumX2
Definition: numeric.c:4477
int64 pInfcount
Definition: numeric.c:4482
int64 NaNcount
Definition: numeric.c:4481
int64 maxScaleCount
Definition: numeric.c:4479
NumericSumAccum sumX
Definition: numeric.c:4476
#define init_var(v)
Definition: numeric.c:495

◆ do_numeric_discard()

static bool do_numeric_discard ( NumericAggState state,
Numeric  newval 
)
static

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

4605 {
4606  NumericVar X;
4607  NumericVar X2;
4608  MemoryContext old_context;
4609 
4610  /* Count NaN/infinity inputs separately from all else */
4611  if (NUMERIC_IS_SPECIAL(newval))
4612  {
4613  if (NUMERIC_IS_PINF(newval))
4614  state->pInfcount--;
4615  else if (NUMERIC_IS_NINF(newval))
4616  state->nInfcount--;
4617  else
4618  state->NaNcount--;
4619  return true;
4620  }
4621 
4622  /* load processed number in short-lived context */
4623  init_var_from_num(newval, &X);
4624 
4625  /*
4626  * state->sumX's dscale is the maximum dscale of any of the inputs.
4627  * Removing the last input with that dscale would require us to recompute
4628  * the maximum dscale of the *remaining* inputs, which we cannot do unless
4629  * no more non-NaN inputs remain at all. So we report a failure instead,
4630  * and force the aggregation to be redone from scratch.
4631  */
4632  if (X.dscale == state->maxScale)
4633  {
4634  if (state->maxScaleCount > 1 || state->maxScale == 0)
4635  {
4636  /*
4637  * Some remaining inputs have same dscale, or dscale hasn't gotten
4638  * above zero anyway
4639  */
4640  state->maxScaleCount--;
4641  }
4642  else if (state->N == 1)
4643  {
4644  /* No remaining non-NaN inputs at all, so reset maxScale */
4645  state->maxScale = 0;
4646  state->maxScaleCount = 0;
4647  }
4648  else
4649  {
4650  /* Correct new maxScale is uncertain, must fail */
4651  return false;
4652  }
4653  }
4654 
4655  /* if we need X^2, calculate that in short-lived context */
4656  if (state->calcSumX2)
4657  {
4658  init_var(&X2);
4659  mul_var(&X, &X, &X2, X.dscale * 2);
4660  }
4661 
4662  /* The rest of this needs to work in the aggregate context */
4663  old_context = MemoryContextSwitchTo(state->agg_context);
4664 
4665  if (state->N-- > 1)
4666  {
4667  /* Negate X, to subtract it from the sum */
4668  X.sign = (X.sign == NUMERIC_POS ? NUMERIC_NEG : NUMERIC_POS);
4669  accum_sum_add(&(state->sumX), &X);
4670 
4671  if (state->calcSumX2)
4672  {
4673  /* Negate X^2. X^2 is always positive */
4674  X2.sign = NUMERIC_NEG;
4675  accum_sum_add(&(state->sumX2), &X2);
4676  }
4677  }
4678  else
4679  {
4680  /* Zero the sums */
4681  Assert(state->N == 0);
4682 
4683  accum_sum_reset(&state->sumX);
4684  if (state->calcSumX2)
4685  accum_sum_reset(&state->sumX2);
4686  }
4687 
4688  MemoryContextSwitchTo(old_context);
4689 
4690  return true;
4691 }
int64 nInfcount
Definition: numeric.c:4483
#define NUMERIC_POS
Definition: numeric.c:167
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext agg_context
Definition: numeric.c:4474
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:10853
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:6953
#define Assert(condition)
Definition: c.h:800
static void mul_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result, int rscale)
Definition: numeric.c:8130
#define NUMERIC_IS_NINF(n)
Definition: numeric.c:207
NumericSumAccum sumX2
Definition: numeric.c:4477
static void accum_sum_reset(NumericSumAccum *accum)
Definition: numeric.c:10837
int64 pInfcount
Definition: numeric.c:4482
int64 NaNcount
Definition: numeric.c:4481
int64 maxScaleCount
Definition: numeric.c:4479
NumericSumAccum sumX
Definition: numeric.c:4476
#define init_var(v)
Definition: numeric.c:495

◆ duplicate_numeric()

static Numeric duplicate_numeric ( Numeric  num)
static

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

7235 {
7236  Numeric res;
7237 
7238  res = (Numeric) palloc(VARSIZE(num));
7239  memcpy(res, num, VARSIZE(num));
7240  return res;
7241 }
#define VARSIZE(PTR)
Definition: postgres.h:303
struct NumericData * Numeric
Definition: numeric.h:43
void * palloc(Size size)
Definition: mcxt.c:950

◆ estimate_ln_dweight()

static int estimate_ln_dweight ( const NumericVar var)
static

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

9821 {
9822  int ln_dweight;
9823 
9824  if (cmp_var(var, &const_zero_point_nine) >= 0 &&
9825  cmp_var(var, &const_one_point_one) <= 0)
9826  {
9827  /*
9828  * 0.9 <= var <= 1.1
9829  *
9830  * ln(var) has a negative weight (possibly very large). To get a
9831  * reasonably accurate result, estimate it using ln(1+x) ~= x.
9832  */
9833  NumericVar x;
9834 
9835  init_var(&x);
9836  sub_var(var, &const_one, &x);
9837 
9838  if (x.ndigits > 0)
9839  {
9840  /* Use weight of most significant decimal digit of x */
9841  ln_dweight = x.weight * DEC_DIGITS + (int) log10(x.digits[0]);
9842  }
9843  else
9844  {
9845  /* x = 0. Since ln(1) = 0 exactly, we don't need extra digits */
9846  ln_dweight = 0;
9847  }
9848 
9849  free_var(&x);
9850  }
9851  else
9852  {
9853  /*
9854  * Estimate the logarithm using the first couple of digits from the
9855  * input number. This will give an accurate result whenever the input
9856  * is not too close to 1.
9857  */
9858  if (var->ndigits > 0)
9859  {
9860  int digits;
9861  int dweight;
9862  double ln_var;
9863 
9864  digits = var->digits[0];
9865  dweight = var->weight * DEC_DIGITS;
9866 
9867  if (var->ndigits > 1)
9868  {
9869  digits = digits * NBASE + var->digits[1];
9870  dweight -= DEC_DIGITS;
9871  }
9872 
9873  /*----------
9874  * We have var ~= digits * 10^dweight
9875  * so ln(var) ~= ln(digits) + dweight * ln(10)
9876  *----------
9877  */
9878  ln_var = log((double) digits) + dweight * 2.302585092994046;
9879  ln_dweight = (int) log10(Abs(ln_var));
9880  }
9881  else
9882  {
9883  /* Caller should fail on ln(0), but for the moment return zero */
9884  ln_dweight = 0;
9885  }
9886  }
9887 
9888  return ln_dweight;
9889 }
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:9898
int ndigits
Definition: numeric.c:306
#define Abs(x)
Definition: c.h:988
#define NBASE
Definition: numeric.c:97
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:7834
static void free_var(NumericVar *var)
Definition: numeric.c:6720
NumericDigit * digits
Definition: numeric.c:311
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8009
#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 9694 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().

9695 {
9696  NumericVar x;
9697  NumericVar elem;
9698  NumericVar ni;
9699  double val;
9700  int dweight;
9701  int ndiv2;
9702  int sig_digits;
9703  int local_rscale;
9704 
9705  init_var(&x);
9706  init_var(&elem);
9707  init_var(&ni);
9708 
9709  set_var_from_var(arg, &x);
9710 
9711  /*
9712  * Estimate the dweight of the result using floating point arithmetic, so
9713  * that we can choose an appropriate local rscale for the calculation.
9714  */
9716 
9717  /* Guard against overflow */
9718  /* If you change this limit, see also power_var()'s limit */
9719  if (Abs(val) >= NUMERIC_MAX_RESULT_SCALE * 3)
9720  ereport(ERROR,
9721  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
9722  errmsg("value overflows numeric format")));
9723 
9724  /* decimal weight = log10(e^x) = x * log10(e) */
9725  dweight = (int) (val * 0.434294481903252);
9726 
9727  /*
9728  * Reduce x to the range -0.01 <= x <= 0.01 (approximately) by dividing by
9729  * 2^n, to improve the convergence rate of the Taylor series.
9730  */
9731  if (Abs(val) > 0.01)
9732  {
9733  NumericVar tmp;
9734 
9735  init_var(&tmp);
9736  set_var_from_var(&const_two, &tmp);
9737 
9738  ndiv2 = 1;
9739  val /= 2;
9740 
9741  while (Abs(val) > 0.01)
9742  {
9743  ndiv2++;
9744  val /= 2;
9745  add_var(&tmp, &tmp, &tmp);
9746  }
9747 
9748  local_rscale = x.dscale + ndiv2;
9749  div_var_fast(&x, &tmp, &x, local_rscale, true);
9750 
9751  free_var(&tmp);
9752  }
9753  else
9754  ndiv2 = 0;
9755 
9756  /*
9757  * Set the scale for the Taylor series expansion. The final result has
9758  * (dweight + rscale + 1) significant digits. In addition, we have to
9759  * raise the Taylor series result to the power 2^ndiv2, which introduces
9760  * an error of up to around log10(2^ndiv2) digits, so work with this many
9761  * extra digits of precision (plus a few more for good measure).
9762  */
9763  sig_digits = 1 + dweight + rscale + (int) (ndiv2 * 0.301029995663981);
9764  sig_digits = Max(sig_digits, 0) + 8;
9765 
9766  local_rscale = sig_digits - 1;
9767 
9768  /*
9769  * Use the Taylor series
9770  *
9771  * exp(x) = 1 + x + x^2/2! + x^3/3! + ...
9772  *
9773  * Given the limited range of x, this should converge reasonably quickly.
9774  * We run the series until the terms fall below the local_rscale limit.
9775  */
9776  add_var(&const_one, &x, result);
9777 
9778  mul_var(&x, &x, &elem, local_rscale);
9779  set_var_from_var(&const_two, &ni);
9780  div_var_fast(&elem, &ni, &elem, local_rscale, true);
9781 
9782  while (elem.ndigits != 0)
9783  {
9784  add_var(result, &elem, result);
9785 
9786  mul_var(&elem, &x, &elem, local_rscale);
9787  add_var(&ni, &const_one, &ni);
9788  div_var_fast(&elem, &ni, &elem, local_rscale, true);
9789  }
9790 
9791  /*
9792  * Compensate for the argument range reduction. Since the weight of the
9793  * result doubles with each multiplication, we can reduce the local rscale
9794  * as we proceed.
9795  */
9796  while (ndiv2-- > 0)
9797  {
9798  local_rscale = sig_digits - result->weight * 2 * DEC_DIGITS;
9799  local_rscale = Max(local_rscale, NUMERIC_MIN_DISPLAY_SCALE);
9800  mul_var(result, result, result, local_rscale);
9801  }
9802 
9803  /* Round to requested rscale */
9804  round_var(result, rscale);
9805 
9806  free_var(&x);
9807  free_var(&elem);
9808  free_var(&ni);
9809 }
static void round_var(NumericVar *var, int rscale)
Definition: numeric.c:10628
int weight
Definition: numeric.c:307
static const NumericVar const_one
Definition: numeric.c:420
int errcode(int sqlerrcode)
Definition: elog.c:691
int ndigits
Definition: numeric.c:306
int dscale
Definition: numeric.c:309
#define Abs(x)
Definition: c.h:988
#define ERROR
Definition: elog.h:43
static double numericvar_to_double_no_overflow(const NumericVar *var)
Definition: numeric.c:7802
#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:8623
static void free_var(NumericVar *var)
Definition: numeric.c:6720
#define ereport(elevel,...)
Definition: elog.h:155
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:8130
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7892
int errmsg(const char *fmt,...)
Definition: elog.c:902
#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:6970
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 4365 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.

4366 {
4368  Numeric res;
4369  NumericVar result;
4370  char buf[FLT_DIG + 100];
4371 
4372  if (isnan(val))
4374 
4375  if (isinf(val))
4376  {
4377  if (val < 0)
4379  else
4381  }
4382 
4383  snprintf(buf, sizeof(buf), "%.*g", FLT_DIG, val);
4384 
4385  init_var(&result);
4386 
4387  /* Assume we need not worry about leading/trailing spaces */
4388  (void) set_var_from_str(buf, buf, &result);
4389 
4390  res = make_result(&result);
4391 
4392  free_var(&result);
4393 
4394  PG_RETURN_NUMERIC(res);
4395 }
#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:552
static void free_var(NumericVar *var)
Definition: numeric.c:6720
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:6760
static const NumericVar const_nan
Definition: numeric.c:460
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:7362
#define snprintf
Definition: port.h:215
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 4272 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().

4273 {
4275  Numeric res;
4276  NumericVar result;
4277  char buf[DBL_DIG + 100];
4278 
4279  if (isnan(val))
4281 
4282  if (isinf(val))
4283  {
4284  if (val < 0)
4286  else
4288  }
4289 
4290  snprintf(buf, sizeof(buf), "%.*g", DBL_DIG, val);
4291 
4292  init_var(&result);
4293 
4294  /* Assume we need not worry about leading/trailing spaces */
4295  (void) set_var_from_str(buf, buf, &result);
4296 
4297  res = make_result(&result);
4298 
4299  free_var(&result);
4300 
4301  PG_RETURN_NUMERIC(res);
4302 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
double float8
Definition: c.h:553
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:6720
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:6760
static const NumericVar const_nan
Definition: numeric.c:460
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:7362
#define snprintf
Definition: port.h:215
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 9121 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().

9122 {
9123  NumericVar tmp;
9124 
9125  init_var(&tmp);
9126  set_var_from_var(var, &tmp);
9127 
9128  trunc_var(&tmp, 0);
9129 
9130  if (var->sign == NUMERIC_NEG && cmp_var(var, &tmp) != 0)
9131  sub_var(&tmp, &const_one, &tmp);
9132 
9133  set_var_from_var(&tmp, result);
9134  free_var(&tmp);
9135 }
static void trunc_var(NumericVar *var, int rscale)
Definition: numeric.c:10734
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:7834
static void free_var(NumericVar *var)
Definition: numeric.c:6720
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8009
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:6970
#define init_var(v)
Definition: numeric.c:495

◆ free_var()

static void free_var ( NumericVar var)
static

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

6721 {
6722  digitbuf_free(var->buf);
6723  var->buf = NULL;
6724  var->digits = NULL;
6725  var->sign = NUMERIC_NAN;
6726 }
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 9144 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().

9145 {
9146  int res_dscale;
9147  int cmp;
9148  NumericVar tmp_arg;
9149  NumericVar mod;
9150 
9151  res_dscale = Max(var1->dscale, var2->dscale);
9152 
9153  /*
9154  * Arrange for var1 to be the number with the greater absolute value.
9155  *
9156  * This would happen automatically in the loop below, but avoids an
9157  * expensive modulo operation.
9158  */
9159  cmp = cmp_abs(var1, var2);
9160  if (cmp < 0)
9161  {
9162  const NumericVar *tmp = var1;
9163 
9164  var1 = var2;
9165  var2 = tmp;
9166  }
9167 
9168  /*
9169  * Also avoid the taking the modulo if the inputs have the same absolute
9170  * value, or if the smaller input is zero.
9171  */
9172  if (cmp == 0 || var2->ndigits == 0)
9173  {
9174  set_var_from_var(var1, result);
9175  result->sign = NUMERIC_POS;
9176  result->dscale = res_dscale;
9177  return;
9178  }
9179 
9180  init_var(&tmp_arg);
9181  init_var(&mod);
9182 
9183  /* Use the Euclidean algorithm to find the GCD */
9184  set_var_from_var(var1, &tmp_arg);
9185  set_var_from_var(var2, result);
9186 
9187  for (;;)
9188  {
9189  /* this loop can take a while, so allow it to be interrupted */
9191 
9192  mod_var(&tmp_arg, result, &mod);
9193  if (mod.ndigits == 0)
9194  break;
9195  set_var_from_var(result, &tmp_arg);
9196  set_var_from_var(&mod, result);
9197  }
9198  result->sign = NUMERIC_POS;
9199  result->dscale = res_dscale;
9200 
9201  free_var(&tmp_arg);
9202  free_var(&mod);
9203 }
#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:8998
static void free_var(NumericVar *var)
Definition: numeric.c:6720
static int cmp_abs(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:10383
#define Max(x, y)
Definition: numeric.c:13
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:6970
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define init_var(v)
Definition: numeric.c:495
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ 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:691
#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:43
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:6953
static const NumericVar const_zero
Definition: numeric.c:416
static int cmp_var(const NumericVar *var1, const NumericVar *var2)
Definition: numeric.c:7834
#define ereport(elevel,...)
Definition: elog.h:155
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:52
static void set_var_from_num(Numeric value, NumericVar *dest)
Definition: numeric.c:6922
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7892
#define PG_NARGS()
Definition: fmgr.h:203
static Numeric make_result(const NumericVar *var)
Definition: numeric.c:7362
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:205
static void set_var_from_var(const NumericVar *value, NumericVar *dest)
Definition: numeric.c:6970
#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 6996 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().

6997 {
6998  int dscale;
6999  char *str;
7000  char *cp;
7001  char *endcp;
7002  int i;
7003  int d;
7004  NumericDigit dig;
7005 
7006 #if DEC_DIGITS > 1
7007  NumericDigit d1;
7008 #endif
7009 
7010  dscale = var->dscale;
7011 
7012  /*
7013  * Allocate space for the result.
7014  *
7015  * i is set to the # of decimal digits before decimal point. dscale is the
7016  * # of decimal digits we will print after decimal point. We may generate
7017  * as many as DEC_DIGITS-1 excess digits at the end, and in addition we
7018  * need room for sign, decimal point, null terminator.
7019  */
7020  i = (var->weight + 1) * DEC_DIGITS;
7021  if (i <= 0)
7022  i = 1;
7023 
7024  str = palloc(i + dscale + DEC_DIGITS + 2);
7025  cp = str;
7026 
7027  /*
7028  * Output a dash for negative values
7029  */
7030  if (var->sign == NUMERIC_NEG)
7031  *cp++ = '-';
7032 
7033  /*
7034  * Output all digits before the decimal point
7035  */
7036  if (var->weight < 0)
7037  {
7038  d = var->weight + 1;
7039  *cp++ = '0';
7040  }
7041  else
7042  {
7043  for (d = 0; d <= var->weight; d++)
7044  {
7045  dig = (d < var->ndigits) ? var->digits[d] : 0;
7046  /* In the first digit, suppress extra leading decimal zeroes */
7047 #if DEC_DIGITS == 4
7048  {
7049  bool putit = (d > 0);
7050 
7051  d1 = dig / 1000;
7052  dig -= d1 * 1000;
7053  putit |= (d1 > 0);
7054  if (putit)
7055  *cp++ = d1 + '0';
7056  d1 = dig / 100;
7057  dig -= d1 * 100;
7058  putit |= (d1 > 0);
7059  if (putit)
7060  *cp++ = d1 + '0';
7061  d1 = dig / 10;
7062  dig -= d1 * 10;
7063  putit |= (d1 > 0);
7064  if (putit)
7065  *cp++ = d1 + '0';
7066  *cp++ = dig + '0';
7067  }
7068 #elif DEC_DIGITS == 2
7069  d1 = dig / 10;
7070  dig -= d1 * 10;
7071  if (d1 > 0 || d > 0)
7072  *cp++ = d1 + '0';
7073  *cp++ = dig + '0';
7074 #elif DEC_DIGITS == 1
7075  *cp++ = dig + '0';
7076 #else
7077 #error unsupported NBASE
7078 #endif
7079  }
7080  }
7081 
7082  /*
7083  * If requested, output a decimal point and all the digits that follow it.
7084  * We initially put out a multiple of DEC_DIGITS digits, then truncate if
7085  * needed.
7086  */
7087  if (dscale > 0)
7088  {
7089  *cp++ = '.';
7090  endcp = cp + dscale;
7091  for (i = 0; i < dscale; d++, i += DEC_DIGITS)
7092  {
7093  dig = (d >= 0 && d < var->ndigits) ? var->digits[d] : 0;
7094 #if DEC_DIGITS == 4
7095  d1 = dig / 1000;
7096  dig -= d1 * 1000;
7097  *cp++ = d1 + '0';
7098  d1 = dig / 100;
7099  dig -= d1 * 100;
7100  *cp++ = d1 + '0';
7101  d1 = dig / 10;
7102  dig -= d1 * 10;
7103  *cp++ = d1 + '0';
7104  *cp++ = dig + '0';
7105 #elif DEC_DIGITS == 2
7106  d1 = dig / 10;
7107  dig -= d1 * 10;
7108  *cp++ = d1 + '0';
7109  *cp++ = dig + '0';
7110 #elif DEC_DIGITS == 1
7111  *cp++ = dig + '0';
7112 #else
7113 #error unsupported NBASE
7114 #endif
7115  }
7116  cp = endcp;
7117  }
7118 
7119  /*
7120  * terminate the string and return it
7121  */
7122  *cp = '\0';
7123  return str;
7124 }
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:950
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 7149 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().

7150 {
7151  int32 exponent;
7152  NumericVar denominator;
7153  NumericVar significand;
7154  int denom_scale;
7155  size_t len;
7156  char *str;
7157  char *sig_out;
7158 
7159  if (rscale < 0)
7160  rscale = 0;
7161 
7162  /*
7163  * Determine the exponent of this number in normalised form.
7164  *
7165  * This is the exponent required to represent the number with only one
7166  * significant digit before the decimal place.
7167  */
7168  if (var->ndigits > 0)
7169  {
7170  exponent = (var->weight + 1) * DEC_DIGITS;
7171 
7172  /*
7173  * Compensate for leading decimal zeroes in the first numeric digit by
7174  * decrementing the exponent.
7175  */
7176  exponent -= DEC_DIGITS - (int) log10(var->digits[0]);
7177  }
7178  else
7179  {
7180  /*
7181  * If var has no digits, then it must be zero.
7182  *
7183  * Zero doesn't technically have a meaningful exponent in normalised
7184  * notation, but we just display the exponent as zero for consistency
7185  * of output.
7186  */
7187  exponent = 0;
7188  }
7189 
7190  /*
7191  * The denominator is set to 10 raised to the power of the exponent.
7192  *
7193  * We then divide var by the denominator to get the significand, rounding
7194  * to rscale decimal digits in the process.
7195  */
7196  if (exponent < 0)
7197  denom_scale = -exponent;
7198  else
7199  denom_scale = 0;
7200 
7201  init_var(&denominator);
7202  init_var(&significand);
7203 
7204  power_var_int(&const_ten, exponent, &denominator, denom_scale);
7205  div_var(var, &denominator, &significand, rscale, true);
7206  sig_out = get_str_from_var(&significand);
7207 
7208  free_var(&denominator);
7209  free_var(&significand);
7210 
7211  /*
7212  * Allocate space for the result.
7213  *
7214  * In addition to the significand, we need room for the exponent
7215  * decoration ("e"), the sign of the exponent, up to 10 digits for the
7216  * exponent itself, and of course the null terminator.
7217  */
7218  len = strlen(sig_out) + 13;
7219  str = palloc(len);
7220  snprintf(str, len, "%se%+03d", sig_out, exponent);
7221 
7222  pfree(sig_out);
7223 
7224  return str;
7225 }
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:8338
signed int int32
Definition: c.h:417
void pfree(void *pointer)
Definition: mcxt.c:1057
static void power_var_int(const NumericVar *base, int exp, NumericVar *result, int rscale)
Definition: numeric.c:10193
static const NumericVar const_ten
Definition: numeric.c:432
static void free_var(NumericVar *var)
Definition: numeric.c:6720
NumericDigit * digits
Definition: numeric.c:311
void * palloc(Size size)
Definition: mcxt.c:950
static char * get_str_from_var(const NumericVar *var)
Definition: numeric.c:6996
#define DEC_DIGITS
Definition: numeric.c:99
#define snprintf
Definition: port.h:215
#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:367
#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:800
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:648
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:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define DatumGetUInt64(X)
Definition: postgres.h:634
#define NUMERIC_WEIGHT(n)
Definition: numeric.c:247
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:52
#define Assert(condition)
Definition: c.h:800
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:691
#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:43
#define NUMERIC_IS_PINF(n)
Definition: numeric.c:206
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:6953
#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:7834
static void free_var(NumericVar *var)
Definition: numeric.c:6720
#define ereport(elevel,...)
Definition: elog.h:155
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:52
#define Assert(condition)
Definition: c.h:800
static void add_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:7892
#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:902
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:205
static void sub_var(const NumericVar *var1, const NumericVar *var2, NumericVar *result)
Definition: numeric.c:8009
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 6953 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().

6954 {
6955  dest->ndigits = NUMERIC_NDIGITS(num);
6956  dest->weight = NUMERIC_WEIGHT(num);
6957  dest->sign = NUMERIC_SIGN(num);
6958  dest->dscale = NUMERIC_DSCALE(num);
6959  dest->digits = NUMERIC_DIGITS(num);
6960  dest->buf = NULL; /* digits array is not palloc'd */
6961 }
#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 5266 of file numeric.c.

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

5267 {
5269 
5270  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
5271 
5272  /* Create the state data on the first call */
5273  if (state == NULL)
5274  state = makePolyNumAggState(fcinfo, true);
5275 
5276  if (!PG_ARGISNULL(1))
5277  {
5278 #ifdef HAVE_INT128
5279  do_int128_accum(state, (int128) PG_GETARG_INT16(1));
5280 #else
5282 #endif
5283  }
5284 
5285  PG_RETURN_POINTER(state);
5286 }
#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:4534
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:298
#define makePolyNumAggState
Definition: numeric.c:5261

◆ int2_accum_inv()

Datum int2_accum_inv ( PG_FUNCTION_ARGS  )

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

5726 {
5728 
5729  state = PG_ARGISNULL(0) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(0);
5730 
5731  /* Should not get here with no state */
5732  if (state == NULL)
5733  elog(ERROR, "int2_accum_inv called with NULL state");
5734 
5735  if (!PG_ARGISNULL(1))
5736  {
5737 #ifdef HAVE_INT128
5738  do_int128_discard(state, (int128) PG_GETARG_INT16(1));
5739 #else
5740  /* Should never fail, all inputs have dscale 0 */
5742  elog(ERROR, "do_numeric_discard failed unexpectedly");
5743 #endif
5744  }
5745 
5746  PG_RETURN_POINTER(state);
5747 }
#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:4604
#define ERROR
Definition: elog.h:43
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:298
#define elog(elevel,...)
Definition: elog.h:228

◆ int2_avg_accum()

Datum int2_avg_accum ( PG_FUNCTION_ARGS  )

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

6409 {
6410  ArrayType *transarray;
6412  Int8TransTypeData *transdata;
6413 
6414  /*
6415  * If we're invoked as an aggregate, we can cheat and modify our first
6416  * parameter in-place to reduce palloc overhead. Otherwise we need to make
6417  * a copy of it before scribbling on it.
6418  */
6419  if (AggCheckCallContext(fcinfo, NULL))
6420  transarray = PG_GETARG_ARRAYTYPE_P(0);
6421  else
6422  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
6423 
6424  if (ARR_HASNULL(transarray) ||
6425  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
6426  elog(ERROR, "expected 2-element int8 array");
6427 
6428  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
6429  transdata->count++;
6430  transdata->sum += newval;
6431 
6432  PG_RETURN_ARRAYTYPE_P(transarray);
6433 }
signed short int16
Definition: c.h:416
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:298
#define ARR_SIZE(a)
Definition: array.h:277
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition: array.h:252
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#define ERROR
Definition: elog.h:43
#define ARR_DATA_PTR(a)
Definition: array.h:310
#define ARR_HASNULL(a)
Definition: array.h:279
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:253
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4572
#define elog(elevel,...)
Definition: elog.h:228

◆ int2_avg_accum_inv()

Datum int2_avg_accum_inv ( PG_FUNCTION_ARGS  )

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

6496 {
6497  ArrayType *transarray;
6499  Int8TransTypeData *transdata;
6500 
6501  /*
6502  * If we're invoked as an aggregate, we can cheat and modify our first
6503  * parameter in-place to reduce palloc overhead. Otherwise we need to make
6504  * a copy of it before scribbling on it.
6505  */
6506  if (AggCheckCallContext(fcinfo, NULL))
6507  transarray = PG_GETARG_ARRAYTYPE_P(0);
6508  else
6509  transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
6510 
6511  if (ARR_HASNULL(transarray) ||
6512  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
6513  elog(ERROR, "expected 2-element int8 array");
6514 
6515  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
6516  transdata->count--;
6517  transdata->sum -= newval;
6518 
6519  PG_RETURN_ARRAYTYPE_P(transarray);
6520 }
signed short int16
Definition: c.h:416
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:298
#define ARR_SIZE(a)
Definition: array.h:277
#define PG_GETARG_ARRAYTYPE_P_COPY(n)
Definition: array.h:252
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#define ERROR
Definition: elog.h:43
#define ARR_DATA_PTR(a)
Definition: array.h:310
#define ARR_HASNULL(a)
Definition: array.h:279
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:253
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define newval
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4572
#define elog(elevel,...)
Definition: elog.h:228

◆ int2_numeric()

Datum int2_numeric ( PG_FUNCTION_ARGS  )

Definition at line 4222 of file numeric.c.

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

4223 {
4224  int16 val = PG_GETARG_INT16(0);
4225 
4227 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
signed short int16
Definition: c.h:416
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 6259 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.

6260 {
6261  int64 newval;
6262 
6263  if (PG_ARGISNULL(0))
6264  {
6265  /* No non-null input seen so far... */
6266  if (PG_ARGISNULL(1))
6267  PG_RETURN_NULL(); /* still no non-null */
6268  /* This is the first non-null input. */
6269  newval = (int64) PG_GETARG_INT16(1);
6270  PG_RETURN_INT64(newval);
6271  }
6272 
6273  /*
6274  * If we're invoked as an aggregate, we can cheat and modify our first
6275  * parameter in-place to avoid palloc overhead. If not, we need to return
6276  * the new value of the transition variable. (If int8 is pass-by-value,
6277  * then of course this is useless as well as incorrect, so just ifdef it
6278  * out.)
6279  */
6280 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
6281  if (AggCheckCallContext(fcinfo, NULL))
6282  {
6283  int64 *oldsum = (int64 *) PG_GETARG_POINTER(0);
6284 
6285  /* Leave the running sum unchanged in the new input is null */
6286  if (!PG_ARGISNULL(1))
6287  *oldsum = *oldsum + (int64) PG_GETARG_INT16(1);
6288 
6289  PG_RETURN_POINTER(oldsum);
6290  }
6291  else
6292 #endif
6293  {
6294  int64 oldsum = PG_GETARG_INT64(0);
6295 
6296  /* Leave sum unchanged if new input is null. */
6297  if (PG_ARGISNULL(1))
6298  PG_RETURN_INT64(oldsum);
6299 
6300  /* OK to do the addition. */
6301  newval = oldsum + (int64) PG_GETARG_INT16(1);
6302 
6303  PG_RETURN_INT64(newval);
6304  }
6305 }
#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:4572
#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 6578 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.

6579 {
6580  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
6581  Int8TransTypeData *transdata;
6582 
6583  if (ARR_HASNULL(transarray) ||
6584  ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
6585  elog(ERROR, "expected 2-element int8 array");
6586  transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
6587 
6588  /* SQL defines SUM of no values to be NULL */
6589  if (transdata->count == 0)
6590  PG_RETURN_NULL();
6591 
6592  PG_RETURN_DATUM(Int64GetDatumFast(transdata->sum));
6593 }
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:298
#define ARR_SIZE(a)
Definition: array.h:277