1*ea6088e7Sguenther /* $OpenBSD: memmove.c,v 1.3 2017/11/29 05:13:57 guenther Exp $ */
25b859c19Sderaadt /*-
35b859c19Sderaadt * Copyright (c) 1990 The Regents of the University of California.
45b859c19Sderaadt * All rights reserved.
55b859c19Sderaadt *
65b859c19Sderaadt * This code is derived from software contributed to Berkeley by
75b859c19Sderaadt * Chris Torek.
85b859c19Sderaadt *
95b859c19Sderaadt * Redistribution and use in source and binary forms, with or without
105b859c19Sderaadt * modification, are permitted provided that the following conditions
115b859c19Sderaadt * are met:
125b859c19Sderaadt * 1. Redistributions of source code must retain the above copyright
135b859c19Sderaadt * notice, this list of conditions and the following disclaimer.
145b859c19Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
155b859c19Sderaadt * notice, this list of conditions and the following disclaimer in the
165b859c19Sderaadt * documentation and/or other materials provided with the distribution.
175b859c19Sderaadt * 3. Neither the name of the University nor the names of its contributors
185b859c19Sderaadt * may be used to endorse or promote products derived from this software
195b859c19Sderaadt * without specific prior written permission.
205b859c19Sderaadt *
215b859c19Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
225b859c19Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235b859c19Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245b859c19Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
255b859c19Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265b859c19Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275b859c19Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285b859c19Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295b859c19Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305b859c19Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315b859c19Sderaadt * SUCH DAMAGE.
325b859c19Sderaadt */
335b859c19Sderaadt
345b859c19Sderaadt #include <string.h>
355b859c19Sderaadt
365b859c19Sderaadt /*
375b859c19Sderaadt * sizeof(word) MUST BE A POWER OF TWO
385b859c19Sderaadt * SO THAT wmask BELOW IS ALL ONES
395b859c19Sderaadt */
405b859c19Sderaadt typedef long word; /* "word" used for optimal copy speed */
415b859c19Sderaadt
425b859c19Sderaadt #define wsize sizeof(word)
435b859c19Sderaadt #define wmask (wsize - 1)
445b859c19Sderaadt
455b859c19Sderaadt /*
465b859c19Sderaadt * Copy a block of memory, handling overlap.
475b859c19Sderaadt */
485b859c19Sderaadt void *
memmove(void * dst0,const void * src0,size_t length)495b859c19Sderaadt memmove(void *dst0, const void *src0, size_t length)
505b859c19Sderaadt {
515b859c19Sderaadt char *dst = dst0;
525b859c19Sderaadt const char *src = src0;
535b859c19Sderaadt size_t t;
545b859c19Sderaadt
555b859c19Sderaadt if (length == 0 || dst == src) /* nothing to do */
565b859c19Sderaadt goto done;
575b859c19Sderaadt
585b859c19Sderaadt /*
595b859c19Sderaadt * Macros: loop-t-times; and loop-t-times, t>0
605b859c19Sderaadt */
615b859c19Sderaadt #define TLOOP(s) if (t) TLOOP1(s)
625b859c19Sderaadt #define TLOOP1(s) do { s; } while (--t)
635b859c19Sderaadt
645b859c19Sderaadt if ((unsigned long)dst < (unsigned long)src) {
655b859c19Sderaadt /*
665b859c19Sderaadt * Copy forward.
675b859c19Sderaadt */
685b859c19Sderaadt t = (long)src; /* only need low bits */
695b859c19Sderaadt if ((t | (long)dst) & wmask) {
705b859c19Sderaadt /*
715b859c19Sderaadt * Try to align operands. This cannot be done
725b859c19Sderaadt * unless the low bits match.
735b859c19Sderaadt */
745b859c19Sderaadt if ((t ^ (long)dst) & wmask || length < wsize)
755b859c19Sderaadt t = length;
765b859c19Sderaadt else
775b859c19Sderaadt t = wsize - (t & wmask);
785b859c19Sderaadt length -= t;
795b859c19Sderaadt TLOOP1(*dst++ = *src++);
805b859c19Sderaadt }
815b859c19Sderaadt /*
825b859c19Sderaadt * Copy whole words, then mop up any trailing bytes.
835b859c19Sderaadt */
845b859c19Sderaadt t = length / wsize;
855b859c19Sderaadt TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
865b859c19Sderaadt t = length & wmask;
875b859c19Sderaadt TLOOP(*dst++ = *src++);
885b859c19Sderaadt } else {
895b859c19Sderaadt /*
905b859c19Sderaadt * Copy backwards. Otherwise essentially the same.
915b859c19Sderaadt * Alignment works as before, except that it takes
925b859c19Sderaadt * (t&wmask) bytes to align, not wsize-(t&wmask).
935b859c19Sderaadt */
945b859c19Sderaadt src += length;
955b859c19Sderaadt dst += length;
965b859c19Sderaadt t = (long)src;
975b859c19Sderaadt if ((t | (long)dst) & wmask) {
985b859c19Sderaadt if ((t ^ (long)dst) & wmask || length <= wsize)
995b859c19Sderaadt t = length;
1005b859c19Sderaadt else
1015b859c19Sderaadt t &= wmask;
1025b859c19Sderaadt length -= t;
1035b859c19Sderaadt TLOOP1(*--dst = *--src);
1045b859c19Sderaadt }
1055b859c19Sderaadt t = length / wsize;
1065b859c19Sderaadt TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src);
1075b859c19Sderaadt t = length & wmask;
1085b859c19Sderaadt TLOOP(*--dst = *--src);
1095b859c19Sderaadt }
1105b859c19Sderaadt done:
1115b859c19Sderaadt return (dst0);
1125b859c19Sderaadt }
113*ea6088e7Sguenther DEF_BUILTIN(memmove);
114