Version française
Home     About     Download     Resources     Contact us    
Browse thread
IEEE floating point emulation library?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: IEEE floating point emulation library?
> For a compiler project I am looking for an IEEE floating point
> emulation.  I have already checked http://caml.inria.fr/hump.html and
> the OCaml link database for such a libray.  Does anybody has written
> something like this for OCaml?

This sounds hard.  IEEE floating-point arithmetic is quite intricate.
However:

> To answer the obvious question, why not use the built in float data
> type:  I need access to the representation at the bit level.  Some
> code that decodes a float value into a bit vector (int32/int64) and
> back would be also helpful.

You can do this easily using the following piece of C code:

  #include <caml/mlvalues.h>
  #include <caml/alloc.h>

  value unpack_float(value f)
  {
    union { double d; int64 i; } buffer;
    buffer.d = Double_val(f);
    return copy_int64(buffer.i);
  }

  value pack_float(value i)
  {
    union { double d; int64 i; } buffer;
    buffer.i = Int64_val(i);
    return copy_double(buffer.d);
  }

and the following external declarations:

  external unpack_float: float -> int64 = "unpack_float"
  external pack_float: int64 -> float = "pack_float"

>From the int64 representation, you can easily access every bit of the
float representation.  For single-precision floats and int32, substitute
"double" by "float" and "int64" by "int32" in the code above.

Hope this helps.

- Xavier Leroy