Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -2250,7 +2250,13 @@ private byte ValueScaleCore(object value)
{
if (value is decimal decimalValue)
{
return (byte)((decimal.GetBits(decimalValue)[3] & 0x00ff0000) >> 0x10);
#if NET
Span<int> decimalBits = stackalloc int[4];
decimal.GetBits(decimalValue, decimalBits);
#else
int[] decimalBits = decimal.GetBits(decimalValue);
#endif
return (byte)((decimalBits[3] & 0x00ff0000) >> 0x10);
}
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7702,8 +7702,15 @@ internal Task WriteSqlVariantValue(object value, int length, int offset, TdsPars

case TdsEnums.SQLNUMERICN:
{
#if NET
Span<int> decimalBits = stackalloc int[4];
decimal.GetBits((decimal)value, decimalBits);
#else
int[] decimalBits = decimal.GetBits((decimal)value);
#endif

stateObj.WriteByte(mt.Precision); //propbytes: precision
stateObj.WriteByte((byte)((decimal.GetBits((decimal)value)[3] & 0x00ff0000) >> 0x10)); // propbytes: scale
stateObj.WriteByte((byte)((decimalBits[3] & 0x00ff0000) >> 0x10)); // propbytes: scale
WriteDecimal((decimal)value, stateObj);
break;
}
Expand Down Expand Up @@ -7864,9 +7871,15 @@ internal Task WriteSqlVariantDataRowValue(object value, TdsParserStateObject sta

case TdsEnums.SQLNUMERICN:
{
#if NET
Span<int> decimalBits = stackalloc int[4];
decimal.GetBits((decimal)value, decimalBits);
#else
int[] decimalBits = decimal.GetBits((decimal)value);
#endif
WriteSqlVariantHeader(21, metatype.TDSType, metatype.PropBytes, stateObj);
stateObj.WriteByte(metatype.Precision); //propbytes: precision
stateObj.WriteByte((byte)((decimal.GetBits((decimal)value)[3] & 0x00ff0000) >> 0x10)); // propbytes: scale
stateObj.WriteByte((byte)((decimalBits[3] & 0x00ff0000) >> 0x10)); // propbytes: scale
WriteDecimal((decimal)value, stateObj);
break;
}
Expand Down Expand Up @@ -7923,7 +7936,12 @@ private byte[] SerializeSqlMoney(SqlMoney value, int length, TdsParserStateObjec
private void WriteSqlMoney(SqlMoney value, int length, TdsParserStateObject stateObj)
{
// UNDONE: can I use SqlMoney.ToInt64()?
#if NET
Span<int> bits = stackalloc int[4];
decimal.GetBits(value.Value, bits);
#else
int[] bits = decimal.GetBits(value.Value);
#endif

// this decimal should be scaled by 10000 (regardless of what the incoming decimal was scaled by)
bool isNeg = (0 != (bits[3] & unchecked((int)0x80000000)));
Expand Down Expand Up @@ -7956,7 +7974,12 @@ private void WriteSqlMoney(SqlMoney value, int length, TdsParserStateObject stat
private byte[] SerializeCurrency(Decimal value, int length, TdsParserStateObject stateObj)
{
SqlMoney m = new SqlMoney(value);
int[] bits = Decimal.GetBits(m.Value);
#if NET
Span<int> bits = stackalloc int[4];
decimal.GetBits(m.Value, bits);
#else
int[] bits = decimal.GetBits(m.Value);
#endif

// this decimal should be scaled by 10000 (regardless of what the incoming decimal was scaled by)
bool isNeg = (0 != (bits[3] & unchecked((int)0x80000000)));
Expand Down Expand Up @@ -8001,7 +8024,12 @@ private byte[] SerializeCurrency(Decimal value, int length, TdsParserStateObject
private void WriteCurrency(decimal value, int length, TdsParserStateObject stateObj)
{
SqlMoney m = new SqlMoney(value);
#if NET
Span<int> bits = stackalloc int[4];
decimal.GetBits(m.Value, bits);
#else
int[] bits = decimal.GetBits(m.Value);
#endif

// this decimal should be scaled by 10000 (regardless of what the incoming decimal was scaled by)
bool isNeg = (0 != (bits[3] & unchecked((int)0x80000000)));
Expand Down Expand Up @@ -8201,7 +8229,13 @@ internal static SqlDecimal AdjustSqlDecimalScale(SqlDecimal d, int newScale)

internal static decimal AdjustDecimalScale(decimal value, int newScale)
{
int oldScale = (decimal.GetBits(value)[3] & 0x00ff0000) >> 0x10;
#if NET
Span<int> decimalBits = stackalloc int[4];
decimal.GetBits(value, decimalBits);
#else
int[] decimalBits = decimal.GetBits(value);
#endif
int oldScale = (decimalBits[3] & 0x00ff0000) >> 0x10;

if (newScale != oldScale)
{
Expand Down Expand Up @@ -8283,7 +8317,12 @@ internal void WriteSqlDecimal(SqlDecimal d, TdsParserStateObject stateObj)

private byte[] SerializeDecimal(decimal value, TdsParserStateObject stateObj)
{
int[] decimalBits = Decimal.GetBits(value);
#if NET
Span<int> decimalBits = stackalloc int[4];
decimal.GetBits(value, decimalBits);
#else
int[] decimalBits = decimal.GetBits(value);
#endif
if (stateObj._bDecimalBytes == null)
{
stateObj._bDecimalBytes = new byte[17];
Expand Down Expand Up @@ -8338,8 +8377,14 @@ struct {

private void WriteDecimal(decimal value, TdsParserStateObject stateObj)
{
stateObj._decimalBits = decimal.GetBits(value);
#if NET
Span<int> decimalBits = stackalloc int[4];
decimal.GetBits(value, decimalBits);
#else
int[] decimalBits = decimal.GetBits(value);
stateObj._decimalBits = decimalBits;
Debug.Assert(stateObj._decimalBits != null, "decimalBits should be filled in at TdsExecuteRPC time");
#endif

/*
Returns a binary representation of a Decimal. The return value is an integer
Expand All @@ -8361,7 +8406,7 @@ struct {
*/

// write the sign (note that COM and SQL are opposite)
if (0x80000000 == (stateObj._decimalBits[3] & 0x80000000))
if ((decimalBits[3] & 0x80000000) == 0x80000000)
{
stateObj.WriteByte(0);
}
Expand All @@ -8370,9 +8415,9 @@ struct {
stateObj.WriteByte(1);
}

WriteInt(stateObj._decimalBits[0], stateObj);
WriteInt(stateObj._decimalBits[1], stateObj);
WriteInt(stateObj._decimalBits[2], stateObj);
WriteInt(decimalBits[0], stateObj);
WriteInt(decimalBits[1], stateObj);
WriteInt(decimalBits[2], stateObj);
WriteInt(0, stateObj);
}

Expand Down
Loading