powerpc.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. #pragma once
  2. /*
  3. The header contains code which translates SSE intrinsics
  4. to PowerPC AltiVec or software emulation.
  5. See also: https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/W51a7ffcf4dfd_4b40_9d82_446ebc23c550/page/Intel%20SSE%20to%20PowerPC%20AltiVec%20migration
  6. */
  7. /* Author: Vadim Rumyantsev <rumvadim@yandex-team.ru> */
  8. #if !defined(_ppc64_)
  9. #error "This header is for PowerPC (ppc64) platform only." \
  10. "Include sse.h instead of including this header directly."
  11. #endif
  12. #include <util/system/types.h>
  13. #include <util/system/compiler.h>
  14. #include <altivec.h>
  15. typedef __attribute__((__aligned__(8))) unsigned long long __m64;
  16. typedef __attribute__((__aligned__(16), __may_alias__)) vector float __m128;
  17. typedef __attribute__((__aligned__(16), __may_alias__)) vector unsigned char __m128i;
  18. typedef __attribute__((__aligned__(16), __may_alias__)) vector double __m128d;
  19. using __v2df = __vector double;
  20. using __v2di = __vector long long;
  21. using __v2du = __vector unsigned long long;
  22. using __v4si = __vector int;
  23. using __v4su = __vector unsigned int;
  24. using __v8hi = __vector short;
  25. using __v8hu = __vector unsigned short;
  26. using __v16qi = __vector signed char;
  27. using __v16qu = __vector unsigned char;
  28. using __v4sf = __vector float;
  29. enum _mm_hint
  30. {
  31. /* _MM_HINT_ET is _MM_HINT_T with set 3rd bit. */
  32. _MM_HINT_ET0 = 7,
  33. _MM_HINT_ET1 = 6,
  34. _MM_HINT_T0 = 3,
  35. _MM_HINT_T1 = 2,
  36. _MM_HINT_T2 = 1,
  37. _MM_HINT_NTA = 0
  38. };
  39. #define _MM_SHUFFLE(a, b, c, d) ((signed char)(a * 64 + b * 16 + c * 4 + d))
  40. /// Functions that work with floats.
  41. Y_FORCE_INLINE __m128 _mm_setzero_ps() {
  42. return (__m128){0.0f, 0.0f, 0.0f, 0.0f};
  43. };
  44. Y_FORCE_INLINE __m128d _mm_setzero_pd() {
  45. return (__m128d)vec_splats((double)0);
  46. }
  47. // bug in clang compiler until 7.0.0 inclusive, Y_NO_INLINE is vital/essential
  48. static Y_NO_INLINE __m128 _mm_set1_ps(float f) {
  49. return (vector float)f;
  50. }
  51. Y_FORCE_INLINE __m128 _mm_set_ps1(float f) {
  52. return _mm_set1_ps(f);
  53. }
  54. Y_FORCE_INLINE __m128 _mm_set_ps(float v3, float v2, float v1, float v0) {
  55. return (__m128)(__v4sf){v0, v1, v2, v3};
  56. }
  57. Y_FORCE_INLINE __m128d _mm_set_pd(double d1, double d0) {
  58. return (__m128d){d0, d1};
  59. }
  60. Y_FORCE_INLINE __m128 _mm_loadu_ps(const float* p) {
  61. return vec_vsx_ld(0, p);
  62. }
  63. Y_FORCE_INLINE __m128 _mm_load_ps(const float* p) {
  64. return (__m128)vec_ld(0, (vector float*)p);
  65. }
  66. Y_FORCE_INLINE __m128d _mm_loadu_pd(const double* d) {
  67. return vec_vsx_ld(0, d);
  68. }
  69. Y_FORCE_INLINE void _mm_storeu_ps(float* p, __m128 a) {
  70. *(__m128*)p = a;
  71. }
  72. Y_FORCE_INLINE __m128 _mm_xor_ps(__m128 a, __m128 b) {
  73. return (__m128)vec_xor((__v4sf)a, (__v4sf)b);
  74. }
  75. Y_FORCE_INLINE __m128 _mm_xor_pd(__m128d a, __m128d b) {
  76. return (__m128)vec_xor((__v2df)a, (__v2df)b);
  77. }
  78. Y_FORCE_INLINE __m128 _mm_add_ps(__m128 a, __m128 b) {
  79. return (__m128)((__v4sf)a + (__v4sf)b);
  80. }
  81. Y_FORCE_INLINE __m128d _mm_add_pd(__m128d a, __m128d b) {
  82. return (__m128d)((__v2df)a + (__v2df)b);
  83. }
  84. Y_FORCE_INLINE __m128 _mm_sub_ps(__m128 a, __m128 b) {
  85. return (__m128)((__v4sf)a - (__v4sf)b);
  86. }
  87. Y_FORCE_INLINE __m128d _mm_sub_pd(__m128d a, __m128d b) {
  88. return (__m128d)((__v2df)a - (__v2df)b);
  89. }
  90. Y_FORCE_INLINE __m128 _mm_mul_ps(__m128 a, __m128 b) {
  91. return (__m128)((__v4sf)a * (__v4sf)b);
  92. }
  93. Y_FORCE_INLINE __m128d _mm_mul_pd(__m128d a, __m128d b) {
  94. return (__m128d)((__v2df)a * (__v2df)b);
  95. }
  96. Y_FORCE_INLINE __m128 _mm_div_ps(__m128 a, __m128 b) {
  97. return (__m128)((__v4sf)a / (__v4sf)b);
  98. }
  99. Y_FORCE_INLINE __m128d _mm_div_pd(__m128d a, __m128d b) {
  100. return (__m128d)((__v2df)a / (__v2df)b);
  101. }
  102. Y_FORCE_INLINE __m128 _mm_cmpeq_ps(__m128 a, __m128 b) {
  103. return ((__m128)vec_cmpeq((__v4sf)a, (__v4sf)b));
  104. ;
  105. }
  106. Y_FORCE_INLINE __m128 _mm_cmpgt_ps(__m128 a, __m128 b) {
  107. return ((__m128)vec_cmpgt((__v4sf)a, (__v4sf)b));
  108. }
  109. Y_FORCE_INLINE __m128 _mm_max_ps(__m128 a, __m128 b) {
  110. return (__m128)vec_max((vector float)a, (vector float)b);
  111. }
  112. Y_FORCE_INLINE __m128i _mm_max_epu8(__m128i a, __m128i b) {
  113. return (__m128i)vec_max((__v16qu)a, (__v16qu)b);
  114. }
  115. Y_FORCE_INLINE __m128 _mm_min_ps(__m128 a, __m128 b) {
  116. return (__m128)vec_min((vector float)a, (vector float)b);
  117. }
  118. Y_FORCE_INLINE __m128 _mm_and_ps(__m128 a, __m128 b) {
  119. return ((__m128)vec_and((__v4sf)a, (__v4sf)b));
  120. }
  121. Y_FORCE_INLINE __m128d _mm_and_pd(__m128d a, __m128d b) {
  122. return vec_and((__v2df)a, (__v2df)b);
  123. }
  124. Y_FORCE_INLINE __m128 _mm_rsqrt_ps(__m128 a) {
  125. return vec_rsqrte(a);
  126. }
  127. Y_FORCE_INLINE __m128 _mm_rsqrt_ss(__m128 a) {
  128. __m128 a1, c;
  129. const vector unsigned int mask = {0xffffffff, 0, 0, 0};
  130. a1 = vec_splat(a, 0);
  131. c = vec_rsqrte(a1);
  132. return (vec_sel((vector float)a, c, mask));
  133. }
  134. Y_FORCE_INLINE int _mm_movemask_ps(__m128 a) {
  135. __vector unsigned long long result;
  136. const __vector unsigned int perm_mask =
  137. {
  138. #ifdef __LITTLE_ENDIAN__
  139. 0x00204060, 0x80808080, 0x80808080, 0x80808080
  140. #elif __BIG_ENDIAN__
  141. 0x80808080, 0x80808080, 0x80808080, 0x00204060
  142. #endif
  143. };
  144. result = (__vector unsigned long long)vec_vbpermq((__vector unsigned char)a,
  145. (__vector unsigned char)perm_mask);
  146. #ifdef __LITTLE_ENDIAN__
  147. return result[1];
  148. #elif __BIG_ENDIAN__
  149. return result[0];
  150. #endif
  151. }
  152. Y_FORCE_INLINE __m128 _mm_cvtepi32_ps(__m128i a) {
  153. return ((__m128)vec_ctf((__v4si)a, 0));
  154. }
  155. Y_FORCE_INLINE float _mm_cvtss_f32(__m128 a) {
  156. return ((__v4sf)a)[0];
  157. }
  158. Y_FORCE_INLINE __m128 _mm_cmpunord_ps(__m128 A, __m128 B) {
  159. __vector unsigned int a, b;
  160. __vector unsigned int c, d;
  161. const __vector unsigned int float_exp_mask =
  162. {0x7f800000, 0x7f800000, 0x7f800000, 0x7f800000};
  163. a = (__vector unsigned int)vec_abs((__v4sf)A);
  164. b = (__vector unsigned int)vec_abs((__v4sf)B);
  165. c = (__vector unsigned int)vec_cmpgt(a, float_exp_mask);
  166. d = (__vector unsigned int)vec_cmpgt(b, float_exp_mask);
  167. return ((__m128)vec_or(c, d));
  168. }
  169. Y_FORCE_INLINE __m128 _mm_andnot_ps(__m128 a, __m128 b) {
  170. return ((__m128)vec_andc((__v4sf)b, (__v4sf)a));
  171. }
  172. Y_FORCE_INLINE __m128 _mm_or_ps(__m128 a, __m128 b) {
  173. return ((__m128)vec_or((__v4sf)a, (__v4sf)b));
  174. }
  175. Y_FORCE_INLINE void _mm_store_ss(float* p, __m128 a) {
  176. *p = ((__v4sf)a)[0];
  177. }
  178. Y_FORCE_INLINE void _mm_store_ps(float* p, __m128 a) {
  179. vec_st(a, 0, p);
  180. }
  181. Y_FORCE_INLINE void _mm_storeu_pd(double* p, __m128d a) {
  182. *(__m128d*)p = a;
  183. }
  184. Y_FORCE_INLINE void _mm_store_pd(double* p, __m128d a) {
  185. vec_st((vector unsigned char)a, 0, (vector unsigned char*)p);
  186. }
  187. Y_FORCE_INLINE __m128 _mm_shuffle_ps(__m128 a, __m128 b, long shuff) {
  188. unsigned long element_selector_10 = shuff & 0x03;
  189. unsigned long element_selector_32 = (shuff >> 2) & 0x03;
  190. unsigned long element_selector_54 = (shuff >> 4) & 0x03;
  191. unsigned long element_selector_76 = (shuff >> 6) & 0x03;
  192. const unsigned int permute_selectors[4] =
  193. {
  194. #ifdef __LITTLE_ENDIAN__
  195. 0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C
  196. #elif __BIG_ENDIAN__
  197. 0x0C0D0E0F, 0x08090A0B, 0x04050607, 0x00010203
  198. #endif
  199. };
  200. __vector unsigned int t;
  201. #ifdef __LITTLE_ENDIAN__
  202. t[0] = permute_selectors[element_selector_10];
  203. t[1] = permute_selectors[element_selector_32];
  204. t[2] = permute_selectors[element_selector_54] + 0x10101010;
  205. t[3] = permute_selectors[element_selector_76] + 0x10101010;
  206. #elif __BIG_ENDIAN__
  207. t[3] = permute_selectors[element_selector_10] + 0x10101010;
  208. t[2] = permute_selectors[element_selector_32] + 0x10101010;
  209. t[1] = permute_selectors[element_selector_54];
  210. t[0] = permute_selectors[element_selector_76];
  211. #endif
  212. return vec_perm((__v4sf)a, (__v4sf)b, (__vector unsigned char)t);
  213. }
  214. Y_FORCE_INLINE __m128d _mm_shuffle_pd(__m128d a, __m128d b, const int mask) {
  215. __vector double result;
  216. const int litmsk = mask & 0x3;
  217. if (litmsk == 0)
  218. result = vec_mergeh(a, b);
  219. else if (litmsk == 1)
  220. result = vec_xxpermdi(a, b, 2);
  221. else if (litmsk == 2)
  222. result = vec_xxpermdi(a, b, 1);
  223. else
  224. result = vec_mergel(a, b);
  225. return result;
  226. }
  227. Y_FORCE_INLINE __m128i _mm_cvtps_epi32(__m128 a) {
  228. vector float rounded;
  229. __v4si result;
  230. rounded = vec_rint((vector float)a);
  231. result = vec_cts(rounded, 0);
  232. return (__m128i)result;
  233. }
  234. /// Functions that work with integers.
  235. Y_FORCE_INLINE int _mm_movemask_epi8(__m128i a) {
  236. __vector unsigned long long result;
  237. const __vector unsigned char perm_mask =
  238. {
  239. #ifdef __LITTLE_ENDIAN__
  240. 0x78, 0x70, 0x68, 0x60, 0x58, 0x50, 0x48, 0x40,
  241. 0x38, 0x30, 0x28, 0x20, 0x18, 0x10, 0x08, 0x00
  242. #elif __BIG_ENDIAN__
  243. 0x00, 0x08, 0x10, 0x18, 0x20, 0x28, 0x30, 0x38,
  244. 0x40, 0x48, 0x50, 0x58, 0x60, 0x68, 0x70, 0x78
  245. #endif
  246. };
  247. result = (__vector unsigned long long)vec_vbpermq((__vector unsigned char)a,
  248. (__vector unsigned char)perm_mask);
  249. #ifdef __LITTLE_ENDIAN__
  250. return result[1];
  251. #elif __BIG_ENDIAN__
  252. return result[0];
  253. #endif
  254. }
  255. Y_FORCE_INLINE __m128i _mm_cvttps_epi32(__m128 a) {
  256. __v4si result;
  257. result = vec_cts((__v4sf)a, 0);
  258. return (__m128i)result;
  259. }
  260. #define _MM_TRANSPOSE4_PS(row0, row1, row2, row3) \
  261. do { \
  262. __v4sf __r0 = (row0), __r1 = (row1), __r2 = (row2), __r3 = (row3); \
  263. __v4sf __t0 = vec_vmrghw((vector unsigned int)__r0, (vector unsigned int)__r1); \
  264. __v4sf __t1 = vec_vmrghw((vector unsigned int)__r2, (vector unsigned int)__r3); \
  265. __v4sf __t2 = vec_vmrglw((vector unsigned int)__r0, (vector unsigned int)__r1); \
  266. __v4sf __t3 = vec_vmrglw((vector unsigned int)__r2, (vector unsigned int)__r3); \
  267. (row0) = (__v4sf)vec_mergeh((vector long long)__t0, \
  268. (vector long long)__t1); \
  269. (row1) = (__v4sf)vec_mergel((vector long long)__t0, \
  270. (vector long long)__t1); \
  271. (row2) = (__v4sf)vec_mergeh((vector long long)__t2, \
  272. (vector long long)__t3); \
  273. (row3) = (__v4sf)vec_mergel((vector long long)__t2, \
  274. (vector long long)__t3); \
  275. } while (0)
  276. Y_FORCE_INLINE __m128i _mm_or_si128(__m128i a, __m128i b) {
  277. return (__m128i)vec_or((__v2di)a, (__v2di)b);
  278. }
  279. Y_FORCE_INLINE __m128i _mm_and_si128(__m128i a, __m128i b) {
  280. return (__m128i)vec_and((__v2di)a, (__v2di)b);
  281. }
  282. Y_FORCE_INLINE __m128i _mm_andnot_si128(__m128i a, __m128i b) {
  283. return (__m128i)vec_andc((__v2di)b, (__v2di)a);
  284. }
  285. Y_FORCE_INLINE __m128i _mm_xor_si128(__m128i a, __m128i b) {
  286. return (__m128i)vec_xor((__v2di)a, (__v2di)b);
  287. }
  288. Y_FORCE_INLINE __m128i _mm_setzero_si128() {
  289. return (__m128i)(__v4si){0, 0, 0, 0};
  290. }
  291. Y_FORCE_INLINE __m128i _mm_shuffle_epi32(__m128i op1, long op2) {
  292. unsigned long element_selector_10 = op2 & 0x03;
  293. unsigned long element_selector_32 = (op2 >> 2) & 0x03;
  294. unsigned long element_selector_54 = (op2 >> 4) & 0x03;
  295. unsigned long element_selector_76 = (op2 >> 6) & 0x03;
  296. const unsigned int permute_selectors[4] =
  297. {
  298. #ifdef __LITTLE_ENDIAN__
  299. 0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C
  300. #elif __BIG_ENDIAN__
  301. 0x0C0D0E0F, 0x08090A0B, 0x04050607, 0x00010203
  302. #endif
  303. };
  304. __v4su t;
  305. #ifdef __LITTLE_ENDIAN__
  306. t[0] = permute_selectors[element_selector_10];
  307. t[1] = permute_selectors[element_selector_32];
  308. t[2] = permute_selectors[element_selector_54] + 0x10101010;
  309. t[3] = permute_selectors[element_selector_76] + 0x10101010;
  310. #elif __BIG_ENDIAN__
  311. t[3] = permute_selectors[element_selector_10] + 0x10101010;
  312. t[2] = permute_selectors[element_selector_32] + 0x10101010;
  313. t[1] = permute_selectors[element_selector_54];
  314. t[0] = permute_selectors[element_selector_76];
  315. #endif
  316. return (__m128i)vec_perm((__v4si)op1, (__v4si)op1, (__vector unsigned char)t);
  317. }
  318. Y_FORCE_INLINE int _mm_extract_epi16(__m128i a, int imm) {
  319. return (unsigned short)((__v8hi)a)[imm & 7];
  320. }
  321. Y_FORCE_INLINE int _mm_extract_epi8(__m128i a, int imm) {
  322. return (unsigned char)((__v16qi)a)[imm & 15];
  323. }
  324. Y_FORCE_INLINE int _mm_extract_epi32(__m128i a, int imm) {
  325. return ((__v4si)a)[imm & 3];
  326. }
  327. Y_FORCE_INLINE long long _mm_extract_epi64(__m128i a, int imm) {
  328. return ((__v2di)a)[imm & 1];
  329. }
  330. Y_FORCE_INLINE int _mm_extract_ps(__m128 a, int imm) {
  331. return ((__v4si)a)[imm & 3];
  332. }
  333. Y_FORCE_INLINE __m128i _mm_slli_epi16(__m128i a, int count) {
  334. __v8hu lshift;
  335. __v8hi result = {0, 0, 0, 0, 0, 0, 0, 0};
  336. if (count >= 0 && count < 16) {
  337. if (__builtin_constant_p(count)) {
  338. lshift = (__v8hu)vec_splat_s16(count);
  339. } else {
  340. lshift = vec_splats((unsigned short)count);
  341. }
  342. result = vec_vslh((__v8hi)a, lshift);
  343. }
  344. return (__m128i)result;
  345. }
  346. Y_FORCE_INLINE __m128i _mm_slli_epi32(__m128i a, int count) {
  347. __v4su lshift;
  348. __v4si result = {0, 0, 0, 0};
  349. if (count >= 0 && count < 32) {
  350. if (__builtin_constant_p(count) && count < 16) {
  351. lshift = (__v4su)vec_splat_s32(count);
  352. } else {
  353. lshift = vec_splats((unsigned int)count);
  354. }
  355. result = vec_vslw((__v4si)a, lshift);
  356. }
  357. return (__m128i)result;
  358. }
  359. Y_FORCE_INLINE __m128i _mm_slli_epi64(__m128i a, int count) {
  360. __v2du lshift;
  361. __v2di result = {0, 0};
  362. if (count >= 0 && count < 64) {
  363. if (__builtin_constant_p(count) && count < 16) {
  364. lshift = (__v2du)vec_splat_s32(count);
  365. } else {
  366. lshift = (__v2du)vec_splats((unsigned int)count);
  367. }
  368. result = vec_sl((__v2di)a, lshift);
  369. }
  370. return (__m128i)result;
  371. }
  372. Y_FORCE_INLINE __m128i _mm_slli_si128(__m128i a, int imm) {
  373. __v16qu result;
  374. const __v16qu zeros = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  375. if (imm < 16)
  376. #ifdef __LITTLE_ENDIAN__
  377. result = vec_sld((__v16qu)a, zeros, imm);
  378. #elif __BIG_ENDIAN__
  379. result = vec_sld(zeros, (__v16qu)a, (16 - imm));
  380. #endif
  381. else
  382. result = zeros;
  383. return (__m128i)result;
  384. }
  385. Y_FORCE_INLINE __m128i _mm_srli_epi16(__m128i a, int count) {
  386. if ((unsigned long)count >= 16) {
  387. /* SSE2 shifts >= element_size or < 0 produce 0; Altivec/MMX shifts by count%element_size. */
  388. return (__m128i)vec_splats(0);
  389. } else if (count == 0) {
  390. return a;
  391. } else {
  392. /* The PowerPC Architecture says all shift count fields must contain the same shift count. */
  393. __v8hi replicated_count;
  394. replicated_count = vec_splats((short)count);
  395. return (__m128i)vec_sr((vector signed short)a, (vector unsigned short)replicated_count);
  396. }
  397. }
  398. Y_FORCE_INLINE __m128i _mm_srli_epi32(__m128i a, int count) {
  399. if ((unsigned long)count >= 32) {
  400. /* SSE2 shifts >= element_size or < 0 produce 0; Altivec/MMX shifts by count%element_size. */
  401. return (__m128i)vec_splats(0);
  402. } else if (count == 0) {
  403. return a;
  404. } else {
  405. /* The PowerPC Architecture says all shift count fields must contain the same shift count. */
  406. __v4si replicated_count;
  407. replicated_count = vec_splats(count);
  408. return (__m128i)vec_sr((vector signed int)a, (vector unsigned int)replicated_count);
  409. }
  410. }
  411. Y_FORCE_INLINE __m128i _mm_srli_epi64(__m128i a, int count) {
  412. if ((unsigned long)count >= 64) {
  413. /* SSE2 shifts >= element_size or < 0 produce 0; Altivec/MMX shifts by count%element_size. */
  414. return (__m128i)vec_splats(0);
  415. } else if (count == 0) {
  416. return a;
  417. } else {
  418. /* The PowerPC Architecture says all shift count fields must contain the same shift count. */
  419. /* On Power7 vec_slo (vslo) does use just the documented bits 121:124. */
  420. /* On Power7 vec_sll (vsll) uses the lower 3 bits of each byte instead (legal). */
  421. __v16qu replicated_count;
  422. replicated_count = vec_splats((unsigned char)count);
  423. long long m = 0xFFFFFFFFFFFFFFFFull >> count;
  424. __v2di mask;
  425. mask[0] = m;
  426. mask[1] = m;
  427. return vec_and(vec_srl(vec_sro(a, (__m128i)replicated_count), (__m128i)replicated_count), (__v16qu)mask);
  428. }
  429. }
  430. Y_FORCE_INLINE __m128i _mm_bsrli_si128(__m128i a, const int __N) {
  431. __v16qu result;
  432. const __v16qu zeros = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  433. if (__N < 16)
  434. if (__builtin_constant_p(__N))
  435. /* Would like to use Vector Shift Left Double by Octet
  436. Immediate here to use the immediate form and avoid
  437. load of __N * 8 value into a separate VR. */
  438. result = vec_sld(zeros, (__v16qu)a, (16 - __N));
  439. else {
  440. __v16qu shift = vec_splats((unsigned char)(__N * 8));
  441. result = vec_sro((__v16qu)a, shift);
  442. }
  443. else
  444. result = zeros;
  445. return (__m128i)result;
  446. }
  447. Y_FORCE_INLINE __m128i _mm_srli_si128(__m128i a, int imm) {
  448. return _mm_bsrli_si128(a, imm);
  449. }
  450. Y_FORCE_INLINE __m128i _mm_srai_epi16(__m128i a, int count) {
  451. __v8hu rshift = {15, 15, 15, 15, 15, 15, 15, 15};
  452. __v8hi result;
  453. if (count < 16) {
  454. if (__builtin_constant_p(count)) {
  455. rshift = (__v8hu)vec_splat_s16(count);
  456. } else {
  457. rshift = vec_splats((unsigned short)count);
  458. }
  459. }
  460. result = vec_vsrah((__v8hi)a, rshift);
  461. return (__m128i)result;
  462. }
  463. Y_FORCE_INLINE __m128i _mm_srai_epi32(__m128i a, int count) {
  464. // return vec_shiftrightarithmetic4wimmediate(a, count); //!< Failes to work with count >= 32.
  465. __v4su rshift = {31, 31, 31, 31};
  466. __v4si result;
  467. if (count < 32) {
  468. if (__builtin_constant_p(count)) {
  469. if (count < 16) {
  470. rshift = (__v4su)vec_splat_s32(count);
  471. } else {
  472. rshift = (__v4su)vec_splats((unsigned int)count);
  473. }
  474. } else {
  475. rshift = vec_splats((unsigned int)count);
  476. }
  477. }
  478. result = vec_vsraw((__v4si)a, rshift);
  479. return (__m128i)result;
  480. }
  481. Y_FORCE_INLINE __m128i _mm_sll_epi16(__m128i a, __m128i count) {
  482. __v8hu lshift, shmask;
  483. const __v8hu shmax = {15, 15, 15, 15, 15, 15, 15, 15};
  484. __v8hu result;
  485. #ifdef __LITTLE_ENDIAN__
  486. lshift = vec_splat((__v8hu)count, 0);
  487. #elif __BIG_ENDIAN__
  488. lshift = vec_splat((__v8hu)count, 3);
  489. #endif
  490. shmask = vec_cmple(lshift, shmax);
  491. result = vec_vslh((__v8hu)a, lshift);
  492. result = vec_sel(shmask, result, shmask);
  493. return (__m128i)result;
  494. }
  495. Y_FORCE_INLINE __m128i _mm_sll_epi32(__m128i a, __m128i count) {
  496. __v4su lshift, shmask;
  497. const __v4su shmax = {32, 32, 32, 32};
  498. __v4su result;
  499. #ifdef __LITTLE_ENDIAN__
  500. lshift = vec_splat((__v4su)count, 0);
  501. #elif __BIG_ENDIAN__
  502. lshift = vec_splat((__v4su)count, 1);
  503. #endif
  504. shmask = vec_cmplt(lshift, shmax);
  505. result = vec_vslw((__v4su)a, lshift);
  506. result = vec_sel(shmask, result, shmask);
  507. return (__m128i)result;
  508. }
  509. Y_FORCE_INLINE __m128i _mm_sll_epi64(__m128i a, __m128i count) {
  510. __v2du lshift, shmask;
  511. const __v2du shmax = {64, 64};
  512. __v2du result;
  513. lshift = (__v2du)vec_splat((__v2du)count, 0);
  514. shmask = vec_cmplt(lshift, shmax);
  515. result = vec_sl((__v2du)a, lshift);
  516. result = result & shmask;
  517. return (__m128i)result;
  518. }
  519. Y_FORCE_INLINE __m128i _mm_srl_epi16(__m128i a, __m128i count) {
  520. __v8hu rshift, shmask;
  521. const __v8hu shmax = {15, 15, 15, 15, 15, 15, 15, 15};
  522. __v8hu result;
  523. #ifdef __LITTLE_ENDIAN__
  524. rshift = vec_splat((__v8hu)count, 0);
  525. #elif __BIG_ENDIAN__
  526. rshift = vec_splat((__v8hu)count, 3);
  527. #endif
  528. shmask = vec_cmple(rshift, shmax);
  529. result = vec_vsrh((__v8hu)a, rshift);
  530. result = vec_sel(shmask, result, shmask);
  531. return (__m128i)result;
  532. }
  533. Y_FORCE_INLINE __m128i _mm_srl_epi32(__m128i a, __m128i count) {
  534. __v4su rshift, shmask;
  535. const __v4su shmax = {32, 32, 32, 32};
  536. __v4su result;
  537. #ifdef __LITTLE_ENDIAN__
  538. rshift = vec_splat((__v4su)count, 0);
  539. #elif __BIG_ENDIAN__
  540. rshift = vec_splat((__v4su)count, 1);
  541. #endif
  542. shmask = vec_cmplt(rshift, shmax);
  543. result = vec_vsrw((__v4su)a, rshift);
  544. result = vec_sel(shmask, result, shmask);
  545. return (__m128i)result;
  546. }
  547. Y_FORCE_INLINE __m128i _mm_srl_epi64(__m128i a, __m128i count) {
  548. __v2du rshift, shmask;
  549. const __v2du shmax = {64, 64};
  550. __v2du result;
  551. rshift = (__v2du)vec_splat((__v2du)count, 0);
  552. shmask = vec_cmplt(rshift, shmax);
  553. result = vec_sr((__v2du)a, rshift);
  554. result = (__v2du)vec_sel((__v2du)shmask, (__v2du)result, (__v2du)shmask);
  555. return (__m128i)result;
  556. }
  557. Y_FORCE_INLINE void _mm_storeu_si128(__m128i* p, __m128i a) {
  558. vec_vsx_st(a, 0, p);
  559. }
  560. Y_FORCE_INLINE void _mm_store_si128(__m128i* p, __m128i a) {
  561. vec_st((__v16qu)a, 0, (__v16qu*)p);
  562. }
  563. Y_FORCE_INLINE __m128i _mm_unpackhi_epi8(__m128i a, __m128i b) {
  564. return (__m128i)vec_mergel((__v16qu)a, (__v16qu)b);
  565. }
  566. Y_FORCE_INLINE __m128i _mm_unpackhi_epi16(__m128i a, __m128i b) {
  567. return (__m128i)vec_mergel((__v8hu)a, (__v8hu)b);
  568. }
  569. Y_FORCE_INLINE __m128i _mm_unpackhi_epi32(__m128i a, __m128i b) {
  570. return (__m128i)vec_mergel((__v4su)a, (__v4su)b);
  571. }
  572. Y_FORCE_INLINE __m128i _mm_unpackhi_epi64(__m128i a, __m128i b) {
  573. return (__m128i)vec_mergel((vector long long)a, (vector long long)b);
  574. }
  575. Y_FORCE_INLINE __m128i _mm_unpacklo_epi8(__m128i a, __m128i b) {
  576. return (__m128i)vec_mergeh((__v16qu)a, (__v16qu)b);
  577. }
  578. Y_FORCE_INLINE __m128i _mm_unpacklo_epi16(__m128i a, __m128i b) {
  579. return (__m128i)vec_mergeh((__v8hi)a, (__v8hi)b);
  580. }
  581. Y_FORCE_INLINE __m128i _mm_unpacklo_epi32(__m128i a, __m128i b) {
  582. return (__m128i)vec_mergeh((__v4si)a, (__v4si)b);
  583. }
  584. Y_FORCE_INLINE __m128i _mm_unpacklo_epi64(__m128i a, __m128i b) {
  585. return (__m128i)vec_mergeh((vector long long)a, (vector long long)b);
  586. }
  587. Y_FORCE_INLINE __m128i _mm_add_epi8(__m128i a, __m128i b) {
  588. return (__m128i)((__v16qu)a + (__v16qu)b);
  589. }
  590. Y_FORCE_INLINE __m128i _mm_add_epi16(__m128i a, __m128i b) {
  591. return (__m128i)((__v8hu)a + (__v8hu)b);
  592. }
  593. Y_FORCE_INLINE __m128i _mm_add_epi32(__m128i a, __m128i b) {
  594. return (__m128i)((__v4su)a + (__v4su)b);
  595. }
  596. Y_FORCE_INLINE __m128i _mm_add_epi64(__m128i a, __m128i b) {
  597. return (__m128i)((__v2du)a + (__v2du)b);
  598. }
  599. Y_FORCE_INLINE __m128i _mm_madd_epi16(__m128i a, __m128i b) {
  600. const vector signed int zero = {0, 0, 0, 0};
  601. return (__m128i)vec_vmsumshm((__v8hi)a, (__v8hi)b, zero);
  602. }
  603. Y_FORCE_INLINE __m128i _mm_sub_epi8(__m128i a, __m128i b) {
  604. return (__m128i)((__v16qu)a - (__v16qu)b);
  605. }
  606. Y_FORCE_INLINE __m128i _mm_sub_epi16(__m128i a, __m128i b) {
  607. return (__m128i)((__v8hu)a - (__v8hu)b);
  608. }
  609. Y_FORCE_INLINE __m128i _mm_sub_epi32(__m128i a, __m128i b) {
  610. return (__m128i)((__v4su)a - (__v4su)b);
  611. }
  612. Y_FORCE_INLINE __m128i _mm_sub_epi64(__m128i a, __m128i b) {
  613. return (__m128i)((__v2du)a - (__v2du)b);
  614. }
  615. Y_FORCE_INLINE __m128i _mm_mul_epu32(__m128i a, __m128i b) {
  616. #ifdef __LITTLE_ENDIAN__
  617. return (__m128i)vec_mule((__v4su)a, (__v4su)b);
  618. #elif __BIG_ENDIAN__
  619. return (__m128i)vec_mulo((__v4su)a, (__v4su)b);
  620. #endif
  621. }
  622. Y_FORCE_INLINE __m128i _mm_set_epi8(char q15, char q14, char q13, char q12, char q11, char q10, char q09, char q08, char q07, char q06, char q05, char q04, char q03, char q02, char q01, char q00) {
  623. return (__m128i)(__v16qi){q00, q01, q02, q03, q04, q05, q06, q07, q08, q09, q10, q11, q12, q13, q14, q15};
  624. };
  625. Y_FORCE_INLINE __m128i _mm_setr_epi8(char q15, char q14, char q13, char q12, char q11, char q10, char q09, char q08, char q07, char q06, char q05, char q04, char q03, char q02, char q01, char q00) {
  626. return (__m128i)(__v16qi){q15, q14, q13, q12, q11, q10, q09, q08, q07, q06, q05, q04, q03, q02, q01, q00};
  627. };
  628. Y_FORCE_INLINE __m128i _mm_set_epi16(short q7, short q6, short q5, short q4, short q3, short q2, short q1, short q0) {
  629. return (__m128i)(__v8hi){q0, q1, q2, q3, q4, q5, q6, q7};
  630. }
  631. Y_FORCE_INLINE __m128i _mm_setr_epi16(short q7, short q6, short q5, short q4, short q3, short q2, short q1, short q0) {
  632. return (__m128i)(__v8hi){q7, q6, q5, q4, q3, q2, q1, q0};
  633. }
  634. Y_FORCE_INLINE __m128i _mm_set_epi32(int q3, int q2, int q1, int q0) {
  635. return (__m128i)(__v4si){q0, q1, q2, q3};
  636. }
  637. Y_FORCE_INLINE __m128i _mm_setr_epi32(int q3, int q2, int q1, int q0) {
  638. return (__m128i)(__v4si){q3, q2, q1, q0};
  639. }
  640. Y_FORCE_INLINE __m128i _mm_set1_epi8(char a) {
  641. return _mm_set_epi8(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a);
  642. }
  643. Y_FORCE_INLINE __m128i _mm_set1_epi16(short a) {
  644. return _mm_set_epi16(a, a, a, a, a, a, a, a);
  645. }
  646. Y_FORCE_INLINE __m128i _mm_set1_epi32(int a) {
  647. return _mm_set_epi32(a, a, a, a);
  648. }
  649. Y_FORCE_INLINE __m128i _mm_cmpeq_epi8(__m128i a, __m128i b) {
  650. return (__m128i)vec_cmpeq((__v16qi)a, (__v16qi)b);
  651. }
  652. Y_FORCE_INLINE __m128i _mm_cmpeq_epi16(__m128i a, __m128i b) {
  653. return (__m128i)vec_cmpeq((__v8hi)a, (__v8hi)b);
  654. }
  655. Y_FORCE_INLINE __m128i _mm_cmpeq_epi32(__m128i a, __m128i b) {
  656. return (__m128i)vec_cmpeq((__v4si)a, (__v4si)b);
  657. }
  658. Y_FORCE_INLINE __m128i _mm_packs_epi16(__m128i a, __m128i b) {
  659. return (__m128i)vec_packs((__v8hi)a, (__v8hi)b);
  660. }
  661. Y_FORCE_INLINE __m128i _mm_packs_epi32(__m128i a, __m128i b) {
  662. return (__m128i)vec_packs((__v4si)a, (__v4si)b);
  663. }
  664. Y_FORCE_INLINE __m128i _mm_packus_epi16(__m128i a, __m128i b) {
  665. return (__m128i)vec_packsu((vector signed short)a, (vector signed short)b);
  666. }
  667. Y_FORCE_INLINE __m128i _mm_cvtsi64_si128(i64 a) {
  668. return (__m128i)(__v2di){a, 0LL};
  669. }
  670. Y_FORCE_INLINE __m128i _mm_cvtsi32_si128(int a) {
  671. return _mm_set_epi32(0, 0, 0, a);
  672. }
  673. Y_FORCE_INLINE int _mm_cvtsi128_si32(__m128i a) {
  674. return ((__v4si)a)[0];
  675. }
  676. Y_FORCE_INLINE i64 _mm_cvtsi128_si64(__m128i a) {
  677. return ((__v2di)a)[0];
  678. }
  679. Y_FORCE_INLINE __m128i _mm_load_si128(const __m128i* p) {
  680. return *p;
  681. }
  682. Y_FORCE_INLINE __m128i _mm_loadu_si128(const __m128i* p) {
  683. return (__m128i)(vec_vsx_ld(0, (signed int const*)p));
  684. }
  685. Y_FORCE_INLINE __m128i _mm_lddqu_si128(const __m128i* p) {
  686. return _mm_loadu_si128(p);
  687. }
  688. Y_FORCE_INLINE __m128i _mm_loadl_epi64(const __m128i* a) {
  689. #ifdef __LITTLE_ENDIAN__
  690. const vector bool long long mask = {
  691. 0xFFFFFFFFFFFFFFFFull, 0x0000000000000000ull};
  692. #elif __BIG_ENDIAN__
  693. const vector bool long long mask = {
  694. 0x0000000000000000ull, 0xFFFFFFFFFFFFFFFFull};
  695. #endif
  696. return (__m128i)vec_and(_mm_loadu_si128(a), (vector unsigned char)mask);
  697. }
  698. Y_FORCE_INLINE void _mm_storel_epi64(__m128i* a, __m128i b) {
  699. *(long long*)a = ((__v2di)b)[0];
  700. }
  701. Y_FORCE_INLINE double _mm_cvtsd_f64(__m128d a) {
  702. return ((__v2df)a)[0];
  703. }
  704. #pragma GCC diagnostic push
  705. #pragma GCC diagnostic ignored "-Wuninitialized"
  706. Y_FORCE_INLINE __m128d _mm_undefined_pd(void) {
  707. __m128d ans = ans;
  708. return ans;
  709. }
  710. #pragma GCC diagnostic pop
  711. Y_FORCE_INLINE __m128d _mm_loadh_pd(__m128d a, const double* b) {
  712. __v2df result = (__v2df)a;
  713. result[1] = *b;
  714. return (__m128d)result;
  715. }
  716. Y_FORCE_INLINE __m128d _mm_loadl_pd(__m128d a, const double* b) {
  717. __v2df result = (__v2df)a;
  718. result[0] = *b;
  719. return (__m128d)result;
  720. }
  721. Y_FORCE_INLINE __m128 _mm_castsi128_ps(__m128i a) {
  722. return (__m128)a;
  723. }
  724. Y_FORCE_INLINE __m128i _mm_castps_si128(__m128 a) {
  725. return (__m128i)a;
  726. }
  727. Y_FORCE_INLINE __m128i _mm_cmpgt_epi8(__m128i a, __m128i b) {
  728. return (__m128i)vec_cmpgt((__v16qi)a, (__v16qi)b);
  729. }
  730. Y_FORCE_INLINE __m128i _mm_cmpgt_epi16(__m128i a, __m128i b) {
  731. return (__m128i)vec_cmpgt((__v8hi)a, (__v8hi)b);
  732. }
  733. Y_FORCE_INLINE __m128i _mm_cmpgt_epi32(__m128i a, __m128i b) {
  734. return (__m128i)vec_cmpgt((__v4si)a, (__v4si)b);
  735. }
  736. Y_FORCE_INLINE __m128i _mm_cmpgt_epi64(__m128i a, __m128i b) {
  737. return (__m128i)vec_cmpgt((vector signed long long)a, (vector signed long long)b);
  738. }
  739. Y_FORCE_INLINE __m128i _mm_cmplt_epi8(__m128i a, __m128i b) {
  740. return (__m128i)vec_cmplt((__v16qi)a, (__v16qi)b);
  741. }
  742. Y_FORCE_INLINE __m128i _mm_cmplt_epi16(__m128i a, __m128i b) {
  743. return (__m128i)vec_cmplt((__v8hi)a, (__v8hi)b);
  744. }
  745. Y_FORCE_INLINE __m128i _mm_cmplt_epi32(__m128i a, __m128i b) {
  746. return (__m128i)vec_cmplt((__v4si)a, (__v4si)b);
  747. }
  748. Y_FORCE_INLINE __m128i _mm_cmplt_epi64(__m128i a, __m128i b) {
  749. return (__m128i)vec_cmplt((vector signed long long)a, (vector signed long long)b);
  750. }
  751. Y_FORCE_INLINE __m128i _mm_sad_epu8(__m128i A, __m128i B) {
  752. __v16qu a, b;
  753. __v16qu vmin, vmax, vabsdiff;
  754. __v4si vsum;
  755. const __v4su zero = {0, 0, 0, 0};
  756. __v4si result;
  757. a = (__v16qu)A;
  758. b = (__v16qu)B;
  759. vmin = vec_min(a, b);
  760. vmax = vec_max(a, b);
  761. vabsdiff = vec_sub(vmax, vmin);
  762. /* Sum four groups of bytes into integers. */
  763. vsum = (__vector signed int)vec_sum4s(vabsdiff, zero);
  764. /* Sum across four integers with two integer results. */
  765. result = vec_sum2s(vsum, (__vector signed int)zero);
  766. /* Rotate the sums into the correct position. */
  767. #ifdef __LITTLE_ENDIAN__
  768. result = vec_sld(result, result, 4);
  769. #elif __BIG_ENDIAN__
  770. result = vec_sld(result, result, 6);
  771. #endif
  772. /* Rotate the sums into the correct position. */
  773. return (__m128i)result;
  774. }
  775. Y_FORCE_INLINE __m128i _mm_subs_epi8(__m128i a, __m128i b) {
  776. return (__m128i)vec_subs((__v16qi)a, (__v16qi)b);
  777. }
  778. Y_FORCE_INLINE __m128i _mm_subs_epi16(__m128i a, __m128i b) {
  779. return (__m128i)vec_subs((__v8hi)a, (__v8hi)b);
  780. }
  781. Y_FORCE_INLINE __m128i _mm_subs_epu8(__m128i a, __m128i b) {
  782. return (__m128i)vec_subs((__v16qu)a, (__v16qu)b);
  783. }
  784. Y_FORCE_INLINE __m128i _mm_subs_epu16(__m128i a, __m128i b) {
  785. return (__m128i)vec_subs((__v8hu)a, (__v8hu)b);
  786. }
  787. Y_FORCE_INLINE __m128i _mm_adds_epi8(__m128i a, __m128i b) {
  788. return (__m128i)vec_adds((__v16qi)a, (__v16qi)b);
  789. }
  790. Y_FORCE_INLINE __m128i _mm_adds_epi16(__m128i a, __m128i b) {
  791. return (__m128i)vec_adds((__v8hi)a, (__v8hi)b);
  792. }
  793. Y_FORCE_INLINE __m128i _mm_adds_epu8(__m128i a, __m128i b) {
  794. return (__m128i)vec_adds((__v16qu)a, (__v16qu)b);
  795. }
  796. Y_FORCE_INLINE __m128i _mm_adds_epu16(__m128i a, __m128i b) {
  797. return (__m128i)vec_adds((__v8hu)a, (__v8hu)b);
  798. }
  799. Y_FORCE_INLINE __m128d _mm_castsi128_pd(__m128i a) {
  800. return (__m128d)a;
  801. }
  802. Y_FORCE_INLINE void _mm_prefetch(const void *p, enum _mm_hint) {
  803. __builtin_prefetch(p);
  804. }
  805. Y_FORCE_INLINE __m128i _mm_hadd_epi16(__m128i a, __m128i b) {
  806. const __v16qu p = { 0, 1, 4, 5, 8, 9, 12, 13, 16, 17, 20, 21, 24, 25, 28, 29 };
  807. const __v16qu q = { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 };
  808. __v8hi c = vec_perm((__v8hi)a, (__v8hi)b, p);
  809. __v8hi d = vec_perm((__v8hi)a, (__v8hi)b, q);
  810. return (__m128i)vec_add(c, d);
  811. }