New hwSqrt which is up to 8.7% more efficient than old one on synthetic tests
authorunc0rr
Sun, 16 Sep 2012 22:55:33 +0400
changeset 7688 9daa06188551
parent 7686 fcdc864cdbef
child 7690 6ef121a80cb0
New hwSqrt which is up to 8.7% more efficient than old one on synthetic tests
hedgewars/uFloat.pas
--- a/hedgewars/uFloat.pas	Sun Sep 16 09:33:57 2012 +0400
+++ b/hedgewars/uFloat.pas	Sun Sep 16 22:55:33 2012 +0400
@@ -491,6 +491,9 @@
 end;
 
 function hwSqrt(const t: hwFloat): hwFloat;
+const pwr = 8; // even value, feel free to adjust
+      rThreshold = 1 shl (pwr + 32);
+      lThreshold = 1 shl (pwr div 2 + 32);
 var l, r: QWord;
     c: hwFloat;
 begin
@@ -503,14 +506,24 @@
     end
 else
     begin
-    l:= $100000000;
-    r:= t.QWordValue div 2 + $80000000; // r:= t / 2 + 0.5
-    if r > $FFFFFFFFFFFF then
-        r:= $FFFFFFFFFFFF
+    if t.QWordValue > $FFFFFFFFFFFF then // t.Round > 65535.9999
+        begin
+        l:= $10000000000; // 256
+        r:= $FFFFFFFFFFFF; // 65535.9999
+        end else
+        if t.QWordValue >= rThreshold then
+            begin
+            l:= lThreshold;
+            r:= $10000000000; // 256
+            end else
+            begin
+            l:= $100000000;
+            r:= lThreshold;
+            end;
     end;
 
 repeat
-    c.QWordValue:= (l + r) div 2;
+    c.QWordValue:= (l + r) shr 1;
     if hwSqr(c).QWordValue > t.QWordValue then
         r:= c.QWordValue
     else