Narrow four utf16 chars to ascii and write to buffer (#39508)
authorPrashanth Govindarajan <prgovi@microsoft.com>
Wed, 29 Jul 2020 18:02:03 +0000 (11:02 -0700)
committerGitHub <noreply@github.com>
Wed, 29 Jul 2020 18:02:03 +0000 (11:02 -0700)
* Shims

* shim

* Cherry-pick

* NarrowFourUtf16CharsToAsciiAndWriteToBuffer

* Address comments

* Nit

src/libraries/System.Private.CoreLib/src/System/Text/ASCIIUtility.cs
src/libraries/System.Utf8String.Experimental/src/System/Runtime/Intrinsics/Intrinsics.Shims.cs

index 7628f6f..76075a5 100644 (file)
@@ -1003,6 +1003,16 @@ namespace System.Text
                 Vector128<uint> vecNarrow = Sse2.PackUnsignedSaturate(vecWide, vecWide).AsUInt32();
                 Unsafe.WriteUnaligned<uint>(ref outputBuffer, Sse2.ConvertToUInt32(vecNarrow));
             }
+            else if (AdvSimd.IsSupported)
+            {
+                // Narrows a vector of words [ w0 w1 w2 w3 ] to a vector of bytes
+                // [ b0 b1 b2 b3 * * * * ], then writes 4 bytes (32 bits) to the destination.
+
+                Vector128<short> vecWide = Vector128.CreateScalarUnsafe(value).AsInt16();
+                Vector64<byte> lower = AdvSimd.ExtractNarrowingSaturateUnsignedLower(vecWide);
+                Unsafe.WriteUnaligned<uint>(ref outputBuffer, lower.AsUInt32().ToScalar());
+            }
+
             else
             {
                 if (BitConverter.IsLittleEndian)
index fa4602b..ff020e0 100644 (file)
@@ -9,6 +9,8 @@ namespace System.Runtime.Intrinsics
         public static Vector64<uint> CreateScalar(uint value) => throw new PlatformNotSupportedException();
         public static Vector64<byte> AsByte<T>(this Vector64<T> vector) where T : struct => throw new PlatformNotSupportedException();
         public static Vector64<uint> AsUInt32<T>(this Vector64<T> vector) where T : struct => throw new PlatformNotSupportedException();
+        public static Vector64<T> GetLower<T>(this Vector128<T> vector) where T : struct => throw new PlatformNotSupportedException();
+        public static Vector64<ulong> AsUInt64<T>(this Vector64<T> vector) where T : struct => throw new PlatformNotSupportedException();
     }
     internal readonly struct Vector64<T>
         where T : struct
@@ -21,6 +23,8 @@ namespace System.Runtime.Intrinsics
         public static Vector128<short> Create(short value) => throw new PlatformNotSupportedException();
         public static Vector128<ulong> Create(ulong value) => throw new PlatformNotSupportedException();
         public static Vector128<ushort> Create(ushort value) => throw new PlatformNotSupportedException();
+        public static Vector128<byte> Create(byte value) => throw new PlatformNotSupportedException();
+        public static Vector128<uint> Create(uint value) => throw new PlatformNotSupportedException();
         public static Vector128<ulong> CreateScalarUnsafe(ulong value) => throw new PlatformNotSupportedException();
         public static Vector128<byte> AsByte<T>(this Vector128<T> vector) where T : struct => throw new PlatformNotSupportedException();
         public static Vector128<short> AsInt16<T>(this Vector128<T> vector) where T : struct => throw new PlatformNotSupportedException();