#include "game_types.h"

/* Physics_MemCopyFast @ 0x08019B28 (real 94B) -- forward memcpy with word bursts, returns dst. Mirrors src/matched/Physics_MemCopyFast.c. */
/* Forward memcpy(d, s, n): 16-word bursts then singles when 4-aligned,
   -1-countdown byte tail otherwise. Returns d. (No overlap handling --
   use Physics_Cleanup for that.) */
void *Physics_MemCopyFast(unsigned char *d, unsigned char *s, unsigned n)
{
    unsigned char *p = d;
    if (n > 15 && (((unsigned)d | (unsigned)s) & 3) == 0) {
        do {
            *(int *)d = *(int *)s;
            d += 4;
            s += 4;
            n -= 4;
            *(int *)d = *(int *)s;
            d += 4;
            s += 4;
            n -= 4;
            *(int *)d = *(int *)s;
            d += 4;
            s += 4;
            n -= 4;
            *(int *)d = *(int *)s;
            d += 4;
            s += 4;
            n -= 4;
        } while (n > 15);
        while (n > 3) {
            *(int *)d = *(int *)s;
            d += 4;
            s += 4;
            n -= 4;
        }
        p = d;
    }
    {
        int left = (int)n - 1;
        if (left != -1) {
            do {
                *d++ = *s++;
            } while (--left != -1);
        }
    }
    return p;
}
