Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <stdio.h>
- #include <time.h>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <smmintrin.h>
- typedef unsigned int UINT32;
- UINT32 GetIP(const char *p)
- {
- UINT32 dwIP=0,dwIP_Part=0;
- while(true)
- {
- if(p[0] == 0)
- {
- dwIP = (dwIP << 8) | dwIP_Part;
- break;
- }
- if(p[0]=='.')
- {
- dwIP = (dwIP << 8) | dwIP_Part;
- dwIP_Part = 0;
- p++;
- }
- dwIP_Part = (dwIP_Part*10)+(p[0]-'0');
- p++;
- }
- return dwIP;
- }
- //__m128i shuffleTable[65536];/* DEEP MAGIC BEGINS HERE. */
- return (unsigned)_mm_crc32_u32(0,v*0x000020de)>>24;
- }
- UINT32 MyGetIP(const char *str) {
- __m128i input = _mm_lddqu_si128((const __m128i*)str); //"192.167.1.3"
- // for (int i = 0; i < 16; i++) printf("%c ", input.m128i_u8[i]); printf("\n");
- __m128i z = _mm_cmpeq_epi8(input, _mm_setzero_si128());
- int zfill = 31-__builtin_ctz(_mm_movemask_epi8(z));
- input = _mm_sub_epi8(input, _mm_set1_epi8('0')); //1 9 2 254 1 6 7 254 1 254 3 208 245 0 8 40
- // for (int i = 0; i < 16; i++) printf("%d ", int(input.m128i_u8[i])); printf("\n");
- __m128i cmp = input; //...X...X.X.XX... (signs)
- // for (int i = 0; i < 16; i++) printf("%c", cmp.m128i_i8[i] < 0 ? 'X' : '.'); printf("\n");
- //uint64_t mask;
- //asm("rex.w pmovmskb %1, %0\n\t" : "=r"(mask) : "x"(cmp));
- uint64_t mask = _mm_movemask_epi8(cmp); //6792 - magic index
- mask &= 0xFFFFFFFFU >> zfill;
- uint64_t hashmask = perfecthash(mask);
- // printf("%d\n", mask);
- __m128i shuf = shuffleTable[hashmask]; //10 -1 -1 -1 8 -1 -1 -1 6 5 4 -1 2 1 0 -1
- // for (int i = 0; i < 16; i++) printf("%d ", int(shuf.m128i_i8[i])); printf("\n");
- __m128i arr = _mm_shuffle_epi8(input, shuf); //3 0 0 0 | 1 0 0 0 | 7 6 1 0 | 2 9 1 0
- // for (int i = 0; i < 16; i++) printf("%d ", int(arr.m128i_u8[i])); printf("\n");
- __m128i coeffs = _mm_set_epi8(0, 100, 10, 1, 0, 100, 10, 1, 0, 100, 10, 1, 0, 100, 10, 1);
- __m128i prod = _mm_maddubs_epi16(coeffs, arr); //3 0 | 1 0 | 67 100 | 92 100
- // for (int i = 0; i < 8; i++) printf("%d ", int(prod.m128i_u16[i])); printf("\n");
- prod = _mm_hadd_epi16(prod, prod); //3 | 1 | 167 | 192 | ? | ? | ? | ?
- // for (int i = 0; i < 4; i++) printf("%d ", int(prod.m128i_u16[i])); printf("\n");
- __m128i imm = _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 6, 4, 2, 0);
- prod = _mm_shuffle_epi8(prod, imm); //3 1 167 192 0 0 0 0 0 0 0 0 0 0 0 0
- // for (int i = 0; i < 4; i++) printf("%d ", int(prod.m128i_u8[i])); printf("\n");
- return _mm_extract_epi32(prod, 0);
- // return (UINT32(_mm_extract_epi16(prod, 1)) << 16) + UINT32(_mm_extract_epi16(prod, 0)); //no SSE 4.1
- }
- static const int CNT = 16<<10;
- int main() {
- printf("%08X\n", MyGetIP("192.167.1.3"));
- // return 0;
- std::vector<std::string> samples;
- for (int i = 0; i < CNT; i++) {
- std::string str;
- for (int j = 0; j < 4; j++) {
- if (j) str += '.';
- int x = rand() % 256;
- char qq[16];
- sprintf(qq, "%d", x);
- str += qq;
- }
- samples.push_back(str);
- // printf("%s\n", samples[i].c_str());
- auto a = GetIP(samples.back().c_str());
- auto b = MyGetIP(samples.back().c_str());
- if (a != b)
- printf("%s: %08X vs %08X\n", samples[i].c_str(), a, b);
- }
- {
- int start = clock();
- int sum = 0;
- for (int i = 0; i < 1<<27; i++) {
- const char *input = samples[i & (CNT-1)].c_str();
- sum += MyGetIP(input);
- }
- int elapsed = clock() - start;
- printf("Time = %0.3lf (%d)\n", double(elapsed) / CLOCKS_PER_SEC, sum);
- }
- {
- int start = clock();
- int sum = 0;
- for (int i = 0; i < 1<<27; i++) {
- const char *input = samples[i & (CNT-1)].c_str();
- sum += GetIP(input);
- }
- int elapsed = clock() - start;
- printf("Time = %0.3lf (%d)\n", double(elapsed) / CLOCKS_PER_SEC, sum);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement