This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Obtaining the bit pattern of a floating point number


And please be careful not to confuse anyone with the name ftoi: the ftoi
below and the MASM (and perhaps other assemblers') pseudo intstruction
FTOI are not the same.

http://www-zeuthen.desy.de/apewww/APE/software/asm/anext/MASM/conversion/ftoi_masm.php

MASM FTOI takes the integer part of the floating point number and
converts it to an integer. The ftoi function below (not part of any
standard library that I know of, nor could find on a short google
search), just gets the bits, not the integer portion.

		--Dean

On Wed, 27 May 2009, Michael Meissner wrote:

> On Tue, May 26, 2009 at 09:42:40PM -0700, me22 wrote:
> > 2009/5/26 Florian Weimer <fw@deneb.enyo.de>:
> > > * Michael Meissner:
> > >
> > >> On Sat, May 23, 2009 at 03:55:34PM +0200, Florian Weimer wrote:
> > >>> It seems to me that x86_64 supports direct moves from XMM to
> > >>> general-purpose registers, so that it's possible to access the
> > >>> representation of a floating point value without going through memory:
> > >>>
> > >>> long getbits(double src)
> > >>> {
> > >>> ? long result;
> > >>> ? __asm__ ("movq %1, %0" : "=q" (result) : "x" (src));
> > >>> ? return result;
> > >>> }
> > >>>
> > >>> Is this functionally available as some sort of built-in?
> > >>
> > >> Well you typically can use a union to get the bits.
> > >
> > > Okay. ?I didn't realize that GCC was smart enough to avoid the
> > > round-trip through memory.
> > >
> > 
> > In my experience compilers are often even smart enough to have this
> > avoid the memory roundtrip, and it's undeniably legal:
> > 
> > int ftoi(float x) {
> >     int i;
> >     memcpy(&i, &x, 4);
> >     return i;
> > }
> 
> Well from a pedantic point of view, it is only legal if sizeof (i) == sizeof
> (x) == 4, but you presumably already know that.
> 
> 

-- 
Av8 Internet   Prepared to pay a premium for better service?
www.av8.net         faster, more reliable, better service
617 344 9000   



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]