Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 1 | /* ===-- udivmoddi4.c - Implement __udivmoddi4 -----------------------------=== |
| 2 | * |
| 3 | * The LLVM Compiler Infrastructure |
| 4 | * |
Howard Hinnant | 9ad441f | 2010-11-16 22:13:33 +0000 | [diff] [blame] | 5 | * This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | * Source Licenses. See LICENSE.TXT for details. |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 7 | * |
| 8 | * ===----------------------------------------------------------------------=== |
| 9 | * |
| 10 | * This file implements __udivmoddi4 for the compiler_rt library. |
| 11 | * |
| 12 | * ===----------------------------------------------------------------------=== |
| 13 | */ |
Anton Korobeynikov | 1c5f89b | 2011-04-19 17:52:09 +0000 | [diff] [blame] | 14 | #include "abi.h" |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 15 | |
| 16 | #include "int_lib.h" |
| 17 | |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 18 | /* Effects: if rem != 0, *rem = a % b |
| 19 | * Returns: a / b |
| 20 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 21 | |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 22 | /* Translated from Figure 3-40 of The PowerPC Compiler Writer's Guide */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 23 | |
Anton Korobeynikov | 37b97d1 | 2011-04-19 17:51:24 +0000 | [diff] [blame] | 24 | ARM_EABI_FNALIAS(uldivmod, udivmoddi4); |
| 25 | |
Anton Korobeynikov | 1c5f89b | 2011-04-19 17:52:09 +0000 | [diff] [blame] | 26 | COMPILER_RT_ABI du_int |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 27 | __udivmoddi4(du_int a, du_int b, du_int* rem) |
| 28 | { |
| 29 | const unsigned n_uword_bits = sizeof(su_int) * CHAR_BIT; |
| 30 | const unsigned n_udword_bits = sizeof(du_int) * CHAR_BIT; |
| 31 | udwords n; |
| 32 | n.all = a; |
| 33 | udwords d; |
| 34 | d.all = b; |
| 35 | udwords q; |
| 36 | udwords r; |
| 37 | unsigned sr; |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 38 | /* special cases, X is unknown, K != 0 */ |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 39 | if (n.s.high == 0) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 40 | { |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 41 | if (d.s.high == 0) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 42 | { |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 43 | /* 0 X |
| 44 | * --- |
| 45 | * 0 X |
| 46 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 47 | if (rem) |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 48 | *rem = n.s.low % d.s.low; |
| 49 | return n.s.low / d.s.low; |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 50 | } |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 51 | /* 0 X |
| 52 | * --- |
| 53 | * K X |
| 54 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 55 | if (rem) |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 56 | *rem = n.s.low; |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 57 | return 0; |
| 58 | } |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 59 | /* n.s.high != 0 */ |
| 60 | if (d.s.low == 0) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 61 | { |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 62 | if (d.s.high == 0) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 63 | { |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 64 | /* K X |
| 65 | * --- |
| 66 | * 0 0 |
| 67 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 68 | if (rem) |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 69 | *rem = n.s.high % d.s.low; |
| 70 | return n.s.high / d.s.low; |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 71 | } |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 72 | /* d.s.high != 0 */ |
| 73 | if (n.s.low == 0) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 74 | { |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 75 | /* K 0 |
| 76 | * --- |
| 77 | * K 0 |
| 78 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 79 | if (rem) |
| 80 | { |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 81 | r.s.high = n.s.high % d.s.high; |
| 82 | r.s.low = 0; |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 83 | *rem = r.all; |
| 84 | } |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 85 | return n.s.high / d.s.high; |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 86 | } |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 87 | /* K K |
| 88 | * --- |
| 89 | * K 0 |
| 90 | */ |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 91 | if ((d.s.high & (d.s.high - 1)) == 0) /* if d is a power of 2 */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 92 | { |
| 93 | if (rem) |
| 94 | { |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 95 | r.s.low = n.s.low; |
| 96 | r.s.high = n.s.high & (d.s.high - 1); |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 97 | *rem = r.all; |
| 98 | } |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 99 | return n.s.high >> __builtin_ctz(d.s.high); |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 100 | } |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 101 | /* K K |
| 102 | * --- |
| 103 | * K 0 |
| 104 | */ |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 105 | sr = __builtin_clz(d.s.high) - __builtin_clz(n.s.high); |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 106 | /* 0 <= sr <= n_uword_bits - 2 or sr large */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 107 | if (sr > n_uword_bits - 2) |
| 108 | { |
| 109 | if (rem) |
| 110 | *rem = n.all; |
| 111 | return 0; |
| 112 | } |
| 113 | ++sr; |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 114 | /* 1 <= sr <= n_uword_bits - 1 */ |
| 115 | /* q.all = n.all << (n_udword_bits - sr); */ |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 116 | q.s.low = 0; |
| 117 | q.s.high = n.s.low << (n_uword_bits - sr); |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 118 | /* r.all = n.all >> sr; */ |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 119 | r.s.high = n.s.high >> sr; |
| 120 | r.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr); |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 121 | } |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 122 | else /* d.s.low != 0 */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 123 | { |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 124 | if (d.s.high == 0) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 125 | { |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 126 | /* K X |
| 127 | * --- |
| 128 | * 0 K |
| 129 | */ |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 130 | if ((d.s.low & (d.s.low - 1)) == 0) /* if d is a power of 2 */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 131 | { |
| 132 | if (rem) |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 133 | *rem = n.s.low & (d.s.low - 1); |
| 134 | if (d.s.low == 1) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 135 | return n.all; |
Joerg Sonnenberger | d426091 | 2011-07-28 19:47:33 +0000 | [diff] [blame] | 136 | sr = __builtin_ctz(d.s.low); |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 137 | q.s.high = n.s.high >> sr; |
| 138 | q.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr); |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 139 | return q.all; |
| 140 | } |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 141 | /* K X |
| 142 | * --- |
| 143 | *0 K |
| 144 | */ |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 145 | sr = 1 + n_uword_bits + __builtin_clz(d.s.low) - __builtin_clz(n.s.high); |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 146 | /* 2 <= sr <= n_udword_bits - 1 |
| 147 | * q.all = n.all << (n_udword_bits - sr); |
| 148 | * r.all = n.all >> sr; |
| 149 | * if (sr == n_uword_bits) |
| 150 | * { |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 151 | * q.s.low = 0; |
| 152 | * q.s.high = n.s.low; |
| 153 | * r.s.high = 0; |
| 154 | * r.s.low = n.s.high; |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 155 | * } |
| 156 | * else if (sr < n_uword_bits) // 2 <= sr <= n_uword_bits - 1 |
| 157 | * { |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 158 | * q.s.low = 0; |
| 159 | * q.s.high = n.s.low << (n_uword_bits - sr); |
| 160 | * r.s.high = n.s.high >> sr; |
| 161 | * r.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr); |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 162 | * } |
| 163 | * else // n_uword_bits + 1 <= sr <= n_udword_bits - 1 |
| 164 | * { |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 165 | * q.s.low = n.s.low << (n_udword_bits - sr); |
| 166 | * q.s.high = (n.s.high << (n_udword_bits - sr)) | |
| 167 | * (n.s.low >> (sr - n_uword_bits)); |
| 168 | * r.s.high = 0; |
| 169 | * r.s.low = n.s.high >> (sr - n_uword_bits); |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 170 | * } |
| 171 | */ |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 172 | q.s.low = (n.s.low << (n_udword_bits - sr)) & |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 173 | ((si_int)(n_uword_bits - sr) >> (n_uword_bits-1)); |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 174 | q.s.high = ((n.s.low << ( n_uword_bits - sr)) & |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 175 | ((si_int)(sr - n_uword_bits - 1) >> (n_uword_bits-1))) | |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 176 | (((n.s.high << (n_udword_bits - sr)) | |
| 177 | (n.s.low >> (sr - n_uword_bits))) & |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 178 | ((si_int)(n_uword_bits - sr) >> (n_uword_bits-1))); |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 179 | r.s.high = (n.s.high >> sr) & |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 180 | ((si_int)(sr - n_uword_bits) >> (n_uword_bits-1)); |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 181 | r.s.low = ((n.s.high >> (sr - n_uword_bits)) & |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 182 | ((si_int)(n_uword_bits - sr - 1) >> (n_uword_bits-1))) | |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 183 | (((n.s.high << (n_uword_bits - sr)) | |
| 184 | (n.s.low >> sr)) & |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 185 | ((si_int)(sr - n_uword_bits) >> (n_uword_bits-1))); |
| 186 | } |
| 187 | else |
| 188 | { |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 189 | /* K X |
| 190 | * --- |
| 191 | * K K |
| 192 | */ |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 193 | sr = __builtin_clz(d.s.high) - __builtin_clz(n.s.high); |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 194 | /* 0 <= sr <= n_uword_bits - 1 or sr large */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 195 | if (sr > n_uword_bits - 1) |
| 196 | { |
| 197 | if (rem) |
| 198 | *rem = n.all; |
| 199 | return 0; |
| 200 | } |
| 201 | ++sr; |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 202 | /* 1 <= sr <= n_uword_bits */ |
| 203 | /* q.all = n.all << (n_udword_bits - sr); */ |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 204 | q.s.low = 0; |
| 205 | q.s.high = n.s.low << (n_uword_bits - sr); |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 206 | /* r.all = n.all >> sr; |
| 207 | * if (sr < n_uword_bits) |
| 208 | * { |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 209 | * r.s.high = n.s.high >> sr; |
| 210 | * r.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr); |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 211 | * } |
| 212 | * else |
| 213 | * { |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 214 | * r.s.high = 0; |
| 215 | * r.s.low = n.s.high; |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 216 | * } |
| 217 | */ |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 218 | r.s.high = (n.s.high >> sr) & |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 219 | ((si_int)(sr - n_uword_bits) >> (n_uword_bits-1)); |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 220 | r.s.low = (n.s.high << (n_uword_bits - sr)) | |
| 221 | ((n.s.low >> sr) & |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 222 | ((si_int)(sr - n_uword_bits) >> (n_uword_bits-1))); |
| 223 | } |
| 224 | } |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 225 | /* Not a special case |
| 226 | * q and r are initialized with: |
| 227 | * q.all = n.all << (n_udword_bits - sr); |
| 228 | * r.all = n.all >> sr; |
| 229 | * 1 <= sr <= n_udword_bits - 1 |
| 230 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 231 | su_int carry = 0; |
| 232 | for (; sr > 0; --sr) |
| 233 | { |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 234 | /* r:q = ((r:q) << 1) | carry */ |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 235 | r.s.high = (r.s.high << 1) | (r.s.low >> (n_uword_bits - 1)); |
| 236 | r.s.low = (r.s.low << 1) | (q.s.high >> (n_uword_bits - 1)); |
| 237 | q.s.high = (q.s.high << 1) | (q.s.low >> (n_uword_bits - 1)); |
| 238 | q.s.low = (q.s.low << 1) | carry; |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 239 | /* carry = 0; |
| 240 | * if (r.all >= d.all) |
| 241 | * { |
| 242 | * r.all -= d.all; |
| 243 | * carry = 1; |
| 244 | * } |
| 245 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 246 | const di_int s = (di_int)(d.all - r.all - 1) >> (n_udword_bits - 1); |
| 247 | carry = s & 1; |
| 248 | r.all -= d.all & s; |
| 249 | } |
| 250 | q.all = (q.all << 1) | carry; |
| 251 | if (rem) |
| 252 | *rem = r.all; |
| 253 | return q.all; |
| 254 | } |