From d83fbb965acbadb93067a73f24912c919ef5f71b Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 20 Dec 2021 16:12:43 -0800 Subject: [PATCH] Add //flutter/fml/math.h with common math constants. (flutter/engine#30425) This came up as I was [debugging failures on Windows](https://ci.chromium.org/p/flutter/builders/try/Windows%20Host%20Engine/30618?) due to use of the non-portable constants. Fixes https://github.com/flutter/flutter/issues/82216 --- .../ci/licenses_golden/licenses_flutter | 2 + engine/src/flutter/fml/BUILD.gn | 2 + engine/src/flutter/fml/math.h | 48 +++++++++++++++++++ engine/src/flutter/fml/math_unittests.cc | 27 +++++++++++ 4 files changed, 79 insertions(+) create mode 100644 engine/src/flutter/fml/math.h create mode 100644 engine/src/flutter/fml/math_unittests.cc diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 149cdd1460..c7278d1628 100755 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -176,6 +176,8 @@ FILE: ../../../flutter/fml/make_copyable.h FILE: ../../../flutter/fml/mapping.cc FILE: ../../../flutter/fml/mapping.h FILE: ../../../flutter/fml/mapping_unittests.cc +FILE: ../../../flutter/fml/math.h +FILE: ../../../flutter/fml/math_unittests.cc FILE: ../../../flutter/fml/memory/ref_counted.h FILE: ../../../flutter/fml/memory/ref_counted_internal.h FILE: ../../../flutter/fml/memory/ref_counted_unittest.cc diff --git a/engine/src/flutter/fml/BUILD.gn b/engine/src/flutter/fml/BUILD.gn index 430bba1101..1f764cea4b 100644 --- a/engine/src/flutter/fml/BUILD.gn +++ b/engine/src/flutter/fml/BUILD.gn @@ -37,6 +37,7 @@ source_set("fml") { "make_copyable.h", "mapping.cc", "mapping.h", + "math.h", "memory/ref_counted.h", "memory/ref_counted_internal.h", "memory/ref_ptr.h", @@ -272,6 +273,7 @@ if (enable_unittests) { "hex_codec_unittest.cc", "logging_unittests.cc", "mapping_unittests.cc", + "math_unittests.cc", "memory/ref_counted_unittest.cc", "memory/task_runner_checker_unittest.cc", "memory/weak_ptr_unittest.cc", diff --git a/engine/src/flutter/fml/math.h b/engine/src/flutter/fml/math.h new file mode 100644 index 0000000000..848f161f57 --- /dev/null +++ b/engine/src/flutter/fml/math.h @@ -0,0 +1,48 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +namespace flutter { +namespace math { + +// e +constexpr float kE = 2.7182818284590452354; + +// log_2 e +constexpr float kLog2_E = 1.4426950408889634074; + +// log_10 e +constexpr float kLog10_E = 0.43429448190325182765; + +// log_e 2 +constexpr float klogE_2 = 0.69314718055994530942; + +// log_e 10 +constexpr float klogE_10 = 2.30258509299404568402; + +// pi +constexpr float kPi = 3.14159265358979323846; + +// pi/2 +constexpr float kPiOver2 = 1.57079632679489661923; + +// pi/4 +constexpr float kPiOver4 = 0.78539816339744830962; + +// 1/pi +constexpr float k1OverPi = 0.31830988618379067154; + +// 2/pi +constexpr float k2OverPi = 0.63661977236758134308; + +// 2/sqrt(pi) +constexpr float k2OverSqrtPi = 1.12837916709551257390; + +// sqrt(2) +constexpr float kSqrt2 = 1.41421356237309504880; + +// 1/sqrt(2) +constexpr float k1OverSqrt2 = 0.70710678118654752440; + +} // namespace math +} // namespace flutter diff --git a/engine/src/flutter/fml/math_unittests.cc b/engine/src/flutter/fml/math_unittests.cc new file mode 100644 index 0000000000..0e627e28bb --- /dev/null +++ b/engine/src/flutter/fml/math_unittests.cc @@ -0,0 +1,27 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include + +#include "flutter/fml/math.h" +#include "gtest/gtest.h" + +namespace flutter { +namespace testing { + +TEST(MathTest, Constants) { + // Don't use the constants in cmath as those aren't portable. + EXPECT_FLOAT_EQ(std::log2(math::kE), math::kLog2_E); + EXPECT_FLOAT_EQ(std::log10(math::kE), math::kLog10_E); + EXPECT_FLOAT_EQ(std::log(2.0f), math::klogE_2); + EXPECT_FLOAT_EQ(math::kPi / 2.0f, math::kPiOver2); + EXPECT_FLOAT_EQ(math::kPi / 4.0f, math::kPiOver4); + EXPECT_FLOAT_EQ(1.0f / math::kPi, math::k1OverPi); + EXPECT_FLOAT_EQ(2.0f / math::kPi, math::k2OverPi); + EXPECT_FLOAT_EQ(std::sqrt(2.0f), math::kSqrt2); + EXPECT_FLOAT_EQ(1.0f / std::sqrt(2.0f), math::k1OverSqrt2); +} + +} // namespace testing +} // namespace flutter