CompilerSpecific.inl 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2017 Intel Corporation
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. // use this file except in compliance with the License. You may obtain a copy
  6. // of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. // License for the specific language governing permissions and limitations
  14. // under the License.
  15. ////////////////////////////////////////////////////////////////////////////////
  16. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. // Common shared include file to hide compiler/os specific functions from the rest of the code.
  18. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  19. #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) && !defined(__clang__)
  20. #define __MICROSOFT_COMPILER
  21. #endif
  22. #if defined(_WIN32) && (defined(_MSC_VER) || defined(__INTEL_COMPILER) || defined(__clang__)) // Windows: MSVC / Intel compiler / clang
  23. #include <intrin.h>
  24. #include <new.h>
  25. #define FORCE_INLINE __forceinline
  26. FORCE_INLINE unsigned long find_clear_lsb(unsigned int *mask)
  27. {
  28. unsigned long idx;
  29. _BitScanForward(&idx, *mask);
  30. *mask &= *mask - 1;
  31. return idx;
  32. }
  33. FORCE_INLINE void *aligned_alloc(size_t alignment, size_t size)
  34. {
  35. return _aligned_malloc(size, alignment);
  36. }
  37. FORCE_INLINE void aligned_free(void *ptr)
  38. {
  39. _aligned_free(ptr);
  40. }
  41. #elif defined(__GNUG__) || defined(__clang__) // G++ or clang
  42. #include <cpuid.h>
  43. #if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
  44. #include <malloc/malloc.h> // memalign
  45. #else
  46. #include <malloc.h> // memalign
  47. #endif
  48. #include <mm_malloc.h>
  49. #include <immintrin.h>
  50. #include <new>
  51. #define FORCE_INLINE inline
  52. FORCE_INLINE unsigned long find_clear_lsb(unsigned int *mask)
  53. {
  54. unsigned long idx;
  55. idx = __builtin_ctzl(*mask);
  56. *mask &= *mask - 1;
  57. return idx;
  58. }
  59. FORCE_INLINE void *aligned_alloc(size_t alignment, size_t size)
  60. {
  61. return memalign(alignment, size);
  62. }
  63. FORCE_INLINE void aligned_free(void *ptr)
  64. {
  65. free(ptr);
  66. }
  67. FORCE_INLINE void __cpuidex(int* cpuinfo, int function, int subfunction)
  68. {
  69. __cpuid_count(function, subfunction, cpuinfo[0], cpuinfo[1], cpuinfo[2], cpuinfo[3]);
  70. }
  71. FORCE_INLINE unsigned long long _xgetbv(unsigned int index)
  72. {
  73. unsigned int eax, edx;
  74. __asm__ __volatile__(
  75. "xgetbv;"
  76. : "=a" (eax), "=d"(edx)
  77. : "c" (index)
  78. );
  79. return ((unsigned long long)edx << 32) | eax;
  80. }
  81. #else
  82. #error Unsupported compiler
  83. #endif