Thread: Struggling to get binary multiplication working

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

    Struggling to get binary multiplication working

    Here's the segment of code (all safety checks have been done before this)
    Code:
    	for ( v = val.init; v.b < val.width; v = bit_op_inc(v) ) {
    		if ( val.value[v.byte] & v.bit )
    			(void)bnum_op_add( num, cpy );
    #if 0
    		num.init = bit_op_inc( num.init );
    		num.init.b = 0;
    		num.width--;
    	}
    	num.init = cpy.init;
    	num.width = cpy.width;
    #else
    		bnum_op_shl_z( cpy, 1 );
    	}
    #endif
    Both variants return the same result (just with an obvious cycle difference:
    bnum_op_shl_z() method produces
    Code:
    ...
    operation2(-861021530,'<',5) call 4
    rstarted 603, rstopped 603, nstarted 603, nstopped 607
    num << val -1782885184, -1782885184
    ...
    num + val 679910964, 679910964
    operation2(-1125898167,'*',1369133069) call 9
    rstarted 630, rstopped 630, nstarted 631, nstopped 680
    num * val 1633672117, 2147483647
    r(int)		= 0b01100001010111111101101110110101
    t(bnum_t)	= 0b01111111111111111111111111111111
    num(int)	= 0b10111100111001000010100001001001
    val(int)	= 0b01010001100110110101000000001101
    Numerical argument out of domain
    ../makefile:16: recipe for target 'run' failed
    make: *** [run] Error 1
    Compilation failed.
    Other method produces:
    Code:
    operation2(-861021530,'<',5) call 4
    rstarted 936, rstopped 937, nstarted 937, nstopped 945
    num << val -1782885184, -1782885184
    ...
    operation2(-635723058,'+',1315634022) call 8
    rstarted 987, rstopped 988, nstarted 988, nstopped 992
    num + val 679910964, 679910964
    operation2(-1125898167,'*',1369133069) call 9
    rstarted 995, rstopped 996, nstarted 997, nstopped 1024
    num * val 1633672117, 2147483647
    r(int)		= 0b01100001010111111101101110110101
    t(bnum_t)	= 0b01111111111111111111111111111111
    num(int)	= 0b10111100111001000010100001001001
    val(int)	= 0b01010001100110110101000000001101
    Numerical argument out of domain
    ../makefile:16: recipe for target 'run' failed
    make: *** [run] Error 1
    Compilation failed.
    Incedently operation2() is called like so:
    Code:
    	for ( i = 0; operations2[i]; ++i ) {
    		ret = operation2( -(rand()), operations2[i], rand() );
    		if ( ret != 0 ) return printerr(ret);
    	}

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    886
    I managed to fix it somehow, the results got screwed up when I tried to call bnum_op_add() apparently,
    I copied & pasted the code from that function into bnum_op_mul(), changed the val related variables to cpy equivalants
    and everything started going hunky doory ('''一_一)

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,977
    Copy paste gets you every time
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    886
    Yeah but this time instead of handling variables wrong or different than intended it actually did the same thing but skipped a few cycles worth of checking (plus the overhead of calling the function), the problem only persists when I call the bnum_op_add() function which does the same thing but checks the parameters before hand and clamps at MIN/MAX the number can hold afterwards

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-02-2018, 03:08 AM
  2. Multiplication table not working, why ?
    By Szoke Lori in forum C Programming
    Replies: 1
    Last Post: 12-29-2015, 02:25 PM
  3. multiplication function in Binary Field, please help
    By lovesunset21 in forum C Programming
    Replies: 18
    Last Post: 12-08-2010, 08:03 PM
  4. How can a binary get its working dir at run-time?
    By meili100 in forum C++ Programming
    Replies: 16
    Last Post: 04-09-2008, 05:48 PM
  5. working with binary files
    By spank in forum C Programming
    Replies: 21
    Last Post: 01-04-2006, 08:55 AM

Tags for this Thread