5 public static int ByteLength (Array array)
7 // note: the other methods in this class also use ByteLength to test for
8 // null and non-primitive arguments as a side-effect.
11 throw new ArgumentNullException ("array");
13 int length = _ByteLength (array);
15 throw new ArgumentException (Locale.GetText ("Object must be an array of primitives."));
20 public static byte GetByte (Array array, int index)
22 if (index < 0 || index >= ByteLength (array))
23 throw new ArgumentOutOfRangeException ("index");
25 return _GetByte (array, index);
28 public static void SetByte (Array array, int index, byte value)
30 if (index < 0 || index >= ByteLength (array))
31 throw new ArgumentOutOfRangeException ("index");
33 _SetByte (array, index, value);
36 public static void BlockCopy (Array src, int srcOffset, Array dst, int dstOffset, int count)
39 throw new ArgumentNullException ("src");
42 throw new ArgumentNullException ("dst");
45 throw new ArgumentOutOfRangeException ("srcOffset", Locale.GetText(
46 "Non-negative number required."));
49 throw new ArgumentOutOfRangeException ("dstOffset", Locale.GetText (
50 "Non-negative number required."));
53 throw new ArgumentOutOfRangeException ("count", Locale.GetText (
54 "Non-negative number required."));
56 // We do the checks in unmanaged code for performance reasons
57 bool res = InternalBlockCopy (src, srcOffset, dst, dstOffset, count);
59 // watch for integer overflow
60 if ((srcOffset > ByteLength (src) - count) || (dstOffset > ByteLength (dst) - count))
61 throw new ArgumentException (Locale.GetText (
62 "Offset and length were out of bounds for the array or count is greater than " +
63 "the number of elements from index to the end of the source collection."));
67 internal static unsafe void memcpy4 (byte *dest, byte *src, int size) {
68 /*while (size >= 32) {
69 // using long is better than int and slower than double
70 // FIXME: enable this only on correct alignment or on platforms
71 // that can tolerate unaligned reads/writes of doubles
72 ((double*)dest) [0] = ((double*)src) [0];
73 ((double*)dest) [1] = ((double*)src) [1];
74 ((double*)dest) [2] = ((double*)src) [2];
75 ((double*)dest) [3] = ((double*)src) [3];
81 ((int*)dest) [0] = ((int*)src) [0];
82 ((int*)dest) [1] = ((int*)src) [1];
83 ((int*)dest) [2] = ((int*)src) [2];
84 ((int*)dest) [3] = ((int*)src) [3];
90 ((int*)dest) [0] = ((int*)src) [0];
96 ((byte*)dest) [0] = ((byte*)src) [0];
102 internal static unsafe void memcpy2 (byte *dest, byte *src, int size) {
104 ((short*)dest) [0] = ((short*)src) [0];
105 ((short*)dest) [1] = ((short*)src) [1];
106 ((short*)dest) [2] = ((short*)src) [2];
107 ((short*)dest) [3] = ((short*)src) [3];
113 ((short*)dest) [0] = ((short*)src) [0];
119 ((byte*)dest) [0] = ((byte*)src) [0];
121 static unsafe void memcpy1 (byte *dest, byte *src, int size) {
123 ((byte*)dest) [0] = ((byte*)src) [0];
124 ((byte*)dest) [1] = ((byte*)src) [1];
125 ((byte*)dest) [2] = ((byte*)src) [2];
126 ((byte*)dest) [3] = ((byte*)src) [3];
127 ((byte*)dest) [4] = ((byte*)src) [4];
128 ((byte*)dest) [5] = ((byte*)src) [5];
129 ((byte*)dest) [6] = ((byte*)src) [6];
130 ((byte*)dest) [7] = ((byte*)src) [7];
136 ((byte*)dest) [0] = ((byte*)src) [0];
137 ((byte*)dest) [1] = ((byte*)src) [1];
143 ((byte*)dest) [0] = ((byte*)src) [0];
146 internal static unsafe void Memcpy (byte *dest, byte *src, int size) {
147 // FIXME: if pointers are not aligned, try to align them
148 // so a faster routine can be used. Handle the case where
149 // the pointers can't be reduced to have the same alignment
150 // (just ignore the issue on x86?)
151 if ((((int)dest | (int)src) & 3) != 0) {
152 if (((int)dest & 1) != 0 && ((int)src & 1) != 0 && size >= 1) {
158 if (((int)dest & 2) != 0 && ((int)src & 2) != 0 && size >= 2) {
159 ((short*)dest) [0] = ((short*)src) [0];
164 if ((((int)dest | (int)src) & 1) != 0) {
165 memcpy1 (dest, src, size);
168 if ((((int)dest | (int)src) & 2) != 0) {
169 memcpy2 (dest, src, size);
173 memcpy4 (dest, src, size);