This commit is contained in:
Vitor Fernandes 2020-07-16 01:20:12 +01:00
parent 6bd2593d3e
commit 757b73238e
No known key found for this signature in database
GPG key ID: EBFB4EE09F348A26
2 changed files with 12 additions and 4 deletions

View file

@ -8,10 +8,11 @@ int main(void)
Int a = -12;
Float b = -12.0f;
Int c=3;
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;
std::cout << "MAX(C,D)=(" << c << ", " << d << "), max(C,D): " << Math<Int>::max(d,c) << std::endl;
std::cout << "MIN(C,D)=(" << c << ", " << d << "), min(C,D): " << Math<Int>::min(c,d) << std::endl;
}

View file

@ -1,6 +1,7 @@
#include "math.hpp"
//http://graphics.stanford.edu/~seander/bithacks.html
//https://hbfs.wordpress.com/2008/08/05/branchless-equivalents-of-simple-functions/
//access CHAR_BIT --> const with number of bits in a byte (usually 8)
#include <limits.h>
#include <iostream>
@ -32,7 +33,7 @@ inline Int msb2(Int value)
} SInt;
SInt sint = {.sint = value};
return sint.sint;
return sint.int_with_msb.signal;
};
/**
@ -50,8 +51,14 @@ Float BMath::Math<Float>::abs(Float value)
return value > 0 ? value : -value;
};
template <>
Int BMath::Math<Int>::min(Int a,Int b)
{
return b + ((a-b) & msb2(a-b));
}
template <>
Int BMath::Math<Int>::max(Int a,Int b)
{
return (a-b) & msb2(a-b);
return a + ((b-a) & ~msb2(b-a));
}