diff --git a/src/bmath/demos/math.cpp b/src/bmath/demos/math.cpp new file mode 100644 index 0000000..f593012 --- /dev/null +++ b/src/bmath/demos/math.cpp @@ -0,0 +1,17 @@ +#include "../math.hpp" +#include + +using namespace BMath; + +int main(void) +{ + Int a = -12; + Float b = -12.0f; + + Int c=3; + Int d=4; + + std::cout << "A: " << a << ", abs(A): " << Math::abs(a) << std::endl; + std::cout << "B: " << b << ", abs(B): " << Math::abs(b) << std::endl; + std::cout << "MAX(C,D)=(" << c << ", " << d << "), max(C,D): " << Math::max(c,d) << std::endl; +} diff --git a/src/bmath/math.cpp b/src/bmath/math.cpp new file mode 100644 index 0000000..e389d62 --- /dev/null +++ b/src/bmath/math.cpp @@ -0,0 +1,57 @@ +#include "math.hpp" + +//http://graphics.stanford.edu/~seander/bithacks.html +//access CHAR_BIT --> const with number of bits in a byte (usually 8) +#include +#include + +/** + * This will get the most significant bit of the Int value. If the most significant bit is 1 then the number is + * 0x11111111 --> equals to -1 integer + */ +inline Int msb(Int value) +{ + return value >> (CHAR_BIT * sizeof(Int) - 1); +}; + +/** + * This version of msb2 avoids the use of bitwise operators by destructuring the datastructure + * with the help of union and struct trickery. + * We basically define a new Union datatype named SInt we use the structure int_with_msb as a way to de-structure + * the Int sint information. + */ +inline Int msb2(Int value) +{ + typedef union { + struct + { + Int v : 31; + Int signal : 1; + } int_with_msb; + Int sint; + } SInt; + + SInt sint = {.sint = value}; + return sint.sint; +}; + +/** + * + */ +template <> +Int BMath::Math::abs(Int value) +{ + return (value ^ msb2(value)) - msb2(value); +}; + +template <> +Float BMath::Math::abs(Float value) +{ + return value > 0 ? value : -value; +}; + +template <> +Int BMath::Math::max(Int a,Int b) +{ + return (a-b) & msb2(a-b); +} diff --git a/src/bmath/math.hpp b/src/bmath/math.hpp new file mode 100644 index 0000000..cfe85b7 --- /dev/null +++ b/src/bmath/math.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "../cpu/types.hpp" + +namespace BMath +{ +template +class Math +{ +public: + static T abs(T value); + static T min(T a, T b); + static T max(T a, T b); +}; +} // namespace BMath \ No newline at end of file diff --git a/src/misc/memory/CMakeLists.txt b/src/misc/memory/CMakeLists.txt new file mode 100644 index 0000000..919ea12 --- /dev/null +++ b/src/misc/memory/CMakeLists.txt @@ -0,0 +1,4 @@ +add_executable( + spointer + smartpointer.cpp +) diff --git a/src/misc/memory/smartpointer.cpp b/src/misc/memory/smartpointer.cpp new file mode 100644 index 0000000..a31e68c --- /dev/null +++ b/src/misc/memory/smartpointer.cpp @@ -0,0 +1,71 @@ +#include "smartpointer.h" +#include + +#define UPtr std::unique_ptr +#define SPtr std::shared_ptr + +DummyObject::DummyObject(int num_elements) +{ + std::cout << "Constructor called " << num_elements << std::endl; + size = num_elements; + list_of_numbers = new int[size]; +} + +DummyObject::~DummyObject() +{ + std::cout << "Destructor called " << size << std::endl; + delete[] list_of_numbers; +} + +DummyObject::DummyObject(const DummyObject &other) +{ + std::cout << "Copy constructor called " << other.size << std::endl; + list_of_numbers = new int[other.size]; + for (int i = 0; i < other.size; i++) + { + list_of_numbers[i] = other.list_of_numbers[i]; + } + size = other.size; +} + +DummyObject::DummyObject(DummyObject&& moved) +{ + std::cout << "Move constructor called " << moved.size << std::endl; +} + +void DummyObject::doSomething() +{ + std::cout << "This do amazing stuff: " << size << std::endl; +} + +DummyObject createDummy(int size) +{ + DummyObject d(size); + return d; +} + +int main(void) +{ + std::cout << "Smart pointer examples" << std::endl; + + DummyObject d(15); + DummyObject copy = d; + + DummyObject *dmtPtr = new DummyObject(20); + UPtr dmySmtPtr = UPtr(new DummyObject(30)); + + dmySmtPtr->doSomething(); + dmtPtr->doSomething(); + + SPtr firstSmtPtr = SPtr(dmtPtr); + SPtr secondSmtPtr = firstSmtPtr; + + DummyObject retDummy = createDummy(32); + DummyObject secondDummy = std::move(retDummy); + + retDummy.doSomething(); + secondDummy.doSomething(); + + firstSmtPtr->doSomething(); + secondSmtPtr->doSomething(); +} \ No newline at end of file diff --git a/src/misc/memory/smartpointer.h b/src/misc/memory/smartpointer.h new file mode 100644 index 0000000..07b537f --- /dev/null +++ b/src/misc/memory/smartpointer.h @@ -0,0 +1,19 @@ +#pragma once + +#include + +class DummyObject { + public: + DummyObject(int); //Constructor + ~DummyObject(); //destructor + DummyObject(const DummyObject&); //copy constructor + DummyObject(DummyObject&&); //move constructor + + void doSomething(); + + + private: + int size; + int* list_of_numbers; + +}; \ No newline at end of file