Format an f64 per RFC 8785 · C 🤝 Gemini 4 Neon · #1 Fastest suite
SHA-256
ebe2a7a95df062c794eec8aeb7464af719973d027e1480e62f431cc5a279483d
View source (12.4 KiB of C)
#include <stdint.h>
#define INPUT_CAP 65536
#define OUTPUT_CAP 212992
static uint8_t INPUT_BUF[INPUT_CAP];
static uint8_t OUTPUT_BUF[OUTPUT_CAP + 32];
__attribute__((export_name("input_ptr")))
uint32_t input_ptr(void) {
return (uint32_t)(uintptr_t)INPUT_BUF;
}
__attribute__((export_name("input_bytes_cap")))
uint32_t input_bytes_cap(void) {
return INPUT_CAP;
}
__attribute__((export_name("output_bytes_cap")))
uint32_t output_bytes_cap(void) {
return OUTPUT_CAP;
}
static const uint64_t POW10_BIG[45][2] = {
{ 0xa37fce126597973cULL, 0xe50ff107bab528a1ULL },
{ 0xe858ad248f5c22c9ULL, 0xd1b3400f8f9cff69ULL },
{ 0xa5178fff668ae0b6ULL, 0x626e974dbe39a873ULL },
{ 0xea9c227723ee8bcbULL, 0x465e15a979c1caddULL },
{ 0xa6b34ad8c9dfc06fULL, 0xf42faa48c0ea481fULL },
{ 0xece53cec4a314ebdULL, 0xa4f8bf5635246429ULL },
{ 0xa8530886b54dbdebULL, 0xd9f57f830283fdfdULL },
{ 0xef340a98172aace4ULL, 0x86fb897116c87c35ULL },
{ 0xa9f6d30a038d1dbcULL, 0x5e9fcf4ccd211f4dULL },
{ 0xf18899b1bc3f8ca1ULL, 0xdc44e6c3cb279ac2ULL },
{ 0xab9eb47c81f5114fULL, 0x066ea92f3f326565ULL },
{ 0xf3e2f893dec3f126ULL, 0x5a89dba3c3efccfbULL },
{ 0xad4ab7112eb3929dULL, 0x86c16c98d2c953c7ULL },
{ 0xf64335bcf065d37dULL, 0x4d4617b5ff4a16d6ULL },
{ 0xaefae51477a06b03ULL, 0xede622920b6b23f2ULL },
{ 0xf8a95fcf88747d94ULL, 0x75a44c6397ce912bULL },
{ 0xb0af48ec79ace837ULL, 0x2d835a9df0c6d852ULL },
{ 0xfb158592be068d2eULL, 0xeed6e2f0f0d56713ULL },
{ 0xb267ed1940f1c61cULL, 0x55f038b237591ed4ULL },
{ 0xfd87b5f28300ca0dULL, 0x8bca9d6e188853fdULL },
{ 0xb424dc35095cd80fULL, 0x538484c19ef38c95ULL },
{ 0x8000000000000000ULL, 0x0000000000000001ULL },
{ 0xb5e620f480000000ULL, 0x0000000000000001ULL },
{ 0x813f3978f8940984ULL, 0x4000000000000001ULL },
{ 0xb7abc627050305adULL, 0xf14a3d9e40000001ULL },
{ 0x82818f1281ed449fULL, 0xbff8f10e7a8921a5ULL },
{ 0xb975d6b6ee39e436ULL, 0xb3e2fd538e122b45ULL },
{ 0x83c7088e1aab65dbULL, 0x792667c6da79e0fbULL },
{ 0xbb445da9ca61281fULL, 0x2a8a6e45ae8edc98ULL },
{ 0x850fadc09923329eULL, 0x03e2cf6bc604ddb1ULL },
{ 0xbd176620a501fbffULL, 0xb650e5a93bc3d899ULL },
{ 0x865b86925b9bc5c2ULL, 0x0b8a2392ba45a9b3ULL },
{ 0xbeeefb584aff8603ULL, 0xaafb550ffacfd8fbULL },
{ 0x87aa9aff79042286ULL, 0x90fb44d2f05d0843ULL },
{ 0xc0cb28a98fcf3c7fULL, 0x84576a1bb416a7deULL },
{ 0x88fcf317f22241e2ULL, 0x441fece3bdf81f04ULL },
{ 0xc2abf989935ddbfeULL, 0x6acff893d00ea436ULL },
{ 0x8a5296ffe33cc92fULL, 0x82bd6b70d99aaa70ULL },
{ 0xc491798a08a2ad4eULL, 0xf1a6f2bab92a27e3ULL },
{ 0x8bab8eefb6409c1aULL, 0x1ad089b6c2f7548fULL },
{ 0xc67bb4597ce2ce48ULL, 0xb143c6053edcd0d6ULL },
{ 0x8d07e33455637eb2ULL, 0xdb0b487b6423e1e9ULL },
{ 0xc86ab5c39fa63440ULL, 0x8dd9472bf3fefaa8ULL },
{ 0x8e679c2f5e44ff8fULL, 0x570f09eaa7ea7649ULL },
{ 0xca5e89b18b602368ULL, 0x385bb19cb14bdfc5ULL },
};
static const uint32_t POW10_SMALL[14] = {
0x80000000U,
0xa0000000U,
0xc8000000U,
0xfa000000U,
0x9c400000U,
0xc3500000U,
0xf4240000U,
0x98968000U,
0xbebc2000U,
0xee6b2800U,
0x9502f900U,
0xba43b740U,
0xe8d4a510U,
0x9184e72aU,
};
static const char DIGITS2[200] =
"00010203040506070809"
"10111213141516171819"
"20212223242526272829"
"30313233343536373839"
"40414243444546474849"
"50515253545556575859"
"60616263646566676869"
"70717273747576777879"
"80818283848586878889"
"90919293949596979899";
typedef struct {
uint64_t hi;
uint64_t lo;
} u128_t;
static inline u128_t umul128(uint64_t a, uint64_t b) {
uint64_t a0 = (uint32_t)a, a1 = a >> 32;
uint64_t b0 = (uint32_t)b, b1 = b >> 32;
__asm__("" : "+r"(a0));
uint64_t p00 = a0 * b0;
uint64_t p01 = a0 * b1;
uint64_t p10 = a1 * b0;
uint64_t p11 = a1 * b1;
uint64_t mid = p10 + (p00 >> 32);
uint64_t mid2 = (uint32_t)mid + p01;
u128_t r;
r.lo = (mid2 << 32) | (uint32_t)p00;
r.hi = p11 + (mid >> 32) + (mid2 >> 32);
return r;
}
static inline uint64_t div10_64(uint64_t x) {
return umul128(x, 0xCCCCCCCCCCCCCCCDULL).hi >> 3;
}
static inline uint32_t div1e8_64(uint64_t x) {
return (uint32_t)(umul128(x >> 8, 0xabcc77118461cefdULL).hi >> 18);
}
static inline void get_g(int32_t k, uint64_t *g1_out, uint64_t *g0_out) {
uint32_t m = (uint32_t)(294 - k);
uint32_t q = m / 14;
uint32_t r = m - q * 14;
uint64_t b1 = POW10_BIG[q][0];
uint64_t b0 = POW10_BIG[q][1];
if (r == 0) {
*g1_out = b1;
*g0_out = b0;
return;
}
uint64_t sr = POW10_SMALL[r];
uint64_t p0 = (uint32_t)b0 * sr;
uint64_t p1 = (b0 >> 32) * sr + (p0 >> 32);
uint64_t p2 = (uint32_t)b1 * sr + (p1 >> 32);
uint64_t p3 = (b1 >> 32) * sr + (p2 >> 32);
uint64_t h1 = p3;
uint64_t h0 = ((uint64_t)(uint32_t)p2 << 32) | (uint32_t)p1;
uint64_t g0, g1;
if (h1 >> 63) {
g0 = h0 + 1;
g1 = h1 + (g0 == 0);
} else {
uint64_t lo_bit = ((uint32_t)p0) >> 31;
uint64_t t0 = (h0 << 1) | lo_bit;
uint64_t t1 = (h1 << 1) | (h0 >> 63);
g0 = t0 + 1;
g1 = t1 + (g0 == 0);
}
*g1_out = g1;
*g0_out = g0;
}
static inline uint8_t *write_u32_pair(uint8_t *p, uint32_t v) {
const uint16_t *d = (const uint16_t *)DIGITS2;
__builtin_memcpy(p, &d[v], 2);
return p + 2;
}
static inline uint8_t *write_u32_rev(uint8_t *p, uint32_t v) {
while (v >= 100) {
uint32_t q = v / 100;
uint32_t r = v - q * 100;
v = q;
p -= 2;
write_u32_pair(p, r);
}
if (v >= 10) {
p -= 2;
write_u32_pair(p, v);
} else {
*--p = (uint8_t)(39;039; + v);
}
return p;
}
static inline void copy16(uint8_t *dst, const uint8_t *src) {
__builtin_memcpy(dst, src, 16);
}
static inline uint8_t *format_one(uint8_t *out, uint64_t bits) {
if ((bits << 1) == 0) {
out[0] = 39;039;;
out[1] = 39;\n39;;
return out + 2;
}
if (bits >> 63) {
*out++ = 39;-39;;
}
uint32_t ebits = (uint32_t)((bits >> 52) & 0x7FFU);
uint64_t mbits = bits & 0xFFFFFFFFFFFFFULL;
// Fast path for exact unsigned 32-bit integers: 1 <= v < 2^32 (ebits in [1023, 1054])
if (ebits - 1023U <= 31U) {
uint32_t shift = 1075 - ebits;
if ((mbits << (64 - shift)) == 0) {
uint32_t iv = (uint32_t)(((1ULL << 52) | mbits) >> shift);
if (iv < 10) {
out[0] = (uint8_t)(39;039; + iv);
out[1] = 39;\n39;;
return out + 2;
}
if (iv < 100) {
write_u32_pair(out, iv);
out[2] = 39;\n39;;
return out + 3;
}
uint8_t tmp[16];
uint8_t *p = write_u32_rev(tmp + 16, iv);
uint32_t len = (uint32_t)((tmp + 16) - p);
__builtin_memcpy(out, p, 8);
if (len > 8) __builtin_memcpy(out + 8, p + 8, 2);
out[len] = 39;\n39;;
return out + len + 1;
}
}
uint64_t c;
int32_t q;
if (ebits != 0) {
c = (1ULL << 52) | mbits;
q = (int32_t)ebits - 1075;
} else {
c = mbits;
q = -1074;
}
uint64_t out_bound = c & 1;
uint32_t regular = (mbits != 0 || ebits <= 1);
int32_t k = regular ? ((q * 1262611) >> 22) : ((q * 1262611 - 524031) >> 22);
int32_t h = q + ((-k * 1741647) >> 19);
uint64_t g1, g0;
get_g(k, &g1, &g0);
// Compute 192-bit product P = g * (cb << h) once, then derive P_r and P_l by add/sub
uint64_t cp = c << (h + 2);
u128_t lo = umul128(g0, cp);
u128_t hi = umul128(g1, cp);
uint64_t p0 = lo.lo;
uint64_t p1 = hi.lo + lo.hi;
uint64_t p2 = hi.hi + (p1 < lo.hi);
uint64_t vb = (p2 << 1) | (uint64_t)(p1 != 0);
// P_r = P + (g << (h + 1))
uint32_t sr = (uint32_t)(h + 1);
uint64_t dr0 = g0 << sr;
uint64_t dr1 = (g1 << sr) | (g0 >> (64 - sr));
uint64_t dr2 = g1 >> (64 - sr);
uint64_t pr0 = p0 + dr0;
uint64_t cr0 = (pr0 < p0);
uint64_t pr1a = p1 + dr1;
uint64_t cr1 = (pr1a < p1);
uint64_t pr1 = pr1a + cr0;
cr1 += (pr1 < cr0);
uint64_t pr2 = p2 + dr2 + cr1;
uint64_t vbr = (pr2 << 1) | (uint64_t)(pr1 != 0);
// P_l = P - (g << sl), where sl = h + 1 (regular) or h (irregular)
uint64_t dl0, dl1, dl2;
if (regular) {
dl0 = dr0;
dl1 = dr1;
dl2 = dr2;
} else if (h == 0) {
dl0 = g0;
dl1 = g1;
dl2 = 0;
} else {
uint32_t sl = (uint32_t)h;
dl0 = g0 << sl;
dl1 = (g1 << sl) | (g0 >> (64 - sl));
dl2 = g1 >> (64 - sl);
}
uint64_t br0 = (p0 < dl0);
uint64_t pl1a = p1 - dl1;
uint64_t br1 = (p1 < dl1) + (pl1a < br0);
uint64_t pl1 = pl1a - br0;
uint64_t pl2 = p2 - dl2 - br1;
uint64_t vbl = (pl2 << 1) | (uint64_t)(pl1 != 0);
uint64_t lower = vbl + out_bound;
uint64_t upper = vbr - out_bound;
uint64_t s = vb >> 2;
uint64_t sig;
int32_t exp10;
if (s >= 10) {
uint64_t sp = div10_64(s);
uint64_t up_in = lower <= 40 * sp;
uint64_t wp_in = 40 * sp + 40 <= upper;
if (up_in != wp_in) {
sig = sp + wp_in;
exp10 = k + 1;
goto format_digits;
}
}
{
uint64_t u_in = lower <= 4 * s;
uint64_t w_in = 4 * s + 4 <= upper;
if (u_in != w_in) {
sig = s + w_in;
exp10 = k;
} else {
uint64_t mid = 4 * s + 2;
uint64_t round_up = (vb > mid) | ((vb == mid) & (s & 1));
sig = s + round_up;
exp10 = k;
}
}
format_digits: ;
// Extract digits backwards into buf[0..23] (padded for 16-byte copy)
uint8_t buf[36];
uint8_t *end = buf + 20;
uint8_t *start = end;
if (sig < 100000000ULL) {
start = write_u32_rev(start, (uint32_t)sig);
} else {
uint32_t hi32 = div1e8_64(sig);
uint32_t lo32 = (uint32_t)(sig - (uint64_t)hi32 * 100000000ULL);
if (lo32 == 0) {
exp10 += 8;
} else {
uint32_t a = lo32 / 10000;
uint32_t b = lo32 - a * 10000;
uint32_t b1 = b / 100, b2 = b - b1 * 100;
uint32_t a1 = a / 100, a2 = a - a1 * 100;
start -= 8;
write_u32_pair(start + 0, a1);
write_u32_pair(start + 2, a2);
write_u32_pair(start + 4, b1);
write_u32_pair(start + 6, b2);
}
start = write_u32_rev(start, hi32);
}
// Strip trailing zeros from end
while (end > start + 1 && end[-1] == 39;039;) {
end--;
exp10++;
}
int32_t len = (int32_t)(end - start);
int32_t n = exp10 + len;
if (len <= n && n <= 21) {
copy16(out, start);
if (len > 16) out[16] = start[16];
out += len;
int32_t z = n - len;
while (z--) *out++ = 39;039;;
} else if (0 < n && n < len) {
copy16(out, start);
out[n] = 39;.39;;
copy16(out + n + 1, start + n);
out += len + 1;
} else if (-6 < n && n <= 0) {
out[0] = 39;039;;
out[1] = 39;.39;;
out += 2;
int32_t z = -n;
while (z--) *out++ = 39;039;;
copy16(out, start);
if (len > 16) out[16] = start[16];
out += len;
} else {
out[0] = start[0];
if (len > 1) {
out[1] = 39;.39;;
copy16(out + 2, start + 1);
out += len + 1;
} else {
out += 1;
}
*out++ = 39;e39;;
int32_t e = n - 1;
if (e >= 0) {
*out++ = 39;+39;;
} else {
*out++ = 39;-39;;
e = -e;
}
if (e >= 100) {
uint32_t d0 = (uint32_t)e / 100;
uint32_t rem = (uint32_t)e - d0 * 100;
*out++ = (uint8_t)(39;039; + d0);
out = write_u32_pair(out, rem);
} else if (e >= 10) {
out = write_u32_pair(out, (uint32_t)e);
} else {
*out++ = (uint8_t)(39;039; + e);
}
}
*out++ = 39;\n39;;
return out;
}
__attribute__((export_name("render")))
uint64_t render(uint32_t size) {
uint8_t *out = OUTPUT_BUF;
uint32_t count = size >> 3;
const uint64_t *in = (const uint64_t *)INPUT_BUF;
for (uint32_t i = 0; i < count; i++) {
out = format_one(out, in[i]);
}
uint32_t out_len = (uint32_t)(out - OUTPUT_BUF);
uint32_t out_ptr = (uint32_t)(uintptr_t)OUTPUT_BUF;
return ((uint64_t)out_ptr << 32) | (uint64_t)out_len;
}
Download source MIT licensed — build on it
Schubfach binary64 formatter with a base-14 compressed power-of-10 table (776 bytes), single-product 192-bit boundary derivation, exact uint32 integer fast path, and SIMD128 16-byte digit copies.
Build details
Compiler, size, memory and checksum
- Submitted
- 1 December 2026 at 11:27 UTC
- Language
- C
- Compiler
-
Homebrew clang 23.1.6--target=wasm32 -O3 -msimd128 -nostdlib -Wl,--no-entry,--export-dynamic,--strip-all,--initial-memory=327680,--max-memory=327680,-z,stack-size=4096 - Size
- 3.6 KiB, gzip 2.5 KiB
- Download
- ~150 ms on Slow 4G (Lighthouse’s 1.6 Mbps, 150 ms round trips)
- Memory
- 5 pages (320 KiB)
- SIMD
- yes
- AI models
- Gemini 4 Neon
Functions, loops, branches and other static counts
| Count | Value |
|---|---|
| Functions | 4 |
| Instructions | 1,313 |
| Loops | 5 |
| Conditional branches | 43 |
| Indirect calls | 0 |
| Memory loads | 22 |
| Memory stores | 35 |
| SIMD instructions | 10 |
| Potentially trapping instructions | 77 |
Instruction sites in the binary, not executions, counted by qip.dev’s wasm-counts.