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> 9#include <type_traits> 10 11#include "base/compiler_specific.h" 12#include "build/build_config.h" 13 14// bit_cast<Dest,Source> is a template function that implements the equivalent 15// of "*reinterpret_cast<Dest*>(&source)". We need this in very low-level 16// functions like the protobuf library and fast math support. 17// 18// float f = 3.14159265358979; 19// int i = bit_cast<int32_t>(f); 20// // i = 0x40490fdb 21// 22// The classical address-casting method is: 23// 24// // WRONG 25// float f = 3.14159265358979; // WRONG 26// int i = * reinterpret_cast<int*>(&f); // WRONG 27// 28// The address-casting method actually produces undefined behavior according to 29// the ISO C++98 specification, section 3.10 ("basic.lval"), paragraph 15. 30// (This did not substantially change in C++11.) Roughly, this section says: if 31// an object in memory has one type, and a program accesses it with a different 32// type, then the result is undefined behavior for most values of "different 33// type". 34// 35// This is true for any cast syntax, either *(int*)&f or 36// *reinterpret_cast<int*>(&f). And it is particularly true for conversions 37// between integral lvalues and floating-point lvalues. 38// 39// The purpose of this paragraph is to allow optimizing compilers to assume that 40// expressions with different types refer to different memory. Compilers are 41// known to take advantage of this. So a non-conforming program quietly 42// produces wildly incorrect output. 43// 44// The problem is not the use of reinterpret_cast. The problem is type punning: 45// holding an object in memory of one type and reading its bits back using a 46// different type. 47// 48// The C++ standard is more subtle and complex than this, but that is the basic 49// idea. 50// 51// Anyways ... 52// 53// bit_cast<> calls memcpy() which is blessed by the standard, especially by the 54// example in section 3.9 . Also, of course, bit_cast<> wraps up the nasty 55// logic in one place. 56// 57// Fortunately memcpy() is very fast. In optimized mode, compilers replace 58// calls to memcpy() with inline object code when the size argument is a 59// compile-time constant. On a 32-bit system, memcpy(d,s,4) compiles to one 60// load and one store, and memcpy(d,s,8) compiles to two loads and two stores. 61 62template <class Dest, class Source> 63inline 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 67#if (__GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1) || \ 68 (defined(__clang__) && 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 // However, with libc++ when GCC is the compiler the trait is buggy, see 72 // crbug.com/607158, so fall back to the less strict variant for non-clang. 73 static_assert(std::is_trivially_copyable<Dest>::value, 74 "non-trivially-copyable bit_cast is undefined"); 75 static_assert(std::is_trivially_copyable<Source>::value, 76 "non-trivially-copyable bit_cast is undefined"); 77#elif HAS_FEATURE(is_trivially_copyable) 78 // The compiler supports an equivalent intrinsic. 79 static_assert(__is_trivially_copyable(Dest), 80 "non-trivially-copyable bit_cast is undefined"); 81 static_assert(__is_trivially_copyable(Source), 82 "non-trivially-copyable bit_cast is undefined"); 83#elif COMPILER_GCC 84 // Fallback to compiler intrinsic on GCC and clang (which pretends to be 85 // GCC). This isn't quite the same as is_trivially_copyable but it'll do for 86 // our purpose. 87 static_assert(__has_trivial_copy(Dest), 88 "non-trivially-copyable bit_cast is undefined"); 89 static_assert(__has_trivial_copy(Source), 90 "non-trivially-copyable bit_cast is undefined"); 91#else 92 // Do nothing, let the bots handle it. 93#endif 94 95 Dest dest; 96 memcpy(&dest, &source, sizeof(dest)); 97 return dest; 98} 99 100#endif // BASE_BIT_CAST_H_ 101