tesseract  4.00.00dev
dotproductsse.cpp
Go to the documentation of this file.
1 // File: dotproductsse.cpp
3 // Description: Architecture-specific dot-product function.
4 // Author: Ray Smith
5 // Created: Wed Jul 22 10:57:45 PDT 2015
6 //
7 // (C) Copyright 2015, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
18 
19 #if !defined(__SSE4_1__)
20 // This code can't compile with "-msse4.1", so use dummy stubs.
21 
22 #include "dotproductsse.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 
26 namespace tesseract {
27 double DotProductSSE(const double* u, const double* v, int n) {
28  fprintf(stderr, "DotProductSSE can't be used on Android\n");
29  abort();
30 }
31 int32_t IntDotProductSSE(const int8_t* u, const int8_t* v, int n) {
32  fprintf(stderr, "IntDotProductSSE can't be used on Android\n");
33  abort();
34 }
35 } // namespace tesseract
36 
37 #else // !defined(__SSE4_1__)
38 // Non-Android code here
39 
40 #include <emmintrin.h>
41 #include <smmintrin.h>
42 #include <stdint.h>
43 #include "dotproductsse.h"
44 #include "host.h"
45 
46 namespace tesseract {
47 
48 // Computes and returns the dot product of the n-vectors u and v.
49 // Uses Intel SSE intrinsics to access the SIMD instruction set.
50 double DotProductSSE(const double* u, const double* v, int n) {
51  int max_offset = n - 2;
52  int offset = 0;
53  // Accumulate a set of 2 sums in sum, by loading pairs of 2 values from u and
54  // v, and multiplying them together in parallel.
55  __m128d sum = _mm_setzero_pd();
56  if (offset <= max_offset) {
57  offset = 2;
58  // Aligned load is reputedly faster but requires 16 byte aligned input.
59  if ((reinterpret_cast<const uintptr_t>(u) & 15) == 0 &&
60  (reinterpret_cast<const uintptr_t>(v) & 15) == 0) {
61  // Use aligned load.
62  sum = _mm_load_pd(u);
63  __m128d floats2 = _mm_load_pd(v);
64  // Multiply.
65  sum = _mm_mul_pd(sum, floats2);
66  while (offset <= max_offset) {
67  __m128d floats1 = _mm_load_pd(u + offset);
68  floats2 = _mm_load_pd(v + offset);
69  offset += 2;
70  floats1 = _mm_mul_pd(floats1, floats2);
71  sum = _mm_add_pd(sum, floats1);
72  }
73  } else {
74  // Use unaligned load.
75  sum = _mm_loadu_pd(u);
76  __m128d floats2 = _mm_loadu_pd(v);
77  // Multiply.
78  sum = _mm_mul_pd(sum, floats2);
79  while (offset <= max_offset) {
80  __m128d floats1 = _mm_loadu_pd(u + offset);
81  floats2 = _mm_loadu_pd(v + offset);
82  offset += 2;
83  floats1 = _mm_mul_pd(floats1, floats2);
84  sum = _mm_add_pd(sum, floats1);
85  }
86  }
87  }
88  // Add the 2 sums in sum horizontally.
89  sum = _mm_hadd_pd(sum, sum);
90  // Extract the low result.
91  double result = _mm_cvtsd_f64(sum);
92  // Add on any left-over products.
93  while (offset < n) {
94  result += u[offset] * v[offset];
95  ++offset;
96  }
97  return result;
98 }
99 
100 // Computes and returns the dot product of the n-vectors u and v.
101 // Uses Intel SSE intrinsics to access the SIMD instruction set.
102 int32_t IntDotProductSSE(const int8_t* u, const int8_t* v, int n) {
103  int max_offset = n - 8;
104  int offset = 0;
105  // Accumulate a set of 4 32-bit sums in sum, by loading 8 pairs of 8-bit
106  // values, extending to 16 bit, multiplying to make 32 bit results.
107  __m128i sum = _mm_setzero_si128();
108  if (offset <= max_offset) {
109  offset = 8;
110  __m128i packed1 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(u));
111  __m128i packed2 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(v));
112  sum = _mm_cvtepi8_epi16(packed1);
113  packed2 = _mm_cvtepi8_epi16(packed2);
114  // The magic _mm_add_epi16 is perfect here. It multiplies 8 pairs of 16 bit
115  // ints to make 32 bit results, which are then horizontally added in pairs
116  // to make 4 32 bit results that still fit in a 128 bit register.
117  sum = _mm_madd_epi16(sum, packed2);
118  while (offset <= max_offset) {
119  packed1 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(u + offset));
120  packed2 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(v + offset));
121  offset += 8;
122  packed1 = _mm_cvtepi8_epi16(packed1);
123  packed2 = _mm_cvtepi8_epi16(packed2);
124  packed1 = _mm_madd_epi16(packed1, packed2);
125  sum = _mm_add_epi32(sum, packed1);
126  }
127  }
128  // Sum the 4 packed 32 bit sums and extract the low result.
129  sum = _mm_hadd_epi32(sum, sum);
130  sum = _mm_hadd_epi32(sum, sum);
131  int32_t result = _mm_cvtsi128_si32(sum);
132  while (offset < n) {
133  result += u[offset] * v[offset];
134  ++offset;
135  }
136  return result;
137 }
138 
139 } // namespace tesseract.
140 
141 #endif // ANDROID_BUILD
double u[max]
double DotProductSSE(const double *u, const double *v, int n)
voidpf uLong offset
Definition: ioapi.h:42
int32_t IntDotProductSSE(const int8_t *u, const int8_t *v, int n)
double v[max]