diff options
author | Andrew Waterman <waterman@cs.berkeley.edu> | 2016-03-14 16:56:34 -0700 |
---|---|---|
committer | Andrew Waterman <waterman@cs.berkeley.edu> | 2016-03-14 16:56:34 -0700 |
commit | ef1457fa7a3bf3fd58dadd0f30002828b353e573 (patch) | |
tree | d17883848be632baa085628399176c116bb85cd1 /v/string.c | |
parent | b7528b89c8673bf38e5e4ec1e8f037ec2bcbee24 (diff) | |
download | env-ef1457fa7a3bf3fd58dadd0f30002828b353e573.zip env-ef1457fa7a3bf3fd58dadd0f30002828b353e573.tar.gz env-ef1457fa7a3bf3fd58dadd0f30002828b353e573.tar.bz2 |
Support RV32 virtual memory testspriv-1.9
Diffstat (limited to 'v/string.c')
-rw-r--r-- | v/string.c | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/v/string.c b/v/string.c new file mode 100644 index 0000000..46cd989 --- /dev/null +++ b/v/string.c @@ -0,0 +1,99 @@ +#include <string.h> +#include <stdint.h> +#include <ctype.h> + +void* memcpy(void* dest, const void* src, size_t len) +{ + if ((((uintptr_t)dest | (uintptr_t)src | len) & (sizeof(uintptr_t)-1)) == 0) { + const uintptr_t* s = src; + uintptr_t *d = dest; + while (d < (uintptr_t*)(dest + len)) + *d++ = *s++; + } else { + const char* s = src; + char *d = dest; + while (d < (char*)(dest + len)) + *d++ = *s++; + } + return dest; +} + +void* memset(void* dest, int byte, size_t len) +{ + if ((((uintptr_t)dest | len) & (sizeof(uintptr_t)-1)) == 0) { + uintptr_t word = byte & 0xFF; + word |= word << 8; + word |= word << 16; + word |= word << 16 << 16; + + uintptr_t *d = dest; + while (d < (uintptr_t*)(dest + len)) + *d++ = word; + } else { + char *d = dest; + while (d < (char*)(dest + len)) + *d++ = byte; + } + return dest; +} + +size_t strlen(const char *s) +{ + const char *p = s; + while (*p) + p++; + return p - s; +} + +int strcmp(const char* s1, const char* s2) +{ + unsigned char c1, c2; + + do { + c1 = *s1++; + c2 = *s2++; + } while (c1 != 0 && c1 == c2); + + return c1 - c2; +} + +int memcmp(const void* s1, const void* s2, size_t n) +{ + while (n--) { + unsigned char c1 = *(const unsigned char*)s1++; + unsigned char c2 = *(const unsigned char*)s2++; + if (c1 != c2) + return c1 - c2; + } + + return 0; +} + +char* strcpy(char* dest, const char* src) +{ + char* d = dest; + while ((*d++ = *src++)) + ; + return dest; +} + +long atol(const char* str) +{ + long res = 0; + int sign = 0; + + while (*str == ' ') + str++; + + if (*str == '-' || *str == '+') { + sign = *str == '-'; + str++; + } + + while (*str) { + res *= 10; + res += *str++ - '0'; + } + + return sign ? -res : res; +} |