1 //===-- llvm/ADT/bit.h - C++20 <bit> ----------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// This file implements the C++20 <bit> header. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_ADT_BIT_H 15 #define LLVM_ADT_BIT_H 16 17 #include "llvm/Support/Compiler.h" 18 #include <cstring> 19 #include <type_traits> 20 21 namespace llvm { 22 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 62 } // namespace llvm 63 64 #endif 65