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.

absv

absv.absv
pub inline fn absv(comptime ST: type, a: ST) ST

File

Code

pub inline fn absv(comptime ST: type, a: ST) ST {
    const UT = switch (ST) {
        i32 => u32,
        i64 => u64,
        i128 => u128,
        else => unreachable,
    };
    // taken from  Bit Twiddling Hacks
    // compute the integer absolute value (abs) without branching
    var x: ST = a;
    const N: UT = @bitSizeOf(ST);
    const sign: ST = a >> N - 1;
    x +%= sign;
    x ^= sign;
    if (x < 0)
        @panic("compiler_rt absv: overflow");
    return x;
}