x86cpu.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright (c) 2014, Cisco Systems, INC
  2. Written by XiangMingZhu WeiZhou MinPeng YanWang
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  12. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  13. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  14. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  15. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  16. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  20. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #if !defined(X86CPU_H)
  24. # define X86CPU_H
  25. # if defined(OPUS_X86_MAY_HAVE_SSE)
  26. # define MAY_HAVE_SSE(name) name ## _sse
  27. # else
  28. # define MAY_HAVE_SSE(name) name ## _c
  29. # endif
  30. # if defined(OPUS_X86_MAY_HAVE_SSE2)
  31. # define MAY_HAVE_SSE2(name) name ## _sse2
  32. # else
  33. # define MAY_HAVE_SSE2(name) name ## _c
  34. # endif
  35. # if defined(OPUS_X86_MAY_HAVE_SSE4_1)
  36. # define MAY_HAVE_SSE4_1(name) name ## _sse4_1
  37. # else
  38. # define MAY_HAVE_SSE4_1(name) name ## _c
  39. # endif
  40. # if defined(OPUS_X86_MAY_HAVE_AVX)
  41. # define MAY_HAVE_AVX(name) name ## _avx
  42. # else
  43. # define MAY_HAVE_AVX(name) name ## _c
  44. # endif
  45. # if defined(OPUS_HAVE_RTCD)
  46. int opus_select_arch(void);
  47. # endif
  48. /*gcc appears to emit MOVDQA's to load the argument of an _mm_cvtepi8_epi32()
  49. or _mm_cvtepi16_epi32() when optimizations are disabled, even though the
  50. actual PMOVSXWD instruction takes an m32 or m64. Unlike a normal memory
  51. reference, these require 16-byte alignment and load a full 16 bytes (instead
  52. of 4 or 8), possibly reading out of bounds.
  53. We can insert an explicit MOVD or MOVQ using _mm_cvtsi32_si128() or
  54. _mm_loadl_epi64(), which should have the same semantics as an m32 or m64
  55. reference in the PMOVSXWD instruction itself, but gcc is not smart enough to
  56. optimize this out when optimizations ARE enabled.
  57. Clang, in contrast, requires us to do this always for _mm_cvtepi8_epi32
  58. (which is fair, since technically the compiler is always allowed to do the
  59. dereference before invoking the function implementing the intrinsic).
  60. However, it is smart enough to eliminate the extra MOVD instruction.
  61. For _mm_cvtepi16_epi32, it does the right thing, though does *not* optimize out
  62. the extra MOVQ if it's specified explicitly */
  63. # if defined(__clang__) || !defined(__OPTIMIZE__)
  64. # define OP_CVTEPI8_EPI32_M32(x) \
  65. (_mm_cvtepi8_epi32(_mm_cvtsi32_si128(*(int *)(x))))
  66. # else
  67. # define OP_CVTEPI8_EPI32_M32(x) \
  68. (_mm_cvtepi8_epi32(*(__m128i *)(x)))
  69. #endif
  70. /* similar reasoning about the instruction sequence as in the 32-bit macro above,
  71. */
  72. # if defined(__clang__) || !defined(__OPTIMIZE__)
  73. # define OP_CVTEPI16_EPI32_M64(x) \
  74. (_mm_cvtepi16_epi32(_mm_loadl_epi64((__m128i *)(x))))
  75. # else
  76. # define OP_CVTEPI16_EPI32_M64(x) \
  77. (_mm_cvtepi16_epi32(*(__m128i *)(x)))
  78. # endif
  79. #endif