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.

182 lines
3.4KB

  1. /*
  2. * Copyright (c) 2010 Mans Rullgard <mans@mansr.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef AVUTIL_INTMATH_H
  21. #define AVUTIL_INTMATH_H
  22. #include <stdint.h>
  23. #include "config.h"
  24. #include "attributes.h"
  25. #if ARCH_ARM
  26. # include "arm/intmath.h"
  27. #endif
  28. #if ARCH_X86
  29. # include "x86/intmath.h"
  30. #endif
  31. /**
  32. * @addtogroup lavu_internal
  33. * @{
  34. */
  35. #if HAVE_FAST_CLZ
  36. #if AV_GCC_VERSION_AT_LEAST(3,4)
  37. #ifndef ff_log2
  38. # define ff_log2(x) (31 - __builtin_clz((x)|1))
  39. # ifndef ff_log2_16bit
  40. # define ff_log2_16bit av_log2
  41. # endif
  42. #endif /* ff_log2 */
  43. #elif defined( __INTEL_COMPILER )
  44. #ifndef ff_log2
  45. # define ff_log2(x) (_bit_scan_reverse((x)|1))
  46. # ifndef ff_log2_16bit
  47. # define ff_log2_16bit av_log2
  48. # endif
  49. #endif /* ff_log2 */
  50. #endif
  51. #endif /* AV_GCC_VERSION_AT_LEAST(3,4) */
  52. extern const uint8_t ff_log2_tab[256];
  53. #ifndef ff_log2
  54. #define ff_log2 ff_log2_c
  55. #if !defined( _MSC_VER )
  56. static av_always_inline av_const int ff_log2_c(unsigned int v)
  57. {
  58. int n = 0;
  59. if (v & 0xffff0000) {
  60. v >>= 16;
  61. n += 16;
  62. }
  63. if (v & 0xff00) {
  64. v >>= 8;
  65. n += 8;
  66. }
  67. n += ff_log2_tab[v];
  68. return n;
  69. }
  70. #else
  71. static av_always_inline av_const int ff_log2_c(unsigned int v)
  72. {
  73. unsigned long n;
  74. _BitScanReverse(&n, v|1);
  75. return n;
  76. }
  77. #define ff_log2_16bit av_log2
  78. #endif
  79. #endif
  80. #ifndef ff_log2_16bit
  81. #define ff_log2_16bit ff_log2_16bit_c
  82. static av_always_inline av_const int ff_log2_16bit_c(unsigned int v)
  83. {
  84. int n = 0;
  85. if (v & 0xff00) {
  86. v >>= 8;
  87. n += 8;
  88. }
  89. n += ff_log2_tab[v];
  90. return n;
  91. }
  92. #endif
  93. #define av_log2 ff_log2
  94. #define av_log2_16bit ff_log2_16bit
  95. /**
  96. * @}
  97. */
  98. /**
  99. * @addtogroup lavu_math
  100. * @{
  101. */
  102. #if HAVE_FAST_CLZ
  103. #if AV_GCC_VERSION_AT_LEAST(3,4)
  104. #ifndef ff_ctz
  105. #define ff_ctz(v) __builtin_ctz(v)
  106. #endif
  107. #elif defined( __INTEL_COMPILER )
  108. #ifndef ff_ctz
  109. #define ff_ctz(v) _bit_scan_forward(v)
  110. #endif
  111. #endif
  112. #endif
  113. #ifndef ff_ctz
  114. #define ff_ctz ff_ctz_c
  115. #if !defined( _MSC_VER )
  116. static av_always_inline av_const int ff_ctz_c(int v)
  117. {
  118. int c;
  119. if (v & 0x1)
  120. return 0;
  121. c = 1;
  122. if (!(v & 0xffff)) {
  123. v >>= 16;
  124. c += 16;
  125. }
  126. if (!(v & 0xff)) {
  127. v >>= 8;
  128. c += 8;
  129. }
  130. if (!(v & 0xf)) {
  131. v >>= 4;
  132. c += 4;
  133. }
  134. if (!(v & 0x3)) {
  135. v >>= 2;
  136. c += 2;
  137. }
  138. c -= v & 0x1;
  139. return c;
  140. }
  141. #else
  142. static av_always_inline av_const int ff_ctz_c( int v )
  143. {
  144. unsigned long c;
  145. _BitScanForward(&c, v);
  146. return c;
  147. }
  148. #endif
  149. #endif
  150. /**
  151. * Trailing zero bit count.
  152. *
  153. * @param v input value. If v is 0, the result is undefined.
  154. * @return the number of trailing 0-bits
  155. */
  156. int av_ctz(int v);
  157. /**
  158. * @}
  159. */
  160. #endif /* AVUTIL_INTMATH_H */