1 /*
2  * LIBOIL - Library of Optimized Inner Loops
3  * Copyright (c) 2004 David A. Schleef <ds@schleef.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <string.h>
33 
34 #include <liboil/liboilfunction.h>
35 
36 OIL_DECLARE_CLASS (copy_u8);
37 
38 static void
copy_u8_libc(uint8_t * dest,const uint8_t * src,int n)39 copy_u8_libc (uint8_t *dest, const uint8_t *src, int n)
40 {
41   memcpy (dest, src, n);
42 }
43 OIL_DEFINE_IMPL (copy_u8_libc, copy_u8);
44 
45 static void
copy_u8_ptr(uint8_t * dest,const uint8_t * src,int n)46 copy_u8_ptr (uint8_t *dest, const uint8_t *src, int n)
47 {
48   while(n--) {
49     *dest++ = *src++;
50   }
51 }
52 OIL_DEFINE_IMPL (copy_u8_ptr, copy_u8);
53 
54 #ifdef HAVE_UNALIGNED_ACCESS
55 static void
copy_u8_ints(uint8_t * dest,const uint8_t * src,int n)56 copy_u8_ints (uint8_t *dest, const uint8_t *src, int n)
57 {
58   int i;
59   for(i=0;i<(n&3);i++){
60     *dest++ = *src++;
61   }
62   n >>= 2;
63   for(i=0;i<n;i++){
64     *(uint32_t *)dest = *(uint32_t *)src;
65     dest += 4;
66     src += 4;
67   }
68 }
69 OIL_DEFINE_IMPL (copy_u8_ints, copy_u8);
70 #endif
71 
72 #ifdef HAVE_UNALIGNED_ACCESS
73 /* Submitted by Adam Moss */
74 static void
copy_u8_llints(uint8_t * dest,const uint8_t * src,int n)75 copy_u8_llints (uint8_t *dest, const uint8_t *src, int n)
76 {
77   int i;
78   for(i=0;i<(n&7);i++){
79     *dest++ = *src++;
80   }
81   n >>= 3;
82   for(i=0;i<n;i++){
83     *(uint64_t *)dest = *(uint64_t *)src;
84     dest += 8;
85     src += 8;
86   }
87 }
88 OIL_DEFINE_IMPL (copy_u8_llints, copy_u8);
89 #endif
90 
91 #ifdef HAVE_UNALIGNED_ACCESS
92 /* Submitted by Adam Moss */
93 static void
copy_u8_llints_duff(uint8_t * dest,const uint8_t * src,int n)94 copy_u8_llints_duff (uint8_t *dest, const uint8_t *src, int n)
95 {
96   switch(n&7) {
97   case 7: *dest++ = *src++;
98   case 6: *dest++ = *src++;
99   case 5: *dest++ = *src++;
100   case 4: *dest++ = *src++;
101   case 3: *dest++ = *src++;
102   case 2: *dest++ = *src++;
103   case 1: *dest++ = *src++;
104   default: ;
105   }
106   n >>= 3;
107   while (n) {
108     switch (n & 15) {
109     default:
110     case 0: *(uint64_t *)dest = *(uint64_t *)src; dest += 8; src += 8;
111     case 15: *(uint64_t *)dest = *(uint64_t *)src; dest += 8; src += 8;
112     case 14: *(uint64_t *)dest = *(uint64_t *)src; dest += 8; src += 8;
113     case 13: *(uint64_t *)dest = *(uint64_t *)src; dest += 8; src += 8;
114     case 12: *(uint64_t *)dest = *(uint64_t *)src; dest += 8; src += 8;
115     case 11: *(uint64_t *)dest = *(uint64_t *)src; dest += 8; src += 8;
116     case 10: *(uint64_t *)dest = *(uint64_t *)src; dest += 8; src += 8;
117     case 9: *(uint64_t *)dest = *(uint64_t *)src; dest += 8; src += 8;
118     case 8: *(uint64_t *)dest = *(uint64_t *)src; dest += 8; src += 8;
119     case 7: *(uint64_t *)dest = *(uint64_t *)src; dest += 8; src += 8;
120     case 6: *(uint64_t *)dest = *(uint64_t *)src; dest += 8; src += 8;
121     case 5: *(uint64_t *)dest = *(uint64_t *)src; dest += 8; src += 8;
122     case 4: *(uint64_t *)dest = *(uint64_t *)src; dest += 8; src += 8;
123     case 3: *(uint64_t *)dest = *(uint64_t *)src; dest += 8; src += 8;
124     case 2: *(uint64_t *)dest = *(uint64_t *)src; dest += 8; src += 8;
125     case 1: *(uint64_t *)dest = *(uint64_t *)src; dest += 8; src += 8;
126     }
127     n = (n - 1) & ~(int)15;
128   }
129 }
130 OIL_DEFINE_IMPL (copy_u8_llints_duff, copy_u8);
131 #endif
132 
133