Fix last ChangeLog entry.
[gnulib.git] / lib / totalordermagf.c
blobb6879e2d27f0b45a044890d835cd0b74e608bd21
1 /* Total order of absolute value for 'float'.
2 Copyright 2023-2025 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation, either version 3 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Paul Eggert and Bruno Haible. */
19 #include <config.h>
21 /* Specification. */
22 #include <math.h>
24 #include <string.h>
26 #include "verify.h"
28 int
29 totalordermagf (float const *x, float const *y)
31 /* If one of *X, *Y is a NaN and the other isn't, the answer is easy:
32 the NaN is "greater" than the other argument. */
33 int xn = isnanf (*x);
34 int yn = isnanf (*y);
35 if (!xn != !yn)
36 return yn;
37 /* If none of *X, *Y is a NaN, the '<=' operator on the absolute values
38 does the job, including for -Infinity and +Infinity. */
39 if (!xn)
40 return (signbit (*x) ? - *x : *x) <= (signbit (*y) ? - *y : *y);
42 /* At this point, *X and *Y are NaNs. */
44 #if defined FLT_SIGNBIT_WORD && defined FLT_SIGNBIT_BIT
45 /* The use of a union to extract the bits of the representation of a
46 'float' is safe in practice, despite of the "aliasing rules" of
47 C99, because the GCC docs say
48 "Even with '-fstrict-aliasing', type-punning is allowed, provided the
49 memory is accessed through the union type."
50 and similarly for other compilers. */
51 # define NWORDS \
52 ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
53 union { unsigned int word[NWORDS]; float value; }
54 xu = {0}, yu = {0};
56 # if 0
57 xu.value = *x;
58 yu.value = *y;
59 # else
60 # if defined __GNUC__ || defined __clang__
61 /* Prevent gcc and clang from reusing the values of *x and *y (fetched above)
62 in optimized inlined memcpy expansions.
63 Seen with gcc <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114659>
64 and with clang 16.0.6 on OpenBSD 7.5. */
65 __asm__ __volatile__ ("" : : : "memory");
66 # endif
67 /* On 32-bit x86 processors, as well as on x86_64 processors with
68 CC="gcc -mfpmath=387", the evaluation of *x and *y above is done through
69 an 'flds' instruction, which converts a signalling NaN to a quiet NaN. See
70 <https://lists.gnu.org/archive/html/bug-gnulib/2023-10/msg00060.html>
71 for details. Use memcpy to avoid this. */
72 memcpy (&xu.value, x, sizeof (float));
73 memcpy (&yu.value, y, sizeof (float));
74 # endif
76 verify (NWORDS == 1);
78 return ((xu.word[0] & ~(1U << FLT_SIGNBIT_BIT)) /* *X without its sign bit */
79 # if defined __hppa || (defined __mips__ && !MIPS_NAN2008_FLOAT) || defined __sh__
80 /* Invert the most significant bit of the mantissa field.
81 Cf. snan.h. */
82 ^ (1U << 22)
83 # endif
84 ) <=
85 ((yu.word[0] & ~(1U << FLT_SIGNBIT_BIT)) /* *Y without its sign bit */
86 # if defined __hppa || (defined __mips__ && !MIPS_NAN2008_FLOAT) || defined __sh__
87 /* Invert the most significant bit of the mantissa field.
88 Cf. snan.h. */
89 ^ (1U << 22)
90 # endif
92 #else
93 # error "Please port gnulib totalordermagf.c to your platform!"
94 #endif