Skip to content
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

Add gamma function to f32 and f64 #99747

Merged
merged 2 commits into from
Aug 9, 2023
Merged

Add gamma function to f32 and f64 #99747

merged 2 commits into from
Aug 9, 2023

Conversation

ankane
Copy link
Contributor

@ankane ankane commented Jul 26, 2022

Adds the gamma function to f32 and f64 (tgamma and tgammaf from C).

Refs:

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jul 26, 2022
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @m-ou-se (or someone else) soon.

Please see the contribution instructions for more information.

@rustbot
Copy link
Collaborator

rustbot commented Jul 26, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 26, 2022
@rust-log-analyzer

This comment has been minimized.

@joshtriplett
Copy link
Member

LGTM. Please file a tracking issue, then r=me.

@ankane ankane mentioned this pull request Jul 28, 2022
3 tasks
@ankane
Copy link
Contributor Author

ankane commented Jul 28, 2022

Thanks @joshtriplett, tracking issue added.

@joshtriplett
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jul 28, 2022

📌 Commit 7d2e763 has been approved by joshtriplett

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 28, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Jul 28, 2022
Add gamma function to f32 and f64

Adds the [gamma function](https://en.wikipedia.org/wiki/Gamma_function) to `f32` and `f64` (`tgamma` and `tgammaf` from C).

Refs:
- rust-lang/rfcs#864
- rust-lang#18271
@Dylan-DPC
Copy link
Member

failed in rollup

@Dylan-DPC
Copy link
Member

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 28, 2022
@@ -550,6 +550,21 @@ fn test_atanh() {
assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
}

#[test]
fn test_gamma() {
assert_eq!(0.0f32.gamma(), f32::INFINITY);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test causes compile time errors on platforms like x86_64-fortanix-unknown-sgx and probably others:

12:04:56   = note: rust-lld: error: undefined symbol: tgammaf
12:04:56           >>> referenced by std.143fe29c-cgu.2
12:04:56           >>>               /home/jenkins/workspace/rust-sgx-ci/rust/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-fortanix-unknown-sgx/release/deps/std-e08c880d8c834560.std.143fe29c-cgu.2.rcgu.o:(std::f32::tests::test_gamma::he9b80e8a0cc72b6a)
12:04:56           >>> referenced by std.143fe29c-cgu.2
12:04:56           >>>               /home/jenkins/workspace/rust-sgx-ci/rust/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-fortanix-unknown-sgx/release/deps/std-e08c880d8c834560.std.143fe29c-cgu.2.rcgu.o:(std::f32::tests::test_gamma::he9b80e8a0cc72b6a)
12:04:56           >>> referenced by std.143fe29c-cgu.2
12:04:56           >>>               /home/jenkins/workspace/rust-sgx-ci/rust/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-fortanix-unknown-sgx/release/deps/std-e08c880d8c834560.std.143fe29c-cgu.2.rcgu.o:(std::f32::tests::test_gamma::he9b80e8a0cc72b6a)
12:04:56           >>> referenced 6 more times
12:04:56           
12:04:56           rust-lld: error: undefined symbol: tgamma
12:04:56           >>> referenced by std.143fe29c-cgu.3
12:04:56           >>>               /home/jenkins/workspace/rust-sgx-ci/rust/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-fortanix-unknown-sgx/release/deps/std-e08c880d8c834560.std.143fe29c-cgu.3.rcgu.o:(std::f64::tests::test_gamma::hff115a05914e5bdc)
12:04:56           >>> referenced by std.143fe29c-cgu.3
12:04:56           >>>               /home/jenkins/workspace/rust-sgx-ci/rust/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-fortanix-unknown-sgx/release/deps/std-e08c880d8c834560.std.143fe29c-cgu.3.rcgu.o:(std::f64::tests::test_gamma::hff115a05914e5bdc)
12:04:56           >>> referenced by std.143fe29c-cgu.3
12:04:56           >>>               /home/jenkins/workspace/rust-sgx-ci/rust/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-fortanix-unknown-sgx/release/deps/std-e08c880d8c834560.std.143fe29c-cgu.3.rcgu.o:(std::f64::tests::test_gamma::hff115a05914e5bdc)
12:04:56           >>> referenced 6 more times
12:04:56           
12:04:56 
12:04:56 error: could not compile `std` due to previous error

Looking into it, these tgamma and tgammaf symbols exists, but are not exported from the compiler-builtins crate. These should probably be added here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @raoulstrackx! Submitted a PR there: rust-lang/compiler-builtins#482

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thanks @ankane !

@@ -535,6 +535,21 @@ fn test_atanh() {
assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
}

#[test]
fn test_gamma() {
assert_eq!(0.0f64.gamma(), f64::INFINITY);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Causes similar issues as above

@mati865
Copy link
Contributor

mati865 commented Jul 29, 2022

I think this PR should never be rolled up because it's going to fail on more targets like *-windows-gnu (only non llvm ABI) as MSVCRT doesn't provide these symbols.

@bors rollup=never

@bors
Copy link
Contributor

bors commented Jul 29, 2022

@mati865: 🔑 Insufficient privileges: not in try users

@Dylan-DPC
Copy link
Member

@bors rollup=never

@joshtriplett
Copy link
Member

@mati865 I was expecting that these symbols were being provided by compiler-builtins fallbacks on all platforms. We do need fallbacks for platforms that don't have these.

@mati865
Copy link
Contributor

mati865 commented Jul 29, 2022

@joshtriplett I'm away until next week but mingw-w64 seems to provide own implementation when using MSVCRT but it returns wrong results for certain values. This issue has been fixed 3 years ago but Rust uses 5 years old release.
I hope to open PR with bump next week but merging it may take some time so feel free to search for workarounds (even more extreme ones like disabling those unit tests for windows-gnu).

@ankane
Copy link
Contributor Author

ankane commented Jul 30, 2022

Just bumped compiler_builtins with the exported symbols.

@bors
Copy link
Contributor

bors commented Aug 13, 2022

☔ The latest upstream changes (presumably #100380) made this pull request unmergeable. Please resolve the merge conflicts.

@bors
Copy link
Contributor

bors commented Jul 31, 2023

⌛ Testing commit 793e952d9beb8ed40ecbfa188e37882dfdb15555 with merge ee51e45123e931937c445a123b67d24d3e3cd4fa...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Jul 31, 2023

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 31, 2023
@workingjubilee
Copy link
Member

r? @workingjubilee
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 31, 2023
@ankane
Copy link
Contributor Author

ankane commented Aug 7, 2023

Bumped compiler_builtins

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 7, 2023
@workingjubilee
Copy link
Member

I hope we can solve the underlying problem there, soon. ( And that it doesn't crop up again for this PR... )

@bors r+

@bors
Copy link
Contributor

bors commented Aug 8, 2023

📌 Commit a75e228 has been approved by workingjubilee

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 8, 2023
@bors
Copy link
Contributor

bors commented Aug 9, 2023

⌛ Testing commit a75e228 with merge 8838c73...

@bors
Copy link
Contributor

bors commented Aug 9, 2023

☀️ Test successful - checks-actions
Approved by: workingjubilee
Pushing 8838c73 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Aug 9, 2023
@bors bors merged commit 8838c73 into rust-lang:master Aug 9, 2023
11 checks passed
@rustbot rustbot added this to the 1.73.0 milestone Aug 9, 2023
@ankane
Copy link
Contributor Author

ankane commented Aug 9, 2023

Thanks everyone for helping with this!

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (8838c73): comparison URL.

Overall result: ❌✅ regressions and improvements - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.4% [0.4%, 0.4%] 1
Regressions ❌
(secondary)
0.6% [0.6%, 0.6%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.1% [-1.8%, -0.3%] 2
All ❌✅ (primary) 0.4% [0.4%, 0.4%] 1

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.4% [-2.4%, -2.4%] 1
All ❌✅ (primary) - - 0

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 633.338s -> 632.915s (-0.07%)

unsafe { cmath::tgammaf(self) }
}

/// Returns the natural logarithm of the gamma function.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong, it returns the natural log of the absolute value of the gamma function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[test]
fn test_ln_gamma() {
    assert_approx_eq!(1.0f64.ln_gamma().0, 0.0f64);
    assert_eq!(1.0f64.ln_gamma().1, 1);
    assert_approx_eq!(2.0f64.ln_gamma().0, 0.0f64);
    assert_eq!(2.0f64.ln_gamma().1, 1);
    assert_approx_eq!(3.0f64.ln_gamma().0, 2.0f64.ln());
    assert_eq!(3.0f64.ln_gamma().1, 1);
    assert_approx_eq!((-0.5f64).ln_gamma().0, (2.0 * consts::PI.sqrt()).ln());
    assert_eq!((-0.5f64).ln_gamma().1, -1);
}

? am I just missing something, if so why does the final test case pass...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Γ(-0.5f64) is negative, its natural log should be undefined

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...well, it sounds obvious now that you've said it, so...!
#114754

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.