1 /***************************************************************************
2 Copyright (c) 2014, The OpenBLAS Project
3 All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
7 1. Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 2. Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
12 distribution.
13 3. Neither the name of the OpenBLAS project nor the names of
14 its contributors may be used to endorse or promote products
15 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBLAS PROJECT OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
25 USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *****************************************************************************/
27 
28 /* need a new enough GCC for avx512 support */
29 #if (( defined(__GNUC__)  && __GNUC__   > 6 && defined(__AVX2__)) || (defined(__clang__) && __clang_major__ >= 6))
30 
31 #define HAVE_KERNEL_8 1
32 
33 #include <immintrin.h>
34 
ddot_kernel_8(BLASLONG n,FLOAT * x,FLOAT * y,FLOAT * dot)35 static void ddot_kernel_8( BLASLONG n, FLOAT *x, FLOAT *y, FLOAT *dot)
36 {
37 	int i = 0;
38 	__m256d accum_0, accum_1, accum_2, accum_3;
39 
40 	accum_0 = _mm256_setzero_pd();
41 	accum_1 = _mm256_setzero_pd();
42 	accum_2 = _mm256_setzero_pd();
43 	accum_3 = _mm256_setzero_pd();
44 
45 #ifdef __AVX512CD__
46 	__m512d accum_05, accum_15, accum_25, accum_35;
47 	int n32;
48 	n32 = n & (~31);
49 
50 	accum_05 = _mm512_setzero_pd();
51 	accum_15 = _mm512_setzero_pd();
52 	accum_25 = _mm512_setzero_pd();
53 	accum_35 = _mm512_setzero_pd();
54 
55 	for (; i < n32; i += 32) {
56 		accum_05 += _mm512_loadu_pd(&x[i+ 0]) * _mm512_loadu_pd(&y[i+ 0]);
57 		accum_15 += _mm512_loadu_pd(&x[i+ 8]) * _mm512_loadu_pd(&y[i+ 8]);
58 		accum_25 += _mm512_loadu_pd(&x[i+16]) * _mm512_loadu_pd(&y[i+16]);
59 		accum_35 += _mm512_loadu_pd(&x[i+24]) * _mm512_loadu_pd(&y[i+24]);
60 	}
61 
62 	/*
63 	 * we need to fold our 512 bit wide accumulator vectors into 256 bit wide vectors so that the AVX2 code
64 	 * below can continue using the intermediate results in its loop
65 	 */
66 	accum_0 = _mm512_extractf64x4_pd(accum_05, 0) + _mm512_extractf64x4_pd(accum_05, 1);
67 	accum_1 = _mm512_extractf64x4_pd(accum_15, 0) + _mm512_extractf64x4_pd(accum_15, 1);
68 	accum_2 = _mm512_extractf64x4_pd(accum_25, 0) + _mm512_extractf64x4_pd(accum_25, 1);
69 	accum_3 = _mm512_extractf64x4_pd(accum_35, 0) + _mm512_extractf64x4_pd(accum_35, 1);
70 
71 #endif
72 	for (; i < n; i += 16) {
73 		accum_0 += _mm256_loadu_pd(&x[i+ 0]) * _mm256_loadu_pd(&y[i+ 0]);
74 		accum_1 += _mm256_loadu_pd(&x[i+ 4]) * _mm256_loadu_pd(&y[i+ 4]);
75 		accum_2 += _mm256_loadu_pd(&x[i+ 8]) * _mm256_loadu_pd(&y[i+ 8]);
76 		accum_3 += _mm256_loadu_pd(&x[i+12]) * _mm256_loadu_pd(&y[i+12]);
77 	}
78 
79 	/* we now have the partial sums of the dot product in the 4 accumulation vectors, time to consolidate */
80 
81 	accum_0 = accum_0 + accum_1 + accum_2 + accum_3;
82 
83 	__m128d half_accum0;
84 
85 	/* Add upper half to lower half of each of the 256 bit vector to get a 128 bit vector */
86 	half_accum0 = _mm_add_pd(_mm256_extractf128_pd(accum_0, 0), _mm256_extractf128_pd(accum_0, 1));
87 
88 	/* in 128 bit land there is a hadd operation to do the rest of the element-wise sum in one go */
89 	half_accum0 = _mm_hadd_pd(half_accum0, half_accum0);
90 
91 	*dot = half_accum0[0];
92 }
93 
94 #else
95 #include "ddot_microk_haswell-2.c"
96 #endif
97