Skip to content

Commit 1f6f8b0

Browse files
committed
x86_64: implement integer @reduce(.Add)
1 parent d69f4c4 commit 1f6f8b0

File tree

16 files changed

+3089
-113
lines changed

16 files changed

+3089
-113
lines changed

lib/std/crypto/Certificate.zig

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ const Date = struct {
607607
while (month < date.month) : (month += 1) {
608608
const days: u64 = std.time.epoch.getDaysInMonth(
609609
date.year,
610-
@as(std.time.epoch.Month, @enumFromInt(month)),
610+
@enumFromInt(month),
611611
);
612612
sec += days * std.time.epoch.secs_per_day;
613613
}
@@ -623,15 +623,13 @@ const Date = struct {
623623
};
624624

625625
pub fn parseTimeDigits(text: *const [2]u8, min: u8, max: u8) !u8 {
626-
const result = if (use_vectors) result: {
627-
const nn: @Vector(2, u16) = .{ text[0], text[1] };
628-
const zero: @Vector(2, u16) = .{ '0', '0' };
629-
const mm: @Vector(2, u16) = .{ 10, 1 };
630-
break :result @reduce(.Add, (nn -% zero) *% mm);
631-
} else std.fmt.parseInt(u8, text, 10) catch return error.CertificateTimeInvalid;
626+
const nn: @Vector(2, u16) = .{ text[0], text[1] };
627+
const zero: @Vector(2, u16) = .{ '0', '0' };
628+
const mm: @Vector(2, u16) = .{ 10, 1 };
629+
const result = @reduce(.Add, (nn -% zero) *% mm);
632630
if (result < min) return error.CertificateTimeInvalid;
633631
if (result > max) return error.CertificateTimeInvalid;
634-
return @truncate(result);
632+
return @intCast(result);
635633
}
636634

637635
test parseTimeDigits {
@@ -647,14 +645,12 @@ test parseTimeDigits {
647645
}
648646

649647
pub fn parseYear4(text: *const [4]u8) !u16 {
650-
const result = if (use_vectors) result: {
651-
const nnnn: @Vector(4, u32) = .{ text[0], text[1], text[2], text[3] };
652-
const zero: @Vector(4, u32) = .{ '0', '0', '0', '0' };
653-
const mmmm: @Vector(4, u32) = .{ 1000, 100, 10, 1 };
654-
break :result @reduce(.Add, (nnnn -% zero) *% mmmm);
655-
} else std.fmt.parseInt(u16, text, 10) catch return error.CertificateTimeInvalid;
648+
const nnnn: @Vector(4, u32) = .{ text[0], text[1], text[2], text[3] };
649+
const zero: @Vector(4, u32) = .{ '0', '0', '0', '0' };
650+
const mmmm: @Vector(4, u32) = .{ 1000, 100, 10, 1 };
651+
const result = @reduce(.Add, (nnnn -% zero) *% mmmm);
656652
if (result > 9999) return error.CertificateTimeInvalid;
657-
return @truncate(result);
653+
return @intCast(result);
658654
}
659655

660656
test parseYear4 {
@@ -858,7 +854,7 @@ pub const der = struct {
858854

859855
pub fn parse(bytes: []const u8, index: u32) Element.ParseError!Element {
860856
var i = index;
861-
const identifier = @as(Identifier, @bitCast(bytes[i]));
857+
const identifier: Identifier = @bitCast(bytes[i]);
862858
i += 1;
863859
const size_byte = bytes[i];
864860
i += 1;
@@ -872,7 +868,7 @@ pub const der = struct {
872868
};
873869
}
874870

875-
const len_size = @as(u7, @truncate(size_byte));
871+
const len_size: u7 = @truncate(size_byte);
876872
if (len_size > @sizeOf(u32)) {
877873
return error.CertificateFieldHasInvalidLength;
878874
}
@@ -1244,5 +1240,3 @@ pub const rsa = struct {
12441240
return res;
12451241
}
12461242
};
1247-
1248-
const use_vectors = @import("builtin").zig_backend != .stage2_x86_64;

lib/std/http/Client.zig

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const net = std.net;
1313
const Uri = std.Uri;
1414
const Allocator = mem.Allocator;
1515
const assert = std.debug.assert;
16-
const use_vectors = builtin.zig_backend != .stage2_x86_64;
1716

1817
const Client = @This();
1918
const proto = @import("protocol.zig");
@@ -594,13 +593,10 @@ pub const Response = struct {
594593
}
595594

596595
fn parseInt3(text: *const [3]u8) u10 {
597-
if (use_vectors) {
598-
const nnn: @Vector(3, u8) = text.*;
599-
const zero: @Vector(3, u8) = .{ '0', '0', '0' };
600-
const mmm: @Vector(3, u10) = .{ 100, 10, 1 };
601-
return @reduce(.Add, @as(@Vector(3, u10), nnn -% zero) *% mmm);
602-
}
603-
return std.fmt.parseInt(u10, text, 10) catch unreachable;
596+
const nnn: @Vector(3, u8) = text.*;
597+
const zero: @Vector(3, u8) = .{ '0', '0', '0' };
598+
const mmm: @Vector(3, u10) = .{ 100, 10, 1 };
599+
return @reduce(.Add, (nnn -% zero) *% mmm);
604600
}
605601

606602
test parseInt3 {
@@ -1796,5 +1792,6 @@ pub fn fetch(client: *Client, options: FetchOptions) !FetchResult {
17961792
}
17971793

17981794
test {
1795+
_ = Response;
17991796
_ = &initDefaultProxies;
18001797
}

lib/std/http/HeadParser.zig

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,21 @@ pub fn feed(p: *HeadParser, bytes: []const u8) usize {
109109
continue;
110110
},
111111
else => {
112+
const Vector = @Vector(vector_len, u8);
113+
// const BoolVector = @Vector(vector_len, bool);
114+
const BitVector = @Vector(vector_len, u1);
115+
const SizeVector = @Vector(vector_len, u8);
116+
112117
const chunk = bytes[index..][0..vector_len];
113-
const matches = if (use_vectors) matches: {
114-
const Vector = @Vector(vector_len, u8);
115-
// const BoolVector = @Vector(vector_len, bool);
116-
const BitVector = @Vector(vector_len, u1);
117-
const SizeVector = @Vector(vector_len, u8);
118-
119-
const v: Vector = chunk.*;
120-
const matches_r: BitVector = @bitCast(v == @as(Vector, @splat('\r')));
121-
const matches_n: BitVector = @bitCast(v == @as(Vector, @splat('\n')));
122-
const matches_or: SizeVector = matches_r | matches_n;
123-
124-
break :matches @reduce(.Add, matches_or);
125-
} else matches: {
126-
var matches: u8 = 0;
127-
for (chunk) |byte| switch (byte) {
128-
'\r', '\n' => matches += 1,
129-
else => {},
130-
};
131-
break :matches matches;
132-
};
118+
const v: Vector = chunk.*;
119+
// depends on https://github.com/ziglang/zig/issues/19755
120+
// const matches_r: BitVector = @bitCast(v == @as(Vector, @splat('\r')));
121+
// const matches_n: BitVector = @bitCast(v == @as(Vector, @splat('\n')));
122+
const matches_r: BitVector = @select(u1, v == @as(Vector, @splat('\r')), @as(Vector, @splat(1)), @as(Vector, @splat(0)));
123+
const matches_n: BitVector = @select(u1, v == @as(Vector, @splat('\n')), @as(Vector, @splat(1)), @as(Vector, @splat(0)));
124+
const matches_or: SizeVector = matches_r | matches_n;
125+
126+
const matches = @reduce(.Add, matches_or);
133127
switch (matches) {
134128
0 => {},
135129
1 => switch (chunk[vector_len - 1]) {
@@ -357,7 +351,6 @@ inline fn intShift(comptime T: type, x: anytype) T {
357351

358352
const HeadParser = @This();
359353
const std = @import("std");
360-
const use_vectors = builtin.zig_backend != .stage2_x86_64;
361354
const builtin = @import("builtin");
362355

363356
test feed {

lib/std/zon/parse.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3091,7 +3091,6 @@ test "std.zon free on error" {
30913091

30923092
test "std.zon vector" {
30933093
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/15330
3094-
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/15329
30953094

30963095
const gpa = std.testing.allocator;
30973096

0 commit comments

Comments
 (0)