aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/misc/numeric_helpers-inl.h
diff options
context:
space:
mode:
authorbabenko <babenko@yandex-team.com>2024-11-06 13:24:38 +0300
committerbabenko <babenko@yandex-team.com>2024-11-06 13:54:01 +0300
commitb60a78031c047a8c8543bf6a3dc4f828d104dd3c (patch)
treee8ff58e6fe24f57d35a6cc9a91ca61522706d088 /library/cpp/yt/misc/numeric_helpers-inl.h
parent658682dbe663353ecdf6cb50a846c54b3dd24481 (diff)
downloadydb-b60a78031c047a8c8543bf6a3dc4f828d104dd3c.tar.gz
NaN-safe comparison and hashing
commit_hash:46d59ab3acbd313753d3e46f3a6f10a8ebc424d8
Diffstat (limited to 'library/cpp/yt/misc/numeric_helpers-inl.h')
-rw-r--r--library/cpp/yt/misc/numeric_helpers-inl.h59
1 files changed, 59 insertions, 0 deletions
diff --git a/library/cpp/yt/misc/numeric_helpers-inl.h b/library/cpp/yt/misc/numeric_helpers-inl.h
new file mode 100644
index 0000000000..c0dbc07490
--- /dev/null
+++ b/library/cpp/yt/misc/numeric_helpers-inl.h
@@ -0,0 +1,59 @@
+#ifndef NUMERIC_HELPERS_INL_H_
+#error "Direct inclusion of this file is not allowed, include numeric_helpers.h"
+// For the sake of sane code completion.
+#include "numeric_helpers.h"
+#endif
+
+#include <cstdlib>
+#include <cinttypes>
+#include <cmath>
+#include <algorithm>
+
+#include <util/system/compiler.h>
+
+namespace NYT {
+
+////////////////////////////////////////////////////////////////////////////////
+
+template <class T>
+T DivCeil(const T& numerator, const T& denominator)
+{
+ YT_VERIFY(denominator != 0);
+ auto res = std::div(numerator, denominator);
+ return res.quot + (res.rem > static_cast<T>(0) ? static_cast<T>(1) : static_cast<T>(0));
+}
+
+template <typename T>
+T DivRound(const T& numerator, const T& denominator)
+{
+ auto res = std::div(numerator, denominator);
+ return res.quot + (res.rem >= (denominator + 1) / 2 ? static_cast<T>(1) : static_cast<T>(0));
+}
+
+template <class T>
+T RoundUp(const T& numerator, const T& denominator)
+{
+ return DivCeil(numerator, denominator) * denominator;
+}
+
+template <class T>
+T RoundDown(const T& numerator, const T& denominator)
+{
+ return (numerator / denominator) * denominator;
+}
+
+template <class T>
+Y_FORCE_INLINE int GetSign(const T& value)
+{
+ if (value < 0) {
+ return -1;
+ } else if (value > 0) {
+ return +1;
+ } else {
+ return 0;
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace NYT