dsp TIPS&TRICKS [ ] Clay S. Turner

A Fast Binary

algorithm’s mathematical development. We see the right-hand side of (6) is a “DSP Tips and Tricks” introduces To start, we simply desire to find product of two factors, where the left fac- practical design and implementation tor is equal to either one or two depend- signal processing that y 5 log 1x2. (1) 2 ing on the value of y . Thus we have the you may wish to incorporate into 1 your designs. We welcome readers Thus we can invert this and find following two cases: 211 1 211 1 211 1...222 to submit their contributions. 5 y 5 2 5 2 y2 2 y3 2 y4 x 2 . (2) y1 0: x 2 Contact Associate Editors Rick Lyons (7) ([email protected]) or C. Britton Since the iterative part of our log algo- Rorabaugh ([email protected]). rithm assumes x is in 1 # x , 2, we or

may need to “normalize” x. This nor- # 211 1 211 1 211 1...222 y 51: x 2 52 22 y2 2 y3 2 y4 . malization is performed by a simple 1 (8) his article presents a com- succession of divides/multiplies by two. putationally fast algorithm These divides/multiplies by two may be Since both (7) and (8) contain the com- 211 1 211 1 211 1 222 for computing . efficiently effected by binary shifts. The mon factor 22 y2 2 y3 2 y4 ... , and The algorithm is particular- count of the divides/multiplies gives this factor will have a value that is great- ly well suited for implemen- the characteristic of our logarithm er than or equal to one and less than Ttation using fixed-point processors. result. This will be added to the man- two, we see the square of x will the great- tissa (calculated below) to form the er than or equal to two if and only if 5 5 BACKGROUND complete logarithm. The number of y1 1, otherwise y1 0. Long before the technological age, in divides is taken as a positive number Step 2: Compare the result of squar- 1427, the Persian mathematician and and the number of multiplies is taken ing and if x2 is greater than or equal to astronomer al Kashi presented an algo- as a . two, then set mantissa to “1” and rithm for efficient integral exponentia- Now with x properly scaled, then we divide x2 by two. Otherwise set mantissa tion via repeated squaring and know from (1) that 0 # y , 1. So let’s bit to “0” and leave x2 unchanged. multiplying [1]. Al Kashi’s method now expand y into a binary , thus So now at the end of step 2, we have sees much use in modern cryptographic 2 2 2 2 2 11 y 12 11 y 12 11 y 1...222 5 # 21 1 # 22 1 # 23 x 5 2 2 3 4 . (9) systems where its efficiency with respect y y1 2 y2 2 y3 2 2 to very large numbers is paramount. 1 # 24 1 ... Realizing that x is just a number, we see y4 2 . (3) Related but not as well known as al that (9) has the exact same form as (5). Kashi’s method is an algorithm for find- It will be efficacious to put this into nest- Thus we can repeat Steps 1 and 2 to ing binary logarithms via repeated ed parenthetical form extract the remaining in y. squaring and dividing. Performing these After the prerequisite number of bits operations in a radix two-number for- 5 21 1 1 21 1 1 21 in the mantissa is obtained, then simply y 2 y1 2 y2 2 mat reduces the divisions to binary add the characteristic to the mantissa. If 3 1 1 21 1 1 ...2222 shifts, thus making the algorithm ame- y3 2 y4 . (4) we require a logarithm having a radix nable to fixed-point implementations on other than two, then we may employ the low-complexity microprocessors and So putting (4) into (2) we have following property of logarithms field-programmable gate arrays. 1 2 2 2 2 2 log2 x 2 11 y 12 11 y 12 11 y 12 11 y 1...2222 1 2 5 x 5 2 1 2 3 4 . (5) loga x 1 2 , (10) THE LOGARITHM ALGORITHM log2 a 1 2 Now that we have revealed the two main Now we are ready to see how to sequen- where the division by log2 a is imple- types of computations for finding the tially extract the bits comprising y. mented as a multiplication by the fixed Step 1: 1 2 binary logarithm, let’s go through our Square x as value 1/log2 a .

2 y # 2211 y 12211 y 12211 y 1...222 Digital Object Identifier 10.1109/MSP.2010.937503 x 5 2 1 2 2 3 4 . (6) (continued on page 140)

IEEE SIGNAL PROCESSING MAGAZINE [124] SEPTEMBER 2010 1053-5888/10/$26.00©2010IEEE [dates AHEAD]

