http://uos-embedded.googlecode.com/svn/trunk/sources/runtime/memset-fast.c

/*
FUNCTION
	<<memset>>---set an area of memory

INDEX
	memset

ANSI_SYNOPSIS
	#include <string.h>
	void *memset(const void *<[dst]>, int <[c]>, size_t <[length]>);

TRAD_SYNOPSIS
	#include <string.h>
	void *memset(<[dst]>, <[c]>, <[length]>)
	void *<[dst]>;
	int <[c]>;
	size_t <[length]>;

DESCRIPTION
	This function converts the argument <[c]> into an unsigned
	char and fills the first <[length]> characters of the array
	pointed to by <[dst]> to the value.

RETURNS
	<<memset>> returns the value of <[m]>.

PORTABILITY
<<memset>> is ANSI C.

    <<memset>> requires no supporting OS subroutines.

QUICKREF
	memset ansi pure
*/
#include <runtime/lib.h>

#undef memset

#define LBLOCKSIZE	(sizeof(long))
#define UNALIGNED(X)	((long)X & (LBLOCKSIZE - 1))
#define TOO_SMALL(LEN)	((LEN) < LBLOCKSIZE)

void *
memset(void *m, unsigned char c, size_t n)
{
  unsigned char *s;
  unsigned long buffer;

  if (n == 0)
    return m;

  s = (unsigned char*) m;
  while (UNALIGNED (s))
    {
      *s++ = c;
      if (--n == 0)
        return m;
    }

  if (! TOO_SMALL (n))
    {
      /* If we get this far, we know that n is large and s is word-aligned. */

      /* Store D into each char sized location in BUFFER so that
         we can set large blocks quickly.  */
      buffer = c;
      buffer |= (buffer << 8);
      buffer |= (buffer << 16);
      if (LBLOCKSIZE > 4)
        buffer |= (buffer << 16) << 16;

      while (n >= LBLOCKSIZE*4)
        {
          *((unsigned long*) s) = buffer;	s += LBLOCKSIZE;
          *((unsigned long*) s) = buffer;	s += LBLOCKSIZE;
          *((unsigned long*) s) = buffer;	s += LBLOCKSIZE;
          *((unsigned long*) s) = buffer;	s += LBLOCKSIZE;
          n -= 4*LBLOCKSIZE;
        }

      while (n >= LBLOCKSIZE)
        {
          *((unsigned long*) s) = buffer;	s += LBLOCKSIZE;
          n -= LBLOCKSIZE;
        }
    }

  /* Pick up the remainder with a bytewise loop.  */
  while (n--)
    {
      *s++ = c;
    }

  return m;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值