MySQL 9.1.0
Source Code Documentation
|
#include "my_config.h"
#include "mysql/strings/dtoa.h"
#include <algorithm>
#include <cassert>
#include <cerrno>
#include <cfloat>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <limits>
#include "my_pointer_arithmetic.h"
#include "template_utils.h"
Classes | |
union | U |
struct | Bigint |
struct | Stack_alloc |
Macros | |
#define | EOVERFLOW 84 |
#define | DTOA_BUFF_SIZE (460 * sizeof(void *)) |
Appears to suffice to not call malloc() in most cases. More... | |
#define | DTOA_OVERFLOW 9999 |
#define | word0(x) (x)->L[1] |
#define | word1(x) (x)->L[0] |
#define | dval(x) (x)->d |
#define | Exp_shift 20 |
#define | Exp_shift1 20 |
#define | Exp_msk1 0x100000 |
#define | Exp_mask 0x7ff00000 |
#define | P 53 |
#define | Bias 1023 |
#define | Emin (-1022) |
#define | Exp_1 0x3ff00000 |
#define | Exp_11 0x3ff00000 |
#define | Ebits 11 |
#define | Frac_mask 0xfffff |
#define | Frac_mask1 0xfffff |
#define | Ten_pmax 22 |
#define | Bletch 0x10 |
#define | Bndry_mask 0xfffff |
#define | Bndry_mask1 0xfffff |
#define | LSB 1 |
#define | Sign_bit 0x80000000 |
#define | Log2P 1 |
#define | Tiny1 1 |
#define | Quick_max 14 |
#define | Int_max 14 |
#define | Flt_Rounds 1 |
#define | rounded_product(a, b) a *= b |
#define | rounded_quotient(a, b) a /= b |
#define | Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1)) |
#define | Big1 0xffffffff |
#define | FFFFFFFF 0xffffffffUL |
#define | Kmax 15 |
#define | Bcopy(x, y) |
#define | P5A_MAX (sizeof(p5_a) / sizeof(*p5_a) - 1) |
#define | d0 word0(&d) |
#define | d1 word1(&d) |
#define | d0 word0(d) |
#define | d1 word1(d) |
#define | Scale_Bit 0x10 |
#define | n_bigtens 5 |
Typedefs | |
typedef struct Bigint | Bigint |
typedef struct Stack_alloc | Stack_alloc |
Functions | |
static double | my_strtod_int (const char *, const char **, int *, char *, size_t) |
static char * | dtoa (double, int, int, int *, int *, char **, char *, size_t) |
static void | dtoa_free (char *, char *, size_t) |
static size_t | my_fcvt_internal (double x, int precision, bool shorten, char *to, bool *error) |
Converts a given floating point number to a zero-terminated string representation using the 'f' format. More... | |
size_t | my_fcvt (double x, int precision, char *to, bool *error) |
Converts a given floating point number to a zero-terminated string representation using the 'f' format. More... | |
size_t | my_fcvt_no_trailing_zero (double x, int precision, char *to, bool *error) |
Identical to my_fcvt, except that trailing zeros after the decimal point will be removed. More... | |
size_t | my_fcvt_compact (double x, char *to, bool *error) |
Converts a given floating point number to a zero-terminated string representation using the 'f' format. More... | |
size_t | my_gcvt (double x, my_gcvt_arg_type type, int width, char *to, bool *error) |
Converts a given floating point number to a zero-terminated string representation with a given field width using the 'e' format (aka scientific notation) or the 'f' one. More... | |
double | my_strtod (const char *str, const char **end, int *error) |
Converts string to double (string does not have to be zero-terminated) More... | |
static Bigint * | Balloc (int k, Stack_alloc *alloc) |
static void | Bfree (Bigint *v, Stack_alloc *alloc) |
static char * | dtoa_alloc (int i, Stack_alloc *alloc) |
static Bigint * | multadd (Bigint *b, int m, int a, Stack_alloc *alloc) |
static Bigint * | s2b (const char *s, int nd0, int nd, uint32_t y9, Stack_alloc *alloc) |
Converts a string to Bigint. More... | |
static int | hi0bits (uint32_t x) |
static int | lo0bits (uint32_t *y) |
static Bigint * | i2b (int i, Stack_alloc *alloc) |
static Bigint * | mult (Bigint *a, Bigint *b, Stack_alloc *alloc) |
static Bigint * | pow5mult (Bigint *b, int k, Stack_alloc *alloc) |
static Bigint * | lshift (Bigint *b, int k, Stack_alloc *alloc) |
static int | cmp (Bigint *a, Bigint *b) |
static Bigint * | diff (Bigint *a, Bigint *b, Stack_alloc *alloc) |
static double | ulp (U *x) |
static double | b2d (Bigint *a, int *e) |
static Bigint * | d2b (U *d, int *e, int *bits, Stack_alloc *alloc) |
static double | ratio (Bigint *a, Bigint *b) |
static int | quorem (Bigint *b, Bigint *S) |
Variables | |
static uint32_t | powers5 [] |
static Bigint | p5_a [] |
static const double | tens [] |
static const double | bigtens [] = {1e16, 1e32, 1e64, 1e128, 1e256} |
static const double | tinytens [] |
#define Bcopy | ( | x, | |
y | |||
) |
#define Bias 1023 |
#define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1)) |
#define Big1 0xffffffff |
#define Bletch 0x10 |
#define Bndry_mask 0xfffff |
#define Bndry_mask1 0xfffff |
#define d0 word0(&d) |
#define d0 word0(d) |
#define d1 word1(&d) |
#define d1 word1(d) |
#define DTOA_BUFF_SIZE (460 * sizeof(void *)) |
Appears to suffice to not call malloc() in most cases.
#define DTOA_OVERFLOW 9999 |
#define dval | ( | x | ) | (x)->d |
#define Ebits 11 |
#define Emin (-1022) |
#define EOVERFLOW 84 |
#define Exp_1 0x3ff00000 |
#define Exp_11 0x3ff00000 |
#define Exp_mask 0x7ff00000 |
#define Exp_msk1 0x100000 |
#define Exp_shift 20 |
#define Exp_shift1 20 |
#define FFFFFFFF 0xffffffffUL |
#define Flt_Rounds 1 |
#define Frac_mask 0xfffff |
#define Frac_mask1 0xfffff |
#define Int_max 14 |
#define Kmax 15 |
#define Log2P 1 |
#define LSB 1 |
#define n_bigtens 5 |
#define P 53 |
#define Quick_max 14 |
#define rounded_product | ( | a, | |
b | |||
) | a *= b |
#define rounded_quotient | ( | a, | |
b | |||
) | a /= b |
#define Scale_Bit 0x10 |
#define Sign_bit 0x80000000 |
#define Ten_pmax 22 |
#define Tiny1 1 |
#define word0 | ( | x | ) | (x)->L[1] |
#define word1 | ( | x | ) | (x)->L[0] |
typedef struct Stack_alloc Stack_alloc |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
size_t my_fcvt | ( | double | x, |
int | precision, | ||
char * | to, | ||
bool * | error | ||
) |
Converts a given floating point number to a zero-terminated string representation using the 'f' format.
This function is a wrapper around dtoa() to do the same as sprintf(to, "%-.*f", precision, x), though the conversion is usually more precise. The only difference is in handling [-,+]infinity and nan values, in which case we print '0\0' to the output string and indicate an overflow.
x | the input floating point number. |
precision | the number of digits after the decimal point. All properties of sprintf() apply:
|
to | pointer to the output buffer. The longest string which my_fcvt() can return is FLOATING_POINT_BUFFER bytes (including the terminating '\0'). |
error | if not NULL, points to a location where the status of conversion is stored upon return. false successful conversion true the input number is [-,+]infinity or nan. The output string in this case is always '0'. |
size_t my_fcvt_compact | ( | double | x, |
char * | to, | ||
bool * | error | ||
) |
Converts a given floating point number to a zero-terminated string representation using the 'f' format.
This function is a wrapper around dtoa() to do almost the same as sprintf(to, "%-.*f", precision, x), though the conversion is usually more precise. The only difference is in handling [-,+]infinity and nan values, in which case we print '0\0' to the output string and indicate an overflow.
The string always contains the minimum number of digits necessary to reproduce the same binary double value if the string is parsed back to a double value.
x | the input floating point number. |
to | pointer to the output buffer. The longest string which my_fcvt() can return is FLOATING_POINT_BUFFER bytes (including the terminating '\0'). |
error | if not NULL, points to a location where the status of conversion is stored upon return. false successful conversion true the input number is [-,+]infinity or nan. The output string in this case is always '0'. |
|
static |
Converts a given floating point number to a zero-terminated string representation using the 'f' format.
This function is a wrapper around dtoa() to do the same as sprintf(to, "%-.*f", precision, x), though the conversion is usually more precise. The only difference is in handling [-,+]infinity and nan values, in which case we print '0\0' to the output string and indicate an overflow.
x | the input floating point number. |
precision | the number of digits after the decimal point. All properties of sprintf() apply:
|
to | pointer to the output buffer. The longest string which my_fcvt() can return is FLOATING_POINT_BUFFER bytes (including the terminating '\0'). |
error | if not NULL, points to a location where the status of conversion is stored upon return. false successful conversion true the input number is [-,+]infinity or nan. The output string in this case is always '0'. |
shorten | Whether to minimize the number of significant digits. If true, write only the minimum number of digits necessary to reproduce the double value when parsing the string. If false, zeros are added to the end to reach the precision limit. |
size_t my_fcvt_no_trailing_zero | ( | double | x, |
int | precision, | ||
char * | to, | ||
bool * | error | ||
) |
Identical to my_fcvt, except that trailing zeros after the decimal point will be removed.
size_t my_gcvt | ( | double | x, |
my_gcvt_arg_type | type, | ||
int | width, | ||
char * | to, | ||
bool * | error | ||
) |
Converts a given floating point number to a zero-terminated string representation with a given field width using the 'e' format (aka scientific notation) or the 'f' one.
The format is chosen automatically to provide the most number of significant digits (and thus, precision) with a given field width. In many cases, the result is similar to that of sprintf(to, "%g", x) with a few notable differences:
x | the input floating point number. |
type | is either MY_GCVT_ARG_FLOAT or MY_GCVT_ARG_DOUBLE. Specifies the type of the input number (see notes above). |
width | field width in characters. The minimal field width to hold any number representation (albeit rounded) is 7 characters ("-Ne-NNN"). |
to | pointer to the output buffer. The result is always zero-terminated, and the longest returned string is thus 'width + 1' bytes. |
error | if not NULL, points to a location where the status of conversion is stored upon return. false successful conversion true the input number is [-,+]infinity or nan. The output string in this case is always '0'. |
my_gcvt(-9e-3, ..., 4, ...); my_gcvt(-9e-3, ..., 2, ...); my_gcvt(1.87e-3, ..., 4, ...); my_gcvt(55, ..., 1, ...);
We do our best to minimize such cases by:
double my_strtod | ( | const char * | str, |
const char ** | end, | ||
int * | error | ||
) |
Converts string to double (string does not have to be zero-terminated)
This is a wrapper around dtoa's version of strtod().
str | input string |
end | address of a pointer to the first character after the input string. Upon return the pointer is set to point to the first rejected character. |
error | Upon return is set to EOVERFLOW in case of underflow or overflow. |
|
static |
|
static |
|
static |
Converts a string to Bigint.
Now we have nd0 digits, starting at s, followed by a decimal point, followed by nd-nd0 digits. Unless nd0 == nd, in which case we have a number of the form: ".xxxxxx" or "xxxxxx."
s | Input string, already partially parsed by my_strtod_int(). |
nd0 | Number of digits before decimal point. |
nd | Total number of digits. |
y9 | Pre-computed value of the first nine digits. |
alloc | Stack allocator for Bigints. |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |