elpa2_kernels_real_sse_4hv.c 30.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
//    This file is part of ELPA.
//
//    The ELPA library was originally created by the ELPA consortium,
//    consisting of the following organizations:
//
//    - Max Planck Computing and Data Facility (MPCDF), formerly known as
//      Rechenzentrum Garching der Max-Planck-Gesellschaft (RZG),
//    - Bergische Universität Wuppertal, Lehrstuhl für angewandte
//      Informatik,
//    - Technische Universität München, Lehrstuhl für Informatik mit
//      Schwerpunkt Wissenschaftliches Rechnen ,
//    - Fritz-Haber-Institut, Berlin, Abt. Theorie,
//    - Max-Plack-Institut für Mathematik in den Naturwissenschaftrn,
//      Leipzig, Abt. Komplexe Strukutren in Biologie und Kognition,
//      and
//    - IBM Deutschland GmbH
//
//    This particular source code file contains additions, changes and
//    enhancements authored by Intel Corporation which is not part of
//    the ELPA consortium.
//
//    More information can be found here:
//    http://elpa.mpcdf.mpg.de/
//
//    ELPA is free software: you can redistribute it and/or modify
//    it under the terms of the version 3 of the license of the
//    GNU Lesser General Public License as published by the Free
//    Software Foundation.
//
//    ELPA is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with ELPA.  If not, see <http://www.gnu.org/licenses/>
//
//    ELPA reflects a substantial effort on the part of the original
//    ELPA consortium, and we ask you to respect the spirit of the
//    license that we chose: i.e., please contribute any changes you
//    may have back to the original ELPA library distribution, and keep
//    any derivatives of ELPA under the same license that we chose for
//    the original distribution, the GNU Lesser General Public License.
//
//
// --------------------------------------------------------------------------------------------------
//
// This file contains the compute intensive kernels for the Householder transformations.
// It should be compiled with the highest possible optimization level.
//
// On Intel Nehalem or Intel Westmere or AMD Magny Cours use -O3 -msse3
// On Intel Sandy Bridge use -O3 -mavx
//
// Copyright of the original code rests with the authors inside the ELPA
// consortium. The copyright of any additional modifications shall rest
// with their original authors, but shall adhere to the licensing terms
// distributed along with the original code in the file "COPYING".
//
// Author: Alexander Heinecke (alexander.heinecke@mytum.de)
// Adapted for building a shared-library by Andreas Marek, MPCDF (andreas.marek@mpcdf.mpg.de)
// --------------------------------------------------------------------------------------------------

#include "config-f90.h"

#include <x86intrin.h>

#define __forceinline __attribute__((always_inline)) static

#ifdef HAVE_SSE
#undef __AVX__
#endif

//Forward declaration
__forceinline void hh_trafo_kernel_2_SSE_4hv(double* q, double* hh, int nb, int ldq, int ldh, double s_1_2, double s_1_3, double s_2_3, double s_1_4, double s_2_4, double s_3_4);
__forceinline void hh_trafo_kernel_4_SSE_4hv(double* q, double* hh, int nb, int ldq, int ldh, double s_1_2, double s_1_3, double s_2_3, double s_1_4, double s_2_4, double s_3_4);
__forceinline void hh_trafo_kernel_6_SSE_4hv(double* q, double* hh, int nb, int ldq, int ldh, double s_1_2, double s_1_3, double s_2_3, double s_1_4, double s_2_4, double s_3_4);

void quad_hh_trafo_real_sse_4hv_(double* q, double* hh, int* pnb, int* pnq, int* pldq, int* pldh);
#if 0
void quad_hh_trafo_fast_(double* q, double* hh, int* pnb, int* pnq, int* pldq, int* pldh);
#endif

void quad_hh_trafo_real_sse_4hv_(double* q, double* hh, int* pnb, int* pnq, int* pldq, int* pldh)
{
	int i;
	int nb = *pnb;
	int nq = *pldq;
	int ldq = *pldq;
	int ldh = *pldh;

	// calculating scalar products to compute
	// 4 householder vectors simultaneously
	double s_1_2 = hh[(ldh)+1];
	double s_1_3 = hh[(ldh*2)+2];
	double s_2_3 = hh[(ldh*2)+1];
	double s_1_4 = hh[(ldh*3)+3];
	double s_2_4 = hh[(ldh*3)+2];
	double s_3_4 = hh[(ldh*3)+1];

	// calculate scalar product of first and fourth householder vector
	// loop counter = 2
	s_1_2 += hh[2-1] * hh[(2+ldh)];
	s_2_3 += hh[(ldh)+2-1] * hh[2+(ldh*2)];
	s_3_4 += hh[(ldh*2)+2-1] * hh[2+(ldh*3)];

	// loop counter = 3
	s_1_2 += hh[3-1] * hh[(3+ldh)];
	s_2_3 += hh[(ldh)+3-1] * hh[3+(ldh*2)];
	s_3_4 += hh[(ldh*2)+3-1] * hh[3+(ldh*3)];

	s_1_3 += hh[3-2] * hh[3+(ldh*2)];
	s_2_4 += hh[(ldh*1)+3-2] * hh[3+(ldh*3)];

	#pragma ivdep
	for (i = 4; i < nb; i++)
	{
		s_1_2 += hh[i-1] * hh[(i+ldh)];
		s_2_3 += hh[(ldh)+i-1] * hh[i+(ldh*2)];
		s_3_4 += hh[(ldh*2)+i-1] * hh[i+(ldh*3)];

		s_1_3 += hh[i-2] * hh[i+(ldh*2)];
		s_2_4 += hh[(ldh*1)+i-2] * hh[i+(ldh*3)];

		s_1_4 += hh[i-3] * hh[i+(ldh*3)];
	}

//	printf("s_1_2: %f\n", s_1_2);
//	printf("s_1_3: %f\n", s_1_3);
//	printf("s_2_3: %f\n", s_2_3);
//	printf("s_1_4: %f\n", s_1_4);
//	printf("s_2_4: %f\n", s_2_4);
//	printf("s_3_4: %f\n", s_3_4);

	// Production level kernel calls with padding
	for (i = 0; i < nq-4; i+=6)
	{
		hh_trafo_kernel_6_SSE_4hv(&q[i], hh, nb, ldq, ldh, s_1_2, s_1_3, s_2_3, s_1_4, s_2_4, s_3_4);
	}
	if (nq == i)
	{
		return;
	}
	else
	{
		if (nq-i > 2)
		{
			hh_trafo_kernel_4_SSE_4hv(&q[i], hh, nb, ldq, ldh, s_1_2, s_1_3, s_2_3, s_1_4, s_2_4, s_3_4);
		}
		else
		{
			hh_trafo_kernel_2_SSE_4hv(&q[i], hh, nb, ldq, ldh, s_1_2, s_1_3, s_2_3, s_1_4, s_2_4, s_3_4);
		}
	}
}

