|
| 1 | +using Test |
| 2 | +using Microfloats |
| 3 | + |
| 4 | +const TYPES = [ |
| 5 | + Microfloat(0, 3, 4), |
| 6 | + Microfloat(0, 4, 3), |
| 7 | + Microfloat(0, 3, 3), |
| 8 | + Microfloat(0, 4, 2), |
| 9 | + Microfloat(0, 5, 1), |
| 10 | + Microfloat(0, 3, 2), |
| 11 | + Microfloat(0, 2, 3), |
| 12 | + Microfloat(0, 2, 2), |
| 13 | + Microfloat(0, 3, 1), |
| 14 | + Microfloat(0, 1, 3), |
| 15 | + Microfloat(0, 2, 1), |
| 16 | + Microfloat(1, 3, 4), |
| 17 | + Microfloat(1, 4, 3), |
| 18 | + Microfloat(1, 3, 3), |
| 19 | + Microfloat(1, 4, 2), |
| 20 | + Microfloat(1, 5, 1), |
| 21 | + Microfloat(1, 3, 2), |
| 22 | + Microfloat(1, 2, 3), |
| 23 | + Microfloat(1, 2, 2), |
| 24 | + Microfloat(1, 3, 1), |
| 25 | + Microfloat(1, 1, 3), |
| 26 | + Microfloat(1, 2, 1), |
| 27 | +] |
| 28 | + |
| 29 | +@testset "IEEE microfloats: subnormals and rounding" begin |
| 30 | + for T in TYPES |
| 31 | + @testset "$T boundaries" begin |
| 32 | + bias = Microfloats.bias(T) |
| 33 | + M = Microfloats.n_mantissa_bits(T) |
| 34 | + mo = Microfloats.mantissa_offset(T) |
| 35 | + # Encoding for the minimum positive subnormal (mantissa LSB only) |
| 36 | + min_sub_u = UInt8(1) << mo |
| 37 | + min_sub = reinterpret(T, min_sub_u) |
| 38 | + |
| 39 | + # Real values |
| 40 | + min_sub_val = Float32(2.0)^(1 - bias - M) |
| 41 | + half = min_sub_val/2 |
| 42 | + just_below_half = prevfloat(half) |
| 43 | + just_above_half = nextfloat(half) |
| 44 | + just_below = prevfloat(min_sub_val) |
| 45 | + just_above = nextfloat(min_sub_val) |
| 46 | + |
| 47 | + # Exact min subnormal |
| 48 | + @test Float32(min_sub) == min_sub_val |
| 49 | + |
| 50 | + # Values well below half of min subnormal should round to +0 |
| 51 | + @test T(half/4) == zero(T) |
| 52 | + |
| 53 | + # Exactly half rounds to even -> zero; below half also zero |
| 54 | + @test T(half) == zero(T) |
| 55 | + @test T(just_below_half) == zero(T) |
| 56 | + |
| 57 | + # Values just above half of min subnormal should round to min subnormal |
| 58 | + @test T(just_above_half) == min_sub |
| 59 | + |
| 60 | + # Values just below min subnormal remain min subnormal after rounding up from Float32 |
| 61 | + @test T(just_below) == min_sub |
| 62 | + |
| 63 | + # Values just above min subnormal quantize to min subnormal or the next representable |
| 64 | + # depending on spacing; at least should be >= min_sub |
| 65 | + @test Float32(T(just_above)) >= min_sub_val |
| 66 | + end |
| 67 | + end |
| 68 | +end |
| 69 | + |
| 70 | +@testset "IEEE microfloats: monotonic Float32 mapping (canonical encodings)" begin |
| 71 | + for T in TYPES |
| 72 | + @testset "$T" begin |
| 73 | + vals = Tuple{UInt8,Float32,Any}[] |
| 74 | + mshift = Microfloats.mantissa_offset(T) |
| 75 | + mmask = UInt8(Microfloats.mantissa_mask(T)) |
| 76 | + for u in UInt8(0):UInt8(0xff) |
| 77 | + x = reinterpret(T, u) |
| 78 | + isnan(x) && continue |
| 79 | + # Only include canonical encodings: mantissa padding bits zero |
| 80 | + ((u & ~mmask) != (u & ~mmask & ~(UInt8(1)<<mshift - UInt8(1)))) && continue |
| 81 | + push!(vals, (u, Float32(x), x)) |
| 82 | + end |
| 83 | + sort!(vals, by = t -> t[2]) |
| 84 | + for i in 1:length(vals)-1 |
| 85 | + a = vals[i]; b = vals[i+1] |
| 86 | + if a[2] == b[2] |
| 87 | + # duplicate comes only from signed zeros |
| 88 | + @test iszero(a[3]) && iszero(b[3]) |
| 89 | + else |
| 90 | + @test a[2] < b[2] |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | +end |
0 commit comments