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

Commit ef343cb

Browse files
authored
[interpreter] Implement i8x16.popcnt (#451)
Small fix to the test generation script, the order of applying operators was incorrect. Includes a fix to popcnt implementation that is upstream (WebAssembly/spec#1286).
1 parent 634be58 commit ef343cb

File tree

12 files changed

+136
-23
lines changed

12 files changed

+136
-23
lines changed

interpreter/binary/decode.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ let simd_prefix s =
341341
| 0x78l -> i8x16_max_s
342342
| 0x79l -> i8x16_max_u
343343
| 0x7bl -> i8x16_avgr_u
344+
| 0x7cl -> i8x16_popcnt
344345
| 0x80l -> i16x8_abs
345346
| 0x81l -> i16x8_neg
346347
| 0x83l -> i16x8_all_true

interpreter/binary/encode.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ let encode m =
337337
| Unary (V128 V128Op.(V128 Not)) -> simd_op 0x4dl
338338
| Unary (V128 V128Op.(I8x16 Abs)) -> simd_op 0x60l
339339
| Unary (V128 V128Op.(I8x16 Neg)) -> simd_op 0x61l
340+
| Unary (V128 V128Op.(I8x16 Popcnt)) -> simd_op 0x7cl
340341
| Unary (V128 V128Op.(I16x8 Abs)) -> simd_op 0x80l
341342
| Unary (V128 V128Op.(I16x8 Neg)) -> simd_op 0x81l
342343
| Unary (V128 V128Op.(I16x8 WidenLowS)) -> simd_op 0x87l

interpreter/exec/eval_simd.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module SimdOp (SXX : Simd.S) (Value : ValueType with type t = SXX.t) = struct
1616
fun v -> match op with
1717
| I8x16 Neg -> to_value (SXX.I8x16.neg (of_value 1 v))
1818
| I8x16 Abs -> to_value (SXX.I8x16.abs (of_value 1 v))
19+
| I8x16 Popcnt -> to_value (SXX.I8x16.popcnt (of_value 1 v))
1920
| I16x8 Neg -> to_value (SXX.I16x8.neg (of_value 1 v))
2021
| I16x8 Abs -> to_value (SXX.I16x8.abs (of_value 1 v))
2122
| I16x8 WidenLowS -> to_value (SXX.I16x8_convert.widen_low_s (of_value 1 v))

interpreter/exec/int.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ struct
240240

241241
let popcnt x =
242242
let rec loop acc i n =
243-
if n = Rep.zero then
243+
if i = 0 then
244244
acc
245245
else
246246
let acc' = if and_ n Rep.one = Rep.one then acc + 1 else acc in

interpreter/exec/simd.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ sig
7171
val ge_u : t -> t -> t
7272
val abs : t -> t
7373
val neg : t -> t
74+
val popcnt : t -> t
7475
val add : t -> t -> t
7576
val sub : t -> t -> t
7677
val min_s : t -> t -> t
@@ -319,6 +320,7 @@ struct
319320
let ge_u = binop (cmp Int.ge_u)
320321
let abs = unop Int.abs
321322
let neg = unop Int.neg
323+
let popcnt = unop Int.popcnt
322324
let add = binop Int.add
323325
let sub = binop Int.sub
324326
let mul = binop Int.mul

interpreter/syntax/ast.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ module SimdOp =
5050
struct
5151
type iunop = Abs | Neg | TruncSatF32x4S | TruncSatF32x4U
5252
| WidenLowS | WidenLowU | WidenHighS | WidenHighU
53+
| Popcnt
5354
type ibinop = Add | Sub | MinS | MinU | MaxS | MaxU | Mul | AvgrU
5455
| Eq | Ne | LtS | LtU | LeS | LeU | GtS | GtU | GeS | GeU
5556
| Swizzle | Shuffle of int list | NarrowS | NarrowU

interpreter/syntax/operators.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ let i8x16_sub = Binary (V128 V128Op.(I8x16 Sub))
299299
let i8x16_sub_sat_s = Binary (V128 V128Op.(I8x16 SubSatS))
300300
let i8x16_sub_sat_u = Binary (V128 V128Op.(I8x16 SubSatU))
301301
let i8x16_abs = Unary (V128 V128Op.(I8x16 Abs))
302+
let i8x16_popcnt = Unary (V128 V128Op.(I8x16 Popcnt))
302303
let i8x16_min_s = Binary (V128 V128Op.(I8x16 MinS))
303304
let i8x16_min_u = Binary (V128 V128Op.(I8x16 MinU))
304305
let i8x16_max_s = Binary (V128 V128Op.(I8x16 MaxS))

interpreter/text/arrange.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ struct
202202
let unop xx (op : unop) = match op with
203203
| I8x16 Neg -> "i8x16.neg"
204204
| I8x16 Abs -> "i8x16.abs"
205+
| I8x16 Popcnt -> "i8x16.popcnt"
205206
| I16x8 Abs -> "i16x8.abs"
206207
| I16x8 Neg -> "i16x8.neg"
207208
| I16x8 WidenLowS -> "i16x8.widen_low_i8x16_s"

interpreter/text/lexer.mll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,8 @@ rule token = parse
532532
| (simd_shape as s)".abs"
533533
{ only ["i8x16"; "i16x8"; "i32x4"; "f32x4"; "f64x2"] s lexbuf;
534534
UNARY (simdop s i8x16_abs i16x8_abs i32x4_abs unreachable f32x4_abs f64x2_abs) }
535+
| "i8x16.popcnt"
536+
{ UNARY i8x16_popcnt }
535537
| (simd_int_shape as s)".all_true"
536538
{ only ["i8x16"; "i16x8"; "i32x4"] s lexbuf;
537539
UNARY (simd_int_op s i8x16_all_true i16x8_all_true i32x4_all_true unreachable) }

test/core/simd/meta/simd_int_arith2.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,8 @@ def gen_test_case_combination(self):
450450
for op2 in unary_ops:
451451
o2 = ArithmeticOp(op2)
452452
result3 = []
453-
ret3 = o1.unary_op('-1', self.LANE_VALUE)
454-
ret3 = o2.unary_op(ret3, self.LANE_VALUE)
453+
ret3 = o2.unary_op('-1', self.LANE_VALUE)
454+
ret3 = o1.unary_op(ret3, self.LANE_VALUE)
455455
result3.append(ret3)
456456
cases += '\n' + str(AssertReturn('{lane_type}.{op1}-{lane_type}.{op2}'.format(lane_type=self.LANE_TYPE, op1=op1, op2=op2),
457457
[SIMD.v128_const('-1', self.LANE_TYPE)],
@@ -543,6 +543,7 @@ class Simdi16x8Case(SimdLaneWiseInteger):
543543
class Simdi8x16Case(SimdLaneWiseInteger):
544544
LANE_TYPE = 'i8x16'
545545

546+
UNARY_OPS = ('abs','popcnt')
546547
BINARY_OPS = ('min_s', 'min_u', 'max_s', 'max_u', 'avgr_u')
547548
UNKNOWN_BINARY_OPS = ('i32x4.avgr_u', 'f32x4.avgr_u',
548549
'i64x2.avgr_u', 'f64x2.avgr_u',

0 commit comments

Comments
 (0)