Skip to content

Figure out behavior of powf/powi(sNaN, 0) #4286

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

Open
RalfJung opened this issue Apr 22, 2025 · 4 comments
Open

Figure out behavior of powf/powi(sNaN, 0) #4286

RalfJung opened this issue Apr 22, 2025 · 4 comments
Labels
A-shims Area: This affects the external function shims C-spec-question Category: it is unclear what the intended behavior of Miri for this case is

Comments

@RalfJung
Copy link
Member

RalfJung commented Apr 22, 2025

The C standard annex F.10 says that the result of x^0 = 1 for all x, even a NaN. For pown it explicitly calls out "for all x not a signalling NaN" (and doesn't say what happens on a signalling NaN); for powf it just says "any x, even a NaN" -- it is unclear whether that is an intentional difference or not since C generally leaves everything about the behavior of signalling NaNs unspecified. I am not sure what the IEEE spec says. This is a somewhat odd case as usually in C, operations behave the same for qNaN and sNaN, but some widely-used libc implementations return a qNaN for sNan^0 but return 1 for qNan^0 (the latter part being required by the standard), therefore clearly distinguishing qNaN and sNaN.

The "easiest" choice for us is to say that our functions return either 1 or a NaN non-deterministically, and if they do return a NaN the usual NaN propagation rules apply -- since one input is an sNaN, the payload if the returned NaN is completely arbitrary (in particular, it can be a qNaN or an sNaN). glibc and musl seem to disagree on the result here (see https://rust.godbolt.org/z/chsbv5v4d), so requiring more consistent behavior would involve convincing one of them that they are wrong.

@tgross35 does that sound reasonable? Should https://doc.rust-lang.org/nightly/std/primitive.f32.html#method.powf talk and friends about this case or is that not worth it?

@RalfJung RalfJung changed the title Figure out behaviior of powf/powi(sNaN, 0) Figure out behavior of powf/powi(sNaN, 0) Apr 22, 2025
@RalfJung RalfJung added A-shims Area: This affects the external function shims C-spec-question Category: it is unclear what the intended behavior of Miri for this case is labels Apr 22, 2025
@tgross35
Copy link
Contributor

The "easiest" choice for us is to say that our functions return either 1 or a NaN non-deterministically, and if they do return a NaN the usual NaN propagation rules apply -- since one input is an sNaN, the payload if the returned NaN is completely arbitrary (in particular, it can be a qNaN or an sNaN).

Agreed that this is probably the best we can do here for now. If we want more deterministic behavior, the best thing to do would probably be to quiet sNaNs ourselves in this and any other functions where behavior seems to vary - though needing to pay for this would be a bit unfortunate.

Cc rust-lang/rust#138062 (comment) where there was a bit more discussion. Asking on the glibc mailing list would probably still be a good idea (@LorrensP-2158466 you didn't happen to reach out already did you?)

@LorrensP-2158466
Copy link
Contributor

I have not, sorry. It's hectic at school at the moment. I'll try this weekend if that's okey.

@tgross35
Copy link
Contributor

I have not, sorry. It's hectic at school at the moment. I'll try this weekend if that's okey.

This is open source, there are no deadlines and school is important :) reach out whenever you get the chance, or I'll write something up if you don't get to it.

@LorrensP-2158466
Copy link
Contributor

Yeah :) I forget that sometimes. I'll keep u guys updated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-shims Area: This affects the external function shims C-spec-question Category: it is unclear what the intended behavior of Miri for this case is
Projects
None yet
Development

No branches or pull requests

3 participants