diff --git a/src/KKdLib/prj/math.hpp b/src/KKdLib/prj/math.hpp index b16bc44..f45de9f 100644 --- a/src/KKdLib/prj/math.hpp +++ b/src/KKdLib/prj/math.hpp @@ -7,6 +7,7 @@ #include "../default.hpp" #include +#include namespace prj { inline int32_t extract_sign(float_t x) { @@ -40,4 +41,36 @@ namespace prj { else return ceilf(x); } + + inline int32_t extract_sign(double_t x) { + return _mm_movemask_pd(_mm_set_sd(x)) & 0x01; + } + + inline double_t ceil(double_t x) { + int64_t x_int = (int64_t)x; + if (x_int != 0x8000000000000000 && (float_t)x_int != x) + x = (float_t)(x_int + !extract_sign(x)); + return x; + } + + inline double_t floor(double_t x) { + int64_t x_int = (int64_t)x; + if (x_int != 0x8000000000000000 && (float_t)x_int != x) + x = (float_t)(x_int - extract_sign(x)); + return x; + } + + inline double_t roundf(double_t x) { + if (x >= 0.0f) + return floor(x + 0.5f); + else + return ceil(x - 0.5f); + } + + inline double_t truncf(double_t x) { + if (x >= 0.0f) + return floor(x); + else + return ceil(x); + } }