1343171Sdim//===-- llvm/ADT/bit.h - C++20 <bit> ----------------------------*- C++ -*-===//
2343171Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6343171Sdim//
7343171Sdim//===----------------------------------------------------------------------===//
8343171Sdim//
9343171Sdim// This file implements the C++20 <bit> header.
10343171Sdim//
11343171Sdim//===----------------------------------------------------------------------===//
12343171Sdim
13343171Sdim#ifndef LLVM_ADT_BIT_H
14343171Sdim#define LLVM_ADT_BIT_H
15343171Sdim
16343171Sdim#include "llvm/Support/Compiler.h"
17343171Sdim#include <cstring>
18343171Sdim#include <type_traits>
19343171Sdim
20343171Sdimnamespace llvm {
21343171Sdim
22343171Sdim// This implementation of bit_cast is different from the C++17 one in two ways:
23343171Sdim//  - It isn't constexpr because that requires compiler support.
24343171Sdim//  - It requires trivially-constructible To, to avoid UB in the implementation.
25343171Sdimtemplate <typename To, typename From
26343171Sdim          , typename = typename std::enable_if<sizeof(To) == sizeof(From)>::type
27343171Sdim#if (__has_feature(is_trivially_constructible) && defined(_LIBCPP_VERSION)) || \
28343171Sdim    (defined(__GNUC__) && __GNUC__ >= 5)
29343171Sdim          , typename = typename std::is_trivially_constructible<To>::type
30343171Sdim#elif __has_feature(is_trivially_constructible)
31343171Sdim          , typename = typename std::enable_if<__is_trivially_constructible(To)>::type
32343171Sdim#else
33343171Sdim  // See comment below.
34343171Sdim#endif
35343171Sdim#if (__has_feature(is_trivially_copyable) && defined(_LIBCPP_VERSION)) || \
36343171Sdim    (defined(__GNUC__) && __GNUC__ >= 5)
37343171Sdim          , typename = typename std::enable_if<std::is_trivially_copyable<To>::value>::type
38343171Sdim          , typename = typename std::enable_if<std::is_trivially_copyable<From>::value>::type
39343171Sdim#elif __has_feature(is_trivially_copyable)
40343171Sdim          , typename = typename std::enable_if<__is_trivially_copyable(To)>::type
41343171Sdim          , typename = typename std::enable_if<__is_trivially_copyable(From)>::type
42343171Sdim#else
43353358Sdim// This case is GCC 4.x. clang with libc++ or libstdc++ never get here. Unlike
44353358Sdim// llvm/Support/type_traits.h's is_trivially_copyable we don't want to
45353358Sdim// provide a good-enough answer here: developers in that configuration will hit
46353358Sdim// compilation failures on the bots instead of locally. That's acceptable
47353358Sdim// because it's very few developers, and only until we move past C++11.
48343171Sdim#endif
49343171Sdim>
50343171Sdiminline To bit_cast(const From &from) noexcept {
51343171Sdim  To to;
52343171Sdim  std::memcpy(&to, &from, sizeof(To));
53343171Sdim  return to;
54343171Sdim}
55343171Sdim
56343171Sdim} // namespace llvm
57343171Sdim
58343171Sdim#endif
59