This commit is contained in:
swrup 2025-11-11 02:07:51 +01:00
parent aa2ff7b2f0
commit 2f3113f55d
11742 changed files with 1223940 additions and 0 deletions

View file

@ -0,0 +1,13 @@
(library
(name ocaml_intrinsics_kernel_test)
(preprocessor_deps config.h)
(libraries ocaml_intrinsics_kernel ppx_bench.runtime-lib ppx_expect.runtime
ppx_module_timer.runtime)
(preprocess
(pps ppx_jane ppx_optcomp)))
(rule
(targets config.h)
(deps)
(action
(bash "cp %{lib:jst-config:config.h} .")))

View file

@ -0,0 +1,195 @@
[%%import "config.h"]
open Base
open Stdio
module I = Ocaml_intrinsics_kernel
let test ~op ~op_name ~to_string x = printf "%s %s = %d\n" op_name (to_string x) (op x)
let%expect_test "clz int64" =
let open Int64 in
let numbers =
[ 0L (* Int.num_bits *)
; 1L (* Int.num_bits - 1 *)
; 7L (* Int.num_bits - 3 *)
; max_value
; min_value
; -1L
]
in
let f =
test ~op:I.Int64.count_leading_zeros ~op_name:"clz" ~to_string:Hex.to_string_hum
in
List.iter ~f numbers;
[%expect
{|
clz 0x0 = 64
clz 0x1 = 63
clz 0x7 = 61
clz 0x7fff_ffff_ffff_ffff = 1
clz -0x8000_0000_0000_0000 = 0
clz -0x1 = 0
|}]
;;
let%expect_test "clz int32" =
let open Int32 in
let numbers =
[ 0l (* Int.num_bits *)
; 1l (* Int.num_bits - 1 *)
; 7l (* Int.num_bits - 3 *)
; max_value
; min_value
; -1l
]
in
let f =
test ~op:I.Int32.count_leading_zeros ~op_name:"clz" ~to_string:Hex.to_string_hum
in
List.iter ~f numbers;
[%expect
{|
clz 0x0 = 32
clz 0x1 = 31
clz 0x7 = 29
clz 0x7fff_ffff = 1
clz -0x8000_0000 = 0
clz -0x1 = 0
|}]
;;
[%%ifdef JSC_ARCH_SIXTYFOUR]
let%expect_test "clz int" =
let open Int in
let numbers =
[ 0 (* Int.num_bits *)
; 1 (* Int.num_bits - 1 *)
; 7 (* Int.num_bits - 3 *)
; max_value
; min_value
; -1
]
in
let f =
test ~op:I.Int.count_leading_zeros ~op_name:"clz" ~to_string:Hex.to_string_hum
in
List.iter ~f numbers;
[%expect
{|
clz 0x0 = 63
clz 0x1 = 62
clz 0x7 = 60
clz 0x3fff_ffff_ffff_ffff = 1
clz -0x4000_0000_0000_0000 = 0
clz -0x1 = 0
|}];
let f =
test ~op:I.Int.count_leading_zeros2 ~op_name:"clz2" ~to_string:Hex.to_string_hum
in
List.iter ~f numbers;
[%expect
{|
clz2 0x0 = 63
clz2 0x1 = 62
clz2 0x7 = 60
clz2 0x3fff_ffff_ffff_ffff = 1
clz2 -0x4000_0000_0000_0000 = 0
clz2 -0x1 = 0
|}]
;;
let%expect_test "clz nativeint" =
let open Nativeint in
let numbers =
[ 0n (* Int.num_bits *)
; 1n (* Int.num_bits - 1 *)
; 7n (* Int.num_bits - 3 *)
; max_value
; min_value
; -1n
]
in
let f =
test ~op:I.Nativeint.count_leading_zeros ~op_name:"clz" ~to_string:Hex.to_string_hum
in
List.iter ~f numbers;
[%expect
{|
clz 0x0 = 64
clz 0x1 = 63
clz 0x7 = 61
clz 0x7fff_ffff_ffff_ffff = 1
clz -0x8000_0000_0000_0000 = 0
clz -0x1 = 0
|}]
;;
[%%else]
let%expect_test "clz int" =
let open Int in
let numbers =
[ 0 (* Int.num_bits *)
; 1 (* Int.num_bits - 1 *)
; 7 (* Int.num_bits - 3 *)
; max_value
; min_value
; -1
]
in
let f =
test ~op:I.Int.count_leading_zeros ~op_name:"clz" ~to_string:Hex.to_string_hum
in
List.iter ~f numbers;
[%expect
{|
clz 0x0 = 31
clz 0x1 = 30
clz 0x7 = 28
clz 0x3fff_ffff = 1
clz -0x4000_0000 = 0
clz -0x1 = 0
|}];
let f =
test ~op:I.Int.count_leading_zeros2 ~op_name:"clz2" ~to_string:Hex.to_string_hum
in
List.iter ~f numbers;
[%expect
{|
clz2 0x0 = 31
clz2 0x1 = 30
clz2 0x7 = 28
clz2 0x3fff_ffff = 1
clz2 -0x4000_0000 = 0
clz2 -0x1 = 0
|}]
;;
let%expect_test "clz nativeint" =
let open Nativeint in
let numbers =
[ 0n (* Int.num_bits *)
; 1n (* Int.num_bits - 1 *)
; 7n (* Int.num_bits - 3 *)
; max_value
; min_value
; -1n
]
in
let f =
test ~op:I.Nativeint.count_leading_zeros ~op_name:"clz" ~to_string:Hex.to_string_hum
in
List.iter ~f numbers;
[%expect
{|
clz 0x0 = 32
clz 0x1 = 31
clz 0x7 = 29
clz 0x7fff_ffff = 1
clz -0x8000_0000 = 0
clz -0x1 = 0
|}]
;;
[%%endif]

