ここでは、ハードウェアエンコードでお馴染み?の System.Runtime.Intrinsics.Vector64<T> System.Runtime.Intrinsics.Vector128<T> System.Runtime.Intrinsics.Vector256<T> について解説します。(備忘録)
解説がなかなか見当たらないので、手当たり次第追加します。
複数の値に対して、同じ演算を行うことができる。
System.Runtime.Intrinsics 空間にある Vector64<T> 、 Vector128<T> 、 Vector256<T> 型などを用いると、 CPU によっては専用命令文を使用できるので、個々に計算するより演算速度が速くなる"場合"がある。
あくまでも環境依存。CPU によって専用命令文がない場合は遅くなる場合がる。ちゃんと if 文で分岐しよう。
ちなみに if 文による分岐は JIT 時に最適化されるみたい。(参考: https://ufcpp.net/blog/2018/12/hdintrinsic/)
System.Numerics.Vector<T> という似た型も存在する。こちらは 256 bit の固定長 (System.Runtime.Intrinsics.Vector256<T> と同じような構造体)。
また、 3 つの float 型の値を扱う場合は System.Numerics.Vector3 型を、 4 つの float 型の場合は System.Numerics.Vector4 型を使う手段もある(こちらは .NET Framework 4.6 以降対応)。
RoundCurrentDirection
Vector256<float> System.Runtime.Intrinsics.X86.Avx.RoundCurrentDirection(Vector256<float> value)
Vector256<double> System.Runtime.Intrinsics.X86.Avx.RoundCurrentDirection(Vector256<double> value)
Vector128<float> System.Runtime.Intrinsics.X86.Sse41.RoundCurrentDirection(Vector128<float> value)
Vector128<double> System.Runtime.Intrinsics.X86.Sse41.RoundCurrentDirection(Vector128<double> value)
各要素の値に対して、 System.Math.Round(value, System.MidpointRounding.AwayFromZero) の処理を行うのと同等。
例えば 3.5 は 4.0 に、-3.5 は -4.0 に、-3.49 は -3.0に丸められる。
System.Runtime.Intrinsics.Arm.AdvSimd.RoundAwayFromZero(Vector128<float> value) は PlatformNotSupportedException を throw するので注意。
ConvertToVector128Single
Vector128<float> System.Runtime.Intrinsics.X86.Sse2.ConvertToVector128Single(Vector128<int> value)
Vector128<float> System.Runtime.Intrinsics.X86.Sse2.ConvertToVector128Single(Vector128<double> value)
各要素の値を float 型に変換する。 (float)value の処理を行うのと同等。