1
$\begingroup$

Reading articles about calculating square root of an integer I stumbled across the pseudocode attached below.

Original paper:
T-count and Qubit Optimized Quantum Circuit Design of the Non-Restoring Square Root Algorithm by Edgard Muñoz-Coreas and Himanshu Thapliyal.
ACM Journal on Emerging Technologies in Computing Systems (JETC), Volume 14, Issue 3. Article No.: 36, Pages 1 - 15.
https://arxiv.org/pdf/1712.08254 Pseudocode for non-restoring square root algorithm

It seemed interesting, so I started testing it for different values a, but in some cases my calculations were wrong. For example if a = 73

$a = 73_{10} = 0100 1001_2$

$n = 8$


$\text{Step 1: initialization of R and F (line 2 -> 4)}$

$R = 0000 0001$

$F = 0000 0001$

$R = R - F = 0000 0000$


$\text{Step 2: first iteration of the loop (line 15 -> 20)}$

$i = n/2 - 1 = 3$

$R \ge 0 \Rightarrow Y_{3} = 1$

$R = 00000000$ $\text{(shifting R by two bits and appending } a_5 \text{ and } a_4)$

$F = 0000 0101$ $\text{(000... + } Y_3 \text{ + 01)}$

$R = R - F = 0000 0000 - 0000 0101 = 1111 1011$


$\text{Step 3: second iteration of the loop (line 8 -> 13)}$

$ i = 2 $

$ R \le 0 \Rightarrow Y_2 = 0$

$ R = 11101110 $ $\text{(shifting R by two bits and appending } a_3 \text{ and } a_2)$

$ F = 0000 1011 $ $\text{(000... + } Y_3 + Y_2 \text{ + 11)}$

$ R = R + F = 1110 1110 + 0000 1011 = 1111 1001$


$\text{Step 4: third iteration of the loop (line 8 -> 13)}$

$ i = 1 $

$ R \le 0 \Rightarrow Y_1 = 0 $

$ R = 1110 0101 $ $\text{(shifting R by two bits and appending } a_1 \text{ and } a_0)$

$ F = 0001 0011 $ $\text{(000... + } Y_3 + Y_2 + Y_1 \text{ + 11)}$

$ R = R + F = 1110 0101 + 0001 0011 = 1111 1000 $


$\text{Step 5: final remainder restoration (line 25 -> 28)} $

$ R \le 0 \Rightarrow Y_0 = 0 $

$ F = 0100 0001 $ $(0 + Y_3 + Y_2 + Y_1 + Y_0 + 01)$

$ R = R + F = 1111 1000 + 0100 0001 = 0011 1001 $


The problem is in the final step, Y contains 1000 which is 8 which is correct, but R should contain 9 because 73 = 8 * 8 + 9, but the value is 57.

I noticed that if in the final step we shift F by one bit and append 1 instead of 01, the result will be correct, but the document says otherwise.

Another thing that I don't understand is shifting R in a loop. Pseudocode says we should add zeros at the beginning and then shift R but when I do that the result is messing up when R < 0, so instead I shift the whole R without adding leading zeros.

I hope somebody will be able to help me understand what I'm doing wrong and why the results of the algorithm are wrong.

Updated: For any given a algorithm works if

  1. shift R without adding leading zeros
  2. in the last step when R < 0 shifting F by 1 bit (instead of 2) and appending 1 (instead of 01)
$\endgroup$
1
  • $\begingroup$ I get a smaller value for $F$ in Step 5: Please check. $\endgroup$ Commented Apr 16 at 5:36

1 Answer 1

4
+100
$\begingroup$

There is some mess in this pseudocode, but if we look to the article Modular array structure for non-restoring square root circuit by S. Samavi, A. Sadrabadi, A. Fanian which was used as the main idea for design, we can find the following pseudocode:

algorithm Non-restoring square root is input: 2n-bit operand X output: n-bit quotient Q, n-bit remainder R Step 1: R_0 = 0.x_1x_2; F_0 = 0.01; i = 1; Step 2: R_1 = R_0 - F_0; Step 3: If (R_i < 0) then (q_i ← 0; R_i ← R_i.x_{2i+1}x_{2i+2}; F_i ← 0.0...0q_1q_2...q_i11; R_{i+1} ← R_i + F_i;) Else (q_i ← 1; R_i ← R_i.x_{2i+1}x_{2i+2}; F_i ← 0.0...0q_1q_2...q_i01; R_{i+1} ← R_i - F_i;) Step 4: i ← i + 1; Step 5: If (i ≤ n) then (go to step 3) Else (Q = 0.q_1q_2q_3...q_n); End; 

The essential difference is that negative number is converted to the complement representation for output only. Intermediate negative value of $R_i$ is thought as minus sign and a positive value. That's why appending digits $x_{2i + 1}x_{2i + 2}$ to negative $R_i$ is equivalent to subtraction of $\overline{x_{2i + 1}x_{2i + 2}} \cdot 2^{-2i - 2}$ from $R_i$. In the way you understand the article you cite appending these digits is equivalent to addition of the same value, that doesn't seem to be correct.

Also it's a typo that the line 26 has $01$ at the end instead of $11$. However it is correct that for $n = 4$ there should $2$ leading zeroes. Try numbers with multiple $1$ digits in the square root to see that your "fix" doesn't work in general.

So the correct iterations of the algorithm should look like this:

$a = 73_{10} = 01001001_2$

$n = 8$


Step 1: initialization of $R$ and $F$ (line $2 \to 4$)

$R = 00000001$

$F = 00000001$

$R = R - F = 00000000$


Step 2: first iteration of the loop (line $15 \to 20$)

$i = n / 2 − 1 = 3$

$R \ge 0 \Rightarrow Y_{3} = 1$

$R = 00000000$ (shifting $R$ by two bits and appending $a_5$ and $a_4$)

$F = 0000 0101$

$R = R - F = 0000 0000 - 0000 0101 = -000 0101$


Step 3: second iteration of the loop (line $8 \to 13$)

$ i = 2 $

$ R < 0 \Rightarrow Y_2 = 0$

$ R = -0001 0110 $ (shifting $R$ by two bits and appending $a_3$ and $a_2$)

$ F = 0000 1011 $

$ R = R + F = -0001 0110 + 0000 1011 = -0000 1011$


Step 4: third iteration of the loop (line $8 \to 13$)

$ i = 1 $

$ R < 0 \Rightarrow Y_1 = 0 $

$ R = -0010 1101 $ (shifting $R$ by two bits and appending $a_1$ and $a_0$

$ F = 0001 0011 $

$ R = R + F = -0010 1101 + 0001 0011 = -0001 1010 $


Step 5: final remainder restoration (line $25 \to 28$)

$ R < 0 \Rightarrow Y_0 = 0 $

$ F = 0010 0011 $

$ R = R + F = -0001 1010 + 0010 0011 = 0000 1001 $


So $Y = 1000_2$ and $R = 1001_2$, that is exactly what you expect.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.