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

Side by Side Diff: base/bit_cast.h

Issue 399313006: Move bit_cast from base/macros.h to its own header (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: bit_cast spruce up+fix typo Created 4 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « base/base.gypi ('k') | base/macros.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef BASE_BIT_CAST_H_
6 #define BASE_BIT_CAST_H_
7
8 #include <string.h> // For memcpy.
9
10 #include <type_traits>
11
12 // bit_cast<Dest,Source> is a template function that implements the equivalent
13 // of "*reinterpret_cast<Dest*>(&source)". We need this in very low-level
14 // functions like the protobuf library and fast math support.
15 //
16 // float f = 3.14159265358979;
17 // int i = bit_cast<int32_t>(f);
18 // // i = 0x40490fdb
19 //
20 // The classical address-casting method is:
21 //
22 // // WRONG
23 // float f = 3.14159265358979; // WRONG
24 // int i = * reinterpret_cast<int*>(&f); // WRONG
25 //
26 // The address-casting method actually produces undefined behavior according to
27 // the ISO C++98 specification, section 3.10 ("basic.lval"), paragraph 15.
28 // (This did not substantially change in C++11.) Roughly, this section says: if
29 // an object in memory has one type, and a program accesses it with a different
30 // type, then the result is undefined behavior for most values of "different
31 // type".
32 //
33 // This is true for any cast syntax, either *(int*)&f or
34 // *reinterpret_cast<int*>(&f). And it is particularly true for conversions
35 // between integral lvalues and floating-point lvalues.
36 //
37 // The purpose of this paragraph is to allow optimizing compilers to assume that
38 // expressions with different types refer to different memory. Compilers are
39 // known to take advantage of this. So a non-conforming program quietly
40 // produces wildly incorrect output.
41 //
42 // The problem is not the use of reinterpret_cast. The problem is type punning:
43 // holding an object in memory of one type and reading its bits back using a
44 // different type.
45 //
46 // The C++ standard is more subtle and complex than this, but that is the basic
47 // idea.
48 //
49 // Anyways ...
50 //
51 // bit_cast<> calls memcpy() which is blessed by the standard, especially by the
52 // example in section 3.9 . Also, of course, bit_cast<> wraps up the nasty
53 // logic in one place.
54 //
55 // Fortunately memcpy() is very fast. In optimized mode, compilers replace
56 // calls to memcpy() with inline object code when the size argument is a
57 // compile-time constant. On a 32-bit system, memcpy(d,s,4) compiles to one
58 // load and one store, and memcpy(d,s,8) compiles to two loads and two stores.
59 //
60 // Note: Dest and Source must be POD types.
61 //
62 template <class Dest, class Source>
63 inline Dest bit_cast(const Source& source) {
64 static_assert(sizeof(Dest) == sizeof(Source),
65 "bit_cast requires source and destination to be the same size");
66 static_assert(std::is_trivially_copyable<Source>::value,
67 "Source must be a POD type or trivially copyable");
68 static_assert(std::is_trivially_copyable<Dest>::value,
69 "Dest must be a POD type or trivially copyable");
Avi (use Gerrit) 2016/01/04 00:24:46 This is indeed a better choice than is_pod.
tapted 2016/01/04 00:59:30 Doh - bots are still reporting in, but it looks li
70
71 Dest dest;
72 memcpy(&dest, &source, sizeof(dest));
73 return dest;
74 }
75
76 #endif // BASE_BIT_CAST_H_
OLDNEW
« no previous file with comments | « base/base.gypi ('k') | base/macros.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698