2
0

int128.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * 128-bit division and remainder for compilers not supporting __int128
  3. *
  4. * Copyright (c) 2021 Frédéric Pétrot <frederic.petrot@univ-grenoble-alpes.fr>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/host-utils.h"
  26. #include "qemu/int128.h"
  27. #ifndef CONFIG_INT128
  28. /*
  29. * Division and remainder algorithms for 128-bit due to Stefan Kanthak,
  30. * https://skanthak.homepage.t-online.de/integer.html#udivmodti4
  31. * Preconditions:
  32. * - function should never be called with v equals to 0, it has to
  33. * be dealt with beforehand
  34. * - quotien pointer must be valid
  35. */
  36. static Int128 divrem128(Int128 u, Int128 v, Int128 *q)
  37. {
  38. Int128 qq;
  39. uint64_t hi, lo, tmp;
  40. int s = clz64(v.hi);
  41. if (s == 64) {
  42. /* we have uu÷0v => let's use divu128 */
  43. hi = u.hi;
  44. lo = u.lo;
  45. tmp = divu128(&lo, &hi, v.lo);
  46. *q = int128_make128(lo, hi);
  47. return int128_make128(tmp, 0);
  48. } else {
  49. hi = int128_gethi(int128_lshift(v, s));
  50. if (hi > u.hi) {
  51. lo = u.lo;
  52. tmp = u.hi;
  53. divu128(&lo, &tmp, hi);
  54. lo = int128_gethi(int128_lshift(int128_make128(lo, 0), s));
  55. } else { /* prevent overflow */
  56. lo = u.lo;
  57. tmp = u.hi - hi;
  58. divu128(&lo, &tmp, hi);
  59. lo = int128_gethi(int128_lshift(int128_make128(lo, 1), s));
  60. }
  61. qq = int128_make64(lo);
  62. tmp = lo * v.hi;
  63. mulu64(&lo, &hi, lo, v.lo);
  64. hi += tmp;
  65. if (hi < tmp /* quotient * divisor >= 2**128 > dividend */
  66. || hi > u.hi /* quotient * divisor > dividend */
  67. || (hi == u.hi && lo > u.lo)) {
  68. qq.lo -= 1;
  69. mulu64(&lo, &hi, qq.lo, v.lo);
  70. hi += qq.lo * v.hi;
  71. }
  72. *q = qq;
  73. u.hi -= hi + (u.lo < lo);
  74. u.lo -= lo;
  75. return u;
  76. }
  77. }
  78. Int128 int128_divu(Int128 a, Int128 b)
  79. {
  80. Int128 q;
  81. divrem128(a, b, &q);
  82. return q;
  83. }
  84. Int128 int128_remu(Int128 a, Int128 b)
  85. {
  86. Int128 q;
  87. return divrem128(a, b, &q);
  88. }
  89. Int128 int128_divs(Int128 a, Int128 b)
  90. {
  91. Int128 q;
  92. bool sgna = !int128_nonneg(a);
  93. bool sgnb = !int128_nonneg(b);
  94. if (sgna) {
  95. a = int128_neg(a);
  96. }
  97. if (sgnb) {
  98. b = int128_neg(b);
  99. }
  100. divrem128(a, b, &q);
  101. if (sgna != sgnb) {
  102. q = int128_neg(q);
  103. }
  104. return q;
  105. }
  106. Int128 int128_rems(Int128 a, Int128 b)
  107. {
  108. Int128 q, r;
  109. bool sgna = !int128_nonneg(a);
  110. bool sgnb = !int128_nonneg(b);
  111. if (sgna) {
  112. a = int128_neg(a);
  113. }
  114. if (sgnb) {
  115. b = int128_neg(b);
  116. }
  117. r = divrem128(a, b, &q);
  118. if (sgna) {
  119. r = int128_neg(r);
  120. }
  121. return r;
  122. }
  123. #elif defined(CONFIG_TCG_INTERPRETER)
  124. Int128 int128_divu(Int128 a_s, Int128 b_s)
  125. {
  126. Int128Alias r, a, b;
  127. a.s = a_s;
  128. b.s = b_s;
  129. r.u = a.u / b.u;
  130. return r.s;
  131. }
  132. Int128 int128_remu(Int128 a_s, Int128 b_s)
  133. {
  134. Int128Alias r, a, b;
  135. a.s = a_s;
  136. b.s = b_s;
  137. r.u = a.u % b.u;
  138. return r.s;
  139. }
  140. Int128 int128_divs(Int128 a_s, Int128 b_s)
  141. {
  142. Int128Alias r, a, b;
  143. a.s = a_s;
  144. b.s = b_s;
  145. r.i = a.i / b.i;
  146. return r.s;
  147. }
  148. Int128 int128_rems(Int128 a_s, Int128 b_s)
  149. {
  150. Int128Alias r, a, b;
  151. a.s = a_s;
  152. b.s = b_s;
  153. r.i = a.i % b.i;
  154. return r.s;
  155. }
  156. #endif