xref: /minix3/lib/libm/src/s_fmax.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
12fe8fb19SBen Gras /*-
22fe8fb19SBen Gras  * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
32fe8fb19SBen Gras  * All rights reserved.
42fe8fb19SBen Gras  *
52fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
62fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
72fe8fb19SBen Gras  * are met:
82fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
92fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
102fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
122fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
132fe8fb19SBen Gras  *
142fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242fe8fb19SBen Gras  * SUCH DAMAGE.
252fe8fb19SBen Gras  */
262fe8fb19SBen Gras 
272fe8fb19SBen Gras #include <sys/cdefs.h>
28*84d9c625SLionel Sambuc __RCSID("$NetBSD: s_fmax.c,v 1.3 2013/11/29 22:16:10 joerg Exp $");
292fe8fb19SBen Gras #ifdef notdef
302fe8fb19SBen Gras __FBSDID("$FreeBSD: src/lib/msun/src/s_fmax.c,v 1.1 2004/06/30 07:04:01 das Exp $");
312fe8fb19SBen Gras #endif
322fe8fb19SBen Gras 
332fe8fb19SBen Gras #include <math.h>
342fe8fb19SBen Gras 
352fe8fb19SBen Gras #include <machine/ieee.h>
362fe8fb19SBen Gras 
37*84d9c625SLionel Sambuc #ifndef __HAVE_LONG_DOUBLE
__strong_alias(fmaxl,fmax)38*84d9c625SLionel Sambuc __strong_alias(fmaxl, fmax)
39*84d9c625SLionel Sambuc #endif
40*84d9c625SLionel Sambuc 
412fe8fb19SBen Gras double
422fe8fb19SBen Gras fmax(double x, double y)
432fe8fb19SBen Gras {
442fe8fb19SBen Gras 	union ieee_double_u u[2];
452fe8fb19SBen Gras 
462fe8fb19SBen Gras 	u[0].dblu_d = x;
472fe8fb19SBen Gras 	u[1].dblu_d = y;
482fe8fb19SBen Gras 
492fe8fb19SBen Gras 	/* Check for NaNs to avoid raising spurious exceptions. */
502fe8fb19SBen Gras 	if (u[0].dblu_dbl.dbl_exp == DBL_EXP_INFNAN &&
512fe8fb19SBen Gras 	    (u[0].dblu_dbl.dbl_frach | u[0].dblu_dbl.dbl_fracl) != 0)
522fe8fb19SBen Gras 		return (y);
532fe8fb19SBen Gras 	if (u[1].dblu_dbl.dbl_exp == DBL_EXP_INFNAN &&
542fe8fb19SBen Gras 	    (u[1].dblu_dbl.dbl_frach | u[1].dblu_dbl.dbl_fracl) != 0)
552fe8fb19SBen Gras 		return (x);
562fe8fb19SBen Gras 
572fe8fb19SBen Gras 	/* Handle comparisons of signed zeroes. */
582fe8fb19SBen Gras 	if (u[0].dblu_dbl.dbl_sign != u[1].dblu_dbl.dbl_sign)
592fe8fb19SBen Gras 		return (u[u[0].dblu_dbl.dbl_sign].dblu_d);
602fe8fb19SBen Gras 
612fe8fb19SBen Gras 	return (x > y ? x : y);
622fe8fb19SBen Gras }
63