about summary refs log tree commit diff
path: root/sysdeps/loongarch/lp64/multiarch/memset-aligned.S
blob: e1be66b7993b4e7fa34441f09e7cef6ba978d062 (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
/* Optimized memset aligned implementation using basic LoongArch instructions.
   Copyright (C) 2023-2024 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)
# define MEMSET_NAME __memset_aligned
#else
# define MEMSET_NAME memset
#endif

LEAF(MEMSET_NAME, 6)
    move        t0, a0
    andi        a3, a0, 0x7
    li.w        t6, 16
    beqz        a3, L(align)
    bltu        a2, t6, L(short_data)

L(make_align):
    li.w        t8, 8
    sub.d       t2, t8, a3
    pcaddi      t1, 11
    slli.d      t3, t2, 2
    sub.d       t1, t1, t3
    jr          t1

L(al7):
    st.b        a1, t0, 6
L(al6):
    st.b        a1, t0, 5
L(al5):
    st.b        a1, t0, 4
L(al4):
    st.b        a1, t0, 3
L(al3):
    st.b        a1, t0, 2
L(al2):
    st.b        a1, t0, 1
L(al1):
    st.b        a1, t0, 0
L(al0):
    add.d       t0, t0, t2
    sub.d       a2, a2, t2

L(align):
    bstrins.d   a1, a1, 15, 8
    bstrins.d   a1, a1, 31, 16
    bstrins.d   a1, a1, 63, 32
    bltu        a2, t6, L(less_16bytes)

    andi        a4, a2, 0x3f
    beq         a4, a2, L(less_64bytes)

    sub.d       t1, a2, a4
    move        a2, a4
    add.d       a5, t0, t1

L(loop_64bytes):
    addi.d      t0, t0, 64
    st.d        a1, t0, -64
    st.d        a1, t0, -56
    st.d        a1, t0, -48
    st.d        a1, t0, -40

    st.d        a1, t0, -32
    st.d        a1, t0, -24
    st.d        a1, t0, -16
    st.d        a1, t0, -8
    bne         t0, a5, L(loop_64bytes)

L(less_64bytes):
    srai.d      a4, a2, 5
    beqz        a4, L(less_32bytes)
    addi.d      a2, a2, -32
    st.d        a1, t0, 0

    st.d        a1, t0, 8
    st.d        a1, t0, 16
    st.d        a1, t0, 24
    addi.d      t0, t0, 32

L(less_32bytes):
    bltu        a2, t6, L(less_16bytes)
    addi.d      a2, a2, -16
    st.d        a1, t0, 0
    st.d        a1, t0, 8
    addi.d      t0, t0, 16

L(less_16bytes):
    srai.d      a4, a2, 3
    beqz        a4, L(less_8bytes)
    addi.d      a2, a2, -8
    st.d        a1, t0, 0
    addi.d      t0, t0, 8

L(less_8bytes):
    beqz        a2, L(less_1byte)
    srai.d      a4, a2, 2
    beqz        a4, L(less_4bytes)
    addi.d      a2, a2, -4
    st.w        a1, t0, 0
    addi.d      t0, t0, 4

L(less_4bytes):
    srai.d      a3, a2, 1
    beqz        a3, L(less_2bytes)
    addi.d      a2, a2, -2
    st.h        a1, t0, 0
    addi.d      t0, t0, 2

L(less_2bytes):
    beqz        a2, L(less_1byte)
    st.b        a1, t0, 0
L(less_1byte):
    jr          ra

L(short_data):
    pcaddi      t1, 19
    slli.d      t3, a2, 2
    sub.d       t1, t1, t3
    jr          t1
L(short_15):
    st.b        a1, a0, 14
L(short_14):
    st.b        a1, a0, 13
L(short_13):
    st.b        a1, a0, 12
L(short_12):
    st.b        a1, a0, 11
L(short_11):
    st.b        a1, a0, 10
L(short_10):
    st.b        a1, a0, 9
L(short_9):
    st.b        a1, a0, 8
L(short_8):
    st.b        a1, a0, 7
L(short_7):
    st.b        a1, a0, 6
L(short_6):
    st.b        a1, a0, 5
L(short_5):
    st.b        a1, a0, 4
L(short_4):
    st.b        a1, a0, 3
L(short_3):
    st.b        a1, a0, 2
L(short_2):
    st.b        a1, a0, 1
L(short_1):
    st.b        a1, a0, 0
L(short_0):
    jr          ra
END(MEMSET_NAME)

libc_hidden_builtin_def (MEMSET_NAME)