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

Commit 07a4fe6

Browse files
Honrytlively
authored andcommitted
[test] Integrate tests for 64x2 arithmetic ops from WAVM (#135)
This PR also centralizes the processing of float-point ops in one file named simd_float_op.py, see WAVM PR: WAVM/WAVM#209
1 parent a78672c commit 07a4fe6

File tree

10 files changed

+5610
-189
lines changed

10 files changed

+5610
-189
lines changed

test/core/simd/meta/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Currently it only support following simd test files generation.
1212
- 'simd_i16x8_arith.wast'
1313
- 'simd_i32x4_arith.wast'
1414
- 'simd_f32x4_arith.wast'
15+
- 'simd_i64x2_arith.wast'
16+
- 'simd_f64x2_arith.wast'
1517
- 'simd_bitwise.wast'
1618
- 'simd_i8x16_sat_arith.wast'
1719
- 'simd_i16x8_sat_arith.wast'

test/core/simd/meta/gen_tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
'simd_i16x8_arith',
1919
'simd_i32x4_arith',
2020
'simd_f32x4_arith',
21+
'simd_i64x2_arith',
22+
'simd_f64x2_arith',
2123
'simd_sat_arith',
2224
'simd_bitwise',
2325
'simd_f32x4',

test/core/simd/meta/simd_f32x4.py

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,15 @@
55
"""
66

77
from simd_f32x4_arith import Simdf32x4ArithmeticCase
8+
from simd_float_op import FloatingPointSimpleOp
89
from simd import SIMD
910
from test_assert import AssertReturn
1011

1112

12-
def binary_op(op: str, p1: str, p2: str) -> str:
13-
"""Binary operation on p1 and p2 with the operation specified by op
14-
15-
:param op: min, max,
16-
:param p1: float number in hex
17-
:param p2: float number in hex
18-
:return:
19-
"""
20-
f1 = float.fromhex(p1)
21-
f2 = float.fromhex(p2)
22-
23-
if '-nan' in [p1, p2] and 'nan' in [p1, p2]:
24-
return p1
25-
26-
if 'nan' in [p1, p2]:
27-
return 'nan'
28-
29-
if '-nan' in [p1, p2]:
30-
return '-nan'
31-
32-
if op == 'min':
33-
if '-0x0p+0' in [p1, p2] and '0x0p+0' in [p1, p2]:
34-
return '-0x0p+0'
35-
result = min(f1, f2)
36-
37-
elif op == 'max':
38-
if '-0x0p+0' in [p1, p2] and '0x0p+0' in [p1, p2]:
39-
return '0x0p+0'
40-
result = max(f1, f2)
41-
42-
else:
43-
raise Exception('Unknown binary operation: {}'.format(op))
44-
45-
return result.hex()
46-
47-
48-
def unary_op(op: str, p1: str) -> str:
49-
"""Unnary operation on p1 with the operation specified by op
50-
51-
:param op: abs,
52-
:param p1: float number in hex
53-
:return:
54-
"""
55-
f1 = float.fromhex(p1)
56-
if op == 'abs':
57-
return abs(f1).hex()
58-
59-
raise Exception('Unknown unary operation: {}'.format(op))
60-
61-
6213
class Simdf32x4Case(Simdf32x4ArithmeticCase):
6314
UNARY_OPS = ('abs',)
6415
BINARY_OPS = ('min', 'max',)
16+
floatOp = FloatingPointSimpleOp()
6517

6618
FLOAT_NUMBERS = (
6719
'0x0p+0', '-0x0p+0', '0x1p-149', '-0x1p-149', '0x1p-126', '-0x1p-126', '0x1p-1', '-0x1p-1', '0x1p+0', '-0x1p+0',
@@ -367,7 +319,7 @@ def get_normal_case(self):
367319
op_name = self.full_op_name(op)
368320
for p1 in self.FLOAT_NUMBERS:
369321
for p2 in self.FLOAT_NUMBERS:
370-
result = binary_op(op, p1, p2)
322+
result = self.floatOp.binary_op(op, p1, p2)
371323
if 'nan' not in result:
372324
# Normal floating point numbers as the results
373325
binary_test_data.append(['assert_return', op_name, p1, p2, result])
@@ -443,7 +395,7 @@ def get_normal_case(self):
443395

444396
for p in self.FLOAT_NUMBERS:
445397
op_name = self.full_op_name('abs')
446-
result = unary_op('abs', p)
398+
result = self.floatOp.unary_op('abs', p)
447399
# Abs operation is valid for all the floating point numbers
448400
unary_test_data.append(['assert_return', op_name, p, result])
449401

@@ -486,4 +438,4 @@ def gen_test_cases():
486438

487439

488440
if __name__ == '__main__':
489-
gen_test_cases()
441+
gen_test_cases()

test/core/simd/meta/simd_f32x4_arith.py

Lines changed: 12 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4,100 +4,21 @@
44
Generate f32x4 floating-point arithmetic operation cases.
55
"""
66

7-
import math
87
from simd_arithmetic import SimdArithmeticCase
8+
from simd_float_op import FloatingPointArithOp
99

1010

11-
def binary_op(op: str, p1: str, p2: str) -> str:
12-
"""Binary operation on p1 and p2 with the operation specified by op
13-
14-
:param op: add, sub, mul,
15-
:param p1: float number in hex
16-
:param p2: float number in hex
17-
:return:
18-
"""
19-
f1 = float.fromhex(p1)
20-
f2 = float.fromhex(p2)
21-
if op == 'add':
22-
if 'inf' in p1 and 'inf' in p2 and p1 != p2:
23-
return '-nan'
24-
result = f1 + f2
25-
26-
elif op == 'sub':
27-
if 'inf' in p1 and 'inf' in p2 and p1 == p2:
28-
return '-nan'
29-
result = f1 - f2
30-
31-
elif op == 'mul':
32-
if '0x0p+0' in p1 and 'inf' in p2 or 'inf' in p1 and '0x0p+0' in p2:
33-
return '-nan'
34-
result = f1 * f2
35-
36-
elif op == 'div':
37-
if '0x0p+0' in p1 and '0x0p+0' in p2:
38-
return '-nan'
39-
if 'inf' in p1 and 'inf' in p2:
40-
return '-nan'
41-
42-
try:
43-
result = f1 / f2
44-
return get_valid_float(result)
45-
except ZeroDivisionError:
46-
if p1[0] == p2[0]:
47-
return 'inf'
48-
elif p1 == 'inf' and p2 == '0x0p+0':
49-
return 'inf'
50-
else:
51-
return '-inf'
52-
53-
else:
54-
raise Exception('Unknown binary operation')
55-
56-
return get_valid_float(result)
57-
58-
59-
def get_valid_float(value):
60-
if value > float.fromhex('0x1.fffffep+127'):
61-
return 'inf'
62-
if value < float.fromhex('-0x1.fffffep+127'):
63-
return '-inf'
64-
return value.hex()
65-
66-
67-
def float_sqrt(p):
68-
if p == '-0x0p+0':
69-
return '-0x0p+0'
70-
71-
try:
72-
p = float.fromhex(p)
73-
result = float.hex(math.sqrt(p))
74-
except ValueError:
75-
result = '-nan'
76-
77-
return result
78-
79-
80-
def float_neg(p):
81-
if p == 'nan':
82-
return '-nan'
83-
try:
84-
p = float.fromhex(p)
85-
result = float.hex(-p)
86-
except ValueError:
87-
if p.startswith('nan:'):
88-
return '-' + p
89-
if p.startswith('-nan:'):
90-
return p[1:]
91-
92-
return result
11+
class F32ArithOp(FloatingPointArithOp):
12+
maximum = '0x1.fffffep+127'
9313

9414

9515
class Simdf32x4ArithmeticCase(SimdArithmeticCase):
9616
LANE_LEN = 4
9717
LANE_TYPE = 'f32x4'
9818

19+
floatOp = F32ArithOp()
9920
UNARY_OPS = ('neg', 'sqrt')
100-
BINARY_OPS = ('add', 'sub', 'mul', 'div',)
21+
BINARY_OPS = ('add', 'sub', 'mul', 'div')
10122

10223
FLOAT_NUMBERS = (
10324
'0x0p+0', '-0x0p+0', '0x1p-149', '-0x1p-149', '0x1p-126', '-0x1p-126', '0x1p-1', '-0x1p-1', '0x1p+0', '-0x1p+0',
@@ -250,7 +171,7 @@ def get_normal_case(self):
250171
op_name = self.full_op_name(op)
251172
for p1 in self.FLOAT_NUMBERS:
252173
for p2 in self.FLOAT_NUMBERS:
253-
result = binary_op(op, p1, p2)
174+
result = self.floatOp.binary_op(op, p1, p2)
254175
if 'nan' not in result:
255176
# Normal floating point numbers as the results
256177
binary_test_data.append(['assert_return', op_name, p1, p2, result])
@@ -290,7 +211,7 @@ def get_normal_case(self):
290211
else:
291212
# Normal floating point numbers for sqrt operation
292213
op_name = self.full_op_name('sqrt')
293-
result = float_sqrt(p)
214+
result = self.floatOp.float_sqrt(p)
294215
if 'nan' not in result:
295216
# Get the sqrt value correctly
296217
unary_test_data.append(['assert_return', op_name, p, result])
@@ -300,7 +221,7 @@ def get_normal_case(self):
300221

301222
for p in self.FLOAT_NUMBERS + self.NAN_NUMBERS:
302223
op_name = self.full_op_name('neg')
303-
result = float_neg(p)
224+
result = self.floatOp.float_neg(p)
304225
# Neg operation is valid for all the floating point numbers
305226
unary_test_data.append(['assert_return', op_name, p, result])
306227

@@ -363,9 +284,10 @@ def mixed_nan_test(self, cases):
363284
cases.append(template.format(
364285
'assert_return_arithmetic_nan', test_type, i))
365286
else:
366-
cases.append('({} (invoke "f32x4_extract_lane_{}_{}") '.format(
367-
'assert_return', test_type, i) +
368-
'(f32.const {}))'.format(result))
287+
cases.append(''.join([
288+
'({} (invoke "f32x4_extract_lane_{}_{}") '.format(
289+
'assert_return', test_type, i),
290+
'(f32.const {}))'.format(result)]))
369291

370292

371293
def gen_test_cases():

0 commit comments

Comments
 (0)