about summary refs log tree commit diff
path: root/sysdeps/loongarch/lp64/multiarch/memmove-lsx.S
blob: 9e1502a79b150da6a4fff7eee803b91f8b2e2c59 (plain) (blame)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/* Optimized memmove implementation using Loongarch LSX instructions.
   Copyright (C) 2023 Free Software Foundation, Inc.

   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library.  If not, see
   <https://www.gnu.org/licenses/>.  */

#include <sysdep.h>
#include <sys/regdef.h>
#include <sys/asm.h>

#if IS_IN (libc) && !defined __loongarch_soft_float

# define MEMCPY_NAME __memcpy_lsx
# define MEMMOVE_NAME __memmove_lsx

LEAF(MEMCPY_NAME, 6)
    li.d            t6, 16
    add.d           a3, a0, a2
    add.d           a4, a1, a2
    bgeu            t6, a2, L(less_16bytes)

    li.d            t8, 64
    li.d            t7, 32
    bltu            t8, a2, L(copy_long)
    bltu            t7, a2, L(more_32bytes)

    vld             vr0, a1, 0
    vld             vr1, a4, -16
    vst             vr0, a0, 0
    vst             vr1, a3, -16

    jr              ra
L(more_32bytes):
    vld             vr0, a1, 0
    vld             vr1, a1, 16
    vld             vr2, a4, -32


    vld             vr3, a4, -16
    vst             vr0, a0, 0
    vst             vr1, a0, 16
    vst             vr2, a3, -32

    vst             vr3, a3, -16
    jr              ra
L(less_16bytes):
    srli.d          t0, a2, 3
    beqz            t0, L(less_8bytes)

    vldrepl.d       vr0, a1, 0
    vldrepl.d       vr1, a4, -8
    vstelm.d        vr0, a0, 0, 0
    vstelm.d        vr1, a3, -8, 0

    jr              ra
L(less_8bytes):
    srli.d          t0, a2, 2
    beqz            t0, L(less_4bytes)
    vldrepl.w       vr0, a1, 0


    vldrepl.w       vr1, a4, -4
    vstelm.w        vr0, a0, 0, 0
    vstelm.w        vr1, a3, -4, 0
    jr              ra

L(less_4bytes):
    srli.d          t0, a2, 1
    beqz            t0, L(less_2bytes)
    vldrepl.h       vr0, a1, 0
    vldrepl.h       vr1, a4, -2

    vstelm.h        vr0, a0, 0, 0
    vstelm.h        vr1, a3, -2, 0
    jr              ra
L(less_2bytes):
    beqz            a2, L(less_1bytes)

    ld.b            t0, a1, 0
    st.b            t0, a0, 0
L(less_1bytes):
    jr              ra
    nop
END(MEMCPY_NAME)

LEAF(MEMMOVE_NAME, 6)
    li.d            t6, 16
    add.d           a3, a0, a2
    add.d           a4, a1, a2
    bgeu            t6, a2, L(less_16bytes)

    li.d            t8, 64
    li.d            t7, 32
    bltu            t8, a2, L(move_long)
    bltu            t7, a2, L(more_32bytes)

    vld             vr0, a1, 0
    vld             vr1, a4, -16
    vst             vr0, a0, 0
    vst             vr1, a3, -16

    jr              ra
    nop
L(move_long):
    sub.d           t0, a0, a1
    bltu            t0, a2, L(copy_back)


L(copy_long):
    vld             vr2, a1, 0
    andi            t0, a0, 0xf
    sub.d           t0, t6, t0
    add.d           a1, a1, t0

    sub.d           a2, a2, t0
    andi            t1, a1, 0xf
    bnez            t1, L(unaligned)
    vld             vr0, a1, 0

    addi.d          a2, a2, -16
    vst             vr2, a0, 0
    andi            t2, a2, 0x7f
    add.d           a5, a0, t0

    beq             a2, t2, L(al_less_128)
    sub.d           t3, a2, t2
    move            a2, t2
    add.d           a6, a1, t3


