Skip to content
Closed
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
123 changes: 110 additions & 13 deletions src/float.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

"""
StaticFloat64{N}

StaticFloat64{N}
A statically sized `Float64`.
Use `StaticInt(N)` instead of `Val(N)` when you want it to behave like a number.
"""
Expand Down Expand Up @@ -41,22 +40,100 @@ Base.isone(::StaticFloat64) = false
Base.zero(::Type{T}) where {T<:StaticFloat64} = FloatZero()
Base.one(::Type{T}) where {T<:StaticFloat64} = FloatOne()

Base.@pure function fsub(::StaticFloat64{X}, ::StaticFloat64{Y}) where {X,Y}
return StaticFloat64{Base.sub_float(X, Y)::Float64}()
function fneg(::StaticFloat64{X}) where {X}
return StaticFloat64{Base.neg_float(X)::Float64}()
end

function fabs(::StaticFloat64{X}) where {X}
return StaticFloat64{Base.abs_float(X)::Float64}()
end

function fcopysign(::StaticFloat64{X}, ::StaticFloat64{Y}) where {X,Y}
return StaticFloat64{Base.copysign_float(X, Y)::Float64}()
end

function feq(::StaticFloat64{X}, ::StaticFloat64{Y}) where {X,Y}
return StaticFloat64{Base.eq_float(X, Y)::Float64}()
end

function fne(::StaticFloat64{X}, ::StaticFloat64{Y}) where {X,Y}
return StaticFloat64{Base.ne_float(X, Y)::Float64}()
end

function fle(::StaticFloat64{X}, ::StaticFloat64{Y}) where {X,Y}
return StaticFloat64{Base.le_float(X, Y)::Float64}()
end

function flt(::StaticFloat64{X}, ::StaticFloat64{Y}) where {X,Y}
return StaticFloat64{Base.lt_float(X, Y)::Float64}()
end

Base.@pure function fadd(::StaticFloat64{X}, ::StaticFloat64{Y}) where {X,Y}
function fadd(::StaticFloat64{X}, ::StaticFloat64{Y}) where {X,Y}
return StaticFloat64{Base.add_float(X, Y)::Float64}()
end

Base.@pure function fdiv(::StaticFloat64{X}, ::StaticFloat64{Y}) where {X,Y}
return StaticFloat64{Base.div_float(X, Y)::Float64}()
function fsub(::StaticFloat64{X}, ::StaticFloat64{Y}) where {X,Y}
return StaticFloat64{Base.sub_float(X, Y)::Float64}()
end

Base.@pure function fmul(::StaticFloat64{X}, ::StaticFloat64{Y}) where {X,Y}
function fmul(::StaticFloat64{X}, ::StaticFloat64{Y}) where {X,Y}
return StaticFloat64{Base.mul_float(X, Y)::Float64}()
end

function fdiv(::StaticFloat64{X}, ::StaticFloat64{Y}) where {X,Y}
return StaticFloat64{Base.div_float(X, Y)::Float64}()
end

function frem(::StaticFloat64{X}, ::StaticFloat64{Y}) where {X,Y}
return StaticFloat64{Base.rem_float(X, Y)::Float64}()
end

function fmuladd(::StaticFloat64{X}, ::StaticFloat64{Y}, ::StaticFloat64{Z}) where {X,Y,Z}
return StaticFloat64{Base.muladd_float(X,Y,Z)::Float64}()
end

function ffma(::StaticFloat64{X}, ::StaticFloat64{Y}, ::StaticFloat64{Z}) where {X,Y,Z}
return StaticFloat64{Base.fma_float(X,Y,Z)::Float64}()
end

Base.:(-)(x::FloatZero) = x
Base.:(-)(x::StaticFloat64{X}) where {X} = fneg(x)

Base.abs(x::FloatZero) = x
Base.abs(x::StaticFloat64{X}) where {X} = fabs(x)

