SIMDCalc.cs 40 KB

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