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