Skip to content

Commit

Permalink
Merge branch 'master' into mk/decompose
Browse files Browse the repository at this point in the history
  • Loading branch information
milankl authored Oct 31, 2023
2 parents 733a67f + a42c4fa commit fe6ddca
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 73 deletions.
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
12 changes: 8 additions & 4 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@ jobs:
matrix:
version:
- '1.6'
# - 'nightly'
- '1.7'
- '1.8'
- '1.9'
- '1.10.0-beta3'
- 'nightly'
os:
- ubuntu-latest
- macOS-latest
- windows-latest
arch:
- x64
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: actions/cache@v1
- uses: actions/cache@v3
env:
cache-name: cache-artifacts
with:
Expand All @@ -40,6 +44,6 @@ jobs:
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v1
- uses: codecov/codecov-action@v3
with:
file: lcov.info
5 changes: 5 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
coverage:
status:
patch: false
project: false
changes: false
1 change: 0 additions & 1 deletion src/BFloat16s.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ export BFloat16, LowPrecArray

include("bfloat16.jl")
include("lowprecarrays.jl")
include("printf.jl")

end # module BFloat16s
86 changes: 48 additions & 38 deletions src/bfloat16.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Base: isfinite, isnan, precision, iszero, eps,
signbit, exponent, significand, frexp, ldexp,
round, Int16, Int32, Int64,
+, -, *, /, ^, ==, <, <=, >=, >, !=, inv,
abs, abs2, sqrt, cbrt,
abs, abs2, uabs, sqrt, cbrt,
exp, exp2, exp10, expm1,
log, log2, log10, log1p,
sin, cos, tan, csc, sec, cot,
Expand All @@ -17,6 +17,9 @@ import Base: isfinite, isnan, precision, iszero, eps,

primitive type BFloat16 <: AbstractFloat 16 end

Base.reinterpret(::Type{Unsigned}, x::BFloat16) = reinterpret(UInt16, x)
Base.reinterpret(::Type{Signed}, x::BFloat16) = reinterpret(Int16, x)

# Floating point property queries
for f in (:sign_mask, :exponent_mask, :exponent_one,
:exponent_half, :significand_mask)
Expand All @@ -26,21 +29,21 @@ end
Base.exponent_bias(::Type{BFloat16}) = 127
Base.exponent_bits(::Type{BFloat16}) = 8
Base.significand_bits(::Type{BFloat16}) = 7
Base.signbit(x::BFloat16) = (reinterpret(UInt16, x) & 0x8000) !== 0x0000
Base.signbit(x::BFloat16) = (reinterpret(Unsigned, x) & 0x8000) !== 0x0000

function Base.significand(x::BFloat16)
result = abs_significand(x)
ifelse(signbit(x), -result, result)
end

@inline function abs_significand(x::BFloat16)
usig = Base.significand_mask(BFloat16) & reinterpret(UInt16, x)
usig = Base.significand_mask(BFloat16) & reinterpret(Unsigned, x)
isig = Int16(usig)
1 + isig / BFloat16(2)^7
end

Base.exponent(x::BFloat16) =
((reinterpret(UInt16, x) & Base.exponent_mask(BFloat16)) >> 7) - Base.exponent_bias(BFloat16)
((reinterpret(Unsigned, x) & Base.exponent_mask(BFloat16)) >> 7) - Base.exponent_bias(BFloat16)

function Base.decompose(x::BFloat16)::NTuple{3,Int}
isnan(x) && return 0, 0, 0
Expand All @@ -67,9 +70,9 @@ function Base.rem(x::BFloat16, ::Type{T}) where {T<:Integer}
T(trunc(x))
end

iszero(x::BFloat16) = reinterpret(UInt16, x) & ~sign_mask(BFloat16) == 0x0000
isfinite(x::BFloat16) = (reinterpret(UInt16,x) & exponent_mask(BFloat16)) != exponent_mask(BFloat16)
isnan(x::BFloat16) = (reinterpret(UInt16,x) & ~sign_mask(BFloat16)) > exponent_mask(BFloat16)
iszero(x::BFloat16) = reinterpret(Unsigned, x) & ~sign_mask(BFloat16) == 0x0000
isfinite(x::BFloat16) = (reinterpret(Unsigned,x) & exponent_mask(BFloat16)) != exponent_mask(BFloat16)
isnan(x::BFloat16) = (reinterpret(Unsigned,x) & ~sign_mask(BFloat16)) > exponent_mask(BFloat16)
precision(::Type{BFloat16}) = 8
eps(::Type{BFloat16}) = Base.bitcast(BFloat16, 0x3c00)

Expand Down Expand Up @@ -140,7 +143,7 @@ end

# Expansion to Float32
function Base.Float32(x::BFloat16)
reinterpret(Float32, UInt32(reinterpret(UInt16, x)) << 16)
reinterpret(Float32, UInt32(reinterpret(Unsigned, x)) << 16)
end

