Refactoring code

* Cleanup in main.cpp
* Refactor in bmath.h
* Refactor in avx2.hpp avx2.cpp
* Added types.hpp to abstract architecture datatypes
* Refactored utils.hpp
* Refactored sse.cpp
* Added some utility methods for integer, long packed values
* Convert print into a generic method with use of templates
This commit is contained in:
Balhau 2018-12-02 10:09:37 +00:00
parent 7d0629887a
commit 618e5efcfa
11 changed files with 105 additions and 44 deletions

View file

@ -5,8 +5,8 @@
#include <string>
#include <iomanip>
using namespace core::bmath;
using namespace core::cpu;
using namespace Core::bmath;
using namespace Core::Cpu;
int main(int argc, char **argv) {

View file

@ -4,7 +4,7 @@
#include <sys/types.h>
#include <iostream>
namespace core {
namespace Core {
namespace bmath {
class BMath {
public:

View file

@ -3,11 +3,11 @@
#include <string>
void core::cpu::avx2::avx_sum(int *a,int *b){
void Core::Cpu::avx2::avx_sum(int *a,int *b){
}
void core::cpu::avx2::printAVX2Num(int *num){
void Core::Cpu::avx2::printAVX2Num(int *num){
for(int i=0;i<avx2::AVX2_INT_SIZE;i++){
cout << i;
if(i<avx2::AVX2_INT_SIZE-1){

View file

@ -5,8 +5,8 @@ using namespace std;
/**
* This will define a set of functions
*/
namespace core {
namespace cpu {
namespace Core {
namespace Cpu {
class avx2 {
public:
static int const AVX2_INT_SIZE=16;

View file

@ -1,11 +1,12 @@
#include "naive.hpp"
#include "types.hpp"
#include <iostream>
void core::cpu::Naive::sum_128_long(int *a,int *b){
long a1 = ((long)a[0] << 32) | a[1];
long a2 = ((long)a[2] << 32) | a[3];
long b1 = ((long)b[0] << 32) | b[1];
long b2 = ((long)b[2] << 32) | b[3];
void Core::Cpu::Naive::sum_128_long(UInt *a,UInt *b){
ULong a1 = ((ULong)a[0] << 32) | a[1];
ULong a2 = ((ULong)a[2] << 32) | a[3];
ULong b1 = ((ULong)b[0] << 32) | b[1];
ULong b2 = ((ULong)b[2] << 32) | b[3];
a1=a1+b1;
a2=a2+b2;

View file

@ -1,16 +1,18 @@
#ifndef CORE_CPU_NAIVE_H
#define CORE_CPU_NAIVE_H
#include "types.hpp"
using namespace std;
/**
* This will define a set of functions
*/
namespace core {
namespace cpu {
namespace Core {
namespace Cpu {
class Naive {
public:
//Methods
static void sum_128_long(int* a, int* b);
static void sum_128_long(UInt* a, UInt* b);
};
};
};

View file

@ -2,7 +2,7 @@
#include <iostream>
//X86 Assembly to add two 128 bit numbers in the form of packed integers 32bit
void core::cpu::SSE::sum_128(int *a,int *b) {
void Core::Cpu::SSE::sum_128(UInt *a,UInt *b) {
asm(
"movdqa %0, %%xmm1\n"
"paddw %1, %%xmm1\n"
@ -14,7 +14,7 @@ void core::cpu::SSE::sum_128(int *a,int *b) {
//X86 Assembly to add two 128 bit numbers in the form of packed long 64bit
void core::cpu::SSE::sum_128(long *a,long *b) {
void Core::Cpu::SSE::sum_128(ULong *a,ULong *b) {
asm(
"movdqa %0, %%xmm1\n"
"paddd %1, %%xmm1\n"
@ -22,5 +22,4 @@ void core::cpu::SSE::sum_128(long *a,long *b) {
: "=m"(*a)
: "m"(*b)
);
}
};

View file

@ -1,16 +1,18 @@
#ifndef CORE_CPU_SSE_H
#define CORE_CPU_SSE_H
#include "types.hpp"
using namespace std;
/**
* This will define a set of functions
*/
namespace core {
namespace cpu {
namespace Core {
namespace Cpu {
class SSE {
public:
static void sum_128(int *a,int *b);
static void sum_128(long *a,long *b);
static void sum_128(UInt *a,UInt *b);
static void sum_128(ULong *a,ULong *b);
};
};
};

25
src/core/cpu/types.hpp Normal file
View file

@ -0,0 +1,25 @@
#ifndef CORE_CPU_TYPES_H
#define CORE_CPU_TYPES_H
/*
* Types for x86 arquitectures
*/
#if defined(__x86_64__)
//Signed type alias
typedef int Int;
typedef int Int;
typedef long Long;
typedef char Char;
typedef short Short;
//IEEE floating point alias
typedef float Float;
typedef double Double;
//Unsigned type alias
typedef unsigned char UChar;
typedef unsigned short UShort;
typedef unsigned int UInt;
typedef unsigned long ULong;
#endif
#endif

View file

@ -2,30 +2,38 @@
#define CORE_CPU_UTILS_H
#include <iostream>
#include "types.hpp"
using namespace::std;
namespace core {
namespace cpu {
namespace Core {
namespace Cpu {
class Utils {
public:
//Lengths
static const unsigned char INT_LEN_64 = 2;
static const unsigned char INT_LEN_128 = 4;
static const unsigned char INT_LEN_256 = 8;
static const unsigned char INT_LEN_512 = 16;
static const UChar INT_LEN_64 = 2;
static const UChar INT_LEN_128 = 4;
static const UChar INT_LEN_256 = 8;
static const UChar INT_LEN_512 = 16;
static const unsigned char LONG_LEN_64 = 1;
static const unsigned char LONG_LEN_128 = 2;
static const unsigned char LONG_LEN_256 = 4;
static const unsigned char LONG_LEN_512 = 8;
static const UChar LONG_LEN_64 = 1;
static const UChar LONG_LEN_128 = 2;
static const UChar LONG_LEN_256 = 4;
static const UChar LONG_LEN_512 = 8;
//Masks
static const unsigned int MASK_32=0xFFFFFFFF;
static const unsigned int MASK_16=0xFFFF;
static const unsigned int MASK_8=0xFF;
static const UInt MASK_32=0xFFFFFFFF;
static const UInt MASK_16=0xFFFF;
static const UInt MASK_8=0xFF;
static void print(int* num,int len) {
//Shifts
static const UChar SHIFT_32 = 32;
static const UChar SHIFT_16 = 16;
static const UChar SHIFT_8 = 8;
template<typename Number>
static void print(Number *num,Int len) {
for(int i=0;i<len;i++){
cout << num[i] ;
if(i<len-1){
@ -33,7 +41,28 @@ namespace core {
}
}
cout << endl;
}
};
/**
* Utility method to convert a packaged 128bit int to 128bit long
*/
static void int128BitToLong(UInt *packedUInteger,ULong *packedULong){
packedULong[0] = (ULong)packedUInteger[1]<<32 | packedUInteger[0];
packedULong[1] = (ULong)packedUInteger[3]<<32 | packedUInteger[2];
};
/**
* Utility method to convert a packed 128bit long into a 128 int
*/
static void long128BitToInt(ULong *packedULong, UInt *packedUInteger){
//Unpack first long
packedUInteger[0] = packedULong[0] && MASK_32;
packedUInteger[1] = packedULong[0] >> SHIFT_32;
//Unpack second long
packedUInteger[2] = packedULong[1] && MASK_32;
packedUInteger[3] = packedULong[1] >> SHIFT_32;
};
};
};
};

13
sse.cpp
View file

@ -7,7 +7,7 @@
#include "src/core/cpu/sse.hpp"
#include "src/core/cpu/utils.hpp"
using namespace core::cpu;
using namespace Core::Cpu;
long int gettime(){
struct timeval tp;
@ -19,11 +19,14 @@ long int gettime(){
#define MAX_ITER 1000*1000*100
int main(int argc, char** argcv){
int v1_128[Utils::INT_LEN_128] = { 0x1, 0x2,0x3,0x4 };
int v2_128[Utils::INT_LEN_128] = { 0x1, 0x2,0x3,0x4 };
UInt v1_128[Utils::INT_LEN_128] = { 0x1, 0x2,0x3,0x4 };
UInt v2_128[Utils::INT_LEN_128] = { 0x1, 0x2,0x3,0x4 };
long v1_128_l[Utils::LONG_LEN_128] = { (long)0x2<<32 | 0x2, (long)0x4<<32 | 0x3 };
long v2_128_l[Utils::LONG_LEN_128] = { (long)0x2<<32 | 0x2, (long)0x4<<32 | 0x3 };
ULong v1_128_l[Utils::LONG_LEN_128];
ULong v2_128_l[Utils::LONG_LEN_128];
Utils::int128BitToLong(v1_128,v1_128_l);
Utils::int128BitToLong(v2_128,v2_128_l);
long int start,end;