lua-users home
lua-l archive

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


> 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))); }

"Hacker's Delight" suggests this:

  -(x >> K) | (-x >> K), where K = sizeof(x) * 8 - 1.

No conditionals, no multiplications, two logical shifts and three one-clock
instructions (2 subs and 1 or).

-- Roberto