Skip to content

Commit

Permalink
Make it possible to specialize compressed_cache_detail for other form…
Browse files Browse the repository at this point in the history
…ats than IEEE-754 binary64
  • Loading branch information
jk-jeon committed Mar 13, 2024
1 parent d85bc4d commit 5918e0f
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions include/dragonbox/dragonbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -1722,9 +1722,12 @@ namespace jkj {
cache_holder<ieee754_binary64, Dummy>::cache[];
#endif

// Compressed cache for double
template <class Dummy = void>
struct compressed_cache_detail {
// Compressed cache.
template <class FloatFormat, class Dummy = void>
struct compressed_cache_detail;

template <class Dummy>
struct compressed_cache_detail<ieee754_binary64, Dummy> {
static constexpr int compression_ratio = 27;
static constexpr stdr::size_t compressed_table_size =
(cache_holder<ieee754_binary64>::max_k - cache_holder<ieee754_binary64>::min_k +
Expand Down Expand Up @@ -1773,11 +1776,11 @@ namespace jkj {
};
#if !JKJ_HAS_INLINE_VARIABLE
template <class Dummy>
constexpr typename compressed_cache_detail<Dummy>::cache_holder_t
compressed_cache_detail<Dummy>::cache;
constexpr typename compressed_cache_detail<ieee754_binary64, Dummy>::cache_holder_t
compressed_cache_detail<ieee754_binary64, Dummy>::cache;
template <class Dummy>
constexpr typename compressed_cache_detail<Dummy>::pow5_holder_t
compressed_cache_detail<Dummy>::pow5;
constexpr typename compressed_cache_detail<ieee754_binary64, Dummy>::pow5_holder_t
compressed_cache_detail<ieee754_binary64, Dummy>::pow5;
#endif
}

Expand Down Expand Up @@ -2323,15 +2326,16 @@ namespace jkj {
// Compute the base index.
auto const cache_index = int(
stdr::uint_least32_t(k - cache_holder<ieee754_binary64>::min_k) /
compressed_cache_detail<>::compression_ratio);
compressed_cache_detail<ieee754_binary64>::compression_ratio);
auto const kb =
cache_index * compressed_cache_detail<>::compression_ratio +
cache_index *
compressed_cache_detail<ieee754_binary64>::compression_ratio +
cache_holder<ieee754_binary64>::min_k;
auto const offset = k - kb;

// Get the base cache.
auto const base_cache =
compressed_cache_detail<>::cache.table[cache_index];
compressed_cache_detail<ieee754_binary64>::cache.table[cache_index];

if (offset == 0) {
return base_cache;
Expand All @@ -2343,7 +2347,8 @@ namespace jkj {
assert(alpha > 0 && alpha < 64);

// Try to recover the real cache.
auto const pow5 = compressed_cache_detail<>::pow5.table[offset];
auto const pow5 =
compressed_cache_detail<ieee754_binary64>::pow5.table[offset];
auto recovered_cache = wuint::umul128(base_cache.high(), pow5);
auto const middle_low = wuint::umul128(base_cache.low(), pow5);

Expand Down

0 comments on commit 5918e0f

Please sign in to comment.