14e30c129SJesús Blázquez /*
24e30c129SJesús Blázquez * SPDX-License-Identifier: BSD-2-Clause
34e30c129SJesús Blázquez *
44e30c129SJesús Blázquez * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
54e30c129SJesús Blázquez * Copyright (c) 2026 Jesús Blázquez <jesuscblazquez@gmail.com>
64e30c129SJesús Blázquez * All rights reserved.
74e30c129SJesús Blázquez *
84e30c129SJesús Blázquez * Redistribution and use in source and binary forms, with or without
94e30c129SJesús Blázquez * modification, are permitted provided that the following conditions
104e30c129SJesús Blázquez * are met:
114e30c129SJesús Blázquez * 1. Redistributions of source code must retain the above copyright
124e30c129SJesús Blázquez * notice, this list of conditions and the following disclaimer.
134e30c129SJesús Blázquez * 2. Redistributions in binary form must reproduce the above copyright
144e30c129SJesús Blázquez * notice, this list of conditions and the following disclaimer in the
154e30c129SJesús Blázquez * documentation and/or other materials provided with the distribution.
164e30c129SJesús Blázquez *
174e30c129SJesús Blázquez * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
184e30c129SJesús Blázquez * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
194e30c129SJesús Blázquez * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
204e30c129SJesús Blázquez * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
214e30c129SJesús Blázquez * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
224e30c129SJesús Blázquez * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
234e30c129SJesús Blázquez * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
244e30c129SJesús Blázquez * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
254e30c129SJesús Blázquez * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
264e30c129SJesús Blázquez * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
274e30c129SJesús Blázquez * SUCH DAMAGE.
284e30c129SJesús Blázquez */
294e30c129SJesús Blázquez
304e30c129SJesús Blázquez #include <math.h>
314e30c129SJesús Blázquez #include <stdbool.h>
324e30c129SJesús Blázquez
334e30c129SJesús Blázquez #include "fpmath.h"
344e30c129SJesús Blázquez
354e30c129SJesús Blázquez long double
fmaximum_numl(long double x,long double y)364e30c129SJesús Blázquez fmaximum_numl(long double x, long double y)
374e30c129SJesús Blázquez {
384e30c129SJesús Blázquez union IEEEl2bits u[2];
394e30c129SJesús Blázquez bool nan_x, nan_y;
404e30c129SJesús Blázquez
414e30c129SJesús Blázquez u[0].e = x;
424e30c129SJesús Blázquez mask_nbit_l(u[0]);
434e30c129SJesús Blázquez u[1].e = y;
444e30c129SJesús Blázquez mask_nbit_l(u[1]);
454e30c129SJesús Blázquez
46*f6e1f27eSJesús Blázquez nan_x = isnan(x);
47*f6e1f27eSJesús Blázquez nan_y = isnan(y);
484e30c129SJesús Blázquez
494e30c129SJesús Blázquez if (nan_x || nan_y) {
504e30c129SJesús Blázquez /* These ternary conditionals force (x+y), so that sNaN's raise exceptions */
514e30c129SJesús Blázquez if (nan_x && nan_y)
524e30c129SJesús Blázquez return (x + y);
534e30c129SJesús Blázquez if (nan_x)
544e30c129SJesús Blázquez return ((x + y) != 0.0 ? y : y);
554e30c129SJesús Blázquez return ((x + y) != 0.0 ? x : x);
564e30c129SJesús Blázquez }
574e30c129SJesús Blázquez
584e30c129SJesús Blázquez /* Handle comparisons of signed zeroes. */
594e30c129SJesús Blázquez if (u[0].bits.sign != u[1].bits.sign)
604e30c129SJesús Blázquez return (u[0].bits.sign ? y : x);
614e30c129SJesús Blázquez
624e30c129SJesús Blázquez return (x > y ? x : y);
634e30c129SJesús Blázquez }
64