Skip to content

Commit f25212a

Browse files
authored
Merge pull request #23924 from jacobly0/x86_64-rewrite
x86_64: implement reduce
2 parents 4f3b59f + 9f9e7e3 commit f25212a

40 files changed

+46928
-1187
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/crypto/timing_safe.zig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,6 @@ test eql {
192192
}
193193

194194
test "eql (vectors)" {
195-
if (@import("builtin").zig_backend == .stage2_x86_64) return error.SkipZigTest;
196-
197195
const random = std.crypto.random;
198196
const expect = std.testing.expect;
199197
var a: [100]u8 = undefined;

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/simd.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,8 @@ pub fn countElementsWithValue(vec: anytype, value: std.meta.Child(@TypeOf(vec)))
368368
}
369369

370370
test "vector searching" {
371-
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
371+
if (builtin.zig_backend == .stage2_x86_64 and
372+
!comptime std.Target.x86.featureSetHas(builtin.cpu.features, .ssse3)) return error.SkipZigTest;
372373

373374
const base = @Vector(8, u32){ 6, 4, 7, 4, 4, 2, 3, 7 };
374375

lib/std/zig/Zir.zig

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2142,7 +2142,7 @@ pub const Inst = struct {
21422142
ref_start_index = static_len,
21432143
_,
21442144

2145-
pub const static_len = 101;
2145+
pub const static_len = 118;
21462146

21472147
pub fn toRef(i: Index) Inst.Ref {
21482148
return @enumFromInt(@intFromEnum(Index.ref_start_index) + @intFromEnum(i));
@@ -2190,6 +2190,7 @@ pub const Inst = struct {
21902190
u80_type,
21912191
u128_type,
21922192
i128_type,
2193+
u256_type,
21932194
usize_type,
21942195
isize_type,
21952196
c_char_type,
@@ -2228,34 +2229,50 @@ pub const Inst = struct {
22282229
vector_8_i8_type,
22292230
vector_16_i8_type,
22302231
vector_32_i8_type,
2232+
vector_64_i8_type,
22312233
vector_1_u8_type,
22322234
vector_2_u8_type,
22332235
vector_4_u8_type,
22342236
vector_8_u8_type,
22352237
vector_16_u8_type,
22362238
vector_32_u8_type,
2239+
vector_64_u8_type,
2240+
vector_2_i16_type,
22372241
vector_4_i16_type,
22382242
vector_8_i16_type,
22392243
vector_16_i16_type,
2244+
vector_32_i16_type,
22402245
vector_4_u16_type,
22412246
vector_8_u16_type,
22422247
vector_16_u16_type,
2248+
vector_32_u16_type,
2249+
vector_2_i32_type,
22432250
vector_4_i32_type,
22442251
vector_8_i32_type,
2252+
vector_16_i32_type,
22452253
vector_4_u32_type,
22462254
vector_8_u32_type,
2255+
vector_16_u32_type,
22472256
vector_2_i64_type,
22482257
vector_4_i64_type,
2258+
vector_8_i64_type,
22492259
vector_2_u64_type,
22502260
vector_4_u64_type,
2261+
vector_8_u64_type,
2262+
vector_1_u128_type,
22512263
vector_2_u128_type,
2264+
vector_1_u256_type,
22522265
vector_4_f16_type,
22532266
vector_8_f16_type,
2267+
vector_16_f16_type,
2268+
vector_32_f16_type,
22542269
vector_2_f32_type,
22552270
vector_4_f32_type,
22562271
vector_8_f32_type,
2272+
vector_16_f32_type,
22572273
vector_2_f64_type,
22582274
vector_4_f64_type,
2275+
vector_8_f64_type,
22592276
optional_noreturn_type,
22602277
anyerror_void_error_union_type,
22612278
adhoc_inferred_error_set_type,

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

src/Air.zig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,7 @@ pub const Inst = struct {
973973
u80_type = @intFromEnum(InternPool.Index.u80_type),
974974
u128_type = @intFromEnum(InternPool.Index.u128_type),
975975
i128_type = @intFromEnum(InternPool.Index.i128_type),
976+
u256_type = @intFromEnum(InternPool.Index.u256_type),
976977
usize_type = @intFromEnum(InternPool.Index.usize_type),
977978
isize_type = @intFromEnum(InternPool.Index.isize_type),
978979
c_char_type = @intFromEnum(InternPool.Index.c_char_type),
@@ -1011,34 +1012,50 @@ pub const Inst = struct {
10111012
vector_8_i8_type = @intFromEnum(InternPool.Index.vector_8_i8_type),
10121013
vector_16_i8_type = @intFromEnum(InternPool.Index.vector_16_i8_type),
10131014
vector_32_i8_type = @intFromEnum(InternPool.Index.vector_32_i8_type),
1015+
vector_64_i8_type = @intFromEnum(InternPool.Index.vector_64_i8_type),
10141016
vector_1_u8_type = @intFromEnum(InternPool.Index.vector_1_u8_type),
10151017
vector_2_u8_type = @intFromEnum(InternPool.Index.vector_2_u8_type),
10161018
vector_4_u8_type = @intFromEnum(InternPool.Index.vector_4_u8_type),
10171019
vector_8_u8_type = @intFromEnum(InternPool.Index.vector_8_u8_type),
10181020
vector_16_u8_type = @intFromEnum(InternPool.Index.vector_16_u8_type),
10191021
vector_32_u8_type = @intFromEnum(InternPool.Index.vector_32_u8_type),
1022+
vector_64_u8_type = @intFromEnum(InternPool.Index.vector_64_u8_type),
1023+
vector_2_i16_type = @intFromEnum(InternPool.Index.vector_2_i16_type),
10201024
vector_4_i16_type = @intFromEnum(InternPool.Index.vector_4_i16_type),
10211025
vector_8_i16_type = @intFromEnum(InternPool.Index.vector_8_i16_type),
10221026
vector_16_i16_type = @intFromEnum(InternPool.Index.vector_16_i16_type),
1027+
vector_32_i16_type = @intFromEnum(InternPool.Index.vector_32_i16_type),
10231028
vector_4_u16_type = @intFromEnum(InternPool.Index.vector_4_u16_type),
10241029
vector_8_u16_type = @intFromEnum(InternPool.Index.vector_8_u16_type),
10251030
vector_16_u16_type = @intFromEnum(InternPool.Index.vector_16_u16_type),
1031+
vector_32_u16_type = @intFromEnum(InternPool.Index.vector_32_u16_type),
1032+
vector_2_i32_type = @intFromEnum(InternPool.Index.vector_2_i32_type),
10261033
vector_4_i32_type = @intFromEnum(InternPool.Index.vector_4_i32_type),
10271034
vector_8_i32_type = @intFromEnum(InternPool.Index.vector_8_i32_type),
1035+
vector_16_i32_type = @intFromEnum(InternPool.Index.vector_16_i32_type),
10281036
vector_4_u32_type = @intFromEnum(InternPool.Index.vector_4_u32_type),
10291037
vector_8_u32_type = @intFromEnum(InternPool.Index.vector_8_u32_type),
1038+
vector_16_u32_type = @intFromEnum(InternPool.Index.vector_16_u32_type),
10301039
vector_2_i64_type = @intFromEnum(InternPool.Index.vector_2_i64_type),
10311040
vector_4_i64_type = @intFromEnum(InternPool.Index.vector_4_i64_type),
1041+
vector_8_i64_type = @intFromEnum(InternPool.Index.vector_8_i64_type),
10321042
vector_2_u64_type = @intFromEnum(InternPool.Index.vector_2_u64_type),
10331043
vector_4_u64_type = @intFromEnum(InternPool.Index.vector_4_u64_type),
1044+
vector_8_u64_type = @intFromEnum(InternPool.Index.vector_8_u64_type),
1045+
vector_1_u128_type = @intFromEnum(InternPool.Index.vector_1_u128_type),
10341046
vector_2_u128_type = @intFromEnum(InternPool.Index.vector_2_u128_type),
1047+
vector_1_u256_type = @intFromEnum(InternPool.Index.vector_1_u256_type),
10351048
vector_4_f16_type = @intFromEnum(InternPool.Index.vector_4_f16_type),
10361049
vector_8_f16_type = @intFromEnum(InternPool.Index.vector_8_f16_type),
1050+
vector_16_f16_type = @intFromEnum(InternPool.Index.vector_16_f16_type),
1051+
vector_32_f16_type = @intFromEnum(InternPool.Index.vector_32_f16_type),
10371052
vector_2_f32_type = @intFromEnum(InternPool.Index.vector_2_f32_type),
10381053
vector_4_f32_type = @intFromEnum(InternPool.Index.vector_4_f32_type),
10391054
vector_8_f32_type = @intFromEnum(InternPool.Index.vector_8_f32_type),
1055+
vector_16_f32_type = @intFromEnum(InternPool.Index.vector_16_f32_type),
10401056
vector_2_f64_type = @intFromEnum(InternPool.Index.vector_2_f64_type),
10411057
vector_4_f64_type = @intFromEnum(InternPool.Index.vector_4_f64_type),
1058+
vector_8_f64_type = @intFromEnum(InternPool.Index.vector_8_f64_type),
10421059
optional_noreturn_type = @intFromEnum(InternPool.Index.optional_noreturn_type),
10431060
anyerror_void_error_union_type = @intFromEnum(InternPool.Index.anyerror_void_error_union_type),
10441061
adhoc_inferred_error_set_type = @intFromEnum(InternPool.Index.adhoc_inferred_error_set_type),

0 commit comments

Comments
 (0)