diff --git a/csharp/Facebook.Yoga/YogaConstants.cs b/csharp/Facebook.Yoga/YogaConstants.cs index a715b43c..9e05cca3 100644 --- a/csharp/Facebook.Yoga/YogaConstants.cs +++ b/csharp/Facebook.Yoga/YogaConstants.cs @@ -1,4 +1,4 @@ -/** +/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the @@ -9,28 +9,11 @@ namespace Facebook.Yoga { public static class YogaConstants { - /** - * Large positive number signifies that the property(float) is undefined. Earlier we used to have - * YGundefined as NAN, but the downside of this is that we can't use -ffast-math compiler flag as - * it assumes all floating-point calculation involve and result into finite numbers. For more - * information regarding -ffast-math compiler flag in clang, have a look at - * https://clang.llvm.org/docs/UsersManual.html#cmdoption-ffast-math - */ - public const float Undefined = 10E20F; + public const float Undefined = float.NaN; public static bool IsUndefined(float value) { - // Value of a float in the case of it being not defined is 10.1E20. Earlier it used to be NAN, - // the benefit of which - // was that if NAN is involved in any mathematical expression the result was NAN. But since we - // want to have `-ffast-math` - // flag being used by compiler which assumes that the floating point values are not NAN and Inf, - // we represent YGUndefined as 10.1E20. - // But now if YGUndefined is involved in any mathematical operations this value(10.1E20) would - // change. - // So the following check makes sure that if the value is outside a range (-10E8, 10E8) then it - // is undefined. - return value >= 10E8F || value <= -10E8; + return float.IsNaN(value); } public static bool IsUndefined(YogaValue value) diff --git a/java/com/facebook/yoga/YogaConstants.java b/java/com/facebook/yoga/YogaConstants.java index 61e212ef..b04a7e53 100644 --- a/java/com/facebook/yoga/YogaConstants.java +++ b/java/com/facebook/yoga/YogaConstants.java @@ -1,35 +1,17 @@ -/* - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. +/** + * Copyright (c) Facebook, Inc. and its affiliates. * + * This source code is licensed under the MIT license found in the LICENSE + * file in the root directory of this source tree. */ package com.facebook.yoga; public class YogaConstants { - /** - * Large positive number signifies that the property(float) is undefined. Earlier we used to have - * YGundefined as NAN, but the downside of this is that we can't use -ffast-math compiler flag as - * it assumes all floating-point calculation involve and result into finite numbers. For more - * information regarding -ffast-math compiler flag in clang, have a look at - * https://clang.llvm.org/docs/UsersManual.html#cmdoption-ffast-math - */ - public static final float UNDEFINED = (float) (10E20); + public static final float UNDEFINED = Float.NaN; public static boolean isUndefined(float value) { - // Value of a float in the case of it being not defined is 10.1E20. Earlier it used to be NAN, - // the benefit of which - // was that if NAN is involved in any mathematical expression the result was NAN. But since we - // want to have `-ffast-math` - // flag being used by compiler which assumes that the floating point values are not NAN and Inf, - // we represent YGUndefined as 10.1E20. - // But now if YGUndefined is involved in any mathematical operations this value(10.1E20) would - // change. - // So the following check makes sure that if the value is outside a range (-10E8, 10E8) then it - // is undefined. - return (Float.compare(value, (float) 10E8) >= 0 || Float.compare(value, (float) -10E8) <= 0); + return Float.compare(value, UNDEFINED) == 0; } public static boolean isUndefined(YogaValue value) { diff --git a/tools/build_defs/oss/yoga_defs.bzl b/tools/build_defs/oss/yoga_defs.bzl index 7eca7b9b..5a5872fe 100644 --- a/tools/build_defs/oss/yoga_defs.bzl +++ b/tools/build_defs/oss/yoga_defs.bzl @@ -58,7 +58,6 @@ BASE_COMPILER_FLAGS = [ "-Wall", "-Werror", "-O3", - "-ffast-math", ] LIBRARY_COMPILER_FLAGS = BASE_COMPILER_FLAGS + [ diff --git a/yoga/Utils.h b/yoga/Utils.h index d578511c..d007ff54 100644 --- a/yoga/Utils.h +++ b/yoga/Utils.h @@ -57,22 +57,12 @@ bool YGValueEqual(const YGValue a, const YGValue b); // difference between two floats is less than 0.0001f or both are undefined. bool YGFloatsEqual(const float a, const float b); -// We need custom max function, since we want that, if one argument is -// YGUndefined then the max funtion should return the other argument as the max -// value. We wouldn't have needed a custom max function if YGUndefined was NAN -// as fmax has the same behaviour, but with NAN we cannot use `-ffast-math` -// compiler flag. float YGFloatMax(const float a, const float b); YGFloatOptional YGFloatOptionalMax( const YGFloatOptional& op1, const YGFloatOptional& op2); -// We need custom min function, since we want that, if one argument is -// YGUndefined then the min funtion should return the other argument as the min -// value. We wouldn't have needed a custom min function if YGUndefined was NAN -// as fmin has the same behaviour, but with NAN we cannot use `-ffast-math` -// compiler flag. float YGFloatMin(const float a, const float b); // This custom float comparision function compares the array of float with @@ -106,7 +96,9 @@ inline bool YGFlexDirectionIsRow(const YGFlexDirection flexDirection) { flexDirection == YGFlexDirectionRowReverse; } -inline YGFloatOptional YGResolveValue(const YGValue value, const float ownerSize) { +inline YGFloatOptional YGResolveValue( + const YGValue value, + const float ownerSize) { switch (value.unit) { case YGUnitUndefined: case YGUnitAuto: diff --git a/yoga/Yoga-internal.h b/yoga/Yoga-internal.h index 1e9a1aaf..ef5e5d8c 100644 --- a/yoga/Yoga-internal.h +++ b/yoga/Yoga-internal.h @@ -27,15 +27,7 @@ namespace facebook { namespace yoga { inline bool isUndefined(float value) { - // Value of a float in the case of it being not defined is 10.1E20. Earlier - // it used to be NAN, the benefit of which was that if NAN is involved in any - // mathematical expression the result was NAN. But since we want to have - // `-ffast-math` flag being used by compiler which assumes that the floating - // point values are not NAN and Inf, we represent YGUndefined as 10.1E20. But - // now if YGUndefined is involved in any mathematical operations this - // value(10.1E20) would change. So the following check makes sure that if the - // value is outside a range (-10E8, 10E8) then it is undefined. - return value >= 10E8 || value <= -10E8; + return std::isnan(value); } } // namespace yoga diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index 6abfb7d3..d5fc12ef 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -18,10 +18,7 @@ /* define fmaxf if < VC12 */ #if _MSC_VER < 1800 __forceinline const float fmaxf(const float a, const float b) { - if (!YGFloatIsUndefined(a) && !YGFloatIsUndefined(b)) { - return (a > b) ? a : b; - } - return YGFloatIsUndefined(a) ? b : a; + return (a > b) ? a : b; } #endif #endif diff --git a/yoga/Yoga.h b/yoga/Yoga.h index 96e71dc7..61aae0fe 100644 --- a/yoga/Yoga.h +++ b/yoga/Yoga.h @@ -17,14 +17,13 @@ #include #endif -/** Large positive number signifies that the property(float) is undefined. - *Earlier we used to have YGundefined as NAN, but the downside of this is that - *we can't use -ffast-math compiler flag as it assumes all floating-point - *calculation involve and result into finite numbers. For more information - *regarding -ffast-math compiler flag in clang, have a look at - *https://clang.llvm.org/docs/UsersManual.html#cmdoption-ffast-math - **/ -#define YGUndefined 10E20F +// Not defined in MSVC++ +#ifndef NAN +static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff}; +#define NAN (*(const float*)__nan) +#endif + +#define YGUndefined NAN #include "YGEnums.h" #include "YGMacros.h"