• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "tensorflow/core/framework/bfloat16.h"
17 
18 namespace tensorflow {
19 
FloatToBFloat16(const float * src,bfloat16 * dst,int64 size)20 void FloatToBFloat16(const float* src, bfloat16* dst, int64 size) {
21   const uint16_t* p = reinterpret_cast<const uint16_t*>(src);
22   uint16_t* q = reinterpret_cast<uint16_t*>(dst);
23 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
24   for (; size != 0; p += 2, q++, size--) {
25     *q = p[0];
26   }
27 #else
28   for (; size != 0; p += 2, q++, size--) {
29     *q = p[1];
30   }
31 #endif
32 }
33 
BFloat16ToFloat(const bfloat16 * src,float * dst,int64 size)34 void BFloat16ToFloat(const bfloat16* src, float* dst, int64 size) {
35   const uint16_t* p = reinterpret_cast<const uint16_t*>(src);
36   uint16_t* q = reinterpret_cast<uint16_t*>(dst);
37 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
38   for (; size != 0; p++, q += 2, size--) {
39     q[0] = *p;
40     q[1] = 0;
41   }
42 #else
43   for (; size != 0; p++, q += 2, size--) {
44     q[0] = 0;
45     q[1] = *p;
46   }
47 #endif
48 }
49 
50 }  // end namespace tensorflow
51