Store YGFloatOptional
in 32 bits
Summary: @public After removing `-ffast-math`, `NaN` can again be used to represent `undefined`. That allows us to remove the additional flag from `YGFloatOptional`, and reduce memory usage. Reviewed By: SidharthGuglani Differential Revision: D13439611 fbshipit-source-id: 93e90f72f0415edb228b4e7d145e1fae35cc6b43
This commit is contained in:
committed by
Facebook Github Bot
parent
da678ef971
commit
6bdd39d0ed
@@ -10,11 +10,11 @@
|
|||||||
#include <yoga/YGFloatOptional.h>
|
#include <yoga/YGFloatOptional.h>
|
||||||
#include <yoga/YGValue.h>
|
#include <yoga/YGValue.h>
|
||||||
|
|
||||||
static const auto empty = YGFloatOptional{};
|
constexpr auto empty = YGFloatOptional{};
|
||||||
static const auto zero = YGFloatOptional{0.0f};
|
constexpr auto zero = YGFloatOptional{0.0f};
|
||||||
static const auto one = YGFloatOptional{1.0f};
|
constexpr auto one = YGFloatOptional{1.0f};
|
||||||
static const auto positive = YGFloatOptional{1234.5f};
|
constexpr auto positive = YGFloatOptional{1234.5f};
|
||||||
static const auto negative = YGFloatOptional{-9876.5f};
|
constexpr auto negative = YGFloatOptional{-9876.5f};
|
||||||
|
|
||||||
TEST(YGFloatOptional, value) {
|
TEST(YGFloatOptional, value) {
|
||||||
ASSERT_EQ(zero.getValue(), 0.0f);
|
ASSERT_EQ(zero.getValue(), 0.0f);
|
||||||
|
@@ -7,23 +7,13 @@
|
|||||||
#include "YGFloatOptional.h"
|
#include "YGFloatOptional.h"
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "Yoga.h"
|
|
||||||
#include "Yoga-internal.h"
|
#include "Yoga-internal.h"
|
||||||
|
#include "Yoga.h"
|
||||||
|
|
||||||
using namespace facebook;
|
using namespace facebook;
|
||||||
|
|
||||||
YGFloatOptional::YGFloatOptional(float value) {
|
|
||||||
if (yoga::isUndefined(value)) {
|
|
||||||
isUndefined_ = true;
|
|
||||||
value_ = 0;
|
|
||||||
} else {
|
|
||||||
value_ = value;
|
|
||||||
isUndefined_ = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
float YGFloatOptional::getValue() const {
|
float YGFloatOptional::getValue() const {
|
||||||
if (isUndefined_) {
|
if (isUndefined()) {
|
||||||
// Abort, accessing a value of an undefined float optional
|
// Abort, accessing a value of an undefined float optional
|
||||||
std::cerr << "Tried to get value of an undefined YGFloatOptional\n";
|
std::cerr << "Tried to get value of an undefined YGFloatOptional\n";
|
||||||
std::exit(EXIT_FAILURE);
|
std::exit(EXIT_FAILURE);
|
||||||
@@ -31,53 +21,38 @@ float YGFloatOptional::getValue() const {
|
|||||||
return value_;
|
return value_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool YGFloatOptional::operator==(const YGFloatOptional& op) const {
|
bool YGFloatOptional::operator==(YGFloatOptional op) const {
|
||||||
if (isUndefined_ == op.isUndefined()) {
|
return value_ == op.value_ || (isUndefined() && op.isUndefined());
|
||||||
return isUndefined_ || value_ == op.getValue();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool YGFloatOptional::operator!=(const YGFloatOptional& op) const {
|
bool YGFloatOptional::operator!=(YGFloatOptional op) const {
|
||||||
return !(*this == op);
|
return !(*this == op);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool YGFloatOptional::operator==(float val) const {
|
bool YGFloatOptional::operator==(float val) const {
|
||||||
if (yoga::isUndefined(val) == isUndefined_) {
|
return value_ == val || (isUndefined() && yoga::isUndefined(val));
|
||||||
return isUndefined_ || val == value_;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool YGFloatOptional::operator!=(float val) const {
|
bool YGFloatOptional::operator!=(float val) const {
|
||||||
return !(*this == val);
|
return !(*this == val);
|
||||||
}
|
}
|
||||||
|
|
||||||
YGFloatOptional YGFloatOptional::operator+(const YGFloatOptional& op) const {
|
YGFloatOptional YGFloatOptional::operator+(YGFloatOptional op) const {
|
||||||
if (!isUndefined_ && !op.isUndefined_) {
|
return YGFloatOptional{value_ + op.value_};
|
||||||
return YGFloatOptional(value_ + op.value_);
|
|
||||||
}
|
|
||||||
return YGFloatOptional();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool YGFloatOptional::operator>(const YGFloatOptional& op) const {
|
bool YGFloatOptional::operator>(YGFloatOptional op) const {
|
||||||
if (isUndefined_ || op.isUndefined_) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return value_ > op.value_;
|
return value_ > op.value_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool YGFloatOptional::operator<(const YGFloatOptional& op) const {
|
bool YGFloatOptional::operator<(YGFloatOptional op) const {
|
||||||
if (isUndefined_ || op.isUndefined_) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return value_ < op.value_;
|
return value_ < op.value_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool YGFloatOptional::operator>=(const YGFloatOptional& op) const {
|
bool YGFloatOptional::operator>=(YGFloatOptional op) const {
|
||||||
return *this == op || *this > op;
|
return *this > op || *this == op;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool YGFloatOptional::operator<=(const YGFloatOptional& op) const {
|
bool YGFloatOptional::operator<=(YGFloatOptional op) const {
|
||||||
return *this == op || *this < op;
|
return *this < op || *this == op;
|
||||||
}
|
}
|
||||||
|
@@ -6,37 +6,33 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
struct YGFloatOptional {
|
struct YGFloatOptional {
|
||||||
private:
|
private:
|
||||||
float value_ = 0;
|
float value_ = std::numeric_limits<float>::quiet_NaN();
|
||||||
bool isUndefined_ = true;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit YGFloatOptional(float value);
|
explicit constexpr YGFloatOptional(float value) : value_(value) {}
|
||||||
YGFloatOptional() = default;
|
constexpr YGFloatOptional() = default;
|
||||||
|
|
||||||
// Program will terminate if the value of an undefined is accessed. Please
|
// Program will terminate if the value of an undefined is accessed. Please
|
||||||
// make sure to check if the optional is defined before calling this function.
|
// make sure to check if the optional is defined before calling this function.
|
||||||
// To check if float optional is defined, use `isUndefined()`.
|
// To check if float optional is defined, use `isUndefined()`.
|
||||||
float getValue() const;
|
float getValue() const;
|
||||||
|
|
||||||
// Sets the value of float optional, and thus isUndefined is assigned false.
|
|
||||||
void setValue(float val) {
|
|
||||||
value_ = val;
|
|
||||||
isUndefined_ = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isUndefined() const {
|
bool isUndefined() const {
|
||||||
return isUndefined_;
|
return std::isnan(value_);
|
||||||
}
|
}
|
||||||
|
|
||||||
YGFloatOptional operator+(const YGFloatOptional& op) const;
|
YGFloatOptional operator+(YGFloatOptional op) const;
|
||||||
bool operator>(const YGFloatOptional& op) const;
|
bool operator>(YGFloatOptional op) const;
|
||||||
bool operator<(const YGFloatOptional& op) const;
|
bool operator<(YGFloatOptional op) const;
|
||||||
bool operator>=(const YGFloatOptional& op) const;
|
bool operator>=(YGFloatOptional op) const;
|
||||||
bool operator<=(const YGFloatOptional& op) const;
|
bool operator<=(YGFloatOptional op) const;
|
||||||
bool operator==(const YGFloatOptional& op) const;
|
bool operator==(YGFloatOptional op) const;
|
||||||
bool operator!=(const YGFloatOptional& op) const;
|
bool operator!=(YGFloatOptional op) const;
|
||||||
|
|
||||||
bool operator==(float val) const;
|
bool operator==(float val) const;
|
||||||
bool operator!=(float val) const;
|
bool operator!=(float val) const;
|
||||||
|
@@ -211,7 +211,7 @@ YGFloatOptional YGNode::relativePosition(
|
|||||||
|
|
||||||
YGFloatOptional trailingPosition = getTrailingPosition(axis, axisSize);
|
YGFloatOptional trailingPosition = getTrailingPosition(axis, axisSize);
|
||||||
if (!trailingPosition.isUndefined()) {
|
if (!trailingPosition.isUndefined()) {
|
||||||
trailingPosition.setValue(-1 * trailingPosition.getValue());
|
trailingPosition = YGFloatOptional{-1 * trailingPosition.getValue()};
|
||||||
}
|
}
|
||||||
return trailingPosition;
|
return trailingPosition;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user