Skip to content

Commit

Permalink
Define Base.issubnormal(x:BFloat16)
Browse files Browse the repository at this point in the history
  • Loading branch information
christiangnrd committed Mar 7, 2025
1 parent 8dc1905 commit aa7edcf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/bfloat16.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ Base.exponent_bits(::Type{BFloat16}) = 8
Base.significand_bits(::Type{BFloat16}) = 7
Base.signbit(x::BFloat16) = (reinterpret(Unsigned, x) & 0x8000) !== 0x0000

function Base.issubnormal(x::BFloat16)
y = reinterpret(Unsigned, x)
return (y & exponent_mask(BFloat16) == 0) & (y & significand_mask(BFloat16) != 0)
end

function Base.significand(x::BFloat16)
xu = reinterpret(Unsigned, x)
xs = xu & ~sign_mask(BFloat16)
Expand Down
11 changes: 9 additions & 2 deletions test/structure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ end
@test significand(whole) == one(BFloat16)

# subnormal
@test significand(reinterpret(BFloat16, 0b0000000000000001)) == one(BFloat16)

@testset "subnormal" begin
allbfs = UInt16(1):typemax(UInt16)
res = map(allbfs) do raw
bf = reinterpret(BFloat16, raw)
issubnormal(bf) == issubnormal(Float32(bf))
end
@test all(res)
@test significand(reinterpret(BFloat16, 0b0000000000000001)) == one(BFloat16)
end
@test frexp(phi) == (BFloat16(0.80859375), 1)
@test ldexp(BFloat16(0.80859375), 1) == phi

Expand Down

0 comments on commit aa7edcf

Please sign in to comment.