Skip to content

Commit e618ecc

Browse files
committed
provide primitive caml_int32_float_of_bits and caml_int32_bits_of_float
1 parent d13a333 commit e618ecc

14 files changed

+279
-90
lines changed

jscomp/lam_dispatch_primitive.ml

+3
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ let query (prim : Lam_compile_env.primitive_description)
290290
-> Js_long.compare args
291291
| "caml_int64_of_string"
292292
-> Js_long.of_string args
293+
294+
| "caml_int32_float_of_bits"
295+
| "caml_int32_bits_of_float"
293296
| "caml_int64_bits_of_float"
294297
| "caml_int64_float_of_bits"
295298
| "caml_classify_float"

jscomp/runtime/.depend

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ caml_array.cmi :
22
caml_bigarray.cmi :
33
caml_builtin_exceptions.cmi :
44
caml_exceptions.cmi :
5-
caml_float.cmi :
5+
caml_float.cmi : caml_int64.cmi
66
caml_format.cmi :
77
caml_int64.cmi :
88
caml_lexer.cmi :
@@ -23,8 +23,8 @@ caml_curry.cmo : caml_oo.cmi
2323
caml_curry.cmx : caml_oo.cmx
2424
caml_exceptions.cmo : caml_exceptions.cmi
2525
caml_exceptions.cmx : caml_exceptions.cmi
26-
caml_float.cmo : caml_float.cmi
27-
caml_float.cmx : caml_float.cmi
26+
caml_float.cmo : typed_array.cmo caml_int64.cmi caml_float.cmi
27+
caml_float.cmx : typed_array.cmx caml_int64.cmx caml_float.cmi
2828
caml_format.cmo : caml_utils.cmi caml_format.cmi
2929
caml_format.cmx : caml_utils.cmx caml_format.cmi
3030
caml_int64.cmo : caml_int64.cmi
@@ -47,6 +47,8 @@ caml_sys.cmo : caml_sys.cmi
4747
caml_sys.cmx : caml_sys.cmi
4848
caml_utils.cmo : caml_utils.cmi
4949
caml_utils.cmx : caml_utils.cmi
50+
typed_array.cmo :
51+
typed_array.cmx :
5052
caml_array.cmo : caml_array.cmi
5153
caml_array.cmj : caml_array.cmi
5254
caml_bigarray.cmo : caml_bigarray.cmi
@@ -57,8 +59,8 @@ caml_curry.cmo : caml_oo.cmi
5759
caml_curry.cmj : caml_oo.cmj
5860
caml_exceptions.cmo : caml_exceptions.cmi
5961
caml_exceptions.cmj : caml_exceptions.cmi
60-
caml_float.cmo : caml_float.cmi
61-
caml_float.cmj : caml_float.cmi
62+
caml_float.cmo : typed_array.cmo caml_int64.cmi caml_float.cmi
63+
caml_float.cmj : typed_array.cmj caml_int64.cmj caml_float.cmi
6264
caml_format.cmo : caml_utils.cmi caml_format.cmi
6365
caml_format.cmj : caml_utils.cmj caml_format.cmi
6466
caml_int64.cmo : caml_int64.cmi
@@ -81,3 +83,5 @@ caml_sys.cmo : caml_sys.cmi
8183
caml_sys.cmj : caml_sys.cmi
8284
caml_utils.cmo : caml_utils.cmi
8385
caml_utils.cmj : caml_utils.cmi
86+
typed_array.cmo :
87+
typed_array.cmj :

jscomp/runtime/caml_float.d.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
export var caml_int64_bits_of_float: any ;
2-
export var caml_int64_float_of_bits: any ;
1+
export var caml_int64_float_of_bits: (x : any) => any ;
2+
export var caml_int32_float_of_bits: (x : any) => any ;
3+
export var caml_int64_bits_of_float: (x : any) => any ;
4+
export var caml_int32_bits_of_float: (x : any) => any ;
35
export var caml_classify_float: (x : any) => any ;
46
export var caml_modf_float: (x : any) => any ;
57
export var caml_ldexp_float: any ;

