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 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
28 #error "Only <glib.h> can be included directly."
29 #endif
30 
31 #ifndef __G_TYPES_H__
32 #define __G_TYPES_H__
33 
34 #include <glibconfig.h>
35 #include <glib/gmacros.h>
36 #include <time.h>
37 
38 G_BEGIN_DECLS
39 
40 /* Provide type definitions for commonly used types.
41  *  These are useful because a "gint8" can be adjusted
42  *  to be 1 byte (8 bits) on all platforms. Similarly and
43  *  more importantly, "gint32" can be adjusted to be
44  *  4 bytes (32 bits) on all platforms.
45  */
46 
47 typedef char   gchar;
48 typedef short  gshort;
49 typedef long   glong;
50 typedef int    gint;
51 typedef gint   gboolean;
52 
53 typedef unsigned char   guchar;
54 typedef unsigned short  gushort;
55 typedef unsigned long   gulong;
56 typedef unsigned int    guint;
57 
58 typedef float   gfloat;
59 typedef double  gdouble;
60 
61 /* Define min and max constants for the fixed size numerical types */
62 #define G_MININT8	((gint8)  0x80)
63 #define G_MAXINT8	((gint8)  0x7f)
64 #define G_MAXUINT8	((guint8) 0xff)
65 
66 #define G_MININT16	((gint16)  0x8000)
67 #define G_MAXINT16	((gint16)  0x7fff)
68 #define G_MAXUINT16	((guint16) 0xffff)
69 
70 #define G_MININT32	((gint32)  0x80000000)
71 #define G_MAXINT32	((gint32)  0x7fffffff)
72 #define G_MAXUINT32	((guint32) 0xffffffff)
73 
74 #define G_MININT64	((gint64) G_GINT64_CONSTANT(0x8000000000000000))
75 #define G_MAXINT64	G_GINT64_CONSTANT(0x7fffffffffffffff)
76 #define G_MAXUINT64	G_GINT64_CONSTANT(0xffffffffffffffffU)
77 
78 typedef void* gpointer;
79 typedef const void *gconstpointer;
80 
81 typedef gint            (*GCompareFunc)         (gconstpointer  a,
82                                                  gconstpointer  b);
83 typedef gint            (*GCompareDataFunc)     (gconstpointer  a,
84                                                  gconstpointer  b,
85 						 gpointer       user_data);
86 typedef gboolean        (*GEqualFunc)           (gconstpointer  a,
87                                                  gconstpointer  b);
88 typedef void            (*GDestroyNotify)       (gpointer       data);
89 typedef void            (*GFunc)                (gpointer       data,
90                                                  gpointer       user_data);
91 typedef guint           (*GHashFunc)            (gconstpointer  key);
92 typedef void            (*GHFunc)               (gpointer       key,
93                                                  gpointer       value,
94                                                  gpointer       user_data);
95 
96 /**
97  * GFreeFunc:
98  * @data: a data pointer
99  *
100  * Declares a type of function which takes an arbitrary
101  * data pointer argument and has no return value. It is
102  * not currently used in GLib or GTK+.
103  */
104 typedef void            (*GFreeFunc)            (gpointer       data);
105 
106 /**
107  * GTranslateFunc:
108  * @str: the untranslated string
109  * @data: user data specified when installing the function, e.g.
110  *  in g_option_group_set_translate_func()
111  *
112  * The type of functions which are used to translate user-visible
113  * strings, for <option>--help</option> output.
114  *
115  * Returns: a translation of the string for the current locale.
116  *  The returned string is owned by GLib and must not be freed.
117  */
118 typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
119 						 gpointer       data);
120 
121 
122 /* Define some mathematical constants that aren't available
123  * symbolically in some strict ISO C implementations.
124  *
125  * Note that the large number of digits used in these definitions
126  * doesn't imply that GLib or current computers in general would be
127  * able to handle floating point numbers with an accuracy like this.
128  * It's mostly an exercise in futility and future proofing. For
129  * extended precision floating point support, look somewhere else
130  * than GLib.
131  */
132 #define G_E     2.7182818284590452353602874713526624977572470937000
133 #define G_LN2   0.69314718055994530941723212145817656807550013436026
134 #define G_LN10  2.3025850929940456840179914546843642076011014886288
135 #define G_PI    3.1415926535897932384626433832795028841971693993751
136 #define G_PI_2  1.5707963267948966192313216916397514420985846996876
137 #define G_PI_4  0.78539816339744830961566084581987572104929234984378
138 #define G_SQRT2 1.4142135623730950488016887242096980785696718753769
139 
140 /* Portable endian checks and conversions
141  *
142  * glibconfig.h defines G_BYTE_ORDER which expands to one of
143  * the below macros.
144  */
145 #define G_LITTLE_ENDIAN 1234
146 #define G_BIG_ENDIAN    4321
147 #define G_PDP_ENDIAN    3412		/* unused, need specific PDP check */
148 
149 
150 /* Basic bit swapping functions
151  */
152 #define GUINT16_SWAP_LE_BE_CONSTANT(val)	((guint16) ( \
153     (guint16) ((guint16) (val) >> 8) |	\
154     (guint16) ((guint16) (val) << 8)))
155 
156 #define GUINT32_SWAP_LE_BE_CONSTANT(val)	((guint32) ( \
157     (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
158     (((guint32) (val) & (guint32) 0x0000ff00U) <<  8) | \
159     (((guint32) (val) & (guint32) 0x00ff0000U) >>  8) | \
160     (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
161 
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 
180 /* Arch specific stuff for speed
181  */
182 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
183 #  if defined (__i386__)
184 #    define GUINT16_SWAP_LE_BE_IA32(val) \
185        (G_GNUC_EXTENSION					\
186 	({ register guint16 __v, __x = ((guint16) (val));	\
187 	   if (__builtin_constant_p (__x))			\
188 	     __v = GUINT16_SWAP_LE_BE_CONSTANT (__x);		\
189 	   else							\
190 	     __asm__ ("rorw $8, %w0"				\
191 		      : "=r" (__v)				\
192 		      : "0" (__x)				\
193 		      : "cc");					\
194 	    __v; }))
195 #    if !defined (__i486__) && !defined (__i586__) \
196 	&& !defined (__pentium__) && !defined (__i686__) \
197 	&& !defined (__pentiumpro__) && !defined (__pentium4__)
198 #       define GUINT32_SWAP_LE_BE_IA32(val) \
199 	  (G_GNUC_EXTENSION					\
200 	   ({ register guint32 __v, __x = ((guint32) (val));	\
201 	      if (__builtin_constant_p (__x))			\
202 		__v = GUINT32_SWAP_LE_BE_CONSTANT (__x);	\
203 	      else						\
204 		__asm__ ("rorw $8, %w0\n\t"			\
205 			 "rorl $16, %0\n\t"			\
206 			 "rorw $8, %w0"				\
207 			 : "=r" (__v)				\
208 			 : "0" (__x)				\
209 			 : "cc");				\
210 	      __v; }))
211 #    else /* 486 and higher has bswap */
212 #       define GUINT32_SWAP_LE_BE_IA32(val) \
213 	  (G_GNUC_EXTENSION					\
214 	   ({ register guint32 __v, __x = ((guint32) (val));	\
215 	      if (__builtin_constant_p (__x))			\
216 		__v = GUINT32_SWAP_LE_BE_CONSTANT (__x);	\
217 	      else						\
218 		__asm__ ("bswap %0"				\
219 			 : "=r" (__v)				\
220 			 : "0" (__x));				\
221 	      __v; }))
222 #    endif /* processor specific 32-bit stuff */
223 #    define GUINT64_SWAP_LE_BE_IA32(val) \
224        (G_GNUC_EXTENSION						\
225 	({ union { guint64 __ll;					\
226 		   guint32 __l[2]; } __w, __r;				\
227 	   __w.__ll = ((guint64) (val));				\
228 	   if (__builtin_constant_p (__w.__ll))				\
229 	     __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (__w.__ll);		\
230 	   else								\
231 	     {								\
232 	       __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]);		\
233 	       __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]);		\
234 	     }								\
235 	   __r.__ll; }))
236      /* Possibly just use the constant version and let gcc figure it out? */
237 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA32 (val))
238 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA32 (val))
239 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA32 (val))
240 #  elif defined (__ia64__)
241 #    define GUINT16_SWAP_LE_BE_IA64(val) \
242        (G_GNUC_EXTENSION					\
243 	({ register guint16 __v, __x = ((guint16) (val));	\
244 	   if (__builtin_constant_p (__x))			\
245 	     __v = GUINT16_SWAP_LE_BE_CONSTANT (__x);		\
246 	   else							\
247 	     __asm__ __volatile__ ("shl %0 = %1, 48 ;;"		\
248 				   "mux1 %0 = %0, @rev ;;"	\
249 				    : "=r" (__v)		\
250 				    : "r" (__x));		\
251 	    __v; }))
252 #    define GUINT32_SWAP_LE_BE_IA64(val) \
253        (G_GNUC_EXTENSION					\
254 	 ({ register guint32 __v, __x = ((guint32) (val));	\
255 	    if (__builtin_constant_p (__x))			\
256 	      __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);		\
257 	    else						\
258 	     __asm__ __volatile__ ("shl %0 = %1, 32 ;;"		\
259 				   "mux1 %0 = %0, @rev ;;"	\
260 				    : "=r" (__v)		\
261 				    : "r" (__x));		\
262 	    __v; }))
263 #    define GUINT64_SWAP_LE_BE_IA64(val) \
264        (G_GNUC_EXTENSION					\
265 	({ register guint64 __v, __x = ((guint64) (val));	\
266 	   if (__builtin_constant_p (__x))			\
267 	     __v = GUINT64_SWAP_LE_BE_CONSTANT (__x);		\
268 	   else							\
269 	     __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;"	\
270 				   : "=r" (__v)			\
271 				   : "r" (__x));		\
272 	   __v; }))
273 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA64 (val))
274 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA64 (val))
275 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA64 (val))
276 #  elif defined (__x86_64__)
277 #    define GUINT32_SWAP_LE_BE_X86_64(val) \
278        (G_GNUC_EXTENSION					\
279 	 ({ register guint32 __v, __x = ((guint32) (val));	\
280 	    if (__builtin_constant_p (__x))			\
281 	      __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);		\
282 	    else						\
283 	     __asm__ ("bswapl %0"				\
284 		      : "=r" (__v)				\
285 		      : "0" (__x));				\
286 	    __v; }))
287 #    define GUINT64_SWAP_LE_BE_X86_64(val) \
288        (G_GNUC_EXTENSION					\
289 	({ register guint64 __v, __x = ((guint64) (val));	\
290 	   if (__builtin_constant_p (__x))			\
291 	     __v = GUINT64_SWAP_LE_BE_CONSTANT (__x);		\
292 	   else							\
293 	     __asm__ ("bswapq %0"				\
294 		      : "=r" (__v)				\
295 		      : "0" (__x));				\
296 	   __v; }))
297      /* gcc seems to figure out optimal code for this on its own */
298 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
299 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86_64 (val))
300 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86_64 (val))
301 #  else /* generic gcc */
302 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
303 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
304 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
305 #  endif
306 #else /* generic */
307 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
308 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
309 #  define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
310 #endif /* generic */
311 
312 #define GUINT16_SWAP_LE_PDP(val)	((guint16) (val))
313 #define GUINT16_SWAP_BE_PDP(val)	(GUINT16_SWAP_LE_BE (val))
314 #define GUINT32_SWAP_LE_PDP(val)	((guint32) ( \
315     (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
316     (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
317 #define GUINT32_SWAP_BE_PDP(val)	((guint32) ( \
318     (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
319     (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
320 
321 /* The G*_TO_?E() macros are defined in glibconfig.h.
322  * The transformation is symmetric, so the FROM just maps to the TO.
323  */
324 #define GINT16_FROM_LE(val)	(GINT16_TO_LE (val))
325 #define GUINT16_FROM_LE(val)	(GUINT16_TO_LE (val))
326 #define GINT16_FROM_BE(val)	(GINT16_TO_BE (val))
327 #define GUINT16_FROM_BE(val)	(GUINT16_TO_BE (val))
328 #define GINT32_FROM_LE(val)	(GINT32_TO_LE (val))
329 #define GUINT32_FROM_LE(val)	(GUINT32_TO_LE (val))
330 #define GINT32_FROM_BE(val)	(GINT32_TO_BE (val))
331 #define GUINT32_FROM_BE(val)	(GUINT32_TO_BE (val))
332 
333 #define GINT64_FROM_LE(val)	(GINT64_TO_LE (val))
334 #define GUINT64_FROM_LE(val)	(GUINT64_TO_LE (val))
335 #define GINT64_FROM_BE(val)	(GINT64_TO_BE (val))
336 #define GUINT64_FROM_BE(val)	(GUINT64_TO_BE (val))
337 
338 #define GLONG_FROM_LE(val)	(GLONG_TO_LE (val))
339 #define GULONG_FROM_LE(val)	(GULONG_TO_LE (val))
340 #define GLONG_FROM_BE(val)	(GLONG_TO_BE (val))
341 #define GULONG_FROM_BE(val)	(GULONG_TO_BE (val))
342 
343 #define GINT_FROM_LE(val)	(GINT_TO_LE (val))
344 #define GUINT_FROM_LE(val)	(GUINT_TO_LE (val))
345 #define GINT_FROM_BE(val)	(GINT_TO_BE (val))
346 #define GUINT_FROM_BE(val)	(GUINT_TO_BE (val))
347 
348 #define GSIZE_FROM_LE(val)	(GSIZE_TO_LE (val))
349 #define GSSIZE_FROM_LE(val)	(GSSIZE_TO_LE (val))
350 #define GSIZE_FROM_BE(val)	(GSIZE_TO_BE (val))
351 #define GSSIZE_FROM_BE(val)	(GSSIZE_TO_BE (val))
352 
353 
354 /* Portable versions of host-network order stuff
355  */
356 #define g_ntohl(val) (GUINT32_FROM_BE (val))
357 #define g_ntohs(val) (GUINT16_FROM_BE (val))
358 #define g_htonl(val) (GUINT32_TO_BE (val))
359 #define g_htons(val) (GUINT16_TO_BE (val))
360 
361 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
362  *
363  *        31 30           23 22            0
364  * +--------+---------------+---------------+
365  * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
366  * +--------+---------------+---------------+
367  * B0------------------->B1------->B2-->B3-->
368  *
369  * IEEE Standard 754 Double Precision Storage Format (gdouble):
370  *
371  *        63 62            52 51            32   31            0
372  * +--------+----------------+----------------+ +---------------+
373  * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
374  * +--------+----------------+----------------+ +---------------+
375  * B0--------------->B1---------->B2--->B3---->  B4->B5->B6->B7->
376  */
377 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
378 typedef union  _GDoubleIEEE754	GDoubleIEEE754;
379 typedef union  _GFloatIEEE754	GFloatIEEE754;
380 #define G_IEEE754_FLOAT_BIAS	(127)
381 #define G_IEEE754_DOUBLE_BIAS	(1023)
382 /* multiply with base2 exponent to get base10 exponent (normal numbers) */
383 #define G_LOG_2_BASE_10		(0.30102999566398119521)
384 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
385 union _GFloatIEEE754
386 {
387   gfloat v_float;
388   struct {
389     guint mantissa : 23;
390     guint biased_exponent : 8;
391     guint sign : 1;
392   } mpn;
393 };
394 union _GDoubleIEEE754
395 {
396   gdouble v_double;
397   struct {
398     guint mantissa_low : 32;
399     guint mantissa_high : 20;
400     guint biased_exponent : 11;
401     guint sign : 1;
402   } mpn;
403 };
404 #elif G_BYTE_ORDER == G_BIG_ENDIAN
405 union _GFloatIEEE754
406 {
407   gfloat v_float;
408   struct {
409     guint sign : 1;
410     guint biased_exponent : 8;
411     guint mantissa : 23;
412   } mpn;
413 };
414 union _GDoubleIEEE754
415 {
416   gdouble v_double;
417   struct {
418     guint sign : 1;
419     guint biased_exponent : 11;
420     guint mantissa_high : 20;
421     guint mantissa_low : 32;
422   } mpn;
423 };
424 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
425 #error unknown ENDIAN type
426 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
427 
428 typedef struct _GTimeVal                GTimeVal;
429 
430 struct _GTimeVal
431 {
432   glong tv_sec;
433   glong tv_usec;
434 };
435 
436 G_END_DECLS
437 
438 /* We prefix variable declarations so they can
439  * properly get exported in Windows DLLs.
440  */
441 #ifndef GLIB_VAR
442 #  ifdef G_PLATFORM_WIN32
443 #    ifdef GLIB_STATIC_COMPILATION
444 #      define GLIB_VAR extern
445 #    else /* !GLIB_STATIC_COMPILATION */
446 #      ifdef GLIB_COMPILATION
447 #        ifdef DLL_EXPORT
448 #          define GLIB_VAR __declspec(dllexport)
449 #        else /* !DLL_EXPORT */
450 #          define GLIB_VAR extern
451 #        endif /* !DLL_EXPORT */
452 #      else /* !GLIB_COMPILATION */
453 #        define GLIB_VAR extern __declspec(dllimport)
454 #      endif /* !GLIB_COMPILATION */
455 #    endif /* !GLIB_STATIC_COMPILATION */
456 #  else /* !G_PLATFORM_WIN32 */
457 #    define GLIB_VAR extern
458 #  endif /* !G_PLATFORM_WIN32 */
459 #endif /* GLIB_VAR */
460 
461 #endif /* __G_TYPES_H__ */
462