View file

@ -0,0 +1 @@
(*_ This signature is deliberately empty. *)

View file

@ -0,0 +1,197 @@
open Base
open Stdio
module I = Ocaml_intrinsics_kernel.Conditional
let%expect_test "csel int" =
let inputs = [ 0; 1; 4; 6; 5 ] in
List.iter inputs ~f:(fun a ->
let expect = if a % 2 = 0 then a else a + 1 in
let actual = I.select_value (a % 2 = 0) a (a + 1) in
printf "%d %d\n" expect actual);
[%expect {|
0 0
2 2
4 4
6 6
6 6
|}]
;;
let%expect_test "csel max int value" =
let inputs = [ 0, 1; 4, 5 ] in
List.iter inputs ~f:(fun (a, b) ->
let expect = if a > b then a else b in
let actual = I.select_value (a > b) a b in
printf "%d %d\n" expect actual);
[%expect {|
1 1
5 5
|}]
;;
let%expect_test "csel max float value" =
let inputs = [ 0.5, Float.neg_infinity; 0.0, 0.1; Float.nan, 5.0 ] in
List.iter inputs ~f:(fun (a, b) ->
let expect = if Float.(a > b) then a else b in
let actual = I.select_value Float.(a > b) a b in
printf "%f %f\n" expect actual);
[%expect {|
0.500000 0.500000
0.100000 0.100000
5.000000 5.000000
|}]
;;
let%expect_test "csel max int untagged" =
let inputs = [ 0, 1; 4, 5 ] in
List.iter inputs ~f:(fun (a, b) ->
let expect = if a > b then a else b in
let actual = I.select_int (a > b) a b in
printf "%d %d\n" expect actual);
[%expect {|
1 1
5 5
|}]
;;
let%expect_test "csel max int64 unboxed" =
let inputs = [ 0L, 1L; 4L, 5L; Int64.max_value, Int64.min_value ] in
List.iter inputs ~f:(fun (a, b) ->
let expect = if Int64.(a > b) then a else b in
let actual = I.select_int64 Int64.(a > b) a b in
printf "%Ld %Ld\n" expect actual);
[%expect {|
1 1
5 5
9223372036854775807 9223372036854775807
|}]
;;
let%expect_test "csel max int32 unboxed" =
let inputs = [ 0l, 1l; 4l, 5l; Int32.max_value, Int32.min_value ] in
List.iter inputs ~f:(fun (a, b) ->
let expect = if Int32.(a > b) then a else b in
let actual = I.select_int32 Int32.(a > b) a b in
printf "%ld %ld\n" expect actual);
[%expect {|
1 1
5 5
2147483647 2147483647
|}]
;;
[%%import "config.h"]
[%%ifdef JSC_ARCH_SIXTYFOUR]
let%expect_test "csel max nativeint unboxed" =
let inputs = [ 0n, 1n; 4n, 5n; Nativeint.max_value, Nativeint.min_value ] in
List.iter inputs ~f:(fun (a, b) ->
let expect = if Nativeint.(a > b) then a else b in
let actual = I.select_nativeint Nativeint.(a > b) a b in
printf "%nd %nd\n" expect actual);
[%expect {|
1 1
5 5
9223372036854775807 9223372036854775807
|}]
;;
[%%else]
let%expect_test "csel max nativeint unboxed" =
let inputs = [ 0n, 1n; 4n, 5n; Nativeint.max_value, Nativeint.min_value ] in
List.iter inputs ~f:(fun (a, b) ->
let expect = if Nativeint.(a > b) then a else b in
let actual = I.select_nativeint Nativeint.(a > b) a b in
printf "%nd %nd\n" expect actual);
[%expect {|
1 1
5 5
2147483647 2147483647
|}]
;;
[%%endif]
let%expect_test "csel sideffects" =
let inputs = [ 0, 1; 5, 4 ] in
List.iter inputs ~f:(fun (a, b) ->
let expect =
if a > b
then (
printf "hello 0\n";
a)
else (
printf "world 0\n";
b)
in
let actual =
I.select_value
(a > b)
(printf "hello 1\n";
a)
(printf "world 1\n";
b)
in
printf "%d %d\n" expect actual);
[%expect
{|
world 0
world 1
hello 1
1 1
hello 0
world 1
hello 1
5 5
|}]
;;
let%expect_test "min extra moves" =
(* Currently [min] emits extra moves:
*
* actual:
*
* camlT__min_266:
* movq %rax, %rdi
* movq %rbx, %rax
* cmpq %rax, %rdi
* cmovl %rdi, %rax
* ret
*
* [min2] is
*
* camlT__min2_273:
* cmpq %rax, %rbx
* cmovl %rbx, %rax
* ret
* ret *)
let[@inline never] min (x : int) (y : int) = I.select_value (x < y) x y in
let[@inline never] min2 (x : int) (y : int) = I.select_value (y < x) y x in
let inputs = [ 0, 1; 5, 4 ] in
List.iter inputs ~f:(fun (a, b) ->
printf "%d " (min a b);
printf "%d\n" (min2 a b));
[%expect {|
0 0
4 4
|}]
;;
let%expect_test "float deadcode" =
(* Currently [nop_float] emits extra loads, because there is no dead code elimination
* after register allocation:
*
* camlT__nop_float_292:
* movsd (%rbx), %xmm0
* movsd (%rax), %xmm1
* ret *)
let[@inline never] nop_float (x : float) (y : float) : float =
I.select_value Float.(x > y) x x
in
let inputs =
[ 0.5, Float.neg_infinity; 0.0, 0.1; Float.nan, 5.0; Float.infinity, -0.0 ]
in
List.iter inputs ~f:(fun (a, b) -> printf "%f " (nop_float a b));
[%expect {| 0.500000 0.000000 nan inf |}]
;;

