Nugget
Bare-metal libraries and examples for the original PlayStation
Loading...
Searching...
No Matches
memory-c.c
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2024 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#include <stddef.h>
28#include <stdint.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
41void* 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}
void * memcpy(void *s1_, const void *s2_, size_t n)
__attribute__((weak))
Definition memory-c.c:43
void * memset(void *s, int c, size_t n)
int memcmp(const void *s1, const void *s2, size_t n)
void * memmove(void *s1, const void *s2, size_t n)
int i
Definition gte-regio.c:297
n
Definition gentable.py:95
char * s
Definition string.c:48
static const char * s2
Definition syscalls.h:92
static int c
Definition syscalls.h:122