comp.lang.ada
 help / color / mirror / Atom feed
From: "Dr. Adrian Wrigley" <amtw@linuxchip.demon.co.uk.uk.uk>
Subject: Problems converting Float to Integer efficiently
Date: Thu, 09 Oct 2003 01:06:01 +0100
Date: 2003-10-09T01:06:01+01:00	[thread overview]
Message-ID: <KD1hb.8538$RU4.82065@newsfep4-glfd.server.ntli.net> (raw)

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




             reply	other threads:[~2003-10-09  0:06 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-09  0:06 Dr. Adrian Wrigley [this message]
2003-10-09  1:08 ` Problems converting Float to Integer efficiently 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
replies disabled

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