From db53bfdcff566b504b4104c5e717d96875a83a4b Mon Sep 17 00:00:00 2001 From: ZhuangYumin Date: Tue, 31 Oct 2023 18:00:33 +0800 Subject: [PATCH] upd: trying to optmize --- data/CMakeLists.txt | 2 +- include/int2048.h | 3 ++- src/CMakeLists.txt | 2 +- src/int2048.cpp | 8 ++++++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/data/CMakeLists.txt b/data/CMakeLists.txt index 2410c31..78cb5b2 100644 --- a/data/CMakeLists.txt +++ b/data/CMakeLists.txt @@ -1,6 +1,6 @@ set(PROJECT_NAME ${CMAKE_PROJECT_NAME}) set(CMAKE_CXX_STANDARD 17) -set(CMAKE_CXX_FLAGS "-O2 -pg") +set(CMAKE_CXX_FLAGS "-g") set(ENV{MAKEFLAGS} "-j16") include_directories(${PROJECT_SOURCE_DIR}/include) link_directories(${PROJECT_SOURCE_DIR}/src) diff --git a/include/int2048.h b/include/int2048.h index ce94023..92e0703 100644 --- a/include/int2048.h +++ b/include/int2048.h @@ -52,6 +52,7 @@ root= 6 void RightMoveBy(int); void ProcessHalfBlock(); void RestoreHalfBlock(); + public: int2048(); int2048(long long); @@ -86,7 +87,7 @@ root= 6 int2048 &operator-=(const int2048 &); friend int2048 operator-(int2048, const int2048 &); - inline friend void UnsignedMultiply(int2048 &, const int2048 *, bool); + inline friend void UnsignedMultiply(int2048 &, const int2048 *, bool, int); int2048 &Multiply(const int2048 &); friend int2048 Multiply(int2048, const int2048 &); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ae73419..ef0718e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,5 @@ set(PROJECT_NAME ${CMAKE_PROJECT_NAME}) set(CMAKE_CXX_STANDARD 17) -set(CMAKE_CXX_FLAGS "-O2 -pg") +set(CMAKE_CXX_FLAGS "-g") include_directories(${PROJECT_SOURCE_DIR}/include) add_library(int2048 STATIC int2048.cpp) \ No newline at end of file diff --git a/src/int2048.cpp b/src/int2048.cpp index ea80abd..bfb93de 100644 --- a/src/int2048.cpp +++ b/src/int2048.cpp @@ -501,10 +501,14 @@ void int2048::NTTTransform(__int128_t *a, int NTT_blocks, } } inline void UnsignedMultiply(int2048 &A, const int2048 *pB, - bool inverse = false) { + bool inverse = false, int lenght_limit = 0) { if (&A == pB) throw "UnsignedMultiply: A and B are the same object"; int blocks_of_A = ((A.num_length + int2048::kNum - 1) / int2048::kNum); int blocks_of_B = ((pB->num_length + int2048::kNum - 1) / int2048::kNum); + if (inverse) { + lenght_limit = std::min(lenght_limit, pB->num_length); + blocks_of_B = lenght_limit; + } int max_blocks = blocks_of_A + blocks_of_B; int NTT_blocks = 2; while (NTT_blocks < (max_blocks << 1)) NTT_blocks <<= 1; @@ -704,7 +708,7 @@ inline void UnsignedDivide(int2048 &A, const int2048 *pB) { } assert(tmp_x.num_length % int2048::kNum == 0); assert(inverse_B.num_length % int2048::kNum == 0); - UnsignedMultiply(tmp_x, &inverse_B, true); + UnsignedMultiply(tmp_x, &inverse_B, true, inverse_B.num_length); for (int i = 0; i < tmp_x_error + inverseB_error; i++) tmp_x.RestoreHalfBlock(); UnsignedMinus(inverse_two, &tmp_x, true);