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#include <picolibc.h>
13
14.section .text.memset
15.global memset
16.type	memset, @function
17memset:
18#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
19  mv t1, a0
20  beqz a2, 2f
21
221:
23  sb a1, 0(t1)
24  add   a2, a2, -1
25  add   t1, t1, 1
26  bnez a2, 1b
27
282:
29  ret
30
31#else
32  li t1, 15
33  move a4, a0
34  bleu a2, t1, .Ltiny
35  and a5, a4, 15
36  bnez a5, .Lmisaligned
37
38.Laligned:
39  bnez a1, .Lwordify
40
41.Lwordified:
42  and a3, a2, ~15
43  and a2, a2, 15
44  add a3, a3, a4
45
46#if __riscv_xlen == 64
471:sd a1, 0(a4)
48  sd a1, 8(a4)
49#else
501:sw a1, 0(a4)
51  sw a1, 4(a4)
52  sw a1, 8(a4)
53  sw a1, 12(a4)
54#endif
55  add a4, a4, 16
56  bltu a4, a3, 1b
57
58  bnez a2, .Ltiny
59  ret
60
61.Ltiny:
62  sub a3, t1, a2
63  sll a3, a3, 2
641:auipc t0, %pcrel_hi(.Ltable)
65  add a3, a3, t0
66.option push
67.option norvc
68.Ltable_misaligned:
69  jr a3, %pcrel_lo(1b)
70.Ltable:
71  sb a1,14(a4)
72  sb a1,13(a4)
73  sb a1,12(a4)
74  sb a1,11(a4)
75  sb a1,10(a4)
76  sb a1, 9(a4)
77  sb a1, 8(a4)
78  sb a1, 7(a4)
79  sb a1, 6(a4)
80  sb a1, 5(a4)
81  sb a1, 4(a4)
82  sb a1, 3(a4)
83  sb a1, 2(a4)
84  sb a1, 1(a4)
85  sb a1, 0(a4)
86.option pop
87  ret
88
89.Lwordify:
90  and a1, a1, 0xFF
91  sll a3, a1, 8
92  or  a1, a1, a3
93  sll a3, a1, 16
94  or  a1, a1, a3
95#if __riscv_xlen == 64
96  sll a3, a1, 32
97  or  a1, a1, a3
98#endif
99  j .Lwordified
100
101.Lmisaligned:
102  sll a3, a5, 2
1031:auipc t0, %pcrel_hi(.Ltable_misaligned)
104  add a3, a3, t0
105  mv t0, ra
106  jalr a3, %pcrel_lo(1b)
107  mv ra, t0
108
109  add a5, a5, -16
110  sub a4, a4, a5
111  add a2, a2, a5
112  bleu a2, t1, .Ltiny
113  j .Laligned
114#endif
115  .size	memset, .-memset
116