This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [gfortran] Fix PR 17568: Shortcomings in ISHFT constant folder
Paul Brook wrote:
>>I verified that gcc 2.95 supports variable length arrays, according to its
>>documentation, so I committed a patch which implements the twos_complement
>>function using a VLA.
>
>
> Better would be to avoid using the buffer at all.
> Why can't you just use gmp arithmetic?
>
Patch appended. I didn't reimplement ishft and ishftc as I said in my other
post, because that would either require introducing masking in several places,
and might therefore not be as good as I initially thought.
Built & tested. Ok?
- Tobi
2004-10-08 Tobias Schlueter <tobias.schlueter@physik.uni-muenchen.de>
* simplify.c (twos_complement): Calculate mask in GMP arithmetic.
Index: simplify.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fortran/simplify.c,v
retrieving revision 1.14
diff -u -p -r1.14 simplify.c
--- simplify.c 6 Oct 2004 23:16:43 -0000 1.14
+++ simplify.c 8 Oct 2004 15:45:04 -0000
@@ -146,19 +146,17 @@ static void
twos_complement (mpz_t x, int bitsize)
{
mpz_t mask;
- char mask_s[bitsize + 1];
if (mpz_tstbit (x, bitsize - 1) == 1)
{
- /* The mpz_init_set_{u|s}i functions take a long argument, but
- the widest integer the target supports might be wider, so we
- have to go via an intermediate string. */
- memset (mask_s, '1', bitsize);
- mask_s[bitsize] = '\0';
- mpz_init_set_str (mask, mask_s, 2);
-
- /* We negate the number by hand, zeroing the high bits, and then
- have it negated by GMP. */
+ mpz_init_set_ui(mask, 1);
+ mpz_mul_2exp(mask, mask, bitsize);
+ mpz_sub_ui(mask, mask, 1);
+
+ /* We negate the number by hand, zeroing the high bits, that is
+ make it the corresponding positive number, and then have it
+ negated by GMP, giving the correct representation of the
+ negative number. */
mpz_com (x, x);
mpz_add_ui (x, x, 1);
mpz_and (x, x, mask);