Divide and C

Karl Nicholas karln!karln at uunet.uu.net
Thu Mar 28 04:58:04 AEST 1991


    Greetings most knowledgable C experts.

    I have a problem.


    I get a number. I need to use this number to index into a
    2 dimensional array.

    EX: int array[10][10];
        val = 24;
        ++array[val/10][val%10];

    This is the only way I know how to do this.
    My problem with it is that is requires TWO divides.
    
    Divide 1: val/10. Here the result is used. The remainder
                      is thrown away.

    Divide 2: val%10. Here the remainder is used, the result is thrown away.

    I happen to know that divide instructions are one of the MOST time
    consuming instructions around. I measured it.

    Wanting my program to run almost twice as fast, I wrote an ASSEMBLY
    lang subroutine, to be called from C.

    void ASMDIVIDE( int dividend, int divisor, int *result, int* remain );

    so now I have in my code:

    int, result, remain;
    int array[10][10];
  
    ASMDIVIDE ( val, 10, &result, &remain );
    ++array[result][remain];

    And got most of my desired increase in performance.

    Since I do not consider ASSEMBLY to be portable, how do I do
    this in a C only and portable way?

    I will summarize any e-mailed responses.

    Thanks all.

    Karl Nicholas

    karln!karln at uunet.uu.net



More information about the Comp.lang.c mailing list