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

Replace double/float IsNaN + IsInfinity checks with optimized IsFinite #10690

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -632,12 +632,7 @@ internal static bool IsValidAnimationValueDecimal(Decimal value)

internal static bool IsValidAnimationValueDouble(Double value)
{
if (IsInvalidDouble(value))
{
return false;
}

return true;
return double.IsFinite(value);
}

internal static bool IsValidAnimationValueInt16(Int16 value)
Expand All @@ -662,95 +657,53 @@ internal static bool IsValidAnimationValueMatrix(Matrix value)

internal static bool IsValidAnimationValuePoint(Point value)
{
if (IsInvalidDouble(value.X) || IsInvalidDouble(value.Y))
{
return false;
}

return true;
return double.IsFinite(value.X) && double.IsFinite(value.Y);
}

internal static bool IsValidAnimationValuePoint3D(Point3D value)
{
if (IsInvalidDouble(value.X) || IsInvalidDouble(value.Y) || IsInvalidDouble(value.Z))
{
return false;
}

return true;
return double.IsFinite(value.X) && double.IsFinite(value.Y) && double.IsFinite(value.Z);
}

internal static bool IsValidAnimationValueQuaternion(Quaternion value)
{
if ( IsInvalidDouble(value.X) || IsInvalidDouble(value.Y)
|| IsInvalidDouble(value.Z) || IsInvalidDouble(value.W))
{
return false;
}

return true;
return double.IsFinite(value.X) && double.IsFinite(value.Y) && double.IsFinite(value.Z) && double.IsFinite(value.W);
}

internal static bool IsValidAnimationValueRect(Rect value)
{
if ( IsInvalidDouble(value.Location.X) || IsInvalidDouble(value.Location.Y)
|| IsInvalidDouble(value.Size.Width) || IsInvalidDouble(value.Size.Height)
|| value.IsEmpty)
{
return false;
}

return true;
return double.IsFinite(value.Location.X) && double.IsFinite(value.Location.Y) &&
double.IsFinite(value.Size.Width) && double.IsFinite(value.Size.Height) && !value.IsEmpty;
}

internal static bool IsValidAnimationValueRotation3D(Rotation3D value)
{
return IsValidAnimationValueQuaternion(value.InternalQuaternion);
}

internal static bool IsValidAnimationValueSingle(Single value)
internal static bool IsValidAnimationValueSingle(float value)
{
if (IsInvalidDouble(value))
{
return false;
}

return true;
return float.IsFinite(value);
}

internal static bool IsValidAnimationValueSize(Size value)
{
if (IsInvalidDouble(value.Width) || IsInvalidDouble(value.Height))
{
return false;
}

return true;
return double.IsFinite(value.Width) && double.IsFinite(value.Height);
}

internal static bool IsValidAnimationValueString(String value)
internal static bool IsValidAnimationValueString(string value)
{
return true;
}

internal static bool IsValidAnimationValueVector(System.Windows.Vector value)
internal static bool IsValidAnimationValueVector(Vector value)
{
if (IsInvalidDouble(value.X) || IsInvalidDouble(value.Y))
{
return false;
}

return true;
return double.IsFinite(value.X) && double.IsFinite(value.Y);
}

internal static bool IsValidAnimationValueVector3D(Vector3D value)
{
if (IsInvalidDouble(value.X) || IsInvalidDouble(value.Y) || IsInvalidDouble(value.Z))
{
return false;
}

return true;
return double.IsFinite(value.X) && double.IsFinite(value.Y) && double.IsFinite(value.Z);
}

#endregion
Expand Down Expand Up @@ -839,14 +792,5 @@ internal static Rotation3D GetZeroValueRotation3D(Rotation3D baseValue)

#endregion

#region Helpers

