1 /* This code in included by numbers.c to generate integer conversion
2 functions like scm_to_int and scm_from_int. It is only for signed
3 types, see conv-uinteger.i.c for the unsigned variant.
6 /* You need to define the following macros before including this
7 template. They are undefined at the end of this file to give a
8 clean slate for the next inclusion.
10 TYPE - the integral type to be converted
11 TYPE_MIN - the smallest representable number of TYPE
12 TYPE_MAX - the largest representable number of TYPE
13 SIZEOF_TYPE - the size of TYPE, equal to "sizeof (TYPE)" but
14 in a form that can be computed by the preprocessor.
15 When this number is 0, the preprocessor is not used
16 to select which code to compile; the most general
19 SCM_TO_TYPE_PROTO(arg), SCM_FROM_TYPE_PROTO(arg)
20 - These two macros should expand into the prototype
21 for the two defined functions, without the return
27 SCM_TO_TYPE_PROTO (SCM val)
29 if (SCM_I_INUMP (val))
31 scm_t_signed_bits n = SCM_I_INUM (val);
32 #if SIZEOF_TYPE != 0 && SIZEOF_TYPE > SIZEOF_SCM_T_BITS
35 if (n >= TYPE_MIN && n <= TYPE_MAX)
43 else if (SCM_BIGP (val))
45 if (TYPE_MIN >= SCM_MOST_NEGATIVE_FIXNUM
46 && TYPE_MAX <= SCM_MOST_POSITIVE_FIXNUM)
48 else if (TYPE_MIN >= LONG_MIN && TYPE_MAX <= LONG_MAX)
50 if (mpz_fits_slong_p (SCM_I_BIG_MPZ (val)))
52 long n = mpz_get_si (SCM_I_BIG_MPZ (val));
53 #if SIZEOF_TYPE != 0 && SIZEOF_TYPE > SCM_SIZEOF_LONG
56 if (n >= TYPE_MIN && n <= TYPE_MAX)
70 if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2)
71 > CHAR_BIT*sizeof (scm_t_uintmax))
74 mpz_export (&n, &count, 1, sizeof (scm_t_uintmax), 0, 0,
77 if (mpz_sgn (SCM_I_BIG_MPZ (val)) >= 0)
89 if (n >= TYPE_MIN && n <= TYPE_MAX)
94 scm_i_range_error (val,
95 scm_from_signed_integer (TYPE_MIN),
96 scm_from_signed_integer (TYPE_MAX));
103 scm_wrong_type_arg_msg (NULL, 0, val, "exact integer");
109 SCM_FROM_TYPE_PROTO (TYPE val)
111 #if SIZEOF_TYPE != 0 && SIZEOF_TYPE < SIZEOF_SCM_T_BITS
112 return SCM_I_MAKINUM (val);
114 if (SCM_FIXABLE (val))
115 return SCM_I_MAKINUM (val);
116 else if (val >= LONG_MIN && val <= LONG_MAX)
117 return scm_i_long2big (val);
120 SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0);
121 mpz_init (SCM_I_BIG_MPZ (z));
125 mpz_import (SCM_I_BIG_MPZ (z), 1, 1, sizeof (TYPE), 0, 0,
127 mpz_neg (SCM_I_BIG_MPZ (z), SCM_I_BIG_MPZ (z));
130 mpz_import (SCM_I_BIG_MPZ (z), 1, 1, sizeof (TYPE), 0, 0,
142 #undef SCM_TO_TYPE_PROTO
143 #undef SCM_FROM_TYPE_PROTO