mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-17 01:00:20 +02:00
Implement centered-quotient with new integer lib
* libguile/integers.c (scm_integer_centered_quotient_ii) (scm_integer_centered_quotient_iz, scm_integer_centered_quotient_zi) (scm_integer_centered_quotient_zz): New internal functions. * libguile/integers.h: Declare internal functions. * libguile/numbers.c (scm_centered_quotient): Use the new functions. (scm_i_bigint_centered_quotient): Remove unused helper.
This commit is contained in:
parent
f8a92773ac
commit
0ccdf06c81
3 changed files with 144 additions and 138 deletions
|
@ -2111,7 +2111,6 @@ scm_i_exact_rational_truncate_divide (SCM x, SCM y, SCM *qp, SCM *rp)
|
|||
}
|
||||
|
||||
static SCM scm_i_inexact_centered_quotient (double x, double y);
|
||||
static SCM scm_i_bigint_centered_quotient (SCM x, SCM y);
|
||||
static SCM scm_i_exact_rational_centered_quotient (SCM x, SCM y);
|
||||
|
||||
SCM_PRIMITIVE_GENERIC (scm_centered_quotient, "centered-quotient", 2, 0, 0,
|
||||
|
@ -2129,58 +2128,16 @@ SCM_PRIMITIVE_GENERIC (scm_centered_quotient, "centered-quotient", 2, 0, 0,
|
|||
"@end lisp")
|
||||
#define FUNC_NAME s_scm_centered_quotient
|
||||
{
|
||||
if (SCM_LIKELY (SCM_I_INUMP (x)))
|
||||
if (SCM_I_INUMP (x))
|
||||
{
|
||||
scm_t_inum xx = SCM_I_INUM (x);
|
||||
if (SCM_LIKELY (SCM_I_INUMP (y)))
|
||||
{
|
||||
scm_t_inum yy = SCM_I_INUM (y);
|
||||
if (SCM_UNLIKELY (yy == 0))
|
||||
scm_num_overflow (s_scm_centered_quotient);
|
||||
else
|
||||
{
|
||||
scm_t_inum qq = xx / yy;
|
||||
scm_t_inum rr = xx % yy;
|
||||
if (SCM_LIKELY (xx > 0))
|
||||
{
|
||||
if (SCM_LIKELY (yy > 0))
|
||||
{
|
||||
if (rr >= (yy + 1) / 2)
|
||||
qq++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rr >= (1 - yy) / 2)
|
||||
qq--;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SCM_LIKELY (yy > 0))
|
||||
{
|
||||
if (rr < -yy / 2)
|
||||
qq--;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rr < yy / 2)
|
||||
qq++;
|
||||
}
|
||||
}
|
||||
if (SCM_LIKELY (SCM_FIXABLE (qq)))
|
||||
return SCM_I_MAKINUM (qq);
|
||||
else
|
||||
return scm_i_inum2big (qq);
|
||||
}
|
||||
}
|
||||
if (SCM_I_INUMP (y))
|
||||
return scm_integer_centered_quotient_ii (SCM_I_INUM (x),
|
||||
SCM_I_INUM (y));
|
||||
else if (SCM_BIGP (y))
|
||||
{
|
||||
/* Pass a denormalized bignum version of x (even though it
|
||||
can fit in a fixnum) to scm_i_bigint_centered_quotient */
|
||||
return scm_i_bigint_centered_quotient (scm_i_long2big (xx), y);
|
||||
}
|
||||
return scm_integer_centered_quotient_iz (SCM_I_INUM (x), y);
|
||||
else if (SCM_REALP (y))
|
||||
return scm_i_inexact_centered_quotient (xx, SCM_REAL_VALUE (y));
|
||||
return scm_i_inexact_centered_quotient (SCM_I_INUM (x),
|
||||
SCM_REAL_VALUE (y));
|
||||
else if (SCM_FRACTIONP (y))
|
||||
return scm_i_exact_rational_centered_quotient (x, y);
|
||||
else
|
||||
|
@ -2189,44 +2146,10 @@ SCM_PRIMITIVE_GENERIC (scm_centered_quotient, "centered-quotient", 2, 0, 0,
|
|||
}
|
||||
else if (SCM_BIGP (x))
|
||||
{
|
||||
if (SCM_LIKELY (SCM_I_INUMP (y)))
|
||||
{
|
||||
scm_t_inum yy = SCM_I_INUM (y);
|
||||
if (SCM_UNLIKELY (yy == 0))
|
||||
scm_num_overflow (s_scm_centered_quotient);
|
||||
else if (SCM_UNLIKELY (yy == 1))
|
||||
return x;
|
||||
else
|
||||
{
|
||||
SCM q = scm_i_mkbig ();
|
||||
scm_t_inum rr;
|
||||
/* Arrange for rr to initially be non-positive,
|
||||
because that simplifies the test to see
|
||||
if it is within the needed bounds. */
|
||||
if (yy > 0)
|
||||
{
|
||||
rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q),
|
||||
SCM_I_BIG_MPZ (x), yy);
|
||||
scm_remember_upto_here_1 (x);
|
||||
if (rr < -yy / 2)
|
||||
mpz_sub_ui (SCM_I_BIG_MPZ (q),
|
||||
SCM_I_BIG_MPZ (q), 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q),
|
||||
SCM_I_BIG_MPZ (x), -yy);
|
||||
scm_remember_upto_here_1 (x);
|
||||
mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
|
||||
if (rr < yy / 2)
|
||||
mpz_add_ui (SCM_I_BIG_MPZ (q),
|
||||
SCM_I_BIG_MPZ (q), 1);
|
||||
}
|
||||
return scm_i_normbig (q);
|
||||
}
|
||||
}
|
||||
if (SCM_I_INUMP (y))
|
||||
return scm_integer_centered_quotient_zi (x, SCM_I_INUM (y));
|
||||
else if (SCM_BIGP (y))
|
||||
return scm_i_bigint_centered_quotient (x, y);
|
||||
return scm_integer_centered_quotient_zz (x, y);
|
||||
else if (SCM_REALP (y))
|
||||
return scm_i_inexact_centered_quotient
|
||||
(scm_i_big2dbl (x), SCM_REAL_VALUE (y));
|
||||
|
@ -2276,49 +2199,6 @@ scm_i_inexact_centered_quotient (double x, double y)
|
|||
return scm_nan ();
|
||||
}
|
||||
|
||||
/* Assumes that both x and y are bigints, though
|
||||
x might be able to fit into a fixnum. */
|
||||
static SCM
|
||||
scm_i_bigint_centered_quotient (SCM x, SCM y)
|
||||
{
|
||||
SCM q, r, min_r;
|
||||
|
||||
/* Note that x might be small enough to fit into a
|
||||
fixnum, so we must not let it escape into the wild */
|
||||
q = scm_i_mkbig ();
|
||||
r = scm_i_mkbig ();
|
||||
|
||||
/* min_r will eventually become -abs(y)/2 */
|
||||
min_r = scm_i_mkbig ();
|
||||
mpz_tdiv_q_2exp (SCM_I_BIG_MPZ (min_r),
|
||||
SCM_I_BIG_MPZ (y), 1);
|
||||
|
||||
/* Arrange for rr to initially be non-positive,
|
||||
because that simplifies the test to see
|
||||
if it is within the needed bounds. */
|
||||
if (mpz_sgn (SCM_I_BIG_MPZ (y)) > 0)
|
||||
{
|
||||
mpz_cdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
|
||||
SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
|
||||
scm_remember_upto_here_2 (x, y);
|
||||
mpz_neg (SCM_I_BIG_MPZ (min_r), SCM_I_BIG_MPZ (min_r));
|
||||
if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0)
|
||||
mpz_sub_ui (SCM_I_BIG_MPZ (q),
|
||||
SCM_I_BIG_MPZ (q), 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
|
||||
SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
|
||||
scm_remember_upto_here_2 (x, y);
|
||||
if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0)
|
||||
mpz_add_ui (SCM_I_BIG_MPZ (q),
|
||||
SCM_I_BIG_MPZ (q), 1);
|
||||
}
|
||||
scm_remember_upto_here_2 (r, min_r);
|
||||
return scm_i_normbig (q);
|
||||
}
|
||||
|
||||
static SCM
|
||||
scm_i_exact_rational_centered_quotient (SCM x, SCM y)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue