diff --git a/arithmetics/Words.cs b/arithmetics/Words.cs new file mode 100644 index 0000000..235fd9e --- /dev/null +++ b/arithmetics/Words.cs @@ -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; + } + + } +} diff --git a/ln.types.csproj b/ln.types.csproj index e610e5a..3bd2372 100644 --- a/ln.types.csproj +++ b/ln.types.csproj @@ -100,6 +100,7 @@ + @@ -114,6 +115,7 @@ +