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.

atanBinary16

atan.atanBinary16
fn atanBinary16(x: f16) f16

File

lib/std/math/atan.zig:45

Code

fn atanBinary16(x: f16) f16 {
    const atanhi: []const f32 = &.{
        4.6364760399e-01, // atan(0.5)hi 0x3eed6338
        7.8539812565e-01, // atan(1.0)hi 0x3f490fda
        9.8279368877e-01, // atan(1.5)hi 0x3f7b985e
        1.5707962513e+00, // atan(inf)hi 0x3fc90fda
    };
    const aT: []const f32 = &.{
        0x1.fffcccp-1,
        -0x1.52e8ccp-2,
        0x1.522336p-3,
    };

    const hx: u16 = @bitCast(x);
    const ix = hx & 0x7fff;
    const sign = (hx >> 15) != 0;
    // if |x| >= 2^11
    if (ix >= 0x6800) {
        if (math.isNan(x)) {
            return x;
        }
        const z = atanhi[3] + 0x1p-120;
        return @floatCast(if (sign) -z else z);
    }
    const x_: f32, const id: ?usize = blk: {
        // |x| < 0.4375
        if (ix < 0x3700) {
            // |x| < 2^(-6)
            if (ix < 0x2400) {
                if (ix < 0x400) {
                    // raise underflow for subnormal x
                    mem.doNotOptimizeAway(x * x);
                }
                return x;
            }
            break :blk .{ @floatCast(x), null };
        } else {
            const x_: f32 = @floatCast(@abs(x));
            // |x| < 1.1875
            if (ix < 0x3cc0) {
                // 7/16 <= |x| < 11/16
                if (ix < 0x3980) {
                    break :blk .{ (2.0 * x_ - 1.0) / (2.0 + x_), 0 };
                }
                // 11/16 <= |x| < 19/16
                else {
                    break :blk .{ (x_ - 1.0) / (x_ + 1.0), 1 };
                }
            } else {
                // |x| < 2.4375
                if (ix < 0x40e0) {
                    break :blk .{ (x_ - 1.5) / (1.0 + 1.5 * x_), 2 };
                }
                // 2.4375 <= |x| < 2^11
                else {
                    break :blk .{ -1.0 / x_, 3 };
                }
            }
        }
    };
    // end of argument reduction
    const z = x_ * x_;
    const s = aT[0] + z * (aT[1] + z * aT[2]);
    if (id) |id_| {
        const z_ = atanhi[id_] + x_ * s;
        return @floatCast(if (sign) -z_ else z_);
    } else {
        return @floatCast(x_ * s);
    }
}