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