Alter #if BIGENDIAN code with run-time condition.
authorAtsushi Eno <atsushieno@gmail.com>
Mon, 23 Feb 2015 12:22:42 +0000 (20:22 +0800)
committerMarek Safar <marek.safar@gmail.com>
Mon, 2 May 2016 22:07:57 +0000 (00:07 +0200)
Should fix bug #27258.

mcs/class/referencesource/mscorlib/system/text/unicodeencoding.cs
mcs/class/referencesource/mscorlib/system/text/utf8encoding.cs

index 80d5e2d343c1f3e36b9991f23b24d9ab266c853e..f6830ec5a386210822c23ff896d9714d6935dd5f 100644 (file)
@@ -494,11 +494,8 @@ namespace System.Text
                 {
                     // No fallback, maybe we can do it fast
 #if !NO_FAST_UNICODE_LOOP
-#if BIGENDIAN       // If endianess is backwards then each pair of bytes would be backwards.
-                    if ( bigEndian &&
-#else
-                    if ( !bigEndian &&
-#endif // BIGENDIAN
+                                       // If endianess is backwards then each pair of bytes would be backwards.
+                    if ( (bigEndian ^ BitConverter.IsLittleEndian) &&
 
 #if WIN64           // 64 bit CPU needs to be long aligned for this to work.
                           charLeftOver == 0 && (unchecked((long)chars) & 7) == 0)
@@ -778,11 +775,8 @@ namespace System.Text
                 {
                     // No fallback, maybe we can do it fast
 #if !NO_FAST_UNICODE_LOOP
-#if BIGENDIAN           // If endianess is backwards then each pair of bytes would be backwards.
-                    if ( bigEndian &&
-#else
-                    if ( !bigEndian &&
-#endif // BIGENDIAN
+                                       // If endianess is backwards then each pair of bytes would be backwards.
+                    if ( (bigEndian ^ BitConverter.IsLittleEndian) &&
 #if WIN64           // 64 bit CPU needs to be long aligned for this to work, 32 bit CPU needs to be 32 bit aligned
                         (unchecked((long)chars) & 7) == 0 && (unchecked((long)bytes) & 7) == 0 &&
 #else
@@ -863,11 +857,7 @@ namespace System.Text
                     // Also somehow this optimizes the above loop?  It seems to cause something above
                     // to get enregistered, but I haven't figured out how to make that happen without this loop.
                     else if ((charLeftOver == 0) &&
-#if BIGENDIAN
-                        bigEndian &&
-#else
-                        !bigEndian &&
-#endif // BIGENDIAN
+                        (bigEndian ^ BitConverter.IsLittleEndian) &&
 
 #if WIN64
                         (unchecked((long)chars) & 7) != (unchecked((long)bytes) & 7) &&  // Only do this if chars & bytes are out of line, otherwise faster loop'll be faster next time
@@ -1200,11 +1190,7 @@ namespace System.Text
                 // If we're aligned then maybe we can do it fast
                 // This'll hurt if we're unaligned because we'll always test but never be aligned
 #if !NO_FAST_UNICODE_LOOP
-#if BIGENDIAN
-                if (bigEndian &&
-#else // BIGENDIAN
-                if (!bigEndian &&
-#endif // BIGENDIAN
+                if ((bigEndian ^ BitConverter.IsLittleEndian) &&
 #if WIN64 // win64 has to be long aligned
                     (unchecked((long)bytes) & 7) == 0 &&
 #else
@@ -1527,11 +1513,7 @@ namespace System.Text
                 // If we're aligned then maybe we can do it fast
                 // This'll hurt if we're unaligned because we'll always test but never be aligned
 #if !NO_FAST_UNICODE_LOOP
-#if BIGENDIAN
-                if (bigEndian &&
-#else // BIGENDIAN
-                if (!bigEndian &&
-#endif // BIGENDIAN
+                if ((bigEndian ^ BitConverter.IsLittleEndian) &&
 #if WIN64 // win64 has to be long aligned
                     (unchecked((long)chars) & 7) == 0 && (unchecked((long)bytes) & 7) == 0 &&
 #else
index b548523db4e02b5e6a2c499819f4fbe822a5b621..fca5a790fed8fcf181d1ceeacf30b06a4cce566d 100644 (file)
@@ -755,12 +755,12 @@ namespace System.Text
                     break;
 
                 LongCodeWithMask:
-#if BIGENDIAN
+if (!BitConverter.IsLittleEndian) {
                     // be careful about the sign extension
                     ch = (int)(((uint)ch) >> 16);
-#else // BIGENDIAN
+} else {
                     ch = (char)ch;
-#endif // BIGENDIAN
+}
                     pSrc++;
 
                     if (ch <= 0x7F) {
@@ -1146,31 +1146,31 @@ namespace System.Text
                         }
 
                         // Unfortunately, this is endianess sensitive
-#if BIGENDIAN
+if (!BitConverter.IsLittleEndian) {
                         *pTarget = (byte)(ch>>16);
                         *(pTarget+1) = (byte)ch;
                         pSrc += 4;
                         *(pTarget+2) = (byte)(chc>>16);
                         *(pTarget+3) = (byte)chc;
                         pTarget += 4;
-#else // BIGENDIAN
+} else {
                         *pTarget = (byte)ch;
                         *(pTarget+1) = (byte)(ch>>16);
                         pSrc += 4;
                         *(pTarget+2) = (byte)chc;
                         *(pTarget+3) = (byte)(chc>>16);
                         pTarget += 4;
-#endif // BIGENDIAN
+}
                     }
                     continue;
 
                 LongCodeWithMask:
-#if BIGENDIAN
+if (!BitConverter.IsLittleEndian) {
                     // be careful about the sign extension
                     ch = (int)(((uint)ch) >> 16);
-#else // BIGENDIAN
+} else {
                     ch = (char)ch;
-#endif // BIGENDIAN
+}
                     pSrc++;
 
                     if (ch > 0x7F) {
@@ -1534,17 +1534,19 @@ namespace System.Text
                     }
                     break;
 
-#if BIGENDIAN
                 LongCodeWithMask32:
                     // be careful about the sign extension
+if (!BitConverter.IsLittleEndian) {
                     ch = (int)(((uint)ch) >> 16);
+} else {
+                    ch &= 0xFF;
+}
                 LongCodeWithMask16:
+if (!BitConverter.IsLittleEndian) {
                     ch = (int)(((uint)ch) >> 8);
-#else // BIGENDIAN
-                LongCodeWithMask32:
-                LongCodeWithMask16:
+} else {
                     ch &= 0xFF;
-#endif // BIGENDIAN
+}
                     pSrc++;
                     if (ch <= 0x7F) {
                         continue;
@@ -1976,17 +1978,17 @@ namespace System.Text
                         }
 
                         // Unfortunately, this is endianess sensitive
-#if BIGENDIAN
+if (!BitConverter.IsLittleEndian) {
                         *pTarget = (char)((ch >> 8) & 0x7F);
                         pSrc += 2;
                         *(pTarget+1) = (char)(ch & 0x7F);
                         pTarget += 2;
-#else // BIGENDIAN
+} else {
                         *pTarget = (char)(ch & 0x7F);
                         pSrc += 2;
                         *(pTarget+1) = (char)((ch >> 8) & 0x7F);
                         pTarget += 2;
-#endif // BIGENDIAN
+}
                     }
 
                     // Run 8 characters at a time!
@@ -1998,7 +2000,7 @@ namespace System.Text
                         }
 
                         // Unfortunately, this is endianess sensitive
-#if BIGENDIAN
+if (!BitConverter.IsLittleEndian) {
                         *pTarget = (char)((ch >> 24) & 0x7F);
                         *(pTarget+1) = (char)((ch >> 16) & 0x7F);
                         *(pTarget+2) = (char)((ch >> 8) & 0x7F);
@@ -2009,7 +2011,7 @@ namespace System.Text
                         *(pTarget+6) = (char)((chb >> 8) & 0x7F);
                         *(pTarget+7) = (char)(chb & 0x7F);
                         pTarget += 8;
-#else // BIGENDIAN
+} else {
                         *pTarget = (char)(ch & 0x7F);
                         *(pTarget+1) = (char)((ch >> 8) & 0x7F);
                         *(pTarget+2) = (char)((ch >> 16) & 0x7F);
@@ -2020,21 +2022,23 @@ namespace System.Text
                         *(pTarget+6) = (char)((chb >> 16) & 0x7F);
                         *(pTarget+7) = (char)((chb >> 24) & 0x7F);
                         pTarget += 8;
-#endif // BIGENDIAN
+}
                     }
                     break;
 
-#if BIGENDIAN
                 LongCodeWithMask32:
+if (!BitConverter.IsLittleEndian) {
                     // be careful about the sign extension
                     ch = (int)(((uint)ch) >> 16);
+} else {
+                    ch &= 0xFF;
+}
                 LongCodeWithMask16:
+if (!BitConverter.IsLittleEndian) {
                     ch = (int)(((uint)ch) >> 8);
-#else // BIGENDIAN
-                LongCodeWithMask32:
-                LongCodeWithMask16:
+} else {
                     ch &= 0xFF;
-#endif // BIGENDIAN
+}
                     pSrc++;
                     if (ch <= 0x7F) {
                         *pTarget = (char)ch;