Zig 0.17.0-dev (Split by item)

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 library itself.

fastMemset

compiler_rt.fastMemset
fn fastMemset(dest: ?[*]u8, c: c_int, len: usize) callconv(.c) ?[*]u8

File

lib/compiler_rt.zig:685

Code

fn fastMemset(dest: ?[*]u8, c: c_int, len: usize) callconv(.c) ?[*]u8 {
    const b: u8 = @truncate(@as(c_uint, @bitCast(c)));
    const n = std.simd.suggestVectorLength(u8) orelse @sizeOf(usize);

    const d = dest.?;

    if (len > 2 * n) {
        memsetSmallPowerOf2(d, b, n);

        const begin_aligned = std.mem.alignBackward(usize, @intFromPtr(d) + n, n);
        const end_aligned = std.mem.alignForward(usize, @intFromPtr(d) + len - n, n);

        const aligned_ptr: [*]align(n) u8 = @ptrFromInt(begin_aligned);

        var i: usize = 0;
        while (true) {
            memsetSmallPowerOf2(aligned_ptr + n * i, b, n);

            i += 1;
            if (i == @divExact(end_aligned - begin_aligned, n))
                break;
        }

        memsetSmallPowerOf2(d + len - n, b, n);
    } else {
        if (len == 0) return dest;

        shortMemset(0, @ctz(@as(usize, 2 * n)), d, b, len);
    }

    return dest;
}