caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: malc <malc@pulsesoft.com>
To: "Rafael 'Dido' Sevilla" <sevillar@team.ph.inter.net>
Cc: Caml List <caml-list@pauillac.inria.fr>
Subject: Re: [Caml-list] how to split up a Caml float into its component bytes
Date: Fri, 9 Nov 2001 14:28:28 +0300 (MSK)	[thread overview]
Message-ID: <Pine.LNX.4.21.0111091425280.665-100000@oyster> (raw)
In-Reply-To: <20011109112909.A9417@team.ph.inter.net>

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


  parent reply	other threads:[~2001-11-09 11:28 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-11-09  3:29 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 ` malc [this message]
2001-11-09 17:09 ` [Caml-list] how to split up a Caml float into its component bytes Ken Rose

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.21.0111091425280.665-100000@oyster \
    --to=malc@pulsesoft.com \
    --cc=caml-list@pauillac.inria.fr \
    --cc=sevillar@team.ph.inter.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).