Merge branch 'feature-concurrent-sweep'
[mono.git] / mcs / class / corlib / ReferenceSources / Buffer.cs
1 namespace System
2 {
3         partial class Buffer
4         {
5                 public static int ByteLength (Array array)
6                 {
7                         // note: the other methods in this class also use ByteLength to test for
8                         // null and non-primitive arguments as a side-effect.
9
10                         if (array == null)
11                                 throw new ArgumentNullException ("array");
12
13                         int length = _ByteLength (array);
14                         if (length < 0)
15                                 throw new ArgumentException (Locale.GetText ("Object must be an array of primitives."));
16
17                         return length;
18                 }
19
20                 public static byte GetByte (Array array, int index)
21                 {
22                         if (index < 0 || index >= ByteLength (array))
23                                 throw new ArgumentOutOfRangeException ("index");
24
25                         return _GetByte (array, index);
26                 }
27
28                 public static void SetByte (Array array, int index, byte value)
29                 {
30                         if (index < 0 || index >= ByteLength (array))
31                                 throw new ArgumentOutOfRangeException ("index");
32
33                         _SetByte (array, index, value);
34                 }
35
36                 public static void BlockCopy (Array src, int srcOffset, Array dst, int dstOffset, int count)
37                 {
38                         if (src == null)
39                                 throw new ArgumentNullException ("src");
40
41                         if (dst == null)
42                                 throw new ArgumentNullException ("dst");
43
44                         if (srcOffset < 0)
45                                 throw new ArgumentOutOfRangeException ("srcOffset", Locale.GetText(
46                                         "Non-negative number required."));
47
48                         if (dstOffset < 0)
49                                 throw new ArgumentOutOfRangeException ("dstOffset", Locale.GetText (
50                                         "Non-negative number required."));
51
52                         if (count < 0)
53                                 throw new ArgumentOutOfRangeException ("count", Locale.GetText (
54                                         "Non-negative number required."));
55
56                         // We do the checks in unmanaged code for performance reasons
57                         bool res = InternalBlockCopy (src, srcOffset, dst, dstOffset, count);
58                         if (!res) {
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."));
64                         }
65                 }
66
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];
76                                 dest += 32;
77                                 src += 32;
78                                 size -= 32;
79                         }*/
80                         while (size >= 16) {
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];
85                                 dest += 16;
86                                 src += 16;
87                                 size -= 16;
88                         }
89                         while (size >= 4) {
90                                 ((int*)dest) [0] = ((int*)src) [0];
91                                 dest += 4;
92                                 src += 4;
93                                 size -= 4;
94                         }
95                         while (size > 0) {
96                                 ((byte*)dest) [0] = ((byte*)src) [0];
97                                 dest += 1;
98                                 src += 1;
99                                 --size;
100                         }
101                 }
102                 internal static unsafe void memcpy2 (byte *dest, byte *src, int size) {
103                         while (size >= 8) {
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];
108                                 dest += 8;
109                                 src += 8;
110                                 size -= 8;
111                         }
112                         while (size >= 2) {
113                                 ((short*)dest) [0] = ((short*)src) [0];
114                                 dest += 2;
115                                 src += 2;
116                                 size -= 2;
117                         }
118                         if (size > 0)
119                                 ((byte*)dest) [0] = ((byte*)src) [0];
120                 }
121                 static unsafe void memcpy1 (byte *dest, byte *src, int size) {
122                         while (size >= 8) {
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];
131                                 dest += 8;
132                                 src += 8;
133                                 size -= 8;
134                         }
135                         while (size >= 2) {
136                                 ((byte*)dest) [0] = ((byte*)src) [0];
137                                 ((byte*)dest) [1] = ((byte*)src) [1];
138                                 dest += 2;
139                                 src += 2;
140                                 size -= 2;
141                         }
142                         if (size > 0)
143                                 ((byte*)dest) [0] = ((byte*)src) [0];
144                 }
145
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) {
153                                         dest [0] = src [0];
154                                         ++dest;
155                                         ++src;
156                                         --size;
157                                 }
158                                 if (((int)dest & 2) != 0 && ((int)src & 2) != 0 && size >= 2) {
159                                         ((short*)dest) [0] = ((short*)src) [0];
160                                         dest += 2;
161                                         src += 2;
162                                         size -= 2;
163                                 }
164                                 if ((((int)dest | (int)src) & 1) != 0) {
165                                         memcpy1 (dest, src, size);
166                                         return;
167                                 }
168                                 if ((((int)dest | (int)src) & 2) != 0) {
169                                         memcpy2 (dest, src, size);
170                                         return;
171                                 }
172                         }
173                         memcpy4 (dest, src, size);
174                 }
175         }
176 }