xref: /freebsd/lib/msun/src/s_fmaximum_magl.c (revision f6e1f27e9f005447e88a9b37df8a75751b1e0c3a)
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  * All rights reserved.
64e30c129SJesús Blázquez  *
74e30c129SJesús Blázquez  * Redistribution and use in source and binary forms, with or without
84e30c129SJesús Blázquez  * modification, are permitted provided that the following conditions
94e30c129SJesús Blázquez  * are met:
104e30c129SJesús Blázquez  * 1. Redistributions of source code must retain the above copyright
114e30c129SJesús Blázquez  *    notice, this list of conditions and the following disclaimer.
124e30c129SJesús Blázquez  * 2. Redistributions in binary form must reproduce the above copyright
134e30c129SJesús Blázquez  *    notice, this list of conditions and the following disclaimer in the
144e30c129SJesús Blázquez  *    documentation and/or other materials provided with the distribution.
154e30c129SJesús Blázquez  *
164e30c129SJesús Blázquez  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
174e30c129SJesús Blázquez  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184e30c129SJesús Blázquez  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194e30c129SJesús Blázquez  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
204e30c129SJesús Blázquez  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214e30c129SJesús Blázquez  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224e30c129SJesús Blázquez  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234e30c129SJesús Blázquez  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244e30c129SJesús Blázquez  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254e30c129SJesús Blázquez  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264e30c129SJesús Blázquez  * SUCH DAMAGE.
274e30c129SJesús Blázquez  */
284e30c129SJesús Blázquez 
294e30c129SJesús Blázquez #include <math.h>
304e30c129SJesús Blázquez 
314e30c129SJesús Blázquez #include "fpmath.h"
324e30c129SJesús Blázquez 
334e30c129SJesús Blázquez long double
fmaximum_magl(long double x,long double y)344e30c129SJesús Blázquez fmaximum_magl(long double x, long double y)
354e30c129SJesús Blázquez {
364e30c129SJesús Blázquez 	union IEEEl2bits u[2];
374e30c129SJesús Blázquez 
384e30c129SJesús Blázquez 	u[0].e = x;
394e30c129SJesús Blázquez 	mask_nbit_l(u[0]);
404e30c129SJesús Blázquez 	u[1].e = y;
414e30c129SJesús Blázquez 	mask_nbit_l(u[1]);
424e30c129SJesús Blázquez 
434e30c129SJesús Blázquez 	/* Handle NaN according to ISO/IEC 60559. NaN argument -> NaN return */
44*f6e1f27eSJesús Blázquez 	if (isnan(x) || isnan(y))
454e30c129SJesús Blázquez 		return (NAN);
464e30c129SJesús Blázquez 
474e30c129SJesús Blázquez 	long double ax = fabsl(x);
484e30c129SJesús Blázquez 	long double ay = fabsl(y);
494e30c129SJesús Blázquez 
504e30c129SJesús Blázquez 	if (ay > ax)
514e30c129SJesús Blázquez 		return (y);
524e30c129SJesús Blázquez 	if (ax > ay)
534e30c129SJesús Blázquez 		return (x);
544e30c129SJesús Blázquez 
554e30c129SJesús Blázquez 	/* If magnitudes are equal, we break the tie with the sign */
564e30c129SJesús Blázquez 	if (u[0].bits.sign != u[1].bits.sign)
574e30c129SJesús Blázquez 		return (u[0].bits.sign ? y : x);
584e30c129SJesús Blázquez 
594e30c129SJesús Blázquez 	return (x);
604e30c129SJesús Blázquez }
614e30c129SJesús Blázquez 
62