Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_BIT_CAST_H_ | 5 #ifndef BASE_BIT_CAST_H_ |
| 6 #define BASE_BIT_CAST_H_ | 6 #define BASE_BIT_CAST_H_ |
| 7 | 7 |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 #include <type_traits> | 9 #include <type_traits> |
| 10 | 10 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 56 // | 56 // |
| 57 // Fortunately memcpy() is very fast. In optimized mode, compilers replace | 57 // Fortunately memcpy() is very fast. In optimized mode, compilers replace |
| 58 // calls to memcpy() with inline object code when the size argument is a | 58 // calls to memcpy() with inline object code when the size argument is a |
| 59 // compile-time constant. On a 32-bit system, memcpy(d,s,4) compiles to one | 59 // compile-time constant. On a 32-bit system, memcpy(d,s,4) compiles to one |
| 60 // load and one store, and memcpy(d,s,8) compiles to two loads and two stores. | 60 // load and one store, and memcpy(d,s,8) compiles to two loads and two stores. |
| 61 | 61 |
| 62 template <class Dest, class Source> | 62 template <class Dest, class Source> |
| 63 inline Dest bit_cast(const Source& source) { | 63 inline Dest bit_cast(const Source& source) { |
| 64 static_assert(sizeof(Dest) == sizeof(Source), | 64 static_assert(sizeof(Dest) == sizeof(Source), |
| 65 "bit_cast requires source and destination to be the same size"); | 65 "bit_cast requires source and destination to be the same size"); |
| 66 static_assert(std::is_trivially_copyable<Dest>::value, | |
| 67 "bit_cast requires the destination type to be copyable"); | |
| 68 static_assert(std::is_trivially_copyable<Source>::value, | |
| 69 "bit_cast requires the source type to be copyable"); | |
| 66 | 70 |
| 67 #if (__GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1) || \ | 71 #if (__GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1) || \ |
| 68 (defined(__clang__) && defined(_LIBCPP_VERSION))) | 72 (defined(__clang__) && defined(_LIBCPP_VERSION))) |
| 69 // GCC 5.1 contains the first libstdc++ with is_trivially_copyable. | 73 // GCC 5.1 contains the first libstdc++ with is_trivially_copyable. |
| 70 // Assume libc++ Just Works: is_trivially_copyable added on May 13th 2011. | 74 // Assume libc++ Just Works: is_trivially_copyable added on May 13th 2011. |
| 71 // However, with libc++ when GCC is the compiler the trait is buggy, see | 75 // However, with libc++ when GCC is the compiler the trait is buggy, see |
| 72 // crbug.com/607158, so fall back to the less strict variant for non-clang. | 76 // crbug.com/607158, so fall back to the less strict variant for non-clang. |
| 73 static_assert(std::is_trivially_copyable<Dest>::value, | 77 static_assert(std::is_trivially_copyable<Dest>::value, |
|
danakj
2016/12/20 16:40:49
Wait we already have this. With crazy stuff for va
| |
| 74 "non-trivially-copyable bit_cast is undefined"); | 78 "non-trivially-copyable bit_cast is undefined"); |
| 75 static_assert(std::is_trivially_copyable<Source>::value, | 79 static_assert(std::is_trivially_copyable<Source>::value, |
| 76 "non-trivially-copyable bit_cast is undefined"); | 80 "non-trivially-copyable bit_cast is undefined"); |
| 77 #elif HAS_FEATURE(is_trivially_copyable) | 81 #elif HAS_FEATURE(is_trivially_copyable) |
| 78 // The compiler supports an equivalent intrinsic. | 82 // The compiler supports an equivalent intrinsic. |
| 79 static_assert(__is_trivially_copyable(Dest), | 83 static_assert(__is_trivially_copyable(Dest), |
| 80 "non-trivially-copyable bit_cast is undefined"); | 84 "non-trivially-copyable bit_cast is undefined"); |
| 81 static_assert(__is_trivially_copyable(Source), | 85 static_assert(__is_trivially_copyable(Source), |
| 82 "non-trivially-copyable bit_cast is undefined"); | 86 "non-trivially-copyable bit_cast is undefined"); |
| 83 #elif COMPILER_GCC | 87 #elif COMPILER_GCC |
| 84 // Fallback to compiler intrinsic on GCC and clang (which pretends to be | 88 // Fallback to compiler intrinsic on GCC and clang (which pretends to be |
| 85 // GCC). This isn't quite the same as is_trivially_copyable but it'll do for | 89 // GCC). This isn't quite the same as is_trivially_copyable but it'll do for |
| 86 // our purpose. | 90 // our purpose. |
| 87 static_assert(__has_trivial_copy(Dest), | 91 static_assert(__has_trivial_copy(Dest), |
| 88 "non-trivially-copyable bit_cast is undefined"); | 92 "non-trivially-copyable bit_cast is undefined"); |
| 89 static_assert(__has_trivial_copy(Source), | 93 static_assert(__has_trivial_copy(Source), |
| 90 "non-trivially-copyable bit_cast is undefined"); | 94 "non-trivially-copyable bit_cast is undefined"); |
| 91 #else | 95 #else |
| 92 // Do nothing, let the bots handle it. | 96 // Do nothing, let the bots handle it. |
| 93 #endif | 97 #endif |
| 94 | 98 |
| 95 Dest dest; | 99 Dest dest; |
| 96 memcpy(&dest, &source, sizeof(dest)); | 100 memcpy(&dest, &source, sizeof(dest)); |
| 97 return dest; | 101 return dest; |
| 98 } | 102 } |
| 99 | 103 |
| 100 #endif // BASE_BIT_CAST_H_ | 104 #endif // BASE_BIT_CAST_H_ |
| OLD | NEW |