@@ -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 }
@@ -35,6 +37,7 @@ zero{T,f}(::Type{UFixed{T,f}}) = UFixed{T,f}(zero(T),0)
35
37
f = 2 ^ nbitsfrac (T)- 1
36
38
:( T ($ f,0 ) )
37
39
end
40
+ one {f} (:: Type{UFixed{UInt128,f}} ) = UFixed {UInt128,f} (UInt128 (2 )^ f- 1 , 0 )
38
41
zero (x:: UFixed ) = zero (typeof (x))
39
42
one (x:: UFixed ) = one (typeof (x))
40
43
rawone (v) = reinterpret (one (v))
@@ -65,7 +68,7 @@ rem{T<:UFixed}(x::Real, ::Type{T}) = reinterpret(T, _unsafe_trunc(rawtype(T), ro
65
68
66
69
convert (:: Type{BigFloat} , x:: UFixed ) = reinterpret (x)* (1 / BigFloat (rawone (x)))
67
70
function convert {T<:AbstractFloat} (:: Type{T} , x:: UFixed )
68
- y = reinterpret (x)* (1 / convert (T, rawone (x)))
71
+ y = reinterpret (x)* (one ( rawtype (x)) / convert (T, rawone (x)))
69
72
convert (T, y) # needed for types like Float16 which promote arithmetic to Float32
70
73
end
71
74
convert (:: Type{Bool} , x:: UFixed ) = x == zero (x) ? false : true
@@ -80,13 +83,17 @@ sizeof{T<:UFixed}(::Type{T}) = sizeof(rawtype(T))
80
83
abs (x:: UFixed ) = x
81
84
82
85
# Arithmetic
86
+ @generated function floattype {U<:UFixed} (:: Type{U} )
87
+ eps (U) < eps (Float32) ? :(Float64) : :(Float32)
88
+ end
89
+
83
90
(- ){T<: UFixed }(x:: T ) = T (- reinterpret (x), 0 )
84
91
(~ ){T<: UFixed }(x:: T ) = T (~ reinterpret (x), 0 )
85
92
86
93
+ {T,f}(x:: UFixed{T,f} , y:: UFixed{T,f} ) = UFixed {T,f} (convert (T, x. i+ y. i),0 )
87
94
- {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))
95
+ * {T<: UFixed }(x:: T , y:: T ) = convert (T,convert (floattype (T) , x)* convert (floattype (T) , y))
96
+ / {T<: UFixed }(x:: T , y:: T ) = convert (T,convert (floattype (T) , x)/ convert (floattype (T) , y))
90
97
91
98
# Comparisons
92
99
< {T<: UFixed }(x:: T , y:: T ) = reinterpret (x) < reinterpret (y)
0 commit comments