memset.S 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (C) 2016 Andes Technology, Inc.
  3. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  4. */
  5. /* Copyright (C) 1998, 2003 Free Software Foundation, Inc.
  6. * Contributed by Philip Blundell <philb@gnu.org>
  7. *
  8. * The GNU C Library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * The GNU C Library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with the GNU C Library; if not, write to the Free
  20. * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA.
  22. */
  23. #include <features.h>
  24. #include <sysdep.h>
  25. !==========================================================
  26. ! void *memset(void *dst, int val, int len);
  27. !
  28. ! dst: $r0
  29. ! val: $r1
  30. ! len: $r2
  31. ! ret: $r0 - pointer to the memory area dst.
  32. !==========================================================
  33. .weak memset
  34. ENTRY(memset)
  35. move $r5, $r0 ! Return value
  36. beqz $r2, .Lend_memset ! Exit when len = 0
  37. srli $r3, $r2, 2 ! r3 is how many words to copy
  38. andi $r2, $r2, 3 ! How many bytes are less than a word
  39. beqz $r3, .Lbyte_set ! When n is less than a word
  40. ! set r1 from to abababab
  41. andi $r1, $r1, 0x00ff ! r1 = 000000ab
  42. slli $r4, $r1, 8 ! r4 = 0000ab00
  43. or $r1, $r1, $r4 ! r1 = 0000abab
  44. slli $r4, $r1, 16 ! r4 = abab0000
  45. or $r1, $r1, $r4 ! r1 = abababab
  46. .Lword_set:
  47. addi $r3, $r3, -1 ! How many words left to copy
  48. smw.bim $r1, [$r5], $r1 ! Copy the word to det
  49. bnez $r3, .Lword_set ! Still words to set, continue looping
  50. beqz $r2, .Lend_memset ! No left byte to set
  51. .Lbyte_set:
  52. ! Less than 4 bytes left to set
  53. addi $r2, $r2, -1 ! Decrease len by 1
  54. sbi.p $r1, [$r5], 1 ! Set data of the next byte to r1
  55. bnez $r2, .Lbyte_set ! Still bytes left to set
  56. .Lend_memset:
  57. ret
  58. END(memset)
  59. libc_hidden_def(memset)