libm.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_CBRTF
  44. static av_always_inline float cbrtf(float x)
  45. {
  46. return x < 0 ? -powf(-x, 1.0 / 3.0) : powf(x, 1.0 / 3.0);
  47. }
  48. #endif
  49. #if !HAVE_COSF
  50. #undef cosf
  51. #define cosf(x) ((float)cos(x))
  52. #endif
  53. #if !HAVE_EXPF
  54. #undef expf
  55. #define expf(x) ((float)exp(x))
  56. #endif
  57. #if !HAVE_EXP2
  58. #undef exp2
  59. #define exp2(x) exp((x) * 0.693147180559945)
  60. #endif /* HAVE_EXP2 */
  61. #if !HAVE_EXP2F
  62. #undef exp2f
  63. #define exp2f(x) ((float)exp2(x))
  64. #endif /* HAVE_EXP2F */
  65. #if !HAVE_ISINF
  66. static av_always_inline av_const int isinf(float x)
  67. {
  68. uint32_t v = av_float2int(x);
  69. if ((v & 0x7f800000) != 0x7f800000)
  70. return 0;
  71. return !(v & 0x007fffff);
  72. }
  73. #endif /* HAVE_ISINF */
  74. #if !HAVE_ISNAN
  75. static av_always_inline av_const int isnan(float x)
  76. {
  77. uint32_t v = av_float2int(x);
  78. if ((v & 0x7f800000) != 0x7f800000)
  79. return 0;
  80. return v & 0x007fffff;
  81. }
  82. #endif /* HAVE_ISNAN */
  83. #if !HAVE_LDEXPF
  84. #undef ldexpf
  85. #define ldexpf(x, exp) ((float)ldexp(x, exp))
  86. #endif
  87. #if !HAVE_LLRINT
  88. #undef llrint
  89. #define llrint(x) ((long long)rint(x))
  90. #endif /* HAVE_LLRINT */
  91. #if !HAVE_LLRINTF
  92. #undef llrintf
  93. #define llrintf(x) ((long long)rint(x))
  94. #endif /* HAVE_LLRINT */
  95. #if !HAVE_LOG2
  96. #undef log2
  97. #define log2(x) (log(x) * 1.44269504088896340736)
  98. #endif /* HAVE_LOG2 */
  99. #if !HAVE_LOG2F
  100. #undef log2f
  101. #define log2f(x) ((float)log2(x))
  102. #endif /* HAVE_LOG2F */
  103. #if !HAVE_LOG10F
  104. #undef log10f
  105. #define log10f(x) ((float)log10(x))
  106. #endif
  107. #if !HAVE_SINF
  108. #undef sinf
  109. #define sinf(x) ((float)sin(x))
  110. #endif
  111. #if !HAVE_RINT
  112. static inline double rint(double x)
  113. {
  114. return x >= 0 ? floor(x + 0.5) : ceil(x - 0.5);
  115. }
  116. #endif /* HAVE_RINT */
  117. #if !HAVE_LRINT
  118. static av_always_inline av_const long int lrint(double x)
  119. {
  120. return rint(x);
  121. }
  122. #endif /* HAVE_LRINT */
  123. #if !HAVE_LRINTF
  124. static av_always_inline av_const long int lrintf(float x)
  125. {
  126. return (int)(rint(x));
  127. }
  128. #endif /* HAVE_LRINTF */
  129. #if !HAVE_ROUND
  130. static av_always_inline av_const double round(double x)
  131. {
  132. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  133. }
  134. #endif /* HAVE_ROUND */
  135. #if !HAVE_ROUNDF
  136. static av_always_inline av_const float roundf(float x)
  137. {
  138. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  139. }
  140. #endif /* HAVE_ROUNDF */
  141. #if !HAVE_TRUNC
  142. static av_always_inline av_const double trunc(double x)
  143. {
  144. return (x > 0) ? floor(x) : ceil(x);
  145. }
  146. #endif /* HAVE_TRUNC */
  147. #if !HAVE_TRUNCF
  148. static av_always_inline av_const float truncf(float x)
  149. {
  150. return (x > 0) ? floor(x) : ceil(x);
  151. }
  152. #endif /* HAVE_TRUNCF */
  153. #endif /* AVUTIL_LIBM_H */