Skip to content

Commit 8234fe3

Browse files
authored
Simplify TryConvert for Half, Int128 and UInt128 (#116902)
1 parent 9a4be5b commit 8234fe3

File tree

7 files changed

+44
-16
lines changed

7 files changed

+44
-16
lines changed

src/libraries/System.Private.CoreLib/src/System/Double.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,8 +1443,7 @@ private static bool TryConvertTo<TOther>(double value, [MaybeNullWhen(false)] ou
14431443
}
14441444
else if (typeof(TOther) == typeof(UInt128))
14451445
{
1446-
UInt128 actualResult = (value >= 340282366920938463463374607431768211455.0) ? UInt128.MaxValue :
1447-
(value <= 0.0) ? UInt128.MinValue : (UInt128)value;
1446+
UInt128 actualResult = (UInt128)value;
14481447
result = (TOther)(object)actualResult;
14491448
return true;
14501449
}

src/libraries/System.Private.CoreLib/src/System/Half.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,16 +2122,24 @@ private static bool TryConvertTo<TOther>(Half value, [MaybeNullWhen(false)] out
21222122
}
21232123
else if (typeof(TOther) == typeof(uint))
21242124
{
2125+
#if MONO
21252126
uint actualResult = (value == PositiveInfinity) ? uint.MaxValue :
21262127
(value <= Zero) ? uint.MinValue : (uint)value;
2128+
#else
2129+
uint actualResult = (uint)value;
2130+
#endif
21272131
result = (TOther)(object)actualResult;
21282132
return true;
21292133
}
21302134
else if (typeof(TOther) == typeof(ulong))
21312135
{
2136+
#if MONO
21322137
ulong actualResult = (value == PositiveInfinity) ? ulong.MaxValue :
21332138
(value <= Zero) ? ulong.MinValue :
21342139
IsNaN(value) ? 0 : (ulong)value;
2140+
#else
2141+
ulong actualResult = (ulong)value;
2142+
#endif
21352143
result = (TOther)(object)actualResult;
21362144
return true;
21372145
}
@@ -2144,8 +2152,12 @@ private static bool TryConvertTo<TOther>(Half value, [MaybeNullWhen(false)] out
21442152
}
21452153
else if (typeof(TOther) == typeof(nuint))
21462154
{
2155+
#if MONO
21472156
nuint actualResult = (value == PositiveInfinity) ? nuint.MaxValue :
21482157
(value <= Zero) ? nuint.MinValue : (nuint)value;
2158+
#else
2159+
nuint actualResult = (nuint)value;
2160+
#endif
21492161
result = (TOther)(object)actualResult;
21502162
return true;
21512163
}

src/libraries/System.Private.CoreLib/src/System/Int128.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,15 +1580,13 @@ private static bool TryConvertFromSaturating<TOther>(TOther value, out Int128 re
15801580
if (typeof(TOther) == typeof(double))
15811581
{
15821582
double actualValue = (double)(object)value;
1583-
result = (actualValue >= +170141183460469231731687303715884105727.0) ? MaxValue :
1584-
(actualValue <= -170141183460469231731687303715884105728.0) ? MinValue : (Int128)actualValue;
1583+
result = (Int128)actualValue;
15851584
return true;
15861585
}
15871586
else if (typeof(TOther) == typeof(Half))
15881587
{
15891588
Half actualValue = (Half)(object)value;
1590-
result = (actualValue == Half.PositiveInfinity) ? MaxValue :
1591-
(actualValue == Half.NegativeInfinity) ? MinValue : (Int128)actualValue;
1589+
result = (Int128)actualValue;
15921590
return true;
15931591
}
15941592
else if (typeof(TOther) == typeof(short))
@@ -1624,8 +1622,7 @@ private static bool TryConvertFromSaturating<TOther>(TOther value, out Int128 re
16241622
else if (typeof(TOther) == typeof(float))
16251623
{
16261624
float actualValue = (float)(object)value;
1627-
result = (actualValue >= +170141183460469231731687303715884105727.0f) ? MaxValue :
1628-
(actualValue <= -170141183460469231731687303715884105728.0f) ? MinValue : (Int128)actualValue;
1625+
result = (Int128)actualValue;
16291626
return true;
16301627
}
16311628
else
@@ -1655,15 +1652,13 @@ private static bool TryConvertFromTruncating<TOther>(TOther value, out Int128 re
16551652
if (typeof(TOther) == typeof(double))
16561653
{
16571654
double actualValue = (double)(object)value;
1658-
result = (actualValue >= +170141183460469231731687303715884105727.0) ? MaxValue :
1659-
(actualValue <= -170141183460469231731687303715884105728.0) ? MinValue : (Int128)actualValue;
1655+
result = (Int128)actualValue;
16601656
return true;
16611657
}
16621658
else if (typeof(TOther) == typeof(Half))
16631659
{
16641660
Half actualValue = (Half)(object)value;
1665-
result = (actualValue == Half.PositiveInfinity) ? MaxValue :
1666-
(actualValue == Half.NegativeInfinity) ? MinValue : (Int128)actualValue;
1661+
result = (Int128)actualValue;
16671662
return true;
16681663
}
16691664
else if (typeof(TOther) == typeof(short))
@@ -1699,8 +1694,7 @@ private static bool TryConvertFromTruncating<TOther>(TOther value, out Int128 re
16991694
else if (typeof(TOther) == typeof(float))
17001695
{
17011696
float actualValue = (float)(object)value;
1702-
result = (actualValue >= +170141183460469231731687303715884105727.0f) ? MaxValue :
1703-
(actualValue <= -170141183460469231731687303715884105728.0f) ? MinValue : (Int128)actualValue;
1697+
result = (Int128)actualValue;
17041698
return true;
17051699
}
17061700
else

src/libraries/System.Private.CoreLib/src/System/Int32.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,8 +1013,12 @@ private static bool TryConvertFromSaturating<TOther>(TOther value, out int resul
10131013
else if (typeof(TOther) == typeof(Half))
10141014
{
10151015
Half actualValue = (Half)(object)value;
1016+
#if MONO
10161017
result = (actualValue == Half.PositiveInfinity) ? MaxValue :
10171018
(actualValue == Half.NegativeInfinity) ? MinValue : (int)actualValue;
1019+
#else
1020+
result = (int)actualValue;
1021+
#endif
10181022
return true;
10191023
}
10201024
else if (typeof(TOther) == typeof(short))
@@ -1099,8 +1103,12 @@ private static bool TryConvertFromTruncating<TOther>(TOther value, out int resul
10991103
else if (typeof(TOther) == typeof(Half))
11001104
{
11011105
Half actualValue = (Half)(object)value;
1106+
#if MONO
11021107
result = (actualValue == Half.PositiveInfinity) ? MaxValue :
11031108
(actualValue == Half.NegativeInfinity) ? MinValue : (int)actualValue;
1109+
#else
1110+
result = (int)actualValue;
1111+
#endif
11041112
return true;
11051113
}
11061114
else if (typeof(TOther) == typeof(short))

src/libraries/System.Private.CoreLib/src/System/Int64.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,8 +1010,12 @@ private static bool TryConvertFromSaturating<TOther>(TOther value, out long resu
10101010
else if (typeof(TOther) == typeof(Half))
10111011
{
10121012
Half actualValue = (Half)(object)value;
1013+
#if MONO
10131014
result = (actualValue == Half.PositiveInfinity) ? MaxValue :
10141015
(actualValue == Half.NegativeInfinity) ? MinValue : (long)actualValue;
1016+
#else
1017+
result = (long)actualValue;
1018+
#endif
10151019
return true;
10161020
}
10171021
else if (typeof(TOther) == typeof(short))
@@ -1094,8 +1098,12 @@ private static bool TryConvertFromTruncating<TOther>(TOther value, out long resu
10941098
else if (typeof(TOther) == typeof(Half))
10951099
{
10961100
Half actualValue = (Half)(object)value;
1101+
#if MONO
10971102
result = (actualValue == Half.PositiveInfinity) ? MaxValue :
10981103
(actualValue == Half.NegativeInfinity) ? MinValue : (long)actualValue;
1104+
#else
1105+
result = (long)actualValue;
1106+
#endif
10991107
return true;
11001108
}
11011109
else if (typeof(TOther) == typeof(short))

src/libraries/System.Private.CoreLib/src/System/IntPtr.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,8 +1005,12 @@ private static bool TryConvertFromSaturating<TOther>(TOther value, out nint resu
10051005
else if (typeof(TOther) == typeof(Half))
10061006
{
10071007
Half actualValue = (Half)(object)value;
1008+
#if MONO
10081009
result = (actualValue == Half.PositiveInfinity) ? unchecked((nint)nint_t.MaxValue) :
10091010
(actualValue == Half.NegativeInfinity) ? unchecked((nint)nint_t.MinValue) : (nint)actualValue;
1011+
#else
1012+
result = (nint)actualValue;
1013+
#endif
10101014
return true;
10111015
}
10121016
else if (typeof(TOther) == typeof(short))
@@ -1090,8 +1094,12 @@ private static bool TryConvertFromTruncating<TOther>(TOther value, out nint resu
10901094
else if (typeof(TOther) == typeof(Half))
10911095
{
10921096
Half actualValue = (Half)(object)value;
1097+
#if MONO
10931098
result = (actualValue == Half.PositiveInfinity) ? unchecked((nint)nint_t.MaxValue) :
10941099
(actualValue == Half.NegativeInfinity) ? unchecked((nint)nint_t.MinValue) : (nint)actualValue;
1100+
#else
1101+
result = (nint)actualValue;
1102+
#endif
10951103
return true;
10961104
}
10971105
else if (typeof(TOther) == typeof(short))

src/libraries/System.Private.CoreLib/src/System/Single.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,8 +1462,7 @@ private static bool TryConvertTo<TOther>(float value, [MaybeNullWhen(false)] out
14621462
}
14631463
else if (typeof(TOther) == typeof(UInt128))
14641464
{
1465-
UInt128 actualResult = (value == PositiveInfinity) ? UInt128.MaxValue :
1466-
(value <= 0.0f) ? UInt128.MinValue : (UInt128)value;
1465+
UInt128 actualResult = (UInt128)value;
14671466
result = (TOther)(object)actualResult;
14681467
return true;
14691468
}

0 commit comments

Comments
 (0)