Skip to content

Commit aa7edcf

Browse files
committed
Define Base.issubnormal(x:BFloat16)
1 parent 8dc1905 commit aa7edcf

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/bfloat16.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ Base.exponent_bits(::Type{BFloat16}) = 8
6363
Base.significand_bits(::Type{BFloat16}) = 7
6464
Base.signbit(x::BFloat16) = (reinterpret(Unsigned, x) & 0x8000) !== 0x0000
6565

66+
function Base.issubnormal(x::BFloat16)
67+
y = reinterpret(Unsigned, x)
68+
return (y & exponent_mask(BFloat16) == 0) & (y & significand_mask(BFloat16) != 0)
69+
end
70+
6671
function Base.significand(x::BFloat16)
6772
xu = reinterpret(Unsigned, x)
6873
xs = xu & ~sign_mask(BFloat16)

test/structure.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,15 @@ end
2222
@test significand(whole) == one(BFloat16)
2323

2424
# subnormal
25-
@test significand(reinterpret(BFloat16, 0b0000000000000001)) == one(BFloat16)
26-
25+
@testset "subnormal" begin
26+
allbfs = UInt16(1):typemax(UInt16)
27+
res = map(allbfs) do raw
28+
bf = reinterpret(BFloat16, raw)
29+
issubnormal(bf) == issubnormal(Float32(bf))
30+
end
31+
@test all(res)
32+
@test significand(reinterpret(BFloat16, 0b0000000000000001)) == one(BFloat16)
33+
end
2734
@test frexp(phi) == (BFloat16(0.80859375), 1)
2835
@test ldexp(BFloat16(0.80859375), 1) == phi
2936

0 commit comments

Comments
 (0)