BTW, why use addition for "rlb"?  The result of gcd
will always be smaller than the arguments.

The following will save a few bytes on stack allocation.

- Qian

diff --git a/src/lisp/num_gmp.lisp b/src/lisp/num_gmp.lisp
index f9015a2b..debcb796 100644
--- a/src/lisp/num_gmp.lisp
+++ b/src/lisp/num_gmp.lisp
@@ -462,7 +462,7 @@
          (rl (if (< xl yl) xl yl))
          (xlb (words_to_bytes xl))
          (ylb (words_to_bytes yl))
-         (rlb (+ xlb ylb))
+         (rlb (if (< xlb ylb) xlb ylb))
         )
         (declare (type fixnum xl yl rl xlb ylb rlb))



On 10/17/24 8:06 PM, Qian Yun wrote:
Solved.

diff --git a/src/lib/gmp_wrap.c b/src/lib/gmp_wrap.c
index a1065104..45e3fa7b 100644
--- a/src/lib/gmp_wrap.c
+++ b/src/lib/gmp_wrap.c
@@ -165,7 +165,7 @@ gmp_wrap_gcd(mp_limb_t * rp, mp_limb_t *s1p,
          rp[res] = rc;
          res++;
      }
-    if (rp[res - 1] & (1ul << (BIT_CNT - 1))) {
+    if (rp[res - 1] & (1ull << (BIT_CNT - 1))) {
          rp[res] = 0;
          res++;
      }

On Windows, 1ul (unsigned long) is 4 bytes instead of 8.
So changing to 1ull fixes this.

Actually there is compiler warning on this line on Windows:

../../../fricas/src/lib/gmp_wrap.c: In function 'gmp_wrap_gcd':
../../../fricas/src/lib/gmp_wrap.c:168:28: warning: left shift count >= width of type [-Wshift-count-overflow]
   168 |     if (rp[res - 1] & (1ul << (BIT_CNT - 1))) {
       |                            ^~

- Qian

On 10/16/24 12:48 PM, Qian Yun wrote:
The correct answer is 13936666434531148641,
and 13936666434531148641+4510077639178402975=2^64

So looks like a sign error.

- Qian

On 10/16/24 12:30 PM, Qian Yun wrote:
On Windows, build fricas with CCL (Clozure CL) and gmp support,
the build is successfully, but when running tests, there are a
few places (for example first test in bugs2018.input) where
FRICASsys enters into infinite loop and I can observe memory
is leaking. (to 10GB.)

This does not happen if I disable GMP support.

Luckily this problem is easy to reproduce, and backtrace
easily points to this minimal reproducible example:

(1) -> gcd(4068020004323239869867049777123762790865, 238106358327114453439438371357)


    (1)  - 4510077639178402975

- Qian




--
You received this message because you are subscribed to the Google Groups "FriCAS - 
computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/fricas-devel/92ca3f74-2898-44c2-84d4-66d6b2f3092d%40gmail.com.

Reply via email to