Skip to content

Commit

Permalink
Shorten saturating instructions to sat
Browse files Browse the repository at this point in the history
Also update the test generate file, and regenerated all test files.

Fixed WebAssembly#332.
  • Loading branch information
ngzhian committed Sep 10, 2020
1 parent 163e122 commit 460474a
Show file tree
Hide file tree
Showing 13 changed files with 1,335 additions and 1,335 deletions.
16 changes: 8 additions & 8 deletions interpreter/binary/decode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,11 @@ let simd_prefix s =
| 0x65l -> i8x16_narrow_i16x8_s
| 0x66l -> i8x16_narrow_i16x8_u
| 0x6el -> i8x16_add
| 0x6fl -> i8x16_add_saturate_s
| 0x70l -> i8x16_add_saturate_u
| 0x6fl -> i8x16_add_sat_s
| 0x70l -> i8x16_add_sat_u
| 0x71l -> i8x16_sub
| 0x72l -> i8x16_sub_saturate_s
| 0x73l -> i8x16_sub_saturate_u
| 0x72l -> i8x16_sub_sat_s
| 0x73l -> i8x16_sub_sat_u
| 0x76l -> i8x16_min_s
| 0x77l -> i8x16_min_u
| 0x78l -> i8x16_max_s
Expand All @@ -340,11 +340,11 @@ let simd_prefix s =
| 0x8cl -> i16x8_shr_s
| 0x8dl -> i16x8_shr_u
| 0x8el -> i16x8_add
| 0x8fl -> i16x8_add_saturate_s
| 0x90l -> i16x8_add_saturate_u
| 0x8fl -> i16x8_add_sat_s
| 0x90l -> i16x8_add_sat_u
| 0x91l -> i16x8_sub
| 0x92l -> i16x8_sub_saturate_s
| 0x93l -> i16x8_sub_saturate_u
| 0x92l -> i16x8_sub_sat_s
| 0x93l -> i16x8_sub_sat_u
| 0x95l -> i16x8_mul
| 0x96l -> i16x8_min_s
| 0x97l -> i16x8_min_u
Expand Down
16 changes: 8 additions & 8 deletions interpreter/syntax/operators.ml
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,11 @@ let i8x16_shl = SimdShift V128Op.(I8x16 Shl)
let i8x16_shr_s = SimdShift V128Op.(I8x16 ShrS)
let i8x16_shr_u = SimdShift V128Op.(I8x16 ShrU)
let i8x16_add = Binary (V128 (V128Op.I8x16 V128Op.Add))
let i8x16_add_saturate_s = Binary (V128 V128Op.(I8x16 AddSatS))
let i8x16_add_saturate_u = Binary (V128 V128Op.(I8x16 AddSatU))
let i8x16_add_sat_s = Binary (V128 V128Op.(I8x16 AddSatS))
let i8x16_add_sat_u = Binary (V128 V128Op.(I8x16 AddSatU))
let i8x16_sub = Binary (V128 (V128Op.I8x16 V128Op.Sub))
let i8x16_sub_saturate_s = Binary (V128 V128Op.(I8x16 SubSatS))
let i8x16_sub_saturate_u = Binary (V128 V128Op.(I8x16 SubSatU))
let i8x16_sub_sat_s = Binary (V128 V128Op.(I8x16 SubSatS))
let i8x16_sub_sat_u = Binary (V128 V128Op.(I8x16 SubSatU))
let i8x16_abs = Unary (V128 (V128Op.I8x16 V128Op.Abs))
let i8x16_min_s = Binary (V128 (V128Op.I8x16 V128Op.MinS))
let i8x16_min_u = Binary (V128 (V128Op.I8x16 V128Op.MinU))
Expand Down Expand Up @@ -314,11 +314,11 @@ let i16x8_shl = SimdShift V128Op.(I16x8 Shl)
let i16x8_shr_s = SimdShift V128Op.(I16x8 ShrS)
let i16x8_shr_u = SimdShift V128Op.(I16x8 ShrU)
let i16x8_add = Binary (V128 (V128Op.I16x8 V128Op.Add))
let i16x8_add_saturate_s = Binary (V128 V128Op.(I16x8 AddSatS))
let i16x8_add_saturate_u = Binary (V128 V128Op.(I16x8 AddSatU))
let i16x8_add_sat_s = Binary (V128 V128Op.(I16x8 AddSatS))
let i16x8_add_sat_u = Binary (V128 V128Op.(I16x8 AddSatU))
let i16x8_sub = Binary (V128 (V128Op.I16x8 V128Op.Sub))
let i16x8_sub_saturate_s = Binary (V128 V128Op.(I16x8 SubSatS))
let i16x8_sub_saturate_u = Binary (V128 V128Op.(I16x8 SubSatU))
let i16x8_sub_sat_s = Binary (V128 V128Op.(I16x8 SubSatS))
let i16x8_sub_sat_u = Binary (V128 V128Op.(I16x8 SubSatU))
let i16x8_mul = Binary (V128 (V128Op.I16x8 V128Op.Mul))
let i16x8_abs = Unary (V128 (V128Op.I16x8 V128Op.Abs))
let i16x8_min_s = Binary (V128 (V128Op.I16x8 V128Op.MinS))
Expand Down
16 changes: 8 additions & 8 deletions interpreter/text/arrange.ml
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ struct
| I8x16 NarrowS -> "i8x16.narrow_i16x8_s"
| I8x16 NarrowU -> "i8x16.narrow_i16x8_u"
| I8x16 Add -> "i8x16.add"
| I8x16 AddSatS -> "i8x16.add_saturate_s"
| I8x16 AddSatU -> "i8x16.add_saturate_u"
| I8x16 AddSatS -> "i8x16.add_sat_s"
| I8x16 AddSatU -> "i8x16.add_sat_u"
| I8x16 Sub -> "i8x16.sub"
| I8x16 SubSatS -> "i8x16.sub_saturate_s"
| I8x16 SubSatU -> "i8x16.sub_saturate_u"
| I8x16 SubSatS -> "i8x16.sub_sat_s"
| I8x16 SubSatU -> "i8x16.sub_sat_u"
| I8x16 MinS -> "i8x16.min_s"
| I8x16 MinU -> "i8x16.min_u"
| I8x16 MaxS -> "i8x16.max_s"
Expand All @@ -279,11 +279,11 @@ struct
| I16x8 NarrowS -> "i16x8.narrow_i32x4_s"
| I16x8 NarrowU -> "i16x8.narrow_i32x4_u"
| I16x8 Add -> "i16x8.add"
| I16x8 AddSatS -> "i16x8.add_saturate_s"
| I16x8 AddSatU -> "i16x8.add_saturate_u"
| I16x8 AddSatS -> "i16x8.add_sat_s"
| I16x8 AddSatU -> "i16x8.add_sat_u"
| I16x8 Sub -> "i16x8.sub"
| I16x8 SubSatS -> "i16x8.sub_saturate_s"
| I16x8 SubSatU -> "i16x8.sub_saturate_u"
| I16x8 SubSatS -> "i16x8.sub_sat_s"
| I16x8 SubSatU -> "i16x8.sub_sat_u"
| I16x8 Mul -> "i16x8.mul"
| I16x8 MinS -> "i16x8.min_s"
| I16x8 MinU -> "i16x8.min_u"
Expand Down
16 changes: 8 additions & 8 deletions interpreter/text/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -550,14 +550,14 @@ rule token = parse
| "i32x4.widen_high_i16x8_"(sign as s)
{ UNARY (ext s i32x4_widen_high_i16x8_s i32x4_widen_high_i16x8_u) }

