Skip to content

Commit

Permalink
SWEEP: Replace J2N's TripleShift call with C# 11's unsigned right shi…
Browse files Browse the repository at this point in the history
…ft operator, #115 (#116)
  • Loading branch information
paulirwin authored Nov 4, 2024
1 parent df6fc4a commit bfe6442
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 149 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<RepositoryRoot>$(MSBuildThisFileDirectory)</RepositoryRoot>
<LangVersion>10.0</LangVersion>
<LangVersion>11.0</LangVersion>
<GitHubOrganization>NightOwl888</GitHubOrganization>
<GitHubProject>J2N</GitHubProject>
</PropertyGroup>
Expand Down Expand Up @@ -43,4 +43,4 @@
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

</Project>
</Project>
25 changes: 12 additions & 13 deletions src/J2N/Collections/BitSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
#endregion

using J2N.Numerics;
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -311,11 +310,11 @@ public virtual BitSet Get(int position1, int position2)
int idx1 = position1 >> Offset;
int idx2 = (position2 - 1) >> Offset;
long factor1 = (~0L) << (position1 & RightBits);
long factor2 = (~0L).TripleShift(ElmSize - (position2 & RightBits));
long factor2 = (~0L) >>> (ElmSize - (position2 & RightBits));

if (idx1 == idx2)
{
long result = (bits[idx1] & (factor1 & factor2)).TripleShift(position1 % ElmSize);
long result = (bits[idx1] & (factor1 & factor2)) >>> (position1 % ElmSize);
if (result == 0)
{
return new BitSet(0);
Expand Down Expand Up @@ -343,7 +342,7 @@ public virtual BitSet Get(int position1, int position2)
{
// shift the current element to the right regardless of
// sign
newbits[i] = newbits[i].TripleShift(numBitsToShift);
newbits[i] >>>= numBitsToShift;

// apply the last x bits of newbits[i+1] to the current
// element
Expand Down Expand Up @@ -440,7 +439,7 @@ public virtual void Set(int position1, int position2)
int idx1 = position1 >> Offset;
int idx2 = (position2 - 1) >> Offset;
long factor1 = (~0L) << (position1 & RightBits);
long factor2 = (~0L).TripleShift(ElmSize - (position2 & RightBits));
long factor2 = (~0L) >>> (ElmSize - (position2 & RightBits));

if (idx1 == idx2)
{
Expand Down Expand Up @@ -572,7 +571,7 @@ public virtual void Clear(int position1, int position2)
int idx1 = position1 >> Offset;
int idx2 = (position2 - 1) >> Offset;
long factor1 = (~0L) << (position1 & RightBits);
long factor2 = (~0L).TripleShift(ElmSize - (position2 & RightBits));
long factor2 = (~0L) >>> (ElmSize - (position2 & RightBits));

if (idx1 == idx2)
{
Expand Down Expand Up @@ -651,7 +650,7 @@ public virtual void Flip(int position1, int position2)
int idx1 = position1 >> Offset;
int idx2 = (position2 - 1) >> Offset;
long factor1 = (~0L) << (position1 & RightBits);
long factor2 = (~0L).TripleShift(ElmSize - (position2 & RightBits));
long factor2 = (~0L) >>> (ElmSize - (position2 & RightBits));

if (idx1 == idx2)
{
Expand Down Expand Up @@ -1115,7 +1114,7 @@ public virtual int Cardinality
for (int idx = 0; idx < length; idx++)
{
count += Pop(bits[idx] & 0xffffffffL);
count += Pop(bits[idx].TripleShift(32));
count += Pop(bits[idx] >>> 32);
}
return count;
}
Expand All @@ -1124,11 +1123,11 @@ public virtual int Cardinality
[SuppressMessage("Style", "IDE0054:Use compound assignment", Justification = "Aligning code style with Apache Harmony")]
private static int Pop(long x)
{
x = x - (x.TripleShift(1) & 0x55555555);
x = (x & 0x33333333) + ((x.TripleShift(2)) & 0x33333333);
x = (x + (x.TripleShift(4))) & 0x0f0f0f0f;
x = x + (x.TripleShift(8));
x = x + (x.TripleShift(16));
x = x - ((x >>> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
x = (x + (x >>> 4)) & 0x0f0f0f0f;
x = x + (x >>> 8);
x = x + (x >>> 16);
return (int)x & 0x0000003f;
}

Expand Down
6 changes: 2 additions & 4 deletions src/J2N/MathExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
*/
#endregion

using J2N.Numerics;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;


namespace J2N
{
/// <summary>
Expand Down Expand Up @@ -54,7 +52,7 @@ public static class MathExtensions
public static int Signum(this int value)
{
// HD, Section 2-7
return (value >> 31) | ((-value).TripleShift(31));
return (value >> 31) | (-value >>> 31);
}

/// <summary>
Expand All @@ -68,7 +66,7 @@ public static int Signum(this int value)
public static int Signum(this long value)
{
// HD, Section 2-7
return (int)((value >> 63) | ((-value).TripleShift(63)));
return (int)((value >> 63) | (-value >>> 63));
}

/// <summary>
Expand Down
60 changes: 30 additions & 30 deletions src/J2N/Numerics/BitOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public static int PopCount(this int value)
return System.Numerics.BitOperations.PopCount((uint)value);
#else
// Hacker's Delight, Figure 5-2
value -= ((value.TripleShift(1)) & 0x55555555);
value = (value & 0x33333333) + ((value.TripleShift(2)) & 0x33333333);
value = (value + (value.TripleShift(4))) & 0x0f0f0f0f;
value += (value.TripleShift(8));
value += (value.TripleShift(16));
value -= ((value >>> 1) & 0x55555555);
value = (value & 0x33333333) + ((value >>> 2) & 0x33333333);
value = (value + (value >>> 4)) & 0x0f0f0f0f;
value += (value >>> 8);
value += (value >>> 16);
return value & 0x3f;
#endif
}
Expand All @@ -67,12 +67,12 @@ public static int PopCount(this long value)
return System.Numerics.BitOperations.PopCount((ulong)value);
#else
// Hacker's Delight, Figure 5-14
value -= ((value.TripleShift(1)) & 0x5555555555555555L);
value = (value & 0x3333333333333333L) + ((value.TripleShift(2)) & 0x3333333333333333L);
value = (value + (value.TripleShift(4))) & 0x0f0f0f0f0f0f0f0fL;
value += (value.TripleShift(8));
value += (value.TripleShift(16));
value += (value.TripleShift(32));
value -= ((value >>> 1) & 0x5555555555555555L);
value = (value & 0x3333333333333333L) + ((value >>> 2) & 0x3333333333333333L);
value = (value + (value >>> 4)) & 0x0f0f0f0f0f0f0f0fL;
value += (value >>> 8);
value += (value >>> 16);
value += (value >>> 32);
return (int)value & 0x7f;
#endif
}
Expand Down Expand Up @@ -239,7 +239,7 @@ public static int HighestOneBit(this int value) // Harmony
value |= (value >> 4);
value |= (value >> 8);
value |= (value >> 16);
return (value & ~(value.TripleShift(1)));
return (value & ~(value >>> 1));
}

/// <summary>
Expand All @@ -262,7 +262,7 @@ public static long HighestOneBit(this long value)
value |= (value >> 8);
value |= (value >> 16);
value |= (value >> 32);
return (value & ~(value.TripleShift(1)));
return (value & ~(value >>> 1));
}

#endregion HighestOneBit
Expand Down Expand Up @@ -347,11 +347,11 @@ public static long LowestOneBit(this long value)
public static int Reverse(this int value)
{
// From Hacker's Delight, 7-1, Figure 7-1
value = (value & 0x55555555) << 1 | (value.TripleShift(1)) & 0x55555555;
value = (value & 0x33333333) << 2 | (value.TripleShift(2)) & 0x33333333;
value = (value & 0x0f0f0f0f) << 4 | (value.TripleShift(4)) & 0x0f0f0f0f;
value = (value & 0x55555555) << 1 | (value >>> 1) & 0x55555555;
value = (value & 0x33333333) << 2 | (value >>> 2) & 0x33333333;
value = (value & 0x0f0f0f0f) << 4 | (value >>> 4) & 0x0f0f0f0f;
value = (value << 24) | ((value & 0xff00) << 8) |
((value.TripleShift(8)) & 0xff00) | (value.TripleShift(24));
((value >>> 8) & 0xff00) | (value >>> 24);
return value;
}

Expand All @@ -365,12 +365,12 @@ public static int Reverse(this int value)
public static long Reverse(this long value)
{
// From Hacker's Delight, 7-1, Figure 7-1
value = (value & 0x5555555555555555L) << 1 | (value.TripleShift(1)) & 0x5555555555555555L;
value = (value & 0x3333333333333333L) << 2 | (value.TripleShift(2)) & 0x3333333333333333L;
value = (value & 0x0f0f0f0f0f0f0f0fL) << 4 | (value.TripleShift(4)) & 0x0f0f0f0f0f0f0f0fL;
value = (value & 0x00ff00ff00ff00ffL) << 8 | (value.TripleShift(8)) & 0x00ff00ff00ff00ffL;
value = (value & 0x5555555555555555L) << 1 | (value >>> 1) & 0x5555555555555555L;
value = (value & 0x3333333333333333L) << 2 | (value >>> 2) & 0x3333333333333333L;
value = (value & 0x0f0f0f0f0f0f0f0fL) << 4 | (value >>> 4) & 0x0f0f0f0f0f0f0f0fL;
value = (value & 0x00ff00ff00ff00ffL) << 8 | (value >>> 8) & 0x00ff00ff00ff00ffL;
value = (value << 48) | ((value & 0xffff0000L) << 16) |
((value.TripleShift(16)) & 0xffff0000L) | (value.TripleShift(48));
((value >>> 16) & 0xffff0000L) | (value >>> 48);
return value;
}

Expand Down Expand Up @@ -401,7 +401,7 @@ public static short ReverseBytes(this short value)
/// <paramref name="value"/>.</returns>
public static int ReverseBytes(this int value)
{
return ((value.TripleShift(24))) |
return ((value >>> 24)) |
((value >> 8) & 0xFF00) |
((value << 8) & 0xFF0000) |
((value << 24));
Expand All @@ -417,11 +417,11 @@ public static int ReverseBytes(this int value)
public static long ReverseBytes(this long value)
{
value = (value & 0x00ff00ff00ff00ffL) << 8 |
(value.TripleShift(8)) & 0x00ff00ff00ff00ffL;
(value >>> 8) & 0x00ff00ff00ff00ffL;
return (value << 48) |
((value & 0xffff0000L) << 16) |
((value.TripleShift(16)) & 0xffff0000L) |
(value.TripleShift(48));
((value >>> 16) & 0xffff0000L) |
(value >>> 48);
}

#endregion
Expand Down Expand Up @@ -459,7 +459,7 @@ public static int RotateLeft(this int value, int distance)
#if FEATURE_NUMERICBITOPERATIONS
return (int)System.Numerics.BitOperations.RotateLeft((uint)value, distance);
#else
return ((value << distance) | (value.TripleShift(-distance)));
return ((value << distance) | (value >>> -distance));
#endif
}

Expand Down Expand Up @@ -497,7 +497,7 @@ public static long RotateLeft(this long value, int distance)
#if FEATURE_NUMERICBITOPERATIONS
return (long)System.Numerics.BitOperations.RotateLeft((ulong)value, distance);
#else
return ((value << distance) | (value.TripleShift(-distance)));
return ((value << distance) | (value >>> -distance));
#endif
}

Expand Down Expand Up @@ -536,7 +536,7 @@ public static int RotateRight(this int value, int distance)
#if FEATURE_NUMERICBITOPERATIONS
return (int)System.Numerics.BitOperations.RotateRight((uint)value, distance);
#else
return ((value.TripleShift(distance)) | (value << (-distance)));
return ((value >>> distance) | (value << (-distance)));
#endif
}

Expand Down Expand Up @@ -571,7 +571,7 @@ public static long RotateRight(this long value, int distance)
#if FEATURE_NUMERICBITOPERATIONS
return (long)System.Numerics.BitOperations.RotateRight((ulong)value, distance);
#else
return ((value.TripleShift(distance)) | (value << (-distance)));
return ((value >>> distance) | (value << -distance));
#endif
}

Expand Down
15 changes: 6 additions & 9 deletions src/J2N/Numerics/DotNetNumber.Formatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ internal static string DoubleToHexStr(double value, NumberFormatInfo info, bool

bool negative = (bitValue & unchecked((long)0x8000000000000000L)) != 0;
// mask exponent bits and shift down
long exponent = (bitValue & 0x7FF0000000000000L).TripleShift(52);
long exponent = (bitValue & 0x7FF0000000000000L) >>> 52;
// mask significand bits and shift up
long significand = bitValue & 0x000FFFFFFFFFFFFFL;

Expand Down Expand Up @@ -542,7 +542,7 @@ internal static string DoubleToHexStr(double value, NumberFormatInfo info, bool
// them
while ((significand != 0) && ((significand & 0xF) == 0))
{
significand = significand.TripleShift(4);
significand >>>= 4;
fractionDigits--;
}
string hexSignificand = significand.ToString(upperCase ? "X" : "x", info);
Expand All @@ -569,7 +569,7 @@ internal static string DoubleToHexStr(double value, NumberFormatInfo info, bool
// them
while ((significand != 0) && ((significand & 0xF) == 0))
{
significand = significand.TripleShift(4);
significand >>>= 4;
fractionDigits--;
}
string hexSignificand = significand.ToString(upperCase ? "X" : "x", info);
Expand Down Expand Up @@ -913,7 +913,7 @@ internal static string SingleToHexStr(float value, NumberFormatInfo info, bool u

bool negative = (bitValue & 0x80000000) != 0;
// mask exponent bits and shift down
int exponent = (bitValue & 0x7f800000).TripleShift(23);
int exponent = (bitValue & 0x7f800000) >>> 23;
// mask significand bits and shift up
// significand is 23-bits, so we shift to treat it like 24-bits
int significand = (bitValue & 0x007FFFFF) << 1;
Expand Down Expand Up @@ -942,8 +942,7 @@ internal static string SingleToHexStr(float value, NumberFormatInfo info, bool u
// them
while ((significand != 0) && ((significand & 0xF) == 0))
{
//significand >>>= 4;
significand = significand.TripleShift(4);
significand >>>= 4;
fractionDigits--;
}
string hexSignificand = significand.ToString(upperCase ? "X" : "x", info);
Expand All @@ -970,8 +969,7 @@ internal static string SingleToHexStr(float value, NumberFormatInfo info, bool u
// them
while ((significand != 0) && ((significand & 0xF) == 0))
{
//significand >>>= 4;
significand = significand.TripleShift(4);
significand >>>= 4;
fractionDigits--;
}
string hexSignificand = significand.ToString(upperCase ? "X" : "x", info);
Expand Down Expand Up @@ -3228,4 +3226,3 @@ private static uint ExtractFractionAndBiasedExponent(float value, out int expone
}
}
}

2 changes: 1 addition & 1 deletion src/J2N/Numerics/Double.cs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ public override bool Equals(object? obj)
public override int GetHashCode()
{
long v = BitConversion.DoubleToInt64Bits(value);
return (int)(v ^ (v.TripleShift(32)));
return (int)(v ^ (v >>> 32));
}

#endregion GetHashCode
Expand Down
2 changes: 1 addition & 1 deletion src/J2N/Numerics/Int64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ public override bool Equals(object? obj)
/// <returns>A 32-bit signed integer hash code.</returns>
public override int GetHashCode()
{
return (int)(value ^ (value.TripleShift(32)));
return (int)(value ^ (value >>> 32));
}

#endregion GetHashCode
Expand Down
Loading

0 comments on commit bfe6442

Please sign in to comment.