about summary refs log tree commit diff
path: root/sysdeps/loongarch/lp64/multiarch/strcpy-unaligned.S
blob: 9e31883bb12932ab745f8310111a1e5a20accb25 (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
/* Optimized strcpy unaligned implementation using basic LoongArch
   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)

# ifndef STRCPY
#  define STRCPY __strcpy_unaligned
# endif

# ifdef USE_AS_STPCPY
#  define dstend a0
# else
#  define dstend a4
# endif

LEAF(STRCPY, 6)
    lu12i.w     t5, 0x01010
    li.w        t0, 0xff8
    ori         t5, t5, 0x101
    andi        t1, a1, 0xfff

    bstrins.d   t5, t5, 63, 32
    move        a2, a0
    slli.d      t6, t5, 7
    bltu        t0, t1, L(page_cross)

L(start_entry):
    ld.d        t0, a1, 0
    li.d        t3, 8
    andi        a3, a1, 0x7
    sub.d       t1, t0, t5

    andn        t2, t6, t0
    sub.d       t3, t3, a3
    and         t1, t1, t2
    bnez        t1, L(end)


    add.d       a1, a1, t3
    st.d        t0, a2, 0
    add.d       a2, a2, t3
    ld.d        t0, a1, 0

    sub.d       t1, t0, t5
    andn        t2, t6, t0
    and         t1, t1, t2
    bnez        t1, L(long_end)

L(loop):
    st.d        t0, a2, 0
    ld.d        t0, a1, 8
    addi.d      a2, a2, 8
    addi.d      a1, a1, 8

    sub.d       t1, t0, t5
    andn        t2, t6, t0
    and         t1, t1, t2
    beqz        t1, L(loop)


L(long_end):
    ctz.d       t1, t1
    srli.d      t1, t1, 3
    add.d       a1, a1, t1
    ld.d        t0, a1, -7

    add.d       dstend, a2, t1
    st.d        t0, dstend, -7
    jr          ra
    nop

L(end):
    ctz.d       t1, t1
    srli.d      t1, t1, 3
    add.d       a3, a1, t1
    add.d       dstend, a2, t1

L(less_8):
    li.d        t0, 3
    bltu        t1, t0, L(less_3)
    ld.w        t1, a1, 0
    ld.w        t2, a3, -3


    st.w        t1, a2, 0
    st.w        t2, dstend, -3
    jr          ra
L(less_3):
    beqz        t1, L(zero_bytes)

    ld.h        t1, a1, 0
    st.h        t1, a2, 0
L(zero_bytes):
    st.b        zero, dstend, 0
    jr          ra

L(page_cross):
    move        a4, a1
    bstrins.d   a4, zero, 2, 0
    ld.d        t0, a4, 0
    li.d        t3, -1

    slli.d      t4, a1, 3
    srl.d       t3, t3, t4
    srl.d       t0, t0, t4
    orn         t0, t0, t3


    sub.d       t1, t0, t5
    andn        t2, t6, t0
    and         t1, t1, t2
    beqz        t1, L(start_entry)

    b           L(end)
END(STRCPY)

libc_hidden_builtin_def (STRCPY)
#endif