Break circular dependency between FIR dialect and utilities
[llvm-project.git] / flang / test / Lower / Intrinsics / abs.f90
blob9f6775750d5e87bd33e54c2c2bb4471e9a459d89
1 ! RUN: bbc -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CMPLX,CMPLX-FAST"
2 ! RUN: bbc -emit-fir --math-runtime=precise %s -o - | FileCheck %s --check-prefixes="CMPLX,CMPLX-PRECISE"
3 ! RUN: bbc --disable-mlir-complex -emit-fir %s -o - | FileCheck %s --check-prefixes="CMPLX,CMPLX-PRECISE"
4 ! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CMPLX,CMPLX-FAST"
5 ! RUN: %flang_fc1 -emit-fir -mllvm --math-runtime=precise %s -o - | FileCheck %s --check-prefixes="CMPLX,CMPLX-PRECISE"
6 ! RUN: %flang_fc1 -emit-fir -mllvm --disable-mlir-complex %s -o - | FileCheck %s --check-prefixes="CMPLX,CMPLX-PRECISE"
8 ! Test abs intrinsic for various types (int, float, complex)
10 ! CHECK-LABEL: func @_QPabs_testi
11 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<i32>{{.*}}, %[[VAL_1:.*]]: !fir.ref<i32>
12 subroutine abs_testi(a, b)
13 ! CHECK: %[[VAL_2:.*]] = fir.load %[[VAL_0]] : !fir.ref<i32>
14 ! CHECK: %[[VAL_3:.*]] = arith.constant 31 : i32
15 ! CHECK: %[[VAL_4:.*]] = arith.shrsi %[[VAL_2]], %[[VAL_3]] : i32
16 ! CHECK: %[[VAL_5:.*]] = arith.xori %[[VAL_2]], %[[VAL_4]] : i32
17 ! CHECK: %[[VAL_6:.*]] = arith.subi %[[VAL_5]], %[[VAL_4]] : i32
18 ! CHECK: fir.store %[[VAL_6]] to %[[VAL_1]] : !fir.ref<i32>
19 ! CHECK: return
20 integer :: a, b
21 b = abs(a)
22 end subroutine
24 ! CHECK-LABEL: func @_QPabs_testi16
25 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<i128>{{.*}}, %[[VAL_1:.*]]: !fir.ref<i128>
26 subroutine abs_testi16(a, b)
27 ! CHECK: %[[VAL_2:.*]] = fir.load %[[VAL_0]] : !fir.ref<i128>
28 ! CHECK: %[[VAL_3:.*]] = arith.constant 127 : i128
29 ! CHECK: %[[VAL_4:.*]] = arith.shrsi %[[VAL_2]], %[[VAL_3]] : i128
30 ! CHECK: %[[VAL_5:.*]] = arith.xori %[[VAL_2]], %[[VAL_4]] : i128
31 ! CHECK: %[[VAL_6:.*]] = arith.subi %[[VAL_5]], %[[VAL_4]] : i128
32 ! CHECK: fir.store %[[VAL_6]] to %[[VAL_1]] : !fir.ref<i128>
33 ! CHECK: return
34 integer(kind=16) :: a, b
35 b = abs(a)
36 end subroutine
38 ! CHECK-LABEL: func @_QPabs_testh(
39 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<f16>{{.*}}, %[[VAL_1:.*]]: !fir.ref<f16>{{.*}}) {
40 subroutine abs_testh(a, b)
41 ! CHECK: %[[VAL_2:.*]] = fir.load %[[VAL_0]] : !fir.ref<f16>
42 ! CHECK: %[[VAL_2_1:.*]] = fir.convert %[[VAL_2]] : (f16) -> f32
43 ! CHECK: %[[VAL_3:.*]] = math.absf %[[VAL_2_1]] {{.*}}: f32
44 ! CHECK: %[[VAL_3_1:.*]] = fir.convert %[[VAL_3]] : (f32) -> f16
45 ! CHECK: fir.store %[[VAL_3_1]] to %[[VAL_1]] : !fir.ref<f16>
46 ! CHECK: return
47 real(kind=2) :: a, b
48 b = abs(a)
49 end subroutine
51 ! CHECK-LABEL: func @_QPabs_testb(
52 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<bf16>{{.*}}, %[[VAL_1:.*]]: !fir.ref<bf16>{{.*}}) {
53 subroutine abs_testb(a, b)
54 ! CHECK: %[[VAL_2:.*]] = fir.load %[[VAL_0]] : !fir.ref<bf16>
55 ! CHECK: %[[VAL_2_1:.*]] = fir.convert %[[VAL_2]] : (bf16) -> f32
56 ! CHECK: %[[VAL_3:.*]] = math.absf %[[VAL_2_1]] {{.*}}: f32
57 ! CHECK: %[[VAL_3_1:.*]] = fir.convert %[[VAL_3]] : (f32) -> bf16
58 ! CHECK: fir.store %[[VAL_3_1]] to %[[VAL_1]] : !fir.ref<bf16>
59 ! CHECK: return
60 real(kind=3) :: a, b
61 b = abs(a)
62 end subroutine
64 ! CHECK-LABEL: func @_QPabs_testr(
65 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<f32>{{.*}}, %[[VAL_1:.*]]: !fir.ref<f32>{{.*}}) {
66 subroutine abs_testr(a, b)
67 ! CHECK: %[[VAL_2:.*]] = fir.load %[[VAL_0]] : !fir.ref<f32>
68 ! CHECK: %[[VAL_3:.*]] = math.absf %[[VAL_2]] {{.*}}: f32
69 ! CHECK: fir.store %[[VAL_3]] to %[[VAL_1]] : !fir.ref<f32>
70 ! CHECK: return
71 real :: a, b
72 b = abs(a)
73 end subroutine
75 ! CHECK-LABEL: func @_QPabs_testd(
76 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<f64>{{.*}}, %[[VAL_1:.*]]: !fir.ref<f64>{{.*}}) {
77 subroutine abs_testd(a, b)
78 ! CHECK: %[[VAL_2:.*]] = fir.load %[[VAL_0]] : !fir.ref<f64>
79 ! CHECK: %[[VAL_3:.*]] = math.absf %[[VAL_2]] {{.*}}: f64
80 ! CHECK: fir.store %[[VAL_3]] to %[[VAL_1]] : !fir.ref<f64>
81 ! CHECK: return
82 real(kind=8) :: a, b
83 b = abs(a)
84 end subroutine
86 ! CHECK-LABEL: func @_QPabs_testr16(
87 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<f128>{{.*}}, %[[VAL_1:.*]]: !fir.ref<f128>{{.*}}) {
88 subroutine abs_testr16(a, b)
89 ! CHECK: %[[VAL_2:.*]] = fir.load %[[VAL_0]] : !fir.ref<f128>
90 ! CHECK: %[[VAL_3:.*]] = math.absf %[[VAL_2]] {{.*}}: f128
91 ! CHECK: fir.store %[[VAL_3]] to %[[VAL_1]] : !fir.ref<f128>
92 ! CHECK: return
93 real(kind=16) :: a, b
94 b = abs(a)
95 end subroutine
97 ! CMPLX-LABEL: func @_QPabs_testzr(
98 ! CMPLX-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.complex<4>>{{.*}}, %[[VAL_1:.*]]: !fir.ref<f32>{{.*}}) {
99 subroutine abs_testzr(a, b)
100 ! CMPLX: %[[VAL_2:.*]] = fir.load %[[VAL_0]] : !fir.ref<!fir.complex<4>>
101 ! CMPLX-FAST: %[[VAL_3:.*]] = fir.convert %[[VAL_2]] : (!fir.complex<4>) -> complex<f32>
102 ! CMPLX-FAST: %[[VAL_4:.*]] = complex.abs %[[VAL_3]] : complex<f32>
103 ! CMPLX-PRECISE: %[[VAL_4:.*]] = fir.call @cabsf(%[[VAL_2]]) {{.*}}: (!fir.complex<4>) -> f32
104 ! CMPLX: fir.store %[[VAL_4]] to %[[VAL_1]] : !fir.ref<f32>
105 ! CMPLX: return
106 complex :: a
107 real :: b
108 b = abs(a)
109 end subroutine abs_testzr
111 ! CMPLX-LABEL: func @_QPabs_testzd(
112 ! CMPLX-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.complex<8>>{{.*}}, %[[VAL_1:.*]]: !fir.ref<f64>{{.*}}) {
113 subroutine abs_testzd(a, b)
114 ! CMPLX: %[[VAL_2:.*]] = fir.load %[[VAL_0]] : !fir.ref<!fir.complex<8>>
115 ! CMPLX-FAST: %[[VAL_3:.*]] = fir.convert %[[VAL_2]] : (!fir.complex<8>) -> complex<f64>
116 ! CMPLX-FAST: %[[VAL_4:.*]] = complex.abs %[[VAL_3]] : complex<f64>
117 ! CMPLX-PRECISE: %[[VAL_4:.*]] = fir.call @cabs(%[[VAL_2]]) {{.*}}: (!fir.complex<8>) -> f64
118 ! CMPLX: fir.store %[[VAL_4]] to %[[VAL_1]] : !fir.ref<f64>
119 ! CMPLX: return
120 complex(kind=8) :: a
121 real(kind=8) :: b
122 b = abs(a)
123 end subroutine abs_testzd