DPDK 24.11中的rte_memcpy函数解析

DPDK中,rte_memcpy 函数是一个非常典型的 DPDK 利用其SIMD指令,对常见功能进行优化的一个示例。

x86版本的实现在 dpdk-src/lib/librte_eal/x86/include/rte_memcpy.h 文件中实现。

# 1、rte_memcpy()

1
2
3
4
5
6
7
8
static __rte_always_inline void *rte_memcpy(void *dst, const void *src, size_t n)              
{
    /* 如果源地址与目的地址都是地址对齐的,则使用 rte_memcpy_aligned() 函数 */
    if (!(((uintptr_t)dst | (uintptr_t)src) & ALIGNMENT_MASK))
        return rte_memcpy_aligned(dst, src, n);               
    else
        return rte_memcpy_generic(dst, src, n);               
} 

ALIGNMENT_MASK 宏定义的值,根据CPU的不同而不同。 对于支持到 AVX512 指令的CPU,ALIGNMENT_MASK 的值定义为 0x3F,即64字节对齐。 对于支持到 AVX2 指令的CPU,ALIGNMENT_MASK 的值定义为 0x1F,即32字节对齐。 其余的所有CPU,ALIGNMENT_MASK 的值定义为 0x0F,即16字节对齐。

# 2、rte_mov16()rte_mov32()等函数

由于 Intel CPU 对 SSE、AVX等 SIMD指令的支持,使每次最多处理的数据超过64字节成为可能。 以目前最常见的支持 AVX2 指令较多的CPU为例,该指令集最高支持256位的宽指令。

# 2.1 rte_mov16()

该函数用来拷贝16字节(128位)数据,且数据的源空间与目的空间不能有重叠。

1
2
3
4
5
6
7
static __rte_always_inline void rte_mov16(uint8_t *dst, const uint8_t *src)
{ 
    __m128i xmm0; 

    xmm0 = _mm_loadu_si128((const __m128i *)src); /* 从源空间加载128位到寄存器 */
    _mm_storeu_si128((__m128i *)dst, xmm0);  /* 将128位数据保存到目的内存空间 */
}

# 2.2 rte_mov32()

该函数用来拷贝32字节(256位)数据,且数据的源空间与目的空间不能有重叠。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
static __rte_always_inline void rte_mov32(uint8_t *dst, const uint8_t *src)
{
#if defined RTE_MEMCPY_AVX
    __m256i ymm0;
    
    ymm0 = _mm256_loadu_si256((const __m256i *)src); /* 从源空间加载256位到寄存器 */
    _mm256_storeu_si256((__m256i *)dst, ymm0); /* 将256位数据保存到目的内存空间 */
#else /* SSE implementation */                                        
    rte_mov16((uint8_t *)dst + 0 * 16, (const uint8_t *)src + 0 * 16);
    rte_mov16((uint8_t *)dst + 1 * 16, (const uint8_t *)src + 1 * 16);
#endif                                                                
}

# 2.3 rte_mov64()

该函数用来拷贝64字节(512位)数据,且数据的源空间与目的空间不能有重叠。 如果支持到 avx2 指令集的CPU 不支持 512 位宽的指令,则该函数会将被拷贝数据拆分成两个256位的数据进行拷贝。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
static __rte_always_inline void rte_mov64(uint8_t *dst, const uint8_t *src) 
{
#if defined __AVX512F__ && defined RTE_MEMCPY_AVX512
    __m512i zmm0;

    zmm0 = _mm512_loadu_si512((const void *)src);
    _mm512_storeu_si512((void *)dst, zmm0);
#else /* AVX2, AVX & SSE implementation */
    rte_mov32((uint8_t *)dst + 0 * 32, (const uint8_t *)src + 0 * 32);
    rte_mov32((uint8_t *)dst + 1 * 32, (const uint8_t *)src + 1 * 32);
#endif
} 

# 2.4 rte_mov128()

该函数的实现与 rte_mov64() 类似,只是将数据分成2部分512位的数据进行拷贝。 数据的源空间与目的空间不能有重叠。

1
2
3
4
5
static __rte_always_inline void rte_mov128(uint8_t *dst, const uint8_t *src)                          
{
    rte_mov64(dst + 0 * 64, src + 0 * 64);
    rte_mov64(dst + 1 * 64, src + 1 * 64);
}

# 2.5 rte_mov256()

该函数的实现与 rte_mov128() 类似,只是将数据分成2部分128位的数据进行拷贝。 数据的源空间与目的空间不能有重叠。

