Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: OCaml and C math routines
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: John R Harrison <johnh@i...>
Subject: Re: OCaml and C math routines

David McClain writes:

| OCaml'ers... I would be very interested to hear your response to this!

I guess I should comment, since I work closely with the people designing
math functions for various Intel architectures, and have even written a
few myself.

| Members of the SML basis committee and the Ocaml group probably can 
| tell us the pros and cons of calling the native math library vs. having
| it written in ML itself.

There are (at least) three things to consider in writing a good math
library function:

 * accuracy

 * speed

 * proper handling of exceptional cases

Getting good speed and accuracy together (or sometimes just one of them at
at a time) is often non-trivial. And even given a good basic design for the
core function, it's a serious amount of work to make all the peculiar
corner cases work sensibly, e.g. detecting overflow and underflow
correctly, behaving sensibly when unusual exceptions like "inexact" are
enabled, or even providing accurate answers when the rounding mode is not
the default of round-to-nearest or when trigonometric functions are applied
to huge arguments.

Besides, there's room for disagreement over what *should* be done in many
of the obscure cases. There's no IEEE standard for the transcendentals,
though C99 is establishing some sort of norm. In the absence of absolute
standards, there are hard trade-offs to be made between various desirable

So, on the one hand, it's ambitious bordering on hubristic for SML experts
who are not floating point specialists to reimplement all this stuff
themselves. On the other hand, it's certainly true that most available math
libraries have shortcomings in some area or another, so if the SML people
are able and determined enough, it would be possible in principle for them
to do better. For an idea of some of the techniques we've used at Intel in
recent math library work, you might look at the following:

  "The Computation of Transcendental Functions on the IA-64 Architecture"

  "New Algorithms for Improved Transcendental Functions on IA-64"

The original question seemed particularly focused on speed. In my
experience the really critical math functions tend to be coded in
assembler, simply because C compilers don't usually provide adequate
control or provide intrinsics to access special hardware features. For
less popular functions (like the cube root and Bessel functions), it's
common to use some portable C routines such as those in FDLIBM:

There's certainly a case for having a similar library in ML, but the case
for using it as the default for important functions is more questionable.