SDL_memcpy.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. #ifdef __GNUC__
  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. case 2:
  65. *dstp1++ = *srcp1++;
  66. case 1:
  67. *dstp1++ = *srcp1++;
  68. }
  69. }
  70. return dst;
  71. #endif /* __GNUC__ */
  72. }
  73. /* The optimizer on Visual Studio 2005 and later generates memcpy() and memset() calls.
  74. Always provide it for the SDL3 DLL, but skip it when building static lib w/ static runtime. */
  75. #if defined(_MSC_VER) && (_MSC_VER >= 1400) && (!defined(_MT) || defined(DLL_EXPORT))
  76. /* NOLINTNEXTLINE(readability-redundant-declaration) */
  77. extern void *memcpy(void *dst, const void *src, size_t len);
  78. #ifndef __INTEL_LLVM_COMPILER
  79. #pragma intrinsic(memcpy)
  80. #endif
  81. #ifndef __clang__
  82. #pragma function(memcpy)
  83. #endif
  84. /* NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name) */
  85. void *memcpy(void *dst, const void *src, size_t len)
  86. {
  87. return SDL_memcpy(dst, src, len);
  88. }
  89. #endif /* (_MSC_VER >= 1400) && (!defined(_MT) || defined(DLL_EXPORT)) */