#if 0
void quad_hh_trafo_fast_(double* q, double* hh, int* pnb, int* pnq, int* pldq, int* pldh)
{
	int i;
	int nb = *pnb;
	int nq = *pldq;
	int ldq = *pldq;
	int ldh = *pldh;

	// calculating scalar products to compute
	// 4 householder vectors simultaneously
	double s_1_2 = hh[(ldh)+1];
	double s_1_3 = hh[(ldh*2)+2];
	double s_2_3 = hh[(ldh*2)+1];
	double s_1_4 = hh[(ldh*3)+3];
	double s_2_4 = hh[(ldh*3)+2];
	double s_3_4 = hh[(ldh*3)+1];

	// calculate scalar product of first and fourth householder vector
	// loop counter = 2
	s_1_2 += hh[2-1] * hh[(2+ldh)];
	s_2_3 += hh[(ldh)+2-1] * hh[2+(ldh*2)];
	s_3_4 += hh[(ldh*2)+2-1] * hh[2+(ldh*3)];

	// loop counter = 3
	s_1_2 += hh[3-1] * hh[(3+ldh)];
	s_2_3 += hh[(ldh)+3-1] * hh[3+(ldh*2)];
	s_3_4 += hh[(ldh*2)+3-1] * hh[3+(ldh*3)];

	s_1_3 += hh[3-2] * hh[3+(ldh*2)];
	s_2_4 += hh[(ldh*1)+3-2] * hh[3+(ldh*3)];

	#pragma ivdep
	for (i = 4; i < nb; i++)
	{
		s_1_2 += hh[i-1] * hh[(i+ldh)];
		s_2_3 += hh[(ldh)+i-1] * hh[i+(ldh*2)];
		s_3_4 += hh[(ldh*2)+i-1] * hh[i+(ldh*3)];

		s_1_3 += hh[i-2] * hh[i+(ldh*2)];
		s_2_4 += hh[(ldh*1)+i-2] * hh[i+(ldh*3)];

		s_1_4 += hh[i-3] * hh[i+(ldh*3)];
	}

	// Production level kernel calls with padding
#ifdef __AVX__
	for (i = 0; i < nq; i+=12)
	{
		hh_trafo_kernel_12_AVX_4hv(&q[i], hh, nb, ldq, ldh, s_1_2, s_1_3, s_2_3, s_1_4, s_2_4, s_3_4);
	}
#else
	for (i = 0; i < nq; i+=6)
	{
		hh_trafo_kernel_6_SSE_4hv(&q[i], hh, nb, ldq, ldh, s_1_2, s_1_3, s_2_3, s_1_4, s_2_4, s_3_4);
	}
#endif
}
#endif
/**
 * Unrolled kernel that computes
 * 6 rows of Q simultaneously, a
 * matrix vector product with two householder
 * vectors + a rank 1 update is performed
 */
