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=0.5 required=5.0 tests=BAYES_00,TO_NO_BRKTS_PCNT autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e0423f8984d47f76,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-08 17:06:03 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!newsfeed.vmunix.org!newspeer1-gui.server.ntli.net!ntli.net!newsfep4-glfd.server.ntli.net.POSTED!53ab2750!not-for-mail From: "Dr. Adrian Wrigley" User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Problems converting Float to Integer efficiently Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Date: Thu, 09 Oct 2003 01:06:01 +0100 NNTP-Posting-Host: 81.100.88.147 X-Complaints-To: abuse@ntlworld.com X-Trace: newsfep4-glfd.server.ntli.net 1065657962 81.100.88.147 (Thu, 09 Oct 2003 01:06:02 BST) NNTP-Posting-Date: Thu, 09 Oct 2003 01:06:02 BST Organization: ntl Cablemodem News Service Xref: archiver1.google.com comp.lang.ada:507 Date: 2003-10-09T01:06:01+01:00 List-Id: Hi all! I have been having problems getting decent code out of GNAT 3.15p (Intel architecture) All I want to do is get the integer part of a Float variable (without checks). I don't want rounding to nearest like I get with "Integer (Y)" In C, a function to do this might look like: int x (float y) { return y; } Which generates code like: pushl %ebp movl %esp,%ebp subl $12,%esp flds 8(%ebp) fnstcw -4(%ebp) movl -4(%ebp),%edx movb $12,%dh movl %edx,-12(%ebp) fldcw -12(%ebp) fistpl -12(%ebp) movl -12(%ebp),%eax fldcw -4(%ebp) movl %ebp,%esp popl %ebp ret In Ada you could try something like: pragma Suppress (All_Checks); function X (A : Float) return Integer is begin return Integer (Float'Floor (A)); end X; which generates twice as much code, and a call to system__fat_flt__fat_float__floor I have also seen "Integer (A - 0.5)" used, but the core of the code for this is still *much* more verbose than the core of the C function. No matter what I try, I get *at least* twice as much code to execute out of the Ada. My application is generating indexes for a multi-dimensional array structure from floating point values. If I get a major performance penalty from Ada (>20%), I will be tempted to code this in C or assembly :( Could I get the right code in my application using a Machine Operation in an pragama Inlined function? 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?? -- Adrian Wrigley, Cambridge, UK