/src/yoga/yoga/numeric/Comparison.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * |
4 | | * This source code is licensed under the MIT license found in the |
5 | | * LICENSE file in the root directory of this source tree. |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <algorithm> |
11 | | #include <array> |
12 | | #include <cmath> |
13 | | #include <concepts> |
14 | | |
15 | | #include <yoga/Yoga.h> |
16 | | |
17 | | namespace facebook::yoga { |
18 | | |
19 | 2.17G | constexpr bool isUndefined(std::floating_point auto value) { |
20 | 2.17G | return value != value; |
21 | 2.17G | } _ZN8facebook4yoga11isUndefinedITkNSt3__114floating_pointEfEEbT_ Line | Count | Source | 19 | 2.09G | constexpr bool isUndefined(std::floating_point auto value) { | 20 | 2.09G | return value != value; | 21 | 2.09G | } |
_ZN8facebook4yoga11isUndefinedITkNSt3__114floating_pointEdEEbT_ Line | Count | Source | 19 | 80.2M | constexpr bool isUndefined(std::floating_point auto value) { | 20 | 80.2M | return value != value; | 21 | 80.2M | } |
|
22 | | |
23 | 970M | constexpr bool isDefined(std::floating_point auto value) { |
24 | 970M | return !isUndefined(value); |
25 | 970M | } _ZN8facebook4yoga9isDefinedITkNSt3__114floating_pointEfEEbT_ Line | Count | Source | 23 | 915M | constexpr bool isDefined(std::floating_point auto value) { | 24 | 915M | return !isUndefined(value); | 25 | 915M | } |
_ZN8facebook4yoga9isDefinedITkNSt3__114floating_pointEdEEbT_ Line | Count | Source | 23 | 54.7M | constexpr bool isDefined(std::floating_point auto value) { | 24 | 54.7M | return !isUndefined(value); | 25 | 54.7M | } |
|
26 | | |
27 | | /** |
28 | | * Constexpr version of `std::isinf` before C++ 23 |
29 | | */ |
30 | 38.4M | constexpr bool isinf(auto value) { |
31 | 38.4M | return value == +std::numeric_limits<decltype(value)>::infinity() || |
32 | 38.4M | value == -std::numeric_limits<decltype(value)>::infinity(); |
33 | 38.4M | } |
34 | | |
35 | | constexpr auto maxOrDefined( |
36 | | std::floating_point auto a, |
37 | 422M | std::floating_point auto b) { |
38 | 422M | if (yoga::isDefined(a) && yoga::isDefined(b)) { |
39 | 67.4M | return std::max(a, b); |
40 | 67.4M | } |
41 | 355M | return yoga::isUndefined(a) ? b : a; |
42 | 422M | } |
43 | | |
44 | | constexpr auto minOrDefined( |
45 | | std::floating_point auto a, |
46 | 2.25M | std::floating_point auto b) { |
47 | 2.25M | if (yoga::isDefined(a) && yoga::isDefined(b)) { |
48 | 2.25M | return std::min(a, b); |
49 | 2.25M | } |
50 | | |
51 | 0 | return yoga::isUndefined(a) ? b : a; |
52 | 2.25M | } |
53 | | |
54 | | // Custom equality functions using a hardcoded epsilon of 0.0001f, or returning |
55 | | // true if both floats are NaN. |
56 | 81.2M | inline bool inexactEquals(float a, float b) { |
57 | 81.2M | if (yoga::isDefined(a) && yoga::isDefined(b)) { |
58 | 68.4M | return std::abs(a - b) < 0.0001f; |
59 | 68.4M | } |
60 | 12.7M | return yoga::isUndefined(a) && yoga::isUndefined(b); |
61 | 81.2M | } |
62 | | |
63 | 33.7M | inline bool inexactEquals(double a, double b) { |
64 | 33.7M | if (yoga::isDefined(a) && yoga::isDefined(b)) { |
65 | 20.9M | return std::abs(a - b) < 0.0001; |
66 | 20.9M | } |
67 | 12.7M | return yoga::isUndefined(a) && yoga::isUndefined(b); |
68 | 33.7M | } |
69 | | |
70 | | template <std::size_t Size, typename ElementT> |
71 | | bool inexactEquals( |
72 | | const std::array<ElementT, Size>& val1, |
73 | | const std::array<ElementT, Size>& val2) { |
74 | | bool areEqual = true; |
75 | | for (std::size_t i = 0; i < Size && areEqual; ++i) { |
76 | | areEqual = inexactEquals(val1[i], val2[i]); |
77 | | } |
78 | | return areEqual; |
79 | | } |
80 | | |
81 | | } // namespace facebook::yoga |