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  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 {
67  };
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 
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 {
107  };
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 
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 {
134  };
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_
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 bool operator!=(const Half &v) const
Definition: half.h:56
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:129
InternalHalf x
Definition: half.h:132
constexpr bool operator==(const HalfVector2 &v) const
Definition: half.h:145
constexpr HalfVector2(const Vector2 &a)
Definition: half.h:140
constexpr HalfVector2(InternalHalf x, InternalHalf y)
Definition: half.h:143
constexpr HalfVector2()
Definition: half.h:138
InternalHalf e[2]
Definition: half.h:135
constexpr bool operator!=(const HalfVector2 &v) const
Definition: half.h:149
InternalHalf y
Definition: half.h:133
A storage only class for half precision floating point vector 3.
Definition: half.h:101
InternalHalf e[3]
Definition: half.h:108
constexpr HalfVector3(const Vector3 &a)
Definition: half.h:113
constexpr HalfVector3(InternalHalf x, InternalHalf y, InternalHalf z)
Definition: half.h:116
InternalHalf x
Definition: half.h:104
constexpr bool operator!=(const HalfVector3 &v) const
Definition: half.h:123
constexpr HalfVector3()
Definition: half.h:111
constexpr bool operator==(const HalfVector3 &v) const
Definition: half.h:119
InternalHalf z
Definition: half.h:106
InternalHalf y
Definition: half.h:105
A storage only class for half precision floating point vector 4.
Definition: half.h:60
InternalHalf e[4]
Definition: half.h:68
constexpr bool operator==(const HalfVector4 &v) const
Definition: half.h:91
constexpr HalfVector4(const Color &a)
Definition: half.h:73
constexpr HalfVector4(const Vector4 &a)
Definition: half.h:79
constexpr bool operator!=(const HalfVector4 &v) const
Definition: half.h:95
InternalHalf x
Definition: half.h:63
constexpr HalfVector4(InternalHalf x, InternalHalf y, InternalHalf z, InternalHalf w)
Definition: half.h:85
InternalHalf w
Definition: half.h:66
InternalHalf y
Definition: half.h:64
InternalHalf z
Definition: half.h:65
constexpr HalfVector4()
Definition: half.h:71