intmath.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 ARCH_ARM
  33. # include "arm/intmath.h"
  34. #endif
  35. #if HAVE_FAST_CLZ && AV_GCC_VERSION_AT_LEAST(3,4)
  36. #ifndef ff_log2
  37. # define ff_log2(x) (31 - __builtin_clz((x)|1))
  38. # ifndef ff_log2_16bit
  39. # define ff_log2_16bit av_log2
  40. # endif
  41. #endif /* ff_log2 */
  42. #endif /* AV_GCC_VERSION_AT_LEAST(3,4) */
  43. extern const uint8_t ff_log2_tab[256];
  44. #ifndef ff_log2
  45. #define ff_log2 ff_log2_c
  46. static av_always_inline av_const int ff_log2_c(unsigned int v)
  47. {
  48. int n = 0;
  49. if (v & 0xffff0000) {
  50. v >>= 16;
  51. n += 16;
  52. }
  53. if (v & 0xff00) {
  54. v >>= 8;
  55. n += 8;
  56. }
  57. n += ff_log2_tab[v];
  58. return n;
  59. }
  60. #endif
  61. #ifndef ff_log2_16bit
  62. #define ff_log2_16bit ff_log2_16bit_c
  63. static av_always_inline av_const int ff_log2_16bit_c(unsigned int v)
  64. {
  65. int n = 0;
  66. if (v & 0xff00) {
  67. v >>= 8;
  68. n += 8;
  69. }
  70. n += ff_log2_tab[v];
  71. return n;
  72. }
  73. #endif
  74. #define av_log2 ff_log2
  75. #define av_log2_16bit ff_log2_16bit
  76. /**
  77. * @}
  78. */
  79. /**
  80. * @addtogroup lavu_math
  81. * @{
  82. */
  83. #if HAVE_FAST_CLZ && AV_GCC_VERSION_AT_LEAST(3,4)
  84. #ifndef ff_ctz
  85. #define ff_ctz(v) __builtin_ctz(v)
  86. #endif
  87. #endif
  88. #ifndef ff_ctz
  89. #define ff_ctz ff_ctz_c
  90. static av_always_inline av_const int ff_ctz_c(int v)
  91. {
  92. int c;
  93. if (v & 0x1)
  94. return 0;
  95. c = 1;
  96. if (!(v & 0xffff)) {
  97. v >>= 16;
  98. c += 16;
  99. }
  100. if (!(v & 0xff)) {
  101. v >>= 8;
  102. c += 8;
  103. }
  104. if (!(v & 0xf)) {
  105. v >>= 4;
  106. c += 4;
  107. }
  108. if (!(v & 0x3)) {
  109. v >>= 2;
  110. c += 2;
  111. }
  112. c -= v & 0x1;
  113. return c;
  114. }
  115. #endif
  116. /**
  117. * Trailing zero bit count.
  118. *
  119. * @param v input value. If v is 0, the result is undefined.
  120. * @return the number of trailing 0-bits
  121. */
  122. int av_ctz(int v);
  123. /**
  124. * @}
  125. */
  126. #endif /* AVUTIL_INTMATH_H */