fork download
  1. #include <stdio.h>
  2.  
  3. typedef struct {
  4. int flag; // 0:負、1:正
  5. unsigned int upper;
  6. unsigned int lower;
  7. } w_int;
  8.  
  9. void DivFunc(w_int* pNumA, w_int* pNumB)
  10. {
  11. w_int q, r;
  12. int i;
  13.  
  14. if (pNumB->upper == 0 && pNumB->lower == 0) return;
  15. q.upper = q.lower = 0;
  16. r.upper = r.lower = 0;
  17. for (i = 63; 0 <= i; i--) {
  18. r.upper <<= 1;
  19. r.upper |= r.lower >> 31;
  20. r.lower <<= 1;
  21. if (32 <= i) {
  22. r.lower |= (pNumA->upper >> (i - 32)) & 1;
  23. } else {
  24. r.lower |= (pNumA->lower >> i) & 1;
  25. }
  26. if (r.upper < pNumB->upper) continue;
  27. if (r.upper == pNumB->upper && r.lower < pNumB->lower) continue;
  28. r.upper -= pNumB->upper;
  29. if (r.lower < pNumB->lower) r.upper--;
  30. r.lower -= pNumB->lower;
  31. if (32 <= i) {
  32. q.upper |= 1 << (i - 32);
  33. } else {
  34. q.lower |= 1 << i;
  35. }
  36. }
  37. q.flag = !(!pNumA->flag ^ !pNumB->flag);
  38. r.flag = 1;
  39. *pNumA = q;
  40. *pNumB = r;
  41. }
  42.  
  43. int main()
  44. {
  45. w_int n, d;
  46. long long ln, ld, lq, lr;
  47.  
  48. n.flag = 1;
  49. n.upper = 123;
  50. n.lower = 456;
  51. d.flag = 1;
  52. d.upper = 7;
  53. d.lower = 89;
  54. printf("n=%d %#x %#x\n", n.flag, n.upper, n.lower);
  55. printf("d=%d %#x %#x\n", d.flag, d.upper, d.lower);
  56.  
  57. ln = ((n.flag << 1) - 1) * (((long long)n.upper << 32) + n.lower);
  58. ld = ((d.flag << 1) - 1) * (((long long)d.upper << 32) + d.lower);
  59. lq = ln / ld;
  60. lr = ln % ld;
  61. printf("ln=%#llx ld=%#llx lq=%#llx lr=%#llx\n", ln, ld, lq, lr);
  62.  
  63. DivFunc(&n, &d);
  64. printf("n=%d %#x %#x\n", n.flag, n.upper, n.lower);
  65. printf("d=%d %#x %#x\n", d.flag, d.upper, d.lower);
  66.  
  67. return 0;
  68. }
  69.  
  70. /*
  71. unsigned int n, d, q, r;
  72. int i;
  73.  
  74. n = 10;
  75. d = 3;
  76.  
  77. if (d == 0) return 1;
  78. q = 0;
  79. r = 0;
  80. for (i = 31; 0 <= i; i--) {
  81. r <<= 1;
  82. r |= (n >> i) & 1;
  83. if (r >= d) {
  84. r -= d;
  85. q |= 1 << i;
  86. }
  87. }
  88. */
  89.  
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
n=1 0x7b 0x1c8
d=1 0x7 0x59
ln=0x7b000001c8 ld=0x700000059 lq=0x11 lr=0x3fffffbdf
n=1 0 0x11
d=1 0x3 0xfffffbdf