LCOV - code coverage report
Current view: top level - cmdline - murmur3.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 70 70 100.0 %
Date: 2015-05-12 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2011 Andrea Mazzoleni
       3             :  *
       4             :  * This program is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 3 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * This program is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : /*
      19             :  * Derivative work from MurmorHash3.cpp revision r136
      20             :  *
      21             :  * SMHasher & MurmurHash
      22             :  * http://code.google.com/p/smhasher/
      23             :  *
      24             :  * Exact source used as reference:
      25             :  * http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp?spec=svn136&r=136
      26             :  */
      27             : 
      28             : // MurmurHash3 was written by Austin Appleby, and is placed in the public
      29             : // domain. The author hereby disclaims copyright to this source code.
      30             : 
      31             : /* Finalization mix - force all bits of a hash block to avalanche */
      32     8699708 : static inline uint32_t fmix32(uint32_t h)
      33             : {
      34     8699708 :         h ^= h >> 16;
      35     8699708 :         h *= 0x85ebca6b;
      36     8699708 :         h ^= h >> 13;
      37     8699708 :         h *= 0xc2b2ae35;
      38     8699708 :         h ^= h >> 16;
      39     8699708 :         return h;
      40             : }
      41             : 
      42             : /*
      43             :  * Warning!
      44             :  * Don't declare these variables static, otherwise the gcc optimizer
      45             :  * may generate very slow code for multiplication with these constants,
      46             :  * like:
      47             : 
      48             :    -> .cpp
      49             :    k1 *= c1;
      50             :    -> .asm
      51             :    152:   8d 14 80                lea    (%eax,%eax,4),%edx
      52             :    155:   8d 14 90                lea    (%eax,%edx,4),%edx
      53             :    158:   c1 e2 03                shl    $0x3,%edx
      54             :    15b:   29 c2                   sub    %eax,%edx
      55             :    15d:   8d 14 d2                lea    (%edx,%edx,8),%edx
      56             :    160:   8d 14 90                lea    (%eax,%edx,4),%edx
      57             :    163:   8d 14 d0                lea    (%eax,%edx,8),%edx
      58             :    166:   8d 14 90                lea    (%eax,%edx,4),%edx
      59             :    169:   8d 14 50                lea    (%eax,%edx,2),%edx
      60             :    16c:   8d 14 90                lea    (%eax,%edx,4),%edx
      61             :    16f:   8d 14 92                lea    (%edx,%edx,4),%edx
      62             :    172:   8d 14 50                lea    (%eax,%edx,2),%edx
      63             :    175:   8d 04 d0                lea    (%eax,%edx,8),%eax
      64             :    178:   8d 14 c5 00 00 00 00    lea    0x0(,%eax,8),%edx
      65             :    17f:   29 d0                   sub    %edx,%eax
      66             : 
      67             :  * resulting in speeds of 500 MB/s instead of 3000 MB/s.
      68             :  *
      69             :  * Verified with gcc 4.4.4 compiling with :
      70             :  *
      71             :  * g++ -g -c -O2 MurmurHash3.cpp -o MurmurHash3.o
      72             :  */
      73             : uint32_t c1 = 0x239b961b;
      74             : uint32_t c2 = 0xab0e9789;
      75             : uint32_t c3 = 0x38b34ae5;
      76             : uint32_t c4 = 0xa1e38b93;
      77             : 
      78     2174927 : void MurmurHash3_x86_128(const void* data, size_t size, const uint8_t* seed, void* digest)
      79             : {
      80             :         size_t nblocks;
      81             :         const uint32_t* blocks;
      82             :         const uint32_t* end;
      83             :         size_t remainder;
      84             : 
      85     2174927 :         uint32_t h1 = ((const uint32_t*)seed)[0];
      86     2174927 :         uint32_t h2 = ((const uint32_t*)seed)[1];
      87     2174927 :         uint32_t h3 = ((const uint32_t*)seed)[2];
      88     2174927 :         uint32_t h4 = ((const uint32_t*)seed)[3];
      89             : 
      90             : #if WORDS_BIGENDIAN
      91             :         h1 = util_swap32(h1);
      92             :         h2 = util_swap32(h2);
      93             :         h3 = util_swap32(h3);
      94             :         h4 = util_swap32(h4);
      95             : #endif
      96             : 
      97     2174927 :         nblocks = size / 16;
      98     2174927 :         blocks = data;
      99     2174927 :         end = blocks + nblocks * 4;
     100             : 
     101             :         /* body */
     102   115758500 :         while (blocks < end) {
     103   111408646 :                 uint32_t k1 = blocks[0];
     104   111408646 :                 uint32_t k2 = blocks[1];
     105   111408646 :                 uint32_t k3 = blocks[2];
     106   111408646 :                 uint32_t k4 = blocks[3];
     107             : 
     108             : #if WORDS_BIGENDIAN
     109             :                 k1 = util_swap32(k1);
     110             :                 k2 = util_swap32(k2);
     111             :                 k3 = util_swap32(k3);
     112             :                 k4 = util_swap32(k4);
     113             : #endif
     114             : 
     115   111408646 :                 k1 *= c1; k1 = util_rotl32(k1, 15); k1 *= c2; h1 ^= k1;
     116             : 
     117   111408646 :                 h1 = util_rotl32(h1, 19); h1 += h2; h1 = h1 * 5 + 0x561ccd1b;
     118             : 
     119   111408646 :                 k2 *= c2; k2 = util_rotl32(k2, 16); k2 *= c3; h2 ^= k2;
     120             : 
     121   111408646 :                 h2 = util_rotl32(h2, 17); h2 += h3; h2 = h2 * 5 + 0x0bcaa747;
     122             : 
     123   111408646 :                 k3 *= c3; k3 = util_rotl32(k3, 17); k3 *= c4; h3 ^= k3;
     124             : 
     125   111408646 :                 h3 = util_rotl32(h3, 15); h3 += h4; h3 = h3 * 5 + 0x96cd1c35;
     126             : 
     127   111408646 :                 k4 *= c4; k4 = util_rotl32(k4, 18); k4 *= c1; h4 ^= k4;
     128             : 
     129   111408646 :                 h4 = util_rotl32(h4, 13); h4 += h1; h4 = h4 * 5 + 0x32ac3b17;
     130             : 
     131   111408646 :                 blocks += 4;
     132             :         }
     133             : 
     134             :         /* tail */
     135     2174927 :         remainder = size & 15;
     136     2174927 :         if (remainder != 0) {
     137      818892 :                 const uint8_t* tail = (const uint8_t*)blocks;
     138             : 
     139      818892 :                 uint32_t k1 = 0;
     140      818892 :                 uint32_t k2 = 0;
     141      818892 :                 uint32_t k3 = 0;
     142      818892 :                 uint32_t k4 = 0;
     143             : 
     144      818892 :                 switch (remainder) {
     145       53434 :                 case 15 : k4 ^= tail[14] << 16;
     146      108492 :                 case 14 : k4 ^= tail[13] << 8;
     147      163702 :                 case 13 : k4 ^= tail[12] << 0;
     148      163702 :                         k4 *= c4; k4 = util_rotl32(k4, 18); k4 *= c1; h4 ^= k4;
     149      218979 :                 case 12 : k3 ^= tail[11] << 24;
     150      274534 :                 case 11 : k3 ^= tail[10] << 16;
     151      329422 :                 case 10 : k3 ^= tail[ 9] << 8;
     152      385757 :                 case 9 : k3 ^= tail[ 8] << 0;
     153      385757 :                         k3 *= c3; k3 = util_rotl32(k3, 17); k3 *= c4; h3 ^= k3;
     154      442006 :                 case 8 : k2 ^= tail[ 7] << 24;
     155      494153 :                 case 7 : k2 ^= tail[ 6] << 16;
     156      549477 :                 case 6 : k2 ^= tail[ 5] << 8;
     157      602557 :                 case 5 : k2 ^= tail[ 4] << 0;
     158      602557 :                         k2 *= c2; k2 = util_rotl32(k2, 16); k2 *= c3; h2 ^= k2;
     159      656596 :                 case 4 : k1 ^= tail[ 3] << 24;
     160      709640 :                 case 3 : k1 ^= tail[ 2] << 16;
     161      763641 :                 case 2 : k1 ^= tail[ 1] << 8;
     162      818892 :                 case 1 : k1 ^= tail[ 0] << 0;
     163      818892 :                         k1 *= c1; k1 = util_rotl32(k1, 15); k1 *= c2; h1 ^= k1;
     164             :                 }
     165             :         }
     166             : 
     167             :         /* finalization */
     168     2174927 :         h1 ^= size; h2 ^= size; h3 ^= size; h4 ^= size;
     169             : 
     170     2174927 :         h1 += h2; h1 += h3; h1 += h4;
     171     2174927 :         h2 += h1; h3 += h1; h4 += h1;
     172             : 
     173     2174927 :         h1 = fmix32(h1);
     174     2174927 :         h2 = fmix32(h2);
     175     2174927 :         h3 = fmix32(h3);
     176     2174927 :         h4 = fmix32(h4);
     177             : 
     178     2174927 :         h1 += h2; h1 += h3; h1 += h4;
     179     2174927 :         h2 += h1; h3 += h1; h4 += h1;
     180             : 
     181             : #if WORDS_BIGENDIAN
     182             :         h1 = util_swap32(h1);
     183             :         h2 = util_swap32(h2);
     184             :         h3 = util_swap32(h3);
     185             :         h4 = util_swap32(h4);
     186             : #endif
     187             : 
     188     2174927 :         ((uint32_t*)digest)[0] = h1;
     189     2174927 :         ((uint32_t*)digest)[1] = h2;
     190     2174927 :         ((uint32_t*)digest)[2] = h3;
     191     2174927 :         ((uint32_t*)digest)[3] = h4;
     192     2174927 : }
     193             : 

Generated by: LCOV version 1.10