Conversation
| v = v or v shr 4 | ||
| v = v or v shr 8 | ||
| v = v or v shr 16 | ||
| cast[int](lookup[uint32(v * 0x07C4ACDD'u32) shr 27]) |
There was a problem hiding this comment.
the cast here is intentional to avoid a runtime check of the returned value - so this should be cast[int](uint(lookup... - ditto other int casts below
There was a problem hiding this comment.
Isn't the compiler smart enough to not put checks that are impossible? uint8->int is always safe and doesn't require checks
There was a problem hiding this comment.
var xxx = 5'u8
var yyy: int = int(xxx)
echo yyy=>
N_LIB_PRIVATE NU8 xxx__test_1 = ((NU8)5);
N_LIB_PRIVATE NI yyy__test_2;
....
nimln_(2, "/tmp/test.nim");
yyy__test_2 = ((NI) (xxx__test_1));
nimln_(3, "/tmp/test.nim");There was a problem hiding this comment.
ah good point, if this indeed works - it should be able to do it, but that's one side of the coin only ;)
There was a problem hiding this comment.
Well, if it doesn't, and there's a reproducible test case, that sounds like a good candidate for a Nim bug to file. In the meantime, the correctness per se of removing casts wouldn't be in question, just efficiency, in versions of Nim which don't receive said backports.
There was a problem hiding this comment.
My motivation was this comment nim-lang/Nim#20103 (comment).
Is type cast for different number of bits well-defined in Nim?
There was a problem hiding this comment.
How about const lookup: array[32, int]?
There was a problem hiding this comment.
safeConvertint
#34 - this would / should be covered by the "lossless conversion" part of that framework - the idea generalises quite well.
I'm somewhat against putting "safe" in names frivolously without defining "safe" generally in the language - it gets overloaded for a bunch of confusing and sometimes contradictory purposes since "safe" on its own has no real meaning.
Since the language already warns against "unsafe" usage of conversions specifically, all we need to do is to turn the warning into an error once all known uses are fixed.
Is type cast for different number of bits well-defined in Nim?
no - that's why there's a warning for it now - casting to a larger type is UB (in other words: if ever you manage to cast to a larger type without a warning being printed, it's a bug that should be reported upstream).
There was a problem hiding this comment.
Since the language already warns against "unsafe" usage of conversions specifically, all we need to do is to turn the warning into an error once all known uses are fixed.
Oops, right, that was silly: it doesn't warn in all cases, specifically the ones we're discussing here :)
Ok, so I agree a construct would be useful that would allow the defect-free conversions.
There was a problem hiding this comment.
this would / should be covered by the "lossless conversion" part of that framework
👍, would definitively be nice to have this framework set up, could also include the work that was done on enum conversion #115
No description provided.