compare_common.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_t HammingDistance_C1(const uint8_t* src_a,
  18. const uint8_t* src_b,
  19. int count) {
  20. uint32_t diff = 0u;
  21. int i;
  22. for (i = 0; i < count; ++i) {
  23. int x = src_a[i] ^ src_b[i];
  24. if (x & 1)
  25. ++diff;
  26. if (x & 2)
  27. ++diff;
  28. if (x & 4)
  29. ++diff;
  30. if (x & 8)
  31. ++diff;
  32. if (x & 16)
  33. ++diff;
  34. if (x & 32)
  35. ++diff;
  36. if (x & 64)
  37. ++diff;
  38. if (x & 128)
  39. ++diff;
  40. }
  41. return diff;
  42. }
  43. #endif
  44. // Hakmem method for hamming distance.
  45. uint32_t HammingDistance_C(const uint8_t* src_a,
  46. const uint8_t* src_b,
  47. int count) {
  48. uint32_t diff = 0u;
  49. int i;
  50. for (i = 0; i < count - 3; i += 4) {
  51. uint32_t x = *((const uint32_t*)src_a) ^ *((const uint32_t*)src_b);
  52. uint32_t u = x - ((x >> 1) & 0x55555555);
  53. u = ((u >> 2) & 0x33333333) + (u & 0x33333333);
  54. diff += ((((u + (u >> 4)) & 0x0f0f0f0f) * 0x01010101) >> 24);
  55. src_a += 4;
  56. src_b += 4;
  57. }
  58. for (; i < count; ++i) {
  59. uint32_t x = *src_a ^ *src_b;
  60. uint32_t u = x - ((x >> 1) & 0x55);
  61. u = ((u >> 2) & 0x33) + (u & 0x33);
  62. diff += (u + (u >> 4)) & 0x0f;
  63. src_a += 1;
  64. src_b += 1;
  65. }
  66. return diff;
  67. }
  68. uint32_t SumSquareError_C(const uint8_t* src_a,
  69. const uint8_t* src_b,
  70. int count) {
  71. uint32_t sse = 0u;
  72. int i;
  73. for (i = 0; i < count; ++i) {
  74. int diff = src_a[i] - src_b[i];
  75. sse += (uint32_t)(diff * diff);
  76. }
  77. return sse;
  78. }
  79. // hash seed of 5381 recommended.
  80. // Internal C version of HashDjb2 with int sized count for efficiency.
  81. uint32_t HashDjb2_C(const uint8_t* src, int count, uint32_t seed) {
  82. uint32_t hash = seed;
  83. int i;
  84. for (i = 0; i < count; ++i) {
  85. hash += (hash << 5) + src[i];
  86. }
  87. return hash;
  88. }
  89. #ifdef __cplusplus
  90. } // extern "C"
  91. } // namespace libyuv
  92. #endif