Square Root - this method computes and returns the square root of "this". Three mechanisms are used for computation. For small values (<= 5 bits), a table lookup is done. This gets some performance for common cases. For values using less than 52 bits, the value is converted to double and then the libc sqrt function is called. The result is rounded and then converted back to a uint64_t which is the
| 1095 | // back to a uint64_t which is then used to construct the result. Finally, |
| 1096 | // the Babylonian method for computing square roots is used. |
| 1097 | APInt APInt::sqrt() const { |
| 1098 | |
| 1099 | // Determine the magnitude of the value. |
| 1100 | unsigned magnitude = getActiveBits(); |
| 1101 | |
| 1102 | // Use a fast table for some small values. This also gets rid of some |
| 1103 | // rounding errors in libc sqrt for small values. |
| 1104 | if (magnitude <= 5) { |
| 1105 | static const uint8_t results[32] = { |
| 1106 | /* 0 */ 0, |
| 1107 | /* 1- 2 */ 1, 1, |
| 1108 | /* 3- 6 */ 2, 2, 2, 2, |
| 1109 | /* 7-12 */ 3, 3, 3, 3, 3, 3, |
| 1110 | /* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4, |
| 1111 | /* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 1112 | /* 31 */ 6 |
| 1113 | }; |
| 1114 | return APInt(BitWidth, results[ (isSingleWord() ? U.VAL : U.pVal[0]) ]); |
| 1115 | } |
| 1116 | |
| 1117 | // If the magnitude of the value fits in less than 52 bits (the precision of |
| 1118 | // an IEEE double precision floating point value), then we can use the |
| 1119 | // libc sqrt function which will probably use a hardware sqrt computation. |
| 1120 | // This should be faster than the algorithm below. |
| 1121 | if (magnitude < 52) { |
| 1122 | return APInt(BitWidth, |
| 1123 | uint64_t(::round(::sqrt(double(isSingleWord() ? U.VAL |
| 1124 | : U.pVal[0]))))); |
| 1125 | } |
| 1126 | |
| 1127 | // Okay, all the short cuts are exhausted. We must compute it. The following |
| 1128 | // is a classical Babylonian method for computing the square root. This code |
| 1129 | // was adapted to APInt from a wikipedia article on such computations. |
| 1130 | // See http://www.wikipedia.org/ and go to the page named |
| 1131 | // Calculate_an_integer_square_root. |
| 1132 | unsigned nbits = BitWidth, i = 4; |
| 1133 | APInt testy(BitWidth, 16); |
| 1134 | APInt x_old(BitWidth, 1); |
| 1135 | APInt x_new(BitWidth, 0); |
| 1136 | APInt two(BitWidth, 2); |
| 1137 | |
| 1138 | // Select a good starting value using binary logarithms. |
| 1139 | for (;; i += 2, testy = testy.shl(2)) |
| 1140 | if (i >= nbits || this->ule(testy)) { |
| 1141 | x_old = x_old.shl(i / 2); |
| 1142 | break; |
| 1143 | } |
| 1144 | |
| 1145 | // Use the Babylonian method to arrive at the integer square root: |
| 1146 | for (;;) { |
| 1147 | x_new = (this->udiv(x_old) + x_old).udiv(two); |
| 1148 | if (x_old.ule(x_new)) |
| 1149 | break; |
| 1150 | x_old = x_new; |
| 1151 | } |
| 1152 | |
| 1153 | // Make sure we return the closest approximation |
| 1154 | // NOTE: The rounding calculation below is correct. It will produce an |
no test coverage detected