| 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
57 /// @brief A storage only class for half precision floating point vector 4.
58 struct HalfVector4 {
59 union {
60 struct {
61 InternalHalf x = 0;
62 InternalHalf y = 0;
63 InternalHalf z = 0;
64 InternalHalf w = 0;
65 };
66 InternalHalf e[4];
67 };
68
69 constexpr HalfVector4() {}
70
71 constexpr HalfVector4(const Color& a)
72 : x(ScalarToHalf(a.red)),
73 y(ScalarToHalf(a.green)),
74 z(ScalarToHalf(a.blue)),
75 w(ScalarToHalf(a.alpha)) {}
76
77 constexpr HalfVector4(const Vector4& a)
78 : x(ScalarToHalf(a.x)),
79 y(ScalarToHalf(a.y)),
80 z(ScalarToHalf(a.z)),
81 w(ScalarToHalf(a.w)) {}
82
83 constexpr HalfVector4(InternalHalf x,
84 InternalHalf y,
85 InternalHalf z,
86 InternalHalf w)
87 : x(x), y(y), z(z), w(w) {}
88
89 constexpr bool operator==(const HalfVector4& v) const {
90 return v.x == x && v.y == y && v.z == z && v.w == w;
91 }
92
93 constexpr bool operator!=(const HalfVector4& v) const {
94 return v.x != x || v.y != y || v.z != z || v.w != w;
95 }
96 };
97
98 /// @brief A storage only class for half precision floating point vector 3.
99 struct HalfVector3 {
100 union {
101 struct {
102 InternalHalf x = 0;
103 InternalHalf y = 0;
104 InternalHalf z = 0;
105 };
106 InternalHalf e[3];
107 };
108
109 constexpr HalfVector3() {}
110
111 constexpr HalfVector3(const Vector3& a)
112 : x(ScalarToHalf(a.x)), y(ScalarToHalf(a.y)), z(ScalarToHalf(a.z)) {}
113
114 constexpr HalfVector3(InternalHalf x, InternalHalf y, InternalHalf z)
115 : x(x), y(y), z(z) {}
116
117 constexpr bool operator==(const HalfVector3& v) const {
118 return v.x == x && v.y == y && v.z == z;
119 }
120
121 constexpr bool operator!=(const HalfVector3& v) const {
122 return v.x != x || v.y != y || v.z != z;
123 }
124 };
125
126 /// @brief A storage only class for half precision floating point vector 2.
127 struct HalfVector2 {
128 union {
129 struct {
130 InternalHalf x = 0;
131 InternalHalf y = 0;
132 };
133 InternalHalf e[2];
134 };
135
136 constexpr HalfVector2() {}
137
138 constexpr HalfVector2(const Vector2& a)
139 : x(ScalarToHalf(a.x)), y(ScalarToHalf(a.y)) {}
140
141 constexpr HalfVector2(InternalHalf x, InternalHalf y) : x(x), y(y) {};
142
143 constexpr bool operator==(const HalfVector2& v) const {
144 return v.x == x && v.y == y;
145 }
146
147 constexpr bool operator!=(const HalfVector2& v) const {
148 return v.x != x || v.y != y;
149 }
150 };
151
152 static_assert(sizeof(Half) == sizeof(uint16_t));
153 static_assert(sizeof(HalfVector2) == 2 * sizeof(Half));
154 static_assert(sizeof(HalfVector3) == 3 * sizeof(Half));
155 static_assert(sizeof(HalfVector4) == 4 * sizeof(Half));
156
157 } // namespace impeller
158
159 namespace std {
160
161 inline std::ostream& operator<<(std::ostream& out, const impeller::Half& p) {
162 out << "(" << static_cast<impeller::Scalar>(p.x) << ")";
163 return out;
164 }
165
166 inline std::ostream& operator<<(std::ostream& out,
167 const impeller::HalfVector2& p) {
168 out << "(" << static_cast<impeller::Scalar>(p.x) << ", "
169 << static_cast<impeller::Scalar>(p.y) << ")";
170 return out;
171 }
172
173 inline std::ostream& operator<<(std::ostream& out,
174 const impeller::HalfVector3& p) {
175 out << "(" << static_cast<impeller::Scalar>(p.x) << ", "
176 << static_cast<impeller::Scalar>(p.y) << ", "
177 << static_cast<impeller::Scalar>(p.z) << ")";
178 return out;
179 }
180
181 inline std::ostream& operator<<(std::ostream& out,
182 const impeller::HalfVector4& p) {
183 out << "(" << static_cast<impeller::Scalar>(p.x) << ", "
184 << static_cast<impeller::Scalar>(p.y) << ", "
185 << static_cast<impeller::Scalar>(p.z) << ", "
186 << static_cast<impeller::Scalar>(p.w) << ")";
187 return out;
188 }
189
190 // NOLINTEND(google-explicit-constructor)
191
192 } // namespace std
193
194 #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::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:127
impeller::HalfVector2::x
InternalHalf x
Definition: half.h:130
impeller::HalfVector2::operator==
constexpr bool operator==(const HalfVector2 &v) const
Definition: half.h:143
impeller::HalfVector2::HalfVector2
constexpr HalfVector2(const Vector2 &a)
Definition: half.h:138
impeller::HalfVector2::HalfVector2
constexpr HalfVector2(InternalHalf x, InternalHalf y)
Definition: half.h:141
impeller::HalfVector2::HalfVector2
constexpr HalfVector2()
Definition: half.h:136
impeller::HalfVector2::e
InternalHalf e[2]
Definition: half.h:133
impeller::HalfVector2::operator!=
constexpr bool operator!=(const HalfVector2 &v) const
Definition: half.h:147
impeller::HalfVector2::y
InternalHalf y
Definition: half.h:131
impeller::HalfVector3
A storage only class for half precision floating point vector 3.
Definition: half.h:99
impeller::HalfVector3::e
InternalHalf e[3]
Definition: half.h:106
impeller::HalfVector3::HalfVector3
constexpr HalfVector3(const Vector3 &a)
Definition: half.h:111
impeller::HalfVector3::HalfVector3
constexpr HalfVector3(InternalHalf x, InternalHalf y, InternalHalf z)
Definition: half.h:114
impeller::HalfVector3::x
InternalHalf x
Definition: half.h:102
impeller::HalfVector3::operator!=
constexpr bool operator!=(const HalfVector3 &v) const
Definition: half.h:121
impeller::HalfVector3::HalfVector3
constexpr HalfVector3()
Definition: half.h:109
impeller::HalfVector3::operator==
constexpr bool operator==(const HalfVector3 &v) const
Definition: half.h:117
impeller::HalfVector3::z
InternalHalf z
Definition: half.h:104
impeller::HalfVector3::y
InternalHalf y
Definition: half.h:103
impeller::HalfVector4
A storage only class for half precision floating point vector 4.
Definition: half.h:58
impeller::HalfVector4::e
InternalHalf e[4]
Definition: half.h:66
impeller::HalfVector4::operator==
constexpr bool operator==(const HalfVector4 &v) const
Definition: half.h:89
impeller::HalfVector4::HalfVector4
constexpr HalfVector4(const Color &a)
Definition: half.h:71
impeller::HalfVector4::HalfVector4
constexpr HalfVector4(const Vector4 &a)
Definition: half.h:77
impeller::HalfVector4::operator!=
constexpr bool operator!=(const HalfVector4 &v) const
Definition: half.h:93
impeller::HalfVector4::x
InternalHalf x
Definition: half.h:61
impeller::HalfVector4::HalfVector4
constexpr HalfVector4(InternalHalf x, InternalHalf y, InternalHalf z, InternalHalf w)
Definition: half.h:83
impeller::HalfVector4::w
InternalHalf w
Definition: half.h:64
impeller::HalfVector4::y
InternalHalf y
Definition: half.h:62
impeller::HalfVector4::z
InternalHalf z
Definition: half.h:63
impeller::HalfVector4::HalfVector4
constexpr HalfVector4()
Definition: half.h:69
impeller::TPoint< Scalar >
impeller::Vector3
Definition: vector.h:20
impeller::Vector4
Definition: vector.h:232
vector.h