(SCM_EVALIM, SCM_EVALIM2, SCM_XEVAL, SCM_XEVALCAR): Renamed to SCM_I_*
[bpt/guile.git] / libguile / conv-uinteger.i.c
CommitLineData
9d3ebce4
MV
1/* This code in included by number.s.c to generate integer conversion
2 functions like scm_to_int and scm_from_int. It is only for
3 unsigned types, see conv-integer.i.c for the signed variant.
4*/
5
6/* You need to define the following macros before including this
7 template. They are undefined at the end of this file to giove a
8 clean slate for the next inclusion.
9
10 TYPE - the integral type to be converted
11 TYPE_MIN - the smallest representable number of TYPE, typically 0.
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
17 code is always used.
18
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
22 type.
23
24*/
25
bfd7932e
MV
26TYPE
27SCM_TO_TYPE_PROTO (SCM val)
28{
29 if (SCM_I_INUMP (val))
30 {
31 scm_t_signed_bits n = SCM_I_INUM (val);
32 if (n >= 0
33 && ((scm_t_uintmax)n) >= TYPE_MIN && ((scm_t_uintmax)n) <= TYPE_MAX)
34 return n;
35 else
36 {
37 out_of_range:
38 scm_out_of_range (NULL, val);
39 return 0;
40 }
41 }
42 else if (SCM_BIGP (val))
43 {
44 if (TYPE_MAX <= SCM_MOST_POSITIVE_FIXNUM)
45 goto out_of_range;
46 else if (TYPE_MAX <= ULONG_MAX)
47 {
48 if (mpz_fits_ulong_p (SCM_I_BIG_MPZ (val)))
49 {
50 unsigned long n = mpz_get_ui (SCM_I_BIG_MPZ (val));
51#if SIZEOF_TYPE != 0 && SIZEOF_TYPE > SCM_SIZEOF_LONG
52 return n;
53#else
54 if (n >= TYPE_MIN && n <= TYPE_MAX)
55 return n;
56 else
57 goto out_of_range;
58#endif
59 }
60 else
61 goto out_of_range;
62 }
63 else
64 {
65 scm_t_uintmax n;
66 size_t count;
67
68 if (mpz_sgn (SCM_I_BIG_MPZ (val)) < 0)
69 goto out_of_range;
70
71 if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2)
72 > CHAR_BIT*sizeof (TYPE))
73 goto out_of_range;
74
75 mpz_export (&n, &count, 1, sizeof (TYPE), 0, 0, SCM_I_BIG_MPZ (val));
76
77 if (n >= TYPE_MIN && n <= TYPE_MAX)
78 return n;
79 else
80 goto out_of_range;
81 }
82 }
83 else
84 {
85 scm_wrong_type_arg_msg (NULL, 0, val, "exact integer");
86 return 0;
87 }
88}
89
90SCM
91SCM_FROM_TYPE_PROTO (TYPE val)
92{
93#if SIZEOF_TYPE != 0 && SIZEOF_TYPE < SIZEOF_SCM_T_BITS
94 return SCM_I_MAKINUM (val);
95#else
96 if (SCM_POSFIXABLE (val))
97 return SCM_I_MAKINUM (val);
98 else if (val <= ULONG_MAX)
99 {
100 SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0);
101 mpz_init_set_ui (SCM_I_BIG_MPZ (z), val);
102 return z;
103 }
104 else
105 {
106 SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0);
107 mpz_init (SCM_I_BIG_MPZ (z));
108 mpz_import (SCM_I_BIG_MPZ (z), 1, 1, sizeof (TYPE), 0, 0, &val);
109 return z;
110 }
111#endif
112}
113
114#undef TYPE
115#undef TYPE_MIN
116#undef TYPE_MAX
117#undef SIZEOF_TYPE
118#undef SCM_TO_TYPE_PROTO
119#undef SCM_FROM_TYPE_PROTO
120