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