jscomp/runtime/caml_float.js

+30-21
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,39 @@
22
'use strict';
33

44

5+
function caml_int64_float_of_bits(x) {
6+
var to_int32 = function (x) {
7+
return x;
8+
};
9+
var int32 = new Int32Array(/* array */[
10+
to_int32(x[/* lo */0]),
11+
to_int32(x[/* hi */1])
12+
]);
13+
return new Float64Array(int32.buffer)[0];
14+
}
515

6-
function $$caml_int64_bits_of_float (x) {
7-
// TODO:
8-
// can be allocated globally to avoid allocation each time
9-
// Need check existense of the API we used here
10-
var u = new Float64Array([x]);
16+
function caml_int64_bits_of_float(x) {
17+
var to_nat = function (x) {
18+
return x;
19+
};
20+
var u = new Float64Array(/* float array */[x]);
1121
var int32 = new Int32Array(u.buffer);
12-
// actually we can return it directly without a conversion
13-
return [int32[0], int32[1]];
22+
return /* record */[
23+
to_nat(int32[0]),
24+
to_nat(int32[1])
25+
];
1426
}
1527

16-
function $$caml_int64_float_of_bits (x) {
17-
var int32 = new Int32Array(x);
18-
var float64 = new Float64Array(int32.buffer);
19-
return float64[0];
28+
function caml_int32_float_of_bits(x) {
29+
var int32 = new Int32Array(/* array */[x]);
30+
var float32 = new Float32Array(int32.buffer);
31+
return float32[0];
2032
}
2133

22-
;
34+
function caml_int32_bits_of_float(x) {
35+
var float32 = new Float32Array(/* float array */[x]);
36+
return new Int32Array(float32.buffer)[0];
37+
}
2338

2439
function caml_classify_float(x) {
2540
if (isFinite(x)) {
@@ -166,14 +181,6 @@ function $$caml_log10_float (x) { return Math.LOG10E * Math.log(x); }
166181

167182
;
168183

169-
function caml_int64_bits_of_float(prim) {
170-
return $$caml_int64_bits_of_float(prim);
171-
}
172-
173-
function caml_int64_float_of_bits(prim) {
174-
return $$caml_int64_float_of_bits(prim);
175-
}
176-
177184
function caml_ldexp_float(prim, prim$1) {
178185
return $$caml_ldexp_float(prim, prim$1);
179186
}
@@ -186,8 +193,10 @@ function caml_hypot_float(prim, prim$1) {
186193
return $$caml_hypot_float(prim, prim$1);
187194
}
188195

189-
exports.caml_int64_bits_of_float = caml_int64_bits_of_float;
190196
exports.caml_int64_float_of_bits = caml_int64_float_of_bits;
197+
exports.caml_int32_float_of_bits = caml_int32_float_of_bits;
198+
exports.caml_int64_bits_of_float = caml_int64_bits_of_float;
199+
exports.caml_int32_bits_of_float = caml_int32_bits_of_float;
191200
exports.caml_classify_float = caml_classify_float;
192201
exports.caml_modf_float = caml_modf_float;
193202
exports.caml_ldexp_float = caml_ldexp_float;

jscomp/runtime/caml_float.ml

+33-24
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,44 @@
1818

1919
(* Author: Hongbo Zhang *)
2020

21-
[%%js.raw{|
22-
function $$caml_int64_bits_of_float (x) {
23-
// TODO:
24-
// can be allocated globally to avoid allocation each time
25-
// Need check existense of the API we used here
26-
var u = new Float64Array([x]);
27-
var int32 = new Int32Array(u.buffer);
28-
// actually we can return it directly without a conversion
29-
return [int32[0], int32[1]];
30-
}
21+
open Typed_array
3122

32-
function $$caml_int64_float_of_bits (x) {
33-
var int32 = new Int32Array(x);
34-
var float64 = new Float64Array(int32.buffer);
35-
return float64[0];
36-
}
37-
|}]
3823

39-
external caml_int64_float_of_bits : Int64.t -> float = "$$caml_int64_float_of_bits"
40-
[@@js.call ] [@@js.local]
24+
let caml_int64_float_of_bits (x : Caml_int64.t) =
25+
let to_int32 (x : nativeint) = x |> Nativeint.to_int |> Int32.of_int
26+
in
27+
(*TODO:
28+
This should get inlined, we should apply a simple inliner in the js layer,
29+
the thing is its lambda representation is complex but after js layer,
30+
it's qutie simple
31+
*)
32+
let int32 = Int32_array.create [| to_int32 x.lo; to_int32 x.hi |] in
33+
Float64_array.get (Float64_array.of_buffer (Int32_array.buffer int32)) 0
34+
35+
let caml_int64_bits_of_float (x : float) : Caml_int64.t =
36+
37+
let to_nat (x : int32) = x |> Int32.to_int |> Nativeint.of_int in
38+
39+
let u = Float64_array.create [| x |] in
40+
let int32 = Int32_array.of_buffer (Float64_array.buffer u) in
41+
{lo = to_nat (Int32_array.get int32 0) ; hi = to_nat (Int32_array.get int32 1) }
42+
43+
let caml_int32_float_of_bits (x : int32) =
44+
let int32 = Int32_array.create [| x |] in
45+
let float32 = Float32_array.of_buffer ( Int32_array.buffer int32) in
46+
Float32_array.get float32 0
47+
48+
let caml_int32_bits_of_float (x : float) =
49+
let float32 = Float32_array.create [|x|] in
50+
Int32_array.get (Int32_array.of_buffer (Float32_array.buffer float32)) 0
51+
4152

42-
external caml_int64_bits_of_float : float -> Int64.t = "$$caml_int64_bits_of_float"
43-
[@@js.call ] [@@js.local]
4453

45-
external is_finite : float -> bool = ""
46-
[@@js.call "isFinite"]
54+
external is_finite : float -> bool = "isFinite"
55+
[@@js.call ]
4756

48-
external is_nan : float -> bool = ""
49-
[@@js.call "isNaN"]
57+
external is_nan : float -> bool = "isNaN"
58+
[@@js.call ]
5059

5160
let caml_classify_float x : fpclass =
5261
if is_finite x then

jscomp/runtime/caml_float.mli

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11

22

3-
val caml_int64_bits_of_float : float -> Int64.t
4-
val caml_int64_float_of_bits : Int64.t -> float
3+
4+
5+
val caml_int64_float_of_bits : Caml_int64.t -> float
6+
val caml_int32_float_of_bits : int32 -> float
7+
8+
val caml_int64_bits_of_float : float -> Caml_int64.t
9+
val caml_int32_bits_of_float : float -> int32
10+
511
val caml_classify_float : float -> fpclass
612
val caml_modf_float : float -> float * float
713
val caml_ldexp_float : float -> int -> float

jscomp/runtime/runtime.mllib

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ caml_lexer
1414
caml_parser
1515
caml_primitive
1616
caml_format
17-
caml_bigarray
17+
caml_bigarray
18+
19+
typed_array

jscomp/runtime/typed_array.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export var Int32_array: any ;
2+
export var Float64_array: any ;
3+
export var Float32_array: any ;
4+

jscomp/runtime/typed_array.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Generated CODE, PLEASE EDIT WITH CARE
2+
'use strict';
3+
4+
5+
var Int32_array = /* module */[];
6+
7+
var Float64_array = /* module */[];
8+
9+
var Float32_array = /* module */[];
10+
11+
exports.Int32_array = Int32_array;
12+
exports.Float64_array = Float64_array;
13+
exports.Float32_array = Float32_array;
14+
/* No side effect */

jscomp/runtime/typed_array.ml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
type buffer
3+
4+
5+
6+
7+
module Int32_array = struct
8+
type t
9+
external buffer : t -> buffer = "buffer" [@@js.get]
10+
external get : t -> int -> int32 = "" [@@js.get_index]
11+
external set : t -> int -> int32 -> unit = "" [@@js.set_index]
12+
external create : int32 array -> t = "Int32Array" [@@js.new]
13+
external of_buffer : buffer -> t = "Int32Array" [@@js.new]
14+
end
15+
16+
module Float64_array = struct
17+
type t
18+
external buffer : t -> buffer = "buffer" [@@js.get]
19+
external get : t -> int -> float = "" [@@js.get_index]
20+
external set : t -> int -> float -> unit = "" [@@js.set_index]
21+
external create : float array -> t = "Float64Array" [@@js.new]
22+
external of_buffer : buffer -> t = "Float64Array" [@@js.new]
23+
end
24+
25+
26+
(*
27+
it still return number, [float] in this case
28+
*)
29+
module Float32_array = struct
30+
type t
31+
external buffer : t -> buffer = "buffer" [@@js.get]
32+
external get : t -> int -> float = "" [@@js.get_index]
33+
external set : t -> int -> float -> unit = "" [@@js.set_index]
34+
external create : float array -> t = "Float32Array" [@@js.new]
35+
external of_buffer : buffer -> t = "Float32Array" [@@js.new]
36+
end

jscomp/test/.depend

+8-4
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,10 @@ ffi_test.cmo : ../lib/js.cmi
116116
ffi_test.cmx : ../lib/js.cmx
117117
fib.cmo :
118118
fib.cmx :
119-
float_of_bits_test.cmo : mt.cmi ../stdlib/int64.cmi
120-
float_of_bits_test.cmx : mt.cmx ../stdlib/int64.cmx
119+
float_of_bits_test.cmo : ../stdlib/printf.cmi mt.cmi ../stdlib/list.cmi \
120+
../stdlib/int64.cmi ../stdlib/int32.cmi ../stdlib/array.cmi
121+
float_of_bits_test.cmx : ../stdlib/printf.cmx mt.cmx ../stdlib/list.cmx \
122+
../stdlib/int64.cmx ../stdlib/int32.cmx ../stdlib/array.cmx
121123
float_test.cmo : mt.cmi ../stdlib/int64.cmi
122124
float_test.cmx : mt.cmx ../stdlib/int64.cmx
123125
for_loop_test.cmo : mt.cmi ../stdlib/list.cmi ../stdlib/array.cmi
@@ -572,8 +574,10 @@ ffi_test.cmo : ../lib/js.cmi
572574
ffi_test.cmj : ../lib/js.cmj
573575
fib.cmo :
574576
fib.cmj :
575-
float_of_bits_test.cmo : mt.cmi ../stdlib/int64.cmi
576-
float_of_bits_test.cmj : mt.cmj ../stdlib/int64.cmj
577+
float_of_bits_test.cmo : ../stdlib/printf.cmi mt.cmi ../stdlib/list.cmi \
578+
../stdlib/int64.cmi ../stdlib/int32.cmi ../stdlib/array.cmi
579+
float_of_bits_test.cmj : ../stdlib/printf.cmj mt.cmj ../stdlib/list.cmj \
580+
../stdlib/int64.cmj ../stdlib/int32.cmj ../stdlib/array.cmj
577581
float_test.cmo : mt.cmi ../stdlib/int64.cmi
578582
float_test.cmj : mt.cmj ../stdlib/int64.cmj
579583
for_loop_test.cmo : mt.cmi ../stdlib/list.cmi ../stdlib/array.cmi

jscomp/test/float_of_bits_test.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export var one_float: any ;
2+
export var int32_pairs: any ;
3+
export var from_pairs: (pair : any) => any ;
24
export var suites: any ;
35

0 commit comments

Comments
 (0)