ARM64-memcpy.S 汇编源码分析

来源:互联网 发布:照片转换卡通软件 编辑:程序博客网 时间:2024/05/17 22:27

内存拷贝优先级:


数据块地址:




源码分析:
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright (C) 2013 ARM Ltd.
 * Copyright (C) 2013 Linaro.
 *
 * This code is based on glibc cortex strings work originally authored by Linaro
 * and re-licensed under GPLv2 for the Linux kernel. The original code can
 * be found @
 *
 * http://bazaar.launchpad.net/~linaro-toolchain-dev/cortex-strings/trunk/
 * files/head:/src/aarch64/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
#include <linux/linkage.h>
#include <asm/assembler.h>
#include <asm/cache.h>
 
/*
 * Copy a buffer from src to dest (alignment handled by the hardware)
 *
 * Parameters:
 * x0 - dest
 * x1 - src
 * x2 - n
 * Returns:
 * x0 - dest
 */
dstin   .req    x0
src .req    x1
count   .req    x2
tmp1    .req    x3
tmp1w   .req    w3
tmp2    .req    x4
tmp2w   .req    w4
tmp3    .req    x5
tmp3w   .req    w5
dst .req    x6
 
A_l .req    x7
A_h .req    x8
B_l .req    x9
B_h .req    x10
C_l .req    x11
C_h .req    x12
D_l .req    x13
D_h .req    x14
 
ENTRY(memcpy)          //memcpy 程序入口   
    mov dst, dstin     //X6指针指向返回地址X0
    cmp count, #16    
    b.lo    .Ltiny15   //判断数据块长度,若 count < 16,CMP发生借位,则跳转到.Ltiny15处 执行
 
    neg tmp2, src            //tmp2 = 0-src,这里为什么要这样做呢?这个地方思考讨论了很久,其实就是为了取初始非对齐内存块到16字节对齐之间的内存块长度,如上图“数据块地址”描述,注意二级制里面忽略进借位的话,16-7和0-7结果是同一个

    ands  tmp2, tmp2, #15    //#15==1111,取temp2的低4位值,

    b.eq    .LSrcAligned     //如果结果为0则表示数据块的首段memroy可以整除16,说明首地址是落在能被16整除的位置上,所以是16字节对齐的,则进入 .LSrcAligned处理
    sub count, count, tmp2   //否则说明首段地址非16字节对齐,需要剪掉低4bit尾巴单独处理
 
    tbz tmp2, #0, 1f         //以下这段代码依次取tmp2[3:0]的每一个bit做非0比较,判断找个“尾巴”值的范围,如果tmp2[0] ==1,则表明尾巴>=1字节,取出存入dst

    ldrb    tmp1w, [src], #1  
    strb    tmp1w, [dst], #1
1:
    tbz tmp2, #1, 2f          //如果tmp2[1] ==1 ==》“尾巴” >=2字节取出存入dst
    ldrh    tmp1w, [src], #2       
    strh    tmp1w, [dst], #2
2:
    tbz tmp2, #2, 3f          //如果tmp2[2] ==1 ==》“尾巴” >=4字节取出存入dst
    ldr tmp1w, [src], #4
    str tmp1w, [dst], #4
3:
    tbz tmp2, #3, .LSrcAligned  //如果tmp2[3] ==1 ==》“尾巴” >=8字节取出存入dst,否则跳转.LSrcAligned程序处理

    ldr tmp1, [src],#8
    str tmp1, [dst],#8
 
.LSrcAligned:
    cmp count, #64            //数据块长度如果>=64字节则进入.Lcpy_over64处理,否则进入.Ltail63处理

    b.ge    .Lcpy_over64
 
.Ltail63:
    ands    tmp1, count, #0x30      //0x30==0011 0000 == 48,取5、6bit的值,结果作为下面b.eq的跳转条件

    b.eq    .Ltiny15                //如果count <= 15,则进入.Ltiny15处理,否则向下走
    cmp tmp1w, #0x20           //0x20==0010 0000,这里做用于下面的b跳转判断,
    b.eq    1f                    //如果 ==32 字节,则跳到1:处程序,
    b.lt    2f                    //如果 <32 字节则跳到2:程序,如果 >32 字节则继续向下走

    ldp A_l, A_h, [src], #16      //取出16字节写到dst内存地址,scr指针地址加16字节
    stp A_l, A_h, [dst], #16
