-
Notifications
You must be signed in to change notification settings - Fork 0
/
memset8.S
646 lines (531 loc) · 15.3 KB
/
memset8.S
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/* The MIT License (MIT)
*
* Copyright (c) 2013-2015 Norbert Lange
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "acle-compat.h"
#include "memsetpriv.h"
#if !defined(_MEMSET_THUMB) || defined(__thumb2__) /* || defined(__ARM_ARCH_ISA_ARM) */
#if defined(__WCHAR_MAX__) && __WCHAR_MAX__ <= 255
FUNCTION wmemset
#endif
FUNCTION _memset8
/* fixup registers to look like the many __eabi calls */
lsls r3, r1, #24
movs r1, r2
cmp r1, #3
bhi .Lmemset32sharedPre
it ls /* so we dont mess up the flags in thumb */
lsrls r2, r3, #24
.Lsupershort8eabi:
/* we can only end up here if len <=3 */
it eq /* if len == 3 */
strbeq r2, [r0, #2]
cmp r1, #1
it hs /* if len >= 1 */
strbhs r2, [r0, #0]
it hi /* if len > 1 */
strbhi r2, [r0, #1]
RETURN
FUNCTION __aeabi_memclr
movs r2, #0
FUNCTION __aeabi_memset
FUNCTION __aeabi_memset4
FUNCTION __aeabi_memset8
lsls r3, r2, #24
cmp r1, #3
bls .Lsupershort8eabi
/* r0 - ptr, r1: size, r3: Fill (in highbyte) */
.Lmemset32sharedPre:
orr r2, r3, r3, lsr #8
orr r2, r2, r2, lsr #16 /* r2 = fill */
/* Optimized 32bit memset
*
* Writes a 32bit value n times to the given address. Can deal with unaligned addresses.
* This routine is called from within all other memset routines
*
* The properties and rationale of this implementation are:
* - uste store multiple ins, since some CPUS can generate burst writes to DRam with stm.
* eg. ARM926EJ-S can generate 4 or 8 word bursts, result in 2-3 times the bandwidth of regular memset
* - optimized block loops
* the slower the cpu compared to the ram (and cache) the more it gains from optimized code
* - writes are always (fully) aligned to the write-size
* - amount of write instructions should be as small as reasonably possible
* archs without Ooe or using uncached accesses can stall on memory accesses
* - doesnt need to spill registers to the stack for small lengths (= doesnt need a stack at all)
*/
FUNCTION _memset32shared
/* private entry point */
/* TODO: ARM Mode will be used for thumb1 CPUs which support it */
.Lmemset32shared:
/* atleast 4 bytes to copy */
mov ip, r0
/* r0 - ptr, r1: endptr, r2: fill, ip: org */
/* align to 4 byte */
#if 0
/* This is code to correctly rotate the fillvalue
* for wmemset while aligning destination address.
* The perf hit is not worth it */
#if defined(__ARMEL__)
lsrs r3, r0, #1
itt cs
strbcs r2, [r0], #1
rorcs r2, #8
FUNCTION _memset16entry
lsrs r3, r0, #2
itt cs
strhcs r2, [r0], #2
rorcs r2, #16
#elif defined(__ARMEB__)
lsrs r3, r0, #1
itt cs
rorcs r2, #24
strbcs r2, [r0], #1
FUNCTION _memset16entry
lsrs r3, r0, #2
itt cs
rorcs r2, #16
strhcs r2, [r0], #2
#else
#error Undefined endian
#endif
#else
lsrs r3, r0, #1
#if __ARM_ARCH < 8
it cs
strbcs r2, [r0], #1
#else
it cs
strbcs r2, [r0]
it cs
addcs r0, #1
#endif
FUNCTION _memset16entry
.Lmemset16entry:
lsrs r3, r0, #2
STORE2BI r0, r2, r3, cs
#endif
/* correct the written bytes by substracting the count from size */
subs r3, r0, ip
subs r1, r3
b .Lmemset32entry
FUNCTION __aeabi_memclr4
FUNCTION __aeabi_memclr8
movs r2, #0
FUNCTION _memset32entry
.Lmemset32entry:
/* r0 - ptr, r1: size, r2: fill, ip: org */
/* align to 8 byte if rem big enough */
lsrs r3, r1, #2 /* rem >= 4 bytes -> ne */
#if !defined(_MEMSET_THUMB) || __ARM_ARCH < 8
mov r3, r2
it ne
tstne r0, #4 /* addr needs alignment -> ne (only tst and cmp set flags in it block) */
itt ne
strne r2, [r0], #4
subne r1, #4
#else
beq 1f
lsrs r3, r0, #(2 + 1) /* shift the 2nd bit into carry */
bcc 1f
stmia r0!, {r2}
subs r1, #4
1:
movs r3, r2
#endif
#if !(defined(_MEMSET_SIMPLELOOP) && (_MEMSET_SIMPLELOOP != 0))
/* r0 - ptr(align 4), r1: rem, r2: fill, r3: fill, ip: org */
/* take the short routine, unless the long is faster
* the value needs to be atleast 128 for correct operation
*/
cmp r1, #(_MEMSET_SMALLBLOCKLEN) /* todo: research better value! */
/* r0: ptr(align 4 or 8), r1: rem, r2: fill, r3: fill, ip: org */
blo .Lskiplong
/* if big enough store registers on stack and use 64byte loop */
push {r4 - r9}
/* set 8 registers to fill value inbetween dependend instructions */
/* align to 32 bytes, 64 bytes would be easily doable but wouldnt help at all */
adds r1, r0 /* store endptr */
movs r4, r2
lsrs r7, r0, #(3 + 1) /* shift the 3rd bit into carry */
mov r9, r2
it cs
STORE8PI r0, r2, r3, cs
movs r5, r2
lsrs r7, r0, #(4 + 1) /* shift the 4t bit into carry */
mov r6, r2
it cs
stmiacs r0!, {r2 - r5}
subs r1, r0 /* recalculate remaining */
subs r1, #64
mov r8, r2
lsrs r7, r1, #(5 + 1) /* jump to second write if at least 32 byte would remain after loop
* , the count gets masked away to < 16 bytes so dont bother decrementing */
mov r7, r2
bcs 2f
/* r0 - ptr(align 32), r1: rem-64, r2 - r9: fill, ip: org */
/* 64 byte set loop
* To enable dual-issue, stm must be in pipeline 0 (Cortex A8, possibly others)
* alignment to 8 byte should help ifetches too */
#ifndef _MEMSET_THUMB
.align 3
#endif
1:
stmia r0!, {r2 - r9}
subs r1, #64
2:
stmia r0!, {r2 - r9}
bhs 1b
lsrs r7, r1, #(4 + 1) /* still a 16 byte-block remaining? */
it cs
stmiacs r0!, {r2 - r5}
and r1, #0xF /* fixup remaining */
pop {r4 - r9}
#elif defined( _MEMSET_FPU) /* defined(_MEMSET_SIMPLELOOP) && (_MEMSET_SIMPLELOOP != 0) */
/* r0 - ptr(align 4), r1: rem, r2: fill, r3: fill, ip: org */
subs r1, #32
blo 2f /* skip if less than 32 byte */
/* 32byte copy loop using fpu registers */
#ifdef __ARM_NEON__
veor q0, q0
veor q1, q1
#else
vmov d0, r2, r2
vmov.f64 d1, d0
vmov.f64 d2, d0
vmov.f64 d3, d0
#endif
1:
subs r1, #32
vstm r0!, {q0, q1}
bhs 1b
2:
adds r1, #32
#else /* defined(_MEMSET_SIMPLELOOP) && (_MEMSET_SIMPLELOOP != 0) */
/* r0 - ptr(align 4), r1: rem, r2: fill, r3: fill, ip: org */
subs r1, #32
blo 2f /* skip if less than 32 byte */
/* 32byte copy loop using r2, r3 */
#ifndef _MEMSET_THUMB
.align 3
#endif
1:
#if __ARM_ARCH >= 6
strd r2, [r0], #32
subs r1, #32
strd r2, [r0, #(0x08 - 32)]
strd r2, [r0, #(0x10 - 32)]
strd r2, [r0, #(0x18 - 32)]
#else
stmia r0!, {r2, r3}
subs r1, #32
stmia r0!, {r2, r3}
stmia r0!, {r2, r3}
stmia r0!, {r2, r3}
#endif
bhs 1b
2:
adds r1, #32
#endif /* defined(_MEMSET_SIMPLELOOP) && (_MEMSET_SIMPLELOOP != 0) */
.Lskiplong:
/* we are either aligned to 8 byte,
* or to 4 byte with less than 4 bytes remaining
* or no bytes are remaining */
subs r1, #8
blo 2f /* skip if less than 8 byte */
/* r0 - ptr(align 8), r1: rem-8, r2: fill, r3: fill, ip: org */
1: /* 8 byte set loop */
subs r1, #8
STORE8PI r0, r2, r3
bhs 1b
2:
/* r0 - ptr(align 4), r1: rem - 8, r2: fill, r3: fill, ip: org
* test the bits in rem while use shifts since those are smaller opcodes in thumb */
/* write remaining bytes in optimal aligned manner */
lsls r1, #(32 - 2)
#if !defined(_MEMSET_THUMB) || __ARM_ARCH < 8
it cs
strcs r2, [r0], #4
#else
it cs
strcs r2, [r0]
it cs
addcs r0, #4
#endif
/* write last < 4 bytes */
#if 0
#if defined(__ARMEL__)
lsls r1, #1
itt cs
strhcs r2, [r0], #2
lsrcs r2, #16
lsls r1, #1
it cs
strbcs r2, [r0]
#elif defined(__ARMEB__)
lsls r1, #1
itt cs
rorcs r2, #16
strhcs r2, [r0], #2
lsls r1, #1
itt cs
lsrcs r2, #24
strbcs r2, [r0]
#else
#error Undefined endian
#endif
#else
lsls r1, #1
STORE2BI r0, r2, r3, cs
lsls r1, #1
it cs
strbcs r2, [r0]
#endif
mov r0, ip
RETURN
.size _memset32shared, . - _memset32shared
#else /* !defined(__thumb__) || defined(__thumb2__) */
/* Thumb 1 */
#if defined(__WCHAR_MAX__) && __WCHAR_MAX__ <= 255
FUNCTION wmemset
#endif
FUNCTION _memset8
/* fixup registers to look like the many __eabi calls */
lsls r3, r1, #24
movs r1, r2
cmp r1, #3
bhi .Lmemset32sharedPre
lsrs r2, r3, #24
cmp r1, #3
.Lsupershort8eabi:
/* we can only end up here if len <=3 */
bne 1f
strb r2, [r0, #2] /* if len == 3 */
1:
cmp r1, #1
blo 1f
strb r2, [r0, #0] /* if len >= 1 */
1:
bls 1f
strb r2, [r0, #1] /* if len > 1 */
1:
RETURN
FUNCTION __aeabi_memclr
movs r2, #0
FUNCTION __aeabi_memset
FUNCTION __aeabi_memset4
FUNCTION __aeabi_memset8
lsls r3, r2, #24
cmp r1, #3
bls .Lsupershort8eabi
/* r0 - ptr, r1: size, r3: Fill (in highbyte) */
.Lmemset32sharedPre:
lsrs r2, r3, #8
orrs r3, r2
lsrs r2, r3, #16
orrs r2, r3
/* Optimized 32bit memset
*
* Writes a 32bit value n times to the given address. Can deal with unaligned addresses.
* This routine is called from within all other memset routines
*
* The properties and rationale of this implementation are:
* - uste store multiple ins, since some CPUS can generate burst writes to DRam with stm.
* eg. ARM926EJ-S can generate 4 or 8 word bursts, result in 2-3 times the bandwidth of regular memset
* - optimized block loops
* the slower the cpu compared to the ram (and cache) the more it gains from optimized code
* - writes are always (fully) aligned to the write-size
* - amount of write instructions should be as small as reasonably possible
* archs without Ooe or using uncached accesses can stall on memory accesses
* - doesnt need to spill registers to the stack for small lengths (= doesnt need a stack at all)
*/
FUNCTION _memset32shared
/* private entry point */
/* TODO: ARM Mode will be used for thumb1 CPUs which support it */
.Lmemset32shared:
/* atleast 4 bytes to copy */
mov ip, r0
/* r0 - ptr, r1: endptr, r2: fill, ip: org */
/* align to 4 byte */
#if 0
/* This is code to correctly rotate the fillvalue
* for wmemset while aligning destination address.
* The perf hit is not worth it */
#if defined(__ARMEL__)
lsrs r3, r0, #1
itt cs
strbcs r2, [r0], #1
rorcs r2, #8
FUNCTION _memset16entry
lsrs r3, r0, #2
itt cs
strhcs r2, [r0], #2
rorcs r2, #16
#elif defined(__ARMEB__)
lsrs r3, r0, #1
itt cs
rorcs r2, #24
strbcs r2, [r0], #1
FUNCTION _memset16entry
lsrs r3, r0, #2
itt cs
rorcs r2, #16
strhcs r2, [r0], #2
#else
#error Undefined endian
#endif
#else
lsrs r3, r0, #1
bcc 1f
strb r2, [r0]
adds r0, #1
1:
FUNCTION _memset16entry
.Lmemset16entry:
lsrs r3, r0, #2
bcc 1f
strh r2, [r0]
adds r0, #2
1:
#endif
/* correct the written bytes by substracting the count from size */
mov r3, ip
subs r3, r0, r3
subs r1, r3
b .Lmemset32entry
FUNCTION __aeabi_memclr4
FUNCTION __aeabi_memclr8
movs r2, #0
FUNCTION _memset32entry
.Lmemset32entry:
/* r0 - ptr, r1: size, r2: fill, ip: org */
/* align to 8 byte if rem big enough */
lsrs r3, r1, #2 /* rem >= 4 bytes -> ne */
beq 1f
lsrs r3, r0, #(2 + 1) /* shift the 2nd bit into carry */
bcc 1f
stmia r0!, {r2}
subs r1, #4
1:
movs r3, r2
#if !(defined(_MEMSET_SIMPLELOOP) && (_MEMSET_SIMPLELOOP != 0))
/* r0 - ptr(align 4), r1: rem, r2: fill, r3: fill, ip: org */
/* take the short routine, unless the long is faster
* the value needs to be atleast 128 for correct operation
*/
cmp r1, #(_MEMSET_SMALLBLOCKLEN) /* todo: research better value! */
/* r0: ptr(align 4 or 8), r1: rem, r2: fill, r3: fill, ip: org */
blo .Lskiplong
/* if big enough store registers on stack and use 64byte loop */
push {r4 - r5, r6, r7}
/* set 8 registers to fill value inbetween dependend instructions */
/* align to 32 bytes, 64 bytes would be easily doable but wouldnt help at all */
adds r1, r0 /* store endptr */
movs r4, r2
lsrs r7, r0, #(3 + 1) /* shift the 3rd bit into carry */
bcc 1f
STORE8PI r0, r2, r3
1:
movs r5, r2
lsrs r7, r0, #(4 + 1) /* shift the 4t bit into carry */
bcc 1f
stmia r0!, {r2 - r5}
1:
subs r1, r0 /* recalculate remaining */
subs r1, #64
lsrs r7, r1, #(5 + 1) /* jump to second write if at least 32 byte would remain after loop
* , the count gets masked away to < 16 bytes so dont bother decrementing */
bcs 2f
/* r0 - ptr(align 32), r1: rem-64, r2 - r5: fill, ip: org */
/* 64 byte set loop
* To enable dual-issue, stm must be in pipeline 0 (Cortex A8, possibly others)
* alignment to 8 byte should help ifetches too */
1:
stmia r0!, {r2 - r5}
stmia r0!, {r2 - r5}
subs r1, #64
2:
stmia r0!, {r2 - r5}
stmia r0!, {r2 - r5}
bhs 1b
lsrs r7, r1, #(4 + 1) /* still a 16 byte-block remaining? */
bcc 1f
stmia r0!, {r2 - r5}
1:
movs r5, #15
ands r1, r5 /* fixup remaining */
pop {r4 - r5, r6, r7}
#endif /* defined(_MEMSET_SIMPLELOOP) && (_MEMSET_SIMPLELOOP != 0) */
.Lskiplong:
/* we are either aligned to 8 byte,
* or to 4 byte with less than 4 bytes remaining
* or no bytes are remaining */
subs r1, #8
blo 2f /* skip if less than 8 byte */
/* r0 - ptr(align 8), r1: rem-8, r2: fill, r3: fill, ip: org */
1: /* 8 byte set loop */
subs r1, #8
STORE8PI r0, r2, r3
bhs 1b
2:
/* r0 - ptr(align 4), r1: rem - 8, r2: fill, r3: fill, ip: org
* test the bits in rem while use shifts since those are smaller opcodes in thumb */
/* write remaining bytes in optimal aligned manner */
lsls r1, #(32 - 2)
bcc 1f
stmia r0!, {r2}
1:
/* write last < 4 bytes */
#if 0
#if defined(__ARMEL__)
lsls r1, #1
itt cs
strhcs r2, [r0], #2
lsrcs r2, #16
lsls r1, #1
it cs
strbcs r2, [r0]
#elif defined(__ARMEB__)
lsls r1, #1
itt cs
rorcs r2, #16
strhcs r2, [r0], #2
lsls r1, #1
itt cs
lsrcs r2, #24
strbcs r2, [r0]
#else
#error Undefined endian
#endif
#else
lsls r1, #1
bcc 1f
strh r2, [r0]
adds r0, #2
1:
lsls r1, #1
bcc 1f
strb r2, [r0]
1:
#endif
mov r0, ip
RETURN
.size _memset32shared, . - _memset32shared
#endif /* !defined(__thumb__) || defined(__thumb2__) */