Remove warnings about conflicts with the stable version.
[platform/upstream/glib.git] / 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) (val) & (guint16) 0x00ffU) << 8) | \
110     (((guint16) (val) & (guint16) 0xff00U) >> 8)))
111 #define GUINT32_SWAP_LE_BE_CONSTANT(val)        ((guint32) ( \
112     (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
113     (((guint32) (val) & (guint32) 0x0000ff00U) <<  8) | \
114     (((guint32) (val) & (guint32) 0x00ff0000U) >>  8) | \
115     (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
116
117 /* Intel specific stuff for speed
118  */
119 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
120 #  define GUINT16_SWAP_LE_BE_X86(val) \
121      (__extension__                                     \
122       ({ register guint16 __v;                          \
123          if (__builtin_constant_p (val))                \
124            __v = GUINT16_SWAP_LE_BE_CONSTANT (val);     \
125          else                                           \
126            __asm__ __const__ ("rorw $8, %w0"            \
127                               : "=r" (__v)              \
128                               : "0" ((guint16) (val))); \
129         __v; }))
130 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
131 #  if !defined(__i486__) && !defined(__i586__) \
132       && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
133 #     define GUINT32_SWAP_LE_BE_X86(val) \
134         (__extension__                                          \
135          ({ register guint32 __v;                               \
136             if (__builtin_constant_p (val))                     \
137               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
138           else                                                  \
139             __asm__ __const__ ("rorw $8, %w0\n\t"               \
140                                "rorl $16, %0\n\t"               \
141                                "rorw $8, %w0"                   \
142                                : "=r" (__v)                     \
143                                : "0" ((guint32) (val)));        \
144         __v; }))
145 #  else /* 486 and higher has bswap */
146 #     define GUINT32_SWAP_LE_BE_X86(val) \
147         (__extension__                                          \
148          ({ register guint32 __v;                               \
149             if (__builtin_constant_p (val))                     \
150               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
151           else                                                  \
152             __asm__ __const__ ("bswap %0"                       \
153                                : "=r" (__v)                     \
154                                : "0" ((guint32) (val)));        \
155         __v; }))
156 #  endif /* processor specific 32-bit stuff */
157 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
158 #else /* !__i386__ */
159 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
160 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
161 #endif /* __i386__ */
162
163 #ifdef G_HAVE_GINT64
164 #  define GUINT64_SWAP_LE_BE_CONSTANT(val)      ((guint64) ( \
165       (((guint64) (val) &                                               \
166         (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) |      \
167       (((guint64) (val) &                                               \
168         (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) |      \
169       (((guint64) (val) &                                               \
170         (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) |      \
171       (((guint64) (val) &                                               \
172         (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) <<  8) |      \
173       (((guint64) (val) &                                               \
174         (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >>  8) |      \
175       (((guint64) (val) &                                               \
176         (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) |      \
177       (((guint64) (val) &                                               \
178         (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) |      \
179       (((guint64) (val) &                                               \
180         (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
181 #  if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
182 #    define GUINT64_SWAP_LE_BE_X86(val) \
183         (__extension__                                          \
184          ({ union { guint64 __ll;                               \
185                     guint32 __l[2]; } __r;                      \
186             if (__builtin_constant_p (val))                     \
187               __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val);     \
188             else                                                \
189               {                                                 \
190                 union { guint64 __ll;                           \
191                         guint32 __l[2]; } __w;                  \
192                 __w.__ll = ((guint64) val);                     \
193                 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]);   \
194                 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]);   \
195               }                                                 \
196           __r.__ll; }))
197 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
198 #  else /* !__i386__ */
199 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
200 #  endif
201 #endif
202
203 #define GUINT16_SWAP_LE_PDP(val)        ((guint16) (val))
204 #define GUINT16_SWAP_BE_PDP(val)        (GUINT16_SWAP_LE_BE (val))
205 #define GUINT32_SWAP_LE_PDP(val)        ((guint32) ( \
206     (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
207     (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
208 #define GUINT32_SWAP_BE_PDP(val)        ((guint32) ( \
209     (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
210     (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
211
212 /* The G*_TO_?E() macros are defined in glibconfig.h.
213  * The transformation is symmetric, so the FROM just maps to the TO.
214  */
215 #define GINT16_FROM_LE(val)     (GINT16_TO_LE (val))
216 #define GUINT16_FROM_LE(val)    (GUINT16_TO_LE (val))
217 #define GINT16_FROM_BE(val)     (GINT16_TO_BE (val))
218 #define GUINT16_FROM_BE(val)    (GUINT16_TO_BE (val))
219 #define GINT32_FROM_LE(val)     (GINT32_TO_LE (val))
220 #define GUINT32_FROM_LE(val)    (GUINT32_TO_LE (val))
221 #define GINT32_FROM_BE(val)     (GINT32_TO_BE (val))
222 #define GUINT32_FROM_BE(val)    (GUINT32_TO_BE (val))
223
224 #ifdef G_HAVE_GINT64
225 #define GINT64_FROM_LE(val)     (GINT64_TO_LE (val))
226 #define GUINT64_FROM_LE(val)    (GUINT64_TO_LE (val))
227 #define GINT64_FROM_BE(val)     (GINT64_TO_BE (val))
228 #define GUINT64_FROM_BE(val)    (GUINT64_TO_BE (val))
229 #endif
230
231 #define GLONG_FROM_LE(val)      (GLONG_TO_LE (val))
232 #define GULONG_FROM_LE(val)     (GULONG_TO_LE (val))
233 #define GLONG_FROM_BE(val)      (GLONG_TO_BE (val))
234 #define GULONG_FROM_BE(val)     (GULONG_TO_BE (val))
235
236 #define GINT_FROM_LE(val)       (GINT_TO_LE (val))
237 #define GUINT_FROM_LE(val)      (GUINT_TO_LE (val))
238 #define GINT_FROM_BE(val)       (GINT_TO_BE (val))
239 #define GUINT_FROM_BE(val)      (GUINT_TO_BE (val))
240
241
242 /* Portable versions of host-network order stuff
243  */
244 #define g_ntohl(val) (GUINT32_FROM_BE (val))
245 #define g_ntohs(val) (GUINT16_FROM_BE (val))
246 #define g_htonl(val) (GUINT32_TO_BE (val))
247 #define g_htons(val) (GUINT16_TO_BE (val))
248
249 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
250  *
251  *        31 30           23 22            0
252  * +--------+---------------+---------------+
253  * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
254  * +--------+---------------+---------------+
255  * B0------------------->B1------->B2-->B3-->
256  *
257  * IEEE Standard 754 Double Precision Storage Format (gdouble):
258  *
259  *        63 62            52 51            32   31            0
260  * +--------+----------------+----------------+ +---------------+
261  * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
262  * +--------+----------------+----------------+ +---------------+
263  * B0--------------->B1---------->B2--->B3---->  B4->B5->B6->B7->
264  */
265 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
266 typedef union  _GDoubleIEEE754  GDoubleIEEE754;
267 typedef union  _GFloatIEEE754   GFloatIEEE754;
268 #define G_IEEE754_FLOAT_BIAS    (127)
269 #define G_IEEE754_DOUBLE_BIAS   (1023)
270 /* multiply with base2 exponent to get base10 exponent (nomal numbers) */
271 #define G_LOG_2_BASE_10         (0.30102999566398119521)
272 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
273 union _GFloatIEEE754
274 {
275   gfloat v_float;
276   struct {
277     guint mantissa : 23;
278     guint biased_exponent : 8;
279     guint sign : 1;
280   } mpn;
281 };
282 union _GDoubleIEEE754
283 {
284   gdouble v_double;
285   struct {
286     guint mantissa_low : 32;
287     guint mantissa_high : 20;
288     guint biased_exponent : 11;
289     guint sign : 1;
290   } mpn;
291 };
292 #elif G_BYTE_ORDER == G_BIG_ENDIAN
293 union _GFloatIEEE754
294 {
295   gfloat v_float;
296   struct {
297     guint sign : 1;
298     guint biased_exponent : 8;
299     guint mantissa : 23;
300   } mpn;
301 };
302 union _GDoubleIEEE754
303 {
304   gdouble v_double;
305   struct {
306     guint sign : 1;
307     guint biased_exponent : 11;
308     guint mantissa_high : 20;
309     guint mantissa_low : 32;
310   } mpn;
311 };
312 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
313 #error unknown ENDIAN type
314 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
315
316 typedef struct _GTimeVal                GTimeVal;
317
318 struct _GTimeVal
319 {
320   glong tv_sec;
321   glong tv_usec;
322 };
323
324 G_END_DECLS
325
326 /* We prefix variable declarations so they can
327  * properly get exported in windows dlls.
328  */
329 #ifndef GLIB_VAR
330 #  ifdef G_PLATFORM_WIN32
331 #    ifdef GLIB_STATIC_COMPILATION
332 #      define GLIB_VAR extern
333 #    else /* !GLIB_STATIC_COMPILATION */
334 #      ifdef GLIB_COMPILATION
335 #        ifdef DLL_EXPORT
336 #          define GLIB_VAR __declspec(dllexport)
337 #        else /* !DLL_EXPORT */
338 #          define GLIB_VAR extern
339 #        endif /* !DLL_EXPORT */
340 #      else /* !GLIB_COMPILATION */
341 #        define GLIB_VAR extern __declspec(dllimport)
342 #      endif /* !GLIB_COMPILATION */
343 #    endif /* !GLIB_STATIC_COMPILATION */
344 #  else /* !G_PLATFORM_WIN32 */
345 #    define GLIB_VAR extern
346 #  endif /* !G_PLATFORM_WIN32 */
347 #endif /* GLIB_VAR */
348
349 #endif /* __G_TYPES_H__ */
350