Asm.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #include <folly/Portability.h>
  18. #include <chrono>
  19. #include <cstdint>
  20. #ifdef _MSC_VER
  21. #include <intrin.h>
  22. #endif
  23. namespace folly {
  24. inline void asm_volatile_memory() {
  25. #if defined(__clang__) || defined(__GNUC__)
  26. asm volatile("" : : : "memory");
  27. #elif defined(_MSC_VER)
  28. ::_ReadWriteBarrier();
  29. #endif
  30. }
  31. inline void asm_volatile_pause() {
  32. #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
  33. ::_mm_pause();
  34. #elif defined(__i386__) || FOLLY_X64
  35. asm volatile("pause");
  36. #elif FOLLY_AARCH64 || defined(__arm__)
  37. asm volatile("yield");
  38. #elif FOLLY_PPC64
  39. asm volatile("or 27,27,27");
  40. #endif
  41. }
  42. inline std::uint64_t asm_rdtsc() {
  43. #if _MSC_VER
  44. return (uint64_t)__rdtsc();
  45. #elif defined(__i386__) || FOLLY_X64
  46. // read the timestamp counter on x86
  47. auto hi = std::uint32_t{};
  48. auto lo = std::uint32_t{};
  49. asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
  50. return (((std::uint64_t)lo) + (((std::uint64_t)hi) << 32));
  51. #else
  52. // use steady_clock::now() as an approximation for the timestamp counter on
  53. // non-x86 systems
  54. return std::chrono::steady_clock::now().time_since_epoch().count();
  55. #endif
  56. }
  57. } // namespace folly