4.1.1.2.2 : Le fichier source

Écrivons le fichier sqrt_vectorize.cpp :



Il nous faut tout d'abord inclure un header qui permet d'utiliser la racine carré :

1
#include <cmath>


Ensuite, nous devons inclure un header qui permet de définir l'alignement des données en float avec PLIB_VECTOR_SIZE_FLOAT :

1
#include "phoenix_intrinsics.h"


Après, nous incluons notre header :

1
#include "sqrt_vectorize.h"


Ensuite, nous implémentons notre produit de hadamard en prennant en compte le fait que nous devons indiquer au compilateur que les tableaux que nous utiliserons seront bien alignés en mémoire sur PLIB_VECTOR_SIZE_BYTE_FLOAT octets avec la fonction __builtin_assume_aligned :

1
2
3
4
5
6
7
8
9
10
11
12
13
///Do the sqrt
/**	@param[out] ptabResult : table of results of sqrt(tabX)
 * 	@param ptabX : input table
 * 	@param nbElement : number of elements in the tables
*/
void sqrt_vectorize(float* __restrict__ ptabResult, const float* __restrict__ ptabX, size_t nbElement){
	float* tabResult = (float*)__builtin_assume_aligned(ptabResult, PLIB_VECTOR_SIZE_BYTE_FLOAT);
	const float* tabX = (const float*)__builtin_assume_aligned(ptabX, PLIB_VECTOR_SIZE_BYTE_FLOAT);
	
	for(size_t i(0lu); i < nbElement; ++i){
		tabResult[i] = std::sqrt(tabX[i]);
	}
}


Le fichier sqrt_vectorize.cpp complet :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/***************************************
	Auteur : Pierre Aubert
	Mail : pierre.aubert@lapp.in2p3.fr
	Licence : CeCILL-C
****************************************/
#include <cmath>
#include "phoenix_intrinsics.h"
#include "sqrt_vectorize.h"
///Do the sqrt
/**	@param[out] ptabResult : table of results of sqrt(tabX)
 * 	@param ptabX : input table
 * 	@param nbElement : number of elements in the tables
*/
void sqrt_vectorize(float* __restrict__ ptabResult, const float* __restrict__ ptabX, size_t nbElement){
	float* tabResult = (float*)__builtin_assume_aligned(ptabResult, PLIB_VECTOR_SIZE_BYTE_FLOAT);
	const float* tabX = (const float*)__builtin_assume_aligned(ptabX, PLIB_VECTOR_SIZE_BYTE_FLOAT);
	
	for(size_t i(0lu); i < nbElement; ++i){
		tabResult[i] = std::sqrt(tabX[i]);
	}
}


Vous pouvez le télécharger ici.