| 1 |
/* -*- c -*- */ |
| 2 |
/* |
| 3 |
* vim:syntax=c |
| 4 |
* |
| 5 |
* Low-level routines related to IEEE-754 format |
| 6 |
* |
| 7 |
* Adapted from https://github.com/numpy/numpy/blob/master/numpy/core/src/npymath/ieee754.c.src, |
| 8 |
* removing all functions except for npy_set_floatstatus_invalid, which is |
| 9 |
* made static and has is renamed to _npy_set_floatstatus_invalid. |
| 10 |
* |
| 11 |
* |
| 12 |
* Copyright (c) 2005-2017, NumPy Developers. |
| 13 |
* All rights reserved. |
| 14 |
* |
| 15 |
* Redistribution and use in source and binary forms, with or without |
| 16 |
* modification, are permitted provided that the following conditions are |
| 17 |
* met: |
| 18 |
* |
| 19 |
* * Redistributions of source code must retain the above copyright |
| 20 |
* notice, this list of conditions and the following disclaimer. |
| 21 |
* |
| 22 |
* * Redistributions in binary form must reproduce the above |
| 23 |
* copyright notice, this list of conditions and the following |
| 24 |
* disclaimer in the documentation and/or other materials provided |
| 25 |
* with the distribution. |
| 26 |
* |
| 27 |
* * Neither the name of the NumPy Developers nor the names of any |
| 28 |
* contributors may be used to endorse or promote products derived |
| 29 |
* from this software without specific prior written permission. |
| 30 |
* |
| 31 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 32 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 33 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 34 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 35 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 36 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 37 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 38 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 39 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 40 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 41 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 42 |
*/ |
| 43 |
#include "numpy/utils.h" |
| 44 |
|
| 45 |
|
| 46 |
#if (defined(__unix__) || defined(unix)) && !defined(USG) |
| 47 |
#include <sys/param.h> |
| 48 |
#endif |
| 49 |
|
| 50 |
|
| 51 |
/* Solaris --------------------------------------------------------*/ |
| 52 |
/* --------ignoring SunOS ieee_flags approach, someone else can |
| 53 |
** deal with that! */ |
| 54 |
#if defined(sun) || defined(__BSD__) || defined(__OpenBSD__) || \ |
| 55 |
(defined(__FreeBSD__) && (__FreeBSD_version < 502114)) || \ |
| 56 |
defined(__NetBSD__) |
| 57 |
#include <ieeefp.h> |
| 58 |
|
| 59 |
static void _npy_set_floatstatus_invalid(void) |
| 60 |
{ |
| 61 |
fpsetsticky(FP_X_INV); |
| 62 |
} |
| 63 |
|
| 64 |
#elif defined(_AIX) |
| 65 |
#include <float.h> |
| 66 |
#include <fpxcp.h> |
| 67 |
|
| 68 |
static void _npy_set_floatstatus_invalid(void) |
| 69 |
{ |
| 70 |
fp_raise_xcp(FP_INVALID); |
| 71 |
} |
| 72 |
|
| 73 |
#elif defined(_MSC_VER) || (defined(__osf__) && defined(__alpha)) |
| 74 |
|
| 75 |
/* |
| 76 |
* By using a volatile floating point value, |
| 77 |
* the compiler is forced to actually do the requested |
| 78 |
* operations because of potential concurrency. |
| 79 |
* |
| 80 |
* We shouldn't write multiple values to a single |
| 81 |
* global here, because that would cause |
| 82 |
* a race condition. |
| 83 |
*/ |
| 84 |
static volatile double _npy_floatstatus_x, _npy_floatstatus_inf; |
| 85 |
|
| 86 |
static void _npy_set_floatstatus_invalid(void) |
| 87 |
{ |
| 88 |
_npy_floatstatus_inf = NPY_INFINITY; |
| 89 |
_npy_floatstatus_x = _npy_floatstatus_inf - NPY_INFINITY; |
| 90 |
} |
| 91 |
|
| 92 |
#else |
| 93 |
/* General GCC code, should work on most platforms */ |
| 94 |
# include <fenv.h> |
| 95 |
|
| 96 |
static void _npy_set_floatstatus_invalid(void) |
| 97 |
{ |
| 98 |
feraiseexcept(FE_INVALID); |
| 99 |
} |
| 100 |
|
| 101 |
#endif |