]> git.ipfire.org Git - thirdparty/gcc.git/commit
wide-int: Fix wi::to_mpz [PR98474]
authorJakub Jelinek <jakub@redhat.com>
Thu, 31 Dec 2020 10:06:56 +0000 (11:06 +0100)
committerJakub Jelinek <jakub@redhat.com>
Wed, 6 Jan 2021 09:38:39 +0000 (10:38 +0100)
commita4d191d08c6acb24034af4182b3524e6ef97546c
tree218e71e99a0ce16fda8b6cc5dfdb8320c83de290
parentf3113a85f098df8165624321cc85d20219fb2ada
wide-int: Fix wi::to_mpz [PR98474]

The following testcase is miscompiled, because niter analysis miscomputes
the number of iterations to 0.
The problem is that niter analysis uses mpz_t (wonder why, wouldn't
widest_int do the same job?) and when wi::to_mpz is called e.g. on the
TYPE_MAX_VALUE of __uint128_t, it initializes the mpz_t result with wrong
value.
wi::to_mpz has code to handle negative wide_ints in signed types by
inverting all bits, importing to mpz and complementing it, which is fine,
but doesn't handle correctly the case when the wide_int's len (times
HOST_BITS_PER_WIDE_INT) is smaller than precision when wi::neg_p.
E.g. the 0xffffffffffffffffffffffffffffffff TYPE_MAX_VALUE is represented
in wide_int as 0xffffffffffffffff len 1, and wi::to_mpz would create
0xffffffffffffffff mpz_t value from that.
This patch handles it by adding the needed -1 host wide int words (and has
also code to deal with precision that aren't multiple of
HOST_BITS_PER_WIDE_INT).

2020-12-31  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/98474
* wide-int.cc (wi::to_mpz): If wide_int has MSB set, but type
is unsigned and excess negative, append set bits after len until
precision.

* gcc.c-torture/execute/pr98474.c: New test.

(cherry picked from commit 9e603837f7ad886df62e02ac0cd395ec17b7d587)
gcc/testsuite/gcc.c-torture/execute/pr98474.c [new file with mode: 0644]
gcc/wide-int.cc