OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [libc/] [string/] [memccpy.c] - Blame information for rev 461

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
/*
2
FUNCTION
3
        <<memccpy>>---copy memory regions with end-token check
4
 
5
ANSI_SYNOPSIS
6
        #include <string.h>
7
        void* memccpy(void *<[out]>, const void *<[in]>,
8
                      int <[endchar]>, size_t <[n]>);
9
 
10
TRAD_SYNOPSIS
11
        void *memccpy(<[out]>, <[in]>, <[endchar]>, <[n]>
12
        void *<[out]>;
13
        void *<[in]>;
14
        int <[endchar]>;
15
        size_t <[n]>;
16
 
17
DESCRIPTION
18
        This function copies up to <[n]> bytes from the memory region
19
        pointed to by <[in]> to the memory region pointed to by
20
        <[out]>.  If a byte matching the <[endchar]> is encountered,
21
        the byte is copied and copying stops.
22
 
23
        If the regions overlap, the behavior is undefined.
24
 
25
RETURNS
26
        <<memccpy>> returns a pointer to the first byte following the
27
        <[endchar]> in the <[out]> region.  If no byte matching
28
        <[endchar]> was copied, then <<NULL>> is returned.
29
 
30
PORTABILITY
31
<<memccpy>> is a GNU extension.
32
 
33
<<memccpy>> requires no supporting OS subroutines.
34
 
35
        */
36
 
37
#include <_ansi.h>
38
#include <stddef.h>
39
#include <string.h>
40
#include <limits.h>
41
 
42
/* Nonzero if either X or Y is not aligned on a "long" boundary.  */
43
#define UNALIGNED(X, Y) \
44
  (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
45
 
46
/* How many bytes are copied each iteration of the word copy loop.  */
47
#define LITTLEBLOCKSIZE (sizeof (long))
48
 
49
/* Threshhold for punting to the byte copier.  */
50
#define TOO_SMALL(LEN)  ((LEN) < LITTLEBLOCKSIZE)
51
 
52
/* Macros for detecting endchar */
53
#if LONG_MAX == 2147483647L
54
#define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
55
#else
56
#if LONG_MAX == 9223372036854775807L
57
/* Nonzero if X (a long int) contains a NULL byte. */
58
#define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
59
#else
60
#error long int is not a 32bit or 64bit type.
61
#endif
62
#endif
63
 
64
 
65
_PTR
66
_DEFUN (memccpy, (dst0, src0, endchar, len0),
67
        _PTR dst0 _AND
68
        _CONST _PTR src0 _AND
69
        int endchar0 _AND
70
        size_t len0)
71
{
72
 
73
#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
74
  _PTR ptr = NULL;
75
  char *dst = (char *) dst0;
76
  char *src = (char *) src0;
77
  char endchar = endchar0 & 0xff;
78
 
79
  while (len0--)
80
    {
81
      if ((*dst++ = *src++) == endchar)
82
        {
83
          ptr = dst;
84
          break;
85
        }
86
    }
87
 
88
  return ptr;
89
#else
90
  _PTR ptr = NULL;
91
  char *dst = dst0;
92
  _CONST char *src = src0;
93
  long *aligned_dst;
94
  _CONST long *aligned_src;
95
  int   len =  len0;
96
  char endchar = endchar0 & 0xff;
97
 
98
  /* If the size is small, or either SRC or DST is unaligned,
99
     then punt into the byte copy loop.  This should be rare.  */
100
  if (!TOO_SMALL(len) && !UNALIGNED (src, dst))
101
    {
102
      int i;
103
      unsigned long mask = 0;
104
 
105
      aligned_dst = (long*)dst;
106
      aligned_src = (long*)src;
107
 
108
      /* The fast code reads the ASCII one word at a time and only
109
         performs the bytewise search on word-sized segments if they
110
         contain the search character, which is detected by XORing
111
         the word-sized segment with a word-sized block of the search
112
         character and then detecting for the presence of NULL in the
113
         result.  */
114
      for (i = 0; i < LITTLEBLOCKSIZE; i++)
115
        mask = (mask << 8) + endchar;
116
 
117
 
118
      /* Copy one long word at a time if possible.  */
119
      while (len >= LITTLEBLOCKSIZE)
120
        {
121
          unsigned long buffer = (unsigned long)(*aligned_src);
122
          buffer ^=  mask;
123
          if (DETECTNULL (buffer))
124
            break; /* endchar is found, go byte by byte from here */
125
          *aligned_dst++ = *aligned_src++;
126
          len -= LITTLEBLOCKSIZE;
127
        }
128
 
129
       /* Pick up any residual with a byte copier.  */
130
      dst = (char*)aligned_dst;
131
      src = (char*)aligned_src;
132
    }
133
 
134
  while (len--)
135
    {
136
      if ((*dst++ = *src++) == endchar)
137
        {
138
          ptr = dst;
139
          break;
140
        }
141
    }
142
 
143
  return ptr;
144
#endif /* not PREFER_SIZE_OVER_SPEED */
145
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.