Skip to content

Commit c2131d8

Browse files
committed
Commonize reinterpret and float between Fixed and Normed
1 parent c6f8621 commit c2131d8

File tree

3 files changed

+3
-7
lines changed

3 files changed

+3
-7
lines changed

src/FixedPointNumbers.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ include("utilities.jl")
3636
# reinterpretation
3737
reinterpret(x::FixedPoint) = x.i
3838
reinterpret(::Type{T}, x::FixedPoint{T,f}) where {T,f} = x.i
39+
reinterpret(::Type{X}, x::T) where {T <: Integer, X <: FixedPoint{T}} = X(x, 0)
3940

4041
# static parameters
4142
nbitsfrac(::Type{X}) where {T, f, X <: FixedPoint{T,f}} = f
@@ -113,6 +114,8 @@ floattype(::Type{X}) where {T <: ShortInts, X <: FixedPoint{T}} = Float32
113114
floattype(::Type{X}) where {T <: Integer, X <: FixedPoint{T}} = Float64
114115
floattype(::Type{X}) where {T <: LongInts, X <: FixedPoint{T}} = BigFloat
115116

117+
float(x::FixedPoint) = convert(floattype(x), x)
118+
116119
for f in (:zero, :oneunit, :one, :eps, :rawone, :rawtype, :floattype)
117120
@eval begin
118121
$f(x::FixedPoint) = $f(typeof(x))

src/fixed.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ Fixed{T,f}(x::Integer) where {T,f} = Fixed{T,f}(round(T, convert(widen1(T),x)<<f
2525
Fixed{T,f}(x::AbstractFloat) where {T,f} = Fixed{T,f}(round(T, trunc(widen1(T),x)<<f + rem(x,1)*(one(widen1(T))<<f)),0)
2626
Fixed{T,f}(x::Rational) where {T,f} = Fixed{T,f}(x.num)/Fixed{T,f}(x.den)
2727

28-
reinterpret(::Type{Fixed{T,f}}, x::T) where {T <: Signed,f} = Fixed{T,f}(x, 0)
29-
3028
typechar(::Type{X}) where {X <: Fixed} = 'Q'
3129
signbits(::Type{X}) where {X <: Fixed} = 1
3230

@@ -73,7 +71,6 @@ end
7371
rem(x::Integer, ::Type{Fixed{T,f}}) where {T,f} = Fixed{T,f}(rem(x,T)<<f,0)
7472
rem(x::Real, ::Type{Fixed{T,f}}) where {T,f} = Fixed{T,f}(rem(Integer(trunc(x)),T)<<f + rem(Integer(round(rem(x,1)*(one(widen1(T))<<f))),T),0)
7573

76-
float(x::Fixed) = convert(floattype(x), x)
7774

7875
Base.BigFloat(x::Fixed{T,f}) where {T,f} =
7976
BigFloat(x.i>>f) + BigFloat(x.i&(one(widen1(T))<<f - 1))/BigFloat(one(widen1(T))<<f)

src/normed.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ for T in (UInt8, UInt16, UInt32, UInt64)
3535
end
3636
end
3737

38-
reinterpret(::Type{Normed{T,f}}, x::T) where {T <: Unsigned,f} = Normed{T,f}(x, 0)
39-
4038
function rawone(::Type{Normed{T,f}}) where {T <: Unsigned, f}
4139
typemax(T) >> (bitwidth(T) - f)
4240
end
@@ -105,8 +103,6 @@ rem(x::Normed, ::Type{T}) where {T <: Normed} = reinterpret(T, _unsafe_trunc(raw
105103
rem(x::Real, ::Type{T}) where {T <: Normed} = reinterpret(T, _unsafe_trunc(rawtype(T), round(rawone(T)*x)))
106104
rem(x::Float16, ::Type{T}) where {T <: Normed} = rem(Float32(x), T) # avoid overflow
107105

108-
float(x::Normed) = convert(floattype(x), x)
109-
110106

111107
function (::Type{T})(x::Normed) where {T <: AbstractFloat}
112108
# The following optimization for constant division may cause rounding errors.

0 commit comments

Comments
 (0)