Trivial: Cleanup some typos.
[vpp.git] / src / vppinfra / memcpy_avx512.h
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*-
16  *   BSD LICENSE
17  *
18  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
19  *   All rights reserved.
20  *
21  *   Redistribution and use in source and binary forms, with or without
22  *   modification, are permitted provided that the following conditions
23  *   are met:
24  *
25  *     * Redistributions of source code must retain the above copyright
26  *       notice, this list of conditions and the following disclaimer.
27  *     * Redistributions in binary form must reproduce the above copyright
28  *       notice, this list of conditions and the following disclaimer in
29  *       the documentation and/or other materials provided with the
30  *       distribution.
31  *     * Neither the name of Intel Corporation nor the names of its
32  *       contributors may be used to endorse or promote products derived
33  *       from this software without specific prior written permission.
34  *
35  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46  */
47
48 #ifndef included_clib_memcpy_avx512_h
49 #define included_clib_memcpy_avx512_h
50
51 #include <stdint.h>
52 #include <x86intrin.h>
53
54 static inline void
55 clib_mov16 (u8 * dst, const u8 * src)
56 {
57   __m128i xmm0;
58
59   xmm0 = _mm_loadu_si128 ((const __m128i *) src);
60   _mm_storeu_si128 ((__m128i *) dst, xmm0);
61 }
62
63 static inline void
64 clib_mov32 (u8 * dst, const u8 * src)
65 {
66   __m256i ymm0;
67
68   ymm0 = _mm256_loadu_si256 ((const __m256i *) src);
69   _mm256_storeu_si256 ((__m256i *) dst, ymm0);
70 }
71
72 static inline void
73 clib_mov64 (u8 * dst, const u8 * src)
74 {
75   __m512i zmm0;
76
77   zmm0 = _mm512_loadu_si512 ((const void *) src);
78   _mm512_storeu_si512 ((void *) dst, zmm0);
79 }
80
81 static inline void
82 clib_mov128 (u8 * dst, const u8 * src)
83 {
84   clib_mov64 (dst + 0 * 64, src + 0 * 64);
85   clib_mov64 (dst + 1 * 64, src + 1 * 64);
86 }
87
88 static inline void
89 clib_mov256 (u8 * dst, const u8 * src)
90 {
91   clib_mov128 (dst + 0 * 128, src + 0 * 128);
92   clib_mov128 (dst + 1 * 128, src + 1 * 128);
93 }
94
95 static inline void
96 clib_mov128blocks (u8 * dst, const u8 * src, size_t n)
97 {
98   __m512i zmm0, zmm1;
99
100   while (n >= 128)
101     {
102       zmm0 = _mm512_loadu_si512 ((const void *) (src + 0 * 64));
103       n -= 128;
104       zmm1 = _mm512_loadu_si512 ((const void *) (src + 1 * 64));
105       src = src + 128;
106       _mm512_storeu_si512 ((void *) (dst + 0 * 64), zmm0);
107       _mm512_storeu_si512 ((void *) (dst + 1 * 64), zmm1);
108       dst = dst + 128;
109     }
110 }
111
112 static inline void
113 clib_mov512blocks (u8 * dst, const u8 * src, size_t n)
114 {
115   __m512i zmm0, zmm1, zmm2, zmm3, zmm4, zmm5, zmm6, zmm7;
116
117   while (n >= 512)
118     {
119       zmm0 = _mm512_loadu_si512 ((const void *) (src + 0 * 64));
120       n -= 512;
121       zmm1 = _mm512_loadu_si512 ((const void *) (src + 1 * 64));
122       zmm2 = _mm512_loadu_si512 ((const void *) (src + 2 * 64));
123       zmm3 = _mm512_loadu_si512 ((const void *) (src + 3 * 64));
124       zmm4 = _mm512_loadu_si512 ((const void *) (src + 4 * 64));
125       zmm5 = _mm512_loadu_si512 ((const void *) (src + 5 * 64));
126       zmm6 = _mm512_loadu_si512 ((const void *) (src + 6 * 64));
127       zmm7 = _mm512_loadu_si512 ((const void *) (src + 7 * 64));
128       src = src + 512;
129       _mm512_storeu_si512 ((void *) (dst + 0 * 64), zmm0);
130       _mm512_storeu_si512 ((void *) (dst + 1 * 64), zmm1);
131       _mm512_storeu_si512 ((void *) (dst + 2 * 64), zmm2);
132       _mm512_storeu_si512 ((void *) (dst + 3 * 64), zmm3);
133       _mm512_storeu_si512 ((void *) (dst + 4 * 64), zmm4);
134       _mm512_storeu_si512 ((void *) (dst + 5 * 64), zmm5);
135       _mm512_storeu_si512 ((void *) (dst + 6 * 64), zmm6);
136       _mm512_storeu_si512 ((void *) (dst + 7 * 64), zmm7);
137       dst = dst + 512;
138     }
139 }
140
141 static inline void *
142 clib_memcpy (void *dst, const void *src, size_t n)
143 {
144   uword dstu = (uword) dst;
145   uword srcu = (uword) src;
146   void *ret = dst;
147   size_t dstofss;
148   size_t bits;
149
150         /**
151          * Copy less than 16 bytes
152          */
153   if (n < 16)
154     {
155       if (n & 0x01)
156         {
157           *(u8 *) dstu = *(const u8 *) srcu;
158           srcu = (uword) ((const u8 *) srcu + 1);
159           dstu = (uword) ((u8 *) dstu + 1);
160         }
161       if (n & 0x02)
162         {
163           *(u16 *) dstu = *(const u16 *) srcu;
164           srcu = (uword) ((const u16 *) srcu + 1);
165           dstu = (uword) ((u16 *) dstu + 1);
166         }
167       if (n & 0x04)
168         {
169           *(u32 *) dstu = *(const u32 *) srcu;
170           srcu = (uword) ((const u32 *) srcu + 1);
171           dstu = (uword) ((u32 *) dstu + 1);
172         }
173       if (n & 0x08)
174         *(u64 *) dstu = *(const u64 *) srcu;
175       return ret;
176     }
177
178         /**
179          * Fast way when copy size doesn't exceed 512 bytes
180          */
181   if (n <= 32)
182     {
183       clib_mov16 ((u8 *) dst, (const u8 *) src);
184       clib_mov16 ((u8 *) dst - 16 + n, (const u8 *) src - 16 + n);
185       return ret;
186     }
187   if (n <= 64)
188     {
189       clib_mov32 ((u8 *) dst, (const u8 *) src);
190       clib_mov32 ((u8 *) dst - 32 + n, (const u8 *) src - 32 + n);
191       return ret;
192     }
193   if (n <= 512)
194     {
195       if (n >= 256)
196         {
197           n -= 256;
198           clib_mov256 ((u8 *) dst, (const u8 *) src);
199           src = (const u8 *) src + 256;
200           dst = (u8 *) dst + 256;
201         }
202       if (n >= 128)
203         {
204           n -= 128;
205           clib_mov128 ((u8 *) dst, (const u8 *) src);
206           src = (const u8 *) src + 128;
207           dst = (u8 *) dst + 128;
208         }
209     COPY_BLOCK_128_BACK63:
210       if (n > 64)
211         {
212           clib_mov64 ((u8 *) dst, (const u8 *) src);
213           clib_mov64 ((u8 *) dst - 64 + n, (const u8 *) src - 64 + n);
214           return ret;
215         }
216       if (n > 0)
217         clib_mov64 ((u8 *) dst - 64 + n, (const u8 *) src - 64 + n);
218       return ret;
219     }
220
221         /**
222          * Make store aligned when copy size exceeds 512 bytes
223          */
224   dstofss = (uword) dst & 0x3F;
225   if (dstofss > 0)
226     {
227       dstofss = 64 - dstofss;
228       n -= dstofss;
229       clib_mov64 ((u8 *) dst, (const u8 *) src);
230       src = (const u8 *) src + dstofss;
231       dst = (u8 *) dst + dstofss;
232     }
233
234         /**
235          * Copy 512-byte blocks.
236          * Use copy block function for better instruction order control,
237          * which is important when load is unaligned.
238          */
239   clib_mov512blocks ((u8 *) dst, (const u8 *) src, n);
240   bits = n;
241   n = n & 511;
242   bits -= n;
243   src = (const u8 *) src + bits;
244   dst = (u8 *) dst + bits;
245
246         /**
247          * Copy 128-byte blocks.
248          * Use copy block function for better instruction order control,
249          * which is important when load is unaligned.
250          */
251   if (n >= 128)
252     {
253       clib_mov128blocks ((u8 *) dst, (const u8 *) src, n);
254       bits = n;
255       n = n & 127;
256       bits -= n;
257       src = (const u8 *) src + bits;
258       dst = (u8 *) dst + bits;
259     }
260
261         /**
262          * Copy whatever left
263          */
264   goto COPY_BLOCK_128_BACK63;
265 }
266
267
268 #endif /* included_clib_memcpy_avx512_h */
269
270
271 /*
272  * fd.io coding-style-patch-verification: ON
273  *
274  * Local Variables:
275  * eval: (c-set-style "gnu")
276  * End:
277  */