1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#ifndef LIBTEXTCLASSIFIER_UTIL_BASE_CASTS_H_ 18#define LIBTEXTCLASSIFIER_UTIL_BASE_CASTS_H_ 19 20#include <string.h> // for memcpy 21 22namespace libtextclassifier { 23 24// lang_id_bit_cast<Dest,Source> is a template function that implements the 25// equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in 26// very low-level functions like the protobuf library and fast math 27// support. 28// 29// float f = 3.14159265358979; 30// int i = lang_id_bit_cast<int32>(f); 31// // i = 0x40490fdb 32// 33// The classical address-casting method is: 34// 35// // WRONG 36// float f = 3.14159265358979; // WRONG 37// int i = * reinterpret_cast<int*>(&f); // WRONG 38// 39// The address-casting method actually produces undefined behavior 40// according to ISO C++ specification section 3.10 -15 -. Roughly, this 41// section says: if an object in memory has one type, and a program 42// accesses it with a different type, then the result is undefined 43// behavior for most values of "different type". 44// 45// This is true for any cast syntax, either *(int*)&f or 46// *reinterpret_cast<int*>(&f). And it is particularly true for 47// conversions between integral lvalues and floating-point lvalues. 48// 49// The purpose of 3.10 -15- is to allow optimizing compilers to assume 50// that expressions with different types refer to different memory. gcc 51// 4.0.1 has an optimizer that takes advantage of this. So a 52// non-conforming program quietly produces wildly incorrect output. 53// 54// The problem is not the use of reinterpret_cast. The problem is type 55// punning: holding an object in memory of one type and reading its bits 56// back using a different type. 57// 58// The C++ standard is more subtle and complex than this, but that 59// is the basic idea. 60// 61// Anyways ... 62// 63// lang_id_bit_cast<> calls memcpy() which is blessed by the standard, 64// especially by the example in section 3.9 . Also, of course, 65// lang_id_bit_cast<> wraps up the nasty logic in one place. 66// 67// Fortunately memcpy() is very fast. In optimized mode, with a 68// constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline 69// code with the minimal amount of data movement. On a 32-bit system, 70// memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8) 71// compiles to two loads and two stores. 72// 73// I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1. 74// 75// WARNING: if Dest or Source is a non-POD type, the result of the memcpy 76// is likely to surprise you. 77// 78// Props to Bill Gibbons for the compile time assertion technique and 79// Art Komninos and Igor Tandetnik for the msvc experiments. 80// 81// -- mec 2005-10-17 82 83template <class Dest, class Source> 84inline Dest bit_cast(const Source &source) { 85 static_assert(sizeof(Dest) == sizeof(Source), "Sizes do not match"); 86 87 Dest dest; 88 memcpy(&dest, &source, sizeof(dest)); 89 return dest; 90} 91 92} // namespace libtextclassifier 93 94#endif // LIBTEXTCLASSIFIER_UTIL_BASE_CASTS_H_ 95