/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/20#include "SDL_internal.h"212223#ifdef SDL_memset24#undef SDL_memset25#endif26#if SDL_DYNAMIC_API27#define SDL_memset SDL_memset_REAL28#endif29void *SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len)30{31#if defined(__GNUC__) && (defined(HAVE_LIBC) && HAVE_LIBC)32return __builtin_memset(dst, c, len);33#elif defined(HAVE_MEMSET)34return memset(dst, c, len);35#else36size_t left;37Uint32 *dstp4;38Uint8 *dstp1 = (Uint8 *)dst;39Uint8 value1;40Uint32 value4;4142// The value used in memset() is a byte, passed as an int43c &= 0xff;4445/* The destination pointer needs to be aligned on a 4-byte boundary to46* execute a 32-bit set. Set first bytes manually if needed until it is47* aligned. */48value1 = (Uint8)c;49while ((uintptr_t)dstp1 & 0x3) {50if (len--) {51*dstp1++ = value1;52} else {53return dst;54}55}5657value4 = ((Uint32)c | ((Uint32)c << 8) | ((Uint32)c << 16) | ((Uint32)c << 24));58dstp4 = (Uint32 *)dstp1;59left = (len % 4);60len /= 4;61while (len--) {62*dstp4++ = value4;63}6465dstp1 = (Uint8 *)dstp4;66switch (left) {67case 3:68*dstp1++ = value1;69SDL_FALLTHROUGH;70case 2:71*dstp1++ = value1;72SDL_FALLTHROUGH;73case 1:74*dstp1++ = value1;75}7677return dst;78#endif // HAVE_MEMSET79}8081// Note that memset() is a byte assignment and this is a 32-bit assignment, so they're not directly equivalent.82void *SDL_memset4(void *dst, Uint32 val, size_t dwords)83{84#if defined(__APPLE__) && defined(HAVE_STRING_H)85memset_pattern4(dst, &val, dwords * 4);86#elif defined(__GNUC__) && defined(__i386__)87int u0, u1, u2;88__asm__ __volatile__(89"cld \n\t"90"rep ; stosl \n\t"91: "=&D"(u0), "=&a"(u1), "=&c"(u2)92: "0"(dst), "1"(val), "2"(SDL_static_cast(Uint32, dwords))93: "memory");94#else95size_t _n = (dwords + 3) / 4;96Uint32 *_p = SDL_static_cast(Uint32 *, dst);97Uint32 _val = (val);98if (dwords == 0) {99return dst;100}101switch (dwords % 4) {102case 0:103do {104*_p++ = _val;105SDL_FALLTHROUGH;106case 3:107*_p++ = _val;108SDL_FALLTHROUGH;109case 2:110*_p++ = _val;111SDL_FALLTHROUGH;112case 1:113*_p++ = _val;114} while (--_n);115}116#endif117return dst;118}119120/* The optimizer on Visual Studio 2005 and later generates memcpy() and memset() calls.121We will provide our own implementation if we're not building with a C runtime. */122#ifndef HAVE_LIBC123// NOLINTNEXTLINE(readability-redundant-declaration)124extern void *memset(void *dst, int c, size_t len);125#if defined(_MSC_VER) && !defined(__INTEL_LLVM_COMPILER)126#pragma intrinsic(memset)127#endif128129#if defined(_MSC_VER) && !defined(__clang__)130#pragma function(memset)131#endif132// NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name)133void *memset(void *dst, int c, size_t len)134{135return SDL_memset(dst, c, len);136}137#endif // !HAVE_LIBC138139140141