Browse Source

+ added helper isabspowerof2, which checks whether abs(value) is a power of 2

git-svn-id: trunk@36796 -
nickysn 8 years ago
parent
commit
4b00414183
1 changed files with 15 additions and 0 deletions
  1. 15 0
      compiler/cutils.pas

+ 15 - 0
compiler/cutils.pas

@@ -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