Advertisement
Guest User

Untitled

a guest
Feb 10th, 2021
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stddef.h>
  3. #include <emmintrin.h>
  4. #include <tmmintrin.h>
  5.  
  6. /// Aligned search max item
  7. static inline uint8_t search_max_octet_aligned(const __m128i *src, size_t length) {
  8.     __m128i vmax = _mm_set_epi32(0, 0, 0, 0);
  9.     __m128i mj;
  10.     while (length --> 0) {
  11.         mj   = _mm_load_si128(src++);
  12.         vmax = _mm_max_epu8  (vmax, mj);
  13.     }
  14.     vmax = _mm_max_epu8(vmax, _mm_alignr_epi8(vmax, vmax, 1));
  15.     vmax = _mm_max_epu8(vmax, _mm_alignr_epi8(vmax, vmax, 2));
  16.     vmax = _mm_max_epu8(vmax, _mm_alignr_epi8(vmax, vmax, 4));
  17.     vmax = _mm_max_epu8(vmax, _mm_alignr_epi8(vmax, vmax, 8));
  18.     return _mm_extract_epi16(vmax, 0) & 0xFF;
  19. }
  20.  
  21. uint8_t search_max_octet(const uint8_t *src, size_t length) {
  22.     uint8_t result = 0;
  23.     while (length && (((uintptr_t)src) & 0xF)) {
  24.        result = result < src[0] ? src[0] : result;
  25.        src++;
  26.        length--;
  27.     }
  28.     if (length > 16) {
  29.        size_t rl = length & ~0xF;
  30.        result = search_max_octet_aligned((const __m128i *)src, rl >> 16);
  31.        src += rl;
  32.        length -= rl;
  33.     }
  34.     for (unsigned i = 0; i < length; i++)
  35.        result = result < src[i] ? src[i] : result;
  36.     return result;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement