Skip to content

Fix type deprecations for 0.4 #26

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

Merged
merged 1 commit into from
Sep 16, 2015
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
6 changes: 3 additions & 3 deletions src/fixed32.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ abs{f}(x::Fixed32{f}) = Fixed32{f}(abs(x.i),0)

# conversions and promotions
convert{f}(::Type{Fixed32{f}}, x::Integer) = Fixed32{f}(x<<f,0)
convert{f}(::Type{Fixed32{f}}, x::FloatingPoint) = Fixed32{f}(trunc(Int32,x)<<f + round(Int32, rem(x,1)*(1<<f)),0)
convert{f}(::Type{Fixed32{f}}, x::AbstractFloat) = Fixed32{f}(trunc(Int32,x)<<f + round(Int32, rem(x,1)*(1<<f)),0)
convert{f}(::Type{Fixed32{f}}, x::Rational) = Fixed32{f}(x.num)/Fixed32{f}(x.den)

convert{f}(::Type{BigFloat}, x::Fixed32{f}) =
convert(BigFloat,x.i>>f) + convert(BigFloat,x.i&(1<<f - 1))/convert(BigFloat,1<<f)
convert{T<:FloatingPoint, f}(::Type{T}, x::Fixed32{f}) =
convert{T<:AbstractFloat, f}(::Type{T}, x::Fixed32{f}) =
convert(T,x.i>>f) + convert(T,x.i&(1<<f - 1))/convert(T,1<<f)

