Skip to content

Fix integer overflow in single rounding requantization#220

Merged
mansnils merged 2 commits into
ARM-software:mainfrom
veblush:single
May 27, 2026
Merged

Fix integer overflow in single rounding requantization#220
mansnils merged 2 commits into
ARM-software:mainfrom
veblush:single

Conversation

@veblush
Copy link
Copy Markdown
Contributor

@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.

Comment thread Include/arm_nnsupportfunctions.h
@mansnils mansnils merged commit ece5a38 into ARM-software:main May 27, 2026
2 checks passed
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