daily update on 20241002 by nitload youmu, intrinsic function rebuild, syntax style arrange
This commit is contained in:
@@ -28,7 +28,7 @@
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using StgSharp.Data;
|
||||
using StgSharp.Data.Intrinsic;
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
@@ -46,38 +46,30 @@ namespace StgSharp.Data
|
||||
|
||||
private byte[] currentKey;
|
||||
|
||||
|
||||
private readonly byte[] originKey;
|
||||
private int cycleCount;
|
||||
private SHA512 hash;
|
||||
|
||||
public RandomGenerator() : this(DateTime.UtcNow.ToBinary())
|
||||
{
|
||||
public RandomGenerator() : this( DateTime.UtcNow.ToBinary() ) { }
|
||||
|
||||
}
|
||||
|
||||
public RandomGenerator(object source)
|
||||
public RandomGenerator( object source )
|
||||
{
|
||||
try
|
||||
{
|
||||
if (source == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(source));
|
||||
try {
|
||||
if( source == null ) {
|
||||
throw new ArgumentNullException( nameof( source ) );
|
||||
}
|
||||
XmlSerializer s = new XmlSerializer(source.GetType());
|
||||
using (StringWriter sw = new StringWriter())
|
||||
{
|
||||
s.Serialize(sw, source);
|
||||
XmlSerializer s = new XmlSerializer( source.GetType() );
|
||||
using( StringWriter sw = new StringWriter() ) {
|
||||
s.Serialize( sw, source );
|
||||
string data = sw.ToString();
|
||||
originKey = Encoding.UTF8.GetBytes(data);
|
||||
originKey = Encoding.UTF8.GetBytes( data );
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
catch( Exception ) {
|
||||
throw;
|
||||
}
|
||||
hash = SHA512.Create();
|
||||
originKey = hash.ComputeHash(originKey);
|
||||
originKey = hash.ComputeHash( originKey );
|
||||
currentKey = originKey;
|
||||
cycleCount = 0;
|
||||
}
|
||||
@@ -105,54 +97,44 @@ namespace StgSharp.Data
|
||||
|
||||
/**/
|
||||
|
||||
return source.Read<int>(cycleCount % 4);
|
||||
return source.Read<int>( cycleCount % 4 );
|
||||
}
|
||||
|
||||
public byte GenRandomInt8()
|
||||
{
|
||||
cycleCount++;
|
||||
if (cycleCount % 64 == 0)
|
||||
{
|
||||
if( cycleCount % 64 == 0 ) {
|
||||
PrivateRand();
|
||||
}
|
||||
return currentKey[cycleCount % 64];
|
||||
return currentKey[ cycleCount % 64 ];
|
||||
}
|
||||
|
||||
public float GenRandomSingle()
|
||||
{
|
||||
cycleCount++;
|
||||
|
||||
while (true)
|
||||
{
|
||||
while( true ) {
|
||||
M128 source = PrivateRand();
|
||||
|
||||
uint
|
||||
temp0 = source.Read<uint>(0),
|
||||
temp1 = source.Read<uint>(2);
|
||||
temp0 = source.Read<uint>( 0 ),
|
||||
temp1 = source.Read<uint>( 2 );
|
||||
|
||||
if (temp0 * temp1 != 0)
|
||||
{
|
||||
float ret = ((temp0 % temp1) * 1.0f) / temp1;
|
||||
if (ret < 0.335f)
|
||||
{
|
||||
ret = ret * ((ret * (-0.7811934f)) + 1.4489113f);
|
||||
if( temp0 * temp1 != 0 ) {
|
||||
float ret = ( ( temp0 % temp1 ) * 1.0f ) / temp1;
|
||||
if( ret < 0.335f ) {
|
||||
ret = ret * ( ( ret * ( -0.7811934f ) ) + 1.4489113f );
|
||||
} else {
|
||||
ret = ( ret * ( ( ret * ( -0.1158368f ) ) + 1.0580600f ) ) + 0.0590584f;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = (ret * ((ret * (-0.1158368f)) + 1.0580600f)) + 0.0590584f;
|
||||
}
|
||||
if (ret > 1)
|
||||
{
|
||||
if( ret > 1 ) {
|
||||
continue;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
currentKey = originKey;
|
||||
@@ -161,24 +143,27 @@ namespace StgSharp.Data
|
||||
|
||||
private unsafe M128 PrivateRand()
|
||||
{
|
||||
currentKey = hash.ComputeHash(currentKey);
|
||||
currentKey = hash.ComputeHash( currentKey );
|
||||
M128 ret;
|
||||
fixed (byte* bptr = currentKey)
|
||||
{
|
||||
switch ((cycleCount % 16) / 4)
|
||||
{
|
||||
fixed( byte* bptr = currentKey ) {
|
||||
switch( ( cycleCount % 16 ) / 4 ) {
|
||||
case 0:
|
||||
ret = *(((M128*)bptr) + 0); break;
|
||||
ret = *( ( ( M128* )bptr ) + 0 );
|
||||
break;
|
||||
case 1:
|
||||
ret = *(((M128*)bptr) + 1); break;
|
||||
ret = *( ( ( M128* )bptr ) + 1 );
|
||||
break;
|
||||
case 2:
|
||||
ret = *(((M128*)bptr) + 2); break;
|
||||
ret = *( ( ( M128* )bptr ) + 2 );
|
||||
break;
|
||||
case 3:
|
||||
ret = *(((M128*)bptr) + 3); break;
|
||||
ret = *( ( ( M128* )bptr ) + 3 );
|
||||
break;
|
||||
default:
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
//Console.WriteLine(ret.ToString(16));
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user