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
70#ifndef NULL
71#define NULL 0
72#endif
73
74#ifndef M_PI
75#define M_PI 3.14159265358979
76#endif
77
79
80#if NEED_MINILIBC
81
82static INLINE void libc_memset(void *dest, int value, int count) {
83 if (!count) 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) return;
95 __asm {
96 cld
97 mov esi, src
98 mov edi, dest
99 mov ecx, count
100 rep movsb
101 }
102}
103
104#define libc_memmove libc_memcpy
105
106static INLINE void* libc_malloc(int size) {
107 return (void*)LocalAlloc(LMEM_FIXED, size);
108}
109
110static INLINE void* libc_calloc(int size, int nmemb) {
111 return (void*)LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, size * nmemb);
112}
113
114static INLINE void* libc_realloc(void* old, int size) {
115 int oldsize = (int)LocalSize((HLOCAL)old);
116 void *mem;
117 if (size <= oldsize) return old;
118 mem = LocalAlloc(LMEM_FIXED, size);
119 libc_memcpy(mem, old, oldsize);
120 LocalFree((HLOCAL)old);
121 return mem;
122}
123
124static INLINE void libc_free(void *mem) {
125 LocalFree((HLOCAL)mem);
126}
127
128static INLINE double libc_frexp(double x, int *e) {
129 double res = -9999.999;
130 unsigned __int64 i = *(unsigned __int64*)(&x);
131 if (!(i & 0x7F00000000000000UL)) {
132 *e = 0;
133 return x;
134 }
135 *e = ((i << 1) >> 53) - 1022;
136 i &= 0x800FFFFFFFFFFFFFUL;
137 i |= 0x3FF0000000000000UL;
138 return *(double*)(&i) * 0.5;
139}
140
141static INLINE double __declspec(naked) libc_exp(double x) {
142 __asm {
143 fldl2e
144 fld qword ptr[esp + 4]
145 fmul
146 fst st(1)
147 frndint
148 fxch
149 fsub st(0), st(1)
150 f2xm1
151 fld1
152 fadd
153 fscale
154 ret
155 }
156}
157
158
159static INLINE double __declspec(naked) libc_pow(double b, double e) {
160 __asm {
161 fld qword ptr[esp + 12]
162 fld qword ptr[esp + 4]
163 fyl2x
164 // following is a copy of libc_exp:
165 fst st(1)
166 frndint
167 fxch
168 fsub st(0), st(1)
169 f2xm1
170 fld1
171 fadd
172 fscale
173 ret
174 }
175}
176
177
178
179#else // NEED_MINILIBC == 0
180
181#define libc_malloc malloc
182#define libc_calloc calloc
183#define libc_realloc realloc
184#define libc_free free
185
186#define libc_memset memset
187#define libc_memcpy memcpy
188#define libc_memmove memmove
189
190#define libc_frexp frexp
191#define libc_exp exp
192#define libc_pow pow
193
194#endif // NEED_MINILIBC
195
196#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
__declspec(align(2)) typedef struct _icmp_head_
Definition icmp.h:37
#define INLINE
Definition libc.h:24
#define libc_frexp
Definition libc.h:190
#define libc_calloc
Definition libc.h:182
#define libc_realloc
Definition libc.h:183
#define libc_memset
Definition libc.h:186
#define libc_malloc
Definition libc.h:181
#define libc_exp
Definition libc.h:191
#define libc_free
Definition libc.h:184
#define libc_pow
Definition libc.h:192
#define libc_memcpy
Definition libc.h:187
int x
Definition term.cpp:49