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/.
30 #include <glibconfig.h>
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.
45 typedef gint gboolean;
47 typedef unsigned char guchar;
48 typedef unsigned short gushort;
49 typedef unsigned long gulong;
50 typedef unsigned int guint;
53 typedef double gdouble;
55 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
56 * Since gldouble isn't used anywhere, just disable it for now */
59 #ifdef HAVE_LONG_DOUBLE
60 typedef long double gldouble;
61 #else /* HAVE_LONG_DOUBLE */
62 typedef double gldouble;
63 #endif /* HAVE_LONG_DOUBLE */
66 typedef void* gpointer;
67 typedef const void *gconstpointer;
69 typedef gint (*GCompareFunc) (gconstpointer a,
71 typedef gint (*GCompareDataFunc) (gconstpointer a,
74 typedef gboolean (*GEqualFunc) (gconstpointer a,
76 typedef void (*GDestroyNotify) (gpointer data);
77 typedef void (*GFunc) (gpointer data,
79 typedef guint (*GHashFunc) (gconstpointer key);
80 typedef void (*GHFunc) (gpointer key,
83 typedef void (*GFreeFunc) (gpointer data);
85 /* Define some mathematical constants that aren't available
86 * symbolically in some strict ISO C implementations.
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
96 /* Portable endian checks and conversions
98 * glibconfig.h defines G_BYTE_ORDER which expands to one of
101 #define G_LITTLE_ENDIAN 1234
102 #define G_BIG_ENDIAN 4321
103 #define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
106 /* Basic bit swapping functions
108 #define GUINT16_SWAP_LE_BE_CONSTANT(val) ((guint16) ( \
109 (guint16) ((guint16) (val) >> 8) | \
110 (guint16) ((guint16) (val) << 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 #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)))
136 /* Arch specific stuff for speed
138 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
139 # if defined (__i386__)
140 # define GUINT16_SWAP_LE_BE_IA32(val) \
142 ({ register guint16 __v, __x = ((guint16) (val)); \
143 if (__builtin_constant_p (__x)) \
144 __v = GUINT16_SWAP_LE_BE_CONSTANT (__x); \
146 __asm__ ("rorw $8, %w0" \
151 # if !defined (__i486__) && !defined (__i586__) \
152 && !defined (__pentium__) && !defined (__i686__) \
153 && !defined (__pentiumpro__)
154 # define GUINT32_SWAP_LE_BE_IA32(val) \
156 ({ register guint32 __v, __x = ((guint32) (val)); \
157 if (__builtin_constant_p (__x)) \
158 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
160 __asm__ ("rorw $8, %w0\n\t" \
167 # else /* 486 and higher has bswap */
168 # define GUINT32_SWAP_LE_BE_IA32(val) \
170 ({ register guint32 __v, __x = ((guint32) (val)); \
171 if (__builtin_constant_p (__x)) \
172 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
174 __asm__ ("bswap %0" \
178 # endif /* processor specific 32-bit stuff */
179 # define GUINT64_SWAP_LE_BE_IA32(val) \
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); \
188 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]); \
189 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]); \
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) \
199 ({ register guint16 __v, __x = ((guint16) (val)); \
200 if (__builtin_constant_p (__x)) \
201 __v = GUINT16_SWAP_LE_BE_CONSTANT (__x); \
203 __asm__ __volatile__ ("shl %0 = %1, 48 ;;" \
204 "mux1 %0 = %0, @rev ;;" \
208 # define GUINT32_SWAP_LE_BE_IA64(val) \
210 ({ register guint32 __v, __x = ((guint32) (val)); \
211 if (__builtin_constant_p (__x)) \
212 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
214 __asm__ __volatile__ ("shl %0 = %1, 32 ;;" \
215 "mux1 %0 = %0, @rev ;;" \
219 # define GUINT64_SWAP_LE_BE_IA64(val) \
221 ({ register guint64 __v, __x = ((guint64) (val)); \
222 if (__builtin_constant_p (__x)) \
223 __v = GUINT64_SWAP_LE_BE_CONSTANT (__x); \
225 __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;" \
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) \
235 ({ register guint32 __v, __x = ((guint32) (val)); \
236 if (__builtin_constant_p (__x)) \
237 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
239 __asm__ ("bswapl %0" \
243 # define GUINT64_SWAP_LE_BE_X86_64(val) \
245 ({ register guint64 __v, __x = ((guint64) (val)); \
246 if (__builtin_constant_p (__x)) \
247 __v = GUINT64_SWAP_LE_BE_CONSTANT (__x); \
249 __asm__ ("bswapq %0" \
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))
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))
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)))
277 /* The G*_TO_?E() macros are defined in glibconfig.h.
278 * The transformation is symmetric, so the FROM just maps to the TO.
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))
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))
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))
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))
305 /* Portable versions of host-network order stuff
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))
312 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
315 * +--------+---------------+---------------+
316 * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
317 * +--------+---------------+---------------+
318 * B0------------------->B1------->B2-->B3-->
320 * IEEE Standard 754 Double Precision Storage Format (gdouble):
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->
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
341 guint biased_exponent : 8;
345 union _GDoubleIEEE754
349 guint mantissa_low : 32;
350 guint mantissa_high : 20;
351 guint biased_exponent : 11;
355 #elif G_BYTE_ORDER == G_BIG_ENDIAN
361 guint biased_exponent : 8;
365 union _GDoubleIEEE754
370 guint biased_exponent : 11;
371 guint mantissa_high : 20;
372 guint mantissa_low : 32;
375 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
376 #error unknown ENDIAN type
377 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
379 typedef struct _GTimeVal GTimeVal;
389 /* We prefix variable declarations so they can
390 * properly get exported in windows dlls.
393 # ifdef G_PLATFORM_WIN32
394 # ifdef GLIB_STATIC_COMPILATION
395 # define GLIB_VAR extern
396 # else /* !GLIB_STATIC_COMPILATION */
397 # ifdef GLIB_COMPILATION
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 */
412 #endif /* __G_TYPES_H__ */