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