Skip to content

Commit e9ba16f

Browse files
committed
Replace reinterpret by getindex
Since writing `x.i` is so short compared to `reinterpret(x)`, it is still all over the codebase. Using `getindex` gives a shorthand `x[]` and makes the code cleaner to read. I was contemplating using `get`, but there is no syntax sugar for that.
1 parent 34d7fec commit e9ba16f

File tree

5 files changed

+59
-58
lines changed

5 files changed

+59
-58
lines changed

src/FixedPointNumbers.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using Base: IdFun, AddFun, MulFun, reducedim_initarray
77
import Base: ==, <, <=, -, +, *, /, ~,
88
convert, promote_rule, show, showcompact, isinteger, abs, decompose,
99
isnan, isinf, isfinite,
10-
zero, one, typemin, typemax, realmin, realmax, eps, sizeof, reinterpret,
10+
zero, one, typemin, typemax, realmin, realmax, eps, sizeof, reinterpret, getindex,
1111
trunc, round, floor, ceil, bswap,
1212
div, fld, rem, mod, mod1, rem1, fld1, min, max,
1313
start, next, done, r_promote, reducedim_init
@@ -40,15 +40,15 @@ export
4040
# Functions
4141
scaledual
4242

43-
reinterpret(x::FixedPoint) = x.i
43+
getindex(x::FixedPoint) = x.i
4444

4545
# comparison
46-
=={T <: FixedPoint}(x::T, y::T) = x.i == y.i
47-
<{T <: FixedPoint}(x::T, y::T) = x.i < y.i
48-
<={T <: FixedPoint}(x::T, y::T) = x.i <= y.i
46+
=={T <: FixedPoint}(x::T, y::T) = x[] == y[]
47+
<{T <: FixedPoint}(x::T, y::T) = x[] < y[]
48+
<={T <: FixedPoint}(x::T, y::T) = x[] <= y[]
4949

5050
# predicates
51-
isinteger{T,f}(x::FixedPoint{T,f}) = (x.i&(1<<f-1)) == 0
51+
isinteger{T,f}(x::FixedPoint{T,f}) = (x[]&(1<<f-1)) == 0
5252

