comp.lang.ada
 help / color / mirror / Atom feed
From: hebisch@math.uni.wroc.pl (Waldek Hebisch)
Subject: Re: Problems converting Float to Integer efficiently
Date: 11 Oct 2003 01:47:13 GMT
Date: 2003-10-11T01:47:13+00:00	[thread overview]
Message-ID: <bm7nf1$hv$1@panorama.wcss.wroc.pl> (raw)
In-Reply-To: Oplhb.9211$RU4.88029@newsfep4-glfd.server.ntli.net

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 



  parent reply	other threads:[~2003-10-11  1:47 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 [this message]
2003-10-09  7:10 ` Robert I. Eachus
replies disabled

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