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

Commit dad9d0d

Browse files
authored
Implement v128.const parsing for f64x2 with nans (#219)
And enough operations to pass the simd_f64x2.wast test.
1 parent 32a11d8 commit dad9d0d

File tree

8 files changed

+53
-6
lines changed

8 files changed

+53
-6
lines changed

interpreter/exec/eval_numeric.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,14 @@ struct
128128
let unop op =
129129
fun v -> match op with
130130
| F32x4Abs -> to_value (SXX.f32x4_abs (of_value 1 v))
131+
| F64x2Abs -> to_value (SXX.f64x2_abs (of_value 1 v))
131132

132133
let binop op =
133134
let f = match op with
134135
| F32x4Min -> SXX.f32x4_min
135136
| F32x4Max -> SXX.f32x4_max
137+
| F64x2Min -> SXX.f64x2_min
138+
| F64x2Max -> SXX.f64x2_max
136139
in fun v1 v2 -> to_value (f (of_value 1 v1) (of_value 2 v2))
137140

138141
(* FIXME *)

interpreter/exec/simd.ml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ let lanes shape =
1212
| F64x2 -> 2
1313

1414
let f32x4_indices = [0; 4; 8; 12]
15+
let f64x2_indices = [0; 8]
1516

1617
module type RepType =
1718
sig
@@ -27,6 +28,9 @@ sig
2728

2829
val to_f32x4 : t -> F32.t list
2930
val of_f32x4 : F32.t list -> t
31+
32+
val to_f64x2 : t -> F64.t list
33+
val of_f64x2 : F64.t list -> t
3034
end
3135

3236
module type S =
@@ -45,6 +49,11 @@ sig
4549
val f32x4_max : t -> t -> t
4650
val f32x4_abs : t -> t
4751
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
4857
end
4958

5059
module Make (Rep : RepType) : S with type bits = Rep.t =
@@ -73,4 +82,16 @@ struct
7382
let f32x4_min = f32x4_binop F32.min
7483
let f32x4_max = f32x4_binop F32.max
7584
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
7697
end

interpreter/exec/v128.ml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@ include Simd.Make
1111
List.map (fun i -> F32.of_bits (Bytes.get_int32_le (Bytes.of_string s) i)) Simd.f32x4_indices
1212

1313
let of_f32x4 fs =
14-
let b = create bytewidth in
14+
let b = Bytes.create bytewidth in
1515
List.iter2 (fun i f -> Bytes.set_int32_le b i (F32.to_bits f)) Simd.f32x4_indices fs;
1616
Bytes.to_string b
1717

18+
let to_f64x2 s =
19+
List.map (fun i -> F64.of_bits (Bytes.get_int64_le (Bytes.of_string s) i)) Simd.f64x2_indices
20+
21+
let of_f64x2 fs =
22+
let b = Bytes.create bytewidth in
23+
List.iter2 (fun i f -> Bytes.set_int64_le b i (F64.to_bits f)) Simd.f64x2_indices fs;
24+
Bytes.to_string b
25+
1826
let of_strings shape ss =
1927
if List.length ss <> Simd.lanes shape then raise (Invalid_argument "wrong length");
2028
let range_check i32 min max at =

interpreter/script/run.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,12 @@ let assert_result at got expect =
383383
List.exists2 (fun v r ->
384384
assert_num_pat at v r
385385
) [l0; l1; l2; l3] vs
386+
| 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
389+
List.exists2 (fun v r ->
390+
assert_num_pat at v r
391+
) [l0; l1] vs
386392
| _ -> failwith "impossible"
387393
end
388394
) got expect

interpreter/syntax/ast.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ end
4747
(* FIXME *)
4848
module SimdOp =
4949
struct
50-
type unop = F32x4Abs
51-
type binop = F32x4Min | F32x4Max
50+
type unop = F32x4Abs | F64x2Abs
51+
type binop = F32x4Min | F32x4Max | F64x2Min | F64x2Max
5252
type testop = TodoTestOp
5353
type relop = TodoRelOp
5454
type cvtop = TodoCvtOp

interpreter/syntax/operators.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,7 @@ let f32x4_extract_lane imm = ExtractLane (V128Op.F32x4ExtractLane imm)
210210
let f32x4_min = Binary (V128 V128Op.F32x4Min)
211211
let f32x4_max = Binary (V128 V128Op.F32x4Max)
212212
let f32x4_abs = Unary (V128 V128Op.F32x4Abs)
213+
214+
let f64x2_min = Binary (V128 V128Op.F64x2Min)
215+
let f64x2_max = Binary (V128 V128Op.F64x2Max)
216+
let f64x2_abs = Unary (V128 V128Op.F64x2Abs)

interpreter/text/lexer.mll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,13 +382,13 @@ rule token = parse
382382

383383
| (simd_shape as s)".min"
384384
{ if s <> "f32x4" && s <> "f64x2" then error lexbuf "unknown operator";
385-
BINARY (simdop s unreachable unreachable unreachable unreachable f32x4_min unreachable) }
385+
BINARY (simdop s unreachable unreachable unreachable unreachable f32x4_min f64x2_min) }
386386
| (simd_shape as s)".max"
387387
{ if s <> "f32x4" && s <> "f64x2" then error lexbuf "unknown operator";
388-
BINARY (simdop s unreachable unreachable unreachable unreachable f32x4_max unreachable) }
388+
BINARY (simdop s unreachable unreachable unreachable unreachable f32x4_max f64x2_max) }
389389
| (simd_shape as s)".abs"
390390
{ if s = "i64x2" then error lexbuf "unknown operator";
391-
UNARY (simdop s unreachable unreachable unreachable unreachable f32x4_abs unreachable) }
391+
UNARY (simdop s unreachable unreachable unreachable unreachable f32x4_abs f64x2_abs) }
392392
| (simd_shape as s) { SIMD_SHAPE (simd_shape s) }
393393

394394
| name as s { VAR s }

interpreter/text/parser.mly

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,13 @@ result :
880880
| const { NumResult (LitPat $1 @@ at ()) @@ at () }
881881
| LPAR CONST NAN RPAR { NumResult (NanPat (nanop $2 ($3 @@ ati 3)) @@ ati 3) @@ at () }
882882
| LPAR V128_CONST SIMD_SHAPE numpat numpat numpat numpat RPAR {
883+
if ($3 <> Simd.F32x4) then error (ati 3) "invalid SIMD shape";
883884
SimdResult ($3, [$4 $3; $5 $3; $6 $3; $7 $3]) @@ at ()
884885
}
886+
| LPAR V128_CONST SIMD_SHAPE numpat numpat RPAR {
887+
if ($3 <> Simd.F64x2) then error (ati 3) "invalid SIMD shape";
888+
SimdResult ($3, [$4 $3; $5 $3]) @@ at ()
889+
}
885890
886891
result_list :
887892
| /* empty */ { [] }

0 commit comments

Comments
 (0)