Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / include / c99_math.h
index 9e31d0693b66440b17db865de3c79d9e65628154..8a67fb133d6e26013c0e5be8e5d84d33d0a5b5e9 100644 (file)
 
 #if defined(_MSC_VER)
 
-#if _MSC_VER < 1400 && !defined(__cplusplus)
-
-static inline float cosf( float f )
-{
-   return (float) cos( (double) f );
-}
-
-static inline float sinf( float f )
-{
-   return (float) sin( (double) f );
-}
-
-static inline float ceilf( float f )
-{
-   return (float) ceil( (double) f );
-}
-
-static inline float floorf( float f )
-{
-   return (float) floor( (double) f );
-}
-
-static inline float powf( float f, float g )
-{
-   return (float) pow( (double) f, (double) g );
-}
-
-static inline float sqrtf( float f )
-{
-   return (float) sqrt( (double) f );
-}
-
-static inline float fabsf( float f )
-{
-   return (float) fabs( (double) f );
-}
-
-static inline float logf( float f )
-{
-   return (float) log( (double) f );
-}
-
-static inline float frexpf(float x, int *exp)
-{
-   return (float) frexp(x, exp);
-}
-
-static inline float ldexpf(float x, int exp)
-{
-   return (float) ldexp(x, exp);
-}
-
-static inline float logf(float x)
-{
-   return (float) log(x);
-}
-
-static inline float expf(float x)
-{
-   return (float) exp(x);
-}
-
-
-#else
-/* Work-around an extra semi-colon in VS 2005 logf definition */
-#ifdef logf
-#undef logf
-#define logf(x) ((float)log((double)(x)))
-#endif /* logf */
+/* This is to ensure that we get M_PI, etc. definitions */
+#if !defined(_USE_MATH_DEFINES)
+#error _USE_MATH_DEFINES define required when building with MSVC
+#endif 
 
 #if _MSC_VER < 1800
 #define isfinite(x) _finite((double)(x))
 #define isnan(x) _isnan((double)(x))
 #endif /* _MSC_VER < 1800 */
-#endif /* _MSC_VER < 1400 && !defined(__cplusplus) */
 
 #if _MSC_VER < 1800
 static inline double log2( double x )
@@ -137,6 +71,7 @@ roundf(float x)
 #endif
 
 #ifndef INFINITY
+#include <float.h> // DBL_MAX
 #define INFINITY (DBL_MAX + DBL_MAX)
 #endif
 
@@ -147,7 +82,12 @@ roundf(float x)
 #endif /* _MSC_VER */
 
 
-#if __STDC_VERSION__ < 199901L && (!defined(__cplusplus) || defined(_MSC_VER))
+#if (defined(_MSC_VER) && _MSC_VER < 1800) || \
+    (!defined(_MSC_VER) && \
+     __STDC_VERSION__ < 199901L && \
+     (!defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 600) && \
+     !defined(__cplusplus))
+
 static inline long int
 lrint(double d)
 {
@@ -199,6 +139,19 @@ llrintf(float f)
 
    return rounded;
 }
+
+static inline float
+exp2f(float f)
+{
+   return powf(2.0f, f);
+}
+
+static inline double
+exp2(double d)
+{
+   return pow(2.0, d);
+}
+
 #endif /* C99 */
 
 
@@ -210,4 +163,65 @@ llrintf(float f)
 #endif
 
 
+#ifndef M_PI
+#define M_PI (3.14159265358979323846)
+#endif
+
+#ifndef M_E
+#define M_E (2.7182818284590452354)
+#endif
+
+#ifndef M_LOG2E
+#define M_LOG2E (1.4426950408889634074)
+#endif
+
+#ifndef FLT_MAX_EXP
+#define FLT_MAX_EXP 128
+#endif
+
+
+#if defined(fpclassify)
+/* ISO C99 says that fpclassify is a macro.  Assume that any implementation
+ * of fpclassify, whether it's in a C99 compiler or not, will be a macro.
+ */
+#elif defined(__cplusplus)
+/* For C++, fpclassify() should be defined in <cmath> */
+#elif defined(_MSC_VER)
+/* Not required on VS2013 and above.  Oddly, the fpclassify() function
+ * doesn't exist in such a form on MSVC.  This is an implementation using
+ * slightly different lower-level Windows functions.
+ */
+#include <float.h>
+
+static inline enum {FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, FP_NORMAL}
+fpclassify(double x)
+{
+   switch(_fpclass(x)) {
+   case _FPCLASS_SNAN: /* signaling NaN */
+   case _FPCLASS_QNAN: /* quiet NaN */
+      return FP_NAN;
+   case _FPCLASS_NINF: /* negative infinity */
+   case _FPCLASS_PINF: /* positive infinity */
+      return FP_INFINITE;
+   case _FPCLASS_NN:   /* negative normal */
+   case _FPCLASS_PN:   /* positive normal */
+      return FP_NORMAL;
+   case _FPCLASS_ND:   /* negative denormalized */
+   case _FPCLASS_PD:   /* positive denormalized */
+      return FP_SUBNORMAL;
+   case _FPCLASS_NZ:   /* negative zero */
+   case _FPCLASS_PZ:   /* positive zero */
+      return FP_ZERO;
+   default:
+      /* Should never get here; but if we do, this will guarantee
+       * that the pattern is not treated like a number.
+       */
+      return FP_NAN;
+   }
+}
+#else
+#error "Need to include or define an fpclassify function"
+#endif
+
+
 #endif /* #define _C99_MATH_H_ */