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 copysignf16, copysignf128, fabsf16, and fabsf128 #320

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

tgross35
Copy link
Contributor

@tgross35 tgross35 commented Oct 26, 2024

Add generic versions of abs and copysign, which is used to implement f16 and f128 versions of these functions. These currently aren't tested because the builtin musl tests don't provide a reference, but the implementations are straightforward and we will be able to test against #311 once it lands. (Technically musl on aarch64 has fabsl and copysignl as f128, but I don't think it is worth updating the serialization tests).

The second commit ("Add float and integer traits...") just copies the exact trait implementations that we use in compiler-builtins, with the assumption that we will use it more in the future and for testing.

The third commit ("Add f16 and f128 configuration...") copies the logic from compiler-builtins for setting enable_f16 and enable_f128 with the exception that here, the unstable feature must also be enabled. This will probably need to be tweaked a bit in the future so it isn't tied to other behavior enabled by unstable, but this is enough to get us started.

@tgross35 tgross35 force-pushed the generic-algorithms branch 2 times, most recently from eb24e93 to 965cf5a Compare October 26, 2024 06:01
@burrbull
Copy link
Contributor

Using traits blocks us from using functions in const context, is not it?

@tgross35
Copy link
Contributor Author

It would indeed prevent us from making any of the public functions const (none of them are now), but providing const implementations is out of scope for this crate. Instead, there is work that can be done so that the equivalent functions in rust-lang/rust can become const, e.g. const { 100.0f32.cos() } will work.

I have some more details here if you are interested #310

@burrbull
Copy link
Contributor

is out of scope for this crate

Who has decided this?

@tgross35
Copy link
Contributor Author

It has been a goal for a while to make the float math functions available in core, which require various changes in this crate (improving accuracy, testing across all targets, performance, supporting the new float types). Conveniently, figuring out those same things will allow us to make the functions in std/core const. If that gets done then there won't be much use for this crate in its current form - so making that happen seems like a much better time investment.

The existing implementations aren't going away either. Generic methods just make it easier to support f16 and f128, as well as f32 algorithms that don't rely on f64 (for cases where there is no hardfloat f64).

@tgross35 tgross35 changed the title [wip] prepare for generic algorithms Add copysignf16, copysignf128, fabsf16, and fabsf128 Oct 26, 2024
@tgross35 tgross35 marked this pull request as ready for review October 26, 2024 08:44
Don't try to generate tests for directories, or for files that contain
`f16` or `f128` (as these types are not provided by musl's math
implementations).
In preparation of adding generic algorithms to `libm`, add the traits
from `compiler-builtins`.

Eventually we should be able to unify the two crates so we don't have
duplicate implementations.
In preparation of adding routines from these two types, duplicate the
`compiler-builtins` configuration here.
Currently there is a macro called `llvm_intrinsically_optimized` that
uses an intrinsic rather than the function implementation if the
configuration is correct. Add a new macro `select_implementation` that
is somewhat cleaner to use, and make use of it in two relevant
locations.

In the future, we can update this macro with more fields to specify
other implementations that may be selected, such as something
architecture-specific or e.g. using a generic implementation for `f32`
routines, rather than those that convert to `f64`.

This also introduces a `macros` module within `math/support`. We will be
able to move more things here later.
Add generic versions of `abs` and `copysign`. Make use of it for the
`f32` and `f64` versions of these functions.
`cfg_if` is helpful for applying `cfg` attributes to groups of items,
like we will need to do with `f16` and `f128` to properly gate them.
However, `libm` can't have dependencies.

The `cfg_if` macro is small, so just vendor it here.
Use the generic algorithm to make these two functions available whenever
`f16_enabled` or `f128_enabled` are true. These require the `unstable`
feature.
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