Skip to content

Return a zero-filled register with uint8_t - #10

Open
zaitcev wants to merge 1 commit into
Mi-V-Soft-RISC-V:mainfrom
zaitcev:u-zaitcev-gcc-uint8-lbu
Open

Return a zero-filled register with uint8_t#10
zaitcev wants to merge 1 commit into
Mi-V-Soft-RISC-V:mainfrom
zaitcev:u-zaitcev-gcc-uint8-lbu

Conversation

@zaitcev

@zaitcev zaitcev commented Jun 24, 2026

Copy link
Copy Markdown

I found that the following code that fails:

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.

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 lbu instruction instead of the lb.

I don't know if this applies to shorts and ints, so for safety's sake, I didn't try to change those accessors.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant