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.

convertFast

convert_fast.convertFast
pub fn convertFast(comptime T: type, n: Number(T)) ?T

File

lib/std/fmt/parse_float/convert_fast.zig:105

Code

pub fn convertFast(comptime T: type, n: Number(T)) ?T {
    const MantissaT = common.mantissaType(T);

    if (!isFastPath(T, n)) {
        return null;
    }

    // TODO: x86 (no SSE/SSE2) requires x87 FPU to be setup correctly with fldcw
    const info = FloatInfo.from(T);

    var value: T = 0;
    if (n.exponent <= info.max_exponent_fast_path) {
        // normal fast path
        value = @as(T, @floatFromInt(n.mantissa));
        value = if (n.exponent < 0)
            value / fastPow10(T, @as(usize, @intCast(-n.exponent)))
        else
            value * fastPow10(T, @as(usize, @intCast(n.exponent)));
    } else {
        // disguised fast path
        const shift = n.exponent - info.max_exponent_fast_path;
        const mantissa = math.mul(MantissaT, n.mantissa, fastIntPow10(MantissaT, @as(usize, @intCast(shift)))) catch return null;
        if (mantissa > info.max_mantissa_fast_path) {
            return null;
        }
        value = @as(T, @floatFromInt(mantissa)) * fastPow10(T, info.max_exponent_fast_path);
    }

    if (n.negative) {
        value = -value;
    }
    return value;
}