/libc/string/memcpy.c

https://gitlab.com/PedroFalcato/sortix · C · 113 lines · 81 code · 14 blank · 18 comment · 18 complexity · cef7e8c258ff03d0dd3042209a4293ee MD5 · raw file

  1. /*
  2. * Copyright (c) 2011, 2012, 2014 Jonas 'Sortie' Termansen.
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. *
  16. * string/memcpy.c
  17. * Copy memory between non-overlapping regions.
  18. */
  19. #include <stdint.h>
  20. #include <string.h>
  21. inline static
  22. void* memcpy_slow(void* restrict dst_ptr,
  23. const void* restrict src_ptr,
  24. size_t size)
  25. {
  26. unsigned char* restrict dst = (unsigned char* restrict) dst_ptr;
  27. const unsigned char* restrict src = (const unsigned char* restrict) src_ptr;
  28. for ( size_t i = 0; i < size; i++ )
  29. dst[i] = src[i];
  30. return dst_ptr;
  31. }
  32. void* memcpy(void* restrict dst_ptr,
  33. const void* restrict src_ptr,
  34. size_t size)
  35. {
  36. #if 8 < __SIZEOF_LONG__
  37. #warning "you should add support for your unexpectedly large unsigned long."
  38. return memcpy_slow(dst_ptr, src_ptr, size);
  39. #else
  40. unsigned long unalign_mask = sizeof(unsigned long) - 1;
  41. unsigned long src_unalign = (unsigned long) src_ptr & unalign_mask;
  42. unsigned long dst_unalign = (unsigned long) dst_ptr & unalign_mask;
  43. if ( src_unalign != dst_unalign )
  44. return memcpy_slow(dst_ptr, src_ptr, size);
  45. union
  46. {
  47. unsigned long srcval;
  48. const unsigned char* restrict src8;
  49. const uint16_t* restrict src16;
  50. const uint32_t* restrict src32;
  51. const uint64_t* restrict src64;
  52. const unsigned long* restrict srcul;
  53. } su;
  54. su.srcval = (unsigned long) src_ptr;
  55. union
  56. {
  57. unsigned long dstval;
  58. unsigned char* restrict dst8;
  59. uint16_t* restrict dst16;
  60. uint32_t* restrict dst32;
  61. uint64_t* restrict dst64;
  62. unsigned long* restrict dstul;
  63. } du;
  64. du.dstval = (unsigned long) dst_ptr;
  65. if ( dst_unalign )
  66. {
  67. if ( 1 <= size && !(du.dstval & (1-1)) && (du.dstval & (2-1)) )
  68. *du.dst8++ = *su.src8++,
  69. size -= 1;
  70. if ( 2 <= size && !(du.dstval & (2-1)) && (du.dstval & (4-1)) )
  71. *du.dst16++ = *su.src16++,
  72. size -= 2;
  73. #if 8 <= __SIZEOF_LONG__
  74. if ( 4 <= size && !(du.dstval & (4-1)) && (du.dstval & (8-1)) )
  75. *du.dst32++ = *su.src32++,
  76. size -= 4;
  77. #endif
  78. }
  79. size_t num_copies = size / sizeof(unsigned long);
  80. for ( size_t i = 0; i < num_copies; i++ )
  81. *du.dstul++ = *su.srcul++;
  82. size -= num_copies * sizeof(unsigned long);
  83. if ( size )
  84. {
  85. #if 8 <= __SIZEOF_LONG__
  86. if ( 4 <= size )
  87. *du.dst32++ = *su.src32++,
  88. size -= 4;
  89. #endif
  90. if ( 2 <= size )
  91. *du.dst16++ = *su.src16++,
  92. size -= 2;
  93. if ( 1 <= size )
  94. *du.dst8++ = *su.src8++,
  95. size -= 1;
  96. }
  97. return dst_ptr;
  98. #endif
  99. }