L(al_loop):
    vld             vr1, a1, 16
    vld             vr2, a1, 32
    vld             vr3, a1, 48
    vld             vr4, a1, 64

    vld             vr5, a1, 80
    vld             vr6, a1, 96
    vld             vr7, a1, 112
    vst             vr0, a5, 0

    vld             vr0, a1, 128
    addi.d          a1, a1, 128
    vst             vr1, a5, 16
    vst             vr2, a5, 32

    vst             vr3, a5, 48
    vst             vr4, a5, 64
    vst             vr5, a5, 80
    vst             vr6, a5, 96


    vst             vr7, a5, 112
    addi.d          a5, a5, 128
    bne             a1, a6, L(al_loop)
L(al_less_128):
    blt             a2, t8, L(al_less_64)

    vld             vr1, a1, 16
    vld             vr2, a1, 32
    vld             vr3, a1, 48
    addi.d          a2, a2, -64

    vst             vr0, a5, 0
    vld             vr0, a1, 64
    addi.d          a1, a1, 64
    vst             vr1, a5, 16

    vst             vr2, a5, 32
    vst             vr3, a5, 48
    addi.d          a5, a5, 64
L(al_less_64):
    blt             a2, t7, L(al_less_32)


    vld             vr1, a1, 16
    addi.d          a2, a2, -32
    vst             vr0, a5, 0
    vld             vr0, a1, 32

    addi.d          a1, a1, 32
    vst             vr1, a5, 16
    addi.d          a5, a5, 32
L(al_less_32):
    blt             a2, t6, L(al_less_16)

    vst             vr0, a5, 0
    vld             vr0, a1, 16
    addi.d          a5, a5, 16
L(al_less_16):
    vld             vr1, a4, -16

    vst             vr0, a5, 0
    vst             vr1, a3, -16
    jr              ra
    nop


L(magic_num):
    .dword          0x0706050403020100
    .dword          0x0f0e0d0c0b0a0908
L(unaligned):
    pcaddi          t2, -4
    bstrins.d       a1, zero, 3, 0
    vld             vr8, t2, 0
    vld             vr0, a1, 0

    vld             vr1, a1, 16
    addi.d          a2, a2, -16
    vst             vr2, a0, 0
    add.d           a5, a0, t0

    vreplgr2vr.b    vr9, t1
    andi            t2, a2, 0x7f
    vadd.b          vr9, vr9, vr8
    addi.d          a1, a1, 32


    beq             t2, a2, L(un_less_128)
    sub.d           t3, a2, t2
    move            a2, t2
    add.d           a6, a1, t3

L(un_loop):
    vld             vr2, a1, 0
    vld             vr3, a1, 16
    vld             vr4, a1, 32
    vld             vr5, a1, 48

    vld             vr6, a1, 64
    vld             vr7, a1, 80
    vshuf.b         vr8, vr1, vr0, vr9
    vld             vr0, a1, 96

    vst             vr8, a5, 0
    vshuf.b         vr8, vr2, vr1, vr9
    vld             vr1, a1, 112
    vst             vr8, a5, 16


    addi.d          a1, a1, 128
    vshuf.b         vr2, vr3, vr2, vr9
    vshuf.b         vr3, vr4, vr3, vr9
    vst             vr2, a5, 32

    vshuf.b         vr4, vr5, vr4, vr9
    vst             vr3, a5, 48
    vshuf.b         vr5, vr6, vr5, vr9
    vst             vr4, a5, 64

    vshuf.b         vr6, vr7, vr6, vr9
    vst             vr5, a5, 80
    vshuf.b         vr7, vr0, vr7, vr9
    vst             vr6, a5, 96

    vst             vr7, a5, 112
    addi.d          a5, a5, 128
    bne             a1, a6, L(un_loop)
