2 * This file is part of the libpayload project.
4 * It has originally been taken from the HelenOS project
5 * (http://www.helenos.eu), and slightly modified for our purposes.
7 * Copyright (c) 2005 Martin Decky
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * - Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * - The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <libpayload.h>
36 static void *default_memset(void *const s
, const int c
, size_t n
)
40 unsigned long w
= c
& 0xff;
42 const u8
*const aligned_start
=
43 (const u8
*)ALIGN_UP((uintptr_t)dst
, sizeof(unsigned long));
44 for (; n
> 0 && dst
!= aligned_start
; --n
, ++dst
)
47 for (i
= 1; i
< sizeof(unsigned long); i
<<= 1)
48 w
= (w
<< (i
* 8)) | w
;
50 for (i
= 0; i
< n
/ sizeof(unsigned long); i
++)
51 ((unsigned long *)dst
)[i
] = w
;
53 dst
+= i
* sizeof(unsigned long);
55 for (i
= 0; i
< n
% sizeof(unsigned long); i
++)
61 void *memset(void *s
, int c
, size_t n
)
62 __attribute__((weak
, alias("default_memset")));
64 static void *default_memcpy(void *dst
, const void *src
, size_t n
)
69 if (IS_ALIGNED((uintptr_t)dst
, sizeof(unsigned long)) &&
70 IS_ALIGNED((uintptr_t)src
, sizeof(unsigned long))) {
71 for (i
= 0; i
< n
/ sizeof(unsigned long); i
++)
72 ((unsigned long *)dst
)[i
] = ((unsigned long *)src
)[i
];
74 src
+= i
* sizeof(unsigned long);
75 dst
+= i
* sizeof(unsigned long);
76 n
-= i
* sizeof(unsigned long);
79 for (i
= 0; i
< n
; i
++)
80 ((u8
*)dst
)[i
] = ((u8
*)src
)[i
];
85 void *memcpy(void *dst
, const void *src
, size_t n
)
86 __attribute__((weak
, alias("default_memcpy")));
88 static void *default_memmove(void *dst
, const void *src
, size_t n
)
94 return memcpy(dst
, src
, n
);
96 if (!IS_ALIGNED((uintptr_t)dst
, sizeof(unsigned long)) ||
97 !IS_ALIGNED((uintptr_t)src
, sizeof(unsigned long))) {
98 for (i
= n
- 1; i
>= 0; i
--)
99 ((u8
*)dst
)[i
] = ((u8
*)src
)[i
];
103 offs
= n
- (n
% sizeof(unsigned long));
105 for (i
= (n
% sizeof(unsigned long)) - 1; i
>= 0; i
--)
106 ((u8
*)dst
)[i
+ offs
] = ((u8
*)src
)[i
+ offs
];
108 for (i
= n
/ sizeof(unsigned long) - 1; i
>= 0; i
--)
109 ((unsigned long *)dst
)[i
] = ((unsigned long *)src
)[i
];
114 void *memmove(void *dst
, const void *src
, size_t n
)
115 __attribute__((weak
, alias("default_memmove")));
118 * Compare two memory areas.
120 * @param s1 Pointer to the first area to compare.
121 * @param s2 Pointer to the second area to compare.
122 * @param n Size of the first area in bytes (both must have the same length).
123 * @return If n is 0, return zero. Otherwise, return a value less than, equal
124 * to, or greater than zero if s1 is found less than, equal to, or
125 * greater than s2 respectively.
128 static int default_memcmp(const void *s1
, const void *s2
, size_t n
)
131 const unsigned long *w1
= s1
, *w2
= s2
;
133 if (IS_ALIGNED((uintptr_t)s1
, sizeof(unsigned long)) &&
134 IS_ALIGNED((uintptr_t)s2
, sizeof(unsigned long)))
135 for (; i
< n
/ sizeof(unsigned long); i
++)
137 break; /* fall through to find differing byte */
139 for (i
*= sizeof(unsigned long); i
< n
; i
++)
140 if (((u8
*)s1
)[i
] != ((u8
*)s2
)[i
])
141 return ((u8
*)s1
)[i
] - ((u8
*)s2
)[i
];
146 int memcmp(const void *s1
, const void *s2
, size_t n
)
147 __attribute__((weak
, alias("default_memcmp")));