Created
January 30, 2014 04:08
-
-
Save sneves/8702353 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
SSSE3/XOP bit reversal | |
Written in 2014 by Samuel Neves <[email protected]> | |
To the extent possible under law, the author(s) have dedicated all copyright | |
and related and neighboring rights to this software to the public domain | |
worldwide. This software is distributed without any warranty. | |
You should have received a copy of the CC0 Public Domain Dedication along with | |
this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. | |
*/ | |
#ifndef BITREV_H | |
#define BITREV_H | |
#include <x86intrin.h> | |
/* | |
Reverse the bit order of an entire XMM register | |
*/ | |
static inline __m128i bitrev(__m128i x) | |
{ | |
/* Based on code by Solar Designer */ | |
/* http://permalink.gmane.org/gmane.comp.security.phc/773 */ | |
const __m128i sel = _mm_set_epi8(0x4F, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A, 0x49, 0x48, | |
0x47, 0x46, 0x45, 0x44, 0x43, 0x42, 0x41, 0x40); | |
#if defined(__XOP__) | |
return _mm_perm_epi8(x, x, sel); | |
#elif defined(__SSSE3__) | |
const __m128i rev0 = _mm_set_epi8(0x0F, 0x07, 0x0B, 0x03, 0x0D, 0x05, 0x09, 0x01, | |
0x0E, 0x06, 0x0A, 0x02, 0x0C, 0x04, 0x08, 0x00); | |
const __m128i rev1 = _mm_set_epi8(0xF0, 0x70, 0xB0, 0x30, 0xD0, 0x50, 0x90, 0x10, | |
0xE0, 0x60, 0xA0, 0x20, 0xC0, 0x40, 0x80, 0x00); | |
const __m128i c0f = _mm_set1_epi8(0x0F); | |
__m128i t1, t2, t3; | |
t1 = _mm_shuffle_epi8(x, sel); /* reverse bytes */ | |
t2 = _mm_and_si128(t1, c0f); /* x & 0xf */ | |
t3 = _mm_srli_epi32(_mm_andnot_si128(c0f, t1), 4); /* x & 0xF0 >> 4 */ | |
t2 = _mm_shuffle_epi8(rev1, t2); /* rev(x) << 4 */ | |
t3 = _mm_shuffle_epi8(rev0, t3); /* rev(x) */ | |
return _mm_or_si128(t2, t3); /* combine 4-bit halves */ | |
#else | |
#error At least SSSE3 is required | |
#endif | |
} | |
#endif /* BITREV_H */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment