bit.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/ADT/bit.h - C++20 <bit> ----------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file implements the C++20 <bit> header.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ADT_BIT_H
  18. #define LLVM_ADT_BIT_H
  19. #include "llvm/Support/Compiler.h"
  20. #include <cstring>
  21. #include <type_traits>
  22. namespace llvm {
  23. // This implementation of bit_cast is different from the C++17 one in two ways:
  24. // - It isn't constexpr because that requires compiler support.
  25. // - It requires trivially-constructible To, to avoid UB in the implementation.
  26. template <
  27. typename To, typename From,
  28. typename = std::enable_if_t<sizeof(To) == sizeof(From)>
  29. #if (__has_feature(is_trivially_constructible) && defined(_LIBCPP_VERSION)) || \
  30. (defined(__GNUC__) && __GNUC__ >= 5)
  31. ,
  32. typename = std::enable_if_t<std::is_trivially_constructible<To>::value>
  33. #elif __has_feature(is_trivially_constructible)
  34. ,
  35. typename = std::enable_if_t<__is_trivially_constructible(To)>
  36. #else
  37. // See comment below.
  38. #endif
  39. #if (__has_feature(is_trivially_copyable) && defined(_LIBCPP_VERSION)) || \
  40. (defined(__GNUC__) && __GNUC__ >= 5)
  41. ,
  42. typename = std::enable_if_t<std::is_trivially_copyable<To>::value>,
  43. typename = std::enable_if_t<std::is_trivially_copyable<From>::value>
  44. #elif __has_feature(is_trivially_copyable)
  45. ,
  46. typename = std::enable_if_t<__is_trivially_copyable(To)>,
  47. typename = std::enable_if_t<__is_trivially_copyable(From)>
  48. #else
  49. // This case is GCC 4.x. clang with libc++ or libstdc++ never get here. Unlike
  50. // llvm/Support/type_traits.h's is_trivially_copyable we don't want to
  51. // provide a good-enough answer here: developers in that configuration will hit
  52. // compilation failures on the bots instead of locally. That's acceptable
  53. // because it's very few developers, and only until we move past C++11.
  54. #endif
  55. >
  56. inline To bit_cast(const From &from) noexcept {
  57. To to;
  58. std::memcpy(&to, &from, sizeof(To));
  59. return to;
  60. }
  61. } // namespace llvm
  62. #endif
  63. #ifdef __GNUC__
  64. #pragma GCC diagnostic pop
  65. #endif