Skip to content

Fix signed overflow in SA-1 clock frequency calculation#4

Open
mrehkopf wants to merge 1 commit into
VitorVilela7:masterfrom
mrehkopf:signed-overflow-fix
Open

Fix signed overflow in SA-1 clock frequency calculation#4
mrehkopf wants to merge 1 commit into
VitorVilela7:masterfrom
mrehkopf:signed-overflow-fix

Conversation

@mrehkopf
Copy link
Copy Markdown

When the loop counter exceeds 32767, the hardware multiplier treats it as signed negative. Correct for this by conditionally adding the constant multipler to the upper 16 bits of the result (the loop counter is added unconditionally since the constant multiplier is always "signed negative")

Also fix the subsequent division: halve the dividend before division so it stays out of signed negative territory, then double the quotient and remainder of the result and adjust for odd dividends and "modulo overflow" (remainder >= quotient).

This allows the test to actually show frequencies between ~29 and ~59MHz. Before it would wrap to ~3MHz and scale strangely.

When the loop counter exceeds 32767, the hardware multiplier treats it as signed negative. Correct for this by conditionally adding the constant multipler to the upper 16 bits of the result (the loop counter is added unconditionally since the constant multiplier is always "signed negative")

Also fix the subsequent division:
halve the dividend before division so it stays out of signed negative territory, then double the quotient and remainder of the result and adjust for odd dividends and "modulo overflow" (remainder >= quotient).

This allows the test to actually show frequencies between ~29 and ~59MHz. Before it would wrap to ~3MHz and scale strangely.
@mrehkopf
Copy link
Copy Markdown
Author

(feel free to cut down on the comments inside the code; I chose verbosity partly to explain to myself what was happening 😅)

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