Skip to content

Commit

Permalink
Use unsigned right shift directly from C# 11.
Browse files Browse the repository at this point in the history
  • Loading branch information
cincuranet committed Oct 1, 2023
1 parent e80871c commit d667c74
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 43 deletions.
30 changes: 0 additions & 30 deletions src/FirebirdSql.Data.FirebirdClient/Common/BitHelpers.cs

This file was deleted.

19 changes: 9 additions & 10 deletions src/FirebirdSql.Data.FirebirdClient/Common/DecimalCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using System;
using System.Diagnostics;
using FirebirdSql.Data.Types;
using static FirebirdSql.Data.Common.BitHelpers;

namespace FirebirdSql.Data.Common;

Expand Down Expand Up @@ -107,7 +106,7 @@ public FbDecFloat ParseBytes(byte[] decBytes)
_decimalFormat.ValidateByteLength(decBytes);

var firstByte = decBytes[0] & 0xff;
var signum = -1 * UnsignedRightShift(firstByte, 7) | 1;
var signum = -1 * (firstByte >>> 7) | 1;
var decimalType = DecimalTypeFromFirstByte(firstByte);
switch (decimalType)
{
Expand All @@ -124,13 +123,13 @@ public FbDecFloat ParseBytes(byte[] decBytes)
int firstDigit;
if ((firstByte & Combination2) != Combination2)
{
exponentMSB = UnsignedRightShift(firstByte, 3) & 0b01100 | (firstByte & 0b011);
firstDigit = UnsignedRightShift(firstByte, 2) & 0b0111;
exponentMSB = (firstByte >>> 3) & 0b01100 | (firstByte & 0b011);
firstDigit = (firstByte >>> 2) & 0b0111;
}
else
{
exponentMSB = UnsignedRightShift(firstByte, 1) & 0b01100 | (firstByte & 0b011);
firstDigit = 0b01000 | (UnsignedRightShift(firstByte, 2) & 0b01);
exponentMSB = (firstByte >>> 1) & 0b01100 | (firstByte & 0b011);
firstDigit = 0b01000 | ((firstByte >>> 2) & 0b01);
}
var exponentBitsRemaining = _decimalFormat.ExponentContinuationBits - 2;
Debug.Assert(exponentBitsRemaining == _decimalFormat.FormatBitLength - 8 - _decimalFormat.CoefficientContinuationBits, $"Unexpected exponent remaining length {exponentBitsRemaining}.");
Expand Down Expand Up @@ -175,8 +174,8 @@ void EncodeFinite(FbDecFloat @decimal, byte[] decBytes)
var biasedExponent = _decimalFormat.BiasedExponent(@decimal.Exponent);
var coefficient = @decimal.Coefficient;
var mostSignificantDigit = _coefficientCoder.EncodeValue(coefficient, decBytes);
var expMSB = UnsignedRightShift(biasedExponent, _decimalFormat.ExponentContinuationBits);
var expTwoBitCont = UnsignedRightShift(biasedExponent, _decimalFormat.ExponentContinuationBits - 2) & 0b011;
var expMSB = biasedExponent >>> _decimalFormat.ExponentContinuationBits;
var expTwoBitCont = (biasedExponent >>> _decimalFormat.ExponentContinuationBits - 2) & 0b011;
if (mostSignificantDigit <= 7)
{
decBytes[0] |= (byte)((expMSB << 5)
Expand All @@ -198,7 +197,7 @@ static void EncodeExponentContinuation(byte[] decBytes, int expAndBias, int expB
var expByteIndex = 1;
while (expBitsRemaining > 8)
{
decBytes[expByteIndex++] = (byte)UnsignedRightShift(expAndBias, expBitsRemaining - 8);
decBytes[expByteIndex++] = (byte)(expAndBias >>> expBitsRemaining - 8);
expBitsRemaining -= 8;
}
if (expBitsRemaining > 0)
Expand All @@ -219,7 +218,7 @@ static int DecodeExponent(byte[] decBytes, int exponentMSB, int exponentBitsRema
}
if (exponentBitsRemaining > 0)
{
exponent = (exponent << exponentBitsRemaining) | (UnsignedRightShift(decBytes[byteIndex] & 0xFF, 8 - exponentBitsRemaining));
exponent = (exponent << exponentBitsRemaining) | ((decBytes[byteIndex] & 0xFF >>> 8 - exponentBitsRemaining));
}
return exponent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using System;
using System.Diagnostics;
using System.Numerics;
using static FirebirdSql.Data.Common.BitHelpers;

namespace FirebirdSql.Data.Common;

Expand Down Expand Up @@ -309,7 +308,7 @@ BigInteger DecodeValue0(int signum, int firstDigit, byte[] decBytes, int lsbInde
var firstByteIndex = lsbIndex - digitBitsFromEnd / BitPerByte;

var dpdGroupBits = 0x3FF & (
UnsignedRightShift((decBytes[firstByteIndex] & 0xFF), firstByteBitOffset)
((decBytes[firstByteIndex] & 0xFF) >>> firstByteBitOffset)
| decBytes[firstByteIndex - 1] << BitPerByte - firstByteBitOffset);

if (dpdGroupBits != 0)
Expand Down Expand Up @@ -343,7 +342,7 @@ int EncodeValue0(BigInteger value, byte[] decBytes, int lsbIndex)
decBytes[firstByteIndex] =
(byte)(decBytes[firstByteIndex] | (currentGroup << firstByteBitOffset));
decBytes[firstByteIndex - 1] =
(byte)(decBytes[firstByteIndex - 1] | UnsignedRightShift(currentGroup, BitPerByte - firstByteBitOffset));
(byte)(decBytes[firstByteIndex - 1] | (currentGroup >>> BitPerByte - firstByteBitOffset));
}
var mostSignificantDigit = (int)remainingValue;
Debug.Assert(0 <= mostSignificantDigit && mostSignificantDigit <= 9, $"{nameof(mostSignificantDigit)} out of range, was {mostSignificantDigit}.");
Expand Down

0 comments on commit d667c74

Please sign in to comment.