comp.lang.ada
 help / color / mirror / Atom feed
From: "Robert I. Eachus" <rieachus@comcast.net>
Subject: Re: Problems converting Float to Integer efficiently
Date: Thu, 09 Oct 2003 07:10:37 GMT
Date: 2003-10-09T07:10:37+00:00	[thread overview]
Message-ID: <3F8509D2.7050509@comcast.net> (raw)
In-Reply-To: KD1hb.8538$RU4.82065@newsfep4-glfd.server.ntli.net

Dr. Adrian Wrigley wrote:

> This should be so simple!  What is the expression for getting the right 
> code?
> Shouldn't there be an attribute to an integer out of a float, rounding 
> up/down/nearest/etc??

Two things to keep in mind.  First, there is no right way to do this on 
an x86 architecture chip.  There are best ways for particular models of 
x86 chips, and there are best ways for x86 chips that are in particular 
rounding modes.  Here is more than you will ever want to know about the 
issue: http://www.stereopsis.com/FPU.html and 
http://www.df.lth.se/~john_e/gems/gem0042.html

And finally, my comment on the mess: The real (ouch!) problem is that 
you can't convert to integer and get the result in a register.  Add to 
that all the fun you can have getting a floating-point value into a 
register, and you start to wonder why you are using the floating-point 
registers at all.  Worse, on a Pentium 4 you don't even want to use the 
FP (st(n)) registers for floating-point, you want to use the SSE2 registers!

But I did have one case where I needed to do about the same thing as you 
are doing realtively fast (the code was in an interrupt handler).  I was 
calculating a floating-point distance and needed to truncate to an 
integer.  I knew the value was in the range 0..2^31-1, so I did the 
magic add, stored the value, and used an overlay to reference the 
low-order 32-bits as an integer.  Worked fine, but I felt very guilty 
afterwards.  As I said to start, there is no right way to do this, just 
best ways for particular processors and situations.

-- 
                                                     Robert I. Eachus

"Quality is the Buddha. Quality is scientific reality. Quality is the 
goal of Art. It remains to work these concepts into a practical, 
down-to-earth context, and for this there is nothing more practical or 
down-to-earth than what I have been talking about all along...the repair 
of an old motorcycle."  -- from Zen and the Art of Motorcycle 
Maintenance by Robert Pirsig




      parent reply	other threads:[~2003-10-09  7:10 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-09  0:06 Problems converting Float to Integer efficiently Dr. Adrian Wrigley
2003-10-09  1:08 ` Jeffrey Carter
2003-10-09  2:36 ` Jeff C,
2003-10-09  3:21   ` Dr. Adrian Wrigley
2003-10-09  3:36     ` Jeff C,
2003-10-17 20:57     ` Randy Brukardt
2003-10-09 22:36   ` Dr. Adrian Wrigley
2003-10-10  2:05     ` Jeff C,
2003-10-10 17:15     ` Robert I. Eachus
2003-10-11  1:47     ` Waldek Hebisch
2003-10-09  7:10 ` Robert I. Eachus [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox