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

Make a subset of libm symbols weakly available on all platforms #763

Merged
merged 1 commit into from
Feb 25, 2025

Conversation

tgross35
Copy link
Contributor

018616e ("Always have math functions but with weak linking attribute if we can") made all math symbols available on platforms that support weak linkage. This caused some unexpected regressions, however, because our less accurate and sometimes slow routines were being selected over the system libm, which also tends to be weak 1. Thus, 0fab77e ("Don't include math for unix and wasi targets") was applied to undo these changes on many platforms.

Now that some improvements have been made to libm, add back a subset of these functions:

  • cbrt
  • ceil
  • copysign
  • fabs
  • fdim
  • floor
  • fma
  • fmax
  • fmaximum
  • fmin
  • fminimum
  • fmod
  • rint
  • round
  • roundeven
  • sqrt
  • trunc

This list includes only functions that produce exact results (verified with exhaustive / extensive tests, and also required by IEEE in most cases), and for which benchmarks indicate performance similar to or better than Musl's soft float math routines 1. All except cbrt also have f16 and f128 implementations. Once more routines meet these criteria, we can move them from platform-specific availability to always available.

Once this change makes it to rust-lang/rust, we will also be able to move the relevant functions from std to core.

Footnotes

  1. We still rely on the backend to provide optimized assmebly
    routines when available.

@tgross35
Copy link
Contributor Author

Tests will completely fail until I can update libm (I am holding off until a couple of other compiler-builtins changes make it to rust-lang/rust) but does this seem reasonable @Amanieu?

Cc @Amjad50 as you authored #577

@Amanieu
Copy link
Member

Amanieu commented Feb 23, 2025

Yes, this sounds perfectly reasonable.

@tgross35 tgross35 force-pushed the extern-libm-functions branch 4 times, most recently from 4832ff0 to cbc5f3a Compare February 25, 2025 00:01
018616e ("Always have math functions but with `weak` linking
attribute if we can") made all math symbols available on platforms that
support weak linkage. This caused some unexpected regressions, however,
because our less accurate and sometimes slow routines were being
selected over the system `libm`, which also tends to be weak [1]. Thus,
0fab77e ("Don't include `math` for `unix` and `wasi` targets") was
applied to undo these changes on many platforms.

Now that some improvements have been made to `libm`, add back a subset
of these functions:

* cbrt
* ceil
* copysign
* fabs
* fdim
* floor
* fma
* fmax
* fmaximum
* fmin
* fminimum
* fmod
* rint
* round
* roundeven
* sqrt
* trunc

This list includes only functions that produce exact results (verified
with exhaustive / extensive tests, and also required by IEEE in most
cases), and for which benchmarks indicate performance similar to or
better than Musl's soft float math routines [^1]. All except `cbrt` also
have `f16` and `f128` implementations. Once more routines meet these
criteria, we can move them from platform-specific availability to always
available.

Once this change makes it to rust-lang/rust, we will also be able to
move the relevant functions from `std` to `core`.

[^1]: We still rely on the backend to provide optimized assmebly
      routines when available.

[1]: rust-lang/rust#128386
@tgross35 tgross35 force-pushed the extern-libm-functions branch from cbc5f3a to 31f7b60 Compare February 25, 2025 00:34
tgross35 added a commit to tgross35/rust that referenced this pull request Feb 25, 2025
Since [1], `compiler-builtins` makes a certain set of math symbols
weakly available on all platforms. This means we can begin exposing some
of the related functions in `core`, so begin this process here.

It is not possible to provide inerent methods in both `core` and `std`
while giving them different stability gates, so standalone functions are
added instead. This provides a way to experiment with the functionality
while unstable; once it is time to stabilize, they can be converted to
inherent.

For `f16` and `f128`, everything is unstable so we can move the inherent
methods.

[1]: rust-lang/compiler-builtins#763
@tgross35 tgross35 merged commit 7b62812 into rust-lang:master Feb 25, 2025
26 checks passed
@tgross35 tgross35 deleted the extern-libm-functions branch February 25, 2025 19:11
tgross35 added a commit to tgross35/rust that referenced this pull request Feb 25, 2025
Includes a change to make a subset of math symbols available on all
platforms [1].

[1]: rust-lang/compiler-builtins#763
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 25, 2025
Update `compiler-builtins` to 0.1.149

Includes a change to make a subset of math symbols available on all platforms [1].

[1]: rust-lang/compiler-builtins#763

try-job: aarch64-gnu
try-job: armhf-gnu
try-job: i686-gnu-1
try-job: i686-mingw-1
try-job: i686-mingw-2
try-job: test-various
try-job: x86_64-msvc-1
try-job: x86_64-msvc-2
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 28, 2025
Update `compiler-builtins` to 0.1.149

Includes a change to make a subset of math symbols available on all platforms [1].

[1]: rust-lang/compiler-builtins#763

try-job: aarch64-gnu
try-job: armhf-gnu
try-job: i686-gnu-1
try-job: i686-mingw-1
try-job: i686-mingw-2
try-job: test-various
try-job: x86_64-msvc-1
try-job: x86_64-msvc-2
tgross35 added a commit to tgross35/rust that referenced this pull request Mar 1, 2025
Includes a change to make a subset of math symbols available on all
platforms [1].

[1]: rust-lang/compiler-builtins#763
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 1, 2025
Update `compiler-builtins` to 0.1.149

Includes a change to make a subset of math symbols available on all platforms [1].

[1]: rust-lang/compiler-builtins#763

try-job: dist-various-1
try-job: dist-various-2
try-job: dist-aarch64-linux
try-job: dist-x86_64-linux
tgross35 added a commit to tgross35/rust that referenced this pull request Mar 1, 2025
Includes a change to make a subset of math symbols available on all
platforms [1], and disables `f16` on aarch64 without neon [2].

[1]: rust-lang/compiler-builtins#763
[2]: rust-lang/compiler-builtins#775
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 1, 2025
Update `compiler-builtins` to 0.1.149

Includes a change to make a subset of math symbols available on all platforms [1], and disables `f16` on aarch64 without neon [2].

[1]: rust-lang/compiler-builtins#763
[2]: rust-lang/compiler-builtins#775

try-job: aarch64-gnu
try-job: aarch64-gnu-debug
try-job: armhf-gnu
try-job: dist-various-1
try-job: dist-various-2
try-job: dist-aarch64-linux
try-job: dist-arm-linux
try-job: dist-armv7-linux
try-job: dist-x86_64-linux
try-job: test-various
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Mar 4, 2025
Update `compiler-builtins` to 0.1.149

Includes a change to make a subset of math symbols available on all platforms [1], and disables `f16` on aarch64 without neon [2].

[1]: rust-lang/compiler-builtins#763
[2]: rust-lang/compiler-builtins#775

try-job: aarch64-gnu
try-job: aarch64-gnu-debug
try-job: armhf-gnu
try-job: dist-various-1
try-job: dist-various-2
try-job: dist-aarch64-linux
try-job: dist-arm-linux
try-job: dist-armv7-linux
try-job: dist-x86_64-linux
try-job: test-various
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Mar 4, 2025
Update `compiler-builtins` to 0.1.149

Includes a change to make a subset of math symbols available on all platforms [1], and disables `f16` on aarch64 without neon [2].

[1]: rust-lang/compiler-builtins#763
[2]: rust-lang/compiler-builtins#775

try-job: aarch64-gnu
try-job: aarch64-gnu-debug
try-job: armhf-gnu
try-job: dist-various-1
try-job: dist-various-2
try-job: dist-aarch64-linux
try-job: dist-arm-linux
try-job: dist-armv7-linux
try-job: dist-x86_64-linux
try-job: test-various
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants