Issue with some bit division math

0 votes
asked Jan 12, 2018 by Lee Shallis (340 points)
First my results:

[code]

vuc1 =      111010110111100110100010101u                                                             

vuc2 =                              111u                                                             

(vul1 / vul2) =        1000011010001110101001100u                                                    

(vuv1 / vuv2) =        1000011010001110101001100u                                                    

vuc1 =        1000011010001110101001100u                                                             

vuc3 =                             1101u                                                             

(vul1 / vul3) =         100100001110100001100100u                                                    

(vuv1 / vuv3) =    10100010000100010001101110111u                                                    

vuc2 =                              111u                                                             

vuc3 =                             1101u                                                             

(vul2 / vul3) =                                 u                                                    

(vuv2 / vuv3) =                                 u

[/code]

The vul* are unsigned ints, the vuv* use pointers to copies of those values for which to test the buffered division on, the resultant vuv* values are supposed to match the resultant vul* values, as you can see the 2nd & 3rd set of results fail on that front and I would like help on identifying where the fault is in this code:

[code]

zuv_t* __zuvDiv( zuv_t *num, zuv_t const *val, zuv_t *rem ) {
    if ( !_zuvValidNum(num) || !_zuvValidNum(val) || !_zuvValidNum(rem)
        || num->size != rem->size )
        return NULL;
    memcpy( rem->buff, num->buff, num->size );
    memset( num->buff, 0, num->size );
    zuv_t tmp= {0}, inc = {0};
    tmp.bits = rem->bits % CHAR_BIT;
    if ( !tmp.bits ) tmp.bits = CHAR_BIT;
    inc.endian = tmp.endian = ziv_endian_78563412;
    for ( ; rem->size > 0; tmp.bits += CHAR_BIT ) {
        --rem->size;
        ++tmp.size;
        ++inc.size;
        inc.bits = tmp.bits;
        // Like decimal division start from left side
        tmp.buff = &(rem->buff[rem->size]);
        inc.buff = &(num->buff[rem->size]);
        while ( zuvMeq( &tmp, val ) ) {
            _zuvSub( &tmp, val );
            _zuvInc( &inc );
        }
    }
    return NULL;
}

// What I called directly
zuv_t* _zuvDiv( zuv_t *num, zuv_t const *val ) {
    zuv_t rem = *num, *result;
    rem.buff = calloc( 1, num->size );
    if ( !rem.buff ) return NULL;
    result = __zuvDiv( num, val, &rem );
    free( rem.buff );
    return result;
}

[/code]

Don't worry about that endian parameter because I will add something later to convert to and from small endian (78563412) and the current GDB online uses that same endian anyways so I just need to sort out the base math first.

1 Answer

0 votes
answered Jan 22, 2018 by Lee Shallis (340 points)
selected Jan 27, 2018 by Lee Shallis
 
Best answer
I managed to fix it, turned out when I had modified my main code to be more readable I messed up at the value passing stage, I had passed a pointer to the second structure of the first structure instead of a pointer of the second value.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...