Decoded: OpenTTD (2003) v1.8 (2018) Source file: math_func.hpp Line-by-line code walkthrough by MaiZure math_func.hpp defines various math functions Original code: https://github.com/MaiZure/OpenTTD-1.8/blob/master/src/core/math_func.hpp 1 COMMENT (*) 2 BLANK (-) 3 * 4 * 5 * 6 * 7 * 8 * 9 - 10 * 11 - 12 Header guard 13 Header guard 14 - 15 * 16 * 17 * 18 * 19 * 20 * 21 * 22 * 23 * 24 * 25 Sets up a function template with one type parameter 26 Defines max with two arguments: input objects to test 27 BLOCK START - max, returns the larger of two inputs 28 Return the result of the ternary comparison 29 BLOCK END - max 30 - 31 * 32 * 33 * 34 * 35 * 36 * 37 * 38 * 39 * 40 * 41 Sets up a function template with one type parameter 42 Defines min with two arguments: input objects to test 43 BLOCK START - min, returns the smaller of two inputs 44 Return the result of the ternary comparison 45 BLOCK END - min 46 - 47 * 48 * 49 * 50 * 51 * 52 * 53 * 54 * 55 * 56 Define min with two input integers 57 BLOCK START - min, returns the smaller of two input integers 58 Returns the result of the int function template for min 59 BLOCK END - min 60 - 61 * 62 * 63 * 64 * 65 * 66 * 67 * 68 * 69 * 70 Defines minu with two input integers 71 BLOCK START - minu, returns the smaller of two unsigned integers 72 Returns the result of the uint function template for min 73 BLOCK END - minu 74 - 75 * 76 * 77 * 78 * 79 * 80 * 81 * 82 Sets up a function template with one parameter for type 83 Defines abs with one argument: a value of some type 84 BLOCK START - abs, returns the absolute value 85 Return the positive variant of the input 86 BLOCK END - abs 87 - 88 * 89 * 90 * 91 * 92 * 93 * 94 * 95 * 96 Sets up a function template a type parameter 97 Defines Align with two arguments, a minimum value and an alignment base 98 BLOCK START - Align, returns the smallest multiple of n greater than x 99 Check that n is a power of 2 and isn't 0 100 Decrement n 101 Return the complement mask of x + n 102 BLOCK END - Align 103 - 104 * 105 * 106 * 107 * 108 * 109 * 110 * 111 * 112 * 113 * 114 Sets up a function template a type parameter 115 Defines AlignPrt with two arguments: a minimum and a base (multiplier) 116 BLOCK START - AlignPtr, ensures the pointer is a multiple of n 117 Ensures that size_t and void * are the same 118 Returns a type-pointer to the aligned value 119 BLOCK END - AlignPtr 120 - 121 * 122 * 123 * 124 * 125 * 126 * 127 * 128 * 129 * 130 * 131 * 132 * 133 * 134 * 135 * 136 * 137 * 138 Sets up a function template with a type parameter 139 Defines clamp with 3 arguments: a typed value, a min, and a max 140 BLOCK START - Clamp, ensures that the input value is between min and max 141 Checks that this call makes sense -- min isn't greater than max 142 If the input is less than min, then return min 143 If the input is less than max, then return max 144 Input must be good already, return the input without changes 145 BLOCK END - Clamp 146 - 147 * 148 * 149 * 150 * 151 * 152 * 153 * 154 * 155 * 156 * 157 * 158 * 159 * 160 * 161 * 162 * 163 Defines Clamp with three arugments, an input, min, and max 164 BLOCK START - Clamp, clamp for integers 165 Returns the result of the Clamp function template with type int 166 BLOCK END - Clamp 167 - 168 * 169 * 170 * 171 * 172 * 173 * 174 * 175 * 176 * 177 * 178 * 179 * 180 * 181 * 182 * 183 * 184 Defines Clamp with three arugments, an input, min, and max 185 BLOCK START - Clamp, with unsigned integers 186 Returns the result of the Clamp function template with type int 187 BLOCK END - Clamp 188 - 189 * 190 * 191 * 192 * 193 * 194 * 195 * 196 * 197 * 198 * 199 * 200 * 201 * 202 * 203 Defines ClampToI32 with one argument: a 64-bit integer 204 BLOCK START - ClampToI32, ensures integer fits in to 32-bits signed 205 Returns Clamp with the min and max range of 32 signed bits 206 BLOCK END - ClampToI32 207 - 208 * 209 * 210 * 211 * 212 * 213 * 214 * 215 Defines ClampToU16 with one argument: a 64-bit integer 216 BLOCK START - ClampToU16, ensures input integer fits in to 16-bits 217 * 218 * 219 * 220 * 221 Returns Clamp with the min and max range of 16 unsigned bits 222 BLOCK END - Clamp 223 - 224 * 225 * 226 * 227 * 228 * 229 * 230 * 231 Sets up a function template with one type parameter 232 Defines Delta with two inputs: two typed values to compare 233 BLOCK START - Delta, returns the positive difference between two inputs 234 Compares input values and subtracts them appropriately for a positive difference 235 BLOCK END - Delta 236 - 237 * 238 * 239 * 240 * 241 * 242 * 243 * 244 * 245 * 246 * 247 * 248 * 249 Sets up a function template with one type parameter 250 Defines IsInsideBS with 4 arguments, an input value, a base, and an offset 251 BLOCK START - IsInsideBS 252 Returns true if the input value is between the base and base+offset 253 BLOCK END - IsInsideBS 254 - 255 * 256 * 257 * 258 * 259 * 260 * 261 * 262 * 263 * 264 * 265 Sets up a function template with one type parameter 266 Defines IsInsideMM with three arguments: a value, min, and max 267 BLOCK START - IsInsideMM, the boolean test of clamp 268 Return true if the value is between the min and max inclusive 269 BLOCK END - IsInsideMM 270 - 271 * 272 * 273 * 274 * 275 * 276 Sets up a function template with one type parameter 277 Defines Swap with two input references 278 BLOCK START - Swap, A type-specific swap 279 Initializes a temporary to the first argument 280 Moves the second value to the first value 281 Moves the stored first value to the second value 282 BLOCK END - Swap 283 - 284 * 285 * 286 * 287 * 288 * 289 Defines ToPercent8 with one argument: an unsigned integer 290 BLOCK START - ToPercent8, returns the percentile range of a byte 291 Verifies that the input integer will fit in a byte 292 Reranges it to between 0 and 100. (255*101>>8 == 100) 293 BLOCK END - ToPercent8 294 - 295 * 296 * 297 * 298 * 299 * 300 Defines ToPercent16 with one argument: an unsigned integer 301 BLOCK START - ToPercent16, returns the percentile range of a two-byte value 302 Confirms that the input value will fit in two bytes 303 Reranges it between 0 and 100 (65535*101>>16 == 100) 304 BLOCK END - ToPercent16 305 - 306 Forward declared LeastCommonMultiple with two arguments 307 Forward declared GreatestCommonDivisor with two arguments 308 Forward declared DivideApprox with two arguments 309 - 310 * 311 * 312 * 313 * 314 * 315 * 316 Defines CeilDiv with two arguments, the numerator and demoninator 317 BLOCK START - CeilDiv, divides two values and returns the ceiling 318 Adds both numbers less 1 and divides by denomintor (common algorithm for uints) 319 BLOCK END - CeilDiv 320 - 321 * 322 * 323 * 324 * 325 * 326 * 327 Defines Ceil with two arguments: a numerator and denominator 328 BLOCK START - Ceil, rounds up. 329 Returns a / b * b 330 BLOCK END - Ceil 331 - 332 * 333 * 334 * 335 * 336 * 337 * 338 Defines RoundDivSU with two arguments: a numerator and denominator 339 BLOCK START - RoundDivSU, round div for signed and unsigned 340 If a is positive...(we round higher at 0.5) 341 * 342 Return the rounded value 343 If a is negative...(we round lower at -0.5) 344 * 345 Return the rounded value 346 End check for the sign of a 347 BLOCK END - RoundDivSU 348 - 349 Forward declares IntSqrt 350 - 351 Header guard end