From f1af2f23f9fd255af0adb4e77cbf556da09692d0 Mon Sep 17 00:00:00 2001 From: copi143 Date: Sat, 14 Dec 2024 22:55:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dplmp=E7=9A=84=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/plmp/mpi32.h | 1 + test/CMakeLists.txt | 2 + test/fib.c | 90 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 test/fib.c diff --git a/include/plmp/mpi32.h b/include/plmp/mpi32.h index e93bd1f8..55396327 100755 --- a/include/plmp/mpi32.h +++ b/include/plmp/mpi32.h @@ -315,6 +315,7 @@ API bool mpi_mul2i(ARGS_2_MI) noexcept { // a *= b } } API bool mpi_mul3(ARGS_3) noexcept { // c = a * b + mpi_set0(c, len); u32 overflow = 0; for (size_t i = 0; i < len; i++) { for (size_t j = 0; j < len - i; j++) { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7cc3e866..07f4e9ea 100755 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -22,3 +22,5 @@ add_subdirectory(misc) testexec(plmp plmp.c) testexec(plmp-cpp plmp.cpp) testlib(plmp-cpp util-libc-base) + +testexec(fib fib.c) diff --git a/test/fib.c b/test/fib.c new file mode 100644 index 00000000..5cf9f3fc --- /dev/null +++ b/test/fib.c @@ -0,0 +1,90 @@ +#include +#include + +#define NO_STD 0 +#include + +const char _plos_lut_alnum_lower[62] = + "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; +const char _plos_lut_alnum_upper[62] = + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + +finline cstr tostring(u32 *_rest a, size_t len) { + static char buf[1024]; + return mpi_tostr(buf, 1024, a, len, 10, true); +} + +#define NBITS 1024 +#define LEN LEN_MPI(NBITS) + +void fib(u32 *result, i32 x) { + x -= 3; + if (x < 0) { + mpi_set1(result, LEN); + return; + } + u32 *a = malloc(NBITS / 8); + u32 *b = malloc(NBITS / 8); + u32 *c = malloc(NBITS / 8); + u32 *a2 = malloc(NBITS / 8); + u32 *b2 = malloc(NBITS / 8); + u32 *c2 = malloc(NBITS / 8); + u32 *i = malloc(NBITS / 8); + u32 *j = malloc(NBITS / 8); + u32 *t1 = malloc(NBITS / 8); + u32 *t2 = malloc(NBITS / 8); + u32 *t3 = malloc(NBITS / 8); + u32 *t4 = malloc(NBITS / 8); + + mpi_set0(a, LEN); + mpi_set1(b, LEN); + mpi_set1(c, LEN); + mpi_set1(i, LEN); + mpi_set1(j, LEN); + + while (x > 0) { + if (x & 1) { + mpi_mul3(t1, a, i, LEN); + mpi_mul3(t2, b, j, LEN); + mpi_mul3(t3, b, i, LEN); + mpi_mul3(t4, c, j, LEN); + mpi_add3(i, t1, t2, LEN); + mpi_add3(j, t3, t4, LEN); + } + mpi_mul3(a2, a, a, LEN); + mpi_mul3(b2, b, b, LEN); + mpi_mul3(c2, c, c, LEN); + mpi_add3(t1, a, c, LEN); + + mpi_add3(a, a2, b2, LEN); + + mpi_set(t2, b, LEN); + mpi_mul3(b, t1, t2, LEN); + + mpi_add3(c, b2, c2, LEN); + + x >>= 1; + } + + mpi_add3(result, i, j, LEN); + + free(a); + free(b); + free(c); + free(a2); + free(b2); + free(c2); + free(i); + free(j); + free(t1); + free(t2); + free(t3); + free(t4); +} + +int main() { + u32 *result = malloc(NBITS / 8); + fib(result, 400); + printf("%s\n", tostring(result, LEN)); + free(result); +}