Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADT: add <bit> header, implement C++20 bit_cast, use
Summary: I saw a few places that were punning through a union of FP and integer, and that made me sad. Luckily, C++20 adds bit_cast for exactly that purpose. Implement our own version in ADT (without constexpr, leaving us a bit sad), and use it in the few places my grep-fu found silly union punning. Reviewers: javed.absar Subscribers: dexonsmith, llvm-commits Differential Revision: https://reviews.llvm.org/D51693 llvm-svn: 341728
- Loading branch information
Showing
4 changed files
with
61 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//===-- llvm/ADT/bit.h - C++20 <bit> ----------------------------*- C++ -*-===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file implements the C++20 <bit> header. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_ADT_BIT_H | ||
#define LLVM_ADT_BIT_H | ||
|
||
#include <cstring> | ||
#include <type_traits> | ||
|
||
namespace llvm { | ||
|
||
template < | ||
typename To, typename From, | ||
typename = typename std::enable_if<sizeof(To) == sizeof(From)>::type, | ||
typename = | ||
typename std::enable_if<std::is_trivially_copyable<To>::value>::type, | ||
typename = | ||
typename std::enable_if<std::is_trivially_copyable<From>::value>::type> | ||
inline To bit_cast(const From &from) noexcept { | ||
typename std::aligned_storage<sizeof(To), alignof(To)>::type storage; | ||
std::memcpy(&storage, &from, sizeof(To)); | ||
return reinterpret_cast<To &>(storage); | ||
} | ||
|
||
} // namespace llvm | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters