Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update int dot tests to allow for saturation of intermediate 16-bit #130

Merged
merged 2 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions proposals/relaxed-simd/Overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,22 +261,45 @@ When the second operand of the product has the high bit set in a lane, that
lane's result is implementation defined.

```python
def dot_product(signed, elements, a, b, c):
def i16x8_dot_i8x16_i7x16_s(a, b):
intermediate = []
result = []
for i in range(16):
if (b[i] & 0x80):
lhs = as_signed(a[i]) if signed else a[i]
rhs = IMPLEMENTATION_DEFINED_ONE_OF(as_signed(b[i]), b[i])
lhs = as_signed(a[i])
rhs = IMPLEMENTATION_DEFINED_ONE_OF(as_signed(b[i]), as_unsigned(b[i]))
intermediate[i] = lhs * rhs
else:
intermediate[i] = (as_signed(a[i]) if signed else a[i]) * b[i]
for i in range(0, 16, elements):
result[i/elements] = sum(intermediate[i:i+elements])
result[i/elements] += c[i/elements] if c else 0
intermediate[i] = as_signed(a[i]) * b[i]
for i in range(0, 16, 2):
result[i/2] = IMPLEMENTATION_DEFINED_ONE_OF(
intermediate[i] + intermediate[i+1],
saturate(intermediate[i] + intermediate[i+1]))

i16x8_dot_i8x16_i7x16_s(a, b) = dot_product(signed=True, elements=2, a, b)
i32x4.dot_i8x16_i7x16_add_s(a, b, c) = dot_product(signed=True, elements=4, a, b, c)
def i32x4.dot_i8x16_i7x16_add_s(a, b, c):
intermediate = []
tmp = []
result = []
for i in range(16):
if (b[i] & 0x80):
lhs = as_signed(a[i])
rhs = IMPLEMENTATION_DEFINED_ONE_OF(as_signed(b[i]), as_unsigned(b[i]))
intermediate[i] = lhs * rhs
else:
intermediate[i] = as_signed(a[i]) * b[i]

for i in range(0, 16, 2):
tmp[i/2] = IMPLEMENTATION_DEFINED_ONE_OF(
intermediate[i] + intermediate[i+1],
saturate(intermediate[i] + intermediate[i+1]))

for i in range(0, 8, 2):
dst[i/4] = tmp[i] + tmp[i+1]

for i in range(0, 4):
dst[i] += c[i]

saturate(x) = min(INT16_MAX, max(INT16_MIN, x))
```

## Binary format
Expand Down
11 changes: 9 additions & 2 deletions test/core/relaxed-simd/relaxed_dot_product.wast
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@
;; intermediate result is [-65024, 64516, 0, 0]
(v128.const i32x4 -65023 64518 3 4))

;; signed * unsigned : -128 * 129 * 4 = -66,048 (+ 1)
;; signed * unsigned : -128 * 129 * 4 = -66,048 (+ 1) VPDPBUSD AVX2-VNNI or AVX512-VNNI
;; signed * unsigned with intermediate saturation :
;; (-128 * 129) + (-128 * 129) = -33024 saturated to -32768 (PMADDUBSW)
;; -32768 + -32768 = -65536 (+ 1)
;; signed * signed : -128 * -127 * 4 = 65,024 (+ 1)
;; unsigned * unsigned : 128 * 129 * 2 = 66,048 (+ 1)
(assert_return (invoke "i32x4.relaxed_dot_i8x16_i7x16_add_s"
Expand All @@ -62,6 +65,7 @@
(v128.const i32x4 1 2 3 4))
(either
(v128.const i32x4 -66047 2 3 4)
(v128.const i32x4 -65536 2 3 4)
ngzhian marked this conversation as resolved.
Show resolved Hide resolved
(v128.const i32x4 65025 2 3 4)
(v128.const i32x4 66049 2 3 4)))

Expand Down Expand Up @@ -89,7 +93,10 @@
(v128.const i8x16 -127 -127 0 0 0 0 0 0 0 0 0 0 0 0 0 0))
(v128.const i16x8 -1 -1 -1 -1 -1 -1 -1 -1))

;; signed * unsigned : -128 * 129 * 4 = -66,048 (+ 1)
;; signed * unsigned : -128 * 129 * 4 = -66,048 (+ 1) VPDPBUSD AVX2-VNNI or AVX512-VNNI
;; signed * unsigned with intermediate saturation :
;; (-128 * 129) + (-128 * 129) = -33024 saturated to -32768 (PMADDUBSW)
;; -32768 + -32768 = -65536 (+ 1)
;; signed * signed : -128 * -127 * 4 = 65,024 (+ 1)
;; unsigned * unsigned : 128 * 129 * 2 = 66,048 (+ 1)
(assert_return (invoke "i32x4.relaxed_dot_i8x16_i7x16_add_s_cmp"
Expand Down