Zuse's Z3 Square Root Algorithm Tal given at Fall meeting of the Ohio Section of the MAA October 999 - College of Wooster (Revised 0/0/009 Abstract Brian J. Shelburne Dept of Math and Comp Sci Wittenberg University In 94 Konrad Zuse completed the Z3 sequence controlled calculator. The Z3 was a binary machine (numbers were stored internally in floating point binary with a 64 by bit memory which could perform the four standard arithmetic operations as well as tae square roots. This tal examinations the algorithm Zuse used for his square root instruction and how it was implemented on the Z3. Zuse's Z3 Square Root Algorithm- The Tal Konrad Zuse (90-995 born 90 in Berlin, Germany studied civil engineering at Technische Hochschule Berlin-Charlottenburg wored as design engineer in aircraft industry needed calculations for design and analysis of structures his first attempt at automating calculation process was to design a form to trac intermediate results of calculations not familiar with design of mechanical calculators His Early Machines: The Z - Z3 Z: begun in 936 floating-point binary representation 4 bit word length - sign bit, 7 bit exponent, 6 bit mantissa (fraction 4 decimal digit precision completely mechanical (memory, arithmetic unit and control 6 word prototype memory completed 937 controlled by tape (used discarded 35 mm film - hand punched! four functions : + - mechanical arithmetic unit unreliable Z: 938-940 (smaller hybrid machine relay based ALU and control; mechanical memory "proof of concept" for relay based calculator demonstration led to funding for Z3 by Deutsche Versuchsanstalt fur Luftfahrt Z3: 939-94 (relay based machine same design as Z except included square root operation
An Overview of the Z3 Tape Reader Control Unit Numeric Keypad Light Display Arithmetic Unit Memory 64 Words Exponent ALU Fraction ALU Memory : 64 by bits - normalized floating point binary _ _ ± exponent fraction a 6 a 0 b -... b -4 Numeric Keyboard input: 4 columns of 0 eys for digits row of exponent eys labeled: -8, -7... 7, 8 Display Output - row of 5 lights for fraction plus exponent lights labeled: -8, -7...7, 8 Tape Control: nine 8 bit instructions 0 0000 - read eyboard 0 00000 - add 0 000 - display result 0 0000 - subtract 0 00000 - multiply z z z 3 z 4 z 5 z 6 - load address 0 00000 - divide 0 z z z 3 z 4 z 5 z 6 - store address 0 0000 - square root Arithmetic Unit: separate ALU's for exponents and fractions Control Unit: instructions were implemented as a sequence of micro-operations which controlled movement of data through machine.
How Do You Tae A Square Root?. Use a calculator. Use a slide rule 3. Use a table of logarithms 4 Use Newton's Iteration Formula to solve f ( x = x a by using the iteration f ( xn a formula xn+ = xn = ( xn where x 0 equals some initial "guess" ( x 0 = a wors f '( xn xn well for a >. Note this requires one division per iteration (expensive!. 5. Use the binomial expansion of x x = 3 5...( 3 x = x! = 3 3 3 5 4 x x x x.. 3 4! 3! 4! We can calculate the a for x = since b a by finding some value a b a b b b close to a and using the above series expansion a = + = ( b 6. Complete the Square: This technique is based on the simple idea that if x is a good approximation to a where a x = e > 0 is the error, then find a small value b such that ( x b + is a better approximation; that is ( ( another way, find a value b such that e b( x b a x a x b a x bx b > + = 0 or to put + 0. The inequality is not linear in b but is it not difficult to questimate a value. Generally this is done digit by digit. Completing the Square (details Without loss of generality assume a < 00 and suppose x = Q is an approximation to ( a accurate to 0 (i.e. the st digit below the decimal point. That is 0 Q = d + d 0 i= i i where di is a digit between 0 and 9 3
Letting b = d 0, find the largest digit d such that ( ( ( ( ( a x + b = a Q + d 0 = a Q Q d 0 d 0 0 To find the largest such digit d factor out d 0 to obtain ( a Q d ( Q d 0 + 0 0 Scaling (multiply by 0 does not change the sign of the expression but it maes all values integers. ( a Q d ( Q d 0 0 + 0 To find the largest digit d use the following guess and chec method.. Guess the largest digit d such that d 0 Q. Chec that d ( 0 Q d try again. is less than 0 e = 0 ( a Q + is still less than the scaled error. If not reduce by and i If successful then Q = d0 + di0 is a better approximation with a smaller scaled error now equal to i= ( a Q d ( Q + d = ( a Q < ( a Q 0 0 0 0 4
Finding the largest d which maintains this inequality is the theory behind the following well-nown algorithm (well-nown before the advent of cheap calculators used to compute a square root by hand. To demonstrate we tae the square root of 0.375. d 0 d d d 3 4. 5 3 0. 37 50 00 <- a 6 00 <- 0 Q 0 ----- 4 0+d -> 85 4 37 <- 0 (a - Q 0 4 5 <- d (Q 0 0 + d ------ 45 0+d -> 90 50 <- 0 4 (a - Q 9 0 <- d (Q 0 + d ----- 3 49 00 <- 0 6 (a - Q 45 0+d 3 -> 903 70 69 <- d 3 (Q 0 3 + d 3 -------- 78 3 00 First - find the largest integer d0 = Q0 whose square is less than or equal to the integer part of the number a. Square Q0 and subtract it from the integer part. Bring down the next two digits. Second. - Find the largest integer d such that Q0 0 + d times d is less than the previous remainder. Subtract it and bring down the next two digits. Q Q d = + 0 0 Third - Find the largest integer d such that Q 0 + d times d is less than the previous remainder. Subtract it and bring down the next two integers. Q Q d = + 0 th Step - Find the largest integer d such that Q 0 + d times d is less than the previous 0 remainder. Subtract it and bring down the next two integers. Q = Q + d Repeat. ( 5
The Binary Version of the "Completing the Square" Square Root Algorithm The same algorithm can be easily done in binary. By way of demonstration, we find the square root of the binary value 000.0 which is 0.375 decimal. The floating point representation is.0000 x 00 (note the even exponent. Therefore the square root of 000.0 is the square root of.0000 times 0 (or 4. Remember that multiplication by in binary is equivalent to a left shift. Furthermore scaling by powers of 0 (binary is scaling by powers of.. 0 0 0 0 0 0 0. 0 00 0 0 00 00 00 00 00 00-0 0 <- (a - Q 0 00 0 00 <- b ( Q 0 + b ------ 00<- 4 (a - Q 000 0 00<- b ( Q + b ---- 00 0 <- etc... 000 00 0 ------- 0 0 0000 0 00 ---- 0 00 00000 00 00 ----- 0 00 00 000000 00 00 00 ---------- 0 00 00 00 0000000 00 00 00 00 ----------- 0 00 00 00 00 00000000 00 00 00 00 00 -------------- 0 00 00 00 00 00 00000000 00 0 00 00 0 ----------------- 0 00 00000000 0 0 00 00 00 whew!!! So therefore the square root of 000.0 is approximately.0000000 x 0 = 00.00000 = 4.57875 which is "close" to 4.538675 6
The Z3 Square Root Implementation - Completing the Square in Binary The Z3 used a variant of the above algorithm to compute a square root. Given a < 00 ( values are in binary if Q is an approximation to 0 a accurate to Q b b = + i= i i (, that is where each bi is either 0 or, then find the largest binary digit b (0 or such that ( ( ( ( a Q + b = a Q Q b b 0 Because there are only two possible choices for b, either 0 or, this algorithm is easy to implement. Moreover since b = b these are some trics to mae iteration efficient. Begin by scaling (multiplying the above inequality by (instead of. If we have then b is ; otherwise b = 0. In any case since expression ( a Q ( Q ( a Q ( Q + 0 b = b it follows that the above + equals ( ( a Q b ( a Q be used in the calculation for the next step. If we multiply ( a Q + ( a Q machine using a left shift. + = which can by we obtain for use in the next iteration step. Multiplication by is easily done on a binary The algorithm is given below. Assume a 00 where e ( a =. 0 < and let e ( a Q. let Q[0] =. let e[0] = a - 3. for =,, 3,... 3. Compute e[-] - ( Q[-] + - 3. If positive or zero (i.e. b[] = 3.. Q[] = Q[-] + - 3.. e[] = e[-] - ( Q[-] + - 3.3 Otherwise (i.e. b[] = 0 3.3. Q[] = Q[-] 3.3. e[] = e[-] = (a scaled error value 7
Example - Compute the square root of.0000 e[-] - ( Q[-] + - e[]= a Q[] Q[] 0 0.0000. 0.000-0. negative 0.000.0.000-0.0 negative.000.00 3 0.00-0.00 positive 0.000.00 4. 0.00-0.00 negative 0.00.000 5. 0.0-0.000 negative 0.0.0000 6 0. - 0.00000 negative 0..00000 7.0-0.00000 negative.0.000000 8 0.0-0.000000 negative 0.0.0000000 9 00.0-0.0000000 positive.0.0000000 0.0-0.000000 positive.000.0000000 This agrees with the example given above. 8
A Closer Loo at the Architecture of the Z3 Arithmetic Unit Bf Register Shift -.. Shift -6..5 Ba Register Bb Register Adder/Subtractor Be Register Z3 had separate ALU's to handle the exponent and fractional parts of a number Basic data path of the fraction ALU consisted of four registers (Bf, Ba, Bb, and Be an addition/subtraction unit and two shifter units connected by data lines. A number of gates controlled the follow of data around the data path Hardware operations included addition and subtraction, pre-shifting input to the Ba and Bb registers, detecting a positive or zero result and setting individual bits in a register. A micro-operation consisted of a timed sequence of gates openings that allowed the contents of registers to flow through the pre-shifters and addition/subtraction unit. Detecting a positive or zero result could conditionally alter the flow of data. Instructions were implemented as sequences of micro-operations. 9
Note Bf[-] refers to bit - in register Bf Micro-operation Implementation of Z3 Square Root 0. Bf := a // Load Bf with a Ba := Bf (or Bf if exponent is odd // Ba = a Bb[0] := // Q 0 =. if Ba - Bb 0 then // test Be := Ba - Bb Bf[0] := // set bit else Be := Ba // set up next comparison Ba = Be // r 0 Bb = Bf; Bb[-] := // Q 0 + - if Ba - Bb 0 then // test Be := Ba - Bb Bf[-] := // set bit else Be := Ba // set up next comparison Ba := Be // r Bb := Bf; Bb[-] := // Q + - and in general if Ba - Bb 0 then // if r - - ( Q - + - 0 then else Be := Ba - Bb // r = r - - ( Q - + - Bf[-] := // Q = Q - + - Be := Ba // r = r - // Q = Q - // set up next comparison Ba := Be // r Bb := Bf; Bb[-(+] := // Q + -(+ At the end the exponent which was even was divided by (a right shift yielding the square root. The thing to notice was the economy of operations needed to compute a square root - shifts, subtractions, comparisons and setting individual bits within a word. 0
Summary. The square root algorithm used by the Z3 was the binary analog of the "completing the square" algorithm, a widely nown (? manual technique for obtaining a square root one digit at a time.. For a < 00 and Q an approximation of if ( a Q + b 0 a accurate to (, set bit b equal to otherwise set bit b equal to 0. It was shown that this is the same as checing if ( a Q ( Q + 0 and leads to the iteration algorithm Repeat until tired Compute e[-] - ( Q[-] + - If positive or zero Q[] = Q[-] + - e[] = e[-] - ( Q[-] + - Otherwise Q[] = Q[-] e[] = e[-] where e[] = (a Q[] and initial condition Q 0 = and e[0] = (a - 3. A close examination of this algorithm reveals that it requires only the operations of subtraction, left shift (multiplication by, set bit, and test if positive or zero. - all of which are easy to implement in binary logic. 4. The Z3 had a data path that could easily implement these operations and a micro-operation sequencer that could iterate each step of the algorithm the requisite number of times. Use of floating point binary notation helped. 5. Binary representation is used in computers because for economic reasons computer hardware is built out of bi-stable components. However, many complicated arithmetic operations lie multiplication, division and square root are easy to implement in binary. Even though we humans find binary notation complicated to use (even moderately sized numbers tae a lot of bits, because of the ease in implementing complicated algorithms in binary, there is something to be said for the advantages of binary notation in and of itself. Any sufficiently advanced civilization uses a number system based on a power of two.
Epilog: A Follow Up on the Z3 - the Z4 The Z3 was really a "proof of concept" for the more ambitious Z4, a 3 bit machine begun after the Z3 was completed. Unlie the Z3, the Z4 used a mechanical memory of 000 words (which too up a cubic meter of space!. Before it was completed it was moved to Gottingen to avoid the Berlin air raids which had destroyed the Z3. Just before Gottingen was "liberated" at the end of WWII, the Z4 was moved to Hinterstein, Bavaria. In 950 the refurbished Z4 with some additional features that gave it a conditional jump was moved to ETH Zurich. In 955 it was moved to Basel where it gave "acceptable service" until 960. After the war Zuse founded Zuse Kommandit Gesellschaft (Zuse KG which produced for the Leitz Optical Company a relay based, punch-tape controlled "computer" called the Z5 which was based on the Z4 design. This led to the production of a number of relay-based calculators, called the Z. In 956 Zuse began wor on a vacuum-tube based computer called the Z with a transistor version following. A series of mergers and taeovers finally lead to Zuse KG becoming part of Siemens. References R. Rojas, "Konrad Zuses' Legacy: The Architecture of the Z and Z3", Annals of the History of Computing, vol. 9, no., pp. 5-6, 997 I. Koran, Computer Arithmetic Algorithms, Englewood Cliff, N.J.: Prentice-Hall, 993 M. Williams, A History of Computing Technology, Los Alamitos, CA: IEEE Computer Society Press, 997