Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
BasedOnStyle: LLVM
ColumnLimit: 0
SpaceAfterCStyleCast: true
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortBlocksOnASingleLine: Always
SortIncludes: Never
PointerAlignment: Left
9 changes: 9 additions & 0 deletions .github/workflows/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,12 @@ jobs:
bazel run -c opt --copt=-DRYU_OPTIMIZE_SIZE //ryu/benchmark:ryu_printf_benchmark -- -samples=200
bazel run -c opt --copt=-DRYU_OPTIMIZE_SIZE --copt=-DRYU_ONLY_64_BIT_OPS //ryu/benchmark:ryu_benchmark
bazel run -c opt --copt=-DRYU_OPTIMIZE_SIZE --copt=-DRYU_ONLY_64_BIT_OPS //ryu/benchmark:ryu_printf_benchmark -- -samples=200

clang-format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install clang-format
run: pip install clang-format==18.1.8
- name: Check formatting
run: ./scripts/check-clang-format.sh
9 changes: 4 additions & 5 deletions ryu/benchmark/benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@
#include "third_party/double-conversion/double-conversion/utils.h"
#include "third_party/double-conversion/double-conversion/double-conversion.h"

using double_conversion::StringBuilder;
using double_conversion::DoubleToStringConverter;
using double_conversion::StringBuilder;
using namespace std::chrono;

