Module pico8lib.number
Non-mathematical manipulation of numbers
popcount_loop (n) |
Count the 1 bits in a number
This version runs one loop per set bit, 26 tokens |
popcount_magic (v) |
Count the 1 bits in a number
This version runs in constant time, 42 tokens
Constant time is 111% the one-set-bit time of the shorter loop function |
rotr8 (n, b) |
Rotate an 8-bit integer n to the right by b bits |
rotl8 (n, b) |
Rotate an 8-bit integer n to the left by b bits |
-
const_number_minfrac
-
Smallest representable positive fraction (
1/32768
)
-
const_number_maxint
-
Maximum representable positive integer (
32767
)
-
const_number_maxnum
-
Maximum representable positive number (
32767 + 32767/32768
)
-
const_number_minnum
-
Minimum representable negative integer and number (
-32768
)
-
rand (x)
-
Random value [0 to x-1]
such as pixel or grid offsets
Parameters:
-
randi (x)
-
Random value [0 to x]
such as pixels in/on a rect
Parameters:
-
randp (x)
-
Random value [1 to x-1]
such as pixels in a rect
Parameters:
-
randpi (x)
-
Random value [1 to x]
such as lua list indices
Parameters:
-
randb (n, x)
-
Random value [n to x-1]
Parameters:
-
randbi (n, x)
-
Random value [n to x-1]
Parameters:
-
randfrac (n, x)
-
Random value [n to x] for non-integer n and x
Parameters:
-
popcount_loop (n)
-
Count the 1 bits in a number
This version runs one loop per set bit, 26 tokens
Parameters:
- n
algorithm from https
//graphics.stanford.edu/~seander/bithacks.html
-
popcount_magic (v)
-
Count the 1 bits in a number
This version runs in constant time, 42 tokens
Constant time is 111% the one-set-bit time of the shorter loop function
Parameters:
- v
original implementation from https
//www.lexaloffle.com/bbs/?pid=72850 [2020-02-06 16:25] felice: if i post code in a bbs post then it's free for all to use. credit is nice of course if it's significant. algorithm from https://support.amd.com/techdocs/25112.pdf or https://graphics.stanford.edu/~seander/bithacks.html
-
rotr8 (n, b)
-
Rotate an 8-bit integer n to the right by b bits
Parameters:
-
rotl8 (n, b)
-
Rotate an 8-bit integer n to the left by b bits
Parameters:
-
flrceil (x, d)
-
Floor or Ceiling of a number
Parameters:
- x
number
The number to floor or ceiling
- d
number
1 for floor, -1 for ceiling
Returns:
number
The floor'd or ceiling'd number
-
tozero (n)
-
Round toward zero
Parameters:
-
toinf (n)
-
Round toward infinity
Parameters:
-
round (n)
-
Round to nearest integer, 0.5 rounds toward +Inf
Parameters: