-
Notifications
You must be signed in to change notification settings - Fork 8
/
wwfcLibC.cpp
167 lines (135 loc) · 2.8 KB
/
wwfcLibC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "wwfcLibC.hpp"
#include <algorithm>
#include <ctype.h>
#include <string.h>
extern "C" {
int tolower(int c)
{
if (c >= 'A' && c <= 'Z') {
return c + 'a' - 'A';
}
return c;
}
int toupper(int c)
{
if (c >= 'a' && c <= 'z') {
return c + 'A' - 'a';
}
return c;
}
__attribute__((__optimize__("-fno-tree-loop-distribute-patterns"))) //
void* memcpy(void* __restrict dest, const void* __restrict src, size_t n)
{
u8* d = (u8*) dest;
const u8* s = (const u8*) src;
while (n-- > 0) {
*d++ = *s++;
}
return dest;
}
__attribute__((__optimize__("-fno-tree-loop-distribute-patterns"))) //
void* memset(void* s, int c, size_t n)
{
u8* p = (u8*) s;
for (size_t i = 0; i < n; i++) {
p[i] = c;
}
return s;
}
int memcmp(const void* s1, const void* s2, size_t n)
{
const u8* su1 = (const u8*) s1;
const u8* su2 = (const u8*) s2;
size_t i = 0;
for (; i < n && su1[i] == su2[i]; i++) {
}
return i < n ? su1[i] - su2[i] : 0;
}
void* memchr(const void* s, int c, size_t n)
{
const u8* su = (const u8*) s;
for (size_t i = 0; i < n; i++) {
if (su[i] == c) {
// This function is also a const-cast
return (void*) &su[i];
}
}
return NULL;
}
size_t strlen(const char* s)
{
const char* f = s;
while (*s != '\0') {
s++;
}
return s - f;
}
int strcmp(const char* s1, const char* s2)
{
size_t i = 0;
for (; s1[i] == s2[i]; i++) {
if (s1[i] == '\0') {
break;
}
}
return s1[i] - s2[i];
}
int strncmp(const char* s1, const char* s2, size_t n)
{
size_t i = 0;
for (; i < n && s1[i] == s2[i]; i++) {
if (s1[i] == '\0') {
break;
}
}
return i < n ? s1[i] - s2[i] : 0;
}
int strcasecmp(const char* s1, const char* s2)
{
size_t i = 0;
for (; tolower(s1[i]) == tolower(s2[i]); i++) {
if (s1[i] == '\0') {
break;
}
}
return tolower(s1[i]) - tolower(s2[i]);
}
char* strchr(const char* s, int c)
{
while (*s != '\0') {
if (*s == c) {
// This function is also a const-cast
return (char*) s;
}
s++;
}
return NULL;
}
char* strcpy(char* __restrict dst, const char* __restrict src)
{
return (char*) memcpy(dst, src, strlen(src) + 1);
}
char* strncpy(char* __restrict dst, const char* __restrict src, size_t n)
{
if (n == 0) {
return dst;
}
return (char*) memcpy(dst, src, std::min<size_t>(strlen(src) + 1, n));
}
int atexit(void (*__func)(void))
{
return 1;
}
int __cxa_guard_acquire(long long int* guard)
{
return !(*guard);
}
void __cxa_guard_release(long long int* guard)
{
*guard = 1;
}
void __cxa_guard_abort(long long int* guard)
{
*guard = 0;
}
}