Format an f64 per RFC 8785 · Zig 🤝 GPT-6 · #2 Fastest suite
SHA-256
23099f20015c4a9ee82b1535e62a63f1bdb4ae8b703f78d89f031a4ce069ea32
View source (2.8 KiB of Zig)
// SPDX-License-Identifier: MIT
// RFC 8785 layout over Zig 0.14.1's standard-library Ryu conversion.
// The standard-library implementation is MIT licensed by the Zig contributors.
const std = @import("std");
var input: [65536]u8 align(8) = undefined;
var output: [212992]u8 = undefined;
export fn input_ptr() u32 { return @intFromPtr(&input); }
export fn input_bytes_cap() u32 { return input.len; }
export fn output_bytes_cap() u32 { return output.len; }
fn format(dst: []u8, bits: u64) usize {
if (bits & 0x7fffffffffffffff == 0) {
dst[0] = 39;039;;
return 1;
}
var buf: [53]u8 = undefined;
const s = std.fmt.formatFloat(&buf, @as(f64, @bitCast(bits)), .{}) catch unreachable;
var pos: usize = 0;
var i: usize = 0;
if (s[0] == 39;-39;) { dst[0] = 39;-39;; pos = 1; i = 1; }
var digits: [17]u8 = undefined;
var k: usize = 0;
while (s[i] != 39;e39;) : (i += 1) {
if (s[i] != 39;.39;) { digits[k] = s[i]; k += 1; }
}
i += 1;
const neg = s[i] == 39;-39;;
if (neg) i += 1;
var exp: i32 = 0;
while (i < s.len) : (i += 1) exp = exp * 10 + s[i] - 39;039;;
if (neg) exp = -exp;
const n = exp + 1;
if (n > 0 and n <= 21) {
const split: usize = @intCast(n);
if (k <= split) {
@memcpy(dst[pos..][0..k], digits[0..k]); pos += k;
@memset(dst[pos..][0..split-k], 39;039;); pos += split-k;
} else {
@memcpy(dst[pos..][0..split], digits[0..split]); pos += split;
dst[pos] = 39;.39;; pos += 1;
@memcpy(dst[pos..][0..k-split], digits[split..k]); pos += k-split;
}
} else if (n > -6 and n <= 0) {
dst[pos] = 39;039;; dst[pos+1] = 39;.39;; pos += 2;
const zeros: usize = @intCast(-n);
@memset(dst[pos..][0..zeros], 39;039;); pos += zeros;
@memcpy(dst[pos..][0..k], digits[0..k]); pos += k;
} else {
dst[pos] = digits[0]; pos += 1;
if (k > 1) {
dst[pos] = 39;.39;; pos += 1;
@memcpy(dst[pos..][0..k-1], digits[1..k]); pos += k-1;
}
dst[pos] = 39;e39;; dst[pos+1] = if (neg) 39;-39; else 39;+39;; pos += 2;
var exponent: u32 = @intCast(if (exp < 0) -exp else exp);
if (exponent >= 100) { dst[pos] = 39;039; + @as(u8, @intCast(exponent/100)); pos += 1; exponent %= 100; }
if (exp >= 100 or exp <= -100 or exponent >= 10) {
dst[pos] = 39;039; + @as(u8, @intCast(exponent/10)); pos += 1;
}
dst[pos] = 39;039; + @as(u8, @intCast(exponent%10)); pos += 1;
}
return pos;
}
export fn render(size: u32) u64 {
var pos: usize = 0;
var i: usize = 0;
while (i < size) : (i += 8) {
const bits = std.mem.readInt(u64, input[i..][0..8], .little);
pos += format(output[pos..], bits);
output[pos] = 39;\n39;; pos += 1;
}
return (@as(u64, @intFromPtr(&output)) << 32) | @as(u64, pos);
}
Download source MIT licensed — build on it
Ryu shortest round-trip conversion with ECMAScript/RFC 8785 decimal layout. Passed all 2,018 official compliance cases and 1,011,807 additional finite bit patterns against V8 Number.toString.
- Builds on
- Uses Zig 0.14.1 standard library floating-point formatting (Ryu algorithm), MIT licensed by the Zig contributors.
Build details
Compiler, size, memory and checksum
- Submitted
- 26 November 2026 at 12:35 UTC
- Language
- Zig
- Compiler
-
zig 0.14.1-target wasm32-freestanding -O ReleaseFast -fno-entry -rdynamic -fstrip --stack 16384 --initial-memory=393216 --max-memory=393216 - Size
- 17.9 KiB, gzip 13.8 KiB
- Download
- ~150 ms on Slow 4G (Lighthouse’s 1.6 Mbps, 150 ms round trips)
- Memory
- 6 pages (384 KiB)
- SIMD
- no
- AI models
- gpt-6
Functions, loops, branches and other static counts
| Count | Value |
|---|---|
| Functions | 5 |
| Instructions | 3,734 |
| Loops | 17 |
| Conditional branches | 158 |
| Indirect calls | 0 |
| Memory loads | 70 |
| Memory stores | 83 |
| SIMD instructions | 0 |
| Potentially trapping instructions | 257 |
Instruction sites in the binary, not executions, counted by qip.dev’s wasm-counts.