Skip to content

Commit ec1dd96

Browse files
chinmaygardednfield
authored andcommitted
Fix issues with constexpr correctness. (flutter#77)
Some like std::abs are not available till C++23. The others were real warnings.
1 parent b97e965 commit ec1dd96

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

impeller/geometry/scalar.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66

77
#include <cfloat>
8+
#include <type_traits>
89
#include <valarray>
910

1011
#include "impeller/geometry/constants.h"
@@ -13,10 +14,15 @@ namespace impeller {
1314

1415
using Scalar = float;
1516

17+
template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>>
18+
constexpr T Absolute(const T& val) {
19+
return val >= T{} ? val : -val;
20+
}
21+
1622
constexpr inline bool ScalarNearlyEqual(Scalar x,
1723
Scalar y,
1824
Scalar tolerance = 1e-3) {
19-
return std::abs(x - y) <= tolerance;
25+
return Absolute(x - y) <= tolerance;
2026
}
2127

2228
struct Degrees;

impeller/geometry/vector.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct Vector3 {
4141
*
4242
* @return the calculated length.
4343
*/
44-
constexpr Scalar Length() const { return sqrt(x * x + y * y + z * z); }
44+
Scalar Length() const { return sqrt(x * x + y * y + z * z); }
4545

4646
constexpr Vector3 Normalize() const {
4747
const auto len = Length();
@@ -125,7 +125,7 @@ struct Vector4 {
125125

126126
constexpr Vector4(const Point& p) : x(p.x), y(p.y) {}
127127

128-
constexpr Vector4 Normalize() const {
128+
Vector4 Normalize() const {
129129
const Scalar inverse = 1.0 / sqrt(x * x + y * y + z * z + w * w);
130130
return Vector4(x * inverse, y * inverse, z * inverse, w * inverse);
131131
}

impeller/renderer/backend/metal/formats_mtl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ constexpr MTLSamplerAddressMode ToMTLSamplerAddressMode(
294294
return MTLSamplerAddressModeClampToEdge;
295295
}
296296

297-
constexpr MTLClearColor ToMTLClearColor(const Color& color) {
297+
inline MTLClearColor ToMTLClearColor(const Color& color) {
298298
return MTLClearColorMake(color.red, color.green, color.blue, color.alpha);
299299
}
300300

0 commit comments

Comments
 (0)