Skip to content

Commit 4537f0e

Browse files
authored
[flang] Optimize atanpi precision (llvm#153207)
Part of llvm#150452.
1 parent c664ce4 commit 4537f0e

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2905,10 +2905,10 @@ mlir::Value IntrinsicLibrary::genAtanpi(mlir::Type resultType,
29052905
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
29062906
atan = getRuntimeCallGenerator("atan", ftype)(builder, loc, args);
29072907
}
2908-
llvm::APFloat inv_pi = llvm::APFloat(llvm::numbers::inv_pi);
2909-
mlir::Value dfactor =
2910-
builder.createRealConstant(loc, mlir::Float64Type::get(context), inv_pi);
2911-
mlir::Value factor = builder.createConvert(loc, resultType, dfactor);
2908+
llvm::APFloat inv_pi =
2909+
llvm::APFloat(llvm::cast<mlir::FloatType>(resultType).getFloatSemantics(),
2910+
llvm::numbers::inv_pis);
2911+
mlir::Value factor = builder.createRealConstant(loc, resultType, inv_pi);
29122912
return mlir::arith::MulFOp::create(builder, loc, atan, factor);
29132913
}
29142914

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
1+
! REQUIRES: flang-supports-f128-math
12
! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
23
! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
34

45

5-
function test_real4(y,x)
6+
function test_real4(y, x)
67
real(4) :: x, y, test_real4
7-
test_real4 = atan2pi(y,x)
8+
test_real4 = atan2pi(y, x)
89
end function
910

1011
! CHECK-LABEL: @_QPtest_real4
1112
! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
12-
! CHECK: %[[dpi:.*]] = arith.constant 0.31830988618379069 : f64
13-
! CHECK: %[[inv_pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
13+
! CHECK: %[[inv_pi:.*]] = arith.constant 0.318309873 : f32
1414
! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[inv_pi]] fastmath<contract> : f32
1515

16-
function test_real8(y,x)
16+
function test_real8(y, x)
1717
real(8) :: x, y, test_real8
18-
test_real8 = atan2pi(y,x)
18+
test_real8 = atan2pi(y, x)
1919
end function
2020

2121
! CHECK-LABEL: @_QPtest_real8
2222
! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
2323
! CHECK: %[[inv_pi:.*]] = arith.constant 0.31830988618379069 : f64
2424
! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[inv_pi]] fastmath<contract> : f64
25+
26+
function test_real16(y, x)
27+
real(16) :: x, y, test_real16
28+
test_real16 = atan2pi(y, x)
29+
end function
30+
31+
! CHECK-LABEL: @_QPtest_real16
32+
! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f128
33+
! CHECK: %[[inv_pi:.*]] = arith.constant 0.3183098861837906715377675267450{{.*}} : f128
34+
! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[inv_pi]] fastmath<contract> : f128

flang/test/Lower/Intrinsics/atanpi.f90

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
! REQUIRES: flang-supports-f128-math
12
! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
23
! RUN: bbc --math-runtime=precise -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE"
34
! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
@@ -10,8 +11,7 @@ function test_real4(x)
1011
! CHECK-LABEL: @_QPtest_real4
1112
! CHECK-PRECISE: %[[atan:.*]] = fir.call @atanf({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f32) -> f32
1213
! CHECK-FAST: %[[atan:.*]] = math.atan %{{.*}} : f32
13-
! CHECK: %[[dpi:.*]] = arith.constant 0.31830988618379069 : f64
14-
! CHECK: %[[inv_pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
14+
! CHECK: %[[inv_pi:.*]] = arith.constant 0.318309873 : f32
1515
! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[inv_pi]] fastmath<contract> : f32
1616

1717
function test_real8(x)
@@ -25,23 +25,42 @@ function test_real8(x)
2525
! CHECK: %[[inv_pi:.*]] = arith.constant 0.31830988618379069 : f64
2626
! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[inv_pi]] fastmath<contract> : f64
2727

28-
function test_real4_yx(y,x)
28+
function test_real16(x)
29+
real(16) :: x, test_real16
30+
test_real16 = atanpi(x)
31+
end function
32+
33+
! CHECK-LABEL: @_QPtest_real16
34+
! CHECK: %[[atan:.*]] = fir.call @_FortranAAtanF128({{.*}}) fastmath<contract> : (f128) -> f128
35+
! CHECK: %[[inv_pi:.*]] = arith.constant 0.3183098861837906715377675267450{{.*}} : f128
36+
! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[inv_pi]] fastmath<contract> : f128
37+
38+
function test_real4_yx(y, x)
2939
real(4) :: x, y, test_real4
30-
test_real4 = atanpi(y,x)
40+
test_real4 = atanpi(y, x)
3141
end function
3242

3343
! CHECK-LABEL: @_QPtest_real4_yx
3444
! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
35-
! CHECK: %[[dpi:.*]] = arith.constant 0.31830988618379069 : f64
36-
! CHECK: %[[inv_pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
45+
! CHECK: %[[inv_pi:.*]] = arith.constant 0.318309873 : f32
3746
! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[inv_pi]] fastmath<contract> : f32
3847

39-
function test_real8_yx(y,x)
48+
function test_real8_yx(y, x)
4049
real(8) :: x, y, test_real8
41-
test_real8 = atanpi(y,x)
50+
test_real8 = atanpi(y, x)
4251
end function
4352

4453
! CHECK-LABEL: @_QPtest_real8_yx
4554
! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
4655
! CHECK: %[[inv_pi:.*]] = arith.constant 0.31830988618379069 : f64
4756
! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[inv_pi]] fastmath<contract> : f64
57+
58+
function test_real16_yx(y, x)
59+
real(16) :: x, y, test_real16
60+
test_real16 = atanpi(y, x)
61+
end function
62+
63+
! CHECK-LABEL: @_QPtest_real16_yx
64+
! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f128
65+
! CHECK: %[[inv_pi:.*]] = arith.constant 0.3183098861837906715377675267450{{.*}} : f128
66+
! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[inv_pi]] fastmath<contract> : f128

0 commit comments

Comments
 (0)