lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Mar 26, 2014, at 9:41 AM, Roberto Ierusalimschy <> wrote:

>> Let’s be clear, arithmetic shift is a useful tool as a low-level
>> operation for more than just shorthand division — this is the
>> reason it exists as a basic operation in every mainstream CPU
>> architecture. I’ve used it over the years for the signum function
>> (where is works very well indeed) optimized bit-counting etc etc. It
>> should be noted that almost all these uses are where performance is
>> hyper-critical.
> Just for curiosity, do you remember those techniques?
> -- Roberto

I’m a bit ashamed to say that i do lol. Signum for example:

static inline int sgn2(int x) { return (x != 0) * (1 | (x >> ((sizeof(x) * 8) - 1))); }

The code generated has only one conditional branch … significant on a non super-scalar CPU.

Mostly of course these are techniques used in assembly language “poking through” into C.