| 206 | } |
| 207 | |
| 208 | Int128 Int128::div(Int128 op2, int scale) const |
| 209 | { |
| 210 | if (compare(MIN_Int128) == 0 && op2.compare(minus1) == 0) |
| 211 | Arg::Gds(isc_exception_integer_overflow).raise(); |
| 212 | |
| 213 | if (op2.v == 0) |
| 214 | zerodivide(); |
| 215 | |
| 216 | static const CInt128 MIN_BY10(MIN_Int128 / 10); |
| 217 | static const CInt128 MAX_BY10(MAX_Int128 / 10); |
| 218 | |
| 219 | // Scale op1 by as many of the needed powers of 10 as possible without an overflow. |
| 220 | Int128 op1(*this); |
| 221 | int sign1 = op1.sign(); |
| 222 | while ((scale < 0) && (sign1 >= 0 ? op1.compare(MAX_BY10) <= 0 : op1.compare(MIN_BY10) >= 0)) |
| 223 | { |
| 224 | op1.v *= 10; |
| 225 | ++scale; |
| 226 | } |
| 227 | |
| 228 | // Scale op2 shifting it to the right as long as only zeroes are thrown away. |
| 229 | while (scale < 0) |
| 230 | { |
| 231 | int rem = int(v % 10); |
| 232 | if (rem) |
| 233 | break; |
| 234 | op2.v /= 10; |
| 235 | ++scale; |
| 236 | } |
| 237 | |
| 238 | op1.v /= op2.v; |
| 239 | |
| 240 | op1.setScale(scale); |
| 241 | return op1; |
| 242 | } |
| 243 | |
| 244 | void Int128::zerodivide() |
| 245 | { |
no test coverage detected