Solved

function for y=-fabs(x)

Posted on 2004-10-17
9
612 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
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!
Kent
0
 
LVL 45

Expert Comment

by:Kdo
ID: 12333250

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
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.

Cheers!
0
 
LVL 22

Assisted Solution

by:grg99
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.

0
Give your grad a cloud of their own!

With up to 8TB of storage, give your favorite graduate their own personal cloud to centralize all their photos, videos and music in one safe place. They can save, sync and share all their stuff, and automatic photo backup helps free up space on their smartphone and tablet.

 
LVL 12

Expert Comment

by:stefan73
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.

Cheers!

Stefan
0
 

Author Comment

by:hengck23
ID: 12345928
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
ID: 12346430
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
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).
0
 

Author Comment

by:hengck23
ID: 12373454
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

911 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

19 Experts available now in Live!

Get 1:1 Help Now