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> | |
| 10 | |
| 11 #include "build/build_config.h" | |
| 9 | 12 |
| 10 // bit_cast<Dest,Source> is a template function that implements the equivalent | 13 // bit_cast<Dest,Source> is a template function that implements the equivalent |
| 11 // of "*reinterpret_cast<Dest*>(&source)". We need this in very low-level | 14 // of "*reinterpret_cast<Dest*>(&source)". We need this in very low-level |
| 12 // functions like the protobuf library and fast math support. | 15 // functions like the protobuf library and fast math support. |
| 13 // | 16 // |
| 14 // float f = 3.14159265358979; | 17 // float f = 3.14159265358979; |
| 15 // int i = bit_cast<int32_t>(f); | 18 // int i = bit_cast<int32_t>(f); |
| 16 // // i = 0x40490fdb | 19 // // i = 0x40490fdb |
| 17 // | 20 // |
| 18 // The classical address-casting method is: | 21 // The classical address-casting method is: |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 47 // Anyways ... | 50 // Anyways ... |
| 48 // | 51 // |
| 49 // bit_cast<> calls memcpy() which is blessed by the standard, especially by the | 52 // bit_cast<> calls memcpy() which is blessed by the standard, especially by the |
| 50 // example in section 3.9 . Also, of course, bit_cast<> wraps up the nasty | 53 // example in section 3.9 . Also, of course, bit_cast<> wraps up the nasty |
| 51 // logic in one place. | 54 // logic in one place. |
| 52 // | 55 // |
| 53 // Fortunately memcpy() is very fast. In optimized mode, compilers replace | 56 // Fortunately memcpy() is very fast. In optimized mode, compilers replace |
| 54 // calls to memcpy() with inline object code when the size argument is a | 57 // calls to memcpy() with inline object code when the size argument is a |
| 55 // compile-time constant. On a 32-bit system, memcpy(d,s,4) compiles to one | 58 // compile-time constant. On a 32-bit system, memcpy(d,s,4) compiles to one |
| 56 // load and one store, and memcpy(d,s,8) compiles to two loads and two stores. | 59 // load and one store, and memcpy(d,s,8) compiles to two loads and two stores. |
| 57 // | |
| 58 // WARNING: if Dest or Source is a non-POD type, the result of the memcpy | |
| 59 // is likely to surprise you. | |
| 60 | 60 |
| 61 template <class Dest, class Source> | 61 template <class Dest, class Source> |
| 62 inline Dest bit_cast(const Source& source) { | 62 inline Dest bit_cast(const Source& source) { |
| 63 static_assert(sizeof(Dest) == sizeof(Source), | 63 static_assert(sizeof(Dest) == sizeof(Source), |
| 64 "bit_cast requires source and destination to be the same size"); | 64 "bit_cast requires source and destination to be the same size"); |
| 65 | 65 |
| 66 #if ((defined(__GLIBCXX__) && (__GLIBCXX__ >= 20150422)) || \ | |
| 67 defined(_LIBCPP_VERSION)) | |
| 68 // GCC 5.1 contains the first libstdc++ with is_trivially_copyable. | |
| 69 // Assume libc++ Just Works: is_trivially_copyable added on May 13th 2011. | |
| 70 static_assert(std::is_trivially_copyable<Dest>::value, | |
| 71 "non-trivially-copyable bit_cast is undefined"); | |
| 72 static_assert(std::is_trivially_copyable<Source>::value, | |
| 73 "non-trivially-copyable bit_cast is undefined"); | |
| 74 #elif defined(__has_feature) && __has_feature(is_trivially_copyable) | |
|
Nico
2016/03/31 22:28:15
this has to be on two lines:
../../base/bit_cast.
| |
| 75 // The compiler supports an equivalent intrinsic. | |
| 76 static_assert(__is_trivially_copyable(Dest), | |
| 77 "non-trivially-copyable bit_cast is undefined"); | |
| 78 static_assert(__is_trivially_copyable(Source), | |
| 79 "non-trivially-copyable bit_cast is undefined"); | |
| 80 #elif COMPILER_GCC | |
| 81 // Fallback to compiler intrinsic on GCC and clang (which pretends to be | |
| 82 // GCC). This isn't quite the same as is_trivially_copyable but it'll do for | |
| 83 // our purpose. | |
| 84 static_assert(__has_trivial_copy(Dest), | |
| 85 "non-trivially-copyable bit_cast is undefined"); | |
| 86 static_assert(__has_trivial_copy(Source), | |
| 87 "non-trivially-copyable bit_cast is undefined"); | |
| 88 #else | |
| 89 // Do nothing, let the bots handle it. | |
| 90 #endif | |
| 91 | |
| 66 Dest dest; | 92 Dest dest; |
| 67 memcpy(&dest, &source, sizeof(dest)); | 93 memcpy(&dest, &source, sizeof(dest)); |
| 68 return dest; | 94 return dest; |
| 69 } | 95 } |
| 70 | 96 |
| 71 #endif // BASE_BIT_CAST_H_ | 97 #endif // BASE_BIT_CAST_H_ |
| OLD | NEW |