hoshi-lang dev
Yet another programming language
Loading...
Searching...
No Matches
detail::dtoa_impl Namespace Reference

implements the Grisu2 algorithm for binary to decimal floating-point conversion. More...

Classes

struct  boundaries
 
struct  cached_power
 
struct  diyfp
 

Functions

template<typename Target , typename Source >
Target reinterpret_bits (const Source source)
 
template<typename FloatType >
boundaries compute_boundaries (FloatType value)
 
cached_power get_cached_power_for_binary_exponent (int e)
 
int find_largest_pow10 (const std::uint32_t n, std::uint32_t &pow10)
 
void grisu2_round (char *buf, int len, std::uint64_t dist, std::uint64_t delta, std::uint64_t rest, std::uint64_t ten_k)
 
void grisu2_digit_gen (char *buffer, int &length, int &decimal_exponent, diyfp M_minus, diyfp w, diyfp M_plus)
 
void grisu2 (char *buf, int &len, int &decimal_exponent, diyfp m_minus, diyfp v, diyfp m_plus)
 
template<typename FloatType >
void grisu2 (char *buf, int &len, int &decimal_exponent, FloatType value)
 
JSON_HEDLEY_RETURNS_NON_NULL char * append_exponent (char *buf, int e)
 appends a decimal representation of e to buf
 
JSON_HEDLEY_RETURNS_NON_NULL char * format_buffer (char *buf, int len, int decimal_exponent, int min_exp, int max_exp)
 prettify v = buf * 10^decimal_exponent
 

Variables

constexpr int kAlpha = -60
 
constexpr int kGamma = -32
 

Detailed Description

implements the Grisu2 algorithm for binary to decimal floating-point conversion.

This implementation is a slightly modified version of the reference implementation which may be obtained from http://florian.loitsch.com/publications (bench.tar.gz).

The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.

For a detailed description of the algorithm see:

[1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming Language Design and Implementation, PLDI 2010 [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately", Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language Design and Implementation, PLDI 1996

Function Documentation

◆ append_exponent()

JSON_HEDLEY_RETURNS_NON_NULL char * append_exponent ( char *  buf,
int  e 
)
inlineprivate

appends a decimal representation of e to buf

Returns
a pointer to the element following the exponent.
Precondition
-1000 < e < 1000

Definition at line 18548 of file json.hpp.

References JSON_ASSERT.

Referenced by format_buffer().

Here is the caller graph for this function:

◆ compute_boundaries()

template<typename FloatType >
boundaries compute_boundaries ( FloatType  value)
private

Compute the (normalized) diyfp representing the input number 'value' and its boundaries.

Precondition
value must be finite and positive

Definition at line 17809 of file json.hpp.

References compute_boundaries(), diyfp::e, diyfp::f, and JSON_ASSERT.

Referenced by compute_boundaries(), and grisu2().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_largest_pow10()

int find_largest_pow10 ( const std::uint32_t  n,
std::uint32_t &  pow10 
)
inlineprivate

For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k. For n == 0, returns 1 and sets pow10 := 1.

Definition at line 18112 of file json.hpp.

References find_largest_pow10().

Referenced by find_largest_pow10(), and grisu2_digit_gen().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ format_buffer()

JSON_HEDLEY_RETURNS_NON_NULL char * format_buffer ( char *  buf,
int  len,
int  decimal_exponent,
int  min_exp,
int  max_exp 
)
inlineprivate

prettify v = buf * 10^decimal_exponent

If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point notation. Otherwise it will be printed in exponential notation.

Precondition
min_exp < 0
max_exp > 0

Definition at line 18600 of file json.hpp.

References append_exponent(), and JSON_ASSERT.

Here is the call graph for this function:

◆ get_cached_power_for_binary_exponent()

cached_power get_cached_power_for_binary_exponent ( int  e)
inlineprivate

For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c satisfies (Definition 3.2 from [1])

 alpha <= e_c + e + q <= gamma.

Definition at line 17948 of file json.hpp.

References cached_power::e, get_cached_power_for_binary_exponent(), JSON_ASSERT, kAlpha, and kGamma.

Referenced by get_cached_power_for_binary_exponent(), and grisu2().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ grisu2() [1/2]

void grisu2 ( char *  buf,
int &  len,
int &  decimal_exponent,
diyfp  m_minus,
diyfp  v,
diyfp  m_plus 
)
inlineprivate

v = buf * 10^decimal_exponent len is the length of the buffer (number of decimal digits) The buffer must be large enough, i.e. >= max_digits10.

Definition at line 18448 of file json.hpp.

References diyfp::e, cached_power::e, diyfp::f, cached_power::f, get_cached_power_for_binary_exponent(), grisu2_digit_gen(), JSON_ASSERT, and cached_power::k.

Referenced by grisu2().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ grisu2() [2/2]

template<typename FloatType >
void grisu2 ( char *  buf,
int &  len,
int &  decimal_exponent,
FloatType  value 
)
private

v = buf * 10^decimal_exponent len is the length of the buffer (number of decimal digits) The buffer must be large enough, i.e. >= max_digits10.

Definition at line 18508 of file json.hpp.

References compute_boundaries(), grisu2(), JSON_ASSERT, boundaries::minus, boundaries::plus, and boundaries::w.

Here is the call graph for this function:

◆ grisu2_digit_gen()

void grisu2_digit_gen ( char *  buffer,
int &  length,
int &  decimal_exponent,
diyfp  M_minus,
diyfp  w,
diyfp  M_plus 
)
inlineprivate

Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+. M- and M+ must be normalized and share the same exponent -60 <= e <= -32.

Definition at line 18207 of file json.hpp.

References diyfp::e, diyfp::f, find_largest_pow10(), grisu2_digit_gen(), grisu2_round(), JSON_ASSERT, kAlpha, and kGamma.

Referenced by grisu2(), and grisu2_digit_gen().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ grisu2_round()

void grisu2_round ( char *  buf,
int  len,
std::uint64_t  dist,
std::uint64_t  delta,
std::uint64_t  rest,
std::uint64_t  ten_k 
)
inlineprivate

Definition at line 18166 of file json.hpp.

References grisu2_round(), and JSON_ASSERT.

Referenced by grisu2_digit_gen(), and grisu2_round().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reinterpret_bits()

template<typename Target , typename Source >
Target reinterpret_bits ( const Source  source)
private

Definition at line 17668 of file json.hpp.

References reinterpret_bits().

Referenced by reinterpret_bits().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ kAlpha

constexpr int kAlpha = -60
constexprprivate

Definition at line 17931 of file json.hpp.

Referenced by get_cached_power_for_binary_exponent(), and grisu2_digit_gen().

◆ kGamma

constexpr int kGamma = -32
constexprprivate

Definition at line 17932 of file json.hpp.

Referenced by get_cached_power_for_binary_exponent(), and grisu2_digit_gen().