comp.lang.ada
 help / color / mirror / Atom feed
* Problems converting Float to Integer efficiently
@ 2003-10-09  0:06 Dr. Adrian Wrigley
  2003-10-09  1:08 ` Jeffrey Carter
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Dr. Adrian Wrigley @ 2003-10-09  0:06 UTC (permalink / 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




^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2003-10-17 20:57 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2003-10-09  7:10 ` Robert I. Eachus

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