xref: /freebsd/lib/msun/src/s_fmaxf.c (revision 0dd5a5603e7a33d976f8e6015620bbc79839c609)
14f82cb46SDavid Schultz /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
44f82cb46SDavid Schultz  * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
54f82cb46SDavid Schultz  * All rights reserved.
64f82cb46SDavid Schultz  *
74f82cb46SDavid Schultz  * Redistribution and use in source and binary forms, with or without
84f82cb46SDavid Schultz  * modification, are permitted provided that the following conditions
94f82cb46SDavid Schultz  * are met:
104f82cb46SDavid Schultz  * 1. Redistributions of source code must retain the above copyright
114f82cb46SDavid Schultz  *    notice, this list of conditions and the following disclaimer.
124f82cb46SDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
134f82cb46SDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
144f82cb46SDavid Schultz  *    documentation and/or other materials provided with the distribution.
154f82cb46SDavid Schultz  *
164f82cb46SDavid Schultz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
174f82cb46SDavid Schultz  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184f82cb46SDavid Schultz  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194f82cb46SDavid Schultz  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
204f82cb46SDavid Schultz  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214f82cb46SDavid Schultz  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224f82cb46SDavid Schultz  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234f82cb46SDavid Schultz  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244f82cb46SDavid Schultz  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254f82cb46SDavid Schultz  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264f82cb46SDavid Schultz  * SUCH DAMAGE.
274f82cb46SDavid Schultz  */
284f82cb46SDavid Schultz 
294f82cb46SDavid Schultz #include <math.h>
304f82cb46SDavid Schultz 
314f82cb46SDavid Schultz #include "fpmath.h"
324f82cb46SDavid Schultz 
33b2e84316SAndrew Turner #ifdef USE_BUILTIN_FMAXF
34b2e84316SAndrew Turner float
fmaxf(float x,float y)35b2e84316SAndrew Turner fmaxf(float x, float y)
36b2e84316SAndrew Turner {
37b2e84316SAndrew Turner 	return (__builtin_fmaxf(x, y));
38b2e84316SAndrew Turner }
39b2e84316SAndrew Turner #else
404f82cb46SDavid Schultz float
fmaxf(float x,float y)414f82cb46SDavid Schultz fmaxf(float x, float y)
424f82cb46SDavid Schultz {
434f82cb46SDavid Schultz 	union IEEEf2bits u[2];
444f82cb46SDavid Schultz 
454f82cb46SDavid Schultz 	u[0].f = x;
464f82cb46SDavid Schultz 	u[1].f = y;
474f82cb46SDavid Schultz 
484f82cb46SDavid Schultz 	/* Check for NaNs to avoid raising spurious exceptions. */
494f82cb46SDavid Schultz 	if (u[0].bits.exp == 255 && u[0].bits.man != 0)
504f82cb46SDavid Schultz 		return (y);
514f82cb46SDavid Schultz 	if (u[1].bits.exp == 255 && u[1].bits.man != 0)
524f82cb46SDavid Schultz 		return (x);
534f82cb46SDavid Schultz 
544f82cb46SDavid Schultz 	/* Handle comparisons of signed zeroes. */
554f82cb46SDavid Schultz 	if (u[0].bits.sign != u[1].bits.sign)
564f82cb46SDavid Schultz 		return (u[u[0].bits.sign].f);
574f82cb46SDavid Schultz 
584f82cb46SDavid Schultz 	return (x > y ? x : y);
594f82cb46SDavid Schultz }
60b2e84316SAndrew Turner #endif
61