comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeff C," <nolongersafeto@userealemailsniff.com>
Subject: Re: Problems converting Float to Integer efficiently
Date: Thu, 09 Oct 2003 02:36:23 GMT
Date: 2003-10-09T02:36:23+00:00	[thread overview]
Message-ID: <HQ3hb.526332$cF.190598@rwcrnsc53> (raw)
In-Reply-To: KD1hb.8538$RU4.82065@newsfep4-glfd.server.ntli.net


"Dr. Adrian Wrigley" <amtw@linuxchip.demon.co.uk.uk.uk> wrote in message
news:KD1hb.8538$RU4.82065@newsfep4-glfd.server.ntli.net...
> 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
>


Kind of yucky, although the C version that wants rounding ends up being a
little
ugly and calling a builtin to do a round...It does seem to be a shame that
the compiler does not do something better here.

If you can use a more up to date gcc (a 3.X series) and have a pentium 4
(maybe ok on III)
then you can try this little thing I made

contents of a file called truncate.ads

function truncate(y : in float) return Integer;
pragma inline(truncate);

contents of a file called truncate.adb

with Machine_Code;
use Machine_Code;

function truncate(y : in float) return Integer is

temp : integer;
begin
  Asm ("cvttss2si %1, %0",inputs  => float'asm_input("m", y),
                          Outputs => integer'asm_output("=r",temp));
  return temp;
end truncate;

and a little test driver

with Text_IO;
with Truncate;

procedure TestX is

I : Integer;
begin
  I := Truncate(100.8);
  Text_IO.Put_Line(Integer'image(I));
end TestX;


This prints 100 when it runs...Beyond that I can not promise anything :)


In terms of the code, it looks like
gcc -S -O2 -gnatp -fomit-frame-pointer truncate.adb

C:\msys\1.0\home\jcreem>type truncate.s
        .file   "truncate.adb"
.globl _truncate_E
        .data
_truncate_E:
        .byte   0
        .text
        .p2align 4,,15
.globl __ada_truncate
        .def    __ada_truncate; .scl    2;      .type   32;     .endef
__ada_truncate:
/APP
        cvttss2si 4(%esp), %eax
/NO_APP
        ret


Or in other words, it is a single instruction. I can confirm that when
compiled with -O2 -gnatn it does indeed
inline this and you essentially end up with the single instruction.












  parent reply	other threads:[~2003-10-09  2:36 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, [this message]
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