```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 ``` ```/* Problem statement: Write a function to perform integer division */ /* These numbers are signed integer and are ALWAYS positive */ /* Try to minimize the branch and number of operations */ #include #include #include #include #include /** * Calculate quotient and remainder * @param[in] nn Numerator * @param[in] dd Denominator * @param[out] pq Quotient if passed, zero otherwise * @param[out] pr Remainder if passed, zero otherwise * @ret 0 if passed, -1 if error */ void my_int_div(int nn, int dd, int *pq, int *pr) { assert(pq); assert(pr); *pq = *pr = 0; if(dd) { unsigned int n = abs(nn), d = abs(dd); const unsigned int sign = (nn < 0) == (dd < 0) ? 1 : -1; unsigned int pow2[sizeof n * CHAR_BIT] = {d}; int idx = 0; /* Prepare table containing multiple of nth power of 2 */ while(pow2[idx] < n) { pow2[idx+1] = pow2[idx] << 1; ++idx; } /* Keep subtracting for successive approximation */ do { /* Scope of betterment here */ if( pow2[idx] <= n ) { n -= pow2[idx]; *pq |= (1 << idx); } } while(idx--); *pr = n; *pq *= sign; } else { /* Handling for divide by zero error case */ return -1; } return 0; } int main() { const int TEST_COUNT = 5000; int i; srand(0); for(i = 1; i <= TEST_COUNT; ++i) { const int n = rand(); /* Should I use unsigned??? */ const int d = rand() % (RAND_MAX / (1 + rand() % 4)); const int exp_q = (d == 0) ? 0 : (n / d); const int exp_r = (d == 0) ? 0 : (n % d); int act_q, act_r; my_int_div(n, d, &act_q, &act_r); if(exp_q != act_q || exp_r != act_r) { printf("exp_q = %d, act_q = %d, exp_r = %d, act_r = %d (for %d / %d)\n", exp_q, act_q, exp_r, act_r, n, d); return -1; } else { /* printf("%-4d: passed for %d / %d\n", i, n, d); */ } } printf("All test cases passed\n"); return 0; } ```
 ```1 ``` ```All test cases passed ```