Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 1 | //===-- llvm/ADT/bit.h - C++20 <bit> ----------------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the C++20 <bit> header. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_ADT_BIT_H |
| 14 | #define LLVM_ADT_BIT_H |
| 15 | |
| 16 | #include "llvm/Support/Compiler.h" |
| 17 | #include <cstring> |
| 18 | #include <type_traits> |
| 19 | |
| 20 | namespace llvm { |
| 21 | |
| 22 | // This implementation of bit_cast is different from the C++17 one in two ways: |
| 23 | // - It isn't constexpr because that requires compiler support. |
| 24 | // - It requires trivially-constructible To, to avoid UB in the implementation. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 25 | template < |
| 26 | typename To, typename From, |
| 27 | typename = std::enable_if_t<sizeof(To) == sizeof(From)> |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 28 | #if (__has_feature(is_trivially_constructible) && defined(_LIBCPP_VERSION)) || \ |
| 29 | (defined(__GNUC__) && __GNUC__ >= 5) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 30 | , |
| 31 | typename = std::enable_if_t<std::is_trivially_constructible<To>::value> |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 32 | #elif __has_feature(is_trivially_constructible) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 33 | , |
| 34 | typename = std::enable_if_t<__is_trivially_constructible(To)> |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 35 | #else |
| 36 | // See comment below. |
| 37 | #endif |
| 38 | #if (__has_feature(is_trivially_copyable) && defined(_LIBCPP_VERSION)) || \ |
| 39 | (defined(__GNUC__) && __GNUC__ >= 5) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 40 | , |
| 41 | typename = std::enable_if_t<std::is_trivially_copyable<To>::value>, |
| 42 | typename = std::enable_if_t<std::is_trivially_copyable<From>::value> |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 43 | #elif __has_feature(is_trivially_copyable) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 44 | , |
| 45 | typename = std::enable_if_t<__is_trivially_copyable(To)>, |
| 46 | typename = std::enable_if_t<__is_trivially_copyable(From)> |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 47 | #else |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 48 | // This case is GCC 4.x. clang with libc++ or libstdc++ never get here. Unlike |
| 49 | // llvm/Support/type_traits.h's is_trivially_copyable we don't want to |
| 50 | // provide a good-enough answer here: developers in that configuration will hit |
| 51 | // compilation failures on the bots instead of locally. That's acceptable |
| 52 | // because it's very few developers, and only until we move past C++11. |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 53 | #endif |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 54 | > |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 55 | inline To bit_cast(const From &from) noexcept { |
| 56 | To to; |
| 57 | std::memcpy(&to, &from, sizeof(To)); |
| 58 | return to; |
| 59 | } |
| 60 | |
| 61 | } // namespace llvm |
| 62 | |
| 63 | #endif |