1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_CORE_FRAMEWORK_NUMERIC_TYPES_H_ 17 #define TENSORFLOW_CORE_FRAMEWORK_NUMERIC_TYPES_H_ 18 19 #include <complex> 20 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 21 // Disable clang-format to prevent 'FixedPoint' header from being included 22 // before 'Tensor' header on which it depends. 23 // clang-format off 24 #include "third_party/eigen3/unsupported/Eigen/CXX11/FixedPoint" 25 // clang-format on 26 27 #include "tensorflow/core/platform/types.h" 28 29 namespace tensorflow { 30 31 // Single precision complex. 32 typedef std::complex<float> complex64; 33 // Double precision complex. 34 typedef std::complex<double> complex128; 35 36 // We use Eigen's QInt implementations for our quantized int types. 37 typedef Eigen::QInt8 qint8; 38 typedef Eigen::QUInt8 quint8; 39 typedef Eigen::QInt32 qint32; 40 typedef Eigen::QInt16 qint16; 41 typedef Eigen::QUInt16 quint16; 42 43 } // namespace tensorflow 44 FloatToBFloat16(float float_val)45static inline tensorflow::bfloat16 FloatToBFloat16(float float_val) { 46 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 47 return *reinterpret_cast<tensorflow::bfloat16*>( 48 reinterpret_cast<uint16_t*>(&float_val)); 49 #else 50 return *reinterpret_cast<tensorflow::bfloat16*>( 51 &(reinterpret_cast<uint16_t*>(&float_val)[1])); 52 #endif 53 } 54 55 namespace Eigen { 56 template <> 57 struct NumTraits<tensorflow::tstring> : GenericNumTraits<tensorflow::tstring> { 58 enum { 59 RequireInitialization = 1, 60 ReadCost = HugeCost, 61 AddCost = HugeCost, 62 MulCost = HugeCost 63 }; 64 65 static inline int digits10() { return 0; } 66 67 private: 68 static inline tensorflow::tstring epsilon(); 69 static inline tensorflow::tstring dummy_precision(); 70 static inline tensorflow::tstring lowest(); 71 static inline tensorflow::tstring highest(); 72 static inline tensorflow::tstring infinity(); 73 static inline tensorflow::tstring quiet_NaN(); 74 }; 75 76 } // namespace Eigen 77 78 #if defined(_MSC_VER) && !defined(__clang__) 79 namespace std { 80 template <> 81 struct hash<Eigen::half> { 82 std::size_t operator()(const Eigen::half& a) const { 83 return static_cast<std::size_t>(a.x); 84 } 85 }; 86 87 template <> 88 struct hash<Eigen::bfloat16> { 89 std::size_t operator()(const Eigen::bfloat16& a) const { 90 return hash<float>()(static_cast<float>(a)); 91 } 92 }; 93 } // namespace std 94 #endif // _MSC_VER 95 96 #endif // TENSORFLOW_CORE_FRAMEWORK_NUMERIC_TYPES_H_ 97