This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Public Function
pubfnfindScalarPos(comptimeT: type, slice: []constT, start_index: usize, value: T) ?usize {
if (start_index >= slice.len) returnnull;
vari: usize = start_index;
if (use_vectors_for_comparisonand
!std.debug.inValgrind() and// https://github.com/ziglang/zig/issues/17717
!@inComptime() and
(@typeInfo(T) == .intor@typeInfo(T) == .float) andstd.math.isPowerOfTwo(@bitSizeOf(T)))
{
if (std.simd.suggestVectorLength(T)) |block_len| {
// For Intel Nehalem (2009) and AMD Bulldozer (2012) or later, unaligned loads on aligned data result// in the same execution as aligned loads. We ignore older arch's here and don't bother pre-aligning.//// Use `std.simd.suggestVectorLength(T)` to get the same alignment as used in this function// however this usually isn't necessary unless your arch has a performance penalty due to this.//// This may differ for other arch's. Arm for example costs a cycle when loading across a cache// line so explicit alignment prologues may be worth exploration.// Unrolling here is ~10% improvement. We can then do one bounds check every 2 blocks// instead of one which adds up.constBlock = @Vector(block_len, T);
if (i + 2 * block_len < slice.len) {
constmask: Block = @splat(value);
while (true) {
inlinefor (0..2) |_| {
constblock: Block = slice[i..][0..block_len].*;
constmatches = block == mask;
if (@reduce(.Or, matches)) {
returni + std.simd.firstTrue(matches).?;
}
i += block_len;
}
if (i + 2 * block_len >= slice.len) break;
}
}
// {block_len, block_len / 2} checkinlinefor (0..2) |j| {
constblock_x_len = block_len / (1 << j);
comptimeif (block_x_len < 4) break;
constBlockX = @Vector(block_x_len, T);
if (i + block_x_len < slice.len) {
constmask: BlockX = @splat(value);
constblock: BlockX = slice[i..][0..block_x_len].*;
constmatches = block == mask;
if (@reduce(.Or, matches)) {
returni + std.simd.firstTrue(matches).?;
}
i += block_x_len;
}
}
}
}
for (slice[i..], i..) |c, j| {
if (c == value) returnj;
}
returnnull;
}
Linear search for the index of a scalar value inside a slice, starting from a given position. Returns null if the value is not found.