Variable byte sizes

Monkey Forums/Monkey Programming/Variable byte sizes

EdzUp(Posted 2015) [#1]
What are the size of the variables used by monkey.

I know that int is 4 bytes (32bit) but what are the others like bool and float etc?


ImmutableOctet(SKNG)(Posted 2015) [#2]
This depends on the target, but generally, 'Int' is going to be a signed 32-bit integer. This does not have to be the case, but it's preferred. The 'Float' type can either be 32-bit (Single precision), or 64-bit (Double precision) depending on the target and settings. On the C++ targets, for example, the 'Float' type can be manually set to double-precision with the preprocessor. This can be done by setting 'CPP_DOUBLE_PRECISION_FLOATS' to 'True' with the preprocessor. The 'Bool' type is generally a byte, however, this is also system specific. For example, some C++ compilers can optimize booleans to take up single bits each. On some platforms (Very rare), booleans take up the default integer size for performance reasons. Most platforms would just make it a byte (Octet), though. The 'String' class is a bit of a special type, as it's basically a consistent interface for many different string implementations on each of the targets. It doesn't work the same way as most external types, as it's partially stack-based on some targets. The 'Array' type works similarly.

That being said, for portability reasons, Monkey doesn't have actual 'Byte', 'Short', or 'Long' types. All it has is 'Int' and 'Bool' as far as ALU support goes. This can be worked around on some targets by using external classes/other. Some people have already modified 'transcc' (Monkey's compiler) to have smaller integer types on some platforms, though. I did this a long time ago, but I didn't bother uploading or maintaining it. I made a module for abstraction reasons, but that's not really useful for most people.

The only reason you'd really want smaller integer types is for long-term storage, though. Performance-wise, 'Ints' are generally as fast, if not faster. Having 'Long' would be nice, but I wouldn't hold my breath. It's not really perfect, but I did start a 'sizeof' module before. It's a bit overly complicated, considering the nature of the module.

It's best not to care about type sizes, though. Well, unless you're writing something which needs to care about it. For example, a hashing algorithm, or perhaps you're dealing with pixel-data in integers.

EDIT 01: Added an extra bit about 'CPP_DOUBLE_PRECISION_FLOATS'.
EDIT 02: I should also mention that types in Monkey are preferred as little-endian. This isn't always the case, though. The Flash target had floating-point problems with this, if I remember right.


tiresius(Posted 2015) [#3]
Can you get byte-sized values by indexing a string?


Samah(Posted 2015) [#4]
@tiresius: Can you get byte-sized values by indexing a string?

No, because strings in Monkey are usually implemented with wide characters. Otherwise you wouldn't be able to do localisations to languages with non roman characters.

Also, variable types in Monkey all have set sizes if you're working purely within Monkey.


EdzUp(Posted 2015) [#5]
Ah I see I was talking about the basic values for each variable type

Int I know is 4 bytes
Bool should be 1 byte (minimum size a value can be allocated by cpu or something to that effect)
Float is four bytes
String minimum is eight for some reason (according to the docs)


Gerry Quinn(Posted 2015) [#6]
You can't be sure how Bool in particular is implemented, though I'd guess that on most targets nowadays it's a byte.

If you are worried about efficiency, you can use flags instead of Bools, and pack 32 into an Int.