Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright 2007-2015 VMware, Inc.
  4.  * All Rights Reserved.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the
  8.  * "Software"), to deal in the Software without restriction, including
  9.  * without limitation the rights to use, copy, modify, merge, publish,
  10.  * distribute, sub license, and/or sell copies of the Software, and to
  11.  * permit persons to whom the Software is furnished to do so, subject to
  12.  * the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice (including the
  15.  * next paragraph) shall be included in all copies or substantial portions
  16.  * of the Software.
  17.  *
  18.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21.  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
  22.  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23.  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24.  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  *
  26.  **************************************************************************/
  27.  
  28. /**
  29.  * Wrapper for math.h which makes sure we have definitions of all the c99
  30.  * functions.
  31.  */
  32.  
  33.  
  34. #ifndef _C99_MATH_H_
  35. #define _C99_MATH_H_
  36.  
  37. #include <math.h>
  38. #include "c99_compat.h"
  39.  
  40.  
  41. #if defined(_MSC_VER)
  42.  
  43. /* This is to ensure that we get M_PI, etc. definitions */
  44. #if !defined(_USE_MATH_DEFINES)
  45. #error _USE_MATH_DEFINES define required when building with MSVC
  46. #endif
  47.  
  48. #if _MSC_VER < 1800
  49. #define isfinite(x) _finite((double)(x))
  50. #define isnan(x) _isnan((double)(x))
  51. #endif /* _MSC_VER < 1800 */
  52.  
  53. #if _MSC_VER < 1800
  54. static inline double log2( double x )
  55. {
  56.    const double invln2 = 1.442695041;
  57.    return log( x ) * invln2;
  58. }
  59.  
  60. static inline double
  61. round(double x)
  62. {
  63.    return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
  64. }
  65.  
  66. static inline float
  67. roundf(float x)
  68. {
  69.    return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
  70. }
  71. #endif
  72.  
  73. #ifndef INFINITY
  74. #include <float.h> // DBL_MAX
  75. #define INFINITY (DBL_MAX + DBL_MAX)
  76. #endif
  77.  
  78. #ifndef NAN
  79. #define NAN (INFINITY - INFINITY)
  80. #endif
  81.  
  82. #endif /* _MSC_VER */
  83.  
  84.  
  85. #if (defined(_MSC_VER) && _MSC_VER < 1800) || \
  86.     (!defined(_MSC_VER) && \
  87.      __STDC_VERSION__ < 199901L && \
  88.      (!defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 600) && \
  89.      !defined(__cplusplus))
  90.  
  91. static inline long int
  92. lrint(double d)
  93. {
  94.    long int rounded = (long int)(d + 0.5);
  95.  
  96.    if (d - floor(d) == 0.5) {
  97.       if (rounded % 2 != 0)
  98.          rounded += (d > 0) ? -1 : 1;
  99.    }
  100.  
  101.    return rounded;
  102. }
  103.  
  104. static inline long int
  105. lrintf(float f)
  106. {
  107.    long int rounded = (long int)(f + 0.5f);
  108.  
  109.    if (f - floorf(f) == 0.5f) {
  110.       if (rounded % 2 != 0)
  111.          rounded += (f > 0) ? -1 : 1;
  112.    }
  113.  
  114.    return rounded;
  115. }
  116.  
  117. static inline long long int
  118. llrint(double d)
  119. {
  120.    long long int rounded = (long long int)(d + 0.5);
  121.  
  122.    if (d - floor(d) == 0.5) {
  123.       if (rounded % 2 != 0)
  124.          rounded += (d > 0) ? -1 : 1;
  125.    }
  126.  
  127.    return rounded;
  128. }
  129.  
  130. static inline long long int
  131. llrintf(float f)
  132. {
  133.    long long int rounded = (long long int)(f + 0.5f);
  134.  
  135.    if (f - floorf(f) == 0.5f) {
  136.       if (rounded % 2 != 0)
  137.          rounded += (f > 0) ? -1 : 1;
  138.    }
  139.  
  140.    return rounded;
  141. }
  142.  
  143. #endif /* C99 */
  144.  
  145.  
  146. /*
  147.  * signbit() is a macro on Linux.  Not available on Windows.
  148.  */
  149. #ifndef signbit
  150. #define signbit(x) ((x) < 0.0f)
  151. #endif
  152.  
  153.  
  154. #ifndef M_PI
  155. #define M_PI (3.14159265358979323846)
  156. #endif
  157.  
  158. #ifndef M_E
  159. #define M_E (2.7182818284590452354)
  160. #endif
  161.  
  162. #ifndef M_LOG2E
  163. #define M_LOG2E (1.4426950408889634074)
  164. #endif
  165.  
  166. #ifndef FLT_MAX_EXP
  167. #define FLT_MAX_EXP 128
  168. #endif
  169.  
  170.  
  171. #if defined(fpclassify)
  172. /* ISO C99 says that fpclassify is a macro.  Assume that any implementation
  173.  * of fpclassify, whether it's in a C99 compiler or not, will be a macro.
  174.  */
  175. #elif defined(__cplusplus)
  176. /* For C++, fpclassify() should be defined in <cmath> */
  177. #elif defined(_MSC_VER)
  178. /* Not required on VS2013 and above.  Oddly, the fpclassify() function
  179.  * doesn't exist in such a form on MSVC.  This is an implementation using
  180.  * slightly different lower-level Windows functions.
  181.  */
  182. #include <float.h>
  183.  
  184. static inline enum {FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, FP_NORMAL}
  185. fpclassify(double x)
  186. {
  187.    switch(_fpclass(x)) {
  188.    case _FPCLASS_SNAN: /* signaling NaN */
  189.    case _FPCLASS_QNAN: /* quiet NaN */
  190.       return FP_NAN;
  191.    case _FPCLASS_NINF: /* negative infinity */
  192.    case _FPCLASS_PINF: /* positive infinity */
  193.       return FP_INFINITE;
  194.    case _FPCLASS_NN:   /* negative normal */
  195.    case _FPCLASS_PN:   /* positive normal */
  196.       return FP_NORMAL;
  197.    case _FPCLASS_ND:   /* negative denormalized */
  198.    case _FPCLASS_PD:   /* positive denormalized */
  199.       return FP_SUBNORMAL;
  200.    case _FPCLASS_NZ:   /* negative zero */
  201.    case _FPCLASS_PZ:   /* positive zero */
  202.       return FP_ZERO;
  203.    default:
  204.       /* Should never get here; but if we do, this will guarantee
  205.        * that the pattern is not treated like a number.
  206.        */
  207.       return FP_NAN;
  208.    }
  209. }
  210. #else
  211. #error "Need to include or define an fpclassify function"
  212. #endif
  213.  
  214.  
  215. #endif /* #define _C99_MATH_H_ */
  216.