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 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-10 18:47:11 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.tpinternet.pl!news.man.poznan.pl!pwr.wroc.pl!panorama.wcss.wroc.pl!hebisch From: hebisch@math.uni.wroc.pl (Waldek Hebisch) Newsgroups: comp.lang.ada Subject: Re: Problems converting Float to Integer efficiently Date: 11 Oct 2003 01:47:13 GMT Organization: Politechnika Wroclawska Message-ID: References: NNTP-Posting-Host: hera.math.uni.wroc.pl X-Trace: panorama.wcss.wroc.pl 1065836833 575 156.17.86.1 (11 Oct 2003 01:47:13 GMT) X-Complaints-To: abuse@news.pwr.wroc.pl NNTP-Posting-Date: 11 Oct 2003 01:47:13 GMT X-Newsreader: TIN [version 1.2 PL2] Xref: archiver1.google.com comp.lang.ada:651 Date: 2003-10-11T01:47:13+00:00 List-Id: Dr. Adrian Wrigley (amtw@linuxchip.demon.co.uk.uk.uk) wrote: : I have had no problems on my Athlon (1100MHz clock speed) and GNAT 3.15p : My experiments show the following: : -- X is integer, Y is float : X := X + Truncate(Y); -- 0.029us 1x : X := X + Integer (Y); -- 0.360us 12x : X := X + Integer (Float'Floor (Y)); -- 1.196us 41x The times seem way too high, you should be able to do such calculation in 2-3 clocks and you get more then 30 (the relative timings however look reasonable). : apparently good source code to crawl. Is this a necessary price for ditching low-level HLLs : like 'C'? Perhaps someone should create a web site highlighting the problem areas and : explaining the solutions? Volunteers anybody? Rounding/truncating is a well known problem for many years. I had very similar problem -- just in C. I wanted rounding, gcc introduced truncation. So I added bias and called floor first. When profiling it turned out that my program spent 70% time roundind and I made about 10 FLOPS per rounding. So the problem really has nothing to do with Ada, but just Intel folks probably forget about HLLs when designing 8087 -- they choose to save two bits in instructions and used rounding mode stored in proccesor state word. So to exexute a single instruction using different rounding mode you need to store old mode, set new one, do the work and restore old mode -- that is many instructions (and state change used to be very slow one). Surely, gcc could do better job, but if you want really fast rouding/truncation usually you can do faster. If processor is set to rounding (usual for normal calculation) then a single (1 clock) assembly instruction will round a number to integer. If your number have limited range and you know the precision setting of the processor then you can truncate adding and substracting apropriate constant. All this is really much below C level (not to mention Ada). And only programmer knows if assumptions are satisfied, no compiler can check that. -- Waldek Hebisch hebisch@math.uni.wroc.pl