| author | |
| committer | |
| log | eac1e613be56875a08102bd06e333a0621600ee6 |
| tree | 7c65fbc28f805bcb3d0e1fe4522a738cb36df97d |
| parent | 7f508480f49b78817bf67579a8810d4499cbbc13 |
This re-write was needed to fix deficiencies in the existing ldexp,
which was failing to compute correct results for both f16 and f80.
It would be nice to add a fast multiplication-based fallback in the
future for targets that have a hardware FPU, but this implementation
should be much faster than the existing for targets without one.2 files changed, 227 insertions(+), 194 deletions(-)
lib/std/math/ilogb.zig+110-128| ... | ... | @@ -15,175 +15,157 @@ const minInt = std.math.minInt; |
| 15 | 15 | /// |
| 16 | 16 | /// Special Cases: |
| 17 | 17 | /// - ilogb(+-inf) = maxInt(i32) |
| 18 | /// - ilogb(0) = maxInt(i32) | |
| 19 | /// - ilogb(nan) = maxInt(i32) | |
| 18 | /// - ilogb(+-0) = minInt(i32) | |
| 19 | /// - ilogb(nan) = minInt(i32) | |
| 20 | 20 | pub fn ilogb(x: anytype) i32 { |
| 21 | 21 | const T = @TypeOf(x); |
| 22 | return switch (T) { | |
| 23 | f32 => ilogb32(x), | |
| 24 | f64 => ilogb64(x), | |
| 25 | f128 => ilogb128(x), | |
| 26 | else => @compileError("ilogb not implemented for " ++ @typeName(T)), | |
| 27 | }; | |
| 22 | return ilogbX(T, x); | |
| 28 | 23 | } |
| 29 | 24 | |
| 30 | // TODO: unify these implementations with generics | |
| 25 | pub const fp_ilogbnan = minInt(i32); | |
| 26 | pub const fp_ilogb0 = minInt(i32); | |
| 31 | 27 | |
| 32 | // NOTE: Should these be exposed publicly? | |
| 33 | const fp_ilogbnan = -1 - @as(i32, maxInt(u32) >> 1); | |
| 34 | const fp_ilogb0 = fp_ilogbnan; | |
| 28 | fn ilogbX(comptime T: type, x: T) i32 { | |
| 29 | const typeWidth = @typeInfo(T).Float.bits; | |
| 30 | const significandBits = math.floatMantissaBits(T); | |
| 31 | const exponentBits = math.floatExponentBits(T); | |
| 35 | 32 | |
| 36 | fn ilogb32(x: f32) i32 { | |
| 37 | var u = @bitCast(u32, x); | |
| 38 | var e = @intCast(i32, (u >> 23) & 0xFF); | |
| 33 | const Z = std.meta.Int(.unsigned, typeWidth); | |
| 39 | 34 | |
| 40 | // TODO: We should be able to merge this with the lower check. | |
| 41 | if (math.isNan(x)) { | |
| 42 | return maxInt(i32); | |
| 43 | } | |
| 35 | const signBit = (@as(Z, 1) << (significandBits + exponentBits)); | |
| 36 | const maxExponent = ((1 << exponentBits) - 1); | |
| 37 | const exponentBias = (maxExponent >> 1); | |
| 44 | 38 | |
| 45 | if (e == 0) { | |
| 46 | u <<= 9; | |
| 47 | if (u == 0) { | |
| 48 | math.raiseInvalid(); | |
| 49 | return fp_ilogb0; | |
| 50 | } | |
| 39 | const absMask = signBit - 1; | |
| 51 | 40 | |
| 52 | // subnormal | |
| 53 | e = -0x7F; | |
| 54 | while (u >> 31 == 0) : (u <<= 1) { | |
| 55 | e -= 1; | |
| 56 | } | |
| 57 | return e; | |
| 58 | } | |
| 59 | ||
| 60 | if (e == 0xFF) { | |
| 61 | math.raiseInvalid(); | |
| 62 | if (u << 9 != 0) { | |
| 63 | return fp_ilogbnan; | |
| 64 | } else { | |
| 65 | return maxInt(i32); | |
| 66 | } | |
| 67 | } | |
| 68 | ||
| 69 | return e - 0x7F; | |
| 70 | } | |
| 71 | ||
| 72 | fn ilogb64(x: f64) i32 { | |
| 73 | var u = @bitCast(u64, x); | |
| 74 | var e = @intCast(i32, (u >> 52) & 0x7FF); | |
| 75 | ||
| 76 | if (math.isNan(x)) { | |
| 77 | return maxInt(i32); | |
| 78 | } | |
| 41 | var u = @bitCast(Z, x) & absMask; | |
| 42 | var e = @intCast(i32, u >> significandBits); | |
| 79 | 43 | |
| 80 | 44 | if (e == 0) { |
| 81 | u <<= 12; | |
| 82 | 45 | if (u == 0) { |
| 83 | 46 | math.raiseInvalid(); |
| 84 | 47 | return fp_ilogb0; |
| 85 | 48 | } |
| 86 | 49 | |
| 87 | // subnormal | |
| 88 | e = -0x3FF; | |
| 89 | while (u >> 63 == 0) : (u <<= 1) { | |
| 90 | e -= 1; | |
| 91 | } | |
| 92 | return e; | |
| 50 | // offset sign bit, exponent bits, and integer bit (if present) + bias | |
| 51 | const offset = 1 + exponentBits + @boolToInt(T == f80) - exponentBias; | |
| 52 | return offset - @intCast(i32, @clz(u)); | |
| 93 | 53 | } |
| 94 | 54 | |
| 95 | if (e == 0x7FF) { | |
| 55 | if (e == maxExponent) { | |
| 96 | 56 | math.raiseInvalid(); |
| 97 | if (u << 12 != 0) { | |
| 98 | return fp_ilogbnan; | |
| 99 | } else { | |
| 100 | return maxInt(i32); | |
| 101 | } | |
| 57 | if (u > @bitCast(Z, math.inf(T))) { | |
| 58 | return fp_ilogbnan; // u is a NaN | |
| 59 | } else return maxInt(i32); | |
| 102 | 60 | } |
| 103 | 61 | |
| 104 | return e - 0x3FF; | |
| 62 | return e - exponentBias; | |
| 105 | 63 | } |
| 106 | 64 | |
| 107 | fn ilogb128(x: f128) i32 { | |
| 108 | var u = @bitCast(u128, x); | |
| 109 | var e = @intCast(i32, (u >> 112) & 0x7FFF); | |
| 110 | ||
| 111 | if (math.isNan(x)) { | |
| 112 | return maxInt(i32); | |
| 113 | } | |
| 114 | ||
| 115 | if (e == 0) { | |
| 116 | u <<= 16; | |
| 117 | if (u == 0) { | |
| 118 | math.raiseInvalid(); | |
| 119 | return fp_ilogb0; | |
| 120 | } | |
| 121 | ||
| 122 | // subnormal x | |
| 123 | return ilogb128(x * 0x1p120) - 120; | |
| 124 | } | |
| 125 | ||
| 126 | if (e == 0x7FFF) { | |
| 127 | math.raiseInvalid(); | |
| 128 | if (u << 16 != 0) { | |
| 129 | return fp_ilogbnan; | |
| 130 | } else { | |
| 131 | return maxInt(i32); | |
| 132 | } | |
| 133 | } | |
| 134 | ||
| 135 | return e - 0x3FFF; | |
| 65 | test "type dispatch" { | |
| 66 | try expect(ilogb(@as(f32, 0.2)) == ilogbX(f32, 0.2)); | |
| 67 | try expect(ilogb(@as(f64, 0.2)) == ilogbX(f64, 0.2)); | |
| 136 | 68 | } |
| 137 | 69 | |
| 138 | test "type dispatch" { | |
| 139 | try expect(ilogb(@as(f32, 0.2)) == ilogb32(0.2)); | |
| 140 | try expect(ilogb(@as(f64, 0.2)) == ilogb64(0.2)); | |
| 70 | test "16" { | |
| 71 | try expect(ilogbX(f16, 0.0) == fp_ilogb0); | |
| 72 | try expect(ilogbX(f16, 0.5) == -1); | |
| 73 | try expect(ilogbX(f16, 0.8923) == -1); | |
| 74 | try expect(ilogbX(f16, 10.0) == 3); | |
| 75 | try expect(ilogbX(f16, -65504) == 15); | |
| 76 | try expect(ilogbX(f16, 2398.23) == 11); | |
| 77 | ||
| 78 | try expect(ilogbX(f16, 0x1p-1) == -1); | |
| 79 | try expect(ilogbX(f16, 0x1p-17) == -17); | |
| 80 | try expect(ilogbX(f16, 0x1p-24) == -24); | |
| 141 | 81 | } |
| 142 | 82 | |
| 143 | 83 | test "32" { |
| 144 | try expect(ilogb32(0.0) == fp_ilogb0); | |
| 145 | try expect(ilogb32(0.5) == -1); | |
| 146 | try expect(ilogb32(0.8923) == -1); | |
| 147 | try expect(ilogb32(10.0) == 3); | |
| 148 | try expect(ilogb32(-123984) == 16); | |
| 149 | try expect(ilogb32(2398.23) == 11); | |
| 84 | try expect(ilogbX(f32, 0.0) == fp_ilogb0); | |
| 85 | try expect(ilogbX(f32, 0.5) == -1); | |
| 86 | try expect(ilogbX(f32, 0.8923) == -1); | |
| 87 | try expect(ilogbX(f32, 10.0) == 3); | |
| 88 | try expect(ilogbX(f32, -123984) == 16); | |
| 89 | try expect(ilogbX(f32, 2398.23) == 11); | |
| 90 | ||
| 91 | try expect(ilogbX(f32, 0x1p-1) == -1); | |
| 92 | try expect(ilogbX(f32, 0x1p-122) == -122); | |
| 93 | try expect(ilogbX(f32, 0x1p-127) == -127); | |
| 150 | 94 | } |
| 151 | 95 | |
| 152 | 96 | test "64" { |
| 153 | try expect(ilogb64(0.0) == fp_ilogb0); | |
| 154 | try expect(ilogb64(0.5) == -1); | |
| 155 | try expect(ilogb64(0.8923) == -1); | |
| 156 | try expect(ilogb64(10.0) == 3); | |
| 157 | try expect(ilogb64(-123984) == 16); | |
| 158 | try expect(ilogb64(2398.23) == 11); | |
| 97 | try expect(ilogbX(f64, 0.0) == fp_ilogb0); | |
| 98 | try expect(ilogbX(f64, 0.5) == -1); | |
| 99 | try expect(ilogbX(f64, 0.8923) == -1); | |
| 100 | try expect(ilogbX(f64, 10.0) == 3); | |
| 101 | try expect(ilogbX(f64, -123984) == 16); | |
| 102 | try expect(ilogbX(f64, 2398.23) == 11); | |
| 103 | ||
| 104 | try expect(ilogbX(f64, 0x1p-1) == -1); | |
| 105 | try expect(ilogbX(f64, 0x1p-127) == -127); | |
| 106 | try expect(ilogbX(f64, 0x1p-1012) == -1012); | |
| 107 | try expect(ilogbX(f64, 0x1p-1023) == -1023); | |
| 108 | } | |
| 109 | ||
| 110 | test "80" { | |
| 111 | try expect(ilogbX(f80, 0.0) == fp_ilogb0); | |
| 112 | try expect(ilogbX(f80, 0.5) == -1); | |
| 113 | try expect(ilogbX(f80, 0.8923) == -1); | |
| 114 | try expect(ilogbX(f80, 10.0) == 3); | |
| 115 | try expect(ilogbX(f80, -123984) == 16); | |
| 116 | try expect(ilogbX(f80, 2398.23) == 11); | |
| 117 | ||
| 118 | try expect(ilogbX(f80, 0x1p-1) == -1); | |
| 119 | try expect(ilogbX(f80, 0x1p-127) == -127); | |
| 120 | try expect(ilogbX(f80, 0x1p-1023) == -1023); | |
| 121 | try expect(ilogbX(f80, 0x1p-16383) == -16383); | |
| 159 | 122 | } |
| 160 | 123 | |
| 161 | 124 | test "128" { |
| 162 | try expect(ilogb128(0.0) == fp_ilogb0); | |
| 163 | try expect(ilogb128(0.5) == -1); | |
| 164 | try expect(ilogb128(0.8923) == -1); | |
| 165 | try expect(ilogb128(10.0) == 3); | |
| 166 | try expect(ilogb128(-123984) == 16); | |
| 167 | try expect(ilogb128(2398.23) == 11); | |
| 125 | try expect(ilogbX(f128, 0.0) == fp_ilogb0); | |
| 126 | try expect(ilogbX(f128, 0.5) == -1); | |
| 127 | try expect(ilogbX(f128, 0.8923) == -1); | |
| 128 | try expect(ilogbX(f128, 10.0) == 3); | |
| 129 | try expect(ilogbX(f128, -123984) == 16); | |
| 130 | try expect(ilogbX(f128, 2398.23) == 11); | |
| 131 | ||
| 132 | try expect(ilogbX(f128, 0x1p-1) == -1); | |
| 133 | try expect(ilogbX(f128, 0x1p-127) == -127); | |
| 134 | try expect(ilogbX(f128, 0x1p-1023) == -1023); | |
| 135 | try expect(ilogbX(f128, 0x1p-16383) == -16383); | |
| 136 | } | |
| 137 | ||
| 138 | test "16 special" { | |
| 139 | try expect(ilogbX(f16, math.inf(f16)) == maxInt(i32)); | |
| 140 | try expect(ilogbX(f16, -math.inf(f16)) == maxInt(i32)); | |
| 141 | try expect(ilogbX(f16, 0.0) == minInt(i32)); | |
| 142 | try expect(ilogbX(f16, math.nan(f16)) == fp_ilogbnan); | |
| 168 | 143 | } |
| 169 | 144 | |
| 170 | 145 | test "32 special" { |
| 171 | try expect(ilogb32(math.inf(f32)) == maxInt(i32)); | |
| 172 | try expect(ilogb32(-math.inf(f32)) == maxInt(i32)); | |
| 173 | try expect(ilogb32(0.0) == minInt(i32)); | |
| 174 | try expect(ilogb32(math.nan(f32)) == maxInt(i32)); | |
| 146 | try expect(ilogbX(f32, math.inf(f32)) == maxInt(i32)); | |
| 147 | try expect(ilogbX(f32, -math.inf(f32)) == maxInt(i32)); | |
| 148 | try expect(ilogbX(f32, 0.0) == minInt(i32)); | |
| 149 | try expect(ilogbX(f32, math.nan(f32)) == fp_ilogbnan); | |
| 175 | 150 | } |
| 176 | 151 | |
| 177 | 152 | test "64 special" { |
| 178 | try expect(ilogb64(math.inf(f64)) == maxInt(i32)); | |
| 179 | try expect(ilogb64(-math.inf(f64)) == maxInt(i32)); | |
| 180 | try expect(ilogb64(0.0) == minInt(i32)); | |
| 181 | try expect(ilogb64(math.nan(f64)) == maxInt(i32)); | |
| 153 | try expect(ilogbX(f64, math.inf(f64)) == maxInt(i32)); | |
| 154 | try expect(ilogbX(f64, -math.inf(f64)) == maxInt(i32)); | |
| 155 | try expect(ilogbX(f64, 0.0) == minInt(i32)); | |
| 156 | try expect(ilogbX(f64, math.nan(f64)) == fp_ilogbnan); | |
| 157 | } | |
| 158 | ||
| 159 | test "80 special" { | |
| 160 | try expect(ilogbX(f80, math.inf(f80)) == maxInt(i32)); | |
| 161 | try expect(ilogbX(f80, -math.inf(f80)) == maxInt(i32)); | |
| 162 | try expect(ilogbX(f80, 0.0) == minInt(i32)); | |
| 163 | try expect(ilogbX(f80, math.nan(f80)) == fp_ilogbnan); | |
| 182 | 164 | } |
| 183 | 165 | |
| 184 | 166 | test "128 special" { |
| 185 | try expect(ilogb128(math.inf(f128)) == maxInt(i32)); | |
| 186 | try expect(ilogb128(-math.inf(f128)) == maxInt(i32)); | |
| 187 | try expect(ilogb128(0.0) == minInt(i32)); | |
| 188 | try expect(ilogb128(math.nan(f128)) == maxInt(i32)); | |
| 167 | try expect(ilogbX(f128, math.inf(f128)) == maxInt(i32)); | |
| 168 | try expect(ilogbX(f128, -math.inf(f128)) == maxInt(i32)); | |
| 169 | try expect(ilogbX(f128, 0.0) == minInt(i32)); | |
| 170 | try expect(ilogbX(f128, math.nan(f128)) == fp_ilogbnan); | |
| 189 | 171 | } |
lib/std/math/ldexp.zig+117-66| ... | ... | @@ -1,91 +1,142 @@ |
| 1 | // Ported from musl, which is licensed under the MIT license: | |
| 2 | // https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT | |
| 3 | // | |
| 4 | // https://git.musl-libc.org/cgit/musl/tree/src/math/ldexpf.c | |
| 5 | // https://git.musl-libc.org/cgit/musl/tree/src/math/ldexp.c | |
| 6 | ||
| 7 | 1 | const std = @import("std"); |
| 8 | 2 | const math = std.math; |
| 3 | const Log2Int = std.math.Log2Int; | |
| 9 | 4 | const assert = std.debug.assert; |
| 10 | 5 | const expect = std.testing.expect; |
| 11 | 6 | |
| 12 | 7 | /// Returns x * 2^n. |
| 13 | 8 | pub fn ldexp(x: anytype, n: i32) @TypeOf(x) { |
| 14 | var base = x; | |
| 15 | var shift = n; | |
| 16 | ||
| 17 | const T = @TypeOf(base); | |
| 9 | const T = @TypeOf(x); | |
| 18 | 10 | const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); |
| 19 | 11 | |
| 12 | const exponent_bits = math.floatExponentBits(T); | |
| 20 | 13 | const mantissa_bits = math.floatMantissaBits(T); |
| 21 | const exponent_min = math.floatExponentMin(T); | |
| 22 | const exponent_max = math.floatExponentMax(T); | |
| 23 | ||
| 24 | const exponent_bias = exponent_max; | |
| 25 | ||
| 26 | // fix double rounding errors in subnormal ranges | |
| 27 | // https://git.musl-libc.org/cgit/musl/commit/src/math/ldexp.c?id=8c44a060243f04283ca68dad199aab90336141db | |
| 28 | const scale_min_expo = exponent_min + mantissa_bits + 1; | |
| 29 | const scale_min = @bitCast(T, @as(TBits, scale_min_expo + exponent_bias) << mantissa_bits); | |
| 30 | const scale_max = @bitCast(T, @intCast(TBits, exponent_max + exponent_bias) << mantissa_bits); | |
| 31 | ||
| 32 | // scale `shift` within floating point limits, if possible | |
| 33 | // second pass is possible due to subnormal range | |
| 34 | // third pass always results in +/-0.0 or +/-inf | |
| 35 | if (shift > exponent_max) { | |
| 36 | base *= scale_max; | |
| 37 | shift -= exponent_max; | |
| 38 | if (shift > exponent_max) { | |
| 39 | base *= scale_max; | |
| 40 | shift -= exponent_max; | |
| 41 | if (shift > exponent_max) shift = exponent_max; | |
| 14 | const fractional_bits = math.floatFractionalBits(T); | |
| 15 | ||
| 16 | const max_biased_exponent = 2 * math.floatExponentMax(T); | |
| 17 | const mantissa_mask = @as(TBits, (1 << mantissa_bits) - 1); | |
| 18 | ||
| 19 | const repr = @bitCast(TBits, x); | |
| 20 | const sign_bit = repr & (1 << (exponent_bits + mantissa_bits)); | |
| 21 | ||
| 22 | if (math.isNan(x) or !math.isFinite(x)) | |
| 23 | return x; | |
| 24 | ||
| 25 | var exponent: i32 = @intCast(i32, (repr << 1) >> (mantissa_bits + 1)); | |
| 26 | if (exponent == 0) | |
| 27 | exponent += (@as(i32, exponent_bits) + @boolToInt(T == f80)) - @clz(repr << 1); | |
| 28 | ||
| 29 | if (n >= 0) { | |
| 30 | if (n > max_biased_exponent - exponent) { | |
| 31 | // Overflow. Return +/- inf | |
| 32 | return @bitCast(T, @bitCast(TBits, math.inf(T)) | sign_bit); | |
| 33 | } else if (exponent + n <= 0) { | |
| 34 | // Result is subnormal | |
| 35 | return @bitCast(T, (repr << @intCast(Log2Int(TBits), n)) | sign_bit); | |
| 36 | } else if (exponent <= 0) { | |
| 37 | // Result is normal, but needs shifting | |
| 38 | var result = @intCast(TBits, n + exponent) << mantissa_bits; | |
| 39 | result |= (repr << @intCast(Log2Int(TBits), 1 - exponent)) & mantissa_mask; | |
| 40 | return @bitCast(T, result | sign_bit); | |
| 42 | 41 | } |
| 43 | } else if (shift < exponent_min) { | |
| 44 | base *= scale_min; | |
| 45 | shift -= scale_min_expo; | |
| 46 | if (shift < exponent_min) { | |
| 47 | base *= scale_min; | |
| 48 | shift -= scale_min_expo; | |
| 49 | if (shift < exponent_min) shift = exponent_min; | |
| 42 | ||
| 43 | // Result needs no shifting | |
| 44 | return @bitCast(T, repr + (@intCast(TBits, n) << mantissa_bits)); | |
| 45 | } else { | |
| 46 | if (n <= -exponent) { | |
| 47 | if (n < -(mantissa_bits + exponent)) | |
| 48 | return @bitCast(T, sign_bit); // Severe underflow. Return +/- 0 | |
| 49 | ||
| 50 | // Result underflowed, we need to shift and round | |
| 51 | const shift = @intCast(Log2Int(TBits), math.min(-n, -(exponent + n) + 1)); | |
| 52 | const exact_tie: bool = @ctz(repr) == shift - 1; | |
| 53 | var result = repr & mantissa_mask; | |
| 54 | ||
| 55 | if (T != f80) // Include integer bit | |
| 56 | result |= @as(TBits, @boolToInt(exponent > 0)) << fractional_bits; | |
| 57 | result = @intCast(TBits, (result >> (shift - 1))); | |
| 58 | ||
| 59 | // Round result, including round-to-even for exact ties | |
| 60 | result = ((result + 1) >> 1) & ~@as(TBits, @boolToInt(exact_tie)); | |
| 61 | return @bitCast(T, result | sign_bit); | |
| 50 | 62 | } |
| 51 | } | |
| 52 | 63 | |
| 53 | return base * @bitCast(T, @intCast(TBits, shift + exponent_bias) << mantissa_bits); | |
| 64 | // Result is exact, and needs no shifting | |
| 65 | return @bitCast(T, repr - (@intCast(TBits, -n) << mantissa_bits)); | |
| 66 | } | |
| 54 | 67 | } |
| 55 | 68 | |
| 56 | 69 | test "math.ldexp" { |
| 57 | // TODO derive the various constants here with new maths API | |
| 58 | ||
| 59 | // basic usage | |
| 60 | try expect(ldexp(@as(f16, 1.5), 4) == 24.0); | |
| 61 | try expect(ldexp(@as(f32, 1.5), 4) == 24.0); | |
| 62 | try expect(ldexp(@as(f64, 1.5), 4) == 24.0); | |
| 63 | try expect(ldexp(@as(f128, 1.5), 4) == 24.0); | |
| 64 | 70 | |
| 65 | 71 | // subnormals |
| 66 | try expect(math.isNormal(ldexp(@as(f16, 1.0), -14))); | |
| 67 | try expect(!math.isNormal(ldexp(@as(f16, 1.0), -15))); | |
| 68 | try expect(math.isNormal(ldexp(@as(f32, 1.0), -126))); | |
| 69 | try expect(!math.isNormal(ldexp(@as(f32, 1.0), -127))); | |
| 70 | try expect(math.isNormal(ldexp(@as(f64, 1.0), -1022))); | |
| 71 | try expect(!math.isNormal(ldexp(@as(f64, 1.0), -1023))); | |
| 72 | try expect(math.isNormal(ldexp(@as(f128, 1.0), -16382))); | |
| 73 | try expect(!math.isNormal(ldexp(@as(f128, 1.0), -16383))); | |
| 74 | // unreliable due to lack of native f16 support, see talk on PR #8733 | |
| 75 | // try expect(ldexp(@as(f16, 0x1.1FFp-1), -14 - 9) == math.floatTrueMin(f16)); | |
| 72 | try expect(ldexp(@as(f16, 0x1.1FFp14), -14 - 9 - 15) == math.floatTrueMin(f16)); | |
| 76 | 73 | try expect(ldexp(@as(f32, 0x1.3FFFFFp-1), -126 - 22) == math.floatTrueMin(f32)); |
| 77 | 74 | try expect(ldexp(@as(f64, 0x1.7FFFFFFFFFFFFp-1), -1022 - 51) == math.floatTrueMin(f64)); |
| 75 | try expect(ldexp(@as(f80, 0x1.7FFFFFFFFFFFFFFEp-1), -16382 - 62) == math.floatTrueMin(f80)); | |
| 78 | 76 | try expect(ldexp(@as(f128, 0x1.7FFFFFFFFFFFFFFFFFFFFFFFFFFFp-1), -16382 - 111) == math.floatTrueMin(f128)); |
| 79 | 77 | |
| 80 | // float limits | |
| 81 | 78 | try expect(ldexp(math.floatMax(f32), -128 - 149) > 0.0); |
| 82 | 79 | try expect(ldexp(math.floatMax(f32), -128 - 149 - 1) == 0.0); |
| 83 | try expect(!math.isPositiveInf(ldexp(math.floatTrueMin(f16), 15 + 24))); | |
| 84 | try expect(math.isPositiveInf(ldexp(math.floatTrueMin(f16), 15 + 24 + 1))); | |
| 85 | try expect(!math.isPositiveInf(ldexp(math.floatTrueMin(f32), 127 + 149))); | |
| 86 | try expect(math.isPositiveInf(ldexp(math.floatTrueMin(f32), 127 + 149 + 1))); | |
| 87 | try expect(!math.isPositiveInf(ldexp(math.floatTrueMin(f64), 1023 + 1074))); | |
| 88 | try expect(math.isPositiveInf(ldexp(math.floatTrueMin(f64), 1023 + 1074 + 1))); | |
| 89 | try expect(!math.isPositiveInf(ldexp(math.floatTrueMin(f128), 16383 + 16494))); | |
| 90 | try expect(math.isPositiveInf(ldexp(math.floatTrueMin(f128), 16383 + 16494 + 1))); | |
| 80 | ||
| 81 | @setEvalBranchQuota(10_000); | |
| 82 | ||
| 83 | inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| { | |
| 84 | const fractional_bits = math.floatFractionalBits(T); | |
| 85 | ||
| 86 | const min_exponent = math.floatExponentMin(T); | |
| 87 | const max_exponent = math.floatExponentMax(T); | |
| 88 | const exponent_bias = max_exponent; | |
| 89 | ||
| 90 | // basic usage | |
| 91 | try expect(ldexp(@as(T, 1.5), 4) == 24.0); | |
| 92 | ||
| 93 | // normals -> subnormals | |
| 94 | try expect(math.isNormal(ldexp(@as(T, 1.0), min_exponent))); | |
| 95 | try expect(!math.isNormal(ldexp(@as(T, 1.0), min_exponent - 1))); | |
| 96 | ||
| 97 | // normals -> zero | |
| 98 | try expect(ldexp(@as(T, 1.0), min_exponent - fractional_bits) > 0.0); | |
| 99 | try expect(ldexp(@as(T, 1.0), min_exponent - fractional_bits - 1) == 0.0); | |
| 100 | ||
| 101 | // subnormals -> zero | |
| 102 | try expect(ldexp(math.floatTrueMin(T), 0) > 0.0); | |
| 103 | try expect(ldexp(math.floatTrueMin(T), -1) == 0.0); | |
| 104 | ||
| 105 | // subnormals -> subnormals | |
| 106 | try expect(ldexp(math.floatTrueMin(T), 3) == math.floatTrueMin(T) * 8); | |
| 107 | try expect(ldexp(math.floatTrueMin(T) * 8, -2) == math.floatTrueMin(T) * 2); | |
| 108 | try expect(ldexp(math.floatTrueMin(T) * 8, -3) == math.floatTrueMin(T)); | |
| 109 | ||
| 110 | // subnormals -> normals (+) | |
| 111 | try expect(ldexp(math.floatTrueMin(T), fractional_bits) == math.floatMin(T)); | |
| 112 | try expect(ldexp(math.floatTrueMin(T), fractional_bits - 1) == math.floatMin(T) * 0.5); | |
| 113 | ||
| 114 | // subnormals -> normals (-) | |
| 115 | try expect(ldexp(-math.floatTrueMin(T), fractional_bits) == -math.floatMin(T)); | |
| 116 | try expect(ldexp(-math.floatTrueMin(T), fractional_bits - 1) == -math.floatMin(T) * 0.5); | |
| 117 | ||
| 118 | // subnormals -> float limits (+inf) | |
| 119 | try expect(math.isFinite(ldexp(math.floatTrueMin(T), max_exponent + exponent_bias + fractional_bits - 1))); | |
| 120 | try expect(ldexp(math.floatTrueMin(T), max_exponent + exponent_bias + fractional_bits) == math.inf(T)); | |
| 121 | ||
| 122 | // subnormals -> float limits (-inf) | |
| 123 | try expect(math.isFinite(ldexp(-math.floatTrueMin(T), max_exponent + exponent_bias + fractional_bits - 1))); | |
| 124 | try expect(ldexp(-math.floatTrueMin(T), max_exponent + exponent_bias + fractional_bits) == -math.inf(T)); | |
| 125 | ||
| 126 | // infinity -> infinity | |
| 127 | try expect(ldexp(math.inf(T), math.maxInt(i32)) == math.inf(T)); | |
| 128 | try expect(ldexp(math.inf(T), math.minInt(i32)) == math.inf(T)); | |
| 129 | try expect(ldexp(math.inf(T), max_exponent) == math.inf(T)); | |
| 130 | try expect(ldexp(math.inf(T), min_exponent) == math.inf(T)); | |
| 131 | try expect(ldexp(-math.inf(T), math.maxInt(i32)) == -math.inf(T)); | |
| 132 | try expect(ldexp(-math.inf(T), math.minInt(i32)) == -math.inf(T)); | |
| 133 | ||
| 134 | // extremely large n | |
| 135 | try expect(ldexp(math.floatMax(T), math.maxInt(i32)) == math.inf(T)); | |
| 136 | try expect(ldexp(math.floatMax(T), -math.maxInt(i32)) == 0.0); | |
| 137 | try expect(ldexp(math.floatMax(T), math.minInt(i32)) == 0.0); | |
| 138 | try expect(ldexp(math.floatTrueMin(T), math.maxInt(i32)) == math.inf(T)); | |
| 139 | try expect(ldexp(math.floatTrueMin(T), -math.maxInt(i32)) == 0.0); | |
| 140 | try expect(ldexp(math.floatTrueMin(T), math.minInt(i32)) == 0.0); | |
| 141 | } | |
| 91 | 142 | } |