SIMDCalc.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. using FFTW.NET;
  2. using FxpConvert.Common;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Numerics;
  7. using System.Runtime.CompilerServices;
  8. using System.Runtime.Intrinsics;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace SIMDFxpConvert
  12. {
  13. public sealed class SIMDCalc : ICalc
  14. {
  15. public IAdd Add { get; } = new SIMDAdd();
  16. public ISubtract Subtract { get; } = new SIMDSubtract();
  17. public IMultiply Multiply { get; } = new SIMDMultiply();
  18. public IDivision Division { get; } = new SIMDDivision();
  19. public IFFT FFT { get; } = new SIMDFFT();
  20. public IArraySum Sum { get; } = new SIMDArraySum();
  21. public unsafe void Fill(ref float result, float value, uint count)
  22. {
  23. if (count == 0) return;
  24. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref result);
  25. var desc = Vector512<float>.One * value;
  26. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  27. uint c = count / onecount;
  28. uint c1 = count % onecount;
  29. uint start = c * onecount;
  30. for (int i = 0; i < c; i++)
  31. {
  32. ref var tempdesc = ref Unsafe.Add(ref source, i);
  33. tempdesc = desc;
  34. }
  35. if (c1 > 0)
  36. {
  37. float* resultptr = (float*)Unsafe.AsPointer(ref result);
  38. for (int i = 0; i < c1; i++)
  39. {
  40. resultptr[start + i] = value;
  41. }
  42. }
  43. }
  44. public unsafe void Fill(ref double result, double value, uint count)
  45. {
  46. if (count == 0) return;
  47. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref result);
  48. var desc = Vector512<double>.One * value;
  49. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  50. uint c = count / onecount;
  51. uint c1 = count % onecount;
  52. uint start = c * onecount;
  53. for (int i = 0; i < c; i++)
  54. {
  55. ref var tempdesc = ref Unsafe.Add(ref source, i);
  56. tempdesc = desc;
  57. }
  58. if (c1 > 0)
  59. {
  60. double* resultptr = (double*)Unsafe.AsPointer(ref result);
  61. for (int i = 0; i < c1; i++)
  62. {
  63. resultptr[start + i] = value;
  64. }
  65. }
  66. }
  67. }
  68. public sealed class SIMDArraySum : IArraySum
  69. {
  70. public unsafe float Rms(ref float value, uint count)
  71. {
  72. if (count == 0) return 0;
  73. if (count == 1) return value;
  74. float temp = 0;
  75. float* ptr = (float*)Unsafe.AsPointer(ref value);
  76. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  77. uint c = count / onecount;
  78. uint c1 = count % onecount;
  79. uint start = c * onecount;
  80. ref var refvalue = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref value);
  81. for (int i = 0; i < c; i++)
  82. {
  83. ref var tempref = ref Unsafe.Add(ref refvalue, i);
  84. var t = tempref * tempref;
  85. for (int j = 0; j < onecount; j++)
  86. {
  87. temp += t[j];
  88. }
  89. }
  90. if (c1 > 0)
  91. {
  92. for (int i = 0; i < c1; i++)
  93. {
  94. temp += (ptr[start + i] * ptr[start + i]);
  95. }
  96. }
  97. return MathF.Sqrt(temp / count);
  98. }
  99. public unsafe double Rms(ref double value, uint count)
  100. {
  101. if (count == 0) return 0;
  102. if (count == 1) return value;
  103. double temp = 0;
  104. double* ptr = (double*)Unsafe.AsPointer(ref value);
  105. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  106. uint c = count / onecount;
  107. uint c1 = count % onecount;
  108. uint start = c * onecount;
  109. ref var refvalue = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref value);
  110. for (int i = 0; i < c; i++)
  111. {
  112. ref var tempref = ref Unsafe.Add(ref refvalue, i);
  113. tempref *= tempref;
  114. for (int j = 0; j < onecount; j++)
  115. {
  116. temp += tempref[j];
  117. }
  118. }
  119. if (c1 > 0)
  120. {
  121. for (int i = 0; i < c1; i++)
  122. {
  123. temp += (ptr[start + i] * ptr[start + i]);
  124. }
  125. }
  126. return Math.Sqrt(temp / count);
  127. }
  128. public unsafe float Sum(ref float value, uint count)
  129. {
  130. if (count == 0) return 0;
  131. if (count == 1) return value;
  132. float* ptr = (float*)Unsafe.AsPointer(ref value);
  133. float temp = value;
  134. for (int i = 1; i < count; i++)
  135. {
  136. temp += ptr[i];
  137. }
  138. return temp;
  139. }
  140. public unsafe double Sum(ref double value, uint count)
  141. {
  142. if (count == 0) return 0;
  143. if (count == 1) return value;
  144. double* ptr = (double*)Unsafe.AsPointer(ref value);
  145. double temp = value;
  146. for (int i = 1; i < count; i++)
  147. {
  148. temp += ptr[i];
  149. }
  150. return temp;
  151. }
  152. }
  153. public sealed class SIMDFFT : IFFT
  154. {
  155. public void FFT(double[] real, double[] imaginary)
  156. {
  157. MathNet.Numerics.IntegralTransforms.Fourier.Forward(real,imaginary, MathNet.Numerics.IntegralTransforms.FourierOptions.Matlab);
  158. }
  159. public void FFT(float[] real, float[] imaginary)
  160. {
  161. MathNet.Numerics.IntegralTransforms.Fourier.Forward(real,imaginary, MathNet.Numerics.IntegralTransforms.FourierOptions.Matlab);
  162. }
  163. }
  164. public unsafe sealed class SIMDAdd : IAdd
  165. {
  166. public void Add(ref float left, float right, uint count, ref float result)
  167. {
  168. if (count == 0) return;
  169. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref left);
  170. ref var desc = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref result);
  171. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  172. uint c = count / onecount;
  173. uint c1 = count % onecount;
  174. uint start = c * onecount;
  175. var rightarray = Enumerable.Repeat(right, (int)onecount).ToArray();
  176. ref var tempright = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref rightarray[0]);
  177. for (int i = 0; i < c; i++)
  178. {
  179. ref var tempdesc = ref Unsafe.Add(ref desc, i);
  180. tempdesc = Unsafe.Add(ref source, i) + tempright;
  181. }
  182. if (c1 > 0)
  183. {
  184. float* leftptr = (float*)Unsafe.AsPointer(ref left);
  185. float* resultptr = (float*)Unsafe.AsPointer(ref result);
  186. for (int i = 0; i < c1; i++)
  187. {
  188. resultptr[start + i] = leftptr[start + i] + right;
  189. }
  190. }
  191. }
  192. public void Add(ref float left, ref float right, uint count, ref float result)
  193. {
  194. if (count == 0) return;
  195. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref left);
  196. ref var desc = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref result);
  197. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  198. uint c = count / onecount;
  199. uint c1 = count % onecount;
  200. uint start = c * onecount;
  201. ref var tempright = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref right);
  202. for (int i = 0; i < c; i++)
  203. {
  204. ref var tempdesc = ref Unsafe.Add(ref desc, i);
  205. tempdesc = Unsafe.Add(ref source, i) + Unsafe.Add(ref tempright, i);
  206. }
  207. if (c1 > 0)
  208. {
  209. float* leftptr = (float*)Unsafe.AsPointer(ref left);
  210. float* resultptr = (float*)Unsafe.AsPointer(ref result);
  211. float* rightptr = (float*)Unsafe.AsPointer(ref right);
  212. for (int i = 0; i < c1; i++)
  213. {
  214. resultptr[start + i] = leftptr[start + i] + rightptr[start + i];
  215. }
  216. }
  217. }
  218. public void Add(ref float left, float right, uint count)
  219. {
  220. if (count == 0) return;
  221. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref left);
  222. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  223. uint c = count / onecount;
  224. uint c1 = count % onecount;
  225. uint start = c * onecount;
  226. var rightarray = Enumerable.Repeat(right, (int)onecount).ToArray();
  227. ref var tempright = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref rightarray[0]);
  228. for (int i = 0; i < c; i++)
  229. {
  230. ref var tempdesc = ref Unsafe.Add(ref source, i);
  231. tempdesc += tempright;
  232. }
  233. if (c1 > 0)
  234. {
  235. float* leftptr = (float*)Unsafe.AsPointer(ref left);
  236. for (int i = 0; i < c1; i++)
  237. {
  238. leftptr[start + i] += right;
  239. }
  240. }
  241. }
  242. public void Add(ref float left, ref float right, uint count)
  243. {
  244. if (count == 0) return;
  245. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref left);
  246. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  247. uint c = count / onecount;
  248. uint c1 = count % onecount;
  249. uint start = c * onecount;
  250. ref var tempright = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref right);
  251. for (int i = 0; i < c; i++)
  252. {
  253. ref var tempdesc = ref Unsafe.Add(ref source, i);
  254. tempdesc += Unsafe.Add(ref tempright, i);
  255. }
  256. if (c1 > 0)
  257. {
  258. float* leftptr = (float*)Unsafe.AsPointer(ref left);
  259. float* rightptr = (float*)Unsafe.AsPointer(ref right);
  260. for (int i = 0; i < c1; i++)
  261. {
  262. leftptr[start + i] += rightptr[start + i];
  263. }
  264. }
  265. }
  266. public void Add(ref double left, double right, uint count, ref double result)
  267. {
  268. if (count == 0) return;
  269. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref left);
  270. ref var desc = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref result);
  271. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  272. uint c = count / onecount;
  273. uint c1 = count % onecount;
  274. uint start = c * onecount;
  275. var rightarray = Enumerable.Repeat(right, (int)onecount).ToArray();
  276. ref var tempright = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref rightarray[0]);
  277. for (int i = 0; i < c; i++)
  278. {
  279. ref var tempdesc = ref Unsafe.Add(ref desc, i);
  280. tempdesc = Unsafe.Add(ref source, i) + tempright;
  281. }
  282. if (c1 > 0)
  283. {
  284. double* leftptr = (double*)Unsafe.AsPointer(ref left);
  285. double* resultptr = (double*)Unsafe.AsPointer(ref result);
  286. for (int i = 0; i < c1; i++)
  287. {
  288. resultptr[start + i] = leftptr[start + i] + right;
  289. }
  290. }
  291. }
  292. public void Add(ref double left, ref double right, uint count, ref double result)
  293. {
  294. if (count == 0) return;
  295. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref left);
  296. ref var desc = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref result);
  297. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  298. uint c = count / onecount;
  299. uint c1 = count % onecount;
  300. uint start = c * onecount;
  301. ref var tempright = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref right);
  302. for (int i = 0; i < c; i++)
  303. {
  304. ref var tempdesc = ref Unsafe.Add(ref desc, i);
  305. tempdesc = Unsafe.Add(ref source, i) + Unsafe.Add(ref tempright, i);
  306. }
  307. if (c1 > 0)
  308. {
  309. double* leftptr = (double*)Unsafe.AsPointer(ref left);
  310. double* resultptr = (double*)Unsafe.AsPointer(ref result);
  311. double* rightptr = (double*)Unsafe.AsPointer(ref right);
  312. for (int i = 0; i < c1; i++)
  313. {
  314. resultptr[start + i] = leftptr[start + i] + rightptr[start + i];
  315. }
  316. }
  317. }
  318. public void Add(ref double left, double right, uint count)
  319. {
  320. if (count == 0) return;
  321. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref left);
  322. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  323. uint c = count / onecount;
  324. uint c1 = count % onecount;
  325. uint start = c * onecount;
  326. var rightarray = Enumerable.Repeat(right, (int)onecount).ToArray();
  327. ref var tempright = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref rightarray[0]);
  328. for (int i = 0; i < c; i++)
  329. {
  330. ref var tempdesc = ref Unsafe.Add(ref source, i);
  331. tempdesc += tempright;
  332. }
  333. if (c1 > 0)
  334. {
  335. double* leftptr = (double*)Unsafe.AsPointer(ref left);
  336. for (int i = 0; i < c1; i++)
  337. {
  338. leftptr[start + i] += right;
  339. }
  340. }
  341. }
  342. public void Add(ref double left, ref double right, uint count)
  343. {
  344. if (count == 0) return;
  345. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref left);
  346. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  347. uint c = count / onecount;
  348. uint c1 = count % onecount;
  349. uint start = c * onecount;
  350. ref var tempright = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref right);
  351. for (int i = 0; i < c; i++)
  352. {
  353. ref var tempdesc = ref Unsafe.Add(ref source, i);
  354. tempdesc += Unsafe.Add(ref tempright, i);
  355. }
  356. if (c1 > 0)
  357. {
  358. double* leftptr = (double*)Unsafe.AsPointer(ref left);
  359. double* rightptr = (double*)Unsafe.AsPointer(ref right);
  360. for (int i = 0; i < c1; i++)
  361. {
  362. leftptr[start + i] += rightptr[start + i];
  363. }
  364. }
  365. }
  366. }
  367. public unsafe sealed class SIMDSubtract : ISubtract
  368. {
  369. public void Subtract(ref float left, float right, uint count, ref float result)
  370. {
  371. if (count == 0) return;
  372. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref left);
  373. ref var desc = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref result);
  374. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  375. uint c = count / onecount;
  376. uint c1 = count % onecount;
  377. uint start = c * onecount;
  378. var rightarray = Enumerable.Repeat(right, (int)onecount).ToArray();
  379. ref var tempright = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref rightarray[0]);
  380. for (int i = 0; i < c; i++)
  381. {
  382. ref var tempdesc = ref Unsafe.Add(ref desc, i);
  383. tempdesc = Unsafe.Add(ref source, i) - tempright;
  384. }
  385. if (c1 > 0)
  386. {
  387. float* leftptr = (float*)Unsafe.AsPointer(ref left);
  388. float* resultptr = (float*)Unsafe.AsPointer(ref result);
  389. for (int i = 0; i < c1; i++)
  390. {
  391. resultptr[start + i] = leftptr[start + i] - right;
  392. }
  393. }
  394. }
  395. public void Subtract(ref float left, ref float right, uint count, ref float result)
  396. {
  397. if (count == 0) return;
  398. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref left);
  399. ref var desc = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref result);
  400. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  401. uint c = count / onecount;
  402. uint c1 = count % onecount;
  403. uint start = c * onecount;
  404. ref var tempright = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref right);
  405. for (int i = 0; i < c; i++)
  406. {
  407. ref var tempdesc = ref Unsafe.Add(ref desc, i);
  408. tempdesc = Unsafe.Add(ref source, i) - Unsafe.Add(ref tempright, i);
  409. }
  410. if (c1 > 0)
  411. {
  412. float* leftptr = (float*)Unsafe.AsPointer(ref left);
  413. float* resultptr = (float*)Unsafe.AsPointer(ref result);
  414. float* rightptr = (float*)Unsafe.AsPointer(ref right);
  415. for (int i = 0; i < c1; i++)
  416. {
  417. resultptr[start + i] = leftptr[start + i] - rightptr[start + i];
  418. }
  419. }
  420. }
  421. public void Subtract(ref float left, float right, uint count)
  422. {
  423. if (count == 0) return;
  424. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref left);
  425. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  426. uint c = count / onecount;
  427. uint c1 = count % onecount;
  428. uint start = c * onecount;
  429. var rightarray = Enumerable.Repeat(right, (int)onecount).ToArray();
  430. ref var tempright = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref rightarray[0]);
  431. for (int i = 0; i < c; i++)
  432. {
  433. ref var tempdesc = ref Unsafe.Add(ref source, i);
  434. tempdesc -= tempright;
  435. }
  436. if (c1 > 0)
  437. {
  438. float* leftptr = (float*)Unsafe.AsPointer(ref left);
  439. for (int i = 0; i < c1; i++)
  440. {
  441. leftptr[start + i] -= right;
  442. }
  443. }
  444. }
  445. public void Subtract(ref float left, ref float right, uint count)
  446. {
  447. if (count == 0) return;
  448. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref left);
  449. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  450. uint c = count / onecount;
  451. uint c1 = count % onecount;
  452. uint start = c * onecount;
  453. ref var tempright = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref right);
  454. for (int i = 0; i < c; i++)
  455. {
  456. ref var tempdesc = ref Unsafe.Add(ref source, i);
  457. tempdesc -= Unsafe.Add(ref tempright, i);
  458. }
  459. if (c1 > 0)
  460. {
  461. float* leftptr = (float*)Unsafe.AsPointer(ref left);
  462. float* rightptr = (float*)Unsafe.AsPointer(ref right);
  463. for (int i = 0; i < c1; i++)
  464. {
  465. leftptr[start + i] -= rightptr[start + i];
  466. }
  467. }
  468. }
  469. public void Subtract(ref double left, double right, uint count, ref double result)
  470. {
  471. if (count == 0) return;
  472. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref left);
  473. ref var desc = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref result);
  474. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  475. uint c = count / onecount;
  476. uint c1 = count % onecount;
  477. uint start = c * onecount;
  478. var rightarray = Enumerable.Repeat(right, (int)onecount).ToArray();
  479. ref var tempright = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref rightarray[0]);
  480. for (int i = 0; i < c; i++)
  481. {
  482. ref var tempdesc = ref Unsafe.Add(ref desc, i);
  483. tempdesc = Unsafe.Add(ref source, i) - tempright;
  484. }
  485. if (c1 > 0)
  486. {
  487. double* leftptr = (double*)Unsafe.AsPointer(ref left);
  488. double* resultptr = (double*)Unsafe.AsPointer(ref result);
  489. for (int i = 0; i < c1; i++)
  490. {
  491. resultptr[start + i] = leftptr[start + i] - right;
  492. }
  493. }
  494. }
  495. public void Subtract(ref double left, ref double right, uint count, ref double result)
  496. {
  497. if (count == 0) return;
  498. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref left);
  499. ref var desc = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref result);
  500. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  501. uint c = count / onecount;
  502. uint c1 = count % onecount;
  503. uint start = c * onecount;
  504. ref var tempright = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref right);
  505. for (int i = 0; i < c; i++)
  506. {
  507. ref var tempdesc = ref Unsafe.Add(ref desc, i);
  508. tempdesc = Unsafe.Add(ref source, i) - Unsafe.Add(ref tempright, i);
  509. }
  510. if (c1 > 0)
  511. {
  512. double* leftptr = (double*)Unsafe.AsPointer(ref left);
  513. double* resultptr = (double*)Unsafe.AsPointer(ref result);
  514. double* rightptr = (double*)Unsafe.AsPointer(ref right);
  515. for (int i = 0; i < c1; i++)
  516. {
  517. resultptr[start + i] = leftptr[start + i] - rightptr[start + i];
  518. }
  519. }
  520. }
  521. public void Subtract(ref double left, double right, uint count)
  522. {
  523. if (count == 0) return;
  524. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref left);
  525. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  526. uint c = count / onecount;
  527. uint c1 = count % onecount;
  528. uint start = c * onecount;
  529. var rightarray = Enumerable.Repeat(right, (int)onecount).ToArray();
  530. ref var tempright = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref rightarray[0]);
  531. for (int i = 0; i < c; i++)
  532. {
  533. ref var tempdesc = ref Unsafe.Add(ref source, i);
  534. tempdesc -= tempright;
  535. }
  536. if (c1 > 0)
  537. {
  538. double* leftptr = (double*)Unsafe.AsPointer(ref left);
  539. for (int i = 0; i < c1; i++)
  540. {
  541. leftptr[start + i] -= right;
  542. }
  543. }
  544. }
  545. public void Subtract(ref double left, ref double right, uint count)
  546. {
  547. if (count == 0) return;
  548. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref left);
  549. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  550. uint c = count / onecount;
  551. uint c1 = count % onecount;
  552. uint start = c * onecount;
  553. ref var tempright = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref right);
  554. for (int i = 0; i < c; i++)
  555. {
  556. ref var tempdesc = ref Unsafe.Add(ref source, i);
  557. tempdesc -= Unsafe.Add(ref tempright, i);
  558. }
  559. if (c1 > 0)
  560. {
  561. double* leftptr = (double*)Unsafe.AsPointer(ref left);
  562. double* rightptr = (double*)Unsafe.AsPointer(ref right);
  563. for (int i = 0; i < c1; i++)
  564. {
  565. leftptr[start + i] -= rightptr[start + i];
  566. }
  567. }
  568. }
  569. }
  570. public unsafe sealed class SIMDMultiply : IMultiply
  571. {
  572. public void Multiply(ref float left, float right, uint count, ref float result)
  573. {
  574. if (count == 0) return;
  575. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref left);
  576. ref var desc = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref result);
  577. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  578. uint c = count / onecount;
  579. uint c1 = count % onecount;
  580. uint start = c * onecount;
  581. for (int i = 0; i < c; i++)
  582. {
  583. ref var tempdesc = ref Unsafe.Add(ref desc, i);
  584. tempdesc = Unsafe.Add(ref source, i) * right;
  585. }
  586. if (c1 > 0)
  587. {
  588. float* leftptr = (float*)Unsafe.AsPointer(ref left);
  589. float* resultptr = (float*)Unsafe.AsPointer(ref result);
  590. for (int i = 0; i < c1; i++)
  591. {
  592. resultptr[start + i] = leftptr[start + i] * right;
  593. }
  594. }
  595. }
  596. public void Multiply(ref float left, ref float right, uint count, ref float result)
  597. {
  598. if (count == 0) return;
  599. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref left);
  600. ref var desc = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref result);
  601. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  602. uint c = count / onecount;
  603. uint c1 = count % onecount;
  604. uint start = c * onecount;
  605. ref var tempright = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref right);
  606. for (int i = 0; i < c; i++)
  607. {
  608. ref var tempdesc = ref Unsafe.Add(ref desc, i);
  609. tempdesc = Unsafe.Add(ref source, i) * Unsafe.Add(ref tempright, i);
  610. }
  611. if (c1 > 0)
  612. {
  613. float* leftptr = (float*)Unsafe.AsPointer(ref left);
  614. float* resultptr = (float*)Unsafe.AsPointer(ref result);
  615. float* rightptr = (float*)Unsafe.AsPointer(ref right);
  616. for (int i = 0; i < c1; i++)
  617. {
  618. resultptr[start + i] = leftptr[start + i] * rightptr[start + i];
  619. }
  620. }
  621. }
  622. public void Multiply(ref float left, float right, uint count)
  623. {
  624. if (count == 0) return;
  625. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref left);
  626. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  627. uint c = count / onecount;
  628. uint c1 = count % onecount;
  629. uint start = c * onecount;
  630. for (int i = 0; i < c; i++)
  631. {
  632. ref var tempdesc = ref Unsafe.Add(ref source, i);
  633. tempdesc *= right;
  634. }
  635. if (c1 > 0)
  636. {
  637. float* leftptr = (float*)Unsafe.AsPointer(ref left);
  638. for (int i = 0; i < c1; i++)
  639. {
  640. leftptr[start + i] *= right;
  641. }
  642. }
  643. }
  644. public void Multiply(ref float left, ref float right, uint count)
  645. {
  646. if (count == 0) return;
  647. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref left);
  648. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  649. uint c = count / onecount;
  650. uint c1 = count % onecount;
  651. uint start = c * onecount;
  652. ref var tempright = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref right);
  653. for (int i = 0; i < c; i++)
  654. {
  655. ref var tempdesc = ref Unsafe.Add(ref source, i);
  656. tempdesc *= Unsafe.Add(ref tempright, i);
  657. }
  658. if (c1 > 0)
  659. {
  660. float* leftptr = (float*)Unsafe.AsPointer(ref left);
  661. float* rightptr = (float*)Unsafe.AsPointer(ref right);
  662. for (int i = 0; i < c1; i++)
  663. {
  664. leftptr[start + i] *= rightptr[start + i];
  665. }
  666. }
  667. }
  668. public void Multiply(ref double left, double right, uint count, ref double result)
  669. {
  670. if (count == 0) return;
  671. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref left);
  672. ref var desc = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref result);
  673. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  674. uint c = count / onecount;
  675. uint c1 = count % onecount;
  676. uint start = c * onecount;
  677. for (int i = 0; i < c; i++)
  678. {
  679. ref var tempdesc = ref Unsafe.Add(ref desc, i);
  680. tempdesc = Unsafe.Add(ref source, i) * right;
  681. }
  682. if (c1 > 0)
  683. {
  684. double* leftptr = (double*)Unsafe.AsPointer(ref left);
  685. double* resultptr = (double*)Unsafe.AsPointer(ref result);
  686. for (int i = 0; i < c1; i++)
  687. {
  688. resultptr[start + i] = leftptr[start + i] * right;
  689. }
  690. }
  691. }
  692. public void Multiply(ref double left, ref double right, uint count, ref double result)
  693. {
  694. if (count == 0) return;
  695. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref left);
  696. ref var desc = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref result);
  697. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  698. uint c = count / onecount;
  699. uint c1 = count % onecount;
  700. uint start = c * onecount;
  701. ref var tempright = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref right);
  702. for (int i = 0; i < c; i++)
  703. {
  704. ref var tempdesc = ref Unsafe.Add(ref desc, i);
  705. tempdesc = Unsafe.Add(ref source, i) * Unsafe.Add(ref tempright, i);
  706. }
  707. if (c1 > 0)
  708. {
  709. double* leftptr = (double*)Unsafe.AsPointer(ref left);
  710. double* resultptr = (double*)Unsafe.AsPointer(ref result);
  711. double* rightptr = (double*)Unsafe.AsPointer(ref right);
  712. for (int i = 0; i < c1; i++)
  713. {
  714. resultptr[start + i] = leftptr[start + i] * rightptr[start + i];
  715. }
  716. }
  717. }
  718. public void Multiply(ref double left, double right, uint count)
  719. {
  720. if (count == 0) return;
  721. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref left);
  722. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  723. uint c = count / onecount;
  724. uint c1 = count % onecount;
  725. uint start = c * onecount;
  726. for (int i = 0; i < c; i++)
  727. {
  728. ref var tempdesc = ref Unsafe.Add(ref source, i);
  729. tempdesc *= right;
  730. }
  731. if (c1 > 0)
  732. {
  733. double* leftptr = (double*)Unsafe.AsPointer(ref left);
  734. for (int i = 0; i < c1; i++)
  735. {
  736. leftptr[start + i] *= right;
  737. }
  738. }
  739. }
  740. public void Multiply(ref double left, ref double right, uint count)
  741. {
  742. if (count == 0) return;
  743. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref left);
  744. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  745. uint c = count / onecount;
  746. uint c1 = count % onecount;
  747. uint start = c * onecount;
  748. ref var tempright = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref right);
  749. for (int i = 0; i < c; i++)
  750. {
  751. ref var tempdesc = ref Unsafe.Add(ref source, i);
  752. tempdesc *= Unsafe.Add(ref tempright, i);
  753. }
  754. if (c1 > 0)
  755. {
  756. double* leftptr = (double*)Unsafe.AsPointer(ref left);
  757. double* rightptr = (double*)Unsafe.AsPointer(ref right);
  758. for (int i = 0; i < c1; i++)
  759. {
  760. leftptr[start + i] *= rightptr[start + i];
  761. }
  762. }
  763. }
  764. }
  765. public unsafe sealed class SIMDDivision : IDivision
  766. {
  767. public void Division(ref float left, float right, uint count, ref float result)
  768. {
  769. if (count == 0) return;
  770. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref left);
  771. ref var desc = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref result);
  772. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  773. uint c = count / onecount;
  774. uint c1 = count % onecount;
  775. uint start = c * onecount;
  776. for (int i = 0; i < c; i++)
  777. {
  778. ref var tempdesc = ref Unsafe.Add(ref desc, i);
  779. tempdesc = Unsafe.Add(ref source, i) / right;
  780. }
  781. if (c1 > 0)
  782. {
  783. float* leftptr = (float*)Unsafe.AsPointer(ref left);
  784. float* resultptr = (float*)Unsafe.AsPointer(ref result);
  785. for (int i = 0; i < c1; i++)
  786. {
  787. resultptr[start + i] = leftptr[start + i] / right;
  788. }
  789. }
  790. }
  791. public void Division(ref float left, ref float right, uint count, ref float result)
  792. {
  793. if (count == 0) return;
  794. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref left);
  795. ref var desc = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref result);
  796. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  797. uint c = count / onecount;
  798. uint c1 = count % onecount;
  799. uint start = c * onecount;
  800. ref var tempright = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref right);
  801. for (int i = 0; i < c; i++)
  802. {
  803. ref var tempdesc = ref Unsafe.Add(ref desc, i);
  804. tempdesc = Unsafe.Add(ref source, i) / Unsafe.Add(ref tempright, i);
  805. }
  806. if (c1 > 0)
  807. {
  808. float* leftptr = (float*)Unsafe.AsPointer(ref left);
  809. float* resultptr = (float*)Unsafe.AsPointer(ref result);
  810. float* rightptr = (float*)Unsafe.AsPointer(ref right);
  811. for (int i = 0; i < c1; i++)
  812. {
  813. resultptr[start + i] = leftptr[start + i] / rightptr[start + i];
  814. }
  815. }
  816. }
  817. public void Division(ref float left, float right, uint count)
  818. {
  819. if (count == 0) return;
  820. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref left);
  821. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  822. uint c = count / onecount;
  823. uint c1 = count % onecount;
  824. uint start = c * onecount;
  825. for (int i = 0; i < c; i++)
  826. {
  827. ref var tempdesc = ref Unsafe.Add(ref source, i);
  828. tempdesc /= right;
  829. }
  830. if (c1 > 0)
  831. {
  832. float* leftptr = (float*)Unsafe.AsPointer(ref left);
  833. for (int i = 0; i < c1; i++)
  834. {
  835. leftptr[start + i] /= right;
  836. }
  837. }
  838. }
  839. public void Division(ref float left, ref float right, uint count)
  840. {
  841. if (count == 0) return;
  842. ref var source = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref left);
  843. uint onecount = (uint)(512 / (Unsafe.SizeOf<float>() * 8));
  844. uint c = count / onecount;
  845. uint c1 = count % onecount;
  846. uint start = c * onecount;
  847. ref var tempright = ref Unsafe.As<float, System.Runtime.Intrinsics.Vector512<float>>(ref right);
  848. for (int i = 0; i < c; i++)
  849. {
  850. ref var tempdesc = ref Unsafe.Add(ref source, i);
  851. tempdesc /= Unsafe.Add(ref tempright, i);
  852. }
  853. if (c1 > 0)
  854. {
  855. float* leftptr = (float*)Unsafe.AsPointer(ref left);
  856. float* rightptr = (float*)Unsafe.AsPointer(ref right);
  857. for (int i = 0; i < c1; i++)
  858. {
  859. leftptr[start + i] /= rightptr[start + i];
  860. }
  861. }
  862. }
  863. public void Division(ref double left, double right, uint count, ref double result)
  864. {
  865. if (count == 0) return;
  866. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref left);
  867. ref var desc = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref result);
  868. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  869. uint c = count / onecount;
  870. uint c1 = count % onecount;
  871. uint start = c * onecount;
  872. for (int i = 0; i < c; i++)
  873. {
  874. ref var tempdesc = ref Unsafe.Add(ref desc, i);
  875. tempdesc = Unsafe.Add(ref source, i) / right;
  876. }
  877. if (c1 > 0)
  878. {
  879. double* leftptr = (double*)Unsafe.AsPointer(ref left);
  880. double* resultptr = (double*)Unsafe.AsPointer(ref result);
  881. for (int i = 0; i < c1; i++)
  882. {
  883. resultptr[start + i] = leftptr[start + i] / right;
  884. }
  885. }
  886. }
  887. public void Division(ref double left, ref double right, uint count, ref double result)
  888. {
  889. if (count == 0) return;
  890. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref left);
  891. ref var desc = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref result);
  892. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  893. uint c = count / onecount;
  894. uint c1 = count % onecount;
  895. uint start = c * onecount;
  896. ref var tempright = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref right);
  897. for (int i = 0; i < c; i++)
  898. {
  899. ref var tempdesc = ref Unsafe.Add(ref desc, i);
  900. tempdesc = Unsafe.Add(ref source, i) / Unsafe.Add(ref tempright, i);
  901. }
  902. if (c1 > 0)
  903. {
  904. double* leftptr = (double*)Unsafe.AsPointer(ref left);
  905. double* resultptr = (double*)Unsafe.AsPointer(ref result);
  906. double* rightptr = (double*)Unsafe.AsPointer(ref right);
  907. for (int i = 0; i < c1; i++)
  908. {
  909. resultptr[start + i] = leftptr[start + i] / rightptr[start + i];
  910. }
  911. }
  912. }
  913. public void Division(ref double left, double right, uint count)
  914. {
  915. if (count == 0) return;
  916. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref left);
  917. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  918. uint c = count / onecount;
  919. uint c1 = count % onecount;
  920. uint start = c * onecount;
  921. for (int i = 0; i < c; i++)
  922. {
  923. ref var tempdesc = ref Unsafe.Add(ref source, i);
  924. tempdesc /= right;
  925. }
  926. if (c1 > 0)
  927. {
  928. double* leftptr = (double*)Unsafe.AsPointer(ref left);
  929. for (int i = 0; i < c1; i++)
  930. {
  931. leftptr[start + i] /= right;
  932. }
  933. }
  934. }
  935. public void Division(ref double left, ref double right, uint count)
  936. {
  937. if (count == 0) return;
  938. ref var source = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref left);
  939. uint onecount = (uint)(512 / (Unsafe.SizeOf<double>() * 8));
  940. uint c = count / onecount;
  941. uint c1 = count % onecount;
  942. uint start = c * onecount;
  943. ref var tempright = ref Unsafe.As<double, System.Runtime.Intrinsics.Vector512<double>>(ref right);
  944. for (int i = 0; i < c; i++)
  945. {
  946. ref var tempdesc = ref Unsafe.Add(ref source, i);
  947. tempdesc /= Unsafe.Add(ref tempright, i);
  948. }
  949. if (c1 > 0)
  950. {
  951. double* leftptr = (double*)Unsafe.AsPointer(ref left);
  952. double* rightptr = (double*)Unsafe.AsPointer(ref right);
  953. for (int i = 0; i < c1; i++)
  954. {
  955. leftptr[start + i] /= rightptr[start + i];
  956. }
  957. }
  958. }
  959. }
  960. }