From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e0423f8984d47f76 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-09 00:10:37 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!wn14feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi.com!rwcrnsc54.POSTED!not-for-mail Message-ID: <3F8509D2.7050509@comcast.net> From: "Robert I. Eachus" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Problems converting Float to Integer efficiently References: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.34.139.183 X-Complaints-To: abuse@comcast.net X-Trace: rwcrnsc54 1065683437 24.34.139.183 (Thu, 09 Oct 2003 07:10:37 GMT) NNTP-Posting-Date: Thu, 09 Oct 2003 07:10:37 GMT Organization: Comcast Online Date: Thu, 09 Oct 2003 07:10:37 GMT Xref: archiver1.google.com comp.lang.ada:516 Date: 2003-10-09T07:10:37+00:00 List-Id: 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