/*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_memcpy24#undef SDL_memcpy25#endif26#if SDL_DYNAMIC_API27#define SDL_memcpy SDL_memcpy_REAL28#endif29void *SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len)30{31#if defined(__GNUC__) && (defined(HAVE_LIBC) && HAVE_LIBC)32/* Presumably this is well tuned for speed.33On my machine this is twice as fast as the C code below.34*/35return __builtin_memcpy(dst, src, len);36#elif defined(HAVE_MEMCPY)37return memcpy(dst, src, len);38#elif defined(HAVE_BCOPY)39bcopy(src, dst, len);40return dst;41#else42/* GCC 4.9.0 with -O3 will generate movaps instructions with the loop43using Uint32* pointers, so we need to make sure the pointers are44aligned before we loop using them.45*/46if (((uintptr_t)src & 0x3) || ((uintptr_t)dst & 0x3)) {47// Do an unaligned byte copy48Uint8 *srcp1 = (Uint8 *)src;49Uint8 *dstp1 = (Uint8 *)dst;5051while (len--) {52*dstp1++ = *srcp1++;53}54} else {55size_t left = (len % 4);56Uint32 *srcp4, *dstp4;57Uint8 *srcp1, *dstp1;5859srcp4 = (Uint32 *)src;60dstp4 = (Uint32 *)dst;61len /= 4;62while (len--) {63*dstp4++ = *srcp4++;64}6566srcp1 = (Uint8 *)srcp4;67dstp1 = (Uint8 *)dstp4;68switch (left) {69case 3:70*dstp1++ = *srcp1++;71SDL_FALLTHROUGH;72case 2:73*dstp1++ = *srcp1++;74SDL_FALLTHROUGH;75case 1:76*dstp1++ = *srcp1++;77}78}79return dst;80#endif // HAVE_MEMCPY81}8283/* The optimizer on Visual Studio 2005 and later generates memcpy() and memset() calls.84We will provide our own implementation if we're not building with a C runtime. */85#ifndef HAVE_LIBC86// NOLINTNEXTLINE(readability-redundant-declaration)87extern void *memcpy(void *dst, const void *src, size_t len);88#if defined(_MSC_VER) && !defined(__INTEL_LLVM_COMPILER)89#pragma intrinsic(memcpy)90#endif9192#if defined(_MSC_VER) && !defined(__clang__)93#pragma function(memcpy)94#endif95// NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name)96void *memcpy(void *dst, const void *src, size_t len)97{98return SDL_memcpy(dst, src, len);99}100#endif // !HAVE_LIBC101102103