1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-20 11:40:18 +02:00

Implement scm_logior with new integer library

* libguile/integers.c (scm_integer_logior_ii, scm_integer_logior_zi)
(scm_integer_logior_zz): New internal functions.
* libguile/integers.h: Declare the new internal functions.
* libguile/numbers.c (scm_logior): Use new internal functions.
This commit is contained in:
Andy Wingo 2021-12-19 10:13:42 +01:00
parent 4a380aa6ac
commit 7e85ffa822
3 changed files with 42 additions and 36 deletions

View file

@ -1924,3 +1924,37 @@ scm_integer_logand_zz (SCM x, SCM y)
scm_remember_upto_here_2 (x, y); scm_remember_upto_here_2 (x, y);
return take_mpz (result); return take_mpz (result);
} }
SCM
scm_integer_logior_ii (scm_t_inum x, scm_t_inum y)
{
return SCM_I_MAKINUM (x | y);
}
SCM
scm_integer_logior_zi (SCM x, scm_t_inum y)
{
if (y == 0)
return x;
mpz_t result, zx, zy;
mpz_init (result);
alias_bignum_to_mpz (scm_bignum (x), zx);
mpz_init_set_si (zy, y);
mpz_ior (result, zy, zx);
scm_remember_upto_here_1 (x);
mpz_clear (zy);
return take_mpz (result);
}
SCM
scm_integer_logior_zz (SCM x, SCM y)
{
mpz_t result, zx, zy;
mpz_init (result);
alias_bignum_to_mpz (scm_bignum (x), zx);
alias_bignum_to_mpz (scm_bignum (y), zy);
mpz_ior (result, zy, zx);
scm_remember_upto_here_2 (x, y);
return take_mpz (result);
}

View file

@ -136,6 +136,10 @@ SCM_INTERNAL SCM scm_integer_logand_ii (scm_t_inum x, scm_t_inum y);
SCM_INTERNAL SCM scm_integer_logand_zi (SCM x, scm_t_inum y); SCM_INTERNAL SCM scm_integer_logand_zi (SCM x, scm_t_inum y);
SCM_INTERNAL SCM scm_integer_logand_zz (SCM x, SCM y); SCM_INTERNAL SCM scm_integer_logand_zz (SCM x, SCM y);
SCM_INTERNAL SCM scm_integer_logior_ii (scm_t_inum x, scm_t_inum y);
SCM_INTERNAL SCM scm_integer_logior_zi (SCM x, scm_t_inum y);
SCM_INTERNAL SCM scm_integer_logior_zz (SCM x, SCM y);
#endif /* SCM_INTEGERS_H */ #endif /* SCM_INTEGERS_H */

View file

@ -2945,8 +2945,6 @@ SCM_DEFINE (scm_i_logand, "logand", 0, 2, 1,
SCM scm_logand (SCM n1, SCM n2) SCM scm_logand (SCM n1, SCM n2)
#define FUNC_NAME s_scm_logand #define FUNC_NAME s_scm_logand
{ {
scm_t_inum nn1;
if (SCM_UNBNDP (n2)) if (SCM_UNBNDP (n2))
{ {
if (SCM_UNBNDP (n1)) if (SCM_UNBNDP (n1))
@ -3007,8 +3005,6 @@ SCM_DEFINE (scm_i_logior, "logior", 0, 2, 1,
SCM scm_logior (SCM n1, SCM n2) SCM scm_logior (SCM n1, SCM n2)
#define FUNC_NAME s_scm_logior #define FUNC_NAME s_scm_logior
{ {
scm_t_inum nn1;
if (SCM_UNBNDP (n2)) if (SCM_UNBNDP (n2))
{ {
if (SCM_UNBNDP (n1)) if (SCM_UNBNDP (n1))
@ -3021,47 +3017,19 @@ SCM scm_logior (SCM n1, SCM n2)
if (SCM_I_INUMP (n1)) if (SCM_I_INUMP (n1))
{ {
nn1 = SCM_I_INUM (n1);
if (SCM_I_INUMP (n2)) if (SCM_I_INUMP (n2))
{ return scm_integer_logior_ii (SCM_I_INUM (n1), SCM_I_INUM (n2));
long nn2 = SCM_I_INUM (n2);
return SCM_I_MAKINUM (nn1 | nn2);
}
else if (SCM_BIGP (n2)) else if (SCM_BIGP (n2))
{ return scm_integer_logior_zi (n2, SCM_I_INUM (n1));
intbig:
if (nn1 == 0)
return n2;
{
SCM result_z = scm_i_mkbig ();
mpz_t nn1_z;
mpz_init_set_si (nn1_z, nn1);
mpz_ior (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2));
scm_remember_upto_here_1 (n2);
mpz_clear (nn1_z);
return scm_i_normbig (result_z);
}
}
else else
SCM_WRONG_TYPE_ARG (SCM_ARG2, n2); SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
} }
else if (SCM_BIGP (n1)) else if (SCM_BIGP (n1))
{ {
if (SCM_I_INUMP (n2)) if (SCM_I_INUMP (n2))
{ return scm_integer_logior_zi (n1, SCM_I_INUM (n2));
SCM_SWAP (n1, n2);
nn1 = SCM_I_INUM (n1);
goto intbig;
}
else if (SCM_BIGP (n2)) else if (SCM_BIGP (n2))
{ return scm_integer_logior_zz (n1, n2);
SCM result_z = scm_i_mkbig ();
mpz_ior (SCM_I_BIG_MPZ (result_z),
SCM_I_BIG_MPZ (n1),
SCM_I_BIG_MPZ (n2));
scm_remember_upto_here_2 (n1, n2);
return scm_i_normbig (result_z);
}
else else
SCM_WRONG_TYPE_ARG (SCM_ARG2, n2); SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
} }