nuclear@10: /* nuclear@10: libvmath - a vector math library nuclear@10: Copyright (C) 2004-2015 John Tsiombikas nuclear@10: nuclear@10: This program is free software: you can redistribute it and/or modify nuclear@10: it under the terms of the GNU Lesser General Public License as published nuclear@10: by the Free Software Foundation, either version 3 of the License, or nuclear@10: (at your option) any later version. nuclear@10: nuclear@10: This program is distributed in the hope that it will be useful, nuclear@10: but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@10: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@10: GNU Lesser General Public License for more details. nuclear@10: nuclear@10: You should have received a copy of the GNU Lesser General Public License nuclear@10: along with this program. If not, see . nuclear@10: */ nuclear@10: #include nuclear@10: #include nuclear@10: #include "vmath.h" nuclear@10: nuclear@10: #if defined(__APPLE__) && !defined(TARGET_IPHONE) nuclear@10: #include nuclear@10: nuclear@10: void enable_fpexcept(void) nuclear@10: { nuclear@10: unsigned int bits; nuclear@10: bits = _MM_MASK_INVALID | _MM_MASK_DIV_ZERO | _MM_MASK_OVERFLOW | _MM_MASK_UNDERFLOW; nuclear@10: _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~bits); nuclear@10: } nuclear@10: nuclear@10: void disable_fpexcept(void) nuclear@10: { nuclear@10: unsigned int bits; nuclear@10: bits = _MM_MASK_INVALID | _MM_MASK_DIV_ZERO | _MM_MASK_OVERFLOW | _MM_MASK_UNDERFLOW; nuclear@10: _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() | bits); nuclear@10: } nuclear@10: nuclear@10: #elif defined(__GNUC__) && !defined(TARGET_IPHONE) && !defined(__MINGW32__) nuclear@10: #define __USE_GNU nuclear@10: #include nuclear@10: nuclear@10: void enable_fpexcept(void) nuclear@10: { nuclear@10: feenableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW); nuclear@10: } nuclear@10: nuclear@10: void disable_fpexcept(void) nuclear@10: { nuclear@10: fedisableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW); nuclear@10: } nuclear@10: nuclear@10: #elif defined(_MSC_VER) || defined(__MINGW32__) nuclear@10: #include nuclear@10: nuclear@10: #if defined(__MINGW32__) && !defined(_EM_OVERFLOW) nuclear@10: /* if gcc's float.h gets precedence, the mingw MSVC includes won't be declared */ nuclear@10: #define _MCW_EM 0x8001f nuclear@10: #define _EM_INVALID 0x10 nuclear@10: #define _EM_ZERODIVIDE 0x08 nuclear@10: #define _EM_OVERFLOW 0x04 nuclear@10: unsigned int __cdecl _clearfp(void); nuclear@10: unsigned int __cdecl _controlfp(unsigned int, unsigned int); nuclear@10: #endif nuclear@10: nuclear@10: void enable_fpexcept(void) nuclear@10: { nuclear@10: _clearfp(); nuclear@10: _controlfp(_controlfp(0, 0) & ~(_EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW), _MCW_EM); nuclear@10: } nuclear@10: nuclear@10: void disable_fpexcept(void) nuclear@10: { nuclear@10: _clearfp(); nuclear@10: _controlfp(_controlfp(0, 0) | (_EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW), _MCW_EM); nuclear@10: } nuclear@10: #else nuclear@10: void enable_fpexcept(void) {} nuclear@10: void disable_fpexcept(void) {} nuclear@10: #endif nuclear@10: nuclear@10: nuclear@10: /** Numerical calculation of integrals using simpson's rule */ nuclear@10: scalar_t integral(scalar_t (*f)(scalar_t), scalar_t low, scalar_t high, int samples) nuclear@10: { nuclear@10: int i; nuclear@10: scalar_t h = (high - low) / (scalar_t)samples; nuclear@10: scalar_t sum = 0.0; nuclear@10: nuclear@10: for(i=0; i