From 70906ad75321cf313dbb37e14ab7cf7cddd4c6b7 Mon Sep 17 00:00:00 2001 From: Jose Luis Blanco-Claraco Date: Thu, 31 Aug 2017 14:35:10 +0200 Subject: [PATCH] import bit_cast<> --- libs/base/include/mrpt/utils/bit_cast.h | 79 +++++++++++++++++++++++++ packaging/debian/copyright | 5 ++ 2 files changed, 84 insertions(+) create mode 100644 libs/base/include/mrpt/utils/bit_cast.h diff --git a/libs/base/include/mrpt/utils/bit_cast.h b/libs/base/include/mrpt/utils/bit_cast.h new file mode 100644 index 0000000000..0c41305c04 --- /dev/null +++ b/libs/base/include/mrpt/utils/bit_cast.h @@ -0,0 +1,79 @@ +/* +------------------------------------------------------------------------+ + | Mobile Robot Programming Toolkit (MRPT) | + | http://www.mrpt.org/ | + | | + | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file | + | See: http://www.mrpt.org/Authors - All rights reserved. | + | Released under BSD License. See details in http://www.mrpt.org/License | + +------------------------------------------------------------------------+ */ + +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +#ifndef BASE_BIT_CAST_H_ +#define BASE_BIT_CAST_H_ +#include +#include +#include "base/compiler_specific.h" +#include "base/template_util.h" +#include "build/build_config.h" +// bit_cast is a template function that implements the equivalent +// of "*reinterpret_cast(&source)". We need this in very low-level +// functions like the protobuf library and fast math support. +// +// float f = 3.14159265358979; +// int i = bit_cast(f); +// // i = 0x40490fdb +// +// The classical address-casting method is: +// +// // WRONG +// float f = 3.14159265358979; // WRONG +// int i = * reinterpret_cast(&f); // WRONG +// +// The address-casting method actually produces undefined behavior according to +// the ISO C++98 specification, section 3.10 ("basic.lval"), paragraph 15. +// (This did not substantially change in C++11.) Roughly, this section says: if +// an object in memory has one type, and a program accesses it with a different +// type, then the result is undefined behavior for most values of "different +// type". +// +// This is true for any cast syntax, either *(int*)&f or +// *reinterpret_cast(&f). And it is particularly true for conversions +// between integral lvalues and floating-point lvalues. +// +// The purpose of this paragraph is to allow optimizing compilers to assume that +// expressions with different types refer to different memory. Compilers are +// known to take advantage of this. So a non-conforming program quietly +// produces wildly incorrect output. +// +// The problem is not the use of reinterpret_cast. The problem is type punning: +// holding an object in memory of one type and reading its bits back using a +// different type. +// +// The C++ standard is more subtle and complex than this, but that is the basic +// idea. +// +// Anyways ... +// +// bit_cast<> calls memcpy() which is blessed by the standard, especially by the +// example in section 3.9 . Also, of course, bit_cast<> wraps up the nasty +// logic in one place. +// +// Fortunately memcpy() is very fast. In optimized mode, compilers replace +// calls to memcpy() with inline object code when the size argument is a +// compile-time constant. On a 32-bit system, memcpy(d,s,4) compiles to one +// load and one store, and memcpy(d,s,8) compiles to two loads and two stores. +template +inline Dest bit_cast(const Source& source) { + static_assert(sizeof(Dest) == sizeof(Source), + "bit_cast requires source and destination to be the same size"); + static_assert(base::is_trivially_copyable::value, + "bit_cast requires the destination type to be copyable"); + static_assert(base::is_trivially_copyable::value, + "bit_cast requires the source type to be copyable"); + Dest dest; + memcpy(&dest, &source, sizeof(dest)); + return dest; +} +#endif // BASE_BIT_CAST_H_ \ No newline at end of file diff --git a/packaging/debian/copyright b/packaging/debian/copyright index c4642d2bd3..47204f5ccd 100644 --- a/packaging/debian/copyright +++ b/packaging/debian/copyright @@ -756,3 +756,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * rplidar: Low-cost LiDAR driver Copyright (c) 2014 - 2016 Shanghai Slamtec Co., Ltd. 2-clause BSD License + + * libs/base/include/mrpt/utils/bit_cast.h + Copyright 2016 The Chromium Authors. All rights reserved. + Use of this source code is governed by a BSD-style license that can be + found in the LICENSE file.