You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
3.6KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav 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. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * Replacements for frequently missing libm functions
  21. */
  22. #ifndef AVUTIL_LIBM_H
  23. #define AVUTIL_LIBM_H
  24. #include <math.h>
  25. #include "config.h"
  26. #include "attributes.h"
  27. #include "intfloat.h"
  28. #if !HAVE_ATANF
  29. #undef atanf
  30. #define atanf(x) ((float)atan(x))
  31. #endif
  32. #if !HAVE_ATAN2F
  33. #undef atan2f
  34. #define atan2f(y, x) ((float)atan2(y, x))
  35. #endif
  36. #if !HAVE_POWF
  37. #undef powf
  38. #define powf(x, y) ((float)pow(x, y))
  39. #endif
  40. #if !HAVE_CBRTF
  41. static av_always_inline float cbrtf(float x)
  42. {
  43. return x < 0 ? -powf(-x, 1.0 / 3.0) : powf(x, 1.0 / 3.0);
  44. }
  45. #endif
  46. #if !HAVE_COSF
  47. #undef cosf
  48. #define cosf(x) ((float)cos(x))
  49. #endif
  50. #if !HAVE_EXPF
  51. #undef expf
  52. #define expf(x) ((float)exp(x))
  53. #endif
  54. #if !HAVE_EXP2
  55. #undef exp2
  56. #define exp2(x) exp((x) * 0.693147180559945)
  57. #endif /* HAVE_EXP2 */
  58. #if !HAVE_EXP2F
  59. #undef exp2f
  60. #define exp2f(x) ((float)exp2(x))
  61. #endif /* HAVE_EXP2F */
  62. #if !HAVE_ISINF
  63. static av_always_inline av_const int isinf(float x)
  64. {
  65. uint32_t v = av_float2int(x);
  66. if ((v & 0x7f800000) != 0x7f800000)
  67. return 0;
  68. return !(v & 0x007fffff);
  69. }
  70. #endif /* HAVE_ISINF */
  71. #if !HAVE_ISNAN
  72. static av_always_inline av_const int isnan(float x)
  73. {
  74. uint32_t v = av_float2int(x);
  75. if ((v & 0x7f800000) != 0x7f800000)
  76. return 0;
  77. return v & 0x007fffff;
  78. }
  79. #endif /* HAVE_ISNAN */
  80. #if !HAVE_LDEXPF
  81. #undef ldexpf
  82. #define ldexpf(x, exp) ((float)ldexp(x, exp))
  83. #endif
  84. #if !HAVE_LLRINT
  85. #undef llrint
  86. #define llrint(x) ((long long)rint(x))
  87. #endif /* HAVE_LLRINT */
  88. #if !HAVE_LLRINTF
  89. #undef llrintf
  90. #define llrintf(x) ((long long)rint(x))
  91. #endif /* HAVE_LLRINT */
  92. #if !HAVE_LOG2
  93. #undef log2
  94. #define log2(x) (log(x) * 1.44269504088896340736)
  95. #endif /* HAVE_LOG2 */
  96. #if !HAVE_LOG2F
  97. #undef log2f
  98. #define log2f(x) ((float)log2(x))
  99. #endif /* HAVE_LOG2F */
  100. #if !HAVE_LOG10F
  101. #undef log10f
  102. #define log10f(x) ((float)log10(x))
  103. #endif
  104. #if !HAVE_SINF
  105. #undef sinf
  106. #define sinf(x) ((float)sin(x))
  107. #endif
  108. #if !HAVE_RINT
  109. static inline double rint(double x)
  110. {
  111. return x >= 0 ? floor(x + 0.5) : ceil(x - 0.5);
  112. }
  113. #endif /* HAVE_RINT */
  114. #if !HAVE_LRINT
  115. static av_always_inline av_const long int lrint(double x)
  116. {
  117. return rint(x);
  118. }
  119. #endif /* HAVE_LRINT */
  120. #if !HAVE_LRINTF
  121. static av_always_inline av_const long int lrintf(float x)
  122. {
  123. return (int)(rint(x));
  124. }
  125. #endif /* HAVE_LRINTF */
  126. #if !HAVE_ROUND
  127. static av_always_inline av_const double round(double x)
  128. {
  129. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  130. }
  131. #endif /* HAVE_ROUND */
  132. #if !HAVE_ROUNDF
  133. static av_always_inline av_const float roundf(float x)
  134. {
  135. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  136. }
  137. #endif /* HAVE_ROUNDF */
  138. #if !HAVE_TRUNC
  139. static av_always_inline av_const double trunc(double x)
  140. {
  141. return (x > 0) ? floor(x) : ceil(x);
  142. }
  143. #endif /* HAVE_TRUNC */
  144. #if !HAVE_TRUNCF
  145. static av_always_inline av_const float truncf(float x)
  146. {
  147. return (x > 0) ? floor(x) : ceil(x);
  148. }
  149. #endif /* HAVE_TRUNCF */
  150. #endif /* AVUTIL_LIBM_H */