flint-2.4.5-gmp6-compat.patch 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. From a7f911140e7d0a0125653a40aa2c5fe257bd78f5 Mon Sep 17 00:00:00 2001
  2. From: Fredrik Johansson <fredrik.johansson@gmail.com>
  3. Date: Thu, 18 Sep 2014 14:49:05 +0200
  4. Subject: [PATCH] redefine fmpz_invmod to consider any integer invertible mod 1
  5. (for gmp 6.0 compatibility)
  6. ---
  7. fmpz/doc/fmpz.txt | 3 ++-
  8. fmpz/invmod.c | 11 +++++++++--
  9. fmpz/test/t-invmod.c | 21 +++++++++++++++++----
  10. 3 files changed, 28 insertions(+), 7 deletions(-)
  11. diff --git a/fmpz/doc/fmpz.txt b/fmpz/doc/fmpz.txt
  12. index fb422d8..2ada719 100644
  13. --- a/fmpz/doc/fmpz.txt
  14. +++ b/fmpz/doc/fmpz.txt
  15. @@ -899,7 +899,8 @@ int fmpz_invmod(fmpz_t f, const fmpz_t g, const fmpz_t h)
  16. Sets $f$ to the inverse of $g$ modulo $h$. The value of $h$ may
  17. not be $0$ otherwise an exception results. If the inverse exists
  18. the return value will be non-zero, otherwise the return value will
  19. - be $0$ and the value of $f$ undefined.
  20. + be $0$ and the value of $f$ undefined. As a special case, we
  21. + consider any number invertible modulo $h = \pm 1$, with inverse 0.
  22. void fmpz_negmod(fmpz_t f, const fmpz_t g, const fmpz_t h)
  23. diff --git a/fmpz/invmod.c b/fmpz/invmod.c
  24. index a0cf601..0e20f39 100644
  25. --- a/fmpz/invmod.c
  26. +++ b/fmpz/invmod.c
  27. @@ -67,7 +67,11 @@ fmpz_invmod(fmpz_t f, const fmpz_t g, const fmpz_t h)
  28. if (c2 < WORD(0))
  29. c2 = -c2;
  30. if (c2 == WORD(1))
  31. - return 0; /* special case not handled by n_invmod */
  32. + {
  33. + fmpz_zero(f);
  34. + return 1; /* special case not handled by n_invmod */
  35. + }
  36. +
  37. gcd = z_gcdinv(&inv, c1, c2);
  38. return (gcd == UWORD(1) ? fmpz_set_si(f, inv), 1 : 0);
  39. @@ -106,7 +110,10 @@ fmpz_invmod(fmpz_t f, const fmpz_t g, const fmpz_t h)
  40. if (c2 < WORD(0))
  41. c2 = -c2;
  42. if (c2 == WORD(1))
  43. - return 0; /* special case not handled by z_gcd_invert */
  44. + {
  45. + fmpz_zero(f);
  46. + return 1; /* special case not handled by z_gcd_invert */
  47. + }
  48. /* reduce g mod h first */
  49. r = flint_mpz_fdiv_ui(COEFF_TO_PTR(c1), c2);
  50. diff --git a/fmpz/test/t-invmod.c b/fmpz/test/t-invmod.c
  51. index aea236e..8ff1c7f 100644
  52. --- a/fmpz/test/t-invmod.c
  53. +++ b/fmpz/test/t-invmod.c
  54. @@ -30,6 +30,19 @@
  55. #include "ulong_extras.h"
  56. #include "fmpz.h"
  57. +/* Use the definiton of GMP versions >= 6.0 */
  58. +int
  59. +mpz_invert2(mpz_t a, const mpz_t b, const mpz_t c)
  60. +{
  61. + if (mpz_cmpabs_ui(c, 1) == 0)
  62. + {
  63. + mpz_set_ui(a, 0);
  64. + return 1;
  65. + }
  66. + else
  67. + return mpz_invert(a, b, c);
  68. +}
  69. +
  70. int
  71. main(void)
  72. {
  73. @@ -63,7 +76,7 @@ main(void)
  74. fmpz_get_mpz(e, b);
  75. r1 = fmpz_invmod(c, a, b);
  76. - r2 = mpz_invert(f, d, e);
  77. + r2 = mpz_invert2(f, d, e);
  78. fmpz_get_mpz(g, c);
  79. @@ -106,7 +119,7 @@ main(void)
  80. fmpz_get_mpz(d, a);
  81. r1 = fmpz_invmod(c, a, a);
  82. - r2 = mpz_invert(f, d, d);
  83. + r2 = mpz_invert2(f, d, d);
  84. fmpz_get_mpz(g, c);
  85. @@ -149,7 +162,7 @@ main(void)
  86. fmpz_get_mpz(e, b);
  87. r1 = fmpz_invmod(a, a, b);
  88. - r2 = mpz_invert(f, d, e);
  89. + r2 = mpz_invert2(f, d, e);
  90. fmpz_get_mpz(g, a);
  91. @@ -192,7 +205,7 @@ main(void)
  92. fmpz_get_mpz(e, b);
  93. r1 = fmpz_invmod(b, a, b);
  94. - r2 = mpz_invert(f, d, e);
  95. + r2 = mpz_invert2(f, d, e);
  96. fmpz_get_mpz(g, b);