| "i8x16.add_saturate_"(sign as s)
{ BINARY (ext s i8x16_add_saturate_s i8x16_add_saturate_u) }
| "i8x16.sub_saturate_"(sign as s)
{ BINARY (ext s i8x16_sub_saturate_s i8x16_sub_saturate_u) }
| "i16x8.add_saturate_"(sign as s)
{ BINARY (ext s i16x8_add_saturate_s i16x8_add_saturate_u) }
| "i16x8.sub_saturate_"(sign as s)
{ BINARY (ext s i16x8_sub_saturate_s i16x8_sub_saturate_u) }
| "i8x16.add_sat_"(sign as s)
{ BINARY (ext s i8x16_add_sat_s i8x16_add_sat_u) }
| "i8x16.sub_sat_"(sign as s)
{ BINARY (ext s i8x16_sub_sat_s i8x16_sub_sat_u) }
| "i16x8.add_sat_"(sign as s)
{ BINARY (ext s i16x8_add_sat_s i16x8_add_sat_u) }
| "i16x8.sub_sat_"(sign as s)
{ BINARY (ext s i16x8_sub_sat_s i16x8_sub_sat_u) }

| (simd_shape as s) { SIMD_SHAPE (simd_shape s) }

Expand Down
16 changes: 8 additions & 8 deletions proposals/simd/BinarySIMD.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ For example, `ImmLaneIdx16` is a byte with values in the range 0-15 (inclusive).
| `i8x16.shr_s` | `0x6c`| - |
| `i8x16.shr_u` | `0x6d`| - |
| `i8x16.add` | `0x6e`| - |
| `i8x16.add_saturate_s` | `0x6f`| - |
| `i8x16.add_saturate_u` | `0x70`| - |
| `i8x16.add_sat_s` | `0x6f`| - |
| `i8x16.add_sat_u` | `0x70`| - |
| `i8x16.sub` | `0x71`| - |
| `i8x16.sub_saturate_s` | `0x72`| - |
| `i8x16.sub_saturate_u` | `0x73`| - |
| `i8x16.sub_sat_s` | `0x72`| - |
| `i8x16.sub_sat_u` | `0x73`| - |
| `i8x16.min_s` | `0x76`| - |
| `i8x16.min_u` | `0x77`| - |
| `i8x16.max_s` | `0x78`| - |
Expand All @@ -151,11 +151,11 @@ For example, `ImmLaneIdx16` is a byte with values in the range 0-15 (inclusive).
| `i16x8.shr_s` | `0x8c`| - |
| `i16x8.shr_u` | `0x8d`| - |
| `i16x8.add` | `0x8e`| - |
| `i16x8.add_saturate_s` | `0x8f`| - |
| `i16x8.add_saturate_u` | `0x90`| - |
| `i16x8.add_sat_s` | `0x8f`| - |
| `i16x8.add_sat_u` | `0x90`| - |
| `i16x8.sub` | `0x91`| - |
| `i16x8.sub_saturate_s` | `0x92`| - |
| `i16x8.sub_saturate_u` | `0x93`| - |
| `i16x8.sub_sat_s` | `0x92`| - |
| `i16x8.sub_sat_u` | `0x93`| - |
| `i16x8.mul` | `0x95`| - |
| `i16x8.min_s` | `0x96`| - |
| `i16x8.min_u` | `0x97`| - |
Expand Down
16 changes: 8 additions & 8 deletions proposals/simd/ImplementationStatus.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@
| `i8x16.shr_s` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i8x16.shr_u` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i8x16.add` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i8x16.add_saturate_s` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i8x16.add_saturate_u` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i8x16.add_sat_s` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i8x16.add_sat_u` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i8x16.sub` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i8x16.sub_saturate_s` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i8x16.sub_saturate_u` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i8x16.sub_sat_s` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i8x16.sub_sat_u` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i8x16.min_s` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i8x16.min_u` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i8x16.max_s` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
Expand All @@ -119,11 +119,11 @@
| `i16x8.shr_s` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i16x8.shr_u` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i16x8.add` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i16x8.add_saturate_s` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i16x8.add_saturate_u` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i16x8.add_sat_s` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i16x8.add_sat_u` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i16x8.sub` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i16x8.sub_saturate_s` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i16x8.sub_saturate_u` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i16x8.sub_sat_s` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i16x8.sub_sat_u` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i16x8.mul` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i16x8.min_s` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
| `i16x8.min_u` | `-msimd128` | :heavy_check_mark: | | | :heavy_check_mark: |
Expand Down
8 changes: 4 additions & 4 deletions proposals/simd/NewOpcodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@
| i8x16.shr_s | 0x6c | i16x8.shr_s | 0x8c | i32x4.shr_s | 0xac | i64x2.shr_s | 0xcc |
| i8x16.shr_u | 0x6d | i16x8.shr_u | 0x8d | i32x4.shr_u | 0xad | i64x2.shr_u | 0xcd |
| i8x16.add | 0x6e | i16x8.add | 0x8e | i32x4.add | 0xae | i64x2.add | 0xce |
| i8x16.add_saturate_s | 0x6f | i16x8.add_saturate_s | 0x8f | ---- add_sat ---- | 0xaf | ---- | 0xcf |
| i8x16.add_saturate_u | 0x70 | i16x8.add_saturate_u | 0x90 | ---- add_sat ---- | 0xb0 | ---- | 0xd0 |
| i8x16.add_sat_s | 0x6f | i16x8.add_sat_s | 0x8f | ---- add_sat ---- | 0xaf | ---- | 0xcf |
| i8x16.add_sat_u | 0x70 | i16x8.add_sat_u | 0x90 | ---- add_sat ---- | 0xb0 | ---- | 0xd0 |
| i8x16.sub | 0x71 | i16x8.sub | 0x91 | i32x4.sub | 0xb1 | i64x2.sub | 0xd1 |
| i8x16.sub_saturate_s | 0x72 | i16x8.sub_saturate_s | 0x92 | ---- sub_sat ---- | 0xb2 | ---- | 0xd2 |
| i8x16.sub_saturate_u | 0x73 | i16x8.sub_saturate_u | 0x93 | ---- sub_sat ---- | 0xb3 | ---- | 0xd3 |
| i8x16.sub_sat_s | 0x72 | i16x8.sub_sat_s | 0x92 | ---- sub_sat ---- | 0xb2 | ---- | 0xd2 |
| i8x16.sub_sat_u | 0x73 | i16x8.sub_sat_u | 0x93 | ---- sub_sat ---- | 0xb3 | ---- | 0xd3 |
| ---- dot ---- | 0x74 | ---- dot ---- | 0x94 | ---- dot ---- | 0xb4 | ---- | 0xd4 |
| ---- mul ---- | 0x75 | i16x8.mul | 0x95 | i32x4.mul | 0xb5 | i64x2.mul | 0xd5 |
| i8x16.min_s | 0x76 | i16x8.min_s | 0x96 | i32x4.min_s | 0xb6 | ---- | 0xd6 |
Expand Down
24 changes: 12 additions & 12 deletions proposals/simd/SIMD.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,40 +433,40 @@ def S.UnsignedSaturate(x):
```

