Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 // bit_cast<Dest,Source> is a template function that implements the | |
| 11 // equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in | |
| 12 // very low-level functions like the protobuf library and fast math | |
| 13 // support. | |
| 14 // | |
| 15 // float f = 3.14159265358979; | |
| 16 // int i = bit_cast<int32_t>(f); | |
| 17 // // i = 0x40490fdb | |
| 18 // | |
| 19 // The classical address-casting method is: | |
| 20 // | |
| 21 // // WRONG | |
| 22 // float f = 3.14159265358979; // WRONG | |
| 23 // int i = * reinterpret_cast<int*>(&f); // WRONG | |
| 24 // | |
| 25 // The address-casting method actually produces undefined behavior | |
| 26 // according to ISO C++ specification section 3.10 -15 -. Roughly, this | |
|
Avi (use Gerrit)
2015/12/30 16:57:37
"-15 -"?
Perhaps:
"according to the ISO C++98 sp
tapted
2016/01/03 23:51:06
Done.
| |
| 27 // section says: if an object in memory has one type, and a program | |
| 28 // accesses it with a different type, then the result is undefined | |
| 29 // behavior for most values of "different type". | |
| 30 // | |
| 31 // This is true for any cast syntax, either *(int*)&f or | |
| 32 // *reinterpret_cast<int*>(&f). And it is particularly true for | |
| 33 // conversions between integral lvalues and floating-point lvalues. | |
| 34 // | |
| 35 // The purpose of 3.10 -15- is to allow optimizing compilers to assume | |
|
Avi (use Gerrit)
2015/12/30 16:57:37
"-15-" again?
"The purpose of this paragraph..."
tapted
2016/01/03 23:51:06
Done.
| |
| 36 // that expressions with different types refer to different memory. gcc | |
| 37 // 4.0.1 has an optimizer that takes advantage of this. So a | |
|
Avi (use Gerrit)
2015/12/30 16:57:37
GCC 4.0.1 was released in 2005. Do we want to keep
tapted
2016/01/03 23:51:06
I went with "Compilers are known to take advantage
| |
| 38 // non-conforming program quietly produces wildly incorrect output. | |
| 39 // | |
| 40 // The problem is not the use of reinterpret_cast. The problem is type | |
| 41 // punning: holding an object in memory of one type and reading its bits | |
| 42 // back using a different type. | |
| 43 // | |
| 44 // The C++ standard is more subtle and complex than this, but that | |
| 45 // is the basic idea. | |
| 46 // | |
| 47 // Anyways ... | |
| 48 // | |
| 49 // bit_cast<> calls memcpy() which is blessed by the standard, | |
| 50 // especially by the example in section 3.9 . Also, of course, | |
| 51 // bit_cast<> wraps up the nasty logic in one place. | |
| 52 // | |
| 53 // Fortunately memcpy() is very fast. In optimized mode, with a | |
| 54 // constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline | |
| 55 // code with the minimal amount of data movement. On a 32-bit system, | |
| 56 // memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8) | |
| 57 // compiles to two loads and two stores. | |
| 58 // | |
| 59 // I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1. | |
|
Avi (use Gerrit)
2015/12/30 16:57:37
More references to incredibly obsolete compilers :
tapted
2016/01/03 23:51:06
I think what's happening anyway is not that memcpy
| |
| 60 // | |
| 61 // WARNING: if Dest or Source is a non-POD type, the result of the memcpy | |
| 62 // is likely to surprise you. | |
|
Avi (use Gerrit)
2015/12/30 16:57:37
We should totally rewrite this template using the
tapted
2016/01/03 23:51:06
Done.
| |
| 63 | |
| 64 template <class Dest, class Source> | |
| 65 inline Dest bit_cast(const Source& source) { | |
| 66 static_assert(sizeof(Dest) == sizeof(Source), | |
| 67 "bit_cast requires source and destination to be the same size"); | |
|
Avi (use Gerrit)
2015/12/30 17:32:43
Actually, just insert two lines:
static_assert(st
tapted
2016/01/03 23:51:06
Done.
| |
| 68 | |
| 69 Dest dest; | |
| 70 memcpy(&dest, &source, sizeof(dest)); | |
| 71 return dest; | |
| 72 } | |
| 73 | |
| 74 #endif // BASE_BIT_CAST_H_ | |
| OLD | NEW |