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.

cosf

cos.cosf
pub fn cosf(x: f32) callconv(.c) f32

File

Code

pub fn cosf(x: f32) callconv(.c) f32 {
    // Small multiples of pi/2 rounded to double precision.
    const c1pio2: f64 = 1.0 * math.pi / 2.0; // 0x3FF921FB, 0x54442D18
    const c2pio2: f64 = 2.0 * math.pi / 2.0; // 0x400921FB, 0x54442D18
    const c3pio2: f64 = 3.0 * math.pi / 2.0; // 0x4012D97C, 0x7F3321D2
    const c4pio2: f64 = 4.0 * math.pi / 2.0; // 0x401921FB, 0x54442D18

    var ix: u32 = @bitCast(x);
    const sign = ix >> 31 != 0;
    ix &= 0x7fffffff;

    if (ix <= 0x3f490fda) { // |x| ~<= pi/4
        if (ix < 0x39800000) { // |x| < 2**-12
            // raise inexact if x != 0
            if (compiler_rt.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
            return 1.0;
        }
        return trig.cosdf(x);
    }
    if (ix <= 0x407b53d1) { // |x| ~<= 5*pi/4
        if (ix > 0x4016cbe3) { // |x|  ~> 3*pi/4
            return -trig.cosdf(if (sign) x + c2pio2 else x - c2pio2);
        } else {
            if (sign) {
                return trig.sindf(x + c1pio2);
            } else {
                return trig.sindf(c1pio2 - x);
            }
        }
    }
    if (ix <= 0x40e231d5) { // |x| ~<= 9*pi/4
        if (ix > 0x40afeddf) { // |x| ~> 7*pi/4
            return trig.cosdf(if (sign) x + c4pio2 else x - c4pio2);
        } else {
            if (sign) {
                return trig.sindf(-x - c3pio2);
            } else {
                return trig.sindf(x - c3pio2);
            }
        }
    }

    // cos(Inf or NaN) is NaN
    if (ix >= 0x7f800000) {
        return x - x;
    }

    var y: f64 = undefined;
    const n = rem_pio2f(x, &y);
    return switch (n & 3) {
        0 => trig.cosdf(y),
        1 => trig.sindf(-y),
        2 => -trig.cosdf(y),
        else => trig.sindf(y),
    };
}