Skip to content

Commit 505ff34

Browse files
committed
fixed NextPowerOf2
1 parent 742cda1 commit 505ff34

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

Source/Base/Spring.pas

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3113,9 +3113,9 @@ procedure IncUnchecked(var i: Integer; const n: Integer = 1); inline;
31133113

31143114
procedure SwapPtr(arr: PPointer; left, right: Integer); inline;
31153115

3116-
function IsPowerOf2(value: Integer): Boolean;
3116+
function IsPowerOf2(value: NativeInt): Boolean;
31173117

3118-
function NextPowerOf2(value: Integer): Integer;
3118+
function NextPowerOf2(value: NativeInt): NativeInt;
31193119

31203120
// copy from System.pas to make it possible to inline
31213121
function DynArrayLength(const A: Pointer): NativeInt; inline;
@@ -4138,25 +4138,37 @@ procedure IncUnchecked(var i: Integer; const n: Integer = 1); inline;
41384138
{$IFDEF OVERFLOWCHECKS_ON}{$Q+}{$ENDIF}
41394139
end;
41404140

4141-
function IsPowerOf2(value: Integer): Boolean;
4141+
function IsPowerOf2(value: NativeInt): Boolean;
41424142
begin
41434143
Result := (value > 0) and (value and (value - 1) = 0);
41444144
end;
41454145

4146-
function NextPowerOf2(value: Integer): Integer;
4147-
{$IFDEF MSWINDOWS}
4146+
function NextPowerOf2(value: NativeInt): NativeInt;
4147+
{$IFDEF ASSEMBLER}
4148+
{$IFDEF CPUX86}
41484149
asm
4149-
cmp value, 0
4150+
test eax, eax
41504151
jle @negative
4151-
bsr ecx, value
4152-
add ecx, 1
4153-
mov eax, 1
4152+
bsr ecx, eax
4153+
mov eax, 2
41544154
shl eax, cl
41554155
ret
41564156
@negative:
41574157
mov eax, 1
41584158
end;
41594159
{$ELSE}
4160+
asm
4161+
test rcx, rcx
4162+
jle @negative
4163+
bsr rcx, rcx
4164+
mov eax, 2
4165+
shl rax, cl
4166+
ret
4167+
@negative:
4168+
mov eax, 1
4169+
end;
4170+
{$ENDIF}
4171+
{$ELSE}
41604172
begin
41614173
Result := 1;
41624174
while (Result <= value) and (Result > 0) do

Tests/Source/Base/Spring.Tests.Base.pas

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4235,9 +4235,9 @@ procedure TTestEnum.TestParseStringException;
42354235

42364236
procedure TTestBaseRoutines.TestNextPowerOf2;
42374237

4238-
procedure TestRange(Low, High, Power: Integer);
4238+
procedure TestRange(Low, High, Power: NativeInt);
42394239
var
4240-
i: Integer;
4240+
i: NativeInt;
42414241
begin
42424242
for i := Low to High do
42434243
CheckEquals(Power, NextPowerOf2(i), Format('NextPowerOf2(%d) did not return %d', [i, Power]));
@@ -4254,8 +4254,8 @@ procedure TTestBaseRoutines.TestNextPowerOf2;
42544254
TestRange( 16, 31, 32);
42554255

42564256
TestRange(Pow_2_30 - 50, Pow_2_30 - 1, Pow_2_30);
4257-
TestRange(High(Integer) div 2 + 1, High(Integer) div 2 + 2, Integer($80000000));
4258-
TestRange(High(Integer) - 1, High(Integer), Integer($80000000));
4257+
TestRange(High(NativeInt) div 2 + 1, High(NativeInt) div 2 + 2, Low(NativeInt));
4258+
TestRange(High(NativeInt) - 1, High(NativeInt), Low(NativeInt));
42594259
end;
42604260

42614261
{$ENDREGION}

0 commit comments

Comments
 (0)