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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [string/] [memset.c] - Diff between revs 1010 and 1765

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 1010 Rev 1765
/*
/*
FUNCTION
FUNCTION
        <<memset>>---set an area of memory
        <<memset>>---set an area of memory
 
 
INDEX
INDEX
        memset
        memset
 
 
ANSI_SYNOPSIS
ANSI_SYNOPSIS
        #include <string.h>
        #include <string.h>
        void *memset(const void *<[dst]>, int <[c]>, size_t <[length]>);
        void *memset(const void *<[dst]>, int <[c]>, size_t <[length]>);
 
 
TRAD_SYNOPSIS
TRAD_SYNOPSIS
        #include <string.h>
        #include <string.h>
        void *memset(<[dst]>, <[c]>, <[length]>)
        void *memset(<[dst]>, <[c]>, <[length]>)
        void *<[dst]>;
        void *<[dst]>;
        int <[c]>;
        int <[c]>;
        size_t <[length]>;
        size_t <[length]>;
 
 
DESCRIPTION
DESCRIPTION
        This function converts the argument <[c]> into an unsigned
        This function converts the argument <[c]> into an unsigned
        char and fills the first <[length]> characters of the array
        char and fills the first <[length]> characters of the array
        pointed to by <[dst]> to the value.
        pointed to by <[dst]> to the value.
 
 
RETURNS
RETURNS
        <<memset>> returns the value of <[m]>.
        <<memset>> returns the value of <[m]>.
 
 
PORTABILITY
PORTABILITY
<<memset>> is ANSI C.
<<memset>> is ANSI C.
 
 
    <<memset>> requires no supporting OS subroutines.
    <<memset>> requires no supporting OS subroutines.
 
 
QUICKREF
QUICKREF
        memset ansi pure
        memset ansi pure
*/
*/
 
 
#include <string.h>
#include <string.h>
 
 
#define LBLOCKSIZE (sizeof(long))
#define LBLOCKSIZE (sizeof(long))
#define UNALIGNED(X)   ((long)X & (LBLOCKSIZE - 1))
#define UNALIGNED(X)   ((long)X & (LBLOCKSIZE - 1))
#define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE)
#define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE)
 
 
_PTR
_PTR
_DEFUN (memset, (m, c, n),
_DEFUN (memset, (m, c, n),
        _PTR m _AND
        _PTR m _AND
        int c _AND
        int c _AND
        size_t n)
        size_t n)
{
{
#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
  char *s = (char *) m;
  char *s = (char *) m;
 
 
  while (n-- != 0)
  while (n-- != 0)
    {
    {
      *s++ = (char) c;
      *s++ = (char) c;
    }
    }
 
 
  return m;
  return m;
#else
#else
  char *s = (char *) m;
  char *s = (char *) m;
  int i;
  int i;
  unsigned long buffer;
  unsigned long buffer;
  unsigned long *aligned_addr;
  unsigned long *aligned_addr;
 
 
  if (!TOO_SMALL (n) && !UNALIGNED (m))
  if (!TOO_SMALL (n) && !UNALIGNED (m))
    {
    {
      /* If we get this far, we know that n is large and m is word-aligned. */
      /* If we get this far, we know that n is large and m is word-aligned. */
 
 
      aligned_addr = (unsigned long*)m;
      aligned_addr = (unsigned long*)m;
 
 
      /* Store C into each char sized location in BUFFER so that
      /* Store C into each char sized location in BUFFER so that
         we can set large blocks quickly.  */
         we can set large blocks quickly.  */
      c &= 0xff;
      c &= 0xff;
      if (LBLOCKSIZE == 4)
      if (LBLOCKSIZE == 4)
        {
        {
          buffer = (c << 8) | c;
          buffer = (c << 8) | c;
          buffer |= (buffer << 16);
          buffer |= (buffer << 16);
        }
        }
      else
      else
        {
        {
          buffer = 0;
          buffer = 0;
          for (i = 0; i < LBLOCKSIZE; i++)
          for (i = 0; i < LBLOCKSIZE; i++)
            buffer = (buffer << 8) | c;
            buffer = (buffer << 8) | c;
        }
        }
 
 
      while (n >= LBLOCKSIZE*4)
      while (n >= LBLOCKSIZE*4)
        {
        {
          *aligned_addr++ = buffer;
          *aligned_addr++ = buffer;
          *aligned_addr++ = buffer;
          *aligned_addr++ = buffer;
          *aligned_addr++ = buffer;
          *aligned_addr++ = buffer;
          *aligned_addr++ = buffer;
          *aligned_addr++ = buffer;
          n -= 4*LBLOCKSIZE;
          n -= 4*LBLOCKSIZE;
        }
        }
 
 
      while (n >= LBLOCKSIZE)
      while (n >= LBLOCKSIZE)
        {
        {
          *aligned_addr++ = buffer;
          *aligned_addr++ = buffer;
          n -= LBLOCKSIZE;
          n -= LBLOCKSIZE;
        }
        }
      /* Pick up the remainder with a bytewise loop.  */
      /* Pick up the remainder with a bytewise loop.  */
      s = (char*)aligned_addr;
      s = (char*)aligned_addr;
    }
    }
 
 
  while (n--)
  while (n--)
    {
    {
      *s++ = (char)c;
      *s++ = (char)c;
    }
    }
 
 
  return m;
  return m;
#endif /* not PREFER_SIZE_OVER_SPEED */
#endif /* not PREFER_SIZE_OVER_SPEED */
}
}
 
 

powered by: WebSVN 2.1.0

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