[corlib] Generic comparers from reference sources
[mono.git] / mcs / class / corlib / System / Buffer.cs
1 //
2 // System.Buffer.cs
3 //
4 // Authors:
5 //   Paolo Molaro (lupus@ximian.com)
6 //   Dan Lewis (dihlewis@yahoo.co.uk)
7 //
8 // (C) 2001 Ximian, Inc.  http://www.ximian.com
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36 using System.Diagnostics.Contracts;
37
38 namespace System {
39         [ComVisible (true)]
40         public static class Buffer {
41
42                 public static int ByteLength (Array array)
43                 {
44                         // note: the other methods in this class also use ByteLength to test for
45                         // null and non-primitive arguments as a side-effect.
46
47                         if (array == null)
48                                 throw new ArgumentNullException ("array");
49
50                         int length = ByteLengthInternal (array);
51                         if (length < 0)
52                                 throw new ArgumentException (Locale.GetText ("Object must be an array of primitives."));
53
54                         return length;
55                 }
56
57                 public static byte GetByte (Array array, int index)
58                 {
59                         if (index < 0 || index >= ByteLength (array))
60                                 throw new ArgumentOutOfRangeException ("index", Locale.GetText(
61                                         "Value must be non-negative and less than the size of the collection."));
62
63                         return GetByteInternal (array, index);
64                 }
65
66                 public static void SetByte (Array array, int index, byte value)
67                 {
68                         if (index < 0 || index >= ByteLength (array))
69                                 throw new ArgumentOutOfRangeException ("index", Locale.GetText(
70                                         "Value must be non-negative and less than the size of the collection."));
71
72                         SetByteInternal (array, index, value);
73                 }
74
75                 public static void BlockCopy (Array src, int srcOffset, Array dst, int dstOffset, int count)
76                 {
77                         if (src == null)
78                                 throw new ArgumentNullException ("src");
79
80                         if (dst == null)
81                                 throw new ArgumentNullException ("dst");
82
83                         if (srcOffset < 0)
84                                 throw new ArgumentOutOfRangeException ("srcOffset", Locale.GetText(
85                                         "Non-negative number required."));
86
87                         if (dstOffset < 0)
88                                 throw new ArgumentOutOfRangeException ("dstOffset", Locale.GetText (
89                                         "Non-negative number required."));
90
91                         if (count < 0)
92                                 throw new ArgumentOutOfRangeException ("count", Locale.GetText (
93                                         "Non-negative number required."));
94
95                         // We do the checks in unmanaged code for performance reasons
96                         bool res = BlockCopyInternal (src, srcOffset, dst, dstOffset, count);
97                         if (!res) {
98                                 // watch for integer overflow
99                                 if ((srcOffset > ByteLength (src) - count) || (dstOffset > ByteLength (dst) - count))
100                                         throw new ArgumentException (Locale.GetText (
101                                                 "Offset and length were out of bounds for the array or count is greater than " + 
102                                                 "the number of elements from index to the end of the source collection."));
103                         }
104                 }
105
106                 // private
107                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
108                 private extern static int ByteLengthInternal (Array array);
109
110                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
111                 private extern static byte GetByteInternal (Array array, int index);
112
113                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
114                 private extern static void SetByteInternal (Array array, int index, int value);
115
116                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
117                 internal extern static bool BlockCopyInternal (Array src, int src_offset, Array dest, int dest_offset, int count);
118
119                 internal static bool InternalBlockCopy (Array src, int src_offset, Array dest, int dest_offset, int count)
120                 {
121                         return BlockCopyInternal (src, src_offset, dest, dest_offset, count);
122                 }
123
124                 internal unsafe static void ZeroMemory (byte* src, long len)
125                 {
126                         while(len-- > 0)
127                                 *(src + len) = 0;
128                 }
129
130         internal unsafe static void Memcpy (byte* pDest, int destIndex, byte[] src, int srcIndex, int len)
131         {
132             Contract.Assert( (srcIndex >= 0) && (destIndex >= 0) && (len >= 0), "Index and length must be non-negative!");        
133             Contract.Assert(src.Length - srcIndex >= len, "not enough bytes in src");
134             // If dest has 0 elements, the fixed statement will throw an 
135             // IndexOutOfRangeException.  Special-case 0-byte copies.
136             if (len==0)
137                 return;
138             fixed(byte* pSrc = src) {
139                 Memcpy(pDest + destIndex, pSrc + srcIndex, len);
140             }
141         }
142
143         internal unsafe static void Memcpy(byte[] dest, int destIndex, byte* src, int srcIndex, int len) {
144             Contract.Assert( (srcIndex >= 0) && (destIndex >= 0) && (len >= 0), "Index and length must be non-negative!");
145             Contract.Assert(dest.Length - destIndex >= len, "not enough bytes in dest");
146             // If dest has 0 elements, the fixed statement will throw an 
147             // IndexOutOfRangeException.  Special-case 0-byte copies.
148             if (len==0)
149                 return;
150             fixed(byte* pDest = dest) {
151                 Memcpy(pDest + destIndex, src + srcIndex, len);
152             }
153         }
154
155                 internal static unsafe void memcpy4 (byte *dest, byte *src, int size) {
156                         /*while (size >= 32) {
157                                 // using long is better than int and slower than double
158                                 // FIXME: enable this only on correct alignment or on platforms
159                                 // that can tolerate unaligned reads/writes of doubles
160                                 ((double*)dest) [0] = ((double*)src) [0];
161                                 ((double*)dest) [1] = ((double*)src) [1];
162                                 ((double*)dest) [2] = ((double*)src) [2];
163                                 ((double*)dest) [3] = ((double*)src) [3];
164                                 dest += 32;
165                                 src += 32;
166                                 size -= 32;
167                         }*/
168                         while (size >= 16) {
169                                 ((int*)dest) [0] = ((int*)src) [0];
170                                 ((int*)dest) [1] = ((int*)src) [1];
171                                 ((int*)dest) [2] = ((int*)src) [2];
172                                 ((int*)dest) [3] = ((int*)src) [3];
173                                 dest += 16;
174                                 src += 16;
175                                 size -= 16;
176                         }
177                         while (size >= 4) {
178                                 ((int*)dest) [0] = ((int*)src) [0];
179                                 dest += 4;
180                                 src += 4;
181                                 size -= 4;
182                         }
183                         while (size > 0) {
184                                 ((byte*)dest) [0] = ((byte*)src) [0];
185                                 dest += 1;
186                                 src += 1;
187                                 --size;
188                         }
189                 }
190                 internal static unsafe void memcpy2 (byte *dest, byte *src, int size) {
191                         while (size >= 8) {
192                                 ((short*)dest) [0] = ((short*)src) [0];
193                                 ((short*)dest) [1] = ((short*)src) [1];
194                                 ((short*)dest) [2] = ((short*)src) [2];
195                                 ((short*)dest) [3] = ((short*)src) [3];
196                                 dest += 8;
197                                 src += 8;
198                                 size -= 8;
199                         }
200                         while (size >= 2) {
201                                 ((short*)dest) [0] = ((short*)src) [0];
202                                 dest += 2;
203                                 src += 2;
204                                 size -= 2;
205                         }
206                         if (size > 0)
207                                 ((byte*)dest) [0] = ((byte*)src) [0];
208                 }
209                 static unsafe void memcpy1 (byte *dest, byte *src, int size) {
210                         while (size >= 8) {
211                                 ((byte*)dest) [0] = ((byte*)src) [0];
212                                 ((byte*)dest) [1] = ((byte*)src) [1];
213                                 ((byte*)dest) [2] = ((byte*)src) [2];
214                                 ((byte*)dest) [3] = ((byte*)src) [3];
215                                 ((byte*)dest) [4] = ((byte*)src) [4];
216                                 ((byte*)dest) [5] = ((byte*)src) [5];
217                                 ((byte*)dest) [6] = ((byte*)src) [6];
218                                 ((byte*)dest) [7] = ((byte*)src) [7];
219                                 dest += 8;
220                                 src += 8;
221                                 size -= 8;
222                         }
223                         while (size >= 2) {
224                                 ((byte*)dest) [0] = ((byte*)src) [0];
225                                 ((byte*)dest) [1] = ((byte*)src) [1];
226                                 dest += 2;
227                                 src += 2;
228                                 size -= 2;
229                         }
230                         if (size > 0)
231                                 ((byte*)dest) [0] = ((byte*)src) [0];
232                 }
233
234                 internal static unsafe void Memcpy (byte *dest, byte *src, int size) {
235                         // FIXME: if pointers are not aligned, try to align them
236                         // so a faster routine can be used. Handle the case where
237                         // the pointers can't be reduced to have the same alignment
238                         // (just ignore the issue on x86?)
239                         if ((((int)dest | (int)src) & 3) != 0) {
240                                 if (((int)dest & 1) != 0 && ((int)src & 1) != 0 && size >= 1) {
241                                         dest [0] = src [0];
242                                         ++dest;
243                                         ++src;
244                                         --size;
245                                 }
246                                 if (((int)dest & 2) != 0 && ((int)src & 2) != 0 && size >= 2) {
247                                         ((short*)dest) [0] = ((short*)src) [0];
248                                         dest += 2;
249                                         src += 2;
250                                         size -= 2;
251                                 }
252                                 if ((((int)dest | (int)src) & 1) != 0) {
253                                         memcpy1 (dest, src, size);
254                                         return;
255                                 }
256                                 if ((((int)dest | (int)src) & 2) != 0) {
257                                         memcpy2 (dest, src, size);
258                                         return;
259                                 }
260                         }
261                         memcpy4 (dest, src, size);
262                 }
263
264         internal unsafe static int IndexOfByte(byte* src, byte value, int index, int count)
265         {
266             Contract.Assert(src != null, "src should not be null");
267
268             byte* pByte = src + index;
269
270             // Align up the pointer to sizeof(int).
271             while (((int)pByte & 3) != 0)
272             {
273                 if (count == 0)
274                     return -1;
275                 else if (*pByte == value)
276                     return (int) (pByte - src);
277
278                 count--;
279                 pByte++;
280             }
281
282             // Fill comparer with value byte for comparisons
283             //
284             // comparer = 0/0/value/value
285             uint comparer = (((uint)value << 8) + (uint)value);
286             // comparer = value/value/value/value
287             comparer = (comparer << 16) + comparer;
288
289             // Run through buffer until we hit a 4-byte section which contains
290             // the byte we're looking for or until we exhaust the buffer.
291             while (count > 3)
292             {
293                 // Test the buffer for presence of value. comparer contains the byte
294                 // replicated 4 times.
295                 uint t1 = *(uint*)pByte;
296                 t1 = t1 ^ comparer;
297                 uint t2 = 0x7efefeff + t1;
298                 t1 = t1 ^ 0xffffffff;
299                 t1 = t1 ^ t2;
300                 t1 = t1 & 0x81010100;
301
302                 // if t1 is zero then these 4-bytes don't contain a match
303                 if (t1 != 0)
304                 {
305                     // We've found a match for value, figure out which position it's in.
306                     int foundIndex = (int) (pByte - src);
307                     if (pByte[0] == value)
308                         return foundIndex;
309                     else if (pByte[1] == value)
310                         return foundIndex + 1;
311                     else if (pByte[2] == value)
312                         return foundIndex + 2;
313                     else if (pByte[3] == value)
314                         return foundIndex + 3;
315                 }
316
317                 count -= 4;
318                 pByte += 4;
319
320             }
321
322             // Catch any bytes that might be left at the tail of the buffer
323             while (count > 0)
324             {
325                 if (*pByte == value)
326                     return (int) (pByte - src);
327
328                 count--;
329                 pByte++;
330             }
331
332             // If we don't have a match return -1;
333             return -1;
334         }               
335         }
336 }