Skip to content

Commit

Permalink
check all values of argminForMulVec in [1, 100000000]
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Aug 6, 2024
1 parent c80860c commit caffc31
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
10 changes: 7 additions & 3 deletions include/mcl/ec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,9 +986,13 @@ inline size_t argminForMulVec0(size_t n)
inline size_t argminForMulVec(size_t n)
{
size_t x = argminForMulVec0(n);
size_t v = costMulVec(n, x);
if (x > 1 && costMulVec(n, x-1) <= v) return x-1;
if (costMulVec(n,x+1) < v) return x+1;
#if 1
size_t vm1 = x > 1 ? costMulVec(n, x-1) : n;
size_t v0 = costMulVec(n, x);
size_t vp1 = costMulVec(n, x+1);
if (vm1 <= v0) return x-1;
if (vp1 < v0) return x+1;
#endif
return x;
}

Expand Down
30 changes: 10 additions & 20 deletions misc/mulvec_test.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
#include <mcl/ec.hpp>
#include <math.h>

int f(int n, int x)
{
// return int((n + pow(2, x+1) - 1)/x);
return int((n + (1ull<<(x+1))-2)/x);
}

void put(int n, int x)
{
printf(" x=%d(%d)", x, f(n, x));
printf(" x=%d(%zd)", x, mcl::ec::costMulVec(n, x));
}

int getmin(int n)
{
int min = 100000000;
int a = 0;
for (int x = 1; x < 30; x++) {
int v = f(n, x);
int v = mcl::ec::costMulVec(n, x);
if (v < min) {
a = x;
min = v;
Expand All @@ -26,25 +20,14 @@ int getmin(int n)
return a;
}

int getmin2(int n)
{
int x = mcl::ec::argminForMulVec(n);
int v = f(n, x);
if (x > 1 && f(n, x-1) <= v) return x-1;
if (f(n,x+1) < v) return x+1;
return x;
}

void disp(int n)
{
int x0 = getmin(n);
int x1 = mcl::ec::argminForMulVec(n);
int x2 = getmin2(n);
printf("n=%d", n);
put(n, x0);
put(n, x1);
put(n, x2);
printf(" diff=%d x0==x2=%d\n", x1-x0, x0==x2);
printf(" diff=%d\n", x1-x0);
}

int main()
Expand All @@ -58,4 +41,11 @@ int main()
disp(n);
disp(n*1.1);
}
puts("all search");
for (int i = 1; i < 100000000; i++) {
int x0 = getmin(i);
int x1 = mcl::ec::argminForMulVec(i);
// if (std::abs(x0-x1) > 1) printf("i=%d x0=%d x1=%d\n", i, x0, x1);
if (x0 != x1) printf("i=%d x0=%d x1=%d\n", i, x0, x1);
}
}

0 comments on commit caffc31

Please sign in to comment.