Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 645
  • Last Modified:

function for y=-fabs(x)

Hi,

I want to write a fst function for y=-fabs(x).
Is the following correct? Which is faster?

inline float fastFunc(float x){
   *(long*)&x |= 0x80000000;
    return x;
}

inline float fastFunc(float x){
   *(long*)&x &= 0x7fffffff;
    return -x;
}
0
hengck23
Asked:
hengck23
4 Solutions
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi hengck23,

Perhaps faster still is this:

inline float fastFunc (float x){
  return ( ((*(long*)&x) >> 31) ? x : -x);
}

This function simply loads the operand (x), shifts it right 31 places, and if the remaining bit is a 1 (which meant that the original value was negative) it returns that value.  If the remaining bit is 1, it negates the value before returning it.

This function should be smaller and faster than either of the first two.

Good Luck!
Kent
0
 
Kent OlsenData Warehouse Architect / DBACommented:

err....  


If the remaining bit is 0, it negates the value before returning it.

sigh...
0
 
pankajtiwaryCommented:
Hi hengck23,

I still say, you should not write these kind of functions which directly manipulates the bit patterns. C or C++ does not say anything about the bit patterns of floating point numbers so your program will not be portable at all.

Cheers!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
grg99Commented:
It's probably not worth optimizing this.  I know of several compilers that recogise abs() as a simple function to generate in-line, with some very optimized code like the above shift and xor example.

Even with compilers that generate calls to an actual library fabs() routine, some library routines have been known to wipe out their calling sequence with the optimum shift and xor instructions.

0
 
stefan73Commented:
Hi grg99,
I agree. The C libraries are highly optimized, there's nothing to be gained from twiddling with the bits by yourself.

Cheers!

Stefan
0
 
hengck23Author Commented:
Hi,

I am not sure why but the timing i obtained shows that "*(long*)&x &= 0x7fffffff;" is the fastest, even faster than the c libraries. Does anyone have any clues?
0
 
ozoCommented:
This assumes sizeof(long)==sizeof(float),
and that your c libraries aren't very optimized,
to name just two non-portable assumptions.
0
 
aib_42Commented:
This is bad, bad, bad coding. First of all, you have a non-C 'inline' keyword, which, I'm guessing, makes the function expand inline. A simple preprocessor macro would suffice here IMHO. Then, you are converting a pointer to double to pointer to long and fiddling with the bits, which is two more Bad Things to do.

As for your C library being slower than the function you have written, there may be several reasons why. The most probable one is that your function has the keyword (I'm guessing that it's a keyword) 'inline'. The library will most probably not have it, and thus will generate a call/return overhead each time the function is called.

If you want the fastest possible solution, try using inline assembly and take a look at your CPU documentation to find the fastest possible group of instructions that will take the absolute value of a C 'float', whatever that is on your specific platform.

For the x86, the 'float' is most probably a 32-bit floating point variable, and the fastest way to take its absolute value is using the 'FABS' instruction (486DX+, I'd presume).
0
 
hengck23Author Commented:
Hi I have tried asm. the code is:

inline float pslNegFabs32f(float x){
      __asm{
        fld  x //Push 'x' into st(0) of FPU stack
        fabs
        fchs   //change sign
        fstp x //Pop from st(0) of FPU stack
       }
      return x;
}

And this is the fastest! THank you
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now