libfovis
sad.hpp
1 #ifndef __fovis_sad_hpp__
2 #define __fovis_sad_hpp__
3 
4 #ifdef FOVIS_USE_SSE
5 #include <emmintrin.h>
6 #endif
7 
8 namespace fovis
9 {
10 
17 class SAD {
18 public:
19  SAD(int descriptor_len) :
20  _descriptor_len(descriptor_len),
21  _nsad_ops(descriptor_len / 16 + ((descriptor_len % 16) ? 1 : 0))
22  { }
23 
29  int32_t score(const uint8_t *ref_desc, const uint8_t *target_desc) {
30 #ifdef FOVIS_USE_SSE
31  // compute sum of absolute differences (fast)
32  const uint8_t * pp = ref_desc;
33  const uint8_t * cp = target_desc;
34  __m128i d = _mm_setzero_si128();
35  for (int i = 0; i < _nsad_ops; i++) {
36  __m128i c = _mm_sad_epu8(*(__m128i *) pp, *(__m128i *) cp);
37  d = _mm_add_epi16(c, d);
38  pp += 16;
39  cp += 16;
40  }
41 
42  __m128i e = _mm_srli_si128(d, 8);
43  __m128i f = _mm_add_epi32(d, e);
44 
45  int32_t score = _mm_cvtsi128_si32(f);
46 #else
47  int32_t score = 0;
48  for(int i=0; i<_descriptor_len; i++) {
49  score += abs(ref_desc[i] - target_desc[i]);
50  }
51 #endif
52  return score;
53  }
54 
55  int getWorstScore() const {
56  return _descriptor_len * 255;
57  }
58 
59 private:
60  int _descriptor_len;
61  int _nsad_ops;
62 };
63 
64 }
65 
66 #endif
Calculates the Sum of Absolute Deviations (SAD) score between two vectors of length descriptor_len...
Definition: sad.hpp:17
quick and dirty profiling tool.inspired by the matlab tic/toc command
Definition: camera_intrinsics.hpp:6
int32_t score(const uint8_t *ref_desc, const uint8_t *target_desc)
Definition: sad.hpp:29