Imported Upstream version 16.04
[deb_dpdk.git] / lib / librte_eal / common / include / arch / ppc_64 / rte_memcpy.h
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) IBM Corporation 2014.
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  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of IBM Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #ifndef _RTE_MEMCPY_PPC_64_H_
34 #define _RTE_MEMCPY_PPC_64_H_
35
36 #include <stdint.h>
37 #include <string.h>
38 /*To include altivec.h, GCC version must  >= 4.8 */
39 #include <altivec.h>
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 #include "generic/rte_memcpy.h"
46
47 static inline void
48 rte_mov16(uint8_t *dst, const uint8_t *src)
49 {
50         vec_vsx_st(vec_vsx_ld(0, src), 0, dst);
51 }
52
53 static inline void
54 rte_mov32(uint8_t *dst, const uint8_t *src)
55 {
56         vec_vsx_st(vec_vsx_ld(0, src), 0, dst);
57         vec_vsx_st(vec_vsx_ld(16, src), 16, dst);
58 }
59
60 static inline void
61 rte_mov48(uint8_t *dst, const uint8_t *src)
62 {
63         vec_vsx_st(vec_vsx_ld(0, src), 0, dst);
64         vec_vsx_st(vec_vsx_ld(16, src), 16, dst);
65         vec_vsx_st(vec_vsx_ld(32, src), 32, dst);
66 }
67
68 static inline void
69 rte_mov64(uint8_t *dst, const uint8_t *src)
70 {
71         vec_vsx_st(vec_vsx_ld(0, src), 0, dst);
72         vec_vsx_st(vec_vsx_ld(16, src), 16, dst);
73         vec_vsx_st(vec_vsx_ld(32, src), 32, dst);
74         vec_vsx_st(vec_vsx_ld(48, src), 48, dst);
75 }
76
77 static inline void
78 rte_mov128(uint8_t *dst, const uint8_t *src)
79 {
80         vec_vsx_st(vec_vsx_ld(0, src), 0, dst);
81         vec_vsx_st(vec_vsx_ld(16, src), 16, dst);
82         vec_vsx_st(vec_vsx_ld(32, src), 32, dst);
83         vec_vsx_st(vec_vsx_ld(48, src), 48, dst);
84         vec_vsx_st(vec_vsx_ld(64, src), 64, dst);
85         vec_vsx_st(vec_vsx_ld(80, src), 80, dst);
86         vec_vsx_st(vec_vsx_ld(96, src), 96, dst);
87         vec_vsx_st(vec_vsx_ld(112, src), 112, dst);
88 }
89
90 static inline void
91 rte_mov256(uint8_t *dst, const uint8_t *src)
92 {
93         rte_mov128(dst, src);
94         rte_mov128(dst + 128, src + 128);
95 }
96
97 #define rte_memcpy(dst, src, n)              \
98         ({ (__builtin_constant_p(n)) ?       \
99         memcpy((dst), (src), (n)) :          \
100         rte_memcpy_func((dst), (src), (n)); })
101
102 static inline void *
103 rte_memcpy_func(void *dst, const void *src, size_t n)
104 {
105         void *ret = dst;
106
107         /* We can't copy < 16 bytes using XMM registers so do it manually. */
108         if (n < 16) {
109                 if (n & 0x01) {
110                         *(uint8_t *)dst = *(const uint8_t *)src;
111                         dst = (uint8_t *)dst + 1;
112                         src = (const uint8_t *)src + 1;
113                 }
114                 if (n & 0x02) {
115                         *(uint16_t *)dst = *(const uint16_t *)src;
116                         dst = (uint16_t *)dst + 1;
117                         src = (const uint16_t *)src + 1;
118                 }
119                 if (n & 0x04) {
120                         *(uint32_t *)dst = *(const uint32_t *)src;
121                         dst = (uint32_t *)dst + 1;
122                         src = (const uint32_t *)src + 1;
123                 }
124                 if (n & 0x08)
125                         *(uint64_t *)dst = *(const uint64_t *)src;
126                 return ret;
127         }
128
129         /* Special fast cases for <= 128 bytes */
130         if (n <= 32) {
131                 rte_mov16((uint8_t *)dst, (const uint8_t *)src);
132                 rte_mov16((uint8_t *)dst - 16 + n,
133                         (const uint8_t *)src - 16 + n);
134                 return ret;
135         }
136
137         if (n <= 64) {
138                 rte_mov32((uint8_t *)dst, (const uint8_t *)src);
139                 rte_mov32((uint8_t *)dst - 32 + n,
140                         (const uint8_t *)src - 32 + n);
141                 return ret;
142         }
143
144         if (n <= 128) {
145                 rte_mov64((uint8_t *)dst, (const uint8_t *)src);
146                 rte_mov64((uint8_t *)dst - 64 + n,
147                         (const uint8_t *)src - 64 + n);
148                 return ret;
149         }
150
151         /*
152          * For large copies > 128 bytes. This combination of 256, 64 and 16 byte
153          * copies was found to be faster than doing 128 and 32 byte copies as
154          * well.
155          */
156         for ( ; n >= 256; n -= 256) {
157                 rte_mov256((uint8_t *)dst, (const uint8_t *)src);
158                 dst = (uint8_t *)dst + 256;
159                 src = (const uint8_t *)src + 256;
160         }
161
162         /*
163          * We split the remaining bytes (which will be less than 256) into
164          * 64byte (2^6) chunks.
165          * Using incrementing integers in the case labels of a switch statement
166          * enourages the compiler to use a jump table. To get incrementing
167          * integers, we shift the 2 relevant bits to the LSB position to first
168          * get decrementing integers, and then subtract.
169          */
170         switch (3 - (n >> 6)) {
171         case 0x00:
172                 rte_mov64((uint8_t *)dst, (const uint8_t *)src);
173                 n -= 64;
174                 dst = (uint8_t *)dst + 64;
175                 src = (const uint8_t *)src + 64;      /* fallthrough */
176         case 0x01:
177                 rte_mov64((uint8_t *)dst, (const uint8_t *)src);
178                 n -= 64;
179                 dst = (uint8_t *)dst + 64;
180                 src = (const uint8_t *)src + 64;      /* fallthrough */
181         case 0x02:
182                 rte_mov64((uint8_t *)dst, (const uint8_t *)src);
183                 n -= 64;
184                 dst = (uint8_t *)dst + 64;
185                 src = (const uint8_t *)src + 64;      /* fallthrough */
186         default:
187                 ;
188         }
189
190         /*
191          * We split the remaining bytes (which will be less than 64) into
192          * 16byte (2^4) chunks, using the same switch structure as above.
193          */
194         switch (3 - (n >> 4)) {
195         case 0x00:
196                 rte_mov16((uint8_t *)dst, (const uint8_t *)src);
197                 n -= 16;
198                 dst = (uint8_t *)dst + 16;
199                 src = (const uint8_t *)src + 16;      /* fallthrough */
200         case 0x01:
201                 rte_mov16((uint8_t *)dst, (const uint8_t *)src);
202                 n -= 16;
203                 dst = (uint8_t *)dst + 16;
204                 src = (const uint8_t *)src + 16;      /* fallthrough */
205         case 0x02:
206                 rte_mov16((uint8_t *)dst, (const uint8_t *)src);
207                 n -= 16;
208                 dst = (uint8_t *)dst + 16;
209                 src = (const uint8_t *)src + 16;      /* fallthrough */
210         default:
211                 ;
212         }
213
214         /* Copy any remaining bytes, without going beyond end of buffers */
215         if (n != 0)
216                 rte_mov16((uint8_t *)dst - 16 + n,
217                         (const uint8_t *)src - 16 + n);
218         return ret;
219 }
220
221 #ifdef __cplusplus
222 }
223 #endif
224
225 #endif /* _RTE_MEMCPY_PPC_64_H_ */