1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
3*4882a593Smuzhiyun * Copyright (C) 2008-2009 PetaLogix
4*4882a593Smuzhiyun * Copyright (C) 2007 John Williams
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Reasonably optimised generic C-code for memset on Microblaze
7*4882a593Smuzhiyun * This is generic C code to do efficient, alignment-aware memcpy.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * It is based on demo code originally Copyright 2001 by Intel Corp, taken from
10*4882a593Smuzhiyun * http://www.embedded.com/showArticle.jhtml?articleID=19205567
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Attempts were made, unsuccessfully, to contact the original
13*4882a593Smuzhiyun * author of this code (Michael Morrow, Intel). Below is the original
14*4882a593Smuzhiyun * copyright notice.
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * This software has been developed by Intel Corporation.
17*4882a593Smuzhiyun * Intel specifically disclaims all warranties, express or
18*4882a593Smuzhiyun * implied, and all liability, including consequential and
19*4882a593Smuzhiyun * other indirect damages, for the use of this program, including
20*4882a593Smuzhiyun * liability for infringement of any proprietary rights,
21*4882a593Smuzhiyun * and including the warranties of merchantability and fitness
22*4882a593Smuzhiyun * for a particular purpose. Intel does not assume any
23*4882a593Smuzhiyun * responsibility for and errors which may appear in this program
24*4882a593Smuzhiyun * not any responsibility to update it.
25*4882a593Smuzhiyun */
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include <linux/export.h>
28*4882a593Smuzhiyun #include <linux/types.h>
29*4882a593Smuzhiyun #include <linux/stddef.h>
30*4882a593Smuzhiyun #include <linux/compiler.h>
31*4882a593Smuzhiyun #include <linux/string.h>
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #ifdef __HAVE_ARCH_MEMSET
34*4882a593Smuzhiyun #ifndef CONFIG_OPT_LIB_FUNCTION
memset(void * v_src,int c,__kernel_size_t n)35*4882a593Smuzhiyun void *memset(void *v_src, int c, __kernel_size_t n)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun char *src = v_src;
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun /* Truncate c to 8 bits */
40*4882a593Smuzhiyun c = (c & 0xFF);
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /* Simple, byte oriented memset or the rest of count. */
43*4882a593Smuzhiyun while (n--)
44*4882a593Smuzhiyun *src++ = c;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun return v_src;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun #else /* CONFIG_OPT_LIB_FUNCTION */
memset(void * v_src,int c,__kernel_size_t n)49*4882a593Smuzhiyun void *memset(void *v_src, int c, __kernel_size_t n)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun char *src = v_src;
52*4882a593Smuzhiyun uint32_t *i_src;
53*4882a593Smuzhiyun uint32_t w32 = 0;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /* Truncate c to 8 bits */
56*4882a593Smuzhiyun c = (c & 0xFF);
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun if (unlikely(c)) {
59*4882a593Smuzhiyun /* Make a repeating word out of it */
60*4882a593Smuzhiyun w32 = c;
61*4882a593Smuzhiyun w32 |= w32 << 8;
62*4882a593Smuzhiyun w32 |= w32 << 16;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun if (likely(n >= 4)) {
66*4882a593Smuzhiyun /* Align the destination to a word boundary */
67*4882a593Smuzhiyun /* This is done in an endian independent manner */
68*4882a593Smuzhiyun switch ((unsigned) src & 3) {
69*4882a593Smuzhiyun case 1:
70*4882a593Smuzhiyun *src++ = c;
71*4882a593Smuzhiyun --n;
72*4882a593Smuzhiyun case 2:
73*4882a593Smuzhiyun *src++ = c;
74*4882a593Smuzhiyun --n;
75*4882a593Smuzhiyun case 3:
76*4882a593Smuzhiyun *src++ = c;
77*4882a593Smuzhiyun --n;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun i_src = (void *)src;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /* Do as many full-word copies as we can */
83*4882a593Smuzhiyun for (; n >= 4; n -= 4)
84*4882a593Smuzhiyun *i_src++ = w32;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun src = (void *)i_src;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun /* Simple, byte oriented memset or the rest of count. */
90*4882a593Smuzhiyun while (n--)
91*4882a593Smuzhiyun *src++ = c;
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun return v_src;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun #endif /* CONFIG_OPT_LIB_FUNCTION */
96*4882a593Smuzhiyun EXPORT_SYMBOL(memset);
97*4882a593Smuzhiyun #endif /* __HAVE_ARCH_MEMSET */
98