1*635a8641SAndroid Build Coastguard Worker // Copyright 2016 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_BIT_CAST_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_BIT_CAST_H_
7*635a8641SAndroid Build Coastguard Worker
8*635a8641SAndroid Build Coastguard Worker #include <string.h>
9*635a8641SAndroid Build Coastguard Worker #include <type_traits>
10*635a8641SAndroid Build Coastguard Worker
11*635a8641SAndroid Build Coastguard Worker #include "base/compiler_specific.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/template_util.h"
13*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
14*635a8641SAndroid Build Coastguard Worker
15*635a8641SAndroid Build Coastguard Worker // bit_cast<Dest,Source> is a template function that implements the equivalent
16*635a8641SAndroid Build Coastguard Worker // of "*reinterpret_cast<Dest*>(&source)". We need this in very low-level
17*635a8641SAndroid Build Coastguard Worker // functions like the protobuf library and fast math support.
18*635a8641SAndroid Build Coastguard Worker //
19*635a8641SAndroid Build Coastguard Worker // float f = 3.14159265358979;
20*635a8641SAndroid Build Coastguard Worker // int i = bit_cast<int32_t>(f);
21*635a8641SAndroid Build Coastguard Worker // // i = 0x40490fdb
22*635a8641SAndroid Build Coastguard Worker //
23*635a8641SAndroid Build Coastguard Worker // The classical address-casting method is:
24*635a8641SAndroid Build Coastguard Worker //
25*635a8641SAndroid Build Coastguard Worker // // WRONG
26*635a8641SAndroid Build Coastguard Worker // float f = 3.14159265358979; // WRONG
27*635a8641SAndroid Build Coastguard Worker // int i = * reinterpret_cast<int*>(&f); // WRONG
28*635a8641SAndroid Build Coastguard Worker //
29*635a8641SAndroid Build Coastguard Worker // The address-casting method actually produces undefined behavior according to
30*635a8641SAndroid Build Coastguard Worker // the ISO C++98 specification, section 3.10 ("basic.lval"), paragraph 15.
31*635a8641SAndroid Build Coastguard Worker // (This did not substantially change in C++11.) Roughly, this section says: if
32*635a8641SAndroid Build Coastguard Worker // an object in memory has one type, and a program accesses it with a different
33*635a8641SAndroid Build Coastguard Worker // type, then the result is undefined behavior for most values of "different
34*635a8641SAndroid Build Coastguard Worker // type".
35*635a8641SAndroid Build Coastguard Worker //
36*635a8641SAndroid Build Coastguard Worker // This is true for any cast syntax, either *(int*)&f or
37*635a8641SAndroid Build Coastguard Worker // *reinterpret_cast<int*>(&f). And it is particularly true for conversions
38*635a8641SAndroid Build Coastguard Worker // between integral lvalues and floating-point lvalues.
39*635a8641SAndroid Build Coastguard Worker //
40*635a8641SAndroid Build Coastguard Worker // The purpose of this paragraph is to allow optimizing compilers to assume that
41*635a8641SAndroid Build Coastguard Worker // expressions with different types refer to different memory. Compilers are
42*635a8641SAndroid Build Coastguard Worker // known to take advantage of this. So a non-conforming program quietly
43*635a8641SAndroid Build Coastguard Worker // produces wildly incorrect output.
44*635a8641SAndroid Build Coastguard Worker //
45*635a8641SAndroid Build Coastguard Worker // The problem is not the use of reinterpret_cast. The problem is type punning:
46*635a8641SAndroid Build Coastguard Worker // holding an object in memory of one type and reading its bits back using a
47*635a8641SAndroid Build Coastguard Worker // different type.
48*635a8641SAndroid Build Coastguard Worker //
49*635a8641SAndroid Build Coastguard Worker // The C++ standard is more subtle and complex than this, but that is the basic
50*635a8641SAndroid Build Coastguard Worker // idea.
51*635a8641SAndroid Build Coastguard Worker //
52*635a8641SAndroid Build Coastguard Worker // Anyways ...
53*635a8641SAndroid Build Coastguard Worker //
54*635a8641SAndroid Build Coastguard Worker // bit_cast<> calls memcpy() which is blessed by the standard, especially by the
55*635a8641SAndroid Build Coastguard Worker // example in section 3.9 . Also, of course, bit_cast<> wraps up the nasty
56*635a8641SAndroid Build Coastguard Worker // logic in one place.
57*635a8641SAndroid Build Coastguard Worker //
58*635a8641SAndroid Build Coastguard Worker // Fortunately memcpy() is very fast. In optimized mode, compilers replace
59*635a8641SAndroid Build Coastguard Worker // calls to memcpy() with inline object code when the size argument is a
60*635a8641SAndroid Build Coastguard Worker // compile-time constant. On a 32-bit system, memcpy(d,s,4) compiles to one
61*635a8641SAndroid Build Coastguard Worker // load and one store, and memcpy(d,s,8) compiles to two loads and two stores.
62*635a8641SAndroid Build Coastguard Worker
63*635a8641SAndroid Build Coastguard Worker template <class Dest, class Source>
bit_cast(const Source & source)64*635a8641SAndroid Build Coastguard Worker inline Dest bit_cast(const Source& source) {
65*635a8641SAndroid Build Coastguard Worker static_assert(sizeof(Dest) == sizeof(Source),
66*635a8641SAndroid Build Coastguard Worker "bit_cast requires source and destination to be the same size");
67*635a8641SAndroid Build Coastguard Worker static_assert(base::is_trivially_copyable<Dest>::value,
68*635a8641SAndroid Build Coastguard Worker "bit_cast requires the destination type to be copyable");
69*635a8641SAndroid Build Coastguard Worker static_assert(base::is_trivially_copyable<Source>::value,
70*635a8641SAndroid Build Coastguard Worker "bit_cast requires the source type to be copyable");
71*635a8641SAndroid Build Coastguard Worker
72*635a8641SAndroid Build Coastguard Worker Dest dest;
73*635a8641SAndroid Build Coastguard Worker memcpy(&dest, &source, sizeof(dest));
74*635a8641SAndroid Build Coastguard Worker return dest;
75*635a8641SAndroid Build Coastguard Worker }
76*635a8641SAndroid Build Coastguard Worker
77*635a8641SAndroid Build Coastguard Worker #endif // BASE_BIT_CAST_H_
78