-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[flang] Implement sinpi
#149525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[flang] Implement sinpi
#149525
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK" | ||
|
||
function test_real4(x) | ||
real :: x, test_real4 | ||
test_real4 = sinpi(x) | ||
end function | ||
|
||
! CHECK-LABEL: @_QPtest_real4 | ||
! CHECK: %[[dfactor:.*]] = arith.constant 3.1415926535897931 : f64 | ||
! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32 | ||
! CHECK: %[[mul:.*]] = arith.mulf %{{.*}}, %[[factor]] fastmath<contract> : f32 | ||
! CHECK: %[[sin:.*]] = math.sin %[[mul]] fastmath<contract> : f32 | ||
|
||
function test_real8(x) | ||
real(8) :: x, test_real8 | ||
test_real8 = sinpi(x) | ||
end function | ||
|
||
! CHECK-LABEL: @_QPtest_real8 | ||
! CHECK: %[[dfactor:.*]] = arith.constant 3.1415926535897931 : f64 | ||
! CHECK: %[[mul:.*]] = arith.mulf %{{.*}}, %[[dfactor]] fastmath<contract> : f64 | ||
! CHECK: %[[sin:.*]] = math.sin %[[mul]] fastmath<contract> : f64 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not create the real constant with the argument type in the first place? This might matter for real(16).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the argument type in the creation process seems to result in the following error:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears these two floating-point types have different semantics. To ensure pi uses the same semantics as the argument type, we'd still need to convert it from APFloat. Either way, a conversion seems necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh okay what I think is happening here is that APFloat is forcing its size to f64 because
llvm::numbers::pi
is defined as a double. I'm concerned that this could produce inaccurate results for real(16) (aka floating point types with more precision thanf64
: f128 on aarch64 systems, I think f80 on x86).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah so I think the ultimate solution for trig-pi functions is to use the glibc version, when we have glibc>=2.41(it does provide f128 version). And if not maybe we can use this as a fallback or something like libquadmath. Or maybe we need a interface return
pi
based on fltSemantic. Not quite sure how to do it right since there are a lot of them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, handling these is somewhat tricky. I'm not exactly sure how to handle this correctly in flang. I am currently working on adding sinpi-related functions to libquadmath in the GCC source tree. The GCC maintainers are not satisfied with the precision of the generic implementation imported from glibc, so this patch has not been merged yet.
C23 has added support for sinpi-related functions, and eventually libc will also have support. This will allow us to achieve better precision. If neither glibc nor libquadmath have it, we can fallback to our current approach.
However, glibc does not provide sind-related functions. We just need to fix the constant pi type later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you may follow the implementation of
SinF128
as Tom suggested. As long aslibquadmath
does not supportsinpi
forfloat128
you may implementSinpiF128
inflang-rt/lib/quadmath
same way you implement it here but using properPI
definition from https://gcc.gnu.org/onlinedocs/libquadmath/Typedef-and-constants.htmlThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a good approach for handling the f128 case. However, if we implement this logic in libquadmath, will it diverge from how we handle f32/f64 here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Diverge in what sense?
If we "lower" the f32/f64 cases here as
sin(PI * x)
, and the f128 case in Flang'squadmath
assinq(M_PIq * x)
, then they should be pretty consistent, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you're right. I get it. That seems like a good approach.