Skip to content

Instantly share code, notes, and snippets.

@take-cheeze
Created April 26, 2017 01:18
Show Gist options
  • Save take-cheeze/62f9984354aa78fc5704c702b6d989d2 to your computer and use it in GitHub Desktop.
Save take-cheeze/62f9984354aa78fc5704c702b6d989d2 to your computer and use it in GitHub Desktop.
13 files changed, 105 insertions(+), 13 deletions(-)
include/core/string.h | 40 ++++++++++++++++++++++++++++++----------
include/core/types.h | 2 ++
include/ctype.h | 0
include/errno.h | 6 ++++++
include/float.h | 10 ++++++++++
include/inttypes.h | 2 +-
include/math.h | 17 +++++++++++++++++
include/setjmp.h | 8 ++++++++
include/stdarg.h | 1 +
include/stddef.h | 0
include/stdint.h | 19 +++++++++++++++++--
include/stdlib.h | 12 ++++++++++++
include/string.h | 1 +
modified include/core/string.h
@@ -46,9 +46,10 @@ memset_slow (void *addr, int val, int len)
}
static inline void *
-memcpy_slow (void *dest, void *src, int len)
+memcpy_slow (void *dest, void const *src, int len)
{
- char *p, *q;
+ char *p;
+ char const *q;
p = dest;
q = src;
@@ -58,7 +59,7 @@ memcpy_slow (void *dest, void *src, int len)
}
static inline int
-strcmp_slow (char *s1, char *s2)
+strcmp_slow (char const *s1, char const *s2)
{
int r, c1, c2;
@@ -71,10 +72,10 @@ strcmp_slow (char *s1, char *s2)
}
static inline int
-memcmp_slow (void *p1, void *p2, int len)
+memcmp_slow (void const *p1, void const *p2, int len)
{
int r, i;
- char *q1, *q2;
+ char const *q1, *q2;
q1 = p1;
q2 = p2;
@@ -84,7 +85,7 @@ memcmp_slow (void *p1, void *p2, int len)
}
static inline int
-strlen_slow (char *p)
+strlen_slow (char const *p)
{
int len = 0;
@@ -93,18 +94,30 @@ strlen_slow (char *p)
return len;
}
+static inline void*
+memchr_slow(const void *ptr, int ch, size_t count)
+{
+ char const *p = ptr, *end = p + count;
+ for (p = ptr; p < end; ++p) {
+ if (*p == ch) { return (void*)p; }
+ }
+ return NULL;
+}
+
#ifdef USE_BUILTIN_STRING
# define memset(addr, val, len) memset_builtin (addr, val, len)
# define memcpy(dest, src, len) memcpy_builtin (dest, src, len)
# define strcmp(s1, s2) strcmp_builtin (s1, s2)
# define memcmp(p1, p2, len) memcmp_builtin (p1, p2, len)
# define strlen(p) strlen_builtin (p)
+# define memchr(p, c, count) memchr_builtin (p, c, count)
#else /* USE_BUILTIN_STRING */
# define memset(addr, val, len) memset_slow (addr, val, len)
# define memcpy(dest, src, len) memcpy_slow (dest, src, len)
# define strcmp(s1, s2) strcmp_slow (s1, s2)
# define memcmp(p1, p2, len) memcmp_slow (p1, p2, len)
# define strlen(p) strlen_slow (p)
+# define memchr(p, c, count) memchr_slow (p, c, count)
#endif /* USE_BUILTIN_STRING */
#ifdef USE_BUILTIN_STRING
@@ -115,28 +128,35 @@ memset_builtin (void *addr, int val, int len)
}
static inline void *
-memcpy_builtin (void *dest, void *src, int len)
+memcpy_builtin (void *dest, void const *src, int len)
{
return __builtin_memcpy (dest, src, len);
}
static inline int
-strcmp_builtin (char *s1, char *s2)
+strcmp_builtin (char const *s1, char const *s2)
{
return __builtin_strcmp (s1, s2);
}
static inline int
-memcmp_builtin (void *p1, void *p2, int len)
+memcmp_builtin (void const *p1, void const *p2, int len)
{
return __builtin_memcmp (p1, p2, len);
}
static inline int
-strlen_builtin (char *p)
+strlen_builtin (char const *p)
{
return __builtin_strlen (p);
}
+
+static inline void*
+memchr_builtin (void const *p, int ch, int count)
+{
+ return __builtin_memchr (p, ch, count);
+}
+
#endif /* USE_BUILTIN_STRING */
#endif
modified include/core/types.h
@@ -30,7 +30,9 @@
#ifndef __CORE_TYPES_H
#define __CORE_TYPES_H
+#ifndef NULL
#define NULL ((void *)0)
+#endif
typedef signed char i8;
typedef signed short int i16;
new file include/ctype.h
new file include/errno.h
@@ -0,0 +1,6 @@
+#ifndef _BITVISOR_ERRNO_H
+#define _BITVISOR_ERRNO_H
+
+#define errno 0
+
+#endif
new file include/float.h
@@ -0,0 +1,10 @@
+#ifndef _BITVISOR_FLOAT_H
+#define _BITVISOR_FLOAT_H
+
+#define DBL_DIG 15
+
+#define LDBL_MANT_DIG 64
+#define LDBL_MAX_EXP 16384
+#define LDBL_EPSILON 1.08420217248550443e-19
+
+#endif
modified include/inttypes.h
@@ -1,7 +1,7 @@
#ifndef _INTTYPES_H
#define _INTTYPES_H
-#include <features.h>
+//#include <features.h>
#include <stdint.h>
#endif
new file include/math.h
@@ -0,0 +1,17 @@
+#ifndef _BITVISOR_MATH_H
+#define _BITVISOR_MATH_H
+
+#define signbit(a) __builtin_signbit(a)
+#define frexp(a, b) __builtin_frexp(a, b)
+#define isfinite(a) __builtin_isfinite(a)
+#define pow(a, b) __builtin_pow(a, b)
+#define isnan(a) __builtin_isnan(a)
+#define isinf(a) __builtin_isinf(a)
+#define fmod(a, b) __builtin_fmod(a, b)
+#define floor(a) __builtin_floor(a)
+#define ceil(a) __builtin_ceil(a)
+
+#define NAN __builtin_nan("")
+#define INFINITY __builtin_huge_valf()
+
+#endif
new file include/setjmp.h
@@ -0,0 +1,8 @@
+#ifndef _BITVISOR_SETJMP_H
+#define _BITVISOR_SETJMP_H
+
+typedef int jmp_buf[6];
+#define setjmp(env) __builtin_setjmp(env)
+#define longjmp(env, val) __builtin_longjmp(env, val)
+
+#endif
new file include/stdarg.h
@@ -0,0 +1 @@
+#include <core/stdarg.h>
new file include/stddef.h
modified include/stdint.h
@@ -27,7 +27,15 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
+#ifndef _BITVISOR_STDINT_H
+#define _BITVISOR_STDINT_H
+
+#ifndef NULL
+#define NULL ((void*)0)
+#endif
+
typedef long int intptr_t;
+typedef unsigned long int uintptr_t;
typedef unsigned long long int uint64_t;
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
@@ -37,13 +45,16 @@ typedef int int32_t;
typedef short int16_t;
typedef char int8_t;
-typedef long int intptr_t;
-typedef unsigned long int uintptr_t;
+typedef unsigned long int size_t;
+typedef intptr_t ptrdiff_t;
#define INT64_MAX 0x7ffffffffffffffff
#define INT64_MIN -0x7fffffffffffffff - 1
+#define INT32_MAX 2147483647
+#define INT32_MIN -2147483648
+
#if __WORDSIZE == 64
# define SIZE_MAX (18446744073709551615UL)
#else
@@ -54,6 +65,7 @@ typedef unsigned long int uintptr_t;
# endif
#endif
+#if !defined(__INT64_C) && !defined(__UINT64_C)
#if __WORDSIZE == 64
# define __INT64_C(c) c
# define __UINT64_C(c) c
@@ -61,7 +73,10 @@ typedef unsigned long int uintptr_t;
# define __INT64_C(c) c
# define __UINT64_C(c) c
#endif
+#endif
#define UINT16_MAX (65535)
#define UINT64_MAX ( __UINT64_C(18446744073709551615))
+
+#endif // _BITVISOR_STDINT_H
modified include/stdlib.h
@@ -27,6 +27,12 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
+#ifndef _BITVISOR_STDLIB_H
+#define _BITVISOR_STDLIB_H
+
+#include <stdint.h>
+#include <core/mm.h>
+
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
//#define __THROW NULL
@@ -34,5 +40,11 @@
extern void abort(void) __attribute__ ((__noreturn__));
extern void exit(int __status) __attribute__ ((__noreturn__));
+#define malloc(s) alloc(m);
+#define realloc(p, s) realloc(p, s)
+#define free(p) free(p)
+
+extern double strtod(char const *s, char **s_end);
+#endif
new file include/string.h
@@ -0,0 +1 @@
+#include <core/string.h>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment