2019-06-07 20:29:34 +03:00
|
|
|
#include <AK/Assertions.h>
|
2018-10-17 13:07:39 +03:00
|
|
|
#include <AK/Types.h>
|
2019-06-07 20:29:34 +03:00
|
|
|
#include <Kernel/kmalloc.h>
|
2018-10-16 12:01:38 +03:00
|
|
|
|
2018-11-09 14:20:44 +03:00
|
|
|
extern "C" {
|
|
|
|
|
2019-02-22 12:23:06 +03:00
|
|
|
void* memcpy(void* dest_ptr, const void* src_ptr, size_t n)
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
2019-02-22 12:23:06 +03:00
|
|
|
size_t dest = (size_t)dest_ptr;
|
|
|
|
size_t src = (size_t)src_ptr;
|
2019-01-13 01:33:13 +03:00
|
|
|
// FIXME: Support starting at an unaligned address.
|
|
|
|
if (!(dest & 0x3) && !(src & 0x3) && n >= 12) {
|
2019-02-22 12:23:06 +03:00
|
|
|
size_t size_ts = n / sizeof(size_t);
|
2019-01-12 20:13:21 +03:00
|
|
|
asm volatile(
|
|
|
|
"rep movsl\n"
|
|
|
|
: "=S"(src), "=D"(dest)
|
2019-02-22 12:23:06 +03:00
|
|
|
: "S"(src), "D"(dest), "c"(size_ts)
|
2019-06-07 12:43:58 +03:00
|
|
|
: "memory");
|
2019-02-22 12:23:06 +03:00
|
|
|
n -= size_ts * sizeof(size_t);
|
2019-01-12 20:13:21 +03:00
|
|
|
if (n == 0)
|
2019-02-22 12:23:06 +03:00
|
|
|
return dest_ptr;
|
2019-01-12 20:13:21 +03:00
|
|
|
}
|
|
|
|
asm volatile(
|
2019-06-07 12:43:58 +03:00
|
|
|
"rep movsb\n" ::"S"(src), "D"(dest), "c"(n)
|
|
|
|
: "memory");
|
2019-02-22 12:23:06 +03:00
|
|
|
return dest_ptr;
|
2018-10-16 12:01:38 +03:00
|
|
|
}
|
|
|
|
|
2019-03-24 00:03:17 +03:00
|
|
|
void* memmove(void* dest, const void* src, size_t n)
|
|
|
|
{
|
|
|
|
if (dest < src)
|
|
|
|
return memcpy(dest, src, n);
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u8* pd = (u8*)dest;
|
|
|
|
const u8* ps = (const u8*)src;
|
2019-03-24 00:03:17 +03:00
|
|
|
for (pd += n, ps += n; n--;)
|
|
|
|
*--pd = *--ps;
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2019-06-07 12:43:58 +03:00
|
|
|
char* strcpy(char* dest, const char* src)
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
2019-02-22 12:23:06 +03:00
|
|
|
auto* dest_ptr = dest;
|
|
|
|
auto* src_ptr = src;
|
2019-06-07 12:43:58 +03:00
|
|
|
while ((*dest_ptr++ = *src_ptr++) != '\0')
|
|
|
|
;
|
2019-02-22 12:23:06 +03:00
|
|
|
return dest;
|
2018-10-16 12:01:38 +03:00
|
|
|
}
|
|
|
|
|
2019-02-07 12:29:26 +03:00
|
|
|
char* strncpy(char* dest, const char* src, size_t n)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < n && src[i] != '\0'; ++i)
|
|
|
|
dest[i] = src[i];
|
2019-06-07 12:43:58 +03:00
|
|
|
for (; i < n; ++i)
|
2019-02-07 12:29:26 +03:00
|
|
|
dest[i] = '\0';
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2019-02-22 12:23:06 +03:00
|
|
|
void* memset(void* dest_ptr, int c, size_t n)
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
2019-02-22 12:23:06 +03:00
|
|
|
size_t dest = (size_t)dest_ptr;
|
2019-01-13 01:33:13 +03:00
|
|
|
// FIXME: Support starting at an unaligned address.
|
|
|
|
if (!(dest & 0x3) && n >= 12) {
|
2019-02-22 12:23:06 +03:00
|
|
|
size_t size_ts = n / sizeof(size_t);
|
2019-07-03 22:17:35 +03:00
|
|
|
size_t expanded_c = (u8)c;
|
2019-01-15 10:14:08 +03:00
|
|
|
expanded_c |= expanded_c << 8;
|
|
|
|
expanded_c |= expanded_c << 16;
|
2019-01-12 20:13:21 +03:00
|
|
|
asm volatile(
|
|
|
|
"rep stosl\n"
|
|
|
|
: "=D"(dest)
|
2019-02-22 12:23:06 +03:00
|
|
|
: "D"(dest), "c"(size_ts), "a"(expanded_c)
|
2019-06-07 12:43:58 +03:00
|
|
|
: "memory");
|
2019-02-22 12:23:06 +03:00
|
|
|
n -= size_ts * sizeof(size_t);
|
2019-01-12 20:13:21 +03:00
|
|
|
if (n == 0)
|
|
|
|
return dest_ptr;
|
|
|
|
}
|
2019-01-12 04:19:02 +03:00
|
|
|
asm volatile(
|
|
|
|
"rep stosb\n"
|
2019-06-07 12:43:58 +03:00
|
|
|
: "=D"(dest), "=c"(n)
|
|
|
|
: "0"(dest), "1"(n), "a"(c)
|
|
|
|
: "memory");
|
2019-01-12 04:19:02 +03:00
|
|
|
return dest_ptr;
|
2018-10-16 12:01:38 +03:00
|
|
|
}
|
|
|
|
|
2018-11-01 00:43:49 +03:00
|
|
|
char* strrchr(const char* str, int ch)
|
|
|
|
{
|
2019-06-07 12:43:58 +03:00
|
|
|
char* last = nullptr;
|
2018-11-01 00:43:49 +03:00
|
|
|
char c;
|
|
|
|
for (; (c = *str); ++str) {
|
|
|
|
if (c == ch)
|
2018-11-09 12:03:21 +03:00
|
|
|
last = const_cast<char*>(str);
|
2018-11-01 00:43:49 +03:00
|
|
|
}
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
2019-02-22 12:23:06 +03:00
|
|
|
size_t strlen(const char* str)
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
2019-02-22 12:23:06 +03:00
|
|
|
size_t len = 0;
|
2018-10-16 12:01:38 +03:00
|
|
|
while (*(str++))
|
|
|
|
++len;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2019-08-10 18:01:33 +03:00
|
|
|
size_t strnlen(const char* str, size_t maxlen)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
for (; len < maxlen && *str; str++)
|
|
|
|
len++;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2019-06-07 12:43:58 +03:00
|
|
|
int strcmp(const char* s1, const char* s2)
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
|
|
|
for (; *s1 == *s2; ++s1, ++s2) {
|
|
|
|
if (*s1 == 0)
|
|
|
|
return 0;
|
|
|
|
}
|
2019-07-03 22:17:35 +03:00
|
|
|
return *(const u8*)s1 < *(const u8*)s2 ? -1 : 1;
|
2018-10-16 12:01:38 +03:00
|
|
|
}
|
|
|
|
|
2019-06-07 12:43:58 +03:00
|
|
|
char* strdup(const char* str)
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
2019-02-03 14:33:11 +03:00
|
|
|
size_t len = strlen(str);
|
|
|
|
char* new_str = (char*)kmalloc(len + 1);
|
|
|
|
strcpy(new_str, str);
|
|
|
|
return new_str;
|
2018-10-16 12:01:38 +03:00
|
|
|
}
|
|
|
|
|
2018-10-17 11:55:43 +03:00
|
|
|
int memcmp(const void* v1, const void* v2, size_t n)
|
|
|
|
{
|
2019-07-03 22:17:35 +03:00
|
|
|
auto* s1 = (const u8*)v1;
|
|
|
|
auto* s2 = (const u8*)v2;
|
2018-10-17 13:07:39 +03:00
|
|
|
while (n-- > 0) {
|
|
|
|
if (*s1++ != *s2++)
|
|
|
|
return s1[-1] < s2[-1] ? -1 : 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2018-10-17 11:55:43 +03:00
|
|
|
}
|
|
|
|
|
2019-02-15 14:30:48 +03:00
|
|
|
[[noreturn]] void __cxa_pure_virtual()
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2019-07-29 12:58:44 +03:00
|
|
|
|
|
|
|
static inline uint32_t divq(uint64_t n, uint32_t d)
|
|
|
|
{
|
|
|
|
uint32_t n1 = n >> 32;
|
|
|
|
uint32_t n0 = n;
|
|
|
|
uint32_t q;
|
|
|
|
uint32_t r;
|
|
|
|
asm volatile("divl %4"
|
|
|
|
: "=d"(r), "=a"(q)
|
|
|
|
: "0"(n1), "1"(n0), "rm"(d));
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t unsigned_divide64(uint64_t n, uint64_t d)
|
|
|
|
{
|
|
|
|
if ((d >> 32) == 0) {
|
|
|
|
uint64_t b = 1ULL << 32;
|
|
|
|
uint32_t n1 = n >> 32;
|
|
|
|
uint32_t n0 = n;
|
|
|
|
uint32_t d0 = d;
|
|
|
|
return divq(b * (n1 % d0) + n0, d0) + b * (n1 / d0);
|
|
|
|
}
|
|
|
|
if (n < d)
|
|
|
|
return 0;
|
|
|
|
uint32_t d1 = d >> 32u;
|
|
|
|
int s = __builtin_clz(d1);
|
|
|
|
uint64_t q = divq(n >> 1, (d << s) >> 32) >> (31 - s);
|
|
|
|
return n - (q - 1) * d < d ? q - 1 : q;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t unsigned_modulo64(uint64_t n, uint64_t d)
|
|
|
|
{
|
|
|
|
return n - d * unsigned_divide64(n, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t signed_divide64(int64_t n, int64_t d)
|
|
|
|
{
|
|
|
|
uint64_t n_abs = n >= 0 ? (uint64_t)n : -(uint64_t)n;
|
|
|
|
uint64_t d_abs = d >= 0 ? (uint64_t)d : -(uint64_t)d;
|
|
|
|
uint64_t q_abs = unsigned_divide64(n_abs, d_abs);
|
|
|
|
return (n < 0) == (d < 0) ? (int64_t)q_abs : -(int64_t)q_abs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t signed_modulo64(int64_t n, int64_t d)
|
|
|
|
{
|
|
|
|
return n - d * signed_divide64(n, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t __divdi3(int64_t n, int64_t d)
|
|
|
|
{
|
|
|
|
return signed_divide64(n, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t __moddi3(int64_t n, int64_t d)
|
|
|
|
{
|
|
|
|
return signed_modulo64(n, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t __udivdi3(uint64_t n, uint64_t d)
|
|
|
|
{
|
|
|
|
return unsigned_divide64(n, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t __umoddi3(uint64_t n, uint64_t d)
|
|
|
|
{
|
|
|
|
return unsigned_modulo64(n, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t __udivmoddi4(uint64_t n, uint64_t d, uint64_t* r)
|
|
|
|
{
|
|
|
|
uint64_t q = 0;
|
|
|
|
uint64_t qbit = 1;
|
|
|
|
|
|
|
|
if (!d)
|
|
|
|
return 1 / ((unsigned)d);
|
|
|
|
|
|
|
|
while ((int64_t)d >= 0) {
|
|
|
|
d <<= 1;
|
|
|
|
qbit <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (qbit) {
|
|
|
|
if (d <= n) {
|
|
|
|
n -= d;
|
|
|
|
q += qbit;
|
|
|
|
}
|
|
|
|
d >>= 1;
|
|
|
|
qbit >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r)
|
|
|
|
*r = n;
|
|
|
|
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2018-11-09 14:20:44 +03:00
|
|
|
}
|