Skip to content

Commit ff16bf6

Browse files
committed
OxCaml: expose float16 <-> float conversion primitives
1 parent f591e4f commit ff16bf6

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

compiler/lib-wasm/gc_target.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,7 @@ module Bigarray = struct
15641564
, fun x ->
15651565
let* conv =
15661566
register_import
1567-
~name:"caml_double_to_float16"
1567+
~name:"caml_float16_of_double"
15681568
(Fun { W.params = [ F64 ]; result = [ I32 ] })
15691569
in
15701570
let* x = x in

compiler/lib-wasm/generate.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ module Generate (Target : Target_sig.S) = struct
146146
; "caml_erf_float32_bytecode", (`Pure, [ Float32 ], Float32)
147147
; "caml_erfc_float32_bytecode", (`Pure, [ Float32 ], Float32)
148148
; "caml_float32_compare", (`Pure, [ Float32; Float32 ], Int Normalized)
149+
; "caml_float16_of_double", (`Pure, [ Float ], Int32)
150+
; "caml_double_of_float16", (`Pure, [ Int32 ], Float)
149151
];
150152
h
151153

runtime/js/bigarray.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ function caml_ba_get_size(dims) {
4242
return size;
4343
}
4444

45-
//Provides: caml_unpackFloat16
46-
var caml_unpackFloat16 = (function () {
45+
//Provides: caml_double_of_float16 pure
46+
var caml_double_of_float16 = (function () {
4747
var pow = Math.pow;
4848

4949
var EXP_MASK16 = 31; // 2 ** 5 - 1
@@ -72,8 +72,8 @@ var caml_unpackFloat16 = (function () {
7272
};
7373
})();
7474

75-
//Provides: caml_packFloat16
76-
var caml_packFloat16 = (function () {
75+
//Provides: caml_float16_of_double pure
76+
var caml_float16_of_double = (function () {
7777
const INVERSE_OF_EPSILON = 1 / Number.EPSILON;
7878

7979
function roundTiesToEven(num) {
@@ -252,7 +252,7 @@ var caml_ba_custom_name = "_bigarr02";
252252
//Provides: Ml_Bigarray
253253
//Requires: caml_array_bound_error, caml_invalid_argument, caml_ba_custom_name
254254
//Requires: caml_int64_create_lo_hi, caml_int64_hi32, caml_int64_lo32
255-
//Requires: caml_packFloat16, caml_unpackFloat16
255+
//Requires: caml_float16_of_double, caml_double_of_float16
256256
class Ml_Bigarray {
257257
constructor(kind, layout, dims, buffer) {
258258
this.kind = kind;
@@ -299,7 +299,7 @@ class Ml_Bigarray {
299299
var i = this.data[ofs * 2 + 1];
300300
return [254, r, i];
301301
case 13:
302-
return caml_unpackFloat16(this.data[ofs]);
302+
return caml_double_of_float16(this.data[ofs]);
303303
default:
304304
return this.data[ofs];
305305
}
@@ -319,7 +319,7 @@ class Ml_Bigarray {
319319
this.data[ofs * 2 + 1] = v[2];
320320
break;
321321
case 13:
322-
this.data[ofs] = caml_packFloat16(v);
322+
this.data[ofs] = caml_float16_of_double(v);
323323
break;
324324
default:
325325
this.data[ofs] = v;
@@ -356,7 +356,7 @@ class Ml_Bigarray {
356356
}
357357
break;
358358
case 13:
359-
this.data.fill(caml_packFloat16(v));
359+
this.data.fill(caml_float16_of_double(v));
360360
break;
361361
default:
362362
this.data.fill(v);
@@ -406,8 +406,8 @@ class Ml_Bigarray {
406406
break;
407407
case 13:
408408
for (var i = 0; i < this.data.length; i++) {
409-
var aa = caml_unpackFloat16(this.data[i]);
410-
var bb = caml_unpackFloat16(b.data[i]);
409+
var aa = caml_double_of_float16(this.data[i]);
410+
var bb = caml_double_of_float16(b.data[i]);
411411
if (aa < bb) return -1;
412412
if (aa > bb) return 1;
413413
}
@@ -769,7 +769,7 @@ function caml_ba_reshape(ba, vind) {
769769
//Provides: caml_ba_serialize
770770
//Requires: caml_int64_bits_of_float, caml_int64_to_bytes
771771
//Requires: caml_int32_bits_of_float
772-
//Requires: caml_packFloat16
772+
//Requires: caml_float16_of_double
773773
function caml_ba_serialize(writer, ba, sz) {
774774
writer.write(32, ba.dims.length);
775775
writer.write(32, ba.kind | (ba.layout << 8));
@@ -859,7 +859,7 @@ function caml_ba_serialize(writer, ba, sz) {
859859
//Requires: caml_int64_of_bytes, caml_int64_float_of_bits
860860
//Requires: caml_int32_float_of_bits
861861
//Requires: caml_ba_create_buffer
862-
//Requires: caml_unpackFloat16
862+
//Requires: caml_double_of_float16
863863
function caml_ba_deserialize(reader, sz, name) {
864864
var num_dims = reader.read32s();
865865
if (num_dims < 0 || num_dims > 16)
@@ -985,7 +985,7 @@ function caml_ba_create_from(data1, data2, _jstyp, kind, layout, dims) {
985985

986986
//Provides: caml_ba_hash const
987987
//Requires: caml_ba_get_size, caml_hash_mix_int, caml_hash_mix_float
988-
//Requires: caml_unpackFloat16, caml_hash_mix_float16, caml_hash_mix_float32
988+
//Requires: caml_double_of_float16, caml_hash_mix_float16, caml_hash_mix_float32
989989
function caml_ba_hash(ba) {
990990
var num_elts = caml_ba_get_size(ba.dims);
991991
var h = 0;

runtime/wasm/bigarray.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@
184184
(field $ba_kind i8) ;; kind
185185
(field $ba_layout i8)))) ;; layout
186186

187-
(func $double_to_float16 (export "caml_double_to_float16")
187+
(func $double_to_float16 (export "caml_float16_of_double")
188188
(param $f f64) (result i32)
189189
(local $x i32) (local $sign i32) (local $o i32)
190190
(local.set $x (i32.reinterpret_f32 (f32.demote_f64 (local.get $f))))

0 commit comments

Comments
 (0)