|
11 | 11 | rawtype{T,f}(::Type{UFixed{T,f}}) = T |
12 | 12 | rawtype(x::Number) = rawtype(typeof(x)) |
13 | 13 | nbitsfrac{T,f}(::Type{UFixed{T,f}}) = f |
14 | | -nbitsfrac(x::Number) = nbitsfract(typeof(x)) |
15 | 14 |
|
16 | 15 | typealias UFixed8 UFixed{UInt8,8} |
17 | 16 | typealias UFixed10 UFixed{UInt16,10} |
@@ -42,10 +41,12 @@ rawone(v) = reinterpret(one(v)) |
42 | 41 |
|
43 | 42 | # Conversions |
44 | 43 | convert{T<:UFixed}(::Type{T}, x::T) = x |
45 | | -function convert{T<:UFixed}(::Type{T}, x::UFixed) |
46 | | - y = round((rawone(T)/rawone(x))*reinterpret(x)) |
47 | | - (0 <= y) & (y <= typemax(rawtype(T))) || throw_converterror(T, x) |
48 | | - reinterpret(T, _unsafe_trunc(rawtype(T), y)) |
| 44 | +convert{T1,T2,f}(::Type{UFixed{T1,f}}, x::UFixed{T2,f}) = UFixed{T1,f}(convert(T1, x.i), 0) |
| 45 | +function convert{T,f}(::Type{UFixed{T,f}}, x::UFixed) |
| 46 | + U = UFixed{T,f} |
| 47 | + y = round((rawone(U)/rawone(x))*reinterpret(x)) |
| 48 | + (0 <= y) & (y <= typemax(T)) || throw_converterror(U, x) |
| 49 | + reinterpret(U, _unsafe_trunc(T, y)) |
49 | 50 | end |
50 | 51 | convert(::Type{UFixed16}, x::UFixed8) = reinterpret(UFixed16, convert(UInt16, 0x0101*reinterpret(x))) |
51 | 52 | convert{U<:UFixed}(::Type{U}, x::Real) = _convert(U, rawtype(U), x) |
@@ -144,14 +145,11 @@ end |
144 | 145 | # Iteration |
145 | 146 | # The main subtlety here is that iterating over 0x00uf8:0xffuf8 will wrap around |
146 | 147 | # unless we iterate using a wider type |
147 | | -if VERSION < v"0.3-" |
148 | | - start{T<:UFixed}(r::Range{T}) = convert(typeof(reinterpret(r.start)+reinterpret(r.step)), reinterpret(r.start)) |
149 | | - next{T<:UFixed}(r::Range{T}, i::Integer) = (T(i,0), i+reinterpret(r.step)) |
150 | | - done{T<:UFixed}(r::Range{T}, i::Integer) = isempty(r) || (i > r.len) |
151 | | -else |
152 | | - start{T<:UFixed}(r::StepRange{T}) = convert(typeof(reinterpret(r.start)+reinterpret(r.step)), reinterpret(r.start)) |
153 | | - next{T<:UFixed}(r::StepRange{T}, i::Integer) = (T(i,0), i+reinterpret(r.step)) |
154 | | - done{T<:UFixed}(r::StepRange{T}, i::Integer) = isempty(r) || (i > reinterpret(r.stop)) |
| 148 | +@inline start{T<:UFixed}(r::StepRange{T}) = widen1(reinterpret(r.start)) |
| 149 | +@inline next{T<:UFixed}(r::StepRange{T}, i::Integer) = (T(i,0), i+reinterpret(r.step)) |
| 150 | +@inline function done{T<:UFixed}(r::StepRange{T}, i::Integer) |
| 151 | + i1, i2 = reinterpret(r.start), reinterpret(r.stop) |
| 152 | + isempty(r) | (i < min(i1, i2)) | (i > max(i1, i2)) |
155 | 153 | end |
156 | 154 |
|
157 | 155 | function decompose(x::UFixed) |
|
0 commit comments