authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2023-09-15 22:19:10+12:00
committergravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2023-09-30 21:22:12+13:00
log5b5da0ef8c8be40c25106f17a264950d25ba82c9
tree0c3fd4eaecf255a58f30c785b1448e92b64e6c7a
parentcd766513febecd79fc62a0f46ee18867fabbbf7d

std.mem: check backend vector support for indexOfSentinel/indexOfScalarPos


1 files changed, 13 insertions(+), 2 deletions(-)

lib/std/mem.zig+13-2
...@@ -953,10 +953,18 @@ test "len" {...@@ -953,10 +953,18 @@ test "len" {
953 try testing.expect(len(c_ptr) == 2);953 try testing.expect(len(c_ptr) == 2);
954}954}
955955
956const backend_supports_vectors = switch (builtin.zig_backend) {
957 .stage2_llvm, .stage2_c => true,
958 else => false,
959};
960
956pub fn indexOfSentinel(comptime T: type, comptime sentinel: T, p: [*:sentinel]const T) usize {961pub fn indexOfSentinel(comptime T: type, comptime sentinel: T, p: [*:sentinel]const T) usize {
957 var i: usize = 0;962 var i: usize = 0;
958963
959 if (!@inComptime() and (@typeInfo(T) == .Int or @typeInfo(T) == .Float) and std.math.isPowerOfTwo(@bitSizeOf(T))) {964 if (backend_supports_vectors and
965 !@inComptime() and
966 (@typeInfo(T) == .Int or @typeInfo(T) == .Float) and std.math.isPowerOfTwo(@bitSizeOf(T)))
967 {
960 switch (@import("builtin").cpu.arch) {968 switch (@import("builtin").cpu.arch) {
961 // The below branch assumes that reading past the end of the buffer is valid, as long969 // The below branch assumes that reading past the end of the buffer is valid, as long
962 // as we don't read into a new page. This should be the case for most architectures970 // as we don't read into a new page. This should be the case for most architectures
...@@ -1066,7 +1074,10 @@ pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize,...@@ -1066,7 +1074,10 @@ pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize,
1066 if (start_index >= slice.len) return null;1074 if (start_index >= slice.len) return null;
10671075
1068 var i: usize = start_index;1076 var i: usize = start_index;
1069 if (!@inComptime() and (@typeInfo(T) == .Int or @typeInfo(T) == .Float) and std.math.isPowerOfTwo(@bitSizeOf(T))) {1077 if (backend_supports_vectors and
1078 !@inComptime() and
1079 (@typeInfo(T) == .Int or @typeInfo(T) == .Float) and std.math.isPowerOfTwo(@bitSizeOf(T)))
1080 {
1070 if (comptime std.simd.suggestVectorSize(T)) |block_len| {1081 if (comptime std.simd.suggestVectorSize(T)) |block_len| {
1071 // For Intel Nehalem (2009) and AMD Bulldozer (2012) or later, unaligned loads on aligned data result1082 // For Intel Nehalem (2009) and AMD Bulldozer (2012) or later, unaligned loads on aligned data result
1072 // in the same execution as aligned loads. We ignore older arch's here and don't bother pre-aligning.1083 // in the same execution as aligned loads. We ignore older arch's here and don't bother pre-aligning.