Skip to content

Commit 8fbc64e

Browse files
committed
Add docstrings and Update comments
1 parent 7ad0f0c commit 8fbc64e

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

src/FixedPointNumbers.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ import Base: ==, <, <=, -, +, *, /, ~, isapprox,
1010

1111
using Base: @pure
1212

13-
# T => BaseType
14-
# f => Number of bits reserved for fractional part
13+
"""
14+
FixedPoint{T <: Integer, f} <: Real
15+
16+
Supertype of the two fixed-point number types: `Fixed{T, f}` and `Normed{T, f}`.
17+
18+
The parameter `T` is the underlying machine representation and `f` is the number
19+
of fraction bits.
20+
"""
1521
abstract type FixedPoint{T <: Integer, f} <: Real end
1622

1723

src/fixed.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
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}
314
i::T
415

516
# constructor for manipulating the representation;
@@ -37,7 +48,7 @@ abs(x::Fixed{T,f}) where {T,f} = Fixed{T,f}(abs(x.i),0)
3748
-(x::Fixed{T,f}, y::Fixed{T,f}) where {T,f} = Fixed{T,f}(x.i-y.i,0)
3849

3950
# 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)
4152
# with rounding up:
4253
*(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)
4354

src/normed.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
# Normed{T,f} maps UInts from 0 to 2^f-1 to the range [0.0, 1.0]
2-
# For example, Normed{UInt8,8} == N0f8 maps 0x00 to 0.0 and 0xff to 1.0
3-
4-
struct Normed{T<:Unsigned,f} <: FixedPoint{T,f}
1+
"""
2+
Normed{T <: Unsigned, f} <: FixedPoint{T, f}
3+
4+
`Normed{T,f}` maps `Unsigned` integers from `0` to `2^f-1` to the range
5+
[0.0, 1.0]. For example, `Normed{UInt8,8}` maps `0x00` to `0.0` and `0xff` to
6+
`1.0`.
7+
8+
There are the typealiases for `Normed` in the `NXfY` notation, where `Y` is
9+
the number of fractional bits (i.e. `f`), and `X+Y` equals the number of
10+
underlying bits used. For example, `N0f8` is aliased to `Normed{UInt8,8}` and
11+
`N4f12` is aliased to `Normed{UInt16,12}`.
12+
"""
13+
struct Normed{T <: Unsigned, f} <: FixedPoint{T, f}
514
i::T
615

716
Normed{T, f}(i::Integer,_) where {T,f} = new{T, f}(i%T) # for setting by raw representation

0 commit comments

Comments
 (0)