xref: /freebsd/lib/msun/src/s_fmin.c (revision fae95359ee6dbdb28fe0d0f79e33ec16dfb3f501)
14f82cb46SDavid Schultz /*-
24f82cb46SDavid Schultz  * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
34f82cb46SDavid Schultz  * All rights reserved.
44f82cb46SDavid Schultz  *
54f82cb46SDavid Schultz  * Redistribution and use in source and binary forms, with or without
64f82cb46SDavid Schultz  * modification, are permitted provided that the following conditions
74f82cb46SDavid Schultz  * are met:
84f82cb46SDavid Schultz  * 1. Redistributions of source code must retain the above copyright
94f82cb46SDavid Schultz  *    notice, this list of conditions and the following disclaimer.
104f82cb46SDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
114f82cb46SDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
124f82cb46SDavid Schultz  *    documentation and/or other materials provided with the distribution.
134f82cb46SDavid Schultz  *
144f82cb46SDavid Schultz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
154f82cb46SDavid Schultz  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
164f82cb46SDavid Schultz  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
174f82cb46SDavid Schultz  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
184f82cb46SDavid Schultz  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
194f82cb46SDavid Schultz  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
204f82cb46SDavid Schultz  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
214f82cb46SDavid Schultz  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
224f82cb46SDavid Schultz  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
234f82cb46SDavid Schultz  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
244f82cb46SDavid Schultz  * SUCH DAMAGE.
254f82cb46SDavid Schultz  */
264f82cb46SDavid Schultz 
274f82cb46SDavid Schultz #include <sys/cdefs.h>
284f82cb46SDavid Schultz __FBSDID("$FreeBSD$");
294f82cb46SDavid Schultz 
30*fae95359SRuslan Bukin #include <float.h>
314f82cb46SDavid Schultz #include <math.h>
324f82cb46SDavid Schultz 
334f82cb46SDavid Schultz #include "fpmath.h"
344f82cb46SDavid Schultz 
354f82cb46SDavid Schultz double
364f82cb46SDavid Schultz fmin(double x, double y)
374f82cb46SDavid Schultz {
384f82cb46SDavid Schultz 	union IEEEd2bits u[2];
394f82cb46SDavid Schultz 
404f82cb46SDavid Schultz 	u[0].d = x;
414f82cb46SDavid Schultz 	u[1].d = y;
424f82cb46SDavid Schultz 
434f82cb46SDavid Schultz 	/* Check for NaNs to avoid raising spurious exceptions. */
444f82cb46SDavid Schultz 	if (u[0].bits.exp == 2047 && (u[0].bits.manh | u[0].bits.manl) != 0)
454f82cb46SDavid Schultz 		return (y);
464f82cb46SDavid Schultz 	if (u[1].bits.exp == 2047 && (u[1].bits.manh | u[1].bits.manl) != 0)
474f82cb46SDavid Schultz 		return (x);
484f82cb46SDavid Schultz 
494f82cb46SDavid Schultz 	/* Handle comparisons of signed zeroes. */
504f82cb46SDavid Schultz 	if (u[0].bits.sign != u[1].bits.sign)
514f82cb46SDavid Schultz 		return (u[u[1].bits.sign].d);
524f82cb46SDavid Schultz 
534f82cb46SDavid Schultz 	return (x < y ? x : y);
544f82cb46SDavid Schultz }
55*fae95359SRuslan Bukin 
56*fae95359SRuslan Bukin #if (LDBL_MANT_DIG == 53)
57*fae95359SRuslan Bukin __weak_reference(fmin, fminl);
58*fae95359SRuslan Bukin #endif
59