Skip to content

Fix integer overflow in single rounding requantization#220

Open
veblush wants to merge 1 commit into
ARM-software:mainfrom
veblush:single
Open

Fix integer overflow in single rounding requantization#220
veblush wants to merge 1 commit into
ARM-software:mainfrom
veblush:single

Conversation

@veblush
Copy link
Copy Markdown

@veblush veblush commented May 21, 2026

Problem

When CMSIS_NN_USE_SINGLE_ROUNDING is enabled, arm_nn_requantize suffered from an integer overflow. The intermediate value evaluated as new_val >> (total_shift - 1) can require up to 33 bits. Truncating this into an int32_t prematurely flipped the sign bit for large positive values, resulting in catastrophic output errors (e.g., yielding heavily negative numbers instead of saturated maximums).

Solution

  • Promoted the intermediate result variable from int32_t to int64_t to safely hold the 33-bit value.
  • Moved the int32_t cast to the final return after the >> 1 shift has brought the value safely back into 32-bit bounds.

Copy link
Copy Markdown
Contributor

@mansnils mansnils left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this Esun!

Could you also update the date/revision? New version v22.9.1.

const int64_t new_val = val * (int64_t)multiplier;

int32_t result = new_val >> (total_shift - 1);
int64_t result = new_val >> (total_shift - 1);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should work as well?
Avoiding INT32_MAX + 1 overflow.

const int64_t new_val = (int64_t)val * multiplier;
const int32_t total_shift = 31 - shift;
const int32_t result = (int32_t)(new_val >> (total_shift - 1));

return (result >> 1) + (result & 1);

And likely be more performant.

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.

2 participants