Coverage for /opt/cloud/slavespace/usr1/096471637100f3de0fcfc01072822a80/ut/utils/show_kernel_debug_data/show_kernel_debug_data/data_converter.py: 100%
18 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-27 14:38 +0800
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-27 14:38 +0800
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3# ----------------------------------------------------------------------------------------------------------
4# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6# CANN Open Software License Agreement Version 2.0 (the "License").
7# Please refer to the License for details. You may not use this file except in compliance with the License.
8# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10# See LICENSE in the root of the software repository for the full text of the License.
11# ----------------------------------------------------------------------------------------------------------
13EXPONENT_BIAS_BF16 = 127
16def decode_bfloat16(num : int) -> float:
17 if num == 0:
18 return 0
19 elif num == 0xFF80:
20 return float("-inf")
21 elif num == 0x7F80:
22 return float("inf")
23 if num >= 0x8000:
24 signal_val = -1
25 else:
26 signal_val = 1
27 exponent_val = (num % 0x8000 // 0x0080)
28 mantissa_val = (num % 0x8000 % 0x0080) / 0x0080
29 if exponent_val == 0xFF and mantissa_val > 0:
30 return float("nan")
31 if exponent_val == 0:
32 return signal_val * 2 ** (-EXPONENT_BIAS_BF16 + 1) * mantissa_val
33 else:
34 return signal_val * 2 ** (exponent_val - EXPONENT_BIAS_BF16) * (1 + mantissa_val)