Please send calendar submissions to: 2010 IEEE Workshop on Signal General Chair: Masayuki Tanimoto Dates Ahead, c/o Jessica Barragué, Processing Systems (SiPS 2010) URL: http://www.pcs2010.org/ IEEE Signal Processing Magazine 6–8 October, San Francisco, California. 445 Hoes Lane General Cochairs: Shuvra Battacharyya 2010 IEEE Spoken Language Piscataway, NJ 08855 USA, and Jorn Janneck Technology Workshop (SLT’10) e-mail: [email protected] URL: http://www.sips2010.org/ (Colored conference title indicates 12–15 December, Berkeley, California. SP-sponsored conference.) General Chairs: Dilek Hakkani-Tür and 2010 IEEE International Symposium Mari Ostendorf on Phased Array Systems and URL: http://www.slt2010.org/ 2010 Technology (ARRAY’10) 12–15 October, Waltham, Massachusetts. The IEEE International Workshop [SEPTEMBER] Conference Chair: Mark Russell on Information Forensics and URL: http://www.array2010.org/ 2010 International Conference Security (WIFS) on Image Processing (ICIP 2010) 12–15 December, Seattle, Washington. 26–29 September, Hong Kong. 2010 The 10th IEEE International General Cochairs: Darko Kirovski and General Chair: Wan-Chi Siu Conference on Signal Processing Paul Van Oorschot URL: http://www.icip2010.org (ICSP’10) URL: http://www.wifs10.org 24–28 October, Beijing, China. OCTOBER [ ] Conference Chair: Yuan Baozong The 6th IEEE Sensor Array and URL: http://icsp10.bjtu.edu.cn 2011 Multichannel Signal Processing Workshop (SAM ‘10) [JANUARY] [NOVEMBER] 4–7 October, Israel. 2011 IEEE Digital Signal Processing General Cochairs: Hagit Messer 2010 2nd International Conference and Signal Processing Education and Jeffrey L. Krolik on Audio, Language, and Image Workshop (DSP/SPE’10) URL: http://www.sam-2010.org/ Processing 4–7 January, Sedona, Arizona. 23–25 November 2010, Shanghai, China. General Chairs: Lina Karam General Chairs: Fa-Long Luo, Wanggen Wan, The 1st IEEE International Conference and Ronald Schafer and Thomas Sikora on Smart Grid Communications URL: http://www.dspe2011.org 4–6 October, Gaithersburg, Maryland. URL: http://www.icalip2010.cn/ General Cochairs: George Arnold and [MARCH] Stefano Galli [DECEMBER] URL: http://www.ieee-smartgridcomm.org Data Compression Conference 28th Picture Coding 29–31 March, Snowbird, Utah. Symposium (PCS’10) General Chair: James A. Storer Digital Object Identifier 10.1109/MSP.2010.937314 7–10 December, Nagoya, Japan. URL: http://www.cs.brandeis.edu/~dcc/

[dsp TIPS&TRICKS] continued from page 124

We list the binary logarithm algo- 8) Scale for next bit: b 5 b/2. AUTHOR rithm’s steps as follows: 9) Go to Step 6 and repeat until Clay S. Turner (Clay.Turner@PaceOMatic. 1) Initialize result to 0: y 5 0. desired number of mantissa bits are com) is chief scientist for Pace-O-Matic, 2) Initialize mantissa-bit decimal found. Inc. He has over 30 years of experience in value to 0.5: b 5 1/2. 10) Final log1x2 value: y. digital signal processing and mathemati- 3) While x , 1, x 5 2x, y 5 y 2 1. A “C” program, and MATLAB code, cal and embedded programming. 4) While x $ 2, x 5 x/2, y 5 y 1 1. demonstrating the algorithm are avail- 5) Go to Step 3 and repeat until able for download at http://www. REFERENCE 1 # x , 2. signalprocessingsociety.org/publications/ [1] D. E. Knuth, The Art of Computer Programming: # Seminumerical Algorithms, 2nd ed. Reading, MA: 6) Square: x 5 x x. periodicals/spm/columns-resources- Addison-Wesley, vol. 2, 1981, pp. 441–466. 7) If x $ 2, x 5 x/2, y 5 y 1 b. archive/2010-columns-resources/#tips. [SP]

IEEE SIGNAL PROCESSING MAGAZINE [140] SEPTEMBER 2010