SDL_memcpy.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_memcpy
  20. #undef SDL_memcpy
  21. #endif
  22. #if SDL_DYNAMIC_API
  23. #define SDL_memcpy SDL_memcpy_REAL
  24. #endif
  25. void *SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len)
  26. {
  27. #if defined(__GNUC__) && (defined(HAVE_LIBC) && HAVE_LIBC)
  28. /* Presumably this is well tuned for speed.
  29. On my machine this is twice as fast as the C code below.
  30. */
  31. return __builtin_memcpy(dst, src, len);
  32. #elif defined(HAVE_MEMCPY)
  33. return memcpy(dst, src, len);
  34. #elif defined(HAVE_BCOPY)
  35. bcopy(src, dst, len);
  36. return dst;
  37. #else
  38. /* GCC 4.9.0 with -O3 will generate movaps instructions with the loop
  39. using Uint32* pointers, so we need to make sure the pointers are
  40. aligned before we loop using them.
  41. */
  42. if (((uintptr_t)src & 0x3) || ((uintptr_t)dst & 0x3)) {
  43. // Do an unaligned byte copy
  44. Uint8 *srcp1 = (Uint8 *)src;
  45. Uint8 *dstp1 = (Uint8 *)dst;
  46. while (len--) {
  47. *dstp1++ = *srcp1++;
  48. }
  49. } else {
  50. size_t left = (len % 4);
  51. Uint32 *srcp4, *dstp4;
  52. Uint8 *srcp1, *dstp1;
  53. srcp4 = (Uint32 *)src;
  54. dstp4 = (Uint32 *)dst;
  55. len /= 4;
  56. while (len--) {
  57. *dstp4++ = *srcp4++;
  58. }
  59. srcp1 = (Uint8 *)srcp4;
  60. dstp1 = (Uint8 *)dstp4;
  61. switch (left) {
  62. case 3:
  63. *dstp1++ = *srcp1++;
  64. SDL_FALLTHROUGH;
  65. case 2:
  66. *dstp1++ = *srcp1++;
  67. SDL_FALLTHROUGH;
  68. case 1:
  69. *dstp1++ = *srcp1++;
  70. }
  71. }
  72. return dst;
  73. #endif // HAVE_MEMCPY
  74. }
  75. /* The optimizer on Visual Studio 2005 and later generates memcpy() and memset() calls.
  76. We will provide our own implementation if we're not building with a C runtime. */
  77. #ifndef HAVE_LIBC
  78. // NOLINTNEXTLINE(readability-redundant-declaration)
  79. extern void *memcpy(void *dst, const void *src, size_t len);
  80. #if defined(_MSC_VER) && !defined(__INTEL_LLVM_COMPILER)
  81. #pragma intrinsic(memcpy)
  82. #endif
  83. #if defined(_MSC_VER) && !defined(__clang__)
  84. #pragma function(memcpy)
  85. #endif
  86. // NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name)
  87. void *memcpy(void *dst, const void *src, size_t len)
  88. {
  89. return SDL_memcpy(dst, src, len);
  90. }
  91. #endif // !HAVE_LIBC