summaryrefslogtreecommitdiff
path: root/libs/PowerPC_EABI_Support/Runtime/Src/__mem.c
blob: 84c77474c0d1c6b739edd6023cc4720fa85b3eee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifdef __REVOLUTION_SDK__
#include <revolution.h>
#else
#include <dolphin.h>
#endif

__declspec(section ".init") void* memcpy(void* dst, const void* src, size_t n) {
    const unsigned char* s;
    unsigned char* d;

    if ((unsigned long)src >= (unsigned long)dst) {
        s = (const unsigned char*)src - 1;
        d = (unsigned char*)dst - 1;
        n++;
        while (--n != 0)
            *++d = *++s;
    } else {
        s = (const unsigned char*)src + n;
        d = (unsigned char*)dst + n;
        n++;
        while (--n != 0)
            *--d = *--s;
    }
    return dst;
}

__declspec(section ".init") void __fill_mem(void* dst, int val, size_t n) {
    unsigned long v = (unsigned char)val;
    unsigned long i;

    ((unsigned char*)dst) = ((unsigned char*)dst) - 1;

    if (n >= 32) {
        i = (~(unsigned long)dst) & 3;

        if (i) {
            n -= i;

            do {
                *++(((unsigned char*)dst)) = v;
            } while (--i);
        }

        if (v)
            v |= v << 24 | v << 16 | v << 8;

        ((unsigned long*)dst) = ((unsigned long*)(((unsigned char*)dst) + 1)) - 1;

        i = n >> 5;

        if (i) {
            do {
                *++((unsigned long*)dst) = v;
                *++((unsigned long*)dst) = v;
                *++((unsigned long*)dst) = v;
                *++((unsigned long*)dst) = v;
                *++((unsigned long*)dst) = v;
                *++((unsigned long*)dst) = v;
                *++((unsigned long*)dst) = v;
                *++((unsigned long*)dst) = v;
            } while (--i);
        }

        i = (n & 31) >> 2;

        if (i) {
            do {
                *++((unsigned long*)dst) = v;
            } while (--i);
        }

        ((unsigned char*)dst) = ((unsigned char*)(((unsigned long*)dst) + 1)) - 1;

        n &= 3;
    }

    if (n)
        do {
            *++((unsigned char*)dst) = v;
        } while (--n);

    return;
}

__declspec(section ".init") void* memset(void* dst, int val, size_t n) {
    __fill_mem(dst, val, n);

    return dst;
}