simd_builtin.h 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951
  1. // Simd Abi specific implementations -*- C++ -*-
  2. // Copyright (C) 2020-2021 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. #ifndef _GLIBCXX_EXPERIMENTAL_SIMD_ABIS_H_
  21. #define _GLIBCXX_EXPERIMENTAL_SIMD_ABIS_H_
  22. #if __cplusplus >= 201703L
  23. #include <array>
  24. #include <cmath>
  25. #include <cstdlib>
  26. _GLIBCXX_SIMD_BEGIN_NAMESPACE
  27. // _S_allbits{{{
  28. template <typename _V>
  29. static inline _GLIBCXX_SIMD_USE_CONSTEXPR _V _S_allbits
  30. = reinterpret_cast<_V>(~__vector_type_t<char, sizeof(_V) / sizeof(char)>());
  31. // }}}
  32. // _S_signmask, _S_absmask{{{
  33. template <typename _V, typename = _VectorTraits<_V>>
  34. static inline _GLIBCXX_SIMD_USE_CONSTEXPR _V _S_signmask
  35. = __xor(_V() + 1, _V() - 1);
  36. template <typename _V, typename = _VectorTraits<_V>>
  37. static inline _GLIBCXX_SIMD_USE_CONSTEXPR _V _S_absmask
  38. = __andnot(_S_signmask<_V>, _S_allbits<_V>);
  39. //}}}
  40. // __vector_permute<Indices...>{{{
  41. // Index == -1 requests zeroing of the output element
  42. template <int... _Indices, typename _Tp, typename _TVT = _VectorTraits<_Tp>>
  43. _Tp
  44. __vector_permute(_Tp __x)
  45. {
  46. static_assert(sizeof...(_Indices) == _TVT::_S_full_size);
  47. return __make_vector<typename _TVT::value_type>(
  48. (_Indices == -1 ? 0 : __x[_Indices == -1 ? 0 : _Indices])...);
  49. }
  50. // }}}
  51. // __vector_shuffle<Indices...>{{{
  52. // Index == -1 requests zeroing of the output element
  53. template <int... _Indices, typename _Tp, typename _TVT = _VectorTraits<_Tp>>
  54. _Tp
  55. __vector_shuffle(_Tp __x, _Tp __y)
  56. {
  57. return _Tp{(_Indices == -1 ? 0
  58. : _Indices < _TVT::_S_full_size
  59. ? __x[_Indices]
  60. : __y[_Indices - _TVT::_S_full_size])...};
  61. }
  62. // }}}
  63. // __make_wrapper{{{
  64. template <typename _Tp, typename... _Args>
  65. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper<_Tp, sizeof...(_Args)>
  66. __make_wrapper(const _Args&... __args)
  67. { return __make_vector<_Tp>(__args...); }
  68. // }}}
  69. // __wrapper_bitcast{{{
  70. template <typename _Tp, size_t _ToN = 0, typename _Up, size_t _M,
  71. size_t _Np = _ToN != 0 ? _ToN : sizeof(_Up) * _M / sizeof(_Tp)>
  72. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper<_Tp, _Np>
  73. __wrapper_bitcast(_SimdWrapper<_Up, _M> __x)
  74. {
  75. static_assert(_Np > 1);
  76. return __intrin_bitcast<__vector_type_t<_Tp, _Np>>(__x._M_data);
  77. }
  78. // }}}
  79. // __shift_elements_right{{{
  80. // if (__shift % 2ⁿ == 0) => the low n Bytes are correct
  81. template <unsigned __shift, typename _Tp, typename _TVT = _VectorTraits<_Tp>>
  82. _GLIBCXX_SIMD_INTRINSIC _Tp
  83. __shift_elements_right(_Tp __v)
  84. {
  85. [[maybe_unused]] const auto __iv = __to_intrin(__v);
  86. static_assert(__shift <= sizeof(_Tp));
  87. if constexpr (__shift == 0)
  88. return __v;
  89. else if constexpr (__shift == sizeof(_Tp))
  90. return _Tp();
  91. #if _GLIBCXX_SIMD_X86INTRIN // {{{
  92. else if constexpr (__have_sse && __shift == 8
  93. && _TVT::template _S_is<float, 4>)
  94. return _mm_movehl_ps(__iv, __iv);
  95. else if constexpr (__have_sse2 && __shift == 8
  96. && _TVT::template _S_is<double, 2>)
  97. return _mm_unpackhi_pd(__iv, __iv);
  98. else if constexpr (__have_sse2 && sizeof(_Tp) == 16)
  99. return reinterpret_cast<typename _TVT::type>(
  100. _mm_srli_si128(reinterpret_cast<__m128i>(__iv), __shift));
  101. else if constexpr (__shift == 16 && sizeof(_Tp) == 32)
  102. {
  103. /*if constexpr (__have_avx && _TVT::template _S_is<double, 4>)
  104. return _mm256_permute2f128_pd(__iv, __iv, 0x81);
  105. else if constexpr (__have_avx && _TVT::template _S_is<float, 8>)
  106. return _mm256_permute2f128_ps(__iv, __iv, 0x81);
  107. else if constexpr (__have_avx)
  108. return reinterpret_cast<typename _TVT::type>(
  109. _mm256_permute2f128_si256(__iv, __iv, 0x81));
  110. else*/
  111. return __zero_extend(__hi128(__v));
  112. }
  113. else if constexpr (__have_avx2 && sizeof(_Tp) == 32 && __shift < 16)
  114. {
  115. const auto __vll = __vector_bitcast<_LLong>(__v);
  116. return reinterpret_cast<typename _TVT::type>(
  117. _mm256_alignr_epi8(_mm256_permute2x128_si256(__vll, __vll, 0x81),
  118. __vll, __shift));
  119. }
  120. else if constexpr (__have_avx && sizeof(_Tp) == 32 && __shift < 16)
  121. {
  122. const auto __vll = __vector_bitcast<_LLong>(__v);
  123. return reinterpret_cast<typename _TVT::type>(
  124. __concat(_mm_alignr_epi8(__hi128(__vll), __lo128(__vll), __shift),
  125. _mm_srli_si128(__hi128(__vll), __shift)));
  126. }
  127. else if constexpr (sizeof(_Tp) == 32 && __shift > 16)
  128. return __zero_extend(__shift_elements_right<__shift - 16>(__hi128(__v)));
  129. else if constexpr (sizeof(_Tp) == 64 && __shift == 32)
  130. return __zero_extend(__hi256(__v));
  131. else if constexpr (__have_avx512f && sizeof(_Tp) == 64)
  132. {
  133. if constexpr (__shift >= 48)
  134. return __zero_extend(
  135. __shift_elements_right<__shift - 48>(__extract<3, 4>(__v)));
  136. else if constexpr (__shift >= 32)
  137. return __zero_extend(
  138. __shift_elements_right<__shift - 32>(__hi256(__v)));
  139. else if constexpr (__shift % 8 == 0)
  140. return reinterpret_cast<typename _TVT::type>(
  141. _mm512_alignr_epi64(__m512i(), __intrin_bitcast<__m512i>(__v),
  142. __shift / 8));
  143. else if constexpr (__shift % 4 == 0)
  144. return reinterpret_cast<typename _TVT::type>(
  145. _mm512_alignr_epi32(__m512i(), __intrin_bitcast<__m512i>(__v),
  146. __shift / 4));
  147. else if constexpr (__have_avx512bw && __shift < 16)
  148. {
  149. const auto __vll = __vector_bitcast<_LLong>(__v);
  150. return reinterpret_cast<typename _TVT::type>(
  151. _mm512_alignr_epi8(_mm512_shuffle_i32x4(__vll, __vll, 0xf9),
  152. __vll, __shift));
  153. }
  154. else if constexpr (__have_avx512bw && __shift < 32)
  155. {
  156. const auto __vll = __vector_bitcast<_LLong>(__v);
  157. return reinterpret_cast<typename _TVT::type>(
  158. _mm512_alignr_epi8(_mm512_shuffle_i32x4(__vll, __m512i(), 0xee),
  159. _mm512_shuffle_i32x4(__vll, __vll, 0xf9),
  160. __shift - 16));
  161. }
  162. else
  163. __assert_unreachable<_Tp>();
  164. }
  165. /*
  166. } else if constexpr (__shift % 16 == 0 && sizeof(_Tp) == 64)
  167. return __auto_bitcast(__extract<__shift / 16, 4>(__v));
  168. */
  169. #endif // _GLIBCXX_SIMD_X86INTRIN }}}
  170. else
  171. {
  172. constexpr int __chunksize = __shift % 8 == 0 ? 8
  173. : __shift % 4 == 0 ? 4
  174. : __shift % 2 == 0 ? 2
  175. : 1;
  176. auto __w = __vector_bitcast<__int_with_sizeof_t<__chunksize>>(__v);
  177. using _Up = decltype(__w);
  178. return __intrin_bitcast<_Tp>(
  179. __call_with_n_evaluations<(sizeof(_Tp) - __shift) / __chunksize>(
  180. [](auto... __chunks) { return _Up{__chunks...}; },
  181. [&](auto __i) { return __w[__shift / __chunksize + __i]; }));
  182. }
  183. }
  184. // }}}
  185. // __extract_part(_SimdWrapper<_Tp, _Np>) {{{
  186. template <int _Index, int _Total, int _Combine, typename _Tp, size_t _Np>
  187. _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_CONST
  188. _SimdWrapper<_Tp, _Np / _Total * _Combine>
  189. __extract_part(const _SimdWrapper<_Tp, _Np> __x)
  190. {
  191. if constexpr (_Index % 2 == 0 && _Total % 2 == 0 && _Combine % 2 == 0)
  192. return __extract_part<_Index / 2, _Total / 2, _Combine / 2>(__x);
  193. else
  194. {
  195. constexpr size_t __values_per_part = _Np / _Total;
  196. constexpr size_t __values_to_skip = _Index * __values_per_part;
  197. constexpr size_t __return_size = __values_per_part * _Combine;
  198. using _R = __vector_type_t<_Tp, __return_size>;
  199. static_assert((_Index + _Combine) * __values_per_part * sizeof(_Tp)
  200. <= sizeof(__x),
  201. "out of bounds __extract_part");
  202. // the following assertion would ensure no "padding" to be read
  203. // static_assert(_Total >= _Index + _Combine, "_Total must be greater
  204. // than _Index");
  205. // static_assert(__return_size * _Total == _Np, "_Np must be divisible
  206. // by _Total");
  207. if (__x._M_is_constprop())
  208. return __generate_from_n_evaluations<__return_size, _R>(
  209. [&](auto __i) { return __x[__values_to_skip + __i]; });
  210. if constexpr (_Index == 0 && _Total == 1)
  211. return __x;
  212. else if constexpr (_Index == 0)
  213. return __intrin_bitcast<_R>(__as_vector(__x));
  214. #if _GLIBCXX_SIMD_X86INTRIN // {{{
  215. else if constexpr (sizeof(__x) == 32
  216. && __return_size * sizeof(_Tp) <= 16)
  217. {
  218. constexpr size_t __bytes_to_skip = __values_to_skip * sizeof(_Tp);
  219. if constexpr (__bytes_to_skip == 16)
  220. return __vector_bitcast<_Tp, __return_size>(
  221. __hi128(__as_vector(__x)));
  222. else
  223. return __vector_bitcast<_Tp, __return_size>(
  224. _mm_alignr_epi8(__hi128(__vector_bitcast<_LLong>(__x)),
  225. __lo128(__vector_bitcast<_LLong>(__x)),
  226. __bytes_to_skip));
  227. }
  228. #endif // _GLIBCXX_SIMD_X86INTRIN }}}
  229. else if constexpr (_Index > 0
  230. && (__values_to_skip % __return_size != 0
  231. || sizeof(_R) >= 8)
  232. && (__values_to_skip + __return_size) * sizeof(_Tp)
  233. <= 64
  234. && sizeof(__x) >= 16)
  235. return __intrin_bitcast<_R>(
  236. __shift_elements_right<__values_to_skip * sizeof(_Tp)>(
  237. __as_vector(__x)));
  238. else
  239. {
  240. _R __r = {};
  241. __builtin_memcpy(&__r,
  242. reinterpret_cast<const char*>(&__x)
  243. + sizeof(_Tp) * __values_to_skip,
  244. __return_size * sizeof(_Tp));
  245. return __r;
  246. }
  247. }
  248. }
  249. // }}}
  250. // __extract_part(_SimdWrapper<bool, _Np>) {{{
  251. template <int _Index, int _Total, int _Combine = 1, size_t _Np>
  252. _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper<bool, _Np / _Total * _Combine>
  253. __extract_part(const _SimdWrapper<bool, _Np> __x)
  254. {
  255. static_assert(_Combine == 1, "_Combine != 1 not implemented");
  256. static_assert(__have_avx512f && _Np == _Np);
  257. static_assert(_Total >= 2 && _Index + _Combine <= _Total && _Index >= 0);
  258. return __x._M_data >> (_Index * _Np / _Total);
  259. }
  260. // }}}
  261. // __vector_convert {{{
  262. // implementation requires an index sequence
  263. template <typename _To, typename _From, size_t... _I>
  264. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  265. __vector_convert(_From __a, index_sequence<_I...>)
  266. {
  267. using _Tp = typename _VectorTraits<_To>::value_type;
  268. return _To{static_cast<_Tp>(__a[_I])...};
  269. }
  270. template <typename _To, typename _From, size_t... _I>
  271. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  272. __vector_convert(_From __a, _From __b, index_sequence<_I...>)
  273. {
  274. using _Tp = typename _VectorTraits<_To>::value_type;
  275. return _To{static_cast<_Tp>(__a[_I])..., static_cast<_Tp>(__b[_I])...};
  276. }
  277. template <typename _To, typename _From, size_t... _I>
  278. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  279. __vector_convert(_From __a, _From __b, _From __c, index_sequence<_I...>)
  280. {
  281. using _Tp = typename _VectorTraits<_To>::value_type;
  282. return _To{static_cast<_Tp>(__a[_I])..., static_cast<_Tp>(__b[_I])...,
  283. static_cast<_Tp>(__c[_I])...};
  284. }
  285. template <typename _To, typename _From, size_t... _I>
  286. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  287. __vector_convert(_From __a, _From __b, _From __c, _From __d,
  288. index_sequence<_I...>)
  289. {
  290. using _Tp = typename _VectorTraits<_To>::value_type;
  291. return _To{static_cast<_Tp>(__a[_I])..., static_cast<_Tp>(__b[_I])...,
  292. static_cast<_Tp>(__c[_I])..., static_cast<_Tp>(__d[_I])...};
  293. }
  294. template <typename _To, typename _From, size_t... _I>
  295. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  296. __vector_convert(_From __a, _From __b, _From __c, _From __d, _From __e,
  297. index_sequence<_I...>)
  298. {
  299. using _Tp = typename _VectorTraits<_To>::value_type;
  300. return _To{static_cast<_Tp>(__a[_I])..., static_cast<_Tp>(__b[_I])...,
  301. static_cast<_Tp>(__c[_I])..., static_cast<_Tp>(__d[_I])...,
  302. static_cast<_Tp>(__e[_I])...};
  303. }
  304. template <typename _To, typename _From, size_t... _I>
  305. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  306. __vector_convert(_From __a, _From __b, _From __c, _From __d, _From __e,
  307. _From __f, index_sequence<_I...>)
  308. {
  309. using _Tp = typename _VectorTraits<_To>::value_type;
  310. return _To{static_cast<_Tp>(__a[_I])..., static_cast<_Tp>(__b[_I])...,
  311. static_cast<_Tp>(__c[_I])..., static_cast<_Tp>(__d[_I])...,
  312. static_cast<_Tp>(__e[_I])..., static_cast<_Tp>(__f[_I])...};
  313. }
  314. template <typename _To, typename _From, size_t... _I>
  315. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  316. __vector_convert(_From __a, _From __b, _From __c, _From __d, _From __e,
  317. _From __f, _From __g, index_sequence<_I...>)
  318. {
  319. using _Tp = typename _VectorTraits<_To>::value_type;
  320. return _To{static_cast<_Tp>(__a[_I])..., static_cast<_Tp>(__b[_I])...,
  321. static_cast<_Tp>(__c[_I])..., static_cast<_Tp>(__d[_I])...,
  322. static_cast<_Tp>(__e[_I])..., static_cast<_Tp>(__f[_I])...,
  323. static_cast<_Tp>(__g[_I])...};
  324. }
  325. template <typename _To, typename _From, size_t... _I>
  326. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  327. __vector_convert(_From __a, _From __b, _From __c, _From __d, _From __e,
  328. _From __f, _From __g, _From __h, index_sequence<_I...>)
  329. {
  330. using _Tp = typename _VectorTraits<_To>::value_type;
  331. return _To{static_cast<_Tp>(__a[_I])..., static_cast<_Tp>(__b[_I])...,
  332. static_cast<_Tp>(__c[_I])..., static_cast<_Tp>(__d[_I])...,
  333. static_cast<_Tp>(__e[_I])..., static_cast<_Tp>(__f[_I])...,
  334. static_cast<_Tp>(__g[_I])..., static_cast<_Tp>(__h[_I])...};
  335. }
  336. template <typename _To, typename _From, size_t... _I>
  337. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  338. __vector_convert(_From __a, _From __b, _From __c, _From __d, _From __e,
  339. _From __f, _From __g, _From __h, _From __i,
  340. index_sequence<_I...>)
  341. {
  342. using _Tp = typename _VectorTraits<_To>::value_type;
  343. return _To{static_cast<_Tp>(__a[_I])..., static_cast<_Tp>(__b[_I])...,
  344. static_cast<_Tp>(__c[_I])..., static_cast<_Tp>(__d[_I])...,
  345. static_cast<_Tp>(__e[_I])..., static_cast<_Tp>(__f[_I])...,
  346. static_cast<_Tp>(__g[_I])..., static_cast<_Tp>(__h[_I])...,
  347. static_cast<_Tp>(__i[_I])...};
  348. }
  349. template <typename _To, typename _From, size_t... _I>
  350. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  351. __vector_convert(_From __a, _From __b, _From __c, _From __d, _From __e,
  352. _From __f, _From __g, _From __h, _From __i, _From __j,
  353. index_sequence<_I...>)
  354. {
  355. using _Tp = typename _VectorTraits<_To>::value_type;
  356. return _To{static_cast<_Tp>(__a[_I])..., static_cast<_Tp>(__b[_I])...,
  357. static_cast<_Tp>(__c[_I])..., static_cast<_Tp>(__d[_I])...,
  358. static_cast<_Tp>(__e[_I])..., static_cast<_Tp>(__f[_I])...,
  359. static_cast<_Tp>(__g[_I])..., static_cast<_Tp>(__h[_I])...,
  360. static_cast<_Tp>(__i[_I])..., static_cast<_Tp>(__j[_I])...};
  361. }
  362. template <typename _To, typename _From, size_t... _I>
  363. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  364. __vector_convert(_From __a, _From __b, _From __c, _From __d, _From __e,
  365. _From __f, _From __g, _From __h, _From __i, _From __j,
  366. _From __k, index_sequence<_I...>)
  367. {
  368. using _Tp = typename _VectorTraits<_To>::value_type;
  369. return _To{static_cast<_Tp>(__a[_I])..., static_cast<_Tp>(__b[_I])...,
  370. static_cast<_Tp>(__c[_I])..., static_cast<_Tp>(__d[_I])...,
  371. static_cast<_Tp>(__e[_I])..., static_cast<_Tp>(__f[_I])...,
  372. static_cast<_Tp>(__g[_I])..., static_cast<_Tp>(__h[_I])...,
  373. static_cast<_Tp>(__i[_I])..., static_cast<_Tp>(__j[_I])...,
  374. static_cast<_Tp>(__k[_I])...};
  375. }
  376. template <typename _To, typename _From, size_t... _I>
  377. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  378. __vector_convert(_From __a, _From __b, _From __c, _From __d, _From __e,
  379. _From __f, _From __g, _From __h, _From __i, _From __j,
  380. _From __k, _From __l, index_sequence<_I...>)
  381. {
  382. using _Tp = typename _VectorTraits<_To>::value_type;
  383. return _To{static_cast<_Tp>(__a[_I])..., static_cast<_Tp>(__b[_I])...,
  384. static_cast<_Tp>(__c[_I])..., static_cast<_Tp>(__d[_I])...,
  385. static_cast<_Tp>(__e[_I])..., static_cast<_Tp>(__f[_I])...,
  386. static_cast<_Tp>(__g[_I])..., static_cast<_Tp>(__h[_I])...,
  387. static_cast<_Tp>(__i[_I])..., static_cast<_Tp>(__j[_I])...,
  388. static_cast<_Tp>(__k[_I])..., static_cast<_Tp>(__l[_I])...};
  389. }
  390. template <typename _To, typename _From, size_t... _I>
  391. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  392. __vector_convert(_From __a, _From __b, _From __c, _From __d, _From __e,
  393. _From __f, _From __g, _From __h, _From __i, _From __j,
  394. _From __k, _From __l, _From __m, index_sequence<_I...>)
  395. {
  396. using _Tp = typename _VectorTraits<_To>::value_type;
  397. return _To{static_cast<_Tp>(__a[_I])..., static_cast<_Tp>(__b[_I])...,
  398. static_cast<_Tp>(__c[_I])..., static_cast<_Tp>(__d[_I])...,
  399. static_cast<_Tp>(__e[_I])..., static_cast<_Tp>(__f[_I])...,
  400. static_cast<_Tp>(__g[_I])..., static_cast<_Tp>(__h[_I])...,
  401. static_cast<_Tp>(__i[_I])..., static_cast<_Tp>(__j[_I])...,
  402. static_cast<_Tp>(__k[_I])..., static_cast<_Tp>(__l[_I])...,
  403. static_cast<_Tp>(__m[_I])...};
  404. }
  405. template <typename _To, typename _From, size_t... _I>
  406. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  407. __vector_convert(_From __a, _From __b, _From __c, _From __d, _From __e,
  408. _From __f, _From __g, _From __h, _From __i, _From __j,
  409. _From __k, _From __l, _From __m, _From __n,
  410. index_sequence<_I...>)
  411. {
  412. using _Tp = typename _VectorTraits<_To>::value_type;
  413. return _To{static_cast<_Tp>(__a[_I])..., static_cast<_Tp>(__b[_I])...,
  414. static_cast<_Tp>(__c[_I])..., static_cast<_Tp>(__d[_I])...,
  415. static_cast<_Tp>(__e[_I])..., static_cast<_Tp>(__f[_I])...,
  416. static_cast<_Tp>(__g[_I])..., static_cast<_Tp>(__h[_I])...,
  417. static_cast<_Tp>(__i[_I])..., static_cast<_Tp>(__j[_I])...,
  418. static_cast<_Tp>(__k[_I])..., static_cast<_Tp>(__l[_I])...,
  419. static_cast<_Tp>(__m[_I])..., static_cast<_Tp>(__n[_I])...};
  420. }
  421. template <typename _To, typename _From, size_t... _I>
  422. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  423. __vector_convert(_From __a, _From __b, _From __c, _From __d, _From __e,
  424. _From __f, _From __g, _From __h, _From __i, _From __j,
  425. _From __k, _From __l, _From __m, _From __n, _From __o,
  426. index_sequence<_I...>)
  427. {
  428. using _Tp = typename _VectorTraits<_To>::value_type;
  429. return _To{static_cast<_Tp>(__a[_I])..., static_cast<_Tp>(__b[_I])...,
  430. static_cast<_Tp>(__c[_I])..., static_cast<_Tp>(__d[_I])...,
  431. static_cast<_Tp>(__e[_I])..., static_cast<_Tp>(__f[_I])...,
  432. static_cast<_Tp>(__g[_I])..., static_cast<_Tp>(__h[_I])...,
  433. static_cast<_Tp>(__i[_I])..., static_cast<_Tp>(__j[_I])...,
  434. static_cast<_Tp>(__k[_I])..., static_cast<_Tp>(__l[_I])...,
  435. static_cast<_Tp>(__m[_I])..., static_cast<_Tp>(__n[_I])...,
  436. static_cast<_Tp>(__o[_I])...};
  437. }
  438. template <typename _To, typename _From, size_t... _I>
  439. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  440. __vector_convert(_From __a, _From __b, _From __c, _From __d, _From __e,
  441. _From __f, _From __g, _From __h, _From __i, _From __j,
  442. _From __k, _From __l, _From __m, _From __n, _From __o,
  443. _From __p, index_sequence<_I...>)
  444. {
  445. using _Tp = typename _VectorTraits<_To>::value_type;
  446. return _To{static_cast<_Tp>(__a[_I])..., static_cast<_Tp>(__b[_I])...,
  447. static_cast<_Tp>(__c[_I])..., static_cast<_Tp>(__d[_I])...,
  448. static_cast<_Tp>(__e[_I])..., static_cast<_Tp>(__f[_I])...,
  449. static_cast<_Tp>(__g[_I])..., static_cast<_Tp>(__h[_I])...,
  450. static_cast<_Tp>(__i[_I])..., static_cast<_Tp>(__j[_I])...,
  451. static_cast<_Tp>(__k[_I])..., static_cast<_Tp>(__l[_I])...,
  452. static_cast<_Tp>(__m[_I])..., static_cast<_Tp>(__n[_I])...,
  453. static_cast<_Tp>(__o[_I])..., static_cast<_Tp>(__p[_I])...};
  454. }
  455. // Defer actual conversion to the overload that takes an index sequence. Note
  456. // that this function adds zeros or drops values off the end if you don't ensure
  457. // matching width.
  458. template <typename _To, typename... _From, size_t _FromSize>
  459. _GLIBCXX_SIMD_INTRINSIC constexpr _To
  460. __vector_convert(_SimdWrapper<_From, _FromSize>... __xs)
  461. {
  462. #ifdef _GLIBCXX_SIMD_WORKAROUND_PR85048
  463. using _From0 = __first_of_pack_t<_From...>;
  464. using _FW = _SimdWrapper<_From0, _FromSize>;
  465. if (!_FW::_S_is_partial && !(... && __xs._M_is_constprop()))
  466. {
  467. if constexpr ((sizeof...(_From) & (sizeof...(_From) - 1))
  468. == 0) // power-of-two number of arguments
  469. return __convert_x86<_To>(__as_vector(__xs)...);
  470. else // append zeros and recurse until the above branch is taken
  471. return __vector_convert<_To>(__xs..., _FW{});
  472. }
  473. else
  474. #endif
  475. return __vector_convert<_To>(
  476. __as_vector(__xs)...,
  477. make_index_sequence<(sizeof...(__xs) == 1 ? std::min(
  478. _VectorTraits<_To>::_S_full_size, int(_FromSize))
  479. : _FromSize)>());
  480. }
  481. // }}}
  482. // __convert function{{{
  483. template <typename _To, typename _From, typename... _More>
  484. _GLIBCXX_SIMD_INTRINSIC constexpr auto
  485. __convert(_From __v0, _More... __vs)
  486. {
  487. static_assert((true && ... && is_same_v<_From, _More>) );
  488. if constexpr (__is_vectorizable_v<_From>)
  489. {
  490. using _V = typename _VectorTraits<_To>::type;
  491. using _Tp = typename _VectorTraits<_To>::value_type;
  492. return _V{static_cast<_Tp>(__v0), static_cast<_Tp>(__vs)...};
  493. }
  494. else if constexpr (__is_vector_type_v<_From>)
  495. return __convert<_To>(__as_wrapper(__v0), __as_wrapper(__vs)...);
  496. else // _SimdWrapper arguments
  497. {
  498. constexpr size_t __input_size = _From::_S_size * (1 + sizeof...(_More));
  499. if constexpr (__is_vectorizable_v<_To>)
  500. return __convert<__vector_type_t<_To, __input_size>>(__v0, __vs...);
  501. else if constexpr (!__is_vector_type_v<_To>)
  502. return _To(__convert<typename _To::_BuiltinType>(__v0, __vs...));
  503. else
  504. {
  505. static_assert(
  506. sizeof...(_More) == 0
  507. || _VectorTraits<_To>::_S_full_size >= __input_size,
  508. "__convert(...) requires the input to fit into the output");
  509. return __vector_convert<_To>(__v0, __vs...);
  510. }
  511. }
  512. }
  513. // }}}
  514. // __convert_all{{{
  515. // Converts __v into array<_To, N>, where N is _NParts if non-zero or
  516. // otherwise deduced from _To such that N * #elements(_To) <= #elements(__v).
  517. // Note: this function may return less than all converted elements
  518. template <typename _To,
  519. size_t _NParts = 0, // allows to convert fewer or more (only last
  520. // _To, to be partially filled) than all
  521. size_t _Offset = 0, // where to start, # of elements (not Bytes or
  522. // Parts)
  523. typename _From, typename _FromVT = _VectorTraits<_From>>
  524. _GLIBCXX_SIMD_INTRINSIC auto
  525. __convert_all(_From __v)
  526. {
  527. if constexpr (is_arithmetic_v<_To> && _NParts != 1)
  528. {
  529. static_assert(_Offset < _FromVT::_S_full_size);
  530. constexpr auto _Np
  531. = _NParts == 0 ? _FromVT::_S_partial_width - _Offset : _NParts;
  532. return __generate_from_n_evaluations<_Np, array<_To, _Np>>(
  533. [&](auto __i) { return static_cast<_To>(__v[__i + _Offset]); });
  534. }
  535. else
  536. {
  537. static_assert(__is_vector_type_v<_To>);
  538. using _ToVT = _VectorTraits<_To>;
  539. if constexpr (__is_vector_type_v<_From>)
  540. return __convert_all<_To, _NParts>(__as_wrapper(__v));
  541. else if constexpr (_NParts == 1)
  542. {
  543. static_assert(_Offset % _ToVT::_S_full_size == 0);
  544. return array<_To, 1>{__vector_convert<_To>(
  545. __extract_part<_Offset / _ToVT::_S_full_size,
  546. __div_roundup(_FromVT::_S_partial_width,
  547. _ToVT::_S_full_size)>(__v))};
  548. }
  549. #if _GLIBCXX_SIMD_X86INTRIN // {{{
  550. else if constexpr (!__have_sse4_1 && _Offset == 0
  551. && is_integral_v<typename _FromVT::value_type>
  552. && sizeof(typename _FromVT::value_type)
  553. < sizeof(typename _ToVT::value_type)
  554. && !(sizeof(typename _FromVT::value_type) == 4
  555. && is_same_v<typename _ToVT::value_type, double>))
  556. {
  557. using _ToT = typename _ToVT::value_type;
  558. using _FromT = typename _FromVT::value_type;
  559. constexpr size_t _Np
  560. = _NParts != 0
  561. ? _NParts
  562. : (_FromVT::_S_partial_width / _ToVT::_S_full_size);
  563. using _R = array<_To, _Np>;
  564. // __adjust modifies its input to have _Np (use _SizeConstant)
  565. // entries so that no unnecessary intermediate conversions are
  566. // requested and, more importantly, no intermediate conversions are
  567. // missing
  568. [[maybe_unused]] auto __adjust
  569. = [](auto __n,
  570. auto __vv) -> _SimdWrapper<_FromT, decltype(__n)::value> {
  571. return __vector_bitcast<_FromT, decltype(__n)::value>(__vv);
  572. };
  573. [[maybe_unused]] const auto __vi = __to_intrin(__v);
  574. auto&& __make_array = [](auto __x0, [[maybe_unused]] auto __x1) {
  575. if constexpr (_Np == 1)
  576. return _R{__intrin_bitcast<_To>(__x0)};
  577. else
  578. return _R{__intrin_bitcast<_To>(__x0),
  579. __intrin_bitcast<_To>(__x1)};
  580. };
  581. if constexpr (_Np == 0)
  582. return _R{};
  583. else if constexpr (sizeof(_FromT) == 1 && sizeof(_ToT) == 2)
  584. {
  585. static_assert(is_integral_v<_FromT>);
  586. static_assert(is_integral_v<_ToT>);
  587. if constexpr (is_unsigned_v<_FromT>)
  588. return __make_array(_mm_unpacklo_epi8(__vi, __m128i()),
  589. _mm_unpackhi_epi8(__vi, __m128i()));
  590. else
  591. return __make_array(
  592. _mm_srai_epi16(_mm_unpacklo_epi8(__vi, __vi), 8),
  593. _mm_srai_epi16(_mm_unpackhi_epi8(__vi, __vi), 8));
  594. }
  595. else if constexpr (sizeof(_FromT) == 2 && sizeof(_ToT) == 4)
  596. {
  597. static_assert(is_integral_v<_FromT>);
  598. if constexpr (is_floating_point_v<_ToT>)
  599. {
  600. const auto __ints
  601. = __convert_all<__vector_type16_t<int>, _Np>(
  602. __adjust(_SizeConstant<_Np * 4>(), __v));
  603. return __generate_from_n_evaluations<_Np, _R>(
  604. [&](auto __i) {
  605. return __vector_convert<_To>(__as_wrapper(__ints[__i]));
  606. });
  607. }
  608. else if constexpr (is_unsigned_v<_FromT>)
  609. return __make_array(_mm_unpacklo_epi16(__vi, __m128i()),
  610. _mm_unpackhi_epi16(__vi, __m128i()));
  611. else
  612. return __make_array(
  613. _mm_srai_epi32(_mm_unpacklo_epi16(__vi, __vi), 16),
  614. _mm_srai_epi32(_mm_unpackhi_epi16(__vi, __vi), 16));
  615. }
  616. else if constexpr (sizeof(_FromT) == 4 && sizeof(_ToT) == 8
  617. && is_integral_v<_FromT> && is_integral_v<_ToT>)
  618. {
  619. if constexpr (is_unsigned_v<_FromT>)
  620. return __make_array(_mm_unpacklo_epi32(__vi, __m128i()),
  621. _mm_unpackhi_epi32(__vi, __m128i()));
  622. else
  623. return __make_array(
  624. _mm_unpacklo_epi32(__vi, _mm_srai_epi32(__vi, 31)),
  625. _mm_unpackhi_epi32(__vi, _mm_srai_epi32(__vi, 31)));
  626. }
  627. else if constexpr (sizeof(_FromT) == 4 && sizeof(_ToT) == 8
  628. && is_integral_v<_FromT> && is_integral_v<_ToT>)
  629. {
  630. if constexpr (is_unsigned_v<_FromT>)
  631. return __make_array(_mm_unpacklo_epi32(__vi, __m128i()),
  632. _mm_unpackhi_epi32(__vi, __m128i()));
  633. else
  634. return __make_array(
  635. _mm_unpacklo_epi32(__vi, _mm_srai_epi32(__vi, 31)),
  636. _mm_unpackhi_epi32(__vi, _mm_srai_epi32(__vi, 31)));
  637. }
  638. else if constexpr (sizeof(_FromT) == 1 && sizeof(_ToT) >= 4
  639. && is_signed_v<_FromT>)
  640. {
  641. const __m128i __vv[2] = {_mm_unpacklo_epi8(__vi, __vi),
  642. _mm_unpackhi_epi8(__vi, __vi)};
  643. const __vector_type_t<int, 4> __vvvv[4] = {
  644. __vector_bitcast<int>(_mm_unpacklo_epi16(__vv[0], __vv[0])),
  645. __vector_bitcast<int>(_mm_unpackhi_epi16(__vv[0], __vv[0])),
  646. __vector_bitcast<int>(_mm_unpacklo_epi16(__vv[1], __vv[1])),
  647. __vector_bitcast<int>(_mm_unpackhi_epi16(__vv[1], __vv[1]))};
  648. if constexpr (sizeof(_ToT) == 4)
  649. return __generate_from_n_evaluations<_Np, _R>([&](auto __i) {
  650. return __vector_convert<_To>(
  651. _SimdWrapper<int, 4>(__vvvv[__i] >> 24));
  652. });
  653. else if constexpr (is_integral_v<_ToT>)
  654. return __generate_from_n_evaluations<_Np, _R>([&](auto __i) {
  655. const auto __signbits = __to_intrin(__vvvv[__i / 2] >> 31);
  656. const auto __sx32 = __to_intrin(__vvvv[__i / 2] >> 24);
  657. return __vector_bitcast<_ToT>(
  658. __i % 2 == 0 ? _mm_unpacklo_epi32(__sx32, __signbits)
  659. : _mm_unpackhi_epi32(__sx32, __signbits));
  660. });
  661. else
  662. return __generate_from_n_evaluations<_Np, _R>([&](auto __i) {
  663. const _SimdWrapper<int, 4> __int4 = __vvvv[__i / 2] >> 24;
  664. return __vector_convert<_To>(
  665. __i % 2 == 0 ? __int4
  666. : _SimdWrapper<int, 4>(
  667. _mm_unpackhi_epi64(__to_intrin(__int4),
  668. __to_intrin(__int4))));
  669. });
  670. }
  671. else if constexpr (sizeof(_FromT) == 1 && sizeof(_ToT) == 4)
  672. {
  673. const auto __shorts = __convert_all<__vector_type16_t<
  674. conditional_t<is_signed_v<_FromT>, short, unsigned short>>>(
  675. __adjust(_SizeConstant<(_Np + 1) / 2 * 8>(), __v));
  676. return __generate_from_n_evaluations<_Np, _R>([&](auto __i) {
  677. return __convert_all<_To>(__shorts[__i / 2])[__i % 2];
  678. });
  679. }
  680. else if constexpr (sizeof(_FromT) == 2 && sizeof(_ToT) == 8
  681. && is_signed_v<_FromT> && is_integral_v<_ToT>)
  682. {
  683. const __m128i __vv[2] = {_mm_unpacklo_epi16(__vi, __vi),
  684. _mm_unpackhi_epi16(__vi, __vi)};
  685. const __vector_type16_t<int> __vvvv[4]
  686. = {__vector_bitcast<int>(
  687. _mm_unpacklo_epi32(_mm_srai_epi32(__vv[0], 16),
  688. _mm_srai_epi32(__vv[0], 31))),
  689. __vector_bitcast<int>(
  690. _mm_unpackhi_epi32(_mm_srai_epi32(__vv[0], 16),
  691. _mm_srai_epi32(__vv[0], 31))),
  692. __vector_bitcast<int>(
  693. _mm_unpacklo_epi32(_mm_srai_epi32(__vv[1], 16),
  694. _mm_srai_epi32(__vv[1], 31))),
  695. __vector_bitcast<int>(
  696. _mm_unpackhi_epi32(_mm_srai_epi32(__vv[1], 16),
  697. _mm_srai_epi32(__vv[1], 31)))};
  698. return __generate_from_n_evaluations<_Np, _R>([&](auto __i) {
  699. return __vector_bitcast<_ToT>(__vvvv[__i]);
  700. });
  701. }
  702. else if constexpr (sizeof(_FromT) <= 2 && sizeof(_ToT) == 8)
  703. {
  704. const auto __ints
  705. = __convert_all<__vector_type16_t<conditional_t<
  706. is_signed_v<_FromT> || is_floating_point_v<_ToT>, int,
  707. unsigned int>>>(
  708. __adjust(_SizeConstant<(_Np + 1) / 2 * 4>(), __v));
  709. return __generate_from_n_evaluations<_Np, _R>([&](auto __i) {
  710. return __convert_all<_To>(__ints[__i / 2])[__i % 2];
  711. });
  712. }
  713. else
  714. __assert_unreachable<_To>();
  715. }
  716. #endif // _GLIBCXX_SIMD_X86INTRIN }}}
  717. else if constexpr ((_FromVT::_S_partial_width - _Offset)
  718. > _ToVT::_S_full_size)
  719. {
  720. /*
  721. static_assert(
  722. (_FromVT::_S_partial_width & (_FromVT::_S_partial_width - 1)) ==
  723. 0,
  724. "__convert_all only supports power-of-2 number of elements.
  725. Otherwise " "the return type cannot be array<_To, N>.");
  726. */
  727. constexpr size_t _NTotal
  728. = (_FromVT::_S_partial_width - _Offset) / _ToVT::_S_full_size;
  729. constexpr size_t _Np = _NParts == 0 ? _NTotal : _NParts;
  730. static_assert(
  731. _Np <= _NTotal
  732. || (_Np == _NTotal + 1
  733. && (_FromVT::_S_partial_width - _Offset) % _ToVT::_S_full_size
  734. > 0));
  735. using _R = array<_To, _Np>;
  736. if constexpr (_Np == 1)
  737. return _R{__vector_convert<_To>(
  738. __extract_part<_Offset, _FromVT::_S_partial_width,
  739. _ToVT::_S_full_size>(__v))};
  740. else
  741. return __generate_from_n_evaluations<_Np, _R>([&](
  742. auto __i) constexpr {
  743. auto __part
  744. = __extract_part<__i * _ToVT::_S_full_size + _Offset,
  745. _FromVT::_S_partial_width,
  746. _ToVT::_S_full_size>(__v);
  747. return __vector_convert<_To>(__part);
  748. });
  749. }
  750. else if constexpr (_Offset == 0)
  751. return array<_To, 1>{__vector_convert<_To>(__v)};
  752. else
  753. return array<_To, 1>{__vector_convert<_To>(
  754. __extract_part<_Offset, _FromVT::_S_partial_width,
  755. _FromVT::_S_partial_width - _Offset>(__v))};
  756. }
  757. }
  758. // }}}
  759. // _GnuTraits {{{
  760. template <typename _Tp, typename _Mp, typename _Abi, size_t _Np>
  761. struct _GnuTraits
  762. {
  763. using _IsValid = true_type;
  764. using _SimdImpl = typename _Abi::_SimdImpl;
  765. using _MaskImpl = typename _Abi::_MaskImpl;
  766. // simd and simd_mask member types {{{
  767. using _SimdMember = _SimdWrapper<_Tp, _Np>;
  768. using _MaskMember = _SimdWrapper<_Mp, _Np>;
  769. static constexpr size_t _S_simd_align = alignof(_SimdMember);
  770. static constexpr size_t _S_mask_align = alignof(_MaskMember);
  771. // }}}
  772. // size metadata {{{
  773. static constexpr size_t _S_full_size = _SimdMember::_S_full_size;
  774. static constexpr bool _S_is_partial = _SimdMember::_S_is_partial;
  775. // }}}
  776. // _SimdBase / base class for simd, providing extra conversions {{{
  777. struct _SimdBase2
  778. {
  779. explicit operator __intrinsic_type_t<_Tp, _Np>() const
  780. {
  781. return __to_intrin(static_cast<const simd<_Tp, _Abi>*>(this)->_M_data);
  782. }
  783. explicit operator __vector_type_t<_Tp, _Np>() const
  784. {
  785. return static_cast<const simd<_Tp, _Abi>*>(this)->_M_data.__builtin();
  786. }
  787. };
  788. struct _SimdBase1
  789. {
  790. explicit operator __intrinsic_type_t<_Tp, _Np>() const
  791. { return __data(*static_cast<const simd<_Tp, _Abi>*>(this)); }
  792. };
  793. using _SimdBase = conditional_t<
  794. is_same<__intrinsic_type_t<_Tp, _Np>, __vector_type_t<_Tp, _Np>>::value,
  795. _SimdBase1, _SimdBase2>;
  796. // }}}
  797. // _MaskBase {{{
  798. struct _MaskBase2
  799. {
  800. explicit operator __intrinsic_type_t<_Tp, _Np>() const
  801. {
  802. return static_cast<const simd_mask<_Tp, _Abi>*>(this)
  803. ->_M_data.__intrin();
  804. }
  805. explicit operator __vector_type_t<_Tp, _Np>() const
  806. {
  807. return static_cast<const simd_mask<_Tp, _Abi>*>(this)->_M_data._M_data;
  808. }
  809. };
  810. struct _MaskBase1
  811. {
  812. explicit operator __intrinsic_type_t<_Tp, _Np>() const
  813. { return __data(*static_cast<const simd_mask<_Tp, _Abi>*>(this)); }
  814. };
  815. using _MaskBase = conditional_t<
  816. is_same<__intrinsic_type_t<_Tp, _Np>, __vector_type_t<_Tp, _Np>>::value,
  817. _MaskBase1, _MaskBase2>;
  818. // }}}
  819. // _MaskCastType {{{
  820. // parameter type of one explicit simd_mask constructor
  821. class _MaskCastType
  822. {
  823. using _Up = __intrinsic_type_t<_Tp, _Np>;
  824. _Up _M_data;
  825. public:
  826. _MaskCastType(_Up __x) : _M_data(__x) {}
  827. operator _MaskMember() const { return _M_data; }
  828. };
  829. // }}}
  830. // _SimdCastType {{{
  831. // parameter type of one explicit simd constructor
  832. class _SimdCastType1
  833. {
  834. using _Ap = __intrinsic_type_t<_Tp, _Np>;
  835. _SimdMember _M_data;
  836. public:
  837. _SimdCastType1(_Ap __a) : _M_data(__vector_bitcast<_Tp>(__a)) {}
  838. operator _SimdMember() const { return _M_data; }
  839. };
  840. class _SimdCastType2
  841. {
  842. using _Ap = __intrinsic_type_t<_Tp, _Np>;
  843. using _Bp = __vector_type_t<_Tp, _Np>;
  844. _SimdMember _M_data;
  845. public:
  846. _SimdCastType2(_Ap __a) : _M_data(__vector_bitcast<_Tp>(__a)) {}
  847. _SimdCastType2(_Bp __b) : _M_data(__b) {}
  848. operator _SimdMember() const { return _M_data; }
  849. };
  850. using _SimdCastType = conditional_t<
  851. is_same<__intrinsic_type_t<_Tp, _Np>, __vector_type_t<_Tp, _Np>>::value,
  852. _SimdCastType1, _SimdCastType2>;
  853. //}}}
  854. };
  855. // }}}
  856. struct _CommonImplX86;
  857. struct _CommonImplNeon;
  858. struct _CommonImplBuiltin;
  859. template <typename _Abi> struct _SimdImplBuiltin;
  860. template <typename _Abi> struct _MaskImplBuiltin;
  861. template <typename _Abi> struct _SimdImplX86;
  862. template <typename _Abi> struct _MaskImplX86;
  863. template <typename _Abi> struct _SimdImplNeon;
  864. template <typename _Abi> struct _MaskImplNeon;
  865. template <typename _Abi> struct _SimdImplPpc;
  866. template <typename _Abi> struct _MaskImplPpc;
  867. // simd_abi::_VecBuiltin {{{
  868. template <int _UsedBytes>
  869. struct simd_abi::_VecBuiltin
  870. {
  871. template <typename _Tp>
  872. static constexpr size_t _S_size = _UsedBytes / sizeof(_Tp);
  873. // validity traits {{{
  874. struct _IsValidAbiTag : __bool_constant<(_UsedBytes > 1)> {};
  875. template <typename _Tp>
  876. struct _IsValidSizeFor
  877. : __bool_constant<(_UsedBytes / sizeof(_Tp) > 1
  878. && _UsedBytes % sizeof(_Tp) == 0
  879. && _UsedBytes <= __vectorized_sizeof<_Tp>()
  880. && (!__have_avx512f || _UsedBytes <= 32))> {};
  881. template <typename _Tp>
  882. struct _IsValid : conjunction<_IsValidAbiTag, __is_vectorizable<_Tp>,
  883. _IsValidSizeFor<_Tp>> {};
  884. template <typename _Tp>
  885. static constexpr bool _S_is_valid_v = _IsValid<_Tp>::value;
  886. // }}}
  887. // _SimdImpl/_MaskImpl {{{
  888. #if _GLIBCXX_SIMD_X86INTRIN
  889. using _CommonImpl = _CommonImplX86;
  890. using _SimdImpl = _SimdImplX86<_VecBuiltin<_UsedBytes>>;
  891. using _MaskImpl = _MaskImplX86<_VecBuiltin<_UsedBytes>>;
  892. #elif _GLIBCXX_SIMD_HAVE_NEON
  893. using _CommonImpl = _CommonImplNeon;
  894. using _SimdImpl = _SimdImplNeon<_VecBuiltin<_UsedBytes>>;
  895. using _MaskImpl = _MaskImplNeon<_VecBuiltin<_UsedBytes>>;
  896. #else
  897. using _CommonImpl = _CommonImplBuiltin;
  898. #ifdef __ALTIVEC__
  899. using _SimdImpl = _SimdImplPpc<_VecBuiltin<_UsedBytes>>;
  900. using _MaskImpl = _MaskImplPpc<_VecBuiltin<_UsedBytes>>;
  901. #else
  902. using _SimdImpl = _SimdImplBuiltin<_VecBuiltin<_UsedBytes>>;
  903. using _MaskImpl = _MaskImplBuiltin<_VecBuiltin<_UsedBytes>>;
  904. #endif
  905. #endif
  906. // }}}
  907. // __traits {{{
  908. template <typename _Tp>
  909. using _MaskValueType = __int_for_sizeof_t<_Tp>;
  910. template <typename _Tp>
  911. using __traits
  912. = conditional_t<_S_is_valid_v<_Tp>,
  913. _GnuTraits<_Tp, _MaskValueType<_Tp>,
  914. _VecBuiltin<_UsedBytes>, _S_size<_Tp>>,
  915. _InvalidTraits>;
  916. //}}}
  917. // size metadata {{{
  918. template <typename _Tp>
  919. static constexpr size_t _S_full_size = __traits<_Tp>::_S_full_size;
  920. template <typename _Tp>
  921. static constexpr bool _S_is_partial = __traits<_Tp>::_S_is_partial;
  922. // }}}
  923. // implicit masks {{{
  924. template <typename _Tp>
  925. using _MaskMember = _SimdWrapper<_MaskValueType<_Tp>, _S_size<_Tp>>;
  926. template <typename _Tp>
  927. _GLIBCXX_SIMD_INTRINSIC static constexpr _MaskMember<_Tp>
  928. _S_implicit_mask()
  929. {
  930. using _UV = typename _MaskMember<_Tp>::_BuiltinType;
  931. if constexpr (!_MaskMember<_Tp>::_S_is_partial)
  932. return ~_UV();
  933. else
  934. {
  935. constexpr auto __size = _S_size<_Tp>;
  936. _GLIBCXX_SIMD_USE_CONSTEXPR auto __r = __generate_vector<_UV>(
  937. [](auto __i) constexpr { return __i < __size ? -1 : 0; });
  938. return __r;
  939. }
  940. }
  941. template <typename _Tp>
  942. _GLIBCXX_SIMD_INTRINSIC static constexpr __intrinsic_type_t<_Tp,
  943. _S_size<_Tp>>
  944. _S_implicit_mask_intrin()
  945. {
  946. return __to_intrin(
  947. __vector_bitcast<_Tp>(_S_implicit_mask<_Tp>()._M_data));
  948. }
  949. template <typename _TW, typename _TVT = _VectorTraits<_TW>>
  950. _GLIBCXX_SIMD_INTRINSIC static constexpr _TW _S_masked(_TW __x)
  951. {
  952. using _Tp = typename _TVT::value_type;
  953. if constexpr (!_MaskMember<_Tp>::_S_is_partial)
  954. return __x;
  955. else
  956. return __and(__as_vector(__x),
  957. __vector_bitcast<_Tp>(_S_implicit_mask<_Tp>()));
  958. }
  959. template <typename _TW, typename _TVT = _VectorTraits<_TW>>
  960. _GLIBCXX_SIMD_INTRINSIC static constexpr auto
  961. __make_padding_nonzero(_TW __x)
  962. {
  963. using _Tp = typename _TVT::value_type;
  964. if constexpr (!_S_is_partial<_Tp>)
  965. return __x;
  966. else
  967. {
  968. _GLIBCXX_SIMD_USE_CONSTEXPR auto __implicit_mask
  969. = __vector_bitcast<_Tp>(_S_implicit_mask<_Tp>());
  970. if constexpr (is_integral_v<_Tp>)
  971. return __or(__x, ~__implicit_mask);
  972. else
  973. {
  974. _GLIBCXX_SIMD_USE_CONSTEXPR auto __one
  975. = __andnot(__implicit_mask,
  976. __vector_broadcast<_S_full_size<_Tp>>(_Tp(1)));
  977. // it's not enough to return `x | 1_in_padding` because the
  978. // padding in x might be inf or nan (independent of
  979. // __FINITE_MATH_ONLY__, because it's about padding bits)
  980. return __or(__and(__x, __implicit_mask), __one);
  981. }
  982. }
  983. }
  984. // }}}
  985. };
  986. // }}}
  987. // simd_abi::_VecBltnBtmsk {{{
  988. template <int _UsedBytes>
  989. struct simd_abi::_VecBltnBtmsk
  990. {
  991. template <typename _Tp>
  992. static constexpr size_t _S_size = _UsedBytes / sizeof(_Tp);
  993. // validity traits {{{
  994. struct _IsValidAbiTag : __bool_constant<(_UsedBytes > 1)> {};
  995. template <typename _Tp>
  996. struct _IsValidSizeFor
  997. : __bool_constant<(_UsedBytes / sizeof(_Tp) > 1
  998. && _UsedBytes % sizeof(_Tp) == 0 && _UsedBytes <= 64
  999. && (_UsedBytes > 32 || __have_avx512vl))> {};
  1000. // Bitmasks require at least AVX512F. If sizeof(_Tp) < 4 the AVX512BW is also
  1001. // required.
  1002. template <typename _Tp>
  1003. struct _IsValid
  1004. : conjunction<
  1005. _IsValidAbiTag, __bool_constant<__have_avx512f>,
  1006. __bool_constant<__have_avx512bw || (sizeof(_Tp) >= 4)>,
  1007. __bool_constant<(__vectorized_sizeof<_Tp>() > sizeof(_Tp))>,
  1008. _IsValidSizeFor<_Tp>> {};
  1009. template <typename _Tp>
  1010. static constexpr bool _S_is_valid_v = _IsValid<_Tp>::value;
  1011. // }}}
  1012. // simd/_MaskImpl {{{
  1013. #if _GLIBCXX_SIMD_X86INTRIN
  1014. using _CommonImpl = _CommonImplX86;
  1015. using _SimdImpl = _SimdImplX86<_VecBltnBtmsk<_UsedBytes>>;
  1016. using _MaskImpl = _MaskImplX86<_VecBltnBtmsk<_UsedBytes>>;
  1017. #else
  1018. template <int>
  1019. struct _MissingImpl;
  1020. using _CommonImpl = _MissingImpl<_UsedBytes>;
  1021. using _SimdImpl = _MissingImpl<_UsedBytes>;
  1022. using _MaskImpl = _MissingImpl<_UsedBytes>;
  1023. #endif
  1024. // }}}
  1025. // __traits {{{
  1026. template <typename _Tp>
  1027. using _MaskMember = _SimdWrapper<bool, _S_size<_Tp>>;
  1028. template <typename _Tp>
  1029. using __traits = conditional_t<
  1030. _S_is_valid_v<_Tp>,
  1031. _GnuTraits<_Tp, bool, _VecBltnBtmsk<_UsedBytes>, _S_size<_Tp>>,
  1032. _InvalidTraits>;
  1033. //}}}
  1034. // size metadata {{{
  1035. template <typename _Tp>
  1036. static constexpr size_t _S_full_size = __traits<_Tp>::_S_full_size;
  1037. template <typename _Tp>
  1038. static constexpr bool _S_is_partial = __traits<_Tp>::_S_is_partial;
  1039. // }}}
  1040. // implicit mask {{{
  1041. private:
  1042. template <typename _Tp>
  1043. using _ImplicitMask = _SimdWrapper<bool, _S_size<_Tp>>;
  1044. public:
  1045. template <size_t _Np>
  1046. _GLIBCXX_SIMD_INTRINSIC static constexpr __bool_storage_member_type_t<_Np>
  1047. __implicit_mask_n()
  1048. {
  1049. using _Tp = __bool_storage_member_type_t<_Np>;
  1050. return _Np < sizeof(_Tp) * __CHAR_BIT__ ? _Tp((1ULL << _Np) - 1) : ~_Tp();
  1051. }
  1052. template <typename _Tp>
  1053. _GLIBCXX_SIMD_INTRINSIC static constexpr _ImplicitMask<_Tp>
  1054. _S_implicit_mask()
  1055. { return __implicit_mask_n<_S_size<_Tp>>(); }
  1056. template <typename _Tp>
  1057. _GLIBCXX_SIMD_INTRINSIC static constexpr __bool_storage_member_type_t<
  1058. _S_size<_Tp>>
  1059. _S_implicit_mask_intrin()
  1060. { return __implicit_mask_n<_S_size<_Tp>>(); }
  1061. template <typename _Tp, size_t _Np>
  1062. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  1063. _S_masked(_SimdWrapper<_Tp, _Np> __x)
  1064. {
  1065. if constexpr (is_same_v<_Tp, bool>)
  1066. if constexpr (_Np < 8 || (_Np & (_Np - 1)) != 0)
  1067. return _MaskImpl::_S_bit_and(
  1068. __x, _SimdWrapper<_Tp, _Np>(
  1069. __bool_storage_member_type_t<_Np>((1ULL << _Np) - 1)));
  1070. else
  1071. return __x;
  1072. else
  1073. return _S_masked(__x._M_data);
  1074. }
  1075. template <typename _TV>
  1076. _GLIBCXX_SIMD_INTRINSIC static constexpr _TV
  1077. _S_masked(_TV __x)
  1078. {
  1079. using _Tp = typename _VectorTraits<_TV>::value_type;
  1080. static_assert(
  1081. !__is_bitmask_v<_TV>,
  1082. "_VecBltnBtmsk::_S_masked cannot work on bitmasks, since it doesn't "
  1083. "know the number of elements. Use _SimdWrapper<bool, N> instead.");
  1084. if constexpr (_S_is_partial<_Tp>)
  1085. {
  1086. constexpr size_t _Np = _S_size<_Tp>;
  1087. return __make_dependent_t<_TV, _CommonImpl>::_S_blend(
  1088. _S_implicit_mask<_Tp>(), _SimdWrapper<_Tp, _Np>(),
  1089. _SimdWrapper<_Tp, _Np>(__x));
  1090. }
  1091. else
  1092. return __x;
  1093. }
  1094. template <typename _TV, typename _TVT = _VectorTraits<_TV>>
  1095. _GLIBCXX_SIMD_INTRINSIC static constexpr auto
  1096. __make_padding_nonzero(_TV __x)
  1097. {
  1098. using _Tp = typename _TVT::value_type;
  1099. if constexpr (!_S_is_partial<_Tp>)
  1100. return __x;
  1101. else
  1102. {
  1103. constexpr size_t _Np = _S_size<_Tp>;
  1104. if constexpr (is_integral_v<typename _TVT::value_type>)
  1105. return __x
  1106. | __generate_vector<_Tp, _S_full_size<_Tp>>(
  1107. [](auto __i) -> _Tp {
  1108. if (__i < _Np)
  1109. return 0;
  1110. else
  1111. return 1;
  1112. });
  1113. else
  1114. return __make_dependent_t<_TV, _CommonImpl>::_S_blend(
  1115. _S_implicit_mask<_Tp>(),
  1116. _SimdWrapper<_Tp, _Np>(
  1117. __vector_broadcast<_S_full_size<_Tp>>(_Tp(1))),
  1118. _SimdWrapper<_Tp, _Np>(__x))
  1119. ._M_data;
  1120. }
  1121. }
  1122. // }}}
  1123. };
  1124. //}}}
  1125. // _CommonImplBuiltin {{{
  1126. struct _CommonImplBuiltin
  1127. {
  1128. // _S_converts_via_decomposition{{{
  1129. // This lists all cases where a __vector_convert needs to fall back to
  1130. // conversion of individual scalars (i.e. decompose the input vector into
  1131. // scalars, convert, compose output vector). In those cases, _S_masked_load &
  1132. // _S_masked_store prefer to use the _S_bit_iteration implementation.
  1133. template <typename _From, typename _To, size_t _ToSize>
  1134. static inline constexpr bool __converts_via_decomposition_v
  1135. = sizeof(_From) != sizeof(_To);
  1136. // }}}
  1137. // _S_load{{{
  1138. template <typename _Tp, size_t _Np, size_t _Bytes = _Np * sizeof(_Tp)>
  1139. _GLIBCXX_SIMD_INTRINSIC static __vector_type_t<_Tp, _Np>
  1140. _S_load(const void* __p)
  1141. {
  1142. static_assert(_Np > 1);
  1143. static_assert(_Bytes % sizeof(_Tp) == 0);
  1144. using _Rp = __vector_type_t<_Tp, _Np>;
  1145. if constexpr (sizeof(_Rp) == _Bytes)
  1146. {
  1147. _Rp __r;
  1148. __builtin_memcpy(&__r, __p, _Bytes);
  1149. return __r;
  1150. }
  1151. else
  1152. {
  1153. #ifdef _GLIBCXX_SIMD_WORKAROUND_PR90424
  1154. using _Up = conditional_t<
  1155. is_integral_v<_Tp>,
  1156. conditional_t<_Bytes % 4 == 0,
  1157. conditional_t<_Bytes % 8 == 0, long long, int>,
  1158. conditional_t<_Bytes % 2 == 0, short, signed char>>,
  1159. conditional_t<(_Bytes < 8 || _Np % 2 == 1 || _Np == 2), _Tp,
  1160. double>>;
  1161. using _V = __vector_type_t<_Up, _Np * sizeof(_Tp) / sizeof(_Up)>;
  1162. if constexpr (sizeof(_V) != sizeof(_Rp))
  1163. { // on i386 with 4 < _Bytes <= 8
  1164. _Rp __r{};
  1165. __builtin_memcpy(&__r, __p, _Bytes);
  1166. return __r;
  1167. }
  1168. else
  1169. #else // _GLIBCXX_SIMD_WORKAROUND_PR90424
  1170. using _V = _Rp;
  1171. #endif // _GLIBCXX_SIMD_WORKAROUND_PR90424
  1172. {
  1173. _V __r{};
  1174. static_assert(_Bytes <= sizeof(_V));
  1175. __builtin_memcpy(&__r, __p, _Bytes);
  1176. return reinterpret_cast<_Rp>(__r);
  1177. }
  1178. }
  1179. }
  1180. // }}}
  1181. // _S_store {{{
  1182. template <size_t _ReqBytes = 0, typename _TV>
  1183. _GLIBCXX_SIMD_INTRINSIC static void _S_store(_TV __x, void* __addr)
  1184. {
  1185. constexpr size_t _Bytes = _ReqBytes == 0 ? sizeof(__x) : _ReqBytes;
  1186. static_assert(sizeof(__x) >= _Bytes);
  1187. if constexpr (__is_vector_type_v<_TV>)
  1188. {
  1189. using _Tp = typename _VectorTraits<_TV>::value_type;
  1190. constexpr size_t _Np = _Bytes / sizeof(_Tp);
  1191. static_assert(_Np * sizeof(_Tp) == _Bytes);
  1192. #ifdef _GLIBCXX_SIMD_WORKAROUND_PR90424
  1193. using _Up = conditional_t<
  1194. (is_integral_v<_Tp> || _Bytes < 4),
  1195. conditional_t<(sizeof(__x) > sizeof(long long)), long long, _Tp>,
  1196. float>;
  1197. const auto __v = __vector_bitcast<_Up>(__x);
  1198. #else // _GLIBCXX_SIMD_WORKAROUND_PR90424
  1199. const __vector_type_t<_Tp, _Np> __v = __x;
  1200. #endif // _GLIBCXX_SIMD_WORKAROUND_PR90424
  1201. if constexpr ((_Bytes & (_Bytes - 1)) != 0)
  1202. {
  1203. constexpr size_t _MoreBytes = std::__bit_ceil(_Bytes);
  1204. alignas(decltype(__v)) char __tmp[_MoreBytes];
  1205. __builtin_memcpy(__tmp, &__v, _MoreBytes);
  1206. __builtin_memcpy(__addr, __tmp, _Bytes);
  1207. }
  1208. else
  1209. __builtin_memcpy(__addr, &__v, _Bytes);
  1210. }
  1211. else
  1212. __builtin_memcpy(__addr, &__x, _Bytes);
  1213. }
  1214. template <typename _Tp, size_t _Np>
  1215. _GLIBCXX_SIMD_INTRINSIC static void _S_store(_SimdWrapper<_Tp, _Np> __x,
  1216. void* __addr)
  1217. { _S_store<_Np * sizeof(_Tp)>(__x._M_data, __addr); }
  1218. // }}}
  1219. // _S_store_bool_array(_BitMask) {{{
  1220. template <size_t _Np, bool _Sanitized>
  1221. _GLIBCXX_SIMD_INTRINSIC static constexpr void
  1222. _S_store_bool_array(_BitMask<_Np, _Sanitized> __x, bool* __mem)
  1223. {
  1224. if constexpr (_Np == 1)
  1225. __mem[0] = __x[0];
  1226. else if constexpr (_Np == 2)
  1227. {
  1228. short __bool2 = (__x._M_to_bits() * 0x81) & 0x0101;
  1229. _S_store<_Np>(__bool2, __mem);
  1230. }
  1231. else if constexpr (_Np == 3)
  1232. {
  1233. int __bool3 = (__x._M_to_bits() * 0x4081) & 0x010101;
  1234. _S_store<_Np>(__bool3, __mem);
  1235. }
  1236. else
  1237. {
  1238. __execute_n_times<__div_roundup(_Np, 4)>([&](auto __i) {
  1239. constexpr int __offset = __i * 4;
  1240. constexpr int __remaining = _Np - __offset;
  1241. if constexpr (__remaining > 4 && __remaining <= 7)
  1242. {
  1243. const _ULLong __bool7
  1244. = (__x.template _M_extract<__offset>()._M_to_bits()
  1245. * 0x40810204081ULL)
  1246. & 0x0101010101010101ULL;
  1247. _S_store<__remaining>(__bool7, __mem + __offset);
  1248. }
  1249. else if constexpr (__remaining >= 4)
  1250. {
  1251. int __bits = __x.template _M_extract<__offset>()._M_to_bits();
  1252. if constexpr (__remaining > 7)
  1253. __bits &= 0xf;
  1254. const int __bool4 = (__bits * 0x204081) & 0x01010101;
  1255. _S_store<4>(__bool4, __mem + __offset);
  1256. }
  1257. });
  1258. }
  1259. }
  1260. // }}}
  1261. // _S_blend{{{
  1262. template <typename _Tp, size_t _Np>
  1263. _GLIBCXX_SIMD_INTRINSIC static constexpr auto
  1264. _S_blend(_SimdWrapper<__int_for_sizeof_t<_Tp>, _Np> __k,
  1265. _SimdWrapper<_Tp, _Np> __at0, _SimdWrapper<_Tp, _Np> __at1)
  1266. { return __k._M_data ? __at1._M_data : __at0._M_data; }
  1267. // }}}
  1268. };
  1269. // }}}
  1270. // _SimdImplBuiltin {{{1
  1271. template <typename _Abi>
  1272. struct _SimdImplBuiltin
  1273. {
  1274. // member types {{{2
  1275. template <typename _Tp>
  1276. static constexpr size_t _S_max_store_size = 16;
  1277. using abi_type = _Abi;
  1278. template <typename _Tp>
  1279. using _TypeTag = _Tp*;
  1280. template <typename _Tp>
  1281. using _SimdMember = typename _Abi::template __traits<_Tp>::_SimdMember;
  1282. template <typename _Tp>
  1283. using _MaskMember = typename _Abi::template _MaskMember<_Tp>;
  1284. template <typename _Tp>
  1285. static constexpr size_t _S_size = _Abi::template _S_size<_Tp>;
  1286. template <typename _Tp>
  1287. static constexpr size_t _S_full_size = _Abi::template _S_full_size<_Tp>;
  1288. using _CommonImpl = typename _Abi::_CommonImpl;
  1289. using _SuperImpl = typename _Abi::_SimdImpl;
  1290. using _MaskImpl = typename _Abi::_MaskImpl;
  1291. // _M_make_simd(_SimdWrapper/__intrinsic_type_t) {{{2
  1292. template <typename _Tp, size_t _Np>
  1293. _GLIBCXX_SIMD_INTRINSIC static simd<_Tp, _Abi>
  1294. _M_make_simd(_SimdWrapper<_Tp, _Np> __x)
  1295. { return {__private_init, __x}; }
  1296. template <typename _Tp, size_t _Np>
  1297. _GLIBCXX_SIMD_INTRINSIC static simd<_Tp, _Abi>
  1298. _M_make_simd(__intrinsic_type_t<_Tp, _Np> __x)
  1299. { return {__private_init, __vector_bitcast<_Tp>(__x)}; }
  1300. // _S_broadcast {{{2
  1301. template <typename _Tp>
  1302. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdMember<_Tp>
  1303. _S_broadcast(_Tp __x) noexcept
  1304. { return __vector_broadcast<_S_full_size<_Tp>>(__x); }
  1305. // _S_generator {{{2
  1306. template <typename _Fp, typename _Tp>
  1307. inline static constexpr _SimdMember<_Tp> _S_generator(_Fp&& __gen,
  1308. _TypeTag<_Tp>)
  1309. {
  1310. return __generate_vector<_Tp, _S_full_size<_Tp>>([&](
  1311. auto __i) constexpr {
  1312. if constexpr (__i < _S_size<_Tp>)
  1313. return __gen(__i);
  1314. else
  1315. return 0;
  1316. });
  1317. }
  1318. // _S_load {{{2
  1319. template <typename _Tp, typename _Up>
  1320. _GLIBCXX_SIMD_INTRINSIC static _SimdMember<_Tp>
  1321. _S_load(const _Up* __mem, _TypeTag<_Tp>) noexcept
  1322. {
  1323. constexpr size_t _Np = _S_size<_Tp>;
  1324. constexpr size_t __max_load_size
  1325. = (sizeof(_Up) >= 4 && __have_avx512f) || __have_avx512bw ? 64
  1326. : (is_floating_point_v<_Up> && __have_avx) || __have_avx2 ? 32
  1327. : 16;
  1328. constexpr size_t __bytes_to_load = sizeof(_Up) * _Np;
  1329. if constexpr (sizeof(_Up) > 8)
  1330. return __generate_vector<_Tp, _SimdMember<_Tp>::_S_full_size>([&](
  1331. auto __i) constexpr {
  1332. return static_cast<_Tp>(__i < _Np ? __mem[__i] : 0);
  1333. });
  1334. else if constexpr (is_same_v<_Up, _Tp>)
  1335. return _CommonImpl::template _S_load<_Tp, _S_full_size<_Tp>,
  1336. _Np * sizeof(_Tp)>(__mem);
  1337. else if constexpr (__bytes_to_load <= __max_load_size)
  1338. return __convert<_SimdMember<_Tp>>(
  1339. _CommonImpl::template _S_load<_Up, _Np>(__mem));
  1340. else if constexpr (__bytes_to_load % __max_load_size == 0)
  1341. {
  1342. constexpr size_t __n_loads = __bytes_to_load / __max_load_size;
  1343. constexpr size_t __elements_per_load = _Np / __n_loads;
  1344. return __call_with_n_evaluations<__n_loads>(
  1345. [](auto... __uncvted) {
  1346. return __convert<_SimdMember<_Tp>>(__uncvted...);
  1347. },
  1348. [&](auto __i) {
  1349. return _CommonImpl::template _S_load<_Up, __elements_per_load>(
  1350. __mem + __i * __elements_per_load);
  1351. });
  1352. }
  1353. else if constexpr (__bytes_to_load % (__max_load_size / 2) == 0
  1354. && __max_load_size > 16)
  1355. { // e.g. int[] -> <char, 12> with AVX2
  1356. constexpr size_t __n_loads
  1357. = __bytes_to_load / (__max_load_size / 2);
  1358. constexpr size_t __elements_per_load = _Np / __n_loads;
  1359. return __call_with_n_evaluations<__n_loads>(
  1360. [](auto... __uncvted) {
  1361. return __convert<_SimdMember<_Tp>>(__uncvted...);
  1362. },
  1363. [&](auto __i) {
  1364. return _CommonImpl::template _S_load<_Up, __elements_per_load>(
  1365. __mem + __i * __elements_per_load);
  1366. });
  1367. }
  1368. else // e.g. int[] -> <char, 9>
  1369. return __call_with_subscripts(
  1370. __mem, make_index_sequence<_Np>(), [](auto... __args) {
  1371. return __vector_type_t<_Tp, _S_full_size<_Tp>>{
  1372. static_cast<_Tp>(__args)...};
  1373. });
  1374. }
  1375. // _S_masked_load {{{2
  1376. template <typename _Tp, size_t _Np, typename _Up>
  1377. static inline _SimdWrapper<_Tp, _Np>
  1378. _S_masked_load(_SimdWrapper<_Tp, _Np> __merge, _MaskMember<_Tp> __k,
  1379. const _Up* __mem) noexcept
  1380. {
  1381. _BitOps::_S_bit_iteration(_MaskImpl::_S_to_bits(__k), [&](auto __i) {
  1382. __merge._M_set(__i, static_cast<_Tp>(__mem[__i]));
  1383. });
  1384. return __merge;
  1385. }
  1386. // _S_store {{{2
  1387. template <typename _Tp, typename _Up>
  1388. _GLIBCXX_SIMD_INTRINSIC static void
  1389. _S_store(_SimdMember<_Tp> __v, _Up* __mem, _TypeTag<_Tp>) noexcept
  1390. {
  1391. // TODO: converting int -> "smaller int" can be optimized with AVX512
  1392. constexpr size_t _Np = _S_size<_Tp>;
  1393. constexpr size_t __max_store_size
  1394. = _SuperImpl::template _S_max_store_size<_Up>;
  1395. if constexpr (sizeof(_Up) > 8)
  1396. __execute_n_times<_Np>([&](auto __i) constexpr {
  1397. __mem[__i] = __v[__i];
  1398. });
  1399. else if constexpr (is_same_v<_Up, _Tp>)
  1400. _CommonImpl::_S_store(__v, __mem);
  1401. else if constexpr (sizeof(_Up) * _Np <= __max_store_size)
  1402. _CommonImpl::_S_store(_SimdWrapper<_Up, _Np>(__convert<_Up>(__v)),
  1403. __mem);
  1404. else
  1405. {
  1406. constexpr size_t __vsize = __max_store_size / sizeof(_Up);
  1407. // round up to convert the last partial vector as well:
  1408. constexpr size_t __stores = __div_roundup(_Np, __vsize);
  1409. constexpr size_t __full_stores = _Np / __vsize;
  1410. using _V = __vector_type_t<_Up, __vsize>;
  1411. const array<_V, __stores> __converted
  1412. = __convert_all<_V, __stores>(__v);
  1413. __execute_n_times<__full_stores>([&](auto __i) constexpr {
  1414. _CommonImpl::_S_store(__converted[__i], __mem + __i * __vsize);
  1415. });
  1416. if constexpr (__full_stores < __stores)
  1417. _CommonImpl::template _S_store<(_Np - __full_stores * __vsize)
  1418. * sizeof(_Up)>(
  1419. __converted[__full_stores], __mem + __full_stores * __vsize);
  1420. }
  1421. }
  1422. // _S_masked_store_nocvt {{{2
  1423. template <typename _Tp, size_t _Np>
  1424. _GLIBCXX_SIMD_INTRINSIC static void
  1425. _S_masked_store_nocvt(_SimdWrapper<_Tp, _Np> __v, _Tp* __mem,
  1426. _MaskMember<_Tp> __k)
  1427. {
  1428. _BitOps::_S_bit_iteration(
  1429. _MaskImpl::_S_to_bits(__k), [&](auto __i) constexpr {
  1430. __mem[__i] = __v[__i];
  1431. });
  1432. }
  1433. // _S_masked_store {{{2
  1434. template <typename _TW, typename _TVT = _VectorTraits<_TW>,
  1435. typename _Tp = typename _TVT::value_type, typename _Up>
  1436. static inline void
  1437. _S_masked_store(const _TW __v, _Up* __mem, const _MaskMember<_Tp> __k)
  1438. noexcept
  1439. {
  1440. constexpr size_t _TV_size = _S_size<_Tp>;
  1441. [[maybe_unused]] const auto __vi = __to_intrin(__v);
  1442. constexpr size_t __max_store_size
  1443. = _SuperImpl::template _S_max_store_size<_Up>;
  1444. if constexpr (
  1445. is_same_v<
  1446. _Tp,
  1447. _Up> || (is_integral_v<_Tp> && is_integral_v<_Up> && sizeof(_Tp) == sizeof(_Up)))
  1448. {
  1449. // bitwise or no conversion, reinterpret:
  1450. const _MaskMember<_Up> __kk = [&]() {
  1451. if constexpr (__is_bitmask_v<decltype(__k)>)
  1452. return _MaskMember<_Up>(__k._M_data);
  1453. else
  1454. return __wrapper_bitcast<__int_for_sizeof_t<_Up>>(__k);
  1455. }();
  1456. _SuperImpl::_S_masked_store_nocvt(__wrapper_bitcast<_Up>(__v),
  1457. __mem, __kk);
  1458. }
  1459. else if constexpr (__vectorized_sizeof<_Up>() > sizeof(_Up)
  1460. && !_CommonImpl::
  1461. template __converts_via_decomposition_v<
  1462. _Tp, _Up, __max_store_size>)
  1463. { // conversion via decomposition is better handled via the
  1464. // bit_iteration
  1465. // fallback below
  1466. constexpr size_t _UW_size
  1467. = std::min(_TV_size, __max_store_size / sizeof(_Up));
  1468. static_assert(_UW_size <= _TV_size);
  1469. using _UW = _SimdWrapper<_Up, _UW_size>;
  1470. using _UV = __vector_type_t<_Up, _UW_size>;
  1471. using _UAbi = simd_abi::deduce_t<_Up, _UW_size>;
  1472. if constexpr (_UW_size == _TV_size) // one convert+store
  1473. {
  1474. const _UW __converted = __convert<_UW>(__v);
  1475. _SuperImpl::_S_masked_store_nocvt(
  1476. __converted, __mem,
  1477. _UAbi::_MaskImpl::template _S_convert<
  1478. __int_for_sizeof_t<_Up>>(__k));
  1479. }
  1480. else
  1481. {
  1482. static_assert(_UW_size * sizeof(_Up) == __max_store_size);
  1483. constexpr size_t _NFullStores = _TV_size / _UW_size;
  1484. constexpr size_t _NAllStores
  1485. = __div_roundup(_TV_size, _UW_size);
  1486. constexpr size_t _NParts = _S_full_size<_Tp> / _UW_size;
  1487. const array<_UV, _NAllStores> __converted
  1488. = __convert_all<_UV, _NAllStores>(__v);
  1489. __execute_n_times<_NFullStores>([&](auto __i) {
  1490. _SuperImpl::_S_masked_store_nocvt(
  1491. _UW(__converted[__i]), __mem + __i * _UW_size,
  1492. _UAbi::_MaskImpl::template _S_convert<
  1493. __int_for_sizeof_t<_Up>>(
  1494. __extract_part<__i, _NParts>(__k.__as_full_vector())));
  1495. });
  1496. if constexpr (_NAllStores
  1497. > _NFullStores) // one partial at the end
  1498. _SuperImpl::_S_masked_store_nocvt(
  1499. _UW(__converted[_NFullStores]),
  1500. __mem + _NFullStores * _UW_size,
  1501. _UAbi::_MaskImpl::template _S_convert<
  1502. __int_for_sizeof_t<_Up>>(
  1503. __extract_part<_NFullStores, _NParts>(
  1504. __k.__as_full_vector())));
  1505. }
  1506. }
  1507. else
  1508. _BitOps::_S_bit_iteration(
  1509. _MaskImpl::_S_to_bits(__k), [&](auto __i) constexpr {
  1510. __mem[__i] = static_cast<_Up>(__v[__i]);
  1511. });
  1512. }
  1513. // _S_complement {{{2
  1514. template <typename _Tp, size_t _Np>
  1515. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  1516. _S_complement(_SimdWrapper<_Tp, _Np> __x) noexcept
  1517. { return ~__x._M_data; }
  1518. // _S_unary_minus {{{2
  1519. template <typename _Tp, size_t _Np>
  1520. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  1521. _S_unary_minus(_SimdWrapper<_Tp, _Np> __x) noexcept
  1522. {
  1523. // GCC doesn't use the psign instructions, but pxor & psub seem to be
  1524. // just as good a choice as pcmpeqd & psign. So meh.
  1525. return -__x._M_data;
  1526. }
  1527. // arithmetic operators {{{2
  1528. template <typename _Tp, size_t _Np>
  1529. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  1530. _S_plus(_SimdWrapper<_Tp, _Np> __x, _SimdWrapper<_Tp, _Np> __y)
  1531. { return __x._M_data + __y._M_data; }
  1532. template <typename _Tp, size_t _Np>
  1533. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  1534. _S_minus(_SimdWrapper<_Tp, _Np> __x, _SimdWrapper<_Tp, _Np> __y)
  1535. { return __x._M_data - __y._M_data; }
  1536. template <typename _Tp, size_t _Np>
  1537. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  1538. _S_multiplies(_SimdWrapper<_Tp, _Np> __x, _SimdWrapper<_Tp, _Np> __y)
  1539. { return __x._M_data * __y._M_data; }
  1540. template <typename _Tp, size_t _Np>
  1541. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  1542. _S_divides(_SimdWrapper<_Tp, _Np> __x, _SimdWrapper<_Tp, _Np> __y)
  1543. {
  1544. // Note that division by 0 is always UB, so we must ensure we avoid the
  1545. // case for partial registers
  1546. if constexpr (!_Abi::template _S_is_partial<_Tp>)
  1547. return __x._M_data / __y._M_data;
  1548. else
  1549. return __x._M_data / _Abi::__make_padding_nonzero(__y._M_data);
  1550. }
  1551. template <typename _Tp, size_t _Np>
  1552. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  1553. _S_modulus(_SimdWrapper<_Tp, _Np> __x, _SimdWrapper<_Tp, _Np> __y)
  1554. {
  1555. if constexpr (!_Abi::template _S_is_partial<_Tp>)
  1556. return __x._M_data % __y._M_data;
  1557. else
  1558. return __as_vector(__x)
  1559. % _Abi::__make_padding_nonzero(__as_vector(__y));
  1560. }
  1561. template <typename _Tp, size_t _Np>
  1562. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  1563. _S_bit_and(_SimdWrapper<_Tp, _Np> __x, _SimdWrapper<_Tp, _Np> __y)
  1564. { return __and(__x, __y); }
  1565. template <typename _Tp, size_t _Np>
  1566. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  1567. _S_bit_or(_SimdWrapper<_Tp, _Np> __x, _SimdWrapper<_Tp, _Np> __y)
  1568. { return __or(__x, __y); }
  1569. template <typename _Tp, size_t _Np>
  1570. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  1571. _S_bit_xor(_SimdWrapper<_Tp, _Np> __x, _SimdWrapper<_Tp, _Np> __y)
  1572. { return __xor(__x, __y); }
  1573. template <typename _Tp, size_t _Np>
  1574. _GLIBCXX_SIMD_INTRINSIC static _SimdWrapper<_Tp, _Np>
  1575. _S_bit_shift_left(_SimdWrapper<_Tp, _Np> __x, _SimdWrapper<_Tp, _Np> __y)
  1576. { return __x._M_data << __y._M_data; }
  1577. template <typename _Tp, size_t _Np>
  1578. _GLIBCXX_SIMD_INTRINSIC static _SimdWrapper<_Tp, _Np>
  1579. _S_bit_shift_right(_SimdWrapper<_Tp, _Np> __x, _SimdWrapper<_Tp, _Np> __y)
  1580. { return __x._M_data >> __y._M_data; }
  1581. template <typename _Tp, size_t _Np>
  1582. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  1583. _S_bit_shift_left(_SimdWrapper<_Tp, _Np> __x, int __y)
  1584. { return __x._M_data << __y; }
  1585. template <typename _Tp, size_t _Np>
  1586. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  1587. _S_bit_shift_right(_SimdWrapper<_Tp, _Np> __x, int __y)
  1588. { return __x._M_data >> __y; }
  1589. // compares {{{2
  1590. // _S_equal_to {{{3
  1591. template <typename _Tp, size_t _Np>
  1592. _GLIBCXX_SIMD_INTRINSIC static constexpr _MaskMember<_Tp>
  1593. _S_equal_to(_SimdWrapper<_Tp, _Np> __x, _SimdWrapper<_Tp, _Np> __y)
  1594. { return __x._M_data == __y._M_data; }
  1595. // _S_not_equal_to {{{3
  1596. template <typename _Tp, size_t _Np>
  1597. _GLIBCXX_SIMD_INTRINSIC static constexpr _MaskMember<_Tp>
  1598. _S_not_equal_to(_SimdWrapper<_Tp, _Np> __x, _SimdWrapper<_Tp, _Np> __y)
  1599. { return __x._M_data != __y._M_data; }
  1600. // _S_less {{{3
  1601. template <typename _Tp, size_t _Np>
  1602. _GLIBCXX_SIMD_INTRINSIC static constexpr _MaskMember<_Tp>
  1603. _S_less(_SimdWrapper<_Tp, _Np> __x, _SimdWrapper<_Tp, _Np> __y)
  1604. { return __x._M_data < __y._M_data; }
  1605. // _S_less_equal {{{3
  1606. template <typename _Tp, size_t _Np>
  1607. _GLIBCXX_SIMD_INTRINSIC static constexpr _MaskMember<_Tp>
  1608. _S_less_equal(_SimdWrapper<_Tp, _Np> __x, _SimdWrapper<_Tp, _Np> __y)
  1609. { return __x._M_data <= __y._M_data; }
  1610. // _S_negate {{{2
  1611. template <typename _Tp, size_t _Np>
  1612. _GLIBCXX_SIMD_INTRINSIC static constexpr _MaskMember<_Tp>
  1613. _S_negate(_SimdWrapper<_Tp, _Np> __x) noexcept
  1614. { return !__x._M_data; }
  1615. // _S_min, _S_max, _S_minmax {{{2
  1616. template <typename _Tp, size_t _Np>
  1617. _GLIBCXX_SIMD_NORMAL_MATH _GLIBCXX_SIMD_INTRINSIC static constexpr
  1618. _SimdWrapper<_Tp, _Np>
  1619. _S_min(_SimdWrapper<_Tp, _Np> __a, _SimdWrapper<_Tp, _Np> __b)
  1620. { return __a._M_data < __b._M_data ? __a._M_data : __b._M_data; }
  1621. template <typename _Tp, size_t _Np>
  1622. _GLIBCXX_SIMD_NORMAL_MATH _GLIBCXX_SIMD_INTRINSIC static constexpr
  1623. _SimdWrapper<_Tp, _Np>
  1624. _S_max(_SimdWrapper<_Tp, _Np> __a, _SimdWrapper<_Tp, _Np> __b)
  1625. { return __a._M_data > __b._M_data ? __a._M_data : __b._M_data; }
  1626. template <typename _Tp, size_t _Np>
  1627. _GLIBCXX_SIMD_NORMAL_MATH _GLIBCXX_SIMD_INTRINSIC static constexpr
  1628. pair<_SimdWrapper<_Tp, _Np>, _SimdWrapper<_Tp, _Np>>
  1629. _S_minmax(_SimdWrapper<_Tp, _Np> __a, _SimdWrapper<_Tp, _Np> __b)
  1630. {
  1631. return {__a._M_data < __b._M_data ? __a._M_data : __b._M_data,
  1632. __a._M_data < __b._M_data ? __b._M_data : __a._M_data};
  1633. }
  1634. // reductions {{{2
  1635. template <size_t _Np, size_t... _Is, size_t... _Zeros, typename _Tp,
  1636. typename _BinaryOperation>
  1637. _GLIBCXX_SIMD_INTRINSIC static _Tp
  1638. _S_reduce_partial(index_sequence<_Is...>, index_sequence<_Zeros...>,
  1639. simd<_Tp, _Abi> __x, _BinaryOperation&& __binary_op)
  1640. {
  1641. using _V = __vector_type_t<_Tp, _Np / 2>;
  1642. static_assert(sizeof(_V) <= sizeof(__x));
  1643. // _S_full_size is the size of the smallest native SIMD register that
  1644. // can store _Np/2 elements:
  1645. using _FullSimd = __deduced_simd<_Tp, _VectorTraits<_V>::_S_full_size>;
  1646. using _HalfSimd = __deduced_simd<_Tp, _Np / 2>;
  1647. const auto __xx = __as_vector(__x);
  1648. return _HalfSimd::abi_type::_SimdImpl::_S_reduce(
  1649. static_cast<_HalfSimd>(__as_vector(__binary_op(
  1650. static_cast<_FullSimd>(__intrin_bitcast<_V>(__xx)),
  1651. static_cast<_FullSimd>(__intrin_bitcast<_V>(
  1652. __vector_permute<(_Np / 2 + _Is)..., (int(_Zeros * 0) - 1)...>(
  1653. __xx)))))),
  1654. __binary_op);
  1655. }
  1656. template <typename _Tp, typename _BinaryOperation>
  1657. _GLIBCXX_SIMD_INTRINSIC static constexpr _Tp
  1658. _S_reduce(simd<_Tp, _Abi> __x, _BinaryOperation&& __binary_op)
  1659. {
  1660. constexpr size_t _Np = simd_size_v<_Tp, _Abi>;
  1661. if constexpr (_Np == 1)
  1662. return __x[0];
  1663. else if constexpr (_Np == 2)
  1664. return __binary_op(simd<_Tp, simd_abi::scalar>(__x[0]),
  1665. simd<_Tp, simd_abi::scalar>(__x[1]))[0];
  1666. else if constexpr (_Abi::template _S_is_partial<_Tp>) //{{{
  1667. {
  1668. [[maybe_unused]] constexpr auto __full_size
  1669. = _Abi::template _S_full_size<_Tp>;
  1670. if constexpr (_Np == 3)
  1671. return __binary_op(
  1672. __binary_op(simd<_Tp, simd_abi::scalar>(__x[0]),
  1673. simd<_Tp, simd_abi::scalar>(__x[1])),
  1674. simd<_Tp, simd_abi::scalar>(__x[2]))[0];
  1675. else if constexpr (is_same_v<__remove_cvref_t<_BinaryOperation>,
  1676. plus<>>)
  1677. {
  1678. using _Ap = simd_abi::deduce_t<_Tp, __full_size>;
  1679. return _Ap::_SimdImpl::_S_reduce(
  1680. simd<_Tp, _Ap>(__private_init,
  1681. _Abi::_S_masked(__as_vector(__x))),
  1682. __binary_op);
  1683. }
  1684. else if constexpr (is_same_v<__remove_cvref_t<_BinaryOperation>,
  1685. multiplies<>>)
  1686. {
  1687. using _Ap = simd_abi::deduce_t<_Tp, __full_size>;
  1688. using _TW = _SimdWrapper<_Tp, __full_size>;
  1689. _GLIBCXX_SIMD_USE_CONSTEXPR auto __implicit_mask_full
  1690. = _Abi::template _S_implicit_mask<_Tp>().__as_full_vector();
  1691. _GLIBCXX_SIMD_USE_CONSTEXPR _TW __one
  1692. = __vector_broadcast<__full_size>(_Tp(1));
  1693. const _TW __x_full = __data(__x).__as_full_vector();
  1694. const _TW __x_padded_with_ones
  1695. = _Ap::_CommonImpl::_S_blend(__implicit_mask_full, __one,
  1696. __x_full);
  1697. return _Ap::_SimdImpl::_S_reduce(
  1698. simd<_Tp, _Ap>(__private_init, __x_padded_with_ones),
  1699. __binary_op);
  1700. }
  1701. else if constexpr (_Np & 1)
  1702. {
  1703. using _Ap = simd_abi::deduce_t<_Tp, _Np - 1>;
  1704. return __binary_op(
  1705. simd<_Tp, simd_abi::scalar>(_Ap::_SimdImpl::_S_reduce(
  1706. simd<_Tp, _Ap>(
  1707. __intrin_bitcast<__vector_type_t<_Tp, _Np - 1>>(
  1708. __as_vector(__x))),
  1709. __binary_op)),
  1710. simd<_Tp, simd_abi::scalar>(__x[_Np - 1]))[0];
  1711. }
  1712. else
  1713. return _S_reduce_partial<_Np>(
  1714. make_index_sequence<_Np / 2>(),
  1715. make_index_sequence<__full_size - _Np / 2>(), __x, __binary_op);
  1716. } //}}}
  1717. else if constexpr (sizeof(__x) == 16) //{{{
  1718. {
  1719. if constexpr (_Np == 16)
  1720. {
  1721. const auto __y = __data(__x);
  1722. __x = __binary_op(
  1723. _M_make_simd<_Tp, _Np>(
  1724. __vector_permute<0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  1725. 7, 7>(__y)),
  1726. _M_make_simd<_Tp, _Np>(
  1727. __vector_permute<8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
  1728. 14, 14, 15, 15>(__y)));
  1729. }
  1730. if constexpr (_Np >= 8)
  1731. {
  1732. const auto __y = __vector_bitcast<short>(__data(__x));
  1733. __x = __binary_op(
  1734. _M_make_simd<_Tp, _Np>(__vector_bitcast<_Tp>(
  1735. __vector_permute<0, 0, 1, 1, 2, 2, 3, 3>(__y))),
  1736. _M_make_simd<_Tp, _Np>(__vector_bitcast<_Tp>(
  1737. __vector_permute<4, 4, 5, 5, 6, 6, 7, 7>(__y))));
  1738. }
  1739. if constexpr (_Np >= 4)
  1740. {
  1741. using _Up = conditional_t<is_floating_point_v<_Tp>, float, int>;
  1742. const auto __y = __vector_bitcast<_Up>(__data(__x));
  1743. __x = __binary_op(__x,
  1744. _M_make_simd<_Tp, _Np>(__vector_bitcast<_Tp>(
  1745. __vector_permute<3, 2, 1, 0>(__y))));
  1746. }
  1747. using _Up = conditional_t<is_floating_point_v<_Tp>, double, _LLong>;
  1748. const auto __y = __vector_bitcast<_Up>(__data(__x));
  1749. __x = __binary_op(__x, _M_make_simd<_Tp, _Np>(__vector_bitcast<_Tp>(
  1750. __vector_permute<1, 1>(__y))));
  1751. return __x[0];
  1752. } //}}}
  1753. else
  1754. {
  1755. static_assert(sizeof(__x) > __min_vector_size<_Tp>);
  1756. static_assert((_Np & (_Np - 1)) == 0); // _Np must be a power of 2
  1757. using _Ap = simd_abi::deduce_t<_Tp, _Np / 2>;
  1758. using _V = simd<_Tp, _Ap>;
  1759. return _Ap::_SimdImpl::_S_reduce(
  1760. __binary_op(_V(__private_init, __extract<0, 2>(__as_vector(__x))),
  1761. _V(__private_init,
  1762. __extract<1, 2>(__as_vector(__x)))),
  1763. static_cast<_BinaryOperation&&>(__binary_op));
  1764. }
  1765. }
  1766. // math {{{2
  1767. // frexp, modf and copysign implemented in simd_math.h
  1768. #define _GLIBCXX_SIMD_MATH_FALLBACK(__name) \
  1769. template <typename _Tp, typename... _More> \
  1770. static _Tp _S_##__name(const _Tp& __x, const _More&... __more) \
  1771. { \
  1772. return __generate_vector<_Tp>( \
  1773. [&](auto __i) { return __name(__x[__i], __more[__i]...); }); \
  1774. }
  1775. #define _GLIBCXX_SIMD_MATH_FALLBACK_MASKRET(__name) \
  1776. template <typename _Tp, typename... _More> \
  1777. static typename _Tp::mask_type _S_##__name(const _Tp& __x, \
  1778. const _More&... __more) \
  1779. { \
  1780. return __generate_vector<_Tp>( \
  1781. [&](auto __i) { return __name(__x[__i], __more[__i]...); }); \
  1782. }
  1783. #define _GLIBCXX_SIMD_MATH_FALLBACK_FIXEDRET(_RetTp, __name) \
  1784. template <typename _Tp, typename... _More> \
  1785. static auto _S_##__name(const _Tp& __x, const _More&... __more) \
  1786. { \
  1787. return __fixed_size_storage_t<_RetTp, \
  1788. _VectorTraits<_Tp>::_S_partial_width>:: \
  1789. _S_generate([&](auto __meta) constexpr { \
  1790. return __meta._S_generator( \
  1791. [&](auto __i) { \
  1792. return __name(__x[__meta._S_offset + __i], \
  1793. __more[__meta._S_offset + __i]...); \
  1794. }, \
  1795. static_cast<_RetTp*>(nullptr)); \
  1796. }); \
  1797. }
  1798. _GLIBCXX_SIMD_MATH_FALLBACK(acos)
  1799. _GLIBCXX_SIMD_MATH_FALLBACK(asin)
  1800. _GLIBCXX_SIMD_MATH_FALLBACK(atan)
  1801. _GLIBCXX_SIMD_MATH_FALLBACK(atan2)
  1802. _GLIBCXX_SIMD_MATH_FALLBACK(cos)
  1803. _GLIBCXX_SIMD_MATH_FALLBACK(sin)
  1804. _GLIBCXX_SIMD_MATH_FALLBACK(tan)
  1805. _GLIBCXX_SIMD_MATH_FALLBACK(acosh)
  1806. _GLIBCXX_SIMD_MATH_FALLBACK(asinh)
  1807. _GLIBCXX_SIMD_MATH_FALLBACK(atanh)
  1808. _GLIBCXX_SIMD_MATH_FALLBACK(cosh)
  1809. _GLIBCXX_SIMD_MATH_FALLBACK(sinh)
  1810. _GLIBCXX_SIMD_MATH_FALLBACK(tanh)
  1811. _GLIBCXX_SIMD_MATH_FALLBACK(exp)
  1812. _GLIBCXX_SIMD_MATH_FALLBACK(exp2)
  1813. _GLIBCXX_SIMD_MATH_FALLBACK(expm1)
  1814. _GLIBCXX_SIMD_MATH_FALLBACK(ldexp)
  1815. _GLIBCXX_SIMD_MATH_FALLBACK_FIXEDRET(int, ilogb)
  1816. _GLIBCXX_SIMD_MATH_FALLBACK(log)
  1817. _GLIBCXX_SIMD_MATH_FALLBACK(log10)
  1818. _GLIBCXX_SIMD_MATH_FALLBACK(log1p)
  1819. _GLIBCXX_SIMD_MATH_FALLBACK(log2)
  1820. _GLIBCXX_SIMD_MATH_FALLBACK(logb)
  1821. // modf implemented in simd_math.h
  1822. _GLIBCXX_SIMD_MATH_FALLBACK(scalbn)
  1823. _GLIBCXX_SIMD_MATH_FALLBACK(scalbln)
  1824. _GLIBCXX_SIMD_MATH_FALLBACK(cbrt)
  1825. _GLIBCXX_SIMD_MATH_FALLBACK(fabs)
  1826. _GLIBCXX_SIMD_MATH_FALLBACK(pow)
  1827. _GLIBCXX_SIMD_MATH_FALLBACK(sqrt)
  1828. _GLIBCXX_SIMD_MATH_FALLBACK(erf)
  1829. _GLIBCXX_SIMD_MATH_FALLBACK(erfc)
  1830. _GLIBCXX_SIMD_MATH_FALLBACK(lgamma)
  1831. _GLIBCXX_SIMD_MATH_FALLBACK(tgamma)
  1832. _GLIBCXX_SIMD_MATH_FALLBACK_FIXEDRET(long, lrint)
  1833. _GLIBCXX_SIMD_MATH_FALLBACK_FIXEDRET(long long, llrint)
  1834. _GLIBCXX_SIMD_MATH_FALLBACK_FIXEDRET(long, lround)
  1835. _GLIBCXX_SIMD_MATH_FALLBACK_FIXEDRET(long long, llround)
  1836. _GLIBCXX_SIMD_MATH_FALLBACK(fmod)
  1837. _GLIBCXX_SIMD_MATH_FALLBACK(remainder)
  1838. template <typename _Tp, typename _TVT = _VectorTraits<_Tp>>
  1839. static _Tp
  1840. _S_remquo(const _Tp __x, const _Tp __y,
  1841. __fixed_size_storage_t<int, _TVT::_S_partial_width>* __z)
  1842. {
  1843. return __generate_vector<_Tp>([&](auto __i) {
  1844. int __tmp;
  1845. auto __r = remquo(__x[__i], __y[__i], &__tmp);
  1846. __z->_M_set(__i, __tmp);
  1847. return __r;
  1848. });
  1849. }
  1850. // copysign in simd_math.h
  1851. _GLIBCXX_SIMD_MATH_FALLBACK(nextafter)
  1852. _GLIBCXX_SIMD_MATH_FALLBACK(fdim)
  1853. _GLIBCXX_SIMD_MATH_FALLBACK(fmax)
  1854. _GLIBCXX_SIMD_MATH_FALLBACK(fmin)
  1855. _GLIBCXX_SIMD_MATH_FALLBACK(fma)
  1856. template <typename _Tp, size_t _Np>
  1857. static constexpr _MaskMember<_Tp>
  1858. _S_isgreater(_SimdWrapper<_Tp, _Np> __x,
  1859. _SimdWrapper<_Tp, _Np> __y) noexcept
  1860. {
  1861. using _Ip = __int_for_sizeof_t<_Tp>;
  1862. const auto __xn = __vector_bitcast<_Ip>(__x);
  1863. const auto __yn = __vector_bitcast<_Ip>(__y);
  1864. const auto __xp = __xn < 0 ? -(__xn & __finite_max_v<_Ip>) : __xn;
  1865. const auto __yp = __yn < 0 ? -(__yn & __finite_max_v<_Ip>) : __yn;
  1866. return __andnot(_SuperImpl::_S_isunordered(__x, __y)._M_data,
  1867. __xp > __yp);
  1868. }
  1869. template <typename _Tp, size_t _Np>
  1870. static constexpr _MaskMember<_Tp>
  1871. _S_isgreaterequal(_SimdWrapper<_Tp, _Np> __x,
  1872. _SimdWrapper<_Tp, _Np> __y) noexcept
  1873. {
  1874. using _Ip = __int_for_sizeof_t<_Tp>;
  1875. const auto __xn = __vector_bitcast<_Ip>(__x);
  1876. const auto __yn = __vector_bitcast<_Ip>(__y);
  1877. const auto __xp = __xn < 0 ? -(__xn & __finite_max_v<_Ip>) : __xn;
  1878. const auto __yp = __yn < 0 ? -(__yn & __finite_max_v<_Ip>) : __yn;
  1879. return __andnot(_SuperImpl::_S_isunordered(__x, __y)._M_data,
  1880. __xp >= __yp);
  1881. }
  1882. template <typename _Tp, size_t _Np>
  1883. static constexpr _MaskMember<_Tp>
  1884. _S_isless(_SimdWrapper<_Tp, _Np> __x, _SimdWrapper<_Tp, _Np> __y) noexcept
  1885. {
  1886. using _Ip = __int_for_sizeof_t<_Tp>;
  1887. const auto __xn = __vector_bitcast<_Ip>(__x);
  1888. const auto __yn = __vector_bitcast<_Ip>(__y);
  1889. const auto __xp = __xn < 0 ? -(__xn & __finite_max_v<_Ip>) : __xn;
  1890. const auto __yp = __yn < 0 ? -(__yn & __finite_max_v<_Ip>) : __yn;
  1891. return __andnot(_SuperImpl::_S_isunordered(__x, __y)._M_data,
  1892. __xp < __yp);
  1893. }
  1894. template <typename _Tp, size_t _Np>
  1895. static constexpr _MaskMember<_Tp>
  1896. _S_islessequal(_SimdWrapper<_Tp, _Np> __x,
  1897. _SimdWrapper<_Tp, _Np> __y) noexcept
  1898. {
  1899. using _Ip = __int_for_sizeof_t<_Tp>;
  1900. const auto __xn = __vector_bitcast<_Ip>(__x);
  1901. const auto __yn = __vector_bitcast<_Ip>(__y);
  1902. const auto __xp = __xn < 0 ? -(__xn & __finite_max_v<_Ip>) : __xn;
  1903. const auto __yp = __yn < 0 ? -(__yn & __finite_max_v<_Ip>) : __yn;
  1904. return __andnot(_SuperImpl::_S_isunordered(__x, __y)._M_data,
  1905. __xp <= __yp);
  1906. }
  1907. template <typename _Tp, size_t _Np>
  1908. static constexpr _MaskMember<_Tp>
  1909. _S_islessgreater(_SimdWrapper<_Tp, _Np> __x,
  1910. _SimdWrapper<_Tp, _Np> __y) noexcept
  1911. {
  1912. return __andnot(_SuperImpl::_S_isunordered(__x, __y),
  1913. _SuperImpl::_S_not_equal_to(__x, __y));
  1914. }
  1915. #undef _GLIBCXX_SIMD_MATH_FALLBACK
  1916. #undef _GLIBCXX_SIMD_MATH_FALLBACK_MASKRET
  1917. #undef _GLIBCXX_SIMD_MATH_FALLBACK_FIXEDRET
  1918. // _S_abs {{{3
  1919. template <typename _Tp, size_t _Np>
  1920. _GLIBCXX_SIMD_INTRINSIC static _SimdWrapper<_Tp, _Np>
  1921. _S_abs(_SimdWrapper<_Tp, _Np> __x) noexcept
  1922. {
  1923. // if (__builtin_is_constant_evaluated())
  1924. // {
  1925. // return __x._M_data < 0 ? -__x._M_data : __x._M_data;
  1926. // }
  1927. if constexpr (is_floating_point_v<_Tp>)
  1928. // `v < 0 ? -v : v` cannot compile to the efficient implementation of
  1929. // masking the signbit off because it must consider v == -0
  1930. // ~(-0.) & v would be easy, but breaks with fno-signed-zeros
  1931. return __and(_S_absmask<__vector_type_t<_Tp, _Np>>, __x._M_data);
  1932. else
  1933. return __x._M_data < 0 ? -__x._M_data : __x._M_data;
  1934. }
  1935. // }}}3
  1936. // _S_plus_minus {{{
  1937. // Returns __x + __y - __y without -fassociative-math optimizing to __x.
  1938. // - _TV must be __vector_type_t<floating-point type, N>.
  1939. // - _UV must be _TV or floating-point type.
  1940. template <typename _TV, typename _UV>
  1941. _GLIBCXX_SIMD_INTRINSIC static constexpr _TV _S_plus_minus(_TV __x,
  1942. _UV __y) noexcept
  1943. {
  1944. #if defined __i386__ && !defined __SSE_MATH__
  1945. if constexpr (sizeof(__x) == 8)
  1946. { // operations on __x would use the FPU
  1947. static_assert(is_same_v<_TV, __vector_type_t<float, 2>>);
  1948. const auto __x4 = __vector_bitcast<float, 4>(__x);
  1949. if constexpr (is_same_v<_TV, _UV>)
  1950. return __vector_bitcast<float, 2>(
  1951. _S_plus_minus(__x4, __vector_bitcast<float, 4>(__y)));
  1952. else
  1953. return __vector_bitcast<float, 2>(_S_plus_minus(__x4, __y));
  1954. }
  1955. #endif
  1956. #if !defined __clang__ && __GCC_IEC_559 == 0
  1957. if (__builtin_is_constant_evaluated()
  1958. || (__builtin_constant_p(__x) && __builtin_constant_p(__y)))
  1959. return (__x + __y) - __y;
  1960. else
  1961. return [&] {
  1962. __x += __y;
  1963. if constexpr(__have_sse)
  1964. {
  1965. if constexpr (sizeof(__x) >= 16)
  1966. asm("" : "+x"(__x));
  1967. else if constexpr (is_same_v<__vector_type_t<float, 2>, _TV>)
  1968. asm("" : "+x"(__x[0]), "+x"(__x[1]));
  1969. else
  1970. __assert_unreachable<_TV>();
  1971. }
  1972. else if constexpr(__have_neon)
  1973. asm("" : "+w"(__x));
  1974. else if constexpr (__have_power_vmx)
  1975. {
  1976. if constexpr (is_same_v<__vector_type_t<float, 2>, _TV>)
  1977. asm("" : "+fgr"(__x[0]), "+fgr"(__x[1]));
  1978. else
  1979. asm("" : "+v"(__x));
  1980. }
  1981. else
  1982. asm("" : "+g"(__x));
  1983. return __x - __y;
  1984. }();
  1985. #else
  1986. return (__x + __y) - __y;
  1987. #endif
  1988. }
  1989. // }}}
  1990. // _S_nearbyint {{{3
  1991. template <typename _Tp, typename _TVT = _VectorTraits<_Tp>>
  1992. _GLIBCXX_SIMD_INTRINSIC static _Tp _S_nearbyint(_Tp __x_) noexcept
  1993. {
  1994. using value_type = typename _TVT::value_type;
  1995. using _V = typename _TVT::type;
  1996. const _V __x = __x_;
  1997. const _V __absx = __and(__x, _S_absmask<_V>);
  1998. static_assert(__CHAR_BIT__ * sizeof(1ull) >= __digits_v<value_type>);
  1999. _GLIBCXX_SIMD_USE_CONSTEXPR _V __shifter_abs
  2000. = _V() + (1ull << (__digits_v<value_type> - 1));
  2001. const _V __shifter = __or(__and(_S_signmask<_V>, __x), __shifter_abs);
  2002. const _V __shifted = _S_plus_minus(__x, __shifter);
  2003. return __absx < __shifter_abs ? __shifted : __x;
  2004. }
  2005. // _S_rint {{{3
  2006. template <typename _Tp, typename _TVT = _VectorTraits<_Tp>>
  2007. _GLIBCXX_SIMD_INTRINSIC static _Tp _S_rint(_Tp __x) noexcept
  2008. {
  2009. return _SuperImpl::_S_nearbyint(__x);
  2010. }
  2011. // _S_trunc {{{3
  2012. template <typename _Tp, size_t _Np>
  2013. _GLIBCXX_SIMD_INTRINSIC static _SimdWrapper<_Tp, _Np>
  2014. _S_trunc(_SimdWrapper<_Tp, _Np> __x)
  2015. {
  2016. using _V = __vector_type_t<_Tp, _Np>;
  2017. const _V __absx = __and(__x._M_data, _S_absmask<_V>);
  2018. static_assert(__CHAR_BIT__ * sizeof(1ull) >= __digits_v<_Tp>);
  2019. constexpr _Tp __shifter = 1ull << (__digits_v<_Tp> - 1);
  2020. _V __truncated = _S_plus_minus(__absx, __shifter);
  2021. __truncated -= __truncated > __absx ? _V() + 1 : _V();
  2022. return __absx < __shifter ? __or(__xor(__absx, __x._M_data), __truncated)
  2023. : __x._M_data;
  2024. }
  2025. // _S_round {{{3
  2026. template <typename _Tp, size_t _Np>
  2027. _GLIBCXX_SIMD_INTRINSIC static _SimdWrapper<_Tp, _Np>
  2028. _S_round(_SimdWrapper<_Tp, _Np> __x)
  2029. {
  2030. const auto __abs_x = _SuperImpl::_S_abs(__x);
  2031. const auto __t_abs = _SuperImpl::_S_trunc(__abs_x)._M_data;
  2032. const auto __r_abs // round(abs(x)) =
  2033. = __t_abs + (__abs_x._M_data - __t_abs >= _Tp(.5) ? _Tp(1) : 0);
  2034. return __or(__xor(__abs_x._M_data, __x._M_data), __r_abs);
  2035. }
  2036. // _S_floor {{{3
  2037. template <typename _Tp, size_t _Np>
  2038. _GLIBCXX_SIMD_INTRINSIC static _SimdWrapper<_Tp, _Np>
  2039. _S_floor(_SimdWrapper<_Tp, _Np> __x)
  2040. {
  2041. const auto __y = _SuperImpl::_S_trunc(__x)._M_data;
  2042. const auto __negative_input
  2043. = __vector_bitcast<_Tp>(__x._M_data < __vector_broadcast<_Np, _Tp>(0));
  2044. const auto __mask
  2045. = __andnot(__vector_bitcast<_Tp>(__y == __x._M_data), __negative_input);
  2046. return __or(__andnot(__mask, __y),
  2047. __and(__mask, __y - __vector_broadcast<_Np, _Tp>(1)));
  2048. }
  2049. // _S_ceil {{{3
  2050. template <typename _Tp, size_t _Np>
  2051. _GLIBCXX_SIMD_INTRINSIC static _SimdWrapper<_Tp, _Np>
  2052. _S_ceil(_SimdWrapper<_Tp, _Np> __x)
  2053. {
  2054. const auto __y = _SuperImpl::_S_trunc(__x)._M_data;
  2055. const auto __negative_input
  2056. = __vector_bitcast<_Tp>(__x._M_data < __vector_broadcast<_Np, _Tp>(0));
  2057. const auto __inv_mask
  2058. = __or(__vector_bitcast<_Tp>(__y == __x._M_data), __negative_input);
  2059. return __or(__and(__inv_mask, __y),
  2060. __andnot(__inv_mask, __y + __vector_broadcast<_Np, _Tp>(1)));
  2061. }
  2062. // _S_isnan {{{3
  2063. template <typename _Tp, size_t _Np>
  2064. _GLIBCXX_SIMD_INTRINSIC static _MaskMember<_Tp>
  2065. _S_isnan([[maybe_unused]] _SimdWrapper<_Tp, _Np> __x)
  2066. {
  2067. #if __FINITE_MATH_ONLY__
  2068. return {}; // false
  2069. #elif !defined __SUPPORT_SNAN__
  2070. return ~(__x._M_data == __x._M_data);
  2071. #elif defined __STDC_IEC_559__
  2072. using _Ip = __int_for_sizeof_t<_Tp>;
  2073. const auto __absn = __vector_bitcast<_Ip>(_SuperImpl::_S_abs(__x));
  2074. const auto __infn
  2075. = __vector_bitcast<_Ip>(__vector_broadcast<_Np>(__infinity_v<_Tp>));
  2076. return __infn < __absn;
  2077. #else
  2078. #error "Not implemented: how to support SNaN but non-IEC559 floating-point?"
  2079. #endif
  2080. }
  2081. // _S_isfinite {{{3
  2082. template <typename _Tp, size_t _Np>
  2083. _GLIBCXX_SIMD_INTRINSIC static _MaskMember<_Tp>
  2084. _S_isfinite([[maybe_unused]] _SimdWrapper<_Tp, _Np> __x)
  2085. {
  2086. #if __FINITE_MATH_ONLY__
  2087. using _UV = typename _MaskMember<_Tp>::_BuiltinType;
  2088. _GLIBCXX_SIMD_USE_CONSTEXPR _UV __alltrue = ~_UV();
  2089. return __alltrue;
  2090. #else
  2091. // if all exponent bits are set, __x is either inf or NaN
  2092. using _Ip = __int_for_sizeof_t<_Tp>;
  2093. const auto __absn = __vector_bitcast<_Ip>(_SuperImpl::_S_abs(__x));
  2094. const auto __maxn
  2095. = __vector_bitcast<_Ip>(__vector_broadcast<_Np>(__finite_max_v<_Tp>));
  2096. return __absn <= __maxn;
  2097. #endif
  2098. }
  2099. // _S_isunordered {{{3
  2100. template <typename _Tp, size_t _Np>
  2101. _GLIBCXX_SIMD_INTRINSIC static _MaskMember<_Tp>
  2102. _S_isunordered(_SimdWrapper<_Tp, _Np> __x, _SimdWrapper<_Tp, _Np> __y)
  2103. {
  2104. return __or(_S_isnan(__x), _S_isnan(__y));
  2105. }
  2106. // _S_signbit {{{3
  2107. template <typename _Tp, size_t _Np>
  2108. _GLIBCXX_SIMD_INTRINSIC static _MaskMember<_Tp>
  2109. _S_signbit(_SimdWrapper<_Tp, _Np> __x)
  2110. {
  2111. using _Ip = __int_for_sizeof_t<_Tp>;
  2112. return __vector_bitcast<_Ip>(__x) < 0;
  2113. // Arithmetic right shift (SRA) would also work (instead of compare), but
  2114. // 64-bit SRA isn't available on x86 before AVX512. And in general,
  2115. // compares are more likely to be efficient than SRA.
  2116. }
  2117. // _S_isinf {{{3
  2118. template <typename _Tp, size_t _Np>
  2119. _GLIBCXX_SIMD_INTRINSIC static _MaskMember<_Tp>
  2120. _S_isinf([[maybe_unused]] _SimdWrapper<_Tp, _Np> __x)
  2121. {
  2122. #if __FINITE_MATH_ONLY__
  2123. return {}; // false
  2124. #else
  2125. return _SuperImpl::template _S_equal_to<_Tp, _Np>(_SuperImpl::_S_abs(__x),
  2126. __vector_broadcast<_Np>(
  2127. __infinity_v<_Tp>));
  2128. // alternative:
  2129. // compare to inf using the corresponding integer type
  2130. /*
  2131. return
  2132. __vector_bitcast<_Tp>(__vector_bitcast<__int_for_sizeof_t<_Tp>>(
  2133. _S_abs(__x)._M_data)
  2134. ==
  2135. __vector_bitcast<__int_for_sizeof_t<_Tp>>(__vector_broadcast<_Np>(
  2136. __infinity_v<_Tp>)));
  2137. */
  2138. #endif
  2139. }
  2140. // _S_isnormal {{{3
  2141. template <typename _Tp, size_t _Np>
  2142. _GLIBCXX_SIMD_INTRINSIC static _MaskMember<_Tp>
  2143. _S_isnormal(_SimdWrapper<_Tp, _Np> __x)
  2144. {
  2145. using _Ip = __int_for_sizeof_t<_Tp>;
  2146. const auto __absn = __vector_bitcast<_Ip>(_SuperImpl::_S_abs(__x));
  2147. const auto __minn
  2148. = __vector_bitcast<_Ip>(__vector_broadcast<_Np>(__norm_min_v<_Tp>));
  2149. #if __FINITE_MATH_ONLY__
  2150. return __absn >= __minn;
  2151. #else
  2152. const auto __maxn
  2153. = __vector_bitcast<_Ip>(__vector_broadcast<_Np>(__finite_max_v<_Tp>));
  2154. return __minn <= __absn && __absn <= __maxn;
  2155. #endif
  2156. }
  2157. // _S_fpclassify {{{3
  2158. template <typename _Tp, size_t _Np>
  2159. _GLIBCXX_SIMD_INTRINSIC static __fixed_size_storage_t<int, _Np>
  2160. _S_fpclassify(_SimdWrapper<_Tp, _Np> __x)
  2161. {
  2162. using _I = __int_for_sizeof_t<_Tp>;
  2163. const auto __xn
  2164. = __vector_bitcast<_I>(__to_intrin(_SuperImpl::_S_abs(__x)));
  2165. constexpr size_t _NI = sizeof(__xn) / sizeof(_I);
  2166. _GLIBCXX_SIMD_USE_CONSTEXPR auto __minn
  2167. = __vector_bitcast<_I>(__vector_broadcast<_NI>(__norm_min_v<_Tp>));
  2168. _GLIBCXX_SIMD_USE_CONSTEXPR auto __infn
  2169. = __vector_bitcast<_I>(__vector_broadcast<_NI>(__infinity_v<_Tp>));
  2170. _GLIBCXX_SIMD_USE_CONSTEXPR auto __fp_normal
  2171. = __vector_broadcast<_NI, _I>(FP_NORMAL);
  2172. #if !__FINITE_MATH_ONLY__
  2173. _GLIBCXX_SIMD_USE_CONSTEXPR auto __fp_nan
  2174. = __vector_broadcast<_NI, _I>(FP_NAN);
  2175. _GLIBCXX_SIMD_USE_CONSTEXPR auto __fp_infinite
  2176. = __vector_broadcast<_NI, _I>(FP_INFINITE);
  2177. #endif
  2178. #ifndef __FAST_MATH__
  2179. _GLIBCXX_SIMD_USE_CONSTEXPR auto __fp_subnormal
  2180. = __vector_broadcast<_NI, _I>(FP_SUBNORMAL);
  2181. #endif
  2182. _GLIBCXX_SIMD_USE_CONSTEXPR auto __fp_zero
  2183. = __vector_broadcast<_NI, _I>(FP_ZERO);
  2184. __vector_type_t<_I, _NI>
  2185. __tmp = __xn < __minn
  2186. #ifdef __FAST_MATH__
  2187. ? __fp_zero
  2188. #else
  2189. ? (__xn == 0 ? __fp_zero : __fp_subnormal)
  2190. #endif
  2191. #if __FINITE_MATH_ONLY__
  2192. : __fp_normal;
  2193. #else
  2194. : (__xn < __infn ? __fp_normal
  2195. : (__xn == __infn ? __fp_infinite : __fp_nan));
  2196. #endif
  2197. if constexpr (sizeof(_I) == sizeof(int))
  2198. {
  2199. using _FixedInt = __fixed_size_storage_t<int, _Np>;
  2200. const auto __as_int = __vector_bitcast<int, _Np>(__tmp);
  2201. if constexpr (_FixedInt::_S_tuple_size == 1)
  2202. return {__as_int};
  2203. else if constexpr (_FixedInt::_S_tuple_size == 2
  2204. && is_same_v<
  2205. typename _FixedInt::_SecondType::_FirstAbi,
  2206. simd_abi::scalar>)
  2207. return {__extract<0, 2>(__as_int), __as_int[_Np - 1]};
  2208. else if constexpr (_FixedInt::_S_tuple_size == 2)
  2209. return {__extract<0, 2>(__as_int),
  2210. __auto_bitcast(__extract<1, 2>(__as_int))};
  2211. else
  2212. __assert_unreachable<_Tp>();
  2213. }
  2214. else if constexpr (_Np == 2 && sizeof(_I) == 8
  2215. && __fixed_size_storage_t<int, _Np>::_S_tuple_size == 2)
  2216. {
  2217. const auto __aslong = __vector_bitcast<_LLong>(__tmp);
  2218. return {int(__aslong[0]), {int(__aslong[1])}};
  2219. }
  2220. #if _GLIBCXX_SIMD_X86INTRIN
  2221. else if constexpr (sizeof(_Tp) == 8 && sizeof(__tmp) == 32
  2222. && __fixed_size_storage_t<int, _Np>::_S_tuple_size == 1)
  2223. return {_mm_packs_epi32(__to_intrin(__lo128(__tmp)),
  2224. __to_intrin(__hi128(__tmp)))};
  2225. else if constexpr (sizeof(_Tp) == 8 && sizeof(__tmp) == 64
  2226. && __fixed_size_storage_t<int, _Np>::_S_tuple_size == 1)
  2227. return {_mm512_cvtepi64_epi32(__to_intrin(__tmp))};
  2228. #endif // _GLIBCXX_SIMD_X86INTRIN
  2229. else if constexpr (__fixed_size_storage_t<int, _Np>::_S_tuple_size == 1)
  2230. return {__call_with_subscripts<_Np>(__vector_bitcast<_LLong>(__tmp),
  2231. [](auto... __l) {
  2232. return __make_wrapper<int>(__l...);
  2233. })};
  2234. else
  2235. __assert_unreachable<_Tp>();
  2236. }
  2237. // _S_increment & _S_decrement{{{2
  2238. template <typename _Tp, size_t _Np>
  2239. _GLIBCXX_SIMD_INTRINSIC static void
  2240. _S_increment(_SimdWrapper<_Tp, _Np>& __x)
  2241. { __x = __x._M_data + 1; }
  2242. template <typename _Tp, size_t _Np>
  2243. _GLIBCXX_SIMD_INTRINSIC static void
  2244. _S_decrement(_SimdWrapper<_Tp, _Np>& __x)
  2245. { __x = __x._M_data - 1; }
  2246. // smart_reference access {{{2
  2247. template <typename _Tp, size_t _Np, typename _Up>
  2248. _GLIBCXX_SIMD_INTRINSIC constexpr static void
  2249. _S_set(_SimdWrapper<_Tp, _Np>& __v, int __i, _Up&& __x) noexcept
  2250. { __v._M_set(__i, static_cast<_Up&&>(__x)); }
  2251. // _S_masked_assign{{{2
  2252. template <typename _Tp, typename _K, size_t _Np>
  2253. _GLIBCXX_SIMD_INTRINSIC static void
  2254. _S_masked_assign(_SimdWrapper<_K, _Np> __k, _SimdWrapper<_Tp, _Np>& __lhs,
  2255. __type_identity_t<_SimdWrapper<_Tp, _Np>> __rhs)
  2256. {
  2257. if (__k._M_is_constprop_none_of())
  2258. return;
  2259. else if (__k._M_is_constprop_all_of())
  2260. __lhs = __rhs;
  2261. else
  2262. __lhs = _CommonImpl::_S_blend(__k, __lhs, __rhs);
  2263. }
  2264. template <typename _Tp, typename _K, size_t _Np>
  2265. _GLIBCXX_SIMD_INTRINSIC static void
  2266. _S_masked_assign(_SimdWrapper<_K, _Np> __k, _SimdWrapper<_Tp, _Np>& __lhs,
  2267. __type_identity_t<_Tp> __rhs)
  2268. {
  2269. if (__k._M_is_constprop_none_of())
  2270. return;
  2271. else if (__k._M_is_constprop_all_of())
  2272. __lhs = __vector_broadcast<_Np>(__rhs);
  2273. else if (__builtin_constant_p(__rhs) && __rhs == 0)
  2274. {
  2275. if constexpr (!is_same_v<bool, _K>)
  2276. // the __andnot optimization only makes sense if __k._M_data is a
  2277. // vector register
  2278. __lhs._M_data
  2279. = __andnot(__vector_bitcast<_Tp>(__k), __lhs._M_data);
  2280. else
  2281. // for AVX512/__mmask, a _mm512_maskz_mov is best
  2282. __lhs
  2283. = _CommonImpl::_S_blend(__k, __lhs, _SimdWrapper<_Tp, _Np>());
  2284. }
  2285. else
  2286. __lhs = _CommonImpl::_S_blend(__k, __lhs,
  2287. _SimdWrapper<_Tp, _Np>(
  2288. __vector_broadcast<_Np>(__rhs)));
  2289. }
  2290. // _S_masked_cassign {{{2
  2291. template <typename _Op, typename _Tp, typename _K, size_t _Np>
  2292. _GLIBCXX_SIMD_INTRINSIC static void
  2293. _S_masked_cassign(const _SimdWrapper<_K, _Np> __k,
  2294. _SimdWrapper<_Tp, _Np>& __lhs,
  2295. const __type_identity_t<_SimdWrapper<_Tp, _Np>> __rhs,
  2296. _Op __op)
  2297. {
  2298. if (__k._M_is_constprop_none_of())
  2299. return;
  2300. else if (__k._M_is_constprop_all_of())
  2301. __lhs = __op(_SuperImpl{}, __lhs, __rhs);
  2302. else
  2303. __lhs = _CommonImpl::_S_blend(__k, __lhs,
  2304. __op(_SuperImpl{}, __lhs, __rhs));
  2305. }
  2306. template <typename _Op, typename _Tp, typename _K, size_t _Np>
  2307. _GLIBCXX_SIMD_INTRINSIC static void
  2308. _S_masked_cassign(const _SimdWrapper<_K, _Np> __k,
  2309. _SimdWrapper<_Tp, _Np>& __lhs,
  2310. const __type_identity_t<_Tp> __rhs, _Op __op)
  2311. { _S_masked_cassign(__k, __lhs, __vector_broadcast<_Np>(__rhs), __op); }
  2312. // _S_masked_unary {{{2
  2313. template <template <typename> class _Op, typename _Tp, typename _K,
  2314. size_t _Np>
  2315. _GLIBCXX_SIMD_INTRINSIC static _SimdWrapper<_Tp, _Np>
  2316. _S_masked_unary(const _SimdWrapper<_K, _Np> __k,
  2317. const _SimdWrapper<_Tp, _Np> __v)
  2318. {
  2319. if (__k._M_is_constprop_none_of())
  2320. return __v;
  2321. auto __vv = _M_make_simd(__v);
  2322. _Op<decltype(__vv)> __op;
  2323. if (__k._M_is_constprop_all_of())
  2324. return __data(__op(__vv));
  2325. else
  2326. return _CommonImpl::_S_blend(__k, __v, __data(__op(__vv)));
  2327. }
  2328. //}}}2
  2329. };
  2330. // _MaskImplBuiltinMixin {{{1
  2331. struct _MaskImplBuiltinMixin
  2332. {
  2333. template <typename _Tp>
  2334. using _TypeTag = _Tp*;
  2335. // _S_to_maskvector {{{
  2336. template <typename _Up, size_t _ToN = 1>
  2337. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Up, _ToN>
  2338. _S_to_maskvector(bool __x)
  2339. {
  2340. static_assert(is_same_v<_Up, __int_for_sizeof_t<_Up>>);
  2341. return __x ? __vector_type_t<_Up, _ToN>{~_Up()}
  2342. : __vector_type_t<_Up, _ToN>{};
  2343. }
  2344. template <typename _Up, size_t _UpN = 0, size_t _Np, bool _Sanitized,
  2345. size_t _ToN = _UpN == 0 ? _Np : _UpN>
  2346. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Up, _ToN>
  2347. _S_to_maskvector(_BitMask<_Np, _Sanitized> __x)
  2348. {
  2349. static_assert(is_same_v<_Up, __int_for_sizeof_t<_Up>>);
  2350. return __generate_vector<__vector_type_t<_Up, _ToN>>([&](
  2351. auto __i) constexpr {
  2352. if constexpr (__i < _Np)
  2353. return __x[__i] ? ~_Up() : _Up();
  2354. else
  2355. return _Up();
  2356. });
  2357. }
  2358. template <typename _Up, size_t _UpN = 0, typename _Tp, size_t _Np,
  2359. size_t _ToN = _UpN == 0 ? _Np : _UpN>
  2360. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Up, _ToN>
  2361. _S_to_maskvector(_SimdWrapper<_Tp, _Np> __x)
  2362. {
  2363. static_assert(is_same_v<_Up, __int_for_sizeof_t<_Up>>);
  2364. using _TW = _SimdWrapper<_Tp, _Np>;
  2365. using _UW = _SimdWrapper<_Up, _ToN>;
  2366. if constexpr (sizeof(_Up) == sizeof(_Tp) && sizeof(_TW) == sizeof(_UW))
  2367. return __wrapper_bitcast<_Up, _ToN>(__x);
  2368. else if constexpr (is_same_v<_Tp, bool>) // bits -> vector
  2369. return _S_to_maskvector<_Up, _ToN>(_BitMask<_Np>(__x._M_data));
  2370. else
  2371. { // vector -> vector
  2372. /*
  2373. [[maybe_unused]] const auto __y = __vector_bitcast<_Up>(__x._M_data);
  2374. if constexpr (sizeof(_Tp) == 8 && sizeof(_Up) == 4 && sizeof(__y) ==
  2375. 16) return __vector_permute<1, 3, -1, -1>(__y); else if constexpr
  2376. (sizeof(_Tp) == 4 && sizeof(_Up) == 2
  2377. && sizeof(__y) == 16)
  2378. return __vector_permute<1, 3, 5, 7, -1, -1, -1, -1>(__y);
  2379. else if constexpr (sizeof(_Tp) == 8 && sizeof(_Up) == 2
  2380. && sizeof(__y) == 16)
  2381. return __vector_permute<3, 7, -1, -1, -1, -1, -1, -1>(__y);
  2382. else if constexpr (sizeof(_Tp) == 2 && sizeof(_Up) == 1
  2383. && sizeof(__y) == 16)
  2384. return __vector_permute<1, 3, 5, 7, 9, 11, 13, 15, -1, -1, -1, -1,
  2385. -1, -1, -1, -1>(__y); else if constexpr (sizeof(_Tp) == 4 &&
  2386. sizeof(_Up) == 1
  2387. && sizeof(__y) == 16)
  2388. return __vector_permute<3, 7, 11, 15, -1, -1, -1, -1, -1, -1, -1,
  2389. -1, -1, -1, -1, -1>(__y); else if constexpr (sizeof(_Tp) == 8 &&
  2390. sizeof(_Up) == 1
  2391. && sizeof(__y) == 16)
  2392. return __vector_permute<7, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  2393. -1, -1, -1, -1, -1>(__y); else
  2394. */
  2395. {
  2396. return __generate_vector<__vector_type_t<_Up, _ToN>>([&](
  2397. auto __i) constexpr {
  2398. if constexpr (__i < _Np)
  2399. return _Up(__x[__i.value]);
  2400. else
  2401. return _Up();
  2402. });
  2403. }
  2404. }
  2405. }
  2406. // }}}
  2407. // _S_to_bits {{{
  2408. template <typename _Tp, size_t _Np>
  2409. _GLIBCXX_SIMD_INTRINSIC static constexpr _SanitizedBitMask<_Np>
  2410. _S_to_bits(_SimdWrapper<_Tp, _Np> __x)
  2411. {
  2412. static_assert(!is_same_v<_Tp, bool>);
  2413. static_assert(_Np <= __CHAR_BIT__ * sizeof(_ULLong));
  2414. using _Up = make_unsigned_t<__int_for_sizeof_t<_Tp>>;
  2415. const auto __bools
  2416. = __vector_bitcast<_Up>(__x) >> (sizeof(_Up) * __CHAR_BIT__ - 1);
  2417. _ULLong __r = 0;
  2418. __execute_n_times<_Np>(
  2419. [&](auto __i) { __r |= _ULLong(__bools[__i.value]) << __i; });
  2420. return __r;
  2421. }
  2422. // }}}
  2423. };
  2424. // _MaskImplBuiltin {{{1
  2425. template <typename _Abi>
  2426. struct _MaskImplBuiltin : _MaskImplBuiltinMixin
  2427. {
  2428. using _MaskImplBuiltinMixin::_S_to_bits;
  2429. using _MaskImplBuiltinMixin::_S_to_maskvector;
  2430. // member types {{{
  2431. template <typename _Tp>
  2432. using _SimdMember = typename _Abi::template __traits<_Tp>::_SimdMember;
  2433. template <typename _Tp>
  2434. using _MaskMember = typename _Abi::template _MaskMember<_Tp>;
  2435. using _SuperImpl = typename _Abi::_MaskImpl;
  2436. using _CommonImpl = typename _Abi::_CommonImpl;
  2437. template <typename _Tp>
  2438. static constexpr size_t _S_size = simd_size_v<_Tp, _Abi>;
  2439. // }}}
  2440. // _S_broadcast {{{
  2441. template <typename _Tp>
  2442. _GLIBCXX_SIMD_INTRINSIC static constexpr _MaskMember<_Tp>
  2443. _S_broadcast(bool __x)
  2444. {
  2445. return __x ? _Abi::template _S_implicit_mask<_Tp>()
  2446. : _MaskMember<_Tp>();
  2447. }
  2448. // }}}
  2449. // _S_load {{{
  2450. template <typename _Tp>
  2451. _GLIBCXX_SIMD_INTRINSIC static constexpr _MaskMember<_Tp>
  2452. _S_load(const bool* __mem)
  2453. {
  2454. using _I = __int_for_sizeof_t<_Tp>;
  2455. if constexpr (sizeof(_Tp) == sizeof(bool))
  2456. {
  2457. const auto __bools
  2458. = _CommonImpl::template _S_load<_I, _S_size<_Tp>>(__mem);
  2459. // bool is {0, 1}, everything else is UB
  2460. return __bools > 0;
  2461. }
  2462. else
  2463. return __generate_vector<_I, _S_size<_Tp>>([&](auto __i) constexpr {
  2464. return __mem[__i] ? ~_I() : _I();
  2465. });
  2466. }
  2467. // }}}
  2468. // _S_convert {{{
  2469. template <typename _Tp, size_t _Np, bool _Sanitized>
  2470. _GLIBCXX_SIMD_INTRINSIC static constexpr auto
  2471. _S_convert(_BitMask<_Np, _Sanitized> __x)
  2472. {
  2473. if constexpr (__is_builtin_bitmask_abi<_Abi>())
  2474. return _SimdWrapper<bool, simd_size_v<_Tp, _Abi>>(__x._M_to_bits());
  2475. else
  2476. return _SuperImpl::template _S_to_maskvector<__int_for_sizeof_t<_Tp>,
  2477. _S_size<_Tp>>(
  2478. __x._M_sanitized());
  2479. }
  2480. template <typename _Tp, size_t _Np>
  2481. _GLIBCXX_SIMD_INTRINSIC static constexpr auto
  2482. _S_convert(_SimdWrapper<bool, _Np> __x)
  2483. {
  2484. if constexpr (__is_builtin_bitmask_abi<_Abi>())
  2485. return _SimdWrapper<bool, simd_size_v<_Tp, _Abi>>(__x._M_data);
  2486. else
  2487. return _SuperImpl::template _S_to_maskvector<__int_for_sizeof_t<_Tp>,
  2488. _S_size<_Tp>>(
  2489. _BitMask<_Np>(__x._M_data)._M_sanitized());
  2490. }
  2491. template <typename _Tp, typename _Up, size_t _Np>
  2492. _GLIBCXX_SIMD_INTRINSIC static constexpr auto
  2493. _S_convert(_SimdWrapper<_Up, _Np> __x)
  2494. {
  2495. if constexpr (__is_builtin_bitmask_abi<_Abi>())
  2496. return _SimdWrapper<bool, simd_size_v<_Tp, _Abi>>(
  2497. _SuperImpl::_S_to_bits(__x));
  2498. else
  2499. return _SuperImpl::template _S_to_maskvector<__int_for_sizeof_t<_Tp>,
  2500. _S_size<_Tp>>(__x);
  2501. }
  2502. template <typename _Tp, typename _Up, typename _UAbi>
  2503. _GLIBCXX_SIMD_INTRINSIC static constexpr auto
  2504. _S_convert(simd_mask<_Up, _UAbi> __x)
  2505. {
  2506. if constexpr (__is_builtin_bitmask_abi<_Abi>())
  2507. {
  2508. using _R = _SimdWrapper<bool, simd_size_v<_Tp, _Abi>>;
  2509. if constexpr (__is_builtin_bitmask_abi<_UAbi>()) // bits -> bits
  2510. return _R(__data(__x));
  2511. else if constexpr (__is_scalar_abi<_UAbi>()) // bool -> bits
  2512. return _R(__data(__x));
  2513. else if constexpr (__is_fixed_size_abi_v<_UAbi>) // bitset -> bits
  2514. return _R(__data(__x)._M_to_bits());
  2515. else // vector -> bits
  2516. return _R(_UAbi::_MaskImpl::_S_to_bits(__data(__x))._M_to_bits());
  2517. }
  2518. else
  2519. return _SuperImpl::template _S_to_maskvector<__int_for_sizeof_t<_Tp>,
  2520. _S_size<_Tp>>(
  2521. __data(__x));
  2522. }
  2523. // }}}
  2524. // _S_masked_load {{{2
  2525. template <typename _Tp, size_t _Np>
  2526. static inline _SimdWrapper<_Tp, _Np>
  2527. _S_masked_load(_SimdWrapper<_Tp, _Np> __merge,
  2528. _SimdWrapper<_Tp, _Np> __mask, const bool* __mem) noexcept
  2529. {
  2530. // AVX(2) has 32/64 bit maskload, but nothing at 8 bit granularity
  2531. auto __tmp = __wrapper_bitcast<__int_for_sizeof_t<_Tp>>(__merge);
  2532. _BitOps::_S_bit_iteration(_SuperImpl::_S_to_bits(__mask),
  2533. [&](auto __i) {
  2534. __tmp._M_set(__i, -__mem[__i]);
  2535. });
  2536. __merge = __wrapper_bitcast<_Tp>(__tmp);
  2537. return __merge;
  2538. }
  2539. // _S_store {{{2
  2540. template <typename _Tp, size_t _Np>
  2541. _GLIBCXX_SIMD_INTRINSIC static void _S_store(_SimdWrapper<_Tp, _Np> __v,
  2542. bool* __mem) noexcept
  2543. {
  2544. __execute_n_times<_Np>([&](auto __i) constexpr {
  2545. __mem[__i] = __v[__i];
  2546. });
  2547. }
  2548. // _S_masked_store {{{2
  2549. template <typename _Tp, size_t _Np>
  2550. static inline void
  2551. _S_masked_store(const _SimdWrapper<_Tp, _Np> __v, bool* __mem,
  2552. const _SimdWrapper<_Tp, _Np> __k) noexcept
  2553. {
  2554. _BitOps::_S_bit_iteration(
  2555. _SuperImpl::_S_to_bits(__k), [&](auto __i) constexpr {
  2556. __mem[__i] = __v[__i];
  2557. });
  2558. }
  2559. // _S_from_bitmask{{{2
  2560. template <size_t _Np, typename _Tp>
  2561. _GLIBCXX_SIMD_INTRINSIC static _MaskMember<_Tp>
  2562. _S_from_bitmask(_SanitizedBitMask<_Np> __bits, _TypeTag<_Tp>)
  2563. {
  2564. return _SuperImpl::template _S_to_maskvector<_Tp, _S_size<_Tp>>(__bits);
  2565. }
  2566. // logical and bitwise operators {{{2
  2567. template <typename _Tp, size_t _Np>
  2568. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  2569. _S_logical_and(const _SimdWrapper<_Tp, _Np>& __x,
  2570. const _SimdWrapper<_Tp, _Np>& __y)
  2571. { return __and(__x._M_data, __y._M_data); }
  2572. template <typename _Tp, size_t _Np>
  2573. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  2574. _S_logical_or(const _SimdWrapper<_Tp, _Np>& __x,
  2575. const _SimdWrapper<_Tp, _Np>& __y)
  2576. { return __or(__x._M_data, __y._M_data); }
  2577. template <typename _Tp, size_t _Np>
  2578. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  2579. _S_bit_not(const _SimdWrapper<_Tp, _Np>& __x)
  2580. {
  2581. if constexpr (_Abi::template _S_is_partial<_Tp>)
  2582. return __andnot(__x, __wrapper_bitcast<_Tp>(
  2583. _Abi::template _S_implicit_mask<_Tp>()));
  2584. else
  2585. return __not(__x._M_data);
  2586. }
  2587. template <typename _Tp, size_t _Np>
  2588. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  2589. _S_bit_and(const _SimdWrapper<_Tp, _Np>& __x,
  2590. const _SimdWrapper<_Tp, _Np>& __y)
  2591. { return __and(__x._M_data, __y._M_data); }
  2592. template <typename _Tp, size_t _Np>
  2593. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  2594. _S_bit_or(const _SimdWrapper<_Tp, _Np>& __x,
  2595. const _SimdWrapper<_Tp, _Np>& __y)
  2596. { return __or(__x._M_data, __y._M_data); }
  2597. template <typename _Tp, size_t _Np>
  2598. _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
  2599. _S_bit_xor(const _SimdWrapper<_Tp, _Np>& __x,
  2600. const _SimdWrapper<_Tp, _Np>& __y)
  2601. { return __xor(__x._M_data, __y._M_data); }
  2602. // smart_reference access {{{2
  2603. template <typename _Tp, size_t _Np>
  2604. static constexpr void _S_set(_SimdWrapper<_Tp, _Np>& __k, int __i,
  2605. bool __x) noexcept
  2606. {
  2607. if constexpr (is_same_v<_Tp, bool>)
  2608. __k._M_set(__i, __x);
  2609. else
  2610. {
  2611. static_assert(is_same_v<_Tp, __int_for_sizeof_t<_Tp>>);
  2612. if (__builtin_is_constant_evaluated())
  2613. {
  2614. __k = __generate_from_n_evaluations<_Np,
  2615. __vector_type_t<_Tp, _Np>>(
  2616. [&](auto __j) {
  2617. if (__i == __j)
  2618. return _Tp(-__x);
  2619. else
  2620. return __k[+__j];
  2621. });
  2622. }
  2623. else
  2624. __k._M_data[__i] = -__x;
  2625. }
  2626. }
  2627. // _S_masked_assign{{{2
  2628. template <typename _Tp, size_t _Np>
  2629. _GLIBCXX_SIMD_INTRINSIC static void
  2630. _S_masked_assign(_SimdWrapper<_Tp, _Np> __k,
  2631. _SimdWrapper<_Tp, _Np>& __lhs,
  2632. __type_identity_t<_SimdWrapper<_Tp, _Np>> __rhs)
  2633. { __lhs = _CommonImpl::_S_blend(__k, __lhs, __rhs); }
  2634. template <typename _Tp, size_t _Np>
  2635. _GLIBCXX_SIMD_INTRINSIC static void
  2636. _S_masked_assign(_SimdWrapper<_Tp, _Np> __k,
  2637. _SimdWrapper<_Tp, _Np>& __lhs, bool __rhs)
  2638. {
  2639. if (__builtin_constant_p(__rhs))
  2640. {
  2641. if (__rhs == false)
  2642. __lhs = __andnot(__k, __lhs);
  2643. else
  2644. __lhs = __or(__k, __lhs);
  2645. return;
  2646. }
  2647. __lhs = _CommonImpl::_S_blend(__k, __lhs,
  2648. __data(simd_mask<_Tp, _Abi>(__rhs)));
  2649. }
  2650. //}}}2
  2651. // _S_all_of {{{
  2652. template <typename _Tp>
  2653. _GLIBCXX_SIMD_INTRINSIC static bool
  2654. _S_all_of(simd_mask<_Tp, _Abi> __k)
  2655. {
  2656. return __call_with_subscripts(
  2657. __data(__k), make_index_sequence<_S_size<_Tp>>(),
  2658. [](const auto... __ent) constexpr { return (... && !(__ent == 0)); });
  2659. }
  2660. // }}}
  2661. // _S_any_of {{{
  2662. template <typename _Tp>
  2663. _GLIBCXX_SIMD_INTRINSIC static bool
  2664. _S_any_of(simd_mask<_Tp, _Abi> __k)
  2665. {
  2666. return __call_with_subscripts(
  2667. __data(__k), make_index_sequence<_S_size<_Tp>>(),
  2668. [](const auto... __ent) constexpr { return (... || !(__ent == 0)); });
  2669. }
  2670. // }}}
  2671. // _S_none_of {{{
  2672. template <typename _Tp>
  2673. _GLIBCXX_SIMD_INTRINSIC static bool
  2674. _S_none_of(simd_mask<_Tp, _Abi> __k)
  2675. {
  2676. return __call_with_subscripts(
  2677. __data(__k), make_index_sequence<_S_size<_Tp>>(),
  2678. [](const auto... __ent) constexpr { return (... && (__ent == 0)); });
  2679. }
  2680. // }}}
  2681. // _S_some_of {{{
  2682. template <typename _Tp>
  2683. _GLIBCXX_SIMD_INTRINSIC static bool
  2684. _S_some_of(simd_mask<_Tp, _Abi> __k)
  2685. {
  2686. const int __n_true = _SuperImpl::_S_popcount(__k);
  2687. return __n_true > 0 && __n_true < int(_S_size<_Tp>);
  2688. }
  2689. // }}}
  2690. // _S_popcount {{{
  2691. template <typename _Tp>
  2692. _GLIBCXX_SIMD_INTRINSIC static int
  2693. _S_popcount(simd_mask<_Tp, _Abi> __k)
  2694. {
  2695. using _I = __int_for_sizeof_t<_Tp>;
  2696. if constexpr (is_default_constructible_v<simd<_I, _Abi>>)
  2697. return -reduce(
  2698. simd<_I, _Abi>(__private_init, __wrapper_bitcast<_I>(__data(__k))));
  2699. else
  2700. return -reduce(__bit_cast<rebind_simd_t<_I, simd<_Tp, _Abi>>>(
  2701. simd<_Tp, _Abi>(__private_init, __data(__k))));
  2702. }
  2703. // }}}
  2704. // _S_find_first_set {{{
  2705. template <typename _Tp>
  2706. _GLIBCXX_SIMD_INTRINSIC static int
  2707. _S_find_first_set(simd_mask<_Tp, _Abi> __k)
  2708. {
  2709. return std::__countr_zero(
  2710. _SuperImpl::_S_to_bits(__data(__k))._M_to_bits());
  2711. }
  2712. // }}}
  2713. // _S_find_last_set {{{
  2714. template <typename _Tp>
  2715. _GLIBCXX_SIMD_INTRINSIC static int
  2716. _S_find_last_set(simd_mask<_Tp, _Abi> __k)
  2717. {
  2718. return std::__bit_width(
  2719. _SuperImpl::_S_to_bits(__data(__k))._M_to_bits()) - 1;
  2720. }
  2721. // }}}
  2722. };
  2723. //}}}1
  2724. _GLIBCXX_SIMD_END_NAMESPACE
  2725. #endif // __cplusplus >= 201703L
  2726. #endif // _GLIBCXX_EXPERIMENTAL_SIMD_ABIS_H_
  2727. // vim: foldmethod=marker foldmarker={{{,}}} sw=2 noet ts=8 sts=2 tw=80