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_memset |
25 | #undef SDL_memset |
26 | #endif |
27 | #if SDL_DYNAMIC_API |
28 | #define SDL_memset SDL_memset_REAL |
29 | #endif |
30 | void *SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len) |
31 | { |
32 | #if defined(__GNUC__) && (defined(HAVE_LIBC) && HAVE_LIBC) |
33 | return __builtin_memset(dst, c, len); |
34 | #elif defined(HAVE_MEMSET) |
35 | return memset(dst, c, len); |
36 | #else |
37 | size_t left; |
38 | Uint32 *dstp4; |
39 | Uint8 *dstp1 = (Uint8 *)dst; |
40 | Uint8 value1; |
41 | Uint32 value4; |
42 | |
43 | // The value used in memset() is a byte, passed as an int |
44 | c &= 0xff; |
45 | |
46 | /* The destination pointer needs to be aligned on a 4-byte boundary to |
47 | * execute a 32-bit set. Set first bytes manually if needed until it is |
48 | * aligned. */ |
49 | value1 = (Uint8)c; |
50 | while ((uintptr_t)dstp1 & 0x3) { |
51 | if (len--) { |
52 | *dstp1++ = value1; |
53 | } else { |
54 | return dst; |
55 | } |
56 | } |
57 | |
58 | value4 = ((Uint32)c | ((Uint32)c << 8) | ((Uint32)c << 16) | ((Uint32)c << 24)); |
59 | dstp4 = (Uint32 *)dstp1; |
60 | left = (len % 4); |
61 | len /= 4; |
62 | while (len--) { |
63 | *dstp4++ = value4; |
64 | } |
65 | |
66 | dstp1 = (Uint8 *)dstp4; |
67 | switch (left) { |
68 | case 3: |
69 | *dstp1++ = value1; |
70 | SDL_FALLTHROUGH; |
71 | case 2: |
72 | *dstp1++ = value1; |
73 | SDL_FALLTHROUGH; |
74 | case 1: |
75 | *dstp1++ = value1; |
76 | } |
77 | |
78 | return dst; |
79 | #endif // HAVE_MEMSET |
80 | } |
81 | |
82 | // Note that memset() is a byte assignment and this is a 32-bit assignment, so they're not directly equivalent. |
83 | void *SDL_memset4(void *dst, Uint32 val, size_t dwords) |
84 | { |
85 | #if defined(__APPLE__) && defined(HAVE_STRING_H) |
86 | memset_pattern4(dst, &val, dwords * 4); |
87 | #elif defined(__GNUC__) && defined(__i386__) |
88 | int u0, u1, u2; |
89 | __asm__ __volatile__( |
90 | "cld \n\t" |
91 | "rep ; stosl \n\t" |
92 | : "=&D" (u0), "=&a" (u1), "=&c" (u2) |
93 | : "0" (dst), "1" (val), "2" (SDL_static_cast(Uint32, dwords)) |
94 | : "memory" ); |
95 | #else |
96 | size_t _n = (dwords + 3) / 4; |
97 | Uint32 *_p = SDL_static_cast(Uint32 *, dst); |
98 | Uint32 _val = (val); |
99 | if (dwords == 0) { |
100 | return dst; |
101 | } |
102 | switch (dwords % 4) { |
103 | case 0: |
104 | do { |
105 | *_p++ = _val; |
106 | SDL_FALLTHROUGH; |
107 | case 3: |
108 | *_p++ = _val; |
109 | SDL_FALLTHROUGH; |
110 | case 2: |
111 | *_p++ = _val; |
112 | SDL_FALLTHROUGH; |
113 | case 1: |
114 | *_p++ = _val; |
115 | } while (--_n); |
116 | } |
117 | #endif |
118 | return dst; |
119 | } |
120 | |
121 | /* The optimizer on Visual Studio 2005 and later generates memcpy() and memset() calls. |
122 | We will provide our own implementation if we're not building with a C runtime. */ |
123 | #ifndef HAVE_LIBC |
124 | // NOLINTNEXTLINE(readability-redundant-declaration) |
125 | extern void *memset(void *dst, int c, size_t len); |
126 | #if defined(_MSC_VER) && !defined(__INTEL_LLVM_COMPILER) |
127 | #pragma intrinsic(memset) |
128 | #endif |
129 | |
130 | #if defined(_MSC_VER) && !defined(__clang__) |
131 | #pragma function(memset) |
132 | #endif |
133 | // NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name) |
134 | void *memset(void *dst, int c, size_t len) |
135 | { |
136 | return SDL_memset(dst, c, len); |
137 | } |
138 | #endif // !HAVE_LIBC |
139 | |
140 | |