L(un_less_128):
    blt             a2, t8, L(un_less_64)


    vld             vr2, a1, 0
    vld             vr3, a1, 16
    vshuf.b         vr4, vr1, vr0, vr9
    vld             vr0, a1, 32

    vst             vr4, a5, 0
    addi.d          a2, a2, -64
    vshuf.b         vr4, vr2, vr1, vr9
    vld             vr1, a1, 48

    addi.d          a1, a1, 64
    vst             vr4, a5, 16
    vshuf.b         vr2, vr3, vr2, vr9
    vshuf.b         vr3, vr0, vr3, vr9

    vst             vr2, a5, 32
    vst             vr3, a5, 48
    addi.d          a5, a5, 64
L(un_less_64):
    blt             a2, t7, L(un_less_32)


    vshuf.b         vr3, vr1, vr0, vr9
    vld             vr0, a1, 0
    vst             vr3, a5, 0
    addi.d          a2, a2, -32

    vshuf.b         vr3, vr0, vr1, vr9
    vld             vr1, a1, 16
    addi.d          a1, a1, 32
    vst             vr3, a5, 16

    addi.d          a5, a5, 32
L(un_less_32):
    blt             a2, t6, L(un_less_16)
    vshuf.b         vr2, vr1, vr0, vr9
    vor.v           vr0, vr1, vr1

    vld             vr1, a1, 0
    vst             vr2, a5, 0
    addi.d          a5, a5, 16
L(un_less_16):
    vld             vr2, a4, -16


    vshuf.b         vr0, vr1, vr0, vr9
    vst             vr0, a5, 0
    vst             vr2, a3, -16
    jr              ra

L(copy_back):
    addi.d          t0, a3, -1
    vld             vr2, a4, -16
    andi            t0, t0, 0xf
    addi.d          t0, t0, 1

    sub.d           a4, a4, t0
    sub.d           a2, a2, t0
    andi            t1, a4, 0xf
    bnez            t1, L(back_unaligned)

    vld             vr0, a4, -16
    addi.d          a2, a2, -16
    vst             vr2, a3, -16
    andi            t2, a2, 0x7f


    sub.d           a3, a3, t0
    beq             t2, a2, L(back_al_less_128)
    sub.d           t3, a2, t2
    move            a2, t2

    sub.d           a6, a4, t3
L(back_al_loop):
    vld             vr1, a4, -32
    vld             vr2, a4, -48
    vld             vr3, a4, -64

    vld             vr4, a4, -80
    vld             vr5, a4, -96
    vld             vr6, a4, -112
    vld             vr7, a4, -128

    vst             vr0, a3, -16
    vld             vr0, a4, -144
    addi.d          a4, a4, -128
    vst             vr1, a3, -32


    vst             vr2, a3, -48
    vst             vr3, a3, -64
    vst             vr4, a3, -80
    vst             vr5, a3, -96

    vst             vr6, a3, -112
    vst             vr7, a3, -128
    addi.d          a3, a3, -128
    bne             a4, a6, L(back_al_loop)

L(back_al_less_128):
    blt             a2, t8, L(back_al_less_64)
    vld             vr1, a4, -32
    vld             vr2, a4, -48
    vld             vr3, a4, -64

    addi.d          a2, a2, -64
    vst             vr0, a3, -16
    vld             vr0, a4, -80
    addi.d          a4, a4, -64


    vst             vr1, a3, -32
    vst             vr2, a3, -48
    vst             vr3, a3, -64
    addi.d          a3, a3, -64

L(back_al_less_64):
    blt             a2, t7, L(back_al_less_32)
    vld             vr1, a4, -32
    addi.d          a2, a2, -32
    vst             vr0, a3, -16

    vld             vr0, a4, -48
    vst             vr1, a3, -32
    addi.d          a3, a3, -32
    addi.d          a4, a4, -32

L(back_al_less_32):
    blt             a2, t6, L(back_al_less_16)
    vst             vr0, a3, -16
    vld             vr0, a4, -32
    addi.d          a3, a3, -16


