Solved

function for y=-fabs(x)

Posted on 2004-10-17
9
608 Views
Last Modified: 2009-07-29
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
Comment
Question by:hengck23
9 Comments
 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 100 total points
Comment Utility
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
 
LVL 45

Expert Comment

by:Kdo
Comment Utility

err....  


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

sigh...
0
 
LVL 4

Assisted Solution

by:pankajtiwary
pankajtiwary earned 100 total points
Comment Utility
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
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 100 total points
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 12

Expert Comment

by:stefan73
Comment Utility
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
 

Author Comment

by:hengck23
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
This assumes sizeof(long)==sizeof(float),
and that your c libraries aren't very optimized,
to name just two non-portable assumptions.
0
 
LVL 7

Accepted Solution

by:
aib_42 earned 200 total points
Comment Utility
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
 

Author Comment

by:hengck23
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 (http://en.wikipedia.org/wiki/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 how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now