summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChia-I Wu <olvaffe@gmail.com>2022-04-28 14:25:36 -0700
committerDylan Baker <dylan.c.baker@intel.com>2022-05-03 09:41:32 -0700
commit9febc11af9a8a5b227ddaeda4798ae0ef52bd644 (patch)
tree10e17b1c423a77a9592be7e9fb9c583077e9b2e4
parentd29fe64c7e5eaff00f879c28a0e2c7ae877de60d (diff)
util: Keep quiet NaNs quiet when converting to half float.
We don't want to be throwing exceptions and changing float values later by emitting a signaling binary16 nan. If we don't do this, then when we convert back to f32 in NIR constant expression evaluation, the signaling NaN can end up giving NaN for fmax(NaN, 0.0), instead of 0.0. Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/5933 Cc: mesa-stable Reviewed-by: Emma Anholt <emma@anholt.net> Reviewed-by: Jason Ekstrand <jason.ekstrand@collabora.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16233> (cherry picked from commit 27e33d5c963f13a47c4b9276378db913d8ecb189)
-rw-r--r--.pick_status.json2
-rw-r--r--src/util/half_float.c8
-rw-r--r--src/util/softfloat.c7
3 files changed, 13 insertions, 4 deletions
diff --git a/.pick_status.json b/.pick_status.json
index 5807233d9f3..5b2590ec005 100644
--- a/.pick_status.json
+++ b/.pick_status.json
@@ -274,7 +274,7 @@
"description": "util: Keep quiet NaNs quiet when converting to half float.",
"nominated": true,
"nomination_type": 0,
- "resolution": 0,
+ "resolution": 1,
"main_sha": null,
"because_sha": null
},
diff --git a/src/util/half_float.c b/src/util/half_float.c
index 05aeac14abb..606e4b9522e 100644
--- a/src/util/half_float.c
+++ b/src/util/half_float.c
@@ -83,8 +83,12 @@ _mesa_float_to_half_slow(float val)
e = 31;
}
else if ((flt_e == 0xff) && (flt_m != 0)) {
- /* NaN */
- m = 1;
+ /* Retain the top bits of a NaN to make sure that the quiet/signaling
+ * status stays the same.
+ */
+ m = flt_m >> 13;
+ if (!m)
+ m = 1;
e = 31;
}
else {
diff --git a/src/util/softfloat.c b/src/util/softfloat.c
index cc386aa46fb..2ff835f7452 100644
--- a/src/util/softfloat.c
+++ b/src/util/softfloat.c
@@ -1452,7 +1452,12 @@ _mesa_float_to_half_rtz_slow(float val)
if (flt_m != 0) {
/* 'val' is a NaN, return NaN */
e = 0x1f;
- m = 0x1;
+ /* Retain the top bits of a NaN to make sure that the quiet/signaling
+ * status stays the same.
+ */
+ m = flt_m >> 13;
+ if (!m)
+ m = 1;
return (s << 15) + (e << 10) + m;
}