PostgreSQL Source Code  git master
shortest_dec.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define DOUBLE_SHORTEST_DECIMAL_LEN   25
 
#define FLOAT_SHORTEST_DECIMAL_LEN   16
 

Functions

int double_to_shortest_decimal_bufn (double f, char *result)
 
int double_to_shortest_decimal_buf (double f, char *result)
 
char * double_to_shortest_decimal (double f)
 
int float_to_shortest_decimal_bufn (float f, char *result)
 
int float_to_shortest_decimal_buf (float f, char *result)
 
char * float_to_shortest_decimal (float f)
 

Macro Definition Documentation

◆ DOUBLE_SHORTEST_DECIMAL_LEN

#define DOUBLE_SHORTEST_DECIMAL_LEN   25

Definition at line 44 of file shortest_dec.h.

◆ FLOAT_SHORTEST_DECIMAL_LEN

#define FLOAT_SHORTEST_DECIMAL_LEN   16

Definition at line 57 of file shortest_dec.h.

Function Documentation

◆ double_to_shortest_decimal()

char* double_to_shortest_decimal ( double  f)

Definition at line 1070 of file d2s.c.

1071 {
1072  char *const result = (char *) palloc(DOUBLE_SHORTEST_DECIMAL_LEN);
1073 
1074  double_to_shortest_decimal_buf(f, result);
1075  return result;
1076 }
int double_to_shortest_decimal_buf(double f, char *result)
Definition: d2s.c:1053
void * palloc(Size size)
Definition: mcxt.c:1317
#define DOUBLE_SHORTEST_DECIMAL_LEN
Definition: shortest_dec.h:44

References DOUBLE_SHORTEST_DECIMAL_LEN, double_to_shortest_decimal_buf(), and palloc().

◆ double_to_shortest_decimal_buf()

int double_to_shortest_decimal_buf ( double  f,
char *  result 
)

Definition at line 1053 of file d2s.c.

1054 {
1055  const int index = double_to_shortest_decimal_bufn(f, result);
1056 
1057  /* Terminate the string. */
1059  result[index] = '\0';
1060  return index;
1061 }
#define Assert(condition)
Definition: c.h:861
int double_to_shortest_decimal_bufn(double f, char *result)
Definition: d2s.c:1015
Definition: type.h:95

References Assert, DOUBLE_SHORTEST_DECIMAL_LEN, and double_to_shortest_decimal_bufn().

Referenced by double_to_shortest_decimal(), float8out_internal(), and outDouble().

◆ double_to_shortest_decimal_bufn()

int double_to_shortest_decimal_bufn ( double  f,
char *  result 
)

Definition at line 1015 of file d2s.c.

1016 {
1017  /*
1018  * Step 1: Decode the floating-point number, and unify normalized and
1019  * subnormal cases.
1020  */
1021  const uint64 bits = double_to_bits(f);
1022 
1023  /* Decode bits into sign, mantissa, and exponent. */
1024  const bool ieeeSign = ((bits >> (DOUBLE_MANTISSA_BITS + DOUBLE_EXPONENT_BITS)) & 1) != 0;
1025  const uint64 ieeeMantissa = bits & ((UINT64CONST(1) << DOUBLE_MANTISSA_BITS) - 1);
1026  const uint32 ieeeExponent = (uint32) ((bits >> DOUBLE_MANTISSA_BITS) & ((1u << DOUBLE_EXPONENT_BITS) - 1));
1027 
1028  /* Case distinction; exit early for the easy cases. */
1029  if (ieeeExponent == ((1u << DOUBLE_EXPONENT_BITS) - 1u) || (ieeeExponent == 0 && ieeeMantissa == 0))
1030  {
1031  return copy_special_str(result, ieeeSign, (ieeeExponent != 0), (ieeeMantissa != 0));
1032  }
1033 
1035  const bool isSmallInt = d2d_small_int(ieeeMantissa, ieeeExponent, &v);
1036 
1037  if (!isSmallInt)
1038  {
1039  v = d2d(ieeeMantissa, ieeeExponent);
1040  }
1041 
1042  return to_chars(v, ieeeSign, result);
1043 }
unsigned int uint32
Definition: c.h:509
#define DOUBLE_EXPONENT_BITS
Definition: d2s.c:66
static floating_decimal_64 d2d(const uint64 ieeeMantissa, const uint32 ieeeExponent)
Definition: d2s.c:346
#define DOUBLE_MANTISSA_BITS
Definition: d2s.c:65
static bool d2d_small_int(const uint64 ieeeMantissa, const uint32 ieeeExponent, floating_decimal_64 *v)
Definition: d2s.c:962
static int to_chars(floating_decimal_64 v, const bool sign, char *const result)
Definition: d2s.c:787
static uint64 double_to_bits(const double d)
Definition: ryu_common.h:125
static int copy_special_str(char *const result, const bool sign, const bool exponent, const bool mantissa)
Definition: ryu_common.h:95

