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