[patch, fortran] PR 30381, PR 30420: Fix constant-folding in bitwise intrinsics

Brooks Moses brooks.moses@codesourcery.com
Wed Jan 10 05:01:00 GMT 2007


In bitwise intrinsics such as IBCLR(), an integer variable is treated as 
a finite bit array.  In the constant-folding, where the variable is 
represented as an mpz_t infinite-precision integer, this requires 
conversion to handle the sign bit in particular.  This is currently 
being done somewhat haphazardly, resulting in things like PR 30420.
This patch cleans this up, with explicit functions for converting to and 
from unsigned finite-width representations as needed.  It also adds some 
gcc_asserts to what of this code already existed, to make sure that the 
conversion is only being applied to valid values.

Interestingly, in gfc_simplify_not (just like in _iand, _ior, and 
_ieor), there is no need to do the conversion -- the above-range high 
bits will always be the same as the sign bit -- and so the masking there 
is irrelevant.  This patch removes it.

The constant-folding in ISHFTC() is somewhat of a mess; PR30381 
describes about half the stuff I found with that.

The first problem with ISHFTC() is that there wasn't a check to bail out 
of the constant-folding if the third argument isn't constant, so I added 
that.

The second problem is that, if SIZE is present and less than the size of
the first argument, then only the low bits should be shifted, and the
high bits should be carried over.  This was fixed by setting the result
value to the first argument value (and then converting to an unsigned 
representation) before doing the bit shuffling.

The third problem is that the process of converting back to a signed 
representation at the end of the function was using the SIZEth bit as 
the sign bit, which is wrong -- it should use the leftmost bit, 
regardless of the value of SIZE.

The fourth thing is that no checking was performed to make sure that
SHIFT and SIZE did not exceed the size of the first argument.  So I
added checks for that.

In looking at the chkbits.f90 test, I noticed that although it appears 
to be checking four different integer kinds, it's merely repeating the 
same INTEGER(4) tests and assigning them to different integer variables. 
  So I fixed that, and added the relevant IBCLR() tests to it.

-----------------------------------------------------------
2007-01-09  Brooks Moses  <brooks.moses@codesourcery.com>

	PR 30381
	PR 30420
	* simplify.c (convert_mpz_to_unsigned): New function.
	(convert_mpz_to_signed): New function, largely based on
	twos_complement().
	(twos_complement): Removed.
	(gfc_simplify_ibclr): Add conversions to and from an
	unsigned representation before bit-twiddling.
	(gfc_simplify_ibset): Same.
	(gfc_simplify_ishftc): Add checks for overly large
	constant arguments, only check the third argument if
	it's present, carry over high bits into	the result as
	appropriate, and perform the final conversion back to
	a signed representation using the correct sign bit.
	(gfc_simplify_not): Removed unnecessary masking.

-----------------------------------------------------------
2007-01-09  Brooks Moses  <brooks.moses@codesourcery.com>

	* chkbits.f90: Added IBCLR tests; test calls for
	different integer kinds.
	* ishft.f90: Renamed to ishft_1.f90...
	* ishft_1.f90: ...Renamed from ishft.f90.
	* ishft_2.f90: New test.
	* ishft_3.f90: New test.

-----------------------------------------------------------

Tested on i686-pc-linux-gnu for trunk.  Ok to commit?

Ok to backport to 4.2 a week later, after testing there?

- Brooks

-------------- next part --------------
A non-text attachment was scrubbed...
Name: bitintrinsics.diff
Type: text/x-patch
Size: 6633 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/fortran/attachments/20070110/4a1c215a/attachment.bin>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ishft_2.f90
URL: <http://gcc.gnu.org/pipermail/fortran/attachments/20070110/4a1c215a/attachment.f90>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ishft_3.f90
URL: <http://gcc.gnu.org/pipermail/fortran/attachments/20070110/4a1c215a/attachment-0001.f90>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: chkbits.diff
Type: text/x-patch
Size: 1097 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/fortran/attachments/20070110/4a1c215a/attachment-0001.bin>


More information about the Fortran mailing list