Thread: Drawing a blank, posting in case someone else has an idea

  1. #16
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,200
    Appears the video was not as informative as it needed to be, ended up removing something that I thought was needed because of the video and got more correct results, still fails at a point and need some help understanding where I'm going wrong. GitHub - awsdert/alu at 95aa2046bb89246a0482ee4601265abb89d7ede3

    Code:
    make check.run
    ...
    Running suite(s): ALU
    ../src/alup.c:1986: alup__mul() 'final.bit < _DEXP.from'
    ../src/alup.c:1986: alup__mul() 'final.bit < _DEXP.from'
    ../src/alup.c:1986: alup__mul() 'final.bit < _DEXP.from'
    ../src/alup.c:1986: alup__mul() 'final.bit < _DEXP.from'
    ../src/alup.c:1986: alup__mul() 'final.bit < _DEXP.from'
    ../src/alup.c:1986: alup__mul() 'final.bit < _DEXP.from'
    ../src/alup.c:1986: alup__mul() 'final.bit < _DEXP.from'
    ../src/alup.c:1986: alup__mul() 'final.bit < _DEXP.from'
    ../src/alup.c:1986: alup__mul() 'final.bit < _DEXP.from'
    ../src/alup.c:1986: alup__mul() 'final.bit < _DEXP.from'
    ../src/alup.c:1986: alup__mul() 'final.bit < _DEXP.from'
    ../src/alup.c:1986: alup__mul() 'final.bit < _DEXP.from'
    ../src/alup.c:1986: alup__mul() 'final.bit < _DEXP.from'
    ../src/alup.c:1986: alup__mul() 'final.bit < _DEXP.from'
    ../src/alup.c:1986: alup__mul() 'final.bit < _DEXP.from'
    ../tests/check_alu.c:1234: test_alup__op4_floating_incremental_fn() 15.000000 * 143.000000 = 2145.000000, got 1072.500000
    ../tests/check_alu.c:1244: test_alup__op4_floating_incremental_fn() &_VALUE1 = 0 +0003 10000010 111'0000'0000'0000'0000'0000
    ../tests/check_alu.c:1245: test_alup__op4_floating_incremental_fn() &_VALUE2 = 0 +0007 10000110 000'1111'0000'0000'0000'0000
    ../tests/check_alu.c:1247: test_alup__op4_floating_incremental_fn() &_EXPECT = 0 +0011 10001010 000'0110'0001'0000'0000'0000
    ../tests/check_alu.c:1248: test_alup__op4_floating_incremental_fn() &_RESULT = 0 +0010 10001001 000'0110'0001'0000'0000'0000
    99%: Checks: 4568, Failures: 1, Errors: 0
    ...
    Compilation finished successfully.
    Code:
    int_t alup__mul( alup_t const * const _NUM, alup_t const * const _VAL, void *_cpy, void *_tmp )
    {
    	if ( alup_floating( _NUM ) || alup_floating( _VAL ) )
    	{
    		alup_t _DST, _SRC;
    		ssize_t exp, dexp, sexp, dbias, sbias, bits = _NUM->bits;
    		bool_t dneg, sneg;
    		
    		/* Ensure dealing with just floating numbers */
    		alup_init_floating( _DST, _tmp, bits * 2 );
    		alup_mov( &_DST, _NUM );
    		
    		alup_init_floating( _SRC, _NUM->data, bits );
    		_SRC.upto = _NUM->upto;
    		_SRC.last = _NUM->last;
    		_SRC.from = _NUM->from;
    		alup_mov( &_SRC, _VAL );
    		
    		dneg = alup_below0( &_DST );
    		sneg = alup_below0( &_SRC );
    		
    		dexp = alup_get_exponent( &_DST );
    		sexp = alup_get_exponent( &_SRC );
    		
    		if ( !dexp || !sexp )
    		{
    			alup_set( _NUM, 0 );
    			alup_set_sign( &_DST, dneg != sneg );
    			return 0;
    		}
    		
    		dbias = alup_get_exponent_bias( &_DST );
    		sbias = alup_get_exponent_bias( &_SRC );
    		
    		exp = (dexp - dbias) + (sexp - sbias);
    		
    		if ( exp >= dbias )
    		{	
    			alu_puts("MUL 1");
    			
    			(void)alup_set_fltinf( _NUM, dneg != sneg );
    			return ERANGE;
    		}
    		else
    		{
    			size_t mov;
    			ssize_t dmov, smov;
    			alup_t _DEXP, _SEXP, _DMAN, _SMAN, __DST, __SRC, _DINFER, _SINFER;
    			alub_t final, d1st, s1st, dinfer, sinfer;
    			
    			alup_init_exponent( &_DST, _DEXP );
    			alup_init_exponent( &_SRC, _SEXP );
    			
    			alup_set( &_DEXP, !!dexp );
    			alup_set( &_SEXP, !!sexp );
    			
    			alup_init_mantissa( &_DST, _DMAN );
    			alup_init_mantissa( &_SRC, _SMAN );
    			
    			__DST = _DST;
    			__DST.mdig = 0;
    			__DST.sign = false;
    			
    			__SRC = _SRC;
    			__SRC.mdig = 0;
    			__SRC.sign = false;
    			
    			/* Set position data of assumed bits */
    			_DINFER = __DST;
    			_SINFER = __SRC;
    			
    			_DINFER.from = _DEXP.from;
    			_SINFER.from = _SEXP.from;
    			
    			_DINFER.bits = _DINFER.upto - _DINFER.from;
    			_SINFER.bits = _SINFER.upto - _SINFER.from;
    			
    			dinfer = alup_bit( &_DINFER, _DINFER.from );
    			sinfer = alup_bit( &_SINFER, _SINFER.from );
    			
    			/* Insert assumed bits */
    			alup_set( &_DINFER, 0 );
    			alup_set( &_SINFER, 0 );
    			alub_set_val( dinfer, !!dexp );
    			alub_set_val( sinfer, !!sexp );
    			
    			/* Determine how far to move mantissa to right */
    			d1st = alup_first_bit_with_val( &_DMAN, 1 );
    			s1st = alup_first_bit_with_val( &_SMAN, 1 );
    			
    			dmov = d1st.bit - __DST.from;
    			smov = s1st.bit - __SRC.from;
    	
    			/* Remove useless 0s */
    			(void)alup__shr_int2int( &__DST, dmov );
    			(void)alup__shr_int2int( &__SRC, smov );
    			
    			(void)alup__mul_int2int( &__DST, &__SRC, _cpy );
    			
    			/* Normalise */
    			final = alup_final_bit_with_val( &__DST, 1 );
    			
    			if ( final.bit > _DEXP.from )
    			{
    				alu_puts("final.bit > _DEXP.from");
    				mov = final.bit - _DEXP.from;
    				
    				//exp += mov;
    				alup__shr_int2int( &__DST, mov );
    			}
    			else
    			{
    				alu_puts("final.bit < _DEXP.from");
    				mov = _DEXP.from - final.bit;
    				
    				//exp -= mov;
    				alup__shl_int2int( &__DST, mov );
    			}
    			
    			/* Mantissa is in place so all we need to do is set the exponent
    			 * and sign */
    			alup_set_exponent( &_DST, exp + dbias );
    			alup_set_sign( &_DST, dneg != sneg );
    				
    			return alup_mov( _NUM, &_DST );
    		}
    	}
    	
    	return alup__mul_int2int( _NUM, _VAL, _cpy );
    }

  2. #17
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,200
    Managed to fix it, this was all I needed to add after all: GitHub - awsdert/alu at 5f4c54b0303fa98c63a776212cd65a9241371707
    Code:
    dmov = _DEXP.from - d1st.bit;
    smov = _SEXP.from - s1st.bit;
    mov = final.bit - __DST.from;
    exp += mov - (dmov + smov);

  3. #18
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,200
    Managed to fix the mantissa in division but still having problems with the exponent in that field, hoping someone here can see where I'm going wrong.
    GitHub - awsdert/alu at a6fdf986875298697915e81934bcb31d8c67b048

    Output:
    Code:
    make check.run
    ...
    Running suite(s): ALU
    ../src/alup.c:2185: alup__div() &__DST = 1000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000
    ../src/alup.c:2186: alup__div() &__SRC = 1100'0001
    ../src/alup.c:2190: alup__div() &__DST = 0000'0000'1010'1001'1100'1000'0100'1010'0100'0111'1010'0000'0111'1111'0101'0110
    ../src/alup.c:2199: alup__div() exp = -7, mov = 55, dmov = 1, smov = 7
    ../src/alup.c:2208: alup__div() exp = -7, mov = 55, dmov = 1, smov = 7
    ../tests/check_alu.c:1234: test_alup__op4_floating_incremental_fn() 1.000000 / 193.000000 = 0.005181, got 0.010363
    ../tests/check_alu.c:1244: test_alup__op4_floating_incremental_fn() &_VALUE1 = 0 +0000 01111111 000'0000'0000'0000'0000'0000
    ../tests/check_alu.c:1245: test_alup__op4_floating_incremental_fn() &_VALUE2 = 0 +0007 10000110 100'0001'0000'0000'0000'0000
    ../tests/check_alu.c:1247: test_alup__op4_floating_incremental_fn() &_EXPECT = 0 -0008 01110111 010'1001'1100'1000'0100'1010
    ../tests/check_alu.c:1248: test_alup__op4_floating_incremental_fn() &_RESULT = 0 -0007 01111000 010'1001'1100'1000'0100'1010
    99%: Checks: 4568, Failures: 1, Errors: 0
    ...
    Compilation finished successfully.
    Code:
    Code:
    int_t alup__div(
    	alup_t const * const _NUM
    	, alup_t const * const _VAL
    	, void *_rem
    	, void *_tmp
    )
    {
    	if ( alup_floating( _NUM ) || alup_floating( _VAL ) )
    	{
    		alup_t _DST, _SRC;
    		ssize_t exp, dexp, sexp, dbias, sbias, bits = _NUM->bits;
    		bool_t dneg, sneg;
    		
    		/* Ensure dealing with just floating numbers */
    		alup_init_floating( _DST, _tmp, bits * 2 );
    		alup_mov( &_DST, _NUM );
    		
    		alup_init_floating( _SRC, _NUM->data, bits );
    		_SRC.upto = _NUM->upto;
    		_SRC.last = _NUM->last;
    		_SRC.from = _NUM->from;
    		alup_mov( &_SRC, _VAL );
    		
    		dneg = alup_below0( &_DST );
    		sneg = alup_below0( &_SRC );
    		
    		dexp = alup_get_exponent( &_DST );
    		sexp = alup_get_exponent( &_SRC );
    		
    		if ( !dexp || !sexp )
    		{
    			alup_set( _NUM, 0 );
    			alup_set_sign( &_DST, dneg != sneg );
    			return 0;
    		}
    		
    		dbias = alup_get_exponent_bias( &_DST );
    		sbias = alup_get_exponent_bias( &_SRC );
    		
    		exp = (dexp - dbias) - (sexp - sbias);
    		
    		if ( exp >= dbias )
    		{	
    			alu_puts("MUL 1");
    			
    			(void)alup_set_fltinf( _NUM, dneg != sneg );
    			return ERANGE;
    		}
    		else
    		{
    			int_t ret = 0;
    			size_t mov;
    			ssize_t dmov, smov;
    			alup_t _DEXP, _SEXP, _DMAN, _SMAN, __DST, __SRC, _DINFER, _SINFER;
    			alub_t final, d1st, s1st, dinfer, sinfer;
    			bool_t truncated = false;
    			
    			alup_init_exponent( &_DST, _DEXP );
    			alup_init_exponent( &_SRC, _SEXP );
    			
    			alup_set( &_DEXP, !!dexp );
    			alup_set( &_SEXP, !!sexp );
    			
    			alup_init_mantissa( &_DST, _DMAN );
    			alup_init_mantissa( &_SRC, _SMAN );
    			
    			__DST = _DST;
    			__DST.mdig = 0;
    			__DST.sign = false;
    
    			__SRC = _SRC;
    			__SRC.mdig = 0;
    			__SRC.sign = false;
    
    			/* Set position data of assumed bits */
    			_DINFER = __DST;
    			_SINFER = __SRC;
    
    			_DINFER.from = _DEXP.from;
    			_SINFER.from = _SEXP.from;
    
    			_DINFER.bits = _DINFER.upto - _DINFER.from;
    			_SINFER.bits = _SINFER.upto - _SINFER.from;
    			
    			dinfer = alup_bit( &_DINFER, _DINFER.from );
    			sinfer = alup_bit( &_SINFER, _SINFER.from );
    			
    			/* Insert assumed bits */
    			alup_set( &_DINFER, 0 );
    			alup_set( &_SINFER, 0 );
    			alub_set_val( dinfer, !!dexp );
    			alub_set_val( sinfer, !!sexp );
    			
    			/* Determine how far to move mantissa to right */
    			d1st = alup_first_bit_with_val( &_DMAN, 1 );
    			s1st = alup_first_bit_with_val( &_SMAN, 1 );
    			
    			dmov = d1st.bit - __DST.from;
    			smov = s1st.bit - __SRC.from;
    	
    			/* Insert 0s for  */
    			(void)alup__shl_int2int( &__DST, _DEXP.bits );
    			
    			/* Remove useless 0s */
    			__SRC.from = s1st.bit;
    			__SRC.last = _SEXP.from;
    			__SRC.upto = _SEXP.from + 1;
    			__SRC.bits = __SRC.upto - __SRC.from;
    			
    			alup_print( &__DST, 0, 1 );
    			alup_print( &__SRC, 0, 1 );
    			
    			ret = alup__div_int2int( &__DST, &__SRC, _rem );
    			
    			alup_print( &__DST, 0, 1 );
    			truncated = (ret == ERANGE);
    			
    			/* Normalise */
    			final = alup_final_bit_with_val( &__DST, 1 );
    			dmov = _DEXP.from - d1st.bit;
    			smov = _SEXP.from - s1st.bit;
    			mov = final.bit - __DST.from;
    			
    			alu_printf
    			(
    				"exp = %zd, mov = %zu, dmov = %zd, smov = %zd"
    				, exp
    				, mov
    				, dmov
    				, smov
    			);
    			//exp += mov - (dmov + smov);
    			alu_printf
    			(
    				"exp = %zd, mov = %zu, dmov = %zd, smov = %zd"
    				, exp
    				, mov
    				, dmov
    				, smov
    			);
    			
    			if ( final.bit > _DEXP.from )
    			{
    				alup__shr_int2int( &__DST, final.bit - _DEXP.from );
    			}
    			else
    			{
    				alup__shl_int2int( &__DST, _DEXP.from - final.bit );
    			}
    			
    			/* Mantissa is in place so all we need to do is set the exponent
    			 * and sign */
    			alup_set_exponent( &_DST, exp + dbias );
    			alup_set_sign( &_DST, dneg != sneg );
    				
    			ret = alup_mov( _NUM, &_DST );
    			return EITHER( ret, ret, IFTRUE( truncated, ERANGE ) );
    		}
    	}
    	
    	return alup__div_int2int( _NUM, _VAL, _rem );
    }

  4. #19
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,200
    Figured out the fix:
    Code:
    exp -= ((__DST.upto - final.bit) - __SRC.bits);

  5. #20
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,200
    Attempted to fix a rounding error but the fix seems to be ignored, I got other stuff to do now so hoping someone will spot the cause by the time I come back, here's the code & output:
    GitHub - awsdert/alu at be5f04199d53f7f93cb858377027b1805ab9758f
    Code:
    make check.run
    ...
    Running suite(s): ALU
    ../src/alup.c:2184: alup__div() &_DST = 0 -1013 00000001010 '1001'1100'1000'0100'1010'0100'0111'1010'0000'0111'1111'0101'0110
    ../src/alup.c:2208: alup__div() &_DST = 0 -1013 00000001010 '1001'1100'1000'0100'1010'0100'0111'1010'0000'0111'1111'0101'1000
    ../src/alup.c:2184: alup__div() &_DST = 0 -1002 00000010101 '0001'1101'0000'0111'1110'1010'1110'0010'1111'1000'0001'0101'0001
    ../src/alup.c:2208: alup__div() &_DST = 0 -1002 00000010101 '0001'1101'0000'0111'1110'1010'1110'0010'1111'1000'0001'0101'0010
    ../src/alup.c:2184: alup__div() &_DST = 0 -1008 00000001111 '1100'0000'1111'1100'0000'1111'1100'0000'1111'1100'0000'1111'1100
    ../src/alup.c:2208: alup__div() &_DST = 0 -1008 00000001111 '1100'0000'1111'1100'0000'1111'1100'0000'1111'1100'0000'1111'1101
    ../tests/check_alu.c:1233: test_alup__op4_floating_incremental_fn() 3: 3.000000 / 195.000000 = 0.015385, got 0.015385
    ../tests/check_alu.c:1244: test_alup__op4_floating_incremental_fn() &_VALUE1 = 0 +0001 10000000 100'0000'0000'0000'0000'0000
    ../tests/check_alu.c:1245: test_alup__op4_floating_incremental_fn() &_VALUE2 = 0 +0007 10000110 100'0011'0000'0000'0000'0000
    ../tests/check_alu.c:1246: test_alup__op4_floating_incremental_fn() &_EXPECT = 0 -0007 01111000 111'1100'0000'1111'1100'0001
    ../tests/check_alu.c:1247: test_alup__op4_floating_incremental_fn() &_RESULT = 0 -0007 01111000 111'1100'0000'1111'1100'0000
    99%: Checks: 4568, Failures: 1, Errors: 0
    ...
    Compilation finished successfully.
    Code:
    int_t alup__div(
    	alup_t const * const _NUM
    	, alup_t const * const _VAL
    	, void *_rem
    	, void *_tmp
    )
    {
    	if ( alup_floating( _NUM ) || alup_floating( _VAL ) )
    	{
    		alup_t _DST, _SRC;
    		ssize_t exp, dexp, sexp, dbias, sbias, bits = _NUM->bits;
    		bool_t dneg, sneg;
    		
    		/* Ensure dealing with just floating numbers */
    		alup_init_floating( _DST, _tmp, bits * 2 );
    		alup_mov( &_DST, _NUM );
    		
    		alup_init_floating( _SRC, _NUM->data, bits );
    		_SRC.upto = _NUM->upto;
    		_SRC.last = _NUM->last;
    		_SRC.from = _NUM->from;
    		alup_mov( &_SRC, _VAL );
    		
    		dneg = alup_below0( &_DST );
    		sneg = alup_below0( &_SRC );
    		
    		dexp = alup_get_exponent( &_DST );
    		sexp = alup_get_exponent( &_SRC );
    		
    		if ( !dexp || !sexp )
    		{
    			alup_set( _NUM, 0 );
    			alup_set_sign( &_DST, dneg != sneg );
    			return 0;
    		}
    		
    		dbias = alup_get_exponent_bias( &_DST );
    		sbias = alup_get_exponent_bias( &_SRC );
    		
    		exp = (dexp - dbias) - (sexp - sbias);
    		
    		if ( exp >= dbias )
    		{	
    			alu_puts("MUL 1");
    			
    			(void)alup_set_fltinf( _NUM, dneg != sneg );
    			return ERANGE;
    		}
    		else
    		{
    			int_t ret = 0;
    			alup_t _DEXP, _SEXP, _DMAN, _SMAN, __DST, __SRC, _LOST;
    			alub_t final, first, s1st, lost;
    			bool_t truncated = false;
    			
    			alup_init_exponent( &_DST, _DEXP );
    			alup_init_exponent( &_SRC, _SEXP );
    			
    			alup_set( &_DEXP, !!dexp );
    			alup_set( &_SEXP, !!sexp );
    			
    			alup_init_mantissa( &_DST, _DMAN );
    			alup_init_mantissa( &_SRC, _SMAN );
    			
    			__DST = _DST;
    			__DST.mdig = 0;
    			__DST.sign = false;
    
    			__SRC = _SRC;
    			__SRC.mdig = 0;
    			__SRC.sign = false;
    			
    			/* ??? */
    			s1st = alup_first_bit_with_val( &_SMAN, 1 );
    			
    			/* Insert assumed bits */
    			alup_set_exponent( &_DST, !!dexp );
    			alup_set_exponent( &_SRC, !!sexp );
    
    			/* Insert 0s to avoid loosing bits in division */
    			(void)alup__shl_int2int( &__DST, _DEXP.bits );
    			
    			/* Remove useless 0s */
    			__SRC.from = s1st.bit;
    			__SRC.last = _SEXP.from;
    			__SRC.upto = _SEXP.from + 1;
    			__SRC.bits = __SRC.upto - __SRC.from;
    			
    			ret = alup__div_int2int( &__DST, &__SRC, _rem );
    			truncated = (ret == ERANGE);
    			
    			alup_print( &_DST, 0, 1 );
    			
    			/* Normalise */
    			final = alup_final_bit_with_val( &__DST, 1 );
    			first.bit = final.bit - _DST.from;
    			first.bit = EITHER( first.bit > _DST.mdig, _DST.mdig, first.bit );
    			first = alup_bit( &__DST, final.bit - first.bit );
    			
    			_LOST = __DST;
    			_LOST.upto = first.bit;
    			_LOST.last =
    				EITHER( _LOST.upto > _LOST.from, _LOST.upto - 1, _LOST.from );
    			_LOST.bits = _LOST.upto - _LOST.from;
    			lost = alup_final_bit_with_val( &_LOST, 1 );
    			
    			if ( lost.bit < first.bit )
    			{
    				_LOST.from = lost.bit;
    				_LOST.upto = __DST.upto;
    				_LOST.last = __DST.last;
    				_LOST.bits = _LOST.upto - _LOST.from;
    				alup_inc( &_LOST );
    				ret = ERANGE;
    				truncated = true;
    			}
    			
    			alup_print( &_DST, 0, 1 );
    			
    			exp -= ((__DST.upto - final.bit) - __SRC.bits);
    			
    			if ( final.bit > _DEXP.from )
    			{
    				alup__shr_int2int( &__DST, final.bit - _DEXP.from );
    			}
    			else
    			{
    				alup__shl_int2int( &__DST, _DEXP.from - final.bit );
    			}
    			
    			/* Mantissa is in place so all we need to do is set the exponent
    			 * and sign */
    			alup_set_exponent( &_DST, exp + dbias );
    			alup_set_sign( &_DST, dneg != sneg );
    				
    			ret = alup_mov( _NUM, &_DST );
    			return EITHER( ret, ret, IFTRUE( truncated, ERANGE ) );
    		}
    	}
    	
    	return alup__div_int2int( _NUM, _VAL, _rem );
    }

  6. #21
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,200
    Never mind, found the cause, I used the wrong value to compare against, used _DST.mdig instead of _NUM->mdig

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-09-2016, 03:58 PM
  2. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  3. Stupid Error That I'm Drawing a Blank On
    By DarkDot in forum C++ Programming
    Replies: 2
    Last Post: 05-07-2007, 06:34 PM
  4. upper case to lower case problem
    By Jasonymk in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 05:35 AM
  5. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:15 PM

Tags for this Thread