Added class arithmetics.Words

dev_timestamp
Harald Wolff 2019-09-15 14:23:28 +02:00
parent 947737fb9c
commit 6932f9e851
2 changed files with 125 additions and 0 deletions

View File

@ -0,0 +1,123 @@
using System;
namespace ln.types.arithmetics
{
public static class Words
{
public static UInt16[] Add(UInt16[] a, UInt16 b)
{
UInt16[] b2 = new ushort[a.Length];
b2[0] = b;
return Add(a, b2);
}
public static UInt16[] Add(UInt16[] a, int b)
{
UInt16[] b2 = new ushort[a.Length];
b2[0] = (ushort)(b & 0xffff);
if (b2.Length > 1)
b2[1] = (ushort)((b >> 16) & 0xffff);
return Add(a, b2);
}
public static UInt16[] Add(UInt16[] a, UInt16[] b)
{
UInt16[] result = new ushort[a.Length > b.Length ? a.Length : b.Length];
int m = 0;
for (int n = 0; n < result.Length; n++)
{
m += (n < a.Length ? a[n] : 0) + (n < b.Length ? b[n] : 0);
result[n] = (ushort)(m & 0xFFFF);
m >>= 16;
}
return result;
}
public static UInt16[] Del(UInt16[] a, UInt16 b)
{
UInt16[] b2 = new ushort[a.Length];
b2[0] = b;
return Del(a, b2);
}
public static UInt16[] Del(UInt16[] a, int b)
{
UInt16[] b2 = new ushort[a.Length];
b2[0] = (ushort)(b & 0xffff);
if (b2.Length > 1)
b2[1] = (ushort)((b >> 16) & 0xffff);
return Del(a, b2);
}
public static UInt16[] Del(UInt16[] a, UInt16[] b)
{
UInt16[] result = new ushort[a.Length > b.Length ? a.Length : b.Length];
int m = 0;
for (int n = 0; n < result.Length; n++)
{
m += (n < a.Length ? a[n] : 0) - (n < b.Length ? b[n] : 0);
result[n] = (ushort)(m & 0xFFFF);
m >>= 16;
}
return result;
}
public static UInt16[] And(UInt16[] a, UInt16[] b)
{
UInt16[] result = new ushort[a.Length > b.Length ? a.Length : b.Length];
for (int n = 0; n < result.Length; n++)
{
result[n] = (ushort)((n < a.Length ? a[n] : 0) & (n < b.Length ? b[n] : 0));
}
return result;
}
public static UInt16[] Or(UInt16[] a, UInt16[] b)
{
UInt16[] result = new ushort[a.Length > b.Length ? a.Length : b.Length];
for (int n = 0; n < result.Length; n++)
{
result[n] = (ushort)((n < a.Length ? a[n] : 0) | (n < b.Length ? b[n] : 0));
}
return result;
}
public static UInt16[] SHL(UInt16[] src,int shift)
{
UInt16[] result = new ushort[src.Length];
int step = (shift >> 4);
shift &= 0xF;
uint m = 0;
for (int n = 0; (n+step) < src.Length; n++)
{
m |= src[n];
m <<= shift;
result[n + step] |= (ushort)(m & 0xFFFF);
m >>= 16;
}
return result;
}
public static ushort[] BigEndian(this ushort[] bytes)
{
if (BitConverter.IsLittleEndian)
{
bytes = bytes.Slice(0);
Array.Reverse(bytes);
}
return bytes;
}
}
}

View File

@ -100,6 +100,7 @@
<Compile Include="threads\DynamicPool.cs" />
<Compile Include="threads\PoolThread.cs" />
<Compile Include="odb\ng\DocumentChanges.cs" />
<Compile Include="arithmetics\Words.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="odb\" />
@ -114,6 +115,7 @@
<Folder Include="odb\ng\mappings\" />
<Folder Include="stream\" />
<Folder Include="odb\ng\index\" />
<Folder Include="arithmetics\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ln.logging\ln.logging.csproj">