-
Notifications
You must be signed in to change notification settings - Fork 3
/
BitMath.pas
242 lines (203 loc) · 6.15 KB
/
BitMath.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
{
Copyright (c) Peter Karpov 2010 - 2018.
Usage of the works is permitted provided that this instrument is retained with
the works, so that any entity that uses the works is notified of this instrument.
DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
}
{$IFDEF FPC} {$MODE DELPHI} {$ASMMODE INTEL} {$ENDIF}
unit BitMath; ///////////////////////////////////////////////////////////////////////
{
>> Version: 1.3
>> Description
Bit-level arithmetic functions. Part of InvLibs unit collection.
>> Author
Peter Karpov
Email : [email protected]
Homepage : inversed.ru
GitHub : inversed-ru
Twitter : @inversed_ru
>> References
Henry S. Warren, Hacker's Delight.
>> ToDo
- Add more functions
>> Changelog
1.3 : 2018.09.20 ~ FreePascal compatibility
1.2 : 2015.07.02 + LowestBit function
~ Renamed HighestSetBit to HighestBit
1.1 : 2014.01.27 + BitCount64 function
1.0 : 2013.10.12 + BitShift, BitCount functions
~ Changed to common types
~ Simplified implementation
0.0 : 2010.05.06 + Initial version
Notation: + added, - removed, * fixed, ~ changed
}
interface ///////////////////////////////////////////////////////////////////////////
// Bitshift X left if Shift is positive, right if negative
function BitShift(
x : Integer;
Shift : Integer
) : Integer;
// Return the index of the highest set bit in X, or -1 if there are none
function HighestBit(
x : Integer
) : Integer;
// Return the index of the lowest set bit in X, or -1 if there are none
function LowestBit(
x : Integer
) : Integer;
// Floor of base 2 logarithm of X
function FloorLog2(
x : Integer
) : Integer;
// The smallest power of two that is not less than than X
function Ceil2p(
x : Integer
) : Integer;
// The largest power of two that is not greater than than X
function Floor2p(
x : Integer
) : Integer;
// Return the number of set bits in X
function BitCount(
x : Integer
) : Integer;
// Return the number of set bits in X
function BitCount64(
x : Int64
) : Integer;
const
IntNBytes = 4;
IntHighByte = IntNBytes - 1;
Int64NBytes = 8;
Int64HighByte = Int64NBytes - 1;
ByteNBits = 8;
ByteHighBit = ByteNBits - 1;
IntNBits = IntNBytes * ByteNBits;
IntHighBit = IntNBits - 1;
type
TIntBytes = array [0 .. IntHighByte] of Byte;
TInt64Bytes = array [0 .. Int64HighByte] of Byte;
PIntBytes = ^TIntBytes;
PInt64Bytes = ^TInt64Bytes;
var
ByteBitCount : array [Byte] of Integer;
RevBytes : array [Byte] of Byte;
implementation //////////////////////////////////////////////////////////////////////
{$RANGECHECKS OFF} {$OVERFLOWCHECKS OFF}
// Bitshift X left if Shift is positive, right if negative
function BitShift(
x : Integer;
Shift : Integer
) : Integer;
begin
if Shift > 0 then
Result := x shl Shift else
Result := x shr -Shift ;
end;
// Return the index of the highest set bit in X, or -1 if there are none
function HighestBit(
x : Integer
) : Integer;
asm
bsr eax , eax
jnz @Done
xor eax , eax
dec eax
@Done:
end;
// Return the index of the lowest set bit in X, or -1 if there are none
function LowestBit(
x : Integer
) : Integer;
asm
bsf eax , eax
jnz @Done
xor eax , eax
dec eax
@Done:
end;
// Floor of base 2 logarithm of X
function FloorLog2(
x : Integer
) : Integer;
begin
Result := HighestBit(x);
end;
// The smallest power of two that is not less than than X
function Ceil2p(
x : Integer
) : Integer;
begin
Result := 1 shl (HighestBit(x - 1) + 1);
end;
// The largest power of two that is not greater than than X
function Floor2p(
x : Integer
) : Integer;
begin
Result := 1 shl HighestBit(x);
end;
// Return the number of set bits in X
function BitCount(
x : Integer
) : Integer;
var
PtrBytes : PIntBytes;
begin
PtrBytes := @x;
Result :=
ByteBitCount[PtrBytes[0]] +
ByteBitCount[PtrBytes[1]] +
ByteBitCount[PtrBytes[2]] +
ByteBitCount[PtrBytes[3]] ;
end;
// Return the number of set bits in X
function BitCount64(
x : Int64
) : Integer;
var
PtrBytes : PInt64Bytes;
begin
PtrBytes := @x;
Result :=
ByteBitCount[PtrBytes[0]] +
ByteBitCount[PtrBytes[1]] +
ByteBitCount[PtrBytes[2]] +
ByteBitCount[PtrBytes[3]] +
ByteBitCount[PtrBytes[4]] +
ByteBitCount[PtrBytes[5]] +
ByteBitCount[PtrBytes[6]] +
ByteBitCount[PtrBytes[7]] ;
end;
{$RANGECHECKS ON} {$OVERFLOWCHECKS ON}
{-----------------------<< Initialization >>----------------------------------------}
// Precalculate the reversed byte table
procedure PrecalcRevBytes;
var
i, j, b : Integer;
r : Byte;
begin
for i := 0 to High(Byte) do
begin
r := 0;
for j := 0 to ByteHighBit do
begin
b := i and (1 shl j);
r := r or BitShift(b, ByteHighBit - 2 * j);
end;
RevBytes[i] := r;
end;
end;
// Precalculate the bit count table
procedure PrecalcBitCount;
var
i : Integer;
begin
ByteBitCount[0] := 0;
for i := 0 to High(Byte) do
ByteBitCount[i] := ByteBitCount[i div 2] + (i and 1);
end;
initialization //////////////////////////////////////////////////////////////////////
PrecalcBitCount;
PrecalcRevBytes;
end.