From 2c384db27cc46862fb7a386ba652980bd4accd35 Mon Sep 17 00:00:00 2001
From: Kevin B Kenny <kennykb@acm.org>
Date: Thu, 30 Nov 2006 23:33:49 +0000
Subject: fixed bug in floating point initial approximation that caused
 convergence failure

---
 libtommath/bn_mp_sqrt.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/libtommath/bn_mp_sqrt.c b/libtommath/bn_mp_sqrt.c
index 72a9ff5..f6e65dc 100644
--- a/libtommath/bn_mp_sqrt.c
+++ b/libtommath/bn_mp_sqrt.c
@@ -68,12 +68,18 @@ int mp_sqrt(mp_int *arg, mp_int *ret)
   dig = (mp_digit) ldexp(d, -DIGIT_BIT);
   if (dig) {
       t1.used = i+2;
-      t1.dp[i+1] = dig;
       d -= ldexp((double) dig, DIGIT_BIT);
+      if (d != 0,0) {
+	  t1.dp[i+1] = dig;
+	  t1.dp[i] = ((mp_digit) d) - 1;
+      } else {
+	  t1.dp[i+1] = dig-1;
+	  t1.dp[i] = MP_DIGIT_MAX;
+      }
   } else {
       t1.used = i+1;
+      t1.dp[i] = ((mp_digit) d) - 1;
   }
-  t1.dp[i] = (mp_digit) d;
 
 #else
 
@@ -119,5 +125,5 @@ E2: mp_clear(&t1);
 #endif
 
 /* $Source: /root/tcl/repos-to-convert/tcl/libtommath/bn_mp_sqrt.c,v $ */
-/* $Revision: 1.2 $ */
-/* $Date: 2005/12/27 17:39:02 $ */
+/* $Revision: 1.3 $ */
+/* $Date: 2006/11/30 23:33:49 $ */
-- 
cgit v0.12

