#include "game_types.h"

/* Physics_Cleanup @ 0x08019B88 (real 136B) -- memmove with backward/word/burst paths. Mirrors src/matched/Physics_Cleanup.c. */
/* memmove(dst, src, n): backward byte loop on overlap, 16-word bursts when
   4-aligned (singles folded into the byte tail -- same bytes), byte tail
   otherwise. Returns dst. */
void *Physics_Cleanup(void *dst, void *src, unsigned n)
{
    char *d = dst;
    const char *s = src;
    if (s < d && d < s + n) {
        d += n;
        s += n;
        while (n-- > 0)
            *--d = *--s;
    } else {
        while (n >= 16 && (((unsigned)d | (unsigned)s) & 3) == 0) {
            *(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-- > 0)
            *d++ = *s++;
    }
    return dst;
}
