]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR fortran/31964 ([4.2, 4.1 only]ishftc fails with certain thrid argument)
authorJerry DeLisle <jvdelisle@gcc.gnu.org>
Thu, 24 May 2007 05:53:27 +0000 (05:53 +0000)
committerJerry DeLisle <jvdelisle@gcc.gnu.org>
Thu, 24 May 2007 05:53:27 +0000 (05:53 +0000)
2007-05-23  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

PR libfortran/31964
Backport from trunk.
* intrinsics/ishftc.c (ishftc4, ishftc8, ishftc16): Fix mask to handle
shift of bit-size number of bits.

From-SVN: r125016

libgfortran/ChangeLog
libgfortran/intrinsics/ishftc.c

index 705303adf5b405ca5f4e365d6f11d90baa9d68ed..a71d215b8ce7a4abcedd65c35e2f03c97c53a336 100644 (file)
@@ -1,3 +1,10 @@
+2007-05-23  Jerry DeLisle  <jvdelisle@gcc.gnu.org>
+
+       PR libfortran/31964
+       Backport from trunk.
+       * intrinsics/ishftc.c (ishftc4, ishftc8, ishftc16): Fix mask to handle
+       shift of bit-size number of bits.
+
 2007-05-10  Jerry DeLisle  <jvdelisle@gcc.gnu.org>
 
        PR libfortran/31409
index a147b9683898e3dfa660ee3e3edd004507ed713f..91e0db2048d10cf482f404d86a8980fba160bb40 100644 (file)
@@ -36,8 +36,7 @@ export_proto(ishftc4);
 GFC_INTEGER_4
 ishftc4 (GFC_INTEGER_4 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size)
 {
-  GFC_INTEGER_4 mask;
-  GFC_UINTEGER_4 bits;
+  GFC_UINTEGER_4 mask, bits;
 
   if (shift < 0)
     shift = shift + size;
@@ -45,9 +44,14 @@ ishftc4 (GFC_INTEGER_4 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size)
   if (shift == 0 || shift == size)
     return i;
 
-  mask = (~(GFC_INTEGER_4)0) << size;
-  bits = i & ~mask;
-  return (i & mask) | (bits >> (size - shift)) | ((i << shift) & ~mask);
+  /* In C, the result of the shift operator is undefined if the right operand
+     is greater than or equal to the number of bits in the left operand. So we
+     have to special case it for fortran.  */
+  mask = ~((size == 32) ? 0 : (~0 << size));
+
+  bits = i & mask;
+  
+  return (i & ~mask) | ((bits << shift) & mask) | (bits >> (size - shift));
 }
 
 extern GFC_INTEGER_8 ishftc8 (GFC_INTEGER_8, GFC_INTEGER_4, GFC_INTEGER_4);
@@ -56,8 +60,7 @@ export_proto(ishftc8);
 GFC_INTEGER_8
 ishftc8 (GFC_INTEGER_8 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size)
 {
-  GFC_INTEGER_8 mask;
-  GFC_UINTEGER_8 bits;
+  GFC_UINTEGER_8 mask, bits;
 
   if (shift < 0)
     shift = shift + size;
@@ -65,9 +68,14 @@ ishftc8 (GFC_INTEGER_8 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size)
   if (shift == 0 || shift == size)
     return i;
 
-  mask = (~(GFC_INTEGER_8)0) << size;
-  bits = i & ~mask;
-  return (i & mask) | (bits >> (size - shift)) | ((i << shift) & ~mask);
+  /* In C, the result of the shift operator is undefined if the right operand
+     is greater than or equal to the number of bits in the left operand. So we
+     have to special case it for fortran.  */
+  mask = ~((size == 64) ? 0 : (~0 << size));
+
+  bits = i & mask;
+  
+  return (i & ~mask) | ((bits << shift) & mask) | (bits >> (size - shift));
 }
 
 #ifdef HAVE_GFC_INTEGER_16
@@ -77,8 +85,7 @@ export_proto(ishftc16);
 GFC_INTEGER_16
 ishftc16 (GFC_INTEGER_16 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size)
 {
-  GFC_INTEGER_16 mask;
-  GFC_UINTEGER_16 bits;
+  GFC_UINTEGER_16 mask, bits;
 
   if (shift < 0)
     shift = shift + size;
@@ -86,8 +93,13 @@ ishftc16 (GFC_INTEGER_16 i, GFC_INTEGER_4 shift, GFC_INTEGER_4 size)
   if (shift == 0 || shift == size)
     return i;
 
-  mask = (~(GFC_INTEGER_16)0) << size;
-  bits = i & ~mask;
-  return (i & mask) | (bits >> (size - shift)) | ((i << shift) & ~mask);
+  /* In C, the result of the shift operator is undefined if the right operand
+     is greater than or equal to the number of bits in the left operand. So we
+     have to special case it for fortran.  */
+  mask = ~((size == 128) ? 0 : (~0 << size));
+
+  bits = i & mask;
+  
+  return (i & ~mask) | ((bits << shift) & mask) | (bits >> (size - shift));
 }
 #endif