Base.copysign(x::FloatZero, ::FloatZero) = x
Base.copysign(x::StaticFloat64{X}, ::FloatZero) where {X} = x
Base.copysign(::FloatZero, y::StaticFloat64{Y}) where {Y} = FloatZero
Base.copysign(x::StaticFloat64{X}, y::StaticFloat64{Y}) where {X,Y} = fcopysign(x,y )
Base.copysign(x::StaticFloat64{X}, y::StaticInt{Y}) where {X,Y} = copysign(x, float(y))
Base.copysign(x::StaticInt{X}, y::StaticFloat64{Y}) where {X,Y} = copysign(float(x), y)

Base.:(==)(x::FloatZero, ::FloatZero) = true
Base.:(==)(x::StaticFloat64{X}, ::FloatZero) where {X} = false
Base.:(==)(::FloatZero, y::StaticFloat64{Y}) where {Y} = false
Base.:(==)(x::StaticFloat64{X}, y::StaticFloat64{Y}) where {X,Y} = feq(x,y)
Base.:(==)(x::StaticFloat64{X}, y::StaticInt{Y}) where {X,Y} = ==(x, float(y))
Base.:(==)(x::StaticInt{X}, y::StaticFloat64{Y}) where {X,Y} = ==(float(x), y)

Base.:(!=)(x::FloatZero, ::FloatZero) = false
Base.:(!=)(x::StaticFloat64{X}, ::FloatZero) where {X} = true
Base.:(!=)(::FloatZero, y::StaticFloat64{Y}) where {Y} = true
Base.:(!=)(x::StaticFloat64{X}, y::StaticFloat64{Y}) where {X,Y} = fne(x,y)
Base.:(!=)(x::StaticFloat64{X}, y::StaticInt{Y}) where {X,Y} = !=(x, float(y))
Base.:(!=)(x::StaticInt{X}, y::StaticFloat64{Y}) where {X,Y} = !=(float(x), y)

Base.:(<=)(x::FloatZero, ::FloatZero) = true
Base.:(<=)(x::StaticFloat64{X}, y::StaticFloat64{Y}) where {X,Y} = fle(x,y)
Base.:(<=)(x::StaticFloat64{X}, y::StaticInt{Y}) where {X,Y} = <=(x, float(y))
Base.:(<=)(x::StaticInt{X}, y::StaticFloat64{Y}) where {X,Y} = <=(float(x), y)

Base.:(<)(x::FloatZero, ::FloatZero) = false
Base.:(<)(x::StaticFloat64{X}, y::StaticFloat64{Y}) where {X,Y} = flt(x,y)
Base.:(<)(x::StaticFloat64{X}, y::StaticInt{Y}) where {X,Y} = <(x, float(y))
Base.:(<)(x::StaticInt{X}, y::StaticFloat64{Y}) where {X,Y} = <(float(x), y)


Base.:+(x::StaticFloat64{X}, y::StaticFloat64{Y}) where {X,Y} = fadd(x, y)
Base.:+(x::StaticFloat64{X}, y::StaticInt{Y}) where {X,Y} = +(x, float(y))
Base.:+(x::StaticInt{X}, y::StaticFloat64{Y}) where {X,Y} = +(float(x), y)
Expand Down Expand Up @@ -92,10 +169,31 @@ Base.:*(x::StaticFloat64{X}, ::FloatOne) where {X} = x
Base.:*(::FloatOne, y::StaticFloat64{Y}) where {Y} = y
Base.:*(::FloatOne, y::FloatZero) = y


Base.:/(x::StaticFloat64{X}, y::StaticFloat64{Y}) where {X,Y} = fdiv(x, y)
Base.:/(x::StaticFloat64{X}, y::StaticInt{Y}) where {X,Y} = /(x, float(y))
Base.:/(x::StaticInt{X}, y::StaticFloat64{Y}) where {X,Y} = /(float(x), y)
Base.:(/)(x::StaticFloat64{X}, y::StaticFloat64{Y}) where {X,Y} = fdiv(x, y)
Base.:(/)(x::StaticFloat64{X}, y::StaticInt{Y}) where {X,Y} = /(x, float(y))
Base.:(/)(x::StaticInt{X}, y::StaticFloat64{Y}) where {X,Y} = /(float(x), y)

