35#ifndef __ADS_SimdInline
36 #define __ADS_SimdInline
48 #include <hwy/highway.h>
55 #include <type_traits>
61HWY_BEFORE_NAMESPACE();
63namespace ADS::detail::simd::HWY_NAMESPACE
66namespace hn = hwy::HWY_NAMESPACE;
69using Bytes16 = hn::CappedTag< std::uint8_t , 16 >;
73template<
class D ,
class M >
74HWY_INLINE std::uint32_t maskBits( D d , M m )
76 std::uint8_t b[ 8 ] = {};
77 hn::StoreMaskBits( d , m , b );
78 return( std::uint32_t( b[ 0 ] ) | ( std::uint32_t( b[ 1 ] ) << 8 ) );
83HWY_INLINE std::uint32_t bytesEqual16(
const std::uint8_t * c ,
87 const std::size_t L = hn::Lanes( d );
89 for( std::size_t i = 0 ; i < 16 ; i += L )
90 m |= maskBits( d , hn::Eq( hn::Load( d , c + i ) , hn::Set( d , t ) ) ) << i;
97HWY_INLINE
void clearByte16( std::uint8_t * c , std::size_t j )
100 const std::size_t L = hn::Lanes( d );
101 for( std::size_t i = 0 ; i < 16 ; i += L ) {
102 const auto at = hn::Eq( hn::Iota( d , std::uint8_t( i ) ) ,
103 hn::Set( d , std::uint8_t( j ) ) );
104 hn::Store( hn::IfThenZeroElse( at , hn::Load( d , c + i ) ) , d , c + i );
111inline constexpr bool kLanesOf =
112 ( ( std::is_integral_v< T > && ! std::is_same_v< T , bool > ) ||
113 std::is_same_v< T , float > || std::is_same_v< T , double > ) &&
114 ( (
sizeof( T ) == 4 ) || (
sizeof( T ) == 8 ) );
118template< std::
size_t n ,
bool Up ,
class T >
119HWY_INLINE std::size_t countLess(
const T * a , T x )
121 using U = std::conditional_t<
122 std::is_floating_point_v< T > , T ,
124 std::is_signed_v< T > ,
125 std::conditional_t<
sizeof( T ) == 4 , std::int32_t , std::int64_t > ,
126 std::conditional_t<
sizeof( T ) == 4 , std::uint32_t ,
128 const hn::ScalableTag< U > d;
129 const std::size_t L = hn::Lanes( d );
130 const U * b =
reinterpret_cast< const U *
>( a );
131 const auto vx = hn::Set( d , U( x ) );
133 for( std::size_t k = 0 ; k < n / L ; ++k ) {
134 const auto v = hn::LoadU( d , b + k * L );
136 r += hn::CountTrue( d , hn::Not( hn::Lt( vx , v ) ) );
138 r += hn::CountTrue( d , hn::Lt( v , vx ) );
140 for( std::size_t i = n / L * L ; i < n ; ++i )
142 r += ! ( x < a[ i ] );
150template< std::
size_t n ,
class T >
151HWY_INLINE std::uint64_t greaterBits(
const T * a , T x )
153 using U = std::conditional_t<
154 std::is_floating_point_v< T > , T ,
156 std::is_signed_v< T > ,
157 std::conditional_t<
sizeof( T ) == 4 , std::int32_t , std::int64_t > ,
158 std::conditional_t<
sizeof( T ) == 4 , std::uint32_t ,
160 const hn::CappedTag< U , 64 > d;
161 const std::size_t L = hn::Lanes( d );
162 const U * b =
reinterpret_cast< const U *
>( a );
163 const auto vx = hn::Set( d , U( x ) );
165 for( std::size_t k = 0 ; k < n / L ; ++k ) {
166 const std::size_t i = k * L;
167 std::uint8_t c[ 8 ] = {};
168 hn::StoreMaskBits( d , hn::Lt( vx , hn::LoadU( d , b + i ) ) , c );
170 for(
int j = 7 ; j >= 0 ; --j )
171 m = ( m << 8 ) | c[ j ];
174 for( std::size_t i = n / L * L ; i < n ; ++i )
175 r |= std::uint64_t( x < a[ i ] ) << i;
181template< std::
size_t W >
182HWY_INLINE std::uint64_t bytesEqual(
const std::uint8_t * p , std::uint8_t b )
184 static_assert( W <= 64 ,
"the bits of a word" );
185 const hn::CappedTag< std::uint8_t , W > d;
186 const std::size_t L = hn::Lanes( d );
187 const auto vb = hn::Set( d , b );
189 for( std::size_t i = 0 ; i < W ; i += L ) {
190 std::uint8_t c[ 8 ] = {};
191 hn::StoreMaskBits( d , hn::Eq( hn::LoadU( d , p + i ) , vb ) , c );
193 for(
int j = 7 ; j >= 0 ; --j )
194 m = ( m << 8 ) | c[ j ];
202template< std::
size_t W >
203HWY_INLINE std::uint64_t bytesBelow(
const std::uint8_t * p , std::uint8_t b )
205 static_assert( W <= 64 ,
"the bits of a word" );
206 const hn::CappedTag< std::uint8_t , W > d;
207 const std::size_t L = hn::Lanes( d );
208 const auto vb = hn::Set( d , b );
210 for( std::size_t i = 0 ; i < W ; i += L ) {
211 std::uint8_t c[ 8 ] = {};
212 hn::StoreMaskBits( d , hn::Lt( hn::LoadU( d , p + i ) , vb ) , c );
214 for(
int j = 7 ; j >= 0 ; --j )
215 m = ( m << 8 ) | c[ j ];
223template< std::
size_t W >
224HWY_INLINE std::size_t bytesMismatch(
const std::uint8_t * p ,
225 const std::uint8_t * q )
227 const hn::CappedTag< std::uint8_t , W > d;
228 const std::size_t L = hn::Lanes( d );
229 for( std::size_t i = 0 ; i < W ; i += L ) {
230 const std::intptr_t f = hn::FindFirstTrue(
231 d , hn::Ne( hn::LoadU( d , p + i ) , hn::LoadU( d , q + i ) ) );
233 return( i + std::size_t( f ) );
241HWY_INLINE
void blockBits8( std::uint32_t h ,
const std::uint32_t * s ,
244 const hn::CappedTag< std::uint32_t , 8 > d;
245 const std::size_t L = hn::Lanes( d );
246 for( std::size_t i = 0 ; i < 8 ; i += L )
247 hn::StoreU( hn::Shl( hn::Set( d , 1u ) ,
248 hn::ShiftRight< 27 >(
249 hn::Mul( hn::Set( d , h ) ,
250 hn::LoadU( d , s + i ) ) ) ) ,
256HWY_INLINE
bool blockHas8(
const std::uint32_t * w ,
const std::uint32_t * m )
258 const hn::CappedTag< std::uint32_t , 8 > d;
259 const std::size_t L = hn::Lanes( d );
260 auto miss = hn::Zero( d );
261 for( std::size_t i = 0 ; i < 8 ; i += L )
264 hn::AndNot( hn::LoadU( d , w + i ) , hn::LoadU( d , m + i ) ) );
265 return( hn::AllTrue( d , hn::Eq( miss , hn::Zero( d ) ) ) );
269HWY_INLINE
void blockSet8( std::uint32_t * w ,
const std::uint32_t * m )
271 const hn::CappedTag< std::uint32_t , 8 > d;
272 const std::size_t L = hn::Lanes( d );
273 for( std::size_t i = 0 ; i < 8 ; i += L )
274 hn::StoreU( hn::Or( hn::LoadU( d , w + i ) , hn::LoadU( d , m + i ) ) , d ,
280HWY_INLINE std::uint32_t parities8(
const std::uint64_t * u ,
281 const std::uint64_t * v ,
285 const hn::CappedTag< std::uint64_t , 8 > d;
286 const std::size_t L = hn::Lanes( d );
287 const auto vl = hn::Set( d , lo ) , vh = hn::Set( d , hi );
288 const auto one = hn::Set( d , std::uint64_t( 1 ) );
290 for( std::size_t j = 0 ; j < 8 ; j += L ) {
291 const auto x = hn::Xor( hn::And( hn::LoadU( d , u + j ) , vl ) ,
292 hn::And( hn::LoadU( d , v + j ) , vh ) );
293 std::uint8_t c[ 8 ] = {};
295 d , hn::Eq( hn::And( hn::PopulationCount( x ) , one ) , one ) , c );
296 y |= std::uint32_t( c[ 0 ] ) << j;
302HWY_INLINE std::size_t lineRank512(
const std::uint64_t * w ,
unsigned p )
304 const hn::CappedTag< std::uint64_t , 8 > d;
305 const hn::RebindToSigned<
decltype( d ) > di;
306 const std::size_t L = hn::Lanes( d );
307 const auto ones = hn::Set( d , ~std::uint64_t( 0 ) );
308 const auto all = hn::Set( d , std::uint64_t( 64 ) );
309 auto acc = hn::Zero( d );
310 for( std::size_t j = 0 ; j < 8 ; j += L ) {
312 const auto c = hn::BitCast(
313 d , hn::Min( hn::Max( hn::Sub( hn::Set( di , std::int64_t( p ) ) ,
315 hn::Iota( di , std::int64_t( j ) ) ) ) ,
317 hn::Set( di , std::int64_t( 64 ) ) ) );
318 const auto low = hn::IfThenElse(
319 hn::Eq( c , all ) , ones ,
321 hn::Shl( ones , hn::And( c , hn::Set( d , std::uint64_t( 63 ) ) ) ) ) );
323 acc , hn::PopulationCount( hn::And( hn::LoadU( d , w + j ) , low ) ) );
325 return( std::size_t( hn::GetLane( hn::SumOfLanes( d , acc ) ) ) );
330HWY_INLINE
void shiftedWords8(
const std::uint64_t * w ,
unsigned o ,
333 const hn::CappedTag< std::uint64_t , 8 > d;
334 const std::size_t L = hn::Lanes( d );
335 for( std::size_t j = 0 ; j < 8 ; j += L ) {
336 const auto lo = hn::ShiftRightSame( hn::LoadU( d , w + j ) ,
int( o ) );
337 const auto hi = hn::ShiftLeftSame(
338 hn::ShiftLeft< 1 >( hn::LoadU( d , w + j + 1 ) ) ,
int( 63 - o ) );
339 hn::StoreU( hn::Or( lo , hi ) , d , x + j );
347inline constexpr bool kSelectInWord512 =
348 ( HWY_TARGET <= HWY_AVX3_DL ) &&
349 ( hn::MaxLanes( hn::CappedTag< std::uint8_t , 64 >() ) >= 64 );
356HWY_INLINE
int selectInWord512( std::uint64_t x ,
int k )
358 const hn::CappedTag< std::uint8_t , 64 > d;
360 for(
int j = 0 ; j < 8 ; ++j )
361 b[ j ] = std::uint8_t( x >> ( 8 * j ) );
362 const auto v = hn::Compress( hn::Iota( d , 0 ) , hn::LoadMaskBits( d , b ) );
363 return(
int( hn::GetLane( hn::TableLookupLanes(
364 v , hn::IndicesFromVec( d , hn::Set( d , std::uint8_t( k ) ) ) ) ) ) );
374template< std::
size_t K ,
bool Masked >
375HWY_INLINE
void myersLanes( std::uint64_t * pv , std::uint64_t * mv ,
376 const std::uint64_t * eq , std::uint64_t * cp ,
377 std::uint64_t * cn ,
const std::uint64_t * act )
379 const hn::CappedTag< std::uint64_t , K > d;
380 const std::size_t L = hn::Lanes( d );
381 for( std::size_t i = 0 ; i < K ; i += L ) {
382 const auto Pv = hn::LoadU( d , pv + i ) , Mv = hn::LoadU( d , mv + i ) ,
383 Cp = hn::LoadU( d , cp + i ) , Cn = hn::LoadU( d , cn + i ) ,
384 E = hn::LoadU( d , eq + i );
385 const auto Xv = hn::Or( E , Mv ) , Eq = hn::Or( E , Cn );
387 hn::Or( hn::Xor( hn::Add( hn::And( Eq , Pv ) , Pv ) , Pv ) , Eq );
388 const auto ph = hn::Or( Mv , hn::Not( hn::Or( Xh , Pv ) ) ) ,
389 mh = hn::And( Pv , Xh );
390 const auto Ph = hn::Or( hn::ShiftLeft< 1 >( ph ) , Cp ) ,
391 Mh = hn::Or( hn::ShiftLeft< 1 >( mh ) , Cn );
392 auto P = hn::Or( Mh , hn::Not( hn::Or( Xv , Ph ) ) ) ,
393 M = hn::And( Ph , Xv ) ,
394 op = hn::ShiftRight< 63 >( ph ) , on = hn::ShiftRight< 63 >( mh );
395 if constexpr( Masked ) {
396 const auto a = hn::LoadU( d , act + i );
397 P = hn::IfVecThenElse( a , P , Pv );
398 M = hn::IfVecThenElse( a , M , Mv );
399 op = hn::IfVecThenElse( a , op , Cp );
400 on = hn::IfVecThenElse( a , on , Cn );
402 hn::StoreU( P , d , pv + i );
403 hn::StoreU( M , d , mv + i );
404 hn::StoreU( op , d , cp + i );
405 hn::StoreU( on , d , cn + i );
412using LaneInt = std::conditional_t<
413 std::is_signed_v< T > ,
415 sizeof( T ) == 1 , std::int8_t ,
417 sizeof( T ) == 2 , std::int16_t ,
418 std::conditional_t<
sizeof( T ) == 4 , std::int32_t ,
421 sizeof( T ) == 1 , std::uint8_t ,
422 std::conditional_t<
sizeof( T ) == 2 , std::uint16_t ,
423 std::conditional_t<
sizeof( T ) == 4 , std::uint32_t ,
424 std::uint64_t > > > >;
429template<
class D ,
class F >
430HWY_INLINE std::uint64_t maskWord64( D d , F && m )
432 const std::size_t L = hn::Lanes( d );
434 for( std::size_t i = 0 ; i < 64 ; i += L ) {
435 std::uint8_t c[ 8 ] = {};
436 hn::StoreMaskBits( d , m( i ) , c );
438 for(
int j = 7 ; j >= 0 ; --j )
439 w = ( w << 8 ) | c[ j ];
450HWY_INLINE
void neighbors64(
const T * x , std::uint64_t * g ,
453 static_assert( std::is_integral_v< T > &&
454 ( (
sizeof( T ) == 1 ) || (
sizeof( T ) == 4 ) ) ,
455 "the integers of 1 or 4 bytes" );
456 using U = LaneInt< T >;
457 const U * a =
reinterpret_cast< const U *
>( x );
458 const hn::CappedTag< U , 64 > d;
459 *g = maskWord64( d , [ & ]( std::size_t i ) {
460 return( hn::Lt( hn::LoadU( d , a + i ) , hn::LoadU( d , a + i + 1 ) ) );
462 *e = maskWord64( d , [ & ]( std::size_t i ) {
463 return( hn::Eq( hn::LoadU( d , a + i ) , hn::LoadU( d , a + i + 1 ) ) );
470HWY_INLINE
void kinds64(
const std::uint8_t * x , std::uint64_t t ,
471 std::uint64_t b , std::uint16_t * o )
473 const hn::CappedTag< std::uint16_t , 64 > d;
474 const hn::Rebind< std::uint8_t ,
decltype( d ) > d8;
475 const std::size_t L = hn::Lanes( d );
476 const auto two = hn::Set( d , std::uint16_t( 2 ) ) ,
477 one = hn::Set( d , std::uint16_t( 1 ) );
479 auto bits = [ & ]( std::uint64_t w , std::size_t i ) {
481 for(
int j = 0 ; j < 8 ; ++j )
482 c[ j ] = std::uint8_t( ( w >> i ) >> ( 8 * j ) );
483 return( hn::LoadMaskBits( d , c ) );
485 for( std::size_t i = 0 ; i < 64 ; i += L ) {
486 const auto k = hn::Or( hn::IfThenElseZero( bits( t , i ) , two ) ,
487 hn::IfThenElseZero( bits( b , i ) , one ) );
488 const auto v = hn::PromoteTo( d , hn::LoadU( d8 , x + i ) );
489 hn::StoreU( hn::Or( hn::ShiftLeft< 2 >( v ) , k ) , d , o + i );
498HWY_INLINE
void compactLess( T * x , std::size_t & i , std::size_t to ,
501 using U = LaneInt< T >;
502 U * a =
reinterpret_cast< U *
>( x );
503 const hn::ScalableTag< U > d;
504 const std::size_t L = hn::Lanes( d );
505 const auto one = hn::Set( d , U( 1 ) );
506 for( ; i + L <= to ; i += L ) {
507 const auto v = hn::LoadU( d , a + i );
508 j += hn::CompressStore( hn::Sub( v , one ) , hn::Ne( v , hn::Zero( d ) ) ,
517HWY_INLINE std::uint64_t takeSigns64( T * x )
519 using U = LaneInt< T >;
520 static_assert( std::is_signed_v< U > ,
"the signed integers" );
521 U * a =
reinterpret_cast< U *
>( x );
522 const hn::CappedTag< U , 64 > d;
523 const auto keep = hn::Set( d , std::numeric_limits< U >::max() );
524 return( maskWord64( d , [ & ]( std::size_t i ) {
525 const auto v = hn::LoadU( d , a + i );
526 hn::StoreU( hn::And( v , keep ) , d , a + i );
527 return( hn::Lt( v , hn::Zero( d ) ) );
533HWY_INLINE
void shiftIn4( std::uint64_t * w , std::uint32_t b1 ,
534 std::uint32_t b2 , std::uint32_t b3 ,
537 const hn::CappedTag< std::uint64_t , 8 > d;
538 const std::size_t L = hn::Lanes( d );
539 const auto one = hn::Set( d , std::uint64_t( 1 ) );
540 for( std::size_t j = 0 ; j < 8 ; j += L ) {
541 const auto at = hn::Iota( d , std::uint64_t( j ) );
542 auto bit = [ & ]( std::uint32_t b ) {
543 return( hn::And( hn::Shr( hn::Set( d , std::uint64_t( b ) ) , at ) ,
546 const auto in = hn::Or(
547 hn::Or( hn::ShiftLeft< 3 >( bit( b1 ) ) ,
548 hn::ShiftLeft< 2 >( bit( b2 ) ) ) ,
549 hn::Or( hn::ShiftLeft< 1 >( bit( b3 ) ) , bit( b4 ) ) );
550 hn::StoreU( hn::Or( hn::ShiftLeft< 4 >( hn::LoadU( d , w + j ) ) , in ) , d ,
562HWY_INLINE std::uint32_t
563nibbleFilter32(
const std::uint8_t * t ,
564 const std::array< std::uint8_t , 16 > * lo ,
565 const std::array< std::uint8_t , 16 > * hi , std::size_t q ,
568 const hn::CappedTag< std::uint8_t , 32 > d;
569 if constexpr( hn::MaxLanes( hn::CappedTag< std::uint8_t , 32 >() ) >= 16 ) {
570 const std::size_t L = hn::Lanes( d );
571 const auto low = hn::Set( d , std::uint8_t( 0x0f ) );
573 for( std::size_t i = 0 ; i < 32 ; i += L ) {
574 auto acc = hn::Set( d , std::uint8_t( 0xff ) );
575 for( std::size_t r = 0 ; r < q ; ++r ) {
576 const auto v = hn::LoadU( d , t + i + r );
579 hn::And( hn::TableLookupBytes( hn::LoadDup128( d , lo[ r ].data() ) ,
580 hn::And( v , low ) ) ,
581 hn::TableLookupBytes( hn::LoadDup128( d ,
583 hn::ShiftRight< 4 >( v ) ) ) );
585 hn::StoreU( acc , d , C + i );
586 std::uint8_t c[ 4 ] = {};
587 hn::StoreMaskBits( d , hn::Ne( acc , hn::Zero( d ) ) , c );
589 ( std::uint32_t( c[ 0 ] ) | ( std::uint32_t( c[ 1 ] ) << 8 ) |
590 ( std::uint32_t( c[ 2 ] ) << 16 ) | ( std::uint32_t( c[ 3 ] ) << 24 ) )
597 for( std::size_t p = 0 ; p < 32 ; ++p ) {
599 for( std::size_t r = 0 ; r < q ; ++r )
600 c &= lo[ r ][ t[ p + r ] & 15 ] & hi[ r ][ t[ p + r ] >> 4 ];
601 C[ p ] = std::uint8_t( c );
602 m |= std::uint32_t( c != 0 ) << p;
610HWY_AFTER_NAMESPACE();
612namespace ADS::detail::simd
616namespace here = HWY_NAMESPACE;
The vector kernels of ADS on Google Highway, with ADS_WITH_HIGHWAY: the loops over arrays that a stru...