[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Wishful request for next version: '// 1' should produce integer
- From: Flyer31 Test <flyer31@...>
- Date: Mon, 10 Jan 2022 08:56:39 +0100
> Specialists deserve comfortable programming too :-)
> I'd prefer some sort of @+, @-, @/, @%, @//, @^, @= for integer operators.
> The integer attribute (symbol "@") may be changed to some other symbol (backslash, dollar).
> "x @= y" means "x = floor(y)", it is shorter than "x = y // 1"
Hi Igor,
after another night sleeping over this, I just would like to throw out
@^ from your list (and the @%).
As nice as the power operator ^ is in Lua for "standard users", for
"int specialist programming" ^with int would not make any sense as I
see it (besides ^ is a very special operator, as it is assymetric /
non-commutative, so in principle int^int might be even less
interesting than float^int, and then some others might come up with
int^float). Also I see no reason for @%, sorry.
And I think the Lua team should best decide by themselves, whether
they prefer the "mini solution" with only 2 new operators ** and++
(and keeping //), or whether they prefer @+,@-,@*,@/ (and possibly @=,
why not). This @... writing style would have the advantage, that by
searching for the character @ in a Lua code, you could very easily
check, whether such int arithmetics is used or not in a piece of code.
@Lua team:
Sorry for this "heck-meck", and this so shortly in a New Year. This
was without intention that my "wishful simple request" exteded so much
further. I have now my Lua32 (5.4.3) STM32G4 ARM Cortex M4
applications running, and I am perfectly "flattened and excited" by
Lua, thank you very much.
Concerning my further "side wish" for this zero bit counting and bit
reversal, this would be the following 3 math functions which I would
propose now:
First side wish: math.firstbit( i) (or call it math.lsbitnr) gives the
position of the least significant non-zero bit in an int (resp. the
number of TRAILING zeros). This is VERY useful for bitmask handling of
ints. So then e. g. to get a "bit-masked" value in an int i, you can
use the following small helper function: function getbitmaskvalue( i,
mask) return (i & mask) >> math.lsbitnr( mask) end.
Please best code this in luacode.h as macro. The "general c" style
macro unfortunately quite cumbersome (here given for Lua32, but
extensino to 64 bits straight forward of course):
#define LSBITNR(x) (x&0x1?0:x&0x2?1:x&0x4?2:x&0x8?3: \
x&0x10?4:x&0x20?5:x&0x40?6:x&0x80?7: \
x&0x100?8:x&0x200?9:x&0x400?10:x&0x800?11: \
x&0x1000?12:x&0x2000?13:x&0x4000?14:x&0x8000?15: \
x&0x10000?16:x&0x20000?17:x&0x40000?18:x&0x80000?19: \
x&0x100000?20:x&0x200000?21:x&0x400000?22:x&0x800000?23: \
x&0x1000000?24:x&0x2000000?25:x&0x4000000?26:x&0x8000000?27: \
x&0x10000000?28:x&0x20000000?29:x&0x40000000?30:x&0x80000000?31:32)
In ARM controller you would write #LSBITNR(x) __clz(__rbit(x)), so
much faster. New Intel controllers also have fast assembly coding for
this, but in "general c" unfortuantely still very cumbersome.
Second side wish: math.floatbits( number), the C macro would be
#define FLOATBITS(x) *(int*)&(float)(x) (for Lua32) ... so a very
"wonderful and crazy C macro". This is quite often needed if you want
to analyze binary datastreams containing float numbers.
Third side wisth: math.reversebits( i) (sorry, I do not know a nice c
macro for this - this is NOT so important, but sometimes very nice. In
ARM this is then just __rbit(i)).
Concerning Lua documentation update required for this int arithmetics:
Also these are fully new operators, I think there is NOT much need of
documentation change: This mainly concerns the following 2 paragraphs:
3.4.1 Arithmetic Operators: This will become much shorter again (just
"throw out" any reference to integer, also throw out // floor
division, just write that integer numbers are handled float numbers in
such arithmetics)
3.4.2 Bitwise Operators: Just replace by "Bitwise Operators and
Integer arithmetics", and then there of course add the warning "In
case of overflow in integer arithmetic, all operators wrap around, so
sign inversion will happen."
... and of course strip all partly cumersome and lenghty explanations
of "automatic integer conversion" in the manual - but this clearly any
user will appreciate I think.
Concerning general motivation of int support:
Please do NOT think about stripping int's again from Lua. Int and Bit
arithmetic definitely IS very important, in a fast and "up to date"
language which should assist such things as encryption calculation or
binary data flow analysis (or full resolutino integer counting for
long-time counters / encoders), it is very important that these
operations which are single cycle operation in each modern CPU MUST be
supported/standardized by the language. Thiis also is one of the main
reasons, why C in the 1980's has become so dominant.
And thank you very much for this miracle "world of code" called Lua.