Reduct  v1.0.4-3-gdaf0d70
A functional and immutable language.
Loading...
Searching...
No Matches
defs.h
Go to the documentation of this file.
1#ifndef REDUCT_DEFS_H
2#define REDUCT_DEFS_H 1
3
4#ifdef REDUCT_INLINE
5#define REDUCT_API static inline
6#else
7#define REDUCT_API
8#endif
9
10#ifndef REDUCT_FREESTANDING
11#define _CRT_SECURE_NO_WARNINGS // Make windows stop being annoying
12#include <assert.h>
13#include <math.h>
14#include <setjmp.h>
15#include <stdarg.h>
16#include <stddef.h>
17#include <stdint.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <time.h>
22
23#define REDUCT_NULL NULL
24
25#define REDUCT_ASSERT(_cond) assert(_cond)
26
27#define REDUCT_MALLOC(_size) malloc(_size)
28#define REDUCT_CALLOC(_nmemb, _size) calloc(_nmemb, _size)
29#define REDUCT_REALLOC(_ptr, _size) realloc(_ptr, _size)
30#define REDUCT_FREE(_ptr) free(_ptr)
31
32#define REDUCT_MEMSET(_ptr, _val, _size) memset(_ptr, _val, _size)
33#define REDUCT_MEMCPY(_dest, _src, _size) memcpy(_dest, _src, _size)
34#define REDUCT_MEMCMP(_s1, _s2, _size) memcmp(_s1, _s2, _size)
35
36#define REDUCT_STRNCPY(dest, src, n) strncpy(dest, src, n)
37#define REDUCT_STRCMP(s1, s2) strcmp(s1, s2)
38#define REDUCT_STRLEN(s) strlen(s)
39
40typedef jmp_buf reduct_jmp_buf_t;
41#define REDUCT_SETJMP(_env) setjmp(_env)
42#define REDUCT_LONGJMP(_env, _val) longjmp(_env, _val)
43
44typedef FILE* reduct_file_t;
45#define REDUCT_FOPEN(_path, _mode) fopen(_path, _mode)
46#define REDUCT_FCLOSE(_file) fclose(_file)
47#define REDUCT_FREAD(_ptr, _size, _nmemb, _file) fread(_ptr, _size, _nmemb, _file)
48#define REDUCT_FWRITE(_ptr, _size, _nmemb, _file) fwrite(_ptr, _size, _nmemb, _file)
49#define REDUCT_FGETC(_file) fgetc(_file)
50#define REDUCT_FPRINTF fprintf
51#define REDUCT_SNPRINTF snprintf
52#define REDUCT_VSNPRINTF vsnprintf
53#define REDUCT_STDIN stdin
54#define REDUCT_STDOUT stdout
55#define REDUCT_STDERR stderr
56
57#define REDUCT_TIME() time(REDUCT_NULL)
58#define REDUCT_CLOCK() clock()
59#define REDUCT_GETENV(_name) getenv(_name)
60
61#define REDUCT_FLOOR(_x) floor(_x)
62#define REDUCT_CEIL(_x) ceil(_x)
63#define REDUCT_ROUND(_x) round(_x)
64#define REDUCT_POW(_x, _y) pow(_x, _y)
65#define REDUCT_EXP(_x) exp(_x)
66#define REDUCT_LOG(_x) log(_x)
67#define REDUCT_SQRT(_x) sqrt(_x)
68#define REDUCT_SIN(_x) sin(_x)
69#define REDUCT_COS(_x) cos(_x)
70#define REDUCT_TAN(_x) tan(_x)
71#define REDUCT_ASIN(_x) asin(_x)
72#define REDUCT_ACOS(_x) acos(_x)
73#define REDUCT_ATAN(_x) atan(_x)
74#define REDUCT_ATAN2(_y, _x) atan2(_y, _x)
75#define REDUCT_SINH(_x) sinh(_x)
76#define REDUCT_COSH(_x) cosh(_x)
77#define REDUCT_TANH(_x) tanh(_x)
78#define REDUCT_ASINH(_x) asinh(_x)
79#define REDUCT_ACOSH(_x) acosh(_x)
80#define REDUCT_ATANH(_x) atanh(_x)
81#define REDUCT_FABS(_x) fabs(_x)
82
83#define REDUCT_RAND() rand()
84#define REDUCT_SRAND(_seed) srand(_seed)
85#define REDUCT_RAND_MAX RAND_MAX
86
87#define REDUCT_VA_START(_ap, _last) va_start(_ap, _last)
88#define REDUCT_VA_END(_ap) va_end(_ap)
89#define REDUCT_VA_COPY(_dest, _src) va_copy(_dest, _src)
90typedef va_list reduct_va_list;
91
92typedef int64_t reduct_int64_t;
93typedef uint64_t reduct_uint64_t;
94typedef int32_t reduct_int32_t;
95typedef uint32_t reduct_uint32_t;
96typedef int16_t reduct_int16_t;
97typedef uint16_t reduct_uint16_t;
98typedef int8_t reduct_int8_t;
99typedef uint8_t reduct_uint8_t;
100typedef size_t reduct_size_t;
101typedef int reduct_int_t;
102typedef double reduct_float_t;
103#endif
104
105#if defined(__GNUC__) || defined(__clang__)
106#define REDUCT_LIKELY(_x) __builtin_expect(!!(_x), 1)
107#define REDUCT_UNLIKELY(_x) __builtin_expect(!!(_x), 0)
108#define REDUCT_NORETURN __attribute__((noreturn))
109#define REDUCT_ALWAYS_INLINE __attribute__((always_inline))
110#define REDUCT_HAS_COMPUTED_GOTO
111#elif defined(_MSC_VER)
112#define REDUCT_LIKELY(_x) (_x)
113#define REDUCT_UNLIKELY(_x) (_x)
114#define REDUCT_NORETURN __declspec(noreturn)
115#define REDUCT_ALWAYS_INLINE __forceinline
116#else
117#define REDUCT_LIKELY(_x) (_x)
118#define REDUCT_UNLIKELY(_x) (_x)
119#define REDUCT_NORETURN
120#define REDUCT_ALWAYS_INLINE
121#endif
122
123#define REDUCT_MIN(_a, _b) ((_a) < (_b) ? (_a) : (_b))
124#define REDUCT_MAX(_a, _b) ((_a) > (_b) ? (_a) : (_b))
125
126#define REDUCT_UNUSED(_x) ((void)(_x))
127
128/**
129 * @brief Boolean type.
130 * @enum reduct_bool_t
131 *
132 * Equivalent to bool.
133 */
134typedef enum
135{
137 REDUCT_FALSE = 0
139
140/**
141 * @brief PI constant.
142 */
143#define REDUCT_PI 3.14159265358979323846
144
145/**
146 * @brief E constant.
147 */
148#define REDUCT_E 2.7182818284590452354
149
150/**
151 * @brief INFINITY constant.
152 */
153#define REDUCT_INF \
154 (((union { \
155 reduct_uint64_t u; \
156 reduct_float_t f; \
157 }){0x7FF0000000000000ULL}) \
158 .f)
159
160/**
161 * @brief NAN constant.
162 */
163#define REDUCT_NAN \
164 (((union { \
165 reduct_uint64_t u; \
166 reduct_float_t f; \
167 }){0x7FF8000000000000ULL}) \
168 .f)
169
170/**
171 * @brief Maximum path length for Reduct.
172 */
173#define REDUCT_PATH_MAX 512
174
175/**
176 * @brief Container of macro.
177 *
178 * Used to get the pointer to a structure from a pointer to one of its members.
179 *
180 * @param _ptr The pointer to the member.
181 * @param _type The type of the structure.
182 * @param _member The name of the member.
183 */
184#define REDUCT_CONTAINER_OF(_ptr, _type, _member) ((_type*)((char*)(_ptr) - offsetof(_type, _member)))
185
186/**
187 * @brief Handle type.
188 */
190
191#define REDUCT_STACK_BUFFER_SIZE 256 ///< The size of temporary stack allocated buffers.
192
193/**
194 * @brief Scratch buffer macro.
195 *
196 * Will allocate a buffer on the stack if its small enough, otherwise it will create a heap allocated buffer.
197 *
198 * @param _name The name of the buffer pointer.
199 * @param _size The size of the buffer to allocate.
200 */
201#define REDUCT_SCRATCH_BUFFER(_name, _size) \
202 char _name##Stack[REDUCT_STACK_BUFFER_SIZE]; \
203 char* _name = ((reduct_size_t)(_size) + 1 <= REDUCT_STACK_BUFFER_SIZE) \
204 ? _name##Stack \
205 : (char*)REDUCT_MALLOC((reduct_size_t)(_size) + 1); \
206 if (_name == REDUCT_NULL) \
207 { \
208 REDUCT_ERROR_INTERNAL(reduct, "out of memory"); \
209 }
210
211/**
212 * @brief Scratch buffer free macro.
213 *
214 * Frees a scratch buffer if it was heap allocated.
215 *
216 * @param _name The name of the buffer pointer.
217 */
218#define REDUCT_SCRATCH_BUFFER_FREE(_name) \
219 if (_name != _name##Stack) \
220 { \
221 REDUCT_FREE(_name); \
222 }
223
224#endif
int16_t reduct_int16_t
Definition defs.h:96
size_t reduct_size_t
Definition defs.h:100
va_list reduct_va_list
Definition defs.h:90
reduct_bool_t
Boolean type.
Definition defs.h:135
@ REDUCT_FALSE
Definition defs.h:137
@ REDUCT_TRUE
Definition defs.h:136
int64_t reduct_int64_t
Definition defs.h:92
reduct_uint64_t reduct_handle_t
Handle type.
Definition defs.h:189
int8_t reduct_int8_t
Definition defs.h:98
uint32_t reduct_uint32_t
Definition defs.h:95
FILE * reduct_file_t
Definition defs.h:44
double reduct_float_t
Definition defs.h:102
uint8_t reduct_uint8_t
Definition defs.h:99
jmp_buf reduct_jmp_buf_t
Definition defs.h:40
int32_t reduct_int32_t
Definition defs.h:94
int reduct_int_t
Definition defs.h:101
uint16_t reduct_uint16_t
Definition defs.h:97
uint64_t reduct_uint64_t
Definition defs.h:93