caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] how to split up a Caml float into its component bytes
@ 2001-11-09  3:29 Rafael 'Dido' Sevilla
  2001-11-09 11:05 ` Thorsten Ohl
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Rafael 'Dido' Sevilla @ 2001-11-09  3:29 UTC (permalink / raw)
  To: Caml List


I've been writing a byte compiler for a small language using Objective
Caml, and now am thinking about incorporating floating point support
into the language.  I'm wondering how I would convert a floating point
number in OCaml (which I hope I am safe in assuming is IEEE-754) into
its equivalent bytes.  I need it to be able to output bytecode
instructions that will load floating point constants into the virtual
machine.  In C this is fairly trivial to do; not sure how to do it in
OCaml.

-- 
Rafael R. Sevilla <sevillar@team.ph.inter.net>   +63(2)   8177746 ext. 8311
Programmer, Inter.Net Philippines                +63(917) 4458925
http://dido.engr.internet.org.ph/                OpenPGP Key ID: 0x5CDA17D8
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* [Caml-list] how to split up a Caml float into its component bytes
  2001-11-09  3:29 [Caml-list] how to split up a Caml float into its component bytes Rafael 'Dido' Sevilla
@ 2001-11-09 11:05 ` Thorsten Ohl
  2001-11-09 12:18   ` Xavier Leroy
  2001-11-09 11:28 ` [Caml-list] how to split up a Caml float into its component bytes malc
  2001-11-09 17:09 ` Ken Rose
  2 siblings, 1 reply; 6+ messages in thread
From: Thorsten Ohl @ 2001-11-09 11:05 UTC (permalink / raw)
  To: Caml List; +Cc: Rafael 'Dido' Sevilla

Rafael 'Dido' Sevilla writes:

> I'm wondering how I would convert a floating point
> number in OCaml (which I hope I am safe in assuming is IEEE-754) into
> its equivalent bytes.

AFAIK, you have to write a C function.  There is an undocumented
primitive 

    external float_of_bytes : string -> float = "float_of_bytes"

defined in byterun/floats.c.  Unfortunately:

   a) the inverse is absent
   b) on little endian machines one has to swap bytes.

A pair of primitives

    val unformatted_float_of_string : string -> float
    val unformatted_float_to_string : float -> string

together with

    val output_binary_float : out_channel -> float -> unit
    val input_binary_float : in_channel -> float

would be very helpful.  I made a version of input_binary_float from
input_string and some byte-swapping, but a portable version (that gives
me the warm fuzzy feeling that it does not run only on Linux) would be
very welcome: there are programs that spend most of their time parsing
and formatting floats.
-- 
Thorsten Ohl, Physics Department, TU Darmstadt -- ohl@hep.tu-darmstadt.de
http://heplix.ikp.physik.tu-darmstadt.de/~ohl/ [<=== PGP public key here]
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] how to split up a Caml float into its component bytes
  2001-11-09  3:29 [Caml-list] how to split up a Caml float into its component bytes Rafael 'Dido' Sevilla
  2001-11-09 11:05 ` Thorsten Ohl
@ 2001-11-09 11:28 ` malc
  2001-11-09 17:09 ` Ken Rose
  2 siblings, 0 replies; 6+ messages in thread
From: malc @ 2001-11-09 11:28 UTC (permalink / raw)
  To: Rafael 'Dido' Sevilla; +Cc: Caml List

On Fri, 9 Nov 2001, Rafael 'Dido' Sevilla wrote:

> 
> I've been writing a byte compiler for a small language using Objective
> Caml, and now am thinking about incorporating floating point support
> into the language.  I'm wondering how I would convert a floating point
> number in OCaml (which I hope I am safe in assuming is IEEE-754) into
> its equivalent bytes.  I need it to be able to output bytecode
> instructions that will load floating point constants into the virtual
> machine.  In C this is fairly trivial to do; not sure how to do it in
> OCaml.

This code is for single precission floats.

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

/* Kindly suggested by Xavier Leroy on Caml Mailing List */
value unpack_float (value s)
{
  union { float f; char c[4]; } buffer;
  memcpy (buffer.c, String_val (s), 4);
  return copy_double ((double) buffer.f);
}

value pack_float (value d)
{
  value s;
  union { float f; char c[4]; } buffer;

  s = alloc_string (4);
  buffer.f = (float) Double_val (d);
  memcpy (String_val (s), buffer.c, 4);
  return s;
}

module Fltstub = struct
  external pack : float -> string = "pack_float"
  external unpack : string -> float = "unpack_float"
end

-- 
mailto:malc@pulsesoft.com

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] how to split up a Caml float into its component bytes
  2001-11-09 11:05 ` Thorsten Ohl
@ 2001-11-09 12:18   ` Xavier Leroy
  2001-11-09 13:32     ` Thorsten Ohl
  0 siblings, 1 reply; 6+ messages in thread
From: Xavier Leroy @ 2001-11-09 12:18 UTC (permalink / raw)
  To: Thorsten Ohl; +Cc: Caml List, Rafael 'Dido' Sevilla

> > I'm wondering how I would convert a floating point
> > number in OCaml (which I hope I am safe in assuming is IEEE-754) into
> > its equivalent bytes.
> 
> AFAIK, you have to write a C function.

Starting with OCaml 3.01, the Int64 module provides two functions
"bits_of_float" and "float_of_bits" which convert between floats and
an int64 matching the underlying 64-bit representation of the float.
>From the int64, extracting individual bytes is a simple matter of
shifting and masking.

> There is an undocumented primitive 
> 
>     external float_of_bytes : string -> float = "float_of_bytes"
> 
> defined in byterun/floats.c.  Unfortunately:
> 
>    a) the inverse is absent
>    b) on little endian machines one has to swap bytes.

a) is correct, b) is not (big endian representation is enforced).
Still, the functions from Int64 are less error-prone,
e.g. w.r.t. endianness.

- Xavier Leroy
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* (no subject)
  2001-11-09 12:18   ` Xavier Leroy
