math-compat.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* GStreamer
  2. * Copyright (C) 2010 Tim-Philipp Müller <tim centricular net>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. */
  19. #ifndef __GST_MATH_COMPAT_H__
  20. #define __GST_MATH_COMPAT_H__
  21. /* This header is not included automatically via gst/gst.h, you need to
  22. * include it explicitly if you need it. */
  23. #ifndef _USE_MATH_DEFINES
  24. #define _USE_MATH_DEFINES /* so MSVC defines M_PI etc. */
  25. #endif
  26. #include <math.h>
  27. #include <glib.h>
  28. G_BEGIN_DECLS
  29. /* http://en.wikipedia.org/wiki/Math.h */
  30. #define __GST_MATH_COMPAT_NEED_RINT
  31. #define __GST_MATH_COMPAT_NEED_RINTF
  32. #define __GST_MATH_COMPAT_NEED_ISNAN
  33. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  34. #undef __GST_MATH_COMPAT_NEED_RINT
  35. #undef __GST_MATH_COMPAT_NEED_RINTF
  36. #undef __GST_MATH_COMPAT_NEED_ISNAN
  37. #endif
  38. #if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
  39. #undef __GST_MATH_COMPAT_NEED_RINT
  40. #undef __GST_MATH_COMPAT_NEED_RINTF
  41. #undef __GST_MATH_COMPAT_NEED_ISNAN
  42. #endif
  43. #ifndef M_PI
  44. #define M_PI G_PI
  45. #endif
  46. #ifndef M_PI_2
  47. #define M_PI_2 G_PI_2
  48. #endif
  49. #ifndef M_PI_4
  50. #define M_PI_4 G_PI_4
  51. #endif
  52. static inline double
  53. __gst_math_compat_rint (double x)
  54. {
  55. return floor (x + 0.5);
  56. }
  57. static inline float
  58. __gst_math_compat_rintf (float x)
  59. {
  60. return floorf (x + 0.5);
  61. }
  62. static inline gboolean
  63. __gst_math_compat_isnan (double x)
  64. {
  65. return x != x;
  66. }
  67. #if defined (__GST_MATH_COMPAT_NEED_RINT) && !defined (rint)
  68. #define rint(x) __gst_math_compat_rint(x)
  69. #endif
  70. #if defined (__GST_MATH_COMPAT_NEED_RINTF) && !defined (rintf)
  71. #define rintf(x) __gst_math_compat_rintf(x)
  72. #endif
  73. #if defined (__GST_MATH_COMPAT_NEED_ISNAN) && !defined (isnan)
  74. #define isnan(x) __gst_math_compat_isnan (x)
  75. #endif
  76. #ifndef NAN
  77. #if G_BYTE_ORDER == G_BIG_ENDIAN
  78. #define __GST_NAN_BYTES { 0x7f, 0xc0, 0, 0 }
  79. #elif G_BYTE_ORDER == G_LITTLE_ENDIAN
  80. #define __GST_NAN_BYTES { 0, 0, 0xc0, 0x7f }
  81. #endif
  82. static union {
  83. unsigned char __c[4];
  84. float __d;
  85. } __gst_nan_union G_GNUC_UNUSED = {
  86. __GST_NAN_BYTES
  87. };
  88. #define NAN (__gst_nan_union.__d)
  89. #endif
  90. G_END_DECLS
  91. #endif /* __GST_MATH_COMPAT_H__ */