xref: /dragonfly/lib/libc/gen/fpclassify.c (revision 6ff43c94)
1*6ff43c94SPeter Avalos /*-
2*6ff43c94SPeter Avalos  * Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
3*6ff43c94SPeter Avalos  * Copyright (c) 2002, 2003 David Schultz <das@FreeBSD.ORG>
4*6ff43c94SPeter Avalos  * All rights reserved.
5*6ff43c94SPeter Avalos  *
6*6ff43c94SPeter Avalos  * Redistribution and use in source and binary forms, with or without
7*6ff43c94SPeter Avalos  * modification, are permitted provided that the following conditions
8*6ff43c94SPeter Avalos  * are met:
9*6ff43c94SPeter Avalos  * 1. Redistributions of source code must retain the above copyright
10*6ff43c94SPeter Avalos  *    notice, this list of conditions and the following disclaimer.
11*6ff43c94SPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
12*6ff43c94SPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
13*6ff43c94SPeter Avalos  *    documentation and/or other materials provided with the distribution.
14*6ff43c94SPeter Avalos  *
15*6ff43c94SPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*6ff43c94SPeter Avalos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*6ff43c94SPeter Avalos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*6ff43c94SPeter Avalos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*6ff43c94SPeter Avalos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*6ff43c94SPeter Avalos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*6ff43c94SPeter Avalos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*6ff43c94SPeter Avalos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*6ff43c94SPeter Avalos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*6ff43c94SPeter Avalos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*6ff43c94SPeter Avalos  * SUCH DAMAGE.
26*6ff43c94SPeter Avalos  *
27*6ff43c94SPeter Avalos  * $FreeBSD: head/lib/libc/gen/fpclassify.c 141379 2005-02-06 03:23:31Z das $
28*6ff43c94SPeter Avalos  */
29*6ff43c94SPeter Avalos 
30*6ff43c94SPeter Avalos #include <sys/endian.h>
31*6ff43c94SPeter Avalos 
32*6ff43c94SPeter Avalos #include <math.h>
33*6ff43c94SPeter Avalos #include <stdint.h>
34*6ff43c94SPeter Avalos 
35*6ff43c94SPeter Avalos #include "fpmath.h"
36*6ff43c94SPeter Avalos 
37*6ff43c94SPeter Avalos int
__fpclassifyf(float f)38*6ff43c94SPeter Avalos __fpclassifyf(float f)
39*6ff43c94SPeter Avalos {
40*6ff43c94SPeter Avalos 	union IEEEf2bits u;
41*6ff43c94SPeter Avalos 
42*6ff43c94SPeter Avalos 	u.f = f;
43*6ff43c94SPeter Avalos 	if (u.bits.exp == 0) {
44*6ff43c94SPeter Avalos 		if (u.bits.man == 0)
45*6ff43c94SPeter Avalos 			return (FP_ZERO);
46*6ff43c94SPeter Avalos 		return (FP_SUBNORMAL);
47*6ff43c94SPeter Avalos 	}
48*6ff43c94SPeter Avalos 	if (u.bits.exp == 255) {
49*6ff43c94SPeter Avalos 		if (u.bits.man == 0)
50*6ff43c94SPeter Avalos 			return (FP_INFINITE);
51*6ff43c94SPeter Avalos 		return (FP_NAN);
52*6ff43c94SPeter Avalos 	}
53*6ff43c94SPeter Avalos 	return (FP_NORMAL);
54*6ff43c94SPeter Avalos }
55*6ff43c94SPeter Avalos 
56*6ff43c94SPeter Avalos int
__fpclassifyd(double d)57*6ff43c94SPeter Avalos __fpclassifyd(double d)
58*6ff43c94SPeter Avalos {
59*6ff43c94SPeter Avalos 	union IEEEd2bits u;
60*6ff43c94SPeter Avalos 
61*6ff43c94SPeter Avalos 	u.d = d;
62*6ff43c94SPeter Avalos 	if (u.bits.exp == 0) {
63*6ff43c94SPeter Avalos 		if ((u.bits.manl | u.bits.manh) == 0)
64*6ff43c94SPeter Avalos 			return (FP_ZERO);
65*6ff43c94SPeter Avalos 		return (FP_SUBNORMAL);
66*6ff43c94SPeter Avalos 	}
67*6ff43c94SPeter Avalos 	if (u.bits.exp == 2047) {
68*6ff43c94SPeter Avalos 		if ((u.bits.manl | u.bits.manh) == 0)
69*6ff43c94SPeter Avalos 			return (FP_INFINITE);
70*6ff43c94SPeter Avalos 		return (FP_NAN);
71*6ff43c94SPeter Avalos 	}
72*6ff43c94SPeter Avalos 	return (FP_NORMAL);
73*6ff43c94SPeter Avalos }
74*6ff43c94SPeter Avalos 
75*6ff43c94SPeter Avalos int
__fpclassifyl(long double e)76*6ff43c94SPeter Avalos __fpclassifyl(long double e)
77*6ff43c94SPeter Avalos {
78*6ff43c94SPeter Avalos 	union IEEEl2bits u;
79*6ff43c94SPeter Avalos 
80*6ff43c94SPeter Avalos 	u.e = e;
81*6ff43c94SPeter Avalos 	if (u.bits.exp == 0) {
82*6ff43c94SPeter Avalos 		if ((u.bits.manl | u.bits.manh) == 0)
83*6ff43c94SPeter Avalos 			return (FP_ZERO);
84*6ff43c94SPeter Avalos 		return (FP_SUBNORMAL);
85*6ff43c94SPeter Avalos 	}
86*6ff43c94SPeter Avalos 	mask_nbit_l(u);		/* Mask normalization bit if applicable. */
87*6ff43c94SPeter Avalos 	if (u.bits.exp == 32767) {
88*6ff43c94SPeter Avalos 		if ((u.bits.manl | u.bits.manh) == 0)
89*6ff43c94SPeter Avalos 			return (FP_INFINITE);
90*6ff43c94SPeter Avalos 		return (FP_NAN);
91*6ff43c94SPeter Avalos 	}
92*6ff43c94SPeter Avalos 	return (FP_NORMAL);
93*6ff43c94SPeter Avalos }
94