View file

@ -0,0 +1 @@
(* blank *)

View file

@ -0,0 +1,144 @@
[%%import "config.h"]
open Base
open Stdio
module I = Ocaml_intrinsics_kernel
let test ~op ~op_name ~to_string x = printf "%s %s = %d\n" op_name (to_string x) (op x)
let numbers = [ 0 (* Int.num_bits *); 1; 7; 2; 4; 12; 18; -1 ]
let%expect_test "ctz int64" =
let open Int64 in
let numbers = List.map numbers ~f:of_int in
let f =
test ~op:I.Int64.count_trailing_zeros ~op_name:"ctz" ~to_string:Hex.to_string_hum
in
List.iter ~f (max_value :: min_value :: numbers);
[%expect
{|
ctz 0x7fff_ffff_ffff_ffff = 0
ctz -0x8000_0000_0000_0000 = 63
ctz 0x0 = 64
ctz 0x1 = 0
ctz 0x7 = 0
ctz 0x2 = 1
ctz 0x4 = 2
ctz 0xc = 2
ctz 0x12 = 1
ctz -0x1 = 0
|}]
;;
let%expect_test "ctz int32" =
let open Int32 in
let numbers = List.map numbers ~f:of_int_trunc in
let f =
test ~op:I.Int32.count_trailing_zeros ~op_name:"ctz" ~to_string:Hex.to_string_hum
in
List.iter ~f (max_value :: min_value :: numbers);
[%expect
{|
ctz 0x7fff_ffff = 0
ctz -0x8000_0000 = 31
ctz 0x0 = 32
ctz 0x1 = 0
ctz 0x7 = 0
ctz 0x2 = 1
ctz 0x4 = 2
ctz 0xc = 2
ctz 0x12 = 1
ctz -0x1 = 0
|}]
;;
[%%ifdef JSC_ARCH_SIXTYFOUR]
let%expect_test "ctz int" =
let open Int in
let f =
test ~op:I.Int.count_trailing_zeros ~op_name:"ctz" ~to_string:Hex.to_string_hum
in
List.iter ~f (max_value :: min_value :: numbers);
[%expect
{|
ctz 0x3fff_ffff_ffff_ffff = 0
ctz -0x4000_0000_0000_0000 = 62
ctz 0x0 = 63
ctz 0x1 = 0
ctz 0x7 = 0
ctz 0x2 = 1
ctz 0x4 = 2
ctz 0xc = 2
ctz 0x12 = 1
ctz -0x1 = 0
|}]
;;
let%expect_test "ctz nativeint" =
let open Nativeint in
let numbers = List.map numbers ~f:of_int in
let f =
test ~op:I.Nativeint.count_trailing_zeros ~op_name:"ctz" ~to_string:Hex.to_string_hum
in
List.iter ~f (max_value :: min_value :: numbers);
[%expect
{|
ctz 0x7fff_ffff_ffff_ffff = 0
ctz -0x8000_0000_0000_0000 = 63
ctz 0x0 = 64
ctz 0x1 = 0
ctz 0x7 = 0
ctz 0x2 = 1
ctz 0x4 = 2
ctz 0xc = 2
ctz 0x12 = 1
ctz -0x1 = 0
|}]
;;
[%%else]
let%expect_test "ctz int" =
let open Int in
let f =
test ~op:I.Int.count_trailing_zeros ~op_name:"ctz" ~to_string:Hex.to_string_hum
in
List.iter ~f (max_value :: min_value :: numbers);
[%expect
{|
ctz 0x3fff_ffff = 0
ctz -0x4000_0000 = 30
ctz 0x0 = 31
ctz 0x1 = 0
ctz 0x7 = 0
ctz 0x2 = 1
ctz 0x4 = 2
ctz 0xc = 2
ctz 0x12 = 1
ctz -0x1 = 0
|}]
;;
let%expect_test "ctz nativeint" =
let open Nativeint in
let numbers = List.map numbers ~f:of_int in
let f =
test ~op:I.Nativeint.count_trailing_zeros ~op_name:"ctz" ~to_string:Hex.to_string_hum
in
List.iter ~f (max_value :: min_value :: numbers);
[%expect
{|
ctz 0x7fff_ffff = 0
ctz -0x8000_0000 = 31
ctz 0x0 = 32
ctz 0x1 = 0
ctz 0x7 = 0
ctz 0x2 = 1
ctz 0x4 = 2
ctz 0xc = 2
ctz 0x12 = 1
ctz -0x1 = 0
|}]
;;
[%%endif]

