summaryrefslogtreecommitdiff
path: root/src/caffe/util/math_functions.cpp
diff options
context:
space:
mode:
authorKai Li <kaili_kloud@163.com>2014-02-25 18:23:04 +0800
committerKai Li <kaili_kloud@163.com>2014-03-19 23:04:41 +0800
commit4de8280c1e97bef319b4b65e708137a5d54fdb88 (patch)
tree1e5d823421e1658a52ddbe9ed045582006d73ba8 /src/caffe/util/math_functions.cpp
parentfc740a3e7ec90829c142f5a7a9f409b5c849cd00 (diff)
downloadcaffeonacl-4de8280c1e97bef319b4b65e708137a5d54fdb88.tar.gz
caffeonacl-4de8280c1e97bef319b4b65e708137a5d54fdb88.tar.bz2
caffeonacl-4de8280c1e97bef319b4b65e708137a5d54fdb88.zip
Add __builtin_popcount* based fast Hamming distance math function
Diffstat (limited to 'src/caffe/util/math_functions.cpp')
-rw-r--r--src/caffe/util/math_functions.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/caffe/util/math_functions.cpp b/src/caffe/util/math_functions.cpp
index 60656b87..790f00ea 100644
--- a/src/caffe/util/math_functions.cpp
+++ b/src/caffe/util/math_functions.cpp
@@ -1,4 +1,5 @@
// Copyright 2013 Yangqing Jia
+// Copyright 2014 kloudkl@github
#include <mkl.h>
#include <cublas_v2.h>
@@ -293,4 +294,26 @@ void caffe_gpu_dot<double>(const int n, const double* x, const double* y,
CUBLAS_CHECK(cublasDdot(Caffe::cublas_handle(), n, x, 1, y, 1, out));
}
+template <>
+int caffe_hamming_distance<float>(const int n, const float* x,
+ const float* y) {
+ int dist = 0;
+ for (int i = 0; i < n; ++i) {
+ dist += __builtin_popcount(static_cast<uint32_t>(x[i]) ^
+ static_cast<uint32_t>(y[i]));
+ }
+ return dist;
+}
+
+template <>
+int caffe_hamming_distance<double>(const int n, const double* x,
+ const double* y) {
+ int dist = 0;
+ for (int i = 0; i < n; ++i) {
+ dist += __builtin_popcountl(static_cast<uint64_t>(x[i]) ^
+ static_cast<uint64_t>(y[i]));
+ }
+ return dist;
+}
+
} // namespace caffe