More on math
This commit is contained in:
parent
9cc9397ec9
commit
6bd2593d3e
6 changed files with 183 additions and 0 deletions
17
src/bmath/demos/math.cpp
Normal file
17
src/bmath/demos/math.cpp
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#include "../math.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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<Int>::abs(a) << std::endl;
|
||||||
|
std::cout << "B: " << b << ", abs(B): " << Math<Float>::abs(b) << std::endl;
|
||||||
|
std::cout << "MAX(C,D)=(" << c << ", " << d << "), max(C,D): " << Math<Int>::max(c,d) << std::endl;
|
||||||
|
}
|
57
src/bmath/math.cpp
Normal file
57
src/bmath/math.cpp
Normal file
|
@ -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 <limits.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<Int>::abs(Int value)
|
||||||
|
{
|
||||||
|
return (value ^ msb2(value)) - msb2(value);
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
Float BMath::Math<Float>::abs(Float value)
|
||||||
|
{
|
||||||
|
return value > 0 ? value : -value;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
Int BMath::Math<Int>::max(Int a,Int b)
|
||||||
|
{
|
||||||
|
return (a-b) & msb2(a-b);
|
||||||
|
}
|
15
src/bmath/math.hpp
Normal file
15
src/bmath/math.hpp
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../cpu/types.hpp"
|
||||||
|
|
||||||
|
namespace BMath
|
||||||
|
{
|
||||||
|
template <typename T>
|
||||||
|
class Math
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static T abs(T value);
|
||||||
|
static T min(T a, T b);
|
||||||
|
static T max(T a, T b);
|
||||||
|
};
|
||||||
|
} // namespace BMath
|
4
src/misc/memory/CMakeLists.txt
Normal file
4
src/misc/memory/CMakeLists.txt
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
add_executable(
|
||||||
|
spointer
|
||||||
|
smartpointer.cpp
|
||||||
|
)
|
71
src/misc/memory/smartpointer.cpp
Normal file
71
src/misc/memory/smartpointer.cpp
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
#include "smartpointer.h"
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#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<DummyObject> dmySmtPtr = UPtr<DummyObject>(new DummyObject(30));
|
||||||
|
|
||||||
|
dmySmtPtr->doSomething();
|
||||||
|
dmtPtr->doSomething();
|
||||||
|
|
||||||
|
SPtr<DummyObject> firstSmtPtr = SPtr<DummyObject>(dmtPtr);
|
||||||
|
SPtr<DummyObject> secondSmtPtr = firstSmtPtr;
|
||||||
|
|
||||||
|
DummyObject retDummy = createDummy(32);
|
||||||
|
DummyObject secondDummy = std::move(retDummy);
|
||||||
|
|
||||||
|
retDummy.doSomething();
|
||||||
|
secondDummy.doSomething();
|
||||||
|
|
||||||
|
firstSmtPtr->doSomething();
|
||||||
|
secondSmtPtr->doSomething();
|
||||||
|
}
|
19
src/misc/memory/smartpointer.h
Normal file
19
src/misc/memory/smartpointer.h
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
};
|
Loading…
Reference in a new issue