1 // RUN: mlir-opt %s -test-math-polynomial-approximation \
2 // RUN: -convert-arith-to-llvm \
3 // RUN: -convert-vector-to-llvm \
4 // RUN: -convert-math-to-llvm \
5 // RUN: -convert-std-to-llvm \
6 // RUN: -reconcile-unrealized-casts \
7 // RUN: | mlir-cpu-runner \
8 // RUN: -e main -entry-point-result=void -O0 \
9 // RUN: -shared-libs=%linalg_test_lib_dir/libmlir_c_runner_utils%shlibext \
10 // RUN: -shared-libs=%linalg_test_lib_dir/libmlir_runner_utils%shlibext \
11 // RUN: | FileCheck %s
13 // -------------------------------------------------------------------------- //
15 // -------------------------------------------------------------------------- //
18 %0 = arith.constant 1.25 : f32
19 %1 = math.tanh %0 : f32
22 // CHECK: 0.244919, 0.635149, 0.761594, 0.848284
23 %2 = arith.constant dense<[0.25, 0.75, 1.0, 1.25]> : vector<4xf32>
24 %3 = math.tanh %2 : vector<4xf32>
25 vector.print %3 : vector<4xf32>
27 // CHECK: 0.099668, 0.197375, 0.291313, 0.379949, 0.462117, 0.53705, 0.604368, 0.664037
28 %4 = arith.constant dense<[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]> : vector<8xf32>
29 %5 = math.tanh %4 : vector<8xf32>
30 vector.print %5 : vector<8xf32>
35 // -------------------------------------------------------------------------- //
37 // -------------------------------------------------------------------------- //
40 %0 = arith.constant 14.112233 : f32
41 %1 = math.log %0 : f32
44 // CHECK: -1.38629, -0.287682, 0, 0.223144
45 %2 = arith.constant dense<[0.25, 0.75, 1.0, 1.25]> : vector<4xf32>
46 %3 = math.log %2 : vector<4xf32>
47 vector.print %3 : vector<4xf32>
49 // CHECK: -2.30259, -1.60944, -1.20397, -0.916291, -0.693147, -0.510826, -0.356675, -0.223144
50 %4 = arith.constant dense<[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]> : vector<8xf32>
51 %5 = math.log %4 : vector<8xf32>
52 vector.print %5 : vector<8xf32>
55 %zero = arith.constant 0.0 : f32
56 %log_zero = math.log %zero : f32
57 vector.print %log_zero : f32
60 %neg_one = arith.constant -1.0 : f32
61 %log_neg_one = math.log %neg_one : f32
62 vector.print %log_neg_one : f32
65 %inf = arith.constant 0x7f800000 : f32
66 %log_inf = math.log %inf : f32
67 vector.print %log_inf : f32
69 // CHECK: -inf, nan, inf, 0.693147
70 %special_vec = arith.constant dense<[0.0, -1.0, 0x7f800000, 2.0]> : vector<4xf32>
71 %log_special_vec = math.log %special_vec : vector<4xf32>
72 vector.print %log_special_vec : vector<4xf32>
79 %0 = arith.constant 14.112233 : f32
80 %1 = math.log2 %0 : f32
83 // CHECK: -2, -0.415037, 0, 0.321928
84 %2 = arith.constant dense<[0.25, 0.75, 1.0, 1.25]> : vector<4xf32>
85 %3 = math.log2 %2 : vector<4xf32>
86 vector.print %3 : vector<4xf32>
88 // CHECK: -3.32193, -2.32193, -1.73697, -1.32193, -1, -0.736966, -0.514573, -0.321928
89 %4 = arith.constant dense<[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]> : vector<8xf32>
90 %5 = math.log2 %4 : vector<8xf32>
91 vector.print %5 : vector<8xf32>
94 %zero = arith.constant 0.0 : f32
95 %log_zero = math.log2 %zero : f32
96 vector.print %log_zero : f32
99 %neg_one = arith.constant -1.0 : f32
100 %log_neg_one = math.log2 %neg_one : f32
101 vector.print %log_neg_one : f32
104 %inf = arith.constant 0x7f800000 : f32
105 %log_inf = math.log2 %inf : f32
106 vector.print %log_inf : f32
108 // CHECK: -inf, nan, inf, 1.58496
109 %special_vec = arith.constant dense<[0.0, -1.0, 0x7f800000, 3.0]> : vector<4xf32>
110 %log_special_vec = math.log2 %special_vec : vector<4xf32>
111 vector.print %log_special_vec : vector<4xf32>
118 %0 = arith.constant 0.01 : f32
119 %1 = math.log1p %0 : f32
120 vector.print %1 : f32
122 // CHECK: -4.60517, -0.693147, 0, 1.38629
123 %2 = arith.constant dense<[-0.99, -0.5, 0.0, 3.0]> : vector<4xf32>
124 %3 = math.log1p %2 : vector<4xf32>
125 vector.print %3 : vector<4xf32>
127 // CHECK: 0.0953102, 0.182322, 0.262364, 0.336472, 0.405465, 0.470004, 0.530628, 0.587787
128 %4 = arith.constant dense<[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]> : vector<8xf32>
129 %5 = math.log1p %4 : vector<8xf32>
130 vector.print %5 : vector<8xf32>
133 %neg_one = arith.constant -1.0 : f32
134 %log_neg_one = math.log1p %neg_one : f32
135 vector.print %log_neg_one : f32
138 %neg_two = arith.constant -2.0 : f32
139 %log_neg_two = math.log1p %neg_two : f32
140 vector.print %log_neg_two : f32
143 %inf = arith.constant 0x7f800000 : f32
144 %log_inf = math.log1p %inf : f32
145 vector.print %log_inf : f32
147 // CHECK: -inf, nan, inf, 9.99995e-06
148 %special_vec = arith.constant dense<[-1.0, -1.1, 0x7f800000, 0.00001]> : vector<4xf32>
149 %log_special_vec = math.log1p %special_vec : vector<4xf32>
150 vector.print %log_special_vec : vector<4xf32>
155 // -------------------------------------------------------------------------- //
157 // -------------------------------------------------------------------------- //
159 // CHECK: -0.000274406
160 %val1 = arith.constant -2.431864e-4 : f32
161 %erfVal1 = math.erf %val1 : f32
162 vector.print %erfVal1 : f32
165 %val2 = arith.constant 0.79999 : f32
166 %erfVal2 = math.erf %val2 : f32
167 vector.print %erfVal2 : f32
170 %val3 = arith.constant 0.8 : f32
171 %erfVal3 = math.erf %val3 : f32
172 vector.print %erfVal3 : f32
175 %val4 = arith.constant 1.99999 : f32
176 %erfVal4 = math.erf %val4 : f32
177 vector.print %erfVal4 : f32
180 %val5 = arith.constant 2.0 : f32
181 %erfVal5 = math.erf %val5 : f32
182 vector.print %erfVal5 : f32
185 %val6 = arith.constant 3.74999 : f32
186 %erfVal6 = math.erf %val6 : f32
187 vector.print %erfVal6 : f32
190 %val7 = arith.constant 3.75 : f32
191 %erfVal7 = math.erf %val7 : f32
192 vector.print %erfVal7 : f32
195 %negativeInf = arith.constant 0xff800000 : f32
196 %erfNegativeInf = math.erf %negativeInf : f32
197 vector.print %erfNegativeInf : f32
199 // CHECK: -1, -1, -0.913759, -0.731446
200 %vecVals1 = arith.constant dense<[-3.4028235e+38, -4.54318, -1.2130899, -7.8234202e-01]> : vector<4xf32>
201 %erfVecVals1 = math.erf %vecVals1 : vector<4xf32>
202 vector.print %erfVecVals1 : vector<4xf32>
204 // CHECK: -1.3264e-38, 0, 1.3264e-38, 0.121319
205 %vecVals2 = arith.constant dense<[-1.1754944e-38, 0.0, 1.1754944e-38, 1.0793410e-01]> : vector<4xf32>
206 %erfVecVals2 = math.erf %vecVals2 : vector<4xf32>
207 vector.print %erfVecVals2 : vector<4xf32>
209 // CHECK: 0.919477, 0.999069, 1, 1
210 %vecVals3 = arith.constant dense<[1.23578, 2.34093, 3.82342, 3.4028235e+38]> : vector<4xf32>
211 %erfVecVals3 = math.erf %vecVals3 : vector<4xf32>
212 vector.print %erfVecVals3 : vector<4xf32>
215 %inf = arith.constant 0x7f800000 : f32
216 %erfInf = math.erf %inf : f32
217 vector.print %erfInf : f32
220 %nan = arith.constant 0x7fc00000 : f32
221 %erfNan = math.erf %nan : f32
222 vector.print %erfNan : f32
227 // -------------------------------------------------------------------------- //
229 // -------------------------------------------------------------------------- //
232 %0 = arith.constant 1.0 : f32
233 %1 = math.exp %0 : f32
234 vector.print %1 : f32
236 // CHECK: 0.778802, 2.117, 2.71828, 3.85742
237 %2 = arith.constant dense<[-0.25, 0.75, 1.0, 1.35]> : vector<4xf32>
238 %3 = math.exp %2 : vector<4xf32>
239 vector.print %3 : vector<4xf32>
242 %zero = arith.constant 0.0 : f32
243 %exp_zero = math.exp %zero : f32
244 vector.print %exp_zero : f32
246 // CHECK: 1.17549e-38, 1.38879e-11, 7.20049e+10, inf
247 %special_vec = arith.constant dense<[-89.0, -25.0, 25.0, 89.0]> : vector<4xf32>
248 %exp_special_vec = math.exp %special_vec : vector<4xf32>
249 vector.print %exp_special_vec : vector<4xf32>
252 %inf = arith.constant 0x7f800000 : f32
253 %exp_inf = math.exp %inf : f32
254 vector.print %exp_inf : f32
257 %negative_inf = arith.constant 0xff800000 : f32
258 %exp_negative_inf = math.exp %negative_inf : f32
259 vector.print %exp_negative_inf : f32
266 %0 = arith.constant 1.0e-10 : f32
267 %1 = math.expm1 %0 : f32
268 vector.print %1 : f32
270 // CHECK: -0.00995016, 0.0100502, 0.648721, 6.38905
271 %2 = arith.constant dense<[-0.01, 0.01, 0.5, 2.0]> : vector<4xf32>
272 %3 = math.expm1 %2 : vector<4xf32>
273 vector.print %3 : vector<4xf32>
275 // CHECK: -0.181269, 0, 0.221403, 0.491825, 0.822119, 1.22554, 1.71828, 2.32012
276 %4 = arith.constant dense<[-0.2, 0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2]> : vector<8xf32>
277 %5 = math.expm1 %4 : vector<8xf32>
278 vector.print %5 : vector<8xf32>
281 %neg_inf = arith.constant 0xff800000 : f32
282 %expm1_neg_inf = math.expm1 %neg_inf : f32
283 vector.print %expm1_neg_inf : f32
286 %inf = arith.constant 0x7f800000 : f32
287 %expm1_inf = math.expm1 %inf : f32
288 vector.print %expm1_inf : f32
290 // CHECK: -1, inf, 1e-10
291 %special_vec = arith.constant dense<[0xff800000, 0x7f800000, 1.0e-10]> : vector<3xf32>
292 %log_special_vec = math.expm1 %special_vec : vector<3xf32>
293 vector.print %log_special_vec : vector<3xf32>
297 // -------------------------------------------------------------------------- //
299 // -------------------------------------------------------------------------- //
302 %0 = arith.constant 0.0 : f32
303 %sin_0 = math.sin %0 : f32
304 vector.print %sin_0 : f32
307 %pi_over_4 = arith.constant 0.78539816339 : f32
308 %sin_pi_over_4 = math.sin %pi_over_4 : f32
309 vector.print %sin_pi_over_4 : f32
312 %pi_over_2 = arith.constant 1.57079632679 : f32
313 %sin_pi_over_2 = math.sin %pi_over_2 : f32
314 vector.print %sin_pi_over_2 : f32
318 %pi = arith.constant 3.14159265359 : f32
319 %sin_pi = math.sin %pi : f32
320 vector.print %sin_pi : f32
323 %pi_3_over_2 = arith.constant 4.71238898038 : f32
324 %sin_pi_3_over_2 = math.sin %pi_3_over_2 : f32
325 vector.print %sin_pi_3_over_2 : f32
327 // CHECK: 0, 0.866025, -1
328 %vec_x = arith.constant dense<[9.42477796077, 2.09439510239, -1.57079632679]> : vector<3xf32>
329 %sin_vec_x = math.sin %vec_x : vector<3xf32>
330 vector.print %sin_vec_x : vector<3xf32>
335 // -------------------------------------------------------------------------- //
337 // -------------------------------------------------------------------------- //
341 %0 = arith.constant 0.0 : f32
342 %cos_0 = math.cos %0 : f32
343 vector.print %cos_0 : f32
346 %pi_over_4 = arith.constant 0.78539816339 : f32
347 %cos_pi_over_4 = math.cos %pi_over_4 : f32
348 vector.print %cos_pi_over_4 : f32
351 %pi_over_2 = arith.constant 1.57079632679 : f32
352 %cos_pi_over_2 = math.cos %pi_over_2 : f32
353 vector.print %cos_pi_over_2 : f32
356 %pi = arith.constant 3.14159265359 : f32
357 %cos_pi = math.cos %pi : f32
358 vector.print %cos_pi : f32
361 %pi_3_over_2 = arith.constant 4.71238898038 : f32
362 %cos_pi_3_over_2 = math.cos %pi_3_over_2 : f32
363 vector.print %cos_pi_3_over_2 : f32
365 // CHECK: -1, -0.5, 0
366 %vec_x = arith.constant dense<[9.42477796077, 2.09439510239, -1.57079632679]> : vector<3xf32>
367 %cos_vec_x = math.cos %vec_x : vector<3xf32>
368 vector.print %cos_vec_x : vector<3xf32>
376 call @tanh(): () -> ()
377 call @log(): () -> ()
378 call @log2(): () -> ()
379 call @log1p(): () -> ()
380 call @erf(): () -> ()
381 call @exp(): () -> ()
382 call @expm1(): () -> ()
383 call @sin(): () -> ()
384 call @cos(): () -> ()