constexpr int BUFFER_SIZE = 40;
static char buffer[BUFFER_SIZE];
static DoubleToStringConverter converter(
DoubleToStringConverter::Flags::EMIT_TRAILING_DECIMAL_POINT
| DoubleToStringConverter::Flags::EMIT_TRAILING_ZERO_AFTER_POINT,
DoubleToStringConverter::Flags::EMIT_TRAILING_DECIMAL_POINT | DoubleToStringConverter::Flags::EMIT_TRAILING_ZERO_AFTER_POINT,
"Infinity",
"NaN",
'E',
Expand Down Expand Up @@ -113,7 +112,7 @@ class benchmark_options {
bool classic() const { return m_classic; }
int small_digits() const { return m_small_digits; }

void parse(const char * const arg) {
void parse(const char* const arg) {
if (strcmp(arg, "-32") == 0) {
m_run32 = true;
m_run64 = false;
Expand Down Expand Up @@ -144,7 +143,7 @@ class benchmark_options {
}

private:
void fail(const char * const arg) {
void fail(const char* const arg) {
printf("Unrecognized option '%s'.\n", arg);
exit(EXIT_FAILURE);
}
Expand Down
50 changes: 25 additions & 25 deletions ryu/benchmark/benchmark_fixed.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,27 @@ struct mean_and_variance {

typedef struct mean_and_variance mean_and_variance;

void init(mean_and_variance* mv) {
mv->n = 0;
mv->mean = 0;
mv->m2 = 0;
}
void init(mean_and_variance* mv) {
mv->n = 0;
mv->mean = 0;
mv->m2 = 0;
}

void update(mean_and_variance* mv, double x) {
++mv->n;
double d = x - mv->mean;
mv->mean += d / mv->n;
double d2 = x - mv->mean;
mv->m2 += d * d2;
}
void update(mean_and_variance* mv, double x) {
++mv->n;
double d = x - mv->mean;
mv->mean += d / mv->n;
double d2 = x - mv->mean;
mv->m2 += d * d2;
}

double variance(mean_and_variance* mv) {
return mv->m2 / (mv->n - 1);
}
double variance(mean_and_variance* mv) {
return mv->m2 / (mv->n - 1);
}

double stddev(mean_and_variance* mv) {
return sqrt(variance(mv));
}
double stddev(mean_and_variance* mv) {
return sqrt(variance(mv));
}

double generate_double(uint64_t* r) {
*r = RandomU64();
Expand All @@ -97,7 +97,7 @@ static int bench64_fixed(const uint32_t samples, const uint32_t iterations, cons
uint64_t r = 0;
const double f = generate_double(&r);

// printf("%f\n", f);
// printf("%f\n", f);
clock_t t1 = clock();
for (int j = 0; j < iterations; ++j) {
d2fixed_buffered(f, precision, bufferown);
Expand All @@ -121,7 +121,7 @@ static int bench64_fixed(const uint32_t samples, const uint32_t iterations, cons
printf("%s,%" PRIu64 ",%f,%f\n", bufferown, r, delta1, delta2);
}

// printf("For %16" PRIX64 " %28s %28s\n", r, bufferown, buffer);
// printf("For %16" PRIX64 " %28s %28s\n", r, bufferown, buffer);
if (strcmp(bufferown, buffer) != 0) {
printf("For %16" PRIX64 " %28s %28s\n", r, bufferown, buffer);
}
Expand Down Expand Up @@ -150,8 +150,8 @@ static int bench64_exp(const uint32_t samples, const uint32_t iterations, const
uint64_t r = 0;
const double f = generate_double(&r);

// printf("%.20e\n", f);
// printf("For %16" PRIX64 "\n", r);
// printf("%.20e\n", f);
// printf("For %16" PRIX64 "\n", r);
clock_t t1 = clock();
for (int j = 0; j < iterations; ++j) {
d2exp_buffered(f, precision, bufferown);
Expand All @@ -175,7 +175,7 @@ static int bench64_exp(const uint32_t samples, const uint32_t iterations, const
printf("%s,%" PRIu64 ",%f,%f\n", bufferown, r, delta1, delta2);
}

// printf("For %16" PRIX64 " %28s %28s\n", r, bufferown, buffer);
// printf("For %16" PRIX64 " %28s %28s\n", r, bufferown, buffer);
if ((strcmp(bufferown, buffer) != 0) && !verbose) {
printf("For %16" PRIX64 " %28s %28s\n", r, bufferown, buffer);
}
Expand Down Expand Up @@ -219,8 +219,8 @@ int main(int argc, char** argv) {
}
}
if (false) {
// double d = int64Bits2Double(0x426E5FDA4A181F94);
// double d = int64Bits2Double(0xC27EF2838AD07A1A);
// double d = int64Bits2Double(0x426E5FDA4A181F94);
// double d = int64Bits2Double(0xC27EF2838AD07A1A);
double d = int64Bits2Double(0x426C19FD2EFA7294);
printf("%.10f\n", 1043657281728.98681640625);
printf("%.10f\n", -2126683614471.63134765625);
Expand Down
12 changes: 6 additions & 6 deletions ryu/benchmark/benchmark_fixed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class benchmark_options {
int small_digits() const { return m_small_digits; }
int precision() const { return m_precision; }

void parse(const char * const arg) {
void parse(const char* const arg) {
if (strcmp(arg, "-f") == 0) {
m_run32 = false;
m_run64 = true;
Expand Down Expand Up @@ -115,7 +115,7 @@ class benchmark_options {
}

private:
void fail(const char * const arg) {
void fail(const char* const arg) {
printf("Unrecognized option '%s'.\n", arg);
exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -176,7 +176,7 @@ static int bench64_fixed(const benchmark_options& options) {
uint64_t r = 0;
const double f = generate_double(options, mt32, r);

// printf("%f\n", f);
// printf("%f\n", f);
auto t1 = steady_clock::now();
for (int j = 0; j < options.iterations(); ++j) {
d2fixed_buffered(f, static_cast<uint32_t>(precision), bufferown);
Expand Down Expand Up @@ -206,7 +206,7 @@ static int bench64_fixed(const benchmark_options& options) {
}
}

// printf("For %16" PRIX64 " %28s %28s\n", r, bufferown, buffer);
// printf("For %16" PRIX64 " %28s %28s\n", r, bufferown, buffer);
if (!options.ryu_only() && strcmp(bufferown, buffer) != 0) {
printf("For %16" PRIX64 " %28s %28s\n", r, bufferown, buffer);
}
Expand Down Expand Up @@ -234,7 +234,7 @@ static int bench64_exp(const benchmark_options& options) {
uint64_t r = 0;
const double f = generate_double(options, mt32, r);

// printf("%f\n", f);
// printf("%f\n", f);
auto t1 = steady_clock::now();
for (int j = 0; j < options.iterations(); ++j) {
d2exp_buffered(f, static_cast<uint32_t>(precision), bufferown);
Expand Down Expand Up @@ -264,7 +264,7 @@ static int bench64_exp(const benchmark_options& options) {
}
}

// printf("For %16" PRIX64 " %28s %28s\n", r, bufferown, buffer);
// printf("For %16" PRIX64 " %28s %28s\n", r, bufferown, buffer);
if (!options.ryu_only() && !options.verbose() && strcmp(bufferown, buffer) != 0) {
printf("For %16" PRIX64 " %28s %28s\n", r, bufferown, buffer);
}
Expand Down
2 changes: 1 addition & 1 deletion ryu/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static inline uint32_t log10Pow5(const int32_t e) {
return (((uint32_t) e) * 732923) >> 20;
}

static inline int copy_special_str(char * const result, const bool sign, const bool exponent, const bool mantissa) {
static inline int copy_special_str(char* const result, const bool sign, const bool exponent, const bool mantissa) {
if (mantissa) {
memcpy(result, "NaN", 3);
return 3;
Expand Down
50 changes: 23 additions & 27 deletions ryu/d2fixed.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,27 @@

#if defined(HAS_UINT128)
static inline uint128_t umul256(const uint128_t a, const uint64_t bHi, const uint64_t bLo, uint128_t* const productHi) {
const uint64_t aLo = (uint64_t)a;
const uint64_t aHi = (uint64_t)(a >> 64);
const uint64_t aLo = (uint64_t) a;
const uint64_t aHi = (uint64_t) (a >> 64);

const uint128_t b00 = (uint128_t)aLo * bLo;
const uint128_t b01 = (uint128_t)aLo * bHi;
const uint128_t b10 = (uint128_t)aHi * bLo;
const uint128_t b11 = (uint128_t)aHi * bHi;
const uint128_t b00 = (uint128_t) aLo * bLo;
const uint128_t b01 = (uint128_t) aLo * bHi;
const uint128_t b10 = (uint128_t) aHi * bLo;
const uint128_t b11 = (uint128_t) aHi * bHi;

const uint64_t b00Lo = (uint64_t)b00;
const uint64_t b00Hi = (uint64_t)(b00 >> 64);
const uint64_t b00Lo = (uint64_t) b00;
const uint64_t b00Hi = (uint64_t) (b00 >> 64);

const uint128_t mid1 = b10 + b00Hi;
const uint64_t mid1Lo = (uint64_t)(mid1);
const uint64_t mid1Hi = (uint64_t)(mid1 >> 64);
const uint64_t mid1Lo = (uint64_t) (mid1);
const uint64_t mid1Hi = (uint64_t) (mid1 >> 64);

const uint128_t mid2 = b01 + mid1Lo;
const uint64_t mid2Lo = (uint64_t)(mid2);
const uint64_t mid2Hi = (uint64_t)(mid2 >> 64);
const uint64_t mid2Lo = (uint64_t) (mid2);
const uint64_t mid2Hi = (uint64_t) (mid2 >> 64);

const uint128_t pHi = b11 + mid1Hi + mid2Hi;
const uint128_t pLo = ((uint128_t)mid2Lo << 64) | b00Lo;
const uint128_t pLo = ((uint128_t) mid2Lo << 64) | b00Lo;

*productHi = pHi;
return pLo;
Expand Down Expand Up @@ -121,7 +121,7 @@ static inline uint32_t mulShift_mod1e9(const uint64_t m, const uint64_t* const m
#if defined(HAS_64_BIT_INTRINSICS)
// Returns the low 64 bits of the high 128 bits of the 256-bit product of a and b.
static inline uint64_t umul256_hi128_lo64(
const uint64_t aHi, const uint64_t aLo, const uint64_t bHi, const uint64_t bLo) {
const uint64_t aHi, const uint64_t aLo, const uint64_t bHi, const uint64_t bLo) {
uint64_t b00Hi;
const uint64_t b00Lo = umul128(aLo, bLo, &b00Hi);
uint64_t b01Hi;
Expand Down Expand Up @@ -158,12 +158,12 @@ static inline uint32_t mulShift_mod1e9(const uint64_t m, const uint64_t* const m
const uint64_t low1 = umul128(m, mul[1], &high1); // 64
uint64_t high2; // 192
const uint64_t low2 = umul128(m, mul[2], &high2); // 128
const uint64_t s0low = low0; // 0
(void) s0low; // unused
const uint64_t s0high = low1 + high0; // 64
const uint64_t s0low = low0; // 0
(void) s0low; // unused
const uint64_t s0high = low1 + high0; // 64
const uint32_t c1 = s0high < low1;
const uint64_t s1low = low2 + high1 + c1; // 128
const uint32_t c2 = s1low < low2; // high1 + c1 can't overflow, so compare against low2
const uint32_t c2 = s1low < low2; // high1 + c1 can't overflow, so compare against low2
const uint64_t s1high = high2 + c2; // 192
#ifdef RYU_DEBUG
if (j < 128 || j > 180) {
Expand All @@ -177,7 +177,7 @@ static inline uint32_t mulShift_mod1e9(const uint64_t m, const uint64_t* const m
const uint64_t shiftedhigh = s1high >> dist;
const uint64_t shiftedlow = shiftright128(s1low, s1high, dist);
return uint128_mod1e9(shiftedhigh, shiftedlow);
#else // HAS_64_BIT_INTRINSICS
#else // HAS_64_BIT_INTRINSICS
if (j < 160) { // j: [128, 160)
const uint64_t r0 = mod1e9(s1high);
const uint64_t r1 = mod1e9((r0 << 32) | (s1low >> 32));
Expand Down Expand Up @@ -493,8 +493,7 @@ int d2fixed_buffered_n(double d, uint32_t precision, char* result) {
} else {
// Is m * 10^(additionalDigits + 1) / 2^(-e2) integer?
const int32_t requiredTwos = -e2 - (int32_t) precision - 1;
const bool trailingZeros = requiredTwos <= 0
|| (requiredTwos < 60 && multipleOfPowerOf2(m2, (uint32_t) requiredTwos));
const bool trailingZeros = requiredTwos <= 0 || (requiredTwos < 60 && multipleOfPowerOf2(m2, (uint32_t) requiredTwos));
roundUp = trailingZeros ? 2 : 1;
#ifdef RYU_DEBUG
printf("requiredTwos=%d\n", requiredTwos);
Expand Down Expand Up @@ -555,14 +554,12 @@ void d2fixed_buffered(double d, uint32_t precision, char* result) {
}

char* d2fixed(double d, uint32_t precision) {
char* const buffer = (char*)malloc(2000);
char* const buffer = (char*) malloc(2000);
const int index = d2fixed_buffered_n(d, precision, buffer);
buffer[index] = '\0';
return buffer;
}



int d2exp_buffered_n(double d, uint32_t precision, char* result) {
const uint64_t bits = double_to_bits(d);
#ifdef RYU_DEBUG
Expand Down Expand Up @@ -730,8 +727,7 @@ int d2exp_buffered_n(double d, uint32_t precision, char* result) {
// precision was already increased by 1, so we don't need to write + 1 here.
const int32_t rexp = (int32_t) precision - exp;
const int32_t requiredTwos = -e2 - rexp;
bool trailingZeros = requiredTwos <= 0
|| (requiredTwos < 60 && multipleOfPowerOf2(m2, (uint32_t) requiredTwos));
bool trailingZeros = requiredTwos <= 0 || (requiredTwos < 60 && multipleOfPowerOf2(m2, (uint32_t) requiredTwos));
if (rexp < 0) {
const int32_t requiredFives = -rexp;
trailingZeros = trailingZeros && multipleOfPowerOf5(m2, (uint32_t) requiredFives);
Expand Down Expand Up @@ -812,7 +808,7 @@ void d2exp_buffered(double d, uint32_t precision, char* result) {
}

char* d2exp(double d, uint32_t precision) {
char* const buffer = (char*)malloc(2000);
char* const buffer = (char*) malloc(2000);
const int index = d2exp_buffered_n(d, precision, buffer);
buffer[index] = '\0';
return buffer;
Expand Down
2 changes: 2 additions & 0 deletions ryu/d2fixed_full_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#define TABLE_SIZE 64

// clang-format off
static const uint16_t POW10_OFFSET[TABLE_SIZE] = {
0, 2, 5, 8, 12, 16, 21, 26, 32, 39,
46, 54, 62, 71, 80, 90, 100, 111, 122, 134,
Expand Down Expand Up @@ -4416,5 +4417,6 @@ static const uint64_t POW10_SPLIT_2[3133][3] = {
{ 0u, 0u, 241525390625u },
{ 0u, 0u, 33000000000u },
};
// clang-format on

#endif // RYU_D2FIXED_FULL_TABLE_H
2 changes: 1 addition & 1 deletion ryu/d2s.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ static inline int to_chars(const floating_decimal_64 v, const bool sign, char* c
}

static inline bool d2d_small_int(const uint64_t ieeeMantissa, const uint32_t ieeeExponent,
floating_decimal_64* const v) {
floating_decimal_64* const v) {
const uint64_t m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
const int32_t e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS;

Expand Down
2 changes: 2 additions & 0 deletions ryu/d2s_full_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define DOUBLE_POW5_INV_TABLE_SIZE 342
#define DOUBLE_POW5_TABLE_SIZE 326

// clang-format off
static const uint64_t DOUBLE_POW5_INV_SPLIT[DOUBLE_POW5_INV_TABLE_SIZE][2] = {
{ 1u, 2305843009213693952u }, { 11068046444225730970u, 1844674407370955161u },
{ 5165088340638674453u, 1475739525896764129u }, { 7821419487252849886u, 1180591620717411303u },
Expand Down Expand Up @@ -363,5 +364,6 @@ static const uint64_t DOUBLE_POW5_SPLIT[DOUBLE_POW5_TABLE_SIZE][2] = {
{ 507629346944023544u, 1822780504889099373u }, { 5246222702107417334u, 2278475631111374216u },
{ 3278889188817135834u, 1424047269444608885u }, { 8710297504448807696u, 1780059086805761106u }
};
// clang-format on

#endif // RYU_D2S_FULL_TABLE_H
Loading