Thread: Bugs in quickly made code

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733

    Bugs in quickly made code

    Would've stuck this in a previous thread but I think my previous post there will be ignored for sure if I do that so separate thread it is, made some bitwise math for my bignum code but results came out partially incorrect (I'm just gonna assuming the rest was just a coincidence)

    I have to get ready for work so figured I'd post the code here with the output and see if I strike lucky by the time I get a chance to return to this:
    Code:
    int _alu_upto( alu_t *alu, int num, int val, alu_bit_t *pos )
    {
    	int ret = _alu_check2( alu, num, val );
    	alu_reg_t *N, *V;
    	alu_bit_t n, v;
    	size_t ndiff, vdiff;
    	
    	if ( ret != 0 )
    	{
    		alu_error( ret );
    		return ret;
    	}
    	
    	if ( !pos )
    	{
    		ret = EDESTADDRREQ;
    		alu_error( ret );
    		return ret;
    	}
    	
    	N = alu->regv + num;
    	V = alu->regv + val;
    	
    	ndiff = (N->upto.b - N->init.b);
    	vdiff = (V->upto.b - V->init.b);
    	
    	*pos = alu_bit_set_bit(
    		N->part,
    		N->init.b + ((ndiff < vdiff) ? ndiff : vdiff)
    	);
    	
    	return 0;
    }
    
    int _alu_and( alu_t *alu, int num, int val )
    {
    	alu_bit_t n, v, e = {0};
    	int ret = _alu_upto( alu, num, val, &e );
    	alu_reg_t *N, *V;
    	
    	if ( ret != 0 )
    	{
    		alu_error( ret );
    		return ret;
    	}
    	
    	N = alu->regv + num;
    	V = alu->regv + val;
    	
    	for
    	(
    		n = N->init, v = V->init;
    		n.b < e.b;
    		n = alu_bit_inc( n ), v = alu_bit_inc( v )
    	)
    	{
    		if ( (*(n.S) & n.B) && !(*(v.S) & v.B) )
    			*(n.S) ^= n.B;
    	}
    	
    	while ( n.b < N->upto.b )
    	{
    		*(n.S) &= ~(n.B);
    		n = alu_bit_inc(n);
    	}
    	
    	return 0;
    }
    
    int _alu__or( alu_t *alu, int num, int val )
    {
    	alu_bit_t n, v, e = {0};
    	int ret = _alu_upto( alu, num, val, &e );
    	alu_reg_t *N, *V;
    	
    	if ( ret != 0 )
    	{
    		alu_error( ret );
    		return ret;
    	}
    	
    	N = alu->regv + num;
    	V = alu->regv + val;
    	
    	for
    	(
    		n = N->init, v = V->init;
    		n.b < e.b;
    		n = alu_bit_inc( n ), v = alu_bit_inc( v )
    	)
    	{
    		*(n.S) |= (*(v.S) & v.B) ? n.B : ALU_SEG_C(0);
    	}
    	
    	return 0;
    }
    
    int _alu_xor( alu_t *alu, int num, int val )
    {
    	alu_bit_t n, v, e = {0};
    	int ret = _alu_upto( alu, num, val, &e );
    	alu_reg_t *N, *V;
    	
    	if ( ret != 0 )
    	{
    		alu_error( ret );
    		return ret;
    	}
    	
    	N = alu->regv + num;
    	V = alu->regv + val;
    	
    	for
    	(
    		n = N->init, v = V->init;
    		n.b < e.b;
    		n = alu_bit_inc( n ), v = alu_bit_inc( v )
    	)
    	{
    		*(n.S) ^= (*(v.S) & v.B) ? n.B : ALU_SEG_C(0);
    	}
    	
    	return 0;
    }
    Code:
    ...
    ./alu.AppImage
    test.c:224: main() 'Initiating ALU to 0...'
    test.c:227: main() 'Pre-allocating 16 ALU registers...'
    test.c:230: main() 'Requesting register for number to modify...'
    test.c:239: main() 'Requesting register for value to modify by...'
    test.c:248: main() 'Comparing values...'
    test.c:56: compare() Expected 1, Got 1
    test.c:56: compare() Expected 0, Got 0
    test.c:56: compare() Expected -1, Got -1
    test.c:254: main() 'Shifting values...'
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000000002, _num = 00000000DEADC0DE, _val = 0000000000000002
    test.c:199: modify() 00000000DEADC0DE << 0000000000000002, Expected 000000037AB70378, Got 000000037AB70378, op = '<'
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000000002, _num = 00000000DEADC0DE, _val = 0000000000000002
    test.c:199: modify() 00000000DEADC0DE >> 0000000000000002, Expected 0000000037AB7037, Got 0000000037AB7037, op = '>'
    test.c:258: main() 'Rotating values...'
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000000002, _num = 00000000DEADC0DE, _val = 0000000000000002
    test.c:199: modify() 00000000DEADC0DE <<< 0000000000000002, Expected 000000037AB70378, Got 000000037AB70378, op = 'l'
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000000002, _num = 00000000DEADC0DE, _val = 0000000000000002
    test.c:199: modify() 00000000DEADC0DE >>> 0000000000000002, Expected 8000000037AB7037, Got 8000000037AB7037, op = 'r'
    test.c:262: main() 'Bitwising values...'
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000C0FFEE, _num = 00000000DEADC0DE, _val = 0000000000C0FFEE
    test.c:199: modify() 00000000DEADC0DE & 0000000000C0FFEE, Expected 000000000080C0CE, Got 00000000DF6EC0CC, op = '&'
    alu_main.c:86: alu_pri_reg() reg = 6, REG->part = 0x5616e5aa02d0
    alu_main.c:88: alu_pri_reg() REG->upto.b = 64, REG->last.b = 63, REG->init.b = 0
    alu_main.c:93: alu_pri_reg() REG->upto.s = 1, REG->last.s = 0, REG->init.s = 0
    alu_main.c:98: alu_pri_reg() REG->upto.S = 0x5616e5aa02d8, REG->last.S = 0x5616e5aa02d0, REG->init.S = 0x5616e5aa02d0
    0000000000000000000000000000000011011111011011101100000011001100
    alu_main.c:86: alu_pri_reg() reg = 6, REG->part = 0x5616e5aa02d0
    alu_main.c:88: alu_pri_reg() REG->upto.b = 64, REG->last.b = 63, REG->init.b = 0
    alu_main.c:93: alu_pri_reg() REG->upto.s = 1, REG->last.s = 0, REG->init.s = 0
    alu_main.c:98: alu_pri_reg() REG->upto.S = 0x5616e5aa02d8, REG->last.S = 0x5616e5aa02d0, REG->init.S = 0x5616e5aa02d0
    0000000000000000000000000000000000000000100000001100000011001110
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000C0FFEE, _num = 00000000DEADC0DE, _val = 0000000000C0FFEE
    test.c:199: modify() 00000000DEADC0DE | 0000000000C0FFEE, Expected 00000000DEEDFFFE, Got 00000000DDECC0F0, op = '|'
    alu_main.c:86: alu_pri_reg() reg = 6, REG->part = 0x5616e5aa02d0
    alu_main.c:88: alu_pri_reg() REG->upto.b = 64, REG->last.b = 63, REG->init.b = 0
    alu_main.c:93: alu_pri_reg() REG->upto.s = 1, REG->last.s = 0, REG->init.s = 0
    alu_main.c:98: alu_pri_reg() REG->upto.S = 0x5616e5aa02d8, REG->last.S = 0x5616e5aa02d0, REG->init.S = 0x5616e5aa02d0
    0000000000000000000000000000000011011101111011001100000011110000
    alu_main.c:86: alu_pri_reg() reg = 6, REG->part = 0x5616e5aa02d0
    alu_main.c:88: alu_pri_reg() REG->upto.b = 64, REG->last.b = 63, REG->init.b = 0
    alu_main.c:93: alu_pri_reg() REG->upto.s = 1, REG->last.s = 0, REG->init.s = 0
    alu_main.c:98: alu_pri_reg() REG->upto.S = 0x5616e5aa02d8, REG->last.S = 0x5616e5aa02d0, REG->init.S = 0x5616e5aa02d0
    0000000000000000000000000000000011011110111011011111111111111110
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000C0FFEE, _num = 00000000DEADC0DE, _val = 0000000000C0FFEE
    test.c:199: modify() 00000000DEADC0DE ^ 0000000000C0FFEE, Expected 00000000DE6D3F30, Got 00000000DDECC0F0, op = '^'
    alu_main.c:86: alu_pri_reg() reg = 6, REG->part = 0x5616e5aa02d0
    alu_main.c:88: alu_pri_reg() REG->upto.b = 64, REG->last.b = 63, REG->init.b = 0
    alu_main.c:93: alu_pri_reg() REG->upto.s = 1, REG->last.s = 0, REG->init.s = 0
    alu_main.c:98: alu_pri_reg() REG->upto.S = 0x5616e5aa02d8, REG->last.S = 0x5616e5aa02d0, REG->init.S = 0x5616e5aa02d0
    0000000000000000000000000000000011011101111011001100000011110000
    alu_main.c:86: alu_pri_reg() reg = 6, REG->part = 0x5616e5aa02d0
    alu_main.c:88: alu_pri_reg() REG->upto.b = 64, REG->last.b = 63, REG->init.b = 0
    alu_main.c:93: alu_pri_reg() REG->upto.s = 1, REG->last.s = 0, REG->init.s = 0
    alu_main.c:98: alu_pri_reg() REG->upto.S = 0x5616e5aa02d8, REG->last.S = 0x5616e5aa02d0, REG->init.S = 0x5616e5aa02d0
    0000000000000000000000000000000011011110011011010011111100110000
    test.c:268: main() 'Adding values...'
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000000002, _num = 00000000DEADC0DE, _val = 0000000000000002
    test.c:199: modify() 00000000DEADC0DE++, Expected 00000000DEADC0DF, Got 00000000DEADC0DF, op = 'i'
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000000002, _num = 00000000DEADC0DE, _val = 0000000000000002
    test.c:199: modify() 00000000DEADC0DE + 0000000000000002, Expected 00000000DEADC0E0, Got 00000000DEADC0E0, op = '+'
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000000000, _num = 00000000DEADC0DE, _val = 0000000000000000
    test.c:199: modify() 00000000DEADC0DE + 0000000000000000, Expected 00000000DEADC0DE, Got 00000000DEADC0DE, op = '+'
    test.c:273: main() 'Subtracting values...'
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000000002, _num = 00000000DEADC0DE, _val = 0000000000000002
    test.c:199: modify() 00000000DEADC0DE--, Expected 00000000DEADC0DD, Got 00000000DEADC0DD, op = 'd'
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000000002, _num = 00000000DEADC0DE, _val = 0000000000000002
    test.c:199: modify() 00000000DEADC0DE - 0000000000000002, Expected 00000000DEADC0DC, Got 00000000DEADC0DC, op = '-'
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000000000, _num = 00000000DEADC0DE, _val = 0000000000000000
    test.c:199: modify() 00000000DEADC0DE - 0000000000000000, Expected 00000000DEADC0DE, Got 00000000DEADC0DE, op = '-'
    test.c:278: main() 'Multiplying values...'
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000000002, _num = 00000000DEADC0DE, _val = 0000000000000002
    test.c:199: modify() 00000000DEADC0DE * 0000000000000002, Expected 00000001BD5B81BC, Got 00000001BD5B81BC, op = '*'
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000000000, _num = 00000000DEADC0DE, _val = 0000000000000000
    test.c:199: modify() 00000000DEADC0DE * 0000000000000000, Expected 0000000000000000, Got 0000000000000000, op = '*'
    test.c:282: main() 'Dividing values...'
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000000002, _num = 00000000DEADC0DE, _val = 0000000000000002
    test.c:199: modify() 00000000DEADC0DE / 0000000000000002, Expected 000000006F56E06F, Got 000000006F56E06F, op = '/'
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000000000, _num = 00000000DEADC0DE, _val = 0000000000000000
    test.c:199: modify() 00000000DEADC0DE / 0000000000000000, Expected 0000000000000000, Got 0000000000000000, op = '/'
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000000002, _num = 00000000DEADC0DE, _val = 0000000000000002
    test.c:199: modify() 00000000DEADC0DE % 0000000000000002, Expected 0000000000000000, Got 0000000000000000, op = '%'
    test.c:95: modify() num = 6, val = 7, N = 00000000DEADC0DE, V = 0000000000000000, _num = 00000000DEADC0DE, _val = 0000000000000000
    test.c:199: modify() 00000000DEADC0DE % 0000000000000000, Expected 00000000DEADC0DE, Got 00000000DEADC0DE, op = '%'
    Compilation finished successfully.
    If I don't strike lucky then that's too bad but no biggy

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    ...well this is embarrassing, I made a silly mistake when I duplicated code for the test, while I did change the operators that was all I changed, turned out I forgot to change which function is being called for each case, changing that solved the output being incorrect

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can you find any bugs in my code
    By jeremy duncan in forum C# Programming
    Replies: 5
    Last Post: 06-09-2017, 06:58 PM
  2. Code seems to hang if quickly ran consecutively
    By jjwooyoung in forum C Programming
    Replies: 4
    Last Post: 10-16-2016, 10:56 PM
  3. annoying bugs on working code
    By sammyfallows in forum C++ Programming
    Replies: 5
    Last Post: 11-20-2010, 09:05 PM
  4. what are the bugs in this code..please help me.
    By me001 in forum C Programming
    Replies: 12
    Last Post: 09-23-2008, 10:52 AM
  5. May anyone find some bugs from this code?
    By Mathsniper in forum C Programming
    Replies: 3
    Last Post: 12-30-2006, 12:16 PM

Tags for this Thread