|
1 | | -# 32-bit fixed point; parameter `f` is the number of fraction bits |
2 | | -struct Fixed{T <: Signed,f} <: FixedPoint{T, f} |
| 1 | +""" |
| 2 | + Fixed{T <: Signed, f} <: FixedPoint{T, f} |
| 3 | +
|
| 4 | +`Fixed{T,f}` maps `Signed` integers from `-2^f` to `2^f` to the range |
| 5 | +[-1.0, 1.0]. For example, `Fixed{Int8,7}` maps `-128` to `-1.0` and `127` to |
| 6 | +`127/128 ≈ 0.992`. |
| 7 | +
|
| 8 | +There are the typealiases for `Fixed` in the `QXfY` notation, where `Y` is |
| 9 | +the number of fractional bits (i.e. `f`), and `X+Y+1` equals the number of |
| 10 | +underlying bits used (`+1` means the sign bit). For example, `Q0f7` is aliased |
| 11 | +to `Fixed{Int8,7}` and `Q3f12` is aliased to `Fixed{Int16,12}`. |
| 12 | +""" |
| 13 | +struct Fixed{T <: Signed, f} <: FixedPoint{T, f} |
3 | 14 | i::T |
4 | 15 |
|
5 | 16 | # constructor for manipulating the representation; |
@@ -37,7 +48,7 @@ abs(x::Fixed{T,f}) where {T,f} = Fixed{T,f}(abs(x.i),0) |
37 | 48 | -(x::Fixed{T,f}, y::Fixed{T,f}) where {T,f} = Fixed{T,f}(x.i-y.i,0) |
38 | 49 |
|
39 | 50 | # with truncation: |
40 | | -#*{f}(x::Fixed32{f}, y::Fixed32{f}) = Fixed32{f}(Base.widemul(x.i,y.i)>>f,0) |
| 51 | +#*(x::Fixed{T,f}, y::Fixed{T,f}) = Fixed{T,f}(Base.widemul(x.i,y.i)>>f,0) |
41 | 52 | # with rounding up: |
42 | 53 | *(x::Fixed{T,f}, y::Fixed{T,f}) where {T,f} = Fixed{T,f}((Base.widemul(x.i,y.i) + (one(widen(T)) << (f-1)))>>f,0) |
43 | 54 |
|
|
0 commit comments