1*249415e0Skettenis /* $OpenBSD: bcopy.c,v 1.2 2017/01/24 08:09:05 kettenis Exp $ */
2899eb4daSderaadt
3899eb4daSderaadt /*-
4899eb4daSderaadt * Copyright (c) 1990 The Regents of the University of California.
5899eb4daSderaadt * All rights reserved.
6899eb4daSderaadt *
7899eb4daSderaadt * This code is derived from software contributed to Berkeley by
8899eb4daSderaadt * Chris Torek.
9899eb4daSderaadt *
10899eb4daSderaadt * Redistribution and use in source and binary forms, with or without
11899eb4daSderaadt * modification, are permitted provided that the following conditions
12899eb4daSderaadt * are met:
13899eb4daSderaadt * 1. Redistributions of source code must retain the above copyright
14899eb4daSderaadt * notice, this list of conditions and the following disclaimer.
15899eb4daSderaadt * 2. Redistributions in binary form must reproduce the above copyright
16899eb4daSderaadt * notice, this list of conditions and the following disclaimer in the
17899eb4daSderaadt * documentation and/or other materials provided with the distribution.
18899eb4daSderaadt * 3. Neither the name of the University nor the names of its contributors
19899eb4daSderaadt * may be used to endorse or promote products derived from this software
20899eb4daSderaadt * without specific prior written permission.
21899eb4daSderaadt *
22899eb4daSderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23899eb4daSderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24899eb4daSderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25899eb4daSderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26899eb4daSderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27899eb4daSderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28899eb4daSderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29899eb4daSderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30899eb4daSderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31899eb4daSderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32899eb4daSderaadt * SUCH DAMAGE.
33899eb4daSderaadt */
34899eb4daSderaadt
35899eb4daSderaadt #include <sys/types.h>
36899eb4daSderaadt #include <sys/systm.h>
37899eb4daSderaadt
38*249415e0Skettenis #undef bcopy
39*249415e0Skettenis
40899eb4daSderaadt /*
41899eb4daSderaadt * sizeof(word) MUST BE A POWER OF TWO
42899eb4daSderaadt * SO THAT wmask BELOW IS ALL ONES
43899eb4daSderaadt */
44899eb4daSderaadt typedef long word; /* "word" used for optimal copy speed */
45899eb4daSderaadt
46899eb4daSderaadt #define wsize sizeof(word)
47899eb4daSderaadt #define wmask (wsize - 1)
48899eb4daSderaadt
49899eb4daSderaadt /*
50899eb4daSderaadt * Copy a block of memory, handling overlap.
51899eb4daSderaadt * This is the routine that actually implements
52899eb4daSderaadt * (the portable versions of) bcopy, memcpy, and memmove.
53899eb4daSderaadt */
54899eb4daSderaadt void
bcopy(const void * src0,void * dst0,size_t length)55899eb4daSderaadt bcopy(const void *src0, void *dst0, size_t length)
56899eb4daSderaadt {
57899eb4daSderaadt char *dst = dst0;
58899eb4daSderaadt const char *src = src0;
59899eb4daSderaadt size_t t;
60899eb4daSderaadt
61899eb4daSderaadt if (length == 0 || dst == src) /* nothing to do */
62899eb4daSderaadt goto done;
63899eb4daSderaadt
64899eb4daSderaadt /*
65899eb4daSderaadt * Macros: loop-t-times; and loop-t-times, t>0
66899eb4daSderaadt */
67899eb4daSderaadt #define TLOOP(s) if (t) TLOOP1(s)
68899eb4daSderaadt #define TLOOP1(s) do { s; } while (--t)
69899eb4daSderaadt
70899eb4daSderaadt if ((unsigned long)dst < (unsigned long)src) {
71899eb4daSderaadt /*
72899eb4daSderaadt * Copy forward.
73899eb4daSderaadt */
74899eb4daSderaadt t = (long)src; /* only need low bits */
75899eb4daSderaadt if ((t | (long)dst) & wmask) {
76899eb4daSderaadt /*
77899eb4daSderaadt * Try to align operands. This cannot be done
78899eb4daSderaadt * unless the low bits match.
79899eb4daSderaadt */
80899eb4daSderaadt if ((t ^ (long)dst) & wmask || length < wsize)
81899eb4daSderaadt t = length;
82899eb4daSderaadt else
83899eb4daSderaadt t = wsize - (t & wmask);
84899eb4daSderaadt length -= t;
85899eb4daSderaadt TLOOP1(*dst++ = *src++);
86899eb4daSderaadt }
87899eb4daSderaadt /*
88899eb4daSderaadt * Copy whole words, then mop up any trailing bytes.
89899eb4daSderaadt */
90899eb4daSderaadt t = length / wsize;
91899eb4daSderaadt TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
92899eb4daSderaadt t = length & wmask;
93899eb4daSderaadt TLOOP(*dst++ = *src++);
94899eb4daSderaadt } else {
95899eb4daSderaadt /*
96899eb4daSderaadt * Copy backwards. Otherwise essentially the same.
97899eb4daSderaadt * Alignment works as before, except that it takes
98899eb4daSderaadt * (t&wmask) bytes to align, not wsize-(t&wmask).
99899eb4daSderaadt */
100899eb4daSderaadt src += length;
101899eb4daSderaadt dst += length;
102899eb4daSderaadt t = (long)src;
103899eb4daSderaadt if ((t | (long)dst) & wmask) {
104899eb4daSderaadt if ((t ^ (long)dst) & wmask || length <= wsize)
105899eb4daSderaadt t = length;
106899eb4daSderaadt else
107899eb4daSderaadt t &= wmask;
108899eb4daSderaadt length -= t;
109899eb4daSderaadt TLOOP1(*--dst = *--src);
110899eb4daSderaadt }
111899eb4daSderaadt t = length / wsize;
112899eb4daSderaadt TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src);
113899eb4daSderaadt t = length & wmask;
114899eb4daSderaadt TLOOP(*--dst = *--src);
115899eb4daSderaadt }
116899eb4daSderaadt done:
117899eb4daSderaadt return;
118899eb4daSderaadt }
119