DSPRelated.com
Forums

floating point number and efficient sign operation

Started by ramestica December 16, 2009
I'm just wondering what's the most efficient way for changing the sign of
floating point number. I'm wondering whether a boolean operation on the
binary representation of the number would be faster than multiplying the
number by minus one. My compiler is gcc, it could be that this one is
clever enough to already do the 'fast' thing when a multiplication by -1 is
encountered. I'm not sure.

thanks,
 Rodrigo


ramestica wrote:
> I'm just wondering what's the most efficient way for changing the sign of > floating point number. I'm wondering whether a boolean operation on the > binary representation of the number would be faster than multiplying the > number by minus one. My compiler is gcc, it could be that this one is > clever enough to already do the 'fast' thing when a multiplication by -1 is > encountered. I'm not sure.
Get out the book and look up the format definition for the floating point you're using. (I assume IEEE 745.) Then find the pattern. (For IEEE 745, just flip the sign bit -- bit 31 or 63 depending on the precision.) See http://babbage.cs.qc.edu/IEEE-754/ Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������

ramestica wrote:

> I'm just wondering what's the most efficient way for changing the sign of > floating point number. I'm wondering whether a boolean operation on the > binary representation of the number would be faster than multiplying the > number by minus one.
Why can't you try for yourself? VLV
Vladimir Vassilevsky wrote:
> > > ramestica wrote: > >> I'm just wondering what's the most efficient way for changing the sign of >> floating point number. I'm wondering whether a boolean operation on the >> binary representation of the number would be faster than multiplying the >> number by minus one. > > Why can't you try for yourself?
I assumed he wanted to know if there is a Boolean method that works, as there is with two's-complement numbers. Although some processors do a floating-point multiplication as fast as a Boolean op, none that I know of do it faster. Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
ramestica <ramestica@lavabit.com> wrote:

> I'm just wondering what's the most efficient way for changing the sign of > floating point number. I'm wondering whether a boolean operation on the > binary representation of the number would be faster than multiplying the > number by minus one. My compiler is gcc, it could be that this one is > clever enough to already do the 'fast' thing when a multiplication by -1 is > encountered. I'm not sure.
If the value is already in a register, then you can change the sign directly. Usually there is a "change sign of floating point value" instruction. If it is in memory, it is probably faster to change with bit manipulation instructions than to load, change, and store. I have never known a compiler to do that, though. -- glen
>Vladimir Vassilevsky wrote: >> >> >> ramestica wrote: >> >>> I'm just wondering what's the most efficient way for changing the sign
of
>>> floating point number. I'm wondering whether a boolean operation on
the
>>> binary representation of the number would be faster than multiplying
the
>>> number by minus one. >> >> Why can't you try for yourself? > >I assumed he wanted to know if there is a Boolean method that works, as >there is with two's-complement numbers. Although some processors do a >floating-point multiplication as fast as a Boolean op, none that I know >of do it faster.
If it's already in an FP register, there's a *chance* it might be faster to do the multiply (or subtract from 0) than if it has to be fetched from RAM. Of course, then there's cache. If you've already been working with that number in the last statement, there's a good chance gcc can keep it in a register. If you're just looping through a long array of numbers and negating them all, and that's it, then a bitwise operation might be faster. It all depends. For that matter, you might try some naive approach and see if the optimizer even loads it into an FP register (by looking at the listing).
>If the value is already in a register, then you can change the >sign directly. Usually there is a "change sign of floating point >value" instruction. If it is in memory, it is probably faster to >change with bit manipulation instructions than to load, change, >and store. I have never known a compiler to do that, though.
Beat me to it. :)
If it is in memory, just xor (eor) first byte with 0x80
Some has it on last byte (little-endian format machines like Intel)

You might end up having both 0 and minus-0 then :-)

Christen


On Wed, 16 Dec 2009 14:59:07 -0600, ramestica wrote:

> I'm just wondering what's the most efficient way for changing the sign > of floating point number. I'm wondering whether a boolean operation on > the binary representation of the number would be faster than multiplying > the number by minus one. My compiler is gcc, it could be that this one > is clever enough to already do the 'fast' thing when a multiplication by > -1 is encountered. I'm not sure. > > thanks, > Rodrigo
gcc -s my_sample.c Then look at the assembly. -- www.wescottdesign.com
Christen Fihl wrote:
> If it is in memory, just xor (eor) first byte with 0x80 > Some has it on last byte (little-endian format machines like Intel) > > You might end up having both 0 and minus-0 then :-)
Which is a good thing to have sometimes.. Nils