### Saturating integer addition
* `i8x16.add_saturate_s(a: v128, b: v128) -> v128`
* `i8x16.add_saturate_u(a: v128, b: v128) -> v128`
* `i16x8.add_saturate_s(a: v128, b: v128) -> v128`
* `i16x8.add_saturate_u(a: v128, b: v128) -> v128`
* `i8x16.add_sat_s(a: v128, b: v128) -> v128`
* `i8x16.add_sat_u(a: v128, b: v128) -> v128`
* `i16x8.add_sat_s(a: v128, b: v128) -> v128`
* `i16x8.add_sat_u(a: v128, b: v128) -> v128`

Lane-wise saturating addition:

```python
def S.add_saturate_s(a, b):
def S.add_sat_s(a, b):
def addsat(x, y):
return S.SignedSaturate(x + y)
return S.lanewise_binary(addsat, S.AsSigned(a), S.AsSigned(b))

def S.add_saturate_u(a, b):
def S.add_sat_u(a, b):
def addsat(x, y):
return S.UnsignedSaturate(x + y)
return S.lanewise_binary(addsat, S.AsUnsigned(a), S.AsUnsigned(b))
```

### Saturating integer subtraction
* `i8x16.sub_saturate_s(a: v128, b: v128) -> v128`
* `i8x16.sub_saturate_u(a: v128, b: v128) -> v128`
* `i16x8.sub_saturate_s(a: v128, b: v128) -> v128`
* `i16x8.sub_saturate_u(a: v128, b: v128) -> v128`
* `i8x16.sub_sat_s(a: v128, b: v128) -> v128`
* `i8x16.sub_sat_u(a: v128, b: v128) -> v128`
* `i16x8.sub_sat_s(a: v128, b: v128) -> v128`
* `i16x8.sub_sat_u(a: v128, b: v128) -> v128`

