Skip to content

Commit 9967441

Browse files
committed
Simplify generic floating point->integer conversions
Floating point to integer conversions for 32-bit and 64-bit integers have predictable truncating behavior on all platforms now. The manual truncation should be unnecessary. Keep the existing behavior for Mono since Mono has not been fixed to have the predictable truncating behavior.
1 parent 96e283d commit 9967441

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,16 +1420,24 @@ private static bool TryConvertTo<TOther>(double value, [MaybeNullWhen(false)] ou
14201420
}
14211421
else if (typeof(TOther) == typeof(uint))
14221422
{
1423+
#if MONO
14231424
uint actualResult = (value >= uint.MaxValue) ? uint.MaxValue :
14241425
(value <= uint.MinValue) ? uint.MinValue : (uint)value;
1426+
#else
1427+
uint actualResult = (uint)value;
1428+
#endif
14251429
result = (TOther)(object)actualResult;
14261430
return true;
14271431
}
14281432
else if (typeof(TOther) == typeof(ulong))
14291433
{
1434+
#if MONO
14301435
ulong actualResult = (value >= ulong.MaxValue) ? ulong.MaxValue :
14311436
(value <= ulong.MinValue) ? ulong.MinValue :
14321437
IsNaN(value) ? 0 : (ulong)value;
1438+
#else
1439+
ulong actualResult = (ulong)value;
1440+
#endif
14331441
result = (TOther)(object)actualResult;
14341442
return true;
14351443
}
@@ -1442,6 +1450,7 @@ private static bool TryConvertTo<TOther>(double value, [MaybeNullWhen(false)] ou
14421450
}
14431451
else if (typeof(TOther) == typeof(nuint))
14441452
{
1453+
#if MONO
14451454
#if TARGET_64BIT
14461455
nuint actualResult = (value >= ulong.MaxValue) ? unchecked((nuint)ulong.MaxValue) :
14471456
(value <= ulong.MinValue) ? unchecked((nuint)ulong.MinValue) : (nuint)value;
@@ -1453,6 +1462,11 @@ private static bool TryConvertTo<TOther>(double value, [MaybeNullWhen(false)] ou
14531462
result = (TOther)(object)actualResult;
14541463
return true;
14551464
#endif
1465+
#else
1466+
nuint actualResult = (nuint)value;
1467+
#endif
1468+
result = (TOther)(object)actualResult;
1469+
return true;
14561470
}
14571471
else
14581472
{

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,8 +1002,12 @@ private static bool TryConvertFromSaturating<TOther>(TOther value, out int resul
10021002
if (typeof(TOther) == typeof(double))
10031003
{
10041004
double actualValue = (double)(object)value;
1005+
#if MONO
10051006
result = (actualValue >= MaxValue) ? MaxValue :
10061007
(actualValue <= MinValue) ? MinValue : (int)actualValue;
1008+
#else
1009+
result = (int)actualValue;
1010+
#endif
10071011
return true;
10081012
}
10091013
else if (typeof(TOther) == typeof(Half))
@@ -1049,8 +1053,12 @@ private static bool TryConvertFromSaturating<TOther>(TOther value, out int resul
10491053
else if (typeof(TOther) == typeof(float))
10501054
{
10511055
float actualValue = (float)(object)value;
1056+
#if MONO
10521057
result = (actualValue >= MaxValue) ? MaxValue :
10531058
(actualValue <= MinValue) ? MinValue : (int)actualValue;
1059+
#else
1060+
result = (int)actualValue;
1061+
#endif
10541062
return true;
10551063
}
10561064
else
@@ -1080,8 +1088,12 @@ private static bool TryConvertFromTruncating<TOther>(TOther value, out int resul
10801088
if (typeof(TOther) == typeof(double))
10811089
{
10821090
double actualValue = (double)(object)value;
1091+
#if MONO
10831092
result = (actualValue >= MaxValue) ? MaxValue :
10841093
(actualValue <= MinValue) ? MinValue : (int)actualValue;
1094+
#else
1095+
result = (int)actualValue;
1096+
#endif
10851097
return true;
10861098
}
10871099
else if (typeof(TOther) == typeof(Half))
@@ -1124,8 +1136,12 @@ private static bool TryConvertFromTruncating<TOther>(TOther value, out int resul
11241136
else if (typeof(TOther) == typeof(float))
11251137
{
11261138
float actualValue = (float)(object)value;
1139+
#if MONO
11271140
result = (actualValue >= MaxValue) ? MaxValue :
11281141
(actualValue <= MinValue) ? MinValue : (int)actualValue;
1142+
#else
1143+
result = (int)actualValue;
1144+
#endif
11291145
return true;
11301146
}
11311147
else

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,8 +999,12 @@ private static bool TryConvertFromSaturating<TOther>(TOther value, out long resu
999999
if (typeof(TOther) == typeof(double))
10001000
{
10011001
double actualValue = (double)(object)value;
1002+
#if MONO
10021003
result = (actualValue >= MaxValue) ? MaxValue :
10031004
(actualValue <= MinValue) ? MinValue : (long)actualValue;
1005+
#else
1006+
result = (long)actualValue;
1007+
#endif
10041008
return true;
10051009
}
10061010
else if (typeof(TOther) == typeof(Half))
@@ -1044,8 +1048,12 @@ private static bool TryConvertFromSaturating<TOther>(TOther value, out long resu
10441048
else if (typeof(TOther) == typeof(float))
10451049
{
10461050
float actualValue = (float)(object)value;
1051+
#if MONO
10471052
result = (actualValue >= MaxValue) ? MaxValue :
10481053
(actualValue <= MinValue) ? MinValue : (long)actualValue;
1054+
#else
1055+
result = (long)actualValue;
1056+
#endif
10491057
return true;
10501058
}
10511059
else
@@ -1075,8 +1083,12 @@ private static bool TryConvertFromTruncating<TOther>(TOther value, out long resu
10751083
if (typeof(TOther) == typeof(double))
10761084
{
10771085
double actualValue = (double)(object)value;
1086+
#if MONO
10781087
result = (actualValue >= MaxValue) ? MaxValue :
10791088
(actualValue <= MinValue) ? MinValue : (long)actualValue;
1089+
#else
1090+
result = (long)actualValue;
1091+
#endif
10801092
return true;
10811093
}
10821094
else if (typeof(TOther) == typeof(Half))
@@ -1119,8 +1131,12 @@ private static bool TryConvertFromTruncating<TOther>(TOther value, out long resu
11191131
else if (typeof(TOther) == typeof(float))
11201132
{
11211133
float actualValue = (float)(object)value;
1134+
#if MONO
11221135
result = (actualValue >= MaxValue) ? MaxValue :
11231136
(actualValue <= MinValue) ? MinValue : (long)actualValue;
1137+
#else
1138+
result = (long)actualValue;
1139+
#endif
11241140
return true;
11251141
}
11261142
else

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,8 +994,12 @@ private static bool TryConvertFromSaturating<TOther>(TOther value, out nint resu
994994
if (typeof(TOther) == typeof(double))
995995
{
996996
double actualValue = (double)(object)value;
997+
#if MONO
997998
result = (actualValue >= nint_t.MaxValue) ? unchecked((nint)nint_t.MaxValue) :
998999
(actualValue <= nint_t.MinValue) ? unchecked((nint)nint_t.MinValue) : (nint)actualValue;
1000+
#else
1001+
result = (nint)actualValue;
1002+
#endif
9991003
return true;
10001004
}
10011005
else if (typeof(TOther) == typeof(Half))
@@ -1040,8 +1044,12 @@ private static bool TryConvertFromSaturating<TOther>(TOther value, out nint resu
10401044
else if (typeof(TOther) == typeof(float))
10411045
{
10421046
float actualValue = (float)(object)value;
1047+
#if MONO
10431048
result = (actualValue >= nint_t.MaxValue) ? unchecked((nint)nint_t.MaxValue) :
10441049
(actualValue <= nint_t.MinValue) ? unchecked((nint)nint_t.MinValue) : (nint)actualValue;
1050+
#else
1051+
result = (nint)actualValue;
1052+
#endif
10451053
return true;
10461054
}
10471055
else
@@ -1071,8 +1079,12 @@ private static bool TryConvertFromTruncating<TOther>(TOther value, out nint resu
10711079
if (typeof(TOther) == typeof(double))
10721080
{
10731081
double actualValue = (double)(object)value;
1082+
#if MONO
10741083
result = (actualValue >= nint_t.MaxValue) ? unchecked((nint)nint_t.MaxValue) :
10751084
(actualValue <= nint_t.MinValue) ? unchecked((nint)nint_t.MinValue) : (nint)actualValue;
1085+
#else
1086+
result = (nint)actualValue;
1087+
#endif
10761088
return true;
10771089
}
10781090
else if (typeof(TOther) == typeof(Half))
@@ -1115,8 +1127,12 @@ private static bool TryConvertFromTruncating<TOther>(TOther value, out nint resu
11151127
else if (typeof(TOther) == typeof(float))
11161128
{
11171129
float actualValue = (float)(object)value;
1130+
#if MONO
11181131
result = (actualValue >= nint_t.MaxValue) ? unchecked((nint)nint_t.MaxValue) :
11191132
(actualValue <= nint_t.MinValue) ? unchecked((nint)nint_t.MinValue) : (nint)actualValue;
1133+
#else
1134+
result = (nint)actualValue;
1135+
#endif
11201136
return true;
11211137
}
11221138
else

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,16 +1439,24 @@ private static bool TryConvertTo<TOther>(float value, [MaybeNullWhen(false)] out
14391439
}
14401440
else if (typeof(TOther) == typeof(uint))
14411441
{
1442+
#if MONO
14421443
uint actualResult = (value >= uint.MaxValue) ? uint.MaxValue :
14431444
(value <= uint.MinValue) ? uint.MinValue : (uint)value;
1445+
#else
1446+
uint actualResult = (uint)value;
1447+
#endif
14441448
result = (TOther)(object)actualResult;
14451449
return true;
14461450
}
14471451
else if (typeof(TOther) == typeof(ulong))
14481452
{
1453+
#if MONO
14491454
ulong actualResult = (value >= ulong.MaxValue) ? ulong.MaxValue :
14501455
(value <= ulong.MinValue) ? ulong.MinValue :
14511456
IsNaN(value) ? 0 : (ulong)value;
1457+
#else
1458+
ulong actualResult = (ulong)value;
1459+
#endif
14521460
result = (TOther)(object)actualResult;
14531461
return true;
14541462
}
@@ -1461,6 +1469,7 @@ private static bool TryConvertTo<TOther>(float value, [MaybeNullWhen(false)] out
14611469
}
14621470
else if (typeof(TOther) == typeof(nuint))
14631471
{
1472+
#if MONO
14641473
#if TARGET_64BIT
14651474
nuint actualResult = (value >= ulong.MaxValue) ? unchecked((nuint)ulong.MaxValue) :
14661475
(value <= ulong.MinValue) ? unchecked((nuint)ulong.MinValue) : (nuint)value;
@@ -1472,6 +1481,11 @@ private static bool TryConvertTo<TOther>(float value, [MaybeNullWhen(false)] out
14721481
result = (TOther)(object)actualResult;
14731482
return true;
14741483
#endif
1484+
#else
1485+
nuint actualResult = (nuint)value;
1486+
#endif
1487+
result = (TOther)(object)actualResult;
1488+
return true;
14751489
}
14761490
else
14771491
{

0 commit comments

Comments
 (0)