source: trunk/libs/newlib/src/newlib/libc/string/mempcpy.c @ 577

Last change on this file since 577 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 2.5 KB
Line 
1/*
2FUNCTION
3        <<mempcpy>>---copy memory regions and return end pointer
4
5SYNOPSIS
6        #include <string.h>
7        void* mempcpy(void *<[out]>, const void *<[in]>, size_t <[n]>);
8
9DESCRIPTION
10        This function copies <[n]> bytes from the memory region
11        pointed to by <[in]> to the memory region pointed to by
12        <[out]>.
13
14        If the regions overlap, the behavior is undefined.
15
16RETURNS
17        <<mempcpy>> returns a pointer to the byte following the
18        last byte copied to the <[out]> region.
19
20PORTABILITY
21<<mempcpy>> is a GNU extension.
22
23<<mempcpy>> requires no supporting OS subroutines.
24
25        */
26
27#include <_ansi.h>
28#include <stddef.h>
29#include <limits.h>
30#include <string.h>
31
32/* Nonzero if either X or Y is not aligned on a "long" boundary.  */
33#define UNALIGNED(X, Y) \
34  (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
35
36/* How many bytes are copied each iteration of the 4X unrolled loop.  */
37#define BIGBLOCKSIZE    (sizeof (long) << 2)
38
39/* How many bytes are copied each iteration of the word copy loop.  */
40#define LITTLEBLOCKSIZE (sizeof (long))
41
42/* Threshhold for punting to the byte copier.  */
43#define TOO_SMALL(LEN)  ((LEN) < BIGBLOCKSIZE)
44
45void *
46mempcpy (void *dst0,
47        const void *src0,
48        size_t len0)
49{
50#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
51  char *dst = (char *) dst0;
52  char *src = (char *) src0;
53
54  while (len0--)
55    {
56      *dst++ = *src++;
57    }
58
59  return dst;
60#else
61  char *dst = dst0;
62  const char *src = src0;
63  long *aligned_dst;
64  const long *aligned_src;
65
66  /* If the size is small, or either SRC or DST is unaligned,
67     then punt into the byte copy loop.  This should be rare.  */
68  if (!TOO_SMALL(len0) && !UNALIGNED (src, dst))
69    {
70      aligned_dst = (long*)dst;
71      aligned_src = (long*)src;
72
73      /* Copy 4X long words at a time if possible.  */
74      while (len0 >= BIGBLOCKSIZE)
75        {
76          *aligned_dst++ = *aligned_src++;
77          *aligned_dst++ = *aligned_src++;
78          *aligned_dst++ = *aligned_src++;
79          *aligned_dst++ = *aligned_src++;
80          len0 -= BIGBLOCKSIZE;
81        }
82
83      /* Copy one long word at a time if possible.  */
84      while (len0 >= LITTLEBLOCKSIZE)
85        {
86          *aligned_dst++ = *aligned_src++;
87          len0 -= LITTLEBLOCKSIZE;
88        }
89
90       /* Pick up any residual with a byte copier.  */
91      dst = (char*)aligned_dst;
92      src = (char*)aligned_src;
93    }
94
95  while (len0--)
96    *dst++ = *src++;
97
98  return dst;
99#endif /* not PREFER_SIZE_OVER_SPEED */
100}
Note: See TracBrowser for help on using the repository browser.