GSHash.cpp
上传用户:xjjlds
上传日期:2015-12-05
资源大小:22823k
文件大小:7k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. /* 
  2.  * Copyright (C) 2003-2005 Gabest
  3.  * http://www.gabest.org
  4.  *
  5.  *  This Program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2, or (at your option)
  8.  *  any later version.
  9.  *   
  10.  *  This Program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13.  *  GNU General Public License for more details.
  14.  *   
  15.  *  You should have received a copy of the GNU General Public License
  16.  *  along with GNU Make; see the file COPYING.  If not, write to
  17.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  18.  *  http://www.gnu.org/copyleft/gpl.html
  19.  *
  20.  */
  21. #include "stdafx.h"
  22. static const DWORD s_crctable[256] = 
  23. {
  24. 0x00000000l, 0x90910101l, 0x91210201l, 0x01b00300l,
  25. 0x92410401l, 0x02d00500l, 0x03600600l, 0x93f10701l,
  26. 0x94810801l, 0x04100900l, 0x05a00a00l, 0x95310b01l,
  27. 0x06c00c00l, 0x96510d01l, 0x97e10e01l, 0x07700f00l,
  28. 0x99011001l, 0x09901100l, 0x08201200l, 0x98b11301l,
  29. 0x0b401400l, 0x9bd11501l, 0x9a611601l, 0x0af01700l,
  30. 0x0d801800l, 0x9d111901l, 0x9ca11a01l, 0x0c301b00l,
  31. 0x9fc11c01l, 0x0f501d00l, 0x0ee01e00l, 0x9e711f01l,
  32. 0x82012001l, 0x12902100l, 0x13202200l, 0x83b12301l,
  33. 0x10402400l, 0x80d12501l, 0x81612601l, 0x11f02700l,
  34. 0x16802800l, 0x86112901l, 0x87a12a01l, 0x17302b00l,
  35. 0x84c12c01l, 0x14502d00l, 0x15e02e00l, 0x85712f01l,
  36. 0x1b003000l, 0x8b913101l, 0x8a213201l, 0x1ab03300l,
  37. 0x89413401l, 0x19d03500l, 0x18603600l, 0x88f13701l,
  38. 0x8f813801l, 0x1f103900l, 0x1ea03a00l, 0x8e313b01l,
  39. 0x1dc03c00l, 0x8d513d01l, 0x8ce13e01l, 0x1c703f00l,
  40. 0xb4014001l, 0x24904100l, 0x25204200l, 0xb5b14301l,
  41. 0x26404400l, 0xb6d14501l, 0xb7614601l, 0x27f04700l,
  42. 0x20804800l, 0xb0114901l, 0xb1a14a01l, 0x21304b00l,
  43. 0xb2c14c01l, 0x22504d00l, 0x23e04e00l, 0xb3714f01l,
  44. 0x2d005000l, 0xbd915101l, 0xbc215201l, 0x2cb05300l,
  45. 0xbf415401l, 0x2fd05500l, 0x2e605600l, 0xbef15701l,
  46. 0xb9815801l, 0x29105900l, 0x28a05a00l, 0xb8315b01l,
  47. 0x2bc05c00l, 0xbb515d01l, 0xbae15e01l, 0x2a705f00l,
  48. 0x36006000l, 0xa6916101l, 0xa7216201l, 0x37b06300l,
  49. 0xa4416401l, 0x34d06500l, 0x35606600l, 0xa5f16701l,
  50. 0xa2816801l, 0x32106900l, 0x33a06a00l, 0xa3316b01l,
  51. 0x30c06c00l, 0xa0516d01l, 0xa1e16e01l, 0x31706f00l,
  52. 0xaf017001l, 0x3f907100l, 0x3e207200l, 0xaeb17301l,
  53. 0x3d407400l, 0xadd17501l, 0xac617601l, 0x3cf07700l,
  54. 0x3b807800l, 0xab117901l, 0xaaa17a01l, 0x3a307b00l,
  55. 0xa9c17c01l, 0x39507d00l, 0x38e07e00l, 0xa8717f01l,
  56. 0xd8018001l, 0x48908100l, 0x49208200l, 0xd9b18301l,
  57. 0x4a408400l, 0xdad18501l, 0xdb618601l, 0x4bf08700l,
  58. 0x4c808800l, 0xdc118901l, 0xdda18a01l, 0x4d308b00l,
  59. 0xdec18c01l, 0x4e508d00l, 0x4fe08e00l, 0xdf718f01l,
  60. 0x41009000l, 0xd1919101l, 0xd0219201l, 0x40b09300l,
  61. 0xd3419401l, 0x43d09500l, 0x42609600l, 0xd2f19701l,
  62. 0xd5819801l, 0x45109900l, 0x44a09a00l, 0xd4319b01l,
  63. 0x47c09c00l, 0xd7519d01l, 0xd6e19e01l, 0x46709f00l,
  64. 0x5a00a000l, 0xca91a101l, 0xcb21a201l, 0x5bb0a300l,
  65. 0xc841a401l, 0x58d0a500l, 0x5960a600l, 0xc9f1a701l,
  66. 0xce81a801l, 0x5e10a900l, 0x5fa0aa00l, 0xcf31ab01l,
  67. 0x5cc0ac00l, 0xcc51ad01l, 0xcde1ae01l, 0x5d70af00l,
  68. 0xc301b001l, 0x5390b100l, 0x5220b200l, 0xc2b1b301l,
  69. 0x5140b400l, 0xc1d1b501l, 0xc061b601l, 0x50f0b700l,
  70. 0x5780b800l, 0xc711b901l, 0xc6a1ba01l, 0x5630bb00l,
  71. 0xc5c1bc01l, 0x5550bd00l, 0x54e0be00l, 0xc471bf01l,
  72. 0x6c00c000l, 0xfc91c101l, 0xfd21c201l, 0x6db0c300l,
  73. 0xfe41c401l, 0x6ed0c500l, 0x6f60c600l, 0xfff1c701l,
  74. 0xf881c801l, 0x6810c900l, 0x69a0ca00l, 0xf931cb01l,
  75. 0x6ac0cc00l, 0xfa51cd01l, 0xfbe1ce01l, 0x6b70cf00l,
  76. 0xf501d001l, 0x6590d100l, 0x6420d200l, 0xf4b1d301l,
  77. 0x6740d400l, 0xf7d1d501l, 0xf661d601l, 0x66f0d700l,
  78. 0x6180d800l, 0xf111d901l, 0xf0a1da01l, 0x6030db00l,
  79. 0xf3c1dc01l, 0x6350dd00l, 0x62e0de00l, 0xf271df01l,
  80. 0xee01e001l, 0x7e90e100l, 0x7f20e200l, 0xefb1e301l,
  81. 0x7c40e400l, 0xecd1e501l, 0xed61e601l, 0x7df0e700l,
  82. 0x7a80e800l, 0xea11e901l, 0xeba1ea01l, 0x7b30eb00l,
  83. 0xe8c1ec01l, 0x7850ed00l, 0x79e0ee00l, 0xe971ef01l,
  84. 0x7700f000l, 0xe791f101l, 0xe621f201l, 0x76b0f300l,
  85. 0xe541f401l, 0x75d0f500l, 0x7460f600l, 0xe4f1f701l,
  86. 0xe381f801l, 0x7310f900l, 0x72a0fa00l, 0xe231fb01l,
  87. 0x71c0fc00l, 0xe151fd01l, 0xe0e1fe01l, 0x7070ff00l
  88. };
  89. DWORD hash_crc(const CRect& r, int pitch, BYTE* p)
  90. {
  91. if(r.Width() == 0) {ASSERT(0); return 0;}
  92. DWORD hash = ~0;
  93. for(int j = r.top; j < r.bottom; j++, p += pitch)
  94. {
  95. for(int i = r.left, x = 0; i < r.right; i++, x += 4)
  96. {
  97. hash = s_crctable[(hash ^ p[x+0]) & 0xff] ^ (hash >> 8); 
  98. hash = s_crctable[(hash ^ p[x+1]) & 0xff] ^ (hash >> 8); 
  99. hash = s_crctable[(hash ^ p[x+2]) & 0xff] ^ (hash >> 8); 
  100. hash = s_crctable[(hash ^ p[x+3]) & 0xff] ^ (hash >> 8); 
  101. }
  102. }
  103. return hash;
  104. }
  105. #define MOD_ADLER 65521
  106. DWORD hash_adler(const CRect& r, int pitch, BYTE* p)
  107. {
  108. if(r.Width() == 0) {ASSERT(0); return 0;}
  109. DWORD a = 1, b = 0;
  110. for(int j = r.top; j < r.bottom; j++, p += pitch)
  111. {
  112. for(int i = r.left, x = 0; i < r.right; i++, x += 4)
  113. {
  114. a += p[x+0]; b += a;
  115. a += p[x+1]; b += a;
  116. a += p[x+2]; b += a;
  117. a += p[x+3]; b += a;
  118. }
  119. a = (a & 0xffff) + (a >> 16) * (65536 - MOD_ADLER);
  120. b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
  121. }
  122. // a = (a & 0xffff) + (a >> 16) * (65536 - MOD_ADLER);
  123. if(a >= MOD_ADLER) a -= MOD_ADLER;
  124. b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
  125. if(b >= MOD_ADLER) b -= MOD_ADLER;
  126. return b << 16 | a;
  127. }
  128. DWORD hash_checksum(const CRect& r, int pitch, BYTE* p)
  129. {
  130. if(r.Width() == 0) {ASSERT(0); return 0;}
  131. DWORD hash = 0;
  132. #if defined(_M_AMD64) || _M_IX86_FP >= 2
  133. if(r.Width() >= 4 && !((DWORD_PTR)p & 0xf) && !(pitch & 0xf))
  134. {
  135. __m128i hash128 = _mm_setzero_si128();
  136. for(int j = r.top; j < r.bottom; j++, p += pitch)
  137. {
  138. hash128 = _mm_add_epi32(hash128, _mm_set1_epi32(j));
  139. for(int i = r.left, x = 0; i < r.right; i += 4, x++)
  140. {
  141. hash128 = _mm_shuffle_epi32(hash128, 0x93);
  142. ////hash128 = _mm_shufflelo_epi16(hash128, 0x93);
  143. //hash128 = _mm_shufflehi_epi16(hash128, 0x93);
  144. hash128 = _mm_add_epi32(hash128, _mm_load_si128(&((__m128i*)p)[x]));
  145. }
  146. }
  147. hash128 = _mm_add_epi32(hash128, _mm_srli_si128(hash128, 8));
  148. hash128 = _mm_add_epi32(hash128, _mm_srli_si128(hash128, 4));
  149. hash += _mm_cvtsi128_si32(hash128);
  150. }
  151. else 
  152. #endif
  153. {
  154. for(int j = r.top; j < r.bottom; j++, p += pitch)
  155. {
  156. hash += j;
  157. for(int i = r.left, x = 0; i < r.right; i++, x++)
  158. {
  159. hash += ((DWORD*)p)[x];
  160. }
  161. }
  162. }
  163. return hash;
  164. }