References copy_special_str(), d2d(), d2d_small_int(), DOUBLE_EXPONENT_BITS, DOUBLE_MANTISSA_BITS, double_to_bits(), and to_chars().

Referenced by double_to_shortest_decimal_buf().

◆ float_to_shortest_decimal()

char* float_to_shortest_decimal ( float  f)

Definition at line 797 of file f2s.c.

798 {
799  char *const result = (char *) palloc(FLOAT_SHORTEST_DECIMAL_LEN);
800 
802  return result;
803 }
int float_to_shortest_decimal_buf(float f, char *result)
Definition: f2s.c:780
#define FLOAT_SHORTEST_DECIMAL_LEN
Definition: shortest_dec.h:57

References FLOAT_SHORTEST_DECIMAL_LEN, float_to_shortest_decimal_buf(), and palloc().

◆ float_to_shortest_decimal_buf()

int float_to_shortest_decimal_buf ( float  f,
char *  result 
)

Definition at line 780 of file f2s.c.

781 {
782  const int index = float_to_shortest_decimal_bufn(f, result);
783 
784  /* Terminate the string. */
786  result[index] = '\0';
787  return index;
788 }
int float_to_shortest_decimal_bufn(float f, char *result)
Definition: f2s.c:742

References Assert, FLOAT_SHORTEST_DECIMAL_LEN, and float_to_shortest_decimal_bufn().

Referenced by float4out(), and float_to_shortest_decimal().

◆ float_to_shortest_decimal_bufn()

int float_to_shortest_decimal_bufn ( float  f,
char *  result 
)

Definition at line 742 of file f2s.c.

743 {
744  /*
745  * Step 1: Decode the floating-point number, and unify normalized and
746  * subnormal cases.
747  */
748  const uint32 bits = float_to_bits(f);
749 
750  /* Decode bits into sign, mantissa, and exponent. */
751  const bool ieeeSign = ((bits >> (FLOAT_MANTISSA_BITS + FLOAT_EXPONENT_BITS)) & 1) != 0;
752  const uint32 ieeeMantissa = bits & ((1u << FLOAT_MANTISSA_BITS) - 1);
753  const uint32 ieeeExponent = (bits >> FLOAT_MANTISSA_BITS) & ((1u << FLOAT_EXPONENT_BITS) - 1);
754 
755  /* Case distinction; exit early for the easy cases. */
756  if (ieeeExponent == ((1u << FLOAT_EXPONENT_BITS) - 1u) || (ieeeExponent == 0 && ieeeMantissa == 0))
757  {
758  return copy_special_str(result, ieeeSign, (ieeeExponent != 0), (ieeeMantissa != 0));
759  }
760 
762  const bool isSmallInt = f2d_small_int(ieeeMantissa, ieeeExponent, &v);
763 
764  if (!isSmallInt)
765  {
766  v = f2d(ieeeMantissa, ieeeExponent);
767  }
768 
769  return to_chars(v, ieeeSign, result);
770 }
static int to_chars(const floating_decimal_32 v, const bool sign, char *const result)
Definition: f2s.c:563
#define FLOAT_MANTISSA_BITS
Definition: f2s.c:45
static bool f2d_small_int(const uint32 ieeeMantissa, const uint32 ieeeExponent, floating_decimal_32 *v)
Definition: f2s.c:689
static floating_decimal_32 f2d(const uint32 ieeeMantissa, const uint32 ieeeExponent)
Definition: f2s.c:222
#define FLOAT_EXPONENT_BITS
Definition: f2s.c:46
static uint32 float_to_bits(const float f)
Definition: ryu_common.h:116

References copy_special_str(), f2d(), f2d_small_int(), FLOAT_EXPONENT_BITS, FLOAT_MANTISSA_BITS, float_to_bits(), and to_chars().

Referenced by float_to_shortest_decimal_buf().