# Expansion to Float64
Expand All @@ -156,7 +159,7 @@ Base.trunc(::Type{T}, x::BFloat16) where {T<:Integer} = trunc(T, Float32(x))
for f in (:+, :-, :*, :/, :^)
@eval ($f)(x::BFloat16, y::BFloat16) = BFloat16($(f)(Float32(x), Float32(y)))
end
-(x::BFloat16) = reinterpret(BFloat16, reinterpret(UInt16, x) sign_mask(BFloat16))
-(x::BFloat16) = reinterpret(BFloat16, reinterpret(Unsigned, x) sign_mask(BFloat16))
^(x::BFloat16, y::Integer) = BFloat16(^(Float32(x), y))

const ZeroBFloat16 = BFloat16(0.0f0)
Expand All @@ -168,8 +171,8 @@ inv(x::BFloat16) = one(BFloat16) / x

# Floating point comparison
function ==(x::BFloat16, y::BFloat16)
ix = reinterpret(UInt16, x)
iy = reinterpret(UInt16, y)
ix = reinterpret(Unsigned, x)
iy = reinterpret(Unsigned, y)
# NaNs (isnan(x) || isnan(y))
if (ix|iy)&~sign_mask(BFloat16) > exponent_mask(BFloat16)
return false
Expand Down Expand Up @@ -216,37 +219,45 @@ randn(rng::AbstractRNG, ::Type{BFloat16}) = convert(BFloat16, randn(rng))
randexp(rng::AbstractRNG, ::Type{BFloat16}) = convert(BFloat16, randexp(rng))

# Bitstring
bitstring(x::BFloat16) = bitstring(reinterpret(UInt16, x))
bitstring(x::BFloat16) = bitstring(reinterpret(Unsigned, x))

# next/prevfloat
function Base.nextfloat(x::BFloat16)
if isfinite(x)
ui = reinterpret(UInt16,x)
if ui < 0x8000 # positive numbers
return reinterpret(BFloat16,ui+0x0001)
elseif ui == 0x8000 # =-zero(T)
return reinterpret(BFloat16,0x0001)
else # negative numbers
return reinterpret(BFloat16,ui-0x0001)
function Base.nextfloat(f::BFloat16, d::Integer)
F = typeof(f)
fumax = reinterpret(Unsigned, F(Inf))
U = typeof(fumax)

isnan(f) && return f
fi = reinterpret(Signed, f)
fneg = fi < 0
fu = unsigned(fi & typemax(fi))

dneg = d < 0
da = uabs(d)
if da > typemax(U)
fneg = dneg
fu = fumax
else
du = da % U
if fneg dneg
if du > fu
fu = min(fumax, du - fu)
fneg = !fneg
else
fu = fu - du
end
else
if fumax - fu < du
fu = fumax
else
fu = fu + du
end
end
else # NaN / Inf case
return x
end
end

function Base.prevfloat(x::BFloat16)
if isfinite(x)
ui = reinterpret(UInt16,x)
if ui == 0x0000 # =zero(T)
return reinterpret(BFloat16,0x8001)
elseif ui < 0x8000 # positive numbers
return reinterpret(BFloat16,ui-0x0001)
else # negative numbers
return reinterpret(BFloat16,ui+0x0001)
end
else # NaN / Inf case
return x
if fneg
fu |= sign_mask(F)
end
reinterpret(F, fu)
end

# math functions
Expand All @@ -261,4 +272,3 @@ for F in (:abs, :abs2, :sqrt, :cbrt,
Base.$F(x::BFloat16) = BFloat16($F(Float32(x)))
end
end

28 changes: 0 additions & 28 deletions src/printf.jl

This file was deleted.

22 changes: 20 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ end
@test isinf(nextfloat(BFloat16s.InfB16))

@test isnan(prevfloat(BFloat16s.NaNB16))
@test isinf(prevfloat(BFloat16s.InfB16))
end

@testset "Decompose BFloat16" begin
Expand All @@ -139,6 +138,25 @@ end
end
end


@testset "Next/prevfloat(x,::Integer)" begin

x = one(BFloat16)
@test x == prevfloat(nextfloat(x,100),100)
@test x == nextfloat(prevfloat(x,100),100)

x = -one(BFloat16)
@test x == prevfloat(nextfloat(x,100),100)
@test x == nextfloat(prevfloat(x,100),100)

x = one(BFloat16)
@test nextfloat(x,5) == prevfloat(x,-5)
@test prevfloat(x,-5) == nextfloat(x,5)

@test isinf(nextfloat(floatmax(BFloat16),5))
@test prevfloat(floatmin(BFloat16),2^8) < 0
@test nextfloat(-floatmin(BFloat16),2^8) > 0
end

include("structure.jl")
include("mathfuncs.jl")

0 comments on commit fe6ddca

Please sign in to comment.