-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Math.Round[F](x, MidpointRounding)
doesn't use VROUNDS[SD]
outside of "to even"
#98164
Comments
Tagging subscribers to this area: @dotnet/area-system-numerics Issue DetailsDescriptionIt appears that The "round control" bits of
Bit three of the immediate is set to use DataExpected behavior occurs for "to even" rounding, but with the "use using System;
public class Program
{
public double Test(double val) =>
Math.Round(val, MidpointRounding.ToEven);
} Program.Test(Double)
L0000: vzeroupper
L0003: vroundsd xmm0, xmm0, xmm1, 4
L0009: ret However, using a mode that is not set in using System;
public class Program
{
public double Test(double val) =>
Math.Round(val, MidpointRounding.ToPositiveInfinity);
} Program.Test(Double)
L0000: vzeroupper
L0003: vmovaps xmm0, xmm1
L0007: xor edx, edx
L0009: mov r8d, 4
L000f: mov rax, 0x7ff7e0257618
L0019: jmp qword ptr [rax]
|
It's typically preferred to just use the simpler dedicated API instead:
We can of course optimize the explicit enum modes as well, but its not as high of a priority. |
I do see that the dedicated methods use the embedded round control. For example, Would it be preferable to just have the enum variant forward to the other methods like it already does for "to even"? I personally prefer |
If you'd like to put up a PR, then feel free. |
The issue with that was that iirc those methods forward to the enum ones on platforms that aren't accelerated. |
Description
It appears that
Math[F].Round
doesn't always useVROUNDS[SD]
with an embedded round control. It does useVROUNDS[SD]
for "to even" as that follows MXCSRThe "round control" bits of
VROUNDS[SD]
are as follows:00
: nearest/to even01
: down/to negative infinity10
: up/to positive infinity11
: truncate/to zeroBit three of the immediate is set to use
MXCSR.RC
and cleared to use the embedded round control bits. It looks like the JIT only usesVROUND[SD]
with "useMXCSR
" set.Data
Expected behavior occurs for "to even" rounding, but with the "use
MXCSR
" bit set - hence the suspicion. In this case, no harm, no foul.https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKBuIGYACM58gdhoG8bHfmmAJhACuwADYxGAFRi4MACiGiJjAG7YxASkYBeAHw8+RgLLYMACwB0AJREA7AfPVi0jYwEsBABwju7GW2EHPwBzSykIAFFVGDtNAG4aAF8gA
However, using a mode that is not set in
MXCSR
calls into the framework for help instead of using an embedding rounding mode. For the below,VROUNDSD
should be used with an immediate ofb010
(ignoreMXCSR
, and use "round up/to infinity").https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKBuIGYACM58gdhoG8bHfmmAJhACuwADYxGAFRi4MACiGiJjAG7YxASkYBeAHw8+RgLLYMACwB0AJREA7AfPVi0jYwEsBABwju7GW2EHPwBzSykIAAUIXHcMd1UYAEk7ADM/OIBPTQBuGgBfIA===
Expected behavior for this would be:
The text was updated successfully, but these errors were encountered: