crc32c_sse4_intrin.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2010 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Provides _mm_crc32_u64/32/8 intrinsics.
  15. #ifndef CRCUTIL_CRC32C_SSE4_INTRIN_H_
  16. #define CRCUTIL_CRC32C_SSE4_INTRIN_H_
  17. #include "platform.h"
  18. #include "base_types.h"
  19. #if CRCUTIL_USE_MM_CRC32 && (HAVE_I386 || HAVE_AMD64)
  20. #if defined(_MSC_VER) || defined(__SSE4_2__)
  21. #if defined(_MSC_VER)
  22. #pragma warning(push)
  23. // '_M_IA64' is not defined as a preprocessor macro
  24. #pragma warning(disable: 4668)
  25. #endif // defined(_MSC_VER)
  26. #include <nmmintrin.h>
  27. #if defined(_MSC_VER)
  28. #pragma warning(pop)
  29. #endif // defined(_MSC_VER)
  30. #elif GCC_VERSION_AVAILABLE(4, 5) && !defined(CRCUTIL_FORCE_ASM_CRC32C)
  31. // Allow the use of _mm_crc32_u* intrinsic when CRCUTIL_USE_MM_CRC32
  32. // is set irrespective of "-msse*" settings. This way, the sources
  33. // may be compiled with "-msse2 -mcrc32" and work on older CPUs,
  34. // while taking full advantage of "crc32" instruction on newer
  35. // CPUs (requires dynamic CPU detection). See "interface.cc".
  36. //
  37. // If neither -msse4 or -mcrc32 is provided and CRCUTIL_USE_MM_CRC32 is set
  38. // and CRCUTIL_FORCE_ASM_CRC32 is not set, compile-time error will happen.
  39. // Why? Becuase GCC disables __builtin_ia32_crc32* intrinsics when compiled
  40. // without -msse4 or -mcrc32. -msse4 could be detected at run time by checking
  41. // whether __SSE4_2__ is defined, but there is no way to tell whether the
  42. // sources are compiled with -mcrc32.
  43. extern __inline unsigned int __attribute__((
  44. __gnu_inline__, __always_inline__, __artificial__))
  45. _mm_crc32_u8(unsigned int __C, unsigned char __V) {
  46. return __builtin_ia32_crc32qi(__C, __V);
  47. }
  48. #ifdef __x86_64__
  49. extern __inline unsigned long long __attribute__((
  50. __gnu_inline__, __always_inline__, __artificial__))
  51. _mm_crc32_u64(unsigned long long __C, unsigned long long __V) {
  52. return __builtin_ia32_crc32di(__C, __V);
  53. }
  54. #else
  55. extern __inline unsigned int __attribute__((
  56. __gnu_inline__, __always_inline__, __artificial__))
  57. _mm_crc32_u32(unsigned int __C, unsigned int __V) {
  58. return __builtin_ia32_crc32si (__C, __V);
  59. }
  60. #endif // __x86_64__
  61. #else
  62. // GCC 4.4.x and earlier: use inline asm.
  63. namespace crcutil {
  64. __forceinline uint64 _mm_crc32_u64(uint64 crc, uint64 value) {
  65. asm("crc32q %[value], %[crc]\n" : [crc] "+r" (crc) : [value] "rm" (value));
  66. return crc;
  67. }
  68. __forceinline uint32 _mm_crc32_u32(uint32 crc, uint64 value) {
  69. asm("crc32l %[value], %[crc]\n" : [crc] "+r" (crc) : [value] "rm" (value));
  70. return crc;
  71. }
  72. __forceinline uint32 _mm_crc32_u8(uint32 crc, uint8 value) {
  73. asm("crc32b %[value], %[crc]\n" : [crc] "+r" (crc) : [value] "rm" (value));
  74. return crc;
  75. }
  76. } // namespace crcutil
  77. #endif
  78. #endif // CRCUTIL_USE_MM_CRC32 && (HAVE_I386 || HAVE_AMD64)
  79. #endif // CRCUTIL_CRC32C_SSE4_INTRIN_H_