public static int CompareInline(void *p1, void *p2, int size) { // If we use an unmanaged bulk version with an inline compare the caller site does not get optimized properly. // If you know you will be comparing big memory chunks do not use the inline version. int l = size; if (l > CompareInlineVsCallThreshold) { if (size >= 256) { return(UnmanagedMemory.Compare((byte *)p1, (byte *)p2, l)); } } byte *bpx = (byte *)p1, bpy = (byte *)p2; int last; for (int i = 0; i < l / 8; i++, bpx += 8, bpy += 8) { if (*((long *)bpx) != *((long *)bpy)) { last = 8; goto TAIL; } } if ((l & 4) != 0) { if (*((int *)bpx) != *((int *)bpy)) { last = 4; goto TAIL; } bpx += 4; bpy += 4; } if ((l & 2) != 0) { if (*((short *)bpx) != *((short *)bpy)) { last = 2; goto TAIL; } bpx += 2; bpy += 2; } if ((l & 1) != 0) { return(*((byte *)bpx) - *((byte *)bpy)); } return(0); TAIL: while (last > 0) { if (*((byte *)bpx) != *((byte *)bpy)) { return(*bpx - *bpy); } bpx++; bpy++; last--; } return(0); }
public unsafe static void BulkCopy(byte *dest, byte *src, int n) { UnmanagedMemory.Copy(dest, src, n); }
/// <summary> /// Bulk copy is optimized to handle copy operations where n is statistically big. While it will use a faster copy operation for /// small amounts of memory, when you have smaller than 2048 bytes calls (depending on the target CPU) it will always be /// faster to call .Copy() directly. /// </summary> private static void BulkCopy(byte *dest, byte *src, long n) { UnmanagedMemory.Copy(dest, src, n); }