Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
- 'lts'
- '1'
- 'pre'
- 'nightly'
os:
- ubuntu-latest
- windows-latest
Expand All @@ -26,10 +27,10 @@ jobs:
- os: macos-latest
arch: aarch64
version: '1'
- os: macos-13
- os: macos-15-intel
arch: x64
version: '1'

steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v2
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "BFloat16s"
uuid = "ab4f0b2a-ad5b-11e8-123f-65d77653426b"
authors = ["Keno Fischer <[email protected]>"]
version = "0.5.1"
version = "0.5.2"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
16 changes: 12 additions & 4 deletions src/bfloat16.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ const llvm_storage = if isdefined(Core, :BFloat16)
else
false
end
const llvm_arithmetic = if llvm_storage
if llvm_storage
import Core: BFloat16
end
const llvm_arithmetic = if llvm_storage
if Sys.ARCH in [:x86_64, :i686] && Base.libllvm_version >= v"15"
true
elseif Sys.ARCH == :aarch64 && Base.libllvm_version >= v"19"
Expand Down Expand Up @@ -389,7 +391,15 @@ randexp(rng::AbstractRNG, ::Type{BFloat16}) = convert(BFloat16, randexp(rng))
bitstring(x::BFloat16) = bitstring(reinterpret(Unsigned, x))

# next/prevfloat
function Base.nextfloat(f::BFloat16, d::Integer)
@static if isdefined(Base, :_nextfloat) # JuliaLang#59668
Base._nextfloat(f::BFloat16, dneg::Bool, da::Integer) = _nextbfloat(f, dneg, da)
else
Base.nextfloat(f::BFloat16, d::Integer) = _nextbfloat(f, d < 0, uabs(d))
Base.prevfloat(f::BFloat16, d::Integer) = _nextbfloat(f, d > 0, uabs(d))
end

function _nextbfloat(f::BFloat16, dneg::Bool, da::Integer)
# da must be > 0
F = typeof(f)
fumax = reinterpret(Unsigned, F(Inf))
U = typeof(fumax)
Expand All @@ -399,8 +409,6 @@ function Base.nextfloat(f::BFloat16, d::Integer)
fneg = fi < 0
fu = unsigned(fi & typemax(fi))

dneg = d < 0
da = uabs(d)
if da > typemax(U)
fneg = dneg
fu = fumax
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ end
@test isinf(nextfloat(BFloat16s.InfB16))

@test isnan(prevfloat(BFloat16s.NaNB16))

@test nextfloat(BFloat16(1.0), UInt(5)) == nextfloat(BFloat16(1.0), 5)
@test prevfloat(BFloat16(1.0), UInt(5)) == prevfloat(BFloat16(1.0), 5)
@test nextfloat(BFloat16(0.0), typemax(UInt64)) == Inf
@test prevfloat(BFloat16(0.0), typemax(UInt64)) == -Inf
end

@testset "Decompose BFloat16" begin
Expand Down
Loading