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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
19 * All rights reserved.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
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
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.
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.
48 #ifndef included_clib_memcpy_avx2_h
49 #define included_clib_memcpy_avx2_h
52 #include <x86intrin.h>
53 #include <vppinfra/warnings.h>
56 WARN_OFF (stringop-overflow)
60 clib_mov16 (u8 * dst, const u8 * src)
64 xmm0 = _mm_loadu_si128 ((const __m128i *) src);
65 _mm_storeu_si128 ((__m128i *) dst, xmm0);
69 clib_mov32 (u8 * dst, const u8 * src)
73 ymm0 = _mm256_loadu_si256 ((const __m256i *) src);
74 _mm256_storeu_si256 ((__m256i *) dst, ymm0);
78 clib_mov64 (u8 * dst, const u8 * src)
80 clib_mov32 ((u8 *) dst + 0 * 32, (const u8 *) src + 0 * 32);
81 clib_mov32 ((u8 *) dst + 1 * 32, (const u8 *) src + 1 * 32);
85 clib_mov128 (u8 * dst, const u8 * src)
87 clib_mov64 ((u8 *) dst + 0 * 64, (const u8 *) src + 0 * 64);
88 clib_mov64 ((u8 *) dst + 1 * 64, (const u8 *) src + 1 * 64);
92 clib_mov128blocks (u8 * dst, const u8 * src, size_t n)
94 __m256i ymm0, ymm1, ymm2, ymm3;
99 _mm256_loadu_si256 ((const __m256i *) ((const u8 *) src + 0 * 32));
102 _mm256_loadu_si256 ((const __m256i *) ((const u8 *) src + 1 * 32));
104 _mm256_loadu_si256 ((const __m256i *) ((const u8 *) src + 2 * 32));
106 _mm256_loadu_si256 ((const __m256i *) ((const u8 *) src + 3 * 32));
107 src = (const u8 *) src + 128;
108 _mm256_storeu_si256 ((__m256i *) ((u8 *) dst + 0 * 32), ymm0);
109 _mm256_storeu_si256 ((__m256i *) ((u8 *) dst + 1 * 32), ymm1);
110 _mm256_storeu_si256 ((__m256i *) ((u8 *) dst + 2 * 32), ymm2);
111 _mm256_storeu_si256 ((__m256i *) ((u8 *) dst + 3 * 32), ymm3);
112 dst = (u8 *) dst + 128;
117 clib_memcpy_fast (void *dst, const void *src, size_t n)
119 uword dstu = (uword) dst;
120 uword srcu = (uword) src;
126 * Copy less than 16 bytes
132 *(u8 *) dstu = *(const u8 *) srcu;
133 srcu = (uword) ((const u8 *) srcu + 1);
134 dstu = (uword) ((u8 *) dstu + 1);
138 *(u16 *) dstu = *(const u16 *) srcu;
139 srcu = (uword) ((const u16 *) srcu + 1);
140 dstu = (uword) ((u16 *) dstu + 1);
144 *(u32 *) dstu = *(const u32 *) srcu;
145 srcu = (uword) ((const u32 *) srcu + 1);
146 dstu = (uword) ((u32 *) dstu + 1);
150 *(u64 *) dstu = *(const u64 *) srcu;
156 * Fast way when copy size doesn't exceed 512 bytes
160 clib_mov16 ((u8 *) dst, (const u8 *) src);
161 clib_mov16 ((u8 *) dst - 16 + n, (const u8 *) src - 16 + n);
166 clib_mov16 ((u8 *) dst, (const u8 *) src);
167 clib_mov16 ((u8 *) dst + 16, (const u8 *) src + 16);
168 clib_mov16 ((u8 *) dst - 16 + n, (const u8 *) src - 16 + n);
173 clib_mov32 ((u8 *) dst, (const u8 *) src);
174 clib_mov32 ((u8 *) dst - 32 + n, (const u8 *) src - 32 + n);
182 clib_mov128 ((u8 *) dst, (const u8 *) src);
183 src = (const u8 *) src + 128;
184 dst = (u8 *) dst + 128;
186 COPY_BLOCK_128_BACK31:
190 clib_mov64 ((u8 *) dst, (const u8 *) src);
191 src = (const u8 *) src + 64;
192 dst = (u8 *) dst + 64;
196 clib_mov32 ((u8 *) dst, (const u8 *) src);
197 clib_mov32 ((u8 *) dst - 32 + n, (const u8 *) src - 32 + n);
202 clib_mov32 ((u8 *) dst - 32 + n, (const u8 *) src - 32 + n);
208 * Make store aligned when copy size exceeds 256 bytes
210 dstofss = (uword) dst & 0x1F;
213 dstofss = 32 - dstofss;
215 clib_mov32 ((u8 *) dst, (const u8 *) src);
216 src = (const u8 *) src + dstofss;
217 dst = (u8 *) dst + dstofss;
221 * Copy 128-byte blocks.
223 clib_mov128blocks ((u8 *) dst, (const u8 *) src, n);
227 src = (const u8 *) src + bits;
228 dst = (u8 *) dst + bits;
233 goto COPY_BLOCK_128_BACK31;
237 WARN_ON (stringop-overflow)
240 #endif /* included_clib_memcpy_avx2_h */
244 * fd.io coding-style-patch-verification: ON
247 * eval: (c-set-style "gnu")