tesseract v5.3.3.20231005
dotproductavx512.cpp
Go to the documentation of this file.
1
2// File: dotproductavx512.cpp
3// Description: Architecture-specific dot-product function.
4// Author: Stefan Weil
5//
6// (C) Copyright 2022
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# if defined(__i686__) || defined(__x86_64__)
20# error Implementation only for AVX capable architectures
21# endif
22#else
23
24# include <immintrin.h>
25# include <cstdint>
26# include "dotproduct.h"
27
28namespace tesseract {
29
30// Computes and returns the dot product of the n-vectors u and v.
31// Uses Intel AVX intrinsics to access the SIMD instruction set.
32# if defined(FAST_FLOAT)
33float DotProductAVX512F(const float *u, const float *v, int n) {
34 const unsigned quot = n / 16;
35 const unsigned rem = n % 16;
36 __m512 t0 = _mm512_setzero_ps();
37 for (unsigned k = 0; k < quot; k++) {
38 __m512 f0 = _mm512_loadu_ps(u);
39 __m512 f1 = _mm512_loadu_ps(v);
40 t0 = _mm512_fmadd_ps(f0, f1, t0);
41 u += 16;
42 v += 16;
43 }
44 float result = _mm512_reduce_add_ps(t0);
45 for (unsigned k = 0; k < rem; k++) {
46 result += *u++ * *v++;
47 }
48 return result;
49}
50# else
51double DotProductAVX512F(const double *u, const double *v, int n) {
52 const unsigned quot = n / 8;
53 const unsigned rem = n % 8;
54 __m512d t0 = _mm512_setzero_pd();
55 for (unsigned k = 0; k < quot; k++) {
56 t0 = _mm512_fmadd_pd(_mm512_loadu_pd(u), _mm512_loadu_pd(v), t0);
57 u += 8;
58 v += 8;
59 }
60 double result = _mm512_reduce_add_pd(t0);
61 for (unsigned k = 0; k < rem; k++) {
62 result += *u++ * *v++;
63 }
64 return result;
65}
66# endif
67
68} // namespace tesseract.
69
70#endif
TFloat DotProductAVX512F(const TFloat *u, const TFloat *v, int n)