about summary refs log tree commit diff
path: root/sysdeps/loongarch/lp64/multiarch/memset-unaligned.S
blob: 12e63687a9e1332aec4b9cf3d124c9249b47c5c9 (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
/* Optimized memset unaligned 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_unaligned

#define ST_128(n)              \
    st.d        a1, a0, n;     \
    st.d        a1, a0, n+8  ; \
    st.d        a1, a0, n+16 ; \
    st.d        a1, a0, n+24 ; \
    st.d        a1, a0, n+32 ; \
    st.d        a1, a0, n+40 ; \
    st.d        a1, a0, n+48 ; \
    st.d        a1, a0, n+56 ; \
    st.d        a1, a0, n+64 ; \
    st.d        a1, a0, n+72 ; \
    st.d        a1, a0, n+80 ; \
    st.d        a1, a0, n+88 ; \
    st.d        a1, a0, n+96 ; \
    st.d        a1, a0, n+104; \
    st.d        a1, a0, n+112; \
    st.d        a1, a0, n+120;

LEAF(MEMSET_NAME, 6)
    bstrins.d   a1, a1, 15, 8
    add.d       t7, a0, a2
    bstrins.d   a1, a1, 31, 16
    move        t0, a0

    bstrins.d   a1, a1, 63, 32
    srai.d      t8, a2, 4
    beqz        t8, L(less_16bytes)
    srai.d      t8, a2, 6

    bnez        t8, L(more_64bytes)
    srai.d      t8, a2, 5
    beqz        t8, L(less_32bytes)

    st.d        a1, a0, 0
    st.d        a1, a0, 8
    st.d        a1, a0, 16
    st.d        a1, a0, 24

    st.d        a1, t7, -32
    st.d        a1, t7, -24
    st.d        a1, t7, -16
    st.d        a1, t7, -8

    jr          ra

L(less_32bytes):
    st.d        a1, a0, 0
    st.d        a1, a0, 8
    st.d        a1, t7, -16
    st.d        a1, t7, -8

    jr          ra

L(less_16bytes):
    srai.d      t8, a2, 3
    beqz        t8, L(less_8bytes)
    st.d        a1, a0, 0
    st.d        a1, t7, -8

    jr          ra

L(less_8bytes):
    srai.d      t8, a2, 2
    beqz        t8, L(less_4bytes)
    st.w        a1, a0, 0
    st.w        a1, t7, -4

    jr          ra

L(less_4bytes):
    srai.d      t8, a2, 1
    beqz        t8, L(less_2bytes)
    st.h        a1, a0, 0
    st.h        a1, t7, -2

    jr          ra

L(less_2bytes):
    beqz        a2, L(less_1bytes)
    st.b        a1, a0, 0

    jr          ra

L(less_1bytes):
    jr          ra

L(more_64bytes):
    srli.d      a0, a0, 3
    slli.d      a0, a0, 3
    addi.d      a0, a0, 0x8
    st.d        a1, t0, 0

    sub.d       t2, t0, a0
    add.d       a2, t2, a2
    addi.d      a2, a2, -0x80
    blt         a2, zero, L(end_unalign_proc)

L(loop_less):
    ST_128(0)
    addi.d      a0, a0,  0x80
    addi.d      a2, a2, -0x80
    bge         a2, zero, L(loop_less)

L(end_unalign_proc):
    addi.d      a2, a2, 0x80
    pcaddi      t1, 20
    andi        t5, a2, 0x78
    srli.d      t5, t5, 1

    sub.d       t1, t1, t5
    jr          t1

    st.d        a1, a0, 112
    st.d        a1, a0, 104
    st.d        a1, a0, 96
    st.d        a1, a0, 88
    st.d        a1, a0, 80
    st.d        a1, a0, 72
    st.d        a1, a0, 64
    st.d        a1, a0, 56
    st.d        a1, a0, 48
    st.d        a1, a0, 40
    st.d        a1, a0, 32
    st.d        a1, a0, 24
    st.d        a1, a0, 16
    st.d        a1, a0, 8
    st.d        a1, a0, 0
    st.d        a1, t7, -8

    move        a0, t0
    jr          ra
END(MEMSET_NAME)

libc_hidden_builtin_def (MEMSET_NAME)
#endif