gtypes.h

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

Copyright © Nokia Corporation 2001-2008
Back to top