c99_math.h revision 01e04c3f
101e04c3fSmrg/************************************************************************** 201e04c3fSmrg * 301e04c3fSmrg * Copyright 2007-2015 VMware, Inc. 401e04c3fSmrg * All Rights Reserved. 501e04c3fSmrg * 601e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a 701e04c3fSmrg * copy of this software and associated documentation files (the 801e04c3fSmrg * "Software"), to deal in the Software without restriction, including 901e04c3fSmrg * without limitation the rights to use, copy, modify, merge, publish, 1001e04c3fSmrg * distribute, sub license, and/or sell copies of the Software, and to 1101e04c3fSmrg * permit persons to whom the Software is furnished to do so, subject to 1201e04c3fSmrg * the following conditions: 1301e04c3fSmrg * 1401e04c3fSmrg * The above copyright notice and this permission notice (including the 1501e04c3fSmrg * next paragraph) shall be included in all copies or substantial portions 1601e04c3fSmrg * of the Software. 1701e04c3fSmrg * 1801e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 1901e04c3fSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2001e04c3fSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 2101e04c3fSmrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 2201e04c3fSmrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 2301e04c3fSmrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 2401e04c3fSmrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2501e04c3fSmrg * 2601e04c3fSmrg **************************************************************************/ 2701e04c3fSmrg 2801e04c3fSmrg/** 2901e04c3fSmrg * Wrapper for math.h which makes sure we have definitions of all the c99 3001e04c3fSmrg * functions. 3101e04c3fSmrg */ 3201e04c3fSmrg 3301e04c3fSmrg 3401e04c3fSmrg#ifndef _C99_MATH_H_ 3501e04c3fSmrg#define _C99_MATH_H_ 3601e04c3fSmrg 3701e04c3fSmrg#include <math.h> 3801e04c3fSmrg#include "c99_compat.h" 3901e04c3fSmrg 4001e04c3fSmrg 4101e04c3fSmrg/* This is to ensure that we get M_PI, etc. definitions */ 4201e04c3fSmrg#if defined(_MSC_VER) && !defined(_USE_MATH_DEFINES) 4301e04c3fSmrg#error _USE_MATH_DEFINES define required when building with MSVC 4401e04c3fSmrg#endif 4501e04c3fSmrg 4601e04c3fSmrg 4701e04c3fSmrg#if !defined(_MSC_VER) && \ 4801e04c3fSmrg __STDC_VERSION__ < 199901L && \ 4901e04c3fSmrg (!defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 600) && \ 5001e04c3fSmrg !defined(__cplusplus) 5101e04c3fSmrg 5201e04c3fSmrgstatic inline long int 5301e04c3fSmrglrint(double d) 5401e04c3fSmrg{ 5501e04c3fSmrg long int rounded = (long int)(d + 0.5); 5601e04c3fSmrg 5701e04c3fSmrg if (d - floor(d) == 0.5) { 5801e04c3fSmrg if (rounded % 2 != 0) 5901e04c3fSmrg rounded += (d > 0) ? -1 : 1; 6001e04c3fSmrg } 6101e04c3fSmrg 6201e04c3fSmrg return rounded; 6301e04c3fSmrg} 6401e04c3fSmrg 6501e04c3fSmrgstatic inline long int 6601e04c3fSmrglrintf(float f) 6701e04c3fSmrg{ 6801e04c3fSmrg long int rounded = (long int)(f + 0.5f); 6901e04c3fSmrg 7001e04c3fSmrg if (f - floorf(f) == 0.5f) { 7101e04c3fSmrg if (rounded % 2 != 0) 7201e04c3fSmrg rounded += (f > 0) ? -1 : 1; 7301e04c3fSmrg } 7401e04c3fSmrg 7501e04c3fSmrg return rounded; 7601e04c3fSmrg} 7701e04c3fSmrg 7801e04c3fSmrgstatic inline long long int 7901e04c3fSmrgllrint(double d) 8001e04c3fSmrg{ 8101e04c3fSmrg long long int rounded = (long long int)(d + 0.5); 8201e04c3fSmrg 8301e04c3fSmrg if (d - floor(d) == 0.5) { 8401e04c3fSmrg if (rounded % 2 != 0) 8501e04c3fSmrg rounded += (d > 0) ? -1 : 1; 8601e04c3fSmrg } 8701e04c3fSmrg 8801e04c3fSmrg return rounded; 8901e04c3fSmrg} 9001e04c3fSmrg 9101e04c3fSmrgstatic inline long long int 9201e04c3fSmrgllrintf(float f) 9301e04c3fSmrg{ 9401e04c3fSmrg long long int rounded = (long long int)(f + 0.5f); 9501e04c3fSmrg 9601e04c3fSmrg if (f - floorf(f) == 0.5f) { 9701e04c3fSmrg if (rounded % 2 != 0) 9801e04c3fSmrg rounded += (f > 0) ? -1 : 1; 9901e04c3fSmrg } 10001e04c3fSmrg 10101e04c3fSmrg return rounded; 10201e04c3fSmrg} 10301e04c3fSmrg 10401e04c3fSmrgstatic inline float 10501e04c3fSmrgexp2f(float f) 10601e04c3fSmrg{ 10701e04c3fSmrg return powf(2.0f, f); 10801e04c3fSmrg} 10901e04c3fSmrg 11001e04c3fSmrgstatic inline double 11101e04c3fSmrgexp2(double d) 11201e04c3fSmrg{ 11301e04c3fSmrg return pow(2.0, d); 11401e04c3fSmrg} 11501e04c3fSmrg 11601e04c3fSmrg#endif /* C99 */ 11701e04c3fSmrg 11801e04c3fSmrg 11901e04c3fSmrg/* 12001e04c3fSmrg * signbit() is a macro on Linux. Not available on Windows. 12101e04c3fSmrg */ 12201e04c3fSmrg#ifndef signbit 12301e04c3fSmrg#define signbit(x) ((x) < 0.0f) 12401e04c3fSmrg#endif 12501e04c3fSmrg 12601e04c3fSmrg 12701e04c3fSmrg#ifndef M_PI 12801e04c3fSmrg#define M_PI (3.14159265358979323846) 12901e04c3fSmrg#endif 13001e04c3fSmrg 13101e04c3fSmrg#ifndef M_E 13201e04c3fSmrg#define M_E (2.7182818284590452354) 13301e04c3fSmrg#endif 13401e04c3fSmrg 13501e04c3fSmrg#ifndef M_LOG2E 13601e04c3fSmrg#define M_LOG2E (1.4426950408889634074) 13701e04c3fSmrg#endif 13801e04c3fSmrg 13901e04c3fSmrg#ifndef FLT_MAX_EXP 14001e04c3fSmrg#define FLT_MAX_EXP 128 14101e04c3fSmrg#endif 14201e04c3fSmrg 14301e04c3fSmrg 14401e04c3fSmrg#if defined(fpclassify) 14501e04c3fSmrg/* ISO C99 says that fpclassify is a macro. Assume that any implementation 14601e04c3fSmrg * of fpclassify, whether it's in a C99 compiler or not, will be a macro. 14701e04c3fSmrg */ 14801e04c3fSmrg#elif defined(__cplusplus) 14901e04c3fSmrg/* For C++, fpclassify() should be defined in <cmath> */ 15001e04c3fSmrg#elif defined(_MSC_VER) 15101e04c3fSmrg/* Not required on VS2013 and above. Oddly, the fpclassify() function 15201e04c3fSmrg * doesn't exist in such a form on MSVC. This is an implementation using 15301e04c3fSmrg * slightly different lower-level Windows functions. 15401e04c3fSmrg */ 15501e04c3fSmrg#include <float.h> 15601e04c3fSmrg 15701e04c3fSmrgstatic inline enum {FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, FP_NORMAL} 15801e04c3fSmrgfpclassify(double x) 15901e04c3fSmrg{ 16001e04c3fSmrg switch(_fpclass(x)) { 16101e04c3fSmrg case _FPCLASS_SNAN: /* signaling NaN */ 16201e04c3fSmrg case _FPCLASS_QNAN: /* quiet NaN */ 16301e04c3fSmrg return FP_NAN; 16401e04c3fSmrg case _FPCLASS_NINF: /* negative infinity */ 16501e04c3fSmrg case _FPCLASS_PINF: /* positive infinity */ 16601e04c3fSmrg return FP_INFINITE; 16701e04c3fSmrg case _FPCLASS_NN: /* negative normal */ 16801e04c3fSmrg case _FPCLASS_PN: /* positive normal */ 16901e04c3fSmrg return FP_NORMAL; 17001e04c3fSmrg case _FPCLASS_ND: /* negative denormalized */ 17101e04c3fSmrg case _FPCLASS_PD: /* positive denormalized */ 17201e04c3fSmrg return FP_SUBNORMAL; 17301e04c3fSmrg case _FPCLASS_NZ: /* negative zero */ 17401e04c3fSmrg case _FPCLASS_PZ: /* positive zero */ 17501e04c3fSmrg return FP_ZERO; 17601e04c3fSmrg default: 17701e04c3fSmrg /* Should never get here; but if we do, this will guarantee 17801e04c3fSmrg * that the pattern is not treated like a number. 17901e04c3fSmrg */ 18001e04c3fSmrg return FP_NAN; 18101e04c3fSmrg } 18201e04c3fSmrg} 18301e04c3fSmrg#else 18401e04c3fSmrg#error "Need to include or define an fpclassify function" 18501e04c3fSmrg#endif 18601e04c3fSmrg 18701e04c3fSmrg 18801e04c3fSmrg/* Since C++11, the following functions are part of the std namespace. Their C 18901e04c3fSmrg * counteparts should still exist in the global namespace, however cmath 19001e04c3fSmrg * undefines those functions, which in glibc 2.23, are defined as macros rather 19101e04c3fSmrg * than functions as in glibc 2.22. 19201e04c3fSmrg */ 19301e04c3fSmrg#if __cplusplus >= 201103L && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 23)) 19401e04c3fSmrg#include <cmath> 19501e04c3fSmrg 19601e04c3fSmrgusing std::fpclassify; 19701e04c3fSmrgusing std::isfinite; 19801e04c3fSmrgusing std::isinf; 19901e04c3fSmrgusing std::isnan; 20001e04c3fSmrgusing std::isnormal; 20101e04c3fSmrgusing std::signbit; 20201e04c3fSmrgusing std::isgreater; 20301e04c3fSmrgusing std::isgreaterequal; 20401e04c3fSmrgusing std::isless; 20501e04c3fSmrgusing std::islessequal; 20601e04c3fSmrgusing std::islessgreater; 20701e04c3fSmrgusing std::isunordered; 20801e04c3fSmrg#endif 20901e04c3fSmrg 21001e04c3fSmrg 21101e04c3fSmrg#endif /* #define _C99_MATH_H_ */ 212