1
2
3
4
5
static __rte_always_inline void rte_mov256(uint8_t *dst, const uint8_t *src) 
{
    rte_mov128(dst + 0 * 128, src + 0 * 128);
    rte_mov128(dst + 1 * 128, src + 1 * 128);
}

# 2.6 rte_mov128blocks()

该函数用来拷贝128字节块(1024位)数据,且数据的源空间与目的空间不能有重叠。

被拷贝的字节块不能小于128字节,否则直接退出。

如果定义了__AVX512F__RTE_MEMCPY_AVX512,则实现如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
static __rte_always_inline void rte_mov128blocks(uint8_t *dst, const uint8_t *src, size_t n) 
{
    __m512i zmm0, zmm1;

    while (n >= 128) {
        zmm0 = _mm512_loadu_si512((const void *)(src + 0 * 64));
        n -= 128;
        zmm1 = _mm512_loadu_si512((const void *)(src + 1 * 64));
        src = src + 128;
        _mm512_storeu_si512((void *)(dst + 0 * 64), zmm0);
        _mm512_storeu_si512((void *)(dst + 1 * 64), zmm1);
        dst = dst + 128;
    }
}

如果定义了RTE_MEMCPY_AVX,则实现如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
static __rte_always_inline void                                  
rte_mov128blocks(uint8_t *dst, const uint8_t *src, size_t n)     
{
    __m256i ymm0, ymm1, ymm2, ymm3;

    while (n >= 128) {
        ymm0 = _mm256_loadu_si256((const __m256i *)(const void *)
                      ((const uint8_t *)src + 0 * 32));
        n -= 128;
        ymm1 = _mm256_loadu_si256((const __m256i *)(const void *)
                      ((const uint8_t *)src + 1 * 32));          
        ymm2 = _mm256_loadu_si256((const __m256i *)(const void *)
                      ((const uint8_t *)src + 2 * 32));          
        ymm3 = _mm256_loadu_si256((const __m256i *)(const void *)
                      ((const uint8_t *)src + 3 * 32));
        src = (const uint8_t *)src + 128;
        _mm256_storeu_si256((__m256i *)(void *)
                    ((uint8_t *)dst + 0 * 32), ymm0);
        _mm256_storeu_si256((__m256i *)(void *)
                    ((uint8_t *)dst + 1 * 32), ymm1);
        _mm256_storeu_si256((__m256i *)(void *)
                    ((uint8_t *)dst + 2 * 32), ymm2);
        _mm256_storeu_si256((__m256i *)(void *)
                    ((uint8_t *)dst + 3 * 32), ymm3);
        dst = (uint8_t *)dst + 128;
    }
}

# 2.7 rte_mov512blocks()

该函数循环拷贝512字节块(4096位)数据,直到剩余的数据不足512字节,且数据的源空间与目的空间不能有重叠。

只有在定义了__AVX512F__RTE_MEMCPY_AVX512时才定义该函数,实现如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
static inline void
rte_mov512blocks(uint8_t *dst, const uint8_t *src, size_t n)
{
    __m512i zmm0, zmm1, zmm2, zmm3, zmm4, zmm5, zmm6, zmm7;

    while (n >= 512) {
        zmm0 = _mm512_loadu_si512((const void *)(src + 0 * 64));
        n -= 512;
        zmm1 = _mm512_loadu_si512((const void *)(src + 1 * 64));
        zmm2 = _mm512_loadu_si512((const void *)(src + 2 * 64));
        zmm3 = _mm512_loadu_si512((const void *)(src + 3 * 64));
        zmm4 = _mm512_loadu_si512((const void *)(src + 4 * 64));
        zmm5 = _mm512_loadu_si512((const void *)(src + 5 * 64));
        zmm6 = _mm512_loadu_si512((const void *)(src + 6 * 64));
        zmm7 = _mm512_loadu_si512((const void *)(src + 7 * 64));
        src = src + 512;
        _mm512_storeu_si512((void *)(dst + 0 * 64), zmm0);
        _mm512_storeu_si512((void *)(dst + 1 * 64), zmm1);
        _mm512_storeu_si512((void *)(dst + 2 * 64), zmm2);
        _mm512_storeu_si512((void *)(dst + 3 * 64), zmm3);
        _mm512_storeu_si512((void *)(dst + 4 * 64), zmm4);
        _mm512_storeu_si512((void *)(dst + 5 * 64), zmm5);
        _mm512_storeu_si512((void *)(dst + 6 * 64), zmm6);
        _mm512_storeu_si512((void *)(dst + 7 * 64), zmm7);
        dst = dst + 512;
    }
}

