1*f62d826aSJesús Blázquez /*
2*f62d826aSJesús Blázquez * SPDX-License-Identifier: BSD-2-Clause
3*f62d826aSJesús Blázquez *
4*f62d826aSJesús Blázquez * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
5*f62d826aSJesús Blázquez * Copyright (c) 2026 Jesús Blázquez <jesuscblazquez@gmail.com>
6*f62d826aSJesús Blázquez * All rights reserved.
7*f62d826aSJesús Blázquez *
8*f62d826aSJesús Blázquez * Redistribution and use in source and binary forms, with or without
9*f62d826aSJesús Blázquez * modification, are permitted provided that the following conditions
10*f62d826aSJesús Blázquez * are met:
11*f62d826aSJesús Blázquez * 1. Redistributions of source code must retain the above copyright
12*f62d826aSJesús Blázquez * notice, this list of conditions and the following disclaimer.
13*f62d826aSJesús Blázquez * 2. Redistributions in binary form must reproduce the above copyright
14*f62d826aSJesús Blázquez * notice, this list of conditions and the following disclaimer in the
15*f62d826aSJesús Blázquez * documentation and/or other materials provided with the distribution.
16*f62d826aSJesús Blázquez *
17*f62d826aSJesús Blázquez * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*f62d826aSJesús Blázquez * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*f62d826aSJesús Blázquez * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*f62d826aSJesús Blázquez * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*f62d826aSJesús Blázquez * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*f62d826aSJesús Blázquez * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*f62d826aSJesús Blázquez * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*f62d826aSJesús Blázquez * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*f62d826aSJesús Blázquez * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*f62d826aSJesús Blázquez * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*f62d826aSJesús Blázquez * SUCH DAMAGE.
28*f62d826aSJesús Blázquez */
29*f62d826aSJesús Blázquez
30*f62d826aSJesús Blázquez #include <float.h>
31*f62d826aSJesús Blázquez #include <math.h>
32*f62d826aSJesús Blázquez #include <stdbool.h>
33*f62d826aSJesús Blázquez
34*f62d826aSJesús Blázquez #include "fpmath.h"
35*f62d826aSJesús Blázquez
36*f62d826aSJesús Blázquez #ifdef USE_BUILTIN_FMAXIMUM_MAG_NUM
37*f62d826aSJesús Blázquez double
fmaximum_mag_num(double x,double y)38*f62d826aSJesús Blázquez fmaximum_mag_num(double x, double y)
39*f62d826aSJesús Blázquez {
40*f62d826aSJesús Blázquez return (__builtin_fmaximum_mag_num(x, y));
41*f62d826aSJesús Blázquez }
42*f62d826aSJesús Blázquez #else
43*f62d826aSJesús Blázquez double
fmaximum_mag_num(double x,double y)44*f62d826aSJesús Blázquez fmaximum_mag_num(double x, double y)
45*f62d826aSJesús Blázquez {
46*f62d826aSJesús Blázquez union IEEEd2bits u[2];
47*f62d826aSJesús Blázquez bool nan_x, nan_y;
48*f62d826aSJesús Blázquez
49*f62d826aSJesús Blázquez u[0].d = x;
50*f62d826aSJesús Blázquez u[1].d = y;
51*f62d826aSJesús Blázquez
52*f62d826aSJesús Blázquez nan_x = isnan(x);
53*f62d826aSJesús Blázquez nan_y = isnan(y);
54*f62d826aSJesús Blázquez
55*f62d826aSJesús Blázquez if (nan_x || nan_y) {
56*f62d826aSJesús Blázquez /* If both are NaN, adding returns qNaN */
57*f62d826aSJesús Blázquez if (nan_x && nan_y)
58*f62d826aSJesús Blázquez return (x + y);
59*f62d826aSJesús Blázquez
60*f62d826aSJesús Blázquez /* force_except makes sure sNaN's raise exceptions */
61*f62d826aSJesús Blázquez volatile double force_except = x + y;
62*f62d826aSJesús Blázquez force_except;
63*f62d826aSJesús Blázquez
64*f62d826aSJesús Blázquez if (nan_x)
65*f62d826aSJesús Blázquez return (y);
66*f62d826aSJesús Blázquez else
67*f62d826aSJesús Blázquez return (x);
68*f62d826aSJesús Blázquez }
69*f62d826aSJesús Blázquez
70*f62d826aSJesús Blázquez double ax = fabs(x);
71*f62d826aSJesús Blázquez double ay = fabs(y);
72*f62d826aSJesús Blázquez
73*f62d826aSJesús Blázquez if (ay > ax)
74*f62d826aSJesús Blázquez return (y);
75*f62d826aSJesús Blázquez if (ax > ay)
76*f62d826aSJesús Blázquez return (x);
77*f62d826aSJesús Blázquez
78*f62d826aSJesús Blázquez /* If magnitudes are equal, we break the tie with the sign */
79*f62d826aSJesús Blázquez if (u[0].bits.sign != u[1].bits.sign)
80*f62d826aSJesús Blázquez return (u[u[0].bits.sign].d);
81*f62d826aSJesús Blázquez
82*f62d826aSJesús Blázquez return (x);
83*f62d826aSJesús Blázquez }
84*f62d826aSJesús Blázquez #endif
85*f62d826aSJesús Blázquez
86*f62d826aSJesús Blázquez #if (LDBL_MANT_DIG == 53)
87*f62d826aSJesús Blázquez __weak_reference(fmaximum_mag_num, fmaximum_mag_numl);
88*f62d826aSJesús Blázquez #endif
89