Skip to content

Commit a72ca46

Browse files
committed
Replace double/float IsNaN + IsInfinity checks with optimized IsFinite
1 parent 2ded801 commit a72ca46

File tree

54 files changed

+165
-666
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+165
-666
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AnimatedTypeHelpers.cs

+13-69
Original file line numberDiff line numberDiff line change
@@ -632,12 +632,7 @@ internal static bool IsValidAnimationValueDecimal(Decimal value)
632632

633633
internal static bool IsValidAnimationValueDouble(Double value)
634634
{
635-
if (IsInvalidDouble(value))
636-
{
637-
return false;
638-
}
639-
640-
return true;
635+
return double.IsFinite(value);
641636
}
642637

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

663658
internal static bool IsValidAnimationValuePoint(Point value)
664659
{
665-
if (IsInvalidDouble(value.X) || IsInvalidDouble(value.Y))
666-
{
667-
return false;
668-
}
669-
670-
return true;
660+
return double.IsFinite(value.X) && double.IsFinite(value.Y);
671661
}
672662

673663
internal static bool IsValidAnimationValuePoint3D(Point3D value)
674664
{
675-
if (IsInvalidDouble(value.X) || IsInvalidDouble(value.Y) || IsInvalidDouble(value.Z))
676-
{
677-
return false;
678-
}
679-
680-
return true;
665+
return double.IsFinite(value.X) && double.IsFinite(value.Y) && double.IsFinite(value.Z);
681666
}
682667

683668
internal static bool IsValidAnimationValueQuaternion(Quaternion value)
684669
{
685-
if ( IsInvalidDouble(value.X) || IsInvalidDouble(value.Y)
686-
|| IsInvalidDouble(value.Z) || IsInvalidDouble(value.W))
687-
{
688-
return false;
689-
}
690-
691-
return true;
670+
return double.IsFinite(value.X) && double.IsFinite(value.Y) && double.IsFinite(value.Z) && double.IsFinite(value.W);
692671
}
693672

694673
internal static bool IsValidAnimationValueRect(Rect value)
695674
{
696-
if ( IsInvalidDouble(value.Location.X) || IsInvalidDouble(value.Location.Y)
697-
|| IsInvalidDouble(value.Size.Width) || IsInvalidDouble(value.Size.Height)
698-
|| value.IsEmpty)
699-
{
700-
return false;
701-
}
702-
703-
return true;
675+
return double.IsFinite(value.Location.X) && double.IsFinite(value.Location.Y) &&
676+
double.IsFinite(value.Size.Width) && double.IsFinite(value.Size.Height) && !value.IsEmpty;
704677
}
705678

706679
internal static bool IsValidAnimationValueRotation3D(Rotation3D value)
707680
{
708681
return IsValidAnimationValueQuaternion(value.InternalQuaternion);
709682
}
710683

711-
internal static bool IsValidAnimationValueSingle(Single value)
684+
internal static bool IsValidAnimationValueSingle(float value)
712685
{
713-
if (IsInvalidDouble(value))
714-
{
715-
return false;
716-
}
717-
718-
return true;
686+
return double.IsFinite(value);
719687
}
720688

721689
internal static bool IsValidAnimationValueSize(Size value)
722690
{
723-
if (IsInvalidDouble(value.Width) || IsInvalidDouble(value.Height))
724-
{
725-
return false;
726-
}
727-
728-
return true;
691+
return double.IsFinite(value.Width) && double.IsFinite(value.Height);
729692
}
730693

731-
internal static bool IsValidAnimationValueString(String value)
694+
internal static bool IsValidAnimationValueString(string value)
732695
{
733696
return true;
734697
}
735698

736-
internal static bool IsValidAnimationValueVector(System.Windows.Vector value)
699+
internal static bool IsValidAnimationValueVector(Vector value)
737700
{
738-
if (IsInvalidDouble(value.X) || IsInvalidDouble(value.Y))
739-
{
740-
return false;
741-
}
742-
743-
return true;
701+
return double.IsFinite(value.X) && double.IsFinite(value.Y);
744702
}
745703

746704
internal static bool IsValidAnimationValueVector3D(Vector3D value)
747705
{
748-
if (IsInvalidDouble(value.X) || IsInvalidDouble(value.Y) || IsInvalidDouble(value.Z))
749-
{
750-
return false;
751-
}
752-
753-
return true;
706+
return double.IsFinite(value.X) && double.IsFinite(value.Y) && double.IsFinite(value.Z);
754707
}
755708

756709
#endregion
@@ -839,14 +792,5 @@ internal static Rotation3D GetZeroValueRotation3D(Rotation3D baseValue)
839792

840793
#endregion
841794

