Skip to content

Commit 6072fc1

Browse files
authored
[flang] optimize cosd precision (#154955)
Part of #150452.
1 parent 5d17296 commit 6072fc1

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3707,10 +3707,11 @@ mlir::Value IntrinsicLibrary::genCosd(mlir::Type resultType,
37073707
mlir::MLIRContext *context = builder.getContext();
37083708
mlir::FunctionType ftype =
37093709
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
3710-
llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
3711-
mlir::Value dfactor = builder.createRealConstant(
3712-
loc, mlir::Float64Type::get(context), pi / llvm::APFloat(180.0));
3713-
mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
3710+
const llvm::fltSemantics &fltSem =
3711+
llvm::cast<mlir::FloatType>(resultType).getFloatSemantics();
3712+
llvm::APFloat pi = llvm::APFloat(fltSem, llvm::numbers::pis);
3713+
mlir::Value factor = builder.createRealConstant(
3714+
loc, resultType, pi / llvm::APFloat(fltSem, "180.0"));
37143715
mlir::Value arg = mlir::arith::MulFOp::create(builder, loc, args[0], factor);
37153716
return getRuntimeCallGenerator("cos", ftype)(builder, loc, {arg});
37163717
}

flang/test/Lower/Intrinsics/cosd.f90

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
! REQUIRES: flang-supports-f128-math
12
! RUN: bbc -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
23
! RUN: bbc --math-runtime=precise -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE"
34
! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
@@ -8,8 +9,7 @@ function test_real4(x)
89
end function
910

1011
! CHECK-LABEL: @_QPtest_real4
11-
! CHECK: %[[dfactor:.*]] = arith.constant 0.017453292519943295 : f64
12-
! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
12+
! CHECK: %[[factor:.*]] = arith.constant 0.0174532924 : f32
1313
! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath<contract> : f32
1414
! CHECK-PRECISE: %{{.*}} = fir.call @cosf(%[[arg]]) fastmath<contract> : (f32) -> f32
1515
! CHECK-FAST: %{{.*}} = math.cos %[[arg]] fastmath<contract> : f32
@@ -24,3 +24,13 @@ function test_real8(x)
2424
! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath<contract> : f64
2525
! CHECK-PRECISE: %{{.*}} = fir.call @cos(%[[arg]]) fastmath<contract> : (f64) -> f64
2626
! CHECK-FAST: %{{.*}} = math.cos %[[arg]] fastmath<contract> : f64
27+
28+
function test_real16(x)
29+
real(16) :: x, test_real16
30+
test_real16 = cosd(x)
31+
end function
32+
33+
! CHECK-LABEL: @_QPtest_real16
34+
! CHECK: %[[factor:.*]] = arith.constant 0.0174532925199432957692369076848861{{.*}} : f128
35+
! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath<contract> : f128
36+
! CHECK: %[[result:.*]] = fir.call @_FortranACosF128({{.*}}) fastmath<contract> : (f128) -> f128

0 commit comments

Comments
 (0)