1/* Copyright (c) 2017  SiFive Inc. All rights reserved.
2
3   This copyrighted material is made available to anyone wishing to use,
4   modify, copy, or redistribute it subject to the terms and conditions
5   of the FreeBSD License.   This program is distributed in the hope that
6   it will be useful, but WITHOUT ANY WARRANTY expressed or implied,
7   including the implied warranties of MERCHANTABILITY or FITNESS FOR
8   A PARTICULAR PURPOSE.  A copy of this license is available at
9   http://www.opensource.org/licenses.
10*/
11
12.section .text.memset
13.global memset
14.type	memset, @function
15memset:
16#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
17  mv t1, a0
18  beqz a2, 2f
19
201:
21  sb a1, 0(t1)
22  add   a2, a2, -1
23  add   t1, t1, 1
24  bnez a2, 1b
25
262:
27  ret
28
29#else
30  li t1, 15
31  move a4, a0
32  bleu a2, t1, .Ltiny
33  and a5, a4, 15
34  bnez a5, .Lmisaligned
35
36.Laligned:
37  bnez a1, .Lwordify
38
39.Lwordified:
40  and a3, a2, ~15
41  and a2, a2, 15
42  add a3, a3, a4
43
44#if __riscv_xlen == 64
451:sd a1, 0(a4)
46  sd a1, 8(a4)
47#else
481:sw a1, 0(a4)
49  sw a1, 4(a4)
50  sw a1, 8(a4)
51  sw a1, 12(a4)
52#endif
53  add a4, a4, 16
54  bltu a4, a3, 1b
55
56  bnez a2, .Ltiny
57  ret
58
59.Ltiny:
60  sub a3, t1, a2
61  sll a3, a3, 2
621:auipc t0, %pcrel_hi(.Ltable)
63  add a3, a3, t0
64.option push
65.option norvc
66.Ltable_misaligned:
67  jr a3, %pcrel_lo(1b)
68.Ltable:
69  sb a1,14(a4)
70  sb a1,13(a4)
71  sb a1,12(a4)
72  sb a1,11(a4)
73  sb a1,10(a4)
74  sb a1, 9(a4)
75  sb a1, 8(a4)
76  sb a1, 7(a4)
77  sb a1, 6(a4)
78  sb a1, 5(a4)
79  sb a1, 4(a4)
80  sb a1, 3(a4)
81  sb a1, 2(a4)
82  sb a1, 1(a4)
83  sb a1, 0(a4)
84.option pop
85  ret
86
87.Lwordify:
88  and a1, a1, 0xFF
89  sll a3, a1, 8
90  or  a1, a1, a3
91  sll a3, a1, 16
92  or  a1, a1, a3
93#if __riscv_xlen == 64
94  sll a3, a1, 32
95  or  a1, a1, a3
96#endif
97  j .Lwordified
98
99.Lmisaligned:
100  sll a3, a5, 2
1011:auipc t0, %pcrel_hi(.Ltable_misaligned)
102  add a3, a3, t0
103  mv t0, ra
104  jalr a3, %pcrel_lo(1b)
105  mv ra, t0
106
107  add a5, a5, -16
108  sub a4, a4, a5
109  add a2, a2, a5
110  bleu a2, t1, .Ltiny
111  j .Laligned
112#endif
113  .size	memset, .-memset
114