Builtins.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2016-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy 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,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #if defined(_WIN32) && !defined(__clang__)
  18. #include <assert.h>
  19. #include <folly/Portability.h>
  20. #include <intrin.h>
  21. #include <stdint.h>
  22. namespace folly {
  23. namespace portability {
  24. namespace detail {
  25. void call_flush_instruction_cache_self_pid(void* begin, size_t size);
  26. }
  27. } // namespace portability
  28. } // namespace folly
  29. FOLLY_ALWAYS_INLINE void __builtin___clear_cache(char* begin, char* end) {
  30. if (folly::kIsArchAmd64) {
  31. // x86_64 doesn't require the instruction cache to be flushed after
  32. // modification.
  33. } else {
  34. // Default to flushing it for everything else, such as ARM.
  35. folly::portability::detail::call_flush_instruction_cache_self_pid(
  36. static_cast<void*>(begin), static_cast<size_t>(end - begin));
  37. }
  38. }
  39. FOLLY_ALWAYS_INLINE int __builtin_clz(unsigned int x) {
  40. unsigned long index;
  41. return int(_BitScanReverse(&index, (unsigned long)x) ? 31 - index : 32);
  42. }
  43. FOLLY_ALWAYS_INLINE int __builtin_clzl(unsigned long x) {
  44. return __builtin_clz((unsigned int)x);
  45. }
  46. FOLLY_ALWAYS_INLINE int __builtin_clzll(unsigned long long x) {
  47. unsigned long index;
  48. return int(_BitScanReverse64(&index, x) ? 63 - index : 64);
  49. }
  50. FOLLY_ALWAYS_INLINE int __builtin_ctz(unsigned int x) {
  51. unsigned long index;
  52. return int(_BitScanForward(&index, (unsigned long)x) ? index : 32);
  53. }
  54. FOLLY_ALWAYS_INLINE int __builtin_ctzl(unsigned long x) {
  55. return __builtin_ctz((unsigned int)x);
  56. }
  57. FOLLY_ALWAYS_INLINE int __builtin_ctzll(unsigned long long x) {
  58. unsigned long index;
  59. return int(_BitScanForward64(&index, x) ? index : 64);
  60. }
  61. FOLLY_ALWAYS_INLINE int __builtin_ffs(int x) {
  62. unsigned long index;
  63. return int(_BitScanForward(&index, (unsigned long)x) ? index + 1 : 0);
  64. }
  65. FOLLY_ALWAYS_INLINE int __builtin_ffsl(long x) {
  66. return __builtin_ffs(int(x));
  67. }
  68. FOLLY_ALWAYS_INLINE int __builtin_ffsll(long long x) {
  69. unsigned long index;
  70. return int(_BitScanForward64(&index, (unsigned long long)x) ? index + 1 : 0);
  71. }
  72. FOLLY_ALWAYS_INLINE int __builtin_popcount(unsigned int x) {
  73. return int(__popcnt(x));
  74. }
  75. FOLLY_ALWAYS_INLINE int __builtin_popcountl(unsigned long x) {
  76. static_assert(sizeof(x) == 4, "");
  77. return int(__popcnt(x));
  78. }
  79. FOLLY_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x) {
  80. return int(__popcnt64(x));
  81. }
  82. FOLLY_ALWAYS_INLINE void* __builtin_return_address(unsigned int frame) {
  83. // I really hope frame is zero...
  84. (void)frame;
  85. assert(frame == 0);
  86. return _ReturnAddress();
  87. }
  88. #endif