libm.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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_MIPSFPU && HAVE_INLINE_ASM
  29. #include "libavutil/mips/libm_mips.h"
  30. #endif /* HAVE_MIPSFPU && HAVE_INLINE_ASM*/
  31. #if !HAVE_ATANF
  32. #undef atanf
  33. #define atanf(x) ((float)atan(x))
  34. #endif
  35. #if !HAVE_ATAN2F
  36. #undef atan2f
  37. #define atan2f(y, x) ((float)atan2(y, x))
  38. #endif
  39. #if !HAVE_POWF
  40. #undef powf
  41. #define powf(x, y) ((float)pow(x, y))
  42. #endif
  43. #if !HAVE_CBRT
  44. static av_always_inline double cbrt(double x)
  45. {
  46. return x < 0 ? -pow(-x, 1.0 / 3.0) : pow(x, 1.0 / 3.0);
  47. }
  48. #endif
  49. #if !HAVE_CBRTF
  50. static av_always_inline float cbrtf(float x)
  51. {
  52. return x < 0 ? -powf(-x, 1.0 / 3.0) : powf(x, 1.0 / 3.0);
  53. }
  54. #endif
  55. #if !HAVE_COSF
  56. #undef cosf
  57. #define cosf(x) ((float)cos(x))
  58. #endif
  59. #if !HAVE_EXPF
  60. #undef expf
  61. #define expf(x) ((float)exp(x))
  62. #endif
  63. #if !HAVE_EXP2
  64. #undef exp2
  65. #define exp2(x) exp((x) * 0.693147180559945)
  66. #endif /* HAVE_EXP2 */
  67. #if !HAVE_EXP2F
  68. #undef exp2f
  69. #define exp2f(x) ((float)exp2(x))
  70. #endif /* HAVE_EXP2F */
  71. #if !HAVE_ISINF
  72. static av_always_inline av_const int isinf(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_ISINF */
  80. #if !HAVE_ISNAN
  81. static av_always_inline av_const int isnan(float x)
  82. {
  83. uint32_t v = av_float2int(x);
  84. if ((v & 0x7f800000) != 0x7f800000)
  85. return 0;
  86. return v & 0x007fffff;
  87. }
  88. #endif /* HAVE_ISNAN */
  89. #if !HAVE_LDEXPF
  90. #undef ldexpf
  91. #define ldexpf(x, exp) ((float)ldexp(x, exp))
  92. #endif
  93. #if !HAVE_LLRINT
  94. #undef llrint
  95. #define llrint(x) ((long long)rint(x))
  96. #endif /* HAVE_LLRINT */
  97. #if !HAVE_LLRINTF
  98. #undef llrintf
  99. #define llrintf(x) ((long long)rint(x))
  100. #endif /* HAVE_LLRINT */
  101. #if !HAVE_LOG2
  102. #undef log2
  103. #define log2(x) (log(x) * 1.44269504088896340736)
  104. #endif /* HAVE_LOG2 */
  105. #if !HAVE_LOG2F
  106. #undef log2f
  107. #define log2f(x) ((float)log2(x))
  108. #endif /* HAVE_LOG2F */
  109. #if !HAVE_LOG10F
  110. #undef log10f
  111. #define log10f(x) ((float)log10(x))
  112. #endif
  113. #if !HAVE_SINF
  114. #undef sinf
  115. #define sinf(x) ((float)sin(x))
  116. #endif
  117. #if !HAVE_RINT
  118. static inline double rint(double x)
  119. {
  120. return x >= 0 ? floor(x + 0.5) : ceil(x - 0.5);
  121. }
  122. #endif /* HAVE_RINT */
  123. #if !HAVE_LRINT
  124. static av_always_inline av_const long int lrint(double x)
  125. {
  126. return rint(x);
  127. }
  128. #endif /* HAVE_LRINT */
  129. #if !HAVE_LRINTF
  130. static av_always_inline av_const long int lrintf(float x)
  131. {
  132. return (int)(rint(x));
  133. }
  134. #endif /* HAVE_LRINTF */
  135. #if !HAVE_ROUND
  136. static av_always_inline av_const double round(double x)
  137. {
  138. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  139. }
  140. #endif /* HAVE_ROUND */
  141. #if !HAVE_ROUNDF
  142. static av_always_inline av_const float roundf(float x)
  143. {
  144. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  145. }
  146. #endif /* HAVE_ROUNDF */
  147. #if !HAVE_TRUNC
  148. static av_always_inline av_const double trunc(double x)
  149. {
  150. return (x > 0) ? floor(x) : ceil(x);
  151. }
  152. #endif /* HAVE_TRUNC */
  153. #if !HAVE_TRUNCF
  154. static av_always_inline av_const float truncf(float x)
  155. {
  156. return (x > 0) ? floor(x) : ceil(x);
  157. }
  158. #endif /* HAVE_TRUNCF */
  159. #endif /* AVUTIL_LIBM_H */