__forceinline void hh_trafo_kernel_6_SSE_4hv(double* q, double* hh, int nb, int ldq, int ldh, double s_1_2, double s_1_3, double s_2_3, double s_1_4, double s_2_4, double s_3_4)
{
	/////////////////////////////////////////////////////
	// Matrix Vector Multiplication, Q [6 x nb+3] * hh
	// hh contains four householder vectors
	/////////////////////////////////////////////////////
	int i;

	__m128d a1_1 = _mm_load_pd(&q[ldq*3]);
	__m128d a2_1 = _mm_load_pd(&q[ldq*2]);
	__m128d a3_1 = _mm_load_pd(&q[ldq]);
	__m128d a4_1 = _mm_load_pd(&q[0]);

	__m128d h_2_1 = _mm_loaddup_pd(&hh[ldh+1]);
	__m128d h_3_2 = _mm_loaddup_pd(&hh[(ldh*2)+1]);
	__m128d h_3_1 = _mm_loaddup_pd(&hh[(ldh*2)+2]);
	__m128d h_4_3 = _mm_loaddup_pd(&hh[(ldh*3)+1]);
	__m128d h_4_2 = _mm_loaddup_pd(&hh[(ldh*3)+2]);
	__m128d h_4_1 = _mm_loaddup_pd(&hh[(ldh*3)+3]);

	register __m128d w1 = _mm_add_pd(a4_1, _mm_mul_pd(a3_1, h_4_3));
	w1 = _mm_add_pd(w1, _mm_mul_pd(a2_1, h_4_2));
	w1 = _mm_add_pd(w1, _mm_mul_pd(a1_1, h_4_1));
	register __m128d z1 = _mm_add_pd(a3_1, _mm_mul_pd(a2_1, h_3_2));
	z1 = _mm_add_pd(z1, _mm_mul_pd(a1_1, h_3_1));
	register __m128d y1 = _mm_add_pd(a2_1, _mm_mul_pd(a1_1, h_2_1));
	register __m128d x1 = a1_1;

	__m128d a1_2 = _mm_load_pd(&q[(ldq*3)+2]);
	__m128d a2_2 = _mm_load_pd(&q[(ldq*2)+2]);
	__m128d a3_2 = _mm_load_pd(&q[ldq+2]);
	__m128d a4_2 = _mm_load_pd(&q[0+2]);

	register __m128d w2 = _mm_add_pd(a4_2, _mm_mul_pd(a3_2, h_4_3));
	w2 = _mm_add_pd(w2, _mm_mul_pd(a2_2, h_4_2));
	w2 = _mm_add_pd(w2, _mm_mul_pd(a1_2, h_4_1));
	register __m128d z2 = _mm_add_pd(a3_2, _mm_mul_pd(a2_2, h_3_2));
	z2 = _mm_add_pd(z2, _mm_mul_pd(a1_2, h_3_1));
	register __m128d y2 = _mm_add_pd(a2_2, _mm_mul_pd(a1_2, h_2_1));
	register __m128d x2 = a1_2;

	__m128d a1_3 = _mm_load_pd(&q[(ldq*3)+4]);
	__m128d a2_3 = _mm_load_pd(&q[(ldq*2)+4]);
	__m128d a3_3 = _mm_load_pd(&q[ldq+4]);
	__m128d a4_3 = _mm_load_pd(&q[0+4]);

	register __m128d w3 = _mm_add_pd(a4_3, _mm_mul_pd(a3_3, h_4_3));
	w3 = _mm_add_pd(w3, _mm_mul_pd(a2_3, h_4_2));
	w3 = _mm_add_pd(w3, _mm_mul_pd(a1_3, h_4_1));
	register __m128d z3 = _mm_add_pd(a3_3, _mm_mul_pd(a2_3, h_3_2));
	z3 = _mm_add_pd(z3, _mm_mul_pd(a1_3, h_3_1));
	register __m128d y3 = _mm_add_pd(a2_3, _mm_mul_pd(a1_3, h_2_1));
	register __m128d x3 = a1_3;

	__m128d q1;
	__m128d q2;
	__m128d q3;

	__m128d h1;
	__m128d h2;
	__m128d h3;
	__m128d h4;

	for(i = 4; i < nb; i++)
	{
		h1 = _mm_loaddup_pd(&hh[i-3]);
		q1 = _mm_load_pd(&q[i*ldq]);
		q2 = _mm_load_pd(&q[(i*ldq)+2]);
		q3 = _mm_load_pd(&q[(i*ldq)+4]);

		x1 = _mm_add_pd(x1, _mm_mul_pd(q1,h1));
		x2 = _mm_add_pd(x2, _mm_mul_pd(q2,h1));
		x3 = _mm_add_pd(x3, _mm_mul_pd(q3,h1));

		h2 = _mm_loaddup_pd(&hh[ldh+i-2]);
296

297 298 299 300 301
		y1 = _mm_add_pd(y1, _mm_mul_pd(q1,h2));
		y2 = _mm_add_pd(y2, _mm_mul_pd(q2,h2));
		y3 = _mm_add_pd(y3, _mm_mul_pd(q3,h2));

		h3 = _mm_loaddup_pd(&hh[(ldh*2)+i-1]);
302

303 304 305 306 307
		z1 = _mm_add_pd(z1, _mm_mul_pd(q1,h3));
		z2 = _mm_add_pd(z2, _mm_mul_pd(q2,h3));
		z3 = _mm_add_pd(z3, _mm_mul_pd(q3,h3));

		h4 = _mm_loaddup_pd(&hh[(ldh*3)+i]);
308

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
		w1 = _mm_add_pd(w1, _mm_mul_pd(q1,h4));
		w2 = _mm_add_pd(w2, _mm_mul_pd(q2,h4));
		w3 = _mm_add_pd(w3, _mm_mul_pd(q3,h4));
	}

	h1 = _mm_loaddup_pd(&hh[nb-3]);

	q1 = _mm_load_pd(&q[nb*ldq]);
	q2 = _mm_load_pd(&q[(nb*ldq)+2]);
	q3 = _mm_load_pd(&q[(nb*ldq)+4]);

	x1 = _mm_add_pd(x1, _mm_mul_pd(q1,h1));
	x2 = _mm_add_pd(x2, _mm_mul_pd(q2,h1));
	x3 = _mm_add_pd(x3, _mm_mul_pd(q3,h1));

	h2 = _mm_loaddup_pd(&hh[ldh+nb-2]);
325

326 327 328 329 330
	y1 = _mm_add_pd(y1, _mm_mul_pd(q1,h2));
	y2 = _mm_add_pd(y2, _mm_mul_pd(q2,h2));
	y3 = _mm_add_pd(y3, _mm_mul_pd(q3,h2));

	h3 = _mm_loaddup_pd(&hh[(ldh*2)+nb-1]);
331

332 333 334 335 336 337 338 339 340
	z1 = _mm_add_pd(z1, _mm_mul_pd(q1,h3));
	z2 = _mm_add_pd(z2, _mm_mul_pd(q2,h3));
	z3 = _mm_add_pd(z3, _mm_mul_pd(q3,h3));

	h1 = _mm_loaddup_pd(&hh[nb-2]);

	q1 = _mm_load_pd(&q[(nb+1)*ldq]);
	q2 = _mm_load_pd(&q[((nb+1)*ldq)+2]);
	q3 = _mm_load_pd(&q[((nb+1)*ldq)+4]);
341

342 343 344 345 346
	x1 = _mm_add_pd(x1, _mm_mul_pd(q1,h1));
	x2 = _mm_add_pd(x2, _mm_mul_pd(q2,h1));
	x3 = _mm_add_pd(x3, _mm_mul_pd(q3,h1));

	h2 = _mm_loaddup_pd(&hh[(ldh*1)+nb-1]);
347

348 349 350 351 352 353 354 355 356
	y1 = _mm_add_pd(y1, _mm_mul_pd(q1,h2));
	y2 = _mm_add_pd(y2, _mm_mul_pd(q2,h2));
	y3 = _mm_add_pd(y3, _mm_mul_pd(q3,h2));

	h1 = _mm_loaddup_pd(&hh[nb-1]);

	q1 = _mm_load_pd(&q[(nb+2)*ldq]);
	q2 = _mm_load_pd(&q[((nb+2)*ldq)+2]);
	q3 = _mm_load_pd(&q[((nb+2)*ldq)+4]);
357

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
	x1 = _mm_add_pd(x1, _mm_mul_pd(q1,h1));
	x2 = _mm_add_pd(x2, _mm_mul_pd(q2,h1));
	x3 = _mm_add_pd(x3, _mm_mul_pd(q3,h1));

	/////////////////////////////////////////////////////
	// Rank-1 update of Q [6 x nb+3]
	/////////////////////////////////////////////////////

	__m128d tau1 = _mm_loaddup_pd(&hh[0]);

	h1 = tau1;
	x1 = _mm_mul_pd(x1, h1);
	x2 = _mm_mul_pd(x2, h1);
	x3 = _mm_mul_pd(x3, h1);

	__m128d tau2 = _mm_loaddup_pd(&hh[ldh]);
	__m128d vs_1_2 = _mm_loaddup_pd(&s_1_2);

	h1 = tau2;
	h2 = _mm_mul_pd(h1, vs_1_2);
378

379 380 381 382 383 384 385 386 387 388 389
	y1 = _mm_sub_pd(_mm_mul_pd(y1,h1), _mm_mul_pd(x1,h2));
	y2 = _mm_sub_pd(_mm_mul_pd(y2,h1), _mm_mul_pd(x2,h2));
	y3 = _mm_sub_pd(_mm_mul_pd(y3,h1), _mm_mul_pd(x3,h2));

	__m128d tau3 = _mm_loaddup_pd(&hh[ldh*2]);
	__m128d vs_1_3 = _mm_loaddup_pd(&s_1_3);
	__m128d vs_2_3 = _mm_loaddup_pd(&s_2_3);

	h1 = tau3;
	h2 = _mm_mul_pd(h1, vs_1_3);
	h3 = _mm_mul_pd(h1, vs_2_3);
390

391 392 393 394 395 396 397 398 399 400 401 402 403
	z1 = _mm_sub_pd(_mm_mul_pd(z1,h1), _mm_add_pd(_mm_mul_pd(y1,h3), _mm_mul_pd(x1,h2)));
	z2 = _mm_sub_pd(_mm_mul_pd(z2,h1), _mm_add_pd(_mm_mul_pd(y2,h3), _mm_mul_pd(x2,h2)));
	z3 = _mm_sub_pd(_mm_mul_pd(z3,h1), _mm_add_pd(_mm_mul_pd(y3,h3), _mm_mul_pd(x3,h2)));

	__m128d tau4 = _mm_loaddup_pd(&hh[ldh*3]);
	__m128d vs_1_4 = _mm_loaddup_pd(&s_1_4);
	__m128d vs_2_4 = _mm_loaddup_pd(&s_2_4);
	__m128d vs_3_4 = _mm_loaddup_pd(&s_3_4);

	h1 = tau4;
	h2 = _mm_mul_pd(h1, vs_1_4);
	h3 = _mm_mul_pd(h1, vs_2_4);
	h4 = _mm_mul_pd(h1, vs_3_4);
404

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	w1 = _mm_sub_pd(_mm_mul_pd(w1,h1), _mm_add_pd(_mm_mul_pd(z1,h4), _mm_add_pd(_mm_mul_pd(y1,h3), _mm_mul_pd(x1,h2))));
	w2 = _mm_sub_pd(_mm_mul_pd(w2,h1), _mm_add_pd(_mm_mul_pd(z2,h4), _mm_add_pd(_mm_mul_pd(y2,h3), _mm_mul_pd(x2,h2))));
	w3 = _mm_sub_pd(_mm_mul_pd(w3,h1), _mm_add_pd(_mm_mul_pd(z3,h4), _mm_add_pd(_mm_mul_pd(y3,h3), _mm_mul_pd(x3,h2))));

	q1 = _mm_load_pd(&q[0]);
	q2 = _mm_load_pd(&q[2]);
	q3 = _mm_load_pd(&q[4]);
	q1 = _mm_sub_pd(q1, w1);
	q2 = _mm_sub_pd(q2, w2);
	q3 = _mm_sub_pd(q3, w3);
	_mm_store_pd(&q[0],q1);
	_mm_store_pd(&q[2],q2);
	_mm_store_pd(&q[4],q3);

	h4 = _mm_loaddup_pd(&hh[(ldh*3)+1]);
	q1 = _mm_load_pd(&q[ldq]);
	q2 = _mm_load_pd(&q[ldq+2]);
	q3 = _mm_load_pd(&q[ldq+4]);
423

424 425 426
	q1 = _mm_sub_pd(q1, _mm_add_pd(z1, _mm_mul_pd(w1, h4)));
	q2 = _mm_sub_pd(q2, _mm_add_pd(z2, _mm_mul_pd(w2, h4)));
	q3 = _mm_sub_pd(q3, _mm_add_pd(z3, _mm_mul_pd(w3, h4)));
427

428 429 430 431 432 433 434 435 436 437 438
	_mm_store_pd(&q[ldq],q1);
	_mm_store_pd(&q[ldq+2],q2);
	_mm_store_pd(&q[ldq+4],q3);

	h4 = _mm_loaddup_pd(&hh[(ldh*3)+2]);
	q1 = _mm_load_pd(&q[ldq*2]);
	q2 = _mm_load_pd(&q[(ldq*2)+2]);
	q3 = _mm_load_pd(&q[(ldq*2)+4]);
	q1 = _mm_sub_pd(q1, y1);
	q2 = _mm_sub_pd(q2, y2);
	q3 = _mm_sub_pd(q3, y3);
439

440 441 442 443
	q1 = _mm_sub_pd(q1, _mm_mul_pd(w1, h4));
	q2 = _mm_sub_pd(q2, _mm_mul_pd(w2, h4));
	q3 = _mm_sub_pd(q3, _mm_mul_pd(w3, h4));
	h3 = _mm_loaddup_pd(&hh[(ldh*2)+1]);
444

445 446 447
	q1 = _mm_sub_pd(q1, _mm_mul_pd(z1, h3));
	q2 = _mm_sub_pd(q2, _mm_mul_pd(z2, h3));
	q3 = _mm_sub_pd(q3, _mm_mul_pd(z3, h3));
448

449 450 451 452 453 454 455 456 457 458 459
	_mm_store_pd(&q[ldq*2],q1);
	_mm_store_pd(&q[(ldq*2)+2],q2);
	_mm_store_pd(&q[(ldq*2)+4],q3);

	h4 = _mm_loaddup_pd(&hh[(ldh*3)+3]);
	q1 = _mm_load_pd(&q[ldq*3]);
	q2 = _mm_load_pd(&q[(ldq*3)+2]);
	q3 = _mm_load_pd(&q[(ldq*3)+4]);
	q1 = _mm_sub_pd(q1, x1);
	q2 = _mm_sub_pd(q2, x2);
	q3 = _mm_sub_pd(q3, x3);
460

461 462 463
	q1 = _mm_sub_pd(q1, _mm_mul_pd(w1, h4));
	q2 = _mm_sub_pd(q2, _mm_mul_pd(w2, h4));
	q3 = _mm_sub_pd(q3, _mm_mul_pd(w3, h4));
464

465
	h2 = _mm_loaddup_pd(&hh[ldh+1]);
466

467 468 469
	q1 = _mm_sub_pd(q1, _mm_mul_pd(y1, h2));
	q2 = _mm_sub_pd(q2, _mm_mul_pd(y2, h2));
	q3 = _mm_sub_pd(q3, _mm_mul_pd(y3, h2));
470

471
	h3 = _mm_loaddup_pd(&hh[(ldh*2)+2]);
472

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	q1 = _mm_sub_pd(q1, _mm_mul_pd(z1, h3));
	q2 = _mm_sub_pd(q2, _mm_mul_pd(z2, h3));
	q3 = _mm_sub_pd(q3, _mm_mul_pd(z3, h3));
	_mm_store_pd(&q[ldq*3], q1);
	_mm_store_pd(&q[(ldq*3)+2], q2);
	_mm_store_pd(&q[(ldq*3)+4], q3);

	for (i = 4; i < nb; i++)
	{
		h1 = _mm_loaddup_pd(&hh[i-3]);

		q1 = _mm_load_pd(&q[i*ldq]);
		q2 = _mm_load_pd(&q[(i*ldq)+2]);
		q3 = _mm_load_pd(&q[(i*ldq)+4]);

		q1 = _mm_sub_pd(q1, _mm_mul_pd(x1,h1));
		q2 = _mm_sub_pd(q2, _mm_mul_pd(x2,h1));
		q3 = _mm_sub_pd(q3, _mm_mul_pd(x3,h1));

		h2 = _mm_loaddup_pd(&hh[ldh+i-2]);
493

494 495 496 497 498
		q1 = _mm_sub_pd(q1, _mm_mul_pd(y1,h2));
		q2 = _mm_sub_pd(q2, _mm_mul_pd(y2,h2));
		q3 = _mm_sub_pd(q3, _mm_mul_pd(y3,h2));

		h3 = _mm_loaddup_pd(&hh[(ldh*2)+i-1]);
499

500 501 502 503 504
		q1 = _mm_sub_pd(q1, _mm_mul_pd(z1,h3));
		q2 = _mm_sub_pd(q2, _mm_mul_pd(z2,h3));
		q3 = _mm_sub_pd(q3, _mm_mul_pd(z3,h3));

		h4 = _mm_loaddup_pd(&hh[(ldh*3)+i]);
505

506 507 508 509 510 511 512 513 514 515 516 517 518
		q1 = _mm_sub_pd(q1, _mm_mul_pd(w1,h4));
		q2 = _mm_sub_pd(q2, _mm_mul_pd(w2,h4));
		q3 = _mm_sub_pd(q3, _mm_mul_pd(w3,h4));

		_mm_store_pd(&q[i*ldq],q1);
		_mm_store_pd(&q[(i*ldq)+2],q2);
		_mm_store_pd(&q[(i*ldq)+4],q3);
	}

	h1 = _mm_loaddup_pd(&hh[nb-3]);
	q1 = _mm_load_pd(&q[nb*ldq]);
	q2 = _mm_load_pd(&q[(nb*ldq)+2]);
	q3 = _mm_load_pd(&q[(nb*ldq)+4]);
519

520 521 522
	q1 = _mm_sub_pd(q1, _mm_mul_pd(x1, h1));
	q2 = _mm_sub_pd(q2, _mm_mul_pd(x2, h1));
	q3 = _mm_sub_pd(q3, _mm_mul_pd(x3, h1));
523

524
	h2 = _mm_loaddup_pd(&hh[ldh+nb-2]);
525

526 527 528
	q1 = _mm_sub_pd(q1, _mm_mul_pd(y1, h2));
	q2 = _mm_sub_pd(q2, _mm_mul_pd(y2, h2));
	q3 = _mm_sub_pd(q3, _mm_mul_pd(y3, h2));
529

530
	h3 = _mm_loaddup_pd(&hh[(ldh*2)+nb-1]);
531

532 533 534
	q1 = _mm_sub_pd(q1, _mm_mul_pd(z1, h3));
	q2 = _mm_sub_pd(q2, _mm_mul_pd(z2, h3));
	q3 = _mm_sub_pd(q3, _mm_mul_pd(z3, h3));
535

536 537 538 539 540 541 542 543
	_mm_store_pd(&q[nb*ldq],q1);
	_mm_store_pd(&q[(nb*ldq)+2],q2);
	_mm_store_pd(&q[(nb*ldq)+4],q3);

	h1 = _mm_loaddup_pd(&hh[nb-2]);
	q1 = _mm_load_pd(&q[(nb+1)*ldq]);
	q2 = _mm_load_pd(&q[((nb+1)*ldq)+2]);
	q3 = _mm_load_pd(&q[((nb+1)*ldq)+4]);
544

545 546 547
	q1 = _mm_sub_pd(q1, _mm_mul_pd(x1, h1));
	q2 = _mm_sub_pd(q2, _mm_mul_pd(x2, h1));
	q3 = _mm_sub_pd(q3, _mm_mul_pd(x3, h1));
548

549
	h2 = _mm_loaddup_pd(&hh[ldh+nb-1]);
550

551 552 553
	q1 = _mm_sub_pd(q1, _mm_mul_pd(y1, h2));
	q2 = _mm_sub_pd(q2, _mm_mul_pd(y2, h2));
	q3 = _mm_sub_pd(q3, _mm_mul_pd(y3, h2));
554

555 556 557 558 559 560 561 562
	_mm_store_pd(&q[(nb+1)*ldq],q1);
	_mm_store_pd(&q[((nb+1)*ldq)+2],q2);
	_mm_store_pd(&q[((nb+1)*ldq)+4],q3);

	h1 = _mm_loaddup_pd(&hh[nb-1]);
	q1 = _mm_load_pd(&q[(nb+2)*ldq]);
	q2 = _mm_load_pd(&q[((nb+2)*ldq)+2]);
	q3 = _mm_load_pd(&q[((nb+2)*ldq)+4]);
563

564 565 566
	q1 = _mm_sub_pd(q1, _mm_mul_pd(x1, h1));
	q2 = _mm_sub_pd(q2, _mm_mul_pd(x2, h1));
	q3 = _mm_sub_pd(q3, _mm_mul_pd(x3, h1));
567

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
	_mm_store_pd(&q[(nb+2)*ldq],q1);
	_mm_store_pd(&q[((nb+2)*ldq)+2],q2);
	_mm_store_pd(&q[((nb+2)*ldq)+4],q3);
}

