-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsdpLogic.pas
416 lines (367 loc) · 11.2 KB
/
sdpLogic.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Íàáîð ïðîöåäóð äëÿ âûïîëíåíèÿ ëîãè÷åñêèõ îïåðàöèé íàä ïîðÿäêîâûìè òèïàìè.
// Âåðñèÿ äëÿ Delphi 5 è 32-õ ðàçðÿäíûõ ïðîöåññîðîâ.
// Àâòîð: Ñèäîðîâ Ä.Ï.
unit sdpLogic;
interface
uses
sdpBigint;
// Âîçâðàùàåò íîìåð ñòàðøåãî íåíóëåâîãî áèòà (íóìåðàöèÿ íà÷èíàåòñÿ ñ 0).
// Åñëè X = 0, òî âîçâðàùàåò -1.
function HighestBit(X: Byte): Integer; overload;
function HighestBit(X: Shortint): Integer; overload;
function HighestBit(X: Word): Integer; overload;
function HighestBit(X: Smallint): Integer; overload;
function HighestBit(X: Cardinal): Integer; overload;
function HighestBit(X: Integer): Integer; overload;
function HighestBit(X: Int64): Integer; overload;
function HighestBit(const X: TBigint): Integer; overload;
// Âîçâðàùàåò íîìåð ìëàäøåãî íåíóëåâîãî áèòà (íóìåðàöèÿ íà÷èíàåòñÿ ñ 0).
// Åñëè X = 0, òî âîçâðàùàåò -1.
function LowestBit(X: Shortint): Integer; overload;
function LowestBit(X: Byte): Integer; overload;
function LowestBit(X: Smallint): Integer; overload;
function LowestBit(X: Word): Integer; overload;
function LowestBit(X: Integer): Integer; overload;
function LowestBit(X: Cardinal): Integer; overload;
function LowestBit(X: Int64): Integer; overload;
function LowestBit(const X: TBigint): Integer; overload;
procedure ShiftLeft(var X: TBigint; N: Byte);
// Ñðàâíåíèå àðãóìåíòîâ ïî àáñîëþòíîé âåëè÷èíå. Ðåçóëüòàò ðàâåí 1 åñëè A>B,
// 0 - A=B, -1 - A<B.
function CompareAbs(A, B: Shortint): Shortint; overload;
function CompareAbs(A, B: Smallint): Shortint; overload;
function CompareAbs(A, B: Integer): Shortint; overload;
function CompareAbs(A, B: Int64): Shortint; overload;
function CompareAbs(const A, B: TBigint): Shortint; overload;
function CompareAbs(const A: TBigint; B: Cardinal): Shortint; overload;
// Ñðàâíåíèå àðãóìåíòîâ. Ðåçóëüòàò ðàâåí 1 åñëè A>B, 0 - A=B, -1 - A<B.
function Compare(A, B: Byte): Shortint; overload;
function Compare(A, B: Shortint): Shortint; overload;
function Compare(A, B: Word): Shortint; overload;
function Compare(A, B: Smallint): Shortint; overload;
function Compare(A, B: Cardinal): Shortint; overload;
function Compare(A, B: Integer): Shortint; overload;
function Compare(A, B: Int64): Shortint; overload;
function Compare(const A, B: TBigint): Shortint; overload;
implementation
const
BytesInWord = SizeOf(Word);
BytesInSmallint = SizeOf(Smallint);
BytesInCardinal = SizeOf(Cardinal);
BytesInInteger = SizeOf(Integer);
BytesInInt64 = SizeOf(Int64);
BitsInByte = 8;
BitsInShortint = SizeOf(Shortint)*BitsInByte;
BitsInWord = BytesInWord*BitsInByte;
BitsInSmallint = BytesInSmallint*BitsInByte;
BitsInCardinal = BytesInCardinal*BitsInByte;
BitsInInteger = BytesInInteger*BitsInByte;
BitsInInt64 = BytesInInt64*BitsInByte;
type
PCardinal = ^Cardinal;
//  öåëÿõ ïîëó÷åíèÿ ìàêñèìàëüíîãî áûñòðîäåéñòâèÿ, âîïðåêè ïðèíöèïàì
// ïðîöåäóðíîãî ïðîãðàììèðîâàíèÿ, íåêîòîðûå ôðàãìåíòû êîäà ïîâòîðÿþòñÿ
// íåñêîëüêî ðàç âìåñòî òîãî, ÷òîáû âûçûâàòüñÿ êàê îòäåëüíàÿ ïðîöåäóðà.
function HighestBit(X: Byte): Integer;
asm
movzx ecx, X
mov eax, -1
bsr eax, ecx
end;
function HighestBit(X: Shortint): Integer;
asm
movzx ecx, X
mov eax, -1
bsr eax, ecx
end;
function HighestBit(X: Word): Integer;
asm
movzx ecx, X
mov eax, -1
bsr eax, ecx
end;
function HighestBit(X: Smallint): Integer;
asm
movzx ecx, X
mov eax, -1
bsr eax, ecx
end;
function HighestBit(X: Cardinal): Integer;
asm
mov ecx, X
mov eax, -1
bsr eax, ecx
end;
function HighestBit(X: Integer): Integer;
asm
mov ecx, X
mov eax, -1
bsr eax, ecx
end;
function HighestBit(X: Int64): Integer;
asm
push esi
lea esi, X+BytesInCardinal // esi = óêàçàòåëü íà ñòàðøóþ ÷àñòü
mov eax, -1
bsr eax, [esi]
jz @ZeroDWord // Ñòàðøàÿ ÷àñòü = 0
add eax, BitsInCardinal
jmp @Exit
@ZeroDWord:
sub esi, BytesInCardinal // esi = óêàçàòåëü íà ìëàäøóþ ÷àñòü
bsr eax, [esi]
@Exit:
pop esi
end;
function HighestBit(const X: TBigint): Integer;
asm
push esi
mov esi, X+ofPtrDigits // esi = óêàçàòåëü íà ìëàäøèé áëîê
movzx ecx, byte ptr (X+ofMSB) // ecx = êîëè÷åñòâî áëîêîâ â ÷èñëå
dec ecx
shl ecx, 2 // ecx := ecx*4 - êîëè÷åñòâî áàéò äî ñòàðøåãî áëîêà
add esi, ecx // esi = óêàçàòåëü íà ñòàðøèé áëîê
mov eax, -1
bsr eax, [esi]
jz @Exit // Ñòàðøèé áëîê = 0, òî X = 0
shl ecx, 3 // ecx := ecx*8 - êîëè÷åñòâî áèò äî ñòàðøåãî áëîêà
add eax, ecx
@Exit:
pop esi
end;
// ****************************************************************************
function LowestBit(X: Shortint): Integer;
asm
movzx ecx, X
mov eax, -1
bsf eax, ecx
end;
function LowestBit(X: Byte): Integer;
asm
movzx ecx, X
mov eax, -1
bsf eax, ecx
end;
function LowestBit(X: Smallint): Integer;
asm
movzx ecx, X
mov eax, -1
bsf eax, ecx
end;
function LowestBit(X: Word): Integer;
asm
movzx ecx, X
mov eax, -1
bsf eax, ecx
end;
function LowestBit(X: Integer): Integer;
asm
mov ecx, X
mov eax, -1
bsf eax, ecx
end;
function LowestBit(X: Cardinal): Integer;
asm
mov ecx, X
mov eax, -1
bsf eax, ecx
end;
function LowestBit(X: Int64): Integer;
asm
push esi
lea esi, X // esi = óêàçàòåëü íà ìëàäøóþ ÷àñòü
mov eax, -1
bsf eax, dword ptr [esi]
jnz @Exit // Ìëàäøàÿ ÷àñòü <> 0
add esi, BytesInCardinal // esi = óêàçàòåëü íà ñòàðøóþ ÷àñòü
bsf eax, dword ptr [esi]
jz @Exit // Ñòàðøàÿ ÷àñòü = 0
add eax, BitsInCardinal
@Exit:
pop esi
end;
function LowestBit(const X: TBigint): Integer;
asm
push esi
mov esi, X+ofPtrDigits // esi = óêàçàòåëü íà ìëàäøèé áëîê
movzx ecx, byte ptr (X+ofMSB) // ecx = êîëè÷åñòâî áëîêîâ â ÷èñëå
mov eax, -1
xor edx, edx // Ñ÷åò÷èê îáðàáîòàííûõ áèòîâ
@NextBlock:
bsf eax, [esi]
jnz @BreakLoop // Íàéäåí íåíóëîé áëîê
add esi, BytesInCardinal
add edx, BitsInCardinal
loop @NextBlock
cmp eax, -1 // X = 0?
je @Exit
@BreakLoop:
add eax, edx
@Exit:
pop esi
end;
// ****************************************************************************
procedure ShiftLeft(var X: TBigint; N: Byte);
begin
// HighestBit(TDigits(X.PtrDigits^)[X.MSB]);
end;
// ****************************************************************************
function CompareAbs(A, B: Shortint): Shortint;
begin
A := Abs(A);
B := Abs(B);
if A > B then
Result := 1
else
if A = B then Result := 0 else Result := -1;
end;
function CompareAbs(A, B: Smallint): Shortint;
begin
A := Abs(A);
B := Abs(B);
if A > B then
Result := 1
else
if A = B then Result := 0 else Result := -1;
end;
function CompareAbs(A, B: Integer): Shortint;
begin
A := Abs(A);
B := Abs(B);
if A > B then
Result := 1
else
if A = B then Result := 0 else Result := -1;
end;
function CompareAbs(A, B: Int64): Shortint;
begin
A := Abs(A);
B := Abs(B);
if A > B then
Result := 1
else
if A = B then Result := 0 else Result := -1;
end;
function CompareAbs(const A, B: TBigint): Shortint;
var
PtrFirst, PtrSecond: PCardinal;
Count: Byte;
begin
// Çíàêè íàñ íå èíòåðåñóþò, ïîýòîìó ïðîâåðÿåì äëèíó
if A.MSB = B.MSB then // Àðãóìåíòû îäíîé äëèíû
begin
PtrFirst := A.PtrDigits;
PtrSecond := B.PtrDigits;
Count := A.MSB;
// Óñòàíàâëèâàåì óêàçàòåëü íà ñòàðøèé çíà÷àùèé áëîê
Inc(PtrFirst, Count-1);
Inc(PtrSecond, Count-1);
// Ïðîèçâîäèì ïîáëî÷íîå ñðàâíåíèå
while (PtrFirst^ = PtrSecond^) and (Count > 0) do
begin
Dec(PtrFirst);
Dec(PtrSecond);
Count := Count - 1;
end;
if Count = 0 then // Ñîâïàëè çíà÷åíèÿ âñåõ áëîêîâ
Result := 0
else // Àðãóìåíòû óæå òî÷íî íå ðàâíû
if PtrFirst^ > PtrSecond^ then Result := 1 else Result := -1;
end
else // Àðãóìåíòû ðàçíîé äëèíû, ïîýòîìó çàâåäîìî íå ðàâíû
if A.MSB > B.MSB then Result := 1 else Result := -1;
end;
function CompareAbs(const A: TBigint; B: Cardinal): Shortint;
begin
// Çíàê íàñ íå èíòåðåñóåò, ïðîâåðÿåì äëèíó
if A.MSB = 1 then // Àðãóìåíòû îäíîé äëèíû
begin
if PCardinal(A.PtrDigits)^ > B then
Result := 1
else
if PCardinal(A.PtrDigits)^ = B then Result := 0 else Result := -1;
end
else // Àðãóìåíòû ðàçíîé äëèíû, ïîýòîìó çàâåäîìî |A| > |B|
Result := 1;
end;
// ****************************************************************************
function Compare(A, B: Byte): Shortint;
begin
if A > B then
Result := 1
else
if A = B then Result := 0 else Result := -1;
end;
function Compare(A, B: Shortint): Shortint;
begin
if A > B then
Result := 1
else
if A = B then Result := 0 else Result := -1;
end;
function Compare(A, B: Word): Shortint;
begin
if A > B then
Result := 1
else
if A = B then Result := 0 else Result := -1;
end;
function Compare(A, B: Smallint): Shortint;
begin
if A > B then
Result := 1
else
if A = B then Result := 0 else Result := -1;
end;
function Compare(A, B: Cardinal): Shortint;
begin
if A > B then
Result := 1
else
if A = B then Result := 0 else Result := -1;
end;
function Compare(A, B: Integer): Shortint;
begin
if A > B then
Result := 1
else
if A = B then Result := 0 else Result := -1;
end;
function Compare(A, B: Int64): Shortint;
begin
if A > B then
Result := 1
else
if A = B then Result := 0 else Result := -1;
end;
function Compare(const A, B: TBigint): Shortint;
begin
// Ïðîâåðÿåì çíàêè àðãóìåíòîâ
if A.Sign > B.Sign then
begin
Result := 1;
Exit;
end;
if A.Sign < B.Sign then
begin
Result := -1;
Exit;
end;
// Àðãóìåíòû îäíîãî çíàêà, ïðîâåðÿåì äëèíó
if A.MSB = B.MSB then // Àðãóìåíòû îäíîé äëèíû
begin
if A.Sign = -1 then // A < 0, B < 0
Result := -CompareAbs(A, B)
else // A = 0, B = 0 èíà÷å A > 0, B > 0
if A.Sign = 0 then Result := 0 else Result := CompareAbs(A, B);
end
else // Àðãóìåíòû ðàçíîé äëèíû
begin
// Çàâåäîìî A.Sign <> 0, èíà÷å áû ñîâïàëè è äëèíû àðãóìåíòîâ
if A.Sign = -1 then // A < 0, B < 0
if A.MSB < B.MSB then Result := 1 else Result := -1
else // A > 0, B > 0
if A.MSB > B.MSB then Result := 1 else Result := -1;
end;
end;
end.