Skip to content

replace for @eval loop with generic methods #45

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

Closed
wants to merge 2 commits into from
Closed
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
57 changes: 24 additions & 33 deletions src/ufixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ typealias UFixed16 UFixed{UInt16,16}

const UF = (UFixed8, UFixed10, UFixed12, UFixed14, UFixed16)

for (uf) in UF
T = rawtype(uf)
f = nbitsfrac(uf)
@eval reinterpret(::Type{UFixed{$T,$f}}, x::$T) = UFixed{$T,$f}(x, 0)
end
reinterpret{T<:Unsigned,f}(::Type{UFixed{T,f}}, x::T) = UFixed{T,f}(x, 0)

# The next lines mimic the floating-point literal syntax "3.2f0"
immutable UFixedConstructor{T,f} end
Expand All @@ -35,14 +31,8 @@ const uf14 = UFixedConstructor{UInt16,14}()
const uf16 = UFixedConstructor{UInt16,16}()

zero{T,f}(::Type{UFixed{T,f}}) = UFixed{T,f}(zero(T),0)
for uf in UF
TT = rawtype(uf)
f = nbitsfrac(uf)
T = UFixed{TT,f}
@eval begin
one(::Type{$T}) = $T($(2^f-1),0)
end
end
one{T, f}(::Type{UFixed{T,f}}) = UFixed{T,f}(2^f-1,0)

zero(x::UFixed) = zero(typeof(x))
one(x::UFixed) = one(typeof(x))
rawone(v) = reinterpret(one(v))
Expand Down Expand Up @@ -98,16 +88,20 @@ abs(x::UFixed) = x
# Functions
trunc{T<:UFixed}(x::T) = T(div(reinterpret(x), rawone(T))*rawone(T),0)
floor{T<:UFixed}(x::T) = trunc(x)
for T in UF
f = nbitsfrac(T)
R = rawtype(T)
roundmask = convert(R, 1<<(f-1))
k = 8*sizeof(R)-f
ceilmask = (typemax(R)<<k)>>k
@eval begin
round(x::$T) = (y = trunc(x); return convert(rawtype($T), reinterpret(x)-reinterpret(y))&$roundmask>0 ? $T(y+one($T)) : y)
ceil(x::$T) = (y = trunc(x); return convert(rawtype($T), reinterpret(x)-reinterpret(y))&$ceilmask >0 ? $T(y+one($T)) : y)
end

roundmask{T,f}(::Type{UFixed{T,f}}) = convert(T, 1<<(f-1))
function ceilmask{T,f}(::Type{UFixed{T,f}})
k = 8*sizeof(T)-f
return (typemax(T)<<k)>>k
end

function round{T<:UFixed}(x::T)
y = trunc(x)
return convert(rawtype(T), reinterpret(x)-reinterpret(y))&roundmask(T)>0 ? T(y+one(T)) : y
end
function ceil{T<:UFixed}(x::T)
y = trunc(x)
return convert(rawtype(T), reinterpret(x)-reinterpret(y))&ceilmask(T)>0 ? T(y+one(T)) : y
end

trunc{T<:Integer}(::Type{T}, x::UFixed) = convert(T, div(reinterpret(x), rawone(x)))
Expand Down Expand Up @@ -155,17 +149,14 @@ function decompose(x::UFixed)
end

# Promotions
for T in UF
promote_rule{T<:UFixed}(::Type{T}, ::Type{Float32}) = Float32
promote_rule{T<:UFixed}(::Type{T}, ::Type{Float64}) = Float64
promote_rule{T<:UFixed, TR<:Rational}(::Type{T}, ::Type{TR}) = TR

for Ti in (Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64)
Tp = eps(convert(Float32, typemax(Ti)))
@eval begin
promote_rule(::Type{$T}, ::Type{Float32}) = Float32
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)
Tp = eps(convert(Float32, typemax(Ti))) > eps(T) ? Float64 : Float32
@eval begin
promote_rule(::Type{$T}, ::Type{$Ti}) = $Tp
end
promote_rule{T<:UFixed}(::Type{T}, ::Type{$Ti}) = $Tp > eps(T) ? Float64 : Float32
end
end

Expand Down
8 changes: 4 additions & 4 deletions test/ufixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ using FixedPointNumbers, Base.Test
@test ufixed14(1.0) == 0x3fffuf14
@test ufixed12([2]) == UFixed12[0x1ffeuf12]

for T in FixedPointNumbers.UF
for T in (FixedPointNumbers.UF..., UFixed{UInt32,16})
@test zero(T) == 0
@test one(T) == 1
@test one(T) * one(T) == one(T)
@test typemin(T) == 0
@test realmin(T) == 0
@test eps(zero(T)) == eps(typemax(T))
@test sizeof(T) == 1 + (T != UFixed8)
@test sizeof(T) == sizeof(FixedPointNumbers.rawtype(T))
end
@test typemax(UFixed8) == 1
@test typemax(UFixed10) == typemax(UInt16)//(2^10-1)
Expand All @@ -51,7 +51,7 @@ x = UFixed8(0.5)
@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
for T in (FixedPointNumbers.UF..., UFixed{UInt32,16})
@test convert(Bool, zero(T)) == false
@test convert(Bool, one(T)) == true
@test convert(Bool, convert(T, 0.2)) == true
Expand All @@ -64,7 +64,7 @@ x = UFixed8(0b01010001, 0)
@test ~x == UFixed8(0b10101110, 0)
@test -x == 0xafuf8

for T in FixedPointNumbers.UF
for T in (FixedPointNumbers.UF..., UFixed{UInt32,16})
x = T(0x10,0)
y = T(0x25,0)
fx = convert(Float32, x)
Expand Down