libm.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_CBRTF
  32. static av_always_inline float cbrtf(float x)
  33. {
  34. return x < 0 ? -powf(-x, 1.0 / 3.0) : powf(x, 1.0 / 3.0);
  35. }
  36. #endif
  37. #if !HAVE_EXP2
  38. #undef exp2
  39. #define exp2(x) exp((x) * 0.693147180559945)
  40. #endif /* HAVE_EXP2 */
  41. #if !HAVE_EXP2F
  42. #undef exp2f
  43. #define exp2f(x) ((float)exp2(x))
  44. #endif /* HAVE_EXP2F */
  45. #if !HAVE_ISINF
  46. static av_always_inline av_const int isinf(float x)
  47. {
  48. uint32_t v = av_float2int(x);
  49. if ((v & 0x7f800000) != 0x7f800000)
  50. return 0;
  51. return !(v & 0x007fffff);
  52. }
  53. #endif /* HAVE_ISINF */
  54. #if !HAVE_ISNAN
  55. static av_always_inline av_const int isnan(float x)
  56. {
  57. uint32_t v = av_float2int(x);
  58. if ((v & 0x7f800000) != 0x7f800000)
  59. return 0;
  60. return v & 0x007fffff;
  61. }
  62. #endif /* HAVE_ISNAN */
  63. #if !HAVE_LLRINT
  64. #undef llrint
  65. #define llrint(x) ((long long)rint(x))
  66. #endif /* HAVE_LLRINT */
  67. #if !HAVE_LLRINTF
  68. #undef llrintf
  69. #define llrintf(x) ((long long)rint(x))
  70. #endif /* HAVE_LLRINT */
  71. #if !HAVE_LOG2
  72. #undef log2
  73. #define log2(x) (log(x) * 1.44269504088896340736)
  74. #endif /* HAVE_LOG2 */
  75. #if !HAVE_LOG2F
  76. #undef log2f
  77. #define log2f(x) ((float)log2(x))
  78. #endif /* HAVE_LOG2F */
  79. #if !HAVE_RINT
  80. static inline double rint(double x)
  81. {
  82. return x >= 0 ? floor(x + 0.5) : ceil(x - 0.5);
  83. }
  84. #endif /* HAVE_RINT */
  85. #if !HAVE_LRINT
  86. static av_always_inline av_const long int lrint(double x)
  87. {
  88. return rint(x);
  89. }
  90. #endif /* HAVE_LRINT */
  91. #if !HAVE_LRINTF
  92. static av_always_inline av_const long int lrintf(float x)
  93. {
  94. return (int)(rint(x));
  95. }
  96. #endif /* HAVE_LRINTF */
  97. #if !HAVE_ROUND
  98. static av_always_inline av_const double round(double x)
  99. {
  100. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  101. }
  102. #endif /* HAVE_ROUND */
  103. #if !HAVE_ROUNDF
  104. static av_always_inline av_const float roundf(float x)
  105. {
  106. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  107. }
  108. #endif /* HAVE_ROUNDF */
  109. #if !HAVE_TRUNC
  110. static av_always_inline av_const double trunc(double x)
  111. {
  112. return (x > 0) ? floor(x) : ceil(x);
  113. }
  114. #endif /* HAVE_TRUNC */
  115. #if !HAVE_TRUNCF
  116. static av_always_inline av_const float truncf(float x)
  117. {
  118. return (x > 0) ? floor(x) : ceil(x);
  119. }
  120. #endif /* HAVE_TRUNCF */
  121. #endif /* AVUTIL_LIBM_H */