dec_ssse3.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // If we have SSSE3 support, pick off 16 bytes at a time for as long as we can,
  2. // but make sure that we quit before seeing any == markers at the end of the
  3. // string. Also, because we write four zeroes at the end of the output, ensure
  4. // that there are at least 6 valid bytes of input data remaining to close the
  5. // gap. 16 + 2 + 6 = 24 bytes:
  6. while (srclen >= 24)
  7. {
  8. // Load string:
  9. __m128i str = _mm_loadu_si128((__m128i *)c);
  10. // The input consists of six character sets in the Base64 alphabet,
  11. // which we need to map back to the 6-bit values they represent.
  12. // There are three ranges, two singles, and then there's the rest.
  13. //
  14. // # From To Add Characters
  15. // 1 [43] [62] +19 +
  16. // 2 [47] [63] +16 /
  17. // 3 [48..57] [52..61] +4 0..9
  18. // 4 [65..90] [0..25] -65 A..Z
  19. // 5 [97..122] [26..51] -71 a..z
  20. // (6) Everything else => invalid input
  21. const __m128i set1 = CMPEQ(str, '+');
  22. const __m128i set2 = CMPEQ(str, '/');
  23. const __m128i set3 = RANGE(str, '0', '9');
  24. const __m128i set4 = RANGE(str, 'A', 'Z');
  25. const __m128i set5 = RANGE(str, 'a', 'z');
  26. const __m128i set6 = CMPEQ(str, '-');
  27. const __m128i set7 = CMPEQ(str, '_');
  28. __m128i delta = REPLACE(set1, 19);
  29. delta = _mm_or_si128(delta, REPLACE(set2, 16));
  30. delta = _mm_or_si128(delta, REPLACE(set3, 4));
  31. delta = _mm_or_si128(delta, REPLACE(set4, -65));
  32. delta = _mm_or_si128(delta, REPLACE(set5, -71));
  33. delta = _mm_or_si128(delta, REPLACE(set6, 17));
  34. delta = _mm_or_si128(delta, REPLACE(set7, -32));
  35. // Check for invalid input: if any of the delta values are zero,
  36. // fall back on bytewise code to do error checking and reporting:
  37. if (_mm_movemask_epi8(CMPEQ(delta, 0))) {
  38. break;
  39. }
  40. // Now simply add the delta values to the input:
  41. str = _mm_add_epi8(str, delta);
  42. // Reshuffle the input to packed 12-byte output format:
  43. str = dec_reshuffle(str);
  44. // Store back:
  45. _mm_storeu_si128((__m128i *)o, str);
  46. c += 16;
  47. o += 12;
  48. outl += 12;
  49. srclen -= 16;
  50. }