blob: d116606794cc7d20e41e327a31a76d0e5f8adbde [file] [log] [blame]
Jakub Kuderskic6c713c2023-04-21 18:36:38 -04001func.func @narrow_int_i32_i8() {
2 %input = util.unfoldable_constant dense<[-42, 0, 42]> : tensor<3xi32>
3 %res = "stablehlo.convert"(%input) : (tensor<3xi32>) -> tensor<3xi8>
4 check.expect_eq_const(%res, dense<[-42, 0, 42]> : tensor<3xi8>) : tensor<3xi8>
5 return
6}
7
8func.func @widen_int_i8_i32() {
9 %input = util.unfoldable_constant dense<[-42, 0, 42]> : tensor<3xi8>
10 %res = "stablehlo.convert"(%input) : (tensor<3xi8>) -> tensor<3xi32>
11 check.expect_eq_const(%res, dense<[-42, 0, 42]> : tensor<3xi32>) : tensor<3xi32>
12 return
13}
14
15func.func @narrow_int_i32_i16() {
16 %input = util.unfoldable_constant dense<[-42, 0, 42]> : tensor<3xi32>
17 %res = "stablehlo.convert"(%input) : (tensor<3xi32>) -> tensor<3xi16>
18 check.expect_eq_const(%res, dense<[-42, 0, 42]> : tensor<3xi16>) : tensor<3xi16>
19 return
20}
21
22func.func @widen_int_i16_i32() {
23 %input = util.unfoldable_constant dense<[-42, 0, 42]> : tensor<3xi16>
24 %res = "stablehlo.convert"(%input) : (tensor<3xi16>) -> tensor<3xi32>
25 check.expect_eq_const(%res, dense<[-42, 0, 42]> : tensor<3xi32>) : tensor<3xi32>
26 return
27}
28
29func.func @narrow_int_i64_i32() {
30 %input = util.unfoldable_constant dense<[-42, 0, 42]> : tensor<3xi64>
31 %res = "stablehlo.convert"(%input) : (tensor<3xi64>) -> tensor<3xi32>
32 check.expect_eq_const(%res, dense<[-42, 0, 42]> : tensor<3xi32>) : tensor<3xi32>
33 return
34}
35
36func.func @widen_int_i32_i64() {
37 %input = util.unfoldable_constant dense<[-42, 0, 42]> : tensor<3xi32>
38 %res = "stablehlo.convert"(%input) : (tensor<3xi32>) -> tensor<3xi64>
39 check.expect_eq_const(%res, dense<[-42, 0, 42]> : tensor<3xi64>) : tensor<3xi64>
40 return
41}
42
43func.func @int_to_float() {
44 %input = util.unfoldable_constant dense<[-42, 0, 42]> : tensor<3xi32>
45 %res = "stablehlo.convert"(%input) : (tensor<3xi32>) -> tensor<3xf32>
46 check.expect_almost_eq_const(%res, dense<[-42.0, 0.0, 42.0]> : tensor<3xf32>) : tensor<3xf32>
47 return
48}
49
50// TODO(#6160): XLA does not specify the rounding behavior, meaning that we
51// can't test something like -10.5 as that could be -11 (roundf) or -10 (rint
52// with round-to-even mode).
53//
54// For casting rules, see
55// https://www.tensorflow.org/xla/operation_semantics#convertelementtype
56// func.func @float_to_int() {
57// %input = util.unfoldable_constant dense<[-10.5, -4.4, 4.4, 10.5]> : tensor<4xf32>
58// %res = "stablehlo.convert"(%input) : (tensor<4xf32>) -> tensor<4xi32>
59// check.expect_eq_const(%res, dense<[-10, -4, 4, 10]> : tensor<4xi32>) : tensor<4xi32>
60// return
61// }