xref: /freebsd/lib/msun/src/s_fminimum_mag_numl.c (revision f62d826a6f5b9022b0cedfe22a698998ad9cb7f4)
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 long double
fminimum_mag_numl(long double x,long double y)36*f62d826aSJesús Blázquez fminimum_mag_numl(long double x, long double y)
37*f62d826aSJesús Blázquez {
38*f62d826aSJesús Blázquez 	union IEEEl2bits u[2];
39*f62d826aSJesús Blázquez 	bool nan_x, nan_y;
40*f62d826aSJesús Blázquez 
41*f62d826aSJesús Blázquez 	u[0].e = x;
42*f62d826aSJesús Blázquez 	mask_nbit_l(u[0]);
43*f62d826aSJesús Blázquez 	u[1].e = y;
44*f62d826aSJesús Blázquez 	mask_nbit_l(u[1]);
45*f62d826aSJesús Blázquez 
46*f62d826aSJesús Blázquez 	nan_x = isnan(x);
47*f62d826aSJesús Blázquez 	nan_y = isnan(y);
48*f62d826aSJesús Blázquez 
49*f62d826aSJesús Blázquez 	if (nan_x || nan_y) {
50*f62d826aSJesús Blázquez 		/* If both are NaN, adding returns qNaN */
51*f62d826aSJesús Blázquez 		if (nan_x && nan_y)
52*f62d826aSJesús Blázquez 		    return (x + y);
53*f62d826aSJesús Blázquez 
54*f62d826aSJesús Blázquez 		/* force_except makes sure sNaN's raise exceptions */
55*f62d826aSJesús Blázquez 		volatile long double force_except = x + y;
56*f62d826aSJesús Blázquez 		force_except;
57*f62d826aSJesús Blázquez 
58*f62d826aSJesús Blázquez 		if (nan_x)
59*f62d826aSJesús Blázquez 			return (y);
60*f62d826aSJesús Blázquez 		else
61*f62d826aSJesús Blázquez 			return (x);
62*f62d826aSJesús Blázquez 	}
63*f62d826aSJesús Blázquez 
64*f62d826aSJesús Blázquez 	long double ax = fabsl(x);
65*f62d826aSJesús Blázquez 	long double ay = fabsl(y);
66*f62d826aSJesús Blázquez 
67*f62d826aSJesús Blázquez 	if (ay < ax)
68*f62d826aSJesús Blázquez 		return (y);
69*f62d826aSJesús Blázquez 	if (ax < ay)
70*f62d826aSJesús Blázquez 		return (x);
71*f62d826aSJesús Blázquez 
72*f62d826aSJesús Blázquez 	/* If magnitudes are equal, we break the tie with the sign */
73*f62d826aSJesús Blázquez 	if (u[0].bits.sign != u[1].bits.sign)
74*f62d826aSJesús Blázquez 		return (u[1].bits.sign ? y : x);
75*f62d826aSJesús Blázquez 
76*f62d826aSJesús Blázquez 	return (x);
77*f62d826aSJesús Blázquez }
78*f62d826aSJesús Blázquez 
79*f62d826aSJesús Blázquez 
80