Skip to content

Commit 6560adb

Browse files
authored
[flang] optimize atand/atan2d precision (#154544)
Part of #150452.
1 parent 2b46f31 commit 6560adb

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2917,10 +2917,11 @@ mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
29172917
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
29182918
atan = getRuntimeCallGenerator("atan", ftype)(builder, loc, args);
29192919
}
2920-
llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
2921-
mlir::Value dfactor = builder.createRealConstant(
2922-
loc, mlir::Float64Type::get(context), llvm::APFloat(180.0) / pi);
2923-
mlir::Value factor = builder.createConvert(loc, resultType, dfactor);
2920+
const llvm::fltSemantics &fltSem =
2921+
llvm::cast<mlir::FloatType>(resultType).getFloatSemantics();
2922+
llvm::APFloat pi = llvm::APFloat(fltSem, llvm::numbers::pis);
2923+
mlir::Value factor = builder.createRealConstant(
2924+
loc, resultType, llvm::APFloat(fltSem, "180.0") / pi);
29242925
return mlir::arith::MulFOp::create(builder, loc, atan, factor);
29252926
}
29262927

flang/test/Lower/Intrinsics/atan2d.f90

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
22
! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
33

4-
5-
function test_real4(y,x)
4+
function test_real4(y, x)
65
real(4) :: x, y, test_real4
7-
test_real4 = atan2d(y,x)
6+
test_real4 = atan2d(y, x)
87
end function
98

109
! CHECK-LABEL: @_QPtest_real4
1110
! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
12-
! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
13-
! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
11+
! CHECK: %[[factor:.*]] = arith.constant 57.2957763 : f32
1412
! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath<contract> : f32
1513

16-
function test_real8(y,x)
14+
function test_real8(y, x)
1715
real(8) :: x, y, test_real8
18-
test_real8 = atan2d(y,x)
16+
test_real8 = atan2d(y, x)
1917
end function
2018

2119
! CHECK-LABEL: @_QPtest_real8

flang/test/Lower/Intrinsics/atand.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: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
14-
! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
14+
! CHECK: %[[factor:.*]] = arith.constant 57.2957763 : f32
1515
! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] fastmath<contract> : f32
1616

1717
function test_real8(x)
@@ -25,23 +25,42 @@ function test_real8(x)
2525
! CHECK: %[[factor:.*]] = arith.constant 57.295779513082323 : f64
2626
! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] 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 = atand(x)
31+
end function
32+
33+
! CHECK-LABEL: @_QPtest_real16
34+
! CHECK: %[[atan:.*]] = fir.call @_FortranAAtanF128({{.*}}) fastmath<contract> : (f128) -> f128
35+
! CHECK: %[[factor:.*]] = arith.constant 57.295779513082320876798154814105{{.*}} : f128
36+
! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] fastmath<contract> : f128
37+
38+
function test_real4_yx(y, x)
2939
real(4) :: x, y, test_real4
30-
test_real4 = atand(y,x)
40+
test_real4 = atand(y, x)
3141
end function
3242

3343
! CHECK-LABEL: @_QPtest_real4_yx
3444
! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
35-
! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
36-
! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
45+
! CHECK: %[[factor:.*]] = arith.constant 57.2957763 : f32
3746
! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] 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 = atand(y,x)
50+
test_real8 = atand(y, x)
4251
end function
4352

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

0 commit comments

Comments
 (0)