bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: signed-nan: Don't assume that '-' works as expected on NaN values


From: Jeffrey Walton
Subject: Re: signed-nan: Don't assume that '-' works as expected on NaN values
Date: Fri, 19 Apr 2024 08:44:46 -0400



On Fri, Apr 19, 2024 at 8:06 AM Bruno Haible <bruno@clisp.org> wrote:
...

+/* Returns - x, implemented by inverting the sign bit,
+   so that it works also on 'float' NaN values.  */
+_GL_UNUSED static float
+minus_NaNf (float x)
+{
+#if defined __mips__
+  /* The mips instruction neg.s may have no effect on NaNs.
+     Therefore, invert the sign bit using integer operations.  */
+  union { unsigned int i; float value; } u;
+  u.value = x;
+  u.i ^= 1U << 31;
+  return u.value;
+#else
+  return - x;
+#endif
+}

Accessing the union first though 'value' and then through 'i' may be undefined behavior. I don't think you can legally access the union through the inactive member.

Jeff

reply via email to

[Prev in Thread] Current Thread [Next in Thread]