You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

gemm_common.c 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #ifndef GEMM_COMMON_C
  2. #define GEMM_COMMON_C
  3. #include "common.h"
  4. #include <altivec.h>
  5. #include <inttypes.h>
  6. #define NBMAX 4096
  7. #define FORCEINLINE inline __attribute__((always_inline))
  8. #ifdef _ARCH_PWR10
  9. #ifdef __has_builtin
  10. #if !__has_builtin(__builtin_vsx_assemble_pair)
  11. #define __builtin_vsx_assemble_pair __builtin_mma_assemble_pair
  12. #endif
  13. #if !__has_builtin(__builtin_vsx_disassemble_pair)
  14. #define __builtin_vsx_disassemble_pair __builtin_mma_disassemble_pair
  15. #endif
  16. #endif
  17. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  18. #define __builtin_vsx_assemble_pair2(vp0, v0, v1) __builtin_vsx_assemble_pair(vp0, v1, v0)
  19. #else
  20. #define __builtin_vsx_assemble_pair2(vp0, v0, v1) __builtin_vsx_assemble_pair(vp0, v0, v1)
  21. #endif
  22. #define USE_VECTOR_PAIRS
  23. #endif
  24. typedef __vector IFLOAT vec_bf16;
  25. typedef __vector FLOAT vec_f32;
  26. typedef __vector unsigned char vec_uc8;
  27. FORCEINLINE vec_uc8 vec_load_vec(void *src)
  28. {
  29. return vec_xl(0, (unsigned char *)(src));
  30. }
  31. FORCEINLINE void vec_load_pair(vec_f32 *dst, vec_f32 *src)
  32. {
  33. #ifdef USE_VECTOR_PAIRS
  34. __vector_pair vy0p;
  35. #ifdef __clang__
  36. vy0p = __builtin_vsx_lxvp(0L, (const __vector_pair *)(src));
  37. #else
  38. vy0p = *(__vector_pair *)(src);
  39. #endif
  40. __builtin_vsx_disassemble_pair((void *)(dst), &vy0p);
  41. #else
  42. dst[0] = src[0];
  43. dst[1] = src[1];
  44. #endif
  45. }
  46. FORCEINLINE void vec_store_pair(vec_f32 *dst, vec_f32 *src)
  47. {
  48. #ifdef USE_VECTOR_PAIRS
  49. __vector_pair vy0p;
  50. __builtin_vsx_assemble_pair2(&vy0p, (vec_uc8)src[1], (vec_uc8)src[0]);
  51. #ifdef __clang__
  52. __builtin_vsx_stxvp(vy0p, 0L, (__vector_pair *)(dst));
  53. #else
  54. *(__vector_pair *)(dst) = vy0p;
  55. #endif
  56. #else
  57. dst[0] = src[0];
  58. dst[1] = src[1];
  59. #endif
  60. }
  61. FORCEINLINE vec_bf16 vec_loadN(void *src, BLASLONG n)
  62. {
  63. IFLOAT *src2 = (IFLOAT *)(src);
  64. #ifdef _ARCH_PWR9
  65. return vec_xl_len(src2, n * sizeof(IFLOAT));
  66. #else
  67. __attribute__((aligned(16))) IFLOAT data[sizeof(vec_bf16) / sizeof(IFLOAT)];
  68. memset(data, 0, sizeof(vec_bf16));
  69. if (n & 4) {
  70. memcpy(data, src2, sizeof(uint64_t));
  71. }
  72. if (n & 2) {
  73. BLASLONG n4 = n & 4;
  74. memcpy(data + n4, src2 + n4, sizeof(uint32_t));
  75. }
  76. if (n & 1) {
  77. BLASLONG n6 = n & 6;
  78. data[n6] = src2[n6];
  79. }
  80. return (vec_bf16)vec_load_vec(data);
  81. #endif
  82. }
  83. FORCEINLINE vec_f32 vec_loadN_f32(void *src, BLASLONG n)
  84. {
  85. #ifndef _ARCH_PWR9
  86. if (n & 4) {
  87. return (vec_f32)vec_load_vec(src);
  88. }
  89. #endif
  90. return (vec_f32)vec_loadN(src, n * (sizeof(FLOAT) / sizeof(IFLOAT)));
  91. }
  92. FORCEINLINE void vec_loadN2_f32(vec_f32 *data, vec_f32 *src, BLASLONG n)
  93. {
  94. data[0] = src[0];
  95. data[1] = vec_loadN_f32(&src[1], n);
  96. }
  97. FORCEINLINE void vec_storeN(vec_bf16 data, void *dst, BLASLONG n)
  98. {
  99. IFLOAT *dst2 = (IFLOAT *)(dst);
  100. #ifdef _ARCH_PWR9
  101. vec_xst_len(data, dst2, n * sizeof(IFLOAT));
  102. #else
  103. if (n & 8) {
  104. vec_xst(data, 0, dst2);
  105. return;
  106. }
  107. __attribute__((aligned(16))) IFLOAT data2[sizeof(vec_f32) / sizeof(IFLOAT)];
  108. vec_xst(data, 0, data2);
  109. if (n & 4) {
  110. memcpy(dst2, data2, sizeof(uint64_t));
  111. }
  112. if (n & 2) {
  113. BLASLONG n4 = n & 4;
  114. memcpy(dst2 + n4, data2 + n4, sizeof(uint32_t));
  115. }
  116. if (n & 1) {
  117. BLASLONG n6 = n & 6;
  118. dst2[n6] = data2[n6];
  119. }
  120. #endif
  121. }
  122. FORCEINLINE void vec_storeN_f32(vec_f32 data, void *dst, BLASLONG n)
  123. {
  124. #ifndef _ARCH_PWR9
  125. if (n & 4) {
  126. vec_xst(data, 0, (FLOAT *)dst);
  127. return;
  128. }
  129. #endif
  130. return vec_storeN((vec_bf16)data, dst, n * (sizeof(FLOAT) / sizeof(IFLOAT)));
  131. }
  132. FORCEINLINE void vec_storeN2_f32(vec_f32 *data, vec_f32 *dst, BLASLONG n)
  133. {
  134. dst[0] = data[0];
  135. vec_storeN_f32(data[1], &dst[1], n);
  136. }
  137. #endif