|
1 | 1 | # This file is a part of BitOperations.jl, licensed under the MIT License (MIT).
|
2 | 2 |
|
3 | 3 |
|
4 |
| -export BitCount |
| 4 | +""" |
| 5 | + BitOperations.BitCount = Union{Signed, Unsigned} |
| 6 | +
|
| 7 | +A number of bits or the index of a bit. |
| 8 | +""" |
5 | 9 | const BitCount = Union{Signed, Unsigned}
|
6 | 10 |
|
7 |
| -function bsizeof end |
8 |
| -export bsizeof |
9 | 11 |
|
10 |
| -function bmask end |
11 |
| -export bmask |
12 | 12 |
|
13 |
| -function lsbmask end |
14 |
| -export lsbmask |
15 |
| - |
16 |
| -function msbmask end |
17 |
| -export msbmask |
18 |
| - |
19 |
| -function bget end |
20 |
| -export bget |
21 |
| - |
22 |
| -function bset end |
23 |
| -export bset |
24 |
| - |
25 |
| -function bclear end |
26 |
| -export bclear |
| 13 | +@inline fbc(::Type{T}, x) where {T} = x%unsigned(T) |
| 14 | +@inline fbc(::Type{T}, bits::UnitRange{U}) where {T,U} = fbc(T, bits.start):fbc(T, bits.stop) |
27 | 15 |
|
28 |
| -function bflip end |
29 |
| -export bflip |
30 | 16 |
|
31 |
| -function lsbget end |
32 |
| -export lsbget |
33 |
| - |
34 |
| -function msbget end |
35 |
| -export msbget |
| 17 | +function bsizeof end |
| 18 | +export bsizeof |
36 | 19 |
|
| 20 | +""" |
| 21 | + bsizeof(x) |
37 | 22 |
|
38 |
| -@inline fbc(::Type{T}, x) where {T} = x%unsigned(T) |
39 |
| -@inline fbc(::Type{T}, bits::UnitRange{U}) where {T,U} = fbc(T, bits.start):fbc(T, bits.stop) |
| 23 | +Returns the data size of x in bits. |
| 24 | +""" |
| 25 | +@inline bsizeof(x) = sizeof(x) << 3 |
40 | 26 |
|
41 | 27 |
|
42 |
| -@inline bsizeof(x) = sizeof(x) << 3 |
| 28 | +function bmask end |
| 29 | +export bmask |
43 | 30 |
|
| 31 | +""" |
| 32 | + bmask(::Type{T}, bit::Integer)::T where {T<:Integer} |
44 | 33 |
|
| 34 | +Generates a bit mask of type `T` with bit `bit` set to one. |
| 35 | +""" |
45 | 36 | @inline bmask(::Type{T}, bit::BitCount) where {T<:Integer} = one(T) << fbc(T, bit)
|
46 | 37 |
|
47 |
| -@inline bmask(::Type{T}, bits::UnitRange{U}) where {T<:Integer,U<:BitCount} = begin |
| 38 | +""" |
| 39 | + bmask(::Type{T}, bits::UnitRange{<:Integer})::T where {T<:Integer} |
| 40 | +
|
| 41 | +Generates a bit mask of type `T` with bit-range `bits` set to one. |
| 42 | +""" |
| 43 | +@inline bmask(::Type{T}, bits::UnitRange{<:BitCount}) where {T<:Integer} = begin |
48 | 44 | fbits = fbc(T, bits)
|
49 | 45 | #@assert fbits.stop >= fbits.start "Bitmask range of fbits can't be reverse"
|
50 | 46 | ((one(T) << (fbits.stop - fbits.start + 1)) - one(T)) << fbits.start
|
51 | 47 | end
|
52 | 48 |
|
53 | 49 |
|
54 |
| -@inline lsbmask(::Type{T}) where {T<:Integer} = one(T) |
| 50 | +function lsbmask end |
| 51 | +export lsbmask |
55 | 52 |
|
56 |
| -@inline msbmask(::Type{T}) where {T<:Integer} = one(T) << fbc(T, bsizeof(T) - 1) |
| 53 | +""" |
| 54 | + lsbmask(::Type{T})::T where {T<:Integer} |
| 55 | +
|
| 56 | +Generates a bit mask with only the least significant bit set. |
| 57 | +""" |
| 58 | +@inline lsbmask(::Type{T}) where {T<:Integer} = one(T) |
57 | 59 |
|
| 60 | +""" |
| 61 | + lsbmask(::Type{T}, nbits::Integer)::T where {T<:Integer} |
58 | 62 |
|
| 63 | +Generates a bit mask with only the `nbits` least significant bits set. |
| 64 | +""" |
59 | 65 | @inline lsbmask(::Type{T}, nbits::BitCount) where {T<:Integer} = ~(~zero(T) << fbc(T, nbits))
|
60 | 66 |
|
| 67 | + |
| 68 | +function msbmask end |
| 69 | +export msbmask |
| 70 | + |
| 71 | +""" |
| 72 | + msbmask(::Type{T})::T where {T<:Integer} |
| 73 | +
|
| 74 | +Generates a bit mask with only the most significant bit set. |
| 75 | +""" |
| 76 | +@inline msbmask(::Type{T}) where {T<:Integer} = one(T) << fbc(T, bsizeof(T) - 1) |
| 77 | + |
| 78 | +""" |
| 79 | + msbmask(::Type{T}, nbits::Integer)::T where {T<:Integer} |
| 80 | +
|
| 81 | +Generates a bit mask with only the `nbits` most significant bits set. |
| 82 | +""" |
61 | 83 | @inline msbmask(::Type{T}, nbits::BitCount) where {T<:Integer} = ~(~zero(T) >>> fbc(T, nbits))
|
62 | 84 |
|
63 | 85 |
|
64 |
| -@inline bget(x::T, bit::BitCount) where {T<:Integer} = x & bmask(typeof(x), fbc(T, bit)) != zero(typeof(x)) |
| 86 | +function bget end |
| 87 | +export bget |
65 | 88 |
|
66 |
| -@inline bset(x::T, bit::BitCount, y::Bool) where {T<:Integer} = y ? bset(x, fbc(T, bit)) : bclear(x, fbc(T, bit)) |
67 |
| -@inline bset(x::T, bit::BitCount) where {T<:Integer} = x | bmask(typeof(x), fbc(T, bit)) |
| 89 | +""" |
| 90 | + bget(x::T, bit::Integer)::Bool where {T<:Integer} |
68 | 91 |
|
69 |
| -@inline bclear(x::T, bit::BitCount) where {T<:Integer} = x & ~bmask(typeof(x), fbc(T, bit)) |
| 92 | +Get the value of bit `bit` of x. |
| 93 | +""" |
| 94 | +@inline bget(x::T, bit::BitCount) where {T<:Integer} = x & bmask(typeof(x), fbc(T, bit)) != zero(typeof(x)) |
70 | 95 |
|
| 96 | +""" |
| 97 | + bget(x::T, bits::UnitRange{<:Integer})::T where {T<:Integer} |
71 | 98 |
|
72 |
| -@inline bget(x::T, bits::UnitRange{U}) where {T<:Integer,U<:BitCount} = begin |
| 99 | +Get the value of the bit range `bits` of x. |
| 100 | +""" |
| 101 | +@inline bget(x::T, bits::UnitRange{<:BitCount}) where {T<:Integer} = begin |
73 | 102 | fbits = fbc(T, bits)
|
74 | 103 | (x & bmask(typeof(x), fbits)) >>> fbits.start
|
75 | 104 | end
|
76 | 105 |
|
77 |
| -@inline bset(x::T, bits::UnitRange{U}, y::Integer) where {T<:Integer,U<:BitCount} = begin |
| 106 | + |
| 107 | +function bset end |
| 108 | +export bset |
| 109 | + |
| 110 | +""" |
| 111 | + bset(x::T, bit::Integer)::T where {T<:Integer} |
| 112 | +
|
| 113 | +Returns a modified copy of x, with bit `bit` set (to one). |
| 114 | +""" |
| 115 | +@inline bset(x::T, bit::BitCount) where {T<:Integer} = x | bmask(typeof(x), fbc(T, bit)) |
| 116 | + |
| 117 | +""" |
| 118 | + bset(x::T, bit::Integer, y::Bool)::T where {T<:Integer} |
| 119 | +
|
| 120 | +Returns a modified copy of x, with bit `bit` set to `y`. |
| 121 | +""" |
| 122 | +@inline bset(x::T, bit::BitCount, y::Bool) where {T<:Integer} = y ? bset(x, fbc(T, bit)) : bclear(x, fbc(T, bit)) |
| 123 | + |
| 124 | +""" |
| 125 | + bset(x::T, bits::UnitRange{<:Integer}, y::Integer)::T where {T<:Integer} |
| 126 | +
|
| 127 | +Returns a modified copy of x, with bit range `bits` set to `y`. |
| 128 | +""" |
| 129 | +@inline bset(x::T, bits::UnitRange{<:BitCount}, y::Integer) where {T<:Integer} = begin |
78 | 130 | fbits = fbc(T, bits)
|
79 | 131 | local bm = bmask(typeof(x), fbits)
|
80 | 132 | (x & ~bm) | ((convert(typeof(x), y) << fbits.start) & bm)
|
81 | 133 | end
|
82 | 134 |
|
83 | 135 |
|
| 136 | +function bclear end |
| 137 | +export bclear |
| 138 | + |
| 139 | +""" |
| 140 | + bclear(x::T, bit::Integer)::T where {T<:Integer} |
| 141 | +
|
| 142 | +Returns a modified copy of x, with bit `bit` cleared (set to zero). |
| 143 | +""" |
| 144 | +@inline bclear(x::T, bit::BitCount) where {T<:Integer} = x & ~bmask(typeof(x), fbc(T, bit)) |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +function bflip end |
| 149 | +export bflip |
| 150 | + |
| 151 | +""" |
| 152 | + bflip(x::T, bit::Integer)::T where {T<:Integer} |
| 153 | +
|
| 154 | +Returns a modified copy of x, with bit `bit` flipped. |
| 155 | +""" |
84 | 156 | @inline bflip(x::T, bit::BitCount) where {T<:Integer} = xor(x, bmask(typeof(x), fbc(T, bit)))
|
85 | 157 |
|
86 |
| -@inline bflip(x::T, bits::UnitRange{U}) where {T<:Integer,U<:BitCount} = xor(x, bmask(typeof(x), fbc(T, bits))) |
| 158 | +""" |
| 159 | + bflip(x::T, bits::UnitRange{<:Integer})::T where {T<:Integer} |
| 160 | +
|
| 161 | +Returns a modified copy of x, with all bits in bit range `bits` flipped. |
| 162 | +""" |
| 163 | +@inline bflip(x::T, bits::UnitRange{<:BitCount}) where {T<:Integer} = xor(x, bmask(typeof(x), fbc(T, bits))) |
| 164 | + |
| 165 | + |
| 166 | +function lsbget end |
| 167 | +export lsbget |
87 | 168 |
|
| 169 | +""" |
| 170 | + lsbget(x::Integer)::Bool |
88 | 171 |
|
| 172 | +Returns the value of the least significant bit of x. |
| 173 | +""" |
89 | 174 | @inline lsbget(x::T) where {T<:Integer} =
|
90 | 175 | x & lsbmask(typeof(x)) != zero(typeof(x))
|
91 | 176 |
|
92 |
| -@inline msbget(x::T) where {T<:Integer} = |
93 |
| - x & msbmask(typeof(x)) != zero(typeof(x)) |
94 |
| - |
| 177 | +""" |
| 178 | + lsbget(x::T)::T where {T <: Integer} |
95 | 179 |
|
| 180 | +Returns the value of the `nbits` least significant bits of x. |
| 181 | +""" |
96 | 182 | @inline lsbget(x::T, nbits::BitCount) where {T<:Integer} =
|
97 | 183 | x & lsbmask(typeof(x), fbc(T, nbits))
|
98 | 184 |
|
| 185 | + |
| 186 | +function msbget end |
| 187 | +export msbget |
| 188 | + |
| 189 | +""" |
| 190 | + msbget(x::T)::Bool where {T<:Integer} |
| 191 | +
|
| 192 | +Returns the value of the most significant bit of x. |
| 193 | +""" |
| 194 | +@inline msbget(x::T) where {T<:Integer} = |
| 195 | + x & msbmask(typeof(x)) != zero(typeof(x)) |
| 196 | + |
| 197 | +""" |
| 198 | + msbget(x::T)::T where {T<:Integer} |
| 199 | +
|
| 200 | +Returns the value of the `nbits` most significant bits of x. |
| 201 | +""" |
99 | 202 | @inline msbget(x::T, nbits::BitCount) where {T<:Integer} =
|
100 | 203 | x >>> (bsizeof(x) - fbc(T, nbits))
|
0 commit comments