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