more bitshifting and benchmark changes
This commit is contained in:
parent
e9c6b615a1
commit
2165511624
112
faststack.nim
112
faststack.nim
@ -1,6 +1,10 @@
|
|||||||
import bitops, strutils
|
import bitops, strutils
|
||||||
|
|
||||||
|
|
||||||
|
proc show(i: SomeInteger, bitlength = 16) =
|
||||||
|
echo BiggestInt(i).toBin(bitlength)
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
Color = enum
|
Color = enum
|
||||||
cRed, cGreen, cBlue, cYellow, cPurple
|
cRed, cGreen, cBlue, cYellow, cPurple
|
||||||
@ -12,14 +16,29 @@ type
|
|||||||
|
|
||||||
const
|
const
|
||||||
masks = [
|
masks = [
|
||||||
0'u16,
|
0'u16, # dummy value just to get the indices right
|
||||||
7,
|
0b0_000_000_000_000_111,
|
||||||
63,
|
0b0_000_000_000_111_111,
|
||||||
511,
|
0b0_000_000_111_111_111,
|
||||||
4095,
|
0b0_000_111_111_111_111,
|
||||||
|
0b0_111_111_111_111_111,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
template offset(s: ColorStack, idx: Natural): uint8 =
|
||||||
|
# Compute the bit offset for a given index.
|
||||||
|
# Dependent on the stack's length.
|
||||||
|
(s.len - 1 - cast[uint8](idx)) * 3
|
||||||
|
# items are stored from left to right but right-aligned, so we
|
||||||
|
# need to shift right to access anything other than the last
|
||||||
|
|
||||||
|
|
||||||
|
template offset(s: ColorStack, idx: BackwardsIndex): uint8 =
|
||||||
|
# for backwards index, we are still shifting right but
|
||||||
|
# the lower the index the less we have to shift
|
||||||
|
(cast[uint8](idx) - 1) * 3
|
||||||
|
|
||||||
|
|
||||||
proc add(s: var ColorStack, c: Color) =
|
proc add(s: var ColorStack, c: Color) =
|
||||||
# e.g. if stack is 0b0000000000000100:
|
# e.g. if stack is 0b0000000000000100:
|
||||||
# and color is 0b00000011
|
# and color is 0b00000011
|
||||||
@ -27,34 +46,65 @@ proc add(s: var ColorStack, c: Color) =
|
|||||||
# bitor: 0b0000000000100000 and 0b00000011
|
# bitor: 0b0000000000100000 and 0b00000011
|
||||||
# results in 0b0000000000100011
|
# results in 0b0000000000100011
|
||||||
s.data = (s.data shl 3).bitor(cast[uint8](c))
|
s.data = (s.data shl 3).bitor(cast[uint8](c))
|
||||||
echo BiggestInt(s.data).toBin(16)
|
|
||||||
inc s.len
|
inc s.len
|
||||||
|
|
||||||
|
|
||||||
|
proc high(s: ColorStack): uint8 = s.len - 1
|
||||||
|
|
||||||
|
proc low(s: ColorStack): uint8 = 0 # just... always 0, I guess
|
||||||
|
|
||||||
|
|
||||||
|
proc `[]`(s: ColorStack, i: uint8 | BackwardsIndex): Color =
|
||||||
|
# shift, then mask everything but the three rightmost bits
|
||||||
|
result = cast[Color](
|
||||||
|
(s.data shr s.offset(i)) and masks[1]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
proc `[]=`(s: var ColorStack, i: uint8 | BackwardsIndex, c: Color) =
|
||||||
|
let offset = s.offset(i)
|
||||||
|
s.data = (s.data and bitnot(masks[1] shl offset)) or (cast[uint16](c) shl offset)
|
||||||
|
|
||||||
|
|
||||||
|
iterator items(s: ColorStack): Color =
|
||||||
|
# s.len is unsigned so it will wrap around if it's 0
|
||||||
|
# but that's no problem, we can just no-op if len is 0
|
||||||
|
if s.len != 0:
|
||||||
|
for i in countdown(s.len - 1, 0'u8):
|
||||||
|
yield cast[Color]((s.data shr (i * 3)) and masks[1])
|
||||||
|
|
||||||
|
|
||||||
|
iterator pairs(s: ColorStack): (int, Color) =
|
||||||
|
var count = 0
|
||||||
|
for color in s:
|
||||||
|
yield (count, color)
|
||||||
|
inc count
|
||||||
|
|
||||||
|
|
||||||
proc moveSubstack(src, dst: var ColorStack, nToMove: uint8) =
|
proc moveSubstack(src, dst: var ColorStack, nToMove: uint8) =
|
||||||
|
# Moves a sub-stack from the top of src to the top of dst
|
||||||
# shift the dst stack by the length of the substack to make room
|
# shift the dst stack by the length of the substack to make room
|
||||||
let shift = nToMove * 3
|
let shift = nToMove * 3
|
||||||
dst.data = dst.data shl shift
|
dst.data = dst.data shl shift
|
||||||
# then we mask the source data to present only the items
|
# then we mask the source data to present only the items
|
||||||
# being moved, and AND that with the shifted dst data
|
# being moved, and OR that with the shifted dst data
|
||||||
dst.data = bitor(
|
dst.data = dst.data or (src.data and masks[src.len])
|
||||||
bitand(src.data, masks[src.len]),
|
|
||||||
dst.data
|
|
||||||
)
|
|
||||||
dst.len += nToMove
|
dst.len += nToMove
|
||||||
# then we shift the source to get rid of the moved items
|
# then we shift the source to get rid of the moved items
|
||||||
src.data = src.data shr shift
|
src.data = src.data shr shift
|
||||||
src.len -= nToMove
|
src.len -= nToMove
|
||||||
|
|
||||||
|
|
||||||
iterator items(s: ColorStack): Color =
|
proc moveSubstackPre(src, dst: var ColorStack, nToMove: uint8) =
|
||||||
for offset in countdown(s.len - 1, 0'u8):
|
# Moves a sub-stack from the top of src to the bottom of dst
|
||||||
let shifted = s.data shr offset
|
# shift src stack to position substack above its destination,
|
||||||
echo shifted
|
# get rid of everything to the left of the substack,
|
||||||
echo bitand(shifted, masks[0])
|
# and OR that with the existing dst data
|
||||||
yield Color(bitand(shifted, masks[0]))
|
dst.data = dst.data or ( (src.data shl (dst.len * 3)) and masks[src.len])
|
||||||
# yield s.data shr i * 3 |> bitand(masks[0]) |> Color
|
dst.len += nToMove
|
||||||
# :(
|
# get rid of the substack we just moved
|
||||||
|
src.data = src.data shr (nToMove * 3)
|
||||||
|
src.len -= nToMove
|
||||||
|
|
||||||
|
|
||||||
proc `$`(s: ColorStack): string =
|
proc `$`(s: ColorStack): string =
|
||||||
@ -69,14 +119,30 @@ proc `$`(s: ColorStack): string =
|
|||||||
var one: ColorStack
|
var one: ColorStack
|
||||||
one.add(cGreen)
|
one.add(cGreen)
|
||||||
one.add(cBlue)
|
one.add(cBlue)
|
||||||
|
one.add(cYellow)
|
||||||
|
# one.add(cPurple)
|
||||||
|
# one.add(cRed)
|
||||||
|
|
||||||
var two: ColorStack
|
var two: ColorStack
|
||||||
two.add(cPurple)
|
# two.add(cPurple)
|
||||||
two.add(cRed)
|
two.add(cRed)
|
||||||
two.add(cYellow)
|
# two.add(cYellow)
|
||||||
|
|
||||||
echo "one: ", one
|
echo "one: ", one
|
||||||
# echo "two: ", two
|
echo "two: ", two
|
||||||
|
|
||||||
|
one[^2] = cPurple
|
||||||
|
echo "one: ", one
|
||||||
|
|
||||||
|
# echo "one 0: ", one[0]
|
||||||
|
# echo "one 2: ", one[2]
|
||||||
|
# echo "one 3: ", one[3]
|
||||||
|
|
||||||
|
# echo "one ^1: ", one[^1]
|
||||||
|
# echo "one ^2: ", one[^2]
|
||||||
|
# echo "one ^4: ", one[^4]
|
||||||
|
|
||||||
|
# echo "<move>"
|
||||||
# moveSubstack(one, two, 2)
|
# moveSubstack(one, two, 2)
|
||||||
# echo two
|
# echo "one: ", one
|
||||||
|
# echo "two: ", two
|
57
test.nim
57
test.nim
@ -1,7 +1,32 @@
|
|||||||
import math, random, strformat, times
|
import math, random, strformat, times, std/monotimes
|
||||||
import fixedseq, game, simulation, ui
|
import fixedseq, game, simulation, ui
|
||||||
|
|
||||||
|
|
||||||
|
type
|
||||||
|
TestResults = object
|
||||||
|
ops: int
|
||||||
|
time: Duration
|
||||||
|
|
||||||
|
|
||||||
|
proc summarize(tr: TestResults) =
|
||||||
|
let secs = tr.time.inMilliseconds.float / 1000
|
||||||
|
stdout.write("Test completed:\n")
|
||||||
|
stdout.write(" " & $tr.ops, " operations in " & $round(secs, 2) & " seconds\n")
|
||||||
|
stdout.write(" " & $round(tr.ops.float / secs, 2) & " operations per second")
|
||||||
|
stdout.flushFile()
|
||||||
|
|
||||||
|
|
||||||
|
template executionTime(body: untyped): Duration =
|
||||||
|
let start = getMonoTime()
|
||||||
|
body
|
||||||
|
getMonoTime() - start
|
||||||
|
|
||||||
|
|
||||||
|
proc getRand(): Rand =
|
||||||
|
randomize()
|
||||||
|
result = initRand(rand(int64))
|
||||||
|
|
||||||
|
|
||||||
proc randomDice(r: var Rand): seq[tuple[c: Color, p: int]] =
|
proc randomDice(r: var Rand): seq[tuple[c: Color, p: int]] =
|
||||||
for c in Color:
|
for c in Color:
|
||||||
let v = r.rand(1..3)
|
let v = r.rand(1..3)
|
||||||
@ -18,18 +43,15 @@ proc newRandomGame(r: var Rand): Board =
|
|||||||
result.setState(dice, [])
|
result.setState(dice, [])
|
||||||
|
|
||||||
|
|
||||||
proc testGames(n: SomeInteger = 100): auto =
|
proc games(nTests, nSamples: SomeInteger): TestResults =
|
||||||
var r = initRand(rand(int64))
|
var r = getRand()
|
||||||
let dice = randomDice(r)
|
var scores: ScoreSet
|
||||||
var b: Board
|
for i in 1 .. nTests:
|
||||||
b.init
|
let b = newRandomGame(r)
|
||||||
b.setState(dice, [])
|
let dur = executionTime:
|
||||||
b.display(1, 5)
|
let s = b.randomGames(nSamples)
|
||||||
|
result.ops += s.sum()
|
||||||
let startTime = cpuTime()
|
result.time += dur
|
||||||
let scores = b.randomGames(n)
|
|
||||||
result = cpuTime() - startTime
|
|
||||||
scores.display()
|
|
||||||
|
|
||||||
|
|
||||||
proc testLegs(n: Natural = 100): auto =
|
proc testLegs(n: Natural = 100): auto =
|
||||||
@ -72,14 +94,7 @@ proc testSpread(nTests, nSamples: Natural) =
|
|||||||
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
randomize()
|
games(100, 1_000_000).summarize()
|
||||||
var r = initRand(rand(int64))
|
|
||||||
let b = newRandomGame(r)
|
|
||||||
b.display(1, 5)
|
|
||||||
echo b.showSpaces(1, 16)
|
|
||||||
|
|
||||||
let scores = b.getLegScores
|
|
||||||
echo scores.showPercents
|
|
||||||
# let start_states = 2_000
|
# let start_states = 2_000
|
||||||
# let executionTime = testLegs(start_states)
|
# let executionTime = testLegs(start_states)
|
||||||
# echo "Execution time: ", executionTime
|
# echo "Execution time: ", executionTime
|
||||||
|
Loading…
x
Reference in New Issue
Block a user