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

Use Roslyn support for RuntimeHelpers.CreateSpan #70179

Closed
wants to merge 1 commit into from
Closed
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 @@ -119,34 +119,32 @@ internal static bool IsRelational(int op)
/// Mapping from Operator to priorities
/// CONSIDER: fast, but hard to maintain
/// </summary>

private static readonly int[] s_priority = new int[] {
priStart, // Noop
priNeg, priNeg, priNot, // Unary -, +, Not
priBetweenAnd, priBetweenInLike, priBetweenInLike,
priRelOp, priRelOp, priRelOp, priRelOp, priRelOp, priRelOp,
priIs,
priBetweenInLike, // Like

priPlusMinus, priPlusMinus, // +, -
priMulDiv, priMulDiv, priIDiv, priMod, // *, /, \, Mod
priExp, // **

priAnd, priOr, priXor, priNot,
priAnd, priOr,

priParen, priProc, priDot, priDot, // Proc, Iff, Qula, Dot..

priMax, priMax, priMax, priMax, priMax, priMax, priMax,
priMax, priMax, priMax, priMax,
priMax,
};

internal static int Priority(int op)
{
if ((uint)op >= (uint)s_priority.Length)
return priMax;
return s_priority[op];
ReadOnlySpan<int> priority = new int[]
{
priStart, // Noop
priNeg, priNeg, priNot, // Unary -, +, Not
priBetweenAnd, priBetweenInLike, priBetweenInLike,
priRelOp, priRelOp, priRelOp, priRelOp, priRelOp, priRelOp,
priIs,
priBetweenInLike, // Like

priPlusMinus, priPlusMinus, // +, -
priMulDiv, priMulDiv, priIDiv, priMod, // *, /, \, Mod
priExp, // **

priAnd, priOr, priXor, priNot,
priAnd, priOr,

priParen, priProc, priDot, priDot, // Proc, Iff, Qula, Dot..

priMax, priMax, priMax, priMax, priMax, priMax, priMax,
priMax, priMax, priMax, priMax,
priMax,
};

return (uint)op < (uint)priority.Length ? priority[op] : priMax;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public struct SqlDateTime : INullable, IComparable, IXmlSerializable, IEquatable
public static readonly int SQLTicksPerHour = SQLTicksPerMinute * 60;
private static readonly int s_SQLTicksPerDay = SQLTicksPerHour * 24;

private const long s_ticksPerSecond = TimeSpan.TicksPerMillisecond * 1000;

private static readonly DateTime s_SQLBaseDate = new DateTime(1900, 1, 1);
private static readonly long s_SQLBaseDateTicks = s_SQLBaseDate.Ticks;

Expand All @@ -51,17 +49,14 @@ public struct SqlDateTime : INullable, IComparable, IXmlSerializable, IEquatable

private const int s_dayBase = 693595; // Jan 1 1900 is this many days from Jan 1 0001


private static readonly int[] s_daysToMonth365 = new int[] {
private static ReadOnlySpan<int> DaysToMonth365 => new int[] {
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
private static readonly int[] s_daysToMonth366 = new int[] {
private static ReadOnlySpan<int> DaysToMonth366 => new int[] {
0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};

private static readonly DateTime s_minDateTime = new DateTime(1753, 1, 1);
private static readonly DateTime s_maxDateTime = DateTime.MaxValue;
private static readonly TimeSpan s_minTimeSpan = s_minDateTime.Subtract(s_SQLBaseDate);
private static readonly TimeSpan s_maxTimeSpan = s_maxDateTime.Subtract(s_SQLBaseDate);
private const string s_ISO8601_DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fff";
private static readonly TimeSpan s_minTimeSpan = new DateTime(1753, 1, 1).Subtract(s_SQLBaseDate);
private static readonly TimeSpan s_maxTimeSpan = DateTime.MaxValue.Subtract(s_SQLBaseDate);
private const string ISO8601_DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fff";

// These formats are valid styles in SQL Server (style 9, 12, 13, 14)
// but couldn't be recognized by the default parse. Needs to call
Expand Down Expand Up @@ -105,7 +100,9 @@ public SqlDateTime(int year, int month, int day, int hour, int minute, int secon
{
if (year >= s_minYear && year <= s_maxYear && month >= 1 && month <= 12)
{
int[] days = IsLeapYear(year) ? s_daysToMonth366 : s_daysToMonth365;
ReadOnlySpan<int> days = IsLeapYear(year) ?
DaysToMonth366 :
DaysToMonth365;
if (day >= 1 && day <= days[month] - days[month - 1])
{
int y = year - 1;
Expand Down Expand Up @@ -670,7 +667,7 @@ void IXmlSerializable.WriteXml(XmlWriter writer)
}
else
{
writer.WriteString(XmlConvert.ToString(Value, s_ISO8601_DateTimeFormat));
writer.WriteString(XmlConvert.ToString(Value, ISO8601_DateTimeFormat));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public struct SqlDecimal : INullable, IComparable, IXmlSerializable, IEquatable<
private const byte s_cNumeDivScaleMin = 6; // Minimum result scale of numeric division

// Array of multipliers for lAdjust and Ceiling/Floor.
private static readonly uint[] s_rgulShiftBase = new uint[9] {
private static ReadOnlySpan<uint> RgulShiftBase => new uint[9] {
10,
10 * 10,
10 * 10 * 10,
Expand Down Expand Up @@ -130,7 +130,7 @@ public static void CreateDecimalHelperTable()
#endregion

#region DecimalHelperTable
private static readonly uint[] s_decimalHelpersLo = {
private static ReadOnlySpan<uint> DecimalHelpersLo => new uint[] {
0x0000000a, // precision:2, value:10
0x00000064, // precision:3, value:100
0x000003e8, // precision:4, value:1000
Expand Down Expand Up @@ -171,7 +171,7 @@ public static void CreateDecimalHelperTable()
0x00000000, // precision:38+1, value:99999999999999999999999999999999999999+1
};

private static readonly uint[] s_decimalHelpersMid = {
private static ReadOnlySpan<uint> DecimalHelpersMid => new uint[] {
0x00000000, // precision:2, value:10
0x00000000, // precision:3, value:100
0x00000000, // precision:4, value:1000
Expand Down Expand Up @@ -212,7 +212,7 @@ public static void CreateDecimalHelperTable()
0x098a2240, // precision:38+1, value:99999999999999999999999999999999999999+1
};

private static readonly uint[] s_decimalHelpersHi = {
private static ReadOnlySpan<uint> DecimalHelpersHi => new uint[] {
0x00000000, // precision:2, value:10
0x00000000, // precision:3, value:100
0x00000000, // precision:4, value:1000
Expand Down Expand Up @@ -253,7 +253,7 @@ public static void CreateDecimalHelperTable()
0x5a86c47a, // precision:38+1, value:99999999999999999999999999999999999999+1
};

private static readonly uint[] s_decimalHelpersHiHi = {
private static ReadOnlySpan<uint> DecimalHelpersHiHi => new uint[] {
0x00000000, // precision:2, value:10
0x00000000, // precision:3, value:100
0x00000000, // precision:4, value:1000
Expand Down Expand Up @@ -313,31 +313,31 @@ private byte CalculatePrecision()
{
int tableIndex;
byte precision;
uint[] decimalHelpers;
ReadOnlySpan<uint> decimalHelpers;
uint decimalPart;

if (_data4 != 0)
{
tableIndex = HelperTableStartIndexHiHi;
decimalHelpers = s_decimalHelpersHiHi;
decimalHelpers = DecimalHelpersHiHi;
decimalPart = _data4;
}
else if (_data3 != 0)
{
tableIndex = HelperTableStartIndexHi;
decimalHelpers = s_decimalHelpersHi;
decimalHelpers = DecimalHelpersHi;
decimalPart = _data3;
}
else if (_data2 != 0)
{
tableIndex = HelperTableStartIndexMid;
decimalHelpers = s_decimalHelpersMid;
decimalHelpers = DecimalHelpersMid;
decimalPart = _data2;
}
else
{
tableIndex = HelperTableStartIndexLo;
decimalHelpers = s_decimalHelpersLo;
decimalHelpers = DecimalHelpersLo;
decimalPart = _data1;
}

Expand Down Expand Up @@ -429,25 +429,25 @@ private bool VerifyPrecision(byte precision)
Debug.Assert(precision <= MaxPrecision, "Precision > MaxPrecision");

int tableIndex = checked((precision - 1));
if (_data4 < s_decimalHelpersHiHi[tableIndex])
if (_data4 < DecimalHelpersHiHi[tableIndex])
{
return true;
}
else if (_data4 == s_decimalHelpersHiHi[tableIndex])
else if (_data4 == DecimalHelpersHiHi[tableIndex])
{
if (_data3 < s_decimalHelpersHi[tableIndex])
if (_data3 < DecimalHelpersHi[tableIndex])
{
return true;
}
else if (_data3 == s_decimalHelpersHi[tableIndex])
else if (_data3 == DecimalHelpersHi[tableIndex])
{
if (_data2 < s_decimalHelpersMid[tableIndex])
if (_data2 < DecimalHelpersMid[tableIndex])
{
return true;
}
else if (_data2 == s_decimalHelpersMid[tableIndex])
else if (_data2 == DecimalHelpersMid[tableIndex])
{
if (_data1 < s_decimalHelpersLo[tableIndex])
if (_data1 < DecimalHelpersLo[tableIndex])
{
return true;
}
Expand Down Expand Up @@ -756,9 +756,9 @@ public SqlDecimal(double dVal) : this(false)
{
ulLenDelta = (ulLen >= 9) ? 9 : ulLen;

dFrac *= s_rgulShiftBase[(int)ulLenDelta - 1];
dFrac *= RgulShiftBase[(int)ulLenDelta - 1];
ulLen -= ulLenDelta;
MultByULong(s_rgulShiftBase[(int)ulLenDelta - 1]);
MultByULong(RgulShiftBase[(int)ulLenDelta - 1]);
AddULong((uint)dFrac);
dFrac -= Math.Floor(dFrac);
}
Expand Down Expand Up @@ -1525,12 +1525,12 @@ public static explicit operator decimal(SqlDecimal x)
{
if (lScaleAdjust <= -9)
{
ulShiftBase = s_rgulShiftBase[8];
ulShiftBase = RgulShiftBase[8];
lScaleAdjust += 9;
}
else
{
ulShiftBase = s_rgulShiftBase[-lScaleAdjust - 1];
ulShiftBase = RgulShiftBase[-lScaleAdjust - 1];
lScaleAdjust = 0;
}
MpDiv1(rgulRes, ref culRes, ulShiftBase, out ulRem);
Expand Down Expand Up @@ -2287,12 +2287,12 @@ internal void AdjustScale(int digits, bool fRound)
//if lAdjust>=9, downshift by 10^9 each time, otherwise by the full amount
if (lAdjust >= 9)
{
ulShiftBase = s_rgulShiftBase[8];
ulShiftBase = RgulShiftBase[8];
lAdjust -= 9;
}
else
{
ulShiftBase = s_rgulShiftBase[lAdjust - 1];
ulShiftBase = RgulShiftBase[lAdjust - 1];
lAdjust = 0;
}
MultByULong(ulShiftBase);
Expand All @@ -2304,12 +2304,12 @@ internal void AdjustScale(int digits, bool fRound)
{
if (lAdjust <= -9)
{
ulShiftBase = s_rgulShiftBase[8];
ulShiftBase = RgulShiftBase[8];
lAdjust += 9;
}
else
{
ulShiftBase = s_rgulShiftBase[-lAdjust - 1];
ulShiftBase = RgulShiftBase[-lAdjust - 1];
lAdjust = 0;
}
ulRem = DivByULong(ulShiftBase);
Expand Down Expand Up @@ -3034,12 +3034,12 @@ private void MakeInteger(out bool fFraction)
{
if (iAdjust >= 9)
{
ulRem = DivByULong(s_rgulShiftBase[8]);
ulRem = DivByULong(RgulShiftBase[8]);
iAdjust -= 9;
}
else
{
ulRem = DivByULong(s_rgulShiftBase[iAdjust - 1]);
ulRem = DivByULong(RgulShiftBase[iAdjust - 1]);
iAdjust = 0;
}

Expand Down Expand Up @@ -3172,14 +3172,14 @@ private static SqlDecimal Round(SqlDecimal n, int lPosition, bool fTruncate)
{
if (lAdjust >= 9)
{
ulRem = n.DivByULong(s_rgulShiftBase[8]);
ulLastDivBase = s_rgulShiftBase[8];
ulRem = n.DivByULong(RgulShiftBase[8]);
ulLastDivBase = RgulShiftBase[8];
lAdjust -= 9;
}
else
{
ulRem = n.DivByULong(s_rgulShiftBase[lAdjust - 1]);
ulLastDivBase = s_rgulShiftBase[lAdjust - 1];
ulRem = n.DivByULong(RgulShiftBase[lAdjust - 1]);
ulLastDivBase = RgulShiftBase[lAdjust - 1];
lAdjust = 0;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,22 @@ private unsafe FileVersionInfo(string fileName)
uint langid = GetVarEntry(memIntPtr);
if (!GetVersionInfoForCodePage(memIntPtr, ConvertTo8DigitHex(langid)))
{
// Some dlls might not contain correct codepage information,
// in which case the lookup will fail. Explorer will take
// a few shots in dark. We'll simulate similar behavior by
// falling back to the following lang-codepages:
ReadOnlySpan<uint> fallbackLanguageCodePages = new uint[]
{
0x040904B0, // US English + CP_UNICODE
0x040904E4, // US English + CP_USASCII
0x04090000 // US English + unknown codepage
};

// Some DLLs might not contain correct codepage information. In these cases we will fail during lookup.
// Explorer will take a few shots in dark by trying several specific lang-codepages
// (Explorer also randomly guesses 041D04B0=Swedish+CP_UNICODE and 040704B0=German+CP_UNICODE sometimes).
// We will try to simulate similar behavior here.
foreach (uint id in s_fallbackLanguageCodePages)
foreach (uint id in fallbackLanguageCodePages)
{
if (id != langid)
{
Expand All @@ -52,17 +63,6 @@ private unsafe FileVersionInfo(string fileName)
}
}

// Some dlls might not contain correct codepage information,
// in which case the lookup will fail. Explorer will take
// a few shots in dark. We'll simulate similar behavior by
// falling back to the following lang-codepages:
private static readonly uint[] s_fallbackLanguageCodePages = new uint[]
{
0x040904B0, // US English + CP_UNICODE
0x040904E4, // US English + CP_USASCII
0x04090000 // US English + unknown codepage
};

private static string ConvertTo8DigitHex(uint value)
{
return value.ToString("X8");
Expand Down
Loading