ecintrin.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Copyright (c) 2003-2008 Timothy B. Terriberry
  2. Copyright (c) 2008 Xiph.Org Foundation */
  3. /*
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  16. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. /*Some common macros for potential platform-specific optimization.*/
  25. #include "opus_types.h"
  26. #include <math.h>
  27. #include <limits.h>
  28. #include "arch.h"
  29. #if !defined(_ecintrin_H)
  30. # define _ecintrin_H (1)
  31. /*Some specific platforms may have optimized intrinsic or OPUS_INLINE assembly
  32. versions of these functions which can substantially improve performance.
  33. We define macros for them to allow easy incorporation of these non-ANSI
  34. features.*/
  35. /*Modern gcc (4.x) can compile the naive versions of min and max with cmov if
  36. given an appropriate architecture, but the branchless bit-twiddling versions
  37. are just as fast, and do not require any special target architecture.
  38. Earlier gcc versions (3.x) compiled both code to the same assembly
  39. instructions, because of the way they represented ((_b)>(_a)) internally.*/
  40. # define EC_MINI(_a,_b) ((_a)+(((_b)-(_a))&-((_b)<(_a))))
  41. /*Count leading zeros.
  42. This macro should only be used for implementing ec_ilog(), if it is defined.
  43. All other code should use EC_ILOG() instead.*/
  44. #if defined(_MSC_VER) && (_MSC_VER >= 1400)
  45. #if defined(_MSC_VER) && (_MSC_VER >= 1910)
  46. # include <intrin0.h> /* Improve compiler throughput. */
  47. #else
  48. # include <intrin.h>
  49. #endif
  50. /*In _DEBUG mode this is not an intrinsic by default.*/
  51. # pragma intrinsic(_BitScanReverse)
  52. static __inline int ec_bsr(unsigned long _x){
  53. unsigned long ret;
  54. _BitScanReverse(&ret,_x);
  55. return (int)ret;
  56. }
  57. # define EC_CLZ0 (1)
  58. # define EC_CLZ(_x) (-ec_bsr(_x))
  59. #elif defined(ENABLE_TI_DSPLIB)
  60. # include "dsplib.h"
  61. # define EC_CLZ0 (31)
  62. # define EC_CLZ(_x) (_lnorm(_x))
  63. #elif __GNUC_PREREQ(3,4)
  64. # if INT_MAX>=2147483647
  65. # define EC_CLZ0 ((int)sizeof(unsigned)*CHAR_BIT)
  66. # define EC_CLZ(_x) (__builtin_clz(_x))
  67. # elif LONG_MAX>=2147483647L
  68. # define EC_CLZ0 ((int)sizeof(unsigned long)*CHAR_BIT)
  69. # define EC_CLZ(_x) (__builtin_clzl(_x))
  70. # endif
  71. #endif
  72. #if defined(EC_CLZ)
  73. /*Note that __builtin_clz is not defined when _x==0, according to the gcc
  74. documentation (and that of the BSR instruction that implements it on x86).
  75. The majority of the time we can never pass it zero.
  76. When we need to, it can be special cased.*/
  77. # define EC_ILOG(_x) (EC_CLZ0-EC_CLZ(_x))
  78. #else
  79. int ec_ilog(opus_uint32 _v);
  80. # define EC_ILOG(_x) (ec_ilog(_x))
  81. #endif
  82. #endif