# 2.8 rte_mov15_or_less()

该函数定义了当要拷贝的数据不大于15字节时,该如何拷贝,且源地址与目的地址不能重叠,实现如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
static __rte_always_inline void *
rte_mov15_or_less(void *dst, const void *src, size_t n)
{
    /**
     * Use the following structs to avoid violating C standard 
     * alignment requirements and to avoid strict aliasing bugs
     * 使用以下结构体以避免违反C标准对齐要求,并避免严格的别名相关bugs
     */
    struct rte_uint64_alias {
        uint64_t val;
    } __rte_packed __rte_may_alias;
    struct rte_uint32_alias {
        uint32_t val;
    } __rte_packed __rte_may_alias;
    struct rte_uint16_alias {
        uint16_t val;
    } __rte_packed __rte_may_alias;

    void *ret = dst;

    if (n & 8) { // 拷贝高8字节(如果存在)
        ((struct rte_uint64_alias *)dst)->val =
            ((const struct rte_uint64_alias *)src)->val;
        src = (const uint64_t *)src + 1;
        dst = (uint64_t *)dst + 1;
    }
    if (n & 4) { // 拷贝之后的4字节(如果存在)
        ((struct rte_uint32_alias *)dst)->val =
            ((const struct rte_uint32_alias *)src)->val;
        src = (const uint32_t *)src + 1;
        dst = (uint32_t *)dst + 1;
    }
    if (n & 2) { // 拷贝之后的2字节(如果存在)
        ((struct rte_uint16_alias *)dst)->val =
            ((const struct rte_uint16_alias *)src)->val;
        src = (const uint16_t *)src + 1;
        dst = (uint16_t *)dst + 1;
    }
    if (n & 1) // 拷贝最后的1字节(如果存在)
        *(uint8_t *)dst = *(const uint8_t *)src;
    return ret;
}

如果要拷贝的数据小于16字节,则采用分段拷贝的方式,将数据最多分成4段,再对每一段进行拷贝,即最多4次即可完成拷贝。

# 3、rte_memcpy_aligned()

如果要拷贝的源地址和目的地址都是对 ALIGNMENT_MASK 对齐的,则会调用该函数。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
static __rte_always_inline void *rte_memcpy_aligned(void *dst, const void *src, size_t n)
{
    void *ret = dst; 

    /* 如果要拷贝的数据小于16字节,会对数据进行分段,再对每一段进行拷贝 */ 
    /* 最多分成了 4 段,即最多 4 次即可拷贝完成 */
    if (n < 16) {
        return rte_mov15_or_less(dst, src, n);
    }

    /* 如果要拷贝的数据在编译时就可以确定是32字节,则直接调用rte_mov32()进行拷贝 */
    if (__rte_constant(n) && n == 32) {
        rte_mov32((uint8_t *)dst, (const uint8_t *)src);
        return ret;
    }
    /* 如果要拷贝的数据 大于等于 16字节,且不大于32字节,则将数据分成两段,
     * 前16字节(即128位)是一段,用一个 128 位宽的 SIMD指定完成;
     * 剩余的数据长度是一段,也用一个 128 位宽的 SIMD指定完成;
     */
    if (n <= 32) {
        rte_mov16((uint8_t *)dst, (const uint8_t *)src);
        if (__rte_constant(n) && n == 16)
            return ret; /* 添加判断,避免(无害的)重复拷贝 */
        /* 确保拷贝的数据为16字节,避免越界访问 */
        rte_mov16((uint8_t *)dst - 16 + n,
                (const uint8_t *)src - 16 + n);

        return ret;
    }

    /* 如果要拷贝的数据在编译时就可以确定是64字节,则直接调用rte_mov64()进行拷贝 */
    if (__rte_constant(n) && n == 64) {
        rte_mov64((uint8_t *)dst, (const uint8_t *)src);
        return ret;
    }

    /* 如果要拷贝的数据大于 32 字节,不超过 64 字节,将数据分成 2 部分,
     * 前 32 字节(即256位)是一段,用一个 256 位宽的 SIMD 指令完成;
     * 剩余的数据长度是一段,也用一个 256 位宽的 SIMD 指令完成 */                    
    if (n <= 64) {
        rte_mov32((uint8_t *)dst, (const uint8_t *)src);
        // 确保拷贝的数据是32字节,避免越界访问
        rte_mov32((uint8_t *)dst - 32 + n, (const uint8_t *)src - 32 + n); 
        return ret;  
    } 

    /* 如果要拷贝的数据不小于 64 字节,则将数据拆分成 64 字节(256位)为一段进行拷贝 */
    for (; n >= 64; n -= 64) {
        rte_mov64((uint8_t *)dst, (const uint8_t *)src);
        dst = (uint8_t *)dst + 64; 
        src = (const uint8_t *)src + 64;  
    }

    /* 最后不管剩余多少(少于64字节),都用256位宽的 SIMD 指令完成 */  
    rte_mov64((uint8_t *)dst - 64 + n,  (const uint8_t *)src - 64 + n);  

    return ret; 
} 

# 4、rte_memcpy_generic()

如果要拷贝的源地址或目的地址没有对ALIGNMENT_MASK对齐,则会调用该函数进行数据拷贝。

根据CPU指令集宽度不同,该函数有多种实现方式。

# 4.1 如果定义了__AVX512F__RTE_MEMCPY_AVX512

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
static __rte_always_inline void *
rte_memcpy_generic(void *dst, const void *src, size_t n)
{
    void *ret = dst;
    size_t dstofss;
    size_t bits;

    /**
     * 如果要拷贝的数据小于16字节,则直接调用rte_mov15_or_less()函数
     */
    if (n < 16) {
        return rte_mov15_or_less(dst, src, n);
    }

    /**
     * Fast way when copy size doesn't exceed 512 bytes
     */
    if (__rte_constant(n) && n == 32) {
        rte_mov32((uint8_t *)dst, (const uint8_t *)src);
        return ret;
    }
    if (n <= 32) {
        rte_mov16((uint8_t *)dst, (const uint8_t *)src);
        if (__rte_constant(n) && n == 16)
            return ret; /* avoid (harmless) duplicate copy */
        rte_mov16((uint8_t *)dst - 16 + n,
                  (const uint8_t *)src - 16 + n);
        return ret;
    }
    if (__rte_constant(n) && n == 64) {
        rte_mov64((uint8_t *)dst, (const uint8_t *)src);
        return ret;
    }
    if (n <= 64) {
        rte_mov32((uint8_t *)dst, (const uint8_t *)src);
        rte_mov32((uint8_t *)dst - 32 + n,
                  (const uint8_t *)src - 32 + n);
        return ret;
    if (n <= 512) {
        if (n >= 256) {
            n -= 256;
            rte_mov256((uint8_t *)dst, (const uint8_t *)src);
            src = (const uint8_t *)src + 256;
            dst = (uint8_t *)dst + 256;
        }
        if (n >= 128) {
            n -= 128;
            rte_mov128((uint8_t *)dst, (const uint8_t *)src);
            src = (const uint8_t *)src + 128;
            dst = (uint8_t *)dst + 128;
        }
COPY_BLOCK_128_BACK63:
        if (n > 64) {
            rte_mov64((uint8_t *)dst, (const uint8_t *)src); 
            rte_mov64((uint8_t *)dst - 64 + n,
                      (const uint8_t *)src - 64 + n);
            return ret;
        }
        if (n > 0)
            rte_mov64((uint8_t *)dst - 64 + n,
                      (const uint8_t *)src - 64 + n);
        return ret;
    }

    /**
     * Make store aligned when copy size exceeds 512 bytes
     * 如果要拷贝的数据超过512字节,先使存储对齐,将未对齐的部分先拷贝过去
     */
    dstofss = ((uintptr_t)dst & 0x3F);
    if (dstofss > 0) {
        dstofss = 64 - dstofss;
        n -= dstofss;
        rte_mov64((uint8_t *)dst, (const uint8_t *)src);
        src = (const uint8_t *)src + dstofss;
        dst = (uint8_t *)dst + dstofss;
    }

    /**
     * Copy 512-byte blocks.
     * Use copy block function for better instruction order control,
     * which is important when load is unaligned.
     * 循环以512字节为分段进行拷贝,直到剩余的数据不足512字节。
     */
    rte_mov512blocks((uint8_t *)dst, (const uint8_t *)src, n);      
    bits = n;
    n = n & 511;
    bits -= n;
    src = (const uint8_t *)src + bits;
    dst = (uint8_t *)dst + bits;

    /**
     * Copy 128-byte blocks.
     * Use copy block function for better instruction order control,
     * which is important when load is unaligned.
     * 对于小于512字节的部分,以128字节为分块进行拷贝,直到剩下的数据不足128字节
     */
    if (n >= 128) {
        rte_mov128blocks((uint8_t *)dst, (const uint8_t *)src, n);  
        bits = n;
        n = n & 127;
        bits -= n;
        src = (const uint8_t *)src + bits;
        dst = (uint8_t *)dst + bits;
    }

    /**
     * Copy whatever left
     * 拷贝剩余的不足128字节的数据
     */
    goto COPY_BLOCK_128_BACK63;
}

# 4.2 如果定义了RTE_MEMCPY_AVX

如果没有定义__AVX512F__RTE_MEMCPY_AVX512,但定义了RTE_MEMCPY_AVX,则实现方式如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
static __rte_always_inline void *
rte_memcpy_generic(void *dst, const void *src, size_t n)     
{
    void *ret = dst;
    size_t dstofss;
    size_t bits;

    /**                                                      
     * Copy less than 16 bytes                               
     */                                                      
    if (n < 16) {                                            
        return rte_mov15_or_less(dst, src, n);               
    }                                                        
                                                             
    /**                                                      
     * Fast way when copy size doesn't exceed 256 bytes      
     */                                                      
    if (__rte_constant(n) && n == 32) {                      
        rte_mov32((uint8_t *)dst, (const uint8_t *)src);     
        return ret;                                          
    }                                                        
    if (n <= 32) {                                           
        rte_mov16((uint8_t *)dst, (const uint8_t *)src);     
        if (__rte_constant(n) && n == 16)                    
            return ret; /* avoid (harmless) duplicate copy */
        rte_mov16((uint8_t *)dst - 16 + n,                   
                (const uint8_t *)src - 16 + n);              
        return ret;                                          
    }                                                        
    if (n <= 64) {                                           
        rte_mov32((uint8_t *)dst, (const uint8_t *)src);     
        rte_mov32((uint8_t *)dst - 32 + n,                   
                (const uint8_t *)src - 32 + n);              
        return ret;                                          
    }
    if (n <= 256) {                                          
        if (n >= 128) {                                      
            n -= 128;                                        
            rte_mov128((uint8_t *)dst, (const uint8_t *)src);
            src = (const uint8_t *)src + 128;                
            dst = (uint8_t *)dst + 128;                      
        }                                                    
COPY_BLOCK_128_BACK31:                                       
        if (n >= 64) {                                       
            n -= 64;                                         
            rte_mov64((uint8_t *)dst, (const uint8_t *)src); 
            src = (const uint8_t *)src + 64;                 
            dst = (uint8_t *)dst + 64;                       
        }                                                    
        if (n > 32) {                                        
            rte_mov32((uint8_t *)dst, (const uint8_t *)src); 
            rte_mov32((uint8_t *)dst - 32 + n,               
                    (const uint8_t *)src - 32 + n);          
            return ret;                                      
        }                                                    
        if (n > 0) {                                         
            rte_mov32((uint8_t *)dst - 32 + n,               
                    (const uint8_t *)src - 32 + n);          
        }                                                    
        return ret;                                          
    }                                                        
                                                             
    /**                                                      
     * Make store aligned when copy size exceeds 256 bytes   
     */
    dstofss = (uintptr_t)dst & 0x1F;                          
    if (dstofss > 0) {
        dstofss = 32 - dstofss;                               
        n -= dstofss;                                         
        rte_mov32((uint8_t *)dst, (const uint8_t *)src);      
        src = (const uint8_t *)src + dstofss;                 
        dst = (uint8_t *)dst + dstofss;                       
    }                                                         
                                                              
    /**                                                       
     * Copy 128-byte blocks                                   
     */                                                       
    rte_mov128blocks((uint8_t *)dst, (const uint8_t *)src, n);
    bits = n;                                                 
    n = n & 127;                                              
    bits -= n;                                                
    src = (const uint8_t *)src + bits;                        
    dst = (uint8_t *)dst + bits;                              
                                                              
    /**                                                       
     * Copy whatever left                                     
     */                                                       
    goto COPY_BLOCK_128_BACK31;                               
}

该函数与使用rte_mov512blocks()函数的版本类似,只不过循环拷贝以128字节为分块,效率理论上相对差一些。

# 4.3 常规定义

如果以上几个宏都没有定义,则定义了rte_memcpy_generic()函数实现的最通用版本,实现如下:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
static __rte_always_inline void *
rte_memcpy_generic(void *dst, const void *src, size_t n)
{
    __m128i xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8;
    void *ret = dst;
    size_t dstofss;
    size_t srcofs;

    /**
     * Copy less than 16 bytes
     */
    if (n < 16) {
        return rte_mov15_or_less(dst, src, n);
    }

    /**
     * Fast way when copy size doesn't exceed 512 bytes
     */
    if (n <= 32) {
        rte_mov16((uint8_t *)dst, (const uint8_t *)src);
        if (__rte_constant(n) && n == 16)
            return ret; /* avoid (harmless) duplicate copy */
        rte_mov16((uint8_t *)dst - 16 + n, (const uint8_t *)src - 16 + n);
        return ret;
    }
    if (n <= 64) {
        rte_mov32((uint8_t *)dst, (const uint8_t *)src);
        if (n > 48)
            rte_mov16((uint8_t *)dst + 32, (const uint8_t *)src + 32);
        rte_mov16((uint8_t *)dst - 16 + n, (const uint8_t *)src - 16 + n);
        return ret;
    }
    if (n <= 128) {
        goto COPY_BLOCK_128_BACK15;
    }
    // 如果要拷贝的数据大于128字节且不大于512字节,则按256、128、64等分段拷贝
    if (n <= 512) { 
        if (n >= 256) {
            n -= 256;
            rte_mov128((uint8_t *)dst, (const uint8_t *)src);
            rte_mov128((uint8_t *)dst + 128, (const uint8_t *)src + 128);
            src = (const uint8_t *)src + 256;
            dst = (uint8_t *)dst + 256;
        }
COPY_BLOCK_255_BACK15:
        if (n >= 128) {
            n -= 128;
            rte_mov128((uint8_t *)dst, (const uint8_t *)src);
            src = (const uint8_t *)src + 128;
            dst = (uint8_t *)dst + 128;
        }
COPY_BLOCK_128_BACK15:
        if (n >= 64) {
            n -= 64;
            rte_mov64((uint8_t *)dst, (const uint8_t *)src);
            src = (const uint8_t *)src + 64;
            dst = (uint8_t *)dst + 64;
        }
COPY_BLOCK_64_BACK15:
        if (n >= 32) {
            n -= 32;
            rte_mov32((uint8_t *)dst, (const uint8_t *)src);
            src = (const uint8_t *)src + 32;
            dst = (uint8_t *)dst + 32;
        }
        if (n > 16) {
            rte_mov16((uint8_t *)dst, (const uint8_t *)src);
            rte_mov16((uint8_t *)dst - 16 + n, (const uint8_t *)src - 16 + n);
            return ret;
        }
        if (n > 0) {
            rte_mov16((uint8_t *)dst - 16 + n, (const uint8_t *)src - 16 + n);
        }
        return ret;
    }

    /**
     * Make store aligned when copy size exceeds 512 bytes,  
     * and make sure the first 15 bytes are copied, because  
     * unaligned copy functions require up to 15 bytes       
     * backwards access.
     * 如果要拷贝的数据超过512字节,则先使存储地址对齐
     */
    dstofss = (uintptr_t)dst & 0x0F;
    if (dstofss > 0) {
        dstofss = 16 - dstofss + 16;
        n -= dstofss;
        rte_mov32((uint8_t *)dst, (const uint8_t *)src);
        src = (const uint8_t *)src + dstofss;
        dst = (uint8_t *)dst + dstofss;
    }
    srcofs = ((uintptr_t)src & 0x0F);

    /**
     * For aligned copy
     */
    if (srcofs == 0) {
        /**
         * Copy 256-byte blocks
         */
        for (; n >= 256; n -= 256) {
            rte_mov256((uint8_t *)dst, (const uint8_t *)src);
            dst = (uint8_t *)dst + 256;
            src = (const uint8_t *)src + 256;
        }

        /**
         * Copy whatever left
         * 拷贝剩余的数据
         */
        goto COPY_BLOCK_255_BACK15;
    }

    /**
     * For copy with unaligned load
     * 如果还是没对齐,则使用定义的宏以128bit为一段进行拷贝
     */
    MOVEUNALIGNED_LEFT47(dst, src, n, srcofs);

    /**
     * Copy whatever left
     * 拷贝剩余数据,最多15字节
     */
    goto COPY_BLOCK_64_BACK15;
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2025-01-08 21:21 CST
网站已稳定运行 小时 分钟
使用 Hugo 构建
主题 StackJimmy 设计