Skip to content

Commit

Permalink
Fix issues with constexpr correctness. (flutter#77)
Browse files Browse the repository at this point in the history
Some like std::abs are not available till C++23. The others were
real warnings.
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent b97e965 commit ec1dd96
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
8 changes: 7 additions & 1 deletion impeller/geometry/scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <cfloat>
#include <type_traits>
#include <valarray>

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

using Scalar = float;

template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>>
constexpr T Absolute(const T& val) {
return val >= T{} ? val : -val;
}

constexpr inline bool ScalarNearlyEqual(Scalar x,
Scalar y,
Scalar tolerance = 1e-3) {
return std::abs(x - y) <= tolerance;
return Absolute(x - y) <= tolerance;
}

struct Degrees;
Expand Down
4 changes: 2 additions & 2 deletions impeller/geometry/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct Vector3 {
*
* @return the calculated length.
*/
constexpr Scalar Length() const { return sqrt(x * x + y * y + z * z); }
Scalar Length() const { return sqrt(x * x + y * y + z * z); }

constexpr Vector3 Normalize() const {
const auto len = Length();
Expand Down Expand Up @@ -125,7 +125,7 @@ struct Vector4 {

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

constexpr Vector4 Normalize() const {
Vector4 Normalize() const {
const Scalar inverse = 1.0 / sqrt(x * x + y * y + z * z + w * w);
return Vector4(x * inverse, y * inverse, z * inverse, w * inverse);
}
Expand Down
2 changes: 1 addition & 1 deletion impeller/renderer/backend/metal/formats_mtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ constexpr MTLSamplerAddressMode ToMTLSamplerAddressMode(
return MTLSamplerAddressModeClampToEdge;
}

constexpr MTLClearColor ToMTLClearColor(const Color& color) {
inline MTLClearColor ToMTLClearColor(const Color& color) {
return MTLClearColorMake(color.red, color.green, color.blue, color.alpha);
}

Expand Down

0 comments on commit ec1dd96

Please sign in to comment.