Repository navigation
Add polyfills for System.Numerics.BitOperations #137
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d80becc
Add polyfills for System.Numerics.BitOperations
Copilot cf7e8f8
Replace InlineData theories with multiple assertions in BitOperations…
Copilot 715945a
Update BitOperations.cs
Tyrrrz b1da8c7
Merge remote-tracking branch 'origin/prime' into copilot/provide-poly…
Copilot e374b3f
Merge origin/prime and regenerate Signatures.md
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| using System.Numerics; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.Net60; | ||
|
|
||
| public class BitOperationsTests | ||
| { | ||
| [Theory] | ||
| [InlineData(0, false)] | ||
| [InlineData(1, true)] | ||
| [InlineData(2, true)] | ||
| [InlineData(3, false)] | ||
| [InlineData(-4, false)] | ||
| public void IsPow2_Int32_Test(int value, bool expected) => | ||
| BitOperations.IsPow2(value).Should().Be(expected); | ||
|
|
||
| [Theory] | ||
| [InlineData(0u, false)] | ||
| [InlineData(1u, true)] | ||
| [InlineData(1024u, true)] | ||
| [InlineData(1023u, false)] | ||
| public void IsPow2_UInt32_Test(uint value, bool expected) => | ||
| BitOperations.IsPow2(value).Should().Be(expected); | ||
|
|
||
| [Theory] | ||
| [InlineData(0L, false)] | ||
| [InlineData(1L, true)] | ||
| [InlineData(2L, true)] | ||
| [InlineData(-4L, false)] | ||
| public void IsPow2_Int64_Test(long value, bool expected) => | ||
| BitOperations.IsPow2(value).Should().Be(expected); | ||
|
|
||
| [Theory] | ||
| [InlineData(0ul, false)] | ||
| [InlineData(1ul, true)] | ||
| [InlineData(1024ul, true)] | ||
| [InlineData(1023ul, false)] | ||
| public void IsPow2_UInt64_Test(ulong value, bool expected) => | ||
| BitOperations.IsPow2(value).Should().Be(expected); | ||
|
|
||
| [Theory] | ||
| [InlineData(0u, 0u)] | ||
| [InlineData(1u, 1u)] | ||
| [InlineData(2u, 2u)] | ||
| [InlineData(3u, 4u)] | ||
| [InlineData(5u, 8u)] | ||
| [InlineData(1000u, 1024u)] | ||
| public void RoundUpToPowerOf2_UInt32_Test(uint value, uint expected) => | ||
| BitOperations.RoundUpToPowerOf2(value).Should().Be(expected); | ||
|
|
||
| [Fact] | ||
| public void RoundUpToPowerOf2_UInt32_Overflow_Test() => | ||
| BitOperations.RoundUpToPowerOf2(uint.MaxValue).Should().Be(0u); | ||
|
|
||
| [Theory] | ||
| [InlineData(0ul, 0ul)] | ||
| [InlineData(1ul, 1ul)] | ||
| [InlineData(3ul, 4ul)] | ||
| [InlineData(1000ul, 1024ul)] | ||
| public void RoundUpToPowerOf2_UInt64_Test(ulong value, ulong expected) => | ||
| BitOperations.RoundUpToPowerOf2(value).Should().Be(expected); | ||
|
|
||
| [Fact] | ||
| public void RoundUpToPowerOf2_UInt64_Overflow_Test() => | ||
| BitOperations.RoundUpToPowerOf2(ulong.MaxValue).Should().Be(0ul); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| using System; | ||
| using System.Numerics; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.Net70; | ||
|
|
||
| public class BitOperationsTests | ||
| { | ||
| [Theory] | ||
| [InlineData(0, false)] | ||
| [InlineData(1, true)] | ||
| [InlineData(2, true)] | ||
| [InlineData(3, false)] | ||
| public void IsPow2_IntPtr_Test(int value, bool expected) => | ||
| BitOperations.IsPow2((IntPtr)value).Should().Be(expected); | ||
|
|
||
| [Theory] | ||
| [InlineData(0u, false)] | ||
| [InlineData(1u, true)] | ||
| [InlineData(1024u, true)] | ||
| [InlineData(1023u, false)] | ||
| public void IsPow2_UIntPtr_Test(uint value, bool expected) => | ||
| BitOperations.IsPow2((UIntPtr)value).Should().Be(expected); | ||
|
|
||
| [Theory] | ||
| [InlineData(0u, 0u)] | ||
| [InlineData(3u, 4u)] | ||
| [InlineData(1000u, 1024u)] | ||
| public void RoundUpToPowerOf2_UIntPtr_Test(uint value, uint expected) => | ||
| BitOperations.RoundUpToPowerOf2((UIntPtr)value).Should().Be((UIntPtr)expected); | ||
|
|
||
| [Fact] | ||
| public void LeadingZeroCount_UIntPtr_Test() | ||
| { | ||
| // Arrange | ||
| var bitWidth = UIntPtr.Size * 8; | ||
|
|
||
| // Act & assert | ||
| BitOperations.LeadingZeroCount((UIntPtr)0u).Should().Be(bitWidth); | ||
| BitOperations.LeadingZeroCount((UIntPtr)1u).Should().Be(bitWidth - 1); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(1u, 0)] | ||
| [InlineData(16u, 4)] | ||
| public void Log2_UIntPtr_Test(uint value, int expected) => | ||
| BitOperations.Log2((UIntPtr)value).Should().Be(expected); | ||
|
|
||
| [Theory] | ||
| [InlineData(0u, 0)] | ||
| [InlineData(7u, 3)] | ||
| public void PopCount_UIntPtr_Test(uint value, int expected) => | ||
| BitOperations.PopCount((UIntPtr)value).Should().Be(expected); | ||
|
|
||
| [Fact] | ||
| public void TrailingZeroCount_IntPtr_Test() | ||
| { | ||
| // Arrange | ||
| var bitWidth = UIntPtr.Size * 8; | ||
|
|
||
| // Act & assert | ||
| BitOperations.TrailingZeroCount((IntPtr)0).Should().Be(bitWidth); | ||
| BitOperations.TrailingZeroCount((IntPtr)8).Should().Be(3); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TrailingZeroCount_UIntPtr_Test() | ||
| { | ||
| // Arrange | ||
| var bitWidth = UIntPtr.Size * 8; | ||
|
|
||
| // Act & assert | ||
| BitOperations.TrailingZeroCount((UIntPtr)0u).Should().Be(bitWidth); | ||
| BitOperations.TrailingZeroCount((UIntPtr)8u).Should().Be(3); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RotateLeft_UIntPtr_Test() => | ||
| BitOperations.RotateLeft((UIntPtr)0b_1000_0000u, 1).Should().Be((UIntPtr)0b_1_0000_0000u); | ||
|
|
||
| [Fact] | ||
| public void RotateRight_UIntPtr_Test() => | ||
| BitOperations.RotateRight((UIntPtr)0b_0000_0010u, 1).Should().Be((UIntPtr)0b_0000_0001u); | ||
|
|
||
| [Fact] | ||
| public void RotateRight_UIntPtr_FullCircle_Test() | ||
| { | ||
| // Arrange | ||
| var bitWidth = UIntPtr.Size * 8; | ||
|
|
||
| // Act & assert | ||
| BitOperations.RotateRight((UIntPtr)1u, bitWidth).Should().Be((UIntPtr)1u); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| using System.Numerics; | ||
| using System.Text; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.Net80; | ||
|
|
||
| public class BitOperationsTests | ||
| { | ||
| [Fact] | ||
| public void Crc32C_Byte_KnownCheckValue_Test() | ||
| { | ||
| // Arrange | ||
| var data = Encoding.ASCII.GetBytes("123456789"); | ||
|
|
||
| // Act | ||
| var crc = uint.MaxValue; | ||
| foreach (var b in data) | ||
| crc = BitOperations.Crc32C(crc, b); | ||
| crc = ~crc; | ||
|
|
||
| // Assert | ||
| // https://reveng.sourceforge.io/crc-catalogue/17plus.htm#crc.cat.crc-32-iscsi | ||
| crc.Should().Be(0xE3069283u); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Crc32C_UInt16_MatchesByteByByte_Test() | ||
| { | ||
| // Arrange | ||
| ushort data = 0x1234; | ||
|
|
||
| // Act | ||
| var viaUInt16 = BitOperations.Crc32C(0u, data); | ||
|
|
||
| var viaBytes = BitOperations.Crc32C(0u, (byte)data); | ||
| viaBytes = BitOperations.Crc32C(viaBytes, (byte)(data >> 8)); | ||
|
|
||
| // Assert | ||
| viaUInt16.Should().Be(viaBytes); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Crc32C_UInt32_MatchesByteByByte_Test() | ||
| { | ||
| // Arrange | ||
| uint data = 0x1234_5678u; | ||
|
|
||
| // Act | ||
| var viaUInt32 = BitOperations.Crc32C(0u, data); | ||
|
|
||
| var viaBytes = BitOperations.Crc32C(0u, (byte)data); | ||
| viaBytes = BitOperations.Crc32C(viaBytes, (byte)(data >> 8)); | ||
| viaBytes = BitOperations.Crc32C(viaBytes, (byte)(data >> 16)); | ||
| viaBytes = BitOperations.Crc32C(viaBytes, (byte)(data >> 24)); | ||
|
|
||
| // Assert | ||
| viaUInt32.Should().Be(viaBytes); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Crc32C_UInt64_MatchesUInt32Halves_Test() | ||
| { | ||
| // Arrange | ||
| ulong data = 0x1234_5678_9ABC_DEF0ul; | ||
|
|
||
| // Act | ||
| var viaUInt64 = BitOperations.Crc32C(0u, data); | ||
|
|
||
| var viaUInt32Halves = BitOperations.Crc32C(0u, (uint)data); | ||
| viaUInt32Halves = BitOperations.Crc32C(viaUInt32Halves, (uint)(data >> 32)); | ||
|
|
||
| // Assert | ||
| viaUInt64.Should().Be(viaUInt32Halves); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| using System.Numerics; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.NetCore30; | ||
|
|
||
| public class BitOperationsTests | ||
| { | ||
| [Theory] | ||
| [InlineData(0b_0000_0000u, 0)] | ||
| [InlineData(0b_0000_0001u, 1)] | ||
| [InlineData(0b_0000_0111u, 3)] | ||
| [InlineData(0b_1111_1111u, 8)] | ||
| [InlineData(uint.MaxValue, 32)] | ||
| public void PopCount_UInt32_Test(uint value, int expected) => | ||
| BitOperations.PopCount(value).Should().Be(expected); | ||
|
|
||
| [Theory] | ||
| [InlineData(0ul, 0)] | ||
| [InlineData(1ul, 1)] | ||
| [InlineData(0b_0000_0111ul, 3)] | ||
| [InlineData(ulong.MaxValue, 64)] | ||
| public void PopCount_UInt64_Test(ulong value, int expected) => | ||
| BitOperations.PopCount(value).Should().Be(expected); | ||
|
|
||
| [Theory] | ||
| [InlineData(0u, 32)] | ||
| [InlineData(1u, 31)] | ||
| [InlineData(uint.MaxValue, 0)] | ||
| [InlineData(0b_1000_0000_0000_0000_0000_0000_0000_0000u, 0)] | ||
| public void LeadingZeroCount_UInt32_Test(uint value, int expected) => | ||
| BitOperations.LeadingZeroCount(value).Should().Be(expected); | ||
|
|
||
| [Theory] | ||
| [InlineData(0ul, 64)] | ||
| [InlineData(1ul, 63)] | ||
| [InlineData(ulong.MaxValue, 0)] | ||
| public void LeadingZeroCount_UInt64_Test(ulong value, int expected) => | ||
| BitOperations.LeadingZeroCount(value).Should().Be(expected); | ||
|
|
||
| [Theory] | ||
| [InlineData(0u, 0)] | ||
| [InlineData(1u, 0)] | ||
| [InlineData(2u, 1)] | ||
| [InlineData(15u, 3)] | ||
| [InlineData(16u, 4)] | ||
| [InlineData(uint.MaxValue, 31)] | ||
| public void Log2_UInt32_Test(uint value, int expected) => | ||
| BitOperations.Log2(value).Should().Be(expected); | ||
|
|
||
| [Theory] | ||
| [InlineData(0ul, 0)] | ||
| [InlineData(1ul, 0)] | ||
| [InlineData(1024ul, 10)] | ||
| [InlineData(ulong.MaxValue, 63)] | ||
| public void Log2_UInt64_Test(ulong value, int expected) => | ||
| BitOperations.Log2(value).Should().Be(expected); | ||
|
|
||
| [Theory] | ||
| [InlineData(0, 32)] | ||
| [InlineData(1, 0)] | ||
| [InlineData(8, 3)] | ||
| [InlineData(-1, 0)] | ||
| public void TrailingZeroCount_Int32_Test(int value, int expected) => | ||
| BitOperations.TrailingZeroCount(value).Should().Be(expected); | ||
|
|
||
| [Theory] | ||
| [InlineData(0u, 32)] | ||
| [InlineData(1u, 0)] | ||
| [InlineData(8u, 3)] | ||
| [InlineData(uint.MaxValue, 0)] | ||
| public void TrailingZeroCount_UInt32_Test(uint value, int expected) => | ||
| BitOperations.TrailingZeroCount(value).Should().Be(expected); | ||
|
|
||
| [Theory] | ||
| [InlineData(0L, 64)] | ||
| [InlineData(1L, 0)] | ||
| [InlineData(-1L, 0)] | ||
| public void TrailingZeroCount_Int64_Test(long value, int expected) => | ||
| BitOperations.TrailingZeroCount(value).Should().Be(expected); | ||
|
|
||
| [Theory] | ||
| [InlineData(0ul, 64)] | ||
| [InlineData(1ul, 0)] | ||
| [InlineData(ulong.MaxValue, 0)] | ||
| public void TrailingZeroCount_UInt64_Test(ulong value, int expected) => | ||
| BitOperations.TrailingZeroCount(value).Should().Be(expected); | ||
|
|
||
| [Fact] | ||
| public void RotateLeft_UInt32_Test() => | ||
| BitOperations.RotateLeft(0b_1000_0000u, 1).Should().Be(0b_1_0000_0000u); | ||
|
|
||
| [Fact] | ||
| public void RotateLeft_UInt32_Overflow_Test() => | ||
| BitOperations | ||
| .RotateLeft(0b_1000_0000_0000_0000_0000_0000_0000_0000u, 1) | ||
| .Should() | ||
| .Be(0b_0000_0000_0000_0000_0000_0000_0000_0001u); | ||
|
|
||
| [Fact] | ||
| public void RotateLeft_UInt64_Test() => | ||
| BitOperations.RotateLeft(0b_1000_0000ul, 1).Should().Be(0b_1_0000_0000ul); | ||
|
|
||
| [Fact] | ||
| public void RotateRight_UInt32_Test() => | ||
| BitOperations | ||
| .RotateRight(0b_0000_0001u, 1) | ||
| .Should() | ||
| .Be(0b_1000_0000_0000_0000_0000_0000_0000_0000u); | ||
|
|
||
| [Fact] | ||
| public void RotateRight_UInt64_Test() => | ||
| BitOperations | ||
| .RotateRight(0b_0000_0001ul, 1) | ||
| .Should() | ||
| .Be( | ||
| 0b_1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000ul | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.