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