• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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_UTILS_BASE_CASTS_H_
18 #define LIBTEXTCLASSIFIER_UTILS_BASE_CASTS_H_
19 
20 #include <string.h>  // for memcpy
21 
22 namespace libtextclassifier3 {
23 
24 // bit_cast<Dest, Source> is a template function that implements the equivalent
25 // of "*reinterpret_cast<Dest*>(&source)".  We need this in very low-level
26 // functions like fast math support.
27 //
28 //   float f = 3.14159265358979;
29 //   int i = bit_cast<int32>(f);
30 //   // i = 0x40490fdb
31 //
32 // The classical address-casting method is:
33 //
34 //   // WRONG
35 //   float f = 3.14159265358979;            // WRONG
36 //   int i = * reinterpret_cast<int*>(&f);  // WRONG
37 //
38 // The address-casting method actually produces undefined behavior
39 // according to ISO C++ specification section 3.10 -15 -.  Roughly, this
40 // section says: if an object in memory has one type, and a program
41 // accesses it with a different type, then the result is undefined
42 // behavior for most values of "different type".
43 //
44 // This is true for any cast syntax, either *(int*)&f or
45 // *reinterpret_cast<int*>(&f).  And it is particularly true for
46 // conversions between integral lvalues and floating-point lvalues.
47 //
48 // The purpose of 3.10 -15- is to allow optimizing compilers to assume
49 // that expressions with different types refer to different memory.  gcc
50 // 4.0.1 has an optimizer that takes advantage of this.  So a
51 // non-conforming program quietly produces wildly incorrect output.
52 //
53 // The problem is not the use of reinterpret_cast.  The problem is type
54 // punning: holding an object in memory of one type and reading its bits
55 // back using a different type.
56 //
57 // The C++ standard is more subtle and complex than this, but that
58 // is the basic idea.
59 //
60 // Anyways ...
61 //
62 // bit_cast<> calls memcpy() which is blessed by the standard, especially by the
63 // example in section 3.9 .  Also, of course, bit_cast<> wraps up the nasty
64 // logic in one place.
65 //
66 // Fortunately memcpy() is very fast.  In optimized mode, with a
67 // constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline
68 // code with the minimal amount of data movement.  On a 32-bit system,
69 // memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8)
70 // compiles to two loads and two stores.
71 //
72 // Mike Chastain tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc
73 // 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 template <class Dest, class Source>
bit_cast(const Source & source)82 inline Dest bit_cast(const Source &source) {
83   static_assert(sizeof(Dest) == sizeof(Source), "Sizes do not match");
84 
85   Dest dest;
86   memcpy(&dest, &source, sizeof(dest));
87   return dest;
88 }
89 
90 }  // namespace libtextclassifier3
91 
92 #endif  // LIBTEXTCLASSIFIER_UTILS_BASE_CASTS_H_
93