842-
#region Helpers
843-
844-
private static Boolean IsInvalidDouble(Double value)
845-
{
846-
return Double.IsInfinity(value)
847-
|| double.IsNaN(value);
848-
}
849-
850-
#endregion
851795
}
852796
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/StylusShape.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ internal StylusShape(){}
3838
///</summary>
3939
internal StylusShape(StylusTip tip, double width, double height, double rotation)
4040
{
41-
if (Double.IsNaN(width) || Double.IsInfinity(width) || width < DrawingAttributes.MinWidth || width > DrawingAttributes.MaxWidth)
41+
if (!double.IsFinite(width) || width < DrawingAttributes.MinWidth || width > DrawingAttributes.MaxWidth)
4242
{
4343
throw new ArgumentOutOfRangeException(nameof(width));
4444
}
4545

46-
if (Double.IsNaN(height) || Double.IsInfinity(height) || height < DrawingAttributes.MinHeight || height > DrawingAttributes.MaxHeight)
46+
if (!double.IsFinite(height) || height < DrawingAttributes.MinHeight || height > DrawingAttributes.MaxHeight)
4747
{
4848
throw new ArgumentOutOfRangeException(nameof(height));
4949
}
5050

51-
if (Double.IsNaN(rotation) || Double.IsInfinity(rotation))
51+
if (!double.IsFinite(rotation))
5252
{
5353
throw new ArgumentOutOfRangeException(nameof(rotation));
5454
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InertiaExpansionBehavior.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public double DesiredDeceleration
4949
get { return _desiredDeceleration; }
5050
set
5151
{
52-
if (Double.IsInfinity(value) || Double.IsNaN(value))
52+
if (!double.IsFinite(value))
5353
{
5454
throw new ArgumentOutOfRangeException(nameof(value));
5555
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InertiaRotationBehavior.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public double DesiredDeceleration
4949
get { return _desiredDeceleration; }
5050
set
5151
{
52-
if (Double.IsInfinity(value) || Double.IsNaN(value))
52+
if (!double.IsFinite(value))
5353
{
5454
throw new ArgumentOutOfRangeException(nameof(value));
5555
}
@@ -69,7 +69,7 @@ public double DesiredRotation
6969
get { return _desiredRotation; }
7070
set
7171
{
72-
if (Double.IsInfinity(value) || Double.IsNaN(value))
72+
if (!double.IsFinite(value))
7373
{
7474
throw new ArgumentOutOfRangeException(nameof(value));
7575
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InertiaTranslationBehavior.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public double DesiredDeceleration
4949
get { return _desiredDeceleration; }
5050
set
5151
{
52-
if (Double.IsInfinity(value) || Double.IsNaN(value))
52+
if (!double.IsFinite(value))
5353
{
5454
throw new ArgumentOutOfRangeException(nameof(value));
5555
}
@@ -69,7 +69,7 @@ public double DesiredDisplacement
6969
get { return _desiredDisplacement; }
7070
set
7171
{
72-
if (Double.IsInfinity(value) || Double.IsNaN(value))
72+
if (!double.IsFinite(value))
7373
{
7474
throw new ArgumentOutOfRangeException(nameof(value));
7575
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace System.Windows.Media.Animation
3535
/// <param name="count">The number of iterations specified by this RepeatBehavior.</param>
3636
public RepeatBehavior(double count)
3737
{
38-
if (double.IsInfinity(count) || double.IsNaN(count) || count < 0.0)
38+
if (!double.IsFinite(count) || count < 0.0)
3939
throw new ArgumentOutOfRangeException(nameof(count), SR.Format(SR.Timing_RepeatBehaviorInvalidIterationCount, count));
4040

4141
_repeatDuration = TimeSpan.Zero;

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/MeshGeometry3D.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
//
@@ -404,8 +404,7 @@ ref Point barycentric
404404
return;
405405
}
406406

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

411410
double dist = (worldPointHit - rayParams.Origin).Length;
@@ -490,8 +489,7 @@ ref Point barycentric
490489
return;
491490
}
492491

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

497495
Point3D a = v0, b = v1, c = v2;

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1085,9 +1085,7 @@ internal static double RoundLayoutValue(double value, double dpiScale)
10851085
{
10861086
newValue = Math.Round(value * dpiScale) / dpiScale;
10871087
// If rounding produces a value unacceptable to layout (NaN, Infinity or MaxValue), use the original value.
1088-
if (double.IsNaN(newValue) ||
1089-
Double.IsInfinity(newValue) ||
1090-
DoubleUtil.AreClose(newValue, Double.MaxValue))
1088+
if (!double.IsFinite(newValue) || DoubleUtil.AreClose(newValue, double.MaxValue))
10911089
{
10921090
newValue = value;
10931091
}
@@ -1388,9 +1386,9 @@ private static void RenderTransform_Changed(DependencyObject d, DependencyProper
13881386

13891387
private static bool IsRenderTransformOriginValid(object value)
13901388
{
1391-
Point v = (Point)value;
1392-
return ( (!double.IsNaN(v.X) && !Double.IsPositiveInfinity(v.X) && !Double.IsNegativeInfinity(v.X))
1393-
&& (!double.IsNaN(v.Y) && !Double.IsPositiveInfinity(v.Y) && !Double.IsNegativeInfinity(v.Y)));
1389+
Point origin = (Point)value;
1390+
1391+
return double.IsFinite(origin.X) && double.IsFinite(origin.Y);
13941392
}
13951393

13961394

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AnimatedTypeHelpers.cs

+2-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44

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

114114
#region IsValidAnimationValue Methods
115115

116-
private static bool IsValidAnimationValueDouble(Double value)
117-
{
118-
if (IsInvalidDouble(value))
119-
{
120-
return false;
121-
}
122-
123-
return true;
124-
}
125-
126116
internal static bool IsValidAnimationValueThickness(Thickness value)
127117
{
128118
// At least one of the sub-values must be an interpolatable length.
129-
if ( IsValidAnimationValueDouble(value.Left)
130-
|| IsValidAnimationValueDouble(value.Top)
131-
|| IsValidAnimationValueDouble(value.Right)
132-
|| IsValidAnimationValueDouble(value.Bottom))
133-
{
134-
return true;
135-
}
136-
137-
return false;
119+
return double.IsFinite(value.Left) || double.IsFinite(value.Top) || double.IsFinite(value.Right) || double.IsFinite(value.Bottom);
138120
}
139121

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

158140
#endregion
159141

160-
#region Helpers
161-
162-
private static bool IsInvalidDouble(double value)
163-
{
164-
return Double.IsInfinity(value)
165-
|| double.IsNaN(value);
166-
}
167-
168-
#endregion
169142
}
170143
}

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Helper.cs

-11
Original file line numberDiff line numberDiff line change
@@ -540,17 +540,6 @@ internal static Size ArrangeElementWithSingleChild(UIElement element, Size arran
540540
return arrangeSize;
541541
}
542542

543-
/// <summary>
544-
/// Helper method used for double parameter validation. Returns false
545-
/// if the value is either Infinity (positive or negative) or NaN.
546-
/// </summary>
547-
/// <param name="value">The double value to test</param>
548-
/// <returns>Whether the value is a valid double.</returns>
549-
internal static bool IsDoubleValid(double value)
550-
{
551-
return !(Double.IsInfinity(value) || Double.IsNaN(value));
552-
}
553-
554543
/// <summary>
555544
/// Checks if the given IProvideValueTarget can receive
556545
/// a DynamicResource or Binding MarkupExtension.

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/DocumentGrid.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ public void SetScale(double scale)
422422
{
423423
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(scale, 0.0);
424424

425-
if (!Helper.IsDoubleValid(scale))
425+
if (!double.IsFinite(scale))
426426
{
427427
throw new ArgumentOutOfRangeException(nameof(scale));
428428
}
@@ -798,7 +798,7 @@ public double VerticalPageSpacing
798798

799799
set
800800
{
801-
if (!Helper.IsDoubleValid(value))
801+
if (!double.IsFinite(value))
802802
{
803803
throw new ArgumentOutOfRangeException(nameof(value));
804804
}
@@ -820,7 +820,7 @@ public double HorizontalPageSpacing
820820

821821
set
822822
{
823-
if (!Helper.IsDoubleValid(value))
823+
if (!double.IsFinite(value))
824824
{
825825
throw new ArgumentOutOfRangeException(nameof(value));
826826
}

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/GridSplitterAutomationPeer.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ void ITransformProvider.Move(double x, double y)
3939
if (!IsEnabled())
4040
throw new ElementNotEnabledException();
4141

42-
if (double.IsInfinity(x) || double.IsNaN(x))
42+
if (!double.IsFinite(x))
4343
throw new ArgumentOutOfRangeException(nameof(x));
4444

45-
if (double.IsInfinity(y) || double.IsNaN(y))
45+
if (!double.IsFinite(y))
4646
throw new ArgumentOutOfRangeException(nameof(y));
4747

4848
((GridSplitter)Owner).KeyboardMoveSplitter(x, y);

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Border.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ public static readonly DependencyProperty CornerRadiusProperty
159159
private static bool IsCornerRadiusValid(object value)
160160
{
161161
CornerRadius cr = (CornerRadius)value;
162-
return (cr.IsValid(false, false, false, false));
162+
163+
return cr.IsValid();
163164
}
164165

165166
/// <summary>

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridCellsPanel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,7 @@ private double GetViewportWidth()
20142014
if (DoubleUtil.AreClose(availableViewportWidth, 0.0) && parentRowsPresenter != null)
20152015
{
20162016
Size rowPresenterAvailableSize = parentRowsPresenter.AvailableSize;
2017-
if (!double.IsNaN(rowPresenterAvailableSize.Width) && !Double.IsInfinity(rowPresenterAvailableSize.Width))
2017+
if (double.IsFinite(rowPresenterAvailableSize.Width))
20182018
{
20192019
availableViewportWidth = rowPresenterAvailableSize.Width;
20202020
}

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridLength.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public DataGridLength(double value, DataGridLengthUnitType type)
7272
/// </exception>
7373
public DataGridLength(double value, DataGridLengthUnitType type, double desiredValue, double displayValue)
7474
{
75-
if (double.IsNaN(value) || Double.IsInfinity(value))
75+
if (!double.IsFinite(value))
7676
{
7777
throw new ArgumentException(
7878
SR.DataGridLength_Infinity,

0 commit comments

Comments
 (0)