caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Need for a built in round_to_int function
@ 2005-02-20 20:22 Erik de Castro Lopo
  2005-02-21  0:00 ` [Caml-list] " Robert Roessler
  2005-02-21 11:54 ` Erik de Castro Lopo
  0 siblings, 2 replies; 9+ messages in thread
From: Erik de Castro Lopo @ 2005-02-20 20:22 UTC (permalink / raw)
  To: caml-list

Hi all,

I am about to port some code from C to O'caml. This code uses the 
C99 function :

    long int lrint (double d) ;

which performs rounding on the double and then converts that to
a long int.

In O'caml the only option seems to be:

    let round_to_int f = int_of_float (f +. 0.5) ;;

The problem is that this code on i386 produces really slow code:

    804b385:    dd 44 98 fc        fldl   0xfffffffc(%eax,%ebx,4)
    804b389:    de c1              faddp  %st,%st(1)
    804b38b:    83 ec 08           sub    $0x8,%esp
    804b38e:    d9 7c 24 04        fnstcw 0x4(%esp)
    804b392:    66 8b 44 24 04     mov    0x4(%esp),%ax
    804b397:    b4 0c              mov    $0xc,%ah
    804b399:    66 89 44 24 00     mov    %ax,0x0(%esp)
    804b39e:    d9 6c 24 00        fldcw  0x0(%esp)
    804b3a2:    db 1c 24           fistpl (%esp)
    804b3a5:    8b 04 24           mov    (%esp),%eax
    804b3a8:    d9 6c 24 04        fldcw  0x4(%esp)
    804b3ac:    83 c4 08           add    $0x8,%esp

The killer here is the two fldcw (floating point load control word)
instructions, around the fistpl (which actually does the float to int 
conversion). Loading the FP control work causes a flush of the FPU
pipeline. In code with a lot of floating point code interspersed
with a round to int, there can be a significant slow down due to
the fldcw instructions.

The lrint function in C, replaces all the above with one fistpl
and a single mov instruction and leaves the floating point
control word intact. In C code that moved from:

    (int) floor (f + 0.5)

to
    lrintf (f)

I have seen an up to 4 fold increase in speed.

I've looked at the code for the O'Caml compiler and I think I 
know how to implement this, at least for x86 and PowerPC, the two
architectures I have access to. If I was to supply a patch would
it be accepted?


I know other suggestions like this one :

    http://sardes.inrialpes.fr/~aschmitt/cwn/2003.11.18.html#1

were not viewed favourably, but the addition of a single function
with an explicit behaviour is a far neater solution.

Regards,
Erik
-- 
+-----------------------------------------------------------+
  Erik de Castro Lopo  nospam@mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
"There are two kinds of large software systems: those that evolved
from small systems and those that don't work."
-- Seen on slashdot.org, then quoted by amk


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

end of thread, other threads:[~2005-02-22  7:23 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-02-20 20:22 Need for a built in round_to_int function Erik de Castro Lopo
2005-02-21  0:00 ` [Caml-list] " Robert Roessler
2005-02-21  1:51   ` Erik de Castro Lopo
2005-02-21  4:34     ` Robert Roessler
2005-02-21  6:50       ` Erik de Castro Lopo
2005-02-21 11:54 ` Erik de Castro Lopo
2005-02-21 16:00   ` Xavier Leroy
2005-02-22  7:23     ` Erik de Castro Lopo
2005-02-21 16:01   ` Hendrik Tews

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).