5353
typemax{T<: FixedPoint}(::Type{T}) = T(typemax(rawtype(T)), 0)
5454
typemin{T<: FixedPoint}(::Type{T}) = T(typemin(rawtype(T)), 0)
@@ -77,7 +77,7 @@ reducedim_init{T<:FixedPoint}(f::IdFun, op::MulFun,
7777
for T in tuple(Fixed16, UF...)
7878
R = rawtype(T)
7979
@eval begin
80-
reinterpret(::Type{$R}, x::$T) = x.i
80+
reinterpret(::Type{$R}, x::$T) = x[]
8181
end
8282
end
8383

src/deprecations.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ import Base.@deprecate_binding
1111

1212
@deprecate_binding Fixed32 Fixed16
1313
@deprecate Fixed(x::Real) convert(Fixed{Int32, 16}, x)
14+
@deprecate reinterpret(x::FixedPoint) getindex(x)

src/fixed.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ typealias Fixed16 Fixed{Int32, 16}
1515
nbitsfrac{T,f}(::Type{Fixed{T,f}}) = f
1616

1717
# basic operators
18-
-{T,f}(x::Fixed{T,f}) = Fixed{T,f}(-x.i,0)
19-
abs{T,f}(x::Fixed{T,f}) = Fixed{T,f}(abs(x.i),0)
18+
-{T,f}(x::Fixed{T,f}) = Fixed{T,f}(-x[],0)
19+
abs{T,f}(x::Fixed{T,f}) = Fixed{T,f}(abs(x[]),0)
2020

21-
+{T,f}(x::Fixed{T,f}, y::Fixed{T,f}) = Fixed{T,f}(x.i+y.i,0)
22-
-{T,f}(x::Fixed{T,f}, y::Fixed{T,f}) = Fixed{T,f}(x.i-y.i,0)
21+
+{T,f}(x::Fixed{T,f}, y::Fixed{T,f}) = Fixed{T,f}(x[]+y[],0)
22+
-{T,f}(x::Fixed{T,f}, y::Fixed{T,f}) = Fixed{T,f}(x[]-y[],0)
2323

2424
# with truncation:
25-
#*{f}(x::Fixed32{f}, y::Fixed32{f}) = Fixed32{f}(Base.widemul(x.i,y.i)>>f,0)
25+
#*{f}(x::Fixed32{f}, y::Fixed32{f}) = Fixed32{f}(Base.widemul(x[],y[])>>f,0)
2626
# with rounding up:
27-
*{T,f}(x::Fixed{T,f}, y::Fixed{T,f}) = Fixed{T,f}((Base.widemul(x.i,y.i) + (convert(widen(T), 1) << (f-1) ))>>f,0)
27+
*{T,f}(x::Fixed{T,f}, y::Fixed{T,f}) = Fixed{T,f}((Base.widemul(x[],y[]) + (convert(widen(T), 1) << (f-1) ))>>f,0)
2828

29-
/{T,f}(x::Fixed{T,f}, y::Fixed{T,f}) = Fixed{T,f}(div(convert(widen(T), x.i) << f, y.i), 0)
29+
/{T,f}(x::Fixed{T,f}, y::Fixed{T,f}) = Fixed{T,f}(div(convert(widen(T), x[]) << f, y[]), 0)
3030

3131

3232
# # conversions and promotions
@@ -35,25 +35,25 @@ convert{T,f}(::Type{Fixed{T,f}}, x::AbstractFloat) = Fixed{T,f}(trunc(T,x)<<f +
3535
convert{T,f}(::Type{Fixed{T,f}}, x::Rational) = Fixed{T,f}(x.num)/Fixed{T,f}(x.den)
3636

3737
convert{T,f}(::Type{BigFloat}, x::Fixed{T,f}) =
38-
convert(BigFloat,x.i>>f) + convert(BigFloat,x.i&(1<<f - 1))/convert(BigFloat,1<<f)
38+
convert(BigFloat,x[]>>f) + convert(BigFloat,x[]&(1<<f - 1))/convert(BigFloat,1<<f)
3939
convert{TF<:AbstractFloat,T,f}(::Type{TF}, x::Fixed{T,f}) =
40-
convert(TF,x.i>>f) + convert(TF,x.i&(1<<f - 1))/convert(TF,1<<f)
40+
convert(TF,x[]>>f) + convert(TF,x[]&(1<<f - 1))/convert(TF,1<<f)
4141

42-
convert{T,f}(::Type{Bool}, x::Fixed{T,f}) = x.i!=0
42+
convert{T,f}(::Type{Bool}, x::Fixed{T,f}) = x[]!=0
4343
function convert{TI<:Integer, T,f}(::Type{TI}, x::Fixed{T,f})
4444
isinteger(x) || throw(InexactError())
45-
convert(TI, x.i>>f)
45+
convert(TI, x[]>>f)
4646
end
4747

4848
convert{TR<:Rational,T,f}(::Type{TR}, x::Fixed{T,f}) =
49-
convert(TR, x.i>>f + (x.i&(1<<f-1))//(1<<f))
49+
convert(TR, x[]>>f + (x[]&(1<<f-1))//(1<<f))
5050

5151
promote_rule{T,f,TI<:Integer}(ft::Type{Fixed{T,f}}, ::Type{TI}) = Fixed{T,f}
5252
promote_rule{T,f,TF<:AbstractFloat}(::Type{Fixed{T,f}}, ::Type{TF}) = TF
5353
promote_rule{T,f,TR}(::Type{Fixed{T,f}}, ::Type{Rational{TR}}) = Rational{TR}
5454

5555
# TODO: Document and check that it still does the right thing.
56-
decompose{T,f}(x::Fixed{T,f}) = x.i, -f, 1
56+
decompose{T,f}(x::Fixed{T,f}) = x[], -f, 1
5757

5858
# printing
5959
function show(io::IO, x::Fixed)

src/ufixed.jl

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ for uf in UF
4545
end
4646
zero(x::UFixed) = zero(typeof(x))
4747
one(x::UFixed) = one(typeof(x))
48-
rawone(v) = reinterpret(one(v))
48+
rawone(v) = one(v)[]
4949

5050
# Conversions
5151
convert{T<:UFixed}(::Type{T}, x::T) = x
52-
convert{T1<:UFixed}(::Type{T1}, x::UFixed) = reinterpret(T1, round(rawtype(T1), (rawone(T1)/rawone(x))*reinterpret(x)))
53-
convert(::Type{UFixed16}, x::UFixed8) = reinterpret(UFixed16, convert(UInt16, 0x0101*reinterpret(x)))
52+
convert{T1<:UFixed}(::Type{T1}, x::UFixed) = reinterpret(T1, round(rawtype(T1), (rawone(T1)/rawone(x))*x[]))
53+
convert(::Type{UFixed16}, x::UFixed8) = reinterpret(UFixed16, convert(UInt16, 0x0101*x[]))
5454
convert{T<:UFixed}(::Type{T}, x::Real) = T(round(rawtype(T), rawone(T)*x),0)
5555

5656
ufixed8(x) = convert(UFixed8, x)
@@ -66,12 +66,12 @@ ufixed16(x) = convert(UFixed16, x)
6666
@vectorize_1arg Real ufixed16
6767

6868

69-
convert(::Type{BigFloat}, x::UFixed) = reinterpret(x)*(1/BigFloat(rawone(x)))
70-
convert{T<:AbstractFloat}(::Type{T}, x::UFixed) = reinterpret(x)*(1/convert(T, rawone(x)))
69+
convert(::Type{BigFloat}, x::UFixed) = x[]*(1/BigFloat(rawone(x)))
70+
convert{T<:AbstractFloat}(::Type{T}, x::UFixed) = x[]*(1/convert(T, rawone(x)))
7171
convert(::Type{Bool}, x::UFixed) = x == zero(x) ? false : true
7272
convert{T<:Integer}(::Type{T}, x::UFixed) = convert(T, x*(1/one(T)))
73-
convert{Ti<:Integer}(::Type{Rational{Ti}}, x::UFixed) = convert(Ti, reinterpret(x))//convert(Ti, rawone(x))
74-
convert(::Type{Rational}, x::UFixed) = reinterpret(x)//rawone(x)
73+
convert{Ti<:Integer}(::Type{Rational{Ti}}, x::UFixed) = convert(Ti, x[])//convert(Ti, rawone(x))
74+
convert(::Type{Rational}, x::UFixed) = x[]//rawone(x)
7575

7676
# Traits
7777
eps{T<:UFixed}(::Type{T}) = T(one(rawtype(T)),0)
@@ -80,20 +80,20 @@ sizeof{T<:UFixed}(::Type{T}) = sizeof(rawtype(T))
8080
abs(x::UFixed) = x
8181

8282
# Arithmetic
83-
(-){T<:UFixed}(x::T) = T(-reinterpret(x), 0)
84-
(~){T<:UFixed}(x::T) = T(~reinterpret(x), 0)
83+
(-){T<:UFixed}(x::T) = T(-x[], 0)
84+
(~){T<:UFixed}(x::T) = T(~x[], 0)
8585

86-
+{T,f}(x::UFixed{T,f}, y::UFixed{T,f}) = UFixed{T,f}(convert(T, x.i+y.i),0)
87-
-{T,f}(x::UFixed{T,f}, y::UFixed{T,f}) = UFixed{T,f}(convert(T, x.i-y.i),0)
88-
*{T,f}(x::UFixed{T,f}, y::UFixed{T,f}) = UFixed{T,f}((Base.widemul(x.i,y.i) + (convert(widen(T), 1) << (f-1) ))>>f,0)
89-
/{T,f}(x::UFixed{T,f}, y::UFixed{T,f}) = UFixed{T,f}(div(convert(widen(T), x.i)<<f, y.i),0)
86+
+{T,f}(x::UFixed{T,f}, y::UFixed{T,f}) = UFixed{T,f}(convert(T, x[]+y[]),0)
87+
-{T,f}(x::UFixed{T,f}, y::UFixed{T,f}) = UFixed{T,f}(convert(T, x[]-y[]),0)
88+
*{T,f}(x::UFixed{T,f}, y::UFixed{T,f}) = UFixed{T,f}((Base.widemul(x[],y[]) + (convert(widen(T), 1) << (f-1) ))>>f,0)
89+
/{T,f}(x::UFixed{T,f}, y::UFixed{T,f}) = UFixed{T,f}(div(convert(widen(T), x[])<<f, y[]),0)
9090

9191
# Comparisons
92-
<{T<:UFixed}(x::T, y::T) = reinterpret(x) < reinterpret(y)
93-
<={T<:UFixed}(x::T, y::T) = reinterpret(x) < reinterpret(y)
92+
<{T<:UFixed}(x::T, y::T) = x[] < y[]
93+
<={T<:UFixed}(x::T, y::T) = x[] < y[]
9494

9595
# Functions
96-
trunc{T<:UFixed}(x::T) = T(div(reinterpret(x), rawone(T))*rawone(T),0)
96+
trunc{T<:UFixed}(x::T) = T(div(x[], rawone(T))*rawone(T),0)
9797
floor{T<:UFixed}(x::T) = trunc(x)
9898
for T in UF
9999
f = nbitsfrac(T)
@@ -102,15 +102,15 @@ for T in UF
102102
k = 8*sizeof(R)-f
103103
ceilmask = (typemax(R)<<k)>>k
104104
@eval begin
105-
round(x::$T) = (y = trunc(x); return convert(rawtype($T), reinterpret(x)-reinterpret(y))&$roundmask>0 ? $T(y+one($T)) : y)
106-
ceil(x::$T) = (y = trunc(x); return convert(rawtype($T), reinterpret(x)-reinterpret(y))&$ceilmask >0 ? $T(y+one($T)) : y)
105+
round(x::$T) = (y = trunc(x); return convert(rawtype($T), x[]-y[])&$roundmask>0 ? $T(y+one($T)) : y)
106+
ceil(x::$T) = (y = trunc(x); return convert(rawtype($T), x[]-y[])&$ceilmask >0 ? $T(y+one($T)) : y)
107107
end
108108
end
109109

110-
trunc{T<:Integer}(::Type{T}, x::UFixed) = convert(T, div(reinterpret(x), rawone(x)))
111-
round{T<:Integer}(::Type{T}, x::UFixed) = round(T, reinterpret(x)/rawone(x))
110+
trunc{T<:Integer}(::Type{T}, x::UFixed) = convert(T, div(x[], rawone(x)))
111+
round{T<:Integer}(::Type{T}, x::UFixed) = round(T, x[]/rawone(x))
112112
floor{T<:Integer}(::Type{T}, x::UFixed) = trunc(T, x)
113-
ceil{T<:Integer}(::Type{T}, x::UFixed) = ceil(T, reinterpret(x)/rawone(x))
113+
ceil{T<:Integer}(::Type{T}, x::UFixed) = ceil(T, x[]/rawone(x))
114114
trunc(x::UFixed) = trunc(Int, x)
115115
round(x::UFixed) = round(Int, x)
116116
floor(x::UFixed) = floor(Int, x)
@@ -121,34 +121,34 @@ isnan(x::UFixed) = false
121121
isinf(x::UFixed) = false
122122

123123
bswap{f}(x::UFixed{UInt8,f}) = x
124-
bswap(x::UFixed) = typeof(x)(bswap(reinterpret(x)),0)
124+
bswap(x::UFixed) = typeof(x)(bswap(x[]),0)
125125

126126
for f in (:div, :fld, :rem, :mod, :mod1, :rem1, :fld1, :min, :max)
127127
@eval begin
128-
$f{T<:UFixed}(x::T, y::T) = T($f(reinterpret(x),reinterpret(y)),0)
128+
$f{T<:UFixed}(x::T, y::T) = T($f(x[],y[]),0)
129129
end
130130
end
131131
function minmax{T<:UFixed}(x::T, y::T)
132-
a, b = minmax(reinterpret(x), reinterpret(y))
132+
a, b = minmax(x[], y[])
133133
T(a,0), T(b,0)
134134
end
135135

136136
# Iteration
137137
# The main subtlety here is that iterating over 0x00uf8:0xffuf8 will wrap around
138138
# unless we iterate using a wider type
139139
if VERSION < v"0.3-"
140-
start{T<:UFixed}(r::Range{T}) = convert(typeof(reinterpret(r.start)+reinterpret(r.step)), reinterpret(r.start))
141-
next{T<:UFixed}(r::Range{T}, i::Integer) = (T(i,0), i+reinterpret(r.step))
140+
start{T<:UFixed}(r::Range{T}) = convert(typeof(r.start[] + r.step[]), r.start[])
141+
next{T<:UFixed}(r::Range{T}, i::Integer) = (T(i,0), i + r.step[])
142142
done{T<:UFixed}(r::Range{T}, i::Integer) = isempty(r) || (i > r.len)
143143
else
144-
start{T<:UFixed}(r::StepRange{T}) = convert(typeof(reinterpret(r.start)+reinterpret(r.step)), reinterpret(r.start))
145-
next{T<:UFixed}(r::StepRange{T}, i::Integer) = (T(i,0), i+reinterpret(r.step))
146-
done{T<:UFixed}(r::StepRange{T}, i::Integer) = isempty(r) || (i > reinterpret(r.stop))
144+
start{T<:UFixed}(r::StepRange{T}) = convert(typeof(r.start[] + r.step[]), r.start[])
145+
next{T<:UFixed}(r::StepRange{T}, i::Integer) = (T(i,0), i + r.step[])
146+
done{T<:UFixed}(r::StepRange{T}, i::Integer) = isempty(r) || (i > r.stop[])
147147
end
148148

149149
function decompose(x::UFixed)
150-
g = gcd(reinterpret(x), rawone(x))
151-
div(reinterpret(x),g), 0, div(rawone(x),g)
150+
g = gcd(x[], rawone(x))
151+
div(x[],g), 0, div(rawone(x),g)
152152
end
153153

154154
# Promotions

test/ufixed.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using FixedPointNumbers, Base.Test
22

3-
@test reinterpret(0xa2uf8) == 0xa2
4-
@test reinterpret(0xa2uf10) == 0xa2
5-
@test reinterpret(0xa2uf12) == 0xa2
6-
@test reinterpret(0xa2uf14) == 0xa2
7-
@test reinterpret(0xa2uf16) == 0xa2
3+
@test (0xa2uf8)[] == 0xa2
4+
@test (0xa2uf10)[] == 0xa2
5+
@test (0xa2uf12)[] == 0xa2
6+
@test (0xa2uf14)[] == 0xa2
7+
@test (0xa2uf16)[] == 0xa2
88

99
@test reinterpret(UFixed8, 0xa2) == 0xa2uf8
1010
@test reinterpret(UFixed10, 0x1fa2) == 0x1fa2uf10
@@ -90,9 +90,9 @@ end
9090

9191
function testtrunc{T}(inc::T)
9292
incf = convert(Float64, inc)
93-
tm = reinterpret(typemax(T))/reinterpret(one(T))
93+
tm = typemax(T)[] / one(T)[]
9494
x = zero(T)
95-
for i = 0:reinterpret(typemax(T))-1
95+
for i = 0:typemax(T)[]-1
9696
xf = incf*i
9797
try
9898
@test trunc(x) == trunc(xf)

0 commit comments

Comments
 (0)