From d9eacb05c9a56dc07c2803d4ab6c88cf608af1d1 Mon Sep 17 00:00:00 2001 From: Jesse Natalie Date: Thu, 20 Jun 2024 10:55:56 -0700 Subject: [PATCH] nir_range_analysis: Use fmin/fmax to fix NAN handling The following probable MSVC bug clued us in to some probably-unexpected behavior: https://developercommunity.visualstudio.com/t/Incorrect-SSE-code-for-minmax-with-NaNs/10687862 Change the logic here so that we're always starting with NANs and use fmin/fmax, which have more-deterministic handling of NANs. If one argument is NAN, the non-NAN argument is returned. The previous code would've returned the second argument if one was NAN. Reviewed-by: Caio Oliveira Part-of: --- src/compiler/nir/nir_range_analysis.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler/nir/nir_range_analysis.c b/src/compiler/nir/nir_range_analysis.c index 79c7ed92cfd..ac076a9e5de 100644 --- a/src/compiler/nir/nir_range_analysis.c +++ b/src/compiler/nir/nir_range_analysis.c @@ -175,8 +175,8 @@ analyze_constant(const struct nir_alu_instr *instr, unsigned src, switch (nir_alu_type_get_base_type(use_type)) { case nir_type_float: { - double min_value = DBL_MAX; - double max_value = -DBL_MAX; + double min_value = NAN; + double max_value = NAN; bool any_zero = false; bool all_zero = true; @@ -199,8 +199,8 @@ analyze_constant(const struct nir_alu_instr *instr, unsigned src, any_zero = any_zero || (v == 0.0); all_zero = all_zero && (v == 0.0); - min_value = MIN2(min_value, v); - max_value = MAX2(max_value, v); + min_value = fmin(min_value, v); + max_value = fmax(max_value, v); } assert(any_zero >= all_zero);