vsx_utils.hpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html
  4. #ifndef OPENCV_HAL_VSX_UTILS_HPP
  5. #define OPENCV_HAL_VSX_UTILS_HPP
  6. #include "opencv2/core/cvdef.h"
  7. #ifndef SKIP_INCLUDES
  8. # include <assert.h>
  9. #endif
  10. //! @addtogroup core_utils_vsx
  11. //! @{
  12. #if CV_VSX
  13. #define __VSX_S16__(c, v) (c){v, v, v, v, v, v, v, v, v, v, v, v, v, v, v, v}
  14. #define __VSX_S8__(c, v) (c){v, v, v, v, v, v, v, v}
  15. #define __VSX_S4__(c, v) (c){v, v, v, v}
  16. #define __VSX_S2__(c, v) (c){v, v}
  17. typedef __vector unsigned char vec_uchar16;
  18. #define vec_uchar16_set(...) (vec_uchar16){__VA_ARGS__}
  19. #define vec_uchar16_sp(c) (__VSX_S16__(vec_uchar16, (unsigned char)c))
  20. #define vec_uchar16_c(v) ((vec_uchar16)(v))
  21. #define vec_uchar16_z vec_uchar16_sp(0)
  22. typedef __vector signed char vec_char16;
  23. #define vec_char16_set(...) (vec_char16){__VA_ARGS__}
  24. #define vec_char16_sp(c) (__VSX_S16__(vec_char16, (signed char)c))
  25. #define vec_char16_c(v) ((vec_char16)(v))
  26. #define vec_char16_z vec_char16_sp(0)
  27. typedef __vector unsigned short vec_ushort8;
  28. #define vec_ushort8_set(...) (vec_ushort8){__VA_ARGS__}
  29. #define vec_ushort8_sp(c) (__VSX_S8__(vec_ushort8, (unsigned short)c))
  30. #define vec_ushort8_c(v) ((vec_ushort8)(v))
  31. #define vec_ushort8_z vec_ushort8_sp(0)
  32. typedef __vector signed short vec_short8;
  33. #define vec_short8_set(...) (vec_short8){__VA_ARGS__}
  34. #define vec_short8_sp(c) (__VSX_S8__(vec_short8, (signed short)c))
  35. #define vec_short8_c(v) ((vec_short8)(v))
  36. #define vec_short8_z vec_short8_sp(0)
  37. typedef __vector unsigned int vec_uint4;
  38. #define vec_uint4_set(...) (vec_uint4){__VA_ARGS__}
  39. #define vec_uint4_sp(c) (__VSX_S4__(vec_uint4, (unsigned int)c))
  40. #define vec_uint4_c(v) ((vec_uint4)(v))
  41. #define vec_uint4_z vec_uint4_sp(0)
  42. typedef __vector signed int vec_int4;
  43. #define vec_int4_set(...) (vec_int4){__VA_ARGS__}
  44. #define vec_int4_sp(c) (__VSX_S4__(vec_int4, (signed int)c))
  45. #define vec_int4_c(v) ((vec_int4)(v))
  46. #define vec_int4_z vec_int4_sp(0)
  47. typedef __vector float vec_float4;
  48. #define vec_float4_set(...) (vec_float4){__VA_ARGS__}
  49. #define vec_float4_sp(c) (__VSX_S4__(vec_float4, c))
  50. #define vec_float4_c(v) ((vec_float4)(v))
  51. #define vec_float4_z vec_float4_sp(0)
  52. typedef __vector unsigned long long vec_udword2;
  53. #define vec_udword2_set(...) (vec_udword2){__VA_ARGS__}
  54. #define vec_udword2_sp(c) (__VSX_S2__(vec_udword2, (unsigned long long)c))
  55. #define vec_udword2_c(v) ((vec_udword2)(v))
  56. #define vec_udword2_z vec_udword2_sp(0)
  57. typedef __vector signed long long vec_dword2;
  58. #define vec_dword2_set(...) (vec_dword2){__VA_ARGS__}
  59. #define vec_dword2_sp(c) (__VSX_S2__(vec_dword2, (signed long long)c))
  60. #define vec_dword2_c(v) ((vec_dword2)(v))
  61. #define vec_dword2_z vec_dword2_sp(0)
  62. typedef __vector double vec_double2;
  63. #define vec_double2_set(...) (vec_double2){__VA_ARGS__}
  64. #define vec_double2_c(v) ((vec_double2)(v))
  65. #define vec_double2_sp(c) (__VSX_S2__(vec_double2, c))
  66. #define vec_double2_z vec_double2_sp(0)
  67. #define vec_bchar16 __vector __bool char
  68. #define vec_bchar16_set(...) (vec_bchar16){__VA_ARGS__}
  69. #define vec_bchar16_c(v) ((vec_bchar16)(v))
  70. #define vec_bshort8 __vector __bool short
  71. #define vec_bshort8_set(...) (vec_bshort8){__VA_ARGS__}
  72. #define vec_bshort8_c(v) ((vec_bshort8)(v))
  73. #define vec_bint4 __vector __bool int
  74. #define vec_bint4_set(...) (vec_bint4){__VA_ARGS__}
  75. #define vec_bint4_c(v) ((vec_bint4)(v))
  76. #define vec_bdword2 __vector __bool long long
  77. #define vec_bdword2_set(...) (vec_bdword2){__VA_ARGS__}
  78. #define vec_bdword2_c(v) ((vec_bdword2)(v))
  79. #define VSX_FINLINE(tp) extern inline tp __attribute__((always_inline))
  80. #define VSX_REDIRECT_1RG(rt, rg, fnm, fn2) \
  81. VSX_FINLINE(rt) fnm(const rg& a) { return fn2(a); }
  82. #define VSX_REDIRECT_2RG(rt, rg, fnm, fn2) \
  83. VSX_FINLINE(rt) fnm(const rg& a, const rg& b) { return fn2(a, b); }
  84. /*
  85. * GCC VSX compatibility
  86. **/
  87. #if defined(__GNUG__) && !defined(__clang__)
  88. // inline asm helper
  89. #define VSX_IMPL_1RG(rt, rg, opc, fnm) \
  90. VSX_FINLINE(rt) fnm(const rg& a) \
  91. { rt rs; __asm__ __volatile__(#opc" %x0,%x1" : "=wa" (rs) : "wa" (a)); return rs; }
  92. #define VSX_IMPL_1VRG(rt, rg, opc, fnm) \
  93. VSX_FINLINE(rt) fnm(const rg& a) \
  94. { rt rs; __asm__ __volatile__(#opc" %0,%1" : "=v" (rs) : "v" (a)); return rs; }
  95. #define VSX_IMPL_2VRG_F(rt, rg, fopc, fnm) \
  96. VSX_FINLINE(rt) fnm(const rg& a, const rg& b) \
  97. { rt rs; __asm__ __volatile__(fopc : "=v" (rs) : "v" (a), "v" (b)); return rs; }
  98. #define VSX_IMPL_2VRG(rt, rg, opc, fnm) VSX_IMPL_2VRG_F(rt, rg, #opc" %0,%1,%2", fnm)
  99. #if __GNUG__ < 8
  100. // Support for int4 -> dword2 expanding multiply was added in GCC 8.
  101. #ifdef vec_mule
  102. #undef vec_mule
  103. #endif
  104. #ifdef vec_mulo
  105. #undef vec_mulo
  106. #endif
  107. VSX_REDIRECT_2RG(vec_ushort8, vec_uchar16, vec_mule, __builtin_vec_mule)
  108. VSX_REDIRECT_2RG(vec_short8, vec_char16, vec_mule, __builtin_vec_mule)
  109. VSX_REDIRECT_2RG(vec_int4, vec_short8, vec_mule, __builtin_vec_mule)
  110. VSX_REDIRECT_2RG(vec_uint4, vec_ushort8, vec_mule, __builtin_vec_mule)
  111. VSX_REDIRECT_2RG(vec_ushort8, vec_uchar16, vec_mulo, __builtin_vec_mulo)
  112. VSX_REDIRECT_2RG(vec_short8, vec_char16, vec_mulo, __builtin_vec_mulo)
  113. VSX_REDIRECT_2RG(vec_int4, vec_short8, vec_mulo, __builtin_vec_mulo)
  114. VSX_REDIRECT_2RG(vec_uint4, vec_ushort8, vec_mulo, __builtin_vec_mulo)
  115. // dword2 support arrived in ISA 2.07 and GCC 8+
  116. VSX_IMPL_2VRG(vec_dword2, vec_int4, vmulosw, vec_mule)
  117. VSX_IMPL_2VRG(vec_udword2, vec_uint4, vmulouw, vec_mule)
  118. VSX_IMPL_2VRG(vec_dword2, vec_int4, vmulesw, vec_mulo)
  119. VSX_IMPL_2VRG(vec_udword2, vec_uint4, vmuleuw, vec_mulo)
  120. #endif
  121. #if __GNUG__ < 7
  122. // up to GCC 6 vec_mul only supports precisions and llong
  123. # ifdef vec_mul
  124. # undef vec_mul
  125. # endif
  126. /*
  127. * there's no a direct instruction for supporting 8-bit, 16-bit multiplication in ISA 2.07,
  128. * XLC Implement it by using instruction "multiply even", "multiply odd" and "permute"
  129. **/
  130. # define VSX_IMPL_MULH(Tvec, cperm) \
  131. VSX_FINLINE(Tvec) vec_mul(const Tvec& a, const Tvec& b) \
  132. { \
  133. static const vec_uchar16 ev_od = {cperm}; \
  134. return vec_perm((Tvec)vec_mule(a, b), (Tvec)vec_mulo(a, b), ev_od); \
  135. }
  136. #define VSX_IMPL_MULH_P16 0, 16, 2, 18, 4, 20, 6, 22, 8, 24, 10, 26, 12, 28, 14, 30
  137. VSX_IMPL_MULH(vec_char16, VSX_IMPL_MULH_P16)
  138. VSX_IMPL_MULH(vec_uchar16, VSX_IMPL_MULH_P16)
  139. #define VSX_IMPL_MULH_P8 0, 1, 16, 17, 4, 5, 20, 21, 8, 9, 24, 25, 12, 13, 28, 29
  140. VSX_IMPL_MULH(vec_short8, VSX_IMPL_MULH_P8)
  141. VSX_IMPL_MULH(vec_ushort8, VSX_IMPL_MULH_P8)
  142. // vmuluwm can be used for unsigned or signed integers, that's what they said
  143. VSX_IMPL_2VRG(vec_int4, vec_int4, vmuluwm, vec_mul)
  144. VSX_IMPL_2VRG(vec_uint4, vec_uint4, vmuluwm, vec_mul)
  145. // redirect to GCC builtin vec_mul, since it already supports precisions and llong
  146. VSX_REDIRECT_2RG(vec_float4, vec_float4, vec_mul, __builtin_vec_mul)
  147. VSX_REDIRECT_2RG(vec_double2, vec_double2, vec_mul, __builtin_vec_mul)
  148. VSX_REDIRECT_2RG(vec_dword2, vec_dword2, vec_mul, __builtin_vec_mul)
  149. VSX_REDIRECT_2RG(vec_udword2, vec_udword2, vec_mul, __builtin_vec_mul)
  150. #endif // __GNUG__ < 7
  151. #if __GNUG__ < 6
  152. /*
  153. * Instruction "compare greater than or equal" in ISA 2.07 only supports single
  154. * and double precision.
  155. * In XLC and new versions of GCC implement integers by using instruction "greater than" and NOR.
  156. **/
  157. # ifdef vec_cmpge
  158. # undef vec_cmpge
  159. # endif
  160. # ifdef vec_cmple
  161. # undef vec_cmple
  162. # endif
  163. # define vec_cmple(a, b) vec_cmpge(b, a)
  164. # define VSX_IMPL_CMPGE(rt, rg, opc, fnm) \
  165. VSX_IMPL_2VRG_F(rt, rg, #opc" %0,%2,%1\n\t xxlnor %x0,%x0,%x0", fnm)
  166. VSX_IMPL_CMPGE(vec_bchar16, vec_char16, vcmpgtsb, vec_cmpge)
  167. VSX_IMPL_CMPGE(vec_bchar16, vec_uchar16, vcmpgtub, vec_cmpge)
  168. VSX_IMPL_CMPGE(vec_bshort8, vec_short8, vcmpgtsh, vec_cmpge)
  169. VSX_IMPL_CMPGE(vec_bshort8, vec_ushort8, vcmpgtuh, vec_cmpge)
  170. VSX_IMPL_CMPGE(vec_bint4, vec_int4, vcmpgtsw, vec_cmpge)
  171. VSX_IMPL_CMPGE(vec_bint4, vec_uint4, vcmpgtuw, vec_cmpge)
  172. VSX_IMPL_CMPGE(vec_bdword2, vec_dword2, vcmpgtsd, vec_cmpge)
  173. VSX_IMPL_CMPGE(vec_bdword2, vec_udword2, vcmpgtud, vec_cmpge)
  174. // redirect to GCC builtin cmpge, since it already supports precisions
  175. VSX_REDIRECT_2RG(vec_bint4, vec_float4, vec_cmpge, __builtin_vec_cmpge)
  176. VSX_REDIRECT_2RG(vec_bdword2, vec_double2, vec_cmpge, __builtin_vec_cmpge)
  177. // up to gcc5 vec_nor doesn't support bool long long
  178. # undef vec_nor
  179. template<typename T>
  180. VSX_REDIRECT_2RG(T, T, vec_nor, __builtin_vec_nor)
  181. VSX_FINLINE(vec_bdword2) vec_nor(const vec_bdword2& a, const vec_bdword2& b)
  182. { return vec_bdword2_c(__builtin_vec_nor(vec_dword2_c(a), vec_dword2_c(b))); }
  183. // vec_packs doesn't support double words in gcc4 and old versions of gcc5
  184. # undef vec_packs
  185. VSX_REDIRECT_2RG(vec_char16, vec_short8, vec_packs, __builtin_vec_packs)
  186. VSX_REDIRECT_2RG(vec_uchar16, vec_ushort8, vec_packs, __builtin_vec_packs)
  187. VSX_REDIRECT_2RG(vec_short8, vec_int4, vec_packs, __builtin_vec_packs)
  188. VSX_REDIRECT_2RG(vec_ushort8, vec_uint4, vec_packs, __builtin_vec_packs)
  189. VSX_IMPL_2VRG_F(vec_int4, vec_dword2, "vpksdss %0,%2,%1", vec_packs)
  190. VSX_IMPL_2VRG_F(vec_uint4, vec_udword2, "vpkudus %0,%2,%1", vec_packs)
  191. #endif // __GNUG__ < 6
  192. #if __GNUG__ < 5
  193. // vec_xxpermdi in gcc4 missing little-endian supports just like clang
  194. # define vec_permi(a, b, c) vec_xxpermdi(b, a, (3 ^ (((c) & 1) << 1 | (c) >> 1)))
  195. // same as vec_xxpermdi
  196. # undef vec_vbpermq
  197. VSX_IMPL_2VRG(vec_udword2, vec_uchar16, vbpermq, vec_vbpermq)
  198. VSX_IMPL_2VRG(vec_dword2, vec_char16, vbpermq, vec_vbpermq)
  199. #else
  200. # define vec_permi vec_xxpermdi
  201. #endif // __GNUG__ < 5
  202. // shift left double by word immediate
  203. #ifndef vec_sldw
  204. # define vec_sldw __builtin_vsx_xxsldwi
  205. #endif
  206. // vector population count
  207. VSX_IMPL_1VRG(vec_uchar16, vec_uchar16, vpopcntb, vec_popcntu)
  208. VSX_IMPL_1VRG(vec_uchar16, vec_char16, vpopcntb, vec_popcntu)
  209. VSX_IMPL_1VRG(vec_ushort8, vec_ushort8, vpopcnth, vec_popcntu)
  210. VSX_IMPL_1VRG(vec_ushort8, vec_short8, vpopcnth, vec_popcntu)
  211. VSX_IMPL_1VRG(vec_uint4, vec_uint4, vpopcntw, vec_popcntu)
  212. VSX_IMPL_1VRG(vec_uint4, vec_int4, vpopcntw, vec_popcntu)
  213. VSX_IMPL_1VRG(vec_udword2, vec_udword2, vpopcntd, vec_popcntu)
  214. VSX_IMPL_1VRG(vec_udword2, vec_dword2, vpopcntd, vec_popcntu)
  215. // converts between single and double-precision
  216. VSX_REDIRECT_1RG(vec_float4, vec_double2, vec_cvfo, __builtin_vsx_xvcvdpsp)
  217. VSX_REDIRECT_1RG(vec_double2, vec_float4, vec_cvfo, __builtin_vsx_xvcvspdp)
  218. // converts word and doubleword to double-precision
  219. #undef vec_ctd
  220. VSX_IMPL_1RG(vec_double2, vec_int4, xvcvsxwdp, vec_ctdo)
  221. VSX_IMPL_1RG(vec_double2, vec_uint4, xvcvuxwdp, vec_ctdo)
  222. VSX_IMPL_1RG(vec_double2, vec_dword2, xvcvsxddp, vec_ctd)
  223. VSX_IMPL_1RG(vec_double2, vec_udword2, xvcvuxddp, vec_ctd)
  224. // converts word and doubleword to single-precision
  225. #undef vec_ctf
  226. VSX_IMPL_1RG(vec_float4, vec_int4, xvcvsxwsp, vec_ctf)
  227. VSX_IMPL_1RG(vec_float4, vec_uint4, xvcvuxwsp, vec_ctf)
  228. VSX_IMPL_1RG(vec_float4, vec_dword2, xvcvsxdsp, vec_ctfo)
  229. VSX_IMPL_1RG(vec_float4, vec_udword2, xvcvuxdsp, vec_ctfo)
  230. // converts single and double precision to signed word
  231. #undef vec_cts
  232. VSX_IMPL_1RG(vec_int4, vec_double2, xvcvdpsxws, vec_ctso)
  233. VSX_IMPL_1RG(vec_int4, vec_float4, xvcvspsxws, vec_cts)
  234. // converts single and double precision to unsigned word
  235. #undef vec_ctu
  236. VSX_IMPL_1RG(vec_uint4, vec_double2, xvcvdpuxws, vec_ctuo)
  237. VSX_IMPL_1RG(vec_uint4, vec_float4, xvcvspuxws, vec_ctu)
  238. // converts single and double precision to signed doubleword
  239. #undef vec_ctsl
  240. VSX_IMPL_1RG(vec_dword2, vec_double2, xvcvdpsxds, vec_ctsl)
  241. VSX_IMPL_1RG(vec_dword2, vec_float4, xvcvspsxds, vec_ctslo)
  242. // converts single and double precision to unsigned doubleword
  243. #undef vec_ctul
  244. VSX_IMPL_1RG(vec_udword2, vec_double2, xvcvdpuxds, vec_ctul)
  245. VSX_IMPL_1RG(vec_udword2, vec_float4, xvcvspuxds, vec_ctulo)
  246. // just in case if GCC doesn't define it
  247. #ifndef vec_xl
  248. # define vec_xl vec_vsx_ld
  249. # define vec_xst vec_vsx_st
  250. #endif
  251. #endif // GCC VSX compatibility
  252. /*
  253. * CLANG VSX compatibility
  254. **/
  255. #if defined(__clang__) && !defined(__IBMCPP__)
  256. /*
  257. * CLANG doesn't support %x<n> in the inline asm template which fixes register number
  258. * when using any of the register constraints wa, wd, wf
  259. *
  260. * For more explanation checkout PowerPC and IBM RS6000 in https://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html
  261. * Also there's already an open bug https://bugs.llvm.org/show_bug.cgi?id=31837
  262. *
  263. * So we're not able to use inline asm and only use built-in functions that CLANG supports
  264. * and use __builtin_convertvector if clang missing any of vector conversions built-in functions
  265. *
  266. * todo: clang asm template bug is fixed, need to reconsider the current workarounds.
  267. */
  268. // convert vector helper
  269. #define VSX_IMPL_CONVERT(rt, rg, fnm) \
  270. VSX_FINLINE(rt) fnm(const rg& a) { return __builtin_convertvector(a, rt); }
  271. #if __clang_major__ < 5
  272. // implement vec_permi in a dirty way
  273. # define VSX_IMPL_CLANG_4_PERMI(Tvec) \
  274. VSX_FINLINE(Tvec) vec_permi(const Tvec& a, const Tvec& b, unsigned const char c) \
  275. { \
  276. switch (c) \
  277. { \
  278. case 0: \
  279. return vec_mergeh(a, b); \
  280. case 1: \
  281. return vec_mergel(vec_mergeh(a, a), b); \
  282. case 2: \
  283. return vec_mergeh(vec_mergel(a, a), b); \
  284. default: \
  285. return vec_mergel(a, b); \
  286. } \
  287. }
  288. VSX_IMPL_CLANG_4_PERMI(vec_udword2)
  289. VSX_IMPL_CLANG_4_PERMI(vec_dword2)
  290. VSX_IMPL_CLANG_4_PERMI(vec_double2)
  291. // vec_xxsldwi is missing in clang 4
  292. # define vec_xxsldwi(a, b, c) vec_sld(a, b, (c) * 4)
  293. #else
  294. // vec_xxpermdi is missing little-endian supports in clang 4 just like gcc4
  295. # define vec_permi(a, b, c) vec_xxpermdi(b, a, (3 ^ (((c) & 1) << 1 | (c) >> 1)))
  296. #endif // __clang_major__ < 5
  297. // shift left double by word immediate
  298. #ifndef vec_sldw
  299. # define vec_sldw vec_xxsldwi
  300. #endif
  301. // Implement vec_rsqrt since clang only supports vec_rsqrte
  302. #ifndef vec_rsqrt
  303. VSX_FINLINE(vec_float4) vec_rsqrt(const vec_float4& a)
  304. { return vec_div(vec_float4_sp(1), vec_sqrt(a)); }
  305. VSX_FINLINE(vec_double2) vec_rsqrt(const vec_double2& a)
  306. { return vec_div(vec_double2_sp(1), vec_sqrt(a)); }
  307. #endif
  308. // vec_promote missing support for doubleword
  309. VSX_FINLINE(vec_dword2) vec_promote(long long a, int b)
  310. {
  311. vec_dword2 ret = vec_dword2_z;
  312. ret[b & 1] = a;
  313. return ret;
  314. }
  315. VSX_FINLINE(vec_udword2) vec_promote(unsigned long long a, int b)
  316. {
  317. vec_udword2 ret = vec_udword2_z;
  318. ret[b & 1] = a;
  319. return ret;
  320. }
  321. // vec_popcnt should return unsigned but clang has different thought just like gcc in vec_vpopcnt
  322. #define VSX_IMPL_POPCNTU(Tvec, Tvec2, ucast) \
  323. VSX_FINLINE(Tvec) vec_popcntu(const Tvec2& a) \
  324. { return ucast(vec_popcnt(a)); }
  325. VSX_IMPL_POPCNTU(vec_uchar16, vec_char16, vec_uchar16_c);
  326. VSX_IMPL_POPCNTU(vec_ushort8, vec_short8, vec_ushort8_c);
  327. VSX_IMPL_POPCNTU(vec_uint4, vec_int4, vec_uint4_c);
  328. VSX_IMPL_POPCNTU(vec_udword2, vec_dword2, vec_udword2_c);
  329. // redirect unsigned types
  330. VSX_REDIRECT_1RG(vec_uchar16, vec_uchar16, vec_popcntu, vec_popcnt)
  331. VSX_REDIRECT_1RG(vec_ushort8, vec_ushort8, vec_popcntu, vec_popcnt)
  332. VSX_REDIRECT_1RG(vec_uint4, vec_uint4, vec_popcntu, vec_popcnt)
  333. VSX_REDIRECT_1RG(vec_udword2, vec_udword2, vec_popcntu, vec_popcnt)
  334. // converts between single and double precision
  335. VSX_REDIRECT_1RG(vec_float4, vec_double2, vec_cvfo, __builtin_vsx_xvcvdpsp)
  336. VSX_REDIRECT_1RG(vec_double2, vec_float4, vec_cvfo, __builtin_vsx_xvcvspdp)
  337. // converts word and doubleword to double-precision
  338. #ifdef vec_ctd
  339. # undef vec_ctd
  340. #endif
  341. VSX_REDIRECT_1RG(vec_double2, vec_int4, vec_ctdo, __builtin_vsx_xvcvsxwdp)
  342. VSX_REDIRECT_1RG(vec_double2, vec_uint4, vec_ctdo, __builtin_vsx_xvcvuxwdp)
  343. VSX_IMPL_CONVERT(vec_double2, vec_dword2, vec_ctd)
  344. VSX_IMPL_CONVERT(vec_double2, vec_udword2, vec_ctd)
  345. // converts word and doubleword to single-precision
  346. #if __clang_major__ > 4
  347. # undef vec_ctf
  348. #endif
  349. VSX_IMPL_CONVERT(vec_float4, vec_int4, vec_ctf)
  350. VSX_IMPL_CONVERT(vec_float4, vec_uint4, vec_ctf)
  351. VSX_REDIRECT_1RG(vec_float4, vec_dword2, vec_ctfo, __builtin_vsx_xvcvsxdsp)
  352. VSX_REDIRECT_1RG(vec_float4, vec_udword2, vec_ctfo, __builtin_vsx_xvcvuxdsp)
  353. // converts single and double precision to signed word
  354. #if __clang_major__ > 4
  355. # undef vec_cts
  356. #endif
  357. VSX_REDIRECT_1RG(vec_int4, vec_double2, vec_ctso, __builtin_vsx_xvcvdpsxws)
  358. VSX_IMPL_CONVERT(vec_int4, vec_float4, vec_cts)
  359. // converts single and double precision to unsigned word
  360. #if __clang_major__ > 4
  361. # undef vec_ctu
  362. #endif
  363. VSX_REDIRECT_1RG(vec_uint4, vec_double2, vec_ctuo, __builtin_vsx_xvcvdpuxws)
  364. VSX_IMPL_CONVERT(vec_uint4, vec_float4, vec_ctu)
  365. // converts single and double precision to signed doubleword
  366. #ifdef vec_ctsl
  367. # undef vec_ctsl
  368. #endif
  369. VSX_IMPL_CONVERT(vec_dword2, vec_double2, vec_ctsl)
  370. // __builtin_convertvector unable to convert, xvcvspsxds is missing on it
  371. VSX_FINLINE(vec_dword2) vec_ctslo(const vec_float4& a)
  372. { return vec_ctsl(vec_cvfo(a)); }
  373. // converts single and double precision to unsigned doubleword
  374. #ifdef vec_ctul
  375. # undef vec_ctul
  376. #endif
  377. VSX_IMPL_CONVERT(vec_udword2, vec_double2, vec_ctul)
  378. // __builtin_convertvector unable to convert, xvcvspuxds is missing on it
  379. VSX_FINLINE(vec_udword2) vec_ctulo(const vec_float4& a)
  380. { return vec_ctul(vec_cvfo(a)); }
  381. #endif // CLANG VSX compatibility
  382. /*
  383. * Common GCC, CLANG compatibility
  384. **/
  385. #if defined(__GNUG__) && !defined(__IBMCPP__)
  386. #ifdef vec_cvf
  387. # undef vec_cvf
  388. #endif
  389. #define VSX_IMPL_CONV_EVEN_4_2(rt, rg, fnm, fn2) \
  390. VSX_FINLINE(rt) fnm(const rg& a) \
  391. { return fn2(vec_sldw(a, a, 1)); }
  392. VSX_IMPL_CONV_EVEN_4_2(vec_double2, vec_float4, vec_cvf, vec_cvfo)
  393. VSX_IMPL_CONV_EVEN_4_2(vec_double2, vec_int4, vec_ctd, vec_ctdo)
  394. VSX_IMPL_CONV_EVEN_4_2(vec_double2, vec_uint4, vec_ctd, vec_ctdo)
  395. VSX_IMPL_CONV_EVEN_4_2(vec_dword2, vec_float4, vec_ctsl, vec_ctslo)
  396. VSX_IMPL_CONV_EVEN_4_2(vec_udword2, vec_float4, vec_ctul, vec_ctulo)
  397. #define VSX_IMPL_CONV_EVEN_2_4(rt, rg, fnm, fn2) \
  398. VSX_FINLINE(rt) fnm(const rg& a) \
  399. { \
  400. rt v4 = fn2(a); \
  401. return vec_sldw(v4, v4, 3); \
  402. }
  403. VSX_IMPL_CONV_EVEN_2_4(vec_float4, vec_double2, vec_cvf, vec_cvfo)
  404. VSX_IMPL_CONV_EVEN_2_4(vec_float4, vec_dword2, vec_ctf, vec_ctfo)
  405. VSX_IMPL_CONV_EVEN_2_4(vec_float4, vec_udword2, vec_ctf, vec_ctfo)
  406. VSX_IMPL_CONV_EVEN_2_4(vec_int4, vec_double2, vec_cts, vec_ctso)
  407. VSX_IMPL_CONV_EVEN_2_4(vec_uint4, vec_double2, vec_ctu, vec_ctuo)
  408. // Only for Eigen!
  409. /*
  410. * changing behavior of conversion intrinsics for gcc has effect on Eigen
  411. * so we redefine old behavior again only on gcc, clang
  412. */
  413. #if !defined(__clang__) || __clang_major__ > 4
  414. // ignoring second arg since Eigen only truncates toward zero
  415. # define VSX_IMPL_CONV_2VARIANT(rt, rg, fnm, fn2) \
  416. VSX_FINLINE(rt) fnm(const rg& a, int only_truncate) \
  417. { \
  418. assert(only_truncate == 0); \
  419. CV_UNUSED(only_truncate); \
  420. return fn2(a); \
  421. }
  422. VSX_IMPL_CONV_2VARIANT(vec_int4, vec_float4, vec_cts, vec_cts)
  423. VSX_IMPL_CONV_2VARIANT(vec_uint4, vec_float4, vec_ctu, vec_ctu)
  424. VSX_IMPL_CONV_2VARIANT(vec_float4, vec_int4, vec_ctf, vec_ctf)
  425. VSX_IMPL_CONV_2VARIANT(vec_float4, vec_uint4, vec_ctf, vec_ctf)
  426. // define vec_cts for converting double precision to signed doubleword
  427. // which isn't compatible with xlc but its okay since Eigen only uses it for gcc
  428. VSX_IMPL_CONV_2VARIANT(vec_dword2, vec_double2, vec_cts, vec_ctsl)
  429. #endif // Eigen
  430. #endif // Common GCC, CLANG compatibility
  431. /*
  432. * XLC VSX compatibility
  433. **/
  434. #if defined(__IBMCPP__)
  435. // vector population count
  436. #define vec_popcntu vec_popcnt
  437. // overload and redirect with setting second arg to zero
  438. // since we only support conversions without the second arg
  439. #define VSX_IMPL_OVERLOAD_Z2(rt, rg, fnm) \
  440. VSX_FINLINE(rt) fnm(const rg& a) { return fnm(a, 0); }
  441. VSX_IMPL_OVERLOAD_Z2(vec_double2, vec_int4, vec_ctd)
  442. VSX_IMPL_OVERLOAD_Z2(vec_double2, vec_uint4, vec_ctd)
  443. VSX_IMPL_OVERLOAD_Z2(vec_double2, vec_dword2, vec_ctd)
  444. VSX_IMPL_OVERLOAD_Z2(vec_double2, vec_udword2, vec_ctd)
  445. VSX_IMPL_OVERLOAD_Z2(vec_float4, vec_int4, vec_ctf)
  446. VSX_IMPL_OVERLOAD_Z2(vec_float4, vec_uint4, vec_ctf)
  447. VSX_IMPL_OVERLOAD_Z2(vec_float4, vec_dword2, vec_ctf)
  448. VSX_IMPL_OVERLOAD_Z2(vec_float4, vec_udword2, vec_ctf)
  449. VSX_IMPL_OVERLOAD_Z2(vec_int4, vec_double2, vec_cts)
  450. VSX_IMPL_OVERLOAD_Z2(vec_int4, vec_float4, vec_cts)
  451. VSX_IMPL_OVERLOAD_Z2(vec_uint4, vec_double2, vec_ctu)
  452. VSX_IMPL_OVERLOAD_Z2(vec_uint4, vec_float4, vec_ctu)
  453. VSX_IMPL_OVERLOAD_Z2(vec_dword2, vec_double2, vec_ctsl)
  454. VSX_IMPL_OVERLOAD_Z2(vec_dword2, vec_float4, vec_ctsl)
  455. VSX_IMPL_OVERLOAD_Z2(vec_udword2, vec_double2, vec_ctul)
  456. VSX_IMPL_OVERLOAD_Z2(vec_udword2, vec_float4, vec_ctul)
  457. // fixme: implement conversions of odd-numbered elements in a dirty way
  458. // since xlc doesn't support VSX registers operand in inline asm.
  459. #define VSX_IMPL_CONV_ODD_4_2(rt, rg, fnm, fn2) \
  460. VSX_FINLINE(rt) fnm(const rg& a) { return fn2(vec_sldw(a, a, 3)); }
  461. VSX_IMPL_CONV_ODD_4_2(vec_double2, vec_float4, vec_cvfo, vec_cvf)
  462. VSX_IMPL_CONV_ODD_4_2(vec_double2, vec_int4, vec_ctdo, vec_ctd)
  463. VSX_IMPL_CONV_ODD_4_2(vec_double2, vec_uint4, vec_ctdo, vec_ctd)
  464. VSX_IMPL_CONV_ODD_4_2(vec_dword2, vec_float4, vec_ctslo, vec_ctsl)
  465. VSX_IMPL_CONV_ODD_4_2(vec_udword2, vec_float4, vec_ctulo, vec_ctul)
  466. #define VSX_IMPL_CONV_ODD_2_4(rt, rg, fnm, fn2) \
  467. VSX_FINLINE(rt) fnm(const rg& a) \
  468. { \
  469. rt v4 = fn2(a); \
  470. return vec_sldw(v4, v4, 1); \
  471. }
  472. VSX_IMPL_CONV_ODD_2_4(vec_float4, vec_double2, vec_cvfo, vec_cvf)
  473. VSX_IMPL_CONV_ODD_2_4(vec_float4, vec_dword2, vec_ctfo, vec_ctf)
  474. VSX_IMPL_CONV_ODD_2_4(vec_float4, vec_udword2, vec_ctfo, vec_ctf)
  475. VSX_IMPL_CONV_ODD_2_4(vec_int4, vec_double2, vec_ctso, vec_cts)
  476. VSX_IMPL_CONV_ODD_2_4(vec_uint4, vec_double2, vec_ctuo, vec_ctu)
  477. #endif // XLC VSX compatibility
  478. // ignore GCC warning that caused by -Wunused-but-set-variable in rare cases
  479. #if defined(__GNUG__) && !defined(__clang__)
  480. # define VSX_UNUSED(Tvec) Tvec __attribute__((__unused__))
  481. #else // CLANG, XLC
  482. # define VSX_UNUSED(Tvec) Tvec
  483. #endif
  484. // gcc can find his way in casting log int and XLC, CLANG ambiguous
  485. #if defined(__clang__) || defined(__IBMCPP__)
  486. VSX_FINLINE(vec_udword2) vec_splats(uint64 v)
  487. { return vec_splats((unsigned long long) v); }
  488. VSX_FINLINE(vec_dword2) vec_splats(int64 v)
  489. { return vec_splats((long long) v); }
  490. VSX_FINLINE(vec_udword2) vec_promote(uint64 a, int b)
  491. { return vec_promote((unsigned long long) a, b); }
  492. VSX_FINLINE(vec_dword2) vec_promote(int64 a, int b)
  493. { return vec_promote((long long) a, b); }
  494. #endif
  495. /*
  496. * implement vsx_ld(offset, pointer), vsx_st(vector, offset, pointer)
  497. * load and set using offset depend on the pointer type
  498. *
  499. * implement vsx_ldf(offset, pointer), vsx_stf(vector, offset, pointer)
  500. * load and set using offset depend on fixed bytes size
  501. *
  502. * Note: In clang vec_xl and vec_xst fails to load unaligned addresses
  503. * so we are using vec_vsx_ld, vec_vsx_st instead
  504. */
  505. #if defined(__clang__) && !defined(__IBMCPP__)
  506. # define vsx_ldf vec_vsx_ld
  507. # define vsx_stf vec_vsx_st
  508. #else // GCC , XLC
  509. # define vsx_ldf vec_xl
  510. # define vsx_stf vec_xst
  511. #endif
  512. #define VSX_OFFSET(o, p) ((o) * sizeof(*(p)))
  513. #define vsx_ld(o, p) vsx_ldf(VSX_OFFSET(o, p), p)
  514. #define vsx_st(v, o, p) vsx_stf(v, VSX_OFFSET(o, p), p)
  515. /*
  516. * implement vsx_ld2(offset, pointer), vsx_st2(vector, offset, pointer) to load and store double words
  517. * In GCC vec_xl and vec_xst it maps to vec_vsx_ld, vec_vsx_st which doesn't support long long
  518. * and in CLANG we are using vec_vsx_ld, vec_vsx_st because vec_xl, vec_xst fails to load unaligned addresses
  519. *
  520. * In XLC vec_xl and vec_xst fail to cast int64(long int) to long long
  521. */
  522. #if (defined(__GNUG__) || defined(__clang__)) && !defined(__IBMCPP__)
  523. VSX_FINLINE(vec_udword2) vsx_ld2(long o, const uint64* p)
  524. { return vec_udword2_c(vsx_ldf(VSX_OFFSET(o, p), (unsigned int*)p)); }
  525. VSX_FINLINE(vec_dword2) vsx_ld2(long o, const int64* p)
  526. { return vec_dword2_c(vsx_ldf(VSX_OFFSET(o, p), (int*)p)); }
  527. VSX_FINLINE(void) vsx_st2(const vec_udword2& vec, long o, uint64* p)
  528. { vsx_stf(vec_uint4_c(vec), VSX_OFFSET(o, p), (unsigned int*)p); }
  529. VSX_FINLINE(void) vsx_st2(const vec_dword2& vec, long o, int64* p)
  530. { vsx_stf(vec_int4_c(vec), VSX_OFFSET(o, p), (int*)p); }
  531. #else // XLC
  532. VSX_FINLINE(vec_udword2) vsx_ld2(long o, const uint64* p)
  533. { return vsx_ldf(VSX_OFFSET(o, p), (unsigned long long*)p); }
  534. VSX_FINLINE(vec_dword2) vsx_ld2(long o, const int64* p)
  535. { return vsx_ldf(VSX_OFFSET(o, p), (long long*)p); }
  536. VSX_FINLINE(void) vsx_st2(const vec_udword2& vec, long o, uint64* p)
  537. { vsx_stf(vec, VSX_OFFSET(o, p), (unsigned long long*)p); }
  538. VSX_FINLINE(void) vsx_st2(const vec_dword2& vec, long o, int64* p)
  539. { vsx_stf(vec, VSX_OFFSET(o, p), (long long*)p); }
  540. #endif
  541. // Store lower 8 byte
  542. #define vec_st_l8(v, p) *((uint64*)(p)) = vec_extract(vec_udword2_c(v), 0)
  543. // Store higher 8 byte
  544. #define vec_st_h8(v, p) *((uint64*)(p)) = vec_extract(vec_udword2_c(v), 1)
  545. // Load 64-bits of integer data to lower part
  546. #define VSX_IMPL_LOAD_L8(Tvec, Tp) \
  547. VSX_FINLINE(Tvec) vec_ld_l8(const Tp *p) \
  548. { return ((Tvec)vec_promote(*((uint64*)p), 0)); }
  549. VSX_IMPL_LOAD_L8(vec_uchar16, uchar)
  550. VSX_IMPL_LOAD_L8(vec_char16, schar)
  551. VSX_IMPL_LOAD_L8(vec_ushort8, ushort)
  552. VSX_IMPL_LOAD_L8(vec_short8, short)
  553. VSX_IMPL_LOAD_L8(vec_uint4, uint)
  554. VSX_IMPL_LOAD_L8(vec_int4, int)
  555. VSX_IMPL_LOAD_L8(vec_float4, float)
  556. VSX_IMPL_LOAD_L8(vec_udword2, uint64)
  557. VSX_IMPL_LOAD_L8(vec_dword2, int64)
  558. VSX_IMPL_LOAD_L8(vec_double2, double)
  559. // logical not
  560. #define vec_not(a) vec_nor(a, a)
  561. // power9 yaya
  562. // not equal
  563. #ifndef vec_cmpne
  564. # define vec_cmpne(a, b) vec_not(vec_cmpeq(a, b))
  565. #endif
  566. // absolute difference
  567. #ifndef vec_absd
  568. # define vec_absd(a, b) vec_sub(vec_max(a, b), vec_min(a, b))
  569. #endif
  570. /*
  571. * Implement vec_unpacklu and vec_unpackhu
  572. * since vec_unpackl, vec_unpackh only support signed integers
  573. **/
  574. #define VSX_IMPL_UNPACKU(rt, rg, zero) \
  575. VSX_FINLINE(rt) vec_unpacklu(const rg& a) \
  576. { return (rt)(vec_mergel(a, zero)); } \
  577. VSX_FINLINE(rt) vec_unpackhu(const rg& a) \
  578. { return (rt)(vec_mergeh(a, zero)); }
  579. VSX_IMPL_UNPACKU(vec_ushort8, vec_uchar16, vec_uchar16_z)
  580. VSX_IMPL_UNPACKU(vec_uint4, vec_ushort8, vec_ushort8_z)
  581. VSX_IMPL_UNPACKU(vec_udword2, vec_uint4, vec_uint4_z)
  582. /*
  583. * Implement vec_mergesqe and vec_mergesqo
  584. * Merges the sequence values of even and odd elements of two vectors
  585. */
  586. #define VSX_IMPL_PERM(rt, fnm, ...) \
  587. VSX_FINLINE(rt) fnm(const rt& a, const rt& b) \
  588. { static const vec_uchar16 perm = {__VA_ARGS__}; return vec_perm(a, b, perm); }
  589. // 16
  590. #define perm16_mergesqe 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30
  591. #define perm16_mergesqo 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31
  592. VSX_IMPL_PERM(vec_uchar16, vec_mergesqe, perm16_mergesqe)
  593. VSX_IMPL_PERM(vec_uchar16, vec_mergesqo, perm16_mergesqo)
  594. VSX_IMPL_PERM(vec_char16, vec_mergesqe, perm16_mergesqe)
  595. VSX_IMPL_PERM(vec_char16, vec_mergesqo, perm16_mergesqo)
  596. // 8
  597. #define perm8_mergesqe 0, 1, 4, 5, 8, 9, 12, 13, 16, 17, 20, 21, 24, 25, 28, 29
  598. #define perm8_mergesqo 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31
  599. VSX_IMPL_PERM(vec_ushort8, vec_mergesqe, perm8_mergesqe)
  600. VSX_IMPL_PERM(vec_ushort8, vec_mergesqo, perm8_mergesqo)
  601. VSX_IMPL_PERM(vec_short8, vec_mergesqe, perm8_mergesqe)
  602. VSX_IMPL_PERM(vec_short8, vec_mergesqo, perm8_mergesqo)
  603. // 4
  604. #define perm4_mergesqe 0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, 24, 25, 26, 27
  605. #define perm4_mergesqo 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31
  606. VSX_IMPL_PERM(vec_uint4, vec_mergesqe, perm4_mergesqe)
  607. VSX_IMPL_PERM(vec_uint4, vec_mergesqo, perm4_mergesqo)
  608. VSX_IMPL_PERM(vec_int4, vec_mergesqe, perm4_mergesqe)
  609. VSX_IMPL_PERM(vec_int4, vec_mergesqo, perm4_mergesqo)
  610. VSX_IMPL_PERM(vec_float4, vec_mergesqe, perm4_mergesqe)
  611. VSX_IMPL_PERM(vec_float4, vec_mergesqo, perm4_mergesqo)
  612. // 2
  613. VSX_REDIRECT_2RG(vec_double2, vec_double2, vec_mergesqe, vec_mergeh)
  614. VSX_REDIRECT_2RG(vec_double2, vec_double2, vec_mergesqo, vec_mergel)
  615. VSX_REDIRECT_2RG(vec_dword2, vec_dword2, vec_mergesqe, vec_mergeh)
  616. VSX_REDIRECT_2RG(vec_dword2, vec_dword2, vec_mergesqo, vec_mergel)
  617. VSX_REDIRECT_2RG(vec_udword2, vec_udword2, vec_mergesqe, vec_mergeh)
  618. VSX_REDIRECT_2RG(vec_udword2, vec_udword2, vec_mergesqo, vec_mergel)
  619. /*
  620. * Implement vec_mergesqh and vec_mergesql
  621. * Merges the sequence most and least significant halves of two vectors
  622. */
  623. #define VSX_IMPL_MERGESQHL(Tvec) \
  624. VSX_FINLINE(Tvec) vec_mergesqh(const Tvec& a, const Tvec& b) \
  625. { return (Tvec)vec_mergeh(vec_udword2_c(a), vec_udword2_c(b)); } \
  626. VSX_FINLINE(Tvec) vec_mergesql(const Tvec& a, const Tvec& b) \
  627. { return (Tvec)vec_mergel(vec_udword2_c(a), vec_udword2_c(b)); }
  628. VSX_IMPL_MERGESQHL(vec_uchar16)
  629. VSX_IMPL_MERGESQHL(vec_char16)
  630. VSX_IMPL_MERGESQHL(vec_ushort8)
  631. VSX_IMPL_MERGESQHL(vec_short8)
  632. VSX_IMPL_MERGESQHL(vec_uint4)
  633. VSX_IMPL_MERGESQHL(vec_int4)
  634. VSX_IMPL_MERGESQHL(vec_float4)
  635. VSX_REDIRECT_2RG(vec_udword2, vec_udword2, vec_mergesqh, vec_mergeh)
  636. VSX_REDIRECT_2RG(vec_udword2, vec_udword2, vec_mergesql, vec_mergel)
  637. VSX_REDIRECT_2RG(vec_dword2, vec_dword2, vec_mergesqh, vec_mergeh)
  638. VSX_REDIRECT_2RG(vec_dword2, vec_dword2, vec_mergesql, vec_mergel)
  639. VSX_REDIRECT_2RG(vec_double2, vec_double2, vec_mergesqh, vec_mergeh)
  640. VSX_REDIRECT_2RG(vec_double2, vec_double2, vec_mergesql, vec_mergel)
  641. // 2 and 4 channels interleave for all types except 2 lanes
  642. #define VSX_IMPL_ST_INTERLEAVE(Tp, Tvec) \
  643. VSX_FINLINE(void) vec_st_interleave(const Tvec& a, const Tvec& b, Tp* ptr) \
  644. { \
  645. vsx_stf(vec_mergeh(a, b), 0, ptr); \
  646. vsx_stf(vec_mergel(a, b), 16, ptr); \
  647. } \
  648. VSX_FINLINE(void) vec_st_interleave(const Tvec& a, const Tvec& b, \
  649. const Tvec& c, const Tvec& d, Tp* ptr) \
  650. { \
  651. Tvec ac = vec_mergeh(a, c); \
  652. Tvec bd = vec_mergeh(b, d); \
  653. vsx_stf(vec_mergeh(ac, bd), 0, ptr); \
  654. vsx_stf(vec_mergel(ac, bd), 16, ptr); \
  655. ac = vec_mergel(a, c); \
  656. bd = vec_mergel(b, d); \
  657. vsx_stf(vec_mergeh(ac, bd), 32, ptr); \
  658. vsx_stf(vec_mergel(ac, bd), 48, ptr); \
  659. }
  660. VSX_IMPL_ST_INTERLEAVE(uchar, vec_uchar16)
  661. VSX_IMPL_ST_INTERLEAVE(schar, vec_char16)
  662. VSX_IMPL_ST_INTERLEAVE(ushort, vec_ushort8)
  663. VSX_IMPL_ST_INTERLEAVE(short, vec_short8)
  664. VSX_IMPL_ST_INTERLEAVE(uint, vec_uint4)
  665. VSX_IMPL_ST_INTERLEAVE(int, vec_int4)
  666. VSX_IMPL_ST_INTERLEAVE(float, vec_float4)
  667. // 2 and 4 channels deinterleave for 16 lanes
  668. #define VSX_IMPL_ST_DINTERLEAVE_8(Tp, Tvec) \
  669. VSX_FINLINE(void) vec_ld_deinterleave(const Tp* ptr, Tvec& a, Tvec& b) \
  670. { \
  671. Tvec v0 = vsx_ld(0, ptr); \
  672. Tvec v1 = vsx_ld(16, ptr); \
  673. a = vec_mergesqe(v0, v1); \
  674. b = vec_mergesqo(v0, v1); \
  675. } \
  676. VSX_FINLINE(void) vec_ld_deinterleave(const Tp* ptr, Tvec& a, Tvec& b, \
  677. Tvec& c, Tvec& d) \
  678. { \
  679. Tvec v0 = vsx_ld(0, ptr); \
  680. Tvec v1 = vsx_ld(16, ptr); \
  681. Tvec v2 = vsx_ld(32, ptr); \
  682. Tvec v3 = vsx_ld(48, ptr); \
  683. Tvec m0 = vec_mergesqe(v0, v1); \
  684. Tvec m1 = vec_mergesqe(v2, v3); \
  685. a = vec_mergesqe(m0, m1); \
  686. c = vec_mergesqo(m0, m1); \
  687. m0 = vec_mergesqo(v0, v1); \
  688. m1 = vec_mergesqo(v2, v3); \
  689. b = vec_mergesqe(m0, m1); \
  690. d = vec_mergesqo(m0, m1); \
  691. }
  692. VSX_IMPL_ST_DINTERLEAVE_8(uchar, vec_uchar16)
  693. VSX_IMPL_ST_DINTERLEAVE_8(schar, vec_char16)
  694. // 2 and 4 channels deinterleave for 8 lanes
  695. #define VSX_IMPL_ST_DINTERLEAVE_16(Tp, Tvec) \
  696. VSX_FINLINE(void) vec_ld_deinterleave(const Tp* ptr, Tvec& a, Tvec& b) \
  697. { \
  698. Tvec v0 = vsx_ld(0, ptr); \
  699. Tvec v1 = vsx_ld(8, ptr); \
  700. a = vec_mergesqe(v0, v1); \
  701. b = vec_mergesqo(v0, v1); \
  702. } \
  703. VSX_FINLINE(void) vec_ld_deinterleave(const Tp* ptr, Tvec& a, Tvec& b, \
  704. Tvec& c, Tvec& d) \
  705. { \
  706. Tvec v0 = vsx_ld(0, ptr); \
  707. Tvec v1 = vsx_ld(8, ptr); \
  708. Tvec m0 = vec_mergeh(v0, v1); \
  709. Tvec m1 = vec_mergel(v0, v1); \
  710. Tvec ab0 = vec_mergeh(m0, m1); \
  711. Tvec cd0 = vec_mergel(m0, m1); \
  712. v0 = vsx_ld(16, ptr); \
  713. v1 = vsx_ld(24, ptr); \
  714. m0 = vec_mergeh(v0, v1); \
  715. m1 = vec_mergel(v0, v1); \
  716. Tvec ab1 = vec_mergeh(m0, m1); \
  717. Tvec cd1 = vec_mergel(m0, m1); \
  718. a = vec_mergesqh(ab0, ab1); \
  719. b = vec_mergesql(ab0, ab1); \
  720. c = vec_mergesqh(cd0, cd1); \
  721. d = vec_mergesql(cd0, cd1); \
  722. }
  723. VSX_IMPL_ST_DINTERLEAVE_16(ushort, vec_ushort8)
  724. VSX_IMPL_ST_DINTERLEAVE_16(short, vec_short8)
  725. // 2 and 4 channels deinterleave for 4 lanes
  726. #define VSX_IMPL_ST_DINTERLEAVE_32(Tp, Tvec) \
  727. VSX_FINLINE(void) vec_ld_deinterleave(const Tp* ptr, Tvec& a, Tvec& b) \
  728. { \
  729. a = vsx_ld(0, ptr); \
  730. b = vsx_ld(4, ptr); \
  731. Tvec m0 = vec_mergeh(a, b); \
  732. Tvec m1 = vec_mergel(a, b); \
  733. a = vec_mergeh(m0, m1); \
  734. b = vec_mergel(m0, m1); \
  735. } \
  736. VSX_FINLINE(void) vec_ld_deinterleave(const Tp* ptr, Tvec& a, Tvec& b, \
  737. Tvec& c, Tvec& d) \
  738. { \
  739. Tvec v0 = vsx_ld(0, ptr); \
  740. Tvec v1 = vsx_ld(4, ptr); \
  741. Tvec v2 = vsx_ld(8, ptr); \
  742. Tvec v3 = vsx_ld(12, ptr); \
  743. Tvec m0 = vec_mergeh(v0, v2); \
  744. Tvec m1 = vec_mergeh(v1, v3); \
  745. a = vec_mergeh(m0, m1); \
  746. b = vec_mergel(m0, m1); \
  747. m0 = vec_mergel(v0, v2); \
  748. m1 = vec_mergel(v1, v3); \
  749. c = vec_mergeh(m0, m1); \
  750. d = vec_mergel(m0, m1); \
  751. }
  752. VSX_IMPL_ST_DINTERLEAVE_32(uint, vec_uint4)
  753. VSX_IMPL_ST_DINTERLEAVE_32(int, vec_int4)
  754. VSX_IMPL_ST_DINTERLEAVE_32(float, vec_float4)
  755. // 2 and 4 channels interleave and deinterleave for 2 lanes
  756. #define VSX_IMPL_ST_D_INTERLEAVE_64(Tp, Tvec, ld_func, st_func) \
  757. VSX_FINLINE(void) vec_st_interleave(const Tvec& a, const Tvec& b, Tp* ptr) \
  758. { \
  759. st_func(vec_mergeh(a, b), 0, ptr); \
  760. st_func(vec_mergel(a, b), 2, ptr); \
  761. } \
  762. VSX_FINLINE(void) vec_st_interleave(const Tvec& a, const Tvec& b, \
  763. const Tvec& c, const Tvec& d, Tp* ptr) \
  764. { \
  765. st_func(vec_mergeh(a, b), 0, ptr); \
  766. st_func(vec_mergeh(c, d), 2, ptr); \
  767. st_func(vec_mergel(a, b), 4, ptr); \
  768. st_func(vec_mergel(c, d), 6, ptr); \
  769. } \
  770. VSX_FINLINE(void) vec_ld_deinterleave(const Tp* ptr, Tvec& a, Tvec& b) \
  771. { \
  772. Tvec m0 = ld_func(0, ptr); \
  773. Tvec m1 = ld_func(2, ptr); \
  774. a = vec_mergeh(m0, m1); \
  775. b = vec_mergel(m0, m1); \
  776. } \
  777. VSX_FINLINE(void) vec_ld_deinterleave(const Tp* ptr, Tvec& a, Tvec& b, \
  778. Tvec& c, Tvec& d) \
  779. { \
  780. Tvec v0 = ld_func(0, ptr); \
  781. Tvec v1 = ld_func(2, ptr); \
  782. Tvec v2 = ld_func(4, ptr); \
  783. Tvec v3 = ld_func(6, ptr); \
  784. a = vec_mergeh(v0, v2); \
  785. b = vec_mergel(v0, v2); \
  786. c = vec_mergeh(v1, v3); \
  787. d = vec_mergel(v1, v3); \
  788. }
  789. VSX_IMPL_ST_D_INTERLEAVE_64(int64, vec_dword2, vsx_ld2, vsx_st2)
  790. VSX_IMPL_ST_D_INTERLEAVE_64(uint64, vec_udword2, vsx_ld2, vsx_st2)
  791. VSX_IMPL_ST_D_INTERLEAVE_64(double, vec_double2, vsx_ld, vsx_st)
  792. /* 3 channels */
  793. #define VSX_IMPL_ST_INTERLEAVE_3CH_16(Tp, Tvec) \
  794. VSX_FINLINE(void) vec_st_interleave(const Tvec& a, const Tvec& b, \
  795. const Tvec& c, Tp* ptr) \
  796. { \
  797. static const vec_uchar16 a12 = {0, 16, 0, 1, 17, 0, 2, 18, 0, 3, 19, 0, 4, 20, 0, 5}; \
  798. static const vec_uchar16 a123 = {0, 1, 16, 3, 4, 17, 6, 7, 18, 9, 10, 19, 12, 13, 20, 15}; \
  799. vsx_st(vec_perm(vec_perm(a, b, a12), c, a123), 0, ptr); \
  800. static const vec_uchar16 b12 = {21, 0, 6, 22, 0, 7, 23, 0, 8, 24, 0, 9, 25, 0, 10, 26}; \
  801. static const vec_uchar16 b123 = {0, 21, 2, 3, 22, 5, 6, 23, 8, 9, 24, 11, 12, 25, 14, 15}; \
  802. vsx_st(vec_perm(vec_perm(a, b, b12), c, b123), 16, ptr); \
  803. static const vec_uchar16 c12 = {0, 11, 27, 0, 12, 28, 0, 13, 29, 0, 14, 30, 0, 15, 31, 0}; \
  804. static const vec_uchar16 c123 = {26, 1, 2, 27, 4, 5, 28, 7, 8, 29, 10, 11, 30, 13, 14, 31}; \
  805. vsx_st(vec_perm(vec_perm(a, b, c12), c, c123), 32, ptr); \
  806. } \
  807. VSX_FINLINE(void) vec_ld_deinterleave(const Tp* ptr, Tvec& a, Tvec& b, Tvec& c) \
  808. { \
  809. Tvec v1 = vsx_ld(0, ptr); \
  810. Tvec v2 = vsx_ld(16, ptr); \
  811. Tvec v3 = vsx_ld(32, ptr); \
  812. static const vec_uchar16 a12_perm = {0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 0, 0, 0, 0, 0}; \
  813. static const vec_uchar16 a123_perm = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 17, 20, 23, 26, 29}; \
  814. a = vec_perm(vec_perm(v1, v2, a12_perm), v3, a123_perm); \
  815. static const vec_uchar16 b12_perm = {1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 0, 0, 0, 0, 0}; \
  816. static const vec_uchar16 b123_perm = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 18, 21, 24, 27, 30}; \
  817. b = vec_perm(vec_perm(v1, v2, b12_perm), v3, b123_perm); \
  818. static const vec_uchar16 c12_perm = {2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 0, 0, 0, 0, 0, 0}; \
  819. static const vec_uchar16 c123_perm = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 19, 22, 25, 28, 31}; \
  820. c = vec_perm(vec_perm(v1, v2, c12_perm), v3, c123_perm); \
  821. }
  822. VSX_IMPL_ST_INTERLEAVE_3CH_16(uchar, vec_uchar16)
  823. VSX_IMPL_ST_INTERLEAVE_3CH_16(schar, vec_char16)
  824. #define VSX_IMPL_ST_INTERLEAVE_3CH_8(Tp, Tvec) \
  825. VSX_FINLINE(void) vec_st_interleave(const Tvec& a, const Tvec& b, \
  826. const Tvec& c, Tp* ptr) \
  827. { \
  828. static const vec_uchar16 a12 = {0, 1, 16, 17, 0, 0, 2, 3, 18, 19, 0, 0, 4, 5, 20, 21}; \
  829. static const vec_uchar16 a123 = {0, 1, 2, 3, 16, 17, 6, 7, 8, 9, 18, 19, 12, 13, 14, 15}; \
  830. vsx_st(vec_perm(vec_perm(a, b, a12), c, a123), 0, ptr); \
  831. static const vec_uchar16 b12 = {0, 0, 6, 7, 22, 23, 0, 0, 8, 9, 24, 25, 0, 0, 10, 11}; \
  832. static const vec_uchar16 b123 = {20, 21, 2, 3, 4, 5, 22, 23, 8, 9, 10, 11, 24, 25, 14, 15}; \
  833. vsx_st(vec_perm(vec_perm(a, b, b12), c, b123), 8, ptr); \
  834. static const vec_uchar16 c12 = {26, 27, 0, 0, 12, 13, 28, 29, 0, 0, 14, 15, 30, 31, 0, 0}; \
  835. static const vec_uchar16 c123 = {0, 1, 26, 27, 4, 5, 6, 7, 28, 29, 10, 11, 12, 13, 30, 31}; \
  836. vsx_st(vec_perm(vec_perm(a, b, c12), c, c123), 16, ptr); \
  837. } \
  838. VSX_FINLINE(void) vec_ld_deinterleave(const Tp* ptr, Tvec& a, Tvec& b, Tvec& c) \
  839. { \
  840. Tvec v1 = vsx_ld(0, ptr); \
  841. Tvec v2 = vsx_ld(8, ptr); \
  842. Tvec v3 = vsx_ld(16, ptr); \
  843. static const vec_uchar16 a12_perm = {0, 1, 6, 7, 12, 13, 18, 19, 24, 25, 30, 31, 0, 0, 0, 0}; \
  844. static const vec_uchar16 a123_perm = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 20, 21, 26, 27}; \
  845. a = vec_perm(vec_perm(v1, v2, a12_perm), v3, a123_perm); \
  846. static const vec_uchar16 b12_perm = {2, 3, 8, 9, 14, 15, 20, 21, 26, 27, 0, 0, 0, 0, 0, 0}; \
  847. static const vec_uchar16 b123_perm = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 22, 23, 28, 29}; \
  848. b = vec_perm(vec_perm(v1, v2, b12_perm), v3, b123_perm); \
  849. static const vec_uchar16 c12_perm = {4, 5, 10, 11, 16, 17, 22, 23, 28, 29, 0, 0, 0, 0, 0, 0}; \
  850. static const vec_uchar16 c123_perm = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 18, 19, 24, 25, 30, 31}; \
  851. c = vec_perm(vec_perm(v1, v2, c12_perm), v3, c123_perm); \
  852. }
  853. VSX_IMPL_ST_INTERLEAVE_3CH_8(ushort, vec_ushort8)
  854. VSX_IMPL_ST_INTERLEAVE_3CH_8(short, vec_short8)
  855. #define VSX_IMPL_ST_INTERLEAVE_3CH_4(Tp, Tvec) \
  856. VSX_FINLINE(void) vec_st_interleave(const Tvec& a, const Tvec& b, \
  857. const Tvec& c, Tp* ptr) \
  858. { \
  859. Tvec hbc = vec_mergeh(b, c); \
  860. static const vec_uchar16 ahbc = {0, 1, 2, 3, 16, 17, 18, 19, 20, 21, 22, 23, 4, 5, 6, 7}; \
  861. vsx_st(vec_perm(a, hbc, ahbc), 0, ptr); \
  862. Tvec lab = vec_mergel(a, b); \
  863. vsx_st(vec_sld(lab, hbc, 8), 4, ptr); \
  864. static const vec_uchar16 clab = {8, 9, 10, 11, 24, 25, 26, 27, 28, 29, 30, 31, 12, 13, 14, 15};\
  865. vsx_st(vec_perm(c, lab, clab), 8, ptr); \
  866. } \
  867. VSX_FINLINE(void) vec_ld_deinterleave(const Tp* ptr, Tvec& a, Tvec& b, Tvec& c) \
  868. { \
  869. Tvec v1 = vsx_ld(0, ptr); \
  870. Tvec v2 = vsx_ld(4, ptr); \
  871. Tvec v3 = vsx_ld(8, ptr); \
  872. static const vec_uchar16 flp = {0, 1, 2, 3, 12, 13, 14, 15, 16, 17, 18, 19, 28, 29, 30, 31}; \
  873. a = vec_perm(v1, vec_sld(v3, v2, 8), flp); \
  874. static const vec_uchar16 flp2 = {28, 29, 30, 31, 0, 1, 2, 3, 12, 13, 14, 15, 16, 17, 18, 19}; \
  875. b = vec_perm(v2, vec_sld(v1, v3, 8), flp2); \
  876. c = vec_perm(vec_sld(v2, v1, 8), v3, flp); \
  877. }
  878. VSX_IMPL_ST_INTERLEAVE_3CH_4(uint, vec_uint4)
  879. VSX_IMPL_ST_INTERLEAVE_3CH_4(int, vec_int4)
  880. VSX_IMPL_ST_INTERLEAVE_3CH_4(float, vec_float4)
  881. #define VSX_IMPL_ST_INTERLEAVE_3CH_2(Tp, Tvec, ld_func, st_func) \
  882. VSX_FINLINE(void) vec_st_interleave(const Tvec& a, const Tvec& b, \
  883. const Tvec& c, Tp* ptr) \
  884. { \
  885. st_func(vec_mergeh(a, b), 0, ptr); \
  886. st_func(vec_permi(c, a, 1), 2, ptr); \
  887. st_func(vec_mergel(b, c), 4, ptr); \
  888. } \
  889. VSX_FINLINE(void) vec_ld_deinterleave(const Tp* ptr, Tvec& a, \
  890. Tvec& b, Tvec& c) \
  891. { \
  892. Tvec v1 = ld_func(0, ptr); \
  893. Tvec v2 = ld_func(2, ptr); \
  894. Tvec v3 = ld_func(4, ptr); \
  895. a = vec_permi(v1, v2, 1); \
  896. b = vec_permi(v1, v3, 2); \
  897. c = vec_permi(v2, v3, 1); \
  898. }
  899. VSX_IMPL_ST_INTERLEAVE_3CH_2(int64, vec_dword2, vsx_ld2, vsx_st2)
  900. VSX_IMPL_ST_INTERLEAVE_3CH_2(uint64, vec_udword2, vsx_ld2, vsx_st2)
  901. VSX_IMPL_ST_INTERLEAVE_3CH_2(double, vec_double2, vsx_ld, vsx_st)
  902. #endif // CV_VSX
  903. //! @}
  904. #endif // OPENCV_HAL_VSX_UTILS_HPP