Skip to content

Commit

Permalink
Use firstIndexOfValue
Browse files Browse the repository at this point in the history
  • Loading branch information
notcancername committed Aug 2, 2023
1 parent 685e8c4 commit 36b7b37
Showing 1 changed file with 2 additions and 20 deletions.
22 changes: 2 additions & 20 deletions lib/std/mem.zig
Original file line number Diff line number Diff line change
Expand Up @@ -999,28 +999,10 @@ pub fn indexOfSentinel(comptime Elem: type, comptime sentinel: Elem, ptr: [*:sen
i += 1;
} else return i;

const all_sentinel: V = @splat(sentinel);

while (true) : (i += v_len) {
const v: V = @bitCast(ptr[i..][0..v_len].*);

const equal_mask = v == all_sentinel;

if (comptime builtin.cpu.arch.isX86()) {
// HACK: This /should/ work on other architectures as well, but
// as a safety measure, it is only done on x86.

const equal_int: meta.Int(.unsigned, v_len) = @bitCast(equal_mask);

if (equal_int != 0) {
return i + @ctz(equal_int);
}
} else {
// use the slow guaranteed-to-work method
const maybe_first_true = simd.firstTrue(equal_mask);
if (maybe_first_true) |first_true| {
return i + first_true;
}
if (simd.firstIndexOfValue(v, sentinel)) |index| {
return i + index;
}
}
}
Expand Down

0 comments on commit 36b7b37

Please sign in to comment.