If a character can't be converted, don't replace it with a NUL byte, but
[glib.git] / glib / gtypes.h
blob21b55e7c952aa76e0c8c678bef0de620ca01ff04
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 ((gint64) 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);
90 typedef const gchar * (*GTranslateFunc) (const gchar *str,
91 gpointer data);
94 /* Define some mathematical constants that aren't available
95 * symbolically in some strict ISO C implementations.
97 #define G_E 2.7182818284590452353602874713526624977572470937000
98 #define G_LN2 0.69314718055994530941723212145817656807550013436026
99 #define G_LN10 2.3025850929940456840179914546843642076011014886288
100 #define G_PI 3.1415926535897932384626433832795028841971693993751
101 #define G_PI_2 1.5707963267948966192313216916397514420985846996876
102 #define G_PI_4 0.78539816339744830961566084581987572104929234984378
103 #define G_SQRT2 1.4142135623730950488016887242096980785696718753769
105 /* Portable endian checks and conversions
107 * glibconfig.h defines G_BYTE_ORDER which expands to one of
108 * the below macros.
110 #define G_LITTLE_ENDIAN 1234
111 #define G_BIG_ENDIAN 4321
112 #define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
115 /* Basic bit swapping functions
117 #define GUINT16_SWAP_LE_BE_CONSTANT(val) ((guint16) ( \
118 (guint16) ((guint16) (val) >> 8) | \
119 (guint16) ((guint16) (val) << 8)))
121 #define GUINT32_SWAP_LE_BE_CONSTANT(val) ((guint32) ( \
122 (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
123 (((guint32) (val) & (guint32) 0x0000ff00U) << 8) | \
124 (((guint32) (val) & (guint32) 0x00ff0000U) >> 8) | \
125 (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
127 #define GUINT64_SWAP_LE_BE_CONSTANT(val) ((guint64) ( \
128 (((guint64) (val) & \
129 (guint64) G_GINT64_CONSTANT (0x00000000000000ffU)) << 56) | \
130 (((guint64) (val) & \
131 (guint64) G_GINT64_CONSTANT (0x000000000000ff00U)) << 40) | \
132 (((guint64) (val) & \
133 (guint64) G_GINT64_CONSTANT (0x0000000000ff0000U)) << 24) | \
134 (((guint64) (val) & \
135 (guint64) G_GINT64_CONSTANT (0x00000000ff000000U)) << 8) | \
136 (((guint64) (val) & \
137 (guint64) G_GINT64_CONSTANT (0x000000ff00000000U)) >> 8) | \
138 (((guint64) (val) & \
139 (guint64) G_GINT64_CONSTANT (0x0000ff0000000000U)) >> 24) | \
140 (((guint64) (val) & \
141 (guint64) G_GINT64_CONSTANT (0x00ff000000000000U)) >> 40) | \
142 (((guint64) (val) & \
143 (guint64) G_GINT64_CONSTANT (0xff00000000000000U)) >> 56)))
145 /* Arch specific stuff for speed
147 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
148 # if defined (__i386__)
149 # define GUINT16_SWAP_LE_BE_IA32(val) \
150 (__extension__ \
151 ({ register guint16 __v, __x = ((guint16) (val)); \
152 if (__builtin_constant_p (__x)) \
153 __v = GUINT16_SWAP_LE_BE_CONSTANT (__x); \
154 else \
155 __asm__ ("rorw $8, %w0" \
156 : "=r" (__v) \
157 : "0" (__x) \
158 : "cc"); \
159 __v; }))
160 # if !defined (__i486__) && !defined (__i586__) \
161 && !defined (__pentium__) && !defined (__i686__) \
162 && !defined (__pentiumpro__) && !defined (__pentium4__)
163 # define GUINT32_SWAP_LE_BE_IA32(val) \
164 (__extension__ \
165 ({ register guint32 __v, __x = ((guint32) (val)); \
166 if (__builtin_constant_p (__x)) \
167 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
168 else \
169 __asm__ ("rorw $8, %w0\n\t" \
170 "rorl $16, %0\n\t" \
171 "rorw $8, %w0" \
172 : "=r" (__v) \
173 : "0" (__x) \
174 : "cc"); \
175 __v; }))
176 # else /* 486 and higher has bswap */
177 # define GUINT32_SWAP_LE_BE_IA32(val) \
178 (__extension__ \
179 ({ register guint32 __v, __x = ((guint32) (val)); \
180 if (__builtin_constant_p (__x)) \
181 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
182 else \
183 __asm__ ("bswap %0" \
184 : "=r" (__v) \
185 : "0" (__x)); \
186 __v; }))
187 # endif /* processor specific 32-bit stuff */
188 # define GUINT64_SWAP_LE_BE_IA32(val) \
189 (__extension__ \
190 ({ union { guint64 __ll; \
191 guint32 __l[2]; } __w, __r; \
192 __w.__ll = ((guint64) (val)); \
193 if (__builtin_constant_p (__w.__ll)) \
194 __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (__w.__ll); \
195 else \
197 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]); \
198 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]); \
200 __r.__ll; }))
201 /* Possibly just use the constant version and let gcc figure it out? */
202 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA32 (val))
203 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA32 (val))
204 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA32 (val))
205 # elif defined (__ia64__)
206 # define GUINT16_SWAP_LE_BE_IA64(val) \
207 (__extension__ \
208 ({ register guint16 __v, __x = ((guint16) (val)); \
209 if (__builtin_constant_p (__x)) \
210 __v = GUINT16_SWAP_LE_BE_CONSTANT (__x); \
211 else \
212 __asm__ __volatile__ ("shl %0 = %1, 48 ;;" \
213 "mux1 %0 = %0, @rev ;;" \
214 : "=r" (__v) \
215 : "r" (__x)); \
216 __v; }))
217 # define GUINT32_SWAP_LE_BE_IA64(val) \
218 (__extension__ \
219 ({ register guint32 __v, __x = ((guint32) (val)); \
220 if (__builtin_constant_p (__x)) \
221 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
222 else \
223 __asm__ __volatile__ ("shl %0 = %1, 32 ;;" \
224 "mux1 %0 = %0, @rev ;;" \
225 : "=r" (__v) \
226 : "r" (__x)); \
227 __v; }))
228 # define GUINT64_SWAP_LE_BE_IA64(val) \
229 (__extension__ \
230 ({ register guint64 __v, __x = ((guint64) (val)); \
231 if (__builtin_constant_p (__x)) \
232 __v = GUINT64_SWAP_LE_BE_CONSTANT (__x); \
233 else \
234 __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;" \
235 : "=r" (__v) \
236 : "r" (__x)); \
237 __v; }))
238 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA64 (val))
239 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA64 (val))
240 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA64 (val))
241 # elif defined (__x86_64__)
242 # define GUINT32_SWAP_LE_BE_X86_64(val) \
243 (__extension__ \
244 ({ register guint32 __v, __x = ((guint32) (val)); \
245 if (__builtin_constant_p (__x)) \
246 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
247 else \
248 __asm__ ("bswapl %0" \
249 : "=r" (__v) \
250 : "0" (__x)); \
251 __v; }))
252 # define GUINT64_SWAP_LE_BE_X86_64(val) \
253 (__extension__ \
254 ({ register guint64 __v, __x = ((guint64) (val)); \
255 if (__builtin_constant_p (__x)) \
256 __v = GUINT64_SWAP_LE_BE_CONSTANT (__x); \
257 else \
258 __asm__ ("bswapq %0" \
259 : "=r" (__v) \
260 : "0" (__x)); \
261 __v; }))
262 /* gcc seems to figure out optimal code for this on its own */
263 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
264 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86_64 (val))
265 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86_64 (val))
266 # else /* generic gcc */
267 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
268 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
269 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
270 # endif
271 #else /* generic */
272 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
273 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
274 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
275 #endif /* generic */
277 #define GUINT16_SWAP_LE_PDP(val) ((guint16) (val))
278 #define GUINT16_SWAP_BE_PDP(val) (GUINT16_SWAP_LE_BE (val))
279 #define GUINT32_SWAP_LE_PDP(val) ((guint32) ( \
280 (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
281 (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
282 #define GUINT32_SWAP_BE_PDP(val) ((guint32) ( \
283 (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
284 (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
286 /* The G*_TO_?E() macros are defined in glibconfig.h.
287 * The transformation is symmetric, so the FROM just maps to the TO.
289 #define GINT16_FROM_LE(val) (GINT16_TO_LE (val))
290 #define GUINT16_FROM_LE(val) (GUINT16_TO_LE (val))
291 #define GINT16_FROM_BE(val) (GINT16_TO_BE (val))
292 #define GUINT16_FROM_BE(val) (GUINT16_TO_BE (val))
293 #define GINT32_FROM_LE(val) (GINT32_TO_LE (val))
294 #define GUINT32_FROM_LE(val) (GUINT32_TO_LE (val))
295 #define GINT32_FROM_BE(val) (GINT32_TO_BE (val))
296 #define GUINT32_FROM_BE(val) (GUINT32_TO_BE (val))
298 #define GINT64_FROM_LE(val) (GINT64_TO_LE (val))
299 #define GUINT64_FROM_LE(val) (GUINT64_TO_LE (val))
300 #define GINT64_FROM_BE(val) (GINT64_TO_BE (val))
301 #define GUINT64_FROM_BE(val) (GUINT64_TO_BE (val))
303 #define GLONG_FROM_LE(val) (GLONG_TO_LE (val))
304 #define GULONG_FROM_LE(val) (GULONG_TO_LE (val))
305 #define GLONG_FROM_BE(val) (GLONG_TO_BE (val))
306 #define GULONG_FROM_BE(val) (GULONG_TO_BE (val))
308 #define GINT_FROM_LE(val) (GINT_TO_LE (val))
309 #define GUINT_FROM_LE(val) (GUINT_TO_LE (val))
310 #define GINT_FROM_BE(val) (GINT_TO_BE (val))
311 #define GUINT_FROM_BE(val) (GUINT_TO_BE (val))
314 /* Portable versions of host-network order stuff
316 #define g_ntohl(val) (GUINT32_FROM_BE (val))
317 #define g_ntohs(val) (GUINT16_FROM_BE (val))
318 #define g_htonl(val) (GUINT32_TO_BE (val))
319 #define g_htons(val) (GUINT16_TO_BE (val))
321 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
323 * 31 30 23 22 0
324 * +--------+---------------+---------------+
325 * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
326 * +--------+---------------+---------------+
327 * B0------------------->B1------->B2-->B3-->
329 * IEEE Standard 754 Double Precision Storage Format (gdouble):
331 * 63 62 52 51 32 31 0
332 * +--------+----------------+----------------+ +---------------+
333 * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
334 * +--------+----------------+----------------+ +---------------+
335 * B0--------------->B1---------->B2--->B3----> B4->B5->B6->B7->
337 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
338 typedef union _GDoubleIEEE754 GDoubleIEEE754;
339 typedef union _GFloatIEEE754 GFloatIEEE754;
340 #define G_IEEE754_FLOAT_BIAS (127)
341 #define G_IEEE754_DOUBLE_BIAS (1023)
342 /* multiply with base2 exponent to get base10 exponent (normal numbers) */
343 #define G_LOG_2_BASE_10 (0.30102999566398119521)
344 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
345 union _GFloatIEEE754
347 gfloat v_float;
348 struct {
349 guint mantissa : 23;
350 guint biased_exponent : 8;
351 guint sign : 1;
352 } mpn;
354 union _GDoubleIEEE754
356 gdouble v_double;
357 struct {
358 guint mantissa_low : 32;
359 guint mantissa_high : 20;
360 guint biased_exponent : 11;
361 guint sign : 1;
362 } mpn;
364 #elif G_BYTE_ORDER == G_BIG_ENDIAN
365 union _GFloatIEEE754
367 gfloat v_float;
368 struct {
369 guint sign : 1;
370 guint biased_exponent : 8;
371 guint mantissa : 23;
372 } mpn;
374 union _GDoubleIEEE754
376 gdouble v_double;
377 struct {
378 guint sign : 1;
379 guint biased_exponent : 11;
380 guint mantissa_high : 20;
381 guint mantissa_low : 32;
382 } mpn;
384 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
385 #error unknown ENDIAN type
386 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
388 typedef struct _GTimeVal GTimeVal;
390 struct _GTimeVal
392 glong tv_sec;
393 glong tv_usec;
396 G_END_DECLS
398 /* We prefix variable declarations so they can
399 * properly get exported in windows dlls.
401 #ifndef GLIB_VAR
402 # ifdef G_PLATFORM_WIN32
403 # ifdef GLIB_STATIC_COMPILATION
404 # define GLIB_VAR extern
405 # else /* !GLIB_STATIC_COMPILATION */
406 # ifdef GLIB_COMPILATION
407 # ifdef DLL_EXPORT
408 # define GLIB_VAR __declspec(dllexport)
409 # else /* !DLL_EXPORT */
410 # define GLIB_VAR extern
411 # endif /* !DLL_EXPORT */
412 # else /* !GLIB_COMPILATION */
413 # define GLIB_VAR extern __declspec(dllimport)
414 # endif /* !GLIB_COMPILATION */
415 # endif /* !GLIB_STATIC_COMPILATION */
416 # else /* !G_PLATFORM_WIN32 */
417 # define GLIB_VAR extern
418 # endif /* !G_PLATFORM_WIN32 */
419 #endif /* GLIB_VAR */
421 #endif /* __G_TYPES_H__ */