mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-07 09:47:52 +02:00
+ added helper isabspowerof2, which checks whether abs(value) is a power of 2
git-svn-id: trunk@36796 -
This commit is contained in:
parent
7ee567fcca
commit
4b00414183
@ -108,6 +108,10 @@ interface
|
||||
}
|
||||
function ispowerof2(value : int64;out power : longint) : boolean;
|
||||
function ispowerof2(const value : Tconstexprint;out power : longint) : boolean;
|
||||
{# Returns true if abs(value) is a power of 2, the actual
|
||||
exponent value is returned in power.
|
||||
}
|
||||
function isabspowerof2(const value : Tconstexprint;out power : longint) : boolean;
|
||||
function nextpowerof2(value : int64; out power: longint) : int64;
|
||||
{$ifdef VER2_6} { only 2.7.1+ has a popcnt function in the system unit }
|
||||
function PopCnt(AValue : Byte): Byte;
|
||||
@ -929,6 +933,17 @@ implementation
|
||||
end;
|
||||
|
||||
|
||||
function isabspowerof2(const value : Tconstexprint;out power : longint) : boolean;
|
||||
begin
|
||||
if ispowerof2(value,power) then
|
||||
result:=true
|
||||
else if value.signed and (value.svalue<0) and (value.svalue<>low(int64)) and ispowerof2(-value.svalue,power) then
|
||||
result:=true
|
||||
else
|
||||
result:=false;
|
||||
end;
|
||||
|
||||
|
||||
function nextpowerof2(value : int64; out power: longint) : int64;
|
||||
{
|
||||
returns the power of 2 >= value
|
||||
|
Loading…
Reference in New Issue
Block a user