## loop optimization

Usage of VEX tools

### loop optimization

hi,

I have a doubt on this piece of code from the function gfInvert of the pegwit algorithm that is executed the most of the time. There are two infinite loop that call each other. Through some printf we discovered that these loops call each other eight times and then the first one return.

How can I use #pragma rules in this case?

for (;;) {
if (f == 1) {
assert (f != 0);
gfSmallDiv (b, f);
/* destroy potentially sensitive data: */
gfClear (c); gfClear (f); gfClear (g); x = j = alpha = 0;
return 0;
}
if (f < g) {
goto SWAP_FG;
}
SWAP_GF:
j = f - g;
x = logt[f[f]] - logt[g[g]] + TOGGLE;
alpha = expt[x >= TOGGLE ? x - TOGGLE : x];
gfAddMul (f, alpha, j, g);
gfAddMul (b, alpha, j, c);
}

/* basically same code with b,c,f,g swapped */
for (;;) {
if (g == 1) {
assert (g != 0);
gfSmallDiv (c, g);
gfCopy (b, c);
/* destroy potentially sensitive data: */
gfClear (c); gfClear (f); gfClear (g); x = j = alpha = 0;
return 0;
}
if (g < f) {
goto SWAP_GF;
}
SWAP_FG:
j = g - f;
x = logt[g[g]] - logt[f[f]] + TOGGLE;
alpha = expt[x >= TOGGLE ? x - TOGGLE : x];
gfAddMul (g, alpha, j, f);
gfAddMul (c, alpha, j, b);
}
} /* gfInvert

Thanks,
Nicola
nik

Posts: 2
Joined: Mon Feb 20, 2006 7:04 pm

### Re: loop optimization

nik wrote:hi,

I have a doubt on this piece of code from the function gfInvert of the pegwit algorithm that is executed the most of the time. There are two infinite loop that call each other. Through some printf we discovered that these loops call each other eight times and then the first one return.

How can I use #pragma rules in this case?

for (;;) {
if (f == 1) {
assert (f != 0);
gfSmallDiv (b, f);
/* destroy potentially sensitive data: */
gfClear (c); gfClear (f); gfClear (g); x = j = alpha = 0;
return 0;
}
if (f < g) {
goto SWAP_FG;
}
SWAP_GF:
j = f - g;
x = logt[f[f]] - logt[g[g]] + TOGGLE;
alpha = expt[x >= TOGGLE ? x - TOGGLE : x];
gfAddMul (f, alpha, j, g);
gfAddMul (b, alpha, j, c);
}

/* basically same code with b,c,f,g swapped */
for (;;) {
if (g == 1) {
assert (g != 0);
gfSmallDiv (c, g);
gfCopy (b, c);
/* destroy potentially sensitive data: */
gfClear (c); gfClear (f); gfClear (g); x = j = alpha = 0;
return 0;
}
if (g < f) {
goto SWAP_GF;
}
SWAP_FG:
j = g - f;
x = logt[g[g]] - logt[f[f]] + TOGGLE;
alpha = expt[x >= TOGGLE ? x - TOGGLE : x];
gfAddMul (g, alpha, j, f);
gfAddMul (c, alpha, j, b);
}
} /* gfInvert

Thanks,
Nicola

What #pragma are you planning to use, and what for?

-- Paolo
frb

Posts: 62
Joined: Thu Nov 12, 2009 3:44 pm

### Re: loop optimization

frb wrote:
nik wrote:hi,

I have a doubt on this piece of code from the function gfInvert of the pegwit algorithm that is executed the most of the time. There are two infinite loop that call each other. Through some printf we discovered that these loops call each other eight times and then the first one return.

How can I use #pragma rules in this case?

for (;;) {
if (f == 1) {
assert (f != 0);
gfSmallDiv (b, f);
/* destroy potentially sensitive data: */
gfClear (c); gfClear (f); gfClear (g); x = j = alpha = 0;
return 0;
}
if (f < g) {
goto SWAP_FG;
}
SWAP_GF:
j = f - g;
x = logt[f[f]] - logt[g[g]] + TOGGLE;
alpha = expt[x >= TOGGLE ? x - TOGGLE : x];
gfAddMul (f, alpha, j, g);
gfAddMul (b, alpha, j, c);
}

/* basically same code with b,c,f,g swapped */
for (;;) {
if (g == 1) {
assert (g != 0);
gfSmallDiv (c, g);
gfCopy (b, c);
/* destroy potentially sensitive data: */
gfClear (c); gfClear (f); gfClear (g); x = j = alpha = 0;
return 0;
}
if (g < f) {
goto SWAP_GF;
}
SWAP_FG:
j = g - f;
x = logt[g[g]] - logt[f[f]] + TOGGLE;
alpha = expt[x >= TOGGLE ? x - TOGGLE : x];
gfAddMul (g, alpha, j, f);
gfAddMul (c, alpha, j, b);
}
} /* gfInvert

Thanks,
Nicola

What #pragma are you planning to use, and what for?

-- Paolo

We made some proof for seeing the behaviour of these loops. Each loop cycles for two times and then go in the other loop. All of them are executed for 16 times. We have used the #pragma for loop unrolling but, how we thought, there are no improvements in the IPC.

Thanks,
Nicola
Guest 