Return a zero-filled register with uint8_t - #10
Open
zaitcev wants to merge 1 commit into
Open
Conversation
Apparently, gcc expects a uint8_t to be zero-padded if it's
loaded into a register. A code that fails looks like this:
uint8_t addr;
uint64_t full_906 = (uint64_t)BUILD_GEN << (32 + 24);
full_906 |= ((uint64_t)pfI2cCfgs[2].slaveAddress) << (32 + 16);
addr = HW_get_8bit_reg(pfI2cCfgs[2].baseAddress, ADDRESS);
full_906 |= ((uint64_t)addr) << (32 + 8);
At this point, the value of full_906 is 0xFFFFFF8400000000
and the field of BUILD_GEN is destroyed. Observe,
there's no signed int sneaking in through promotion.
The sign-extension happens inside the assembly code.
Therefore, we fix it.
Signed-off-by: Pete Zaitcev <zaitcev@yahoo.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I found that the following code that fails:
At this point, the value of full_906 is 0xFFFFFF8400000000 and the field of BUILD_GEN is destroyed. Observe,
there's no signed int sneaking in through promotion. The sign-extension happens inside the assembly code.
Apparently, gcc expects a uint8_t to be zero-padded if it's loaded into a register. It does not add any additional masking before shifting.
An easy fix is to use the
lbuinstruction instead of thelb.I don't know if this applies to shorts and ints, so for safety's sake, I didn't try to change those accessors.