Fix msb2
This commit is contained in:
parent
6bd2593d3e
commit
757b73238e
2 changed files with 12 additions and 4 deletions
|
@ -8,10 +8,11 @@ int main(void)
|
||||||
Int a = -12;
|
Int a = -12;
|
||||||
Float b = -12.0f;
|
Float b = -12.0f;
|
||||||
|
|
||||||
Int c=3;
|
Int c=-3;
|
||||||
Int d=4;
|
Int d=4;
|
||||||
|
|
||||||
std::cout << "A: " << a << ", abs(A): " << Math<Int>::abs(a) << std::endl;
|
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 << "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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "math.hpp"
|
#include "math.hpp"
|
||||||
|
|
||||||
//http://graphics.stanford.edu/~seander/bithacks.html
|
//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)
|
//access CHAR_BIT --> const with number of bits in a byte (usually 8)
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
@ -32,7 +33,7 @@ inline Int msb2(Int value)
|
||||||
} SInt;
|
} SInt;
|
||||||
|
|
||||||
SInt sint = {.sint = value};
|
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;
|
return value > 0 ? value : -value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
Int BMath::Math<Int>::min(Int a,Int b)
|
||||||
|
{
|
||||||
|
return b + ((a-b) & msb2(a-b));
|
||||||
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
Int BMath::Math<Int>::max(Int a,Int b)
|
Int BMath::Math<Int>::max(Int a,Int b)
|
||||||
{
|
{
|
||||||
return (a-b) & msb2(a-b);
|
return a + ((b-a) & ~msb2(b-a));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue