aboutsummaryrefslogtreecommitdiff
path: root/lib/std/http
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-11-04 14:24:59 -0400
committerGitHub <noreply@github.com>2023-11-04 14:24:59 -0400
commita7d8cd591c47536b0a1e359bf3b1806fc057ffe9 (patch)
tree069ca321e822ea95d7265ece0b79173832a0745a /lib/std/http
parentf6de3ec963e3a7d96cd4f6c72b0f076f0437c45d (diff)
parent095c4294aa8b275da0627adefad046923fcaae46 (diff)
downloadzig-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')
-rw-r--r--lib/std/http/Client.zig26
-rw-r--r--lib/std/http/Server.zig2
-rw-r--r--lib/std/http/protocol.zig35
3 files changed, 39 insertions, 24 deletions
diff --git a/lib/std/http/Client.zig b/lib/std/http/Client.zig
index e8e0363c2e..eb9896d40a 100644
--- a/lib/std/http/Client.zig
+++ b/lib/std/http/Client.zig
@@ -9,6 +9,7 @@ const net = std.net;
const Uri = std.Uri;
const Allocator = mem.Allocator;
const assert = std.debug.assert;
+const use_vectors = builtin.zig_backend != .stage2_x86_64;
const Client = @This();
const proto = @import("protocol.zig");
@@ -408,7 +409,7 @@ pub const Response = struct {
else => return error.HttpHeadersInvalid,
};
if (first_line[8] != ' ') return error.HttpHeadersInvalid;
- const status = @as(http.Status, @enumFromInt(parseInt3(first_line[9..12].*)));
+ const status: http.Status = @enumFromInt(parseInt3(first_line[9..12]));
const reason = mem.trimLeft(u8, first_line[12..], " ");
res.version = version;
@@ -481,20 +482,24 @@ pub const Response = struct {
}
inline fn int64(array: *const [8]u8) u64 {
- return @as(u64, @bitCast(array.*));
+ return @bitCast(array.*);
}
- fn parseInt3(nnn: @Vector(3, u8)) u10 {
- const zero: @Vector(3, u8) = .{ '0', '0', '0' };
- const mmm: @Vector(3, u10) = .{ 100, 10, 1 };
- return @reduce(.Add, @as(@Vector(3, u10), nnn -% zero) *% mmm);
+ fn parseInt3(text: *const [3]u8) u10 {
+ if (use_vectors) {
+ const nnn: @Vector(3, u8) = text.*;
+ const zero: @Vector(3, u8) = .{ '0', '0', '0' };
+ const mmm: @Vector(3, u10) = .{ 100, 10, 1 };
+ return @reduce(.Add, @as(@Vector(3, u10), nnn -% zero) *% mmm);
+ }
+ return std.fmt.parseInt(u10, text, 10) catch unreachable;
}
test parseInt3 {
const expectEqual = testing.expectEqual;
- try expectEqual(@as(u10, 0), parseInt3("000".*));
- try expectEqual(@as(u10, 418), parseInt3("418".*));
- try expectEqual(@as(u10, 999), parseInt3("999".*));
+ try expectEqual(@as(u10, 0), parseInt3("000"));
+ try expectEqual(@as(u10, 418), parseInt3("418"));
+ try expectEqual(@as(u10, 999), parseInt3("999"));
}
version: http.Version,
@@ -1588,7 +1593,8 @@ test {
if (builtin.os.tag == .wasi) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_x86_64 and
+ !comptime std.Target.x86.featureSetHas(builtin.cpu.features, .avx)) return error.SkipZigTest;
std.testing.refAllDecls(@This());
}
diff --git a/lib/std/http/Server.zig b/lib/std/http/Server.zig
index 055d16eb8a..6928606b1b 100644
--- a/lib/std/http/Server.zig
+++ b/lib/std/http/Server.zig
@@ -736,8 +736,6 @@ test "HTTP server handles a chunked transfer coding request" {
return error.SkipZigTest;
}
- if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
-
const native_endian = comptime builtin.cpu.arch.endian();
if (builtin.zig_backend == .stage2_llvm and native_endian == .big) {
// https://github.com/ziglang/zig/issues/13782
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]) {