GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
arc

arc: Correct ctz(0) and clz(0) for ARCompact

Fixes incorrect results for count trailing zeros (ctz) and count leading zeros (clz) of zero on ARCompact.

This commit corrects the behavior of ctz(0) and clz(0) for the ARCompact architecture. While the instruction set defines these operations on zero to return 31, the emulation using the norm instruction on ARCompact incorrectly returned 32. The fix adjusts the emulation patterns for clzsi2 and ctzsi2 to produce the correct result of 31 for a zero operand, aligning with the architecture’s definition and preventing incorrect calculations.

In Details

For the ARCompact target, clzsi2 and ctzsi2 patterns are emulated using norm when ARCv2 fls/ffs instructions are not available. The CLZ_DEFINED_VALUE_AT_ZERO and CTZ_DEFINED_VALUE_AT_ZERO macros specify that clz(0) and ctz(0) should result in 31. However, the norm emulation incorrectly yields 31 for non-zero inputs and 32 for zero. The patch modifies the *arc_clzsi2 and arc_ctzsi2 patterns to increment the norm result only for strictly positive operands in clzsi2, and to explicitly produce 31 for zero in ctzsi2 via its negative-flag path.

For Context
ctz
Count Trailing Zeros. An operation that returns the number of zero bits following the least significant one bit in an integer. ctz(0) is often undefined or has a special value.
clz
Count Leading Zeros. An operation that returns the number of zero bits preceding the most significant one bit in an integer. clz(0) is often undefined or has a special value.
ARCompact
A specific instruction set architecture (ISA) variant of the ARC processor, often used in embedded systems.
Emulation
When a processor or software simulates the behavior of a different instruction set or hardware feature that is not natively supported. Here, ARCv2 instructions are emulated using other instructions.
Pattern
In compiler construction, a pattern refers to a specific structure or sequence of operations that a compiler tries to match within the intermediate representation to apply an optimization or generate specific machine code.
Filed Under: arctargetbugfixinteger