| Flutter Impeller |

half.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef FLUTTER_IMPELLER_GEOMETRY_HALF_H_
6 #define FLUTTER_IMPELLER_GEOMETRY_HALF_H_
7
8 #include <cstdint>
9
10 #include "flutter/fml/build_config.h"
11
12 #include "impeller/geometry/color.h"
13 #include "impeller/geometry/point.h"
14 #include "impeller/geometry/scalar.h"
15 #include "impeller/geometry/vector.h"
16
17 // NOLINTBEGIN(google-explicit-constructor)
18
19 #if defined(FML_OS_MACOSX) || defined(FML_OS_IOS) || \
20 defined(FML_OS_IOS_SIMULATOR)
21 using InternalHalf = _Float16;
22 #else
23 using InternalHalf = uint16_t;
24 #endif
25
26 namespace impeller {
27
28 /// @brief Convert a scalar to a half precision float.
29 ///
30 /// See also: https://clang.llvm.org/docs/LanguageExtensions.html
31 /// This is not currently supported on Windows toolchains.
32 inline constexpr InternalHalf ScalarToHalf(Scalar f) {
33 #ifdef FML_OS_WIN
34 return static_cast<InternalHalf>(0);
35 #else
36 return static_cast<InternalHalf>(f);
37 #endif
38 }
39
40 /// @brief A storage only class for half precision floating point.
41 struct Half {
42 InternalHalf x = 0;
43
44 constexpr Half() {}
45
46 constexpr Half(double value) : x(ScalarToHalf(static_cast<Scalar>(value))) {}
47
48 constexpr Half(Scalar value) : x(ScalarToHalf(value)) {}
49
50 constexpr Half(int value) : x(ScalarToHalf(static_cast<Scalar>(value))) {}
51
52 constexpr Half(InternalHalf x) : x(x) {}
53
54 constexpr bool operator==(const Half& v) const { return v.x == x; }
55
56 constexpr bool operator!=(const Half& v) const { return v.x != x; }
57 };
58
59 /// @brief A storage only class for half precision floating point vector 4.
60 struct HalfVector4 {
61 union {
62 struct {
63 InternalHalf x = 0;
64 InternalHalf y = 0;
65 InternalHalf z = 0;
66 InternalHalf w = 0;
67 };
68 InternalHalf e[4];
69 };
70
71 constexpr HalfVector4() {}
72
73 constexpr HalfVector4(const Color& a)
74 : x(ScalarToHalf(a.red)),
75 y(ScalarToHalf(a.green)),
76 z(ScalarToHalf(a.blue)),
77 w(ScalarToHalf(a.alpha)) {}
78
79 constexpr HalfVector4(const Vector4& a)
80 : x(ScalarToHalf(a.x)),
81 y(ScalarToHalf(a.y)),
82 z(ScalarToHalf(a.z)),
83 w(ScalarToHalf(a.w)) {}
84
85 constexpr HalfVector4(InternalHalf x,
86 InternalHalf y,
87 InternalHalf z,
88 InternalHalf w)
89 : x(x), y(y), z(z), w(w) {}
90
91 constexpr bool operator==(const HalfVector4& v) const {
92 return v.x == x && v.y == y && v.z == z && v.w == w;
93 }
94
95 constexpr bool operator!=(const HalfVector4& v) const {
96 return v.x != x || v.y != y || v.z != z || v.w != w;
97 }
98 };
99
100 /// @brief A storage only class for half precision floating point vector 3.
101 struct HalfVector3 {
102 union {
103 struct {
104 InternalHalf x = 0;
105 InternalHalf y = 0;
106 InternalHalf z = 0;
107 };
108 InternalHalf e[3];
109 };
110
111 constexpr HalfVector3() {}
112
113 constexpr HalfVector3(const Vector3& a)
114 : x(ScalarToHalf(a.x)), y(ScalarToHalf(a.y)), z(ScalarToHalf(a.z)) {}
115
116 constexpr HalfVector3(InternalHalf x, InternalHalf y, InternalHalf z)
117 : x(x), y(y), z(z) {}
118
119 constexpr bool operator==(const HalfVector3& v) const {
120 return v.x == x && v.y == y && v.z == z;
121 }
122
123 constexpr bool operator!=(const HalfVector3& v) const {
124 return v.x != x || v.y != y || v.z != z;
125 }
126 };
127
128 /// @brief A storage only class for half precision floating point vector 2.
129 struct HalfVector2 {
130 union {
131 struct {
132 InternalHalf x = 0;
133 InternalHalf y = 0;
134 };
135 InternalHalf e[2];
136 };
137
138 constexpr HalfVector2() {}
139
140 constexpr HalfVector2(const Vector2& a)
141 : x(ScalarToHalf(a.x)), y(ScalarToHalf(a.y)) {}
142
143 constexpr HalfVector2(InternalHalf x, InternalHalf y) : x(x), y(y){};
144
145 constexpr bool operator==(const HalfVector2& v) const {
146 return v.x == x && v.y == y;
147 }
148
149 constexpr bool operator!=(const HalfVector2& v) const {
150 return v.x != x || v.y != y;
151 }
152 };
153
154 static_assert(sizeof(Half) == sizeof(uint16_t));
155 static_assert(sizeof(HalfVector2) == 2 * sizeof(Half));
156 static_assert(sizeof(HalfVector3) == 3 * sizeof(Half));
157 static_assert(sizeof(HalfVector4) == 4 * sizeof(Half));
158
159 } // namespace impeller
160
161 namespace std {
162
163 inline std::ostream& operator<<(std::ostream& out, const impeller::Half& p) {
164 out << "(" << static_cast<impeller::Scalar>(p.x) << ")";
165 return out;
166 }
167
168 inline std::ostream& operator<<(std::ostream& out,
169 const impeller::HalfVector2& p) {
170 out << "(" << static_cast<impeller::Scalar>(p.x) << ", "
171 << static_cast<impeller::Scalar>(p.y) << ")";
172 return out;
173 }
174
175 inline std::ostream& operator<<(std::ostream& out,
176 const impeller::HalfVector3& p) {
177 out << "(" << static_cast<impeller::Scalar>(p.x) << ", "
178 << static_cast<impeller::Scalar>(p.y) << ", "
179 << static_cast<impeller::Scalar>(p.z) << ")";
180 return out;
181 }
182
183 inline std::ostream& operator<<(std::ostream& out,
184 const impeller::HalfVector4& p) {
185 out << "(" << static_cast<impeller::Scalar>(p.x) << ", "
186 << static_cast<impeller::Scalar>(p.y) << ", "
187 << static_cast<impeller::Scalar>(p.z) << ", "
188 << static_cast<impeller::Scalar>(p.w) << ")";
189 return out;
190 }
191
192 // NOLINTEND(google-explicit-constructor)
193
194 } // namespace std
195
196 #endif // FLUTTER_IMPELLER_GEOMETRY_HALF_H_
color.h
value
int32_t value
Definition: dl_golden_unittests.cc:459
InternalHalf
uint16_t InternalHalf
Definition: half.h:23
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::ScalarToHalf
constexpr InternalHalf ScalarToHalf(Scalar f)
Convert a scalar to a half precision float.
Definition: half.h:32
std
Definition: comparable.h:95
std::operator<<
std::ostream & operator<<(std::ostream &out, const impeller::Arc &a)
Definition: arc.h:141
point.h
scalar.h
impeller::Color
Definition: color.h:124
impeller::Half
A storage only class for half precision floating point.
Definition: half.h:41
impeller::Half::operator==
constexpr bool operator==(const Half &v) const
Definition: half.h:54
impeller::Half::Half
constexpr Half(Scalar value)
Definition: half.h:48
impeller::Half::Half
constexpr Half()
Definition: half.h:44
impeller::Half::Half
constexpr Half(InternalHalf x)
Definition: half.h:52
impeller::Half::x
InternalHalf x
Definition: half.h:42
impeller::Half::operator!=
constexpr bool operator!=(const Half &v) const
Definition: half.h:56
impeller::Half::Half
constexpr Half(double value)
Definition: half.h:46
impeller::Half::Half
constexpr Half(int value)
Definition: half.h:50
impeller::HalfVector2
A storage only class for half precision floating point vector 2.
Definition: half.h:129
impeller::HalfVector2::x
InternalHalf x
Definition: half.h:132
impeller::HalfVector2::operator==
constexpr bool operator==(const HalfVector2 &v) const
Definition: half.h:145
impeller::HalfVector2::HalfVector2
constexpr HalfVector2(const Vector2 &a)
Definition: half.h:140
impeller::HalfVector2::HalfVector2
constexpr HalfVector2(InternalHalf x, InternalHalf y)
Definition: half.h:143
impeller::HalfVector2::HalfVector2
constexpr HalfVector2()
Definition: half.h:138
impeller::HalfVector2::e
InternalHalf e[2]
Definition: half.h:135
impeller::HalfVector2::operator!=
constexpr bool operator!=(const HalfVector2 &v) const
Definition: half.h:149
impeller::HalfVector2::y
InternalHalf y
Definition: half.h:133
impeller::HalfVector3
A storage only class for half precision floating point vector 3.
Definition: half.h:101
impeller::HalfVector3::e
InternalHalf e[3]
Definition: half.h:108
impeller::HalfVector3::HalfVector3
constexpr HalfVector3(const Vector3 &a)
Definition: half.h:113
impeller::HalfVector3::HalfVector3
constexpr HalfVector3(InternalHalf x, InternalHalf y, InternalHalf z)
Definition: half.h:116
impeller::HalfVector3::x
InternalHalf x
Definition: half.h:104
impeller::HalfVector3::operator!=
constexpr bool operator!=(const HalfVector3 &v) const
Definition: half.h:123
impeller::HalfVector3::HalfVector3
constexpr HalfVector3()
Definition: half.h:111
impeller::HalfVector3::operator==
constexpr bool operator==(const HalfVector3 &v) const
Definition: half.h:119
impeller::HalfVector3::z
InternalHalf z
Definition: half.h:106
impeller::HalfVector3::y
InternalHalf y
Definition: half.h:105
impeller::HalfVector4
A storage only class for half precision floating point vector 4.
Definition: half.h:60
impeller::HalfVector4::e
InternalHalf e[4]
Definition: half.h:68
impeller::HalfVector4::operator==
constexpr bool operator==(const HalfVector4 &v) const
Definition: half.h:91
impeller::HalfVector4::HalfVector4
constexpr HalfVector4(const Color &a)
Definition: half.h:73
impeller::HalfVector4::HalfVector4
constexpr HalfVector4(const Vector4 &a)
Definition: half.h:79
impeller::HalfVector4::operator!=
constexpr bool operator!=(const HalfVector4 &v) const
Definition: half.h:95
impeller::HalfVector4::x
InternalHalf x
Definition: half.h:63
impeller::HalfVector4::HalfVector4
constexpr HalfVector4(InternalHalf x, InternalHalf y, InternalHalf z, InternalHalf w)
Definition: half.h:85
impeller::HalfVector4::w
InternalHalf w
Definition: half.h:66
impeller::HalfVector4::y
InternalHalf y
Definition: half.h:64
impeller::HalfVector4::z
InternalHalf z
Definition: half.h:65
impeller::HalfVector4::HalfVector4
constexpr HalfVector4()
Definition: half.h:71
impeller::TPoint< Scalar >
impeller::Vector3
Definition: vector.h:20
impeller::Vector4
Definition: vector.h:232
vector.h