sizeExtend & signExtend
# kotlin-native
z
sizeExtend & signExtend
o
not sure if I understand, what's your point here. When casting 16-bit to 32-bit value, for example, there are diffrent ways of performing this operation for negative values, i.e. 0xffff can produce either 0x0000ffff (==65535 if seen as Int) or 0xfffffff (-1 if seen as Int), signExtend explicitly specifies that later behavior is taken
z
but how do you know when to extend and when to shrink?
o
it depends on the API you're trying to call, look at generated bindings to see what type is in method signature
z
the size of the type is runtime dependend, so how do you know at compile time if you need to extend or shrink?
(or am I missing something?)
o
Could you provide an example, so that we have better context?
z
say on one architecture size_t is 16-bit, on another architecture it's 64-bit
I have a 32-bit int that I need to convert to size_it
in one case I need to narrow it, in another case I need to extend it
how would I know which one to choose if the choice depends on the runtime?
o
any really usable platform where this happens?
z
depends if you want to support something like arduino uno which is 16-bit
the thing is that the current approach only really works if you intend to compile code for 2 different sized architectures (eg 32 & 64)
(which is fine for me btw, it was mostly an observation)
one more question
does the extend or reduce call always reduce/extend size to the target type, or does it do it regardless of the target type's size?
*narrow
ie, if I call extend on a 32bit int, and my target size_t is 32bit at runtime, will it make it 64bit or keep it at 32bit?
o
note that stubs are generated per-target, so
sizeof(size_t)
is known at compile time
s
ie, if I call extend on a 32bit int, and my target size_t is 32bit at runtime, will it make it 64bit or keep it at 32bit?
These functions extend or reduce the size exactly to the target type. So if you call extend on a 32bit int, and your target size_t is 32bit, then it will keep it at 32bit.
say on one architecture size_t is 16-bit, on another architecture it's 64-bit
I have a 32-bit int that I need to convert to size_it
In this case you can convert your 32-bit
Int
to
Short
and then sign-extend it to
size_t
. It seems to be the most safe approach.
z
ok thanks, that makes things clear!