From fabe02ddc2e802e75fa90db003779ba961b90523 Mon Sep 17 00:00:00 2001 From: Tiago Koji Castro Shibata Date: Tue, 13 Oct 2020 20:10:33 -0700 Subject: [PATCH] Don't change global FPU state during round-half-to-even (#5376) * Don't change global FPU state * Handle infinity properly --- onnxruntime/core/util/qmath.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/onnxruntime/core/util/qmath.h b/onnxruntime/core/util/qmath.h index bdaed61b04..055241061f 100644 --- a/onnxruntime/core/util/qmath.h +++ b/onnxruntime/core/util/qmath.h @@ -43,9 +43,11 @@ void QGemm( concurrency::ThreadPool* thread_pool); inline float RoundHalfToEven(float input) { - std::fesetround(FE_TONEAREST); - auto result = std::nearbyintf(input); - return result; + if (!std::isfinite(input)) { + return input; + } + // std::remainder returns x - n, where n is the integral value nearest to x. When |x - n| = 0.5, n is chosen to be even + return input - std::remainderf(input, 1.f); } } // namespace onnxruntime