[ocaml-extlib] r422 committed - IO: read/write 32-bit floats (Closes issue 6)

4 views
Skip to first unread message

codesite...@google.com

unread,
Nov 12, 2013, 10:48:35 AM11/12/13
to ocaml-extl...@googlegroups.com
Revision: 422
Author: ygrekheretix
Date: Tue Nov 12 15:48:23 2013 UTC
Log: IO: read/write 32-bit floats (Closes issue 6)

http://code.google.com/p/ocaml-extlib/source/detail?r=422

Modified:
/trunk/extlib/IO.ml
/trunk/extlib/IO.mli

=======================================
--- /trunk/extlib/IO.ml Sun Jul 7 07:04:11 2013 UTC
+++ /trunk/extlib/IO.ml Tue Nov 12 15:48:23 2013 UTC
@@ -491,6 +491,9 @@
let big = Int64.of_int32 (read_real_i32 ch) in
Int64.logor (Int64.shift_left big 32) small

+let read_float32 ch =
+ Int32.float_of_bits (read_real_i32 ch)
+
let read_double ch =
Int64.float_of_bits (read_i64 ch)

@@ -536,6 +539,9 @@
write_real_i32 ch (Int64.to_int32 n);
write_real_i32 ch (Int64.to_int32 (Int64.shift_right_logical n 32))

+let write_float32 ch f =
+ write_real_i32 ch (Int32.bits_of_float f)
+
let write_double ch f =
write_i64 ch (Int64.bits_of_float f)

@@ -589,6 +595,9 @@
let small = Int64.logor base (Int64.shift_left (Int64.of_int ch4) 24) in
Int64.logor (Int64.shift_left big 32) small

+let read_float32 ch =
+ Int32.float_of_bits (read_real_i32 ch)
+
let read_double ch =
Int64.float_of_bits (read_i64 ch)

@@ -622,6 +631,9 @@
write_real_i32 ch (Int64.to_int32 (Int64.shift_right_logical n 32));
write_real_i32 ch (Int64.to_int32 n)

+let write_float32 ch f =
+ write_real_i32 ch (Int32.bits_of_float f)
+
let write_double ch f =
write_i64 ch (Int64.bits_of_float f)

=======================================
--- /trunk/extlib/IO.mli Sun Jul 7 07:04:11 2013 UTC
+++ /trunk/extlib/IO.mli Tue Nov 12 15:48:23 2013 UTC
@@ -196,8 +196,11 @@
val read_i64 : input -> int64
(** Read a signed 64-bit integer as an OCaml int64. *)

+val read_float32 : input -> float
+(** Read an IEEE single precision floating point value (32 bits). *)
+
val read_double : input -> float
-(** Read an IEEE double precision floating point value. *)
+(** Read an IEEE double precision floating point value (64 bits). *)

val read_string : input -> string
(** Read a null-terminated string. *)
@@ -223,8 +226,11 @@
val write_i64 : 'a output -> int64 -> unit
(** Write an OCaml int64. *)

+val write_float32 : 'a output -> float -> unit
+(** Write an IEEE single precision floating point value (32 bits). *)
+
val write_double : 'a output -> float -> unit
-(** Write an IEEE double precision floating point value. *)
+(** Write an IEEE double precision floating point value (64 bits). *)

val write_string : 'a output -> string -> unit
(** Write a string and append an null character. *)
@@ -242,6 +248,7 @@
val read_i32 : input -> int
val read_real_i32 : input -> int32
val read_i64 : input -> int64
+ val read_float32 : input -> float
val read_double : input -> float

val write_ui16 : 'a output -> int -> unit
@@ -249,6 +256,7 @@
val write_i32 : 'a output -> int -> unit
val write_real_i32 : 'a output -> int32 -> unit
val write_i64 : 'a output -> int64 -> unit
+ val write_float32 : 'a output -> float -> unit
val write_double : 'a output -> float -> unit

end
Reply all
Reply to author
Forward
0 new messages