mpq_mul_ui
Marco Bodrato
bodrato at mail.dm.unipi.it
Tue Feb 8 11:26:39 CET 2022
Ciao,
Il 2022-01-23 01:34 Marc Glisse ha scritto:
> Hello,
>
> What would you think of adding mpq_mul_ui, mpq_div_ui, mpq_ui_div, and
> also the _z versions?
I tried to think to the mini-mpq version of the _z flavor.
diff -r ed0406cf3c70 mini-gmp/mini-mpq.c
--- a/mini-gmp/mini-mpq.c Wed Feb 02 19:16:36 2022 +0100
+++ b/mini-gmp/mini-mpq.c Tue Feb 08 10:04:55 2022 +0100
@@ -395,6 +395,30 @@
}
void
+mpq_mul_z (mpq_t r, const mpq_t a, const mpz_t b)
+{
+ mpq_t t;
+ static const mp_limb_t u = 1;
+
+ mpq_mul (r, a, mpq_roinit_normal_nn (t, b->_mp_d, b->_mp_size, &u,
1));
+}
+
+void
+mpq_z_div (mpq_t r, const mpz_t b, const mpq_t a)
+{
+ mpq_t t;
+ mpq_mul_z (r, mpq_roinit_zz (t, mpq_denref (a), mpq_numref (a)), b);
+}
+
+void
+mpq_div_z (mpq_t r, const mpq_t a, const mpz_t b)
+{
+ mpq_t t;
+ mpq_z_div (r, b, a);
+ mpq_inv (r, r);
+}
+
+void
mpq_div_2exp (mpq_t r, const mpq_t q, mp_bitcnt_t e)
{
mp_bitcnt_t z = mpz_scan1 (mpq_numref (q), 0);
It seems simple enough. It should support (but I'm not sure!) the reuse
cases
mpq_mul_z (B, A, mpq_denref (B))
and
mpq_mul_z (A, A, Z)
but I fear it would not support
mpq_mul_z (A, A, mpq_denref (A))
or
mpq_mul_z (A, A, mpq_numref (A))
Those are actually equivalent respectively to
mpz_set_ui (mpq_denref (A), 1);
and
mpz_mul (mpq_numref (A), mpq_numref (A), mpq_numref (A))
But I think we should support all reuse cases, shouldn't we?
Ĝis,
m
More information about the gmp-devel
mailing list