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