1
2
3
4
5
6
7
8
9
10
11#include <linux/types.h>
12#include <linux/compiler.h>
13#include <asm/string.h>
14
15typedef uint32_t op_t;
16
17void *memset(void *dstpp, int c, size_t len)
18{
19 int d0;
20 unsigned long int dstp = (unsigned long int) dstpp;
21
22
23 register op_t x asm("ax");
24
25 x = (unsigned char) c;
26
27
28 asm volatile("cld");
29
30
31 if (len >= 12) {
32
33 x |= (x << 8);
34 x |= (x << 16);
35
36
37 len -= (-dstp) % sizeof(op_t);
38
39
40
41
42
43
44
45 asm volatile(
46 "rep\n"
47 "stosb" :
48 "=D" (dstp), "=c" (d0) :
49 "0" (dstp), "1" ((-dstp) % sizeof(op_t)), "a" (x) :
50 "memory");
51
52
53 asm volatile(
54 "rep\n"
55 "stosl" :
56 "=D" (dstp), "=c" (d0) :
57 "0" (dstp), "1" (len / sizeof(op_t)), "a" (x) :
58 "memory");
59 len %= sizeof(op_t);
60 }
61
62
63 asm volatile(
64 "rep\n"
65 "stosb" :
66 "=D" (dstp), "=c" (d0) :
67 "0" (dstp), "1" (len), "a" (x) :
68 "memory");
69
70 return dstpp;
71}
72
73#define OP_T_THRES 8
74#define OPSIZ (sizeof(op_t))
75
76#define BYTE_COPY_FWD(dst_bp, src_bp, nbytes) \
77do { \
78 int __d0; \
79 asm volatile( \
80 \
81 "cld\n" \
82 \
83 "rep\n" \
84 "movsb" : \
85 "=D" (dst_bp), "=S" (src_bp), "=c" (__d0) : \
86 "0" (dst_bp), "1" (src_bp), "2" (nbytes) : \
87 "memory"); \
88} while (0)
89
90#define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes) \
91do { \
92 int __d0; \
93 asm volatile( \
94 \
95 "cld\n" \
96 \
97 "rep\n" \
98 "movsl" : \
99 "=D" (dst_bp), "=S" (src_bp), "=c" (__d0) : \
100 "0" (dst_bp), "1" (src_bp), "2" ((nbytes) / 4) : \
101 "memory"); \
102 (nbytes_left) = (nbytes) % 4; \
103} while (0)
104
105void *memcpy(void *dstpp, const void *srcpp, size_t len)
106{
107 unsigned long int dstp = (long int)dstpp;
108 unsigned long int srcp = (long int)srcpp;
109
110
111
112
113 if (len >= OP_T_THRES) {
114
115 len -= (-dstp) % OPSIZ;
116 BYTE_COPY_FWD(dstp, srcp, (-dstp) % OPSIZ);
117
118
119
120
121
122
123 WORD_COPY_FWD(dstp, srcp, len, len);
124
125
126 }
127
128
129 BYTE_COPY_FWD(dstp, srcp, len);
130
131 return dstpp;
132}
133