Knowledge learned from a program (floating point operation to fixed point operation) on the Internet

Source: Internet
Author: User

During this period of time, I learned how to convert a DSP floating point operation to a fixed point operation. On the Internet, I saw a program provided by a user, as shown below:

---------------------------------------------------------
Statement:
This article is original. You are welcome to reprint it. Please keep the following information for reprinting.
Author: Nie Fei (afreez) Beijing-Zhongguancun
Contact: afreez@sina.com (welcome to contact the author)
Initial Release Date: 2006-11-28
Without my consent, I shall not use words for commercial or profit purposes. Otherwise, the author has the right to pursue relevant responsibilities!

---------------------------------------------------------

/*************************************** ************************
// Convert the 32-bit floating point fval to a 32-bit integer and store it in ival
// The fractional part will be cropped
//
Void trunctoint32 (Int & ival, float fval)
{
Ival = * (int *) & fval;

// Extract the ending number
// Note that there is another missing 1 before the actual ending number.
Int mantissa = (ival & 0x07fffff) | 0x800000;

// Extract the index
// Divide by 23. If the index is greater than 23, the value is shifted to the left. Otherwise, the value is shifted to the right.
// Because the exponent is expressed by offset, 23 + 127 = 150
Int exponent = 150-(ival> 23) & 0xff );

If (exponent <0)
Ival = (mantissa <-exponent );
Else
Ival = (mantissa> exponent );

// If the value is less than 0, the result is reversed.
If (* (int *) & fval) & 0x80000000)
Ival =-ival;
}
**************************************** ***************/

The program process was clearly written. I was excited and immediately tested it on my machine. The results showed that it was always incorrect, roughly as follows:

// Main Function
...
Int ival;
Float fval = 4.23;
Trunctoint32 (& ival, fval );
// The output here is incorrect.
Printf ("ival = % d/N", ival );
...

// Function Definition

Void trunctoint32 (Int & ival, float fval)
{
// Skip
...
// If the value is less than 0, the result is reversed.
If (* (int *) & fval) & 0x80000000)
Ival =-ival;
// The output result here is correct.
Printf ("ival = % d/N", ival );
}

After taking a closer look, we found that there was a problem with parameter passing during function calling. I mainly understand pointers and pointer pointers. Let's consider them against my ideas.

Modify the main program as follows:
...
Int ival;
Float fval = 4.23;
Righttrunctoint32 (& ival, fval); // row N, which is assumed to be the corrected function.
Ival = (INT) fval; // row n + 1

Printf ("ival = % d/N", ival );
...

Perform a time efficiency test on N rows and n + 1 rows. It is shown that the computing time required for line n + 1 is less than that for line N.

The main program is further improved as follows:
...
Int ival;
Float fval = 4.23;

// ------- Begin more fast section? -----------
// Extract the ending number
Int ival = (* (int *) (& fval) & 0x07fffff) | 0x800000;

// Extract the index
Int exponent = 150-(* (int *) (& fval)> 23) & 0xff );

If (exponent <0)
Ival = (ival <-exponent );
Else
Ival = (ival> exponent );

// If the value is less than 0, the result is reversed.
If (* (int *) & fval) & 0x80000000)
Ival =-ival;
// ------- End more fast section? -----------

Ival = (INT) fval; // row n + 1
...
The results show that the test on the RedHat 9.0 platform shows that if the optimization option is not added to the compilation, the execution time efficiency of the more fast section is better than that of the n + 1 line, and the optimization option is added, the execution time of line n + 1 is more efficient than that of more fast section.

Analysis conclusion:
As the more fast section still has some code lines that can be optimized, I think this idea can be used for reference in some specific scenarios.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.