s_fmin.c revision 131320
1261363Sgshapiro/*-
264562Sgshapiro * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
338032Speter * All rights reserved.
438032Speter *
538032Speter * Redistribution and use in source and binary forms, with or without
638032Speter * modification, are permitted provided that the following conditions
738032Speter * are met:
838032Speter * 1. Redistributions of source code must retain the above copyright
938032Speter *    notice, this list of conditions and the following disclaimer.
1038032Speter * 2. Redistributions in binary form must reproduce the above copyright
1138032Speter *    notice, this list of conditions and the following disclaimer in the
12266692Sgshapiro *    documentation and/or other materials provided with the distribution.
1338032Speter *
1438032Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1580785Sgshapiro * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1680785Sgshapiro * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1780785Sgshapiro * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1880785Sgshapiro * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1980785Sgshapiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2080785Sgshapiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2180785Sgshapiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2280785Sgshapiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23110560Sgshapiro * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24110560Sgshapiro * SUCH DAMAGE.
2580785Sgshapiro */
2680785Sgshapiro
2780785Sgshapiro#include <sys/cdefs.h>
2880785Sgshapiro__FBSDID("$FreeBSD: head/lib/msun/src/s_fmin.c 131320 2004-06-30 07:04:01Z das $");
2980785Sgshapiro
3080785Sgshapiro#include <math.h>
3180785Sgshapiro
3280785Sgshapiro#include "fpmath.h"
3380785Sgshapiro
3480785Sgshapirodouble
3580785Sgshapirofmin(double x, double y)
3680785Sgshapiro{
3780785Sgshapiro	union IEEEd2bits u[2];
3880785Sgshapiro
3980785Sgshapiro	u[0].d = x;
4080785Sgshapiro	u[1].d = y;
4180785Sgshapiro
4280785Sgshapiro	/* Check for NaNs to avoid raising spurious exceptions. */
4380785Sgshapiro	if (u[0].bits.exp == 2047 && (u[0].bits.manh | u[0].bits.manl) != 0)
4480785Sgshapiro		return (y);
4580785Sgshapiro	if (u[1].bits.exp == 2047 && (u[1].bits.manh | u[1].bits.manl) != 0)
4680785Sgshapiro		return (x);
4780785Sgshapiro
4880785Sgshapiro	/* Handle comparisons of signed zeroes. */
4980785Sgshapiro	if (u[0].bits.sign != u[1].bits.sign)
5080785Sgshapiro		return (u[u[1].bits.sign].d);
5138032Speter
5238032Speter	return (x < y ? x : y);
5338032Speter}
5438032Speter