tesseract  5.0.0-alpha-619-ge9db
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 //
6 // (C) Copyright 2015, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 
18 #if !defined(__AVX__)
19 #error Implementation only for AVX capable architectures
20 #endif
21 
22 #include <immintrin.h>
23 #include <cstdint>
24 #include "dotproduct.h"
25 
26 namespace tesseract {
27 
28 // Computes and returns the dot product of the n-vectors u and v.
29 // Uses Intel AVX intrinsics to access the SIMD instruction set.
30 double DotProductAVX(const double* u, const double* v, int n) {
31  const unsigned quot = n / 8;
32  const unsigned rem = n % 8;
33  __m256d t0 = _mm256_setzero_pd();
34  __m256d t1 = _mm256_setzero_pd();
35  for (unsigned k = 0; k < quot; k++) {
36  __m256d f0 = _mm256_loadu_pd(u);
37  __m256d f1 = _mm256_loadu_pd(v);
38  f0 = _mm256_mul_pd(f0, f1);
39  t0 = _mm256_add_pd(t0, f0);
40  u += 4;
41  v += 4;
42  __m256d f2 = _mm256_loadu_pd(u);
43  __m256d f3 = _mm256_loadu_pd(v);
44  f2 = _mm256_mul_pd(f2, f3);
45  t1 = _mm256_add_pd(t1, f2);
46  u += 4;
47  v += 4;
48  }
49  t0 = _mm256_hadd_pd(t0, t1);
50  alignas(32) double tmp[4];
51  _mm256_store_pd(tmp, t0);
52  double result = tmp[0] + tmp[1] + tmp[2] + tmp[3];
53  for (unsigned k = 0; k < rem; k++) {
54  result += *u++ * *v++;
55  }
56  return result;
57 }
58 
59 } // namespace tesseract.
dotproduct.h
tesseract
Definition: baseapi.h:65
tesseract::DotProductAVX
double DotProductAVX(const double *u, const double *v, int n)
Definition: dotproductavx.cpp:30