MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / IntSqrt

Function IntSqrt

src/core/math_func.cpp:42–64  ·  view source on GitHub ↗

* Compute the integer square root. * @param num Radicand. * @return Rounded integer square root. * @note Algorithm taken from http://en.wikipedia.org/wiki/Methods_of_computing_square_roots */

Source from the content-addressed store, hash-verified

40 * @note Algorithm taken from http://en.wikipedia.org/wiki/Methods_of_computing_square_roots
41 */
42uint32_t IntSqrt(uint32_t num)
43{
44 uint32_t res = 0;
45 uint32_t bit = 1UL << 30; // Second to top bit number.
46
47 /* 'bit' starts at the highest power of four <= the argument. */
48 while (bit > num) bit >>= 2;
49
50 while (bit != 0) {
51 if (num >= res + bit) {
52 num -= res + bit;
53 res = (res >> 1) + bit;
54 } else {
55 res >>= 1;
56 }
57 bit >>= 2;
58 }
59
60 /* Arithmetic rounding to nearest integer. */
61 if (num > res) res++;
62
63 return res;
64}

Callers 7

CanalMaintenanceCostFunction · 0.85
StationMaintenanceCostFunction · 0.85
RoadMaintenanceCostFunction · 0.85
RailMaintenanceCostFunction · 0.85
SignalMaintenanceCostFunction · 0.85
DemandCalculatorMethod · 0.85
math_func.cppFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected