[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Is there a C API that could exchange the values at different index of the stack?
- From: Francisco Olarte <folarte@...>
- Date: Fri, 22 Jan 2021 17:55:59 +0100
Andrew:
On Fri, Jan 22, 2021 at 12:06 PM Andrew Gierth
<andrew@tao11.riddles.org.uk> wrote:
> If you're not tied to lua 5.1, you could simplify that:
AAMOF my (production) code is tied to 5.3+, but I was translating the
straight forward push i, push j , pop i, pop j and forgot the API has
lua_copy, which in fact I've used sometimes, and can do push i, i=j,
pop j translating my ususal C-one liner "int t=i; i=j; j=t;"
> (The stack check could also be omitted and made the responsibility of
> the caller)
Yep, I'm not sure what is the usual policy, but I tend to code my aux
funcs assuming the caller has checked for enough space for what I'm
returning, and as I return nothing there I checked it.
> In 5.3 or later you can do it without needing an extra stack slot,
> though this is probably less efficient (needs 4 rotates).
I toyed with the idea, but discarded it as it needs still more
normalization and possibly ordering of the parameters, besides the
potentially huge impact of rotations. A native swap, even if limited
to stack entries, would be a nice addition, as I've found myself
rotating the top entries many times, and a swap seems to be easy to
do.
Francisco Olarte.