[flang-commits] [flang] 4537f0e - [flang] Optimize `atanpi` precision (#153207)

via flang-commits flang-commits at lists.llvm.org
Tue Aug 12 19:05:51 PDT 2025


Author: Connector Switch
Date: 2025-08-13T10:05:48+08:00
New Revision: 4537f0ee61a7791d28d34457a86feb5f84ab366d

URL: https://github.com/llvm/llvm-project/commit/4537f0ee61a7791d28d34457a86feb5f84ab366d
DIFF: https://github.com/llvm/llvm-project/commit/4537f0ee61a7791d28d34457a86feb5f84ab366d.diff

LOG: [flang] Optimize `atanpi` precision (#153207)

Part of #150452.

Added: 
    

Modified: 
    flang/lib/Optimizer/Builder/IntrinsicCall.cpp
    flang/test/Lower/Intrinsics/atan2pi.f90
    flang/test/Lower/Intrinsics/atanpi.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index f2edd39218601..fe415852e7151 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -2905,10 +2905,10 @@ mlir::Value IntrinsicLibrary::genAtanpi(mlir::Type resultType,
         mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
     atan = getRuntimeCallGenerator("atan", ftype)(builder, loc, args);
   }
-  llvm::APFloat inv_pi = llvm::APFloat(llvm::numbers::inv_pi);
-  mlir::Value dfactor =
-      builder.createRealConstant(loc, mlir::Float64Type::get(context), inv_pi);
-  mlir::Value factor = builder.createConvert(loc, resultType, dfactor);
+  llvm::APFloat inv_pi =
+      llvm::APFloat(llvm::cast<mlir::FloatType>(resultType).getFloatSemantics(),
+                    llvm::numbers::inv_pis);
+  mlir::Value factor = builder.createRealConstant(loc, resultType, inv_pi);
   return mlir::arith::MulFOp::create(builder, loc, atan, factor);
 }
 

diff  --git a/flang/test/Lower/Intrinsics/atan2pi.f90 b/flang/test/Lower/Intrinsics/atan2pi.f90
index df72237610609..83039c0b6431e 100644
--- a/flang/test/Lower/Intrinsics/atan2pi.f90
+++ b/flang/test/Lower/Intrinsics/atan2pi.f90
@@ -1,24 +1,34 @@
+! REQUIRES: flang-supports-f128-math
 ! 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 = atan2pi(y,x)
+  test_real4 = atan2pi(y, x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real4
 ! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
-! CHECK: %[[dpi:.*]] = arith.constant 0.31830988618379069 : f64
-! CHECK: %[[inv_pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
+! CHECK: %[[inv_pi:.*]] = arith.constant 0.318309873 : f32
 ! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[inv_pi]] fastmath<contract> : f32
 
-function test_real8(y,x)
+function test_real8(y, x)
   real(8) :: x, y, test_real8
-  test_real8 = atan2pi(y,x)
+  test_real8 = atan2pi(y, x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real8
 ! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
 ! CHECK: %[[inv_pi:.*]] = arith.constant 0.31830988618379069 : f64
 ! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[inv_pi]] fastmath<contract> : f64
+
+function test_real16(y, x)
+  real(16) :: x, y, test_real16
+  test_real16 = atan2pi(y, x)
+end function
+
+! CHECK-LABEL: @_QPtest_real16
+! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f128
+! CHECK: %[[inv_pi:.*]] = arith.constant 0.3183098861837906715377675267450{{.*}} : f128
+! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[inv_pi]] fastmath<contract> : f128

diff  --git a/flang/test/Lower/Intrinsics/atanpi.f90 b/flang/test/Lower/Intrinsics/atanpi.f90
index 6382dbd1a30cf..ece42f9500ce8 100644
--- a/flang/test/Lower/Intrinsics/atanpi.f90
+++ b/flang/test/Lower/Intrinsics/atanpi.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: %[[dpi:.*]] = arith.constant 0.31830988618379069 : f64
-! CHECK: %[[inv_pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
+! CHECK: %[[inv_pi:.*]] = arith.constant 0.318309873 : f32
 ! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[inv_pi]] fastmath<contract> : f32
 
 function test_real8(x)
@@ -25,23 +25,42 @@ function test_real8(x)
 ! CHECK: %[[inv_pi:.*]] = arith.constant 0.31830988618379069 : f64
 ! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[inv_pi]] fastmath<contract> : f64
 
-function test_real4_yx(y,x)
+function test_real16(x)
+  real(16) :: x, test_real16
+  test_real16 = atanpi(x)
+end function
+
+! CHECK-LABEL: @_QPtest_real16
+! CHECK: %[[atan:.*]] = fir.call @_FortranAAtanF128({{.*}}) fastmath<contract> : (f128) -> f128
+! CHECK: %[[inv_pi:.*]] = arith.constant 0.3183098861837906715377675267450{{.*}} : f128
+! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[inv_pi]] fastmath<contract> : f128
+
+function test_real4_yx(y, x)
   real(4) :: x, y, test_real4
-  test_real4 = atanpi(y,x)
+  test_real4 = atanpi(y, x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real4_yx
 ! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
-! CHECK: %[[dpi:.*]] = arith.constant 0.31830988618379069 : f64
-! CHECK: %[[inv_pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
+! CHECK: %[[inv_pi:.*]] = arith.constant 0.318309873 : f32
 ! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[inv_pi]] fastmath<contract> : f32
 
-function test_real8_yx(y,x)
+function test_real8_yx(y, x)
   real(8) :: x, y, test_real8
-  test_real8 = atanpi(y,x)
+  test_real8 = atanpi(y, x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real8_yx
 ! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
 ! CHECK: %[[inv_pi:.*]] = arith.constant 0.31830988618379069 : f64
 ! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[inv_pi]] fastmath<contract> : f64
+
+function test_real16_yx(y, x)
+  real(16) :: x, y, test_real16
+  test_real16 = atanpi(y, x)
+end function
+
+! CHECK-LABEL: @_QPtest_real16_yx
+! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f128
+! CHECK: %[[inv_pi:.*]] = arith.constant 0.3183098861837906715377675267450{{.*}} : f128
+! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[inv_pi]] fastmath<contract> : f128


        


More information about the flang-commits mailing list