function for y=-fabs(x)

Posted on 2004-10-17
Last Modified: 2009-07-29

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;
Question by:hengck23
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 100 total points
ID: 12332850
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!
LVL 45

Expert Comment

by:Kent Olsen
ID: 12333250


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


Assisted Solution

pankajtiwary earned 100 total points
ID: 12334427
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.

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!

LVL 22

Assisted Solution

grg99 earned 100 total points
ID: 12335289
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.

LVL 12

Expert Comment

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



Author Comment

ID: 12345928

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?
LVL 84

Expert Comment

ID: 12346430
This assumes sizeof(long)==sizeof(float),
and that your c libraries aren't very optimized,
to name just two non-portable assumptions.

Accepted Solution

aib_42 earned 200 total points
ID: 12368230
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).

Author Comment

ID: 12373454
Hi I have tried asm. the code is:

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

And this is the fastest! THank you

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question