[flang-commits] [flang] 6560adb - [flang] optimize atand/atan2d precision (#154544)
via flang-commits
flang-commits at lists.llvm.org
Fri Aug 22 00:55:49 PDT 2025
Author: Connector Switch
Date: 2025-08-22T15:55:46+08:00
New Revision: 6560adb5840596afbeb3f4574bf6d0c52e439d69
URL: https://github.com/llvm/llvm-project/commit/6560adb5840596afbeb3f4574bf6d0c52e439d69
DIFF: https://github.com/llvm/llvm-project/commit/6560adb5840596afbeb3f4574bf6d0c52e439d69.diff
LOG: [flang] optimize atand/atan2d precision (#154544)
Part of https://github.com/llvm/llvm-project/issues/150452.
Added:
Modified:
flang/lib/Optimizer/Builder/IntrinsicCall.cpp
flang/test/Lower/Intrinsics/atan2d.f90
flang/test/Lower/Intrinsics/atand.f90
Removed:
################################################################################
diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index cc3ae31ff4668..7e987de94c5d5 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -2917,10 +2917,11 @@ mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
atan = getRuntimeCallGenerator("atan", ftype)(builder, loc, args);
}
- llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
- mlir::Value dfactor = builder.createRealConstant(
- loc, mlir::Float64Type::get(context), llvm::APFloat(180.0) / pi);
- mlir::Value factor = builder.createConvert(loc, resultType, dfactor);
+ const llvm::fltSemantics &fltSem =
+ llvm::cast<mlir::FloatType>(resultType).getFloatSemantics();
+ llvm::APFloat pi = llvm::APFloat(fltSem, llvm::numbers::pis);
+ mlir::Value factor = builder.createRealConstant(
+ loc, resultType, llvm::APFloat(fltSem, "180.0") / pi);
return mlir::arith::MulFOp::create(builder, loc, atan, factor);
}
diff --git a/flang/test/Lower/Intrinsics/atan2d.f90 b/flang/test/Lower/Intrinsics/atan2d.f90
index 6ebf2976266ab..ea9174243c457 100644
--- a/flang/test/Lower/Intrinsics/atan2d.f90
+++ b/flang/test/Lower/Intrinsics/atan2d.f90
@@ -1,21 +1,19 @@
! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
-
-function test_real4(y,x)
+function test_real4(y, x)
real(4) :: x, y, test_real4
- test_real4 = atan2d(y,x)
+ test_real4 = atan2d(y, x)
end function
! CHECK-LABEL: @_QPtest_real4
! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
-! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
-! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
+! CHECK: %[[factor:.*]] = arith.constant 57.2957763 : f32
! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath<contract> : f32
-function test_real8(y,x)
+function test_real8(y, x)
real(8) :: x, y, test_real8
- test_real8 = atan2d(y,x)
+ test_real8 = atan2d(y, x)
end function
! CHECK-LABEL: @_QPtest_real8
diff --git a/flang/test/Lower/Intrinsics/atand.f90 b/flang/test/Lower/Intrinsics/atand.f90
index 07ea56eff1e49..45303228a42b5 100644
--- a/flang/test/Lower/Intrinsics/atand.f90
+++ b/flang/test/Lower/Intrinsics/atand.f90
@@ -1,3 +1,4 @@
+! REQUIRES: flang-supports-f128-math
! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
! RUN: bbc --math-runtime=precise -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE"
! 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)
! CHECK-LABEL: @_QPtest_real4
! CHECK-PRECISE: %[[atan:.*]] = fir.call @atanf({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f32) -> f32
! CHECK-FAST: %[[atan:.*]] = math.atan %{{.*}} : f32
-! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
-! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
+! CHECK: %[[factor:.*]] = arith.constant 57.2957763 : f32
! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] fastmath<contract> : f32
function test_real8(x)
@@ -25,23 +25,42 @@ function test_real8(x)
! CHECK: %[[factor:.*]] = arith.constant 57.295779513082323 : f64
! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] fastmath<contract> : f64
-function test_real4_yx(y,x)
+function test_real16(x)
+ real(16) :: x, test_real16
+ test_real16 = atand(x)
+end function
+
+! CHECK-LABEL: @_QPtest_real16
+! CHECK: %[[atan:.*]] = fir.call @_FortranAAtanF128({{.*}}) fastmath<contract> : (f128) -> f128
+! CHECK: %[[factor:.*]] = arith.constant 57.295779513082320876798154814105{{.*}} : f128
+! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] fastmath<contract> : f128
+
+function test_real4_yx(y, x)
real(4) :: x, y, test_real4
- test_real4 = atand(y,x)
+ test_real4 = atand(y, x)
end function
! CHECK-LABEL: @_QPtest_real4_yx
! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
-! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
-! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
+! CHECK: %[[factor:.*]] = arith.constant 57.2957763 : f32
! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath<contract> : f32
-function test_real8_yx(y,x)
+function test_real8_yx(y, x)
real(8) :: x, y, test_real8
- test_real8 = atand(y,x)
+ test_real8 = atand(y, x)
end function
! CHECK-LABEL: @_QPtest_real8_yx
! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
! CHECK: %[[factor:.*]] = arith.constant 57.295779513082323 : f64
! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath<contract> : f64
+
+function test_real16_yx(y, x)
+ real(16) :: x, y, test_real16
+ test_real16 = atand(y, x)
+end function
+
+! CHECK-LABEL: @_QPtest_real16_yx
+! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f128
+! CHECK: %[[factor:.*]] = arith.constant 57.295779513082320876798154814105{{.*}} : f128
+! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] fastmath<contract> : f128
More information about the flang-commits
mailing list