string.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 1991,1992,1993,1997,1998,2003, 2005 Free Software Foundation, Inc.
  3. * This file is part of the GNU C Library.
  4. * Copyright (c) 2011 The Chromium OS Authors.
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. /* From glibc-2.14, sysdeps/i386/memset.c */
  25. #include <compiler.h>
  26. #include <asm/string.h>
  27. #include <linux/types.h>
  28. typedef uint32_t op_t;
  29. void *memset(void *dstpp, int c, size_t len)
  30. {
  31. int d0;
  32. unsigned long int dstp = (unsigned long int) dstpp;
  33. /* This explicit register allocation improves code very much indeed. */
  34. register op_t x asm("ax");
  35. x = (unsigned char) c;
  36. /* Clear the direction flag, so filling will move forward. */
  37. asm volatile("cld");
  38. /* This threshold value is optimal. */
  39. if (len >= 12) {
  40. /* Fill X with four copies of the char we want to fill with. */
  41. x |= (x << 8);
  42. x |= (x << 16);
  43. /* Adjust LEN for the bytes handled in the first loop. */
  44. len -= (-dstp) % sizeof(op_t);
  45. /*
  46. * There are at least some bytes to set. No need to test for
  47. * LEN == 0 in this alignment loop.
  48. */
  49. /* Fill bytes until DSTP is aligned on a longword boundary. */
  50. asm volatile(
  51. "rep\n"
  52. "stosb" /* %0, %2, %3 */ :
  53. "=D" (dstp), "=c" (d0) :
  54. "0" (dstp), "1" ((-dstp) % sizeof(op_t)), "a" (x) :
  55. "memory");
  56. /* Fill longwords. */
  57. asm volatile(
  58. "rep\n"
  59. "stosl" /* %0, %2, %3 */ :
  60. "=D" (dstp), "=c" (d0) :
  61. "0" (dstp), "1" (len / sizeof(op_t)), "a" (x) :
  62. "memory");
  63. len %= sizeof(op_t);
  64. }
  65. /* Write the last few bytes. */
  66. asm volatile(
  67. "rep\n"
  68. "stosb" /* %0, %2, %3 */ :
  69. "=D" (dstp), "=c" (d0) :
  70. "0" (dstp), "1" (len), "a" (x) :
  71. "memory");
  72. return dstpp;
  73. }
  74. #define OP_T_THRES 8
  75. #define OPSIZ (sizeof(op_t))
  76. #define BYTE_COPY_FWD(dst_bp, src_bp, nbytes) \
  77. do { \
  78. int __d0; \
  79. asm volatile( \
  80. /* Clear the direction flag, so copying goes forward. */ \
  81. "cld\n" \
  82. /* Copy bytes. */ \
  83. "rep\n" \
  84. "movsb" : \
  85. "=D" (dst_bp), "=S" (src_bp), "=c" (__d0) : \
  86. "0" (dst_bp), "1" (src_bp), "2" (nbytes) : \
  87. "memory"); \
  88. } while (0)
  89. #define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes) \
  90. do { \
  91. int __d0; \
  92. asm volatile( \
  93. /* Clear the direction flag, so copying goes forward. */ \
  94. "cld\n" \
  95. /* Copy longwords. */ \
  96. "rep\n" \
  97. "movsl" : \
  98. "=D" (dst_bp), "=S" (src_bp), "=c" (__d0) : \
  99. "0" (dst_bp), "1" (src_bp), "2" ((nbytes) / 4) : \
  100. "memory"); \
  101. (nbytes_left) = (nbytes) % 4; \
  102. } while (0)
  103. void *memcpy(void *dstpp, const void *srcpp, size_t len)
  104. {
  105. unsigned long int dstp = (long int)dstpp;
  106. unsigned long int srcp = (long int)srcpp;
  107. /* Copy from the beginning to the end. */
  108. /* If there not too few bytes to copy, use word copy. */
  109. if (len >= OP_T_THRES) {
  110. /* Copy just a few bytes to make DSTP aligned. */
  111. len -= (-dstp) % OPSIZ;
  112. BYTE_COPY_FWD(dstp, srcp, (-dstp) % OPSIZ);
  113. /* Copy from SRCP to DSTP taking advantage of the known
  114. * alignment of DSTP. Number of bytes remaining is put
  115. * in the third argument, i.e. in LEN. This number may
  116. * vary from machine to machine.
  117. */
  118. WORD_COPY_FWD(dstp, srcp, len, len);
  119. /* Fall out and copy the tail. */
  120. }
  121. /* There are just a few bytes to copy. Use byte memory operations. */
  122. BYTE_COPY_FWD(dstp, srcp, len);
  123. return dstpp;
  124. }