lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2002, 2003 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Hartvig Ekner <hartvige@mips.com>, 2002. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, write to the Free |
| 17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 18 | 02111-1307 USA. */ |
| 19 | |
| 20 | #include <features.h> |
| 21 | /*#include <sysdep.h>*/ |
| 22 | #include <endian.h> |
| 23 | #include "sysdep.h" |
| 24 | |
| 25 | /* void *memset(void *s, int c, size_t n). */ |
| 26 | |
| 27 | #ifdef __mips64 |
| 28 | |
| 29 | #include <sys/asm.h> |
| 30 | |
| 31 | #if __BYTE_ORDER == __BIG_ENDIAN |
| 32 | # define SDHI sdl /* high part is left in big-endian */ |
| 33 | #else |
| 34 | # define SDHI sdr /* high part is right in little-endian */ |
| 35 | #endif |
| 36 | |
| 37 | ENTRY (memset) |
| 38 | .set noreorder |
| 39 | |
| 40 | slti ta1, a2, 16 # Less than 16? |
| 41 | bne ta1, zero, L(last16) |
| 42 | move v0, a0 # Setup exit value before too late |
| 43 | |
| 44 | beq a1, zero, L(ueven) # If zero pattern, no need to extend |
| 45 | andi a1, 0xff # Avoid problems with bogus arguments |
| 46 | dsll ta0, a1, 8 |
| 47 | or a1, ta0 |
| 48 | dsll ta0, a1, 16 |
| 49 | or a1, ta0 # a1 is now pattern in full word |
| 50 | dsll ta0, a1, 32 |
| 51 | or a1, ta0 # a1 is now pattern in double word |
| 52 | |
| 53 | L(ueven): |
| 54 | PTR_SUBU ta0, zero, a0 # Unaligned address? |
| 55 | andi ta0, 0x7 |
| 56 | beq ta0, zero, L(chkw) |
| 57 | PTR_SUBU a2, ta0 |
| 58 | SDHI a1, 0(a0) # Yes, handle first unaligned part |
| 59 | PTR_ADDU a0, ta0 # Now both a0 and a2 are updated |
| 60 | |
| 61 | L(chkw): |
| 62 | andi ta0, a2, 0xf # Enough left for one loop iteration? |
| 63 | beq ta0, a2, L(chkl) |
| 64 | PTR_SUBU a3, a2, ta0 |
| 65 | PTR_ADDU a3, a0 # a3 is last loop address +1 |
| 66 | move a2, ta0 # a2 is now # of bytes left after loop |
| 67 | L(loopw): |
| 68 | PTR_ADDIU a0, 16 # Handle 2 dwords pr. iteration |
| 69 | sd a1, -16(a0) |
| 70 | bne a0, a3, L(loopw) |
| 71 | sd a1, -8(a0) |
| 72 | |
| 73 | L(chkl): |
| 74 | andi ta0, a2, 0x8 # Check if there is at least a double |
| 75 | beq ta0, zero, L(last16) # word remaining after the loop |
| 76 | PTR_SUBU a2, ta0 |
| 77 | sd a1, 0(a0) # Yes... |
| 78 | PTR_ADDIU a0, 8 |
| 79 | |
| 80 | L(last16): |
| 81 | blez a2, L(exit) # Handle last 16 bytes (if cnt>0) |
| 82 | PTR_ADDU a3, a2, a0 # a3 is last address +1 |
| 83 | L(lst16l): |
| 84 | PTR_ADDIU a0, 1 |
| 85 | bne a0, a3, L(lst16l) |
| 86 | sb a1, -1(a0) |
| 87 | L(exit): |
| 88 | j ra # Bye, bye |
| 89 | nop |
| 90 | |
| 91 | .set reorder |
| 92 | END (memset) |
| 93 | |
| 94 | #else /* !__mips64 */ |
| 95 | |
| 96 | #if __BYTE_ORDER == __BIG_ENDIAN |
| 97 | # define SWHI swl /* high part is left in big-endian */ |
| 98 | #else |
| 99 | # define SWHI swr /* high part is right in little-endian */ |
| 100 | #endif |
| 101 | |
| 102 | ENTRY (memset) |
| 103 | .set noreorder |
| 104 | |
| 105 | slti t1, a2, 8 # Less than 8? |
| 106 | bne t1, zero, L(last8) |
| 107 | move v0, a0 # Setup exit value before too late |
| 108 | |
| 109 | beq a1, zero, L(ueven) # If zero pattern, no need to extend |
| 110 | andi a1, 0xff # Avoid problems with bogus arguments |
| 111 | sll t0, a1, 8 |
| 112 | or a1, t0 |
| 113 | sll t0, a1, 16 |
| 114 | or a1, t0 # a1 is now pattern in full word |
| 115 | |
| 116 | L(ueven): |
| 117 | subu t0, zero, a0 # Unaligned address? |
| 118 | andi t0, 0x3 |
| 119 | beq t0, zero, L(chkw) |
| 120 | subu a2, t0 |
| 121 | SWHI a1, 0(a0) # Yes, handle first unaligned part |
| 122 | addu a0, t0 # Now both a0 and a2 are updated |
| 123 | |
| 124 | L(chkw): |
| 125 | andi t0, a2, 0x7 # Enough left for one loop iteration? |
| 126 | beq t0, a2, L(chkl) |
| 127 | subu a3, a2, t0 |
| 128 | addu a3, a0 # a3 is last loop address +1 |
| 129 | move a2, t0 # a2 is now # of bytes left after loop |
| 130 | L(loopw): |
| 131 | addiu a0, 8 # Handle 2 words pr. iteration |
| 132 | sw a1, -8(a0) |
| 133 | bne a0, a3, L(loopw) |
| 134 | sw a1, -4(a0) |
| 135 | |
| 136 | L(chkl): |
| 137 | andi t0, a2, 0x4 # Check if there is at least a full |
| 138 | beq t0, zero, L(last8) # word remaining after the loop |
| 139 | subu a2, t0 |
| 140 | sw a1, 0(a0) # Yes... |
| 141 | addiu a0, 4 |
| 142 | |
| 143 | L(last8): |
| 144 | blez a2, L(exit) # Handle last 8 bytes (if cnt>0) |
| 145 | addu a3, a2, a0 # a3 is last address +1 |
| 146 | L(lst8l): |
| 147 | addiu a0, 1 |
| 148 | bne a0, a3, L(lst8l) |
| 149 | sb a1, -1(a0) |
| 150 | L(exit): |
| 151 | j ra # Bye, bye |
| 152 | nop |
| 153 | |
| 154 | .set reorder |
| 155 | END (memset) |
| 156 | |
| 157 | #endif /* !__mips64 */ |
| 158 | |
| 159 | libc_hidden_def(memset) |