Alexei Fedorov | 3ad26ae | 2020-08-16 16:01:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <asm_macros.S> |
| 8 | |
| 9 | .global memset |
| 10 | |
| 11 | /* ----------------------------------------------------------------------- |
| 12 | * void memset(void *dst, int val, size_t count) |
| 13 | * |
| 14 | * Copy the value of 'val' (converted to an unsigned char) into |
| 15 | * each of the first 'count' characters of the object pointed to by 'dst'. |
| 16 | * |
| 17 | * Returns the value of 'dst'. |
| 18 | * ----------------------------------------------------------------------- |
| 19 | */ |
| 20 | func memset |
| 21 | cbz w2, exit /* exit if 'count' = 0 */ |
| 22 | mov x3, x0 /* keep x0 */ |
| 23 | tst x0, #7 |
| 24 | b.eq aligned /* 8-bytes aligned */ |
| 25 | |
| 26 | /* Unaligned 'dst' */ |
| 27 | unaligned: |
| 28 | strb w1, [x3], #1 |
| 29 | subs w2, w2, #1 |
| 30 | b.eq exit /* exit if 0 */ |
| 31 | tst x3, #7 |
| 32 | b.ne unaligned /* continue while unaligned */ |
| 33 | |
| 34 | /* 8-bytes aligned */ |
| 35 | aligned:cbz x1, x1_zero |
| 36 | bfi w1, w1, #8, #8 /* propagate 'val' */ |
| 37 | bfi w1, w1, #16, #16 |
| 38 | bfi x1, x1, #32, #32 |
| 39 | |
| 40 | x1_zero:ands w4, w2, #~0x3f |
| 41 | b.eq less_64 |
| 42 | |
| 43 | write_64: |
| 44 | .rept 4 |
| 45 | stp x1, x1, [x3], #16 /* write 64 bytes in a loop */ |
| 46 | .endr |
| 47 | subs w4, w4, #64 |
| 48 | b.ne write_64 |
| 49 | ands w2, w2, #0x3f |
| 50 | b.eq exit /* exit if 0 */ |
| 51 | |
| 52 | less_64:tbz w2, #5, less_32 /* < 32 bytes */ |
| 53 | stp x1, x1, [x3], #16 /* write 32 bytes */ |
| 54 | stp x1, x1, [x3], #16 |
| 55 | ands w2, w2, #0x1f |
| 56 | b.eq exit |
| 57 | |
| 58 | less_32:tbz w2, #4, less_16 /* < 16 bytes */ |
| 59 | stp x1, x1, [x3], #16 /* write 16 bytes */ |
| 60 | ands w2, w2, #0xf |
| 61 | b.eq exit |
| 62 | |
| 63 | less_16:tbz w2, #3, less_8 /* < 8 bytes */ |
| 64 | str x1, [x3], #8 /* write 8 bytes */ |
| 65 | ands w2, w2, #7 |
| 66 | b.eq exit |
| 67 | |
| 68 | less_8: tbz w2, #2, less_4 /* < 4 bytes */ |
| 69 | str w1, [x3], #4 /* write 4 bytes */ |
| 70 | ands w2, w2, #3 |
| 71 | b.eq exit |
| 72 | |
| 73 | less_4: tbz w2, #1, less_2 /* < 2 bytes */ |
| 74 | strh w1, [x3], #2 /* write 2 bytes */ |
| 75 | tbz w2, #0, exit |
| 76 | less_2: strb w1, [x3] /* write 1 byte */ |
| 77 | exit: ret |
| 78 | |
| 79 | endfunc memset |