Skip to content

Commit d295879

Browse files
committed
Add step 1 (extract bits from IEEE representation)
Some solvers, e.g., dragon4, assume the sign, mantissa and exponent are known before the algorithm is called.
1 parent 66b31a2 commit d295879

File tree

5 files changed

+54
-11
lines changed

5 files changed

+54
-11
lines changed

benchmarks/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
add_executable(benchmark
22
benchmark.cpp
3-
decimalToString.cpp
3+
ieeeToString.cpp
44
)
55

66
include(CheckSourceCompiles)

benchmarks/benchmark.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
#include "grisu2.h"
3131
#include "random_generators.h"
32-
#include "decimalToString.h"
32+
#include "ieeeToString.h"
3333

3434
#include <charconv>
3535
#include <climits>

benchmarks/decimalToString.h

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1-
#include "decimalToString.h"
1+
#include "ieeeToString.h"
22

33
#include <cassert>
44
#include <cstring>
55

66
#include "ryu/digit_table.h"
77

8+
IEEE754f decode_ieee754(float f) {
9+
const uint32_t& bits = reinterpret_cast<const uint32_t&>(f);
10+
11+
IEEE754f decomposed;
12+
decomposed.exponent = (bits >> FloatMantissaBits) & ((1 << FloatExponentBits) - 1);
13+
decomposed.mantissa = bits & ((1 << FloatMantissaBits) - 1);
14+
decomposed.sign = bits >> 31;
15+
return decomposed;
16+
}
17+
18+
IEEE754d decode_ieee754(double f) {
19+
const uint64_t& bits = reinterpret_cast<const uint64_t&>(f);
20+
21+
IEEE754d decomposed;
22+
decomposed.exponent = (bits >> DoubleMantissaBits) & ((1ull << DoubleExponentBits) - 1);
23+
decomposed.mantissa = bits & ((1ull << DoubleMantissaBits) - 1);
24+
decomposed.sign = bits >> 63;
25+
return decomposed;
26+
}
27+
828
// Extracted from the Ryu implementation.
929
static inline uint32_t decimalLength17(const uint64_t v) {
1030
// Function precondition: v is not an 18, 19, or 20-digit number.

benchmarks/ieeeToString.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef IEEETOSTRING_H
2+
#define IEEETOSTRING_H
3+
4+
#include <cstdint>
5+
6+
constexpr uint8_t FloatMantissaBits = 23;
7+
constexpr uint8_t FloatExponentBits = 8;
8+
9+
constexpr uint8_t DoubleMantissaBits = 52;
10+
constexpr uint8_t DoubleExponentBits = 11;
11+
12+
// Step 1: extract the sign, mantissa and exponent from an IEEE 754 number
13+
struct IEEE754f {
14+
uint32_t mantissa;
15+
uint32_t exponent;
16+
bool sign;
17+
};
18+
19+
struct IEEE754d {
20+
uint64_t mantissa;
21+
uint32_t exponent;
22+
bool sign;
23+
};
24+
25+
IEEE754f decode_ieee754(float f);
26+
IEEE754d decode_ieee754(double f);
27+
28+
// Step 3: convert a decimal exponent and mantissa to a string representation
29+
int to_chars(uint64_t mantissa, int32_t exponent, bool sign, char* const result);
30+
31+
#endif

0 commit comments

Comments
 (0)