View file

@ -0,0 +1 @@
(*_ This signature is deliberately empty. *)

View file

@ -0,0 +1,41 @@
open! Base
open! Stdio
module I = Ocaml_intrinsics_kernel.Float
let%expect_test "min and max" =
let args =
[ 0., 1.
; 1., 0.
; Float.neg_infinity, Float.infinity
; Float.infinity, Float.neg_infinity
; -0., 0.
; 0., -0.
; Float.nan, 0.
; 0., Float.nan
]
in
List.iter args ~f:(fun (x, y) -> printf "min %.19g %.19g = %.19g\n" x y (I.min x y));
[%expect
{|
min 0 1 = 0
min 1 0 = 0
min -inf inf = -inf
min inf -inf = -inf
min -0 0 = 0
min 0 -0 = -0
min nan 0 = 0
min 0 nan = nan
|}];
List.iter args ~f:(fun (x, y) -> printf "max %.19g %.19g = %.19g\n" x y (I.max x y));
[%expect
{|
max 0 1 = 1
max 1 0 = 1
max -inf inf = inf
max inf -inf = inf
max -0 0 = 0
max 0 -0 = -0
max nan 0 = 0
max 0 nan = nan
|}]
;;

View file

@ -0,0 +1 @@
(*_ This signature is deliberately empty. *)

View file

