/root/doris/contrib/faiss/faiss/utils/bf16.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * |
4 | | * This source code is licensed under the MIT license found in the |
5 | | * LICENSE file in the root directory of this source tree. |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <cstdint> |
11 | | |
12 | | namespace faiss { |
13 | | |
14 | | namespace { |
15 | | |
16 | | union fp32_bits { |
17 | | uint32_t as_u32; |
18 | | float as_f32; |
19 | | }; |
20 | | |
21 | | } // namespace |
22 | | |
23 | 0 | inline uint16_t encode_bf16(const float f) { |
24 | | // Round off |
25 | 0 | fp32_bits fp; |
26 | 0 | fp.as_f32 = f; |
27 | 0 | return static_cast<uint16_t>((fp.as_u32 + 0x8000) >> 16); |
28 | 0 | } |
29 | | |
30 | 0 | inline float decode_bf16(const uint16_t v) { |
31 | 0 | fp32_bits fp; |
32 | 0 | fp.as_u32 = (uint32_t(v) << 16); |
33 | 0 | return fp.as_f32; |
34 | 0 | } |
35 | | |
36 | | } // namespace faiss |