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