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