Skip to content

Commit

Permalink
float16
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Jan 17, 2025
1 parent 3ab3b05 commit 7a67757
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/tests-ocaml/lib-bigarray/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(tests
(names bigarrays change_layout fftba pr5115 weak_bigarray)
(names bigarrays change_layout fftba pr5115 weak_bigarray specialized)
(libraries)
(modes js wasm))
91 changes: 91 additions & 0 deletions compiler/tests-ocaml/lib-bigarray/specialized.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
(* TEST *)

open Bigarray

(* Check that type-specialized accesses produce the same results
as generic accesses *)

let generic (a: ('a, 'b, 'c) Array1.t) v0 v1 v2 =
a.{0} <- v0; a.{1} <- v1; a.{2} <- v2;
(a.{0}, a.{1}, a.{2})

let special_float16 (a: (float, float16_elt, c_layout) Array1.t) v0 v1 v2 =
a.{0} <- v0; a.{1} <- v1; a.{2} <- v2;
(a.{0}, a.{1}, a.{2})

let special_float32 (a: (float, float32_elt, c_layout) Array1.t) v0 v1 v2 =
a.{0} <- v0; a.{1} <- v1; a.{2} <- v2;
(a.{0}, a.{1}, a.{2})

let special_float64 (a: (float, float64_elt, c_layout) Array1.t) v0 v1 v2 =
a.{0} <- v0; a.{1} <- v1; a.{2} <- v2;
(a.{0}, a.{1}, a.{2})

let special_int8s (a: (int, int8_signed_elt, c_layout) Array1.t) v0 v1 v2 =
a.{0} <- v0; a.{1} <- v1; a.{2} <- v2;
(a.{0}, a.{1}, a.{2})

let special_int8u (a: (int, int8_unsigned_elt, c_layout) Array1.t) v0 v1 v2 =
a.{0} <- v0; a.{1} <- v1; a.{2} <- v2;
(a.{0}, a.{1}, a.{2})

let special_int16s (a: (int, int16_signed_elt, c_layout) Array1.t) v0 v1 v2 =
a.{0} <- v0; a.{1} <- v1; a.{2} <- v2;
(a.{0}, a.{1}, a.{2})

let special_int16u (a: (int, int16_unsigned_elt, c_layout) Array1.t) v0 v1 v2 =
a.{0} <- v0; a.{1} <- v1; a.{2} <- v2;
(a.{0}, a.{1}, a.{2})

let special_int32 (a: (int32, int32_elt, c_layout) Array1.t) v0 v1 v2 =
a.{0} <- v0; a.{1} <- v1; a.{2} <- v2;
(a.{0}, a.{1}, a.{2})

let special_int64 (a: (int64, int64_elt, c_layout) Array1.t) v0 v1 v2 =
a.{0} <- v0; a.{1} <- v1; a.{2} <- v2;
(a.{0}, a.{1}, a.{2})

let special_int (a: (int, int_elt, c_layout) Array1.t) v0 v1 v2 =
a.{0} <- v0; a.{1} <- v1; a.{2} <- v2;
(a.{0}, a.{1}, a.{2})

let special_nativeint (a: (nativeint, nativeint_elt, c_layout) Array1.t)
v0 v1 v2 =
a.{0} <- v0; a.{1} <- v1; a.{2} <- v2;
(a.{0}, a.{1}, a.{2})

let special_complex32 (a: (Complex.t, complex32_elt, c_layout) Array1.t)
v0 v1 v2 =
a.{0} <- v0; a.{1} <- v1; a.{2} <- v2;
(a.{0}, a.{1}, a.{2})

let special_complex64 (a: (Complex.t, complex64_elt, c_layout) Array1.t)
v0 v1 v2 =
a.{0} <- v0; a.{1} <- v1; a.{2} <- v2;
(a.{0}, a.{1}, a.{2})

let special_char (a: (char, int8_unsigned_elt, c_layout) Array1.t) v0 v1 v2 =
a.{0} <- v0; a.{1} <- v1; a.{2} <- v2;
(a.{0}, a.{1}, a.{2})

let test kind special v0 v1 v2 =
let a = Array1.create kind c_layout 3 in
let s = special a v0 v1 v2 in
let g = generic a v0 v1 v2 in
assert (s = g)

let _ =
(* test float16 special_float16 1.0 (-2.0) Float.pi; *)
test float32 special_float32 1.0 (-2.0) Float.pi;
test float64 special_float64 1.0 (-2.0) Float.pi;
test int8_signed special_int8s 123 (-456) 0xFF00FF;
test int8_unsigned special_int8u 123 (-456) 0xFF00FF;
test int16_signed special_int16s 123 (-456) 0xFF00FF;
test int16_unsigned special_int16u 123 (-456) 0xFF00FF;
test int32 special_int32 123l (-456l) (0x22334455l);
test int64 special_int64 123L (-456L) (0x2233445566778899L);
test int special_int 123 (-456) 0xFF00FF;
test nativeint special_nativeint 123n (-456n) (0x22334455n);
test complex32 special_complex32 Complex.zero Complex.one Complex.i;
test complex64 special_complex64 Complex.zero Complex.one Complex.i;
test char special_char 'A' '-' 'Z'
3 changes: 3 additions & 0 deletions runtime/js/bigarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ function caml_ba_create_buffer(kind, size) {
case 12:
view = Uint8Array;
break;
case 13:
view = globalThis.Float16Array;
break;
}
if (!view) caml_invalid_argument("Bigarray.create: unsupported kind");
var data = new view(size * caml_ba_get_size_per_element(kind));
Expand Down

0 comments on commit 7a67757

Please sign in to comment.