okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1//==---- math.hpp ---------------------------------*- C++ -*----------------==// 2// 3// Copyright (C) Intel Corporation 4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 5// See https://llvm.org/LICENSE.txt for license information. 6// 7//===----------------------------------------------------------------------===// 8 9#ifndef __DPCT_MATH_HPP__ 10#define __DPCT_MATH_HPP__ 11 12#include <climits> 13#include <limits> 14#include <sycl/sycl.hpp> 15#include <type_traits> 16 17#include "detail/math_detail.hpp" 18 19namespace dpct { 20 21/// Compute fast_length for variable-length array 22/// \param [in] a The array 23/// \param [in] len Length of the array 24/// \returns The computed fast_length 25inline float fast_length (const float * a ,int len ) { 26switch (len ) { 27case 1 : 28return a [0 ]; 29case 2 : 30return sycl::fast_length (sycl::float2 (a [0 ],a [1 ])); 31case 3 : 32return sycl::fast_length (sycl::float3 (a [0 ],a [1 ],a [2 ])); 33case 4 : 34return sycl::fast_length (sycl::float4 (a [0 ],a [1 ],a [2 ],a [3 ])); 35case 0 : 36return 0 ; 37default : 38float f = 0 ; 39for (int i = 0 ;i < len ;++ i ) 40f += a [i ]* a [i ]; 41return sycl::sqrt (f ); 42 } 43} 44 45/// Calculate the square root of the input array. 46/// \param [in] a The array pointer 47/// \param [in] len Length of the array 48/// \returns The square root 49template < typename T > inline T length (const T * a ,const int len ) { 50switch (len ) { 51case 1 : 52return a [0 ]; 53case 2 : 54return sycl::length (sycl::vec < T ,2 > (a [0 ],a [1 ])); 55case 3 : 56return sycl::length (sycl::vec < T ,3 > (a [0 ],a [1 ],a [2 ])); 57case 4 : 58return sycl::length (sycl::vec < T ,4 > (a [0 ],a [1 ],a [2 ],a [3 ])); 59default : 60T ret = 0 ; 61for (int i = 0 ;i < len ;++ i ) 62ret += a [i ]* a [i ]; 63return sycl::sqrt (ret ); 64 } 65} 66 67/// Returns min(max(val, min_val), max_val) 68/// \param [in] val The input value 69/// \param [in] min_val The minimum value 70/// \param [in] max_val The maximum value 71/// \returns the value between min_val and max_val 72template < typename T > inline T clamp (T val ,T min_val ,T max_val ) { 73return sycl::clamp (val ,min_val ,max_val ); 74} 75#ifdef SYCL_EXT_ONEAPI_BFLOAT16_MATH_FUNCTIONS 76template <> 77inline sycl::ext::oneapi::bfloat16 clamp (sycl::ext::oneapi::bfloat16 val , 78 sycl::ext::oneapi::bfloat16 min_val , 79 sycl::ext::oneapi::bfloat16 max_val ) { 80if (val < min_val ) 81return min_val ; 82if (val > max_val ) 83return max_val ; 84return val ; 85} 86template <> 87inline sycl::vec < sycl::ext::oneapi::bfloat16 ,2 > 88clamp (sycl::vec < sycl::ext::oneapi::bfloat16 ,2 > val , 89 sycl::vec < sycl::ext::oneapi::bfloat16 ,2 > min_val , 90 sycl::vec < sycl::ext::oneapi::bfloat16 ,2 > max_val ) { 91return {clamp (val [0 ],min_val [0 ],max_val [0 ]), 92clamp (val [1 ],min_val [1 ],max_val [1 ])}; 93} 94#endif 95template < typename T > 96inline sycl::marray < T ,2 > clamp (sycl::marray < T ,2 > val , 97 sycl::marray < T ,2 > min_val , 98 sycl::marray < T ,2 > max_val ) { 99return {clamp (val [0 ],min_val [0 ],max_val [0 ]), 100clamp (val [1 ],min_val [1 ],max_val [1 ])}; 101} 102 103/// Performs comparison. 104/// \param [in] a The first value 105/// \param [in] b The second value 106/// \param [in] binary_op functor that implements the binary operation 107/// \returns the comparison result 108template < typename T ,class BinaryOperation > 109inline std::enable_if_t < 110 std::is_same_v < std::invoke_result_t < BinaryOperation ,T ,T > ,bool > ,bool > 111compare (const T a ,const T b ,const BinaryOperation binary_op ) { 112return binary_op (a ,b ); 113} 114template < typename T > 115inline std::enable_if_t < 116 std::is_same_v < std::invoke_result_t < std::not_equal_to <> ,T ,T > ,bool > ,bool > 117compare (const T a ,const T b ,const std::not_equal_to <> binary_op ) { 118return !detail::isnan (a )&& !detail::isnan (b )&& binary_op (a ,b ); 119} 120 121/// Performs unordered comparison. 122/// \param [in] a The first value 123/// \param [in] b The second value 124/// \param [in] binary_op functor that implements the binary operation 125/// \returns the comparison result 126template < typename T ,class BinaryOperation > 127inline std::enable_if_t < 128 std::is_same_v < std::invoke_result_t < BinaryOperation ,T ,T > ,bool > ,bool > 129unordered_compare (const T a ,const T b ,const BinaryOperation binary_op ) { 130return detail::isnan (a )|| detail::isnan (b )|| binary_op (a ,b ); 131} 132 133/// Performs 2 element comparison and return true if both results are true. 134/// \param [in] a The first value 135/// \param [in] b The second value 136/// \param [in] binary_op functor that implements the binary operation 137/// \returns the comparison result 138template < typename T ,class BinaryOperation > 139inline std::enable_if_t < T ::size ()== 2 ,bool > 140compare_both (const T a ,const T b ,const BinaryOperation binary_op ) { 141return compare (a [0 ],b [0 ],binary_op )&& compare (a [1 ],b [1 ],binary_op ); 142} 143 144/// Performs 2 element unordered comparison and return true if both results are 145/// true. 146/// \param [in] a The first value 147/// \param [in] b The second value 148/// \param [in] binary_op functor that implements the binary operation 149/// \returns the comparison result 150template < typename T ,class BinaryOperation > 151inline std::enable_if_t < T ::size ()== 2 ,bool > 152unordered_compare_both (const T a ,const T b ,const BinaryOperation binary_op ) { 153return unordered_compare (a [0 ],b [0 ],binary_op )&& 154unordered_compare (a [1 ],b [1 ],binary_op ); 155} 156 157/// Performs 2 element comparison. 158/// \param [in] a The first value 159/// \param [in] b The second value 160/// \param [in] binary_op functor that implements the binary operation 161/// \returns the comparison result 162template < typename T ,class BinaryOperation > 163inline std::enable_if_t < T ::size ()== 2 ,T > 164compare (const T a ,const T b ,const BinaryOperation binary_op ) { 165return {compare (a [0 ],b [0 ],binary_op ),compare (a [1 ],b [1 ],binary_op )}; 166} 167 168/// Performs 2 elements comparison, compare result of each element is 0 (false) 169/// or 0xffff (true), returns an unsigned int by composing compare result of two 170/// elements. 171/// \param [in] a The first value 172/// \param [in] b The second value 173/// \param [in] binary_op functor that implements the binary operation 174/// \returns the comparison result 175template < typename T ,class BinaryOperation > 176inline unsigned compare_mask (const sycl::vec < T ,2 > a ,const sycl::vec < T ,2 > b , 177const BinaryOperation binary_op ) { 178return sycl::vec < short ,2 > (- compare (a [0 ],b [0 ],binary_op ), 179- compare (a [1 ],b [1 ],binary_op )) 180 .as < sycl::vec < unsigned ,1 >> (); 181} 182template < typename T ,class BinaryOperation > 183inline unsigned compare_mask (const sycl::marray < T ,2 > a , 184const sycl::marray < T ,2 > b , 185const BinaryOperation binary_op ) { 186return sycl::vec < short ,2 > (- compare (a [0 ],b [0 ],binary_op ), 187- compare (a [1 ],b [1 ],binary_op )) 188 .as < sycl::vec < unsigned ,1 >> (); 189} 190 191/// Performs 2 element unordered comparison. 192/// \param [in] a The first value 193/// \param [in] b The second value 194/// \param [in] binary_op functor that implements the binary operation 195/// \returns the comparison result 196template < typename T ,class BinaryOperation > 197inline std::enable_if_t < T ::size ()== 2 ,T > 198unordered_compare (const T a ,const T b ,const BinaryOperation binary_op ) { 199return {unordered_compare (a [0 ],b [0 ],binary_op ), 200unordered_compare (a [1 ],b [1 ],binary_op )}; 201} 202 203/// Performs 2 elements unordered comparison, compare result of each element is 204/// 0 (false) or 0xffff (true), returns an unsigned int by composing compare 205/// result of two elements. 206/// \param [in] a The first value 207/// \param [in] b The second value 208/// \param [in] binary_op functor that implements the binary operation 209/// \returns the comparison result 210template < typename T ,class BinaryOperation > 211inline unsigned unordered_compare_mask (const sycl::vec < T ,2 > a , 212const sycl::vec < T ,2 > b , 213const BinaryOperation binary_op ) { 214return sycl::vec < short ,2 > (- unordered_compare (a [0 ],b [0 ],binary_op ), 215- unordered_compare (a [1 ],b [1 ],binary_op )) 216 .as < sycl::vec < unsigned ,1 >> (); 217} 218template < typename T ,class BinaryOperation > 219inline unsigned unordered_compare_mask (const sycl::marray < T ,2 > a , 220const sycl::marray < T ,2 > b , 221const BinaryOperation binary_op ) { 222return sycl::vec < short ,2 > (- unordered_compare (a [0 ],b [0 ],binary_op ), 223- unordered_compare (a [1 ],b [1 ],binary_op )) 224 .as < sycl::vec < unsigned ,1 >> (); 225} 226 227/// Bitfield-extract. 228/// 229/// \tparam T The type of \param source value, must be an integer. 230/// \param source The source value to extracting. 231/// \param bit_start The position to start extracting. 232/// \param num_bits The number of bits to extracting. 233template < typename T > 234inline std::enable_if_t < std::is_unsigned_v < T > ,T > 235bfe (const T source ,const uint32_t bit_start ,const uint32_t num_bits ) { 236const T mask = (T {1 } <<num_bits )- 1 ; 237return (source >>bit_start )& mask ; 238} 239 240/// Bitfield-extract with boundary checking. 241/// 242/// Extract bit field from \param source and return the zero or sign-extended 243/// result. Source \param bit_start gives the bit field starting bit position, 244/// and source \param num_bits gives the bit field length in bits. 245/// 246/// The result is padded with the sign bit of the extracted field. If the start 247/// position is beyond the msb of the input, the result was filled with the 248/// replicated sign bit of the extracted field. 249/// 250/// \tparam T The type of \param source value, must be an integer. 251/// \param source The source value to extracting. 252/// \param bit_start The position to start extracting. 253/// \param num_bits The number of bits to extracting. 254template < typename T > 255inline std::enable_if_t < std::is_integral_v < T > ,T > 256bfe_safe (const T source ,const uint32_t bit_start ,const uint32_t num_bits ) { 257#if defined(__SYCL_DEVICE_ONLY__ )&& defined(__NVPTX__ ) 258if constexpr (std::is_same_v < T ,int8_t > || std::is_same_v < T ,int16_t > || 259 std::is_same_v < T ,int32_t > ) { 260int32_t res {}; 261 asmvolatile ("bfe.s32 %0, %1, %2, %3;" 262 :"=r" (res ) 263 :"r" ((int32_t )source ),"r" (bit_start ),"r" (num_bits )); 264return res ; 265 }else if constexpr (std::is_same_v < T ,uint8_t > || 266 std::is_same_v < T ,uint16_t > || 267 std::is_same_v < T ,uint32_t > ) { 268uint32_t res {}; 269 asmvolatile ("bfe.u32 %0, %1, %2, %3;" 270 :"=r" (res ) 271 :"r" ((uint32_t )source ),"r" (bit_start ),"r" (num_bits )); 272return res ; 273 }else if constexpr (std::is_same_v < T ,int64_t > ) { 274T res {}; 275 asmvolatile ("bfe.s64 %0, %1, %2, %3;" 276 :"=l" (res ) 277 :"l" (source ),"r" (bit_start ),"r" (num_bits )); 278return res ; 279 }else if constexpr (std::is_same_v < T ,uint64_t > ) { 280T res {}; 281 asmvolatile ("bfe.u64 %0, %1, %2, %3;" 282 :"=l" (res ) 283 :"l" (source ),"r" (bit_start ),"r" (num_bits )); 284return res ; 285 } 286#endif 287const uint32_t bit_width = CHAR_BIT * sizeof (T ); 288const uint32_t pos = (std::min )(bit_start ,bit_width ); 289const uint32_t len = (std::min )(pos + num_bits ,bit_width )- pos ; 290if constexpr (std::is_signed_v < T > ) { 291const T mask = (T {1 } <<len )- 1 ; 292 293// Find the sign-bit, the result is padded with the sign bit of the 294// extracted field. 295// 296// sign_bit = len == 0 ? 0 : source[min(pos + len - 1, bit_width - 1)] 297const uint32_t sign_bit_pos = (std::min )(pos + len - 1 ,bit_width - 1 ); 298const T sign_bit = len != 0 && ((source >>sign_bit_pos )& 1 ); 299const T sign_bit_padding = (- sign_bit & ~mask ); 300return ((source >>pos )& mask ) |sign_bit_padding ; 301 }else { 302return dpct::bfe (source ,pos ,len ); 303 } 304} 305 306/// Bitfield-insert. 307/// 308/// \tparam T The type of \param x and \param y , must be an unsigned integer. 309/// \param x The source of the bitfield. 310/// \param y The source where bitfield is inserted. 311/// \param bit_start The position to start insertion. 312/// \param num_bits The number of bits to insertion. 313template < typename T > 314inline std::enable_if_t < std::is_unsigned_v < T > ,T > 315bfi (const T x ,const T y ,const uint32_t bit_start ,const uint32_t num_bits ) { 316constexpr unsigned bit_width = CHAR_BIT * sizeof (T ); 317 318// if bit_start > bit_width || len == 0, should return y. 319const uint32_t ignore_bfi = bit_start > bit_width || num_bits == 0 ; 320T extract_bitfield_mask = (~(T {0 }) >> (bit_width - num_bits )) <<bit_start ; 321T clean_bitfield_mask = ~extract_bitfield_mask ; 322return (y & (- ignore_bfi |clean_bitfield_mask )) | 323 (~- ignore_bfi & ((x <<bit_start )& extract_bitfield_mask )); 324} 325 326/// Bitfield-insert with boundary checking. 327/// 328/// Align and insert a bit field from \param x into \param y . Source \param 329/// bit_start gives the starting bit position for the insertion, and source 330/// \param num_bits gives the bit field length in bits. 331/// 332/// \tparam T The type of \param x and \param y , must be an unsigned integer. 333/// \param x The source of the bitfield. 334/// \param y The source where bitfield is inserted. 335/// \param bit_start The position to start insertion. 336/// \param num_bits The number of bits to insertion. 337template < typename T > 338inline std::enable_if_t < std::is_unsigned_v < T > ,T > 339bfi_safe (const T x ,const T y ,const uint32_t bit_start , 340const uint32_t num_bits ) { 341#if defined(__SYCL_DEVICE_ONLY__ )&& defined(__NVPTX__ ) 342if constexpr (std::is_same_v < T ,uint8_t > || std::is_same_v < T ,uint16_t > || 343 std::is_same_v < T ,uint32_t > ) { 344uint32_t res {}; 345 asmvolatile ("bfi.b32 %0, %1, %2, %3, %4;" 346 :"=r" (res ) 347 :"r" ((uint32_t )x ),"r" ((uint32_t )y ),"r" (bit_start ), 348"r" (num_bits )); 349return res ; 350 }else if constexpr (std::is_same_v < T ,uint64_t > ) { 351uint64_t res {}; 352 asmvolatile ("bfi.b64 %0, %1, %2, %3, %4;" 353 :"=l" (res ) 354 :"l" (x ),"l" (y ),"r" (bit_start ),"r" (num_bits )); 355return res ; 356 } 357#endif 358constexpr unsigned bit_width = CHAR_BIT * sizeof (T ); 359const uint32_t pos = (std::min )(bit_start ,bit_width ); 360const uint32_t len = (std::min )(pos + num_bits ,bit_width )- pos ; 361return dpct::bfi (x ,y ,pos ,len ); 362} 363 364/// Determine whether 2 element value is NaN. 365/// \param [in] a The input value 366/// \returns the comparison result 367template < typename T > 368inline std::enable_if_t < T ::size ()== 2 ,T > isnan (const T a ) { 369return {detail::isnan (a [0 ]), detail::isnan (a [1 ])}; 370} 371 372/// Emulated function for __funnelshift_l 373inline unsigned int funnelshift_l (unsigned int low ,unsigned int high , 374unsigned int shift ) { 375return (sycl::upsample (high ,low ) << (shift & 31U )) >>32 ; 376} 377 378/// Emulated function for __funnelshift_lc 379inline unsigned int funnelshift_lc (unsigned int low ,unsigned int high , 380unsigned int shift ) { 381return (sycl::upsample (high ,low ) << sycl::min (shift ,32U )) >>32 ; 382} 383 384/// Emulated function for __funnelshift_r 385inline unsigned int funnelshift_r (unsigned int low ,unsigned int high , 386unsigned int shift ) { 387return (sycl::upsample (high ,low ) >> (shift & 31U ))& 0xFFFFFFFF ; 388} 389 390/// Emulated function for __funnelshift_rc 391inline unsigned int funnelshift_rc (unsigned int low ,unsigned int high , 392unsigned int shift ) { 393return (sycl::upsample (high ,low ) >> sycl::min (shift ,32U ))& 0xFFFFFFFF ; 394} 395 396/// cbrt function wrapper. 397template < typename T > inline T cbrt (T val ) {return sycl::cbrt ((T )val ); } 398 399template < typename T1 ,typename T2 > 400std::enable_if_t < std::is_integral_v < T1 > && std::is_integral_v < T2 > , 401 std::common_type_t < T1 ,T2 >> 402min (T1 a ,T2 b ) { 403using common_t = std::common_type_t < T1 ,T2 > ; 404return sycl::min (static_cast < common_t > (a ),static_cast < common_t > (b )); 405} 406template < typename T1 ,typename T2 > 407std::enable_if_t < std::is_floating_point_v < T1 > && std::is_floating_point_v < T2 > , 408 std::common_type_t < T1 ,T2 >> 409min (T1 a ,T2 b ) { 410using common_t = std::common_type_t < T1 ,T2 > ; 411return sycl::fmin (static_cast < common_t > (a ),static_cast < common_t > (b )); 412} 413template < typename T1 ,typename T2 > 414std::enable_if_t < std::is_integral_v < T1 > && std::is_integral_v < T2 > , 415 std::common_type_t < T1 ,T2 >> 416max (T1 a ,T2 b ) { 417using common_t = std::common_type_t < T1 ,T2 > ; 418return sycl::max (static_cast < common_t > (a ),static_cast < common_t > (b )); 419} 420template < typename T1 ,typename T2 > 421std::enable_if_t < std::is_floating_point_v < T1 > && std::is_floating_point_v < T2 > , 422 std::common_type_t < T1 ,T2 >> 423max (T1 a ,T2 b ) { 424using common_t = std::common_type_t < T1 ,T2 > ; 425return sycl::fmax (static_cast < common_t > (a ),static_cast < common_t > (b )); 426} 427 428// pow functions overload. 429inline float pow (const float a ,const int b ) {return sycl::pown (a ,b ); } 430inline double pow (const double a ,const int b ) {return sycl::pown (a ,b ); } 431inline float pow (const float a ,const float b ) {return sycl::pow (a ,b ); } 432inline double pow (const double a ,const double b ) {return sycl::pow (a ,b ); } 433template < typename T ,typename U > 434inline typename std::enable_if_t < std::is_floating_point_v < T > ,T > 435pow (const T a ,const U b ) { 436return sycl::pow (a ,static_cast < T > (b )); 437} 438template < typename T ,typename U > 439inline typename std::enable_if_t < !std::is_floating_point_v < T > ,double > 440pow (const T a ,const U b ) { 441return sycl::pow (static_cast < double > (a ),static_cast < double > (b )); 442} 443 444/// Performs relu saturation. 445/// \param [in] a The input value 446/// \returns the relu saturation result 447template < typename T > inline T relu (T a ) { 448T zero {}; 449if constexpr (detail::is_floating_point < T > ) 450return !detail::isnan (a )&& a < zero ?zero :a ; 451else 452return a < zero ?zero :a ; 453} 454template < typename T ,int N > 455inline sycl::vec < T ,N > relu (const sycl::vec < T ,N > a ) { 456 sycl::vec < T ,N > ret ; 457for (int i = 0 ;i < N ;++ i ) 458ret [i ]= relu (a [i ]); 459return ret ; 460} 461template < class T > inline sycl::marray < T ,2 > relu (const sycl::marray < T ,2 > a ) { 462return {relu (a [0 ]),relu (a [1 ])}; 463} 464 465/// Performs complex number multiply addition. 466/// \param [in] a The first value 467/// \param [in] b The second value 468/// \param [in] c The third value 469/// \returns the operation result 470template < typename T > 471inline sycl::vec < T ,2 > complex_mul_add (const sycl::vec < T ,2 > a , 472const sycl::vec < T ,2 > b , 473const sycl::vec < T ,2 > c ) { 474return sycl::vec < T ,2 > {a [0 ]* b [0 ]- a [1 ]* b [1 ]+ c [0 ], 475a [0 ]* b [1 ]+ a [1 ]* b [0 ]+ c [1 ]}; 476} 477template < typename T > 478inline sycl::marray < T ,2 > complex_mul_add (const sycl::marray < T ,2 > a , 479const sycl::marray < T ,2 > b , 480const sycl::marray < T ,2 > c ) { 481return sycl::marray < T ,2 > {a [0 ]* b [0 ]- a [1 ]* b [1 ]+ c [0 ], 482a [0 ]* b [1 ]+ a [1 ]* b [0 ]+ c [1 ]}; 483} 484 485/// Performs 2 elements comparison and returns the bigger one. If either of 486/// inputs is NaN, then return NaN. 487/// \param [in] a The first value 488/// \param [in] b The second value 489/// \returns the bigger value 490template < typename T > inline T fmax_nan (const T a ,const T b ) { 491if (detail::isnan (a )|| detail::isnan (b )) 492return NAN ; 493return sycl::fmax (a ,b ); 494} 495#ifdef SYCL_EXT_ONEAPI_BFLOAT16_MATH_FUNCTIONS 496template <> 497inline sycl::ext::oneapi::bfloat16 498fmax_nan (const sycl::ext::oneapi::bfloat16 a , 499const sycl::ext::oneapi::bfloat16 b ) { 500if (detail::isnan (a )|| detail::isnan (b )) 501return NAN ; 502return sycl::fmax (float (a ),float (b )); 503} 504#endif 505template < typename T > 506inline sycl::vec < T ,2 > fmax_nan (const sycl::vec < T ,2 > a , 507const sycl::vec < T ,2 > b ) { 508return {fmax_nan (a [0 ],b [0 ]),fmax_nan (a [1 ],b [1 ])}; 509} 510template < typename T > 511inline sycl::marray < T ,2 > fmax_nan (const sycl::marray < T ,2 > a , 512const sycl::marray < T ,2 > b ) { 513return {fmax_nan (a [0 ],b [0 ]),fmax_nan (a [1 ],b [1 ])}; 514} 515 516/// Performs 2 elements comparison and returns the smaller one. If either of 517/// inputs is NaN, then return NaN. 518/// \param [in] a The first value 519/// \param [in] b The second value 520/// \returns the smaller value 521template < typename T > inline T fmin_nan (const T a ,const T b ) { 522if (detail::isnan (a )|| detail::isnan (b )) 523return NAN ; 524return sycl::fmin (a ,b ); 525} 526#ifdef SYCL_EXT_ONEAPI_BFLOAT16_MATH_FUNCTIONS 527template <> 528inline sycl::ext::oneapi::bfloat16 529fmin_nan (const sycl::ext::oneapi::bfloat16 a , 530const sycl::ext::oneapi::bfloat16 b ) { 531if (detail::isnan (a )|| detail::isnan (b )) 532return NAN ; 533return sycl::fmin (float (a ),float (b )); 534} 535#endif 536template < typename T > 537inline sycl::vec < T ,2 > fmin_nan (const sycl::vec < T ,2 > a , 538const sycl::vec < T ,2 > b ) { 539return {fmin_nan (a [0 ],b [0 ]),fmin_nan (a [1 ],b [1 ])}; 540} 541template < typename T > 542inline sycl::marray < T ,2 > fmin_nan (const sycl::marray < T ,2 > a , 543const sycl::marray < T ,2 > b ) { 544return {fmin_nan (a [0 ],b [0 ]),fmin_nan (a [1 ],b [1 ])}; 545} 546 547/// A sycl::abs wrapper functors. 548struct abs { 549template < typename T > auto operator()(const T x )const { 550return sycl::abs (x ); 551 } 552}; 553 554/// A sycl::abs_diff wrapper functors. 555struct abs_diff { 556template < typename T > auto operator()(const T x ,const T y )const { 557return sycl::abs_diff (x ,y ); 558 } 559}; 560 561/// A sycl::add_sat wrapper functors. 562struct add_sat { 563template < typename T > auto operator()(const T x ,const T y )const { 564return sycl::add_sat (x ,y ); 565 } 566}; 567 568/// A sycl::rhadd wrapper functors. 569struct rhadd { 570template < typename T > auto operator()(const T x ,const T y )const { 571return sycl::rhadd (x ,y ); 572 } 573}; 574 575/// A sycl::hadd wrapper functors. 576struct hadd { 577template < typename T > auto operator()(const T x ,const T y )const { 578return sycl::hadd (x ,y ); 579 } 580}; 581 582/// A sycl::max wrapper functors. 583struct maximum { 584template < typename T > auto operator()(const T x ,const T y )const { 585return sycl::max (x ,y ); 586 } 587template < typename T > 588auto operator()(const T x ,const T y ,bool * pred )const { 589return (x >=y ) ? ((* pred = true),x ) : ((* pred = false),y ); 590 } 591}; 592 593/// A sycl::min wrapper functors. 594struct minimum { 595template < typename T > auto operator()(const T x ,const T y )const { 596return sycl::min (x ,y ); 597 } 598template < typename T > 599auto operator()(const T x ,const T y ,bool * pred )const { 600return (x <=y ) ? ((* pred = true),x ) : ((* pred = false),y ); 601 } 602}; 603 604/// A sycl::sub_sat wrapper functors. 605struct sub_sat { 606template < typename T > auto operator()(const T x ,const T y )const { 607return sycl::sub_sat (x ,y ); 608 } 609}; 610 611/// Compute vectorized binary operation value for two values, with each value 612/// treated as a vector type \p VecT. 613/// \tparam [in] VecT The type of the vector 614/// \tparam [in] BinaryOperation The binary operation class 615/// \param [in] a The first value 616/// \param [in] b The second value 617/// \param [in] binary_op The operation to do with the two values 618/// \param [in] need_relu Whether the result need relu saturation 619/// \returns The vectorized binary operation value of the two values 620template < typename VecT ,class BinaryOperation > 621inline unsigned vectorized_binary (unsigned a ,unsigned b , 622const BinaryOperation binary_op , 623bool need_relu = false) { 624 sycl::vec < unsigned ,1 > v0 {a },v1 {b }; 625auto v2 = v0 .as < VecT > (); 626auto v3 = v1 .as < VecT > (); 627auto v4 = 628 detail::vectorized_binary < VecT ,BinaryOperation > ()(v2 ,v3 ,binary_op ); 629if (need_relu ) 630v4 = relu (v4 ); 631v0 = v4 .template as < sycl::vec < unsigned ,1 >> (); 632return v0 ; 633} 634 635/// Compute vectorized binary operation value with pred for two values, with 636/// each value treated as a 2 \p T type elements vector type. 637/// 638/// \tparam [in] VecT The type of the vector 639/// \tparam [in] BinaryOperation The binary operation class 640/// \param [in] a The first value 641/// \param [in] b The second value 642/// \param [in] binary_op The operation with pred to do with the two values 643/// \param [out] pred_hi The pred pointer that pass into high halfword operation 644/// \param [out] pred_lo The pred pointer that pass into low halfword operation 645/// \returns The vectorized binary operation value of the two values 646template < typename VecT ,typename BinaryOperation > 647inline unsigned vectorized_binary_with_pred (unsigned a ,unsigned b , 648const BinaryOperation binary_op , 649bool * pred_hi ,bool * pred_lo ) { 650auto v1 = sycl::vec < unsigned ,1 > (a ).as < VecT > (); 651auto v2 = sycl::vec < unsigned ,1 > (b ).as < VecT > (); 652VecT ret ; 653ret [0 ]= binary_op (v1 [0 ],v2 [0 ],pred_lo ); 654ret [1 ]= binary_op (v1 [1 ],v2 [1 ],pred_hi ); 655return ret .template as < sycl::vec < unsigned ,1 >> (); 656} 657 658/// Compute vectorized isgreater for two values, with each value treated as a 659/// vector type \p S. 660/// \tparam [in] S The type of the vector 661/// \tparam [in] T The type of the original values 662/// \param [in] a The first value 663/// \param [in] b The second value 664/// \returns The vectorized greater than of the two values 665template < typename S ,typename T > inline T vectorized_isgreater (T a ,T b ) { 666 sycl::vec < T ,1 > v0 {a },v1 {b }; 667auto v2 = v0 .template as < S > (); 668auto v3 = v1 .template as < S > (); 669auto v4 = v2 > v3 ; 670v0 = v4 .template as < sycl::vec < T ,1 >> (); 671return v0 ; 672} 673 674/// Compute vectorized max for two values, with each value treated as a vector 675/// type \p S. 676/// \tparam [in] S The type of the vector 677/// \tparam [in] T The type of the original values 678/// \param [in] a The first value 679/// \param [in] b The second value 680/// \returns The vectorized max of the two values 681template < typename S ,typename T > inline T vectorized_max (T a ,T b ) { 682 sycl::vec < T ,1 > v0 {a },v1 {b }; 683auto v2 = v0 .template as < S > (); 684auto v3 = v1 .template as < S > (); 685auto v4 = sycl::max (v2 ,v3 ); 686v0 = v4 .template as < sycl::vec < T ,1 >> (); 687return v0 ; 688} 689 690/// Compute vectorized min for two values, with each value treated as a vector 691/// type \p S. 692/// \tparam [in] S The type of the vector 693/// \tparam [in] T The type of the original values 694/// \param [in] a The first value 695/// \param [in] b The second value 696/// \returns The vectorized min of the two values 697template < typename S ,typename T > inline T vectorized_min (T a ,T b ) { 698 sycl::vec < T ,1 > v0 {a },v1 {b }; 699auto v2 = v0 .template as < S > (); 700auto v3 = v1 .template as < S > (); 701auto v4 = sycl::min (v2 ,v3 ); 702v0 = v4 .template as < sycl::vec < T ,1 >> (); 703return v0 ; 704} 705 706/// Compute vectorized unary operation for a value, with the value treated as a 707/// vector type \p VecT. 708/// \tparam [in] VecT The type of the vector 709/// \tparam [in] UnaryOperation The unary operation class 710/// \param [in] a The input value 711/// \returns The vectorized unary operation value of the input value 712template < typename VecT ,class UnaryOperation > 713inline unsigned vectorized_unary (unsigned a ,const UnaryOperation unary_op ) { 714 sycl::vec < unsigned ,1 > v0 {a }; 715auto v1 = v0 .as < VecT > (); 716auto v2 = unary_op (v1 ); 717v0 = v2 .template as < sycl::vec < unsigned ,1 >> (); 718return v0 ; 719} 720 721/// Compute vectorized absolute difference for two values without modulo 722/// overflow, with each value treated as a vector type \p VecT. 723/// \tparam [in] VecT The type of the vector 724/// \param [in] a The first value 725/// \param [in] b The second value 726/// \returns The vectorized absolute difference of the two values 727template < typename VecT > 728inline unsigned vectorized_sum_abs_diff (unsigned a ,unsigned b ) { 729 sycl::vec < unsigned ,1 > v0 {a },v1 {b }; 730// Need convert element type to wider signed type to avoid overflow. 731auto v2 = v0 .as < VecT > ().template convert < int > (); 732auto v3 = v1 .as < VecT > ().template convert < int > (); 733auto v4 = sycl::abs_diff (v2 ,v3 ); 734unsigned sum = 0 ; 735for (size_t i = 0 ;i < v4 .size ();++ i ) { 736sum += v4 [i ]; 737 } 738return sum ; 739} 740 741/// Compute two vectorized binary operation value with pred for three values, 742/// with each value treated as a 2 \p T type elements vector type. 743/// 744/// \tparam [in] VecT The type of the vector 745/// \tparam [in] BinaryOperation1 The first binary operation class 746/// \tparam [in] BinaryOperation2 The second binary operation class 747/// \param [in] a The first value 748/// \param [in] b The second value 749/// \param [in] c The third value 750/// \param [in] binary_op1 The first operation to do with the first two values 751/// \param [in] binary_op2 The second operation to do with the third values 752/// \param [in] need_relu Whether the result need relu saturation 753/// \returns The two vectorized binary operation value of the three values 754template < typename VecT ,typename BinaryOperation1 ,typename BinaryOperation2 > 755inline unsigned vectorized_ternary (unsigned a ,unsigned b ,unsigned c , 756const BinaryOperation1 binary_op1 , 757const BinaryOperation2 binary_op2 , 758bool need_relu = false) { 759const auto v1 = sycl::vec < unsigned ,1 > (a ).as < VecT > (); 760const auto v2 = sycl::vec < unsigned ,1 > (b ).as < VecT > (); 761const auto v3 = sycl::vec < unsigned ,1 > (c ).as < VecT > (); 762auto v4 = 763 detail::vectorized_binary < VecT ,BinaryOperation1 > ()(v1 ,v2 ,binary_op1 ); 764v4 = detail::vectorized_binary < VecT ,BinaryOperation2 > ()(v4 ,v3 ,binary_op2 ); 765if (need_relu ) 766v4 = relu (v4 ); 767return v4 .template as < sycl::vec < unsigned ,1 >> (); 768} 769 770/// Two-way dot product-accumulate. Calculate and return interger_vector2( 771/// \param a) dot product interger_vector2(low16_bit( \param b)) + \param c 772/// 773/// \tparam [in] T1 The type of first value. 774/// \tparam [in] T2 The type of second value. 775/// \param [in] a The first value. 776/// \param [in] b The second value. 777/// \param [in] c The third value. It has type uint32_t if both T1 and T1 are 778/// uint32_t else has type int32_t. 779/// \return Two-way 16-bit to 8-bit dot product which is accumulated in 32-bit 780/// result. 781template < typename T1 ,typename T2 ,typename T3 > 782inline auto dp2a_lo (T1 a ,T2 b ,T3 c ) { 783 detail::dot_product_acc_t < T1 ,T2 > res = c ; 784#if defined(__SYCL_DEVICE_ONLY__ )&& defined(__NVPTX__ )&& \ 785 defined(__CUDA_ARCH__ )&& __CUDA_ARCH__ >=610 786res = __dp2a_lo (a ,b ,c ); 787#else 788auto va = ::dpct::detail::extract_and_sign_or_zero_extend2 (a ); 789auto vb = ::dpct::detail::extract_and_sign_or_zero_extend4 (b ); 790res += va [0 ]* vb [0 ]; 791res += va [1 ]* vb [1 ]; 792#endif 793return res ; 794} 795 796/// Two-way dot product-accumulate. Calculate and return interger_vector2( 797/// \param a) dot product interger_vector2(high_16bit( \param b)) + \param c 798/// 799/// \tparam [in] T1 The type of first value. 800/// \tparam [in] T2 The type of second value. 801/// \param [in] a The first value. 802/// \param [in] b The second value. 803/// \param [in] c The third value. It has type uint32_t if both T1 and T1 are 804/// uint32_t else has type int32_t. 805/// \return Two-way 16-bit to 8-bit dot product which is accumulated in 32-bit 806/// result. 807template < typename T1 ,typename T2 ,typename T3 > 808inline auto dp2a_hi (T1 a ,T2 b ,T3 c ) { 809 detail::dot_product_acc_t < T1 ,T2 > res = c ; 810#if defined(__SYCL_DEVICE_ONLY__ )&& defined(__NVPTX__ )&& \ 811 defined(__CUDA_ARCH__ )&& __CUDA_ARCH__ >=610 812res = __dp2a_hi (a ,b ,c ); 813#else 814auto va = ::dpct::detail::extract_and_sign_or_zero_extend2 (a ); 815auto vb = ::dpct::detail::extract_and_sign_or_zero_extend4 (b ); 816res += va [0 ]* vb [2 ]; 817res += va [1 ]* vb [3 ]; 818#endif 819return res ; 820} 821 822/// Four-way byte dot product-accumulate. Calculate and return interger_vector4( 823/// \param a) dot product interger_vector4( \param b) + \param c 824/// 825/// \tparam [in] T1 The type of first value. 826/// \tparam [in] T2 The type of second value. 827/// \param [in] a The first value. 828/// \param [in] b The second value. 829/// \param [in] c The third value. It has type uint32_t if both T1 and T1 are 830/// uint32_t else has type int32_t. 831/// \return Four-way byte dot product which is accumulated in 32-bit result. 832template < typename T1 ,typename T2 ,typename T3 > 833inline auto dp4a (T1 a ,T2 b ,T3 c ) { 834 detail::dot_product_acc_t < T1 ,T2 > res = c ; 835#if defined(__SYCL_DEVICE_ONLY__ )&& defined(__NVPTX__ )&& \ 836 defined(__CUDA_ARCH__ )&& __CUDA_ARCH__ >=610 837res = __dp4a (a ,b ,c ); 838#else 839auto va = ::dpct::detail::extract_and_sign_or_zero_extend4 (a ); 840auto vb = ::dpct::detail::extract_and_sign_or_zero_extend4 (b ); 841res += va [0 ]* vb [0 ]; 842res += va [1 ]* vb [1 ]; 843res += va [2 ]* vb [2 ]; 844res += va [3 ]* vb [3 ]; 845#endif 846return res ; 847} 848 849/// Extend \p a and \p b to 33 bit and add them. 850/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 851/// \tparam [in] AT The type of the first value, can only be 32 bit integer 852/// \tparam [in] BT The type of the second value, can only be 32 bit integer 853/// \param [in] a The first value 854/// \param [in] b The second value 855/// \returns The extend addition of the two values 856template < typename RetT ,typename AT ,typename BT > 857inline constexpr RetT extend_add (AT a ,BT b ) { 858return detail::extend_binary < RetT , false> (a ,b , std::plus ()); 859} 860 861/// Extend Inputs to 33 bit, add \p a, \p b, then do \p second_op with \p c. 862/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 863/// \tparam [in] AT The type of the first value, can only be 32 bit integer 864/// \tparam [in] BT The type of the second value, can only be 32 bit integer 865/// \tparam [in] CT The type of the third value, can only be 32 bit integer 866/// \tparam [in] BinaryOperation The type of the second operation 867/// \param [in] a The first value 868/// \param [in] b The second value 869/// \param [in] c The third value 870/// \param [in] second_op The operation to do with the third value 871/// \returns The extend addition of \p a, \p b and \p second_op with \p c 872template < typename RetT ,typename AT ,typename BT ,typename CT , 873typename BinaryOperation > 874inline constexpr RetT extend_add (AT a ,BT b ,CT c ,BinaryOperation second_op ) { 875return detail::extend_binary < RetT , false> (a ,b ,c , std::plus (),second_op ); 876} 877 878/// Extend \p a and \p b to 33 bit and add them with saturation. 879/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 880/// \tparam [in] AT The type of the first value, can only be 32 bit integer 881/// \tparam [in] BT The type of the second value, can only be 32 bit integer 882/// \param [in] a The first value 883/// \param [in] b The second value 884/// \returns The extend addition of the two values with saturation 885template < typename RetT ,typename AT ,typename BT > 886inline constexpr RetT extend_add_sat (AT a ,BT b ) { 887return detail::extend_binary < RetT , true> (a ,b , std::plus ()); 888} 889 890/// Extend Inputs to 33 bit, add \p a, \p b with saturation, then do \p 891/// second_op with \p c. 892/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 893/// \tparam [in] AT The type of the first value, can only be 32 bit integer 894/// \tparam [in] BT The type of the second value, can only be 32 bit integer 895/// \tparam [in] CT The type of the third value, can only be 32 bit integer 896/// \tparam [in] BinaryOperation The type of the second operation 897/// \param [in] a The first value 898/// \param [in] b The second value 899/// \param [in] c The third value 900/// \param [in] second_op The operation to do with the third value 901/// \returns The extend addition of \p a, \p b with saturation and \p second_op 902/// with \p c 903template < typename RetT ,typename AT ,typename BT ,typename CT , 904typename BinaryOperation > 905inline constexpr RetT extend_add_sat (AT a ,BT b ,CT c , 906BinaryOperation second_op ) { 907return detail::extend_binary < RetT , true> (a ,b ,c , std::plus (),second_op ); 908} 909 910/// Extend \p a and \p b to 33 bit and minus them. 911/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 912/// \tparam [in] AT The type of the first value, can only be 32 bit integer 913/// \tparam [in] BT The type of the second value, can only be 32 bit integer 914/// \param [in] a The first value 915/// \param [in] b The second value 916/// \returns The extend subtraction of the two values 917template < typename RetT ,typename AT ,typename BT > 918inline constexpr RetT extend_sub (AT a ,BT b ) { 919return detail::extend_binary < RetT , false> (a ,b , std::minus ()); 920} 921 922/// Extend Inputs to 33 bit, minus \p a, \p b, then do \p second_op with \p c. 923/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 924/// \tparam [in] AT The type of the first value, can only be 32 bit integer 925/// \tparam [in] BT The type of the second value, can only be 32 bit integer 926/// \tparam [in] CT The type of the third value, can only be 32 bit integer 927/// \tparam [in] BinaryOperation The type of the second operation 928/// \param [in] a The first value 929/// \param [in] b The second value 930/// \param [in] c The third value 931/// \param [in] second_op The operation to do with the third value 932/// \returns The extend subtraction of \p a, \p b and \p second_op with \p c 933template < typename RetT ,typename AT ,typename BT ,typename CT , 934typename BinaryOperation > 935inline constexpr RetT extend_sub (AT a ,BT b ,CT c ,BinaryOperation second_op ) { 936return detail::extend_binary < RetT , false> (a ,b ,c , std::minus (),second_op ); 937} 938 939/// Extend \p a and \p b to 33 bit and minus them with saturation. 940/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 941/// \tparam [in] AT The type of the first value, can only be 32 bit integer 942/// \tparam [in] BT The type of the second value, can only be 32 bit integer 943/// \param [in] a The first value 944/// \param [in] b The second value 945/// \returns The extend subtraction of the two values with saturation 946template < typename RetT ,typename AT ,typename BT > 947inline constexpr RetT extend_sub_sat (AT a ,BT b ) { 948return detail::extend_binary < RetT , true> (a ,b , std::minus ()); 949} 950 951/// Extend Inputs to 33 bit, minus \p a, \p b with saturation, then do \p 952/// second_op with \p c. 953/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 954/// \tparam [in] AT The type of the first value, can only be 32 bit integer 955/// \tparam [in] BT The type of the second value, can only be 32 bit integer 956/// \tparam [in] CT The type of the third value, can only be 32 bit integer 957/// \tparam [in] BinaryOperation The type of the second operation 958/// \param [in] a The first value 959/// \param [in] b The second value 960/// \param [in] c The third value 961/// \param [in] second_op The operation to do with the third value 962/// \returns The extend subtraction of \p a, \p b with saturation and \p 963/// second_op with \p c 964template < typename RetT ,typename AT ,typename BT ,typename CT , 965typename BinaryOperation > 966inline constexpr RetT extend_sub_sat (AT a ,BT b ,CT c , 967BinaryOperation second_op ) { 968return detail::extend_binary < RetT , true> (a ,b ,c , std::minus (),second_op ); 969} 970 971/// Extend \p a and \p b to 33 bit and do abs_diff. 972/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 973/// \tparam [in] AT The type of the first value, can only be 32 bit integer 974/// \tparam [in] BT The type of the second value, can only be 32 bit integer 975/// \param [in] a The first value 976/// \param [in] b The second value 977/// \returns The extend abs_diff of the two values 978template < typename RetT ,typename AT ,typename BT > 979inline constexpr RetT extend_absdiff (AT a ,BT b ) { 980return detail::extend_binary < RetT , false> (a ,b ,abs_diff ()); 981} 982 983/// Extend Inputs to 33 bit, abs_diff \p a, \p b, then do \p second_op with \p 984/// c. 985/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 986/// \tparam [in] AT The type of the first value, can only be 32 bit integer 987/// \tparam [in] BT The type of the second value, can only be 32 bit integer 988/// \tparam [in] CT The type of the third value, can only be 32 bit integer 989/// \tparam [in] BinaryOperation The type of the second operation 990/// \param [in] a The first value 991/// \param [in] b The second value 992/// \param [in] c The third value 993/// \param [in] second_op The operation to do with the third value 994/// \returns The extend abs_diff of \p a, \p b and \p second_op with \p c 995template < typename RetT ,typename AT ,typename BT ,typename CT , 996typename BinaryOperation > 997inline constexpr RetT extend_absdiff (AT a ,BT b ,CT c , 998BinaryOperation second_op ) { 999return detail::extend_binary < RetT , false> (a ,b ,c ,abs_diff (),second_op ); 1000} 1001 1002/// Extend \p a and \p b to 33 bit and do abs_diff with saturation. 1003/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1004/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1005/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1006/// \param [in] a The first value 1007/// \param [in] b The second value 1008/// \returns The extend abs_diff of the two values with saturation 1009template < typename RetT ,typename AT ,typename BT > 1010inline constexpr RetT extend_absdiff_sat (AT a ,BT b ) { 1011return detail::extend_binary < RetT , true> (a ,b ,abs_diff ()); 1012} 1013 1014/// Extend Inputs to 33 bit, abs_diff \p a, \p b with saturation, then do \p 1015/// second_op with \p c. 1016/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1017/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1018/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1019/// \tparam [in] CT The type of the third value, can only be 32 bit integer 1020/// \tparam [in] BinaryOperation The type of the second operation 1021/// \param [in] a The first value 1022/// \param [in] b The second value 1023/// \param [in] c The third value 1024/// \param [in] second_op The operation to do with the third value 1025/// \returns The extend abs_diff of \p a, \p b with saturation and \p 1026/// second_op with \p c 1027template < typename RetT ,typename AT ,typename BT ,typename CT , 1028typename BinaryOperation > 1029inline constexpr RetT extend_absdiff_sat (AT a ,BT b ,CT c , 1030BinaryOperation second_op ) { 1031return detail::extend_binary < RetT , true> (a ,b ,c ,abs_diff (),second_op ); 1032} 1033 1034/// Extend \p a and \p b to 33 bit and return smaller one. 1035/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1036/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1037/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1038/// \param [in] a The first value 1039/// \param [in] b The second value 1040/// \returns The smaller one of the two extended values 1041template < typename RetT ,typename AT ,typename BT > 1042inline constexpr RetT extend_min (AT a ,BT b ) { 1043return detail::extend_binary < RetT , false> (a ,b ,minimum ()); 1044} 1045 1046/// Extend Inputs to 33 bit, find the smaller one in \p a, \p b, then do \p 1047/// second_op with \p c. 1048/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1049/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1050/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1051/// \tparam [in] CT The type of the third value, can only be 32 bit integer 1052/// \tparam [in] BinaryOperation The type of the second operation 1053/// \param [in] a The first value 1054/// \param [in] b The second value 1055/// \param [in] c The third value 1056/// \param [in] second_op The operation to do with the third value 1057/// \returns The smaller one of \p a, \p b and \p second_op with \p c 1058template < typename RetT ,typename AT ,typename BT ,typename CT , 1059typename BinaryOperation > 1060inline constexpr RetT extend_min (AT a ,BT b ,CT c ,BinaryOperation second_op ) { 1061return detail::extend_binary < RetT , false> (a ,b ,c ,minimum (),second_op ); 1062} 1063 1064/// Extend \p a and \p b to 33 bit and return smaller one with saturation. 1065/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1066/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1067/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1068/// \param [in] a The first value 1069/// \param [in] b The second value 1070/// \returns The smaller one of the two extended values with saturation 1071template < typename RetT ,typename AT ,typename BT > 1072inline constexpr RetT extend_min_sat (AT a ,BT b ) { 1073return detail::extend_binary < RetT , true> (a ,b ,minimum ()); 1074} 1075 1076/// Extend Inputs to 33 bit, find the smaller one in \p a, \p b with saturation, 1077/// then do \p second_op with \p c. 1078/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1079/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1080/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1081/// \tparam [in] CT The type of the third value, can only be 32 bit integer 1082/// \tparam [in] BinaryOperation The type of the second operation 1083/// \param [in] a The first value 1084/// \param [in] b The second value 1085/// \param [in] c The third value 1086/// \param [in] second_op The operation to do with the third value 1087/// \returns The smaller one of \p a, \p b with saturation and \p 1088/// second_op with \p c 1089template < typename RetT ,typename AT ,typename BT ,typename CT , 1090typename BinaryOperation > 1091inline constexpr RetT extend_min_sat (AT a ,BT b ,CT c , 1092BinaryOperation second_op ) { 1093return detail::extend_binary < RetT , true> (a ,b ,c ,minimum (),second_op ); 1094} 1095 1096/// Extend \p a and \p b to 33 bit and return bigger one. 1097/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1098/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1099/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1100/// \param [in] a The first value 1101/// \param [in] b The second value 1102/// \returns The bigger one of the two extended values 1103template < typename RetT ,typename AT ,typename BT > 1104inline constexpr RetT extend_max (AT a ,BT b ) { 1105return detail::extend_binary < RetT , false> (a ,b ,maximum ()); 1106} 1107 1108/// Extend Inputs to 33 bit, find the bigger one in \p a, \p b, then do \p 1109/// second_op with \p c. 1110/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1111/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1112/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1113/// \tparam [in] CT The type of the third value, can only be 32 bit integer 1114/// \tparam [in] BinaryOperation The type of the second operation 1115/// \param [in] a The first value 1116/// \param [in] b The second value 1117/// \param [in] c The third value 1118/// \param [in] second_op The operation to do with the third value 1119/// \returns The bigger one of \p a, \p b and \p second_op with \p c 1120template < typename RetT ,typename AT ,typename BT ,typename CT , 1121typename BinaryOperation > 1122inline constexpr RetT extend_max (AT a ,BT b ,CT c ,BinaryOperation second_op ) { 1123return detail::extend_binary < RetT , false> (a ,b ,c ,maximum (),second_op ); 1124} 1125 1126/// Extend \p a and \p b to 33 bit and return bigger one with saturation. 1127/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1128/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1129/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1130/// \param [in] a The first value 1131/// \param [in] b The second value 1132/// \returns The bigger one of the two extended values with saturation 1133template < typename RetT ,typename AT ,typename BT > 1134inline constexpr RetT extend_max_sat (AT a ,BT b ) { 1135return detail::extend_binary < RetT , true> (a ,b ,maximum ()); 1136} 1137 1138/// Extend Inputs to 33 bit, find the bigger one in \p a, \p b with saturation, 1139/// then do \p second_op with \p c. 1140/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1141/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1142/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1143/// \tparam [in] CT The type of the third value, can only be 32 bit integer 1144/// \tparam [in] BinaryOperation The type of the second operation 1145/// \param [in] a The first value 1146/// \param [in] b The second value 1147/// \param [in] c The third value 1148/// \param [in] second_op The operation to do with the third value 1149/// \returns The bigger one of \p a, \p b with saturation and \p 1150/// second_op with \p c 1151template < typename RetT ,typename AT ,typename BT ,typename CT , 1152typename BinaryOperation > 1153inline constexpr RetT extend_max_sat (AT a ,BT b ,CT c , 1154BinaryOperation second_op ) { 1155return detail::extend_binary < RetT , true> (a ,b ,c ,maximum (),second_op ); 1156} 1157 1158/// Extend \p a and \p b to 33 bit and compare input values using specified 1159/// comparison \p cmp . 1160/// 1161/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1162/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1163/// \tparam [in] BinaryOperation The type of the compare operation 1164/// \param [in] a The first value 1165/// \param [in] b The second value 1166/// \param [in] cmp The comparsion operator 1167/// \returns The comparison result of the two extended values. 1168template < typename AT ,typename BT ,typename BinaryOperation > 1169inline constexpr unsigned extend_compare (AT a ,BT b ,BinaryOperation cmp ) { 1170return detail::extend_binary < unsigned , false> (a ,b ,cmp ); 1171} 1172 1173/// Extend Inputs to 33 bit, and compare input values using specified comparison 1174/// \p cmp , then do \p second_op with \p c . 1175/// 1176/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1177/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1178/// \tparam [in] BinaryOperation The type of the compare operation 1179/// \tparam [in] SecondBinaryOperation The type of the second operation 1180/// \param [in] a The first value 1181/// \param [in] b The second value 1182/// \param [in] c The third value 1183/// \param [in] cmp The comparsion operator 1184/// \param [in] second_op The operation to do with the third value 1185/// \returns The comparison result of the two extended values. and \p second_op 1186/// with \p c . 1187template < typename AT ,typename BT ,typename BinaryOperation , 1188typename SecondBinaryOperation > 1189inline constexpr unsigned extend_compare (AT a ,BT b ,unsigned c , 1190BinaryOperation cmp , 1191SecondBinaryOperation second_op ) { 1192return detail::extend_binary < unsigned , false> (a ,b ,c ,cmp ,second_op ); 1193} 1194 1195/// Extend \p a and \p b to 33 bit and return a << clamp(b, 0, 32). 1196/// \param [in] a The source value 1197/// \param [in] b The offset to shift 1198/// \returns a << clamp(b, 0, 32) 1199template < typename RetT ,typename T > 1200inline constexpr RetT extend_shl_clamp (T a ,uint32_t b ) { 1201return detail::extend_binary < RetT , false> (a , sycl::clamp (b ,0u ,32u ), 1202 detail::shift_left ()); 1203} 1204 1205/// Extend Inputs to 33 bit, and return second_op(a << clamp(b, 0, 32), c). 1206/// \param [in] a The source value 1207/// \param [in] b The offset to shift 1208/// \param [in] c The value to merge 1209/// \param [in] second_op The operation to do with the third value 1210/// \returns second_op(a << clamp(b, 0, 32), c) 1211template < typename RetT ,typename T ,typename BinaryOperation > 1212inline constexpr RetT extend_shl_clamp (T a ,uint32_t b ,uint32_t c , 1213BinaryOperation second_op ) { 1214return detail::extend_binary < RetT , false> (a , sycl::clamp (b ,0u ,32u ),c , 1215 detail::shift_left (),second_op ); 1216} 1217 1218/// Extend \p a and \p b to 33 bit and return sat(a << clamp(b, 0, 32)). 1219/// \param [in] a The source value 1220/// \param [in] b The offset to shift 1221/// \returns sat(a << clamp(b, 0, 32)) 1222template < typename RetT ,typename T > 1223inline constexpr RetT extend_shl_sat_clamp (T a ,uint32_t b ) { 1224return detail::extend_binary < RetT , true> (a , sycl::clamp (b ,0u ,32u ), 1225 detail::shift_left ()); 1226} 1227 1228/// Extend Inputs to 33 bit, and return second_op(sat(a << clamp(b, 0, 32)), c). 1229/// \param [in] a The source value 1230/// \param [in] b The offset to shift 1231/// \param [in] c The value to merge 1232/// \param [in] second_op The operation to do with the third value 1233/// \returns second_op(sat(a << clamp(b, 0, 32)), c) 1234template < typename RetT ,typename T ,typename BinaryOperation > 1235inline constexpr RetT extend_shl_sat_clamp (T a ,uint32_t b ,uint32_t c , 1236BinaryOperation second_op ) { 1237return detail::extend_binary < RetT , true> (a , sycl::clamp (b ,0u ,32u ),c , 1238 detail::shift_left (),second_op ); 1239} 1240 1241/// Extend \p a and \p b to 33 bit and return a << (b & 0x1F). 1242/// \param [in] a The source value 1243/// \param [in] b The offset to shift 1244/// \returns a << (b & 0x1F) 1245template < typename RetT ,typename T > 1246inline constexpr RetT extend_shl_wrap (T a ,uint32_t b ) { 1247return detail::extend_binary < RetT , false> (a ,b & 0x1F , detail::shift_left ()); 1248} 1249 1250/// Extend Inputs to 33 bit, and return second_op(a << (b & 0x1F), c). 1251/// \param [in] a The source value 1252/// \param [in] b The offset to shift 1253/// \param [in] c The value to merge 1254/// \param [in] second_op The operation to do with the third value 1255/// \returns second_op(a << (b & 0x1F), c) 1256template < typename RetT ,typename T ,typename BinaryOperation > 1257inline constexpr RetT extend_shl_wrap (T a ,uint32_t b ,uint32_t c , 1258BinaryOperation second_op ) { 1259return detail::extend_binary < RetT , false> (a ,b & 0x1F ,c , 1260 detail::shift_left (),second_op ); 1261} 1262 1263/// Extend \p a and \p b to 33 bit and return sat(a << (b & 0x1F)). 1264/// \param [in] a The source value 1265/// \param [in] b The offset to shift 1266/// \returns sat(a << (b & 0x1F)) 1267template < typename RetT ,typename T > 1268inline constexpr RetT extend_shl_sat_wrap (T a ,uint32_t b ) { 1269return detail::extend_binary < RetT , true> (a ,b & 0x1F , detail::shift_left ()); 1270} 1271 1272/// Extend Inputs to 33 bit, and return second_op(sat(a << (b & 0x1F)), c). 1273/// \param [in] a The source value 1274/// \param [in] b The offset to shift 1275/// \param [in] c The value to merge 1276/// \param [in] second_op The operation to do with the third value 1277/// \returns second_op(sat(a << (b & 0x1F)), c) 1278template < typename RetT ,typename T ,typename BinaryOperation > 1279inline constexpr RetT extend_shl_sat_wrap (T a ,uint32_t b ,uint32_t c , 1280BinaryOperation second_op ) { 1281return detail::extend_binary < RetT , true> (a ,b & 0x1F ,c , detail::shift_left (), 1282second_op ); 1283} 1284 1285/// Extend \p a and \p b to 33 bit and return a >> clamp(b, 0, 32). 1286/// \param [in] a The source value 1287/// \param [in] b The offset to shift 1288/// \returns a >> clamp(b, 0, 32) 1289template < typename RetT ,typename T > 1290inline constexpr RetT extend_shr_clamp (T a ,uint32_t b ) { 1291return detail::extend_binary < RetT , false> (a , sycl::clamp (b ,0u ,32u ), 1292 detail::shift_right ()); 1293} 1294 1295/// Extend Inputs to 33 bit, and return second_op(a >> clamp(b, 0, 32), c). 1296/// \param [in] a The source value 1297/// \param [in] b The offset to shift 1298/// \param [in] c The value to merge 1299/// \param [in] second_op The operation to do with the third value 1300/// \returns second_op(a >> clamp(b, 0, 32), c) 1301template < typename RetT ,typename T ,typename BinaryOperation > 1302inline constexpr RetT extend_shr_clamp (T a ,uint32_t b ,uint32_t c , 1303BinaryOperation second_op ) { 1304return detail::extend_binary < RetT , false> (a , sycl::clamp (b ,0u ,32u ),c , 1305 detail::shift_right (),second_op ); 1306} 1307 1308/// Extend \p a and \p b to 33 bit and return sat(a >> clamp(b, 0, 32)). 1309/// \param [in] a The source value 1310/// \param [in] b The offset to shift 1311/// \returns sat(a >> clamp(b, 0, 32)) 1312template < typename RetT ,typename T > 1313inline constexpr RetT extend_shr_sat_clamp (T a ,uint32_t b ) { 1314return detail::extend_binary < RetT , true> (a , sycl::clamp (b ,0u ,32u ), 1315 detail::shift_right ()); 1316} 1317 1318/// Extend Inputs to 33 bit, and return second_op(sat(a >> clamp(b, 0, 32)), c). 1319/// \param [in] a The source value 1320/// \param [in] b The offset to shift 1321/// \param [in] c The value to merge 1322/// \param [in] second_op The operation to do with the third value 1323/// \returns second_op(sat(a >> clamp(b, 0, 32)), c) 1324template < typename RetT ,typename T ,typename BinaryOperation > 1325inline constexpr RetT extend_shr_sat_clamp (T a ,uint32_t b ,uint32_t c , 1326BinaryOperation second_op ) { 1327return detail::extend_binary < RetT , true> (a , sycl::clamp (b ,0u ,32u ),c , 1328 detail::shift_right (),second_op ); 1329} 1330 1331/// Extend \p a and \p b to 33 bit and return a >> (b & 0x1F). 1332/// \param [in] a The source value 1333/// \param [in] b The offset to shift 1334/// \returns a >> (b & 0x1F) 1335template < typename RetT ,typename T > 1336inline constexpr RetT extend_shr_wrap (T a ,uint32_t b ) { 1337return detail::extend_binary < RetT , false> (a ,b & 0x1F , detail::shift_right ()); 1338} 1339 1340/// Extend Inputs to 33 bit, and return second_op(a >> (b & 0x1F), c). 1341/// \param [in] a The source value 1342/// \param [in] b The offset to shift 1343/// \param [in] c The value to merge 1344/// \param [in] second_op The operation to do with the third value 1345/// \returns second_op(a >> (b & 0x1F), c) 1346template < typename RetT ,typename T ,typename BinaryOperation > 1347inline constexpr RetT extend_shr_wrap (T a ,uint32_t b ,uint32_t c , 1348BinaryOperation second_op ) { 1349return detail::extend_binary < RetT , false> (a ,b & 0x1F ,c , 1350 detail::shift_right (),second_op ); 1351} 1352 1353/// Extend \p a and \p b to 33 bit and return sat(a >> (b & 0x1F)). 1354/// \param [in] a The source value 1355/// \param [in] b The offset to shift 1356/// \returns sat(a >> (b & 0x1F)) 1357template < typename RetT ,typename T > 1358inline constexpr RetT extend_shr_sat_wrap (T a ,uint32_t b ) { 1359return detail::extend_binary < RetT , true> (a ,b & 0x1F , detail::shift_right ()); 1360} 1361 1362/// Extend Inputs to 33 bit, and return second_op(sat(a >> (b & 0x1F)), c). 1363/// \param [in] a The source value 1364/// \param [in] b The offset to shift 1365/// \param [in] c The value to merge 1366/// \param [in] second_op The operation to do with the third value 1367/// \returns second_op(sat(a >> (b & 0x1F)), c) 1368template < typename RetT ,typename T ,typename BinaryOperation > 1369inline constexpr RetT extend_shr_sat_wrap (T a ,uint32_t b ,uint32_t c , 1370BinaryOperation second_op ) { 1371return detail::extend_binary < RetT , true> (a ,b & 0x1F ,c , 1372 detail::shift_right (),second_op ); 1373} 1374 1375/// Compute vectorized addition of \p a and \p b, with each value treated as a 1376/// 2 elements vector type and extend each element to 17 bit. 1377/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1378/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1379/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1380/// \param [in] a The first value 1381/// \param [in] b The second value 1382/// \param [in] c The third value 1383/// \returns The extend vectorized addition of the two values 1384template < typename RetT ,typename AT ,typename BT > 1385inline constexpr RetT extend_vadd2 (AT a ,BT b ,RetT c ) { 1386return detail::extend_vbinary2 < RetT , false, false> (a ,b ,c , std::plus ()); 1387} 1388 1389/// Compute vectorized addition of \p a and \p b, with each value treated as a 2 1390/// elements vector type and extend each element to 17 bit. Then add each half 1391/// of the result and add with \p c. 1392/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1393/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1394/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1395/// \param [in] a The first value 1396/// \param [in] b The second value 1397/// \param [in] c The third value 1398/// \returns The addition of each half of extend vectorized addition of the two 1399/// values and the third value 1400template < typename RetT ,typename AT ,typename BT > 1401inline constexpr RetT extend_vadd2_add (AT a ,BT b ,RetT c ) { 1402return detail::extend_vbinary2 < RetT , false, true> (a ,b ,c , std::plus ()); 1403} 1404 1405/// Compute vectorized addition of \p a and \p b with saturation, with each 1406/// value treated as a 2 elements vector type and extend each element to 17 bit. 1407/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1408/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1409/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1410/// \param [in] a The first value 1411/// \param [in] b The second value 1412/// \param [in] c The third value 1413/// \returns The extend vectorized addition of the two values with saturation 1414template < typename RetT ,typename AT ,typename BT > 1415inline constexpr RetT extend_vadd2_sat (AT a ,BT b ,RetT c ) { 1416return detail::extend_vbinary2 < RetT , true, false> (a ,b ,c , std::plus ()); 1417} 1418 1419/// Compute vectorized subtraction of \p a and \p b, with each value treated as 1420/// a 2 elements vector type and extend each element to 17 bit. 1421/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1422/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1423/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1424/// \param [in] a The first value 1425/// \param [in] b The second value 1426/// \param [in] c The third value 1427/// \returns The extend vectorized subtraction of the two values 1428template < typename RetT ,typename AT ,typename BT > 1429inline constexpr RetT extend_vsub2 (AT a ,BT b ,RetT c ) { 1430return detail::extend_vbinary2 < RetT , false, false> (a ,b ,c , std::minus ()); 1431} 1432 1433/// Compute vectorized subtraction of \p a and \p b, with each value treated as 1434/// a 2 elements vector type and extend each element to 17 bit. Then add each 1435/// half of the result and add with \p c. 1436/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1437/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1438/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1439/// \param [in] a The first value 1440/// \param [in] b The second value 1441/// \param [in] c The third value 1442/// \returns The addition of each half of extend vectorized subtraction of the 1443/// two values and the third value 1444template < typename RetT ,typename AT ,typename BT > 1445inline constexpr RetT extend_vsub2_add (AT a ,BT b ,RetT c ) { 1446return detail::extend_vbinary2 < RetT , false, true> (a ,b ,c , std::minus ()); 1447} 1448 1449/// Compute vectorized subtraction of \p a and \p b with saturation, with each 1450/// value treated as a 2 elements vector type and extend each element to 17 bit. 1451/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1452/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1453/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1454/// \param [in] a The first value 1455/// \param [in] b The second value 1456/// \param [in] c The third value 1457/// \returns The extend vectorized subtraction of the two values with saturation 1458template < typename RetT ,typename AT ,typename BT > 1459inline constexpr RetT extend_vsub2_sat (AT a ,BT b ,RetT c ) { 1460return detail::extend_vbinary2 < RetT , true, false> (a ,b ,c , std::minus ()); 1461} 1462 1463/// Compute vectorized abs_diff of \p a and \p b, with each value treated as a 2 1464/// elements vector type and extend each element to 17 bit. 1465/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1466/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1467/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1468/// \param [in] a The first value 1469/// \param [in] b The second value 1470/// \param [in] c The third value 1471/// \returns The extend vectorized abs_diff of the two values 1472template < typename RetT ,typename AT ,typename BT > 1473inline constexpr RetT extend_vabsdiff2 (AT a ,BT b ,RetT c ) { 1474return detail::extend_vbinary2 < RetT , false, false> (a ,b ,c ,abs_diff ()); 1475} 1476 1477/// Compute vectorized abs_diff of \p a and \p b, with each value treated as a 2 1478/// elements vector type and extend each element to 17 bit. Then add each half 1479/// of the result and add with \p c. 1480/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1481/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1482/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1483/// \param [in] a The first value 1484/// \param [in] b The second value 1485/// \param [in] c The third value 1486/// \returns The addition of each half of extend vectorized abs_diff of the 1487/// two values and the third value 1488template < typename RetT ,typename AT ,typename BT > 1489inline constexpr RetT extend_vabsdiff2_add (AT a ,BT b ,RetT c ) { 1490return detail::extend_vbinary2 < RetT , false, true> (a ,b ,c ,abs_diff ()); 1491} 1492 1493/// Compute vectorized abs_diff of \p a and \p b with saturation, with each 1494/// value treated as a 2 elements vector type and extend each element to 17 bit. 1495/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1496/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1497/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1498/// \param [in] a The first value 1499/// \param [in] b The second value 1500/// \param [in] c The third value 1501/// \returns The extend vectorized abs_diff of the two values with saturation 1502template < typename RetT ,typename AT ,typename BT > 1503inline constexpr RetT extend_vabsdiff2_sat (AT a ,BT b ,RetT c ) { 1504return detail::extend_vbinary2 < RetT , true, false> (a ,b ,c ,abs_diff ()); 1505} 1506 1507/// Compute vectorized minimum of \p a and \p b, with each value treated as a 2 1508/// elements vector type and extend each element to 17 bit. 1509/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1510/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1511/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1512/// \param [in] a The first value 1513/// \param [in] b The second value 1514/// \param [in] c The third value 1515/// \returns The extend vectorized minimum of the two values 1516template < typename RetT ,typename AT ,typename BT > 1517inline constexpr RetT extend_vmin2 (AT a ,BT b ,RetT c ) { 1518return detail::extend_vbinary2 < RetT , false, false> (a ,b ,c ,minimum ()); 1519} 1520 1521/// Compute vectorized minimum of \p a and \p b, with each value treated as a 2 1522/// elements vector type and extend each element to 17 bit. Then add each half 1523/// of the result and add with \p c. 1524/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1525/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1526/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1527/// \param [in] a The first value 1528/// \param [in] b The second value 1529/// \param [in] c The third value 1530/// \returns The addition of each half of extend vectorized minimum of the 1531/// two values and the third value 1532template < typename RetT ,typename AT ,typename BT > 1533inline constexpr RetT extend_vmin2_add (AT a ,BT b ,RetT c ) { 1534return detail::extend_vbinary2 < RetT , false, true> (a ,b ,c ,minimum ()); 1535} 1536 1537/// Compute vectorized minimum of \p a and \p b with saturation, with each value 1538/// treated as a 2 elements vector type and extend each element to 17 bit. 1539/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1540/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1541/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1542/// \param [in] a The first value 1543/// \param [in] b The second value 1544/// \param [in] c The third value 1545/// \returns The extend vectorized minimum of the two values with saturation 1546template < typename RetT ,typename AT ,typename BT > 1547inline constexpr RetT extend_vmin2_sat (AT a ,BT b ,RetT c ) { 1548return detail::extend_vbinary2 < RetT , true, false> (a ,b ,c ,minimum ()); 1549} 1550 1551/// Compute vectorized maximum of \p a and \p b, with each value treated as a 2 1552/// elements vector type and extend each element to 17 bit. 1553/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1554/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1555/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1556/// \param [in] a The first value 1557/// \param [in] b The second value 1558/// \param [in] c The third value 1559/// \returns The extend vectorized maximum of the two values 1560template < typename RetT ,typename AT ,typename BT > 1561inline constexpr RetT extend_vmax2 (AT a ,BT b ,RetT c ) { 1562return detail::extend_vbinary2 < RetT , false, false> (a ,b ,c ,maximum ()); 1563} 1564 1565/// Compute vectorized maximum of \p a and \p b, with each value treated as a 2 1566/// elements vector type and extend each element to 17 bit. Then add each half 1567/// of the result and add with \p c. 1568/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1569/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1570/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1571/// \param [in] a The first value 1572/// \param [in] b The second value 1573/// \param [in] c The third value 1574/// \returns The addition of each half of extend vectorized maximum of the 1575/// two values and the third value 1576template < typename RetT ,typename AT ,typename BT > 1577inline constexpr RetT extend_vmax2_add (AT a ,BT b ,RetT c ) { 1578return detail::extend_vbinary2 < RetT , false, true> (a ,b ,c ,maximum ()); 1579} 1580 1581/// Compute vectorized maximum of \p a and \p b with saturation, with each value 1582/// treated as a 2 elements vector type and extend each element to 17 bit. 1583/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1584/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1585/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1586/// \param [in] a The first value 1587/// \param [in] b The second value 1588/// \param [in] c The third value 1589/// \returns The extend vectorized maximum of the two values with saturation 1590template < typename RetT ,typename AT ,typename BT > 1591inline constexpr RetT extend_vmax2_sat (AT a ,BT b ,RetT c ) { 1592return detail::extend_vbinary2 < RetT , true, false> (a ,b ,c ,maximum ()); 1593} 1594 1595/// Extend \p a and \p b to 33 bit and vectorized compare input values using 1596/// 1597/// specified comparison \p cmp . 1598/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1599/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1600/// \tparam [in] BinaryOperation The type of the compare operation 1601/// \param [in] a The first value 1602/// \param [in] b The second value 1603/// \param [in] cmp The comparsion operator 1604/// \returns The comparison result of the two extended values. 1605template < typename AT ,typename BT ,typename BinaryOperation > 1606inline constexpr unsigned extend_vcompare2 (AT a ,BT b ,BinaryOperation cmp ) { 1607return detail::extend_vbinary2 < unsigned , false, false> (a ,b ,0 ,cmp ); 1608} 1609 1610/// Extend Inputs to 33 bit, and vectorized compare input values using specified 1611/// comparison \p cmp , then add the result with \p c . 1612/// 1613/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1614/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1615/// \tparam [in] BinaryOperation The type of the compare operation 1616/// \param [in] a The first value 1617/// \param [in] b The second value 1618/// \param [in] c The third value 1619/// \param [in] cmp The comparsion operator 1620/// \returns The comparison result of the two extended values, and add the 1621/// result with \p c . 1622template < typename AT ,typename BT ,typename BinaryOperation > 1623inline constexpr unsigned extend_vcompare2_add (AT a ,BT b ,unsigned c , 1624BinaryOperation cmp ) { 1625return detail::extend_vbinary2 < unsigned , false, true> (a ,b ,c ,cmp ); 1626} 1627 1628/// Compute vectorized average of \p a and \p b, with each value treated as a 2 1629/// elements vector type and extend each element to 17 bit. 1630/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1631/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1632/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1633/// \param [in] a The first value 1634/// \param [in] b The second value 1635/// \param [in] c The third value 1636/// \returns The extend vectorized average of the two values 1637template < typename RetT ,typename AT ,typename BT > 1638inline constexpr RetT extend_vavrg2 (AT a ,BT b ,RetT c ) { 1639return detail::extend_vbinary2 < RetT , false, false> (a ,b ,c , 1640 detail::average ()); 1641} 1642 1643/// Compute vectorized average of \p a and \p b, with each value treated as a 2 1644/// elements vector type and extend each element to 17 bit. Then add each half 1645/// of the result and add with \p c. 1646/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1647/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1648/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1649/// \param [in] a The first value 1650/// \param [in] b The second value 1651/// \param [in] c The third value 1652/// \returns The addition of each half of extend average maximum of the 1653/// two values and the third value 1654template < typename RetT ,typename AT ,typename BT > 1655inline constexpr RetT extend_vavrg2_add (AT a ,BT b ,RetT c ) { 1656return detail::extend_vbinary2 < RetT , false, true> (a ,b ,c , detail::average ()); 1657} 1658 1659/// Compute vectorized average of \p a and \p b with saturation, with each value 1660/// treated as a 2 elements vector type and extend each element to 17 bit. 1661/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1662/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1663/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1664/// \param [in] a The first value 1665/// \param [in] b The second value 1666/// \param [in] c The third value 1667/// \returns The extend vectorized average of the two values with saturation 1668template < typename RetT ,typename AT ,typename BT > 1669inline constexpr RetT extend_vavrg2_sat (AT a ,BT b ,RetT c ) { 1670return detail::extend_vbinary2 < RetT , true, false> (a ,b ,c , detail::average ()); 1671} 1672 1673/// Compute vectorized addition of \p a and \p b, with each value treated as a 1674/// 4 elements vector type and extend each element to 9 bit. 1675/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1676/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1677/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1678/// \param [in] a The first value 1679/// \param [in] b The second value 1680/// \param [in] c The third value 1681/// \returns The extend vectorized addition of the two values 1682template < typename RetT ,typename AT ,typename BT > 1683inline constexpr RetT extend_vadd4 (AT a ,BT b ,RetT c ) { 1684return detail::extend_vbinary4 < RetT , false, false> (a ,b ,c , std::plus ()); 1685} 1686 1687/// Compute vectorized addition of \p a and \p b, with each value treated as a 4 1688/// elements vector type and extend each element to 9 bit. Then add each half 1689/// of the result and add with \p c. 1690/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1691/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1692/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1693/// \param [in] a The first value 1694/// \param [in] b The second value 1695/// \param [in] c The third value 1696/// \returns The addition of each half of extend vectorized addition of the two 1697/// values and the third value 1698template < typename RetT ,typename AT ,typename BT > 1699inline constexpr RetT extend_vadd4_add (AT a ,BT b ,RetT c ) { 1700return detail::extend_vbinary4 < RetT , false, true> (a ,b ,c , std::plus ()); 1701} 1702 1703/// Compute vectorized addition of \p a and \p b with saturation, with each 1704/// value treated as a 4 elements vector type and extend each element to 9 bit. 1705/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1706/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1707/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1708/// \param [in] a The first value 1709/// \param [in] b The second value 1710/// \param [in] c The third value 1711/// \returns The extend vectorized addition of the two values with saturation 1712template < typename RetT ,typename AT ,typename BT > 1713inline constexpr RetT extend_vadd4_sat (AT a ,BT b ,RetT c ) { 1714return detail::extend_vbinary4 < RetT , true, false> (a ,b ,c , std::plus ()); 1715} 1716 1717/// Compute vectorized subtraction of \p a and \p b, with each value treated as 1718/// a 4 elements vector type and extend each element to 9 bit. 1719/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1720/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1721/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1722/// \param [in] a The first value 1723/// \param [in] b The second value 1724/// \param [in] c The third value 1725/// \returns The extend vectorized subtraction of the two values 1726template < typename RetT ,typename AT ,typename BT > 1727inline constexpr RetT extend_vsub4 (AT a ,BT b ,RetT c ) { 1728return detail::extend_vbinary4 < RetT , false, false> (a ,b ,c , std::minus ()); 1729} 1730 1731/// Compute vectorized subtraction of \p a and \p b, with each value treated as 1732/// a 4 elements vector type and extend each element to 9 bit. Then add each 1733/// half of the result and add with \p c. 1734/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1735/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1736/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1737/// \param [in] a The first value 1738/// \param [in] b The second value 1739/// \param [in] c The third value 1740/// \returns The addition of each half of extend vectorized subtraction of the 1741/// two values and the third value 1742template < typename RetT ,typename AT ,typename BT > 1743inline constexpr RetT extend_vsub4_add (AT a ,BT b ,RetT c ) { 1744return detail::extend_vbinary4 < RetT , false, true> (a ,b ,c , std::minus ()); 1745} 1746 1747/// Compute vectorized subtraction of \p a and \p b with saturation, with each 1748/// value treated as a 4 elements vector type and extend each element to 9 bit. 1749/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1750/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1751/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1752/// \param [in] a The first value 1753/// \param [in] b The second value 1754/// \param [in] c The third value 1755/// \returns The extend vectorized subtraction of the two values with saturation 1756template < typename RetT ,typename AT ,typename BT > 1757inline constexpr RetT extend_vsub4_sat (AT a ,BT b ,RetT c ) { 1758return detail::extend_vbinary4 < RetT , true, false> (a ,b ,c , std::minus ()); 1759} 1760 1761/// Compute vectorized abs_diff of \p a and \p b, with each value treated as a 4 1762/// elements vector type and extend each element to 9 bit. 1763/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1764/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1765/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1766/// \param [in] a The first value 1767/// \param [in] b The second value 1768/// \param [in] c The third value 1769/// \returns The extend vectorized abs_diff of the two values 1770template < typename RetT ,typename AT ,typename BT > 1771inline constexpr RetT extend_vabsdiff4 (AT a ,BT b ,RetT c ) { 1772return detail::extend_vbinary4 < RetT , false, false> (a ,b ,c ,abs_diff ()); 1773} 1774 1775/// Compute vectorized abs_diff of \p a and \p b, with each value treated as a 4 1776/// elements vector type and extend each element to 9 bit. Then add each half 1777/// of the result and add with \p c. 1778/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1779/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1780/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1781/// \param [in] a The first value 1782/// \param [in] b The second value 1783/// \param [in] c The third value 1784/// \returns The addition of each half of extend vectorized abs_diff of the 1785/// two values and the third value 1786template < typename RetT ,typename AT ,typename BT > 1787inline constexpr RetT extend_vabsdiff4_add (AT a ,BT b ,RetT c ) { 1788return detail::extend_vbinary4 < RetT , false, true> (a ,b ,c ,abs_diff ()); 1789} 1790 1791/// Compute vectorized abs_diff of \p a and \p b with saturation, with each 1792/// value treated as a 4 elements vector type and extend each element to 9 bit. 1793/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1794/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1795/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1796/// \param [in] a The first value 1797/// \param [in] b The second value 1798/// \param [in] c The third value 1799/// \returns The extend vectorized abs_diff of the two values with saturation 1800template < typename RetT ,typename AT ,typename BT > 1801inline constexpr RetT extend_vabsdiff4_sat (AT a ,BT b ,RetT c ) { 1802return detail::extend_vbinary4 < RetT , true, false> (a ,b ,c ,abs_diff ()); 1803} 1804 1805/// Compute vectorized minimum of \p a and \p b, with each value treated as a 4 1806/// elements vector type and extend each element to 9 bit. 1807/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1808/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1809/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1810/// \param [in] a The first value 1811/// \param [in] b The second value 1812/// \param [in] c The third value 1813/// \returns The extend vectorized minimum of the two values 1814template < typename RetT ,typename AT ,typename BT > 1815inline constexpr RetT extend_vmin4 (AT a ,BT b ,RetT c ) { 1816return detail::extend_vbinary4 < RetT , false, false> (a ,b ,c ,minimum ()); 1817} 1818 1819/// Compute vectorized minimum of \p a and \p b, with each value treated as a 4 1820/// elements vector type and extend each element to 9 bit. Then add each half 1821/// of the result and add with \p c. 1822/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1823/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1824/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1825/// \param [in] a The first value 1826/// \param [in] b The second value 1827/// \param [in] c The third value 1828/// \returns The addition of each half of extend vectorized minimum of the 1829/// two values and the third value 1830template < typename RetT ,typename AT ,typename BT > 1831inline constexpr RetT extend_vmin4_add (AT a ,BT b ,RetT c ) { 1832return detail::extend_vbinary4 < RetT , false, true> (a ,b ,c ,minimum ()); 1833} 1834 1835/// Compute vectorized minimum of \p a and \p b with saturation, with each value 1836/// treated as a 4 elements vector type and extend each element to 9 bit. 1837/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1838/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1839/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1840/// \param [in] a The first value 1841/// \param [in] b The second value 1842/// \param [in] c The third value 1843/// \returns The extend vectorized minimum of the two values with saturation 1844template < typename RetT ,typename AT ,typename BT > 1845inline constexpr RetT extend_vmin4_sat (AT a ,BT b ,RetT c ) { 1846return detail::extend_vbinary4 < RetT , true, false> (a ,b ,c ,minimum ()); 1847} 1848 1849/// Compute vectorized maximum of \p a and \p b, with each value treated as a 4 1850/// elements vector type and extend each element to 9 bit. 1851/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1852/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1853/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1854/// \param [in] a The first value 1855/// \param [in] b The second value 1856/// \param [in] c The third value 1857/// \returns The extend vectorized maximum of the two values 1858template < typename RetT ,typename AT ,typename BT > 1859inline constexpr RetT extend_vmax4 (AT a ,BT b ,RetT c ) { 1860return detail::extend_vbinary4 < RetT , false, false> (a ,b ,c ,maximum ()); 1861} 1862 1863/// Compute vectorized maximum of \p a and \p b, with each value treated as a 4 1864/// elements vector type and extend each element to 9 bit. Then add each half 1865/// of the result and add with \p c. 1866/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1867/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1868/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1869/// \param [in] a The first value 1870/// \param [in] b The second value 1871/// \param [in] c The third value 1872/// \returns The addition of each half of extend vectorized maximum of the 1873/// two values and the third value 1874template < typename RetT ,typename AT ,typename BT > 1875inline constexpr RetT extend_vmax4_add (AT a ,BT b ,RetT c ) { 1876return detail::extend_vbinary4 < RetT , false, true> (a ,b ,c ,maximum ()); 1877} 1878 1879/// Compute vectorized maximum of \p a and \p b with saturation, with each value 1880/// treated as a 4 elements vector type and extend each element to 9 bit. 1881/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1882/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1883/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1884/// \param [in] a The first value 1885/// \param [in] b The second value 1886/// \param [in] c The third value 1887/// \returns The extend vectorized maximum of the two values with saturation 1888template < typename RetT ,typename AT ,typename BT > 1889inline constexpr RetT extend_vmax4_sat (AT a ,BT b ,RetT c ) { 1890return detail::extend_vbinary4 < RetT , true, false> (a ,b ,c ,maximum ()); 1891} 1892 1893/// Extend \p a and \p b to 33 bit and vectorized compare input values using 1894/// 1895/// specified comparison \p cmp . 1896/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1897/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1898/// \tparam [in] BinaryOperation The type of the compare operation 1899/// \param [in] a The first value 1900/// \param [in] b The second value 1901/// \param [in] cmp The comparsion operator 1902/// \returns The comparison result of the two extended values. 1903template < typename AT ,typename BT ,typename BinaryOperation > 1904inline constexpr unsigned extend_vcompare4 (AT a ,BT b ,BinaryOperation cmp ) { 1905return detail::extend_vbinary4 < unsigned , false, false> (a ,b ,0 ,cmp ); 1906} 1907 1908/// Extend Inputs to 33 bit, and vectorized compare input values using specified 1909/// comparison \p cmp , then add the result with \p c . 1910/// 1911/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1912/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1913/// \tparam [in] BinaryOperation The type of the compare operation 1914/// \param [in] a The first value 1915/// \param [in] b The second value 1916/// \param [in] c The third value 1917/// \param [in] cmp The comparsion operator 1918/// \returns The comparison result of the two extended values, and add the 1919/// result with \p c . 1920template < typename AT ,typename BT ,typename BinaryOperation > 1921inline constexpr unsigned extend_vcompare4_add (AT a ,BT b ,unsigned c , 1922BinaryOperation cmp ) { 1923return detail::extend_vbinary4 < unsigned , false, true> (a ,b ,c ,cmp ); 1924} 1925 1926/// Compute vectorized average of \p a and \p b, with each value treated as a 4 1927/// elements vector type and extend each element to 9 bit. 1928/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1929/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1930/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1931/// \param [in] a The first value 1932/// \param [in] b The second value 1933/// \param [in] c The third value 1934/// \returns The extend vectorized average of the two values 1935template < typename RetT ,typename AT ,typename BT > 1936inline constexpr RetT extend_vavrg4 (AT a ,BT b ,RetT c ) { 1937return detail::extend_vbinary4 < RetT , false, false> (a ,b ,c , 1938 detail::average ()); 1939} 1940 1941/// Compute vectorized average of \p a and \p b, with each value treated as a 4 1942/// elements vector type and extend each element to 9 bit. Then add each half 1943/// of the result and add with \p c. 1944/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1945/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1946/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1947/// \param [in] a The first value 1948/// \param [in] b The second value 1949/// \param [in] c The third value 1950/// \returns The addition of each half of extend vectorized average of the 1951/// two values and the third value 1952template < typename RetT ,typename AT ,typename BT > 1953inline constexpr RetT extend_vavrg4_add (AT a ,BT b ,RetT c ) { 1954return detail::extend_vbinary4 < RetT , false, true> (a ,b ,c , detail::average ()); 1955} 1956 1957/// Compute vectorized average of \p a and \p b with saturation, with each value 1958/// treated as a 4 elements vector type and extend each element to 9 bit. 1959/// \tparam [in] RetT The type of the return value, can only be 32 bit integer 1960/// \tparam [in] AT The type of the first value, can only be 32 bit integer 1961/// \tparam [in] BT The type of the second value, can only be 32 bit integer 1962/// \param [in] a The first value 1963/// \param [in] b The second value 1964/// \param [in] c The third value 1965/// \returns The extend vectorized average of the two values with saturation 1966template < typename RetT ,typename AT ,typename BT > 1967inline constexpr RetT extend_vavrg4_sat (AT a ,BT b ,RetT c ) { 1968return detail::extend_vbinary4 < RetT , true, false> (a ,b ,c , detail::average ()); 1969} 1970 1971namespace experimental { 1972namespace matrix { 1973namespace syclex= sycl::ext::oneapi::experimental; 1974struct row_major 1975 :public std::integral_constant < syclex::matrix::layout , 1976 syclex::matrix::layout::row_major > {}; 1977struct col_major 1978 :public std::integral_constant < syclex::matrix::layout , 1979 syclex::matrix::layout::col_major > {}; 1980struct a :public std::integral_constant < syclex::matrix::use , 1981 syclex::matrix::use::a > {}; 1982struct b :public std::integral_constant < syclex::matrix::use , 1983 syclex::matrix::use::b > {}; 1984struct accumulator 1985 :public std::integral_constant < syclex::matrix::use , 1986 syclex::matrix::use::accumulator > {}; 1987 1988template < class use ,int m ,int n ,int k > struct matrix_size_traits ; 1989template < int m ,int n ,int k > struct matrix_size_traits < a ,m ,n ,k > { 1990static constexpr int rows = m ; 1991static constexpr int cols = k ; 1992}; 1993 1994template < int m ,int n ,int k > struct matrix_size_traits < b ,m ,n ,k > { 1995static constexpr int rows = k ; 1996static constexpr int cols = n ; 1997}; 1998 1999template < int m ,int n ,int k > struct matrix_size_traits < accumulator ,m ,n ,k > { 2000static constexpr int rows = m ; 2001static constexpr int cols = n ; 2002}; 2003 2004// A class that wraps the syclex::matrix::joint_matrix class and provides 2005// copy constructor and assignment operator. 2006template < typename use ,int m ,int n ,int k ,typename T , 2007typename layout = std::integral_constant < 2008 syclex::matrix::layout , syclex::matrix::layout::dynamic >> 2009class joint_matrix { 2010using joint_matrix_type = syclex::matrix::joint_matrix < 2011 sycl::sub_group ,T , use::value ,matrix_size_traits < use ,m ,n ,k > ::rows , 2012matrix_size_traits < use ,m ,n ,k > ::cols , layout::value > ; 2013 2014static inline decltype(auto )get_wi_data (joint_matrix_type & matrix ) { 2015return sycl::ext::oneapi::detail::get_wi_data ( 2016 sycl::ext::oneapi::this_work_item::get_sub_group (),matrix ); 2017 } 2018 2019public : 2020joint_matrix () 2021 :matrix (),x (matrix ),num_elements (get_wi_data (matrix ).length ()) {} 2022joint_matrix (joint_matrix & other ) 2023 :x (matrix ),num_elements (get_wi_data (matrix ).length ()) { 2024 syclex::matrix::joint_matrix_copy ( 2025 sycl::ext::oneapi::this_work_item::get_sub_group (),other .get (), 2026matrix ); 2027 } 2028joint_matrix & operator= (joint_matrix & other ) { 2029if (this != & other ) { 2030 syclex::matrix::joint_matrix_copy ( 2031 sycl::ext::oneapi::this_work_item::get_sub_group (),other .get (), 2032matrix ); 2033 } 2034return * this ; 2035 } 2036 2037joint_matrix_type & get () {return matrix ; } 2038 2039const joint_matrix_type & get ()const {return matrix ; } 2040 2041class matrix_accessor { 2042friend joint_matrix ; 2043joint_matrix_type & matrix ; 2044matrix_accessor (joint_matrix_type & matrix ) :matrix (matrix ) {} 2045 2046public : 2047 decltype(auto ) operator[](unsigned I ) {return get_wi_data (matrix )[I ]; } 2048 decltype(auto ) operator[](unsigned I )const { 2049return get_wi_data (matrix )[I ]; 2050 } 2051 }; 2052 2053private : 2054joint_matrix_type matrix ; 2055 2056public : 2057matrix_accessor x ; 2058const size_t num_elements ; 2059}; 2060 2061/// Collectively loads 1 8x8 b16 (128 bytes) matrix from local memory to private 2062/// memory per sub-group. Requires the sub-group size of kernel calling this 2063/// function to be 32. 2064/// 'mat' specifies the matrix index to be loaded. The first '(mat + 1) * 8' 2065/// work items of sub-group contain the starting address of their respective 2066/// matrix row in 'addr'. 2067/// After distributing addresses to other work items, each of the 32 work items 2068/// load 32-bits (2 packed 16-bit data) into 'm' for a total of 128 bytes. 2069/// 'trans' specifies to perform a transposed/non-transposed load by each work 2070/// item like below 2071/// Row Major: Each row of the matrix is loaded by a group of 4 work items(wi) 2072/// row-0: wi0 wi0 wi1 wi1 ... wi3 wi3 2073/// row-1: wi4 wi4 wi5 wi5 ... wi7 wi7 2074/// ... 2075/// row-6: wi24 wi24 wi25 wi25 ... wi27 wi27 2076/// row-7: wi28 wi28 wi29 wi29 ... wi31 wi31 2077/// Col Major: Each col of the matrix is loaded by a group of 4 work items(wi) 2078/// row-0: wi0 wi4 wi8 ... wi28 2079/// row-1: wi0 wi4 wi8 ... wi28 2080/// ... 2081/// row-6: wi3 wi7 wi11 ... wi31 2082/// row-7: wi3 wi7 wi11 ... wi31 2083/// \tparam [in] T Type of result variable (currently only supports 16-bit type) 2084/// \param [in] addr The starting address of corresponding matrix row for a work 2085/// item in local memory 2086/// \param [in] m The private memory to store the matrix. It points to 2 b16 2087/// type elements. 2088/// \param [in] trans Indicates whether the matrix to be loaded transposed 2089/// \param [in] mat The matrix index to be loaded 2090template < typename T > 2091void ldmatrix (uintptr_t addr ,T * m ,bool trans = false,unsigned mat = 0 ) { 2092auto sg = sycl::ext::oneapi::this_work_item::get_sub_group (); 2093int lane = sg .get_local_linear_id (); 2094 2095int lane_group8_row = lane /8 ; 2096int lane_group8_col = lane %8 ; 2097 2098if (!trans ) { 2099// calculate the source lane 2100int src_lane = 2 * lane_group8_row ; 2101if (lane_group8_col >=4 ) 2102src_lane += 1 ; 2103 2104// Broadcast the address from the source lane 2105auto recv_addr_uintp = 2106 dpct::select_from_sub_group (sg ,addr ,mat * 8 + src_lane ); 2107 2108// Cast the received address from uintptr_t to the type of 'm' 2109auto recv_addr = reinterpret_cast < T *> (recv_addr_uintp ); 2110 2111// Non-transposed load 2112* m = recv_addr [lane_group8_col %4 ]; 2113 }else { 2114// calculate the source lane 2115int src_lane = (lane %4 )* 2 ; 2116 2117// Broadcast the address from the source lane 2118auto recv_addr_uintp_1 = 2119 dpct::select_from_sub_group (sg ,addr ,mat * 8 + src_lane ); 2120auto recv_addr_uintp_2 = 2121 dpct::select_from_sub_group (sg ,addr ,mat * 8 + src_lane + 1 ); 2122 2123// Cast the received address from uintptr_t to 'half *' 2124auto recv_addr_1 = reinterpret_cast < sycl::half *> (recv_addr_uintp_1 ); 2125auto recv_addr_2 = reinterpret_cast < sycl::half *> (recv_addr_uintp_2 ); 2126 2127// Transposed load 2128int index = lane /4 ; 2129 sycl::half val0 = recv_addr_1 [index ]; 2130 sycl::half val1 = recv_addr_2 [index ]; 2131 2132// Combine the two 16-bits into one 32-bit value 2133 sycl::half2 val = sycl::half2 (val0 ,val1 ); 2134* m = * reinterpret_cast < T *> (& val ); 2135 } 2136} 2137 2138/// Collectively loads 2 8x8 b16 (256 bytes) matrix from local memory to private 2139/// memory per sub-group. Requires the sub-group size of kernel calling this 2140/// function to be 32. 2141/// The first 16 work items of sub-group contain the starting address of their 2142/// respective matrix row in 'addr'. 2143/// After distributing addresses to other work items, each of the 32 work items 2144/// load 64-bits (32-bits per matrix) into 'm1' & 'm2' for a total of 256 bytes. 2145/// 'trans' specifies to perform a transposed/non-transposed load by each work 2146/// item like below 2147/// Row Major: Each row of the matrices is loaded by a group of 4 work items(wi) 2148/// row-0: wi0 wi0 wi1 wi1 ... wi3 wi3 2149/// row-1: wi4 wi4 wi5 wi5 ... wi7 wi7 2150/// ... 2151/// row-6: wi24 wi24 wi25 wi25 ... wi27 wi27 2152/// row-7: wi28 wi28 wi29 wi29 ... wi31 wi31 2153/// Col Major: Each col of the matrices is loaded by a group of 4 work items(wi) 2154/// row-0: wi0 wi4 wi8 ... wi28 2155/// row-1: wi0 wi4 wi8 ... wi28 2156/// ... 2157/// row-6: wi3 wi7 wi11 ... wi31 2158/// row-7: wi3 wi7 wi11 ... wi31 2159/// \tparam [in] T Type of result variable (currently only supports 16-bit type) 2160/// \param [in] addr The starting address of corresponding matrix row for a work 2161/// item in local memory 2162/// \param [in] m1 The private memory to store the data of 1st matrix. It points 2163/// to 2 b16 type elements. 2164/// \param [in] m2 The private memory to store the data of 2nd matrix. It points 2165/// to 2 b16 type elements. 2166/// \param [in] trans Indicates whether the matrix to be loaded transposed 2167template < typename T > 2168void ldmatrix (uintptr_t addr ,T * m1 ,T * m2 ,bool trans = false) { 2169// Load 1st matrix 2170ldmatrix (addr ,m1 ,trans ,0 ); 2171// Load 2nd matrix 2172ldmatrix (addr ,m2 ,trans ,1 ); 2173} 2174 2175/// Collectively loads 4 8x8 b16 (512 bytes) matrix from local memory to private 2176/// memory per sub-group. Requires the sub-group size of kernel calling this 2177/// function to be 32. 2178/// Each work item of sub-group contains the starting address of their 2179/// respective matrix row in 'addr'. 2180/// After distributing addresses to other work items, each of the 32 work items 2181/// load 128-bits (32-bits per matrix) into 'm1', 'm2', 'm3' & 'm4' for a total 2182/// of 512 bytes. 2183/// 'trans' specifies to perform a transposed/non-transposed load by each work 2184/// item like below 2185/// Row Major: Each row of the matrices is loaded by a group of 4 work items(wi) 2186/// row-0: wi0 wi0 wi1 wi1 ... wi3 wi3 2187/// row-1: wi4 wi4 wi5 wi5 ... wi7 wi7 2188/// ... 2189/// row-6: wi24 wi24 wi25 wi25 ... wi27 wi27 2190/// row-7: wi28 wi28 wi29 wi29 ... wi31 wi31 2191/// Col Major: Each col of the matrices is loaded by a group of 4 work items(wi) 2192/// row-0: wi0 wi4 wi8 ... wi28 2193/// row-1: wi0 wi4 wi8 ... wi28 2194/// ... 2195/// row-6: wi3 wi7 wi11 ... wi31 2196/// row-7: wi3 wi7 wi11 ... wi31 2197/// \tparam [in] T Type of result variable (currently only supports 16-bit type) 2198/// \param [in] addr The starting address of corresponding matrix row for a work 2199/// item in local memory 2200/// \param [in] m1 The private memory to store the data of 1st matrix. It points 2201/// to 2 b16 type elements. 2202/// \param [in] m2 The private memory to store the data of 2nd matrix. It points 2203/// to 2 b16 type elements. 2204/// \param [in] m3 The private memory to store the data of 3rd matrix. It points 2205/// to 2 b16 type elements. 2206/// \param [in] m4 The private memory to store the data of 4th matrix. It points 2207/// to 2 b16 type elements. 2208/// \param [in] trans Indicates whether the matrix to be loaded transposed 2209template < typename T > 2210void ldmatrix (uintptr_t addr ,T * m1 ,T * m2 ,T * m3 ,T * m4 ,bool trans = false) { 2211// Load 1st matrix 2212ldmatrix (addr ,m1 ,trans ,0 ); 2213// Load 2nd matrix 2214ldmatrix (addr ,m2 ,trans ,1 ); 2215// Load 3rd matrix 2216ldmatrix (addr ,m3 ,trans ,2 ); 2217// Load 4th matrix 2218ldmatrix (addr ,m4 ,trans ,3 ); 2219} 2220 2221/// Collectively stores 1 8x8 b16 (128 bytes) matrix from private memory to 2222/// local memory per sub-group. 2223/// Requires the sub-group size of kernel calling this function to be 32. 2224/// 'mat' specifies the matrix index to be stored. The first '(mat + 1) * 8' 2225/// work items of sub-group contain the starting address of their respective 2226/// matrix row in 'addr'. 2227/// After distributing addresses to other work items, each of the 32 work items 2228/// store 32-bits (2 packed 16-bit data) into 'm' for a total of 128 bytes. 2229/// 'trans' specifies to perform a transposed/non-transposed store by each work 2230/// item like below 2231/// Row Major: Each row of the matrix is stored by a group of 4 work items(wi) 2232/// row-0: wi0 wi0 wi1 wi1 ... wi3 wi3 2233/// row-1: wi4 wi4 wi5 wi5 ... wi7 wi7 2234/// ... 2235/// row-6: wi24 wi24 wi25 wi25 ... wi27 wi27 2236/// row-7: wi28 wi28 wi29 wi29 ... wi31 wi31 2237/// Col Major: Each col of the matrix is stored by a group of 4 work items(wi) 2238/// row-0: wi0 wi4 wi8 ... wi28 2239/// row-1: wi0 wi4 wi8 ... wi28 2240/// ... 2241/// row-6: wi3 wi7 wi11 ... wi31 2242/// row-7: wi3 wi7 wi11 ... wi31 2243/// \tparam [in] T Type of result variable (currently only supports 16-bit type) 2244/// \param [in] addr The starting address of corresponding matrix row for a work 2245/// item in local memory 2246/// \param [in] m The local memory to store the matrix. It points to 2 b16 2247/// type elements. 2248/// \param [in] trans Indicates whether the matrix to be stored transposed 2249/// \param [in] mat The matrix index to be stored 2250template < typename T > 2251void stmatrix (uintptr_t addr ,T m ,bool trans = false,unsigned mat = 0 ) { 2252auto sg = sycl::ext::oneapi::this_work_item::get_sub_group (); 2253int lane = sg .get_local_linear_id (); 2254 2255int lane_group8_row = lane /8 ; 2256int lane_group8_col = lane %8 ; 2257 2258if (!trans ) { 2259// calculate the source lane 2260int src_lane = 2 * lane_group8_row ; 2261if (lane_group8_col >=4 ) 2262src_lane += 1 ; 2263 2264// Broadcast the address from the source lane 2265auto recv_addr_uintp = 2266 dpct::select_from_sub_group (sg ,addr ,mat * 8 + src_lane ); 2267 2268// Cast the received address from uintptr_t to the type of 'm' 2269auto recv_addr = reinterpret_cast < T *> (recv_addr_uintp ); 2270 2271// Non-transposed store 2272recv_addr [lane_group8_col %4 ]= m ; 2273 }else { 2274// calculate the source lane 2275int src_lane = (lane %4 )* 2 ; 2276 2277// Broadcast the address from the source lane 2278auto recv_addr_uintp_1 = 2279 dpct::select_from_sub_group (sg ,addr ,mat * 8 + src_lane ); 2280auto recv_addr_uintp_2 = 2281 dpct::select_from_sub_group (sg ,addr ,mat * 8 + src_lane + 1 ); 2282 2283// Cast the received address from uintptr_t to 'half *' 2284auto recv_addr_1 = reinterpret_cast < sycl::half *> (recv_addr_uintp_1 ); 2285auto recv_addr_2 = reinterpret_cast < sycl::half *> (recv_addr_uintp_2 ); 2286 2287// Split the 32-bit value of 'm' into two 16-bits 2288 sycl::half * val = reinterpret_cast < sycl::half *> (& m ); 2289 2290// Transposed store 2291int index = lane /4 ; 2292recv_addr_1 [index ]= val [0 ]; 2293recv_addr_2 [index ]= val [1 ]; 2294 } 2295} 2296 2297/// Collectively stores 2 8x8 b16 (256 bytes) matrix from private memory to 2298/// local memory per sub-group. 2299/// Requires the sub-group size of kernel calling this function to be 32. 2300/// The first 16 work items of sub-group contain the starting address of their 2301/// respective matrix row in 'addr'. 2302/// After distributing addresses to other work items, each of the 32 work items 2303/// store 64-bits (32-bits per matrix) into 'm1' & 'm2' for a total of 256 2304/// bytes. 2305/// 'trans' specifies to perform a transposed/non-transposed store by each work 2306/// item like below 2307/// Row Major: Each row of the matrices is stored by a group of 4 work items(wi) 2308/// row-0: wi0 wi0 wi1 wi1 ... wi3 wi3 2309/// row-1: wi4 wi4 wi5 wi5 ... wi7 wi7 2310/// ... 2311/// row-6: wi24 wi24 wi25 wi25 ... wi27 wi27 2312/// row-7: wi28 wi28 wi29 wi29 ... wi31 wi31 2313/// Col Major: Each col of the matrices is stored by a group of 4 work items(wi) 2314/// row-0: wi0 wi4 wi8 ... wi28 2315/// row-1: wi0 wi4 wi8 ... wi28 2316/// ... 2317/// row-6: wi3 wi7 wi11 ... wi31 2318/// row-7: wi3 wi7 wi11 ... wi31 2319/// \tparam [in] T Type of result variable (currently only supports 16-bit type) 2320/// \param [in] addr The starting address of corresponding matrix row for a work 2321/// item in local memory 2322/// \param [in] m1 The local memory to store the data of 1st matrix. It points 2323/// to 2 b16 type elements. 2324/// \param [in] m2 The local memory to store the data of 2nd matrix. It points 2325/// to 2 b16 type elements. 2326/// \param [in] trans Indicates whether the matrix to be stored transposed 2327template < typename T > 2328void stmatrix (uintptr_t addr ,T m1 ,T m2 ,bool trans = false) { 2329// Store 1st matrix 2330stmatrix (addr ,m1 ,trans ,0 ); 2331// Store 2nd matrix 2332stmatrix (addr ,m2 ,trans ,1 ); 2333} 2334 2335/// Collectively stores 4 8x8 b16 (512 bytes) matrix from private memory to 2336/// local memory per sub-group. 2337/// Requires the sub-group size of kernel calling this function to be 32. 2338/// Each work item of sub-group contains the starting address of their 2339/// respective matrix row in 'addr'. 2340/// After distributing addresses to other work items, each of the 32 work items 2341/// store 128-bits (32-bits per matrix) into 'm1', 'm2', 'm3' & 'm4' for a total 2342/// of 512 bytes. 2343/// 'trans' specifies to perform a transposed/non-transposed store by each work 2344/// item like below 2345/// Row Major: Each row of the matrices is stored by a group of 4 work items(wi) 2346/// row-0: wi0 wi0 wi1 wi1 ... wi3 wi3 2347/// row-1: wi4 wi4 wi5 wi5 ... wi7 wi7 2348/// ... 2349/// row-6: wi24 wi24 wi25 wi25 ... wi27 wi27 2350/// row-7: wi28 wi28 wi29 wi29 ... wi31 wi31 2351/// Col Major: Each col of the matrices is stored by a group of 4 work items(wi) 2352/// row-0: wi0 wi4 wi8 ... wi28 2353/// row-1: wi0 wi4 wi8 ... wi28 2354/// ... 2355/// row-6: wi3 wi7 wi11 ... wi31 2356/// row-7: wi3 wi7 wi11 ... wi31 2357/// \tparam [in] T Type of result variable (currently only supports 16-bit type) 2358/// \param [in] addr The starting address of corresponding matrix row for a work 2359/// item in local memory 2360/// \param [in] m1 The local memory to store the data of 1st matrix. It points 2361/// to 2 b16 type elements. 2362/// \param [in] m2 The local memory to store the data of 2nd matrix. It points 2363/// to 2 b16 type elements. 2364/// \param [in] m3 The local memory to store the data of 3rd matrix. It points 2365/// to 2 b16 type elements. 2366/// \param [in] m4 The local memory to store the data of 4th matrix. It points 2367/// to 2 b16 type elements. 2368/// \param [in] trans Indicates whether the matrix to be stored transposed 2369template < typename T > 2370void stmatrix (uintptr_t addr ,T m1 ,T m2 ,T m3 ,T m4 ,bool trans = false) { 2371// Store 1st matrix 2372stmatrix (addr ,m1 ,trans ,0 ); 2373// Store 2nd matrix 2374stmatrix (addr ,m2 ,trans ,1 ); 2375// Store 3rd matrix 2376stmatrix (addr ,m3 ,trans ,2 ); 2377// Store 4th matrix 2378stmatrix (addr ,m4 ,trans ,3 ); 2379} 2380 2381/// A helper struct that defines the pack type for the input matrix fragments 2382/// of mma() function based on the type of input matrix fragments. 2383/// The MMAType struct is specialized for different types of input matrices. 2384/// Currently, the specialization for f16, bf16 and s8 types is defined below. 2385/// \tparam [in] T The type of the input matrix fragments 2386template < typename T > struct MMAType { 2387using PackType = uint32_t ; 2388}; 2389 2390/// Each work item of a sub-group (limited to size 32) calling this function 2391/// calculates a subset fragment for the output matrix D using MAD operation on 2392/// A, B & C matrix fragments (D = A * B + C). 2393/// Current supported shapes & types: 2394/// - m8n8k4 (f32.f16.f16.f32) 2395/// - m8n8k16 (s32.s8.s8.s32) 2396/// - m16n8k8 (f32.f16.f16.f32 & f32.bf16.bf16.f32) 2397/// - m16n8k16 (f32.f16.f16.f32 & s32.s8.s8.s32) 2398/// - m16n8k32 (s32.s8.s8.s32) 2399/// Here, m, n & k define the shapes of A, B & C matrices respectively 2400/// (A = [m x k], B = [k x n], C = [m x n]). 2401/// \tparam [in] M The rows of A, C & D matrices 2402/// \tparam [in] N The columns of B, C, D matrices 2403/// \tparam [in] K The columns & rows of A & B matrices respectively 2404/// \tparam [in] ABType The type of the input matrix (A & B) fragment 2405/// \tparam [in] CDType The type of the output matrix (C & D) fragment 2406/// \param [out] d_mat_frag The fragment of the output matrix D to store the 2407/// result of A * B + C 2408/// \param [in] a_mat_frag The fragment of the input matrix A to be multiplied 2409/// with B matrix fragment 2410/// \param [in] b_mat_frag The fragment of the input matrix B to be multiplied 2411/// with A matrix fragment 2412/// \param [in] c_mat_frag The fragment of the input matrix C to be added with 2413/// the result of A * B fragments 2414template < int M ,int N ,int K ,typename ABType ,typename CDType > 2415void mma (volatile void ** d_mat_frag ,void * a_mat_frag ,void * b_mat_frag , 2416void * c_mat_frag ) { 2417auto d = reinterpret_cast < volatile CDType **> (d_mat_frag ); 2418auto a = reinterpret_cast < typename MMAType < ABType > ::PackType *> (a_mat_frag ); 2419auto b = reinterpret_cast < typename MMAType < ABType > ::PackType *> (b_mat_frag ); 2420auto c = reinterpret_cast < CDType *> (c_mat_frag ); 2421 2422auto sg = sycl::ext::oneapi::this_work_item::get_sub_group (); 2423int lane = sg .get_local_linear_id (); 2424 2425 static_assert((M == 8 && N == 8 && K == 4 )|| (M == 8 && N == 8 && K == 16 )|| 2426 (M == 16 && N == 8 && K == 8 )|| 2427 (M == 16 && N == 8 && K == 16 )|| 2428 (M == 16 && N == 8 && K == 32 ), 2429"Unsupported MMA shape!" ); 2430 2431short row_load_offset = 4 * (lane >>2 ); 2432short col_load_offset = 8 * (lane %4 ); 2433 2434if constexpr (M == 8 && N == 8 && K == 4 ) { 2435if constexpr (std::is_floating_point_v < CDType > ) { 2436col_load_offset = row_load_offset %16 ; 2437 2438// Init D matrix with fragments of C matrix 2439* d [0 ]= c [0 ]; 2440* d [1 ]= c [1 ]; 2441* d [2 ]= c [2 ]; 2442* d [3 ]= c [3 ]; 2443* d [4 ]= c [4 ]; 2444* d [5 ]= c [5 ]; 2445* d [6 ]= c [6 ]; 2446* d [7 ]= c [7 ]; 2447 2448// Calculate the row and col offset indices to iterate through the row & 2449// col fragments of A & B matrices 2450int r_ind = (lane %2 ) ?1 :0 ; 2451int c_ind = ((lane %4 ) /2 ) ?2 :0 ; 2452 2453// Each sub-group is responsible for computing a fragment size of 8*8 2454// elements of matrix D for each of 4 MMA computations. 2455// Each work item computes 8 elements of matrix D by gathering 2456// their corresponding col & row matrix fragments of length k (4) 2457// from A & B matrices respectively using below mapping logic: 2458// row0 = (i % 4) if (lane < 16) else (i % 4) + 4 2459// col0 = (lane % 4) 2460// As each row & col fragment of A & B matrices is distributed across 2461// 4 work items, each iteration of below loop loads a partial fragment of 2462// matrix A (row) and matrix B (col) using the row & col offsets. 2463typename MMAType < ABType > ::PackType recv_a [2 ],recv_b [2 ]; 2464 2465for (int i = 0 ;i < 4 ;i ++ ) { 2466// Load partial fragment from col0 of matrix A ({a0, a1}) 2467recv_a [0 ]= dpct::select_from_sub_group (sg ,a [0 ],row_load_offset + i ); 2468// Load partial fragment from col0 of matrix A ({a2, a3}) 2469recv_a [1 ]= dpct::select_from_sub_group (sg ,a [1 ],row_load_offset + i ); 2470 2471// Load partial fragment from row0 of matrix B ({b0, b1}) 2472recv_b [0 ]= dpct::select_from_sub_group (sg ,b [0 ],col_load_offset + i ); 2473// Load partial fragment from row0 of matrix B ({b2, b3}) 2474recv_b [1 ]= dpct::select_from_sub_group (sg ,b [1 ],col_load_offset + i ); 2475 2476auto ra = reinterpret_cast < ABType *> (recv_a ); 2477auto rb = reinterpret_cast < ABType *> (recv_b ); 2478 2479// Each work item calculates a partial product of A & B matrix fragments 2480// and adds it to the corresponding D matrix fragment 2481// (for even work item indices) 2482// d0 += col0{ a0 } * row0{ b0 } 2483// d1 += col0{ a0 } * row0{ b1 } 2484// d2 += col1{ a2 } * row0{ b0 } 2485// d3 += col1{ a2 } * row0{ b1 } 2486// (for odd work item indices) 2487// d0 += col0{ a1 } * row0{ b2 } 2488// d1 += col0{ a1 } * row0{ b3 } 2489// d2 += col1{ a3 } * row0{ b2 } 2490// d3 += col1{ a3 } * row0{ b3 } 2491* d [0 ]+= static_cast < float > (ra [r_ind ])* static_cast < float > (rb [c_ind ]); 2492* d [1 ]+= 2493static_cast < float > (ra [r_ind ])* static_cast < float > (rb [c_ind + 1 ]); 2494* d [2 ]+= 2495static_cast < float > (ra [r_ind + 2 ])* static_cast < float > (rb [c_ind ]); 2496* d [3 ]+= static_cast < float > (ra [r_ind + 2 ])* 2497static_cast < float > (rb [c_ind + 1 ]); 2498 2499// Load partial fragment from row1 of matrix B ({b0, b1}) 2500recv_b [0 ]= 2501 dpct::select_from_sub_group (sg ,b [0 ],col_load_offset + i + 16 ); 2502// Load partial fragment from row1 of matrix B ({b2, b3}) 2503recv_b [1 ]= 2504 dpct::select_from_sub_group (sg ,b [1 ],col_load_offset + i + 16 ); 2505 2506// (for even work item indices) 2507// d0 += col0{ a0 } * row1{ b0 } 2508// d1 += col0{ a0 } * row1{ b1 } 2509// d2 += col1{ a2 } * row1{ b0 } 2510// d3 += col1{ a2 } * row1{ b1 } 2511// (for odd work item indices) 2512// d0 += col0{ a1 } * row1{ b2 } 2513// d1 += col0{ a1 } * row1{ b3 } 2514// d2 += col1{ a3 } * row1{ b2 } 2515// d3 += col1{ a3 } * row1{ b3 } 2516* d [4 ]+= static_cast < float > (ra [r_ind ])* static_cast < float > (rb [c_ind ]); 2517* d [5 ]+= 2518static_cast < float > (ra [r_ind ])* static_cast < float > (rb [c_ind + 1 ]); 2519* d [6 ]+= 2520static_cast < float > (ra [r_ind + 2 ])* static_cast < float > (rb [c_ind ]); 2521* d [7 ]+= static_cast < float > (ra [r_ind + 2 ])* 2522static_cast < float > (rb [c_ind + 1 ]); 2523 } 2524 } 2525 }else if constexpr (M == 8 && N == 8 && K == 16 ) { 2526if constexpr (std::is_integral_v < ABType > ) { 2527// Init D matrix with fragments of C matrix 2528* d [0 ]= c [0 ]; 2529* d [1 ]= c [1 ]; 2530 2531// Each sub-group is responsible for computing a fragment size of 16*8 2532// elements of matrix D. 2533// Each work item computes 2 elements of matrix D by gathering 2534// their corresponding row & col matrix fragments of length k (16) 2535// from A & B matrices respectively using below mapping logic: 2536// row0 = ((lane % 4) * 4) + i 2537// col0 = (lane >> 2) 2538// As each row & col fragment of A & B matrices is distributed across 2539// 4 work items, each iteration of below loop loads a partial fragment of 2540// matrix A (row) and matrix B (col) using the row & col offsets. 2541for (int i = 0 ;i < 4 ;i ++ ) { 2542typename MMAType < ABType > ::PackType recv_a ,recv_b [2 ]; 2543 2544// Load partial fragment from row0 of matrix A ({a0, a1, a2, a3}) 2545recv_a = dpct::select_from_sub_group (sg ,a [0 ],row_load_offset + i ); 2546// Load partial fragment from col0 of matrix B ({b0, b1, b2, b3}) 2547recv_b [0 ]= dpct::select_from_sub_group (sg ,b [0 ],col_load_offset + i ); 2548// Load partial fragment from col1 of matrix B ({b0, b1, b2, b3}) 2549recv_b [1 ]= 2550 dpct::select_from_sub_group (sg ,b [0 ],col_load_offset + i + 4 ); 2551 2552auto a = reinterpret_cast < ABType *> (& recv_a ); 2553auto b = reinterpret_cast < ABType *> (recv_b ); 2554 2555// Each work item calculates a partial product of A & B matrix fragments 2556// and adds it to the corresponding D matrix fragment 2557// d0 += row0{ a0, a1, a2, a3 } * col0{ b0, b1, b2, b3 } 2558// d1 += row0{ a0, a1, a2, a3 } * col1{ b0, b1, b2, b3 } 2559// d2 += row0{ a0, a1, a2, a3 } * col0{ b0, b1, b2, b3 } 2560// d3 += row0{ a0, a1, a2, a3 } * col1{ b0, b1, b2, b3 } 2561for (int j = 0 ;j < 4 ;j ++ ) { 2562* d [0 ]+= a [j ]* b [j ]; 2563* d [1 ]+= a [j ]* b [j + 4 ]; 2564 } 2565 } 2566 } 2567 }else if constexpr (M == 16 && N == 8 && K == 8 ) { 2568if constexpr (std::is_floating_point_v < CDType > ) { 2569// Init D matrix fragment with C matrix fragment 2570* d [0 ]= c [0 ]; 2571* d [1 ]= c [1 ]; 2572* d [2 ]= c [2 ]; 2573* d [3 ]= c [3 ]; 2574 2575// Each sub-group is responsible for computing a fragment size of 16*8 2576// elements of matrix D. 2577// Each work item computes 4 elements of matrix D by gathering 2578// their corresponding row & col matrix fragments of length k (8) 2579// from A & B matrices respectively using below mapping logic: 2580// row0 = (lane >> 2) & row1 = (lane >> 2) + 8 2581// col0 = (lane % 4) * 2 + (i & 0x1) 2582// As each row & col fragment of A & B matrices is distributed across 2583// 4 work items, each iteration of below loop loads a partial fragment of 2584// matrix A (row) and matrix B (col) using the row & col offsets. 2585for (int i = 0 ;i < 4 ;i ++ ) { 2586typename MMAType < ABType > ::PackType recv_a [2 ],recv_b [2 ]; 2587 2588// Load partial fragment from row0 of matrix A ({a0, a1}) 2589recv_a [0 ]= dpct::select_from_sub_group (sg ,a [0 ],row_load_offset + i ); 2590// Load partial fragment from row1 of matrix A ({a2, a3}) 2591recv_a [1 ]= dpct::select_from_sub_group (sg ,a [1 ],row_load_offset + i ); 2592// Load partial fragment from col0 of matrix B ({b0, b1}) 2593recv_b [0 ]= dpct::select_from_sub_group (sg ,b [0 ],col_load_offset + i ); 2594// Load partial fragment from col1 of matrix B ({b0, b1}) 2595recv_b [1 ]= 2596 dpct::select_from_sub_group (sg ,b [0 ],col_load_offset + i + 4 ); 2597 2598auto ra = reinterpret_cast < ABType *> (recv_a ); 2599auto rb = reinterpret_cast < ABType *> (recv_b ); 2600 2601// Each work item calculates a partial product of A & B matrix fragments 2602// and adds it to the corresponding D matrix fragment 2603// d0 += row0{ a0, a1 } * col0{ b0, b1 } 2604// d1 += row0{ a0, a1 } * col1{ b0, b1 } 2605// d2 += row1{ a2, a3 } * col0{ b0, b1 } 2606// d3 += row1{ a2, a3 } * col1{ b0, b1 } 2607for (int j = 0 ;j < 2 ;j ++ ) { 2608* d [0 ]+= static_cast < float > (ra [j ])* static_cast < float > (rb [j ]); 2609* d [1 ]+= static_cast < float > (ra [j ])* static_cast < float > (rb [j + 2 ]); 2610* d [2 ]+= static_cast < float > (ra [j + 2 ])* static_cast < float > (rb [j ]); 2611* d [3 ]+= 2612static_cast < float > (ra [j + 2 ])* static_cast < float > (rb [j + 2 ]); 2613 } 2614 } 2615 } 2616 }else if constexpr (M == 16 && N == 8 && K == 16 ) { 2617if constexpr (std::is_floating_point_v < CDType > ) { 2618// Init D matrix fragment with C matrix fragment 2619* d [0 ]= c [0 ]; 2620* d [1 ]= c [1 ]; 2621* d [2 ]= c [2 ]; 2622* d [3 ]= c [3 ]; 2623 2624// Each sub-group is responsible for computing a fragment size of 16*8 2625// elements of matrix D. 2626// Each work item computes 4 elements of matrix D by gathering 2627// their corresponding row & col matrix fragments of length k (8) 2628// from A & B matrices respectively using below mapping logic: 2629// row0 = (lane >> 2) & row1 = (lane >> 2) + 8 2630// col0 = (lane % 4) * 2 & col1 = (lane % 4) * 2 + 1 2631// As each row & col fragment of A & B matrices is distributed across 2632// 4 work items, each iteration of below loop loads a partial fragment of 2633// matrix A (row) and matrix B (col) using the row & col offsets. 2634for (int i = 0 ;i < 4 ;i ++ ) { 2635typename MMAType < ABType > ::PackType recv_a [4 ],recv_b [4 ]; 2636 2637// Load partial fragment from row0 of matrix A ({a0, a1}) 2638recv_a [0 ]= dpct::select_from_sub_group (sg ,a [0 ],row_load_offset + i ); 2639// Load partial fragment from row0 of matrix A ({a2, a3}) 2640recv_a [1 ]= dpct::select_from_sub_group (sg ,a [2 ],row_load_offset + i ); 2641// Load partial fragment from row1 of matrix A ({a0, a1}) 2642recv_a [2 ]= dpct::select_from_sub_group (sg ,a [1 ],row_load_offset + i ); 2643// Load partial fragment from row1 of matrix A ({a2, a3}) 2644recv_a [3 ]= dpct::select_from_sub_group (sg ,a [3 ],row_load_offset + i ); 2645 2646// Load partial fragment from col0 of matrix B ({b0, b1}) 2647recv_b [0 ]= dpct::select_from_sub_group (sg ,b [0 ],col_load_offset + i ); 2648// Load partial fragment from col0 of matrix B ({b2, b3}) 2649recv_b [1 ]= dpct::select_from_sub_group (sg ,b [1 ],col_load_offset + i ); 2650// Load partial fragment from col1 of matrix B ({b0, b1}) 2651recv_b [2 ]= 2652 dpct::select_from_sub_group (sg ,b [0 ],col_load_offset + 4 + i ); 2653// Load partial fragment from col1 of matrix B ({b2, b3}) 2654recv_b [3 ]= 2655 dpct::select_from_sub_group (sg ,b [1 ],col_load_offset + 4 + i ); 2656 2657auto ra = reinterpret_cast < ABType *> (recv_a ); 2658auto rb = reinterpret_cast < ABType *> (recv_b ); 2659 2660// Each work item calculates a partial product of A & B matrix fragments 2661// and adds it to the corresponding D matrix fragment 2662// d0 += row0{ a0, a1, a2, a3 } * col0{ b0, b1, b2, b3 } 2663// d1 += row0{ a0, a1, a2, a3 } * col1{ b0, b1, b2, b3 } 2664// d2 += row1{ a0, a1, a2, a3 } * col0{ b0, b1, b2, b3 } 2665// d3 += row1{ a0, a1, a2, a3 } * col1{ b0, b1, b2, b3 } 2666for (int j = 0 ;j < 4 ;j ++ ) { 2667* d [0 ]+= static_cast < CDType > (ra [j ])* static_cast < CDType > (rb [j ]); 2668* d [1 ]+= static_cast < CDType > (ra [j ])* static_cast < CDType > (rb [j + 4 ]); 2669* d [2 ]+= static_cast < CDType > (ra [j + 4 ])* static_cast < CDType > (rb [j ]); 2670* d [3 ]+= 2671static_cast < CDType > (ra [j + 4 ])* static_cast < CDType > (rb [j + 4 ]); 2672 } 2673 } 2674 }else if constexpr (std::is_integral_v < ABType > ) { 2675// Init D matrix with fragments of C matrix 2676* d [0 ]= c [0 ]; 2677* d [1 ]= c [1 ]; 2678* d [2 ]= c [2 ]; 2679* d [3 ]= c [3 ]; 2680 2681// Each sub-group is responsible for computing a fragment size of 16*8 2682// elements of matrix D. 2683// Each work item computes 4 elements of matrix D by gathering 2684// their corresponding row & col matrix fragments of length k (8) 2685// from A & B matrices respectively using below mapping logic: 2686// row0 = (lane >> 2) & row1 = (lane >> 2) + 8 2687// col0 = (lane % 4) * 2 & col1 = (lane % 4) * 2 + 1 2688// As each row & col fragment of A & B matrices is distributed across 2689// 4 work items, each iteration of below loop loads a partial fragment of 2690// matrix A (row) and matrix B (col) using the row & col offsets. 2691for (int i = 0 ;i < 4 ;i ++ ) { 2692typename MMAType < ABType > ::PackType recv_a [2 ],recv_b [2 ]; 2693 2694// Load partial fragment from row0 of matrix A ({a0, a1, a2, a3}) 2695recv_a [0 ]= dpct::select_from_sub_group (sg ,a [0 ],row_load_offset + i ); 2696// Load partial fragment from row1 of matrix A ({a4, a5, a6, a7}) 2697recv_a [1 ]= dpct::select_from_sub_group (sg ,a [1 ],row_load_offset + i ); 2698// Load partial fragment from col0 of matrix B ({b0, b1, b2, b3}) 2699recv_b [0 ]= dpct::select_from_sub_group (sg ,b [0 ],col_load_offset + i ); 2700// Load partial fragment from col1 of matrix B ({b4, b5, b6, b7}) 2701recv_b [1 ]= 2702 dpct::select_from_sub_group (sg ,b [0 ],col_load_offset + i + 4 ); 2703 2704auto ra = reinterpret_cast < ABType *> (recv_a ); 2705auto rb = reinterpret_cast < ABType *> (recv_b ); 2706 2707// Each work item calculates a partial product of A & B matrix fragments 2708// and adds it to the corresponding D matrix fragment 2709// d0 += row0{ a0, a1, a2, a3 } * col0{ b0, b1, b2, b3 } 2710// d1 += row0{ a0, a1, a2, a3 } * col1{ b4, b5, b6, b7 } 2711// d2 += row1{ a4, a5, a6, a7 } * col0{ b0, b1, b2, b3 } 2712// d3 += row1{ a4, a5, a6, a7 } * col1{ b4, b5, b6, b7 } 2713for (int i = 0 ;i < 4 ;i ++ ) { 2714* d [0 ]+= ra [i ]* rb [i ]; 2715* d [1 ]+= ra [i ]* rb [i + 4 ]; 2716* d [2 ]+= ra [i + 4 ]* rb [i ]; 2717* d [3 ]+= ra [i + 4 ]* rb [i + 4 ]; 2718 } 2719 } 2720 } 2721 }else if constexpr (M == 16 && N == 8 && K == 32 ) { 2722if constexpr (std::is_integral_v < ABType > ) { 2723// Init D matrix with fragments of C matrix 2724* d [0 ]= c [0 ]; 2725* d [1 ]= c [1 ]; 2726* d [2 ]= c [2 ]; 2727* d [3 ]= c [3 ]; 2728 2729// Each sub-group is responsible for computing a fragment size of 16*8 2730// elements of matrix D. 2731// Each work item computes 4 elements of matrix D by gathering 2732// their corresponding row & col matrix fragments of length k (32) 2733// from A & B matrices respectively using below mapping logic: 2734// row0 = (lane >> 2) & row1 = (lane >> 2) + 8 2735// col0 = ((lane % 4) * 4) + (i & 0x3) & col1 = ((lane % 4) * 4) + (i & 2736// 0x3) As each row & col fragment of A & B matrices is distributed across 2737// 4 work items, each iteration of below loop loads a partial fragment of 2738// matrix A (row) and matrix B (col) using the row & col offsets. 2739for (int i = 0 ;i < 4 ;i ++ ) { 2740typename MMAType < ABType > ::PackType recv_a [2 ],recv_b [2 ]; 2741 2742// Load partial fragment from row0 of matrix A ({a0, a1, a2, a3}) 2743recv_a [0 ]= dpct::select_from_sub_group (sg ,a [0 ],row_load_offset + i ); 2744// Load partial fragment from row1 of matrix A ({a4, a5, a6, a7}) 2745recv_a [1 ]= dpct::select_from_sub_group (sg ,a [1 ],row_load_offset + i ); 2746// Load partial fragment from col0 of matrix B ({b0, b1, b2, b3}) 2747recv_b [0 ]= dpct::select_from_sub_group (sg ,b [0 ],col_load_offset + i ); 2748// Load partial fragment from col1 of matrix B ({b0, b1, b2, b3}) 2749recv_b [1 ]= 2750 dpct::select_from_sub_group (sg ,b [0 ],col_load_offset + i + 4 ); 2751 2752auto a = reinterpret_cast < ABType *> (recv_a ); 2753auto b = reinterpret_cast < ABType *> (recv_b ); 2754 2755// Each work item calculates a partial product of A & B matrix fragments 2756// and adds it to the corresponding D matrix fragment 2757// d0 += row0{ a0, a1, a2, a3 } * col0{ b0, b1, b2, b3 } 2758// d1 += row0{ a0, a1, a2, a3 } * col1{ b0, b1, b2, b3 } 2759// d2 += row1{ a4, a5, a6, a7 } * col0{ b0, b1, b2, b3 } 2760// d3 += row1{ a4, a5, a6, a7 } * col1{ b0, b1, b2, b3 } 2761for (int j = 0 ;j < 4 ;j ++ ) { 2762* d [0 ]+= a [j ]* b [j ]; 2763* d [1 ]+= a [j ]* b [j + 4 ]; 2764* d [2 ]+= a [j + 4 ]* b [j ]; 2765* d [3 ]+= a [j + 4 ]* b [j + 4 ]; 2766 } 2767 } 2768 2769for (int i = 0 ;i < 4 ;i ++ ) { 2770typename MMAType < ABType > ::PackType recv_a [2 ],recv_b [2 ]; 2771 2772// Load partial fragment from row0 of matrix A ({a8, a9, a10, a11}) 2773recv_a [0 ]= dpct::select_from_sub_group (sg ,a [2 ],row_load_offset + i ); 2774// Load partial fragment from row1 of matrix A ({a12, a13, a14, a15}) 2775recv_a [1 ]= dpct::select_from_sub_group (sg ,a [3 ],row_load_offset + i ); 2776// Load partial fragment from col0 of matrix B ({b4, b5, b6, b7}) 2777recv_b [0 ]= dpct::select_from_sub_group (sg ,b [1 ],col_load_offset + i ); 2778// Load partial fragment from col1 of matrix B ({b4, b5, b6, b7}) 2779recv_b [1 ]= 2780 dpct::select_from_sub_group (sg ,b [1 ],col_load_offset + i + 4 ); 2781 2782auto a = reinterpret_cast < ABType *> (recv_a ); 2783auto b = reinterpret_cast < ABType *> (recv_b ); 2784 2785// Each work item calculates a partial product of A & B matrix fragments 2786// and adds it to the corresponding D matrix fragment 2787// d0 += row0{ a8, a9, a10, a11 } * col0{ b4, b5, b6, b7 } 2788// d1 += row0{ a8, a9, a10, a11 } * col1{ b4, b5, b6, b7 } 2789// d2 += row1{ a12, a13, a14, a15 } * col0{ b4, b5, b6, b7 } 2790// d3 += row1{ a12, a13, a14, a15 } * col1{ b4, b5, b6, b7 } 2791for (int j = 0 ;j < 4 ;j ++ ) { 2792* d [0 ]+= a [j ]* b [j ]; 2793* d [1 ]+= a [j ]* b [j + 4 ]; 2794* d [2 ]+= a [j + 4 ]* b [j ]; 2795* d [3 ]+= a [j + 4 ]* b [j + 4 ]; 2796 } 2797 } 2798 } 2799 } 2800} 2801 2802}// namespace matrix 2803}// namespace experimental 2804 2805}// namespace dpct 2806 2807#endif // __DPCT_MATH_HPP__