private static Boolean IsInvalidDouble(Double value)
{
return Double.IsInfinity(value)
|| double.IsNaN(value);
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ internal StylusShape(){}
///</summary>
internal StylusShape(StylusTip tip, double width, double height, double rotation)
{
if (Double.IsNaN(width) || Double.IsInfinity(width) || width < DrawingAttributes.MinWidth || width > DrawingAttributes.MaxWidth)
if (!double.IsFinite(width) || width < DrawingAttributes.MinWidth || width > DrawingAttributes.MaxWidth)
{
throw new ArgumentOutOfRangeException(nameof(width));
}

if (Double.IsNaN(height) || Double.IsInfinity(height) || height < DrawingAttributes.MinHeight || height > DrawingAttributes.MaxHeight)
if (!double.IsFinite(height) || height < DrawingAttributes.MinHeight || height > DrawingAttributes.MaxHeight)
{
throw new ArgumentOutOfRangeException(nameof(height));
}

if (Double.IsNaN(rotation) || Double.IsInfinity(rotation))
if (!double.IsFinite(rotation))
{
throw new ArgumentOutOfRangeException(nameof(rotation));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public double DesiredDeceleration
get { return _desiredDeceleration; }
set
{
if (Double.IsInfinity(value) || Double.IsNaN(value))
if (!double.IsFinite(value))
{
throw new ArgumentOutOfRangeException(nameof(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public double DesiredDeceleration
get { return _desiredDeceleration; }
set
{
if (Double.IsInfinity(value) || Double.IsNaN(value))
if (!double.IsFinite(value))
{
throw new ArgumentOutOfRangeException(nameof(value));
}
Expand All @@ -69,7 +69,7 @@ public double DesiredRotation
get { return _desiredRotation; }
set
{
if (Double.IsInfinity(value) || Double.IsNaN(value))
if (!double.IsFinite(value))
{
throw new ArgumentOutOfRangeException(nameof(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public double DesiredDeceleration
get { return _desiredDeceleration; }
set
{
if (Double.IsInfinity(value) || Double.IsNaN(value))
if (!double.IsFinite(value))
{
throw new ArgumentOutOfRangeException(nameof(value));
}
Expand All @@ -69,7 +69,7 @@ public double DesiredDisplacement
get { return _desiredDisplacement; }
set
{
if (Double.IsInfinity(value) || Double.IsNaN(value))
if (!double.IsFinite(value))
{
throw new ArgumentOutOfRangeException(nameof(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace System.Windows.Media.Animation
/// <param name="count">The number of iterations specified by this RepeatBehavior.</param>
public RepeatBehavior(double count)
{
if (double.IsInfinity(count) || double.IsNaN(count) || count < 0.0)
if (!double.IsFinite(count) || count < 0.0)
throw new ArgumentOutOfRangeException(nameof(count), SR.Format(SR.Timing_RepeatBehaviorInvalidIterationCount, count));

_repeatDuration = TimeSpan.Zero;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

//
Expand Down Expand Up @@ -404,8 +404,7 @@ ref Point barycentric
return;
}

Debug.Assert(!double.IsInfinity(pz / pw) && !double.IsNaN(pz / pw),
"Expected near/far tests to cull -Inf/+Inf and NaN.");
Debug.Assert(double.IsFinite(pz / pw), "Expected near/far tests to cull -Inf/+Inf and NaN.");
}

double dist = (worldPointHit - rayParams.Origin).Length;
Expand Down Expand Up @@ -490,8 +489,7 @@ ref Point barycentric
return;
}

Debug.Assert(!double.IsInfinity(pz / pw) && !double.IsNaN(pz / pw),
"Expected near/far tests to cull -Inf/+Inf and NaN.");
Debug.Assert(double.IsFinite(pz / pw), "Expected near/far tests to cull -Inf/+Inf and NaN.");
}

Point3D a = v0, b = v1, c = v2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1085,9 +1085,7 @@ internal static double RoundLayoutValue(double value, double dpiScale)
{
newValue = Math.Round(value * dpiScale) / dpiScale;
// If rounding produces a value unacceptable to layout (NaN, Infinity or MaxValue), use the original value.
if (double.IsNaN(newValue) ||
Double.IsInfinity(newValue) ||
DoubleUtil.AreClose(newValue, Double.MaxValue))
if (!double.IsFinite(newValue) || DoubleUtil.AreClose(newValue, double.MaxValue))
{
newValue = value;
}
Expand Down Expand Up @@ -1388,9 +1386,9 @@ private static void RenderTransform_Changed(DependencyObject d, DependencyProper

private static bool IsRenderTransformOriginValid(object value)
{
Point v = (Point)value;
return ( (!double.IsNaN(v.X) && !Double.IsPositiveInfinity(v.X) && !Double.IsNegativeInfinity(v.X))
&& (!double.IsNaN(v.Y) && !Double.IsPositiveInfinity(v.Y) && !Double.IsNegativeInfinity(v.Y)));
Point origin = (Point)value;

return double.IsFinite(origin.X) && double.IsFinite(origin.Y);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.


Expand Down Expand Up @@ -113,28 +113,10 @@ internal static Thickness ScaleThickness(Thickness value, double factor)

#region IsValidAnimationValue Methods

private static bool IsValidAnimationValueDouble(Double value)
{
if (IsInvalidDouble(value))
{
return false;
}

return true;
}

internal static bool IsValidAnimationValueThickness(Thickness value)
{
// At least one of the sub-values must be an interpolatable length.
if ( IsValidAnimationValueDouble(value.Left)
|| IsValidAnimationValueDouble(value.Top)
|| IsValidAnimationValueDouble(value.Right)
|| IsValidAnimationValueDouble(value.Bottom))
{
return true;
}

return false;
return double.IsFinite(value.Left) || double.IsFinite(value.Top) || double.IsFinite(value.Right) || double.IsFinite(value.Bottom);
}

#endregion
Expand All @@ -157,14 +139,5 @@ internal static Thickness GetZeroValueThickness(Thickness baseValue)

#endregion

#region Helpers

private static bool IsInvalidDouble(double value)
{
return Double.IsInfinity(value)
|| double.IsNaN(value);
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -540,17 +540,6 @@ internal static Size ArrangeElementWithSingleChild(UIElement element, Size arran
return arrangeSize;
}

/// <summary>
/// Helper method used for double parameter validation. Returns false
/// if the value is either Infinity (positive or negative) or NaN.
/// </summary>
/// <param name="value">The double value to test</param>
/// <returns>Whether the value is a valid double.</returns>
internal static bool IsDoubleValid(double value)
{
return !(Double.IsInfinity(value) || Double.IsNaN(value));
}

/// <summary>
/// Checks if the given IProvideValueTarget can receive
/// a DynamicResource or Binding MarkupExtension.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ public void SetScale(double scale)
{
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(scale, 0.0);

if (!Helper.IsDoubleValid(scale))
if (!double.IsFinite(scale))
{
throw new ArgumentOutOfRangeException(nameof(scale));
}
Expand Down Expand Up @@ -798,7 +798,7 @@ public double VerticalPageSpacing

set
{
if (!Helper.IsDoubleValid(value))
if (!double.IsFinite(value))
{
throw new ArgumentOutOfRangeException(nameof(value));
}
Expand All @@ -820,7 +820,7 @@ public double HorizontalPageSpacing

set
{
if (!Helper.IsDoubleValid(value))
if (!double.IsFinite(value))
{
throw new ArgumentOutOfRangeException(nameof(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ void ITransformProvider.Move(double x, double y)
if (!IsEnabled())
throw new ElementNotEnabledException();

if (double.IsInfinity(x) || double.IsNaN(x))
if (!double.IsFinite(x))
throw new ArgumentOutOfRangeException(nameof(x));

if (double.IsInfinity(y) || double.IsNaN(y))
if (!double.IsFinite(y))
throw new ArgumentOutOfRangeException(nameof(y));

((GridSplitter)Owner).KeyboardMoveSplitter(x, y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ public static readonly DependencyProperty CornerRadiusProperty
private static bool IsCornerRadiusValid(object value)
{
CornerRadius cr = (CornerRadius)value;
return (cr.IsValid(false, false, false, false));

return cr.IsValid();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2014,7 +2014,7 @@ private double GetViewportWidth()
if (DoubleUtil.AreClose(availableViewportWidth, 0.0) && parentRowsPresenter != null)
{
Size rowPresenterAvailableSize = parentRowsPresenter.AvailableSize;
if (!double.IsNaN(rowPresenterAvailableSize.Width) && !Double.IsInfinity(rowPresenterAvailableSize.Width))
if (double.IsFinite(rowPresenterAvailableSize.Width))
{
availableViewportWidth = rowPresenterAvailableSize.Width;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public DataGridLength(double value, DataGridLengthUnitType type)
/// </exception>
public DataGridLength(double value, DataGridLengthUnitType type, double desiredValue, double displayValue)
{
if (double.IsNaN(value) || Double.IsInfinity(value))
if (!double.IsFinite(value))
{
throw new ArgumentException(
SR.DataGridLength_Infinity,
Expand Down
Loading