diff --git a/.gitignore b/.gitignore index 3657e18..07843e5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ bin/ *.dis *.o +*.dylib .kdev4/ .vscode/ CMakeFiles/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 668fa7b..82e962c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,23 +6,6 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) add_subdirectory(src) -add_executable(cpplab main.cpp) +#add_executable(c_time time.cpp) -add_executable( - sse - src/core/cpu/sse.cpp - src/core/cpu/naive.cpp - src/core/cpu/avx2.cpp - src/core/bmath/complex.cpp - sse.cpp -) - -add_executable( - complex - src/core/bmath/complex.cpp - complex.cpp -) - -add_executable(c_time time.cpp) - -install(TARGETS cpplab RUNTIME DESTINATION bin) +#install(TARGETS cpplab RUNTIME DESTINATION bin) diff --git a/complex.cpp b/complex.cpp deleted file mode 100644 index 4f72495..0000000 --- a/complex.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include -#include - -#include "src/core/bmath/complex.hpp" - -using namespace Core::Bmath; -using namespace std; - -int main(int argc, char** argcv){ - Complex c1(1,1); - Complex c2(2,2); - Complex c3=c1*c2; - Complex c4=c1+c2; - - cout << c1 << "*" << c2 << "=" << c3 << endl; - cout << c1 << "+" << c2 << "=" << c4 << endl; - -} \ No newline at end of file diff --git a/main.cpp b/main.cpp deleted file mode 100644 index 9f69a8d..0000000 --- a/main.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include -#include "src/core/blist.hpp" -#include "src/core/cpu/avx2.hpp" -#include -#include - -using namespace Core::Cpu; - - -int main(int argc, char **argv) { - - - blist list = blist(); - std::cout << "List size: " << list.getSize() <value << std::endl; - - int *values = list.values(); - for(int i=0;i n = node(2); - - //std::cout << "Hello node: " << ni->value << endl; - - /* - std::cout << "Hello node: " << ni->value << endl; - std::cout << "Hello, world!" << std::endl; - std::cout << "Hello Blist: " << "Hello" << std::endl; - - //Template structs - struct node stringNode; - stringNode.value = std::string("Hello Master"); - - std::cout << stringNode.value << std::endl; - */ - - return 0; -} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e30d712..35972e7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,5 @@ -add_subdirectory(core) +add_subdirectory(bmath) +add_subdirectory(cpu) +add_subdirectory(dtstruct) add_subdirectory(opengl) add_subdirectory(misc) \ No newline at end of file diff --git a/src/bmath/CMakeLists.txt b/src/bmath/CMakeLists.txt new file mode 100644 index 0000000..7dd23fb --- /dev/null +++ b/src/bmath/CMakeLists.txt @@ -0,0 +1,10 @@ +add_library( + bmath SHARED + complex.cpp +) + +add_executable( + complex + demos/complex.cpp + complex.cpp +) \ No newline at end of file diff --git a/src/bmath/complex.cpp b/src/bmath/complex.cpp new file mode 100644 index 0000000..1f1dc85 --- /dev/null +++ b/src/bmath/complex.cpp @@ -0,0 +1,48 @@ +#include "complex.hpp" + +/** + * Constructor implementation + */ +BMath::Complex::Complex(Double real, Double imaginary) +{ + this->real = real; + this->imaginary = imaginary; +}; + +BMath::Complex::Complex(const Complex &other) +{ + this->real = other.real; + this->imaginary = other.imaginary; +} + +/** + * Add complex number + */ +BMath::Complex &BMath::Complex::operator+=(const Complex &rightOp) +{ + this->real += rightOp.real; + this->imaginary += rightOp.imaginary; + return *this; +}; + +BMath::Complex BMath::Complex::operator-(const Complex &rightOp) +{ + return Complex( + this->real-rightOp.real, + this->imaginary-rightOp.imaginary + ); +}; + +BMath::Complex BMath::Complex::operator+(const Complex &rightOp) +{ + return Complex( + this->real + rightOp.real, + this->imaginary + rightOp.imaginary); +}; + +BMath::Complex BMath::Complex::operator*(const Complex &rigthOp) +{ + return Complex( + this->real * rigthOp.real - this->imaginary * rigthOp.imaginary, + this->imaginary * rigthOp.real + this->real * rigthOp.imaginary); +}; diff --git a/src/bmath/complex.hpp b/src/bmath/complex.hpp new file mode 100644 index 0000000..01f6838 --- /dev/null +++ b/src/bmath/complex.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include "../cpu/types.hpp" +#include + +using namespace std; +namespace BMath +{ +class Complex +{ +private: + Double real; + Double imaginary; + +public: + Complex(Double real, Double imaginary); + Complex(const Complex &other); + Complex &operator+=(const Complex &rightOp); + Complex operator+(const Complex &rightOp); + Complex operator-(const Complex &rigthOp); + Complex operator*(const Complex &rightOp); + + //Overload to enable toString operations + friend std::ostream &operator<<(std::ostream &stream, BMath::Complex const &c) + { + return stream << "(" << c.real << "+" << c.imaginary << "i)"; + } +}; +}; // namespace Bmath \ No newline at end of file diff --git a/src/bmath/demos/complex.cpp b/src/bmath/demos/complex.cpp new file mode 100644 index 0000000..e542c17 --- /dev/null +++ b/src/bmath/demos/complex.cpp @@ -0,0 +1,12 @@ +#include +#include "../complex.hpp" + +using BMath::Complex; + +int main(void) +{ + std::cout << "Complex numbers " << std::endl; + Complex c1(1,1); + Complex c2(2,2); + std::cout << c1 << c2 << c2-c1 << c2+c1 < - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef BLIST_H -#define BLIST_H - -#include -#include -#include - -template -class blist -{ -public: - - typedef struct node{ - node(T v,node* p,node* n){ - value = v; - next = n; - previous = p; - } - node(T v){ - value=v; - next=NULL; - previous=NULL; - } - T value; - node* next; - node* previous; - } Node; - - blist(){ - this->rootNode = NULL; - this->currentNode=this->rootNode; - size = 0; - }; - blist(T value){ - this->rootNode=new Node(value,NULL,NULL); - this->currentNode=this->rootNode; - size = 1; - }; - blist(const blist& other){ - this->rootNode = other.root(); - }; - void add(T value){ - if(this->rootNode==NULL){ - this->rootNode=new Node(value,NULL,NULL); - this->currentNode=rootNode; - }else{ - Node* newNode = new Node(value,currentNode,NULL); - this->currentNode->next = newNode; - currentNode=newNode; - } - size++; - }; - ~blist(){ - std::cout << "Destructing list" << std::endl; - Node *aux; - while(this->currentNode!=this->rootNode){ - aux = this->currentNode; - this->currentNode = this->currentNode->previous; - delete aux; - } - delete this->rootNode; - }; - blist& operator=(const blist& other){ - this->rootNode = other.root(); - }; - bool operator==(const blist& other){ - this->rootNode==other.root(); - }; - Node * root() const { - return this->rootNode; - }; - Node * current() const { - return this->currentNode; - }; - - T* values() { - T *aux = new T[size]; - Node *naux = this->rootNode; - for(int i=0;ivalue; - naux=naux->next; - } - return aux; - }; - int getSize(){ - return size; - }; -private: - Node * rootNode; - Node * currentNode; - int size; -}; - -#endif // BLIST_H diff --git a/src/core/bmath/complex.cpp b/src/core/bmath/complex.cpp deleted file mode 100644 index 7a209b9..0000000 --- a/src/core/bmath/complex.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "complex.hpp" - -/** - * Constructor implementation - */ -Core::Bmath::Complex::Complex(Double real,Double imaginary){ - this->real=real; - this->imaginary=imaginary; -}; - -/** - * Add complex number - */ -Core::Bmath::Complex& Core::Bmath::Complex::operator+= (const Complex& rightOp){ - this->real+=rightOp.real; - this->imaginary+=rightOp.imaginary; - return *this; -}; - -Core::Bmath::Complex Core::Bmath::Complex::operator+ (const Complex& rightOp){ - return Complex( - this->real+rightOp.real, - this->imaginary+rightOp.imaginary - ); -}; - -Core::Bmath::Complex Core::Bmath::Complex::operator* (const Complex& rigthOp){ - return Complex( - this->real*rigthOp.real-this->imaginary*rigthOp.imaginary, - this->imaginary*rigthOp.real+this->real*rigthOp.imaginary - ); -}; diff --git a/src/core/bmath/complex.hpp b/src/core/bmath/complex.hpp deleted file mode 100644 index be145ec..0000000 --- a/src/core/bmath/complex.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef CORE_BMATH_COMPLEX -#define CORE_BMATH_COMPLEX - -#include "../cpu/types.hpp" -#include - -using namespace std; - -namespace Core{ - namespace Bmath{ - class Complex{ - private: - Double real; - Double imaginary; - - public: - Complex(Double real,Double imaginary); - Complex& operator+=(const Complex& rightOp); - Complex operator+(const Complex &rightOp); - Complex operator*(const Complex &rightOp); - - //Overload to enable toString operations - friend std::ostream& operator<<(std::ostream &stream, Core::Bmath::Complex const &c){ - return stream << "(" << c.real << "+" << c.imaginary << "i)"; - } - }; - }; -}; - -#endif \ No newline at end of file diff --git a/src/core/cpu/CMakeLists.txt b/src/core/cpu/CMakeLists.txt deleted file mode 100644 index e69de29..0000000 diff --git a/src/core/cpu/avx2.cpp b/src/core/cpu/avx2.cpp deleted file mode 100644 index 610c74e..0000000 --- a/src/core/cpu/avx2.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "avx2.hpp" -#include -#include - - -void Core::Cpu::avx2::avx_sum(int *a,int *b){ - -} - -void Core::Cpu::avx2::printAVX2Num(int *num){ - for(int i=0;i -#include "types.hpp" - - -namespace Core { - namespace Cpu { - class Utils { - public: - //Lengths - 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 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 UInt MASK_32=0xFFFFFFFF; - static const UInt MASK_16=0xFFFF; - static const UInt MASK_8=0xFF; - - //Shifts - static const UChar SHIFT_32 = 32; - static const UChar SHIFT_16 = 16; - static const UChar SHIFT_8 = 8; - - /** - * Template function (generics) to enable the printing of several datatypes - */ - template - static void printHex(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; - }; - }; - }; -}; - -#endif \ No newline at end of file diff --git a/src/cpu/CMakeLists.txt b/src/cpu/CMakeLists.txt new file mode 100644 index 0000000..038945a --- /dev/null +++ b/src/cpu/CMakeLists.txt @@ -0,0 +1,7 @@ +add_executable( + sse + demos/sse.cpp + sse.cpp + naive.cpp + avx2.cpp +) \ No newline at end of file diff --git a/src/cpu/avx2.cpp b/src/cpu/avx2.cpp new file mode 100644 index 0000000..cb0c724 --- /dev/null +++ b/src/cpu/avx2.cpp @@ -0,0 +1,19 @@ +#include "avx2.hpp" +#include +#include + +void Cpu::avx2::avx_sum(int *a, int *b) +{ +} + +void Cpu::avx2::printAVX2Num(int *num) +{ + for (int i = 0; i < avx2::AVX2_INT_SIZE; i++) + { + cout << i; + if (i < avx2::AVX2_INT_SIZE - 1) + { + cout << ","; + } + } +} \ No newline at end of file diff --git a/src/cpu/avx2.hpp b/src/cpu/avx2.hpp new file mode 100644 index 0000000..ba7b045 --- /dev/null +++ b/src/cpu/avx2.hpp @@ -0,0 +1,13 @@ +#pragma once + +using namespace std; +namespace Cpu +{ +class avx2 +{ +public: + static int const AVX2_INT_SIZE = 16; + static void avx_sum(int *a, int *b); + static void printAVX2Num(int *num); +}; +}; diff --git a/src/cpu/demos/sse.cpp b/src/cpu/demos/sse.cpp new file mode 100644 index 0000000..4c202e8 --- /dev/null +++ b/src/cpu/demos/sse.cpp @@ -0,0 +1,63 @@ +#include +#include "../utils.hpp" +#include "../sse.hpp" +#include "../naive.hpp" +#include + +using namespace Cpu; +using namespace std; + +long int gettime(){ + struct timeval tp; + gettimeofday(&tp, NULL); + long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000; + return ms; +}; + +#define MAX_ITER 1000*1000*100 + +int main(int argc, char** argcv){ + UInt v1_128[INT_LEN_128] = { 0x1, 0x2,0x3,0x4 }; + UInt v2_128[INT_LEN_128] = { 0x1, 0x2,0x3,0x4 }; + + ULong v1_128_l[LONG_LEN_128]; + ULong v2_128_l[LONG_LEN_128]; + + Utils::int128BitToLong(v1_128,v1_128_l); + Utils::int128BitToLong(v2_128,v2_128_l); + + SSE::paddw(v1_128,v2_128); + + long int start,end; + + Utils::printHex(v1_128,INT_LEN_128); + Utils::printHex(v2_128,INT_LEN_128); + + Utils::printHex(v1_128_l,LONG_LEN_128); + Utils::printHex(v2_128_l,LONG_LEN_128); + + start = gettime(); + for(int i=0;i -void Core::Cpu::Naive::sum_128_long(UInt *a,UInt *b){ +void 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]; diff --git a/src/cpu/naive.hpp b/src/cpu/naive.hpp new file mode 100644 index 0000000..e6cabad --- /dev/null +++ b/src/cpu/naive.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "types.hpp" + +using namespace std; +/** + * This will define a set of functions + */ +namespace Cpu +{ +class Naive +{ +public: + //Methods + static void sum_128_long(UInt *a, UInt *b); +}; +}; \ No newline at end of file diff --git a/src/core/cpu/sse.cpp b/src/cpu/sse.cpp similarity index 83% rename from src/core/cpu/sse.cpp rename to src/cpu/sse.cpp index 43e7173..9db8e3d 100644 --- a/src/core/cpu/sse.cpp +++ b/src/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::paddw(UInt *a,UInt *b) { +void Cpu::SSE::paddw(UInt *a,UInt *b) { asm( "movdqa %0, %%xmm1\n" "paddw %1, %%xmm1\n" @@ -14,7 +14,7 @@ void Core::Cpu::SSE::paddw(UInt *a,UInt *b) { //X86 Assembly to add two 128 bit numbers in the form of packed long 64bit -void Core::Cpu::SSE::paddd(ULong *a,ULong *b) { +void Cpu::SSE::paddd(ULong *a,ULong *b) { asm( "movdqa %0, %%xmm1\n" "paddd %1, %%xmm1\n" diff --git a/src/cpu/sse.hpp b/src/cpu/sse.hpp new file mode 100644 index 0000000..479bb1c --- /dev/null +++ b/src/cpu/sse.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "types.hpp" + +using namespace std; + + +namespace Cpu +{ +class SSE +{ +public: + static void paddw(UInt *a, UInt *b); + static void paddd(ULong *a, ULong *b); +}; +}; \ No newline at end of file diff --git a/src/cpu/types.hpp b/src/cpu/types.hpp new file mode 100644 index 0000000..1cad1df --- /dev/null +++ b/src/cpu/types.hpp @@ -0,0 +1,42 @@ +#pragma once +/* + * Types for x86 architectures + */ +#if defined(__x86_64__) + //Signed type alias + 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; + + //SSE DataTypes + #define INT_LEN_64 2 + #define INT_LEN_128 4 + #define INT_LEN_256 8 + #define INT_LEN_512 16 + + #define LONG_LEN_64 1 + #define LONG_LEN_128 2 + #define LONG_LEN_256 4 + #define LONG_LEN_512 8 + + //Masks + #define MASK_32 0xFFFFFFFF + #define MASK_16 0xFFFF + #define MASK_8 0xFF + + //Shifts + #define SHIFT_32 32 + #define SHIFT_16 16 + #define SHIFT_8 8 +#endif \ No newline at end of file diff --git a/src/cpu/utils.hpp b/src/cpu/utils.hpp new file mode 100644 index 0000000..2e1b1b5 --- /dev/null +++ b/src/cpu/utils.hpp @@ -0,0 +1,50 @@ +#pragma once + +#include +#include "types.hpp" + +using namespace std; + +namespace Cpu +{ +class Utils +{ +public: + template + static void printHex(Number *num, Int len) + { + for (int i = 0; i < len; i++) + { + cout << hex << num[i]; + if (i < len - 1) + { + cout << ","; + } + } + cout << std::dec << 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; + }; +}; +}; // namespace Cpu \ No newline at end of file diff --git a/src/core/bmath/CMakeLists.txt b/src/dtstruct/CMakeLists.txt similarity index 100% rename from src/core/bmath/CMakeLists.txt rename to src/dtstruct/CMakeLists.txt diff --git a/src/core/blist.cpp b/src/dtstruct/blist.cpp similarity index 100% rename from src/core/blist.cpp rename to src/dtstruct/blist.cpp diff --git a/src/dtstruct/blist.hpp b/src/dtstruct/blist.hpp new file mode 100644 index 0000000..2c25593 --- /dev/null +++ b/src/dtstruct/blist.hpp @@ -0,0 +1,126 @@ +/* + * Copyright 2017 Vitor Fernandes + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#pragma once + +#include +#include +#include + +template +class blist +{ +public: + typedef struct node + { + node(T v, node *p, node *n) + { + value = v; + next = n; + previous = p; + } + node(T v) + { + value = v; + next = NULL; + previous = NULL; + } + T value; + node *next; + node *previous; + } Node; + + blist() + { + this->rootNode = NULL; + this->currentNode = this->rootNode; + size = 0; + }; + blist(T value) + { + this->rootNode = new Node(value, NULL, NULL); + this->currentNode = this->rootNode; + size = 1; + }; + blist(const blist &other) + { + this->rootNode = other.root(); + }; + void add(T value) + { + if (this->rootNode == NULL) + { + this->rootNode = new Node(value, NULL, NULL); + this->currentNode = rootNode; + } + else + { + Node *newNode = new Node(value, currentNode, NULL); + this->currentNode->next = newNode; + currentNode = newNode; + } + size++; + }; + ~blist() + { + std::cout << "Destructing list" << std::endl; + Node *aux; + while (this->currentNode != this->rootNode) + { + aux = this->currentNode; + this->currentNode = this->currentNode->previous; + delete aux; + } + delete this->rootNode; + }; + blist &operator=(const blist &other) + { + this->rootNode = other.root(); + }; + bool operator==(const blist &other) + { + this->rootNode == other.root(); + }; + Node *root() const + { + return this->rootNode; + }; + Node *current() const + { + return this->currentNode; + }; + + T *values() + { + T *aux = new T[size]; + Node *naux = this->rootNode; + for (int i = 0; i < size; i++) + { + aux[i] = naux->value; + naux = naux->next; + } + return aux; + }; + int getSize() + { + return size; + }; + +private: + Node *rootNode; + Node *currentNode; + int size; +}; \ No newline at end of file