Cast integer literals to target type
I ran into a case where the C++ compiler wants the suffix (or a cast): std::max(0, x)
does not compile if x
is of 64 bit integer type.
On my machine 32 bit is the correct threshold and I think this is fairly standard.
However, AFAIK it is technically platform dependent, so should we emit a cast to the target type instead?
Update: According to the standard long long (ll
suffix) guarantees at least 64 bit.
However, we might run into similar issues with smaller types.
So I updated the MR such that integer literals are always cast to the target type.
Starting with C99, an integer literal is automatically assigned a 64 bit type, if the number does not fit into 32 bit.
This means that we currently do not support integer literals >32 bit prior to C99.
A quarter century later, I think this is OK.
Merge request reports
Activity
requested review from @da15siwa
assigned to @he66coqe
However, AFAIK it is technically platform dependent, so should we emit a cast to the target type instead?
long
is 32 bits on Windows and 64 bits on Linux. So better do the cast. I guess we don't have Windows CI testing right now so our Windows support might already be broken, but we had it a while ago and we should probably restore it at some point.I thoroughly dislike introducing type casts here, but I agree that literal suffixes are not an option since they are not portable. (Why can't C have fixed-size literals like Rust?
) On the other hand, we're already doing the same thing with half-precision float literals.I came across the Function macros for minimum-width integer constants here and would just like to put them here for reference, but I don't think they are promising, either, since the actual type they produce is still platform-dependent.
So, grudgingly I agree that this is, for now, the best option.
I would like to share another idea, that relates to the C/C++ question (#37). Personally, I'd find it a lot cleaner if we didn't do C-style type casts, but proper C++ constructor calls (
uint32_t(42)
instead of((uint32_t) 42))
).Edited by Frederik HennigNo, I think to make the compilers treat our literals as the correct type, in C, casting them is the safest approach. It does clutter the emitted code, but since that code is only meant to be machine-consumed, that shouldn't really matter.
Also, as far as I can see, casts are the only way to force literals into a lower precision than that of
int
(same problem for fp16).mentioned in commit 90239d2c
mentioned in merge request !389 (merged)