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