Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!spool.mu.edu!uunet!karln!karln!karln From: karln!karln@uunet.uu.net (Karl Nicholas) Newsgroups: comp.lang.c Subject: Divide and C Summary: Why two divides needed? Message-ID: <1991Mar27.185804.7221@uunet.uu.net> Date: 27 Mar 91 18:58:04 GMT Article-I.D.: uunet.1991Mar27.185804.7221 Sender: karln@uunet.uu.net Organization: Sam76 - Pennington NJ Lines: 49 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@uunet.uu.net