Approximate reciprocal and reciprocal-square-root mismatch
For selected subnormal inputs and underflow boundaries, Miri differs from the native reciprocal and reciprocal-square-root instructions.
Program
use std::arch::x86_64::*;
fn main() { unsafe {
for bits in [0x00400000u32, 0x80400000, 0x7f7fffff, 0xff7fffff] {
let v = _mm_set_ss(f32::from_bits(bits));
println!("rcp input={bits:08x} output={:08x}", _mm_cvtss_f32(_mm_rcp_ss(v)).to_bits());
}
for bits in [0x00400000u32, 0x80400000] {
let v = _mm_set_ss(f32::from_bits(bits));
println!("rsqrt input={bits:08x} output={:08x}", _mm_cvtss_f32(_mm_rsqrt_ss(v)).to_bits());
}
} }
Output
native:
rcp input=00400000 output=7f800000
rcp input=80400000 output=ff800000
rcp input=7f7fffff output=00000000
rcp input=ff7fffff output=80000000
rsqrt input=00400000 output=7f800000
rsqrt input=80400000 output=ff800000
miri:
rcp input=00400000 output=7f000485
rcp input=80400000 output=ff00030c
rcp input=7f7fffff output=0020006f
rcp input=ff7fffff output=801ffef1
rsqrt input=00400000 output=5f35047e
rsqrt input=80400000 output=7fc00000
All six mismatches come from two rules in the Intel SDM Vol. 2 (order number 325383-093US) that Miri doesn't implement.
@rustbot claim
My understanding is that these operations are approximate so the mismatch is not a bug.
@programskillforverification which rules are you referring to? The link is not helpful, it links to the beginning of the document. The Intel docs I could find only say
The maximum relative error for this approximation is less than 1.5*2^-12.
My understanding is that these operations are approximate so the mismatch is not a bug.
@programskillforverification which rules are you referring to? The link is not helpful, it links to the beginning of the document. The Intel docs I could find only say
The maximum relative error for this approximation is less than 1.5*2^-12.
Download the volums 2B, you can find RCPPS p. 4-538, RCPSS p. 4-540, RSQRTPS p. 4-594, RSQRTSS p. 4-596
Is there no online version of that that we can hyperlink?
Is this what you mean?
Tiny results (see Section 4.9.1.5, “Numeric Underflow Exception (#U)” in Intel® 64 and IA-32 Architectures Soft- ware Developer’s Manual, Volume 1) are always flushed to 0.0, with the sign of the operand. (Input values greater than or equal to |1.11111111110100000000000B∗2125| are guaranteed to not produce tiny results; input values less than or equal to |1.00000000000110000000001B*2126| are guaranteed to produce tiny results, which are in turn flushed to 0.0; and input values in between this range may or may not produce tiny results, depending on the implementation.)
The test case in the issue is very hard to follow because of its use of f32::from_bits. It would be better to use float literals. If that's not possible, a detailed comment explaining what those magic constants mean should be added.
Intel only publishes the SDM as a PDF. Fortunately, I found an unofficial version "felixcloutier.com", which has an HTML rendering of it(but no Vol. 1): RCPSS, RSQRTSS.
I agree these operations are approximate in general [error bound], but the SDM has special-case rules that fix the exact result for these inputs, and Miri doesn't implement them. Miri's finite values are actually within 1.5*2^-12 of the true 1/x or 1/sqrt(x); it is the hardware that departs from the true value here, as the SDM specifies. From the Description of both RCPSS and RSQRTSS:
When a source value is a 0.0, an ∞ of the sign of the source value is returned. A denormal source value is treated as a 0.0 (of the same sign).
So for a denormal input the result is ±inf. This includes RSQRT of a negative denormal: it becomes -0.0, and the RSQRTSS rule for negative inputs explicitly excludes it ("a negative value (other than −0.0)").
The paragraph you quoted (RCP only) is the other rule: a tiny result is flushed to ±0.0. 1/f32::MAX is about 2^-128, and even with the maximum allowed error it stays below 2^-126, the smallest normal f32, so it is tiny.
For these inputs the SDM leaves no freedom: the result must be exactly ±inf or ±0.0, so Miri's values are not results the SDM allows.
Here is the test without from_bits:
use std::arch::x86_64::*;
use std::hint::black_box;
fn rcp(x: f32) -> f32 {
unsafe { _mm_cvtss_f32(_mm_rcp_ss(_mm_set_ss(black_box(x)))) }
}
fn rsqrt(x: f32) -> f32 {
unsafe { _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(black_box(x)))) }
}
fn show(name: &str, r: f32) {
println!("{name:<17} = {r:e} ({:#010x})", r.to_bits());
}
fn main() {
println!("arch: {}", std::env::consts::ARCH);
// 2^-127, a denormal. RCP and RSQRT treat it as 0.0 (of the same sign).
let denormal = f32::MIN_POSITIVE / 2.0;
show("rcp(denormal)", rcp(denormal));
show("rcp(-denormal)", rcp(-denormal));
show("rsqrt(denormal)", rsqrt(denormal));
show("rsqrt(-denormal)", rsqrt(-denormal));
// 1/f32::MAX is about 2^-128, a tiny result. RCP flushes it to 0.0.
show("rcp(f32::MAX)", rcp(f32::MAX));
show("rcp(-f32::MAX)", rcp(-f32::MAX));
}
Native (x86_64, run it on playground):
rcp(denormal) = inf (0x7f800000)
rcp(-denormal) = -inf (0xff800000)
rsqrt(denormal) = inf (0x7f800000)
rsqrt(-denormal) = -inf (0xff800000)
rcp(f32::MAX) = 0e0 (0x00000000)
rcp(-f32::MAX) = -0e0 (0x80000000)
Miri (run it on local):
rcp(denormal) = 1.7010358e38 (0x7efff184)
rcp(-denormal) = -1.7013563e38 (0xfefffddc)
rsqrt(denormal) = 1.3046266e19 (0x5f350da6)
rsqrt(-denormal) = NaN (0x7fc00000)
rcp(f32::MAX) = 2.938178e-39 (0x001ffe72)
rcp(-f32::MAX) = -2.938088e-39 (0x801ffe32)
The native results match the SDM rules above bit for bit: ±inf for the denormal inputs (including -inf, not NaN, for RSQRT of the negative denormal) and ±0.0 for 1/±f32::MAX. Miri returns none of them: finite values close to the true 1/x or 1/sqrt(x), and NaN for RSQRT of the negative denormal.
Is there no online version of that that we can hyperlink?
Is this what you mean?
Tiny results (see Section 4.9.1.5, “Numeric Underflow Exception (#U)” in Intel® 64 and IA-32 Architectures Soft- ware Developer’s Manual, Volume 1) are always flushed to 0.0, with the sign of the operand. (Input values greater than or equal to |1.11111111110100000000000B∗2125| are guaranteed to not produce tiny results; input values less than or equal to |1.00000000000110000000001B*2126| are guaranteed to produce tiny results, which are in turn flushed to 0.0; and input values in between this range may or may not produce tiny results, depending on the implementation.)
The test case in the issue is very hard to follow because of its use of
f32::from_bits. It would be better to use float literals. If that's not possible, a detailed comment explaining what those magic constants mean should be added.
What do you think of the paragraph you quoted? I think "greater than or equal to" and "less than or equal to" are swapped there.
Hm yeah it does seem like the swapped "greater" and "less" there.
So the issue is that we're not implementing the part of RCP where some results are guaranteed to be tiny and hence flushed to 0.0? As well as denormal handling for both of them? Sure, it's documented to we should have the same check. (I kind of doubt anyone actually relies on this though.)
(Btw, reminder to please not paste LLM text here, except in hidden sections that are marked as LLM-generated.)
@programskillforverification You can find such instructions at https://intel.github.io/SDM/instruction/RCPSS.html which, if you actually looked at Felix's website, you would have known.
@RalfJung These instructions should not be brought in conformance with Intel, because Intel and AMD implement different variations in their algorithms so presenting a match to either hardware provides incorrect assurances about x86-64 code.
https://robert.ocallahan.org/2021/09/rr-trace-portability-diverging-behavior.html
We're not trying to match the hardware in all cases. Just in the ones the docs explicitly mention. Does AMD also diverge from Intel on those?
They do apply what appears to be a similar rule, but that's why I think the difference is fine: if your algorithm is sensitive to the variation here, it is going to bork on the fact that AMD implements a finer precision with a wider max variation.
To be clear, Miri randomizes the output of RCP/RSQRT, and we'll keep going that. Even running the instruction on the same input twice does not give the same result. That's kind of silly and could actually be problematic (we should do something arbitrary-but-fixed-per-execution). But this issue is, in my eyes, not about changing that.
What we are considering here is to add specific checks for subnormals and "tiny" results so in those extreme cases we match the vendor's docs. If your algorithm depends on the exact values outside these corner cases, Miri will continue flagging that.
AMD does not document this rule explicitly fwiw.
We were quoting some official-looking Intel PDF above. How more explicit could it be...?
I mean, specifically, that AMD has documentation for all the x86 instructions as well and their documentation can vary.
If you desire to implement this case then that is a reasonable judgement call, I'm just skeptical of the need.
I assumed Intel "owns" the ISA so is in charge of setting the expectations here. But it seems worth checking what AMD has to say on this matter as well, fair point.
It's funny. Intel can definitely be said to "own" the IA32 ISA... the baseline.
But you may have seen AMD64 because, well, AMD got there first.
And then for a while they were both trying to build "the next thing after SSE4", and they both released/announced different instruction extensions, which resulted in some awkward stuff.
So they're in sort of a co-ownership situation. Most SSE instructions started with Intel though, yeah.
(Btw, reminder to please not paste LLM text here, except in hidden sections that are marked as LLM-generated.)
It's not LLM-generated text although the comment is long. You ask several questions and this issue includes many citations, so it's hard to shorten response if provides detailed comment and code.