summaryrefslogtreecommitdiff
path: root/sysdeps/i386/i686/multiarch/memmove.c
blob: 9e5ad6dc1a7876464b2ea58cac9d339c31959337 (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
#include <stddef.h>

void *
__memmove (void *dst, const void *src, size_t n)
{
  const char *s = src;
  char *d = dst;
  void *ret = dst;
  size_t offset_src = ((size_t) s) & (sizeof(size_t) - 1);
  size_t offset_dst = ((size_t) d) & (sizeof(size_t) - 1);

  if (offset_src != offset_dst)
  {
    if (s < d)
    {
      // backward copying
      d += n;
      s += n;
      while (n--)
        *--d = *--s;
    }
    else
      // forward copying
      while (n--)
        *d++ = *s++;
  }
  else
  {
    if (s < d)
    {
		offset_src = (offset_src + (size_t)src) & (sizeof(size_t) - 1);
      // backward copying
      d += n;
      s += n;
      while (n-- && offset_src--)
        *--d = *--s;
      n++;
      if (!n) return ret;
      void **d1 = (void **)d;
      void **s1 = (void **)s;
      while (n >= sizeof(void *))
      {
        n -= sizeof(void *);
        *--d1 = *--s1;
      }
      s = (char *)s1;
      d = (char *)d1;
      while (n--)
        *--d = *--s;
    }
    else
    {
		if (offset_src) offset_src = sizeof(size_t) - offset_src;
      // forward copying
      while (n-- && offset_src--)
        *d++ = *s++;
      n++;
      if (!n) return ret;
      void **d1 = (void **)d;
      void **s1 = (void **)s;
      while (n >= sizeof(void *))
      {
        n -= sizeof(void *);
        *d1++ = *s1++;
      }
      s = (char *)s1;
      d = (char *)d1;
      while (n--)
        *d++ = *s++;
    }
  }
  return ret;
}

weak_alias (__memmove, __GI_memmove)
weak_alias (__memmove, memmove)