Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace System
{
Expand Down Expand Up @@ -176,13 +175,15 @@ public override long NextInt64(long minValue, long maxValue)

public override void NextBytes(byte[] buffer) => NextBytes((Span<byte>)buffer);

public override unsafe void NextBytes(Span<byte> buffer)
public override void NextBytes(Span<byte> buffer)
{
uint s0 = _s0, s1 = _s1, s2 = _s2, s3 = _s3;

while (buffer.Length >= sizeof(uint))
{
MemoryMarshal.Write(buffer, BitOperations.RotateLeft(s1 * 5, 7) * 9);
uint next = BitOperations.RotateLeft(s1 * 5, 7) * 9;
bool success = BitConverter.TryWriteBytes(buffer, next);
Debug.Assert(success);

// Update PRNG state.
uint t = s1 << 9;
Expand All @@ -199,12 +200,11 @@ public override unsafe void NextBytes(Span<byte> buffer)
if (!buffer.IsEmpty)
{
uint next = BitOperations.RotateLeft(s1 * 5, 7) * 9;
byte* remainingBytes = (byte*)&next;
Debug.Assert(buffer.Length < sizeof(uint));
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = remainingBytes[i];
}
Span<byte> remainingBytes = [0, 0, 0, 0];
Comment thread
EgorBo marked this conversation as resolved.
bool success = BitConverter.TryWriteBytes(remainingBytes, next);
Debug.Assert(success);
remainingBytes.Slice(0, buffer.Length).CopyTo(buffer);

// Update PRNG state.
uint t = s1 << 9;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace System
{
Expand Down Expand Up @@ -133,13 +132,15 @@ public override long NextInt64(long minValue, long maxValue)

public override void NextBytes(byte[] buffer) => NextBytes((Span<byte>)buffer);

public override unsafe void NextBytes(Span<byte> buffer)
public override void NextBytes(Span<byte> buffer)
{
ulong s0 = _s0, s1 = _s1, s2 = _s2, s3 = _s3;

while (buffer.Length >= sizeof(ulong))
{
MemoryMarshal.Write(buffer, BitOperations.RotateLeft(s1 * 5, 7) * 9);
ulong next = BitOperations.RotateLeft(s1 * 5, 7) * 9;
bool success = BitConverter.TryWriteBytes(buffer, next);
Debug.Assert(success);

// Update PRNG state.
ulong t = s1 << 17;
Expand All @@ -156,12 +157,11 @@ public override unsafe void NextBytes(Span<byte> buffer)
if (!buffer.IsEmpty)
{
ulong next = BitOperations.RotateLeft(s1 * 5, 7) * 9;
byte* remainingBytes = (byte*)&next;
Debug.Assert(buffer.Length < sizeof(ulong));
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = remainingBytes[i];
}
Span<byte> remainingBytes = [0, 0, 0, 0, 0, 0, 0, 0];
Comment thread
EgorBo marked this conversation as resolved.
bool success = BitConverter.TryWriteBytes(remainingBytes, next);
Debug.Assert(success);
remainingBytes.Slice(0, buffer.Length).CopyTo(buffer);

// Update PRNG state.
ulong t = s1 << 17;
Expand Down
Loading