Clone of mesa.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. * Wrapper for math.h which makes sure we have definitions of all the c99
  29. * functions.
  30. */
  31. #ifndef _C99_MATH_H_
  32. #define _C99_MATH_H_
  33. #include <math.h>
  34. #include "c99_compat.h"
  35. #if defined(_MSC_VER)
  36. /* This is to ensure that we get M_PI, etc. definitions */
  37. #if !defined(_USE_MATH_DEFINES)
  38. #error _USE_MATH_DEFINES define required when building with MSVC
  39. #endif
  40. #if _MSC_VER < 1800
  41. #define isfinite(x) _finite((double)(x))
  42. #define isnan(x) _isnan((double)(x))
  43. #endif /* _MSC_VER < 1800 */
  44. #if _MSC_VER < 1800
  45. static inline double log2( double x )
  46. {
  47. const double invln2 = 1.442695041;
  48. return log( x ) * invln2;
  49. }
  50. static inline double
  51. round(double x)
  52. {
  53. return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
  54. }
  55. static inline float
  56. roundf(float x)
  57. {
  58. return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
  59. }
  60. #endif
  61. #ifndef INFINITY
  62. #include <float.h> // DBL_MAX
  63. #define INFINITY (DBL_MAX + DBL_MAX)
  64. #endif
  65. #ifndef NAN
  66. #define NAN (INFINITY - INFINITY)
  67. #endif
  68. #endif /* _MSC_VER */
  69. #if (defined(_MSC_VER) && _MSC_VER < 1800) || \
  70. (!defined(_MSC_VER) && \
  71. __STDC_VERSION__ < 199901L && \
  72. (!defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 600) && \
  73. !defined(__cplusplus))
  74. static inline long int
  75. lrint(double d)
  76. {
  77. long int rounded = (long int)(d + 0.5);
  78. if (d - floor(d) == 0.5) {
  79. if (rounded % 2 != 0)
  80. rounded += (d > 0) ? -1 : 1;
  81. }
  82. return rounded;
  83. }
  84. static inline long int
  85. lrintf(float f)
  86. {
  87. long int rounded = (long int)(f + 0.5f);
  88. if (f - floorf(f) == 0.5f) {
  89. if (rounded % 2 != 0)
  90. rounded += (f > 0) ? -1 : 1;
  91. }
  92. return rounded;
  93. }
  94. static inline long long int
  95. llrint(double d)
  96. {
  97. long long int rounded = (long long int)(d + 0.5);
  98. if (d - floor(d) == 0.5) {
  99. if (rounded % 2 != 0)
  100. rounded += (d > 0) ? -1 : 1;
  101. }
  102. return rounded;
  103. }
  104. static inline long long int
  105. llrintf(float f)
  106. {
  107. long long int rounded = (long long int)(f + 0.5f);
  108. if (f - floorf(f) == 0.5f) {
  109. if (rounded % 2 != 0)
  110. rounded += (f > 0) ? -1 : 1;
  111. }
  112. return rounded;
  113. }
  114. static inline float
  115. exp2f(float f)
  116. {
  117. return powf(2.0f, f);
  118. }
  119. static inline double
  120. exp2(double d)
  121. {
  122. return pow(2.0, d);
  123. }
  124. #endif /* C99 */
  125. /*
  126. * signbit() is a macro on Linux. Not available on Windows.
  127. */
  128. #ifndef signbit
  129. #define signbit(x) ((x) < 0.0f)
  130. #endif
  131. #ifndef M_PI
  132. #define M_PI (3.14159265358979323846)
  133. #endif
  134. #ifndef M_E
  135. #define M_E (2.7182818284590452354)
  136. #endif
  137. #ifndef M_LOG2E
  138. #define M_LOG2E (1.4426950408889634074)
  139. #endif
  140. #ifndef FLT_MAX_EXP
  141. #define FLT_MAX_EXP 128
  142. #endif
  143. #if defined(fpclassify)
  144. /* ISO C99 says that fpclassify is a macro. Assume that any implementation
  145. * of fpclassify, whether it's in a C99 compiler or not, will be a macro.
  146. */
  147. #elif defined(__cplusplus)
  148. /* For C++, fpclassify() should be defined in <cmath> */
  149. #elif defined(_MSC_VER)
  150. /* Not required on VS2013 and above. Oddly, the fpclassify() function
  151. * doesn't exist in such a form on MSVC. This is an implementation using
  152. * slightly different lower-level Windows functions.
  153. */
  154. #include <float.h>
  155. static inline enum {FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, FP_NORMAL}
  156. fpclassify(double x)
  157. {
  158. switch(_fpclass(x)) {
  159. case _FPCLASS_SNAN: /* signaling NaN */
  160. case _FPCLASS_QNAN: /* quiet NaN */
  161. return FP_NAN;
  162. case _FPCLASS_NINF: /* negative infinity */
  163. case _FPCLASS_PINF: /* positive infinity */
  164. return FP_INFINITE;
  165. case _FPCLASS_NN: /* negative normal */
  166. case _FPCLASS_PN: /* positive normal */
  167. return FP_NORMAL;
  168. case _FPCLASS_ND: /* negative denormalized */
  169. case _FPCLASS_PD: /* positive denormalized */
  170. return FP_SUBNORMAL;
  171. case _FPCLASS_NZ: /* negative zero */
  172. case _FPCLASS_PZ: /* positive zero */
  173. return FP_ZERO;
  174. default:
  175. /* Should never get here; but if we do, this will guarantee
  176. * that the pattern is not treated like a number.
  177. */
  178. return FP_NAN;
  179. }
  180. }
  181. #else
  182. #error "Need to include or define an fpclassify function"
  183. #endif
  184. #endif /* #define _C99_MATH_H_ */