Poplar and PopLibs
IeeeHalf.hpp
1// Copyright (c) 2018 Graphcore Ltd. All rights reserved.
2
3/*********************************************************************
4 * Half-precision floating-point abstraction for Colossus
5 * Instruction Set Simulator
6 *
7 * Notes:
8 * Most operations are performed by:
9 * 1 Converting the half-precision value to single-precision (float)
10 * 2 Performing the operation at single-precision accuracy
11 * 3 Converting the result back to half-precision
12 *
13 *********************************************************************/
14
15#ifndef poplar_IeeeHalf_hpp
16#define poplar_IeeeHalf_hpp
17
18#include <stdint.h>
19
20namespace poplar {
21
27class IeeeHalf {
28
29public:
33 IeeeHalf() = default;
34
38 IeeeHalf(float value);
39
44 static IeeeHalf fromBits(uint16_t bitPattern);
45
49 operator float() const;
50
55 uint16_t bit16() const { return ihalf; }
56
62 uint32_t bitz32() const { return (uint32_t)ihalf; }
63
64 // Destructive operators
65 IeeeHalf &operator=(const IeeeHalf &other) = default;
66 IeeeHalf &operator+=(float other);
67 IeeeHalf &operator-=(float other);
68 IeeeHalf &operator*=(float other);
69 IeeeHalf &operator/=(float other);
70
71 // Comparison operators.
72 bool operator<(float other) const;
73 bool operator>(float other) const;
74 bool operator<=(float other) const;
75 bool operator>=(float other) const;
76 bool operator==(IeeeHalf other) const;
77 bool operator!=(IeeeHalf other) const;
78
79 // These are necessary to avoid ambiguous comparisons when doing
80 // `IeeeHalf == float`, because we could either convert the first parameter
81 // to a float, or the second to an IeeeHalf. To avoid the ambiguity we provide
82 // functions that require no conversion (internally they convert *this
83 // to a float and then compare). The same is true for double.
84 bool operator==(float other) const;
85 bool operator!=(float other) const;
86
87 // Unary negation.
88 IeeeHalf operator-() const;
89
90 // Basic arithmetic operators.
91 IeeeHalf operator+(float other);
92 IeeeHalf operator-(float other);
93 IeeeHalf operator*(float other);
94 IeeeHalf operator/(float other);
95
96 bool isqNaN() const;
97 bool issNaN() const;
98 bool isNaN() const;
99 bool isInf() const;
100 bool isNorm() const;
101 bool isZero() const;
102
103private:
104 uint16_t ihalf;
105};
106
107} // namespace poplar
108
109#endif // poplar_IeeeHalf_hpp
A IeeeHalf.
Definition: IeeeHalf.hpp:27
IeeeHalf()=default
Uninitialised.
uint16_t bit16() const
Obtain half-precision bit-pattern.
Definition: IeeeHalf.hpp:55
IeeeHalf(float value)
Initialise from a single-precision fp value.
uint32_t bitz32() const
Obtain half-precision bit-pattern.
Definition: IeeeHalf.hpp:62
static IeeeHalf fromBits(uint16_t bitPattern)
Initialise from a raw 16-bit pattern (conforming to IEEE 754-2008 binary16 format)
Poplar classes and functions.
Definition: ArrayRef.hpp:14