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(const std::string &bitString, const std::string &exponentBit, const std::string &mantissaBit,
36 : const uint32_t exponentBitNum) const;
37 :
38 : protected:
39 : uint8_t val;
40 : };
41 :
42 : class Fp8E5M2 : public FpaEbMc {
43 : public:
44 4 : explicit Fp8E5M2(uint8_t v = 0) : FpaEbMc(v) {}
45 : float GetValue() const;
46 : };
47 :
48 : class Fp8E4M3 : public FpaEbMc {
49 : public:
50 3 : explicit Fp8E4M3(uint8_t v = 0) : FpaEbMc(v) {}
51 : float GetValue() const;
52 : };
53 :
54 : class Fp8E8M0 {
55 : public:
56 2 : explicit Fp8E8M0(uint8_t v = 0) : val(v) {}
57 : float GetValue() const;
58 :
59 : private:
60 : uint8_t val;
61 : };
62 :
63 : }; // namespace
64 :
65 : #endif
|