root/src/gauche/int64.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. ScmInt64
  2. ScmUInt64
  3. ScmInt64
  4. ScmUInt64

   1 /*
   2  * int64.h - auxilirary definition for 64bit integers
   3  *
   4  *   Copyright (c) 2004 Shiro Kawai, All rights reserved.
   5  * 
   6  *   Redistribution and use in source and binary forms, with or without
   7  *   modification, are permitted provided that the following conditions
   8  *   are met:
   9  * 
  10  *   1. Redistributions of source code must retain the above copyright
  11  *      notice, this list of conditions and the following disclaimer.
  12  *
  13  *   2. Redistributions in binary form must reproduce the above copyright
  14  *      notice, this list of conditions and the following disclaimer in the
  15  *      documentation and/or other materials provided with the distribution.
  16  *
  17  *   3. Neither the name of the authors nor the names of its contributors
  18  *      may be used to endorse or promote products derived from this
  19  *      software without specific prior written permission.
  20  *
  21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  27  *   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28  *   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  *
  33  *  $Id: int64.h,v 1.5 2004/11/05 10:33:52 shirok Exp $
  34  */
  35 
  36 /* Some Scheme API needs to deal with 64bit signed/unsigned integer
  37    (such as srfi-4 vectors and binary I/O routines.)
  38    This file defines some macros to help writing code without
  39    concerning about how the hardware/compiler supports int64.
  40 
  41    This header defines the following types and macros.
  42 
  43    [type] ScmInt64     64-bit signed integer.
  44    [type] ScmUInt64    64-bit unsigned integer.
  45    [type] ScmInt32     32-bit signed integer.
  46    [type] ScmUInt32    32-bit signed integer.
  47 
  48    Based on these types, gauche.h declares some conversion functions
  49    that can be used regardless of the representation of 64bit integer
  50    on the target machine.
  51 
  52    To box 64bit integer as ScmObj, use Scm_MakeInteger64 and
  53    Scm_MakeIntegerU64.
  54 
  55    To unbox 64bit integer from ScmObj, use Scm_GetInteger64 and
  56    Scm_GetIntegerU64.
  57 */
  58 
  59 #ifndef GAUCHE_INT64_H
  60 #define GAUCHE_INT64_H
  61 
  62 /* assuming gauche/config.h is read. */
  63 
  64 #ifdef HAVE_STDINT_H
  65 #include <stdint.h>
  66 #endif
  67 #ifdef HAVE_INTTYPES_H
  68 #include <inttypes.h>
  69 #endif
  70 
  71 /* typedefs ScmInt64 and ScmUInt64 to appropriate type */
  72 
  73 #if defined(HAVE_INT64_T) && defined(HAVE_UINT64_T)
  74 /* If the system has int64_t, it is the best way to use it.
  75    Even some of ILP32 systems have int64_t. */
  76 typedef int64_t  ScmInt64;
  77 typedef uint64_t ScmUInt64;
  78 #elif SIZEOF_LONG >= 8
  79 /* NB: this would fail if sizeof(long) becomes 16; but such newer systems
  80    are likely to have int64_t defined. */
  81 typedef long   ScmInt64;
  82 typedef u_long ScmUInt64;
  83 #else  /* SIZEOF_LONG == 4 */
  84 /* This is the fallback. */
  85 #define SCM_EMULATE_INT64 1
  86 #ifdef WORDS_BIGENDIAN
  87 typedef struct {
  88     unsigned long hi;
  89     unsigned long lo;
  90 } ScmInt64, ScmUInt64;
  91 #else  /* !WORDS_BIGENDIAN */
  92 typedef struct {
  93     unsigned long lo;
  94     unsigned long hi;
  95 } ScmInt64, ScmUInt64;
  96 #endif /* !WORDS_BIGENDIAN */
  97 #endif /* SIZEOF_LONG == 4 */
  98 
  99 /* Used internally to set up 64bit integer values */
 100 #if SIZEOF_LONG == 4
 101 #if SCM_EMULATE_INT64
 102 #define SCM_SET_INT64_MAX(v64)  \
 103     ((v64).hi = LONG_MAX, (v64).lo = ULONG_MAX)
 104 #define SCM_SET_INT64_MIN(v64)  \
 105     ((v64).hi = (u_long)LONG_MAX + 1, (v64).lo = 0)
 106 #define SCM_SET_UINT64_MAX(v64) \
 107     ((v64).hi = ULONG_MAX, (v64).lo = ULONG_MAX)
 108 #define SCM_SET_INT64_ZERO(v64) \
 109     ((v64).hi = (v64).lo = 0)
 110 #else  /* !SCM_EMULATE_INT64 */
 111 #define SCM_SET_INT64_MAX(v64)  \
 112     ((v64) = ((((int64_t)LONG_MAX)<<32) + (int64_t)ULONG_MAX))
 113 #define SCM_SET_INT64_MIN(v64)  \
 114     ((v64) = (((int64_t)LONG_MAX + 1) << 32))
 115 #define SCM_SET_UINT64_MAX(v64) \
 116     ((v64) = ((((uint64_t)ULONG_MAX) << 32) + (uint64_t)ULONG_MAX))
 117 #define SCM_SET_INT64_ZERO(v64) \
 118     ((v64) = 0)
 119 #endif /* !SCM_EMULATE_INT64 */
 120 #elif  SIZEOF_LONG == 8
 121 #define SCM_SET_INT64_MAX(v64)    ((v64) = LONG_MAX)
 122 #define SCM_SET_INT64_MIN(v64)    ((v64) = LONG_MIN)
 123 #define SCM_SET_UINT64_MAX(v64)   ((v64) = ULONG_MAX)
 124 #define SCM_SET_INT64_ZERO(v64)   ((v64) = 0)
 125 #endif /* SIZEOF_LONG == 4 or 8 */
 126 
 127 /* ScmInt32 and ScmUInt32 can be used when one needs exactly 32bit int */
 128 
 129 #if defined(HAVE_INT32_T) && defined(HAVE_UINT32_T)
 130 typedef int32_t  ScmInt32;
 131 typedef uint32_t ScmUInt32;
 132 #elif SIZEOF_INT == 4
 133 typedef int   ScmInt32;
 134 typedef u_int ScmUInt32;
 135 #elif SIZEOF_LONG == 4
 136 typedef long   ScmInt32;
 137 typedef u_long ScmUInt32;
 138 #else
 139 #error system does not have 32bit integer
 140 #endif
 141 
 142 /* Some macros that can be used without concerning underlying implementation
 143    of int64. */
 144 
 145 /* SCM_INT64_TO_DOUBLE  - coerce int64 value to double */
 146 #if SCM_EMULATE_INT64
 147 #define SCM_INT64_TO_DOUBLE(v64) \
 148     ((v64.hi)*4294967296.0 + (double)(v64.lo))
 149 #else /* !SCM_EMULATE_INT64 */
 150 #define SCM_INT64_TO_DOUBLE(v64)   ((double)(v64))
 151 #endif
 152 
 153 
 154 
 155 
 156 #endif /*GAUCHE_INT64_H*/

/* [<][>][^][v][top][bottom][index][help] */