| 1 |
/* Copyright (C) 2017, bel2125 |
| 2 |
* |
| 3 |
* This library is free software; you can redistribute it and/or |
| 4 |
* modify it under the terms of the GNU Lesser General Public |
| 5 |
* License as published by the Free Software Foundation; either |
| 6 |
* version 2.1 of the License, or (at your option) any later version. |
| 7 |
* |
| 8 |
* This library is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
* Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public |
| 14 |
* License along with this library; if not, write to the |
| 15 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
| 16 |
* MA 02110-1301, USA. |
| 17 |
*/ |
| 18 |
/* Note: Within "check", this file uses the LGPL license. |
| 19 |
* If you need only this file, you may find a MIT licensed version in |
| 20 |
* the "floatmagic" repository of the initial author. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "libcompat.h" |
| 24 |
|
| 25 |
double DOUBLE_ZERO = 0.0; |
| 26 |
|
| 27 |
#if defined(NEED_fpclassify) |
| 28 |
|
| 29 |
#if defined(HAVE_STDINT_H) |
| 30 |
#include <stdint.h> |
| 31 |
typedef uint64_t bitfield64; |
| 32 |
#elif defined(_MSC_VER) |
| 33 |
typedef unsigned __int64 bitfield64; |
| 34 |
#else |
| 35 |
typedef unsigned long long bitfield64; |
| 36 |
#endif |
| 37 |
|
| 38 |
static bitfield64 ms = 0x8000000000000000; |
| 39 |
static bitfield64 me = 0x7FF0000000000000; |
| 40 |
static bitfield64 mf = 0x000FFFFFFFFFFFFF; |
| 41 |
|
| 42 |
int fpclassify(double d) |
| 43 |
{ |
| 44 |
bitfield64 *p = (bitfield64 *)&d; |
| 45 |
if ((*p & me) != me) { |
| 46 |
/* finite */ |
| 47 |
if (*p & mf) { |
| 48 |
/* finite and not null */ |
| 49 |
if (*p & me) { |
| 50 |
return FP_NORMAL; |
| 51 |
} |
| 52 |
return FP_SUBNORMAL; |
| 53 |
} |
| 54 |
return FP_ZERO; |
| 55 |
} |
| 56 |
if (*p & mf) { |
| 57 |
return FP_NAN; |
| 58 |
} |
| 59 |
return FP_INFINITE; |
| 60 |
} |
| 61 |
|
| 62 |
#endif |
| 63 |
|