From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id MAA10141; Fri, 9 Nov 2001 12:28:40 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id MAA10454 for ; Fri, 9 Nov 2001 12:28:39 +0100 (MET) Received: from comtv.ru (mail.comtv.ru [217.10.32.4]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id fA9BSdn05112 for ; Fri, 9 Nov 2001 12:28:39 +0100 (MET) Received: from [10.2.64.72] (HELO oyster2) by comtv.ru (CommuniGate Pro SMTP 3.4.7) with ESMTP id 1124505; Fri, 09 Nov 2001 14:28:38 +0300 Date: Fri, 9 Nov 2001 14:28:28 +0300 (MSK) From: malc X-Sender: malc@oyster To: "Rafael 'Dido' Sevilla" cc: Caml List Subject: Re: [Caml-list] how to split up a Caml float into its component bytes In-Reply-To: <20011109112909.A9417@team.ph.inter.net> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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 #include #include /* 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