make it possible to disable single-file includes by defining
[platform/upstream/glib.git] / glib / gtypes.h
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/.
25  */
26
27 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__G_LIB_H__) && !defined (GLIB_COMPILATION)
28 #error "Only <glib.h> can be included directly."
29 #endif
30
31 #ifndef __G_TYPES_H__
32 #define __G_TYPES_H__
33
34 #include <glibconfig.h>
35
36 G_BEGIN_DECLS
37
38 /* Provide type definitions for commonly used types.
39  *  These are useful because a "gint8" can be adjusted
40  *  to be 1 byte (8 bits) on all platforms. Similarly and
41  *  more importantly, "gint32" can be adjusted to be
42  *  4 bytes (32 bits) on all platforms.
43  */
44
45 typedef char   gchar;
46 typedef short  gshort;
47 typedef long   glong;
48 typedef int    gint;
49 typedef gint   gboolean;
50
51 typedef unsigned char   guchar;
52 typedef unsigned short  gushort;
53 typedef unsigned long   gulong;
54 typedef unsigned int    guint;
55
56 typedef float   gfloat;
57 typedef double  gdouble;
58
59 /* Define min and max constants for the fixed size numerical types */
60 #define G_MININT8       ((gint8)  0x80)
61 #define G_MAXINT8       ((gint8)  0x7f)
62 #define G_MAXUINT8      ((guint8) 0xff)
63
64 #define G_MININT16      ((gint16)  0x8000)
65 #define G_MAXINT16      ((gint16)  0x7fff)
66 #define G_MAXUINT16     ((guint16) 0xffff)
67
68 #define G_MININT32      ((gint32)  0x80000000)
69 #define G_MAXINT32      ((gint32)  0x7fffffff)
70 #define G_MAXUINT32     ((guint32) 0xffffffff)
71
72 #define G_MININT64      ((gint64) G_GINT64_CONSTANT(0x8000000000000000))
73 #define G_MAXINT64      G_GINT64_CONSTANT(0x7fffffffffffffff)
74 #define G_MAXUINT64     G_GINT64_CONSTANT(0xffffffffffffffffU)
75
76 typedef void* gpointer;
77 typedef const void *gconstpointer;
78
79 typedef gint            (*GCompareFunc)         (gconstpointer  a,
80                                                  gconstpointer  b);
81 typedef gint            (*GCompareDataFunc)     (gconstpointer  a,
82                                                  gconstpointer  b,
83                                                  gpointer       user_data);
84 typedef gboolean        (*GEqualFunc)           (gconstpointer  a,
85                                                  gconstpointer  b);
86 typedef void            (*GDestroyNotify)       (gpointer       data);
87 typedef void            (*GFunc)                (gpointer       data,
88                                                  gpointer       user_data);
89 typedef guint           (*GHashFunc)            (gconstpointer  key);
90 typedef void            (*GHFunc)               (gpointer       key,
91                                                  gpointer       value,
92                                                  gpointer       user_data);
93 typedef void            (*GFreeFunc)            (gpointer       data);
94 typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
95                                                  gpointer       data);
96
97
98 /* Define some mathematical constants that aren't available
99  * symbolically in some strict ISO C implementations.
100  *
101  * Note that the large number of digits used in these definitions
102  * doesn't imply that GLib or current computers in general would be
103  * able to handle floating point numbers with an accuracy like this.
104  * It's mostly an exercise in futility and future proofing. For
105  * extended precision floating point support, look somewhere else
106  * than GLib.
107  */
108 #define G_E     2.7182818284590452353602874713526624977572470937000
109 #define G_LN2   0.69314718055994530941723212145817656807550013436026
110 #define G_LN10  2.3025850929940456840179914546843642076011014886288
111 #define G_PI    3.1415926535897932384626433832795028841971693993751
112 #define G_PI_2  1.5707963267948966192313216916397514420985846996876
113 #define G_PI_4  0.78539816339744830961566084581987572104929234984378
114 #define G_SQRT2 1.4142135623730950488016887242096980785696718753769
115
116 /* Portable endian checks and conversions
117  *
118  * glibconfig.h defines G_BYTE_ORDER which expands to one of
119  * the below macros.
120  */
121 #define G_LITTLE_ENDIAN 1234
122 #define G_BIG_ENDIAN    4321
123 #define G_PDP_ENDIAN    3412            /* unused, need specific PDP check */   
124
125
126 /* Basic bit swapping functions
127  */
128 #define GUINT16_SWAP_LE_BE_CONSTANT(val)        ((guint16) ( \
129     (guint16) ((guint16) (val) >> 8) |  \
130     (guint16) ((guint16) (val) << 8)))
131
132 #define GUINT32_SWAP_LE_BE_CONSTANT(val)        ((guint32) ( \
133     (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
134     (((guint32) (val) & (guint32) 0x0000ff00U) <<  8) | \
135     (((guint32) (val) & (guint32) 0x00ff0000U) >>  8) | \
136     (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
137
138 #define GUINT64_SWAP_LE_BE_CONSTANT(val)        ((guint64) ( \
139       (((guint64) (val) &                                               \
140         (guint64) G_GINT64_CONSTANT (0x00000000000000ffU)) << 56) |     \
141       (((guint64) (val) &                                               \
142         (guint64) G_GINT64_CONSTANT (0x000000000000ff00U)) << 40) |     \
143       (((guint64) (val) &                                               \
144         (guint64) G_GINT64_CONSTANT (0x0000000000ff0000U)) << 24) |     \
145       (((guint64) (val) &                                               \
146         (guint64) G_GINT64_CONSTANT (0x00000000ff000000U)) <<  8) |     \
147       (((guint64) (val) &                                               \
148         (guint64) G_GINT64_CONSTANT (0x000000ff00000000U)) >>  8) |     \
149       (((guint64) (val) &                                               \
150         (guint64) G_GINT64_CONSTANT (0x0000ff0000000000U)) >> 24) |     \
151       (((guint64) (val) &                                               \
152         (guint64) G_GINT64_CONSTANT (0x00ff000000000000U)) >> 40) |     \
153       (((guint64) (val) &                                               \
154         (guint64) G_GINT64_CONSTANT (0xff00000000000000U)) >> 56)))
155
156 /* Arch specific stuff for speed
157  */
158 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
159 #  if defined (__i386__)
160 #    define GUINT16_SWAP_LE_BE_IA32(val) \
161        (__extension__                                           \
162         ({ register guint16 __v, __x = ((guint16) (val));       \
163            if (__builtin_constant_p (__x))                      \
164              __v = GUINT16_SWAP_LE_BE_CONSTANT (__x);           \
165            else                                                 \
166              __asm__ ("rorw $8, %w0"                            \
167                       : "=r" (__v)                              \
168                       : "0" (__x)                               \
169                       : "cc");                                  \
170             __v; }))
171 #    if !defined (__i486__) && !defined (__i586__) \
172         && !defined (__pentium__) && !defined (__i686__) \
173         && !defined (__pentiumpro__) && !defined (__pentium4__)
174 #       define GUINT32_SWAP_LE_BE_IA32(val) \
175           (__extension__                                        \
176            ({ register guint32 __v, __x = ((guint32) (val));    \
177               if (__builtin_constant_p (__x))                   \
178                 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);        \
179               else                                              \
180                 __asm__ ("rorw $8, %w0\n\t"                     \
181                          "rorl $16, %0\n\t"                     \
182                          "rorw $8, %w0"                         \
183                          : "=r" (__v)                           \
184                          : "0" (__x)                            \
185                          : "cc");                               \
186               __v; }))
187 #    else /* 486 and higher has bswap */
188 #       define GUINT32_SWAP_LE_BE_IA32(val) \
189           (__extension__                                        \
190            ({ register guint32 __v, __x = ((guint32) (val));    \
191               if (__builtin_constant_p (__x))                   \
192                 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);        \
193               else                                              \
194                 __asm__ ("bswap %0"                             \
195                          : "=r" (__v)                           \
196                          : "0" (__x));                          \
197               __v; }))
198 #    endif /* processor specific 32-bit stuff */
199 #    define GUINT64_SWAP_LE_BE_IA32(val) \
200        (__extension__                                                   \
201         ({ union { guint64 __ll;                                        \
202                    guint32 __l[2]; } __w, __r;                          \
203            __w.__ll = ((guint64) (val));                                \
204            if (__builtin_constant_p (__w.__ll))                         \
205              __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (__w.__ll);         \
206            else                                                         \
207              {                                                          \
208                __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]);            \
209                __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]);            \
210              }                                                          \
211            __r.__ll; }))
212      /* Possibly just use the constant version and let gcc figure it out? */
213 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA32 (val))
214 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA32 (val))
215 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA32 (val))
216 #  elif defined (__ia64__)
217 #    define GUINT16_SWAP_LE_BE_IA64(val) \
218        (__extension__                                           \
219         ({ register guint16 __v, __x = ((guint16) (val));       \
220            if (__builtin_constant_p (__x))                      \
221              __v = GUINT16_SWAP_LE_BE_CONSTANT (__x);           \
222            else                                                 \
223              __asm__ __volatile__ ("shl %0 = %1, 48 ;;"         \
224                                    "mux1 %0 = %0, @rev ;;"      \
225                                     : "=r" (__v)                \
226                                     : "r" (__x));               \
227             __v; }))
228 #    define GUINT32_SWAP_LE_BE_IA64(val) \
229        (__extension__                                           \
230          ({ register guint32 __v, __x = ((guint32) (val));      \
231             if (__builtin_constant_p (__x))                     \
232               __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);          \
233             else                                                \
234              __asm__ __volatile__ ("shl %0 = %1, 32 ;;"         \
235                                    "mux1 %0 = %0, @rev ;;"      \
236                                     : "=r" (__v)                \
237                                     : "r" (__x));               \
238             __v; }))
239 #    define GUINT64_SWAP_LE_BE_IA64(val) \
240        (__extension__                                           \
241         ({ register guint64 __v, __x = ((guint64) (val));       \
242            if (__builtin_constant_p (__x))                      \
243              __v = GUINT64_SWAP_LE_BE_CONSTANT (__x);           \
244            else                                                 \
245              __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;"      \
246                                    : "=r" (__v)                 \
247                                    : "r" (__x));                \
248            __v; }))
249 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA64 (val))
250 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA64 (val))
251 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA64 (val))
252 #  elif defined (__x86_64__)
253 #    define GUINT32_SWAP_LE_BE_X86_64(val) \
254        (__extension__                                           \
255          ({ register guint32 __v, __x = ((guint32) (val));      \
256             if (__builtin_constant_p (__x))                     \
257               __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);          \
258             else                                                \
259              __asm__ ("bswapl %0"                               \
260                       : "=r" (__v)                              \
261                       : "0" (__x));                             \
262             __v; }))
263 #    define GUINT64_SWAP_LE_BE_X86_64(val) \
264        (__extension__                                           \
265         ({ register guint64 __v, __x = ((guint64) (val));       \
266            if (__builtin_constant_p (__x))                      \
267              __v = GUINT64_SWAP_LE_BE_CONSTANT (__x);           \
268            else                                                 \
269              __asm__ ("bswapq %0"                               \
270                       : "=r" (__v)                              \
271                       : "0" (__x));                             \
272            __v; }))
273      /* gcc seems to figure out optimal code for this on its own */
274 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
275 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86_64 (val))
276 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86_64 (val))
277 #  else /* generic gcc */
278 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
279 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
280 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
281 #  endif
282 #else /* generic */
283 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
284 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
285 #  define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
286 #endif /* generic */
287
288 #define GUINT16_SWAP_LE_PDP(val)        ((guint16) (val))
289 #define GUINT16_SWAP_BE_PDP(val)        (GUINT16_SWAP_LE_BE (val))
290 #define GUINT32_SWAP_LE_PDP(val)        ((guint32) ( \
291     (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
292     (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
293 #define GUINT32_SWAP_BE_PDP(val)        ((guint32) ( \
294     (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
295     (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
296
297 /* The G*_TO_?E() macros are defined in glibconfig.h.
298  * The transformation is symmetric, so the FROM just maps to the TO.
299  */
300 #define GINT16_FROM_LE(val)     (GINT16_TO_LE (val))
301 #define GUINT16_FROM_LE(val)    (GUINT16_TO_LE (val))
302 #define GINT16_FROM_BE(val)     (GINT16_TO_BE (val))
303 #define GUINT16_FROM_BE(val)    (GUINT16_TO_BE (val))
304 #define GINT32_FROM_LE(val)     (GINT32_TO_LE (val))
305 #define GUINT32_FROM_LE(val)    (GUINT32_TO_LE (val))
306 #define GINT32_FROM_BE(val)     (GINT32_TO_BE (val))
307 #define GUINT32_FROM_BE(val)    (GUINT32_TO_BE (val))
308
309 #define GINT64_FROM_LE(val)     (GINT64_TO_LE (val))
310 #define GUINT64_FROM_LE(val)    (GUINT64_TO_LE (val))
311 #define GINT64_FROM_BE(val)     (GINT64_TO_BE (val))
312 #define GUINT64_FROM_BE(val)    (GUINT64_TO_BE (val))
313
314 #define GLONG_FROM_LE(val)      (GLONG_TO_LE (val))
315 #define GULONG_FROM_LE(val)     (GULONG_TO_LE (val))
316 #define GLONG_FROM_BE(val)      (GLONG_TO_BE (val))
317 #define GULONG_FROM_BE(val)     (GULONG_TO_BE (val))
318
319 #define GINT_FROM_LE(val)       (GINT_TO_LE (val))
320 #define GUINT_FROM_LE(val)      (GUINT_TO_LE (val))
321 #define GINT_FROM_BE(val)       (GINT_TO_BE (val))
322 #define GUINT_FROM_BE(val)      (GUINT_TO_BE (val))
323
324
325 /* Portable versions of host-network order stuff
326  */
327 #define g_ntohl(val) (GUINT32_FROM_BE (val))
328 #define g_ntohs(val) (GUINT16_FROM_BE (val))
329 #define g_htonl(val) (GUINT32_TO_BE (val))
330 #define g_htons(val) (GUINT16_TO_BE (val))
331
332 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
333  *
334  *        31 30           23 22            0
335  * +--------+---------------+---------------+
336  * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
337  * +--------+---------------+---------------+
338  * B0------------------->B1------->B2-->B3-->
339  *
340  * IEEE Standard 754 Double Precision Storage Format (gdouble):
341  *
342  *        63 62            52 51            32   31            0
343  * +--------+----------------+----------------+ +---------------+
344  * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
345  * +--------+----------------+----------------+ +---------------+
346  * B0--------------->B1---------->B2--->B3---->  B4->B5->B6->B7->
347  */
348 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
349 typedef union  _GDoubleIEEE754  GDoubleIEEE754;
350 typedef union  _GFloatIEEE754   GFloatIEEE754;
351 #define G_IEEE754_FLOAT_BIAS    (127)
352 #define G_IEEE754_DOUBLE_BIAS   (1023)
353 /* multiply with base2 exponent to get base10 exponent (normal numbers) */
354 #define G_LOG_2_BASE_10         (0.30102999566398119521)
355 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
356 union _GFloatIEEE754
357 {
358   gfloat v_float;
359   struct {
360     guint mantissa : 23;
361     guint biased_exponent : 8;
362     guint sign : 1;
363   } mpn;
364 };
365 union _GDoubleIEEE754
366 {
367   gdouble v_double;
368   struct {
369     guint mantissa_low : 32;
370     guint mantissa_high : 20;
371     guint biased_exponent : 11;
372     guint sign : 1;
373   } mpn;
374 };
375 #elif G_BYTE_ORDER == G_BIG_ENDIAN
376 union _GFloatIEEE754
377 {
378   gfloat v_float;
379   struct {
380     guint sign : 1;
381     guint biased_exponent : 8;
382     guint mantissa : 23;
383   } mpn;
384 };
385 union _GDoubleIEEE754
386 {
387   gdouble v_double;
388   struct {
389     guint sign : 1;
390     guint biased_exponent : 11;
391     guint mantissa_high : 20;
392     guint mantissa_low : 32;
393   } mpn;
394 };
395 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
396 #error unknown ENDIAN type
397 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
398
399 typedef struct _GTimeVal                GTimeVal;
400
401 struct _GTimeVal
402 {
403   glong tv_sec;
404   glong tv_usec;
405 };
406
407 G_END_DECLS
408
409 /* We prefix variable declarations so they can
410  * properly get exported in windows dlls.
411  */
412 #ifndef GLIB_VAR
413 #  ifdef G_PLATFORM_WIN32
414 #    ifdef GLIB_STATIC_COMPILATION
415 #      define GLIB_VAR extern
416 #    else /* !GLIB_STATIC_COMPILATION */
417 #      ifdef GLIB_COMPILATION
418 #        ifdef DLL_EXPORT
419 #          define GLIB_VAR __declspec(dllexport)
420 #        else /* !DLL_EXPORT */
421 #          define GLIB_VAR extern
422 #        endif /* !DLL_EXPORT */
423 #      else /* !GLIB_COMPILATION */
424 #        define GLIB_VAR extern __declspec(dllimport)
425 #      endif /* !GLIB_COMPILATION */
426 #    endif /* !GLIB_STATIC_COMPILATION */
427 #  else /* !G_PLATFORM_WIN32 */
428 #    define GLIB_VAR extern
429 #  endif /* !G_PLATFORM_WIN32 */
430 #endif /* GLIB_VAR */
431
432 #endif /* __G_TYPES_H__ */