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