From 1cdc843de084eaa681a0e75fd4e7484df08455b6 Mon Sep 17 00:00:00 2001 From: korenkonder Date: Mon, 25 Dec 2023 08:04:53 +0300 Subject: [PATCH] Add some funcs with fast approximation for double as well Actually all of them can be done with /fp:fast, but I don't bother enabling it --- src/KKdLib/prj/math.hpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) 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); + } }