User:Talarus Luan/IntegerLog
Jump to navigation
Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Fast way to get a base 2 log (aka the Most Significant Bit (MSB) position) of a 32-bit integer:
<lsl>integer IntegerLog(integer piData) {
integer liMask; integer liShift; integer liMSB; for (liShift = 16, liMask = 0xFFFF0000, liMSB = 0; liShift > 0; liShift = liShift >> 1, liMask = (liMask >> liShift) & ~liMask) { if ((liMask & piData) != 0) { liMSB += liShift; piData = piData >> liShift; } } return liMSB;
}</lsl>
Can be used to detect a potential integer math overflow for multiply:
<lsl>if ((IntegerLog(N1)+IntegerLog(N2) < 31)
M = N1 * N2; // safe multiply
else
llOwnerSay("Overflow!");</lsl>