Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# function for y=-fabs(x)

Posted on 2004-10-17
Medium Priority
633 Views
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
Question by:hengck23
[X]
###### 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

LVL 46

Assisted Solution

Kent Olsen earned 300 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 46

Expert Comment

ID: 12333250

err....

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

sigh...
0

LVL 4

Assisted Solution

pankajtiwary earned 300 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

grg99 earned 300 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

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.

Cheers!

Stefan
0

Author Comment

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

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

aib_42 earned 600 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

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

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and infâ€¦
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.
###### Suggested Courses
Course of the Month7 days, 22 hours left to enroll