1 // RUN: mlir-opt %s -pass-pipeline="builtin.module(func.func(arith-expand{include-bf16=true},convert-arith-to-llvm),convert-vector-to-llvm,convert-func-to-llvm,reconcile-unrealized-casts)" \
2 // RUN: | mlir-cpu-runner \
3 // RUN: -e main -entry-point-result=void -O0 \
4 // RUN: -shared-libs=%mlir_c_runner_utils \
5 // RUN: -shared-libs=%mlir_runner_utils \
8 func.func @trunc_bf16(%a : f32) {
9 %b = arith.truncf %a : f32 to bf16
10 %c = arith.extf %b : bf16 to f32
16 // Note: this is a tie (low 16 bits are 0x8000). We expect the rounding behavior
17 // to break ties "to nearest-even", which in this case means downwards,
18 // since bit 16 is not set.
20 %value_1_00391_I = arith.constant 0x3f808000 : i32
21 %value_1_00391_F = arith.bitcast %value_1_00391_I : i32 to f32
22 call @trunc_bf16(%value_1_00391_F): (f32) -> ()
24 // Note: this is a tie (low 16 bits are 0x8000). We expect the rounding behavior
25 // to break ties "to nearest-even", which in this case means upwards,
26 // since bit 16 is set.
28 %value_1_01172_I = arith.constant 0x3f818000 : i32
29 %value_1_01172_F = arith.bitcast %value_1_01172_I : i32 to f32
30 call @trunc_bf16(%value_1_01172_F): (f32) -> ()
33 %noRoundNegOneI = arith.constant 0xbf808000 : i32
34 %noRoundNegOneF = arith.bitcast %noRoundNegOneI : i32 to f32
35 call @trunc_bf16(%noRoundNegOneF): (f32) -> ()
37 // CHECK-NEXT: -1.00781
38 %roundNegOneI = arith.constant 0xbf808001 : i32
39 %roundNegOneF = arith.bitcast %roundNegOneI : i32 to f32
40 call @trunc_bf16(%roundNegOneF): (f32) -> ()
43 %infi = arith.constant 0x7f800000 : i32
44 %inff = arith.bitcast %infi : i32 to f32
45 call @trunc_bf16(%inff): (f32) -> ()
48 %neginfi = arith.constant 0xff800000 : i32
49 %neginff = arith.bitcast %neginfi : i32 to f32
50 call @trunc_bf16(%neginff): (f32) -> ()
52 // Note: this rounds upwards. As the mantissa was already saturated, this rounding
53 // causes the exponent to be incremented. As the exponent was already the
54 // maximum exponent value for finite values, this increment of the exponent
55 // causes this to overflow to +inf.
57 %big_overflowing_i = arith.constant 0x7f7fffff : i32
58 %big_overflowing_f = arith.bitcast %big_overflowing_i : i32 to f32
59 call @trunc_bf16(%big_overflowing_f): (f32) -> ()
61 // Same as the previous testcase but negative.
63 %negbig_overflowing_i = arith.constant 0xff7fffff : i32
64 %negbig_overflowing_f = arith.bitcast %negbig_overflowing_i : i32 to f32
65 call @trunc_bf16(%negbig_overflowing_f): (f32) -> ()
67 // In contrast to the previous two testcases, the upwards-rounding here
68 // does not cause overflow.
69 // CHECK-NEXT: 3.38953e+38
70 %big_nonoverflowing_i = arith.constant 0x7f7effff : i32
71 %big_nonoverflowing_f = arith.bitcast %big_nonoverflowing_i : i32 to f32
72 call @trunc_bf16(%big_nonoverflowing_f): (f32) -> ()
75 %exprolli = arith.constant 0x3fcfffff : i32
76 %exprollf = arith.bitcast %exprolli : i32 to f32
77 call @trunc_bf16(%exprollf): (f32) -> ()
80 %exprollnegi = arith.constant 0xbfcfffff : i32
81 %exprollnegf = arith.bitcast %exprollnegi : i32 to f32
82 call @trunc_bf16(%exprollnegf): (f32) -> ()