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: releng/11.0/lib/libc/gen/fpclassify.c 283694 2015-05-29 09:26:10Z andrew $
28110566Smike */
29110566Smike
30110566Smike#include <sys/endian.h>
31110566Smike
32283694Sandrew#include <machine/float.h>
33283694Sandrew
34110566Smike#include <math.h>
35110566Smike#include <stdint.h>
36110566Smike
37110566Smike#include "fpmath.h"
38110566Smike
39110566Smikeint
40110566Smike__fpclassifyf(float f)
41110566Smike{
42110566Smike	union IEEEf2bits u;
43110566Smike
44110566Smike	u.f = f;
45110566Smike	if (u.bits.exp == 0) {
46110566Smike		if (u.bits.man == 0)
47110566Smike			return (FP_ZERO);
48110566Smike		return (FP_SUBNORMAL);
49110566Smike	}
50110566Smike	if (u.bits.exp == 255) {
51110566Smike		if (u.bits.man == 0)
52110566Smike			return (FP_INFINITE);
53110566Smike		return (FP_NAN);
54110566Smike	}
55110566Smike	return (FP_NORMAL);
56110566Smike}
57110566Smike
58110566Smikeint
59110566Smike__fpclassifyd(double d)
60110566Smike{
61110566Smike	union IEEEd2bits u;
62110566Smike
63110566Smike	u.d = d;
64110566Smike	if (u.bits.exp == 0) {
65110566Smike		if ((u.bits.manl | u.bits.manh) == 0)
66110566Smike			return (FP_ZERO);
67110566Smike		return (FP_SUBNORMAL);
68110566Smike	}
69110566Smike	if (u.bits.exp == 2047) {
70110566Smike		if ((u.bits.manl | u.bits.manh) == 0)
71110566Smike			return (FP_INFINITE);
72110566Smike		return (FP_NAN);
73110566Smike	}
74110566Smike	return (FP_NORMAL);
75110566Smike}
76110566Smike
77110566Smikeint
78110566Smike__fpclassifyl(long double e)
79110566Smike{
80110566Smike	union IEEEl2bits u;
81110566Smike
82110566Smike	u.e = e;
83110566Smike	if (u.bits.exp == 0) {
84110566Smike		if ((u.bits.manl | u.bits.manh) == 0)
85110566Smike			return (FP_ZERO);
86110566Smike		return (FP_SUBNORMAL);
87110566Smike	}
88110566Smike	mask_nbit_l(u);		/* Mask normalization bit if applicable. */
89283694Sandrew#if LDBL_MANT_DIG == 53
90283694Sandrew	if (u.bits.exp == 2047) {
91283694Sandrew		if ((u.bits.manl | u.bits.manh) == 0)
92283694Sandrew			return (FP_INFINITE);
93283694Sandrew		return (FP_NAN);
94283694Sandrew	}
95283694Sandrew#else
96110566Smike	if (u.bits.exp == 32767) {
97110566Smike		if ((u.bits.manl | u.bits.manh) == 0)
98110566Smike			return (FP_INFINITE);
99110566Smike		return (FP_NAN);
100110566Smike	}
101283694Sandrew#endif
102110566Smike	return (FP_NORMAL);
103110566Smike}
104