intmath.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #include <stdlib.h>
  24. #if HAVE_FAST_CLZ
  25. #if defined(_MSC_VER)
  26. #include <intrin.h>
  27. #elif defined(__INTEL_COMPILER)
  28. #include <immintrin.h>
  29. #endif
  30. #endif
  31. #include "config.h"
  32. #if HAVE_FAST_CLZ
  33. #if (defined(__INTEL_COMPILER) && (__INTEL_COMPILER>=1216)) || defined(_MSC_VER)
  34. # if defined(__INTEL_COMPILER)
  35. # define ff_log2(x) (_bit_scan_reverse((x)|1))
  36. # else
  37. # define ff_log2 ff_log2_x86
  38. static av_always_inline av_const int ff_log2_x86(unsigned int v)
  39. {
  40. unsigned long n;
  41. _BitScanReverse(&n, v|1);
  42. return n;
  43. }
  44. # endif
  45. # define ff_log2_16bit av_log2
  46. #if defined(__INTEL_COMPILER) || (defined(_MSC_VER) && (_MSC_VER >= 1700) && \
  47. (defined(__BMI__) || !defined(__clang__)))
  48. # define ff_ctz(v) _tzcnt_u32(v)
  49. # if ARCH_X86_64
  50. # define ff_ctzll(v) _tzcnt_u64(v)
  51. # else
  52. # define ff_ctzll ff_ctzll_x86
  53. static av_always_inline av_const int ff_ctzll_x86(long long v)
  54. {
  55. return ((uint32_t)v == 0) ? _tzcnt_u32((uint32_t)(v >> 32)) + 32 : _tzcnt_u32((uint32_t)v);
  56. }
  57. # endif
  58. #endif /* _MSC_VER */
  59. #endif /* __INTEL_COMPILER */
  60. #endif /* HAVE_FAST_CLZ */
  61. #if defined(__GNUC__)
  62. /* Our generic version of av_popcount is faster than GCC's built-in on
  63. * CPUs that don't support the popcnt instruction.
  64. */
  65. #if defined(__POPCNT__)
  66. #define av_popcount __builtin_popcount
  67. #if ARCH_X86_64
  68. #define av_popcount64 __builtin_popcountll
  69. #endif
  70. #endif /* __POPCNT__ */
  71. #if defined(__BMI2__)
  72. #if AV_GCC_VERSION_AT_LEAST(5,1)
  73. #define av_mod_uintp2 __builtin_ia32_bzhi_si
  74. #elif HAVE_INLINE_ASM
  75. /* GCC releases before 5.1.0 have a broken bzhi builtin, so for those we
  76. * implement it using inline assembly
  77. */
  78. #define av_mod_uintp2 av_mod_uintp2_bmi2
  79. static av_always_inline av_const unsigned av_mod_uintp2_bmi2(unsigned a, unsigned p)
  80. {
  81. if (av_builtin_constant_p(p))
  82. return a & ((1 << p) - 1);
  83. else {
  84. unsigned x;
  85. __asm__ ("bzhi %2, %1, %0 \n\t" : "=r"(x) : "rm"(a), "r"(p));
  86. return x;
  87. }
  88. }
  89. #endif /* AV_GCC_VERSION_AT_LEAST */
  90. #endif /* __BMI2__ */
  91. #if defined(__SSE2__) && !defined(__INTEL_COMPILER)
  92. #define av_clipd av_clipd_sse2
  93. static av_always_inline av_const double av_clipd_sse2(double a, double amin, double amax)
  94. {
  95. #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
  96. if (amin > amax) abort();
  97. #endif
  98. __asm__ ("minsd %2, %0 \n\t"
  99. "maxsd %1, %0 \n\t"
  100. : "+&x"(a) : "xm"(amin), "xm"(amax));
  101. return a;
  102. }
  103. #endif /* __SSE2__ */
  104. #if defined(__SSE__) && !defined(__INTEL_COMPILER)
  105. #define av_clipf av_clipf_sse
  106. static av_always_inline av_const float av_clipf_sse(float a, float amin, float amax)
  107. {
  108. #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
  109. if (amin > amax) abort();
  110. #endif
  111. __asm__ ("minss %2, %0 \n\t"
  112. "maxss %1, %0 \n\t"
  113. : "+&x"(a) : "xm"(amin), "xm"(amax));
  114. return a;
  115. }
  116. #endif /* __SSE__ */
  117. #endif /* __GNUC__ */
  118. #endif /* AVUTIL_X86_INTMATH_H */