blob: 823109327534e219ab9c0a3731a30933e6f6e65b [file] [log] [blame]
Elliott Hughes02c78a32014-04-11 17:02:20 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/types.h>
30
31#include <math.h>
32#include <machine/ieee.h>
33
34// These aren't declared in our <math.h>.
35extern "C" int __isinf(double);
36extern "C" int __isnan(double);
37
38union float_u {
39 float f;
40 ieee_single bits;
41};
42
43union double_u {
44 double d;
45 ieee_double bits;
46};
47
48int __fpclassifyd(double d) {
49 double_u u;
50 u.d = d;
51 if (u.bits.dbl_exp == 0) {
52 return ((u.bits.dbl_fracl | u.bits.dbl_frach) == 0) ? FP_ZERO : FP_SUBNORMAL;
53 }
54 if (u.bits.dbl_exp == DBL_EXP_INFNAN) {
55 return ((u.bits.dbl_fracl | u.bits.dbl_frach) == 0) ? FP_INFINITE : FP_NAN;
56 }
57 return FP_NORMAL;
58}
59__strong_alias(__fpclassify, __fpclassifyd); // glibc uses __fpclassify, BSD __fpclassifyd.
60
61int __fpclassifyf(float f) {
62 float_u u;
63 u.f = f;
64 if (u.bits.sng_exp == 0) {
65 return (u.bits.sng_frac == 0) ? FP_ZERO : FP_SUBNORMAL;
66 }
67 if (u.bits.sng_exp == SNG_EXP_INFNAN) {
68 return (u.bits.sng_frac == 0) ? FP_INFINITE : FP_NAN;
69 }
70 return FP_NORMAL;
71}
72
73int __isinf(double d) {
74 return (__fpclassifyd(d) == FP_INFINITE);
75}
76__strong_alias(isinf, __isinf);
77
78int __isinff(float f) {
79 return (__fpclassifyf(f) == FP_INFINITE);
80}
81__strong_alias(isinff, __isinff);
82
83int __isnan(double d) {
84 return (__fpclassifyd(d) == FP_NAN);
85}
86__strong_alias(isnan, __isnan);
87
88int __isnanf(float f) {
89 return (__fpclassifyf(f) == FP_NAN);
90}
91__strong_alias(isnanf, __isnanf);
92
93#if __LP64__
94
95// LP64 uses 128-bit long doubles.
96
97union long_double_u {
98 long double ld;
99 struct {
100 unsigned long fracl:64;
101 unsigned long frach:48;
102 unsigned int exp:15;
103 unsigned int sign:1;
104 } bits;
105};
106
107int __fpclassifyl(long double ld) {
108 long_double_u u;
109 u.ld = ld;
110 if (u.bits.exp == 0) {
111 return ((u.bits.fracl | u.bits.frach) == 0) ? FP_ZERO : FP_SUBNORMAL;
112 }
113 if (u.bits.exp == EXT_EXP_INFNAN) {
114 return ((u.bits.fracl | u.bits.frach) == 0) ? FP_INFINITE : FP_NAN;
115 }
116 return FP_NORMAL;
117}
118
119int __isinfl(long double ld) {
120 return (__fpclassifyl(ld) == FP_INFINITE);
121}
122
123int __isnanl(long double ld) {
124 return (__fpclassifyl(ld) == FP_NAN);
125}
126
127#else
128
129// LP32 uses double as long double.
130
131__strong_alias(__fpclassifyl, __fpclassify);
132__strong_alias(__isinfl, __isinf);
133__strong_alias(__isnanl, __isnan);
134
135#endif
136
137__strong_alias(isinfl, __isinfl);
138__strong_alias(isnanl, __isnanl);