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.

104 lines
2.7KB

  1. /*
  2. * Copyright (c) 2015 James Almer
  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_X86_INTMATH_H
  21. #define AVUTIL_X86_INTMATH_H
  22. #include <stdint.h>
  23. #if HAVE_FAST_CLZ
  24. #if defined(_MSC_VER)
  25. #include <intrin.h>
  26. #elif defined(__INTEL_COMPILER)
  27. #include <x86intrin.h>
  28. #endif
  29. #endif
  30. #include "config.h"
  31. #if HAVE_FAST_CLZ
  32. #if defined(__INTEL_COMPILER) || defined(_MSC_VER)
  33. # if defined(__INTEL_COMPILER)
  34. # define ff_log2(x) (_bit_scan_reverse((x)|1))
  35. # else
  36. # define ff_log2 ff_log2_x86
  37. static av_always_inline av_const int ff_log2_x86(unsigned int v)
  38. {
  39. unsigned long n;
  40. _BitScanReverse(&n, v|1);
  41. return n;
  42. }
  43. # endif
  44. # define ff_log2_16bit av_log2
  45. # define ff_ctz(v) _tzcnt_u32(v)
  46. # if ARCH_X86_64
  47. # define ff_ctzll(v) _tzcnt_u64(v)
  48. # else
  49. # define ff_ctzll ff_ctzll_x86
  50. static av_always_inline av_const int ff_ctzll_x86(long long v)
  51. {
  52. return ((uint32_t)v == 0) ? _tzcnt_u32((uint32_t)(v >> 32)) + 32 : _tzcnt_u32((uint32_t)v);
  53. }
  54. # endif
  55. #endif /* __INTEL_COMPILER */
  56. #endif /* HAVE_FAST_CLZ */
  57. #if defined(__GNUC__)
  58. /* Our generic version of av_popcount is faster than GCC's built-in on
  59. * CPUs that don't support the popcnt instruction.
  60. */
  61. #if defined(__POPCNT__)
  62. #define av_popcount __builtin_popcount
  63. #if ARCH_X86_64
  64. #define av_popcount64 __builtin_popcountll
  65. #endif
  66. #endif /* __POPCNT__ */
  67. #if defined(__BMI2__)
  68. #if AV_GCC_VERSION_AT_LEAST(5,1)
  69. #define av_mod_uintp2 __builtin_ia32_bzhi_si
  70. #elif HAVE_INLINE_ASM
  71. /* GCC releases before 5.1.0 have a broken bzhi builtin, so for those we
  72. * implement it using inline assembly
  73. */
  74. #define av_mod_uintp2 av_mod_uintp2_bmi2
  75. static av_always_inline av_const unsigned av_mod_uintp2_bmi2(unsigned a, unsigned p)
  76. {
  77. if (av_builtin_constant_p(p))
  78. return a & ((1 << p) - 1);
  79. else {
  80. unsigned x;
  81. __asm__ ("bzhi %2, %1, %0 \n\t" : "=r"(x) : "rm"(a), "r"(p));
  82. return x;
  83. }
  84. }
  85. #endif /* AV_GCC_VERSION_AT_LEAST */
  86. #endif /* __BMI2__ */
  87. #endif /* __GNUC__ */
  88. #endif /* AVUTIL_X86_INTMATH_H */