proc walkKing(kk: KK; s: var KKS) =
var i: int
var kk = kk
while true:
kk.di = king_path[kk.si][i].pos
if kk.di == -1: break
kk.df = board[kk.di]
if kk.df == 0 or capture(kk):
s.add kk
inc i
proc walkKing(kk: KK; s: var KKS) =
var kk = kk
for i in >= 0:#countup(0):
if (kk.di := king_path[kk.si][i].pos) < 0: break
if (kk.df := board[kk.di]) == 0 or capture(kk):
s.add kk
Personally I would prefer the second variant. Seems to work fine with (IntLikeForCount from system.nim)
[EDIT]
I think when counting without an (upper) bound the conditional "when T is IntLikeForCount:" in not necessary. For uint it may wrap around, but that may be the desired behaviour. So now I have:
template countupImpl2(incr: stmt) {.immediate, dirty.} =
#when T is IntLikeForCount:
# var res = int(a)
# while true:
# yield T(res)
# incr
#else:
var res = a
while true:
yield res
incr
iterator countup_by*[T](a: T; step = 1): T {.inline.} =
## Counts from ordinal value `a` up with the given
## step size. `T` may be any ordinal type, `step` may
## be positive or negative.
countupImpl2:
inc(res, step)
iterator countup*[T](a: T): T {.inline.} =
## Counts from ordinal value `a` up with
## step size 1. `T` may be any ordinal type.
countupImpl2:
inc(res, 1)
iterator `>=`*[T](a: T): T {.inline.} =
## An alias for `countup`.
countupImpl2:
inc(res, 1)
proc `:=`(x: var int; v: int): int =
x = v
result = v
I don't like assignment as expression. ;-) It's easy to do with a := template though. Exercise for the reader.
For uint it may wrap around, but that may be the desired behaviour.
It may be, but we got lots of bug reports about it and eventually fixed it...
Yes, it can occur that one wants to write "=" and types "==" or vice versa. So I think that it is a good decision that assignment with "=" is not an expression. But some people may miss the expressions as known from C or Ruby, and for some special cases expressions may be nice. Do you think that the proc definition abve is not good and a template would be better?
Four the unbound countup with only a starting value: I said only that for this case the conditional may not be necessary. Indeed I think that for a countup with an upper boundary it may be necessary to prevent overflow or warp around...
General interesting is, that gcc 5.3 seems not to use inlining at all from itself, its needs always a hint to do so:
proc even(i: int): bool {.inline.} = i mod 2 == 0
Even this is not inlined with -O3 without the inline pragma.
Even this is not inlined with -O3 without the inline pragma.
Are you sure? If it's not .inline and in a different module it ends up in a different .c file and then you need linktime optimizations to get GCC to inline it.
I am nearly sure. And I know about the problems with inline and different modules, Jehan told us...
I tested within a single module, engine.nim of my toy chess game. There exists a few very small procs like odd, even, row(), the are not exported.
I started without explicit inline pragma, guessing that gcc would inline it, because it reall makes sense.
But explicit applying inline pragma reduces total executable code size about 100 bytes for each proc -- so there is an effect. And doing it for all the small procs makes the program really faster. Of course, to be sure I have to look at the assembler listing -- I am not really good at that :-(
I am not sure how other releases of gcc or clang do it, this observation was for gcc 5.3. Of course, when inlining, the address of the function is not available, maybe that is the reason that gcc does it not itself. (from the manual we know that inline pragma is only a hint that gcc may inline it, so I tend to use inline pragma everywhere. gcc seems to be smart enough to ignore it when it makes no sense. For test where it makes no sense I applied it to proc alphabeta() which calls itself recursively, gave no problems, so I think gcc was smart and ignored it here.)
What is also interesting: delault executable size with -d:release is 120k for gcc. LTO can reduce size a lot, smallest is clang -O4 LTO, which is only 70k. (Strip can reduce size again by 10k.)