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