diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-11-04 14:24:59 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-04 14:24:59 -0400 |
| commit | a7d8cd591c47536b0a1e359bf3b1806fc057ffe9 (patch) | |
| tree | 069ca321e822ea95d7265ece0b79173832a0745a /lib/std/http/protocol.zig | |
| parent | f6de3ec963e3a7d96cd4f6c72b0f076f0437c45d (diff) | |
| parent | 095c4294aa8b275da0627adefad046923fcaae46 (diff) | |
| download | zig-a7d8cd591c47536b0a1e359bf3b1806fc057ffe9.tar.gz zig-a7d8cd591c47536b0a1e359bf3b1806fc057ffe9.zip | |
Merge pull request #17788 from jacobly0/x86_64
x86_64: pass more tests
Diffstat (limited to 'lib/std/http/protocol.zig')
| -rw-r--r-- | lib/std/http/protocol.zig | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/lib/std/http/protocol.zig b/lib/std/http/protocol.zig index 8e458ed09c..4fe9c80380 100644 --- a/lib/std/http/protocol.zig +++ b/lib/std/http/protocol.zig @@ -1,8 +1,10 @@ const std = @import("../std.zig"); +const builtin = @import("builtin"); const testing = std.testing; const mem = std.mem; const assert = std.debug.assert; +const use_vectors = builtin.zig_backend != .stage2_x86_64; pub const State = enum { /// Begin header parsing states. @@ -83,7 +85,7 @@ pub const HeadersParser = struct { /// first byte of content is located at `bytes[result]`. pub fn findHeadersEnd(r: *HeadersParser, bytes: []const u8) u32 { const vector_len: comptime_int = @max(std.simd.suggestVectorSize(u8) orelse 1, 8); - const len = @as(u32, @intCast(bytes.len)); + const len: u32 = @intCast(bytes.len); var index: u32 = 0; while (true) { @@ -175,18 +177,27 @@ pub const HeadersParser = struct { continue; }, else => { - const Vector = @Vector(vector_len, u8); - // const BoolVector = @Vector(vector_len, bool); - const BitVector = @Vector(vector_len, u1); - const SizeVector = @Vector(vector_len, u8); - const chunk = bytes[index..][0..vector_len]; - const v: Vector = chunk.*; - const matches_r = @as(BitVector, @bitCast(v == @as(Vector, @splat('\r')))); - const matches_n = @as(BitVector, @bitCast(v == @as(Vector, @splat('\n')))); - const matches_or: SizeVector = matches_r | matches_n; - - const matches = @reduce(.Add, matches_or); + const matches = if (use_vectors) matches: { + const Vector = @Vector(vector_len, u8); + // const BoolVector = @Vector(vector_len, bool); + const BitVector = @Vector(vector_len, u1); + const SizeVector = @Vector(vector_len, u8); + + const v: Vector = chunk.*; + const matches_r: BitVector = @bitCast(v == @as(Vector, @splat('\r'))); + const matches_n: BitVector = @bitCast(v == @as(Vector, @splat('\n'))); + const matches_or: SizeVector = matches_r | matches_n; + + break :matches @reduce(.Add, matches_or); + } else matches: { + var matches: u8 = 0; + for (chunk) |byte| switch (byte) { + '\r', '\n' => matches += 1, + else => {}, + }; + break :matches matches; + }; switch (matches) { 0 => {}, 1 => switch (chunk[vector_len - 1]) { |
