|  | 
|  | 1 | +# This file is a part of Julia. License is MIT: https://julialang.org/license | 
|  | 2 | + | 
|  | 3 | +module RationalToFloat | 
|  | 4 | + | 
|  | 5 | +const Rnd = Base.Rounding | 
|  | 6 | + | 
|  | 7 | +# Performance optimization. Unlike raw `<<` or `>>>`, this is supposed | 
|  | 8 | +# to compile to a single instruction, because the semantics correspond | 
|  | 9 | +# to what hardware usually provides. | 
|  | 10 | +function machine_shift(shift::S, a::T, b) where {S,T<:Base.BitInteger} | 
|  | 11 | +    @inline begin | 
|  | 12 | +        mask = 8*sizeof(T) - 1 | 
|  | 13 | +        c = b & mask | 
|  | 14 | +        shift(a, c) | 
|  | 15 | +    end | 
|  | 16 | +end | 
|  | 17 | + | 
|  | 18 | +machine_shift(::S, a::Bool, ::Any) where {S} = error("unsupported") | 
|  | 19 | + | 
|  | 20 | +# Fallback for `BigInt` etc. | 
|  | 21 | +machine_shift(shift::S, a, b) where {S} = shift(a, b) | 
|  | 22 | + | 
|  | 23 | +# Arguments are positive integers. | 
|  | 24 | +function div_significand_with_remainder(num, den, minimum_significand_size) | 
|  | 25 | +    clamped = x -> max(zero(x), x)::typeof(x) | 
|  | 26 | +    bw = Base.top_set_bit  # bit width | 
|  | 27 | +    shift = clamped(minimum_significand_size + bw(den) - bw(num) + 0x2) | 
|  | 28 | +    t = machine_shift(<<, num, shift) | 
|  | 29 | +    (divrem(t, den, RoundToZero)..., shift) | 
|  | 30 | +end | 
|  | 31 | + | 
|  | 32 | +# `divrem(n, 1<<k, RoundToZero)` | 
|  | 33 | +function divrem_2(n, k) | 
|  | 34 | +    quo = machine_shift(>>>, n,   k) | 
|  | 35 | +    tmp = machine_shift(<<,  quo, k) | 
|  | 36 | +    rem = n - tmp | 
|  | 37 | +    (quo, rem) | 
|  | 38 | +end | 
|  | 39 | + | 
|  | 40 | +function to_float_components_impl(num, den, precision, max_subnormal_exp) | 
|  | 41 | +    # `+1` because we need an extra, "round", bit for some rounding modes. | 
|  | 42 | +    # | 
|  | 43 | +    # TODO: as a performance optimization, only do this when required | 
|  | 44 | +    #       by the rounding mode | 
|  | 45 | +    prec_p_1 = precision + true | 
|  | 46 | + | 
|  | 47 | +    (quo0, rem0, shift) = div_significand_with_remainder(num, den, prec_p_1) | 
|  | 48 | +    width = Base.top_set_bit(quo0) | 
|  | 49 | +    excess_width = width - prec_p_1 | 
|  | 50 | +    exp = width - shift - true | 
|  | 51 | + | 
|  | 52 | +    exp_underflow = if isnothing(max_subnormal_exp) | 
|  | 53 | +        zero(exp) | 
|  | 54 | +    else | 
|  | 55 | +        let d = max_subnormal_exp - exp, T = typeof(d), z = zero(d)::T | 
|  | 56 | +            (signbit(d) ? z : d + true)::T | 
|  | 57 | +        end | 
|  | 58 | +    end | 
|  | 59 | + | 
|  | 60 | +    (quo1, rem1) = divrem_2(quo0, exp_underflow + excess_width) | 
|  | 61 | +    integral_significand = quo1 >>> true | 
|  | 62 | +    round_bit = quo1 % Bool | 
|  | 63 | +    sticky_bit = !iszero(rem1) | !iszero(rem0) | 
|  | 64 | + | 
|  | 65 | +    (; integral_significand, exponent = exp, round_bit, sticky_bit) | 
|  | 66 | +end | 
|  | 67 | + | 
|  | 68 | +struct RoundingIncrementHelper | 
|  | 69 | +    final_bit::Bool | 
|  | 70 | +    round_bit::Bool | 
|  | 71 | +    sticky_bit::Bool | 
|  | 72 | +end | 
|  | 73 | + | 
|  | 74 | +(h::RoundingIncrementHelper)(::Rnd.FinalBit) = h.final_bit | 
|  | 75 | +(h::RoundingIncrementHelper)(::Rnd.RoundBit) = h.round_bit | 
|  | 76 | +(h::RoundingIncrementHelper)(::Rnd.StickyBit) = h.sticky_bit | 
|  | 77 | + | 
|  | 78 | +function to_float_components_rounded(num, den, precision, max_subnormal_exp, romo, sign_bit) | 
|  | 79 | +    overflows = (x, p) -> x == machine_shift(<<, one(x), p) | 
|  | 80 | +    t = to_float_components_impl(num, den, precision, max_subnormal_exp) | 
|  | 81 | +    raw_significand = t.integral_significand | 
|  | 82 | +    rh = RoundingIncrementHelper(raw_significand % Bool, t.round_bit, t.sticky_bit) | 
|  | 83 | +    incr = Rnd.correct_rounding_requires_increment(rh, romo, sign_bit) | 
|  | 84 | +    rounded = raw_significand + incr | 
|  | 85 | +    (integral_significand, exponent) = let exp = t.exponent | 
|  | 86 | +        if overflows(rounded, precision) | 
|  | 87 | +            (rounded >>> true, exp + true) | 
|  | 88 | +        else | 
|  | 89 | +            (rounded, exp) | 
|  | 90 | +        end | 
|  | 91 | +    end | 
|  | 92 | +    (; integral_significand, exponent) | 
|  | 93 | +end | 
|  | 94 | + | 
|  | 95 | +function to_float_components(::Type{T}, num, den, precision, max_subnormal_exp, romo, sb) where {T} | 
|  | 96 | +    to_float_components_rounded(abs(T(num)), den, precision, max_subnormal_exp, romo, sb) | 
|  | 97 | +end | 
|  | 98 | + | 
|  | 99 | +function to_floating_point_fallback(::Type{T}, ::Type{S}, num, den, rm, prec) where {T,S} | 
|  | 100 | +    num_is_zero = iszero(num) | 
|  | 101 | +    den_is_zero = iszero(den) | 
|  | 102 | +    sb = signbit(num) | 
|  | 103 | +    is_zero = num_is_zero & !den_is_zero | 
|  | 104 | +    is_inf = !num_is_zero & den_is_zero | 
|  | 105 | +    is_regular = !num_is_zero & !den_is_zero | 
|  | 106 | +    if is_regular | 
|  | 107 | +        let | 
|  | 108 | +            c = to_float_components(S, num, den, prec, nothing, rm, sb) | 
|  | 109 | +            exp = c.exponent | 
|  | 110 | +            signif = T(c.integral_significand)::T | 
|  | 111 | +            let x = ldexp(signif, exp - prec + true)::T | 
|  | 112 | +                sb ? -x : x | 
|  | 113 | +            end::T | 
|  | 114 | +        end | 
|  | 115 | +    else | 
|  | 116 | +        if is_zero | 
|  | 117 | +            zero(T)::T | 
|  | 118 | +        elseif is_inf | 
|  | 119 | +            T(Inf)::T | 
|  | 120 | +        else | 
|  | 121 | +            T(NaN)::T | 
|  | 122 | +        end | 
|  | 123 | +    end::T | 
|  | 124 | +end | 
|  | 125 | + | 
|  | 126 | +function to_floating_point_impl(::Type{T}, ::Type{S}, num, den, rm, prec) where {T,S} | 
|  | 127 | +    to_floating_point_fallback(T, S, num, den, rm, prec) | 
|  | 128 | +end | 
|  | 129 | + | 
|  | 130 | +function to_floating_point_impl(::Type{T}, ::Type{S}, num, den, rm, prec) where {T<:Base.IEEEFloat,S} | 
|  | 131 | +    num_is_zero = iszero(num) | 
|  | 132 | +    den_is_zero = iszero(den) | 
|  | 133 | +    sb = signbit(num) | 
|  | 134 | +    is_zero = num_is_zero & !den_is_zero | 
|  | 135 | +    is_inf = !num_is_zero & den_is_zero | 
|  | 136 | +    is_regular = !num_is_zero & !den_is_zero | 
|  | 137 | +    (rm_is_to_zero, rm_is_from_zero) = if Rnd.rounds_to_nearest(rm) | 
|  | 138 | +        (false, false) | 
|  | 139 | +    else | 
|  | 140 | +        let from = Rnd.rounds_away_from_zero(rm, sb) | 
|  | 141 | +            (!from, from) | 
|  | 142 | +        end | 
|  | 143 | +    end::NTuple{2,Bool} | 
|  | 144 | +    exp_max = Base.exponent_max(T) | 
|  | 145 | +    exp_min = Base.exponent_min(T) | 
|  | 146 | +    ieee_repr = Base.ieee754_representation | 
|  | 147 | +    repr_zero = ieee_repr(T, sb, Val(:zero)) | 
|  | 148 | +    repr_inf  = ieee_repr(T, sb, Val(:inf)) | 
|  | 149 | +    repr_nan  = ieee_repr(T, sb, Val(:nan)) | 
|  | 150 | +    U = typeof(repr_zero) | 
|  | 151 | +    repr_zero::U | 
|  | 152 | +    repr_inf::U | 
|  | 153 | +    repr_nan::U | 
|  | 154 | + | 
|  | 155 | +    ret_u = if is_regular | 
|  | 156 | +        let | 
|  | 157 | +            c = let e = exp_min - 1 | 
|  | 158 | +                to_float_components(S, num, den, prec, e, rm, sb) | 
|  | 159 | +            end | 
|  | 160 | +            exp = c.exponent | 
|  | 161 | +            exp_diff = exp - exp_min | 
|  | 162 | +            is_normal = 0 ≤ exp_diff | 
|  | 163 | +            exp_is_huge_p = exp_max < exp | 
|  | 164 | +            exp_is_huge_n = signbit(exp_diff + prec) | 
|  | 165 | +            rounds_to_inf  = exp_is_huge_p & !rm_is_to_zero | 
|  | 166 | +            rounds_to_zero = exp_is_huge_n & !rm_is_from_zero | 
|  | 167 | + | 
|  | 168 | +            if !rounds_to_zero & !exp_is_huge_p | 
|  | 169 | +                let signif = (c.integral_significand % U) & Base.significand_mask(T) | 
|  | 170 | +                    exp_field = (max(exp_diff, zero(exp_diff)) + is_normal) % U | 
|  | 171 | +                    ieee_repr(T, sb, exp_field, signif)::U | 
|  | 172 | +                end | 
|  | 173 | +            elseif rounds_to_zero | 
|  | 174 | +                repr_zero | 
|  | 175 | +            elseif rounds_to_inf | 
|  | 176 | +                repr_inf | 
|  | 177 | +            else | 
|  | 178 | +                ieee_repr(T, sb, Val(:omega)) | 
|  | 179 | +            end | 
|  | 180 | +        end | 
|  | 181 | +    else | 
|  | 182 | +        if is_zero | 
|  | 183 | +            repr_zero | 
|  | 184 | +        elseif is_inf | 
|  | 185 | +            repr_inf | 
|  | 186 | +        else | 
|  | 187 | +            repr_nan | 
|  | 188 | +        end | 
|  | 189 | +    end::U | 
|  | 190 | + | 
|  | 191 | +    reinterpret(T, ret_u)::T | 
|  | 192 | +end | 
|  | 193 | + | 
|  | 194 | +# `BigInt` is a safe default. | 
|  | 195 | +to_float_promote_type(::Type{F}, ::Type{S}) where {F,S} = BigInt | 
|  | 196 | + | 
|  | 197 | +const BitIntegerOrBool  = Union{Bool,Base.BitInteger} | 
|  | 198 | + | 
|  | 199 | +# As an optimization, use an integer type narrower than `BigInt` when possible. | 
|  | 200 | +function to_float_promote_type(::Type{F}, ::Type{S}) where {F<:Base.IEEEFloat,S<:BitIntegerOrBool} | 
|  | 201 | +    Max = if sizeof(F) ≤ sizeof(S) | 
|  | 202 | +        S | 
|  | 203 | +    else | 
|  | 204 | +        (S <: Signed) ? Base.inttype(F) : Base.uinttype(F) | 
|  | 205 | +    end | 
|  | 206 | +    widen(Max) | 
|  | 207 | +end | 
|  | 208 | + | 
|  | 209 | +function to_floating_point(::Type{F}, num::T, den::T, rm, prec) where {F,T} | 
|  | 210 | +    S = to_float_promote_type(F, T) | 
|  | 211 | +    to_floating_point_impl(F, S, num, den, rm, prec) | 
|  | 212 | +end | 
|  | 213 | + | 
|  | 214 | +end | 
0 commit comments