dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.85k stars 4.62k forks source link

[NativeAOT] Rounding helpers use wrong rounding algorithm #97922

Closed filipnavara closed 6 months ago

filipnavara commented 6 months ago

RhpFltRound/RhpDblRound are implemented using round/roundf. That's incorrect, since .NET default is round to even. Similar issue was recently fixed in NativeAOT-LLVM.

Unfortunately, roundeven/roundevenf methods are only available in C23 standard. They are implemented on glibc 2.25+ (current .NET requirement is 2.23) and missing on Bionic/MUSL libc. CoreCLR uses a custom implementation which can presumably be reused.

ghost commented 6 months ago

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas See info in area-owners.md if you want to be subscribed.

Issue Details
[RhpFltRound/RhpDblRound](https://github.com/dotnet/runtime/blob/60cfe26dcb28a7fd26c09d082466a7ea650fc505/src/coreclr/nativeaot/Runtime/MathHelpers.cpp#L81-L89) are implemented using `round`/`roundf`. That's incorrect, since .NET default is round to even. Similar issue was [recently fixed](https://github.com/dotnet/runtimelab/pull/2503) in NativeAOT-LLVM. Unfortunately, `roundeven`/`roundevenf` methods are only available in C23 standard. They are implemented on glibc 2.25+ (current .NET requirement is 2.23) and missing on Bionic libc. CoreCLR uses a [custom implementation](https://github.com/dotnet/runtime/blob/60cfe26dcb28a7fd26c09d082466a7ea650fc505/src/coreclr/vm/jithelpers.cpp#L519-L543) which can presumably be reused.
Author: filipnavara
Assignees: -
Labels: `area-NativeAOT-coreclr`
Milestone: -