1:
    ldp A_l, A_h, [src], #16
    stp A_l, A_h, [dst], #16
2:
    ldp A_l, A_h, [src], #16
    stp A_l, A_h, [dst], #16
 
.Ltiny15:                  //此段程序处理count<=15 字节的数据
 
    tbz count, #3, 1f      //判断count[3]是否为0,如果不为0则表示count >=8 字节,继续执行,否则跳到1:执行

    ldr tmp1, [src], #8    //取出8个字节存到dst
    str tmp1, [dst], #8
1:
    tbz count, #2, 2f      //判断count[2]是否为0,如果不为0则表示count >=4 字节,继续执行,否则跳到2:执行

    ldr tmp1w, [src], #4
    str tmp1w, [dst], #4
2:
    tbz count, #1, 3f      //判断count[1]是否为0,如果不为0则表示count >=2 字节,继续执行,否则跳到3:执行

    ldrh    tmp1w, [src], #2
    strh    tmp1w, [dst], #2
3:
    tbz count, #0, .Lexitfunc   //判断count[0]是否为0,如果不为0则表示count ==1 字节,继续执行,否则进入.Lexitfunc

    ldrb    tmp1w, [src]
    strb    tmp1w, [dst]
 
.Lexitfunc:                     //数据copy完成,程序返回.
    ret
 
.Lcpy_over64:
    subs    count, count, #128  //count = count -128; 
    b.ge    .Lcpy_body_large    //如果数据块仍然>=128,则进入.Lcpy_body_large处理,如果<128就向下走,处理64~127之间长度,此处为什么要搞4对64bit寄存器呢?从实现角度讲,使用一对Xt1,Xt2重复写4次也可以实现,此出使用4对Xt的原因是减少指令之间的相关性,降低数据冲突,提高pipeline的并行效率,需要对CPU结构深入了解才能写出高效率代码.
 
    ldp A_l, A_h, [src],#16     //load 64整字节store到目标地址
    stp A_l, A_h, [dst],#16
    ldp B_l, B_h, [src],#16
    ldp C_l, C_h, [src],#16
    stp B_l, B_h, [dst],#16
    stp C_l, C_h, [dst],#16
    ldp D_l, D_h, [src],#16
    stp D_l, D_h, [dst],#16
 
    tst count, #0x3f            //0x3f == 0011 1111==63,
    b.ne    .Ltail63            //如果长度<=63,进入.Ltail63 处理,否则返回。
    ret
 
    .p2align  L1_CACHE_SHIFT    //开始新的cache,64字节,每行,这确保了整个循环是在一行,这个没看懂?

.Lcpy_body_large:                       
    ldp A_l, A_h, [src],#16  //长度>=128,先取出64字节放到tmp寄存器,准备store到dst地址
    ldp B_l, B_h, [src],#16
    ldp C_l, C_h, [src],#16
    ldp D_l, D_h, [src],#16
1:
    stp A_l, A_h, [dst],#16    //store上面取出的64字节数据,同时load新的64字节数据,每执行一条指令,dest指针+16长度

    ldp A_l, A_h, [src],#16
    stp B_l, B_h, [dst],#16
    ldp B_l, B_h, [src],#16
    stp C_l, C_h, [dst],#16
    ldp C_l, C_h, [src],#16
    stp D_l, D_h, [dst],#16
    ldp D_l, D_h, [src],#16
    subs    count, count, #64     //数据块长度更新:count = count - 64;
    b.ge    1b                    //如果count >=64,则跳到上面的1:处循环拷贝,直到count长度小于64字节

    stp A_l, A_h, [dst],#16
    stp B_l, B_h, [dst],#16
    stp C_l, C_h, [dst],#16
    stp D_l, D_h, [dst],#16
 
    tst count, #0x3f         //0x3f==0011 1111,若count<=63,则进入.Ltail63处理,否则拷贝完成返回.

    b.ne    .Ltail63
    ret
ENDPROC(memcpy)             //memcpy 程序结束

2 0
原创粉丝点击