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.

roundf

round.roundf
pub fn roundf(x_: f32) callconv(.c) f32

File

lib/compiler_rt/round.zig:33

Code

pub fn roundf(x_: f32) callconv(.c) f32 {
    const f32_toint = 1.0 / math.floatEps(f32);

    var x = x_;
    const u: u32 = @bitCast(x);
    const e = (u >> 23) & 0xFF;
    var y: f32 = undefined;

    if (e >= 0x7F + 23) {
        return x;
    }
    if (u >> 31 != 0) {
        x = -x;
    }
    if (e < 0x7F - 1) {
        if (compiler_rt.want_float_exceptions) mem.doNotOptimizeAway(x + f32_toint);
        return 0 * @as(f32, @bitCast(u));
    }

    y = x + f32_toint - f32_toint - x;
    if (y > 0.5) {
        y = y + x - 1;
    } else if (y <= -0.5) {
        y = y + x + 1;
    } else {
        y = y + x;
    }

    if (u >> 31 != 0) {
        return -y;
    } else {
        return y;
    }
}