Poplar and PopLibs
Quarter.hpp
1// Copyright (c) 2022 Graphcore Ltd. All rights reserved.
2
3/* Quarter precision floating-point type definitions for IPU host software.
4 *
5 * The Quarter type is a single byte floating-point format internal to poplar.
6 * The format is fully defined at runtime using an independently allocated
7 * metadata variable. Typically a single metadata variable can be used to define
8 * the format for an arbitrary number of elements of type Quarter. The metadata
9 * is of type QuarterMetadata and it includes the following information:
10 * - An enumeration that defines the number of bits of the Quarter data that
11 * is used for sign, mantissa and exponent.
12 * - A signed scale factor.
13 *
14 * The scale factor needs to be applied appropriately when converting from
15 * Quarter to float or vice-versa. In the following code, `d` is of type
16 * float, `q` is of type Quarter, and QuarterMetadata uses scale factor `s`.
17 * d = q * pow(2, s)
18 * q = d * pow(2, -s)
19 */
20
21#ifndef poplar_Quarter_hpp_
22#define poplar_Quarter_hpp_
23
24#include <iosfwd>
25
26namespace poplar {
27
38public:
39 enum class Format { F143, F152 };
40
41 QuarterMetadata() = default;
42
43 explicit QuarterMetadata(const Format fmt, const signed char scale)
44 : value(pack(fmt, scale)){};
45
46 explicit QuarterMetadata(unsigned char value) : value(value){};
47
48 QuarterMetadata(const QuarterMetadata &other) : value(other.value){};
49
50 QuarterMetadata &operator=(const QuarterMetadata &other);
51
52 bool operator==(const QuarterMetadata other) const;
53
54 bool operator!=(const QuarterMetadata other) const;
55
56 // Get the binary representation of the underlying value
57 unsigned char getBinary() const { return value; };
58
59 unsigned char &data() { return value; };
60
61 const unsigned char &data() const { return value; };
62
63 Format getFormat() const;
64
65 signed char getScale() const;
66
67 std::string toString() const;
68
69private:
70 unsigned char pack(const Format fmt, const signed char scale) const;
71
72 unsigned char value;
73};
74
75std::ostream &operator<<(std::ostream &os, const QuarterMetadata &md);
76
77} // namespace poplar
78
79#endif // poplar_Quarter_hpp_
Quarter metadata type.
Definition: Quarter.hpp:37
Poplar classes and functions.
Definition: ArrayRef.hpp:14
std::ostream & operator<<(std::ostream &os, const DebugNameAndId &dnai)
Display the path name of the DebugNameAndId.