2004-07-29 13:54:15 +00:00
|
|
|
/* This code in included by number.s.c to generate integer conversion
|
|
|
|
|
functions like scm_to_int and scm_from_int. It is only for
|
|
|
|
|
unsigned types, see conv-integer.i.c for the signed variant.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* You need to define the following macros before including this
|
|
|
|
|
template. They are undefined at the end of this file to giove a
|
|
|
|
|
clean slate for the next inclusion.
|
|
|
|
|
|
|
|
|
|
TYPE - the integral type to be converted
|
|
|
|
|
TYPE_MIN - the smallest representable number of TYPE, typically 0.
|
|
|
|
|
TYPE_MAX - the largest representable number of TYPE
|
|
|
|
|
SIZEOF_TYPE - the size of TYPE, equal to "sizeof (TYPE)" but
|
|
|
|
|
in a form that can be computed by the preprocessor.
|
|
|
|
|
When this number is 0, the preprocessor is not used
|
|
|
|
|
to select which code to compile; the most general
|
|
|
|
|
code is always used.
|
|
|
|
|
|
|
|
|
|
SCM_TO_TYPE_PROTO(arg), SCM_FROM_TYPE_PROTO(arg)
|
|
|
|
|
- These two macros should expand into the prototype
|
|
|
|
|
for the two defined functions, without the return
|
|
|
|
|
type.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
* conv-integer.i.c, conv-uinteger.i.c: New files, used to generate
the functions below.
* numbers.c, numbers.h (scm_to_int8, scm_to_uint8, scm_to_int16,
scm_to_uint16, scm_to_int32, scm_to_uint32, scm_to_int64,
scm_to_uint64, scm_from_int8, scm_from_uint8, scm_from_int16,
scm_from_uint16, scm_from_int32, scm_from_uint32, scm_from_int64,
scm_from_uint64): Turned from macros into proper functions.
(scm_to_signed_integer, scm_to_unsigned_integer,
scm_from_signed_integer, scm_from_unsigned_integer): Generate via
conv-integer.i.c and conv-uinteger.i.c, as well.
2004-07-29 13:42:50 +00:00
|
|
|
TYPE
|
|
|
|
|
SCM_TO_TYPE_PROTO (SCM val)
|
|
|
|
|
{
|
|
|
|
|
if (SCM_I_INUMP (val))
|
|
|
|
|
{
|
|
|
|
|
scm_t_signed_bits n = SCM_I_INUM (val);
|
|
|
|
|
if (n >= 0
|
|
|
|
|
&& ((scm_t_uintmax)n) >= TYPE_MIN && ((scm_t_uintmax)n) <= TYPE_MAX)
|
|
|
|
|
return n;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
out_of_range:
|
2004-10-19 15:59:56 +00:00
|
|
|
scm_i_range_error (val,
|
|
|
|
|
scm_from_unsigned_integer (TYPE_MIN),
|
|
|
|
|
scm_from_unsigned_integer (TYPE_MAX));
|
* conv-integer.i.c, conv-uinteger.i.c: New files, used to generate
the functions below.
* numbers.c, numbers.h (scm_to_int8, scm_to_uint8, scm_to_int16,
scm_to_uint16, scm_to_int32, scm_to_uint32, scm_to_int64,
scm_to_uint64, scm_from_int8, scm_from_uint8, scm_from_int16,
scm_from_uint16, scm_from_int32, scm_from_uint32, scm_from_int64,
scm_from_uint64): Turned from macros into proper functions.
(scm_to_signed_integer, scm_to_unsigned_integer,
scm_from_signed_integer, scm_from_unsigned_integer): Generate via
conv-integer.i.c and conv-uinteger.i.c, as well.
2004-07-29 13:42:50 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (SCM_BIGP (val))
|
|
|
|
|
{
|
|
|
|
|
if (TYPE_MAX <= SCM_MOST_POSITIVE_FIXNUM)
|
|
|
|
|
goto out_of_range;
|
|
|
|
|
else if (TYPE_MAX <= ULONG_MAX)
|
|
|
|
|
{
|
|
|
|
|
if (mpz_fits_ulong_p (SCM_I_BIG_MPZ (val)))
|
|
|
|
|
{
|
|
|
|
|
unsigned long n = mpz_get_ui (SCM_I_BIG_MPZ (val));
|
|
|
|
|
#if SIZEOF_TYPE != 0 && SIZEOF_TYPE > SCM_SIZEOF_LONG
|
|
|
|
|
return n;
|
|
|
|
|
#else
|
2009-08-20 21:33:49 -07:00
|
|
|
|
|
|
|
|
if (n >= TYPE_MIN && n <= TYPE_MAX)
|
|
|
|
|
return n;
|
|
|
|
|
else
|
|
|
|
|
goto out_of_range;
|
|
|
|
|
|
* conv-integer.i.c, conv-uinteger.i.c: New files, used to generate
the functions below.
* numbers.c, numbers.h (scm_to_int8, scm_to_uint8, scm_to_int16,
scm_to_uint16, scm_to_int32, scm_to_uint32, scm_to_int64,
scm_to_uint64, scm_from_int8, scm_from_uint8, scm_from_int16,
scm_from_uint16, scm_from_int32, scm_from_uint32, scm_from_int64,
scm_from_uint64): Turned from macros into proper functions.
(scm_to_signed_integer, scm_to_unsigned_integer,
scm_from_signed_integer, scm_from_unsigned_integer): Generate via
conv-integer.i.c and conv-uinteger.i.c, as well.
2004-07-29 13:42:50 +00:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
goto out_of_range;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
scm_t_uintmax n;
|
|
|
|
|
size_t count;
|
|
|
|
|
|
|
|
|
|
if (mpz_sgn (SCM_I_BIG_MPZ (val)) < 0)
|
|
|
|
|
goto out_of_range;
|
|
|
|
|
|
|
|
|
|
if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2)
|
|
|
|
|
> CHAR_BIT*sizeof (TYPE))
|
|
|
|
|
goto out_of_range;
|
|
|
|
|
|
|
|
|
|
mpz_export (&n, &count, 1, sizeof (TYPE), 0, 0, SCM_I_BIG_MPZ (val));
|
|
|
|
|
|
|
|
|
|
if (n >= TYPE_MIN && n <= TYPE_MAX)
|
|
|
|
|
return n;
|
2009-08-20 21:33:49 -07:00
|
|
|
else
|
|
|
|
|
goto out_of_range;
|
|
|
|
|
|
* conv-integer.i.c, conv-uinteger.i.c: New files, used to generate
the functions below.
* numbers.c, numbers.h (scm_to_int8, scm_to_uint8, scm_to_int16,
scm_to_uint16, scm_to_int32, scm_to_uint32, scm_to_int64,
scm_to_uint64, scm_from_int8, scm_from_uint8, scm_from_int16,
scm_from_uint16, scm_from_int32, scm_from_uint32, scm_from_int64,
scm_from_uint64): Turned from macros into proper functions.
(scm_to_signed_integer, scm_to_unsigned_integer,
scm_from_signed_integer, scm_from_unsigned_integer): Generate via
conv-integer.i.c and conv-uinteger.i.c, as well.
2004-07-29 13:42:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
scm_wrong_type_arg_msg (NULL, 0, val, "exact integer");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SCM
|
|
|
|
|
SCM_FROM_TYPE_PROTO (TYPE val)
|
|
|
|
|
{
|
|
|
|
|
#if SIZEOF_TYPE != 0 && SIZEOF_TYPE < SIZEOF_SCM_T_BITS
|
|
|
|
|
return SCM_I_MAKINUM (val);
|
|
|
|
|
#else
|
|
|
|
|
if (SCM_POSFIXABLE (val))
|
|
|
|
|
return SCM_I_MAKINUM (val);
|
|
|
|
|
else if (val <= ULONG_MAX)
|
* discouraged.h, discouraged.c: New files.
* deprecated.h (SCM_FALSEP, SCM_NFALSEP, SCM_BOOLP, SCM_EQ_P,
SCM_NEGATE_BOOL, SCM_BOOL, SCM_BOOT_NOT): Promoted from being
deprecated to being discouraged by moving to discouraged.h.
* numbers.h, numbers.c, discouraged.h, discouraged.c
(scm_short2num, scm_ushort2num, scm_int2num, scm_uint2num,
scm_long2num, scm_ulong2num, scm_size2num, scm_ptrdiff2num,
scm_num2short, scm_num2ushort, scm_num2int, scm_num2uint,
scm_num2long, scm_num2ulong, scm_num2size, scm_num2ptrdiff,
scm_long_long2num, scm_ulong_long2num, scm_num2long_long,
scm_num2ulong_long): Discouraged by moving to discouraged.h and
discouraged.c and reimplementing in terms of scm_from_* and
scm_to_*.
* numbers.h, numbers.c: Removed GUILE_DEBUG code.
(scm_i_short2big, scm_i_ushort2big, scm_i_int2big, scm_i_uint2big,
scm_i_size2big, scm_i_ptrdiff2big): Removed.
(scm_i_long2big, scm_i_ulong2big): New, explicit definitions.
* conv-integer.i.c, conv-uinteger.i.c: Use them instead of
explicit code.
2004-08-02 15:57:04 +00:00
|
|
|
return scm_i_ulong2big (val);
|
* conv-integer.i.c, conv-uinteger.i.c: New files, used to generate
the functions below.
* numbers.c, numbers.h (scm_to_int8, scm_to_uint8, scm_to_int16,
scm_to_uint16, scm_to_int32, scm_to_uint32, scm_to_int64,
scm_to_uint64, scm_from_int8, scm_from_uint8, scm_from_int16,
scm_from_uint16, scm_from_int32, scm_from_uint32, scm_from_int64,
scm_from_uint64): Turned from macros into proper functions.
(scm_to_signed_integer, scm_to_unsigned_integer,
scm_from_signed_integer, scm_from_unsigned_integer): Generate via
conv-integer.i.c and conv-uinteger.i.c, as well.
2004-07-29 13:42:50 +00:00
|
|
|
else
|
|
|
|
|
{
|
2016-06-23 14:57:50 +02:00
|
|
|
SCM z = make_bignum ();
|
* conv-integer.i.c, conv-uinteger.i.c: New files, used to generate
the functions below.
* numbers.c, numbers.h (scm_to_int8, scm_to_uint8, scm_to_int16,
scm_to_uint16, scm_to_int32, scm_to_uint32, scm_to_int64,
scm_to_uint64, scm_from_int8, scm_from_uint8, scm_from_int16,
scm_from_uint16, scm_from_int32, scm_from_uint32, scm_from_int64,
scm_from_uint64): Turned from macros into proper functions.
(scm_to_signed_integer, scm_to_unsigned_integer,
scm_from_signed_integer, scm_from_unsigned_integer): Generate via
conv-integer.i.c and conv-uinteger.i.c, as well.
2004-07-29 13:42:50 +00:00
|
|
|
mpz_init (SCM_I_BIG_MPZ (z));
|
|
|
|
|
mpz_import (SCM_I_BIG_MPZ (z), 1, 1, sizeof (TYPE), 0, 0, &val);
|
|
|
|
|
return z;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#undef TYPE
|
|
|
|
|
#undef TYPE_MIN
|
|
|
|
|
#undef TYPE_MAX
|
|
|
|
|
#undef SIZEOF_TYPE
|
|
|
|
|
#undef SCM_TO_TYPE_PROTO
|
|
|
|
|
#undef SCM_FROM_TYPE_PROTO
|
|
|
|
|
|