1*0a6a1f1dSLionel Sambuc /* $NetBSD: memcpy.c,v 1.1 2014/09/03 19:34:25 matt Exp $ */
2*0a6a1f1dSLionel Sambuc /*-
3*0a6a1f1dSLionel Sambuc * Copyright (c) 2014 The NetBSD Foundation, Inc.
4*0a6a1f1dSLionel Sambuc * All rights reserved.
5*0a6a1f1dSLionel Sambuc *
6*0a6a1f1dSLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
7*0a6a1f1dSLionel Sambuc * by Matt Thomas of 3am Software Foundry.
8*0a6a1f1dSLionel Sambuc *
9*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
10*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
11*0a6a1f1dSLionel Sambuc * are met:
12*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
14*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
15*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
16*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
17*0a6a1f1dSLionel Sambuc *
18*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19*0a6a1f1dSLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20*0a6a1f1dSLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*0a6a1f1dSLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22*0a6a1f1dSLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*0a6a1f1dSLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*0a6a1f1dSLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*0a6a1f1dSLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*0a6a1f1dSLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*0a6a1f1dSLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*0a6a1f1dSLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
29*0a6a1f1dSLionel Sambuc */
30*0a6a1f1dSLionel Sambuc
31*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
32*0a6a1f1dSLionel Sambuc
33*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: memcpy.c,v 1.1 2014/09/03 19:34:25 matt Exp $");
34*0a6a1f1dSLionel Sambuc
35*0a6a1f1dSLionel Sambuc #include <stddef.h>
36*0a6a1f1dSLionel Sambuc #include <stdint.h>
37*0a6a1f1dSLionel Sambuc #include <string.h>
38*0a6a1f1dSLionel Sambuc
39*0a6a1f1dSLionel Sambuc static inline unsigned long
combine_words(unsigned long w1,unsigned long w2,int shift1,int shift2)40*0a6a1f1dSLionel Sambuc combine_words(unsigned long w1, unsigned long w2, int shift1, int shift2)
41*0a6a1f1dSLionel Sambuc {
42*0a6a1f1dSLionel Sambuc #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
43*0a6a1f1dSLionel Sambuc return (w1 << shift1) | (w2 >> shift2);
44*0a6a1f1dSLionel Sambuc #else
45*0a6a1f1dSLionel Sambuc return (w1 >> shift1) | (w2 << shift2);
46*0a6a1f1dSLionel Sambuc #endif
47*0a6a1f1dSLionel Sambuc }
48*0a6a1f1dSLionel Sambuc
49*0a6a1f1dSLionel Sambuc void *
memcpy(void * restrict a,const void * restrict b,size_t len)50*0a6a1f1dSLionel Sambuc memcpy(void * restrict a, const void * restrict b, size_t len)
51*0a6a1f1dSLionel Sambuc {
52*0a6a1f1dSLionel Sambuc const unsigned char *cb = b;
53*0a6a1f1dSLionel Sambuc unsigned char *ca = a;
54*0a6a1f1dSLionel Sambuc
55*0a6a1f1dSLionel Sambuc if (len == 0)
56*0a6a1f1dSLionel Sambuc return a;
57*0a6a1f1dSLionel Sambuc
58*0a6a1f1dSLionel Sambuc /*
59*0a6a1f1dSLionel Sambuc * Make sure the destination is long aligned.
60*0a6a1f1dSLionel Sambuc */
61*0a6a1f1dSLionel Sambuc while ((uintptr_t)ca & (sizeof(long) - 1)) {
62*0a6a1f1dSLionel Sambuc *ca++ = *cb++;
63*0a6a1f1dSLionel Sambuc if (--len == 0)
64*0a6a1f1dSLionel Sambuc return a;
65*0a6a1f1dSLionel Sambuc }
66*0a6a1f1dSLionel Sambuc
67*0a6a1f1dSLionel Sambuc unsigned long *la = (long *)ca;
68*0a6a1f1dSLionel Sambuc const int offset = (uintptr_t)cb & (sizeof(*la) - 1);
69*0a6a1f1dSLionel Sambuc const unsigned long *lb = (const unsigned long *) (cb - offset);
70*0a6a1f1dSLionel Sambuc unsigned long * const ea = la + len / sizeof(*la);
71*0a6a1f1dSLionel Sambuc
72*0a6a1f1dSLionel Sambuc if (offset == 0) {
73*0a6a1f1dSLionel Sambuc /*
74*0a6a1f1dSLionel Sambuc * a & b are now both long alignment.
75*0a6a1f1dSLionel Sambuc * First try to copy 4 longs at a time,
76*0a6a1f1dSLionel Sambuc */
77*0a6a1f1dSLionel Sambuc for (; la + 4 <= ea; la += 4, lb += 4) {
78*0a6a1f1dSLionel Sambuc la[0] = lb[0];
79*0a6a1f1dSLionel Sambuc la[1] = lb[1];
80*0a6a1f1dSLionel Sambuc la[2] = lb[2];
81*0a6a1f1dSLionel Sambuc la[3] = lb[3];
82*0a6a1f1dSLionel Sambuc }
83*0a6a1f1dSLionel Sambuc /*
84*0a6a1f1dSLionel Sambuc * Now try to copy one long at a time.
85*0a6a1f1dSLionel Sambuc */
86*0a6a1f1dSLionel Sambuc while (la <= ea) {
87*0a6a1f1dSLionel Sambuc *la++ = *lb++;
88*0a6a1f1dSLionel Sambuc }
89*0a6a1f1dSLionel Sambuc } else {
90*0a6a1f1dSLionel Sambuc const int shift1 = offset * 8;
91*0a6a1f1dSLionel Sambuc const int shift2 = sizeof(*la) * 8 - shift1;
92*0a6a1f1dSLionel Sambuc unsigned long w1 = *lb++;
93*0a6a1f1dSLionel Sambuc
94*0a6a1f1dSLionel Sambuc /*
95*0a6a1f1dSLionel Sambuc * We try to write 4 words per loop.
96*0a6a1f1dSLionel Sambuc */
97*0a6a1f1dSLionel Sambuc for (; la + 4 <= ea; la += 4, lb += 4) {
98*0a6a1f1dSLionel Sambuc unsigned long w2 = lb[0];
99*0a6a1f1dSLionel Sambuc
100*0a6a1f1dSLionel Sambuc la[0] = combine_words(w1, w2, shift1, shift2);
101*0a6a1f1dSLionel Sambuc
102*0a6a1f1dSLionel Sambuc w1 = lb[1];
103*0a6a1f1dSLionel Sambuc
104*0a6a1f1dSLionel Sambuc la[1] = combine_words(w2, w1, shift1, shift2);
105*0a6a1f1dSLionel Sambuc
106*0a6a1f1dSLionel Sambuc w2 = lb[2];
107*0a6a1f1dSLionel Sambuc
108*0a6a1f1dSLionel Sambuc la[2] = combine_words(w1, w2, shift1, shift2);
109*0a6a1f1dSLionel Sambuc
110*0a6a1f1dSLionel Sambuc w1 = lb[3];
111*0a6a1f1dSLionel Sambuc
112*0a6a1f1dSLionel Sambuc la[3] = combine_words(w2, w1, shift1, shift2);
113*0a6a1f1dSLionel Sambuc }
114*0a6a1f1dSLionel Sambuc
115*0a6a1f1dSLionel Sambuc /*
116*0a6a1f1dSLionel Sambuc * Now try to copy one long at a time.
117*0a6a1f1dSLionel Sambuc */
118*0a6a1f1dSLionel Sambuc while (la <= ea) {
119*0a6a1f1dSLionel Sambuc unsigned long w2 = *lb++;
120*0a6a1f1dSLionel Sambuc
121*0a6a1f1dSLionel Sambuc *la++ = combine_words(w1, w2, shift1, shift2);
122*0a6a1f1dSLionel Sambuc
123*0a6a1f1dSLionel Sambuc w1 = w2;
124*0a6a1f1dSLionel Sambuc }
125*0a6a1f1dSLionel Sambuc }
126*0a6a1f1dSLionel Sambuc len &= sizeof(*la) - 1;
127*0a6a1f1dSLionel Sambuc if (len) {
128*0a6a1f1dSLionel Sambuc cb = (const unsigned char *)lb + offset;
129*0a6a1f1dSLionel Sambuc ca = (unsigned char *)la;
130*0a6a1f1dSLionel Sambuc while (len-- > 0) {
131*0a6a1f1dSLionel Sambuc *ca++ = *cb++;
132*0a6a1f1dSLionel Sambuc }
133*0a6a1f1dSLionel Sambuc }
134*0a6a1f1dSLionel Sambuc return a;
135*0a6a1f1dSLionel Sambuc }
136