-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJclLogic.pas
More file actions
2427 lines (2198 loc) · 58.1 KB
/
Copy pathJclLogic.pas
File metadata and controls
2427 lines (2198 loc) · 58.1 KB
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{**************************************************************************************************}
{ }
{ Project JEDI Code Library (JCL) }
{ }
{ The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); }
{ you may not use this file except in compliance with the License. You may obtain a copy of the }
{ License at http://www.mozilla.org/MPL/ }
{ }
{ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF }
{ ANY KIND, either express or implied. See the License for the specific language governing rights }
{ and limitations under the License. }
{ }
{ The Original Code is JclLogic.pas. }
{ }
{ The Initial Developer of the Original Code is Marcel van Brakel. }
{ Portions created by Marcel van Brakel are Copyright (C) Marcel van Brakel. All rights reserved. }
{ }
{ Contributor(s): }
{ Marcel Bestebroer (marcelb) }
{ Marcel van Brakel }
{ ESB Consultancy }
{ Martin Kimmings }
{ Robert Marquardt (marquardt) }
{ Chris Morris }
{ Andreas Schmidt shmia at bizerba.de }
{ Michael Schnell }
{ Matthias Thoma (mthoma) }
{ Petr Vones (pvones) }
{ }
{**************************************************************************************************}
{ }
{ Various routines to perform various arithmetic and logical operations on one or more ordinal }
{ values (integer numbers). This includes various bit manipulation routines, min/max testing and }
{ conversion to string. }
{ }
{**************************************************************************************************}
{ }
{ Last modified: $Date:: $ }
{ Revision: $Rev:: $ }
{ Author: $Author:: $ }
{ }
{**************************************************************************************************}
{.$DEFINE PUREPASCAL}
unit JclLogic;
{$I jcl.inc}
{$RANGECHECKS OFF}
interface
{$IFDEF UNITVERSIONING}
uses
JclUnitVersioning;
{$ENDIF UNITVERSIONING}
// Conversion
function OrdToBinary(Value: Byte): string; overload;
function OrdToBinary(Value: ShortInt): string; overload;
function OrdToBinary(Value: SmallInt): string; overload;
function OrdToBinary(Value: Word): string; overload;
function OrdToBinary(Value: Integer): string; overload;
function OrdToBinary(Value: Cardinal): string; overload;
function OrdToBinary(Value: Int64): string; overload;
// Bit manipulation
type
TBitRange = Byte;
TBooleanArray = array of Boolean;
function BitsHighest(X: Byte): Integer; overload;
function BitsHighest(X: ShortInt): Integer; overload;
function BitsHighest(X: SmallInt): Integer; overload;
function BitsHighest(X: Word): Integer; overload;
function BitsHighest(X: Integer): Integer; overload;
function BitsHighest(X: Cardinal): Integer; overload;
function BitsHighest(X: Int64): Integer; overload;
function BitsLowest(X: Byte): Integer; overload;
function BitsLowest(X: Shortint): Integer; overload;
function BitsLowest(X: Smallint): Integer; overload;
function BitsLowest(X: Word): Integer; overload;
function BitsLowest(X: Cardinal): Integer; overload;
function BitsLowest(X: Integer): Integer; overload;
function BitsLowest(X: Int64): Integer; overload;
function ClearBit(const Value: Byte; const Bit: TBitRange): Byte; overload;
function ClearBit(const Value: Shortint; const Bit: TBitRange): Shortint; overload;
function ClearBit(const Value: Smallint; const Bit: TBitRange): Smallint; overload;
function ClearBit(const Value: Word; const Bit: TBitRange): Word; overload;
function ClearBit(const Value: Integer; const Bit: TBitRange): Integer; overload;
function ClearBit(const Value: Cardinal; const Bit: TBitRange): Cardinal; overload;
function ClearBit(const Value: Int64; const Bit: TBitRange): Int64; overload;
procedure ClearBitBuffer(var Value; const Bit: Cardinal); overload;
{$IFDEF CPU64}
procedure ClearBitBuffer(var Value; const Bit: Int64); overload;
{$ENDIF CPU64}
function CountBitsSet(X: Byte): Integer; overload;
function CountBitsSet(X: Word): Integer; overload;
function CountBitsSet(X: Smallint): Integer; overload;
function CountBitsSet(X: ShortInt): Integer; overload;
function CountBitsSet(X: Integer): Integer; overload;
function CountBitsSet(X: Cardinal): Integer; overload;
function CountBitsSet(X: Int64): Integer; overload;
function CountBitsSet(P: Pointer; Count: Cardinal): Cardinal; overload;
function CountBitsCleared(X: Byte): Integer; overload;
function CountBitsCleared(X: Shortint): Integer; overload;
function CountBitsCleared(X: Smallint): Integer; overload;
function CountBitsCleared(X: Word): Integer; overload;
function CountBitsCleared(X: Integer): Integer; overload;
function CountBitsCleared(X: Cardinal): Integer; overload;
function CountBitsCleared(X: Int64): Integer; overload;
function CountBitsCleared(P: Pointer; Count: Cardinal): Cardinal; overload;
function LRot(const Value: Byte; const Count: TBitRange): Byte; overload;
function LRot(const Value: Word; const Count: TBitRange): Word; overload;
function LRot(const Value: Integer; const Count: TBitRange): Integer; overload;
function LRot(const Value: Int64; const Count: TBitRange): Int64; overload;
function ReverseBits(Value: Byte): Byte; overload;
function ReverseBits(Value: Shortint): Shortint; overload;
function ReverseBits(Value: Smallint): Smallint; overload;
function ReverseBits(Value: Word): Word; overload;
function ReverseBits(Value: Integer): Integer; overload;
function ReverseBits(Value: Cardinal): Cardinal; overload;
function ReverseBits(Value: Int64): Int64; overload;
function ReverseBits(P: Pointer; Count: Integer): Pointer; overload;
function RRot(const Value: Byte; const Count: TBitRange): Byte; overload;
function RRot(const Value: Word; const Count: TBitRange): Word; overload;
function RRot(const Value: Integer; const Count: TBitRange): Integer; overload;
function RRot(const Value: Int64; const Count: TBitRange): Int64; overload;
function Sar(const Value: Shortint; const Count: TBitRange): Shortint; overload;
function Sar(const Value: Smallint; const Count: TBitRange): Smallint; overload;
function Sar(const Value: Integer; const Count: TBitRange): Integer; overload;
function SetBit(const Value: Byte; const Bit: TBitRange): Byte; overload;
function SetBit(const Value: Shortint; const Bit: TBitRange): Shortint; overload;
function SetBit(const Value: Smallint; const Bit: TBitRange): Smallint; overload;
function SetBit(const Value: Word; const Bit: TBitRange): Word; overload;
function SetBit(const Value: Cardinal; const Bit: TBitRange): Cardinal; overload;
function SetBit(const Value: Integer; const Bit: TBitRange): Integer; overload;
function SetBit(const Value: Int64; const Bit: TBitRange): Int64; overload;
procedure SetBitBuffer(var Value; const Bit: Cardinal); overload;
{$IFDEF CPU64}
procedure SetBitBuffer(var Value; const Bit: Int64); overload;
{$ENDIF CPU64}
function TestBit(const Value: Byte; const Bit: TBitRange): Boolean; overload;
function TestBit(const Value: Shortint; const Bit: TBitRange): Boolean; overload;
function TestBit(const Value: Smallint; const Bit: TBitRange): Boolean; overload;
function TestBit(const Value: Word; const Bit: TBitRange): Boolean; overload;
function TestBit(const Value: Cardinal; const Bit: TBitRange): Boolean; overload;
function TestBit(const Value: Integer; const Bit: TBitRange): Boolean; overload;
function TestBit(const Value: Int64; const Bit: TBitRange): Boolean; overload;
function TestBitBuffer(const Value; const Bit: Cardinal): Boolean; overload;
{$IFDEF CPU64}
function TestBitBuffer(const Value; const Bit: Int64): Boolean; overload;
{$ENDIF CPU64}
function TestBits(const Value, Mask: Byte): Boolean; overload;
function TestBits(const Value, Mask: Shortint): Boolean; overload;
function TestBits(const Value, Mask: Smallint): Boolean; overload;
function TestBits(const Value, Mask: Word): Boolean; overload;
function TestBits(const Value, Mask: Cardinal): Boolean; overload;
function TestBits(const Value, Mask: Integer): Boolean; overload;
function TestBits(const Value, Mask: Int64): Boolean; overload;
function ToggleBit(const Value: Byte; const Bit: TBitRange): Byte; overload;
function ToggleBit(const Value: Shortint; const Bit: TBitRange): Shortint; overload;
function ToggleBit(const Value: Smallint; const Bit: TBitRange): Smallint; overload;
function ToggleBit(const Value: Word; const Bit: TBitRange): Word; overload;
function ToggleBit(const Value: Cardinal; const Bit: TBitRange): Cardinal; overload;
function ToggleBit(const Value: Integer; const Bit: TBitRange): Integer; overload;
function ToggleBit(const Value: Int64; const Bit: TBitRange): Int64; overload;
procedure ToggleBitBuffer(var Value; const Bit: Cardinal); overload;
{$IFDEF CPU64}
procedure ToggleBitBuffer(var Value; const Bit: Int64); overload;
{$ENDIF CPU64}
procedure BooleansToBits(var Dest: Byte; const B: TBooleanArray); overload;
procedure BooleansToBits(var Dest: Word; const B: TBooleanArray); overload;
procedure BooleansToBits(var Dest: Integer; const B: TBooleanArray); overload;
procedure BooleansToBits(var Dest: Int64; const B: TBooleanArray); overload;
procedure BitsToBooleans(const Bits: Byte; var B: TBooleanArray; AllBits: Boolean = False); overload;
procedure BitsToBooleans(const Bits: Word; var B: TBooleanArray; AllBits: Boolean = False); overload;
procedure BitsToBooleans(const Bits: Integer; var B: TBooleanArray; AllBits: Boolean = False); overload;
procedure BitsToBooleans(const Bits: Int64; var B: TBooleanArray; AllBits: Boolean = False); overload;
function BitsNeeded(const X: Byte): Integer; overload;
function BitsNeeded(const X: Word): Integer; overload;
function BitsNeeded(const X: Integer): Integer; overload;
function BitsNeeded(const X: Int64): Integer; overload;
function Digits(const X: Cardinal): Integer;
function ReverseBytes(Value: Word): Word; overload;
function ReverseBytes(Value: Smallint): Smallint; overload;
function ReverseBytes(Value: Integer): Integer; overload;
function ReverseBytes(Value: Cardinal): Cardinal; overload;
function ReverseBytes(Value: Int64): Int64; overload;
function ReverseBytes(P: Pointer; Count: Integer): Pointer; overload;
// Arithmetic
procedure SwapOrd(var I, J: Byte); overload;
procedure SwapOrd(var I, J: Shortint); overload;
procedure SwapOrd(var I, J: Smallint); overload;
procedure SwapOrd(var I, J: Word); overload;
procedure SwapOrd(var I, J: Integer); overload;
procedure SwapOrd(var I, J: Cardinal); overload;
procedure SwapOrd(var I, J: Int64); overload;
function IncLimit(var B: Byte; const Limit: Byte; const Incr: Byte = 1): Byte; overload;
function IncLimit(var B: Shortint; const Limit: Shortint; const Incr: Shortint = 1): Shortint; overload;
function IncLimit(var B: Smallint; const Limit: Smallint; const Incr: Smallint = 1): Smallint; overload;
function IncLimit(var B: Word; const Limit: Word; const Incr: Word = 1): Word; overload;
function IncLimit(var B: Integer; const Limit: Integer; const Incr: Integer = 1): Integer; overload;
function IncLimit(var B: Cardinal; const Limit: Cardinal; const Incr: Cardinal = 1): Cardinal; overload;
function IncLimit(var B: Int64; const Limit: Int64; const Incr: Int64 = 1): Int64; overload;
function DecLimit(var B: Byte; const Limit: Byte; const Decr: Byte = 1): Byte; overload;
function DecLimit(var B: Shortint; const Limit: Shortint; const Decr: Shortint = 1): Shortint; overload;
function DecLimit(var B: Smallint; const Limit: Smallint; const Decr: Smallint = 1): Smallint; overload;
function DecLimit(var B: Word; const Limit: Word; const Decr: Word = 1): Word; overload;
function DecLimit(var B: Integer; const Limit: Integer; const Decr: Integer = 1): Integer; overload;
function DecLimit(var B: Cardinal; const Limit: Cardinal; const Decr: Cardinal = 1): Cardinal; overload;
function DecLimit(var B: Int64; const Limit: Int64; const Decr: Int64 = 1): Int64; overload;
function IncLimitClamp(var B: Byte; const Limit: Byte; const Incr: Byte = 1): Byte; overload;
function IncLimitClamp(var B: Shortint; const Limit: Shortint; const Incr: Shortint = 1): Shortint; overload;
function IncLimitClamp(var B: Smallint; const Limit: Smallint; const Incr: Smallint = 1): Smallint; overload;
function IncLimitClamp(var B: Word; const Limit: Word; const Incr: Word = 1): Word; overload;
function IncLimitClamp(var B: Integer; const Limit: Integer; const Incr: Integer = 1): Integer; overload;
function IncLimitClamp(var B: Cardinal; const Limit: Cardinal; const Incr: Cardinal = 1): Cardinal; overload;
function IncLimitClamp(var B: Int64; const Limit: Int64; const Incr: Int64 = 1): Int64; overload;
function DecLimitClamp(var B: Byte; const Limit: Byte; const Decr: Byte = 1): Byte; overload;
function DecLimitClamp(var B: Shortint; const Limit: Shortint; const Decr: Shortint = 1): Shortint; overload;
function DecLimitClamp(var B: Smallint; const Limit: Smallint; const Decr: Smallint = 1): Smallint; overload;
function DecLimitClamp(var B: Word; const Limit: Word; const Decr: Word = 1): Word; overload;
function DecLimitClamp(var B: Integer; const Limit: Integer; const Decr: Integer = 1): Integer; overload;
function DecLimitClamp(var B: Cardinal; const Limit: Cardinal; const Decr: Cardinal = 1): Cardinal; overload;
function DecLimitClamp(var B: Int64; const Limit: Int64; const Decr: Int64 = 1): Int64; overload;
function Max(const B1, B2: Byte): Byte; overload;
function Max(const B1, B2: Shortint): Shortint; overload;
function Max(const B1, B2: Smallint): Smallint; overload;
function Max(const B1, B2: Word): Word; overload;
function Max(const B1, B2: Integer): Integer; overload;
function Max(const B1, B2: Cardinal): Cardinal; overload;
function Max(const B1, B2: Int64): Int64; overload;
function Min(const B1, B2: Byte): Byte; overload;
function Min(const B1, B2: Shortint): Shortint; overload;
function Min(const B1, B2: Smallint): Smallint; overload;
function Min(const B1, B2: Word): Word; overload;
function Min(const B1, B2: Integer): Integer; overload;
function Min(const B1, B2: Cardinal): Cardinal; overload;
function Min(const B1, B2: Int64): Int64; overload;
const
// Constants defining the number of bits in each Integer type
BitsPerNibble = 4;
BitsPerByte = 8;
BitsPerShortint = SizeOf(Shortint) * BitsPerByte;
BitsPerSmallint = SizeOf(Smallint) * BitsPerByte;
BitsPerWord = SizeOf(Word) * BitsPerByte;
BitsPerInteger = SizeOf(Integer) * BitsPerByte;
BitsPerCardinal = SizeOf(Cardinal) * BitsPerByte;
BitsPerInt64 = SizeOf(Int64) * BitsPerByte;
// Constants defining the number of nibbles in each Integer type
NibblesPerByte = BitsPerByte div BitsPerNibble;
NibblesPerShortint = SizeOf(Shortint) * NibblesPerByte;
NibblesPerSmallint = SizeOf(Smallint) * NibblesPerByte;
NibblesPerWord = SizeOf(Word) * NibblesPerByte;
NibblesPerInteger = SizeOf(Integer) * NibblesPerByte;
NibblesPerCardinal = SizeOf(Cardinal) * NibblesPerByte;
NibblesPerInt64 = SizeOf(Int64) * NibblesPerByte;
// Constants defining a mask with all bits set for each Integer type
NibbleMask = $F;
ByteMask = Byte($FF);
ShortintMask = Shortint($FF);
SmallintMask = Smallint($FFFF);
WordMask = Word($FFFF);
IntegerMask = Integer($FFFFFFFF);
CardinalMask = Cardinal($FFFFFFFF);
Int64Mask = Int64($FFFFFFFFFFFFFFFF);
{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$URL$';
Revision: '$Revision$';
Date: '$Date$';
LogPath: 'JCL\source\common';
Extra: '';
Data: nil
);
{$ENDIF UNITVERSIONING}
implementation
uses
JclBase;
// Conversion
function OrdToBinary(Value: Byte): string;
var
I: Integer;
begin
SetLength(Result, BitsPerByte);
for I := Length(Result) - 1 downto 0 do
begin
Result[I + 1] := Chr(48 + (Value and $00000001));
Value := Value shr 1;
end;
end;
function OrdToBinary(Value: Shortint): string;
var
I: Integer;
begin
SetLength(Result, BitsPerShortint);
for I := Length(Result) - 1 downto 0 do
begin
Result[I + 1] := Chr(48 + (Value and $00000001));
Value := Value shr 1;
end;
end;
function OrdToBinary(Value: Smallint): string;
var
I: Integer;
S: Smallint;
begin
SetLength(Result, BitsPerSmallint);
S := Value;
for I := Length(Result) - 1 downto 0 do
begin
Result[I + 1] := Chr(48 + (S and $00000001));
S := S shr 1;
end;
end;
function OrdToBinary(Value: Word): string;
var
I: Integer;
begin
SetLength(Result, BitsPerWord);
for I := Length(Result) - 1 downto 0 do
begin
Result[I + 1] := Chr(48 + (Value and $00000001));
Value := Value shr 1;
end;
end;
function OrdToBinary(Value: Integer): string;
var
I: Integer;
begin
SetLength(Result, BitsPerInteger);
for I := Length(Result) - 1 downto 0 do
begin
Result[I + 1] := Chr(48 + (Value and $00000001));
Value := Value shr 1;
end;
end;
function OrdToBinary(Value: Cardinal): string;
var
I: Integer;
begin
SetLength(Result, BitsPerCardinal);
for I := Length(Result) - 1 downto 0 do
begin
Result[I + 1] := Chr(48 + (Value and $00000001));
Value := Value shr 1;
end;
end;
function OrdToBinary(Value: Int64): string;
var
I: Integer;
begin
SetLength(Result, BitsPerInt64);
for I := Length(Result) - 1 downto 0 do
begin
Result[I + 1] := Chr(48 + (Value and Int64(1)));
Value := Value shr Int64(1);
end;
end;
// Bit manipulation
function BitsHighest(X: Cardinal): Integer;
asm
{$IFDEF CPU32}
// --> EAX X
// <-- EAX
MOV ECX, EAX
MOV EAX, -1
BSR EAX, ECX
JNZ @@End
MOV EAX, -1
@@End:
{$ENDIF CPU32}
{$IFDEF CPU64}
// --> ECX X
// <-- RAX
MOV EAX, -1
MOV R10D, EAX
BSR EAX, ECX
CMOVZ EAX, R10D
{$ENDIF CPU64}
end;
function BitsHighest(X: Integer): Integer;
asm
{$IFDEF CPU32}
// --> EAX X
// <-- EAX
MOV ECX, EAX
MOV EAX, -1
BSR EAX, ECX
JNZ @@End
MOV EAX, -1
@@End:
{$ENDIF CPU32}
{$IFDEF CPU64}
// --> ECX X
// <-- RAX
MOV EAX, -1
MOV R10D, EAX
BSR EAX, ECX
CMOVZ EAX, R10D
{$ENDIF CPU64}
end;
function BitsHighest(X: Byte): Integer;
begin
Result := BitsHighest(Cardinal(X));
end;
function BitsHighest(X: Word): Integer;
begin
Result := BitsHighest(Cardinal(X));
end;
function BitsHighest(X: SmallInt): Integer;
begin
Result := BitsHighest(Integer(X));
end;
function BitsHighest(X: ShortInt): Integer;
begin
Result := BitsHighest(Integer(X));
end;
function BitsHighest(X: Int64): Integer;
{$IFDEF CPU32}
begin
if TJclULargeInteger(X).HighPart = 0 then
Result := BitsHighest(TJclULargeInteger(X).LowPart)
else
Result := BitsHighest(TJclULargeInteger(X).HighPart) + 32;
end;
{$ENDIF CPU32}
{$IFDEF CPU64}
asm
// --> RCX X
// <-- RAX
// this is much shorter than "MOV RAX, -1"
XOR RAX, RAX
DEC EAX
MOV R10, RAX
BSR RAX, RCX
CMOVZ RAX, R10
end;
{$ENDIF CPU64}
function BitsLowest(X: Cardinal): Integer;
asm
{$IFDEF CPU32}
// --> EAX X
// <-- EAX
MOV ECX, EAX
MOV EAX, -1
BSF EAX, ECX
JNZ @@End
MOV EAX, -1
@@End:
{$ENDIF CPU32}
{$IFDEF CPU64}
// --> RCX X
// <-- EAX
MOV EAX, -1
MOV R10D, EAX
BSF EAX, ECX
CMOVZ EAX, R10D
{$ENDIF CPU64}
end;
function BitsLowest(X: Integer): Integer;
asm
{$IFDEF CPU32}
// --> EAX X
// <-- EAX
MOV ECX, EAX
MOV EAX, -1
BSF EAX, ECX
JNZ @@End
MOV EAX, -1
@@End:
{$ENDIF CPU32}
{$IFDEF CPU64}
// --> RCX X
// <-- EAX
MOV EAX, -1
MOV R10D, EAX
BSF EAX, ECX
CMOVZ EAX, R10D
{$ENDIF CPU64}
end;
function BitsLowest(X: Byte): Integer;
begin
Result := BitsLowest(Cardinal(X));
end;
function BitsLowest(X: Shortint): Integer;
begin
Result := BitsLowest(Integer(X));
end;
function BitsLowest(X: Smallint): Integer;
begin
Result := BitsLowest(Integer(X));
end;
function BitsLowest(X: Word): Integer;
begin
Result := BitsLowest(Cardinal(X));
end;
function BitsLowest(X: Int64): Integer;
{$IFDEF CPU32}
begin
if TJclULargeInteger(X).LowPart = 0 then
Result := BitsLowest(TJclULargeInteger(X).HighPart) + 32
else
Result := BitsLowest(TJclULargeInteger(X).LowPart);
end;
{$ENDIF CPU32}
{$IFDEF CPU64}
asm
// --> RCX X
// <-- RAX
// this is much shorter than "MOV RAX, -1"
XOR RAX, RAX
DEC EAX
MOV R10, RAX
BSF RAX, RCX
CMOVZ RAX, R10
end;
{$ENDIF CPU64}
function ClearBit(const Value: Byte; const Bit: TBitRange): Byte;
asm
// 32 --> AL Value
// DL Bit
// <-- AL Result
// 64 --> CL Value
// DL Bit
// <-- AL Result
AND EDX, BitsPerByte - 1 // modulo BitsPerByte
{$IFDEF CPU64}
MOVZX EAX, CL
{$ENDIF CPU64}
BTR EAX, EDX
end;
function ClearBit(const Value: Shortint; const Bit: TBitRange): Shortint;
asm
// 32 --> AL Value
// DL Bit
// <-- AL Result
// 64 --> CL Value
// DL Bit
// <-- AL Result
AND EDX, BitsPerShortint - 1 // modulo BitsPerShortint
{$IFDEF CPU64}
MOVZX EAX, CL
{$ENDIF CPU64}
BTR EAX, EDX
end;
function ClearBit(const Value: Smallint; const Bit: TBitRange): Smallint;
asm
// 32 --> AX Value
// DL Bit
// <-- AX Result
// 64 --> CX Value
// DL Bit
// <-- AX Result
AND EDX, BitsPerSmallint - 1 // modulo BitsPerSmallint
{$IFDEF CPU64}
MOVZX EAX, CX
{$ENDIF CPU64}
BTR EAX, EDX
end;
function ClearBit(const Value: Word; const Bit: TBitRange): Word;
asm
// 32 --> AX Value
// DL Bit
// <-- AX Result
// 64 --> CX Value
// DL Bit
// <-- AX Result
AND EDX, BitsPerWord - 1 // modulo BitsPerWord
{$IFDEF CPU64}
MOVZX EAX, CX
{$ENDIF CPU64}
BTR EAX, EDX
end;
function ClearBit(const Value: Cardinal; const Bit: TBitRange): Cardinal;
asm
// 32 --> EAX Value
// DL Bit
// <-- EAX Result
// 64 --> ECX Value
// DL Bit
// <-- EAX Result
{$IFDEF CPU64}
MOV EAX, ECX
{$ENDIF CPU64}
BTR EAX, EDX
end;
function ClearBit(const Value: Integer; const Bit: TBitRange): Integer;
asm
// 32 --> EAX Value
// DL Bit
// <-- EAX Result
// 64 --> ECX Value
// DL Bit
// <-- EAX Result
{$IFDEF CPU64}
MOV EAX, ECX
{$ENDIF CPU64}
BTR EAX, EDX
end;
function ClearBit(const Value: Int64; const Bit: TBitRange): Int64;
{$IFDEF CPU32}
begin
Result := Value and not (Int64(1) shl (Bit and (BitsPerInt64 - 1)));
end;
{$ENDIF CPU32}
{$IFDEF CPU64}
asm
// --> RCX Value
// DL Bit
// <-- RAX Result
MOV RAX, RCX
BTR RAX, RDX
end;
{$ENDIF CPU64}
procedure ClearBitBuffer(var Value; const Bit: Cardinal);
{$IFDEF PUREPASCAL}
var
P: PByte;
BitOfs: TBitRange;
begin
P := Addr(Value);
Inc(P, Bit div 8);
BitOfs := Bit mod 8;
P^ := ClearBit(P^, BitOfs);
end;
{$ELSE ~PUREPASCAL}
asm
// 32 --> EAX Value
// EDX Bit
// 64 --> RCX Value
// EDX Bit
BTR [Value], Bit
end;
{$ENDIF ~PUREPASCAL}
{$IFDEF CPU64}
procedure ClearBitBuffer(var Value; const Bit: Int64);
{$IFDEF PUREPASCAL}
var
P: PByte;
BitOfs: TBitRange;
begin
P := Addr(Value);
Inc(P, Bit div 8);
BitOfs := Bit mod 8;
P^ := ClearBit(P^, BitOfs);
end;
{$ELSE ~PUREPASCAL}
asm
// 64 --> RCX Value
// RDX Bit
BTR [Value], Bit
end;
{$ENDIF ~PUREPASCAL}
{$ENDIF CPU64}
const
BitSetPerNibble: array[0..15] of Integer = (0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4);
function CountBitsSet(X: Cardinal): Integer;
var
Index: Integer;
begin
Result := 0;
for Index := 0 to NibblesPerCardinal - 1 do
begin
Inc(Result, BitSetPerNibble[X and $F]);
X := X shr BitsPerNibble;
end;
end;
function CountBitsSet(X: Byte): Integer;
begin
Result := BitSetPerNibble[X shr BitsPerNibble] + BitSetPerNibble[X and $F];
end;
function CountBitsSet(X: Word): Integer;
var
Index: Integer;
begin
Result := 0;
for Index := 0 to NibblesPerWord - 1 do
begin
Inc(Result, BitSetPerNibble[X and $F]);
X := X shr BitsPerNibble;
end;
end;
function CountBitsSet(X: Smallint): Integer;
var
Index: Integer;
begin
Result := 0;
for Index := 0 to NibblesPerSmallint - 1 do
begin
Inc(Result, BitSetPerNibble[X and $F]);
X := X shr BitsPerNibble;
end;
end;
function CountBitsSet(X: ShortInt): Integer;
begin
Result := BitSetPerNibble[X shr BitsPerNibble] + BitSetPerNibble[X and $F];
end;
function CountBitsSet(X: Integer): Integer;
var
Index: Integer;
begin
Result := 0;
for Index := 0 to NibblesPerInteger - 1 do
begin
Inc(Result, BitSetPerNibble[X and $F]);
X := X shr BitsPerNibble;
end;
end;
function CountBitsSet(P: Pointer; Count: Cardinal): Cardinal;
var
b: Byte;
begin
Result := 0;
while Count > 0 do
begin
b := PByte(P)^;
// lower Nibble
Inc(Result, BitSetPerNibble[b and $0F]);
// upper Nibble
Inc(Result, BitSetPerNibble[b shr BitsPerNibble]);
Dec(Count);
Inc(PByte(P));
end;
end;
function CountBitsSet(X: Int64): Integer;
begin
Result := CountBitsSet(TJclULargeInteger(X).LowPart) + CountBitsSet(TJclULargeInteger(X).HighPart);
end;
function CountBitsCleared(X: Byte): Integer;
begin
Result := BitsPerByte - CountBitsSet(Byte(X));
end;
function CountBitsCleared(X: Shortint): Integer;
begin
Result := BitsPerShortint - CountBitsSet(Byte(X));
end;
function CountBitsCleared(X: Smallint): Integer;
begin
Result := BitsPerSmallint - CountBitsSet(Word(X));
end;
function CountBitsCleared(X: Word): Integer;
begin
Result := BitsPerWord - CountBitsSet(Word(X));
end;
function CountBitsCleared(X: Integer): Integer;
begin
Result := BitsPerInteger - CountBitsSet(Integer(X));
end;
function CountBitsCleared(X: Cardinal): Integer;
begin
Result := BitsPerCardinal - CountBitsSet(Cardinal(X));
end;
function CountBitsCleared(X: Int64): Integer;
begin
Result := BitsPerInt64 - CountBitsSet(Int64(X));
end;
function CountBitsCleared(P: Pointer; Count: Cardinal): Cardinal;
begin
Result := Count * BitsPerByte - CountBitsSet(P, Count);
end;
function LRot(const Value: Byte; const Count: TBitRange): Byte;
asm
{$IFDEF CPU32}
// --> AL Value
// DL Count
// <-- AL Result
MOV CL, DL
ROL AL, CL
{$ENDIF CPU32}
{$IFDEF CPU64}
// --> CL Value
// DL Count
// <-- AL Result
MOV AL, CL
MOV CL, DL
ROL AL, CL
{$ENDIF CPU64}
end;
function LRot(const Value: Word; const Count: TBitRange): Word;
asm
{$IFDEF CPU32}
// --> AX Value
// DL Count
// <-- AX Result
MOV CL, DL
ROL AX, CL
{$ENDIF CPU32}
{$IFDEF CPU64}
// --> CX Value
// DL Count
// <-- AX Result
MOV AX, CX
MOV CL, DL
ROL AX, CL
{$ENDIF CPU64}
end;
function LRot(const Value: Integer; const Count: TBitRange): Integer;
asm
{$IFDEF CPU32}
// --> EAX Value
// DL Count
// <-- EAX Result
MOV CL, DL
ROL EAX, CL
{$ENDIF CPU32}
{$IFDEF CPU64}
// --> ECX Value
// DL Count
// <-- EAX Result
MOV EAX, ECX
MOV CL, DL
ROL EAX, CL
{$ENDIF CPU64}
end;
function LRot(const Value: Int64; const Count: TBitRange): Int64;
{$IFDEF CPU32}
asm
// --> Value on stack
// AL Count
// <-- EDX:EAX Result
PUSH ESI
PUSH EDI
MOV CL, Count
MOV EDX, DWORD PTR [Value + 4]
MOV EAX, DWORD PTR [Value]
// Count := Count mod 64
AND CL, $3F
JZ @@End
CMP CL, 32
JL @@RolBits
// Count >= 32: "rol Count" = "rol 32" + "rol (32 - Count)"
@@Swap:
// "rol 32"
XCHG EAX, EDX
// Count := 32 - Count
SUB CL, 32
@@RolBits:
MOV EDI, EDX
MOV ESI, EAX
// shift the bits
SHL EDX, CL
SHL EAX, CL
// CounterShiftCount := 32 - Count
NEG CL
ADD CL, 32
// bitwise-or the counter shifted bits
SHR EDI, CL
SHR ESI, CL
OR EAX, EDI
OR EDX, ESI
@@End:
POP EDI
POP ESI
end;
{$ENDIF CPU32}
{$IFDEF CPU64}
asm
// --> RCX Value
// DL Count
// <-- RAX Result
MOV RAX, RCX
MOV CL, DL
ROL RAX, CL
end;
{$ENDIF CPU64}
function RRot(const Value: Int64; const Count: TBitRange): Int64;
{$IFDEF CPU32}
begin
Result := LRot(Value, 64 - (Count and $3F));
end;
{$ENDIF CPU32}
{$IFDEF CPU64}
asm
// --> RCX Value
// DL Count
// <-- RAX Result
MOV RAX, RCX
MOV CL, DL
ROR RAX, CL
end;
{$ENDIF CPU64}
const
// Lookup table of bit reversed nibbles, used by simple overloads of ReverseBits
RevNibbles: array [0..NibbleMask] of Byte =
($0, $8, $4, $C, $2, $A, $6, $E, $1, $9, $5, $D, $3, $B, $7, $F);
function ReverseBits(Value: Byte): Byte;
begin
Result := RevNibbles[Value shr BitsPerNibble] or
(RevNibbles[Value and NibbleMask] shl BitsPerNibble);
end;
function ReverseBits(Value: Shortint): Shortint;
begin
Result := RevNibbles[Byte(Value) shr BitsPerNibble] or
(RevNibbles[Value and NibbleMask] shl BitsPerNibble);