44Generate f32x4 floating-point arithmetic operation cases.
55"""
66
7- import math
87from 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
9515class 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
371293def gen_test_cases ():
0 commit comments