Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions for BFloat16 #11

Merged
merged 5 commits into from
Oct 21, 2019
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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ version = "0.1.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
48 changes: 42 additions & 6 deletions src/bfloat16.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import Base: isfinite, isnan, precision, iszero,
sign_mask, exponent_mask, exponent_one, exponent_half,
significand_mask,
+, -, *, /
+, -, *, /, ^

primitive type BFloat16 <: AbstractFloat 16 end
BFloat16(x::Integer) = convert(BFloat16, convert(Float32, x))

# Floating point property queries
for f in (:sign_mask, :exponent_mask, :exponent_one,
Expand Down Expand Up @@ -46,21 +45,38 @@ function BFloat16(x::Float32)
return reinterpret(BFloat16, (h >> 16) % UInt16)
end

# Conversion from Float64
function BFloat16(x::Float64)
BFloat16(Float32(x))
end

# Conversion from Integer
function BFloat16(x::Integer)
convert(BFloat16, convert(Float32, x))
end

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

# Expansion to Float64
function Base.Float64(x::BFloat16)
Float64(Float32(x))
end

# Truncation to integer types
Base.unsafe_trunc(T::Type{<:Integer}, x::BFloat16) = unsafe_trunc(T, Float32(x))

# Basic arithmetic
for f in (:+, :-, :*, :/)
@eval ($f)(a::BFloat16, b::BFloat16) = BFloat16($(f)(Float32(a), Float32(b)))
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))
abs(x::BFloat16) = reinterpret(BFloat16, reinterpret(UInt16, x) & 0x7fff)
Base.sqrt(x::BFloat16) = BFloat16(sqrt(Float32(x)))

# Floating point comparisoin
# Floating point comparison
function Base.:(==)(x::BFloat16, y::BFloat16)
ix = reinterpret(UInt16, x)
iy = reinterpret(UInt16, y)
Expand All @@ -75,11 +91,31 @@ function Base.:(==)(x::BFloat16, y::BFloat16)
return ix == iy
end

function Base.:(<)(x::BFloat16, y::BFloat16)
return Float32(x) < Float32(y)
end

function Base.:(<=)(x::BFloat16, y::BFloat16)
return Float32(x) <= Float32(y)
end

function Base.:(>)(x::BFloat16, y::BFloat16)
return Float32(x) > Float32(y)
end

function Base.:(>=)(x::BFloat16, y::BFloat16)
return Float32(x) >= Float32(y)
end

Base.widen(::Type{BFloat16}) = Float32
Base.promote_rule(::Type{Float32}, ::Type{BFloat16}) = Float32
Base.promote_rule(::Type{Float64}, ::Type{BFloat16}) = Float64
for t in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128)
@eval Base.promote_rule(::Type{BFloat16}, ::Type{$t}) = BFloat16
end

# Wide multiplication
Base.widemul(a::BFloat16, b::BFloat16) = Float32(a) * Float32(b)
Base.widemul(x::BFloat16, y::BFloat16) = Float32(x) * Float32(y)

# Showing
function Base.show(io::IO, x::BFloat16)
Expand Down
19 changes: 19 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Test, BFloat16s

@test BFloat16(1) < BFloat16(2)
@test BFloat16(1f0) < BFloat16(2f0)
@test BFloat16(1.0) < BFloat16(2.0)
@test BFloat16(1) <= BFloat16(2)
@test BFloat16(1f0) <= BFloat16(2f0)
@test BFloat16(1.0) <= BFloat16(2.0)
@test BFloat16(2) > BFloat16(1)
@test BFloat16(2f0) > BFloat16(1f0)
@test BFloat16(2) >= BFloat16(1)
@test BFloat16(2f0) >= BFloat16(1f0)
@test BFloat16(2.0) >= BFloat16(1.0)

@test abs(BFloat16(-10)) == BFloat16(10)
@test Float32(BFloat16(10)) == 1f1
@test Float64(BFloat16(10)) == 10.0
@test BFloat16(2) ^ BFloat16(4) == BFloat16(16)
@test sqrt(BFloat16(4f0)) == BFloat16(2f0)