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