compare_common.cc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2012 The LibYuv Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "libyuv/basic_types.h"
  11. #include "libyuv/compare_row.h"
  12. #ifdef __cplusplus
  13. namespace libyuv {
  14. extern "C" {
  15. #endif
  16. #if ORIGINAL_OPT
  17. uint32 HammingDistance_C1(const uint8* src_a, const uint8* src_b, int count) {
  18. uint32 diff = 0u;
  19. int i;
  20. for (i = 0; i < count; ++i) {
  21. int x = src_a[i] ^ src_b[i];
  22. if (x & 1)
  23. ++diff;
  24. if (x & 2)
  25. ++diff;
  26. if (x & 4)
  27. ++diff;
  28. if (x & 8)
  29. ++diff;
  30. if (x & 16)
  31. ++diff;
  32. if (x & 32)
  33. ++diff;
  34. if (x & 64)
  35. ++diff;
  36. if (x & 128)
  37. ++diff;
  38. }
  39. return diff;
  40. }
  41. #endif
  42. // Hakmem method for hamming distance.
  43. uint32 HammingDistance_C(const uint8* src_a, const uint8* src_b, int count) {
  44. uint32 diff = 0u;
  45. int i;
  46. for (i = 0; i < count - 3; i += 4) {
  47. uint32 x = *((uint32*)src_a) ^ *((uint32*)src_b);
  48. uint32 u = x - ((x >> 1) & 0x55555555);
  49. u = ((u >> 2) & 0x33333333) + (u & 0x33333333);
  50. diff += ((((u + (u >> 4)) & 0x0f0f0f0f) * 0x01010101) >> 24);
  51. src_a += 4;
  52. src_b += 4;
  53. }
  54. for (; i < count; ++i) {
  55. uint32 x = *src_a ^ *src_b;
  56. uint32 u = x - ((x >> 1) & 0x55);
  57. u = ((u >> 2) & 0x33) + (u & 0x33);
  58. diff += (u + (u >> 4)) & 0x0f;
  59. src_a += 1;
  60. src_b += 1;
  61. }
  62. return diff;
  63. }
  64. uint32 SumSquareError_C(const uint8* src_a, const uint8* src_b, int count) {
  65. uint32 sse = 0u;
  66. int i;
  67. for (i = 0; i < count; ++i) {
  68. int diff = src_a[i] - src_b[i];
  69. sse += (uint32)(diff * diff);
  70. }
  71. return sse;
  72. }
  73. // hash seed of 5381 recommended.
  74. // Internal C version of HashDjb2 with int sized count for efficiency.
  75. uint32 HashDjb2_C(const uint8* src, int count, uint32 seed) {
  76. uint32 hash = seed;
  77. int i;
  78. for (i = 0; i < count; ++i) {
  79. hash += (hash << 5) + src[i];
  80. }
  81. return hash;
  82. }
  83. #ifdef __cplusplus
  84. } // extern "C"
  85. } // namespace libyuv
  86. #endif