Navit SVN

/work/compile/navit/src/navit/support/glib/gtypes.h

00001 /* GLIB - Library of useful routines for C programming
00002  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  */
00019 
00020 /*
00021  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
00022  * file for a list of people on the GLib Team.  See the ChangeLog
00023  * files for a list of changes.  These files are distributed with
00024  * GLib at ftp://ftp.gtk.org/pub/gtk/.
00025  */
00026 
00027 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
00028 #error "Only <glib.h> can be included directly."
00029 #endif
00030 
00031 #ifndef __G_TYPES_H__
00032 #define __G_TYPES_H__
00033 
00034 #include <glibconfig.h>
00035 
00036 G_BEGIN_DECLS
00037 
00038 /* Provide type definitions for commonly used types.
00039  *  These are useful because a "gint8" can be adjusted
00040  *  to be 1 byte (8 bits) on all platforms. Similarly and
00041  *  more importantly, "gint32" can be adjusted to be
00042  *  4 bytes (32 bits) on all platforms.
00043  */
00044 
00045 typedef char   gchar;
00046 typedef short  gshort;
00047 typedef long   glong;
00048 typedef int    gint;
00049 typedef gint   gboolean;
00050 
00051 typedef unsigned char   guchar;
00052 typedef unsigned short  gushort;
00053 typedef unsigned long   gulong;
00054 typedef unsigned int    guint;
00055 
00056 typedef float   gfloat;
00057 typedef double  gdouble;
00058 
00059 /* Define min and max constants for the fixed size numerical types */
00060 #define G_MININT8       ((gint8)  0x80)
00061 #define G_MAXINT8       ((gint8)  0x7f)
00062 #define G_MAXUINT8      ((guint8) 0xff)
00063 
00064 #define G_MININT16      ((gint16)  0x8000)
00065 #define G_MAXINT16      ((gint16)  0x7fff)
00066 #define G_MAXUINT16     ((guint16) 0xffff)
00067 
00068 #define G_MININT32      ((gint32)  0x80000000)
00069 #define G_MAXINT32      ((gint32)  0x7fffffff)
00070 #define G_MAXUINT32     ((guint32) 0xffffffff)
00071 
00072 #define G_MININT64      ((gint64) G_GINT64_CONSTANT(0x8000000000000000))
00073 #define G_MAXINT64      G_GINT64_CONSTANT(0x7fffffffffffffff)
00074 #define G_MAXUINT64     G_GINT64_CONSTANT(0xffffffffffffffffU)
00075 
00076 typedef void* gpointer;
00077 typedef const void *gconstpointer;
00078 
00079 typedef gint            (*GCompareFunc)         (gconstpointer  a,
00080                                                  gconstpointer  b);
00081 typedef gint            (*GCompareDataFunc)     (gconstpointer  a,
00082                                                  gconstpointer  b,
00083                                                  gpointer       user_data);
00084 typedef gboolean        (*GEqualFunc)           (gconstpointer  a,
00085                                                  gconstpointer  b);
00086 typedef void            (*GDestroyNotify)       (gpointer       data);
00087 typedef void            (*GFunc)                (gpointer       data,
00088                                                  gpointer       user_data);
00089 typedef guint           (*GHashFunc)            (gconstpointer  key);
00090 typedef void            (*GHFunc)               (gpointer       key,
00091                                                  gpointer       value,
00092                                                  gpointer       user_data);
00093 typedef void            (*GFreeFunc)            (gpointer       data);
00094 typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
00095                                                  gpointer       data);
00096 
00097 
00098 /* Define some mathematical constants that aren't available
00099  * symbolically in some strict ISO C implementations.
00100  *
00101  * Note that the large number of digits used in these definitions
00102  * doesn't imply that GLib or current computers in general would be
00103  * able to handle floating point numbers with an accuracy like this.
00104  * It's mostly an exercise in futility and future proofing. For
00105  * extended precision floating point support, look somewhere else
00106  * than GLib.
00107  */
00108 #define G_E     2.7182818284590452353602874713526624977572470937000
00109 #define G_LN2   0.69314718055994530941723212145817656807550013436026
00110 #define G_LN10  2.3025850929940456840179914546843642076011014886288
00111 #define G_PI    3.1415926535897932384626433832795028841971693993751
00112 #define G_PI_2  1.5707963267948966192313216916397514420985846996876
00113 #define G_PI_4  0.78539816339744830961566084581987572104929234984378
00114 #define G_SQRT2 1.4142135623730950488016887242096980785696718753769
00115 
00116 /* Portable endian checks and conversions
00117  *
00118  * glibconfig.h defines G_BYTE_ORDER which expands to one of
00119  * the below macros.
00120  */
00121 #define G_LITTLE_ENDIAN 1234
00122 #define G_BIG_ENDIAN    4321
00123 #define G_PDP_ENDIAN    3412            /* unused, need specific PDP check */   
00124 
00125 
00126 /* Basic bit swapping functions
00127  */
00128 #define GUINT16_SWAP_LE_BE_CONSTANT(val)        ((guint16) ( \
00129     (guint16) ((guint16) (val) >> 8) |  \
00130     (guint16) ((guint16) (val) << 8)))
00131 
00132 #define GUINT32_SWAP_LE_BE_CONSTANT(val)        ((guint32) ( \
00133     (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
00134     (((guint32) (val) & (guint32) 0x0000ff00U) <<  8) | \
00135     (((guint32) (val) & (guint32) 0x00ff0000U) >>  8) | \
00136     (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
00137 
00138 #define GUINT64_SWAP_LE_BE_CONSTANT(val)        ((guint64) ( \
00139       (((guint64) (val) &                                               \
00140         (guint64) G_GINT64_CONSTANT (0x00000000000000ffU)) << 56) |     \
00141       (((guint64) (val) &                                               \
00142         (guint64) G_GINT64_CONSTANT (0x000000000000ff00U)) << 40) |     \
00143       (((guint64) (val) &                                               \
00144         (guint64) G_GINT64_CONSTANT (0x0000000000ff0000U)) << 24) |     \
00145       (((guint64) (val) &                                               \
00146         (guint64) G_GINT64_CONSTANT (0x00000000ff000000U)) <<  8) |     \
00147       (((guint64) (val) &                                               \
00148         (guint64) G_GINT64_CONSTANT (0x000000ff00000000U)) >>  8) |     \
00149       (((guint64) (val) &                                               \
00150         (guint64) G_GINT64_CONSTANT (0x0000ff0000000000U)) >> 24) |     \
00151       (((guint64) (val) &                                               \
00152         (guint64) G_GINT64_CONSTANT (0x00ff000000000000U)) >> 40) |     \
00153       (((guint64) (val) &                                               \
00154         (guint64) G_GINT64_CONSTANT (0xff00000000000000U)) >> 56)))
00155 
00156 /* Arch specific stuff for speed
00157  */
00158 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
00159 #  if defined (__i386__)
00160 #    define GUINT16_SWAP_LE_BE_IA32(val) \
00161        (__extension__                                           \
00162         ({ register guint16 __v, __x = ((guint16) (val));       \
00163            if (__builtin_constant_p (__x))                      \
00164              __v = GUINT16_SWAP_LE_BE_CONSTANT (__x);           \
00165            else                                                 \
00166              __asm__ ("rorw $8, %w0"                            \
00167                       : "=r" (__v)                              \
00168                       : "0" (__x)                               \
00169                       : "cc");                                  \
00170             __v; }))
00171 #    if !defined (__i486__) && !defined (__i586__) \
00172         && !defined (__pentium__) && !defined (__i686__) \
00173         && !defined (__pentiumpro__) && !defined (__pentium4__)
00174 #       define GUINT32_SWAP_LE_BE_IA32(val) \
00175           (__extension__                                        \
00176            ({ register guint32 __v, __x = ((guint32) (val));    \
00177               if (__builtin_constant_p (__x))                   \
00178                 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);        \
00179               else                                              \
00180                 __asm__ ("rorw $8, %w0\n\t"                     \
00181                          "rorl $16, %0\n\t"                     \
00182                          "rorw $8, %w0"                         \
00183                          : "=r" (__v)                           \
00184                          : "0" (__x)                            \
00185                          : "cc");                               \
00186               __v; }))
00187 #    else /* 486 and higher has bswap */
00188 #       define GUINT32_SWAP_LE_BE_IA32(val) \
00189           (__extension__                                        \
00190            ({ register guint32 __v, __x = ((guint32) (val));    \
00191               if (__builtin_constant_p (__x))                   \
00192                 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);        \
00193               else                                              \
00194                 __asm__ ("bswap %0"                             \
00195                          : "=r" (__v)                           \
00196                          : "0" (__x));                          \
00197               __v; }))
00198 #    endif /* processor specific 32-bit stuff */
00199 #    define GUINT64_SWAP_LE_BE_IA32(val) \
00200        (__extension__                                                   \
00201         ({ union { guint64 __ll;                                        \
00202                    guint32 __l[2]; } __w, __r;                          \
00203            __w.__ll = ((guint64) (val));                                \
00204            if (__builtin_constant_p (__w.__ll))                         \
00205              __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (__w.__ll);         \
00206            else                                                         \
00207              {                                                          \
00208                __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]);            \
00209                __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]);            \
00210              }                                                          \
00211            __r.__ll; }))
00212      /* Possibly just use the constant version and let gcc figure it out? */
00213 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA32 (val))
00214 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA32 (val))
00215 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA32 (val))
00216 #  elif defined (__ia64__)
00217 #    define GUINT16_SWAP_LE_BE_IA64(val) \
00218        (__extension__                                           \
00219         ({ register guint16 __v, __x = ((guint16) (val));       \
00220            if (__builtin_constant_p (__x))                      \
00221              __v = GUINT16_SWAP_LE_BE_CONSTANT (__x);           \
00222            else                                                 \
00223              __asm__ __volatile__ ("shl %0 = %1, 48 ;;"         \
00224                                    "mux1 %0 = %0, @rev ;;"      \
00225                                     : "=r" (__v)                \
00226                                     : "r" (__x));               \
00227             __v; }))
00228 #    define GUINT32_SWAP_LE_BE_IA64(val) \
00229        (__extension__                                           \
00230          ({ register guint32 __v, __x = ((guint32) (val));      \
00231             if (__builtin_constant_p (__x))                     \
00232               __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);          \
00233             else                                                \
00234              __asm__ __volatile__ ("shl %0 = %1, 32 ;;"         \
00235                                    "mux1 %0 = %0, @rev ;;"      \
00236                                     : "=r" (__v)                \
00237                                     : "r" (__x));               \
00238             __v; }))
00239 #    define GUINT64_SWAP_LE_BE_IA64(val) \
00240        (__extension__                                           \
00241         ({ register guint64 __v, __x = ((guint64) (val));       \
00242            if (__builtin_constant_p (__x))                      \
00243              __v = GUINT64_SWAP_LE_BE_CONSTANT (__x);           \
00244            else                                                 \
00245              __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;"      \
00246                                    : "=r" (__v)                 \
00247                                    : "r" (__x));                \
00248            __v; }))
00249 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA64 (val))
00250 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA64 (val))
00251 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA64 (val))
00252 #  elif defined (__x86_64__)
00253 #    define GUINT32_SWAP_LE_BE_X86_64(val) \
00254        (__extension__                                           \
00255          ({ register guint32 __v, __x = ((guint32) (val));      \
00256             if (__builtin_constant_p (__x))                     \
00257               __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);          \
00258             else                                                \
00259              __asm__ ("bswapl %0"                               \
00260                       : "=r" (__v)                              \
00261                       : "0" (__x));                             \
00262             __v; }))
00263 #    define GUINT64_SWAP_LE_BE_X86_64(val) \
00264        (__extension__                                           \
00265         ({ register guint64 __v, __x = ((guint64) (val));       \
00266            if (__builtin_constant_p (__x))                      \
00267              __v = GUINT64_SWAP_LE_BE_CONSTANT (__x);           \
00268            else                                                 \
00269              __asm__ ("bswapq %0"                               \
00270                       : "=r" (__v)                              \
00271                       : "0" (__x));                             \
00272            __v; }))
00273      /* gcc seems to figure out optimal code for this on its own */
00274 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
00275 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86_64 (val))
00276 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86_64 (val))
00277 #  else /* generic gcc */
00278 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
00279 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
00280 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
00281 #  endif
00282 #else /* generic */
00283 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
00284 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
00285 #  define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
00286 #endif /* generic */
00287 
00288 #define GUINT16_SWAP_LE_PDP(val)        ((guint16) (val))
00289 #define GUINT16_SWAP_BE_PDP(val)        (GUINT16_SWAP_LE_BE (val))
00290 #define GUINT32_SWAP_LE_PDP(val)        ((guint32) ( \
00291     (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
00292     (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
00293 #define GUINT32_SWAP_BE_PDP(val)        ((guint32) ( \
00294     (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
00295     (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
00296 
00297 /* The G*_TO_?E() macros are defined in glibconfig.h.
00298  * The transformation is symmetric, so the FROM just maps to the TO.
00299  */
00300 #define GINT16_FROM_LE(val)     (GINT16_TO_LE (val))
00301 #define GUINT16_FROM_LE(val)    (GUINT16_TO_LE (val))
00302 #define GINT16_FROM_BE(val)     (GINT16_TO_BE (val))
00303 #define GUINT16_FROM_BE(val)    (GUINT16_TO_BE (val))
00304 #define GINT32_FROM_LE(val)     (GINT32_TO_LE (val))
00305 #define GUINT32_FROM_LE(val)    (GUINT32_TO_LE (val))
00306 #define GINT32_FROM_BE(val)     (GINT32_TO_BE (val))
00307 #define GUINT32_FROM_BE(val)    (GUINT32_TO_BE (val))
00308 
00309 #define GINT64_FROM_LE(val)     (GINT64_TO_LE (val))
00310 #define GUINT64_FROM_LE(val)    (GUINT64_TO_LE (val))
00311 #define GINT64_FROM_BE(val)     (GINT64_TO_BE (val))
00312 #define GUINT64_FROM_BE(val)    (GUINT64_TO_BE (val))
00313 
00314 #define GLONG_FROM_LE(val)      (GLONG_TO_LE (val))
00315 #define GULONG_FROM_LE(val)     (GULONG_TO_LE (val))
00316 #define GLONG_FROM_BE(val)      (GLONG_TO_BE (val))
00317 #define GULONG_FROM_BE(val)     (GULONG_TO_BE (val))
00318 
00319 #define GINT_FROM_LE(val)       (GINT_TO_LE (val))
00320 #define GUINT_FROM_LE(val)      (GUINT_TO_LE (val))
00321 #define GINT_FROM_BE(val)       (GINT_TO_BE (val))
00322 #define GUINT_FROM_BE(val)      (GUINT_TO_BE (val))
00323 
00324 
00325 /* Portable versions of host-network order stuff
00326  */
00327 #define g_ntohl(val) (GUINT32_FROM_BE (val))
00328 #define g_ntohs(val) (GUINT16_FROM_BE (val))
00329 #define g_htonl(val) (GUINT32_TO_BE (val))
00330 #define g_htons(val) (GUINT16_TO_BE (val))
00331 
00332 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
00333  *
00334  *        31 30           23 22            0
00335  * +--------+---------------+---------------+
00336  * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
00337  * +--------+---------------+---------------+
00338  * B0------------------->B1------->B2-->B3-->
00339  *
00340  * IEEE Standard 754 Double Precision Storage Format (gdouble):
00341  *
00342  *        63 62            52 51            32   31            0
00343  * +--------+----------------+----------------+ +---------------+
00344  * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
00345  * +--------+----------------+----------------+ +---------------+
00346  * B0--------------->B1---------->B2--->B3---->  B4->B5->B6->B7->
00347  */
00348 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
00349 typedef union  _GDoubleIEEE754  GDoubleIEEE754;
00350 typedef union  _GFloatIEEE754   GFloatIEEE754;
00351 #define G_IEEE754_FLOAT_BIAS    (127)
00352 #define G_IEEE754_DOUBLE_BIAS   (1023)
00353 /* multiply with base2 exponent to get base10 exponent (normal numbers) */
00354 #define G_LOG_2_BASE_10         (0.30102999566398119521)
00355 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
00356 union _GFloatIEEE754
00357 {
00358   gfloat v_float;
00359   struct {
00360     guint mantissa : 23;
00361     guint biased_exponent : 8;
00362     guint sign : 1;
00363   } mpn;
00364 };
00365 union _GDoubleIEEE754
00366 {
00367   gdouble v_double;
00368   struct {
00369     guint mantissa_low : 32;
00370     guint mantissa_high : 20;
00371     guint biased_exponent : 11;
00372     guint sign : 1;
00373   } mpn;
00374 };
00375 #elif G_BYTE_ORDER == G_BIG_ENDIAN
00376 union _GFloatIEEE754
00377 {
00378   gfloat v_float;
00379   struct {
00380     guint sign : 1;
00381     guint biased_exponent : 8;
00382     guint mantissa : 23;
00383   } mpn;
00384 };
00385 union _GDoubleIEEE754
00386 {
00387   gdouble v_double;
00388   struct {
00389     guint sign : 1;
00390     guint biased_exponent : 11;
00391     guint mantissa_high : 20;
00392     guint mantissa_low : 32;
00393   } mpn;
00394 };
00395 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
00396 #error unknown ENDIAN type
00397 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
00398 
00399 typedef struct _GTimeVal                GTimeVal;
00400 
00401 struct _GTimeVal
00402 {
00403   glong tv_sec;
00404   glong tv_usec;
00405 };
00406 
00407 G_END_DECLS
00408 
00409 /* We prefix variable declarations so they can
00410  * properly get exported in Windows DLLs.
00411  */
00412 #ifndef GLIB_VAR
00413 #  ifdef G_PLATFORM_WIN32
00414 #    ifdef GLIB_STATIC_COMPILATION
00415 #      define GLIB_VAR extern
00416 #    else /* !GLIB_STATIC_COMPILATION */
00417 #      ifdef GLIB_COMPILATION
00418 #        ifdef DLL_EXPORT
00419 #          define GLIB_VAR __declspec(dllexport)
00420 #        else /* !DLL_EXPORT */
00421 #          define GLIB_VAR extern
00422 #        endif /* !DLL_EXPORT */
00423 #      else /* !GLIB_COMPILATION */
00424 #        define GLIB_VAR extern __declspec(dllimport)
00425 #      endif /* !GLIB_COMPILATION */
00426 #    endif /* !GLIB_STATIC_COMPILATION */
00427 #  else /* !G_PLATFORM_WIN32 */
00428 #    define GLIB_VAR extern
00429 #  endif /* !G_PLATFORM_WIN32 */
00430 #endif /* GLIB_VAR */
00431 
00432 #endif /* __G_TYPES_H__ */