summaryrefslogtreecommitdiff
path: root/src/Runtime/__mem.c
blob: 84703b790f11aba83851a303d251f5bf78046e01 (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
#include "string.h"

void* memset(void* str, int c, size_t n) {
  __fill_mem(str, c, n);
  return str;
}

#define cps ((unsigned char*)src)
#define cpd ((unsigned char*)dst)
#define lps ((unsigned long*)src)
#define lpd ((unsigned long*)dst)
#define deref_auto_inc(p) *++(p)

void __fill_mem(void* dst, int val, size_t n) {
  unsigned long v = (unsigned char)val;
  unsigned long i;

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

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

    if (i) {
      n -= i;

      do
        deref_auto_inc(cpd) = v;
      while (--i);
    }

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

    lpd = ((unsigned long*)(cpd + 1)) - 1;

    i = n >> 5;

    if (i)
      do {
        deref_auto_inc(lpd) = v;
        deref_auto_inc(lpd) = v;
        deref_auto_inc(lpd) = v;
        deref_auto_inc(lpd) = v;
        deref_auto_inc(lpd) = v;
        deref_auto_inc(lpd) = v;
        deref_auto_inc(lpd) = v;
        deref_auto_inc(lpd) = v;
      } while (--i);

    i = (n & 31) >> 2;

    if (i)
      do
        deref_auto_inc(lpd) = v;
      while (--i);

    cpd = ((unsigned char*)(lpd + 1)) - 1;

    n &= 3;
  }

  if (n)
    do
      deref_auto_inc(cpd) = v;
    while (--n);

  return;
}

void* memcpy(void* dst, const void* src, size_t n) {
  const char* p;
  char* q;
  int rev = ((unsigned long)src < (unsigned long)dst);

  if (!rev) {

    for (p = (const char*)src - 1, q = (char*)dst - 1, n++; --n;)
      *++q = *++p;

  } else {
    for (p = (const char*)src + n, q = (char*)dst + n, n++; --n;)
      *--q = *--p;
  }
  return (dst);
}