1131320Sdas/*-
2131320Sdas * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
3131320Sdas * All rights reserved.
4131320Sdas *
5131320Sdas * Redistribution and use in source and binary forms, with or without
6131320Sdas * modification, are permitted provided that the following conditions
7131320Sdas * are met:
8131320Sdas * 1. Redistributions of source code must retain the above copyright
9131320Sdas *    notice, this list of conditions and the following disclaimer.
10131320Sdas * 2. Redistributions in binary form must reproduce the above copyright
11131320Sdas *    notice, this list of conditions and the following disclaimer in the
12131320Sdas *    documentation and/or other materials provided with the distribution.
13131320Sdas *
14131320Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15131320Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16131320Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17131320Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18131320Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19131320Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20131320Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21131320Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22131320Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23131320Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24131320Sdas * SUCH DAMAGE.
25131320Sdas */
26131320Sdas
27131320Sdas#include <sys/cdefs.h>
28131320Sdas__FBSDID("$FreeBSD$");
29131320Sdas
30131320Sdas#include <math.h>
31131320Sdas
32131320Sdas#include "fpmath.h"
33131320Sdas
34131320Sdaslong double
35131320Sdasfminl(long double x, long double y)
36131320Sdas{
37131320Sdas	union IEEEl2bits u[2];
38131320Sdas
39131320Sdas	u[0].e = x;
40131320Sdas	mask_nbit_l(u[0]);
41131320Sdas	u[1].e = y;
42131320Sdas	mask_nbit_l(u[1]);
43131320Sdas
44131320Sdas	/* Check for NaNs to avoid raising spurious exceptions. */
45131320Sdas	if (u[0].bits.exp == 32767 && (u[0].bits.manh | u[0].bits.manl) != 0)
46131320Sdas		return (y);
47131320Sdas	if (u[1].bits.exp == 32767 && (u[1].bits.manh | u[1].bits.manl) != 0)
48131320Sdas		return (x);
49131320Sdas
50131320Sdas	/* Handle comparisons of signed zeroes. */
51131320Sdas	if (u[0].bits.sign != u[1].bits.sign)
52131320Sdas		return (u[1].bits.sign ? y : x);
53131320Sdas
54131320Sdas	return (x < y ? x : y);
55131320Sdas}
56