@ -0,0 +1,159 @@
[%%import "config.h"]
open Base
open Stdio
module I = Ocaml_intrinsics_kernel
let test ~op ~op_name ~to_string x = printf "%s %s = %d\n" op_name (to_string x) (op x)
let%expect_test "popcnt int64" =
let open Int64 in
let numbers = [ 0L; 1L; 7L; max_value; min_value; -1L ] in
let f =
test ~op:I.Int64.count_set_bits ~op_name:"popcnt" ~to_string:Hex.to_string_hum
in
List.iter ~f numbers;
[%expect
{|
popcnt 0x0 = 0
popcnt 0x1 = 1
popcnt 0x7 = 3
popcnt 0x7fff_ffff_ffff_ffff = 63
popcnt -0x8000_0000_0000_0000 = 1
popcnt -0x1 = 64
|}]
;;
let%expect_test "popcnt int32" =
let open Int32 in
let numbers = [ 0l; 1l; 7l; max_value; min_value; -1l ] in
let f =
test ~op:I.Int32.count_set_bits ~op_name:"popcnt" ~to_string:Hex.to_string_hum
in
List.iter ~f numbers;
[%expect
{|
popcnt 0x0 = 0
popcnt 0x1 = 1
popcnt 0x7 = 3
popcnt 0x7fff_ffff = 31
popcnt -0x8000_0000 = 1
popcnt -0x1 = 32
|}]
;;
[%%ifdef JSC_ARCH_SIXTYFOUR]
let%expect_test "popcnt int" =
let open Int in
let numbers =
[ 0
; 1
; 7
; max_value (* Int.num_bits - 1 *)
; min_value (* 1 *)
; -1 (* Int.num_bits *)
]
in
let f = test ~op:I.Int.count_set_bits ~op_name:"popcnt" ~to_string:Hex.to_string_hum in
List.iter ~f numbers;
[%expect
{|
popcnt 0x0 = 0
popcnt 0x1 = 1
popcnt 0x7 = 3
popcnt 0x3fff_ffff_ffff_ffff = 62
popcnt -0x4000_0000_0000_0000 = 1
popcnt -0x1 = 63
|}];
let f =
test ~op:I.Int.count_set_bits2 ~op_name:"popcnt2" ~to_string:Hex.to_string_hum
in
List.iter ~f numbers;
[%expect
{|
popcnt2 0x0 = 0
popcnt2 0x1 = 1
popcnt2 0x7 = 3
popcnt2 0x3fff_ffff_ffff_ffff = 62
popcnt2 -0x4000_0000_0000_0000 = 1
popcnt2 -0x1 = 63
|}]
;;
let%expect_test "popcnt nativeint" =
let open Nativeint in
let numbers = [ 0n; 1n; 7n; max_value; min_value; -1n ] in
let f =
test ~op:I.Nativeint.count_set_bits ~op_name:"popcnt" ~to_string:Hex.to_string_hum
in
List.iter ~f numbers;
[%expect
{|
popcnt 0x0 = 0
popcnt 0x1 = 1
popcnt 0x7 = 3
popcnt 0x7fff_ffff_ffff_ffff = 63
popcnt -0x8000_0000_0000_0000 = 1
popcnt -0x1 = 64
|}]
;;
[%%else]
let%expect_test "popcnt int" =
let open Int in
let numbers =
[ 0
; 1
; 7
; max_value (* Int.num_bits - 1 *)
; min_value (* 1 *)
; -1 (* Int.num_bits *)
]
in
let f = test ~op:I.Int.count_set_bits ~op_name:"popcnt" ~to_string:Hex.to_string_hum in
List.iter ~f numbers;
[%expect
{|
popcnt 0x0 = 0
popcnt 0x1 = 1
popcnt 0x7 = 3
popcnt 0x3fff_ffff = 30
popcnt -0x4000_0000 = 1
popcnt -0x1 = 31
|}];
let f =
test ~op:I.Int.count_set_bits2 ~op_name:"popcnt2" ~to_string:Hex.to_string_hum
in
List.iter ~f numbers;
[%expect
{|
popcnt2 0x0 = 0
popcnt2 0x1 = 1
popcnt2 0x7 = 3
popcnt2 0x3fff_ffff = 30
popcnt2 -0x4000_0000 = 1
popcnt2 -0x1 = 31
|}]
;;
let%expect_test "popcnt nativeint" =
let open Nativeint in
let numbers = [ 0n; 1n; 7n; max_value; min_value; -1n ] in
let f =
test ~op:I.Nativeint.count_set_bits ~op_name:"popcnt" ~to_string:Hex.to_string_hum
in
List.iter ~f numbers;
[%expect
{|
popcnt 0x0 = 0
popcnt 0x1 = 1
popcnt 0x7 = 3
popcnt 0x7fff_ffff = 31
popcnt -0x8000_0000 = 1
popcnt -0x1 = 32
|}]
;;
[%%endif]

View file

@ -0,0 +1 @@
(*_ This signature is deliberately empty. *)