跳转到内容

std.fmt.parse_float.convert_slow

标准库Zig 0.17.1官方原文
源码std/fmt/parse_float/convert_slow.zig

第 1 行

std.fmt.parse_float.convert_slow.BiasedFp

别名

第 4 行

别名目标 std.fmt.parse_float.common.BiasedFp

std.fmt.parse_float.convert_slow.common

别名

第 3 行

别名目标 std.fmt.parse_float.common

std.fmt.parse_float.convert_slow.convertSlow

函数

第 38 行

解析浮点数的有效数字及带偏置的二进制指数。

这是一种后备算法,使用浮点数的大整数表示,因此比更快的近似算法慢得多。不过,它始终能够确定如何将有效数字舍入到最接近的机器浮点数,从而处理接近中点的情况。

中点位于两个相邻机器浮点数之间。例如,浮点数 16777217.0 的二进制表示为 100000000000000000000000 1。舍入为单精度浮点数时,末尾的 1 被截去。采用舍入到最近值、中点取偶数的规则时,任何大于 16777217.0 的值都必须向上舍入为 16777218.0,而任何小于或等于 16777217.0 的值都必须向下舍入为 16777216.0。因此,接近中点的转换可能需要大量数字,才能无歧义地确定舍入方向。

这里的算法基于《快速处理长数字》,可在此获取:https://arxiv.org/pdf/2101.11408.pdf#section.11。

注意,此函数需要大量栈空间,并标记为冷路径,以提示编译器不要将其内联到调用者中。

pub fn convertSlow(comptime T: type, s: []const u8) BiasedFp(T)

参数

T: type
s: []const u8

std.fmt.parse_float.convert_slow.Decimal

别名

第 5 行

别名目标 std.fmt.parse_float.decimal.Decimal

std.fmt.parse_float.convert_slow.getShift

函数

第 12 行

pub fn getShift(n: usize) usize

参数

n: usize

std.fmt.parse_float.convert_slow.mantissaType

别名

第 6 行

别名目标 std.fmt.parse_float.common.mantissaType

std.fmt.parse_float.convert_slow.math

别名

第 2 行

别名目标 std.math

std.fmt.parse_float.convert_slow.max_shift

常量

第 8 行

std.fmt.parse_float.convert_slow.num_powers

常量

第 9 行

std.fmt.parse_float.convert_slow.powers

常量

第 10 行

std.fmt.parse_float.convert_slow.std

别名

第 1 行

别名目标 std

源码

std/fmt/parse_float/convert_slow.zig

const std = @import("std");
const math = std.math;
const common = @import("common.zig");
const BiasedFp = common.BiasedFp;
const Decimal = @import("decimal.zig").Decimal;
const mantissaType = common.mantissaType;

const max_shift = 60;
const num_powers = 19;
const powers = [_]u8{ 0, 3, 6, 9, 13, 16, 19, 23, 26, 29, 33, 36, 39, 43, 46, 49, 53, 56, 59 };

pub fn getShift(n: usize) usize {
    return if (n < num_powers) powers[n] else max_shift;
}

/// 解析浮点数的有效数字及带偏置的二进制指数。
///
/// 这是一种后备算法,使用浮点数的大整数表示,因此比更快的近似算法慢得多。不过,它始终能够确定如何将有效数字舍入到最接近的机器浮点数,从而处理接近中点的情况。
///
/// 中点位于两个相邻机器浮点数之间。例如,浮点数 `16777217.0` 的二进制表示为 `100000000000000000000000 1`。舍入为单精度浮点数时,末尾的 `1` 被截去。采用舍入到最近值、中点取偶数的规则时,任何大于 `16777217.0` 的值都必须向上舍入为 `16777218.0`,而任何小于或等于 `16777217.0` 的值都必须向下舍入为 `16777216.0`。因此,接近中点的转换可能需要大量数字,才能无歧义地确定舍入方向。
///
/// 这里的算法基于《快速处理长数字》,可在此获取:<https://arxiv.org/pdf/2101.11408.pdf#section.11>。
///
/// 注意,此函数需要大量栈空间,并标记为冷路径,以提示编译器不要将其内联到调用者中。
///
///
///
///
///
///
///
///
///
///
///
///
///
pub fn convertSlow(comptime T: type, s: []const u8) BiasedFp(T) {
    @branchHint(.cold);

    const MantissaT = mantissaType(T);
    const min_exponent = -(1 << (math.floatExponentBits(T) - 1)) + 1;
    const infinite_power = (1 << math.floatExponentBits(T)) - 1;
    const fractional_bits = math.floatFractionalBits(T);

    var d = Decimal(T).parse(s); // 无需重新检查下划线
    if (d.num_digits == 0 or d.decimal_point < Decimal(T).min_exponent) {
        return BiasedFp(T).zero();
    } else if (d.decimal_point >= Decimal(T).max_exponent) {
        return BiasedFp(T).inf(T);
    }

    var exp2: i32 = 0;
    // 右移至 (1/2 .. 1] 范围
    while (d.decimal_point > 0) {
        const n = @as(usize, @intCast(d.decimal_point));
        const shift = getShift(n);
        d.rightShift(shift);
        if (d.decimal_point < -Decimal(T).decimal_point_range) {
            return BiasedFp(T).zero();
        }
        exp2 += @as(i32, @intCast(shift));
    }
    //  向左移至 (1/2 .. 1]
    while (d.decimal_point <= 0) {
        const shift = blk: {
            if (d.decimal_point == 0) {
                break :blk switch (d.digits[0]) {
                    5...9 => break,
                    0, 1 => @as(usize, 2),
                    else => 1,
                };
            } else {
                const n = @as(usize, @intCast(-d.decimal_point));
                break :blk getShift(n);
            }
        };
        d.leftShift(shift);
        if (d.decimal_point > Decimal(T).decimal_point_range) {
            return BiasedFp(T).inf(T);
        }
        exp2 -= @as(i32, @intCast(shift));
    }
    // 当前位于 [1/2 .. 1] 范围,但二进制格式使用 [1 .. 2]。
    exp2 -= 1;
    while (min_exponent + 1 > exp2) {
        var n = @as(usize, @intCast((min_exponent + 1) - exp2));
        if (n > max_shift) {
            n = max_shift;
        }
        d.rightShift(n);
        exp2 += @as(i32, @intCast(n));
    }
    if (exp2 - min_exponent >= infinite_power) {
        return BiasedFp(T).inf(T);
    }

    // 将小数移至隐藏位,再对值进行舍入,
    // 以获得高 mantissa+1 位。
    d.leftShift(fractional_bits + 1);
    var mantissa = d.round();
    if (mantissa >= (@as(MantissaT, 1) << (fractional_bits + 1))) {
        // 向上舍入溢出到进位位,需要移回隐藏位。
        //
        d.rightShift(1);
        exp2 += 1;
        mantissa = d.round();
        if ((exp2 - min_exponent) >= infinite_power) {
            return BiasedFp(T).inf(T);
        }
    }
    var power2 = exp2 - min_exponent;
    if (mantissa < (@as(MantissaT, 1) << fractional_bits)) {
        power2 -= 1;
    }
    // 将尾数位以上的所有位清零。
    mantissa &= (@as(MantissaT, 1) << math.floatMantissaBits(T)) - 1;
    return .{ .f = mantissa, .e = power2 };
}