Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Side by Side Diff: base/bit_cast.h

Issue 1837563002: bit_cast: check is_trivially_copyable (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix __has_feature Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/compiler_specific.h » ('j') | base/compiler_specific.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/compiler_specific.h"
12 #include "build/build_config.h"
9 13
10 // bit_cast<Dest,Source> is a template function that implements the equivalent 14 // 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 15 // of "*reinterpret_cast<Dest*>(&source)". We need this in very low-level
12 // functions like the protobuf library and fast math support. 16 // functions like the protobuf library and fast math support.
13 // 17 //
14 // float f = 3.14159265358979; 18 // float f = 3.14159265358979;
15 // int i = bit_cast<int32_t>(f); 19 // int i = bit_cast<int32_t>(f);
16 // // i = 0x40490fdb 20 // // i = 0x40490fdb
17 // 21 //
18 // The classical address-casting method is: 22 // The classical address-casting method is:
(...skipping 28 matching lines...) Expand all
47 // Anyways ... 51 // Anyways ...
48 // 52 //
49 // bit_cast<> calls memcpy() which is blessed by the standard, especially by the 53 // 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 54 // example in section 3.9 . Also, of course, bit_cast<> wraps up the nasty
51 // logic in one place. 55 // logic in one place.
52 // 56 //
53 // Fortunately memcpy() is very fast. In optimized mode, compilers replace 57 // Fortunately memcpy() is very fast. In optimized mode, compilers replace
54 // 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
55 // 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
56 // 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.
57 //
58 // WARNING: if Dest or Source is a non-POD type, the result of the memcpy
59 // is likely to surprise you.
60 61
61 template <class Dest, class Source> 62 template <class Dest, class Source>
62 inline Dest bit_cast(const Source& source) { 63 inline Dest bit_cast(const Source& source) {
63 static_assert(sizeof(Dest) == sizeof(Source), 64 static_assert(sizeof(Dest) == sizeof(Source),
64 "bit_cast requires source and destination to be the same size"); 65 "bit_cast requires source and destination to be the same size");
65 66
67 #if ((defined(__GLIBCXX__) && (__GLIBCXX__ >= 20150422)) || \
68 defined(_LIBCPP_VERSION))
69 // GCC 5.1 contains the first libstdc++ with is_trivially_copyable.
70 // Assume libc++ Just Works: is_trivially_copyable added on May 13th 2011.
71 static_assert(std::is_trivially_copyable<Dest>::value,
72 "non-trivially-copyable bit_cast is undefined");
73 static_assert(std::is_trivially_copyable<Source>::value,
74 "non-trivially-copyable bit_cast is undefined");
75 #elif __has_feature(is_trivially_copyable)
76 // The compiler supports an equivalent intrinsic.
77 static_assert(__is_trivially_copyable(Dest),
78 "non-trivially-copyable bit_cast is undefined");
79 static_assert(__is_trivially_copyable(Source),
80 "non-trivially-copyable bit_cast is undefined");
81 #elif COMPILER_GCC
82 // Fallback to compiler intrinsic on GCC and clang (which pretends to be
83 // GCC). This isn't quite the same as is_trivially_copyable but it'll do for
84 // our purpose.
85 static_assert(__has_trivial_copy(Dest),
86 "non-trivially-copyable bit_cast is undefined");
87 static_assert(__has_trivial_copy(Source),
88 "non-trivially-copyable bit_cast is undefined");
89 #else
90 // Do nothing, let the bots handle it.
91 #endif
92
66 Dest dest; 93 Dest dest;
67 memcpy(&dest, &source, sizeof(dest)); 94 memcpy(&dest, &source, sizeof(dest));
68 return dest; 95 return dest;
69 } 96 }
70 97
71 #endif // BASE_BIT_CAST_H_ 98 #endif // BASE_BIT_CAST_H_
OLDNEW
« no previous file with comments | « no previous file | base/compiler_specific.h » ('j') | base/compiler_specific.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698