First some context, I've been making a bignum type project, encountered an error with my code's results, don't have time to fix it myself as I have to go to work, if no-one posts a fix by the time I return to this then I'll just fix it myself. As you may have guessed by now the challenge is to fix just one function, this one:
Code:
int _alu_add( alu_t *alu, int num, int val )
{
bool carry = 0;
int ret = _alu_check2( alu, num, val );
alu_reg_t *N, *V;
alu_bit_t n, v = {0}, e;
if ( ret != 0 )
return ret;
N = alu->regv + num;
V = alu->regv + val;
n = N->init;
v = V->init;
e = N->upto;
for ( ; n.b < e.b; alu_bit_inc( &n ), alu_bit_inc( &v ) )
{
if ( carry )
{
if ( !(*(n.S) & n.B) )
{
*(n.S) |= n.B;
carry = false;
}
}
if ( *(v.S) & v.B )
{
if ( *(n.S) & n.B )
{
carry = true;
*(n.S) ^= n.B;
}
else
*(n.S) |= n.B;
}
}
return carry ? EOVERFLOW : 0;
}
I've attached the full project in case you feel like taking on the far harder challenge of finishing it, I'll still finish it myself but doing so can give you the experience of jumping into other developer's code and complete unfinished code which could prove useful in future jobs.
You can also use the project to compile and see why I know the function is faulty