From 1ab11a111ce0717bfbfaca964d04a017cb9b1752 Mon Sep 17 00:00:00 2001 From: "Maxiwell S. Garcia" Date: Fri, 14 Oct 2022 17:43:58 -0400 Subject: [PATCH] ppc64le: mlas: fix both MaximumFloat and MinimumFloat to return NAN (#12628) Avoid using vec_max/vec_min because their behaviors are undefined if one of the elements is NAN. The Power Vector Intrinsic Programming Reference says: "For floating-point types, if both source elements contain signed zeros, or if either source element contains a NaN, it is undefined which of the two source elements is copied into the corresponding result element." As the unittest Activation.ShortExecute expects NAN, this patch uses vec_sel and vec_cmpgt to return NAN if one of the elements is NAN. https://git.openpower.foundation/systemsoftware/Programming-Guides/src/branch/master/Intrinsics_Reference/ch_vec_reference.xml#L26808 --- onnxruntime/core/mlas/lib/mlasi.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/onnxruntime/core/mlas/lib/mlasi.h b/onnxruntime/core/mlas/lib/mlasi.h index 648f195fd6..0f365159ac 100644 --- a/onnxruntime/core/mlas/lib/mlasi.h +++ b/onnxruntime/core/mlas/lib/mlasi.h @@ -1804,7 +1804,8 @@ MlasMaximumFloat32x4(MLAS_FLOAT32X4 Vector1, MLAS_FLOAT32X4 Vector2) #elif defined(MLAS_SSE2_INTRINSICS) return _mm_max_ps(Vector1, Vector2); #elif defined(MLAS_VSX_INTRINSICS) - return vec_max(Vector1, Vector2); + // Don't use vec_max to avoid undefined behavior if NAN + return vec_sel(Vector2, Vector1, vec_cmpgt(Vector1, Vector2)); #elif defined(MLAS_WASM_SIMD_INTRINSICS) return wasm_f32x4_max(Vector1, Vector2); #else @@ -1821,7 +1822,8 @@ MlasMinimumFloat32x4(MLAS_FLOAT32X4 Vector1, MLAS_FLOAT32X4 Vector2) #elif defined(MLAS_SSE2_INTRINSICS) return _mm_min_ps(Vector1, Vector2); #elif defined(MLAS_VSX_INTRINSICS) - return vec_min(Vector1, Vector2); + // Don't use vec_min to avoid undefined behavior if NAN + return vec_sel(Vector2, Vector1, vec_cmpgt(Vector2, Vector1)); #elif defined(MLAS_WASM_SIMD_INTRINSICS) return wasm_f32x4_min(Vector1, Vector2); #else