L(back_al_less_16):
    vld             vr1, a1, 0
    vst             vr0, a3, -16
    vst             vr1, a0, 0
    jr              ra

L(magic_num_2):
    .dword          0x0706050403020100
    .dword          0x0f0e0d0c0b0a0908
L(back_unaligned):
    pcaddi          t2, -4
    bstrins.d       a4, zero, 3, 0
    vld             vr8, t2, 0
    vld             vr0, a4, 0

    vld             vr1, a4, -16
    addi.d          a2, a2, -16
    vst             vr2, a3, -16
    sub.d           a3, a3, t0


    vreplgr2vr.b    vr9, t1
    andi            t2, a2, 0x7f
    vadd.b          vr9, vr9, vr8
    addi.d          a4, a4, -16

    beq             t2, a2, L(back_un_less_128)
    sub.d           t3, a2, t2
    move            a2, t2
    sub.d           a6, a4, t3

L(back_un_loop):
    vld             vr2, a4, -16
    vld             vr3, a4, -32
    vld             vr4, a4, -48

    vld             vr5, a4, -64
    vld             vr6, a4, -80
    vld             vr7, a4, -96
    vshuf.b         vr8, vr0, vr1, vr9


    vld             vr0, a4, -112
    vst             vr8, a3, -16
    vshuf.b         vr8, vr1, vr2, vr9
    vld             vr1, a4, -128

    vst             vr8, a3, -32
    addi.d          a4, a4, -128
    vshuf.b         vr2, vr2, vr3, vr9
    vshuf.b         vr3, vr3, vr4, vr9

    vst             vr2, a3, -48
    vshuf.b         vr4, vr4, vr5, vr9
    vst             vr3, a3, -64
    vshuf.b         vr5, vr5, vr6, vr9

    vst             vr4, a3, -80
    vshuf.b         vr6, vr6, vr7, vr9
    vst             vr5, a3, -96
    vshuf.b         vr7, vr7, vr0, vr9


    vst             vr6, a3, -112
    vst             vr7, a3, -128
    addi.d          a3, a3, -128
    bne             a4, a6, L(back_un_loop)

L(back_un_less_128):
    blt             a2, t8, L(back_un_less_64)
    vld             vr2, a4, -16
    vld             vr3, a4, -32
    vshuf.b         vr4, vr0, vr1, vr9

    vld             vr0, a4, -48
    vst             vr4, a3, -16
    addi.d          a2, a2, -64
    vshuf.b         vr4, vr1, vr2, vr9

    vld             vr1, a4, -64
    addi.d          a4, a4, -64
    vst             vr4, a3, -32
    vshuf.b         vr2, vr2, vr3, vr9


    vshuf.b         vr3, vr3, vr0, vr9
    vst             vr2, a3, -48
    vst             vr3, a3, -64
    addi.d          a3, a3, -64

L(back_un_less_64):
    blt             a2, t7, L(back_un_less_32)
    vshuf.b         vr3, vr0, vr1, vr9
    vld             vr0, a4, -16
    vst             vr3, a3, -16

    addi.d          a2, a2, -32
    vshuf.b         vr3, vr1, vr0, vr9
    vld             vr1, a4, -32
    addi.d          a4, a4, -32

    vst             vr3, a3, -32
    addi.d          a3, a3, -32
L(back_un_less_32):
    blt             a2, t6, L(back_un_less_16)
    vshuf.b         vr2, vr0, vr1, vr9


    vor.v           vr0, vr1, vr1
    vld             vr1, a4, -16
    vst             vr2, a3, -16
    addi.d          a3, a3, -16

L(back_un_less_16):
    vld             vr2, a1, 0
    vshuf.b         vr0, vr0, vr1, vr9
    vst             vr0, a3, -16
    vst             vr2, a0, 0

    jr              ra
END(MEMMOVE_NAME)

libc_hidden_builtin_def (MEMCPY_NAME)
libc_hidden_builtin_def (MEMMOVE_NAME)
#endif