[flang-commits] [flang] [flang] optimize atand/atan2d precision (PR #154544)

Connector Switch via flang-commits flang-commits at lists.llvm.org
Wed Aug 20 07:16:30 PDT 2025


https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/154544

Part of https://github.com/llvm/llvm-project/issues/150452.

>From 96309771095c2c6808878972565fdbdc73664987 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Wed, 20 Aug 2025 22:15:16 +0800
Subject: [PATCH] [flang] optimize atand/atan2d precision

---
 flang/lib/Optimizer/Builder/IntrinsicCall.cpp |  9 ++---
 flang/test/Lower/Intrinsics/atan2d.f90        | 12 +++----
 flang/test/Lower/Intrinsics/atand.f90         | 35 ++++++++++++++-----
 3 files changed, 37 insertions(+), 19 deletions(-)

diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index 8aacdb1815e3b..6d4133c1c79dd 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -2893,10 +2893,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