c - divide and store quotient and reminder in different arrays -


the standard div() function returns div_t struct parameter, example:

/* div example */ #include <stdio.h>      /* printf */ #include <stdlib.h>     /* div, div_t */  int main () {   div_t divresult;   divresult = div (38,5);   printf ("38 div 5 => %d, remainder %d.\n", divresult.quot, divresult.rem);   return 0; } 

my case bit different; have this

#define num_elts 21433 int main () {   unsigned int quotients[num_elts];   unsigned int remainders[num_elts];   int i;    for(i=0;i<num_elts;i++) {       divide_single_instruction(&quotient[i],&reminder[i]);   } } 

i know assembly language division in single instruction, need same here save on cpu cycles, bassicaly move quotient eax , reminder edx memory locations arrays stored. how can done without including asm {} or sse intrinsics in c code ? has portable.

since you're writing arrays in-place (replacing numerator , denominator quotient , remainder) should store results temporary variables before writing arrays.

void foo (unsigned *num, unsigned *den, int n) {     int i;     for(i=0;i<n;i++) {         unsigned q = num[i]/den[i], r = num[i]%den[i];            num[i] = q, den[i] = r;     } } 

produces main loop assembly

.l5:         movl    (%rdi,%rcx,4), %eax         xorl    %edx, %edx         divl    (%rsi,%rcx,4)         movl    %eax, (%rdi,%rcx,4)         movl    %edx, (%rsi,%rcx,4)         addq    $1, %rcx         cmpl    %ecx, %r8d         jg      .l5 

there more complicated cases helps save quotient , remainder when first used. example in testing primes trial division see loop this

for (p = 3; p <= n/p; p += 2)     if (!(n % p)) return 0; 

it turns out gcc not use remainder first division , therefore division instruction twice unnecessary. fix can save remainder when first division done this:

for (p = 3, q=n/p, r=n%p; p <= q; p += 2, q = n/p, r=n%p)     if (!r) return 0; 

this speeds result factor of two.

so in general gcc job particularly if save quotient , remainder when first calculated.


Comments