1110566Smike/*-
2110566Smike * Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
3141379Sdas * Copyright (c) 2002, 2003 David Schultz <das@FreeBSD.ORG>
4110566Smike * All rights reserved.
5110566Smike *
6110566Smike * Redistribution and use in source and binary forms, with or without
7110566Smike * modification, are permitted provided that the following conditions
8110566Smike * are met:
9110566Smike * 1. Redistributions of source code must retain the above copyright
10110566Smike *    notice, this list of conditions and the following disclaimer.
11110566Smike * 2. Redistributions in binary form must reproduce the above copyright
12110566Smike *    notice, this list of conditions and the following disclaimer in the
13110566Smike *    documentation and/or other materials provided with the distribution.
14110566Smike *
15110566Smike * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16110566Smike * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17110566Smike * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18110566Smike * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19110566Smike * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20110566Smike * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21110566Smike * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22110566Smike * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23110566Smike * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24110566Smike * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25110566Smike * SUCH DAMAGE.
26110566Smike *
27110566Smike * $FreeBSD$
28110566Smike */
29110566Smike
30110566Smike#include <sys/endian.h>
31110566Smike
32110566Smike#include <math.h>
33110566Smike#include <stdint.h>
34110566Smike
35110566Smike#include "fpmath.h"
36110566Smike
37110566Smikeint
38110566Smike__fpclassifyf(float f)
39110566Smike{
40110566Smike	union IEEEf2bits u;
41110566Smike
42110566Smike	u.f = f;
43110566Smike	if (u.bits.exp == 0) {
44110566Smike		if (u.bits.man == 0)
45110566Smike			return (FP_ZERO);
46110566Smike		return (FP_SUBNORMAL);
47110566Smike	}
48110566Smike	if (u.bits.exp == 255) {
49110566Smike		if (u.bits.man == 0)
50110566Smike			return (FP_INFINITE);
51110566Smike		return (FP_NAN);
52110566Smike	}
53110566Smike	return (FP_NORMAL);
54110566Smike}
55110566Smike
56110566Smikeint
57110566Smike__fpclassifyd(double d)
58110566Smike{
59110566Smike	union IEEEd2bits u;
60110566Smike
61110566Smike	u.d = d;
62110566Smike	if (u.bits.exp == 0) {
63110566Smike		if ((u.bits.manl | u.bits.manh) == 0)
64110566Smike			return (FP_ZERO);
65110566Smike		return (FP_SUBNORMAL);
66110566Smike	}
67110566Smike	if (u.bits.exp == 2047) {
68110566Smike		if ((u.bits.manl | u.bits.manh) == 0)
69110566Smike			return (FP_INFINITE);
70110566Smike		return (FP_NAN);
71110566Smike	}
72110566Smike	return (FP_NORMAL);
73110566Smike}
74110566Smike
75110566Smikeint
76110566Smike__fpclassifyl(long double e)
77110566Smike{
78110566Smike	union IEEEl2bits u;
79110566Smike
80110566Smike	u.e = e;
81110566Smike	if (u.bits.exp == 0) {
82110566Smike		if ((u.bits.manl | u.bits.manh) == 0)
83110566Smike			return (FP_ZERO);
84110566Smike		return (FP_SUBNORMAL);
85110566Smike	}
86110566Smike	mask_nbit_l(u);		/* Mask normalization bit if applicable. */
87110566Smike	if (u.bits.exp == 32767) {
88110566Smike		if ((u.bits.manl | u.bits.manh) == 0)
89110566Smike			return (FP_INFINITE);
90110566Smike		return (FP_NAN);
91110566Smike	}
92110566Smike	return (FP_NORMAL);
93110566Smike}
94