@ 2001-11-09 13:32     ` Thorsten Ohl
  0 siblings, 0 replies; 6+ messages in thread
From: Thorsten Ohl @ 2001-11-09 13:32 UTC (permalink / raw)
  To: Caml List; +Cc: Xavier Leroy

> Starting with OCaml 3.01, the Int64 module provides two functions
> "bits_of_float" and "float_of_bits" which convert between floats and
> an int64 matching the underlying 64-bit representation of the float.

Thanks.  I forgot to look among the Int* modules when I was hunting
for floating point conversions :-(.

> >     external float_of_bytes : string -> float = "float_of_bytes"
> >    b) on little endian machines one has to swap bytes.
>
> b) is not (big endian representation is enforced).

Now I'm confused.  On my Linux box, I have to swap bytes in order
to read binary floating point numbers from a stream (where I can not
mmap Bigarrays).

    let unsafe_rev8 s =
      let swap i1 i2 =
        let tmp = String.unsafe_get s i1 in
        String.unsafe_set s i1 (String.unsafe_get s i2);
        String.unsafe_set s i2 tmp in
      swap 0 7;
      swap 1 6;
      swap 2 5;
      swap 3 4

    let little_endian = true

    let input_binary_float ic =
      let buf = String.create 8 in
      really_input ic buf 0 8;
      if little_endian then
        unsafe_rev8 buf;
      float_of_bytes buf

But that's academical, because I will switch to Int64 now.  Thanks
again.
-- 
Thorsten Ohl, Physics Department, TU Darmstadt -- ohl@hep.tu-darmstadt.de
http://heplix.ikp.physik.tu-darmstadt.de/~ohl/ [<=== PGP public key here]
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] how to split up a Caml float into its component bytes
  2001-11-09  3:29 [Caml-list] how to split up a Caml float into its component bytes Rafael 'Dido' Sevilla
  2001-11-09 11:05 ` Thorsten Ohl
  2001-11-09 11:28 ` [Caml-list] how to split up a Caml float into its component bytes malc
@ 2001-11-09 17:09 ` Ken Rose
  2 siblings, 0 replies; 6+ messages in thread
From: Ken Rose @ 2001-11-09 17:09 UTC (permalink / raw)
  To: Rafael 'Dido' Sevilla; +Cc: Caml List

Rafael 'Dido' Sevilla wrote:
> 
> I've been writing a byte compiler for a small language using Objective
> Caml, and now am thinking about incorporating floating point support
> into the language.  I'm wondering how I would convert a floating point
> number in OCaml (which I hope I am safe in assuming is IEEE-754) into
> its equivalent bytes.  I need it to be able to output bytecode
> instructions that will load floating point constants into the virtual
> machine.  In C this is fairly trivial to do; not sure how to do it in
> OCaml.

I was having a similar problem and came up with these two functions,
intended for a C compiler.  They produce strings encoding 32 bits at a
time, to write into assembly output.  I'm not sure it handles gradual
underflow correctly, but since my target hardware doesn't, it didn't
matter to me.

 - ken

and cook_float f =
  let mant, exp = frexp f in 
  let sign = if mant < 0.0 then Int32.min_int else Int32.zero in
  let exponent = Int32.shift_left (Int32.of_int(exp + 126)) 23 in
  let mantissa = Int32.of_float((mant -. 0.5) *. 16777216.0) in
  Int32.format "0x%x" (Int32.logor sign (Int32.logor exponent mantissa)) 

and cook_double d =
  if d = 0.0 then ("0","0") else
  let mant, exp = frexp d in
  let sign = if mant < 0.0 then Int32.min_int else Int32.zero in
  let exponent = Int32.shift_left (Int32.of_int(exp + 1022)) 20 in
  let f,t = modf (((abs_float mant) -. 0.5) *. 2097152.0) in
  let highmantissa = Int32.of_float(t) in
  let highword = Int32.logor sign (Int32.logor exponent highmantissa) in
  let l, h = modf (f *. 65536.0) in
  let low1 = Int32.shift_left (Int32.of_float h) 16 in
  let low2 = Int32.of_float (l *. 65536.0) in
  let lowmantissa = Int32.of_float(f *. (ldexp 1.0 32)) in
  let lowword = Int32.logor low1 low2 in
  Int32.format "0x%x" highword, Int32.format "0x%x" lowword
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

end of thread, other threads:[~2001-11-09 17:09 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-09  3:29 [Caml-list] how to split up a Caml float into its component bytes Rafael 'Dido' Sevilla
2001-11-09 11:05 ` Thorsten Ohl
2001-11-09 12:18   ` Xavier Leroy
2001-11-09 13:32     ` Thorsten Ohl
2001-11-09 11:28 ` [Caml-list] how to split up a Caml float into its component bytes malc
2001-11-09 17:09 ` Ken Rose

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).