|
| 1 | +/* |
| 2 | +
|
| 3 | +MIT License |
| 4 | +
|
| 5 | +Copyright (c) 2024 PCSX-Redux authors |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +of this software and associated documentation files (the "Software"), to deal |
| 9 | +in the Software without restriction, including without limitation the rights |
| 10 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +copies of the Software, and to permit persons to whom the Software is |
| 12 | +furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in all |
| 15 | +copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +SOFTWARE. |
| 24 | +
|
| 25 | +*/ |
| 26 | + |
| 27 | +#include <stdint.h> |
| 28 | +#include <stddef.h> |
| 29 | + |
| 30 | +// These are the 4 essential functions expected by gcc for a naked build. |
| 31 | +// The implementation of memcpy is in memory-s.s, and the rest are here. |
| 32 | +// The weak attribute is used to allow the user to override these functions |
| 33 | +// with their own implementation if they so desire. The memory-s.s implementation |
| 34 | +// is a simple byte copy, and is not optimized for speed. The file also contains |
| 35 | +// a faster but bigger implementation that can be used instead, called |
| 36 | +// __wrap_memcpy. The user can override memcpy with __wrap_memcpy to use it using |
| 37 | +// the -Wl,--wrap=memcpy switch to the linker using LDFLAGS. The same file also |
| 38 | +// contains a fast implementation of memset, called __wrap_memset, that can be |
| 39 | +// used in the same way. |
| 40 | + |
| 41 | +void* memcpy(void* s1_, const void* s2_, size_t n); |
| 42 | + |
| 43 | +__attribute__((weak)) void* memmove(void* s1_, const void* s2_, size_t n) { |
| 44 | + uint8_t* s1 = (uint8_t*)s1_; |
| 45 | + const uint8_t* s2 = (uint8_t*)s2_; |
| 46 | + size_t i; |
| 47 | + |
| 48 | + uint8_t* e1 = s1 + n; |
| 49 | + const uint8_t* e2 = s2 + n; |
| 50 | + |
| 51 | + if ((s1 <= s2) && (s2 <= e1) || ((s2 <= s1) && (s1 <= e2))) { |
| 52 | + if (s1 < s2) { |
| 53 | + for (i = 0; i < n; i++) *s1++ = *s2++; |
| 54 | + } else if (s1 > s2) { |
| 55 | + s1 += n; |
| 56 | + s2 += n; |
| 57 | + for (i = 0; i < n; i++) *--s1 = *--s2; |
| 58 | + } |
| 59 | + } else { |
| 60 | + return memcpy(s1_, s2_, n); |
| 61 | + } |
| 62 | + |
| 63 | + return s1_; |
| 64 | +} |
| 65 | + |
| 66 | +__attribute__((weak)) int memcmp(const void* s1_, const void* s2_, size_t n) { |
| 67 | + uint8_t* s1 = (uint8_t*)s1_; |
| 68 | + const uint8_t* s2 = (uint8_t*)s2_; |
| 69 | + size_t i; |
| 70 | + |
| 71 | + for (i = 0; i < n; i++, s1++, s2++) { |
| 72 | + if (*s1 < *s2) { |
| 73 | + return -1; |
| 74 | + } else if (*s1 > *s2) { |
| 75 | + return 1; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | +__attribute__((weak)) void* memset(void* s_, int c, size_t n) { |
| 83 | + uint8_t* s = (uint8_t*)s_; |
| 84 | + size_t i; |
| 85 | + |
| 86 | + for (i = 0; i < n; i++) *s++ = (uint8_t)c; |
| 87 | + |
| 88 | + return s_; |
| 89 | +} |
| 90 | + |
0 commit comments