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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib/] [newlib/] [libc/] [string/] [memset.c] - Blame information for rev 39

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

Line No. Rev Author Line
1 39 lampret
/*
2
FUNCTION
3
        <<memset>>---set an area of memory
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
#define LBLOCKSIZE (sizeof(long))
39
#define UNALIGNED(X)   ((long)X & (LBLOCKSIZE - 1))
40
#define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE)
41
 
42
_PTR
43
_DEFUN (memset, (m, c, n),
44
        _PTR m _AND
45
        int c _AND
46
        size_t n)
47
{
48
#ifdef PREFER_SIZE_OVER_SPEED
49
  char *s = (char *) m;
50
 
51
  while (n-- != 0)
52
    {
53
      *s++ = (char) c;
54
    }
55
 
56
  return m;
57
#else
58
  char *s = (char *) m;
59
  int count, i;
60
  unsigned long buffer;
61
  unsigned long *aligned_addr;
62
  unsigned char *unaligned_addr;
63
 
64
  if (TOO_SMALL (n) || UNALIGNED (m))
65
    {
66
      while (n-- != 0)
67
        {
68
          *s++ = (char) c;
69
        }
70
      return m;
71
    }
72
 
73
  /* If we get this far, we know that n is large and m is word-aligned. */
74
 
75
  aligned_addr = (unsigned long*)m;
76
 
77
  /* Store C into each char sized location in BUFFER so that
78
     we can set large blocks quickly.  */
79
  c &= 0xff;
80
  if (LBLOCKSIZE == 4)
81
    {
82
      buffer = (c << 8) | c;
83
      buffer |= (buffer << 16);
84
    }
85
  else
86
    {
87
      buffer = 0;
88
      for (i = 0; i < LBLOCKSIZE; i++)
89
        buffer = (buffer << 8) | c;
90
    }
91
 
92
  while (n >= LBLOCKSIZE*4)
93
    {
94
      *aligned_addr++ = buffer;
95
      *aligned_addr++ = buffer;
96
      *aligned_addr++ = buffer;
97
      *aligned_addr++ = buffer;
98
      n -= 4*LBLOCKSIZE;
99
    }
100
 
101
  while (n >= LBLOCKSIZE)
102
    {
103
      *aligned_addr++ = buffer;
104
      n -= LBLOCKSIZE;
105
    }
106
 
107
  /* Pick up the remainder with a bytewise loop.  */
108
  unaligned_addr = (unsigned char*)aligned_addr;
109
  while (n)
110
    {
111
      *unaligned_addr++ = (char)c;
112
      n--;
113
    }
114
 
115
  return m;
116
#endif /* not PREFER_SIZE_OVER_SPEED */
117
}

powered by: WebSVN 2.1.0

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