/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
Copyright (c) 2013-2016 Gustav Hartvigsson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stddef.h>
#include <uchar.h>
#include <stdint.h>
#include <inttypes.h>
#include <assert.h>
#include <math.h>

#include "config.h"

/** @file
 * @defgroup Definitions Definitions
 * @addtogroup Definitions
 * @{
 * @brief Declarations of the types and macros that are used extensively
 * throughout libssts.
 */

/**
 * @def S_BEGIN_DECLS
 * For interoperability with C++ we have to have this.
 *
 * Put at the beginning of header files.
 * @see S_END_DECLS
 */

/**
 * @def S_END_DECLS
 *
 * Put at end of header files.
 * @see S_BEGIN_DECLS
 */

#ifdef __cplusplus
  #define S_BEGIN_DECLS extern "C" {
  #define S_END_DECLS }
#else
  #define S_BEGIN_DECLS
  #define S_END_DECLS
#endif /*__cplusplus*/

S_BEGIN_DECLS

/* Make sure that we use utf-32 with the suchar string. */
#define STDC_UTF_32 1

/* ***************************** GENERIC MACROS ***************************** */

/** @def
 */

/**
 * @def S_DEPRECATED
 * Mark function as deprecated.
 */
/**
 * @def S_UNUSED
 * Supress compiler warning that variable is unused.
 */
/**
 * @def S_ALIGNED_8
 * Macro to align memory to 8 bit.
 */
/**
 * @def S_ALIGNED_16
 * Macro to align memory to 16 bit.
 */
#if defined(__GNUC__)
  #define S_DEPRECATED __attribute__((deprecated))
  #define S_UNUSED __attribute__((unused))
  #define S_ALIGNED_8 __attribute__((aligned (8)))
  #define S_ALIGNED_16 __attribute__((aligned (16)))
#elif defined(__MSC_VER)
  #define S_DEPRECATED __declspec(deprecated)
  // TODO, check if this rely works in MSVC.
  #define S_UNUSED __pragma(warning(suppress:4100))
  #define S_ALIGNED_8 __declspec(align(8))
  #define S_ALIGNED_16 __declspec(align(16))
#else
  #define S_DEPRECATED
  #define S_UNUSED
  #define S_ALIGNED_8
  #define S_ALIGNED_16
#endif

/**
 * @def __S_ATOMIC__
 * The stupid atomic value.
 */
#if __STDC_NO_ATOMICS__
  #define __S_ATOMIC__ volatile
#else
  #define __S_ATOMIC__ __Atomic
#endif

/**
 * @def S_EXPORTED
 * Mark function as exported. This makes the function available from outside a
 * library on some platforms.
 *
 * @see S_HIDDEN
 */
/**
 * @def S_HIDDEN
 * Mark function as hidden. This hides a function so it is not exposed to the
 * outside of the library.
 *
 * @see S_EXPORTED
 */

/*
 * Copied from: https://gcc.gnu.org/wiki/Visibility .. with modifications.
 */
#if defined _WIN32 || defined __CYGWIN__
  #ifdef BUILDING_DLL
    #ifdef __GNUC__
      #define S_EXPORTED __attribute__ ((dllexport))
    #else
      #define S_EXPORTED __declspec(dllexport) // Note: actually gcc seems to also supports this syntax.
    #endif
  #else
    #ifdef __GNUC__
      #define S_EXPORTED __attribute__ ((dllimport))
    #else
      #define S_EXPORTED __declspec(dllimport) // Note: actually gcc seems to also supports this syntax.
    #endif
  #endif
  #define S_HIDDEN
#else
  #if __GNUC__ >= 4
    #define S_EXPORTED __attribute__ ((visibility ("default")))
    #define S_HIDDEN  __attribute__ ((visibility ("hidden")))
  #else
    #define S_EXPORTED
    #define S_HIDDEN
  #endif
#endif

/**
 * FALSE has the absolute value of 0, it is used in as a way to represent a
 * false value. A "not" value.
 *
 * @see sboolean
 */
#define FALSE 0

 /**
  * TRUE represents a value that is true. A value that is "not false".
  *
  * @see sboolean
  */
#define TRUE !FALSE

/**
 * Stupid macro to create a hash of a pointer.
 */
#define S_POINTER_TO_HASH_T(p) ((hash_t)(unsigned long) p)


/* ************************************************************************** *
 * Colours
 * ************************************************************************** */

/**
 * @defgroup Colours Colours
 * @addtogroup Colours
 *   @{
 * @brief Printing colours for terminal/console printing.
 *
 * Here is a list of macros for use when printing to the console.
 * @ref S_COLOR_RESET is used to reset the colours back to their original
 *      colour.
 *
 * An example on how to use them can be found in utils.h:
 * @code{.c}
#define s_err_print(p, ...)\
  fprintf (stderr, S_COLOR_RED "[ERR] " p S_COLOR_RESET "\n", ##__VA_ARGS__)
 * @endcode
 */

#define S_COLOR_RESET   "\033[0m"       /**< Reset */
#define S_COLOR_BLACK   "\033[30m"      /**< Black */
#define S_COLOR_RED     "\033[31m"      /**< Red */
#define S_COLOR_GREEN   "\033[32m"      /**< Green */
#define S_COLOR_YELLOW  "\033[33m"      /**< Yellow */
#define S_COLOR_BLUE    "\033[34m"      /**< Blue */
#define S_COLOR_MAGENTA "\033[35m"      /**< Magenta */
#define S_COLOR_CYAN    "\033[36m"      /**< Cyan */
#define S_COLOR_WHITE   "\033[37m"      /**< White */
#define S_COLOR_BOLDBLACK   "\033[1m\033[30m"      /**< Bold Black */
#define S_COLOR_BOLDRED     "\033[1m\033[31m"      /**< Bold Red */
#define S_COLOR_BOLDGREEN   "\033[1m\033[32m"      /**< Bold Green */
#define S_COLOR_BOLDYELLOW  "\033[1m\033[33m"      /**< Bold Yellow */
#define S_COLOR_BOLDBLUE    "\033[1m\033[34m"      /**< Bold Blue */
#define S_COLOR_BOLDMAGENTA "\033[1m\033[35m"      /**< Bold Magenta */
#define S_COLOR_BOLDCYAN    "\033[1m\033[36m"      /**< Bold Cyan */
#define S_COLOR_BOLDWHITE   "\033[1m\033[37m"      /**< Bold White */

/**   @} */

/** @} */

/* We include these last so it does not create any problems.
 * We want people only to have to include defs.h and not mm.h and types.h
 * anyway.
 *
 * It should be considered a continuation of defs.h.
 */

#include "types.h"

#include "primes.h" /* This is already included in types.h... */

#include "mm.h"


S_END_DECLS