XenevaOS
Loading...
Searching...
No Matches
libc.h
Go to the documentation of this file.
1// a libc replacement (more or less) for the Microsoft Visual C compiler
2// this file is public domain -- do with it whatever you want!
3#ifndef __LIBC_H_INCLUDED__
4#define __LIBC_H_INCLUDED__
5
6// check if minilibc is required
7#ifndef NEED_MINILIBC
8#ifndef NOLIBC
9#define NEED_MINILIBC 0
10#else
11#define NEED_MINILIBC 1
12#endif
13#endif
14
15#ifdef _MSC_VER
16#define INLINE __forceinline
17#define FASTCALL __fastcall
18#ifdef NOLIBC
19#ifdef MAIN_PROGRAM
20int _fltused = 0;
21#endif
22#endif
23#else
24#define INLINE inline
25#define FASTCALL __attribute__((fastcall))
26#include <stdint.h>
27#endif
28
29#undef WIN32
30#undef _WIN32
31
32#ifdef _WIN32
33#ifndef WIN32
34#define WIN32
35#endif
36#endif
37#ifdef WIN32
38#include <windows.h>
39#endif
40
41#if !NEED_MINILIBC
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#endif
46#include <math.h>
47
48#undef _MSC_VER
49
50#ifdef _NO_STDINT
51#ifndef __int8_t_defined
52#define __int8_t_defined
53typedef unsigned char uint8_t;
54typedef signed char int8_t;
55typedef unsigned short uint16_t;
56typedef signed short int16_t;
57typedef unsigned int uint32_t;
58typedef signed int int32_t;
59#ifdef _MSC_VER
60typedef unsigned __int64 uint64_t;
61typedef signed __int64 int64_t;
62#else
63typedef unsigned long long uint64_t;
64typedef signed long long int64_t;
65#endif
66#endif
67#endif
68
69#ifndef NULL
70#define NULL 0
71#endif
72
73#ifndef M_PI
74#define M_PI 3.14159265358979
75#endif
76
78
79#if NEED_MINILIBC
80
81static INLINE void libc_memset(void* dest, int value, int count) {
82 if (!count)
83 return;
84 __asm {
85 cld
86 mov edi, dest
87 mov eax, value
88 mov ecx, count
89 rep stosb
90 }
91}
92
93static INLINE void libc_memcpy(void* dest, const void* src, int count) {
94 if (!count)
95 return;
96 __asm {
97 cld
98 mov esi, src
99 mov edi, dest
100 mov ecx, count
101 rep movsb
102 }
103}
104
105#define libc_memmove libc_memcpy
106
107static INLINE void* libc_malloc(int size) {
108 return (void*)LocalAlloc(LMEM_FIXED, size);
109}
110
111static INLINE void* libc_calloc(int size, int nmemb) {
112 return (void*)LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, size * nmemb);
113}
114
115static INLINE void* libc_realloc(void* old, int size) {
116 int oldsize = (int)LocalSize((HLOCAL)old);
117 void* mem;
118 if (size <= oldsize)
119 return old;
120 mem = LocalAlloc(LMEM_FIXED, size);
121 libc_memcpy(mem, old, oldsize);
122 LocalFree((HLOCAL)old);
123 return mem;
124}
125
126static INLINE void libc_free(void* mem) {
127 LocalFree((HLOCAL)mem);
128}
129
130static INLINE double libc_frexp(double x, int* e) {
131 double res = -9999.999;
132 unsigned __int64 i = *(unsigned __int64*)(&x);
133 if (!(i & 0x7F00000000000000UL)) {
134 *e = 0;
135 return x;
136 }
137 *e = ((i << 1) >> 53) - 1022;
138 i &= 0x800FFFFFFFFFFFFFUL;
139 i |= 0x3FF0000000000000UL;
140 return *(double*)(&i) * 0.5;
141}
142
143static INLINE double __declspec(naked) libc_exp(double x) {
144 __asm {
145 fldl2e
146 fld qword ptr[esp + 4]
147 fmul
148 fst st(1)
149 frndint
150 fxch
151 fsub st(0), st(1)
152 f2xm1
153 fld1
154 fadd
155 fscale
156 ret
157 }
158}
159
160static INLINE double __declspec(naked) libc_pow(double b, double e) {
161 __asm {
162 fld qword ptr[esp + 12]
163 fld qword ptr[esp + 4]
164 fyl2x
165 // following is a copy of libc_exp:
166 fst st(1)
167 frndint
168 fxch
169 fsub st(0), st(1)
170 f2xm1
171 fld1
172 fadd
173 fscale
174 ret
175 }
176}
177
178#else // NEED_MINILIBC == 0
179
180#define libc_malloc malloc
181#define libc_calloc calloc
182#define libc_realloc realloc
183#define libc_free free
184
185#define libc_memset memset
186#define libc_memcpy memcpy
187#define libc_memmove memmove
188
189#define libc_frexp frexp
190#define libc_exp exp
191#define libc_pow pow
192
193#endif // NEED_MINILIBC
194
195#endif //__LIBC_H_INCLUDED__
int _fltused
Definition main.cpp:136
int int32_t
Definition acefiex.h:160
unsigned int uint32_t
Definition acefiex.h:163
COMPILER_DEPENDENT_INT64 int64_t
Definition acefiex.h:164
short int int16_t
Definition acefiex.h:159
unsigned char uint8_t
Definition acefiex.h:161
COMPILER_DEPENDENT_UINT64 uint64_t
Definition acefiex.h:165
unsigned short int uint16_t
Definition acefiex.h:162
signed char int8_t
Definition acefiex.h:158
#define INLINE
Definition libc.h:24
#define libc_frexp
Definition libc.h:189
#define libc_calloc
Definition libc.h:181
#define libc_realloc
Definition libc.h:182
#define libc_memset
Definition libc.h:185
#define libc_malloc
Definition libc.h:180
#define libc_exp
Definition libc.h:190
#define libc_free
Definition libc.h:183
#define libc_pow
Definition libc.h:191
#define libc_memcpy
Definition libc.h:186
__declspec(align(4)) static gdt_entry gdt[GDT_ENTRIES]