From 20c9ce6d6d5e5d2220fa592e26f22594626b1993 Mon Sep 17 00:00:00 2001 From: Jiri Cincura Date: Wed, 4 Oct 2023 20:31:00 +0200 Subject: [PATCH] Use unsigned right shift directly from C# 11. --- .../Common/BitHelpers.cs | 30 ------------------- .../Common/DecimalCodec.cs | 20 ++++++------- .../Common/DenselyPackedDecimalCodec.cs | 5 ++-- 3 files changed, 12 insertions(+), 43 deletions(-) delete mode 100644 src/FirebirdSql.Data.FirebirdClient/Common/BitHelpers.cs diff --git a/src/FirebirdSql.Data.FirebirdClient/Common/BitHelpers.cs b/src/FirebirdSql.Data.FirebirdClient/Common/BitHelpers.cs deleted file mode 100644 index fd6fdc38..00000000 --- a/src/FirebirdSql.Data.FirebirdClient/Common/BitHelpers.cs +++ /dev/null @@ -1,30 +0,0 @@ -/* - * The contents of this file are subject to the Initial - * Developer's Public License Version 1.0 (the "License"); - * you may not use this file except in compliance with the - * License. You may obtain a copy of the License at - * https://github.com/FirebirdSQL/NETProvider/raw/master/license.txt. - * - * Software distributed under the License is distributed on - * an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either - * express or implied. See the License for the specific - * language governing rights and limitations under the License. - * - * All Rights Reserved. - */ - -//$Authors = Jiri Cincura (jiri@cincura.net) - -using System; -using System.Diagnostics; -using System.Numerics; - -namespace FirebirdSql.Data.Common; - -internal static class BitHelpers -{ - public static int UnsignedRightShift(int value, int shift) - { - return (int)((uint)value >> shift); - } -} diff --git a/src/FirebirdSql.Data.FirebirdClient/Common/DecimalCodec.cs b/src/FirebirdSql.Data.FirebirdClient/Common/DecimalCodec.cs index fbf63c4b..5ff63dde 100644 --- a/src/FirebirdSql.Data.FirebirdClient/Common/DecimalCodec.cs +++ b/src/FirebirdSql.Data.FirebirdClient/Common/DecimalCodec.cs @@ -18,7 +18,6 @@ using System; using System.Diagnostics; using FirebirdSql.Data.Types; -using static FirebirdSql.Data.Common.BitHelpers; namespace FirebirdSql.Data.Common; @@ -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) { @@ -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}."); @@ -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) @@ -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) @@ -219,7 +218,8 @@ 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; } diff --git a/src/FirebirdSql.Data.FirebirdClient/Common/DenselyPackedDecimalCodec.cs b/src/FirebirdSql.Data.FirebirdClient/Common/DenselyPackedDecimalCodec.cs index c5c71acb..f2a2aaa9 100644 --- a/src/FirebirdSql.Data.FirebirdClient/Common/DenselyPackedDecimalCodec.cs +++ b/src/FirebirdSql.Data.FirebirdClient/Common/DenselyPackedDecimalCodec.cs @@ -18,7 +18,6 @@ using System; using System.Diagnostics; using System.Numerics; -using static FirebirdSql.Data.Common.BitHelpers; namespace FirebirdSql.Data.Common; @@ -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) @@ -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}.");