Skip to content

Instantly share code, notes, and snippets.

@gfoidl
Created May 21, 2019 11:44
Show Gist options
  • Save gfoidl/37ba3471eaeb4f9b7fd7bd5662e8d11e to your computer and use it in GitHub Desktop.
Save gfoidl/37ba3471eaeb4f9b7fd7bd5662e8d11e to your computer and use it in GitHub Desktop.
Prints the contents of a Vector128<sbyte> to the console
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
namespace gfoidl.Tools.Intrinsics
{
public static class Printer
{
public static bool EnablePrint { get; set; } = true;
//---------------------------------------------------------------------
[DebuggerStepThrough, Conditional("DEBUG")]
public static void PrintHeader()
{
if (!EnablePrint) return;
Console.Write("{0,-10}: ", "byte-idx");
Console.ForegroundColor = ConsoleColor.Yellow;
for (int i = Vector128<sbyte>.Count - 1; i >= 0; --i)
Console.Write(" {0,2} ", i);
Console.WriteLine();
Console.ResetColor();
}
//---------------------------------------------------------------------
[DebuggerStepThrough, Conditional("DEBUG")]
public static void Print(long value, string caption = null)
{
if (!EnablePrint) return;
if (!string.IsNullOrWhiteSpace(caption))
Console.Write("{0,-10}: ", caption);
ref sbyte val = ref Unsafe.As<long, sbyte>(ref value);
for (int i = 0; i < sizeof(long); ++i)
Console.Write("{0}", new string(' ', 2 + 2 + 1));
for (int i = sizeof(long) - 1; i >= 0; --i)
{
sbyte tmp = Unsafe.Add(ref val, i);
Console.ForegroundColor = tmp != 0
? ConsoleColor.Green
: ConsoleColor.DarkGray;
Console.Write("0x{0:x2} ", tmp);
}
Console.ResetColor();
Console.WriteLine();
}
//---------------------------------------------------------------------
[DebuggerStepThrough, Conditional("DEBUG")]
public static void Print(Vector128<sbyte> vec, string caption = null, bool printAsScii = false, bool insertEmptyLineBefore = false, bool insertEmpyLineAfter = false)
{
if (!EnablePrint) return;
if (insertEmptyLineBefore)
Console.WriteLine();
if (!string.IsNullOrWhiteSpace(caption))
Console.Write("{0,-10}: ", caption);
if (printAsScii)
PrintAsAscii();
else
PrintAsHex();
Console.ResetColor();
Console.WriteLine();
if (insertEmpyLineAfter)
Console.WriteLine();
//-----------------------------------------------------------------
void PrintAsHex()
{
for (int i = Vector128<sbyte>.Count - 1; i >= 0; --i)
{
sbyte value = vec.GetElement(i);
Console.ForegroundColor = value != 0
? ConsoleColor.Green
: ConsoleColor.DarkGray;
Console.Write("0x{0:x2} ", value);
}
}
//-----------------------------------------------------------------
void PrintAsAscii()
{
Console.ForegroundColor = ConsoleColor.Cyan;
for (int i = Vector128<sbyte>.Count - 1; i >= 0; --i)
Console.Write("{0,4} ", (char)vec.GetElement(i));
}
}
}
}
@gfoidl
Copy link
Author

gfoidl commented May 21, 2019

Vector128<sbyte> vecAC = Vector128.Create(0b_00001111_11000000_11111100_00000000, 0, 0, 0).AsSByte();
const int a     = 1 << (16 - 10);
const int c     = 1 << (16 - 6);
const int facAC = (c << 16) | a;
Vector128<sbyte> resAC0 = Sse2.MultiplyHigh(vecAC.AsUInt16(), Vector128.Create(facAC).AsUInt16()).AsSByte();
Vector128<sbyte> resAC1 = Sse2.MultiplyHigh(vecAC.AsInt16(), Vector128.Create(facAC).AsInt16()).AsSByte();

PrintHeader();
Print(facAC, nameof(facAC));
Print(vecAC, nameof(vecAC));
Print(resAC0, "mulhi_epu");
Print(resAC1, "mulhi_epi");

Vector128<sbyte> vecBD = Vector128.Create(0b_00000000_00111111_00000011_11110000, 0, 0, 0).AsSByte();
const int b     = 1 << 4;
const int d     = 1 << 8;
const int facBD = (d << 16) | b;
Vector128<sbyte> resBD = Sse2.MultiplyLow(vecBD.AsInt16(), Vector128.Create(facBD).AsInt16()).AsSByte();

Console.WriteLine();
PrintHeader();
Print(facBD, nameof(facBD));
Print(vecBD, nameof(vecBD));
Print(resBD, "mullo_epi");

Prints
grafik

Above code is show-casing simd bitshiftint with multiplications.
As can be seen right-shifting must be done with _mm_mulhi_epu16, so no overflow components appear.

_mm_mulhi_epu16 performs actually a (x << amount) >> 16, when amount <= 16, as this is equivalent to x >> (16 - amount).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment