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.

243 lines
5.2KB

  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. #undef isinf
  73. /* Note: these do not follow the BSD/Apple/GNU convention of returning -1 for
  74. -Inf, +1 for Inf, 0 otherwise, but merely follow the POSIX/ISO mandated spec of
  75. returning a non-zero value for +/-Inf, 0 otherwise. */
  76. static av_always_inline av_const int avpriv_isinff(float x)
  77. {
  78. uint32_t v = av_float2int(x);
  79. if ((v & 0x7f800000) != 0x7f800000)
  80. return 0;
  81. return !(v & 0x007fffff);
  82. }
  83. static av_always_inline av_const int avpriv_isinf(double x)
  84. {
  85. uint64_t v = av_double2int(x);
  86. if ((v & 0x7ff0000000000000) != 0x7ff0000000000000)
  87. return 0;
  88. return !(v & 0x000fffffffffffff);
  89. }
  90. #define isinf(x) \
  91. (sizeof(x) == sizeof(float) \
  92. ? avpriv_isinff(x) \
  93. : avpriv_isinf(x))
  94. #endif /* HAVE_ISINF */
  95. #if !HAVE_ISNAN
  96. static av_always_inline av_const int avpriv_isnanf(float x)
  97. {
  98. uint32_t v = av_float2int(x);
  99. if ((v & 0x7f800000) != 0x7f800000)
  100. return 0;
  101. return v & 0x007fffff;
  102. }
  103. static av_always_inline av_const int avpriv_isnan(double x)
  104. {
  105. uint64_t v = av_double2int(x);
  106. if ((v & 0x7ff0000000000000) != 0x7ff0000000000000)
  107. return 0;
  108. return (v & 0x000fffffffffffff) && 1;
  109. }
  110. #define isnan(x) \
  111. (sizeof(x) == sizeof(float) \
  112. ? avpriv_isnanf(x) \
  113. : avpriv_isnan(x))
  114. #endif /* HAVE_ISNAN */
  115. #if !HAVE_HYPOT
  116. #undef hypot
  117. static inline av_const double hypot(double x, double y)
  118. {
  119. double ret, temp;
  120. x = fabs(x);
  121. y = fabs(y);
  122. if (isinf(x) || isinf(y))
  123. return av_int2double(0x7ff0000000000000);
  124. if (x == 0 || y == 0)
  125. return x + y;
  126. if (x < y) {
  127. temp = x;
  128. x = y;
  129. y = temp;
  130. }
  131. y = y/x;
  132. return x*sqrt(1 + y*y);
  133. }
  134. #endif /* HAVE_HYPOT */
  135. #if !HAVE_LDEXPF
  136. #undef ldexpf
  137. #define ldexpf(x, exp) ((float)ldexp(x, exp))
  138. #endif
  139. #if !HAVE_LLRINT
  140. #undef llrint
  141. #define llrint(x) ((long long)rint(x))
  142. #endif /* HAVE_LLRINT */
  143. #if !HAVE_LLRINTF
  144. #undef llrintf
  145. #define llrintf(x) ((long long)rint(x))
  146. #endif /* HAVE_LLRINT */
  147. #if !HAVE_LOG2
  148. #undef log2
  149. #define log2(x) (log(x) * 1.44269504088896340736)
  150. #endif /* HAVE_LOG2 */
  151. #if !HAVE_LOG2F
  152. #undef log2f
  153. #define log2f(x) ((float)log2(x))
  154. #endif /* HAVE_LOG2F */
  155. #if !HAVE_LOG10F
  156. #undef log10f
  157. #define log10f(x) ((float)log10(x))
  158. #endif
  159. #if !HAVE_SINF
  160. #undef sinf
  161. #define sinf(x) ((float)sin(x))
  162. #endif
  163. #if !HAVE_RINT
  164. static inline double rint(double x)
  165. {
  166. return x >= 0 ? floor(x + 0.5) : ceil(x - 0.5);
  167. }
  168. #endif /* HAVE_RINT */
  169. #if !HAVE_LRINT
  170. static av_always_inline av_const long int lrint(double x)
  171. {
  172. return rint(x);
  173. }
  174. #endif /* HAVE_LRINT */
  175. #if !HAVE_LRINTF
  176. static av_always_inline av_const long int lrintf(float x)
  177. {
  178. return (int)(rint(x));
  179. }
  180. #endif /* HAVE_LRINTF */
  181. #if !HAVE_ROUND
  182. static av_always_inline av_const double round(double x)
  183. {
  184. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  185. }
  186. #endif /* HAVE_ROUND */
  187. #if !HAVE_ROUNDF
  188. static av_always_inline av_const float roundf(float x)
  189. {
  190. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  191. }
  192. #endif /* HAVE_ROUNDF */
  193. #if !HAVE_TRUNC
  194. static av_always_inline av_const double trunc(double x)
  195. {
  196. return (x > 0) ? floor(x) : ceil(x);
  197. }
  198. #endif /* HAVE_TRUNC */
  199. #if !HAVE_TRUNCF
  200. static av_always_inline av_const float truncf(float x)
  201. {
  202. return (x > 0) ? floor(x) : ceil(x);
  203. }
  204. #endif /* HAVE_TRUNCF */
  205. #endif /* AVUTIL_LIBM_H */