@@ -45,12 +45,12 @@ for uf in UF
45
45
end
46
46
zero (x:: UFixed ) = zero (typeof (x))
47
47
one (x:: UFixed ) = one (typeof (x))
48
- rawone (v) = reinterpret ( one (v))
48
+ rawone (v) = one (v)[]
49
49
50
50
# Conversions
51
51
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[] ))
54
54
convert {T<:UFixed} (:: Type{T} , x:: Real ) = T (round (rawtype (T), rawone (T)* x),0 )
55
55
56
56
ufixed8 (x) = convert (UFixed8, x)
@@ -66,12 +66,12 @@ ufixed16(x) = convert(UFixed16, x)
66
66
@vectorize_1arg Real ufixed16
67
67
68
68
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)))
71
71
convert (:: Type{Bool} , x:: UFixed ) = x == zero (x) ? false : true
72
72
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)
75
75
76
76
# Traits
77
77
eps {T<:UFixed} (:: Type{T} ) = T (one (rawtype (T)),0 )
@@ -80,20 +80,20 @@ sizeof{T<:UFixed}(::Type{T}) = sizeof(rawtype(T))
80
80
abs (x:: UFixed ) = x
81
81
82
82
# 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 )
85
85
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 )
90
90
91
91
# 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[]
94
94
95
95
# 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 )
97
97
floor {T<:UFixed} (x:: T ) = trunc (x)
98
98
for T in UF
99
99
f = nbitsfrac (T)
@@ -102,15 +102,15 @@ for T in UF
102
102
k = 8 * sizeof (R)- f
103
103
ceilmask = (typemax (R)<< k)>> k
104
104
@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)
107
107
end
108
108
end
109
109
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))
112
112
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))
114
114
trunc (x:: UFixed ) = trunc (Int, x)
115
115
round (x:: UFixed ) = round (Int, x)
116
116
floor (x:: UFixed ) = floor (Int, x)
@@ -121,34 +121,34 @@ isnan(x::UFixed) = false
121
121
isinf (x:: UFixed ) = false
122
122
123
123
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 )
125
125
126
126
for f in (:div , :fld , :rem , :mod , :mod1 , :rem1 , :fld1 , :min , :max )
127
127
@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 )
129
129
end
130
130
end
131
131
function minmax {T<:UFixed} (x:: T , y:: T )
132
- a, b = minmax (reinterpret (x), reinterpret (y) )
132
+ a, b = minmax (x[], y[] )
133
133
T (a,0 ), T (b,0 )
134
134
end
135
135
136
136
# Iteration
137
137
# The main subtlety here is that iterating over 0x00uf8:0xffuf8 will wrap around
138
138
# unless we iterate using a wider type
139
139
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[] )
142
142
done {T<:UFixed} (r:: Range{T} , i:: Integer ) = isempty (r) || (i > r. len)
143
143
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[] )
147
147
end
148
148
149
149
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)
152
152
end
153
153
154
154
# Promotions
0 commit comments