From 3e313410bdb630fbce046c84b7fcb4120a3a9f9f Mon Sep 17 00:00:00 2001 From: Marcel Keller Date: Tue, 5 Dec 2023 23:25:30 +1100 Subject: [PATCH] Fix square root with non-standard fixed-point parameters. --- Compiler/mpc_math.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Compiler/mpc_math.py b/Compiler/mpc_math.py index 0a9f01a50..2325053ee 100644 --- a/Compiler/mpc_math.py +++ b/Compiler/mpc_math.py @@ -624,6 +624,15 @@ def norm_simplified_SQ(b, k): # # @return g: approximated sqrt def sqrt_simplified_fx(x): + # adapt parameters to fit the algorithm + f = x.f + k = x.k + my_f = max(f, k - f + 1) + shift = my_f - f + my_k = k + shift + assert my_k < 2 * my_f + x = type(x)._new(x.v << shift, f=my_f, k=my_k) + # fix theta (number of iterations) theta = max(int(math.ceil(math.log(x.k))), 6) @@ -670,7 +679,7 @@ def sqrt_simplified_fx(x): g = H * x g = g - return g + return type(x)._new((g * 2 ** -shift).v, f=f, k=k) ##