 Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

# Optimizing Math-Intensive Applications with Fixed-Point Arithmetic

### Shift and Add for Exponentials

Having found out about using shifts and adds with CORDIC to implement sines and cosines, I wondered if the same could be done for exponentials and logarithms, too. For exponentials it's rather easy, as:

ex+y=exey

So by choosing values of x such that ex= 2n or ex=1+2-n for some n, then exponentials can again be calculated just as a matter of shifts and adds. In each iteration, the current value of x is compared to the suggested value of y. If x is less than y, then that term is ignored, otherwise x=x-y and the result is multiplied by the appropriate value with a shift and add. By running from high values of y to smaller ones, the value of x approaches zero, and the result approaches the correct value for the exponential. For 64-bit fixed point (with 63 significant bits), this requires 63 table entries.

For negative values of x, the principle is the same, but the table values are for ex=2-n or ex =1/(1-2-n). Here we can use the fact that log(2-n)=-log(2n) to avoid duplicate table entries.

### Logarithms

Logarithms are slightly more complex to get right, though conceptually just as easy, as they are just a reverse lookup using the same tables as for calculating the exponentials. In fact, the implementation is even easier because logarithms are always positive. The key here is to find where to start the calculation, and you do that by shifting the parameter left until there is a 1 in the most significant bit. By counting the shifts, you know what initial value to use for the result. For example, in the 35.28 fixed-point system being used here, if you had to shift-left 30 times, the original value was 25*y for some y, so you start with a result of log(25) and work from there. Similarly, if you had to shift-left 42 times, then the original value was 2-7*y, so you start with a result of log(2-7).

Once you have the starting result for log(x), it's smooth sailing, as you always have a value with the MSB set to 1. For increasing values of n, check to see if the current value of x>=1+(x*2-n). If it is, then subtract (x*2-n) from the current value of x, and add log(1/(1-2-n)) to the result. Repeat until x==1 or until you're out of bits.

This uses the identity that:

log(x*y)=log(x)+log(y)

so so We've already got the values of log(1/(1-2-n)) in a lookup table, so you can reuse them here. It's important to go for increasing values of n, so that the large factors are taken out first.

### More Insights To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.

## Featured Reports ## Featured Whitepapers 