diff --git a/Project.toml b/Project.toml index 290ccca4c..36edfbd21 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "ChainRulesCore" uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" -version = "1.26.3" +version = "1.26.4" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/src/projection.jl b/src/projection.jl index 13d472879..90e11ddb4 100644 --- a/src/projection.jl +++ b/src/projection.jl @@ -168,6 +168,9 @@ ProjectTo(x::Complex{<:Integer}) = ProjectTo(float(x)) for T in (Float16, Float32, Float64, ComplexF16, ComplexF32, ComplexF64) @eval ProjectTo(::$T) = ProjectTo{$T}() end +@static if isdefined(Core, :BFloat16) + ProjectTo(::Core.BFloat16) = ProjectTo{Core.BFloat16}() +end # In these cases we can just `convert` as we know we are dealing with plain and simple types (::ProjectTo{T})(dx::AbstractFloat) where {T<:AbstractFloat} = convert(T, dx) diff --git a/test/projection.jl b/test/projection.jl index 2445b79bd..912131242 100644 --- a/test/projection.jl +++ b/test/projection.jl @@ -59,6 +59,14 @@ struct NoSuperType end @test ProjectTo(Dual(1.0, 2.0))(Tangent{Dual}(; value=1.0)) isa Tangent end + if isdefined(Core, :BFloat16) + @testset "BFloat16" begin + # Base defines no conversions to Core.BFloat16, so build one from its bits + x = reinterpret(Core.BFloat16, 0x3f80) + @test ProjectTo(x) === ProjectTo{Core.BFloat16}() + end + end + @testset "Base: arrays of numbers" begin pvec3 = ProjectTo([1, 2, 3]) @test pvec3(1.0:3.0) === 1.0:3.0