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.

sqrtf

sqrt.sqrtf
pub fn sqrtf(x: f32) callconv(.c) f32

File

lib/compiler_rt/sqrt.zig:96

Code

pub fn sqrtf(x: f32) callconv(.c) f32 {
    var ix: u32 = @bitCast(x);

    if (ix < @as(u32, @bitCast(@as(f32, 0x1p-126))) or @as(u32, @bitCast(std.math.inf(f32))) <= ix) {
        @branchHint(.unlikely);

        if (ix & 0x7fffffff == 0)
            return x;

        if (ix == @as(u32, @bitCast(std.math.inf(f32))))
            return x;

        if (ix > @as(u32, @bitCast(std.math.inf(f32))))
            return if (compiler_rt.want_float_exceptions) (x - x) / 0.0 else math.nan(f32);

        ix = @as(u32, @bitCast(@as(i32, @bitCast(x * 0x1p23)) - (23 << 23)));
    }

    const m: u32 = if (ix & 0x00800000 != 0)
        (ix << 7) & 0x7fffffff
    else
        (ix << 8) | 0x80000000;

    const ey = ((ix >> 1) + (0x3f800000 >> 1)) & 0x7f800000;
    // const ey = ((ix + 0x3f800000) & 0xff000000) >> 1;

    const three = 0xc0000000;
    const i = (ix >> 17) & 0x7f;
    var r = @as(u32, rsqrt_tab[i]) << 16;

    var s = mul32(m, r);
    var d = mul32(s, r);
    var u = three - d;
    r = mul32(r, u) << 1;
    s = mul32(s, u) << 1;
    d = mul32(s, r);
    u = three - d;
    s = mul32(s, u);
    s = (s - 1) >> 6;

    const d0 = (m << 16) -% s *% s;
    const d1 = s -% d0;
    const d2 = d1 +% s +% 1;
    const y: f32 = @bitCast(((s + (d1 >> 31)) & 0x007fffff) | ey);

    const tiny: u32 = if (d2 == 0) blk: {
        @branchHint(.unlikely);
        break :blk 0;
    } else 0x01000000;
    const t: f32 = @bitCast(tiny | ((d1 ^ d2) & 0x80000000));

    return y + t;
}