|
239 | 239 | # @require 4.6.0 Requires ArkScript 4.6.0 or later |
240 | 240 | (let rshift (fun (_a _b) (builtin__math:rshift _a _b))) |
241 | 241 |
|
| 242 | +# @brief Finds the smallest integral power of 2 not less than the given value |
| 243 | +# @param value Number, must be an integer |
| 244 | +# =begin |
| 245 | +# (math:bitCeil 4) # 4 |
| 246 | +# (math:bitCeil 7) # 8 |
| 247 | +# (math:bitCeil 8) # 8 |
| 248 | +# =end |
| 249 | +# @author https://github.com/SuperFola |
| 250 | +# @require 4.6.0 Requires ArkScript 4.6.0 or later |
| 251 | +(let bitCeil (fun (_x) (builtin__math:bitCeil _x))) |
| 252 | + |
| 253 | +# @brief Finds the largest integral power of 2 not greater than the given value |
| 254 | +# @param value Number, must be an integer |
| 255 | +# =begin |
| 256 | +# (math:bitFloor 4) # 4 |
| 257 | +# (math:bitFloor 7) # 4 |
| 258 | +# (math:bitFloor 8) # 8 |
| 259 | +# =end |
| 260 | +# @author https://github.com/SuperFola |
| 261 | +# @require 4.6.0 Requires ArkScript 4.6.0 or later |
| 262 | +(let bitFloor (fun (_x) (builtin__math:bitFloor _x))) |
| 263 | + |
| 264 | +# @brief Finds the smallest number of bits needed to represent the given value |
| 265 | +# @param value Number, must be an integer |
| 266 | +# =begin |
| 267 | +# (math:bitWidth 2) # 2 |
| 268 | +# (math:bitWidth 7) # 3 |
| 269 | +# (math:bitWidth 8) # 4 |
| 270 | +# =end |
| 271 | +# @author https://github.com/SuperFola |
| 272 | +# @require 4.6.0 Requires ArkScript 4.6.0 or later |
| 273 | +(let bitWidth (fun (_x) (builtin__math:bitWidth _x))) |
| 274 | + |
| 275 | +# @brief Counts the number of consecutive 0 bits, starting from the most significant bit |
| 276 | +# @param n Number, must be an integer |
| 277 | +# @param bits Number of bits to represent n in, must be an integer |
| 278 | +# =begin |
| 279 | +# (math:countLeftZeros 30 8) # 3 |
| 280 | +# (math:countLeftZeros 227 8) # 0 |
| 281 | +# =end |
| 282 | +# @author https://github.com/SuperFola |
| 283 | +# @require 4.6.0 Requires ArkScript 4.6.0 or later |
| 284 | +(let countLeftZeros (fun (_n _bits) (builtin__math:countLeftZeros _n _bits))) |
| 285 | + |
| 286 | +# @brief Counts the number of consecutive 1 bits, starting from the most significant bit |
| 287 | +# @param n Number, must be an integer |
| 288 | +# @param bits Number of bits to represent n in, must be an integer |
| 289 | +# =begin |
| 290 | +# (math:countLeftOnes 30 8) # 4 |
| 291 | +# (math:countLeftOnes 227 8) # 3 |
| 292 | +# =end |
| 293 | +# @author https://github.com/SuperFola |
| 294 | +# @require 4.6.0 Requires ArkScript 4.6.0 or later |
| 295 | +(let countLeftOnes (fun (_n _bits) (builtin__math:countLeftOnes _n _bits))) |
| 296 | + |
| 297 | +# @brief Counts the number of consecutive 0 bits, starting from the least significant bit |
| 298 | +# @param n Number, must be an integer |
| 299 | +# @param bits Number of bits to represent n in, must be an integer |
| 300 | +# =begin |
| 301 | +# (math:countRightZeros 30 8) # 1 |
| 302 | +# (math:countRightZeros 227 8) # 0 |
| 303 | +# =end |
| 304 | +# @author https://github.com/SuperFola |
| 305 | +# @require 4.6.0 Requires ArkScript 4.6.0 or later |
| 306 | +(let countRightZeros (fun (_n _bits) (builtin__math:countRightZeros _n _bits))) |
| 307 | + |
| 308 | +# @brief Counts the number of consecutive 1 bits, starting from the least significant bit |
| 309 | +# @param n Number, must be an integer |
| 310 | +# @param bits Number of bits to represent n in, must be an integer |
| 311 | +# =begin |
| 312 | +# (math:countRightOnes 30 8) # 0 |
| 313 | +# (math:countRightOnes 227 8) # 2 |
| 314 | +# =end |
| 315 | +# @author https://github.com/SuperFola |
| 316 | +# @require 4.6.0 Requires ArkScript 4.6.0 or later |
| 317 | +(let countRightOnes (fun (_n _bits) (builtin__math:countRightOnes _n _bits))) |
| 318 | + |
| 319 | +# @brief Computes the result of bitwise left-rotation of _x by _count |
| 320 | +# @param _x Number, must be an integer |
| 321 | +# @param _count Number, must be an integer |
| 322 | +# @param _bytecount Number of bytes to shift on, must be an integer |
| 323 | +# =begin |
| 324 | +# (math:circularLeftShift 29 4 1) # 209 |
| 325 | +# (math:circularLeftShift 29 9 1) # 58 |
| 326 | +# =end |
| 327 | +# @author https://github.com/SuperFola |
| 328 | +# @require 4.6.0 Requires ArkScript 4.6.0 or later |
| 329 | +(let circularLeftShift (fun (_x (mut _count) _bytecount) { |
| 330 | + (assert (and (>= _bytecount 1) (<= _bytecount 8)) "bytecount must be in [1, 8]") |
| 331 | + (let _max (- (* 8 _bytecount) 1)) |
| 332 | + (let _mask (- (lshift 1 (* 8 _bytecount)) 1)) |
| 333 | + (set _count (bitAnd _count _max)) |
| 334 | + (bitAnd (bitOr (lshift _x _count) (rshift _x (bitAnd (* -1 _count) _max))) _mask) })) |
| 335 | + |
| 336 | +# @brief Computes the result of bitwise left-rotation of _x by _count |
| 337 | +# @param _x Number, must be an integer |
| 338 | +# @param _count Number, must be an integer |
| 339 | +# @param _bytecount Number of bytes to shift on, must be an integer |
| 340 | +# =begin |
| 341 | +# (math:circularRightShift 29 9 1) # 142 |
| 342 | +# (math:circularRightShift 29 2 1) # 71 |
| 343 | +# =end |
| 344 | +# @author https://github.com/SuperFola |
| 345 | +# @require 4.6.0 Requires ArkScript 4.6.0 or later |
| 346 | +(let circularRightShift (fun (_x (mut _count) _bytecount) { |
| 347 | + (assert (and (>= _bytecount 1) (<= _bytecount 8)) "bytecount must be in [1, 8]") |
| 348 | + (let _max (- (* 8 _bytecount) 1)) |
| 349 | + (let _mask (- (lshift 1 (* 8 _bytecount)) 1)) |
| 350 | + (set _count (bitAnd _count _max)) |
| 351 | + (bitAnd (bitOr (rshift _x _count) (lshift _x (bitAnd (* -1 _count) _max))) _mask) })) |
| 352 | + |
| 353 | +(let _base_conversion_alphabet "0123456789abcdefghijklmnopqrstuvwxyz") |
| 354 | + |
| 355 | +# @brief Convert a number in a another base (in [2, 36[) |
| 356 | +# @param _x Number, must be an integer |
| 357 | +# @param _base Number, must be an integer |
| 358 | +# =begin |
| 359 | +# (math:toBase 10 2) # "1010" |
| 360 | +# (math:toBase 165 16) # "a5" |
| 361 | +# =end |
| 362 | +# @author https://github.com/SuperFola |
| 363 | +# @require 4.6.0 Requires ArkScript 4.6.0 or later |
| 364 | +(let toBase (fun ((mut _x) _base) { |
| 365 | + (assert (and (> _base 1) (< _base 36)) "Base must be in [2, 36[") |
| 366 | + (mut _out "") |
| 367 | + (while _x { |
| 368 | + (set _out (+ (@ _base_conversion_alphabet (mod _x _base)) _out)) |
| 369 | + (set _x (floordiv _x _base)) }) |
| 370 | + _out })) |
| 371 | + |
| 372 | +# @brief Count the digits of a number in a given base (in [2, 36[) |
| 373 | +# @param _x Number, must be an integer |
| 374 | +# @param _base Number, must be an integer |
| 375 | +# =begin |
| 376 | +# (math:countDigits 10 2) # 4 |
| 377 | +# (math:countDigits 165 16) # 2 |
| 378 | +# =end |
| 379 | +# @author https://github.com/SuperFola |
| 380 | +# @require 4.6.0 Requires ArkScript 4.6.0 or later |
| 381 | +(let countDigits (fun ((mut _x) _base) { |
| 382 | + (assert (and (> _base 1) (< _base 36)) "Base must be in [2, 36[") |
| 383 | + (mut _out 0) |
| 384 | + (while _x { |
| 385 | + (set _out (+ 1 _out)) |
| 386 | + (set _x (floordiv _x _base)) }) |
| 387 | + _out })) |
| 388 | + |
242 | 389 | # @brief Return the absolute value of a number |
243 | 390 | # @param _x the number to get the absolute value of |
244 | 391 | # @author https://github.com/rstefanic |
|
0 commit comments