@@ -9,7 +9,9 @@ immutable UFixed{T<:Unsigned,f} <: FixedPoint{T,f}
9
9
end
10
10
11
11
rawtype {T,f} (:: Type{UFixed{T,f}} ) = T
12
+ rawtype (x:: Number ) = rawtype (typeof (x))
12
13
nbitsfrac {T,f} (:: Type{UFixed{T,f}} ) = f
14
+ nbitsfrac (x:: Number ) = nbitsfract (typeof (x))
13
15
14
16
typealias UFixed8 UFixed{UInt8,8 }
15
17
typealias UFixed10 UFixed{UInt16,10 }
@@ -31,9 +33,8 @@ const uf14 = UFixedConstructor{UInt16,14}()
31
33
const uf16 = UFixedConstructor {UInt16,16} ()
32
34
33
35
zero {T,f} (:: Type{UFixed{T,f}} ) = UFixed {T,f} (zero (T),0 )
34
- @generated function one {T<:UFixed} (:: Type{T} )
35
- f = 2 ^ nbitsfrac (T)- 1
36
- :( T ($ f,0 ) )
36
+ function one {T<:UFixed} (:: Type{T} )
37
+ T (typemax (rawtype (T)) >> (8 * sizeof (T)- nbitsfrac (T)), 0 )
37
38
end
38
39
zero (x:: UFixed ) = zero (typeof (x))
39
40
one (x:: UFixed ) = one (typeof (x))
@@ -65,7 +66,7 @@ rem{T<:UFixed}(x::Real, ::Type{T}) = reinterpret(T, _unsafe_trunc(rawtype(T), ro
65
66
66
67
convert (:: Type{BigFloat} , x:: UFixed ) = reinterpret (x)* (1 / BigFloat (rawone (x)))
67
68
function convert {T<:AbstractFloat} (:: Type{T} , x:: UFixed )
68
- y = reinterpret (x)* (1 / convert (T, rawone (x)))
69
+ y = reinterpret (x)* (one ( rawtype (x)) / convert (T, rawone (x)))
69
70
convert (T, y) # needed for types like Float16 which promote arithmetic to Float32
70
71
end
71
72
convert (:: Type{Bool} , x:: UFixed ) = x == zero (x) ? false : true
@@ -80,13 +81,17 @@ sizeof{T<:UFixed}(::Type{T}) = sizeof(rawtype(T))
80
81
abs (x:: UFixed ) = x
81
82
82
83
# Arithmetic
84
+ @generated function floattype {U<:UFixed} (:: Type{U} )
85
+ eps (U) < eps (Float32) ? :(Float64) : :(Float32)
86
+ end
87
+
83
88
(- ){T<: UFixed }(x:: T ) = T (- reinterpret (x), 0 )
84
89
(~ ){T<: UFixed }(x:: T ) = T (~ reinterpret (x), 0 )
85
90
86
91
+ {T,f}(x:: UFixed{T,f} , y:: UFixed{T,f} ) = UFixed {T,f} (convert (T, x. i+ y. i),0 )
87
92
- {T,f}(x:: UFixed{T,f} , y:: UFixed{T,f} ) = UFixed {T,f} (convert (T, x. i- y. i),0 )
88
- * {T<: UFixed }(x:: T , y:: T ) = convert (T,convert (Float32 , x)* convert (Float32 , y))
89
- / {T<: UFixed }(x:: T , y:: T ) = convert (T,convert (Float32 , x)/ convert (Float32 , y))
93
+ * {T<: UFixed }(x:: T , y:: T ) = convert (T,convert (floattype (T) , x)* convert (floattype (T) , y))
94
+ / {T<: UFixed }(x:: T , y:: T ) = convert (T,convert (floattype (T) , x)/ convert (floattype (T) , y))
90
95
91
96
# Comparisons
92
97
< {T<: UFixed }(x:: T , y:: T ) = reinterpret (x) < reinterpret (y)
0 commit comments