intmath.h 2.7 KB

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