/**
 * Unrolled kernel that computes
 * 4 rows of Q simultaneously, a
 * matrix vector product with two householder
 * vectors + a rank 1 update is performed
 */
__forceinline void hh_trafo_kernel_4_SSE_4hv(double* q, double* hh, int nb, int ldq, int ldh, double s_1_2, double s_1_3, double s_2_3, double s_1_4, double s_2_4, double s_3_4)
{
	/////////////////////////////////////////////////////
	// Matrix Vector Multiplication, Q [4 x nb+3] * hh
	// hh contains four householder vectors
	/////////////////////////////////////////////////////
	int i;

	__m128d a1_1 = _mm_load_pd(&q[ldq*3]);
	__m128d a2_1 = _mm_load_pd(&q[ldq*2]);
	__m128d a3_1 = _mm_load_pd(&q[ldq]);
	__m128d a4_1 = _mm_load_pd(&q[0]);

	__m128d h_2_1 = _mm_loaddup_pd(&hh[ldh+1]);
	__m128d h_3_2 = _mm_loaddup_pd(&hh[(ldh*2)+1]);
	__m128d h_3_1 = _mm_loaddup_pd(&hh[(ldh*2)+2]);
	__m128d h_4_3 = _mm_loaddup_pd(&hh[(ldh*3)+1]);
	__m128d h_4_2 = _mm_loaddup_pd(&hh[(ldh*3)+2]);
	__m128d h_4_1 = _mm_loaddup_pd(&hh[(ldh*3)+3]);

	__m128d w1 = _mm_add_pd(a4_1, _mm_mul_pd(a3_1, h_4_3));
	w1 = _mm_add_pd(w1, _mm_mul_pd(a2_1, h_4_2));
	w1 = _mm_add_pd(w1, _mm_mul_pd(a1_1, h_4_1));
	__m128d z1 = _mm_add_pd(a3_1, _mm_mul_pd(a2_1, h_3_2));
	z1 = _mm_add_pd(z1, _mm_mul_pd(a1_1, h_3_1));
	__m128d y1 = _mm_add_pd(a2_1, _mm_mul_pd(a1_1, h_2_1));
	__m128d x1 = a1_1;

	__m128d a1_2 = _mm_load_pd(&q[(ldq*3)+2]);
	__m128d a2_2 = _mm_load_pd(&q[(ldq*2)+2]);
	__m128d a3_2 = _mm_load_pd(&q[ldq+2]);
	__m128d a4_2 = _mm_load_pd(&q[0+2]);

	__m128d w2 = _mm_add_pd(a4_2, _mm_mul_pd(a3_2, h_4_3));
	w2 = _mm_add_pd(w2, _mm_mul_pd(a2_2, h_4_2));
	w2 = _mm_add_pd(w2, _mm_mul_pd(a1_2, h_4_1));
	__m128d z2 = _mm_add_pd(a3_2, _mm_mul_pd(a2_2, h_3_2));
	z2 = _mm_add_pd(z2, _mm_mul_pd(a1_2, h_3_1));
	__m128d y2 = _mm_add_pd(a2_2, _mm_mul_pd(a1_2, h_2_1));
	__m128d x2 = a1_2;

	__m128d q1;
	__m128d q2;

	__m128d h1;
	__m128d h2;
	__m128d h3;
	__m128d h4;

	for(i = 4; i < nb; i++)
	{
		h1 = _mm_loaddup_pd(&hh[i-3]);
		h2 = _mm_loaddup_pd(&hh[ldh+i-2]);
		h3 = _mm_loaddup_pd(&hh[(ldh*2)+i-1]);
		h4 = _mm_loaddup_pd(&hh[(ldh*3)+i]);

		q1 = _mm_load_pd(&q[i*ldq]);
636

637 638 639 640 641 642
		x1 = _mm_add_pd(x1, _mm_mul_pd(q1,h1));
		y1 = _mm_add_pd(y1, _mm_mul_pd(q1,h2));
		z1 = _mm_add_pd(z1, _mm_mul_pd(q1,h3));
		w1 = _mm_add_pd(w1, _mm_mul_pd(q1,h4));

		q2 = _mm_load_pd(&q[(i*ldq)+2]);
643

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
		x2 = _mm_add_pd(x2, _mm_mul_pd(q2,h1));
		y2 = _mm_add_pd(y2, _mm_mul_pd(q2,h2));
		z2 = _mm_add_pd(z2, _mm_mul_pd(q2,h3));
		w2 = _mm_add_pd(w2, _mm_mul_pd(q2,h4));
	}

	h1 = _mm_loaddup_pd(&hh[nb-3]);
	h2 = _mm_loaddup_pd(&hh[ldh+nb-2]);
	h3 = _mm_loaddup_pd(&hh[(ldh*2)+nb-1]);

	q1 = _mm_load_pd(&q[nb*ldq]);
	q2 = _mm_load_pd(&q[(nb*ldq)+2]);

	x1 = _mm_add_pd(x1, _mm_mul_pd(q1,h1));
	x2 = _mm_add_pd(x2, _mm_mul_pd(q2,h1));
	y1 = _mm_add_pd(y1, _mm_mul_pd(q1,h2));
	y2 = _mm_add_pd(y2, _mm_mul_pd(q2,h2));
	z1 = _mm_add_pd(z1, _mm_mul_pd(q1,h3));
	z2 = _mm_add_pd(z2, _mm_mul_pd(q2,h3));

	h1 = _mm_loaddup_pd(&hh[nb-2]);
	h2 = _mm_loaddup_pd(&hh[(ldh*1)+nb-1]);

	q1 = _mm_load_pd(&q[(nb+1)*ldq]);
	q2 = _mm_load_pd(&q[((nb+1)*ldq)+2]);

	x1 = _mm_add_pd(x1, _mm_mul_pd(q1,h1));
	x2 = _mm_add_pd(x2, _mm_mul_pd(q2,h1));
	y1 = _mm_add_pd(y1, _mm_mul_pd(q1,h2));
	y2 = _mm_add_pd(y2, _mm_mul_pd(q2,h2));

	h1 = _mm_loaddup_pd(&hh[nb-1]);

	q1 = _mm_load_pd(&q[(nb+2)*ldq]);
	q2 = _mm_load_pd(&q[((nb+2)*ldq)+2]);

	x1 = _mm_add_pd(x1, _mm_mul_pd(q1,h1));
	x2 = _mm_add_pd(x2, _mm_mul_pd(q2,h1));

	/////////////////////////////////////////////////////
	// Rank-1 update of Q [4 x nb+3]
	/////////////////////////////////////////////////////

	__m128d tau1 = _mm_loaddup_pd(&hh[0]);
	__m128d tau2 = _mm_loaddup_pd(&hh[ldh]);
	__m128d tau3 = _mm_loaddup_pd(&hh[ldh*2]);
	__m128d tau4 = _mm_loaddup_pd(&hh[ldh*3]);

	__m128d vs_1_2 = _mm_loaddup_pd(&s_1_2);
	__m128d vs_1_3 = _mm_loaddup_pd(&s_1_3);
	__m128d vs_2_3 = _mm_loaddup_pd(&s_2_3);
	__m128d vs_1_4 = _mm_loaddup_pd(&s_1_4);
	__m128d vs_2_4 = _mm_loaddup_pd(&s_2_4);
	__m128d vs_3_4 = _mm_loaddup_pd(&s_3_4);

	h1 = tau1;
	x1 = _mm_mul_pd(x1, h1);
	x2 = _mm_mul_pd(x2, h1);

	h1 = tau2;
	h2 = _mm_mul_pd(h1, vs_1_2);
705

706 707 708 709 710 711
	y1 = _mm_sub_pd(_mm_mul_pd(y1,h1), _mm_mul_pd(x1,h2));
	y2 = _mm_sub_pd(_mm_mul_pd(y2,h1), _mm_mul_pd(x2,h2));

	h1 = tau3;
	h2 = _mm_mul_pd(h1, vs_1_3);
	h3 = _mm_mul_pd(h1, vs_2_3);
712

713 714 715 716 717 718 719
	z1 = _mm_sub_pd(_mm_mul_pd(z1,h1), _mm_add_pd(_mm_mul_pd(y1,h3), _mm_mul_pd(x1,h2)));
	z2 = _mm_sub_pd(_mm_mul_pd(z2,h1), _mm_add_pd(_mm_mul_pd(y2,h3), _mm_mul_pd(x2,h2)));

	h1 = tau4;
	h2 = _mm_mul_pd(h1, vs_1_4);
	h3 = _mm_mul_pd(h1, vs_2_4);
	h4 = _mm_mul_pd(h1, vs_3_4);
720

721 722 723 724 725 726 727 728 729 730 731 732 733
	w1 = _mm_sub_pd(_mm_mul_pd(w1,h1), _mm_add_pd(_mm_mul_pd(z1,h4), _mm_add_pd(_mm_mul_pd(y1,h3), _mm_mul_pd(x1,h2))));
	w2 = _mm_sub_pd(_mm_mul_pd(w2,h1), _mm_add_pd(_mm_mul_pd(z2,h4), _mm_add_pd(_mm_mul_pd(y2,h3), _mm_mul_pd(x2,h2))));

	q1 = _mm_load_pd(&q[0]);
	q2 = _mm_load_pd(&q[2]);
	q1 = _mm_sub_pd(q1, w1);
	q2 = _mm_sub_pd(q2, w2);
	_mm_store_pd(&q[0],q1);
	_mm_store_pd(&q[2],q2);

	h4 = _mm_loaddup_pd(&hh[(ldh*3)+1]);
	q1 = _mm_load_pd(&q[ldq]);
	q2 = _mm_load_pd(&q[ldq+2]);
734

735 736
	q1 = _mm_sub_pd(q1, _mm_add_pd(z1, _mm_mul_pd(w1, h4)));
	q2 = _mm_sub_pd(q2, _mm_add_pd(z2, _mm_mul_pd(w2, h4)));
737

738 739 740 741 742 743 744
	_mm_store_pd(&q[ldq],q1);
	_mm_store_pd(&q[ldq+2],q2);

	h3 = _mm_loaddup_pd(&hh[(ldh*2)+1]);
	h4 = _mm_loaddup_pd(&hh[(ldh*3)+2]);
	q1 = _mm_load_pd(&q[ldq*2]);
	q2 = _mm_load_pd(&q[(ldq*2)+2]);
745

746 747 748 749 750 751 752 753 754 755
	q1 = _mm_sub_pd(q1, _mm_add_pd(y1, _mm_add_pd(_mm_mul_pd(z1, h3), _mm_mul_pd(w1, h4))));
	q2 = _mm_sub_pd(q2, _mm_add_pd(y2, _mm_add_pd(_mm_mul_pd(z2, h3), _mm_mul_pd(w2, h4))));
	_mm_store_pd(&q[ldq*2],q1);
	_mm_store_pd(&q[(ldq*2)+2],q2);

	h2 = _mm_loaddup_pd(&hh[ldh+1]);
	h3 = _mm_loaddup_pd(&hh[(ldh*2)+2]);
	h4 = _mm_loaddup_pd(&hh[(ldh*3)+3]);
	q1 = _mm_load_pd(&q[ldq*3]);
	q2 = _mm_load_pd(&q[(ldq*3)+2]);
756

757 758
	q1 = _mm_sub_pd(q1, _mm_add_pd(x1, _mm_add_pd(_mm_mul_pd(y1, h2), _mm_add_pd(_mm_mul_pd(z1, h3), _mm_mul_pd(w1, h4)))));
	q2 = _mm_sub_pd(q2, _mm_add_pd(x2, _mm_add_pd(_mm_mul_pd(y2, h2), _mm_add_pd(_mm_mul_pd(z2, h3), _mm_mul_pd(w2, h4)))));
759

760 761 762 763 764 765 766 767 768 769 770
	_mm_store_pd(&q[ldq*3], q1);
	_mm_store_pd(&q[(ldq*3)+2], q2);

	for (i = 4; i < nb; i++)
	{
		h1 = _mm_loaddup_pd(&hh[i-3]);
		h2 = _mm_loaddup_pd(&hh[ldh+i-2]);
		h3 = _mm_loaddup_pd(&hh[(ldh*2)+i-1]);
		h4 = _mm_loaddup_pd(&hh[(ldh*3)+i]);

		q1 = _mm_load_pd(&q[i*ldq]);
771

772
		q1 = _mm_sub_pd(q1, _mm_add_pd(_mm_add_pd(_mm_mul_pd(w1, h4), _mm_mul_pd(z1, h3)), _mm_add_pd(_mm_mul_pd(x1,h1), _mm_mul_pd(y1, h2))));
773

774 775 776
		_mm_store_pd(&q[i*ldq],q1);

		q2 = _mm_load_pd(&q[(i*ldq)+2]);
777

778
		q2 = _mm_sub_pd(q2, _mm_add_pd(_mm_add_pd(_mm_mul_pd(w2, h4), _mm_mul_pd(z2, h3)), _mm_add_pd(_mm_mul_pd(x2,h1), _mm_mul_pd(y2, h2))));
779

780 781 782 783 784 785 786 787
		_mm_store_pd(&q[(i*ldq)+2],q2);
	}

	h1 = _mm_loaddup_pd(&hh[nb-3]);
	h2 = _mm_loaddup_pd(&hh[ldh+nb-2]);
	h3 = _mm_loaddup_pd(&hh[(ldh*2)+nb-1]);
	q1 = _mm_load_pd(&q[nb*ldq]);
	q2 = _mm_load_pd(&q[(nb*ldq)+2]);
788

789 790
	q1 = _mm_sub_pd(q1, _mm_add_pd(_mm_add_pd(_mm_mul_pd(z1, h3), _mm_mul_pd(y1, h2)) , _mm_mul_pd(x1, h1)));
	q2 = _mm_sub_pd(q2, _mm_add_pd(_mm_add_pd(_mm_mul_pd(z2, h3), _mm_mul_pd(y2, h2)) , _mm_mul_pd(x2, h1)));
791

792 793 794 795 796 797 798
	_mm_store_pd(&q[nb*ldq],q1);
	_mm_store_pd(&q[(nb*ldq)+2],q2);

	h1 = _mm_loaddup_pd(&hh[nb-2]);
	h2 = _mm_loaddup_pd(&hh[ldh+nb-1]);
	q1 = _mm_load_pd(&q[(nb+1)*ldq]);
	q2 = _mm_load_pd(&q[((nb+1)*ldq)+2]);
799

800 801
	q1 = _mm_sub_pd(q1, _mm_add_pd( _mm_mul_pd(y1, h2) , _mm_mul_pd(x1, h1)));
	q2 = _mm_sub_pd(q2, _mm_add_pd( _mm_mul_pd(y2, h2) , _mm_mul_pd(x2, h1)));
802

803 804 805 806 807 808
	_mm_store_pd(&q[(nb+1)*ldq],q1);
	_mm_store_pd(&q[((nb+1)*ldq)+2],q2);

	h1 = _mm_loaddup_pd(&hh[nb-1]);
	q1 = _mm_load_pd(&q[(nb+2)*ldq]);
	q2 = _mm_load_pd(&q[((nb+2)*ldq)+2]);
809

810 811
	q1 = _mm_sub_pd(q1, _mm_mul_pd(x1, h1));
	q2 = _mm_sub_pd(q2, _mm_mul_pd(x2, h1));
812

813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
	_mm_store_pd(&q[(nb+2)*ldq],q1);
	_mm_store_pd(&q[((nb+2)*ldq)+2],q2);
}

