From 618e5efcfad194f0a5d552a942fcbfadd9235684 Mon Sep 17 00:00:00 2001 From: Balhau Date: Sun, 2 Dec 2018 10:09:37 +0000 Subject: [PATCH] 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 --- main.cpp | 4 +-- src/core/bmath/bmath.h | 2 +- src/core/cpu/avx2.cpp | 4 +-- src/core/cpu/avx2.hpp | 4 +-- src/core/cpu/naive.cpp | 11 ++++---- src/core/cpu/naive.hpp | 8 +++--- src/core/cpu/sse.cpp | 7 +++-- src/core/cpu/sse.hpp | 10 ++++--- src/core/cpu/types.hpp | 25 ++++++++++++++++++ src/core/cpu/utils.hpp | 59 +++++++++++++++++++++++++++++++----------- sse.cpp | 15 ++++++----- 11 files changed, 105 insertions(+), 44 deletions(-) create mode 100644 src/core/cpu/types.hpp diff --git a/main.cpp b/main.cpp index 3de924f..0c650f2 100644 --- a/main.cpp +++ b/main.cpp @@ -5,8 +5,8 @@ #include #include -using namespace core::bmath; -using namespace core::cpu; +using namespace Core::bmath; +using namespace Core::Cpu; int main(int argc, char **argv) { diff --git a/src/core/bmath/bmath.h b/src/core/bmath/bmath.h index 756990f..df548e5 100644 --- a/src/core/bmath/bmath.h +++ b/src/core/bmath/bmath.h @@ -4,7 +4,7 @@ #include #include -namespace core { +namespace Core { namespace bmath { class BMath { public: diff --git a/src/core/cpu/avx2.cpp b/src/core/cpu/avx2.cpp index 238e4be..610c74e 100644 --- a/src/core/cpu/avx2.cpp +++ b/src/core/cpu/avx2.cpp @@ -3,11 +3,11 @@ #include -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 -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; diff --git a/src/core/cpu/naive.hpp b/src/core/cpu/naive.hpp index 4abc4ef..17becbf 100644 --- a/src/core/cpu/naive.hpp +++ b/src/core/cpu/naive.hpp @@ -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); }; }; }; diff --git a/src/core/cpu/sse.cpp b/src/core/cpu/sse.cpp index e0ff2c8..98db493 100644 --- a/src/core/cpu/sse.cpp +++ b/src/core/cpu/sse.cpp @@ -2,7 +2,7 @@ #include //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) ); -} - +}; \ No newline at end of file diff --git a/src/core/cpu/sse.hpp b/src/core/cpu/sse.hpp index da11868..90a9e63 100644 --- a/src/core/cpu/sse.hpp +++ b/src/core/cpu/sse.hpp @@ -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); }; }; }; diff --git a/src/core/cpu/types.hpp b/src/core/cpu/types.hpp new file mode 100644 index 0000000..c25cd5f --- /dev/null +++ b/src/core/cpu/types.hpp @@ -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 \ No newline at end of file diff --git a/src/core/cpu/utils.hpp b/src/core/cpu/utils.hpp index 7e1cb5c..19116a6 100644 --- a/src/core/cpu/utils.hpp +++ b/src/core/cpu/utils.hpp @@ -2,30 +2,38 @@ #define CORE_CPU_UTILS_H #include +#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 + static void print(Number *num,Int len) { for(int i=0;i> SHIFT_32; + + //Unpack second long + packedUInteger[2] = packedULong[1] && MASK_32; + packedUInteger[3] = packedULong[1] >> SHIFT_32; + }; }; }; }; diff --git a/sse.cpp b/sse.cpp index fd64ebe..4a581d3 100644 --- a/sse.cpp +++ b/sse.cpp @@ -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; @@ -49,6 +52,6 @@ int main(int argc, char** argcv){ SSE::sum_128(v1_128_l,v2_128_l); } end = gettime(); - cout << "SSE Approach paddd: " << end-start << endl; + cout << "SSE Approach paddd: " << end-start << endl; } \ No newline at end of file