Reduct  v4.0.5-1-g4851deb
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#include <stddef.h>
5#include <stdint.h>
6
7struct reduct;
8struct reduct_list;
9struct reduct_builder;
10struct reduct_list;
11
12#if defined(_WIN32) || defined(__CYGWIN__)
13#ifdef REDUCT_BUILD_LIB
14#define REDUCT_API __declspec(dllexport)
15#elif defined(REDUCT_USE_LIB)
16#define REDUCT_API __declspec(dllimport)
17#else
18#define REDUCT_API
19#endif
20#else
21#if __GNUC__ >= 4
22#define REDUCT_API __attribute__((visibility("default")))
23#else
24#define REDUCT_API
25#endif
26#endif
27
28#ifdef _WIN32
29#include <windows.h>
30typedef HMODULE reduct_lib_t;
31#define REDUCT_LIB_OPEN(_path) LoadLibraryA(_path)
32#define REDUCT_LIB_CLOSE(_lib) FreeLibrary(_lib)
33#define REDUCT_LIB_SYM(_lib, _name) GetProcAddress(_lib, _name)
34#define REDUCT_LIB_ERROR() "Windows Error"
35#else
36#include <dlfcn.h>
37#include <unistd.h>
38typedef void* reduct_lib_t;
39#define REDUCT_LIB_OPEN(_path) dlopen(_path, RTLD_NOW | RTLD_GLOBAL)
40#define REDUCT_LIB_CLOSE(_lib) dlclose(_lib)
41#define REDUCT_LIB_SYM(_lib, _name) dlsym(_lib, _name)
42#define REDUCT_LIB_ERROR() dlerror()
43#endif
44
45#if defined(__GNUC__) || defined(__clang__)
46#define REDUCT_LIKELY(_x) __builtin_expect(!!(_x), 1)
47#define REDUCT_UNLIKELY(_x) __builtin_expect(!!(_x), 0)
48#define REDUCT_NORETURN __attribute__((noreturn))
49#define REDUCT_ALWAYS_INLINE __attribute__((always_inline))
50#define REDUCT_ALIGNED(_x) __attribute__((aligned(_x)))
51#elif defined(_MSC_VER)
52#define REDUCT_LIKELY(_x) (_x)
53#define REDUCT_UNLIKELY(_x) (_x)
54#define REDUCT_NORETURN __declspec(noreturn)
55#define REDUCT_ALWAYS_INLINE __forceinline
56#define REDUCT_ALIGNED(_x) __declspec(align(_x))
57#else
58#define REDUCT_LIKELY(_x) (_x)
59#define REDUCT_UNLIKELY(_x) (_x)
60#define REDUCT_NORETURN
61#define REDUCT_ALWAYS_INLINE
62#endif
63
64#define REDUCT_MIN(_a, _b) ((_a) < (_b) ? (_a) : (_b))
65#define REDUCT_MAX(_a, _b) ((_a) > (_b) ? (_a) : (_b))
66
67#define REDUCT_UNUSED(_x) ((void)(_x))
68
69/**
70 * @brief PI constant.
71 */
72#define REDUCT_PI 3.14159265358979323846
73
74/**
75 * @brief E constant.
76 */
77#define REDUCT_E 2.7182818284590452354
78
79/**
80 * @brief INFINITY constant.
81 */
82#define REDUCT_INF \
83 (((union { \
84 uint64_t u; \
85 double f; \
86 }){0x7FF0000000000000ULL}) \
87 .f)
88
89/**
90 * @brief NAN constant.
91 */
92#define REDUCT_NAN \
93 (((union { \
94 uint64_t u; \
95 double f; \
96 }){0x7FF8000000000000ULL}) \
97 .f)
98
99/**
100 * @brief Maximum path length for Reduct.
101 */
102#define REDUCT_PATH_MAX 1024
103
104/**
105 * @brief Container of macro.
106 *
107 * Used to get the pointer to a structure from a pointer to one of its members.
108 *
109 * @param _ptr The pointer to the member.
110 * @param _type The type of the structure.
111 * @param _member The name of the member.
112 */
113#define REDUCT_CONTAINER_OF(_ptr, _type, _member) ((_type*)((char*)(_ptr) - offsetof(_type, _member)))
114
115/**
116 * @brief Handle type.
117 */
118typedef struct
119{
120 uint64_t _value;
122
123/**
124 * @brief Native function pointer type.
125 */
126typedef reduct_handle_t (*reduct_native_fn)(struct reduct* reduct, size_t argc, reduct_handle_t* argv);
127
128/**
129 * @brief Intrinsic handler function type.
130 */
131typedef struct reduct_rvsdg_origin* (
132 *reduct_native_intrinsic_fn)(struct reduct_builder* builder, struct reduct_list* expr);
133
134/**
135 * @brief Module initialization function type.
136 */
137typedef reduct_handle_t (*reduct_module_init_fn)(struct reduct* reduct);
138
139#define REDUCT_LIB_ENTRY "reduct_module_init" ///< The name of the entry symbol for a Reduct module.
140
141/**
142 * @brief Identifies a `reduct_input_t` within a Reduct structure.
143 *
144 * Avoid the need to store a `reduct_input_t*` within a `reduct_item_t` saving space.
145 *
146 */
147typedef uint16_t reduct_input_id_t;
148
149/**
150 * @brief Invalid handle value.
151 */
152#define REDUCT_INPUT_ID_NONE ((reduct_input_id_t) - 1)
153
154#define REDUCT_ALIGNMENT 64 ///< The memory alignment for items.
155
156#define REDUCT_ROUND_UP(_val, _align) (((_val) + (_align) - 1) & ~((_align) - 1))
157
158#endif
uint16_t reduct_input_id_t
Identifies a reduct_input_t within a Reduct structure.
Definition defs.h:147
reduct_handle_t(* reduct_native_fn)(struct reduct *reduct, size_t argc, reduct_handle_t *argv)
Native function pointer type.
Definition defs.h:126
struct reduct_rvsdg_origin *(* reduct_native_intrinsic_fn)(struct reduct_builder *builder, struct reduct_list *expr)
Intrinsic handler function type.
Definition defs.h:132
void * reduct_lib_t
Definition defs.h:38
reduct_handle_t(* reduct_module_init_fn)(struct reduct *reduct)
Module initialization function type.
Definition defs.h:137
Handle type.
Definition defs.h:119
uint64_t _value
Definition defs.h:120