Skip to content

Commit 97b4ce5

Browse files
committed
FIXED: #1511 Failure to normalize bigint to tagged int on Windows.
This happened when using GMP for integers that do not fit in a long but do fit in a tagged integer.
1 parent 0cb73a8 commit 97b4ce5

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

src/pl-gmp.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,14 +1382,18 @@ put_mpz(DECL_LD Word at, mpz_t mpz)
13821382
mpz_get_str(buf, 10, mpz));
13831383
});
13841384

1385-
#if SIZEOF_LONG < SIZEOF_WORD
1386-
if ( mpz_cmp(mpz, MPZ_MIN_LONG) >= 0 &&
1387-
mpz_cmp(mpz, MPZ_MAX_LONG) <= 0 )
1388-
#else
13891385
if ( mpz_cmp(mpz, MPZ_MIN_TAGGED) >= 0 &&
13901386
mpz_cmp(mpz, MPZ_MAX_TAGGED) <= 0 )
1387+
{ int64_t v;
1388+
1389+
#if SIZEOF_LONG < SIZEOF_WORD
1390+
bool rc = mpz_to_int64(mpz, &v);
1391+
assert(rc);
1392+
(void)rc;
1393+
#else
1394+
static_assertion(sizeof(word) == sizeof(long));
1395+
v = mpz_get_si(mpz);
13911396
#endif
1392-
{ long v = mpz_get_si(mpz);
13931397

13941398
if ( !ensureGlobalSpace(0, ALLOW_GC) )
13951399
return false;

tests/core/test_arith.pl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* Part of SWI-Prolog
22
33
Author: Jan Wielemaker
4-
E-mail: J.Wielemaker@vu.nl
5-
WWW: http://www.swi-prolog.org
6-
Copyright (C): 1985-2024, University of Amsterdam
4+
E-mail: jan@swi-prolog.org
5+
WWW: https://www.swi-prolog.org
6+
Copyright (C): 1985-2026, University of Amsterdam
77
VU University Amsterdam
88
CWI, Amsterdam
99
SWI-Prolog Solutions b.v.
@@ -58,7 +58,8 @@
5858
float_compare,
5959
arith_misc,
6060
max_integer_size,
61-
moded_int
61+
moded_int,
62+
canonical_bignum
6263
]).
6364

6465
:- begin_tests(arith_basics).
@@ -946,6 +947,15 @@
946947

947948
:- end_tests(moded_int).
948949

950+
:- begin_tests(canonical_bignum).
951+
952+
test(to_tagged, MantissaBits == 2251799813685249 ) :-
953+
Bits = 0x7ff8000000000001,
954+
MantissaWidth = 52,
955+
MantissaBits is Bits /\ ((1 << MantissaWidth) - 1).
956+
957+
:- end_tests(canonical_bignum).
958+
949959
:- begin_tests(arith_misc).
950960

951961
test(string) :-

0 commit comments

Comments
 (0)