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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [newlib-1.17.0/] [newlib/] [libc/] [machine/] [mips/] [memset.c] - Blame information for rev 855

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

Line No. Rev Author Line
1 148 jeremybenn
/*
2
FUNCTION
3
        <<memset>>---set an area of memory, optimized for the MIPS processors
4
 
5
INDEX
6
        memset
7
 
8
ANSI_SYNOPSIS
9
        #include <string.h>
10
        void *memset(const void *<[dst]>, int <[c]>, size_t <[length]>);
11
 
12
TRAD_SYNOPSIS
13
        #include <string.h>
14
        void *memset(<[dst]>, <[c]>, <[length]>)
15
        void *<[dst]>;
16
        int <[c]>;
17
        size_t <[length]>;
18
 
19
DESCRIPTION
20
        This function converts the argument <[c]> into an unsigned
21
        char and fills the first <[length]> characters of the array
22
        pointed to by <[dst]> to the value.
23
 
24
RETURNS
25
        <<memset>> returns the value of <[m]>.
26
 
27
PORTABILITY
28
<<memset>> is ANSI C.
29
 
30
    <<memset>> requires no supporting OS subroutines.
31
 
32
QUICKREF
33
        memset ansi pure
34
*/
35
 
36
#include <string.h>
37
 
38
#ifdef __mips64
39
#define wordtype long long
40
#else
41
#define wordtype long
42
#endif
43
 
44
#define LBLOCKSIZE     (sizeof(wordtype))
45
#define UNALIGNED(X)   ((long)(X) & (LBLOCKSIZE - 1))
46
#define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE * 4)
47
 
48
_PTR
49
_DEFUN (memset, (m, c, n),
50
        _PTR m _AND
51
        int c _AND
52
        size_t n)
53
{
54
#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) || defined(__mips16)
55
  char *s = (char *) m;
56
 
57
  while (n-- != 0)
58
    {
59
      *s++ = (char) c;
60
    }
61
 
62
  return m;
63
#else
64
  char *s = (char *) m;
65
  int i;
66
  unsigned wordtype buffer;
67
  unsigned wordtype *aligned_addr;
68
  unsigned short *short_addr;
69
  size_t iter;
70
 
71
  if (!TOO_SMALL (n))
72
    {
73
      int unaligned = UNALIGNED (s);
74
 
75
      /* We know that N is >= LBLOCKSIZE so we can just word
76
         align the S without having to check the length. */
77
 
78
      if (unaligned)
79
        {
80
          while (unaligned++ < LBLOCKSIZE)
81
            *s++ = (char)c, n--;
82
        }
83
 
84
      /* S is now word-aligned so we can process the remainder
85
         in word sized chunks except for a few (< LBLOCKSIZE)
86
         bytes which might be left over at the end. */
87
 
88
      aligned_addr = (unsigned wordtype *)s;
89
 
90
      /* Store C into each char sized location in BUFFER so that
91
         we can set large blocks quickly.  */
92
      c &= 0xff;
93
      buffer = c;
94
      if (buffer != 0)
95
        {
96
          if (LBLOCKSIZE == 4)
97
            {
98
               buffer |= (buffer << 8);
99
               buffer |= (buffer << 16);
100
            }
101
          else if (LBLOCKSIZE == 8)
102
            {
103
              buffer |= (buffer << 8);
104
              buffer |= (buffer << 16);
105
              buffer |= ((buffer << 31) << 1);
106
            }
107
          else
108
            {
109
              for (i = 1; i < LBLOCKSIZE; i++)
110
                buffer = (buffer << 8) | c;
111
            }
112
        }
113
 
114
      iter = n / (2*LBLOCKSIZE);
115
      n = n % (2*LBLOCKSIZE);
116
      while (iter > 0)
117
        {
118
          aligned_addr[0] = buffer;
119
          aligned_addr[1] = buffer;
120
          aligned_addr += 2;
121
          iter--;
122
        }
123
 
124
      if (n >= LBLOCKSIZE)
125
        {
126
          *aligned_addr++ = buffer;
127
          n -= LBLOCKSIZE;
128
        }
129
 
130
      /* Pick up the remainder with a bytewise loop.  */
131
      s = (char*)aligned_addr;
132
    }
133
 
134
  while (n > 0)
135
    {
136
      *s++ = (char)c;
137
      n--;
138
    }
139
 
140
  return m;
141
#endif /* not PREFER_SIZE_OVER_SPEED */
142
}

powered by: WebSVN 2.1.0

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