Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FloatOptional GCC build fix and more constexpr #1414

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

plugins {
id("com.android.library") version "8.1.1" apply false
id("com.android.application") version "8.1.1" apply false
id("com.android.library") version "8.2.0-beta06" apply false
id("com.android.application") version "8.2.0-beta06" apply false
id("io.github.gradle-nexus.publish-plugin") version "1.3.0"
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ android {
namespace = "com.facebook.yoga"
compileSdk = 34
buildToolsVersion = "34.0.0"
ndkVersion = "25.1.8937393"
ndkVersion = "26.0.10792818"

defaultConfig {
minSdk = 21
Expand Down
2 changes: 2 additions & 0 deletions tests/FloatOptionalTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

#include <cmath>

#include <gtest/gtest.h>

#include <yoga/YGValue.h>
Expand Down
8 changes: 4 additions & 4 deletions yoga/algorithm/CalculateLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,10 +563,10 @@ static void measureNodeWithMeasureFunc(
// We want to make sure we don't call measure with negative size
const float innerWidth = yoga::isUndefined(availableWidth)
? availableWidth
: yoga::maxOrDefined(0, availableWidth - paddingAndBorderAxisRow);
: yoga::maxOrDefined(0.0f, availableWidth - paddingAndBorderAxisRow);
const float innerHeight = yoga::isUndefined(availableHeight)
? availableHeight
: yoga::maxOrDefined(0, availableHeight - paddingAndBorderAxisColumn);
: yoga::maxOrDefined(0.0f, availableHeight - paddingAndBorderAxisColumn);

if (widthMeasureMode == MeasureMode::Exactly &&
heightMeasureMode == MeasureMode::Exactly) {
Expand Down Expand Up @@ -1223,7 +1223,7 @@ static void justifyMainAxis(
const float occupiedSpaceByChildNodes =
availableInnerMainDim - flexLine.layout.remainingFreeSpace;
flexLine.layout.remainingFreeSpace = yoga::maxOrDefined(
0, minAvailableMainDim - occupiedSpaceByChildNodes);
0.0f, minAvailableMainDim - occupiedSpaceByChildNodes);
} else {
flexLine.layout.remainingFreeSpace = 0;
}
Expand Down Expand Up @@ -1260,7 +1260,7 @@ static void justifyMainAxis(
case Justify::SpaceBetween:
if (flexLine.itemsInFlow.size() > 1) {
betweenMainDim +=
yoga::maxOrDefined(flexLine.layout.remainingFreeSpace, 0) /
yoga::maxOrDefined(flexLine.layout.remainingFreeSpace, 0.0f) /
static_cast<float>(flexLine.itemsInFlow.size() - 1);
}
break;
Expand Down
43 changes: 17 additions & 26 deletions yoga/numeric/Comparison.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,54 @@

#pragma once

#include <algorithm>
#include <array>
#include <cmath>
#include <concepts>

#include <yoga/Yoga.h>
#include <yoga/numeric/FloatOptional.h>

namespace facebook::yoga {

template <typename FloatT>
inline bool isUndefined(FloatT value) {
return std::isnan(value);
constexpr bool isUndefined(std::floating_point auto value) {
return value != value;
}

inline float maxOrDefined(const float a, const float b) {
constexpr auto maxOrDefined(
std::floating_point auto a,
std::floating_point auto b) {
if (!yoga::isUndefined(a) && !yoga::isUndefined(b)) {
return fmaxf(a, b);
return std::max(a, b);
}
return yoga::isUndefined(a) ? b : a;
}

inline float minOrDefined(const float a, const float b) {
constexpr auto minOrDefined(
std::floating_point auto a,
std::floating_point auto b) {
if (!yoga::isUndefined(a) && !yoga::isUndefined(b)) {
return fminf(a, b);
return std::min(a, b);
}

return yoga::isUndefined(a) ? b : a;
}

inline FloatOptional maxOrDefined(FloatOptional op1, FloatOptional op2) {
if (op1 >= op2) {
return op1;
}
if (op2 > op1) {
return op2;
}
return op1.isUndefined() ? op2 : op1;
}

// Custom equality functions using a hardcoded epsilon of 0.0001f, or returning
// true if both floats are NaN.
inline bool inexactEquals(const float a, const float b) {
inline bool inexactEquals(float a, float b) {
if (!yoga::isUndefined(a) && !yoga::isUndefined(b)) {
return fabs(a - b) < 0.0001f;
return std::abs(a - b) < 0.0001f;
}
return yoga::isUndefined(a) && yoga::isUndefined(b);
}

inline bool inexactEquals(const double a, const double b) {
inline bool inexactEquals(double a, double b) {
if (!yoga::isUndefined(a) && !yoga::isUndefined(b)) {
return fabs(a - b) < 0.0001;
return std::abs(a - b) < 0.0001;
}
return yoga::isUndefined(a) && yoga::isUndefined(b);
}

inline bool inexactEquals(const FloatOptional a, const FloatOptional b) {
return inexactEquals(a.unwrap(), b.unwrap());
}

inline bool inexactEquals(const YGValue& a, const YGValue& b) {
if (a.unit != b.unit) {
return false;
Expand Down
36 changes: 22 additions & 14 deletions yoga/numeric/FloatOptional.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#pragma once

#include <cmath>
#include <yoga/numeric/Comparison.h>
#include <limits>

namespace facebook::yoga {
Expand All @@ -29,53 +29,61 @@ struct FloatOptional {
return isUndefined() ? defaultValue : value_;
}

bool isUndefined() const {
return std::isnan(value_);
constexpr bool isUndefined() const {
return yoga::isUndefined(value_);
}
};

// operators take FloatOptional by value, as it is a 32bit value

inline bool operator==(FloatOptional lhs, FloatOptional rhs) {
constexpr bool operator==(FloatOptional lhs, FloatOptional rhs) {
return lhs.unwrap() == rhs.unwrap() ||
(lhs.isUndefined() && rhs.isUndefined());
}
inline bool operator!=(FloatOptional lhs, FloatOptional rhs) {
constexpr bool operator!=(FloatOptional lhs, FloatOptional rhs) {
return !(lhs == rhs);
}

inline bool operator==(FloatOptional lhs, float rhs) {
constexpr bool operator==(FloatOptional lhs, float rhs) {
return lhs == FloatOptional{rhs};
}
inline bool operator!=(FloatOptional lhs, float rhs) {
constexpr bool operator!=(FloatOptional lhs, float rhs) {
return !(lhs == rhs);
}

inline bool operator==(float lhs, FloatOptional rhs) {
constexpr bool operator==(float lhs, FloatOptional rhs) {
return rhs == lhs;
}
inline bool operator!=(float lhs, FloatOptional rhs) {
constexpr bool operator!=(float lhs, FloatOptional rhs) {
return !(lhs == rhs);
}

inline FloatOptional operator+(FloatOptional lhs, FloatOptional rhs) {
constexpr FloatOptional operator+(FloatOptional lhs, FloatOptional rhs) {
return FloatOptional{lhs.unwrap() + rhs.unwrap()};
}

inline bool operator>(FloatOptional lhs, FloatOptional rhs) {
constexpr bool operator>(FloatOptional lhs, FloatOptional rhs) {
return lhs.unwrap() > rhs.unwrap();
}

inline bool operator<(FloatOptional lhs, FloatOptional rhs) {
constexpr bool operator<(FloatOptional lhs, FloatOptional rhs) {
return lhs.unwrap() < rhs.unwrap();
}

inline bool operator>=(FloatOptional lhs, FloatOptional rhs) {
constexpr bool operator>=(FloatOptional lhs, FloatOptional rhs) {
return lhs > rhs || lhs == rhs;
}

inline bool operator<=(FloatOptional lhs, FloatOptional rhs) {
constexpr bool operator<=(FloatOptional lhs, FloatOptional rhs) {
return lhs < rhs || lhs == rhs;
}

constexpr FloatOptional maxOrDefined(FloatOptional lhs, FloatOptional rhs) {
return FloatOptional{yoga::maxOrDefined(lhs.unwrap(), rhs.unwrap())};
}

inline bool inexactEquals(FloatOptional lhs, FloatOptional rhs) {
return yoga::inexactEquals(lhs.unwrap(), rhs.unwrap());
}

} // namespace facebook::yoga
Loading