SDL_memset.c 3.9 KB

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