SIMDFxpConverter.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using FxpConvert.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace SIMDFxpConvert
  9. {
  10. public unsafe sealed class SIMDFxpConverter : IFxpConvert
  11. {
  12. public bool IsSupport => true;
  13. public void DoubleConvertToDxp(ref double value, NiFpga_FxpTypeInfo dxpinfo, ref ulong fxp, uint count)
  14. {
  15. if (count == 0) return;
  16. double pow = (1ul << (dxpinfo.wordLength - dxpinfo.integerWordLength));
  17. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  18. uint c = count / onecount;
  19. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref value);
  20. ulong* fxpptr = (ulong*)Unsafe.AsPointer(ref fxp);
  21. for (int i = 0; i < c; i++)
  22. {
  23. var temp = Unsafe.Add(ref source, i) * pow;
  24. for (int j = 0; j < onecount; j++)
  25. {
  26. fxpptr[i * onecount + j] = (ulong)temp[j];
  27. }
  28. }
  29. uint c1 = count % onecount;
  30. if (c1 != 0)
  31. {
  32. uint start = c * onecount;
  33. for (uint i = 0; i < c1; i++)
  34. {
  35. ref double temp = ref Unsafe.Add(ref value, start + i);
  36. ref ulong tempu = ref Unsafe.Add(ref fxp, start + i);
  37. tempu = (ulong)(temp * pow);
  38. }
  39. }
  40. }
  41. public void FloatConvertToDxp(ref float value, NiFpga_FxpTypeInfo dxpinfo, ref ulong fxp, uint count)
  42. {
  43. if (count == 0) return;
  44. float pow = (1ul << (dxpinfo.wordLength - dxpinfo.integerWordLength));
  45. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  46. uint c = count / onecount;
  47. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref value);
  48. ulong* fxpptr = (ulong*)Unsafe.AsPointer(ref fxp);
  49. for (int i = 0; i < c; i++)
  50. {
  51. var temp = Unsafe.Add(ref source, i) * pow;
  52. for (int j = 0; j < onecount; j++)
  53. {
  54. fxpptr[i * onecount + j] = (ulong)temp[j];
  55. }
  56. }
  57. uint c1 = count % onecount;
  58. if (c1 != 0)
  59. {
  60. uint start = c * onecount;
  61. for (uint i = 0; i < c1; i++)
  62. {
  63. ref float temp = ref Unsafe.Add(ref value, start + i);
  64. ref ulong tempu = ref Unsafe.Add(ref fxp, start + i);
  65. tempu = (ulong)(temp * pow);
  66. }
  67. }
  68. }
  69. public void FxpConvertToDouble(ref ulong fxp, NiFpga_FxpTypeInfo dxpinfo, ref double value, uint count)
  70. {
  71. if (count == 0) return;
  72. double delta = Math.Pow(2, dxpinfo.integerWordLength - dxpinfo.wordLength);
  73. ulong mask = dxpinfo.wordLength == 64 ? ulong.MaxValue : ((1ul << dxpinfo.wordLength) - 1);
  74. if (dxpinfo.isSigned)
  75. {
  76. ulong signedmask = 1ul << (dxpinfo.wordLength - 1);
  77. for (int i = 0; i < count; i++)
  78. {
  79. ref double resultref = ref Unsafe.Add(ref value, i);
  80. ref ulong tempfxpref = ref Unsafe.Add(ref fxp, i);
  81. ulong tempdata = tempfxpref & mask;
  82. if ((tempdata & signedmask) != 0)
  83. {
  84. long signeddata = (long)(tempdata ^ mask);
  85. signeddata = (signeddata + 1) * -1;
  86. resultref = signeddata * delta;
  87. }
  88. else resultref = delta * tempdata;
  89. }
  90. }
  91. else
  92. {
  93. for (int i = 0; i < count; i++)
  94. {
  95. ref double resultref = ref Unsafe.Add(ref value, i);
  96. ref ulong tempfxpref = ref Unsafe.Add(ref fxp, i);
  97. ulong tempdata = tempfxpref & mask;
  98. resultref = delta * tempdata;
  99. }
  100. }
  101. }
  102. public void FxpConvertToFloat(ref ulong fxp, NiFpga_FxpTypeInfo dxpinfo, ref float value, uint count)
  103. {
  104. if (count == 0) return;
  105. float delta = MathF.Pow(2, dxpinfo.integerWordLength - dxpinfo.wordLength);
  106. ulong mask = dxpinfo.wordLength == 64 ? ulong.MaxValue : ((1ul << dxpinfo.wordLength) - 1);
  107. if (dxpinfo.isSigned)
  108. {
  109. ulong signedmask = 1ul << (dxpinfo.wordLength - 1);
  110. for (int i = 0; i < count; i++)
  111. {
  112. ref float resultref = ref Unsafe.Add(ref value, i);
  113. ref ulong tempfxpref = ref Unsafe.Add(ref fxp, i);
  114. ulong tempdata = tempfxpref & mask;
  115. if ((tempdata & signedmask) != 0)
  116. {
  117. long signeddata = (long)(tempdata ^ mask);
  118. signeddata = (signeddata + 1) * -1;
  119. resultref = signeddata * delta;
  120. }
  121. else resultref = delta * tempdata;
  122. }
  123. }
  124. else
  125. {
  126. for (int i = 0; i < count; i++)
  127. {
  128. ref float resultref = ref Unsafe.Add(ref value, i);
  129. ref ulong tempfxpref = ref Unsafe.Add(ref fxp, i);
  130. ulong tempdata = tempfxpref & mask;
  131. resultref = delta * tempdata;
  132. }
  133. }
  134. }
  135. }
  136. }