1 /* this file is #include'd (many times) by numbers.c */
5 #define UNSIGNED_ITYPE ITYPE
7 #define UNSIGNED_ITYPE unsigned ITYPE
11 #define UNSIGNED_ITYPE_MAX (~((UNSIGNED_ITYPE)0))
14 #define SIZEOF_ITYPE (2*SIZEOF_SCM_T_BITS)
18 NUM2INTEGRAL (SCM num
, unsigned long int pos
, const char *s_caller
)
23 scm_t_signed_bits n
= SCM_INUM (num
);
27 scm_out_of_range (s_caller
, num
);
30 if (sizeof (ITYPE
) >= sizeof (scm_t_signed_bits
))
31 /* can't fit anything too big for this type in an inum
35 { /* an inum can be out of range, so check */
37 scm_out_of_range (s_caller
, num
);
42 else if (SCM_BIGP (num
))
44 #if SIZEOF_ITYPE >= SIZEOF_SCM_T_BITS
46 UNSIGNED_ITYPE pos_res
= 0;
50 if (SCM_BIGSIGN (num
))
51 scm_out_of_range (s_caller
, num
);
54 for (l
= SCM_NUMDIGS (num
); l
--;)
56 if (pos_res
> SCM_BIGDN (UNSIGNED_ITYPE_MAX
))
57 scm_out_of_range (s_caller
, num
);
58 pos_res
= SCM_I_BIGUP (ITYPE
, pos_res
) + SCM_BDIGITS (num
)[l
];
64 if (SCM_BIGSIGN (num
))
66 ITYPE res
= -((ITYPE
)pos_res
);
70 scm_out_of_range (s_caller
, num
);
74 ITYPE res
= (ITYPE
)pos_res
;
78 scm_out_of_range (s_caller
, num
);
82 #else /* SIZEOF_ITYPE >= SIZEOF_SCM_T_BITS */
83 scm_out_of_range (s_caller
, num
);
88 scm_wrong_type_arg (s_caller
, pos
, num
);
92 INTEGRAL2NUM (ITYPE n
)
94 /* If we know the size of the type, determine at compile time
95 whether we need to perform the FIXABLE test or not. This is not
96 done to get more optimal code out of the compiler (it can figure
97 this out on its own already), but to avoid a spurious warning.
98 If we don't know the size, assume that the test must be done.
101 #if SIZEOF_ITYPE >= SIZEOF_SCM_T_BITS
105 if (SCM_POSFIXABLE (n
))
108 return SCM_MAKINUM ((scm_t_signed_bits
) n
);
111 return INTEGRAL2BIG (n
);
113 return scm_make_real ((double) n
);
120 INTEGRAL2BIG (ITYPE n
)
124 unsigned int n_digits
;
136 /* If n is still negative here, it must be the minimum value of the
137 type (assuming twos-complement, but we are tied to that anyway).
138 If this is the case, we can not count the number of digits by
139 right-shifting n until it is zero.
145 (sizeof (ITYPE
) + sizeof (SCM_BIGDIG
) - 1) / sizeof (SCM_BIGDIG
);
151 for (tn
= n
, n_digits
= 0;
153 ++n_digits
, tn
= SCM_BIGDN (tn
))
158 res
= scm_i_mkbig (n_digits
, neg_p
);
159 digits
= SCM_BDIGITS (res
);
163 digits
[i
++] = SCM_BIGLO (n
);
181 #undef UNSIGNED_ITYPE
182 #undef UNSIGNED_ITYPE_MAX