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 
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 {
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 {
65  };
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 
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 {
105  };
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 
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 {
132  };
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_
int32_t value
uint16_t InternalHalf
Definition: half.h:23
float Scalar
Definition: scalar.h:19
constexpr InternalHalf ScalarToHalf(Scalar f)
Convert a scalar to a half precision float.
Definition: half.h:32
Definition: comparable.h:95
std::ostream & operator<<(std::ostream &out, const impeller::Arc &a)
Definition: arc.h:141
A storage only class for half precision floating point.
Definition: half.h:41
constexpr bool operator==(const Half &v) const
Definition: half.h:54
constexpr Half(Scalar value)
Definition: half.h:48
constexpr Half()
Definition: half.h:44
constexpr Half(InternalHalf x)
Definition: half.h:52
InternalHalf x
Definition: half.h:42
constexpr Half(double value)
Definition: half.h:46
constexpr Half(int value)
Definition: half.h:50
A storage only class for half precision floating point vector 2.
Definition: half.h:127
InternalHalf x
Definition: half.h:130
constexpr bool operator==(const HalfVector2 &v) const
Definition: half.h:143
constexpr HalfVector2(const Vector2 &a)
Definition: half.h:138
constexpr HalfVector2(InternalHalf x, InternalHalf y)
Definition: half.h:141
constexpr HalfVector2()
Definition: half.h:136
InternalHalf e[2]
Definition: half.h:133
constexpr bool operator!=(const HalfVector2 &v) const
Definition: half.h:147
InternalHalf y
Definition: half.h:131
A storage only class for half precision floating point vector 3.
Definition: half.h:99
InternalHalf e[3]
Definition: half.h:106
constexpr HalfVector3(const Vector3 &a)
Definition: half.h:111
constexpr HalfVector3(InternalHalf x, InternalHalf y, InternalHalf z)
Definition: half.h:114
InternalHalf x
Definition: half.h:102
constexpr bool operator!=(const HalfVector3 &v) const
Definition: half.h:121
constexpr HalfVector3()
Definition: half.h:109
constexpr bool operator==(const HalfVector3 &v) const
Definition: half.h:117
InternalHalf z
Definition: half.h:104
InternalHalf y
Definition: half.h:103
A storage only class for half precision floating point vector 4.
Definition: half.h:58
InternalHalf e[4]
Definition: half.h:66
constexpr bool operator==(const HalfVector4 &v) const
Definition: half.h:89
constexpr HalfVector4(const Color &a)
Definition: half.h:71
constexpr HalfVector4(const Vector4 &a)
Definition: half.h:77
constexpr bool operator!=(const HalfVector4 &v) const
Definition: half.h:93
InternalHalf x
Definition: half.h:61
constexpr HalfVector4(InternalHalf x, InternalHalf y, InternalHalf z, InternalHalf w)
Definition: half.h:83
InternalHalf w
Definition: half.h:64
InternalHalf y
Definition: half.h:62
InternalHalf z
Definition: half.h:63
constexpr HalfVector4()
Definition: half.h:69