1e55db843SJesús Blázquez /*
2e55db843SJesús Blázquez * SPDX-License-Identifier: BSD-2-Clause
3e55db843SJesús Blázquez *
4e55db843SJesús Blázquez * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
5e55db843SJesús Blázquez * Copyright (c) 2026 Jesús Blázquez <jesuscblazquez@gmail.com>
6e55db843SJesús Blázquez * All rights reserved.
7e55db843SJesús Blázquez *
8e55db843SJesús Blázquez * Redistribution and use in source and binary forms, with or without
9e55db843SJesús Blázquez * modification, are permitted provided that the following conditions
10e55db843SJesús Blázquez * are met:
11e55db843SJesús Blázquez * 1. Redistributions of source code must retain the above copyright
12e55db843SJesús Blázquez * notice, this list of conditions and the following disclaimer.
13e55db843SJesús Blázquez * 2. Redistributions in binary form must reproduce the above copyright
14e55db843SJesús Blázquez * notice, this list of conditions and the following disclaimer in the
15e55db843SJesús Blázquez * documentation and/or other materials provided with the distribution.
16e55db843SJesús Blázquez *
17e55db843SJesús Blázquez * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18e55db843SJesús Blázquez * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19e55db843SJesús Blázquez * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20e55db843SJesús Blázquez * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21e55db843SJesús Blázquez * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22e55db843SJesús Blázquez * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23e55db843SJesús Blázquez * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24e55db843SJesús Blázquez * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25e55db843SJesús Blázquez * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26e55db843SJesús Blázquez * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27e55db843SJesús Blázquez * SUCH DAMAGE.
28e55db843SJesús Blázquez */
29e55db843SJesús Blázquez
30e55db843SJesús Blázquez #include <math.h>
31e55db843SJesús Blázquez
32e55db843SJesús Blázquez #include "fpmath.h"
33e55db843SJesús Blázquez
34e55db843SJesús Blázquez long double
fmaximuml(long double x,long double y)35e55db843SJesús Blázquez fmaximuml(long double x, long double y)
36e55db843SJesús Blázquez {
37e55db843SJesús Blázquez union IEEEl2bits u[2];
38e55db843SJesús Blázquez
39e55db843SJesús Blázquez u[0].e = x;
40e55db843SJesús Blázquez mask_nbit_l(u[0]);
41e55db843SJesús Blázquez u[1].e = y;
42e55db843SJesús Blázquez mask_nbit_l(u[1]);
43e55db843SJesús Blázquez
44e55db843SJesús Blázquez /* Handle NaN according to ISO/IEC 60559. NaN argument -> NaN return */
45*f6e1f27eSJesús Blázquez if (isnan(x) || isnan(y))
46e55db843SJesús Blázquez return (NAN);
47e55db843SJesús Blázquez
48e55db843SJesús Blázquez /* Handle comparisons of signed zeroes. */
49e55db843SJesús Blázquez if (u[0].bits.sign != u[1].bits.sign)
50e55db843SJesús Blázquez return (u[0].bits.sign ? y : x);
51e55db843SJesús Blázquez
52e55db843SJesús Blázquez return (x > y ? x : y);
53e55db843SJesús Blázquez }
54