Introduced new function type GEqualFunc to return TRUE for equal params.
[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 typedef gchar* gstring;
47
48 typedef unsigned char   guchar;
49 typedef unsigned short  gushort;
50 typedef unsigned long   gulong;
51 typedef unsigned int    guint;
52
53 typedef float   gfloat;
54 typedef double  gdouble;
55
56 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
57  * Since gldouble isn't used anywhere, just disable it for now */
58
59 #if 0
60 #ifdef HAVE_LONG_DOUBLE
61 typedef long double gldouble;
62 #else /* HAVE_LONG_DOUBLE */
63 typedef double gldouble;
64 #endif /* HAVE_LONG_DOUBLE */
65 #endif /* 0 */
66
67 typedef void* gpointer;
68 typedef const void *gconstpointer;
69
70 typedef gint            (*GCompareFunc)         (gconstpointer  a,
71                                                  gconstpointer  b);
72 typedef gboolean        (*GEqualFunc)           (gconstpointer  a,
73                                                  gconstpointer  b);
74 typedef void            (*GDestroyNotify)       (gpointer       data);
75 typedef void            (*GFunc)                (gpointer       data,
76                                                  gpointer       user_data);
77 typedef guint           (*GHashFunc)            (gconstpointer  key);
78 typedef void            (*GHFunc)               (gpointer       key,
79                                                  gpointer       value,
80                                                  gpointer       user_data);
81 typedef void            (*GFreeFunc)            (gpointer       data);
82
83 /* Define some mathematical constants that aren't available
84  * symbolically in some strict ISO C implementations.
85  */
86 #define G_E     2.7182818284590452354E0
87 #define G_LN2   6.9314718055994530942E-1
88 #define G_LN10  2.3025850929940456840E0
89 #define G_PI    3.14159265358979323846E0
90 #define G_PI_2  1.57079632679489661923E0
91 #define G_PI_4  0.78539816339744830962E0
92 #define G_SQRT2 1.4142135623730950488E0
93
94 /* Portable endian checks and conversions
95  *
96  * glibconfig.h defines G_BYTE_ORDER which expands to one of
97  * the below macros.
98  */
99 #define G_LITTLE_ENDIAN 1234
100 #define G_BIG_ENDIAN    4321
101 #define G_PDP_ENDIAN    3412            /* unused, need specific PDP check */   
102
103
104 /* Basic bit swapping functions
105  */
106 #define GUINT16_SWAP_LE_BE_CONSTANT(val)        ((guint16) ( \
107     (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
108     (((guint16) (val) & (guint16) 0xff00U) >> 8)))
109 #define GUINT32_SWAP_LE_BE_CONSTANT(val)        ((guint32) ( \
110     (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
111     (((guint32) (val) & (guint32) 0x0000ff00U) <<  8) | \
112     (((guint32) (val) & (guint32) 0x00ff0000U) >>  8) | \
113     (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
114
115 /* Intel specific stuff for speed
116  */
117 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
118 #  define GUINT16_SWAP_LE_BE_X86(val) \
119      (__extension__                                     \
120       ({ register guint16 __v;                          \
121          if (__builtin_constant_p (val))                \
122            __v = GUINT16_SWAP_LE_BE_CONSTANT (val);     \
123          else                                           \
124            __asm__ __const__ ("rorw $8, %w0"            \
125                               : "=r" (__v)              \
126                               : "0" ((guint16) (val))); \
127         __v; }))
128 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
129 #  if !defined(__i486__) && !defined(__i586__) \
130       && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
131 #     define GUINT32_SWAP_LE_BE_X86(val) \
132         (__extension__                                          \
133          ({ register guint32 __v;                               \
134             if (__builtin_constant_p (val))                     \
135               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
136           else                                                  \
137             __asm__ __const__ ("rorw $8, %w0\n\t"               \
138                                "rorl $16, %0\n\t"               \
139                                "rorw $8, %w0"                   \
140                                : "=r" (__v)                     \
141                                : "0" ((guint32) (val)));        \
142         __v; }))
143 #  else /* 486 and higher has bswap */
144 #     define GUINT32_SWAP_LE_BE_X86(val) \
145         (__extension__                                          \
146          ({ register guint32 __v;                               \
147             if (__builtin_constant_p (val))                     \
148               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
149           else                                                  \
150             __asm__ __const__ ("bswap %0"                       \
151                                : "=r" (__v)                     \
152                                : "0" ((guint32) (val)));        \
153         __v; }))
154 #  endif /* processor specific 32-bit stuff */
155 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
156 #else /* !__i386__ */
157 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
158 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
159 #endif /* __i386__ */
160
161 #ifdef G_HAVE_GINT64
162 #  define GUINT64_SWAP_LE_BE_CONSTANT(val)      ((guint64) ( \
163       (((guint64) (val) &                                               \
164         (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) |      \
165       (((guint64) (val) &                                               \
166         (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) |      \
167       (((guint64) (val) &                                               \
168         (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) |      \
169       (((guint64) (val) &                                               \
170         (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) <<  8) |      \
171       (((guint64) (val) &                                               \
172         (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >>  8) |      \
173       (((guint64) (val) &                                               \
174         (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) |      \
175       (((guint64) (val) &                                               \
176         (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) |      \
177       (((guint64) (val) &                                               \
178         (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
179 #  if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
180 #    define GUINT64_SWAP_LE_BE_X86(val) \
181         (__extension__                                          \
182          ({ union { guint64 __ll;                               \
183                     guint32 __l[2]; } __r;                      \
184             if (__builtin_constant_p (val))                     \
185               __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val);     \
186             else                                                \
187               {                                                 \
188                 union { guint64 __ll;                           \
189                         guint32 __l[2]; } __w;                  \
190                 __w.__ll = ((guint64) val);                     \
191                 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]);   \
192                 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]);   \
193               }                                                 \
194           __r.__ll; }))
195 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
196 #  else /* !__i386__ */
197 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
198 #  endif
199 #endif
200
201 #define GUINT16_SWAP_LE_PDP(val)        ((guint16) (val))
202 #define GUINT16_SWAP_BE_PDP(val)        (GUINT16_SWAP_LE_BE (val))
203 #define GUINT32_SWAP_LE_PDP(val)        ((guint32) ( \
204     (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
205     (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
206 #define GUINT32_SWAP_BE_PDP(val)        ((guint32) ( \
207     (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
208     (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
209
210 /* The G*_TO_?E() macros are defined in glibconfig.h.
211  * The transformation is symmetric, so the FROM just maps to the TO.
212  */
213 #define GINT16_FROM_LE(val)     (GINT16_TO_LE (val))
214 #define GUINT16_FROM_LE(val)    (GUINT16_TO_LE (val))
215 #define GINT16_FROM_BE(val)     (GINT16_TO_BE (val))
216 #define GUINT16_FROM_BE(val)    (GUINT16_TO_BE (val))
217 #define GINT32_FROM_LE(val)     (GINT32_TO_LE (val))
218 #define GUINT32_FROM_LE(val)    (GUINT32_TO_LE (val))
219 #define GINT32_FROM_BE(val)     (GINT32_TO_BE (val))
220 #define GUINT32_FROM_BE(val)    (GUINT32_TO_BE (val))
221
222 #ifdef G_HAVE_GINT64
223 #define GINT64_FROM_LE(val)     (GINT64_TO_LE (val))
224 #define GUINT64_FROM_LE(val)    (GUINT64_TO_LE (val))
225 #define GINT64_FROM_BE(val)     (GINT64_TO_BE (val))
226 #define GUINT64_FROM_BE(val)    (GUINT64_TO_BE (val))
227 #endif
228
229 #define GLONG_FROM_LE(val)      (GLONG_TO_LE (val))
230 #define GULONG_FROM_LE(val)     (GULONG_TO_LE (val))
231 #define GLONG_FROM_BE(val)      (GLONG_TO_BE (val))
232 #define GULONG_FROM_BE(val)     (GULONG_TO_BE (val))
233
234 #define GINT_FROM_LE(val)       (GINT_TO_LE (val))
235 #define GUINT_FROM_LE(val)      (GUINT_TO_LE (val))
236 #define GINT_FROM_BE(val)       (GINT_TO_BE (val))
237 #define GUINT_FROM_BE(val)      (GUINT_TO_BE (val))
238
239
240 /* Portable versions of host-network order stuff
241  */
242 #define g_ntohl(val) (GUINT32_FROM_BE (val))
243 #define g_ntohs(val) (GUINT16_FROM_BE (val))
244 #define g_htonl(val) (GUINT32_TO_BE (val))
245 #define g_htons(val) (GUINT16_TO_BE (val))
246
247 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
248  *
249  *        31 30           23 22            0
250  * +--------+---------------+---------------+
251  * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
252  * +--------+---------------+---------------+
253  * B0------------------->B1------->B2-->B3-->
254  *
255  * IEEE Standard 754 Double Precision Storage Format (gdouble):
256  *
257  *        63 62            52 51            32   31            0
258  * +--------+----------------+----------------+ +---------------+
259  * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
260  * +--------+----------------+----------------+ +---------------+
261  * B0--------------->B1---------->B2--->B3---->  B4->B5->B6->B7->
262  */
263 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
264 typedef union  _GDoubleIEEE754  GDoubleIEEE754;
265 typedef union  _GFloatIEEE754   GFloatIEEE754;
266 #define G_IEEE754_FLOAT_BIAS    (127)
267 #define G_IEEE754_DOUBLE_BIAS   (1023)
268 /* multiply with base2 exponent to get base10 exponent (nomal numbers) */
269 #define G_LOG_2_BASE_10         (0.30102999566398119521)
270 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
271 union _GFloatIEEE754
272 {
273   gfloat v_float;
274   struct {
275     guint mantissa : 23;
276     guint biased_exponent : 8;
277     guint sign : 1;
278   } mpn;
279 };
280 union _GDoubleIEEE754
281 {
282   gdouble v_double;
283   struct {
284     guint mantissa_low : 32;
285     guint mantissa_high : 20;
286     guint biased_exponent : 11;
287     guint sign : 1;
288   } mpn;
289 };
290 #elif G_BYTE_ORDER == G_BIG_ENDIAN
291 union _GFloatIEEE754
292 {
293   gfloat v_float;
294   struct {
295     guint sign : 1;
296     guint biased_exponent : 8;
297     guint mantissa : 23;
298   } mpn;
299 };
300 union _GDoubleIEEE754
301 {
302   gdouble v_double;
303   struct {
304     guint sign : 1;
305     guint biased_exponent : 11;
306     guint mantissa_high : 20;
307     guint mantissa_low : 32;
308   } mpn;
309 };
310 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
311 #error unknown ENDIAN type
312 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
313
314 G_END_DECLS
315
316 #endif /* __G_TYPES_H__ */
317