Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef HIFLOAT_H
12 : #define HIFLOAT_H
13 :
14 : #include <cmath>
15 : #include <string>
16 : #include <cstdint>
17 :
18 : namespace Adx {
19 : constexpr int32_t BASE_NUM = 2;
20 :
21 : class HiFloat8 {
22 : public:
23 4 : explicit HiFloat8(uint8_t v = 0) : val(v) {}
24 : int32_t GetExponent(const std::string& binary) const;
25 : float GetMantissaField(const std::string& binary) const;
26 : float GetValue() const;
27 :
28 : private:
29 : uint8_t val;
30 : };
31 :
32 : class FpaEbMc {
33 : public:
34 7 : explicit FpaEbMc(uint8_t v = 0) : val(v) {}
35 : float Decode(
36 : const std::string& bitString, const std::string& exponentBit, const std::string& mantissaBit,
37 : const uint32_t exponentBitNum) const;
38 :
39 : protected:
40 : uint8_t val;
41 : };
42 :
43 : class Fp8E5M2 : public FpaEbMc {
44 : public:
45 4 : explicit Fp8E5M2(uint8_t v = 0) : FpaEbMc(v) {}
46 : float GetValue() const;
47 : };
48 :
49 : class Fp8E4M3 : public FpaEbMc {
50 : public:
51 3 : explicit Fp8E4M3(uint8_t v = 0) : FpaEbMc(v) {}
52 : float GetValue() const;
53 : };
54 :
55 : class Fp8E8M0 {
56 : public:
57 2 : explicit Fp8E8M0(uint8_t v = 0) : val(v) {}
58 : float GetValue() const;
59 :
60 : private:
61 : uint8_t val;
62 : };
63 :
64 : }; // namespace Adx
65 :
66 : #endif
|