xref: /openbsd-src/lib/libm/src/s_fmax.c (revision 2f2c00629eff6a304ebffb255fc56f4fa7a1833b)
1*2f2c0062Sguenther /*	$OpenBSD: s_fmax.c,v 1.11 2016/09/12 19:47:02 guenther Exp $	*/
27b36286aSmartynas /*-
37b36286aSmartynas  * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
47b36286aSmartynas  * All rights reserved.
57b36286aSmartynas  *
67b36286aSmartynas  * Redistribution and use in source and binary forms, with or without
77b36286aSmartynas  * modification, are permitted provided that the following conditions
87b36286aSmartynas  * are met:
97b36286aSmartynas  * 1. Redistributions of source code must retain the above copyright
107b36286aSmartynas  *    notice, this list of conditions and the following disclaimer.
117b36286aSmartynas  * 2. Redistributions in binary form must reproduce the above copyright
127b36286aSmartynas  *    notice, this list of conditions and the following disclaimer in the
137b36286aSmartynas  *    documentation and/or other materials provided with the distribution.
147b36286aSmartynas  *
157b36286aSmartynas  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
167b36286aSmartynas  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
177b36286aSmartynas  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
187b36286aSmartynas  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
197b36286aSmartynas  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
207b36286aSmartynas  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
217b36286aSmartynas  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
227b36286aSmartynas  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
237b36286aSmartynas  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
247b36286aSmartynas  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
257b36286aSmartynas  * SUCH DAMAGE.
267b36286aSmartynas  */
277b36286aSmartynas 
28390c8400Smartynas #include <float.h>
297b36286aSmartynas #include <math.h>
307b36286aSmartynas 
317b36286aSmartynas double
fmax(double x,double y)327b36286aSmartynas fmax(double x, double y)
337b36286aSmartynas {
347b36286aSmartynas 	/* Check for NaNs to avoid raising spurious exceptions. */
35f73fafd8Smartynas 	if (isnan(x))
367b36286aSmartynas 		return (y);
37f73fafd8Smartynas 	if (isnan(y))
387b36286aSmartynas 		return (x);
397b36286aSmartynas 
407b36286aSmartynas 	/* Handle comparisons of signed zeroes. */
4196943daaSmartynas 	if (signbit(x) != signbit(y)) {
42f73fafd8Smartynas 		if (signbit(x))
437b36286aSmartynas 			return (y);
44f73fafd8Smartynas 		else
457b36286aSmartynas 			return (x);
4696943daaSmartynas 	}
477b36286aSmartynas 
487b36286aSmartynas 	return (x > y ? x : y);
497b36286aSmartynas }
50*2f2c0062Sguenther DEF_STD(fmax);
51*2f2c0062Sguenther LDBL_MAYBE_UNUSED_CLONE(fmax);
52