/**
 * Unrolled kernel that computes
 * 2 rows of Q simultaneously, a
 * matrix vector product with two householder
 * vectors + a rank 1 update is performed
 */
__forceinline void hh_trafo_kernel_2_SSE_4hv(double* q, double* hh, int nb, int ldq, int ldh, double s_1_2, double s_1_3, double s_2_3, double s_1_4, double s_2_4, double s_3_4)
{
	/////////////////////////////////////////////////////
	// Matrix Vector Multiplication, Q [2 x nb+3] * hh
	// hh contains four householder vectors
	/////////////////////////////////////////////////////
	int i;

	__m128d a1_1 = _mm_load_pd(&q[ldq*3]);
	__m128d a2_1 = _mm_load_pd(&q[ldq*2]);
	__m128d a3_1 = _mm_load_pd(&q[ldq]);
	__m128d a4_1 = _mm_load_pd(&q[0]);

	__m128d h_2_1 = _mm_loaddup_pd(&hh[ldh+1]);
	__m128d h_3_2 = _mm_loaddup_pd(&hh[(ldh*2)+1]);
	__m128d h_3_1 = _mm_loaddup_pd(&hh[(ldh*2)+2]);
	__m128d h_4_3 = _mm_loaddup_pd(&hh[(ldh*3)+1]);
	__m128d h_4_2 = _mm_loaddup_pd(&hh[(ldh*3)+2]);
	__m128d h_4_1 = _mm_loaddup_pd(&hh[(ldh*3)+3]);

	__m128d w1 = _mm_add_pd(a4_1, _mm_mul_pd(a3_1, h_4_3));
	w1 = _mm_add_pd(w1, _mm_mul_pd(a2_1, h_4_2));
	w1 = _mm_add_pd(w1, _mm_mul_pd(a1_1, h_4_1));
	__m128d z1 = _mm_add_pd(a3_1, _mm_mul_pd(a2_1, h_3_2));
	z1 = _mm_add_pd(z1, _mm_mul_pd(a1_1, h_3_1));
	__m128d y1 = _mm_add_pd(a2_1, _mm_mul_pd(a1_1, h_2_1));
	__m128d x1 = a1_1;

	__m128d q1;

	__m128d h1;
	__m128d h2;
	__m128d h3;
	__m128d h4;

	for(i = 4; i < nb; i++)
	{
		h1 = _mm_loaddup_pd(&hh[i-3]);
		h2 = _mm_loaddup_pd(&hh[ldh+i-2]);
		h3 = _mm_loaddup_pd(&hh[(ldh*2)+i-1]);
		h4 = _mm_loaddup_pd(&hh[(ldh*3)+i]);

		q1 = _mm_load_pd(&q[i*ldq]);
866

867 868 869 870 871 872 873 874 875 876
		x1 = _mm_add_pd(x1, _mm_mul_pd(q1,h1));
		y1 = _mm_add_pd(y1, _mm_mul_pd(q1,h2));
		z1 = _mm_add_pd(z1, _mm_mul_pd(q1,h3));
		w1 = _mm_add_pd(w1, _mm_mul_pd(q1,h4));
	}

	h1 = _mm_loaddup_pd(&hh[nb-3]);
	h2 = _mm_loaddup_pd(&hh[ldh+nb-2]);
	h3 = _mm_loaddup_pd(&hh[(ldh*2)+nb-1]);
	q1 = _mm_load_pd(&q[nb*ldq]);
877

878 879 880 881 882 883 884
	x1 = _mm_add_pd(x1, _mm_mul_pd(q1,h1));
	y1 = _mm_add_pd(y1, _mm_mul_pd(q1,h2));
	z1 = _mm_add_pd(z1, _mm_mul_pd(q1,h3));

	h1 = _mm_loaddup_pd(&hh[nb-2]);
	h2 = _mm_loaddup_pd(&hh[(ldh*1)+nb-1]);
	q1 = _mm_load_pd(&q[(nb+1)*ldq]);
885

886 887 888 889 890
	x1 = _mm_add_pd(x1, _mm_mul_pd(q1,h1));
	y1 = _mm_add_pd(y1, _mm_mul_pd(q1,h2));

	h1 = _mm_loaddup_pd(&hh[nb-1]);
	q1 = _mm_load_pd(&q[(nb+2)*ldq]);
891

892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
	x1 = _mm_add_pd(x1, _mm_mul_pd(q1,h1));
	/////////////////////////////////////////////////////
	// Rank-1 update of Q [2 x nb+3]
	/////////////////////////////////////////////////////

	__m128d tau1 = _mm_loaddup_pd(&hh[0]);
	__m128d tau2 = _mm_loaddup_pd(&hh[ldh]);
	__m128d tau3 = _mm_loaddup_pd(&hh[ldh*2]);
	__m128d tau4 = _mm_loaddup_pd(&hh[ldh*3]);

	__m128d vs_1_2 = _mm_loaddup_pd(&s_1_2);
	__m128d vs_1_3 = _mm_loaddup_pd(&s_1_3);
	__m128d vs_2_3 = _mm_loaddup_pd(&s_2_3);
	__m128d vs_1_4 = _mm_loaddup_pd(&s_1_4);
	__m128d vs_2_4 = _mm_loaddup_pd(&s_2_4);
	__m128d vs_3_4 = _mm_loaddup_pd(&s_3_4);

	h1 = tau1;
	x1 = _mm_mul_pd(x1, h1);

	h1 = tau2;
	h2 = _mm_mul_pd(h1, vs_1_2);
914

915 916 917 918 919
	y1 = _mm_sub_pd(_mm_mul_pd(y1,h1), _mm_mul_pd(x1,h2));

	h1 = tau3;
	h2 = _mm_mul_pd(h1, vs_1_3);
	h3 = _mm_mul_pd(h1, vs_2_3);
920

921 922 923 924 925 926
	z1 = _mm_sub_pd(_mm_mul_pd(z1,h1), _mm_add_pd(_mm_mul_pd(y1,h3), _mm_mul_pd(x1,h2)));

	h1 = tau4;
	h2 = _mm_mul_pd(h1, vs_1_4);
	h3 = _mm_mul_pd(h1, vs_2_4);
	h4 = _mm_mul_pd(h1, vs_3_4);
927

928 929 930 931 932 933 934 935
	w1 = _mm_sub_pd(_mm_mul_pd(w1,h1), _mm_add_pd(_mm_mul_pd(z1,h4), _mm_add_pd(_mm_mul_pd(y1,h3), _mm_mul_pd(x1,h2))));

	q1 = _mm_load_pd(&q[0]);
	q1 = _mm_sub_pd(q1, w1);
	_mm_store_pd(&q[0],q1);

	h4 = _mm_loaddup_pd(&hh[(ldh*3)+1]);
	q1 = _mm_load_pd(&q[ldq]);
936

937
	q1 = _mm_sub_pd(q1, _mm_add_pd(z1, _mm_mul_pd(w1, h4)));
938

939 940 941 942 943
	_mm_store_pd(&q[ldq],q1);

	h3 = _mm_loaddup_pd(&hh[(ldh*2)+1]);
	h4 = _mm_loaddup_pd(&hh[(ldh*3)+2]);
	q1 = _mm_load_pd(&q[ldq*2]);
944

945
	q1 = _mm_sub_pd(q1, _mm_add_pd(y1, _mm_add_pd(_mm_mul_pd(z1, h3), _mm_mul_pd(w1, h4))));
946

947 948 949 950 951 952
	_mm_store_pd(&q[ldq*2],q1);

	h2 = _mm_loaddup_pd(&hh[ldh+1]);
	h3 = _mm_loaddup_pd(&hh[(ldh*2)+2]);
	h4 = _mm_loaddup_pd(&hh[(ldh*3)+3]);
	q1 = _mm_load_pd(&q[ldq*3]);
953

954
	q1 = _mm_sub_pd(q1, _mm_add_pd(x1, _mm_add_pd(_mm_mul_pd(y1, h2), _mm_add_pd(_mm_mul_pd(z1, h3), _mm_mul_pd(w1, h4)))));
955

956 957 958 959 960 961 962 963 964 965
	_mm_store_pd(&q[ldq*3], q1);

	for (i = 4; i < nb; i++)
	{
		h1 = _mm_loaddup_pd(&hh[i-3]);
		h2 = _mm_loaddup_pd(&hh[ldh+i-2]);
		h3 = _mm_loaddup_pd(&hh[(ldh*2)+i-1]);
		h4 = _mm_loaddup_pd(&hh[(ldh*3)+i]);

		q1 = _mm_load_pd(&q[i*ldq]);
966

967
		q1 = _mm_sub_pd(q1, _mm_add_pd(_mm_add_pd(_mm_mul_pd(w1, h4), _mm_mul_pd(z1, h3)), _mm_add_pd(_mm_mul_pd(x1,h1), _mm_mul_pd(y1, h2))));
968

969 970 971 972 973 974 975
		_mm_store_pd(&q[i*ldq],q1);
	}

	h1 = _mm_loaddup_pd(&hh[nb-3]);
	h2 = _mm_loaddup_pd(&hh[ldh+nb-2]);
	h3 = _mm_loaddup_pd(&hh[(ldh*2)+nb-1]);
	q1 = _mm_load_pd(&q[nb*ldq]);
976

977
	q1 = _mm_sub_pd(q1, _mm_add_pd(_mm_add_pd(_mm_mul_pd(z1, h3), _mm_mul_pd(y1, h2)) , _mm_mul_pd(x1, h1)));
978

979 980 981 982 983
	_mm_store_pd(&q[nb*ldq],q1);

	h1 = _mm_loaddup_pd(&hh[nb-2]);
	h2 = _mm_loaddup_pd(&hh[ldh+nb-1]);
	q1 = _mm_load_pd(&q[(nb+1)*ldq]);
984

985
	q1 = _mm_sub_pd(q1, _mm_add_pd( _mm_mul_pd(y1, h2) , _mm_mul_pd(x1, h1)));
986

987 988 989 990
	_mm_store_pd(&q[(nb+1)*ldq],q1);

	h1 = _mm_loaddup_pd(&hh[nb-1]);
	q1 = _mm_load_pd(&q[(nb+2)*ldq]);
991

992
	q1 = _mm_sub_pd(q1, _mm_mul_pd(x1, h1));
993

994 995
	_mm_store_pd(&q[(nb+2)*ldq],q1);
}