tesseract  4.1.0
dotproductavx.cpp
Go to the documentation of this file.
1 // File: dotproductavx.cpp
3 // Description: Architecture-specific dot-product function.
4 // Author: Ray Smith
5 // Created: Wed Jul 22 10:48:05 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(__AVX__)
20 #error Implementation only for AVX capable architectures
21 #endif
22 
23 #include <immintrin.h>
24 #include <cstdint>
25 #include "dotproductavx.h"
26 
27 namespace tesseract {
28 
29 // Computes and returns the dot product of the n-vectors u and v.
30 // Uses Intel AVX intrinsics to access the SIMD instruction set.
31 double DotProductAVX(const double* u, const double* v, int n) {
32  const unsigned quot = n / 8;
33  const unsigned rem = n % 8;
34  __m256d t0 = _mm256_setzero_pd();
35  __m256d t1 = _mm256_setzero_pd();
36  for (unsigned k = 0; k < quot; k++) {
37  __m256d f0 = _mm256_loadu_pd(u);
38  __m256d f1 = _mm256_loadu_pd(v);
39  f0 = _mm256_mul_pd(f0, f1);
40  t0 = _mm256_add_pd(t0, f0);
41  u += 4;
42  v += 4;
43  __m256d f2 = _mm256_loadu_pd(u);
44  __m256d f3 = _mm256_loadu_pd(v);
45  f2 = _mm256_mul_pd(f2, f3);
46  t1 = _mm256_add_pd(t1, f2);
47  u += 4;
48  v += 4;
49  }
50  t0 = _mm256_hadd_pd(t0, t1);
51  alignas(32) double tmp[4];
52  _mm256_store_pd(tmp, t0);
53  double result = tmp[0] + tmp[1] + tmp[2] + tmp[3];
54  for (unsigned k = 0; k < rem; k++) {
55  result += *u++ * *v++;
56  }
57  return result;
58 }
59 
60 } // namespace tesseract.
double DotProductAVX(const double *u, const double *v, int n)