065dc193632f3469b72cd7a1ab67202d7387b642
1 /* this file is #include'd (many times) by numbers.c */
9 #define UNSIGNED_ITYPE ITYPE
11 #define UNSIGNED_ITYPE unsigned ITYPE
15 #define UNSIGNED_ITYPE_MAX (~((UNSIGNED_ITYPE)0))
18 #define SIZEOF_ITYPE (2*SIZEOF_SCM_T_BITS)
22 NUM2INTEGRAL (SCM num
, unsigned long int pos
, const char *s_caller
)
27 scm_t_signed_bits n
= SCM_INUM (num
);
31 scm_out_of_range (s_caller
, num
);
34 #if SIZEOF_ITYPE >= SIZEOF_SCM_T_BITS
35 /* the target type is large enough to hold any possible inum */
38 /* an inum can be out of range, so check */
40 /* n is known to be >= 0 */
41 if ((scm_t_bits
) n
> UNSIGNED_ITYPE_MAX
)
42 scm_out_of_range (s_caller
, num
);
45 scm_out_of_range (s_caller
, num
);
48 #endif /* SIZEOF_ITYPE >= SIZEOF_SCM_T_BITS */
50 else if (SCM_BIGP (num
))
52 #if SIZEOF_ITYPE >= SIZEOF_SCM_T_BITS
54 UNSIGNED_ITYPE pos_res
= 0;
58 if (SCM_BIGSIGN (num
))
59 scm_out_of_range (s_caller
, num
);
62 for (l
= SCM_NUMDIGS (num
); l
--;)
64 if (pos_res
> SCM_BIGDN (UNSIGNED_ITYPE_MAX
))
65 scm_out_of_range (s_caller
, num
);
66 pos_res
= SCM_I_BIGUP (ITYPE
, pos_res
) + SCM_BDIGITS (num
)[l
];
72 if (SCM_BIGSIGN (num
))
74 ITYPE res
= -((ITYPE
)pos_res
);
78 scm_out_of_range (s_caller
, num
);
82 ITYPE res
= (ITYPE
)pos_res
;
86 scm_out_of_range (s_caller
, num
);
90 #else /* SIZEOF_ITYPE >= SIZEOF_SCM_T_BITS */
91 scm_out_of_range (s_caller
, num
);
96 scm_wrong_type_arg (s_caller
, pos
, num
);
100 INTEGRAL2NUM (ITYPE n
)
102 /* If we know the size of the type, determine at compile time
103 whether we need to perform the FIXABLE test or not. This is not
104 done to get more optimal code out of the compiler (it can figure
105 this out on its own already), but to avoid a spurious warning.
106 If we don't know the size, assume that the test must be done.
109 #if SIZEOF_ITYPE >= SIZEOF_SCM_T_BITS
113 if (SCM_POSFIXABLE (n
))
116 return SCM_MAKINUM ((scm_t_signed_bits
) n
);
119 return INTEGRAL2BIG (n
);
121 return scm_make_real ((double) n
);
128 INTEGRAL2BIG (ITYPE n
)
132 unsigned int n_digits
;
144 /* If n is still negative here, it must be the minimum value of the
145 type (assuming twos-complement, but we are tied to that anyway).
146 If this is the case, we can not count the number of digits by
147 right-shifting n until it is zero.
153 (sizeof (ITYPE
) + sizeof (SCM_BIGDIG
) - 1) / sizeof (SCM_BIGDIG
);
159 for (tn
= n
, n_digits
= 0;
161 ++n_digits
, tn
= SCM_BIGDN (tn
))
166 res
= scm_i_mkbig (n_digits
, neg_p
);
167 digits
= SCM_BDIGITS (res
);
171 digits
[i
++] = SCM_BIGLO (n
);
189 #undef UNSIGNED_ITYPE
190 #undef UNSIGNED_ITYPE_MAX