Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.

Commit a9ef3c9

Browse files
authored
V128 refactor SIMD to use submodule (#222)
* Refactor SIMD operators * Common submodule for different SIMD lane shapes * Tweak the submodule representation * Rename modules, move module into Make * MakeFloat takes a Float.S * Better name for MakeFloat parameter
1 parent c9976b5 commit a9ef3c9

File tree

5 files changed

+77
-53
lines changed

5 files changed

+77
-53
lines changed

interpreter/exec/eval_numeric.ml

+9-7
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,17 @@ struct
127127

128128
let unop op =
129129
fun v -> match op with
130-
| F32x4Abs -> to_value (SXX.f32x4_abs (of_value 1 v))
131-
| F64x2Abs -> to_value (SXX.f64x2_abs (of_value 1 v))
130+
| F32x4 Abs -> to_value (SXX.F32x4.abs (of_value 1 v))
131+
| F64x2 Abs -> to_value (SXX.F64x2.abs (of_value 1 v))
132+
| _ -> failwith "TODO v128 unimplemented unop"
132133

133134
let binop op =
134135
let f = match op with
135-
| F32x4Min -> SXX.f32x4_min
136-
| F32x4Max -> SXX.f32x4_max
137-
| F64x2Min -> SXX.f64x2_min
138-
| F64x2Max -> SXX.f64x2_max
136+
| F32x4 Min -> SXX.F32x4.min
137+
| F32x4 Max -> SXX.F32x4.max
138+
| F64x2 Min -> SXX.F64x2.min
139+
| F64x2 Max -> SXX.F64x2.max
140+
| _ -> failwith "TODO v128 unimplemented unop"
139141
in fun v1 v2 -> to_value (f (of_value 1 v1) (of_value 2 v2))
140142

141143
(* FIXME *)
@@ -147,7 +149,7 @@ struct
147149
let extractop op v =
148150
match op with
149151
| F32x4ExtractLane imm ->
150-
(F32Op.to_value (SXX.f32x4_extract_lane imm (of_value 1 v)))
152+
(F32Op.to_value (SXX.F32x4.extract_lane imm (of_value 1 v)))
151153
| I32x4ExtractLane imm ->
152154
(I32Op.to_value (SXX.i32x4_extract_lane imm (of_value 1 v)))
153155
end

interpreter/exec/simd.ml

+41-32
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ sig
3333
val of_f64x2 : F64.t list -> t
3434
end
3535

36+
(* This signature defines the types and operations SIMD floats can expose. *)
37+
module type Float =
38+
sig
39+
type t
40+
type lane
41+
42+
val abs : t -> t
43+
val min : t -> t -> t
44+
val max : t -> t -> t
45+
val extract_lane : int -> t -> lane
46+
end
47+
3648
module type S =
3749
sig
3850
type t
@@ -45,15 +57,10 @@ sig
4557

4658
val i32x4_extract_lane : int -> t -> I32.t
4759

48-
val f32x4_min : t -> t -> t
49-
val f32x4_max : t -> t -> t
50-
val f32x4_abs : t -> t
51-
val f32x4_extract_lane : int -> t -> F32.t
52-
53-
val f64x2_min : t -> t -> t
54-
val f64x2_max : t -> t -> t
55-
val f64x2_abs : t -> t
56-
val f64x2_extract_lane : int -> t -> F64.t
60+
(* We need type t = t to ensure that all submodule types are S.t,
61+
* then callers don't have to change *)
62+
module F32x4 : Float with type t = t and type lane = F32.t
63+
module F64x2 : Float with type t = t and type lane = F64.t
5764
end
5865

5966
module Make (Rep : RepType) : S with type bits = Rep.t =
@@ -71,27 +78,29 @@ struct
7178

7279
let i32x4_extract_lane i x = List.nth (to_i32x4 x) i
7380

74-
let to_f32x4 = Rep.to_f32x4
75-
let of_f32x4 = Rep.of_f32x4
76-
let f32x4_unop f x =
77-
of_f32x4 (List.map f (to_f32x4 x))
78-
let f32x4_binop f x y =
79-
of_f32x4 (List.map2 f (to_f32x4 x) (to_f32x4 y))
80-
81-
let f32x4_extract_lane i x = List.nth (to_f32x4 x) i
82-
let f32x4_min = f32x4_binop F32.min
83-
let f32x4_max = f32x4_binop F32.max
84-
let f32x4_abs x = f32x4_unop F32.abs x
85-
86-
let to_f64x2 = Rep.to_f64x2
87-
let of_f64x2 = Rep.of_f64x2
88-
let f64x2_unop f x =
89-
of_f64x2 (List.map f (to_f64x2 x))
90-
let f64x2_binop f x y =
91-
of_f64x2 (List.map2 f (to_f64x2 x) (to_f64x2 y))
92-
93-
let f64x2_extract_lane i x = List.nth (to_f64x2 x) i
94-
let f64x2_min = f64x2_binop F64.min
95-
let f64x2_max = f64x2_binop F64.max
96-
let f64x2_abs x = f64x2_unop F64.abs x
81+
module MakeFloat (Float : Float.S) (Convert : sig
82+
val to_shape : Rep.t -> Float.t list
83+
val of_shape : Float.t list -> Rep.t
84+
end) : Float with type t = Rep.t and type lane = Float.t =
85+
struct
86+
type t = Rep.t
87+
type lane = Float.t
88+
let unop f x = Convert.of_shape (List.map f (Convert.to_shape x))
89+
let binop f x y = Convert.of_shape (List.map2 f (Convert.to_shape x) (Convert.to_shape y))
90+
let abs = unop Float.abs
91+
let min = binop Float.min
92+
let max = binop Float.max
93+
let extract_lane i s = List.nth (Convert.to_shape s) i
94+
end
95+
96+
module F32x4 = MakeFloat (F32) (struct
97+
let to_shape = Rep.to_f32x4
98+
let of_shape = Rep.of_f32x4
99+
end)
100+
101+
module F64x2 = MakeFloat (F64) (struct
102+
let to_shape = Rep.to_f64x2
103+
let of_shape = Rep.of_f64x2
104+
end)
105+
97106
end

interpreter/script/run.ml

+6-6
Original file line numberDiff line numberDiff line change
@@ -376,16 +376,16 @@ let assert_result at got expect =
376376
let open Simd in
377377
match shape, v with
378378
| F32x4, V128 v ->
379-
let l0 = F32 (V128.f32x4_extract_lane 0 v) in
380-
let l1 = F32 (V128.f32x4_extract_lane 1 v) in
381-
let l2 = F32 (V128.f32x4_extract_lane 2 v) in
382-
let l3 = F32 (V128.f32x4_extract_lane 3 v) in
379+
let l0 = F32 (V128.F32x4.extract_lane 0 v) in
380+
let l1 = F32 (V128.F32x4.extract_lane 1 v) in
381+
let l2 = F32 (V128.F32x4.extract_lane 2 v) in
382+
let l3 = F32 (V128.F32x4.extract_lane 3 v) in
383383
List.exists2 (fun v r ->
384384
assert_num_pat at v r
385385
) [l0; l1; l2; l3] vs
386386
| F64x2, V128 v ->
387-
let l0 = F64 (V128.f64x2_extract_lane 0 v) in
388-
let l1 = F64 (V128.f64x2_extract_lane 1 v) in
387+
let l0 = F64 (V128.F64x2.extract_lane 0 v) in
388+
let l1 = F64 (V128.F64x2.extract_lane 1 v) in
389389
List.exists2 (fun v r ->
390390
assert_num_pat at v r
391391
) [l0; l1] vs

interpreter/syntax/ast.ml

+15-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,21 @@ end
4747
(* FIXME *)
4848
module SimdOp =
4949
struct
50-
type unop = F32x4Abs | F64x2Abs
51-
type binop = F32x4Min | F32x4Max | F64x2Min | F64x2Max
50+
type iunop = TodoIunop
51+
type ibinop = TodoIbinop
52+
type funop = Abs
53+
type fbinop = Min | Max
54+
55+
type ('i8x16, 'i16x8, 'i32x4, 'i64x2, 'f32x4, 'f64x2) v128op =
56+
| I8x16 of 'i8x16
57+
| I16x8 of 'i16x8
58+
| I32x4 of 'i32x4
59+
| I64x2 of 'i64x2
60+
| F32x4 of 'f32x4
61+
| F64x2 of 'f64x2
62+
63+
type unop = (iunop, iunop, iunop, iunop, funop, funop) v128op
64+
type binop = (ibinop, ibinop, ibinop, ibinop, fbinop, fbinop) v128op
5265
type testop = TodoTestOp
5366
type relop = TodoRelOp
5467
type cvtop = TodoCvtOp

interpreter/syntax/operators.ml

+6-6
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ let memory_grow = MemoryGrow
207207
let i32x4_extract_lane imm = ExtractLane (V128Op.I32x4ExtractLane imm)
208208

209209
let f32x4_extract_lane imm = ExtractLane (V128Op.F32x4ExtractLane imm)
210-
let f32x4_min = Binary (V128 V128Op.F32x4Min)
211-
let f32x4_max = Binary (V128 V128Op.F32x4Max)
212-
let f32x4_abs = Unary (V128 V128Op.F32x4Abs)
210+
let f32x4_min = Binary (V128 (V128Op.F32x4 V128Op.Min))
211+
let f32x4_max = Binary (V128 (V128Op.F32x4 V128Op.Max))
212+
let f32x4_abs = Unary (V128 (V128Op.F32x4 V128Op.Abs))
213213

214-
let f64x2_min = Binary (V128 V128Op.F64x2Min)
215-
let f64x2_max = Binary (V128 V128Op.F64x2Max)
216-
let f64x2_abs = Unary (V128 V128Op.F64x2Abs)
214+
let f64x2_min = Binary (V128 (V128Op.F64x2 V128Op.Min))
215+
let f64x2_max = Binary (V128 (V128Op.F64x2 V128Op.Max))
216+
let f64x2_abs = Unary (V128 (V128Op.F64x2 V128Op.Abs))

0 commit comments

Comments
 (0)