mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-04-28 19:47:08 +03:00
2085a4af56
The previous float-parsing method was lacking in a lot of areas. This commit introduces a state-of-the art implementation that is both accurate and fast to std. Code is derived from working repo https://github.com/tiehuis/zig-parsefloat. This includes more test-cases and performance numbers that are present in this commit. * Accuracy The primary testing regime has been using test-data found at https://github.com/tiehuis/parse-number-fxx-test-data. This is a fork of upstream with support for f128 test-cases added. This data has been verified against other independent implementations and represents accurate round-to-even IEEE-754 floating point semantics. * Performance Compared to the existing parseFloat implementation there is ~5-10x performance improvement using the above corpus. (f128 parsing excluded in below measurements). ** Old $ time ./test_all_fxx_data 3520298/5296694 succeeded (1776396 fail) ________________________________________________________ Executed in 28.68 secs fish external usr time 28.48 secs 0.00 micros 28.48 secs sys time 0.08 secs 694.00 micros 0.08 secs ** This Implementation $ time ./test_all_fxx_data 5296693/5296694 succeeded (1 fail) ________________________________________________________ Executed in 4.54 secs fish external usr time 4.37 secs 515.00 micros 4.37 secs sys time 0.10 secs 171.00 micros 0.10 secs Further performance numbers can be seen using the https://github.com/tiehuis/simple_fastfloat_benchmark/ repository, which compares against some other well-known string-to-float conversion functions. A breakdown can be found here: https://github.com/tiehuis/zig-parsefloat/blob/0d9f020f1a37ca88bf889703b397c1c41779f090/PERFORMANCE.md#commit-b15406a0d2e18b50a4b62fceb5a6a3bb60ca5706 In summary, we are within 20% of the C++ reference implementation and have about ~600-700MB/s throughput on a Intel I5-6500 3.5Ghz. * F128 Support Finally, f128 is now completely supported with full accuracy. This does use a slower path which is possible to improve in future. * Behavioural Changes There are a few behavioural changes to note. - `parseHexFloat` is now redundant and these are now supported directly in `parseFloat`. - We implement round-to-even in all parsing routines. This is as specified by IEEE-754. Previous code used different rounding mechanisms (standard was round-to-zero, hex-parsing looked to use round-up) so there may be subtle differences. Closes #2207. Fixes #11169.
138 lines
3.3 KiB
Zig
138 lines
3.3 KiB
Zig
//! A wrapper over a byte-slice, providing useful methods for parsing string floating point values.
|
|
|
|
const std = @import("std");
|
|
const FloatStream = @This();
|
|
const common = @import("common.zig");
|
|
|
|
slice: []const u8,
|
|
offset: usize,
|
|
underscore_count: usize,
|
|
|
|
pub fn init(s: []const u8) FloatStream {
|
|
return .{ .slice = s, .offset = 0, .underscore_count = 0 };
|
|
}
|
|
|
|
// Returns the offset from the start *excluding* any underscores that were found.
|
|
pub fn offsetTrue(self: FloatStream) usize {
|
|
return self.offset - self.underscore_count;
|
|
}
|
|
|
|
pub fn reset(self: *FloatStream) void {
|
|
self.offset = 0;
|
|
self.underscore_count = 0;
|
|
}
|
|
|
|
pub fn len(self: FloatStream) usize {
|
|
if (self.offset > self.slice.len) {
|
|
return 0;
|
|
}
|
|
return self.slice.len - self.offset;
|
|
}
|
|
|
|
pub fn hasLen(self: FloatStream, n: usize) bool {
|
|
return self.offset + n <= self.slice.len;
|
|
}
|
|
|
|
pub fn firstUnchecked(self: FloatStream) u8 {
|
|
return self.slice[self.offset];
|
|
}
|
|
|
|
pub fn first(self: FloatStream) ?u8 {
|
|
return if (self.hasLen(1))
|
|
return self.firstUnchecked()
|
|
else
|
|
null;
|
|
}
|
|
|
|
pub fn isEmpty(self: FloatStream) bool {
|
|
return !self.hasLen(1);
|
|
}
|
|
|
|
pub fn firstIs(self: FloatStream, c: u8) bool {
|
|
if (self.first()) |ok| {
|
|
return ok == c;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
pub fn firstIsLower(self: FloatStream, c: u8) bool {
|
|
if (self.first()) |ok| {
|
|
return ok | 0x20 == c;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
pub fn firstIs2(self: FloatStream, c1: u8, c2: u8) bool {
|
|
if (self.first()) |ok| {
|
|
return ok == c1 or ok == c2;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
pub fn firstIs3(self: FloatStream, c1: u8, c2: u8, c3: u8) bool {
|
|
if (self.first()) |ok| {
|
|
return ok == c1 or ok == c2 or ok == c3;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
pub fn firstIsDigit(self: FloatStream, comptime base: u8) bool {
|
|
comptime std.debug.assert(base == 10 or base == 16);
|
|
|
|
if (self.first()) |ok| {
|
|
return common.isDigit(ok, base);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
pub fn advance(self: *FloatStream, n: usize) void {
|
|
self.offset += n;
|
|
}
|
|
|
|
pub fn skipChars(self: *FloatStream, c: u8) void {
|
|
while (self.firstIs(c)) : (self.advance(1)) {}
|
|
}
|
|
|
|
pub fn skipChars2(self: *FloatStream, c1: u8, c2: u8) void {
|
|
while (self.firstIs2(c1, c2)) : (self.advance(1)) {}
|
|
}
|
|
|
|
pub fn readU64Unchecked(self: FloatStream) u64 {
|
|
return std.mem.readIntSliceLittle(u64, self.slice[self.offset..]);
|
|
}
|
|
|
|
pub fn readU64(self: FloatStream) ?u64 {
|
|
if (self.hasLen(8)) {
|
|
return self.readU64Unchecked();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
pub fn atUnchecked(self: *FloatStream, i: usize) u8 {
|
|
return self.slice[self.offset + i];
|
|
}
|
|
|
|
pub fn scanDigit(self: *FloatStream, comptime base: u8) ?u8 {
|
|
comptime std.debug.assert(base == 10 or base == 16);
|
|
|
|
retry: while (true) {
|
|
if (self.first()) |ok| {
|
|
if ('0' <= ok and ok <= '9') {
|
|
self.advance(1);
|
|
return ok - '0';
|
|
} else if (base == 16 and 'a' <= ok and ok <= 'f') {
|
|
self.advance(1);
|
|
return ok - 'a' + 10;
|
|
} else if (base == 16 and 'A' <= ok and ok <= 'F') {
|
|
self.advance(1);
|
|
return ok - 'A' + 10;
|
|
} else if (ok == '_') {
|
|
self.advance(1);
|
|
self.underscore_count += 1;
|
|
continue :retry;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|