From f9ee8b0138459e397dde31637e73a9736abd2569 Mon Sep 17 00:00:00 2001 From: Jeffrey Sarnoff Date: Thu, 19 Jun 2025 10:25:25 -0400 Subject: [PATCH 1/5] add fma --- src/bfloat16.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/bfloat16.jl b/src/bfloat16.jl index 79ef956..fdc02ba 100644 --- a/src/bfloat16.jl +++ b/src/bfloat16.jl @@ -5,7 +5,7 @@ import Base: isfinite, isnan, precision, iszero, eps, exponent_one, exponent_half, leading_zeros, signbit, exponent, significand, frexp, ldexp, round, Int16, Int32, Int64, - +, -, *, /, ^, ==, <, <=, inv, + +, -, *, /, ^, fma, ==, <, <=, inv, abs, abs2, uabs, sqrt, cbrt, exp, exp2, exp10, expm1, log, log2, log10, log1p, @@ -440,5 +440,7 @@ for F in (:abs, :abs2, :sqrt, :cbrt, end end +Base.fma(x::BFloat16, y::BFloat16, z::BFloat16) = BFloat16(fma(Float32(x), Float32(y), Float32(z))) + # irrationals BFloat16(x::AbstractIrrational) = BFloat16(Float32(x)::Float32) From 07fb44719104b5a0d38047f9881806541e81ad17 Mon Sep 17 00:00:00 2001 From: Jeffrey Sarnoff Date: Thu, 19 Jun 2025 10:27:24 -0400 Subject: [PATCH 2/5] test fma --- test/mathfuncs.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/mathfuncs.jl b/test/mathfuncs.jl index d4aed2d..2212780 100644 --- a/test/mathfuncs.jl +++ b/test/mathfuncs.jl @@ -13,6 +13,8 @@ for F in (:abs, :abs2, :sqrt, :cbrt, end end +@test fma(phi, invphi, invphi) == BFloat16(Float32(phi), Float32(invphi), Float32(invphi)) + for F in (:asec, :acsc, :cosh, :acosh, :acoth) @eval begin @test $F(phi) == BFloat16($F(Float32(phi))) From 7493d9f9959346450ffcb546b2bcdd71e2de003e Mon Sep 17 00:00:00 2001 From: Jeffrey Sarnoff Date: Fri, 20 Jun 2025 10:19:20 -0400 Subject: [PATCH 3/5] fix fma test --- test/mathfuncs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mathfuncs.jl b/test/mathfuncs.jl index 2212780..8207604 100644 --- a/test/mathfuncs.jl +++ b/test/mathfuncs.jl @@ -13,7 +13,7 @@ for F in (:abs, :abs2, :sqrt, :cbrt, end end -@test fma(phi, invphi, invphi) == BFloat16(Float32(phi), Float32(invphi), Float32(invphi)) +@test fma(phi, invphi, invphi) == BFloat16(fma(Float32(phi), Float32(invphi), Float32(invphi))) for F in (:asec, :acsc, :cosh, :acosh, :acoth) @eval begin From 786626017415845c73b3c7327d5b0b8c5647b6e1 Mon Sep 17 00:00:00 2001 From: Jeffrey Sarnoff Date: Fri, 20 Jun 2025 10:25:52 -0400 Subject: [PATCH 4/5] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index de04aeb..a009764 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "BFloat16s" uuid = "ab4f0b2a-ad5b-11e8-123f-65d77653426b" authors = ["Keno Fischer "] -version = "0.5.1" +version = "0.5.2" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" From faad6d0bd39892f63ee6013401595552951af420 Mon Sep 17 00:00:00 2001 From: Jeffrey Sarnoff Date: Fri, 20 Jun 2025 12:18:02 -0400 Subject: [PATCH 5/5] define fma using llvmcall --- src/bfloat16.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bfloat16.jl b/src/bfloat16.jl index fdc02ba..31d0576 100644 --- a/src/bfloat16.jl +++ b/src/bfloat16.jl @@ -440,7 +440,7 @@ for F in (:abs, :abs2, :sqrt, :cbrt, end end -Base.fma(x::BFloat16, y::BFloat16, z::BFloat16) = BFloat16(fma(Float32(x), Float32(y), Float32(z))) +Base.fma(x::BFloat16, y::BFloat16, z::BFloat16) = ccall("llvm.fma.bf16", llvmcall, BFloat16, (BFloat16, BFloat16, BFloat16), x, y, z) # irrationals BFloat16(x::AbstractIrrational) = BFloat16(Float32(x)::Float32)