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 <math.h>
31*f62d826aSJesús Blázquez #include <stdbool.h>
32*f62d826aSJesús Blázquez
33*f62d826aSJesús Blázquez #include "fpmath.h"
34*f62d826aSJesús Blázquez
35*f62d826aSJesús Blázquez #ifdef USE_BUILTIN_FMINIMUM_MAG_NUMF
36*f62d826aSJesús Blázquez float
fminimum_mag_numf(float x,float y)37*f62d826aSJesús Blázquez fminimum_mag_numf(float x, float y)
38*f62d826aSJesús Blázquez {
39*f62d826aSJesús Blázquez return (__builtin_fminimum_mag_numf(x, y));
40*f62d826aSJesús Blázquez }
41*f62d826aSJesús Blázquez #else
42*f62d826aSJesús Blázquez float
fminimum_mag_numf(float x,float y)43*f62d826aSJesús Blázquez fminimum_mag_numf(float x, float y)
44*f62d826aSJesús Blázquez {
45*f62d826aSJesús Blázquez union IEEEf2bits u[2];
46*f62d826aSJesús Blázquez bool nan_x, nan_y;
47*f62d826aSJesús Blázquez
48*f62d826aSJesús Blázquez u[0].f = x;
49*f62d826aSJesús Blázquez u[1].f = y;
50*f62d826aSJesús Blázquez
51*f62d826aSJesús Blázquez nan_x = isnan(x);
52*f62d826aSJesús Blázquez nan_y = isnan(y);
53*f62d826aSJesús Blázquez
54*f62d826aSJesús Blázquez if (nan_x || nan_y) {
55*f62d826aSJesús Blázquez /* If both are NaN, adding returns qNaN */
56*f62d826aSJesús Blázquez if (nan_x && nan_y)
57*f62d826aSJesús Blázquez return (x + y);
58*f62d826aSJesús Blázquez
59*f62d826aSJesús Blázquez /* force_except makes sure sNaN's raise exceptions */
60*f62d826aSJesús Blázquez volatile float force_except = x + y;
61*f62d826aSJesús Blázquez force_except;
62*f62d826aSJesús Blázquez
63*f62d826aSJesús Blázquez if (nan_x)
64*f62d826aSJesús Blázquez return (y);
65*f62d826aSJesús Blázquez else
66*f62d826aSJesús Blázquez return (x);
67*f62d826aSJesús Blázquez }
68*f62d826aSJesús Blázquez
69*f62d826aSJesús Blázquez float ax = fabsf(x);
70*f62d826aSJesús Blázquez float ay = fabsf(y);
71*f62d826aSJesús Blázquez
72*f62d826aSJesús Blázquez if (ay < ax)
73*f62d826aSJesús Blázquez return (y);
74*f62d826aSJesús Blázquez if (ax < ay)
75*f62d826aSJesús Blázquez return (x);
76*f62d826aSJesús Blázquez
77*f62d826aSJesús Blázquez /* If magnitudes are equal, we break the tie with the sign */
78*f62d826aSJesús Blázquez if (u[0].bits.sign != u[1].bits.sign)
79*f62d826aSJesús Blázquez return (u[u[1].bits.sign].f);
80*f62d826aSJesús Blázquez
81*f62d826aSJesús Blázquez return (x);
82*f62d826aSJesús Blázquez }
83*f62d826aSJesús Blázquez #endif
84*f62d826aSJesús Blázquez
85*f62d826aSJesús Blázquez
86