Skip to content

Specialization of N0f16 -> Float16 conversion #364

Closed

Description

@kimikage

I have long suggested considering the use of Float16 or BFloat16 for image processing tasks where there is a risk of overflow.
I personally believe that, as a result of the increasing use of GPUs in the Julia ecosystem, we find ourselves in the ironic situation where GPU support in the graphics field has lagged behind.

Even if there are counterarguments regarding my understanding of the situation, I don't think there's any problem with improving the conversion to Float16.

The current N0f8->Float16 conversion is performed via Float32, which isn't too bad.

function Base.Float16(x::Normed{Ti,f}) where {Ti <: Union{UInt8, UInt16, UInt32}, f}
f == 1 ? Float16(x.i) : Float16(Float32(x))
end

julia> for i in 0x00:0xff
           f = Float16(i/big"255")
           n = Float16(reinterpret(N0f8, i))
           f === n && continue
           @show i, f, n
       end

On the other hand, converting from N0f16 to Float16 results in an error of 1ULP (0.5ULP).

julia> for i in 0x0000:0xffff
           f = Float16(i/big"65535")
           n = Float16(reinterpret(N0f16, i))
           f === n && continue
           @show i, f, n
       end
(i, f, n) = (0xffaf, Float16(0.9985), Float16(0.999))
(i, f, n) = (0xffef, Float16(0.9995), Float16(1.0))

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions