Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kiminuo committed Jan 9, 2024
1 parent 40ca72b commit befb119
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 10 deletions.
19 changes: 19 additions & 0 deletions NBitcoin.Bench/UInt256Bench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ public void WriteToString()
{
Value.ToString();
}

#if HAS_SPAN
[Benchmark]
[Arguments(new char[] {
'0','0','0','0','0','0','0','0',
'0','0','0','0','0','0','0','0',
'0','0','0','0','0','0','0','0',
'0','0','0','0','0','0','0','0',
'0','0','0','0','0','0','0','0',
'0','0','0','0','0','0','0','0',
'0','0','0','0','0','0','0','0',
'0','0','0','0','0','0','0','0'
})]
public void WriteToSpanString(Span<char> destinationSpan)
{
Value.ToSpanString(destinationSpan);
}
#endif

[Benchmark]
public void Read()
{
Expand Down
4 changes: 2 additions & 2 deletions NBitcoin.Tests/NBitcoin.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
<ProjectReference Include="..\NBitcoin.Altcoins\NBitcoin.Altcoins.csproj" />
<ProjectReference Include="..\NBitcoin.TestFramework\NBitcoin.TestFramework.csproj" />
<PackageReference Include="Microsoft.CSharp" Version="4.6.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PackageReference Include="xunit" Version="2.6.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
18 changes: 12 additions & 6 deletions NBitcoin.Tests/uint256_tests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using NBitcoin.Protocol;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace NBitcoin.Tests
Expand Down Expand Up @@ -147,7 +143,7 @@ public void uitnSerializationTests2()

[Fact]
[Trait("UnitTest", "UnitTest")]
public void uitnSerializationTests()
public void uintSerializationTests()
{
MemoryStream ms = new MemoryStream();
BitcoinStream stream = new BitcoinStream(ms, true);
Expand Down Expand Up @@ -193,6 +189,16 @@ public void uitnSerializationTests()
Assert.True(vs2.SequenceEqual(vs));
}

[Fact]
[Trait("UnitTest", "UnitTest")]
public void uintSpanSerializationTests()
{
var v1 = new uint256("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");

Assert.Equal("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff", v1.ToString());
Assert.Equal("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff".AsSpan(), v1.ToSpanString());

Check failure on line 199 in NBitcoin.Tests/uint256_tests.cs

View workflow job for this annotation

GitHub Actions / 6.0 with netstandard2.0

'uint256' does not contain a definition for 'ToSpanString' and no accessible extension method 'ToSpanString' accepting a first argument of type 'uint256' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 199 in NBitcoin.Tests/uint256_tests.cs

View workflow job for this annotation

GitHub Actions / 6.0 with netstandard2.0

'uint256' does not contain a definition for 'ToSpanString' and no accessible extension method 'ToSpanString' accepting a first argument of type 'uint256' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 199 in NBitcoin.Tests/uint256_tests.cs

View workflow job for this annotation

GitHub Actions / 3.1

The type 'ReadOnlySpan<char>' may not be used as a type argument

Check failure on line 199 in NBitcoin.Tests/uint256_tests.cs

View workflow job for this annotation

GitHub Actions / 3.1

The type 'ReadOnlySpan<char>' may not be used as a type argument
}

private void AssertEquals(uint256 a, uint256 b)
{
Assert.Equal(a, b);
Expand Down
40 changes: 38 additions & 2 deletions NBitcoin/UInt256.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

using NBitcoin.DataEncoders;
using System;
using System.Collections;
using System.Linq;
using System.Runtime.InteropServices;
using NBitcoin.DataEncoders;

namespace NBitcoin
{
Expand Down Expand Up @@ -161,6 +160,43 @@ public override string ToString()
return Encoder.EncodeData(bytes);
}

#if HAS_SPAN
/// <remarks>The method allocates a new 64 char array.</remarks>
public Span<char> ToSpanString()
{
Span<char> result = new char[64];

ToSpanString(result);
return result;
}

/// <remarks>The method does not allocate.</remarks>
public void ToSpanString(Span<char> destination)
{
Span<ulong> ulongs = stackalloc ulong[4];
ulongs[0] = pn0;
ulongs[1] = pn1;
ulongs[2] = pn2;
ulongs[3] = pn3;
Span<byte> bytes = MemoryMarshal.Cast<ulong, byte>(ulongs);

if (BitConverter.IsLittleEndian)
{
for (int i = 31, j = 0; i >= 0; i--, j += 2)
{
HexEncoder.ToCharsBuffer(bytes[i], destination, startingIndex: j);
}
}
else
{
for (int i = 0; i < 32; i++)
{
HexEncoder.ToCharsBuffer(bytes[i], destination, startingIndex: i);
}
}
}
#endif

public uint256(ulong b)
{
pn0 = (uint)b;
Expand Down

0 comments on commit befb119

Please sign in to comment.