intmath.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. /**
  29. * @addtogroup lavu_internal
  30. * @{
  31. */
  32. #if HAVE_FAST_CLZ
  33. #if AV_GCC_VERSION_AT_LEAST(3,4)
  34. #ifndef ff_log2
  35. # define ff_log2(x) (31 - __builtin_clz((x)|1))
  36. # ifndef ff_log2_16bit
  37. # define ff_log2_16bit av_log2
  38. # endif
  39. #endif /* ff_log2 */
  40. #elif defined( __INTEL_COMPILER )
  41. #ifndef ff_log2
  42. # define ff_log2(x) (_bit_scan_reverse(x|1))
  43. # ifndef ff_log2_16bit
  44. # define ff_log2_16bit av_log2
  45. # endif
  46. #endif /* ff_log2 */
  47. #endif
  48. #endif /* AV_GCC_VERSION_AT_LEAST(3,4) */
  49. extern const uint8_t ff_log2_tab[256];
  50. #ifndef ff_log2
  51. #define ff_log2 ff_log2_c
  52. #if !defined( _MSC_VER )
  53. static av_always_inline av_const int ff_log2_c(unsigned int v)
  54. {
  55. int n = 0;
  56. if (v & 0xffff0000) {
  57. v >>= 16;
  58. n += 16;
  59. }
  60. if (v & 0xff00) {
  61. v >>= 8;
  62. n += 8;
  63. }
  64. n += ff_log2_tab[v];
  65. return n;
  66. }
  67. #else
  68. static av_always_inline av_const int ff_log2_c(unsigned int v)
  69. {
  70. unsigned long n;
  71. _BitScanReverse(&n, v|1);
  72. return n;
  73. }
  74. #define ff_log2_16bit av_log2
  75. #endif
  76. #endif
  77. #ifndef ff_log2_16bit
  78. #define ff_log2_16bit ff_log2_16bit_c
  79. static av_always_inline av_const int ff_log2_16bit_c(unsigned int v)
  80. {
  81. int n = 0;
  82. if (v & 0xff00) {
  83. v >>= 8;
  84. n += 8;
  85. }
  86. n += ff_log2_tab[v];
  87. return n;
  88. }
  89. #endif
  90. #define av_log2 ff_log2
  91. #define av_log2_16bit ff_log2_16bit
  92. /**
  93. * @}
  94. */
  95. /**
  96. * @addtogroup lavu_math
  97. * @{
  98. */
  99. #if HAVE_FAST_CLZ
  100. #if AV_GCC_VERSION_AT_LEAST(3,4)
  101. #ifndef ff_ctz
  102. #define ff_ctz(v) __builtin_ctz(v)
  103. #endif
  104. #elif defined( __INTEL_COMPILER )
  105. #ifndef ff_ctz
  106. #define ff_ctz(v) _bit_scan_forward(v)
  107. #endif
  108. #endif
  109. #endif
  110. #ifndef ff_ctz
  111. #define ff_ctz ff_ctz_c
  112. #if !defined( _MSC_VER )
  113. static av_always_inline av_const int ff_ctz_c(int v)
  114. {
  115. int c;
  116. if (v & 0x1)
  117. return 0;
  118. c = 1;
  119. if (!(v & 0xffff)) {
  120. v >>= 16;
  121. c += 16;
  122. }
  123. if (!(v & 0xff)) {
  124. v >>= 8;
  125. c += 8;
  126. }
  127. if (!(v & 0xf)) {
  128. v >>= 4;
  129. c += 4;
  130. }
  131. if (!(v & 0x3)) {
  132. v >>= 2;
  133. c += 2;
  134. }
  135. c -= v & 0x1;
  136. return c;
  137. }
  138. #else
  139. static av_always_inline av_const int ff_ctz_c( int v )
  140. {
  141. unsigned long c;
  142. _BitScanForward(&c, v);
  143. return c;
  144. }
  145. #endif
  146. #endif
  147. /**
  148. * Trailing zero bit count.
  149. *
  150. * @param v input value. If v is 0, the result is undefined.
  151. * @return the number of trailing 0-bits
  152. */
  153. int av_ctz(int v);
  154. /**
  155. * @}
  156. */
  157. #endif /* AVUTIL_INTMATH_H */