xref: /minix3/lib/libm/src/s_fmaxl.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_fmaxl.c,v 1.4 2013/11/29 22:16:10 joerg Exp $");
292fe8fb19SBen Gras #ifdef notdef
302fe8fb19SBen Gras __FBSDID("$FreeBSD: src/lib/msun/src/s_fmaxl.c,v 1.1 2004/06/30 07:04:01 das Exp $");
312fe8fb19SBen Gras #endif
322fe8fb19SBen Gras 
33*84d9c625SLionel Sambuc #include <string.h>
342fe8fb19SBen Gras #include <math.h>
352fe8fb19SBen Gras 
362fe8fb19SBen Gras #include <machine/ieee.h>
37*84d9c625SLionel Sambuc #ifdef __HAVE_LONG_DOUBLE
382fe8fb19SBen Gras long double
fmaxl(long double x,long double y)392fe8fb19SBen Gras fmaxl(long double x, long double y)
402fe8fb19SBen Gras {
412fe8fb19SBen Gras 	union ieee_ext_u u[2];
422fe8fb19SBen Gras 
43*84d9c625SLionel Sambuc 	memset(&u, 0, sizeof u);
442fe8fb19SBen Gras 	u[0].extu_ld = x;
452fe8fb19SBen Gras 	u[0].extu_ext.ext_frach &= ~0x80000000;
462fe8fb19SBen Gras 	u[1].extu_ld = y;
472fe8fb19SBen Gras 	u[1].extu_ext.ext_frach &= ~0x80000000;
482fe8fb19SBen Gras 
492fe8fb19SBen Gras 	/* Check for NaNs to avoid raising spurious exceptions. */
502fe8fb19SBen Gras 	if (u[0].extu_ext.ext_exp == EXT_EXP_INFNAN &&
512fe8fb19SBen Gras 	    (u[0].extu_ext.ext_frach | u[0].extu_ext.ext_fracl) != 0)
522fe8fb19SBen Gras 		return (y);
532fe8fb19SBen Gras 	if (u[1].extu_ext.ext_exp == EXT_EXP_INFNAN &&
542fe8fb19SBen Gras 	    (u[1].extu_ext.ext_frach | u[1].extu_ext.ext_fracl) != 0)
552fe8fb19SBen Gras 		return (x);
562fe8fb19SBen Gras 
572fe8fb19SBen Gras 	/* Handle comparisons of ext_signed zeroes. */
582fe8fb19SBen Gras 	if (u[0].extu_ext.ext_sign != u[1].extu_ext.ext_sign)
592fe8fb19SBen Gras 		return (u[0].extu_ext.ext_sign ? y : x);
602fe8fb19SBen Gras 
612fe8fb19SBen Gras 	return (x > y ? x : y);
622fe8fb19SBen Gras }
632fe8fb19SBen Gras #endif
64