Lane-wise saturating subtraction:

```python
def S.sub_saturate_s(a, b):
def S.sub_sat_s(a, b):
def subsat(x, y):
return S.SignedSaturate(x - y)
return S.lanewise_binary(subsat, S.AsSigned(a), S.AsSigned(b))

def S.sub_saturate_u(a, b):
def S.sub_sat_u(a, b):
def subsat(x, y):
return S.UnsignedSaturate(x - y)
return S.lanewise_binary(subsat, S.AsUnsigned(a), S.AsUnsigned(b))
Expand Down
16 changes: 8 additions & 8 deletions test/core/simd/meta/simd_integer_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class ArithmeticOp:
may be required for the operations.
The following operators are covered:
add, sub, mul, neg,
add_saturate_s, add_saturate_u,
sub_saturate_s, sub_saturate_u,
add_sat_s, add_sat_u,
sub_sat_s, sub_sat_u,
min_s, min_u, max_s, max_u, avgr_u, abs
"""
def __init__(self, op: str):
Expand Down Expand Up @@ -45,7 +45,7 @@ def _saturate(self, operand1: int, operand2: int, lane: LaneValue) -> int:
"""Get the result of saturating arithmetic operation on 2 operands.
The operands can be both signed or unsigned. The following ops
are covered:
add_saturate_s, sub_saturate_s, add_saturate_u, sub_saturate_u,
add_sat_s, sub_sat_s, add_sat_u, sub_sat_u,
Saturating arithmetic can make sure:
When the operation result is less than the minimum, return the minimum.
Expand All @@ -56,7 +56,7 @@ def _saturate(self, operand1: int, operand2: int, lane: LaneValue) -> int:
:param lane: the LaneValue instance of a lane in v128
:return: the result of the saturating arithmetic operation
"""
if self.op.endswith('saturate_s'):
if self.op.endswith('sat_s'):
if operand1 > lane.max:
operand1 -= lane.mod
if operand2 > lane.max:
Expand All @@ -72,7 +72,7 @@ def _saturate(self, operand1: int, operand2: int, lane: LaneValue) -> int:
if value < lane.min:
return lane.min

if self.op.endswith('saturate_u'):
if self.op.endswith('sat_u'):
if operand1 < 0:
operand1 += lane.mod
if operand2 < 0:
Expand Down Expand Up @@ -127,8 +127,8 @@ def binary_op(self, operand1, operand2, lane):
Supported ops:
add, sub, mul,
add_saturate_s, add_saturate_u,
sub_saturate_s, sub_saturate_u,
add_sat_s, add_sat_u,
sub_sat_s, sub_sat_u,
min_s, min_u, max_s, max_u, avgr_u
:param operand1: the operand 1, integer or literal string in hex or decimal format
Expand All @@ -155,7 +155,7 @@ def binary_op(self, operand1, operand2, lane):
value = v1 - v2
elif self.op == 'mul':
value = v1 * v2
elif 'saturate' in self.op:
elif 'sat' in self.op:
value = self._saturate(v1, v2, lane)
if self.op.endswith('_u'):
result_signed = False
Expand Down
Loading

0 comments on commit 460474a

Please sign in to comment.