Antilog Approximation on ADSP-21065L

It took me some time to look for a site that could help me implement a program that does antilog approximation Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details, where Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details is any integer and Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details is any real number. I was searching for “antilog approximation” but I didn’t get anything helpful (at least at the time I was searching), except for one Google forum site.

Based on that site, Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details can be approximated by the following equation:

Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details

Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details

where Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details < Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details

(exact when x=0 and x=1)

It should be noted though that the exponent can only be any real number Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details and the base is always 2. Now, there are two questions that arise:

1. What if the base is not 2?

2. What if the exponent is outside the range?

This blog entry answers the two questions and presents a way of implementing an antilog approximation Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details, where Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details is any integer and Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details is any real number, in the ADSP-21065L platform.

Base is not 2

Any expression Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details can be expressed in terms of Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details as Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details à this means the base is now 2 but the exponent becomes Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details, – very easy eh? ;) This brings me to the second question, what if the exponent is outside the range [0,1].

Exponent x outside the range Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details

Remember that any real number x can be divided into two parts: its integral component, Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details, and fractional component, Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details. Thus, Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details. The fractional component fits in the range [0,1], therefore Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more detailscan be approximated using the equation shown above. But how about the integral part, Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details? It can be computed using continuous multiplication. However, 3 cases exist: (1) Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more detailsis positive; (2) Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details is 0; and (3)Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details is negative. Of course if Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details, Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details. If Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details is positive, continuous multiplication (multiply 2 by itself Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details) will do the trick. If Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more detailsis negative, multiply 2 by itself Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details times and then compute for the reciprocal of the product. This method is ok if Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details is a small number. But what if the exponent is in tens or maybe a hundred? Then this method is already inefficient. Add the division that is needed if Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details is negative. (ADSP doesnt have a built-in division instruction, and so a subroutine has been developed for that). Good thing is, there’s a more efficient way of computing for Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details and that is discussed next.

Implementation on ADSP-21065L

The antilog approximation is implemented by first multiplying the exponent Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details with Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details whatever the base Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details is. The value of Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details is stored in memory. The next step involves extracting the integral and fractional components, Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details and Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details, of the new exponent. Let’s say the new exponent is saved in f0. To extract the integral component, a flooring function is needed. However, no built-in floor function is available, instead fix or trunc commands can be used. The difference between fix and trunc is that fix rounds the number while trunc simply truncates the number. Example:

f0 = 4.7;

r1 = fix f0; à (r1 will have a value of 5)

r1 = trunc f0; à (r1 will have a value of 4)

f0 = -3.3;

r1 = fix f0; à (r1 will have a value of -3)

r1 = trunc f0; à (r1 will have a value of -3)

Whatever the value of r1 is, it should be compared to f0, because the floor of f0 should always be less than f0. If it’s not less than f0, subtract 1 then that is the integral part (save this to r1 still). And then the fractional part can be implemented by subtracting the integral part from the original exponent. However, take note that you should be subtracting (adding or multiplying) both fixed-point operands of both floating-point operands – cant be one fixed point and one floating point. The next lines of code show how the integral and fractional exponents can be extracted (the original exponent is in f0, integral component in f2, fractional component in f3):

r1 = trunc f0; // extracting the integral part (x1)

f2 = float r1;

f15 = 1.0;

comp(f2,f0);

if gt f2 = f2 – f15; // if f2 > f0, subtract 1 from f2 (f2 should always be less than f0, since f2 is the whole part)

f3 = f1 – f2; // extracting the fractional part (x2)

Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details is approximated using the equation above. (the one from the Google site). It is important to save the coefficients given in that equation in memory. Then use a loop that accesses these coefficients and updates the value of the exponent Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details. (You can email me if you have inquiries on how this could be implemented – would be happy to help!)

But the most interesting bit for me was the implementation of Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details. Like I mentioned above, there are three cases for Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details if Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details will be implemented using successive multiplication… And then it hit me, that I don’t have to do successive multiplications. It is only ok to do that if the exponent is less than 10. More than that, the algorithm becomes inefficient. So, what’s the solution, then?

Take note that a 32-bit floating point representation has the MSB as the sign bit, bits (31-24) as exponent (8 bits), and the last 24 bits as mantissa. It represents any floating-point number as

Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details

And since the number that I need to represent is a power of 2, I can just set all the mantissa bits to 0, and whatever the value of Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details is, subtract 127 from that and then store this number to bits 31-24 of the register. And then that register can be read as a floating point number and the value is already Error: http://www.daejavu.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/ must have write access Read the official wpmathpub plugin FAQ for more details. Very simple, eh? The next lines of code show how this (very interesting method, at least for me hehe :D ) was implemented:

r2 = fix f2; // r2 is the integral part in the exponent
r4 = 127; // the exponent in a 32-bit floating point representation needs to be subtracted by 127 first to get the actual value of the exponent
r4 = r2+r4;
r4 = lshift r4 by 23;
// f4 is the floating point version ;)

The beauty of this implementation is that it only includes 4 lines of assembly code and it covers all cases! ;)

Leave a Reply

Currently you have JavaScript disabled. In order to post comments, please make sure JavaScript and Cookies are enabled, and reload the page. Click here for instructions on how to enable JavaScript in your browser.