Base.:(%)(x::StaticFloat64{X}, y::StaticFloat64{Y}) where {X,Y} = frem(x, y)
Base.:(%)(x::StaticFloat64{X}, y::StaticInt{Y}) where {X,Y} = %(x, float(y))
Base.:(%)(x::StaticInt{X}, y::StaticFloat64{Y}) where {X,Y} = %(float(x), y)

Base.:fma(x::StaticFloat64{X}, y::StaticFloat64{Y}, z::StaticFloat64{Z}) where {X,Y,Z} = ffma(x, y, z)
Base.:fma(x::StaticFloat64{X}, y::StaticFloat64{Y}, z::StaticInt{Z}) where {X,Y,Z} = fma(x, y, float(z))
Base.:fma(x::StaticFloat64{X}, y::StaticInt{Y}, z::StaticFloat64{Z}) where {X,Y,Z} = fma(x, float(y), z)
Base.:fma(x::StaticInt{X}, y::StaticFloat64{Y}, z::StaticFloat64{Z}) where {X,Y,Z} = fma(float(x), y, z)
Base.:fma(x::StaticFloat64{X}, y::StaticInt{Y}, z::StaticInt{Z}) where {X,Y,Z} = fma(x, float(y), float(z))
Base.:fma(x::StaticInt{X}, y::StaticInt{Y}, z::StaticFloat64{Z}) where {X,Y,Z} = fma(float(x), float(y), z)
Base.:fma(x::StaticInt{X}, y::StaticFloat64{Y}, z::StaticInt{Z}) where {X,Y,Z} = fma(float(x), y, float(z))
Base.:fma(x::StaticInt{X}, y::StaticInt{Y}, z::StaticInt{Z}) where {X,Y,Z} = fma(float(x), float(y), float(z))

Base.:muladd(x::StaticFloat64{X}, y::StaticFloat64{Y}, z::StaticFloat64{Z}) where {X,Y,Z} = fmuladd(x, y, z)
Base.:muladd(x::StaticFloat64{X}, y::StaticFloat64{Y}, z::StaticInt{Z}) where {X,Y,Z} = muladd(x, y, float(z))
Base.:muladd(x::StaticFloat64{X}, y::StaticInt{Y}, z::StaticFloat64{Z}) where {X,Y,Z} = muladd(x, float(y), z)
Base.:muladd(x::StaticInt{X}, y::StaticFloat64{Y}, z::StaticFloat64{Z}) where {X,Y,Z} = muladd(float(x), y, z)
Base.:muladd(x::StaticFloat64{X}, y::StaticInt{Y}, z::StaticInt{Z}) where {X,Y,Z} = muladd(x, float(y), float(z))
Base.:muladd(x::StaticInt{X}, y::StaticInt{Y}, z::StaticFloat64{Z}) where {X,Y,Z} = muladd(float(x), float(y), z)
Base.:muladd(x::StaticInt{X}, y::StaticFloat64{Y}, z::StaticInt{Z}) where {X,Y,Z} = muladd(float(x), y, float(z))
Base.:muladd(x::StaticInt{X}, y::StaticInt{Y}, z::StaticInt{Z}) where {X,Y,Z} = muladd(float(x), float(y), float(z))

@generated Base.sqrt(::StaticInt{M}) where {M} = Expr(:call, Expr(:curly, :StaticFloat64, sqrt(M)))
@generated Base.sqrt(::StaticFloat64{M}) where {M} = Expr(:call, Expr(:curly, :StaticFloat64, sqrt(M)))
Expand All @@ -109,4 +207,3 @@ floortostaticint(x::AbstractFloat) = Base.fptosi(Int, x)
Base.:(^)(::StaticFloat64{x}, y::Float64) where {x} = exp2(log2(x) * y)

Base.inv(x::StaticFloat64{N}) where {N} = fdiv(one(x), x)