Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ULP bitshift operator starts being circular, when shifting by more than 16 bits (IDFGH-7237) #8831

Closed
niraami opened this issue Apr 25, 2022 · 2 comments
Assignees
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally

Comments

@niraami
Copy link

niraami commented Apr 25, 2022

Environment

  • Development Kit: none
  • Module or chip used: ESP32-WROOM-32E
  • IDF version: v5.0-dev-1471-gcda3063d02
  • Build System: idf.py
  • Compiler version: xtensa-esp32-elf-gcc (crosstool-NG esp-2021r2) 8.4.0

Problem Description

When using the logical shift left operator on the ULP coprocessor with a shift value (imm or Rsrc2) of more than 15, the shift suddenly becomes circular, even though a shift of 15 wasn't circular.

Here is the output from a quick MWE I wrote:

I (1821) lsh-test: [app_main]: bits:                    0000001000100110
I (1821) lsh-test: [app_main]: Left shift << 8:         0010011000000000
I (1821) lsh-test: [app_main]: Left shift << 15:        0000000000000000
I (1821) lsh-test: [app_main]: Left shift << 16:        0000001000100110
I (1831) lsh-test: [app_main]: Left shift << 17:        0000010001001100
I (1841) lsh-test: [app_main]: Left shift << 32:        0000001000100110

The documentation also doesn't mention the circularity of the shift operand anywhere, so I'd assumed it works the same that it does in C or C++ (non-circular). So this behavior is strange and undocumented.

Expected Behavior

Logical shift operator should be either circular, or not (preferably non-circular), so that the output above would look like this:

I (1821) lsh-test: [app_main]: bits:                    0000001000100110
I (1821) lsh-test: [app_main]: Left shift << 8:         0010011000000000
I (1821) lsh-test: [app_main]: Left shift << 15:        0000000000000000
I (1821) lsh-test: [app_main]: Left shift << 16:        0000000000000000
I (1831) lsh-test: [app_main]: Left shift << 17:        0000000000000000
I (1841) lsh-test: [app_main]: Left shift << 32:        0000000000000000

Actual Behavior

The left shift operator acts as if once the value 16 is reached, it resorts to not shifting at all, as if some kind of modulo 16 (%16) operation was taking place in the background.

Steps to reproduce

I've created a repository with an MWE that you can just clone and run here: https://github.com/niraami/esp32-lsh-bug

Code to reproduce this issue

https://gist.github.com/niraami/30b5cb6782acd4ca86f903570d133d58

Debug Logs

Log attached above in Problem Description

Other items if possible

sdkconfig.txt
elf.zip

@espressif-bot espressif-bot added the Status: Opened Issue is new label Apr 25, 2022
@github-actions github-actions bot changed the title ULP bitshift operator starts being circular, when shifting by more than 16 bits ULP bitshift operator starts being circular, when shifting by more than 16 bits (IDFGH-7237) Apr 25, 2022
@negativekelvin
Copy link
Contributor

Seems like it only uses the lower 4 bits of the shift value

@igrr
Copy link
Member

igrr commented May 7, 2022

Hi @niraami,
Thank you for pointing this out. We will update the documentation to mention this behavior!


The documentation also doesn't mention the circularity of the shift operand anywhere, so I'd assumed it works the same that it does in C or C++ (non-circular). So this behavior is strange and undocumented.

For what it's worth, neither C nor C++ standards guarantee any defined behavior when doing a left shift by an amount larger than the width of the type.

C:

The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined

C++:

The behavior is undefined if the right operand is negative, or greater than or equal to the length in bits of the promoted left operand.

@espressif-bot espressif-bot added Status: In Progress Work is in progress Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally Resolution: Done Issue is done internally and removed Status: Opened Issue is new Status: In Progress Work is in progress Resolution: NA Issue resolution is unavailable labels Sep 7, 2022
espressif-bot pushed a commit that referenced this issue Sep 15, 2022
This commit updates the documentation for ULP FSM LSH and RSH
operations.

Closes #8831
espressif-bot pushed a commit that referenced this issue Sep 22, 2022
This commit updates the documentation for ULP FSM LSH and RSH
operations.

Closes #8831
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally
Projects
None yet
Development

No branches or pull requests

6 participants