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