using System; namespace ln.type.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; } } }