Browse Source

*implemented caching for some functions in ClpBigInteger.

Ugochukwu Mmaduekwe 7 years ago
parent
commit
4ac5e2799c
1 changed files with 29 additions and 29 deletions
  1. 29 29
      CryptoLib/src/Math/ClpBigInteger.pas

+ 29 - 29
CryptoLib/src/Math/ClpBigInteger.pas

@@ -749,7 +749,7 @@ function TBigInteger.QuickPow2Check: Boolean;
 begin
 begin
 
 
   // Result := (Fsign > 0) and (FnBits = 1);
   // Result := (Fsign > 0) and (FnBits = 1);
-  Result := (Fsign > 0) and (BitCount = 1);
+  Result := (Fsign > 0) and (BitCount = 1); // review
 end;
 end;
 
 
 function TBigInteger.Negate: TBigInteger;
 function TBigInteger.Negate: TBigInteger;
@@ -1785,41 +1785,41 @@ function TBigInteger.GetBitCount: Int32;
 var
 var
   sum, i: Int32;
   sum, i: Int32;
 begin
 begin
-  // if (FnBits = -1) then
-  // begin
-  if (Fsign < 0) then
-  begin
-    // TODO Optimise this case
-    FnBits := &Not().BitCount;
-  end
-  else
+  if (FnBits = -1) then
   begin
   begin
-    sum := 0;
-    for i := 0 to System.Pred(System.length(Fmagnitude)) do
+    if (Fsign < 0) then
+    begin
+      // TODO Optimise this case
+      FnBits := &Not().BitCount;
+    end
+    else
     begin
     begin
-      sum := sum + BitCnt(Fmagnitude[i]);
+      sum := 0;
+      for i := 0 to System.Pred(System.length(Fmagnitude)) do
+      begin
+        sum := sum + BitCnt(Fmagnitude[i]);
+      end;
+      FnBits := sum;
     end;
     end;
-    FnBits := sum;
   end;
   end;
-  // end;
 
 
   Result := FnBits;
   Result := FnBits;
 end;
 end;
 
 
 function TBigInteger.GetBitLength: Int32;
 function TBigInteger.GetBitLength: Int32;
 begin
 begin
-  // if (FnBitLength = -1) then
-  // begin
-  if Fsign = 0 then
+  if (FnBitLength = -1) then
   begin
   begin
-    FnBitLength := 0;
-  end
-  else
-  begin
-    FnBitLength := CalcBitLength(Fsign, 0, Fmagnitude);
-  end;
+    if Fsign = 0 then
+    begin
+      FnBitLength := 0;
+    end
+    else
+    begin
+      FnBitLength := CalcBitLength(Fsign, 0, Fmagnitude);
+    end;
 
 
-  // end;
+  end;
   Result := FnBitLength;
   Result := FnBitLength;
 end;
 end;
 
 
@@ -1984,11 +1984,11 @@ function TBigInteger.GetMQuote: Int32;
 var
 var
   d: Int32;
   d: Int32;
 begin
 begin
-  // if (FmQuote <> 0) then
-  // begin
-  // Result := FmQuote; // already calculated
-  // Exit;
-  // end;
+  if (FmQuote <> 0) then
+  begin
+    Result := FmQuote; // already calculated
+    Exit;
+  end;
 
 
 {$IFDEF DEBUG}
 {$IFDEF DEBUG}
   System.Assert(Fsign > 0);
   System.Assert(Fsign > 0);