convert(::Type{Bool}, x::Fixed32) = x.i!=0
Expand All @@ -57,7 +57,7 @@ convert{T<:Rational, f}(::Type{T}, x::Fixed32{f}) =
convert(T, x.i>>f + (x.i&(1<<f-1))//(1<<f))

promote_rule{f,T<:Integer}(ft::Type{Fixed32{f}}, ::Type{T}) = ft
promote_rule{f,T<:FloatingPoint}(::Type{Fixed32{f}}, ::Type{T}) = T
promote_rule{f,T<:AbstractFloat}(::Type{Fixed32{f}}, ::Type{T}) = T
promote_rule{f,T}(::Type{Fixed32{f}}, ::Type{Rational{T}}) = Rational{T}

decompose{f}(x::Fixed32{f}) = x.i, -f, 1
Expand Down
34 changes: 17 additions & 17 deletions src/ufixed.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# UfixedBase{T,f} maps Uints from 0 to 2^f-1 to the range [0.0, 1.0]
# UfixedBase{T,f} maps UInts from 0 to 2^f-1 to the range [0.0, 1.0]
# For example, a Ufixed8 maps 0x00 to 0.0 and 0xff to 1.0

immutable UfixedBase{T<:Unsigned,f} <: Ufixed
Expand All @@ -8,31 +8,31 @@ immutable UfixedBase{T<:Unsigned,f} <: Ufixed
UfixedBase(x) = convert(UfixedBase{T,f}, x)
end

typealias Ufixed8 UfixedBase{Uint8,8}
typealias Ufixed10 UfixedBase{Uint16,10}
typealias Ufixed12 UfixedBase{Uint16,12}
typealias Ufixed14 UfixedBase{Uint16,14}
typealias Ufixed16 UfixedBase{Uint16,16}
typealias Ufixed8 UfixedBase{UInt8,8}
typealias Ufixed10 UfixedBase{UInt16,10}
typealias Ufixed12 UfixedBase{UInt16,12}
typealias Ufixed14 UfixedBase{UInt16,14}
typealias Ufixed16 UfixedBase{UInt16,16}

const UF = (Ufixed8, Ufixed10, Ufixed12, Ufixed14, Ufixed16)

rawtype{T,f}(::Type{UfixedBase{T,f}}) = T
nbitsfrac{T,f}(::Type{UfixedBase{T,f}}) = f

reinterpret(::Type{Ufixed8}, x::Uint8) = UfixedBase{Uint8,8}(x,0)
reinterpret(::Type{Ufixed8}, x::UInt8) = UfixedBase{UInt8,8}(x,0)
for (T,f) in ((Ufixed10,10),(Ufixed12,12),(Ufixed14,14),(Ufixed16,16))
@eval reinterpret(::Type{$T}, x::Uint16) = UfixedBase{Uint16,$f}(x, 0)
@eval reinterpret(::Type{$T}, x::UInt16) = UfixedBase{UInt16,$f}(x, 0)
end


# The next lines mimic the floating-point literal syntax "3.2f0"
immutable UfixedConstructor{T,f} end
*{T,f}(n::Integer, ::UfixedConstructor{T,f}) = UfixedBase{T,f}(n,0)
const uf8 = UfixedConstructor{Uint8,8}()
const uf10 = UfixedConstructor{Uint16,10}()
const uf12 = UfixedConstructor{Uint16,12}()
const uf14 = UfixedConstructor{Uint16,14}()
const uf16 = UfixedConstructor{Uint16,16}()
const uf8 = UfixedConstructor{UInt8,8}()
const uf10 = UfixedConstructor{UInt16,10}()
const uf12 = UfixedConstructor{UInt16,12}()
const uf14 = UfixedConstructor{UInt16,14}()
const uf16 = UfixedConstructor{UInt16,16}()

zero{T,f}(::Type{UfixedBase{T,f}}) = UfixedBase{T,f}(zero(T),0)
for T in UF
Expand All @@ -48,7 +48,7 @@ rawone(v) = reinterpret(one(v))
# Conversions
convert{T<:Ufixed}(::Type{T}, x::T) = x
convert{T1<:Ufixed}(::Type{T1}, x::Ufixed) = reinterpret(T1, round(rawtype(T1), (rawone(T1)/rawone(x))*reinterpret(x)))
convert(::Type{Ufixed16}, x::Ufixed8) = reinterpret(Ufixed16, convert(Uint16, 0x0101*reinterpret(x)))
convert(::Type{Ufixed16}, x::Ufixed8) = reinterpret(Ufixed16, convert(UInt16, 0x0101*reinterpret(x)))
convert{T<:Ufixed}(::Type{T}, x::Real) = T(round(rawtype(T), rawone(T)*x),0)

ufixed8(x) = convert(Ufixed8, x)
Expand All @@ -65,7 +65,7 @@ ufixed16(x) = convert(Ufixed16, x)


convert(::Type{BigFloat}, x::Ufixed) = reinterpret(x)*(1/BigFloat(rawone(x)))
convert{T<:FloatingPoint}(::Type{T}, x::Ufixed) = reinterpret(x)*(1/convert(T, rawone(x)))
convert{T<:AbstractFloat}(::Type{T}, x::Ufixed) = reinterpret(x)*(1/convert(T, rawone(x)))
convert(::Type{Bool}, x::Ufixed) = x == zero(x) ? false : true
convert{T<:Integer}(::Type{T}, x::Ufixed) = convert(T, x*(1/one(T)))
convert{Ti<:Integer}(::Type{Rational{Ti}}, x::Ufixed) = convert(Ti, reinterpret(x))//convert(Ti, rawone(x))
Expand Down Expand Up @@ -119,7 +119,7 @@ isfinite(x::Ufixed) = true
isnan(x::Ufixed) = false
isinf(x::Ufixed) = false

bswap{f}(x::UfixedBase{Uint8,f}) = x
bswap{f}(x::UfixedBase{UInt8,f}) = x
bswap(x::Ufixed) = typeof(x)(bswap(reinterpret(x)),0)

for f in (:div, :fld, :rem, :mod, :mod1, :rem1, :fld1, :min, :max)
Expand Down Expand Up @@ -157,7 +157,7 @@ for T in UF
promote_rule(::Type{$T}, ::Type{Float64}) = Float64
promote_rule{TR<:Rational}(::Type{$T}, ::Type{TR}) = TR
end
for Ti in (Int8, Uint8, Int16, Uint16, Int32, Uint32, Int64, Uint64)
for Ti in (Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64)
Tp = eps(convert(Float32, typemax(Ti))) > eps(T) ? Float64 : Float32
@eval begin
promote_rule(::Type{$T}, ::Type{$Ti}) = $Tp
Expand Down
32 changes: 16 additions & 16 deletions test/ufixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,30 @@ for T in FixedPointNumbers.UF
@test sizeof(T) == 1 + (T != Ufixed8)
end
@test typemax(Ufixed8) == 1
@test typemax(Ufixed10) == typemax(Uint16)//(2^10-1)
@test typemax(Ufixed12) == typemax(Uint16)//(2^12-1)
@test typemax(Ufixed14) == typemax(Uint16)//(2^14-1)
@test typemax(Ufixed10) == typemax(UInt16)//(2^10-1)
@test typemax(Ufixed12) == typemax(UInt16)//(2^12-1)
@test typemax(Ufixed14) == typemax(UInt16)//(2^14-1)
@test typemax(Ufixed16) == 1
@test typemax(Ufixed10) == typemax(Uint16) // (2^10-1)
@test typemax(Ufixed12) == typemax(Uint16) // (2^12-1)
@test typemax(Ufixed14) == typemax(Uint16) // (2^14-1)
@test typemax(Ufixed10) == typemax(UInt16) // (2^10-1)
@test typemax(Ufixed12) == typemax(UInt16) // (2^12-1)
@test typemax(Ufixed14) == typemax(UInt16) // (2^14-1)

x = Ufixed8(0.5)
@test isfinite(x) == true
@test isnan(x) == false
@test isinf(x) == false

@test convert(Ufixed8, 1.1/typemax(Uint8)) == eps(Ufixed8)
@test convert(Ufixed10, 1.1/typemax(Uint16)*64) == eps(Ufixed10)
@test convert(Ufixed12, 1.1/typemax(Uint16)*16) == eps(Ufixed12)
@test convert(Ufixed14, 1.1/typemax(Uint16)*4) == eps(Ufixed14)
@test convert(Ufixed16, 1.1/typemax(Uint16)) == eps(Ufixed16)
@test convert(Ufixed8, 1.1/typemax(UInt8)) == eps(Ufixed8)
@test convert(Ufixed10, 1.1/typemax(UInt16)*64) == eps(Ufixed10)
@test convert(Ufixed12, 1.1/typemax(UInt16)*16) == eps(Ufixed12)
@test convert(Ufixed14, 1.1/typemax(UInt16)*4) == eps(Ufixed14)
@test convert(Ufixed16, 1.1/typemax(UInt16)) == eps(Ufixed16)

@test convert(Ufixed8, 1.1f0/typemax(Uint8)) == eps(Ufixed8)
@test convert(Ufixed8, 1.1f0/typemax(UInt8)) == eps(Ufixed8)

@test convert(Float64, eps(Ufixed8)) == 1/typemax(Uint8)
@test convert(Float32, eps(Ufixed8)) == 1.0f0/typemax(Uint8)
@test convert(BigFloat, eps(Ufixed8)) == BigFloat(1)/typemax(Uint8)
@test convert(Float64, eps(Ufixed8)) == 1/typemax(UInt8)
@test convert(Float32, eps(Ufixed8)) == 1.0f0/typemax(UInt8)
@test convert(BigFloat, eps(Ufixed8)) == BigFloat(1)/typemax(UInt8)
for T in FixedPointNumbers.UF
@test convert(Bool, zero(T)) == false
@test convert(Bool, one(T)) == true
Expand Down Expand Up @@ -123,7 +123,7 @@ function generic_scale!(C::AbstractArray, X::AbstractArray, s::Number)
C
end

a = rand(Uint8, 10)
a = rand(UInt8, 10)
rfloat = similar(a, Float32)
rfixed = similar(rfloat)
af8 = reinterpret(Ufixed8, a)
Expand Down