Skip to content

Returned int128 library #312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions library/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ add_subdirectory(uri)
add_subdirectory(yson)
add_subdirectory(yt)

if (YDB_SDK_TESTS)
add_subdirectory(int128)
endif()

if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64")
add_subdirectory(cpuid_check)
endif()
11 changes: 11 additions & 0 deletions library/cpp/int128/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
_ydb_sdk_add_library(int128)

target_sources(int128
PRIVATE
int128.cpp
)

target_link_libraries(int128
PUBLIC
yutil
)
6 changes: 6 additions & 0 deletions library/cpp/int128/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
https://st.yandex-team.ru/IGNIETFERRO-697

(Объединение разрозненных по Аркадии библиотек для поддержки 128-битного целого)

Идея классов ui128 / i128 в том, чтобы они работали так, как будто это компиляторные интовые типы.
Т.е. у этих классов не должно быть публичных функций типа ui128::GetHigh(), конструкторов из нескольких параметров и так далее.
62 changes: 62 additions & 0 deletions library/cpp/int128/int128.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "int128.h"

#include <tuple>

IOutputStream& operator<<(IOutputStream& out, const ui128& other) {
// see http://stackoverflow.com/questions/4361441/c-print-a-biginteger-in-base-10
// and http://stackoverflow.com/questions/8023414/how-to-convert-a-128-bit-integer-to-a-decimal-ascii-string-in-c
int d[39] = {0};
int i;
int j;
for (i = 63; i > -1; i--) {
if ((other.High_ >> i) & 1)
++d[0];
for (j = 0; j < 39; j++)
d[j] *= 2;
for (j = 0; j < 38; j++) {
d[j + 1] += d[j] / 10;
d[j] %= 10;
}
}
for (i = 63; i > -1; i--) {
if ((other.Low_ >> i) & 1)
++d[0];
if (i > 0)
for (j = 0; j < 39; j++)
d[j] *= 2;
for (j = 0; j < 38; j++) {
d[j + 1] += d[j] / 10;
d[j] %= 10;
}
}
for (i = 38; i > 0; i--)
if (d[i] > 0)
break;
for (; i > -1; i--)
out << static_cast<char>('0' + d[i]);

return out;
}

void TSerializer<ui128>::Save(IOutputStream* out, const ui128& Number) {
::Save(out, GetHigh(Number));
::Save(out, GetLow(Number));
}

void TSerializer<ui128>::Load(IInputStream* in, ui128& Number) {
ui64 High;
ui64 Low;
::Load(in, High);
::Load(in, Low);
Number = ui128(High, Low);
}

IOutputStream& operator<<(IOutputStream& out, const i128& other)
{
if (other >= 0) {
out << ui128{other};
} else {
out << '-' << ui128{-other};
}
return out;
}
Loading
Loading