Skip to content

Use core::intrinsics::powi{f32,f64} for powi instead of num-traits - #666

Merged
Firestar99 merged 1 commit into
Rust-GPU:mainfrom
mikwielgus:core-intrinsics-powi
Oct 7, 2026
Merged

Firestar99 merged 1 commit into
Rust-GPU:mainfrom
mikwielgus:core-intrinsics-powi

Conversation

@mikwielgus

@mikwielgus mikwielgus commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

This undoes 8eecf61 code changes, except for the powi.rs test, which is retained.

To provide powi in a no-std environment, instead of pulling it from the num-traits crate and defining a custom intrinsic for it, we just define powi methods on f32 and f64 directly. In these methods, we just emit core::intrinsics::powif32 and core::intrinsics::powif64, respectively.

We can do this thanks to the #[rustc_allow_incoherent_impl] internal rustc attribute, which allows us to add inherent impl blocks on types defined in other crates, which would otherwise violate coherence rules.

(This trait is similarly used inside the Rust compiler to have core module provide some alternative implementations of std's methods in a no-std environment.)

In a normal Rust library, the use of such an internal rustc attribute would have been unacceptable, but since Rust-GPU is a compiler, it seems reasonable.

Unlike f32 and f64, there is no need to implement powi for f16 and f128 because these already have powi methods correctly implemented in core (see https://doc.rust-lang.org/core/primitive.f16.html#method.powi and https://doc.rust-lang.org/core/primitive.f128.html#method.powi). And f32 and f64 actually will have these too, once Rust issue #137578 (rust-lang/rust#137578) is resolved, rendering this commit obsolete. But noone knows how many years it will take them to get that going, so let's just fix the problem on Rust-GPU's end.

There are other mathematical methods, e.g. powf, sqrt, and many others, that Rust-GPU still needs num-traits for. I will be moving their implementations to Rust-GPU and removing num-traits altogether in subsequent commits, after and if this PR is accepted -- this is just the first step to see if this solution is acceptable at all.

I ran cargo test, cargo compiletest, cargo difftest. Tests pass. powi.rs test's output changed, but only marginally -- I have blessed it.

Reference: #520

This undoes 8eecf61 code changes,
except for the `powi.rs` test, which is retained.

To provide `powi` in a no-`std` environment, instead of pulling
it from the `num-traits` crate and defining a custom intrinsic for
it, we just define `powi` methods on `f32` and `f64`
directly. In these methods, we just emit `core::intrinsics::powif32` and
`core::intrinsics::powif64`, respectively.

We can do this thanks to the `#[rustc_allow_incoherent_impl]` internal
rustc attribute, which allows us to add inherent `impl` blocks on types
defined in other crates, which would otherwise violate coherence rules.

(This trait is similarly used inside the Rust compiler to have `core`
module provide some alternative implementations of `std`'s methods
in a no-`std` environment.)

In a normal Rust library, the use of such an internal rustc attribute
would have been unacceptable, but since Rust-GPU is a compiler, it seems
reasonable.

Unlike `f32` and `f64`, there is no need to implement `powi`
for `f16` and `f128` because these already have `powi`
methods correctly implemented in `core` (see
https://doc.rust-lang.org/core/primitive.f16.html#method.powi and
https://doc.rust-lang.org/core/primitive.f128.html#method.powi). And
`f32` and `f64` actually will have these too, once Rust issue #137578
(rust-lang/rust#137578) is resolved, rendering
this commit obsolete. But noone knows how many years it will take them
to get that going, so let's just fix the problem on Rust-GPU's end.

There are other mathematical methods, e.g. `powf`, `sqrt`, and many
others, that Rust-GPU still needs `num-traits` for. I will be moving
their implementations to Rust-GPU and removing `num-traits` altogether
in subsequent commits, after and if this PR is accepted -- this is just
the first step to see if this solution is acceptable at all.

I ran `cargo test`, `cargo compiletest`, `cargo difftest`. Tests pass.
`powi.rs` test's output changed, but only marginally -- I have blessed
it.

Reference: Rust-GPU#520
@mikwielgus
mikwielgus force-pushed the core-intrinsics-powi branch from a26809b to 37df3bf Compare October 7, 2026 00:09
@LegNeato

LegNeato commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

Neat, I did not know about rustc_allow_incoherent_impl!

@Firestar99 Firestar99 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the patch, looks good from my side!

@Firestar99
Firestar99 added this pull request to the merge queue Oct 7, 2026
Merged via the queue into Rust-GPU:main with commit c087f99 Oct 7, 2026
24 checks passed
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.

3 participants