tesseract  5.0.0
dotproductfma.cpp
Go to the documentation of this file.
1 // File: dotproductfma.cpp
3 // Description: Architecture-specific dot-product function.
4 // Author: Stefan Weil
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(__FMA__)
19 # if defined(__i686__) || defined(__x86_64__)
20 # error Implementation only for FMA capable architectures
21 # endif
22 #else
23 
24 # include <immintrin.h>
25 # include <cstdint>
26 # include "dotproduct.h"
27 
28 namespace tesseract {
29 
30 // Computes and returns the dot product of the n-vectors u and v.
31 // Uses Intel FMA intrinsics to access the SIMD instruction set.
32 #if defined(FAST_FLOAT)
33 float DotProductFMA(const float *u, const float *v, int n) {
34  const unsigned quot = n / 16;
35  const unsigned rem = n % 16;
36  __m256 t0 = _mm256_setzero_ps();
37  __m256 t1 = _mm256_setzero_ps();
38  for (unsigned k = 0; k < quot; k++) {
39  __m256 f0 = _mm256_loadu_ps(u);
40  __m256 f1 = _mm256_loadu_ps(v);
41  t0 = _mm256_fmadd_ps(f0, f1, t0);
42  u += 8;
43  v += 8;
44  __m256 f2 = _mm256_loadu_ps(u);
45  __m256 f3 = _mm256_loadu_ps(v);
46  t1 = _mm256_fmadd_ps(f2, f3, t1);
47  u += 8;
48  v += 8;
49  }
50  t0 = _mm256_hadd_ps(t0, t1);
51  alignas(32) float tmp[8];
52  _mm256_store_ps(tmp, t0);
53  float result = tmp[0] + tmp[1] + tmp[2] + tmp[3] + tmp[4] + tmp[5] + tmp[6] + tmp[7];
54  for (unsigned k = 0; k < rem; k++) {
55  result += *u++ * *v++;
56  }
57  return result;
58 }
59 #else
60 double DotProductFMA(const double *u, const double *v, int n) {
61  const unsigned quot = n / 8;
62  const unsigned rem = n % 8;
63  __m256d t0 = _mm256_setzero_pd();
64  __m256d t1 = _mm256_setzero_pd();
65  for (unsigned k = 0; k < quot; k++) {
66  __m256d f0 = _mm256_loadu_pd(u);
67  __m256d f1 = _mm256_loadu_pd(v);
68  t0 = _mm256_fmadd_pd(f0, f1, t0);
69  u += 4;
70  v += 4;
71  __m256d f2 = _mm256_loadu_pd(u);
72  __m256d f3 = _mm256_loadu_pd(v);
73  t1 = _mm256_fmadd_pd(f2, f3, t1);
74  u += 4;
75  v += 4;
76  }
77  t0 = _mm256_hadd_pd(t0, t1);
78  alignas(32) double tmp[4];
79  _mm256_store_pd(tmp, t0);
80  double result = tmp[0] + tmp[1] + tmp[2] + tmp[3];
81  for (unsigned k = 0; k < rem; k++) {
82  result += *u++ * *v++;
83  }
84  return result;
85 }
86 #endif
87 
88 } // namespace tesseract.
89 
90 #endif
TFloat DotProductFMA(const TFloat *u, const TFloat *v, int n)